DBA Data[Home] [Help]

PACKAGE BODY: APPS.INV_RESERVATION_PVT

Source


1 PACKAGE BODY inv_reservation_pvt AS
2   /* $Header: INVRSV3B.pls 120.59.12010000.3 2008/09/30 07:31:41 kkesavar ship $*/
3   --
4   g_pkg_name CONSTANT VARCHAR2(30) := 'Inv_reservation_pvt';
5   g_pkg_version CONSTANT VARCHAR2(100) := '$Header: INVRSV3B.pls 120.59.12010000.3 2008/09/30 07:31:41 kkesavar ship $';
6 
7   --
8   TYPE query_cur_ref_type IS REF CURSOR;
9 
10   g_is_pickrelease_set NUMBER;
11   g_debug NUMBER;
12   -- Added the below 3 variables for Bug Fix 5264987
13   g_oe_line_id	NUMBER;
14   g_project_id	NUMBER;
15   g_task_id	NUMBER;
16   --
17   -- procedure to print a message to dbms_output
18   -- disable by default since dbm_s_output.put_line is not allowed
19   PROCEDURE debug_print(p_message IN VARCHAR2, p_level IN NUMBER := 9) IS
20      --Bug: 3559328: Performance bug fix.The fnd call happens everytime
21      -- debug_print is called.
22     --l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
23   BEGIN
24     inv_log_util.TRACE(p_message, 'INV_RESERVATION_PVT', p_level);
25   END debug_print;
26 
27   --
28   PROCEDURE print_rsv_rec(p_rsv_rec IN inv_reservation_global.mtl_reservation_rec_type) IS
29     l_debug number;
30   BEGIN
31      -- Bug 2944896 -- Commenting out the parameters which may have
32      -- g_miss_char as the default value. This leads to truncation of the
33      -- log file.
34      --Bug 2955454. Removing the commented section. The values are printing
35      -- correctly even if it is g_miss_char.
36     -- Use cache to get value for l_debug
37     IF g_is_pickrelease_set IS NULL THEN
38        g_is_pickrelease_set := 2;
39        IF INV_CACHE.is_pickrelease THEN
40           g_is_pickrelease_set := 1;
41        END IF;
42     END IF;
43     IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
44        g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
45     END IF;
46 
47     l_debug := g_debug;
48     IF (l_debug = 1) THEN
49        debug_print('reservation_id              : '|| TO_CHAR(p_rsv_rec.reservation_id));
50        debug_print('requirement_date            : '|| TO_CHAR(p_rsv_rec.requirement_date, 'YYYY/MM/DD'));
51        debug_print('organization_id             : '|| TO_CHAR(p_rsv_rec.organization_id));
52        debug_print('inventory_item_id           : '|| TO_CHAR(p_rsv_rec.inventory_item_id));
53        debug_print('demand_source_type_id       : '|| TO_CHAR(p_rsv_rec.demand_source_type_id));
54        debug_print('demand_source_name          : '|| p_rsv_rec.demand_source_name);
55        debug_print('demand_source_header_id     : '|| TO_CHAR(p_rsv_rec.demand_source_header_id));
56        debug_print('demand_source_line_id       : '|| TO_CHAR(p_rsv_rec.demand_source_line_id));
57        debug_print('demand_source_line_detail   : '|| TO_CHAR(p_rsv_rec.demand_source_line_detail));
58        debug_print('primary_uom_code            : '|| p_rsv_rec.primary_uom_code);
59        debug_print('primary_uom_id              : '|| TO_CHAR(p_rsv_rec.primary_uom_id));
60        debug_print('reservation_uom_code        : '|| p_rsv_rec.reservation_uom_code);
61        debug_print('reservation_uom_id          : '|| TO_CHAR(p_rsv_rec.reservation_uom_id));
62        debug_print('secondary_uom_code          : '|| p_rsv_rec.secondary_uom_code);
63        debug_print('secondary_uom_id            : '|| TO_CHAR(p_rsv_rec.secondary_uom_id));
64        debug_print('reservation_quantity        : '|| TO_CHAR(p_rsv_rec.reservation_quantity));
65        debug_print('primary_reservation_quantity: '|| TO_CHAR(p_rsv_rec.primary_reservation_quantity));
66        debug_print('secondary_reservation_quantity: '|| TO_CHAR(p_rsv_rec.secondary_reservation_quantity));
67        debug_print('detailed_quantity: '|| TO_CHAR(p_rsv_rec.detailed_quantity));
68        debug_print('secondary_detailed_quantity: '|| TO_CHAR(p_rsv_rec.secondary_detailed_quantity));
69        debug_print('autodetail_group_id         : '|| TO_CHAR(p_rsv_rec.autodetail_group_id));
70        debug_print('external_source_code        : '|| p_rsv_rec.external_source_code);
71        debug_print('external_source_line_id     : '|| TO_CHAR(p_rsv_rec.external_source_line_id));
72        debug_print('supply_source_type_id       : '|| TO_CHAR(p_rsv_rec.supply_source_type_id));
73        debug_print('supply_source_header_id     : '|| TO_CHAR(p_rsv_rec.supply_source_header_id));
74        debug_print('supply_source_line_id       : '|| TO_CHAR(p_rsv_rec.supply_source_line_id));
75        debug_print('supply_source_name          : '|| (p_rsv_rec.supply_source_name));
76        debug_print('supply_source_line_detail   : '|| TO_CHAR(p_rsv_rec.supply_source_line_detail));
77        debug_print('revision                    : '|| p_rsv_rec.revision);
78        debug_print('subinventory_code           : '|| p_rsv_rec.subinventory_code);
79        debug_print('subinventory_id             : '|| TO_CHAR(p_rsv_rec.subinventory_id));
80        debug_print('locator_id                  : '|| TO_CHAR(p_rsv_rec.locator_id));
81        debug_print('lot_number                  : '|| p_rsv_rec.lot_number);
82        debug_print('lot_number_id               : '|| TO_CHAR(p_rsv_rec.lot_number_id));
83        debug_print('pick_slip_number            : '|| TO_CHAR(p_rsv_rec.pick_slip_number));
84        debug_print('lpn_id                      : '|| TO_CHAR(p_rsv_rec.lpn_id));
85        debug_print('ship_ready_flag             : '|| TO_CHAR(p_rsv_rec.ship_ready_flag));
86        debug_print('staged_flag                 : '|| p_rsv_rec.staged_flag);
87     END IF;
88   END print_rsv_rec;
89 
90 
91   -- helper procedure to get requested qty of wdd or order quantity from sales order
92   PROCEDURE get_requested_qty
93           ( p_demand_source_type_id     IN NUMBER
94           , p_demand_source_header_id   IN NUMBER
95           , p_demand_source_line_id     IN NUMBER
96           , p_demand_source_line_detail IN NUMBER
97           , p_project_id                IN NUMBER
98           , p_task_id                   IN NUMBER
99           , x_requested_qty             OUT NOCOPY NUMBER
100           )
101   IS
102     l_debug         NUMBER;
103     l_requested_qty NUMBER := 0;
104   BEGIN
105 
106     IF (g_debug IS NULL) THEN
107         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
108     END IF;
109 
110     l_debug := g_debug;
111 
112     IF (l_debug = 1) THEN
113        debug_print('In get_requested_qty');
114        debug_print('p_demand_source_type_id = ' || p_demand_source_type_id);
115        debug_print('p_demand_source_header_id = ' || p_demand_source_header_id);
116        debug_print('p_demand_source_line_id = ' || p_demand_source_line_id);
117        debug_print('p_demand_source_line_detail = ' || p_demand_source_line_detail);
118        debug_print('p_project_id = ' || p_project_id);
119        debug_print('p_task_id = ' || p_task_id);
120     END IF;
121 
122     IF (p_demand_source_line_detail IS NOT NULL) THEN
123       BEGIN
124 	 IF (l_debug = 1) THEN
125 	    debug_print('Inside source line as not null');
126 	 END IF;
127 	 SELECT   nvl(sum(requested_quantity),0)
128 	   INTO   l_requested_qty
129 	   FROM   wsh_delivery_details
130 	   WHERE  source_line_id = p_demand_source_line_id
131 	   AND    delivery_detail_id = p_demand_source_line_detail;
132 
133       EXCEPTION
134 	 WHEN OTHERS THEN
135 	    l_requested_qty := 0;
136 	    IF (l_debug = 1) THEN
137 	       debug_print('Exception in finding wdd');
138 	    END IF;
139       END;
140     ELSE
141       BEGIN
142 	 SELECT   ordered_quantity
143 	   INTO   l_requested_qty
144 	   FROM   oe_order_lines_all
145 	   WHERE  line_id = p_demand_source_line_id
146            AND    nvl(project_id, -99) = nvl(p_project_id, -99)
147            AND    nvl(task_id, -99) = nvl(p_task_id, -99);
148 
149       EXCEPTION
150 	 WHEN no_data_found THEN
151 	    l_requested_qty := 0;
152 	    IF (l_debug = 1) THEN
153 	       debug_print('No order line found');
154 	    END IF;
155       END;
156     END IF;
157 
158     IF (l_debug = 1) THEN
159        debug_print('l_requested_qty = ' || l_requested_qty);
160     END IF;
161 
162     x_requested_qty := l_requested_qty;
163 
164   EXCEPTION
165     WHEN fnd_api.g_exc_error THEN
166         IF (l_debug = 1) THEN
167             debug_print('excepted error');
168         END IF;
169         x_requested_qty := 0;
170         --
171     WHEN fnd_api.g_exc_unexpected_error THEN
172         IF (l_debug = 1) THEN
173             debug_print('unexpected error');
174         END IF;
175         x_requested_qty := 0;
176         --
177     WHEN OTHERS THEN
178         IF (l_debug = 1) THEN
179             debug_print('others error');
180         END IF;
181         x_requested_qty := 0;
182 
183   END get_requested_qty;
184 
185   -- helper procedure called from update_reservation and
186   -- transfer_reservation to get available to reserve qty
187   -- for the supply source.
188   PROCEDURE get_supply_reservable_qty
189     ( x_return_status                OUT NOCOPY VARCHAR2
190       , x_msg_count                    OUT NOCOPY NUMBER
191       , x_msg_data                     OUT NOCOPY VARCHAR2
192       , p_fm_supply_source_type_id     IN NUMBER
193       , p_fm_supply_source_header_id   IN NUMBER
194       , p_fm_supply_source_line_id     IN NUMBER
195       , p_fm_supply_source_line_detail IN NUMBER
196       , p_fm_primary_reservation_qty   IN NUMBER
197       , p_to_supply_source_type_id     IN NUMBER
198       , p_to_supply_source_header_id   IN NUMBER
199       , p_to_supply_source_line_id     IN NUMBER
200       , p_to_supply_source_line_detail IN NUMBER
201       , p_to_primary_reservation_qty   IN NUMBER
202       , p_to_organization_id           IN NUMBER
203       , p_to_inventory_item_id         IN NUMBER
204       , p_to_revision                  IN VARCHAR2
205       , p_to_lot_number                IN VARCHAR2
206       , p_to_subinventory_code         IN VARCHAR2
207       , p_to_locator_id                IN NUMBER
208       , p_to_lpn_id                    IN NUMBER
209     , p_to_project_id                IN NUMBER
210     , p_to_task_id                   IN NUMBER
211     , x_reservable_qty               OUT NOCOPY NUMBER
212     , x_qty_available                OUT NOCOPY NUMBER
213     )
214     IS
215        l_debug                    NUMBER;
216        l_qty_available_to_reserve NUMBER;
217        l_qty_available            NUMBER;
218        l_reservable_qty           NUMBER;
219   BEGIN
220 
221      IF (g_debug IS NULL) THEN
222         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
223      END IF;
224 
225      l_debug := g_debug;
226 
227      IF (l_debug = 1) THEN
228 	debug_print('In get_supply_reservable_qty');
229 	debug_print('Orig supply source type id = ' || p_fm_supply_source_type_id);
230 	debug_print('Orig supply source header id = ' || p_fm_supply_source_header_id);
231 	debug_print('Orig supply source line id = ' || p_fm_supply_source_line_id);
232 	debug_print('Orig supply line detail  = ' || p_fm_supply_source_line_detail);
233 	debug_print('Orig primary qty = ' || p_fm_primary_reservation_qty);
234 	debug_print('To supply source type id = ' || p_to_supply_source_type_id);
235 	debug_print('To supply source header id = ' || p_to_supply_source_header_id);
236 	debug_print('To supply source line id = ' || p_to_supply_source_line_id);
237 	debug_print('To supply line detail = ' || p_to_supply_source_line_detail);
238 	debug_print('To primary qty = ' || p_to_primary_reservation_qty);
239      END IF;
240 
241      inv_reservation_avail_pvt.available_supply_to_reserve
242       (
243        x_return_status                 => x_return_status
244        , x_msg_count                     => x_msg_count
245        , x_msg_data                      => x_msg_data
246        , x_qty_available_to_reserve      => l_qty_available_to_reserve
247        , x_qty_available                 => l_qty_available
248        , p_organization_id               => p_to_organization_id
249        , p_item_id                       => p_to_inventory_item_id
250        , p_revision                      => p_to_revision
251        , p_lot_number                    => p_to_lot_number
252        , p_subinventory_code             => p_to_subinventory_code
253        , p_locator_id                    => p_to_locator_id
254        , p_lpn_id                        => p_to_lpn_id
255        , p_fm_supply_source_type_id      => p_fm_supply_source_type_id
256        , p_supply_source_type_id         => p_to_supply_source_type_id
257        , p_supply_source_header_id       => p_to_supply_source_header_id
258        , p_supply_source_line_id         => p_to_supply_source_line_id
259        , p_supply_source_line_detail     => Nvl(p_to_supply_source_line_detail,fnd_api.g_miss_num)
260        , p_project_id                    => p_to_project_id
261        , p_task_id                       => p_to_task_id
262        , p_api_version_number            => 1.0
263        , p_init_msg_lst                  => fnd_api.g_false
264        );
265 
266      IF (l_debug = 1) THEN
267 	debug_print('After calling available supply to reserve ' || x_return_status);
268 	debug_print('Available quantity to reserve. l_qty_available_to_reserve: ' || l_qty_available_to_reserve);
269 	debug_print('Available quantity on the document. l_qty_available: ' || l_qty_available);
270      END IF;
271 
272      --
273      IF x_return_status = fnd_api.g_ret_sts_error THEN
274 	RAISE fnd_api.g_exc_error;
275     END IF;
276 
277     --
278     IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
279        RAISE fnd_api.g_exc_unexpected_error;
280     END IF;
281 
282     IF (p_fm_supply_source_type_id = p_to_supply_source_type_id AND
283 	p_fm_supply_source_header_id = p_to_supply_source_header_id AND
284 	nvl(p_fm_supply_source_line_id, -1) = nvl(p_to_supply_source_line_id, -1) AND
285 	nvl(p_fm_supply_source_line_detail, -1) = nvl(p_to_supply_source_line_detail, -1)) THEN
286 
287        -- if supply of orig and to record is the same, we need to add the qty from orig
288        -- record to the reservable qty because we're transfering to same supply source
289        l_reservable_qty := nvl(l_qty_available_to_reserve,0) + p_fm_primary_reservation_qty;
290 
291      ELSE
292        -- if transfer reservation from other supply to receiving, the total qty in rcv
293        -- will increase, so we need to add the qty from orig record to the reservable qty
294        IF (p_to_supply_source_type_id = inv_reservation_global.g_source_type_rcv) THEN
295           l_reservable_qty := nvl(l_qty_available_to_reserve,0) + p_fm_primary_reservation_qty;
296 	ELSE
297           l_reservable_qty := nvl(l_qty_available_to_reserve,0);
298        END IF;
299 
300     END IF;
301 
302     x_reservable_qty := l_reservable_qty;
303     x_qty_available := l_qty_available;
304 
305   EXCEPTION
306      WHEN fnd_api.g_exc_error THEN
307 	x_return_status  := fnd_api.g_ret_sts_error;
308 	x_reservable_qty := 0;
309 	x_qty_available := 0;
310 	--
311      WHEN fnd_api.g_exc_unexpected_error THEN
312 	x_return_status  := fnd_api.g_ret_sts_unexp_error;
313 	x_reservable_qty := 0;
314 	x_qty_available := 0;
315 	--
316      WHEN OTHERS THEN
317 	x_return_status  := fnd_api.g_ret_sts_unexp_error;
318 	x_reservable_qty := 0;
319 	x_qty_available := 0;
320 	--
321       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
322 	 fnd_msg_pub.add_exc_msg(g_pkg_name, 'get_supply_reservable_qty');
323       END IF;
324 
325   END get_supply_reservable_qty;
326 
327   -- helper procedure called from update_reservation and
328   -- transfer_reservation to get available to reserve qty
329   -- for the demand source
330   PROCEDURE get_demand_reservable_qty
331     ( x_return_status                OUT NOCOPY VARCHAR2
332       , x_msg_count                    OUT NOCOPY NUMBER
333       , x_msg_data                     OUT NOCOPY VARCHAR2
334       , p_fm_demand_source_type_id     IN NUMBER
335       , p_fm_demand_source_header_id   IN NUMBER
336       , p_fm_demand_source_line_id     IN NUMBER
337       , p_fm_demand_source_line_detail IN NUMBER
338       , p_fm_primary_reservation_qty   IN NUMBER
339       , p_to_demand_source_type_id     IN NUMBER
340       , p_to_demand_source_header_id   IN NUMBER
341       , p_to_demand_source_line_id     IN NUMBER
342       , p_to_demand_source_line_detail IN NUMBER
343       , p_to_primary_reservation_qty   IN NUMBER
344       , p_to_organization_id           IN NUMBER
345       , p_to_inventory_item_id         IN NUMBER
346       , p_to_primary_uom_code          IN VARCHAR
347       , p_to_project_id                IN NUMBER
348       , p_to_task_id                   IN NUMBER
349       , x_reservable_qty               OUT NOCOPY NUMBER
350       , x_qty_available                OUT NOCOPY NUMBER
351       )
352     IS
353        l_debug                    NUMBER;
354        l_reservable_qty           NUMBER;
355        l_qty_available_to_reserve NUMBER;
356        l_qty_available            NUMBER;
357        l_requested_qty            NUMBER;
358        l_reservation_margin_above NUMBER;
359   BEGIN
360 
361     IF (g_debug IS NULL) THEN
362        g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
363     END IF;
364 
365     l_debug := g_debug;
366 
367     IF (l_debug = 1) THEN
368        debug_print('In get_demand_reservable_qty');
369        debug_print('Orig demand source type id = ' || p_fm_demand_source_type_id);
370        debug_print('Orig demand source header id = ' || p_fm_demand_source_header_id);
371        debug_print('Orig demand source line id = ' || p_fm_demand_source_line_id);
372        debug_print('Orig demand line detail  = ' || p_fm_demand_source_line_detail);
373        debug_print('Orig primary qty = ' || p_fm_primary_reservation_qty);
374        debug_print('To demand source type id = ' || p_to_demand_source_type_id);
375        debug_print('To demand source header id = ' || p_to_demand_source_header_id);
376        debug_print('To demand source line id = ' || p_to_demand_source_line_id);
377        debug_print('To demand line detail = ' || p_to_demand_source_line_detail);
378        debug_print('To primary qty = ' || p_to_primary_reservation_qty);
379        debug_print('To primary uom code = ' || p_to_primary_uom_code);
380     END IF;
381 
382     inv_reservation_avail_pvt.available_demand_to_reserve
383       (
384        x_return_status                 => x_return_status
385        , x_msg_count                     => x_msg_count
386        , x_msg_data                      => x_msg_data
387        , x_qty_available_to_reserve      => l_qty_available_to_reserve
388        , x_qty_available                 => l_qty_available
389        , p_organization_id               => p_to_organization_id
390        , p_item_id                       => p_to_inventory_item_id
391        , p_primary_uom_code              => p_to_primary_uom_code
392        , p_demand_source_type_id         => p_to_demand_source_type_id
393        , p_demand_source_header_id       => p_to_demand_source_header_id
394        , p_demand_source_line_id         => p_to_demand_source_line_id
395        , p_demand_source_line_detail     => Nvl(p_to_demand_source_line_detail,fnd_api.g_miss_num)
396        , p_project_id                    => p_to_project_id
397        , p_task_id                       => p_to_task_id
398        , p_api_version_number            => 1.0
399        , p_init_msg_lst                  => fnd_api.g_false
400        );
401 
402 
403     IF (l_debug = 1) THEN
404        debug_print('After calling available demand to reserve ' || x_return_status);
405        debug_print('Available quantity to reserve. l_qty_available_to_reserve: ' || l_qty_available_to_reserve);
406        debug_print('Available quantity on the document. l_qty_available: ' || l_qty_available);
407     END IF;
408 
409     --
410     IF x_return_status = fnd_api.g_ret_sts_error THEN
411        RAISE fnd_api.g_exc_error;
412     END IF;
413 
414     --
415     IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
416        RAISE fnd_api.g_exc_unexpected_error;
417     END IF;
418 
419     IF (p_fm_demand_source_type_id = p_to_demand_source_type_id AND
420 	p_fm_demand_source_header_id = p_to_demand_source_header_id AND
421 	nvl(p_fm_demand_source_line_id, -1) = nvl(p_to_demand_source_line_id, -1) AND
422        nvl(p_fm_demand_source_line_detail, -1) = nvl(p_to_demand_source_line_detail, -1)) THEN
423 
424        -- if demand of orig and to record is the same, we need to add the qty from orig
425        -- record to the reservable qty because we're transfering to same demand source
426        l_reservable_qty := l_qty_available_to_reserve + p_fm_primary_reservation_qty;
427 
428      ELSE
429 
430        --for sales order or internal order, the reservable qty is the minimum of
431        --requested qty of the line detail/line and the reservable qty of the sales order line
432        IF (p_to_demand_source_type_id in (inv_reservation_global.g_source_type_oe,
433                                           inv_reservation_global.g_source_type_internal_ord,
434 					  inv_reservation_global.g_source_type_rma) AND
435 	   p_fm_demand_source_type_id = p_to_demand_source_type_id AND
436 	   p_fm_demand_source_header_id = p_to_demand_source_header_id AND
437 	   nvl(p_fm_demand_source_line_id, -1) = nvl(p_to_demand_source_line_id, -1) AND
438           nvl(p_fm_demand_source_line_detail, -1) <> nvl(p_to_demand_source_line_detail, -1)) THEN
439 
440           get_requested_qty
441 	    ( p_demand_source_type_id     => p_to_demand_source_type_id
442 	      , p_demand_source_header_id   => p_to_demand_source_header_id
443 	      , p_demand_source_line_id     => p_to_demand_source_line_id
444 	      , p_demand_source_line_detail => p_to_demand_source_line_detail
445 	      , p_project_id                => p_to_project_id
446 	      , p_task_id                   => p_to_task_id
447 	      , x_requested_qty             => l_requested_qty
448 	      );
449 
450 
451           IF (l_debug = 1) THEN
452              debug_print('l_requested_qty = ' || l_requested_qty);
453              debug_print('reservable qty = ' || (l_qty_available_to_reserve + p_fm_primary_reservation_qty));
454           END IF;
455 
456           IF (nvl(l_requested_qty, 0) < (l_qty_available_to_reserve + p_fm_primary_reservation_qty)) THEN
457              l_reservable_qty := nvl(l_requested_qty, 0);
458 	   ELSE
459              l_reservable_qty := l_qty_available_to_reserve + p_fm_primary_reservation_qty;
460           END IF;
461 
462 	ELSE
463           l_reservable_qty := l_qty_available_to_reserve;
464 
465        END IF;
466 
467     END IF;
468 
469     IF (p_to_demand_source_type_id in (inv_reservation_global.g_source_type_oe,
470                                           inv_reservation_global.g_source_type_internal_ord,
471 				       inv_reservation_global.g_source_type_rma)) THEN
472 
473        IF NOT (lot_divisible
474 	       (p_inventory_item_id => p_to_inventory_item_id,
475 		p_organization_id => p_to_organization_id)) THEN
476 	  get_ship_qty_tolerance
477 	    (
478 	     p_api_version_number          =>  1.0
479 	     , p_init_msg_lst              =>  fnd_api.g_false
480 	     , x_return_status             => x_return_status
481 	     , x_msg_count                 => x_msg_count
482 	     , x_msg_data                  => x_msg_data
483 	     , p_demand_type_id            => p_to_demand_source_type_id
484 	     , p_demand_header_id          => p_to_demand_source_header_id
485 	     , p_demand_line_id            => p_to_demand_source_line_id
486 	     , x_reservation_margin_above  => l_reservation_margin_above);
487 
488 	  IF (l_debug = 1) THEN
489 	     debug_print('Inside is lot indivisible');
490 	  END IF;
491 
492 	  IF (l_debug = 1) THEN
493 	     debug_print('After calling get_ship_qty_tolerance ' || x_return_status);
494 	     debug_print('Reservation margin above ' || l_reservation_margin_above);
495 	  END IF;
496 
497 	  --
498 	  IF x_return_status = fnd_api.g_ret_sts_error THEN
499 	     RAISE fnd_api.g_exc_error;
500 	  END IF;
501 
502 	  --
503 	  IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
504 	     RAISE fnd_api.g_exc_unexpected_error;
505 	  END IF;
506 
507 	  l_reservable_qty := l_reservable_qty + l_reservation_margin_above;
508 	  l_qty_available := l_qty_available + l_reservation_margin_above;
509        END IF;
510     END IF;
511 
512     x_reservable_qty := l_reservable_qty;
513     x_qty_available := l_qty_available;
514 
515   EXCEPTION
516      WHEN fnd_api.g_exc_error THEN
517 	x_return_status  := fnd_api.g_ret_sts_error;
518 	x_reservable_qty := 0;
519 	x_qty_available  := 0;
520 	--
521     WHEN fnd_api.g_exc_unexpected_error THEN
522        x_return_status  := fnd_api.g_ret_sts_unexp_error;
523        x_reservable_qty := 0;
524        x_qty_available  := 0;
525        --
526      WHEN OTHERS THEN
527 	x_return_status  := fnd_api.g_ret_sts_unexp_error;
528 	x_reservable_qty := 0;
529 	x_qty_available  := 0;
530 	--
531 	IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
532 	   fnd_msg_pub.add_exc_msg(g_pkg_name, 'get_demand_reservable_qty');
533       END IF;
534 
535   END get_demand_reservable_qty;
536 
537  --This procedure will compute the shipping tolerance for sales order,
538   -- internal order and RMA and return the quantity in the primary uom of
539   -- item
540   PROCEDURE get_ship_qty_tolerance
541   (
542      p_api_version_number            IN  NUMBER
543    , p_init_msg_lst                  IN  VARCHAR2  Default Fnd_API.G_False
544    , x_return_status                 OUT NOCOPY VARCHAR2
545    , x_msg_count                     OUT NOCOPY NUMBER
546    , x_msg_data                      OUT NOCOPY VARCHAR2
547    , p_demand_type_id                IN  NUMBER
548    , p_demand_header_id              IN  NUMBER
549    , p_demand_line_id                IN  NUMBER
550    , x_reservation_margin_above      OUT NOCOPY NUMBER                   -- INVCONV
551    ) is
552       l_api_version_number  CONSTANT NUMBER       := 1.0;
553       l_api_name            CONSTANT VARCHAR2(30) := 'get_ship_qty_tolerance';
554       l_debug NUMBER;
555       l_primary_uom_code  	     	VARCHAR2(3);
556       l_ship_tolerance_above            NUMBER;          -- INVCONV
557       l_line_rec_inventory_item_id      oe_order_lines_all.inventory_item_id%TYPE;
558       l_line_rec_ordered_quantity       oe_order_lines_all.ordered_quantity%TYPE;
559       l_line_rec_order_quantity_uom     oe_order_lines_all.order_quantity_uom%TYPE;
560       l_line_rec_org_id                 oe_order_lines_all.org_id%TYPE;
561       l_ordered_quantity_primary_uom NUMBER;
562   BEGIN
563 
564      IF (g_debug IS NULL) THEN
565 	g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
566      END IF;
567 
568      l_debug := g_debug;
569 
570      -- Initialize return status
571      x_return_status := fnd_api.g_ret_sts_success;
572 
573      --  Standard call to check for call compatibility
574      IF NOT fnd_api.compatible_api_call(l_api_version_number
575 					, p_api_version_number
576 					, l_api_name
577 					, G_PKG_NAME
578 					) THEN
579 	RAISE fnd_api.g_exc_unexpected_error;
580      END IF;
581      --
582      --  Initialize message list.
583      IF fnd_api.to_boolean(p_init_msg_lst) THEN
584 	fnd_msg_pub.initialize;
585      END IF;
586 
587      IF p_demand_type_id IN (inv_reservation_global.g_source_type_oe,
588 			     inv_reservation_global.g_source_type_internal_ord,
589 			     inv_reservation_global.g_source_type_rma) then
590 
591 	--INVCONV - Retrieve ship tolerance above for lot indivisible scenarios
592 	BEGIN
593 	   SELECT inventory_item_id, ordered_quantity, order_quantity_uom, ship_from_org_id,
594 	     ship_tolerance_above
595 	     INTO l_line_rec_inventory_item_id,
596 	     l_line_rec_ordered_quantity,
597 	     l_line_rec_order_quantity_uom,
598 	     l_line_rec_org_id,
599 	     l_ship_tolerance_above
600 	     FROM oe_order_lines_all
601 	     WHERE line_id = p_demand_line_id;
602 	EXCEPTION
603 	   WHEN no_data_found THEN
604 	      IF (l_debug =1) THEN
605 		 debug_print('could not find the record for sales order line ' || p_demand_line_id);
606 	      END IF;
607 	END;
608 
609 	-- Get primary UOM of the item
610 	BEGIN
611 	   select primary_uom_code
612 	     into l_primary_uom_code
613 	     from mtl_system_items
614 	     where organization_id   = l_line_rec_org_id
615 	     and   inventory_item_id = l_line_rec_inventory_item_id;
616 	 EXCEPTION
617 	   WHEN no_data_found THEN
618 	      IF (l_debug =1) THEN
619 	debug_print('could not find the record for the item id ' || l_line_rec_inventory_item_id || ' Org ' ||l_line_rec_org_id );
620 	      END IF;
621 	END;
622 
623 	IF l_primary_uom_code = l_line_rec_order_quantity_uom THEN
624 	   x_reservation_margin_above := l_line_rec_ordered_quantity * NVL(l_ship_tolerance_above,0) / 100;
625 	IF (l_debug =1) THEN
626 	       debug_print('quantity no convert');
627 	       debug_print('margin above :' || x_reservation_margin_above);
628 	   END IF;
629 	 ELSE -- the uoms are different. convert the order qty into primary
630 	   -- uom of the ite,
631 
632 	   -- Convert order quantity into primary uom code
633 	   l_ordered_quantity_primary_uom :=
634 	     inv_convert.inv_um_convert
635 	     (
636 	      l_line_rec_inventory_item_id,
637 	      NULL,
638 	      l_line_rec_ordered_quantity,
639 	      l_line_rec_order_quantity_uom,
640 	      l_primary_uom_code,
641 	      NULL,
642 	      NULL);
643 
644 	   x_reservation_margin_above := l_ordered_quantity_primary_uom *
645 	     NVL(l_ship_tolerance_above,0) / 100;
646 
647 	   IF (l_debug =1) THEN
648 	      debug_print('quantity after convert :' || l_ordered_quantity_primary_uom);
649 	      debug_print('margin above :' || x_reservation_margin_above);
650 	   END IF;
651 
652 	END IF;
653 
654      END IF;
655      x_return_status := fnd_api.g_ret_sts_success;
656 
657   EXCEPTION
658 
659      WHEN fnd_api.g_exc_error THEN
660         x_return_status := fnd_api.g_ret_sts_error;
661 
662         --  Get message count and data
663         fnd_msg_pub.count_and_get
664           (  p_count => x_msg_count
665 	     , p_data  => x_msg_data
666 	     , p_encoded => 'F'
667            );
668 
669      WHEN fnd_api.g_exc_unexpected_error THEN
670         x_return_status := fnd_api.g_ret_sts_unexp_error ;
671 
672         --  Get message count and data
673         fnd_msg_pub.count_and_get
674           (  p_count  => x_msg_count
675 	     , p_data   => x_msg_data
676 	     , p_encoded => 'F'
677 	     );
678 
679      WHEN OTHERS THEN
680         x_return_status := fnd_api.g_ret_sts_unexp_error ;
681 
682         IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
683           THEN
684            fnd_msg_pub.add_exc_msg
685              (  g_pkg_name
686 		, l_api_name
687 		);
688         END IF;
689 
690         --  Get message count and data
691         fnd_msg_pub.count_and_get
692           (  p_count  => x_msg_count
693 	     , p_data   => x_msg_data
694 	     , p_encoded => 'F'
695              );
696   END Get_Ship_qty_Tolerance ;
697 
698   --
699   -- Procedure
700   --   convert_quantity
701   -- Description
702   --   convert quantity from reservation uom to primary uom or
703   --   convert quantity from primary uom to reservation uom
704   --   and store the quantity in the corresponding field in
705   --   the record
706   -- Requirement
707   --   In px_rsv_rec, inventory_item_id must be valid;
708   --   either primary_uom_code, primary_reservation_quantity
709   --   or reservation_uom_code, reservation_quantity must be not null and valid
710   PROCEDURE convert_quantity(x_return_status OUT NOCOPY VARCHAR2, px_rsv_rec IN OUT NOCOPY inv_reservation_global.mtl_reservation_rec_type) IS
711     l_return_status    VARCHAR2(1)                                     := fnd_api.g_ret_sts_success;
712     --l_rsv_rec          inv_reservation_global.mtl_reservation_rec_type;
713     l_primary_uom_code VARCHAR2(3);
714     l_tmp_secondary_quantity     NUMBER                                          := NULL; -- INVCONV
715     l_tmp_quantity     NUMBER                                          := NULL;
716     l_tracking_quantity_ind VARCHAR2(30);   --INVCONV
717   BEGIN
718     --l_rsv_rec        := px_rsv_rec;
719 
720       --
721     -- INVCONV - Retrieve secondary uom
722     IF px_rsv_rec.primary_uom_code IS NULL or px_rsv_rec.secondary_uom_code IS NULL THEN
723        SELECT primary_uom_code, secondary_uom_code,tracking_quantity_ind
724          INTO px_rsv_rec.primary_uom_code, px_rsv_rec.secondary_uom_code,l_tracking_quantity_ind
725          FROM mtl_system_items
726         WHERE inventory_item_id = px_rsv_rec.inventory_item_id
727           AND organization_id = px_rsv_rec.organization_id;
728     END IF;
729 
730     /* it's possible that Secondary UOM is defined for the item but the item is tracked only in Primary */
731     IF(l_tracking_quantity_ind <> 'PS')  THEN --INVCONV
732       px_rsv_rec.secondary_uom_code := NULL;
733     END IF;
734 
735     --
736     --
737     -- convert reservation quantity in reservation uom
738     -- to primary quantity in primary uom if needed
739     IF px_rsv_rec.primary_reservation_quantity IS NULL THEN
740       -- get primary uom code for the item and org
741       --
742       -- compute the primary quantity
743       -- INVCONV - upgrade call to inv_um_convert
744       l_tmp_quantity                          := inv_convert.inv_um_convert(
745                                                    item_id                      => px_rsv_rec.inventory_item_id
746                                                  , lot_number                   => px_rsv_rec.lot_number
747                                                  , organization_id              => px_rsv_rec.organization_id
748                                                  , PRECISION                    => NULL -- use default precision
749                                                  , from_quantity                => px_rsv_rec.reservation_quantity
750                                                  , from_unit                    => px_rsv_rec.reservation_uom_code
751                                                  , to_unit                      => px_rsv_rec.primary_uom_code
752                                                  , from_name                    => NULL -- from uom name
753                                                  , to_name                      => NULL -- to uom name
754                                                  );
755 
756       IF l_tmp_quantity = -99999 THEN
757         -- conversion failed
758         fnd_message.set_name('INV', 'CAN-NOT-CONVERT-TO-PRIMARY-UOM');
759         fnd_msg_pub.ADD;
760         RAISE fnd_api.g_exc_error;
761       END IF;
762 
763       --
764       px_rsv_rec.primary_reservation_quantity  := l_tmp_quantity;
765     --
766     END IF;
767 
768     --
769     -- convert reservation quantity in primary_uom_code
770     -- to reservation quantity in reservation uom code if needed
771     IF px_rsv_rec.reservation_uom_code IS NULL THEN
772       px_rsv_rec.reservation_uom_code  := px_rsv_rec.primary_uom_code;
773       px_rsv_rec.reservation_quantity  := px_rsv_rec.primary_reservation_quantity;
774     ELSIF  px_rsv_rec.reservation_quantity IS NULL
775            AND px_rsv_rec.primary_reservation_quantity IS NOT NULL THEN
776       -- if reservation_quantity is missing or both
777       -- reservation_quantity and primary_reservation_quantity are
778       -- present, we will compute the reservation quantity based
779       -- on the primary reservation quantity
780       -- Bug 1914778 - changed ELSIF so that reservation_quantity
781       -- is calculated again only if it is null
782       -- Bug 2116332 - only call inv_convert if UOMs are different
783       IF px_rsv_rec.primary_uom_code = px_rsv_rec.reservation_uom_code THEN
784         l_tmp_quantity  := px_rsv_rec.primary_reservation_quantity;
785       ELSE
786         -- INVCONV upgrade inv_um_convert call
787         l_tmp_quantity  := inv_convert.inv_um_convert(
788                              item_id                      => px_rsv_rec.inventory_item_id
789                            , lot_number                   => px_rsv_rec.lot_number
790                            , organization_id              => px_rsv_rec.organization_id
791                            , PRECISION                    => NULL -- use default precision
792                            , from_quantity                => px_rsv_rec.primary_reservation_quantity
793                            , from_unit                    => px_rsv_rec.primary_uom_code
794                            , to_unit                      => px_rsv_rec.reservation_uom_code
795                            , from_name                    => NULL -- from uom name
796                            , to_name                      => NULL -- to uom name
797                            );
798 
799         IF l_tmp_quantity = -99999 THEN
800           -- conversion failed
801           fnd_message.set_name('INV', 'CAN-NOT-CONVERT-TO-RSV-UOM');
802           fnd_msg_pub.ADD;
803           RAISE fnd_api.g_exc_error;
804         END IF;
805       END IF;
806 
807       --
808       px_rsv_rec.reservation_quantity  := l_tmp_quantity;
809     END IF;
810 
811      -- INVCONV BEGIN
812          -- If dual control and secondary quantity is missing, calculate it
813          IF px_rsv_rec.secondary_uom_code IS NOT NULL AND
814            px_rsv_rec.secondary_reservation_quantity IS NULL THEN
815            l_tmp_secondary_quantity  := inv_convert.inv_um_convert(
816                                          item_id                      => px_rsv_rec.inventory_item_id
817                                        , lot_number                   => px_rsv_rec.lot_number
818                                        , organization_id              => px_rsv_rec.organization_id
819                                        , PRECISION                    => NULL -- use default precision
820                                        , from_quantity                => px_rsv_rec.primary_reservation_quantity
821                                       , from_unit                    => px_rsv_rec.primary_uom_code
822                                       , to_unit                      => px_rsv_rec.secondary_uom_code
823                                       , from_name                    => NULL -- from uom name
824                                        , to_name                      => NULL -- to uom name
825                                        );
826 
827           IF l_tmp_secondary_quantity = -99999 THEN
828                -- conversion failed
829                fnd_message.set_name('INV', 'CAN-NOT-CONVERT-TO-SECOND-UOM'); -- INVCONV NEW MESSAGE
830                fnd_msg_pub.ADD;
831             RAISE fnd_api.g_exc_error;
832            END IF;
833            px_rsv_rec.secondary_reservation_quantity  := l_tmp_secondary_quantity; -- INVCONV
834          END IF;
835 
836          -- secondary_detailed_quantity could also be missing:
837          IF px_rsv_rec.secondary_uom_code IS NOT NULL AND
838            px_rsv_rec.secondary_detailed_quantity IS NULL THEN
839            IF NVL(px_rsv_rec.detailed_quantity,0) = 0 THEN
840              px_rsv_rec.secondary_detailed_quantity := 0;
841            ELSE -- convert from detailed_quantity to secondary_detailed_quantity
842              l_tmp_secondary_quantity  := inv_convert.inv_um_convert(
843                                          item_id                      => px_rsv_rec.inventory_item_id
844                                        , lot_number                   => px_rsv_rec.lot_number
845                                        , organization_id              => px_rsv_rec.organization_id
846                                        , PRECISION                    => NULL -- use default precision
847                                        , from_quantity                => px_rsv_rec.detailed_quantity
848                                    , from_unit                    => px_rsv_rec.primary_uom_code
849                                        , to_unit                      => px_rsv_rec.secondary_uom_code
850                                        , from_name                    => NULL -- from uom name
851                                        , to_name                      => NULL -- to uom name
852                                        );
853 
854              IF l_tmp_secondary_quantity = -99999 THEN
855                -- conversion failed
856                fnd_message.set_name('INV', 'CAN-NOT-CONVERT-TO-SECOND-UOM'); -- INVCONV NEW MESSAGE
857                fnd_msg_pub.ADD;
858                RAISE fnd_api.g_exc_error;
859              END IF;
860              px_rsv_rec.secondary_detailed_quantity  := l_tmp_secondary_quantity; -- INVCONV
861            END IF;
862          END IF;
863          -- INVCONV END
864     --
865     --px_rsv_rec       := l_rsv_rec;
866     --
867     x_return_status  := l_return_status;
868   --
869   EXCEPTION
870     WHEN fnd_api.g_exc_error THEN
871       x_return_status  := fnd_api.g_ret_sts_error;
872     --
873     WHEN fnd_api.g_exc_unexpected_error THEN
874       x_return_status  := fnd_api.g_ret_sts_unexp_error;
875     --
876     WHEN OTHERS THEN
877       x_return_status  := fnd_api.g_ret_sts_unexp_error;
878 
879       --
880       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
881         fnd_msg_pub.add_exc_msg(g_pkg_name, 'Convert_Quantity');
882       END IF;
883   END convert_quantity;
884 
885   --
886   -- Description
887   --   convert missing value in the input record
888   --   to null in the output record. if the value of
889   --   field in the input record is not missing, it
890   --   would be copied to the output record
891   PROCEDURE convert_missing_to_null(p_rsv_rec IN inv_reservation_global.mtl_reservation_rec_type, x_rsv_rec OUT NOCOPY inv_reservation_global.mtl_reservation_rec_type) IS
892   BEGIN
893     IF p_rsv_rec.reservation_id <> fnd_api.g_miss_num THEN
894       x_rsv_rec.reservation_id  := p_rsv_rec.reservation_id;
895     ELSE
896       x_rsv_rec.reservation_id  := NULL;
897     END IF;
898 
899     --
900     IF p_rsv_rec.requirement_date <> fnd_api.g_miss_date THEN
901       x_rsv_rec.requirement_date  := p_rsv_rec.requirement_date;
902     ELSE
903       x_rsv_rec.requirement_date  := NULL;
904     END IF;
905 
906     --
907     IF p_rsv_rec.organization_id <> fnd_api.g_miss_num THEN
908       x_rsv_rec.organization_id  := p_rsv_rec.organization_id;
909     ELSE
910       x_rsv_rec.organization_id  := NULL;
911     END IF;
912 
913     --
914     IF p_rsv_rec.inventory_item_id <> fnd_api.g_miss_num THEN
915       x_rsv_rec.inventory_item_id  := p_rsv_rec.inventory_item_id;
916     ELSE
917       x_rsv_rec.inventory_item_id  := NULL;
918     END IF;
919 
920     --
921     IF p_rsv_rec.demand_source_type_id <> fnd_api.g_miss_num THEN
922       x_rsv_rec.demand_source_type_id  := p_rsv_rec.demand_source_type_id;
923     ELSE
924       x_rsv_rec.demand_source_type_id  := NULL;
925     END IF;
926 
927     --
928     IF p_rsv_rec.demand_source_name <> fnd_api.g_miss_char THEN
929       x_rsv_rec.demand_source_name  := p_rsv_rec.demand_source_name;
930     ELSE
931       x_rsv_rec.demand_source_name  := NULL;
932     END IF;
933 
934     --
935     IF p_rsv_rec.demand_source_delivery <> fnd_api.g_miss_num THEN
936       x_rsv_rec.demand_source_delivery  := p_rsv_rec.demand_source_delivery;
937     ELSE
938       x_rsv_rec.demand_source_delivery  := NULL;
939     END IF;
940 
941     --
942     IF p_rsv_rec.demand_source_header_id <> fnd_api.g_miss_num THEN
943       x_rsv_rec.demand_source_header_id  := p_rsv_rec.demand_source_header_id;
944     ELSE
945       x_rsv_rec.demand_source_header_id  := NULL;
946     END IF;
947 
948     --
949     IF p_rsv_rec.demand_source_line_id <> fnd_api.g_miss_num THEN
950       x_rsv_rec.demand_source_line_id  := p_rsv_rec.demand_source_line_id;
951     ELSE
952       x_rsv_rec.demand_source_line_id  := NULL;
953     END IF;
954 
955     --
956     IF p_rsv_rec.primary_uom_code <> fnd_api.g_miss_char THEN
957       x_rsv_rec.primary_uom_code  := p_rsv_rec.primary_uom_code;
958     ELSE
959       x_rsv_rec.primary_uom_code  := NULL;
960     END IF;
961 
962     --
963     IF p_rsv_rec.primary_uom_id <> fnd_api.g_miss_num THEN
964       x_rsv_rec.primary_uom_id  := p_rsv_rec.primary_uom_id;
965     ELSE
966       x_rsv_rec.primary_uom_id  := NULL;
967     END IF;
968 
969      -- INVCONV BEGIN
970          IF p_rsv_rec.secondary_uom_code <> fnd_api.g_miss_char THEN
971            x_rsv_rec.secondary_uom_code  := p_rsv_rec.secondary_uom_code;
972          ELSE
973            x_rsv_rec.secondary_uom_code  := NULL;
974          END IF;
975 
976         --
977          IF p_rsv_rec.secondary_uom_id <> fnd_api.g_miss_num THEN
978            x_rsv_rec.secondary_uom_id  := p_rsv_rec.secondary_uom_id;
979          ELSE
980            x_rsv_rec.secondary_uom_id  := NULL;
981          END IF;
982      -- INVCONV END
983 
984     --
985     IF p_rsv_rec.reservation_uom_code <> fnd_api.g_miss_char THEN
986       x_rsv_rec.reservation_uom_code  := p_rsv_rec.reservation_uom_code;
987     ELSE
988       x_rsv_rec.reservation_uom_code  := NULL;
989     END IF;
990 
991     --
992     IF p_rsv_rec.reservation_uom_id <> fnd_api.g_miss_num THEN
993       x_rsv_rec.reservation_uom_id  := p_rsv_rec.reservation_uom_id;
994     ELSE
995       x_rsv_rec.reservation_uom_id  := NULL;
996     END IF;
997 
998     --
999     IF p_rsv_rec.reservation_quantity <> fnd_api.g_miss_num THEN
1000       x_rsv_rec.reservation_quantity  := p_rsv_rec.reservation_quantity;
1001     ELSE
1002       x_rsv_rec.reservation_quantity  := NULL;
1003     END IF;
1004 
1005     --
1006     IF p_rsv_rec.primary_reservation_quantity <> fnd_api.g_miss_num THEN
1007       x_rsv_rec.primary_reservation_quantity  := p_rsv_rec.primary_reservation_quantity;
1008     ELSE
1009       x_rsv_rec.primary_reservation_quantity  := NULL;
1010     END IF;
1011 
1012      -- INVCONV BEGIN
1013          IF p_rsv_rec.secondary_reservation_quantity <> fnd_api.g_miss_num THEN
1014            x_rsv_rec.secondary_reservation_quantity  := p_rsv_rec.secondary_reservation_quantity;
1015          ELSE
1016            x_rsv_rec.secondary_reservation_quantity  := NULL;
1017          END IF;
1018      -- INVCONV END
1019 
1020     --
1021     IF p_rsv_rec.detailed_quantity <> fnd_api.g_miss_num THEN
1022       x_rsv_rec.detailed_quantity  := p_rsv_rec.detailed_quantity;
1023     ELSE
1024       x_rsv_rec.detailed_quantity  := NULL;
1025     END IF;
1026 
1027 
1028      -- INVCONV BEGIN
1029          IF p_rsv_rec.secondary_detailed_quantity <> fnd_api.g_miss_num THEN
1030            x_rsv_rec.secondary_detailed_quantity  := p_rsv_rec.secondary_detailed_quantity;
1031          ELSE
1032            x_rsv_rec.secondary_detailed_quantity  := NULL;
1033          END IF;
1034      -- INVCONV END
1035     --
1036     IF p_rsv_rec.autodetail_group_id <> fnd_api.g_miss_num THEN
1037       x_rsv_rec.autodetail_group_id  := p_rsv_rec.autodetail_group_id;
1038     ELSE
1039       x_rsv_rec.autodetail_group_id  := NULL;
1040     END IF;
1041 
1042     --
1043     IF p_rsv_rec.external_source_code <> fnd_api.g_miss_char THEN
1044       x_rsv_rec.external_source_code  := p_rsv_rec.external_source_code;
1045     ELSE
1046       x_rsv_rec.external_source_code  := NULL;
1047     END IF;
1048 
1049     --
1050     IF p_rsv_rec.external_source_line_id <> fnd_api.g_miss_num THEN
1051       x_rsv_rec.external_source_line_id  := p_rsv_rec.external_source_line_id;
1052     ELSE
1053       x_rsv_rec.external_source_line_id  := NULL;
1054     END IF;
1055 
1056     --
1057     IF p_rsv_rec.supply_source_type_id <> fnd_api.g_miss_num THEN
1058       x_rsv_rec.supply_source_type_id  := p_rsv_rec.supply_source_type_id;
1059     ELSE
1060       x_rsv_rec.supply_source_type_id  := NULL;
1061     END IF;
1062 
1063     --
1064     IF p_rsv_rec.supply_source_header_id <> fnd_api.g_miss_num THEN
1065       x_rsv_rec.supply_source_header_id  := p_rsv_rec.supply_source_header_id;
1066     ELSE
1067       x_rsv_rec.supply_source_header_id  := NULL;
1068     END IF;
1069 
1070     --
1071     IF p_rsv_rec.supply_source_line_id <> fnd_api.g_miss_num THEN
1072       x_rsv_rec.supply_source_line_id  := p_rsv_rec.supply_source_line_id;
1073     ELSE
1074       x_rsv_rec.supply_source_line_id  := NULL;
1075     END IF;
1076 
1077     --
1078     IF p_rsv_rec.supply_source_line_detail <> fnd_api.g_miss_num THEN
1079       x_rsv_rec.supply_source_line_detail  := p_rsv_rec.supply_source_line_detail;
1080     ELSE
1081       x_rsv_rec.supply_source_line_detail  := NULL;
1082     END IF;
1083 
1084     --
1085     IF p_rsv_rec.supply_source_name <> fnd_api.g_miss_char THEN
1086       x_rsv_rec.supply_source_name  := p_rsv_rec.supply_source_name;
1087     ELSE
1088       x_rsv_rec.supply_source_name  := NULL;
1089     END IF;
1090 
1091     --
1092     IF p_rsv_rec.revision <> fnd_api.g_miss_char THEN
1093       x_rsv_rec.revision  := p_rsv_rec.revision;
1094     ELSE
1095       x_rsv_rec.revision  := NULL;
1096     END IF;
1097 
1098     --
1099     IF p_rsv_rec.subinventory_code <> fnd_api.g_miss_char THEN
1100       x_rsv_rec.subinventory_code  := p_rsv_rec.subinventory_code;
1101     ELSE
1102       x_rsv_rec.subinventory_code  := NULL;
1103     END IF;
1104 
1105     --
1106     IF p_rsv_rec.subinventory_id <> fnd_api.g_miss_num THEN
1107       x_rsv_rec.subinventory_id  := p_rsv_rec.subinventory_id;
1108     ELSE
1109       x_rsv_rec.subinventory_id  := NULL;
1110     END IF;
1111 
1112     --
1113     IF p_rsv_rec.locator_id <> fnd_api.g_miss_num THEN
1114       x_rsv_rec.locator_id  := p_rsv_rec.locator_id;
1115     ELSE
1116       x_rsv_rec.locator_id  := NULL;
1117     END IF;
1118 
1119     --
1120     IF p_rsv_rec.lot_number <> fnd_api.g_miss_char THEN
1121       x_rsv_rec.lot_number  := p_rsv_rec.lot_number;
1122     ELSE
1123       x_rsv_rec.lot_number  := NULL;
1124     END IF;
1125 
1126     --
1127     IF p_rsv_rec.lot_number_id <> fnd_api.g_miss_num THEN
1128       x_rsv_rec.lot_number_id  := p_rsv_rec.lot_number_id;
1129     ELSE
1130       x_rsv_rec.lot_number_id  := NULL;
1131     END IF;
1132 
1133     --
1134     IF p_rsv_rec.pick_slip_number <> fnd_api.g_miss_num THEN
1135       x_rsv_rec.pick_slip_number  := p_rsv_rec.pick_slip_number;
1136     ELSE
1137       x_rsv_rec.pick_slip_number  := NULL;
1138     END IF;
1139 
1140     --
1141     IF p_rsv_rec.attribute_category <> fnd_api.g_miss_char THEN
1142       x_rsv_rec.attribute_category  := p_rsv_rec.attribute_category;
1143     ELSE
1144       x_rsv_rec.attribute_category  := NULL;
1145     END IF;
1146 
1147     --
1148     IF p_rsv_rec.attribute1 <> fnd_api.g_miss_char THEN
1149       x_rsv_rec.attribute1  := p_rsv_rec.attribute1;
1150     ELSE
1151       x_rsv_rec.attribute1  := NULL;
1152     END IF;
1153 
1154     IF p_rsv_rec.attribute2 <> fnd_api.g_miss_char THEN
1155       x_rsv_rec.attribute2  := p_rsv_rec.attribute2;
1156     ELSE
1157       x_rsv_rec.attribute2  := NULL;
1158     END IF;
1159 
1160     IF p_rsv_rec.attribute3 <> fnd_api.g_miss_char THEN
1161       x_rsv_rec.attribute3  := p_rsv_rec.attribute3;
1162     ELSE
1163       x_rsv_rec.attribute3  := NULL;
1164     END IF;
1165 
1166     IF p_rsv_rec.attribute4 <> fnd_api.g_miss_char THEN
1167       x_rsv_rec.attribute4  := p_rsv_rec.attribute4;
1168     ELSE
1169       x_rsv_rec.attribute4  := NULL;
1170     END IF;
1171 
1172     IF p_rsv_rec.attribute5 <> fnd_api.g_miss_char THEN
1173       x_rsv_rec.attribute5  := p_rsv_rec.attribute5;
1174     ELSE
1175       x_rsv_rec.attribute5  := NULL;
1176     END IF;
1177 
1178     IF p_rsv_rec.attribute6 <> fnd_api.g_miss_char THEN
1179       x_rsv_rec.attribute6  := p_rsv_rec.attribute6;
1180     ELSE
1181       x_rsv_rec.attribute6  := NULL;
1182     END IF;
1183 
1184     IF p_rsv_rec.attribute7 <> fnd_api.g_miss_char THEN
1185       x_rsv_rec.attribute7  := p_rsv_rec.attribute7;
1186     ELSE
1187       x_rsv_rec.attribute7  := NULL;
1188     END IF;
1189 
1190     IF p_rsv_rec.attribute8 <> fnd_api.g_miss_char THEN
1191       x_rsv_rec.attribute8  := p_rsv_rec.attribute8;
1192     ELSE
1193       x_rsv_rec.attribute8  := NULL;
1194     END IF;
1195 
1196     IF p_rsv_rec.attribute9 <> fnd_api.g_miss_char THEN
1197       x_rsv_rec.attribute9  := p_rsv_rec.attribute9;
1198     ELSE
1199       x_rsv_rec.attribute9  := NULL;
1200     END IF;
1201 
1202     IF p_rsv_rec.attribute10 <> fnd_api.g_miss_char THEN
1203       x_rsv_rec.attribute10  := p_rsv_rec.attribute10;
1204     ELSE
1205       x_rsv_rec.attribute10  := NULL;
1206     END IF;
1207 
1208     IF p_rsv_rec.attribute11 <> fnd_api.g_miss_char THEN
1209       x_rsv_rec.attribute11  := p_rsv_rec.attribute11;
1210     ELSE
1211       x_rsv_rec.attribute11  := NULL;
1212     END IF;
1213 
1214     IF p_rsv_rec.attribute12 <> fnd_api.g_miss_char THEN
1215       x_rsv_rec.attribute12  := p_rsv_rec.attribute12;
1216     ELSE
1217       x_rsv_rec.attribute12  := NULL;
1218     END IF;
1219 
1220     IF p_rsv_rec.attribute13 <> fnd_api.g_miss_char THEN
1221       x_rsv_rec.attribute13  := p_rsv_rec.attribute13;
1222     ELSE
1223       x_rsv_rec.attribute13  := NULL;
1224     END IF;
1225 
1226     IF p_rsv_rec.attribute14 <> fnd_api.g_miss_char THEN
1227       x_rsv_rec.attribute14  := p_rsv_rec.attribute14;
1228     ELSE
1229       x_rsv_rec.attribute14  := NULL;
1230     END IF;
1231 
1232     IF p_rsv_rec.attribute15 <> fnd_api.g_miss_char THEN
1233       x_rsv_rec.attribute15  := p_rsv_rec.attribute15;
1234     ELSE
1235       x_rsv_rec.attribute15  := NULL;
1236     END IF;
1237 
1238     IF p_rsv_rec.ship_ready_flag <> fnd_api.g_miss_num THEN
1239       x_rsv_rec.ship_ready_flag  := p_rsv_rec.ship_ready_flag;
1240     ELSE
1241       x_rsv_rec.ship_ready_flag  := NULL;
1242     END IF;
1243 
1244     IF p_rsv_rec.staged_flag <> fnd_api.g_miss_char THEN
1245       x_rsv_rec.staged_flag  := p_rsv_rec.staged_flag;
1246     ELSE
1247       x_rsv_rec.staged_flag  := NULL;
1248     END IF;
1249 
1250     IF p_rsv_rec.lpn_id <> fnd_api.g_miss_num THEN
1251       x_rsv_rec.lpn_id  := p_rsv_rec.lpn_id;
1252     ELSE
1253       x_rsv_rec.lpn_id  := NULL;
1254     END IF;
1255 
1256     /**** {{ R12 Enhanced reservations code changes. Adding new columns to
1257     -- convert_missing_to_null API}}****/
1258     IF p_rsv_rec.crossdock_flag <> fnd_api.g_miss_char THEN
1259        x_rsv_rec.crossdock_flag  := p_rsv_rec.crossdock_flag;
1260      ELSE
1261        x_rsv_rec.crossdock_flag  := NULL;
1262     END IF;
1263 
1264     IF p_rsv_rec.crossdock_criteria_id <> fnd_api.g_miss_num THEN
1265        x_rsv_rec.crossdock_criteria_id  := p_rsv_rec.crossdock_criteria_id;
1266      ELSE
1267        x_rsv_rec.crossdock_criteria_id  := NULL;
1268     END IF;
1269 
1270     IF p_rsv_rec.demand_source_line_detail <> fnd_api.g_miss_num THEN
1271        x_rsv_rec.demand_source_line_detail  := p_rsv_rec.demand_source_line_detail;
1272      ELSE
1273        x_rsv_rec.demand_source_line_detail  := NULL;
1274     END IF;
1275 
1276     IF p_rsv_rec.serial_reservation_quantity <> fnd_api.g_miss_num THEN
1277        x_rsv_rec.serial_reservation_quantity  := p_rsv_rec.serial_reservation_quantity;
1278      ELSE
1279        x_rsv_rec.serial_reservation_quantity  := NULL;
1280     END IF;
1281 
1282     IF p_rsv_rec.supply_receipt_date <> fnd_api.g_miss_date THEN
1283        x_rsv_rec.supply_receipt_date  := p_rsv_rec.supply_receipt_date;
1284      ELSE
1285        x_rsv_rec.supply_receipt_date  := NULL;
1286     END IF;
1287 
1288      IF p_rsv_rec.demand_ship_date <> fnd_api.g_miss_date THEN
1289        x_rsv_rec.demand_ship_date  := p_rsv_rec.demand_ship_date;
1290      ELSE
1291        x_rsv_rec.demand_ship_date  := NULL;
1292      END IF;
1293 
1294      IF p_rsv_rec.project_id <> fnd_api.g_miss_num THEN
1295        x_rsv_rec.project_id  := p_rsv_rec.project_id;
1296      ELSE
1297        x_rsv_rec.project_id  := NULL;
1298      END IF;
1299 
1300      IF p_rsv_rec.task_id <> fnd_api.g_miss_num THEN
1301        x_rsv_rec.task_id  := p_rsv_rec.task_id;
1302      ELSE
1303        x_rsv_rec.task_id  := NULL;
1304     END IF;
1305 
1306  /*** End R12 ***/
1307   END;
1308 
1309   --
1310   -- Description
1311   -- return true if any attribute in the input record is missing
1312   -- else return false
1313   FUNCTION check_missing(p_rsv_rec IN OUT NOCOPY inv_reservation_global.mtl_reservation_rec_type, x_what_field OUT NOCOPY VARCHAR2)
1314     RETURN BOOLEAN IS
1315   BEGIN
1316     x_what_field  := NULL;
1317 
1318     IF p_rsv_rec.requirement_date = fnd_api.g_miss_date THEN
1319       x_what_field  := 'requirement_date';
1320     END IF;
1321 
1322     IF p_rsv_rec.organization_id = fnd_api.g_miss_num THEN
1323       x_what_field  := 'organization_id';
1324     END IF;
1325 
1326     IF p_rsv_rec.inventory_item_id = fnd_api.g_miss_num THEN
1327       x_what_field  := 'inventory_item_id';
1328     END IF;
1329 
1330     IF p_rsv_rec.demand_source_type_id = fnd_api.g_miss_num THEN
1331       x_what_field  := 'demand_source_type_id';
1332     END IF;
1333 
1334     IF p_rsv_rec.demand_source_name = fnd_api.g_miss_char THEN
1335       x_what_field  := 'demand_source_name';
1336     END IF;
1337 
1338     IF p_rsv_rec.demand_source_delivery = fnd_api.g_miss_num THEN
1339       x_what_field  := 'demand_source_delivery';
1340     END IF;
1341 
1342     IF p_rsv_rec.demand_source_header_id = fnd_api.g_miss_num THEN
1343       x_what_field  := 'demand_source_header_id';
1344     END IF;
1345 
1346     IF p_rsv_rec.demand_source_line_id = fnd_api.g_miss_num THEN
1347       x_what_field  := 'demand_source_line_id';
1348     END IF;
1349 
1350     IF p_rsv_rec.primary_uom_code = fnd_api.g_miss_char THEN
1351       x_what_field  := 'primary_uom_code';
1352     END IF;
1353 
1354     IF p_rsv_rec.primary_uom_id = fnd_api.g_miss_num THEN
1355       x_what_field  := 'primary_uom_id';
1356     END IF;
1357 
1358      -- INVCONV BEGIN
1359          IF p_rsv_rec.secondary_uom_code = fnd_api.g_miss_char THEN
1360            x_what_field  := 'secondary_uom_code';
1361          END IF;
1362 
1363         IF p_rsv_rec.secondary_uom_id = fnd_api.g_miss_num THEN
1364            x_what_field  := 'secondary_uom_id';
1365          END IF;
1366          -- INVCONV END
1367 
1368     IF p_rsv_rec.reservation_uom_code = fnd_api.g_miss_char THEN
1369       x_what_field  := 'reservation_uom_code';
1370     END IF;
1371 
1372     IF p_rsv_rec.reservation_uom_id = fnd_api.g_miss_num THEN
1373       x_what_field  := 'reservation_uom_id';
1374     END IF;
1375 
1376     IF p_rsv_rec.reservation_quantity = fnd_api.g_miss_num THEN
1377       x_what_field  := 'reservation_quantity';
1378     END IF;
1379 
1380     IF p_rsv_rec.primary_reservation_quantity = fnd_api.g_miss_num THEN
1381       x_what_field  := 'primary_reservation_quantity';
1382     END IF;
1383 
1384     -- INVCONV BEGIN
1385     IF p_rsv_rec.secondary_reservation_quantity = fnd_api.g_miss_num THEN
1386       x_what_field  := 'secondary_reservation_quantity';
1387     END IF;
1388     -- INVCONV END
1389 
1390     IF p_rsv_rec.detailed_quantity = fnd_api.g_miss_num THEN
1391       x_what_field  := 'detailed_quantity';
1392     END IF;
1393 
1394     -- INVCONV BEGIN
1395     IF p_rsv_rec.secondary_detailed_quantity = fnd_api.g_miss_num THEN
1396       x_what_field  := 'secondary_detailed_quantity';
1397     END IF;
1398     -- INVCONV BEGIN
1399 
1400     IF p_rsv_rec.autodetail_group_id = fnd_api.g_miss_num THEN
1401       x_what_field  := 'autodetail_group_id';
1402     END IF;
1403 
1404     IF p_rsv_rec.external_source_code = fnd_api.g_miss_char THEN
1405       x_what_field  := 'external_source_code';
1406     END IF;
1407 
1408     IF p_rsv_rec.external_source_line_id = fnd_api.g_miss_num THEN
1409       x_what_field  := 'external_source_line_id';
1410     END IF;
1411 
1412     IF p_rsv_rec.supply_source_type_id = fnd_api.g_miss_num THEN
1413       x_what_field  := 'supply_source_type_id';
1414     END IF;
1415 
1416     IF p_rsv_rec.supply_source_header_id = fnd_api.g_miss_num THEN
1417       x_what_field  := 'supply_source_header_id';
1418     END IF;
1419 
1420     IF p_rsv_rec.supply_source_line_id = fnd_api.g_miss_num THEN
1421       x_what_field  := 'supply_source_line_id';
1422     END IF;
1423 
1424     IF p_rsv_rec.supply_source_name = fnd_api.g_miss_char THEN
1425       x_what_field  := 'supply_source_name';
1426     END IF;
1427 
1428     IF p_rsv_rec.supply_source_line_detail = fnd_api.g_miss_num THEN
1429       x_what_field  := 'supply_source_line_detail';
1430     END IF;
1431 
1432     IF p_rsv_rec.revision = fnd_api.g_miss_char THEN
1433       x_what_field  := 'revision';
1434     END IF;
1435 
1436     IF p_rsv_rec.subinventory_code = fnd_api.g_miss_char THEN
1437       x_what_field  := 'subinventory_code';
1438     END IF;
1439 
1440     IF p_rsv_rec.subinventory_id = fnd_api.g_miss_num THEN
1441       x_what_field  := 'subinventory_id';
1442     END IF;
1443 
1444     IF p_rsv_rec.locator_id = fnd_api.g_miss_num THEN
1445       x_what_field  := 'locator_id';
1446     END IF;
1447 
1448     IF p_rsv_rec.lot_number = fnd_api.g_miss_char THEN
1449       x_what_field  := 'lot_number';
1450     END IF;
1451 
1452     IF p_rsv_rec.lot_number_id = fnd_api.g_miss_num THEN
1453       x_what_field  := 'lot_number_id';
1454     END IF;
1455 
1456     IF p_rsv_rec.pick_slip_number = fnd_api.g_miss_num THEN
1457       x_what_field  := 'pick_slip_number';
1458     END IF;
1459 
1460     IF p_rsv_rec.lpn_id = fnd_api.g_miss_num THEN
1461       x_what_field  := 'lpn_id';
1462     END IF;
1463 
1464     IF p_rsv_rec.attribute_category = fnd_api.g_miss_char THEN
1465       x_what_field  := 'attribute_category';
1466     END IF;
1467 
1468     IF p_rsv_rec.attribute1 = fnd_api.g_miss_char THEN
1469       x_what_field  := 'attribute1';
1470     END IF;
1471 
1472     IF p_rsv_rec.attribute2 = fnd_api.g_miss_char THEN
1473       x_what_field  := 'attribute2';
1474     END IF;
1475 
1476     IF p_rsv_rec.attribute3 = fnd_api.g_miss_char THEN
1477       x_what_field  := 'attribute3';
1478     END IF;
1479 
1480     IF p_rsv_rec.attribute4 = fnd_api.g_miss_char THEN
1481       x_what_field  := 'attribute4';
1482     END IF;
1483 
1484     IF p_rsv_rec.attribute5 = fnd_api.g_miss_char THEN
1485       x_what_field  := 'attribute5';
1486     END IF;
1487 
1488     IF p_rsv_rec.attribute6 = fnd_api.g_miss_char THEN
1489       x_what_field  := 'attribute6';
1490     END IF;
1491 
1492     IF p_rsv_rec.attribute7 = fnd_api.g_miss_char THEN
1493       x_what_field  := 'attribute7';
1494     END IF;
1495 
1496     IF p_rsv_rec.attribute8 = fnd_api.g_miss_char THEN
1497       x_what_field  := 'attribute8';
1498     END IF;
1499 
1500     IF p_rsv_rec.attribute9 = fnd_api.g_miss_char THEN
1501       x_what_field  := 'attribute9';
1502     END IF;
1503 
1504     IF p_rsv_rec.attribute10 = fnd_api.g_miss_char THEN
1505       x_what_field  := 'attribute10';
1506     END IF;
1507 
1508     IF p_rsv_rec.attribute11 = fnd_api.g_miss_char THEN
1509       x_what_field  := 'attribute11';
1510     END IF;
1511 
1512     IF p_rsv_rec.attribute12 = fnd_api.g_miss_char THEN
1513       x_what_field  := 'attribute12';
1514     END IF;
1515 
1516     IF p_rsv_rec.attribute13 = fnd_api.g_miss_char THEN
1517       x_what_field  := 'attribute13';
1518     END IF;
1519 
1520     IF p_rsv_rec.attribute14 = fnd_api.g_miss_char THEN
1521       x_what_field  := 'attribute14';
1522     END IF;
1523 
1524     IF p_rsv_rec.attribute15 = fnd_api.g_miss_char THEN
1525       x_what_field  := 'attribute15';
1526     END IF;
1527 
1528     IF p_rsv_rec.attribute15 = fnd_api.g_miss_char THEN
1529       x_what_field  := 'attribute15';
1530     END IF;
1531 
1532     IF p_rsv_rec.ship_ready_flag = fnd_api.g_miss_num THEN
1533       x_what_field  := 'ship_ready_flag';
1534     END IF;
1535 
1536     IF p_rsv_rec.staged_flag = fnd_api.g_miss_char  THEN
1537        p_rsv_rec.staged_flag := NULL;
1538     END IF;
1539 
1540     /**** {{ R12 Enhanced reservations code changes. Adding new columns to
1541     -- check_missing API }}****/
1542     IF p_rsv_rec.crossdock_flag = fnd_api.g_miss_char  THEN
1543        p_rsv_rec.crossdock_flag := NULL;
1544     END IF;
1545 
1546     IF p_rsv_rec.crossdock_criteria_id = fnd_api.g_miss_num THEN
1547        p_rsv_rec.crossdock_criteria_id := NULL;
1548     END IF;
1549 
1550     IF p_rsv_rec.demand_source_line_detail = fnd_api.g_miss_num  THEN
1551        p_rsv_rec.demand_source_line_detail := NULL;
1552     END IF;
1553 
1554     IF p_rsv_rec.serial_reservation_quantity = fnd_api.g_miss_num  THEN
1555        p_rsv_rec.serial_reservation_quantity := NULL;
1556     END IF;
1557 
1558     IF p_rsv_rec.supply_receipt_date = fnd_api.g_miss_date  THEN
1559        p_rsv_rec.supply_receipt_date := NULL;
1560     END IF;
1561 
1562     IF p_rsv_rec.demand_ship_date = fnd_api.g_miss_date  THEN
1563        p_rsv_rec.demand_ship_date := NULL;
1564     END IF;
1565 
1566     IF p_rsv_rec.project_id = fnd_api.g_miss_num  THEN
1567        p_rsv_rec.project_id := NULL;
1568     END IF;
1569 
1570     IF p_rsv_rec.task_id = fnd_api.g_miss_num  THEN
1571        p_rsv_rec.task_id := NULL;
1572     END IF;
1573 
1574     /*** End R12 ***/
1575     debug_print('Check Missing parameter ' || x_what_field);
1576 
1577     IF x_what_field IS NOT NULL THEN
1578       RETURN TRUE;
1579     ELSE
1580       RETURN FALSE;
1581     END IF;
1582   END check_missing;
1583 
1584   --
1585   -- Description
1586   --   construct an output record based on the input records in the
1587   --   following way
1588   --   1. if the value of a field in p_to_rsv_rec is not missing or
1589   --      is null, copy the value to the corresponding field
1590   --      in x_to_rsv_rec
1591   --   2. else, copy the value of in the corresponding field in
1592   --      p_original_rsv_rec to the corresponding field in x_to_rsv_rec
1593   PROCEDURE construct_to_reservation_row(
1594     p_original_rsv_rec IN     inv_reservation_global.mtl_reservation_rec_type
1595   , p_to_rsv_rec       IN     inv_reservation_global.mtl_reservation_rec_type
1596   , x_to_rsv_rec       OUT    NOCOPY inv_reservation_global.mtl_reservation_rec_type
1597   ) IS
1598   BEGIN
1599     IF p_to_rsv_rec.reservation_id <> fnd_api.g_miss_num
1600        OR p_to_rsv_rec.reservation_id IS NULL THEN
1601       x_to_rsv_rec.reservation_id  := p_to_rsv_rec.reservation_id;
1602     ELSE
1603       x_to_rsv_rec.reservation_id  := p_original_rsv_rec.reservation_id;
1604     END IF;
1605 
1606     --
1607     IF p_to_rsv_rec.requirement_date <> fnd_api.g_miss_date
1608        OR p_to_rsv_rec.requirement_date IS NULL THEN
1609       x_to_rsv_rec.requirement_date  := p_to_rsv_rec.requirement_date;
1610     ELSE
1611       x_to_rsv_rec.requirement_date  := p_original_rsv_rec.requirement_date;
1612     END IF;
1613 
1614     --
1615     IF p_to_rsv_rec.organization_id <> fnd_api.g_miss_num
1616        OR p_to_rsv_rec.organization_id IS NULL THEN
1617       x_to_rsv_rec.organization_id  := p_to_rsv_rec.organization_id;
1618     ELSE
1619       x_to_rsv_rec.organization_id  := p_original_rsv_rec.organization_id;
1620     END IF;
1621 
1622     --
1623     IF p_to_rsv_rec.inventory_item_id <> fnd_api.g_miss_num
1624        OR p_to_rsv_rec.inventory_item_id IS NULL THEN
1625       x_to_rsv_rec.inventory_item_id  := p_to_rsv_rec.inventory_item_id;
1626     ELSE
1627       x_to_rsv_rec.inventory_item_id  := p_original_rsv_rec.inventory_item_id;
1628     END IF;
1629 
1630     --
1631     IF p_to_rsv_rec.demand_source_type_id <> fnd_api.g_miss_num
1632        OR p_to_rsv_rec.demand_source_type_id IS NULL THEN
1633       x_to_rsv_rec.demand_source_type_id  := p_to_rsv_rec.demand_source_type_id;
1634     ELSE
1635       x_to_rsv_rec.demand_source_type_id  := p_original_rsv_rec.demand_source_type_id;
1636     END IF;
1637 
1638     --
1639     IF p_to_rsv_rec.demand_source_name <> fnd_api.g_miss_char
1640        OR p_to_rsv_rec.demand_source_name IS NULL THEN
1641       x_to_rsv_rec.demand_source_name  := p_to_rsv_rec.demand_source_name;
1642     ELSE
1643       x_to_rsv_rec.demand_source_name  := p_original_rsv_rec.demand_source_name;
1644     END IF;
1645 
1646     --
1647     IF p_to_rsv_rec.demand_source_delivery <> fnd_api.g_miss_num
1648        OR p_to_rsv_rec.demand_source_delivery IS NULL THEN
1649       x_to_rsv_rec.demand_source_delivery  := p_to_rsv_rec.demand_source_delivery;
1650     ELSE
1651       x_to_rsv_rec.demand_source_delivery  := p_original_rsv_rec.demand_source_delivery;
1652     END IF;
1653 
1654     --
1655     IF p_to_rsv_rec.demand_source_header_id <> fnd_api.g_miss_num
1656        OR p_to_rsv_rec.demand_source_header_id IS NULL THEN
1657       x_to_rsv_rec.demand_source_header_id  := p_to_rsv_rec.demand_source_header_id;
1658     ELSE
1659       x_to_rsv_rec.demand_source_header_id  := p_original_rsv_rec.demand_source_header_id;
1660     END IF;
1661 
1662     --
1663     IF p_to_rsv_rec.demand_source_line_id <> fnd_api.g_miss_num
1664        OR p_to_rsv_rec.demand_source_line_id IS NULL THEN
1665       x_to_rsv_rec.demand_source_line_id  := p_to_rsv_rec.demand_source_line_id;
1666     ELSE
1667       x_to_rsv_rec.demand_source_line_id  := p_original_rsv_rec.demand_source_line_id;
1668     END IF;
1669 
1670     --
1671     IF p_to_rsv_rec.primary_uom_code <> fnd_api.g_miss_char
1672        OR p_to_rsv_rec.primary_uom_code IS NULL THEN
1673       x_to_rsv_rec.primary_uom_code  := p_to_rsv_rec.primary_uom_code;
1674     ELSE
1675       x_to_rsv_rec.primary_uom_code  := p_original_rsv_rec.primary_uom_code;
1676     END IF;
1677 
1678     --
1679     IF p_to_rsv_rec.primary_uom_id <> fnd_api.g_miss_num
1680        OR p_to_rsv_rec.primary_uom_id IS NULL THEN
1681       x_to_rsv_rec.primary_uom_id  := p_to_rsv_rec.primary_uom_id;
1682     ELSE
1683       x_to_rsv_rec.primary_uom_id  := p_original_rsv_rec.primary_uom_id;
1684     END IF;
1685 
1686     -- INVCONV BEGIN
1687     IF p_to_rsv_rec.secondary_uom_code <> fnd_api.g_miss_char
1688       OR p_to_rsv_rec.secondary_uom_code IS NULL THEN
1689       x_to_rsv_rec.secondary_uom_code  := p_to_rsv_rec.secondary_uom_code;
1690     ELSE
1691       x_to_rsv_rec.secondary_uom_code  := p_original_rsv_rec.secondary_uom_code;
1692     END IF;
1693 
1694     --
1695     IF p_to_rsv_rec.secondary_uom_id <> fnd_api.g_miss_num
1696         OR p_to_rsv_rec.secondary_uom_id IS NULL THEN
1697       x_to_rsv_rec.secondary_uom_id  := p_to_rsv_rec.secondary_uom_id;
1698     ELSE
1699       x_to_rsv_rec.secondary_uom_id  := p_original_rsv_rec.secondary_uom_id;
1700     END IF;
1701     -- INVCONV END
1702     --
1703     IF p_to_rsv_rec.reservation_uom_code <> fnd_api.g_miss_char
1704        OR p_to_rsv_rec.reservation_uom_code IS NULL THEN
1705       x_to_rsv_rec.reservation_uom_code  := p_to_rsv_rec.reservation_uom_code;
1706     ELSE
1707       x_to_rsv_rec.reservation_uom_code  := p_original_rsv_rec.reservation_uom_code;
1708     END IF;
1709 
1710     --
1711     IF p_to_rsv_rec.reservation_uom_id <> fnd_api.g_miss_num
1712        OR p_to_rsv_rec.reservation_uom_id IS NULL THEN
1713       x_to_rsv_rec.reservation_uom_id  := p_to_rsv_rec.reservation_uom_id;
1714     ELSE
1715       x_to_rsv_rec.reservation_uom_id  := p_original_rsv_rec.reservation_uom_id;
1716     END IF;
1717 
1718     --
1719     IF  p_to_rsv_rec.primary_reservation_quantity = fnd_api.g_miss_num
1720         AND p_to_rsv_rec.reservation_quantity = fnd_api.g_miss_num THEN
1721       -- neither primary quantity or reservaton quantity is
1722       -- specified for the to row
1723       x_to_rsv_rec.primary_reservation_quantity  := p_original_rsv_rec.primary_reservation_quantity;
1724       x_to_rsv_rec.reservation_quantity          := p_original_rsv_rec.reservation_quantity;
1725     ELSIF p_to_rsv_rec.primary_reservation_quantity <> fnd_api.g_miss_num
1726           OR p_to_rsv_rec.primary_reservation_quantity IS NULL THEN
1727       -- primary_reservation_quantity is specified for the to row
1728       -- here null is considered as a value
1729       x_to_rsv_rec.primary_reservation_quantity  := p_to_rsv_rec.primary_reservation_quantity;
1730       x_to_rsv_rec.reservation_quantity          := NULL;
1731     ELSE
1732       -- primary_reservation_quantity is fnd_api.g_miss_num
1733       -- but reservation_quantity is null or
1734       -- value other than fnd_api.g_miss_num
1735       -- for the to row
1736       x_to_rsv_rec.primary_reservation_quantity  := NULL;
1737       x_to_rsv_rec.reservation_quantity          := p_to_rsv_rec.reservation_quantity;
1738     END IF;
1739 
1740     --
1741     -- INVCONV BEGIN
1742     IF p_to_rsv_rec.secondary_reservation_quantity <> fnd_api.g_miss_num
1743         OR p_to_rsv_rec.secondary_reservation_quantity IS NULL THEN
1744       x_to_rsv_rec.secondary_reservation_quantity  := p_to_rsv_rec.secondary_reservation_quantity;
1745     ELSE
1746       x_to_rsv_rec.secondary_reservation_quantity  := p_original_rsv_rec.secondary_reservation_quantity;
1747     END IF;
1748     -- INVCONV END
1749 
1750     IF p_to_rsv_rec.detailed_quantity <> fnd_api.g_miss_num
1751        OR p_to_rsv_rec.detailed_quantity IS NULL THEN
1752       x_to_rsv_rec.detailed_quantity  := p_to_rsv_rec.detailed_quantity;
1753     ELSE
1754       x_to_rsv_rec.detailed_quantity  := p_original_rsv_rec.detailed_quantity;
1755     END IF;
1756 
1757     -- INVCONV BEGIN
1758     IF p_to_rsv_rec.secondary_detailed_quantity <> fnd_api.g_miss_num
1759          OR p_to_rsv_rec.secondary_detailed_quantity IS NULL THEN
1760       x_to_rsv_rec.secondary_detailed_quantity  := p_to_rsv_rec.secondary_detailed_quantity;
1761     ELSE
1762       x_to_rsv_rec.secondary_detailed_quantity  := p_original_rsv_rec.secondary_detailed_quantity;
1763     END IF;
1764     -- INVCONV END
1765 
1766     IF p_to_rsv_rec.autodetail_group_id <> fnd_api.g_miss_num
1767        OR p_to_rsv_rec.autodetail_group_id IS NULL THEN
1768       x_to_rsv_rec.autodetail_group_id  := p_to_rsv_rec.autodetail_group_id;
1769     ELSE
1770       x_to_rsv_rec.autodetail_group_id  := p_original_rsv_rec.autodetail_group_id;
1771     END IF;
1772 
1773     --
1774     IF p_to_rsv_rec.external_source_code <> fnd_api.g_miss_char
1775        OR p_to_rsv_rec.external_source_code IS NULL THEN
1776       x_to_rsv_rec.external_source_code  := p_to_rsv_rec.external_source_code;
1777     ELSE
1778       x_to_rsv_rec.external_source_code  := p_original_rsv_rec.external_source_code;
1779     END IF;
1780 
1781     --
1782     IF p_to_rsv_rec.external_source_line_id <> fnd_api.g_miss_num
1783        OR p_to_rsv_rec.external_source_line_id IS NULL THEN
1784       x_to_rsv_rec.external_source_line_id  := p_to_rsv_rec.external_source_line_id;
1785     ELSE
1786       x_to_rsv_rec.external_source_line_id  := p_original_rsv_rec.external_source_line_id;
1787     END IF;
1788 
1789     --
1790     IF p_to_rsv_rec.supply_source_type_id <> fnd_api.g_miss_num
1791        OR p_to_rsv_rec.supply_source_type_id IS NULL THEN
1792       x_to_rsv_rec.supply_source_type_id  := p_to_rsv_rec.supply_source_type_id;
1793     ELSE
1794       x_to_rsv_rec.supply_source_type_id  := p_original_rsv_rec.supply_source_type_id;
1795     END IF;
1796 
1797     --
1798     IF p_to_rsv_rec.supply_source_header_id <> fnd_api.g_miss_num
1799        OR p_to_rsv_rec.supply_source_header_id IS NULL THEN
1800       x_to_rsv_rec.supply_source_header_id  := p_to_rsv_rec.supply_source_header_id;
1801     ELSE
1802       x_to_rsv_rec.supply_source_header_id  := p_original_rsv_rec.supply_source_header_id;
1803     END IF;
1804 
1805     --
1806     IF p_to_rsv_rec.supply_source_line_id <> fnd_api.g_miss_num
1807        OR p_to_rsv_rec.supply_source_line_id IS NULL THEN
1808       x_to_rsv_rec.supply_source_line_id  := p_to_rsv_rec.supply_source_line_id;
1809     ELSE
1810       x_to_rsv_rec.supply_source_line_id  := p_original_rsv_rec.supply_source_line_id;
1811     END IF;
1812 
1813     --
1814     IF p_to_rsv_rec.supply_source_line_detail <> fnd_api.g_miss_num
1815        OR p_to_rsv_rec.supply_source_line_detail IS NULL THEN
1816       x_to_rsv_rec.supply_source_line_detail  := p_to_rsv_rec.supply_source_line_detail;
1817     ELSE
1818       x_to_rsv_rec.supply_source_line_detail  := p_original_rsv_rec.supply_source_line_detail;
1819     END IF;
1820 
1821     --
1822     IF p_to_rsv_rec.supply_source_name <> fnd_api.g_miss_char
1823        OR p_to_rsv_rec.supply_source_name IS NULL THEN
1824       x_to_rsv_rec.supply_source_name  := p_to_rsv_rec.supply_source_name;
1825     ELSE
1826       x_to_rsv_rec.supply_source_name  := p_original_rsv_rec.supply_source_name;
1827     END IF;
1828 
1829     --
1830     IF p_to_rsv_rec.revision <> fnd_api.g_miss_char
1831        OR p_to_rsv_rec.revision IS NULL THEN
1832       x_to_rsv_rec.revision  := p_to_rsv_rec.revision;
1833     ELSE
1834       x_to_rsv_rec.revision  := p_original_rsv_rec.revision;
1835     END IF;
1836 
1837     --
1838     IF p_to_rsv_rec.subinventory_code <> fnd_api.g_miss_char
1839        OR p_to_rsv_rec.subinventory_code IS NULL THEN
1840       x_to_rsv_rec.subinventory_code  := p_to_rsv_rec.subinventory_code;
1841     ELSE
1842       x_to_rsv_rec.subinventory_code  := p_original_rsv_rec.subinventory_code;
1843     END IF;
1844 
1845     --
1846     IF p_to_rsv_rec.subinventory_id <> fnd_api.g_miss_num
1847        OR p_to_rsv_rec.subinventory_id IS NULL THEN
1848       x_to_rsv_rec.subinventory_id  := p_to_rsv_rec.subinventory_id;
1849     ELSE
1850       x_to_rsv_rec.subinventory_id  := p_original_rsv_rec.subinventory_id;
1851     END IF;
1852 
1853     --
1854     IF p_to_rsv_rec.locator_id <> fnd_api.g_miss_num
1855        OR p_to_rsv_rec.locator_id IS NULL THEN
1856       x_to_rsv_rec.locator_id  := p_to_rsv_rec.locator_id;
1857     ELSE
1858       x_to_rsv_rec.locator_id  := p_original_rsv_rec.locator_id;
1859     END IF;
1860 
1861     --
1862     IF p_to_rsv_rec.lot_number <> fnd_api.g_miss_char
1863        OR p_to_rsv_rec.lot_number IS NULL THEN
1864       x_to_rsv_rec.lot_number  := p_to_rsv_rec.lot_number;
1865     ELSE
1866       x_to_rsv_rec.lot_number  := p_original_rsv_rec.lot_number;
1867     END IF;
1868 
1869     --
1870     IF p_to_rsv_rec.lot_number_id <> fnd_api.g_miss_num
1871        OR p_to_rsv_rec.lot_number_id IS NULL THEN
1872       x_to_rsv_rec.lot_number_id  := p_to_rsv_rec.lot_number_id;
1873     ELSE
1874       x_to_rsv_rec.lot_number_id  := p_original_rsv_rec.lot_number_id;
1875     END IF;
1876 
1877     --
1878     IF p_to_rsv_rec.pick_slip_number <> fnd_api.g_miss_num
1879        OR p_to_rsv_rec.pick_slip_number IS NULL THEN
1880       x_to_rsv_rec.pick_slip_number  := p_to_rsv_rec.pick_slip_number;
1881     ELSE
1882       x_to_rsv_rec.pick_slip_number  := p_original_rsv_rec.pick_slip_number;
1883     END IF;
1884 
1885     --
1886     IF p_to_rsv_rec.lpn_id <> fnd_api.g_miss_num
1887        OR p_to_rsv_rec.lpn_id IS NULL THEN
1888       x_to_rsv_rec.lpn_id  := p_to_rsv_rec.lpn_id;
1889     ELSE
1890       x_to_rsv_rec.lpn_id  := p_original_rsv_rec.lpn_id;
1891     END IF;
1892 
1893     --
1894     IF p_to_rsv_rec.attribute_category <> fnd_api.g_miss_char
1895        OR p_to_rsv_rec.attribute_category IS NULL THEN
1896       x_to_rsv_rec.attribute_category  := p_to_rsv_rec.attribute_category;
1897     ELSE
1898       x_to_rsv_rec.attribute_category  := p_original_rsv_rec.attribute_category;
1899     END IF;
1900 
1901     --
1902     IF p_to_rsv_rec.attribute1 <> fnd_api.g_miss_char
1903        OR p_to_rsv_rec.attribute1 IS NULL THEN
1904       x_to_rsv_rec.attribute1  := p_to_rsv_rec.attribute1;
1905     ELSE
1906       x_to_rsv_rec.attribute1  := p_original_rsv_rec.attribute1;
1907     END IF;
1908 
1909     IF p_to_rsv_rec.attribute2 <> fnd_api.g_miss_char
1910        OR p_to_rsv_rec.attribute2 IS NULL THEN
1911       x_to_rsv_rec.attribute2  := p_to_rsv_rec.attribute2;
1912     ELSE
1913       x_to_rsv_rec.attribute2  := p_original_rsv_rec.attribute2;
1914     END IF;
1915 
1916     IF p_to_rsv_rec.attribute3 <> fnd_api.g_miss_char
1917        OR p_to_rsv_rec.attribute3 IS NULL THEN
1918       x_to_rsv_rec.attribute3  := p_to_rsv_rec.attribute3;
1919     ELSE
1920       x_to_rsv_rec.attribute3  := p_original_rsv_rec.attribute3;
1921     END IF;
1922 
1923     IF p_to_rsv_rec.attribute4 <> fnd_api.g_miss_char
1924        OR p_to_rsv_rec.attribute4 IS NULL THEN
1925       x_to_rsv_rec.attribute4  := p_to_rsv_rec.attribute4;
1926     ELSE
1927       x_to_rsv_rec.attribute4  := p_original_rsv_rec.attribute4;
1928     END IF;
1929 
1930     IF p_to_rsv_rec.attribute5 <> fnd_api.g_miss_char
1931        OR p_to_rsv_rec.attribute5 IS NULL THEN
1932       x_to_rsv_rec.attribute5  := p_to_rsv_rec.attribute5;
1933     ELSE
1934       x_to_rsv_rec.attribute5  := p_original_rsv_rec.attribute5;
1935     END IF;
1936 
1937     IF p_to_rsv_rec.attribute6 <> fnd_api.g_miss_char
1938        OR p_to_rsv_rec.attribute6 IS NULL THEN
1939       x_to_rsv_rec.attribute6  := p_to_rsv_rec.attribute6;
1940     ELSE
1941       x_to_rsv_rec.attribute6  := p_original_rsv_rec.attribute6;
1942     END IF;
1943 
1944     IF p_to_rsv_rec.attribute7 <> fnd_api.g_miss_char
1945        OR p_to_rsv_rec.attribute7 IS NULL THEN
1946       x_to_rsv_rec.attribute7  := p_to_rsv_rec.attribute7;
1947     ELSE
1948       x_to_rsv_rec.attribute7  := p_original_rsv_rec.attribute7;
1949     END IF;
1950 
1951     IF p_to_rsv_rec.attribute8 <> fnd_api.g_miss_char
1952        OR p_to_rsv_rec.attribute8 IS NULL THEN
1953       x_to_rsv_rec.attribute8  := p_to_rsv_rec.attribute8;
1954     ELSE
1955       x_to_rsv_rec.attribute8  := p_original_rsv_rec.attribute8;
1956     END IF;
1957 
1958     IF p_to_rsv_rec.attribute9 <> fnd_api.g_miss_char
1959        OR p_to_rsv_rec.attribute9 IS NULL THEN
1960       x_to_rsv_rec.attribute9  := p_to_rsv_rec.attribute9;
1961     ELSE
1962       x_to_rsv_rec.attribute9  := p_original_rsv_rec.attribute9;
1963     END IF;
1964 
1965     IF p_to_rsv_rec.attribute10 <> fnd_api.g_miss_char
1966        OR p_to_rsv_rec.attribute10 IS NULL THEN
1967       x_to_rsv_rec.attribute10  := p_to_rsv_rec.attribute10;
1968     ELSE
1969       x_to_rsv_rec.attribute10  := p_original_rsv_rec.attribute10;
1970     END IF;
1971 
1972     IF p_to_rsv_rec.attribute11 <> fnd_api.g_miss_char
1973        OR p_to_rsv_rec.attribute11 IS NULL THEN
1974       x_to_rsv_rec.attribute11  := p_to_rsv_rec.attribute11;
1975     ELSE
1976       x_to_rsv_rec.attribute11  := p_original_rsv_rec.attribute11;
1977     END IF;
1978 
1979     IF p_to_rsv_rec.attribute12 <> fnd_api.g_miss_char
1980        OR p_to_rsv_rec.attribute12 IS NULL THEN
1981       x_to_rsv_rec.attribute12  := p_to_rsv_rec.attribute12;
1982     ELSE
1983       x_to_rsv_rec.attribute12  := p_original_rsv_rec.attribute12;
1984     END IF;
1985 
1986     IF p_to_rsv_rec.attribute13 <> fnd_api.g_miss_char
1987        OR p_to_rsv_rec.attribute13 IS NULL THEN
1988       x_to_rsv_rec.attribute13  := p_to_rsv_rec.attribute13;
1989     ELSE
1990       x_to_rsv_rec.attribute13  := p_original_rsv_rec.attribute13;
1991     END IF;
1992 
1993     IF p_to_rsv_rec.attribute14 <> fnd_api.g_miss_char
1994        OR p_to_rsv_rec.attribute14 IS NULL THEN
1995       x_to_rsv_rec.attribute14  := p_to_rsv_rec.attribute14;
1996     ELSE
1997       x_to_rsv_rec.attribute14  := p_original_rsv_rec.attribute14;
1998     END IF;
1999 
2000     IF p_to_rsv_rec.attribute15 <> fnd_api.g_miss_char
2001        OR p_to_rsv_rec.attribute15 IS NULL THEN
2002       x_to_rsv_rec.attribute15  := p_to_rsv_rec.attribute15;
2003     ELSE
2004       x_to_rsv_rec.attribute15  := p_original_rsv_rec.attribute15;
2005     END IF;
2006 
2007     IF p_to_rsv_rec.ship_ready_flag <> fnd_api.g_miss_num
2008        OR p_to_rsv_rec.ship_ready_flag IS NULL THEN
2009       x_to_rsv_rec.ship_ready_flag  := p_to_rsv_rec.ship_ready_flag;
2010     ELSE
2011       x_to_rsv_rec.ship_ready_flag  := p_original_rsv_rec.ship_ready_flag;
2012     END IF;
2013 
2014     IF p_to_rsv_rec.staged_flag <> fnd_api.g_miss_char
2015        OR p_to_rsv_rec.staged_flag IS NULL THEN
2016       x_to_rsv_rec.staged_flag  := p_to_rsv_rec.staged_flag;
2017     ELSE
2018       x_to_rsv_rec.staged_flag  := p_original_rsv_rec.staged_flag;
2019     END IF;
2020 
2021     /**** {{ R12 Enhanced reservations code changes. Adding new columns to
2022     -- construct_to_reservation_row API }}****/
2023     IF p_to_rsv_rec.crossdock_flag <> fnd_api.g_miss_char
2024       OR p_to_rsv_rec.crossdock_flag IS NULL THEN
2025        x_to_rsv_rec.crossdock_flag  := p_to_rsv_rec.crossdock_flag;
2026      ELSE
2027        x_to_rsv_rec.crossdock_flag  := p_original_rsv_rec.crossdock_flag;
2028     END IF;
2029 
2030     IF p_to_rsv_rec.crossdock_criteria_id <> fnd_api.g_miss_num
2031       OR p_to_rsv_rec.crossdock_criteria_id IS NULL THEN
2032        x_to_rsv_rec.crossdock_criteria_id  := p_to_rsv_rec.crossdock_criteria_id;
2033      ELSE
2034        x_to_rsv_rec.crossdock_criteria_id  := p_original_rsv_rec.crossdock_criteria_id;
2035     END IF;
2036 
2037     IF p_to_rsv_rec.demand_source_line_detail <> fnd_api.g_miss_num
2038       OR p_to_rsv_rec.demand_source_line_detail IS NULL THEN
2039        x_to_rsv_rec.demand_source_line_detail  := p_to_rsv_rec.demand_source_line_detail;
2040      ELSE
2041        x_to_rsv_rec.demand_source_line_detail  := p_original_rsv_rec.demand_source_line_detail;
2042     END IF;
2043 
2044      IF p_to_rsv_rec.serial_reservation_quantity <> fnd_api.g_miss_num
2045       OR p_to_rsv_rec.serial_reservation_quantity IS NULL THEN
2046        x_to_rsv_rec.serial_reservation_quantity  := p_to_rsv_rec.serial_reservation_quantity;
2047      ELSE
2048        x_to_rsv_rec.serial_reservation_quantity  := p_original_rsv_rec.serial_reservation_quantity;
2049      END IF;
2050 
2051      IF p_to_rsv_rec.supply_receipt_date <> fnd_api.g_miss_date
2052       OR p_to_rsv_rec.supply_receipt_date IS NULL THEN
2053        x_to_rsv_rec.supply_receipt_date  := p_to_rsv_rec.supply_receipt_date;
2054      ELSE
2055        x_to_rsv_rec.supply_receipt_date  := p_original_rsv_rec.supply_receipt_date;
2056      END IF;
2057 
2058      IF p_to_rsv_rec.demand_ship_date <> fnd_api.g_miss_date
2059       OR p_to_rsv_rec.demand_ship_date IS NULL THEN
2060        x_to_rsv_rec.demand_ship_date  := p_to_rsv_rec.demand_ship_date;
2061      ELSE
2062        x_to_rsv_rec.demand_ship_date  := p_original_rsv_rec.demand_ship_date;
2063      END IF;
2064 
2065      IF p_to_rsv_rec.project_id <> fnd_api.g_miss_num
2066       OR p_to_rsv_rec.project_id IS NULL THEN
2067        x_to_rsv_rec.project_id  := p_to_rsv_rec.project_id;
2068      ELSE
2069        x_to_rsv_rec.project_id  := p_original_rsv_rec.project_id;
2070      END IF;
2071 
2072      IF p_to_rsv_rec.task_id <> fnd_api.g_miss_num
2073       OR p_to_rsv_rec.task_id IS NULL THEN
2074        x_to_rsv_rec.task_id  := p_to_rsv_rec.task_id;
2075      ELSE
2076        x_to_rsv_rec.task_id  := p_original_rsv_rec.task_id;
2077     END IF;
2078 
2079     /*** End R12 ***/
2080 
2081   END construct_to_reservation_row;
2082 
2083   --
2084   -- Description
2085   --   return true if the given index points to an item record
2086   --   in the cache which has a revision control code as yes
2087   FUNCTION is_revision_control(p_item_cache_index IN INTEGER)
2088     RETURN BOOLEAN IS
2089   BEGIN
2090     IF inv_reservation_global.g_item_record_cache(p_item_cache_index).revision_qty_control_code = inv_reservation_global.g_revision_control_yes THEN
2091       RETURN TRUE;
2092     ELSE
2093       RETURN FALSE;
2094     END IF;
2095   END is_revision_control;
2096 
2097   --
2098   -- Description
2099   --   return true if the given index points to an item record
2100   --   in the cache which has a lot control code as yes
2101   FUNCTION is_lot_control(p_item_cache_index IN INTEGER)
2102     RETURN BOOLEAN IS
2103   BEGIN
2104     IF inv_reservation_global.g_item_record_cache(p_item_cache_index).lot_control_code = inv_reservation_global.g_lot_control_yes THEN
2105       RETURN TRUE;
2106     ELSE
2107       RETURN FALSE;
2108     END IF;
2109   END is_lot_control;
2110 
2111   --
2112   -- Description
2113   --   return true if the given index points to an item record
2114   --   in the cache which has a serial control code as yes
2115   FUNCTION is_serial_control(p_item_cache_index IN INTEGER)
2116     RETURN BOOLEAN IS
2117   BEGIN
2118     IF inv_reservation_global.g_item_record_cache(p_item_cache_index).serial_number_control_code <> inv_reservation_global.g_serial_control_predefined THEN
2119       RETURN FALSE;
2120     ELSE
2121       RETURN TRUE;
2122     END IF;
2123   END is_serial_control;
2124 
2125   --INVCONV BEGIN
2126   --
2127   -- Description
2128   -- ===========
2129   --   return true if the given index points to an item record
2130   --   in the cache which has a dual UOM control as true
2131   FUNCTION is_dual_control(p_item_cache_index IN INTEGER)
2132      RETURN BOOLEAN IS
2133      --l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
2134   BEGIN
2135     IF inv_reservation_global.g_item_record_cache(p_item_cache_index).tracking_quantity_ind <> 'PS' THEN
2136       RETURN FALSE;
2137     ELSE
2138       RETURN TRUE;
2139     END IF;
2140   END is_dual_control;
2141 
2142   -- Description
2143   -- ===========
2144   --   return true if the given index points to an item record
2145   --   in the cache which has lot divisible true
2146   FUNCTION is_lot_divisible(p_item_cache_index IN INTEGER)
2147      RETURN BOOLEAN IS
2148     l_debug number;
2149   BEGIN
2150     IF (g_debug IS NULL) THEN
2151        g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
2152     END IF;
2153 
2154     l_debug := g_debug;
2155 
2156     IF inv_reservation_global.g_item_record_cache(p_item_cache_index).lot_control_code
2157        = inv_reservation_global.g_lot_control_yes AND
2158        inv_reservation_global.g_item_record_cache(p_item_cache_index).lot_divisible_flag <> 'Y' THEN
2159        IF (l_debug = 1) THEN
2160          debug_print('Lot divisible is FALSE ');
2161        END IF;
2162        RETURN FALSE;
2163     ELSE
2164        IF (l_debug = 1) THEN
2165          debug_print('Lot divisible is TRUE ');
2166        END IF;
2167        RETURN TRUE;
2168     END IF;
2169   END is_lot_divisible;
2170 
2171   -- B4498579 BEGIN
2172   -- Description
2173   -- ===========
2174   --   Determine lot divisibility for cases where caching is not in use
2175   --   and function is_lot_divisible above is not appropriate
2176   --   This function interrogates mtl_system_items.lot_divisible_flag
2177   FUNCTION lot_divisible(p_inventory_item_id NUMBER,p_organization_id NUMBER)
2178      RETURN BOOLEAN IS
2179 	l_debug number;
2180 	l_lot_divisible_flag VARCHAR2(1);
2181 	l_lot_control_code NUMBER;
2182   BEGIN
2183 
2184      IF (g_debug IS NULL) THEN
2185 	 g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
2186       END IF;
2187 
2188       l_debug := g_debug;
2189 
2190       BEGIN
2191 	 SELECT lot_control_code, lot_divisible_flag INTO
2192 	   l_lot_control_code, l_lot_divisible_flag FROM mtl_system_items
2193 	   WHERE inventory_item_id = p_inventory_item_id
2194 	   AND organization_id = p_organization_id;
2195       EXCEPTION
2196 	 WHEN no_data_found THEN
2197 	    IF (l_debug =1) THEN
2198 	       debug_print('could not find the record for the item id ' ||
2199 			   p_inventory_item_id || ' Org ' || p_organization_id);
2200 	    END IF;
2201       END;
2202 
2203       IF (l_debug = 1) THEN
2204 	 debug_print('Lot divisible flag set to '||l_lot_divisible_flag );
2205       END IF;
2206 
2207       IF (l_lot_control_code = 2 AND Upper(l_lot_divisible_flag) <> 'Y') THEN
2208 	 RETURN FALSE;
2209        ELSE
2210 	 RETURN TRUE;
2211       END IF;
2212 
2213   END lot_divisible;
2214   -- B4498579 END
2215 
2216   -- INVCONV END
2217 
2218   /**** {{ R12 Enhanced reservations code changes. New API get_wip_entity_type }}****/
2219   /*  This API will take a set of parameters and return the wip entity type and the job type as output. */
2220 
2221   PROCEDURE get_wip_entity_type
2222     (
2223      p_api_version_number  IN   NUMBER
2224      , p_init_msg_lst      IN   VARCHAR2 DEFAULT fnd_api.g_false
2225      , x_return_status     OUT  NOCOPY VARCHAR2
2226      , x_msg_count         OUT  NOCOPY NUMBER
2227      , x_msg_data          OUT  NOCOPY VARCHAR2
2228      , p_organization_id   IN	NUMBER DEFAULT null
2229      , p_item_id	   IN	NUMBER DEFAULT null
2230      , p_source_type_id    IN	NUMBER DEFAULT INV_RESERVATION_GLOBAL.G_SOURCE_TYPE_WIP
2231      , p_source_header_id  IN	NUMBER
2232      , p_source_line_id	   IN   NUMBER
2233      , p_source_line_detail IN	NUMBER
2234      , x_wip_entity_type   OUT NOCOPY NUMBER
2235      , x_wip_job_type	   OUT NOCOPY VARCHAR2
2236      ) IS
2237 
2238 	l_wip_entity_id NUMBER;
2239 	l_wip_entity_type NUMBER;
2240 	l_wip_job_type VARCHAR2(15);
2241 	l_maintenance_object_source NUMBER;
2242 	l_debug number;
2243 	l_api_name  CONSTANT VARCHAR2(30) := 'Get_WIP_Entity_Type';
2244 
2245   BEGIN
2246      IF (g_debug IS NULL) THEN
2247         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
2248      END IF;
2249 
2250      l_debug := g_debug;
2251 
2252      IF (p_source_type_id <> INV_RESERVATION_GLOBAL.g_source_type_wip) THEN
2253 	fnd_message.set_name('INV', 'INV_INVALID_SUPPLY_SOURCE');
2254 	fnd_msg_pub.add;
2255 	RAISE fnd_api.g_exc_error;
2256      END IF;
2257 
2258   BEGIN
2259      SELECT we.entity_type, wdj.maintenance_object_source INTO
2260        l_wip_entity_id, l_maintenance_object_source FROM wip_entities we,
2261        wip_discrete_jobs wdj WHERE we.wip_entity_id = p_source_header_id
2262        AND we.wip_entity_id = wdj.wip_entity_id(+);
2263   EXCEPTION
2264      WHEN no_data_found THEN
2265      IF (l_debug = 1) THEN
2266 	debug_print('No WIP entity record found for the source header passed' );
2267      END IF;
2268      fnd_message.set_name('INV', 'INV_INVALID_WIP_ENTITY_TYPE');
2269      fnd_msg_pub.add;
2270      RAISE fnd_api.g_exc_error;
2271   END ;
2272 
2273   IF l_wip_entity_id = inv_reservation_global.g_wip_source_type_discrete then
2274      l_wip_entity_type := inv_reservation_global.g_wip_source_type_discrete;
2275      l_wip_job_type := 'DISCRETE';
2276    ELSIF l_wip_entity_id = 2 then
2277      l_wip_entity_type := inv_reservation_global.g_wip_source_type_repetitive;
2278      l_wip_job_type := 'REPETITIVE';
2279    ELSIF l_wip_entity_id = 4 then
2280      l_wip_entity_type := inv_reservation_global.g_wip_source_type_flow;
2281      l_wip_job_type := 'FLOW';
2282    ELSIF l_wip_entity_id = 5 then
2283      l_wip_entity_type := inv_reservation_global.g_wip_source_type_osfm;
2284      l_wip_job_type := 'OSFM';
2285    ELSIF l_wip_entity_id = 6 and l_maintenance_object_source = 1 then
2286      l_wip_entity_type := inv_reservation_global.g_wip_source_type_eam;
2287      l_wip_job_type := 'EAM';
2288    ELSIF l_wip_entity_id = 6 and l_maintenance_object_source = 2  then
2289      l_wip_entity_type := inv_reservation_global.g_wip_source_type_cmro;
2290      l_wip_job_type := 'CMRO'; -- AHL
2291    ELSIF l_wip_entity_id  = 9 then
2292      l_wip_entity_type := inv_reservation_global.g_wip_source_type_fpo;
2293      l_wip_job_type := 'FPO';
2294    ELSIF l_wip_entity_id = 10 then
2295      l_wip_entity_type := inv_reservation_global.g_wip_source_type_batch;
2296      l_wip_job_type := 'BATCH';
2297    ELSIF l_wip_entity_id = 16 THEN
2298      l_wip_entity_type := inv_reservation_global.g_wip_source_type_depot;
2299      l_wip_job_type := 'DEPOT';
2300   END IF;
2301 
2302   x_wip_entity_type := l_wip_entity_type;
2303   x_wip_job_type := l_wip_job_type;
2304 
2305   EXCEPTION
2306      WHEN fnd_api.g_exc_error THEN
2307 	x_return_status  := fnd_api.g_ret_sts_error;
2308 	--  Get message count and data
2309 	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2310      WHEN fnd_api.g_exc_unexpected_error THEN
2311 	x_return_status  := fnd_api.g_ret_sts_unexp_error;
2312 	--  Get message count and data
2313 	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2314      WHEN OTHERS THEN
2315 	x_return_status  := fnd_api.g_ret_sts_unexp_error;
2316 
2317 	IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
2318 	   fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
2319 	END IF;
2320 
2321 	--  Get message count and data
2322 	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2323   END get_wip_entity_type;
2324 
2325   PROCEDURE update_serial_rsv_quantity(
2326      x_return_status       OUT NOCOPY VARCHAR2
2327    , x_msg_count           OUT NOCOPY NUMBER
2328    , x_msg_data            OUT NOCOPY VARCHAR2
2329    , p_reservation_id      IN  NUMBER
2330    , p_update_serial_qty   IN  NUMBER)
2331 
2332   IS
2333     l_api_name     CONSTANT VARCHAR2(30) := 'update_serial_rsv_quantity';
2334     l_update_count NUMBER := 0;
2335     l_debug        NUMBER;
2336   BEGIN
2337     IF (g_debug is NULL) THEN
2338         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
2339     END IF;
2340 
2341     l_debug := g_debug;
2342 
2343     IF (l_debug = 1) THEN
2344         debug_print('In update_serial_rsv_quantity');
2345         debug_print('reservation_id = ' || p_reservation_id);
2346         debug_print('update_serial_qty = ' || p_update_serial_qty);
2347     END IF;
2348 
2349     update mtl_reservations
2350     set    serial_reservation_quantity = serial_reservation_quantity + p_update_serial_qty
2351     where  reservation_id = p_reservation_id;
2352     l_update_count := SQL%ROWCOUNT;
2353 
2354     IF (l_debug = 1) THEN
2355         debug_print('Number of rows update: ' || l_update_count);
2356     END IF;
2357 
2358     x_return_status := fnd_api.g_ret_sts_success;
2359   EXCEPTION
2360     WHEN fnd_api.g_exc_error THEN
2361       x_return_status  := fnd_api.g_ret_sts_error;
2362       --  Get message count and data
2363       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2364     WHEN fnd_api.g_exc_unexpected_error THEN
2365       x_return_status  := fnd_api.g_ret_sts_unexp_error;
2366       --  Get message count and data
2367       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2368     WHEN OTHERS THEN
2369       x_return_status  := fnd_api.g_ret_sts_unexp_error;
2370 
2371       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
2372         fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
2373       END IF;
2374 
2375       --  Get message count and data
2376       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2377 
2378   END update_serial_rsv_quantity;
2379 
2380  PROCEDURE is_serial_number_reserved(
2381      p_api_version_number        IN  NUMBER
2382    , p_init_msg_lst              IN  VARCHAR2 DEFAULT fnd_api.g_false
2383    , x_return_status             OUT NOCOPY VARCHAR2
2384    , x_msg_count                 OUT NOCOPY NUMBER
2385    , x_msg_data                  OUT NOCOPY VARCHAR2
2386    , p_serial_number_tbl         IN  inv_reservation_global.serial_number_tbl_type
2387    , x_serial_number_tbl         OUT NOCOPY inv_reservation_global.rsv_serial_number_table)
2388   IS
2389     l_api_name     CONSTANT VARCHAR2(30) := 'is_serial_number_reserved';
2390     l_debug        NUMBER;
2391     l_index        NUMBER := 0;
2392   BEGIN
2393     IF (g_debug is NULL) THEN
2394         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
2395     END IF;
2396 
2397     l_debug := g_debug;
2398 
2399     IF (l_debug = 1) THEN
2400         debug_print('In is_serial_number_reserved');
2401         FOR i in 1..p_serial_number_tbl.count LOOP
2402             debug_print(i || ' : item = ' || p_serial_number_tbl(i).inventory_item_id ||
2403                         ', serial number = ' || p_serial_number_tbl(i).serial_number);
2404         END LOOP;
2405     END IF;
2406 
2407     -- reset the index for x_serial_number_tbl
2408     l_index := 0;
2409     FOR i in 1..p_serial_number_tbl.count LOOP
2410         BEGIN
2411            SELECT reservation_id, serial_number
2412            INTO   x_serial_number_tbl(l_index).reservation_id, x_serial_number_tbl(l_index).serial_number
2413            FROM   mtl_serial_numbers
2414            WHERE  serial_number = p_serial_number_tbl(i).serial_number
2415            AND    inventory_item_id = p_serial_number_tbl(i).inventory_item_id
2416            AND    reservation_id is not null;
2417            l_index := l_index + 1;
2418         EXCEPTION
2419            WHEN no_data_found THEN
2420                 IF (l_debug = 1) THEN
2421                     debug_print('serial number ' || p_serial_number_tbl(i).serial_number || ' is not reserved.');
2422                 END IF;
2423         END;
2424     END LOOP;
2425 
2426     IF (l_debug = 1) THEN
2427         FOR i in 1..x_serial_number_tbl.count LOOP
2428             debug_print(i || ': reservation id = ' || x_serial_number_tbl(i).reservation_id ||
2429                         ', serial number = ' || x_serial_number_tbl(i).serial_number);
2430         END LOOP;
2431     END IF;
2432 
2433     x_return_status := fnd_api.g_ret_sts_success;
2434   EXCEPTION
2435     WHEN fnd_api.g_exc_error THEN
2436       x_return_status  := fnd_api.g_ret_sts_error;
2437       --  Get message count and data
2438       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2439     WHEN fnd_api.g_exc_unexpected_error THEN
2440       x_return_status  := fnd_api.g_ret_sts_unexp_error;
2441       --  Get message count and data
2442       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2443     WHEN OTHERS THEN
2444       x_return_status  := fnd_api.g_ret_sts_unexp_error;
2445 
2446       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
2447         fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
2448       END IF;
2449 
2450       --  Get message count and data
2451       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2452   END is_serial_number_reserved;
2453 
2454   PROCEDURE is_serial_reserved(
2455      p_api_version_number        IN  NUMBER
2456    , p_init_msg_lst              IN  VARCHAR2 DEFAULT fnd_api.g_false
2457    , x_return_status             OUT NOCOPY VARCHAR2
2458    , x_msg_count                 OUT NOCOPY NUMBER
2459    , x_msg_data                  OUT NOCOPY VARCHAR2
2460    , p_serial_number_tbl         IN  inv_reservation_global.serial_number_tbl_type
2461    , x_serial_number_tbl         OUT NOCOPY inv_reservation_global.rsv_serial_number_table
2462    , x_mtl_reservation_tbl       OUT NOCOPY inv_reservation_global.mtl_reservation_tbl_type)
2463   IS
2464     l_api_name     CONSTANT VARCHAR2(30) := 'is_serial_number_reserved';
2465     l_debug        NUMBER;
2466     l_index        NUMBER := 0;
2467 
2468     CURSOR rsv_csr(p_reservation_id NUMBER) IS
2469        SELECT reservation_id
2470             , requirement_date
2471             , organization_id
2472             , inventory_item_id
2473             , demand_source_type_id
2474             , demand_source_name
2475             , demand_source_header_id
2476             , demand_source_line_id
2477             , demand_source_delivery
2478             , primary_uom_code
2479             , primary_uom_id
2480             , secondary_uom_code
2481             , secondary_uom_id
2482             , reservation_uom_code
2483             , reservation_uom_id
2484             , reservation_quantity
2485             , primary_reservation_quantity
2486             , secondary_reservation_quantity
2487             , detailed_quantity
2488             , secondary_detailed_quantity
2489             , autodetail_group_id
2490             , external_source_code
2491             , external_source_line_id
2492             , supply_source_type_id
2493             , supply_source_header_id
2494             , supply_source_line_id
2495             , supply_source_name
2496             , supply_source_line_detail
2497             , revision
2498             , subinventory_code
2499             , subinventory_id
2500             , locator_id
2501             , lot_number
2502             , lot_number_id
2503             , pick_slip_number
2504             , lpn_id
2505             , attribute_category
2506             , attribute1
2507             , attribute2
2508             , attribute3
2509             , attribute4
2510             , attribute5
2511             , attribute6
2512             , attribute7
2513             , attribute8
2514             , attribute9
2515             , attribute10
2516             , attribute11
2517             , attribute12
2518             , attribute13
2519             , attribute14
2520             , attribute15
2521             , ship_ready_flag
2522             , staged_flag
2523             , crossdock_flag
2524             , crossdock_criteria_id
2525             , demand_source_line_detail
2526             , serial_reservation_quantity
2527             , supply_receipt_date
2528             , demand_ship_date
2529             , project_id
2530             , task_id
2531             , orig_supply_source_type_id
2532             , orig_supply_source_header_id
2533             , orig_supply_source_line_id
2534             , orig_supply_source_line_detail
2535             , orig_demand_source_type_id
2536             , orig_demand_source_header_id
2537             , orig_demand_source_line_id
2538 	 , orig_demand_source_line_detail
2539 	 , serial_number
2540        FROM mtl_reservations
2541        WHERE reservation_id = p_reservation_id;
2542   BEGIN
2543     IF (g_debug is NULL) THEN
2544         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
2545     END IF;
2546 
2547     l_debug := g_debug;
2548 
2549     IF (l_debug = 1) THEN
2550         debug_print('In is_serial_number_reserved');
2551         FOR i in 1..p_serial_number_tbl.count LOOP
2552             debug_print(i || ' : item = ' || p_serial_number_tbl(i).inventory_item_id ||
2553                         ', serial number = ' || p_serial_number_tbl(i).serial_number);
2554         END LOOP;
2555     END IF;
2556 
2557     -- reset the index for x_serial_number_tbl
2558     l_index := 0;
2559     FOR i in 1..p_serial_number_tbl.count LOOP
2560         BEGIN
2561            SELECT reservation_id, serial_number
2562            INTO   x_serial_number_tbl(l_index).reservation_id, x_serial_number_tbl(l_index).serial_number
2563            FROM   mtl_serial_numbers
2564            WHERE  serial_number = p_serial_number_tbl(i).serial_number
2565            AND    inventory_item_id = p_serial_number_tbl(i).inventory_item_id
2566            AND    reservation_id is not null;
2567 
2568            OPEN rsv_csr(x_serial_number_tbl(l_index).reservation_id);
2569            FETCH rsv_csr into x_mtl_reservation_tbl(l_index);
2570            CLOSE rsv_csr;
2571 
2572            l_index := l_index + 1;
2573         EXCEPTION
2574            WHEN no_data_found THEN
2575                 IF (l_debug = 1) THEN
2576                     debug_print('serial number ' || p_serial_number_tbl(i).serial_number || ' is not reserved.');
2577                 END IF;
2578         END;
2579     END LOOP;
2580 
2581     IF (l_debug = 1) THEN
2582         FOR i in 1..x_serial_number_tbl.count LOOP
2583             debug_print(i || ': reservation id = ' || x_serial_number_tbl(i).reservation_id ||
2584                         ', serial number = ' || x_serial_number_tbl(i).serial_number);
2585         END LOOP;
2586     END IF;
2587 
2588     x_return_status := fnd_api.g_ret_sts_success;
2589   EXCEPTION
2590     WHEN fnd_api.g_exc_error THEN
2591       x_return_status  := fnd_api.g_ret_sts_error;
2592       --  Get message count and data
2593       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2594     WHEN fnd_api.g_exc_unexpected_error THEN
2595       x_return_status  := fnd_api.g_ret_sts_unexp_error;
2596       --  Get message count and data
2597       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2598     WHEN OTHERS THEN
2599       x_return_status  := fnd_api.g_ret_sts_unexp_error;
2600 
2601       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
2602         fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
2603       END IF;
2604 
2605       --  Get message count and data
2606       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2607   END is_serial_reserved;
2608   /*** End R12 ***/
2609 
2610 
2611   --   modify_tree_crt_del_rel
2612   -- Description
2613   --   modify quantity tree for create or delete or relieve reservations
2614 
2615   PROCEDURE modify_tree_crt_del_rel(
2616     x_return_status                OUT    NOCOPY VARCHAR2
2617   , p_tree_id                      IN     INTEGER
2618   , p_revision                     IN     VARCHAR2
2619   , p_lot_number                   IN     VARCHAR2
2620   , p_subinventory_code            IN     VARCHAR2
2621   , p_locator_id                   IN     NUMBER
2622   , p_lpn_id                       IN     NUMBER
2623   , p_primary_reservation_quantity IN     NUMBER
2624   , p_second_reservation_quantity  IN     NUMBER           -- INVCONV
2625   , p_detailed_quantity            IN     NUMBER
2626   , p_secondary_detailed_quantity  IN     NUMBER           -- INVCONV
2627   , p_relieve_quantity             IN     NUMBER DEFAULT 0
2628   , p_secondary_relieve_quantity   IN     NUMBER DEFAULT 0 -- INVCONV
2629   , p_partial_reservation_flag     IN     VARCHAR2
2630   , p_force_reservation_flag       IN     VARCHAR2
2631   , p_lot_divisible_flag           IN     VARCHAR2         -- INVCONV
2632   , p_action                       IN     VARCHAR2
2633   , x_qty_changed                  OUT    NOCOPY NUMBER
2634   , x_secondary_qty_changed        OUT    NOCOPY NUMBER    -- INVCONV
2635   ) IS
2636     l_return_status     VARCHAR2(1)   := fnd_api.g_ret_sts_success;
2637     l_qoh               NUMBER;
2638     l_rqoh              NUMBER;
2639     l_qr                NUMBER;
2640     l_qs                NUMBER;
2641     l_att               NUMBER;
2642     l_atr               NUMBER;
2643     l_sqoh              NUMBER;                          -- INVCONV
2644     l_srqoh             NUMBER;                          -- INVCONV
2645     l_sqr               NUMBER;                          -- INVCONV
2646     l_sqs               NUMBER;                          -- INVCONV
2647     l_satt              NUMBER;                          -- INVCONV
2648     l_satr              NUMBER;                          -- INVCONV
2649     l_quantity_reserved NUMBER;
2650     l_quantity_type     NUMBER;
2651     l_secondary_net_qty NUMBER;                          -- INVCONV
2652     l_dual_control      BOOLEAN := FALSE;                -- INVCONV
2653     l_msg_count         NUMBER;
2654     l_msg_data          VARCHAR2(240);
2655     l_net_qty           NUMBER;
2656     l_debug number;
2657 
2658   BEGIN
2659     -- Use cache to get value for l_debug
2660     IF g_is_pickrelease_set IS NULL THEN
2661        g_is_pickrelease_set := 2;
2662        IF INV_CACHE.is_pickrelease THEN
2663           g_is_pickrelease_set := 1;
2664        END IF;
2665     END IF;
2666     IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
2667        g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
2668     END IF;
2669 
2670     l_debug := g_debug;
2671 
2672     -- INVCONV
2673     -- Set boolean for dual control scenarios
2674     IF p_second_reservation_quantity is NOT NULL THEN
2675        l_dual_control := TRUE;
2676     END IF;
2677 
2678     -- INVCONV Include secondary parameters below
2679     inv_quantity_tree_pvt.query_tree(
2680       p_api_version_number         => 1.0
2681     , p_init_msg_lst               => fnd_api.g_true
2682     , x_return_status              => l_return_status
2683     , x_msg_count                  => l_msg_count
2684     , x_msg_data                   => l_msg_data
2685     , p_tree_id                    => p_tree_id
2686     , p_revision                   => p_revision
2687     , p_lot_number                 => p_lot_number
2688     , p_subinventory_code          => p_subinventory_code
2689     , p_locator_id                 => p_locator_id
2690     , x_qoh                        => l_qoh
2691     , x_rqoh                       => l_rqoh
2692     , x_qr                         => l_qr
2693     , x_qs                         => l_qs
2694     , x_att                        => l_att
2695     , x_atr                        => l_atr
2696     , x_sqoh                       => l_sqoh
2697     , x_srqoh                      => l_srqoh
2698     , x_sqr                        => l_sqr
2699     , x_sqs                        => l_sqs
2700     , x_satt                       => l_satt
2701     , x_satr                       => l_satr
2702     , p_lpn_id                     => p_lpn_id
2703     );
2704 
2705     IF l_return_status = fnd_api.g_ret_sts_error THEN
2706       RAISE fnd_api.g_exc_error;
2707     END IF;
2708 
2709     --
2710     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2711       RAISE fnd_api.g_exc_unexpected_error;
2712     END IF;
2713 
2714     IF (l_debug = 1) THEN
2715       debug_print('l_atr ' || l_atr);
2716       debug_print('l_att ' || l_att);
2717       debug_print('l_qoh ' || l_qoh);
2718       debug_print('l_rqoh ' || l_rqoh);
2719       debug_print('l_qr ' || l_qr);
2720       debug_print('l_qs ' || l_qs);
2721 
2722       -- INVCONV BEGIN
2723       debug_print('l_satr ' || l_satr);
2724       debug_print('l_satt ' || l_satt);
2725       debug_print('l_sqoh ' || l_sqoh);
2726       debug_print('l_srqoh ' || l_srqoh);
2727       debug_print('l_sqr ' || l_sqr);
2728       debug_print('l_sqs ' || l_sqs);
2729       -- INVCONV END
2730     END IF;
2731 
2732     --
2733     IF p_action = 'CREATE' THEN
2734       IF (l_debug = 1) THEN
2735         debug_print('CREATE');
2736         debug_print('p_primary_reservation_quantity ' || p_primary_reservation_quantity);
2737         debug_print('p_second_reservation_quantity ' || p_second_reservation_quantity);   -- INVCONV
2738         debug_print('p_detailed_quantity' || p_detailed_quantity);
2739         debug_print('p_secondary_detailed_quantity' || p_secondary_detailed_quantity);    -- INVCONV
2740       END IF;
2741       l_net_qty  := p_primary_reservation_quantity - NVL(p_detailed_quantity, 0);
2742       IF l_dual_control THEN                                                              -- INVCONV
2743         l_secondary_net_qty  := p_second_reservation_quantity - NVL(p_secondary_detailed_quantity, 0); -- INVCONV
2744       END IF;
2745       -- INVCONV BEGIN - lot_indivisible quantities cannot be split
2746       IF p_lot_divisible_flag = 'N' THEN
2747          IF l_net_qty < l_atr THEN
2748             -- Available to reserve cannot be subdivided
2749             fnd_message.set_name('INV', 'INV_LOT_INDIVISIBLE_VIOLATION');       -- INVCONV New Message
2750             fnd_msg_pub.ADD;
2751              RAISE fnd_api.g_exc_error;
2752          END IF;
2753       END IF;
2754       -- INVCONV END   - lot_indivisible quantities cannot be split
2755     ELSIF p_action = 'DELETE' THEN
2756       IF (l_debug = 1) THEN
2757         debug_print('DELETE');
2758         debug_print('p_primary_reservation_quantity ' || p_primary_reservation_quantity);
2759         debug_print('p_detailed_quantity' || p_detailed_quantity);
2760       END IF;
2761       l_net_qty  := -p_primary_reservation_quantity + NVL(p_detailed_quantity, 0);
2762     ELSIF p_action = 'RELIEVE' THEN
2763       IF (l_debug = 1) THEN
2764         debug_print('RELIEVE');
2765         debug_print('p_primary_reservation_quantity ' || p_primary_reservation_quantity);
2766         debug_print('p_second_reservation_quantity ' || p_second_reservation_quantity);   -- INVCONV
2767         debug_print('p_detailed_quantity' || p_detailed_quantity);
2768         debug_print('p_secondary_detailed_quantity' || p_secondary_detailed_quantity);    -- INVCONV
2769       END IF;
2770       l_net_qty  := -p_relieve_quantity;
2771       IF l_dual_control THEN                                                              -- INVCONV
2772         l_secondary_net_qty  := -p_secondary_relieve_quantity;
2773       END IF;                                                                             -- INVCONV
2774     ELSE
2775       RAISE fnd_api.g_exc_unexpected_error;
2776     END IF;
2777 
2778     IF  p_force_reservation_flag <> fnd_api.g_true
2779         AND l_net_qty > 0
2780         AND (l_net_qty > l_atr
2781              AND p_partial_reservation_flag = fnd_api.g_false
2782              OR l_atr <= 0
2783                 AND p_partial_reservation_flag = fnd_api.g_true
2784             ) THEN
2785       fnd_message.set_name('INV', 'INV-INVALID RESERVATION QTY');
2786       fnd_msg_pub.ADD;
2787       RAISE fnd_api.g_exc_error;
2788     END IF;
2789 
2790     IF (l_debug = 1) THEN
2791       debug_print('l_net_qty ' || l_net_qty);
2792       debug_print('l_secondary_net_qty ' || l_secondary_net_qty);                         -- INVCONV
2793     END IF;
2794 
2795     -- adjust the net quantity according to atr
2796     IF  l_net_qty > 0
2797         AND l_net_qty > l_atr THEN
2798       l_net_qty  := l_atr;
2799       IF l_dual_control THEN                                                              -- INVCONV
2800         l_secondary_net_qty  := l_satr;                                                   -- INVCONV
2801       END IF;                                                                             -- INVCONV
2802     END IF;
2803 
2804     l_quantity_type  := inv_quantity_tree_pvt.g_qr_same_demand;
2805     --
2806     inv_quantity_tree_pvt.update_quantities(
2807       p_api_version_number         => 1.0
2808     , p_init_msg_lst               => fnd_api.g_true
2809     , x_return_status              => l_return_status
2810     , x_msg_count                  => l_msg_count
2811     , x_msg_data                   => l_msg_data
2812     , p_tree_id                    => p_tree_id
2813     , p_revision                   => p_revision
2814     , p_lot_number                 => p_lot_number
2815     , p_subinventory_code          => p_subinventory_code
2816     , p_locator_id                 => p_locator_id
2817     , p_primary_quantity           => l_net_qty
2818     , p_secondary_quantity         => l_secondary_net_qty                               -- INVCONV
2819     , p_quantity_type              => l_quantity_type
2820     , x_qoh                        => l_qoh
2821     , x_rqoh                       => l_rqoh
2822     , x_qr                         => l_qr
2823     , x_qs                         => l_qs
2824     , x_att                        => l_att
2825     , x_atr                        => l_atr
2826     , x_sqoh                       => l_sqoh                                             -- INVCONV
2827     , x_srqoh                      => l_srqoh                                            -- INVCONV
2828     , x_sqr                        => l_sqr                                              -- INVCONV
2829     , x_sqs                        => l_sqs                                              -- INVCONV
2830     , x_satt                       => l_satt                                             -- INVCONV
2831     , x_satr                       => l_satr                                             -- INVCONV
2832     , p_lpn_id                     => p_lpn_id
2833     );
2834 
2835     --
2836     IF l_return_status = fnd_api.g_ret_sts_error THEN
2837       RAISE fnd_api.g_exc_error;
2838     END IF;
2839 
2840     --
2841     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2842       RAISE fnd_api.g_exc_unexpected_error;
2843     END IF;
2844 
2845     --
2846     x_qty_changed    := l_net_qty;
2847     IF l_dual_control THEN                                                               -- INVCONV
2848       x_secondary_qty_changed    := l_secondary_net_qty;                                 -- INVCONV
2849     END IF;                                                                              -- INVCONV
2850     x_return_status  := l_return_status;
2851   EXCEPTION
2852     WHEN fnd_api.g_exc_error THEN
2853       x_return_status  := fnd_api.g_ret_sts_error;
2854     --
2855     WHEN fnd_api.g_exc_unexpected_error THEN
2856       x_return_status  := fnd_api.g_ret_sts_unexp_error;
2857     --
2858     WHEN OTHERS THEN
2859       x_return_status  := fnd_api.g_ret_sts_unexp_error;
2860 
2861       --
2862       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
2863         fnd_msg_pub.add_exc_msg(g_pkg_name, 'modify_tree_crt_del_rel');
2864       END IF;
2865   END modify_tree_crt_del_rel;
2866 
2867   --
2868   -- modify the trees for update or transfer reservation
2869   -- INVCONV - Add secondaries to parameter list
2870   PROCEDURE modify_tree_for_update_xfer(
2871     x_return_status              OUT    NOCOPY VARCHAR2
2872   , x_quantity_reserved          OUT    NOCOPY NUMBER
2873   , x_secondary_quantity_reserved OUT    NOCOPY NUMBER   --INVCONV
2874   , p_from_tree_id               IN     NUMBER
2875   , p_from_supply_source_type_id IN     NUMBER
2876   , p_from_revision              IN     VARCHAR2
2877   , p_from_lot_number            IN     VARCHAR2
2878   , p_from_subinventory_code     IN     VARCHAR2
2879   , p_from_locator_id            IN     NUMBER
2880   , p_from_lpn_id                IN     NUMBER
2881   , p_from_primary_rsv_quantity  IN     NUMBER
2882   , p_from_second_rsv_quantity   IN     NUMBER
2883   , p_from_detailed_quantity     IN     NUMBER
2884   , p_from_sec_detailed_quantity IN     NUMBER
2885   , p_to_tree_id                 IN     NUMBER
2886   , p_to_supply_source_type_id   IN     NUMBER
2887   , p_to_revision                IN     VARCHAR2
2888   , p_to_lot_number              IN     VARCHAR2
2889   , p_to_subinventory_code       IN     VARCHAR2
2890   , p_to_locator_id              IN     NUMBER
2891   , p_to_lpn_id                  IN     NUMBER
2892   , p_to_primary_rsv_quantity    IN     NUMBER
2893   , p_to_second_rsv_quantity     IN     NUMBER
2894   , p_to_detailed_quantity       IN     NUMBER
2895   , p_to_second_detailed_quantity IN    NUMBER
2896   , p_to_revision_control        IN     BOOLEAN
2897   , p_to_lot_control             IN     BOOLEAN
2898   , p_action                     IN     VARCHAR2
2899   , p_lot_divisible_flag         IN     VARCHAR2         -- INVCONV
2900   , p_partial_reservation_flag   IN     VARCHAR2 DEFAULT fnd_api.g_false
2901   , p_check_availability         IN     VARCHAR2 DEFAULT fnd_api.g_false
2902   ) IS
2903 
2904     l_return_status  VARCHAR2(1)   := fnd_api.g_ret_sts_success;
2905     l_tmp_revision   VARCHAR2(3);
2906     l_tmp_lot_number VARCHAR2(80);                                  -- BUG 4180127
2907     l_net_qty1       NUMBER;
2908     l_secondary_net_qty1   NUMBER;                                  -- INVCONV
2909     l_net_qty2       NUMBER;
2910     l_secondary_net_qty2   NUMBER;                                  -- INVCONV
2911     l_qoh            NUMBER;
2912     l_rqoh           NUMBER;
2913     l_atr            NUMBER;
2914     l_att            NUMBER;
2915     l_qr             NUMBER;
2916     l_qs             NUMBER;
2917     l_sqoh           NUMBER;                                        -- INVCONV
2918     l_srqoh          NUMBER;                                        -- INVCONV
2919     l_satr           NUMBER;                                        -- INVCONV
2920     l_satt           NUMBER;                                        -- INVCONV
2921     l_sqr            NUMBER;                                        -- INVCONV
2922     l_sqs            NUMBER;                                        -- INVCONV
2923     l_dual_control   BOOLEAN;                                       -- INVCONV
2924     l_modify_tree1   BOOLEAN;
2925     l_modify_tree2   BOOLEAN;
2926     l_msg_count      NUMBER;
2927     l_msg_data       VARCHAR2(240);
2928     l_debug number;
2929 
2930   BEGIN
2931     -- Use cache to get value for l_debug
2932     IF g_is_pickrelease_set IS NULL THEN
2933        g_is_pickrelease_set := 2;
2934        IF INV_CACHE.is_pickrelease THEN
2935           g_is_pickrelease_set := 1;
2936        END IF;
2937     END IF;
2938     IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
2939        g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
2940     END IF;
2941 
2942     l_debug := g_debug;
2943 
2944      -- INVCONV BEGIN
2945      -- Set boolean for dual control scenarios
2946      IF p_from_second_rsv_quantity is NOT NULL THEN
2947         l_dual_control := TRUE;
2948      END IF;
2949      -- INVCONV END
2950 
2951     IF p_action = 'UPDATE' THEN
2952       l_net_qty1  := -p_from_primary_rsv_quantity + NVL(p_from_detailed_quantity, 0);
2953       IF l_dual_control THEN                                        -- INVCONV
2954         l_secondary_net_qty1 :=
2955            -p_from_second_rsv_quantity + NVL(p_from_sec_detailed_quantity, 0); -- INVCONV
2956       END IF;                                                       -- INVCONV
2957     ELSE -- for transfer reservation
2958       l_net_qty1  := -p_to_primary_rsv_quantity + NVL(p_to_detailed_quantity, 0);
2959       IF l_dual_control THEN                                        -- INVCONV
2960         l_secondary_net_qty1 := -p_to_second_rsv_quantity + NVL(p_to_second_detailed_quantity, 0);  -- INVCONV
2961       END IF;                                                       -- INVCONV
2962     END IF;
2963 
2964     l_net_qty2       := p_to_primary_rsv_quantity - NVL(p_to_detailed_quantity, 0);
2965     IF l_dual_control THEN                                          -- INVCONV
2966       l_secondary_net_qty2 := p_to_second_rsv_quantity - NVL(p_to_second_detailed_quantity, 0);  -- INVCONV
2967     END IF;                                                         -- INVCONV
2968     --
2969     IF p_from_supply_source_type_id = inv_reservation_global.g_source_type_inv THEN
2970        -- INVCONV - Incorporate secondaries
2971       inv_quantity_tree_pvt.update_quantities(
2972         p_api_version_number         => 1.0
2973       , p_init_msg_lst               => fnd_api.g_true
2974       , x_return_status              => l_return_status
2975       , x_msg_count                  => l_msg_count
2976       , x_msg_data                   => l_msg_data
2977       , p_tree_id                    => p_from_tree_id
2978       , p_revision                   => p_from_revision
2979       , p_lot_number                 => p_from_lot_number
2980       , p_subinventory_code          => p_from_subinventory_code
2981       , p_locator_id                 => p_from_locator_id
2982       , p_primary_quantity           => l_net_qty1
2983       , p_secondary_quantity         => l_secondary_net_qty1
2984       , p_quantity_type              => inv_quantity_tree_pvt.g_qr_other_demand
2985       , x_qoh                        => l_qoh
2986       , x_rqoh                       => l_rqoh
2987       , x_qr                         => l_qr
2988       , x_qs                         => l_qs
2989       , x_att                        => l_att
2990       , x_atr                        => l_atr
2991       , x_sqoh                       => l_sqoh
2992       , x_srqoh                      => l_srqoh
2993       , x_sqr                        => l_sqr
2994       , x_sqs                        => l_sqs
2995       , x_satt                       => l_satt
2996       , x_satr                       => l_satr
2997       , p_lpn_id                     => p_from_lpn_id
2998       );
2999 
3000       --
3001       IF l_return_status = fnd_api.g_ret_sts_error THEN
3002         RAISE fnd_api.g_exc_error;
3003       END IF;
3004 
3005       --
3006       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3007         RAISE fnd_api.g_exc_unexpected_error;
3008       END IF;
3009 
3010       --
3011       l_modify_tree1  := TRUE;
3012     ELSE
3013       l_modify_tree1  := FALSE;
3014     END IF;
3015 
3016     --
3017     l_modify_tree2   := FALSE;
3018 
3019     IF  p_to_supply_source_type_id = inv_reservation_global.g_source_type_inv
3020         AND l_modify_tree1
3021         AND p_to_tree_id IS NOT NULL
3022         AND p_from_tree_id <> p_to_tree_id THEN
3023       l_modify_tree2  := TRUE;
3024 
3025       -- check before passing the revision, and lot number
3026       -- since the second tree might have different control
3027       -- settings
3028       IF p_to_revision_control THEN
3029         l_tmp_revision  := p_from_revision;
3030       ELSE
3031         l_tmp_revision  := NULL;
3032       END IF;
3033 
3034       --
3035       IF p_to_lot_control THEN
3036         l_tmp_lot_number  := p_from_lot_number;
3037       ELSE
3038         l_tmp_lot_number  := NULL;
3039       END IF;
3040 
3041       --
3042       -- INVCONV - Incorporate secondaries into call
3043       inv_quantity_tree_pvt.update_quantities(
3044         p_api_version_number         => 1.0
3045       , p_init_msg_lst               => fnd_api.g_true
3046       , x_return_status              => l_return_status
3047       , x_msg_count                  => l_msg_count
3048       , x_msg_data                   => l_msg_data
3049       , p_tree_id                    => p_to_tree_id
3050       , p_revision                   => l_tmp_revision
3051       , p_lot_number                 => l_tmp_lot_number
3052       , p_subinventory_code          => p_from_subinventory_code
3053       , p_locator_id                 => p_from_locator_id
3054       , p_primary_quantity           => l_net_qty1
3055       , p_secondary_quantity         => l_secondary_net_qty1
3056       , p_quantity_type              => inv_quantity_tree_pvt.g_qr_other_demand
3057       , x_qoh                        => l_qoh
3058       , x_rqoh                       => l_rqoh
3059       , x_qr                         => l_qr
3060       , x_qs                         => l_qs
3061       , x_att                        => l_att
3062       , x_atr                        => l_atr
3063       , x_sqoh                       => l_sqoh
3064       , x_srqoh                      => l_srqoh
3065       , x_sqr                        => l_sqr
3066       , x_sqs                        => l_sqs
3067       , x_satt                       => l_satt
3068       , x_satr                       => l_satr
3069       , p_lpn_id                     => p_from_lpn_id
3070       );
3071 
3072       --
3073       IF l_return_status = fnd_api.g_ret_sts_error THEN
3074         -- fail to modify in tree2
3075         -- undo the modification in tree1
3076          -- INVCONV - Incorporate secondaries into call
3077         inv_quantity_tree_pvt.update_quantities(
3078           p_api_version_number         => 1.0
3079         , p_init_msg_lst               => fnd_api.g_true
3080         , x_return_status              => l_return_status
3081         , x_msg_count                  => l_msg_count
3082         , x_msg_data                   => l_msg_data
3083         , p_tree_id                    => p_from_tree_id
3084         , p_revision                   => p_from_revision
3085         , p_lot_number                 => p_from_lot_number
3086         , p_subinventory_code          => p_from_subinventory_code
3087         , p_locator_id                 => p_from_locator_id
3088         , p_primary_quantity           => -l_net_qty1
3089         , p_secondary_quantity         => -l_secondary_net_qty1
3090         , p_quantity_type              => inv_quantity_tree_pvt.g_qr_same_demand
3091         , x_qoh                        => l_qoh
3092         , x_rqoh                       => l_rqoh
3093         , x_qr                         => l_qr
3094         , x_qs                         => l_qs
3095         , x_att                        => l_att
3096         , x_atr                        => l_atr
3097         , x_sqoh                       => l_sqoh
3098         , x_srqoh                      => l_srqoh
3099         , x_sqr                        => l_sqr
3100         , x_sqs                        => l_sqs
3101         , x_satt                       => l_satt
3102         , x_satr                       => l_satr
3103         , p_lpn_id                     => p_from_lpn_id
3104         );
3105 
3106         IF l_return_status = fnd_api.g_ret_sts_error THEN
3107           -- can not add the reservation back on the tree
3108           -- panic
3109           RAISE fnd_api.g_exc_unexpected_error;
3110         END IF;
3111 
3112         --
3113         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3114           RAISE fnd_api.g_exc_unexpected_error;
3115         END IF;
3116 
3117         --
3118         -- recover the quantity tree1 correctly, but
3119         -- since we failed to modify the tree, raise expected error
3120         RAISE fnd_api.g_exc_error;
3121       END IF;
3122 
3123       --
3124       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3125         RAISE fnd_api.g_exc_unexpected_error;
3126       END IF;
3127     END IF;
3128 --Start changes for Bug Number#3336837
3129 -- Bug Number 3447373 Removed the qty validation for transfers
3130 
3131 IF p_action='UPDATE' AND p_from_supply_source_type_id = inv_reservation_global.g_source_type_inv then
3132     IF (l_debug = 1) THEN
3133       debug_print('Validate Qty :Action Update');
3134     END IF;
3135 
3136      -- INVCONV - Incorporate secondaries into call
3137      inv_quantity_tree_pvt.query_tree(
3138       p_api_version_number         => 1.0
3139     , p_init_msg_lst               => fnd_api.g_true
3140     , x_return_status              => l_return_status
3141     , x_msg_count                  => l_msg_count
3142     , x_msg_data                   => l_msg_data
3143     , p_tree_id                    => p_from_tree_id
3144     , p_revision                   => p_from_revision
3145     , p_lot_number                 => p_from_lot_number
3146     , p_subinventory_code          => p_from_subinventory_code
3147     , p_locator_id                 => p_from_locator_id
3148     , x_qoh                        => l_qoh
3149     , x_rqoh                       => l_rqoh
3150     , x_qr                         => l_qr
3151     , x_qs                         => l_qs
3152     , x_att                        => l_att
3153     , x_atr                        => l_atr
3154     , x_sqoh                       => l_sqoh
3155     , x_srqoh                      => l_srqoh
3156     , x_sqr                        => l_sqr
3157     , x_sqs                        => l_sqs
3158     , x_satt                       => l_satt
3159     , x_satr                       => l_satr
3160     , p_lpn_id                     => p_from_lpn_id
3161     );
3162 
3163     IF l_return_status = fnd_api.g_ret_sts_error THEN
3164       RAISE fnd_api.g_exc_unexpected_error;
3165     END IF;
3166 
3167     --
3168     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3169       RAISE fnd_api.g_exc_unexpected_error;
3170     END IF;
3171 
3172     -- INVCONV BEGIN - lot indivisible quantities must be fully consumed
3173     IF p_lot_divisible_flag = 'N' THEN
3174       IF (l_debug = 1) THEN
3175          debug_print('Lot indivisible scenario so compare l_atr '||l_atr ||' with l_net_qty2 '||l_net_qty2);
3176       END IF;
3177       IF l_atr > l_net_qty2 THEN
3178         fnd_message.set_name('INV', 'INV_LOT_INDIVISIBLE_VIOLATION');
3179         fnd_msg_pub.ADD;
3180         RAISE fnd_api.g_exc_error;
3181       END IF;
3182     END IF;
3183     -- INVCONV END
3184 
3185  END IF;
3186 
3187    IF (l_debug = 1) THEN
3188       debug_print('l_atr '||l_atr);
3189       debug_print('l_rqoh '||l_rqoh);
3190       debug_print('l_net_qty2 '||l_net_qty2);
3191    END IF;
3192 
3193    -- Bug 6141096: Fail the reservation if available to reserve(ATR) is less than or equal to zero, even if the
3194    -- the partial reservation flag is set to true.
3195    IF ( p_partial_reservation_flag = fnd_api.g_true  AND l_net_qty2 > l_atr AND l_atr > 0)THEN -- Bug 6141096
3196     l_net_qty2 := l_atr;
3197     x_quantity_reserved := l_net_qty2;
3198 
3199     IF l_dual_control THEN                                          -- INVCONV
3200       l_secondary_net_qty2 := l_satr;                               -- INVCONV
3201       x_secondary_quantity_reserved := l_secondary_net_qty2;        -- INVCONV
3202     END IF;                                                         -- INVCONV
3203    ELSIF (      l_net_qty2 > l_atr
3204            AND  (p_partial_reservation_flag <> fnd_api.g_true OR l_atr <= 0) -- Bug 6141096
3205            AND  p_action='UPDATE'
3206            AND  p_check_availability = fnd_api.g_true )THEN
3207     --rollback quantity tree changes .
3208        IF l_modify_tree1 THEN
3209         -- INVCONV Incorporate secondaries
3210         inv_quantity_tree_pvt.update_quantities(
3211           p_api_version_number         => 1.0
3212         , p_init_msg_lst               => fnd_api.g_true
3213         , x_return_status              => l_return_status
3214         , x_msg_count                  => l_msg_count
3215         , x_msg_data                   => l_msg_data
3216         , p_tree_id                    => p_from_tree_id
3217         , p_revision                   => p_from_revision
3218         , p_lot_number                 => p_from_lot_number
3219         , p_subinventory_code          => p_from_subinventory_code
3220         , p_locator_id                 => p_from_locator_id
3221         , p_primary_quantity           => -l_net_qty1
3222         , p_secondary_quantity         => -l_secondary_net_qty1
3223         , p_quantity_type              => inv_quantity_tree_pvt.g_qr_same_demand
3224         , x_qoh                        => l_qoh
3225         , x_rqoh                       => l_rqoh
3226         , x_qr                         => l_qr
3227         , x_qs                         => l_qs
3228         , x_att                        => l_att
3229         , x_atr                        => l_atr
3230         , x_sqoh                       => l_sqoh
3231         , x_srqoh                      => l_srqoh
3232         , x_sqr                        => l_sqr
3233         , x_sqs                        => l_sqs
3234         , x_satt                       => l_satt
3235         , x_satr                       => l_satr
3236         , p_lpn_id                     => p_from_lpn_id
3237         );
3238         IF (l_debug = 1) THEN
3239            debug_print('return status from Rollback Tree1 changes '||l_return_status);
3240         END IF;
3241 
3242         IF l_return_status = fnd_api.g_ret_sts_error THEN
3243           -- can not add the reservation back on the tree
3244           -- panic
3245           RAISE fnd_api.g_exc_unexpected_error;
3246         END IF;
3247 
3248         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3249           RAISE fnd_api.g_exc_unexpected_error;
3250         END IF;
3251        END IF;
3252 
3253 
3254       fnd_message.set_name('INV', 'INV-INVALID RESERVATION QTY');
3255       fnd_msg_pub.ADD;
3256       RAISE fnd_api.g_exc_error;
3257     END IF;
3258 --Changed for Qty validation. Bug 3336837
3259 
3260     --
3261     -- adding the quantity to the to tree
3262     --Bug 1575930
3263     -- The from tree must also be updated with the quantity, since the
3264     --  from tree and the to tree are for the same organization and item
3265 
3266      -- INVCONV Incorporate Secondaries in call
3267     inv_quantity_tree_pvt.update_quantities(
3268       p_api_version_number         => 1.0
3269     , p_init_msg_lst               => fnd_api.g_true
3270     , x_return_status              => l_return_status
3271     , x_msg_count                  => l_msg_count
3272     , x_msg_data                   => l_msg_data
3273     , p_tree_id                    => p_from_tree_id
3274     , p_revision                   => p_from_revision
3275     , p_lot_number                 => p_from_lot_number
3276     , p_subinventory_code          => p_from_subinventory_code
3277     , p_locator_id                 => p_from_locator_id
3278     , p_primary_quantity           => l_net_qty2
3279     , p_secondary_quantity         => l_secondary_net_qty2
3280     , p_quantity_type              => inv_quantity_tree_pvt.g_qr_other_demand
3281     , x_qoh                        => l_qoh
3282     , x_rqoh                       => l_rqoh
3283     , x_qr                         => l_qr
3284     , x_qs                         => l_qs
3285     , x_att                        => l_att
3286     , x_atr                        => l_atr
3287     , x_sqoh                       => l_sqoh
3288     , x_srqoh                      => l_srqoh
3289     , x_sqr                        => l_sqr
3290     , x_sqs                        => l_sqs
3291     , x_satt                       => l_satt
3292     , x_satr                       => l_satr
3293     , p_lpn_id                     => p_from_lpn_id
3294     );
3295 
3296     IF  l_return_status = fnd_api.g_ret_sts_success
3297         AND l_modify_tree2 THEN
3298 
3299       -- INVCONV Incorporate Secondaries in call
3300       inv_quantity_tree_pvt.update_quantities(
3301         p_api_version_number         => 1.0
3302       , p_init_msg_lst               => fnd_api.g_true
3303       , x_return_status              => l_return_status
3304       , x_msg_count                  => l_msg_count
3305       , x_msg_data                   => l_msg_data
3306       , p_tree_id                    => p_to_tree_id
3307       , p_revision                   => p_to_revision
3308       , p_lot_number                 => p_to_lot_number
3309       , p_subinventory_code          => p_to_subinventory_code
3310       , p_locator_id                 => p_to_locator_id
3311       , p_primary_quantity           => l_net_qty2
3312       , p_secondary_quantity         => l_secondary_net_qty2
3313       , p_quantity_type              => inv_quantity_tree_pvt.g_qr_same_demand
3314       , x_qoh                        => l_qoh
3315       , x_rqoh                       => l_rqoh
3316       , x_qr                         => l_qr
3317       , x_qs                         => l_qs
3318       , x_att                        => l_att
3319       , x_atr                        => l_atr
3320       , x_sqoh                       => l_sqoh
3321       , x_srqoh                      => l_srqoh
3322       , x_sqr                        => l_sqr
3323       , x_sqs                        => l_sqs
3324       , x_satt                       => l_satt
3325       , x_satr                       => l_satr
3326       , p_lpn_id                     => p_to_lpn_id
3327       );
3328     END IF;
3329 
3330     IF l_return_status = fnd_api.g_ret_sts_error THEN
3331       -- fail to modify in tree2
3332       -- undo the modification in tree1 if l_modify_tree1 is true
3333       IF l_modify_tree1 THEN
3334         -- INVCONV - Incorporate secondaries
3335         inv_quantity_tree_pvt.update_quantities(
3336           p_api_version_number         => 1.0
3337         , p_init_msg_lst               => fnd_api.g_true
3338         , x_return_status              => l_return_status
3339         , x_msg_count                  => l_msg_count
3340         , x_msg_data                   => l_msg_data
3341         , p_tree_id                    => p_from_tree_id
3342         , p_revision                   => p_from_revision
3343         , p_lot_number                 => p_from_lot_number
3344         , p_subinventory_code          => p_from_subinventory_code
3345         , p_locator_id                 => p_from_locator_id
3346         , p_primary_quantity           => -l_net_qty1
3347         , p_secondary_quantity         => -l_secondary_net_qty1
3348         , p_quantity_type              => inv_quantity_tree_pvt.g_qr_same_demand
3349         , x_qoh                        => l_qoh
3350         , x_rqoh                       => l_rqoh
3351         , x_qr                         => l_qr
3352         , x_qs                         => l_qs
3353         , x_att                        => l_att
3354         , x_atr                        => l_atr
3355         , x_sqoh                       => l_sqoh
3356         , x_srqoh                      => l_srqoh
3357         , x_sqr                        => l_sqr
3358         , x_sqs                        => l_sqs
3359         , x_satt                       => l_satt
3360         , x_satr                       => l_satr
3361         , p_lpn_id                     => p_from_lpn_id
3362         );
3363 
3364         IF l_return_status = fnd_api.g_ret_sts_error THEN
3365           -- can not add the reservation back on the tree
3366           -- panic
3367           RAISE fnd_api.g_exc_unexpected_error;
3368         END IF;
3369 
3370         --
3371         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3372           RAISE fnd_api.g_exc_unexpected_error;
3373         END IF;
3374 
3375         --
3376         IF l_modify_tree2 THEN
3377           -- INVCONV - Incorporate secondaries
3378           inv_quantity_tree_pvt.update_quantities(
3379             p_api_version_number         => 1.0
3380           , p_init_msg_lst               => fnd_api.g_true
3381           , x_return_status              => l_return_status
3382           , x_msg_count                  => l_msg_count
3383           , x_msg_data                   => l_msg_data
3384           , p_tree_id                    => p_to_tree_id
3385           , p_revision                   => l_tmp_revision
3386           , p_lot_number                 => l_tmp_lot_number
3387           , p_subinventory_code          => p_from_subinventory_code
3388           , p_locator_id                 => p_from_locator_id
3389           , p_primary_quantity           => -l_net_qty1
3390           , p_secondary_quantity         => -l_secondary_net_qty1
3391           , p_quantity_type              => inv_quantity_tree_pvt.g_qr_other_demand
3392           , x_qoh                        => l_qoh
3393           , x_rqoh                       => l_rqoh
3394           , x_qr                         => l_qr
3395           , x_qs                         => l_qs
3396           , x_att                        => l_att
3397           , x_atr                        => l_atr
3398           , x_sqoh                       => l_sqoh
3399           , x_srqoh                      => l_srqoh
3400           , x_sqr                        => l_sqr
3401           , x_sqs                        => l_sqs
3402           , x_satt                       => l_satt
3403           , x_satr                       => l_satr
3404           , p_lpn_id                     => p_from_lpn_id
3405           );
3406 
3407           IF l_return_status = fnd_api.g_ret_sts_error THEN
3408             -- can not add the reservation back on the tree
3409             -- panic
3410             RAISE fnd_api.g_exc_unexpected_error;
3411           END IF;
3412 
3413           --
3414           IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3415             RAISE fnd_api.g_exc_unexpected_error;
3416           END IF;
3417         END IF;
3418 
3419         -- recover the quantity tree1 correctly, but
3420         -- since we failed to modify the tree, raise expected error
3421         RAISE fnd_api.g_exc_error;
3422       END IF;
3423 
3424       --
3425       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3426         RAISE fnd_api.g_exc_unexpected_error;
3427       END IF;
3428     END IF;
3429 
3430     x_return_status  := l_return_status;
3431     x_quantity_reserved := l_net_qty2;
3432 
3433     IF l_dual_control THEN                                          -- INVCONV
3434       x_secondary_quantity_reserved := l_secondary_net_qty2;        -- INVCONV
3435     END IF;                                                         -- INVCONV
3436 
3437   --
3438   EXCEPTION
3439     WHEN fnd_api.g_exc_error THEN
3440       x_return_status  := fnd_api.g_ret_sts_error;
3441     --
3442     WHEN fnd_api.g_exc_unexpected_error THEN
3443       x_return_status  := fnd_api.g_ret_sts_unexp_error;
3444     --
3445     WHEN OTHERS THEN
3446       x_return_status  := fnd_api.g_ret_sts_unexp_error;
3447 
3448       --
3449       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
3450         fnd_msg_pub.add_exc_msg(g_pkg_name, 'modify_tree_for_update_xfer');
3451       END IF;
3452   END modify_tree_for_update_xfer;
3453 
3454   --
3455   -- Procedure
3456   --   query_resevation
3457   -- Description
3458   --   This  procedure returns all reservations that satisfy the user
3459   --   specified criteria.
3460   PROCEDURE query_reservation(
3461     p_api_version_number        IN     NUMBER
3462   , p_init_msg_lst              IN     VARCHAR2 DEFAULT fnd_api.g_false
3463   , x_return_status             OUT    NOCOPY VARCHAR2
3464   , x_msg_count                 OUT    NOCOPY NUMBER
3465   , x_msg_data                  OUT    NOCOPY VARCHAR2
3466   , p_query_input               IN     inv_reservation_global.mtl_reservation_rec_type
3467   , p_lock_records              IN     VARCHAR2 DEFAULT fnd_api.g_false
3468   , p_sort_by_req_date          IN     NUMBER DEFAULT inv_reservation_global.g_query_no_sort
3469   , p_cancel_order_mode         IN     NUMBER DEFAULT inv_reservation_global.g_cancel_order_no
3470   , x_mtl_reservation_tbl       OUT    NOCOPY inv_reservation_global.mtl_reservation_tbl_type
3471   , x_mtl_reservation_tbl_count OUT    NOCOPY NUMBER
3472   , x_error_code                OUT    NOCOPY NUMBER
3473   ) IS
3474     l_api_version_number CONSTANT NUMBER                                          := 1.0;
3475     l_api_name           CONSTANT VARCHAR2(30)                                    := 'Query_Reservation';
3476     l_return_status               VARCHAR2(1)                                     := fnd_api.g_ret_sts_success;
3477     l_counter                     INTEGER;
3478     l_rsv_rec                     inv_reservation_global.mtl_reservation_rec_type;
3479     l_sort_stmt                   VARCHAR2(2000);
3480     l_lock_stmt                   VARCHAR2(60);
3481     -- Bug 4881317
3482     l_qry_stmt                    VARCHAR2(15000);
3483     l_cursor_ref                  query_cur_ref_type;
3484     l_miss_num                    NUMBER                                          := fnd_api.g_miss_num;
3485     l_miss_char                   VARCHAR2(1)                                     := fnd_api.g_miss_char;
3486     l_miss_date                   DATE                                            := fnd_api.g_miss_date;
3487     l_update                      BOOLEAN                                         := FALSE;
3488     l_sort_default                BOOLEAN                                         := FALSE;
3489     l_res_cursor                  BOOLEAN                                         := FALSE;
3490     l_demand_cursor               BOOLEAN := FALSE;
3491     l_debug NUMBER := 0;
3492 
3493     -- Bug 3560916: When multiple processes are accessing the same
3494     -- reservation record, the query reservation is failing because of
3495     -- NOLOCK. This happens from pick confirm while transferring
3496     -- reservations. Removed the nowait from c_res_id_update cursor.
3497 
3498     -- INVCONV - Incorporate secondary quantities
3499     CURSOR c_res_id_update IS
3500        SELECT
3501 	 reservation_id
3502 	 , requirement_date
3503 	 , organization_id
3504 	 , inventory_item_id
3505 	 , demand_source_type_id
3506 	 , demand_source_name
3507 	 , demand_source_header_id
3508 	 , demand_source_line_id
3509 	 , demand_source_delivery
3510 	 , primary_uom_code
3511 	 , primary_uom_id
3512 	 , secondary_uom_code
3513 	 , secondary_uom_id
3514 	 , reservation_uom_code
3515 	 , reservation_uom_id
3516 	 , reservation_quantity
3517 	 , primary_reservation_quantity
3518 	 , secondary_reservation_quantity
3519 	 , detailed_quantity
3520 	 , secondary_detailed_quantity
3521 	 , autodetail_group_id
3522 	 , external_source_code
3523 	 , external_source_line_id
3524 	 , supply_source_type_id
3525 	 , supply_source_header_id
3526 	 , supply_source_line_id
3527 	 , supply_source_name
3528 	 , supply_source_line_detail
3529 	 , revision
3530 	 , subinventory_code
3531 	 , subinventory_id
3532 	 , locator_id
3533 	 , lot_number
3534 	 , lot_number_id
3535 	 , pick_slip_number
3536 	 , lpn_id
3537 	 , attribute_category
3538 	 , attribute1
3539 	 , attribute2
3540 	 , attribute3
3541 	 , attribute4
3542 	 , attribute5
3543 	 , attribute6
3544 	 , attribute7
3545 	 , attribute8
3546 	 , attribute9
3547 	 , attribute10
3548 	 , attribute11
3549 	 , attribute12
3550 	 , attribute13
3551 	 , attribute14
3552 	 , attribute15
3553 	 , ship_ready_flag
3554 	 , staged_flag
3555 	 /**** {{ R12 Enhanced reservations code changes. Adding new
3556 	 -- columns to query reservations. id passed for update}}****/
3557 	 , crossdock_flag
3558 	 , crossdock_criteria_id
3559 	 , demand_source_line_detail
3560 	 , serial_reservation_quantity
3561 	 , supply_receipt_date
3562 	 , demand_ship_date
3563 	 , project_id
3564 	 , task_id
3565 	 , orig_supply_source_type_id
3566 	 , orig_supply_source_header_id
3567 	 , orig_supply_source_line_id
3568 	 , orig_supply_source_line_detail
3569 	 , orig_demand_source_type_id
3570 	 , orig_demand_source_header_id
3571 	 , orig_demand_source_line_id
3572 	 , orig_demand_source_line_detail
3573 	 , serial_number
3574 	 /***** End R12 ***/
3575 	 FROM mtl_reservations
3576 	 WHERE reservation_id = p_query_input.reservation_id
3577 	 FOR UPDATE --NOWAIT
3578 	 ORDER BY NVL(revision, ' '), NVL(lot_number, ' '), NVL(subinventory_code, ' '), NVL(locator_id, 0);
3579 
3580     -- INVCONV - Incorporate secondary quantities
3581     CURSOR c_res_id IS
3582        SELECT
3583 	 reservation_id
3584 	 , requirement_date
3585 	 , organization_id
3586 	 , inventory_item_id
3587 	 , demand_source_type_id
3588 	 , demand_source_name
3589 	 , demand_source_header_id
3590 	 , demand_source_line_id
3591 	 , demand_source_delivery
3592 	 , primary_uom_code
3593 	 , primary_uom_id
3594 	 , secondary_uom_code
3595 	 , secondary_uom_id
3596 	 , reservation_uom_code
3597 	 , reservation_uom_id
3598 	 , reservation_quantity
3599 	 , primary_reservation_quantity
3600 	 , secondary_reservation_quantity
3601 	 , detailed_quantity
3602 	 , secondary_detailed_quantity
3603 	 , autodetail_group_id
3604 	 , external_source_code
3605 	 , external_source_line_id
3606 	 , supply_source_type_id
3607 	 , supply_source_header_id
3608 	 , supply_source_line_id
3609 	 , supply_source_name
3610 	 , supply_source_line_detail
3611 	 , revision
3612 	 , subinventory_code
3613 	 , subinventory_id
3614 	 , locator_id
3615 	 , lot_number
3616 	 , lot_number_id
3617 	 , pick_slip_number
3618 	 , lpn_id
3619 	 , attribute_category
3620 	 , attribute1
3621 	 , attribute2
3622 	 , attribute3
3623 	 , attribute4
3624 	 , attribute5
3625 	 , attribute6
3626 	 , attribute7
3627 	 , attribute8
3628 	 , attribute9
3629 	 , attribute10
3630 	 , attribute11
3631 	 , attribute12
3632 	 , attribute13
3633 	 , attribute14
3634 	 , attribute15
3635 	 , ship_ready_flag
3636 	 , staged_flag
3637 	 /**** {{ R12 Enhanced reservations code changes. Adding new
3638 	 -- columns to query reservations. id passed no update}}****/
3639 	 , crossdock_flag
3640 	 , crossdock_criteria_id
3641 	 , demand_source_line_detail
3642 	 , serial_reservation_quantity
3643 	 , supply_receipt_date
3644 	 , demand_ship_date
3645 	 , project_id
3646 	 , task_id
3647 	 , orig_supply_source_type_id
3648 	 , orig_supply_source_header_id
3649 	 , orig_supply_source_line_id
3650 	 , orig_supply_source_line_detail
3651 	 , orig_demand_source_type_id
3652 	 , orig_demand_source_header_id
3653 	 , orig_demand_source_line_id
3654 	 , orig_demand_source_line_detail
3655 	 , serial_number
3656 	 /***** End R12 ***/
3657 	 FROM mtl_reservations
3658          WHERE reservation_id = p_query_input.reservation_id
3659 	 ORDER BY NVL(revision, ' '), NVL(lot_number, ' '), NVL(subinventory_code, ' '), NVL(locator_id, 0);
3660 
3661     -- INVCONV - Incorporate secondary quantities
3662     --modified the order of conditions and removed unnecessary null conditions to improve the performance as part of bug 7343600
3663     CURSOR c_demand_update IS
3664        SELECT
3665 	 reservation_id
3666 	 , requirement_date
3667 	 , organization_id
3668 	 , inventory_item_id
3669 	 , demand_source_type_id
3670 	 , demand_source_name
3671 	 , demand_source_header_id
3672 	 , demand_source_line_id
3673 	 , demand_source_delivery
3674 	 , primary_uom_code
3675 	 , primary_uom_id
3676 	 , secondary_uom_code
3677 	 , secondary_uom_id
3678 	 , reservation_uom_code
3679 	 , reservation_uom_id
3680 	 , reservation_quantity
3681 	 , primary_reservation_quantity
3682 	 , secondary_reservation_quantity
3683 	 , detailed_quantity
3684 	 , secondary_detailed_quantity
3685 	 , autodetail_group_id
3686 	 , external_source_code
3687 	 , external_source_line_id
3688 	 , supply_source_type_id
3689 	 , supply_source_header_id
3690 	 , supply_source_line_id
3691 	 , supply_source_name
3692 	 , supply_source_line_detail
3693 	 , revision
3694 	 , subinventory_code
3695 	 , subinventory_id
3696 	 , locator_id
3697 	 , lot_number
3698 	 , lot_number_id
3699 	 , pick_slip_number
3700 	 , lpn_id
3701 	 , attribute_category
3702 	 , attribute1
3703 	 , attribute2
3704 	 , attribute3
3705 	 , attribute4
3706 	 , attribute5
3707 	 , attribute6
3708 	 , attribute7
3709 	 , attribute8
3710 	 , attribute9
3711 	 , attribute10
3712 	 , attribute11
3713 	 , attribute12
3714 	 , attribute13
3715 	 , attribute14
3716 	 , attribute15
3717 	 , ship_ready_flag
3718 	 , staged_flag
3719 	  /**** {{ R12 Enhanced reservations code changes. Adding new
3720 	 -- columns to query reservations. demand passed }}****/
3721 	 , crossdock_flag
3722 	 , crossdock_criteria_id
3723 	 , demand_source_line_detail
3724 	 , serial_reservation_quantity
3725 	 , supply_receipt_date
3726 	 , demand_ship_date
3727 	 , project_id
3728 	 , task_id
3729 	 , orig_supply_source_type_id
3730 	 , orig_supply_source_header_id
3731 	 , orig_supply_source_line_id
3732 	 , orig_supply_source_line_detail
3733 	 , orig_demand_source_type_id
3734 	 , orig_demand_source_header_id
3735 	 , orig_demand_source_line_id
3736 	 , orig_demand_source_line_detail
3737 	 , serial_number
3738 	 /***** End R12 ***/
3739 	 FROM mtl_reservations
3740 	 WHERE demand_source_line_id = p_query_input.demand_source_line_id
3741 	     AND (p_query_input.lpn_id = l_miss_num
3742                   OR p_query_input.lpn_id IS NULL
3743                      AND lpn_id IS NULL
3744                   OR p_query_input.lpn_id = lpn_id
3745                  )
3746 	     AND
3747 		 (p_query_input.organization_id = l_miss_num
3748                   OR organization_id = p_query_input.organization_id
3749                  )
3750              AND (p_query_input.inventory_item_id = l_miss_num
3751                    OR inventory_item_id = p_query_input.inventory_item_id
3752                  )
3753 	     AND (p_query_input.requirement_date = l_miss_date
3754                   OR  requirement_date = p_query_input.requirement_date
3755                  )
3756              AND (p_query_input.demand_source_type_id = l_miss_num
3757                   OR demand_source_type_id = p_query_input.demand_source_type_id
3758                  )
3759              AND (p_query_input.demand_source_header_id = l_miss_num
3760                   OR p_query_input.demand_source_header_id IS NULL
3761                      AND demand_source_header_id IS NULL
3762                   OR p_query_input.demand_source_header_id = demand_source_header_id
3763                  )
3764              AND (p_query_input.demand_source_name = l_miss_char
3765                   OR p_query_input.demand_source_name IS NULL
3766                      AND demand_source_name IS NULL
3767                   OR p_query_input.demand_source_name = demand_source_name
3768                  )
3769              AND (p_query_input.demand_source_delivery = l_miss_num
3770                   OR p_query_input.demand_source_delivery IS NULL
3771                      AND demand_source_delivery IS NULL
3772                   OR p_query_input.demand_source_delivery = demand_source_delivery
3773                  )
3774              AND (p_query_input.primary_uom_code = l_miss_char
3775                   OR p_query_input.primary_uom_code IS NULL
3776                      AND primary_uom_code IS NULL
3777                   OR p_query_input.primary_uom_code = primary_uom_code
3778                  )
3779              AND (p_query_input.primary_uom_id = l_miss_num
3780                   OR p_query_input.primary_uom_id IS NULL
3781                      AND primary_uom_id IS NULL
3782                   OR p_query_input.primary_uom_id = primary_uom_id
3783                  )
3784 
3785               -- INVCONV BEGIN
3786              AND (p_query_input.secondary_uom_code = l_miss_char
3787                   OR p_query_input.secondary_uom_code IS NULL
3788                      AND secondary_uom_code IS NULL
3789                   OR p_query_input.secondary_uom_code = secondary_uom_code
3790                  )
3791              AND (p_query_input.secondary_uom_id = l_miss_num
3792                   OR p_query_input.secondary_uom_id IS NULL
3793                      AND secondary_uom_id IS NULL
3794                   OR p_query_input.secondary_uom_id = secondary_uom_id
3795                  )
3796              -- INVCONV END
3797              AND (p_query_input.reservation_uom_code = l_miss_char
3798                   OR p_query_input.reservation_uom_code IS NULL
3799                      AND reservation_uom_code IS NULL
3800                   OR p_query_input.reservation_uom_code = reservation_uom_code
3801                  )
3802              AND (p_query_input.reservation_uom_id = l_miss_num
3803                   OR p_query_input.reservation_uom_id IS NULL
3804                      AND reservation_uom_id IS NULL
3805                   OR p_query_input.reservation_uom_id = reservation_uom_id
3806                  )
3807              AND (p_query_input.autodetail_group_id = l_miss_num
3808                   OR p_query_input.autodetail_group_id IS NULL
3809                      AND autodetail_group_id IS NULL
3810                   OR p_query_input.autodetail_group_id = autodetail_group_id
3811                  )
3812              AND (p_query_input.external_source_code = l_miss_char
3813                   OR p_query_input.external_source_code IS NULL
3814                      AND external_source_code IS NULL
3815                   OR p_query_input.external_source_code = external_source_code
3816                  )
3817              AND (p_query_input.external_source_line_id = l_miss_num
3818                   OR p_query_input.external_source_line_id IS NULL
3819                      AND external_source_line_id IS NULL
3820                   OR p_query_input.external_source_line_id = external_source_line_id
3821                  )
3822              AND (p_query_input.supply_source_type_id = l_miss_num
3823                   OR supply_source_type_id = p_query_input.supply_source_type_id
3824                  )
3825              AND (p_query_input.supply_source_header_id = l_miss_num
3826                   OR p_query_input.supply_source_header_id IS NULL
3827                      AND supply_source_header_id IS NULL
3828                   OR p_query_input.supply_source_header_id = supply_source_header_id
3829                  )
3830              AND (p_query_input.supply_source_line_id = l_miss_num
3831                   OR p_query_input.supply_source_line_id IS NULL
3832                      AND supply_source_line_id IS NULL
3833                   OR p_query_input.supply_source_line_id = supply_source_line_id
3834                  )
3835              AND (p_query_input.supply_source_name = l_miss_char
3836                   OR p_query_input.supply_source_name IS NULL
3837                      AND supply_source_name IS NULL
3838                   OR p_query_input.supply_source_name = supply_source_name
3839                  )
3840              AND (p_query_input.supply_source_line_detail = l_miss_num
3841                   OR p_query_input.supply_source_line_detail IS NULL
3842                      AND supply_source_line_detail IS NULL
3843                   OR p_query_input.supply_source_line_detail = supply_source_line_detail
3844                  )
3845              AND (p_query_input.revision = l_miss_char
3846                   OR p_query_input.revision IS NULL
3847                      AND revision IS NULL
3848                   OR p_query_input.revision = revision
3849                  )
3850              AND (p_query_input.subinventory_code = l_miss_char
3851                   OR p_query_input.subinventory_code IS NULL
3852                      AND subinventory_code IS NULL
3853                   OR p_query_input.subinventory_code = subinventory_code
3854                  )
3855              AND (p_query_input.subinventory_id = l_miss_num
3856                   OR p_query_input.subinventory_id IS NULL
3857                      AND subinventory_id IS NULL
3858                   OR p_query_input.subinventory_id = subinventory_id
3859                  )
3860              AND (p_query_input.locator_id = l_miss_num
3861                   OR p_query_input.locator_id IS NULL
3862                      AND locator_id IS NULL
3863                   OR p_query_input.locator_id = locator_id
3864                  )
3865              AND (p_query_input.lot_number = l_miss_char
3866                   OR p_query_input.lot_number IS NULL
3867                      AND lot_number IS NULL
3868                   OR p_query_input.lot_number = lot_number
3869                  )
3870              AND (p_query_input.lot_number_id = l_miss_num
3871                   OR p_query_input.lot_number_id IS NULL
3872                      AND lot_number_id IS NULL
3873                   OR p_query_input.lot_number_id = lot_number_id
3874                  )
3875              AND (p_query_input.ship_ready_flag = l_miss_num
3876                   OR (p_query_input.ship_ready_flag IS NULL OR p_query_input.ship_ready_flag = 2)
3877                      AND (ship_ready_flag IS NULL OR ship_ready_flag = 2)
3878                   OR p_query_input.ship_ready_flag = ship_ready_flag
3879                  )
3880              AND (p_query_input.staged_flag = l_miss_char
3881                   OR (p_query_input.staged_flag IS NULL OR p_query_input.staged_flag = 'N')
3882                      AND (staged_flag IS NULL OR staged_flag = 'N')
3883                   OR p_query_input.staged_flag = staged_flag
3884                  )
3885              AND (p_query_input.attribute_category = l_miss_char
3886                   OR p_query_input.attribute_category IS NULL
3887                      AND attribute_category IS NULL
3888                   OR p_query_input.attribute_category = attribute_category
3889                  )
3890              AND (p_query_input.attribute1 = l_miss_char
3891                   OR p_query_input.attribute1 IS NULL
3892                      AND attribute1 IS NULL
3893                   OR p_query_input.attribute1 = attribute1
3894                  )
3895              AND (p_query_input.attribute2 = l_miss_char
3896                   OR p_query_input.attribute2 IS NULL
3897                      AND attribute2 IS NULL
3898                   OR p_query_input.attribute2 = attribute2
3899                  )
3900              AND (p_query_input.attribute3 = l_miss_char
3901                   OR p_query_input.attribute3 IS NULL
3902                      AND attribute3 IS NULL
3903                   OR p_query_input.attribute3 = attribute3
3904                  )
3905              AND (p_query_input.attribute4 = l_miss_char
3906                   OR p_query_input.attribute4 IS NULL
3907                      AND attribute4 IS NULL
3908                   OR p_query_input.attribute4 = attribute4
3909                  )
3910              AND (p_query_input.attribute5 = l_miss_char
3911                   OR p_query_input.attribute5 IS NULL
3912                      AND attribute5 IS NULL
3913                   OR p_query_input.attribute5 = attribute5
3914                  )
3915              AND (p_query_input.attribute6 = l_miss_char
3916                   OR p_query_input.attribute6 IS NULL
3917                      AND attribute6 IS NULL
3918                   OR p_query_input.attribute6 = attribute6
3919                  )
3920              AND (p_query_input.attribute7 = l_miss_char
3921                   OR p_query_input.attribute7 IS NULL
3922                      AND attribute7 IS NULL
3923                   OR p_query_input.attribute7 = attribute7
3924                  )
3925              AND (p_query_input.attribute8 = l_miss_char
3926                   OR p_query_input.attribute8 IS NULL
3927                      AND attribute8 IS NULL
3928                   OR p_query_input.attribute8 = attribute8
3929                  )
3930              AND (p_query_input.attribute9 = l_miss_char
3931                   OR p_query_input.attribute9 IS NULL
3932                      AND attribute9 IS NULL
3933                   OR p_query_input.attribute9 = attribute9
3934                  )
3935              AND (p_query_input.attribute10 = l_miss_char
3936                   OR p_query_input.attribute10 IS NULL
3937                      AND attribute10 IS NULL
3938                   OR p_query_input.attribute10 = attribute10
3939                  )
3940              AND (p_query_input.attribute11 = l_miss_char
3941                   OR p_query_input.attribute11 IS NULL
3942                      AND attribute11 IS NULL
3943                   OR p_query_input.attribute11 = attribute11
3944                  )
3945              AND (p_query_input.attribute12 = l_miss_char
3946                   OR p_query_input.attribute12 IS NULL
3947                      AND attribute12 IS NULL
3948                   OR p_query_input.attribute12 = attribute12
3949                  )
3950              AND (p_query_input.attribute13 = l_miss_char
3951                   OR p_query_input.attribute13 IS NULL
3952                      AND attribute13 IS NULL
3953                   OR p_query_input.attribute13 = attribute13
3954                  )
3955              AND (p_query_input.attribute14 = l_miss_char
3956                   OR p_query_input.attribute14 IS NULL
3957                      AND attribute14 IS NULL
3958                   OR p_query_input.attribute14 = attribute14
3959                  )
3960              AND (p_query_input.attribute15 = l_miss_char
3961                   OR p_query_input.attribute15 IS NULL
3962                      AND attribute15 IS NULL
3963                   OR p_query_input.attribute15 = attribute15
3964 		  )
3965 	 /**** {{ R12 Enhanced reservations code changes. query reservation
3966 		       -- where clause}}****/
3967 	AND (p_query_input.crossdock_flag = l_miss_char
3968                   OR p_query_input.crossdock_flag IS NULL
3969                      AND crossdock_flag IS NULL
3970                   OR p_query_input.crossdock_flag = crossdock_flag
3971 	     )
3972 	AND (p_query_input.crossdock_criteria_id = l_miss_num
3973                   OR p_query_input.crossdock_criteria_id IS NULL
3974                      AND crossdock_criteria_id IS NULL
3975                   OR p_query_input.crossdock_criteria_id = crossdock_criteria_id
3976 	)
3977 	AND (p_query_input.demand_source_line_detail = l_miss_num
3978                   OR p_query_input.demand_source_line_detail IS NULL
3979                      AND demand_source_line_detail IS NULL
3980                   OR p_query_input.demand_source_line_detail = demand_source_line_detail
3981 	)
3982 	AND (p_query_input.supply_receipt_date = l_miss_date
3983                   OR p_query_input.supply_receipt_date IS NULL
3984                      AND supply_receipt_date IS NULL
3985                   OR p_query_input.supply_receipt_date = supply_receipt_date
3986 	)
3987 	AND (p_query_input.demand_ship_date = l_miss_date
3988                   OR p_query_input.demand_ship_date IS NULL
3989                      AND demand_ship_date IS NULL
3990                   OR p_query_input.demand_ship_date = demand_ship_date
3991 	)
3992 	AND (p_query_input.project_id = l_miss_num
3993                   OR p_query_input.project_id IS NULL
3994                      AND project_id IS NULL
3995                   OR p_query_input.project_id = project_id
3996 	)
3997 	AND (p_query_input.task_id = l_miss_num
3998                   OR p_query_input.task_id IS NULL
3999                      AND task_id IS NULL
4000                   OR p_query_input.task_id = task_id
4001 	)
4002 	 /***** End R12 ***/
4003       FOR UPDATE NOWAIT
4004         ORDER BY NVL(revision, ' '), NVL(lot_number, ' '), NVL(subinventory_code, ' '), NVL(locator_id, 0);
4005 
4006     -- INVCONV - Incorporate secondary quantities
4007     CURSOR c_demand IS
4008        SELECT
4009 	 reservation_id
4010 	 , requirement_date
4011 	 , organization_id
4012 	 , inventory_item_id
4013 	 , demand_source_type_id
4014 	 , demand_source_name
4015 	 , demand_source_header_id
4016 	 , demand_source_line_id
4017 	 , demand_source_delivery
4018 	 , primary_uom_code
4019 	 , primary_uom_id
4020 	 , secondary_uom_code
4021 	 , secondary_uom_id
4022 	 , reservation_uom_code
4023 	 , reservation_uom_id
4024 	 , reservation_quantity
4025 	 , primary_reservation_quantity
4026 	 , secondary_reservation_quantity
4027 	 , detailed_quantity
4028 	 , secondary_detailed_quantity
4029 	 , autodetail_group_id
4030 	 , external_source_code
4031 	 , external_source_line_id
4032 	 , supply_source_type_id
4033 	 , supply_source_header_id
4034 	 , supply_source_line_id
4035 	 , supply_source_name
4036 	 , supply_source_line_detail
4037 	 , revision
4038 	 , subinventory_code
4039 	 , subinventory_id
4040 	 , locator_id
4041 	 , lot_number
4042 	 , lot_number_id
4043 	 , pick_slip_number
4044 	 , lpn_id
4045 	 , attribute_category
4046 	 , attribute1
4047 	 , attribute2
4048 	 , attribute3
4049 	 , attribute4
4050 	 , attribute5
4051 	 , attribute6
4052 	 , attribute7
4053 	 , attribute8
4054 	 , attribute9
4055 	 , attribute10
4056 	 , attribute11
4057 	 , attribute12
4058 	 , attribute13
4059 	 , attribute14
4060 	 , attribute15
4061 	 , ship_ready_flag
4062 	 , staged_flag
4063 	  /**** {{ R12 Enhanced reservations code changes. Adding new
4064 	 -- columns to query reservations. demand passed }}****/
4065 	 , crossdock_flag
4066 	 , crossdock_criteria_id
4067 	 , demand_source_line_detail
4068 	 , serial_reservation_quantity
4069 	 , supply_receipt_date
4070 	 , demand_ship_date
4071 	 , project_id
4072 	 , task_id
4073 	 , orig_supply_source_type_id
4074 	 , orig_supply_source_header_id
4075 	 , orig_supply_source_line_id
4076 	 , orig_supply_source_line_detail
4077 	 , orig_demand_source_type_id
4078 	 , orig_demand_source_header_id
4079 	 , orig_demand_source_line_id
4080 	 , orig_demand_source_line_detail
4081 	 , serial_number
4082 	 /***** End R12 ***/
4083 	 FROM mtl_reservations
4084          WHERE demand_source_line_id = p_query_input.demand_source_line_id
4085            AND (p_query_input.requirement_date = l_miss_date
4086                 OR p_query_input.requirement_date IS NULL
4087                    AND requirement_date IS NULL
4088                 OR p_query_input.requirement_date = requirement_date
4089                )
4090            AND (p_query_input.organization_id = l_miss_num
4091                 OR p_query_input.organization_id IS NULL
4092                    AND organization_id IS NULL
4093                 OR p_query_input.organization_id = organization_id
4094                )
4095            AND (p_query_input.inventory_item_id = l_miss_num
4096                 OR p_query_input.inventory_item_id IS NULL
4097                    AND inventory_item_id IS NULL
4098                 OR p_query_input.inventory_item_id = inventory_item_id
4099                )
4100            AND (p_query_input.demand_source_type_id = l_miss_num
4101                 OR p_query_input.demand_source_type_id IS NULL
4102                    AND demand_source_type_id IS NULL
4103                 OR p_query_input.demand_source_type_id = demand_source_type_id
4104                )
4105            AND (p_query_input.demand_source_header_id = l_miss_num
4106                 OR p_query_input.demand_source_header_id IS NULL
4107                    AND demand_source_header_id IS NULL
4108                 OR p_query_input.demand_source_header_id = demand_source_header_id
4109                )
4110            AND (p_query_input.demand_source_name = l_miss_char
4111                 OR p_query_input.demand_source_name IS NULL
4112                    AND demand_source_name IS NULL
4113                 OR p_query_input.demand_source_name = demand_source_name
4114                )
4115            AND (p_query_input.demand_source_delivery = l_miss_num
4116                 OR p_query_input.demand_source_delivery IS NULL
4117                    AND demand_source_delivery IS NULL
4118                 OR p_query_input.demand_source_delivery = demand_source_delivery
4119                )
4120            AND (p_query_input.primary_uom_code = l_miss_char
4121                 OR p_query_input.primary_uom_code IS NULL
4122                    AND primary_uom_code IS NULL
4123                 OR p_query_input.primary_uom_code = primary_uom_code
4124                )
4125            AND (p_query_input.primary_uom_id = l_miss_num
4126                 OR p_query_input.primary_uom_id IS NULL
4127                    AND primary_uom_id IS NULL
4128                 OR p_query_input.primary_uom_id = primary_uom_id
4129                )
4130 
4131            -- INVCONV BEGIN
4132            AND (p_query_input.secondary_uom_code = l_miss_char
4133                 OR p_query_input.secondary_uom_code IS NULL
4134                    AND secondary_uom_code IS NULL
4135                 OR p_query_input.secondary_uom_code = secondary_uom_code
4136                )
4137            AND (p_query_input.secondary_uom_id = l_miss_num
4138                 OR p_query_input.secondary_uom_id IS NULL
4139                    AND secondary_uom_id IS NULL
4140                 OR p_query_input.secondary_uom_id = secondary_uom_id
4141                )
4142            -- INVCONV END
4143            AND (p_query_input.reservation_uom_code = l_miss_char
4144                 OR p_query_input.reservation_uom_code IS NULL
4145                    AND reservation_uom_code IS NULL
4146                 OR p_query_input.reservation_uom_code = reservation_uom_code
4147                )
4148            AND (p_query_input.reservation_uom_id = l_miss_num
4149                 OR p_query_input.reservation_uom_id IS NULL
4150                    AND reservation_uom_id IS NULL
4151                 OR p_query_input.reservation_uom_id = reservation_uom_id
4152                )
4153            AND (p_query_input.autodetail_group_id = l_miss_num
4154                 OR p_query_input.autodetail_group_id IS NULL
4155                    AND autodetail_group_id IS NULL
4156                 OR p_query_input.autodetail_group_id = autodetail_group_id
4157                )
4158            AND (p_query_input.external_source_code = l_miss_char
4159                 OR p_query_input.external_source_code IS NULL
4160                    AND external_source_code IS NULL
4161                 OR p_query_input.external_source_code = external_source_code
4162                )
4163            AND (p_query_input.external_source_line_id = l_miss_num
4164                 OR p_query_input.external_source_line_id IS NULL
4165                    AND external_source_line_id IS NULL
4166                 OR p_query_input.external_source_line_id = external_source_line_id
4167                )
4168            AND (p_query_input.supply_source_type_id = l_miss_num
4169                 OR p_query_input.supply_source_type_id IS NULL
4170                    AND supply_source_type_id IS NULL
4171                 OR p_query_input.supply_source_type_id = supply_source_type_id
4172                )
4173            AND (p_query_input.supply_source_header_id = l_miss_num
4174                 OR p_query_input.supply_source_header_id IS NULL
4175                    AND supply_source_header_id IS NULL
4176                 OR p_query_input.supply_source_header_id = supply_source_header_id
4177                )
4178            AND (p_query_input.supply_source_line_id = l_miss_num
4179                 OR p_query_input.supply_source_line_id IS NULL
4180                    AND supply_source_line_id IS NULL
4181                 OR p_query_input.supply_source_line_id = supply_source_line_id
4182                )
4183            AND (p_query_input.supply_source_name = l_miss_char
4184                 OR p_query_input.supply_source_name IS NULL
4185                    AND supply_source_name IS NULL
4186                 OR p_query_input.supply_source_name = supply_source_name
4187                )
4188            AND (p_query_input.supply_source_line_detail = l_miss_num
4189                 OR p_query_input.supply_source_line_detail IS NULL
4190                    AND supply_source_line_detail IS NULL
4191                 OR p_query_input.supply_source_line_detail = supply_source_line_detail
4192                )
4193            AND (p_query_input.revision = l_miss_char
4194                 OR p_query_input.revision IS NULL
4195                    AND revision IS NULL
4196                 OR p_query_input.revision = revision
4197                )
4198            AND (p_query_input.subinventory_code = l_miss_char
4199                 OR p_query_input.subinventory_code IS NULL
4200                    AND subinventory_code IS NULL
4201                 OR p_query_input.subinventory_code = subinventory_code
4202                )
4203            AND (p_query_input.subinventory_id = l_miss_num
4204                 OR p_query_input.subinventory_id IS NULL
4205                    AND subinventory_id IS NULL
4206                 OR p_query_input.subinventory_id = subinventory_id
4207                )
4208            AND (p_query_input.locator_id = l_miss_num
4209                 OR p_query_input.locator_id IS NULL
4210                    AND locator_id IS NULL
4211                 OR p_query_input.locator_id = locator_id
4212                )
4213            AND (p_query_input.lot_number = l_miss_char
4214                 OR p_query_input.lot_number IS NULL
4215                    AND lot_number IS NULL
4216                 OR p_query_input.lot_number = lot_number
4217                )
4218            AND (p_query_input.lot_number_id = l_miss_num
4219                 OR p_query_input.lot_number_id IS NULL
4220                    AND lot_number_id IS NULL
4221                 OR p_query_input.lot_number_id = lot_number_id
4222                )
4223            AND (p_query_input.lpn_id = l_miss_num
4224                 OR p_query_input.lpn_id IS NULL
4225                    AND lpn_id IS NULL
4226                 OR p_query_input.lpn_id = lpn_id
4227                )
4228            AND (p_query_input.ship_ready_flag = l_miss_num
4229                 OR (p_query_input.ship_ready_flag IS NULL OR p_query_input.ship_ready_flag = 2)
4230                    AND (ship_ready_flag IS NULL OR ship_ready_flag = 2)
4231                 OR p_query_input.ship_ready_flag = ship_ready_flag
4232                )
4233            AND (p_query_input.staged_flag = l_miss_char
4234                 OR (p_query_input.staged_flag IS NULL OR p_query_input.staged_flag = 'N')
4235                    AND (staged_flag IS NULL OR staged_flag = 'N')
4236                 OR p_query_input.staged_flag = staged_flag
4237                )
4238            AND (p_query_input.attribute_category = l_miss_char
4239                 OR p_query_input.attribute_category IS NULL
4240                    AND attribute_category IS NULL
4241                 OR p_query_input.attribute_category = attribute_category
4242                )
4243            AND (p_query_input.attribute1 = l_miss_char
4244                 OR p_query_input.attribute1 IS NULL
4245                    AND attribute1 IS NULL
4246                 OR p_query_input.attribute1 = attribute1
4247                )
4248            AND (p_query_input.attribute2 = l_miss_char
4249                 OR p_query_input.attribute2 IS NULL
4250                    AND attribute2 IS NULL
4251                 OR p_query_input.attribute2 = attribute2
4252                )
4253            AND (p_query_input.attribute3 = l_miss_char
4254                 OR p_query_input.attribute3 IS NULL
4255                    AND attribute3 IS NULL
4256                 OR p_query_input.attribute3 = attribute3
4257                )
4258            AND (p_query_input.attribute4 = l_miss_char
4259                 OR p_query_input.attribute4 IS NULL
4260                    AND attribute4 IS NULL
4261                 OR p_query_input.attribute4 = attribute4
4262                )
4263            AND (p_query_input.attribute5 = l_miss_char
4264                 OR p_query_input.attribute5 IS NULL
4265                    AND attribute5 IS NULL
4266                 OR p_query_input.attribute5 = attribute5
4267                )
4268            AND (p_query_input.attribute6 = l_miss_char
4269                 OR p_query_input.attribute6 IS NULL
4270                    AND attribute6 IS NULL
4271                 OR p_query_input.attribute6 = attribute6
4272                )
4273            AND (p_query_input.attribute7 = l_miss_char
4274                 OR p_query_input.attribute7 IS NULL
4275                    AND attribute7 IS NULL
4276                 OR p_query_input.attribute7 = attribute7
4277                )
4278            AND (p_query_input.attribute8 = l_miss_char
4279                 OR p_query_input.attribute8 IS NULL
4280                    AND attribute8 IS NULL
4281                 OR p_query_input.attribute8 = attribute8
4282                )
4283            AND (p_query_input.attribute9 = l_miss_char
4284                 OR p_query_input.attribute9 IS NULL
4285                    AND attribute9 IS NULL
4286                 OR p_query_input.attribute9 = attribute9
4287                )
4288            AND (p_query_input.attribute10 = l_miss_char
4289                 OR p_query_input.attribute10 IS NULL
4290                    AND attribute10 IS NULL
4291                 OR p_query_input.attribute10 = attribute10
4292                )
4293            AND (p_query_input.attribute11 = l_miss_char
4294                 OR p_query_input.attribute11 IS NULL
4295                    AND attribute11 IS NULL
4296                 OR p_query_input.attribute11 = attribute11
4297                )
4298            AND (p_query_input.attribute12 = l_miss_char
4299                 OR p_query_input.attribute12 IS NULL
4300                    AND attribute12 IS NULL
4301                 OR p_query_input.attribute12 = attribute12
4302                )
4303            AND (p_query_input.attribute13 = l_miss_char
4304                 OR p_query_input.attribute13 IS NULL
4305                    AND attribute13 IS NULL
4306                 OR p_query_input.attribute13 = attribute13
4307                )
4308            AND (p_query_input.attribute14 = l_miss_char
4309                 OR p_query_input.attribute14 IS NULL
4310                    AND attribute14 IS NULL
4311                 OR p_query_input.attribute14 = attribute14
4312                )
4313            AND (p_query_input.attribute15 = l_miss_char
4314                 OR p_query_input.attribute15 IS NULL
4315                    AND attribute15 IS NULL
4316                 OR p_query_input.attribute15 = attribute15
4317 		)
4318 		/**** {{ R12 Enhanced reservations code changes }}****/
4319 	AND (p_query_input.crossdock_flag = l_miss_char
4320                   OR p_query_input.crossdock_flag IS NULL
4321                      AND crossdock_flag IS NULL
4322                   OR p_query_input.crossdock_flag = crossdock_flag
4323 	     )
4324 	AND (p_query_input.crossdock_criteria_id = l_miss_num
4325                   OR p_query_input.crossdock_criteria_id IS NULL
4326                      AND crossdock_criteria_id IS NULL
4327                   OR p_query_input.crossdock_criteria_id = crossdock_criteria_id
4328 	)
4329 	AND (p_query_input.demand_source_line_detail = l_miss_num
4330                   OR p_query_input.demand_source_line_detail IS NULL
4331                      AND demand_source_line_detail IS NULL
4332                   OR p_query_input.demand_source_line_detail = demand_source_line_detail
4333 	)
4334 	AND (p_query_input.supply_receipt_date = l_miss_date
4335                   OR p_query_input.supply_receipt_date IS NULL
4336                      AND supply_receipt_date IS NULL
4337                   OR p_query_input.supply_receipt_date = supply_receipt_date
4338 	)
4339 	AND (p_query_input.demand_ship_date = l_miss_date
4340                   OR p_query_input.demand_ship_date IS NULL
4341                      AND demand_ship_date IS NULL
4342                   OR p_query_input.demand_ship_date = demand_ship_date
4343 	)
4344 	AND (p_query_input.project_id = l_miss_num
4345                   OR p_query_input.project_id IS NULL
4346                      AND project_id IS NULL
4347                   OR p_query_input.project_id = project_id
4348 	)
4349 	AND (p_query_input.task_id = l_miss_num
4350                   OR p_query_input.task_id IS NULL
4351                      AND task_id IS NULL
4352                   OR p_query_input.task_id = task_id
4353 	)
4354 	 /***** End R12 ***/
4355 
4356 
4357       ORDER BY NVL(revision, ' '), NVL(lot_number, ' '), NVL(subinventory_code, ' '), NVL(locator_id, 0);
4358   BEGIN
4359     x_error_code                 := inv_reservation_global.g_err_unexpected;
4360 
4361     -- Use cache to get value for l_debug
4362     IF g_is_pickrelease_set IS NULL THEN
4363        g_is_pickrelease_set := 2;
4364        IF INV_CACHE.is_pickrelease THEN
4365           g_is_pickrelease_set := 1;
4366        END IF;
4367     END IF;
4368     IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
4369        g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
4370     END IF;
4371 
4372     l_debug := g_debug;
4373 
4374     IF (l_debug = 1) then
4375        debug_print('Inside Query reservations...');
4376     END IF;
4377 
4378     --  Standard call to check for call compatibility
4379     IF NOT fnd_api.compatible_api_call(l_api_version_number, p_api_version_number, l_api_name, g_pkg_name) THEN
4380       RAISE fnd_api.g_exc_unexpected_error;
4381     END IF;
4382 
4383     --
4384     --  Initialize message list.
4385     IF fnd_api.to_boolean(p_init_msg_lst) THEN
4386       fnd_msg_pub.initialize;
4387     END IF;
4388 
4389     --
4390     IF (l_debug = 1) then
4391        debug_print('Lock Records: ' || p_lock_records);
4392        debug_print('p_sort_by_req_date: ' || p_sort_by_req_date);
4393        debug_print('p_cancel_order_mode: ' || p_cancel_order_mode);
4394     END IF;
4395 
4396 
4397     IF p_lock_records = fnd_api.g_true THEN
4398       l_lock_stmt  := ' FOR UPDATE NOWAIT';
4399       l_update     := TRUE;
4400     END IF;
4401 
4402     --
4403     -- Pl. note the order by precedence
4404     -- 1. By requirement_date - asc or desc (For WIP)
4405     -- 2. By ship_ready_flag, detailed quantity (For OE/OM)
4406     -- 3. By revision,lot,sub,locator - high level reservations 1st(For default)
4407     --
4408     IF p_sort_by_req_date = inv_reservation_global.g_query_req_date_asc THEN
4409        l_sort_stmt  := ' ORDER BY REQUIREMENT_DATE ASC ';
4410      ELSIF p_sort_by_req_date = inv_reservation_global.g_query_req_date_desc THEN
4411        l_sort_stmt  := ' ORDER BY REQUIREMENT_DATE DESC ';
4412        /**** {{ R12 Enhanced reservations code changes. Adding new sort by conditions }}****/
4413      ELSIF p_sort_by_req_date = inv_reservation_global.g_query_demand_ship_date_asc THEN
4414        l_sort_stmt  := ' ORDER BY NVL(DEMAND_SHIP_DATE,REQUIREMENT_DATE) ASC ';
4415      ELSIF p_sort_by_req_date = inv_reservation_global.g_query_demand_ship_date_desc THEN
4416        l_sort_stmt  := ' ORDER BY NVL(DEMAND_SHIP_DATE,REQUIREMENT_DATE) DESC ';
4417      ELSIF p_sort_by_req_date = inv_reservation_global.g_query_supply_rcpt_date_asc THEN
4418        l_sort_stmt  := ' ORDER BY NVL(SUPPLY_SHIP_DATE,REQUIREMENT_DATE) ASC ';
4419      ELSIF p_sort_by_req_date = inv_reservation_global.g_query_supply_rcpt_date_asc THEN
4420        l_sort_stmt  := ' ORDER BY NVL(SUPPLY_SHIP_DATE,REQUIREMENT_DATE) DESC ';
4421        /*** End R12 ***/
4422 
4423      ELSIF p_cancel_order_mode = inv_reservation_global.g_cancel_order_yes THEN
4424        l_sort_stmt  := ' ORDER BY NVL(SHIP_READY_FLAG,2) DESC, ' || 'NVL(DETAILED_QUANTITY,0) ';
4425      ELSE
4426        -- Default order by - High level followed by detail
4427        l_sort_default  := TRUE;
4428        l_sort_stmt     := ' ORDER BY NVL(REVISION, ' || ' '' ''), ' || ' NVL(LOT_NUMBER, ' || ' '' ''), ' || ' NVL(SUBINVENTORY_CODE, ' || ' '' ''), ' || ' NVL(LOCATOR_ID,0) ';
4429        -- /*** Easier to read this
4430        --  ORDER BY NVL(REVISION,          ' '),
4431        --	 NVL(LOT_NUMBER,        ' '),
4432        --	 NVL(SUBINVENTORY_CODE, ' '),
4433        --	 NVL(LOCATOR_ID,0) ****/
4434     END IF;
4435 
4436     IF (l_debug = 1) then
4437        debug_print('Reservation ID: ' || p_query_input.reservation_id);
4438        debug_print('demand_source_line_id: ' || p_query_input.demand_source_line_id);
4439        debug_print('inventory_item_id: ' || p_query_input.inventory_item_id);
4440        debug_print('organization_id: ' || p_query_input.organization_id);
4441        debug_print('supply source header id: ' || p_query_input.supply_source_header_id);
4442        debug_print('supply source line id: ' || p_query_input.supply_source_line_id);
4443        debug_print('supply source type id: ' || p_query_input.supply_source_type_id);
4444        IF l_sort_default then
4445 	  debug_print('l_sort_default is true');
4446 	ELSE
4447 	  debug_print('l_sort_default is flase');
4448        END IF;
4449        IF l_update then
4450 	  debug_print('l_update is true ');
4451 	ELSE
4452 	  debug_print('l_update is false ');
4453        END IF;
4454     END IF;
4455 
4456     IF  p_query_input.reservation_id <> fnd_api.g_miss_num
4457       AND p_query_input.reservation_id IS NOT NULL THEN
4458        IF (l_debug = 1) then
4459 	  debug_print(' Inside ref cursor for reservation id');
4460        END IF;
4461        IF l_sort_default THEN
4462 	  IF l_update THEN
4463 	     IF (l_debug = 1) then
4464 		debug_print(' Open res cursor for update');
4465 	     END IF;
4466 	     OPEN c_res_id_update;
4467 	     l_res_cursor  := TRUE;
4468 	   ELSE
4469 	     IF (l_debug = 1) then
4470 		debug_print(' Open res cursor for NO update');
4471 	     END IF;
4472 	     OPEN c_res_id;
4473 	    l_res_cursor  := TRUE;
4474 	  END IF;
4475 	  IF (l_debug = 1) and l_res_cursor then
4476 	     debug_print('l_res_cursor is true ');
4477 	   ELSE
4478 	     debug_print('l_res_cursor is false ');
4479 	  END IF;
4480 	ELSE
4481         -- INVCONV - Incorporate secondary quantities
4482 	  OPEN l_cursor_ref FOR    'SELECT
4483 	    reservation_id
4484 	  , requirement_date
4485 	  , organization_id
4486 	  , inventory_item_id
4487 	  , demand_source_type_id
4488 	  , demand_source_name
4489 	  , demand_source_header_id
4490 	  , demand_source_line_id
4491 	  , demand_source_delivery
4492 	  , primary_uom_code
4493 	  , primary_uom_id
4494 	  , secondary_uom_code
4495 	  , secondary_uom_id
4496 	  , reservation_uom_code
4497 	  , reservation_uom_id
4498 	  , reservation_quantity
4499 	  , primary_reservation_quantity
4500 	  , secondary_reservation_quantity
4501 	  , detailed_quantity
4502 	  , secondary_detailed_quantity
4503 	  , autodetail_group_id
4504 	  , external_source_code
4505 	  , external_source_line_id
4506 	  , supply_source_type_id
4507 	  , supply_source_header_id
4508 	  , supply_source_line_id
4509 	  , supply_source_name
4510 	  , supply_source_line_detail
4511 	  , revision
4512 	  , subinventory_code
4513 	  , subinventory_id
4514 	  , locator_id
4515 	  , lot_number
4516 	  , lot_number_id
4517 	  , pick_slip_number
4518 	  , lpn_id
4519 	  , attribute_category
4520 	  , attribute1
4521 	  , attribute2
4522 	  , attribute3
4523 	  , attribute4
4524 	  , attribute5
4525 	  , attribute6
4526 	  , attribute7
4527 	  , attribute8
4528 	  , attribute9
4529 	  , attribute10
4530 	  , attribute11
4531 	  , attribute12
4532 	  , attribute13
4533 	  , attribute14
4534 	  , attribute15
4535 	  , ship_ready_flag
4536 	  , staged_flag
4537 	   /**** {{ R12 Enhanced reservations code changes. Adding new
4538 	    -- columns for query reservations }}****/
4539 	 , crossdock_flag
4540 	 , crossdock_criteria_id
4541 	 , demand_source_line_detail
4542 	 , serial_reservation_quantity
4543 	 , supply_receipt_date
4544 	 , demand_ship_date
4545 	 , project_id
4546 	 , task_id
4547 	 , orig_supply_source_type_id
4548 	 , orig_supply_source_header_id
4549 	 , orig_supply_source_line_id
4550 	 , orig_supply_source_line_detail
4551 	 , orig_demand_source_type_id
4552 	 , orig_demand_source_header_id
4553 	 , orig_demand_source_line_id
4554 	 , orig_demand_source_line_detail
4555          , serial_number
4556 	 /***** End R12 ***/
4557         FROM mtl_reservations
4558         WHERE
4559         :reservation_id = reservation_id '
4560                               || l_lock_stmt
4561                               || l_sort_stmt USING p_query_input.reservation_id;
4562       END IF;
4563     ELSIF  p_query_input.demand_source_line_id <> fnd_api.g_miss_num
4564       AND p_query_input.demand_source_line_id IS NOT NULL THEN
4565        IF (l_debug = 1) then
4566 	  debug_print(' Inside ref cursor for demand');
4567        END IF;
4568        IF l_sort_default THEN
4569 	  IF l_update THEN
4570 	     l_demand_cursor  := TRUE;
4571 	     IF (l_debug = 1) then
4572 		debug_print(' Open demand cursor for update');
4573 	     END IF;
4574 	     OPEN c_demand_update;
4575 	   ELSE
4576 	     l_demand_cursor  := TRUE;
4577 	     IF (l_debug = 1) then
4578 		debug_print(' Open demand cursor for NO update');
4579 	     END IF;
4580 	     OPEN c_demand;
4581 	  END IF;
4582 	  IF (l_debug = 1) AND l_demand_cursor then
4583 	     debug_print('l_demand_cursor is true');
4584 	   ELSE
4585 	     debug_print('l_demand_cursor is false');
4586 	  END IF;
4587 	ELSE
4588           -- INVCONV - Incorporate secondary quantities
4589 	  OPEN l_cursor_ref FOR    'SELECT
4590 	    reservation_id
4591 	    , requirement_date
4592 	    , organization_id
4593 	    , inventory_item_id
4594 	    , demand_source_type_id
4595 	    , demand_source_name
4596 	    , demand_source_header_id
4597 	    , demand_source_line_id
4598 	    , demand_source_delivery
4599 	    , primary_uom_code
4600 	    , primary_uom_id
4601 	    , secondary_uom_code
4602 	    , secondary_uom_id
4603 	    , reservation_uom_code
4604 	    , reservation_uom_id
4605 	    , reservation_quantity
4606 	    , primary_reservation_quantity
4607 	    , secondary_reservation_quantity
4608 	    , detailed_quantity
4609 	    , secondary_detailed_quantity
4610 	    , autodetail_group_id
4611 	    , external_source_code
4612 	    , external_source_line_id
4613 	    , supply_source_type_id
4614 	    , supply_source_header_id
4615 	    , supply_source_line_id
4616 	    , supply_source_name
4617 	    , supply_source_line_detail
4618 	    , revision
4619 	    , subinventory_code
4620 	    , subinventory_id
4621 	    , locator_id
4622 	    , lot_number
4623 	    , lot_number_id
4624 	    , pick_slip_number
4625 	    , lpn_id
4626 	    , attribute_category
4627 	    , attribute1
4628 	    , attribute2
4629 	    , attribute3
4630 	    , attribute4
4631 	    , attribute5
4632 	    , attribute6
4633 	    , attribute7
4634 	    , attribute8
4635 	    , attribute9
4636 	    , attribute10
4637 	    , attribute11
4638 	    , attribute12
4639 	    , attribute13
4640 	    , attribute14
4641 	    , attribute15
4642 	    , ship_ready_flag
4643 	    , staged_flag
4644 	    /**** {{ R12 Enhanced reservations code changes. Adding new
4645 	    -- columns for query reservations }}****/
4646 	    , crossdock_flag
4647 	    , crossdock_criteria_id
4648 	    , demand_source_line_detail
4649 	    , serial_reservation_quantity
4650 	    , supply_receipt_date
4651 	    , demand_ship_date
4652 	    , project_id
4653 	    , task_id
4654 	    , orig_supply_source_type_id
4655 	    , orig_supply_source_header_id
4656 	    , orig_supply_source_line_id
4657 	    , orig_supply_source_line_detail
4658 	    , orig_demand_source_type_id
4659 	    , orig_demand_source_header_id
4660 	    , orig_demand_source_line_id
4661 	    , orig_demand_source_line_detail
4662             , serial_number
4663 	    /***** End R12 ***/
4664 	    FROM mtl_reservations
4665 	    WHERE
4666 	    demand_source_line_id = :demand_source_line_id
4667 	    AND
4668 	    (:requirement_date = :l_miss_date
4669 	     OR :requirement_date IS NULL
4670 	     AND requirement_date IS NULL
4671 	     OR :requirement_date
4672 	     = requirement_date
4673 	     )
4674 	       AND
4675         (:organization_id = :l_miss_num
4676          OR :organization_id IS NULL
4677          AND organization_id IS NULL
4678          OR :organization_id = organization_id
4679          )
4680         AND
4681         (:inventory_item_id = :l_miss_num
4682          OR :inventory_item_id IS NULL
4683          AND inventory_item_id IS NULL
4684          OR :inventory_item_id = inventory_item_id
4685          )
4686   AND
4687   (:demand_source_type_id = :l_miss_num
4688          OR :demand_source_type_id IS NULL
4689          AND demand_source_type_id IS NULL
4690          OR :demand_source_type_id
4691          = demand_source_type_id
4692          )
4693         AND
4694         (:demand_source_header_id = :l_miss_num
4695          OR :demand_source_header_id IS NULL
4696          AND demand_source_header_id IS NULL
4697          OR :demand_source_header_id
4698          = demand_source_header_id
4699          )
4700         AND
4701         (:demand_source_name = :l_miss_char
4702          OR :demand_source_name IS NULL
4703          AND demand_source_name IS NULL
4704          OR :demand_source_name = demand_source_name
4705          )
4706         AND
4707         (:demand_source_delivery = :l_miss_num
4708          OR :demand_source_delivery IS NULL
4709          AND demand_source_delivery IS NULL
4710          OR :demand_source_delivery = demand_source_delivery
4711          )
4712       AND
4713         (:primary_uom_code = :l_miss_char
4714          OR :primary_uom_code IS NULL
4715          AND primary_uom_code IS NULL
4716          OR :primary_uom_code = primary_uom_code
4717          )
4718         AND
4719         (:primary_uom_id = :l_miss_num
4720          OR :primary_uom_id IS NULL
4721          AND primary_uom_id IS NULL
4722          OR :primary_uom_id = primary_uom_id
4723          )
4724          -- INVCONV BEGIN
4725         AND
4726         (:secondary_uom_code = :l_miss_char
4727          OR :secondary_uom_code IS NULL
4728          AND secondary_uom_code IS NULL
4729          OR :secondary_uom_code = secondary_uom_code
4730          )
4731         AND
4732         (:secondary_uom_id = :l_miss_num
4733          OR :secondary_uom_id IS NULL
4734          AND secondary_uom_id IS NULL
4735          OR :secondary_uom_id = secondary_uom_id
4736          )
4737         -- INVCONV END
4738         AND
4739         (:reservation_uom_code = :l_miss_char
4740          OR :reservation_uom_code IS NULL
4741          AND reservation_uom_code IS NULL
4742          OR :reservation_uom_code = reservation_uom_code
4743          )
4744         AND
4745         (:reservation_uom_id = :l_miss_num
4746          OR :reservation_uom_id IS NULL
4747          AND reservation_uom_id IS NULL
4748          OR :reservation_uom_id = reservation_uom_id
4749          )
4750         AND
4751         (:autodetail_group_id = :l_miss_num
4752          OR :autodetail_group_id IS NULL
4753          AND autodetail_group_id IS NULL
4754          OR :autodetail_group_id = autodetail_group_id
4755          )
4756         AND
4757         (:external_source_code = :l_miss_char
4758          OR :external_source_code IS NULL
4759          AND external_source_code IS NULL
4760          OR :external_source_code = external_source_code
4761          )
4762         AND
4763         (:external_source_line_id = :l_miss_num
4764          OR :external_source_line_id IS NULL
4765          AND external_source_line_id IS NULL
4766          OR :external_source_line_id = external_source_line_id
4767          )
4768         AND
4769         (:supply_source_type_id = :l_miss_num
4770          OR :supply_source_type_id IS NULL
4771          AND supply_source_type_id IS NULL
4772          OR :supply_source_type_id = supply_source_type_id
4773          )
4774         AND
4775         (:supply_source_header_id = :l_miss_num
4776          OR :supply_source_header_id IS NULL
4777          AND supply_source_header_id IS NULL
4778          OR :supply_source_header_id
4779          = supply_source_header_id
4780          )
4781         AND
4782         (:supply_source_line_id = :l_miss_num
4783          OR :supply_source_line_id IS NULL
4784          AND supply_source_line_id IS NULL
4785          OR :supply_source_line_id = supply_source_line_id
4786          )
4787         AND
4788         (:supply_source_name = :l_miss_char
4789          OR :supply_source_name IS NULL
4790          AND supply_source_name IS NULL
4791          OR :supply_source_name = supply_source_name
4792          )
4793         AND
4794         (:supply_source_line_detail = :l_miss_num
4795          OR :supply_source_line_detail IS NULL
4796          AND supply_source_line_detail IS NULL
4797          OR :supply_source_line_detail
4798          = supply_source_line_detail
4799          )
4800         AND
4801         (:revision = :l_miss_char
4802          OR :revision IS NULL
4803          AND revision IS NULL
4804          OR :revision = revision
4805          )
4806         AND
4807         (:subinventory_code = :l_miss_char
4808          OR :subinventory_code IS NULL
4809          AND subinventory_code IS NULL
4810          OR :subinventory_code = subinventory_code
4811          )
4812        AND
4813         (:subinventory_id = :l_miss_num
4814          OR :subinventory_id IS NULL
4815          AND subinventory_id IS NULL
4816          OR :subinventory_id = subinventory_id
4817          )
4818         AND
4819         (:locator_id = :l_miss_num
4820          OR :locator_id IS NULL
4821          AND locator_id IS NULL
4822          OR :locator_id = locator_id
4823          )
4824         AND
4825         (:lot_number = :l_miss_char
4826          OR :lot_number IS NULL
4827          AND lot_number IS NULL
4828          OR :lot_number = lot_number
4829          )
4830         AND
4831         (:lot_number_id = :l_miss_num
4832          OR :lot_number_id IS NULL
4833          AND lot_number_id IS NULL
4834          OR :lot_number_id = lot_number_id
4835          )
4836         AND
4837         (:lpn_id = :l_miss_num
4838          OR :lpn_id IS NULL
4839          AND lpn_id IS NULL
4840          OR :lpn_id = lpn_id
4841          )
4842         AND
4843         (:ship_ready_flag = :l_miss_num
4844          OR (:ship_ready_flag IS NULL OR :ship_ready_flag = 2)
4845          AND (ship_ready_flag IS NULL OR ship_ready_flag = 2)
4846          OR :ship_ready_flag = ship_ready_flag
4847          )
4848         AND
4849         (:staged_flag = :l_miss_char
4850          OR (:staged_flag IS NULL OR :staged_flag = ''N'')
4851          AND (staged_flag IS NULL OR staged_flag = ''N'')
4852          OR :staged_flag = staged_flag
4853          )
4854         AND
4855         (:attribute_category = :l_miss_char
4856          OR :attribute_category IS NULL
4857          AND attribute_category IS NULL
4858          OR :attribute_category = attribute_category
4859          )
4860         AND
4861         (:attribute1 = :l_miss_char
4862          OR :attribute1 IS NULL
4863          AND attribute1 IS NULL
4864          OR :attribute1 = attribute1
4865          )
4866        AND
4867         (:attribute2 = :l_miss_char
4868          OR :attribute2 IS NULL
4869          AND attribute2 IS NULL
4870          OR :attribute2 = attribute2
4871          )
4872         AND
4873         (:attribute3 = :l_miss_char
4874          OR :attribute3 IS NULL
4875          AND attribute3 IS NULL
4876          OR :attribute3 = attribute3
4877          )
4878         AND
4879         (:attribute4 = :l_miss_char
4880          OR :attribute4 IS NULL
4881          AND attribute4 IS NULL
4882          OR :attribute4 = attribute4
4883          )
4884        AND
4885         (:attribute5 = :l_miss_char
4886          OR :attribute5 IS NULL
4887          AND attribute5 IS NULL
4888          OR :attribute5 = attribute5
4889          )
4890         AND
4891         (:attribute6 = :l_miss_char
4892          OR :attribute6 IS NULL
4893          AND attribute6 IS NULL
4894          OR :attribute6 = attribute6
4895          )
4896         AND
4897         (:attribute7 = :l_miss_char
4898          OR :attribute7 IS NULL
4899          AND attribute7 IS NULL
4900          OR :attribute7 = attribute7
4901          )
4902         AND
4903         (:attribute8 = :l_miss_char
4904          OR :attribute8 IS NULL
4905          AND attribute8 IS NULL
4906          OR :attribute8 = attribute8
4907          )
4908         AND
4909         (:attribute9 = :l_miss_char
4910          OR :attribute9 IS NULL
4911          AND attribute9 IS NULL
4912          OR :attribute9 = attribute9
4913          )
4914         AND
4915         (:attribute10 = :l_miss_char
4916          OR :attribute10 IS NULL
4917          AND attribute10 IS NULL
4918          OR :attribute10 = attribute10
4919          )
4920         AND
4921         (:attribute11 = :l_miss_char
4922          OR :attribute11 IS NULL
4923          AND attribute11 IS NULL
4924          OR :attribute11 = attribute11
4925          )
4926         AND
4927         (:attribute12 = :l_miss_char
4928          OR :attribute12 IS NULL
4929          AND attribute12 IS NULL
4930          OR :attribute12 = attribute12
4931          )
4932         AND
4933         (:attribute13 = :l_miss_char
4934          OR :attribute13 IS NULL
4935          AND attribute13 IS NULL
4936          OR :attribute13 = attribute13
4937          )
4938         AND
4939         (:attribute14 = :l_miss_char
4940          OR :attribute14 IS NULL
4941          AND attribute14 IS NULL
4942          OR :attribute14 = attribute14
4943          )
4944         AND
4945         (:attribute15 = :l_miss_char
4946          OR :attribute15 IS NULL
4947          AND attribute15 IS NULL
4948          OR :attribute15 = attribute15
4949          )
4950 /**** {{ R12 Enhanced reservations code changes }}****/
4951 	AND
4952 	   (:crossdock_flag = :l_miss_char
4953          OR :crossdock_flag IS NULL
4954          AND crossdock_flag IS NULL
4955          OR :crossdock_flag = crossdock_flag
4956          )
4957 	AND
4958 	   (:crossdock_criteria_id = :l_miss_num
4959          OR :crossdock_criteria_id IS NULL
4960          AND crossdock_criteria_id IS NULL
4961          OR :crossdock_criteria_id = crossdock_criteria_id
4962 	    )
4963         AND
4964 	   (:demand_source_line_detail = :l_miss_num
4965          OR :demand_source_line_detail IS NULL
4966          AND demand_source_line_detail IS NULL
4967          OR :demand_source_line_detail = demand_source_line_detail
4968 	    )
4969 	AND
4970 	   (:supply_receipt_date = :l_miss_date
4971          OR :supply_receipt_date IS NULL
4972          AND supply_receipt_date IS NULL
4973          OR :supply_receipt_date = supply_receipt_date
4974 	    )
4975 	AND
4976 	   (:demand_ship_date = :l_miss_date
4977          OR :demand_ship_date IS NULL
4978          AND demand_ship_date IS NULL
4979          OR :demand_ship_date = demand_ship_date
4980 	    )
4981 	AND
4982 	   (:project_id = :l_miss_num
4983          OR :project_id IS NULL
4984          AND project_id IS NULL
4985          OR :project_id = project_id
4986 	    )
4987 	 AND
4988 	   (:task_id = :l_miss_num
4989          OR :task_id IS NULL
4990          AND task_id IS NULL
4991          OR :task_id = task_id
4992 	    )
4993 /***** End R12  ***/
4994 	   '
4995                               || l_lock_stmt
4996                               || l_sort_stmt
4997           USING  p_query_input.demand_source_line_id
4998                , p_query_input.requirement_date
4999                , l_miss_date
5000                , p_query_input.requirement_date
5001                , p_query_input.requirement_date
5002                , p_query_input.organization_id
5003                , l_miss_num
5004                , p_query_input.organization_id
5005                , p_query_input.organization_id
5006                , p_query_input.inventory_item_id
5007                , l_miss_num
5008                , p_query_input.inventory_item_id
5009                , p_query_input.inventory_item_id
5010                , p_query_input.demand_source_type_id
5011                , l_miss_num
5012                , p_query_input.demand_source_type_id
5013                , p_query_input.demand_source_type_id
5014                , p_query_input.demand_source_header_id
5015                , l_miss_num
5016                , p_query_input.demand_source_header_id
5017                , p_query_input.demand_source_header_id
5018                , p_query_input.demand_source_name
5019                , l_miss_char
5020                , p_query_input.demand_source_name
5021                , p_query_input.demand_source_name
5022                , p_query_input.demand_source_delivery
5023                , l_miss_num
5024                , p_query_input.demand_source_delivery
5025                , p_query_input.demand_source_delivery
5026                , p_query_input.primary_uom_code
5027                , l_miss_char
5028                , p_query_input.primary_uom_code
5029                , p_query_input.primary_uom_code
5030                , p_query_input.primary_uom_id
5031                , l_miss_num
5032                , p_query_input.primary_uom_id
5033                , p_query_input.primary_uom_id
5034                 -- INVCONV BEGIN
5035                , p_query_input.secondary_uom_code
5036                , l_miss_char
5037                , p_query_input.secondary_uom_code
5038                , p_query_input.secondary_uom_code
5039                , p_query_input.secondary_uom_id
5040                , l_miss_num
5041                , p_query_input.secondary_uom_id
5042                , p_query_input.secondary_uom_id
5043                -- INVCONV END
5044                , p_query_input.reservation_uom_code
5045                , l_miss_char
5046                , p_query_input.reservation_uom_code
5047                , p_query_input.reservation_uom_code
5048                , p_query_input.reservation_uom_id
5049                , l_miss_num
5050                , p_query_input.reservation_uom_id
5051                , p_query_input.reservation_uom_id
5052                , p_query_input.autodetail_group_id
5053                , l_miss_num
5054                , p_query_input.autodetail_group_id
5055                , p_query_input.autodetail_group_id
5056                , p_query_input.external_source_code
5057                , l_miss_char
5058                , p_query_input.external_source_code
5059                , p_query_input.external_source_code
5060                , p_query_input.external_source_line_id
5061                , l_miss_num
5062                , p_query_input.external_source_line_id
5063                , p_query_input.external_source_line_id
5064                , p_query_input.supply_source_type_id
5065                , l_miss_num
5066                , p_query_input.supply_source_type_id
5067                , p_query_input.supply_source_type_id
5068                , p_query_input.supply_source_header_id
5069                , l_miss_num
5070                , p_query_input.supply_source_header_id
5071                , p_query_input.supply_source_header_id
5072                , p_query_input.supply_source_line_id
5073                , l_miss_num
5074                , p_query_input.supply_source_line_id
5075                , p_query_input.supply_source_line_id
5076                , p_query_input.supply_source_name
5077                , l_miss_char
5078                , p_query_input.supply_source_name
5079                , p_query_input.supply_source_name
5080                , p_query_input.supply_source_line_detail
5081                , l_miss_num
5082                , p_query_input.supply_source_line_detail
5083                , p_query_input.supply_source_line_detail
5084                , p_query_input.revision
5085                , l_miss_char
5086                , p_query_input.revision
5087                , p_query_input.revision
5088                , p_query_input.subinventory_code
5089                , l_miss_char
5090                , p_query_input.subinventory_code
5091                , p_query_input.subinventory_code
5092                , p_query_input.subinventory_id
5093                , l_miss_num
5094                , p_query_input.subinventory_id
5095                , p_query_input.subinventory_id
5096                , p_query_input.locator_id
5097                , l_miss_num
5098                , p_query_input.locator_id
5099                , p_query_input.locator_id
5100                , p_query_input.lot_number
5101                , l_miss_char
5102                , p_query_input.lot_number
5103                , p_query_input.lot_number
5104                , p_query_input.lot_number_id
5105                , l_miss_num
5106                , p_query_input.lot_number_id
5107                , p_query_input.lot_number_id
5108                , p_query_input.lpn_id
5109                , l_miss_num
5110                , p_query_input.lpn_id
5111                , p_query_input.lpn_id
5112                , p_query_input.ship_ready_flag
5113                , l_miss_num
5114                , p_query_input.ship_ready_flag
5115                , p_query_input.ship_ready_flag
5116                , p_query_input.ship_ready_flag
5117                , p_query_input.staged_flag
5118                , l_miss_char
5119                , p_query_input.staged_flag
5120                , p_query_input.staged_flag
5121                , p_query_input.staged_flag
5122                , p_query_input.attribute_category
5123                , l_miss_char
5124                , p_query_input.attribute_category
5125                , p_query_input.attribute_category
5126                , p_query_input.attribute1
5127                , l_miss_char
5128                , p_query_input.attribute1
5129                , p_query_input.attribute1
5130                , p_query_input.attribute2
5131                , l_miss_char
5132                , p_query_input.attribute2
5133                , p_query_input.attribute2
5134                , p_query_input.attribute3
5135                , l_miss_char
5136                , p_query_input.attribute3
5137                , p_query_input.attribute3
5138                , p_query_input.attribute4
5139                , l_miss_char
5140                , p_query_input.attribute4
5141                , p_query_input.attribute4
5142                , p_query_input.attribute5
5143                , l_miss_char
5144                , p_query_input.attribute5
5145                , p_query_input.attribute5
5146                , p_query_input.attribute6
5147                , l_miss_char
5148                , p_query_input.attribute6
5149                , p_query_input.attribute6
5150                , p_query_input.attribute7
5151                , l_miss_char
5152                , p_query_input.attribute7
5153                , p_query_input.attribute7
5154                , p_query_input.attribute8
5155                , l_miss_char
5156                , p_query_input.attribute8
5157                , p_query_input.attribute8
5158                , p_query_input.attribute9
5159                , l_miss_char
5160                , p_query_input.attribute9
5161                , p_query_input.attribute9
5162                , p_query_input.attribute10
5163                , l_miss_char
5164                , p_query_input.attribute10
5165                , p_query_input.attribute10
5166                , p_query_input.attribute11
5167                , l_miss_char
5168                , p_query_input.attribute11
5169                , p_query_input.attribute11
5170                , p_query_input.attribute12
5171                , l_miss_char
5172                , p_query_input.attribute12
5173                , p_query_input.attribute12
5174                , p_query_input.attribute13
5175                , l_miss_char
5176                , p_query_input.attribute13
5177                , p_query_input.attribute13
5178                , p_query_input.attribute14
5179                , l_miss_char
5180                , p_query_input.attribute14
5181                , p_query_input.attribute14
5182                , p_query_input.attribute15
5183                , l_miss_char
5184                , p_query_input.attribute15
5185 	   , p_query_input.attribute15
5186            /**** {{ R12 Enhanced reservations code changes }}****/
5187 	   , p_query_input.crossdock_flag
5188 	   , l_miss_char
5189 	   , p_query_input.crossdock_flag
5190 	   , p_query_input.crossdock_flag
5191 	   , p_query_input.crossdock_criteria_id
5192 	   , l_miss_num
5193 	   , p_query_input.crossdock_criteria_id
5194 	   , p_query_input.crossdock_criteria_id
5195 	   , p_query_input.demand_source_line_detail
5196 	   , l_miss_num
5197 	   , p_query_input.demand_source_line_detail
5198 	   , p_query_input.demand_source_line_detail
5199 	   , p_query_input.supply_receipt_date
5200 	   , l_miss_date
5201 	   , p_query_input.supply_receipt_date
5202 	   , p_query_input.supply_receipt_date
5203 	   , p_query_input.demand_ship_date
5204 	   , l_miss_date
5205 	   , p_query_input.demand_ship_date
5206 	   , p_query_input.demand_ship_date
5207 	   , p_query_input.project_id
5208 	   , l_miss_num
5209 	   , p_query_input.project_id
5210 	   , p_query_input.project_id
5211 	   , p_query_input.task_id
5212 	   , l_miss_num
5213 	   , p_query_input.task_id
5214 	   , p_query_input.task_id
5215 
5216            /***** End R12 ***/
5217 	   ;
5218       END IF;
5219     ELSIF  p_query_input.inventory_item_id <> fnd_api.g_miss_num
5220            AND p_query_input.inventory_item_id IS NOT NULL
5221            AND p_query_input.organization_id <> fnd_api.g_miss_num
5222 	     AND p_query_input.organization_id IS NOT NULL THEN
5223        IF (l_debug = 1) then
5224 	  debug_print (' Inside ref cursor for item/org');
5225        END IF;
5226       -- INVCONV - Incorporate secondaries
5227       OPEN l_cursor_ref FOR    'SELECT
5228           reservation_id
5229         , requirement_date
5230         , organization_id
5231         , inventory_item_id
5232         , demand_source_type_id
5233         , demand_source_name
5234         , demand_source_header_id
5235         , demand_source_line_id
5236         , demand_source_delivery
5237         , primary_uom_code
5238         , primary_uom_id
5239         , secondary_uom_code
5240         , secondary_uom_id
5241         , reservation_uom_code
5242         , reservation_uom_id
5243         , reservation_quantity
5244         , primary_reservation_quantity
5245         , secondary_reservation_quantity
5246         , detailed_quantity
5247         , secondary_detailed_quantity
5248         , autodetail_group_id
5249         , external_source_code
5250         , external_source_line_id
5251         , supply_source_type_id
5252         , supply_source_header_id
5253         , supply_source_line_id
5254         , supply_source_name
5255         , supply_source_line_detail
5256         , revision
5257         , subinventory_code
5258         , subinventory_id
5259         , locator_id
5260         , lot_number
5261         , lot_number_id
5262         , pick_slip_number
5263         , lpn_id
5264         , attribute_category
5265         , attribute1
5266         , attribute2
5267         , attribute3
5268         , attribute4
5269         , attribute5
5270         , attribute6
5271         , attribute7
5272         , attribute8
5273         , attribute9
5274         , attribute10
5275         , attribute11
5276         , attribute12
5277         , attribute13
5278         , attribute14
5279         , attribute15
5280         , ship_ready_flag
5281         , staged_flag
5282          /**** {{ R12 Enhanced reservations code changes. Adding new
5283             -- columns for query reservations }}****/
5284 	 , crossdock_flag
5285 	 , crossdock_criteria_id
5286 	 , demand_source_line_detail
5287 	 , serial_reservation_quantity
5288 	 , supply_receipt_date
5289 	 , demand_ship_date
5290 	 , project_id
5291 	 , task_id
5292 	 , orig_supply_source_type_id
5293 	 , orig_supply_source_header_id
5294 	 , orig_supply_source_line_id
5295 	 , orig_supply_source_line_detail
5296 	 , orig_demand_source_type_id
5297 	 , orig_demand_source_header_id
5298 	 , orig_demand_source_line_id
5299 	 , orig_demand_source_line_detail
5300          , serial_number
5301          /***** End R12 ***/
5302         FROM mtl_reservations
5303         WHERE
5304         inventory_item_id = :inventory_item_id
5305         and organization_id = :organization_id
5306         AND
5307    (:requirement_date = :l_miss_date
5308          OR :requirement_date IS NULL
5309          AND requirement_date IS NULL
5310          OR :requirement_date
5311          = requirement_date
5312          )
5313   AND
5314   (:demand_source_type_id = :l_miss_num
5315          OR :demand_source_type_id IS NULL
5316          AND demand_source_type_id IS NULL
5317          OR :demand_source_type_id
5318          = demand_source_type_id
5319          )
5320         AND
5321         (:demand_source_header_id = :l_miss_num
5322          OR :demand_source_header_id IS NULL
5323          AND demand_source_header_id IS NULL
5324          OR :demand_source_header_id
5325          = demand_source_header_id
5326          )
5327         AND
5328         (:demand_source_line_id = :l_miss_num
5329          OR :demand_source_line_id IS NULL
5330          AND demand_source_line_id IS NULL
5331          OR :demand_source_line_id = demand_source_line_id
5332          )
5333         AND
5334         (:demand_source_name = :l_miss_char
5335          OR :demand_source_name IS NULL
5336          AND demand_source_name IS NULL
5337          OR :demand_source_name = demand_source_name
5338          )
5339         AND
5340         (:demand_source_delivery = :l_miss_num
5341          OR :demand_source_delivery IS NULL
5342          AND demand_source_delivery IS NULL
5343          OR :demand_source_delivery = demand_source_delivery
5344          )
5345       AND
5346         (:primary_uom_code = :l_miss_char
5347          OR :primary_uom_code IS NULL
5348          AND primary_uom_code IS NULL
5349          OR :primary_uom_code = primary_uom_code
5350          )
5351         AND
5352         (:primary_uom_id = :l_miss_num
5353          OR :primary_uom_id IS NULL
5354          AND primary_uom_id IS NULL
5355          OR :primary_uom_id = primary_uom_id
5356          )
5357          -- INVCONV BEGIN
5358         AND
5359         (:secondary_uom_code = :l_miss_char
5360          OR :secondary_uom_code IS NULL
5361          AND secondary_uom_code IS NULL
5362          OR :secondary_uom_code = secondary_uom_code
5363          )
5364         AND
5365         (:secondary_uom_id = :l_miss_num
5366          OR :secondary_uom_id IS NULL
5367          AND secondary_uom_id IS NULL
5368          OR :secondary_uom_id = secondary_uom_id
5369          )
5370         -- INVCONV END
5371         AND
5372         (:reservation_uom_code = :l_miss_char
5373          OR :reservation_uom_code IS NULL
5374          AND reservation_uom_code IS NULL
5375          OR :reservation_uom_code = reservation_uom_code
5376          )
5377         AND
5378         (:reservation_uom_id = :l_miss_num
5379          OR :reservation_uom_id IS NULL
5380          AND reservation_uom_id IS NULL
5381          OR :reservation_uom_id = reservation_uom_id
5382          )
5383         AND
5384         (:autodetail_group_id = :l_miss_num
5385          OR :autodetail_group_id IS NULL
5386          AND autodetail_group_id IS NULL
5387          OR :autodetail_group_id = autodetail_group_id
5388          )
5389         AND
5390         (:external_source_code = :l_miss_char
5391          OR :external_source_code IS NULL
5392          AND external_source_code IS NULL
5393          OR :external_source_code = external_source_code
5394          )
5395         AND
5396         (:external_source_line_id = :l_miss_num
5397          OR :external_source_line_id IS NULL
5398          AND external_source_line_id IS NULL
5399          OR :external_source_line_id = external_source_line_id
5400          )
5401         AND
5402         (:supply_source_type_id = :l_miss_num
5403          OR :supply_source_type_id IS NULL
5404          AND supply_source_type_id IS NULL
5405          OR :supply_source_type_id = supply_source_type_id
5406          )
5407         AND
5408         (:supply_source_header_id = :l_miss_num
5409          OR :supply_source_header_id IS NULL
5410          AND supply_source_header_id IS NULL
5411          OR :supply_source_header_id
5412          = supply_source_header_id
5413          )
5414         AND
5415         (:supply_source_line_id = :l_miss_num
5416          OR :supply_source_line_id IS NULL
5417          AND supply_source_line_id IS NULL
5418          OR :supply_source_line_id = supply_source_line_id
5419          )
5420         AND
5421         (:supply_source_name = :l_miss_char
5422          OR :supply_source_name IS NULL
5423          AND supply_source_name IS NULL
5424          OR :supply_source_name = supply_source_name
5425          )
5426         AND
5427         (:supply_source_line_detail = :l_miss_num
5428          OR :supply_source_line_detail IS NULL
5429          AND supply_source_line_detail IS NULL
5430          OR :supply_source_line_detail
5431          = supply_source_line_detail
5432          )
5433         AND
5434         (:revision = :l_miss_char
5435          OR :revision IS NULL
5436          AND revision IS NULL
5437          OR :revision = revision
5438          )
5439         AND
5440         (:subinventory_code = :l_miss_char
5441          OR :subinventory_code IS NULL
5442          AND subinventory_code IS NULL
5443          OR :subinventory_code = subinventory_code
5444          )
5445        AND
5446         (:subinventory_id = :l_miss_num
5447          OR :subinventory_id IS NULL
5448          AND subinventory_id IS NULL
5449          OR :subinventory_id = subinventory_id
5450          )
5451         AND
5452         (:locator_id = :l_miss_num
5453          OR :locator_id IS NULL
5454          AND locator_id IS NULL
5455          OR :locator_id = locator_id
5456          )
5457         AND
5458         (:lot_number = :l_miss_char
5459          OR :lot_number IS NULL
5460          AND lot_number IS NULL
5461          OR :lot_number = lot_number
5462          )
5463         AND
5464         (:lot_number_id = :l_miss_num
5465          OR :lot_number_id IS NULL
5466          AND lot_number_id IS NULL
5467          OR :lot_number_id = lot_number_id
5468          )
5469         AND
5470         (:lpn_id = :l_miss_num
5471          OR :lpn_id IS NULL
5472          AND lpn_id IS NULL
5473          OR :lpn_id = lpn_id
5474          )
5475         AND
5476         (:ship_ready_flag = :l_miss_num
5477          OR (:ship_ready_flag IS NULL OR :ship_ready_flag = 2)
5478          AND (ship_ready_flag IS NULL OR ship_ready_flag = 2)
5479          OR :ship_ready_flag = ship_ready_flag
5480          )
5481         AND
5482         (:staged_flag = :l_miss_char
5483          OR (:staged_flag IS NULL OR :staged_flag = ''N'')
5484          AND (staged_flag IS NULL OR staged_flag = ''N'')
5485          OR :staged_flag = staged_flag
5486          )
5487         AND
5488         (:attribute_category = :l_miss_char
5489          OR :attribute_category IS NULL
5490          AND attribute_category IS NULL
5491          OR :attribute_category = attribute_category
5492          )
5493         AND
5494         (:attribute1 = :l_miss_char
5495          OR :attribute1 IS NULL
5496          AND attribute1 IS NULL
5497          OR :attribute1 = attribute1
5498          )
5499        AND
5500         (:attribute2 = :l_miss_char
5501          OR :attribute2 IS NULL
5502          AND attribute2 IS NULL
5503          OR :attribute2 = attribute2
5504          )
5505         AND
5506         (:attribute3 = :l_miss_char
5507          OR :attribute3 IS NULL
5508          AND attribute3 IS NULL
5509          OR :attribute3 = attribute3
5510          )
5511         AND
5512         (:attribute4 = :l_miss_char
5513          OR :attribute4 IS NULL
5514          AND attribute4 IS NULL
5515          OR :attribute4 = attribute4
5516          )
5517        AND
5518         (:attribute5 = :l_miss_char
5519          OR :attribute5 IS NULL
5520          AND attribute5 IS NULL
5521          OR :attribute5 = attribute5
5522          )
5523         AND
5524         (:attribute6 = :l_miss_char
5525          OR :attribute6 IS NULL
5526          AND attribute6 IS NULL
5527          OR :attribute6 = attribute6
5528          )
5529         AND
5530         (:attribute7 = :l_miss_char
5531          OR :attribute7 IS NULL
5532          AND attribute7 IS NULL
5533          OR :attribute7 = attribute7
5534          )
5535         AND
5536         (:attribute8 = :l_miss_char
5537          OR :attribute8 IS NULL
5538          AND attribute8 IS NULL
5539          OR :attribute8 = attribute8
5540          )
5541         AND
5542         (:attribute9 = :l_miss_char
5543          OR :attribute9 IS NULL
5544          AND attribute9 IS NULL
5545          OR :attribute9 = attribute9
5546          )
5547         AND
5548         (:attribute10 = :l_miss_char
5549          OR :attribute10 IS NULL
5550          AND attribute10 IS NULL
5551          OR :attribute10 = attribute10
5552          )
5553         AND
5554         (:attribute11 = :l_miss_char
5555          OR :attribute11 IS NULL
5556          AND attribute11 IS NULL
5557          OR :attribute11 = attribute11
5558          )
5559         AND
5560         (:attribute12 = :l_miss_char
5561          OR :attribute12 IS NULL
5562          AND attribute12 IS NULL
5563          OR :attribute12 = attribute12
5564          )
5565         AND
5566         (:attribute13 = :l_miss_char
5567          OR :attribute13 IS NULL
5568          AND attribute13 IS NULL
5569          OR :attribute13 = attribute13
5570          )
5571         AND
5572         (:attribute14 = :l_miss_char
5573          OR :attribute14 IS NULL
5574          AND attribute14 IS NULL
5575          OR :attribute14 = attribute14
5576          )
5577         AND
5578         (:attribute15 = :l_miss_char
5579          OR :attribute15 IS NULL
5580          AND attribute15 IS NULL
5581          OR :attribute15 = attribute15
5582          )
5583 
5584 /**** {{ R12 Enhanced reservations code changes }}****/
5585 	AND
5586 	   (:crossdock_flag = :l_miss_char
5587          OR :crossdock_flag IS NULL
5588          AND crossdock_flag IS NULL
5589          OR :crossdock_flag = crossdock_flag
5590          )
5591 	AND
5592 	   (:crossdock_criteria_id = :l_miss_num
5593          OR :crossdock_criteria_id IS NULL
5594          AND crossdock_criteria_id IS NULL
5595          OR :crossdock_criteria_id = crossdock_criteria_id
5596 	    )
5597         AND
5598 	   (:demand_source_line_detail = :l_miss_num
5599          OR :demand_source_line_detail IS NULL
5600          AND demand_source_line_detail IS NULL
5601          OR :demand_source_line_detail = demand_source_line_detail
5602 	    )
5603 	AND
5604 	   (:supply_receipt_date = :l_miss_date
5605          OR :supply_receipt_date IS NULL
5606          AND supply_receipt_date IS NULL
5607          OR :supply_receipt_date = supply_receipt_date
5608 	    )
5609 	AND
5610 	   (:demand_ship_date = :l_miss_date
5611          OR :demand_ship_date IS NULL
5612          AND demand_ship_date IS NULL
5613          OR :demand_ship_date = demand_ship_date
5614 	    )
5615 	AND
5616 	   (:project_id = :l_miss_num
5617          OR :project_id IS NULL
5618          AND project_id IS NULL
5619          OR :project_id = project_id
5620 	    )
5621 	 AND
5622 	   (:task_id = :l_miss_num
5623          OR :task_id IS NULL
5624          AND task_id IS NULL
5625          OR :task_id = task_id
5626 	    )
5627 /***** End R12 ***/
5628 
5629   '
5630                             || l_lock_stmt
5631                             || l_sort_stmt
5632         USING  p_query_input.inventory_item_id
5633              , p_query_input.organization_id
5634              , p_query_input.requirement_date
5635              , l_miss_date
5636              , p_query_input.requirement_date
5637              , p_query_input.requirement_date
5638              , p_query_input.demand_source_type_id
5639              , l_miss_num
5640              , p_query_input.demand_source_type_id
5641              , p_query_input.demand_source_type_id
5642              , p_query_input.demand_source_header_id
5643              , l_miss_num
5644              , p_query_input.demand_source_header_id
5645              , p_query_input.demand_source_header_id
5646              , p_query_input.demand_source_line_id
5647              , l_miss_num
5648              , p_query_input.demand_source_line_id
5649              , p_query_input.demand_source_line_id
5650              , p_query_input.demand_source_name
5651              , l_miss_char
5652              , p_query_input.demand_source_name
5653              , p_query_input.demand_source_name
5654              , p_query_input.demand_source_delivery
5655              , l_miss_num
5656              , p_query_input.demand_source_delivery
5657              , p_query_input.demand_source_delivery
5658              , p_query_input.primary_uom_code
5659              , l_miss_char
5660              , p_query_input.primary_uom_code
5661              , p_query_input.primary_uom_code
5662              , p_query_input.primary_uom_id
5663              , l_miss_num
5664              , p_query_input.primary_uom_id
5665              , p_query_input.primary_uom_id
5666               -- INVCONV BEGIN
5667              , p_query_input.secondary_uom_code
5668              , l_miss_char
5669              , p_query_input.secondary_uom_code
5670              , p_query_input.secondary_uom_code
5671              , p_query_input.secondary_uom_id
5672              , l_miss_num
5673              , p_query_input.secondary_uom_id
5674              , p_query_input.secondary_uom_id
5675              -- INVCONV END
5676              , p_query_input.reservation_uom_code
5677              , l_miss_char
5678              , p_query_input.reservation_uom_code
5679              , p_query_input.reservation_uom_code
5680              , p_query_input.reservation_uom_id
5681              , l_miss_num
5682              , p_query_input.reservation_uom_id
5683              , p_query_input.reservation_uom_id
5684              , p_query_input.autodetail_group_id
5685              , l_miss_num
5686              , p_query_input.autodetail_group_id
5687              , p_query_input.autodetail_group_id
5688              , p_query_input.external_source_code
5689              , l_miss_char
5690              , p_query_input.external_source_code
5691              , p_query_input.external_source_code
5692              , p_query_input.external_source_line_id
5693              , l_miss_num
5694              , p_query_input.external_source_line_id
5695              , p_query_input.external_source_line_id
5696              , p_query_input.supply_source_type_id
5697              , l_miss_num
5698              , p_query_input.supply_source_type_id
5699              , p_query_input.supply_source_type_id
5700              , p_query_input.supply_source_header_id
5701              , l_miss_num
5702              , p_query_input.supply_source_header_id
5703              , p_query_input.supply_source_header_id
5704              , p_query_input.supply_source_line_id
5705              , l_miss_num
5706              , p_query_input.supply_source_line_id
5707              , p_query_input.supply_source_line_id
5708              , p_query_input.supply_source_name
5709              , l_miss_char
5710              , p_query_input.supply_source_name
5711              , p_query_input.supply_source_name
5712              , p_query_input.supply_source_line_detail
5713              , l_miss_num
5714              , p_query_input.supply_source_line_detail
5715              , p_query_input.supply_source_line_detail
5716              , p_query_input.revision
5717              , l_miss_char
5718              , p_query_input.revision
5719              , p_query_input.revision
5720              , p_query_input.subinventory_code
5721              , l_miss_char
5722              , p_query_input.subinventory_code
5723              , p_query_input.subinventory_code
5724              , p_query_input.subinventory_id
5725              , l_miss_num
5726              , p_query_input.subinventory_id
5727              , p_query_input.subinventory_id
5728              , p_query_input.locator_id
5729              , l_miss_num
5730              , p_query_input.locator_id
5731              , p_query_input.locator_id
5732              , p_query_input.lot_number
5733              , l_miss_char
5734              , p_query_input.lot_number
5735              , p_query_input.lot_number
5736              , p_query_input.lot_number_id
5737              , l_miss_num
5738              , p_query_input.lot_number_id
5739              , p_query_input.lot_number_id
5740              , p_query_input.lpn_id
5741              , l_miss_num
5742              , p_query_input.lpn_id
5743              , p_query_input.lpn_id
5744              , p_query_input.ship_ready_flag
5745              , l_miss_num
5746              , p_query_input.ship_ready_flag
5747              , p_query_input.ship_ready_flag
5748              , p_query_input.ship_ready_flag
5749              , p_query_input.staged_flag
5750              , l_miss_char
5751              , p_query_input.staged_flag
5752              , p_query_input.staged_flag
5753              , p_query_input.staged_flag
5754              , p_query_input.attribute_category
5755              , l_miss_char
5756              , p_query_input.attribute_category
5757              , p_query_input.attribute_category
5758              , p_query_input.attribute1
5759              , l_miss_char
5760              , p_query_input.attribute1
5761              , p_query_input.attribute1
5762              , p_query_input.attribute2
5763              , l_miss_char
5764              , p_query_input.attribute2
5765              , p_query_input.attribute2
5766              , p_query_input.attribute3
5767              , l_miss_char
5768              , p_query_input.attribute3
5769              , p_query_input.attribute3
5770              , p_query_input.attribute4
5771              , l_miss_char
5772              , p_query_input.attribute4
5773              , p_query_input.attribute4
5774              , p_query_input.attribute5
5775              , l_miss_char
5776              , p_query_input.attribute5
5777              , p_query_input.attribute5
5778              , p_query_input.attribute6
5779              , l_miss_char
5780              , p_query_input.attribute6
5781              , p_query_input.attribute6
5782              , p_query_input.attribute7
5783              , l_miss_char
5784              , p_query_input.attribute7
5785              , p_query_input.attribute7
5786              , p_query_input.attribute8
5787              , l_miss_char
5788              , p_query_input.attribute8
5789              , p_query_input.attribute8
5790              , p_query_input.attribute9
5791              , l_miss_char
5792              , p_query_input.attribute9
5793              , p_query_input.attribute9
5794              , p_query_input.attribute10
5795              , l_miss_char
5796              , p_query_input.attribute10
5797              , p_query_input.attribute10
5798              , p_query_input.attribute11
5799              , l_miss_char
5800              , p_query_input.attribute11
5801              , p_query_input.attribute11
5802              , p_query_input.attribute12
5803              , l_miss_char
5804              , p_query_input.attribute12
5805              , p_query_input.attribute12
5806              , p_query_input.attribute13
5807              , l_miss_char
5808              , p_query_input.attribute13
5809              , p_query_input.attribute13
5810              , p_query_input.attribute14
5811              , l_miss_char
5812              , p_query_input.attribute14
5813              , p_query_input.attribute14
5814              , p_query_input.attribute15
5815              , l_miss_char
5816              , p_query_input.attribute15
5817 	   , p_query_input.attribute15
5818  /**** {{ R12 Enhanced reservations code changes }}****/
5819 	   , p_query_input.crossdock_flag
5820 	   , l_miss_char
5821 	   , p_query_input.crossdock_flag
5822 	   , p_query_input.crossdock_flag
5823 	   , p_query_input.crossdock_criteria_id
5824 	   , l_miss_num
5825 	   , p_query_input.crossdock_criteria_id
5826 	   , p_query_input.crossdock_criteria_id
5827 	   , p_query_input.demand_source_line_detail
5828 	   , l_miss_num
5829 	   , p_query_input.demand_source_line_detail
5830 	   , p_query_input.demand_source_line_detail
5831 	   , p_query_input.supply_receipt_date
5832 	   , l_miss_date
5833 	   , p_query_input.supply_receipt_date
5834 	   , p_query_input.supply_receipt_date
5835 	   , p_query_input.demand_ship_date
5836 	   , l_miss_date
5837 	   , p_query_input.demand_ship_date
5838 	   , p_query_input.demand_ship_date
5839 	   , p_query_input.project_id
5840 	   , l_miss_num
5841 	   , p_query_input.project_id
5842 	   , p_query_input.project_id
5843 	   , p_query_input.task_id
5844 	   , l_miss_num
5845 	   , p_query_input.task_id
5846 	   , p_query_input.task_id
5847            /***** End R12 ***/
5848 	   ;
5849 
5850       -- kkoothan Added the following  ELSE IF section as part of Bug Fix:2783806
5851       ELSIF p_query_input.supply_source_header_id <  fnd_api.g_miss_num
5852        AND p_query_input.supply_source_header_id IS NOT NULL
5853 --      Bug 4881317 checking these conditions later
5854 --      AND p_query_input.supply_source_line_id <  fnd_api.g_miss_num
5855 --      AND p_query_input.supply_source_line_id IS NOT NULL
5856        AND p_query_input.supply_source_type_id <  fnd_api.g_miss_num
5857 	 AND p_query_input.supply_source_type_id IS NOT NULL  THEN
5858        IF (l_debug = 1) then
5859 	  debug_print(' Inside ref cursor for supply');
5860        END IF;
5861         -- INVCONV - Incorporate secondaries
5862       l_qry_stmt:= 'SELECT
5863             reservation_id
5864           , requirement_date
5865           , organization_id
5866           , inventory_item_id
5867           , demand_source_type_id
5868           , demand_source_name
5869           , demand_source_header_id
5870           , demand_source_line_id
5871           , demand_source_delivery
5872           , primary_uom_code
5873           , primary_uom_id
5874           , secondary_uom_code
5875           , secondary_uom_id
5876           , reservation_uom_code
5877           , reservation_uom_id
5878           , reservation_quantity
5879           , primary_reservation_quantity
5880           , secondary_reservation_quantity
5881           , detailed_quantity
5882           , secondary_detailed_quantity
5883           , autodetail_group_id
5884           , external_source_code
5885           , external_source_line_id
5886           , supply_source_type_id
5887           , supply_source_header_id
5888           , supply_source_line_id
5889           , supply_source_name
5890           , supply_source_line_detail
5891           , revision
5892           , subinventory_code
5893           , subinventory_id
5894           , locator_id
5895           , lot_number
5896           , lot_number_id
5897           , pick_slip_number
5898           , lpn_id
5899           , attribute_category
5900           , attribute1
5901           , attribute2
5902           , attribute3
5903           , attribute4
5904           , attribute5
5905           , attribute6
5906           , attribute7
5907           , attribute8
5908           , attribute9
5909           , attribute10
5910           , attribute11
5911           , attribute12
5912           , attribute13
5913           , attribute14
5914           , attribute15
5915           , ship_ready_flag
5916           , staged_flag
5917 	  /**** {{ R12 Enhanced reservations code changes }}****/
5918 	 , crossdock_flag
5919 	 , crossdock_criteria_id
5920 	 , demand_source_line_detail
5921 	 , serial_reservation_quantity
5922 	 , supply_receipt_date
5923 	 , demand_ship_date
5924 	 , project_id
5925 	 , task_id
5926 	 , orig_supply_source_type_id
5927 	 , orig_supply_source_header_id
5928 	 , orig_supply_source_line_id
5929 	 , orig_supply_source_line_detail
5930 	 , orig_demand_source_type_id
5931 	 , orig_demand_source_header_id
5932 	 , orig_demand_source_line_id
5933 	 , orig_demand_source_line_detail
5934          , serial_number
5935 	 /***** End R12 ***/
5936           FROM mtl_reservations
5937           WHERE
5938           supply_source_header_id = :supply_source_header_id
5939           and supply_source_type_id = :supply_source_type_id
5940           AND
5941            (:requirement_date = :l_miss_date
5942            OR :requirement_date IS NULL
5943            AND requirement_date IS NULL
5944            OR :requirement_date
5945            = requirement_date
5946            )
5947           AND
5948           (:organization_id = :l_miss_num
5949            OR :organization_id IS NULL
5950            AND organization_id IS NULL
5951            OR :organization_id = organization_id
5952            )
5953           AND
5954           (:inventory_item_id = :l_miss_num
5955            OR :inventory_item_id IS NULL
5956            AND inventory_item_id IS NULL
5957            OR :inventory_item_id = inventory_item_id
5958            )
5959           AND
5960           (:demand_source_type_id = :l_miss_num
5961            OR :demand_source_type_id IS NULL
5962            AND demand_source_type_id IS NULL
5963            OR :demand_source_type_id
5964            = demand_source_type_id
5965            )
5966           AND
5967           (:demand_source_header_id = :l_miss_num
5968            OR :demand_source_header_id IS NULL
5969            AND demand_source_header_id IS NULL
5970            OR :demand_source_header_id
5971            = demand_source_header_id
5972            )
5973           AND
5974           (:demand_source_line_id = :l_miss_num
5975            OR :demand_source_line_id IS NULL
5976            AND demand_source_line_id IS NULL
5977            OR :demand_source_line_id = demand_source_line_id
5978            )
5979           AND
5980           (:demand_source_name = :l_miss_char
5981            OR :demand_source_name IS NULL
5982            AND demand_source_name IS NULL
5983            OR :demand_source_name = demand_source_name
5984            )
5985           AND
5986           (:demand_source_delivery = :l_miss_num
5987            OR :demand_source_delivery IS NULL
5988            AND demand_source_delivery IS NULL
5989            OR :demand_source_delivery = demand_source_delivery
5990            )
5991         AND
5992           (:primary_uom_code = :l_miss_char
5993            OR :primary_uom_code IS NULL
5994            AND primary_uom_code IS NULL
5995            OR :primary_uom_code = primary_uom_code
5996            )
5997           AND
5998           (:primary_uom_id = :l_miss_num
5999            OR :primary_uom_id IS NULL
6000            AND primary_uom_id IS NULL
6001            OR :primary_uom_id = primary_uom_id
6002            )
6003            -- INVCONV BEGIN
6004           AND
6005           (:secondary_uom_code = :l_miss_char
6006            OR :secondary_uom_code IS NULL
6007            AND secondary_uom_code IS NULL
6008            OR :secondary_uom_code = secondary_uom_code
6009            )
6010           AND
6011           (:secondary_uom_id = :l_miss_num
6012            OR :secondary_uom_id IS NULL
6013            AND secondary_uom_id IS NULL
6014            OR :secondary_uom_id = secondary_uom_id
6015            )
6016           -- INVCONV END
6017           AND
6018           (:reservation_uom_code = :l_miss_char
6019            OR :reservation_uom_code IS NULL
6020            AND reservation_uom_code IS NULL
6021            OR :reservation_uom_code = reservation_uom_code
6022            )
6023           AND
6024           (:reservation_uom_id = :l_miss_num
6025            OR :reservation_uom_id IS NULL
6026            AND reservation_uom_id IS NULL
6027            OR :reservation_uom_id = reservation_uom_id
6028            )
6029           AND
6030           (:autodetail_group_id = :l_miss_num
6031            OR :autodetail_group_id IS NULL
6032            AND autodetail_group_id IS NULL
6033            OR :autodetail_group_id = autodetail_group_id
6034            )
6035           AND
6036           (:external_source_code = :l_miss_char
6037            OR :external_source_code IS NULL
6038            AND external_source_code IS NULL
6039            OR :external_source_code = external_source_code
6040            )
6041           AND
6042           (:external_source_line_id = :l_miss_num
6043            OR :external_source_line_id IS NULL
6044            AND external_source_line_id IS NULL
6045            OR :external_source_line_id = external_source_line_id
6046            )
6047           AND
6048           (:supply_source_name = :l_miss_char
6049            OR :supply_source_name IS NULL
6050            AND supply_source_name IS NULL
6051            OR :supply_source_name = supply_source_name
6052            )
6053           AND
6054           (:supply_source_line_detail = :l_miss_num
6055            OR :supply_source_line_detail IS NULL
6056            AND supply_source_line_detail IS NULL
6057            OR :supply_source_line_detail
6058            = supply_source_line_detail
6059            )
6060           AND
6061           (:revision = :l_miss_char
6062            OR :revision IS NULL
6063            AND revision IS NULL
6064            OR :revision = revision
6065            )
6066           AND
6067           (:subinventory_code = :l_miss_char
6068            OR :subinventory_code IS NULL
6069            AND subinventory_code IS NULL
6070            OR :subinventory_code = subinventory_code
6071            )
6072          AND
6073           (:subinventory_id = :l_miss_num
6074            OR :subinventory_id IS NULL
6075            AND subinventory_id IS NULL
6076            OR :subinventory_id = subinventory_id
6077            )
6078           AND
6079           (:locator_id = :l_miss_num
6080            OR :locator_id IS NULL
6081            AND locator_id IS NULL
6082            OR :locator_id = locator_id
6083            )
6084           AND
6085           (:lot_number = :l_miss_char
6086            OR :lot_number IS NULL
6087            AND lot_number IS NULL
6088            OR :lot_number = lot_number
6089            )
6090           AND
6091           (:lot_number_id = :l_miss_num
6092            OR :lot_number_id IS NULL
6093            AND lot_number_id IS NULL
6094            OR :lot_number_id = lot_number_id
6095            )
6096           AND
6097           (:lpn_id = :l_miss_num
6098            OR :lpn_id IS NULL
6099            AND lpn_id IS NULL
6100            OR :lpn_id = lpn_id
6101            )
6102           AND
6103           (:ship_ready_flag = :l_miss_num
6104            OR (:ship_ready_flag IS NULL OR :ship_ready_flag = 2)
6105            AND (ship_ready_flag IS NULL OR ship_ready_flag = 2)
6106            OR :ship_ready_flag = ship_ready_flag
6107            )
6108           AND
6109           (:staged_flag = :l_miss_char
6110            OR (:staged_flag IS NULL OR :staged_flag = ''N'')
6111            AND (staged_flag IS NULL OR staged_flag = ''N'')
6112            OR :staged_flag = staged_flag
6113            )
6114           AND
6115           (:attribute_category = :l_miss_char
6116            OR :attribute_category IS NULL
6117            AND attribute_category IS NULL
6118            OR :attribute_category = attribute_category
6119            )
6120           AND
6121           (:attribute1 = :l_miss_char
6122            OR :attribute1 IS NULL
6123            AND attribute1 IS NULL
6124            OR :attribute1 = attribute1
6125            )
6126          AND
6127           (:attribute2 = :l_miss_char
6128            OR :attribute2 IS NULL
6129            AND attribute2 IS NULL
6130            OR :attribute2 = attribute2
6131            )
6132           AND
6133           (:attribute3 = :l_miss_char
6134            OR :attribute3 IS NULL
6135            AND attribute3 IS NULL
6136            OR :attribute3 = attribute3
6137            )
6138           AND
6139           (:attribute4 = :l_miss_char
6140            OR :attribute4 IS NULL
6141            AND attribute4 IS NULL
6142            OR :attribute4 = attribute4
6143            )
6144          AND
6145           (:attribute5 = :l_miss_char
6146            OR :attribute5 IS NULL
6147            AND attribute5 IS NULL
6148            OR :attribute5 = attribute5
6149            )
6150           AND
6151           (:attribute6 = :l_miss_char
6152            OR :attribute6 IS NULL
6153            AND attribute6 IS NULL
6154            OR :attribute6 = attribute6
6155            )
6156           AND
6157           (:attribute7 = :l_miss_char
6158            OR :attribute7 IS NULL
6159            AND attribute7 IS NULL
6160            OR :attribute7 = attribute7
6161            )
6162           AND
6163           (:attribute8 = :l_miss_char
6164            OR :attribute8 IS NULL
6165            AND attribute8 IS NULL
6166            OR :attribute8 = attribute8
6167            )
6168           AND
6169           (:attribute9 = :l_miss_char
6170            OR :attribute9 IS NULL
6171            AND attribute9 IS NULL
6172            OR :attribute9 = attribute9
6173            )
6174           AND
6175           (:attribute10 = :l_miss_char
6176            OR :attribute10 IS NULL
6177            AND attribute10 IS NULL
6178            OR :attribute10 = attribute10
6179            )
6180           AND
6181           (:attribute11 = :l_miss_char
6182            OR :attribute11 IS NULL
6183            AND attribute11 IS NULL
6184            OR :attribute11 = attribute11
6185            )
6186           AND
6187           (:attribute12 = :l_miss_char
6188            OR :attribute12 IS NULL
6189            AND attribute12 IS NULL
6190            OR :attribute12 = attribute12
6191            )
6192           AND
6193           (:attribute13 = :l_miss_char
6194            OR :attribute13 IS NULL
6195            AND attribute13 IS NULL
6196            OR :attribute13 = attribute13
6197            )
6198           AND
6199           (:attribute14 = :l_miss_char
6200            OR :attribute14 IS NULL
6201            AND attribute14 IS NULL
6202            OR :attribute14 = attribute14
6203            )
6204           AND
6205           (:attribute15 = :l_miss_char
6206            OR :attribute15 IS NULL
6207            AND attribute15 IS NULL
6208            OR :attribute15 = attribute15
6209            )
6210 /**** {{ R12 Enhanced reservations code changes }}****/
6211 	AND
6212 	   (:crossdock_flag = :l_miss_char
6213          OR :crossdock_flag IS NULL
6214          AND crossdock_flag IS NULL
6215          OR :crossdock_flag = crossdock_flag
6216          )
6217 	AND
6218 	   (:crossdock_criteria_id = :l_miss_num
6219          OR :crossdock_criteria_id IS NULL
6220          AND crossdock_criteria_id IS NULL
6221          OR :crossdock_criteria_id = crossdock_criteria_id
6222 	    )
6223         AND
6224 	   (:demand_source_line_detail = :l_miss_num
6225          OR :demand_source_line_detail IS NULL
6226          AND demand_source_line_detail IS NULL
6227          OR :demand_source_line_detail = demand_source_line_detail
6228 	    )
6229 	AND
6230 	   (:supply_receipt_date = :l_miss_date
6231          OR :supply_receipt_date IS NULL
6232          AND supply_receipt_date IS NULL
6233          OR :supply_receipt_date = supply_receipt_date
6234 	    )
6235 	AND
6236 	   (:demand_ship_date = :l_miss_date
6237          OR :demand_ship_date IS NULL
6238          AND demand_ship_date IS NULL
6239          OR :demand_ship_date = demand_ship_date
6240 	    )
6241 	AND
6242 	   (:project_id = :l_miss_num
6243          OR :project_id IS NULL
6244          AND project_id IS NULL
6245          OR :project_id = project_id
6246 	    )
6247 	 AND
6248 	   (:task_id = :l_miss_num
6249          OR :task_id IS NULL
6250          AND task_id IS NULL
6251          OR :task_id = task_id
6252 	    )
6253          ';
6254 /***** End R12 ***/
6255 
6256          --Bug 4881317 If supply_source_line_id is passed, append the condition to the query
6257          If  ( p_query_input.supply_source_line_id < fnd_api.g_miss_num
6258                AND p_query_input.supply_source_line_id IS NOT NULL )
6259           THEN
6260               OPEN l_cursor_ref FOR l_qry_stmt
6261                                  || 'AND supply_source_line_id = :supply_source_line_id
6262                                  ' || l_lock_stmt || l_sort_stmt
6263           using
6264            p_query_input.supply_source_header_id
6265           ,p_query_input.supply_source_type_id
6266           ,p_query_input.requirement_date
6267           ,l_miss_date
6268           ,p_query_input.requirement_date
6269           ,p_query_input.requirement_date
6270           ,p_query_input.organization_id
6271           ,l_miss_num
6272           ,p_query_input.organization_id
6273           ,p_query_input.organization_id
6274           ,p_query_input.inventory_item_id
6275           ,l_miss_num
6276           ,p_query_input.inventory_item_id
6277           ,p_query_input.inventory_item_id
6278           ,p_query_input.demand_source_type_id
6279           ,l_miss_num
6280           ,p_query_input.demand_source_type_id
6281           ,p_query_input.demand_source_type_id
6282           ,p_query_input.demand_source_header_id
6283           ,l_miss_num
6284           ,p_query_input.demand_source_header_id
6285           ,p_query_input.demand_source_header_id
6286           ,p_query_input.demand_source_line_id
6287           ,l_miss_num
6288           ,p_query_input.demand_source_line_id
6289           ,p_query_input.demand_source_line_id
6290           ,p_query_input.demand_source_name
6291           ,l_miss_char
6292           ,p_query_input.demand_source_name
6293           ,p_query_input.demand_source_name
6294           ,p_query_input.demand_source_delivery
6295           ,l_miss_num
6296           ,p_query_input.demand_source_delivery
6297           ,p_query_input.demand_source_delivery
6298           ,p_query_input.primary_uom_code
6299           ,l_miss_char
6300           ,p_query_input.primary_uom_code
6301           ,p_query_input.primary_uom_code
6302           ,p_query_input.primary_uom_id
6303           ,l_miss_num
6304           ,p_query_input.primary_uom_id
6305           ,p_query_input.primary_uom_id
6306           -- INVCONV BEGIN
6307           ,p_query_input.secondary_uom_code
6308           ,l_miss_char
6309           ,p_query_input.secondary_uom_code
6310           ,p_query_input.secondary_uom_code
6311           ,p_query_input.secondary_uom_id
6312           ,l_miss_num
6313           ,p_query_input.secondary_uom_id
6314           ,p_query_input.secondary_uom_id
6315           -- INVCONV END
6316           ,p_query_input.reservation_uom_code
6317           ,l_miss_char
6318           ,p_query_input.reservation_uom_code
6319           ,p_query_input.reservation_uom_code
6320           ,p_query_input.reservation_uom_id
6321           ,l_miss_num
6322           ,p_query_input.reservation_uom_id
6323           ,p_query_input.reservation_uom_id
6324           ,p_query_input.autodetail_group_id
6325           ,l_miss_num
6326           ,p_query_input.autodetail_group_id
6327           ,p_query_input.autodetail_group_id
6328           ,p_query_input.external_source_code
6329           ,l_miss_char
6330           ,p_query_input.external_source_code
6331           ,p_query_input.external_source_code
6332           ,p_query_input.external_source_line_id
6333           ,l_miss_num
6334           ,p_query_input.external_source_line_id
6335           ,p_query_input.external_source_line_id
6336           ,p_query_input.supply_source_name
6337           ,l_miss_char
6338           ,p_query_input.supply_source_name
6339           ,p_query_input.supply_source_name
6340           ,p_query_input.supply_source_line_detail
6341           ,l_miss_num
6342           ,p_query_input.supply_source_line_detail
6343           ,p_query_input.supply_source_line_detail
6344           ,p_query_input.revision
6345           ,l_miss_char
6346           ,p_query_input.revision
6347           ,p_query_input.revision
6348           ,p_query_input.subinventory_code
6349           ,l_miss_char
6350           ,p_query_input.subinventory_code
6351           ,p_query_input.subinventory_code
6352           ,p_query_input.subinventory_id
6353           ,l_miss_num
6354           ,p_query_input.subinventory_id
6355           ,p_query_input.subinventory_id
6356           ,p_query_input.locator_id
6357           ,l_miss_num
6358           ,p_query_input.locator_id
6359           ,p_query_input.locator_id
6360           ,p_query_input.lot_number
6361           ,l_miss_char
6362           ,p_query_input.lot_number
6363           ,p_query_input.lot_number
6364           ,p_query_input.lot_number_id
6365           ,l_miss_num
6366           ,p_query_input.lot_number_id
6367           ,p_query_input.lot_number_id
6368           ,p_query_input.lpn_id
6369           ,l_miss_num
6370           ,p_query_input.lpn_id
6371           ,p_query_input.lpn_id
6372           ,p_query_input.ship_ready_flag
6373           ,l_miss_num
6374           ,p_query_input.ship_ready_flag
6375           ,p_query_input.ship_ready_flag
6376           ,p_query_input.ship_ready_flag
6377           ,p_query_input.staged_flag
6378           ,l_miss_char
6379           ,p_query_input.staged_flag
6380           ,p_query_input.staged_flag
6381           ,p_query_input.staged_flag
6382           ,p_query_input.attribute_category
6383           ,l_miss_char
6384           ,p_query_input.attribute_category
6385           ,p_query_input.attribute_category
6386           ,p_query_input.attribute1
6387           ,l_miss_char
6388           ,p_query_input.attribute1
6389           ,p_query_input.attribute1
6390           ,p_query_input.attribute2
6391           ,l_miss_char
6392           ,p_query_input.attribute2
6393           ,p_query_input.attribute2
6394           ,p_query_input.attribute3
6395           ,l_miss_char
6396           ,p_query_input.attribute3
6397           ,p_query_input.attribute3
6398           ,p_query_input.attribute4
6399           ,l_miss_char
6400           ,p_query_input.attribute4
6401           ,p_query_input.attribute4
6402           ,p_query_input.attribute5
6403           ,l_miss_char
6404           ,p_query_input.attribute5
6405           ,p_query_input.attribute5
6406           ,p_query_input.attribute6
6407           ,l_miss_char
6408           ,p_query_input.attribute6
6409           ,p_query_input.attribute6
6410           ,p_query_input.attribute7
6411           ,l_miss_char
6412           ,p_query_input.attribute7
6413           ,p_query_input.attribute7
6414           ,p_query_input.attribute8
6415           ,l_miss_char
6416           ,p_query_input.attribute8
6417           ,p_query_input.attribute8
6418           ,p_query_input.attribute9
6419           ,l_miss_char
6420           ,p_query_input.attribute9
6421           ,p_query_input.attribute9
6422           ,p_query_input.attribute10
6423           ,l_miss_char
6424           ,p_query_input.attribute10
6425           ,p_query_input.attribute10
6426           ,p_query_input.attribute11
6427           ,l_miss_char
6428           ,p_query_input.attribute11
6429           ,p_query_input.attribute11
6430           ,p_query_input.attribute12
6431           ,l_miss_char
6432           ,p_query_input.attribute12
6433           ,p_query_input.attribute12
6434           ,p_query_input.attribute13
6435           ,l_miss_char
6436           ,p_query_input.attribute13
6437           ,p_query_input.attribute13
6438           ,p_query_input.attribute14
6439           ,l_miss_char
6440           ,p_query_input.attribute14
6441           ,p_query_input.attribute14
6442           ,p_query_input.attribute15
6443           ,l_miss_char
6444           ,p_query_input.attribute15
6445 	     ,p_query_input.attribute15
6446  /**** {{ R12 Enhanced reservations code changes }}****/
6447 	   , p_query_input.crossdock_flag
6448 	   , l_miss_char
6449 	   , p_query_input.crossdock_flag
6450 	   , p_query_input.crossdock_flag
6451 	   , p_query_input.crossdock_criteria_id
6452 	   , l_miss_num
6453 	   , p_query_input.crossdock_criteria_id
6454 	   , p_query_input.crossdock_criteria_id
6455 	   , p_query_input.demand_source_line_detail
6456 	   , l_miss_num
6457 	   , p_query_input.demand_source_line_detail
6458 	   , p_query_input.demand_source_line_detail
6459 	   , p_query_input.supply_receipt_date
6460 	   , l_miss_date
6461 	   , p_query_input.supply_receipt_date
6462 	   , p_query_input.supply_receipt_date
6463 	   , p_query_input.demand_ship_date
6464 	   , l_miss_date
6465 	   , p_query_input.demand_ship_date
6466 	   , p_query_input.demand_ship_date
6467 	   , p_query_input.project_id
6468 	   , l_miss_num
6469 	   , p_query_input.project_id
6470 	   , p_query_input.project_id
6471 	   , p_query_input.task_id
6472 	   , l_miss_num
6473 	   , p_query_input.task_id
6474 	   , p_query_input.task_id
6475            ,p_query_input.supply_source_line_id
6476 
6477 	   /***** End R12 ***/
6478 	     ;
6479            ELSE
6480               OPEN l_cursor_ref FOR l_qry_stmt
6481                                  || l_lock_stmt || l_sort_stmt
6482           using
6483            p_query_input.supply_source_header_id
6484           ,p_query_input.supply_source_type_id
6485           ,p_query_input.requirement_date
6486           ,l_miss_date
6487           ,p_query_input.requirement_date
6488           ,p_query_input.requirement_date
6489           ,p_query_input.organization_id
6490           ,l_miss_num
6491           ,p_query_input.organization_id
6492           ,p_query_input.organization_id
6493           ,p_query_input.inventory_item_id
6494           ,l_miss_num
6495           ,p_query_input.inventory_item_id
6496           ,p_query_input.inventory_item_id
6497           ,p_query_input.demand_source_type_id
6498           ,l_miss_num
6499           ,p_query_input.demand_source_type_id
6500           ,p_query_input.demand_source_type_id
6501           ,p_query_input.demand_source_header_id
6502           ,l_miss_num
6503           ,p_query_input.demand_source_header_id
6504           ,p_query_input.demand_source_header_id
6505           ,p_query_input.demand_source_line_id
6506           ,l_miss_num
6507           ,p_query_input.demand_source_line_id
6508           ,p_query_input.demand_source_line_id
6509           ,p_query_input.demand_source_name
6510           ,l_miss_char
6511           ,p_query_input.demand_source_name
6512           ,p_query_input.demand_source_name
6513           ,p_query_input.demand_source_delivery
6514           ,l_miss_num
6515           ,p_query_input.demand_source_delivery
6516           ,p_query_input.demand_source_delivery
6517           ,p_query_input.primary_uom_code
6518           ,l_miss_char
6519           ,p_query_input.primary_uom_code
6520           ,p_query_input.primary_uom_code
6521           ,p_query_input.primary_uom_id
6522           ,l_miss_num
6523           ,p_query_input.primary_uom_id
6524           ,p_query_input.primary_uom_id
6525           -- INVCONV BEGIN
6526           ,p_query_input.secondary_uom_code
6527           ,l_miss_char
6528           ,p_query_input.secondary_uom_code
6529           ,p_query_input.secondary_uom_code
6530           ,p_query_input.secondary_uom_id
6531           ,l_miss_num
6532           ,p_query_input.secondary_uom_id
6533           ,p_query_input.secondary_uom_id
6534           -- INVCONV END
6535           ,p_query_input.reservation_uom_code
6536           ,l_miss_char
6537           ,p_query_input.reservation_uom_code
6538           ,p_query_input.reservation_uom_code
6539           ,p_query_input.reservation_uom_id
6540           ,l_miss_num
6541           ,p_query_input.reservation_uom_id
6542           ,p_query_input.reservation_uom_id
6543           ,p_query_input.autodetail_group_id
6544           ,l_miss_num
6545           ,p_query_input.autodetail_group_id
6546           ,p_query_input.autodetail_group_id
6547           ,p_query_input.external_source_code
6548           ,l_miss_char
6549           ,p_query_input.external_source_code
6550           ,p_query_input.external_source_code
6551           ,p_query_input.external_source_line_id
6552           ,l_miss_num
6553           ,p_query_input.external_source_line_id
6554           ,p_query_input.external_source_line_id
6555           ,p_query_input.supply_source_name
6556           ,l_miss_char
6557           ,p_query_input.supply_source_name
6558           ,p_query_input.supply_source_name
6559           ,p_query_input.supply_source_line_detail
6560           ,l_miss_num
6561           ,p_query_input.supply_source_line_detail
6562           ,p_query_input.supply_source_line_detail
6563           ,p_query_input.revision
6564           ,l_miss_char
6565           ,p_query_input.revision
6566           ,p_query_input.revision
6567           ,p_query_input.subinventory_code
6568           ,l_miss_char
6569           ,p_query_input.subinventory_code
6570           ,p_query_input.subinventory_code
6571           ,p_query_input.subinventory_id
6572           ,l_miss_num
6573           ,p_query_input.subinventory_id
6574           ,p_query_input.subinventory_id
6575           ,p_query_input.locator_id
6576           ,l_miss_num
6577           ,p_query_input.locator_id
6578           ,p_query_input.locator_id
6579           ,p_query_input.lot_number
6580           ,l_miss_char
6581           ,p_query_input.lot_number
6582           ,p_query_input.lot_number
6583           ,p_query_input.lot_number_id
6584           ,l_miss_num
6585           ,p_query_input.lot_number_id
6586           ,p_query_input.lot_number_id
6587           ,p_query_input.lpn_id
6588           ,l_miss_num
6589           ,p_query_input.lpn_id
6590           ,p_query_input.lpn_id
6591           ,p_query_input.ship_ready_flag
6592           ,l_miss_num
6593           ,p_query_input.ship_ready_flag
6594           ,p_query_input.ship_ready_flag
6595           ,p_query_input.ship_ready_flag
6596           ,p_query_input.staged_flag
6597           ,l_miss_char
6598           ,p_query_input.staged_flag
6599           ,p_query_input.staged_flag
6600           ,p_query_input.staged_flag
6601           ,p_query_input.attribute_category
6602           ,l_miss_char
6603           ,p_query_input.attribute_category
6604           ,p_query_input.attribute_category
6605           ,p_query_input.attribute1
6606           ,l_miss_char
6607           ,p_query_input.attribute1
6608           ,p_query_input.attribute1
6609           ,p_query_input.attribute2
6610           ,l_miss_char
6611           ,p_query_input.attribute2
6612           ,p_query_input.attribute2
6613           ,p_query_input.attribute3
6614           ,l_miss_char
6615           ,p_query_input.attribute3
6616           ,p_query_input.attribute3
6617           ,p_query_input.attribute4
6618           ,l_miss_char
6619           ,p_query_input.attribute4
6620           ,p_query_input.attribute4
6621           ,p_query_input.attribute5
6622           ,l_miss_char
6623           ,p_query_input.attribute5
6624           ,p_query_input.attribute5
6625           ,p_query_input.attribute6
6626           ,l_miss_char
6627           ,p_query_input.attribute6
6628           ,p_query_input.attribute6
6629           ,p_query_input.attribute7
6630           ,l_miss_char
6631           ,p_query_input.attribute7
6632           ,p_query_input.attribute7
6633           ,p_query_input.attribute8
6634           ,l_miss_char
6635           ,p_query_input.attribute8
6636           ,p_query_input.attribute8
6637           ,p_query_input.attribute9
6638           ,l_miss_char
6639           ,p_query_input.attribute9
6640           ,p_query_input.attribute9
6641           ,p_query_input.attribute10
6642           ,l_miss_char
6643           ,p_query_input.attribute10
6644           ,p_query_input.attribute10
6645           ,p_query_input.attribute11
6646           ,l_miss_char
6647           ,p_query_input.attribute11
6648           ,p_query_input.attribute11
6649           ,p_query_input.attribute12
6650           ,l_miss_char
6651           ,p_query_input.attribute12
6652           ,p_query_input.attribute12
6653           ,p_query_input.attribute13
6654           ,l_miss_char
6655           ,p_query_input.attribute13
6656           ,p_query_input.attribute13
6657           ,p_query_input.attribute14
6658           ,l_miss_char
6659           ,p_query_input.attribute14
6660           ,p_query_input.attribute14
6661           ,p_query_input.attribute15
6662           ,l_miss_char
6663           ,p_query_input.attribute15
6664 	     ,p_query_input.attribute15
6665  /**** {{ R12 Enhanced reservations code changes }}****/
6666 	   , p_query_input.crossdock_flag
6667 	   , l_miss_char
6668 	   , p_query_input.crossdock_flag
6669 	   , p_query_input.crossdock_flag
6670 	   , p_query_input.crossdock_criteria_id
6671 	   , l_miss_num
6672 	   , p_query_input.crossdock_criteria_id
6673 	   , p_query_input.crossdock_criteria_id
6674 	   , p_query_input.demand_source_line_detail
6675 	   , l_miss_num
6676 	   , p_query_input.demand_source_line_detail
6677 	   , p_query_input.demand_source_line_detail
6678 	   , p_query_input.supply_receipt_date
6679 	   , l_miss_date
6680 	   , p_query_input.supply_receipt_date
6681 	   , p_query_input.supply_receipt_date
6682 	   , p_query_input.demand_ship_date
6683 	   , l_miss_date
6684 	   , p_query_input.demand_ship_date
6685 	   , p_query_input.demand_ship_date
6686 	   , p_query_input.project_id
6687 	   , l_miss_num
6688 	   , p_query_input.project_id
6689 	   , p_query_input.project_id
6690 	   , p_query_input.task_id
6691 	   , l_miss_num
6692 	   , p_query_input.task_id
6693 	   , p_query_input.task_id
6694 
6695 	   /***** End R12 ***/
6696 	     ;
6697 
6698          END IF; -- End If for supply_source_line_id
6699          -- End changes for Bug 4881317
6700 
6701      ELSE
6702        IF (l_debug = 1) then
6703 	  debug_print(' Inside cursor ref no values passed');
6704        END IF;
6705       -- INVCONV - Incorporate secondaries
6706       OPEN l_cursor_ref FOR    'SELECT
6707           reservation_id
6708         , requirement_date
6709         , organization_id
6710         , inventory_item_id
6711         , demand_source_type_id
6712         , demand_source_name
6713         , demand_source_header_id
6714         , demand_source_line_id
6715         , demand_source_delivery
6716         , primary_uom_code
6717         , primary_uom_id
6718         , secondary_uom_code
6719         , secondary_uom_id
6720         , reservation_uom_code
6721         , reservation_uom_id
6722         , reservation_quantity
6723         , primary_reservation_quantity
6724         , secondary_reservation_quantity
6725         , detailed_quantity
6726         , secondary_detailed_quantity
6727         , autodetail_group_id
6728         , external_source_code
6729         , external_source_line_id
6730         , supply_source_type_id
6731         , supply_source_header_id
6732         , supply_source_line_id
6733         , supply_source_name
6734         , supply_source_line_detail
6735         , revision
6736         , subinventory_code
6737         , subinventory_id
6738         , locator_id
6739         , lot_number
6740         , lot_number_id
6741         , pick_slip_number
6742         , lpn_id
6743         , attribute_category
6744         , attribute1
6745         , attribute2
6746         , attribute3
6747         , attribute4
6748         , attribute5
6749         , attribute6
6750         , attribute7
6751         , attribute8
6752         , attribute9
6753         , attribute10
6754         , attribute11
6755         , attribute12
6756         , attribute13
6757         , attribute14
6758         , attribute15
6759         , ship_ready_flag
6760         , staged_flag
6761 	/**** {{ R12 Enhanced reservations code changes }}****/
6762 	 , crossdock_flag
6763 	 , crossdock_criteria_id
6764 	 , demand_source_line_detail
6765 	 , serial_reservation_quantity
6766 	 , supply_receipt_date
6767 	 , demand_ship_date
6768 	 , project_id
6769 	 , task_id
6770 	 , orig_supply_source_type_id
6771 	 , orig_supply_source_header_id
6772 	 , orig_supply_source_line_id
6773 	 , orig_supply_source_line_detail
6774 	 , orig_demand_source_type_id
6775 	 , orig_demand_source_header_id
6776 	 , orig_demand_source_line_id
6777 	 , orig_demand_source_line_detail
6778          , serial_number
6779 	 /***** End R12 ***/
6780         FROM mtl_reservations
6781         WHERE
6782    (:requirement_date = :l_miss_date
6783          OR :requirement_date IS NULL
6784          AND requirement_date IS NULL
6785          OR :requirement_date
6786          = requirement_date
6787          )
6788         AND
6789         (:organization_id = :l_miss_num
6790          OR :organization_id IS NULL
6791          AND organization_id IS NULL
6792          OR :organization_id = organization_id
6793          )
6794         AND
6795         (:inventory_item_id = :l_miss_num
6796          OR :inventory_item_id IS NULL
6797          AND inventory_item_id IS NULL
6798          OR :inventory_item_id = inventory_item_id
6799          )
6800   AND
6801   (:demand_source_type_id = :l_miss_num
6802          OR :demand_source_type_id IS NULL
6803          AND demand_source_type_id IS NULL
6804          OR :demand_source_type_id
6805          = demand_source_type_id
6806          )
6807         AND
6808         (:demand_source_header_id = :l_miss_num
6809          OR :demand_source_header_id IS NULL
6810          AND demand_source_header_id IS NULL
6811          OR :demand_source_header_id
6812          = demand_source_header_id
6813          )
6814         AND
6815         (:demand_source_line_id = :l_miss_num
6816          OR :demand_source_line_id IS NULL
6817          AND demand_source_line_id IS NULL
6818          OR :demand_source_line_id = demand_source_line_id
6819          )
6820         AND
6821         (:demand_source_name = :l_miss_char
6822          OR :demand_source_name IS NULL
6823          AND demand_source_name IS NULL
6824          OR :demand_source_name = demand_source_name
6825          )
6826         AND
6827         (:demand_source_delivery = :l_miss_num
6828          OR :demand_source_delivery IS NULL
6829          AND demand_source_delivery IS NULL
6830          OR :demand_source_delivery = demand_source_delivery
6831          )
6832       AND
6833         (:primary_uom_code = :l_miss_char
6834          OR :primary_uom_code IS NULL
6835          AND primary_uom_code IS NULL
6836          OR :primary_uom_code = primary_uom_code
6837          )
6838         AND
6839         (:primary_uom_id = :l_miss_num
6840          OR :primary_uom_id IS NULL
6841          AND primary_uom_id IS NULL
6842          OR :primary_uom_id = primary_uom_id
6843          )
6844          -- INVCONV BEGIN
6845         AND
6846         (:secondary_uom_code = :l_miss_char
6847          OR :secondary_uom_code IS NULL
6848          AND secondary_uom_code IS NULL
6849          OR :secondary_uom_code = secondary_uom_code
6850          )
6851         AND
6852         (:secondary_uom_id = :l_miss_num
6853          OR :secondary_uom_id IS NULL
6854          AND secondary_uom_id IS NULL
6855          OR :secondary_uom_id = secondary_uom_id
6856          )
6857         -- INVCONV END
6858         AND
6859         (:reservation_uom_code = :l_miss_char
6860          OR :reservation_uom_code IS NULL
6861          AND reservation_uom_code IS NULL
6862          OR :reservation_uom_code = reservation_uom_code
6863          )
6864         AND
6865         (:reservation_uom_id = :l_miss_num
6866          OR :reservation_uom_id IS NULL
6867          AND reservation_uom_id IS NULL
6868          OR :reservation_uom_id = reservation_uom_id
6869          )
6870         AND
6871         (:autodetail_group_id = :l_miss_num
6872          OR :autodetail_group_id IS NULL
6873          AND autodetail_group_id IS NULL
6874          OR :autodetail_group_id = autodetail_group_id
6875          )
6876         AND
6877         (:external_source_code = :l_miss_char
6878          OR :external_source_code IS NULL
6879          AND external_source_code IS NULL
6880          OR :external_source_code = external_source_code
6881          )
6882         AND
6883         (:external_source_line_id = :l_miss_num
6884          OR :external_source_line_id IS NULL
6885          AND external_source_line_id IS NULL
6886          OR :external_source_line_id = external_source_line_id
6887          )
6888         AND
6889         (:supply_source_type_id = :l_miss_num
6890          OR :supply_source_type_id IS NULL
6891          AND supply_source_type_id IS NULL
6892          OR :supply_source_type_id = supply_source_type_id
6893          )
6894         AND
6895         (:supply_source_header_id = :l_miss_num
6896          OR :supply_source_header_id IS NULL
6897          AND supply_source_header_id IS NULL
6898          OR :supply_source_header_id
6899          = supply_source_header_id
6900          )
6901         AND
6902         (:supply_source_line_id = :l_miss_num
6903          OR :supply_source_line_id IS NULL
6904          AND supply_source_line_id IS NULL
6905          OR :supply_source_line_id = supply_source_line_id
6906          )
6907         AND
6908         (:supply_source_name = :l_miss_char
6909          OR :supply_source_name IS NULL
6910          AND supply_source_name IS NULL
6911          OR :supply_source_name = supply_source_name
6912          )
6913         AND
6914         (:supply_source_line_detail = :l_miss_num
6915          OR :supply_source_line_detail IS NULL
6916          AND supply_source_line_detail IS NULL
6917          OR :supply_source_line_detail
6918          = supply_source_line_detail
6919          )
6920         AND
6921         (:revision = :l_miss_char
6922          OR :revision IS NULL
6923          AND revision IS NULL
6924          OR :revision = revision
6925          )
6926         AND
6927         (:subinventory_code = :l_miss_char
6928          OR :subinventory_code IS NULL
6929          AND subinventory_code IS NULL
6930          OR :subinventory_code = subinventory_code
6931          )
6932        AND
6933         (:subinventory_id = :l_miss_num
6934          OR :subinventory_id IS NULL
6935          AND subinventory_id IS NULL
6936          OR :subinventory_id = subinventory_id
6937          )
6938         AND
6939         (:locator_id = :l_miss_num
6940          OR :locator_id IS NULL
6941          AND locator_id IS NULL
6942          OR :locator_id = locator_id
6943          )
6944         AND
6945         (:lot_number = :l_miss_char
6946          OR :lot_number IS NULL
6947          AND lot_number IS NULL
6948          OR :lot_number = lot_number
6949          )
6950         AND
6951         (:lot_number_id = :l_miss_num
6952          OR :lot_number_id IS NULL
6953          AND lot_number_id IS NULL
6954          OR :lot_number_id = lot_number_id
6955          )
6956         AND
6957         (:lpn_id = :l_miss_num
6958          OR :lpn_id IS NULL
6959          AND lpn_id IS NULL
6960          OR :lpn_id = lpn_id
6961          )
6962         AND
6963         (:ship_ready_flag = :l_miss_num
6964          OR (:ship_ready_flag IS NULL OR :ship_ready_flag = 2)
6965          AND (ship_ready_flag IS NULL OR ship_ready_flag = 2)
6966          OR :ship_ready_flag = ship_ready_flag
6967          )
6968         AND
6969         (:staged_flag = :l_miss_char
6970          OR (:staged_flag IS NULL OR :staged_flag = ''N'')
6971          AND (staged_flag IS NULL OR staged_flag = ''N'')
6972          OR :staged_flag = staged_flag
6973          )
6974         AND
6975         (:attribute_category = :l_miss_char
6976          OR :attribute_category IS NULL
6977          AND attribute_category IS NULL
6978          OR :attribute_category = attribute_category
6979          )
6980         AND
6981         (:attribute1 = :l_miss_char
6982          OR :attribute1 IS NULL
6983          AND attribute1 IS NULL
6984          OR :attribute1 = attribute1
6985          )
6986        AND
6987         (:attribute2 = :l_miss_char
6988          OR :attribute2 IS NULL
6989          AND attribute2 IS NULL
6990          OR :attribute2 = attribute2
6991          )
6992         AND
6993         (:attribute3 = :l_miss_char
6994          OR :attribute3 IS NULL
6995          AND attribute3 IS NULL
6996          OR :attribute3 = attribute3
6997          )
6998         AND
6999         (:attribute4 = :l_miss_char
7000          OR :attribute4 IS NULL
7001          AND attribute4 IS NULL
7002          OR :attribute4 = attribute4
7003          )
7004        AND
7005         (:attribute5 = :l_miss_char
7006          OR :attribute5 IS NULL
7007          AND attribute5 IS NULL
7008          OR :attribute5 = attribute5
7009          )
7010         AND
7011         (:attribute6 = :l_miss_char
7012          OR :attribute6 IS NULL
7013          AND attribute6 IS NULL
7014          OR :attribute6 = attribute6
7015          )
7016         AND
7017         (:attribute7 = :l_miss_char
7018          OR :attribute7 IS NULL
7019          AND attribute7 IS NULL
7020          OR :attribute7 = attribute7
7021          )
7022         AND
7023         (:attribute8 = :l_miss_char
7024          OR :attribute8 IS NULL
7025          AND attribute8 IS NULL
7026          OR :attribute8 = attribute8
7027          )
7028         AND
7029         (:attribute9 = :l_miss_char
7030          OR :attribute9 IS NULL
7031          AND attribute9 IS NULL
7032          OR :attribute9 = attribute9
7033          )
7034         AND
7035         (:attribute10 = :l_miss_char
7036          OR :attribute10 IS NULL
7037          AND attribute10 IS NULL
7038          OR :attribute10 = attribute10
7039          )
7040         AND
7041         (:attribute11 = :l_miss_char
7042          OR :attribute11 IS NULL
7043          AND attribute11 IS NULL
7044          OR :attribute11 = attribute11
7045          )
7046         AND
7047         (:attribute12 = :l_miss_char
7048          OR :attribute12 IS NULL
7049          AND attribute12 IS NULL
7050          OR :attribute12 = attribute12
7051          )
7052         AND
7053         (:attribute13 = :l_miss_char
7054          OR :attribute13 IS NULL
7055          AND attribute13 IS NULL
7056          OR :attribute13 = attribute13
7057          )
7058         AND
7059         (:attribute14 = :l_miss_char
7060          OR :attribute14 IS NULL
7061          AND attribute14 IS NULL
7062          OR :attribute14 = attribute14
7063          )
7064         AND
7065         (:attribute15 = :l_miss_char
7066          OR :attribute15 IS NULL
7067          AND attribute15 IS NULL
7068          OR :attribute15 = attribute15
7069          )
7070 
7071 /**** {{ R12 Enhanced reservations code changes }}****/
7072 	AND
7073 	   (:crossdock_flag = :l_miss_char
7074          OR :crossdock_flag IS NULL
7075          AND crossdock_flag IS NULL
7076          OR :crossdock_flag = crossdock_flag
7077          )
7078 	AND
7079 	   (:crossdock_criteria_id = :l_miss_num
7080          OR :crossdock_criteria_id IS NULL
7081          AND crossdock_criteria_id IS NULL
7082          OR :crossdock_criteria_id = crossdock_criteria_id
7083 	    )
7084         AND
7085 	   (:demand_source_line_detail = :l_miss_num
7086          OR :demand_source_line_detail IS NULL
7087          AND demand_source_line_detail IS NULL
7088          OR :demand_source_line_detail = demand_source_line_detail
7089 	    )
7090 	AND
7091 	   (:supply_receipt_date = :l_miss_date
7092          OR :supply_receipt_date IS NULL
7093          AND supply_receipt_date IS NULL
7094          OR :supply_receipt_date = supply_receipt_date
7095 	    )
7096 	AND
7097 	   (:demand_ship_date = :l_miss_date
7098          OR :demand_ship_date IS NULL
7099          AND demand_ship_date IS NULL
7100          OR :demand_ship_date = demand_ship_date
7101 	    )
7102 	AND
7103 	   (:project_id = :l_miss_num
7104          OR :project_id IS NULL
7105          AND project_id IS NULL
7106          OR :project_id = project_id
7107 	    )
7108 	 AND
7109 	   (:task_id = :l_miss_num
7110          OR :task_id IS NULL
7111          AND task_id IS NULL
7112          OR :task_id = task_id
7113 	    )
7114 /***** End R12 ***/
7115 
7116   '
7117                             || l_lock_stmt
7118                             || l_sort_stmt
7119         USING  p_query_input.requirement_date
7120              , l_miss_date
7121              , p_query_input.requirement_date
7122              , p_query_input.requirement_date
7123              , p_query_input.organization_id
7124              , l_miss_num
7125              , p_query_input.organization_id
7126              , p_query_input.organization_id
7127              , p_query_input.inventory_item_id
7128              , l_miss_num
7129              , p_query_input.inventory_item_id
7130              , p_query_input.inventory_item_id
7131              , p_query_input.demand_source_type_id
7132              , l_miss_num
7133              , p_query_input.demand_source_type_id
7134              , p_query_input.demand_source_type_id
7135              , p_query_input.demand_source_header_id
7136              , l_miss_num
7137              , p_query_input.demand_source_header_id
7138              , p_query_input.demand_source_header_id
7139              , p_query_input.demand_source_line_id
7140              , l_miss_num
7141              , p_query_input.demand_source_line_id
7142              , p_query_input.demand_source_line_id
7143              , p_query_input.demand_source_name
7144              , l_miss_char
7145              , p_query_input.demand_source_name
7146              , p_query_input.demand_source_name
7147              , p_query_input.demand_source_delivery
7148              , l_miss_num
7149              , p_query_input.demand_source_delivery
7150              , p_query_input.demand_source_delivery
7151              , p_query_input.primary_uom_code
7152              , l_miss_char
7153              , p_query_input.primary_uom_code
7154              , p_query_input.primary_uom_code
7155              , p_query_input.primary_uom_id
7156              , l_miss_num
7157              , p_query_input.primary_uom_id
7158              , p_query_input.primary_uom_id
7159              -- INVCONV BEGIN
7160              , p_query_input.secondary_uom_code
7161              , l_miss_char
7162              , p_query_input.secondary_uom_code
7163              , p_query_input.secondary_uom_code
7164              , p_query_input.secondary_uom_id
7165              , l_miss_num
7166              , p_query_input.secondary_uom_id
7167              , p_query_input.secondary_uom_id
7168              -- INVCONV END
7169              , p_query_input.reservation_uom_code
7170              , l_miss_char
7171              , p_query_input.reservation_uom_code
7172              , p_query_input.reservation_uom_code
7173              , p_query_input.reservation_uom_id
7174              , l_miss_num
7175              , p_query_input.reservation_uom_id
7176              , p_query_input.reservation_uom_id
7177              , p_query_input.autodetail_group_id
7178              , l_miss_num
7179              , p_query_input.autodetail_group_id
7180              , p_query_input.autodetail_group_id
7181              , p_query_input.external_source_code
7182              , l_miss_char
7183              , p_query_input.external_source_code
7184              , p_query_input.external_source_code
7185              , p_query_input.external_source_line_id
7186              , l_miss_num
7187              , p_query_input.external_source_line_id
7188              , p_query_input.external_source_line_id
7189              , p_query_input.supply_source_type_id
7190              , l_miss_num
7191              , p_query_input.supply_source_type_id
7192              , p_query_input.supply_source_type_id
7193              , p_query_input.supply_source_header_id
7194              , l_miss_num
7195              , p_query_input.supply_source_header_id
7196              , p_query_input.supply_source_header_id
7197              , p_query_input.supply_source_line_id
7198              , l_miss_num
7199              , p_query_input.supply_source_line_id
7200              , p_query_input.supply_source_line_id
7201              , p_query_input.supply_source_name
7202              , l_miss_char
7203              , p_query_input.supply_source_name
7204              , p_query_input.supply_source_name
7205              , p_query_input.supply_source_line_detail
7206              , l_miss_num
7207              , p_query_input.supply_source_line_detail
7208              , p_query_input.supply_source_line_detail
7209              , p_query_input.revision
7210              , l_miss_char
7211              , p_query_input.revision
7212              , p_query_input.revision
7213              , p_query_input.subinventory_code
7214              , l_miss_char
7215              , p_query_input.subinventory_code
7216              , p_query_input.subinventory_code
7217              , p_query_input.subinventory_id
7218              , l_miss_num
7219              , p_query_input.subinventory_id
7220              , p_query_input.subinventory_id
7221              , p_query_input.locator_id
7222              , l_miss_num
7223              , p_query_input.locator_id
7224              , p_query_input.locator_id
7225              , p_query_input.lot_number
7226              , l_miss_char
7227              , p_query_input.lot_number
7228              , p_query_input.lot_number
7229              , p_query_input.lot_number_id
7230              , l_miss_num
7231              , p_query_input.lot_number_id
7232              , p_query_input.lot_number_id
7233              , p_query_input.lpn_id
7234              , l_miss_num
7235              , p_query_input.lpn_id
7236              , p_query_input.lpn_id
7237              , p_query_input.ship_ready_flag
7238              , l_miss_num
7239              , p_query_input.ship_ready_flag
7240              , p_query_input.ship_ready_flag
7241              , p_query_input.ship_ready_flag
7242              , p_query_input.staged_flag
7243              , l_miss_char
7244              , p_query_input.staged_flag
7245              , p_query_input.staged_flag
7246              , p_query_input.staged_flag
7247              , p_query_input.attribute_category
7248              , l_miss_char
7249              , p_query_input.attribute_category
7250              , p_query_input.attribute_category
7251              , p_query_input.attribute1
7252              , l_miss_char
7253              , p_query_input.attribute1
7254              , p_query_input.attribute1
7255              , p_query_input.attribute2
7256              , l_miss_char
7257              , p_query_input.attribute2
7258              , p_query_input.attribute2
7259              , p_query_input.attribute3
7260              , l_miss_char
7261              , p_query_input.attribute3
7262              , p_query_input.attribute3
7263              , p_query_input.attribute4
7264              , l_miss_char
7265              , p_query_input.attribute4
7266              , p_query_input.attribute4
7267              , p_query_input.attribute5
7268              , l_miss_char
7269              , p_query_input.attribute5
7270              , p_query_input.attribute5
7271              , p_query_input.attribute6
7272              , l_miss_char
7273              , p_query_input.attribute6
7274              , p_query_input.attribute6
7275              , p_query_input.attribute7
7276              , l_miss_char
7277              , p_query_input.attribute7
7278              , p_query_input.attribute7
7279              , p_query_input.attribute8
7280              , l_miss_char
7281              , p_query_input.attribute8
7282              , p_query_input.attribute8
7283              , p_query_input.attribute9
7284              , l_miss_char
7285              , p_query_input.attribute9
7286              , p_query_input.attribute9
7287              , p_query_input.attribute10
7288              , l_miss_char
7289              , p_query_input.attribute10
7290              , p_query_input.attribute10
7291              , p_query_input.attribute11
7292              , l_miss_char
7293              , p_query_input.attribute11
7294              , p_query_input.attribute11
7295              , p_query_input.attribute12
7296              , l_miss_char
7297              , p_query_input.attribute12
7298              , p_query_input.attribute12
7299              , p_query_input.attribute13
7300              , l_miss_char
7301              , p_query_input.attribute13
7302              , p_query_input.attribute13
7303              , p_query_input.attribute14
7304              , l_miss_char
7305              , p_query_input.attribute14
7306              , p_query_input.attribute14
7307              , p_query_input.attribute15
7308              , l_miss_char
7309              , p_query_input.attribute15
7310 	   , p_query_input.attribute15
7311  /**** {{ R12 Enhanced reservations code changes }}****/
7312 	   , p_query_input.crossdock_flag
7313 	   , l_miss_char
7314 	   , p_query_input.crossdock_flag
7315 	   , p_query_input.crossdock_flag
7316 	   , p_query_input.crossdock_criteria_id
7317 	   , l_miss_num
7318 	   , p_query_input.crossdock_criteria_id
7319 	   , p_query_input.crossdock_criteria_id
7320 	   , p_query_input.demand_source_line_detail
7321 	   , l_miss_num
7322 	   , p_query_input.demand_source_line_detail
7323 	   , p_query_input.demand_source_line_detail
7324 	   , p_query_input.supply_receipt_date
7325 	   , l_miss_date
7326 	   , p_query_input.supply_receipt_date
7327 	   , p_query_input.supply_receipt_date
7328 	   , p_query_input.demand_ship_date
7329 	   , l_miss_date
7330 	   , p_query_input.demand_ship_date
7331 	   , p_query_input.demand_ship_date
7332 	   , p_query_input.project_id
7333 	   , l_miss_num
7334 	   , p_query_input.project_id
7335 	   , p_query_input.project_id
7336 	   , p_query_input.task_id
7337 	   , l_miss_num
7338 	   , p_query_input.task_id
7339 	   , p_query_input.task_id
7340 
7341 	   /***** End R12 ***/
7342 	   ;
7343     END IF;
7344 
7345     --
7346     l_counter                    := 0;
7347 
7348     LOOP
7349        IF l_res_cursor THEN
7350 	  IF l_update THEN
7351 	     --    IF (l_debug = 1) then
7352 	     --	debug_print(' Inside res cursor for update...');
7353 	     --    END IF;
7354 	     FETCH c_res_id_update INTO l_rsv_rec;
7355 	     EXIT WHEN c_res_id_update%NOTFOUND;
7356 	   ELSE
7357 		--	IF (l_debug = 1) then
7358 		--	   debug_print(' Inside res cursor no update...');
7359 		--	END IF;
7360 		FETCH c_res_id INTO l_rsv_rec;
7361 		EXIT WHEN c_res_id%NOTFOUND;
7362 	  END IF;
7363 	ELSIF l_demand_cursor THEN
7364 		IF l_update THEN
7365 		   --	   IF (l_debug = 1) then
7366 		   --	      debug_print (' Inside demand cursor for update...');
7367 		   --	   END IF;
7368 		   FETCH c_demand_update INTO l_rsv_rec;
7369 		   EXIT WHEN c_demand_update%NOTFOUND;
7370 		 ELSE
7371 		      --      IF (l_debug = 1) then
7372 		      -- debug_print (' Inside demand cursor no update...');
7373 		      -- END IF;
7374 		      FETCH c_demand INTO l_rsv_rec;
7375 		      EXIT WHEN c_demand%NOTFOUND;
7376 		END IF;
7377 	ELSE
7378 		      --IF (l_debug = 1) then
7379 		      --	 debug_print(' Inside ref cursor...');
7380 		      --    END IF;
7381 		      FETCH l_cursor_ref INTO l_rsv_rec;
7382 		      EXIT WHEN l_cursor_ref%NOTFOUND;
7383        END IF;
7384 
7385        l_counter                         := l_counter + 1;
7386        x_mtl_reservation_tbl(l_counter)  := l_rsv_rec;
7387     END LOOP;
7388 
7389     IF c_res_id%ISOPEN THEN
7390        CLOSE c_res_id;
7391      ELSIF c_res_id_update%ISOPEN THEN
7392        CLOSE c_res_id_update;
7393      ELSIF c_demand_update%ISOPEN THEN
7394        CLOSE c_demand_update;
7395      ELSIF c_demand%ISOPEN THEN
7396        CLOSE c_demand;
7397      ELSE
7398        CLOSE l_cursor_ref;
7399     END IF;
7400 
7401     IF (l_debug = 1) then
7402        debug_print(' Counter: ' || l_counter);
7403        debug_print(' return status: ' || l_return_status);
7404        debug_print('error code' || inv_reservation_global.g_err_no_error);
7405     END IF;
7406     --
7407     x_mtl_reservation_tbl_count  := l_counter;
7408     x_return_status              := l_return_status;
7409     x_error_code                 := inv_reservation_global.g_err_no_error;
7410   --
7411   EXCEPTION
7412     WHEN fnd_api.g_exc_error THEN
7413       x_return_status  := fnd_api.g_ret_sts_error;
7414       --  Get message count and data
7415       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7416     WHEN fnd_api.g_exc_unexpected_error THEN
7417       x_return_status  := fnd_api.g_ret_sts_unexp_error;
7418       --  Get message count and data
7419       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7420     WHEN OTHERS THEN
7421       IF SQLCODE = -54 THEN -- failed to lock
7422         x_return_status  := fnd_api.g_ret_sts_error;
7423         x_error_code     := inv_reservation_global.g_err_fail_to_lock_rec;
7424       ELSE
7425         x_return_status  := fnd_api.g_ret_sts_unexp_error;
7426 
7427         IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
7428           fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
7429         END IF;
7430 
7431         --  Get message count and data
7432         fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7433       END IF;
7434   END query_reservation;
7435 
7436  /**** {{ R12 Enhanced reservations code changes. Overloaded query
7437   -- reservation for querying serial numbers  }}****/
7438   -- Overloaded API for query_reservation
7439 
7440   PROCEDURE query_reservation
7441     (
7442      p_api_version_number        	IN     	NUMBER
7443      , p_init_msg_lst              	IN     	VARCHAR2 DEFAULT fnd_api.g_false
7444      , x_return_status             	OUT   	NOCOPY VARCHAR2
7445      , x_msg_count                 	OUT    	NOCOPY NUMBER
7446      , x_msg_data                  	OUT    	NOCOPY VARCHAR2
7447      , p_query_input               	IN     	inv_reservation_global.mtl_reservation_rec_type
7448      , p_lock_records              	IN     	VARCHAR2 DEFAULT fnd_api.g_false
7449      , p_sort_by_req_date          	IN     	NUMBER DEFAULT inv_reservation_global.g_query_no_sort
7450      , p_cancel_order_mode         	IN     	NUMBER DEFAULT inv_reservation_global.g_cancel_order_no
7451      , p_serial_number_table	        IN	inv_reservation_global.rsv_serial_number_table
7452      , x_mtl_reservation_tbl       	OUT    	NOCOPY inv_reservation_global.mtl_reservation_tbl_type
7453      , x_mtl_reservation_tbl_count 	OUT    	NOCOPY NUMBER
7454      , x_serial_number_table	        OUT	NOCOPY inv_reservation_global.rsv_serial_number_table
7455     , x_serial_number_table_count	OUT 	NOCOPY NUMBER
7456     , x_error_code                	OUT    	NOCOPY NUMBER
7457     ) IS
7458 
7459        l_api_name     CONSTANT VARCHAR2(30) := 'Query_Reservation';
7460        l_serial_number_table inv_reservation_global.rsv_serial_number_table;
7461        l_serial_table_index BINARY_INTEGER;
7462        l_output_index BINARY_INTEGER;
7463        l_reservation_index BINARY_INTEGER;
7464        l_reservation_id NUMBER;
7465        l_return_status VARCHAR2(1) := fnd_api.g_ret_sts_success;
7466        l_debug NUMBER;
7467        l_serial_number varchar2(30);
7468        l_mtl_reservation_tbl
7469 	 inv_reservation_global.mtl_reservation_tbl_type;
7470        l_progress NUMBER;
7471        l_mtl_reservation_tbl_count NUMBER;
7472        l_rsv_rec inv_reservation_global.mtl_reservation_rec_type;
7473        l_error_code NUMBER;
7474 
7475 
7476   BEGIN
7477 
7478      -- The new API will be called to query a set of serial numbers or if
7479      -- the reservation record has serial numbers and the query API has to
7480      -- return the serial numbers, then this API will be used.
7481      -- All the serial number retrieval logic will go here. This will
7482      -- inturn call the old API to get the reservation records and for each
7483      -- reservation record, we will have to query the serials and return
7484      -- the serials along with reservation IDs will be passed back as
7485      -- output.
7486 
7487      l_debug := g_debug;
7488 
7489       IF l_debug=1 THEN
7490 	  debug_print ('Inside overloaded query reservations');
7491       END IF;
7492       l_rsv_rec := p_query_input;
7493      -- First call the query reservations to get all the reservation
7494      -- records.
7495       inv_reservation_pvt.query_reservation
7496 	 (p_api_version_number             => 1.0,
7497 	  p_init_msg_lst                   => fnd_api.g_false,
7498 	  x_return_status                  => l_return_status,
7499 	  x_msg_count                      => x_msg_count,
7500 	  x_msg_data                       => x_msg_data,
7501 	  p_query_input                    => l_rsv_rec,
7502 	  p_lock_records                   => fnd_api.g_true,
7503 	  p_sort_by_req_date               => p_sort_by_req_date,
7504 	  x_mtl_reservation_tbl            => l_mtl_reservation_tbl,
7505 	  x_mtl_reservation_tbl_count      => l_mtl_reservation_tbl_count,
7506 	  x_error_code                     => l_error_code
7507 	  );
7508 
7509        IF l_debug=1 THEN
7510 	  debug_print ('Return Status after querying reservations '||l_return_status);
7511        END IF;
7512 
7513        l_progress := 80;
7514 
7515        IF l_return_status = fnd_api.g_ret_sts_error THEN
7516 
7517 	  IF l_debug=1 THEN
7518 	     debug_print('Raising expected error'||l_return_status);
7519 	  END IF;
7520 	  RAISE fnd_api.g_exc_error;
7521 
7522 	ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
7523 
7524 	  IF l_debug=1 THEN
7525 	     debug_print('Rasing Unexpected error'||l_return_status);
7526 	  END IF;
7527 	  RAISE fnd_api.g_exc_unexpected_error;
7528 
7529        END IF;
7530 
7531        IF (l_debug=1) THEN
7532 	    debug_print('x_mtl_reservation_tbl_count='|| l_mtl_reservation_tbl_count);
7533        END IF;
7534 
7535        -- check to see if the serial number table is empty or not
7536        IF p_serial_number_table.COUNT = 0 THEN
7537 	  -- then nothing was passed and get all the serial numbers for
7538 	  -- each reservation record returned from query
7539 
7540 	  FOR i IN 1..l_mtl_reservation_tbl_count LOOP
7541 	     BEGIN
7542 		IF (l_debug=1) THEN
7543 		   debug_print('Count. i: '|| i);
7544 		END IF;
7545 		SELECT reservation_id, serial_number bulk collect INTO
7546 		  l_serial_number_table FROM mtl_serial_numbers
7547 		  WHERE reservation_id = l_mtl_reservation_tbl(i).reservation_id;
7548 	     EXCEPTION
7549 		WHEN no_data_found THEN
7550 		   IF l_debug=1 THEN
7551 		      debug_print('No serials found for reservation record. id: ' || l_mtl_reservation_tbl(i).reservation_id);
7552 		   END IF;
7553 	     END;
7554 
7555 	  END LOOP;
7556 
7557 	ELSE
7558 		   -- serial numbers are passed. loop all the serials and make sure
7559 		   -- that the serial number passed is infact valid and belong to the reservation records returned.
7560 		   IF (l_debug=1) THEN
7561 		      debug_print('Total number of serials passed: '|| p_serial_number_table.COUNT);
7562 		   END IF;
7563 
7564 		   FOR l_serial_table_index IN p_serial_number_table.first..p_serial_number_table.last
7565 		   LOOP
7566 		      l_serial_number :=
7567 			p_serial_number_table(l_serial_table_index).serial_number;
7568 
7569 		      BEGIN
7570 
7571 			 IF (l_debug=1) THEN
7572 			    debug_print('Count. Serial index: '|| l_serial_table_index);
7573 			 END IF;
7574 
7575 			 SELECT reservation_id INTO l_reservation_id FROM
7576 			   mtl_serial_numbers WHERE serial_number = l_serial_number AND
7577 			   current_organization_id = l_mtl_reservation_tbl(l_serial_table_index).organization_id AND
7578 			   inventory_item_id = l_mtl_reservation_tbl(l_serial_table_index).inventory_item_id;
7579 
7580 		      EXCEPTION
7581 			 WHEN no_data_found THEN
7582 			    IF l_debug=1 THEN
7583 			       debug_print('Serial passed cannot be found. Serial Number: ' || l_serial_number);
7584 			    END IF;
7585 		      END;
7586 
7587 		      l_output_index := 1;
7588 		      IF (l_reservation_id IS NOT NULL) THEN
7589 
7590 			 FOR l_reservation_index IN l_mtl_reservation_tbl.first..l_mtl_reservation_tbl.last
7591 			 LOOP
7592 			    IF (l_reservation_id =
7593 				l_mtl_reservation_tbl(l_reservation_index).reservation_id) THEN
7594 			       -- add the serial and the reservation id to
7595 			       -- the serial number table
7596 			       l_serial_number_table(l_output_index).reservation_id := l_reservation_id;
7597 			       l_serial_number_table(l_output_index).serial_number := l_serial_number;
7598 			       l_output_index := l_output_index + 1;
7599 			       EXIT;
7600 			    END IF;
7601 			 END LOOP;
7602 		      END IF;
7603 
7604 		   END LOOP;
7605 
7606        END IF;
7607 
7608        x_mtl_reservation_tbl        := l_mtl_reservation_tbl;
7609        x_mtl_reservation_tbl_count  := l_mtl_reservation_tbl_count;
7610        x_return_status              := l_return_status;
7611        x_error_code                 := inv_reservation_global.g_err_no_error;
7612        x_serial_number_table := l_serial_number_table;
7613        x_serial_number_table_count := l_serial_number_table.COUNT;
7614 
7615   EXCEPTION
7616      WHEN fnd_api.g_exc_error THEN
7617 	x_return_status  := fnd_api.g_ret_sts_error;
7618 	--  Get message count and data
7619 	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7620      WHEN fnd_api.g_exc_unexpected_error THEN
7621 	x_return_status  := fnd_api.g_ret_sts_unexp_error;
7622 	--  Get message count and data
7623 	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7624      WHEN OTHERS THEN
7625 	IF SQLCODE = -54 THEN -- failed to lock
7626 	   x_return_status  := fnd_api.g_ret_sts_error;
7627 	   x_error_code     := inv_reservation_global.g_err_fail_to_lock_rec;
7628 	 ELSE
7629 	   x_return_status  := fnd_api.g_ret_sts_unexp_error;
7630 
7631 	   IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
7632 	      fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
7633 	   END IF;
7634 
7635 	   --  Get message count and data
7636 	   fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7637 	END IF;
7638   END query_reservation;
7639 
7640   /*** End R12 ***/
7641   --
7642   PROCEDURE create_reservation
7643     (
7644      p_api_version_number       IN     NUMBER
7645      , p_init_msg_lst             IN     VARCHAR2 DEFAULT fnd_api.g_false
7646      , x_return_status            OUT    NOCOPY VARCHAR2
7647      , x_msg_count                OUT    NOCOPY NUMBER
7648      , x_msg_data                 OUT    NOCOPY VARCHAR2
7649      , p_rsv_rec                  IN     inv_reservation_global.mtl_reservation_rec_type
7650      , p_serial_number            IN     inv_reservation_global.serial_number_tbl_type
7651      , x_serial_number            OUT    NOCOPY inv_reservation_global.serial_number_tbl_type
7652      , p_partial_reservation_flag IN     VARCHAR2 DEFAULT fnd_api.g_false
7653      , p_force_reservation_flag   IN     VARCHAR2 DEFAULT fnd_api.g_false
7654      , p_validation_flag          IN     VARCHAR2 DEFAULT fnd_api.g_true
7655      , p_over_reservation_flag      IN  NUMBER DEFAULT 0
7656      , x_quantity_reserved        OUT    NOCOPY NUMBER
7657      , x_secondary_quantity_reserved        OUT NOCOPY NUMBER
7658      -- INVCONV
7659      , x_reservation_id           OUT    NOCOPY NUMBER
7660     /**** {{ R12 Enhanced reservations code changes }}****/
7661     , p_partial_rsv_exists        IN  BOOLEAN DEFAULT FALSE
7662     /*** End R12 ***/
7663     , p_substitute_flag           IN  BOOLEAN DEFAULT FALSE /* Bug 6044651 */
7664     ) IS
7665     l_api_version_number CONSTANT NUMBER                                          := 1.0;
7666     l_api_name           CONSTANT VARCHAR2(30)                                    := 'Create_Reservation';
7667     l_return_status               VARCHAR2(1)                                     := fnd_api.g_ret_sts_success;
7668     l_rsv_rec                     inv_reservation_global.mtl_reservation_rec_type;
7669     l_actual_primary_quantity     NUMBER := NULL;
7670     l_tmp_rsv_tbl                 inv_reservation_global.mtl_reservation_tbl_type;
7671     l_tmp_rsv_tbl_count           NUMBER;
7672     l_dummy_rsv_rec               inv_reservation_global.mtl_reservation_rec_type;
7673     l_dummy_serial_array          inv_reservation_global.serial_number_tbl_type;
7674     l_tree_id                     INTEGER;
7675     l_orig_item_cache_index       INTEGER                                         := NULL;
7676     l_orig_org_cache_index        INTEGER                                         := NULL;
7677     l_orig_demand_cache_index     INTEGER                                         := NULL;
7678     l_orig_supply_cache_index     INTEGER                                         := NULL;
7679     l_orig_sub_cache_index        INTEGER                                         := NULL;
7680     l_to_item_cache_index         INTEGER                                         := NULL;
7681     l_to_org_cache_index          INTEGER                                         := NULL;
7682     l_to_demand_cache_index       INTEGER                                         := NULL;
7683     l_to_supply_cache_index       INTEGER                                         := NULL;
7684     l_to_sub_cache_index          INTEGER                                         := NULL;
7685     l_reservation_id              NUMBER;
7686     l_date                        DATE;
7687     l_user_id                     NUMBER;
7688     l_request_id                  NUMBER;
7689     l_login_id                    NUMBER;
7690     l_prog_appl_id                NUMBER;
7691     l_program_id                  NUMBER;
7692     l_rowid                       VARCHAR2(30);
7693     l_error_code                  NUMBER;
7694     l_qty_changed                 NUMBER;
7695     l_secondary_qty_changed       NUMBER; 					-- INVCONV
7696     l_what_field                  VARCHAR2(240);
7697     l_debug number;
7698     l_lot_divisible_flag          VARCHAR2(1)                                    :='Y'; -- INVCONV
7699     l_dual_control_flag           VARCHAR2(1)
7700       :='N'; -- INVCONV
7701     /**** {{ R12 Enhanced reservations code changes }}****/
7702     l_rsv_updated                 BOOLEAN :=FALSE;
7703     l_progress                    NUMBER;
7704     --  l_from_rsv_rec                inv_reservation_global.mtl_reservation_rec_type
7705     --   := p_rsv_rec;
7706     l_to_rsv_rec                  inv_reservation_global.mtl_reservation_rec_type;
7707     l_mtl_reservation_tbl         inv_reservation_global.mtl_reservation_tbl_type;
7708     l_mtl_reservation_tbl_count   NUMBER;
7709     l_quantity_reserved           NUMBER;
7710     l_secondary_quantity_reserved NUMBER;
7711     l_qty_available               NUMBER;
7712     l_serial_index                NUMBER;
7713     l_serial_number inv_reservation_global.serial_number_tbl_type;
7714     l_item_rec  inv_reservation_global.item_record;
7715     l_supply_lock_handle varchar2(128);
7716     l_demand_lock_handle varchar2(128);
7717     l_lock_status NUMBER;
7718     l_group_mark_id NUMBER := NULL;
7719     l_lock_obtained BOOLEAN := FALSE;
7720     l_project_id NUMBER;
7721     l_task_id NUMBER;
7722     l_pjm_enabled NUMBER;
7723     l_reservation_margin_above NUMBER;
7724     l_supply_source_type_id NUMBER;
7725 
7726     CURSOR c_item(p_inventory_item_id NUMBER) IS
7727          SELECT *
7728            FROM mtl_system_items
7729           WHERE inventory_Item_Id = p_inventory_item_id;
7730     /*** End R12 ***/
7731   BEGIN
7732      -- Use cache to get value for l_debug
7733      IF g_is_pickrelease_set IS NULL THEN
7734         g_is_pickrelease_set := 2;
7735         IF INV_CACHE.is_pickrelease THEN
7736            g_is_pickrelease_set := 1;
7737         END IF;
7738      END IF;
7739      IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
7740         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
7741      END IF;
7742 
7743      l_debug := g_debug;
7744 
7745      IF (l_debug = 1) THEN
7746         debug_print('Inside create reservation... ');
7747      END IF;
7748 
7749     --
7750     --  Standard call to check for call compatibility
7751     IF NOT fnd_api.compatible_api_call(l_api_version_number, p_api_version_number, l_api_name, g_pkg_name) THEN
7752       RAISE fnd_api.g_exc_unexpected_error;
7753     END IF;
7754 
7755     --
7756     --  Initialize message list.
7757     IF fnd_api.to_boolean(p_init_msg_lst) THEN
7758       fnd_msg_pub.initialize;
7759     END IF;
7760 
7761     /**** {{ R12 Enhanced reservations code changes.Initializing orig parameters }}****/
7762 
7763     SAVEPOINT create_reservation_sa;
7764 
7765     l_rsv_rec := p_rsv_rec;
7766 
7767     -- Set the original columns to g_miss_xxx as the user should not be
7768     -- setting these values. Do not query by them
7769     l_rsv_rec.orig_supply_source_type_id := fnd_api.g_miss_num;
7770     l_rsv_rec.orig_supply_source_header_id := fnd_api.g_miss_num;
7771     l_rsv_rec.orig_supply_source_line_id := fnd_api.g_miss_num;
7772     l_rsv_rec.orig_supply_source_line_detail := fnd_api.g_miss_num;
7773     l_rsv_rec.orig_demand_source_type_id := fnd_api.g_miss_num;
7774     l_rsv_rec.orig_demand_source_header_id := fnd_api.g_miss_num;
7775     l_rsv_rec.orig_demand_source_line_id := fnd_api.g_miss_num;
7776     l_rsv_rec.orig_demand_source_line_detail := fnd_api.g_miss_num;
7777 
7778 
7779     IF (l_rsv_rec.project_id IS NULL)  THEN
7780        l_rsv_rec.project_id := fnd_api.g_miss_num;
7781     END IF;
7782     IF (l_rsv_rec.task_id IS NULL)  THEN
7783        l_rsv_rec.task_id := fnd_api.g_miss_num;
7784     END IF;
7785 
7786     l_progress :=50;
7787 
7788     inv_reservation_pvt.convert_quantity
7789       (x_return_status      => l_return_status,
7790        px_rsv_rec           => l_rsv_rec
7791        );
7792 
7793     IF l_debug=1 THEN
7794        debug_print('Return Status from convert quantity'||l_return_status);
7795     END IF;
7796 
7797     IF l_return_status = fnd_api.g_ret_sts_error THEN
7798 
7799        IF l_debug=1 THEN
7800 	  debug_print('Raising expected error'||l_return_status);
7801        END IF;
7802        RAISE fnd_api.g_exc_error;
7803 
7804      ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
7805 
7806        IF l_debug=1 THEN
7807 	  debug_print('Rasing Unexpected error'||l_return_status);
7808        END IF;
7809        RAISE fnd_api.g_exc_unexpected_error;
7810 
7811     END IF;
7812 
7813     l_progress :=60;
7814 
7815     IF l_debug=1 THEN
7816        debug_print('Calling query reservation to query existing reservations'||l_return_status);
7817     END IF;
7818 
7819     l_progress := 70;
7820 
7821     inv_reservation_pvt.query_reservation
7822       (p_api_version_number             => 1.0,
7823        p_init_msg_lst                   => fnd_api.g_false,
7824        x_return_status                  => l_return_status,
7825        x_msg_count                      => x_msg_count,
7826        x_msg_data                       => x_msg_data,
7827        p_query_input                    => l_rsv_rec,
7828        p_lock_records                   => fnd_api.g_true,
7829        x_mtl_reservation_tbl            => l_mtl_reservation_tbl,
7830        x_mtl_reservation_tbl_count      => l_mtl_reservation_tbl_count,
7831        x_error_code                     => l_error_code
7832        );
7833 
7834 
7835     IF l_debug=1 THEN
7836        debug_print ('Return Status after querying reservations '||l_return_status);
7837     END IF;
7838 
7839     l_progress := 80;
7840 
7841     IF l_return_status = fnd_api.g_ret_sts_error THEN
7842 
7843        IF l_debug=1 THEN
7844 	  debug_print('Raising expected error'||l_return_status);
7845        END IF;
7846        RAISE fnd_api.g_exc_error;
7847 
7848      ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
7849 
7850        IF l_debug=1 THEN
7851 	  debug_print('Rasing Unexpected error'||l_return_status);
7852        END IF;
7853        RAISE fnd_api.g_exc_unexpected_error;
7854 
7855     END IF;
7856 
7857     IF (l_debug=1) THEN
7858 
7859        debug_print('x_mtl_reservation_tbl_count='|| l_mtl_reservation_tbl_count);
7860 
7861     END IF;
7862 
7863     IF (p_partial_rsv_exists OR l_mtl_reservation_tbl.COUNT > 0) THEN
7864        --Since create reservation is called by OM even when they updated the existing reservation
7865        --we need to query existing reservations and see if the existing reservation can be updated.
7866        IF (l_debug=1) THEN
7867 
7868 	  debug_print('Partial reservation flag passed as true or we found existing reservations');
7869 	  IF p_partial_rsv_exists THEN
7870 	     debug_print('The value of partial rsv exists is TRUE');
7871 	   ELSE
7872 	     debug_print('The value of partial rsv exists is FALSE');
7873 	  END IF;
7874 
7875        END IF;
7876 
7877        IF p_serial_number.COUNT > 0 THEN
7878 	  -- We donot support updating existing reservations if serial
7879 	  --  numbers are passed. the calling api should call update
7880 	  --  reservations and not create reservations
7881 	  IF l_debug=1 THEN
7882 	     debug_print('Serial numbers are passed with partial flag exists. error out');
7883 	  END IF;
7884 	  fnd_message.set_name('INV', 'INV_SER_PARTIAL_RSV_EXISTS');
7885 	  fnd_msg_pub.add;
7886 	  RAISE fnd_api.g_exc_error;
7887        END IF;
7888 
7889        IF l_debug=1 THEN
7890 	  inv_reservation_pvt.print_rsv_rec (p_rsv_rec);
7891        END IF;
7892 
7893        l_progress := 90;
7894 
7895        FOR i IN 1..l_mtl_reservation_tbl_count LOOP
7896 	  inv_reservation_pvt.print_rsv_rec (l_mtl_reservation_tbl(i));
7897 
7898 	  --If the queried reservation record is staged or has a lot number stamped or is
7899 	  -- revision controlled or has an LPN Id stamped or has a different SubInventory
7900 	  l_progress := 100;
7901 
7902 	  IF ((l_mtl_reservation_tbl(i).staged_flag='Y')
7903 	      OR (nvl(l_mtl_reservation_tbl(i).lot_number,'@@@')<>nvl(p_rsv_rec.lot_number,'@@@') AND p_rsv_rec.lot_number<>fnd_api.g_miss_char)
7904 	      OR (nvl(l_mtl_reservation_tbl(i).revision,'@@@')<>nvl(p_rsv_rec.revision,'@@@')AND p_rsv_rec.revision <>fnd_api.g_miss_char)
7905 	      OR (nvl(l_mtl_reservation_tbl(i).lpn_id,-1)<>nvl(p_rsv_rec.lpn_id,-1)AND p_rsv_rec.lpn_id <> fnd_api.g_miss_num)
7906 	      OR (nvl(l_mtl_reservation_tbl(i).subinventory_code,'@@@')<>nvl(p_rsv_rec.subinventory_code,'@@@')AND p_rsv_rec.subinventory_code <>fnd_api.g_miss_char)) THEN
7907 
7908 	     IF (l_debug=1) THEN
7909 		debug_print('Skipping reservation record');
7910 	     END IF;
7911 
7912 	     l_progress := 110;
7913 
7914 	     GOTO next_record;
7915 	   ELSE
7916 
7917 	     IF (l_debug=1) THEN
7918 
7919 		debug_print('Need to update reservation record');
7920 	     END IF;
7921 
7922 	     IF l_debug=1 THEN
7923 
7924 		debug_print('Reservation record that needs to be updated');
7925 		inv_reservation_pvt.print_rsv_rec (l_mtl_reservation_tbl(i));
7926 
7927 	     END IF;
7928 
7929 	     l_progress := 120;
7930 
7931 	     l_to_rsv_rec.primary_reservation_quantity := l_rsv_rec.primary_reservation_quantity
7932 	       + l_mtl_reservation_tbl(i).primary_reservation_quantity;
7933 	     -- INVCONV BEGIN
7934 	     -- Look at the reservation table row to determine if this is a dual control item.
7935 	     -- If it is dual control and a secondary_reservation_quantity has been supplied,
7936 	     -- then  calculate the to_resv_rec.secondary_reservation_quantity.
7937 	     -- Otherwise leave it empty to be computed as necessary by the private level API
7938 	     IF l_mtl_reservation_tbl(i).secondary_reservation_quantity is not NULL and
7939 	       l_to_rsv_rec.secondary_reservation_quantity is not NULL THEN
7940 		l_to_rsv_rec.secondary_reservation_quantity := l_rsv_rec.secondary_reservation_quantity
7941 		  + l_mtl_reservation_tbl(i).secondary_reservation_quantity;
7942 
7943 	     END IF;
7944 	     -- INVCONV END
7945 	     l_progress := 130;
7946 
7947 	     IF l_rsv_rec.reservation_uom_code = l_mtl_reservation_tbl(i).reservation_uom_code THEN
7948 
7949 		l_to_rsv_rec.reservation_quantity := l_rsv_rec.reservation_quantity + l_mtl_reservation_tbl(i).reservation_quantity;
7950 
7951 	      ELSE
7952 
7953 		l_to_rsv_rec.reservation_quantity := NULL;
7954 
7955 	     END IF;
7956 
7957 	     l_progress := 140;
7958 
7959 	     IF (l_debug=1) THEN
7960 
7961 		debug_print('Calling update reservations to update reservation record');
7962 
7963 	     END IF;
7964 
7965 	     inv_reservation_pvt.update_reservation
7966 	       (p_api_version_number          => 1.0,
7967 		p_init_msg_lst                => fnd_api.g_false,
7968 		x_return_status               => l_return_status,
7969 		x_msg_count                   => x_msg_count,
7970 		x_msg_data                    => x_msg_data,
7971 		x_quantity_reserved           => l_quantity_reserved,
7972 		x_secondary_quantity_reserved => l_secondary_quantity_reserved,  -- INVCONV
7973 		p_original_rsv_rec            => l_mtl_reservation_tbl(i),
7974 		p_to_rsv_rec                  => l_to_rsv_rec,
7975 		p_original_serial_number      => p_serial_number,
7976 		p_to_serial_number            => l_dummy_serial_array,
7977 		p_validation_flag             => p_validation_flag,              -- BUG 4705409
7978 		p_partial_reservation_flag    => p_partial_reservation_flag,
7979 		p_check_availability          => fnd_api.g_true,
7980 		p_over_reservation_flag       => p_over_reservation_flag
7981 		);
7982 
7983 	     l_progress := 150;
7984 
7985 	     IF (l_debug=1) THEN
7986 		debug_print ('Return Status after updating reservations '||l_return_status);
7987 	     END IF;
7988 
7989 	     IF l_return_status = fnd_api.g_ret_sts_error THEN
7990 
7991 		IF l_debug=1 THEN
7992 		   debug_print('Raising expected error'||l_return_status);
7993 		END IF;
7994 
7995 		RAISE fnd_api.g_exc_error;
7996 
7997 	      ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
7998 
7999 		IF l_debug=1 THEN
8000 		   debug_print('Raising Unexpected error'||l_return_status);
8001 		END IF;
8002 
8003 		RAISE fnd_api.g_exc_unexpected_error;
8004 	     END IF;
8005 
8006 	     l_progress := 160;
8007 
8008 	     l_quantity_reserved:=l_quantity_reserved - l_mtl_reservation_tbl(i).primary_reservation_quantity;
8009 
8010 	     x_quantity_reserved := l_quantity_reserved;
8011 	     x_reservation_id    := l_mtl_reservation_tbl(i).reservation_id;
8012 
8013 	     IF l_debug=1 THEN
8014 		debug_print('After updating reservation successfully from
8015 			    inside create'|| l_return_status);
8016 		debug_print('Quantity reserved inside create'|| l_quantity_reserved);
8017 		debug_print('Reservation id inside create'|| l_mtl_reservation_tbl(i).reservation_id);
8018 	     END IF;
8019 
8020 	     l_rsv_updated := TRUE;
8021 	     x_return_status := l_return_status;
8022 	     EXIT;
8023 
8024 	  END IF;
8025 
8026 	  <<next_record>>
8027 	    NULL;
8028        END LOOP;
8029 
8030     END IF;
8031 
8032     -- End. Do not need this as we are moving this to the private package.
8033 
8034     IF ((NOT l_rsv_updated) OR (l_mtl_reservation_tbl.COUNT = 0)) THEN
8035 
8036       /*** End R12 ***/
8037 
8038 
8039       /**** {{ R12 Enhanced reservations code changes.Calling the reservation
8040       -- lock API to create a user-defined lock for non-inventory supplies }} *****/
8041 
8042       -- Bug 5199672: Should pass null to supply and demand line detail as
8043       -- we will have to lock the records at the document level and not at
8044       -- the line level. Also, for ASN, pass the source type as PO so that the
8045       -- the lock name would be the same as the PO's
8046 
8047        IF (l_rsv_rec.supply_source_type_id =
8048 	   inv_reservation_global.g_source_type_asn) THEN
8049 	  l_supply_source_type_id :=
8050 	    inv_reservation_global.g_source_type_po;
8051 	ELSE
8052 	  l_supply_source_type_id := l_rsv_rec.supply_source_type_id;
8053        END IF;
8054 
8055        IF (l_rsv_rec.supply_source_type_id <> inv_reservation_global.g_source_type_inv) THEN
8056 	  inv_reservation_lock_pvt.lock_supply_demand_record
8057 	    (p_organization_id => l_rsv_rec.organization_id
8058 	     ,p_inventory_item_id => l_rsv_rec.inventory_item_id
8059 	     ,p_source_type_id => l_supply_source_type_id
8060 	     ,p_source_header_id => l_rsv_rec.supply_source_header_id
8061 	     ,p_source_line_id =>  l_rsv_rec.supply_source_line_id
8062 	     ,p_source_line_detail => NULL
8063 	     ,x_lock_handle => l_supply_lock_handle
8064 	     ,x_lock_status => l_lock_status);
8065 
8066 	  IF l_lock_status = 0 THEN
8067 	     fnd_message.set_name('INV', 'INV_INVALID_LOCK');
8068 	     fnd_msg_pub.ADD;
8069 	     RAISE fnd_api.g_exc_error;
8070 	  END if;
8071 
8072 	  inv_reservation_lock_pvt.lock_supply_demand_record
8073 	    (p_organization_id => l_rsv_rec.organization_id
8074 	     ,p_inventory_item_id => l_rsv_rec.inventory_item_id
8075 	     ,p_source_type_id => l_rsv_rec.demand_source_type_id
8076 	     ,p_source_header_id => l_rsv_rec.demand_source_header_id
8077 	     ,p_source_line_id =>  l_rsv_rec.demand_source_line_id
8078 	     ,p_source_line_detail => NULL
8079 	     ,x_lock_handle => l_demand_lock_handle
8080 	     ,x_lock_status => l_lock_status);
8081 
8082 	  IF l_lock_status = 0 THEN
8083 	     fnd_message.set_name('INV', 'INV_INVALID_LOCK');
8084 	     fnd_msg_pub.ADD;
8085 	     RAISE fnd_api.g_exc_error;
8086 	  END if;
8087 
8088 	 l_lock_obtained := TRUE;
8089 
8090       END IF;
8091 
8092   /*** End R12 ***/
8093 
8094        -- SAVEPOINT create_reservation_sa;
8095        -- Bug #2819700
8096        -- Adding an extra check to make sure that create reservations does not
8097        -- pass a negative reservation quantity.
8098        IF (l_debug = 1) THEN
8099 	  debug_print('Primary_reservation_qty before inserting (create)= '
8100 		              ||To_char(l_rsv_rec.primary_reservation_quantity) );
8101 	  debug_print('Secondary_reservation_qty before inserting (create)= '
8102 		      ||To_char(l_rsv_rec.secondary_reservation_quantity) );   -- INVCONV
8103 	  debug_print('Reservation_qty before inserting (create)= '
8104 		      || To_char(l_rsv_rec.reservation_quantity) );
8105        END IF;
8106 
8107        IF ((NVL(l_rsv_rec.reservation_quantity,0) < 0) OR
8108 	   (NVL(l_rsv_rec.primary_reservation_quantity,0) < 0) ) THEN
8109 	  fnd_message.set_name('INV', 'INV-INVALID RESERVATION QTY');
8110 	  fnd_msg_pub.ADD;
8111 	  RAISE fnd_api.g_exc_error;
8112        END IF;
8113 
8114        /*---------------- Added by nimisra for bug6268983 ----------------
8115        This check is done in Order to Ensure that No custom code can Populate Demand_source_name for
8116        Sales orders Or Internal Orders
8117        --------------------------------------------------------------------*/
8118        IF (p_rsv_rec.demand_source_type_id=2 or p_rsv_rec.demand_source_type_id=8)
8119        and (p_rsv_rec.demand_source_name is NOT NULL) THEN
8120        fnd_message.set_name('INV', 'INV_INVALID_DEMAND_SOURCE');
8121        fnd_msg_pub.ADD; debug_print('For Sales Orders and Internal Orders DEMAND_SOURCE_NAME Should Be Null');
8122        RAISE fnd_api.g_exc_error;
8123        END IF;
8124 
8125        /* ------------------Added by nimisra for bug6268983--------------- */
8126 
8127        -- handle specially for detailed_quantity
8128        IF l_rsv_rec.detailed_quantity IS NULL
8129 	 OR l_rsv_rec.detailed_quantity = fnd_api.g_miss_num THEN
8130 	  l_rsv_rec.detailed_quantity  := 0;
8131        END IF;
8132 
8133        -- INVCONV - KYH Check this
8134        IF l_rsv_rec.secondary_detailed_quantity = fnd_api.g_miss_num THEN
8135 	  l_rsv_rec.secondary_detailed_quantity  := NULL;
8136        END IF;
8137 
8138 
8139        -- INVCONV BEGIN
8140        --
8141        -- validate input
8142        IF check_missing(l_rsv_rec, l_what_field) THEN
8143 	  -- input record attribute can not be missing
8144 	  -- for creation of reservation.
8145 	  -- must be some value or null.
8146 	  fnd_message.set_name('INV', 'INV-RSV-INPUT-MISSING');
8147 	  fnd_message.set_token('FIELD_NAME', l_what_field);
8148 	  fnd_msg_pub.ADD;
8149 	  RAISE fnd_api.g_exc_error;
8150        END IF;
8151 
8152 
8153        IF (l_debug = 1) THEN
8154 	  /**** {{ R12 Enhanced reservations code changes }}****/
8155 	  --  debug_print(' Before CALLING CONVERT record');
8156 	  /*** End R12 ***/
8157 	  debug_print(' reservation is' || l_reservation_id);
8158 	  debug_print(' reservation qty' || l_rsv_rec.reservation_quantity);
8159 	  debug_print(' reservation pri qty' || l_rsv_rec.primary_reservation_quantity);
8160 	  debug_print(' reservation second qty' || l_rsv_rec.secondary_reservation_quantity); -- INVCONV
8161        END IF;
8162 
8163 
8164        /**** {{ R12 Enhanced reservations code changes }}****/
8165 
8166        /**** Commenting out this code as we have already called query
8167        -- reservation in the beginning of this API
8168        --Bug#2729651. Calling the procedure convert_quantity before calling query_reservation. This is
8169        --because the form passes primary_uom as NULL. so this allows multiple reservations for the same
8170        --criteria. We compute the primary UOM if it is passed as NULL before checking for existing reservations.
8171        -- convert quantity between primary uom and reservation uom
8172        -- Bug 2737887. Commneting out the If condition, since
8173        -- we have to call the convert UOM API for all cases.
8174        --  IF (l_rsv_rec.primary_uom_code IS NULL) THEN
8175        convert_quantity(x_return_status => l_return_status, px_rsv_rec => l_rsv_rec);
8176 	 --  END IF;
8177 
8178 	 -- BUG 2737887. Inclding error handling for the above call to
8179 	 -- convert quantity.
8180 	 IF l_return_status = fnd_api.g_ret_sts_error THEN
8181 	 RAISE fnd_api.g_exc_error;
8182 	 END IF;
8183 
8184 	 --
8185 	 IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8186 	 RAISE fnd_api.g_exc_unexpected_error;
8187 	 END IF;
8188 
8189 	 End comment *******/
8190 	 /*** End R12 ***/
8191 
8192 	 IF (l_debug = 1) THEN
8193 	    /**** {{ R12 Enhanced reservations code changes }}****/
8194 	    --debug_print(' AFTER CALLING CONVERT record');
8195 	    /*** End R12 ***/
8196 	    debug_print(' reservation is' || l_reservation_id);
8197 	    debug_print(' reservation qty' || l_rsv_rec.reservation_quantity);
8198 	    debug_print(' reservation pri qty' || l_rsv_rec.primary_reservation_quantity);
8199 	    debug_print(' reservation second qty' || l_rsv_rec.secondary_reservation_quantity); -- INVCONV
8200 	 END IF;
8201 
8202 	 /**** {{ R12 Enhanced reservations code changes }}****/
8203 	 /**** Commenting out this code as we have already called query
8204 	 -- reservation in the beginning of this API
8205 	 -- query to see whether a record with the key
8206 	 -- attributes already exists
8207 	 -- if there is, return error
8208 	 -- user should use update instead.
8209 	 query_reservation
8210 	   (
8211 	   p_api_version_number         => 1.0
8212 	   , p_init_msg_lst               => fnd_api.g_false
8213 	   , x_return_status              => l_return_status
8214 	   , x_msg_count                  => x_msg_count
8215 	   , x_msg_data                   => x_msg_data
8216 	   , p_query_input                => l_rsv_rec
8217 	   , p_lock_records               => fnd_api.g_true
8218 	   , x_mtl_reservation_tbl        => l_tmp_rsv_tbl
8219 	   , x_mtl_reservation_tbl_count  => l_tmp_rsv_tbl_count
8220 	   , x_error_code                 => l_error_code
8221 	   );
8222 
8223 	   IF (l_debug = 1) THEN
8224 	   debug_print('After query_reservations ' || l_return_status);
8225 	   END IF;
8226 	   --
8227 	   IF l_return_status = fnd_api.g_ret_sts_error THEN
8228 	   RAISE fnd_api.g_exc_error;
8229 	   END IF;
8230 
8231 	   --
8232 	   IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8233 	   RAISE fnd_api.g_exc_unexpected_error;
8234 	   END IF;
8235 
8236 	   --
8237 	   IF l_tmp_rsv_tbl_count > 0 THEN
8238 	   fnd_message.set_name('INV', 'INV-RESERVATION-EXIST');
8239 	   fnd_msg_pub.ADD;
8240 	   RAISE fnd_api.g_exc_error;
8241 	   END IF;
8242 
8243 	 ***  End comment *******/
8244 	   -- Get the project and task for demands in OE, INT-ORD and RMA
8245 	   IF l_debug=1 THEN
8246 	      debug_print('Before Rsv rec project id: ' || l_rsv_rec.project_id);
8247 	      debug_print('Before Rsv rec task id: ' || l_rsv_rec.task_id);
8248 	   END IF;
8249          -- Bug : 5264987 : For pick release getting the l_pjm_enabled flag
8250          -- INV_CACHE
8251 	IF INV_CACHE.is_pickrelease THEN
8252 		-- Query for and cache the org record.
8253 		IF (NOT INV_CACHE.set_org_rec(l_rsv_rec.organization_id))
8254 		THEN
8255 			IF (l_debug = 1) THEN
8256 			debug_print('Error caching the org record');
8257 			END IF;
8258 		RAISE fnd_api.g_exc_unexpected_error;
8259 		END IF;
8260 		-- Set the PJM enabled flag.
8261 		l_pjm_enabled := INV_CACHE.org_rec.project_reference_enabled;
8262 	ELSE
8263 
8264 	   BEGIN
8265 	      SELECT project_reference_enabled
8266 		INTO l_pjm_enabled
8267 		FROM   mtl_parameters
8268 		WHERE  organization_id = l_rsv_rec.organization_id;
8269 	   EXCEPTION
8270 	      WHEN no_data_found THEN
8271 		 IF l_debug=1 THEN
8272 		    debug_print('Cannot find the project and task information');
8273 		 END IF;
8274 	   END;
8275 	 END IF;
8276 	 IF (l_rsv_rec.demand_source_type_id IN
8277 	   (inv_reservation_global.g_source_type_oe,
8278 	    inv_reservation_global.g_source_type_internal_ord,
8279 	    inv_reservation_global.g_source_type_rma)) AND
8280 	   (l_pjm_enabled = 1) THEN
8281 
8282 	    IF (l_rsv_rec.demand_source_line_id IS NOT NULL) AND
8283 	      (l_rsv_rec.demand_source_line_id <> fnd_api.g_miss_num)
8284 	      AND ((l_rsv_rec.project_id IS NULL) OR
8285 		   (l_rsv_rec.task_id IS NULL)) THEN
8286 		BEGIN
8287 		   SELECT project_id, task_id INTO l_project_id, l_task_id
8288 		     FROM oe_order_lines_all WHERE
8289 		     line_id = l_rsv_rec.demand_source_line_id;
8290 		     -- Bug 5264987
8291 		     -- Storing in global variables so that they are not queried again during transfer_reservation
8292 		     g_oe_line_id := l_rsv_rec.demand_source_line_id;
8293 		     g_project_id := l_project_id;
8294 		     g_task_id	  := l_task_id;
8295 		EXCEPTION
8296 		   WHEN no_data_found THEN
8297 		      IF l_debug=1 THEN
8298 			 debug_print('Cannot find the project and task information');
8299 		      END IF;
8300 		END;
8301 		IF ((l_rsv_rec.project_id IS NULL) AND
8302 		    (l_project_id IS NOT NULL)) THEN
8303 		   l_rsv_rec.project_id := l_project_id;
8304 		END IF;
8305 		IF ((l_rsv_rec.task_id IS NULL) AND
8306 		    (l_task_id IS NOT NULL)) THEN
8307 		   l_rsv_rec.task_id := l_task_id;
8308 		END IF;
8309 	    END IF;
8310 	  ELSE -- not project enable
8311 	       l_to_rsv_rec.project_id := NULL;
8312 	       l_to_rsv_rec.task_id := NULL;
8313 	 END IF;
8314 	 IF l_debug=1 THEN
8315 	    debug_print('After Rsv rec project id: ' || l_rsv_rec.project_id);
8316 	    debug_print('After Rsv rec task id: ' || l_rsv_rec.task_id);
8317 	 END IF;
8318 	 /*** End R12 ***/
8319 
8320 	   IF (l_debug = 1) THEN
8321 	      debug_print('p_validation_flag' || p_validation_flag);
8322 	   END IF;
8323 	   --
8324 	   /**** {{ R12 Enhanced reservations code changes }}****/
8325 	   IF p_serial_number.COUNT > 0 THEN
8326 	      l_serial_number := p_serial_number;
8327 	   END IF;
8328 	   /*** End R12 ***/
8329 
8330 	   -- call validation api if the validate_flag is set to true
8331 	   -- Bug 2354735: Proceed with Validation if p_validation_flag = 'T' or 'V'
8332 	   IF p_validation_flag = fnd_api.g_true OR p_validation_flag = 'V' THEN
8333 	      inv_reservation_validate_pvt.validate_input_parameters
8334 		(
8335 		 x_return_status              => l_return_status
8336 		 , p_orig_rsv_rec               => l_rsv_rec
8337 		 , p_to_rsv_rec                 => l_dummy_rsv_rec
8338 		 , p_orig_serial_array          => l_serial_number
8339 		 , p_to_serial_array            => l_dummy_serial_array
8340 		 , p_rsv_action_name            => 'CREATE'
8341 		 , x_orig_item_cache_index      => l_orig_item_cache_index
8342 		 , x_orig_org_cache_index       => l_orig_org_cache_index
8343 		 , x_orig_demand_cache_index    => l_orig_demand_cache_index
8344 		 , x_orig_supply_cache_index    => l_orig_supply_cache_index
8345 		 , x_orig_sub_cache_index       => l_orig_sub_cache_index
8346 		 , x_to_item_cache_index        => l_to_item_cache_index
8347 		 , x_to_org_cache_index         => l_to_org_cache_index
8348 		 , x_to_demand_cache_index      => l_to_demand_cache_index
8349 		 , x_to_supply_cache_index      => l_to_supply_cache_index
8350 		 , x_to_sub_cache_index         => l_to_sub_cache_index
8351 		 , p_substitute_flag            => p_substitute_flag           /* Bug 6044651 */
8352 		 );
8353 
8354 	      IF (l_debug = 1) THEN
8355 		 debug_print('After validate_input_parameters ' || l_return_status);
8356 	      END IF;
8357 	      --
8358 	      IF l_return_status = fnd_api.g_ret_sts_error THEN
8359 		 RAISE fnd_api.g_exc_error;
8360 	      END IF;
8361 
8362 	      --
8363 	      IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8364 		 RAISE fnd_api.g_exc_unexpected_error;
8365 	      END IF;
8366 
8367 	      -- INVCONV BEGIN
8368 	      IF NOT is_lot_divisible(l_orig_item_cache_index) THEN
8369 		 l_lot_divisible_flag := 'N';
8370 	      END IF;
8371 	      -- INVCONV END
8372 	   END IF;
8373 
8374 	   --
8375 	   -- Pre Insert CTO Validation
8376 	   IF l_rsv_rec.demand_source_type_id IN (inv_reservation_global.g_source_type_oe, inv_reservation_global.g_source_type_internal_ord, inv_reservation_global.g_source_type_rma) THEN
8377 	      --
8378 	      cto_workflow_api_pk.inventory_reservation_check(p_order_line_id => l_rsv_rec.demand_source_line_id, x_return_status => l_return_status, x_msg_count => x_msg_count, x_msg_data => x_msg_data);
8379 
8380 	      IF (l_debug = 1) THEN
8381 		 debug_print('After CTO validation ' || l_return_status);
8382 	      END IF;
8383 	      --
8384 	      IF l_return_status = fnd_api.g_ret_sts_error THEN
8385 		 RAISE fnd_api.g_exc_error;
8386 	      END IF;
8387 
8388 	      --
8389 	      IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8390 		 RAISE fnd_api.g_exc_unexpected_error;
8391 	      END IF;
8392 	      --
8393 	   END IF;
8394 
8395 
8396 	   IF l_return_status = fnd_api.g_ret_sts_error THEN
8397 	      RAISE fnd_api.g_exc_error;
8398 	   END IF;
8399 
8400 	   --
8401 	   IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8402 	      RAISE fnd_api.g_exc_unexpected_error;
8403 	   END IF;
8404 
8405 	   -- call modify tree procedure to change the tree if supply is inv
8406 	   -- will call quantity validation for other supply sources in the future
8407 	   -- Bug 2354735: Proceed with Trees only if p_validation_flag = 'T'
8408 	   IF  p_force_reservation_flag <> fnd_api.g_true
8409 	     AND p_validation_flag = fnd_api.g_true
8410 	     AND l_rsv_rec.supply_source_type_id = inv_reservation_global.g_source_type_inv THEN
8411 	      inv_quantity_tree_pvt.create_tree
8412 		(
8413 		 p_api_version_number         => 1.0
8414 		 , p_init_msg_lst               => fnd_api.g_true
8415 		 , x_return_status              => l_return_status
8416 		 , x_msg_count                  => x_msg_count
8417 		 , x_msg_data                   => x_msg_data
8418 		 , p_organization_id            => l_rsv_rec.organization_id
8419 		 , p_inventory_item_id          => l_rsv_rec.inventory_item_id
8420 		 , p_tree_mode                  => inv_quantity_tree_pvt.g_reservation_mode
8421 		 , p_is_revision_control        => is_revision_control(l_orig_item_cache_index)
8422 		 , p_is_lot_control             => is_lot_control(l_orig_item_cache_index)
8423 		 , p_is_serial_control          => is_serial_control(l_orig_item_cache_index)
8424 		 , p_asset_sub_only             => FALSE
8425 		 , p_include_suggestion         => TRUE
8426 		 , p_demand_source_type_id      => l_rsv_rec.demand_source_type_id
8427 		 , p_demand_source_header_id    => l_rsv_rec.demand_source_header_id
8428 		 , p_demand_source_line_id      => l_rsv_rec.demand_source_line_id
8429 		 , p_demand_source_name         => l_rsv_rec.demand_source_name
8430 		, p_demand_source_delivery     => l_rsv_rec.demand_source_delivery
8431 		, p_lot_expiration_date        => SYSDATE -- Bug#2716563
8432 		, x_tree_id                    => l_tree_id
8433 		);
8434 
8435 	      IF (l_debug = 1) THEN
8436 		 debug_print('After create tree ' || l_return_status);
8437 	      END IF;
8438 
8439 	      IF l_return_status = fnd_api.g_ret_sts_error THEN
8440 		 RAISE fnd_api.g_exc_error;
8441 	      END IF;
8442 
8443 	      --
8444 	      IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8445 		 RAISE fnd_api.g_exc_unexpected_error;
8446 	      END IF;
8447 
8448 	      IF (l_debug = 1) THEN
8449 		 debug_print('calling modify_tree_crt_del_rel');
8450 		 debug_print('l_rsv_rec.primary_reservation_quantity: ' ||
8451 			     l_rsv_rec.primary_reservation_quantity);
8452 		 debug_print('l_rsv_rec.secondary_reservation_quantity: ' ||
8453 			     l_rsv_rec.secondary_reservation_quantity);  -- INVCONV
8454 
8455 	      END IF;
8456 	      -- INVCONV upgrade call for secondaries and lot divisibility
8457 	      modify_tree_crt_del_rel
8458 		(
8459 		 x_return_status              => l_return_status
8460 		 , p_tree_id                    => l_tree_id
8461 		 , p_revision                   => l_rsv_rec.revision
8462 		 , p_lot_number                 => l_rsv_rec.lot_number
8463 		 , p_subinventory_code          => l_rsv_rec.subinventory_code
8464 		 , p_locator_id                 => l_rsv_rec.locator_id
8465 		 , p_lpn_id                     => l_rsv_rec.lpn_id
8466 		 , p_primary_reservation_quantity=> l_rsv_rec.primary_reservation_quantity
8467 		 , p_second_reservation_quantity=> l_rsv_rec.secondary_reservation_quantity
8468 		 , p_detailed_quantity          => l_rsv_rec.detailed_quantity
8469 		 , p_secondary_detailed_quantity => l_rsv_rec.secondary_detailed_quantity
8470 		 , p_partial_reservation_flag   => p_partial_reservation_flag
8471 		 , p_force_reservation_flag     => p_force_reservation_flag
8472 		 , p_lot_divisible_flag         => l_lot_divisible_flag                            -- INVCONV
8473 		 , p_action                     => 'CREATE'
8474 		 , x_qty_changed                => l_qty_changed
8475 		, x_secondary_qty_changed      => l_secondary_qty_changed				--INVCONV
8476 		);
8477 	      IF (l_debug = 1) THEN
8478 		 debug_print('After modify tree crt del rel ' || l_return_status);
8479 		 debug_print('l_qty_changed: ' || l_qty_changed);
8480 		 debug_print('l_secondary_qty_changed: ' || l_secondary_qty_changed);            -- INVCONV
8481 	      END IF;
8482 
8483 	      --
8484 	      IF l_return_status = fnd_api.g_ret_sts_error THEN
8485 		 RAISE fnd_api.g_exc_error;
8486 	      END IF;
8487 
8488 	      --
8489 	      IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8490 		 RAISE fnd_api.g_exc_unexpected_error;
8491 	      END IF;
8492 
8493 	      /**** {{ R12 Enhanced reservations code changes }}****/
8494 	      --IF (l_debug = 1) THEN
8495 	      -- debug_print(' l_qty_changed' || l_qty_changed);
8496 	      --  debug_print('l_secondary_qty_changed: ' || l_secondary_qty_changed);    -- INVCONV
8497 	      -- END IF;
8498 	      /*** End R12 ***/
8499 
8500 	      IF  l_qty_changed > 0
8501 		AND l_qty_changed < l_rsv_rec.primary_reservation_quantity - NVL(l_rsv_rec.detailed_quantity, 0) THEN
8502 		 -- partial reservation. needs to recompute
8503 		 -- the actual quantity for reservation
8504 		 -- convert quantity between primary uom and reservation uom
8505 		 -- Bug 2116332 - Need to set reservation quantity to NULL
8506 		 --   before calling convert_quantity
8507 
8508 		 l_rsv_rec.primary_reservation_quantity  := l_qty_changed + NVL(l_rsv_rec.detailed_quantity, 0);
8509                  -- 5016196 BEGIN
8510                  -- For dual tracked items recompute the secondary
8511 		 IF l_rsv_rec.secondary_reservation_quantity IS NOT NULL THEN
8512 		   l_rsv_rec.secondary_reservation_quantity  := l_secondary_qty_changed + NVL(l_rsv_rec.secondary_detailed_quantity, 0);
8513                  END IF;
8514                  -- 5016196 END
8515 		 l_rsv_rec.reservation_quantity          := NULL;
8516 		 IF (l_debug = 1) THEN
8517 		    debug_print('l_rsv_rec.detailed_quantity: ' || l_rsv_rec.detailed_quantity);
8518 		    debug_print('l_rsv_rec.primary_reservation_quantity: ' || l_rsv_rec.primary_reservation_quantity);
8519 		    debug_print('l_rsv_rec.secondary_reservation_quantity: ' || l_rsv_rec.secondary_reservation_quantity);
8520 		 END IF;
8521 		 convert_quantity(x_return_status => l_return_status, px_rsv_rec => l_rsv_rec);
8522 
8523 		 IF (l_debug = 1) THEN
8524 		    debug_print('After convert qty ' || l_return_status);
8525 		 END IF;
8526 
8527 		 IF l_return_status = fnd_api.g_ret_sts_error THEN
8528 		    RAISE fnd_api.g_exc_error;
8529 		 END IF;
8530 
8531 		 --
8532 		 IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8533 		    RAISE fnd_api.g_exc_unexpected_error;
8534 		 END IF;
8535 	      END IF;
8536 
8537 	      /**** {{ R12 Enhanced reservations code changes }}****/
8538 	      --Setting the serial reservation quantity
8539 
8540               /*** move to before insert_row
8541 	      IF l_serial_number.COUNT > 0 THEN
8542 		 l_rsv_rec.serial_reservation_quantity := l_serial_number.COUNT;
8543 		 IF (l_rsv_rec.serial_reservation_quantity > l_rsv_rec.primary_reservation_quantity) THEN
8544 		    fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
8545 		    fnd_msg_pub.ADD;
8546 		    RAISE fnd_api.g_exc_error;
8547 		 END IF;
8548 
8549 	      END IF;
8550               ********************/
8551 	      /*** End R12 ***/
8552 
8553 		/**** {{ R12 Enhanced reservations code changes }}****/
8554 		ELSIF (l_rsv_rec.supply_source_type_id IN
8555 		       (inv_reservation_global.g_source_type_wip,
8556 			inv_reservation_global.g_source_type_po,
8557 			inv_reservation_global.g_source_type_asn,
8558 			inv_reservation_global.g_source_type_intransit,
8559 			inv_reservation_global.g_source_type_internal_req,
8560 			inv_reservation_global.g_source_type_rcv)) AND
8561 		  p_over_reservation_flag NOT IN (1,3) THEN
8562 	      -- call the available to reserve API to get the supply and
8563 	      -- demand availability
8564 	      -- Bug 5199672: Should pass g_miss_num as default for supply
8565 	      -- source line detail. Otherwise, high level reservations
8566 	      -- will not be considered.
8567 
8568 	      inv_reservation_avail_pvt.available_supply_to_reserve
8569 		(
8570 		 x_return_status                   => l_return_status
8571 		 , x_msg_count                     => x_msg_count
8572 		 , x_msg_data                      => x_msg_data
8573 		 , x_qty_available_to_reserve      => l_qty_changed
8574 		 , x_qty_available                 => l_qty_available
8575 		 , p_organization_id               => l_rsv_rec.organization_id
8576 		 , p_item_id	                   => l_rsv_rec.inventory_item_id
8577 		 , p_revision                      => l_rsv_rec.revision
8578 		 , p_lot_number                    => l_rsv_rec.lot_number
8579 		 , p_subinventory_code             => l_rsv_rec.subinventory_code
8580 		 , p_locator_id	                   => l_rsv_rec.locator_id
8581 		 , p_supply_source_type_id	    => l_rsv_rec.supply_source_type_id
8582 		 , p_supply_source_header_id	    => l_rsv_rec.supply_source_header_id
8583 		 , p_supply_source_line_id	    => l_rsv_rec.supply_source_line_id
8584 		 , p_supply_source_line_detail	    => Nvl(l_rsv_rec.supply_source_line_detail,fnd_api.g_miss_num)
8585 		 , p_lpn_id			    => l_rsv_rec.lpn_id
8586 		 , p_project_id		            => l_rsv_rec.project_id
8587 		, p_task_id			    => l_rsv_rec.task_id
8588 		, p_api_version_number     	    => 1.0
8589 		, p_init_msg_lst             	    => fnd_api.g_false
8590 		);
8591 
8592 	      IF (l_debug = 1) THEN
8593 		 debug_print('After calling available supply to reserve ' || l_return_status);
8594 		 debug_print('Available quantity to reserve. l_qty_changed: ' || l_qty_changed);
8595 		 debug_print('Available quantity on the document. l_qty_available: ' || l_qty_available);
8596 	      END IF;
8597 
8598 	      --
8599 	      IF l_return_status = fnd_api.g_ret_sts_error THEN
8600 		 RAISE fnd_api.g_exc_error;
8601 	      END IF;
8602 
8603 	      --
8604 	      IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8605 		 RAISE fnd_api.g_exc_unexpected_error;
8606 	      END IF;
8607 
8608          -- bug #5454715. Error out if the available supply to reserve is zero.
8609          IF l_qty_changed = 0 THEN
8610           fnd_message.set_name('INV', 'INV_INVALID_AVAILABLE_QTY');
8611           fnd_msg_pub.ADD;
8612           RAISE fnd_api.g_exc_error;
8613          END IF;
8614 
8615 	      -- Bug 5199672: Removed the condition l_qty_changed > 0
8616 	      IF ((l_rsv_rec.primary_reservation_quantity - l_qty_changed) > 0.000005) THEN
8617 
8618 		   IF (p_partial_reservation_flag = fnd_api.g_false) THEN
8619 		    IF (l_debug = 1) THEN
8620 		       debug_print('The supply document doesnt have enough quantity to be reserved against. error out. ');
8621 		    END IF;
8622 		    fnd_message.set_name('INV', 'INV_INVALID_AVAILABLE_QTY');
8623 		    fnd_msg_pub.ADD;
8624 		    RAISE fnd_api.g_exc_error;
8625 		  ELSE
8626 		    l_rsv_rec.primary_reservation_quantity  := l_qty_changed + NVL(l_rsv_rec.detailed_quantity, 0);
8627 		    l_rsv_rec.reservation_quantity          := NULL;
8628 		    IF (l_debug = 1) THEN
8629 		       debug_print('l_rsv_rec.detailed_quantity: ' || l_rsv_rec.detailed_quantity);
8630 		       debug_print('l_rsv_rec.primary_reservation_quantity: ' || l_rsv_rec.primary_reservation_quantity);
8631 		    END IF;
8632 		    convert_quantity(x_return_status => l_return_status, px_rsv_rec => l_rsv_rec);
8633 
8634 		    IF (l_debug = 1) THEN
8635 		       debug_print('After convert qty ' || l_return_status);
8636 		    END IF;
8637 
8638 		    IF l_return_status = fnd_api.g_ret_sts_error THEN
8639 		       RAISE fnd_api.g_exc_error;
8640 		    END IF;
8641 
8642 		    --
8643 		    IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8644 		       RAISE fnd_api.g_exc_unexpected_error;
8645 		    END IF;
8646 
8647 		 END IF;
8648 	      END IF;
8649 	   END IF;
8650 
8651 	   IF (l_rsv_rec.demand_source_type_id IN
8652 	       (inv_reservation_global.g_source_type_wip,
8653 		inv_reservation_global.g_source_type_oe,
8654 		inv_reservation_global.g_source_type_internal_ord,
8655 		inv_reservation_global.g_source_type_rma))  AND
8656 	     p_over_reservation_flag NOT IN (2,3) THEN
8657 
8658 	      -- Bug 5199672: Should pass g_miss_num as default for demand
8659 	      -- source line detail. Otherwise, high level reservations
8660 	      -- will not be considered.
8661 	      inv_reservation_avail_pvt.available_demand_to_reserve
8662 		(
8663 		 x_return_status                   => l_return_status
8664 		 , x_msg_count                     => x_msg_count
8665 		 , x_msg_data                      => x_msg_data
8666 		 , x_qty_available_to_reserve      => l_qty_changed
8667 		 , x_qty_available                 => l_qty_available
8668 		 , p_organization_id               => l_rsv_rec.organization_id
8669 		 , p_item_id                       => l_rsv_rec.inventory_item_id
8670 		 , p_primary_uom_code              => l_rsv_rec.primary_uom_code
8671 		 , p_demand_source_type_id	   => l_rsv_rec.demand_source_type_id
8672 		 , p_demand_source_header_id	   => l_rsv_rec.demand_source_header_id
8673 		 , p_demand_source_line_id	   => l_rsv_rec.demand_source_line_id
8674 		 , p_demand_source_line_detail     => Nvl(l_rsv_rec.demand_source_line_detail,fnd_api.g_miss_num)
8675 		 , p_project_id		           => l_rsv_rec.project_id
8676 		 , p_task_id			   => l_rsv_rec.task_id
8677 		 , p_api_version_number     	   => 1.0
8678 		 , p_init_msg_lst             	   => fnd_api.g_false
8679 		 );
8680 
8681 
8682 	      IF (l_debug = 1) THEN
8683 		 debug_print('After calling available demand to reserve ' || l_return_status);
8684 		 debug_print('Available quantity to reserve. l_qty_changed: ' || l_qty_changed);
8685 		 debug_print('Available quantity on the document. l_qty_available: ' || l_qty_available);
8686 	      END IF;
8687 
8688 	      --
8689 	      IF l_return_status = fnd_api.g_ret_sts_error THEN
8690 		 RAISE fnd_api.g_exc_error;
8691 	      END IF;
8692 
8693 	      --
8694 	      IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8695 		 RAISE fnd_api.g_exc_unexpected_error;
8696 	      END IF;
8697 
8698 
8699 	      IF (l_rsv_rec.demand_source_type_id in (inv_reservation_global.g_source_type_oe,
8700 						 inv_reservation_global.g_source_type_internal_ord,
8701 						 inv_reservation_global.g_source_type_rma)) THEN
8702 
8703 		 IF NOT
8704 		   (lot_divisible
8705 		    (p_inventory_item_id => l_rsv_rec.inventory_item_id,
8706 		     p_organization_id => l_rsv_rec.organization_id)) THEN
8707 		    get_ship_qty_tolerance
8708 		      (
8709 		       p_api_version_number          =>  1.0
8710 		       , p_init_msg_lst              =>  fnd_api.g_false
8711 		       , x_return_status             => x_return_status
8712 		       , x_msg_count                 => x_msg_count
8713 		       , x_msg_data                  => x_msg_data
8714 		       , p_demand_type_id            => l_rsv_rec.demand_source_type_id
8715 		       , p_demand_header_id          => l_rsv_rec.demand_source_header_id
8716 		       , p_demand_line_id            => l_rsv_rec.demand_source_line_id
8717 		       , x_reservation_margin_above  => l_reservation_margin_above);
8718 
8719 		    IF (l_debug = 1) THEN
8720 		       debug_print('Inside is lot indivisible');
8721 		    END IF;
8722 
8723 		    IF (l_debug = 1) THEN
8724 		       debug_print('After calling get_ship_qty_tolerance ' || x_return_status);
8725 		       debug_print('Reservation margin above ' || l_reservation_margin_above);
8726 		    END IF;
8727 
8728 		    --
8729 		    IF x_return_status = fnd_api.g_ret_sts_error THEN
8730 		       RAISE fnd_api.g_exc_error;
8731 		    END IF;
8732 
8733 		    --
8734 		    IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
8735 		       RAISE fnd_api.g_exc_unexpected_error;
8736 		    END IF;
8737 
8738 		    l_qty_changed := l_qty_changed + l_reservation_margin_above;
8739 		    l_qty_available := l_qty_available + l_reservation_margin_above;
8740 		 END IF;
8741 	      END IF;
8742 
8743 	      IF (l_debug = 1) THEN
8744 		 debug_print('Available quantity to reserve. l_qty_changed: ' || l_qty_changed);
8745 		 debug_print('Available quantity on the document. l_qty_available: ' || l_qty_available);
8746 	      END IF;
8747 
8748 	      -- Bug 5199672: Removed the condition l_qty_changed > 0
8749 	      IF ((l_rsv_rec.primary_reservation_quantity - l_qty_changed) >
8750 		   0.000005) THEN
8751 
8752 		 IF (l_debug = 1) THEN
8753 		    debug_print('The demand document doesnt have enough quantity to be reserved against. error out. ');
8754 		 END IF;
8755 		 fnd_message.set_name('INV', 'INV_INVALID_AVAILABLE_QTY');
8756 		 fnd_msg_pub.ADD;
8757 		 RAISE fnd_api.g_exc_error;
8758 	      END IF;
8759 
8760 	   END IF;
8761 
8762 	   /*** End R12 ***/
8763 
8764 	   -- Bug #2819700
8765 	   -- Adding an extra check to make sure that create reservations does not
8766 	   -- create a negative reservation record.
8767 	   IF (l_debug = 1) THEN
8768 	      debug_print('Primary_reservation_qty before inserting (create)= '
8769 			  || To_char(l_rsv_rec.primary_reservation_quantity) );
8770 	      debug_print('Secondary_reservation_qty before inserting (create)= '
8771 			  || To_char(l_rsv_rec.secondary_reservation_quantity) );  -- INVCONV
8772 	      debug_print('Reservation_qty before inserting (create)= '
8773 			  || To_char(l_rsv_rec.reservation_quantity) );
8774 	   END IF;
8775 
8776 	   IF (  (NVL(l_rsv_rec.reservation_quantity,0) < 0) OR
8777 		 (NVL(l_rsv_rec.primary_reservation_quantity,0) < 0) ) THEN
8778 	      fnd_message.set_name('INV', 'INV-INVALID RESERVATION QTY');
8779 	      fnd_msg_pub.ADD;
8780 	      RAISE fnd_api.g_exc_error;
8781 	   END IF;
8782 
8783 	   --Bug 5535030 Selecting the sequence value has been moved to the table handler in
8784 	   -- INVRSV6B.pls
8785 /*	   -- create reservation id
8786 	   SELECT mtl_demand_s.NEXTVAL
8787 	     INTO l_reservation_id
8788 	     FROM DUAL;*/
8789            l_reservation_id := NULL;
8790 	   --
8791 	   l_date := SYSDATE;
8792 
8793 	   --
8794 	   l_user_id            := fnd_global.user_id;
8795 	   l_login_id           := fnd_global.login_id;
8796 
8797 	   IF l_login_id = -1 THEN
8798 	      l_login_id  := fnd_global.conc_login_id;
8799 	   END IF;
8800 
8801 	   l_request_id         := fnd_global.conc_request_id;
8802 	   l_prog_appl_id       := fnd_global.prog_appl_id;
8803 	   l_program_id         := fnd_global.conc_program_id;
8804 	   --
8805 
8806 	   IF (l_debug = 1) THEN
8807 	      debug_print(' Before inserting record');
8808 	      debug_print(' reservation is' || l_reservation_id);
8809 	      debug_print(' reservation qty' || l_rsv_rec.reservation_quantity);
8810 	      debug_print(' reservation pri qty' || l_rsv_rec.primary_reservation_quantity);
8811 	      debug_print('l_secondary_qty_changed: ' || l_secondary_qty_changed);    -- INVCONV
8812 	   END IF;
8813 
8814 	   -- Bug 3461990: Reservations API should not create reservations with more
8815 	   -- than 5 decimal places, since the transaction quantity is being
8816 	   -- rounded to 5 decimal places.
8817 
8818 	   l_rsv_rec.primary_reservation_quantity :=
8819 	     Round(l_rsv_rec.primary_reservation_quantity,5);
8820 	   l_rsv_rec.reservation_quantity := Round(l_rsv_rec.reservation_quantity,5);
8821 
8822            IF (l_orig_item_cache_index is NULL) THEN
8823               inv_reservation_util_pvt.search_item_cache
8824                 (
8825                   x_return_status      => l_return_status
8826                  ,p_inventory_item_id  => l_rsv_rec.inventory_item_id
8827                  ,p_organization_id    => l_rsv_rec.organization_id
8828                  ,x_index              => l_orig_item_cache_index
8829                  );
8830 
8831               IF (l_return_status = fnd_api.g_ret_sts_error) THEN
8832                  RAISE fnd_api.g_exc_error;
8833               End If;
8834 
8835               IF (l_return_status = fnd_api.g_ret_sts_unexp_error) THEN
8836                  RAISE fnd_api.g_exc_unexpected_error;
8837               END IF;
8838 
8839               IF (l_orig_item_cache_index IS NULL) THEN
8840                  OPEN c_item(l_rsv_rec.inventory_item_id);
8841                  FETCH c_item into l_item_rec;
8842                  CLOSE c_item;
8843 
8844                  inv_reservation_util_pvt.add_item_cache
8845                   (
8846                    x_return_status              => l_return_status
8847                   ,p_item_record                => l_item_rec
8848                   ,x_index                      => l_orig_item_cache_index
8849                   );
8850 
8851                  IF (l_return_status = fnd_api.g_ret_sts_error) THEN
8852                    RAISE fnd_api.g_exc_error;
8853                  END IF;
8854 
8855                  IF (l_return_status = fnd_api.g_ret_sts_unexp_error) THEN
8856                    RAISE fnd_api.g_exc_unexpected_error;
8857                  END IF;
8858               END IF;
8859            END IF;
8860 
8861 	   IF is_dual_control(l_orig_item_cache_index) THEN                          -- INVCONV
8862 	      l_dual_control_flag := 'Y';
8863 	      l_rsv_rec.secondary_reservation_quantity :=
8864 		Round(l_rsv_rec.secondary_reservation_quantity,5);                    -- INVCONV
8865 	   END IF;                                                                   -- INVCONV
8866 
8867            IF l_serial_number.COUNT > 0 THEN
8868               l_rsv_rec.serial_reservation_quantity := l_serial_number.COUNT;
8869               IF (l_rsv_rec.serial_reservation_quantity > l_rsv_rec.primary_reservation_quantity) THEN
8870                  fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
8871                  fnd_msg_pub.ADD;
8872                  RAISE fnd_api.g_exc_error;
8873               END IF;
8874            END IF;
8875 
8876 	   IF (l_debug = 1) THEN
8877 	      debug_print(' Create: Before inserting record');
8878 	      debug_print(' After rounding reservation = ' || l_reservation_id);
8879 	      debug_print(' After rounding reservation qty = ' || l_rsv_rec.reservation_quantity);
8880 	      debug_print(' After rounding reservation pri qty = ' || l_rsv_rec.primary_reservation_quantity);
8881 	      debug_print(' After rounding reservation sec qty = ' || l_rsv_rec.secondary_reservation_quantity); -- INVCONV
8882               debug_print(' serial reservation qty = ' || l_rsv_rec.serial_reservation_quantity);
8883 	   END IF;
8884 
8885 	   -- INVCONV - Upgrade call to incorporate secondaries
8886 	   mtl_reservations_pkg.insert_row
8887 	     (
8888 	      x_rowid                       => l_rowid
8889 	      , x_reservation_id             => l_reservation_id
8890 	      , x_requirement_date           => l_rsv_rec.requirement_date
8891 	      , x_organization_id            => l_rsv_rec.organization_id
8892 	      , x_inventory_item_id          => l_rsv_rec.inventory_item_id
8893 	      , x_demand_source_type_id      => l_rsv_rec.demand_source_type_id
8894 	      , x_demand_source_name         => l_rsv_rec.demand_source_name
8895 	      , x_demand_source_header_id    => l_rsv_rec.demand_source_header_id
8896 	      , x_demand_source_line_id      => l_rsv_rec.demand_source_line_id
8897 	      , x_demand_source_delivery     => l_rsv_rec.demand_source_delivery
8898 	      , x_primary_uom_code           => l_rsv_rec.primary_uom_code
8899 	      , x_primary_uom_id             => l_rsv_rec.primary_uom_id
8900 	      , x_secondary_uom_code         => l_rsv_rec.secondary_uom_code
8901 	      , x_secondary_uom_id           => l_rsv_rec.secondary_uom_id
8902 	      , x_reservation_uom_code       => l_rsv_rec.reservation_uom_code
8903 	     , x_reservation_uom_id         => l_rsv_rec.reservation_uom_id
8904 	     , x_reservation_quantity       => l_rsv_rec.reservation_quantity
8905 	     , x_primary_reservation_quantity=> l_rsv_rec.primary_reservation_quantity
8906 	     , x_second_reservation_quantity=> l_rsv_rec.secondary_reservation_quantity
8907 	     , x_detailed_quantity          => l_rsv_rec.detailed_quantity
8908 	     , x_secondary_detailed_quantity=> l_rsv_rec.secondary_detailed_quantity
8909 	     , x_autodetail_group_id        => l_rsv_rec.autodetail_group_id
8910 	     , x_external_source_code       => l_rsv_rec.external_source_code
8911 	     , x_external_source_line_id    => l_rsv_rec.external_source_line_id
8912 	     , x_supply_source_type_id      => l_rsv_rec.supply_source_type_id
8913 	     , x_supply_source_header_id    => l_rsv_rec.supply_source_header_id
8914 	     , x_supply_source_line_id      => l_rsv_rec.supply_source_line_id
8915 	     , x_supply_source_line_detail  => l_rsv_rec.supply_source_line_detail
8916 	     , x_supply_source_name         => l_rsv_rec.supply_source_name
8917 	     , x_revision                   => l_rsv_rec.revision
8918 	     , x_subinventory_code          => l_rsv_rec.subinventory_code
8919 	     , x_subinventory_id            => l_rsv_rec.subinventory_id
8920 	     , x_locator_id                 => l_rsv_rec.locator_id
8921 	     , x_lot_number                 => l_rsv_rec.lot_number
8922 	     , x_lot_number_id              => l_rsv_rec.lot_number_id
8923 	     , x_serial_number              => NULL
8924 	     , x_serial_number_id           => NULL
8925 	     , x_partial_quantities_allowed => NULL
8926 	     , x_auto_detailed              => NULL
8927 	     , x_pick_slip_number           => l_rsv_rec.pick_slip_number
8928 	     , x_lpn_id                     => l_rsv_rec.lpn_id
8929 	     , x_last_update_date           => l_date
8930 	     , x_last_updated_by            => l_user_id
8931 	     , x_creation_date              => l_date
8932 	     , x_created_by                 => l_user_id
8933 	     , x_last_update_login          => l_login_id
8934 	     , x_request_id                 => l_request_id
8935 	     , x_program_application_id     => l_prog_appl_id
8936 	     , x_program_id                 => l_program_id
8937 	     , x_program_update_date        => l_date
8938 	     , x_attribute_category         => l_rsv_rec.attribute_category
8939 	     , x_attribute1                 => l_rsv_rec.attribute1
8940 	     , x_attribute2                 => l_rsv_rec.attribute2
8941 	     , x_attribute3                 => l_rsv_rec.attribute3
8942 	     , x_attribute4                 => l_rsv_rec.attribute4
8943 	     , x_attribute5                 => l_rsv_rec.attribute5
8944 	     , x_attribute6                 => l_rsv_rec.attribute6
8945 	     , x_attribute7                 => l_rsv_rec.attribute7
8946 	     , x_attribute8                 => l_rsv_rec.attribute8
8947 	     , x_attribute9                 => l_rsv_rec.attribute9
8948 	     , x_attribute10                => l_rsv_rec.attribute10
8949 	     , x_attribute11                => l_rsv_rec.attribute11
8950 	     , x_attribute12                => l_rsv_rec.attribute12
8951 	     , x_attribute13                => l_rsv_rec.attribute13
8952 	     , x_attribute14                => l_rsv_rec.attribute14
8953 	     , x_attribute15                => l_rsv_rec.attribute15
8954 	     , x_ship_ready_flag            => l_rsv_rec.ship_ready_flag
8955 	     , x_staged_flag                => l_rsv_rec.staged_flag
8956 	     /**** {{ R12 Enhanced reservations code changes }}****/
8957 	     , x_crossdock_flag             => l_rsv_rec.crossdock_flag
8958 	     , x_crossdock_criteria_id      => l_rsv_rec.crossdock_criteria_id
8959 	     , x_demand_source_line_detail  => l_rsv_rec.demand_source_line_detail
8960 	     , x_serial_reservation_quantity => l_rsv_rec.serial_reservation_quantity
8961 	     , x_supply_receipt_date        => l_rsv_rec.supply_receipt_date
8962 	     , x_demand_ship_date             => l_rsv_rec.demand_ship_date
8963 	     , x_project_id                   => l_rsv_rec.project_id
8964 	     , x_task_id                      => l_rsv_rec.task_id
8965 	     , x_orig_supply_type_id   => l_rsv_rec.supply_source_type_id
8966 	     , x_orig_supply_header_id => l_rsv_rec.supply_source_header_id
8967 	     , x_orig_supply_line_id     => l_rsv_rec.supply_source_line_id
8968 	     , x_orig_supply_line_detail => l_rsv_rec.supply_source_line_detail
8969 	     , x_orig_demand_type_id     => l_rsv_rec.demand_source_type_id
8970 	     , x_orig_demand_header_id   => l_rsv_rec.demand_source_header_id
8971 	     , x_orig_demand_line_id     => l_rsv_rec.demand_source_line_id
8972 	     , x_orig_demand_line_detail => l_rsv_rec.demand_source_line_detail
8973 	     /*** End R12 ***/
8974 	     );
8975 
8976   	   IF (l_debug = 1) THEN
8977 		   debug_print(' After call to insert_row reservation_id = ' || l_reservation_id);
8978 	   END IF;
8979 	   IF (l_debug = 1) THEN
8980 	      debug_print('before sync ' || l_return_status);
8981 	   END IF;
8982 	   -- for data sync b/w mtl_demand and mtl_reservations
8983 	   inv_rsv_synch.for_insert(p_reservation_id => l_reservation_id, x_return_status => l_return_status, x_msg_count => x_msg_count, x_msg_data => x_msg_data);
8984 
8985 	   IF (l_debug = 1) THEN
8986 	      debug_print('After sync ' || l_return_status);
8987 	   END IF;
8988 
8989 	   IF l_return_status = fnd_api.g_ret_sts_error THEN
8990 	      RAISE fnd_api.g_exc_error;
8991 	   END IF;
8992 
8993 	   --
8994 	   IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8995 	      RAISE fnd_api.g_exc_unexpected_error;
8996 	   END IF;
8997 
8998 	   /**** {{ R12 Enhanced reservations code changes. Serial number
8999 	   -- changes for creating new reservations  }}****/
9000 
9001 	   IF (l_debug = 1) THEN
9002 	      debug_print('Serial number count' || l_serial_number.COUNT);
9003 	      debug_print('Original Serial number count' || p_serial_number.COUNT);
9004 	   END IF;
9005 
9006 	   IF l_serial_number.COUNT > 0 THEN
9007 
9008 	      IF (l_debug = 1) THEN
9009 		 debug_print('Inside serial loop' || l_serial_number.COUNT);
9010 	      END IF;
9011 
9012 	      FOR l_serial_index IN l_serial_number.first..l_serial_number.last
9013 		LOOP
9014 		   IF (l_debug = 1) THEN
9015 		      debug_print('reservation id' ||
9016 				  l_rsv_rec.reservation_id);
9017 		      debug_print('serial number' ||
9018 				  l_serial_number(l_serial_index).serial_number);
9019 		      debug_print('inventory item id' ||
9020 				  l_rsv_rec.inventory_item_id);
9021 		      debug_print('org id' ||
9022 				  l_rsv_rec.organization_id);
9023 
9024 		   END IF;
9025 
9026 		 BEGIN
9027 		    SELECT group_mark_id INTO l_group_mark_id FROM
9028 		      mtl_serial_numbers WHERE
9029 		      serial_number = l_serial_number(l_serial_index).serial_number AND
9030 		      inventory_item_id = l_rsv_rec.inventory_item_id AND
9031 		      current_organization_id = l_rsv_rec.organization_id;
9032 		    EXCEPTION
9033 		    WHEN no_data_found THEN
9034 
9035 		       IF (l_debug = 1) THEN
9036 			  debug_print('Errow while selecting the serial number. serial Number ' || l_serial_number(l_serial_index).serial_number);
9037 		       END IF;
9038 		       fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
9039 		       fnd_msg_pub.ADD;
9040 		       RAISE fnd_api.g_exc_error;
9041 		 END;
9042 
9043 		 IF (l_group_mark_id IS NOT NULL)  AND (l_group_mark_id <> -1)THEN
9044 		    IF (l_debug = 1) THEN
9045 		       debug_print('Group Mark Id is not null for serial ' || l_serial_number(l_serial_index).serial_number);
9046 		    END IF;
9047 		    fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
9048 		    fnd_msg_pub.ADD;
9049 		    RAISE fnd_api.g_exc_error;
9050 		 END IF;
9051 
9052 		 BEGIN
9053 		    UPDATE mtl_serial_numbers SET reservation_id =
9054 		      l_reservation_id, group_mark_id =
9055 		      l_reservation_id WHERE
9056 		      serial_number = l_serial_number(l_serial_index).serial_number AND
9057 		      inventory_item_id = l_rsv_rec.inventory_item_id AND
9058 		      current_organization_id = l_rsv_rec.organization_id;
9059 
9060 		 EXCEPTION
9061 		    WHEN no_data_found THEN
9062 
9063 		       IF (l_debug = 1) THEN
9064 			  debug_print('Errow while updating the serial number. serial Number ' || l_serial_number(l_serial_index).serial_number || ' Reservation Id : ' || l_rsv_rec.reservation_id);
9065 		       END IF;
9066 		       fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
9067 		       fnd_msg_pub.ADD;
9068 		       RAISE fnd_api.g_exc_error;
9069 		 END;
9070 		END LOOP;
9071 
9072 	   END IF;
9073 
9074 	   /*** End R12 ***/
9075 	   --
9076 	   -- Post Insert CTO Validation
9077 	   IF l_rsv_rec.demand_source_type_id IN (inv_reservation_global.g_source_type_oe, inv_reservation_global.g_source_type_internal_ord, inv_reservation_global.g_source_type_rma) THEN
9078 	      --
9079 	      cto_workflow_api_pk.wf_update_after_inv_reserv(p_order_line_id => l_rsv_rec.demand_source_line_id, x_return_status => l_return_status, x_msg_count => x_msg_count, x_msg_data => x_msg_data);
9080 
9081 	      IF (l_debug = 1) THEN
9082 		 debug_print('After post CTO validation ' || l_return_status);
9083 	      END IF;
9084 	      --
9085 	      IF l_return_status = fnd_api.g_ret_sts_error THEN
9086 		 RAISE fnd_api.g_exc_error;
9087 	      END IF;
9088 
9089 	      --
9090 	      IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9091 		 RAISE fnd_api.g_exc_unexpected_error;
9092 	      END IF;
9093 	   END IF;
9094 
9095 	   insert_rsv_temp
9096 	     (
9097 	      p_organization_id            => l_rsv_rec.organization_id
9098 	      , p_inventory_item_id          => l_rsv_rec.inventory_item_id
9099 	      , p_primary_reservation_quantity=> l_rsv_rec.primary_reservation_quantity
9100 	      , p_tree_id                    => l_tree_id
9101 	      , p_reservation_id             => l_reservation_id
9102 	      , x_return_status              => l_return_status
9103 	      , p_demand_source_line_id      => l_rsv_rec.demand_source_line_id
9104 	      , p_demand_source_header_id    => l_rsv_rec.demand_source_header_id
9105 	      , p_demand_source_name         => l_rsv_rec.demand_source_name
9106 	      );
9107 
9108 	   IF (l_debug = 1) THEN
9109 	      debug_print('After insert into rsv temp ' || l_return_status);
9110 	   END IF;
9111 
9112 	   IF l_return_status = fnd_api.g_ret_sts_error THEN
9113 	      RAISE fnd_api.g_exc_error;
9114 	   END IF;
9115 
9116 	   --
9117 	   IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9118 	      RAISE fnd_api.g_exc_unexpected_error;
9119 	   END IF;
9120 
9121 	   --
9122 	   IF (l_debug = 1) THEN
9123 	      debug_print('Final status after create reservation API ' ||
9124 			  l_return_status);
9125 	      debug_print('qty reserved ' ||
9126 			  l_rsv_rec.primary_reservation_quantity);
9127 	      debug_print('secondary qty reserved ' ||
9128 			  l_rsv_rec.secondary_reservation_quantity);                  -- INVCONV
9129 	      debug_print('reservation id' ||
9130 			  l_reservation_id);
9131 
9132 	   END IF;
9133 
9134 	   /**** {{ R12 Enhanced reservations code changes. Should be
9135 	   -- releasing the locks. }} *****/
9136 	   IF l_lock_obtained THEN
9137 	      inv_reservation_lock_pvt.release_lock
9138 		(l_supply_lock_handle);
9139 	      inv_reservation_lock_pvt.release_lock
9140 		(l_demand_lock_handle);
9141 	   END IF;
9142 	   /*** End R12 ***/
9143 
9144 	   -- set output variables
9145 	   x_quantity_reserved  := l_rsv_rec.primary_reservation_quantity;
9146 
9147 	   IF l_dual_control_flag = 'Y' THEN                           -- INVCONV
9148 	      x_secondary_quantity_reserved := l_rsv_rec.secondary_reservation_quantity; -- INVCONV
9149 	   END IF;                                                                    -- INVCONV
9150 
9151 	   x_reservation_id     := l_reservation_id;
9152 	   --
9153 	   x_return_status      := l_return_status;
9154 	   /**** {{ R12 Enhanced reservations code changes. Serial number
9155 	   -- changes for creating new reservations  }}****/
9156 	   x_serial_number := l_serial_number;
9157 	   /*** End R12 ***/
9158 	   --
9159 
9160 	   /**** {{ R12 Enhanced reservations code changes }}****/
9161     END IF; -- If not l_rsv is true
9162     -- Do this only if the l_rsv_is set to false. Otherwise, we
9163     -- would have updated the record.
9164     /*** End R12 ***/
9165 
9166   EXCEPTION
9167      WHEN fnd_api.g_exc_error THEN
9168 	ROLLBACK TO create_reservation_sa;
9169 	x_return_status  := fnd_api.g_ret_sts_error;
9170 	/**** {{ R12 Enhanced reservations code changes. Should be
9171 	-- releasing the locks. }} *****/
9172 	IF l_lock_obtained THEN
9173 	   inv_reservation_lock_pvt.release_lock
9174 	     (l_supply_lock_handle);
9175 	   inv_reservation_lock_pvt.release_lock
9176 	     (l_demand_lock_handle);
9177 	END IF;
9178 	/*** End R12 ***/
9179 	--  Get message count and data
9180 	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
9181      WHEN fnd_api.g_exc_unexpected_error THEN
9182 	ROLLBACK TO create_reservation_sa;
9183 	x_return_status  := fnd_api.g_ret_sts_unexp_error;
9184 	/**** {{ R12 Enhanced reservations code changes. Should be
9185 	-- releasing the locks. }} *****/
9186 	IF l_lock_obtained THEN
9187 	   inv_reservation_lock_pvt.release_lock
9188 	     (l_supply_lock_handle);
9189 	   inv_reservation_lock_pvt.release_lock
9190 	     (l_demand_lock_handle);
9191 	END IF;
9192 	/*** End R12 ***/
9193 	--  Get message count and data
9194 	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
9195      WHEN OTHERS THEN
9196 	ROLLBACK TO create_reservation_sa;
9197 	x_return_status  := fnd_api.g_ret_sts_unexp_error;
9198 	/**** {{ R12 Enhanced reservations code changes. Should be
9199 	-- releasing the locks. }} *****/
9200 	IF l_lock_obtained THEN
9201 	   inv_reservation_lock_pvt.release_lock
9202 	     (l_supply_lock_handle);
9203 	   inv_reservation_lock_pvt.release_lock
9204 	     (l_demand_lock_handle);
9205 	END IF;
9206 	/*** End R12 ***/
9207 	IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
9208 	   fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
9209 	END IF;
9210 
9211 	--  Get message count and data
9212 	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
9213   END create_reservation;
9214 
9215 
9216   PROCEDURE update_reservation
9217     (
9218      p_api_version_number            IN     NUMBER
9219      , p_init_msg_lst                  IN     VARCHAR2 DEFAULT fnd_api.g_false
9220      , x_return_status                 OUT    NOCOPY VARCHAR2
9221      , x_msg_count                     OUT    NOCOPY NUMBER
9222      , x_msg_data                      OUT    NOCOPY VARCHAR2
9223      , p_original_rsv_rec              IN     inv_reservation_global.mtl_reservation_rec_type
9224      , p_to_rsv_rec                    IN     inv_reservation_global.mtl_reservation_rec_type
9225      , p_original_serial_number        IN     inv_reservation_global.serial_number_tbl_type
9226      , p_to_serial_number              IN     inv_reservation_global.serial_number_tbl_type
9227      , p_validation_flag               IN     VARCHAR2 DEFAULT fnd_api.g_true
9228      , p_check_availability            IN     VARCHAR2 DEFAULT fnd_api.g_false
9229      , p_over_reservation_flag         IN  NUMBER DEFAULT 0
9230      ) IS
9231 
9232 
9233      l_api_version_number CONSTANT NUMBER :=  1.0;
9234      l_api_name           CONSTANT VARCHAR2(30) := 'Update_Reservation';
9235      l_return_status      VARCHAR2(1) :=  fnd_api.g_ret_sts_success;
9236      l_quantity_reserved  NUMBER;
9237      l_secondary_quantity_reserved NUMBER;          -- INVCONV
9238      l_debug NUMBER;
9239 
9240     BEGIN
9241        --  Standard call to check for call compatibility
9242        IF NOT fnd_api.compatible_api_call
9243 	 (l_api_version_number
9244 	  , p_api_version_number
9245 	  , l_api_name
9246 	  , G_PKG_NAME
9247 	  ) THEN
9248 	  RAISE fnd_api.g_exc_unexpected_error;
9249        END IF;
9250 
9251        --  Initialize message list.
9252        IF fnd_api.to_boolean(p_init_msg_lst) THEN
9253 	  fnd_msg_pub.initialize;
9254        END IF;
9255 
9256        -- Use cache to get value for l_debug
9257        IF g_is_pickrelease_set IS NULL THEN
9258           g_is_pickrelease_set := 2;
9259           IF INV_CACHE.is_pickrelease THEN
9260              g_is_pickrelease_set := 1;
9261           END IF;
9262        END IF;
9263        IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
9264           g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
9265        END IF;
9266 
9267        l_debug := g_debug;
9268 
9269        IF l_debug=1 THEN
9270 	  debug_print('Calling the overloaded procedure update_reservation');
9271        END IF;
9272 
9273        -- INVCONV Upgrade call to incorporate secondary_quantity_reserved
9274        inv_reservation_pvt.update_reservation
9275 	 (p_api_version_number          => 1.0,
9276 	  p_init_msg_lst                => fnd_api.g_false,
9277 	  x_return_status               => l_return_status,
9278 	  x_msg_count                   => x_msg_count,
9279 	  x_msg_data                    => x_msg_data,
9280 	  x_quantity_reserved           => l_quantity_reserved,
9281 	  x_secondary_quantity_reserved => l_secondary_quantity_reserved,
9282 	  p_original_rsv_rec            => p_original_rsv_rec,
9283 	  p_to_rsv_rec                  => p_to_rsv_rec,
9284 	  p_original_serial_number      => p_original_serial_number ,
9285 	  p_to_serial_number            => p_to_serial_number,
9286 	  p_validation_flag             => p_validation_flag,
9287 	  p_partial_reservation_flag    => fnd_api.g_false,
9288 	  p_check_availability          => p_check_availability,
9289 	  p_over_reservation_flag       => p_over_reservation_flag
9290 	  );
9291 
9292 
9293        IF (l_debug=1) THEN
9294 	  debug_print ('Return Status after updating reservations '||l_return_status);
9295        END IF;
9296 
9297        IF l_return_status = fnd_api.g_ret_sts_error THEN
9298 
9299 	  IF l_debug=1 THEN
9300 	     debug_print('Raising expected error'||l_return_status);
9301 	  END IF;
9302 
9303 	  RAISE fnd_api.g_exc_error;
9304 
9305 	ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9306 
9307 	  IF l_debug=1 THEN
9308 	     debug_print('Raising Unexpected error'||l_return_status);
9309 	  END IF;
9310 
9311 	  RAISE fnd_api.g_exc_unexpected_error;
9312        END IF;
9313 
9314 
9315        x_return_status := l_return_status;
9316 
9317     EXCEPTION
9318 
9319        WHEN fnd_api.g_exc_error THEN
9320 	  x_return_status := fnd_api.g_ret_sts_error;
9321 	  --  Get message count and data
9322 	  fnd_msg_pub.count_and_get
9323 	    (  p_count => x_msg_count
9324 	       , p_data  => x_msg_data
9325 	       );
9326 
9327        WHEN fnd_api.g_exc_unexpected_error THEN
9328 	  x_return_status := fnd_api.g_ret_sts_unexp_error ;
9329 
9330           --  Get message count and data
9331 	  fnd_msg_pub.count_and_get
9332 	    (  p_count  => x_msg_count
9333 	       , p_data   => x_msg_data
9334 	       );
9335 
9336        WHEN OTHERS THEN
9337 	  x_return_status := fnd_api.g_ret_sts_unexp_error ;
9338 
9339 	  IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
9340 	    THEN
9341 	     fnd_msg_pub.add_exc_msg
9342 	       (  g_pkg_name
9343 		  , l_api_name
9344 		  );
9345 	  END IF;
9346 
9347 	  --  Get message count and data
9348 	  fnd_msg_pub.count_and_get
9349 	    (  p_count  => x_msg_count
9350 	       , p_data   => x_msg_data
9351 	       );
9352 
9353     END update_reservation;
9354 
9355     --overloaded procedure
9356     --
9357     -- INVCONV incorporate parameter x_secondary_quantity_reserved
9358     PROCEDURE update_reservation
9359       (
9360        p_api_version_number              IN     NUMBER
9361        , p_init_msg_lst                  IN     VARCHAR2 DEFAULT fnd_api.g_false
9362        , x_return_status                 OUT    NOCOPY VARCHAR2
9363        , x_msg_count                     OUT    NOCOPY NUMBER
9364        , x_msg_data                      OUT    NOCOPY VARCHAR2
9365        , x_quantity_reserved             OUT    NOCOPY NUMBER
9366        , x_secondary_quantity_reserved   OUT    NOCOPY NUMBER
9367        , p_original_rsv_rec              IN     inv_reservation_global.mtl_reservation_rec_type
9368        , p_to_rsv_rec                    IN     inv_reservation_global.mtl_reservation_rec_type
9369        , p_original_serial_number        IN     inv_reservation_global.serial_number_tbl_type
9370        , p_to_serial_number              IN     inv_reservation_global.serial_number_tbl_type
9371        , p_validation_flag               IN     VARCHAR2 DEFAULT fnd_api.g_true
9372        , p_partial_reservation_flag      IN     VARCHAR2 DEFAULT fnd_api.g_false
9373        , p_check_availability            IN     VARCHAR2 DEFAULT fnd_api.g_false
9374        , p_over_reservation_flag         IN  NUMBER DEFAULT 0
9375       ) IS
9376 	 l_api_version_number  CONSTANT NUMBER        := 1.0;
9377 	 l_api_name            CONSTANT VARCHAR2(30)  := 'Update_Reservation';
9378 	 l_return_status  VARCHAR2(1)            := fnd_api.g_ret_sts_success;
9379 	 l_miss_num  NUMBER                 := fnd_api.g_miss_num;
9380 	 l_miss_char VARCHAR2(1)            := fnd_api.g_miss_char;
9381 	 l_original_rsv_rec inv_reservation_global.mtl_reservation_rec_type;
9382 	 l_to_rsv_rec   inv_reservation_global.mtl_reservation_rec_type;
9383 	 l_orig_rsv_tbl inv_reservation_global.mtl_reservation_tbl_type;
9384 	 l_orig_rsv_tbl_count  NUMBER;
9385 	 l_to_rsv_tbl  inv_reservation_global.mtl_reservation_tbl_type;
9386 	 --
9387 	 l_to_rsv_tbl_count            NUMBER;
9388 	 l_tree_id1                    INTEGER;
9389 	 l_tree_id2                    INTEGER;
9390 	 l_primary_quantity_reserved   NUMBER;
9391 	 l_primary_uom_code            VARCHAR2(3);
9392 	 l_orig_item_cache_index       INTEGER                                         := NULL;
9393 	 l_orig_org_cache_index        INTEGER                                         := NULL;
9394 	 l_orig_demand_cache_index     INTEGER                                         := NULL;
9395 	 l_orig_supply_cache_index     INTEGER                                         := NULL;
9396 	 l_orig_sub_cache_index        INTEGER                                         := NULL;
9397 	 l_to_item_cache_index         INTEGER                                         := NULL;
9398 	 l_to_org_cache_index          INTEGER                                         := NULL;
9399 	 l_to_demand_cache_index       INTEGER                                         := NULL;
9400 	 l_to_supply_cache_index       INTEGER                                         := NULL;
9401 	 l_to_sub_cache_index          INTEGER                                         := NULL;
9402 	 l_date                        DATE;
9403 	 l_user_id                     NUMBER;
9404 	 l_request_id                  NUMBER;
9405 	 l_login_id                    NUMBER;
9406 	 l_prog_appl_id                NUMBER;
9407 	 l_program_id                  NUMBER;
9408 	 l_error_code                  NUMBER;
9409 	 l_debug number;
9410 	 l_quantity_reserved           NUMBER;
9411 	 l_secondary_quantity_reserved NUMBER;                                        -- INVCONV
9412 	 l_lot_divisible_flag          VARCHAR2(1)      := 'Y' ;                      -- INVCONV
9413 	 l_dual_control_flag           VARCHAR2(1)      := 'N' ;
9414 	 -- INVCONV
9415 	 /**** {{ R12 Enhanced reservations code changes }}****/
9416 	 l_original_serial_number inv_reservation_global.serial_number_tbl_type;
9417 	 l_to_serial_number inv_reservation_global.serial_number_tbl_type;
9418 	 l_qty_available NUMBER := 0;
9419 	 l_serial_number_table inv_reservation_global.serial_number_tbl_type;
9420 	 l_dummy_serial_array  inv_reservation_global.serial_number_tbl_type;
9421 	 l_dummy_rsv_rec inv_reservation_global.mtl_reservation_rec_type;
9422 	 l_serials_tobe_unreserved NUMBER;
9423 	 l_total_serials_reserved NUMBER;
9424 	 l_reservation_id NUMBER;
9425 	 l_original_serial_count NUMBER;
9426 	 l_to_serial_count NUMBER;
9427 	 l_supply_lock_handle varchar2(128);
9428 	 l_demand_lock_handle varchar2(128);
9429 	 l_lock_status NUMBER;
9430          l_reservable_qty NUMBER;
9431 	 l_booked_flag VARCHAR2(1) := 'N';
9432 	 l_open_flag	VARCHAR2(1);
9433 	 l_group_mark_id NUMBER := NULL;
9434 	 l_lock_obtained BOOLEAN := FALSE;
9435 	 l_pjm_enabled NUMBER;
9436 	 l_project_id NUMBER;
9437 	 l_task_id NUMBER;
9438 	 l_supply_source_type_id NUMBER;
9439 	 /*** End R12 ***/
9440 
9441     BEGIN
9442 
9443      /*Bug 4700706. Moved the following statement up as otherwise when it was somewhere in the middle of  the
9444        procedure and if any error occurs before the definition of the savepoint , it would go to the EXCEPTION
9445        block and there we have 'Rollback to update_reservation_sa' statement which was causing issue.
9446      */
9447      SAVEPOINT update_reservation_sa;
9448 
9449        -- Use cache to get value for l_debug
9450        IF g_is_pickrelease_set IS NULL THEN
9451 	  g_is_pickrelease_set := 2;
9452         IF INV_CACHE.is_pickrelease THEN
9453            g_is_pickrelease_set := 1;
9454         END IF;
9455      END IF;
9456      IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
9457         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
9458      END IF;
9459 
9460      l_debug := g_debug;
9461 
9462      IF (l_debug = 1) THEN
9463         debug_print('Inside update reservation...');
9464      END IF;
9465 
9466 
9467      --  Standard call to check for call compatibility
9468     IF NOT fnd_api.compatible_api_call(l_api_version_number, p_api_version_number, l_api_name, g_pkg_name) THEN
9469       RAISE fnd_api.g_exc_unexpected_error;
9470     END IF;
9471 
9472     IF (l_debug = 1) THEN
9473        debug_print('The original reservation record: ');
9474     END IF;
9475 
9476     print_rsv_rec(p_original_rsv_rec);
9477 
9478     IF (l_debug = 1) THEN
9479        debug_print('The to reservation record: ');
9480     END IF;
9481 
9482     print_rsv_rec(p_to_rsv_rec);
9483 
9484     -- Bug #2819700
9485     -- Adding an extra check to make sure that the update reservations does
9486     -- not update the existing record to negative. Raise an exception.
9487     IF (l_debug = 1) THEN
9488       debug_print('Primary_reservation_qty before inserting (update)= '
9489 		              || To_char(p_to_rsv_rec.primary_reservation_quantity));
9490       debug_print('Secondary_reservation_qty before inserting (update)= '
9491                               || To_char(p_to_rsv_rec.secondary_reservation_quantity));  -- INVCONV
9492       debug_print('Reservation_qty before inserting (update)= '
9493 		              || To_char(p_to_rsv_rec.reservation_quantity));
9494     END IF;
9495 
9496     IF ((NVL(p_to_rsv_rec.reservation_quantity,0) < 0) OR
9497 	      (NVL(p_to_rsv_rec.primary_reservation_quantity,0) < 0) ) THEN
9498       fnd_message.set_name('INV', 'INV-INVALID RESERVATION QTY');
9499       fnd_msg_pub.ADD;
9500       RAISE fnd_api.g_exc_error;
9501     END IF;
9502 
9503     -- INVCONV BEGIN
9504     IF (NVL(p_to_rsv_rec.secondary_reservation_quantity,0) < 0) THEN
9505       fnd_message.set_name('INV', 'INV-INVALID NEGATIVE SECONDARY QTY');    -- INVCONV NEW MESSAGE
9506       fnd_msg_pub.ADD;
9507       RAISE fnd_api.g_exc_error;
9508     END IF;
9509     -- INVCONV END
9510 
9511     /**** {{ R12 Enhanced reservations code changes. Initializing orig parameters }}****/
9512 
9513     -- Set the original columns to g_miss_xxx as the user should not be
9514     -- setting these values. Do not query by them
9515     l_original_rsv_rec := p_original_rsv_rec;
9516 
9517     l_original_rsv_rec.orig_supply_source_type_id := fnd_api.g_miss_num;
9518     l_original_rsv_rec.orig_supply_source_header_id := fnd_api.g_miss_num;
9519     l_original_rsv_rec.orig_supply_source_line_id := fnd_api.g_miss_num;
9520     l_original_rsv_rec.orig_supply_source_line_detail := fnd_api.g_miss_num;
9521     l_original_rsv_rec.orig_demand_source_type_id := fnd_api.g_miss_num;
9522     l_original_rsv_rec.orig_demand_source_header_id := fnd_api.g_miss_num;
9523     l_original_rsv_rec.orig_demand_source_line_id := fnd_api.g_miss_num;
9524     l_original_rsv_rec.orig_demand_source_line_detail := fnd_api.g_miss_num;
9525 
9526     /*** End R12 ***/
9527 
9528     --
9529     -- if the update to quantity is 0, call delete instead
9530     IF p_to_rsv_rec.primary_reservation_quantity = 0 OR
9531       (p_to_rsv_rec.reservation_quantity = 0 AND
9532        (p_to_rsv_rec.primary_reservation_quantity IS NULL OR p_to_rsv_rec.primary_reservation_quantity = fnd_api.g_miss_num))
9533 	 THEN
9534 
9535        delete_reservation
9536 	 (
9537 	  p_api_version_number         => 1.0
9538 	  , p_init_msg_lst             => p_init_msg_lst
9539 	  , x_return_status            => l_return_status
9540 	  , x_msg_count                => x_msg_count
9541 	  , x_msg_data                 => x_msg_data
9542 	  , p_rsv_rec                  => p_original_rsv_rec
9543 	  , p_original_serial_number   => p_original_serial_number
9544 	  , p_validation_flag          => p_validation_flag
9545 	  );
9546 
9547        IF l_return_status = fnd_api.g_ret_sts_error THEN
9548 	  RAISE fnd_api.g_exc_error;
9549        END IF;
9550 
9551        --
9552        IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9553 	  RAISE fnd_api.g_exc_unexpected_error;
9554        END IF;
9555 
9556        --
9557        x_return_status  := l_return_status;
9558        RETURN;
9559     END IF;
9560 
9561     --
9562     --  Initialize message list.
9563     IF fnd_api.to_boolean(p_init_msg_lst) THEN
9564        fnd_msg_pub.initialize;
9565     END IF;
9566 
9567     --
9568     --
9569     -- search for the from row
9570     IF (l_debug = 1) THEN
9571        debug_print('Querying Reservation for the from record');
9572     END IF;
9573     /**** {{ R12 Enhanced reservations code changes }}****/
9574     query_reservation
9575       (
9576        p_api_version_number         => 1.0
9577        , p_init_msg_lst               => fnd_api.g_false
9578        , x_return_status              => l_return_status
9579        , x_msg_count                  => x_msg_count
9580        , x_msg_data                   => x_msg_data
9581        , p_query_input                => l_original_rsv_rec
9582        , p_lock_records               => fnd_api.g_true
9583        , x_mtl_reservation_tbl        => l_orig_rsv_tbl
9584        , x_mtl_reservation_tbl_count  => l_orig_rsv_tbl_count
9585        , x_error_code                 => l_error_code
9586        );
9587     /*** End R12 ***/
9588     IF l_return_status = fnd_api.g_ret_sts_error THEN
9589        IF (l_debug = 1) THEN
9590 	  debug_print('Query Reservation returned error');
9591        END IF;
9592        RAISE fnd_api.g_exc_error;
9593     END IF;
9594 
9595     --
9596     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9597        IF (l_debug = 1) THEN
9598 	  debug_print('Query Reservation returned unexpected error');
9599        END IF;
9600        RAISE fnd_api.g_exc_unexpected_error;
9601     END IF;
9602 
9603     --
9604     IF l_orig_rsv_tbl_count = 0 THEN
9605        IF (l_debug = 1) THEN
9606 	  debug_print('Query Reservation returned no row');
9607        END IF;
9608        fnd_message.set_name('INV', 'INV-ROW NOT FOUND');
9609        fnd_msg_pub.ADD;
9610       RAISE fnd_api.g_exc_error;
9611     END IF;
9612 
9613     --
9614     IF l_orig_rsv_tbl_count > 1 THEN
9615        IF (l_debug = 1) THEN
9616 	  debug_print('Query Reservation returned more than one row');
9617        END IF;
9618        fnd_message.set_name('INV', 'INV-UPATE MORE THAN ONE RSV');
9619        fnd_msg_pub.ADD;
9620        RAISE fnd_api.g_exc_error;
9621     END IF;
9622 
9623     IF (l_debug = 1) THEN
9624        debug_print('Constructing to reservation row');
9625     END IF;
9626 
9627     construct_to_reservation_row(l_orig_rsv_tbl(1), p_to_rsv_rec, l_to_rsv_rec);
9628 
9629     IF (l_debug = 1) THEN
9630        debug_print('Constructed to reservation row');
9631     END IF;
9632 
9633     /**** {{ R12 Enhanced reservations code changes.Calling the reservation
9634     -- lock API to create a user-defined lock for non-inventory supplies }} *****/
9635     -- Bug 5199672: Should pass null to supply and demand line detail as
9636       -- we will have to lock the records at the document level and not at
9637       -- the line level. Also, for ASN, pass the source type as PO so that the
9638       -- the lock name would be the same as the PO's
9639 
9640     IF (l_to_rsv_rec.supply_source_type_id =
9641 	inv_reservation_global.g_source_type_asn) THEN
9642        l_supply_source_type_id :=
9643 	 inv_reservation_global.g_source_type_po;
9644      ELSE
9645        l_supply_source_type_id := l_to_rsv_rec.supply_source_type_id;
9646     END IF;
9647 
9648     IF (l_to_rsv_rec.supply_source_type_id <> inv_reservation_global.g_source_type_inv) THEN
9649        inv_reservation_lock_pvt.lock_supply_demand_record
9650 	 (p_organization_id => l_to_rsv_rec.organization_id
9651 	  ,p_inventory_item_id => l_to_rsv_rec.inventory_item_id
9652 	  ,p_source_type_id => l_supply_source_type_id
9653 	  ,p_source_header_id => l_to_rsv_rec.supply_source_header_id
9654 	  ,p_source_line_id =>  l_to_rsv_rec.supply_source_line_id
9655 	  ,p_source_line_detail => NULL
9656 	  ,x_lock_handle => l_supply_lock_handle
9657 	  ,x_lock_status => l_lock_status);
9658 
9659        IF l_lock_status = 0 THEN
9660 	  fnd_message.set_name('INV', 'INV_INVALID_LOCK');
9661 	  fnd_msg_pub.ADD;
9662 	  RAISE fnd_api.g_exc_error;
9663        END if;
9664 
9665        inv_reservation_lock_pvt.lock_supply_demand_record
9666 	 (p_organization_id => l_to_rsv_rec.organization_id
9667 	  ,p_inventory_item_id => l_to_rsv_rec.inventory_item_id
9668 	  ,p_source_type_id => l_to_rsv_rec.demand_source_type_id
9669 	  ,p_source_header_id => l_to_rsv_rec.demand_source_header_id
9670 	  ,p_source_line_id =>  l_to_rsv_rec.demand_source_line_id
9671 	  ,p_source_line_detail => NULL
9672 	  ,x_lock_handle => l_demand_lock_handle
9673 	  ,x_lock_status => l_lock_status);
9674 
9675        IF l_lock_status = 0 THEN
9676 	  fnd_message.set_name('INV', 'INV_INVALID_LOCK');
9677 	  fnd_msg_pub.ADD;
9678 	  RAISE fnd_api.g_exc_error;
9679        END if;
9680 
9681        l_lock_obtained := TRUE;
9682     END IF;
9683     /*** End R12 ***/
9684 
9685     -- here we might add some validation of input parameters
9686     -- for update api, the reservation id should not be changed
9687     IF l_orig_rsv_tbl(1).reservation_id <> l_to_rsv_rec.reservation_id THEN
9688        IF (l_debug = 1) THEN
9689 	  debug_print('Cannot update reservation ID');
9690        END IF;
9691        fnd_message.set_name('INV', 'CANNOT_UPDATE_RESERVATION_ID');
9692        fnd_msg_pub.ADD;
9693        RAISE fnd_api.g_exc_error;
9694     END IF;
9695 
9696     --
9697     IF l_orig_rsv_tbl(1).organization_id <> l_to_rsv_rec.organization_id THEN
9698        IF (l_debug = 1) THEN
9699 	  debug_print('Cannot update organization ID');
9700        END IF;
9701        fnd_message.set_name('INV', 'CANNOT_UPDATE_ORGANIZATION_ID');
9702        fnd_msg_pub.ADD;
9703        RAISE fnd_api.g_exc_error;
9704     END IF;
9705 
9706     --
9707     IF l_orig_rsv_tbl(1).inventory_item_id <> l_to_rsv_rec.inventory_item_id THEN
9708        IF (l_debug = 1) THEN
9709 	  debug_print('Cannot update Inventory Item ID');
9710        END IF;
9711        fnd_message.set_name('INV', 'CANNOT_UPDATE_INVENTORY_ITEM');
9712        fnd_msg_pub.ADD;
9713        RAISE fnd_api.g_exc_error;
9714     END IF;
9715 
9716     --
9717     -- convert quantity between primary uom and reservation uom
9718     convert_quantity(x_return_status => l_return_status, px_rsv_rec => l_to_rsv_rec);
9719 
9720     IF l_return_status = fnd_api.g_ret_sts_error THEN
9721        RAISE fnd_api.g_exc_error;
9722     END IF;
9723     --
9724     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9725        RAISE fnd_api.g_exc_unexpected_error;
9726     END IF;
9727 
9728     --
9729     -- if the caller does not specified reservation_id, l_to_rsv_rec will
9730     -- has the same reservation_id as l_orig_rsv_tbl(1) due to the way
9731     -- construct_to_reservation_row works.
9732     -- but we should set it to g_miss_num again
9733     -- otherwise query_reservation will use only the
9734     -- reservation_id to do the search.
9735     IF l_to_rsv_rec.reservation_id = l_orig_rsv_tbl(1).reservation_id THEN
9736        l_to_rsv_rec.reservation_id  := fnd_api.g_miss_num;
9737     END IF;
9738 
9739     /**** {{ R12 Enhanced reservations code changes }}****/
9740 
9741     -- Set the original columns to g_miss_xxx as the user should not be
9742     -- setting these values. Do not query by them
9743     l_to_rsv_rec.orig_supply_source_type_id := fnd_api.g_miss_num;
9744     l_to_rsv_rec.orig_supply_source_header_id := fnd_api.g_miss_num;
9745     l_to_rsv_rec.orig_supply_source_line_id := fnd_api.g_miss_num;
9746     l_to_rsv_rec.orig_supply_source_line_detail := fnd_api.g_miss_num;
9747     l_to_rsv_rec.orig_demand_source_type_id := fnd_api.g_miss_num;
9748     l_to_rsv_rec.orig_demand_source_header_id := fnd_api.g_miss_num;
9749     l_to_rsv_rec.orig_demand_source_line_id := fnd_api.g_miss_num;
9750     l_to_rsv_rec.orig_demand_source_line_detail := fnd_api.g_miss_num;
9751 
9752     IF (l_to_rsv_rec.project_id IS NULL)  THEN
9753        l_to_rsv_rec.project_id := fnd_api.g_miss_num;
9754     END IF;
9755     IF (l_to_rsv_rec.task_id IS NULL)  THEN
9756        l_to_rsv_rec.task_id := fnd_api.g_miss_num;
9757     END IF;
9758     /*** End R12 ***/
9759 
9760     IF (l_debug = 1) THEN
9761        debug_print('Querying reservatione for the to record');
9762     END IF;
9763 
9764     query_reservation
9765       (
9766        p_api_version_number           => 1.0
9767        , p_init_msg_lst               => fnd_api.g_false
9768        , x_return_status              => l_return_status
9769        , x_msg_count                  => x_msg_count
9770        , x_msg_data                   => x_msg_data
9771        , p_query_input                => l_to_rsv_rec
9772        , p_lock_records               => fnd_api.g_true
9773        , x_mtl_reservation_tbl        => l_to_rsv_tbl
9774        , x_mtl_reservation_tbl_count  => l_to_rsv_tbl_count
9775        , x_error_code                 => l_error_code
9776        );
9777     IF (l_debug = 1) THEN
9778        debug_print('Queried reservation');
9779     END IF;
9780 
9781     --
9782     IF l_return_status = fnd_api.g_ret_sts_error THEN
9783        IF (l_debug = 1) THEN
9784 	  debug_print('Query Reservation returned error');
9785        END IF;
9786        RAISE fnd_api.g_exc_error;
9787     END IF;
9788 
9789     --
9790     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9791        IF (l_debug = 1) THEN
9792 	  debug_print('Query Reservation returned unexpected error');
9793        END IF;
9794        RAISE fnd_api.g_exc_unexpected_error;
9795     END IF;
9796 
9797     --
9798     IF l_to_rsv_tbl_count > 1
9799       OR l_to_rsv_tbl_count > 0
9800       AND l_to_rsv_tbl(1).reservation_id <> l_orig_rsv_tbl(1).reservation_id THEN
9801        IF (l_debug = 1) THEN
9802 	  debug_print('Reservation target row exists');
9803        END IF;
9804        fnd_message.set_name('INV', 'INV-RSV TARGET ROW EXISTS');
9805        fnd_msg_pub.ADD;
9806        RAISE fnd_api.g_exc_error;
9807     END IF;
9808 
9809     /**** {{ R12 Enhanced reservations code changes }}****/
9810 
9811     -- Get the project and task for demands in OE, INT-ORD and RMA
9812     IF l_debug=1 THEN
9813        debug_print('Before Rsv rec project id: ' || l_to_rsv_rec.project_id);
9814        debug_print('Before Rsv rec task id: ' || l_to_rsv_rec.task_id);
9815     END IF;
9816 
9817     BEGIN
9818        SELECT project_reference_enabled
9819 	 INTO l_pjm_enabled
9820 	 FROM   mtl_parameters
9821 	 WHERE  organization_id = l_to_rsv_rec.organization_id;
9822     EXCEPTION
9823        WHEN no_data_found THEN
9824 	  IF l_debug=1 THEN
9825 	     debug_print('Cannot find the project and task information');
9826 	  END IF;
9827     END;
9828 
9829     IF (l_to_rsv_rec.demand_source_type_id IN
9830 	(inv_reservation_global.g_source_type_oe,
9831 	 inv_reservation_global.g_source_type_internal_ord,
9832 	 inv_reservation_global.g_source_type_rma)) AND
9833       (l_pjm_enabled = 1) THEN
9834 
9835        IF (l_to_rsv_rec.demand_source_line_id IS NOT NULL) AND
9836 	 (l_to_rsv_rec.demand_source_line_id <> fnd_api.g_miss_num)
9837 	 AND ((l_to_rsv_rec.project_id = fnd_api.g_miss_num) OR
9838 	      (l_to_rsv_rec.task_id = fnd_api.g_miss_num)) THEN
9839 	 BEGIN
9840 	    SELECT project_id, task_id INTO l_project_id, l_task_id
9841 	      FROM oe_order_lines_all WHERE
9842 	      line_id = l_to_rsv_rec.demand_source_line_id;
9843 	 EXCEPTION
9844 	    WHEN no_data_found THEN
9845 	       IF l_debug=1 THEN
9846 		  debug_print('Cannot find the project and task information');
9847 	       END IF;
9848 	 END;
9849 
9850 	 IF (l_to_rsv_rec.project_id = fnd_api.g_miss_num) THEN
9851 	    IF (l_project_id IS NOT NULL) THEN
9852 	       l_to_rsv_rec.project_id := l_project_id;
9853 	     ELSE
9854 	       l_to_rsv_rec.project_id := NULL;
9855 	    END IF;
9856 	 END IF;
9857 
9858 	 IF (l_to_rsv_rec.task_id = fnd_api.g_miss_num) THEN
9859 	    IF (l_task_id IS NOT NULL) THEN
9860 	       l_to_rsv_rec.task_id := l_task_id;
9861 	     ELSE
9862 	       l_to_rsv_rec.task_id := NULL;
9863 	    END IF;
9864 	 END IF;
9865 
9866        END IF;
9867      ELSE -- not project enable
9868 	  l_to_rsv_rec.project_id := NULL;
9869 	  l_to_rsv_rec.task_id := NULL;
9870     END IF;
9871     IF l_debug=1 THEN
9872        debug_print('After Rsv rec project id: ' || l_to_rsv_rec.project_id);
9873        debug_print('After Rsv rec task id: ' || l_to_rsv_rec.task_id);
9874     END IF;
9875 
9876     l_original_serial_count := p_original_serial_number.COUNT;
9877     l_to_serial_count := p_to_serial_number.COUNT;
9878     -- if from and to serials are passed, then pass them to the validate API
9879     IF l_original_serial_count > 0 THEN
9880        l_original_serial_number := p_original_serial_number;
9881     END IF;
9882     IF l_to_serial_count > 0 THEN
9883        l_to_serial_number := p_to_serial_number;
9884     END IF;
9885     /*** End R12 ***/
9886     --
9887     --Bug 2354735: Validate if p_validation_flag is either 'T' or 'V'
9888     IF (p_validation_flag = fnd_api.g_true OR p_validation_flag = 'V') THEN
9889       IF (l_debug = 1) THEN
9890          debug_print('Validation flag is true');
9891          debug_print('from dmd src type:'|| l_orig_rsv_tbl(1).demand_source_type_id);
9892          debug_print('to dmd src type:'|| l_to_rsv_rec.demand_source_type_id);
9893          debug_print('from dmd src hdr:'|| l_orig_rsv_tbl(1).demand_source_header_id);
9894          debug_print('to dmd src hdr:'|| l_to_rsv_rec.demand_source_header_id);
9895          debug_print('from dmd src line:'|| l_orig_rsv_tbl(1).demand_source_line_id);
9896          debug_print('to dmd src line:'|| l_to_rsv_rec.demand_source_line_id);
9897       END IF;
9898       -- we do validation after the query because
9899       -- for update, we might have many input value set to
9900       -- missing. We need to use the actual value to do
9901       -- validation
9902       inv_reservation_validate_pvt.validate_input_parameters
9903 	(
9904 	 x_return_status              => l_return_status
9905 	 , p_orig_rsv_rec               => l_orig_rsv_tbl(1)
9906 	 , p_to_rsv_rec                 => l_to_rsv_rec
9907 	 /**** {{ R12 Enhanced reservations code changes }}****/
9908 	 , p_orig_serial_array          => l_original_serial_number
9909 	 , p_to_serial_array            => l_to_serial_number
9910 	 /*** End R12 ***/
9911 	 , p_rsv_action_name            => 'UPDATE'
9912 	 , x_orig_item_cache_index      => l_orig_item_cache_index
9913 	 , x_orig_org_cache_index       => l_orig_org_cache_index
9914 	 , x_orig_demand_cache_index    => l_orig_demand_cache_index
9915 	 , x_orig_supply_cache_index    => l_orig_supply_cache_index
9916 	 , x_orig_sub_cache_index       => l_orig_sub_cache_index
9917 	 , x_to_item_cache_index        => l_to_item_cache_index
9918 	 , x_to_org_cache_index         => l_to_org_cache_index
9919 	 , x_to_demand_cache_index      => l_to_demand_cache_index
9920 	 , x_to_supply_cache_index      => l_to_supply_cache_index
9921 	 , x_to_sub_cache_index         => l_to_sub_cache_index
9922 	 );
9923 
9924       --
9925       IF l_return_status = fnd_api.g_ret_sts_error THEN
9926 	 IF (l_debug = 1) THEN
9927 	    debug_print('Validate input parameters returned error');
9928 	 END IF;
9929 	 RAISE fnd_api.g_exc_error;
9930       END IF;
9931 
9932       --
9933       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9934 	 IF (l_debug = 1) THEN
9935 	    debug_print('Validate input parameters returned unexpected error');
9936 	 END IF;
9937 	 RAISE fnd_api.g_exc_unexpected_error;
9938       END IF;
9939     END IF;
9940 
9941     -- INVCONV B4498579 BEGIN
9942     -- Use cache to determine lot divisibility where possible
9943     IF NVL(l_to_item_cache_index,0) > 0 THEN
9944       IF NOT is_lot_divisible(l_to_item_cache_index) THEN
9945        l_lot_divisible_flag := 'N';
9946       END IF;
9947     ELSE
9948     -- otherwise, where cache not available
9949       IF NOT lot_divisible(l_orig_rsv_tbl(1).inventory_item_id,l_orig_rsv_tbl(1).organization_id) THEN
9950 	l_lot_divisible_flag := 'N';
9951       END IF;
9952     END IF;
9953     IF (l_debug = 1) THEN
9954       debug_print('Update scenario and lot divisible is '||l_lot_divisible_flag);
9955     END IF;
9956     -- INVCONV B4498579 END
9957 
9958     --
9959     --Bug 2354735: Proceed with Trees only if p_validation_flag = 'T'
9960 
9961     -- Pick Releaser Performance - Added validation_flag = 'Q' to distinguish
9962     -- between validation above and the qty tree processing below.
9963      /**** {{ R12 Enhanced reservations code changes }}****/
9964     IF (((p_validation_flag = fnd_api.g_true) OR (p_validation_flag = 'T') OR
9965        (p_validation_flag = 'Q')) AND l_to_rsv_rec.supply_source_type_id = inv_reservation_global.g_source_type_inv)
9966       THEN
9967        /*** End R12 ***/
9968 
9969        inv_quantity_tree_pvt.create_tree
9970 	 (
9971 	  p_api_version_number         => 1.0
9972 	  , p_init_msg_lst               => fnd_api.g_true
9973 	  , x_return_status              => l_return_status
9974 	  , x_msg_count                  => x_msg_count
9975 	  , x_msg_data                   => x_msg_data
9976 	  , p_organization_id            => l_orig_rsv_tbl(1).organization_id
9977 	  , p_inventory_item_id          => l_orig_rsv_tbl(1).inventory_item_id
9978 	  , p_tree_mode                  => inv_quantity_tree_pvt.g_reservation_mode
9979 	  , p_is_revision_control        => is_revision_control(l_orig_item_cache_index)
9980 	  , p_is_lot_control             => is_lot_control(l_orig_item_cache_index)
9981 	  , p_is_serial_control          => is_serial_control(l_orig_item_cache_index)
9982 	  , p_asset_sub_only             => FALSE
9983 	  , p_include_suggestion         => TRUE
9984 	  , p_demand_source_type_id      => l_orig_rsv_tbl(1).demand_source_type_id
9985          , p_demand_source_header_id    => l_orig_rsv_tbl(1).demand_source_header_id
9986          , p_demand_source_line_id      => l_orig_rsv_tbl(1).demand_source_line_id
9987          , p_demand_source_name         => l_orig_rsv_tbl(1).demand_source_name
9988          , p_demand_source_delivery     => l_orig_rsv_tbl(1).demand_source_delivery
9989          , p_lot_expiration_date        => SYSDATE -- Bug#2716563
9990          , x_tree_id                    => l_tree_id1
9991          );
9992 
9993          --
9994          IF l_return_status = fnd_api.g_ret_sts_error THEN
9995            IF (l_debug = 1) THEN
9996               debug_print('Create Tree returned error');
9997            END IF;
9998            RAISE fnd_api.g_exc_error;
9999          END IF;
10000 
10001          --
10002          IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10003            IF (l_debug = 1) THEN
10004               debug_print('Create Tree returned unexpected error');
10005            END IF;
10006            RAISE fnd_api.g_exc_unexpected_error;
10007          END IF;
10008 
10009           --
10010          /*  Bug 1575930
10011           *  Because of changes to the quantity tree API, we no longer
10012           *  build separate trees on the basis of demand info.  The tree created
10013           *  above and this tree would actually be the same tree, but would
10014           *  would have different tree_ids.  We don't need to update the same
10015           *  tree twice, so we don't need to create the same tree twice.
10016           *  Instead, pass NULL to the modify_tree procedure.
10017           *inv_quantity_tree_pvt.create_tree
10018           * (
10019           *   p_api_version_number      => 1.0
10020           * , p_init_msg_lst            => fnd_api.g_true
10021           * , x_return_status           => l_return_status
10022           * , x_msg_count               => x_msg_count
10023           * , x_msg_data                => x_msg_data
10024           * , p_organization_id         => l_to_rsv_rec.organization_id
10025           * , p_inventory_item_id       => l_to_rsv_rec.inventory_item_id
10026           * , p_tree_mode               => inv_quantity_tree_pvt.g_reservation_mode
10027           * , p_is_revision_control     => is_revision_control(l_to_item_cache_index)
10028           * , p_is_lot_control          => is_lot_control(l_to_item_cache_index)
10029           * , p_is_serial_control       => is_serial_control(l_to_item_cache_index)
10030           * , p_asset_sub_only          => FALSE
10031           * , p_include_suggestion      => TRUE
10032           * , p_demand_source_type_id   => l_to_rsv_rec.demand_source_type_id
10033           * , p_demand_source_header_id => l_to_rsv_rec.demand_source_header_id
10034           * , p_demand_source_line_id   => l_to_rsv_rec.demand_source_line_id
10035           * , p_demand_source_name      => l_to_rsv_rec.demand_source_name
10036           * , p_demand_source_delivery  => l_to_rsv_rec.demand_source_delivery
10037           * , p_lot_expiration_date     => NULL
10038           * , x_tree_id                 => l_tree_id2
10039           *);
10040           --
10041           *IF l_return_status = fnd_api.g_ret_sts_error THEN
10042           *   RAISE fnd_api.g_exc_error;
10043           *END IF ;
10044           --
10045           *IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10046           *   RAISE fnd_api.g_exc_unexpected_error;
10047           *END IF;
10048           */
10049           --
10050          l_tree_id2  := NULL;
10051          -- INVCONV - Upgrade call to incorporate secondaries
10052          modify_tree_for_update_xfer(
10053            x_return_status              => l_return_status
10054          , x_quantity_reserved          => l_quantity_reserved
10055          , x_secondary_quantity_reserved => l_secondary_quantity_reserved
10056          , p_from_tree_id               => l_tree_id1
10057          , p_from_supply_source_type_id => l_orig_rsv_tbl(1).supply_source_type_id
10058          , p_from_revision              => l_orig_rsv_tbl(1).revision
10059          , p_from_lot_number            => l_orig_rsv_tbl(1).lot_number
10060          , p_from_subinventory_code     => l_orig_rsv_tbl(1).subinventory_code
10061          , p_from_locator_id            => l_orig_rsv_tbl(1).locator_id
10062          , p_from_lpn_id                => l_orig_rsv_tbl(1).lpn_id
10063          , p_from_primary_rsv_quantity  => l_orig_rsv_tbl(1).primary_reservation_quantity
10064          , p_from_second_rsv_quantity   => l_orig_rsv_tbl(1).secondary_reservation_quantity
10065          , p_from_detailed_quantity     => l_orig_rsv_tbl(1).detailed_quantity
10066          , p_from_sec_detailed_quantity => l_orig_rsv_tbl(1).secondary_detailed_quantity
10067          , p_to_tree_id                 => l_tree_id2
10068          , p_to_supply_source_type_id   => l_to_rsv_rec.supply_source_type_id
10069          , p_to_revision                => l_to_rsv_rec.revision
10070          , p_to_lot_number              => l_to_rsv_rec.lot_number
10071          , p_to_subinventory_code       => l_to_rsv_rec.subinventory_code
10072          , p_to_locator_id              => l_to_rsv_rec.locator_id
10073          , p_to_lpn_id                  => l_to_rsv_rec.lpn_id
10074          , p_to_primary_rsv_quantity    => l_to_rsv_rec.primary_reservation_quantity
10075          , p_to_second_rsv_quantity     => l_to_rsv_rec.secondary_reservation_quantity
10076          , p_to_detailed_quantity       => l_to_rsv_rec.detailed_quantity
10077          , p_to_second_detailed_quantity => l_to_rsv_rec.secondary_detailed_quantity
10078          , p_to_revision_control        => is_revision_control(l_to_item_cache_index)
10079          , p_to_lot_control             => is_lot_control(l_to_item_cache_index)
10080          , p_action                     => 'UPDATE'
10081          , p_lot_divisible_flag         => l_lot_divisible_flag                          -- INVCONV
10082          , p_partial_reservation_flag   => p_partial_reservation_flag
10083          , p_check_availability         => p_check_availability
10084          );
10085 
10086          IF l_return_status = fnd_api.g_ret_sts_error THEN
10087            IF (l_debug = 1) THEN
10088               debug_print('modify_tree_for_update_xfer returned error');
10089            END IF;
10090            RAISE fnd_api.g_exc_error;
10091          END IF;
10092 
10093          --
10094          IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10095            IF (l_debug = 1) THEN
10096               debug_print('modify_tree_for_update_xfer returned unexpected error');
10097            END IF;
10098            RAISE fnd_api.g_exc_unexpected_error;
10099          END IF;
10100 
10101 
10102          IF (l_debug = 1) THEN
10103               debug_print('modify_tree_for_update_xfer returned success ');
10104               debug_print('The value for x_quantity_reserved is'||l_quantity_reserved );
10105               debug_print('The value for x_secondary_quantity_reserved is'||l_secondary_quantity_reserved ); --INVCONV
10106 	 END IF;
10107 
10108 	 IF  l_quantity_reserved > 0
10109 	   AND l_quantity_reserved < l_to_rsv_rec.primary_reservation_quantity - NVL(l_to_rsv_rec.detailed_quantity, 0) THEN
10110 	    -- This is the case of partial reservations. We  need to recompute
10111 	    -- the actual quantity for reservation
10112 	    -- convert quantity between primary uom and reservation uom
10113 	    l_to_rsv_rec.primary_reservation_quantity  := l_quantity_reserved + NVL(l_to_rsv_rec.detailed_quantity, 0);
10114 	    -- INVCONV BEGIN
10115 	    IF is_dual_control(l_orig_item_cache_index) THEN
10116 	       l_dual_control_flag := 'Y';
10117 	       l_to_rsv_rec.secondary_reservation_quantity  := l_secondary_quantity_reserved + NVL(l_to_rsv_rec.secondary_detailed_quantity, 0); -- Bug 6942475
10118 	    END IF;
10119 	    -- INVCONV END
10120 	    l_to_rsv_rec.reservation_quantity          := NULL;
10121 
10122 	    IF (l_debug = 1) THEN
10123 	       debug_print('l_to_rsv_rec.detailed_quantity: ' || l_to_rsv_rec.detailed_quantity);
10124 	       debug_print('l_to_rsv_rec.primary_reservation_quantity: ' || l_to_rsv_rec.primary_reservation_quantity);
10125 	       debug_print('l_to_rsv_rec.secondary_reservation_quantity: ' || l_to_rsv_rec.secondary_reservation_quantity); -- INVCONV
10126 	    END IF;
10127 
10128 	    convert_quantity(x_return_status => l_return_status, px_rsv_rec => l_to_rsv_rec);
10129 
10130 
10131 	    IF (l_debug = 1) THEN
10132 	       debug_print('After convert qty ' || l_return_status);
10133 	    END IF;
10134 
10135 	    IF l_return_status = fnd_api.g_ret_sts_error THEN
10136 	       IF (l_debug = 1) THEN
10137 		  debug_print('The convert_quantity returned a expected error');
10138 	       END IF;
10139 	       RAISE fnd_api.g_exc_error;
10140 	    END IF;
10141 
10142 	    IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10143 	       IF (l_debug = 1) THEN
10144 		  debug_print('The convert_quantity returned a unexpected error');
10145 	       END IF;
10146 	       RAISE fnd_api.g_exc_unexpected_error;
10147 	    END IF;
10148 
10149 	 END IF;--partial reservation case
10150 
10151 
10152  /**** {{ R12 Enhanced reservations code changes }}****/
10153      ELSIF (l_to_rsv_rec.supply_source_type_id IN
10154 	    (inv_reservation_global.g_source_type_wip, inv_reservation_global.g_source_type_po,
10155 	     inv_reservation_global.g_source_type_asn,
10156 	     inv_reservation_global.g_source_type_intransit,
10157 	     inv_reservation_global.g_source_type_internal_req,
10158 	     inv_reservation_global.g_source_type_rcv))  AND
10159        p_over_reservation_flag NOT IN (1,3) THEN
10160        -- call the helper procedure to get the reservable qty of the supply
10161        -- Bug 5199672: Should pass g_miss_num as default for supply
10162        -- source line detail. Otherwise, high level reservations
10163        -- will not be considered.
10164        get_supply_reservable_qty
10165 	 (
10166        	    x_return_status                 => l_return_status
10167 	  , x_msg_count                     => x_msg_count
10168 	  , x_msg_data                      => x_msg_data
10169           , p_fm_supply_source_type_id      => l_orig_rsv_tbl(1).supply_source_type_id
10170           , p_fm_supply_source_header_id    => l_orig_rsv_tbl(1).supply_source_header_id
10171           , p_fm_supply_source_line_id      => l_orig_rsv_tbl(1).supply_source_line_id
10172           , p_fm_supply_source_line_detail  => l_orig_rsv_tbl(1).supply_source_line_detail
10173           , p_fm_primary_reservation_qty    => l_orig_rsv_tbl(1).primary_reservation_quantity
10174           , p_to_supply_source_type_id      => l_to_rsv_rec.supply_source_type_id
10175           , p_to_supply_source_header_id    => l_to_rsv_rec.supply_source_header_id
10176           , p_to_supply_source_line_id      => l_to_rsv_rec.supply_source_line_id
10177           , p_to_supply_source_line_detail  => l_to_rsv_rec.supply_source_line_detail
10178           , p_to_primary_reservation_qty    => l_to_rsv_rec.primary_reservation_quantity
10179           , p_to_organization_id            => l_to_rsv_rec.organization_id
10180           , p_to_inventory_item_id          => l_to_rsv_rec.inventory_item_id
10181           , p_to_revision                   => l_to_rsv_rec.revision
10182           , p_to_lot_number                 => l_to_rsv_rec.lot_number
10183           , p_to_subinventory_code          => l_to_rsv_rec.subinventory_code
10184           , p_to_locator_id                 => l_to_rsv_rec.locator_id
10185           , p_to_lpn_id                     => l_to_rsv_rec.lpn_id
10186           , p_to_project_id                 => l_to_rsv_rec.project_id
10187           , p_to_task_id                    => l_to_rsv_rec.task_id
10188 	 , x_reservable_qty                 => l_quantity_reserved
10189 	 , x_qty_available                  => l_qty_available
10190 	 );
10191 
10192        IF (l_debug = 1) THEN
10193 	  debug_print('After calling get supply reservable qty ' || l_return_status);
10194 	  debug_print('Available quantity to reserve:  ' || l_quantity_reserved);
10195        END IF;
10196 
10197        --
10198        IF l_return_status = fnd_api.g_ret_sts_error THEN
10199 	  RAISE fnd_api.g_exc_error;
10200        END IF;
10201 
10202        --
10203        IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10204 	  RAISE fnd_api.g_exc_unexpected_error;
10205        END IF;
10206 
10207        IF ((l_to_rsv_rec.primary_reservation_quantity - l_quantity_reserved)
10208 	 > 0.000005) THEN
10209 
10210           IF (p_partial_reservation_flag = fnd_api.g_false) THEN
10211              IF (l_debug = 1) THEN
10212                 debug_print('The supply document doesnt have enough quantity to be reserved against. error out. ');
10213              END IF;
10214              fnd_message.set_name('INV', 'INV_INVALID_AVAILABLE_QTY');
10215              fnd_msg_pub.ADD;
10216              RAISE fnd_api.g_exc_error;
10217           ELSE
10218              l_to_rsv_rec.primary_reservation_quantity  := l_quantity_reserved;
10219              l_to_rsv_rec.reservation_quantity          := NULL;
10220              IF (l_debug = 1) THEN
10221                 debug_print('l_to_rsv_rec.primary_reservation_quantity: ' || l_to_rsv_rec.primary_reservation_quantity);
10222              END IF;
10223              convert_quantity(x_return_status => l_return_status, px_rsv_rec => l_to_rsv_rec);
10224 
10225              IF (l_debug = 1) THEN
10226                 debug_print('After convert qty ' || l_return_status);
10227              END IF;
10228 
10229              IF l_return_status = fnd_api.g_ret_sts_error THEN
10230                 RAISE fnd_api.g_exc_error;
10231              END IF;
10232 
10233              --
10234              IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10235                 RAISE fnd_api.g_exc_unexpected_error;
10236              END IF;
10237 
10238           END IF;
10239 
10240        END IF;
10241 
10242     END IF;
10243 
10244     IF (l_debug = 1) THEN
10245        debug_print('From record:');
10246        debug_print('demand_source_type_id = ' || l_orig_rsv_tbl(1).demand_source_type_id);
10247        debug_print('demand_source_header_id = ' || l_orig_rsv_tbl(1).demand_source_header_id);
10248        debug_print('demand_source_line_id = ' || l_orig_rsv_tbl(1).demand_source_line_id);
10249        debug_print('demand_source_line_detail = ' || l_orig_rsv_tbl(1).demand_source_line_detail);
10250        debug_print('To record:');
10251        debug_print('demand_source_type_id = ' || l_to_rsv_rec.demand_source_type_id);
10252        debug_print('demand_source_header_id = ' || l_to_rsv_rec.demand_source_header_id);
10253        debug_print('demand_source_line_id = ' || l_to_rsv_rec.demand_source_line_id);
10254        debug_print('demand_source_line_detail = ' || l_to_rsv_rec.demand_source_line_detail);
10255     END IF;
10256 
10257     IF (l_to_rsv_rec.demand_source_type_id IN
10258 	(inv_reservation_global.g_source_type_oe,
10259 	 inv_reservation_global.g_source_type_internal_ord,inv_reservation_global.g_source_type_rma)) THEN
10260       BEGIN
10261 	 SELECT open_flag, booked_flag
10262 	   INTO l_open_flag,
10263 	   l_booked_flag
10264 	   FROM oe_order_lines_all
10265 	   WHERE line_id = l_to_rsv_rec.demand_source_line_id;
10266       EXCEPTION WHEN no_data_found THEN
10267       fnd_message.set_name('INV', 'INV_INVALID_SALES_ORDER');
10268       fnd_msg_pub.ADD;
10269       RAISE fnd_api.g_exc_error;
10270       END;
10271     END IF;
10272 
10273     IF (l_debug = 1) THEN
10274        debug_print('Open flag :' || l_open_flag);
10275        debug_print('booked flag :' || l_booked_flag);
10276     END IF;
10277 
10278     IF (((l_to_rsv_rec.demand_source_type_id IN
10279 	 (inv_reservation_global.g_source_type_oe,
10280 	  inv_reservation_global.g_source_type_internal_ord, inv_reservation_global.g_source_type_rma)) AND
10281 
10282 	(NOT((Nvl(l_orig_rsv_tbl(1).demand_source_type_id,-99) = Nvl(l_to_rsv_rec.demand_source_type_id,-99)) AND
10283 	 (Nvl(l_orig_rsv_tbl(1).demand_source_header_id,-99) = Nvl(l_to_rsv_rec.demand_source_header_id,-99)) AND
10284 	 (Nvl(l_orig_rsv_tbl(1).demand_source_line_id,-99) = Nvl(l_to_rsv_rec.demand_source_line_id,-99)) AND
10285 	  nvl(l_open_flag, 'N') = 'N'))) OR
10286       (l_to_rsv_rec.demand_source_type_id =
10287        inv_reservation_global.g_source_type_wip)) AND
10288        p_over_reservation_flag NOT IN (2,3)  THEN
10289 
10290        -- call the helper procedure to get the reservable qty of the demand
10291        IF (l_debug = 1) THEN
10292 	  debug_print('Inside get demand reservable qty');
10293        END IF;
10294 
10295        -- Bug 5199672: Should pass g_miss_num as default for demand
10296        -- source line detail. Otherwise, high level reservations
10297        -- will not be considered.
10298        get_demand_reservable_qty
10299 	 (
10300 	  x_return_status                 => l_return_status
10301 	  , x_msg_count                     => x_msg_count
10302 	  , x_msg_data                      => x_msg_data
10303           , p_fm_demand_source_type_id      => l_orig_rsv_tbl(1).demand_source_type_id
10304           , p_fm_demand_source_header_id    => l_orig_rsv_tbl(1).demand_source_header_id
10305           , p_fm_demand_source_line_id      => l_orig_rsv_tbl(1).demand_source_line_id
10306           , p_fm_demand_source_line_detail  => l_orig_rsv_tbl(1).demand_source_line_detail
10307           , p_fm_primary_reservation_qty    => l_orig_rsv_tbl(1).primary_reservation_quantity
10308           , p_to_demand_source_type_id      => l_to_rsv_rec.demand_source_type_id
10309           , p_to_demand_source_header_id    => l_to_rsv_rec.demand_source_header_id
10310           , p_to_demand_source_line_id      => l_to_rsv_rec.demand_source_line_id
10311           , p_to_demand_source_line_detail  => l_to_rsv_rec.demand_source_line_detail
10312           , p_to_primary_reservation_qty    => l_to_rsv_rec.primary_reservation_quantity
10313 	 , p_to_organization_id            => l_to_rsv_rec.organization_id
10314 	 , p_to_inventory_item_id          => l_to_rsv_rec.inventory_item_id
10315 	 , p_to_primary_uom_code           => l_to_rsv_rec.primary_uom_code
10316 	 , p_to_project_id                 => l_to_rsv_rec.project_id
10317 	 , p_to_task_id                    => l_to_rsv_rec.task_id
10318 	 , x_reservable_qty                => l_quantity_reserved
10319 	 , x_qty_available                 => l_qty_available
10320 	 );
10321 
10322 
10323        IF (l_debug = 1) THEN
10324 	  debug_print('After calling available demand to reserve ' || l_return_status);
10325 	  debug_print('Available quantity to reserve. l_qty_changed: ' || l_quantity_reserved);
10326        END IF;
10327 
10328        --
10329        IF l_return_status = fnd_api.g_ret_sts_error THEN
10330 	  RAISE fnd_api.g_exc_error;
10331        END IF;
10332 
10333        --
10334        IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10335 	  RAISE fnd_api.g_exc_unexpected_error;
10336        END IF;
10337 
10338        IF ((l_to_rsv_rec.primary_reservation_quantity -
10339 	    l_quantity_reserved) > 0.000005) THEN
10340 
10341           IF (l_debug = 1) THEN
10342              debug_print('The demand document doesnt have enough quantity to be reserved against. error out. ');
10343              debug_print('l_to_rsv_rec.primary_reservation_quantity: ' || l_to_rsv_rec.primary_reservation_quantity);
10344           END IF;
10345           fnd_message.set_name('INV', 'INV_INVALID_AVAILABLE_QTY');
10346           fnd_msg_pub.ADD;
10347           RAISE fnd_api.g_exc_error;
10348        END IF;
10349 
10350     END IF;
10351 
10352     /*** End R12 ***/
10353 
10354     --
10355     -- Pre Update CTO Validation
10356     IF l_to_rsv_rec.demand_source_type_id IN (inv_reservation_global.g_source_type_oe, inv_reservation_global.g_source_type_internal_ord, inv_reservation_global.g_source_type_rma) THEN
10357        --
10358        IF (l_debug = 1) THEN
10359           debug_print('Pre Update CTO validation');
10360        END IF;
10361 
10362        IF l_orig_rsv_tbl(1).primary_reservation_quantity > l_to_rsv_rec.primary_reservation_quantity THEN
10363 	  cto_workflow_api_pk.inventory_unreservation_check
10364 	    (
10365 	     p_order_line_id              => l_orig_rsv_tbl(1).demand_source_line_id
10366 	     , p_rsv_quantity               => l_orig_rsv_tbl(1).primary_reservation_quantity - l_to_rsv_rec.primary_reservation_quantity
10367 	     , x_return_status              => l_return_status
10368 	     , x_msg_count                  => x_msg_count
10369 	     , x_msg_data                   => x_msg_data
10370 	     );
10371 
10372 	ELSE  --Else Condition Added for Bug#2467387.
10373 	  cto_workflow_api_pk.inventory_reservation_check
10374 	    (
10375 	     p_order_line_id => l_orig_rsv_tbl(1).demand_source_line_id
10376 	     ,x_return_status => l_return_status
10377 	     ,x_msg_count => x_msg_count
10378 	     ,x_msg_data => x_msg_data
10379 	     );
10380        END IF;
10381        --
10382        IF l_return_status = fnd_api.g_ret_sts_error THEN
10383 	  debug_print('Pre Update CTO validation returned error');
10384 	  RAISE fnd_api.g_exc_error;
10385        END IF;
10386 
10387        --
10388        IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10389 	  debug_print('Pre Update CTO validation returned unexpected error');
10390 	  RAISE fnd_api.g_exc_unexpected_error;
10391        END IF;
10392        --
10393     END IF;
10394 
10395     --Bug 1838450.  Detailed quantity should never exceed primary
10396     --  reservation quantity.  So, if someone has reduced the
10397     --  reservation quantity, we should reduce the detailed quantity also
10398 
10399     IF l_to_rsv_rec.detailed_quantity > l_to_rsv_rec.primary_reservation_quantity THEN
10400        IF (l_debug = 1) THEN
10401 	  debug_print('Setting detailed quantity the same as the primary reservation quantity');
10402        END IF;
10403        l_to_rsv_rec.detailed_quantity  := l_to_rsv_rec.primary_reservation_quantity;
10404        -- INVCONV BEGIN
10405        IF l_dual_control_flag = 'Y' THEN
10406 	  l_to_rsv_rec.secondary_detailed_quantity  := l_to_rsv_rec.secondary_reservation_quantity;
10407        END IF;
10408        -- INVCONV END
10409     END IF;
10410 
10411     --Bug #2819700
10412     --Adding an extra check to make sure that the update reservations does
10413     --not update the existing record to negative. Raise an exception.
10414     -- Adding this check after the tree is created and
10415     -- modify_tree_for_update_xfer is called and some computations
10416     -- have been made.
10417     IF (l_debug = 1) THEN
10418       debug_print('Primary_reservation_qty before inserting (update)= '
10419 		              || To_char(l_to_rsv_rec.primary_reservation_quantity));
10420       debug_print('Secondary_reservation_qty before inserting (update)= '
10421                               || To_char(l_to_rsv_rec.secondary_reservation_quantity));
10422       debug_print('Reservation_qty before inserting (update)= '
10423 		              || To_char(l_to_rsv_rec.reservation_quantity)); -- INVCONV
10424     END IF;
10425 
10426     IF (  (NVL(l_to_rsv_rec.reservation_quantity,0) < 0) OR
10427 	        (NVL(l_to_rsv_rec.primary_reservation_quantity,0) < 0) ) THEN
10428       fnd_message.set_name('INV', 'INV-INVALID RESERVATION QTY');
10429       fnd_msg_pub.ADD;
10430       RAISE fnd_api.g_exc_error;
10431     END IF;
10432 
10433      -- INVCONV BEGIN
10434     IF (NVL(l_to_rsv_rec.secondary_reservation_quantity,0) < 0) THEN
10435       fnd_message.set_name('INV', 'INV-INVALID NEGATIVE SECONDARY');   -- INVCONV New Message
10436       fnd_msg_pub.ADD;
10437       RAISE fnd_api.g_exc_error;
10438     END IF;
10439     -- INVCONV END
10440 
10441     /**** {{ R12 Enhanced reservations code changes }}****/
10442     -- Check for the serial reservations. All serial related checks will be
10443     -- done here.
10444     IF (l_debug = 1) THEN
10445        debug_print('Original serial count' || l_original_serial_count);
10446        debug_print('To serial count' || l_to_serial_count);
10447     END IF;
10448 
10449     IF (l_original_serial_count = 0 AND l_to_serial_count = 0) THEN
10450        -- both from and to serial tables are empty. They are not passed.
10451        IF (l_debug = 1) THEN
10452 	  debug_print('Inside serial check. Not passed');
10453        END IF;
10454        BEGIN
10455 	  SELECT inventory_item_id, serial_number bulk collect INTO
10456 	    l_serial_number_table FROM
10457 	    mtl_serial_numbers  WHERE reservation_id =
10458 	   l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
10459 	    l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
10460        EXCEPTION
10461 	  WHEN no_data_found THEN
10462 	     IF l_debug=1 THEN
10463 		debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
10464 	     END IF;
10465        END;
10466 
10467        l_total_serials_reserved := l_serial_number_table.COUNT;
10468        IF l_debug=1 THEN
10469 		debug_print('Total reserved serials: ' || l_total_serials_reserved);
10470        END IF;
10471 
10472        IF (l_total_serials_reserved > 0) THEN
10473 	  -- call validate serials for the to record.
10474 	  inv_reservation_validate_pvt.validate_serials
10475 	    (
10476 	     x_return_status              => l_return_status
10477 	     , p_rsv_action_name          => 'UPDATE'
10478 	     , p_orig_rsv_rec             => l_dummy_rsv_rec
10479 	     , p_to_rsv_rec               => l_to_rsv_rec
10480 	     , p_orig_serial_array        => l_dummy_serial_array
10481 	     , p_to_serial_array          => l_serial_number_table
10482 	     );
10483 
10484 	  IF (l_debug = 1) THEN
10485 	     debug_print('After calling validate serials ' || l_return_status);
10486 	  END IF;
10487 
10488 	  --
10489 	  IF l_return_status = fnd_api.g_ret_sts_error THEN
10490 	     RAISE fnd_api.g_exc_error;
10491 	  END IF;
10492 
10493 
10494 	  --
10495 	  IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10496 	     RAISE fnd_api.g_exc_unexpected_error;
10497 	  END IF;
10498 
10499 	  -- there are some serial reserved. Check if the number of serials
10500 	  -- exceed the reservation quantity.
10501 	  IF (l_total_serials_reserved > l_to_rsv_rec.primary_reservation_quantity) THEN
10502 	     -- we have to unreserve some of the serials
10503 	     -- unreserve the extra serials that are more than the primary_reservation_quantity
10504 	     IF (l_debug = 1) THEN
10505 		debug_print('Total serials more than reservation quantity.');
10506 	     END IF;
10507 
10508 	     l_serials_tobe_unreserved := l_total_serials_reserved - l_to_rsv_rec.primary_reservation_quantity;
10509 
10510 	     FOR i IN 1..l_serials_tobe_unreserved
10511 	       LOOP
10512 	         BEGIN
10513 		    UPDATE mtl_serial_numbers SET reservation_id = NULL,
10514 		      group_mark_id = NULL, line_mark_id = NULL,
10515 		      lot_line_mark_id = NULL WHERE
10516 		      serial_number = l_serial_number_table(i).serial_number AND
10517 		      current_organization_id = l_orig_rsv_tbl(1).organization_id AND
10518 		      inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
10519 		 EXCEPTION
10520 		    WHEN no_data_found THEN
10521 		       IF l_debug=1 THEN
10522 			  debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
10523 		       END IF;
10524 		       fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
10525 		       fnd_msg_pub.ADD;
10526 		       RAISE fnd_api.g_exc_error;
10527 		 END;
10528 		 IF l_debug=1 THEN
10529 		    debug_print('Serial being unreserved. serial number: ' || l_serial_number_table(i).serial_number);
10530 		 END IF;
10531 	       END LOOP;
10532 	       -- update the serial reservation quantity to be the primary
10533 	       -- reservation quantity, as it is in excess.
10534 	       l_to_rsv_rec.serial_reservation_quantity := l_to_rsv_rec.primary_reservation_quantity;
10535 
10536 	   ELSE
10537 	       -- we will have to migrate the serials to the new reservation
10538 	       -- Since it is the same record we are working on. We dont have to
10539 	       -- do anything. Just make sure that the group_mark_id and the
10540 		       -- reservation id are populated.
10541 
10542 	       FOR l_serial_index IN l_serial_number_table.first..l_serial_number_table.last
10543 		 LOOP
10544 		    IF l_debug=1 THEN
10545 		     debug_print('Inside serials not more than res qty');
10546 		     debug_print('reservation id' ||
10547 				 l_orig_rsv_tbl(1).reservation_id);
10548 		     debug_print('serial being processed' ||
10549 				 l_serial_number_table(l_serial_index).serial_number);
10550 		     debug_print('org' ||
10551 				 l_orig_rsv_tbl(1).organization_id);
10552 		      debug_print('item' ||
10553 				 l_orig_rsv_tbl(1).inventory_item_id);
10554 		 END IF;
10555 	           BEGIN
10556 		      UPDATE mtl_serial_numbers SET reservation_id = l_orig_rsv_tbl(1).reservation_id,
10557 			group_mark_id = l_orig_rsv_tbl(1).reservation_id WHERE
10558 			serial_number = l_serial_number_table(l_serial_index).serial_number
10559 			AND current_organization_id = l_orig_rsv_tbl(1).organization_id
10560 			AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
10561 		   EXCEPTION
10562 
10563 		      WHEN no_data_found THEN
10564 			 IF l_debug=1 THEN
10565 			    debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
10566 			 END IF;
10567 			 fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
10568 			 fnd_msg_pub.ADD;
10569 			 RAISE fnd_api.g_exc_error;
10570 		   END;
10571 		   IF l_debug=1 THEN
10572 		      debug_print('Serial being migrated. serial number: ' || l_serial_number_table(l_serial_index).serial_number);
10573 		   END IF;
10574 		 END LOOP;
10575 		 -- update the serial reservation quantity
10576 		 l_to_rsv_rec.serial_reservation_quantity := l_total_serials_reserved;
10577 
10578 	  END IF;
10579        END IF;
10580 
10581      ELSIF (l_original_serial_count > 0 OR l_to_serial_count > 0) THEN
10582 
10583        -- One of them is passed.
10584        -- From record is already validated. There are some extra
10585        -- validations that needs to happen for the to record.
10586 
10587        IF (l_to_serial_count > 0) THEN
10588 	  -- The serial has bo be either reserved to the from record or
10589 	  -- not reserved at all.
10590 	  FOR i IN p_to_serial_number.first..p_to_serial_number.last
10591 	    LOOP
10592 	      BEGIN
10593 		 SELECT reservation_id, group_mark_id INTO
10594 		   l_reservation_id,l_group_mark_id
10595 		   FROM mtl_serial_numbers WHERE
10596 		   serial_number = p_to_serial_number(i).serial_number AND
10597 		   current_organization_id = l_orig_rsv_tbl(1).organization_id AND
10598 		   inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
10599 	      EXCEPTION
10600 		 WHEN no_data_found THEN
10601 		    IF l_debug=1 THEN
10602 		       debug_print('No serials found for this data : ' ||
10603 				   p_to_serial_number(i).serial_number);
10604 		    END IF;
10605 		    fnd_message.set_name('INV', 'INV_INVALID_TO_SERIAL');
10606 		    fnd_msg_pub.ADD;
10607 		    RAISE fnd_api.g_exc_error;
10608 	      END;
10609 
10610 	      IF (l_reservation_id IS NOT NULL AND l_reservation_id <>
10611 		  l_orig_rsv_tbl(1).reservation_id) THEN
10612 		 fnd_message.set_name('INV', 'INV_INVALID_TO_SERIAL');
10613 		 fnd_msg_pub.ADD;
10614 		 RAISE fnd_api.g_exc_error;
10615 	      END IF;
10616 
10617 	      -- If the serial is not reserved then it should not have the
10618 	      -- group mark id stamped. It means it is being used by
10619 	      -- another transaction and cant be reserved.
10620 	      IF (l_reservation_id IS NULL) AND (l_group_mark_id IS NOT NULL) AND
10621 		  (l_group_mark_id <> -1)THEN
10622 		 IF (l_debug = 1) THEN
10623 		    debug_print('Group Mark Id is not null for serial ' || p_to_serial_number(i).serial_number);
10624 		 END IF;
10625 		 fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
10626 		 fnd_msg_pub.ADD;
10627 		 RAISE fnd_api.g_exc_error;
10628 	      END IF;
10629 
10630 	    END LOOP;
10631 
10632        END IF;
10633 
10634        -- unreserve the from and reserve the to and make sure that the
10635        -- total doesnt exceed the primary reservation quantity.
10636        IF (l_original_serial_count > 0) THEN
10637 	  FOR i IN
10638 	    p_original_serial_number.first..p_original_serial_number.last
10639 	    LOOP
10640 	      BEGIN
10641 		 UPDATE mtl_serial_numbers SET reservation_id = NULL,
10642 		   group_mark_id = NULL, line_mark_id = NULL,
10643 		      lot_line_mark_id = NULL  WHERE
10644 		   serial_number = p_original_serial_number(i).serial_number AND
10645 		   current_organization_id = l_orig_rsv_tbl(1).organization_id AND
10646 		   inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
10647 	      EXCEPTION
10648 		 WHEN no_data_found THEN
10649 		    IF l_debug=1 THEN
10650 		       debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
10651 		    END IF;
10652 		    fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
10653 		    fnd_msg_pub.ADD;
10654 		    RAISE fnd_api.g_exc_error;
10655 	      END;
10656 	    END LOOP;
10657        END IF;
10658 
10659        IF (l_to_serial_count > 0) THEN
10660 	  FOR i IN p_to_serial_number.first..p_to_serial_number.last
10661 	    LOOP
10662 	       IF (l_debug = 1) THEN
10663 		  debug_print('reservation id' ||
10664 			      l_orig_rsv_tbl(1).reservation_id);
10665 		  debug_print('serial number' ||
10666 			      p_to_serial_number(i).serial_number);
10667 		  debug_print('organization_id' ||
10668 			      l_orig_rsv_tbl(1).organization_id);
10669 		  debug_print('inventory_item_id = ' || l_orig_rsv_tbl(1).inventory_item_id);
10670 	       END IF;
10671 	      BEGIN
10672 		 UPDATE mtl_serial_numbers SET reservation_id = l_orig_rsv_tbl(1).reservation_id,
10673 		   group_mark_id = l_orig_rsv_tbl(1).reservation_id WHERE
10674 		   serial_number = p_to_serial_number(i).serial_number AND
10675 		   current_organization_id = l_orig_rsv_tbl(1).organization_id AND
10676 		   inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
10677 	      EXCEPTION
10678 		 WHEN no_data_found THEN
10679 		    IF l_debug=1 THEN
10680 		       debug_print('No serials found for reservation
10681 				   record. serial number: ' ||  p_to_serial_number(i).serial_number);
10682 		    END IF;
10683 		    fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
10684 		    fnd_msg_pub.ADD;
10685 		    RAISE fnd_api.g_exc_error;
10686 	      END;
10687 	    END LOOP;
10688        END IF;
10689 
10690 	IF (l_debug = 1) THEN
10691 				   debug_print('reservation id' ||
10692 					       l_orig_rsv_tbl(1).reservation_id);
10693 				   debug_print('organization_id' ||
10694 					       l_orig_rsv_tbl(1).organization_id);
10695 				   debug_print('inventory_item_id = ' || l_orig_rsv_tbl(1).inventory_item_id);
10696 				   END IF;
10697 	BEGIN
10698 	  SELECT COUNT(1) INTO l_total_serials_reserved FROM
10699 	    mtl_serial_numbers  WHERE reservation_id =
10700 	    l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
10701 	    l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
10702        EXCEPTION
10703 	  WHEN no_data_found THEN
10704 	     IF l_debug=1 THEN
10705 		debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
10706 	     END IF;
10707        END;
10708      IF (l_debug = 1) THEN
10709 	debug_print('After counting serials' || l_total_serials_reserved);
10710 	debug_print('Total reservation qty' || l_to_rsv_rec.primary_reservation_quantity);
10711      END IF;
10712 
10713        l_to_rsv_rec.serial_reservation_quantity := l_total_serials_reserved;
10714 
10715        IF (l_total_serials_reserved > l_to_rsv_rec.primary_reservation_quantity) THEN
10716 	  -- we have to unreserve some of the serials
10717 	  -- unreserve the extra serials that are more than the primary_reservation_quantity
10718 	  IF (l_debug = 1) THEN
10719 	     debug_print('Inside relieving serials');
10720 	  END IF;
10721 
10722           BEGIN
10723 	     SELECT inventory_item_id, serial_number bulk collect INTO
10724 	       l_serial_number_table FROM
10725 	       mtl_serial_numbers  WHERE reservation_id =
10726 	       l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
10727 	       l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
10728 	  EXCEPTION
10729 	     WHEN no_data_found THEN
10730 		IF l_debug=1 THEN
10731 		   debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
10732 		END IF;
10733 	  END;
10734 	  l_serials_tobe_unreserved := l_total_serials_reserved - l_to_rsv_rec.primary_reservation_quantity;
10735 
10736 	  FOR i IN 1..l_serials_tobe_unreserved
10737 	    LOOP
10738 	       IF (l_debug = 1) THEN
10739 		  debug_print('Serial being unreserved' || l_serial_number_table(i).serial_number);
10740 	       END IF;
10741 	       BEGIN
10742 		  UPDATE mtl_serial_numbers SET reservation_id = NULL,
10743 		    group_mark_id = NULL, line_mark_id = NULL,
10744 		      lot_line_mark_id = NULL WHERE
10745 		    serial_number = l_serial_number_table(i).serial_number AND
10746 		    current_organization_id = l_orig_rsv_tbl(1).organization_id AND
10747 		    inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
10748 	       EXCEPTION
10749 		  WHEN no_data_found THEN
10750 		     IF l_debug=1 THEN
10751 			debug_print('Inside relieve serials.No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
10752 		     END IF;
10753 		     fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
10754 		     fnd_msg_pub.ADD;
10755 		     RAISE fnd_api.g_exc_error;
10756 	       END;
10757 	    END LOOP;
10758 	    -- update the serial reservation quantity to be the primary
10759 	    -- reservation quantity, as it is in excess.
10760 	    l_to_rsv_rec.serial_reservation_quantity := l_to_rsv_rec.primary_reservation_quantity;
10761        END IF;
10762 
10763     END IF;
10764     /*** End R12  ***/
10765 
10766     -- obtain program and user info
10767     l_date := SYSDATE;
10768 
10769     --
10770     l_user_id        := fnd_global.user_id;
10771     l_login_id       := fnd_global.login_id;
10772 
10773     IF l_login_id = -1 THEN
10774       l_login_id  := fnd_global.conc_login_id;
10775     END IF;
10776 
10777     l_request_id     := fnd_global.conc_request_id;
10778     l_prog_appl_id   := fnd_global.prog_appl_id;
10779     l_program_id     := fnd_global.conc_program_id;
10780     --
10781     -- update the table
10782     IF (l_debug = 1) THEN
10783        debug_print('Calling mtl_reservations_pkg.update_row');
10784     END IF;
10785 
10786     -- Bug 3461990: Reservations API should not update reservations with more
10787     -- than 5 decimal places, since the transaction quantity is being
10788     -- rounded to 5 decimal places.
10789 
10790     l_to_rsv_rec.primary_reservation_quantity :=
10791       round(l_to_rsv_rec.primary_reservation_quantity,5);
10792     l_to_rsv_rec.reservation_quantity :=
10793       round(l_to_rsv_rec.reservation_quantity,5);
10794     l_to_rsv_rec.detailed_quantity :=
10795       round(Nvl(l_to_rsv_rec.detailed_quantity,0),5);
10796 
10797      -- INVCONV BEGIN
10798     IF l_dual_control_flag = 'Y' THEN
10799       l_to_rsv_rec.secondary_reservation_quantity :=
10800         round(l_to_rsv_rec.secondary_reservation_quantity,5);
10801       l_to_rsv_rec.secondary_detailed_quantity :=
10802         round(Nvl(l_to_rsv_rec.secondary_detailed_quantity,0),5);
10803     END IF;
10804     -- INVCONV END
10805 
10806     IF (l_debug = 1) THEN
10807        debug_print(' Update: Before updating record');
10808        debug_print(' After rounding reservation is' || l_orig_rsv_tbl(1).reservation_id);
10809        debug_print(' After rounding reservation qty' || l_to_rsv_rec.reservation_quantity);
10810        debug_print(' After rounding reservation pri qty' || l_to_rsv_rec.primary_reservation_quantity);
10811        debug_print(' After rounding reservation sec qty' || l_to_rsv_rec.secondary_reservation_quantity); -- INVCONV
10812        debug_print(' After rounding detailed quantity' || l_to_rsv_rec.detailed_quantity);
10813        debug_print(' After rounding sec detailed quantity' || l_to_rsv_rec.secondary_detailed_quantity); --INVCONV
10814     END IF;
10815 
10816     -- INVCONV - Upgrade to incorporate secondaries
10817     mtl_reservations_pkg.update_row
10818       (
10819        x_reservation_id             => l_orig_rsv_tbl(1).reservation_id
10820        , x_requirement_date           => l_to_rsv_rec.requirement_date
10821        , x_organization_id            => l_to_rsv_rec.organization_id
10822        , x_inventory_item_id          => l_to_rsv_rec.inventory_item_id
10823        , x_demand_source_type_id      => l_to_rsv_rec.demand_source_type_id
10824        , x_demand_source_name         => l_to_rsv_rec.demand_source_name
10825        , x_demand_source_header_id    => l_to_rsv_rec.demand_source_header_id
10826        , x_demand_source_line_id      => l_to_rsv_rec.demand_source_line_id
10827        , x_demand_source_delivery     => l_to_rsv_rec.demand_source_delivery
10828        , x_primary_uom_code           => l_to_rsv_rec.primary_uom_code
10829        , x_primary_uom_id             => l_to_rsv_rec.primary_uom_id
10830        , x_secondary_uom_code         => l_to_rsv_rec.secondary_uom_code
10831        , x_secondary_uom_id           => l_to_rsv_rec.secondary_uom_id
10832        , x_reservation_uom_code       => l_to_rsv_rec.reservation_uom_code
10833       , x_reservation_uom_id         => l_to_rsv_rec.reservation_uom_id
10834       , x_reservation_quantity       => l_to_rsv_rec.reservation_quantity
10835       , x_primary_reservation_quantity=> l_to_rsv_rec.primary_reservation_quantity
10836       , x_second_reservation_quantity=> l_to_rsv_rec.secondary_reservation_quantity
10837       , x_detailed_quantity          => l_to_rsv_rec.detailed_quantity
10838       , x_secondary_detailed_quantity=> l_to_rsv_rec.secondary_detailed_quantity
10839       , x_autodetail_group_id        => l_to_rsv_rec.autodetail_group_id
10840       , x_external_source_code       => l_to_rsv_rec.external_source_code
10841       , x_external_source_line_id    => l_to_rsv_rec.external_source_line_id
10842       , x_supply_source_type_id      => l_to_rsv_rec.supply_source_type_id
10843       , x_supply_source_header_id    => l_to_rsv_rec.supply_source_header_id
10844       , x_supply_source_line_id      => l_to_rsv_rec.supply_source_line_id
10845       , x_supply_source_line_detail  => l_to_rsv_rec.supply_source_line_detail
10846       , x_supply_source_name         => l_to_rsv_rec.supply_source_name
10847       , x_revision                   => l_to_rsv_rec.revision
10848       , x_subinventory_code          => l_to_rsv_rec.subinventory_code
10849       , x_subinventory_id            => l_to_rsv_rec.subinventory_id
10850       , x_locator_id                 => l_to_rsv_rec.locator_id
10851       , x_lot_number                 => l_to_rsv_rec.lot_number
10852       , x_lot_number_id              => l_to_rsv_rec.lot_number_id
10853       , x_serial_number              => NULL
10854       , x_serial_number_id           => NULL
10855       , x_partial_quantities_allowed => NULL
10856       , x_auto_detailed              => NULL
10857       , x_pick_slip_number           => l_to_rsv_rec.pick_slip_number
10858       , x_lpn_id                     => l_to_rsv_rec.lpn_id
10859       , x_last_update_date           => l_date
10860       , x_last_updated_by            => l_user_id
10861       , x_last_update_login          => l_login_id
10862       , x_request_id                 => l_request_id
10863       , x_program_application_id     => l_prog_appl_id
10864       , x_program_id                 => l_program_id
10865       , x_program_update_date        => l_date
10866       , x_attribute_category         => l_to_rsv_rec.attribute_category
10867       , x_attribute1                 => l_to_rsv_rec.attribute1
10868       , x_attribute2                 => l_to_rsv_rec.attribute2
10869       , x_attribute3                 => l_to_rsv_rec.attribute3
10870       , x_attribute4                 => l_to_rsv_rec.attribute4
10871       , x_attribute5                 => l_to_rsv_rec.attribute5
10872       , x_attribute6                 => l_to_rsv_rec.attribute6
10873       , x_attribute7                 => l_to_rsv_rec.attribute7
10874       , x_attribute8                 => l_to_rsv_rec.attribute8
10875       , x_attribute9                 => l_to_rsv_rec.attribute9
10876       , x_attribute10                => l_to_rsv_rec.attribute10
10877       , x_attribute11                => l_to_rsv_rec.attribute11
10878       , x_attribute12                => l_to_rsv_rec.attribute12
10879       , x_attribute13                => l_to_rsv_rec.attribute13
10880       , x_attribute14                => l_to_rsv_rec.attribute14
10881       , x_attribute15                => l_to_rsv_rec.attribute15
10882       , x_ship_ready_flag            => l_to_rsv_rec.ship_ready_flag
10883       , x_staged_flag                => l_to_rsv_rec.staged_flag
10884       /**** {{ R12 Enhanced reservations code changes }}****/
10885       , x_crossdock_flag             => l_to_rsv_rec.crossdock_flag
10886       , x_crossdock_criteria_id      => l_to_rsv_rec.crossdock_criteria_id
10887       , x_demand_source_line_detail  => l_to_rsv_rec.demand_source_line_detail
10888       , x_serial_reservation_quantity => l_to_rsv_rec.serial_reservation_quantity
10889       , x_supply_receipt_date        => l_to_rsv_rec.supply_receipt_date
10890       , x_demand_ship_date           => l_to_rsv_rec.demand_ship_date
10891       , x_project_id                 => l_to_rsv_rec.project_id
10892       , x_task_id                    => l_to_rsv_rec.task_id
10893       /*** End R12 ***/
10894 
10895       );
10896     -- for data sync b/w mtl_demand and mtl_reservations
10897     IF (l_debug = 1) THEN
10898        debug_print('Calling inv_rsv_synch.for_update');
10899     END IF;
10900     inv_rsv_synch.for_update(p_reservation_id => l_orig_rsv_tbl(1).reservation_id, x_return_status => l_return_status, x_msg_count => x_msg_count, x_msg_data => x_msg_data);
10901 
10902     IF l_return_status = fnd_api.g_ret_sts_error THEN
10903        RAISE fnd_api.g_exc_error;
10904     END IF;
10905 
10906     --
10907     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10908        RAISE fnd_api.g_exc_unexpected_error;
10909     END IF;
10910 
10911     --
10912     -- Post Update CTO Validation
10913     IF l_to_rsv_rec.demand_source_type_id IN (inv_reservation_global.g_source_type_oe, inv_reservation_global.g_source_type_internal_ord, inv_reservation_global.g_source_type_rma) THEN
10914        --
10915        IF l_orig_rsv_tbl(1).primary_reservation_quantity > l_to_rsv_rec.primary_reservation_quantity THEN
10916 	  IF (l_debug = 1) THEN
10917 	     debug_print('Calling cto_workflow_api_pk.wf_update_after_inv_unreserv');
10918 	  END IF;
10919 	  cto_workflow_api_pk.wf_update_after_inv_unreserv
10920 	    (
10921 	     p_order_line_id => l_to_rsv_rec.demand_source_line_id
10922 	     , x_return_status => l_return_status
10923 	     , x_msg_count => x_msg_count
10924 	     , x_msg_data => x_msg_data
10925 	     );
10926 	ELSE --Else Condition Added for Bug#2467387.
10927 	  IF (l_debug = 1) THEN
10928 	     debug_print('Calling cto_workflow_api_pk.wf_update_after_inv_reserv');
10929 	  END IF;
10930           cto_workflow_api_pk.wf_update_after_inv_reserv
10931 	    (
10932 	     p_order_line_id => l_to_rsv_rec.demand_source_line_id
10933 	     ,x_return_status => l_return_status
10934 	     ,x_msg_count => x_msg_count
10935 	     ,x_msg_data => x_msg_data
10936 	     );
10937        END IF;
10938        --
10939        IF l_return_status = fnd_api.g_ret_sts_error THEN
10940           RAISE fnd_api.g_exc_error;
10941        END IF;
10942 
10943        --
10944        IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10945           RAISE fnd_api.g_exc_unexpected_error;
10946        END IF;
10947        --
10948     END IF;
10949 
10950     /**** {{ R12 Enhanced reservations code changes. Should be
10951     -- releasing the locks. }} *****/
10952     IF l_lock_obtained THEN
10953        inv_reservation_lock_pvt.release_lock
10954 	 (l_supply_lock_handle);
10955        inv_reservation_lock_pvt.release_lock
10956 	 (l_demand_lock_handle);
10957     END IF;
10958     /*** End R12 ***/
10959 
10960     x_return_status  := l_return_status;
10961     x_quantity_reserved:=l_quantity_reserved;
10962     -- INVCONV BEGIN
10963     IF l_dual_control_flag = 'Y' THEN
10964        x_secondary_quantity_reserved:=l_secondary_quantity_reserved;
10965     END IF;
10966     -- INVCONV END
10967     EXCEPTION
10968        WHEN fnd_api.g_exc_error THEN
10969 	  IF (l_debug = 1) THEN
10970 	     debug_print('Exception');
10971 	  END IF;
10972 	  ROLLBACK TO update_reservation_sa;
10973 	  x_return_status  := fnd_api.g_ret_sts_error;
10974 	  /**** {{ R12 Enhanced reservations code changes. Should be
10975 	  -- releasing the locks. }} *****/
10976 	  IF l_lock_obtained THEN
10977 	     inv_reservation_lock_pvt.release_lock
10978 	       (l_supply_lock_handle);
10979 	     inv_reservation_lock_pvt.release_lock
10980 	       (l_demand_lock_handle);
10981 	  END IF;
10982 	  /*** End R12 ***/
10983 	  --  Get message count and data
10984 	  fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
10985        WHEN fnd_api.g_exc_unexpected_error THEN
10986 	  IF (l_debug = 1) THEN
10987 	     debug_print('Exception Unexpected');
10988 	  END IF;
10989 	  ROLLBACK TO update_reservation_sa;
10990 	  x_return_status  := fnd_api.g_ret_sts_unexp_error;
10991 	  /**** {{ R12 Enhanced reservations code changes. Should be
10992 	  -- releasing the locks. }} *****/
10993 	  IF l_lock_obtained THEN
10994 	     inv_reservation_lock_pvt.release_lock
10995 	       (l_supply_lock_handle);
10996 	     inv_reservation_lock_pvt.release_lock
10997 	       (l_demand_lock_handle);
10998 	  END IF;
10999 	  /*** End R12 ***/
11000 	  --  Get message count and data
11001 	  fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
11002        WHEN OTHERS THEN
11003 	  IF (l_debug = 1) THEN
11004 	     debug_print('Exception Others'|| SQLERRM);
11005 	  END IF;
11006 	  ROLLBACK TO update_reservation_sa;
11007 	  x_return_status  := fnd_api.g_ret_sts_unexp_error;
11008 	  /**** {{ R12 Enhanced reservations code changes. Should be
11009 	  -- releasing the locks. }} *****/
11010 	  IF l_lock_obtained THEN
11011 	     inv_reservation_lock_pvt.release_lock
11012 	       (l_supply_lock_handle);
11013 	     inv_reservation_lock_pvt.release_lock
11014 	       (l_demand_lock_handle);
11015 	  END IF;
11016 	  /*** End R12 ***/
11017 	  IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
11018 	     fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
11019 	  END IF;
11020 
11021 	  --  Get message count and data
11022 	  fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
11023     END update_reservation;
11024 
11025   --
11026   PROCEDURE relieve_reservation(
11027     p_api_version_number        IN     NUMBER
11028   , p_init_msg_lst              IN     VARCHAR2 DEFAULT fnd_api.g_false
11029   , x_return_status             OUT    NOCOPY VARCHAR2
11030   , x_msg_count                 OUT    NOCOPY NUMBER
11031   , x_msg_data                  OUT    NOCOPY VARCHAR2
11032   , p_rsv_rec                   IN     inv_reservation_global.mtl_reservation_rec_type
11033   , p_primary_relieved_quantity IN     NUMBER
11034   , p_secondary_relieved_quantity IN     NUMBER                                     -- INVCONV
11035   , p_relieve_all               IN     VARCHAR2 DEFAULT fnd_api.g_true
11036   , p_original_serial_number    IN     inv_reservation_global.serial_number_tbl_type
11037   , p_validation_flag           IN     VARCHAR2 DEFAULT fnd_api.g_true
11038   , x_primary_relieved_quantity OUT    NOCOPY NUMBER
11039   , x_secondary_relieved_quantity  OUT    NOCOPY NUMBER 			    -- INVCONV
11040   , x_primary_remain_quantity   OUT    NOCOPY NUMBER
11041   , x_secondary_remain_quantity OUT    NOCOPY NUMBER                                -- INVCONV
11042   ) IS
11043     l_api_version_number          CONSTANT NUMBER       := 1.0;
11044     l_api_name                    CONSTANT VARCHAR2(30) := 'Relieve_Reservation';
11045     l_return_status               VARCHAR2(1)           := fnd_api.g_ret_sts_success;
11046     l_tmp_rsv_tbl                 inv_reservation_global.mtl_reservation_tbl_type;
11047     l_tmp_rsv_tbl_count           NUMBER;
11048     l_reservation_id              NUMBER;
11049     l_dummy_rsv_rec               inv_reservation_global.mtl_reservation_rec_type;
11050     l_dummy_serial_array          inv_reservation_global.serial_number_tbl_type;
11051     --
11052     l_orig_item_cache_index       INTEGER                                         := NULL;
11053     l_orig_org_cache_index        INTEGER                                         := NULL;
11054     l_orig_demand_cache_index     INTEGER                                         := NULL;
11055     l_orig_supply_cache_index     INTEGER                                         := NULL;
11056     l_orig_sub_cache_index        INTEGER                                         := NULL;
11057     l_to_item_cache_index         INTEGER                                         := NULL;
11058     l_to_org_cache_index          INTEGER                                         := NULL;
11059     l_to_demand_cache_index       INTEGER                                         := NULL;
11060     l_to_supply_cache_index       INTEGER                                         := NULL;
11061     l_to_sub_cache_index          INTEGER                                         := NULL;
11062     l_tree_id                     NUMBER;
11063     l_error_code                  NUMBER;
11064     l_primary_relieved_quantity   NUMBER;
11065     l_secondary_relieved_quantity NUMBER;                                         -- INVCONV
11066     l_primary_remain_quantity     NUMBER;
11067     l_secondary_remain_quantity   NUMBER;                                         -- INVCONV
11068     l_date                        DATE;
11069     l_user_id                     NUMBER;
11070     l_request_id                  NUMBER;
11071     l_login_id                    NUMBER;
11072     l_prog_appl_id                NUMBER;
11073     l_program_id                  NUMBER;
11074     l_qty_changed                 NUMBER;
11075     l_secondary_qty_changed       NUMBER;					 -- INVCONV
11076     l_debug number;
11077     l_lot_divisible_flag          VARCHAR2(1);                                   -- INVCONV
11078     l_dual_control_flag		  VARCHAR2(1)			:= 'N';          -- INVCONV
11079     /*** {{ R12 Enhanced reservations code changes ***/
11080     l_count                       NUMBER;
11081     l_count_to_unrsv_serials      NUMBER :=0;
11082     l_serial_number_table         inv_reservation_global.serial_number_tbl_type;
11083     l_rsv_rec                     inv_reservation_global.mtl_reservation_rec_type;
11084     /*** End R12 }} ***/
11085   BEGIN
11086     --  Standard call to check for call compatibility
11087     IF NOT fnd_api.compatible_api_call(l_api_version_number, p_api_version_number, l_api_name, g_pkg_name) THEN
11088       RAISE fnd_api.g_exc_unexpected_error;
11089     END IF;
11090 
11091     --
11092     --  Initialize message list.
11093     IF fnd_api.to_boolean(p_init_msg_lst) THEN
11094       fnd_msg_pub.initialize;
11095     END IF;
11096 
11097     --
11098     SAVEPOINT relieve_reservation_sa;
11099     --
11100      -- Use cache to get value for l_debug
11101      IF g_is_pickrelease_set IS NULL THEN
11102         g_is_pickrelease_set := 2;
11103         IF INV_CACHE.is_pickrelease THEN
11104            g_is_pickrelease_set := 1;
11105         END IF;
11106      END IF;
11107      IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
11108         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
11109      END IF;
11110 
11111      l_debug := g_debug;
11112 
11113     /**** {{ R12 Enhanced reservations code changes ****/
11114     -- Set the original columns to g_miss_xxx as the user should not be
11115     -- setting these values. Do not query by them
11116     l_rsv_rec := p_rsv_rec;
11117 
11118     l_rsv_rec.orig_supply_source_type_id := fnd_api.g_miss_num;
11119     l_rsv_rec.orig_supply_source_header_id := fnd_api.g_miss_num;
11120     l_rsv_rec.orig_supply_source_line_id := fnd_api.g_miss_num;
11121     l_rsv_rec.orig_supply_source_line_detail := fnd_api.g_miss_num;
11122     l_rsv_rec.orig_demand_source_type_id := fnd_api.g_miss_num;
11123     l_rsv_rec.orig_demand_source_header_id := fnd_api.g_miss_num;
11124     l_rsv_rec.orig_demand_source_line_id := fnd_api.g_miss_num;
11125     l_rsv_rec.orig_demand_source_line_detail := fnd_api.g_miss_num;
11126 
11127     /*** End R12 }} ***/
11128 
11129      IF (l_debug = 1) THEN
11130         debug_print('calling query_reservation');
11131      END IF;
11132     query_reservation(
11133       p_api_version_number         => 1.0
11134     , p_init_msg_lst               => fnd_api.g_false
11135     , x_return_status              => l_return_status
11136     , x_msg_count                  => x_msg_count
11137     , x_msg_data                   => x_msg_data
11138     , p_query_input                => l_rsv_rec         /*** {{ R12 Enhanced reservations code changes ***/
11139     , p_lock_records               => fnd_api.g_true
11140     , x_mtl_reservation_tbl        => l_tmp_rsv_tbl
11141     , x_mtl_reservation_tbl_count  => l_tmp_rsv_tbl_count
11142     , x_error_code                 => l_error_code
11143     );
11144 
11145     --
11146     IF l_return_status = fnd_api.g_ret_sts_error THEN
11147      	IF (l_debug = 1) THEN
11148         	debug_print( 'error from query reservation');
11149      	END IF;
11150       RAISE fnd_api.g_exc_error;
11151     END IF;
11152 
11153     --
11154     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11155       IF (l_debug = 1) THEN
11156          debug_print('error from query reservation');
11157       END IF;
11158       RAISE fnd_api.g_exc_unexpected_error;
11159     END IF;
11160 
11161     --
11162     IF l_tmp_rsv_tbl_count = 0 THEN
11163       IF (l_debug = 1) THEN
11164          debug_print('reservation not found ');
11165       END IF;
11166       fnd_message.set_name('INV', 'INV-ROW NOT FOUND');
11167       fnd_msg_pub.ADD;
11168       RAISE fnd_api.g_exc_error;
11169     END IF;
11170 
11171     --
11172     IF l_tmp_rsv_tbl_count > 1 THEN
11173       IF (l_debug = 1) THEN
11174          debug_print('found more than one row reservation');
11175       END IF;
11176       fnd_message.set_name('INV', 'INV-RELIEVE MORE THAN ONE ROW');
11177       fnd_msg_pub.ADD;
11178       RAISE fnd_api.g_exc_error;
11179     END IF;
11180 
11181     --
11182     l_reservation_id             := l_tmp_rsv_tbl(1).reservation_id;
11183     IF (l_debug = 1) THEN
11184        debug_print('reservation id is ' || l_reservation_id);
11185     END IF;
11186 
11187     IF p_validation_flag = fnd_api.g_true THEN
11188          -- validation is needed to get revision, lot,
11189          --serial control information
11190          -- for creating the quantity tree
11191 
11192        -- added for crossdock reservation, pass the to reservation record
11193        -- with values instead of dummy record with the remain quantity
11194        -- populated to the record.
11195        IF ((l_tmp_rsv_tbl(1).crossdock_criteria_id is not null) and
11196 	   (l_tmp_rsv_tbl(1).crossdock_criteria_id <> fnd_api.g_miss_num)) THEN
11197 	  IF (l_debug = 1) THEN
11198 	     debug_print('assigning cross dock to record');
11199 	  END IF;
11200 
11201 	  IF (p_relieve_all = fnd_api.g_false) THEN
11202 	     IF (l_debug = 1) THEN
11203 		debug_print('relieve all is false');
11204 	     END IF;
11205 	     l_primary_remain_quantity :=
11206 	       l_tmp_rsv_tbl(1).primary_reservation_quantity -
11207 	       Nvl(p_primary_relieved_quantity,0);
11208 	   ELSE
11209 	     IF (l_debug = 1) THEN
11210 		debug_print('relieve all is true');
11211 	     END IF;
11212 	     l_primary_remain_quantity := 0;
11213 	  END IF;
11214 
11215 	  l_dummy_rsv_rec := l_tmp_rsv_tbl(1);
11216 	  IF (l_debug = 1) THEN
11217 	     debug_print('from rec primary reservation qty' || l_tmp_rsv_tbl(1).primary_reservation_quantity);
11218 	     debug_print('to rec primary reservation qty' || l_primary_remain_quantity);
11219 	  END IF;
11220           l_dummy_rsv_rec.primary_reservation_quantity := l_primary_remain_quantity;
11221 	  l_dummy_rsv_rec.reservation_quantity := null;
11222 	  --l_dummy_rsv_rec.secondary_reservation_quantity := l_secondary_remain_quantity;
11223        END IF;
11224 
11225        IF (l_debug = 1) THEN
11226           debug_print('calling validate_input_parameters');
11227        END IF;
11228 
11229       inv_reservation_validate_pvt.validate_input_parameters(
11230         x_return_status              => l_return_status
11231       , p_orig_rsv_rec               => l_tmp_rsv_tbl(1)
11232       , p_to_rsv_rec                 => l_dummy_rsv_rec
11233       , p_orig_serial_array          => p_original_serial_number
11234       , p_to_serial_array            => l_dummy_serial_array
11235       , p_rsv_action_name            => 'RELIEVE'
11236       , x_orig_item_cache_index      => l_orig_item_cache_index
11237       , x_orig_org_cache_index       => l_orig_org_cache_index
11238       , x_orig_demand_cache_index    => l_orig_demand_cache_index
11239       , x_orig_supply_cache_index    => l_orig_supply_cache_index
11240       , x_orig_sub_cache_index       => l_orig_sub_cache_index
11241       , x_to_item_cache_index        => l_to_item_cache_index
11242       , x_to_org_cache_index         => l_to_org_cache_index
11243       , x_to_demand_cache_index      => l_to_demand_cache_index
11244       , x_to_supply_cache_index      => l_to_supply_cache_index
11245       , x_to_sub_cache_index         => l_to_sub_cache_index
11246       );
11247 
11248       --
11249       IF l_return_status = fnd_api.g_ret_sts_error THEN
11250          IF (l_debug = 1) THEN
11251             debug_print('Error in validate_input_parameters');
11252          END IF;
11253         RAISE fnd_api.g_exc_error;
11254       END IF;
11255 
11256       --
11257       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11258          IF (l_debug = 1) THEN
11259             debug_print('Unexpected error in validate_input_parameters');
11260          END IF;
11261         RAISE fnd_api.g_exc_unexpected_error;
11262       END IF;
11263 
11264       --
11265        -- INVCONV BEGIN
11266       IF is_lot_divisible(l_orig_item_cache_index) THEN
11267         l_lot_divisible_flag := 'Y';
11268       END IF;
11269       -- INVCONV END
11270       --
11271       -- check quantity
11272       IF  p_primary_relieved_quantity IS NULL
11273           AND p_relieve_all <> fnd_api.g_true THEN
11274         IF (l_debug = 1) THEN
11275            debug_print('relieve_quantity_not_specified');
11276         END IF;
11277         fnd_message.set_name('INV', 'RELIEVE_QUANTITY_NOT_SPECIFIED');
11278         fnd_msg_pub.ADD;
11279         RAISE fnd_api.g_exc_error;
11280       END IF;
11281 
11282       IF p_primary_relieved_quantity > l_tmp_rsv_tbl(1).primary_reservation_quantity THEN
11283         IF (l_debug = 1) THEN
11284            debug_print('relieve_more_than_reserved');
11285         END IF;
11286         fnd_message.set_name('INV', 'RELIEVE_MORE_THAN_RESERVED');
11287         fnd_msg_pub.ADD;
11288         RAISE fnd_api.g_exc_unexpected_error;
11289       END IF;
11290 
11291       IF is_dual_control(l_orig_item_cache_index) THEN
11292         l_dual_control_flag := 'Y';
11293       END IF;
11294 
11295       IF p_relieve_all = fnd_api.g_true THEN
11296         l_primary_relieved_quantity  := l_tmp_rsv_tbl(1).primary_reservation_quantity;
11297         l_primary_remain_quantity    := 0;
11298         -- INVCONV BEGIN
11299         IF l_dual_control_flag = 'Y' THEN
11300           l_secondary_relieved_quantity  := l_tmp_rsv_tbl(1).secondary_reservation_quantity;
11301           l_secondary_remain_quantity    := 0;
11302         END IF;
11303         -- INVCONV END
11304       ELSE
11305         l_primary_relieved_quantity  := p_primary_relieved_quantity;
11306         l_primary_remain_quantity    := l_tmp_rsv_tbl(1).primary_reservation_quantity - l_primary_relieved_quantity;
11307         -- INVCONV BEGIN
11308         IF l_dual_control_flag = 'Y' THEN
11309           l_secondary_relieved_quantity := p_secondary_relieved_quantity;
11310           l_secondary_remain_quantity   := l_tmp_rsv_tbl(1).secondary_reservation_quantity - l_secondary_relieved_quantity;
11311         END IF;
11312         -- INVCONV END
11313       END IF;
11314 
11315       IF l_tmp_rsv_tbl(1).supply_source_type_id = inv_reservation_global.g_source_type_inv THEN
11316            -- call quantity processor to
11317            -- modify the tree so that the quantity tree
11318            -- reflect the deletion
11319         IF (l_debug = 1) THEN
11320            debug_print('calling inv_quantity_tree_pvt.create_tree');
11321         END IF;
11322         inv_quantity_tree_pvt.create_tree(
11323           p_api_version_number         => 1.0
11324         , p_init_msg_lst               => fnd_api.g_true
11325         , x_return_status              => l_return_status
11326         , x_msg_count                  => x_msg_count
11327         , x_msg_data                   => x_msg_data
11328         , p_organization_id            => l_tmp_rsv_tbl(1).organization_id
11329         , p_inventory_item_id          => l_tmp_rsv_tbl(1).inventory_item_id
11330         , p_tree_mode                  => inv_quantity_tree_pvt.g_reservation_mode
11331         , p_is_revision_control        => is_revision_control(l_orig_item_cache_index)
11332         , p_is_lot_control             => is_lot_control(l_orig_item_cache_index)
11333         , p_is_serial_control          => is_serial_control(l_orig_item_cache_index)
11334         , p_asset_sub_only             => FALSE
11335         , p_include_suggestion         => TRUE
11336         , p_demand_source_type_id      => l_tmp_rsv_tbl(1).demand_source_type_id
11337         , p_demand_source_header_id    => l_tmp_rsv_tbl(1).demand_source_header_id
11338         , p_demand_source_line_id      => l_tmp_rsv_tbl(1).demand_source_line_id
11339         , p_demand_source_name         => l_tmp_rsv_tbl(1).demand_source_name
11340         , p_demand_source_delivery     => l_tmp_rsv_tbl(1).demand_source_delivery
11341         , p_lot_expiration_date        => NULL
11342         , x_tree_id                    => l_tree_id
11343         );
11344 
11345         IF l_return_status = fnd_api.g_ret_sts_error THEN
11346          IF (l_debug = 1) THEN
11347             debug_print('error calling inv_quantity_tree_pvt.create_tree');
11348          END IF;
11349           RAISE fnd_api.g_exc_error;
11350         END IF;
11351 
11352         --
11353         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11354             IF (l_debug = 1) THEN
11355                debug_print('unexpected error calling inv_quantity_tree_pvt.create_tree');
11356             END IF;
11357           RAISE fnd_api.g_exc_unexpected_error;
11358         END IF;
11359 
11360         IF (l_debug = 1) THEN
11361            debug_print('calling modify_tree_crt_del_rel');
11362         END IF;
11363         -- INVCONV - Upgrade call to incorporate secondaries
11364         modify_tree_crt_del_rel(
11365           x_return_status              => l_return_status
11366         , p_tree_id                    => l_tree_id
11367         , p_revision                   => l_tmp_rsv_tbl(1).revision
11368         , p_lot_number                 => l_tmp_rsv_tbl(1).lot_number
11369         , p_subinventory_code          => l_tmp_rsv_tbl(1).subinventory_code
11370         , p_locator_id                 => l_tmp_rsv_tbl(1).locator_id
11371         , p_lpn_id                     => l_tmp_rsv_tbl(1).lpn_id
11372         , p_primary_reservation_quantity=> l_tmp_rsv_tbl(1).primary_reservation_quantity
11373         , p_second_reservation_quantity=> l_tmp_rsv_tbl(1).secondary_reservation_quantity
11374         , p_detailed_quantity          => l_tmp_rsv_tbl(1).detailed_quantity
11375         , p_secondary_detailed_quantity=> l_tmp_rsv_tbl(1).secondary_detailed_quantity
11376         , p_relieve_quantity           => l_primary_relieved_quantity
11377         , p_secondary_relieve_quantity => l_secondary_relieved_quantity
11378         , p_partial_reservation_flag   => fnd_api.g_false
11379         , p_force_reservation_flag     => fnd_api.g_false
11380         , p_lot_divisible_flag         => l_lot_divisible_flag      -- INVCONV
11381         , p_action                     => 'RELIEVE'
11382         , x_qty_changed                => l_qty_changed
11383         , x_secondary_qty_changed      => l_secondary_qty_changed
11384         );
11385 
11386         --
11387         IF l_return_status = fnd_api.g_ret_sts_error THEN
11388           IF (l_debug = 1) THEN
11389              debug_print('error calling modify_tree_crt_del_rel');
11390           END IF;
11391           RAISE fnd_api.g_exc_error;
11392         END IF;
11393 
11394         --
11395         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11396           IF (l_debug = 1) THEN
11397              debug_print('unexpected error calling modify_tree_crt_del_rel');
11398           END IF;
11399           RAISE fnd_api.g_exc_unexpected_error;
11400         END IF;
11401       --
11402       END IF;
11403     END IF;
11404 
11405      -- delete the reservation from the db table
11406     IF l_primary_remain_quantity = 0 THEN
11407 
11408        --Bug 3830160: Added a call to delete the demand if the entire
11409        --reservation is relieved. Code to delete mtl_demand.
11410 
11411        IF (l_debug = 1) THEN
11412           debug_print('calling inv_rsv_synch.for_delete');
11413        END IF;
11414 
11415        inv_rsv_synch.for_delete
11416          (
11417           p_reservation_id             => l_reservation_id
11418           , x_return_status              => l_return_status
11419           , x_msg_count                  => x_msg_count
11420           , x_msg_data                   => x_msg_data
11421           );
11422 
11423        IF l_return_status = fnd_api.g_ret_sts_error THEN
11424           IF (l_debug = 1) THEN
11425              debug_print('error calling inv_rsv_synch.for_delete');
11426           END IF;
11427            RAISE fnd_api.g_exc_error;
11428        END IF;
11429 
11430        IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11431           IF (l_debug = 1) THEN
11432              debug_print('unexpected error calling inv_rsv_synch.for_delete');
11433           END IF;
11434           RAISE fnd_api.g_exc_unexpected_error;
11435        END IF;
11436        -- End changes for bug 3827307
11437 
11438        /*** {{ R12 Enhanced reservations code changes ***/
11439        -- update the mtl_serial_numbers, null out the reservation_id
11440        -- and group_mark_id of the reserved serials before delete the
11441        -- reservation. There is no need to update the serial_reservation_quantity
11442        -- because the reservation record will be deleted.
11443        BEGIN
11444          update mtl_serial_numbers
11445          set    reservation_id = NULL,
11446 	   group_mark_id = NULL,
11447 	   line_mark_id = NULL,
11448 	   lot_line_mark_id = NULL
11449          where  reservation_id = l_reservation_id;
11450        EXCEPTION
11451          WHEN NO_DATA_FOUND THEN
11452               IF (l_debug = 1) THEN
11453                  debug_print('No serial numbers stamp with reservation_id ' || l_reservation_id);
11454               END IF;
11455        END;
11456        /*** End R12 }} ***/
11457 
11458        IF (l_debug = 1) THEN
11459           debug_print('calling mtl_reservations_pkg.delete_row');
11460        END IF;
11461        mtl_reservations_pkg.delete_row(x_reservation_id => l_reservation_id);
11462        NULL;
11463 
11464      ELSE
11465 
11466        --Bug 3830160: Moved the relieve demand code inside this block
11467        --so that 'for_relieve' will be called only if there are some pending
11468        --demand and if we are not relieving the entire reservation.
11469 
11470         IF (l_debug = 1) THEN
11471            debug_print('calling inv_rsv_synch.for_relieve');
11472         END IF;
11473 
11474         inv_rsv_synch.for_relieve(
11475           p_reservation_id             => l_reservation_id
11476         , p_primary_relieved_quantity  => l_primary_relieved_quantity
11477         , x_return_status              => l_return_status
11478         , x_msg_count                  => x_msg_count
11479         , x_msg_data                   => x_msg_data
11480         );
11481 
11482         IF l_return_status = fnd_api.g_ret_sts_error THEN
11483           IF (l_debug = 1) THEN
11484              debug_print('error calling inv_rsv_synch.for_relieve');
11485           END IF;
11486           RAISE fnd_api.g_exc_error;
11487         END IF;
11488 
11489         --
11490         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11491            IF (l_debug = 1) THEN
11492               debug_print('unexpected error calling inv_rsv_synch.for_relieve');
11493            END IF;
11494           RAISE fnd_api.g_exc_unexpected_error;
11495         END IF;
11496 
11497         -- End changes Bug3830160.
11498 
11499       l_tmp_rsv_tbl(1).primary_reservation_quantity  := l_tmp_rsv_tbl(1).primary_reservation_quantity - l_primary_relieved_quantity;
11500 
11501       -- INVCONV BEGIN
11502       IF l_dual_control_flag = 'Y' THEN
11503         l_tmp_rsv_tbl(1).secondary_reservation_quantity := l_tmp_rsv_tbl(1).secondary_reservation_quantity - l_secondary_relieved_quantity;
11504       END IF;
11505       -- INVCONV END
11506 
11507       --Bug 2116332 - convert_quantity only updates reservation quantity
11508       -- if that field is NULL.  So, we have to make reservation quantity
11509       -- NULL if we want to populate it from primary_reservation_quantity
11510       l_tmp_rsv_tbl(1).reservation_quantity          := NULL;
11511      	IF (l_debug = 1) THEN
11512         	debug_print('calling convert_quantity');
11513      	END IF;
11514       convert_quantity(x_return_status => l_return_status, px_rsv_rec => l_tmp_rsv_tbl(1));
11515 
11516       --
11517       IF l_return_status = fnd_api.g_ret_sts_error THEN
11518         RAISE fnd_api.g_exc_error;
11519       END IF;
11520 
11521       --
11522       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11523         RAISE fnd_api.g_exc_unexpected_error;
11524       END IF;
11525 
11526       -- obtain program and user info
11527       l_date := SYSDATE;
11528 
11529       --
11530       l_user_id                                      := fnd_global.user_id;
11531       l_login_id                                     := fnd_global.login_id;
11532 
11533       IF l_login_id = -1 THEN
11534         l_login_id  := fnd_global.conc_login_id;
11535       END IF;
11536 
11537       l_request_id                                   := fnd_global.conc_request_id;
11538       l_prog_appl_id                                 := fnd_global.prog_appl_id;
11539       l_program_id                                   := fnd_global.conc_program_id;
11540 
11541       --Adding an extra check to make sure that relieve reservations does not
11542       -- update the record to a negative quantity.
11543       IF (l_debug = 1) THEN
11544         debug_print('Primary_reservation_qty before inserting (relieve)= '
11545 		                || To_char(l_tmp_rsv_tbl(1).primary_reservation_quantity) );
11546         debug_print('Secondary_reservation_qty before inserting (relieve)= '
11547                                 || To_char(l_tmp_rsv_tbl(1).secondary_reservation_quantity) );
11548         debug_print('Reservation_qty before inserting (relieve)= '
11549 		                || To_char(l_tmp_rsv_tbl(1).reservation_quantity) );
11550       END IF;
11551 
11552       IF (  (NVL(l_tmp_rsv_tbl(1).reservation_quantity,0) < 0) OR
11553 	          (NVL(l_tmp_rsv_tbl(1).primary_reservation_quantity,0) < 0) ) THEN
11554         fnd_message.set_name('INV', 'INV-INVALID RESERVATION QTY');
11555         fnd_msg_pub.ADD;
11556         RAISE fnd_api.g_exc_error;
11557       END IF;
11558 
11559       -- INVCONV BEGIN
11560       IF (NVL(l_tmp_rsv_tbl(1).secondary_reservation_quantity,0) < 0) THEN
11561         fnd_message.set_name('INV', 'INV-INVALID NEGATIVE SECONDARY');
11562         fnd_msg_pub.ADD;
11563         RAISE fnd_api.g_exc_error;
11564       END IF;
11565       -- INVCONV END
11566 
11567       /*** {{ R12 Enhanced reservations code changes ***/
11568       -- if serial record is not empty, then unmark the group_mark_id and
11569       -- reservation_id in mtl_serial_numbers of the serial numbers pass in the record.
11570       -- also update the serial_reservation_quantity in the mtl_reservations.
11571       -- if serial record is empty, need to get the serials with reservation_id and
11572       -- unmark the serial until the serial_reservation_quantity = primary_reservation_quantity
11573       IF (p_original_serial_number.COUNT > 0) THEN
11574 	 l_count := 0;
11575 	 IF (l_debug = 1) THEN
11576 	    debug_print('Inside relieve serial numbers');
11577 	 END IF;
11578 	 FOR i in 1..p_original_serial_number.COUNT LOOP
11579 	    IF (l_debug = 1) THEN
11580 	       debug_print('serial number' ||
11581 			   p_original_serial_number(i).serial_number);
11582 	       debug_print('item ' || l_tmp_rsv_tbl(1).inventory_item_id);
11583 	    END IF;
11584              BEGIN
11585                 UPDATE mtl_serial_numbers
11586 		  SET reservation_id = NULL,
11587 		  group_mark_id = NULL, line_mark_id = NULL,
11588 		  lot_line_mark_id = NULL
11589 		  WHERE  serial_number = p_original_serial_number(i).serial_number
11590 		  AND    inventory_item_id =
11591 		  l_tmp_rsv_tbl(1).inventory_item_id
11592 		  AND current_organization_id = l_tmp_rsv_tbl(1).organization_id;
11593              EXCEPTION
11594                 WHEN no_data_found THEN
11595                    IF (l_debug = 1) THEN
11596 		      debug_print('No serials found for serial number ' || p_original_serial_number(i).serial_number);
11597                    END IF;
11598              END;
11599              l_count := l_count + 1;
11600 	     IF (l_debug = 1) THEN
11601 		debug_print('relieved serial count: ' || l_count);
11602 	     END IF;
11603 	 END LOOP;
11604 
11605 	 IF (l_tmp_rsv_tbl(1).primary_reservation_quantity >=
11606 	     Nvl(l_tmp_rsv_tbl(1).serial_reservation_quantity,0) - l_count) THEN
11607 	    l_tmp_rsv_tbl(1).serial_reservation_quantity :=
11608 	      l_tmp_rsv_tbl(1).serial_reservation_quantity - l_count;
11609 	    IF (l_debug = 1) THEN
11610 	       debug_print('Inside rsv > serial count: ' ||
11611 			   l_tmp_rsv_tbl(1).primary_reservation_quantity
11612 			   || ':' ||
11613 			   l_tmp_rsv_tbl(1).serial_reservation_quantity ||
11614 			   ':' || l_count);
11615 	    END IF;
11616           ELSE
11617               -- need to unreserved more serials until the serial_reservation_quantity
11618 	     -- is less than or equal to primary_reservation_quantity
11619 	     IF (l_debug = 1) THEN
11620 		debug_print('Inside serial count > rsv : ' ||
11621 			    l_tmp_rsv_tbl(1).primary_reservation_quantity
11622 			    || ':' ||
11623 			    l_tmp_rsv_tbl(1).serial_reservation_quantity ||
11624 			    ':' || l_count);
11625 	     END IF;
11626               l_count_to_unrsv_serials := (Nvl(l_tmp_rsv_tbl(1).serial_reservation_quantity,0) - l_count)-l_tmp_rsv_tbl(1).primary_reservation_quantity;
11627 
11628 	      IF (l_debug = 1) THEN
11629 		 debug_print('total serials to be unreserved' ||
11630 			     l_count_to_unrsv_serials);
11631 		 debug_print('reservation id ' || l_reservation_id);
11632 	      END IF;
11633 
11634               BEGIN
11635                  SELECT inventory_item_id,serial_number BULK COLLECT
11636                  INTO   l_serial_number_table
11637                  FROM   mtl_serial_numbers
11638                  WHERE  reservation_id = l_reservation_id;
11639               EXCEPTION
11640                  WHEN no_data_found THEN
11641                     IF (l_debug = 1) THEN
11642                         debug_print('No serials found for reservation record. id = ' || l_reservation_id);
11643                     END IF;
11644                     fnd_message.set_name('INV', 'INV_SR_EXCEED_RSV_QTY');
11645                     fnd_msg_pub.add;
11646                     RAISE fnd_api.g_exc_error;
11647               END;
11648 
11649 	      IF (l_debug = 1) THEN
11650 		 debug_print('After query serials');
11651 		 debug_print('l_serial_number_table.COUNT' ||
11652 			     l_serial_number_table.COUNT);
11653 
11654 	      END IF;
11655 
11656               IF (l_serial_number_table.COUNT < l_count_to_unrsv_serials) THEN
11657                   IF (l_debug = 1) THEN
11658                       debug_print('Not enough serials to unreserved for reservation record. id = ' || l_reservation_id);
11659                   END IF;
11660                   fnd_message.set_name('INV', 'INV_SR_EXCEED_RSV_QTY');
11661                   fnd_msg_pub.add;
11662                   RAISE fnd_api.g_exc_error;
11663               END IF;
11664 
11665               FOR i in 1..l_count_to_unrsv_serials LOOP
11666                   BEGIN
11667 		     IF (l_debug = 1) THEN
11668 			debug_print('inside relieve. serial ' ||
11669 				    l_serial_number_table(i).serial_number);
11670 			debug_print('inside relieve. item ' || l_serial_number_table(i).serial_number);
11671 		     END IF;
11672 		     UPDATE mtl_serial_numbers
11673 		       SET reservation_id = NULL,
11674 		       group_mark_id = NULL, line_mark_id = NULL,
11675 		       lot_line_mark_id = NULL
11676 		       WHERE  serial_number = l_serial_number_table(i).serial_number
11677 		       AND inventory_item_id =
11678 		       l_serial_number_table(i).inventory_item_id AND
11679 		       current_organization_id = l_tmp_rsv_tbl(1).organization_id;
11680                   EXCEPTION
11681                      WHEN no_data_found THEN
11682                         IF (l_debug = 1) THEN
11683                             debug_print('No serial found for serial number ' || l_serial_number_table(i).serial_number);
11684                         END IF;
11685                   END;
11686                   l_count := l_count + 1;
11687 		  IF (l_debug = 1) THEN
11688 		     debug_print('l_count' || l_count);
11689 		  END IF;
11690               END LOOP;
11691 
11692               l_tmp_rsv_tbl(1).serial_reservation_quantity :=
11693 		l_tmp_rsv_tbl(1).serial_reservation_quantity - l_count;
11694 	      IF (l_debug = 1) THEN
11695 		 debug_print('final serial count' || l_tmp_rsv_tbl(1).serial_reservation_quantity);
11696 	      END IF;
11697           END IF;
11698       ELSE -- p_original_serial_number is null
11699           IF (l_tmp_rsv_tbl(1).primary_reservation_quantity <
11700                      Nvl(l_tmp_rsv_tbl(1).serial_reservation_quantity,0)) THEN
11701 
11702               -- need to unreserved more serials until the serial_reservation_quantity
11703               -- is less than or equal to primary_reservation_quantity
11704               l_count_to_unrsv_serials :=
11705 		Nvl(l_tmp_rsv_tbl(1).serial_reservation_quantity,0) - l_tmp_rsv_tbl(1).primary_reservation_quantity;
11706 	      IF (l_debug = 1) THEN
11707 		 debug_print('Inside p_original is null. total serials to be unreserved' ||
11708 			     l_count_to_unrsv_serials);
11709 		 debug_print('serial qty ' ||
11710 			     l_tmp_rsv_tbl(1).serial_reservation_quantity);
11711 		 debug_print('rsv qty ' || l_tmp_rsv_tbl(1).primary_reservation_quantity);
11712 	      END IF;
11713 
11714               BEGIN
11715                  SELECT inventory_item_id, serial_number BULK COLLECT
11716                  INTO   l_serial_number_table
11717                  FROM   mtl_serial_numbers
11718                  WHERE  reservation_id = l_reservation_id;
11719               EXCEPTION
11720                  WHEN no_data_found THEN
11721                     IF (l_debug = 1) THEN
11722                         debug_print('No serial found for reservation record. id = ' || l_reservation_id);
11723                     END IF;
11724                     fnd_message.set_name('INV', 'INV_SR_EXCEED_RSV_QTY');
11725                     fnd_msg_pub.add;
11726                     RAISE fnd_api.g_exc_error;
11727               END;
11728 
11729               IF (l_serial_number_table.COUNT < l_count_to_unrsv_serials) THEN
11730                   IF (l_debug = 1) THEN
11731                       debug_print('Not enough serials to unreserved for reservation record. id = ' || l_reservation_id);
11732                   END IF;
11733                   fnd_message.set_name('INV', 'INV_SR_EXCEED_RSV_QTY');
11734                   fnd_msg_pub.add;
11735                   RAISE fnd_api.g_exc_error;
11736               END IF;
11737 
11738               FOR i in 1..l_count_to_unrsv_serials LOOP
11739                   BEGIN
11740                      UPDATE mtl_serial_numbers
11741 		       SET reservation_id = NULL,
11742 		       group_mark_id = NULL, line_mark_id = NULL,
11743 		       lot_line_mark_id = NULL
11744 		       WHERE serial_number = l_serial_number_table(i).serial_number
11745 		       AND inventory_item_id = l_serial_number_table(i).inventory_item_id;
11746                   EXCEPTION
11747                      WHEN no_data_found THEN
11748                         IF (l_debug = 1) THEN
11749 			   debug_print('No serials found for serial number ' || l_serial_number_table(i).serial_number);
11750                         END IF;
11751                   END;
11752               END LOOP;
11753 
11754               l_tmp_rsv_tbl(1).serial_reservation_quantity := l_tmp_rsv_tbl(1).serial_reservation_quantity
11755                                                             - l_count_to_unrsv_serials;
11756           END IF;
11757       END IF;
11758       /*** End R12 }} ***/
11759 
11760          -- update the quantity in a quick and dirty way
11761       IF (l_debug = 1) THEN
11762          debug_print('update reservation');
11763       END IF;
11764 
11765       -- INVCONV - Incorporate secondaries
11766       UPDATE mtl_reservations
11767 	SET primary_reservation_quantity = l_tmp_rsv_tbl(1).primary_reservation_quantity
11768 	, secondary_reservation_quantity = l_tmp_rsv_tbl(1).secondary_reservation_quantity
11769 	, reservation_quantity = l_tmp_rsv_tbl(1).reservation_quantity
11770 	, last_update_date = l_date
11771 	, last_updated_by = l_user_id
11772 	, last_update_login = l_login_id
11773 	, request_id = l_request_id
11774 	, program_application_id = l_prog_appl_id
11775 	, program_id = l_program_id
11776 	, program_update_date = l_date
11777 	/*** {{ R12 Enhanced reservations code changes ***/
11778 	, serial_reservation_quantity =
11779 	l_tmp_rsv_tbl(1).serial_reservation_quantity
11780 	/*** End R12 }} ***/
11781 	WHERE reservation_id = l_reservation_id;
11782     END IF;
11783 
11784     --
11785     x_primary_relieved_quantity  := l_primary_relieved_quantity;
11786     x_primary_remain_quantity    := l_primary_remain_quantity;
11787     -- INVCONV BEGIN
11788     IF l_dual_control_flag = 'Y' THEN
11789       x_secondary_relieved_quantity  := l_secondary_relieved_quantity;
11790       x_secondary_remain_quantity    := l_secondary_remain_quantity;
11791     END IF;
11792     -- INVCONV END
11793     x_return_status              := l_return_status;
11794   --
11795   EXCEPTION
11796     WHEN fnd_api.g_exc_error THEN
11797       ROLLBACK TO relieve_reservation_sa;
11798       x_return_status  := fnd_api.g_ret_sts_error;
11799       --  Get message count and data
11800       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
11801     WHEN fnd_api.g_exc_unexpected_error THEN
11802       ROLLBACK TO relieve_reservation_sa;
11803       x_return_status  := fnd_api.g_ret_sts_unexp_error;
11804       --  Get message count and data
11805       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
11806     WHEN OTHERS THEN
11807       ROLLBACK TO relieve_reservation_sa;
11808       x_return_status  := fnd_api.g_ret_sts_unexp_error;
11809 
11810       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
11811         fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
11812       END IF;
11813 
11814       --  Get message count and data
11815       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
11816   END relieve_reservation;
11817 
11818   --
11819   PROCEDURE delete_reservation(
11820     p_api_version_number     IN     NUMBER
11821   , p_init_msg_lst           IN     VARCHAR2 DEFAULT fnd_api.g_false
11822   , x_return_status          OUT    NOCOPY VARCHAR2
11823   , x_msg_count              OUT    NOCOPY NUMBER
11824   , x_msg_data               OUT    NOCOPY VARCHAR2
11825   , p_rsv_rec                IN     inv_reservation_global.mtl_reservation_rec_type
11826   , p_original_serial_number IN     inv_reservation_global.serial_number_tbl_type
11827   , p_validation_flag        IN     VARCHAR2 DEFAULT fnd_api.g_true
11828   ) IS
11829     l_api_version_number CONSTANT NUMBER                                          := 1.0;
11830     l_api_name           CONSTANT VARCHAR2(30)                                    := 'Delete_Reservation';
11831     l_return_status               VARCHAR2(1)                                     := fnd_api.g_ret_sts_success;
11832     l_tmp_rsv_tbl                 inv_reservation_global.mtl_reservation_tbl_type;
11833     l_tmp_rsv_tbl_count           NUMBER;
11834     l_reservation_id              NUMBER;
11835     l_dummy_rsv_rec               inv_reservation_global.mtl_reservation_rec_type;
11836     l_dummy_serial_array          inv_reservation_global.serial_number_tbl_type;
11837     --
11838     l_orig_item_cache_index       INTEGER                                         := NULL;
11839     l_orig_org_cache_index        INTEGER                                         := NULL;
11840     l_orig_demand_cache_index     INTEGER                                         := NULL;
11841     l_orig_supply_cache_index     INTEGER                                         := NULL;
11842     l_orig_sub_cache_index        INTEGER                                         := NULL;
11843     l_to_item_cache_index         INTEGER                                         := NULL;
11844     l_to_org_cache_index          INTEGER                                         := NULL;
11845     l_to_demand_cache_index       INTEGER                                         := NULL;
11846     l_to_supply_cache_index       INTEGER                                         := NULL;
11847     l_to_sub_cache_index          INTEGER                                         := NULL;
11848     l_tree_id                     NUMBER;
11849     l_error_code                  NUMBER;
11850     l_qty_changed                 NUMBER;
11851     l_secondary_qty_changed       NUMBER;					-- INVCONV
11852     l_debug number;
11853     l_lot_divisible_flag          VARCHAR2(1)                                     :='Y'; -- INVCONV
11854 
11855     /*** {{ R12 Enhanced reservations code changes ***/
11856     l_rsv_rec                     inv_reservation_global.mtl_reservation_rec_type;
11857     /*** End R12 }} ***/
11858   BEGIN
11859      -- Use cache to get value for l_debug
11860      IF g_is_pickrelease_set IS NULL THEN
11861         g_is_pickrelease_set := 2;
11862         IF INV_CACHE.is_pickrelease THEN
11863            g_is_pickrelease_set := 1;
11864         END IF;
11865      END IF;
11866      IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
11867         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
11868      END IF;
11869 
11870      l_debug := g_debug;
11871      IF (l_debug = 1) THEN
11872         debug_print('Inside delete reservation...');
11873      END IF;
11874     --  Standard call to check for call compatibility
11875     IF NOT fnd_api.compatible_api_call(l_api_version_number, p_api_version_number, l_api_name, g_pkg_name) THEN
11876       RAISE fnd_api.g_exc_unexpected_error;
11877     END IF;
11878 
11879     --
11880     --  Initialize message list.
11881     IF fnd_api.to_boolean(p_init_msg_lst) THEN
11882       fnd_msg_pub.initialize;
11883     END IF;
11884 
11885     /**** {{ R12 Enhanced reservations code changes ****/
11886     -- Set the original columns to g_miss_xxx as the user should not be
11887     -- setting these values. Do not query by them
11888     l_rsv_rec := p_rsv_rec;
11889 
11890     l_rsv_rec.orig_supply_source_type_id := fnd_api.g_miss_num;
11891     l_rsv_rec.orig_supply_source_header_id := fnd_api.g_miss_num;
11892     l_rsv_rec.orig_supply_source_line_id := fnd_api.g_miss_num;
11893     l_rsv_rec.orig_supply_source_line_detail := fnd_api.g_miss_num;
11894     l_rsv_rec.orig_demand_source_type_id := fnd_api.g_miss_num;
11895     l_rsv_rec.orig_demand_source_header_id := fnd_api.g_miss_num;
11896     l_rsv_rec.orig_demand_source_line_id := fnd_api.g_miss_num;
11897     l_rsv_rec.orig_demand_source_line_detail := fnd_api.g_miss_num;
11898 
11899     /*** End R12 }} ***/
11900 
11901     IF (l_debug = 1) THEN
11902        debug_print('Before calling query reservation...');
11903     END IF;
11904     --
11905     query_reservation(
11906       p_api_version_number         => 1.0
11907     , p_init_msg_lst               => fnd_api.g_false
11908     , x_return_status              => l_return_status
11909     , x_msg_count                  => x_msg_count
11910     , x_msg_data                   => x_msg_data
11911     , p_query_input                => l_rsv_rec           /*** {{ R12 Enhanced reservations code changes ***/
11912     , p_lock_records               => fnd_api.g_true
11913     , x_mtl_reservation_tbl        => l_tmp_rsv_tbl
11914     , x_mtl_reservation_tbl_count  => l_tmp_rsv_tbl_count
11915     , x_error_code                 => l_error_code
11916     );
11917 
11918     --
11919     IF l_return_status = fnd_api.g_ret_sts_error THEN
11920       RAISE fnd_api.g_exc_error;
11921     END IF;
11922 
11923     --
11924     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11925       RAISE fnd_api.g_exc_unexpected_error;
11926     END IF;
11927 
11928     --
11929     IF l_tmp_rsv_tbl_count = 0 THEN
11930       fnd_message.set_name('INV', 'INV-ROW NOT FOUND');
11931       fnd_msg_pub.ADD;
11932       RAISE fnd_api.g_exc_error;
11933     END IF;
11934 
11935     IF (l_debug = 1) THEN
11936        debug_print('After calling query reservation... ' || l_return_status);
11937        debug_print('No of rows to be deleted '|| l_tmp_rsv_tbl_count);
11938     END IF;
11939     --
11940     IF l_tmp_rsv_tbl_count > 1 THEN
11941       fnd_message.set_name('INV', 'INV-DELETE MORE THAN ONE ROW');
11942       fnd_msg_pub.ADD;
11943       RAISE fnd_api.g_exc_error;
11944     END IF;
11945 
11946     --
11947     l_reservation_id  := l_tmp_rsv_tbl(1).reservation_id;
11948 
11949     --
11950     IF (l_debug = 1) THEN
11951        debug_print('Before calling validate...');
11952     END IF;
11953     -- Bug 2354735: Proceed with Validation if p_validation_flag = 'T' or 'V'
11954      IF p_validation_flag = fnd_api.g_true OR p_validation_flag = 'V' THEN
11955       -- validation is needed to get revision, lot,
11956       --serial control information
11957       -- for creating the quantity tree
11958       inv_reservation_validate_pvt.validate_input_parameters(
11959         x_return_status              => l_return_status
11960       , p_orig_rsv_rec               => l_tmp_rsv_tbl(1)
11961       , p_to_rsv_rec                 => l_dummy_rsv_rec
11962       , p_orig_serial_array          => p_original_serial_number
11963       , p_to_serial_array            => l_dummy_serial_array
11964       , p_rsv_action_name            => 'DELETE'
11965       , x_orig_item_cache_index      => l_orig_item_cache_index
11966       , x_orig_org_cache_index       => l_orig_org_cache_index
11967       , x_orig_demand_cache_index    => l_orig_demand_cache_index
11968       , x_orig_supply_cache_index    => l_orig_supply_cache_index
11969       , x_orig_sub_cache_index       => l_orig_sub_cache_index
11970       , x_to_item_cache_index        => l_to_item_cache_index
11971       , x_to_org_cache_index         => l_to_org_cache_index
11972       , x_to_demand_cache_index      => l_to_demand_cache_index
11973       , x_to_supply_cache_index      => l_to_supply_cache_index
11974       , x_to_sub_cache_index         => l_to_sub_cache_index
11975       );
11976 
11977       --
11978       IF l_return_status = fnd_api.g_ret_sts_error THEN
11979         RAISE fnd_api.g_exc_error;
11980       END IF;
11981 
11982       --
11983       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11984         RAISE fnd_api.g_exc_unexpected_error;
11985       END IF;
11986 
11987       --
11988       -- INVCONV BEGIN
11989       IF NOT is_lot_divisible(l_orig_item_cache_index) THEN
11990         l_lot_divisible_flag := 'N';
11991       END IF;
11992       -- INVCONV END
11993 
11994       -- Bug 2354735: Proceed with Trees only if p_validation_flag = 'T'
11995       IF l_tmp_rsv_tbl(1).supply_source_type_id = inv_reservation_global.g_source_type_inv
11996          AND p_validation_flag = fnd_api.g_true THEN
11997         -- call quantity processor to
11998         -- modify the tree so that the quantity tree
11999         -- reflect the deletion
12000         inv_quantity_tree_pvt.create_tree(
12001           p_api_version_number         => 1.0
12002         , p_init_msg_lst               => fnd_api.g_true
12003         , x_return_status              => l_return_status
12004         , x_msg_count                  => x_msg_count
12005         , x_msg_data                   => x_msg_data
12006         , p_organization_id            => l_tmp_rsv_tbl(1).organization_id
12007         , p_inventory_item_id          => l_tmp_rsv_tbl(1).inventory_item_id
12008         , p_tree_mode                  => inv_quantity_tree_pvt.g_reservation_mode
12009         , p_is_revision_control        => is_revision_control(l_orig_item_cache_index)
12010         , p_is_lot_control             => is_lot_control(l_orig_item_cache_index)
12011         , p_is_serial_control          => is_serial_control(l_orig_item_cache_index)
12012         , p_asset_sub_only             => FALSE
12013         , p_include_suggestion         => TRUE
12014         , p_demand_source_type_id      => l_tmp_rsv_tbl(1).demand_source_type_id
12015         , p_demand_source_header_id    => l_tmp_rsv_tbl(1).demand_source_header_id
12016         , p_demand_source_line_id      => l_tmp_rsv_tbl(1).demand_source_line_id
12017         , p_demand_source_name         => l_tmp_rsv_tbl(1).demand_source_name
12018         , p_demand_source_delivery     => l_tmp_rsv_tbl(1).demand_source_delivery
12019         , p_lot_expiration_date        => NULL
12020         , x_tree_id                    => l_tree_id
12021         );
12022 
12023         IF l_return_status = fnd_api.g_ret_sts_error THEN
12024           RAISE fnd_api.g_exc_error;
12025         END IF;
12026 
12027         --
12028         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12029           RAISE fnd_api.g_exc_unexpected_error;
12030         END IF;
12031 
12032 	IF (l_debug = 1) THEN
12033            debug_print('After calling validate...' || l_return_status);
12034         END IF;
12035 
12036         -- INVCONV - Upgrade call to incorporate secondaries
12037         modify_tree_crt_del_rel(
12038           x_return_status              => l_return_status
12039         , p_tree_id                    => l_tree_id
12040         , p_revision                   => l_tmp_rsv_tbl(1).revision
12041         , p_lot_number                 => l_tmp_rsv_tbl(1).lot_number
12042         , p_subinventory_code          => l_tmp_rsv_tbl(1).subinventory_code
12043         , p_locator_id                 => l_tmp_rsv_tbl(1).locator_id
12044         , p_lpn_id                     => l_tmp_rsv_tbl(1).lpn_id
12045         , p_primary_reservation_quantity=> l_tmp_rsv_tbl(1).primary_reservation_quantity
12046         , p_second_reservation_quantity=> l_tmp_rsv_tbl(1).secondary_reservation_quantity
12047         , p_detailed_quantity          => l_tmp_rsv_tbl(1).detailed_quantity
12048         , p_secondary_detailed_quantity=> l_tmp_rsv_tbl(1).secondary_detailed_quantity
12049         , p_partial_reservation_flag   => fnd_api.g_false
12050         , p_force_reservation_flag     => fnd_api.g_false
12051         , p_lot_divisible_flag         => l_lot_divisible_flag    -- INVCONV
12052         , p_action                     => 'DELETE'
12053         , x_qty_changed                => l_qty_changed
12054         , x_secondary_qty_changed      => l_secondary_qty_changed
12055         );
12056 
12057         --
12058         IF l_return_status = fnd_api.g_ret_sts_error THEN
12059           RAISE fnd_api.g_exc_error;
12060         END IF;
12061 
12062         --
12063         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12064           RAISE fnd_api.g_exc_unexpected_error;
12065         END IF;
12066 	IF (l_debug = 1) THEN
12067            debug_print('Return Status...' || l_return_status);
12068         END IF;
12069       --
12070       END IF;
12071     END IF;
12072 
12073 
12074     -- Pre Delete CTO Validation
12075     IF l_tmp_rsv_tbl(1).demand_source_type_id IN (inv_reservation_global.g_source_type_oe, inv_reservation_global.g_source_type_internal_ord, inv_reservation_global.g_source_type_rma) THEN
12076       --
12077       cto_workflow_api_pk.inventory_unreservation_check(
12078         p_order_line_id              => l_tmp_rsv_tbl(1).demand_source_line_id
12079       , p_rsv_quantity               => l_tmp_rsv_tbl(1).primary_reservation_quantity
12080       , x_return_status              => l_return_status
12081       , x_msg_count                  => x_msg_count
12082       , x_msg_data                   => x_msg_data
12083       );
12084 
12085       --
12086       IF l_return_status = fnd_api.g_ret_sts_error THEN
12087         RAISE fnd_api.g_exc_error;
12088       END IF;
12089 
12090       --
12091       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12092         RAISE fnd_api.g_exc_unexpected_error;
12093       END IF;
12094     --
12095     END IF;
12096     IF (l_debug = 1) THEN
12097        debug_print('After calling inventory_unreservation_check...' || l_return_status);
12098     END IF;
12099 
12100     --
12101     -- for data sync b/w mtl_demand and mtl_reservations
12102     inv_rsv_synch.for_delete(p_reservation_id => l_reservation_id, x_return_status => l_return_status, x_msg_count => x_msg_count, x_msg_data => x_msg_data);
12103 
12104     IF l_return_status = fnd_api.g_ret_sts_error THEN
12105       RAISE fnd_api.g_exc_error;
12106     END IF;
12107 
12108     --
12109     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12110       RAISE fnd_api.g_exc_unexpected_error;
12111     END IF;
12112 
12113     /*** {{ R12 Enhanced reservations code changes ***/
12114     -- update the mtl_serial_numbers, null out the reservation_id
12115     -- and group_mark_id of the reserved serials before delete the
12116     -- reservation. There is no need to update the serial_reservation_quantity
12117     -- because the reservation record will be deleted.
12118     BEGIN
12119        update mtl_serial_numbers
12120 	 set reservation_id = NULL,
12121 	 group_mark_id = NULL, line_mark_id = NULL,
12122 	 lot_line_mark_id = NULL
12123 	 where reservation_id = l_reservation_id;
12124     EXCEPTION
12125        WHEN NO_DATA_FOUND THEN
12126 	  IF (l_debug = 1) THEN
12127 	     debug_print('No serial numbers stamp with reservation_id ' || l_reservation_id);
12128 	  END IF;
12129     END;
12130     /*** End R12 }} ***/
12131 
12132     --
12133     --
12134     -- delete the reservation from the db table
12135     mtl_reservations_pkg.delete_row(x_reservation_id => l_reservation_id);
12136 
12137     -- Post Delete CTO Validation
12138     IF l_tmp_rsv_tbl(1).demand_source_type_id IN (inv_reservation_global.g_source_type_oe, inv_reservation_global.g_source_type_internal_ord, inv_reservation_global.g_source_type_rma) THEN
12139       --
12140       cto_workflow_api_pk.wf_update_after_inv_unreserv(
12141         p_order_line_id              => l_tmp_rsv_tbl(1).demand_source_line_id
12142       , x_return_status              => l_return_status
12143       , x_msg_count                  => x_msg_count
12144       , x_msg_data                   => x_msg_data
12145       );
12146 
12147       --
12148       IF l_return_status = fnd_api.g_ret_sts_error THEN
12149         RAISE fnd_api.g_exc_error;
12150       END IF;
12151 
12152       --
12153       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12154         RAISE fnd_api.g_exc_unexpected_error;
12155       END IF;
12156       --
12157       IF (l_debug = 1) THEN
12158          debug_print('After calling wf_update_after_inv_unreserv...' || l_return_status);
12159       END IF;
12160     END IF;
12161 
12162     x_return_status   := l_return_status;
12163   --
12164   EXCEPTION
12165     WHEN fnd_api.g_exc_error THEN
12166       x_return_status  := fnd_api.g_ret_sts_error;
12167       --  Get message count and data
12168       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
12169     WHEN fnd_api.g_exc_unexpected_error THEN
12170       x_return_status  := fnd_api.g_ret_sts_unexp_error;
12171       --  Get message count and data
12172       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
12173     WHEN OTHERS THEN
12174       x_return_status  := fnd_api.g_ret_sts_unexp_error;
12175 
12176       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
12177         fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
12178       END IF;
12179 
12180       --  Get message count and data
12181       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
12182   END delete_reservation;
12183 
12184 
12185 /**** {{ R12 Enhanced reservations code changes }}****/
12186 -- Description
12187   --   transfer is very similar to update
12188   --   except the to row can be not exist (will be created) or
12189   --   exist (will be add upon)
12190   PROCEDURE transfer_reservation
12191     (
12192      p_api_version_number       IN     NUMBER
12193      , p_init_msg_lst           IN     VARCHAR2 DEFAULT fnd_api.g_false
12194      , x_return_status          OUT    NOCOPY VARCHAR2
12195      , x_msg_count              OUT    NOCOPY NUMBER
12196      , x_msg_data               OUT    NOCOPY VARCHAR2
12197      , p_original_rsv_rec       IN     inv_reservation_global.mtl_reservation_rec_type
12198      , p_to_rsv_rec             IN     inv_reservation_global.mtl_reservation_rec_type
12199      , p_original_serial_number IN     inv_reservation_global.serial_number_tbl_type
12200      , p_validation_flag        IN     VARCHAR2 DEFAULT fnd_api.g_true
12201      , p_over_reservation_flag  IN  NUMBER DEFAULT 0
12202      , x_reservation_id         OUT    NOCOPY NUMBER
12203      ) IS
12204 
12205      l_api_version_number CONSTANT NUMBER :=  1.0;
12206      l_api_name           CONSTANT VARCHAR2(30) := 'Transfrer_Reservation';
12207      l_return_status      VARCHAR2(1) :=  fnd_api.g_ret_sts_success;
12208      l_quantity_reserved  NUMBER;
12209      l_secondary_quantity_reserved NUMBER;          -- INVCONV
12210      l_debug NUMBER;
12211      l_dummy_serial_number inv_reservation_global.serial_number_tbl_type;
12212      l_reservation_id NUMBER;
12213 
12214   BEGIN
12215      --  Standard call to check for call compatibility
12216      IF NOT fnd_api.compatible_api_call
12217        (l_api_version_number
12218 	, p_api_version_number
12219 	, l_api_name
12220 	, G_PKG_NAME
12221 	) THEN
12222 	RAISE fnd_api.g_exc_unexpected_error;
12223      END IF;
12224 
12225      --  Initialize message list.
12226      IF fnd_api.to_boolean(p_init_msg_lst) THEN
12227 	fnd_msg_pub.initialize;
12228      END IF;
12229 
12230      -- Use cache to get value for l_debug
12231      IF g_is_pickrelease_set IS NULL THEN
12232         g_is_pickrelease_set := 2;
12233         IF INV_CACHE.is_pickrelease THEN
12234            g_is_pickrelease_set := 1;
12235         END IF;
12236      END IF;
12237      IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
12238         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
12239      END IF;
12240 
12241      l_debug := g_debug;
12242 
12243      IF l_debug=1 THEN
12244 	debug_print('Calling the overloaded procedure transfer_reservation');
12245      END IF;
12246 
12247      inv_reservation_pvt.transfer_reservation
12248        (p_api_version_number          => 1.0,
12249 	p_init_msg_lst                => p_init_msg_lst,
12250 	x_return_status               => l_return_status,
12251 	x_msg_count                   => x_msg_count,
12252 	x_msg_data                    => x_msg_data,
12253 	p_original_rsv_rec            => p_original_rsv_rec,
12254 	p_to_rsv_rec                  => p_to_rsv_rec,
12255 	p_original_serial_number      => p_original_serial_number,
12256 	p_to_serial_number            => l_dummy_serial_number,
12257 	p_validation_flag             => p_validation_flag,
12258 	p_over_reservation_flag       => p_over_reservation_flag,
12259 	x_reservation_id              => l_reservation_id
12260 	);
12261 
12262      IF (l_debug=1) THEN
12263 	debug_print ('Return Status after transfer reservations '||l_return_status);
12264      END IF;
12265 
12266      IF l_return_status = fnd_api.g_ret_sts_error THEN
12267 
12268 	IF l_debug=1 THEN
12269 	   debug_print('Raising expected error'||l_return_status);
12270 	END IF;
12271 
12272 	RAISE fnd_api.g_exc_error;
12273 
12274       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12275 
12276 	IF l_debug=1 THEN
12277 	   debug_print('Raising Unexpected error'||l_return_status);
12278 	END IF;
12279 
12280 	RAISE fnd_api.g_exc_unexpected_error;
12281      END IF;
12282 
12283 
12284      x_return_status := l_return_status;
12285      x_reservation_id := l_reservation_id;
12286 
12287   EXCEPTION
12288 
12289      WHEN fnd_api.g_exc_error THEN
12290 	x_return_status := fnd_api.g_ret_sts_error;
12291 	--  Get message count and data
12292 	fnd_msg_pub.count_and_get
12293 	  (  p_count => x_msg_count
12294 	     , p_data  => x_msg_data
12295 	     );
12296 
12297      WHEN fnd_api.g_exc_unexpected_error THEN
12298 	x_return_status := fnd_api.g_ret_sts_unexp_error ;
12299 
12300 	--  Get message count and data
12301 	fnd_msg_pub.count_and_get
12302 	  (  p_count  => x_msg_count
12303 	     , p_data   => x_msg_data
12304 	     );
12305 
12306      WHEN OTHERS THEN
12307 	x_return_status := fnd_api.g_ret_sts_unexp_error ;
12308 
12309 	IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
12310 	  THEN
12311 	   fnd_msg_pub.add_exc_msg
12312 	     (  g_pkg_name
12313 		, l_api_name
12314 		);
12315 	END IF;
12316 
12317 	--  Get message count and data
12318 	fnd_msg_pub.count_and_get
12319 	  (  p_count  => x_msg_count
12320 	     , p_data   => x_msg_data
12321 	     );
12322 
12323   END transfer_reservation;
12324   /*** End R12 ***/
12325 
12326   -- Overloaded transfer_reservation API
12327   -- Description
12328   --   transfer is very similar to update
12329   --   except the to row can be not exist (will be created) or
12330   --   exist (will be add upon)
12331   PROCEDURE transfer_reservation
12332     (
12333      p_api_version_number     IN     NUMBER
12334      , p_init_msg_lst           IN     VARCHAR2 DEFAULT fnd_api.g_false
12335      , x_return_status          OUT    NOCOPY VARCHAR2
12336      , x_msg_count              OUT    NOCOPY NUMBER
12337      , x_msg_data               OUT    NOCOPY VARCHAR2
12338      , p_original_rsv_rec       IN     inv_reservation_global.mtl_reservation_rec_type
12339      , p_to_rsv_rec             IN     inv_reservation_global.mtl_reservation_rec_type
12340      , p_original_serial_number IN     inv_reservation_global.serial_number_tbl_type
12341      /**** {{ R12 Enhanced reservations code changes }}****/
12342      , p_to_serial_number  IN  inv_reservation_global.serial_number_tbl_type
12343      /*** End R12 ***/
12344      , p_validation_flag        IN     VARCHAR2 DEFAULT fnd_api.g_true
12345      , p_over_reservation_flag  IN  NUMBER DEFAULT 0
12346      , x_reservation_id         OUT    NOCOPY NUMBER
12347      ) IS
12348 	l_api_version_number  CONSTANT NUMBER                                          := 1.0;
12349 	l_api_name            CONSTANT VARCHAR2(30)                                    := 'Transfer_Reservation';
12350 	l_return_status                VARCHAR2(1)                                     := fnd_api.g_ret_sts_success;
12351 	l_original_rsv_rec             inv_reservation_global.mtl_reservation_rec_type;
12352 	l_to_rsv_rec                   inv_reservation_global.mtl_reservation_rec_type;
12353 	l_orig_rsv_tbl                 inv_reservation_global.mtl_reservation_tbl_type;
12354 	l_orig_rsv_tbl_count           NUMBER;
12355 	l_to_rsv_tbl                   inv_reservation_global.mtl_reservation_tbl_type;
12356 	--
12357 	l_to_rsv_tbl_count             NUMBER;
12358 	l_transfer_all                 BOOLEAN;
12359 	l_to_row_exist                 BOOLEAN;
12360 	l_primary_uom_code             VARCHAR2(3);
12361 	l_reservation_uom_code         VARCHAR2(3);
12362 	l_reservation_id               NUMBER;
12363 	l_orig_item_cache_index        INTEGER                                         := NULL;
12364 	l_orig_org_cache_index         INTEGER                                         := NULL;
12365 	l_orig_demand_cache_index      INTEGER                                         := NULL;
12366 	l_orig_supply_cache_index      INTEGER                                         := NULL;
12367 	l_orig_sub_cache_index         INTEGER                                         := NULL;
12368 	l_to_item_cache_index          INTEGER                                         := NULL;
12369 	l_to_org_cache_index           INTEGER                                         := NULL;
12370 	l_to_demand_cache_index        INTEGER                                         := NULL;
12371 	l_to_supply_cache_index        INTEGER                                         := NULL;
12372 	l_to_sub_cache_index           INTEGER                                         := NULL;
12373 	l_primary_reservation_quantity NUMBER;
12374 	l_second_reservation_quantity  NUMBER;                                      -- INVCONV
12375 	l_tree_id1                     NUMBER;
12376 	l_tree_id2                     NUMBER;
12377 	l_date                         DATE;
12378 	l_user_id                      NUMBER;
12379 	l_request_id                   NUMBER;
12380 	l_login_id                     NUMBER;
12381 	l_prog_appl_id                 NUMBER;
12382 	l_program_id                   NUMBER;
12383 	l_qty                          NUMBER;
12384 	l_rowid                        VARCHAR2(2000);
12385 	l_error_code                   NUMBER;
12386 	l_detailed_quantity            NUMBER;
12387 	l_secondary_detailed_quantity  NUMBER;                                       -- INVCONV
12388 	l_new_orig_rsv_qty             NUMBER;
12389 	l_new_orig_prim_qty            NUMBER;
12390 	l_orig_second_rsv_qty          NUMBER;                                       -- INVCONV
12391 	l_primary_rsv_quantity NUMBER;
12392 	l_secondary_rsv_quantity       NUMBER;                                       -- INVCONV
12393 	l_rsv_quantity NUMBER;
12394 	l_debug number;
12395 	l_quantity_reserved  NUMBER;
12396 	l_secondary_quantity_reserved  NUMBER;                                       -- INVCONV
12397 	l_lot_divisible_flag           VARCHAR2(1)  :='Y';                           -- INVCONV
12398 	l_dual_tracking                BOOLEAN := FALSE;                             -- INVCONV
12399 
12400 	/**** {{ R12 Enhanced reservations code changes }}****/
12401 	l_original_serial_number inv_reservation_global.serial_number_tbl_type;
12402 	l_to_serial_number inv_reservation_global.serial_number_tbl_type;
12403 	l_serial_number_table inv_reservation_global.serial_number_tbl_type;
12404 	l_dummy_rsv_rec inv_reservation_global.mtl_reservation_rec_type;
12405 	l_dummy_serial_array  inv_reservation_global.serial_number_tbl_type;
12406 	l_qty_available NUMBER := 0;
12407 	l_original_serial_count NUMBER;
12408 	l_to_serial_count NUMBER;
12409 	l_serial_param NUMBER;
12410 	l_serials_tobe_unreserved NUMBER;
12411 	l_total_serials_reserved NUMBER;
12412 	l_total_to_serials_reserved NUMBER;
12413 	l_from_reservation_id NUMBER;
12414 	l_validate_serials_reserved NUMBER;
12415 	l_validate_serial_number_table inv_reservation_global.serial_number_tbl_type;
12416 	l_serials_unreserved NUMBER;
12417 	l_to_reservation_id NUMBER;
12418 	l_total_from_serials_reserved NUMBER;
12419 	l_from_primary_reservation_qty NUMBER;
12420 	l_to_primary_reservation_qty NUMBER;
12421 	l_supply_lock_handle varchar2(128);
12422 	l_demand_lock_handle varchar2(128);
12423 	l_lock_status NUMBER;
12424         l_reserved_qty NUMBER := 0;
12425         l_requested_qty NUMBER := 0;
12426 	l_group_mark_id NUMBER := NULL;
12427 	l_lock_obtained BOOLEAN := FALSE;
12428 	l_pjm_enabled NUMBER;
12429 	l_project_id NUMBER;
12430 	l_task_id NUMBER;
12431 	l_orig_supply_type_id NUMBER;
12432 	l_supply_source_type_id NUMBER;
12433 	/*** End R12 ***/
12434 
12435   BEGIN
12436     -- Use cache to get value for l_debug
12437     IF g_is_pickrelease_set IS NULL THEN
12438        g_is_pickrelease_set := 2;
12439        IF INV_CACHE.is_pickrelease THEN
12440           g_is_pickrelease_set := 1;
12441        END IF;
12442     END IF;
12443     IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
12444        g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
12445     END IF;
12446 
12447     l_debug := g_debug;
12448     IF (l_debug = 1) THEN
12449        debug_print('Inside transfer reservation...');
12450     END IF;
12451 
12452     --  Standard call to check for call compatibility
12453     IF NOT fnd_api.compatible_api_call(l_api_version_number, p_api_version_number, l_api_name, g_pkg_name) THEN
12454       RAISE fnd_api.g_exc_unexpected_error;
12455     END IF;
12456 
12457     IF (l_debug = 1) THEN
12458        debug_print('The original reservation record: ');
12459     END IF;
12460     print_rsv_rec(p_original_rsv_rec);
12461     IF (l_debug = 1) THEN
12462        debug_print('The to reservation record: ');
12463     END IF;
12464     print_rsv_rec(p_to_rsv_rec);
12465 
12466     --
12467     --
12468     -- if the transfer quantity is 0, call delete instead
12469     IF p_to_rsv_rec.primary_reservation_quantity = 0
12470        OR (p_to_rsv_rec.reservation_quantity = 0
12471            AND (p_to_rsv_rec.primary_reservation_quantity IS NULL
12472                 OR p_to_rsv_rec.primary_reservation_quantity = fnd_api.g_miss_num
12473                )
12474           ) THEN
12475       x_return_status  := fnd_api.g_ret_sts_success;
12476       RETURN;
12477     END IF;
12478 
12479     --  Initialize message list.
12480     IF fnd_api.to_boolean(p_init_msg_lst) THEN
12481       fnd_msg_pub.initialize;
12482     END IF;
12483 
12484     /**** {{ R12 Enhanced reservations code changes }}****/
12485 
12486     -- Set the original columns to g_miss_xxx as the user should not be
12487     -- setting these values. Do not query by them
12488     l_original_rsv_rec := p_original_rsv_rec;
12489 
12490     l_original_rsv_rec.orig_supply_source_type_id := fnd_api.g_miss_num;
12491     l_original_rsv_rec.orig_supply_source_header_id := fnd_api.g_miss_num;
12492     l_original_rsv_rec.orig_supply_source_line_id := fnd_api.g_miss_num;
12493     l_original_rsv_rec.orig_supply_source_line_detail := fnd_api.g_miss_num;
12494     l_original_rsv_rec.orig_demand_source_type_id := fnd_api.g_miss_num;
12495     l_original_rsv_rec.orig_demand_source_header_id := fnd_api.g_miss_num;
12496     l_original_rsv_rec.orig_demand_source_line_id := fnd_api.g_miss_num;
12497     l_original_rsv_rec.orig_demand_source_line_detail := fnd_api.g_miss_num;
12498 
12499     /*** End R12 ***/
12500 
12501     SAVEPOINT transfer_reservation_sa;
12502     IF (l_debug = 1) THEN
12503        debug_print('Before calling query rsv for the from record: ' || l_return_status);
12504     END IF;
12505 
12506     -- search for the from row
12507     query_reservation(
12508       p_api_version_number         => 1.0
12509     , p_init_msg_lst               => fnd_api.g_false
12510     , x_return_status              => l_return_status
12511     , x_msg_count                  => x_msg_count
12512     , x_msg_data                   => x_msg_data
12513     , p_query_input                => l_original_rsv_rec
12514     , p_lock_records               => fnd_api.g_true
12515     , x_mtl_reservation_tbl        => l_orig_rsv_tbl
12516     , x_mtl_reservation_tbl_count  => l_orig_rsv_tbl_count
12517     , x_error_code                 => l_error_code
12518     );
12519 
12520     --
12521     IF l_return_status = fnd_api.g_ret_sts_error THEN
12522       RAISE fnd_api.g_exc_error;
12523     END IF;
12524 
12525     --
12526     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12527       RAISE fnd_api.g_exc_unexpected_error;
12528     END IF;
12529 
12530     --
12531     IF l_orig_rsv_tbl_count = 0 THEN
12532       fnd_message.set_name('INV', 'INV-ROW NOT FOUND');
12533       fnd_msg_pub.ADD;
12534       RAISE fnd_api.g_exc_error;
12535     END IF;
12536 
12537     --
12538     IF l_orig_rsv_tbl_count > 1 THEN
12539       fnd_message.set_name('INV', 'TRANSFER MORE THAN ONE ROW');
12540       fnd_msg_pub.ADD;
12541       RAISE fnd_api.g_exc_error;
12542     END IF;
12543 
12544     IF (l_debug = 1) THEN
12545        debug_print('After calling query rsv from record: ' || l_return_status);
12546        debug_print(' orig tbl count' || l_orig_rsv_tbl_count);
12547     END IF;
12548 
12549     construct_to_reservation_row(l_orig_rsv_tbl(1), p_to_rsv_rec, l_to_rsv_rec);
12550 
12551     --
12552     -- if the caller does not specified reservation_id, l_to_rsv_rec will
12553     -- has the same reservation_id as l_orig_rsv_tbl(1) due to the way
12554     -- construct_to_reservation_row works.
12555     -- but we should set it to g_miss_num again
12556     -- otherwise query_reservation will use only the
12557     -- reservation_id to do the search.
12558     IF l_to_rsv_rec.reservation_id = l_orig_rsv_tbl(1).reservation_id THEN
12559       l_to_rsv_rec.reservation_id  := fnd_api.g_miss_num;
12560     END IF;
12561 
12562     --
12563     IF l_orig_rsv_tbl(1).organization_id <> l_to_rsv_rec.organization_id THEN
12564       fnd_message.set_name('INV', 'CANNOT_CHANGE_ORGANIZATION_ID');
12565       fnd_msg_pub.ADD;
12566       RAISE fnd_api.g_exc_error;
12567     END IF;
12568 
12569     --
12570     IF l_orig_rsv_tbl(1).inventory_item_id <> l_to_rsv_rec.inventory_item_id THEN
12571       fnd_message.set_name('INV', 'CANNOT_CHANGE_INVENTORY_ITEM');
12572       fnd_msg_pub.ADD;
12573       RAISE fnd_api.g_exc_error;
12574     END IF;
12575 
12576     -- convert quantity between primary uom and reservation uom
12577     convert_quantity(x_return_status => l_return_status, px_rsv_rec => l_to_rsv_rec);
12578 
12579     --
12580     IF l_return_status = fnd_api.g_ret_sts_error THEN
12581       RAISE fnd_api.g_exc_error;
12582     END IF;
12583 
12584     --
12585     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12586       RAISE fnd_api.g_exc_unexpected_error;
12587     END IF;
12588 
12589     /**** {{ R12 Enhanced reservations code changes }}****/
12590 
12591     -- Set the original columns to g_miss_xxx as the user should not be
12592     -- setting these values. Do not query by them
12593     l_to_rsv_rec.orig_supply_source_type_id := fnd_api.g_miss_num;
12594     l_to_rsv_rec.orig_supply_source_header_id := fnd_api.g_miss_num;
12595     l_to_rsv_rec.orig_supply_source_line_id := fnd_api.g_miss_num;
12596     l_to_rsv_rec.orig_supply_source_line_detail := fnd_api.g_miss_num;
12597     l_to_rsv_rec.orig_demand_source_type_id := fnd_api.g_miss_num;
12598     l_to_rsv_rec.orig_demand_source_header_id := fnd_api.g_miss_num;
12599     l_to_rsv_rec.orig_demand_source_line_id := fnd_api.g_miss_num;
12600     l_to_rsv_rec.orig_demand_source_line_detail := fnd_api.g_miss_num;
12601 
12602     IF (l_to_rsv_rec.project_id IS NULL)  THEN
12603        l_to_rsv_rec.project_id := fnd_api.g_miss_num;
12604     END IF;
12605     IF (l_to_rsv_rec.task_id IS NULL)  THEN
12606        l_to_rsv_rec.task_id := fnd_api.g_miss_num;
12607     END IF;
12608     /*** End R12 ***/
12609 
12610     IF (l_debug = 1) THEN
12611        debug_print('Before query reservation for the to rec...' || l_return_status);
12612     END IF;
12613     --
12614     -- search for the to row
12615     query_reservation(
12616       p_api_version_number         => 1.0
12617     , p_init_msg_lst               => fnd_api.g_false
12618     , x_return_status              => l_return_status
12619     , x_msg_count                  => x_msg_count
12620     , x_msg_data                   => x_msg_data
12621     , p_query_input                => l_to_rsv_rec
12622     , p_lock_records               => fnd_api.g_true
12623     , x_mtl_reservation_tbl        => l_to_rsv_tbl
12624     , x_mtl_reservation_tbl_count  => l_to_rsv_tbl_count
12625     , x_error_code                 => l_error_code
12626     );
12627 
12628     --
12629     IF l_return_status = fnd_api.g_ret_sts_error THEN
12630       RAISE fnd_api.g_exc_error;
12631     END IF;
12632 
12633     --
12634     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12635       RAISE fnd_api.g_exc_unexpected_error;
12636     END IF;
12637 
12638     --
12639     IF l_to_rsv_tbl_count > 1 THEN
12640       -- if there more than one target row, the reservation table
12641       -- must be damaged. We can not do anything but failed
12642       fnd_message.set_name('INV', 'INV-RSV-TOO-MANY-TARGET');
12643       fnd_msg_pub.ADD;
12644       RAISE fnd_api.g_exc_error;
12645     END IF;
12646 
12647     IF (l_debug = 1) THEN
12648        debug_print('After query reservation to rec...' || l_return_status);
12649        debug_print('l_to_rsv_tbl_count ' || l_to_rsv_tbl_count);
12650     END IF;
12651 
12652     IF  l_to_rsv_tbl_count = 1
12653         AND l_to_rsv_tbl(1).reservation_id = l_orig_rsv_tbl(1).reservation_id THEN
12654       -- this is a lazy way to find out that the user
12655       -- is trying to use transfer_reservation
12656       -- to update non primary key fields
12657       -- otherwise we would not have the target row having the same values
12658       -- in the primary key columns (see query_reservation select criteria)
12659       -- as the original reservation.
12660       -- we might want to move that to the validation api soon
12661       fnd_message.set_name('INV', 'MISS_USE_TRANSFER');
12662       fnd_msg_pub.ADD;
12663       RAISE fnd_api.g_exc_error;
12664     END IF;
12665 
12666     l_to_row_exist   := (l_to_rsv_tbl_count > 0);
12667 
12668     --
12669     -- if the target reservation already exists, record its reservation id
12670     IF l_to_row_exist THEN
12671       l_to_rsv_rec.reservation_id  := l_to_rsv_tbl(1).reservation_id;
12672     END IF;
12673 
12674    --
12675     IF l_orig_rsv_tbl(1).primary_reservation_quantity < l_to_rsv_rec.primary_reservation_quantity THEN
12676       fnd_message.set_name('INV', 'TRANSFER MORE THAN RESERVE');
12677       fnd_msg_pub.ADD;
12678       RAISE fnd_api.g_exc_error;
12679     END IF;
12680 
12681     /**** {{ R12 Enhanced reservations code changes.Calling the reservation
12682     -- lock API to create a user-defined lock for non-inventory supplies }} *****/
12683       -- Bug 5199672: Should pass null to supply and demand line detail as
12684       -- we will have to lock the records at the document level and not at
12685       -- the line level. Also, for ASN, pass the source type as PO so that the
12686       -- the lock name would be the same as the PO's
12687 
12688     IF (l_to_rsv_rec.supply_source_type_id =
12689 	inv_reservation_global.g_source_type_asn) THEN
12690        l_supply_source_type_id :=
12691 	 inv_reservation_global.g_source_type_po;
12692      ELSE
12693        l_supply_source_type_id := l_to_rsv_rec.supply_source_type_id;
12694     END IF;
12695 
12696     IF (l_to_rsv_rec.supply_source_type_id <> inv_reservation_global.g_source_type_inv) THEN
12697        inv_reservation_lock_pvt.lock_supply_demand_record
12698 	 (p_organization_id => l_to_rsv_rec.organization_id
12699 	  ,p_inventory_item_id => l_to_rsv_rec.inventory_item_id
12700 	  ,p_source_type_id => l_supply_source_type_id
12701 	  ,p_source_header_id => l_to_rsv_rec.supply_source_header_id
12702 	  ,p_source_line_id =>  l_to_rsv_rec.supply_source_line_id
12703 	  ,p_source_line_detail => NULL
12704 	  ,x_lock_handle => l_supply_lock_handle
12705 	  ,x_lock_status => l_lock_status);
12706 
12707        IF l_lock_status = 0 THEN
12708 	  fnd_message.set_name('INV', 'INV_INVALID_LOCK');
12709 	  fnd_msg_pub.ADD;
12710 	  RAISE fnd_api.g_exc_error;
12711        END if;
12712 
12713        inv_reservation_lock_pvt.lock_supply_demand_record
12714 	 (p_organization_id => l_to_rsv_rec.organization_id
12715 	  ,p_inventory_item_id => l_to_rsv_rec.inventory_item_id
12716 	  ,p_source_type_id => l_to_rsv_rec.demand_source_type_id
12717 	  ,p_source_header_id => l_to_rsv_rec.demand_source_header_id
12718 	  ,p_source_line_id =>  l_to_rsv_rec.demand_source_line_id
12719 	  ,p_source_line_detail => NULL
12720 	  ,x_lock_handle => l_demand_lock_handle
12721 	  ,x_lock_status => l_lock_status);
12722 
12723        IF l_lock_status = 0 THEN
12724 	  fnd_message.set_name('INV', 'INV_INVALID_LOCK');
12725 	  fnd_msg_pub.ADD;
12726 	  RAISE fnd_api.g_exc_error;
12727        END if;
12728 
12729        l_lock_obtained := TRUE;
12730     END IF;
12731 
12732     -- Get the project and task for demands in OE, INT-ORD and RMA
12733     IF l_debug=1 THEN
12734        debug_print('Before Rsv rec project id: ' || l_to_rsv_rec.project_id);
12735        debug_print('Before Rsv rec task id: ' || l_to_rsv_rec.task_id);
12736     END IF;
12737     -- Bug : 5264987 : For Pick Release seeting the l_pjm_enabled flag
12738     -- from INV_CACHE.
12739     IF INV_CACHE.is_pickrelease OR g_is_pickrelease_set = 1 THEN
12740         debug_print ('is_pickrelaese is true');
12741 	-- Query for and cache the org record.
12742 	IF (NOT INV_CACHE.set_org_rec(l_to_rsv_rec.organization_id))
12743 	THEN
12744 		IF (l_debug = 1) THEN
12745 			debug_print('Error caching the org record');
12746 		END IF;
12747 		RAISE fnd_api.g_exc_unexpected_error;
12748 	END IF;
12749 	-- Set the PJM enabled flag.
12750 	l_pjm_enabled := INV_CACHE.org_rec.project_reference_enabled;
12751     ELSE
12752         debug_print ('is_pickrelaese is not true');
12753         BEGIN
12754             SELECT project_reference_enabled
12755 	    INTO l_pjm_enabled
12756 	    FROM   mtl_parameters
12757 	    WHERE  organization_id = l_to_rsv_rec.organization_id;
12758         EXCEPTION
12759             WHEN no_data_found THEN
12760 	       IF l_debug=1 THEN
12761 	       debug_print('Cannot find the project and task information');
12762 	       END IF;
12763         END;
12764      END IF;
12765     IF (l_to_rsv_rec.demand_source_type_id IN
12766 	(inv_reservation_global.g_source_type_oe,
12767 	 inv_reservation_global.g_source_type_internal_ord,
12768 	 inv_reservation_global.g_source_type_rma)) AND
12769       (l_pjm_enabled = 1) THEN
12770 
12771        IF (l_to_rsv_rec.demand_source_line_id IS NOT NULL) AND
12772 	 (l_to_rsv_rec.demand_source_line_id <> fnd_api.g_miss_num)
12773 	 AND ((l_to_rsv_rec.project_id = fnd_api.g_miss_num) OR
12774 	      (l_to_rsv_rec.task_id = fnd_api.g_miss_num)) THEN
12775 	 -- Added the below IF condition for Bug Fix 5264987
12776 	 IF l_to_rsv_rec.demand_source_line_id = g_oe_line_id THEN
12777 		l_project_id := g_project_id;
12778 		l_task_id    := g_task_id;
12779 	 ELSE
12780 		 BEGIN
12781 		    SELECT project_id, task_id INTO l_project_id, l_task_id
12782 		      FROM oe_order_lines_all WHERE
12783 		      line_id = l_to_rsv_rec.demand_source_line_id;
12784 		 EXCEPTION
12785 		    WHEN no_data_found THEN
12786 		       IF l_debug=1 THEN
12787 			  debug_print('Cannot find the project and task information');
12788 		       END IF;
12789 		 END;
12790 	 END IF;
12791 
12792 	 IF (l_to_rsv_rec.project_id = fnd_api.g_miss_num) THEN
12793 	    IF (l_project_id IS NOT NULL) THEN
12794 	       l_to_rsv_rec.project_id := l_project_id;
12795 	     ELSE
12796 	       l_to_rsv_rec.project_id := NULL;
12797 	    END IF;
12798 	 END IF;
12799 
12800 	 IF (l_to_rsv_rec.task_id = fnd_api.g_miss_num) THEN
12801 	    IF (l_task_id IS NOT NULL) THEN
12802 	       l_to_rsv_rec.task_id := l_task_id;
12803 	     ELSE
12804 	       l_to_rsv_rec.task_id := NULL;
12805 	    END IF;
12806 	 END IF;
12807 
12808        END IF;
12809      ELSE -- not project enable
12810 	  l_to_rsv_rec.project_id := NULL;
12811 	  l_to_rsv_rec.task_id := NULL;
12812     END IF;
12813     IF l_debug=1 THEN
12814        debug_print('After Rsv rec project id: ' || l_to_rsv_rec.project_id);
12815        debug_print('After Rsv rec task id: ' || l_to_rsv_rec.task_id);
12816     END IF;
12817     /*** End R12 ***/
12818 
12819 
12820     -- INVCONV - Establish whether item is tracked in dual UOMs
12821     IF l_orig_rsv_tbl(1).secondary_uom_code is NOT NULL THEN
12822       l_dual_tracking := TRUE;
12823     END IF;
12824     -- INVCONV END
12825 
12826     --
12827     -- see whether we are transferring all quantity
12828     -- or just partial
12829     l_transfer_all   := (l_orig_rsv_tbl(1).primary_reservation_quantity = l_to_rsv_rec.primary_reservation_quantity);
12830 
12831     /**** {{ R12 Enhanced reservations code changes }}****/
12832     l_original_serial_count := p_original_serial_number.COUNT;
12833     l_to_serial_count := p_to_serial_number.COUNT;
12834     -- set the parameter value to handle serial numbers.
12835     IF (l_original_serial_count = 0 AND l_to_serial_count = 0) THEN
12836        l_serial_param := 1;
12837      ELSIF (l_original_serial_count > 0 AND l_to_serial_count = 0) THEN
12838        l_serial_param := 2;
12839      ELSIF (l_original_serial_count = 0 AND l_to_serial_count > 0) THEN
12840        l_serial_param := 3;
12841      ELSIF (l_original_serial_count > 0 AND l_to_serial_count > 0) THEN
12842        l_serial_param := 4;
12843      ELSE
12844        -- indeterminate value.error
12845        IF (l_debug = 1) THEN
12846 	  debug_print('Cannot determine what is being passed to the serial tables');
12847        END IF;
12848        fnd_message.set_name('INV', 'INV_INVALID_SERIAL_TABLES');
12849        fnd_msg_pub.ADD;
12850        RAISE fnd_api.g_exc_error;
12851     END IF;
12852 
12853     IF (l_debug = 1) THEN
12854        debug_print('Serial Param' || l_serial_param);
12855        debug_print('Original serial count' || l_original_serial_count);
12856        debug_print('To serial count' || l_to_serial_count);
12857     END IF;
12858 
12859     -- if from and to serials are passed, then pass them to the validate API
12860     IF l_original_serial_count > 0 THEN
12861        l_original_serial_number := p_original_serial_number;
12862     END IF;
12863     IF l_to_serial_count > 0 THEN
12864        l_to_serial_number := p_to_serial_number;
12865     END IF;
12866     /*** End R12 ***/
12867     IF (l_debug = 1) THEN
12868        debug_print('Before calling validate');
12869     END IF;
12870 
12871     IF p_validation_flag = fnd_api.g_true THEN
12872       -- we do validation after the query because
12873       -- for transfer, we might have many input value set to
12874       -- missing. We need to use the actual value to do
12875       -- validation
12876        inv_reservation_validate_pvt.validate_input_parameters
12877 	 (
12878 	  x_return_status              => l_return_status
12879 	  , p_orig_rsv_rec               => l_orig_rsv_tbl(1)
12880 	  , p_to_rsv_rec                 => l_to_rsv_rec
12881 	  /**** {{ R12 Enhanced reservations code changes }}****/
12882 	  , p_orig_serial_array          => l_original_serial_number
12883 	  , p_to_serial_array            => l_to_serial_number
12884 	  /*** End R12 ***/
12885 	  , p_rsv_action_name            => 'TRANSFER'
12886 	  , x_orig_item_cache_index      => l_orig_item_cache_index
12887 	  , x_orig_org_cache_index       => l_orig_org_cache_index
12888 	  , x_orig_demand_cache_index    => l_orig_demand_cache_index
12889 	  , x_orig_supply_cache_index    => l_orig_supply_cache_index
12890 	  , x_orig_sub_cache_index       => l_orig_sub_cache_index
12891 	  , x_to_item_cache_index        => l_to_item_cache_index
12892 	  , x_to_org_cache_index         => l_to_org_cache_index
12893 	  , x_to_demand_cache_index      => l_to_demand_cache_index
12894 	  , x_to_supply_cache_index      => l_to_supply_cache_index
12895 	  , x_to_sub_cache_index         => l_to_sub_cache_index
12896 	  );
12897 
12898        --
12899        IF l_return_status = fnd_api.g_ret_sts_error THEN
12900 	  RAISE fnd_api.g_exc_error;
12901        END IF;
12902 
12903        --
12904        IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12905 	  RAISE fnd_api.g_exc_unexpected_error;
12906        END IF;
12907 
12908        IF (l_debug = 1) THEN
12909 	  debug_print('After calling validate' || l_return_status);
12910        END IF;
12911 
12912        /**** {{ R12 Enhanced reservations code changes }}****/
12913     END IF;
12914 
12915     IF p_validation_flag = fnd_api.g_true AND
12916       (l_to_rsv_rec.supply_source_type_id = inv_reservation_global.g_source_type_inv) THEN
12917 
12918        /*** End R12 ***/
12919        -- INVCONV BEGIN
12920        IF NOT is_lot_divisible(l_orig_item_cache_index) THEN
12921 	  l_lot_divisible_flag := 'N';
12922 	  IF (l_debug = 1) THEN
12923 	     debug_print('Lot indivisible is TRUE ');
12924 	  END IF;
12925        END IF;
12926        -- INVCONV END
12927 
12928        inv_quantity_tree_pvt.create_tree
12929 	 (
12930 	  p_api_version_number         => 1.0
12931 	  , p_init_msg_lst               => fnd_api.g_true
12932 	  , x_return_status              => l_return_status
12933 	  , x_msg_count                  => x_msg_count
12934 	  , x_msg_data                   => x_msg_data
12935 	  , p_organization_id            => l_orig_rsv_tbl(1).organization_id
12936 	  , p_inventory_item_id          => l_orig_rsv_tbl(1).inventory_item_id
12937 	  , p_tree_mode                  => inv_quantity_tree_pvt.g_reservation_mode
12938 	  , p_is_revision_control        => is_revision_control(l_orig_item_cache_index)
12939 	  , p_is_lot_control             => is_lot_control(l_orig_item_cache_index)
12940 	  , p_is_serial_control          => is_serial_control(l_orig_item_cache_index)
12941 	  , p_asset_sub_only             => FALSE
12942 	  , p_include_suggestion         => TRUE
12943 	  , p_demand_source_type_id      => l_orig_rsv_tbl(1).demand_source_type_id
12944 	  , p_demand_source_header_id    => l_orig_rsv_tbl(1).demand_source_header_id
12945 	  , p_demand_source_line_id      => l_orig_rsv_tbl(1).demand_source_line_id
12946 	 , p_demand_source_name         => l_orig_rsv_tbl(1).demand_source_name
12947 	 , p_demand_source_delivery     => l_orig_rsv_tbl(1).demand_source_delivery
12948 	 , p_lot_expiration_date        => SYSDATE -- Bug#2716563
12949 	 , x_tree_id                    => l_tree_id1
12950 	 );
12951 
12952        --
12953        IF l_return_status = fnd_api.g_ret_sts_error THEN
12954 	  RAISE fnd_api.g_exc_error;
12955        END IF;
12956 
12957        --
12958        IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12959 	  RAISE fnd_api.g_exc_unexpected_error;
12960        END IF;
12961 
12962        --
12963        inv_quantity_tree_pvt.create_tree
12964 	 (
12965 	  p_api_version_number         => 1.0
12966 	  , p_init_msg_lst               => fnd_api.g_true
12967 	  , x_return_status              => l_return_status
12968 	  , x_msg_count                  => x_msg_count
12969 	  , x_msg_data                   => x_msg_data
12970 	  , p_organization_id            => l_to_rsv_rec.organization_id
12971 	  , p_inventory_item_id          => l_to_rsv_rec.inventory_item_id
12972 	  , p_tree_mode                  => inv_quantity_tree_pvt.g_reservation_mode
12973 	  , p_is_revision_control        => is_revision_control(l_to_item_cache_index)
12974 	  , p_is_lot_control             => is_lot_control(l_to_item_cache_index)
12975 	  , p_is_serial_control          => is_serial_control(l_to_item_cache_index)
12976 	  , p_asset_sub_only             => FALSE
12977 	  , p_include_suggestion         => TRUE
12978 	  , p_demand_source_type_id      => l_to_rsv_rec.demand_source_type_id
12979 	  , p_demand_source_header_id    => l_to_rsv_rec.demand_source_header_id
12980 	  , p_demand_source_line_id      => l_to_rsv_rec.demand_source_line_id
12981 	  , p_demand_source_name         => l_to_rsv_rec.demand_source_name
12982 	 , p_demand_source_delivery     => l_to_rsv_rec.demand_source_delivery
12983 	 , p_lot_expiration_date        => SYSDATE -- Bug#2716563
12984 	 , x_tree_id                    => l_tree_id2
12985 	 );
12986 
12987        --
12988        IF l_return_status = fnd_api.g_ret_sts_error THEN
12989 	  RAISE fnd_api.g_exc_error;
12990        END IF;
12991 
12992        --
12993        IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12994 	  RAISE fnd_api.g_exc_unexpected_error;
12995        END IF;
12996 
12997        --
12998        -- INVCONV - upgrade call to incorporate secondaries
12999        modify_tree_for_update_xfer
13000 	 (
13001 	  x_return_status              => l_return_status
13002 	  , x_quantity_reserved          => l_quantity_reserved
13003 	  , x_secondary_quantity_reserved => l_secondary_quantity_reserved
13004 	  , p_from_tree_id               => l_tree_id1
13005 	  , p_from_supply_source_type_id => l_orig_rsv_tbl(1).supply_source_type_id
13006 	  , p_from_revision              => l_orig_rsv_tbl(1).revision
13007 	  , p_from_lot_number            => l_orig_rsv_tbl(1).lot_number
13008 	  , p_from_subinventory_code     => l_orig_rsv_tbl(1).subinventory_code
13009 	  , p_from_locator_id            => l_orig_rsv_tbl(1).locator_id
13010 	  , p_from_lpn_id                => l_orig_rsv_tbl(1).lpn_id
13011 	  , p_from_primary_rsv_quantity  => l_orig_rsv_tbl(1).primary_reservation_quantity
13012 	  , p_from_second_rsv_quantity   => l_orig_rsv_tbl(1).secondary_reservation_quantity
13013 	  , p_from_detailed_quantity     => l_orig_rsv_tbl(1).detailed_quantity
13014 	  , p_from_sec_detailed_quantity => l_orig_rsv_tbl(1).secondary_detailed_quantity
13015 	  , p_to_tree_id                 => l_tree_id2
13016 	 , p_to_supply_source_type_id   => l_to_rsv_rec.supply_source_type_id
13017 	 , p_to_revision                => l_to_rsv_rec.revision
13018 	 , p_to_lot_number              => l_to_rsv_rec.lot_number
13019 	 , p_to_subinventory_code       => l_to_rsv_rec.subinventory_code
13020 	 , p_to_locator_id              => l_to_rsv_rec.locator_id
13021 	 , p_to_lpn_id                  => l_to_rsv_rec.lpn_id
13022 	 , p_to_primary_rsv_quantity    => l_to_rsv_rec.primary_reservation_quantity
13023 	 , p_to_second_rsv_quantity     => l_to_rsv_rec.secondary_reservation_quantity
13024 	 , p_to_detailed_quantity       => l_to_rsv_rec.detailed_quantity
13025 	 , p_to_second_detailed_quantity => l_to_rsv_rec.secondary_detailed_quantity
13026 	 , p_to_revision_control        => is_revision_control(l_to_item_cache_index)
13027 	 , p_to_lot_control             => is_lot_control(l_to_item_cache_index)
13028 	 , p_action                     => 'TRANSFER'
13029 	 , p_lot_divisible_flag         => l_lot_divisible_flag    -- INVCONV
13030 	 , p_partial_reservation_flag   => fnd_api.g_false
13031 	 , p_check_availability         => fnd_api.g_false
13032 	 );
13033 
13034 
13035        IF l_return_status = fnd_api.g_ret_sts_error THEN
13036 	  RAISE fnd_api.g_exc_error;
13037        END IF;
13038 
13039        --
13040        IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13041 	  RAISE fnd_api.g_exc_unexpected_error;
13042        END IF;
13043 
13044        /**** {{ R12 Enhanced reservations code changes }}****/
13045      ELSIF (l_to_rsv_rec.supply_source_type_id IN
13046 	    (inv_reservation_global.g_source_type_wip, inv_reservation_global.g_source_type_po,
13047 	     inv_reservation_global.g_source_type_asn,
13048 	     inv_reservation_global.g_source_type_intransit,
13049 	     inv_reservation_global.g_source_type_internal_req,
13050 	     inv_reservation_global.g_source_type_rcv)) AND
13051        p_over_reservation_flag NOT IN (1,3) THEN
13052        -- call the helper procedure to get the reservable qty of the supply
13053        -- Bug 5199672: Should pass g_miss_num as default for supply
13054        -- source line detail. Otherwise, high level reservations
13055        -- will not be considered.
13056        get_supply_reservable_qty
13057          (
13058             x_return_status                 => l_return_status
13059           , x_msg_count                     => x_msg_count
13060           , x_msg_data                      => x_msg_data
13061           , p_fm_supply_source_type_id      => l_orig_rsv_tbl(1).supply_source_type_id
13062           , p_fm_supply_source_header_id    => l_orig_rsv_tbl(1).supply_source_header_id
13063           , p_fm_supply_source_line_id      => l_orig_rsv_tbl(1).supply_source_line_id
13064           , p_fm_supply_source_line_detail  => l_orig_rsv_tbl(1).supply_source_line_detail
13065           , p_fm_primary_reservation_qty    => l_orig_rsv_tbl(1).primary_reservation_quantity
13066           , p_to_supply_source_type_id      => l_to_rsv_rec.supply_source_type_id
13067           , p_to_supply_source_header_id    => l_to_rsv_rec.supply_source_header_id
13068           , p_to_supply_source_line_id      => l_to_rsv_rec.supply_source_line_id
13069           , p_to_supply_source_line_detail  => l_to_rsv_rec.supply_source_line_detail
13070           , p_to_primary_reservation_qty    => l_to_rsv_rec.primary_reservation_quantity
13071           , p_to_organization_id            => l_to_rsv_rec.organization_id
13072           , p_to_inventory_item_id          => l_to_rsv_rec.inventory_item_id
13073           , p_to_revision                   => l_to_rsv_rec.revision
13074           , p_to_lot_number                 => l_to_rsv_rec.lot_number
13075           , p_to_subinventory_code          => l_to_rsv_rec.subinventory_code
13076           , p_to_locator_id                 => l_to_rsv_rec.locator_id
13077           , p_to_lpn_id                     => l_to_rsv_rec.lpn_id
13078           , p_to_project_id                 => l_to_rsv_rec.project_id
13079           , p_to_task_id                    => l_to_rsv_rec.task_id
13080 	 , x_reservable_qty                => l_quantity_reserved
13081 	 , x_qty_available                 => l_qty_available
13082          );
13083 
13084        IF (l_debug = 1) THEN
13085 	  debug_print('After calling available supply to reserve ' || l_return_status);
13086 	  debug_print('Available quantity to reserve. l_quantity_reserved: ' || l_quantity_reserved);
13087        END IF;
13088 
13089        --
13090        IF l_return_status = fnd_api.g_ret_sts_error THEN
13091 	  RAISE fnd_api.g_exc_error;
13092        END IF;
13093 
13094        --
13095        IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13096 	  RAISE fnd_api.g_exc_unexpected_error;
13097        END IF;
13098 
13099        IF ((l_to_rsv_rec.primary_reservation_quantity - l_quantity_reserved)
13100 	 > 0.000005) THEN
13101 
13102           IF (l_debug = 1) THEN
13103              debug_print('The supply document doesnt have enough quantity to be reserved against. error out. ');
13104              debug_print('l_to_rsv_rec.primary_reservation_quantity: ' || l_to_rsv_rec.primary_reservation_quantity);
13105           END IF;
13106           fnd_message.set_name('INV', 'INV_INVALID_AVAILABLE_QTY');
13107           fnd_msg_pub.ADD;
13108           RAISE fnd_api.g_exc_error;
13109 
13110        END IF;
13111     END IF;
13112 
13113    IF (l_debug = 1) THEN
13114        debug_print('From record:');
13115        debug_print('demand_source_type_id = ' || l_orig_rsv_tbl(1).demand_source_type_id);
13116        debug_print('demand_source_header_id = ' || l_orig_rsv_tbl(1).demand_source_header_id);
13117        debug_print('demand_source_line_id = ' || l_orig_rsv_tbl(1).demand_source_line_id);
13118        debug_print('demand_source_line_detail = ' || l_orig_rsv_tbl(1).demand_source_line_detail);
13119        debug_print('To record:');
13120        debug_print('demand_source_type_id = ' || l_to_rsv_rec.demand_source_type_id);
13121        debug_print('demand_source_header_id = ' || l_to_rsv_rec.demand_source_header_id);
13122        debug_print('demand_source_line_id = ' || l_to_rsv_rec.demand_source_line_id);
13123        debug_print('demand_source_line_detail = ' || l_to_rsv_rec.demand_source_line_detail);
13124     END IF;
13125 
13126     IF (l_to_rsv_rec.demand_source_type_id IN
13127 	(inv_reservation_global.g_source_type_wip,
13128 	 inv_reservation_global.g_source_type_oe,
13129 	 inv_reservation_global.g_source_type_internal_ord,
13130 	 inv_reservation_global.g_source_type_rma)) AND
13131       p_over_reservation_flag NOT IN (2,3) THEN
13132        -- call the helper procedure to get the reservable qty of the demand
13133        -- Bug 5199672: Should pass g_miss_num as default for demand
13134        -- source line detail. Otherwise, high level reservations
13135        -- will not be considered.
13136        get_demand_reservable_qty
13137          (
13138             x_return_status                 => l_return_status
13139           , x_msg_count                     => x_msg_count
13140           , x_msg_data                      => x_msg_data
13141           , p_fm_demand_source_type_id      => l_orig_rsv_tbl(1).demand_source_type_id
13142           , p_fm_demand_source_header_id    => l_orig_rsv_tbl(1).demand_source_header_id
13143           , p_fm_demand_source_line_id      => l_orig_rsv_tbl(1).demand_source_line_id
13144           , p_fm_demand_source_line_detail  => l_orig_rsv_tbl(1).demand_source_line_detail
13145           , p_fm_primary_reservation_qty    => l_orig_rsv_tbl(1).primary_reservation_quantity
13146           , p_to_demand_source_type_id      => l_to_rsv_rec.demand_source_type_id
13147           , p_to_demand_source_header_id    => l_to_rsv_rec.demand_source_header_id
13148           , p_to_demand_source_line_id      => l_to_rsv_rec.demand_source_line_id
13149           , p_to_demand_source_line_detail  => l_to_rsv_rec.demand_source_line_detail
13150           , p_to_primary_reservation_qty    => l_to_rsv_rec.primary_reservation_quantity
13151           , p_to_organization_id            => l_to_rsv_rec.organization_id
13152           , p_to_inventory_item_id          => l_to_rsv_rec.inventory_item_id
13153           , p_to_primary_uom_code           => l_to_rsv_rec.primary_uom_code
13154           , p_to_project_id                 => l_to_rsv_rec.project_id
13155           , p_to_task_id                    => l_to_rsv_rec.task_id
13156 	 , x_reservable_qty                => l_quantity_reserved
13157 	 , x_qty_available                 => l_qty_available
13158           );
13159 
13160        IF (l_debug = 1) THEN
13161 	  debug_print('After calling available demand to reserve ' || l_return_status);
13162 	  debug_print('Available quantity to reserve. l_quantity_reserved: ' || l_quantity_reserved);
13163        END IF;
13164 
13165        --
13166        IF l_return_status = fnd_api.g_ret_sts_error THEN
13167 	  RAISE fnd_api.g_exc_error;
13168        END IF;
13169 
13170        --
13171        IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13172 	  RAISE fnd_api.g_exc_unexpected_error;
13173        END IF;
13174 
13175        IF ((l_to_rsv_rec.primary_reservation_quantity - l_quantity_reserved)
13176 	 > 0.000005) THEN
13177 
13178           IF (l_debug = 1) THEN
13179              debug_print('The demand document doesnt have enough quantity to be reserved against. error out. ');
13180              debug_print('l_to_rsv_rec.primary_reservation_quantity: ' || l_to_rsv_rec.primary_reservation_quantity);
13181           END IF;
13182 	  fnd_message.set_name('INV', 'INV_INVALID_AVAILABLE_QTY');
13183 	  fnd_msg_pub.ADD;
13184 	  RAISE fnd_api.g_exc_error;
13185 
13186        END IF;
13187 
13188     END IF;
13189     /*** End R12 ***/
13190 
13191     IF (l_debug = 1) THEN
13192        debug_print('After calling create/modify tree ' || l_return_status);
13193     END IF;
13194 
13195     -- obtain program and user info
13196     l_date := SYSDATE;
13197 
13198     --
13199     l_user_id        := fnd_global.user_id;
13200     l_login_id       := fnd_global.login_id;
13201 
13202     IF l_login_id = -1 THEN
13203       l_login_id  := fnd_global.conc_login_id;
13204     END IF;
13205 
13206     l_request_id     := fnd_global.conc_request_id;
13207     l_prog_appl_id   := fnd_global.prog_appl_id;
13208     l_program_id     := fnd_global.conc_program_id;
13209 
13210     --
13211     --  actions based on l_transfer_all and l_to_row_exist
13212     --  l_transfer_all    l_to_row_exist     from row            to row
13213     --    true                true            delete             add qty
13214     --    true                false           update all but id  nothing
13215     --    false               true            reduce qty         add qty
13216     --    false               false           reduce qty         create
13217     --
13218     -- for from row
13219     IF l_transfer_all = FALSE THEN
13220        IF (l_debug = 1) THEN
13221           debug_print('Transfer all is false');
13222        END IF;
13223       -- Pre Update CTO Validation
13224       IF l_orig_rsv_tbl(1).demand_source_type_id IN (inv_reservation_global.g_source_type_oe, inv_reservation_global.g_source_type_internal_ord, inv_reservation_global.g_source_type_rma) THEN
13225 	 --
13226 	IF (l_debug = 1) THEN
13227            debug_print('Before calling cto work flow unresv check');
13228         END IF;
13229         cto_workflow_api_pk.inventory_unreservation_check(
13230           p_order_line_id              => l_orig_rsv_tbl(1).demand_source_line_id
13231         , p_rsv_quantity               => l_to_rsv_rec.primary_reservation_quantity
13232         , x_return_status              => l_return_status
13233         , x_msg_count                  => x_msg_count
13234         , x_msg_data                   => x_msg_data
13235         );
13236 
13237         --
13238         IF l_return_status = fnd_api.g_ret_sts_error THEN
13239           RAISE fnd_api.g_exc_error;
13240         END IF;
13241 
13242         --
13243         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13244           RAISE fnd_api.g_exc_unexpected_error;
13245         END IF;
13246 	--
13247 	IF (l_debug = 1) THEN
13248            debug_print('After calling cto work flow unresv check');
13249         END IF;
13250       END IF;
13251 
13252       -- I don't call the table handler since only the quantities are changed
13253       -- along with who columns
13254 
13255       /*
13256       ** Include detailed quantity too in the update...
13257       */
13258 
13259       IF (l_orig_rsv_tbl(1).detailed_quantity IS NULL) THEN
13260         l_detailed_quantity  := NULL;
13261       END IF;
13262 
13263       IF (l_orig_rsv_tbl(1).detailed_quantity = 0) THEN
13264         l_detailed_quantity  := 0;
13265       END IF;
13266 
13267       IF (l_orig_rsv_tbl(1).detailed_quantity > 0) THEN
13268         l_detailed_quantity  := l_orig_rsv_tbl(1).detailed_quantity - l_to_rsv_rec.primary_reservation_quantity;
13269 
13270         -- INVCONV BEGIN -  populate secondary for dual tracked item
13271         IF l_dual_tracking THEN
13272           l_secondary_detailed_quantity  :=
13273              l_orig_rsv_tbl(1).secondary_detailed_quantity - l_to_rsv_rec.secondary_reservation_quantity;
13274         END IF;
13275         -- INVCONV END
13276 
13277         IF (l_detailed_quantity < 0) THEN
13278           l_detailed_quantity  := 0;
13279           -- INVCONV BEGIN
13280           IF l_dual_tracking THEN
13281             l_secondary_detailed_quantity  := 0;
13282           END IF;
13283           -- INVCONV END
13284         END IF;
13285       END IF;
13286 
13287       --bug 2186857
13288       --The update statement below did a straight subtraction of the
13289       -- to rsv qty from the original rsv qty.  However, this did not take
13290       -- into account that the two reservations could have different
13291       -- reservation UOMs.  So, before the update, we must determine
13292       -- the new original reservation qty
13293       l_new_orig_prim_qty  := l_orig_rsv_tbl(1).primary_reservation_quantity - l_to_rsv_rec.primary_reservation_quantity;
13294       l_primary_uom_code   := l_to_rsv_rec.primary_uom_code;
13295 
13296       IF l_orig_rsv_tbl(1).reservation_uom_code IS NULL THEN
13297         l_reservation_uom_code  := l_primary_uom_code;
13298       ELSE
13299         l_reservation_uom_code  := l_orig_rsv_tbl(1).reservation_uom_code;
13300       END IF;
13301 
13302       IF l_primary_uom_code <> l_reservation_uom_code THEN
13303         -- INVCONV - Upgrade call to inv_um_convert to pass lot and org
13304         l_new_orig_rsv_qty  := inv_convert.inv_um_convert(
13305                                  item_id                      => l_orig_rsv_tbl(1).inventory_item_id
13306                                , lot_number                   => l_orig_rsv_tbl(1).lot_number
13307                                , organization_id              => l_orig_rsv_tbl(1).organization_id
13308                                , PRECISION                    => NULL -- use default precision
13309                                , from_quantity                => l_new_orig_prim_qty
13310                                , from_unit                    => l_primary_uom_code
13311                                , to_unit                      => l_reservation_uom_code
13312                                , from_name                    => NULL -- from uom name
13313                                , to_name                      => NULL -- to uom name
13314                                );
13315 
13316         IF l_new_orig_rsv_qty = -99999 THEN
13317           -- conversion failed
13318           fnd_message.set_name('INV', 'CAN-NOT-CONVERT-TO-PRIMARY-UOM');
13319           fnd_msg_pub.ADD;
13320           RAISE fnd_api.g_exc_error;
13321         END IF;
13322       ELSE
13323         l_new_orig_rsv_qty  := l_new_orig_prim_qty;
13324       END IF;
13325 
13326       -- INVCONV BEGIN
13327       IF l_dual_tracking THEN
13328        l_orig_second_rsv_qty :=
13329          l_orig_rsv_tbl(1).secondary_reservation_quantity - NVL(l_to_rsv_rec.secondary_reservation_quantity,0);
13330       END IF;
13331       -- INVCONV END
13332 
13333       --Bug #2819700
13334       --Adding an extra check to make sure that tranfer reservations does not
13335       -- update the original reservation record to a NEGATIVE NUMBER.
13336       IF (l_debug = 1) THEN
13337         debug_print('Primary_reservation_qty before inserting (xfer)= '
13338 		                || To_char(l_new_orig_prim_qty) );
13339         debug_print('Secondary_reservation_qty before inserting (xfer)= '
13340                                 || To_char(l_orig_second_rsv_qty) );        -- INVCONV
13341         debug_print('Reservation_qty before inserting (xfer)= '
13342 		                || To_char(l_new_orig_rsv_qty) );
13343       END IF;
13344 
13345       IF (  (NVL(l_new_orig_rsv_qty,0) < 0) OR
13346 	          (NVL(l_new_orig_prim_qty,0) < 0) ) THEN
13347         fnd_message.set_name('INV', 'INV-INVALID RESERVATION QTY');
13348         fnd_msg_pub.ADD;
13349         RAISE fnd_api.g_exc_error;
13350       END IF;
13351 
13352        -- INVCONV BEGIN
13353       IF (NVL(l_orig_second_rsv_qty,0) < 0) THEN
13354         fnd_message.set_name('INV', 'INV-INVALID NEGATIVE SECONDARY'); -- INVCONV New Message
13355         fnd_msg_pub.ADD;
13356         RAISE fnd_api.g_exc_error;
13357       END IF;
13358       -- INVCONV END
13359       -- Bug 3461990: Reservations API should not update reservations with more
13360       -- than 5 decimal places, since the transaction quantity is being
13361       -- rounded to 5 decimal places.
13362 
13363       l_new_orig_prim_qty :=
13364 	round(l_new_orig_prim_qty,5);
13365       -- INVCONV BEGIN
13366       IF l_dual_tracking THEN
13367         l_orig_second_rsv_qty :=
13368           round(l_orig_second_rsv_qty,5);
13369       END IF;
13370       -- INVCONV END
13371 
13372       l_new_orig_rsv_qty :=
13373 	round(l_new_orig_rsv_qty,5);
13374       l_detailed_quantity  :=
13375 	round(Nvl(l_detailed_quantity,0),5);
13376 
13377 
13378       IF (l_debug = 1) THEN
13379 	 debug_print(' Transfer: Before updating from record');
13380 	 debug_print(' After rounding reservation is' || l_orig_rsv_tbl(1).reservation_id);
13381 	 debug_print(' After rounding reservation qty' || l_new_orig_rsv_qty);
13382 	 debug_print(' After rounding reservation pri qty' || l_new_orig_prim_qty);
13383          debug_print(' After rounding reservation sec qty' || l_orig_second_rsv_qty);
13384 	 debug_print(' After rounding detailed quantity' || l_detailed_quantity);
13385       END IF;
13386 
13387       -- INVCONV - Incorporate secondary_reservation_quantity
13388       UPDATE mtl_reservations
13389          SET primary_reservation_quantity = l_new_orig_prim_qty
13390            , secondary_reservation_quantity = l_orig_second_rsv_qty
13391            , reservation_quantity = l_new_orig_rsv_qty
13392            , detailed_quantity = l_detailed_quantity
13393            , last_update_date = l_date
13394            , last_updated_by = l_user_id
13395            , last_update_login = l_login_id
13396            , request_id = l_request_id
13397            , program_application_id = l_prog_appl_id
13398            , program_id = l_program_id
13399            , program_update_date = l_date
13400        WHERE reservation_id = l_orig_rsv_tbl(1).reservation_id;
13401 
13402       -- for data sync b/w mtl_demand and mtl_reservations
13403       inv_rsv_synch.for_update(p_reservation_id => l_orig_rsv_tbl(1).reservation_id, x_return_status => l_return_status, x_msg_count => x_msg_count, x_msg_data => x_msg_data);
13404 
13405       IF l_return_status = fnd_api.g_ret_sts_error THEN
13406         RAISE fnd_api.g_exc_error;
13407       END IF;
13408 
13409       --
13410       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13411         RAISE fnd_api.g_exc_unexpected_error;
13412       END IF;
13413       --
13414     /** Commented out by request of CTO.  This was causing an extra
13415      *  update to the workflow that was causing problems.
13416      *  Bug 2073768
13417      *  This code was not interacting correctly with CTO.
13418      *  No longer call the wf_update_after_inv_unreserv api from
13419      *  transfer_reservation
13420      *-- Post Update CTO Validation
13421      * IF l_orig_rsv_tbl(1).demand_source_type_id in (
13422      *    inv_reservation_global.g_source_type_oe
13423      *         ,inv_reservation_global.g_source_type_internal_ord
13424      *         ,inv_reservation_global.g_source_type_rma) THEN
13425      *     --
13426      *     cto_workflow_api_pk.wf_update_after_inv_unreserv(
13427      *       p_order_line_id      => l_orig_rsv_tbl(1).demand_source_line_id
13428      *     , x_return_status      => l_return_status
13429      *     , x_msg_count          => x_msg_count
13430      *     , x_msg_data           => x_msg_data
13431      *     );
13432      *     --
13433      *     IF l_return_status = fnd_api.g_ret_sts_error THEN
13434      *      RAISE fnd_api.g_exc_error;
13435      *    END IF ;
13436      *     --
13437      *     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13438      *      RAISE fnd_api.g_exc_unexpected_error;
13439      *    END IF;
13440      *     --
13441      * END IF;
13442      */
13443        ELSIF l_to_row_exist = TRUE THEN
13444        IF (l_debug = 1) THEN
13445           debug_print('To row exists and transfer all is true');
13446        END IF;
13447        -- Pre Delete CTO Validation
13448       IF l_orig_rsv_tbl(1).demand_source_type_id IN (inv_reservation_global.g_source_type_oe, inv_reservation_global.g_source_type_internal_ord, inv_reservation_global.g_source_type_rma) THEN
13449         --
13450         cto_workflow_api_pk.inventory_unreservation_check(
13451           p_order_line_id              => l_orig_rsv_tbl(1).demand_source_line_id
13452         , p_rsv_quantity               => l_to_rsv_rec.primary_reservation_quantity
13453         , x_return_status              => l_return_status
13454         , x_msg_count                  => x_msg_count
13455         , x_msg_data                   => x_msg_data
13456         );
13457 
13458         --
13459         IF l_return_status = fnd_api.g_ret_sts_error THEN
13460           RAISE fnd_api.g_exc_error;
13461         END IF;
13462 
13463         --
13464         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13465           RAISE fnd_api.g_exc_unexpected_error;
13466         END IF;
13467       --
13468       END IF;
13469 
13470       -- for data sync b/w mtl_demand and mtl_reservations
13471       inv_rsv_synch.for_delete(p_reservation_id => l_orig_rsv_tbl(1).reservation_id, x_return_status => l_return_status, x_msg_count => x_msg_count, x_msg_data => x_msg_data);
13472 
13473       IF l_return_status = fnd_api.g_ret_sts_error THEN
13474         RAISE fnd_api.g_exc_error;
13475       END IF;
13476 
13477       --
13478       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13479         RAISE fnd_api.g_exc_unexpected_error;
13480       END IF;
13481 
13482       -- 2884492
13483        mtl_reservations_pkg.delete_row
13484 	(x_reservation_id => l_orig_rsv_tbl(1).reservation_id
13485 	,x_to_reservation_id => l_to_rsv_tbl(1).reservation_id);
13486     /** commented out by request of CTO.  The workflow was not processing
13487      *  correctly.
13488      *  Bug 2073768
13489      *  This code was not interacting correctly with CTO.
13490      *  No longer call the wf_update_after_inv_unreserv api from
13491      *  transfer_reservation
13492      *-- Post Delete CTO Validation
13493      * IF l_orig_rsv_tbl(1).demand_source_type_id in (
13494      *    inv_reservation_global.g_source_type_oe
13495      *          ,inv_reservation_global.g_source_type_internal_ord
13496      *          ,inv_reservation_global.g_source_type_rma) THEN
13497      *     --
13498      *     cto_workflow_api_pk.wf_update_after_inv_unreserv(
13499      *       p_order_line_id      => l_orig_rsv_tbl(1).demand_source_line_id
13500      *     , x_return_status      => l_return_status
13501      *     , x_msg_count          => x_msg_count
13502      *     , x_msg_data           => x_msg_data
13503      *     );
13504      *     --
13505      *     IF l_return_status = fnd_api.g_ret_sts_error THEN
13506      *      RAISE fnd_api.g_exc_error;
13507      *    END IF ;
13508      *     --
13509      *     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13510      *      RAISE fnd_api.g_exc_unexpected_error;
13511      *    END IF;
13512      *     --
13513      * END IF;
13514      */
13515        ELSE
13516        IF (l_debug = 1) THEN
13517           debug_print('To row does not exist and transfer all is true');
13518        END IF;
13519       -- Pre Update CTO Validation
13520       IF l_orig_rsv_tbl(1).demand_source_type_id IN (inv_reservation_global.g_source_type_oe, inv_reservation_global.g_source_type_internal_ord, inv_reservation_global.g_source_type_rma) THEN
13521         --
13522         cto_workflow_api_pk.inventory_unreservation_check(
13523           p_order_line_id              => l_orig_rsv_tbl(1).demand_source_line_id
13524         , p_rsv_quantity               => l_orig_rsv_tbl(1).primary_reservation_quantity
13525         , x_return_status              => l_return_status
13526         , x_msg_count                  => x_msg_count
13527         , x_msg_data                   => x_msg_data
13528         );
13529 
13530         --
13531         IF l_return_status = fnd_api.g_ret_sts_error THEN
13532           RAISE fnd_api.g_exc_error;
13533         END IF;
13534 
13535         --
13536         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13537           RAISE fnd_api.g_exc_unexpected_error;
13538         END IF;
13539       --
13540       END IF;
13541 
13542       -- Added for bug 2458523
13543       -- Pre Insert CTO Validation
13544       IF l_to_rsv_rec.demand_source_type_id IN
13545 	(inv_reservation_global.g_source_type_oe,
13546 	 inv_reservation_global.g_source_type_internal_ord,
13547 	 inv_reservation_global.g_source_type_rma) THEN
13548 
13549         --
13550         cto_workflow_api_pk.inventory_reservation_check(
13551           p_order_line_id              => l_to_rsv_rec.demand_source_line_id
13552         , x_return_status              => l_return_status
13553         , x_msg_count                  => x_msg_count
13554         , x_msg_data                   => x_msg_data
13555         );
13556 
13557         --
13558         IF l_return_status = fnd_api.g_ret_sts_error THEN
13559           RAISE fnd_api.g_exc_error;
13560         END IF;
13561 
13562         --
13563         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13564           RAISE fnd_api.g_exc_unexpected_error;
13565         END IF;
13566       --
13567       END IF;
13568 
13569       --Bug #2819700
13570       --Adding an extra check to make sure that tranfer reservations does not
13571       -- update the to reservation record to a NEGATIVE NUMBER.
13572       IF (l_debug = 1) THEN
13573         debug_print('Primary_reservation_qty before inserting (xfer)= '
13574 		                || To_char(l_to_rsv_rec.primary_reservation_quantity) );
13575         debug_print('Secondary_reservation_qty before inserting (xfer)= '
13576                                 || To_char(l_to_rsv_rec.secondary_reservation_quantity) );   -- INVCONV
13577         debug_print('Reservation_qty before inserting (xfer)= '
13578 		                || To_char(l_to_rsv_rec.reservation_quantity) );
13579       END IF;
13580 
13581       IF (  (NVL(l_to_rsv_rec.reservation_quantity,0) < 0) OR
13582 	    (NVL(l_to_rsv_rec.primary_reservation_quantity,0) < 0) ) THEN
13583 	 fnd_message.set_name('INV', 'INV-INVALID RESERVATION QTY');
13584 	 fnd_msg_pub.ADD;
13585 	 RAISE fnd_api.g_exc_error;
13586       END IF;
13587 
13588        -- INVCONV BEGIN
13589       IF (NVL(l_to_rsv_rec.secondary_reservation_quantity,0) < 0) THEN
13590          fnd_message.set_name('INV', 'INV-INVALID NEGATIVE SECONDARY'); -- INVCONV New Message
13591          fnd_msg_pub.ADD;
13592          RAISE fnd_api.g_exc_error;
13593       END IF;
13594       -- INVCONV END
13595 
13596       -- Bug 3461990: Reservations API should not update reservations with more
13597       -- than 5 decimal places, since the transaction quantity is being
13598       -- rounded to 5 decimal places.
13599 
13600       l_to_rsv_rec.primary_reservation_quantity :=
13601 	round(l_to_rsv_rec.primary_reservation_quantity,5);
13602 
13603       -- INVCONV BEGIN
13604       IF l_dual_tracking THEN
13605         l_to_rsv_rec.secondary_reservation_quantity :=
13606           round(l_to_rsv_rec.secondary_reservation_quantity,5);
13607       END IF;
13608       -- INVCONV END
13609 
13610       l_to_rsv_rec.reservation_quantity :=
13611 	round(l_to_rsv_rec.reservation_quantity,5);
13612       l_to_rsv_rec.detailed_quantity  :=
13613 	round(Nvl(l_to_rsv_rec.detailed_quantity,0),5);
13614 
13615 
13616       IF (l_debug = 1) THEN
13617 	 debug_print(' Transfer: Before updating all but id for the from record');
13618 	 debug_print(' After rounding reservation is' || l_orig_rsv_tbl(1).reservation_id);
13619 	 debug_print(' After rounding reservation qty' || l_to_rsv_rec.reservation_quantity);
13620 	 debug_print(' After rounding reservation pri qty' || l_to_rsv_rec.primary_reservation_quantity);
13621 	 debug_print(' After rounding detailed quantity' ||l_to_rsv_rec.detailed_quantity );
13622       END IF;
13623 
13624       -- INVCONV - Incorporate secondaries in update
13625       mtl_reservations_pkg.update_row
13626 	(
13627 	 x_reservation_id             => l_orig_rsv_tbl(1).reservation_id
13628 	 , x_requirement_date           => l_to_rsv_rec.requirement_date
13629 	 , x_organization_id            => l_to_rsv_rec.organization_id
13630 	 , x_inventory_item_id          => l_to_rsv_rec.inventory_item_id
13631 	 , x_demand_source_type_id      => l_to_rsv_rec.demand_source_type_id
13632 	 , x_demand_source_header_id    => l_to_rsv_rec.demand_source_header_id
13633 	 , x_demand_source_line_id      => l_to_rsv_rec.demand_source_line_id
13634 	 , x_demand_source_name         => l_to_rsv_rec.demand_source_name
13635 	 , x_demand_source_delivery     => l_to_rsv_rec.demand_source_delivery
13636 	 , x_primary_uom_code           => l_to_rsv_rec.primary_uom_code
13637 	 , x_primary_uom_id             => l_to_rsv_rec.primary_uom_id
13638 	 , x_secondary_uom_code         => l_to_rsv_rec.secondary_uom_code
13639 	 , x_secondary_uom_id           => l_to_rsv_rec.secondary_uom_id
13640 	 , x_reservation_uom_code       => l_to_rsv_rec.reservation_uom_code
13641 	 , x_reservation_uom_id         => l_to_rsv_rec.reservation_uom_id
13642 	, x_reservation_quantity       => l_to_rsv_rec.reservation_quantity
13643 	, x_primary_reservation_quantity=> l_to_rsv_rec.primary_reservation_quantity
13644 	, x_second_reservation_quantity=> l_to_rsv_rec.secondary_reservation_quantity
13645 	, x_detailed_quantity          => l_to_rsv_rec.detailed_quantity
13646 	, x_secondary_detailed_quantity=> l_to_rsv_rec.secondary_detailed_quantity
13647 	, x_autodetail_group_id        => l_to_rsv_rec.autodetail_group_id
13648 	, x_external_source_code       => l_to_rsv_rec.external_source_code
13649 	, x_external_source_line_id    => l_to_rsv_rec.external_source_line_id
13650 	, x_supply_source_type_id      => l_to_rsv_rec.supply_source_type_id
13651 	, x_supply_source_header_id    => l_to_rsv_rec.supply_source_header_id
13652 	, x_supply_source_line_id      => l_to_rsv_rec.supply_source_line_id
13653 	, x_supply_source_name         => l_to_rsv_rec.supply_source_name
13654 	, x_supply_source_line_detail  => l_to_rsv_rec.supply_source_line_detail
13655 	, x_revision                   => l_to_rsv_rec.revision
13656 	, x_subinventory_code          => l_to_rsv_rec.subinventory_code
13657 	, x_subinventory_id            => l_to_rsv_rec.subinventory_id
13658 	, x_locator_id                 => l_to_rsv_rec.locator_id
13659 	, x_lot_number                 => l_to_rsv_rec.lot_number
13660 	, x_lot_number_id              => l_to_rsv_rec.lot_number_id
13661 	, x_serial_number              => NULL
13662 	, x_serial_number_id           => NULL
13663 	, x_partial_quantities_allowed => NULL
13664 	, x_auto_detailed              => NULL
13665 	, x_pick_slip_number           => l_to_rsv_rec.pick_slip_number
13666 	, x_lpn_id                     => l_to_rsv_rec.lpn_id
13667 	, x_last_update_date           => l_date
13668 	, x_last_updated_by            => l_user_id
13669 	, x_last_update_login          => l_login_id
13670 	, x_request_id                 => l_request_id
13671 	, x_program_application_id     => l_prog_appl_id
13672 	, x_program_id                 => l_program_id
13673 	, x_program_update_date        => l_date
13674 	, x_attribute_category         => l_to_rsv_rec.attribute_category
13675 	, x_attribute1                 => l_to_rsv_rec.attribute1
13676 	, x_attribute2                 => l_to_rsv_rec.attribute2
13677 	, x_attribute3                 => l_to_rsv_rec.attribute3
13678 	, x_attribute4                 => l_to_rsv_rec.attribute4
13679 	, x_attribute5                 => l_to_rsv_rec.attribute5
13680 	, x_attribute6                 => l_to_rsv_rec.attribute6
13681 	, x_attribute7                 => l_to_rsv_rec.attribute7
13682 	, x_attribute8                 => l_to_rsv_rec.attribute8
13683 	, x_attribute9                 => l_to_rsv_rec.attribute9
13684 	, x_attribute10                => l_to_rsv_rec.attribute10
13685 	, x_attribute11                => l_to_rsv_rec.attribute11
13686 	, x_attribute12                => l_to_rsv_rec.attribute12
13687 	, x_attribute13                => l_to_rsv_rec.attribute13
13688 	, x_attribute14                => l_to_rsv_rec.attribute14
13689 	, x_attribute15                => l_to_rsv_rec.attribute15
13690 	, x_ship_ready_flag            => l_to_rsv_rec.ship_ready_flag
13691 	, x_staged_flag                => l_to_rsv_rec.staged_flag
13692 	/**** {{ R12 Enhanced reservations code changes }}****/
13693 	, x_crossdock_flag             => l_to_rsv_rec.crossdock_flag
13694 	, x_crossdock_criteria_id      => l_to_rsv_rec.crossdock_criteria_id
13695 	, x_demand_source_line_detail  => l_to_rsv_rec.demand_source_line_detail
13696 	, x_serial_reservation_quantity => l_to_rsv_rec.serial_reservation_quantity
13697 	, x_supply_receipt_date        => l_to_rsv_rec.supply_receipt_date
13698 	, x_demand_ship_date           => l_to_rsv_rec.demand_ship_date
13699 	, x_project_id                 => l_to_rsv_rec.project_id
13700 	, x_task_id                    => l_to_rsv_rec.task_id
13701 	/*** End R12 ***/
13702 	);
13703       --
13704       -- for data sync b/w mtl_demand and mtl_reservations
13705       inv_rsv_synch.for_update(p_reservation_id => l_orig_rsv_tbl(1).reservation_id, x_return_status => l_return_status, x_msg_count => x_msg_count, x_msg_data => x_msg_data);
13706 
13707       debug_print(' return status after updating row' || l_return_status);
13708 
13709       IF l_return_status = fnd_api.g_ret_sts_error THEN
13710         RAISE fnd_api.g_exc_error;
13711       END IF;
13712 
13713       --
13714       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13715         RAISE fnd_api.g_exc_unexpected_error;
13716       END IF;
13717 
13718       --
13719       -- since to row not exists, transfer all
13720       x_reservation_id  := l_orig_rsv_tbl(1).reservation_id;
13721 
13722       IF l_orig_rsv_tbl(1).demand_source_type_id = 2   -- Bug 6195783 : Begin
13723       AND l_to_rsv_rec.demand_source_type_id = 9
13724       AND l_orig_rsv_tbl(1).primary_reservation_quantity = l_to_rsv_rec.primary_reservation_quantity THEN
13725          cto_workflow_api_pk.wf_update_after_inv_unreserv(
13726          p_order_line_id      => l_orig_rsv_tbl(1).demand_source_line_id
13727          , x_return_status      => l_return_status
13728          , x_msg_count          => x_msg_count
13729          , x_msg_data           => x_msg_data
13730          );
13731       END IF;                                          -- Bug 6195783 : End
13732 
13733     /** commented out by request of CTO.  Their workflow was not
13734      *  progressing properly
13735      *  Bug 2073768
13736      *  This code was not interacting correctly with CTO.
13737      *  No longer call the wf_update_after_inv_unreserv api from
13738      *  transfer_reservation
13739      *-- Post Update CTO Validation
13740      * IF l_orig_rsv_tbl(1).demand_source_type_id in (
13741      *    inv_reservation_global.g_source_type_oe
13742      *          ,inv_reservation_global.g_source_type_internal_ord
13743      *          ,inv_reservation_global.g_source_type_rma) THEN
13744      *     --
13745      *     cto_workflow_api_pk.wf_update_after_inv_unreserv(
13746      *       p_order_line_id      => l_orig_rsv_tbl(1).demand_source_line_id
13747      *     , x_return_status      => l_return_status
13748      *     , x_msg_count          => x_msg_count
13749      *     , x_msg_data           => x_msg_data
13750      *     );
13751      *     --
13752      *     IF l_return_status = fnd_api.g_ret_sts_error THEN
13753      *      RAISE fnd_api.g_exc_error;
13754      *    END IF ;
13755      *     --
13756      *     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13757      *      RAISE fnd_api.g_exc_unexpected_error;
13758      *    END IF;
13759      *     --
13760      * END IF;
13761      */
13762     END IF;
13763 
13764     --
13765      IF l_to_row_exist = TRUE THEN
13766 	IF (l_debug = 1) THEN
13767            debug_print('To row does exists');
13768         END IF;
13769 
13770       /* Commenting out call to CTO inventory_unreservation_check API. This
13771 	API has already been called in the previous if then else. This
13772 	  issue has been reported in bug 2458523
13773       -- Pre Update CTO Validation
13774       IF l_to_rsv_rec.demand_source_type_id IN (inv_reservation_global.g_source_type_oe, inv_reservation_global.g_source_type_internal_ord, inv_reservation_global.g_source_type_rma) THEN
13775         --
13776         cto_workflow_api_pk.inventory_unreservation_check(
13777           p_order_line_id              => l_to_rsv_rec.demand_source_line_id
13778         , p_rsv_quantity               => l_to_rsv_rec.primary_reservation_quantity
13779         , x_return_status              => l_return_status
13780         , x_msg_count                  => x_msg_count
13781         , x_msg_data                   => x_msg_data
13782         );
13783 
13784         --
13785         IF l_return_status = fnd_api.g_ret_sts_error THEN
13786           RAISE fnd_api.g_exc_error;
13787         END IF;
13788 
13789         --
13790         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13791           RAISE fnd_api.g_exc_unexpected_error;
13792         END IF;
13793       --
13794       END IF;
13795       */
13796 
13797       -- Added for bug 2458523
13798       -- Pre Insert CTO Validation
13799       IF l_to_rsv_rec.demand_source_type_id IN
13800 	  (inv_reservation_global.g_source_type_oe,
13801 	   inv_reservation_global.g_source_type_internal_ord,
13802 	   inv_reservation_global.g_source_type_rma) THEN
13803 
13804         --
13805         cto_workflow_api_pk.inventory_reservation_check(
13806           p_order_line_id              => l_to_rsv_rec.demand_source_line_id
13807         , x_return_status              => l_return_status
13808         , x_msg_count                  => x_msg_count
13809         , x_msg_data                   => x_msg_data
13810         );
13811 
13812         --
13813         IF l_return_status = fnd_api.g_ret_sts_error THEN
13814           RAISE fnd_api.g_exc_error;
13815         END IF;
13816 
13817         --
13818         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13819           RAISE fnd_api.g_exc_unexpected_error;
13820         END IF;
13821       --
13822       END IF;
13823 
13824       l_primary_rsv_quantity := 0;
13825       l_rsv_quantity := 0;
13826 
13827        -- INVCONV - Retrieve secondaries
13828       SELECT  primary_reservation_quantity, secondary_reservation_quantity, reservation_quantity
13829       INTO    l_primary_rsv_quantity, l_secondary_rsv_quantity, l_rsv_quantity
13830             FROM    mtl_reservations
13831       WHERE   reservation_id = l_to_rsv_tbl(1).reservation_id;
13832 
13833       l_primary_rsv_quantity := l_primary_rsv_quantity + l_to_rsv_rec.primary_reservation_quantity;
13834 
13835        -- INVCONV BEGIN
13836       IF l_dual_tracking THEN
13837         l_secondary_rsv_quantity :=l_secondary_rsv_quantity + l_to_rsv_rec.secondary_reservation_quantity;
13838       END IF;
13839       -- INVCONV END
13840 
13841       l_rsv_quantity :=l_rsv_quantity + l_to_rsv_rec.reservation_quantity;
13842 
13843       --Bug #2819700
13844       --Adding an extra check to make sure that tranfer reservations does not
13845       --update the to reservation record to a NEGATIVE NUMBER.
13846       IF (l_debug = 1) THEN
13847         debug_print('Primary_reservation_qty before inserting (xfer)= ' || To_char(l_primary_rsv_quantity) );
13848         debug_print('Secondary_reservation_qty before inserting (xfer)= ' || To_char(l_secondary_rsv_quantity) ); --INVCONV
13849         debug_print('Rreservation_qty before inserting (xfer)= ' ||To_char(l_rsv_quantity));
13850       END IF;
13851 
13852       IF (  (NVL(l_primary_rsv_quantity,0) < 0) OR
13853 	          (NVL(l_rsv_quantity,0)< 0) ) THEN
13854        fnd_message.set_name('INV', 'INV-INVALID RESERVATION QTY');
13855         fnd_msg_pub.ADD;
13856         RAISE fnd_api.g_exc_error;
13857       END IF;
13858 
13859        -- INVCONV BEGIN
13860       IF (NVL(l_secondary_rsv_quantity,0) < 0) THEN
13861         fnd_message.set_name('INV', 'INV-INVALID NEGATIVE SECONDARY'); -- INVCONV New Message
13862         fnd_msg_pub.ADD;
13863         RAISE fnd_api.g_exc_error;
13864       END IF;
13865       -- INVCONV END
13866 
13867       -- Bug 3461990: Reservations API should not be updated with more
13868       -- than 5 decimal places, since the transaction quantity is being
13869       -- rounded to 5 decimal places.
13870       IF (l_debug = 1) THEN
13871 	 debug_print(' Transfer: Before adding the qty to the to record');
13872       END IF;
13873 
13874       -- wont call table handler since only quantities are changed along
13875       -- with the who column this is simpler to read and understand
13876 
13877       -- INVCONV - Incorporate secondary_reservation_quantity which could be null
13878       UPDATE mtl_reservations
13879          SET primary_reservation_quantity = Round((primary_reservation_quantity + l_to_rsv_rec.primary_reservation_quantity),5)
13880            , secondary_reservation_quantity = Round((secondary_reservation_quantity + l_to_rsv_rec.secondary_reservation_quantity),5)
13881            , reservation_quantity = Round((reservation_quantity + l_to_rsv_rec.reservation_quantity),5)
13882            , detailed_quantity = Round(NVL(detailed_quantity, 0) + NVL(l_to_rsv_rec.detailed_quantity, 0),5)
13883            , last_update_date = l_date
13884            , last_updated_by = l_user_id
13885            , last_update_login = l_login_id
13886            , request_id = l_request_id
13887            , program_application_id = l_prog_appl_id
13888            , program_id = l_program_id
13889            , program_update_date = l_date
13890        WHERE reservation_id = l_to_rsv_tbl(1).reservation_id;
13891 
13892       -- for data sync b/w mtl_demand and mtl_reservations
13893       inv_rsv_synch.for_update(p_reservation_id => l_to_rsv_tbl(1).reservation_id, x_return_status => l_return_status, x_msg_count => x_msg_count, x_msg_data => x_msg_data);
13894 
13895       IF l_return_status = fnd_api.g_ret_sts_error THEN
13896         RAISE fnd_api.g_exc_error;
13897       END IF;
13898 
13899       --
13900       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13901         RAISE fnd_api.g_exc_unexpected_error;
13902       END IF;
13903 
13904       --
13905       x_reservation_id  := l_to_rsv_tbl(1).reservation_id;
13906     /** Commented out by request of CTO.  Their workflow was not
13907      *  progressing correctly
13908      *  Bug 2073768
13909      *  This code was not interacting correctly with CTO.
13910      *  No longer call the wf_update_after_inv_unreserv api from
13911      *  transfer_reservation
13912      *-- Post Update CTO Validation
13913      * IF l_to_rsv_rec.demand_source_type_id in (
13914      *   inv_reservation_global.g_source_type_oe
13915      *         ,inv_reservation_global.g_source_type_internal_ord
13916      *        ,inv_reservation_global.g_source_type_rma) THEN
13917      *    --
13918      *    cto_workflow_api_pk.wf_update_after_inv_unreserv(
13919      *      p_order_line_id      => l_to_rsv_rec.demand_source_line_id
13920      *    , x_return_status      => l_return_status
13921      *    , x_msg_count          => x_msg_count
13922      *    , x_msg_data           => x_msg_data
13923      *    );
13924      *    --
13925      *    IF l_return_status = fnd_api.g_ret_sts_error THEN
13926      *     RAISE fnd_api.g_exc_error;
13927      *   END IF ;
13928      *    --
13929      *    IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13930      *     RAISE fnd_api.g_exc_unexpected_error;
13931      *   END IF;
13932      *    --
13933      *END IF;
13934      */
13935        ELSIF l_transfer_all = FALSE THEN
13936 	IF (l_debug = 1) THEN
13937            debug_print('To row does not exists and transfer all is false');
13938         END IF;
13939       -- Pre Insert CTO Validation
13940       IF l_to_rsv_rec.demand_source_type_id IN (inv_reservation_global.g_source_type_oe, inv_reservation_global.g_source_type_internal_ord, inv_reservation_global.g_source_type_rma) THEN
13941         --
13942         cto_workflow_api_pk.inventory_reservation_check(
13943           p_order_line_id              => l_to_rsv_rec.demand_source_line_id
13944         , x_return_status              => l_return_status
13945         , x_msg_count                  => x_msg_count
13946         , x_msg_data                   => x_msg_data
13947         );
13948 
13949         --
13950         IF l_return_status = fnd_api.g_ret_sts_error THEN
13951           RAISE fnd_api.g_exc_error;
13952         END IF;
13953 
13954         --
13955         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13956           RAISE fnd_api.g_exc_unexpected_error;
13957         END IF;
13958       --
13959       END IF;
13960 
13961       --Bug #2819700
13962       --Adding an extra check to make sure that tranfer reservations does not
13963       -- insert the to reservation record to a NEGATIVE NUMBER.
13964       IF (l_debug = 1) THEN
13965         debug_print('Primary_reservation_qty before inserting (xfer)= '
13966 		                || To_char(l_to_rsv_rec.primary_reservation_quantity) );
13967          debug_print('Secondary_reservation_qty before inserting (xfer)= '
13968                                 || To_char(l_to_rsv_rec.secondary_reservation_quantity) ); --INVCONV
13969         debug_print('Reservation_qty before inserting (xfer)= '
13970 		                || To_char(l_to_rsv_rec.reservation_quantity) );
13971       END IF;
13972 
13973       IF (  (NVL(l_to_rsv_rec.reservation_quantity,0) < 0) OR
13974 	    (NVL(l_to_rsv_rec.primary_reservation_quantity,0) < 0) ) THEN
13975 	 fnd_message.set_name('INV', 'INV-INVALID RESERVATION QTY');
13976 	 fnd_msg_pub.ADD;
13977 	 RAISE fnd_api.g_exc_error;
13978       END IF;
13979 
13980        -- INVCONV BEGIN
13981       IF (NVL(l_to_rsv_rec.secondary_reservation_quantity,0) < 0) THEN
13982          fnd_message.set_name('INV', 'INV-INVALID NEGATIVE SECONDARY'); -- INVCONV New Message
13983          fnd_msg_pub.ADD;
13984          RAISE fnd_api.g_exc_error;
13985       END IF;
13986       -- INVCONV END
13987 
13988 
13989       -- Bug 3461990: Reservations API should not create reservations with more
13990       -- than 5 decimal places, since the transaction quantity is being
13991       -- rounded to 5 decimal places.
13992 
13993       l_to_rsv_rec.primary_reservation_quantity :=
13994 	round(l_to_rsv_rec.primary_reservation_quantity,5);
13995       -- INVCONV BEGIN
13996       IF l_dual_tracking THEN
13997         l_to_rsv_rec.secondary_reservation_quantity :=
13998           round(l_to_rsv_rec.secondary_reservation_quantity,5);
13999       END IF;
14000       -- INVCONV END
14001       l_to_rsv_rec.reservation_quantity  :=
14002 	round(l_to_rsv_rec.reservation_quantity,5);
14003       l_to_rsv_rec.detailed_quantity  :=
14004 	round(Nvl(l_to_rsv_rec.detailed_quantity,0),5);
14005 
14006 
14007       IF (l_debug = 1) THEN
14008 	 debug_print(' Transfer: Before creating new reservations for the to record');
14009 	 debug_print(' After rounding reservation is' || l_orig_rsv_tbl(1).reservation_id);
14010 	 debug_print(' After rounding reservation qty' || l_to_rsv_rec.reservation_quantity);
14011 	 debug_print(' After rounding reservation pri qty' || l_to_rsv_rec.primary_reservation_quantity);
14012          debug_print(' After rounding reservation sec qty' || l_to_rsv_rec.secondary_reservation_quantity);
14013 	 debug_print(' After rounding detailed quantity' ||l_to_rsv_rec.detailed_quantity );
14014       END IF;
14015 
14016       /**** {{ R12 Enhanced reservations code changes }}****/
14017       -- if the reservation being transferred is from PO to ASN
14018       -- we need to update the original supply as PO, because we
14019       -- will have to transfer the reservation back to ASN while reducing
14020       -- and cancelling ASNs
14021       IF (l_to_rsv_rec.supply_source_type_id =
14022 	  inv_reservation_global.g_source_type_asn) AND
14023 	(l_orig_rsv_tbl(1).supply_source_type_id = inv_reservation_global.g_source_type_po) THEN
14024 
14025 	 l_orig_supply_type_id := inv_reservation_global.g_source_type_po;
14026 	 IF (l_debug = 1) THEN
14027 	    debug_print('The original supply is po and the new supply is asn' ||l_orig_supply_type_id);
14028 	 END IF;
14029 
14030       END IF;
14031 
14032       /*** End R12 ***/
14033       -- create reservation id
14034      /* SELECT mtl_demand_s.NEXTVAL
14035         INTO l_reservation_id
14036         FROM DUAL; */
14037 	-- Bug 55350300 --Selecting the sequence value has been moved to the table handler
14038 	-- in INVRSV6B.pls
14039 
14040 	l_reservation_id := NULL;
14041       -- INVCONV - Incorporate secondary columns
14042       mtl_reservations_pkg.insert_row
14043 	(
14044 	 x_rowid                      => l_rowid
14045 	 , x_reservation_id             => l_reservation_id
14046 	 , x_requirement_date           => l_to_rsv_rec.requirement_date
14047 	 , x_organization_id            => l_to_rsv_rec.organization_id
14048 	 , x_inventory_item_id          => l_to_rsv_rec.inventory_item_id
14049 	 , x_demand_source_type_id      => l_to_rsv_rec.demand_source_type_id
14050 	 , x_demand_source_name         => l_to_rsv_rec.demand_source_name
14051 	 , x_demand_source_header_id    => l_to_rsv_rec.demand_source_header_id
14052 	 , x_demand_source_line_id      => l_to_rsv_rec.demand_source_line_id
14053 	 , x_demand_source_delivery     => l_to_rsv_rec.demand_source_delivery
14054 	 , x_primary_uom_code           => l_to_rsv_rec.primary_uom_code
14055 	 , x_primary_uom_id             => l_to_rsv_rec.primary_uom_id
14056 	 , x_secondary_uom_code         => l_to_rsv_rec.secondary_uom_code
14057 	 , x_secondary_uom_id           => l_to_rsv_rec.secondary_uom_id
14058 	 , x_reservation_uom_code       => l_to_rsv_rec.reservation_uom_code
14059 	 , x_reservation_uom_id         => l_to_rsv_rec.reservation_uom_id
14060 	, x_reservation_quantity       => l_to_rsv_rec.reservation_quantity
14061 	, x_primary_reservation_quantity=> l_to_rsv_rec.primary_reservation_quantity
14062 	, x_second_reservation_quantity=> l_to_rsv_rec.secondary_reservation_quantity
14063 	, x_detailed_quantity          => l_to_rsv_rec.detailed_quantity
14064 	, x_secondary_detailed_quantity => l_to_rsv_rec.secondary_detailed_quantity
14065 	, x_autodetail_group_id        => l_to_rsv_rec.autodetail_group_id
14066 	, x_external_source_code       => l_to_rsv_rec.external_source_code
14067 	, x_external_source_line_id    => l_to_rsv_rec.external_source_line_id
14068 	, x_supply_source_type_id      => l_to_rsv_rec.supply_source_type_id
14069 	, x_supply_source_header_id    => l_to_rsv_rec.supply_source_header_id
14070 	, x_supply_source_line_id      => l_to_rsv_rec.supply_source_line_id
14071 	, x_supply_source_line_detail  => l_to_rsv_rec.supply_source_line_detail
14072 	, x_supply_source_name         => l_to_rsv_rec.supply_source_name
14073 	, x_revision                   => l_to_rsv_rec.revision
14074 	, x_subinventory_code          => l_to_rsv_rec.subinventory_code
14075 	, x_subinventory_id            => l_to_rsv_rec.subinventory_id
14076 	, x_locator_id                 => l_to_rsv_rec.locator_id
14077 	, x_lot_number                 => l_to_rsv_rec.lot_number
14078 	, x_lot_number_id              => l_to_rsv_rec.lot_number_id
14079 	, x_serial_number              => NULL
14080 	, x_serial_number_id           => NULL
14081 	, x_partial_quantities_allowed => NULL
14082 	, x_auto_detailed              => NULL
14083 	, x_pick_slip_number           => l_to_rsv_rec.pick_slip_number
14084 	, x_lpn_id                     => l_to_rsv_rec.lpn_id
14085 	, x_last_update_date           => l_date
14086 	, x_last_updated_by            => l_user_id
14087 	, x_creation_date              => l_date
14088 	, x_created_by                 => l_user_id
14089 	, x_last_update_login          => l_login_id
14090 	, x_request_id                 => l_request_id
14091 	, x_program_application_id     => l_prog_appl_id
14092 	, x_program_id                 => l_program_id
14093 	, x_program_update_date        => l_date
14094 	, x_attribute_category         => l_to_rsv_rec.attribute_category
14095 	, x_attribute1                 => l_to_rsv_rec.attribute1
14096 	, x_attribute2                 => l_to_rsv_rec.attribute2
14097 	, x_attribute3                 => l_to_rsv_rec.attribute3
14098 	, x_attribute4                 => l_to_rsv_rec.attribute4
14099 	, x_attribute5                 => l_to_rsv_rec.attribute5
14100 	, x_attribute6                 => l_to_rsv_rec.attribute6
14101 	, x_attribute7                 => l_to_rsv_rec.attribute7
14102 	, x_attribute8                 => l_to_rsv_rec.attribute8
14103 	, x_attribute9                 => l_to_rsv_rec.attribute9
14104 	, x_attribute10                => l_to_rsv_rec.attribute10
14105 	, x_attribute11                => l_to_rsv_rec.attribute11
14106 	, x_attribute12                => l_to_rsv_rec.attribute12
14107 	, x_attribute13                => l_to_rsv_rec.attribute13
14108 	, x_attribute14                => l_to_rsv_rec.attribute14
14109 	, x_attribute15                => l_to_rsv_rec.attribute15
14110 	, x_ship_ready_flag            => l_to_rsv_rec.ship_ready_flag
14111 	, x_staged_flag                => l_to_rsv_rec.staged_flag
14112 	/**** {{ R12 Enhanced reservations code changes }}****/
14113 	, x_crossdock_flag             => l_to_rsv_rec.crossdock_flag
14114 	, x_crossdock_criteria_id      => l_to_rsv_rec.crossdock_criteria_id
14115 	, x_demand_source_line_detail  => l_to_rsv_rec.demand_source_line_detail
14116 	, x_serial_reservation_quantity => l_to_rsv_rec.serial_reservation_quantity
14117 	, x_supply_receipt_date        => l_to_rsv_rec.supply_receipt_date
14118 	, x_demand_ship_date             => l_to_rsv_rec.demand_ship_date
14119 	, x_project_id                   => l_to_rsv_rec.project_id
14120 	, x_task_id                      => l_to_rsv_rec.task_id
14121 	, x_orig_supply_type_id   => l_orig_supply_type_id
14122 	, x_orig_supply_header_id => l_to_rsv_rec.supply_source_header_id
14123 	, x_orig_supply_line_id     => l_to_rsv_rec.supply_source_line_id
14124 	, x_orig_supply_line_detail => l_to_rsv_rec.supply_source_line_detail
14125 	, x_orig_demand_type_id     => l_to_rsv_rec.demand_source_type_id
14126 	, x_orig_demand_header_id   => l_to_rsv_rec.demand_source_header_id
14127 	, x_orig_demand_line_id     => l_to_rsv_rec.demand_source_line_id
14128 	, x_orig_demand_line_detail => l_to_rsv_rec.demand_source_line_detail
14129 	/*** End R12 ***/
14130 	);
14131 
14132       debug_print(' After call to insert_row : reservation_id : ' || l_reservation_id);
14133 
14134       -- insert into mtl_reservations
14135       x_reservation_id  := l_reservation_id;
14136 
14137       -- for data sync b/w mtl_demand and mtl_reservations
14138       inv_rsv_synch.for_insert(p_reservation_id => l_reservation_id, x_return_status => l_return_status, x_msg_count => x_msg_count, x_msg_data => x_msg_data);
14139 
14140       IF l_return_status = fnd_api.g_ret_sts_error THEN
14141         RAISE fnd_api.g_exc_error;
14142       END IF;
14143 
14144       --
14145       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14146         RAISE fnd_api.g_exc_unexpected_error;
14147       END IF;
14148     --
14149     END IF;
14150 
14151     debug_print(' Before serial number check' || l_return_status);
14152 
14153     -- Bug 2073768
14154     -- The order status was not being updated in correctly in the
14155     -- sales order, becuase we were only calling this cto api for
14156     -- partial reservation transfers.  Now, call this API everytime
14157     -- transfer_reservation is called
14158     -- Post Insert CTO Validation
14159     IF l_to_rsv_rec.demand_source_type_id IN (inv_reservation_global.g_source_type_oe, inv_reservation_global.g_source_type_internal_ord, inv_reservation_global.g_source_type_rma) THEN
14160       --
14161       cto_workflow_api_pk.wf_update_after_inv_reserv(p_order_line_id => l_to_rsv_rec.demand_source_line_id, x_return_status => l_return_status, x_msg_count => x_msg_count, x_msg_data => x_msg_data);
14162 
14163       debug_print(' After CTO API' || l_return_status);
14164       --
14165       IF l_return_status = fnd_api.g_ret_sts_error THEN
14166         RAISE fnd_api.g_exc_error;
14167       END IF;
14168 
14169       --
14170       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14171         RAISE fnd_api.g_exc_unexpected_error;
14172       END IF;
14173     --
14174     END IF;
14175 
14176     /**** {{ R12 Enhanced reservations code changes }}****/
14177     -- Handling of serial numbers starts here. This block has the changes
14178     -- related to serial reservations.
14179 
14180     -- get all the from serials that have been reserved.
14181     --check to see if serial are reserved.
14182 
14183        BEGIN
14184 	  SELECT inventory_item_id, serial_number bulk collect INTO
14185 	    l_serial_number_table FROM
14186 	    mtl_serial_numbers  WHERE reservation_id =
14187 	    l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
14188 	    l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
14189        EXCEPTION
14190 	  WHEN no_data_found THEN
14191 	     IF l_debug=1 THEN
14192 		debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
14193 	     END IF;
14194        END;
14195 
14196        l_total_serials_reserved := l_serial_number_table.COUNT;
14197        IF l_debug=1 THEN
14198 	  debug_print('Total reserved serials: ' || l_total_serials_reserved);
14199        END IF;
14200 
14201        -- exceed the reservation quantity.
14202        IF (l_to_row_exist) THEN
14203 	  l_to_reservation_id := l_to_rsv_tbl(1).reservation_id;
14204 	ELSIF (l_transfer_all) THEN
14205 	  l_to_reservation_id := l_orig_rsv_tbl(1).reservation_id;
14206 	ELSE
14207 	  l_to_reservation_id := l_reservation_id;
14208        END IF;
14209 
14210        --check to see if serial are reserved.
14211 	BEGIN
14212 	   SELECT primary_reservation_quantity INTO
14213 	     l_from_primary_reservation_qty FROM mtl_reservations WHERE
14214 	     reservation_id =  l_orig_rsv_tbl(1).reservation_id;
14215 	EXCEPTION
14216 	   WHEN no_data_found THEN
14217 	      IF l_debug=1 THEN
14218 		 debug_print('This is case 1. The from reservation recordhas been deleted. Id: ' || l_orig_rsv_tbl(1).reservation_id);
14219 	      END IF;
14220 	END;
14221 	BEGIN
14222 	   SELECT primary_reservation_quantity INTO
14223 	     l_to_primary_reservation_qty FROM mtl_reservations WHERE
14224 	     reservation_id =  l_to_reservation_id;
14225 	EXCEPTION
14226 	   WHEN no_data_found THEN
14227 	      IF l_debug=1 THEN
14228 		 debug_print('This is case 2. The to reservation record has been deleted. Id: ' || l_to_reservation_id);
14229 	      END IF;
14230 	END;
14231 	l_to_primary_reservation_qty :=
14232 	  Nvl(l_to_primary_reservation_qty,0);
14233 	l_from_primary_reservation_qty := Nvl(l_from_primary_reservation_qty,0);
14234 
14235 	debug_print(' Before Serial param' || l_return_status);
14236 
14237 	--Bug 5198421: If no serials are passed and no serial are reserved
14238 	-- set the serial reservation qty and do nothing.
14239 	IF ((l_serial_param = 1) AND (l_total_serials_reserved = 0)) THEN
14240 	   -- set the serial reservation qty to zero
14241 	   BEGIN
14242 	      UPDATE mtl_reservations SET serial_reservation_quantity = 0
14243 		WHERE reservation_id = l_orig_rsv_tbl(1).reservation_id;
14244 	   EXCEPTION
14245 	      WHEN no_data_found THEN
14246 		 IF l_debug=1 THEN
14247 		    debug_print('Could not find the reservation record: ' || l_orig_rsv_tbl(1).reservation_id);
14248 		 END IF;
14249 	   END;
14250 	END IF;
14251 
14252 
14253 	IF (l_serial_param = 1) AND (l_total_serials_reserved > 0) THEN
14254 	   -- This means that no serials are being passed.
14255 	   -- There are three conditions that we have to handle.
14256 	   --1. If transfer all is true and to row exists, then move everything
14257 	   -- to the to record
14258 	   --2. If transfer all is true and to row does not exist, then we have
14259 	   -- to do nothing as it is the same row that is being updated.
14260 	   --3. If transfer_all is false, then move the excess serials to the
14261 	   -- to record
14262 	   IF l_debug=1 THEN
14263 	      debug_print('Inside param 1');
14264 	      debug_print('Original tbl(1)Org id: ' || l_orig_rsv_tbl(1).organization_id);
14265 	      debug_print('Original tbl(1)Item id: ' || l_orig_rsv_tbl(1).inventory_item_id);
14266 	   END IF;
14267 
14268 	   IF ((l_total_serials_reserved >
14269 		l_from_primary_reservation_qty) OR (l_transfer_all AND
14270 						    (NOT l_to_row_exist))) THEN
14271 	      -- call validate serials for the to record as we may have to
14272 	     -- transfer the serials AT random.
14273 	     inv_reservation_validate_pvt.validate_serials
14274 	       (
14275 		x_return_status              => l_return_status
14276 		 , p_rsv_action_name         => 'TRANSFER'
14277 		, p_orig_rsv_rec             => l_dummy_rsv_rec
14278 		, p_to_rsv_rec               => l_to_rsv_rec
14279 		, p_orig_serial_array        => l_dummy_serial_array
14280 		, p_to_serial_array          => l_serial_number_table
14281 		);
14282 
14283 	     IF (l_debug = 1) THEN
14284 		debug_print('After calling validate serials ' || l_return_status);
14285 	     END IF;
14286 
14287 	     --
14288 	     IF l_return_status = fnd_api.g_ret_sts_error THEN
14289 		RAISE fnd_api.g_exc_error;
14290 	     END IF;
14291 
14292 	     --
14293 	     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14294 		RAISE fnd_api.g_exc_unexpected_error;
14295 	     END IF;
14296 	  END IF;
14297 
14298 	  IF (l_total_serials_reserved > 0) THEN
14299 	     IF (l_transfer_all AND l_to_row_exist) THEN
14300 		debug_print('Inside param 1. transfer_all and to_row_exists');
14301 		-- validate the serials with the to record and move everything.
14302 		IF (l_debug = 1) THEN
14303 		   debug_print('Original serial count: ' || l_original_serial_count);
14304 		   debug_print('To serial count: ' || l_to_serial_count);
14305 		END IF;
14306 
14307 		-- both from and to serial tables are empty. They are not passed.
14308 		IF (l_debug = 1) THEN
14309 		   debug_print('Inside serial check. Not passed');
14310 		END IF;
14311 
14312 		IF (l_total_serials_reserved > l_to_primary_reservation_qty) THEN
14313 		   fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
14314 		   fnd_msg_pub.ADD;
14315 		   RAISE fnd_api.g_exc_error;
14316 		END IF;
14317 		-- we will have to migrate the serials to the new reservation
14318 		-- Just make sure that the group_mark_id and the
14319 		-- reservation id are populated.
14320 		FOR l_serial_index IN l_serial_number_table.first..l_serial_number_table.last
14321 		  LOOP
14322 	            BEGIN
14323 		       UPDATE mtl_serial_numbers SET reservation_id = l_to_reservation_id,
14324 			 group_mark_id = l_to_reservation_id WHERE
14325 			 serial_number = l_serial_number_table(l_serial_index).serial_number AND
14326 			 inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id AND
14327 			 current_organization_id = l_orig_rsv_tbl(1).organization_id;
14328 		    EXCEPTION
14329 		       WHEN no_data_found THEN
14330 			  IF l_debug=1 THEN
14331 			     debug_print('No serials found for serial number. : ' || l_serial_number_table(l_serial_index).serial_number);
14332 			  END IF;
14333 			  fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
14334 			  fnd_msg_pub.ADD;
14335 			  RAISE fnd_api.g_exc_error;
14336 		    END;
14337 		    IF l_debug=1 THEN
14338 		       debug_print('Serial being migrated. serial number: ' || l_serial_number_table(l_serial_index).serial_number);
14339 		    END IF;
14340 		  END LOOP;
14341 
14342 		  -- update the serial reservation quantity
14343 		  update_serial_rsv_quantity
14344 		    (x_return_status => l_return_status
14345 		     , x_msg_count   => x_msg_count
14346 		     , x_msg_data    => x_msg_data
14347 		     , p_reservation_id  => l_to_reservation_id
14348 		     , p_update_serial_qty => l_total_serials_reserved
14349 		     );
14350 
14351 		  IF (l_debug = 1) THEN
14352 		     debug_print('After calling update serial reservations ' || l_return_status);
14353 		  END IF;
14354 
14355 		  IF l_return_status = fnd_api.g_ret_sts_error THEN
14356 		     RAISE fnd_api.g_exc_error;
14357 		  END IF;
14358 
14359 		  IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14360 		     RAISE fnd_api.g_exc_unexpected_error;
14361 		  END IF;
14362 
14363 	      ELSIF (NOT l_transfer_all) THEN
14364 	         -- transfer the excess to the to record there are some serial reserved. Check if the
14365 		 -- number of serials
14366 		IF (l_debug = 1) THEN
14367 			debug_print('Inside param 1. Not l_transfer_all');
14368 		END IF;
14369 
14370 		IF (l_total_serials_reserved > l_from_primary_reservation_qty) THEN
14371 		   -- we have to unreserve some of the serials
14372 		   -- unreserve the extra serials that are more than the primary_reservation_quantity
14373 		   IF (l_debug = 1) THEN
14374 		      debug_print('Total serials more than from reservation quantity.');
14375 		   END IF;
14376 
14377 		   l_serials_tobe_unreserved := l_total_serials_reserved - l_from_primary_reservation_qty;
14378 
14379 		   FOR i IN 1..l_serials_tobe_unreserved
14380 		     LOOP
14381 	               BEGIN
14382 			  UPDATE mtl_serial_numbers SET reservation_id = l_to_reservation_id,
14383 			    group_mark_id = l_to_reservation_id WHERE
14384 			    serial_number = l_serial_number_table(i).serial_number AND
14385 			    inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id AND
14386 			    current_organization_id = l_orig_rsv_tbl(1).organization_id;
14387 		       EXCEPTION
14388 			  WHEN no_data_found THEN
14389 			     IF l_debug=1 THEN
14390 				debug_print('No serials found for Serial Number: ' || l_serial_number_table(i).serial_number);
14391 			     END IF;
14392 			     fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
14393 			     fnd_msg_pub.ADD;
14394 			     RAISE fnd_api.g_exc_error;
14395 		       END;
14396 
14397 		       IF l_debug=1 THEN
14398 			  debug_print('Serial being unreserved. serial number: ' || l_serial_number_table(i).serial_number);
14399 		       END IF;
14400 
14401 		     END LOOP;
14402 		     -- update the serial reservation quantity to be the primary
14403 		     -- reservation quantity, as it is in excess.
14404 		     -- update the serial reservation quantity
14405 		     update_serial_rsv_quantity
14406 		       (x_return_status => l_return_status
14407 			, x_msg_count   => x_msg_count
14408 			, x_msg_data    => x_msg_data
14409 			, p_reservation_id  => l_orig_rsv_tbl(1).reservation_id
14410 			, p_update_serial_qty => -l_serials_tobe_unreserved
14411 			);
14412 
14413 		     IF (l_debug = 1) THEN
14414 			debug_print('After calling update serial reservations ' || l_return_status);
14415 		     END IF;
14416 
14417 		     IF l_return_status = fnd_api.g_ret_sts_error THEN
14418 			RAISE fnd_api.g_exc_error;
14419 		     END IF;
14420 
14421 		     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14422 			RAISE fnd_api.g_exc_unexpected_error;
14423 		     END IF;
14424 
14425 		     update_serial_rsv_quantity
14426 		       (x_return_status => l_return_status
14427 			, x_msg_count   => x_msg_count
14428 			, x_msg_data    => x_msg_data
14429 			, p_reservation_id  => l_to_reservation_id
14430 			, p_update_serial_qty => l_serials_tobe_unreserved
14431 			);
14432 
14433 		     IF (l_debug = 1) THEN
14434 			debug_print('After calling update serial reservations ' || l_return_status);
14435 		     END IF;
14436 
14437 		     IF l_return_status = fnd_api.g_ret_sts_error THEN
14438 			RAISE fnd_api.g_exc_error;
14439 		     END IF;
14440 
14441 		     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14442 			RAISE fnd_api.g_exc_unexpected_error;
14443 		     END IF;
14444 
14445 		     -- check to see if we are transferring more than the
14446 		     -- reserved qty
14447 		     BEGIN
14448 			SELECT COUNT(1) INTO l_total_to_serials_reserved FROM
14449 			  mtl_serial_numbers  WHERE reservation_id =
14450 			  l_to_reservation_id AND current_organization_id =
14451 			  l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
14452 		     EXCEPTION
14453 			WHEN no_data_found THEN
14454 			   IF l_debug=1 THEN
14455 			      debug_print('No serials found for reservation record. id: ' || l_to_reservation_id);
14456 			   END IF;
14457 		     END;
14458 		     IF (l_total_to_serials_reserved > l_to_primary_reservation_qty) THEN
14459 			fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
14460 			fnd_msg_pub.ADD;
14461 			RAISE fnd_api.g_exc_error;
14462 		     END IF;
14463 
14464 		END IF; -- total serials more than orig reservation qty
14465 
14466 	      ELSIF (l_transfer_all AND (NOT l_to_row_exist)) THEN
14467 			     -- make sure that the total serials doesnt
14468 			     -- exceed the to reservation count and they
14469 			     -- are validated.
14470 	        IF (l_total_serials_reserved > l_to_primary_reservation_qty) THEN
14471 		   fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
14472 		   fnd_msg_pub.ADD;
14473 		   RAISE fnd_api.g_exc_error;
14474 		END IF;
14475 	  END IF; -- transfer all and to row exists
14476 
14477        END IF; -- serials reserved
14478     END IF;-- l_serial_param = 1
14479 
14480     IF (l_serial_param = 2) THEN
14481        -- We will have to unreserve the from serials and if the serial count
14482        -- exceeds the from primary qty, transfer them to the to record
14483 
14484        -- validate if the passed serials belong to the from reservation
14485        -- record.
14486        IF l_debug=1 THEN
14487 	  debug_print('Inside param 2');
14488 	  debug_print('Original tbl(1)Org id: ' || l_orig_rsv_tbl(1).organization_id);
14489 	  debug_print('Original tbl(1)Item id: ' || l_orig_rsv_tbl(1).inventory_item_id);
14490        END IF;
14491 
14492        -- The serial has to be reserved to the from record
14493        FOR i IN p_original_serial_number.first..p_original_serial_number.last
14494 	 LOOP
14495 	   BEGIN
14496 	      SELECT reservation_id INTO l_from_reservation_id FROM mtl_serial_numbers WHERE
14497 		serial_number = p_original_serial_number(i).serial_number AND
14498 		current_organization_id = l_orig_rsv_tbl(1).organization_id AND
14499 		inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
14500 	   EXCEPTION
14501 	      WHEN no_data_found THEN
14502 		 IF l_debug=1 THEN
14503 		    debug_print('No serials found for this data : ' || p_to_serial_number(i).serial_number);
14504 		 END IF;
14505 	   END;
14506 
14507 	   -- if the serial is not reserved or if the serial belongs to a
14508 	   -- different reservation record, then fail
14509 	   IF (l_from_reservation_id IS NOT NULL AND l_from_reservation_id <>
14510 	       l_orig_rsv_tbl(1).reservation_id) OR (l_from_reservation_id IS NULL) THEN
14511 	      fnd_message.set_name('INV', 'INV_INVALID_FROM_SERIAL');
14512 	      fnd_msg_pub.ADD;
14513 	      RAISE fnd_api.g_exc_error;
14514 	   END IF;
14515 
14516 	   IF (l_total_serials_reserved > 0) THEN
14517 	   -- unreserve the passed serials.
14518 	     BEGIN
14519 		UPDATE mtl_serial_numbers SET reservation_id = NULL,
14520 		  group_mark_id = NULL, line_mark_id = NULL,
14521 		  lot_line_mark_id = NULL WHERE
14522 		  serial_number = p_original_serial_number(i).serial_number AND
14523 		  current_organization_id = l_orig_rsv_tbl(1).organization_id AND
14524 		  inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
14525 	     EXCEPTION
14526 		WHEN no_data_found THEN
14527 		   IF l_debug=1 THEN
14528 		      debug_print('No serials found for this data : ' || p_to_serial_number(i).serial_number);
14529 		   END IF;
14530 	     END;
14531 	   END IF;
14532 	 END LOOP;
14533 
14534 	 IF (l_total_serials_reserved > 0) THEN
14535 	    IF (l_transfer_all AND l_to_row_exist) THEN
14536 	       -- if more serials are reserved,
14537 	       -- transfer them to the to record
14538 	       -- validate the serials before transferring them to to
14539 	       -- record.
14540 	       IF (l_debug = 1) THEN
14541 		  debug_print('Inside param 2. transfer_all and to_row_exists');
14542 	       END IF;
14543 
14544 	       BEGIN
14545 		  SELECT inventory_item_id, serial_number bulk collect INTO
14546 		    l_validate_serial_number_table FROM
14547 		    mtl_serial_numbers  WHERE reservation_id =
14548 		    l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
14549 		    l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
14550 	       EXCEPTION
14551 		  WHEN no_data_found THEN
14552 		     IF l_debug=1 THEN
14553 			debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
14554 		     END IF;
14555 	       END;
14556 
14557 	       l_validate_serials_reserved := l_validate_serial_number_table.COUNT;
14558 
14559 	       IF l_debug=1 THEN
14560 		  debug_print('Total reserved serials: ' || l_validate_serials_reserved);
14561 	       END IF;
14562 
14563 	       IF (l_validate_serials_reserved > 0) THEN
14564 		  -- from record still has some reserved serials afer
14565 		  -- unreserving the passed serials. transfer them to the to_record
14566 		  inv_reservation_validate_pvt.validate_serials
14567 		    (
14568 		     x_return_status              => l_return_status
14569 		     , p_rsv_action_name          => 'TRANSFER'
14570 		     , p_orig_rsv_rec             => l_dummy_rsv_rec
14571 		     , p_to_rsv_rec               => l_to_rsv_rec
14572 		     , p_orig_serial_array        => l_dummy_serial_array
14573 		     , p_to_serial_array          => l_validate_serial_number_table
14574 		     );
14575 
14576 		  IF (l_debug = 1) THEN
14577 		     debug_print('After calling validate serials ' || l_return_status);
14578 		  END IF;
14579 
14580 		  --
14581 		  IF l_return_status = fnd_api.g_ret_sts_error THEN
14582 		     RAISE fnd_api.g_exc_error;
14583 		  END IF;
14584 
14585 		  --
14586 		  IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14587 		     RAISE fnd_api.g_exc_unexpected_error;
14588 		  END IF;
14589 	       END IF;
14590 
14591                BEGIN
14592 		  UPDATE mtl_serial_numbers SET reservation_id = l_to_reservation_id,
14593 		    group_mark_id = l_to_reservation_id WHERE
14594 		    reservation_id = l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
14595 		    l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
14596 
14597 	       EXCEPTION
14598 		  WHEN no_data_found THEN
14599 		     IF l_debug=1 THEN
14600 			debug_print('No serials found for this data. rsv id: ' || l_orig_rsv_tbl(1).reservation_id);
14601 		     END IF;
14602 		     fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
14603 		     fnd_msg_pub.ADD;
14604 		     RAISE fnd_api.g_exc_error;
14605 	       END;
14606 
14607 	       -- check to see if we have reserved more than the to_record
14608 	       BEGIN
14609 		  SELECT COUNT(1) INTO l_total_to_serials_reserved FROM
14610 		    mtl_serial_numbers  WHERE reservation_id =
14611 		    l_to_reservation_id AND current_organization_id =
14612 		    l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
14613 	       EXCEPTION
14614 		  WHEN no_data_found THEN
14615 		     IF l_debug=1 THEN
14616 			debug_print('No serials found for reservation record. id: ' || l_to_reservation_id);
14617 		     END IF;
14618 	       END;
14619 
14620 	       IF (l_total_to_serials_reserved > l_to_primary_reservation_qty) THEN
14621 		  fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
14622 		  fnd_msg_pub.ADD;
14623 		  RAISE fnd_api.g_exc_error;
14624 	       END IF;
14625 
14626 		 -- update with whatever is being moved to the to record
14627 		 update_serial_rsv_quantity
14628 		   (x_return_status => l_return_status
14629 		    , x_msg_count   => x_msg_count
14630 		    , x_msg_data    => x_msg_data
14631 		    , p_reservation_id  => l_to_reservation_id
14632 		    , p_update_serial_qty => l_validate_serial_number_table.count
14633 		    );
14634 
14635 		 IF (l_debug = 1) THEN
14636 		    debug_print('After calling update serial reservations ' || l_return_status);
14637 		 END IF;
14638 
14639 		 IF l_return_status = fnd_api.g_ret_sts_error THEN
14640 		    RAISE fnd_api.g_exc_error;
14641 		 END IF;
14642 
14643 		 IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14644 		    RAISE fnd_api.g_exc_unexpected_error;
14645 		 END IF;
14646 
14647 	     ELSIF (NOT l_transfer_all) THEN
14648 		 --the from serials are already unreserved. If there are
14649 		 -- more serials, validate and move the excess to the
14650 		 -- to record
14651 		     IF (l_debug = 1) THEN
14652 			debug_print('Inside param 2. not transfer_all');
14653 		     END IF;
14654 		 BEGIN
14655 		    SELECT inventory_item_id, serial_number bulk collect INTO
14656 		      l_validate_serial_number_table FROM
14657 		      mtl_serial_numbers  WHERE reservation_id =
14658 		      l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
14659 		      l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
14660 		 EXCEPTION
14661 		    WHEN no_data_found THEN
14662 		       IF l_debug=1 THEN
14663 			  debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
14664 		       END IF;
14665 		 END;
14666 
14667 		 l_validate_serials_reserved := l_validate_serial_number_table.COUNT;
14668 
14669 		 IF l_debug=1 THEN
14670 		    debug_print('Total reserved serials: ' || l_validate_serials_reserved);
14671 		 END IF;
14672 
14673 		 IF (l_validate_serials_reserved > 0) AND (l_validate_serials_reserved > l_from_primary_reservation_qty) THEN
14674 		    inv_reservation_validate_pvt.validate_serials
14675 		      (
14676 		       x_return_status              => l_return_status
14677 		        , p_rsv_action_name         => 'TRANSFER'
14678 		       , p_orig_rsv_rec             => l_dummy_rsv_rec
14679 		       , p_to_rsv_rec               => l_to_rsv_rec
14680 		       , p_orig_serial_array        => l_dummy_serial_array
14681 		       , p_to_serial_array          => l_validate_serial_number_table
14682 		       );
14683 
14684 		    IF (l_debug = 1) THEN
14685 		       debug_print('After calling validate serials ' || l_return_status);
14686 		    END IF;
14687 
14688 		    --
14689 		    IF l_return_status = fnd_api.g_ret_sts_error THEN
14690 		       RAISE fnd_api.g_exc_error;
14691 		    END IF;
14692 
14693 		    --
14694 		    IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14695 		       RAISE fnd_api.g_exc_unexpected_error;
14696 		    END IF;
14697 
14698 		    -- we have to unreserve some of the serials
14699 		    -- unreserve the extra serials that are more than the primary_reservation_quantity
14700 		    IF (l_debug = 1) THEN
14701 		       debug_print('Total serials more than reservation quantity.');
14702 		    END IF;
14703 
14704 		    l_serials_tobe_unreserved := l_validate_serials_reserved -  l_from_primary_reservation_qty;
14705 
14706 		    FOR i IN 1..l_serials_tobe_unreserved
14707 		      LOOP
14708 	               BEGIN
14709 			  UPDATE mtl_serial_numbers SET reservation_id = l_to_reservation_id,
14710 			    group_mark_id = l_to_reservation_id WHERE
14711 			    serial_number = l_validate_serial_number_table(i).serial_number AND
14712 			    inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id AND
14713 			    current_organization_id = l_orig_rsv_tbl(1).organization_id;
14714 		       EXCEPTION
14715 			  WHEN no_data_found THEN
14716 			     IF l_debug=1 THEN
14717 				debug_print('No serials found for serial number: ' || l_validate_serial_number_table(i).serial_number);
14718 			     END IF;
14719 			     fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
14720 			     fnd_msg_pub.ADD;
14721 			     RAISE fnd_api.g_exc_error;
14722 		       END;
14723 
14724 		       IF l_debug=1 THEN
14725 			  debug_print('Serial being unreserved. serial number: ' || l_serial_number_table(i).serial_number);
14726 		       END IF;
14727 
14728 		      END LOOP;
14729 
14730 		      BEGIN
14731 			 SELECT COUNT(1) INTO l_total_to_serials_reserved FROM
14732 			   mtl_serial_numbers  WHERE reservation_id =
14733 			   l_to_reservation_id AND current_organization_id =
14734 			   l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
14735 		      EXCEPTION
14736 			 WHEN no_data_found THEN
14737 			    IF l_debug=1 THEN
14738 			       debug_print('No serials found for reservation record. id: ' || l_to_reservation_id);
14739 			    END IF;
14740 		      END;
14741 
14742 		      IF (l_total_to_serials_reserved > l_to_primary_reservation_qty) THEN
14743 			 fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
14744 			 fnd_msg_pub.ADD;
14745 			 RAISE fnd_api.g_exc_error;
14746 		      END IF;
14747 
14748 		      update_serial_rsv_quantity
14749 			(x_return_status => l_return_status
14750 			 , x_msg_count   => x_msg_count
14751 			 , x_msg_data    => x_msg_data
14752 			 , p_reservation_id  => l_to_reservation_id
14753 			 , p_update_serial_qty => l_serials_tobe_unreserved
14754 			 );
14755 
14756 		      IF (l_debug = 1) THEN
14757 			 debug_print('After calling update serial reservations ' || l_return_status);
14758 		      END IF;
14759 
14760 		      IF l_return_status = fnd_api.g_ret_sts_error THEN
14761 			 RAISE fnd_api.g_exc_error;
14762 		      END IF;
14763 
14764 		      IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14765 			 RAISE fnd_api.g_exc_unexpected_error;
14766 		      END IF;
14767 		 END IF; -- total serials more than orig reservation qty
14768 		 -- now update the from reservation record as we have to
14769 		 -- update the unreserved serial and the transferred serial
14770 		 l_serials_unreserved := -(p_original_serial_number.COUNT + Nvl(l_serials_tobe_unreserved,0));
14771 		 IF (l_debug = 1) THEN
14772 		    debug_print('Total serials unreserved ' || l_serials_unreserved);
14773 		 END IF;
14774 		 -- update the serial reservation quantity to be the primary
14775 		 -- reservation quantity, as it is in excess.
14776 		 -- update the serial reservation quantity
14777 		 update_serial_rsv_quantity
14778 		   (x_return_status => l_return_status
14779 		    , x_msg_count   => x_msg_count
14780 		    , x_msg_data    => x_msg_data
14781 		    , p_reservation_id  => l_orig_rsv_tbl(1).reservation_id
14782 		    , p_update_serial_qty => l_serials_unreserved
14783 		    );
14784 
14785 		 IF (l_debug = 1) THEN
14786 		    debug_print('After calling update serial reservation qty ' || l_return_status);
14787 		 END IF;
14788 
14789 		 IF l_return_status = fnd_api.g_ret_sts_error THEN
14790 		    RAISE fnd_api.g_exc_error;
14791 		 END IF;
14792 
14793 		 IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14794 		    RAISE fnd_api.g_exc_unexpected_error;
14795 		 END IF;
14796 
14797 	     ELSIF (l_transfer_all AND (NOT l_to_row_exist)) THEN
14798 		    -- we have already unreserved the serials. Since we are
14799 		    -- working on the same record, we will have to update
14800 		    -- the serial reservation quantity
14801 
14802 		IF (l_debug = 1) THEN
14803 		   debug_print('Inside param 2. transfer_all and not to_row_exist');
14804 		END IF;
14805                 BEGIN
14806 		   SELECT inventory_item_id, serial_number bulk collect INTO
14807 		     l_validate_serial_number_table FROM
14808 		     mtl_serial_numbers  WHERE reservation_id =
14809 		     l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
14810 		     l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
14811 		EXCEPTION
14812 		   WHEN no_data_found THEN
14813 		      IF l_debug=1 THEN
14814 			 debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
14815 		      END IF;
14816 		END;
14817 
14818 		l_validate_serials_reserved := l_validate_serial_number_table.COUNT;
14819 
14820 		IF l_debug=1 THEN
14821 		   debug_print('Total reserved serials: ' || l_validate_serials_reserved);
14822 		END IF;
14823 
14824 		IF (l_validate_serials_reserved > l_to_primary_reservation_qty) THEN
14825 		   fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
14826 		   fnd_msg_pub.ADD;
14827 		   RAISE fnd_api.g_exc_error;
14828 		END IF;
14829 
14830 		IF (l_validate_serials_reserved > 0) THEN
14831 		   inv_reservation_validate_pvt.validate_serials
14832 		     (
14833 		      x_return_status              => l_return_status
14834 		      , p_rsv_action_name         => 'TRANSFER'
14835 		      , p_orig_rsv_rec             => l_dummy_rsv_rec
14836 		      , p_to_rsv_rec               => l_to_rsv_rec
14837 		      , p_orig_serial_array        => l_dummy_serial_array
14838 		      , p_to_serial_array          => l_validate_serial_number_table
14839 		      );
14840 
14841 		   IF (l_debug = 1) THEN
14842 		      debug_print('After calling validate serials ' || l_return_status);
14843 		   END IF;
14844 
14845 		   --
14846 		   IF l_return_status = fnd_api.g_ret_sts_error THEN
14847 		      RAISE fnd_api.g_exc_error;
14848 		   END IF;
14849 
14850 		   --
14851 		   IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14852 		      RAISE fnd_api.g_exc_unexpected_error;
14853 		   END IF;
14854 
14855 		   -- we have to unreserve some of the serials
14856 		   -- unreserve the extra serials that are more than the primary_reservation_quantity
14857 		   IF (l_debug = 1) THEN
14858 		      debug_print('Total serials more than reservation quantity.');
14859 		   END IF;
14860 		END IF;
14861 
14862 		update_serial_rsv_quantity
14863 		  (x_return_status => l_return_status
14864 		   , x_msg_count   => x_msg_count
14865 		   , x_msg_data    => x_msg_data
14866 		   , p_reservation_id  => l_orig_rsv_tbl(1).reservation_id
14867 		   , p_update_serial_qty => -p_original_serial_number.COUNT
14868 		   );
14869 
14870 		IF (l_debug = 1) THEN
14871 		   debug_print('After calling update serial reservation qty ' || l_return_status);
14872 		END IF;
14873 
14874 		IF l_return_status = fnd_api.g_ret_sts_error THEN
14875 		       RAISE fnd_api.g_exc_error;
14876 		END IF;
14877 
14878 		IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14879 		   RAISE fnd_api.g_exc_unexpected_error;
14880 		END IF;
14881 
14882 	    END IF;-- transfer all and to row exists
14883 	 END IF ; -- total serials reserved > 0
14884     END IF; -- param = 2
14885 
14886     IF (l_serial_param = 3) THEN
14887        -- to serials are passed but not the from serial.
14888        -- reserve the serials. if the serial belongs to the from record,
14889        -- transfer it to the to record.
14890 
14891        IF l_debug=1 THEN
14892 	  debug_print('Inside param 3');
14893 	  debug_print('Original tbl(1)Org id: ' || l_orig_rsv_tbl(1).organization_id);
14894 	  debug_print('Original tbl(1)Item id: ' || l_orig_rsv_tbl(1).inventory_item_id);
14895        END IF;
14896 
14897        -- validate the serial numbers passed and reserve them
14898 
14899        FOR i IN p_to_serial_number.first..p_to_serial_number.last
14900 	 LOOP
14901 	   BEGIN
14902 	      IF (l_debug = 1) THEN
14903 		 debug_print('Processing serial number' || p_to_serial_number(i).serial_number);
14904 	      END IF;
14905 
14906 	      SELECT reservation_id, group_mark_id INTO
14907 		l_from_reservation_id, l_group_mark_id FROM mtl_serial_numbers WHERE
14908 		serial_number = p_to_serial_number(i).serial_number AND
14909 		current_organization_id = l_orig_rsv_tbl(1).organization_id AND
14910 		inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
14911 	   EXCEPTION
14912 	      WHEN no_data_found THEN
14913 		 IF l_debug=1 THEN
14914 		    debug_print('No serials found for this data : ' ||
14915 				p_to_serial_number(i).serial_number);
14916 		 END IF;
14917 		 fnd_message.set_name('INV', 'INV_INVALID_FROM_SERIAL');
14918 		 fnd_msg_pub.ADD;
14919 		 RAISE fnd_api.g_exc_error;
14920 	   END;
14921 
14922 	   IF (l_from_reservation_id IS NOT NULL AND l_from_reservation_id <>
14923 	       l_orig_rsv_tbl(1).reservation_id) THEN
14924 	      fnd_message.set_name('INV', 'INV_INVALID_FROM_SERIAL');
14925 	      fnd_msg_pub.ADD;
14926 	      RAISE fnd_api.g_exc_error;
14927 	   END IF;
14928 
14929 	   -- If reservation id is null, then we are reserving a new
14930 	   -- serial. we should check to see if the group mark id is null or
14931 	   -- it is not being used by another transaction.
14932 
14933 	   IF (l_from_reservation_id IS NULL) AND (l_group_mark_id IS NOT NULL)  AND (l_group_mark_id <> -1)THEN
14934 	      IF (l_debug = 1) THEN
14935 		 debug_print('Group Mark Id is not null for serial ' || p_to_serial_number(i).serial_number);
14936 	      END IF;
14937 	      fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
14938 	      fnd_msg_pub.ADD;
14939 	      RAISE fnd_api.g_exc_error;
14940 	   END IF;
14941 
14942 	   BEGIN
14943 	      UPDATE mtl_serial_numbers SET reservation_id = l_to_reservation_id,
14944 		group_mark_id = l_to_reservation_id WHERE
14945 		serial_number = p_to_serial_number(i).serial_number AND
14946 		current_organization_id = l_orig_rsv_tbl(1).organization_id AND
14947 		inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
14948 	   EXCEPTION
14949 	      WHEN no_data_found THEN
14950 		 IF l_debug=1 THEN
14951 		    debug_print('No serials found for this data : ' || p_to_serial_number(i).serial_number);
14952 		 END IF;
14953 	   END;
14954 	 END LOOP;
14955 
14956 	 IF (l_transfer_all AND l_to_row_exist) THEN
14957 	    -- validate the from serial with the to record.
14958 	    -- if count + to reserved count exceeds primary qty - fail
14959 	    -- else update from to to
14960 
14961 	    IF (l_debug = 1) THEN
14962 	       debug_print('Inside param 3. transfer_all and to_row_exist');
14963 	    END IF;
14964 
14965 	    -- we need to validate only if there are more serials reserved
14966 	    -- and we need to transfer to the to_record.
14967 	    IF (l_total_serials_reserved > 0) THEN
14968 	       -- call validate serials for the to record.
14969 	       inv_reservation_validate_pvt.validate_serials
14970 		 (
14971 		  x_return_status              => l_return_status
14972 		  , p_rsv_action_name         => 'TRANSFER'
14973 		  , p_orig_rsv_rec             => l_dummy_rsv_rec
14974 		  , p_to_rsv_rec               => l_to_rsv_rec
14975 		  , p_orig_serial_array        => l_dummy_serial_array
14976 		  , p_to_serial_array          => l_serial_number_table
14977 		  );
14978 
14979 	       IF (l_debug = 1) THEN
14980 		  debug_print('After calling validate serials ' || l_return_status);
14981 	       END IF;
14982 
14983 	       --
14984 	       IF l_return_status = fnd_api.g_ret_sts_error THEN
14985 		  RAISE fnd_api.g_exc_error;
14986 	       END IF;
14987 
14988 	       --
14989 	       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14990 		  RAISE fnd_api.g_exc_unexpected_error;
14991 	       END IF;
14992 
14993 	    END IF;
14994 
14995 	    -- we will have to migrate the serials to the new reservation
14996 	    -- Just make sure that the group_mark_id and the
14997 	    -- reservation id are populated.
14998 	    FOR l_serial_index IN l_serial_number_table.first..l_serial_number_table.last
14999 	      LOOP
15000 	        BEGIN
15001 		   UPDATE mtl_serial_numbers SET reservation_id = l_to_reservation_id,
15002 		     group_mark_id = l_to_reservation_id WHERE
15003 		     serial_number = l_serial_number_table(l_serial_index).serial_number AND
15004 		     inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id AND
15005 		     current_organization_id = l_orig_rsv_tbl(1).organization_id;
15006 		EXCEPTION
15007 		   WHEN no_data_found THEN
15008 		      IF l_debug=1 THEN
15009 			 debug_print('No serials found for the serial number: ' || l_serial_number_table(l_serial_index).serial_number);
15010 		      END IF;
15011 		      fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
15012 		      fnd_msg_pub.ADD;
15013 		      RAISE fnd_api.g_exc_error;
15014 		END;
15015 
15016 		IF l_debug=1 THEN
15017 		   debug_print('Serial being migrated. serial number: ' || l_serial_number_table(l_serial_index).serial_number);
15018 		END IF;
15019 
15020 	      END LOOP;
15021 
15022 	      BEGIN
15023 		 SELECT COUNT(1) INTO l_total_to_serials_reserved FROM
15024 		   mtl_serial_numbers  WHERE reservation_id =
15025 		   l_to_reservation_id AND current_organization_id =
15026 		   l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15027 	      EXCEPTION
15028 		 WHEN no_data_found THEN
15029 		    IF l_debug=1 THEN
15030 		       debug_print('No serials found for reservation record. id: ' || l_to_reservation_id);
15031 		    END IF;
15032 	      END;
15033 
15034 	      IF (l_total_to_serials_reserved > l_to_primary_reservation_qty) THEN
15035 		 fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
15036 		 fnd_msg_pub.ADD;
15037 		 RAISE fnd_api.g_exc_error;
15038 	      END IF;
15039 
15040 	      -- update the to serial reservation quantity. dont have
15041 	      -- to update from as the from record is deleted.
15042 
15043 	      BEGIN
15044 		 UPDATE mtl_reservations SET serial_reservation_quantity
15045 		   = l_total_to_serials_reserved WHERE reservation_id =
15046 		   l_to_reservation_id;
15047 
15048 	      EXCEPTION
15049 		 WHEN no_data_found THEN
15050 		    IF l_debug=1 THEN
15051 		       debug_print('Update failed for from reservation record. id: ' || l_to_reservation_id);
15052 		    END IF;
15053 		    fnd_message.set_name('INV', 'INV_INVALID_ROW');
15054 		    fnd_msg_pub.ADD;
15055 		    RAISE fnd_api.g_exc_error;
15056 	      END;
15057 
15058 	      IF (l_debug = 1) THEN
15059 		 debug_print('After updating serial count of the to_record.');
15060 	      END IF;
15061 
15062 	      IF l_return_status = fnd_api.g_ret_sts_error THEN
15063 		 RAISE fnd_api.g_exc_error;
15064 	      END IF;
15065 
15066 	      IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
15067 		 RAISE fnd_api.g_exc_unexpected_error;
15068 	      END IF;
15069 
15070 	  ELSIF (l_transfer_all AND (NOT l_to_row_exist)) THEN
15071 		    -- validate the from serial with the to record.
15072 	      -- if count + to reserved count exceeds primary qty - fail
15073 		    -- else update from to to
15074 
15075 	      IF (l_debug = 1) THEN
15076 		 debug_print('Inside param 3. transfer_all and not to_row_exist');
15077 	      END IF;
15078 
15079 	      IF (l_total_serials_reserved > 0) THEN
15080 		 -- call validate serials for the to record.
15081 		 inv_reservation_validate_pvt.validate_serials
15082 		   (
15083 		    x_return_status              => l_return_status
15084 		    , p_rsv_action_name          => 'TRANSFER'
15085 		    , p_orig_rsv_rec             => l_dummy_rsv_rec
15086 		    , p_to_rsv_rec               => l_to_rsv_rec
15087 		    , p_orig_serial_array        => l_dummy_serial_array
15088 		    , p_to_serial_array          => l_serial_number_table
15089 		    );
15090 
15091 		 IF (l_debug = 1) THEN
15092 		    debug_print('After calling validate serials ' || l_return_status);
15093 		 END IF;
15094 
15095 		 --
15096 		 IF l_return_status = fnd_api.g_ret_sts_error THEN
15097 		    RAISE fnd_api.g_exc_error;
15098 		 END IF;
15099 
15100 		 --
15101 		 IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
15102 		    RAISE fnd_api.g_exc_unexpected_error;
15103 		 END IF;
15104 
15105 	      END IF;
15106 
15107 	      -- we have already reserved the serials. Since we are
15108 	      -- working on the same record, we will have to update
15109 	      -- the serial reservation quantity. check to see if the
15110 	      -- count exceeds
15111 	      BEGIN
15112 		 SELECT COUNT(1) INTO l_total_to_serials_reserved FROM
15113 		   mtl_serial_numbers  WHERE reservation_id =
15114 		   l_orig_rsv_tbl(1).reservation_id AND current_organization_id = l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15115 	      EXCEPTION
15116 		 WHEN no_data_found THEN
15117 		    IF l_debug=1 THEN
15118 		       debug_print('No serials found for reservation record. id: ' || l_to_reservation_id);
15119 		    END IF;
15120 	      END;
15121 
15122 	      IF (l_total_to_serials_reserved > l_to_primary_reservation_qty) THEN
15123 		 fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
15124 		 fnd_msg_pub.ADD;
15125 		 RAISE fnd_api.g_exc_error;
15126 	      END IF;
15127 
15128               BEGIN
15129 		 UPDATE mtl_reservations SET serial_reservation_quantity
15130 		   = l_total_to_serials_reserved WHERE reservation_id =
15131 		   l_orig_rsv_tbl(1).reservation_id;
15132 
15133 	      EXCEPTION
15134 		 WHEN no_data_found THEN
15135 		    IF l_debug=1 THEN
15136 		       debug_print('Update failed for from reservation record. id: ' || l_to_reservation_id);
15137 		    END IF;
15138 		    fnd_message.set_name('INV', 'INV_INVALID_ROW');
15139 		    fnd_msg_pub.ADD;
15140 		    RAISE fnd_api.g_exc_error;
15141 	      END;
15142 
15143 	      IF (l_debug = 1) THEN
15144 		 debug_print('After calling update serial reservation qty ' || l_return_status);
15145 	      END IF;
15146 
15147 	      IF l_return_status = fnd_api.g_ret_sts_error THEN
15148 		 RAISE fnd_api.g_exc_error;
15149 	      END IF;
15150 
15151 	      IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
15152 		 RAISE fnd_api.g_exc_unexpected_error;
15153 	      END IF;
15154 
15155 	  ELSIF (NOT l_transfer_all) THEN
15156 		      -- serials passed have already been reserved. make sure the
15157 		      -- count doesnt exceed.
15158 	      IF l_debug=1 THEN
15159 		 debug_print('Inside param 3. Not transfer all');
15160 		 debug_print('to reservation id: ' || l_to_reservation_id);
15161 		 debug_print('from reservation id: ' || l_orig_rsv_tbl(1).reservation_id);
15162 	      END IF;
15163 
15164 	      -- check the from serial count. if the from count exceeds the
15165 	      -- from reservation qty, transfer to the to record.
15166 	      IF (l_total_serials_reserved > 0) THEN
15167                BEGIN
15168 		  SELECT inventory_item_id, serial_number bulk collect INTO
15169 		    l_validate_serial_number_table FROM
15170 		    mtl_serial_numbers  WHERE reservation_id =
15171 		    l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
15172 		    l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15173 	       EXCEPTION
15174 		  WHEN no_data_found THEN
15175 		     IF l_debug=1 THEN
15176 			debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
15177 		     END IF;
15178 	       END;
15179 
15180 	       l_total_from_serials_reserved := l_validate_serial_number_table.COUNT;
15181 
15182 	       IF (l_total_from_serials_reserved > l_from_primary_reservation_qty) THEN
15183 		  -- we have to transfer some of the serials to the to_record
15184 		  -- unreserve the extra serials that are more than the primary_reservation_quantity
15185 		  IF (l_debug = 1) THEN
15186 		     debug_print('Total serials more than from reservation quantity.');
15187 		  END IF;
15188 
15189 		  -- need to validate serials as we are going to transfer
15190 		  -- them at randon
15191 		  -- call validate serials for the to record.
15192 		  inv_reservation_validate_pvt.validate_serials
15193 		    (
15194 		     x_return_status              => l_return_status
15195 		     , p_rsv_action_name          => 'TRANSFER'
15196 		     , p_orig_rsv_rec             => l_dummy_rsv_rec
15197 		     , p_to_rsv_rec               => l_to_rsv_rec
15198 		     , p_orig_serial_array        => l_dummy_serial_array
15199 		     , p_to_serial_array          => l_validate_serial_number_table
15200 		     );
15201 
15202 		  IF (l_debug = 1) THEN
15203 		     debug_print('After calling validate serials ' || l_return_status);
15204 		  END IF;
15205 
15206 		  --
15207 		  IF l_return_status = fnd_api.g_ret_sts_error THEN
15208 		     RAISE fnd_api.g_exc_error;
15209 		  END IF;
15210 
15211 		  --
15212 		  IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
15213 		     RAISE fnd_api.g_exc_unexpected_error;
15214 		  END IF;
15215 
15216 
15217 		  l_serials_tobe_unreserved := l_total_from_serials_reserved - l_from_primary_reservation_qty;
15218 
15219 		  FOR i IN 1..l_serials_tobe_unreserved
15220 		    LOOP
15221 	             BEGIN
15222 			UPDATE mtl_serial_numbers SET
15223 			  reservation_id = l_to_reservation_id,
15224 			  group_mark_id = l_to_reservation_id WHERE
15225 			  serial_number = l_validate_serial_number_table(i).serial_number AND
15226 			  inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id AND
15227 			  current_organization_id = l_orig_rsv_tbl(1).organization_id;
15228 		     EXCEPTION
15229 			WHEN no_data_found THEN
15230 			   IF l_debug=1 THEN
15231 			      debug_print('No serials found for Serial Number: ' || l_serial_number_table(i).serial_number);
15232 			   END IF;
15233 			   fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
15234 			   fnd_msg_pub.ADD;
15235 			   RAISE fnd_api.g_exc_error;
15236 		     END;
15237 
15238 		     IF l_debug=1 THEN
15239 			debug_print('Serial being unreserved. serial number: ' || l_serial_number_table(i).serial_number);
15240 		     END IF;
15241 
15242 		    END LOOP;
15243 	       END IF;
15244 	      END IF;
15245 
15246               BEGIN
15247 		 SELECT COUNT(1) INTO l_total_to_serials_reserved FROM
15248 		   mtl_serial_numbers  WHERE reservation_id =
15249 		   l_to_reservation_id AND current_organization_id =
15250 		   l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15251 	      EXCEPTION
15252 		 WHEN no_data_found THEN
15253 		    IF l_debug=1 THEN
15254 		       debug_print('No serials found for to reservation record. id: ' || l_to_reservation_id);
15255 		    END IF;
15256 	      END;
15257 
15258 	      IF (l_total_to_serials_reserved > l_to_primary_reservation_qty) THEN
15259 		 fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
15260 		 fnd_msg_pub.ADD;
15261 		 RAISE fnd_api.g_exc_error;
15262 	      END IF;
15263 
15264 	      BEGIN
15265 		 UPDATE mtl_reservations SET serial_reservation_quantity
15266 		   = l_total_to_serials_reserved WHERE reservation_id =
15267 		   l_to_reservation_id;
15268 
15269 	      EXCEPTION
15270 		 WHEN no_data_found THEN
15271 		    IF l_debug=1 THEN
15272 		       debug_print('Update failed for to reservation record. id: ' || l_to_reservation_id);
15273 		    END IF;
15274 		    fnd_message.set_name('INV', 'INV_INVALID_ROW');
15275 		    fnd_msg_pub.ADD;
15276 		    RAISE fnd_api.g_exc_error;
15277 	      END;
15278 
15279 	      BEGIN
15280 		 SELECT COUNT(1) INTO l_total_from_serials_reserved FROM
15281 		   mtl_serial_numbers  WHERE reservation_id =
15282 		   l_orig_rsv_tbl(1).reservation_id AND
15283 		   current_organization_id = l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15284 	      EXCEPTION
15285 		 WHEN no_data_found THEN
15286 		    IF l_debug=1 THEN
15287 		       debug_print('No serials found for to reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
15288 		    END IF;
15289 	      END;
15290 
15291               BEGIN
15292 		 UPDATE mtl_reservations SET serial_reservation_quantity
15293 		   = l_total_from_serials_reserved WHERE reservation_id =
15294 		   l_orig_rsv_tbl(1).reservation_id;
15295 
15296 	      EXCEPTION
15297 		 WHEN no_data_found THEN
15298 		    IF l_debug=1 THEN
15299 		       debug_print('Update failed for from reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
15300 		    END IF;
15301 		    fnd_message.set_name('INV', 'INV_INVALID_ROW');
15302 		    fnd_msg_pub.ADD;
15303 		    RAISE fnd_api.g_exc_error;
15304 	      END;
15305 
15306 
15307 	 END if; -- transfer all and to row exist
15308     END IF; -- param = 3
15309 
15310 
15311     IF (l_serial_param = 4) THEN
15312 
15313        -- We will have to unreserve the from serials and if the serial count
15314        -- exceeds the from primary qty, transfer them to the to record
15315 
15316        -- validate if the passed serials belong to the from reservation
15317        -- record.
15318        IF l_debug=1 THEN
15319 	  debug_print('Inside param 2');
15320 	  debug_print('Original tbl(1)Org id: ' || l_orig_rsv_tbl(1).organization_id);
15321 	  debug_print('Original tbl(1)Item id: ' || l_orig_rsv_tbl(1).inventory_item_id);
15322        END IF;
15323        -- The serial has to be reserved to the from record
15324        FOR i IN p_original_serial_number.first..p_original_serial_number.last
15325 	 LOOP
15326 	   BEGIN
15327 	      SELECT reservation_id INTO l_from_reservation_id FROM mtl_serial_numbers WHERE
15328 		serial_number = p_original_serial_number(i).serial_number AND
15329 		current_organization_id = l_orig_rsv_tbl(1).organization_id AND
15330 		inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15331 	   EXCEPTION
15332 	      WHEN no_data_found THEN
15333 		 IF l_debug=1 THEN
15334 		    debug_print('No serials found for this data : ' || p_original_serial_number(i).serial_number);
15335 		 END IF;
15336 	   END;
15337 
15338 	   -- if the serial is not reserved or if the serial belongs to a
15339 	   -- different reservation record, then fail
15340 	   IF (l_from_reservation_id IS NOT NULL AND l_from_reservation_id <>
15341 	       l_orig_rsv_tbl(1).reservation_id) OR (l_from_reservation_id IS NULL) THEN
15342 	      fnd_message.set_name('INV', 'INV_INVALID_FROM_SERIAL');
15343 	      fnd_msg_pub.ADD;
15344 	      RAISE fnd_api.g_exc_error;
15345 	   END IF;
15346 
15347 	   -- unreserve the passed serials.
15348 	     BEGIN
15349 		UPDATE mtl_serial_numbers SET reservation_id = NULL,
15350 		  group_mark_id = NULL, line_mark_id = NULL,
15351 		  lot_line_mark_id = NULL WHERE
15352 		  serial_number = p_original_serial_number(i).serial_number AND
15353 		  current_organization_id = l_orig_rsv_tbl(1).organization_id AND
15354 		  inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15355 	     EXCEPTION
15356 		WHEN no_data_found THEN
15357 		   IF l_debug=1 THEN
15358 		      debug_print('No serials found for this data : ' || p_original_serial_number(i).serial_number);
15359 		   END IF;
15360 	     END;
15361 	 END LOOP;
15362 
15363 	 -- to serials are passed .
15364 	 -- reserve the serials. if the serial belongs to the from record,
15365 	 -- transfer it to the to record.
15366 
15367 	 -- validate the serial numbers passed and reserve them
15368 
15369 	 FOR i IN p_to_serial_number.first..p_to_serial_number.last
15370 	   LOOP
15371 	     BEGIN
15372 		SELECT reservation_id, group_mark_id INTO
15373 		  l_from_reservation_id, l_group_mark_id FROM mtl_serial_numbers WHERE
15374 		  serial_number = p_to_serial_number(i).serial_number AND
15375 		  current_organization_id = l_orig_rsv_tbl(1).organization_id AND
15376 		  inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15377 	     EXCEPTION
15378 		WHEN no_data_found THEN
15379 		   IF l_debug=1 THEN
15380 		      debug_print('No serials found for this data : ' || p_to_serial_number(i).serial_number);
15381 		   END IF;
15382 		   fnd_message.set_name('INV', 'INV_INVALID_FROM_SERIAL');
15383 		   fnd_msg_pub.ADD;
15384 		   RAISE fnd_api.g_exc_error;
15385 	     END;
15386 
15387 	     IF (l_from_reservation_id IS NOT NULL AND l_from_reservation_id <>
15388 		 l_orig_rsv_tbl(1).reservation_id) THEN
15389 		fnd_message.set_name('INV', 'INV_INVALID_FROM_SERIAL');
15390 		fnd_msg_pub.ADD;
15391 		RAISE fnd_api.g_exc_error;
15392 	     END IF;
15393 
15394 	     IF (l_from_reservation_id IS NULL) AND (l_group_mark_id IS NOT NULL)  AND (l_group_mark_id <> -1)THEN
15395 		IF (l_debug = 1) THEN
15396 		   debug_print('Group Mark Id is not null for serial ' || p_to_serial_number(i).serial_number);
15397 		END IF;
15398 		fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
15399 		fnd_msg_pub.ADD;
15400 		RAISE fnd_api.g_exc_error;
15401 	     END IF;
15402 
15403 	     BEGIN
15404 		UPDATE mtl_serial_numbers SET reservation_id = l_to_reservation_id,
15405 		  group_mark_id = l_to_reservation_id WHERE
15406 		  serial_number = p_to_serial_number(i).serial_number AND
15407 		  current_organization_id = l_orig_rsv_tbl(1).organization_id AND
15408 		  inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15409 	     EXCEPTION
15410 		WHEN no_data_found THEN
15411 		   IF l_debug=1 THEN
15412 		      debug_print('No serials found for this data : ' || p_to_serial_number(i).serial_number);
15413 		   END IF;
15414 	     END;
15415 	   END LOOP;
15416 
15417 	   IF (l_transfer_all AND l_to_row_exist) THEN
15418 	      -- if more serials are reserved,
15419 	      -- transfer them to the to record
15420 	      -- validate the serials before transferring them to to
15421 	      -- record.
15422 	      IF (l_debug = 1) THEN
15423 		 debug_print('Inside param 4. transfer_all and to_row_exist');
15424 	      END IF;
15425 
15426 	      IF (l_total_serials_reserved > 0) THEN
15427 	       BEGIN
15428 		  SELECT inventory_item_id, serial_number bulk collect INTO
15429 		    l_validate_serial_number_table FROM
15430 		    mtl_serial_numbers  WHERE reservation_id =
15431 		    l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
15432 		    l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15433 	       EXCEPTION
15434 		  WHEN no_data_found THEN
15435 		     IF l_debug=1 THEN
15436 			debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
15437 		     END IF;
15438 	       END;
15439 
15440 	       l_validate_serials_reserved := l_validate_serial_number_table.COUNT;
15441 	       IF l_debug=1 THEN
15442 		  debug_print('Total reserved serials: ' || l_validate_serials_reserved);
15443 	       END IF;
15444 
15445 	       IF (l_validate_serials_reserved > 0) THEN
15446 		  inv_reservation_validate_pvt.validate_serials
15447 		    (
15448 		     x_return_status              => l_return_status
15449 		     , p_rsv_action_name         => 'TRANSFER'
15450 		     , p_orig_rsv_rec             => l_dummy_rsv_rec
15451 		     , p_to_rsv_rec               => l_to_rsv_rec
15452 		     , p_orig_serial_array        => l_dummy_serial_array
15453 		     , p_to_serial_array          => l_validate_serial_number_table
15454 		     );
15455 
15456 		  IF (l_debug = 1) THEN
15457 		     debug_print('After calling validate serials ' || l_return_status);
15458 		  END IF;
15459 
15460 		  --
15461 		  IF l_return_status = fnd_api.g_ret_sts_error THEN
15462 		     RAISE fnd_api.g_exc_error;
15463 		  END IF;
15464 
15465 		  --
15466 		  IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
15467 		     RAISE fnd_api.g_exc_unexpected_error;
15468 		  END IF;
15469 
15470 		  BEGIN
15471 		     UPDATE mtl_serial_numbers SET reservation_id = l_to_reservation_id,
15472 		       group_mark_id = l_to_reservation_id WHERE
15473 		       reservation_id = l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
15474 		       l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15475 
15476 		  EXCEPTION
15477 		     WHEN no_data_found THEN
15478 			IF l_debug=1 THEN
15479 			   debug_print('No serials found for this data. rsv id: ' || l_orig_rsv_tbl(1).reservation_id);
15480 			END IF;
15481 			fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
15482 			fnd_msg_pub.ADD;
15483 			RAISE fnd_api.g_exc_error;
15484 		  END;
15485 	       END IF;
15486 	      END IF;
15487 
15488 	      BEGIN
15489 		 SELECT COUNT(1) INTO l_total_to_serials_reserved FROM
15490 		   mtl_serial_numbers  WHERE reservation_id =
15491 		   l_to_reservation_id AND current_organization_id =
15492 		   l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15493 	      EXCEPTION
15494 		 WHEN no_data_found THEN
15495 		    IF l_debug=1 THEN
15496 		       debug_print('No serials found for reservation record. id: ' || l_to_reservation_id);
15497 		    END IF;
15498 	      END;
15499 
15500 	      IF (l_total_to_serials_reserved > l_to_primary_reservation_qty) THEN
15501 		 fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
15502 		 fnd_msg_pub.ADD;
15503 		 RAISE fnd_api.g_exc_error;
15504 	      END IF;
15505 
15506 	      BEGIN
15507 		 UPDATE mtl_reservations SET serial_reservation_quantity
15508 		   = l_total_to_serials_reserved WHERE reservation_id =
15509 		   l_orig_rsv_tbl(1).reservation_id;
15510 
15511 	      EXCEPTION
15512 		 WHEN no_data_found THEN
15513 		    IF l_debug=1 THEN
15514 		       debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
15515 		    END IF;
15516 		    fnd_message.set_name('INV', 'INV_INVALID_ROW');
15517 		    fnd_msg_pub.ADD;
15518 		    RAISE fnd_api.g_exc_error;
15519 	      END;
15520 
15521 	    ELSIF (l_transfer_all AND (NOT l_to_row_exist)) THEN
15522 
15523 		    -- validate the from serial with the to record.
15524 		    -- if count + to reserved count exceeds primary qty - fail
15525 		    -- else update from to to
15526 
15527 	      IF (l_debug = 1) THEN
15528 		 debug_print('Inside param 4. transfer_all and not to_row_exist');
15529 	      END IF;
15530 
15531 	      IF (l_total_serials_reserved > 0) THEN
15532 	        BEGIN
15533 		   SELECT inventory_item_id, serial_number bulk collect INTO
15534 		     l_validate_serial_number_table FROM
15535 		     mtl_serial_numbers  WHERE reservation_id =
15536 		     l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
15537 		     l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15538 		EXCEPTION
15539 		   WHEN no_data_found THEN
15540 		      IF l_debug=1 THEN
15541 			 debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
15542 		      END IF;
15543 		END;
15544 
15545 		l_validate_serials_reserved := l_validate_serial_number_table.COUNT;
15546 		IF l_debug=1 THEN
15547 		   debug_print('Total reserved serials: ' || l_validate_serials_reserved);
15548 		END IF;
15549 
15550 		IF (l_validate_serials_reserved > 0) THEN
15551 		   inv_reservation_validate_pvt.validate_serials
15552 		     (
15553 		      x_return_status              => l_return_status
15554 		      , p_rsv_action_name         => 'TRANSFER'
15555 		      , p_orig_rsv_rec             => l_dummy_rsv_rec
15556 		      , p_to_rsv_rec               => l_to_rsv_rec
15557 		      , p_orig_serial_array        => l_dummy_serial_array
15558 		      , p_to_serial_array          => l_validate_serial_number_table
15559 		      );
15560 
15561 		   IF (l_debug = 1) THEN
15562 		      debug_print('After calling validate serials ' || l_return_status);
15563 		   END IF;
15564 
15565 		   --
15566 		   IF l_return_status = fnd_api.g_ret_sts_error THEN
15567 		      RAISE fnd_api.g_exc_error;
15568 		   END IF;
15569 
15570 		   --
15571 		   IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
15572 		      RAISE fnd_api.g_exc_unexpected_error;
15573 		   END IF;
15574 		END IF;
15575 	      END IF;
15576 
15577               BEGIN
15578 		 SELECT COUNT(1) INTO l_total_to_serials_reserved FROM
15579 		   mtl_serial_numbers  WHERE reservation_id =
15580 		   l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
15581 		   l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15582 	      EXCEPTION
15583 		 WHEN no_data_found THEN
15584 		    IF l_debug=1 THEN
15585 		       debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
15586 		    END IF;
15587 	      END;
15588 	      IF (l_total_to_serials_reserved > l_to_primary_reservation_qty) THEN
15589 		 fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
15590 		 fnd_msg_pub.ADD;
15591 		 RAISE fnd_api.g_exc_error;
15592 	      END IF;
15593 
15594               BEGIN
15595 		 UPDATE mtl_reservations SET serial_reservation_quantity
15596 		   = l_total_to_serials_reserved WHERE reservation_id =
15597 		   l_orig_rsv_tbl(1).reservation_id;
15598 
15599 	      EXCEPTION
15600 		 WHEN no_data_found THEN
15601 		    IF l_debug=1 THEN
15602 		       debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
15603 		    END IF;
15604 		    fnd_message.set_name('INV', 'INV_INVALID_ROW');
15605 		    fnd_msg_pub.ADD;
15606 		    RAISE fnd_api.g_exc_error;
15607 	      END;
15608 
15609 	    ELSIF (NOT l_transfer_all) THEN
15610 		    -- check to see if there are excess serials in from
15611 		    -- record. if so, validate and move them to to record
15612 		    -- then count and update the serial count
15613 
15614 	      IF (l_debug = 1) THEN
15615 		 debug_print('Inside param 4. not transfer_all');
15616 	      END IF;
15617 
15618 	      IF (l_total_serials_reserved > 0) THEN
15619 	        BEGIN
15620 		   SELECT inventory_item_id, serial_number bulk collect INTO
15621 		     l_validate_serial_number_table FROM
15622 		     mtl_serial_numbers  WHERE reservation_id =
15623 		     l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
15624 		     l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15625 		EXCEPTION
15626 		   WHEN no_data_found THEN
15627 		      IF l_debug=1 THEN
15628 			 debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
15629 		      END IF;
15630 		END;
15631 
15632 		l_validate_serials_reserved := l_validate_serial_number_table.COUNT;
15633 		IF l_debug=1 THEN
15634 		   debug_print('Total reserved serials: ' || l_validate_serials_reserved);
15635 		END IF;
15636 
15637 		IF (l_validate_serials_reserved > l_from_primary_reservation_qty) THEN
15638 		   -- we have to unreserve some of the serials
15639 		   -- unreserve the extra serials that are more than the primary_reservation_quantity
15640 		   IF (l_debug = 1) THEN
15641 		      debug_print('Total serials more than reservation quantity.');
15642 		   END IF;
15643 
15644 		   IF (l_validate_serials_reserved > 0) THEN
15645 		      inv_reservation_validate_pvt.validate_serials
15646 			(
15647 			 x_return_status              => l_return_status
15648 			 , p_rsv_action_name         => 'TRANSFER'
15649 			 , p_orig_rsv_rec             => l_dummy_rsv_rec
15650 			 , p_to_rsv_rec               => l_to_rsv_rec
15651 			 , p_orig_serial_array        => l_dummy_serial_array
15652 			 , p_to_serial_array          => l_validate_serial_number_table
15653 			 );
15654 
15655 		      IF (l_debug = 1) THEN
15656 			 debug_print('After calling validate serials ' || l_return_status);
15657 		      END IF;
15658 
15659 		      --
15660 		      IF l_return_status = fnd_api.g_ret_sts_error THEN
15661 			 RAISE fnd_api.g_exc_error;
15662 		      END IF;
15663 
15664 		      --
15665 		      IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
15666 			 RAISE fnd_api.g_exc_unexpected_error;
15667 		      END IF;
15668 
15669 		      l_serials_tobe_unreserved := l_validate_serials_reserved -  l_from_primary_reservation_qty;
15670 
15671 		      FOR i IN 1..l_serials_tobe_unreserved
15672 			LOOP
15673 	                  BEGIN
15674 			     UPDATE mtl_serial_numbers SET reservation_id = l_to_reservation_id,
15675 			       group_mark_id = l_to_reservation_id WHERE
15676 			       serial_number = l_validate_serial_number_table(i).serial_number AND
15677 			       inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id AND
15678 			       current_organization_id = l_orig_rsv_tbl(1).organization_id;
15679 			  EXCEPTION
15680 			     WHEN no_data_found THEN
15681 				IF l_debug=1 THEN
15682 				   debug_print('No serials found for serial number: ' || l_validate_serial_number_table(i).serial_number);
15683 				END IF;
15684 				fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
15685 				fnd_msg_pub.ADD;
15686 				RAISE fnd_api.g_exc_error;
15687 			  END;
15688 
15689 			  IF l_debug=1 THEN
15690 			     debug_print('Serial being unreserved. serial number: ' || l_serial_number_table(i).serial_number);
15691 			  END IF;
15692 
15693 			END LOOP;
15694 		   END IF;
15695 		END IF;
15696 	      END IF;
15697 
15698 	      BEGIN
15699 		 SELECT COUNT(1) INTO l_total_to_serials_reserved FROM
15700 		   mtl_serial_numbers  WHERE reservation_id =
15701 		   l_to_reservation_id AND current_organization_id =
15702 		   l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15703 	      EXCEPTION
15704 		 WHEN no_data_found THEN
15705 		    IF l_debug=1 THEN
15706 		       debug_print('No serials found for reservation record. id: ' || l_to_reservation_id);
15707 		    END IF;
15708 	      END;
15709 	      IF (l_total_to_serials_reserved > l_to_primary_reservation_qty) THEN
15710 		 fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
15711 		 fnd_msg_pub.ADD;
15712 		 RAISE fnd_api.g_exc_error;
15713 	      END IF;
15714 
15715 	      BEGIN
15716 		 UPDATE mtl_reservations SET serial_reservation_quantity
15717 		   = l_total_to_serials_reserved WHERE reservation_id =
15718 		   l_to_reservation_id;
15719 
15720 	      EXCEPTION
15721 		 WHEN no_data_found THEN
15722 		    IF l_debug=1 THEN
15723 		       debug_print('No serials found for reservation record. id: ' || l_to_reservation_id);
15724 		    END IF;
15725 		    fnd_message.set_name('INV', 'INV_INVALID_ROW');
15726 		    fnd_msg_pub.ADD;
15727 		    RAISE fnd_api.g_exc_error;
15728 	      END;
15729 
15730               BEGIN
15731 		 SELECT COUNT(1) INTO l_total_from_serials_reserved FROM
15732 		   mtl_serial_numbers  WHERE reservation_id =
15733 		   l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
15734 		   l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15735 	      EXCEPTION
15736 		 WHEN no_data_found THEN
15737 		    IF l_debug=1 THEN
15738 		       debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
15739 		    END IF;
15740 	      END;
15741 
15742               BEGIN
15743 		 UPDATE mtl_reservations SET serial_reservation_quantity
15744 		   = l_total_from_serials_reserved WHERE reservation_id =
15745 		   l_orig_rsv_tbl(1).reservation_id;
15746 
15747 	      EXCEPTION
15748 		 WHEN no_data_found THEN
15749 		    IF l_debug=1 THEN
15750 		       debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
15751 		    END IF;
15752 		    fnd_message.set_name('INV', 'INV_INVALID_ROW');
15753 		    fnd_msg_pub.ADD;
15754 		    RAISE fnd_api.g_exc_error;
15755 	      END;
15756 
15757 	   END IF;-- transfer all and to row exist
15758 
15759     END IF;-- param = 4
15760 
15761     /*** End R12 ***/
15762 
15763     /**** {{ R12 Enhanced reservations code changes. Should be
15764     -- releasing the locks. }} *****/
15765     IF l_lock_obtained THEN
15766        inv_reservation_lock_pvt.release_lock
15767 	 (l_supply_lock_handle);
15768        inv_reservation_lock_pvt.release_lock
15769 	 (l_demand_lock_handle);
15770     END IF;
15771     /*** End R12 ***/
15772 
15773     x_return_status  := l_return_status;
15774   EXCEPTION
15775      WHEN fnd_api.g_exc_error THEN
15776 	ROLLBACK TO transfer_reservation_sa;
15777 	x_return_status  := fnd_api.g_ret_sts_error;
15778 	/**** {{ R12 Enhanced reservations code changes. Should be
15779 	-- releasing the locks. }} *****/
15780 	IF l_lock_obtained THEN
15781 	   inv_reservation_lock_pvt.release_lock
15782 	     (l_supply_lock_handle);
15783 	   inv_reservation_lock_pvt.release_lock
15784 	     (l_demand_lock_handle);
15785 	END IF;
15786 	/*** End R12 ***/
15787 	--  Get message count and data
15788 	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
15789      WHEN fnd_api.g_exc_unexpected_error THEN
15790 	ROLLBACK TO transfer_reservation_sa;
15791 	x_return_status  := fnd_api.g_ret_sts_unexp_error;
15792 	/**** {{ R12 Enhanced reservations code changes. Should be
15793 	-- releasing the locks. }} *****/
15794 	IF l_lock_obtained THEN
15795 	   inv_reservation_lock_pvt.release_lock
15796 	     (l_supply_lock_handle);
15797 	   inv_reservation_lock_pvt.release_lock
15798 	     (l_demand_lock_handle);
15799 	END IF;
15800 	/*** End R12 ***/
15801 	--  Get message count and data
15802 	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
15803      WHEN OTHERS THEN
15804 	ROLLBACK TO transfer_reservation_sa;
15805 	x_return_status  := fnd_api.g_ret_sts_unexp_error;
15806 	/**** {{ R12 Enhanced reservations code changes. Should be
15807 	-- releasing the locks. }} *****/
15808 	IF l_lock_obtained THEN
15809 	   inv_reservation_lock_pvt.release_lock
15810 	     (l_supply_lock_handle);
15811 	   inv_reservation_lock_pvt.release_lock
15812 	     (l_demand_lock_handle);
15813 	END IF;
15814 	/*** End R12 ***/
15815 	IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
15816 	   fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
15817 	END IF;
15818 
15819 	--  Get message count and data
15820 	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
15821   END transfer_reservation;
15822 
15823   --
15824   -- Procedure
15825   /*
15826   ** ----------------------------------------------------------------------
15827   ** For Order Management(OM) use only. Please read below:
15828   ** MUST PASS DEMAND SOURCE HEADER ID AND DEMAND SOURCE LINE ID
15829   ** ----------------------------------------------------------------------
15830   ** This API has been written exclusively for Order Management, who query
15831   ** reservations extensively.
15832   ** The generic query reservation API, query_reservation(see signature above)
15833   ** builds a dynamic SQL to satisfy all callers as it does not know what the
15834   ** search criteria is, at design time.
15835   ** The dynamic SQL consumes soft parse time, which reduces performance.
15836   ** An excessive use of query_reservation contributes to performance
15837   ** degradation because of soft parse times.
15838   ** Since we know what OM would always use to query reservations
15839   ** - demand source header id and demand source line id, a new API
15840   ** with static SQL would be be effective, with reduced performance impact.
15841   ** ----------------------------------------------------------------------
15842   ** Since OM has been using query_reservation before this, the signature of the
15843   ** new API below remains the same to cause minimal impact.
15844   ** ----------------------------------------------------------------------
15845   **Bug 2872822 Added new constants for p_query_by_req_date and handled two
15846   ** new cursors.Did not use Dynamic SQL for parsing performance issue.
15847   */
15848   PROCEDURE query_reservation_om_hdr_line(
15849     p_api_version_number        IN     NUMBER
15850   , p_init_msg_lst              IN     VARCHAR2 DEFAULT fnd_api.g_false
15851   , x_return_status             OUT    NOCOPY VARCHAR2
15852   , x_msg_count                 OUT    NOCOPY NUMBER
15853   , x_msg_data                  OUT    NOCOPY VARCHAR2
15854   , p_query_input               IN     inv_reservation_global.mtl_reservation_rec_type
15855   , p_lock_records              IN     VARCHAR2 DEFAULT fnd_api.g_false
15856   , p_sort_by_req_date          IN     NUMBER DEFAULT inv_reservation_global.g_query_no_sort
15857   , p_cancel_order_mode         IN     NUMBER DEFAULT inv_reservation_global.g_cancel_order_no
15858   , x_mtl_reservation_tbl       OUT    NOCOPY inv_reservation_global.mtl_reservation_tbl_type
15859   , x_mtl_reservation_tbl_count OUT    NOCOPY NUMBER
15860   , x_error_code                OUT    NOCOPY NUMBER
15861   ) IS
15862     l_api_version_number CONSTANT NUMBER                                          := 1.0;
15863     l_api_name           CONSTANT VARCHAR2(30)                                    := 'Query_Reservation_OM_Hdr_Line';
15864     l_return_status               VARCHAR2(1)                                     := fnd_api.g_ret_sts_success;
15865     l_counter                     INTEGER;
15866     l_rsv_rec                     inv_reservation_global.mtl_reservation_rec_type;
15867 
15868     --   Cursor to fetch MTL_RESERVATION record based on passed
15869     --   demand_source_header_id and demand_source_line_id
15870 
15871     -- INVCONV - Incorporate secondary columns
15872     CURSOR mrc(dmd_source_header_id NUMBER, dmd_source_line_id NUMBER) IS
15873        SELECT
15874 	 mr.reservation_id
15875 	 , mr.requirement_date
15876 	 , mr.organization_id
15877 	 , mr.inventory_item_id
15878 	 , mr.demand_source_type_id
15879 	 , mr.demand_source_name
15880 	 , mr.demand_source_header_id
15881 	 , mr.demand_source_line_id
15882 	 , mr.demand_source_delivery
15883 	 , mr.primary_uom_code
15884 	 , mr.primary_uom_id
15885 	 , mr.secondary_uom_code
15886 	 , mr.secondary_uom_id
15887 	 , mr.reservation_uom_code
15888 	 , mr.reservation_uom_id
15889 	 , mr.reservation_quantity
15890 	 , mr.primary_reservation_quantity
15891 	 , mr.secondary_reservation_quantity
15892 	 , mr.detailed_quantity
15893 	 , mr.secondary_detailed_quantity
15894 	 , mr.autodetail_group_id
15895 	 , mr.external_source_code
15896 	 , mr.external_source_line_id
15897 	 , mr.supply_source_type_id
15898 	 , mr.supply_source_header_id
15899 	 , mr.supply_source_line_id
15900 	 , mr.supply_source_name
15901 	 , mr.supply_source_line_detail
15902 	 , mr.revision
15903 	 , mr.subinventory_code
15904 	 , mr.subinventory_id
15905 	 , mr.locator_id
15906 	 , mr.lot_number
15907 	 , mr.lot_number_id
15908 	 , mr.pick_slip_number
15909 	 , mr.lpn_id
15910 	 , mr.attribute_category
15911 	 , mr.attribute1
15912 	 , mr.attribute2
15913 	 , mr.attribute3
15914 	 , mr.attribute4
15915 	 , mr.attribute5
15916 	 , mr.attribute6
15917 	 , mr.attribute7
15918 	 , mr.attribute8
15919 	 , mr.attribute9
15920 	 , mr.attribute10
15921 	 , mr.attribute11
15922 	 , mr.attribute12
15923 	 , mr.attribute13
15924 	 , mr.attribute14
15925 	 , mr.attribute15
15926 	 , mr.ship_ready_flag
15927 	 , mr.staged_flag
15928 	 /**** {{ R12 Enhanced reservations code changes }}****/
15929 	 , crossdock_flag
15930 	 , crossdock_criteria_id
15931 	 , demand_source_line_detail
15932 	 , serial_reservation_quantity
15933 	 , supply_receipt_date
15934 	 , demand_ship_date
15935 	 , project_id
15936 	 , task_id
15937 	 , orig_supply_source_type_id
15938 	 , orig_supply_source_header_id
15939 	 , orig_supply_source_line_id
15940 	 , orig_supply_source_line_detail
15941 	 , orig_demand_source_type_id
15942 	 , orig_demand_source_header_id
15943 	 , orig_demand_source_line_id
15944 	 , orig_demand_source_line_detail
15945 	 , serial_number
15946 	 /***** End R12 ***/
15947 
15948           FROM mtl_reservations mr
15949          WHERE demand_source_type_id IN (inv_reservation_global.g_source_type_oe
15950                                        , inv_reservation_global.g_source_type_internal_ord
15951                                        , inv_reservation_global.g_source_type_rma
15952                                         )
15953            AND demand_source_header_id = dmd_source_header_id
15954            AND demand_source_line_id = dmd_source_line_id
15955       ORDER BY NVL(mr.revision, ' ')
15956              , NVL(mr.lot_number, ' ')
15957              , NVL(mr.subinventory_code, ' ')
15958              , NVL(mr.locator_id, 0);
15959 
15960     --   Bug#2872822 Cursor to fetch MTL_RESERVATION record based on passed
15961     --   demand_source_header_id and demand_source_line_id
15962     --   the records are obtained in the ascending order of requirement_date
15963     -- INVCONV - Incorporate secondary columns
15964     CURSOR mrc_asc(dmd_source_header_id NUMBER, dmd_source_line_id NUMBER) IS
15965        SELECT
15966 	 mr.reservation_id
15967 	 , mr.requirement_date
15968 	 , mr.organization_id
15969 	 , mr.inventory_item_id
15970 	 , mr.demand_source_type_id
15971 	 , mr.demand_source_name
15972 	 , mr.demand_source_header_id
15973 	 , mr.demand_source_line_id
15974 	 , mr.demand_source_delivery
15975 	 , mr.primary_uom_code
15976 	 , mr.primary_uom_id
15977 	 , mr.reservation_uom_code
15978 	 , mr.reservation_uom_id
15979 	 , mr.secondary_uom_code
15980 	 , mr.secondary_uom_id
15981 	 , mr.reservation_quantity
15982 	 , mr.primary_reservation_quantity
15983 	 , mr.secondary_reservation_quantity
15984 	 , mr.detailed_quantity
15985 	 , mr.secondary_detailed_quantity
15986 	 , mr.autodetail_group_id
15987 	 , mr.external_source_code
15988 	 , mr.external_source_line_id
15989 	 , mr.supply_source_type_id
15990 	 , mr.supply_source_header_id
15991 	 , mr.supply_source_line_id
15992 	 , mr.supply_source_name
15993 	 , mr.supply_source_line_detail
15994 	 , mr.revision
15995 	 , mr.subinventory_code
15996 	 , mr.subinventory_id
15997 	 , mr.locator_id
15998 	 , mr.lot_number
15999 	 , mr.lot_number_id
16000 	 , mr.pick_slip_number
16001 	 , mr.lpn_id
16002 	 , mr.attribute_category
16003 	 , mr.attribute1
16004 	 , mr.attribute2
16005 	 , mr.attribute3
16006 	 , mr.attribute4
16007 	 , mr.attribute5
16008 	 , mr.attribute6
16009 	 , mr.attribute7
16010 	 , mr.attribute8
16011 	 , mr.attribute9
16012 	 , mr.attribute10
16013 	 , mr.attribute11
16014 	 , mr.attribute12
16015 	 , mr.attribute13
16016 	 , mr.attribute14
16017 	 , mr.attribute15
16018 	 , mr.ship_ready_flag
16019 	 , mr.staged_flag
16020 /**** {{ R12 Enhanced reservations code changes }}****/
16021 	 , crossdock_flag
16022 	 , crossdock_criteria_id
16023 	 , demand_source_line_detail
16024 	 , serial_reservation_quantity
16025 	 , supply_receipt_date
16026 	 , demand_ship_date
16027 	 , project_id
16028 	 , task_id
16029 	 , orig_supply_source_type_id
16030 	 , orig_supply_source_header_id
16031 	 , orig_supply_source_line_id
16032 	 , orig_supply_source_line_detail
16033 	 , orig_demand_source_type_id
16034 	 , orig_demand_source_header_id
16035 	 , orig_demand_source_line_id
16036 	 , orig_demand_source_line_detail
16037 	 , serial_number
16038 	 /***** End R12 ***/
16039 
16040           FROM mtl_reservations mr
16041          WHERE demand_source_type_id IN (inv_reservation_global.g_source_type_oe
16042                                        , inv_reservation_global.g_source_type_internal_ord
16043                                        , inv_reservation_global.g_source_type_rma
16044                                         )
16045            AND demand_source_header_id = dmd_source_header_id
16046            AND demand_source_line_id = dmd_source_line_id
16047       ORDER BY mr.requirement_date asc
16048              , NVL(mr.revision, ' ')
16049              , NVL(mr.lot_number, ' ')
16050              , NVL(mr.subinventory_code, ' ')
16051              , NVL(mr.locator_id, 0);
16052 
16053     --   Bug#2872822 Cursor to fetch MTL_RESERVATION record based on passed
16054     --   demand_source_header_id and demand_source_line_id
16055     --   the records are obtained in the descending order of requirement_date
16056     -- INVCONV - Incorporate secondary columns
16057     CURSOR mrc_desc(dmd_source_header_id NUMBER, dmd_source_line_id NUMBER) IS
16058        SELECT
16059 	 mr.reservation_id
16060 	 , mr.requirement_date
16061 	 , mr.organization_id
16062 	 , mr.inventory_item_id
16063 	 , mr.demand_source_type_id
16064 	 , mr.demand_source_name
16065 	 , mr.demand_source_header_id
16066 	 , mr.demand_source_line_id
16067 	 , mr.demand_source_delivery
16068 	 , mr.primary_uom_code
16069 	 , mr.primary_uom_id
16070 	 , mr.secondary_uom_code
16071 	 , mr.secondary_uom_id
16072 	 , mr.reservation_uom_code
16073 	 , mr.reservation_uom_id
16074 	 , mr.reservation_quantity
16075 	 , mr.primary_reservation_quantity
16076 	 , mr.secondary_reservation_quantity
16077 	 , mr.detailed_quantity
16078 	 , mr.secondary_detailed_quantity
16079 	 , mr.autodetail_group_id
16080 	 , mr.external_source_code
16081 	 , mr.external_source_line_id
16082 	 , mr.supply_source_type_id
16083 	 , mr.supply_source_header_id
16084 	 , mr.supply_source_line_id
16085 	 , mr.supply_source_name
16086 	 , mr.supply_source_line_detail
16087 	 , mr.revision
16088 	 , mr.subinventory_code
16089 	 , mr.subinventory_id
16090 	 , mr.locator_id
16091 	 , mr.lot_number
16092 	 , mr.lot_number_id
16093 	 , mr.pick_slip_number
16094 	 , mr.lpn_id
16095 	 , mr.attribute_category
16096 	 , mr.attribute1
16097 	 , mr.attribute2
16098 	 , mr.attribute3
16099 	 , mr.attribute4
16100 	 , mr.attribute5
16101 	 , mr.attribute6
16102 	 , mr.attribute7
16103 	 , mr.attribute8
16104 	 , mr.attribute9
16105 	 , mr.attribute10
16106 	 , mr.attribute11
16107 	 , mr.attribute12
16108 	 , mr.attribute13
16109 	 , mr.attribute14
16110 	 , mr.attribute15
16111 	 , mr.ship_ready_flag
16112 	 , mr.staged_flag
16113 	 /**** {{ R12 Enhanced reservations code changes }}****/
16114 	 , crossdock_flag
16115 	 , crossdock_criteria_id
16116 	 , demand_source_line_detail
16117 	 , serial_reservation_quantity
16118 	 , supply_receipt_date
16119 	 , demand_ship_date
16120 	 , project_id
16121 	 , task_id
16122 	 , orig_supply_source_type_id
16123 	 , orig_supply_source_header_id
16124 	 , orig_supply_source_line_id
16125 	 , orig_supply_source_line_detail
16126 	 , orig_demand_source_type_id
16127 	 , orig_demand_source_header_id
16128 	 , orig_demand_source_line_id
16129 	 , orig_demand_source_line_detail
16130 	 , serial_number
16131 	 /***** End R12 ***/
16132 
16133           FROM mtl_reservations mr
16134          WHERE demand_source_type_id IN (inv_reservation_global.g_source_type_oe
16135                                        , inv_reservation_global.g_source_type_internal_ord
16136                                        , inv_reservation_global.g_source_type_rma
16137                                         )
16138            AND demand_source_header_id = dmd_source_header_id
16139            AND demand_source_line_id = dmd_source_line_id
16140       ORDER BY mr.requirement_date DESC
16141              , NVL(mr.revision, ' ')
16142              , NVL(mr.lot_number, ' ')
16143              , NVL(mr.subinventory_code, ' ')
16144              , NVL(mr.locator_id, 0);
16145   BEGIN
16146     x_error_code                 := inv_reservation_global.g_err_unexpected;
16147 
16148     --
16149     --  Standard call to check for call compatibility
16150     IF NOT fnd_api.compatible_api_call(l_api_version_number, p_api_version_number, l_api_name, g_pkg_name) THEN
16151       RAISE fnd_api.g_exc_unexpected_error;
16152     END IF;
16153 
16154     --
16155     --  Initialize message list.
16156     IF fnd_api.to_boolean(p_init_msg_lst) THEN
16157       fnd_msg_pub.initialize;
16158     END IF;
16159 
16160     --
16161     /*
16162     ** Make sure the required parameters are passed
16163     */
16164     IF (p_query_input.demand_source_header_id = fnd_api.g_miss_num
16165         OR p_query_input.demand_source_header_id IS NULL
16166        ) THEN
16167       fnd_message.set_name('INV', 'INV-RSV-INPUT-MISSING');
16168       fnd_message.set_token('FIELD_NAME', 'demand_source_header_id');
16169       fnd_msg_pub.ADD;
16170       RAISE fnd_api.g_exc_error;
16171     END IF;
16172 
16173     IF (p_query_input.demand_source_line_id = fnd_api.g_miss_num
16174         OR p_query_input.demand_source_line_id IS NULL
16175        ) THEN
16176       fnd_message.set_name('INV', 'INV-RSV-INPUT-MISSING');
16177       fnd_message.set_token('FIELD_NAME', 'demand_source_line_id');
16178       fnd_msg_pub.ADD;
16179       RAISE fnd_api.g_exc_error;
16180     END IF;
16181 
16182     --Bug#2872822/2914726 OM call to this API passes null. This was changed
16183     -- handle null also.
16184     IF (p_sort_by_req_date NOT IN ( inv_reservation_global.g_query_req_date_inv_asc,
16185 				   inv_reservation_global.g_query_req_date_inv_desc)) OR p_sort_by_req_date IS NULL
16186       then
16187        OPEN mrc(p_query_input.demand_source_header_id, p_query_input.demand_source_line_id);
16188        l_counter                    := 0;
16189 
16190        LOOP
16191          FETCH mrc INTO l_rsv_rec;
16192          EXIT WHEN mrc%NOTFOUND;
16193          l_counter                         := l_counter + 1;
16194          x_mtl_reservation_tbl(l_counter)  := l_rsv_rec;
16195        END LOOP;
16196 
16197        CLOSE mrc;
16198     ELSIF p_sort_by_req_date =inv_reservation_global.g_query_req_date_inv_asc then
16199            OPEN mrc_asc(p_query_input.demand_source_header_id,
16200                                     p_query_input.demand_source_line_id);
16201          l_counter                    := 0;
16202         LOOP
16203         FETCH mrc_asc INTO l_rsv_rec;
16204         EXIT WHEN mrc_asc%NOTFOUND;
16205         l_counter                         := l_counter + 1;
16206         x_mtl_reservation_tbl(l_counter)  := l_rsv_rec;
16207         END LOOP;
16208         CLOSE mrc_asc;
16209     ELSIF p_sort_by_req_date =inv_reservation_global.g_query_req_date_inv_desc then
16210          OPEN mrc_desc(p_query_input.demand_source_header_id,
16211                                     p_query_input.demand_source_line_id);
16212             l_counter                    := 0;
16213           LOOP
16214           FETCH mrc_desc INTO l_rsv_rec;
16215           EXIT WHEN mrc_desc%NOTFOUND;
16216           l_counter                         := l_counter + 1;
16217           x_mtl_reservation_tbl(l_counter)  := l_rsv_rec;
16218           END LOOP;
16219 
16220          CLOSE  mrc_desc;
16221 
16222     END IF; --Sort by req date
16223     --
16224     x_mtl_reservation_tbl_count  := l_counter;
16225     x_return_status              := l_return_status;
16226     x_error_code                 := inv_reservation_global.g_err_no_error;
16227   --
16228   EXCEPTION
16229     WHEN fnd_api.g_exc_error THEN
16230       x_return_status  := fnd_api.g_ret_sts_error;
16231       --  Get message count and data
16232       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
16233     WHEN fnd_api.g_exc_unexpected_error THEN
16234       x_return_status  := fnd_api.g_ret_sts_unexp_error;
16235       --  Get message count and data
16236       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
16237     WHEN OTHERS THEN
16238       x_return_status  := fnd_api.g_ret_sts_unexp_error;
16239 
16240       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
16241         fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
16242       END IF;
16243 
16244       --  Get message count and data
16245       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
16246   END query_reservation_om_hdr_line;
16247 
16248   -- Change the signiture of upd_reservation_pup for bug 2879208
16249   -- Create overload porceudre
16250 
16251   PROCEDURE Upd_Reservation_Pup_New(
16252     x_return_status           OUT    NOCOPY VARCHAR2
16253   , x_msg_count               OUT    NOCOPY NUMBER
16254   , x_msg_data                OUT    NOCOPY VARCHAR2
16255   , p_commit                  IN     VARCHAR2 := fnd_api.g_false
16256   , p_init_msg_list           IN     VARCHAR2 := fnd_api.g_false
16257   , p_organization_id         IN     NUMBER
16258   , p_demand_source_header_id IN     NUMBER
16259   , p_demand_source_line_id   IN     NUMBER
16260   , p_from_subinventory_code  IN     VARCHAR2
16261   , p_from_locator_id         IN     NUMBER
16262   , p_to_subinventory_code    IN     VARCHAR2
16263   , p_to_locator_id           IN     NUMBER
16264   , p_inventory_item_id       IN     NUMBER
16265   , p_revision                IN     VARCHAR2
16266   , p_lot_number              IN     VARCHAR2
16267   , p_quantity                IN     NUMBER
16268   , p_uom                     IN     VARCHAR2
16269   , p_lpn_id                  IN     NUMBER   := NULL
16270   , p_validation_flag         IN     VARCHAR2 := fnd_api.g_false
16271   , p_force_reservation_flag  IN     VARCHAR2 := fnd_api.g_false
16272   , p_requirement_date        IN     DATE DEFAULT NULL  -- bug 2879208
16273   , p_source_lpn_id           IN     NUMBER   := NULL -- Bug 4016953/3871066
16274   ) IS
16275     l_api_name    CONSTANT VARCHAR2(30)  := 'Upd_Reservation_PUP_New';
16276     l_api_version CONSTANT NUMBER        := 1.0;
16277     l_debug                NUMBER;
16278     l_progress             VARCHAR2(500) := '0';
16279 
16280     -- Variables used by API logic
16281     l_remaining_qty           NUMBER;
16282     l_remaining_qty_uom        VARCHAR2(3);
16283     l_call_xfr_rsv            BOOLEAN := FALSE;
16284     l_reservation_id          NUMBER;
16285     l_demand_source_header_id NUMBER;
16286 
16287     -- MTL_RESERVATIONS record type declarations
16288     l_src_rsv       inv_reservation_global.mtl_reservation_rec_type;
16289     l_xfr_rsv       inv_reservation_global.mtl_reservation_rec_type;
16290     l_serial_number inv_reservation_global.serial_number_tbl_type;
16291 
16292 
16293     -- Cursor to fetch the from reservations
16294     -- INVCONV - Incorporate secondaries into select
16295     CURSOR  rsv_cur IS
16296        SELECT
16297 	 reservation_id
16298 	 , requirement_date
16299 	 , organization_id
16300 	 , inventory_item_id
16301 	 , demand_source_type_id
16302 	 , demand_source_name
16303 	 , demand_source_header_id
16304 	 , demand_source_line_id
16305 	 , demand_source_delivery
16306 	 , primary_uom_code
16307 	 , primary_uom_id
16308 	 , secondary_uom_code
16309 	 , secondary_uom_id
16310 	 , reservation_uom_code
16311 	 , reservation_uom_id
16312 	 , reservation_quantity
16313 	 , primary_reservation_quantity
16314 	 , secondary_reservation_quantity
16315 	 , detailed_quantity
16316 	 , secondary_detailed_quantity
16317 	 , autodetail_group_id
16318 	 , external_source_code
16319 	 , external_source_line_id
16320 	 , supply_source_type_id
16321 	 , supply_source_header_id
16322 	 , supply_source_line_id
16323 	 , supply_source_name
16324 	 , supply_source_line_detail
16325 	 , revision
16326 	 , subinventory_code
16327 	 , subinventory_id
16328 	 , locator_id
16329 	 , lot_number
16330 	 , lot_number_id
16331 	 , pick_slip_number
16332 	 , lpn_id
16333 	 , attribute_category
16334 	 , attribute1
16335 	 , attribute2
16336 	 , attribute3
16337 	 , attribute4
16338 	 , attribute5
16339 	 , attribute6
16340 	 , attribute7
16341 	 , attribute8
16342 	 , attribute9
16343 	 , attribute10
16344 	 , attribute11
16345 	 , attribute12
16346 	 , attribute13
16347 	 , attribute14
16348 	 , attribute15
16349 	 , ship_ready_flag
16350 	 , staged_flag
16351 	 /**** {{ R12 Enhanced reservations code changes }}****/
16352 	 , crossdock_flag
16353 	 , crossdock_criteria_id
16354 	 , demand_source_line_detail
16355 	 , serial_reservation_quantity
16356 	 , supply_receipt_date
16357 	 , demand_ship_date
16358 	 , project_id
16359 	 , task_id
16360 	 , orig_supply_source_type_id
16361 	 , orig_supply_source_header_id
16362 	 , orig_supply_source_line_id
16363 	 , orig_supply_source_line_detail
16364 	 , orig_demand_source_type_id
16365 	 , orig_demand_source_header_id
16366 	 , orig_demand_source_line_id
16367 	 , orig_demand_source_line_detail
16368 	 , serial_number
16369 	 /***** End R12 ***/
16370 
16371         FROM mtl_reservations
16372        WHERE organization_id = p_organization_id
16373          AND inventory_item_id = p_inventory_item_id
16374          AND (p_revision IS NULL OR revision = p_revision)
16375          AND demand_source_header_id = l_demand_source_header_id
16376          AND demand_source_line_id = p_demand_source_line_id
16377          AND subinventory_code = p_from_subinventory_code
16378          AND locator_id = p_from_locator_id
16379          AND (p_lot_number IS NULL OR lot_number = p_lot_number)
16380          -- NVL added to prevent dependency on TM to not break existing code
16381          -- by resulting in a no records found if p_source_lpn_id is null.
16382          -- All new calls to this api starting with this bug
16383          -- fix (4016953/3871066) pass the p_source_lpn_id
16384          AND (lpn_id = NVL(p_source_lpn_id, lpn_id) OR lpn_id IS NULL)
16385     ORDER BY lpn_id asc;
16386 
16387   BEGIN
16388     -- Initialize return status to success
16389     x_return_status := fnd_api.g_ret_sts_success;
16390 
16391     --  Initialize message list.
16392     IF fnd_api.to_boolean(p_init_msg_list) THEN
16393       fnd_msg_pub.initialize;
16394     END IF;
16395 
16396     SAVEPOINT upd_reservation_pup_new;
16397     l_remaining_qty           := p_quantity;
16398     l_remaining_qty_uom       := p_uom;
16399     l_demand_source_header_id := INV_SALESORDER.Get_Salesorder_For_OEHeader(p_demand_source_header_id);
16400 
16401     -- Use cache to get value for l_debug
16402     IF g_is_pickrelease_set IS NULL THEN
16403        g_is_pickrelease_set := 2;
16404        IF INV_CACHE.is_pickrelease THEN
16405           g_is_pickrelease_set := 1;
16406        END IF;
16407     END IF;
16408     IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
16409        g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
16410     END IF;
16411 
16412     l_debug := g_debug;
16413 
16414     IF (l_debug = 1) THEN
16415        debug_print(l_api_name || ' Entered ' || g_pkg_version);
16416        debug_print('orgid='||p_organization_id||' shdid='||p_demand_source_header_id||' slnid='||
16417          p_demand_source_line_id||' fsub='||p_from_subinventory_code||' floc='||p_from_locator_id);
16418        debug_print('tsub='||p_to_subinventory_code||' tloc='||p_to_locator_id||' itm='||p_inventory_item_id||
16419 	  ' rev=' ||p_revision||' lot='||p_lot_number||' qty='||p_quantity||' uom='||p_uom);
16420        debug_print('lpn='||p_lpn_id||' val='||p_validation_flag||' frc='||p_force_reservation_flag||' rdate='||
16421           to_char(p_requirement_date, 'MON-DD-YYYY HH:MI:SS')||' dsrc='||l_demand_source_header_id||' slpn='||p_source_lpn_id);
16422     END IF;
16423 
16424     FOR l_src_rsv IN rsv_cur LOOP
16425       IF (l_debug = 1) THEN
16426         debug_print('Got rsvid= '||l_src_rsv.reservation_id||' qty=
16427                     '||l_src_rsv.primary_reservation_quantity||' uom=
16428                     '||l_src_rsv.primary_uom_code||' sub='||l_src_rsv.subinventory_code||' loc= '||
16429                     l_src_rsv.locator_id||' lpn= '||l_src_rsv.lpn_id);
16430         debug_print('l_remaining_qty: ' || l_remaining_qty || '
16431                    l_remaining_qty_uom ' || l_remaining_qty_uom || '
16432                    l_src_rsv.primary_uom_code:' || l_src_rsv.primary_uom_code);
16433       END IF;
16434 
16435        -- Copy all data from old reservation to the new reservation
16436       l_xfr_rsv := l_src_rsv;
16437 
16438       l_progress := 'Convert l_remaining_qty to rsv uom '||l_remaining_qty;
16439       IF ( l_remaining_qty_uom <> l_src_rsv.primary_uom_code ) THEN
16440 
16441         -- INVCONV - upgrade call to inv_um_convert to pass lot and org
16442         l_remaining_qty := inv_convert.inv_um_convert(
16443            item_id		=> l_src_rsv.inventory_item_id
16444          , lot_number		=> l_src_rsv.lot_number
16445          , organization_id      => l_src_rsv.organization_id
16446          , PRECISION            => NULL -- use default precision
16447          , from_quantity        => l_remaining_qty
16448          , from_unit            => l_remaining_qty_uom
16449          , to_unit              => l_src_rsv.primary_uom_code
16450          , from_name            => NULL -- from uom name
16451          , to_name              => NULL); -- to uom name
16452 
16453          IF l_remaining_qty = -99999 THEN
16454            -- conversion failed
16455            IF (l_debug = 1) THEN
16456              debug_print('Conversion to RSV UOM Failed');
16457            END IF;
16458            fnd_message.set_name('INV', 'CAN-NOT-CONVERT-TO-RSV-UOM');
16459            fnd_msg_pub.ADD;
16460            RAISE fnd_api.g_exc_error;
16461          END IF;
16462 
16463           -- Remember UOM of current remaining quantity
16464           l_remaining_qty_uom := l_src_rsv.primary_uom_code;
16465         END IF;
16466 
16467         -- Even if the reservation does not need to be transfered we count the qty
16468         l_xfr_rsv.primary_reservation_quantity :=
16469           LEAST(l_src_rsv.Primary_reservation_quantity, l_remaining_qty);
16470         --l_xfr_rsv.primary_uom_code := l_src_rsv.primary_uom_code;
16471         l_remaining_qty := l_remaining_qty - l_xfr_rsv.primary_reservation_quantity;
16472         -- INVCONV B4242576 BEGIN
16473         -- compute secondaries as necessary for items tracking in primary and secondary
16474         IF l_src_rsv.secondary_uom_code is not null THEN
16475           IF (l_debug = 1) THEN
16476             debug_print('Dual tracked item so populate secondary quantities for '||l_src_rsv.secondary_uom_code);
16477           END IF;
16478           IF l_xfr_rsv.primary_reservation_quantity < l_src_rsv.primary_reservation_quantity THEN
16479             -- Transferring less than the full qty so determine the equivalent secondary
16480             l_xfr_rsv.secondary_reservation_quantity := inv_convert.inv_um_convert(
16481               item_id              => l_xfr_rsv.inventory_item_id
16482             , lot_number	   => l_xfr_rsv.lot_number
16483             , organization_id      => l_xfr_rsv.organization_id
16484             , PRECISION            => NULL -- use default precision
16485             , from_quantity        => l_xfr_rsv.primary_reservation_quantity
16486             , from_unit            => l_xfr_rsv.primary_uom_code
16487             , to_unit              => l_src_rsv.secondary_uom_code
16488             , from_name            => NULL -- from uom name
16489             , to_name              => NULL); -- to uom name
16490 
16491             IF l_remaining_qty = -99999 THEN
16492               -- conversion failed
16493               IF (l_debug = 1) THEN
16494                 debug_print('Conversion to SECONDARY UOM Failed');
16495               END IF;
16496               fnd_message.set_name('INV', 'CAN-NOT-CONVERT-TO-SECOND-UOM');
16497               fnd_msg_pub.ADD;
16498               RAISE fnd_api.g_exc_error;
16499             END IF;
16500             IF (l_debug = 1) THEN
16501               debug_print('After conversion secondary_qty= '||l_src_rsv.secondary_reservation_quantity); -- KYH
16502             END IF;
16503           END IF;
16504         END IF;
16505         -- INVCONV B4242576 END
16506 
16507       IF (l_debug = 1) THEN
16508          debug_print('l_remaining_qty before xfer: ' || l_remaining_qty || '
16509                      l_xfr_rsv.primary_reservation_quantity ' ||
16510                      l_xfr_rsv.primary_reservation_quantity);
16511       END IF;
16512 
16513       -- Only if the original reservation is at the LPN level should we
16514       -- transfer the reservatoin to the new LPN not applicable if p_lpn_id
16515       -- is null, i.e. whole staged LPN being subxfered
16516       -- Bug 3846145: The transfer should happen only if the source and the
16517       --destination lpns are different.
16518       IF ( l_src_rsv.lpn_id IS NOT NULL AND p_lpn_id IS NOT NULL AND
16519            (l_src_rsv.lpn_id <> p_lpn_id) ) THEN
16520          l_xfr_rsv.lpn_id := p_lpn_id;
16521          l_call_xfr_rsv := TRUE;
16522          IF (l_debug = 1) THEN
16523             debug_print('Inside source lpn and p_lpn are not null');
16524          END IF;
16525       END IF;
16526 
16527       -- If the reservation needs to transfer locations...
16528       IF ( l_src_rsv.subinventory_code <> p_to_subinventory_code OR
16529            l_src_rsv.locator_id        <> p_to_locator_id )
16530       THEN
16531         l_xfr_rsv.subinventory_code := p_to_subinventory_code;
16532         l_xfr_rsv.locator_id        := p_to_locator_id;
16533         l_call_xfr_rsv := TRUE;
16534         IF (l_debug = 1) THEN
16535             debug_print('Inside loc, sub or both are different');
16536          END IF;
16537       END IF;
16538 
16539       IF ( l_call_xfr_rsv ) THEN
16540         l_xfr_rsv.reservation_quantity := NULL;
16541         l_xfr_rsv.reservation_id       := NULL;
16542 
16543         l_progress := 'Call to Transfer_Reservation';
16544         INV_RESERVATION_PVT.Transfer_Reservation (
16545           p_api_version_number     => 1.0
16546         , p_init_msg_lst           => fnd_api.g_false
16547         , x_return_status          => x_return_status
16548         , x_msg_count              => x_msg_count
16549         , x_msg_data               => x_msg_data
16550         , p_original_rsv_rec       => l_src_rsv
16551         , p_to_rsv_rec             => l_xfr_rsv
16552         , p_original_serial_number => l_serial_number
16553         , p_validation_flag        => p_validation_flag
16554         , x_reservation_id         => l_reservation_id );
16555 
16556         IF ( x_return_status = fnd_api.g_ret_sts_error ) THEN
16557           RAISE FND_API.G_EXC_ERROR;
16558         ELSIF ( x_return_status <> fnd_api.g_ret_sts_success ) THEN
16559           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
16560         END IF;
16561 
16562         l_call_xfr_rsv := FALSE;
16563         l_progress := 'Done with reservation loop';
16564       END IF;
16565 
16566       IF (l_debug = 1) THEN
16567         debug_print('new l_remaining_qty='||l_remaining_qty||' last reservation_id='||l_reservation_id);
16568       END IF;
16569 
16570       EXIT WHEN ROUND(l_remaining_qty, 5) <= 0;
16571     END LOOP;
16572     l_progress := 'Done with reservation loop';
16573 
16574     IF ( ROUND(l_remaining_qty, 5) > 0 ) THEN
16575       IF (l_debug = 1) THEN
16576         debug_print('Not enough reserved quantity l_remaining_qty='||l_remaining_qty);
16577       END IF;
16578       fnd_message.set_name('INV', 'INV_UPDATE_RSV_FAILED');
16579       fnd_msg_pub.ADD;
16580       RAISE FND_API.G_EXC_ERROR;
16581     END IF;
16582 
16583     -- Standard check of p_commit.
16584     IF fnd_api.to_boolean(p_commit) THEN
16585       COMMIT WORK;
16586     END IF;
16587   EXCEPTION
16588     WHEN FND_API.G_EXC_ERROR THEN
16589       ROLLBACK TO upd_reservation_pup_new;
16590       x_return_status := fnd_api.g_ret_sts_error;
16591       FND_MSG_PUB.Count_And_Get(p_count => x_msg_count, p_data => x_msg_data);
16592       IF (l_debug = 1) THEN
16593         debug_print(l_api_name ||' Exec Err prog='||l_progress||' SQL error: '|| SQLERRM(SQLCODE));
16594       END IF;
16595     WHEN OTHERS THEN
16596       ROLLBACK TO upd_reservation_pup_new;
16597       x_return_status := fnd_api.g_ret_sts_unexp_error;
16598       FND_MSG_PUB.Count_And_Get(p_count => x_msg_count, p_data => x_msg_data);
16599       IF (l_debug = 1) THEN
16600         debug_print(l_api_name ||' Unexp Err prog='||l_progress||' SQL error: '|| SQLERRM(SQLCODE));
16601       END IF;
16602   END Upd_Reservation_PUP_New;
16603 
16604 
16605 -- Create overload porceudre for bug 2879208
16606    PROCEDURE upd_reservation_pup(
16607     x_return_status           OUT    NOCOPY VARCHAR2
16608   , x_msg_count               OUT    NOCOPY NUMBER
16609   , x_msg_data                OUT    NOCOPY VARCHAR2
16610   , p_commit                  IN     VARCHAR2 := fnd_api.g_false
16611   , p_init_msg_list           IN     VARCHAR2 := fnd_api.g_false
16612   , p_organization_id         IN     NUMBER
16613   , p_demand_source_header_id IN     NUMBER
16614   , p_demand_source_line_id   IN     NUMBER
16615   , p_from_subinventory_code  IN     VARCHAR2
16616   , p_from_locator_id         IN     NUMBER
16617   , p_to_subinventory_code    IN     VARCHAR2
16618   , p_to_locator_id           IN     NUMBER
16619   , p_inventory_item_id       IN     NUMBER
16620   , p_revision                IN     VARCHAR2
16621   , p_lot_number              IN     VARCHAR2
16622   , p_quantity                IN     NUMBER
16623   , p_uom                     IN     VARCHAR2
16624   , p_validation_flag         IN     VARCHAR2 := fnd_api.g_false
16625   , p_force_reservation_flag  IN     VARCHAR2 := fnd_api.g_false
16626   )
16627      IS
16628 
16629    BEGIN
16630       upd_reservation_pup_new
16631 	(
16632 	 x_return_status              => x_return_status
16633 	 , x_msg_count                  => x_msg_count
16634 	 , x_msg_data                   => x_msg_data
16635 	 , p_organization_id            => p_organization_id
16636 	 , p_demand_source_header_id    => p_demand_source_header_id
16637 	 , p_demand_source_line_id      => p_demand_source_line_id
16638 	 , p_from_subinventory_code     => p_from_subinventory_code
16639 	 , p_from_locator_id            => p_from_locator_id
16640 	 , p_to_subinventory_code       => p_to_subinventory_code
16641 	 , p_to_locator_id              => p_to_locator_id
16642 	 , p_inventory_item_id          => p_inventory_item_id
16643 	 , p_revision                   => p_revision
16644 	 , p_lot_number                 => p_lot_number
16645 	 , p_quantity                   => p_quantity
16646 	 , p_uom                        => p_uom
16647 	 , p_force_reservation_flag     => p_force_reservation_flag
16648 	 , p_requirement_date           => NULL
16649         );
16650    END upd_reservation_pup;
16651 
16652 
16653 
16654 
16655   PROCEDURE transfer_lpn_trx_reservation(
16656     x_return_status          OUT    NOCOPY VARCHAR2
16657   , x_msg_count              OUT    NOCOPY NUMBER
16658   , x_msg_data               OUT    NOCOPY VARCHAR2
16659   , p_commit                 IN     VARCHAR2 := fnd_api.g_false
16660   , p_init_msg_list          IN     VARCHAR2 := fnd_api.g_false
16661   , p_transaction_temp_id    IN     NUMBER := 0
16662   , p_organization_id        IN     NUMBER
16663   , p_lpn_id                 IN     NUMBER
16664   , p_from_subinventory_code IN     VARCHAR2
16665   , p_from_locator_id        IN     NUMBER
16666   , p_to_subinventory_code   IN     VARCHAR2
16667   , p_to_locator_id          IN     NUMBER
16668   , p_inventory_item_id      IN     NUMBER := NULL
16669   , p_revision               IN     VARCHAR2 := NULL
16670   , p_lot_number             IN     VARCHAR2 := NULL
16671   , p_trx_quantity           IN     NUMBER := NULL
16672   , p_trx_uom                IN     VARCHAR2 := NULL
16673   ) IS
16674     l_count    NUMBER  := 0;
16675     l_lotfound BOOLEAN := FALSE;
16676 
16677 
16678     l_api_version_number NUMBER := 1.0;
16679 
16680     l_loop_counter NUMBER := 0;   -- bug 2879208
16681 
16682     CURSOR c_lottmp IS
16683       SELECT lot_number
16684            , transaction_quantity
16685         FROM mtl_transaction_lots_temp
16686        WHERE transaction_temp_id = p_transaction_temp_id;
16687 
16688 	    --bug 2648532 - performance changes
16689 	    CURSOR lpn_deliveries IS
16690 	       SELECT wdd.source_header_id
16691 	            , wdd.source_line_id
16692 	            , wdd.inventory_item_id
16693 	            , wdd.revision
16694 	            , wdd.lot_number
16695 	            , wdd.requested_quantity
16696 	            , wdd.requested_quantity_uom
16697 	            , wdd.delivery_detail_id
16698 	            , wdd2.lpn_id
16699 	         FROM wsh_delivery_details wdd,
16700 	              wsh_delivery_assignments wda,
16701 	              wsh_delivery_details wdd2
16702 	        WHERE wdd.container_flag = 'N' --bug4639858 Only want wdd lines that represent items
16703 	          AND wdd.delivery_detail_id = wda.delivery_detail_id
16704 	          AND wda.parent_delivery_detail_id = wdd2.delivery_detail_id
16705 	          -- Workaround for performance issue 3631133
16706 	          -- lpn_id for the LPN and it's child lpns will be stored in this global
16707 	          -- temp table under the line_id.  this is to avoid a connect by statment
16708 	          -- in this cursor
16709                   -- Note: Repeating the where condition below to force the db optimizer
16710                   -- to drive the main-query from sub-query. bug: 4145360
16711              AND wdd2.released_status = 'X'    -- For LPN reuse ER : 6845650
16712 	          AND wdd2.lpn_id IN ( SELECT line_id
16713 	                               FROM   WMS_TXN_CONTEXT_TEMP
16714 	                               WHERE  txn_source_name = 'XFER_LPN_RES'
16715 	                                 AND  txn_source_name = 'XFER_LPN_RES' );
16716 
16717     CURSOR item_deliveries(p_lot VARCHAR2) IS
16718       SELECT source_header_id
16719            , source_line_id
16720         FROM wsh_delivery_details
16721        WHERE inventory_item_id = p_inventory_item_id
16722          AND NVL(subinventory, '@@@@') = NVL(p_from_subinventory_code, '@@@@')
16723          AND NVL(revision, '@@@@') = NVL(p_revision, '@@@@')
16724          AND NVL(lot_number, '@@@@') = NVL(p_lot, '@@@@')
16725          AND delivery_detail_id IN
16726 	(SELECT wda.delivery_detail_id
16727 	 FROM wsh_delivery_assignments wda,
16728 	 wsh_delivery_details wdd2,
16729 	 wms_license_plate_numbers wlpn
16730 	 WHERE wda.parent_delivery_detail_id = wdd2.delivery_detail_id
16731 	 AND   wdd2.lpn_id                   = wlpn.lpn_id
16732 	 AND   wlpn.outermost_lpn_id         = p_lpn_id
16733     AND   wdd2.released_status          = 'X');  -- For LPN reuse ER : 6845650
16734 
16735     l_debug number;
16736   BEGIN
16737     -- Standard Start of API savepoint
16738     SAVEPOINT transfer_lpn_trx_reservation;
16739     -- Use cache to get value for l_debug
16740     IF g_is_pickrelease_set IS NULL THEN
16741        g_is_pickrelease_set := 2;
16742        IF INV_CACHE.is_pickrelease THEN
16743           g_is_pickrelease_set := 1;
16744        END IF;
16745     END IF;
16746     IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
16747        g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
16748     END IF;     -- Use cache to get value for l_debug
16749 
16750     l_debug := g_debug;
16751     -- If p_lpn_id parameter has been popluated, then transfer is a consolidation
16752     IF (p_inventory_item_id IS NULL) THEN
16753       -- retrieve all the delivery items for that lpn and call transfer reservatin api
16754       IF (l_debug = 1) THEN
16755          debug_print('Reservation transfer of whole lpn: consolidate', 9);
16756       END IF;
16757 
16758       -- Workaround for performance issue 3631133
16759       -- lpn_id for the LPN and it's child lpns will be stored in this global
16760       -- temp table under the line_id
16761       INSERT INTO WMS_TXN_CONTEXT_TEMP ( line_id, txn_source_name )
16762       SELECT distinct lpn_id, 'XFER_LPN_RES'
16763       FROM   wms_license_plate_numbers
16764       START WITH lpn_id = p_lpn_id
16765       CONNECT BY parent_lpn_id = PRIOR lpn_id;
16766 
16767       FOR lpn_del_rec IN lpn_deliveries LOOP
16768 	 l_loop_counter := l_loop_counter + 1;   -- bug 2879208
16769 
16770         IF (l_debug = 1) THEN
16771            debug_print(
16772              'found rec src hdr id: '
16773           || lpn_del_rec.source_header_id
16774           || ' src line id: '
16775           || lpn_del_rec.source_line_id
16776           || ' item id: '
16777           || lpn_del_rec.inventory_item_id
16778           || ' rev: '
16779           || lpn_del_rec.revision
16780           || ' lot: '
16781           || lpn_del_rec.lot_number
16782 	  || ' ddid='||lpn_del_rec.delivery_detail_id
16783 	  || ' lpn='||lpn_del_rec.lpn_id
16784 	  , 9
16785         );
16786         END IF;
16787         upd_reservation_pup_new(
16788           x_return_status              => x_return_status
16789         , x_msg_count                  => x_msg_count
16790         , x_msg_data                   => x_msg_data
16791         , p_organization_id            => p_organization_id
16792         , p_demand_source_header_id    => lpn_del_rec.source_header_id
16793         , p_demand_source_line_id      => lpn_del_rec.source_line_id
16794         , p_from_subinventory_code     => p_from_subinventory_code
16795         , p_from_locator_id            => p_from_locator_id
16796         , p_to_subinventory_code       => p_to_subinventory_code
16797         , p_to_locator_id              => p_to_locator_id
16798         , p_inventory_item_id          => lpn_del_rec.inventory_item_id
16799         , p_revision                   => lpn_del_rec.revision
16800         , p_lot_number                 => lpn_del_rec.lot_number
16801         , p_quantity                   => lpn_del_rec.requested_quantity
16802         , p_uom                        => lpn_del_rec.requested_quantity_uom
16803 	, p_force_reservation_flag     => fnd_api.g_true
16804 	, p_requirement_date           => (Sysdate + l_loop_counter/(24*3600))  -- bug 2879208
16805         );
16806 
16807         IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
16808           IF (l_debug = 1) THEN
16809              debug_print('upd_reservation_pup failed '|| x_msg_data, 1);
16810           END IF;
16811           fnd_message.set_name('WMS', 'UPD_RESERVATION_PUP_FAIL');
16812           fnd_msg_pub.ADD;
16813           RAISE fnd_api.g_exc_unexpected_error;
16814         END IF;
16815       END LOOP;
16816 
16817       -- Workaround for performance issue 3631133
16818       -- need to delete the inserted values in temp table above
16819       DELETE FROM WMS_TXN_CONTEXT_TEMP
16820       WHERE  txn_source_name = 'XFER_LPN_RES';
16821     ELSE
16822       --LPN split, transfer reservation of a single item type
16823       IF (l_debug = 1) THEN
16824          debug_print('reservation transfer of item within an lpn: split', 9);
16825       END IF;
16826 
16827       --Check if item is lot controlled
16828       FOR v_lottmp IN c_lottmp LOOP
16829         l_loop_counter := l_loop_counter + 1;
16830 
16831         IF (l_debug = 1) THEN
16832            debug_print('item is lot controlled found lot= '|| v_lottmp.lot_number, 9);
16833         END IF;
16834         l_lotfound  := TRUE;
16835 
16836         FOR item_del_rec IN item_deliveries(v_lottmp.lot_number) LOOP
16837           IF (l_debug = 1) THEN
16838              debug_print('found rec src hdr id: '|| item_del_rec.source_header_id || ' src line id: ' || item_del_rec.source_line_id, 9);
16839           END IF;
16840 
16841           IF (l_count < 1) THEN
16842             upd_reservation_pup_new(
16843               x_return_status              => x_return_status
16844             , x_msg_count                  => x_msg_count
16845             , x_msg_data                   => x_msg_data
16846             , p_organization_id            => p_organization_id
16847             , p_demand_source_header_id    => item_del_rec.source_header_id
16848             , p_demand_source_line_id      => item_del_rec.source_line_id
16849             , p_from_subinventory_code     => p_from_subinventory_code
16850             , p_from_locator_id            => p_from_locator_id
16851             , p_to_subinventory_code       => p_to_subinventory_code
16852             , p_to_locator_id              => p_to_locator_id
16853             , p_inventory_item_id          => p_inventory_item_id
16854             , p_revision                   => p_revision
16855             , p_lot_number                 => v_lottmp.lot_number
16856             , p_quantity                   => v_lottmp.transaction_quantity
16857             , p_uom                        => p_trx_uom
16858             , p_force_reservation_flag     => fnd_api.g_true
16859             , p_requirement_date           => (Sysdate + l_loop_counter/(24*3600))
16860             );
16861 
16862             IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
16863               IF (l_debug = 1) THEN
16864                  debug_print('upd_reservation_pup failed '|| x_msg_data, 1);
16865               END IF;
16866               fnd_message.set_name('WMS', 'UPD_RESERVATION_PUP_FAIL');
16867               fnd_msg_pub.ADD;
16868               RAISE fnd_api.g_exc_unexpected_error;
16869             END IF;
16870 
16871             l_count  := l_count + 1;
16872           ELSE
16873             IF (l_debug = 1) THEN
16874                debug_print('**Split disallowed for lpns with multiple delivery lines for the same item', 1);
16875             END IF;
16876             fnd_message.set_name('INV', 'INV_MULTI_DEL_SPLIT_ERROR');
16877             fnd_msg_pub.ADD;
16878             RAISE fnd_api.g_exc_error;
16879           END IF;
16880         END LOOP;
16881       END LOOP;
16882 
16883       IF (NOT l_lotfound) THEN
16884         l_loop_counter := l_loop_counter + 1;
16885 
16886         --Non lot controlled item, or single lot entry use given parameters.
16887         IF (l_debug = 1) THEN
16888            debug_print('no rows found in mtlt, processing single row', 9);
16889         END IF;
16890 
16891         FOR item_del_rec IN item_deliveries(p_lot_number) LOOP
16892           IF (l_debug = 1) THEN
16893              debug_print('found rec src hdr id: '|| item_del_rec.source_header_id || ' src line id: ' || item_del_rec.source_line_id, 9);
16894           END IF;
16895 
16896           IF (l_count < 1) THEN
16897             upd_reservation_pup_new(
16898               x_return_status              => x_return_status
16899             , x_msg_count                  => x_msg_count
16900             , x_msg_data                   => x_msg_data
16901             , p_organization_id            => p_organization_id
16902             , p_demand_source_header_id    => item_del_rec.source_header_id
16903             , p_demand_source_line_id      => item_del_rec.source_line_id
16904             , p_from_subinventory_code     => p_from_subinventory_code
16905             , p_from_locator_id            => p_from_locator_id
16906             , p_to_subinventory_code       => p_to_subinventory_code
16907             , p_to_locator_id              => p_to_locator_id
16908             , p_inventory_item_id          => p_inventory_item_id
16909             , p_revision                   => p_revision
16910             , p_lot_number                 => p_lot_number
16911             , p_quantity                   => p_trx_quantity
16912             , p_uom                        => p_trx_uom
16913             , p_force_reservation_flag     => fnd_api.g_true
16914             , p_requirement_date           => (Sysdate + l_loop_counter/(24*3600))
16915             );
16916 
16917             IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
16918               IF (l_debug = 1) THEN
16919                  debug_print('upd_reservation_pup failed '|| x_msg_data, 1);
16920               END IF;
16921               fnd_message.set_name('WMS', 'UPD_RESERVATION_PUP_FAIL');
16922               fnd_msg_pub.ADD;
16923               RAISE fnd_api.g_exc_unexpected_error;
16924             END IF;
16925 
16926             l_count  := l_count + 1;
16927           ELSE
16928             IF (l_debug = 1) THEN
16929                debug_print('**Split disallowed for lpns with multiple delivery lines for the same item', 1);
16930             END IF;
16931             fnd_message.set_name('INV', 'INV_MULTI_DEL_SPLIT_ERROR');
16932             fnd_msg_pub.ADD;
16933             RAISE fnd_api.g_exc_error;
16934           END IF;
16935         END LOOP;
16936       END IF;
16937     END IF;
16938   EXCEPTION
16939     WHEN fnd_api.g_exc_error THEN
16940       fnd_message.set_name('WMS', 'INV_XFR_RSV_FAILURE');
16941       fnd_msg_pub.ADD;
16942       ROLLBACK TO transfer_lpn_trx_reservation;
16943       x_return_status  := fnd_api.g_ret_sts_error;
16944       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
16945     WHEN fnd_api.g_exc_unexpected_error THEN
16946       fnd_message.set_name('WMS', 'INV_XFR_RSV_FAILURE');
16947       fnd_msg_pub.ADD;
16948       ROLLBACK TO transfer_lpn_trx_reservation;
16949       x_return_status  := fnd_api.g_ret_sts_unexp_error;
16950       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
16951     WHEN OTHERS THEN
16952       fnd_message.set_name('WMS', 'INV_XFR_RSV_FAILURE');
16953       fnd_msg_pub.ADD;
16954       ROLLBACK TO transfer_lpn_trx_reservation;
16955       x_return_status  := fnd_api.g_ret_sts_unexp_error;
16956 
16957       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
16958         fnd_msg_pub.add_exc_msg('wms_upd_res_pvt', 'TRANSFER_LPN_TRX_RESERVATION');
16959       END IF;
16960 
16961       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
16962   END transfer_lpn_trx_reservation;
16963 
16964   PROCEDURE insert_rsv_temp(
16965     p_organization_id                  NUMBER
16966   , p_inventory_item_id                NUMBER
16967   , p_primary_reservation_quantity     NUMBER
16968   , p_tree_id                          NUMBER
16969   , p_reservation_id                   NUMBER
16970   , x_return_status                OUT NOCOPY VARCHAR2
16971   , p_demand_source_line_id            NUMBER
16972   , p_demand_source_header_id          NUMBER
16973   , p_demand_source_name               VARCHAR2
16974   ) IS
16975     l_api_name      VARCHAR2(100)  := 'Insert_rsv_temp';
16976     x_msg_count     NUMBER;
16977     x_msg_data      VARCHAR2(1000);
16978     l_return_status VARCHAR2(1)    := fnd_api.g_ret_sts_success;
16979     l_debug         NUMBER := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
16980   BEGIN
16981     INSERT INTO rsv_temp
16982                 (
16983                 p_organization_id
16984               , p_inventory_item_id
16985               , p_primary_reservation_quantity
16986               , p_tree_id
16987               , p_reservation_id
16988               , p_demand_source_line_id
16989               , p_demand_source_header_id
16990               , p_demand_source_name
16991                 )
16992          VALUES (
16993                 p_organization_id
16994               , p_inventory_item_id
16995               , p_primary_reservation_quantity
16996               , p_tree_id
16997               , p_reservation_id
16998               , p_demand_source_line_id
16999               , p_demand_source_header_id
17000               , p_demand_source_name
17001                 );
17002 
17003          IF (l_debug = 1) THEN
17004             debug_print('Inserted rsv_temp org='||p_organization_id||' item='||p_inventory_item_id||', p_tree_id='||p_tree_id||', p_reservation_id='||p_reservation_id||', p_demand_source_line_id ='||p_demand_source_line_id);
17005          END IF;
17006 
17007     x_return_status  := l_return_status;
17008   EXCEPTION
17009     WHEN OTHERS THEN
17010       x_return_status  := fnd_api.g_ret_sts_unexp_error;
17011 
17012       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
17013         fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
17014       END IF;
17015 
17016       --  Get message count and data
17017       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
17018   END insert_rsv_temp;
17019 
17020   --Ref 2132071 This procedure pick up reservations created from last commit issued
17021   --and check if there is any node violation.If there is one then it will clear
17022   --the reservations .This call also clears Quantity cache at the end of call
17023   --to ensure that bad trees are not in place.
17024 
17025   PROCEDURE do_check_for_commit(
17026     p_api_version_number  IN     NUMBER
17027   , p_init_msg_lst        IN     VARCHAR2 DEFAULT fnd_api.g_false
17028   , x_return_status       OUT    NOCOPY VARCHAR2
17029   , x_msg_count           OUT    NOCOPY NUMBER
17030   , x_msg_data            OUT    NOCOPY VARCHAR2
17031   , x_failed_rsv_temp_tbl OUT    NOCOPY inv_reservation_global.mtl_failed_rsv_tbl_type
17032   ) IS
17033     l_api_version_number CONSTANT NUMBER                                          := 1.0;
17034     l_api_name           CONSTANT VARCHAR2(30)                                    := 'Do_check_for_commit';
17035     l_return_status               VARCHAR2(1)                                     := fnd_api.g_ret_sts_success;
17036     l_no_violation                BOOLEAN;
17037     l_root_id                     INTEGER;
17038     l_failed_rsv_temp_tbl         inv_reservation_global.mtl_failed_rsv_tbl_type;
17039     l_failed_rsv_temp_rec         inv_reservation_global.mtl_failed_rsv_rec_type;
17040     l_rsv_rec                     inv_reservation_global.mtl_reservation_rec_type;
17041     p_original_serial_number      inv_reservation_global.serial_number_tbl_type;
17042     l_reservation_id              NUMBER;
17043     l_failed_rsv_temp_tbl_count   INTEGER                                         := 0;
17044     l_error_code                  VARCHAR2(100);
17045     l_demand_source_line_id       NUMBER;
17046     l_demand_source_header_id     NUMBER;
17047     l_demand_source_name          VARCHAR2(1000);
17048     l_organization_id             NUMBER;
17049     l_inventory_item_id           NUMBER;
17050 
17051     l_debug                       NUMBER := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
17052 
17053     -- Bug 3926414, added order by clause to avoid deadlock.
17054     CURSOR tree_csr IS
17055       SELECT p_tree_id
17056         FROM rsv_temp
17057         ORDER BY p_organization_id, p_inventory_item_id;
17058 
17059     --Bug 6812723, changing input from p_tree_id to l_tree_id
17060     CURSOR rsv_csr(l_tree_id NUMBER) IS
17061       SELECT p_reservation_id
17062            , p_organization_id
17063            , p_inventory_item_id
17064            , p_demand_source_line_id
17065            , p_demand_source_header_id
17066            , p_demand_source_name
17067         FROM rsv_temp
17068        WHERE p_tree_id = l_tree_id;
17069   BEGIN
17070     FOR tree_rec IN tree_csr LOOP
17071 
17072       IF (l_debug = 1) THEN
17073             debug_print('Calling do_check for tree_id = '||tree_rec.p_tree_id);
17074       END IF;
17075 
17076       inv_quantity_tree_pvt.do_check(p_api_version_number, p_init_msg_lst, l_return_status, x_msg_count, x_msg_data, tree_rec.p_tree_id, l_no_violation);
17077 
17078       IF l_return_status = fnd_api.g_ret_sts_error THEN
17079         RAISE fnd_api.g_exc_error;
17080       END IF;
17081 
17082       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
17083         RAISE fnd_api.g_exc_unexpected_error;
17084       END IF;
17085 
17086       IF NOT (l_no_violation) THEN
17087         OPEN rsv_csr(tree_rec.p_tree_id);
17088 
17089         LOOP
17090           FETCH rsv_csr INTO l_reservation_id, l_organization_id, l_inventory_item_id, l_demand_source_line_id, l_demand_source_header_id, l_demand_source_name;
17091           EXIT WHEN rsv_csr%NOTFOUND;
17092           l_rsv_rec.reservation_id                            := l_reservation_id;
17093 
17094           IF (l_debug = 1) THEN
17095                 debug_print('Deleting Rsv for Org='||l_organization_id||', item='||l_inventory_item_id||', reservation_id='||l_reservation_id||', demand_source_line_id ='||l_demand_source_line_id);
17096              END IF;
17097 
17098           delete_reservation(
17099             p_api_version_number         => 1.0
17100           , p_init_msg_lst               => p_init_msg_lst
17101           , x_return_status              => l_return_status
17102           , x_msg_count                  => x_msg_count
17103           , x_msg_data                   => x_msg_data
17104           , p_rsv_rec                    => l_rsv_rec
17105           , p_original_serial_number     => p_original_serial_number
17106           , p_validation_flag            => fnd_api.g_false
17107           );
17108 
17109           IF l_return_status = fnd_api.g_ret_sts_error THEN
17110             RAISE fnd_api.g_exc_error;
17111           END IF;
17112 
17113           --
17114           IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
17115             RAISE fnd_api.g_exc_unexpected_error;
17116           --  END IF;
17117           END IF;
17118 
17119           l_failed_rsv_temp_tbl_count                         := l_failed_rsv_temp_tbl_count + 1;
17120           l_failed_rsv_temp_rec.reservation_id                := l_reservation_id;
17121           l_failed_rsv_temp_rec.organization_id               := l_organization_id;
17122           l_failed_rsv_temp_rec.inventory_item_id             := l_inventory_item_id;
17123           l_failed_rsv_temp_rec.demand_source_line_id         := l_demand_source_line_id;
17124           l_failed_rsv_temp_rec.demand_source_header_id       := l_demand_source_header_id;
17125           l_failed_rsv_temp_rec.demand_source_name            := l_demand_source_name;
17126           l_failed_rsv_temp_tbl(l_failed_rsv_temp_tbl_count)  := l_failed_rsv_temp_rec;
17127         END LOOP;
17128 
17129         IF rsv_csr%ISOPEN THEN
17130           CLOSE rsv_csr;
17131         END IF;
17132       END IF;
17133     END LOOP;
17134 
17135     x_failed_rsv_temp_tbl  := l_failed_rsv_temp_tbl;
17136     x_return_status        := l_return_status;
17137     inv_quantity_tree_pub.clear_quantity_cache;
17138   EXCEPTION
17139     WHEN fnd_api.g_exc_error THEN
17140       x_return_status  := fnd_api.g_ret_sts_error;
17141       --  Get message count and data
17142       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
17143     WHEN fnd_api.g_exc_unexpected_error THEN
17144       x_return_status  := fnd_api.g_ret_sts_unexp_error;
17145       --  Get message count and data
17146       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
17147     WHEN OTHERS THEN
17148       x_return_status  := fnd_api.g_ret_sts_unexp_error;
17149 
17150       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
17151         fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
17152       END IF;
17153 
17154       --  Get message count and data
17155       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
17156   END do_check_for_commit;
17157 
17158   /**** {{ R12 Enhanced reservations code changes }}****/
17159   PROCEDURE Transfer_Reservation_SubXfer
17160     ( p_api_version_number         IN  NUMBER   DEFAULT 1.0
17161     , p_init_msg_lst               IN  VARCHAR2 DEFAULT fnd_api.g_false
17162     , x_return_status              OUT NOCOPY VARCHAR2
17163     , x_msg_count                  OUT NOCOPY NUMBER
17164     , x_msg_data                   OUT NOCOPY VARCHAR2
17165     , p_Inventory_Item_Id          IN  Number
17166     , p_Organization_id            IN  Number
17167     , p_original_Reservation_Id    IN  Number
17168     , p_From_Serial_Number         IN  Varchar2
17169     , p_to_SubInventory            IN  Varchar2
17170     , p_To_Locator_Id              IN  Number
17171     , p_to_serial_number           IN  Varchar2
17172     , p_validation_flag            IN  VARCHAR2
17173     , x_to_reservation_id          OUT NOCOPY NUMBER)
17174 
17175     IS
17176 
17177         l_api_name    CONSTANT VARCHAR2(30) := 'Transfer_Reservation_SubXfer';
17178         -- Define local variables
17179         l_original_rsv_rec           inv_reservation_global.mtl_reservation_rec_type ;
17180         l_to_rsv_rec                 inv_reservation_global.mtl_reservation_rec_type ;
17181         l_original_serial_number_Tab inv_reservation_global.serial_number_tbl_type ;
17182         l_to_serial_number_Tab       inv_reservation_global.serial_number_tbl_type ;
17183         l_Reservation_Id             Number;
17184         l_return_status              VARCHAR2(1):= fnd_api.g_ret_sts_success;
17185 	l_debug number;
17186 
17187         --Define a cursor that gets ReservationId information for a given org_id,
17188         --item_id and serial number information
17189         Cursor Get_Reservation_ID_Cur_Type
17190                      ( v_Serial_Number     Varchar2
17191                       ,v_Inventory_Item_Id Number
17192                       ,v_Organization_Id   Number ) IS
17193 
17194              Select Reservation_ID
17195                From Mtl_Serial_Numbers
17196               Where Inventory_Item_Id = v_Inventory_Item_Id
17197                 and current_organization_id = v_Organization_id
17198                 and Serial_Number = v_Serial_Number;
17199         BEGIN
17200 
17201            -- Use cache to get value for l_debug
17202            IF g_is_pickrelease_set IS NULL THEN
17203               g_is_pickrelease_set := 2;
17204               IF INV_CACHE.is_pickrelease THEN
17205                  g_is_pickrelease_set := 1;
17206               END IF;
17207            END IF;
17208            IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
17209               g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
17210            END IF;
17211 
17212            l_debug := g_debug;
17213 
17214               -- Check if reservation id parameter has any value if not then get value from mtl_serial_numbers table.
17215 	   If p_original_Reservation_Id IS Null THEN
17216 	      IF (l_debug = 1) THEN
17217 		 debug_print('reservation_id is not passed');
17218 	      END IF;
17219                    Open Get_Reservation_ID_Cur_Type
17220                         (p_From_Serial_Number,
17221                          p_Inventory_Item_Id,
17222                          p_Organization_Id );
17223                    Fetch Get_Reservation_ID_Cur_Type Into l_Reservation_Id;
17224                    Close Get_Reservation_ID_Cur_Type ;
17225 	    ELSE
17226 	      IF (l_debug = 1) THEN
17227 		 debug_print('reservation_id is passed');
17228 	      END IF;
17229                  l_Reservation_Id := p_original_Reservation_Id ;
17230               End If;
17231               If l_Reservation_Id IS NULL  Then
17232                    RAISE fnd_api.g_exc_unexpected_error;
17233               End If;
17234 
17235               l_original_rsv_rec.Reservation_id := l_Reservation_Id ;
17236               l_To_Rsv_Rec.subinventory_code    := p_to_SubInventory ;
17237               l_To_Rsv_Rec.locator_id           := p_To_Locator_Id ;
17238 	      l_to_rsv_rec.primary_reservation_quantity := 1;
17239              -- l_Original_serial_number_Tab(1).Serial_Number := p_from_serial_number;
17240               l_to_serial_number_tab(1).Serial_Number       := p_to_serial_number;
17241               l_to_serial_number_tab(1).inventory_item_id   := p_inventory_item_id;
17242               --Call Reservation Transafer API
17243 
17244               Inv_Reservation_Pvt.transfer_reservation
17245 		(
17246                  p_api_version_number     => p_api_version_number ,
17247                  p_init_msg_lst           => p_init_msg_lst,
17248                  x_return_status          => l_return_status,
17249                  x_msg_count              => x_msg_count,
17250                  x_msg_data               => x_msg_data,
17251                  p_original_rsv_rec       => l_original_rsv_rec,
17252                  p_to_rsv_rec             => l_To_Rsv_Rec ,
17253                  p_original_serial_number => l_original_serial_number_Tab,
17254                  p_to_serial_number       => l_to_serial_number_Tab,
17255                  p_validation_flag        => p_Validation_Flag,
17256 		 x_reservation_id         => x_To_Reservation_Id );
17257 
17258 	      IF (l_debug = 1) THEN
17259 		 debug_print('After calling transfer reservation. Return status: ' ||  l_return_status);
17260 	      END IF;
17261 
17262 	      IF l_return_status = fnd_api.g_ret_sts_error THEN
17263 		 RAISE fnd_api.g_exc_error;
17264 	      END IF ;
17265 
17266            IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
17267               RAISE fnd_api.g_exc_unexpected_error;
17268            END IF;
17269 
17270            x_return_status := l_return_status;
17271 
17272         EXCEPTION
17273 
17274             WHEN fnd_api.g_exc_error THEN
17275                 x_return_status := fnd_api.g_ret_sts_error;
17276 
17277                 --  Get message count and data
17278                 fnd_msg_pub.count_and_get
17279                   (  p_count => x_msg_count
17280                    , p_data  => x_msg_data
17281                    );
17282 
17283            WHEN fnd_api.g_exc_unexpected_error THEN
17284                 x_return_status := fnd_api.g_ret_sts_unexp_error ;
17285 
17286                 --  Get message count and data
17287                 fnd_msg_pub.count_and_get
17288                   (  p_count  => x_msg_count
17289                    , p_data   => x_msg_data
17290                     );
17291 
17292             WHEN OTHERS THEN
17293                 x_return_status := fnd_api.g_ret_sts_unexp_error ;
17294 
17295                 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
17296                   THEN
17297                    fnd_msg_pub.add_exc_msg
17298                      (  g_pkg_name
17299                       , l_api_name
17300                       );
17301                 END IF;
17302 
17303                 --  Get message count and data
17304                 fnd_msg_pub.count_and_get
17305                   (  p_count  => x_msg_count
17306                    , p_data   => x_msg_data
17307                      );
17308 
17309   END transfer_reservation_SubXfer;
17310 
17311   PROCEDURE transfer_serial_rsv_in_LPN(
17312      x_return_status             OUT NOCOPY VARCHAR2
17313    , x_msg_count                 OUT NOCOPY NUMBER
17314    , x_msg_data                  OUT NOCOPY VARCHAR2
17315    , p_organization_id           IN  NUMBER
17316    , p_inventory_item_id         IN  NUMBER DEFAULT NULL
17317    , p_lpn_id                    IN  NUMBER
17318    , p_outermost_lpn_id          IN  NUMBER
17319    , p_to_subinventory_code      IN  VARCHAR2
17320    , p_to_locator_id             IN  NUMBER)
17321   IS
17322     l_api_name                   CONSTANT VARCHAR2(30) := 'transfer_serial_rsv_in_LPN';
17323     l_return_status              VARCHAR2(1)    := fnd_api.g_ret_sts_success;
17324     l_msg_count                  NUMBER;
17325     l_msg_data                   VARCHAR2(1000);
17326     l_debug                      NUMBER;
17327 
17328     TYPE rsv_serials_item_rec    IS RECORD
17329       (
17330          reservation_id           NUMBER
17331        , serial_number            VARCHAR2(30)
17332        , inventory_item_id        NUMBER
17333       );
17334 
17335     TYPE rsv_serials_item_tbl IS TABLE OF rsv_serials_item_rec
17336       INDEX BY BINARY_INTEGER;
17337 
17338     l_rsv_serials_tbl            rsv_serials_item_tbl;
17339     l_serial_number_tbl          inv_reservation_global.serial_number_tbl_type;
17340     l_index                      NUMBER := 0;
17341     l_rsv_serials_tbl_count      NUMBER := 0;
17342     l_original_rsv_rec           inv_reservation_global.mtl_reservation_rec_type;
17343     l_to_rsv_rec                 inv_reservation_global.mtl_reservation_rec_type;
17344     l_reservation_id             NUMBER;
17345 
17346     -- cursor of getting reservations with serial_number reserved in the lpn which
17347     -- outermost_lpn_id of the lpn = p_outermost_lpn_id and the reservation does not
17348     -- have lpn reserved and p_inventory_item_id is passed.
17349     CURSOR serials_outer_lpn_with_item IS
17350        SELECT msn.reservation_id,
17351               msn.serial_number,
17352               msn.inventory_item_id
17353        FROM   mtl_reservations mr,
17354               mtl_serial_numbers msn
17355        WHERE  mr.organization_id = p_organization_id
17356        AND    mr.inventory_item_id = p_inventory_item_id
17357        AND    mr.reservation_id = msn.reservation_id
17358        AND    mr.lpn_id = null
17359        AND    msn.lpn_id IN (SELECT lpn_id
17360                              FROM   wms_license_plate_numbers
17361                              WHERE  outermost_lpn_id = p_outermost_lpn_id)
17362        GROUP BY msn.reservation_id, msn.serial_number, msn.inventory_item_id;
17363 
17364     -- cursor of getting reservations with serial_number reserved in the lpn which
17365     -- outermost_lpn_id of the lpn = p_outermost_lpn_id and the reservation does not
17366     -- have lpn reserved and p_inventory_item_id is not passed.
17367     CURSOR serials_outer_lpn_no_item IS
17368        SELECT msn.reservation_id,
17369               msn.serial_number,
17370               msn.inventory_item_id
17371        FROM   mtl_reservations mr,
17372               mtl_serial_numbers msn
17373        WHERE  mr.organization_id = p_organization_id
17374        AND    mr.reservation_id = msn.reservation_id
17375        AND    mr.lpn_id = null
17376        AND    msn.lpn_id IN (SELECT lpn_id
17377                              FROM   wms_license_plate_numbers
17378                              WHERE  outermost_lpn_id = p_outermost_lpn_id)
17379        GROUP BY msn.reservation_id, msn.serial_number, msn.inventory_item_id;
17380 
17381     -- cursor of getting reservations with serial_number reserved in the lpn
17382     -- which lpn_id = p_lpn_id and the reservation does not have lpn reserved
17383     -- and p_inventory_item_id is passed.
17384     CURSOR serials_lpn_with_item IS
17385        SELECT msn.reservation_id,
17386               msn.serial_number,
17387               msn.inventory_item_id
17388        FROM   mtl_reservations mr,
17389               mtl_serial_numbers msn
17390        WHERE  mr.organization_id = p_organization_id
17391        AND    mr.inventory_item_id = p_inventory_item_id
17392        AND    mr.reservation_id = msn.reservation_id
17393        AND    mr.lpn_id = null
17394        AND    msn.lpn_id = p_lpn_id
17395        GROUP BY msn.reservation_id, msn.serial_number, msn.inventory_item_id;
17396 
17397     -- cursor of getting reservations with serial_number reserved in the lpn
17398     -- which lpn_id = p_lpn_id and the reservation does not have lpn reserved
17399     -- and p_inventory_item_id is not passed.
17400     CURSOR serials_lpn_no_item IS
17401        SELECT msn.reservation_id,
17402               msn.serial_number,
17403               msn.inventory_item_id
17404        FROM   mtl_reservations mr,
17405               mtl_serial_numbers msn
17406        WHERE  mr.organization_id = p_organization_id
17407        AND    mr.reservation_id = msn.reservation_id
17408        AND    mr.lpn_id = null
17409        AND    msn.lpn_id = p_lpn_id
17410        GROUP BY msn.reservation_id, msn.serial_number, msn.inventory_item_id;
17411 
17412   BEGIN
17413     IF (g_debug IS NULL) THEN
17414         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
17415     END IF;
17416 
17417     l_debug := g_debug;
17418 
17419     IF (l_debug = 1) THEN
17420         debug_print('In transfer_serial_rsv_in_LPN');
17421         debug_print('p_organization_id = ' || p_organization_id);
17422         debug_print('p_inventory_item_id = ' || p_inventory_item_id);
17423         debug_print('p_lpn_id = ' || p_lpn_id);
17424         debug_print('p_outermost_lpn_id = ' || p_outermost_lpn_id);
17425         debug_print('p_to_subinventory_code = ' || p_to_subinventory_code);
17426         debug_print('p_to_locator_id = ' || p_to_locator_id);
17427     END IF;
17428 
17429     IF (p_outermost_lpn_id is not NULL and p_lpn_id is not NULL) THEN
17430         IF (l_debug = 1) THEN
17431             debug_print('Error: both p_outermost_lpn_id and p_lpn_id are populated');
17432         END IF;
17433     ELSIF (p_outermost_lpn_id is not NULL) THEN
17434         IF (l_debug = 1) THEN
17435             debug_print('p_outermost_lpn_id is not NULL');
17436         END IF;
17437 
17438         IF (p_inventory_item_id is not NULL) THEN
17439             IF (l_debug = 1) THEN
17440                 debug_print('p_inventory_item_id is not NULL');
17441             END IF;
17442 
17443             OPEN serials_outer_lpn_with_item;
17444             FETCH serials_outer_lpn_with_item BULK COLLECT INTO l_rsv_serials_tbl;
17445             CLOSE serials_outer_lpn_with_item;
17446         ELSE
17447             IF (l_debug = 1) THEN
17448                 debug_print('p_inventory_item_id is NULL');
17449             END IF;
17450 
17451             OPEN serials_outer_lpn_no_item;
17452             FETCH serials_outer_lpn_no_item BULK COLLECT INTO l_rsv_serials_tbl;
17453             CLOSE serials_outer_lpn_no_item;
17454         END IF;
17455     ELSIF (p_lpn_id is not NULL) THEN
17456         IF (l_debug = 1) THEN
17457             debug_print('p_lpn_id is not NULL');
17458         END IF;
17459 
17460         IF (p_inventory_item_id is not NULL) THEN
17461             OPEN serials_lpn_with_item;
17462             FETCH serials_lpn_with_item BULK COLLECT INTO l_rsv_serials_tbl;
17463             CLOSE serials_lpn_with_item;
17464         ELSE
17465             OPEN serials_lpn_no_item;
17466             FETCH serials_lpn_no_item BULK COLLECT INTO l_rsv_serials_tbl;
17467             CLOSE serials_lpn_no_item;
17468         END IF;
17469     END IF;
17470 
17471     l_rsv_serials_tbl_count := l_rsv_serials_tbl.COUNT;
17472     l_index := 0;
17473 
17474     -- construct to reservation record if l_rsv_serials_tbl_count > 0
17475     IF (l_rsv_serials_tbl_count > 0) THEN
17476         l_to_rsv_rec.subinventory_code := p_to_subinventory_code;
17477         l_to_rsv_rec.locator_id := p_to_locator_id;
17478 
17479        -- construct original reservation record and serial number table
17480        FOR i in 1..l_rsv_serials_tbl_count LOOP
17481           l_index := l_index + 1;
17482 
17483           -- construct serial number table for same reservation_id
17484           l_serial_number_tbl(l_index).inventory_item_id := l_rsv_serials_tbl(i).inventory_item_id;
17485           l_serial_number_tbl(l_index).serial_number := l_rsv_serials_tbl(i).serial_number;
17486 
17487           -- need to check the next record's reservation id with current reservation_id
17488           -- or if it's the last record of the serial number table
17489           IF ((i+1 <= l_rsv_serials_tbl_count AND
17490                 l_rsv_serials_tbl(i).reservation_id <> l_rsv_serials_tbl(i+1).reservation_id)
17491                   OR i = l_rsv_serials_tbl_count) THEN
17492 
17493              -- if the current reservation_id <> next record's reservation_id,
17494              -- or it is the last record of the serial number table, then
17495              -- finished construct the serial number table, contruct original reservation record
17496              -- and call transfer_reservation API
17497 
17498              l_original_rsv_rec.reservation_id := l_rsv_serials_tbl(i).reservation_id;
17499 
17500              IF (l_debug = 1) THEN
17501                  debug_print('calling transfer_reservations');
17502                  debug_print('original rec rsv id = ' || l_original_rsv_rec.reservation_id);
17503              END IF;
17504 
17505              inv_reservation_pvt.transfer_reservation(
17506                   p_api_version_number     => 1.0
17507                 , p_init_msg_lst           => fnd_api.g_false
17508                 , x_return_status          => l_return_status
17509                 , x_msg_count              => l_msg_count
17510                 , x_msg_data               => l_msg_data
17511                 , p_original_rsv_rec       => l_original_rsv_rec
17512                 , p_to_rsv_rec             => l_to_rsv_rec
17513                 , p_original_serial_number => l_serial_number_tbl
17514                 , p_to_serial_number       => l_serial_number_tbl
17515                 , p_validation_flag        => fnd_api.g_true
17516                 , x_reservation_id         => l_reservation_id
17517                 );
17518 
17519              IF (l_return_status = fnd_api.g_ret_sts_error) THEN
17520                 IF (l_debug = 1) THEN
17521                    debug_print('Error return status from transfer_reservation');
17522                 END IF;
17523 
17524                 RAISE fnd_api.g_exc_error;
17525              ELSIF (l_return_status = fnd_api.g_ret_sts_unexp_error) THEN
17526                 IF (l_debug = 1) THEN
17527                     debug_print('Unexpected return status from transfer_reservation');
17528                 END IF;
17529 
17530                 RAISE fnd_api.g_exc_unexpected_error;
17531              END IF;
17532 
17533              -- delete the content of l_serial_number_tbl and reset the index
17534              l_serial_number_tbl.DELETE;
17535              l_index := 0;
17536           END IF;
17537        END LOOP;
17538     END IF;
17539 
17540     x_return_status := l_return_status;
17541     x_msg_count     := l_msg_count;
17542     x_msg_data      := l_msg_data;
17543 
17544   EXCEPTION
17545     WHEN fnd_api.g_exc_error THEN
17546       x_return_status  := fnd_api.g_ret_sts_error;
17547       --  Get message count and data
17548       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
17549     WHEN fnd_api.g_exc_unexpected_error THEN
17550       x_return_status  := fnd_api.g_ret_sts_unexp_error;
17551 
17552       IF (l_debug = 1) THEN
17553           debug_print('unexpected error: ' || SQLERRM);
17554       END IF;
17555 
17556       --  Get message count and data
17557       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
17558     WHEN OTHERS THEN
17559       x_return_status  := fnd_api.g_ret_sts_unexp_error;
17560 
17561       IF (l_debug = 1) THEN
17562           debug_print('others error: ' || SQLERRM);
17563       END IF;
17564 
17565       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
17566         fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
17567       END IF;
17568 
17569       --  Get message count and data
17570       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
17571   END transfer_serial_rsv_in_LPN;
17572 
17573 /*** End R12 ***/
17574 
17575 END inv_reservation_pvt;
17576