DBA Data[Home] [Help]

PACKAGE BODY: APPS.INV_RESERVATION_PVT

Source


1 PACKAGE BODY inv_reservation_pvt AS
2   /* $Header: INVRSV3B.pls 120.83.12020000.9 2013/03/13 12:07:59 avrose ship $*/
3   --
4   g_pkg_name CONSTANT VARCHAR2(30) := 'Inv_reservation_pvt';
5   g_pkg_version CONSTANT VARCHAR2(100) := '$Header: INVRSV3B.pls 120.83.12020000.9 2013/03/13 12:07:59 avrose 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   g_sch_mat_id NUMBER;  /* Added for bug 13829182 */
17 
18   --
19   -- procedure to print a message to dbms_output
20   -- disable by default since dbm_s_output.put_line is not allowed
21   PROCEDURE debug_print(p_message IN VARCHAR2, p_level IN NUMBER := 9) IS
22      --Bug: 3559328: Performance bug fix.The fnd call happens everytime
23      -- debug_print is called.
24     --l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
25   BEGIN
26     inv_log_util.TRACE(p_message, 'INV_RESERVATION_PVT', p_level);
27   END debug_print;
28 
29   --
30   PROCEDURE print_rsv_rec(p_rsv_rec IN inv_reservation_global.mtl_reservation_rec_type) IS
31     l_debug number;
32   BEGIN
33      -- Bug 2944896 -- Commenting out the parameters which may have
34      -- g_miss_char as the default value. This leads to truncation of the
35      -- log file.
36      --Bug 2955454. Removing the commented section. The values are printing
37      -- correctly even if it is g_miss_char.
38     -- Use cache to get value for l_debug
39     IF g_is_pickrelease_set IS NULL THEN
40        g_is_pickrelease_set := 2;
41        IF INV_CACHE.is_pickrelease THEN
42           g_is_pickrelease_set := 1;
43        END IF;
44     END IF;
45     IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
46        g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
47     END IF;
48 
49     l_debug := g_debug;
50     IF (l_debug = 1) THEN
51        debug_print('reservation_id              : '|| TO_CHAR(p_rsv_rec.reservation_id));
52        debug_print('requirement_date            : '|| TO_CHAR(p_rsv_rec.requirement_date, 'YYYY/MM/DD'));
53        debug_print('organization_id             : '|| TO_CHAR(p_rsv_rec.organization_id));
54        debug_print('inventory_item_id           : '|| TO_CHAR(p_rsv_rec.inventory_item_id));
55        debug_print('demand_source_type_id       : '|| TO_CHAR(p_rsv_rec.demand_source_type_id));
56        debug_print('demand_source_name          : '|| p_rsv_rec.demand_source_name);
57        debug_print('demand_source_header_id     : '|| TO_CHAR(p_rsv_rec.demand_source_header_id));
58        debug_print('demand_source_line_id       : '|| TO_CHAR(p_rsv_rec.demand_source_line_id));
59        debug_print('demand_source_line_detail   : '|| TO_CHAR(p_rsv_rec.demand_source_line_detail));
60        debug_print('primary_uom_code            : '|| p_rsv_rec.primary_uom_code);
61        debug_print('primary_uom_id              : '|| TO_CHAR(p_rsv_rec.primary_uom_id));
62        debug_print('reservation_uom_code        : '|| p_rsv_rec.reservation_uom_code);
63        debug_print('reservation_uom_id          : '|| TO_CHAR(p_rsv_rec.reservation_uom_id));
64        debug_print('secondary_uom_code          : '|| p_rsv_rec.secondary_uom_code);
65        debug_print('secondary_uom_id            : '|| TO_CHAR(p_rsv_rec.secondary_uom_id));
66        debug_print('reservation_quantity        : '|| TO_CHAR(p_rsv_rec.reservation_quantity));
67        debug_print('primary_reservation_quantity: '|| TO_CHAR(p_rsv_rec.primary_reservation_quantity));
68        debug_print('secondary_reservation_quantity: '|| TO_CHAR(p_rsv_rec.secondary_reservation_quantity));
69        debug_print('detailed_quantity: '|| TO_CHAR(p_rsv_rec.detailed_quantity));
70        debug_print('secondary_detailed_quantity: '|| TO_CHAR(p_rsv_rec.secondary_detailed_quantity));
71        debug_print('autodetail_group_id         : '|| TO_CHAR(p_rsv_rec.autodetail_group_id));
72        debug_print('external_source_code        : '|| p_rsv_rec.external_source_code);
73        debug_print('external_source_line_id     : '|| TO_CHAR(p_rsv_rec.external_source_line_id));
74        debug_print('supply_source_type_id       : '|| TO_CHAR(p_rsv_rec.supply_source_type_id));
75        debug_print('supply_source_header_id     : '|| TO_CHAR(p_rsv_rec.supply_source_header_id));
76        debug_print('supply_source_line_id       : '|| TO_CHAR(p_rsv_rec.supply_source_line_id));
77        debug_print('supply_source_name          : '|| (p_rsv_rec.supply_source_name));
78        debug_print('supply_source_line_detail   : '|| TO_CHAR(p_rsv_rec.supply_source_line_detail));
79        debug_print('revision                    : '|| p_rsv_rec.revision);
80        debug_print('subinventory_code           : '|| p_rsv_rec.subinventory_code);
81        debug_print('subinventory_id             : '|| TO_CHAR(p_rsv_rec.subinventory_id));
82        debug_print('locator_id                  : '|| TO_CHAR(p_rsv_rec.locator_id));
83        debug_print('lot_number                  : '|| p_rsv_rec.lot_number);
84        debug_print('lot_number_id               : '|| TO_CHAR(p_rsv_rec.lot_number_id));
85        debug_print('pick_slip_number            : '|| TO_CHAR(p_rsv_rec.pick_slip_number));
86        debug_print('lpn_id                      : '|| TO_CHAR(p_rsv_rec.lpn_id));
87        debug_print('ship_ready_flag             : '|| TO_CHAR(p_rsv_rec.ship_ready_flag));
88        debug_print('staged_flag                 : '|| p_rsv_rec.staged_flag);
89     END IF;
90   END print_rsv_rec;
91 
92 
93   -- helper procedure to get requested qty of wdd or order quantity from sales order
94   PROCEDURE get_requested_qty
95           ( p_demand_source_type_id     IN NUMBER
96           , p_demand_source_header_id   IN NUMBER
97           , p_demand_source_line_id     IN NUMBER
98           , p_demand_source_line_detail IN NUMBER
99           , p_project_id                IN NUMBER
100           , p_task_id                   IN NUMBER
101           , x_requested_qty             OUT NOCOPY NUMBER
102 	  , x_requested_qty2            OUT NOCOPY NUMBER
103           )
104   IS
105     l_debug         NUMBER;
106     l_requested_qty NUMBER := 0;
107     l_requested_qty2 NUMBER := 0;
108   BEGIN
109 
110     IF (g_debug IS NULL) THEN
111         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
112     END IF;
113 
114     l_debug := g_debug;
115 
116     IF (l_debug = 1) THEN
117        debug_print('In get_requested_qty');
118        debug_print('p_demand_source_type_id = ' || p_demand_source_type_id);
119        debug_print('p_demand_source_header_id = ' || p_demand_source_header_id);
120        debug_print('p_demand_source_line_id = ' || p_demand_source_line_id);
121        debug_print('p_demand_source_line_detail = ' || p_demand_source_line_detail);
122        debug_print('p_project_id = ' || p_project_id);
123        debug_print('p_task_id = ' || p_task_id);
124     END IF;
125 
126     IF (p_demand_source_line_detail IS NOT NULL) THEN
127       BEGIN
128 	 IF (l_debug = 1) THEN
129 	    debug_print('Inside source line as not null');
130 	 END IF;
131 	 SELECT   nvl(sum(requested_quantity),0) ,nvl(sum(requested_quantity2),0)
132 	   INTO   l_requested_qty ,l_requested_qty2
133 	   FROM   wsh_delivery_details
134 	   WHERE  source_line_id = p_demand_source_line_id
135 	   AND    delivery_detail_id = p_demand_source_line_detail;
136 
137       EXCEPTION
138 	 WHEN OTHERS THEN
139 	    l_requested_qty := 0;
140 	    l_requested_qty2:=0;
141 	    IF (l_debug = 1) THEN
142 	       debug_print('Exception in finding wdd');
143 	    END IF;
144       END;
145     ELSE
146       BEGIN
147 	 SELECT   ordered_quantity , ordered_quantity2
148 	   INTO   l_requested_qty ,l_requested_qty2
149 	   FROM   oe_order_lines_all
150 	   WHERE  line_id = p_demand_source_line_id
151            AND    nvl(project_id, -99) = nvl(p_project_id, -99)
152            AND    nvl(task_id, -99) = nvl(p_task_id, -99);
153 
154       EXCEPTION
155 	 WHEN no_data_found THEN
156 	    l_requested_qty := 0;
157 	    l_requested_qty2 :=0;
158 	    IF (l_debug = 1) THEN
159 	       debug_print('No order line found');
160 	    END IF;
161       END;
162     END IF;
163 
164     IF (l_debug = 1) THEN
165        debug_print('l_requested_qty = ' || l_requested_qty);
166        debug_print('l_requested_qty2 = ' || l_requested_qty2);
167     END IF;
168 
169     x_requested_qty := l_requested_qty;
170    x_requested_qty2 := l_requested_qty2;
171 
172   EXCEPTION
173     WHEN fnd_api.g_exc_error THEN
174         IF (l_debug = 1) THEN
175             debug_print('excepted error');
176         END IF;
177         x_requested_qty := 0;
178 	x_requested_qty2 := 0;
179         --
180     WHEN fnd_api.g_exc_unexpected_error THEN
181         IF (l_debug = 1) THEN
182             debug_print('unexpected error');
183         END IF;
184         x_requested_qty := 0;
185 	x_requested_qty2 := 0;
186         --
187     WHEN OTHERS THEN
188         IF (l_debug = 1) THEN
189             debug_print('others error');
190         END IF;
191         x_requested_qty := 0;
192 	x_requested_qty2 := 0;
193 
194   END get_requested_qty;
195 
196 --Bug 12978409: start
197  	   PROCEDURE get_reservation_qty_lot(
198  	             p_rsv_rec IN inv_reservation_global.mtl_reservation_rec_type,
199  	             p_reservation_qty_lot OUT NOCOPY NUMBER)
200 
201  	     IS
202  	         l_return_value BOOLEAN;
203  	         l_mo_line mtl_txn_request_lines%ROWTYPE;
204  	         l_debug  NUMBER;
205  	         is_debug Boolean;
206  	         l_lot_rsv_qty_order_uom NUMBER := 0;
207  	         l_lot_primary_rsv_qty NUMBER := 0;
208  	         l_lot_conv_factor_flag NUMBER := 0;
209                  l_fulfill_base	VARCHAR2(1) := 'P'; -- MUOM fulfillment Project
210                  l_lot_secondary_rsv_qty NUMBER := 0;-- MUOM fulfillment Project
211 
212  	         CURSOR check_if_lot_conv_exists(p_lot_number varchar2, p_inventory_item_id number, p_organization_id number)  IS
213  	         SELECT count(*)
214  	         FROM mtl_lot_uom_class_conversions
215  	         WHERE lot_number      = p_rsv_rec.lot_number
216  	         AND inventory_item_id = p_rsv_rec.inventory_item_id
217  	         AND organization_id   = p_rsv_rec.organization_id
218  	         AND (disable_date IS NULL or disable_date > sysdate);
219 
220 
221  	   BEGIN
222 
223  	       IF (g_debug IS NULL) THEN
224  	         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
225  	       END IF;
226 
227  	     l_debug := g_debug;
228 
229  	     IF (l_debug = 1) THEN
230  	        debug_print('In get_reservation_qty_lot ');
231  	     END IF;
232 
233 	-- MUOM fulfillment Project
234  	       inv_utilities.get_inv_fulfillment_base(
235                           p_source_line_id		=> p_rsv_rec.demand_source_line_id,
236                           p_demand_source_type_id	=> p_rsv_rec.demand_source_type_id,
237                           p_org_id				=> p_rsv_rec.organization_id,
238                           x_fulfillment_base		=> l_fulfill_base
239                             );
240 
241 	 IF (l_debug = 1) THEN
242             debug_print('get_reservation_qty_lot l_fulfill_base: = '||l_fulfill_base);
243           END IF ;
244 
245          IF (l_fulfill_base = 'S') THEN
246              p_reservation_qty_lot  := p_rsv_rec.secondary_reservation_quantity;
247          ELSE
248  	       p_reservation_qty_lot  := p_rsv_rec.primary_reservation_quantity;
249  	 END IF;
250          -- MUOM fulfillment Project
251 
252  	       l_return_value := inv_cache.set_item_rec( p_rsv_rec.organization_id, p_rsv_rec.inventory_item_id);
253  	       IF NOT l_return_value THEN
254  	         IF (l_debug = 1) THEN
255  	             debug_print('error occurred while setting inv_cache.set_item_rec');
256  	         END IF;
257  	         RAISE fnd_api.g_exc_unexpected_error;
258  	       End IF;
259 
260 
261  	       IF p_rsv_rec.lot_number is NULL OR
262  	       inv_cache.item_rec.lot_control_code <> 2 OR
263  	       inv_cache.item_rec.primary_uom_code = p_rsv_rec.reservation_uom_code THEN
264  	         IF (l_debug = 1) THEN
265  	               debug_print('either item is not lot controlled or or lot num is null or rsv uom is same as prim uom. return');
266  	         END IF;
267 
268  	         RETURN;
269  	       END IF;
270 
271  	          IF (l_debug = 1) THEN
272  	                 debug_print('p_rsv_rec.inventory_item_id           :' || p_rsv_rec.inventory_item_id);
273  	                 debug_print('p_rsv_rec.organization_id             :' || p_rsv_rec.organization_id);
274  	                 debug_print('inv_cache.item_rec.primary_uom_code :' || inv_cache.item_rec.primary_uom_code);
275  	                 debug_print('p_rsv_rec.reservation_uom_code :' ||p_rsv_rec.reservation_uom_code);
276  	                 debug_print('p_rsv_rec.lot_number :' ||p_rsv_rec.lot_number);
277  	         END IF;
278 
279  	                 OPEN  check_if_lot_conv_exists(p_rsv_rec.lot_number, p_rsv_rec.inventory_item_id, p_rsv_rec.organization_id);
280  	                 FETCH check_if_lot_conv_exists into l_lot_conv_factor_flag;
281  	                 CLOSE check_if_lot_conv_exists;
282 
283  	                 IF (l_debug = 1) THEN
284  	                         debug_print('l_lot_conv_factor_flag :' || l_lot_conv_factor_flag );
285  	                 END IF;
286 
287  	        IF l_lot_conv_factor_flag > 0 THEN
288                      IF (l_fulfill_base = 'S') THEN   -- MUOM fulfillment Project
289                        l_lot_rsv_qty_order_uom  := inv_convert.inv_um_convert(
290  	                             item_id          => p_rsv_rec.inventory_item_id
291  	                           , lot_number       => p_rsv_rec.lot_number
292  	                           , organization_id  => p_rsv_rec.organization_id
293  	                           , precision        => null
294  	                           , from_quantity    => p_rsv_rec.secondary_reservation_quantity
295  	                           , from_unit        => inv_cache.item_rec.secondary_uom_code
296  	                           , to_unit          => p_rsv_rec.reservation_uom_code
297  	                           , from_name        => null
298  	                           , to_name          => null
299  	                            );
300 
301  	                         IF (l_debug = 1) THEN
302  	                               debug_print('Allocated qty with lots in order uom (honoring lot conversion) when fulfilment base is S :' || l_lot_rsv_qty_order_uom);
303  	                         END IF;
304 
305  	                     l_lot_secondary_rsv_qty  := inv_convert.inv_um_convert(
306  	                             item_id          => p_rsv_rec.inventory_item_id
307  	                           , organization_id  => p_rsv_rec.organization_id
308  	                           , precision        => null
309  	                           , from_quantity => l_lot_rsv_qty_order_uom
310  	                           , from_unit      =>  p_rsv_rec.reservation_uom_code
311  	                           , to_unit          =>  inv_cache.item_rec.secondary_uom_code
312  	                           , from_name   => null
313  	                           , to_name       => null
314  	                            );
315 
316  	                       p_reservation_qty_lot :=  l_lot_secondary_rsv_qty;
317 
318  	                          IF (l_debug = 1) THEN
319  	                              debug_print('l_lot_secondary_rsv_qty when fulfilment Base is S :' ||  l_lot_secondary_rsv_qty);
320  	                         END IF;
321                      ELSE
322                        l_lot_rsv_qty_order_uom  := inv_convert.inv_um_convert(
323  	                             item_id          => p_rsv_rec.inventory_item_id
324  	                           , lot_number    => p_rsv_rec.lot_number
325  	                           , organization_id  => p_rsv_rec.organization_id
326  	                           , precision        => null
327  	                           , from_quantity   => p_rsv_rec.primary_reservation_quantity
328  	                           , from_unit        => inv_cache.item_rec.primary_uom_code
329  	                           , to_unit          => p_rsv_rec.reservation_uom_code
330  	                           , from_name   => null
331  	                           , to_name       => null
332  	                            );
333 
334  	                          IF (l_debug = 1) THEN
335  	                                 debug_print('allocated qty with lots in order uom (honoring lot conversion) :' || l_lot_rsv_qty_order_uom);
336  	                         END IF;
337 
338  	                     l_lot_primary_rsv_qty  := inv_convert.inv_um_convert(
339  	                             item_id          => p_rsv_rec.inventory_item_id
340  	                           , organization_id  => p_rsv_rec.organization_id
341  	                           , precision        => null
342  	                           , from_quantity    => l_lot_rsv_qty_order_uom
343  	                           , from_unit        =>  p_rsv_rec.reservation_uom_code
344  	                           , to_unit          =>  inv_cache.item_rec.primary_uom_code
345  	                           , from_name        => null
346  	                           , to_name          => null
347  	                            );
348 
349  	                       p_reservation_qty_lot :=  l_lot_primary_rsv_qty;
350 
351  	                          IF (l_debug = 1) THEN
352  	                                 debug_print('l_lot_primary_rsv_qty  :' ||  l_lot_primary_rsv_qty );
353  	                         END IF;
354  	                  END IF;  -- MUOM fulfillment Project
355                    END IF;
356 
357  	                 IF (l_debug = 1) THEN
358  	                      debug_print('p_reservation_qty_lot :' ||  p_reservation_qty_lot );
359  	                 END IF;
360 
361 
362  	   EXCEPTION
363  	   WHEN OTHERS THEN
364  	          IF (l_debug = 1) THEN
365  	                   debug_print('Exception Occurred at get_reservation_qty_lot');
366  	         END IF;
367 
368  	   END get_reservation_qty_lot;
369 
370  	 --Bug 12978409: end
371 
372   -- helper procedure called from update_reservation and
373   -- transfer_reservation to get available to reserve qty
374   -- for the supply source.
375   PROCEDURE get_supply_reservable_qty
376     ( x_return_status                OUT NOCOPY VARCHAR2
377       , x_msg_count                    OUT NOCOPY NUMBER
378       , x_msg_data                     OUT NOCOPY VARCHAR2
379       , p_fm_supply_source_type_id     IN NUMBER
380       , p_fm_supply_source_header_id   IN NUMBER
381       , p_fm_supply_source_line_id     IN NUMBER
382       , p_fm_supply_source_line_detail IN NUMBER
383       , p_fm_primary_reservation_qty   IN NUMBER
384       , p_to_supply_source_type_id     IN NUMBER
385       , p_to_supply_source_header_id   IN NUMBER
386       , p_to_supply_source_line_id     IN NUMBER
387       , p_to_supply_source_line_detail IN NUMBER
388       , p_to_primary_reservation_qty   IN NUMBER
389       , p_to_organization_id           IN NUMBER
390       , p_to_inventory_item_id         IN NUMBER
391       , p_to_revision                  IN VARCHAR2
392       , p_to_lot_number                IN VARCHAR2
393       , p_to_subinventory_code         IN VARCHAR2
394       , p_to_locator_id                IN NUMBER
395       , p_to_lpn_id                    IN NUMBER
396     , p_to_project_id                IN NUMBER
397     , p_to_task_id                   IN NUMBER
398     , x_reservable_qty               OUT NOCOPY NUMBER
399     , x_qty_available                OUT NOCOPY NUMBER
400     )
401     IS
402        l_debug                    NUMBER;
403        l_qty_available_to_reserve NUMBER;
404        l_qty_available            NUMBER;
405        l_reservable_qty           NUMBER;
406   BEGIN
407 
408      IF (g_debug IS NULL) THEN
409         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
410      END IF;
411 
412      l_debug := g_debug;
413 
414      IF (l_debug = 1) THEN
415 	debug_print('In get_supply_reservable_qty');
416 	debug_print('Orig supply source type id = ' || p_fm_supply_source_type_id);
417 	debug_print('Orig supply source header id = ' || p_fm_supply_source_header_id);
418 	debug_print('Orig supply source line id = ' || p_fm_supply_source_line_id);
419 	debug_print('Orig supply line detail  = ' || p_fm_supply_source_line_detail);
420 	debug_print('Orig primary qty = ' || p_fm_primary_reservation_qty);
421 	debug_print('To supply source type id = ' || p_to_supply_source_type_id);
422 	debug_print('To supply source header id = ' || p_to_supply_source_header_id);
423 	debug_print('To supply source line id = ' || p_to_supply_source_line_id);
424 	debug_print('To supply line detail = ' || p_to_supply_source_line_detail);
425 	debug_print('To primary qty = ' || p_to_primary_reservation_qty);
426      END IF;
427 
428      inv_reservation_avail_pvt.available_supply_to_reserve
429       (
430        x_return_status                 => x_return_status
431        , x_msg_count                     => x_msg_count
432        , x_msg_data                      => x_msg_data
433        , x_qty_available_to_reserve      => l_qty_available_to_reserve
434        , x_qty_available                 => l_qty_available
435        , p_organization_id               => p_to_organization_id
436        , p_item_id                       => p_to_inventory_item_id
437        , p_revision                      => p_to_revision
438        , p_lot_number                    => p_to_lot_number
439        , p_subinventory_code             => p_to_subinventory_code
440        , p_locator_id                    => p_to_locator_id
441        , p_lpn_id                        => p_to_lpn_id
442        , p_fm_supply_source_type_id      => p_fm_supply_source_type_id
443        , p_supply_source_type_id         => p_to_supply_source_type_id
444        , p_supply_source_header_id       => p_to_supply_source_header_id
445        , p_supply_source_line_id         => p_to_supply_source_line_id
446        , p_supply_source_line_detail     => Nvl(p_to_supply_source_line_detail,fnd_api.g_miss_num)
447        , p_project_id                    => p_to_project_id
448        , p_task_id                       => p_to_task_id
449        , p_api_version_number            => 1.0
450        , p_init_msg_lst                  => fnd_api.g_false
451        );
452 
453      IF (l_debug = 1) THEN
454 	debug_print('After calling available supply to reserve ' || x_return_status);
455 	debug_print('Available quantity to reserve. l_qty_available_to_reserve: ' || l_qty_available_to_reserve);
456 	debug_print('Available quantity on the document. l_qty_available: ' || l_qty_available);
457      END IF;
458 
459      --
460      IF x_return_status = fnd_api.g_ret_sts_error THEN
461 	RAISE fnd_api.g_exc_error;
462     END IF;
463 
464     --
465     IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
466        RAISE fnd_api.g_exc_unexpected_error;
467     END IF;
468 
469     IF (p_fm_supply_source_type_id = p_to_supply_source_type_id AND
470 	p_fm_supply_source_header_id = p_to_supply_source_header_id AND
471 	nvl(p_fm_supply_source_line_id, -1) = nvl(p_to_supply_source_line_id, -1) AND
472 	nvl(p_fm_supply_source_line_detail, -1) = nvl(p_to_supply_source_line_detail, -1)) THEN
473 
474        -- if supply of orig and to record is the same, we need to add the qty from orig
475        -- record to the reservable qty because we're transfering to same supply source
476        l_reservable_qty := nvl(l_qty_available_to_reserve,0) + p_fm_primary_reservation_qty;
477 
478      ELSE
479        -- if transfer reservation from other supply to receiving, the total qty in rcv
480        -- will increase, so we need to add the qty from orig record to the reservable qty
481        IF (p_to_supply_source_type_id = inv_reservation_global.g_source_type_rcv) THEN
482           l_reservable_qty := nvl(l_qty_available_to_reserve,0) + p_fm_primary_reservation_qty;
483 	ELSE
484           l_reservable_qty := nvl(l_qty_available_to_reserve,0);
485        END IF;
486 
487     END IF;
488 
489     x_reservable_qty := l_reservable_qty;
490     x_qty_available := l_qty_available;
491 
492   EXCEPTION
493      WHEN fnd_api.g_exc_error THEN
494 	x_return_status  := fnd_api.g_ret_sts_error;
495 	x_reservable_qty := 0;
496 	x_qty_available := 0;
497 	--
498      WHEN fnd_api.g_exc_unexpected_error THEN
499 	x_return_status  := fnd_api.g_ret_sts_unexp_error;
500 	x_reservable_qty := 0;
501 	x_qty_available := 0;
502 	--
503      WHEN OTHERS THEN
504 	x_return_status  := fnd_api.g_ret_sts_unexp_error;
505 	x_reservable_qty := 0;
506 	x_qty_available := 0;
507 	--
508       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
509 	 fnd_msg_pub.add_exc_msg(g_pkg_name, 'get_supply_reservable_qty');
510       END IF;
511 
512   END get_supply_reservable_qty;
513 
514   -- helper procedure called from update_reservation and
515   -- transfer_reservation to get available to reserve qty
516   -- for the demand source
517   PROCEDURE get_demand_reservable_qty
518     ( x_return_status                OUT NOCOPY VARCHAR2
519       , x_msg_count                    OUT NOCOPY NUMBER
520       , x_msg_data                     OUT NOCOPY VARCHAR2
521       , p_fm_demand_source_type_id     IN NUMBER
522       , p_fm_demand_source_header_id   IN NUMBER
523       , p_fm_demand_source_line_id     IN NUMBER
524       , p_fm_demand_source_line_detail IN NUMBER
525       , p_fm_primary_reservation_qty   IN NUMBER
526       , p_fm_secondary_reservation_qty   IN NUMBER
527       , p_to_demand_source_type_id     IN NUMBER
528       , p_to_demand_source_header_id   IN NUMBER
529       , p_to_demand_source_line_id     IN NUMBER
530       , p_to_demand_source_line_detail IN NUMBER
531       , p_to_primary_reservation_qty   IN NUMBER
532       , p_to_organization_id           IN NUMBER
533       , p_to_inventory_item_id         IN NUMBER
534       , p_to_primary_uom_code          IN VARCHAR
535       , p_to_project_id                IN NUMBER
536       , p_to_task_id                   IN NUMBER
537       , x_reservable_qty               OUT NOCOPY NUMBER
538       , x_qty_available                OUT NOCOPY NUMBER
539       , x_reservable_qty2               OUT NOCOPY NUMBER
540       , x_qty_available2                OUT NOCOPY NUMBER
541       )
542     IS
543        l_debug                    NUMBER;
544        l_reservable_qty           NUMBER;
545        l_qty_available_to_reserve NUMBER;
546        l_qty_available            NUMBER;
547        l_requested_qty            NUMBER;
548        l_reservation_margin_above NUMBER;
549 	    -- MUOM Fulfillement project
550        l_fulfill_base	VARCHAR2(1) := 'P';
551        l_qty_available_to_reserve2 NUMBER;
552        l_qty_available2           NUMBER;
553        l_reservable_qty2          NUMBER;
554        l_requested_qty2           NUMBER;
555   BEGIN
556 
557     IF (g_debug IS NULL) THEN
558        g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
559     END IF;
560 
561     l_debug := g_debug;
562 
563     IF (l_debug = 1) THEN
564        debug_print('In get_demand_reservable_qty');
565        debug_print('Orig demand source type id = ' || p_fm_demand_source_type_id);
566        debug_print('Orig demand source header id = ' || p_fm_demand_source_header_id);
567        debug_print('Orig demand source line id = ' || p_fm_demand_source_line_id);
568        debug_print('Orig demand line detail  = ' || p_fm_demand_source_line_detail);
569        debug_print('Orig primary qty = ' || p_fm_primary_reservation_qty);
570        debug_print('To demand source type id = ' || p_to_demand_source_type_id);
571        debug_print('To demand source header id = ' || p_to_demand_source_header_id);
572        debug_print('To demand source line id = ' || p_to_demand_source_line_id);
573        debug_print('To demand line detail = ' || p_to_demand_source_line_detail);
574        debug_print('To primary qty = ' || p_to_primary_reservation_qty);
575        debug_print('To primary uom code = ' || p_to_primary_uom_code);
576     END IF;
577 
578     inv_reservation_avail_pvt.available_demand_to_reserve
579       (
580        x_return_status                 => x_return_status
581        , x_msg_count                     => x_msg_count
582        , x_msg_data                      => x_msg_data
583        , x_qty_available_to_reserve      => l_qty_available_to_reserve
584        , x_qty_available                 => l_qty_available
585        , x_qty_available_to_reserve2   => l_qty_available_to_reserve2
586        , x_qty_available2                 => l_qty_available2
587        , p_organization_id               => p_to_organization_id
588        , p_item_id                       => p_to_inventory_item_id
589        , p_primary_uom_code             => p_to_primary_uom_code
590        , p_demand_source_type_id      => p_to_demand_source_type_id
591        , p_demand_source_header_id   => p_to_demand_source_header_id
592        , p_demand_source_line_id         => p_to_demand_source_line_id
593        , p_demand_source_line_detail     => Nvl(p_to_demand_source_line_detail,fnd_api.g_miss_num)
594        , p_project_id                    => p_to_project_id
595        , p_task_id                       => p_to_task_id
596        , p_api_version_number            => 1.0
597        , p_init_msg_lst                  => fnd_api.g_false
598        );
599 
600 
601     IF (l_debug = 1) THEN
602        debug_print('After calling available demand to reserve ' || x_return_status);
603        debug_print('Available quantity to reserve. l_qty_available_to_reserve: ' || l_qty_available_to_reserve);
604        debug_print('Available quantity on the document. l_qty_available: ' || l_qty_available);
605        debug_print('Available quantity to reserve. l_qty_available_to_reserve2: ' || l_qty_available_to_reserve2);
606        debug_print('Available quantity on the document. l_qty_available2: ' || l_qty_available2);
607     END IF;
608 
609     --
610     IF x_return_status = fnd_api.g_ret_sts_error THEN
611        RAISE fnd_api.g_exc_error;
612     END IF;
613 
614     --
615     IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
616        RAISE fnd_api.g_exc_unexpected_error;
617     END IF;
618 
619     IF (p_fm_demand_source_type_id = p_to_demand_source_type_id AND
620 	p_fm_demand_source_header_id = p_to_demand_source_header_id AND
621 	nvl(p_fm_demand_source_line_id, -1) = nvl(p_to_demand_source_line_id, -1) AND
622        nvl(p_fm_demand_source_line_detail, -1) = nvl(p_to_demand_source_line_detail, -1)) THEN
623 
624        -- if demand of orig and to record is the same, we need to add the qty from orig
625        -- record to the reservable qty because we're transfering to same demand source
626        l_reservable_qty := l_qty_available_to_reserve + p_fm_primary_reservation_qty;
627        l_reservable_qty2 := l_qty_available_to_reserve2 +nvl(p_fm_secondary_reservation_qty,0);
628 
629      ELSE
630 
631        --for sales order or internal order, the reservable qty is the minimum of
632        --requested qty of the line detail/line and the reservable qty of the sales order line
633        IF (p_to_demand_source_type_id in (inv_reservation_global.g_source_type_oe,
634                                           inv_reservation_global.g_source_type_internal_ord,
635 					  inv_reservation_global.g_source_type_rma) AND
636 	   p_fm_demand_source_type_id = p_to_demand_source_type_id AND
637 	   p_fm_demand_source_header_id = p_to_demand_source_header_id AND
638 	   nvl(p_fm_demand_source_line_id, -1) = nvl(p_to_demand_source_line_id, -1) AND
639           nvl(p_fm_demand_source_line_detail, -1) <> nvl(p_to_demand_source_line_detail, -1)) THEN
640 
641           get_requested_qty
642 	    ( p_demand_source_type_id     => p_to_demand_source_type_id
643 	      , p_demand_source_header_id   => p_to_demand_source_header_id
644 	      , p_demand_source_line_id     => p_to_demand_source_line_id
645 	      , p_demand_source_line_detail => p_to_demand_source_line_detail
646 	      , p_project_id                => p_to_project_id
647 	      , p_task_id                   => p_to_task_id
648 	      , x_requested_qty           => l_requested_qty
649 	      , x_requested_qty2         => l_requested_qty2
650 	      );
651 
652 
653           IF (l_debug = 1) THEN
654              debug_print('l_requested_qty = ' || l_requested_qty);
655              debug_print('reservable qty = ' || (l_qty_available_to_reserve + p_fm_primary_reservation_qty));
656 	     debug_print('l_requested_qty2 = ' || l_requested_qty2);
657              debug_print('reservable qty2 = ' || (l_qty_available_to_reserve2 + nvl(p_fm_secondary_reservation_qty,0)));
658           END IF;
659 
660           IF (nvl(l_requested_qty, 0) < (l_qty_available_to_reserve + p_fm_primary_reservation_qty)) THEN
661              l_reservable_qty := nvl(l_requested_qty, 0);
662 	   ELSE
663              l_reservable_qty := l_qty_available_to_reserve + p_fm_primary_reservation_qty;
664           END IF;
665 
666 	  IF (nvl(l_requested_qty2, 0) < (l_qty_available_to_reserve2 + p_fm_secondary_reservation_qty)) THEN
667              l_reservable_qty2 := nvl(l_requested_qty2, 0);
668 	     ELSE
669              l_reservable_qty2 := l_qty_available_to_reserve2 + p_fm_secondary_reservation_qty;
670           END IF;
671 
672 	ELSE
673           l_reservable_qty := l_qty_available_to_reserve;
674 	  l_reservable_qty2 := l_qty_available_to_reserve2;
675 
676        END IF;
677 
678     END IF;
679 
680     IF (p_to_demand_source_type_id in (inv_reservation_global.g_source_type_oe,
681                                           inv_reservation_global.g_source_type_internal_ord,
682 				       inv_reservation_global.g_source_type_rma)) THEN
683 
684        IF NOT (lot_divisible
685 	       (p_inventory_item_id => p_to_inventory_item_id,
686 		p_organization_id => p_to_organization_id)) THEN
687 	  get_ship_qty_tolerance
688 	    (
689 	     p_api_version_number          =>  1.0
690 	     , p_init_msg_lst              =>  fnd_api.g_false
691 	     , x_return_status             => x_return_status
692 	     , x_msg_count                 => x_msg_count
693 	     , x_msg_data                  => x_msg_data
694 	     , p_demand_type_id            => p_to_demand_source_type_id
695 	     , p_demand_header_id          => p_to_demand_source_header_id
696 	     , p_demand_line_id            => p_to_demand_source_line_id
697 	     , x_reservation_margin_above  => l_reservation_margin_above);
698 
699 	  IF (l_debug = 1) THEN
700 	     debug_print('Inside is lot indivisible');
701 	  END IF;
702 
703 	  IF (l_debug = 1) THEN
704 	     debug_print('After calling get_ship_qty_tolerance ' || x_return_status);
705 	     debug_print('Reservation margin above ' || l_reservation_margin_above);
706 	  END IF;
707 
708 	  --
709 	  IF x_return_status = fnd_api.g_ret_sts_error THEN
710 	     RAISE fnd_api.g_exc_error;
711 	  END IF;
712 
713 	  --
714 	  IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
715 	     RAISE fnd_api.g_exc_unexpected_error;
716 	  END IF;
717 
718 	  --MUOM Fulfillment Project
719 	  inv_utilities.get_inv_fulfillment_base(
720 			p_source_line_id		=> p_to_demand_source_line_id,
721 			p_demand_source_type_id => p_to_demand_source_type_id,
722 			p_org_id				=> p_to_organization_id,
723 			x_fulfillment_base		=> l_fulfill_base
724 			);
725 
726 	  IF l_fulfill_base <> 'S' THEN
727 	     l_reservable_qty := l_reservable_qty + l_reservation_margin_above;
728 	     l_qty_available := l_qty_available + l_reservation_margin_above;
729 	   ELSE
730 		  l_reservable_qty2 := l_reservable_qty2 + l_reservation_margin_above;
731 		  l_qty_available2 := l_qty_available2 + l_reservation_margin_above;
732 	  END IF;
733        END IF;
734     END IF;
735 
736     IF (l_debug = 1) THEN
737        debug_print('reservable qty = ' || l_reservable_qty);
738        debug_print('reservable qty2 = ' || l_reservable_qty2);
739     END IF;
740 
741     x_reservable_qty := l_reservable_qty;
742     x_qty_available := l_qty_available;
743     x_reservable_qty2 := l_reservable_qty2;
744     x_qty_available2 := l_qty_available2;
745 
746   EXCEPTION
747      WHEN fnd_api.g_exc_error THEN
748 	x_return_status  := fnd_api.g_ret_sts_error;
749 	x_reservable_qty := 0;
750 	x_qty_available  := 0;
751 	x_reservable_qty2 := 0;
752 	x_qty_available2 := 0;
753 	--
754     WHEN fnd_api.g_exc_unexpected_error THEN
755        x_return_status  := fnd_api.g_ret_sts_unexp_error;
756        x_reservable_qty := 0;
757        x_qty_available  := 0;
758        x_reservable_qty2 := 0;
759        x_qty_available2 := 0;
760        --
761      WHEN OTHERS THEN
762 	x_return_status  := fnd_api.g_ret_sts_unexp_error;
763 	x_reservable_qty := 0;
764 	x_qty_available  := 0;
765 	x_reservable_qty2 := 0;
766 	x_qty_available2 := 0;
767 	--
768 	IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
769 	   fnd_msg_pub.add_exc_msg(g_pkg_name, 'get_demand_reservable_qty');
770       END IF;
771 
772   END get_demand_reservable_qty;
773 
774  --This procedure will compute the shipping tolerance for sales order,
775   -- internal order and RMA and return the quantity in the primary uom of
776   -- item
777   PROCEDURE get_ship_qty_tolerance
778   (
779      p_api_version_number            IN  NUMBER
780    , p_init_msg_lst                  IN  VARCHAR2  Default Fnd_API.G_False
781    , x_return_status                 OUT NOCOPY VARCHAR2
782    , x_msg_count                     OUT NOCOPY NUMBER
783    , x_msg_data                      OUT NOCOPY VARCHAR2
784    , p_demand_type_id                IN  NUMBER
785    , p_demand_header_id              IN  NUMBER
786    , p_demand_line_id                IN  NUMBER
787    , x_reservation_margin_above      OUT NOCOPY NUMBER                   -- INVCONV
788    ) is
789       l_api_version_number  CONSTANT NUMBER       := 1.0;
790       l_api_name            CONSTANT VARCHAR2(30) := 'get_ship_qty_tolerance';
791       l_debug NUMBER;
792       l_primary_uom_code  	     	VARCHAR2(3);
793       l_ship_tolerance_above            NUMBER;          -- INVCONV
794       l_line_rec_inventory_item_id      oe_order_lines_all.inventory_item_id%TYPE;
795       l_line_rec_ordered_quantity       oe_order_lines_all.ordered_quantity%TYPE;
796       l_line_rec_order_quantity_uom     oe_order_lines_all.order_quantity_uom%TYPE;
797       l_line_rec_org_id                 oe_order_lines_all.org_id%TYPE;
798       l_ordered_quantity_primary_uom NUMBER;
799 
800 	  -- MUOM Fulfillment Project
801 	  l_fulfill_base	VARCHAR2(1) := 'P';
802 	  l_line_rec_ordered_quantity2  oe_order_lines_all.ordered_quantity2%TYPE;
803   BEGIN
804 
805      IF (g_debug IS NULL) THEN
806 	g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
807      END IF;
808 
809      l_debug := g_debug;
810 
811      -- Initialize return status
812      x_return_status := fnd_api.g_ret_sts_success;
813 
814      --  Standard call to check for call compatibility
815      IF NOT fnd_api.compatible_api_call(l_api_version_number
816 					, p_api_version_number
817 					, l_api_name
818 					, G_PKG_NAME
819 					) THEN
820 	RAISE fnd_api.g_exc_unexpected_error;
821      END IF;
822      --
823      --  Initialize message list.
824      IF fnd_api.to_boolean(p_init_msg_lst) THEN
825 	fnd_msg_pub.initialize;
826      END IF;
827 
828      IF p_demand_type_id IN (inv_reservation_global.g_source_type_oe,
829 			     inv_reservation_global.g_source_type_internal_ord,
830 			     inv_reservation_global.g_source_type_rma) then
831 
832 	--INVCONV - Retrieve ship tolerance above for lot indivisible scenarios
833 	BEGIN
834 	   SELECT inventory_item_id, ordered_quantity, order_quantity_uom, ship_from_org_id,
835 	     ship_tolerance_above , ordered_quantity2
836 	     INTO l_line_rec_inventory_item_id,
837 	     l_line_rec_ordered_quantity,
838 	     l_line_rec_order_quantity_uom,
839 	     l_line_rec_org_id,
840 	     l_ship_tolerance_above,
841 	     l_line_rec_ordered_quantity2
842 	     FROM oe_order_lines_all
843 	     WHERE line_id = p_demand_line_id;
844 	EXCEPTION
845 	   WHEN no_data_found THEN
846 	      IF (l_debug =1) THEN
847 		 debug_print('could not find the record for sales order line ' || p_demand_line_id);
848 	      END IF;
849 	END;
850 
851 	-- Get primary UOM of the item
852 	BEGIN
853 	   select primary_uom_code
854 	     into l_primary_uom_code
855 	     from mtl_system_items
856 	     where organization_id   = l_line_rec_org_id
857 	     and   inventory_item_id = l_line_rec_inventory_item_id;
858 	 EXCEPTION
859 	   WHEN no_data_found THEN
860 	      IF (l_debug =1) THEN
861 	debug_print('could not find the record for the item id ' || l_line_rec_inventory_item_id || ' Org ' ||l_line_rec_org_id );
862 	      END IF;
863 	END;
864 	--MUOM Fulfillment Project
865 	inv_utilities.get_inv_fulfillment_base(
866 			p_source_line_id		=> p_demand_line_id,
867 			p_demand_source_type_id => p_demand_type_id,
868 			p_org_id				=> l_line_rec_org_id,
869 			x_fulfillment_base		=> l_fulfill_base
870 			);
871 	IF l_fulfill_base <> 'S' THEN
872 	 IF l_primary_uom_code = l_line_rec_order_quantity_uom THEN
873 	   x_reservation_margin_above := l_line_rec_ordered_quantity * NVL(l_ship_tolerance_above,0) / 100;
874 	 IF (l_debug =1) THEN
875 	       debug_print('quantity no convert');
876 	       debug_print('margin above :' || x_reservation_margin_above);
877 	   END IF;
878 	  ELSE -- the uoms are different. convert the order qty into primary
879 	   -- uom of the ite,
880 
881 	   -- Convert order quantity into primary uom code
882 	   l_ordered_quantity_primary_uom :=
883 	     inv_convert.inv_um_convert
884 	     (
885 	      l_line_rec_inventory_item_id,
886 	      NULL,
887 	      l_line_rec_ordered_quantity,
888 	      l_line_rec_order_quantity_uom,
889 	      l_primary_uom_code,
890 	      NULL,
891 	      NULL);
892 
893 	   x_reservation_margin_above := l_ordered_quantity_primary_uom *
894 	     NVL(l_ship_tolerance_above,0) / 100;
895 
896 	   IF (l_debug =1) THEN
897 	      debug_print('quantity after convert :' || l_ordered_quantity_primary_uom);
898 	      debug_print('margin above :' || x_reservation_margin_above);
899 	   END IF;
900 
901 	 END IF;
902 	ELSE
903 		x_reservation_margin_above := l_line_rec_ordered_quantity2 *
904 			NVL(l_ship_tolerance_above,0) / 100;
905 
906 		IF (l_debug =1) THEN
907 			debug_print('margin above for fulfillment S:' || x_reservation_margin_above);
908 		END IF;
909 
910 	END IF;
911 
912      END IF;
913      x_return_status := fnd_api.g_ret_sts_success;
914 
915   EXCEPTION
916 
917      WHEN fnd_api.g_exc_error THEN
918         x_return_status := fnd_api.g_ret_sts_error;
919 
920         --  Get message count and data
921         fnd_msg_pub.count_and_get
922           (  p_count => x_msg_count
923 	     , p_data  => x_msg_data
924 	     , p_encoded => 'F'
925            );
926 
927      WHEN fnd_api.g_exc_unexpected_error THEN
928         x_return_status := fnd_api.g_ret_sts_unexp_error ;
929 
930         --  Get message count and data
931         fnd_msg_pub.count_and_get
932           (  p_count  => x_msg_count
933 	     , p_data   => x_msg_data
934 	     , p_encoded => 'F'
935 	     );
936 
937      WHEN OTHERS THEN
938         x_return_status := fnd_api.g_ret_sts_unexp_error ;
939 
940         IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
941           THEN
942            fnd_msg_pub.add_exc_msg
943              (  g_pkg_name
944 		, l_api_name
945 		);
946         END IF;
947 
948         --  Get message count and data
949         fnd_msg_pub.count_and_get
950           (  p_count  => x_msg_count
951 	     , p_data   => x_msg_data
952 	     , p_encoded => 'F'
953              );
954   END Get_Ship_qty_Tolerance ;
955 
956   --
957   -- Procedure
958   --   convert_quantity
959   -- Description
960   --   convert quantity from reservation uom to primary uom or
961   --   convert quantity from primary uom to reservation uom
962   --   and store the quantity in the corresponding field in
963   --   the record
964   -- Requirement
965   --   In px_rsv_rec, inventory_item_id must be valid;
966   --   either primary_uom_code, primary_reservation_quantity
967   --   or reservation_uom_code, reservation_quantity must be not null and valid
968   PROCEDURE convert_quantity(x_return_status OUT NOCOPY VARCHAR2, px_rsv_rec IN OUT NOCOPY inv_reservation_global.mtl_reservation_rec_type) IS
969     l_return_status    VARCHAR2(1)                                     := fnd_api.g_ret_sts_success;
970     --l_rsv_rec          inv_reservation_global.mtl_reservation_rec_type;
971     l_primary_uom_code VARCHAR2(3);
972     l_tmp_secondary_quantity     NUMBER                                          := NULL; -- INVCONV
973     l_tmp_quantity     NUMBER                                          := NULL;
974     l_tracking_quantity_ind VARCHAR2(30);   --INVCONV
975    -- MUOM Fulfillment project
976     l_fulfill_base   VARCHAR2(1) := 'P';
977   BEGIN
978     --l_rsv_rec        := px_rsv_rec;
979 
980       --
981     -- INVCONV - Retrieve secondary uom
982     IF px_rsv_rec.primary_uom_code IS NULL or px_rsv_rec.secondary_uom_code IS NULL THEN
983        SELECT primary_uom_code, secondary_uom_code,tracking_quantity_ind
984          INTO px_rsv_rec.primary_uom_code, px_rsv_rec.secondary_uom_code,l_tracking_quantity_ind
985          FROM mtl_system_items
986         WHERE inventory_item_id = px_rsv_rec.inventory_item_id
987           AND organization_id = px_rsv_rec.organization_id;
988     END IF;
989 
990     /* it's possible that Secondary UOM is defined for the item but the item is tracked only in Primary */
991     IF(l_tracking_quantity_ind <> 'PS')  THEN --INVCONV
992       px_rsv_rec.secondary_uom_code := NULL;
993       px_rsv_rec.secondary_reservation_quantity := NULL; /*Bug#8444523*/
994     END IF;
995 
996     --
997     --
998     -- convert reservation quantity in reservation uom
999     -- to primary quantity in primary uom if needed
1000     IF px_rsv_rec.primary_reservation_quantity IS NULL THEN
1001       -- get primary uom code for the item and org
1002       --
1003       -- compute the primary quantity
1004       -- INVCONV - upgrade call to inv_um_convert
1005       l_tmp_quantity                          := inv_convert.inv_um_convert(
1006                                                    item_id                      => px_rsv_rec.inventory_item_id
1007                                                  , lot_number                   => px_rsv_rec.lot_number
1008                                                  , organization_id              => px_rsv_rec.organization_id
1009                                                  , PRECISION                    => NULL -- use default precision
1010                                                  , from_quantity                => px_rsv_rec.reservation_quantity
1011                                                  , from_unit                    => px_rsv_rec.reservation_uom_code
1012                                                  , to_unit                      => px_rsv_rec.primary_uom_code
1013                                                  , from_name                    => NULL -- from uom name
1014                                                  , to_name                      => NULL -- to uom name
1015                                                  );
1016 
1017       IF l_tmp_quantity = -99999 THEN
1018         -- conversion failed
1019         fnd_message.set_name('INV', 'CAN-NOT-CONVERT-TO-PRIMARY-UOM');
1020         fnd_msg_pub.ADD;
1021         RAISE fnd_api.g_exc_error;
1022       END IF;
1023 
1024       --
1025       px_rsv_rec.primary_reservation_quantity  := l_tmp_quantity;
1026     --
1027     END IF;
1028 
1029 --MUOM Fulfillment Project
1030     inv_utilities.get_inv_fulfillment_base(
1031                p_source_line_id        => px_rsv_rec.demand_source_line_id,
1032                p_demand_source_type_id => px_rsv_rec.demand_source_type_id,
1033                p_org_id                => px_rsv_rec.organization_id,
1034                x_fulfillment_base      => l_fulfill_base
1035               );
1036 
1037     --
1038     -- convert reservation quantity in primary_uom_code
1039     -- to reservation quantity in reservation uom code if needed
1040     IF px_rsv_rec.reservation_uom_code IS NULL THEN
1041       px_rsv_rec.reservation_uom_code  := px_rsv_rec.primary_uom_code;
1042       px_rsv_rec.reservation_quantity  := px_rsv_rec.primary_reservation_quantity;
1043     ELSIF  px_rsv_rec.reservation_quantity IS NULL
1044            AND px_rsv_rec.primary_reservation_quantity IS NOT NULL THEN
1045       -- if reservation_quantity is missing or both
1046       -- reservation_quantity and primary_reservation_quantity are
1047       -- present, we will compute the reservation quantity based
1048       -- on the primary reservation quantity
1049       -- Bug 1914778 - changed ELSIF so that reservation_quantity
1050       -- is calculated again only if it is null
1051       -- Bug 2116332 - only call inv_convert if UOMs are different
1052       IF px_rsv_rec.primary_uom_code = px_rsv_rec.reservation_uom_code THEN
1053         l_tmp_quantity  := px_rsv_rec.primary_reservation_quantity;
1054 	   -- MUOM Fulfillment project
1055       ELSIF l_fulfill_base = 'S' and px_rsv_rec.demand_source_type_id in (2,8)
1056             and px_rsv_rec.reservation_uom_code = px_rsv_rec.secondary_uom_code
1057             and px_rsv_rec.secondary_reservation_quantity is not null THEN
1058         l_tmp_quantity  := px_rsv_rec.secondary_reservation_quantity;
1059       ELSIF l_fulfill_base = 'S' and px_rsv_rec.demand_source_type_id in (2,8)
1060             and px_rsv_rec.secondary_reservation_quantity is not null THEN
1061         l_tmp_quantity  := inv_convert.inv_um_convert(
1062                              item_id                      => px_rsv_rec.inventory_item_id
1063                            , lot_number                   => px_rsv_rec.lot_number
1064                            , organization_id              => px_rsv_rec.organization_id
1065                            , PRECISION                    => NULL -- use default precision
1066                            , from_quantity                => px_rsv_rec.secondary_reservation_quantity
1067                            , from_unit                    => px_rsv_rec.secondary_uom_code
1068                            , to_unit                      => px_rsv_rec.reservation_uom_code
1069                            , from_name                    => NULL -- from uom name
1070                            , to_name                      => NULL -- to uom name
1071                            );
1072 
1073         IF l_tmp_quantity = -99999 THEN
1074           -- conversion failed
1075           fnd_message.set_name('INV', 'CAN-NOT-CONVERT-TO-RSV-UOM');
1076           fnd_msg_pub.ADD;
1077           RAISE fnd_api.g_exc_error;
1078         END IF;
1079       ELSE
1080         -- INVCONV upgrade inv_um_convert call
1081         l_tmp_quantity  := inv_convert.inv_um_convert(
1082                              item_id                      => px_rsv_rec.inventory_item_id
1083                            , lot_number                   => px_rsv_rec.lot_number
1084                            , organization_id              => px_rsv_rec.organization_id
1085                            , PRECISION                    => NULL -- use default precision
1086                            , from_quantity                => px_rsv_rec.primary_reservation_quantity
1087                            , from_unit                    => px_rsv_rec.primary_uom_code
1088                            , to_unit                      => px_rsv_rec.reservation_uom_code
1089                            , from_name                    => NULL -- from uom name
1090                            , to_name                      => NULL -- to uom name
1091                            );
1092 
1093         IF l_tmp_quantity = -99999 THEN
1094           -- conversion failed
1095           fnd_message.set_name('INV', 'CAN-NOT-CONVERT-TO-RSV-UOM');
1096           fnd_msg_pub.ADD;
1097           RAISE fnd_api.g_exc_error;
1098         END IF;
1099       END IF;
1100 
1101       --
1102       px_rsv_rec.reservation_quantity  := l_tmp_quantity;
1103     END IF;
1104 
1105      -- INVCONV BEGIN
1106          -- If dual control and secondary quantity is missing, calculate it
1107          IF px_rsv_rec.secondary_uom_code IS NOT NULL AND
1108            px_rsv_rec.secondary_reservation_quantity IS NULL THEN
1109            l_tmp_secondary_quantity  := inv_convert.inv_um_convert(
1110                                          item_id                      => px_rsv_rec.inventory_item_id
1111                                        , lot_number                   => px_rsv_rec.lot_number
1112                                        , organization_id              => px_rsv_rec.organization_id
1113                                        , PRECISION                    => NULL -- use default precision
1114                                        , from_quantity                => px_rsv_rec.primary_reservation_quantity
1115                                       , from_unit                    => px_rsv_rec.primary_uom_code
1116                                       , to_unit                      => px_rsv_rec.secondary_uom_code
1117                                       , from_name                    => NULL -- from uom name
1118                                        , to_name                      => NULL -- to uom name
1119                                        );
1120 
1121           IF l_tmp_secondary_quantity = -99999 THEN
1122                -- conversion failed
1123                fnd_message.set_name('INV', 'CAN-NOT-CONVERT-TO-SECOND-UOM'); -- INVCONV NEW MESSAGE
1124                fnd_msg_pub.ADD;
1125             RAISE fnd_api.g_exc_error;
1126            END IF;
1127            px_rsv_rec.secondary_reservation_quantity  := l_tmp_secondary_quantity; -- INVCONV
1128          END IF;
1129 
1130          -- secondary_detailed_quantity could also be missing:
1131          IF px_rsv_rec.secondary_uom_code IS NOT NULL AND
1132            px_rsv_rec.secondary_detailed_quantity IS NULL THEN
1133            IF ( NVL(px_rsv_rec.detailed_quantity,0) = 0   OR
1134                 px_rsv_rec.detailed_quantity = fnd_api.g_miss_num ) THEN --Bug#7482123.
1135                  px_rsv_rec.secondary_detailed_quantity := 0;
1136            ELSE -- convert from detailed_quantity to secondary_detailed_quantity
1137              l_tmp_secondary_quantity  := inv_convert.inv_um_convert(
1138                                          item_id                      => px_rsv_rec.inventory_item_id
1139                                        , lot_number                   => px_rsv_rec.lot_number
1140                                        , organization_id              => px_rsv_rec.organization_id
1141                                        , PRECISION                    => NULL -- use default precision
1142                                        , from_quantity                => px_rsv_rec.detailed_quantity
1143                                    , from_unit                    => px_rsv_rec.primary_uom_code
1144                                        , to_unit                      => px_rsv_rec.secondary_uom_code
1145                                        , from_name                    => NULL -- from uom name
1146                                        , to_name                      => NULL -- to uom name
1147                                        );
1148 
1149              IF l_tmp_secondary_quantity = -99999 THEN
1150                -- conversion failed
1151                fnd_message.set_name('INV', 'CAN-NOT-CONVERT-TO-SECOND-UOM'); -- INVCONV NEW MESSAGE
1152                fnd_msg_pub.ADD;
1153                RAISE fnd_api.g_exc_error;
1154              END IF;
1155              px_rsv_rec.secondary_detailed_quantity  := l_tmp_secondary_quantity; -- INVCONV
1156            END IF;
1157          END IF;
1158          -- INVCONV END
1159     --
1160     --px_rsv_rec       := l_rsv_rec;
1161     --
1162     x_return_status  := l_return_status;
1163   --
1164   EXCEPTION
1165     WHEN fnd_api.g_exc_error THEN
1166       x_return_status  := fnd_api.g_ret_sts_error;
1167     --
1168     WHEN fnd_api.g_exc_unexpected_error THEN
1169       x_return_status  := fnd_api.g_ret_sts_unexp_error;
1170     --
1171     WHEN OTHERS THEN
1172       x_return_status  := fnd_api.g_ret_sts_unexp_error;
1173 
1174       --
1175       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
1176         fnd_msg_pub.add_exc_msg(g_pkg_name, 'Convert_Quantity');
1177       END IF;
1178   END convert_quantity;
1179 
1180   --
1181   -- Description
1182   --   convert missing value in the input record
1183   --   to null in the output record. if the value of
1184   --   field in the input record is not missing, it
1185   --   would be copied to the output record
1186   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
1187   BEGIN
1188     IF p_rsv_rec.reservation_id <> fnd_api.g_miss_num THEN
1189       x_rsv_rec.reservation_id  := p_rsv_rec.reservation_id;
1190     ELSE
1191       x_rsv_rec.reservation_id  := NULL;
1192     END IF;
1193 
1194     --
1195     IF p_rsv_rec.requirement_date <> fnd_api.g_miss_date THEN
1196       x_rsv_rec.requirement_date  := p_rsv_rec.requirement_date;
1197     ELSE
1198       x_rsv_rec.requirement_date  := NULL;
1199     END IF;
1200 
1201     --
1202     IF p_rsv_rec.organization_id <> fnd_api.g_miss_num THEN
1203       x_rsv_rec.organization_id  := p_rsv_rec.organization_id;
1204     ELSE
1205       x_rsv_rec.organization_id  := NULL;
1206     END IF;
1207 
1208     --
1209     IF p_rsv_rec.inventory_item_id <> fnd_api.g_miss_num THEN
1210       x_rsv_rec.inventory_item_id  := p_rsv_rec.inventory_item_id;
1211     ELSE
1212       x_rsv_rec.inventory_item_id  := NULL;
1213     END IF;
1214 
1215     --
1216     IF p_rsv_rec.demand_source_type_id <> fnd_api.g_miss_num THEN
1217       x_rsv_rec.demand_source_type_id  := p_rsv_rec.demand_source_type_id;
1218     ELSE
1219       x_rsv_rec.demand_source_type_id  := NULL;
1220     END IF;
1221 
1222     --
1223     IF p_rsv_rec.demand_source_name <> fnd_api.g_miss_char THEN
1224       x_rsv_rec.demand_source_name  := p_rsv_rec.demand_source_name;
1225     ELSE
1226       x_rsv_rec.demand_source_name  := NULL;
1227     END IF;
1228 
1229     --
1230     IF p_rsv_rec.demand_source_delivery <> fnd_api.g_miss_num THEN
1231       x_rsv_rec.demand_source_delivery  := p_rsv_rec.demand_source_delivery;
1232     ELSE
1233       x_rsv_rec.demand_source_delivery  := NULL;
1234     END IF;
1235 
1236     --
1237     IF p_rsv_rec.demand_source_header_id <> fnd_api.g_miss_num THEN
1238       x_rsv_rec.demand_source_header_id  := p_rsv_rec.demand_source_header_id;
1239     ELSE
1240       x_rsv_rec.demand_source_header_id  := NULL;
1241     END IF;
1242 
1243     --
1244     IF p_rsv_rec.demand_source_line_id <> fnd_api.g_miss_num THEN
1245       x_rsv_rec.demand_source_line_id  := p_rsv_rec.demand_source_line_id;
1246     ELSE
1247       x_rsv_rec.demand_source_line_id  := NULL;
1248     END IF;
1249 
1250     --
1251     IF p_rsv_rec.primary_uom_code <> fnd_api.g_miss_char THEN
1252       x_rsv_rec.primary_uom_code  := p_rsv_rec.primary_uom_code;
1253     ELSE
1254       x_rsv_rec.primary_uom_code  := NULL;
1255     END IF;
1256 
1257     --
1258     IF p_rsv_rec.primary_uom_id <> fnd_api.g_miss_num THEN
1259       x_rsv_rec.primary_uom_id  := p_rsv_rec.primary_uom_id;
1260     ELSE
1261       x_rsv_rec.primary_uom_id  := NULL;
1262     END IF;
1263 
1264      -- INVCONV BEGIN
1265          IF p_rsv_rec.secondary_uom_code <> fnd_api.g_miss_char THEN
1266            x_rsv_rec.secondary_uom_code  := p_rsv_rec.secondary_uom_code;
1267          ELSE
1268            x_rsv_rec.secondary_uom_code  := NULL;
1269          END IF;
1270 
1271         --
1272          IF p_rsv_rec.secondary_uom_id <> fnd_api.g_miss_num THEN
1273            x_rsv_rec.secondary_uom_id  := p_rsv_rec.secondary_uom_id;
1274          ELSE
1275            x_rsv_rec.secondary_uom_id  := NULL;
1276          END IF;
1277      -- INVCONV END
1278 
1279     --
1280     IF p_rsv_rec.reservation_uom_code <> fnd_api.g_miss_char THEN
1281       x_rsv_rec.reservation_uom_code  := p_rsv_rec.reservation_uom_code;
1282     ELSE
1283       x_rsv_rec.reservation_uom_code  := NULL;
1284     END IF;
1285 
1286     --
1287     IF p_rsv_rec.reservation_uom_id <> fnd_api.g_miss_num THEN
1288       x_rsv_rec.reservation_uom_id  := p_rsv_rec.reservation_uom_id;
1289     ELSE
1290       x_rsv_rec.reservation_uom_id  := NULL;
1291     END IF;
1292 
1293     --
1294     IF p_rsv_rec.reservation_quantity <> fnd_api.g_miss_num THEN
1295       x_rsv_rec.reservation_quantity  := p_rsv_rec.reservation_quantity;
1296     ELSE
1297       x_rsv_rec.reservation_quantity  := NULL;
1298     END IF;
1299 
1300     --
1301     IF p_rsv_rec.primary_reservation_quantity <> fnd_api.g_miss_num THEN
1302       x_rsv_rec.primary_reservation_quantity  := p_rsv_rec.primary_reservation_quantity;
1303     ELSE
1304       x_rsv_rec.primary_reservation_quantity  := NULL;
1305     END IF;
1306 
1307      -- INVCONV BEGIN
1308          IF p_rsv_rec.secondary_reservation_quantity <> fnd_api.g_miss_num THEN
1309            x_rsv_rec.secondary_reservation_quantity  := p_rsv_rec.secondary_reservation_quantity;
1310          ELSE
1311            x_rsv_rec.secondary_reservation_quantity  := NULL;
1312          END IF;
1313      -- INVCONV END
1314 
1315     --
1316     IF p_rsv_rec.detailed_quantity <> fnd_api.g_miss_num THEN
1317       x_rsv_rec.detailed_quantity  := p_rsv_rec.detailed_quantity;
1318     ELSE
1319       x_rsv_rec.detailed_quantity  := NULL;
1320     END IF;
1321 
1322 
1323      -- INVCONV BEGIN
1324          IF p_rsv_rec.secondary_detailed_quantity <> fnd_api.g_miss_num THEN
1325            x_rsv_rec.secondary_detailed_quantity  := p_rsv_rec.secondary_detailed_quantity;
1326          ELSE
1327            x_rsv_rec.secondary_detailed_quantity  := NULL;
1328          END IF;
1329      -- INVCONV END
1330     --
1331     IF p_rsv_rec.autodetail_group_id <> fnd_api.g_miss_num THEN
1332       x_rsv_rec.autodetail_group_id  := p_rsv_rec.autodetail_group_id;
1333     ELSE
1334       x_rsv_rec.autodetail_group_id  := NULL;
1335     END IF;
1336 
1337     --
1338     IF p_rsv_rec.external_source_code <> fnd_api.g_miss_char THEN
1339       x_rsv_rec.external_source_code  := p_rsv_rec.external_source_code;
1340     ELSE
1341       x_rsv_rec.external_source_code  := NULL;
1342     END IF;
1343 
1344     --
1345     IF p_rsv_rec.external_source_line_id <> fnd_api.g_miss_num THEN
1346       x_rsv_rec.external_source_line_id  := p_rsv_rec.external_source_line_id;
1347     ELSE
1348       x_rsv_rec.external_source_line_id  := NULL;
1349     END IF;
1350 
1351     --
1352     IF p_rsv_rec.supply_source_type_id <> fnd_api.g_miss_num THEN
1353       x_rsv_rec.supply_source_type_id  := p_rsv_rec.supply_source_type_id;
1354     ELSE
1355       x_rsv_rec.supply_source_type_id  := NULL;
1356     END IF;
1357 
1358     --
1359     IF p_rsv_rec.supply_source_header_id <> fnd_api.g_miss_num THEN
1360       x_rsv_rec.supply_source_header_id  := p_rsv_rec.supply_source_header_id;
1361     ELSE
1362       x_rsv_rec.supply_source_header_id  := NULL;
1363     END IF;
1364 
1365     --
1366     IF p_rsv_rec.supply_source_line_id <> fnd_api.g_miss_num THEN
1367       x_rsv_rec.supply_source_line_id  := p_rsv_rec.supply_source_line_id;
1368     ELSE
1369       x_rsv_rec.supply_source_line_id  := NULL;
1370     END IF;
1371 
1372     --
1373     IF p_rsv_rec.supply_source_line_detail <> fnd_api.g_miss_num THEN
1374       x_rsv_rec.supply_source_line_detail  := p_rsv_rec.supply_source_line_detail;
1375     ELSE
1376       x_rsv_rec.supply_source_line_detail  := NULL;
1377     END IF;
1378 
1379     --
1380     IF p_rsv_rec.supply_source_name <> fnd_api.g_miss_char THEN
1381       x_rsv_rec.supply_source_name  := p_rsv_rec.supply_source_name;
1382     ELSE
1383       x_rsv_rec.supply_source_name  := NULL;
1384     END IF;
1385 
1386     --
1387     IF p_rsv_rec.revision <> fnd_api.g_miss_char THEN
1388       x_rsv_rec.revision  := p_rsv_rec.revision;
1389     ELSE
1390       x_rsv_rec.revision  := NULL;
1391     END IF;
1392 
1393     --
1394     IF p_rsv_rec.subinventory_code <> fnd_api.g_miss_char THEN
1395       x_rsv_rec.subinventory_code  := p_rsv_rec.subinventory_code;
1396     ELSE
1397       x_rsv_rec.subinventory_code  := NULL;
1398     END IF;
1399 
1400     --
1401     IF p_rsv_rec.subinventory_id <> fnd_api.g_miss_num THEN
1402       x_rsv_rec.subinventory_id  := p_rsv_rec.subinventory_id;
1403     ELSE
1404       x_rsv_rec.subinventory_id  := NULL;
1405     END IF;
1406 
1407     --
1408     IF p_rsv_rec.locator_id <> fnd_api.g_miss_num THEN
1409       x_rsv_rec.locator_id  := p_rsv_rec.locator_id;
1410     ELSE
1411       x_rsv_rec.locator_id  := NULL;
1412     END IF;
1413 
1414     --
1415     IF p_rsv_rec.lot_number <> fnd_api.g_miss_char THEN
1416       x_rsv_rec.lot_number  := p_rsv_rec.lot_number;
1417     ELSE
1418       x_rsv_rec.lot_number  := NULL;
1419     END IF;
1420 
1421     --
1422     IF p_rsv_rec.lot_number_id <> fnd_api.g_miss_num THEN
1423       x_rsv_rec.lot_number_id  := p_rsv_rec.lot_number_id;
1424     ELSE
1425       x_rsv_rec.lot_number_id  := NULL;
1426     END IF;
1427 
1428     --
1429     IF p_rsv_rec.pick_slip_number <> fnd_api.g_miss_num THEN
1430       x_rsv_rec.pick_slip_number  := p_rsv_rec.pick_slip_number;
1431     ELSE
1432       x_rsv_rec.pick_slip_number  := NULL;
1433     END IF;
1434 
1435     --
1436     IF p_rsv_rec.attribute_category <> fnd_api.g_miss_char THEN
1437       x_rsv_rec.attribute_category  := p_rsv_rec.attribute_category;
1438     ELSE
1439       x_rsv_rec.attribute_category  := NULL;
1440     END IF;
1441 
1442     --
1443     IF p_rsv_rec.attribute1 <> fnd_api.g_miss_char THEN
1444       x_rsv_rec.attribute1  := p_rsv_rec.attribute1;
1445     ELSE
1446       x_rsv_rec.attribute1  := NULL;
1447     END IF;
1448 
1449     IF p_rsv_rec.attribute2 <> fnd_api.g_miss_char THEN
1450       x_rsv_rec.attribute2  := p_rsv_rec.attribute2;
1451     ELSE
1452       x_rsv_rec.attribute2  := NULL;
1453     END IF;
1454 
1455     IF p_rsv_rec.attribute3 <> fnd_api.g_miss_char THEN
1456       x_rsv_rec.attribute3  := p_rsv_rec.attribute3;
1457     ELSE
1458       x_rsv_rec.attribute3  := NULL;
1459     END IF;
1460 
1461     IF p_rsv_rec.attribute4 <> fnd_api.g_miss_char THEN
1462       x_rsv_rec.attribute4  := p_rsv_rec.attribute4;
1463     ELSE
1464       x_rsv_rec.attribute4  := NULL;
1465     END IF;
1466 
1467     IF p_rsv_rec.attribute5 <> fnd_api.g_miss_char THEN
1468       x_rsv_rec.attribute5  := p_rsv_rec.attribute5;
1469     ELSE
1470       x_rsv_rec.attribute5  := NULL;
1471     END IF;
1472 
1473     IF p_rsv_rec.attribute6 <> fnd_api.g_miss_char THEN
1474       x_rsv_rec.attribute6  := p_rsv_rec.attribute6;
1475     ELSE
1476       x_rsv_rec.attribute6  := NULL;
1477     END IF;
1478 
1479     IF p_rsv_rec.attribute7 <> fnd_api.g_miss_char THEN
1480       x_rsv_rec.attribute7  := p_rsv_rec.attribute7;
1481     ELSE
1482       x_rsv_rec.attribute7  := NULL;
1483     END IF;
1484 
1485     IF p_rsv_rec.attribute8 <> fnd_api.g_miss_char THEN
1486       x_rsv_rec.attribute8  := p_rsv_rec.attribute8;
1487     ELSE
1488       x_rsv_rec.attribute8  := NULL;
1489     END IF;
1490 
1491     IF p_rsv_rec.attribute9 <> fnd_api.g_miss_char THEN
1492       x_rsv_rec.attribute9  := p_rsv_rec.attribute9;
1493     ELSE
1494       x_rsv_rec.attribute9  := NULL;
1495     END IF;
1496 
1497     IF p_rsv_rec.attribute10 <> fnd_api.g_miss_char THEN
1498       x_rsv_rec.attribute10  := p_rsv_rec.attribute10;
1499     ELSE
1500       x_rsv_rec.attribute10  := NULL;
1501     END IF;
1502 
1503     IF p_rsv_rec.attribute11 <> fnd_api.g_miss_char THEN
1504       x_rsv_rec.attribute11  := p_rsv_rec.attribute11;
1505     ELSE
1506       x_rsv_rec.attribute11  := NULL;
1507     END IF;
1508 
1509     IF p_rsv_rec.attribute12 <> fnd_api.g_miss_char THEN
1510       x_rsv_rec.attribute12  := p_rsv_rec.attribute12;
1511     ELSE
1512       x_rsv_rec.attribute12  := NULL;
1513     END IF;
1514 
1515     IF p_rsv_rec.attribute13 <> fnd_api.g_miss_char THEN
1516       x_rsv_rec.attribute13  := p_rsv_rec.attribute13;
1517     ELSE
1518       x_rsv_rec.attribute13  := NULL;
1519     END IF;
1520 
1521     IF p_rsv_rec.attribute14 <> fnd_api.g_miss_char THEN
1522       x_rsv_rec.attribute14  := p_rsv_rec.attribute14;
1523     ELSE
1524       x_rsv_rec.attribute14  := NULL;
1525     END IF;
1526 
1527     IF p_rsv_rec.attribute15 <> fnd_api.g_miss_char THEN
1528       x_rsv_rec.attribute15  := p_rsv_rec.attribute15;
1529     ELSE
1530       x_rsv_rec.attribute15  := NULL;
1531     END IF;
1532 
1533     IF p_rsv_rec.ship_ready_flag <> fnd_api.g_miss_num THEN
1534       x_rsv_rec.ship_ready_flag  := p_rsv_rec.ship_ready_flag;
1535     ELSE
1536       x_rsv_rec.ship_ready_flag  := NULL;
1537     END IF;
1538 
1539     IF p_rsv_rec.staged_flag <> fnd_api.g_miss_char THEN
1540       x_rsv_rec.staged_flag  := p_rsv_rec.staged_flag;
1541     ELSE
1542       x_rsv_rec.staged_flag  := NULL;
1543     END IF;
1544 
1545     IF p_rsv_rec.lpn_id <> fnd_api.g_miss_num THEN
1546       x_rsv_rec.lpn_id  := p_rsv_rec.lpn_id;
1547     ELSE
1548       x_rsv_rec.lpn_id  := NULL;
1549     END IF;
1550 
1551     /**** {{ R12 Enhanced reservations code changes. Adding new columns to
1552     -- convert_missing_to_null API}}****/
1553     IF p_rsv_rec.crossdock_flag <> fnd_api.g_miss_char THEN
1554        x_rsv_rec.crossdock_flag  := p_rsv_rec.crossdock_flag;
1555      ELSE
1556        x_rsv_rec.crossdock_flag  := NULL;
1557     END IF;
1558 
1559     IF p_rsv_rec.crossdock_criteria_id <> fnd_api.g_miss_num THEN
1560        x_rsv_rec.crossdock_criteria_id  := p_rsv_rec.crossdock_criteria_id;
1561      ELSE
1562        x_rsv_rec.crossdock_criteria_id  := NULL;
1563     END IF;
1564 
1565     IF p_rsv_rec.demand_source_line_detail <> fnd_api.g_miss_num THEN
1566        x_rsv_rec.demand_source_line_detail  := p_rsv_rec.demand_source_line_detail;
1567      ELSE
1568        x_rsv_rec.demand_source_line_detail  := NULL;
1569     END IF;
1570 
1571     IF p_rsv_rec.serial_reservation_quantity <> fnd_api.g_miss_num THEN
1572        x_rsv_rec.serial_reservation_quantity  := p_rsv_rec.serial_reservation_quantity;
1573      ELSE
1574        x_rsv_rec.serial_reservation_quantity  := NULL;
1575     END IF;
1576 
1577     IF p_rsv_rec.supply_receipt_date <> fnd_api.g_miss_date THEN
1578        x_rsv_rec.supply_receipt_date  := p_rsv_rec.supply_receipt_date;
1579      ELSE
1580        x_rsv_rec.supply_receipt_date  := NULL;
1581     END IF;
1582 
1583      IF p_rsv_rec.demand_ship_date <> fnd_api.g_miss_date THEN
1584        x_rsv_rec.demand_ship_date  := p_rsv_rec.demand_ship_date;
1585      ELSE
1586        x_rsv_rec.demand_ship_date  := NULL;
1587      END IF;
1588 
1589      IF p_rsv_rec.project_id <> fnd_api.g_miss_num THEN
1590        x_rsv_rec.project_id  := p_rsv_rec.project_id;
1591      ELSE
1592        x_rsv_rec.project_id  := NULL;
1593      END IF;
1594 
1595      IF p_rsv_rec.task_id <> fnd_api.g_miss_num THEN
1596        x_rsv_rec.task_id  := p_rsv_rec.task_id;
1597      ELSE
1598        x_rsv_rec.task_id  := NULL;
1599     END IF;
1600 
1601  /*** End R12 ***/
1602   END;
1603 
1604   --
1605   -- Description
1606   -- return true if any attribute in the input record is missing
1607   -- else return false
1608   FUNCTION check_missing(p_rsv_rec IN OUT NOCOPY inv_reservation_global.mtl_reservation_rec_type, x_what_field OUT NOCOPY VARCHAR2)
1609     RETURN BOOLEAN IS
1610   BEGIN
1611     x_what_field  := NULL;
1612 
1613     IF p_rsv_rec.requirement_date = fnd_api.g_miss_date THEN
1614       x_what_field  := 'requirement_date';
1615     END IF;
1616 
1617     IF p_rsv_rec.organization_id = fnd_api.g_miss_num THEN
1618       x_what_field  := 'organization_id';
1619     END IF;
1620 
1621     IF p_rsv_rec.inventory_item_id = fnd_api.g_miss_num THEN
1622       x_what_field  := 'inventory_item_id';
1623     END IF;
1624 
1625     IF p_rsv_rec.demand_source_type_id = fnd_api.g_miss_num THEN
1626       x_what_field  := 'demand_source_type_id';
1627     END IF;
1628 
1629     IF p_rsv_rec.demand_source_name = fnd_api.g_miss_char THEN
1630       x_what_field  := 'demand_source_name';
1631     END IF;
1632 
1633     IF p_rsv_rec.demand_source_delivery = fnd_api.g_miss_num THEN
1634       x_what_field  := 'demand_source_delivery';
1635     END IF;
1636 
1637     IF p_rsv_rec.demand_source_header_id = fnd_api.g_miss_num THEN
1638       x_what_field  := 'demand_source_header_id';
1639     END IF;
1640 
1641     IF p_rsv_rec.demand_source_line_id = fnd_api.g_miss_num THEN
1642       x_what_field  := 'demand_source_line_id';
1643     END IF;
1644 
1645     IF p_rsv_rec.primary_uom_code = fnd_api.g_miss_char THEN
1646       x_what_field  := 'primary_uom_code';
1647     END IF;
1648 
1649     IF p_rsv_rec.primary_uom_id = fnd_api.g_miss_num THEN
1650       x_what_field  := 'primary_uom_id';
1651     END IF;
1652 
1653      -- INVCONV BEGIN
1654          IF p_rsv_rec.secondary_uom_code = fnd_api.g_miss_char THEN
1655            x_what_field  := 'secondary_uom_code';
1656          END IF;
1657 
1658         IF p_rsv_rec.secondary_uom_id = fnd_api.g_miss_num THEN
1659            x_what_field  := 'secondary_uom_id';
1660          END IF;
1661          -- INVCONV END
1662 
1663     IF p_rsv_rec.reservation_uom_code = fnd_api.g_miss_char THEN
1664       x_what_field  := 'reservation_uom_code';
1665     END IF;
1666 
1667     IF p_rsv_rec.reservation_uom_id = fnd_api.g_miss_num THEN
1668       x_what_field  := 'reservation_uom_id';
1669     END IF;
1670 
1671     IF p_rsv_rec.reservation_quantity = fnd_api.g_miss_num THEN
1672       x_what_field  := 'reservation_quantity';
1673     END IF;
1674 
1675     IF p_rsv_rec.primary_reservation_quantity = fnd_api.g_miss_num THEN
1676       x_what_field  := 'primary_reservation_quantity';
1677     END IF;
1678 
1679     -- INVCONV BEGIN
1680     IF p_rsv_rec.secondary_reservation_quantity = fnd_api.g_miss_num THEN
1681       x_what_field  := 'secondary_reservation_quantity';
1682     END IF;
1683     -- INVCONV END
1684 
1685     IF p_rsv_rec.detailed_quantity = fnd_api.g_miss_num THEN
1686       x_what_field  := 'detailed_quantity';
1687     END IF;
1688 
1689     -- INVCONV BEGIN
1690     IF p_rsv_rec.secondary_detailed_quantity = fnd_api.g_miss_num THEN
1691       x_what_field  := 'secondary_detailed_quantity';
1692     END IF;
1693     -- INVCONV BEGIN
1694 
1695     IF p_rsv_rec.autodetail_group_id = fnd_api.g_miss_num THEN
1696       x_what_field  := 'autodetail_group_id';
1697     END IF;
1698 
1699     IF p_rsv_rec.external_source_code = fnd_api.g_miss_char THEN
1700       x_what_field  := 'external_source_code';
1701     END IF;
1702 
1703     IF p_rsv_rec.external_source_line_id = fnd_api.g_miss_num THEN
1704       x_what_field  := 'external_source_line_id';
1705     END IF;
1706 
1707     IF p_rsv_rec.supply_source_type_id = fnd_api.g_miss_num THEN
1708       x_what_field  := 'supply_source_type_id';
1709     END IF;
1710 
1711     IF p_rsv_rec.supply_source_header_id = fnd_api.g_miss_num THEN
1712       x_what_field  := 'supply_source_header_id';
1713     END IF;
1714 
1715     IF p_rsv_rec.supply_source_line_id = fnd_api.g_miss_num THEN
1716       x_what_field  := 'supply_source_line_id';
1717     END IF;
1718 
1719     IF p_rsv_rec.supply_source_name = fnd_api.g_miss_char THEN
1720       x_what_field  := 'supply_source_name';
1721     END IF;
1722 
1723     IF p_rsv_rec.supply_source_line_detail = fnd_api.g_miss_num THEN
1724       x_what_field  := 'supply_source_line_detail';
1725     END IF;
1726 
1727     IF p_rsv_rec.revision = fnd_api.g_miss_char THEN
1728       x_what_field  := 'revision';
1729     END IF;
1730 
1731     IF p_rsv_rec.subinventory_code = fnd_api.g_miss_char THEN
1732       x_what_field  := 'subinventory_code';
1733     END IF;
1734 
1735     IF p_rsv_rec.subinventory_id = fnd_api.g_miss_num THEN
1736       x_what_field  := 'subinventory_id';
1737     END IF;
1738 
1739     IF p_rsv_rec.locator_id = fnd_api.g_miss_num THEN
1740       x_what_field  := 'locator_id';
1741     END IF;
1742 
1743     IF p_rsv_rec.lot_number = fnd_api.g_miss_char THEN
1744       x_what_field  := 'lot_number';
1745     END IF;
1746 
1747     IF p_rsv_rec.lot_number_id = fnd_api.g_miss_num THEN
1748       x_what_field  := 'lot_number_id';
1749     END IF;
1750 
1751     IF p_rsv_rec.pick_slip_number = fnd_api.g_miss_num THEN
1752       x_what_field  := 'pick_slip_number';
1753     END IF;
1754 
1755     IF p_rsv_rec.lpn_id = fnd_api.g_miss_num THEN
1756       x_what_field  := 'lpn_id';
1757     END IF;
1758 
1759     IF p_rsv_rec.attribute_category = fnd_api.g_miss_char THEN
1760       x_what_field  := 'attribute_category';
1761     END IF;
1762 
1763     IF p_rsv_rec.attribute1 = fnd_api.g_miss_char THEN
1764       x_what_field  := 'attribute1';
1765     END IF;
1766 
1767     IF p_rsv_rec.attribute2 = fnd_api.g_miss_char THEN
1768       x_what_field  := 'attribute2';
1769     END IF;
1770 
1771     IF p_rsv_rec.attribute3 = fnd_api.g_miss_char THEN
1772       x_what_field  := 'attribute3';
1773     END IF;
1774 
1775     IF p_rsv_rec.attribute4 = fnd_api.g_miss_char THEN
1776       x_what_field  := 'attribute4';
1777     END IF;
1778 
1779     IF p_rsv_rec.attribute5 = fnd_api.g_miss_char THEN
1780       x_what_field  := 'attribute5';
1781     END IF;
1782 
1783     IF p_rsv_rec.attribute6 = fnd_api.g_miss_char THEN
1784       x_what_field  := 'attribute6';
1785     END IF;
1786 
1787     IF p_rsv_rec.attribute7 = fnd_api.g_miss_char THEN
1788       x_what_field  := 'attribute7';
1789     END IF;
1790 
1791     IF p_rsv_rec.attribute8 = fnd_api.g_miss_char THEN
1792       x_what_field  := 'attribute8';
1793     END IF;
1794 
1795     IF p_rsv_rec.attribute9 = fnd_api.g_miss_char THEN
1796       x_what_field  := 'attribute9';
1797     END IF;
1798 
1799     IF p_rsv_rec.attribute10 = fnd_api.g_miss_char THEN
1800       x_what_field  := 'attribute10';
1801     END IF;
1802 
1803     IF p_rsv_rec.attribute11 = fnd_api.g_miss_char THEN
1804       x_what_field  := 'attribute11';
1805     END IF;
1806 
1807     IF p_rsv_rec.attribute12 = fnd_api.g_miss_char THEN
1808       x_what_field  := 'attribute12';
1809     END IF;
1810 
1811     IF p_rsv_rec.attribute13 = fnd_api.g_miss_char THEN
1812       x_what_field  := 'attribute13';
1813     END IF;
1814 
1815     IF p_rsv_rec.attribute14 = fnd_api.g_miss_char THEN
1816       x_what_field  := 'attribute14';
1817     END IF;
1818 
1819     IF p_rsv_rec.attribute15 = fnd_api.g_miss_char THEN
1820       x_what_field  := 'attribute15';
1821     END IF;
1822 
1823     IF p_rsv_rec.attribute15 = fnd_api.g_miss_char THEN
1824       x_what_field  := 'attribute15';
1825     END IF;
1826 
1827     IF p_rsv_rec.ship_ready_flag = fnd_api.g_miss_num THEN
1828       x_what_field  := 'ship_ready_flag';
1829     END IF;
1830 
1831     IF p_rsv_rec.staged_flag = fnd_api.g_miss_char  THEN
1832        p_rsv_rec.staged_flag := NULL;
1833     END IF;
1834 
1835     /**** {{ R12 Enhanced reservations code changes. Adding new columns to
1836     -- check_missing API }}****/
1837     IF p_rsv_rec.crossdock_flag = fnd_api.g_miss_char  THEN
1838        p_rsv_rec.crossdock_flag := NULL;
1839     END IF;
1840 
1841     IF p_rsv_rec.crossdock_criteria_id = fnd_api.g_miss_num THEN
1842        p_rsv_rec.crossdock_criteria_id := NULL;
1843     END IF;
1844 
1845     IF p_rsv_rec.demand_source_line_detail = fnd_api.g_miss_num  THEN
1846        p_rsv_rec.demand_source_line_detail := NULL;
1847     END IF;
1848 
1849     IF p_rsv_rec.serial_reservation_quantity = fnd_api.g_miss_num  THEN
1850        p_rsv_rec.serial_reservation_quantity := NULL;
1851     END IF;
1852 
1853     IF p_rsv_rec.supply_receipt_date = fnd_api.g_miss_date  THEN
1854        p_rsv_rec.supply_receipt_date := NULL;
1855     END IF;
1856 
1857     IF p_rsv_rec.demand_ship_date = fnd_api.g_miss_date  THEN
1858        p_rsv_rec.demand_ship_date := NULL;
1859     END IF;
1860 
1861     IF p_rsv_rec.project_id = fnd_api.g_miss_num  THEN
1862        p_rsv_rec.project_id := NULL;
1863     END IF;
1864 
1865     IF p_rsv_rec.task_id = fnd_api.g_miss_num  THEN
1866        p_rsv_rec.task_id := NULL;
1867     END IF;
1868 
1869     /*** End R12 ***/
1870     debug_print('Check Missing parameter ' || x_what_field);
1871 
1872     IF x_what_field IS NOT NULL THEN
1873       RETURN TRUE;
1874     ELSE
1875       RETURN FALSE;
1876     END IF;
1877   END check_missing;
1878 
1879   --
1880   -- Description
1881   --   construct an output record based on the input records in the
1882   --   following way
1883   --   1. if the value of a field in p_to_rsv_rec is not missing or
1884   --      is null, copy the value to the corresponding field
1885   --      in x_to_rsv_rec
1886   --   2. else, copy the value of in the corresponding field in
1887   --      p_original_rsv_rec to the corresponding field in x_to_rsv_rec
1888   PROCEDURE construct_to_reservation_row(
1889     p_original_rsv_rec IN     inv_reservation_global.mtl_reservation_rec_type
1890   , p_to_rsv_rec       IN     inv_reservation_global.mtl_reservation_rec_type
1891   , x_to_rsv_rec       OUT    NOCOPY inv_reservation_global.mtl_reservation_rec_type
1892   ) IS
1893 
1894   l_debug        NUMBER;
1895   -- MUOM Fulfillment project
1896   l_fulfill_base   VARCHAR2(1) := 'P';
1897   BEGIN
1898 
1899     IF (g_debug is NULL) THEN
1900         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
1901     END IF;
1902 
1903     l_debug := g_debug;
1904 
1905     IF p_to_rsv_rec.reservation_id <> fnd_api.g_miss_num
1906        OR p_to_rsv_rec.reservation_id IS NULL THEN
1907       x_to_rsv_rec.reservation_id  := p_to_rsv_rec.reservation_id;
1908     ELSE
1909       x_to_rsv_rec.reservation_id  := p_original_rsv_rec.reservation_id;
1910     END IF;
1911 
1912     --
1913     IF p_to_rsv_rec.requirement_date <> fnd_api.g_miss_date
1914        OR p_to_rsv_rec.requirement_date IS NULL THEN
1915       x_to_rsv_rec.requirement_date  := p_to_rsv_rec.requirement_date;
1916     ELSE
1917       x_to_rsv_rec.requirement_date  := p_original_rsv_rec.requirement_date;
1918     END IF;
1919 
1920     --
1921     IF p_to_rsv_rec.organization_id <> fnd_api.g_miss_num
1922        OR p_to_rsv_rec.organization_id IS NULL THEN
1923       x_to_rsv_rec.organization_id  := p_to_rsv_rec.organization_id;
1924     ELSE
1925       x_to_rsv_rec.organization_id  := p_original_rsv_rec.organization_id;
1926     END IF;
1927 
1928     --
1929     IF p_to_rsv_rec.inventory_item_id <> fnd_api.g_miss_num
1930        OR p_to_rsv_rec.inventory_item_id IS NULL THEN
1931       x_to_rsv_rec.inventory_item_id  := p_to_rsv_rec.inventory_item_id;
1932     ELSE
1933       x_to_rsv_rec.inventory_item_id  := p_original_rsv_rec.inventory_item_id;
1934     END IF;
1935 
1936     --
1937     IF p_to_rsv_rec.demand_source_type_id <> fnd_api.g_miss_num
1938        OR p_to_rsv_rec.demand_source_type_id IS NULL THEN
1939       x_to_rsv_rec.demand_source_type_id  := p_to_rsv_rec.demand_source_type_id;
1940     ELSE
1941       x_to_rsv_rec.demand_source_type_id  := p_original_rsv_rec.demand_source_type_id;
1942     END IF;
1943 
1944     --
1945     IF p_to_rsv_rec.demand_source_name <> fnd_api.g_miss_char
1946        OR p_to_rsv_rec.demand_source_name IS NULL THEN
1947       x_to_rsv_rec.demand_source_name  := p_to_rsv_rec.demand_source_name;
1948     ELSE
1949       x_to_rsv_rec.demand_source_name  := p_original_rsv_rec.demand_source_name;
1950     END IF;
1951 
1952     --
1953     IF p_to_rsv_rec.demand_source_delivery <> fnd_api.g_miss_num
1954        OR p_to_rsv_rec.demand_source_delivery IS NULL THEN
1955       x_to_rsv_rec.demand_source_delivery  := p_to_rsv_rec.demand_source_delivery;
1956     ELSE
1957       x_to_rsv_rec.demand_source_delivery  := p_original_rsv_rec.demand_source_delivery;
1958     END IF;
1959 
1960     --
1961     IF p_to_rsv_rec.demand_source_header_id <> fnd_api.g_miss_num
1962        OR p_to_rsv_rec.demand_source_header_id IS NULL THEN
1963       x_to_rsv_rec.demand_source_header_id  := p_to_rsv_rec.demand_source_header_id;
1964     ELSE
1965       x_to_rsv_rec.demand_source_header_id  := p_original_rsv_rec.demand_source_header_id;
1966     END IF;
1967 
1968     --
1969     IF p_to_rsv_rec.demand_source_line_id <> fnd_api.g_miss_num
1970        OR p_to_rsv_rec.demand_source_line_id IS NULL THEN
1971       x_to_rsv_rec.demand_source_line_id  := p_to_rsv_rec.demand_source_line_id;
1972     ELSE
1973       x_to_rsv_rec.demand_source_line_id  := p_original_rsv_rec.demand_source_line_id;
1974     END IF;
1975 
1976     --
1977     IF p_to_rsv_rec.primary_uom_code <> fnd_api.g_miss_char
1978        OR p_to_rsv_rec.primary_uom_code IS NULL THEN
1979       x_to_rsv_rec.primary_uom_code  := p_to_rsv_rec.primary_uom_code;
1980     ELSE
1981       x_to_rsv_rec.primary_uom_code  := p_original_rsv_rec.primary_uom_code;
1982     END IF;
1983 
1984     --
1985     IF p_to_rsv_rec.primary_uom_id <> fnd_api.g_miss_num
1986        OR p_to_rsv_rec.primary_uom_id IS NULL THEN
1987       x_to_rsv_rec.primary_uom_id  := p_to_rsv_rec.primary_uom_id;
1988     ELSE
1989       x_to_rsv_rec.primary_uom_id  := p_original_rsv_rec.primary_uom_id;
1990     END IF;
1991 
1992     -- INVCONV BEGIN
1993     IF p_to_rsv_rec.secondary_uom_code <> fnd_api.g_miss_char
1994       OR p_to_rsv_rec.secondary_uom_code IS NULL THEN
1995       x_to_rsv_rec.secondary_uom_code  := p_to_rsv_rec.secondary_uom_code;
1996     ELSE
1997       x_to_rsv_rec.secondary_uom_code  := p_original_rsv_rec.secondary_uom_code;
1998     END IF;
1999 
2000     --
2001     IF p_to_rsv_rec.secondary_uom_id <> fnd_api.g_miss_num
2002         OR p_to_rsv_rec.secondary_uom_id IS NULL THEN
2003       x_to_rsv_rec.secondary_uom_id  := p_to_rsv_rec.secondary_uom_id;
2004     ELSE
2005       x_to_rsv_rec.secondary_uom_id  := p_original_rsv_rec.secondary_uom_id;
2006     END IF;
2007     -- INVCONV END
2008     --
2009     IF p_to_rsv_rec.reservation_uom_code <> fnd_api.g_miss_char
2010        OR p_to_rsv_rec.reservation_uom_code IS NULL THEN
2011       x_to_rsv_rec.reservation_uom_code  := p_to_rsv_rec.reservation_uom_code;
2012     ELSE
2013       x_to_rsv_rec.reservation_uom_code  := p_original_rsv_rec.reservation_uom_code;
2014     END IF;
2015 
2016     --
2017     IF p_to_rsv_rec.reservation_uom_id <> fnd_api.g_miss_num
2018        OR p_to_rsv_rec.reservation_uom_id IS NULL THEN
2019       x_to_rsv_rec.reservation_uom_id  := p_to_rsv_rec.reservation_uom_id;
2020     ELSE
2021       x_to_rsv_rec.reservation_uom_id  := p_original_rsv_rec.reservation_uom_id;
2022     END IF;
2023 
2024     --
2025     IF  p_to_rsv_rec.primary_reservation_quantity = fnd_api.g_miss_num
2026         AND p_to_rsv_rec.reservation_quantity = fnd_api.g_miss_num THEN
2027       -- neither primary quantity or reservaton quantity is
2028       -- specified for the to row
2029       x_to_rsv_rec.primary_reservation_quantity  := p_original_rsv_rec.primary_reservation_quantity;
2030       x_to_rsv_rec.reservation_quantity          := p_original_rsv_rec.reservation_quantity;
2031     ELSIF p_to_rsv_rec.primary_reservation_quantity <> fnd_api.g_miss_num
2032           OR p_to_rsv_rec.primary_reservation_quantity IS NULL THEN
2033       -- primary_reservation_quantity is specified for the to row
2034       -- here null is considered as a value
2035       x_to_rsv_rec.primary_reservation_quantity  := p_to_rsv_rec.primary_reservation_quantity;
2036 
2037       --ADM bug 9959125, retaining the reservation qty passed for OPM batch reservations, in order to avoid re-conversion.
2038       IF (l_debug = 1) THEN
2039         debug_print('p_to_rsv_rec.demand_source_type_id = '||p_to_rsv_rec.demand_source_type_id);
2040         debug_print('p_to_rsv_rec.reservation_quantity = '||p_to_rsv_rec.reservation_quantity);
2041         debug_print('x_to_rsv_rec.reservation_uom_code = '||x_to_rsv_rec.reservation_uom_code);
2042         debug_print('x_to_rsv_rec.primary_uom_code = '||x_to_rsv_rec.primary_uom_code);
2043         debug_print('p_original_rsv_rec.primary_reservation_quantity = '||p_original_rsv_rec.primary_reservation_quantity);
2044         debug_print('p_to_rsv_rec.primary_reservation_quantity = '||p_to_rsv_rec.primary_reservation_quantity);
2045         debug_print('p_to_rsv_rec.reservation_quantity = '||p_to_rsv_rec.reservation_quantity);
2046         debug_print('p_original_rsv_rec.reservation_quantity = '||p_original_rsv_rec.reservation_quantity);
2047         debug_print('p_to_rsv_rec.secondary_reservation_quantity = '||p_to_rsv_rec.secondary_reservation_quantity);
2048       END IF;
2049 
2050 	    --MUOM Fulfillment Project
2051       inv_utilities.get_inv_fulfillment_base(
2052                p_source_line_id    => p_to_rsv_rec.demand_source_line_id,
2053                p_demand_source_type_id =>p_to_rsv_rec.demand_source_type_id,
2054                p_org_id                => p_to_rsv_rec.organization_id,
2055                x_fulfillment_base      => l_fulfill_base
2056       );
2057 
2058       IF p_to_rsv_rec.demand_source_type_id = 5 and p_to_rsv_rec.reservation_quantity is not null and x_to_rsv_rec.reservation_uom_code is not null
2059          and x_to_rsv_rec.reservation_uom_code <> x_to_rsv_rec.primary_uom_code and p_original_rsv_rec.primary_reservation_quantity = p_to_rsv_rec.primary_reservation_quantity
2060          and p_original_rsv_rec.reservation_quantity = p_to_rsv_rec.reservation_quantity and p_original_rsv_rec.demand_source_type_id = p_to_rsv_rec.demand_source_type_id THEN
2061          x_to_rsv_rec.reservation_quantity := p_to_rsv_rec.reservation_quantity;
2062       ELSIF p_to_rsv_rec.demand_source_type_id = 5 and p_to_rsv_rec.reservation_quantity is not null and x_to_rsv_rec.reservation_uom_code is not null
2063             and x_to_rsv_rec.reservation_uom_code <> x_to_rsv_rec.primary_uom_code and x_to_rsv_rec.reservation_uom_code = x_to_rsv_rec.secondary_uom_code
2064             and (p_to_rsv_rec.secondary_reservation_quantity <> fnd_api.g_miss_num AND p_to_rsv_rec.secondary_reservation_quantity IS NOT NULL)
2065             and p_original_rsv_rec.demand_source_type_id = p_to_rsv_rec.demand_source_type_id THEN
2066             x_to_rsv_rec.reservation_quantity := p_to_rsv_rec.secondary_reservation_quantity;
2067 	    -- MUOM Fulfillment project
2068       ELSIF l_fulfill_base = 'S' and p_to_rsv_rec.demand_source_type_id in (2,8) AND p_to_rsv_rec.reservation_quantity is not null and x_to_rsv_rec.reservation_uom_code is not null
2069             and x_to_rsv_rec.reservation_uom_code <> x_to_rsv_rec.primary_uom_code and x_to_rsv_rec.reservation_uom_code = x_to_rsv_rec.secondary_uom_code
2070             and (p_to_rsv_rec.secondary_reservation_quantity <> fnd_api.g_miss_num AND p_to_rsv_rec.secondary_reservation_quantity IS NOT NULL)
2071             and p_original_rsv_rec.demand_source_type_id = p_to_rsv_rec.demand_source_type_id THEN
2072            x_to_rsv_rec.reservation_quantity := p_to_rsv_rec.secondary_reservation_quantity;
2073       ELSE
2074          IF (l_debug = 1) THEN
2075             debug_print('Nulling out x_to_rsv_rec.reservation_quantity');
2076          END IF;
2077          x_to_rsv_rec.reservation_quantity          := NULL;
2078       END IF;
2079 
2080     ELSE
2081       -- primary_reservation_quantity is fnd_api.g_miss_num
2082       -- but reservation_quantity is null or
2083       -- value other than fnd_api.g_miss_num
2084       -- for the to row
2085       x_to_rsv_rec.primary_reservation_quantity  := NULL;
2086       x_to_rsv_rec.reservation_quantity          := p_to_rsv_rec.reservation_quantity;
2087     END IF;
2088 
2089     --
2090     -- INVCONV BEGIN
2091     IF p_to_rsv_rec.secondary_reservation_quantity <> fnd_api.g_miss_num
2092         OR p_to_rsv_rec.secondary_reservation_quantity IS NULL THEN
2093       x_to_rsv_rec.secondary_reservation_quantity  := p_to_rsv_rec.secondary_reservation_quantity;
2094     ELSE
2095       x_to_rsv_rec.secondary_reservation_quantity  := p_original_rsv_rec.secondary_reservation_quantity;
2096     END IF;
2097     -- INVCONV END
2098 
2099     IF p_to_rsv_rec.detailed_quantity <> fnd_api.g_miss_num
2100        OR p_to_rsv_rec.detailed_quantity IS NULL THEN
2101       x_to_rsv_rec.detailed_quantity  := p_to_rsv_rec.detailed_quantity;
2102     ELSE
2103       x_to_rsv_rec.detailed_quantity  := p_original_rsv_rec.detailed_quantity;
2104     END IF;
2105 
2106     -- INVCONV BEGIN
2107     IF p_to_rsv_rec.secondary_detailed_quantity <> fnd_api.g_miss_num
2108          OR p_to_rsv_rec.secondary_detailed_quantity IS NULL THEN
2109       x_to_rsv_rec.secondary_detailed_quantity  := p_to_rsv_rec.secondary_detailed_quantity;
2110     ELSE
2111       x_to_rsv_rec.secondary_detailed_quantity  := p_original_rsv_rec.secondary_detailed_quantity;
2112     END IF;
2113     -- INVCONV END
2114 
2115     IF p_to_rsv_rec.autodetail_group_id <> fnd_api.g_miss_num
2116        OR p_to_rsv_rec.autodetail_group_id IS NULL THEN
2117       x_to_rsv_rec.autodetail_group_id  := p_to_rsv_rec.autodetail_group_id;
2118     ELSE
2119       x_to_rsv_rec.autodetail_group_id  := p_original_rsv_rec.autodetail_group_id;
2120     END IF;
2121 
2122     --
2123     IF p_to_rsv_rec.external_source_code <> fnd_api.g_miss_char
2124        OR p_to_rsv_rec.external_source_code IS NULL THEN
2125       x_to_rsv_rec.external_source_code  := p_to_rsv_rec.external_source_code;
2126     ELSE
2127       x_to_rsv_rec.external_source_code  := p_original_rsv_rec.external_source_code;
2128     END IF;
2129 
2130     --
2131     IF p_to_rsv_rec.external_source_line_id <> fnd_api.g_miss_num
2132        OR p_to_rsv_rec.external_source_line_id IS NULL THEN
2133       x_to_rsv_rec.external_source_line_id  := p_to_rsv_rec.external_source_line_id;
2134     ELSE
2135       x_to_rsv_rec.external_source_line_id  := p_original_rsv_rec.external_source_line_id;
2136     END IF;
2137 
2138     --
2139     IF p_to_rsv_rec.supply_source_type_id <> fnd_api.g_miss_num
2140        OR p_to_rsv_rec.supply_source_type_id IS NULL THEN
2141       x_to_rsv_rec.supply_source_type_id  := p_to_rsv_rec.supply_source_type_id;
2142     ELSE
2143       x_to_rsv_rec.supply_source_type_id  := p_original_rsv_rec.supply_source_type_id;
2144     END IF;
2145 
2146     --
2147     IF p_to_rsv_rec.supply_source_header_id <> fnd_api.g_miss_num
2148        OR p_to_rsv_rec.supply_source_header_id IS NULL THEN
2149       x_to_rsv_rec.supply_source_header_id  := p_to_rsv_rec.supply_source_header_id;
2150     ELSE
2151       x_to_rsv_rec.supply_source_header_id  := p_original_rsv_rec.supply_source_header_id;
2152     END IF;
2153 
2154     --
2155     IF p_to_rsv_rec.supply_source_line_id <> fnd_api.g_miss_num
2156        OR p_to_rsv_rec.supply_source_line_id IS NULL THEN
2157       x_to_rsv_rec.supply_source_line_id  := p_to_rsv_rec.supply_source_line_id;
2158     ELSE
2159       x_to_rsv_rec.supply_source_line_id  := p_original_rsv_rec.supply_source_line_id;
2160     END IF;
2161 
2162     --
2163     IF p_to_rsv_rec.supply_source_line_detail <> fnd_api.g_miss_num
2164        OR p_to_rsv_rec.supply_source_line_detail IS NULL THEN
2165       x_to_rsv_rec.supply_source_line_detail  := p_to_rsv_rec.supply_source_line_detail;
2166     ELSE
2167       x_to_rsv_rec.supply_source_line_detail  := p_original_rsv_rec.supply_source_line_detail;
2168     END IF;
2169 
2170     --
2171     IF p_to_rsv_rec.supply_source_name <> fnd_api.g_miss_char
2172        OR p_to_rsv_rec.supply_source_name IS NULL THEN
2173       x_to_rsv_rec.supply_source_name  := p_to_rsv_rec.supply_source_name;
2174     ELSE
2175       x_to_rsv_rec.supply_source_name  := p_original_rsv_rec.supply_source_name;
2176     END IF;
2177 
2178     --
2179     IF p_to_rsv_rec.revision <> fnd_api.g_miss_char
2180        OR p_to_rsv_rec.revision IS NULL THEN
2181       x_to_rsv_rec.revision  := p_to_rsv_rec.revision;
2182     ELSE
2183       x_to_rsv_rec.revision  := p_original_rsv_rec.revision;
2184     END IF;
2185 
2186     --
2187     IF p_to_rsv_rec.subinventory_code <> fnd_api.g_miss_char
2188        OR p_to_rsv_rec.subinventory_code IS NULL THEN
2189       x_to_rsv_rec.subinventory_code  := p_to_rsv_rec.subinventory_code;
2190     ELSE
2191       x_to_rsv_rec.subinventory_code  := p_original_rsv_rec.subinventory_code;
2192     END IF;
2193 
2194     --
2195     IF p_to_rsv_rec.subinventory_id <> fnd_api.g_miss_num
2196        OR p_to_rsv_rec.subinventory_id IS NULL THEN
2197       x_to_rsv_rec.subinventory_id  := p_to_rsv_rec.subinventory_id;
2198     ELSE
2199       x_to_rsv_rec.subinventory_id  := p_original_rsv_rec.subinventory_id;
2200     END IF;
2201 
2202     --
2203     IF p_to_rsv_rec.locator_id <> fnd_api.g_miss_num
2204        OR p_to_rsv_rec.locator_id IS NULL THEN
2205       x_to_rsv_rec.locator_id  := p_to_rsv_rec.locator_id;
2206     ELSE
2207       x_to_rsv_rec.locator_id  := p_original_rsv_rec.locator_id;
2208     END IF;
2209 
2210     --
2211     IF p_to_rsv_rec.lot_number <> fnd_api.g_miss_char
2212        OR p_to_rsv_rec.lot_number IS NULL THEN
2213       x_to_rsv_rec.lot_number  := p_to_rsv_rec.lot_number;
2214     ELSE
2215       x_to_rsv_rec.lot_number  := p_original_rsv_rec.lot_number;
2216     END IF;
2217 
2218     --
2219     IF p_to_rsv_rec.lot_number_id <> fnd_api.g_miss_num
2220        OR p_to_rsv_rec.lot_number_id IS NULL THEN
2221       x_to_rsv_rec.lot_number_id  := p_to_rsv_rec.lot_number_id;
2222     ELSE
2223       x_to_rsv_rec.lot_number_id  := p_original_rsv_rec.lot_number_id;
2224     END IF;
2225 
2226     --
2227     IF p_to_rsv_rec.pick_slip_number <> fnd_api.g_miss_num
2228        OR p_to_rsv_rec.pick_slip_number IS NULL THEN
2229       x_to_rsv_rec.pick_slip_number  := p_to_rsv_rec.pick_slip_number;
2230     ELSE
2231       x_to_rsv_rec.pick_slip_number  := p_original_rsv_rec.pick_slip_number;
2232     END IF;
2233 
2234     --
2235     IF p_to_rsv_rec.lpn_id <> fnd_api.g_miss_num
2236        OR p_to_rsv_rec.lpn_id IS NULL THEN
2237       x_to_rsv_rec.lpn_id  := p_to_rsv_rec.lpn_id;
2238     ELSE
2239       x_to_rsv_rec.lpn_id  := p_original_rsv_rec.lpn_id;
2240     END IF;
2241 
2242     --
2243     IF p_to_rsv_rec.attribute_category <> fnd_api.g_miss_char
2244        OR p_to_rsv_rec.attribute_category IS NULL THEN
2245       x_to_rsv_rec.attribute_category  := p_to_rsv_rec.attribute_category;
2246     ELSE
2247       x_to_rsv_rec.attribute_category  := p_original_rsv_rec.attribute_category;
2248     END IF;
2249 
2250     --
2251     IF p_to_rsv_rec.attribute1 <> fnd_api.g_miss_char
2252        OR p_to_rsv_rec.attribute1 IS NULL THEN
2253       x_to_rsv_rec.attribute1  := p_to_rsv_rec.attribute1;
2254     ELSE
2255       x_to_rsv_rec.attribute1  := p_original_rsv_rec.attribute1;
2256     END IF;
2257 
2258     IF p_to_rsv_rec.attribute2 <> fnd_api.g_miss_char
2259        OR p_to_rsv_rec.attribute2 IS NULL THEN
2260       x_to_rsv_rec.attribute2  := p_to_rsv_rec.attribute2;
2261     ELSE
2262       x_to_rsv_rec.attribute2  := p_original_rsv_rec.attribute2;
2263     END IF;
2264 
2265     IF p_to_rsv_rec.attribute3 <> fnd_api.g_miss_char
2266        OR p_to_rsv_rec.attribute3 IS NULL THEN
2267       x_to_rsv_rec.attribute3  := p_to_rsv_rec.attribute3;
2268     ELSE
2269       x_to_rsv_rec.attribute3  := p_original_rsv_rec.attribute3;
2270     END IF;
2271 
2272     IF p_to_rsv_rec.attribute4 <> fnd_api.g_miss_char
2273        OR p_to_rsv_rec.attribute4 IS NULL THEN
2274       x_to_rsv_rec.attribute4  := p_to_rsv_rec.attribute4;
2275     ELSE
2276       x_to_rsv_rec.attribute4  := p_original_rsv_rec.attribute4;
2277     END IF;
2278 
2279     IF p_to_rsv_rec.attribute5 <> fnd_api.g_miss_char
2280        OR p_to_rsv_rec.attribute5 IS NULL THEN
2281       x_to_rsv_rec.attribute5  := p_to_rsv_rec.attribute5;
2282     ELSE
2283       x_to_rsv_rec.attribute5  := p_original_rsv_rec.attribute5;
2284     END IF;
2285 
2286     IF p_to_rsv_rec.attribute6 <> fnd_api.g_miss_char
2287        OR p_to_rsv_rec.attribute6 IS NULL THEN
2288       x_to_rsv_rec.attribute6  := p_to_rsv_rec.attribute6;
2289     ELSE
2290       x_to_rsv_rec.attribute6  := p_original_rsv_rec.attribute6;
2291     END IF;
2292 
2293     IF p_to_rsv_rec.attribute7 <> fnd_api.g_miss_char
2294        OR p_to_rsv_rec.attribute7 IS NULL THEN
2295       x_to_rsv_rec.attribute7  := p_to_rsv_rec.attribute7;
2296     ELSE
2297       x_to_rsv_rec.attribute7  := p_original_rsv_rec.attribute7;
2298     END IF;
2299 
2300     IF p_to_rsv_rec.attribute8 <> fnd_api.g_miss_char
2301        OR p_to_rsv_rec.attribute8 IS NULL THEN
2302       x_to_rsv_rec.attribute8  := p_to_rsv_rec.attribute8;
2303     ELSE
2304       x_to_rsv_rec.attribute8  := p_original_rsv_rec.attribute8;
2305     END IF;
2306 
2307     IF p_to_rsv_rec.attribute9 <> fnd_api.g_miss_char
2308        OR p_to_rsv_rec.attribute9 IS NULL THEN
2309       x_to_rsv_rec.attribute9  := p_to_rsv_rec.attribute9;
2310     ELSE
2311       x_to_rsv_rec.attribute9  := p_original_rsv_rec.attribute9;
2312     END IF;
2313 
2314     IF p_to_rsv_rec.attribute10 <> fnd_api.g_miss_char
2315        OR p_to_rsv_rec.attribute10 IS NULL THEN
2316       x_to_rsv_rec.attribute10  := p_to_rsv_rec.attribute10;
2317     ELSE
2318       x_to_rsv_rec.attribute10  := p_original_rsv_rec.attribute10;
2319     END IF;
2320 
2321     IF p_to_rsv_rec.attribute11 <> fnd_api.g_miss_char
2322        OR p_to_rsv_rec.attribute11 IS NULL THEN
2323       x_to_rsv_rec.attribute11  := p_to_rsv_rec.attribute11;
2324     ELSE
2325       x_to_rsv_rec.attribute11  := p_original_rsv_rec.attribute11;
2326     END IF;
2327 
2328     IF p_to_rsv_rec.attribute12 <> fnd_api.g_miss_char
2329        OR p_to_rsv_rec.attribute12 IS NULL THEN
2330       x_to_rsv_rec.attribute12  := p_to_rsv_rec.attribute12;
2331     ELSE
2332       x_to_rsv_rec.attribute12  := p_original_rsv_rec.attribute12;
2333     END IF;
2334 
2335     IF p_to_rsv_rec.attribute13 <> fnd_api.g_miss_char
2336        OR p_to_rsv_rec.attribute13 IS NULL THEN
2337       x_to_rsv_rec.attribute13  := p_to_rsv_rec.attribute13;
2338     ELSE
2339       x_to_rsv_rec.attribute13  := p_original_rsv_rec.attribute13;
2340     END IF;
2341 
2342     IF p_to_rsv_rec.attribute14 <> fnd_api.g_miss_char
2343        OR p_to_rsv_rec.attribute14 IS NULL THEN
2344       x_to_rsv_rec.attribute14  := p_to_rsv_rec.attribute14;
2345     ELSE
2346       x_to_rsv_rec.attribute14  := p_original_rsv_rec.attribute14;
2347     END IF;
2348 
2349     IF p_to_rsv_rec.attribute15 <> fnd_api.g_miss_char
2350        OR p_to_rsv_rec.attribute15 IS NULL THEN
2351       x_to_rsv_rec.attribute15  := p_to_rsv_rec.attribute15;
2352     ELSE
2353       x_to_rsv_rec.attribute15  := p_original_rsv_rec.attribute15;
2354     END IF;
2355 
2356     IF p_to_rsv_rec.ship_ready_flag <> fnd_api.g_miss_num
2357        OR p_to_rsv_rec.ship_ready_flag IS NULL THEN
2358       x_to_rsv_rec.ship_ready_flag  := p_to_rsv_rec.ship_ready_flag;
2359     ELSE
2360       x_to_rsv_rec.ship_ready_flag  := p_original_rsv_rec.ship_ready_flag;
2361     END IF;
2362 
2363     IF p_to_rsv_rec.staged_flag <> fnd_api.g_miss_char
2364        OR p_to_rsv_rec.staged_flag IS NULL THEN
2365       x_to_rsv_rec.staged_flag  := p_to_rsv_rec.staged_flag;
2366     ELSE
2367       x_to_rsv_rec.staged_flag  := p_original_rsv_rec.staged_flag;
2368     END IF;
2369 
2370     /**** {{ R12 Enhanced reservations code changes. Adding new columns to
2371     -- construct_to_reservation_row API }}****/
2372     IF p_to_rsv_rec.crossdock_flag <> fnd_api.g_miss_char
2373       OR p_to_rsv_rec.crossdock_flag IS NULL THEN
2374        x_to_rsv_rec.crossdock_flag  := p_to_rsv_rec.crossdock_flag;
2375      ELSE
2376        x_to_rsv_rec.crossdock_flag  := p_original_rsv_rec.crossdock_flag;
2377     END IF;
2378 
2379     IF p_to_rsv_rec.crossdock_criteria_id <> fnd_api.g_miss_num
2380       OR p_to_rsv_rec.crossdock_criteria_id IS NULL THEN
2381        x_to_rsv_rec.crossdock_criteria_id  := p_to_rsv_rec.crossdock_criteria_id;
2382      ELSE
2383        x_to_rsv_rec.crossdock_criteria_id  := p_original_rsv_rec.crossdock_criteria_id;
2384     END IF;
2385 
2386     IF p_to_rsv_rec.demand_source_line_detail <> fnd_api.g_miss_num
2387       OR p_to_rsv_rec.demand_source_line_detail IS NULL THEN
2388        x_to_rsv_rec.demand_source_line_detail  := p_to_rsv_rec.demand_source_line_detail;
2389      ELSE
2390        x_to_rsv_rec.demand_source_line_detail  := p_original_rsv_rec.demand_source_line_detail;
2391     END IF;
2392 
2393      IF p_to_rsv_rec.serial_reservation_quantity <> fnd_api.g_miss_num
2394       OR p_to_rsv_rec.serial_reservation_quantity IS NULL THEN
2395        x_to_rsv_rec.serial_reservation_quantity  := p_to_rsv_rec.serial_reservation_quantity;
2396      ELSE
2397        x_to_rsv_rec.serial_reservation_quantity  := p_original_rsv_rec.serial_reservation_quantity;
2398      END IF;
2399 
2400      IF p_to_rsv_rec.supply_receipt_date <> fnd_api.g_miss_date
2401       OR p_to_rsv_rec.supply_receipt_date IS NULL THEN
2402        x_to_rsv_rec.supply_receipt_date  := p_to_rsv_rec.supply_receipt_date;
2403      ELSE
2404        x_to_rsv_rec.supply_receipt_date  := p_original_rsv_rec.supply_receipt_date;
2405      END IF;
2406 
2407      IF p_to_rsv_rec.demand_ship_date <> fnd_api.g_miss_date
2408       OR p_to_rsv_rec.demand_ship_date IS NULL THEN
2409        x_to_rsv_rec.demand_ship_date  := p_to_rsv_rec.demand_ship_date;
2410      ELSE
2411        x_to_rsv_rec.demand_ship_date  := p_original_rsv_rec.demand_ship_date;
2412      END IF;
2413 
2414      IF p_to_rsv_rec.project_id <> fnd_api.g_miss_num
2415       OR p_to_rsv_rec.project_id IS NULL THEN
2416        x_to_rsv_rec.project_id  := p_to_rsv_rec.project_id;
2417      ELSE
2418        x_to_rsv_rec.project_id  := p_original_rsv_rec.project_id;
2419      END IF;
2420 
2421      IF p_to_rsv_rec.task_id <> fnd_api.g_miss_num
2422       OR p_to_rsv_rec.task_id IS NULL THEN
2423        x_to_rsv_rec.task_id  := p_to_rsv_rec.task_id;
2424      ELSE
2425        x_to_rsv_rec.task_id  := p_original_rsv_rec.task_id;
2426     END IF;
2427 
2428     /*** End R12 ***/
2429 
2430   END construct_to_reservation_row;
2431 
2432   --
2433   -- Description
2434   --   return true if the given index points to an item record
2435   --   in the cache which has a revision control code as yes
2436   FUNCTION is_revision_control(p_item_cache_index IN INTEGER)
2437     RETURN BOOLEAN IS
2438   BEGIN
2439     IF inv_reservation_global.g_item_record_cache(p_item_cache_index).revision_qty_control_code = inv_reservation_global.g_revision_control_yes THEN
2440       RETURN TRUE;
2441     ELSE
2442       RETURN FALSE;
2443     END IF;
2444   END is_revision_control;
2445 
2446   --
2447   -- Description
2448   --   return true if the given index points to an item record
2449   --   in the cache which has a lot control code as yes
2450   FUNCTION is_lot_control(p_item_cache_index IN INTEGER)
2451     RETURN BOOLEAN IS
2452   BEGIN
2453     IF inv_reservation_global.g_item_record_cache(p_item_cache_index).lot_control_code = inv_reservation_global.g_lot_control_yes THEN
2454       RETURN TRUE;
2455     ELSE
2456       RETURN FALSE;
2457     END IF;
2458   END is_lot_control;
2459 
2460   --
2461   -- Description
2462   --   return true if the given index points to an item record
2463   --   in the cache which has a serial control code as yes
2464   FUNCTION is_serial_control(p_item_cache_index IN INTEGER)
2465     RETURN BOOLEAN IS
2466   BEGIN
2467     IF inv_reservation_global.g_item_record_cache(p_item_cache_index).serial_number_control_code <> inv_reservation_global.g_serial_control_predefined THEN
2468       RETURN FALSE;
2469     ELSE
2470       RETURN TRUE;
2471     END IF;
2472   END is_serial_control;
2473 
2474   --INVCONV BEGIN
2475   --
2476   -- Description
2477   -- ===========
2478   --   return true if the given index points to an item record
2479   --   in the cache which has a dual UOM control as true
2480   FUNCTION is_dual_control(p_item_cache_index IN INTEGER)
2481      RETURN BOOLEAN IS
2482      --l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
2483   BEGIN
2484     IF inv_reservation_global.g_item_record_cache(p_item_cache_index).tracking_quantity_ind <> 'PS' THEN
2485       RETURN FALSE;
2486     ELSE
2487       RETURN TRUE;
2488     END IF;
2489   END is_dual_control;
2490 
2491   -- Description
2492   -- ===========
2493   --   return true if the given index points to an item record
2494   --   in the cache which has lot divisible true
2495   FUNCTION is_lot_divisible(p_item_cache_index IN INTEGER)
2496      RETURN BOOLEAN IS
2497     l_debug number;
2498   BEGIN
2499     IF (g_debug IS NULL) THEN
2500        g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
2501     END IF;
2502 
2503     l_debug := g_debug;
2504 
2505     IF inv_reservation_global.g_item_record_cache(p_item_cache_index).lot_control_code
2506        = inv_reservation_global.g_lot_control_yes AND
2507        inv_reservation_global.g_item_record_cache(p_item_cache_index).lot_divisible_flag <> 'Y' THEN
2508        IF (l_debug = 1) THEN
2509          debug_print('Lot divisible is FALSE ');
2510        END IF;
2511        RETURN FALSE;
2512     ELSE
2513        IF (l_debug = 1) THEN
2514          debug_print('Lot divisible is TRUE ');
2515        END IF;
2516        RETURN TRUE;
2517     END IF;
2518   END is_lot_divisible;
2519 
2520   -- B4498579 BEGIN
2521   -- Description
2522   -- ===========
2523   --   Determine lot divisibility for cases where caching is not in use
2524   --   and function is_lot_divisible above is not appropriate
2525   --   This function interrogates mtl_system_items.lot_divisible_flag
2526   FUNCTION lot_divisible(p_inventory_item_id NUMBER,p_organization_id NUMBER)
2527      RETURN BOOLEAN IS
2528 	l_debug number;
2529 	l_lot_divisible_flag VARCHAR2(1);
2530 	l_lot_control_code NUMBER;
2531   BEGIN
2532 
2533      IF (g_debug IS NULL) THEN
2534 	 g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
2535       END IF;
2536 
2537       l_debug := g_debug;
2538 
2539       BEGIN
2540 	 SELECT lot_control_code, lot_divisible_flag INTO
2541 	   l_lot_control_code, l_lot_divisible_flag FROM mtl_system_items
2542 	   WHERE inventory_item_id = p_inventory_item_id
2543 	   AND organization_id = p_organization_id;
2544       EXCEPTION
2545 	 WHEN no_data_found THEN
2546 	    IF (l_debug =1) THEN
2547 	       debug_print('could not find the record for the item id ' ||
2548 			   p_inventory_item_id || ' Org ' || p_organization_id);
2549 	    END IF;
2550       END;
2551 
2552       IF (l_debug = 1) THEN
2553 	 debug_print('Lot divisible flag set to '||l_lot_divisible_flag );
2554       END IF;
2555 
2556       IF (l_lot_control_code = 2 AND Upper(l_lot_divisible_flag) <> 'Y') THEN
2557 	 RETURN FALSE;
2558        ELSE
2559 	 RETURN TRUE;
2560       END IF;
2561 
2562   END lot_divisible;
2563   -- B4498579 END
2564 
2565   -- INVCONV END
2566 
2567   /**** {{ R12 Enhanced reservations code changes. New API get_wip_entity_type }}****/
2568   /*  This API will take a set of parameters and return the wip entity type and the job type as output. */
2569 
2570   PROCEDURE get_wip_entity_type
2571     (
2572      p_api_version_number  IN   NUMBER
2573      , p_init_msg_lst      IN   VARCHAR2 DEFAULT fnd_api.g_false
2574      , x_return_status     OUT  NOCOPY VARCHAR2
2575      , x_msg_count         OUT  NOCOPY NUMBER
2576      , x_msg_data          OUT  NOCOPY VARCHAR2
2577      , p_organization_id   IN	NUMBER DEFAULT null
2578      , p_item_id	   IN	NUMBER DEFAULT null
2579      , p_source_type_id    IN	NUMBER DEFAULT INV_RESERVATION_GLOBAL.G_SOURCE_TYPE_WIP
2580      , p_source_header_id  IN	NUMBER
2581      , p_source_line_id	   IN   NUMBER
2582      , p_source_line_detail IN	NUMBER
2583      , x_wip_entity_type   OUT NOCOPY NUMBER
2584      , x_wip_job_type	   OUT NOCOPY VARCHAR2
2585      ) IS
2586 
2587 	l_wip_entity_id NUMBER;
2588 	l_wip_entity_type NUMBER;
2589 	l_wip_job_type VARCHAR2(15);
2590 	l_maintenance_object_source NUMBER;
2591 	l_debug number;
2592 	l_api_name  CONSTANT VARCHAR2(30) := 'Get_WIP_Entity_Type';
2593 
2594   BEGIN
2595      IF (g_debug IS NULL) THEN
2596         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
2597      END IF;
2598 
2599      l_debug := g_debug;
2600 
2601      IF (p_source_type_id <> INV_RESERVATION_GLOBAL.g_source_type_wip) THEN
2602 	fnd_message.set_name('INV', 'INV_INVALID_SUPPLY_SOURCE');
2603 	fnd_msg_pub.add;
2604 	RAISE fnd_api.g_exc_error;
2605      END IF;
2606 
2607   BEGIN
2608      SELECT we.entity_type, wdj.maintenance_object_source INTO
2609        l_wip_entity_id, l_maintenance_object_source FROM wip_entities we,
2610        wip_discrete_jobs wdj WHERE we.wip_entity_id = p_source_header_id
2611        AND we.wip_entity_id = wdj.wip_entity_id(+);
2612   EXCEPTION
2613      WHEN no_data_found THEN
2614      IF (l_debug = 1) THEN
2615 	debug_print('No WIP entity record found for the source header passed' );
2616      END IF;
2617      fnd_message.set_name('INV', 'INV_INVALID_WIP_ENTITY_TYPE');
2618      fnd_msg_pub.add;
2619      RAISE fnd_api.g_exc_error;
2620   END ;
2621 
2622   IF l_wip_entity_id = inv_reservation_global.g_wip_source_type_discrete then
2623      l_wip_entity_type := inv_reservation_global.g_wip_source_type_discrete;
2624      l_wip_job_type := 'DISCRETE';
2625    ELSIF l_wip_entity_id = 2 then
2626      l_wip_entity_type := inv_reservation_global.g_wip_source_type_repetitive;
2627      l_wip_job_type := 'REPETITIVE';
2628    ELSIF l_wip_entity_id = 4 then
2629      l_wip_entity_type := inv_reservation_global.g_wip_source_type_flow;
2630      l_wip_job_type := 'FLOW';
2631    ELSIF l_wip_entity_id = 5 then
2632      l_wip_entity_type := inv_reservation_global.g_wip_source_type_osfm;
2633      l_wip_job_type := 'OSFM';
2634    ELSIF l_wip_entity_id = 6 and l_maintenance_object_source = 1 then
2635      l_wip_entity_type := inv_reservation_global.g_wip_source_type_eam;
2636      l_wip_job_type := 'EAM';
2637    ELSIF l_wip_entity_id = 6 and l_maintenance_object_source = 2  then
2638      l_wip_entity_type := inv_reservation_global.g_wip_source_type_cmro;
2639      l_wip_job_type := 'CMRO'; -- AHL
2640    ELSIF l_wip_entity_id  = 9 then
2641      l_wip_entity_type := inv_reservation_global.g_wip_source_type_fpo;
2642      l_wip_job_type := 'FPO';
2643    ELSIF l_wip_entity_id = 10 then
2644      l_wip_entity_type := inv_reservation_global.g_wip_source_type_batch;
2645      l_wip_job_type := 'BATCH';
2646    ELSIF l_wip_entity_id = 16 THEN
2647      l_wip_entity_type := inv_reservation_global.g_wip_source_type_depot;
2648      l_wip_job_type := 'DEPOT';
2649   END IF;
2650 
2651   x_wip_entity_type := l_wip_entity_type;
2652   x_wip_job_type := l_wip_job_type;
2653 
2654   EXCEPTION
2655      WHEN fnd_api.g_exc_error THEN
2656 	x_return_status  := fnd_api.g_ret_sts_error;
2657 	--  Get message count and data
2658 	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2659      WHEN fnd_api.g_exc_unexpected_error THEN
2660 	x_return_status  := fnd_api.g_ret_sts_unexp_error;
2661 	--  Get message count and data
2662 	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2663      WHEN OTHERS THEN
2664 	x_return_status  := fnd_api.g_ret_sts_unexp_error;
2665 
2666 	IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
2667 	   fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
2668 	END IF;
2669 
2670 	--  Get message count and data
2671 	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2672   END get_wip_entity_type;
2673 
2674   PROCEDURE update_serial_rsv_quantity(
2675      x_return_status       OUT NOCOPY VARCHAR2
2676    , x_msg_count           OUT NOCOPY NUMBER
2677    , x_msg_data            OUT NOCOPY VARCHAR2
2678    , p_reservation_id      IN  NUMBER
2679    , p_update_serial_qty   IN  NUMBER)
2680 
2681   IS
2682     l_api_name     CONSTANT VARCHAR2(30) := 'update_serial_rsv_quantity';
2683     l_update_count NUMBER := 0;
2684     l_debug        NUMBER;
2685   BEGIN
2686     IF (g_debug is NULL) THEN
2687         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
2688     END IF;
2689 
2690     l_debug := g_debug;
2691 
2692     IF (l_debug = 1) THEN
2693         debug_print('In update_serial_rsv_quantity');
2694         debug_print('reservation_id = ' || p_reservation_id);
2695         debug_print('update_serial_qty = ' || p_update_serial_qty);
2696     END IF;
2697 
2698     update mtl_reservations
2699     set    serial_reservation_quantity = serial_reservation_quantity + p_update_serial_qty
2700     where  reservation_id = p_reservation_id;
2701     l_update_count := SQL%ROWCOUNT;
2702 
2703     IF (l_debug = 1) THEN
2704         debug_print('Number of rows update: ' || l_update_count);
2705     END IF;
2706 
2707     x_return_status := fnd_api.g_ret_sts_success;
2708   EXCEPTION
2709     WHEN fnd_api.g_exc_error THEN
2710       x_return_status  := fnd_api.g_ret_sts_error;
2711       --  Get message count and data
2712       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2713     WHEN fnd_api.g_exc_unexpected_error THEN
2714       x_return_status  := fnd_api.g_ret_sts_unexp_error;
2715       --  Get message count and data
2716       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2717     WHEN OTHERS THEN
2718       x_return_status  := fnd_api.g_ret_sts_unexp_error;
2719 
2720       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
2721         fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
2722       END IF;
2723 
2724       --  Get message count and data
2725       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2726 
2727   END update_serial_rsv_quantity;
2728 
2729  PROCEDURE is_serial_number_reserved(
2730      p_api_version_number        IN  NUMBER
2731    , p_init_msg_lst              IN  VARCHAR2 DEFAULT fnd_api.g_false
2732    , x_return_status             OUT NOCOPY VARCHAR2
2733    , x_msg_count                 OUT NOCOPY NUMBER
2734    , x_msg_data                  OUT NOCOPY VARCHAR2
2735    , p_serial_number_tbl         IN  inv_reservation_global.serial_number_tbl_type
2736    , x_serial_number_tbl         OUT NOCOPY inv_reservation_global.rsv_serial_number_table)
2737   IS
2738     l_api_name     CONSTANT VARCHAR2(30) := 'is_serial_number_reserved';
2739     l_debug        NUMBER;
2740     l_index        NUMBER := 0;
2741   BEGIN
2742     IF (g_debug is NULL) THEN
2743         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
2744     END IF;
2745 
2746     l_debug := g_debug;
2747 
2748     IF (l_debug = 1) THEN
2749         debug_print('In is_serial_number_reserved');
2750         FOR i in 1..p_serial_number_tbl.count LOOP
2751             debug_print(i || ' : item = ' || p_serial_number_tbl(i).inventory_item_id ||
2752                         ', serial number = ' || p_serial_number_tbl(i).serial_number);
2753         END LOOP;
2754     END IF;
2755 
2756     -- reset the index for x_serial_number_tbl
2757     l_index := 0;
2758     FOR i in 1..p_serial_number_tbl.count LOOP
2759         BEGIN
2760            SELECT reservation_id, serial_number
2761            INTO   x_serial_number_tbl(l_index).reservation_id, x_serial_number_tbl(l_index).serial_number
2762            FROM   mtl_serial_numbers
2763            WHERE  serial_number = p_serial_number_tbl(i).serial_number
2764            AND    inventory_item_id = p_serial_number_tbl(i).inventory_item_id
2765            AND    reservation_id is not null;
2766            l_index := l_index + 1;
2767         EXCEPTION
2768            WHEN no_data_found THEN
2769                 IF (l_debug = 1) THEN
2770                     debug_print('serial number ' || p_serial_number_tbl(i).serial_number || ' is not reserved.');
2771                 END IF;
2772         END;
2773     END LOOP;
2774 
2775     IF (l_debug = 1) THEN
2776         FOR i in 1..x_serial_number_tbl.count LOOP
2777             debug_print(i || ': reservation id = ' || x_serial_number_tbl(i).reservation_id ||
2778                         ', serial number = ' || x_serial_number_tbl(i).serial_number);
2779         END LOOP;
2780     END IF;
2781 
2782     x_return_status := fnd_api.g_ret_sts_success;
2783   EXCEPTION
2784     WHEN fnd_api.g_exc_error THEN
2785       x_return_status  := fnd_api.g_ret_sts_error;
2786       --  Get message count and data
2787       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2788     WHEN fnd_api.g_exc_unexpected_error THEN
2789       x_return_status  := fnd_api.g_ret_sts_unexp_error;
2790       --  Get message count and data
2791       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2792     WHEN OTHERS THEN
2793       x_return_status  := fnd_api.g_ret_sts_unexp_error;
2794 
2795       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
2796         fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
2797       END IF;
2798 
2799       --  Get message count and data
2800       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2801   END is_serial_number_reserved;
2802 
2803   PROCEDURE is_serial_reserved(
2804      p_api_version_number        IN  NUMBER
2805    , p_init_msg_lst              IN  VARCHAR2 DEFAULT fnd_api.g_false
2806    , x_return_status             OUT NOCOPY VARCHAR2
2807    , x_msg_count                 OUT NOCOPY NUMBER
2808    , x_msg_data                  OUT NOCOPY VARCHAR2
2809    , p_serial_number_tbl         IN  inv_reservation_global.serial_number_tbl_type
2810    , x_serial_number_tbl         OUT NOCOPY inv_reservation_global.rsv_serial_number_table
2811    , x_mtl_reservation_tbl       OUT NOCOPY inv_reservation_global.mtl_reservation_tbl_type)
2812   IS
2813     l_api_name     CONSTANT VARCHAR2(30) := 'is_serial_number_reserved';
2814     l_debug        NUMBER;
2815     l_index        NUMBER := 0;
2816 
2817     CURSOR rsv_csr(p_reservation_id NUMBER) IS
2818        SELECT reservation_id
2819             , requirement_date
2820             , organization_id
2821             , inventory_item_id
2822             , demand_source_type_id
2823             , demand_source_name
2824             , demand_source_header_id
2825             , demand_source_line_id
2826             , demand_source_delivery
2827             , primary_uom_code
2828             , primary_uom_id
2829             , secondary_uom_code
2830             , secondary_uom_id
2831             , reservation_uom_code
2832             , reservation_uom_id
2833             , reservation_quantity
2834             , primary_reservation_quantity
2835             , secondary_reservation_quantity
2836             , detailed_quantity
2837             , secondary_detailed_quantity
2838             , autodetail_group_id
2839             , external_source_code
2840             , external_source_line_id
2841             , supply_source_type_id
2842             , supply_source_header_id
2843             , supply_source_line_id
2844             , supply_source_name
2845             , supply_source_line_detail
2846             , revision
2847             , subinventory_code
2848             , subinventory_id
2849             , locator_id
2850             , lot_number
2851             , lot_number_id
2852             , pick_slip_number
2853             , lpn_id
2854             , attribute_category
2855             , attribute1
2856             , attribute2
2857             , attribute3
2858             , attribute4
2859             , attribute5
2860             , attribute6
2861             , attribute7
2862             , attribute8
2863             , attribute9
2864             , attribute10
2865             , attribute11
2866             , attribute12
2867             , attribute13
2868             , attribute14
2869             , attribute15
2870             , ship_ready_flag
2871             , staged_flag
2872             , crossdock_flag
2873             , crossdock_criteria_id
2874             , demand_source_line_detail
2875             , serial_reservation_quantity
2876             , supply_receipt_date
2877             , demand_ship_date
2878             , project_id
2879             , task_id
2880             , orig_supply_source_type_id
2881             , orig_supply_source_header_id
2882             , orig_supply_source_line_id
2883             , orig_supply_source_line_detail
2884             , orig_demand_source_type_id
2885             , orig_demand_source_header_id
2886             , orig_demand_source_line_id
2887 	 , orig_demand_source_line_detail
2888 	 , serial_number
2889        FROM mtl_reservations
2890        WHERE reservation_id = p_reservation_id;
2891   BEGIN
2892     IF (g_debug is NULL) THEN
2893         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
2894     END IF;
2895 
2896     l_debug := g_debug;
2897 
2898     IF (l_debug = 1) THEN
2899         debug_print('In is_serial_number_reserved');
2900         FOR i in 1..p_serial_number_tbl.count LOOP
2901             debug_print(i || ' : item = ' || p_serial_number_tbl(i).inventory_item_id ||
2902                         ', serial number = ' || p_serial_number_tbl(i).serial_number);
2903         END LOOP;
2904     END IF;
2905 
2906     -- reset the index for x_serial_number_tbl
2907     l_index := 0;
2908     FOR i in 1..p_serial_number_tbl.count LOOP
2909         BEGIN
2910            SELECT reservation_id, serial_number
2911            INTO   x_serial_number_tbl(l_index).reservation_id, x_serial_number_tbl(l_index).serial_number
2912            FROM   mtl_serial_numbers
2913            WHERE  serial_number = p_serial_number_tbl(i).serial_number
2914            AND    inventory_item_id = p_serial_number_tbl(i).inventory_item_id
2915            AND    reservation_id is not null;
2916 
2917            OPEN rsv_csr(x_serial_number_tbl(l_index).reservation_id);
2918            FETCH rsv_csr into x_mtl_reservation_tbl(l_index);
2919            CLOSE rsv_csr;
2920 
2921            l_index := l_index + 1;
2922         EXCEPTION
2923            WHEN no_data_found THEN
2924                 IF (l_debug = 1) THEN
2925                     debug_print('serial number ' || p_serial_number_tbl(i).serial_number || ' is not reserved.');
2926                 END IF;
2927         END;
2928     END LOOP;
2929 
2930     IF (l_debug = 1) THEN
2931         FOR i in 1..x_serial_number_tbl.count LOOP
2932             debug_print(i || ': reservation id = ' || x_serial_number_tbl(i).reservation_id ||
2933                         ', serial number = ' || x_serial_number_tbl(i).serial_number);
2934         END LOOP;
2935     END IF;
2936 
2937     x_return_status := fnd_api.g_ret_sts_success;
2938   EXCEPTION
2939     WHEN fnd_api.g_exc_error THEN
2940       x_return_status  := fnd_api.g_ret_sts_error;
2941       --  Get message count and data
2942       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2943     WHEN fnd_api.g_exc_unexpected_error THEN
2944       x_return_status  := fnd_api.g_ret_sts_unexp_error;
2945       --  Get message count and data
2946       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2947     WHEN OTHERS THEN
2948       x_return_status  := fnd_api.g_ret_sts_unexp_error;
2949 
2950       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
2951         fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
2952       END IF;
2953 
2954       --  Get message count and data
2955       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2956   END is_serial_reserved;
2957   /*** End R12 ***/
2958 
2959 
2960   --   modify_tree_crt_del_rel
2961   -- Description
2962   --   modify quantity tree for create or delete or relieve reservations
2963 
2964   PROCEDURE modify_tree_crt_del_rel(
2965     x_return_status                OUT    NOCOPY VARCHAR2
2966   , p_tree_id                      IN     INTEGER
2967   , p_revision                     IN     VARCHAR2
2968   , p_lot_number                   IN     VARCHAR2
2969   , p_subinventory_code            IN     VARCHAR2
2970   , p_locator_id                   IN     NUMBER
2971   , p_lpn_id                       IN     NUMBER
2972   , p_primary_reservation_quantity IN     NUMBER
2973   , p_second_reservation_quantity  IN     NUMBER           -- INVCONV
2974   , p_detailed_quantity            IN     NUMBER
2975   , p_secondary_detailed_quantity  IN     NUMBER           -- INVCONV
2976   , p_relieve_quantity             IN     NUMBER DEFAULT 0
2977   , p_secondary_relieve_quantity   IN     NUMBER DEFAULT 0 -- INVCONV
2978   , p_partial_reservation_flag     IN     VARCHAR2
2979   , p_force_reservation_flag       IN     VARCHAR2
2980   , p_lot_divisible_flag           IN     VARCHAR2         -- INVCONV
2981   , p_action                       IN     VARCHAR2
2982   , x_qty_changed                  OUT    NOCOPY NUMBER
2983   , x_secondary_qty_changed        OUT    NOCOPY NUMBER    -- INVCONV
2984   , p_organization_id              IN     NUMBER DEFAULT NULL -- MUOM Fulfillemnt Project
2985   , p_demand_source_line_id        IN     NUMBER DEFAULT NULL -- MUOM Fulfillemnt Project
2986   , p_demand_source_type_id		   IN 	  NUMBER DEFAULT NULL -- MUOM Fulfillemnt Project
2987   ) IS
2988     l_return_status     VARCHAR2(1)   := fnd_api.g_ret_sts_success;
2989     l_qoh               NUMBER;
2990     l_rqoh              NUMBER;
2991     l_qr                NUMBER;
2992     l_qs                NUMBER;
2993     l_att               NUMBER;
2994     l_atr               NUMBER;
2995     l_sqoh              NUMBER;                          -- INVCONV
2996     l_srqoh             NUMBER;                          -- INVCONV
2997     l_sqr               NUMBER;                          -- INVCONV
2998     l_sqs               NUMBER;                          -- INVCONV
2999     l_satt              NUMBER;                          -- INVCONV
3000     l_satr              NUMBER;                          -- INVCONV
3001     l_quantity_reserved NUMBER;
3002     l_quantity_type     NUMBER;
3003     l_secondary_net_qty NUMBER;                          -- INVCONV
3004     l_dual_control      BOOLEAN := FALSE;                -- INVCONV
3005     l_msg_count         NUMBER;
3006     l_msg_data          VARCHAR2(240);
3007     l_net_qty           NUMBER;
3008     l_debug number;
3009    --MUOM Fulfillment Project
3010     l_fulfill_base varchar2(1):='P';
3011 
3012   BEGIN
3013     -- Use cache to get value for l_debug
3014     IF g_is_pickrelease_set IS NULL THEN
3015        g_is_pickrelease_set := 2;
3016        IF INV_CACHE.is_pickrelease THEN
3017           g_is_pickrelease_set := 1;
3018        END IF;
3019     END IF;
3020     IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
3021        g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
3022     END IF;
3023 
3024     l_debug := g_debug;
3025 
3026     -- INVCONV
3027     -- Set boolean for dual control scenarios
3028     IF p_second_reservation_quantity is NOT NULL THEN
3029        l_dual_control := TRUE;
3030     END IF;
3031 
3032     -- INVCONV Include secondary parameters below
3033     inv_quantity_tree_pvt.query_tree(
3034       p_api_version_number         => 1.0
3035     , p_init_msg_lst               => fnd_api.g_true
3036     , x_return_status              => l_return_status
3037     , x_msg_count                  => l_msg_count
3038     , x_msg_data                   => l_msg_data
3039     , p_tree_id                    => p_tree_id
3040     , p_revision                   => p_revision
3041     , p_lot_number                 => p_lot_number
3042     , p_subinventory_code          => p_subinventory_code
3043     , p_locator_id                 => p_locator_id
3044     , x_qoh                        => l_qoh
3045     , x_rqoh                       => l_rqoh
3046     , x_qr                         => l_qr
3047     , x_qs                         => l_qs
3048     , x_att                        => l_att
3049     , x_atr                        => l_atr
3050     , x_sqoh                       => l_sqoh
3051     , x_srqoh                      => l_srqoh
3052     , x_sqr                        => l_sqr
3053     , x_sqs                        => l_sqs
3054     , x_satt                       => l_satt
3055     , x_satr                       => l_satr
3056     , p_lpn_id                     => p_lpn_id
3057     );
3058 
3059     IF l_return_status = fnd_api.g_ret_sts_error THEN
3060       RAISE fnd_api.g_exc_error;
3061     END IF;
3062 
3063     --
3064     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3065       RAISE fnd_api.g_exc_unexpected_error;
3066     END IF;
3067 
3068     IF (l_debug = 1) THEN
3069       debug_print('l_atr ' || l_atr);
3070       debug_print('l_att ' || l_att);
3071       debug_print('l_qoh ' || l_qoh);
3072       debug_print('l_rqoh ' || l_rqoh);
3073       debug_print('l_qr ' || l_qr);
3074       debug_print('l_qs ' || l_qs);
3075 
3076       -- INVCONV BEGIN
3077       debug_print('l_satr ' || l_satr);
3078       debug_print('l_satt ' || l_satt);
3079       debug_print('l_sqoh ' || l_sqoh);
3080       debug_print('l_srqoh ' || l_srqoh);
3081       debug_print('l_sqr ' || l_sqr);
3082       debug_print('l_sqs ' || l_sqs);
3083       -- INVCONV END
3084     END IF;
3085 
3086     --
3087     IF p_action = 'CREATE' THEN
3088       IF (l_debug = 1) THEN
3089         debug_print('CREATE');
3090         debug_print('p_primary_reservation_quantity ' || p_primary_reservation_quantity);
3091         debug_print('p_second_reservation_quantity ' || p_second_reservation_quantity);   -- INVCONV
3092         debug_print('p_detailed_quantity' || p_detailed_quantity);
3093         debug_print('p_secondary_detailed_quantity' || p_secondary_detailed_quantity);    -- INVCONV
3094       END IF;
3095       l_net_qty  := p_primary_reservation_quantity - NVL(p_detailed_quantity, 0);
3096       IF l_dual_control THEN                                                              -- INVCONV
3097         l_secondary_net_qty  := p_second_reservation_quantity - NVL(p_secondary_detailed_quantity, 0); -- INVCONV
3098       END IF;
3099       -- INVCONV BEGIN - lot_indivisible quantities cannot be split
3100       IF p_lot_divisible_flag = 'N' THEN
3101          IF l_net_qty < l_atr THEN
3102             -- Available to reserve cannot be subdivided
3103             fnd_message.set_name('INV', 'INV_LOT_INDIVISIBLE_VIOLATION');       -- INVCONV New Message
3104             fnd_msg_pub.ADD;
3105              RAISE fnd_api.g_exc_error;
3106          END IF;
3107       END IF;
3108       -- INVCONV END   - lot_indivisible quantities cannot be split
3109     ELSIF p_action = 'DELETE' THEN
3110       IF (l_debug = 1) THEN
3111         debug_print('DELETE');
3112         debug_print('p_primary_reservation_quantity ' || p_primary_reservation_quantity);
3113         debug_print('p_detailed_quantity' || p_detailed_quantity);
3114       END IF;
3115       l_net_qty  := -p_primary_reservation_quantity + NVL(p_detailed_quantity, 0);
3116     ELSIF p_action = 'RELIEVE' THEN
3117       IF (l_debug = 1) THEN
3118         debug_print('RELIEVE');
3119         debug_print('p_primary_reservation_quantity ' || p_primary_reservation_quantity);
3120         debug_print('p_second_reservation_quantity ' || p_second_reservation_quantity);   -- INVCONV
3121         debug_print('p_detailed_quantity' || p_detailed_quantity);
3122         debug_print('p_secondary_detailed_quantity' || p_secondary_detailed_quantity);    -- INVCONV
3123       END IF;
3124       l_net_qty  := -p_relieve_quantity;
3125       IF l_dual_control THEN                                                              -- INVCONV
3126         l_secondary_net_qty  := -p_secondary_relieve_quantity;
3127       END IF;                                                                             -- INVCONV
3128     ELSE
3129       RAISE fnd_api.g_exc_unexpected_error;
3130     END IF;
3131   --MUOM Fulfillment Project
3132 	inv_utilities.get_inv_fulfillment_base(
3133 			p_source_line_id		=> p_demand_source_line_id,
3134 			p_demand_source_type_id => p_demand_source_type_id,
3135 			p_org_id				=> p_organization_id,
3136 			x_fulfillment_base		=> l_fulfill_base
3137 			);
3138 
3139     IF (l_debug=1) THEN
3140          debug_print('Fulfill Base = '||l_fulfill_base);
3141     END IF;
3142 
3143     IF  p_force_reservation_flag <> fnd_api.g_true
3144         AND l_net_qty > 0
3145         AND ((l_net_qty > l_atr AND p_partial_reservation_flag = fnd_api.g_false AND l_fulfill_base='P')
3146               OR (l_atr <= 0 AND p_partial_reservation_flag = fnd_api.g_true)
3147               OR (l_fulfill_base ='S' AND l_secondary_net_qty > l_satr AND p_partial_reservation_flag = fnd_api.g_false)
3148             ) THEN
3149       fnd_message.set_name('INV', 'INV-INVALID RESERVATION QTY');
3150       fnd_msg_pub.ADD;
3151       RAISE fnd_api.g_exc_error;
3152     END IF;
3153 
3154     IF (l_debug = 1) THEN
3155       debug_print('l_net_qty ' || l_net_qty);
3156       debug_print('l_secondary_net_qty ' || l_secondary_net_qty);                         -- INVCONV
3157     END IF;
3158 
3159     -- adjust the net quantity according to atr
3160     IF  l_net_qty > 0
3161         AND l_net_qty > l_atr THEN
3162       l_net_qty  := l_atr;
3163       IF l_dual_control THEN                                                              -- INVCONV
3164 	   IF l_fulfill_base<>'S' or p_action <> 'CREATE' THEN
3165              l_secondary_net_qty  := l_satr;                                                   -- INVCONV
3166            END IF;
3167 	END IF;                                                                             -- INVCONV
3168     END IF;
3169 
3170    If (l_debug = 1) THEN
3171       debug_print('l_net_qty  ' || l_net_qty);
3172       debug_print('l_secondary_net_qty ' || l_secondary_net_qty);
3173       debug_print('l_atr ' || l_atr);
3174       debug_print('l_satr ' || l_satr);
3175     END IF;
3176 
3177 	--MUOM Fulfillment Project
3178     If l_fulfill_base='S' AND p_action = 'CREATE' THEN
3179      IF  l_secondary_net_qty > 0 THEN
3180          IF l_satr <= l_secondary_net_qty THEN
3181               l_net_qty  := l_atr;
3182               l_secondary_net_qty  := l_satr;
3183          ELSIF l_satr > l_secondary_net_qty THEN
3184               IF ( l_net_qty > l_atr ) THEN
3185                    l_net_qty  := l_atr;
3186               END IF;
3187         END IF;
3188      END IF;
3189    END IF;
3190 
3191     l_quantity_type  := inv_quantity_tree_pvt.g_qr_same_demand;
3192     --
3193     inv_quantity_tree_pvt.update_quantities(
3194       p_api_version_number         => 1.0
3195     , p_init_msg_lst               => fnd_api.g_true
3196     , x_return_status              => l_return_status
3197     , x_msg_count                  => l_msg_count
3198     , x_msg_data                   => l_msg_data
3199     , p_tree_id                    => p_tree_id
3200     , p_revision                   => p_revision
3201     , p_lot_number                 => p_lot_number
3202     , p_subinventory_code          => p_subinventory_code
3203     , p_locator_id                 => p_locator_id
3204     , p_primary_quantity           => l_net_qty
3205     , p_secondary_quantity         => l_secondary_net_qty                               -- INVCONV
3206     , p_quantity_type              => l_quantity_type
3207     , x_qoh                        => l_qoh
3208     , x_rqoh                       => l_rqoh
3209     , x_qr                         => l_qr
3210     , x_qs                         => l_qs
3211     , x_att                        => l_att
3212     , x_atr                        => l_atr
3213     , x_sqoh                       => l_sqoh                                             -- INVCONV
3214     , x_srqoh                      => l_srqoh                                            -- INVCONV
3215     , x_sqr                        => l_sqr                                              -- INVCONV
3216     , x_sqs                        => l_sqs                                              -- INVCONV
3217     , x_satt                       => l_satt                                             -- INVCONV
3218     , x_satr                       => l_satr                                             -- INVCONV
3219     , p_lpn_id                     => p_lpn_id
3220     );
3221 
3222     --
3223     IF l_return_status = fnd_api.g_ret_sts_error THEN
3224       RAISE fnd_api.g_exc_error;
3225     END IF;
3226 
3227     --
3228     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3229       RAISE fnd_api.g_exc_unexpected_error;
3230     END IF;
3231 
3232     --
3233     x_qty_changed    := l_net_qty;
3234     IF l_dual_control THEN                                                               -- INVCONV
3235       x_secondary_qty_changed    := l_secondary_net_qty;                                 -- INVCONV
3236     END IF;                                                                              -- INVCONV
3237     x_return_status  := l_return_status;
3238   EXCEPTION
3239     WHEN fnd_api.g_exc_error THEN
3240       x_return_status  := fnd_api.g_ret_sts_error;
3241     --
3242     WHEN fnd_api.g_exc_unexpected_error THEN
3243       x_return_status  := fnd_api.g_ret_sts_unexp_error;
3244     --
3245     WHEN OTHERS THEN
3246       x_return_status  := fnd_api.g_ret_sts_unexp_error;
3247 
3248       --
3249       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
3250         fnd_msg_pub.add_exc_msg(g_pkg_name, 'modify_tree_crt_del_rel');
3251       END IF;
3252   END modify_tree_crt_del_rel;
3253 
3254   --
3255   -- modify the trees for update or transfer reservation
3256   -- INVCONV - Add secondaries to parameter list
3257   PROCEDURE modify_tree_for_update_xfer(
3258     x_return_status              OUT    NOCOPY VARCHAR2
3259   , x_quantity_reserved          OUT    NOCOPY NUMBER
3260   , x_secondary_quantity_reserved OUT    NOCOPY NUMBER   --INVCONV
3261   , p_from_tree_id               IN     NUMBER
3262   , p_from_supply_source_type_id IN     NUMBER
3263   , p_from_revision              IN     VARCHAR2
3264   , p_from_lot_number            IN     VARCHAR2
3265   , p_from_subinventory_code     IN     VARCHAR2
3266   , p_from_locator_id            IN     NUMBER
3267   , p_from_lpn_id                IN     NUMBER
3268   , p_from_primary_rsv_quantity  IN     NUMBER
3269   , p_from_second_rsv_quantity   IN     NUMBER
3270   , p_from_detailed_quantity     IN     NUMBER
3271   , p_from_sec_detailed_quantity IN     NUMBER
3272   , p_to_tree_id                 IN     NUMBER
3273   , p_to_supply_source_type_id   IN     NUMBER
3274   , p_to_revision                IN     VARCHAR2
3275   , p_to_lot_number              IN     VARCHAR2
3276   , p_to_subinventory_code       IN     VARCHAR2
3277   , p_to_locator_id              IN     NUMBER
3278   , p_to_lpn_id                  IN     NUMBER
3279   , p_to_primary_rsv_quantity    IN     NUMBER
3280   , p_to_second_rsv_quantity     IN     NUMBER
3281   , p_to_detailed_quantity       IN     NUMBER
3282   , p_to_second_detailed_quantity IN    NUMBER
3283   , p_to_revision_control        IN     BOOLEAN
3284   , p_to_lot_control             IN     BOOLEAN
3285   , p_action                     IN     VARCHAR2
3286   , p_lot_divisible_flag         IN     VARCHAR2         -- INVCONV
3287   , p_partial_reservation_flag   IN     VARCHAR2 DEFAULT fnd_api.g_false
3288   , p_check_availability         IN     VARCHAR2 DEFAULT fnd_api.g_false
3289   ) IS
3290 
3291     l_return_status  VARCHAR2(1)   := fnd_api.g_ret_sts_success;
3292     l_tmp_revision   VARCHAR2(3);
3293     l_tmp_lot_number VARCHAR2(80);                                  -- BUG 4180127
3294     l_net_qty1       NUMBER;
3295     l_secondary_net_qty1   NUMBER;                                  -- INVCONV
3296     l_net_qty2       NUMBER;
3297     l_secondary_net_qty2   NUMBER;                                  -- INVCONV
3298     l_qoh            NUMBER;
3299     l_rqoh           NUMBER;
3300     l_atr            NUMBER;
3301     l_att            NUMBER;
3302     l_qr             NUMBER;
3303     l_qs             NUMBER;
3304     l_sqoh           NUMBER;                                        -- INVCONV
3305     l_srqoh          NUMBER;                                        -- INVCONV
3306     l_satr           NUMBER;                                        -- INVCONV
3307     l_satt           NUMBER;                                        -- INVCONV
3308     l_sqr            NUMBER;                                        -- INVCONV
3309     l_sqs            NUMBER;                                        -- INVCONV
3310     l_dual_control   BOOLEAN;                                       -- INVCONV
3311     l_modify_tree1   BOOLEAN;
3312     l_modify_tree2   BOOLEAN;
3313     l_msg_count      NUMBER;
3314     l_msg_data       VARCHAR2(240);
3315     l_debug number;
3316 
3317   BEGIN
3318     -- Use cache to get value for l_debug
3319     IF g_is_pickrelease_set IS NULL THEN
3320        g_is_pickrelease_set := 2;
3321        IF INV_CACHE.is_pickrelease THEN
3322           g_is_pickrelease_set := 1;
3323        END IF;
3324     END IF;
3325     IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
3326        g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
3327     END IF;
3328 
3329     l_debug := g_debug;
3330 
3331      -- INVCONV BEGIN
3332      -- Set boolean for dual control scenarios
3333      IF p_from_second_rsv_quantity is NOT NULL THEN
3334         l_dual_control := TRUE;
3335      END IF;
3336      -- INVCONV END
3337 
3338     IF p_action = 'UPDATE' THEN
3339       l_net_qty1  := -p_from_primary_rsv_quantity + NVL(p_from_detailed_quantity, 0);
3340       IF l_dual_control THEN                                        -- INVCONV
3341         l_secondary_net_qty1 :=
3342            -p_from_second_rsv_quantity + NVL(p_from_sec_detailed_quantity, 0); -- INVCONV
3343       END IF;                                                       -- INVCONV
3344     ELSE -- for transfer reservation
3345       l_net_qty1  := -p_to_primary_rsv_quantity + NVL(p_to_detailed_quantity, 0);
3346       IF l_dual_control THEN                                        -- INVCONV
3347         l_secondary_net_qty1 := -p_to_second_rsv_quantity + NVL(p_to_second_detailed_quantity, 0);  -- INVCONV
3348       END IF;                                                       -- INVCONV
3349     END IF;
3350 
3351     l_net_qty2       := p_to_primary_rsv_quantity - NVL(p_to_detailed_quantity, 0);
3352     IF l_dual_control THEN                                          -- INVCONV
3353       l_secondary_net_qty2 := p_to_second_rsv_quantity - NVL(p_to_second_detailed_quantity, 0);  -- INVCONV
3354     END IF;                                                         -- INVCONV
3355     --
3356     IF p_from_supply_source_type_id = inv_reservation_global.g_source_type_inv THEN
3357        -- INVCONV - Incorporate secondaries
3358       inv_quantity_tree_pvt.update_quantities(
3359         p_api_version_number         => 1.0
3360       , p_init_msg_lst               => fnd_api.g_true
3361       , x_return_status              => l_return_status
3362       , x_msg_count                  => l_msg_count
3363       , x_msg_data                   => l_msg_data
3364       , p_tree_id                    => p_from_tree_id
3365       , p_revision                   => p_from_revision
3366       , p_lot_number                 => p_from_lot_number
3367       , p_subinventory_code          => p_from_subinventory_code
3368       , p_locator_id                 => p_from_locator_id
3369       , p_primary_quantity           => l_net_qty1
3370       , p_secondary_quantity         => l_secondary_net_qty1
3371       , p_quantity_type              => inv_quantity_tree_pvt.g_qr_other_demand
3372       , x_qoh                        => l_qoh
3373       , x_rqoh                       => l_rqoh
3374       , x_qr                         => l_qr
3375       , x_qs                         => l_qs
3376       , x_att                        => l_att
3377       , x_atr                        => l_atr
3378       , x_sqoh                       => l_sqoh
3379       , x_srqoh                      => l_srqoh
3380       , x_sqr                        => l_sqr
3381       , x_sqs                        => l_sqs
3382       , x_satt                       => l_satt
3383       , x_satr                       => l_satr
3384       , p_lpn_id                     => p_from_lpn_id
3385       );
3386 
3387       --
3388       IF l_return_status = fnd_api.g_ret_sts_error THEN
3389         RAISE fnd_api.g_exc_error;
3390       END IF;
3391 
3392       --
3393       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3394         RAISE fnd_api.g_exc_unexpected_error;
3395       END IF;
3396 
3397       --
3398       l_modify_tree1  := TRUE;
3399     ELSE
3400       l_modify_tree1  := FALSE;
3401     END IF;
3402 
3403     --
3404     l_modify_tree2   := FALSE;
3405 
3406     IF  p_to_supply_source_type_id = inv_reservation_global.g_source_type_inv
3407         AND l_modify_tree1
3408         AND p_to_tree_id IS NOT NULL
3409         AND p_from_tree_id <> p_to_tree_id THEN
3410       l_modify_tree2  := TRUE;
3411 
3412       -- check before passing the revision, and lot number
3413       -- since the second tree might have different control
3414       -- settings
3415       IF p_to_revision_control THEN
3416         l_tmp_revision  := p_from_revision;
3417       ELSE
3418         l_tmp_revision  := NULL;
3419       END IF;
3420 
3421       --
3422       IF p_to_lot_control THEN
3423         l_tmp_lot_number  := p_from_lot_number;
3424       ELSE
3425         l_tmp_lot_number  := NULL;
3426       END IF;
3427 
3428       --
3429       -- INVCONV - Incorporate secondaries into call
3430       inv_quantity_tree_pvt.update_quantities(
3431         p_api_version_number         => 1.0
3432       , p_init_msg_lst               => fnd_api.g_true
3433       , x_return_status              => l_return_status
3434       , x_msg_count                  => l_msg_count
3435       , x_msg_data                   => l_msg_data
3436       , p_tree_id                    => p_to_tree_id
3437       , p_revision                   => l_tmp_revision
3438       , p_lot_number                 => l_tmp_lot_number
3439       , p_subinventory_code          => p_from_subinventory_code
3440       , p_locator_id                 => p_from_locator_id
3441       , p_primary_quantity           => l_net_qty1
3442       , p_secondary_quantity         => l_secondary_net_qty1
3443       , p_quantity_type              => inv_quantity_tree_pvt.g_qr_other_demand
3444       , x_qoh                        => l_qoh
3445       , x_rqoh                       => l_rqoh
3446       , x_qr                         => l_qr
3447       , x_qs                         => l_qs
3448       , x_att                        => l_att
3449       , x_atr                        => l_atr
3450       , x_sqoh                       => l_sqoh
3451       , x_srqoh                      => l_srqoh
3452       , x_sqr                        => l_sqr
3453       , x_sqs                        => l_sqs
3454       , x_satt                       => l_satt
3455       , x_satr                       => l_satr
3456       , p_lpn_id                     => p_from_lpn_id
3457       );
3458 
3459       --
3460       IF l_return_status = fnd_api.g_ret_sts_error THEN
3461         -- fail to modify in tree2
3462         -- undo the modification in tree1
3463          -- INVCONV - Incorporate secondaries into call
3464         inv_quantity_tree_pvt.update_quantities(
3465           p_api_version_number         => 1.0
3466         , p_init_msg_lst               => fnd_api.g_true
3467         , x_return_status              => l_return_status
3468         , x_msg_count                  => l_msg_count
3469         , x_msg_data                   => l_msg_data
3470         , p_tree_id                    => p_from_tree_id
3471         , p_revision                   => p_from_revision
3472         , p_lot_number                 => p_from_lot_number
3473         , p_subinventory_code          => p_from_subinventory_code
3474         , p_locator_id                 => p_from_locator_id
3475         , p_primary_quantity           => -l_net_qty1
3476         , p_secondary_quantity         => -l_secondary_net_qty1
3477         , p_quantity_type              => inv_quantity_tree_pvt.g_qr_same_demand
3478         , x_qoh                        => l_qoh
3479         , x_rqoh                       => l_rqoh
3480         , x_qr                         => l_qr
3481         , x_qs                         => l_qs
3482         , x_att                        => l_att
3483         , x_atr                        => l_atr
3484         , x_sqoh                       => l_sqoh
3485         , x_srqoh                      => l_srqoh
3486         , x_sqr                        => l_sqr
3487         , x_sqs                        => l_sqs
3488         , x_satt                       => l_satt
3489         , x_satr                       => l_satr
3490         , p_lpn_id                     => p_from_lpn_id
3491         );
3492 
3493         IF l_return_status = fnd_api.g_ret_sts_error THEN
3494           -- can not add the reservation back on the tree
3495           -- panic
3496           RAISE fnd_api.g_exc_unexpected_error;
3497         END IF;
3498 
3499         --
3500         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3501           RAISE fnd_api.g_exc_unexpected_error;
3502         END IF;
3503 
3504         --
3505         -- recover the quantity tree1 correctly, but
3506         -- since we failed to modify the tree, raise expected error
3507         RAISE fnd_api.g_exc_error;
3508       END IF;
3509 
3510       --
3511       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3512         RAISE fnd_api.g_exc_unexpected_error;
3513       END IF;
3514     END IF;
3515 --Start changes for Bug Number#3336837
3516 -- Bug Number 3447373 Removed the qty validation for transfers
3517 
3518 IF p_action='UPDATE' AND p_from_supply_source_type_id = inv_reservation_global.g_source_type_inv then
3519     IF (l_debug = 1) THEN
3520       debug_print('Validate Qty :Action Update');
3521     END IF;
3522 
3523      -- INVCONV - Incorporate secondaries into call
3524      inv_quantity_tree_pvt.query_tree(
3525       p_api_version_number         => 1.0
3526     , p_init_msg_lst               => fnd_api.g_true
3527     , x_return_status              => l_return_status
3528     , x_msg_count                  => l_msg_count
3529     , x_msg_data                   => l_msg_data
3530     , p_tree_id                    => p_from_tree_id
3531     , p_revision                   => p_from_revision
3532     , p_lot_number                 => p_from_lot_number
3533     , p_subinventory_code          => p_from_subinventory_code
3534     , p_locator_id                 => p_from_locator_id
3535     , x_qoh                        => l_qoh
3536     , x_rqoh                       => l_rqoh
3537     , x_qr                         => l_qr
3538     , x_qs                         => l_qs
3539     , x_att                        => l_att
3540     , x_atr                        => l_atr
3541     , x_sqoh                       => l_sqoh
3542     , x_srqoh                      => l_srqoh
3543     , x_sqr                        => l_sqr
3544     , x_sqs                        => l_sqs
3545     , x_satt                       => l_satt
3546     , x_satr                       => l_satr
3547     , p_lpn_id                     => p_from_lpn_id
3548     );
3549 
3550     IF l_return_status = fnd_api.g_ret_sts_error THEN
3551       RAISE fnd_api.g_exc_unexpected_error;
3552     END IF;
3553 
3554     --
3555     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3556       RAISE fnd_api.g_exc_unexpected_error;
3557     END IF;
3558 
3559     -- INVCONV BEGIN - lot indivisible quantities must be fully consumed
3560     IF p_lot_divisible_flag = 'N' THEN
3561       IF (l_debug = 1) THEN
3562          debug_print('Lot indivisible scenario so compare l_atr '||l_atr ||' with l_net_qty2 '||l_net_qty2);
3563       END IF;
3564       IF l_atr > l_net_qty2 THEN
3565         fnd_message.set_name('INV', 'INV_LOT_INDIVISIBLE_VIOLATION');
3566         fnd_msg_pub.ADD;
3567         RAISE fnd_api.g_exc_error;
3568       END IF;
3569     END IF;
3570     -- INVCONV END
3571 
3572  END IF;
3573 
3574    IF (l_debug = 1) THEN
3575       debug_print('l_atr '||l_atr);
3576       debug_print('l_rqoh '||l_rqoh);
3577       debug_print('l_net_qty2 '||l_net_qty2);
3578    END IF;
3579 
3580    -- Bug 6141096: Fail the reservation if available to reserve(ATR) is less than or equal to zero, even if the
3581    -- the partial reservation flag is set to true.
3582    IF ( p_partial_reservation_flag = fnd_api.g_true  AND l_net_qty2 > l_atr AND l_atr > 0)THEN -- Bug 6141096
3583     l_net_qty2 := l_atr;
3584     x_quantity_reserved := l_net_qty2;
3585 
3586     IF l_dual_control THEN                                          -- INVCONV
3587       l_secondary_net_qty2 := l_satr;                               -- INVCONV
3588       x_secondary_quantity_reserved := l_secondary_net_qty2;        -- INVCONV
3589     END IF;                                                         -- INVCONV
3590    ELSIF (      l_net_qty2 > l_atr
3591            AND  (p_partial_reservation_flag <> fnd_api.g_true OR l_atr <= 0) -- Bug 6141096
3592            AND  p_action='UPDATE'
3593            AND  p_check_availability = fnd_api.g_true )THEN
3594     --rollback quantity tree changes .
3595        IF l_modify_tree1 THEN
3596         -- INVCONV Incorporate secondaries
3597         inv_quantity_tree_pvt.update_quantities(
3598           p_api_version_number         => 1.0
3599         , p_init_msg_lst               => fnd_api.g_true
3600         , x_return_status              => l_return_status
3601         , x_msg_count                  => l_msg_count
3602         , x_msg_data                   => l_msg_data
3603         , p_tree_id                    => p_from_tree_id
3604         , p_revision                   => p_from_revision
3605         , p_lot_number                 => p_from_lot_number
3606         , p_subinventory_code          => p_from_subinventory_code
3607         , p_locator_id                 => p_from_locator_id
3608         , p_primary_quantity           => -l_net_qty1
3609         , p_secondary_quantity         => -l_secondary_net_qty1
3610         , p_quantity_type              => inv_quantity_tree_pvt.g_qr_same_demand
3611         , x_qoh                        => l_qoh
3612         , x_rqoh                       => l_rqoh
3613         , x_qr                         => l_qr
3614         , x_qs                         => l_qs
3615         , x_att                        => l_att
3616         , x_atr                        => l_atr
3617         , x_sqoh                       => l_sqoh
3618         , x_srqoh                      => l_srqoh
3619         , x_sqr                        => l_sqr
3620         , x_sqs                        => l_sqs
3621         , x_satt                       => l_satt
3622         , x_satr                       => l_satr
3623         , p_lpn_id                     => p_from_lpn_id
3624         );
3625         IF (l_debug = 1) THEN
3626            debug_print('return status from Rollback Tree1 changes '||l_return_status);
3627         END IF;
3628 
3629         IF l_return_status = fnd_api.g_ret_sts_error THEN
3630           -- can not add the reservation back on the tree
3631           -- panic
3632           RAISE fnd_api.g_exc_unexpected_error;
3633         END IF;
3634 
3635         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3636           RAISE fnd_api.g_exc_unexpected_error;
3637         END IF;
3638        END IF;
3639 
3640 
3641       fnd_message.set_name('INV', 'INV-INVALID RESERVATION QTY');
3642       fnd_msg_pub.ADD;
3643       RAISE fnd_api.g_exc_error;
3644     END IF;
3645 --Changed for Qty validation. Bug 3336837
3646 
3647     --
3648     -- adding the quantity to the to tree
3649     --Bug 1575930
3650     -- The from tree must also be updated with the quantity, since the
3651     --  from tree and the to tree are for the same organization and item
3652 
3653      -- INVCONV Incorporate Secondaries in call
3654     inv_quantity_tree_pvt.update_quantities(
3655       p_api_version_number         => 1.0
3656     , p_init_msg_lst               => fnd_api.g_true
3657     , x_return_status              => l_return_status
3658     , x_msg_count                  => l_msg_count
3659     , x_msg_data                   => l_msg_data
3660     , p_tree_id                    => p_from_tree_id
3661     , p_revision                   => p_from_revision
3662     , p_lot_number                 => p_from_lot_number
3663     , p_subinventory_code          => p_from_subinventory_code
3664     , p_locator_id                 => p_from_locator_id
3665     , p_primary_quantity           => l_net_qty2
3666     , p_secondary_quantity         => l_secondary_net_qty2
3667     , p_quantity_type              => inv_quantity_tree_pvt.g_qr_other_demand
3668     , x_qoh                        => l_qoh
3669     , x_rqoh                       => l_rqoh
3670     , x_qr                         => l_qr
3671     , x_qs                         => l_qs
3672     , x_att                        => l_att
3673     , x_atr                        => l_atr
3674     , x_sqoh                       => l_sqoh
3675     , x_srqoh                      => l_srqoh
3676     , x_sqr                        => l_sqr
3677     , x_sqs                        => l_sqs
3678     , x_satt                       => l_satt
3679     , x_satr                       => l_satr
3680     , p_lpn_id                     => p_from_lpn_id
3681     );
3682 
3683     IF  l_return_status = fnd_api.g_ret_sts_success
3684         AND l_modify_tree2 THEN
3685 
3686       -- INVCONV Incorporate Secondaries in call
3687       inv_quantity_tree_pvt.update_quantities(
3688         p_api_version_number         => 1.0
3689       , p_init_msg_lst               => fnd_api.g_true
3690       , x_return_status              => l_return_status
3691       , x_msg_count                  => l_msg_count
3692       , x_msg_data                   => l_msg_data
3693       , p_tree_id                    => p_to_tree_id
3694       , p_revision                   => p_to_revision
3695       , p_lot_number                 => p_to_lot_number
3696       , p_subinventory_code          => p_to_subinventory_code
3697       , p_locator_id                 => p_to_locator_id
3698       , p_primary_quantity           => l_net_qty2
3699       , p_secondary_quantity         => l_secondary_net_qty2
3700       , p_quantity_type              => inv_quantity_tree_pvt.g_qr_same_demand
3701       , x_qoh                        => l_qoh
3702       , x_rqoh                       => l_rqoh
3703       , x_qr                         => l_qr
3704       , x_qs                         => l_qs
3705       , x_att                        => l_att
3706       , x_atr                        => l_atr
3707       , x_sqoh                       => l_sqoh
3708       , x_srqoh                      => l_srqoh
3709       , x_sqr                        => l_sqr
3710       , x_sqs                        => l_sqs
3711       , x_satt                       => l_satt
3712       , x_satr                       => l_satr
3713       , p_lpn_id                     => p_to_lpn_id
3714       );
3715     END IF;
3716 
3717     IF l_return_status = fnd_api.g_ret_sts_error THEN
3718       -- fail to modify in tree2
3719       -- undo the modification in tree1 if l_modify_tree1 is true
3720       IF l_modify_tree1 THEN
3721         -- INVCONV - Incorporate secondaries
3722         inv_quantity_tree_pvt.update_quantities(
3723           p_api_version_number         => 1.0
3724         , p_init_msg_lst               => fnd_api.g_true
3725         , x_return_status              => l_return_status
3726         , x_msg_count                  => l_msg_count
3727         , x_msg_data                   => l_msg_data
3728         , p_tree_id                    => p_from_tree_id
3729         , p_revision                   => p_from_revision
3730         , p_lot_number                 => p_from_lot_number
3731         , p_subinventory_code          => p_from_subinventory_code
3732         , p_locator_id                 => p_from_locator_id
3733         , p_primary_quantity           => -l_net_qty1
3734         , p_secondary_quantity         => -l_secondary_net_qty1
3735         , p_quantity_type              => inv_quantity_tree_pvt.g_qr_same_demand
3736         , x_qoh                        => l_qoh
3737         , x_rqoh                       => l_rqoh
3738         , x_qr                         => l_qr
3739         , x_qs                         => l_qs
3740         , x_att                        => l_att
3741         , x_atr                        => l_atr
3742         , x_sqoh                       => l_sqoh
3743         , x_srqoh                      => l_srqoh
3744         , x_sqr                        => l_sqr
3745         , x_sqs                        => l_sqs
3746         , x_satt                       => l_satt
3747         , x_satr                       => l_satr
3748         , p_lpn_id                     => p_from_lpn_id
3749         );
3750 
3751         IF l_return_status = fnd_api.g_ret_sts_error THEN
3752           -- can not add the reservation back on the tree
3753           -- panic
3754           RAISE fnd_api.g_exc_unexpected_error;
3755         END IF;
3756 
3757         --
3758         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3759           RAISE fnd_api.g_exc_unexpected_error;
3760         END IF;
3761 
3762         --
3763         IF l_modify_tree2 THEN
3764           -- INVCONV - Incorporate secondaries
3765           inv_quantity_tree_pvt.update_quantities(
3766             p_api_version_number         => 1.0
3767           , p_init_msg_lst               => fnd_api.g_true
3768           , x_return_status              => l_return_status
3769           , x_msg_count                  => l_msg_count
3770           , x_msg_data                   => l_msg_data
3771           , p_tree_id                    => p_to_tree_id
3772           , p_revision                   => l_tmp_revision
3773           , p_lot_number                 => l_tmp_lot_number
3774           , p_subinventory_code          => p_from_subinventory_code
3775           , p_locator_id                 => p_from_locator_id
3776           , p_primary_quantity           => -l_net_qty1
3777           , p_secondary_quantity         => -l_secondary_net_qty1
3778           , p_quantity_type              => inv_quantity_tree_pvt.g_qr_other_demand
3779           , x_qoh                        => l_qoh
3780           , x_rqoh                       => l_rqoh
3781           , x_qr                         => l_qr
3782           , x_qs                         => l_qs
3783           , x_att                        => l_att
3784           , x_atr                        => l_atr
3785           , x_sqoh                       => l_sqoh
3786           , x_srqoh                      => l_srqoh
3787           , x_sqr                        => l_sqr
3788           , x_sqs                        => l_sqs
3789           , x_satt                       => l_satt
3790           , x_satr                       => l_satr
3791           , p_lpn_id                     => p_from_lpn_id
3792           );
3793 
3794           IF l_return_status = fnd_api.g_ret_sts_error THEN
3795             -- can not add the reservation back on the tree
3796             -- panic
3797             RAISE fnd_api.g_exc_unexpected_error;
3798           END IF;
3799 
3800           --
3801           IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3802             RAISE fnd_api.g_exc_unexpected_error;
3803           END IF;
3804         END IF;
3805 
3806         -- recover the quantity tree1 correctly, but
3807         -- since we failed to modify the tree, raise expected error
3808         RAISE fnd_api.g_exc_error;
3809       END IF;
3810 
3811       --
3812       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3813         RAISE fnd_api.g_exc_unexpected_error;
3814       END IF;
3815     END IF;
3816 
3817     x_return_status  := l_return_status;
3818     x_quantity_reserved := l_net_qty2;
3819 
3820     IF l_dual_control THEN                                          -- INVCONV
3821       x_secondary_quantity_reserved := l_secondary_net_qty2;        -- INVCONV
3822     END IF;                                                         -- INVCONV
3823 
3824   --
3825   EXCEPTION
3826     WHEN fnd_api.g_exc_error THEN
3827       x_return_status  := fnd_api.g_ret_sts_error;
3828     --
3829     WHEN fnd_api.g_exc_unexpected_error THEN
3830       x_return_status  := fnd_api.g_ret_sts_unexp_error;
3831     --
3832     WHEN OTHERS THEN
3833       x_return_status  := fnd_api.g_ret_sts_unexp_error;
3834 
3835       --
3836       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
3837         fnd_msg_pub.add_exc_msg(g_pkg_name, 'modify_tree_for_update_xfer');
3838       END IF;
3839   END modify_tree_for_update_xfer;
3840 
3841   --
3842   -- Procedure
3843   --   query_resevation
3844   -- Description
3845   --   This  procedure returns all reservations that satisfy the user
3846   --   specified criteria.
3847   PROCEDURE query_reservation(
3848     p_api_version_number        IN     NUMBER
3849   , p_init_msg_lst              IN     VARCHAR2 DEFAULT fnd_api.g_false
3850   , x_return_status             OUT    NOCOPY VARCHAR2
3851   , x_msg_count                 OUT    NOCOPY NUMBER
3852   , x_msg_data                  OUT    NOCOPY VARCHAR2
3853   , p_query_input               IN     inv_reservation_global.mtl_reservation_rec_type
3854   , p_lock_records              IN     VARCHAR2 DEFAULT fnd_api.g_false
3855   , p_sort_by_req_date          IN     NUMBER DEFAULT inv_reservation_global.g_query_no_sort
3856   , p_cancel_order_mode         IN     NUMBER DEFAULT inv_reservation_global.g_cancel_order_no
3857   , x_mtl_reservation_tbl       OUT    NOCOPY inv_reservation_global.mtl_reservation_tbl_type
3858   , x_mtl_reservation_tbl_count OUT    NOCOPY NUMBER
3859   , x_error_code                OUT    NOCOPY NUMBER
3860   ) IS
3861     l_api_version_number CONSTANT NUMBER                                          := 1.0;
3862     l_api_name           CONSTANT VARCHAR2(30)                                    := 'Query_Reservation';
3863     l_return_status               VARCHAR2(1)                                     := fnd_api.g_ret_sts_success;
3864     l_counter                     INTEGER;
3865     l_rsv_rec                     inv_reservation_global.mtl_reservation_rec_type;
3866     l_sort_stmt                   VARCHAR2(2000);
3867     l_lock_stmt                   VARCHAR2(60);
3868     -- Bug 4881317
3869     l_qry_stmt                    VARCHAR2(15000);
3870     l_cursor_ref                  query_cur_ref_type;
3871     l_miss_num                    NUMBER                                          := fnd_api.g_miss_num;
3872     l_miss_char                   VARCHAR2(1)                                     := fnd_api.g_miss_char;
3873     l_miss_date                   DATE                                            := fnd_api.g_miss_date;
3874     l_update                      BOOLEAN                                         := FALSE;
3875     l_sort_default                BOOLEAN                                         := FALSE;
3876     l_res_cursor                  BOOLEAN                                         := FALSE;
3877     l_demand_cursor               BOOLEAN := FALSE;
3878     l_debug NUMBER := 0;
3879 
3880     -- Bug 3560916: When multiple processes are accessing the same
3881     -- reservation record, the query reservation is failing because of
3882     -- NOLOCK. This happens from pick confirm while transferring
3883     -- reservations. Removed the nowait from c_res_id_update cursor.
3884 
3885     -- INVCONV - Incorporate secondary quantities
3886     CURSOR c_res_id_update IS
3887        SELECT
3888 	 reservation_id
3889 	 , requirement_date
3890 	 , organization_id
3891 	 , inventory_item_id
3892 	 , demand_source_type_id
3893 	 , demand_source_name
3894 	 , demand_source_header_id
3895 	 , demand_source_line_id
3896 	 , demand_source_delivery
3897 	 , primary_uom_code
3898 	 , primary_uom_id
3899 	 , secondary_uom_code
3900 	 , secondary_uom_id
3901 	 , reservation_uom_code
3902 	 , reservation_uom_id
3903 	 , reservation_quantity
3904 	 , primary_reservation_quantity
3905 	 , secondary_reservation_quantity
3906 	 , detailed_quantity
3907 	 , secondary_detailed_quantity
3908 	 , autodetail_group_id
3909 	 , external_source_code
3910 	 , external_source_line_id
3911 	 , supply_source_type_id
3912 	 , supply_source_header_id
3913 	 , supply_source_line_id
3914 	 , supply_source_name
3915 	 , supply_source_line_detail
3916 	 , revision
3917 	 , subinventory_code
3918 	 , subinventory_id
3919 	 , locator_id
3920 	 , lot_number
3921 	 , lot_number_id
3922 	 , pick_slip_number
3923 	 , lpn_id
3924 	 , attribute_category
3925 	 , attribute1
3926 	 , attribute2
3927 	 , attribute3
3928 	 , attribute4
3929 	 , attribute5
3930 	 , attribute6
3931 	 , attribute7
3932 	 , attribute8
3933 	 , attribute9
3934 	 , attribute10
3935 	 , attribute11
3936 	 , attribute12
3937 	 , attribute13
3938 	 , attribute14
3939 	 , attribute15
3940 	 , ship_ready_flag
3941 	 , staged_flag
3942 	 /**** {{ R12 Enhanced reservations code changes. Adding new
3943 	 -- columns to query reservations. id passed for update}}****/
3944 	 , crossdock_flag
3945 	 , crossdock_criteria_id
3946 	 , demand_source_line_detail
3947 	 , serial_reservation_quantity
3948 	 , supply_receipt_date
3949 	 , demand_ship_date
3950 	 , project_id
3951 	 , task_id
3952 	 , orig_supply_source_type_id
3953 	 , orig_supply_source_header_id
3954 	 , orig_supply_source_line_id
3955 	 , orig_supply_source_line_detail
3956 	 , orig_demand_source_type_id
3957 	 , orig_demand_source_header_id
3958 	 , orig_demand_source_line_id
3959 	 , orig_demand_source_line_detail
3960 	 , serial_number
3961 	 /***** End R12 ***/
3962 	 FROM mtl_reservations
3963 	 WHERE reservation_id = p_query_input.reservation_id
3964 	 FOR UPDATE --NOWAIT
3965 	 ORDER BY NVL(revision, ' '), NVL(lot_number, ' '), NVL(subinventory_code, ' '), NVL(locator_id, 0);
3966 
3967     -- INVCONV - Incorporate secondary quantities
3968     CURSOR c_res_id IS
3969        SELECT
3970 	 reservation_id
3971 	 , requirement_date
3972 	 , organization_id
3973 	 , inventory_item_id
3974 	 , demand_source_type_id
3975 	 , demand_source_name
3976 	 , demand_source_header_id
3977 	 , demand_source_line_id
3978 	 , demand_source_delivery
3979 	 , primary_uom_code
3980 	 , primary_uom_id
3981 	 , secondary_uom_code
3982 	 , secondary_uom_id
3983 	 , reservation_uom_code
3984 	 , reservation_uom_id
3985 	 , reservation_quantity
3986 	 , primary_reservation_quantity
3987 	 , secondary_reservation_quantity
3988 	 , detailed_quantity
3989 	 , secondary_detailed_quantity
3990 	 , autodetail_group_id
3991 	 , external_source_code
3992 	 , external_source_line_id
3993 	 , supply_source_type_id
3994 	 , supply_source_header_id
3995 	 , supply_source_line_id
3996 	 , supply_source_name
3997 	 , supply_source_line_detail
3998 	 , revision
3999 	 , subinventory_code
4000 	 , subinventory_id
4001 	 , locator_id
4002 	 , lot_number
4003 	 , lot_number_id
4004 	 , pick_slip_number
4005 	 , lpn_id
4006 	 , attribute_category
4007 	 , attribute1
4008 	 , attribute2
4009 	 , attribute3
4010 	 , attribute4
4011 	 , attribute5
4012 	 , attribute6
4013 	 , attribute7
4014 	 , attribute8
4015 	 , attribute9
4016 	 , attribute10
4017 	 , attribute11
4018 	 , attribute12
4019 	 , attribute13
4020 	 , attribute14
4021 	 , attribute15
4022 	 , ship_ready_flag
4023 	 , staged_flag
4024 	 /**** {{ R12 Enhanced reservations code changes. Adding new
4025 	 -- columns to query reservations. id passed no update}}****/
4026 	 , crossdock_flag
4027 	 , crossdock_criteria_id
4028 	 , demand_source_line_detail
4029 	 , serial_reservation_quantity
4030 	 , supply_receipt_date
4031 	 , demand_ship_date
4032 	 , project_id
4033 	 , task_id
4034 	 , orig_supply_source_type_id
4035 	 , orig_supply_source_header_id
4036 	 , orig_supply_source_line_id
4037 	 , orig_supply_source_line_detail
4038 	 , orig_demand_source_type_id
4039 	 , orig_demand_source_header_id
4040 	 , orig_demand_source_line_id
4041 	 , orig_demand_source_line_detail
4042 	 , serial_number
4043 	 /***** End R12 ***/
4044 	 FROM mtl_reservations
4045          WHERE reservation_id = p_query_input.reservation_id
4046 	 ORDER BY NVL(revision, ' '), NVL(lot_number, ' '), NVL(subinventory_code, ' '), NVL(locator_id, 0);
4047 
4048     -- INVCONV - Incorporate secondary quantities
4049     --modified the order of conditions and removed unnecessary null conditions to improve the performance as part of bug 7343600
4050     CURSOR c_demand_update IS
4051        SELECT
4052 	 reservation_id
4053 	 , requirement_date
4054 	 , organization_id
4055 	 , inventory_item_id
4056 	 , demand_source_type_id
4057 	 , demand_source_name
4058 	 , demand_source_header_id
4059 	 , demand_source_line_id
4060 	 , demand_source_delivery
4061 	 , primary_uom_code
4062 	 , primary_uom_id
4063 	 , secondary_uom_code
4064 	 , secondary_uom_id
4065 	 , reservation_uom_code
4066 	 , reservation_uom_id
4067 	 , reservation_quantity
4068 	 , primary_reservation_quantity
4069 	 , secondary_reservation_quantity
4070 	 , detailed_quantity
4071 	 , secondary_detailed_quantity
4072 	 , autodetail_group_id
4073 	 , external_source_code
4074 	 , external_source_line_id
4075 	 , supply_source_type_id
4076 	 , supply_source_header_id
4077 	 , supply_source_line_id
4078 	 , supply_source_name
4079 	 , supply_source_line_detail
4080 	 , revision
4081 	 , subinventory_code
4082 	 , subinventory_id
4083 	 , locator_id
4084 	 , lot_number
4085 	 , lot_number_id
4086 	 , pick_slip_number
4087 	 , lpn_id
4088 	 , attribute_category
4089 	 , attribute1
4090 	 , attribute2
4091 	 , attribute3
4092 	 , attribute4
4093 	 , attribute5
4094 	 , attribute6
4095 	 , attribute7
4096 	 , attribute8
4097 	 , attribute9
4098 	 , attribute10
4099 	 , attribute11
4100 	 , attribute12
4101 	 , attribute13
4102 	 , attribute14
4103 	 , attribute15
4104 	 , ship_ready_flag
4105 	 , staged_flag
4106 	  /**** {{ R12 Enhanced reservations code changes. Adding new
4107 	 -- columns to query reservations. demand passed }}****/
4108 	 , crossdock_flag
4109 	 , crossdock_criteria_id
4110 	 , demand_source_line_detail
4111 	 , serial_reservation_quantity
4112 	 , supply_receipt_date
4113 	 , demand_ship_date
4114 	 , project_id
4115 	 , task_id
4116 	 , orig_supply_source_type_id
4117 	 , orig_supply_source_header_id
4118 	 , orig_supply_source_line_id
4119 	 , orig_supply_source_line_detail
4120 	 , orig_demand_source_type_id
4121 	 , orig_demand_source_header_id
4122 	 , orig_demand_source_line_id
4123 	 , orig_demand_source_line_detail
4124 	 , serial_number
4125 	 /***** End R12 ***/
4126 	 FROM mtl_reservations
4127 	 WHERE demand_source_line_id = p_query_input.demand_source_line_id
4128 	     AND (p_query_input.lpn_id = l_miss_num
4129                   OR p_query_input.lpn_id IS NULL
4130                      AND lpn_id IS NULL
4131                   OR p_query_input.lpn_id = lpn_id
4132                  )
4133 	     AND
4134 		 (p_query_input.organization_id = l_miss_num
4135                   OR organization_id = p_query_input.organization_id
4136                  )
4137              AND (p_query_input.inventory_item_id = l_miss_num
4138                    OR inventory_item_id = p_query_input.inventory_item_id
4139                  )
4140 	     AND (p_query_input.requirement_date = l_miss_date
4141                   OR  requirement_date = p_query_input.requirement_date
4142                  )
4143              AND (p_query_input.demand_source_type_id = l_miss_num
4144                   OR demand_source_type_id = p_query_input.demand_source_type_id
4145                  )
4146              AND (p_query_input.demand_source_header_id = l_miss_num
4147                   OR p_query_input.demand_source_header_id IS NULL
4148                      AND demand_source_header_id IS NULL
4149                   OR p_query_input.demand_source_header_id = demand_source_header_id
4150                  )
4151              AND (p_query_input.demand_source_name = l_miss_char
4152                   OR p_query_input.demand_source_name IS NULL
4153                      AND demand_source_name IS NULL
4154                   OR p_query_input.demand_source_name = demand_source_name
4155                  )
4156              AND (p_query_input.demand_source_delivery = l_miss_num
4157                   OR p_query_input.demand_source_delivery IS NULL
4158                      AND demand_source_delivery IS NULL
4159                   OR p_query_input.demand_source_delivery = demand_source_delivery
4160                  )
4161              AND (p_query_input.primary_uom_code = l_miss_char
4162                   OR p_query_input.primary_uom_code IS NULL
4163                      AND primary_uom_code IS NULL
4164                   OR p_query_input.primary_uom_code = primary_uom_code
4165                  )
4166              AND (p_query_input.primary_uom_id = l_miss_num
4167                   OR p_query_input.primary_uom_id IS NULL
4168                      AND primary_uom_id IS NULL
4169                   OR p_query_input.primary_uom_id = primary_uom_id
4170                  )
4171 
4172               -- INVCONV BEGIN
4173              AND (p_query_input.secondary_uom_code = l_miss_char
4174                   OR p_query_input.secondary_uom_code IS NULL
4175                      AND secondary_uom_code IS NULL
4176                   OR p_query_input.secondary_uom_code = secondary_uom_code
4177                  )
4178              AND (p_query_input.secondary_uom_id = l_miss_num
4179                   OR p_query_input.secondary_uom_id IS NULL
4180                      AND secondary_uom_id IS NULL
4181                   OR p_query_input.secondary_uom_id = secondary_uom_id
4182                  )
4183              -- INVCONV END
4184              AND (p_query_input.reservation_uom_code = l_miss_char
4185                   OR p_query_input.reservation_uom_code IS NULL
4186                      AND reservation_uom_code IS NULL
4187                   OR p_query_input.reservation_uom_code = reservation_uom_code
4188                  )
4189              AND (p_query_input.reservation_uom_id = l_miss_num
4190                   OR p_query_input.reservation_uom_id IS NULL
4191                      AND reservation_uom_id IS NULL
4192                   OR p_query_input.reservation_uom_id = reservation_uom_id
4193                  )
4194              AND (p_query_input.autodetail_group_id = l_miss_num
4195                   OR p_query_input.autodetail_group_id IS NULL
4196                      AND autodetail_group_id IS NULL
4197                   OR p_query_input.autodetail_group_id = autodetail_group_id
4198                  )
4199              AND (p_query_input.external_source_code = l_miss_char
4200                   OR p_query_input.external_source_code IS NULL
4201                      AND external_source_code IS NULL
4202                   OR p_query_input.external_source_code = external_source_code
4203                  )
4204              AND (p_query_input.external_source_line_id = l_miss_num
4205                   OR p_query_input.external_source_line_id IS NULL
4206                      AND external_source_line_id IS NULL
4207                   OR p_query_input.external_source_line_id = external_source_line_id
4208                  )
4209              AND (p_query_input.supply_source_type_id = l_miss_num
4210                   OR supply_source_type_id = p_query_input.supply_source_type_id
4211                  )
4212              AND (p_query_input.supply_source_header_id = l_miss_num
4213                   OR p_query_input.supply_source_header_id IS NULL
4214                      AND supply_source_header_id IS NULL
4215                   OR p_query_input.supply_source_header_id = supply_source_header_id
4216                  )
4217              AND (p_query_input.supply_source_line_id = l_miss_num
4218                   OR p_query_input.supply_source_line_id IS NULL
4219                      AND supply_source_line_id IS NULL
4220                   OR p_query_input.supply_source_line_id = supply_source_line_id
4221                  )
4222              AND (p_query_input.supply_source_name = l_miss_char
4223                   OR p_query_input.supply_source_name IS NULL
4224                      AND supply_source_name IS NULL
4225                   OR p_query_input.supply_source_name = supply_source_name
4226                  )
4227              AND (p_query_input.supply_source_line_detail = l_miss_num
4228                   OR p_query_input.supply_source_line_detail IS NULL
4229                      AND supply_source_line_detail IS NULL
4230                   OR p_query_input.supply_source_line_detail = supply_source_line_detail
4231                  )
4232              AND (p_query_input.revision = l_miss_char
4233                   OR p_query_input.revision IS NULL
4234                      AND revision IS NULL
4235                   OR p_query_input.revision = revision
4236                  )
4237              AND (p_query_input.subinventory_code = l_miss_char
4238                   OR p_query_input.subinventory_code IS NULL
4239                      AND subinventory_code IS NULL
4240                   OR p_query_input.subinventory_code = subinventory_code
4241                  )
4242              AND (p_query_input.subinventory_id = l_miss_num
4243                   OR p_query_input.subinventory_id IS NULL
4244                      AND subinventory_id IS NULL
4245                   OR p_query_input.subinventory_id = subinventory_id
4246                  )
4247              AND (p_query_input.locator_id = l_miss_num
4248                   OR p_query_input.locator_id IS NULL
4249                      AND locator_id IS NULL
4250                   OR p_query_input.locator_id = locator_id
4251                  )
4252              AND (p_query_input.lot_number = l_miss_char
4253                   OR p_query_input.lot_number IS NULL
4254                      AND lot_number IS NULL
4255                   OR p_query_input.lot_number = lot_number
4256                  )
4257              AND (p_query_input.lot_number_id = l_miss_num
4258                   OR p_query_input.lot_number_id IS NULL
4259                      AND lot_number_id IS NULL
4260                   OR p_query_input.lot_number_id = lot_number_id
4261                  )
4262              AND (p_query_input.ship_ready_flag = l_miss_num
4263                   OR (p_query_input.ship_ready_flag IS NULL OR p_query_input.ship_ready_flag = 2)
4264                      AND (ship_ready_flag IS NULL OR ship_ready_flag = 2)
4265                   OR p_query_input.ship_ready_flag = ship_ready_flag
4266                  )
4267              AND (p_query_input.staged_flag = l_miss_char
4268                   OR (p_query_input.staged_flag IS NULL OR p_query_input.staged_flag = 'N')
4269                      AND (staged_flag IS NULL OR staged_flag = 'N')
4270                   OR p_query_input.staged_flag = staged_flag
4271                  )
4272              AND (p_query_input.attribute_category = l_miss_char
4273                   OR p_query_input.attribute_category IS NULL
4274                      AND attribute_category IS NULL
4275                   OR p_query_input.attribute_category = attribute_category
4276                  )
4277              AND (p_query_input.attribute1 = l_miss_char
4278                   OR p_query_input.attribute1 IS NULL
4279                      AND attribute1 IS NULL
4280                   OR p_query_input.attribute1 = attribute1
4281                  )
4282              AND (p_query_input.attribute2 = l_miss_char
4283                   OR p_query_input.attribute2 IS NULL
4284                      AND attribute2 IS NULL
4285                   OR p_query_input.attribute2 = attribute2
4286                  )
4287              AND (p_query_input.attribute3 = l_miss_char
4288                   OR p_query_input.attribute3 IS NULL
4289                      AND attribute3 IS NULL
4290                   OR p_query_input.attribute3 = attribute3
4291                  )
4292              AND (p_query_input.attribute4 = l_miss_char
4293                   OR p_query_input.attribute4 IS NULL
4294                      AND attribute4 IS NULL
4295                   OR p_query_input.attribute4 = attribute4
4296                  )
4297              AND (p_query_input.attribute5 = l_miss_char
4298                   OR p_query_input.attribute5 IS NULL
4299                      AND attribute5 IS NULL
4300                   OR p_query_input.attribute5 = attribute5
4301                  )
4302              AND (p_query_input.attribute6 = l_miss_char
4303                   OR p_query_input.attribute6 IS NULL
4304                      AND attribute6 IS NULL
4305                   OR p_query_input.attribute6 = attribute6
4306                  )
4307              AND (p_query_input.attribute7 = l_miss_char
4308                   OR p_query_input.attribute7 IS NULL
4309                      AND attribute7 IS NULL
4310                   OR p_query_input.attribute7 = attribute7
4311                  )
4312              AND (p_query_input.attribute8 = l_miss_char
4313                   OR p_query_input.attribute8 IS NULL
4314                      AND attribute8 IS NULL
4315                   OR p_query_input.attribute8 = attribute8
4316                  )
4317              AND (p_query_input.attribute9 = l_miss_char
4318                   OR p_query_input.attribute9 IS NULL
4319                      AND attribute9 IS NULL
4320                   OR p_query_input.attribute9 = attribute9
4321                  )
4322              AND (p_query_input.attribute10 = l_miss_char
4323                   OR p_query_input.attribute10 IS NULL
4324                      AND attribute10 IS NULL
4325                   OR p_query_input.attribute10 = attribute10
4326                  )
4327              AND (p_query_input.attribute11 = l_miss_char
4328                   OR p_query_input.attribute11 IS NULL
4329                      AND attribute11 IS NULL
4330                   OR p_query_input.attribute11 = attribute11
4331                  )
4332              AND (p_query_input.attribute12 = l_miss_char
4333                   OR p_query_input.attribute12 IS NULL
4334                      AND attribute12 IS NULL
4335                   OR p_query_input.attribute12 = attribute12
4336                  )
4337              AND (p_query_input.attribute13 = l_miss_char
4338                   OR p_query_input.attribute13 IS NULL
4339                      AND attribute13 IS NULL
4340                   OR p_query_input.attribute13 = attribute13
4341                  )
4342              AND (p_query_input.attribute14 = l_miss_char
4343                   OR p_query_input.attribute14 IS NULL
4344                      AND attribute14 IS NULL
4345                   OR p_query_input.attribute14 = attribute14
4346                  )
4347              AND (p_query_input.attribute15 = l_miss_char
4348                   OR p_query_input.attribute15 IS NULL
4349                      AND attribute15 IS NULL
4350                   OR p_query_input.attribute15 = attribute15
4351 		  )
4352 	 /**** {{ R12 Enhanced reservations code changes. query reservation
4353 		       -- where clause}}****/
4354 	AND (p_query_input.crossdock_flag = l_miss_char
4355                   OR p_query_input.crossdock_flag IS NULL
4356                      AND crossdock_flag IS NULL
4357                   OR p_query_input.crossdock_flag = crossdock_flag
4358 	     )
4359 	AND (p_query_input.crossdock_criteria_id = l_miss_num
4360                   OR p_query_input.crossdock_criteria_id IS NULL
4361                      AND crossdock_criteria_id IS NULL
4362                   OR p_query_input.crossdock_criteria_id = crossdock_criteria_id
4363 	)
4364 	AND (p_query_input.demand_source_line_detail = l_miss_num
4365                   OR p_query_input.demand_source_line_detail IS NULL
4366                      AND demand_source_line_detail IS NULL
4367                   OR p_query_input.demand_source_line_detail = demand_source_line_detail
4368 	)
4369 	AND (p_query_input.supply_receipt_date = l_miss_date
4370                   OR p_query_input.supply_receipt_date IS NULL
4371                      AND supply_receipt_date IS NULL
4372                   OR p_query_input.supply_receipt_date = supply_receipt_date
4373 	)
4374 	AND (p_query_input.demand_ship_date = l_miss_date
4375                   OR p_query_input.demand_ship_date IS NULL
4376                      AND demand_ship_date IS NULL
4377                   OR p_query_input.demand_ship_date = demand_ship_date
4378 	)
4379 	AND (p_query_input.project_id = l_miss_num
4380                   OR p_query_input.project_id IS NULL
4381                      AND project_id IS NULL
4382                   OR p_query_input.project_id = project_id
4383 	)
4384 	AND (p_query_input.task_id = l_miss_num
4385                   OR p_query_input.task_id IS NULL
4386                      AND task_id IS NULL
4387                   OR p_query_input.task_id = task_id
4388 	)
4389 	 /***** End R12 ***/
4390       FOR UPDATE NOWAIT
4391         ORDER BY NVL(revision, ' '), NVL(lot_number, ' '), NVL(subinventory_code, ' '), NVL(locator_id, 0);
4392 
4393     -- INVCONV - Incorporate secondary quantities
4394     CURSOR c_demand IS
4395        SELECT
4396 	 reservation_id
4397 	 , requirement_date
4398 	 , organization_id
4399 	 , inventory_item_id
4400 	 , demand_source_type_id
4401 	 , demand_source_name
4402 	 , demand_source_header_id
4403 	 , demand_source_line_id
4404 	 , demand_source_delivery
4405 	 , primary_uom_code
4406 	 , primary_uom_id
4407 	 , secondary_uom_code
4408 	 , secondary_uom_id
4409 	 , reservation_uom_code
4410 	 , reservation_uom_id
4411 	 , reservation_quantity
4412 	 , primary_reservation_quantity
4413 	 , secondary_reservation_quantity
4414 	 , detailed_quantity
4415 	 , secondary_detailed_quantity
4416 	 , autodetail_group_id
4417 	 , external_source_code
4418 	 , external_source_line_id
4419 	 , supply_source_type_id
4420 	 , supply_source_header_id
4421 	 , supply_source_line_id
4422 	 , supply_source_name
4423 	 , supply_source_line_detail
4424 	 , revision
4425 	 , subinventory_code
4426 	 , subinventory_id
4427 	 , locator_id
4428 	 , lot_number
4429 	 , lot_number_id
4430 	 , pick_slip_number
4431 	 , lpn_id
4432 	 , attribute_category
4433 	 , attribute1
4434 	 , attribute2
4435 	 , attribute3
4436 	 , attribute4
4437 	 , attribute5
4438 	 , attribute6
4439 	 , attribute7
4440 	 , attribute8
4441 	 , attribute9
4442 	 , attribute10
4443 	 , attribute11
4444 	 , attribute12
4445 	 , attribute13
4446 	 , attribute14
4447 	 , attribute15
4448 	 , ship_ready_flag
4449 	 , staged_flag
4450 	  /**** {{ R12 Enhanced reservations code changes. Adding new
4451 	 -- columns to query reservations. demand passed }}****/
4452 	 , crossdock_flag
4453 	 , crossdock_criteria_id
4454 	 , demand_source_line_detail
4455 	 , serial_reservation_quantity
4456 	 , supply_receipt_date
4457 	 , demand_ship_date
4458 	 , project_id
4459 	 , task_id
4460 	 , orig_supply_source_type_id
4461 	 , orig_supply_source_header_id
4462 	 , orig_supply_source_line_id
4463 	 , orig_supply_source_line_detail
4464 	 , orig_demand_source_type_id
4465 	 , orig_demand_source_header_id
4466 	 , orig_demand_source_line_id
4467 	 , orig_demand_source_line_detail
4468 	 , serial_number
4469 	 /***** End R12 ***/
4470 	 FROM mtl_reservations
4471          WHERE demand_source_line_id = p_query_input.demand_source_line_id
4472            AND (p_query_input.requirement_date = l_miss_date
4473                 OR p_query_input.requirement_date IS NULL
4474                    AND requirement_date IS NULL
4475                 OR p_query_input.requirement_date = requirement_date
4476                )
4477            AND (p_query_input.organization_id = l_miss_num
4478                 OR p_query_input.organization_id IS NULL
4479                    AND organization_id IS NULL
4480                 OR p_query_input.organization_id = organization_id
4481                )
4482            AND (p_query_input.inventory_item_id = l_miss_num
4483                 OR p_query_input.inventory_item_id IS NULL
4484                    AND inventory_item_id IS NULL
4485                 OR p_query_input.inventory_item_id = inventory_item_id
4486                )
4487            AND (p_query_input.demand_source_type_id = l_miss_num
4488                 OR p_query_input.demand_source_type_id IS NULL
4489                    AND demand_source_type_id IS NULL
4490                 OR p_query_input.demand_source_type_id = demand_source_type_id
4491                )
4492            AND (p_query_input.demand_source_header_id = l_miss_num
4493                 OR p_query_input.demand_source_header_id IS NULL
4494                    AND demand_source_header_id IS NULL
4495                 OR p_query_input.demand_source_header_id = demand_source_header_id
4496                )
4497            AND (p_query_input.demand_source_name = l_miss_char
4498                 OR p_query_input.demand_source_name IS NULL
4499                    AND demand_source_name IS NULL
4500                 OR p_query_input.demand_source_name = demand_source_name
4501                )
4502            AND (p_query_input.demand_source_delivery = l_miss_num
4503                 OR p_query_input.demand_source_delivery IS NULL
4504                    AND demand_source_delivery IS NULL
4505                 OR p_query_input.demand_source_delivery = demand_source_delivery
4506                )
4507            AND (p_query_input.primary_uom_code = l_miss_char
4508                 OR p_query_input.primary_uom_code IS NULL
4509                    AND primary_uom_code IS NULL
4510                 OR p_query_input.primary_uom_code = primary_uom_code
4511                )
4512            AND (p_query_input.primary_uom_id = l_miss_num
4513                 OR p_query_input.primary_uom_id IS NULL
4514                    AND primary_uom_id IS NULL
4515                 OR p_query_input.primary_uom_id = primary_uom_id
4516                )
4517 
4518            -- INVCONV BEGIN
4519            AND (p_query_input.secondary_uom_code = l_miss_char
4520                 OR p_query_input.secondary_uom_code IS NULL
4521                    AND secondary_uom_code IS NULL
4522                 OR p_query_input.secondary_uom_code = secondary_uom_code
4523                )
4524            AND (p_query_input.secondary_uom_id = l_miss_num
4525                 OR p_query_input.secondary_uom_id IS NULL
4526                    AND secondary_uom_id IS NULL
4527                 OR p_query_input.secondary_uom_id = secondary_uom_id
4528                )
4529            -- INVCONV END
4530            AND (p_query_input.reservation_uom_code = l_miss_char
4531                 OR p_query_input.reservation_uom_code IS NULL
4532                    AND reservation_uom_code IS NULL
4533                 OR p_query_input.reservation_uom_code = reservation_uom_code
4534                )
4535            AND (p_query_input.reservation_uom_id = l_miss_num
4536                 OR p_query_input.reservation_uom_id IS NULL
4537                    AND reservation_uom_id IS NULL
4538                 OR p_query_input.reservation_uom_id = reservation_uom_id
4539                )
4540            AND (p_query_input.autodetail_group_id = l_miss_num
4541                 OR p_query_input.autodetail_group_id IS NULL
4542                    AND autodetail_group_id IS NULL
4543                 OR p_query_input.autodetail_group_id = autodetail_group_id
4544                )
4545            AND (p_query_input.external_source_code = l_miss_char
4546                 OR p_query_input.external_source_code IS NULL
4547                    AND external_source_code IS NULL
4548                 OR p_query_input.external_source_code = external_source_code
4549                )
4550            AND (p_query_input.external_source_line_id = l_miss_num
4551                 OR p_query_input.external_source_line_id IS NULL
4552                    AND external_source_line_id IS NULL
4553                 OR p_query_input.external_source_line_id = external_source_line_id
4554                )
4555            AND (p_query_input.supply_source_type_id = l_miss_num
4556                 OR p_query_input.supply_source_type_id IS NULL
4557                    AND supply_source_type_id IS NULL
4558                 OR p_query_input.supply_source_type_id = supply_source_type_id
4559                )
4560            AND (p_query_input.supply_source_header_id = l_miss_num
4561                 OR p_query_input.supply_source_header_id IS NULL
4562                    AND supply_source_header_id IS NULL
4563                 OR p_query_input.supply_source_header_id = supply_source_header_id
4564                )
4565            AND (p_query_input.supply_source_line_id = l_miss_num
4566                 OR p_query_input.supply_source_line_id IS NULL
4567                    AND supply_source_line_id IS NULL
4568                 OR p_query_input.supply_source_line_id = supply_source_line_id
4569                )
4570            AND (p_query_input.supply_source_name = l_miss_char
4571                 OR p_query_input.supply_source_name IS NULL
4572                    AND supply_source_name IS NULL
4573                 OR p_query_input.supply_source_name = supply_source_name
4574                )
4575            AND (p_query_input.supply_source_line_detail = l_miss_num
4576                 OR p_query_input.supply_source_line_detail IS NULL
4577                    AND supply_source_line_detail IS NULL
4578                 OR p_query_input.supply_source_line_detail = supply_source_line_detail
4579                )
4580            AND (p_query_input.revision = l_miss_char
4581                 OR p_query_input.revision IS NULL
4582                    AND revision IS NULL
4583                 OR p_query_input.revision = revision
4584                )
4585            AND (p_query_input.subinventory_code = l_miss_char
4586                 OR p_query_input.subinventory_code IS NULL
4587                    AND subinventory_code IS NULL
4588                 OR p_query_input.subinventory_code = subinventory_code
4589                )
4590            AND (p_query_input.subinventory_id = l_miss_num
4591                 OR p_query_input.subinventory_id IS NULL
4592                    AND subinventory_id IS NULL
4593                 OR p_query_input.subinventory_id = subinventory_id
4594                )
4595            AND (p_query_input.locator_id = l_miss_num
4596                 OR p_query_input.locator_id IS NULL
4597                    AND locator_id IS NULL
4598                 OR p_query_input.locator_id = locator_id
4599                )
4600            AND (p_query_input.lot_number = l_miss_char
4601                 OR p_query_input.lot_number IS NULL
4602                    AND lot_number IS NULL
4603                 OR p_query_input.lot_number = lot_number
4604                )
4605            AND (p_query_input.lot_number_id = l_miss_num
4606                 OR p_query_input.lot_number_id IS NULL
4607                    AND lot_number_id IS NULL
4608                 OR p_query_input.lot_number_id = lot_number_id
4609                )
4610            AND (p_query_input.lpn_id = l_miss_num
4611                 OR p_query_input.lpn_id IS NULL
4612                    AND lpn_id IS NULL
4613                 OR p_query_input.lpn_id = lpn_id
4614                )
4615            AND (p_query_input.ship_ready_flag = l_miss_num
4616                 OR (p_query_input.ship_ready_flag IS NULL OR p_query_input.ship_ready_flag = 2)
4617                    AND (ship_ready_flag IS NULL OR ship_ready_flag = 2)
4618                 OR p_query_input.ship_ready_flag = ship_ready_flag
4619                )
4620            AND (p_query_input.staged_flag = l_miss_char
4621                 OR (p_query_input.staged_flag IS NULL OR p_query_input.staged_flag = 'N')
4622                    AND (staged_flag IS NULL OR staged_flag = 'N')
4623                 OR p_query_input.staged_flag = staged_flag
4624                )
4625            AND (p_query_input.attribute_category = l_miss_char
4626                 OR p_query_input.attribute_category IS NULL
4627                    AND attribute_category IS NULL
4628                 OR p_query_input.attribute_category = attribute_category
4629                )
4630            AND (p_query_input.attribute1 = l_miss_char
4631                 OR p_query_input.attribute1 IS NULL
4632                    AND attribute1 IS NULL
4633                 OR p_query_input.attribute1 = attribute1
4634                )
4635            AND (p_query_input.attribute2 = l_miss_char
4636                 OR p_query_input.attribute2 IS NULL
4637                    AND attribute2 IS NULL
4638                 OR p_query_input.attribute2 = attribute2
4639                )
4640            AND (p_query_input.attribute3 = l_miss_char
4641                 OR p_query_input.attribute3 IS NULL
4642                    AND attribute3 IS NULL
4643                 OR p_query_input.attribute3 = attribute3
4644                )
4645            AND (p_query_input.attribute4 = l_miss_char
4646                 OR p_query_input.attribute4 IS NULL
4647                    AND attribute4 IS NULL
4648                 OR p_query_input.attribute4 = attribute4
4649                )
4650            AND (p_query_input.attribute5 = l_miss_char
4651                 OR p_query_input.attribute5 IS NULL
4652                    AND attribute5 IS NULL
4653                 OR p_query_input.attribute5 = attribute5
4654                )
4655            AND (p_query_input.attribute6 = l_miss_char
4656                 OR p_query_input.attribute6 IS NULL
4657                    AND attribute6 IS NULL
4658                 OR p_query_input.attribute6 = attribute6
4659                )
4660            AND (p_query_input.attribute7 = l_miss_char
4661                 OR p_query_input.attribute7 IS NULL
4662                    AND attribute7 IS NULL
4663                 OR p_query_input.attribute7 = attribute7
4664                )
4665            AND (p_query_input.attribute8 = l_miss_char
4666                 OR p_query_input.attribute8 IS NULL
4667                    AND attribute8 IS NULL
4668                 OR p_query_input.attribute8 = attribute8
4669                )
4670            AND (p_query_input.attribute9 = l_miss_char
4671                 OR p_query_input.attribute9 IS NULL
4672                    AND attribute9 IS NULL
4673                 OR p_query_input.attribute9 = attribute9
4674                )
4675            AND (p_query_input.attribute10 = l_miss_char
4676                 OR p_query_input.attribute10 IS NULL
4677                    AND attribute10 IS NULL
4678                 OR p_query_input.attribute10 = attribute10
4679                )
4680            AND (p_query_input.attribute11 = l_miss_char
4681                 OR p_query_input.attribute11 IS NULL
4682                    AND attribute11 IS NULL
4683                 OR p_query_input.attribute11 = attribute11
4684                )
4685            AND (p_query_input.attribute12 = l_miss_char
4686                 OR p_query_input.attribute12 IS NULL
4687                    AND attribute12 IS NULL
4688                 OR p_query_input.attribute12 = attribute12
4689                )
4690            AND (p_query_input.attribute13 = l_miss_char
4691                 OR p_query_input.attribute13 IS NULL
4692                    AND attribute13 IS NULL
4693                 OR p_query_input.attribute13 = attribute13
4694                )
4695            AND (p_query_input.attribute14 = l_miss_char
4696                 OR p_query_input.attribute14 IS NULL
4697                    AND attribute14 IS NULL
4698                 OR p_query_input.attribute14 = attribute14
4699                )
4700            AND (p_query_input.attribute15 = l_miss_char
4701                 OR p_query_input.attribute15 IS NULL
4702                    AND attribute15 IS NULL
4703                 OR p_query_input.attribute15 = attribute15
4704 		)
4705 		/**** {{ R12 Enhanced reservations code changes }}****/
4706 	AND (p_query_input.crossdock_flag = l_miss_char
4707                   OR p_query_input.crossdock_flag IS NULL
4708                      AND crossdock_flag IS NULL
4709                   OR p_query_input.crossdock_flag = crossdock_flag
4710 	     )
4711 	AND (p_query_input.crossdock_criteria_id = l_miss_num
4712                   OR p_query_input.crossdock_criteria_id IS NULL
4713                      AND crossdock_criteria_id IS NULL
4714                   OR p_query_input.crossdock_criteria_id = crossdock_criteria_id
4715 	)
4716 	AND (p_query_input.demand_source_line_detail = l_miss_num
4717                   OR p_query_input.demand_source_line_detail IS NULL
4718                      AND demand_source_line_detail IS NULL
4719                   OR p_query_input.demand_source_line_detail = demand_source_line_detail
4720 	)
4721 	AND (p_query_input.supply_receipt_date = l_miss_date
4722                   OR p_query_input.supply_receipt_date IS NULL
4723                      AND supply_receipt_date IS NULL
4724                   OR p_query_input.supply_receipt_date = supply_receipt_date
4725 	)
4726 	AND (p_query_input.demand_ship_date = l_miss_date
4727                   OR p_query_input.demand_ship_date IS NULL
4728                      AND demand_ship_date IS NULL
4729                   OR p_query_input.demand_ship_date = demand_ship_date
4730 	)
4731 	AND (p_query_input.project_id = l_miss_num
4732                   OR p_query_input.project_id IS NULL
4733                      AND project_id IS NULL
4734                   OR p_query_input.project_id = project_id
4735 	)
4736 	AND (p_query_input.task_id = l_miss_num
4737                   OR p_query_input.task_id IS NULL
4738                      AND task_id IS NULL
4739                   OR p_query_input.task_id = task_id
4740 	)
4741 	 /***** End R12 ***/
4742 
4743 
4744       ORDER BY NVL(revision, ' '), NVL(lot_number, ' '), NVL(subinventory_code, ' '), NVL(locator_id, 0);
4745   BEGIN
4746     x_error_code                 := inv_reservation_global.g_err_unexpected;
4747 
4748     -- Use cache to get value for l_debug
4749     IF g_is_pickrelease_set IS NULL THEN
4750        g_is_pickrelease_set := 2;
4751        IF INV_CACHE.is_pickrelease THEN
4752           g_is_pickrelease_set := 1;
4753        END IF;
4754     END IF;
4755     IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
4756        g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
4757     END IF;
4758 
4759     l_debug := g_debug;
4760 
4761     IF (l_debug = 1) then
4762        debug_print('Inside Query reservations...');
4763     END IF;
4764 
4765     --  Standard call to check for call compatibility
4766     IF NOT fnd_api.compatible_api_call(l_api_version_number, p_api_version_number, l_api_name, g_pkg_name) THEN
4767       RAISE fnd_api.g_exc_unexpected_error;
4768     END IF;
4769 
4770     --
4771     --  Initialize message list.
4772     IF fnd_api.to_boolean(p_init_msg_lst) THEN
4773       fnd_msg_pub.initialize;
4774     END IF;
4775 
4776     --
4777     IF (l_debug = 1) then
4778        debug_print('Lock Records: ' || p_lock_records);
4779        debug_print('p_sort_by_req_date: ' || p_sort_by_req_date);
4780        debug_print('p_cancel_order_mode: ' || p_cancel_order_mode);
4781     END IF;
4782 
4783 
4784     IF p_lock_records = fnd_api.g_true THEN
4785       l_lock_stmt  := ' FOR UPDATE NOWAIT';
4786       l_update     := TRUE;
4787     END IF;
4788 
4789     --
4790     -- Pl. note the order by precedence
4791     -- 1. By requirement_date - asc or desc (For WIP)
4792     -- 2. By ship_ready_flag, detailed quantity (For OE/OM)
4793     -- 3. By revision,lot,sub,locator - high level reservations 1st(For default)
4794     --
4795     IF p_sort_by_req_date = inv_reservation_global.g_query_req_date_asc THEN
4796        l_sort_stmt  := ' ORDER BY REQUIREMENT_DATE ASC ';
4797      ELSIF p_sort_by_req_date = inv_reservation_global.g_query_req_date_desc THEN
4798        l_sort_stmt  := ' ORDER BY REQUIREMENT_DATE DESC ';
4799        /**** {{ R12 Enhanced reservations code changes. Adding new sort by conditions }}****/
4800      ELSIF p_sort_by_req_date = inv_reservation_global.g_query_demand_ship_date_asc THEN
4801        l_sort_stmt  := ' ORDER BY NVL(DEMAND_SHIP_DATE,REQUIREMENT_DATE) ASC ';
4802      ELSIF p_sort_by_req_date = inv_reservation_global.g_query_demand_ship_date_desc THEN
4803        l_sort_stmt  := ' ORDER BY NVL(DEMAND_SHIP_DATE,REQUIREMENT_DATE) DESC ';
4804      ELSIF p_sort_by_req_date = inv_reservation_global.g_query_supply_rcpt_date_asc THEN
4805        l_sort_stmt  := ' ORDER BY NVL(SUPPLY_SHIP_DATE,REQUIREMENT_DATE) ASC ';
4806      ELSIF p_sort_by_req_date = inv_reservation_global.g_query_supply_rcpt_date_asc THEN
4807        l_sort_stmt  := ' ORDER BY NVL(SUPPLY_SHIP_DATE,REQUIREMENT_DATE) DESC ';
4808        /*** End R12 ***/
4809 
4810      ELSIF p_cancel_order_mode = inv_reservation_global.g_cancel_order_yes THEN
4811        l_sort_stmt  := ' ORDER BY NVL(SHIP_READY_FLAG,2) DESC, ' || 'NVL(DETAILED_QUANTITY,0) ';
4812      ELSE
4813        -- Default order by - High level followed by detail
4814        l_sort_default  := TRUE;
4815        l_sort_stmt     := ' ORDER BY NVL(REVISION, ' || ' '' ''), ' || ' NVL(LOT_NUMBER, ' || ' '' ''), ' || ' NVL(SUBINVENTORY_CODE, ' || ' '' ''), ' || ' NVL(LOCATOR_ID,0) ';
4816        -- /*** Easier to read this
4817        --  ORDER BY NVL(REVISION,          ' '),
4818        --	 NVL(LOT_NUMBER,        ' '),
4819        --	 NVL(SUBINVENTORY_CODE, ' '),
4820        --	 NVL(LOCATOR_ID,0) ****/
4821     END IF;
4822 
4823     IF (l_debug = 1) then
4824        debug_print('Reservation ID: ' || p_query_input.reservation_id);
4825        debug_print('demand_source_line_id: ' || p_query_input.demand_source_line_id);
4826        debug_print('inventory_item_id: ' || p_query_input.inventory_item_id);
4827        debug_print('organization_id: ' || p_query_input.organization_id);
4828        debug_print('supply source header id: ' || p_query_input.supply_source_header_id);
4829        debug_print('supply source line id: ' || p_query_input.supply_source_line_id);
4830        debug_print('supply source type id: ' || p_query_input.supply_source_type_id);
4831        IF l_sort_default then
4832 	  debug_print('l_sort_default is true');
4833 	ELSE
4834 	  debug_print('l_sort_default is flase');
4835        END IF;
4836        IF l_update then
4837 	  debug_print('l_update is true ');
4838 	ELSE
4839 	  debug_print('l_update is false ');
4840        END IF;
4841     END IF;
4842 
4843     IF  p_query_input.reservation_id <> fnd_api.g_miss_num
4844       AND p_query_input.reservation_id IS NOT NULL THEN
4845        IF (l_debug = 1) then
4846 	  debug_print(' Inside ref cursor for reservation id');
4847        END IF;
4848        IF l_sort_default THEN
4849 	  IF l_update THEN
4850 	     IF (l_debug = 1) then
4851 		debug_print(' Open res cursor for update');
4852 	     END IF;
4853 	     OPEN c_res_id_update;
4854 	     l_res_cursor  := TRUE;
4855 	   ELSE
4856 	     IF (l_debug = 1) then
4857 		debug_print(' Open res cursor for NO update');
4858 	     END IF;
4859 	     OPEN c_res_id;
4860 	    l_res_cursor  := TRUE;
4861 	  END IF;
4862 	  IF (l_debug = 1) and l_res_cursor then
4863 	     debug_print('l_res_cursor is true ');
4864 	   ELSE
4865 	     debug_print('l_res_cursor is false ');
4866 	  END IF;
4867 	ELSE
4868         -- INVCONV - Incorporate secondary quantities
4869 	  OPEN l_cursor_ref FOR    'SELECT
4870 	    reservation_id
4871 	  , requirement_date
4872 	  , organization_id
4873 	  , inventory_item_id
4874 	  , demand_source_type_id
4875 	  , demand_source_name
4876 	  , demand_source_header_id
4877 	  , demand_source_line_id
4878 	  , demand_source_delivery
4879 	  , primary_uom_code
4880 	  , primary_uom_id
4881 	  , secondary_uom_code
4882 	  , secondary_uom_id
4883 	  , reservation_uom_code
4884 	  , reservation_uom_id
4885 	  , reservation_quantity
4886 	  , primary_reservation_quantity
4887 	  , secondary_reservation_quantity
4888 	  , detailed_quantity
4889 	  , secondary_detailed_quantity
4890 	  , autodetail_group_id
4891 	  , external_source_code
4892 	  , external_source_line_id
4893 	  , supply_source_type_id
4894 	  , supply_source_header_id
4895 	  , supply_source_line_id
4896 	  , supply_source_name
4897 	  , supply_source_line_detail
4898 	  , revision
4899 	  , subinventory_code
4900 	  , subinventory_id
4901 	  , locator_id
4902 	  , lot_number
4903 	  , lot_number_id
4904 	  , pick_slip_number
4905 	  , lpn_id
4906 	  , attribute_category
4907 	  , attribute1
4908 	  , attribute2
4909 	  , attribute3
4910 	  , attribute4
4911 	  , attribute5
4912 	  , attribute6
4913 	  , attribute7
4914 	  , attribute8
4915 	  , attribute9
4916 	  , attribute10
4917 	  , attribute11
4918 	  , attribute12
4919 	  , attribute13
4920 	  , attribute14
4921 	  , attribute15
4922 	  , ship_ready_flag
4923 	  , staged_flag
4924 	   /**** {{ R12 Enhanced reservations code changes. Adding new
4925 	    -- columns for query reservations }}****/
4926 	 , crossdock_flag
4927 	 , crossdock_criteria_id
4928 	 , demand_source_line_detail
4929 	 , serial_reservation_quantity
4930 	 , supply_receipt_date
4931 	 , demand_ship_date
4932 	 , project_id
4933 	 , task_id
4934 	 , orig_supply_source_type_id
4935 	 , orig_supply_source_header_id
4936 	 , orig_supply_source_line_id
4937 	 , orig_supply_source_line_detail
4938 	 , orig_demand_source_type_id
4939 	 , orig_demand_source_header_id
4940 	 , orig_demand_source_line_id
4941 	 , orig_demand_source_line_detail
4942          , serial_number
4943 	 /***** End R12 ***/
4944         FROM mtl_reservations
4945         WHERE
4946         :reservation_id = reservation_id '
4947                               || l_lock_stmt
4948                               || l_sort_stmt USING p_query_input.reservation_id;
4949       END IF;
4950     ELSIF  p_query_input.demand_source_line_id <> fnd_api.g_miss_num
4951       AND p_query_input.demand_source_line_id IS NOT NULL THEN
4952        IF (l_debug = 1) then
4953 	  debug_print(' Inside ref cursor for demand');
4954        END IF;
4955        IF l_sort_default THEN
4956 	  IF l_update THEN
4957 	     l_demand_cursor  := TRUE;
4958 	     IF (l_debug = 1) then
4959 		debug_print(' Open demand cursor for update');
4960 	     END IF;
4961 	     OPEN c_demand_update;
4962 	   ELSE
4963 	     l_demand_cursor  := TRUE;
4964 	     IF (l_debug = 1) then
4965 		debug_print(' Open demand cursor for NO update');
4966 	     END IF;
4967 	     OPEN c_demand;
4968 	  END IF;
4969 	  IF (l_debug = 1) AND l_demand_cursor then
4970 	     debug_print('l_demand_cursor is true');
4971 	   ELSE
4972 	     debug_print('l_demand_cursor is false');
4973 	  END IF;
4974 	ELSE
4975           -- INVCONV - Incorporate secondary quantities
4976 	  OPEN l_cursor_ref FOR    'SELECT
4977 	    reservation_id
4978 	    , requirement_date
4979 	    , organization_id
4980 	    , inventory_item_id
4981 	    , demand_source_type_id
4982 	    , demand_source_name
4983 	    , demand_source_header_id
4984 	    , demand_source_line_id
4985 	    , demand_source_delivery
4986 	    , primary_uom_code
4987 	    , primary_uom_id
4988 	    , secondary_uom_code
4989 	    , secondary_uom_id
4990 	    , reservation_uom_code
4991 	    , reservation_uom_id
4992 	    , reservation_quantity
4993 	    , primary_reservation_quantity
4994 	    , secondary_reservation_quantity
4995 	    , detailed_quantity
4996 	    , secondary_detailed_quantity
4997 	    , autodetail_group_id
4998 	    , external_source_code
4999 	    , external_source_line_id
5000 	    , supply_source_type_id
5001 	    , supply_source_header_id
5002 	    , supply_source_line_id
5003 	    , supply_source_name
5004 	    , supply_source_line_detail
5005 	    , revision
5006 	    , subinventory_code
5007 	    , subinventory_id
5008 	    , locator_id
5009 	    , lot_number
5010 	    , lot_number_id
5011 	    , pick_slip_number
5012 	    , lpn_id
5013 	    , attribute_category
5014 	    , attribute1
5015 	    , attribute2
5016 	    , attribute3
5017 	    , attribute4
5018 	    , attribute5
5019 	    , attribute6
5020 	    , attribute7
5021 	    , attribute8
5022 	    , attribute9
5023 	    , attribute10
5024 	    , attribute11
5025 	    , attribute12
5026 	    , attribute13
5027 	    , attribute14
5028 	    , attribute15
5029 	    , ship_ready_flag
5030 	    , staged_flag
5031 	    /**** {{ R12 Enhanced reservations code changes. Adding new
5032 	    -- columns for query reservations }}****/
5033 	    , crossdock_flag
5034 	    , crossdock_criteria_id
5035 	    , demand_source_line_detail
5036 	    , serial_reservation_quantity
5037 	    , supply_receipt_date
5038 	    , demand_ship_date
5039 	    , project_id
5040 	    , task_id
5041 	    , orig_supply_source_type_id
5042 	    , orig_supply_source_header_id
5043 	    , orig_supply_source_line_id
5044 	    , orig_supply_source_line_detail
5045 	    , orig_demand_source_type_id
5046 	    , orig_demand_source_header_id
5047 	    , orig_demand_source_line_id
5048 	    , orig_demand_source_line_detail
5049             , serial_number
5050 	    /***** End R12 ***/
5051 	    FROM mtl_reservations
5052 	    WHERE
5053 	    demand_source_line_id = :demand_source_line_id
5054 	    AND
5055 	    (:requirement_date = :l_miss_date
5056 	     OR :requirement_date IS NULL
5057 	     AND requirement_date IS NULL
5058 	     OR :requirement_date
5059 	     = requirement_date
5060 	     )
5061 	       AND
5062         (:organization_id = :l_miss_num
5063          OR :organization_id IS NULL
5064          AND organization_id IS NULL
5065          OR :organization_id = organization_id
5066          )
5067         AND
5068         (:inventory_item_id = :l_miss_num
5069          OR :inventory_item_id IS NULL
5070          AND inventory_item_id IS NULL
5071          OR :inventory_item_id = inventory_item_id
5072          )
5073   AND
5074   (:demand_source_type_id = :l_miss_num
5075          OR :demand_source_type_id IS NULL
5076          AND demand_source_type_id IS NULL
5077          OR :demand_source_type_id
5078          = demand_source_type_id
5079          )
5080         AND
5081         (:demand_source_header_id = :l_miss_num
5082          OR :demand_source_header_id IS NULL
5083          AND demand_source_header_id IS NULL
5084          OR :demand_source_header_id
5085          = demand_source_header_id
5086          )
5087         AND
5088         (:demand_source_name = :l_miss_char
5089          OR :demand_source_name IS NULL
5090          AND demand_source_name IS NULL
5091          OR :demand_source_name = demand_source_name
5092          )
5093         AND
5094         (:demand_source_delivery = :l_miss_num
5095          OR :demand_source_delivery IS NULL
5096          AND demand_source_delivery IS NULL
5097          OR :demand_source_delivery = demand_source_delivery
5098          )
5099       AND
5100         (:primary_uom_code = :l_miss_char
5101          OR :primary_uom_code IS NULL
5102          AND primary_uom_code IS NULL
5103          OR :primary_uom_code = primary_uom_code
5104          )
5105         AND
5106         (:primary_uom_id = :l_miss_num
5107          OR :primary_uom_id IS NULL
5108          AND primary_uom_id IS NULL
5109          OR :primary_uom_id = primary_uom_id
5110          )
5111          -- INVCONV BEGIN
5112         AND
5113         (:secondary_uom_code = :l_miss_char
5114          OR :secondary_uom_code IS NULL
5115          AND secondary_uom_code IS NULL
5116          OR :secondary_uom_code = secondary_uom_code
5117          )
5118         AND
5119         (:secondary_uom_id = :l_miss_num
5120          OR :secondary_uom_id IS NULL
5121          AND secondary_uom_id IS NULL
5122          OR :secondary_uom_id = secondary_uom_id
5123          )
5124         -- INVCONV END
5125         AND
5126         (:reservation_uom_code = :l_miss_char
5127          OR :reservation_uom_code IS NULL
5128          AND reservation_uom_code IS NULL
5129          OR :reservation_uom_code = reservation_uom_code
5130          )
5131         AND
5132         (:reservation_uom_id = :l_miss_num
5133          OR :reservation_uom_id IS NULL
5134          AND reservation_uom_id IS NULL
5135          OR :reservation_uom_id = reservation_uom_id
5136          )
5137         AND
5138         (:autodetail_group_id = :l_miss_num
5139          OR :autodetail_group_id IS NULL
5140          AND autodetail_group_id IS NULL
5141          OR :autodetail_group_id = autodetail_group_id
5142          )
5143         AND
5144         (:external_source_code = :l_miss_char
5145          OR :external_source_code IS NULL
5146          AND external_source_code IS NULL
5147          OR :external_source_code = external_source_code
5148          )
5149         AND
5150         (:external_source_line_id = :l_miss_num
5151          OR :external_source_line_id IS NULL
5152          AND external_source_line_id IS NULL
5153          OR :external_source_line_id = external_source_line_id
5154          )
5155         AND
5156         (:supply_source_type_id = :l_miss_num
5157          OR :supply_source_type_id IS NULL
5158          AND supply_source_type_id IS NULL
5159          OR :supply_source_type_id = supply_source_type_id
5160          )
5161         AND
5162         (:supply_source_header_id = :l_miss_num
5163          OR :supply_source_header_id IS NULL
5164          AND supply_source_header_id IS NULL
5165          OR :supply_source_header_id
5166          = supply_source_header_id
5167          )
5168         AND
5169         (:supply_source_line_id = :l_miss_num
5170          OR :supply_source_line_id IS NULL
5171          AND supply_source_line_id IS NULL
5172          OR :supply_source_line_id = supply_source_line_id
5173          )
5174         AND
5175         (:supply_source_name = :l_miss_char
5176          OR :supply_source_name IS NULL
5177          AND supply_source_name IS NULL
5178          OR :supply_source_name = supply_source_name
5179          )
5180         AND
5181         (:supply_source_line_detail = :l_miss_num
5182          OR :supply_source_line_detail IS NULL
5183          AND supply_source_line_detail IS NULL
5184          OR :supply_source_line_detail
5185          = supply_source_line_detail
5186          )
5187         AND
5188         (:revision = :l_miss_char
5189          OR :revision IS NULL
5190          AND revision IS NULL
5191          OR :revision = revision
5192          )
5193         AND
5194         (:subinventory_code = :l_miss_char
5195          OR :subinventory_code IS NULL
5196          AND subinventory_code IS NULL
5197          OR :subinventory_code = subinventory_code
5198          )
5199        AND
5200         (:subinventory_id = :l_miss_num
5201          OR :subinventory_id IS NULL
5202          AND subinventory_id IS NULL
5203          OR :subinventory_id = subinventory_id
5204          )
5205         AND
5206         (:locator_id = :l_miss_num
5207          OR :locator_id IS NULL
5208          AND locator_id IS NULL
5209          OR :locator_id = locator_id
5210          )
5211         AND
5212         (:lot_number = :l_miss_char
5213          OR :lot_number IS NULL
5214          AND lot_number IS NULL
5215          OR :lot_number = lot_number
5216          )
5217         AND
5218         (:lot_number_id = :l_miss_num
5219          OR :lot_number_id IS NULL
5220          AND lot_number_id IS NULL
5221          OR :lot_number_id = lot_number_id
5222          )
5223         AND
5224         (:lpn_id = :l_miss_num
5225          OR :lpn_id IS NULL
5226          AND lpn_id IS NULL
5227          OR :lpn_id = lpn_id
5228          )
5229         AND
5230         (:ship_ready_flag = :l_miss_num
5231          OR (:ship_ready_flag IS NULL OR :ship_ready_flag = 2)
5232          AND (ship_ready_flag IS NULL OR ship_ready_flag = 2)
5233          OR :ship_ready_flag = ship_ready_flag
5234          )
5235         AND
5236         (:staged_flag = :l_miss_char
5237          OR (:staged_flag IS NULL OR :staged_flag = ''N'')
5238          AND (staged_flag IS NULL OR staged_flag = ''N'')
5239          OR :staged_flag = staged_flag
5240          )
5241         AND
5242         (:attribute_category = :l_miss_char
5243          OR :attribute_category IS NULL
5244          AND attribute_category IS NULL
5245          OR :attribute_category = attribute_category
5246          )
5247         AND
5248         (:attribute1 = :l_miss_char
5249          OR :attribute1 IS NULL
5250          AND attribute1 IS NULL
5251          OR :attribute1 = attribute1
5252          )
5253        AND
5254         (:attribute2 = :l_miss_char
5255          OR :attribute2 IS NULL
5256          AND attribute2 IS NULL
5257          OR :attribute2 = attribute2
5258          )
5259         AND
5260         (:attribute3 = :l_miss_char
5261          OR :attribute3 IS NULL
5262          AND attribute3 IS NULL
5263          OR :attribute3 = attribute3
5264          )
5265         AND
5266         (:attribute4 = :l_miss_char
5267          OR :attribute4 IS NULL
5268          AND attribute4 IS NULL
5269          OR :attribute4 = attribute4
5270          )
5271        AND
5272         (:attribute5 = :l_miss_char
5273          OR :attribute5 IS NULL
5274          AND attribute5 IS NULL
5275          OR :attribute5 = attribute5
5276          )
5277         AND
5278         (:attribute6 = :l_miss_char
5279          OR :attribute6 IS NULL
5280          AND attribute6 IS NULL
5281          OR :attribute6 = attribute6
5282          )
5283         AND
5284         (:attribute7 = :l_miss_char
5285          OR :attribute7 IS NULL
5286          AND attribute7 IS NULL
5287          OR :attribute7 = attribute7
5288          )
5289         AND
5290         (:attribute8 = :l_miss_char
5291          OR :attribute8 IS NULL
5292          AND attribute8 IS NULL
5293          OR :attribute8 = attribute8
5294          )
5295         AND
5296         (:attribute9 = :l_miss_char
5297          OR :attribute9 IS NULL
5298          AND attribute9 IS NULL
5299          OR :attribute9 = attribute9
5300          )
5301         AND
5302         (:attribute10 = :l_miss_char
5303          OR :attribute10 IS NULL
5304          AND attribute10 IS NULL
5305          OR :attribute10 = attribute10
5306          )
5307         AND
5308         (:attribute11 = :l_miss_char
5309          OR :attribute11 IS NULL
5310          AND attribute11 IS NULL
5311          OR :attribute11 = attribute11
5312          )
5313         AND
5314         (:attribute12 = :l_miss_char
5315          OR :attribute12 IS NULL
5316          AND attribute12 IS NULL
5317          OR :attribute12 = attribute12
5318          )
5319         AND
5320         (:attribute13 = :l_miss_char
5321          OR :attribute13 IS NULL
5322          AND attribute13 IS NULL
5323          OR :attribute13 = attribute13
5324          )
5325         AND
5326         (:attribute14 = :l_miss_char
5327          OR :attribute14 IS NULL
5328          AND attribute14 IS NULL
5329          OR :attribute14 = attribute14
5330          )
5331         AND
5332         (:attribute15 = :l_miss_char
5333          OR :attribute15 IS NULL
5334          AND attribute15 IS NULL
5335          OR :attribute15 = attribute15
5336          )
5337 /**** {{ R12 Enhanced reservations code changes }}****/
5338 	AND
5339 	   (:crossdock_flag = :l_miss_char
5340          OR :crossdock_flag IS NULL
5341          AND crossdock_flag IS NULL
5342          OR :crossdock_flag = crossdock_flag
5343          )
5344 	AND
5345 	   (:crossdock_criteria_id = :l_miss_num
5346          OR :crossdock_criteria_id IS NULL
5347          AND crossdock_criteria_id IS NULL
5348          OR :crossdock_criteria_id = crossdock_criteria_id
5349 	    )
5350         AND
5351 	   (:demand_source_line_detail = :l_miss_num
5352          OR :demand_source_line_detail IS NULL
5353          AND demand_source_line_detail IS NULL
5354          OR :demand_source_line_detail = demand_source_line_detail
5355 	    )
5356 	AND
5357 	   (:supply_receipt_date = :l_miss_date
5358          OR :supply_receipt_date IS NULL
5359          AND supply_receipt_date IS NULL
5360          OR :supply_receipt_date = supply_receipt_date
5361 	    )
5362 	AND
5363 	   (:demand_ship_date = :l_miss_date
5364          OR :demand_ship_date IS NULL
5365          AND demand_ship_date IS NULL
5366          OR :demand_ship_date = demand_ship_date
5367 	    )
5368 	AND
5369 	   (:project_id = :l_miss_num
5370          OR :project_id IS NULL
5371          AND project_id IS NULL
5372          OR :project_id = project_id
5373 	    )
5374 	 AND
5375 	   (:task_id = :l_miss_num
5376          OR :task_id IS NULL
5377          AND task_id IS NULL
5378          OR :task_id = task_id
5379 	    )
5380 /***** End R12  ***/
5381 	   '
5382                               || l_lock_stmt
5383                               || l_sort_stmt
5384           USING  p_query_input.demand_source_line_id
5385                , p_query_input.requirement_date
5386                , l_miss_date
5387                , p_query_input.requirement_date
5388                , p_query_input.requirement_date
5389                , p_query_input.organization_id
5390                , l_miss_num
5391                , p_query_input.organization_id
5392                , p_query_input.organization_id
5393                , p_query_input.inventory_item_id
5394                , l_miss_num
5395                , p_query_input.inventory_item_id
5396                , p_query_input.inventory_item_id
5397                , p_query_input.demand_source_type_id
5398                , l_miss_num
5399                , p_query_input.demand_source_type_id
5400                , p_query_input.demand_source_type_id
5401                , p_query_input.demand_source_header_id
5402                , l_miss_num
5403                , p_query_input.demand_source_header_id
5404                , p_query_input.demand_source_header_id
5405                , p_query_input.demand_source_name
5406                , l_miss_char
5407                , p_query_input.demand_source_name
5408                , p_query_input.demand_source_name
5409                , p_query_input.demand_source_delivery
5410                , l_miss_num
5411                , p_query_input.demand_source_delivery
5412                , p_query_input.demand_source_delivery
5413                , p_query_input.primary_uom_code
5414                , l_miss_char
5415                , p_query_input.primary_uom_code
5416                , p_query_input.primary_uom_code
5417                , p_query_input.primary_uom_id
5418                , l_miss_num
5419                , p_query_input.primary_uom_id
5420                , p_query_input.primary_uom_id
5421                 -- INVCONV BEGIN
5422                , p_query_input.secondary_uom_code
5423                , l_miss_char
5424                , p_query_input.secondary_uom_code
5425                , p_query_input.secondary_uom_code
5426                , p_query_input.secondary_uom_id
5427                , l_miss_num
5428                , p_query_input.secondary_uom_id
5429                , p_query_input.secondary_uom_id
5430                -- INVCONV END
5431                , p_query_input.reservation_uom_code
5432                , l_miss_char
5433                , p_query_input.reservation_uom_code
5434                , p_query_input.reservation_uom_code
5435                , p_query_input.reservation_uom_id
5436                , l_miss_num
5437                , p_query_input.reservation_uom_id
5438                , p_query_input.reservation_uom_id
5439                , p_query_input.autodetail_group_id
5440                , l_miss_num
5441                , p_query_input.autodetail_group_id
5442                , p_query_input.autodetail_group_id
5443                , p_query_input.external_source_code
5444                , l_miss_char
5445                , p_query_input.external_source_code
5446                , p_query_input.external_source_code
5447                , p_query_input.external_source_line_id
5448                , l_miss_num
5449                , p_query_input.external_source_line_id
5450                , p_query_input.external_source_line_id
5451                , p_query_input.supply_source_type_id
5452                , l_miss_num
5453                , p_query_input.supply_source_type_id
5454                , p_query_input.supply_source_type_id
5455                , p_query_input.supply_source_header_id
5456                , l_miss_num
5457                , p_query_input.supply_source_header_id
5458                , p_query_input.supply_source_header_id
5459                , p_query_input.supply_source_line_id
5460                , l_miss_num
5461                , p_query_input.supply_source_line_id
5462                , p_query_input.supply_source_line_id
5463                , p_query_input.supply_source_name
5464                , l_miss_char
5465                , p_query_input.supply_source_name
5466                , p_query_input.supply_source_name
5467                , p_query_input.supply_source_line_detail
5468                , l_miss_num
5469                , p_query_input.supply_source_line_detail
5470                , p_query_input.supply_source_line_detail
5471                , p_query_input.revision
5472                , l_miss_char
5473                , p_query_input.revision
5474                , p_query_input.revision
5475                , p_query_input.subinventory_code
5476                , l_miss_char
5477                , p_query_input.subinventory_code
5478                , p_query_input.subinventory_code
5479                , p_query_input.subinventory_id
5480                , l_miss_num
5481                , p_query_input.subinventory_id
5482                , p_query_input.subinventory_id
5483                , p_query_input.locator_id
5484                , l_miss_num
5485                , p_query_input.locator_id
5486                , p_query_input.locator_id
5487                , p_query_input.lot_number
5488                , l_miss_char
5489                , p_query_input.lot_number
5490                , p_query_input.lot_number
5491                , p_query_input.lot_number_id
5492                , l_miss_num
5493                , p_query_input.lot_number_id
5494                , p_query_input.lot_number_id
5495                , p_query_input.lpn_id
5496                , l_miss_num
5497                , p_query_input.lpn_id
5498                , p_query_input.lpn_id
5499                , p_query_input.ship_ready_flag
5500                , l_miss_num
5501                , p_query_input.ship_ready_flag
5502                , p_query_input.ship_ready_flag
5503                , p_query_input.ship_ready_flag
5504                , p_query_input.staged_flag
5505                , l_miss_char
5506                , p_query_input.staged_flag
5507                , p_query_input.staged_flag
5508                , p_query_input.staged_flag
5509                , p_query_input.attribute_category
5510                , l_miss_char
5511                , p_query_input.attribute_category
5512                , p_query_input.attribute_category
5513                , p_query_input.attribute1
5514                , l_miss_char
5515                , p_query_input.attribute1
5516                , p_query_input.attribute1
5517                , p_query_input.attribute2
5518                , l_miss_char
5519                , p_query_input.attribute2
5520                , p_query_input.attribute2
5521                , p_query_input.attribute3
5522                , l_miss_char
5523                , p_query_input.attribute3
5524                , p_query_input.attribute3
5525                , p_query_input.attribute4
5526                , l_miss_char
5527                , p_query_input.attribute4
5528                , p_query_input.attribute4
5529                , p_query_input.attribute5
5530                , l_miss_char
5531                , p_query_input.attribute5
5532                , p_query_input.attribute5
5533                , p_query_input.attribute6
5534                , l_miss_char
5535                , p_query_input.attribute6
5536                , p_query_input.attribute6
5537                , p_query_input.attribute7
5538                , l_miss_char
5539                , p_query_input.attribute7
5540                , p_query_input.attribute7
5541                , p_query_input.attribute8
5542                , l_miss_char
5543                , p_query_input.attribute8
5544                , p_query_input.attribute8
5545                , p_query_input.attribute9
5546                , l_miss_char
5547                , p_query_input.attribute9
5548                , p_query_input.attribute9
5549                , p_query_input.attribute10
5550                , l_miss_char
5551                , p_query_input.attribute10
5552                , p_query_input.attribute10
5553                , p_query_input.attribute11
5554                , l_miss_char
5555                , p_query_input.attribute11
5556                , p_query_input.attribute11
5557                , p_query_input.attribute12
5558                , l_miss_char
5559                , p_query_input.attribute12
5560                , p_query_input.attribute12
5561                , p_query_input.attribute13
5562                , l_miss_char
5563                , p_query_input.attribute13
5564                , p_query_input.attribute13
5565                , p_query_input.attribute14
5566                , l_miss_char
5567                , p_query_input.attribute14
5568                , p_query_input.attribute14
5569                , p_query_input.attribute15
5570                , l_miss_char
5571                , p_query_input.attribute15
5572 	   , p_query_input.attribute15
5573            /**** {{ R12 Enhanced reservations code changes }}****/
5574 	   , p_query_input.crossdock_flag
5575 	   , l_miss_char
5576 	   , p_query_input.crossdock_flag
5577 	   , p_query_input.crossdock_flag
5578 	   , p_query_input.crossdock_criteria_id
5579 	   , l_miss_num
5580 	   , p_query_input.crossdock_criteria_id
5581 	   , p_query_input.crossdock_criteria_id
5582 	   , p_query_input.demand_source_line_detail
5583 	   , l_miss_num
5584 	   , p_query_input.demand_source_line_detail
5585 	   , p_query_input.demand_source_line_detail
5586 	   , p_query_input.supply_receipt_date
5587 	   , l_miss_date
5588 	   , p_query_input.supply_receipt_date
5589 	   , p_query_input.supply_receipt_date
5590 	   , p_query_input.demand_ship_date
5591 	   , l_miss_date
5592 	   , p_query_input.demand_ship_date
5593 	   , p_query_input.demand_ship_date
5594 	   , p_query_input.project_id
5595 	   , l_miss_num
5596 	   , p_query_input.project_id
5597 	   , p_query_input.project_id
5598 	   , p_query_input.task_id
5599 	   , l_miss_num
5600 	   , p_query_input.task_id
5601 	   , p_query_input.task_id
5602 
5603            /***** End R12 ***/
5604 	   ;
5605       END IF;
5606     ELSIF  p_query_input.inventory_item_id <> fnd_api.g_miss_num
5607            AND p_query_input.inventory_item_id IS NOT NULL
5608            AND p_query_input.organization_id <> fnd_api.g_miss_num
5609 	     AND p_query_input.organization_id IS NOT NULL THEN
5610        IF (l_debug = 1) then
5611 	  debug_print (' Inside ref cursor for item/org');
5612        END IF;
5613       -- INVCONV - Incorporate secondaries
5614       OPEN l_cursor_ref FOR    'SELECT
5615           reservation_id
5616         , requirement_date
5617         , organization_id
5618         , inventory_item_id
5619         , demand_source_type_id
5620         , demand_source_name
5621         , demand_source_header_id
5622         , demand_source_line_id
5623         , demand_source_delivery
5624         , primary_uom_code
5625         , primary_uom_id
5626         , secondary_uom_code
5627         , secondary_uom_id
5628         , reservation_uom_code
5629         , reservation_uom_id
5630         , reservation_quantity
5631         , primary_reservation_quantity
5632         , secondary_reservation_quantity
5633         , detailed_quantity
5634         , secondary_detailed_quantity
5635         , autodetail_group_id
5636         , external_source_code
5637         , external_source_line_id
5638         , supply_source_type_id
5639         , supply_source_header_id
5640         , supply_source_line_id
5641         , supply_source_name
5642         , supply_source_line_detail
5643         , revision
5644         , subinventory_code
5645         , subinventory_id
5646         , locator_id
5647         , lot_number
5648         , lot_number_id
5649         , pick_slip_number
5650         , lpn_id
5651         , attribute_category
5652         , attribute1
5653         , attribute2
5654         , attribute3
5655         , attribute4
5656         , attribute5
5657         , attribute6
5658         , attribute7
5659         , attribute8
5660         , attribute9
5661         , attribute10
5662         , attribute11
5663         , attribute12
5664         , attribute13
5665         , attribute14
5666         , attribute15
5667         , ship_ready_flag
5668         , staged_flag
5669          /**** {{ R12 Enhanced reservations code changes. Adding new
5670             -- columns for query reservations }}****/
5671 	 , crossdock_flag
5672 	 , crossdock_criteria_id
5673 	 , demand_source_line_detail
5674 	 , serial_reservation_quantity
5675 	 , supply_receipt_date
5676 	 , demand_ship_date
5677 	 , project_id
5678 	 , task_id
5679 	 , orig_supply_source_type_id
5680 	 , orig_supply_source_header_id
5681 	 , orig_supply_source_line_id
5682 	 , orig_supply_source_line_detail
5683 	 , orig_demand_source_type_id
5684 	 , orig_demand_source_header_id
5685 	 , orig_demand_source_line_id
5686 	 , orig_demand_source_line_detail
5687          , serial_number
5688          /***** End R12 ***/
5689         FROM mtl_reservations
5690         WHERE
5691         inventory_item_id = :inventory_item_id
5692         and organization_id = :organization_id
5693         AND
5694    (:requirement_date = :l_miss_date
5695          OR :requirement_date IS NULL
5696          AND requirement_date IS NULL
5697          OR :requirement_date
5698          = requirement_date
5699          )
5700   AND
5701   (:demand_source_type_id = :l_miss_num
5702          OR :demand_source_type_id IS NULL
5703          AND demand_source_type_id IS NULL
5704          OR :demand_source_type_id
5705          = demand_source_type_id
5706          )
5707         AND
5708         (:demand_source_header_id = :l_miss_num
5709          OR :demand_source_header_id IS NULL
5710          AND demand_source_header_id IS NULL
5711          OR :demand_source_header_id
5712          = demand_source_header_id
5713          )
5714         AND
5715         (:demand_source_line_id = :l_miss_num
5716          OR :demand_source_line_id IS NULL
5717          AND demand_source_line_id IS NULL
5718          OR :demand_source_line_id = demand_source_line_id
5719          )
5720         AND
5721         (:demand_source_name = :l_miss_char
5722          OR :demand_source_name IS NULL
5723          AND demand_source_name IS NULL
5724          OR :demand_source_name = demand_source_name
5725          )
5726         AND
5727         (:demand_source_delivery = :l_miss_num
5728          OR :demand_source_delivery IS NULL
5729          AND demand_source_delivery IS NULL
5730          OR :demand_source_delivery = demand_source_delivery
5731          )
5732       AND
5733         (:primary_uom_code = :l_miss_char
5734          OR :primary_uom_code IS NULL
5735          AND primary_uom_code IS NULL
5736          OR :primary_uom_code = primary_uom_code
5737          )
5738         AND
5739         (:primary_uom_id = :l_miss_num
5740          OR :primary_uom_id IS NULL
5741          AND primary_uom_id IS NULL
5742          OR :primary_uom_id = primary_uom_id
5743          )
5744          -- INVCONV BEGIN
5745         AND
5746         (:secondary_uom_code = :l_miss_char
5747          OR :secondary_uom_code IS NULL
5748          AND secondary_uom_code IS NULL
5749          OR :secondary_uom_code = secondary_uom_code
5750          )
5751         AND
5752         (:secondary_uom_id = :l_miss_num
5753          OR :secondary_uom_id IS NULL
5754          AND secondary_uom_id IS NULL
5755          OR :secondary_uom_id = secondary_uom_id
5756          )
5757         -- INVCONV END
5758         AND
5759         (:reservation_uom_code = :l_miss_char
5760          OR :reservation_uom_code IS NULL
5761          AND reservation_uom_code IS NULL
5762          OR :reservation_uom_code = reservation_uom_code
5763          )
5764         AND
5765         (:reservation_uom_id = :l_miss_num
5766          OR :reservation_uom_id IS NULL
5767          AND reservation_uom_id IS NULL
5768          OR :reservation_uom_id = reservation_uom_id
5769          )
5770         AND
5771         (:autodetail_group_id = :l_miss_num
5772          OR :autodetail_group_id IS NULL
5773          AND autodetail_group_id IS NULL
5774          OR :autodetail_group_id = autodetail_group_id
5775          )
5776         AND
5777         (:external_source_code = :l_miss_char
5778          OR :external_source_code IS NULL
5779          AND external_source_code IS NULL
5780          OR :external_source_code = external_source_code
5781          )
5782         AND
5783         (:external_source_line_id = :l_miss_num
5784          OR :external_source_line_id IS NULL
5785          AND external_source_line_id IS NULL
5786          OR :external_source_line_id = external_source_line_id
5787          )
5788         AND
5789         (:supply_source_type_id = :l_miss_num
5790          OR :supply_source_type_id IS NULL
5791          AND supply_source_type_id IS NULL
5792          OR :supply_source_type_id = supply_source_type_id
5793          )
5794         AND
5795         (:supply_source_header_id = :l_miss_num
5796          OR :supply_source_header_id IS NULL
5797          AND supply_source_header_id IS NULL
5798          OR :supply_source_header_id
5799          = supply_source_header_id
5800          )
5801         AND
5802         (:supply_source_line_id = :l_miss_num
5803          OR :supply_source_line_id IS NULL
5804          AND supply_source_line_id IS NULL
5805          OR :supply_source_line_id = supply_source_line_id
5806          )
5807         AND
5808         (:supply_source_name = :l_miss_char
5809          OR :supply_source_name IS NULL
5810          AND supply_source_name IS NULL
5811          OR :supply_source_name = supply_source_name
5812          )
5813         AND
5814         (:supply_source_line_detail = :l_miss_num
5815          OR :supply_source_line_detail IS NULL
5816          AND supply_source_line_detail IS NULL
5817          OR :supply_source_line_detail
5818          = supply_source_line_detail
5819          )
5820         AND
5821         (:revision = :l_miss_char
5822          OR :revision IS NULL
5823          AND revision IS NULL
5824          OR :revision = revision
5825          )
5826         AND
5827         (:subinventory_code = :l_miss_char
5828          OR :subinventory_code IS NULL
5829          AND subinventory_code IS NULL
5830          OR :subinventory_code = subinventory_code
5831          )
5832        AND
5833         (:subinventory_id = :l_miss_num
5834          OR :subinventory_id IS NULL
5835          AND subinventory_id IS NULL
5836          OR :subinventory_id = subinventory_id
5837          )
5838         AND
5839         (:locator_id = :l_miss_num
5840          OR :locator_id IS NULL
5841          AND locator_id IS NULL
5842          OR :locator_id = locator_id
5843          )
5844         AND
5845         (:lot_number = :l_miss_char
5846          OR :lot_number IS NULL
5847          AND lot_number IS NULL
5848          OR :lot_number = lot_number
5849          )
5850         AND
5851         (:lot_number_id = :l_miss_num
5852          OR :lot_number_id IS NULL
5853          AND lot_number_id IS NULL
5854          OR :lot_number_id = lot_number_id
5855          )
5856         AND
5857         (:lpn_id = :l_miss_num
5858          OR :lpn_id IS NULL
5859          AND lpn_id IS NULL
5860          OR :lpn_id = lpn_id
5861          )
5862         AND
5863         (:ship_ready_flag = :l_miss_num
5864          OR (:ship_ready_flag IS NULL OR :ship_ready_flag = 2)
5865          AND (ship_ready_flag IS NULL OR ship_ready_flag = 2)
5866          OR :ship_ready_flag = ship_ready_flag
5867          )
5868         AND
5869         (:staged_flag = :l_miss_char
5870          OR (:staged_flag IS NULL OR :staged_flag = ''N'')
5871          AND (staged_flag IS NULL OR staged_flag = ''N'')
5872          OR :staged_flag = staged_flag
5873          )
5874         AND
5875         (:attribute_category = :l_miss_char
5876          OR :attribute_category IS NULL
5877          AND attribute_category IS NULL
5878          OR :attribute_category = attribute_category
5879          )
5880         AND
5881         (:attribute1 = :l_miss_char
5882          OR :attribute1 IS NULL
5883          AND attribute1 IS NULL
5884          OR :attribute1 = attribute1
5885          )
5886        AND
5887         (:attribute2 = :l_miss_char
5888          OR :attribute2 IS NULL
5889          AND attribute2 IS NULL
5890          OR :attribute2 = attribute2
5891          )
5892         AND
5893         (:attribute3 = :l_miss_char
5894          OR :attribute3 IS NULL
5895          AND attribute3 IS NULL
5896          OR :attribute3 = attribute3
5897          )
5898         AND
5899         (:attribute4 = :l_miss_char
5900          OR :attribute4 IS NULL
5901          AND attribute4 IS NULL
5902          OR :attribute4 = attribute4
5903          )
5904        AND
5905         (:attribute5 = :l_miss_char
5906          OR :attribute5 IS NULL
5907          AND attribute5 IS NULL
5908          OR :attribute5 = attribute5
5909          )
5910         AND
5911         (:attribute6 = :l_miss_char
5912          OR :attribute6 IS NULL
5913          AND attribute6 IS NULL
5914          OR :attribute6 = attribute6
5915          )
5916         AND
5917         (:attribute7 = :l_miss_char
5918          OR :attribute7 IS NULL
5919          AND attribute7 IS NULL
5920          OR :attribute7 = attribute7
5921          )
5922         AND
5923         (:attribute8 = :l_miss_char
5924          OR :attribute8 IS NULL
5925          AND attribute8 IS NULL
5926          OR :attribute8 = attribute8
5927          )
5928         AND
5929         (:attribute9 = :l_miss_char
5930          OR :attribute9 IS NULL
5931          AND attribute9 IS NULL
5932          OR :attribute9 = attribute9
5933          )
5934         AND
5935         (:attribute10 = :l_miss_char
5936          OR :attribute10 IS NULL
5937          AND attribute10 IS NULL
5938          OR :attribute10 = attribute10
5939          )
5940         AND
5941         (:attribute11 = :l_miss_char
5942          OR :attribute11 IS NULL
5943          AND attribute11 IS NULL
5944          OR :attribute11 = attribute11
5945          )
5946         AND
5947         (:attribute12 = :l_miss_char
5948          OR :attribute12 IS NULL
5949          AND attribute12 IS NULL
5950          OR :attribute12 = attribute12
5951          )
5952         AND
5953         (:attribute13 = :l_miss_char
5954          OR :attribute13 IS NULL
5955          AND attribute13 IS NULL
5956          OR :attribute13 = attribute13
5957          )
5958         AND
5959         (:attribute14 = :l_miss_char
5960          OR :attribute14 IS NULL
5961          AND attribute14 IS NULL
5962          OR :attribute14 = attribute14
5963          )
5964         AND
5965         (:attribute15 = :l_miss_char
5966          OR :attribute15 IS NULL
5967          AND attribute15 IS NULL
5968          OR :attribute15 = attribute15
5969          )
5970 
5971 /**** {{ R12 Enhanced reservations code changes }}****/
5972 	AND
5973 	   (:crossdock_flag = :l_miss_char
5974          OR :crossdock_flag IS NULL
5975          AND crossdock_flag IS NULL
5976          OR :crossdock_flag = crossdock_flag
5977          )
5978 	AND
5979 	   (:crossdock_criteria_id = :l_miss_num
5980          OR :crossdock_criteria_id IS NULL
5981          AND crossdock_criteria_id IS NULL
5982          OR :crossdock_criteria_id = crossdock_criteria_id
5983 	    )
5984         AND
5985 	   (:demand_source_line_detail = :l_miss_num
5986          OR :demand_source_line_detail IS NULL
5987          AND demand_source_line_detail IS NULL
5988          OR :demand_source_line_detail = demand_source_line_detail
5989 	    )
5990 	AND
5991 	   (:supply_receipt_date = :l_miss_date
5992          OR :supply_receipt_date IS NULL
5993          AND supply_receipt_date IS NULL
5994          OR :supply_receipt_date = supply_receipt_date
5995 	    )
5996 	AND
5997 	   (:demand_ship_date = :l_miss_date
5998          OR :demand_ship_date IS NULL
5999          AND demand_ship_date IS NULL
6000          OR :demand_ship_date = demand_ship_date
6001 	    )
6002 	AND
6003 	   (:project_id = :l_miss_num
6004          OR :project_id IS NULL
6005          AND project_id IS NULL
6006          OR :project_id = project_id
6007 	    )
6008 	 AND
6009 	   (:task_id = :l_miss_num
6010          OR :task_id IS NULL
6011          AND task_id IS NULL
6012          OR :task_id = task_id
6013 	    )
6014 /***** End R12 ***/
6015 
6016   '
6017                             || l_lock_stmt
6018                             || l_sort_stmt
6019         USING  p_query_input.inventory_item_id
6020              , p_query_input.organization_id
6021              , p_query_input.requirement_date
6022              , l_miss_date
6023              , p_query_input.requirement_date
6024              , p_query_input.requirement_date
6025              , p_query_input.demand_source_type_id
6026              , l_miss_num
6027              , p_query_input.demand_source_type_id
6028              , p_query_input.demand_source_type_id
6029              , p_query_input.demand_source_header_id
6030              , l_miss_num
6031              , p_query_input.demand_source_header_id
6032              , p_query_input.demand_source_header_id
6033              , p_query_input.demand_source_line_id
6034              , l_miss_num
6035              , p_query_input.demand_source_line_id
6036              , p_query_input.demand_source_line_id
6037              , p_query_input.demand_source_name
6038              , l_miss_char
6039              , p_query_input.demand_source_name
6040              , p_query_input.demand_source_name
6041              , p_query_input.demand_source_delivery
6042              , l_miss_num
6043              , p_query_input.demand_source_delivery
6044              , p_query_input.demand_source_delivery
6045              , p_query_input.primary_uom_code
6046              , l_miss_char
6047              , p_query_input.primary_uom_code
6048              , p_query_input.primary_uom_code
6049              , p_query_input.primary_uom_id
6050              , l_miss_num
6051              , p_query_input.primary_uom_id
6052              , p_query_input.primary_uom_id
6053               -- INVCONV BEGIN
6054              , p_query_input.secondary_uom_code
6055              , l_miss_char
6056              , p_query_input.secondary_uom_code
6057              , p_query_input.secondary_uom_code
6058              , p_query_input.secondary_uom_id
6059              , l_miss_num
6060              , p_query_input.secondary_uom_id
6061              , p_query_input.secondary_uom_id
6062              -- INVCONV END
6063              , p_query_input.reservation_uom_code
6064              , l_miss_char
6065              , p_query_input.reservation_uom_code
6066              , p_query_input.reservation_uom_code
6067              , p_query_input.reservation_uom_id
6068              , l_miss_num
6069              , p_query_input.reservation_uom_id
6070              , p_query_input.reservation_uom_id
6071              , p_query_input.autodetail_group_id
6072              , l_miss_num
6073              , p_query_input.autodetail_group_id
6074              , p_query_input.autodetail_group_id
6075              , p_query_input.external_source_code
6076              , l_miss_char
6077              , p_query_input.external_source_code
6078              , p_query_input.external_source_code
6079              , p_query_input.external_source_line_id
6080              , l_miss_num
6081              , p_query_input.external_source_line_id
6082              , p_query_input.external_source_line_id
6083              , p_query_input.supply_source_type_id
6084              , l_miss_num
6085              , p_query_input.supply_source_type_id
6086              , p_query_input.supply_source_type_id
6087              , p_query_input.supply_source_header_id
6088              , l_miss_num
6089              , p_query_input.supply_source_header_id
6090              , p_query_input.supply_source_header_id
6091              , p_query_input.supply_source_line_id
6092              , l_miss_num
6093              , p_query_input.supply_source_line_id
6094              , p_query_input.supply_source_line_id
6095              , p_query_input.supply_source_name
6096              , l_miss_char
6097              , p_query_input.supply_source_name
6098              , p_query_input.supply_source_name
6099              , p_query_input.supply_source_line_detail
6100              , l_miss_num
6101              , p_query_input.supply_source_line_detail
6102              , p_query_input.supply_source_line_detail
6103              , p_query_input.revision
6104              , l_miss_char
6105              , p_query_input.revision
6106              , p_query_input.revision
6107              , p_query_input.subinventory_code
6108              , l_miss_char
6109              , p_query_input.subinventory_code
6110              , p_query_input.subinventory_code
6111              , p_query_input.subinventory_id
6112              , l_miss_num
6113              , p_query_input.subinventory_id
6114              , p_query_input.subinventory_id
6115              , p_query_input.locator_id
6116              , l_miss_num
6117              , p_query_input.locator_id
6118              , p_query_input.locator_id
6119              , p_query_input.lot_number
6120              , l_miss_char
6121              , p_query_input.lot_number
6122              , p_query_input.lot_number
6123              , p_query_input.lot_number_id
6124              , l_miss_num
6125              , p_query_input.lot_number_id
6126              , p_query_input.lot_number_id
6127              , p_query_input.lpn_id
6128              , l_miss_num
6129              , p_query_input.lpn_id
6130              , p_query_input.lpn_id
6131              , p_query_input.ship_ready_flag
6132              , l_miss_num
6133              , p_query_input.ship_ready_flag
6134              , p_query_input.ship_ready_flag
6135              , p_query_input.ship_ready_flag
6136              , p_query_input.staged_flag
6137              , l_miss_char
6138              , p_query_input.staged_flag
6139              , p_query_input.staged_flag
6140              , p_query_input.staged_flag
6141              , p_query_input.attribute_category
6142              , l_miss_char
6143              , p_query_input.attribute_category
6144              , p_query_input.attribute_category
6145              , p_query_input.attribute1
6146              , l_miss_char
6147              , p_query_input.attribute1
6148              , p_query_input.attribute1
6149              , p_query_input.attribute2
6150              , l_miss_char
6151              , p_query_input.attribute2
6152              , p_query_input.attribute2
6153              , p_query_input.attribute3
6154              , l_miss_char
6155              , p_query_input.attribute3
6156              , p_query_input.attribute3
6157              , p_query_input.attribute4
6158              , l_miss_char
6159              , p_query_input.attribute4
6160              , p_query_input.attribute4
6161              , p_query_input.attribute5
6162              , l_miss_char
6163              , p_query_input.attribute5
6164              , p_query_input.attribute5
6165              , p_query_input.attribute6
6166              , l_miss_char
6167              , p_query_input.attribute6
6168              , p_query_input.attribute6
6169              , p_query_input.attribute7
6170              , l_miss_char
6171              , p_query_input.attribute7
6172              , p_query_input.attribute7
6173              , p_query_input.attribute8
6174              , l_miss_char
6175              , p_query_input.attribute8
6176              , p_query_input.attribute8
6177              , p_query_input.attribute9
6178              , l_miss_char
6179              , p_query_input.attribute9
6180              , p_query_input.attribute9
6181              , p_query_input.attribute10
6182              , l_miss_char
6183              , p_query_input.attribute10
6184              , p_query_input.attribute10
6185              , p_query_input.attribute11
6186              , l_miss_char
6187              , p_query_input.attribute11
6188              , p_query_input.attribute11
6189              , p_query_input.attribute12
6190              , l_miss_char
6191              , p_query_input.attribute12
6192              , p_query_input.attribute12
6193              , p_query_input.attribute13
6194              , l_miss_char
6195              , p_query_input.attribute13
6196              , p_query_input.attribute13
6197              , p_query_input.attribute14
6198              , l_miss_char
6199              , p_query_input.attribute14
6200              , p_query_input.attribute14
6201              , p_query_input.attribute15
6202              , l_miss_char
6203              , p_query_input.attribute15
6204 	   , p_query_input.attribute15
6205  /**** {{ R12 Enhanced reservations code changes }}****/
6206 	   , p_query_input.crossdock_flag
6207 	   , l_miss_char
6208 	   , p_query_input.crossdock_flag
6209 	   , p_query_input.crossdock_flag
6210 	   , p_query_input.crossdock_criteria_id
6211 	   , l_miss_num
6212 	   , p_query_input.crossdock_criteria_id
6213 	   , p_query_input.crossdock_criteria_id
6214 	   , p_query_input.demand_source_line_detail
6215 	   , l_miss_num
6216 	   , p_query_input.demand_source_line_detail
6217 	   , p_query_input.demand_source_line_detail
6218 	   , p_query_input.supply_receipt_date
6219 	   , l_miss_date
6220 	   , p_query_input.supply_receipt_date
6221 	   , p_query_input.supply_receipt_date
6222 	   , p_query_input.demand_ship_date
6223 	   , l_miss_date
6224 	   , p_query_input.demand_ship_date
6225 	   , p_query_input.demand_ship_date
6226 	   , p_query_input.project_id
6227 	   , l_miss_num
6228 	   , p_query_input.project_id
6229 	   , p_query_input.project_id
6230 	   , p_query_input.task_id
6231 	   , l_miss_num
6232 	   , p_query_input.task_id
6233 	   , p_query_input.task_id
6234            /***** End R12 ***/
6235 	   ;
6236 
6237       -- kkoothan Added the following  ELSE IF section as part of Bug Fix:2783806
6238       ELSIF p_query_input.supply_source_header_id <  fnd_api.g_miss_num
6239        AND p_query_input.supply_source_header_id IS NOT NULL
6240 --      Bug 4881317 checking these conditions later
6241 --      AND p_query_input.supply_source_line_id <  fnd_api.g_miss_num
6242 --      AND p_query_input.supply_source_line_id IS NOT NULL
6243        AND p_query_input.supply_source_type_id <  fnd_api.g_miss_num
6244 	 AND p_query_input.supply_source_type_id IS NOT NULL  THEN
6245        IF (l_debug = 1) then
6246 	  debug_print(' Inside ref cursor for supply');
6247        END IF;
6248         -- INVCONV - Incorporate secondaries
6249       l_qry_stmt:= 'SELECT
6250             reservation_id
6251           , requirement_date
6252           , organization_id
6253           , inventory_item_id
6254           , demand_source_type_id
6255           , demand_source_name
6256           , demand_source_header_id
6257           , demand_source_line_id
6258           , demand_source_delivery
6259           , primary_uom_code
6260           , primary_uom_id
6261           , secondary_uom_code
6262           , secondary_uom_id
6263           , reservation_uom_code
6264           , reservation_uom_id
6265           , reservation_quantity
6266           , primary_reservation_quantity
6267           , secondary_reservation_quantity
6268           , detailed_quantity
6269           , secondary_detailed_quantity
6270           , autodetail_group_id
6271           , external_source_code
6272           , external_source_line_id
6273           , supply_source_type_id
6274           , supply_source_header_id
6275           , supply_source_line_id
6276           , supply_source_name
6277           , supply_source_line_detail
6278           , revision
6279           , subinventory_code
6280           , subinventory_id
6281           , locator_id
6282           , lot_number
6283           , lot_number_id
6284           , pick_slip_number
6285           , lpn_id
6286           , attribute_category
6287           , attribute1
6288           , attribute2
6289           , attribute3
6290           , attribute4
6291           , attribute5
6292           , attribute6
6293           , attribute7
6294           , attribute8
6295           , attribute9
6296           , attribute10
6297           , attribute11
6298           , attribute12
6299           , attribute13
6300           , attribute14
6301           , attribute15
6302           , ship_ready_flag
6303           , staged_flag
6304 	  /**** {{ R12 Enhanced reservations code changes }}****/
6305 	 , crossdock_flag
6306 	 , crossdock_criteria_id
6307 	 , demand_source_line_detail
6308 	 , serial_reservation_quantity
6309 	 , supply_receipt_date
6310 	 , demand_ship_date
6311 	 , project_id
6312 	 , task_id
6313 	 , orig_supply_source_type_id
6314 	 , orig_supply_source_header_id
6315 	 , orig_supply_source_line_id
6316 	 , orig_supply_source_line_detail
6317 	 , orig_demand_source_type_id
6318 	 , orig_demand_source_header_id
6319 	 , orig_demand_source_line_id
6320 	 , orig_demand_source_line_detail
6321          , serial_number
6322 	 /***** End R12 ***/
6323           FROM mtl_reservations
6324           WHERE
6325           supply_source_header_id = :supply_source_header_id
6326           and supply_source_type_id = :supply_source_type_id
6327           AND
6328            (:requirement_date = :l_miss_date
6329            OR :requirement_date IS NULL
6330            AND requirement_date IS NULL
6331            OR :requirement_date
6332            = requirement_date
6333            )
6334           AND
6335           (:organization_id = :l_miss_num
6336            OR :organization_id IS NULL
6337            AND organization_id IS NULL
6338            OR :organization_id = organization_id
6339            )
6340           AND
6341           (:inventory_item_id = :l_miss_num
6342            OR :inventory_item_id IS NULL
6343            AND inventory_item_id IS NULL
6344            OR :inventory_item_id = inventory_item_id
6345            )
6346           AND
6347           (:demand_source_type_id = :l_miss_num
6348            OR :demand_source_type_id IS NULL
6349            AND demand_source_type_id IS NULL
6350            OR :demand_source_type_id
6351            = demand_source_type_id
6352            )
6353           AND
6354           (:demand_source_header_id = :l_miss_num
6355            OR :demand_source_header_id IS NULL
6356            AND demand_source_header_id IS NULL
6357            OR :demand_source_header_id
6358            = demand_source_header_id
6359            )
6360           AND
6361           (:demand_source_line_id = :l_miss_num
6362            OR :demand_source_line_id IS NULL
6363            AND demand_source_line_id IS NULL
6364            OR :demand_source_line_id = demand_source_line_id
6365            )
6366           AND
6367           (:demand_source_name = :l_miss_char
6368            OR :demand_source_name IS NULL
6369            AND demand_source_name IS NULL
6370            OR :demand_source_name = demand_source_name
6371            )
6372           AND
6373           (:demand_source_delivery = :l_miss_num
6374            OR :demand_source_delivery IS NULL
6375            AND demand_source_delivery IS NULL
6376            OR :demand_source_delivery = demand_source_delivery
6377            )
6378         AND
6379           (:primary_uom_code = :l_miss_char
6380            OR :primary_uom_code IS NULL
6381            AND primary_uom_code IS NULL
6382            OR :primary_uom_code = primary_uom_code
6383            )
6384           AND
6385           (:primary_uom_id = :l_miss_num
6386            OR :primary_uom_id IS NULL
6387            AND primary_uom_id IS NULL
6388            OR :primary_uom_id = primary_uom_id
6389            )
6390            -- INVCONV BEGIN
6391           AND
6392           (:secondary_uom_code = :l_miss_char
6393            OR :secondary_uom_code IS NULL
6394            AND secondary_uom_code IS NULL
6395            OR :secondary_uom_code = secondary_uom_code
6396            )
6397           AND
6398           (:secondary_uom_id = :l_miss_num
6399            OR :secondary_uom_id IS NULL
6400            AND secondary_uom_id IS NULL
6401            OR :secondary_uom_id = secondary_uom_id
6402            )
6403           -- INVCONV END
6404           AND
6405           (:reservation_uom_code = :l_miss_char
6406            OR :reservation_uom_code IS NULL
6407            AND reservation_uom_code IS NULL
6408            OR :reservation_uom_code = reservation_uom_code
6409            )
6410           AND
6411           (:reservation_uom_id = :l_miss_num
6412            OR :reservation_uom_id IS NULL
6413            AND reservation_uom_id IS NULL
6414            OR :reservation_uom_id = reservation_uom_id
6415            )
6416           AND
6417           (:autodetail_group_id = :l_miss_num
6418            OR :autodetail_group_id IS NULL
6419            AND autodetail_group_id IS NULL
6420            OR :autodetail_group_id = autodetail_group_id
6421            )
6422           AND
6423           (:external_source_code = :l_miss_char
6424            OR :external_source_code IS NULL
6425            AND external_source_code IS NULL
6426            OR :external_source_code = external_source_code
6427            )
6428           AND
6429           (:external_source_line_id = :l_miss_num
6430            OR :external_source_line_id IS NULL
6431            AND external_source_line_id IS NULL
6432            OR :external_source_line_id = external_source_line_id
6433            )
6434           AND
6435           (:supply_source_name = :l_miss_char
6436            OR :supply_source_name IS NULL
6437            AND supply_source_name IS NULL
6438            OR :supply_source_name = supply_source_name
6439            )
6440           AND
6441           (:supply_source_line_detail = :l_miss_num
6442            OR :supply_source_line_detail IS NULL
6443            AND supply_source_line_detail IS NULL
6444            OR :supply_source_line_detail
6445            = supply_source_line_detail
6446            )
6447           AND
6448           (:revision = :l_miss_char
6449            OR :revision IS NULL
6450            AND revision IS NULL
6451            OR :revision = revision
6452            )
6453           AND
6454           (:subinventory_code = :l_miss_char
6455            OR :subinventory_code IS NULL
6456            AND subinventory_code IS NULL
6457            OR :subinventory_code = subinventory_code
6458            )
6459          AND
6460           (:subinventory_id = :l_miss_num
6461            OR :subinventory_id IS NULL
6462            AND subinventory_id IS NULL
6463            OR :subinventory_id = subinventory_id
6464            )
6465           AND
6466           (:locator_id = :l_miss_num
6467            OR :locator_id IS NULL
6468            AND locator_id IS NULL
6469            OR :locator_id = locator_id
6470            )
6471           AND
6472           (:lot_number = :l_miss_char
6473            OR :lot_number IS NULL
6474            AND lot_number IS NULL
6475            OR :lot_number = lot_number
6476            )
6477           AND
6478           (:lot_number_id = :l_miss_num
6479            OR :lot_number_id IS NULL
6480            AND lot_number_id IS NULL
6481            OR :lot_number_id = lot_number_id
6482            )
6483           AND
6484           (:lpn_id = :l_miss_num
6485            OR :lpn_id IS NULL
6486            AND lpn_id IS NULL
6487            OR :lpn_id = lpn_id
6488            )
6489           AND
6490           (:ship_ready_flag = :l_miss_num
6491            OR (:ship_ready_flag IS NULL OR :ship_ready_flag = 2)
6492            AND (ship_ready_flag IS NULL OR ship_ready_flag = 2)
6493            OR :ship_ready_flag = ship_ready_flag
6494            )
6495           AND
6496           (:staged_flag = :l_miss_char
6497            OR (:staged_flag IS NULL OR :staged_flag = ''N'')
6498            AND (staged_flag IS NULL OR staged_flag = ''N'')
6499            OR :staged_flag = staged_flag
6500            )
6501           AND
6502           (:attribute_category = :l_miss_char
6503            OR :attribute_category IS NULL
6504            AND attribute_category IS NULL
6505            OR :attribute_category = attribute_category
6506            )
6507           AND
6508           (:attribute1 = :l_miss_char
6509            OR :attribute1 IS NULL
6510            AND attribute1 IS NULL
6511            OR :attribute1 = attribute1
6512            )
6513          AND
6514           (:attribute2 = :l_miss_char
6515            OR :attribute2 IS NULL
6516            AND attribute2 IS NULL
6517            OR :attribute2 = attribute2
6518            )
6519           AND
6520           (:attribute3 = :l_miss_char
6521            OR :attribute3 IS NULL
6522            AND attribute3 IS NULL
6523            OR :attribute3 = attribute3
6524            )
6525           AND
6526           (:attribute4 = :l_miss_char
6527            OR :attribute4 IS NULL
6528            AND attribute4 IS NULL
6529            OR :attribute4 = attribute4
6530            )
6531          AND
6532           (:attribute5 = :l_miss_char
6533            OR :attribute5 IS NULL
6534            AND attribute5 IS NULL
6535            OR :attribute5 = attribute5
6536            )
6537           AND
6538           (:attribute6 = :l_miss_char
6539            OR :attribute6 IS NULL
6540            AND attribute6 IS NULL
6541            OR :attribute6 = attribute6
6542            )
6543           AND
6544           (:attribute7 = :l_miss_char
6545            OR :attribute7 IS NULL
6546            AND attribute7 IS NULL
6547            OR :attribute7 = attribute7
6548            )
6549           AND
6550           (:attribute8 = :l_miss_char
6551            OR :attribute8 IS NULL
6552            AND attribute8 IS NULL
6553            OR :attribute8 = attribute8
6554            )
6555           AND
6556           (:attribute9 = :l_miss_char
6557            OR :attribute9 IS NULL
6558            AND attribute9 IS NULL
6559            OR :attribute9 = attribute9
6560            )
6561           AND
6562           (:attribute10 = :l_miss_char
6563            OR :attribute10 IS NULL
6564            AND attribute10 IS NULL
6565            OR :attribute10 = attribute10
6566            )
6567           AND
6568           (:attribute11 = :l_miss_char
6569            OR :attribute11 IS NULL
6570            AND attribute11 IS NULL
6571            OR :attribute11 = attribute11
6572            )
6573           AND
6574           (:attribute12 = :l_miss_char
6575            OR :attribute12 IS NULL
6576            AND attribute12 IS NULL
6577            OR :attribute12 = attribute12
6578            )
6579           AND
6580           (:attribute13 = :l_miss_char
6581            OR :attribute13 IS NULL
6582            AND attribute13 IS NULL
6583            OR :attribute13 = attribute13
6584            )
6585           AND
6586           (:attribute14 = :l_miss_char
6587            OR :attribute14 IS NULL
6588            AND attribute14 IS NULL
6589            OR :attribute14 = attribute14
6590            )
6591           AND
6592           (:attribute15 = :l_miss_char
6593            OR :attribute15 IS NULL
6594            AND attribute15 IS NULL
6595            OR :attribute15 = attribute15
6596            )
6597 /**** {{ R12 Enhanced reservations code changes }}****/
6598 	AND
6599 	   (:crossdock_flag = :l_miss_char
6600          OR :crossdock_flag IS NULL
6601          AND crossdock_flag IS NULL
6602          OR :crossdock_flag = crossdock_flag
6603          )
6604 	AND
6605 	   (:crossdock_criteria_id = :l_miss_num
6606          OR :crossdock_criteria_id IS NULL
6607          AND crossdock_criteria_id IS NULL
6608          OR :crossdock_criteria_id = crossdock_criteria_id
6609 	    )
6610         AND
6611 	   (:demand_source_line_detail = :l_miss_num
6612          OR :demand_source_line_detail IS NULL
6613          AND demand_source_line_detail IS NULL
6614          OR :demand_source_line_detail = demand_source_line_detail
6615 	    )
6616 	AND
6617 	   (:supply_receipt_date = :l_miss_date
6618          OR :supply_receipt_date IS NULL
6619          AND supply_receipt_date IS NULL
6620          OR :supply_receipt_date = supply_receipt_date
6621 	    )
6622 	AND
6623 	   (:demand_ship_date = :l_miss_date
6624          OR :demand_ship_date IS NULL
6625          AND demand_ship_date IS NULL
6626          OR :demand_ship_date = demand_ship_date
6627 	    )
6628 	AND
6629 	   (:project_id = :l_miss_num
6630          OR :project_id IS NULL
6631          AND project_id IS NULL
6632          OR :project_id = project_id
6633 	    )
6634 	 AND
6635 	   (:task_id = :l_miss_num
6636          OR :task_id IS NULL
6637          AND task_id IS NULL
6638          OR :task_id = task_id
6639 	    )
6640          ';
6641 /***** End R12 ***/
6642 
6643          --Bug 4881317 If supply_source_line_id is passed, append the condition to the query
6644          If  ( p_query_input.supply_source_line_id < fnd_api.g_miss_num
6645                AND p_query_input.supply_source_line_id IS NOT NULL )
6646           THEN
6647               OPEN l_cursor_ref FOR l_qry_stmt
6648                                  || 'AND supply_source_line_id = :supply_source_line_id
6649                                  ' || l_lock_stmt || l_sort_stmt
6650           using
6651            p_query_input.supply_source_header_id
6652           ,p_query_input.supply_source_type_id
6653           ,p_query_input.requirement_date
6654           ,l_miss_date
6655           ,p_query_input.requirement_date
6656           ,p_query_input.requirement_date
6657           ,p_query_input.organization_id
6658           ,l_miss_num
6659           ,p_query_input.organization_id
6660           ,p_query_input.organization_id
6661           ,p_query_input.inventory_item_id
6662           ,l_miss_num
6663           ,p_query_input.inventory_item_id
6664           ,p_query_input.inventory_item_id
6665           ,p_query_input.demand_source_type_id
6666           ,l_miss_num
6667           ,p_query_input.demand_source_type_id
6668           ,p_query_input.demand_source_type_id
6669           ,p_query_input.demand_source_header_id
6670           ,l_miss_num
6671           ,p_query_input.demand_source_header_id
6672           ,p_query_input.demand_source_header_id
6673           ,p_query_input.demand_source_line_id
6674           ,l_miss_num
6675           ,p_query_input.demand_source_line_id
6676           ,p_query_input.demand_source_line_id
6677           ,p_query_input.demand_source_name
6678           ,l_miss_char
6679           ,p_query_input.demand_source_name
6680           ,p_query_input.demand_source_name
6681           ,p_query_input.demand_source_delivery
6682           ,l_miss_num
6683           ,p_query_input.demand_source_delivery
6684           ,p_query_input.demand_source_delivery
6685           ,p_query_input.primary_uom_code
6686           ,l_miss_char
6687           ,p_query_input.primary_uom_code
6688           ,p_query_input.primary_uom_code
6689           ,p_query_input.primary_uom_id
6690           ,l_miss_num
6691           ,p_query_input.primary_uom_id
6692           ,p_query_input.primary_uom_id
6693           -- INVCONV BEGIN
6694           ,p_query_input.secondary_uom_code
6695           ,l_miss_char
6696           ,p_query_input.secondary_uom_code
6697           ,p_query_input.secondary_uom_code
6698           ,p_query_input.secondary_uom_id
6699           ,l_miss_num
6700           ,p_query_input.secondary_uom_id
6701           ,p_query_input.secondary_uom_id
6702           -- INVCONV END
6703           ,p_query_input.reservation_uom_code
6704           ,l_miss_char
6705           ,p_query_input.reservation_uom_code
6706           ,p_query_input.reservation_uom_code
6707           ,p_query_input.reservation_uom_id
6708           ,l_miss_num
6709           ,p_query_input.reservation_uom_id
6710           ,p_query_input.reservation_uom_id
6711           ,p_query_input.autodetail_group_id
6712           ,l_miss_num
6713           ,p_query_input.autodetail_group_id
6714           ,p_query_input.autodetail_group_id
6715           ,p_query_input.external_source_code
6716           ,l_miss_char
6717           ,p_query_input.external_source_code
6718           ,p_query_input.external_source_code
6719           ,p_query_input.external_source_line_id
6720           ,l_miss_num
6721           ,p_query_input.external_source_line_id
6722           ,p_query_input.external_source_line_id
6723           ,p_query_input.supply_source_name
6724           ,l_miss_char
6725           ,p_query_input.supply_source_name
6726           ,p_query_input.supply_source_name
6727           ,p_query_input.supply_source_line_detail
6728           ,l_miss_num
6729           ,p_query_input.supply_source_line_detail
6730           ,p_query_input.supply_source_line_detail
6731           ,p_query_input.revision
6732           ,l_miss_char
6733           ,p_query_input.revision
6734           ,p_query_input.revision
6735           ,p_query_input.subinventory_code
6736           ,l_miss_char
6737           ,p_query_input.subinventory_code
6738           ,p_query_input.subinventory_code
6739           ,p_query_input.subinventory_id
6740           ,l_miss_num
6741           ,p_query_input.subinventory_id
6742           ,p_query_input.subinventory_id
6743           ,p_query_input.locator_id
6744           ,l_miss_num
6745           ,p_query_input.locator_id
6746           ,p_query_input.locator_id
6747           ,p_query_input.lot_number
6748           ,l_miss_char
6749           ,p_query_input.lot_number
6750           ,p_query_input.lot_number
6751           ,p_query_input.lot_number_id
6752           ,l_miss_num
6753           ,p_query_input.lot_number_id
6754           ,p_query_input.lot_number_id
6755           ,p_query_input.lpn_id
6756           ,l_miss_num
6757           ,p_query_input.lpn_id
6758           ,p_query_input.lpn_id
6759           ,p_query_input.ship_ready_flag
6760           ,l_miss_num
6761           ,p_query_input.ship_ready_flag
6762           ,p_query_input.ship_ready_flag
6763           ,p_query_input.ship_ready_flag
6764           ,p_query_input.staged_flag
6765           ,l_miss_char
6766           ,p_query_input.staged_flag
6767           ,p_query_input.staged_flag
6768           ,p_query_input.staged_flag
6769           ,p_query_input.attribute_category
6770           ,l_miss_char
6771           ,p_query_input.attribute_category
6772           ,p_query_input.attribute_category
6773           ,p_query_input.attribute1
6774           ,l_miss_char
6775           ,p_query_input.attribute1
6776           ,p_query_input.attribute1
6777           ,p_query_input.attribute2
6778           ,l_miss_char
6779           ,p_query_input.attribute2
6780           ,p_query_input.attribute2
6781           ,p_query_input.attribute3
6782           ,l_miss_char
6783           ,p_query_input.attribute3
6784           ,p_query_input.attribute3
6785           ,p_query_input.attribute4
6786           ,l_miss_char
6787           ,p_query_input.attribute4
6788           ,p_query_input.attribute4
6789           ,p_query_input.attribute5
6790           ,l_miss_char
6791           ,p_query_input.attribute5
6792           ,p_query_input.attribute5
6793           ,p_query_input.attribute6
6794           ,l_miss_char
6795           ,p_query_input.attribute6
6796           ,p_query_input.attribute6
6797           ,p_query_input.attribute7
6798           ,l_miss_char
6799           ,p_query_input.attribute7
6800           ,p_query_input.attribute7
6801           ,p_query_input.attribute8
6802           ,l_miss_char
6803           ,p_query_input.attribute8
6804           ,p_query_input.attribute8
6805           ,p_query_input.attribute9
6806           ,l_miss_char
6807           ,p_query_input.attribute9
6808           ,p_query_input.attribute9
6809           ,p_query_input.attribute10
6810           ,l_miss_char
6811           ,p_query_input.attribute10
6812           ,p_query_input.attribute10
6813           ,p_query_input.attribute11
6814           ,l_miss_char
6815           ,p_query_input.attribute11
6816           ,p_query_input.attribute11
6817           ,p_query_input.attribute12
6818           ,l_miss_char
6819           ,p_query_input.attribute12
6820           ,p_query_input.attribute12
6821           ,p_query_input.attribute13
6822           ,l_miss_char
6823           ,p_query_input.attribute13
6824           ,p_query_input.attribute13
6825           ,p_query_input.attribute14
6826           ,l_miss_char
6827           ,p_query_input.attribute14
6828           ,p_query_input.attribute14
6829           ,p_query_input.attribute15
6830           ,l_miss_char
6831           ,p_query_input.attribute15
6832 	     ,p_query_input.attribute15
6833  /**** {{ R12 Enhanced reservations code changes }}****/
6834 	   , p_query_input.crossdock_flag
6835 	   , l_miss_char
6836 	   , p_query_input.crossdock_flag
6837 	   , p_query_input.crossdock_flag
6838 	   , p_query_input.crossdock_criteria_id
6839 	   , l_miss_num
6840 	   , p_query_input.crossdock_criteria_id
6841 	   , p_query_input.crossdock_criteria_id
6842 	   , p_query_input.demand_source_line_detail
6843 	   , l_miss_num
6844 	   , p_query_input.demand_source_line_detail
6845 	   , p_query_input.demand_source_line_detail
6846 	   , p_query_input.supply_receipt_date
6847 	   , l_miss_date
6848 	   , p_query_input.supply_receipt_date
6849 	   , p_query_input.supply_receipt_date
6850 	   , p_query_input.demand_ship_date
6851 	   , l_miss_date
6852 	   , p_query_input.demand_ship_date
6853 	   , p_query_input.demand_ship_date
6854 	   , p_query_input.project_id
6855 	   , l_miss_num
6856 	   , p_query_input.project_id
6857 	   , p_query_input.project_id
6858 	   , p_query_input.task_id
6859 	   , l_miss_num
6860 	   , p_query_input.task_id
6861 	   , p_query_input.task_id
6862            ,p_query_input.supply_source_line_id
6863 
6864 	   /***** End R12 ***/
6865 	     ;
6866            ELSE
6867               OPEN l_cursor_ref FOR l_qry_stmt
6868                                  || l_lock_stmt || l_sort_stmt
6869           using
6870            p_query_input.supply_source_header_id
6871           ,p_query_input.supply_source_type_id
6872           ,p_query_input.requirement_date
6873           ,l_miss_date
6874           ,p_query_input.requirement_date
6875           ,p_query_input.requirement_date
6876           ,p_query_input.organization_id
6877           ,l_miss_num
6878           ,p_query_input.organization_id
6879           ,p_query_input.organization_id
6880           ,p_query_input.inventory_item_id
6881           ,l_miss_num
6882           ,p_query_input.inventory_item_id
6883           ,p_query_input.inventory_item_id
6884           ,p_query_input.demand_source_type_id
6885           ,l_miss_num
6886           ,p_query_input.demand_source_type_id
6887           ,p_query_input.demand_source_type_id
6888           ,p_query_input.demand_source_header_id
6889           ,l_miss_num
6890           ,p_query_input.demand_source_header_id
6891           ,p_query_input.demand_source_header_id
6892           ,p_query_input.demand_source_line_id
6893           ,l_miss_num
6894           ,p_query_input.demand_source_line_id
6895           ,p_query_input.demand_source_line_id
6896           ,p_query_input.demand_source_name
6897           ,l_miss_char
6898           ,p_query_input.demand_source_name
6899           ,p_query_input.demand_source_name
6900           ,p_query_input.demand_source_delivery
6901           ,l_miss_num
6902           ,p_query_input.demand_source_delivery
6903           ,p_query_input.demand_source_delivery
6904           ,p_query_input.primary_uom_code
6905           ,l_miss_char
6906           ,p_query_input.primary_uom_code
6907           ,p_query_input.primary_uom_code
6908           ,p_query_input.primary_uom_id
6909           ,l_miss_num
6910           ,p_query_input.primary_uom_id
6911           ,p_query_input.primary_uom_id
6912           -- INVCONV BEGIN
6913           ,p_query_input.secondary_uom_code
6914           ,l_miss_char
6915           ,p_query_input.secondary_uom_code
6916           ,p_query_input.secondary_uom_code
6917           ,p_query_input.secondary_uom_id
6918           ,l_miss_num
6919           ,p_query_input.secondary_uom_id
6920           ,p_query_input.secondary_uom_id
6921           -- INVCONV END
6922           ,p_query_input.reservation_uom_code
6923           ,l_miss_char
6924           ,p_query_input.reservation_uom_code
6925           ,p_query_input.reservation_uom_code
6926           ,p_query_input.reservation_uom_id
6927           ,l_miss_num
6928           ,p_query_input.reservation_uom_id
6929           ,p_query_input.reservation_uom_id
6930           ,p_query_input.autodetail_group_id
6931           ,l_miss_num
6932           ,p_query_input.autodetail_group_id
6933           ,p_query_input.autodetail_group_id
6934           ,p_query_input.external_source_code
6935           ,l_miss_char
6936           ,p_query_input.external_source_code
6937           ,p_query_input.external_source_code
6938           ,p_query_input.external_source_line_id
6939           ,l_miss_num
6940           ,p_query_input.external_source_line_id
6941           ,p_query_input.external_source_line_id
6942           ,p_query_input.supply_source_name
6943           ,l_miss_char
6944           ,p_query_input.supply_source_name
6945           ,p_query_input.supply_source_name
6946           ,p_query_input.supply_source_line_detail
6947           ,l_miss_num
6948           ,p_query_input.supply_source_line_detail
6949           ,p_query_input.supply_source_line_detail
6950           ,p_query_input.revision
6951           ,l_miss_char
6952           ,p_query_input.revision
6953           ,p_query_input.revision
6954           ,p_query_input.subinventory_code
6955           ,l_miss_char
6956           ,p_query_input.subinventory_code
6957           ,p_query_input.subinventory_code
6958           ,p_query_input.subinventory_id
6959           ,l_miss_num
6960           ,p_query_input.subinventory_id
6961           ,p_query_input.subinventory_id
6962           ,p_query_input.locator_id
6963           ,l_miss_num
6964           ,p_query_input.locator_id
6965           ,p_query_input.locator_id
6966           ,p_query_input.lot_number
6967           ,l_miss_char
6968           ,p_query_input.lot_number
6969           ,p_query_input.lot_number
6970           ,p_query_input.lot_number_id
6971           ,l_miss_num
6972           ,p_query_input.lot_number_id
6973           ,p_query_input.lot_number_id
6974           ,p_query_input.lpn_id
6975           ,l_miss_num
6976           ,p_query_input.lpn_id
6977           ,p_query_input.lpn_id
6978           ,p_query_input.ship_ready_flag
6979           ,l_miss_num
6980           ,p_query_input.ship_ready_flag
6981           ,p_query_input.ship_ready_flag
6982           ,p_query_input.ship_ready_flag
6983           ,p_query_input.staged_flag
6984           ,l_miss_char
6985           ,p_query_input.staged_flag
6986           ,p_query_input.staged_flag
6987           ,p_query_input.staged_flag
6988           ,p_query_input.attribute_category
6989           ,l_miss_char
6990           ,p_query_input.attribute_category
6991           ,p_query_input.attribute_category
6992           ,p_query_input.attribute1
6993           ,l_miss_char
6994           ,p_query_input.attribute1
6995           ,p_query_input.attribute1
6996           ,p_query_input.attribute2
6997           ,l_miss_char
6998           ,p_query_input.attribute2
6999           ,p_query_input.attribute2
7000           ,p_query_input.attribute3
7001           ,l_miss_char
7002           ,p_query_input.attribute3
7003           ,p_query_input.attribute3
7004           ,p_query_input.attribute4
7005           ,l_miss_char
7006           ,p_query_input.attribute4
7007           ,p_query_input.attribute4
7008           ,p_query_input.attribute5
7009           ,l_miss_char
7010           ,p_query_input.attribute5
7011           ,p_query_input.attribute5
7012           ,p_query_input.attribute6
7013           ,l_miss_char
7014           ,p_query_input.attribute6
7015           ,p_query_input.attribute6
7016           ,p_query_input.attribute7
7017           ,l_miss_char
7018           ,p_query_input.attribute7
7019           ,p_query_input.attribute7
7020           ,p_query_input.attribute8
7021           ,l_miss_char
7022           ,p_query_input.attribute8
7023           ,p_query_input.attribute8
7024           ,p_query_input.attribute9
7025           ,l_miss_char
7026           ,p_query_input.attribute9
7027           ,p_query_input.attribute9
7028           ,p_query_input.attribute10
7029           ,l_miss_char
7030           ,p_query_input.attribute10
7031           ,p_query_input.attribute10
7032           ,p_query_input.attribute11
7033           ,l_miss_char
7034           ,p_query_input.attribute11
7035           ,p_query_input.attribute11
7036           ,p_query_input.attribute12
7037           ,l_miss_char
7038           ,p_query_input.attribute12
7039           ,p_query_input.attribute12
7040           ,p_query_input.attribute13
7041           ,l_miss_char
7042           ,p_query_input.attribute13
7043           ,p_query_input.attribute13
7044           ,p_query_input.attribute14
7045           ,l_miss_char
7046           ,p_query_input.attribute14
7047           ,p_query_input.attribute14
7048           ,p_query_input.attribute15
7049           ,l_miss_char
7050           ,p_query_input.attribute15
7051 	     ,p_query_input.attribute15
7052  /**** {{ R12 Enhanced reservations code changes }}****/
7053 	   , p_query_input.crossdock_flag
7054 	   , l_miss_char
7055 	   , p_query_input.crossdock_flag
7056 	   , p_query_input.crossdock_flag
7057 	   , p_query_input.crossdock_criteria_id
7058 	   , l_miss_num
7059 	   , p_query_input.crossdock_criteria_id
7060 	   , p_query_input.crossdock_criteria_id
7061 	   , p_query_input.demand_source_line_detail
7062 	   , l_miss_num
7063 	   , p_query_input.demand_source_line_detail
7064 	   , p_query_input.demand_source_line_detail
7065 	   , p_query_input.supply_receipt_date
7066 	   , l_miss_date
7067 	   , p_query_input.supply_receipt_date
7068 	   , p_query_input.supply_receipt_date
7069 	   , p_query_input.demand_ship_date
7070 	   , l_miss_date
7071 	   , p_query_input.demand_ship_date
7072 	   , p_query_input.demand_ship_date
7073 	   , p_query_input.project_id
7074 	   , l_miss_num
7075 	   , p_query_input.project_id
7076 	   , p_query_input.project_id
7077 	   , p_query_input.task_id
7078 	   , l_miss_num
7079 	   , p_query_input.task_id
7080 	   , p_query_input.task_id
7081 
7082 	   /***** End R12 ***/
7083 	     ;
7084 
7085          END IF; -- End If for supply_source_line_id
7086          -- End changes for Bug 4881317
7087 
7088      ELSE
7089        IF (l_debug = 1) then
7090 	  debug_print(' Inside cursor ref no values passed');
7091        END IF;
7092       -- INVCONV - Incorporate secondaries
7093       OPEN l_cursor_ref FOR    'SELECT
7094           reservation_id
7095         , requirement_date
7096         , organization_id
7097         , inventory_item_id
7098         , demand_source_type_id
7099         , demand_source_name
7100         , demand_source_header_id
7101         , demand_source_line_id
7102         , demand_source_delivery
7103         , primary_uom_code
7104         , primary_uom_id
7105         , secondary_uom_code
7106         , secondary_uom_id
7107         , reservation_uom_code
7108         , reservation_uom_id
7109         , reservation_quantity
7110         , primary_reservation_quantity
7111         , secondary_reservation_quantity
7112         , detailed_quantity
7113         , secondary_detailed_quantity
7114         , autodetail_group_id
7115         , external_source_code
7116         , external_source_line_id
7117         , supply_source_type_id
7118         , supply_source_header_id
7119         , supply_source_line_id
7120         , supply_source_name
7121         , supply_source_line_detail
7122         , revision
7123         , subinventory_code
7124         , subinventory_id
7125         , locator_id
7126         , lot_number
7127         , lot_number_id
7128         , pick_slip_number
7129         , lpn_id
7130         , attribute_category
7131         , attribute1
7132         , attribute2
7133         , attribute3
7134         , attribute4
7135         , attribute5
7136         , attribute6
7137         , attribute7
7138         , attribute8
7139         , attribute9
7140         , attribute10
7141         , attribute11
7142         , attribute12
7143         , attribute13
7144         , attribute14
7145         , attribute15
7146         , ship_ready_flag
7147         , staged_flag
7148 	/**** {{ R12 Enhanced reservations code changes }}****/
7149 	 , crossdock_flag
7150 	 , crossdock_criteria_id
7151 	 , demand_source_line_detail
7152 	 , serial_reservation_quantity
7153 	 , supply_receipt_date
7154 	 , demand_ship_date
7155 	 , project_id
7156 	 , task_id
7157 	 , orig_supply_source_type_id
7158 	 , orig_supply_source_header_id
7159 	 , orig_supply_source_line_id
7160 	 , orig_supply_source_line_detail
7161 	 , orig_demand_source_type_id
7162 	 , orig_demand_source_header_id
7163 	 , orig_demand_source_line_id
7164 	 , orig_demand_source_line_detail
7165          , serial_number
7166 	 /***** End R12 ***/
7167         FROM mtl_reservations
7168         WHERE
7169    (:requirement_date = :l_miss_date
7170          OR :requirement_date IS NULL
7171          AND requirement_date IS NULL
7172          OR :requirement_date
7173          = requirement_date
7174          )
7175         AND
7176         (:organization_id = :l_miss_num
7177          OR :organization_id IS NULL
7178          AND organization_id IS NULL
7179          OR :organization_id = organization_id
7180          )
7181         AND
7182         (:inventory_item_id = :l_miss_num
7183          OR :inventory_item_id IS NULL
7184          AND inventory_item_id IS NULL
7185          OR :inventory_item_id = inventory_item_id
7186          )
7187   AND
7188   (:demand_source_type_id = :l_miss_num
7189          OR :demand_source_type_id IS NULL
7190          AND demand_source_type_id IS NULL
7191          OR :demand_source_type_id
7192          = demand_source_type_id
7193          )
7194         AND
7195         (:demand_source_header_id = :l_miss_num
7196          OR :demand_source_header_id IS NULL
7197          AND demand_source_header_id IS NULL
7198          OR :demand_source_header_id
7199          = demand_source_header_id
7200          )
7201         AND
7202         (:demand_source_line_id = :l_miss_num
7203          OR :demand_source_line_id IS NULL
7204          AND demand_source_line_id IS NULL
7205          OR :demand_source_line_id = demand_source_line_id
7206          )
7207         AND
7208         (:demand_source_name = :l_miss_char
7209          OR :demand_source_name IS NULL
7210          AND demand_source_name IS NULL
7211          OR :demand_source_name = demand_source_name
7212          )
7213         AND
7214         (:demand_source_delivery = :l_miss_num
7215          OR :demand_source_delivery IS NULL
7216          AND demand_source_delivery IS NULL
7217          OR :demand_source_delivery = demand_source_delivery
7218          )
7219       AND
7220         (:primary_uom_code = :l_miss_char
7221          OR :primary_uom_code IS NULL
7222          AND primary_uom_code IS NULL
7223          OR :primary_uom_code = primary_uom_code
7224          )
7225         AND
7226         (:primary_uom_id = :l_miss_num
7227          OR :primary_uom_id IS NULL
7228          AND primary_uom_id IS NULL
7229          OR :primary_uom_id = primary_uom_id
7230          )
7231          -- INVCONV BEGIN
7232         AND
7233         (:secondary_uom_code = :l_miss_char
7234          OR :secondary_uom_code IS NULL
7235          AND secondary_uom_code IS NULL
7236          OR :secondary_uom_code = secondary_uom_code
7237          )
7238         AND
7239         (:secondary_uom_id = :l_miss_num
7240          OR :secondary_uom_id IS NULL
7241          AND secondary_uom_id IS NULL
7242          OR :secondary_uom_id = secondary_uom_id
7243          )
7244         -- INVCONV END
7245         AND
7246         (:reservation_uom_code = :l_miss_char
7247          OR :reservation_uom_code IS NULL
7248          AND reservation_uom_code IS NULL
7249          OR :reservation_uom_code = reservation_uom_code
7250          )
7251         AND
7252         (:reservation_uom_id = :l_miss_num
7253          OR :reservation_uom_id IS NULL
7254          AND reservation_uom_id IS NULL
7255          OR :reservation_uom_id = reservation_uom_id
7256          )
7257         AND
7258         (:autodetail_group_id = :l_miss_num
7259          OR :autodetail_group_id IS NULL
7260          AND autodetail_group_id IS NULL
7261          OR :autodetail_group_id = autodetail_group_id
7262          )
7263         AND
7264         (:external_source_code = :l_miss_char
7265          OR :external_source_code IS NULL
7266          AND external_source_code IS NULL
7267          OR :external_source_code = external_source_code
7268          )
7269         AND
7270         (:external_source_line_id = :l_miss_num
7271          OR :external_source_line_id IS NULL
7272          AND external_source_line_id IS NULL
7273          OR :external_source_line_id = external_source_line_id
7274          )
7275         AND
7276         (:supply_source_type_id = :l_miss_num
7277          OR :supply_source_type_id IS NULL
7278          AND supply_source_type_id IS NULL
7279          OR :supply_source_type_id = supply_source_type_id
7280          )
7281         AND
7282         (:supply_source_header_id = :l_miss_num
7283          OR :supply_source_header_id IS NULL
7284          AND supply_source_header_id IS NULL
7285          OR :supply_source_header_id
7286          = supply_source_header_id
7287          )
7288         AND
7289         (:supply_source_line_id = :l_miss_num
7290          OR :supply_source_line_id IS NULL
7291          AND supply_source_line_id IS NULL
7292          OR :supply_source_line_id = supply_source_line_id
7293          )
7294         AND
7295         (:supply_source_name = :l_miss_char
7296          OR :supply_source_name IS NULL
7297          AND supply_source_name IS NULL
7298          OR :supply_source_name = supply_source_name
7299          )
7300         AND
7301         (:supply_source_line_detail = :l_miss_num
7302          OR :supply_source_line_detail IS NULL
7303          AND supply_source_line_detail IS NULL
7304          OR :supply_source_line_detail
7305          = supply_source_line_detail
7306          )
7307         AND
7308         (:revision = :l_miss_char
7309          OR :revision IS NULL
7310          AND revision IS NULL
7311          OR :revision = revision
7312          )
7313         AND
7314         (:subinventory_code = :l_miss_char
7315          OR :subinventory_code IS NULL
7316          AND subinventory_code IS NULL
7317          OR :subinventory_code = subinventory_code
7318          )
7319        AND
7320         (:subinventory_id = :l_miss_num
7321          OR :subinventory_id IS NULL
7322          AND subinventory_id IS NULL
7323          OR :subinventory_id = subinventory_id
7324          )
7325         AND
7326         (:locator_id = :l_miss_num
7327          OR :locator_id IS NULL
7328          AND locator_id IS NULL
7329          OR :locator_id = locator_id
7330          )
7331         AND
7332         (:lot_number = :l_miss_char
7333          OR :lot_number IS NULL
7334          AND lot_number IS NULL
7335          OR :lot_number = lot_number
7336          )
7337         AND
7338         (:lot_number_id = :l_miss_num
7339          OR :lot_number_id IS NULL
7340          AND lot_number_id IS NULL
7341          OR :lot_number_id = lot_number_id
7342          )
7343         AND
7344         (:lpn_id = :l_miss_num
7345          OR :lpn_id IS NULL
7346          AND lpn_id IS NULL
7347          OR :lpn_id = lpn_id
7348          )
7349         AND
7350         (:ship_ready_flag = :l_miss_num
7351          OR (:ship_ready_flag IS NULL OR :ship_ready_flag = 2)
7352          AND (ship_ready_flag IS NULL OR ship_ready_flag = 2)
7353          OR :ship_ready_flag = ship_ready_flag
7354          )
7355         AND
7356         (:staged_flag = :l_miss_char
7357          OR (:staged_flag IS NULL OR :staged_flag = ''N'')
7358          AND (staged_flag IS NULL OR staged_flag = ''N'')
7359          OR :staged_flag = staged_flag
7360          )
7361         AND
7362         (:attribute_category = :l_miss_char
7363          OR :attribute_category IS NULL
7364          AND attribute_category IS NULL
7365          OR :attribute_category = attribute_category
7366          )
7367         AND
7368         (:attribute1 = :l_miss_char
7369          OR :attribute1 IS NULL
7370          AND attribute1 IS NULL
7371          OR :attribute1 = attribute1
7372          )
7373        AND
7374         (:attribute2 = :l_miss_char
7375          OR :attribute2 IS NULL
7376          AND attribute2 IS NULL
7377          OR :attribute2 = attribute2
7378          )
7379         AND
7380         (:attribute3 = :l_miss_char
7381          OR :attribute3 IS NULL
7382          AND attribute3 IS NULL
7383          OR :attribute3 = attribute3
7384          )
7385         AND
7386         (:attribute4 = :l_miss_char
7387          OR :attribute4 IS NULL
7388          AND attribute4 IS NULL
7389          OR :attribute4 = attribute4
7390          )
7391        AND
7392         (:attribute5 = :l_miss_char
7393          OR :attribute5 IS NULL
7394          AND attribute5 IS NULL
7395          OR :attribute5 = attribute5
7396          )
7397         AND
7398         (:attribute6 = :l_miss_char
7399          OR :attribute6 IS NULL
7400          AND attribute6 IS NULL
7401          OR :attribute6 = attribute6
7402          )
7403         AND
7404         (:attribute7 = :l_miss_char
7405          OR :attribute7 IS NULL
7406          AND attribute7 IS NULL
7407          OR :attribute7 = attribute7
7408          )
7409         AND
7410         (:attribute8 = :l_miss_char
7411          OR :attribute8 IS NULL
7412          AND attribute8 IS NULL
7413          OR :attribute8 = attribute8
7414          )
7415         AND
7416         (:attribute9 = :l_miss_char
7417          OR :attribute9 IS NULL
7418          AND attribute9 IS NULL
7419          OR :attribute9 = attribute9
7420          )
7421         AND
7422         (:attribute10 = :l_miss_char
7423          OR :attribute10 IS NULL
7424          AND attribute10 IS NULL
7425          OR :attribute10 = attribute10
7426          )
7427         AND
7428         (:attribute11 = :l_miss_char
7429          OR :attribute11 IS NULL
7430          AND attribute11 IS NULL
7431          OR :attribute11 = attribute11
7432          )
7433         AND
7434         (:attribute12 = :l_miss_char
7435          OR :attribute12 IS NULL
7436          AND attribute12 IS NULL
7437          OR :attribute12 = attribute12
7438          )
7439         AND
7440         (:attribute13 = :l_miss_char
7441          OR :attribute13 IS NULL
7442          AND attribute13 IS NULL
7443          OR :attribute13 = attribute13
7444          )
7445         AND
7446         (:attribute14 = :l_miss_char
7447          OR :attribute14 IS NULL
7448          AND attribute14 IS NULL
7449          OR :attribute14 = attribute14
7450          )
7451         AND
7452         (:attribute15 = :l_miss_char
7453          OR :attribute15 IS NULL
7454          AND attribute15 IS NULL
7455          OR :attribute15 = attribute15
7456          )
7457 
7458 /**** {{ R12 Enhanced reservations code changes }}****/
7459 	AND
7460 	   (:crossdock_flag = :l_miss_char
7461          OR :crossdock_flag IS NULL
7462          AND crossdock_flag IS NULL
7463          OR :crossdock_flag = crossdock_flag
7464          )
7465 	AND
7466 	   (:crossdock_criteria_id = :l_miss_num
7467          OR :crossdock_criteria_id IS NULL
7468          AND crossdock_criteria_id IS NULL
7469          OR :crossdock_criteria_id = crossdock_criteria_id
7470 	    )
7471         AND
7472 	   (:demand_source_line_detail = :l_miss_num
7473          OR :demand_source_line_detail IS NULL
7474          AND demand_source_line_detail IS NULL
7475          OR :demand_source_line_detail = demand_source_line_detail
7476 	    )
7477 	AND
7478 	   (:supply_receipt_date = :l_miss_date
7479          OR :supply_receipt_date IS NULL
7480          AND supply_receipt_date IS NULL
7481          OR :supply_receipt_date = supply_receipt_date
7482 	    )
7483 	AND
7484 	   (:demand_ship_date = :l_miss_date
7485          OR :demand_ship_date IS NULL
7486          AND demand_ship_date IS NULL
7487          OR :demand_ship_date = demand_ship_date
7488 	    )
7489 	AND
7490 	   (:project_id = :l_miss_num
7491          OR :project_id IS NULL
7492          AND project_id IS NULL
7493          OR :project_id = project_id
7494 	    )
7495 	 AND
7496 	   (:task_id = :l_miss_num
7497          OR :task_id IS NULL
7498          AND task_id IS NULL
7499          OR :task_id = task_id
7500 	    )
7501 /***** End R12 ***/
7502 
7503   '
7504                             || l_lock_stmt
7505                             || l_sort_stmt
7506         USING  p_query_input.requirement_date
7507              , l_miss_date
7508              , p_query_input.requirement_date
7509              , p_query_input.requirement_date
7510              , p_query_input.organization_id
7511              , l_miss_num
7512              , p_query_input.organization_id
7513              , p_query_input.organization_id
7514              , p_query_input.inventory_item_id
7515              , l_miss_num
7516              , p_query_input.inventory_item_id
7517              , p_query_input.inventory_item_id
7518              , p_query_input.demand_source_type_id
7519              , l_miss_num
7520              , p_query_input.demand_source_type_id
7521              , p_query_input.demand_source_type_id
7522              , p_query_input.demand_source_header_id
7523              , l_miss_num
7524              , p_query_input.demand_source_header_id
7525              , p_query_input.demand_source_header_id
7526              , p_query_input.demand_source_line_id
7527              , l_miss_num
7528              , p_query_input.demand_source_line_id
7529              , p_query_input.demand_source_line_id
7530              , p_query_input.demand_source_name
7531              , l_miss_char
7532              , p_query_input.demand_source_name
7533              , p_query_input.demand_source_name
7534              , p_query_input.demand_source_delivery
7535              , l_miss_num
7536              , p_query_input.demand_source_delivery
7537              , p_query_input.demand_source_delivery
7538              , p_query_input.primary_uom_code
7539              , l_miss_char
7540              , p_query_input.primary_uom_code
7541              , p_query_input.primary_uom_code
7542              , p_query_input.primary_uom_id
7543              , l_miss_num
7544              , p_query_input.primary_uom_id
7545              , p_query_input.primary_uom_id
7546              -- INVCONV BEGIN
7547              , p_query_input.secondary_uom_code
7548              , l_miss_char
7549              , p_query_input.secondary_uom_code
7550              , p_query_input.secondary_uom_code
7551              , p_query_input.secondary_uom_id
7552              , l_miss_num
7553              , p_query_input.secondary_uom_id
7554              , p_query_input.secondary_uom_id
7555              -- INVCONV END
7556              , p_query_input.reservation_uom_code
7557              , l_miss_char
7558              , p_query_input.reservation_uom_code
7559              , p_query_input.reservation_uom_code
7560              , p_query_input.reservation_uom_id
7561              , l_miss_num
7562              , p_query_input.reservation_uom_id
7563              , p_query_input.reservation_uom_id
7564              , p_query_input.autodetail_group_id
7565              , l_miss_num
7566              , p_query_input.autodetail_group_id
7567              , p_query_input.autodetail_group_id
7568              , p_query_input.external_source_code
7569              , l_miss_char
7570              , p_query_input.external_source_code
7571              , p_query_input.external_source_code
7572              , p_query_input.external_source_line_id
7573              , l_miss_num
7574              , p_query_input.external_source_line_id
7575              , p_query_input.external_source_line_id
7576              , p_query_input.supply_source_type_id
7577              , l_miss_num
7578              , p_query_input.supply_source_type_id
7579              , p_query_input.supply_source_type_id
7580              , p_query_input.supply_source_header_id
7581              , l_miss_num
7582              , p_query_input.supply_source_header_id
7583              , p_query_input.supply_source_header_id
7584              , p_query_input.supply_source_line_id
7585              , l_miss_num
7586              , p_query_input.supply_source_line_id
7587              , p_query_input.supply_source_line_id
7588              , p_query_input.supply_source_name
7589              , l_miss_char
7590              , p_query_input.supply_source_name
7591              , p_query_input.supply_source_name
7592              , p_query_input.supply_source_line_detail
7593              , l_miss_num
7594              , p_query_input.supply_source_line_detail
7595              , p_query_input.supply_source_line_detail
7596              , p_query_input.revision
7597              , l_miss_char
7598              , p_query_input.revision
7599              , p_query_input.revision
7600              , p_query_input.subinventory_code
7601              , l_miss_char
7602              , p_query_input.subinventory_code
7603              , p_query_input.subinventory_code
7604              , p_query_input.subinventory_id
7605              , l_miss_num
7606              , p_query_input.subinventory_id
7607              , p_query_input.subinventory_id
7608              , p_query_input.locator_id
7609              , l_miss_num
7610              , p_query_input.locator_id
7611              , p_query_input.locator_id
7612              , p_query_input.lot_number
7613              , l_miss_char
7614              , p_query_input.lot_number
7615              , p_query_input.lot_number
7616              , p_query_input.lot_number_id
7617              , l_miss_num
7618              , p_query_input.lot_number_id
7619              , p_query_input.lot_number_id
7620              , p_query_input.lpn_id
7621              , l_miss_num
7622              , p_query_input.lpn_id
7623              , p_query_input.lpn_id
7624              , p_query_input.ship_ready_flag
7625              , l_miss_num
7626              , p_query_input.ship_ready_flag
7627              , p_query_input.ship_ready_flag
7628              , p_query_input.ship_ready_flag
7629              , p_query_input.staged_flag
7630              , l_miss_char
7631              , p_query_input.staged_flag
7632              , p_query_input.staged_flag
7633              , p_query_input.staged_flag
7634              , p_query_input.attribute_category
7635              , l_miss_char
7636              , p_query_input.attribute_category
7637              , p_query_input.attribute_category
7638              , p_query_input.attribute1
7639              , l_miss_char
7640              , p_query_input.attribute1
7641              , p_query_input.attribute1
7642              , p_query_input.attribute2
7643              , l_miss_char
7644              , p_query_input.attribute2
7645              , p_query_input.attribute2
7646              , p_query_input.attribute3
7647              , l_miss_char
7648              , p_query_input.attribute3
7649              , p_query_input.attribute3
7650              , p_query_input.attribute4
7651              , l_miss_char
7652              , p_query_input.attribute4
7653              , p_query_input.attribute4
7654              , p_query_input.attribute5
7655              , l_miss_char
7656              , p_query_input.attribute5
7657              , p_query_input.attribute5
7658              , p_query_input.attribute6
7659              , l_miss_char
7660              , p_query_input.attribute6
7661              , p_query_input.attribute6
7662              , p_query_input.attribute7
7663              , l_miss_char
7664              , p_query_input.attribute7
7665              , p_query_input.attribute7
7666              , p_query_input.attribute8
7667              , l_miss_char
7668              , p_query_input.attribute8
7669              , p_query_input.attribute8
7670              , p_query_input.attribute9
7671              , l_miss_char
7672              , p_query_input.attribute9
7673              , p_query_input.attribute9
7674              , p_query_input.attribute10
7675              , l_miss_char
7676              , p_query_input.attribute10
7677              , p_query_input.attribute10
7678              , p_query_input.attribute11
7679              , l_miss_char
7680              , p_query_input.attribute11
7681              , p_query_input.attribute11
7682              , p_query_input.attribute12
7683              , l_miss_char
7684              , p_query_input.attribute12
7685              , p_query_input.attribute12
7686              , p_query_input.attribute13
7687              , l_miss_char
7688              , p_query_input.attribute13
7689              , p_query_input.attribute13
7690              , p_query_input.attribute14
7691              , l_miss_char
7692              , p_query_input.attribute14
7693              , p_query_input.attribute14
7694              , p_query_input.attribute15
7695              , l_miss_char
7696              , p_query_input.attribute15
7697 	   , p_query_input.attribute15
7698  /**** {{ R12 Enhanced reservations code changes }}****/
7699 	   , p_query_input.crossdock_flag
7700 	   , l_miss_char
7701 	   , p_query_input.crossdock_flag
7702 	   , p_query_input.crossdock_flag
7703 	   , p_query_input.crossdock_criteria_id
7704 	   , l_miss_num
7705 	   , p_query_input.crossdock_criteria_id
7706 	   , p_query_input.crossdock_criteria_id
7707 	   , p_query_input.demand_source_line_detail
7708 	   , l_miss_num
7709 	   , p_query_input.demand_source_line_detail
7710 	   , p_query_input.demand_source_line_detail
7711 	   , p_query_input.supply_receipt_date
7712 	   , l_miss_date
7713 	   , p_query_input.supply_receipt_date
7714 	   , p_query_input.supply_receipt_date
7715 	   , p_query_input.demand_ship_date
7716 	   , l_miss_date
7717 	   , p_query_input.demand_ship_date
7718 	   , p_query_input.demand_ship_date
7719 	   , p_query_input.project_id
7720 	   , l_miss_num
7721 	   , p_query_input.project_id
7722 	   , p_query_input.project_id
7723 	   , p_query_input.task_id
7724 	   , l_miss_num
7725 	   , p_query_input.task_id
7726 	   , p_query_input.task_id
7727 
7728 	   /***** End R12 ***/
7729 	   ;
7730     END IF;
7731 
7732     --
7733     l_counter                    := 0;
7734 
7735     LOOP
7736        IF l_res_cursor THEN
7737 	  IF l_update THEN
7738 	     --    IF (l_debug = 1) then
7739 	     --	debug_print(' Inside res cursor for update...');
7740 	     --    END IF;
7741 	     FETCH c_res_id_update INTO l_rsv_rec;
7742 	     EXIT WHEN c_res_id_update%NOTFOUND;
7743 	   ELSE
7744 		--	IF (l_debug = 1) then
7745 		--	   debug_print(' Inside res cursor no update...');
7746 		--	END IF;
7747 		FETCH c_res_id INTO l_rsv_rec;
7748 		EXIT WHEN c_res_id%NOTFOUND;
7749 	  END IF;
7750 	ELSIF l_demand_cursor THEN
7751 		IF l_update THEN
7752 		   --	   IF (l_debug = 1) then
7753 		   --	      debug_print (' Inside demand cursor for update...');
7754 		   --	   END IF;
7755 		   FETCH c_demand_update INTO l_rsv_rec;
7756 		   EXIT WHEN c_demand_update%NOTFOUND;
7757 		 ELSE
7758 		      --      IF (l_debug = 1) then
7759 		      -- debug_print (' Inside demand cursor no update...');
7760 		      -- END IF;
7761 		      FETCH c_demand INTO l_rsv_rec;
7762 		      EXIT WHEN c_demand%NOTFOUND;
7763 		END IF;
7764 	ELSE
7765 		      --IF (l_debug = 1) then
7766 		      --	 debug_print(' Inside ref cursor...');
7767 		      --    END IF;
7768 		      FETCH l_cursor_ref INTO l_rsv_rec;
7769 		      EXIT WHEN l_cursor_ref%NOTFOUND;
7770        END IF;
7771 
7772        l_counter                         := l_counter + 1;
7773        x_mtl_reservation_tbl(l_counter)  := l_rsv_rec;
7774     END LOOP;
7775 
7776     IF c_res_id%ISOPEN THEN
7777        CLOSE c_res_id;
7778      ELSIF c_res_id_update%ISOPEN THEN
7779        CLOSE c_res_id_update;
7780      ELSIF c_demand_update%ISOPEN THEN
7781        CLOSE c_demand_update;
7782      ELSIF c_demand%ISOPEN THEN
7783        CLOSE c_demand;
7784      ELSE
7785        CLOSE l_cursor_ref;
7786     END IF;
7787 
7788     IF (l_debug = 1) then
7789        debug_print(' Counter: ' || l_counter);
7790        debug_print(' return status: ' || l_return_status);
7791        debug_print('error code' || inv_reservation_global.g_err_no_error);
7792     END IF;
7793     --
7794     x_mtl_reservation_tbl_count  := l_counter;
7795     x_return_status              := l_return_status;
7796     x_error_code                 := inv_reservation_global.g_err_no_error;
7797   --
7798   EXCEPTION
7799     WHEN fnd_api.g_exc_error THEN
7800       x_return_status  := fnd_api.g_ret_sts_error;
7801       --  Get message count and data
7802       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7803     WHEN fnd_api.g_exc_unexpected_error THEN
7804       x_return_status  := fnd_api.g_ret_sts_unexp_error;
7805       --  Get message count and data
7806       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7807     WHEN OTHERS THEN
7808       IF SQLCODE = -54 THEN -- failed to lock
7809         x_return_status  := fnd_api.g_ret_sts_error;
7810         x_error_code     := inv_reservation_global.g_err_fail_to_lock_rec;
7811       ELSE
7812         x_return_status  := fnd_api.g_ret_sts_unexp_error;
7813 
7814         IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
7815           fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
7816         END IF;
7817 
7818         --  Get message count and data
7819         fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7820       END IF;
7821   END query_reservation;
7822 
7823  /**** {{ R12 Enhanced reservations code changes. Overloaded query
7824   -- reservation for querying serial numbers  }}****/
7825   -- Overloaded API for query_reservation
7826 
7827   PROCEDURE query_reservation
7828     (
7829      p_api_version_number        	IN     	NUMBER
7830      , p_init_msg_lst              	IN     	VARCHAR2 DEFAULT fnd_api.g_false
7831      , x_return_status             	OUT   	NOCOPY VARCHAR2
7832      , x_msg_count                 	OUT    	NOCOPY NUMBER
7833      , x_msg_data                  	OUT    	NOCOPY VARCHAR2
7834      , p_query_input               	IN     	inv_reservation_global.mtl_reservation_rec_type
7835      , p_lock_records              	IN     	VARCHAR2 DEFAULT fnd_api.g_false
7836      , p_sort_by_req_date          	IN     	NUMBER DEFAULT inv_reservation_global.g_query_no_sort
7837      , p_cancel_order_mode         	IN     	NUMBER DEFAULT inv_reservation_global.g_cancel_order_no
7838      , p_serial_number_table	        IN	inv_reservation_global.rsv_serial_number_table
7839      , x_mtl_reservation_tbl       	OUT    	NOCOPY inv_reservation_global.mtl_reservation_tbl_type
7840      , x_mtl_reservation_tbl_count 	OUT    	NOCOPY NUMBER
7841      , x_serial_number_table	        OUT	NOCOPY inv_reservation_global.rsv_serial_number_table
7842     , x_serial_number_table_count	OUT 	NOCOPY NUMBER
7843     , x_error_code                	OUT    	NOCOPY NUMBER
7844     ) IS
7845 
7846        l_api_name     CONSTANT VARCHAR2(30) := 'Query_Reservation';
7847        l_serial_number_table inv_reservation_global.rsv_serial_number_table;
7848        l_serial_table_index BINARY_INTEGER;
7849        l_output_index BINARY_INTEGER;
7850        l_reservation_index BINARY_INTEGER;
7851        l_reservation_id NUMBER;
7852        l_return_status VARCHAR2(1) := fnd_api.g_ret_sts_success;
7853        l_debug NUMBER;
7854        l_serial_number varchar2(30);
7855        l_mtl_reservation_tbl
7856 	 inv_reservation_global.mtl_reservation_tbl_type;
7857        l_progress NUMBER;
7858        l_mtl_reservation_tbl_count NUMBER;
7859        l_rsv_rec inv_reservation_global.mtl_reservation_rec_type;
7860        l_error_code NUMBER;
7861        l_serial_number_table_temp inv_reservation_global.rsv_serial_number_table; --Bug# 13479815
7862 
7863   BEGIN
7864 
7865      -- The new API will be called to query a set of serial numbers or if
7866      -- the reservation record has serial numbers and the query API has to
7867      -- return the serial numbers, then this API will be used.
7868      -- All the serial number retrieval logic will go here. This will
7869      -- inturn call the old API to get the reservation records and for each
7870      -- reservation record, we will have to query the serials and return
7871      -- the serials along with reservation IDs will be passed back as
7872      -- output.
7873 
7874      l_debug := g_debug;
7875 
7876       IF l_debug=1 THEN
7877 	  debug_print ('Inside overloaded query reservations');
7878       END IF;
7879       l_rsv_rec := p_query_input;
7880      -- First call the query reservations to get all the reservation
7881      -- records.
7882       inv_reservation_pvt.query_reservation
7883 	 (p_api_version_number             => 1.0,
7884 	  p_init_msg_lst                   => fnd_api.g_false,
7885 	  x_return_status                  => l_return_status,
7886 	  x_msg_count                      => x_msg_count,
7887 	  x_msg_data                       => x_msg_data,
7888 	  p_query_input                    => l_rsv_rec,
7889 	  p_lock_records                   => fnd_api.g_true,
7890 	  p_sort_by_req_date               => p_sort_by_req_date,
7891 	  x_mtl_reservation_tbl            => l_mtl_reservation_tbl,
7892 	  x_mtl_reservation_tbl_count      => l_mtl_reservation_tbl_count,
7893 	  x_error_code                     => l_error_code
7894 	  );
7895 
7896        IF l_debug=1 THEN
7897 	  debug_print ('Return Status after querying reservations '||l_return_status);
7898        END IF;
7899 
7900        l_progress := 80;
7901 
7902        IF l_return_status = fnd_api.g_ret_sts_error THEN
7903 
7904 	  IF l_debug=1 THEN
7905 	     debug_print('Raising expected error'||l_return_status);
7906 	  END IF;
7907 	  RAISE fnd_api.g_exc_error;
7908 
7909 	ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
7910 
7911 	  IF l_debug=1 THEN
7912 	     debug_print('Rasing Unexpected error'||l_return_status);
7913 	  END IF;
7914 	  RAISE fnd_api.g_exc_unexpected_error;
7915 
7916        END IF;
7917 
7918        IF (l_debug=1) THEN
7919 	    debug_print('x_mtl_reservation_tbl_count='|| l_mtl_reservation_tbl_count);
7920        END IF;
7921 
7922        -- check to see if the serial number table is empty or not
7923        IF p_serial_number_table.COUNT = 0 THEN
7924 	  -- then nothing was passed and get all the serial numbers for
7925 	  -- each reservation record returned from query
7926 
7927           l_output_index := 1 ;  --Bug# 13479815
7928           FOR i IN 1..l_mtl_reservation_tbl_count LOOP
7929             BEGIN
7930 		IF (l_debug=1) THEN
7931                    debug_print('Count. i: '|| i);
7932                 END IF;
7933                  SELECT reservation_id, serial_number bulk collect INTO
7934                  -- l_serial_number_table FROM mtl_serial_numbers
7935                  /* Bug# 13479815: replaced l_serial_number_table with l_serial_number_table_temp */
7936                  l_serial_number_table_temp  FROM mtl_serial_numbers
7937                  WHERE reservation_id = l_mtl_reservation_tbl(i).reservation_id;
7938 
7939               IF l_serial_number_table_temp.Count > 0 THEN  --Bug# 13557393
7940                 -- Bug# 13479815 Start : populating l_serial_number_table with all the serial number details
7941                   FOR j IN l_serial_number_table_temp.first..l_serial_number_table_temp.last
7942                   LOOP
7943                         l_serial_number_table(l_output_index) := l_serial_number_table_temp(j);
7944                         l_output_index := l_output_index + 1;
7945                   END LOOP;
7946                 -- Bug# 13479815: End
7947               END IF;
7948 
7949 	     EXCEPTION
7950 		WHEN no_data_found THEN
7951 		   IF l_debug=1 THEN
7952 		      debug_print('No serials found for reservation record. id: ' || l_mtl_reservation_tbl(i).reservation_id);
7953 		   END IF;
7954 	     END;
7955 
7956 	  END LOOP;
7957 
7958 	ELSE
7959 		   -- serial numbers are passed. loop all the serials and make sure
7960 		   -- that the serial number passed is infact valid and belong to the reservation records returned.
7961 		   IF (l_debug=1) THEN
7962 		      debug_print('Total number of serials passed: '|| p_serial_number_table.COUNT);
7963 		   END IF;
7964 
7965 		   FOR l_serial_table_index IN p_serial_number_table.first..p_serial_number_table.last
7966 		   LOOP
7967 		      l_serial_number :=
7968 			p_serial_number_table(l_serial_table_index).serial_number;
7969 
7970 		      BEGIN
7971 
7972 			 IF (l_debug=1) THEN
7973 			    debug_print('Count. Serial index: '|| l_serial_table_index);
7974 			 END IF;
7975 
7976 			 SELECT reservation_id INTO l_reservation_id FROM
7977 			   mtl_serial_numbers WHERE serial_number = l_serial_number AND
7978 			   current_organization_id = l_mtl_reservation_tbl(l_serial_table_index).organization_id AND
7979 			   inventory_item_id = l_mtl_reservation_tbl(l_serial_table_index).inventory_item_id;
7980 
7981 		      EXCEPTION
7982 			 WHEN no_data_found THEN
7983 			    IF l_debug=1 THEN
7984 			       debug_print('Serial passed cannot be found. Serial Number: ' || l_serial_number);
7985 			    END IF;
7986 		      END;
7987 
7988 		      l_output_index := 1;
7989 		      IF (l_reservation_id IS NOT NULL) THEN
7990 
7991 			 FOR l_reservation_index IN l_mtl_reservation_tbl.first..l_mtl_reservation_tbl.last
7992 			 LOOP
7993 			    IF (l_reservation_id =
7994 				l_mtl_reservation_tbl(l_reservation_index).reservation_id) THEN
7995 			       -- add the serial and the reservation id to
7996 			       -- the serial number table
7997 			       l_serial_number_table(l_output_index).reservation_id := l_reservation_id;
7998 			       l_serial_number_table(l_output_index).serial_number := l_serial_number;
7999 			       l_output_index := l_output_index + 1;
8000 			       EXIT;
8001 			    END IF;
8002 			 END LOOP;
8003 		      END IF;
8004 
8005 		   END LOOP;
8006 
8007        END IF;
8008 
8009        x_mtl_reservation_tbl        := l_mtl_reservation_tbl;
8010        x_mtl_reservation_tbl_count  := l_mtl_reservation_tbl_count;
8011        x_return_status              := l_return_status;
8012        x_error_code                 := inv_reservation_global.g_err_no_error;
8013        x_serial_number_table := l_serial_number_table;
8014        x_serial_number_table_count := l_serial_number_table.COUNT;
8015 
8016   EXCEPTION
8017      WHEN fnd_api.g_exc_error THEN
8018 	x_return_status  := fnd_api.g_ret_sts_error;
8019 	--  Get message count and data
8020 	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8021      WHEN fnd_api.g_exc_unexpected_error THEN
8022 	x_return_status  := fnd_api.g_ret_sts_unexp_error;
8023 	--  Get message count and data
8024 	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8025      WHEN OTHERS THEN
8026 	IF SQLCODE = -54 THEN -- failed to lock
8027 	   x_return_status  := fnd_api.g_ret_sts_error;
8028 	   x_error_code     := inv_reservation_global.g_err_fail_to_lock_rec;
8029 	 ELSE
8030 	   x_return_status  := fnd_api.g_ret_sts_unexp_error;
8031 
8032 	   IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
8033 	      fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
8034 	   END IF;
8035 
8036 	   --  Get message count and data
8037 	   fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8038 	END IF;
8039   END query_reservation;
8040 
8041   /*** End R12 ***/
8042   --
8043   PROCEDURE create_reservation
8044     (
8045      p_api_version_number       IN     NUMBER
8046      , p_init_msg_lst             IN     VARCHAR2 DEFAULT fnd_api.g_false
8047      , x_return_status            OUT    NOCOPY VARCHAR2
8048      , x_msg_count                OUT    NOCOPY NUMBER
8049      , x_msg_data                 OUT    NOCOPY VARCHAR2
8050      , p_rsv_rec                  IN     inv_reservation_global.mtl_reservation_rec_type
8051      , p_serial_number            IN     inv_reservation_global.serial_number_tbl_type
8052      , x_serial_number            OUT    NOCOPY inv_reservation_global.serial_number_tbl_type
8053      , p_partial_reservation_flag IN     VARCHAR2 DEFAULT fnd_api.g_false
8054      , p_force_reservation_flag   IN     VARCHAR2 DEFAULT fnd_api.g_false
8055      , p_validation_flag          IN     VARCHAR2 DEFAULT fnd_api.g_true
8056      , p_over_reservation_flag      IN  NUMBER DEFAULT 0
8057      , x_quantity_reserved        OUT    NOCOPY NUMBER
8058      , x_secondary_quantity_reserved        OUT NOCOPY NUMBER
8059      -- INVCONV
8060      , x_reservation_id           OUT    NOCOPY NUMBER
8061     /**** {{ R12 Enhanced reservations code changes }}****/
8062     , p_partial_rsv_exists        IN  BOOLEAN DEFAULT FALSE
8063     /*** End R12 ***/
8064     , p_substitute_flag           IN  BOOLEAN DEFAULT FALSE /* Bug 6044651 */
8065     ) IS
8066     l_api_version_number CONSTANT NUMBER                                          := 1.0;
8067     l_api_name           CONSTANT VARCHAR2(30)                                    := 'Create_Reservation';
8068     l_return_status               VARCHAR2(1)                                     := fnd_api.g_ret_sts_success;
8069     l_rsv_rec                     inv_reservation_global.mtl_reservation_rec_type;
8070     l_actual_primary_quantity     NUMBER := NULL;
8071     l_tmp_rsv_tbl                 inv_reservation_global.mtl_reservation_tbl_type;
8072     l_tmp_rsv_tbl_count           NUMBER;
8073     l_dummy_rsv_rec               inv_reservation_global.mtl_reservation_rec_type;
8074     l_dummy_serial_array          inv_reservation_global.serial_number_tbl_type;
8075     l_tree_id                     INTEGER;
8076     l_orig_item_cache_index       INTEGER                                         := NULL;
8077     l_orig_org_cache_index        INTEGER                                         := NULL;
8078     l_orig_demand_cache_index     INTEGER                                         := NULL;
8079     l_orig_supply_cache_index     INTEGER                                         := NULL;
8080     l_orig_sub_cache_index        INTEGER                                         := NULL;
8081     l_to_item_cache_index         INTEGER                                         := NULL;
8082     l_to_org_cache_index          INTEGER                                         := NULL;
8083     l_to_demand_cache_index       INTEGER                                         := NULL;
8084     l_to_supply_cache_index       INTEGER                                         := NULL;
8085     l_to_sub_cache_index          INTEGER                                         := NULL;
8086     l_reservation_id              NUMBER;
8087     l_date                        DATE;
8088     l_user_id                     NUMBER;
8089     l_request_id                  NUMBER;
8090     l_login_id                    NUMBER;
8091     l_prog_appl_id                NUMBER;
8092     l_program_id                  NUMBER;
8093     l_rowid                       VARCHAR2(30);
8094     l_error_code                  NUMBER;
8095     l_qty_changed                 NUMBER;
8096     l_secondary_qty_changed       NUMBER; 					-- INVCONV
8097     l_what_field                  VARCHAR2(240);
8098     l_debug number;
8099     l_lot_divisible_flag          VARCHAR2(1)                                    :='Y'; -- INVCONV
8100     l_dual_control_flag           VARCHAR2(1)
8101       :='N'; -- INVCONV
8102     /**** {{ R12 Enhanced reservations code changes }}****/
8103     l_rsv_updated                 BOOLEAN :=FALSE;
8104     l_progress                    NUMBER;
8105     --  l_from_rsv_rec                inv_reservation_global.mtl_reservation_rec_type
8106     --   := p_rsv_rec;
8107     l_to_rsv_rec                  inv_reservation_global.mtl_reservation_rec_type;
8108     l_mtl_reservation_tbl         inv_reservation_global.mtl_reservation_tbl_type;
8109     l_mtl_reservation_tbl_count   NUMBER;
8110     l_quantity_reserved           NUMBER;
8111     l_secondary_quantity_reserved NUMBER;
8112     l_qty_available               NUMBER;
8113     l_serial_index                NUMBER;
8114     l_serial_number inv_reservation_global.serial_number_tbl_type;
8115     l_item_rec  inv_reservation_global.item_record;
8116     l_supply_lock_handle varchar2(128);
8117     l_demand_lock_handle varchar2(128);
8118     l_lock_status NUMBER;
8119     l_group_mark_id NUMBER := NULL;
8120     l_lock_obtained BOOLEAN := FALSE;
8121     l_project_id NUMBER;
8122     l_task_id NUMBER;
8123     l_pjm_enabled NUMBER;
8124     l_reservation_margin_above NUMBER;
8125     l_supply_source_type_id NUMBER;
8126     l_exp_date DATE; --Expired lots custom hook
8127     l_reservation_qty_lot NUMBER := 0; --Bug 12978409
8128     /* Added for bug 13829182 */
8129     l_wip_entity_id NUMBER;
8130 	l_wip_entity_type NUMBER;
8131 	l_wip_job_type VARCHAR2(15);
8132 	l_maintenance_object_source NUMBER;
8133     /* End of changes for bug 13829182 */
8134     --MUOM Fulfillment Project
8135     l_qty_changed2  NUMBER;
8136     l_qty_available2 NUMBER;
8137     l_fulfill_base	VARCHAR2(1) := 'P';
8138 
8139     -- changing the cursor
8140     -- adding org_id in the condition
8141     -- bug 9874238
8142     CURSOR c_item(p_inventory_item_id NUMBER,p_organization_id NUMBER) IS
8143          SELECT *
8144            FROM mtl_system_items
8145           WHERE inventory_Item_Id = p_inventory_item_id
8146 	  AND organization_id = p_organization_id;
8147     /*** End R12 ***/
8148   BEGIN
8149      -- Use cache to get value for l_debug
8150      IF g_is_pickrelease_set IS NULL THEN
8151         g_is_pickrelease_set := 2;
8152         IF INV_CACHE.is_pickrelease THEN
8153            g_is_pickrelease_set := 1;
8154         END IF;
8155      END IF;
8156      IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
8157         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
8158      END IF;
8159 
8160      l_debug := g_debug;
8161 
8162      IF (l_debug = 1) THEN
8163         debug_print('Inside create reservation... ');
8164      END IF;
8165 
8166     --
8167     --  Standard call to check for call compatibility
8168     IF NOT fnd_api.compatible_api_call(l_api_version_number, p_api_version_number, l_api_name, g_pkg_name) THEN
8169       RAISE fnd_api.g_exc_unexpected_error;
8170     END IF;
8171 
8172     --
8173     --  Initialize message list.
8174     IF fnd_api.to_boolean(p_init_msg_lst) THEN
8175       fnd_msg_pub.initialize;
8176     END IF;
8177 
8178     /**** {{ R12 Enhanced reservations code changes.Initializing orig parameters }}****/
8179 
8180     SAVEPOINT create_reservation_sa;
8181 
8182     l_rsv_rec := p_rsv_rec;
8183 
8184     -- Set the original columns to g_miss_xxx as the user should not be
8185     -- setting these values. Do not query by them
8186     l_rsv_rec.orig_supply_source_type_id := fnd_api.g_miss_num;
8187     l_rsv_rec.orig_supply_source_header_id := fnd_api.g_miss_num;
8188     l_rsv_rec.orig_supply_source_line_id := fnd_api.g_miss_num;
8189     l_rsv_rec.orig_supply_source_line_detail := fnd_api.g_miss_num;
8190     l_rsv_rec.orig_demand_source_type_id := fnd_api.g_miss_num;
8191     l_rsv_rec.orig_demand_source_header_id := fnd_api.g_miss_num;
8192     l_rsv_rec.orig_demand_source_line_id := fnd_api.g_miss_num;
8193     l_rsv_rec.orig_demand_source_line_detail := fnd_api.g_miss_num;
8194 
8195 
8196     IF (l_rsv_rec.project_id IS NULL)  THEN
8197        l_rsv_rec.project_id := fnd_api.g_miss_num;
8198     END IF;
8199     IF (l_rsv_rec.task_id IS NULL)  THEN
8200        l_rsv_rec.task_id := fnd_api.g_miss_num;
8201     END IF;
8202 
8203     l_progress :=50;
8204 
8205     inv_reservation_pvt.convert_quantity
8206       (x_return_status      => l_return_status,
8207        px_rsv_rec           => l_rsv_rec
8208        );
8209 
8210     IF l_debug=1 THEN
8211        debug_print('Return Status from convert quantity'||l_return_status);
8212     END IF;
8213 
8214     IF l_return_status = fnd_api.g_ret_sts_error THEN
8215 
8216        IF l_debug=1 THEN
8217 	  debug_print('Raising expected error'||l_return_status);
8218        END IF;
8219        RAISE fnd_api.g_exc_error;
8220 
8221      ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8222 
8223        IF l_debug=1 THEN
8224 	  debug_print('Rasing Unexpected error'||l_return_status);
8225        END IF;
8226        RAISE fnd_api.g_exc_unexpected_error;
8227 
8228     END IF;
8229 
8230     l_progress :=60;
8231 
8232     IF l_debug=1 THEN
8233        debug_print('Calling query reservation to query existing reservations'||l_return_status);
8234     END IF;
8235 
8236     l_progress := 70;
8237 
8238     inv_reservation_pvt.query_reservation
8239       (p_api_version_number             => 1.0,
8240        p_init_msg_lst                   => fnd_api.g_false,
8241        x_return_status                  => l_return_status,
8242        x_msg_count                      => x_msg_count,
8243        x_msg_data                       => x_msg_data,
8244        p_query_input                    => l_rsv_rec,
8245        p_lock_records                   => fnd_api.g_true,
8246        x_mtl_reservation_tbl            => l_mtl_reservation_tbl,
8247        x_mtl_reservation_tbl_count      => l_mtl_reservation_tbl_count,
8248        x_error_code                     => l_error_code
8249        );
8250 
8251 
8252     IF l_debug=1 THEN
8253        debug_print ('Return Status after querying reservations '||l_return_status);
8254     END IF;
8255 
8256     l_progress := 80;
8257 
8258     IF l_return_status = fnd_api.g_ret_sts_error THEN
8259 
8260        IF l_debug=1 THEN
8261 	  debug_print('Raising expected error'||l_return_status);
8262        END IF;
8263        RAISE fnd_api.g_exc_error;
8264 
8265      ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8266 
8267        IF l_debug=1 THEN
8268 	  debug_print('Rasing Unexpected error'||l_return_status);
8269        END IF;
8270        RAISE fnd_api.g_exc_unexpected_error;
8271 
8272     END IF;
8273 
8274     IF (l_debug=1) THEN
8275 
8276        debug_print('x_mtl_reservation_tbl_count='|| l_mtl_reservation_tbl_count);
8277 
8278     END IF;
8279 
8280     IF (p_partial_rsv_exists OR l_mtl_reservation_tbl.COUNT > 0) THEN
8281        --Since create reservation is called by OM even when they updated the existing reservation
8282        --we need to query existing reservations and see if the existing reservation can be updated.
8283        IF (l_debug=1) THEN
8284 
8285 	  debug_print('Partial reservation flag passed as true or we found existing reservations');
8286 	  IF p_partial_rsv_exists THEN
8287 	     debug_print('The value of partial rsv exists is TRUE');
8288 	   ELSE
8289 	     debug_print('The value of partial rsv exists is FALSE');
8290 	  END IF;
8291 
8292        END IF;
8293 
8294        IF p_serial_number.COUNT > 0 THEN
8295 	  -- We donot support updating existing reservations if serial
8296 	  --  numbers are passed. the calling api should call update
8297 	  --  reservations and not create reservations
8298 	  IF l_debug=1 THEN
8299 	     debug_print('Serial numbers are passed with partial flag exists. error out');
8300 	  END IF;
8301 	  fnd_message.set_name('INV', 'INV_SER_PARTIAL_RSV_EXISTS');
8302 	  fnd_msg_pub.add;
8303 	  RAISE fnd_api.g_exc_error;
8304        END IF;
8305 
8306        IF l_debug=1 THEN
8307 	  inv_reservation_pvt.print_rsv_rec (p_rsv_rec);
8308        END IF;
8309 
8310        l_progress := 90;
8311 
8312        FOR i IN 1..l_mtl_reservation_tbl_count LOOP
8313 	  inv_reservation_pvt.print_rsv_rec (l_mtl_reservation_tbl(i));
8314 
8315 	  --If the queried reservation record is staged or has a lot number stamped or is
8316 	  -- revision controlled or has an LPN Id stamped or has a different SubInventory
8317 	  l_progress := 100;
8318 
8319 	  IF ((l_mtl_reservation_tbl(i).staged_flag='Y')
8320 	      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)
8321 	      OR (nvl(l_mtl_reservation_tbl(i).revision,'@@@')<>nvl(p_rsv_rec.revision,'@@@')AND p_rsv_rec.revision <>fnd_api.g_miss_char)
8322 	      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)
8323 	      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
8324 
8325 	     IF (l_debug=1) THEN
8326 		debug_print('Skipping reservation record');
8327 	     END IF;
8328 
8329 	     l_progress := 110;
8330 
8331 	     GOTO next_record;
8332 	   ELSE
8333 
8334 	     IF (l_debug=1) THEN
8335 
8336 		debug_print('Need to update reservation record');
8337 	     END IF;
8338 
8339 	     IF l_debug=1 THEN
8340 
8341 		debug_print('Reservation record that needs to be updated');
8342 		inv_reservation_pvt.print_rsv_rec (l_mtl_reservation_tbl(i));
8343 
8344 	     END IF;
8345 
8346 	     l_progress := 120;
8347 
8348 	     l_to_rsv_rec.primary_reservation_quantity := l_rsv_rec.primary_reservation_quantity
8349 	       + l_mtl_reservation_tbl(i).primary_reservation_quantity;
8350 	     -- INVCONV BEGIN
8351 	     -- Look at the reservation table row to determine if this is a dual control item.
8352 	     -- If it is dual control and a secondary_reservation_quantity has been supplied,
8353 	     -- then  calculate the to_resv_rec.secondary_reservation_quantity.
8354 	     -- Otherwise leave it empty to be computed as necessary by the private level API
8355 	     IF l_mtl_reservation_tbl(i).secondary_reservation_quantity is not NULL and
8356 	       l_to_rsv_rec.secondary_reservation_quantity is not NULL THEN
8357 		l_to_rsv_rec.secondary_reservation_quantity := l_rsv_rec.secondary_reservation_quantity
8358 		  + l_mtl_reservation_tbl(i).secondary_reservation_quantity;
8359 
8360 	     END IF;
8361 	     -- INVCONV END
8362 	     l_progress := 130;
8363 
8364 	     IF l_rsv_rec.reservation_uom_code = l_mtl_reservation_tbl(i).reservation_uom_code THEN
8365 
8366 		l_to_rsv_rec.reservation_quantity := l_rsv_rec.reservation_quantity + l_mtl_reservation_tbl(i).reservation_quantity;
8367 
8368 	      ELSE
8369 
8370 		l_to_rsv_rec.reservation_quantity := NULL;
8371 
8372 	     END IF;
8373 
8374 	     l_progress := 140;
8375 
8376 	     IF (l_debug=1) THEN
8377 
8378 		debug_print('Calling update reservations to update reservation record');
8379 
8380 	     END IF;
8381 
8382 	     inv_reservation_pvt.update_reservation
8383 	       (p_api_version_number          => 1.0,
8384 		p_init_msg_lst                => fnd_api.g_false,
8385 		x_return_status               => l_return_status,
8386 		x_msg_count                   => x_msg_count,
8387 		x_msg_data                    => x_msg_data,
8388 		x_quantity_reserved           => l_quantity_reserved,
8389 		x_secondary_quantity_reserved => l_secondary_quantity_reserved,  -- INVCONV
8390 		p_original_rsv_rec            => l_mtl_reservation_tbl(i),
8391 		p_to_rsv_rec                  => l_to_rsv_rec,
8392 		p_original_serial_number      => p_serial_number,
8393 		p_to_serial_number            => l_dummy_serial_array,
8394 		p_validation_flag             => p_validation_flag,              -- BUG 4705409
8395 		p_partial_reservation_flag    => p_partial_reservation_flag,
8396 		p_check_availability          => fnd_api.g_true,
8397 		p_over_reservation_flag       => p_over_reservation_flag
8398 		);
8399 
8400 	     l_progress := 150;
8401 
8402 	     IF (l_debug=1) THEN
8403 		debug_print ('Return Status after updating reservations '||l_return_status);
8404 	     END IF;
8405 
8406 	     IF l_return_status = fnd_api.g_ret_sts_error THEN
8407 
8408 		IF l_debug=1 THEN
8409 		   debug_print('Raising expected error'||l_return_status);
8410 		END IF;
8411 
8412 		RAISE fnd_api.g_exc_error;
8413 
8414 	      ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8415 
8416 		IF l_debug=1 THEN
8417 		   debug_print('Raising Unexpected error'||l_return_status);
8418 		END IF;
8419 
8420 		RAISE fnd_api.g_exc_unexpected_error;
8421 	     END IF;
8422 
8423 	     l_progress := 160;
8424 
8425 	     l_quantity_reserved:=l_quantity_reserved - l_mtl_reservation_tbl(i).primary_reservation_quantity;
8426 		 --bug 16434922
8427 		 l_secondary_quantity_reserved := l_secondary_quantity_reserved - l_mtl_reservation_tbl(i).secondary_reservation_quantity;
8428 
8429 	     x_quantity_reserved := l_quantity_reserved;
8430 		 x_secondary_quantity_reserved := l_secondary_quantity_reserved; -- Bug 16434922
8431 	     x_reservation_id    := l_mtl_reservation_tbl(i).reservation_id;
8432 
8433 	     IF l_debug=1 THEN
8434 		debug_print('After updating reservation successfully from
8435 			    inside create'|| l_return_status);
8436 		debug_print('Quantity reserved inside create'|| l_quantity_reserved);
8437 		debug_print('Reservation id inside create'|| l_mtl_reservation_tbl(i).reservation_id);
8438 	     END IF;
8439 
8440 	     l_rsv_updated := TRUE;
8441 	     x_return_status := l_return_status;
8442 	     EXIT;
8443 
8444 	  END IF;
8445 
8446 	  <<next_record>>
8447 	    NULL;
8448        END LOOP;
8449 
8450     END IF;
8451 
8452     -- End. Do not need this as we are moving this to the private package.
8453 
8454     IF ((NOT l_rsv_updated) OR (l_mtl_reservation_tbl.COUNT = 0)) THEN
8455 
8456       /*** End R12 ***/
8457 
8458 
8459       /**** {{ R12 Enhanced reservations code changes.Calling the reservation
8460       -- lock API to create a user-defined lock for non-inventory supplies }} *****/
8461 
8462       -- Bug 5199672: Should pass null to supply and demand line detail as
8463       -- we will have to lock the records at the document level and not at
8464       -- the line level. Also, for ASN, pass the source type as PO so that the
8465       -- the lock name would be the same as the PO's
8466 
8467        IF (l_rsv_rec.supply_source_type_id =
8468 	   inv_reservation_global.g_source_type_asn) THEN
8469 	  l_supply_source_type_id :=
8470 	    inv_reservation_global.g_source_type_po;
8471 	ELSE
8472 	  l_supply_source_type_id := l_rsv_rec.supply_source_type_id;
8473        END IF;
8474 
8475        IF (l_rsv_rec.supply_source_type_id <> inv_reservation_global.g_source_type_inv) THEN
8476 	  inv_reservation_lock_pvt.lock_supply_demand_record
8477 	    (p_organization_id => l_rsv_rec.organization_id
8478 	     ,p_inventory_item_id => l_rsv_rec.inventory_item_id
8479 	     ,p_source_type_id => l_supply_source_type_id
8480 	     ,p_source_header_id => l_rsv_rec.supply_source_header_id
8481 	     ,p_source_line_id =>  l_rsv_rec.supply_source_line_id
8482 	     ,p_source_line_detail => NULL
8483 	     ,x_lock_handle => l_supply_lock_handle
8484 	     ,x_lock_status => l_lock_status);
8485 
8486 	  IF l_lock_status = 0 THEN
8487 	     fnd_message.set_name('INV', 'INV_INVALID_LOCK');
8488 	     fnd_msg_pub.ADD;
8489 	     RAISE fnd_api.g_exc_error;
8490 	  END if;
8491 
8492 	  inv_reservation_lock_pvt.lock_supply_demand_record
8493 	    (p_organization_id => l_rsv_rec.organization_id
8494 	     ,p_inventory_item_id => l_rsv_rec.inventory_item_id
8495 	     ,p_source_type_id => l_rsv_rec.demand_source_type_id
8496 	     ,p_source_header_id => l_rsv_rec.demand_source_header_id
8497 	     ,p_source_line_id =>  l_rsv_rec.demand_source_line_id
8498 	     ,p_source_line_detail => NULL
8499 	     ,x_lock_handle => l_demand_lock_handle
8500 	     ,x_lock_status => l_lock_status);
8501 
8502 	  IF l_lock_status = 0 THEN
8503 	     fnd_message.set_name('INV', 'INV_INVALID_LOCK');
8504 	     fnd_msg_pub.ADD;
8505 	     RAISE fnd_api.g_exc_error;
8506 	  END if;
8507 
8508 	 l_lock_obtained := TRUE;
8509 
8510       END IF;
8511 
8512   /*** End R12 ***/
8513 
8514        -- SAVEPOINT create_reservation_sa;
8515        -- Bug #2819700
8516        -- Adding an extra check to make sure that create reservations does not
8517        -- pass a negative reservation quantity.
8518        IF (l_debug = 1) THEN
8519 	  debug_print('Primary_reservation_qty before inserting (create)= '
8520 		              ||To_char(l_rsv_rec.primary_reservation_quantity) );
8521 	  debug_print('Secondary_reservation_qty before inserting (create)= '
8522 		      ||To_char(l_rsv_rec.secondary_reservation_quantity) );   -- INVCONV
8523 	  debug_print('Reservation_qty before inserting (create)= '
8524 		      || To_char(l_rsv_rec.reservation_quantity) );
8525        END IF;
8526 
8527        IF ((NVL(l_rsv_rec.reservation_quantity,0) < 0) OR
8528 	   (NVL(l_rsv_rec.primary_reservation_quantity,0) < 0) ) THEN
8529 	  fnd_message.set_name('INV', 'INV-INVALID RESERVATION QTY');
8530 	  fnd_msg_pub.ADD;
8531 	  RAISE fnd_api.g_exc_error;
8532        END IF;
8533 
8534        /*---------------- Added by nimisra for bug6268983 ----------------
8535        This check is done in Order to Ensure that No custom code can Populate Demand_source_name for
8536        Sales orders Or Internal Orders
8537        --------------------------------------------------------------------*/
8538        IF (p_rsv_rec.demand_source_type_id=2 or p_rsv_rec.demand_source_type_id=8)
8539        and (p_rsv_rec.demand_source_name is NOT NULL) THEN
8540        fnd_message.set_name('INV', 'INV_INVALID_DEMAND_SOURCE');
8541        fnd_msg_pub.ADD; debug_print('For Sales Orders and Internal Orders DEMAND_SOURCE_NAME Should Be Null');
8542        RAISE fnd_api.g_exc_error;
8543        END IF;
8544 
8545        /* ------------------Added by nimisra for bug6268983--------------- */
8546 
8547        -- handle specially for detailed_quantity
8548        IF l_rsv_rec.detailed_quantity IS NULL
8549 	 OR l_rsv_rec.detailed_quantity = fnd_api.g_miss_num THEN
8550 	  l_rsv_rec.detailed_quantity  := 0;
8551        END IF;
8552 
8553        -- INVCONV - KYH Check this
8554        IF l_rsv_rec.secondary_detailed_quantity = fnd_api.g_miss_num THEN
8555 	  l_rsv_rec.secondary_detailed_quantity  := NULL;
8556        END IF;
8557 
8558 
8559        -- INVCONV BEGIN
8560        --
8561        -- validate input
8562        IF check_missing(l_rsv_rec, l_what_field) THEN
8563 	  -- input record attribute can not be missing
8564 	  -- for creation of reservation.
8565 	  -- must be some value or null.
8566 	  fnd_message.set_name('INV', 'INV-RSV-INPUT-MISSING');
8567 	  fnd_message.set_token('FIELD_NAME', l_what_field);
8568 	  fnd_msg_pub.ADD;
8569 	  RAISE fnd_api.g_exc_error;
8570        END IF;
8571 
8572 
8573        IF (l_debug = 1) THEN
8574 	  /**** {{ R12 Enhanced reservations code changes }}****/
8575 	  --  debug_print(' Before CALLING CONVERT record');
8576 	  /*** End R12 ***/
8577 	  debug_print(' reservation is' || l_reservation_id);
8578 	  debug_print(' reservation qty' || l_rsv_rec.reservation_quantity);
8579 	  debug_print(' reservation pri qty' || l_rsv_rec.primary_reservation_quantity);
8580 	  debug_print(' reservation second qty' || l_rsv_rec.secondary_reservation_quantity); -- INVCONV
8581        END IF;
8582 
8583 
8584        /**** {{ R12 Enhanced reservations code changes }}****/
8585 
8586        /**** Commenting out this code as we have already called query
8587        -- reservation in the beginning of this API
8588        --Bug#2729651. Calling the procedure convert_quantity before calling query_reservation. This is
8589        --because the form passes primary_uom as NULL. so this allows multiple reservations for the same
8590        --criteria. We compute the primary UOM if it is passed as NULL before checking for existing reservations.
8591        -- convert quantity between primary uom and reservation uom
8592        -- Bug 2737887. Commneting out the If condition, since
8593        -- we have to call the convert UOM API for all cases.
8594        --  IF (l_rsv_rec.primary_uom_code IS NULL) THEN
8595        convert_quantity(x_return_status => l_return_status, px_rsv_rec => l_rsv_rec);
8596 	 --  END IF;
8597 
8598 	 -- BUG 2737887. Inclding error handling for the above call to
8599 	 -- convert quantity.
8600 	 IF l_return_status = fnd_api.g_ret_sts_error THEN
8601 	 RAISE fnd_api.g_exc_error;
8602 	 END IF;
8603 
8604 	 --
8605 	 IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8606 	 RAISE fnd_api.g_exc_unexpected_error;
8607 	 END IF;
8608 
8609 	 End comment *******/
8610 	 /*** End R12 ***/
8611 
8612 	 IF (l_debug = 1) THEN
8613 	    /**** {{ R12 Enhanced reservations code changes }}****/
8614 	    --debug_print(' AFTER CALLING CONVERT record');
8615 	    /*** End R12 ***/
8616 	    debug_print(' reservation is' || l_reservation_id);
8617 	    debug_print(' reservation qty' || l_rsv_rec.reservation_quantity);
8618 	    debug_print(' reservation pri qty' || l_rsv_rec.primary_reservation_quantity);
8619 	    debug_print(' reservation second qty' || l_rsv_rec.secondary_reservation_quantity); -- INVCONV
8620 	 END IF;
8621 
8622 	 /**** {{ R12 Enhanced reservations code changes }}****/
8623 	 /**** Commenting out this code as we have already called query
8624 	 -- reservation in the beginning of this API
8625 	 -- query to see whether a record with the key
8626 	 -- attributes already exists
8627 	 -- if there is, return error
8628 	 -- user should use update instead.
8629 	 query_reservation
8630 	   (
8631 	   p_api_version_number         => 1.0
8632 	   , p_init_msg_lst               => fnd_api.g_false
8633 	   , x_return_status              => l_return_status
8634 	   , x_msg_count                  => x_msg_count
8635 	   , x_msg_data                   => x_msg_data
8636 	   , p_query_input                => l_rsv_rec
8637 	   , p_lock_records               => fnd_api.g_true
8638 	   , x_mtl_reservation_tbl        => l_tmp_rsv_tbl
8639 	   , x_mtl_reservation_tbl_count  => l_tmp_rsv_tbl_count
8640 	   , x_error_code                 => l_error_code
8641 	   );
8642 
8643 	   IF (l_debug = 1) THEN
8644 	   debug_print('After query_reservations ' || l_return_status);
8645 	   END IF;
8646 	   --
8647 	   IF l_return_status = fnd_api.g_ret_sts_error THEN
8648 	   RAISE fnd_api.g_exc_error;
8649 	   END IF;
8650 
8651 	   --
8652 	   IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8653 	   RAISE fnd_api.g_exc_unexpected_error;
8654 	   END IF;
8655 
8656 	   --
8657 	   IF l_tmp_rsv_tbl_count > 0 THEN
8658 	   fnd_message.set_name('INV', 'INV-RESERVATION-EXIST');
8659 	   fnd_msg_pub.ADD;
8660 	   RAISE fnd_api.g_exc_error;
8661 	   END IF;
8662 
8663 	 ***  End comment *******/
8664 	   -- Get the project and task for demands in OE, INT-ORD and RMA
8665 	   IF l_debug=1 THEN
8666 	      debug_print('Before Rsv rec project id: ' || l_rsv_rec.project_id);
8667 	      debug_print('Before Rsv rec task id: ' || l_rsv_rec.task_id);
8668 	   END IF;
8669          -- Bug : 5264987 : For pick release getting the l_pjm_enabled flag
8670          -- INV_CACHE
8671 	IF INV_CACHE.is_pickrelease THEN
8672 		-- Query for and cache the org record.
8673 		IF (NOT INV_CACHE.set_org_rec(l_rsv_rec.organization_id))
8674 		THEN
8675 			IF (l_debug = 1) THEN
8676 			debug_print('Error caching the org record');
8677 			END IF;
8678 		RAISE fnd_api.g_exc_unexpected_error;
8679 		END IF;
8680 		-- Set the PJM enabled flag.
8681 		l_pjm_enabled := INV_CACHE.org_rec.project_reference_enabled;
8682 	ELSE
8683 
8684 	   BEGIN
8685 	      SELECT project_reference_enabled
8686 		INTO l_pjm_enabled
8687 		FROM   mtl_parameters
8688 		WHERE  organization_id = l_rsv_rec.organization_id;
8689 	   EXCEPTION
8690 	      WHEN no_data_found THEN
8691 		 IF l_debug=1 THEN
8692 		    debug_print('Cannot find the project and task information');
8693 		 END IF;
8694 	   END;
8695 	 END IF;
8696 	 IF (l_rsv_rec.demand_source_type_id IN
8697 	   (inv_reservation_global.g_source_type_oe,
8698 	    inv_reservation_global.g_source_type_internal_ord,
8699 	    inv_reservation_global.g_source_type_rma)) AND
8700 	   (l_pjm_enabled = 1) THEN
8701 
8702 	    IF (l_rsv_rec.demand_source_line_id IS NOT NULL) AND
8703 	      (l_rsv_rec.demand_source_line_id <> fnd_api.g_miss_num)
8704 	      AND ((l_rsv_rec.project_id IS NULL) OR
8705 		   (l_rsv_rec.task_id IS NULL)) THEN
8706 		BEGIN
8707 		   SELECT project_id, task_id INTO l_project_id, l_task_id
8708 		     FROM oe_order_lines_all WHERE
8709 		     line_id = l_rsv_rec.demand_source_line_id;
8710 		     -- Bug 5264987
8711 		     -- Storing in global variables so that they are not queried again during transfer_reservation
8712 		     g_oe_line_id := l_rsv_rec.demand_source_line_id;
8713 		     g_project_id := l_project_id;
8714 		     g_task_id	  := l_task_id;
8715 		EXCEPTION
8716 		   WHEN no_data_found THEN
8717 		      IF l_debug=1 THEN
8718 			 debug_print('Cannot find the project and task information');
8719 		      END IF;
8720 		END;
8721 		IF ((l_rsv_rec.project_id IS NULL) AND
8722 		    (l_project_id IS NOT NULL)) THEN
8723 		   l_rsv_rec.project_id := l_project_id;
8724 		END IF;
8725 		IF ((l_rsv_rec.task_id IS NULL) AND
8726 		    (l_task_id IS NOT NULL)) THEN
8727 		   l_rsv_rec.task_id := l_task_id;
8728 		END IF;
8729 	    END IF;
8730 
8731        /* Added for bug 13829182 */
8732     ELSIF ( (l_rsv_rec.demand_source_type_id = inv_reservation_global.g_source_type_wip) AND (l_pjm_enabled = 1))  THEN
8733 
8734         BEGIN
8735             SELECT we.entity_type, wdj.maintenance_object_source
8736                  INTO l_wip_entity_id, l_maintenance_object_source
8737                FROM wip_entities we, wip_discrete_jobs wdj
8738             WHERE we.wip_entity_id = l_to_rsv_rec.demand_source_header_id
8739                  AND we.wip_entity_id = wdj.wip_entity_id(+);
8740         EXCEPTION
8741             WHEN no_data_found THEN
8742                 IF (l_debug = 1) THEN
8743                     debug_print('No WIP entity record found for the source header passed' );
8744                 END IF;
8745         END ;
8746 
8747         IF l_wip_entity_id = 6 and l_maintenance_object_source = 2  then
8748              l_wip_entity_type := inv_reservation_global.g_wip_source_type_cmro;
8749              l_wip_job_type := 'CMRO'; -- AHL
8750         ELSE
8751              l_wip_entity_type := null;
8752              l_wip_job_type := null; -- AHL
8753         END IF;
8754 
8755         IF (l_wip_job_type = 'CMRO' AND l_rsv_rec.demand_source_line_detail IS NOT NULL)
8756                 AND (l_rsv_rec.demand_source_line_detail <> fnd_api.g_miss_num)
8757                 AND ((l_rsv_rec.project_id IS NULL) OR (l_rsv_rec.task_id IS NULL)) THEN
8758             BEGIN
8759                 SELECT wdj.project_id, WDJ.TASK_ID
8760                     INTO l_project_id, l_task_id
8761                    FROM ahl_schedule_materials asmt, ahl_workorders aw, WIP_DISCRETE_JOBS WDJ
8762                 WHERE asmt.scheduled_material_id = l_rsv_rec.demand_source_line_detail
8763                      AND asmt.visit_task_id           = aw.visit_task_id
8764                      AND ASMT.VISIT_ID                = AW.VISIT_ID
8765                      AND aw.wip_entity_id             = wdj.wip_entity_id
8766                      AND AW.STATUS_CODE              IN ('1','3') -- 1:Unreleased,3:Released
8767                      AND ASMT.STATUS                  = 'ACTIVE';
8768 
8769                 g_sch_mat_id := l_rsv_rec.demand_source_line_detail;
8770                 g_project_id := l_project_id;
8771                 g_task_id	  := l_task_id;
8772 
8773             EXCEPTION
8774                 WHEN others THEN
8775                     IF l_debug=1 THEN
8776                         debug_print('Cannot find the project and task information from CMRO WO => '||sqlerrm);
8777                     END IF;
8778             END;
8779             IF ((l_rsv_rec.project_id IS NULL) AND (l_project_id IS NOT NULL)) THEN
8780                 l_rsv_rec.project_id := l_project_id;
8781             END IF;
8782             IF ((l_rsv_rec.task_id IS NULL) AND (l_task_id IS NOT NULL)) THEN
8783                 l_rsv_rec.task_id := l_task_id;
8784             END IF;
8785         END IF;                -- l_wip_job_type = 'CMRO'
8786         /* End of changes for bug 13829182 */
8787 
8788 	  ELSE -- not project enable
8789 	       l_to_rsv_rec.project_id := NULL;
8790 	       l_to_rsv_rec.task_id := NULL;
8791 	 END IF;
8792 	 IF l_debug=1 THEN
8793 	    debug_print('After Rsv rec project id: ' || l_rsv_rec.project_id);
8794 	    debug_print('After Rsv rec task id: ' || l_rsv_rec.task_id);
8795 	 END IF;
8796 	 /*** End R12 ***/
8797 
8798 	   IF (l_debug = 1) THEN
8799 	      debug_print('p_validation_flag' || p_validation_flag);
8800 	   END IF;
8801 	   --
8802 	   /**** {{ R12 Enhanced reservations code changes }}****/
8803 	   IF p_serial_number.COUNT > 0 THEN
8804 	      l_serial_number := p_serial_number;
8805 	   END IF;
8806 	   /*** End R12 ***/
8807 
8808 	   -- call validation api if the validate_flag is set to true
8809 	   -- Bug 2354735: Proceed with Validation if p_validation_flag = 'T' or 'V'
8810 	   IF p_validation_flag = fnd_api.g_true OR p_validation_flag = 'V' THEN
8811 	      inv_reservation_validate_pvt.validate_input_parameters
8812 		(
8813 		 x_return_status              => l_return_status
8814 		 , p_orig_rsv_rec               => l_rsv_rec
8815 		 , p_to_rsv_rec                 => l_dummy_rsv_rec
8816 		 , p_orig_serial_array          => l_serial_number
8817 		 , p_to_serial_array            => l_dummy_serial_array
8818 		 , p_rsv_action_name            => 'CREATE'
8819 		 , x_orig_item_cache_index      => l_orig_item_cache_index
8820 		 , x_orig_org_cache_index       => l_orig_org_cache_index
8821 		 , x_orig_demand_cache_index    => l_orig_demand_cache_index
8822 		 , x_orig_supply_cache_index    => l_orig_supply_cache_index
8823 		 , x_orig_sub_cache_index       => l_orig_sub_cache_index
8824 		 , x_to_item_cache_index        => l_to_item_cache_index
8825 		 , x_to_org_cache_index         => l_to_org_cache_index
8826 		 , x_to_demand_cache_index      => l_to_demand_cache_index
8827 		 , x_to_supply_cache_index      => l_to_supply_cache_index
8828 		 , x_to_sub_cache_index         => l_to_sub_cache_index
8829 		 , p_substitute_flag            => p_substitute_flag           /* Bug 6044651 */
8830 		 );
8831 
8832 	      IF (l_debug = 1) THEN
8833 		 debug_print('After validate_input_parameters ' || l_return_status);
8834 	      END IF;
8835 	      --
8836 	      IF l_return_status = fnd_api.g_ret_sts_error THEN
8837 		 RAISE fnd_api.g_exc_error;
8838 	      END IF;
8839 
8840 	      --
8841 	      IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8842 		 RAISE fnd_api.g_exc_unexpected_error;
8843 	      END IF;
8844 
8845 	      -- INVCONV BEGIN
8846 	      IF NOT is_lot_divisible(l_orig_item_cache_index) THEN
8847 		 l_lot_divisible_flag := 'N';
8848 	      END IF;
8849 	      -- INVCONV END
8850 	   END IF;
8851 
8852 	   --
8853 	   -- Pre Insert CTO Validation
8854 	   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
8855 	      --
8856 	      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);
8857 
8858 	      IF (l_debug = 1) THEN
8859 		 debug_print('After CTO validation ' || l_return_status);
8860 	      END IF;
8861 	      --
8862 	      IF l_return_status = fnd_api.g_ret_sts_error THEN
8863 		 RAISE fnd_api.g_exc_error;
8864 	      END IF;
8865 
8866 	      --
8867 	      IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8868 		 RAISE fnd_api.g_exc_unexpected_error;
8869 	      END IF;
8870 	      --
8871 	   END IF;
8872 
8873 
8874 	   IF l_return_status = fnd_api.g_ret_sts_error THEN
8875 	      RAISE fnd_api.g_exc_error;
8876 	   END IF;
8877 
8878 	   --
8879 	   IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8880 	      RAISE fnd_api.g_exc_unexpected_error;
8881 	   END IF;
8882 
8883       -- Expired lots custom hook
8884       IF inv_pick_release_pub.g_pick_expired_lots THEN
8885           l_exp_date := NULL;
8886       ELSE
8887           l_exp_date := SYSDATE;
8888       END IF;
8889 
8890 	   -- call modify tree procedure to change the tree if supply is inv
8891 	   -- will call quantity validation for other supply sources in the future
8892 	   -- Bug 2354735: Proceed with Trees only if p_validation_flag = 'T'
8893 	   IF  p_force_reservation_flag <> fnd_api.g_true
8894 	     AND p_validation_flag = fnd_api.g_true
8895 	     AND l_rsv_rec.supply_source_type_id = inv_reservation_global.g_source_type_inv THEN
8896 	      inv_quantity_tree_pvt.create_tree
8897 		(
8898 		 p_api_version_number         => 1.0
8899 		 , p_init_msg_lst               => fnd_api.g_true
8900 		 , x_return_status              => l_return_status
8901 		 , x_msg_count                  => x_msg_count
8902 		 , x_msg_data                   => x_msg_data
8903 		 , p_organization_id            => l_rsv_rec.organization_id
8904 		 , p_inventory_item_id          => l_rsv_rec.inventory_item_id
8905 		 , p_tree_mode                  => inv_quantity_tree_pvt.g_reservation_mode
8906 		 , p_is_revision_control        => is_revision_control(l_orig_item_cache_index)
8907 		 , p_is_lot_control             => is_lot_control(l_orig_item_cache_index)
8908 		 , p_is_serial_control          => is_serial_control(l_orig_item_cache_index)
8909 		 , p_asset_sub_only             => FALSE
8910 		 , p_include_suggestion         => TRUE
8911 		 , p_demand_source_type_id      => l_rsv_rec.demand_source_type_id
8912 		 , p_demand_source_header_id    => l_rsv_rec.demand_source_header_id
8913 		 , p_demand_source_line_id      => l_rsv_rec.demand_source_line_id
8914 		 , p_demand_source_name         => l_rsv_rec.demand_source_name
8915 		, p_demand_source_delivery     => l_rsv_rec.demand_source_delivery
8916 		, p_lot_expiration_date        => l_exp_date --SYSDATE -- Bug#2716563
8917 		, x_tree_id                    => l_tree_id
8918 		);
8919 
8920 	      IF (l_debug = 1) THEN
8921 		 debug_print('After create tree ' || l_return_status);
8922 	      END IF;
8923 
8924 	      IF l_return_status = fnd_api.g_ret_sts_error THEN
8925 		 RAISE fnd_api.g_exc_error;
8926 	      END IF;
8927 
8928 	      --
8929 	      IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8930 		 RAISE fnd_api.g_exc_unexpected_error;
8931 	      END IF;
8932 
8933 	      IF (l_debug = 1) THEN
8934 		 debug_print('calling modify_tree_crt_del_rel');
8935 		 debug_print('l_rsv_rec.primary_reservation_quantity: ' ||
8936 			     l_rsv_rec.primary_reservation_quantity);
8937 		 debug_print('l_rsv_rec.secondary_reservation_quantity: ' ||
8938 			     l_rsv_rec.secondary_reservation_quantity);  -- INVCONV
8939 
8940 	      END IF;
8941 	      -- INVCONV upgrade call for secondaries and lot divisibility
8942 	      modify_tree_crt_del_rel
8943 		(
8944 		 x_return_status              => l_return_status
8945 		 , p_tree_id                    => l_tree_id
8946 		 , p_revision                   => l_rsv_rec.revision
8947 		 , p_lot_number                 => l_rsv_rec.lot_number
8948 		 , p_subinventory_code          => l_rsv_rec.subinventory_code
8949 		 , p_locator_id                 => l_rsv_rec.locator_id
8950 		 , p_lpn_id                     => l_rsv_rec.lpn_id
8951 		 , p_primary_reservation_quantity=> l_rsv_rec.primary_reservation_quantity
8952 		 , p_second_reservation_quantity=> l_rsv_rec.secondary_reservation_quantity
8953 		 , p_detailed_quantity          => l_rsv_rec.detailed_quantity
8954 		 , p_secondary_detailed_quantity => l_rsv_rec.secondary_detailed_quantity
8955 		 , p_partial_reservation_flag   => p_partial_reservation_flag
8956 		 , p_force_reservation_flag     => p_force_reservation_flag
8957 		 , p_lot_divisible_flag         => l_lot_divisible_flag                            -- INVCONV
8958 		 , p_action                     => 'CREATE'
8959 		 , x_qty_changed                => l_qty_changed
8960 		 , x_secondary_qty_changed      => l_secondary_qty_changed				--INVCONV
8961 		 , p_organization_id            => l_rsv_rec.organization_id
8962                  , p_demand_source_line_id      => l_rsv_rec.demand_source_line_id
8963 		 , p_demand_source_type_id      => l_rsv_rec.demand_source_type_id
8964 		);
8965 	      IF (l_debug = 1) THEN
8966 		 debug_print('After modify tree crt del rel ' || l_return_status);
8967 		 debug_print('l_qty_changed: ' || l_qty_changed);
8968 		 debug_print('l_secondary_qty_changed: ' || l_secondary_qty_changed);            -- INVCONV
8969 	      END IF;
8970 
8971 	      --
8972 	      IF l_return_status = fnd_api.g_ret_sts_error THEN
8973 		 RAISE fnd_api.g_exc_error;
8974 	      END IF;
8975 
8976 	      --
8977 	      IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8978 		 RAISE fnd_api.g_exc_unexpected_error;
8979 	      END IF;
8980 
8981 	      /**** {{ R12 Enhanced reservations code changes }}****/
8982 	      --IF (l_debug = 1) THEN
8983 	      -- debug_print(' l_qty_changed' || l_qty_changed);
8984 	      --  debug_print('l_secondary_qty_changed: ' || l_secondary_qty_changed);    -- INVCONV
8985 	      -- END IF;
8986 	      /*** End R12 ***/
8987 
8988 	      IF  l_qty_changed > 0
8989 		AND l_qty_changed < l_rsv_rec.primary_reservation_quantity - NVL(l_rsv_rec.detailed_quantity, 0) THEN
8990 		 -- partial reservation. needs to recompute
8991 		 -- the actual quantity for reservation
8992 		 -- convert quantity between primary uom and reservation uom
8993 		 -- Bug 2116332 - Need to set reservation quantity to NULL
8994 		 --   before calling convert_quantity
8995 
8996 		 l_rsv_rec.primary_reservation_quantity  := l_qty_changed + NVL(l_rsv_rec.detailed_quantity, 0);
8997                  -- 5016196 BEGIN
8998                  -- For dual tracked items recompute the secondary
8999 		 IF l_rsv_rec.secondary_reservation_quantity IS NOT NULL THEN
9000 		   l_rsv_rec.secondary_reservation_quantity  := l_secondary_qty_changed + NVL(l_rsv_rec.secondary_detailed_quantity, 0);
9001                  END IF;
9002                  -- 5016196 END
9003 		 l_rsv_rec.reservation_quantity          := NULL;
9004 		 IF (l_debug = 1) THEN
9005 		    debug_print('l_rsv_rec.detailed_quantity: ' || l_rsv_rec.detailed_quantity);
9006 		    debug_print('l_rsv_rec.primary_reservation_quantity: ' || l_rsv_rec.primary_reservation_quantity);
9007 		    debug_print('l_rsv_rec.secondary_reservation_quantity: ' || l_rsv_rec.secondary_reservation_quantity);
9008 		 END IF;
9009 		 convert_quantity(x_return_status => l_return_status, px_rsv_rec => l_rsv_rec);
9010 
9011 		 IF (l_debug = 1) THEN
9012 		    debug_print('After convert qty ' || l_return_status);
9013 		 END IF;
9014 
9015 		 IF l_return_status = fnd_api.g_ret_sts_error THEN
9016 		    RAISE fnd_api.g_exc_error;
9017 		 END IF;
9018 
9019 		 --
9020 		 IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9021 		    RAISE fnd_api.g_exc_unexpected_error;
9022 		 END IF;
9023 	      END IF;
9024 
9025 		   --MUOM Fulfillment Project
9026 		inv_utilities.get_inv_fulfillment_base(
9027 			p_source_line_id		  => l_rsv_rec.demand_source_line_id,
9028 			p_demand_source_type_id => l_rsv_rec.demand_source_type_id,
9029 			p_org_id				  => l_rsv_rec.organization_id,
9030 			x_fulfillment_base		 => l_fulfill_base
9031 			);
9032 
9033    IF  (l_fulfill_base = 'S') THEN
9034        IF  (l_qty_changed > 0
9035 		   AND l_qty_changed > l_rsv_rec.primary_reservation_quantity - nvl(l_rsv_rec.detailed_quantity, 0)
9036        AND l_secondary_qty_changed =l_rsv_rec.secondary_reservation_quantity) THEN
9037 
9038        l_rsv_rec.primary_reservation_quantity  := l_qty_changed + NVL(l_rsv_rec.detailed_quantity, 0);
9039              IF (l_debug = 1) THEN
9040 		    debug_print('l_rsv_rec.detailed_quantity:  when fulfilment base is S' || l_rsv_rec.detailed_quantity);
9041 		    debug_print('l_rsv_rec.primary_reservation_quantity: when fulfilment base is S ' || l_rsv_rec.primary_reservation_quantity);
9042 		    debug_print('l_rsv_rec.secondary_reservation_quantity: when fulfilment base is S' || l_rsv_rec.secondary_reservation_quantity);
9043 	    END IF;
9044        END IF;
9045     END IF;
9046 
9047 	      /**** {{ R12 Enhanced reservations code changes }}****/
9048 	      --Setting the serial reservation quantity
9049 
9050               /*** move to before insert_row
9051 	      IF l_serial_number.COUNT > 0 THEN
9052 		 l_rsv_rec.serial_reservation_quantity := l_serial_number.COUNT;
9053 		 IF (l_rsv_rec.serial_reservation_quantity > l_rsv_rec.primary_reservation_quantity) THEN
9054 		    fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
9055 		    fnd_msg_pub.ADD;
9056 		    RAISE fnd_api.g_exc_error;
9057 		 END IF;
9058 
9059 	      END IF;
9060               ********************/
9061 	      /*** End R12 ***/
9062 
9063 		/**** {{ R12 Enhanced reservations code changes }}****/
9064 		ELSIF (l_rsv_rec.supply_source_type_id IN
9065 		       (inv_reservation_global.g_source_type_wip,
9066 			inv_reservation_global.g_source_type_po,
9067 			inv_reservation_global.g_source_type_asn,
9068 			inv_reservation_global.g_source_type_intransit,
9069 			inv_reservation_global.g_source_type_internal_req,
9070 			inv_reservation_global.g_source_type_rcv)) AND
9071 		  p_over_reservation_flag NOT IN (1,3) THEN
9072 	      -- call the available to reserve API to get the supply and
9073 	      -- demand availability
9074 	      -- Bug 5199672: Should pass g_miss_num as default for supply
9075 	      -- source line detail. Otherwise, high level reservations
9076 	      -- will not be considered.
9077 
9078 	      inv_reservation_avail_pvt.available_supply_to_reserve
9079 		(
9080 		 x_return_status                   => l_return_status
9081 		 , x_msg_count                     => x_msg_count
9082 		 , x_msg_data                      => x_msg_data
9083 		 , x_qty_available_to_reserve      => l_qty_changed
9084 		 , x_qty_available                 => l_qty_available
9085 		 , p_organization_id               => l_rsv_rec.organization_id
9086 		 , p_item_id	                   => l_rsv_rec.inventory_item_id
9087 		 , p_revision                      => l_rsv_rec.revision
9088 		 , p_lot_number                    => l_rsv_rec.lot_number
9089 		 , p_subinventory_code             => l_rsv_rec.subinventory_code
9090 		 , p_locator_id	                   => l_rsv_rec.locator_id
9091 		 , p_supply_source_type_id	    => l_rsv_rec.supply_source_type_id
9092 		 , p_supply_source_header_id	    => l_rsv_rec.supply_source_header_id
9093 		 , p_supply_source_line_id	    => l_rsv_rec.supply_source_line_id
9094 		 , p_supply_source_line_detail	    => Nvl(l_rsv_rec.supply_source_line_detail,fnd_api.g_miss_num)
9095 		 , p_lpn_id			    => l_rsv_rec.lpn_id
9096 		 , p_project_id		            => l_rsv_rec.project_id
9097 		, p_task_id			    => l_rsv_rec.task_id
9098 		, p_api_version_number     	    => 1.0
9099 		, p_init_msg_lst             	    => fnd_api.g_false
9100 		);
9101 
9102 	      IF (l_debug = 1) THEN
9103 		 debug_print('After calling available supply to reserve ' || l_return_status);
9104 		 debug_print('Available quantity to reserve. l_qty_changed: ' || l_qty_changed);
9105 		 debug_print('Available quantity on the document. l_qty_available: ' || l_qty_available);
9106 	      END IF;
9107 
9108 	      --
9109 	      IF l_return_status = fnd_api.g_ret_sts_error THEN
9110 		 RAISE fnd_api.g_exc_error;
9111 	      END IF;
9112 
9113 	      --
9114 	      IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9115 		 RAISE fnd_api.g_exc_unexpected_error;
9116 	      END IF;
9117 
9118          -- bug #5454715. Error out if the available supply to reserve is zero.
9119          IF l_qty_changed = 0 THEN
9120           fnd_message.set_name('INV', 'INV_INVALID_AVAILABLE_QTY');
9121           fnd_msg_pub.ADD;
9122           RAISE fnd_api.g_exc_error;
9123          END IF;
9124 
9125 
9126 	      -- Bug 5199672: Removed the condition l_qty_changed > 0
9127 	      IF ((l_rsv_rec.primary_reservation_quantity - l_qty_changed) > 0.000005) THEN
9128 
9129 		   IF (p_partial_reservation_flag = fnd_api.g_false) THEN
9130 		    IF (l_debug = 1) THEN
9131 		       debug_print('The supply document doesnt have enough quantity to be reserved against. error out. ');
9132 		    END IF;
9133 		    fnd_message.set_name('INV', 'INV_INVALID_AVAILABLE_QTY');
9134 		    fnd_msg_pub.ADD;
9135 		    RAISE fnd_api.g_exc_error;
9136 		  ELSE
9137 		    l_rsv_rec.primary_reservation_quantity  := l_qty_changed + NVL(l_rsv_rec.detailed_quantity, 0);
9138 		    l_rsv_rec.reservation_quantity          := NULL;
9139 		    IF (l_debug = 1) THEN
9140 		       debug_print('l_rsv_rec.detailed_quantity: ' || l_rsv_rec.detailed_quantity);
9141 		       debug_print('l_rsv_rec.primary_reservation_quantity: ' || l_rsv_rec.primary_reservation_quantity);
9142 		    END IF;
9143 		    convert_quantity(x_return_status => l_return_status, px_rsv_rec => l_rsv_rec);
9144 
9145 		    IF (l_debug = 1) THEN
9146 		       debug_print('After convert qty ' || l_return_status);
9147 		    END IF;
9148 
9149 		    IF l_return_status = fnd_api.g_ret_sts_error THEN
9150 		       RAISE fnd_api.g_exc_error;
9151 		    END IF;
9152 
9153 		    --
9154 		    IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9155 		       RAISE fnd_api.g_exc_unexpected_error;
9156 		    END IF;
9157 
9158 		 END IF;
9159 	      END IF;
9160 	   END IF;
9161 
9162 	   IF (l_rsv_rec.demand_source_type_id IN
9163 	       (inv_reservation_global.g_source_type_wip,
9164 		inv_reservation_global.g_source_type_oe,
9165 		inv_reservation_global.g_source_type_internal_ord,
9166 		inv_reservation_global.g_source_type_rma))  AND
9167 	     p_over_reservation_flag NOT IN (2,3) THEN
9168 
9169 	      -- Bug 5199672: Should pass g_miss_num as default for demand
9170 	      -- source line detail. Otherwise, high level reservations
9171 	      -- will not be considered.
9172 
9173 		  -- MUOM Fulfillment project
9174 	      inv_reservation_avail_pvt.available_demand_to_reserve
9175 		(
9176 		 x_return_status                   => l_return_status
9177 		 , x_msg_count                     => x_msg_count
9178 		 , x_msg_data                      => x_msg_data
9179 		 , x_qty_available_to_reserve      => l_qty_changed
9180 		 , x_qty_available                 => l_qty_available
9181 		 , x_qty_available_to_reserve2  => l_qty_changed2
9182                  , x_qty_available2                 => l_qty_available2
9183 		 , p_organization_id               => l_rsv_rec.organization_id
9184 		 , p_item_id                       => l_rsv_rec.inventory_item_id
9185 		 , p_primary_uom_code              => l_rsv_rec.primary_uom_code
9186 		 , p_demand_source_type_id	   => l_rsv_rec.demand_source_type_id
9187 		 , p_demand_source_header_id	   => l_rsv_rec.demand_source_header_id
9188 		 , p_demand_source_line_id	   => l_rsv_rec.demand_source_line_id
9189 		 , p_demand_source_line_detail     => Nvl(l_rsv_rec.demand_source_line_detail,fnd_api.g_miss_num)
9190 		 , p_project_id		           => l_rsv_rec.project_id
9191 		 , p_task_id			   => l_rsv_rec.task_id
9192 		 , p_api_version_number     	   => 1.0
9193 		 , p_init_msg_lst             	   => fnd_api.g_false
9194 		 );
9195 
9196 
9197 	      IF (l_debug = 1) THEN
9198 		 debug_print('After calling available demand to reserve ' || l_return_status);
9199 		 debug_print('Available quantity to reserve. l_qty_changed: ' || l_qty_changed);
9200 		 debug_print('Available quantity on the document. l_qty_available: ' || l_qty_available);
9201                  debug_print('Available quantity to reserve. l_qty_changed2: ' || l_qty_changed2);
9202 		 debug_print('Available quantity on the document. l_qty_available2: ' || l_qty_available2);
9203 
9204 	      END IF;
9205 
9206 	      --
9207 	      IF l_return_status = fnd_api.g_ret_sts_error THEN
9208 		 RAISE fnd_api.g_exc_error;
9209 	      END IF;
9210 
9211 	      --
9212 	      IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9213 		 RAISE fnd_api.g_exc_unexpected_error;
9214 	      END IF;
9215 
9216 
9217 	      IF (l_rsv_rec.demand_source_type_id in (inv_reservation_global.g_source_type_oe,
9218 						 inv_reservation_global.g_source_type_internal_ord,
9219 						 inv_reservation_global.g_source_type_rma)) THEN
9220 
9221 		 IF NOT
9222 		   (lot_divisible
9223 		    (p_inventory_item_id => l_rsv_rec.inventory_item_id,
9224 		     p_organization_id => l_rsv_rec.organization_id)) THEN
9225 		    get_ship_qty_tolerance
9226 		      (
9227 		       p_api_version_number          =>  1.0
9228 		       , p_init_msg_lst              =>  fnd_api.g_false
9229 		       , x_return_status             => x_return_status
9230 		       , x_msg_count                 => x_msg_count
9231 		       , x_msg_data                  => x_msg_data
9232 		       , p_demand_type_id            => l_rsv_rec.demand_source_type_id
9233 		       , p_demand_header_id          => l_rsv_rec.demand_source_header_id
9234 		       , p_demand_line_id            => l_rsv_rec.demand_source_line_id
9235 		       , x_reservation_margin_above  => l_reservation_margin_above);
9236 
9237 		    IF (l_debug = 1) THEN
9238 		       debug_print('Inside is lot indivisible');
9239 		    END IF;
9240 
9241 		    IF (l_debug = 1) THEN
9242 		       debug_print('After calling get_ship_qty_tolerance ' || x_return_status);
9243 		       debug_print('Reservation margin above ' || l_reservation_margin_above);
9244 		    END IF;
9245 
9246 		    --
9247 		    IF x_return_status = fnd_api.g_ret_sts_error THEN
9248 		       RAISE fnd_api.g_exc_error;
9249 		    END IF;
9250 
9251 		    --
9252 		    IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
9253 		       RAISE fnd_api.g_exc_unexpected_error;
9254 		    END IF;
9255 		           --MUOM Fulfillment Project
9256 
9257 	           If l_fulfill_base <> 'S' Then
9258 	              l_qty_changed := l_qty_changed + l_reservation_margin_above;
9259 	              l_qty_available := l_qty_available + l_reservation_margin_above;
9260 	           Else
9261 		       l_qty_changed2 := l_qty_changed2 + l_reservation_margin_above;
9262 		       l_qty_available2 := l_qty_available2 + l_reservation_margin_above;
9263 	           END IF;
9264 
9265 		 END IF;
9266 	      END IF;
9267 
9268 	      IF (l_debug = 1) THEN
9269 		 debug_print('available quantity to reserve. l_qty_changed: ' || l_qty_changed);
9270 		 debug_print('available quantity on the document. l_qty_available: ' || l_qty_available);
9271                  debug_print('available quantity on the document. l_qty_changed2: ' || l_qty_changed2);
9272 	      END IF;
9273 
9274           --Bug 12978409 : start
9275 	      -- Bug 5199672: Removed the condition l_qty_changed > 0
9276 	      /*IF ((l_rsv_rec.primary_reservation_quantity - l_qty_changed) >
9277 		   0.000005) THEN*/
9278            get_reservation_qty_lot(
9279  	                          p_rsv_rec              => l_rsv_rec,
9280  	                          p_reservation_qty_lot  => l_reservation_qty_lot);
9281 
9282       IF (l_fulfill_base = 'S') THEN                   -- MUOM fulfillment Project
9283         --
9284 	IF (l_debug= 1) THEN
9285           debug_print('Available quantity on the document for FB=S is l_reservation_qty_lot: ' || l_reservation_qty_lot);
9286        END IF;
9287        --
9288 	IF((l_reservation_qty_lot-l_qty_changed2)>0.000005) THEN
9289           IF (l_debug= 1) THEN
9290             debug_print('The demand document doesnt have enough quantity to be reserved against. error out. for fulfilment base=S');
9291           END IF;
9292           fnd_message.set_name('INV', 'INV_INVALID_AVAILABLE_QTY');
9293           fnd_msg_pub.add;
9294           raise fnd_api.g_exc_error;
9295         END IF;
9296       ELSE
9297         IF ((l_reservation_qty_lot - l_qty_changed) > 0.000005) THEN
9298           IF (l_debug = 1) THEN
9299             debug_print('The demand document doesnt have enough quantity to be reserved against. error out. ');
9300           END IF;
9301           fnd_message.set_name('INV', 'INV_INVALID_AVAILABLE_QTY');
9302           fnd_msg_pub.add;
9303           raise fnd_api.g_exc_error;
9304         END IF;
9305       END IF;   -- MUOM fulfillment Project ends
9306 
9307     END IF;
9308 
9309 	   /*** End R12 ***/
9310 
9311 	   -- Bug #2819700
9312 	   -- Adding an extra check to make sure that create reservations does not
9313 	   -- create a negative reservation record.
9314 	   IF (l_debug = 1) THEN
9315 	      debug_print('Primary_reservation_qty before inserting (create)= '
9316 			  || To_char(l_rsv_rec.primary_reservation_quantity) );
9317 	      debug_print('Secondary_reservation_qty before inserting (create)= '
9318 			  || To_char(l_rsv_rec.secondary_reservation_quantity) );  -- INVCONV
9319 	      debug_print('Reservation_qty before inserting (create)= '
9320 			  || To_char(l_rsv_rec.reservation_quantity) );
9321 	   END IF;
9322 
9323 	   IF (  (NVL(l_rsv_rec.reservation_quantity,0) < 0) OR
9324 		 (NVL(l_rsv_rec.primary_reservation_quantity,0) < 0) ) THEN
9325 	      fnd_message.set_name('INV', 'INV-INVALID RESERVATION QTY');
9326 	      fnd_msg_pub.ADD;
9327 	      RAISE fnd_api.g_exc_error;
9328 	   END IF;
9329 
9330 	   --Bug 5535030 Selecting the sequence value has been moved to the table handler in
9331 	   -- INVRSV6B.pls
9332 /*	   -- create reservation id
9333 	   SELECT mtl_demand_s.NEXTVAL
9334 	     INTO l_reservation_id
9335 	     FROM DUAL;*/
9336            l_reservation_id := NULL;
9337 	   --
9338 	   l_date := SYSDATE;
9339 
9340 	   --
9341 	   l_user_id            := fnd_global.user_id;
9342 	   l_login_id           := fnd_global.login_id;
9343 
9344 	   IF l_login_id = -1 THEN
9345 	      l_login_id  := fnd_global.conc_login_id;
9346 	   END IF;
9347 
9348 	   l_request_id         := fnd_global.conc_request_id;
9349 	   l_prog_appl_id       := fnd_global.prog_appl_id;
9350 	   l_program_id         := fnd_global.conc_program_id;
9351 	   --
9352 
9353 	   IF (l_debug = 1) THEN
9354 	      debug_print(' Before inserting record');
9355 	      debug_print(' reservation is' || l_reservation_id);
9356 	      debug_print(' reservation qty' || l_rsv_rec.reservation_quantity);
9357 	      debug_print(' reservation pri qty' || l_rsv_rec.primary_reservation_quantity);
9358 	      debug_print('l_secondary_qty_changed: ' || l_secondary_qty_changed);    -- INVCONV
9359 	   END IF;
9360 
9361 	   -- Bug 3461990: Reservations API should not create reservations with more
9362 	   -- than 5 decimal places, since the transaction quantity is being
9363 	   -- rounded to 5 decimal places.
9364 
9365 	   l_rsv_rec.primary_reservation_quantity :=
9366 	     Round(l_rsv_rec.primary_reservation_quantity,5);
9367 	   l_rsv_rec.reservation_quantity := Round(l_rsv_rec.reservation_quantity,5);
9368 
9369            IF (l_orig_item_cache_index is NULL) THEN
9370               inv_reservation_util_pvt.search_item_cache
9371                 (
9372                   x_return_status      => l_return_status
9373                  ,p_inventory_item_id  => l_rsv_rec.inventory_item_id
9374                  ,p_organization_id    => l_rsv_rec.organization_id
9375                  ,x_index              => l_orig_item_cache_index
9376                  );
9377 
9378               IF (l_return_status = fnd_api.g_ret_sts_error) THEN
9379                  RAISE fnd_api.g_exc_error;
9380               End If;
9381 
9382               IF (l_return_status = fnd_api.g_ret_sts_unexp_error) THEN
9383                  RAISE fnd_api.g_exc_unexpected_error;
9384               END IF;
9385 
9386               IF (l_orig_item_cache_index IS NULL) THEN
9387                  OPEN c_item(l_rsv_rec.inventory_item_id,l_rsv_rec.organization_id);
9388                  FETCH c_item into l_item_rec;
9389                  CLOSE c_item;
9390 
9391                  inv_reservation_util_pvt.add_item_cache
9392                   (
9393                    x_return_status              => l_return_status
9394                   ,p_item_record                => l_item_rec
9395                   ,x_index                      => l_orig_item_cache_index
9396                   );
9397 
9398                  IF (l_return_status = fnd_api.g_ret_sts_error) THEN
9399                    RAISE fnd_api.g_exc_error;
9400                  END IF;
9401 
9402                  IF (l_return_status = fnd_api.g_ret_sts_unexp_error) THEN
9403                    RAISE fnd_api.g_exc_unexpected_error;
9404                  END IF;
9405               END IF;
9406            END IF;
9407 
9408 	   IF is_dual_control(l_orig_item_cache_index) THEN                          -- INVCONV
9409 	      l_dual_control_flag := 'Y';
9410 	      l_rsv_rec.secondary_reservation_quantity :=
9411 		Round(l_rsv_rec.secondary_reservation_quantity,5);                    -- INVCONV
9412 	   END IF;                                                                   -- INVCONV
9413 
9414            IF l_serial_number.COUNT > 0 THEN
9415               l_rsv_rec.serial_reservation_quantity := l_serial_number.COUNT;
9416               IF (l_rsv_rec.serial_reservation_quantity > l_rsv_rec.primary_reservation_quantity) THEN
9417                  fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
9418                  fnd_msg_pub.ADD;
9419                  RAISE fnd_api.g_exc_error;
9420               END IF;
9421            END IF;
9422 
9423              -- Added for bug 8851133 -Start
9424             IF (l_dual_control_flag = 'Y') THEN
9425                        IF (  l_rsv_rec.detailed_quantity = 0 ) THEN
9426                                  l_rsv_rec.secondary_detailed_quantity := 0;
9427                         END IF;
9428             ELSE
9429                         l_rsv_rec.secondary_reservation_quantity := NULL;
9430             END IF;
9431 
9432             -- bug 8851133 - End
9433 
9434 	   IF (l_debug = 1) THEN
9435 	      debug_print(' Create: Before inserting record');
9436 	      debug_print(' After rounding reservation = ' || l_reservation_id);
9437 	      debug_print(' After rounding reservation qty = ' || l_rsv_rec.reservation_quantity);
9438 	      debug_print(' After rounding reservation pri qty = ' || l_rsv_rec.primary_reservation_quantity);
9439 	      debug_print(' After rounding reservation sec qty = ' || l_rsv_rec.secondary_reservation_quantity); -- INVCONV
9440               debug_print(' serial reservation qty = ' || l_rsv_rec.serial_reservation_quantity);
9441 	   END IF;
9442 
9443 	   -- INVCONV - Upgrade call to incorporate secondaries
9444 	   mtl_reservations_pkg.insert_row
9445 	     (
9446 	      x_rowid                       => l_rowid
9447 	      , x_reservation_id             => l_reservation_id
9448 	      , x_requirement_date           => l_rsv_rec.requirement_date
9449 	      , x_organization_id            => l_rsv_rec.organization_id
9450 	      , x_inventory_item_id          => l_rsv_rec.inventory_item_id
9451 	      , x_demand_source_type_id      => l_rsv_rec.demand_source_type_id
9452 	      , x_demand_source_name         => l_rsv_rec.demand_source_name
9453 	      , x_demand_source_header_id    => l_rsv_rec.demand_source_header_id
9454 	      , x_demand_source_line_id      => l_rsv_rec.demand_source_line_id
9455 	      , x_demand_source_delivery     => l_rsv_rec.demand_source_delivery
9456 	      , x_primary_uom_code           => l_rsv_rec.primary_uom_code
9457 	      , x_primary_uom_id             => l_rsv_rec.primary_uom_id
9458 	      , x_secondary_uom_code         => l_rsv_rec.secondary_uom_code
9459 	      , x_secondary_uom_id           => l_rsv_rec.secondary_uom_id
9460 	      , x_reservation_uom_code       => l_rsv_rec.reservation_uom_code
9461 	     , x_reservation_uom_id         => l_rsv_rec.reservation_uom_id
9462 	     , x_reservation_quantity       => l_rsv_rec.reservation_quantity
9463 	     , x_primary_reservation_quantity=> l_rsv_rec.primary_reservation_quantity
9464 	     , x_second_reservation_quantity=> l_rsv_rec.secondary_reservation_quantity
9465 	     , x_detailed_quantity          => l_rsv_rec.detailed_quantity
9466 	     , x_secondary_detailed_quantity=> l_rsv_rec.secondary_detailed_quantity
9467 	     , x_autodetail_group_id        => l_rsv_rec.autodetail_group_id
9468 	     , x_external_source_code       => l_rsv_rec.external_source_code
9469 	     , x_external_source_line_id    => l_rsv_rec.external_source_line_id
9470 	     , x_supply_source_type_id      => l_rsv_rec.supply_source_type_id
9471 	     , x_supply_source_header_id    => l_rsv_rec.supply_source_header_id
9472 	     , x_supply_source_line_id      => l_rsv_rec.supply_source_line_id
9473 	     , x_supply_source_line_detail  => l_rsv_rec.supply_source_line_detail
9474 	     , x_supply_source_name         => l_rsv_rec.supply_source_name
9475 	     , x_revision                   => l_rsv_rec.revision
9476 	     , x_subinventory_code          => l_rsv_rec.subinventory_code
9477 	     , x_subinventory_id            => l_rsv_rec.subinventory_id
9478 	     , x_locator_id                 => l_rsv_rec.locator_id
9479 	     , x_lot_number                 => l_rsv_rec.lot_number
9480 	     , x_lot_number_id              => l_rsv_rec.lot_number_id
9481 	     , x_serial_number              => NULL
9482 	     , x_serial_number_id           => NULL
9483 	     , x_partial_quantities_allowed => NULL
9484 	     , x_auto_detailed              => NULL
9485 	     , x_pick_slip_number           => l_rsv_rec.pick_slip_number
9486 	     , x_lpn_id                     => l_rsv_rec.lpn_id
9487 	     , x_last_update_date           => l_date
9488 	     , x_last_updated_by            => l_user_id
9489 	     , x_creation_date              => l_date
9490 	     , x_created_by                 => l_user_id
9491 	     , x_last_update_login          => l_login_id
9492 	     , x_request_id                 => l_request_id
9493 	     , x_program_application_id     => l_prog_appl_id
9494 	     , x_program_id                 => l_program_id
9495 	     , x_program_update_date        => l_date
9496 	     , x_attribute_category         => l_rsv_rec.attribute_category
9497 	     , x_attribute1                 => l_rsv_rec.attribute1
9498 	     , x_attribute2                 => l_rsv_rec.attribute2
9499 	     , x_attribute3                 => l_rsv_rec.attribute3
9500 	     , x_attribute4                 => l_rsv_rec.attribute4
9501 	     , x_attribute5                 => l_rsv_rec.attribute5
9502 	     , x_attribute6                 => l_rsv_rec.attribute6
9503 	     , x_attribute7                 => l_rsv_rec.attribute7
9504 	     , x_attribute8                 => l_rsv_rec.attribute8
9505 	     , x_attribute9                 => l_rsv_rec.attribute9
9506 	     , x_attribute10                => l_rsv_rec.attribute10
9507 	     , x_attribute11                => l_rsv_rec.attribute11
9508 	     , x_attribute12                => l_rsv_rec.attribute12
9509 	     , x_attribute13                => l_rsv_rec.attribute13
9510 	     , x_attribute14                => l_rsv_rec.attribute14
9511 	     , x_attribute15                => l_rsv_rec.attribute15
9512 	     , x_ship_ready_flag            => l_rsv_rec.ship_ready_flag
9513 	     , x_staged_flag                => l_rsv_rec.staged_flag
9514 	     /**** {{ R12 Enhanced reservations code changes }}****/
9515 	     , x_crossdock_flag             => l_rsv_rec.crossdock_flag
9516 	     , x_crossdock_criteria_id      => l_rsv_rec.crossdock_criteria_id
9517 	     , x_demand_source_line_detail  => l_rsv_rec.demand_source_line_detail
9518 	     , x_serial_reservation_quantity => l_rsv_rec.serial_reservation_quantity
9519 	     , x_supply_receipt_date        => l_rsv_rec.supply_receipt_date
9520 	     , x_demand_ship_date             => l_rsv_rec.demand_ship_date
9521 	     , x_project_id                   => l_rsv_rec.project_id
9522 	     , x_task_id                      => l_rsv_rec.task_id
9523 	     , x_orig_supply_type_id   => l_rsv_rec.supply_source_type_id
9524 	     , x_orig_supply_header_id => l_rsv_rec.supply_source_header_id
9525 	     , x_orig_supply_line_id     => l_rsv_rec.supply_source_line_id
9526 	     , x_orig_supply_line_detail => l_rsv_rec.supply_source_line_detail
9527 	     , x_orig_demand_type_id     => l_rsv_rec.demand_source_type_id
9528 	     , x_orig_demand_header_id   => l_rsv_rec.demand_source_header_id
9529 	     , x_orig_demand_line_id     => l_rsv_rec.demand_source_line_id
9530 	     , x_orig_demand_line_detail => l_rsv_rec.demand_source_line_detail
9531 	     /*** End R12 ***/
9532 	     );
9533 
9534   	   IF (l_debug = 1) THEN
9535 		   debug_print(' After call to insert_row reservation_id = ' || l_reservation_id);
9536 	   END IF;
9537 	   IF (l_debug = 1) THEN
9538 	      debug_print('before sync ' || l_return_status);
9539 	   END IF;
9540 	   -- for data sync b/w mtl_demand and mtl_reservations
9541 	   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);
9542 
9543 	   IF (l_debug = 1) THEN
9544 	      debug_print('After sync ' || l_return_status);
9545 	   END IF;
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 	   /**** {{ R12 Enhanced reservations code changes. Serial number
9557 	   -- changes for creating new reservations  }}****/
9558 
9559 	   IF (l_debug = 1) THEN
9560 	      debug_print('Serial number count' || l_serial_number.COUNT);
9561 	      debug_print('Original Serial number count' || p_serial_number.COUNT);
9562 	   END IF;
9563 
9564 	   IF l_serial_number.COUNT > 0 THEN
9565 
9566 	      IF (l_debug = 1) THEN
9567 		 debug_print('Inside serial loop' || l_serial_number.COUNT);
9568 	      END IF;
9569 
9570 	      FOR l_serial_index IN l_serial_number.first..l_serial_number.last
9571 		LOOP
9572 		   IF (l_debug = 1) THEN
9573 		      debug_print('reservation id' ||
9574 				  l_rsv_rec.reservation_id);
9575 		      debug_print('serial number' ||
9576 				  l_serial_number(l_serial_index).serial_number);
9577 		      debug_print('inventory item id' ||
9578 				  l_rsv_rec.inventory_item_id);
9579 		      debug_print('org id' ||
9580 				  l_rsv_rec.organization_id);
9581 
9582 		   END IF;
9583 
9584 		 BEGIN
9585 		    SELECT group_mark_id INTO l_group_mark_id FROM
9586 		      mtl_serial_numbers WHERE
9587 		      serial_number = l_serial_number(l_serial_index).serial_number AND
9588 		      inventory_item_id = l_rsv_rec.inventory_item_id AND
9589 		      current_organization_id = l_rsv_rec.organization_id;
9590 		    EXCEPTION
9591 		    WHEN no_data_found THEN
9592 
9593 		       IF (l_debug = 1) THEN
9594 			  debug_print('Errow while selecting the serial number. serial Number ' || l_serial_number(l_serial_index).serial_number);
9595 		       END IF;
9596 		       fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
9597 		       fnd_msg_pub.ADD;
9598 		       RAISE fnd_api.g_exc_error;
9599 		 END;
9600 
9601 		 IF (l_group_mark_id IS NOT NULL)  AND (l_group_mark_id <> -1)THEN
9602 		    IF (l_debug = 1) THEN
9603 		       debug_print('Group Mark Id is not null for serial ' || l_serial_number(l_serial_index).serial_number);
9604 		    END IF;
9605 		    fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
9606 		    fnd_msg_pub.ADD;
9607 		    RAISE fnd_api.g_exc_error;
9608 		 END IF;
9609 
9610 		 BEGIN
9611 		    UPDATE mtl_serial_numbers SET reservation_id =
9612 		      l_reservation_id, group_mark_id =
9613 		      l_reservation_id WHERE
9614 		      serial_number = l_serial_number(l_serial_index).serial_number AND
9615 		      inventory_item_id = l_rsv_rec.inventory_item_id AND
9616 		      current_organization_id = l_rsv_rec.organization_id;
9617 
9618 		 EXCEPTION
9619 		    WHEN no_data_found THEN
9620 
9621 		       IF (l_debug = 1) THEN
9622 			  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);
9623 		       END IF;
9624 		       fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
9625 		       fnd_msg_pub.ADD;
9626 		       RAISE fnd_api.g_exc_error;
9627 		 END;
9628 		END LOOP;
9629 
9630 	   END IF;
9631 
9632 	   /*** End R12 ***/
9633 	   --
9634 	   -- Post Insert CTO Validation
9635 	   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
9636 	      --
9637 	      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);
9638 
9639 	      IF (l_debug = 1) THEN
9640 		 debug_print('After post CTO validation ' || l_return_status);
9641 	      END IF;
9642 	      --
9643 	      IF l_return_status = fnd_api.g_ret_sts_error THEN
9644 		 RAISE fnd_api.g_exc_error;
9645 	      END IF;
9646 
9647 	      --
9648 	      IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9649 		 RAISE fnd_api.g_exc_unexpected_error;
9650 	      END IF;
9651 	   END IF;
9652 
9653 	   insert_rsv_temp
9654 	     (
9655 	      p_organization_id            => l_rsv_rec.organization_id
9656 	      , p_inventory_item_id          => l_rsv_rec.inventory_item_id
9657 	      , p_primary_reservation_quantity=> l_rsv_rec.primary_reservation_quantity
9658 	      , p_tree_id                    => l_tree_id
9659 	      , p_reservation_id             => l_reservation_id
9660 	      , x_return_status              => l_return_status
9661 	      , p_demand_source_line_id      => l_rsv_rec.demand_source_line_id
9662 	      , p_demand_source_header_id    => l_rsv_rec.demand_source_header_id
9663 	      , p_demand_source_name         => l_rsv_rec.demand_source_name
9664 	      );
9665 
9666 	   IF (l_debug = 1) THEN
9667 	      debug_print('After insert into rsv temp ' || l_return_status);
9668 	   END IF;
9669 
9670 	   IF l_return_status = fnd_api.g_ret_sts_error THEN
9671 	      RAISE fnd_api.g_exc_error;
9672 	   END IF;
9673 
9674 	   --
9675 	   IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9676 	      RAISE fnd_api.g_exc_unexpected_error;
9677 	   END IF;
9678 
9679 	   --
9680 	   IF (l_debug = 1) THEN
9681 	      debug_print('Final status after create reservation API ' ||
9682 			  l_return_status);
9683 	      debug_print('qty reserved ' ||
9684 			  l_rsv_rec.primary_reservation_quantity);
9685 	      debug_print('secondary qty reserved ' ||
9686 			  l_rsv_rec.secondary_reservation_quantity);                  -- INVCONV
9687 	      debug_print('reservation id' ||
9688 			  l_reservation_id);
9689 
9690 	   END IF;
9691 
9692 	   /**** {{ R12 Enhanced reservations code changes. Should be
9693 	   -- releasing the locks. }} *****/
9694 	   IF l_lock_obtained THEN
9695 	      inv_reservation_lock_pvt.release_lock
9696 		(l_supply_lock_handle);
9697 	      inv_reservation_lock_pvt.release_lock
9698 		(l_demand_lock_handle);
9699 	   END IF;
9700 	   /*** End R12 ***/
9701 
9702 	   -- set output variables
9703 	   x_quantity_reserved  := l_rsv_rec.primary_reservation_quantity;
9704 
9705 	   IF l_dual_control_flag = 'Y' THEN                           -- INVCONV
9706 	      x_secondary_quantity_reserved := l_rsv_rec.secondary_reservation_quantity; -- INVCONV
9707 	   END IF;                                                                    -- INVCONV
9708 
9709 	   x_reservation_id     := l_reservation_id;
9710 	   --
9711 	   x_return_status      := l_return_status;
9712 	   /**** {{ R12 Enhanced reservations code changes. Serial number
9713 	   -- changes for creating new reservations  }}****/
9714 	   x_serial_number := l_serial_number;
9715 	   /*** End R12 ***/
9716 	   --
9717 
9718 	   /**** {{ R12 Enhanced reservations code changes }}****/
9719     END IF; -- If not l_rsv is true
9720     -- Do this only if the l_rsv_is set to false. Otherwise, we
9721     -- would have updated the record.
9722     /*** End R12 ***/
9723 
9724   EXCEPTION
9725      WHEN fnd_api.g_exc_error THEN
9726 	ROLLBACK TO create_reservation_sa;
9727 	x_return_status  := fnd_api.g_ret_sts_error;
9728 	/**** {{ R12 Enhanced reservations code changes. Should be
9729 	-- releasing the locks. }} *****/
9730 	IF l_lock_obtained THEN
9731 	   inv_reservation_lock_pvt.release_lock
9732 	     (l_supply_lock_handle);
9733 	   inv_reservation_lock_pvt.release_lock
9734 	     (l_demand_lock_handle);
9735 	END IF;
9736 	/*** End R12 ***/
9737 	--  Get message count and data
9738 	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
9739      WHEN fnd_api.g_exc_unexpected_error THEN
9740 	ROLLBACK TO create_reservation_sa;
9741 	x_return_status  := fnd_api.g_ret_sts_unexp_error;
9742 	/**** {{ R12 Enhanced reservations code changes. Should be
9743 	-- releasing the locks. }} *****/
9744 	IF l_lock_obtained THEN
9745 	   inv_reservation_lock_pvt.release_lock
9746 	     (l_supply_lock_handle);
9747 	   inv_reservation_lock_pvt.release_lock
9748 	     (l_demand_lock_handle);
9749 	END IF;
9750 	/*** End R12 ***/
9751 	--  Get message count and data
9752 	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
9753      WHEN OTHERS THEN
9754 	ROLLBACK TO create_reservation_sa;
9755 	x_return_status  := fnd_api.g_ret_sts_unexp_error;
9756 	/**** {{ R12 Enhanced reservations code changes. Should be
9757 	-- releasing the locks. }} *****/
9758 	IF l_lock_obtained THEN
9759 	   inv_reservation_lock_pvt.release_lock
9760 	     (l_supply_lock_handle);
9761 	   inv_reservation_lock_pvt.release_lock
9762 	     (l_demand_lock_handle);
9763 	END IF;
9764 	/*** End R12 ***/
9765 	IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
9766 	   fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
9767 	END IF;
9768 
9769 	--  Get message count and data
9770 	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
9771   END create_reservation;
9772 
9773 
9774   PROCEDURE update_reservation
9775     (
9776      p_api_version_number            IN     NUMBER
9777      , p_init_msg_lst                  IN     VARCHAR2 DEFAULT fnd_api.g_false
9778      , x_return_status                 OUT    NOCOPY VARCHAR2
9779      , x_msg_count                     OUT    NOCOPY NUMBER
9780      , x_msg_data                      OUT    NOCOPY VARCHAR2
9781      , p_original_rsv_rec              IN     inv_reservation_global.mtl_reservation_rec_type
9782      , p_to_rsv_rec                    IN     inv_reservation_global.mtl_reservation_rec_type
9783      , p_original_serial_number        IN     inv_reservation_global.serial_number_tbl_type
9784      , p_to_serial_number              IN     inv_reservation_global.serial_number_tbl_type
9785      , p_validation_flag               IN     VARCHAR2 DEFAULT fnd_api.g_true
9786      , p_check_availability            IN     VARCHAR2 DEFAULT fnd_api.g_false
9787      , p_over_reservation_flag         IN  NUMBER DEFAULT 0
9788      ) IS
9789 
9790 
9791      l_api_version_number CONSTANT NUMBER :=  1.0;
9792      l_api_name           CONSTANT VARCHAR2(30) := 'Update_Reservation';
9793      l_return_status      VARCHAR2(1) :=  fnd_api.g_ret_sts_success;
9794      l_quantity_reserved  NUMBER;
9795      l_secondary_quantity_reserved NUMBER;          -- INVCONV
9796      l_debug NUMBER;
9797 
9798      l_reservation_qty_lot NUMBER := 0; --Bug 12978409
9799 
9800     BEGIN
9801        --  Standard call to check for call compatibility
9802        IF NOT fnd_api.compatible_api_call
9803 	 (l_api_version_number
9804 	  , p_api_version_number
9805 	  , l_api_name
9806 	  , G_PKG_NAME
9807 	  ) THEN
9808 	  RAISE fnd_api.g_exc_unexpected_error;
9809        END IF;
9810 
9811        --  Initialize message list.
9812        IF fnd_api.to_boolean(p_init_msg_lst) THEN
9813 	  fnd_msg_pub.initialize;
9814        END IF;
9815 
9816        -- Use cache to get value for l_debug
9817        IF g_is_pickrelease_set IS NULL THEN
9818           g_is_pickrelease_set := 2;
9819           IF INV_CACHE.is_pickrelease THEN
9820              g_is_pickrelease_set := 1;
9821           END IF;
9822        END IF;
9823        IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
9824           g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
9825        END IF;
9826 
9827        l_debug := g_debug;
9828 
9829        IF l_debug=1 THEN
9830 	  debug_print('Calling the overloaded procedure update_reservation');
9831        END IF;
9832 
9833        -- INVCONV Upgrade call to incorporate secondary_quantity_reserved
9834        inv_reservation_pvt.update_reservation
9835 	 (p_api_version_number          => 1.0,
9836 	  p_init_msg_lst                => fnd_api.g_false,
9837 	  x_return_status               => l_return_status,
9838 	  x_msg_count                   => x_msg_count,
9839 	  x_msg_data                    => x_msg_data,
9840 	  x_quantity_reserved           => l_quantity_reserved,
9841 	  x_secondary_quantity_reserved => l_secondary_quantity_reserved,
9842 	  p_original_rsv_rec            => p_original_rsv_rec,
9843 	  p_to_rsv_rec                  => p_to_rsv_rec,
9844 	  p_original_serial_number      => p_original_serial_number ,
9845 	  p_to_serial_number            => p_to_serial_number,
9846 	  p_validation_flag             => p_validation_flag,
9847 	  p_partial_reservation_flag    => fnd_api.g_false,
9848 	  p_check_availability          => p_check_availability,
9849 	  p_over_reservation_flag       => p_over_reservation_flag
9850 	  );
9851 
9852 
9853        IF (l_debug=1) THEN
9854 	  debug_print ('Return Status after updating reservations '||l_return_status);
9855        END IF;
9856 
9857        IF l_return_status = fnd_api.g_ret_sts_error THEN
9858 
9859 	  IF l_debug=1 THEN
9860 	     debug_print('Raising expected error'||l_return_status);
9861 	  END IF;
9862 
9863 	  RAISE fnd_api.g_exc_error;
9864 
9865 	ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9866 
9867 	  IF l_debug=1 THEN
9868 	     debug_print('Raising Unexpected error'||l_return_status);
9869 	  END IF;
9870 
9871 	  RAISE fnd_api.g_exc_unexpected_error;
9872        END IF;
9873 
9874 
9875        x_return_status := l_return_status;
9876 
9877     EXCEPTION
9878 
9879        WHEN fnd_api.g_exc_error THEN
9880 	  x_return_status := fnd_api.g_ret_sts_error;
9881 	  --  Get message count and data
9882 	  fnd_msg_pub.count_and_get
9883 	    (  p_count => x_msg_count
9884 	       , p_data  => x_msg_data
9885 	       );
9886 
9887        WHEN fnd_api.g_exc_unexpected_error THEN
9888 	  x_return_status := fnd_api.g_ret_sts_unexp_error ;
9889 
9890           --  Get message count and data
9891 	  fnd_msg_pub.count_and_get
9892 	    (  p_count  => x_msg_count
9893 	       , p_data   => x_msg_data
9894 	       );
9895 
9896        WHEN OTHERS THEN
9897 	  x_return_status := fnd_api.g_ret_sts_unexp_error ;
9898 
9899 	  IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
9900 	    THEN
9901 	     fnd_msg_pub.add_exc_msg
9902 	       (  g_pkg_name
9903 		  , l_api_name
9904 		  );
9905 	  END IF;
9906 
9907 	  --  Get message count and data
9908 	  fnd_msg_pub.count_and_get
9909 	    (  p_count  => x_msg_count
9910 	       , p_data   => x_msg_data
9911 	       );
9912 
9913     END update_reservation;
9914 
9915     --overloaded procedure
9916     --
9917     -- INVCONV incorporate parameter x_secondary_quantity_reserved
9918     PROCEDURE update_reservation
9919       (
9920        p_api_version_number              IN     NUMBER
9921        , p_init_msg_lst                  IN     VARCHAR2 DEFAULT fnd_api.g_false
9922        , x_return_status                 OUT    NOCOPY VARCHAR2
9923        , x_msg_count                     OUT    NOCOPY NUMBER
9924        , x_msg_data                      OUT    NOCOPY VARCHAR2
9925        , x_quantity_reserved             OUT    NOCOPY NUMBER
9926        , x_secondary_quantity_reserved   OUT    NOCOPY NUMBER
9927        , p_original_rsv_rec              IN     inv_reservation_global.mtl_reservation_rec_type
9928        , p_to_rsv_rec                    IN     inv_reservation_global.mtl_reservation_rec_type
9929        , p_original_serial_number        IN     inv_reservation_global.serial_number_tbl_type
9930        , p_to_serial_number              IN     inv_reservation_global.serial_number_tbl_type
9931        , p_validation_flag               IN     VARCHAR2 DEFAULT fnd_api.g_true
9932        , p_partial_reservation_flag      IN     VARCHAR2 DEFAULT fnd_api.g_false
9933        , p_check_availability            IN     VARCHAR2 DEFAULT fnd_api.g_false
9934        , p_over_reservation_flag         IN  NUMBER DEFAULT 0
9935       ) IS
9936 	 l_api_version_number  CONSTANT NUMBER        := 1.0;
9937 	 l_api_name            CONSTANT VARCHAR2(30)  := 'Update_Reservation';
9938 	 l_return_status  VARCHAR2(1)            := fnd_api.g_ret_sts_success;
9939 	 l_miss_num  NUMBER                 := fnd_api.g_miss_num;
9940 	 l_miss_char VARCHAR2(1)            := fnd_api.g_miss_char;
9941 	 l_original_rsv_rec inv_reservation_global.mtl_reservation_rec_type;
9942 	 l_to_rsv_rec   inv_reservation_global.mtl_reservation_rec_type;
9943 	 l_orig_rsv_tbl inv_reservation_global.mtl_reservation_tbl_type;
9944 	 l_orig_rsv_tbl_count  NUMBER;
9945 	 l_to_rsv_tbl  inv_reservation_global.mtl_reservation_tbl_type;
9946 	 --
9947 	 l_to_rsv_tbl_count            NUMBER;
9948 	 l_tree_id1                    INTEGER;
9949 	 l_tree_id2                    INTEGER;
9950 	 l_primary_quantity_reserved   NUMBER;
9951 	 l_primary_uom_code            VARCHAR2(3);
9952 	 l_orig_item_cache_index       INTEGER                                         := NULL;
9953 	 l_orig_org_cache_index        INTEGER                                         := NULL;
9954 	 l_orig_demand_cache_index     INTEGER                                         := NULL;
9955 	 l_orig_supply_cache_index     INTEGER                                         := NULL;
9956 	 l_orig_sub_cache_index        INTEGER                                         := NULL;
9957 	 l_to_item_cache_index         INTEGER                                         := NULL;
9958 	 l_to_org_cache_index          INTEGER                                         := NULL;
9959 	 l_to_demand_cache_index       INTEGER                                         := NULL;
9960 	 l_to_supply_cache_index       INTEGER                                         := NULL;
9961 	 l_to_sub_cache_index          INTEGER                                         := NULL;
9962 	 l_date                        DATE;
9963 	 l_user_id                     NUMBER;
9964 	 l_request_id                  NUMBER;
9965 	 l_login_id                    NUMBER;
9966 	 l_prog_appl_id                NUMBER;
9967 	 l_program_id                  NUMBER;
9968 	 l_error_code                  NUMBER;
9969 	 l_debug number;
9970 	 l_quantity_reserved           NUMBER;
9971 	 l_secondary_quantity_reserved NUMBER;                                        -- INVCONV
9972 	 l_lot_divisible_flag          VARCHAR2(1)      := 'Y' ;                      -- INVCONV
9973 	 l_dual_control_flag           VARCHAR2(1)      := 'N' ;
9974 	 -- INVCONV
9975 	 /**** {{ R12 Enhanced reservations code changes }}****/
9976 	 l_original_serial_number inv_reservation_global.serial_number_tbl_type;
9977 	 l_to_serial_number inv_reservation_global.serial_number_tbl_type;
9978 	 l_qty_available NUMBER := 0;
9979 	 l_serial_number_table inv_reservation_global.serial_number_tbl_type;
9980 	 l_dummy_serial_array  inv_reservation_global.serial_number_tbl_type;
9981 	 l_dummy_rsv_rec inv_reservation_global.mtl_reservation_rec_type;
9982 	 l_serials_tobe_unreserved NUMBER;
9983 	 l_total_serials_reserved NUMBER;
9984 	 l_reservation_id NUMBER;
9985 	 l_original_serial_count NUMBER;
9986 	 l_to_serial_count NUMBER;
9987 	 l_supply_lock_handle varchar2(128);
9988 	 l_demand_lock_handle varchar2(128);
9989 	 l_lock_status NUMBER;
9990          l_reservable_qty NUMBER;
9991 	 l_booked_flag VARCHAR2(1) := 'N';
9992 	 l_open_flag	VARCHAR2(1);
9993 	 l_group_mark_id NUMBER := NULL;
9994 	 l_lock_obtained BOOLEAN := FALSE;
9995 	 l_pjm_enabled NUMBER;
9996 	 l_project_id NUMBER;
9997 	 l_task_id NUMBER;
9998 	 l_supply_source_type_id NUMBER;
9999 	 /*** End R12 ***/
10000 
10001     /* Added for CMRO bug 13829182 */
10002     l_wip_entity_id NUMBER;
10003     l_wip_entity_type NUMBER;
10004     l_wip_job_type VARCHAR2(15);
10005     l_maintenance_object_source NUMBER;
10006     /* End of changes for CMRO bug 13829182 */
10007 	-- MUOM Fulfillment Project
10008     l_reservable_qty2 NUMBER;
10009     l_qty_available2  NUMBER;
10010     l_fulfill_base   VARCHAR2(1) := 'P';
10011     l_org_sec_rsv_qty  NUMBER;
10012 
10013 	-- changes for bug 9874238 start
10014 	 CURSOR c_item(p_inventory_item_id NUMBER,p_organization_id NUMBER) IS
10015          SELECT *
10016            FROM mtl_system_items
10017           WHERE inventory_Item_Id = p_inventory_item_id
10018 	  AND organization_id = p_organization_id;
10019 	 l_item_rec  inv_reservation_global.item_record;
10020 	-- changes for bug 9874238 end
10021     l_reservation_qty_lot NUMBER := 0; --Bug 12978409
10022     BEGIN
10023 
10024      /*Bug 4700706. Moved the following statement up as otherwise when it was somewhere in the middle of  the
10025        procedure and if any error occurs before the definition of the savepoint , it would go to the EXCEPTION
10026        block and there we have 'Rollback to update_reservation_sa' statement which was causing issue.
10027      */
10028      SAVEPOINT update_reservation_sa;
10029 
10030        -- Use cache to get value for l_debug
10031        IF g_is_pickrelease_set IS NULL THEN
10032 	  g_is_pickrelease_set := 2;
10033         IF INV_CACHE.is_pickrelease THEN
10034            g_is_pickrelease_set := 1;
10035         END IF;
10036      END IF;
10037      IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
10038         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
10039      END IF;
10040 
10041      l_debug := g_debug;
10042 
10043      IF (l_debug = 1) THEN
10044         debug_print('Inside update reservation...');
10045      END IF;
10046 
10047 
10048      --  Standard call to check for call compatibility
10049     IF NOT fnd_api.compatible_api_call(l_api_version_number, p_api_version_number, l_api_name, g_pkg_name) THEN
10050       RAISE fnd_api.g_exc_unexpected_error;
10051     END IF;
10052 
10053     IF (l_debug = 1) THEN
10054        debug_print('The original reservation record: ');
10055     END IF;
10056 
10057     print_rsv_rec(p_original_rsv_rec);
10058 
10059     IF (l_debug = 1) THEN
10060        debug_print('The to reservation record: ');
10061     END IF;
10062 
10063     print_rsv_rec(p_to_rsv_rec);
10064 
10065     -- Bug #2819700
10066     -- Adding an extra check to make sure that the update reservations does
10067     -- not update the existing record to negative. Raise an exception.
10068     IF (l_debug = 1) THEN
10069       debug_print('Primary_reservation_qty before inserting (update)= '
10070 		              || To_char(p_to_rsv_rec.primary_reservation_quantity));
10071       debug_print('Secondary_reservation_qty before inserting (update)= '
10072                               || To_char(p_to_rsv_rec.secondary_reservation_quantity));  -- INVCONV
10073       debug_print('Reservation_qty before inserting (update)= '
10074 		              || To_char(p_to_rsv_rec.reservation_quantity));
10075     END IF;
10076 
10077     IF ((NVL(p_to_rsv_rec.reservation_quantity,0) < 0) OR
10078 	      (NVL(p_to_rsv_rec.primary_reservation_quantity,0) < 0) ) THEN
10079       fnd_message.set_name('INV', 'INV-INVALID RESERVATION QTY');
10080       fnd_msg_pub.ADD;
10081       RAISE fnd_api.g_exc_error;
10082     END IF;
10083 
10084     -- INVCONV BEGIN
10085     /*Bug#13045525 Changing the condition as the validation makes sense when the primary quantity is greater than zero */
10086     IF (NVL(p_to_rsv_rec.primary_reservation_quantity,0) > 0) AND (NVL(p_to_rsv_rec.secondary_reservation_quantity,0) < 0) THEN
10087       fnd_message.set_name('INV', 'INV-INVALID NEGATIVE SECONDARY QTY');    -- INVCONV NEW MESSAGE
10088       fnd_msg_pub.ADD;
10089       RAISE fnd_api.g_exc_error;
10090     END IF;
10091     -- INVCONV END
10092 
10093     /**** {{ R12 Enhanced reservations code changes. Initializing orig parameters }}****/
10094 
10095     -- Set the original columns to g_miss_xxx as the user should not be
10096     -- setting these values. Do not query by them
10097     l_original_rsv_rec := p_original_rsv_rec;
10098 
10099     l_original_rsv_rec.orig_supply_source_type_id := fnd_api.g_miss_num;
10100     l_original_rsv_rec.orig_supply_source_header_id := fnd_api.g_miss_num;
10101     l_original_rsv_rec.orig_supply_source_line_id := fnd_api.g_miss_num;
10102     l_original_rsv_rec.orig_supply_source_line_detail := fnd_api.g_miss_num;
10103     l_original_rsv_rec.orig_demand_source_type_id := fnd_api.g_miss_num;
10104     l_original_rsv_rec.orig_demand_source_header_id := fnd_api.g_miss_num;
10105     l_original_rsv_rec.orig_demand_source_line_id := fnd_api.g_miss_num;
10106     l_original_rsv_rec.orig_demand_source_line_detail := fnd_api.g_miss_num;
10107 
10108     /*** End R12 ***/
10109 
10110     --
10111     -- if the update to quantity is 0, call delete instead
10112     IF p_to_rsv_rec.primary_reservation_quantity = 0 OR
10113       (p_to_rsv_rec.reservation_quantity = 0 AND
10114        (p_to_rsv_rec.primary_reservation_quantity IS NULL OR p_to_rsv_rec.primary_reservation_quantity = fnd_api.g_miss_num))
10115 	 THEN
10116 
10117        delete_reservation
10118 	 (
10119 	  p_api_version_number         => 1.0
10120 	  , p_init_msg_lst             => p_init_msg_lst
10121 	  , x_return_status            => l_return_status
10122 	  , x_msg_count                => x_msg_count
10123 	  , x_msg_data                 => x_msg_data
10124 	  , p_rsv_rec                  => p_original_rsv_rec
10125 	  , p_original_serial_number   => p_original_serial_number
10126 	  , p_validation_flag          => p_validation_flag
10127 	  );
10128 
10129        IF l_return_status = fnd_api.g_ret_sts_error THEN
10130 	  RAISE fnd_api.g_exc_error;
10131        END IF;
10132 
10133        --
10134        IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10135 	  RAISE fnd_api.g_exc_unexpected_error;
10136        END IF;
10137 
10138        --
10139        x_return_status  := l_return_status;
10140        RETURN;
10141     END IF;
10142 
10143     --
10144     --  Initialize message list.
10145     IF fnd_api.to_boolean(p_init_msg_lst) THEN
10146        fnd_msg_pub.initialize;
10147     END IF;
10148 
10149     --
10150     --
10151     -- search for the from row
10152     IF (l_debug = 1) THEN
10153        debug_print('Querying Reservation for the from record');
10154     END IF;
10155     /**** {{ R12 Enhanced reservations code changes }}****/
10156     query_reservation
10157       (
10158        p_api_version_number         => 1.0
10159        , p_init_msg_lst               => fnd_api.g_false
10160        , x_return_status              => l_return_status
10161        , x_msg_count                  => x_msg_count
10162        , x_msg_data                   => x_msg_data
10163        , p_query_input                => l_original_rsv_rec
10164        , p_lock_records               => fnd_api.g_true
10165        , x_mtl_reservation_tbl        => l_orig_rsv_tbl
10166        , x_mtl_reservation_tbl_count  => l_orig_rsv_tbl_count
10167        , x_error_code                 => l_error_code
10168        );
10169     /*** End R12 ***/
10170     IF l_return_status = fnd_api.g_ret_sts_error THEN
10171        IF (l_debug = 1) THEN
10172 	  debug_print('Query Reservation returned error');
10173        END IF;
10174        RAISE fnd_api.g_exc_error;
10175     END IF;
10176 
10177     --
10178     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10179        IF (l_debug = 1) THEN
10180 	  debug_print('Query Reservation returned unexpected error');
10181        END IF;
10182        RAISE fnd_api.g_exc_unexpected_error;
10183     END IF;
10184 
10185     --
10186     IF l_orig_rsv_tbl_count = 0 THEN
10187        IF (l_debug = 1) THEN
10188 	  debug_print('Query Reservation returned no row');
10189        END IF;
10190        fnd_message.set_name('INV', 'INV-ROW NOT FOUND');
10191        fnd_msg_pub.ADD;
10192       RAISE fnd_api.g_exc_error;
10193     END IF;
10194 
10195     --
10196     IF l_orig_rsv_tbl_count > 1 THEN
10197        IF (l_debug = 1) THEN
10198 	  debug_print('Query Reservation returned more than one row');
10199        END IF;
10200        fnd_message.set_name('INV', 'INV-UPATE MORE THAN ONE RSV');
10201        fnd_msg_pub.ADD;
10202        RAISE fnd_api.g_exc_error;
10203     END IF;
10204 
10205     IF (l_debug = 1) THEN
10206        debug_print('Constructing to reservation row');
10207     END IF;
10208 
10209     construct_to_reservation_row(l_orig_rsv_tbl(1), p_to_rsv_rec, l_to_rsv_rec);
10210 
10211     IF (l_debug = 1) THEN
10212        debug_print('Constructed to reservation row');
10213     END IF;
10214 
10215     /**** {{ R12 Enhanced reservations code changes.Calling the reservation
10216     -- lock API to create a user-defined lock for non-inventory supplies }} *****/
10217     -- Bug 5199672: Should pass null to supply and demand line detail as
10218       -- we will have to lock the records at the document level and not at
10219       -- the line level. Also, for ASN, pass the source type as PO so that the
10220       -- the lock name would be the same as the PO's
10221 
10222     IF (l_to_rsv_rec.supply_source_type_id =
10223 	inv_reservation_global.g_source_type_asn) THEN
10224        l_supply_source_type_id :=
10225 	 inv_reservation_global.g_source_type_po;
10226      ELSE
10227        l_supply_source_type_id := l_to_rsv_rec.supply_source_type_id;
10228     END IF;
10229 
10230     IF (l_to_rsv_rec.supply_source_type_id <> inv_reservation_global.g_source_type_inv) THEN
10231        inv_reservation_lock_pvt.lock_supply_demand_record
10232 	 (p_organization_id => l_to_rsv_rec.organization_id
10233 	  ,p_inventory_item_id => l_to_rsv_rec.inventory_item_id
10234 	  ,p_source_type_id => l_supply_source_type_id
10235 	  ,p_source_header_id => l_to_rsv_rec.supply_source_header_id
10236 	  ,p_source_line_id =>  l_to_rsv_rec.supply_source_line_id
10237 	  ,p_source_line_detail => NULL
10238 	  ,x_lock_handle => l_supply_lock_handle
10239 	  ,x_lock_status => l_lock_status);
10240 
10241        IF l_lock_status = 0 THEN
10242 	  fnd_message.set_name('INV', 'INV_INVALID_LOCK');
10243 	  fnd_msg_pub.ADD;
10244 	  RAISE fnd_api.g_exc_error;
10245        END if;
10246 
10247        inv_reservation_lock_pvt.lock_supply_demand_record
10248 	 (p_organization_id => l_to_rsv_rec.organization_id
10249 	  ,p_inventory_item_id => l_to_rsv_rec.inventory_item_id
10250 	  ,p_source_type_id => l_to_rsv_rec.demand_source_type_id
10251 	  ,p_source_header_id => l_to_rsv_rec.demand_source_header_id
10252 	  ,p_source_line_id =>  l_to_rsv_rec.demand_source_line_id
10253 	  ,p_source_line_detail => NULL
10254 	  ,x_lock_handle => l_demand_lock_handle
10255 	  ,x_lock_status => l_lock_status);
10256 
10257        IF l_lock_status = 0 THEN
10258 	  fnd_message.set_name('INV', 'INV_INVALID_LOCK');
10259 	  fnd_msg_pub.ADD;
10260 	  RAISE fnd_api.g_exc_error;
10261        END if;
10262 
10263        l_lock_obtained := TRUE;
10264     END IF;
10265     /*** End R12 ***/
10266 
10267     -- here we might add some validation of input parameters
10268     -- for update api, the reservation id should not be changed
10269     IF l_orig_rsv_tbl(1).reservation_id <> l_to_rsv_rec.reservation_id THEN
10270        IF (l_debug = 1) THEN
10271 	  debug_print('Cannot update reservation ID');
10272        END IF;
10273        fnd_message.set_name('INV', 'CANNOT_UPDATE_RESERVATION_ID');
10274        fnd_msg_pub.ADD;
10275        RAISE fnd_api.g_exc_error;
10276     END IF;
10277 
10278     --
10279     IF l_orig_rsv_tbl(1).organization_id <> l_to_rsv_rec.organization_id THEN
10280        IF (l_debug = 1) THEN
10281 	  debug_print('Cannot update organization ID');
10282        END IF;
10283        fnd_message.set_name('INV', 'CANNOT_UPDATE_ORGANIZATION_ID');
10284        fnd_msg_pub.ADD;
10285        RAISE fnd_api.g_exc_error;
10286     END IF;
10287 
10288     --
10289     IF l_orig_rsv_tbl(1).inventory_item_id <> l_to_rsv_rec.inventory_item_id THEN
10290        IF (l_debug = 1) THEN
10291 	  debug_print('Cannot update Inventory Item ID');
10292        END IF;
10293        fnd_message.set_name('INV', 'CANNOT_UPDATE_INVENTORY_ITEM');
10294        fnd_msg_pub.ADD;
10295        RAISE fnd_api.g_exc_error;
10296     END IF;
10297 
10298     --
10299     -- convert quantity between primary uom and reservation uom
10300     convert_quantity(x_return_status => l_return_status, px_rsv_rec => l_to_rsv_rec);
10301 
10302     IF l_return_status = fnd_api.g_ret_sts_error THEN
10303        RAISE fnd_api.g_exc_error;
10304     END IF;
10305     --
10306     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10307        RAISE fnd_api.g_exc_unexpected_error;
10308     END IF;
10309 
10310     --
10311     -- if the caller does not specified reservation_id, l_to_rsv_rec will
10312     -- has the same reservation_id as l_orig_rsv_tbl(1) due to the way
10313     -- construct_to_reservation_row works.
10314     -- but we should set it to g_miss_num again
10315     -- otherwise query_reservation will use only the
10316     -- reservation_id to do the search.
10317     IF l_to_rsv_rec.reservation_id = l_orig_rsv_tbl(1).reservation_id THEN
10318        l_to_rsv_rec.reservation_id  := fnd_api.g_miss_num;
10319     END IF;
10320 
10321     /**** {{ R12 Enhanced reservations code changes }}****/
10322 
10323     -- Set the original columns to g_miss_xxx as the user should not be
10324     -- setting these values. Do not query by them
10325     l_to_rsv_rec.orig_supply_source_type_id := fnd_api.g_miss_num;
10326     l_to_rsv_rec.orig_supply_source_header_id := fnd_api.g_miss_num;
10327     l_to_rsv_rec.orig_supply_source_line_id := fnd_api.g_miss_num;
10328     l_to_rsv_rec.orig_supply_source_line_detail := fnd_api.g_miss_num;
10329     l_to_rsv_rec.orig_demand_source_type_id := fnd_api.g_miss_num;
10330     l_to_rsv_rec.orig_demand_source_header_id := fnd_api.g_miss_num;
10331     l_to_rsv_rec.orig_demand_source_line_id := fnd_api.g_miss_num;
10332     l_to_rsv_rec.orig_demand_source_line_detail := fnd_api.g_miss_num;
10333 
10334     IF (l_to_rsv_rec.project_id IS NULL)  THEN
10335        l_to_rsv_rec.project_id := fnd_api.g_miss_num;
10336     END IF;
10337     IF (l_to_rsv_rec.task_id IS NULL)  THEN
10338        l_to_rsv_rec.task_id := fnd_api.g_miss_num;
10339     END IF;
10340     /*** End R12 ***/
10341 
10342     IF (l_debug = 1) THEN
10343        debug_print('Querying reservatione for the to record');
10344     END IF;
10345 
10346     query_reservation
10347       (
10348        p_api_version_number           => 1.0
10349        , p_init_msg_lst               => fnd_api.g_false
10350        , x_return_status              => l_return_status
10351        , x_msg_count                  => x_msg_count
10352        , x_msg_data                   => x_msg_data
10353        , p_query_input                => l_to_rsv_rec
10354        , p_lock_records               => fnd_api.g_true
10355        , x_mtl_reservation_tbl        => l_to_rsv_tbl
10356        , x_mtl_reservation_tbl_count  => l_to_rsv_tbl_count
10357        , x_error_code                 => l_error_code
10358        );
10359     IF (l_debug = 1) THEN
10360        debug_print('Queried reservation');
10361     END IF;
10362 
10363     --
10364     IF l_return_status = fnd_api.g_ret_sts_error THEN
10365        IF (l_debug = 1) THEN
10366 	  debug_print('Query Reservation returned error');
10367        END IF;
10368        RAISE fnd_api.g_exc_error;
10369     END IF;
10370 
10371     --
10372     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10373        IF (l_debug = 1) THEN
10374 	  debug_print('Query Reservation returned unexpected error');
10375        END IF;
10376        RAISE fnd_api.g_exc_unexpected_error;
10377     END IF;
10378 
10379     --
10380     IF l_to_rsv_tbl_count > 1
10381       OR l_to_rsv_tbl_count > 0
10382       AND l_to_rsv_tbl(1).reservation_id <> l_orig_rsv_tbl(1).reservation_id THEN
10383        IF (l_debug = 1) THEN
10384 	  debug_print('Reservation target row exists');
10385        END IF;
10386        fnd_message.set_name('INV', 'INV-RSV TARGET ROW EXISTS');
10387        fnd_msg_pub.ADD;
10388        RAISE fnd_api.g_exc_error;
10389     END IF;
10390 
10391     /**** {{ R12 Enhanced reservations code changes }}****/
10392 
10393     -- Get the project and task for demands in OE, INT-ORD and RMA
10394     IF l_debug=1 THEN
10395        debug_print('Before Rsv rec project id: ' || l_to_rsv_rec.project_id);
10396        debug_print('Before Rsv rec task id: ' || l_to_rsv_rec.task_id);
10397     END IF;
10398 
10399     BEGIN
10400        SELECT project_reference_enabled
10401 	 INTO l_pjm_enabled
10402 	 FROM   mtl_parameters
10403 	 WHERE  organization_id = l_to_rsv_rec.organization_id;
10404     EXCEPTION
10405        WHEN no_data_found THEN
10406 	  IF l_debug=1 THEN
10407 	     debug_print('Cannot find the project and task information');
10408 	  END IF;
10409     END;
10410 
10411     IF (l_to_rsv_rec.demand_source_type_id IN
10412 	(inv_reservation_global.g_source_type_oe,
10413 	 inv_reservation_global.g_source_type_internal_ord,
10414 	 inv_reservation_global.g_source_type_rma)) AND
10415       (l_pjm_enabled = 1) THEN
10416 
10417        IF (l_to_rsv_rec.demand_source_line_id IS NOT NULL) AND
10418 	 (l_to_rsv_rec.demand_source_line_id <> fnd_api.g_miss_num)
10419 	 AND ((l_to_rsv_rec.project_id = fnd_api.g_miss_num) OR
10420 	      (l_to_rsv_rec.task_id = fnd_api.g_miss_num)) THEN
10421 	 BEGIN
10422 	    SELECT project_id, task_id INTO l_project_id, l_task_id
10423 	      FROM oe_order_lines_all WHERE
10424 	      line_id = l_to_rsv_rec.demand_source_line_id;
10425 	 EXCEPTION
10426 	    WHEN no_data_found THEN
10427 	       IF l_debug=1 THEN
10428 		  debug_print('Cannot find the project and task information');
10429 	       END IF;
10430 	 END;
10431 
10432 	 IF (l_to_rsv_rec.project_id = fnd_api.g_miss_num) THEN
10433 	    IF (l_project_id IS NOT NULL) THEN
10434 	       l_to_rsv_rec.project_id := l_project_id;
10435 	     ELSE
10436 	       l_to_rsv_rec.project_id := NULL;
10437 	    END IF;
10438 	 END IF;
10439 
10440 	 IF (l_to_rsv_rec.task_id = fnd_api.g_miss_num) THEN
10441 	    IF (l_task_id IS NOT NULL) THEN
10442 	       l_to_rsv_rec.task_id := l_task_id;
10443 	     ELSE
10444 	       l_to_rsv_rec.task_id := NULL;
10445 	    END IF;
10446 	 END IF;
10447 
10448        END IF;
10449        /* Added elseif for CMRO bug 13829182 */
10450     ELSIF (  (l_to_rsv_rec.demand_source_type_id = inv_reservation_global.g_source_type_wip) AND (l_pjm_enabled = 1))  THEN
10451         BEGIN
10452                  SELECT we.entity_type, wdj.maintenance_object_source
10453                      INTO  l_wip_entity_id, l_maintenance_object_source
10454                    FROM wip_entities we, wip_discrete_jobs wdj
10455                 WHERE we.wip_entity_id = l_to_rsv_rec.demand_source_header_id
10456                    AND we.wip_entity_id = wdj.wip_entity_id(+);
10457         EXCEPTION
10458             WHEN no_data_found THEN
10459                  IF (l_debug = 1) THEN
10460                     debug_print('No WIP entity record found for the source header passed' );
10461                  END IF;
10462         END;
10463 
10464         IF l_wip_entity_id = 6 and l_maintenance_object_source = 2  then
10465             l_wip_entity_type := inv_reservation_global.g_wip_source_type_cmro;
10466             l_wip_job_type := 'CMRO'; -- AHL
10467         ELSE
10468             l_wip_entity_type := null;
10469             l_wip_job_type := null; -- AHL
10470         END IF;
10471 
10472         IF ( l_wip_job_type = 'CMRO' AND l_to_rsv_rec.demand_source_line_detail IS NOT NULL)
10473             AND (l_to_rsv_rec.demand_source_line_detail <> fnd_api.g_miss_num)
10474             AND ((l_to_rsv_rec.project_id = fnd_api.g_miss_num) OR (l_to_rsv_rec.task_id = fnd_api.g_miss_num)) THEN
10475 
10476             BEGIN
10477                 SELECT wdj.project_id, WDJ.TASK_ID
10478                     INTO l_project_id, l_task_id
10479                    FROM ahl_schedule_materials asmt, ahl_workorders aw, WIP_DISCRETE_JOBS WDJ
10480                 WHERE asmt.scheduled_material_id = l_to_rsv_rec.demand_source_line_detail
10481                      AND asmt.visit_task_id           = aw.visit_task_id
10482                      AND ASMT.VISIT_ID                = AW.VISIT_ID
10483                      AND aw.wip_entity_id             = wdj.wip_entity_id
10484                      AND AW.STATUS_CODE              IN ('1','3') -- 1:Unreleased,3:Released
10485                      AND ASMT.STATUS                  = 'ACTIVE';
10486 
10487             EXCEPTION
10488                 WHEN others THEN
10489                     IF l_debug=1 THEN
10490                         debug_print('Cannot find the project and task information from CMRO WO: '||sqlerrm);
10491                     END IF;
10492             END;
10493 
10494 
10495             IF (l_to_rsv_rec.project_id = fnd_api.g_miss_num) THEN
10496                 IF (l_project_id IS NOT NULL ) THEN
10497                     l_to_rsv_rec.project_id := l_project_id;
10498                 ELSE
10499                     l_to_rsv_rec.project_id := NULL;
10500                 END IF;
10501             END IF;
10502 
10503             IF (l_to_rsv_rec.task_id = fnd_api.g_miss_num) THEN
10504                 IF (l_task_id IS NOT NULL) THEN
10505                     l_to_rsv_rec.task_id := l_task_id;
10506                 ELSE
10507                     l_to_rsv_rec.task_id := NULL;
10508                 END IF;
10509             END IF;
10510 
10511         END IF;
10512         /* End of changes for CMRO bug 13829182 */
10513      ELSE -- not project enable
10514 	  l_to_rsv_rec.project_id := NULL;
10515 	  l_to_rsv_rec.task_id := NULL;
10516     END IF;
10517     IF l_debug=1 THEN
10518        debug_print('After Rsv rec project id: ' || l_to_rsv_rec.project_id);
10519        debug_print('After Rsv rec task id: ' || l_to_rsv_rec.task_id);
10520     END IF;
10521 
10522     l_original_serial_count := p_original_serial_number.COUNT;
10523     l_to_serial_count := p_to_serial_number.COUNT;
10524     -- if from and to serials are passed, then pass them to the validate API
10525     IF l_original_serial_count > 0 THEN
10526        l_original_serial_number := p_original_serial_number;
10527     END IF;
10528     IF l_to_serial_count > 0 THEN
10529        l_to_serial_number := p_to_serial_number;
10530     END IF;
10531     /*** End R12 ***/
10532     --
10533     --Bug 2354735: Validate if p_validation_flag is either 'T' or 'V'
10534     IF (p_validation_flag = fnd_api.g_true OR p_validation_flag = 'V') THEN
10535       IF (l_debug = 1) THEN
10536          debug_print('Validation flag is true');
10537          debug_print('from dmd src type:'|| l_orig_rsv_tbl(1).demand_source_type_id);
10538          debug_print('to dmd src type:'|| l_to_rsv_rec.demand_source_type_id);
10539          debug_print('from dmd src hdr:'|| l_orig_rsv_tbl(1).demand_source_header_id);
10540          debug_print('to dmd src hdr:'|| l_to_rsv_rec.demand_source_header_id);
10541          debug_print('from dmd src line:'|| l_orig_rsv_tbl(1).demand_source_line_id);
10542          debug_print('to dmd src line:'|| l_to_rsv_rec.demand_source_line_id);
10543       END IF;
10544       -- we do validation after the query because
10545       -- for update, we might have many input value set to
10546       -- missing. We need to use the actual value to do
10547       -- validation
10548       inv_reservation_validate_pvt.validate_input_parameters
10549 	(
10550 	 x_return_status              => l_return_status
10551 	 , p_orig_rsv_rec               => l_orig_rsv_tbl(1)
10552 	 , p_to_rsv_rec                 => l_to_rsv_rec
10553 	 /**** {{ R12 Enhanced reservations code changes }}****/
10554 	 , p_orig_serial_array          => l_original_serial_number
10555 	 , p_to_serial_array            => l_to_serial_number
10556 	 /*** End R12 ***/
10557 	 , p_rsv_action_name            => 'UPDATE'
10558 	 , x_orig_item_cache_index      => l_orig_item_cache_index
10559 	 , x_orig_org_cache_index       => l_orig_org_cache_index
10560 	 , x_orig_demand_cache_index    => l_orig_demand_cache_index
10561 	 , x_orig_supply_cache_index    => l_orig_supply_cache_index
10562 	 , x_orig_sub_cache_index       => l_orig_sub_cache_index
10563 	 , x_to_item_cache_index        => l_to_item_cache_index
10564 	 , x_to_org_cache_index         => l_to_org_cache_index
10565 	 , x_to_demand_cache_index      => l_to_demand_cache_index
10566 	 , x_to_supply_cache_index      => l_to_supply_cache_index
10567 	 , x_to_sub_cache_index         => l_to_sub_cache_index
10568 	 );
10569      -- Bug 16434922
10570      IF (l_debug = 1) THEN
10571 	    debug_print('l_orig_item_cache_index: '|| l_orig_item_cache_index);
10572 	 END IF;
10573     IF l_orig_item_cache_index IS NOT NULL THEN
10574 	    IF is_dual_control(l_orig_item_cache_index) THEN
10575 	       l_dual_control_flag := 'Y';
10576 	    END IF;
10577 	END IF;
10578 	-- End Bug 16434922
10579       --
10580       IF l_return_status = fnd_api.g_ret_sts_error THEN
10581 	 IF (l_debug = 1) THEN
10582 	    debug_print('Validate input parameters returned error');
10583 	 END IF;
10584 	 RAISE fnd_api.g_exc_error;
10585       END IF;
10586 
10587       --
10588       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10589 	 IF (l_debug = 1) THEN
10590 	    debug_print('Validate input parameters returned unexpected error');
10591 	 END IF;
10592 	 RAISE fnd_api.g_exc_unexpected_error;
10593       END IF;
10594     END IF;
10595 
10596     -- INVCONV B4498579 BEGIN
10597     -- Use cache to determine lot divisibility where possible
10598     IF NVL(l_to_item_cache_index,0) > 0 THEN
10599       IF NOT is_lot_divisible(l_to_item_cache_index) THEN
10600        l_lot_divisible_flag := 'N';
10601       END IF;
10602     ELSE
10603     -- otherwise, where cache not available
10604       IF NOT lot_divisible(l_orig_rsv_tbl(1).inventory_item_id,l_orig_rsv_tbl(1).organization_id) THEN
10605 	l_lot_divisible_flag := 'N';
10606       END IF;
10607     END IF;
10608     IF (l_debug = 1) THEN
10609       debug_print('Update scenario and lot divisible is '||l_lot_divisible_flag);
10610     END IF;
10611     -- INVCONV B4498579 END
10612 
10613     --
10614     --Bug 2354735: Proceed with Trees only if p_validation_flag = 'T'
10615 
10616     -- Pick Releaser Performance - Added validation_flag = 'Q' to distinguish
10617     -- between validation above and the qty tree processing below.
10618      /**** {{ R12 Enhanced reservations code changes }}****/
10619     IF (((p_validation_flag = fnd_api.g_true) OR (p_validation_flag = 'T') OR
10620        (p_validation_flag = 'Q')) AND l_to_rsv_rec.supply_source_type_id = inv_reservation_global.g_source_type_inv)
10621       THEN
10622        /*** End R12 ***/
10623 
10624 	-- changes for bug 9874238 start
10625        	IF (l_orig_item_cache_index is NULL) THEN
10626               inv_reservation_util_pvt.search_item_cache
10627                 (
10628                   x_return_status      => l_return_status
10629                  ,p_inventory_item_id  => l_orig_rsv_tbl(1).inventory_item_id
10630                  ,p_organization_id    => l_orig_rsv_tbl(1).organization_id
10631                  ,x_index              => l_orig_item_cache_index
10632                  );
10633 
10634               IF (l_return_status = fnd_api.g_ret_sts_error) THEN
10635                  RAISE fnd_api.g_exc_error;
10636               End If;
10637 
10638               IF (l_return_status = fnd_api.g_ret_sts_unexp_error) THEN
10639                  RAISE fnd_api.g_exc_unexpected_error;
10640               END IF;
10641 
10642               IF (l_orig_item_cache_index IS NULL) THEN
10643                  OPEN c_item(l_orig_rsv_tbl(1).inventory_item_id,l_orig_rsv_tbl(1).organization_id);
10644                  FETCH c_item into l_item_rec;
10645                  CLOSE c_item;
10646 
10647                  inv_reservation_util_pvt.add_item_cache
10648                   (
10649                    x_return_status              => l_return_status
10650                   ,p_item_record                => l_item_rec
10651                   ,x_index                      => l_orig_item_cache_index
10652                   );
10653 
10654                  IF (l_return_status = fnd_api.g_ret_sts_error) THEN
10655                    RAISE fnd_api.g_exc_error;
10656                  END IF;
10657 
10658                  IF (l_return_status = fnd_api.g_ret_sts_unexp_error) THEN
10659                    RAISE fnd_api.g_exc_unexpected_error;
10660                  END IF;
10661               END IF;
10662            END IF;
10663 
10664 	IF (l_debug = 1) THEN
10665       		debug_print('After adding item to the cache l_orig_item_cache_index: '||l_orig_item_cache_index);
10666     	END IF;
10667 	-- changes for bug 9874238 end
10668 
10669        inv_quantity_tree_pvt.create_tree
10670 	 (
10671 	  p_api_version_number         => 1.0
10672 	  , p_init_msg_lst               => fnd_api.g_true
10673 	  , x_return_status              => l_return_status
10674 	  , x_msg_count                  => x_msg_count
10675 	  , x_msg_data                   => x_msg_data
10676 	  , p_organization_id            => l_orig_rsv_tbl(1).organization_id
10677 	  , p_inventory_item_id          => l_orig_rsv_tbl(1).inventory_item_id
10678 	  , p_tree_mode                  => inv_quantity_tree_pvt.g_reservation_mode
10679 	  , p_is_revision_control        => is_revision_control(l_orig_item_cache_index)
10680 	  , p_is_lot_control             => is_lot_control(l_orig_item_cache_index)
10681 	  , p_is_serial_control          => is_serial_control(l_orig_item_cache_index)
10682 	  , p_asset_sub_only             => FALSE
10683 	  , p_include_suggestion         => TRUE
10684 	  , p_demand_source_type_id      => l_orig_rsv_tbl(1).demand_source_type_id
10685          , p_demand_source_header_id    => l_orig_rsv_tbl(1).demand_source_header_id
10686          , p_demand_source_line_id      => l_orig_rsv_tbl(1).demand_source_line_id
10687          , p_demand_source_name         => l_orig_rsv_tbl(1).demand_source_name
10688          , p_demand_source_delivery     => l_orig_rsv_tbl(1).demand_source_delivery
10689          , p_lot_expiration_date        => SYSDATE -- Bug#2716563
10690          , x_tree_id                    => l_tree_id1
10691          );
10692 
10693          --
10694          IF l_return_status = fnd_api.g_ret_sts_error THEN
10695            IF (l_debug = 1) THEN
10696               debug_print('Create Tree returned error');
10697            END IF;
10698            RAISE fnd_api.g_exc_error;
10699          END IF;
10700 
10701          --
10702          IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10703            IF (l_debug = 1) THEN
10704               debug_print('Create Tree returned unexpected error');
10705            END IF;
10706            RAISE fnd_api.g_exc_unexpected_error;
10707          END IF;
10708 
10709           --
10710          /*  Bug 1575930
10711           *  Because of changes to the quantity tree API, we no longer
10712           *  build separate trees on the basis of demand info.  The tree created
10713           *  above and this tree would actually be the same tree, but would
10714           *  would have different tree_ids.  We don't need to update the same
10715           *  tree twice, so we don't need to create the same tree twice.
10716           *  Instead, pass NULL to the modify_tree procedure.
10717           *inv_quantity_tree_pvt.create_tree
10718           * (
10719           *   p_api_version_number      => 1.0
10720           * , p_init_msg_lst            => fnd_api.g_true
10721           * , x_return_status           => l_return_status
10722           * , x_msg_count               => x_msg_count
10723           * , x_msg_data                => x_msg_data
10724           * , p_organization_id         => l_to_rsv_rec.organization_id
10725           * , p_inventory_item_id       => l_to_rsv_rec.inventory_item_id
10726           * , p_tree_mode               => inv_quantity_tree_pvt.g_reservation_mode
10727           * , p_is_revision_control     => is_revision_control(l_to_item_cache_index)
10728           * , p_is_lot_control          => is_lot_control(l_to_item_cache_index)
10729           * , p_is_serial_control       => is_serial_control(l_to_item_cache_index)
10730           * , p_asset_sub_only          => FALSE
10731           * , p_include_suggestion      => TRUE
10732           * , p_demand_source_type_id   => l_to_rsv_rec.demand_source_type_id
10733           * , p_demand_source_header_id => l_to_rsv_rec.demand_source_header_id
10734           * , p_demand_source_line_id   => l_to_rsv_rec.demand_source_line_id
10735           * , p_demand_source_name      => l_to_rsv_rec.demand_source_name
10736           * , p_demand_source_delivery  => l_to_rsv_rec.demand_source_delivery
10737           * , p_lot_expiration_date     => NULL
10738           * , x_tree_id                 => l_tree_id2
10739           *);
10740           --
10741           *IF l_return_status = fnd_api.g_ret_sts_error THEN
10742           *   RAISE fnd_api.g_exc_error;
10743           *END IF ;
10744           --
10745           *IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10746           *   RAISE fnd_api.g_exc_unexpected_error;
10747           *END IF;
10748           */
10749           --
10750          l_tree_id2  := NULL;
10751          -- INVCONV - Upgrade call to incorporate secondaries
10752          modify_tree_for_update_xfer(
10753            x_return_status              => l_return_status
10754          , x_quantity_reserved          => l_quantity_reserved
10755          , x_secondary_quantity_reserved => l_secondary_quantity_reserved
10756          , p_from_tree_id               => l_tree_id1
10757          , p_from_supply_source_type_id => l_orig_rsv_tbl(1).supply_source_type_id
10758          , p_from_revision              => l_orig_rsv_tbl(1).revision
10759          , p_from_lot_number            => l_orig_rsv_tbl(1).lot_number
10760          , p_from_subinventory_code     => l_orig_rsv_tbl(1).subinventory_code
10761          , p_from_locator_id            => l_orig_rsv_tbl(1).locator_id
10762          , p_from_lpn_id                => l_orig_rsv_tbl(1).lpn_id
10763          , p_from_primary_rsv_quantity  => l_orig_rsv_tbl(1).primary_reservation_quantity
10764          , p_from_second_rsv_quantity   => l_orig_rsv_tbl(1).secondary_reservation_quantity
10765          , p_from_detailed_quantity     => l_orig_rsv_tbl(1).detailed_quantity
10766          , p_from_sec_detailed_quantity => l_orig_rsv_tbl(1).secondary_detailed_quantity
10767          , p_to_tree_id                 => l_tree_id2
10768          , p_to_supply_source_type_id   => l_to_rsv_rec.supply_source_type_id
10769          , p_to_revision                => l_to_rsv_rec.revision
10770          , p_to_lot_number              => l_to_rsv_rec.lot_number
10771          , p_to_subinventory_code       => l_to_rsv_rec.subinventory_code
10772          , p_to_locator_id              => l_to_rsv_rec.locator_id
10773          , p_to_lpn_id                  => l_to_rsv_rec.lpn_id
10774          , p_to_primary_rsv_quantity    => l_to_rsv_rec.primary_reservation_quantity
10775          , p_to_second_rsv_quantity     => l_to_rsv_rec.secondary_reservation_quantity
10776          , p_to_detailed_quantity       => l_to_rsv_rec.detailed_quantity
10777          , p_to_second_detailed_quantity => l_to_rsv_rec.secondary_detailed_quantity
10778          , p_to_revision_control        => is_revision_control(l_to_item_cache_index)
10779          , p_to_lot_control             => is_lot_control(l_to_item_cache_index)
10780          , p_action                     => 'UPDATE'
10781          , p_lot_divisible_flag         => l_lot_divisible_flag                          -- INVCONV
10782          , p_partial_reservation_flag   => p_partial_reservation_flag
10783          , p_check_availability         => p_check_availability
10784          );
10785 
10786          IF l_return_status = fnd_api.g_ret_sts_error THEN
10787            IF (l_debug = 1) THEN
10788               debug_print('modify_tree_for_update_xfer returned error');
10789            END IF;
10790            RAISE fnd_api.g_exc_error;
10791          END IF;
10792 
10793          --
10794          IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10795            IF (l_debug = 1) THEN
10796               debug_print('modify_tree_for_update_xfer returned unexpected error');
10797            END IF;
10798            RAISE fnd_api.g_exc_unexpected_error;
10799          END IF;
10800 
10801 
10802          IF (l_debug = 1) THEN
10803               debug_print('modify_tree_for_update_xfer returned success ');
10804               debug_print('The value for x_quantity_reserved is'||l_quantity_reserved );
10805               debug_print('The value for x_secondary_quantity_reserved is'||l_secondary_quantity_reserved ); --INVCONV
10806 	 END IF;
10807 
10808 	 IF  l_quantity_reserved > 0
10809 	   AND l_quantity_reserved < l_to_rsv_rec.primary_reservation_quantity - NVL(l_to_rsv_rec.detailed_quantity, 0) THEN
10810 	    -- This is the case of partial reservations. We  need to recompute
10811 	    -- the actual quantity for reservation
10812 	    -- convert quantity between primary uom and reservation uom
10813 	    l_to_rsv_rec.primary_reservation_quantity  := l_quantity_reserved + NVL(l_to_rsv_rec.detailed_quantity, 0);
10814 	    -- INVCONV BEGIN
10815 	    IF is_dual_control(l_orig_item_cache_index) THEN
10816 	       l_dual_control_flag := 'Y';
10817 	       l_to_rsv_rec.secondary_reservation_quantity  := l_secondary_quantity_reserved + NVL(l_to_rsv_rec.secondary_detailed_quantity, 0); -- Bug 6942475
10818 	    END IF;
10819 	    -- INVCONV END
10820 	    l_to_rsv_rec.reservation_quantity          := NULL;
10821 
10822 	    IF (l_debug = 1) THEN
10823 	       debug_print('l_to_rsv_rec.detailed_quantity: ' || l_to_rsv_rec.detailed_quantity);
10824 	       debug_print('l_to_rsv_rec.primary_reservation_quantity: ' || l_to_rsv_rec.primary_reservation_quantity);
10825 	       debug_print('l_to_rsv_rec.secondary_reservation_quantity: ' || l_to_rsv_rec.secondary_reservation_quantity); -- INVCONV
10826 	    END IF;
10827 
10828 	    convert_quantity(x_return_status => l_return_status, px_rsv_rec => l_to_rsv_rec);
10829 
10830 
10831 	    IF (l_debug = 1) THEN
10832 	       debug_print('After convert qty ' || l_return_status);
10833 	    END IF;
10834 
10835 	    IF l_return_status = fnd_api.g_ret_sts_error THEN
10836 	       IF (l_debug = 1) THEN
10837 		  debug_print('The convert_quantity returned a expected error');
10838 	       END IF;
10839 	       RAISE fnd_api.g_exc_error;
10840 	    END IF;
10841 
10842 	    IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10843 	       IF (l_debug = 1) THEN
10844 		  debug_print('The convert_quantity returned a unexpected error');
10845 	       END IF;
10846 	       RAISE fnd_api.g_exc_unexpected_error;
10847 	    END IF;
10848 
10849 	 END IF;--partial reservation case
10850 
10851 
10852  /**** {{ R12 Enhanced reservations code changes }}****/
10853      ELSIF (l_to_rsv_rec.supply_source_type_id IN
10854 	    (inv_reservation_global.g_source_type_wip, inv_reservation_global.g_source_type_po,
10855 	     inv_reservation_global.g_source_type_asn,
10856 	     inv_reservation_global.g_source_type_intransit,
10857 	     inv_reservation_global.g_source_type_internal_req,
10858 	     inv_reservation_global.g_source_type_rcv))  AND
10859        p_over_reservation_flag NOT IN (1,3) THEN
10860        -- call the helper procedure to get the reservable qty of the supply
10861        -- Bug 5199672: Should pass g_miss_num as default for supply
10862        -- source line detail. Otherwise, high level reservations
10863        -- will not be considered.
10864        get_supply_reservable_qty
10865 	 (
10866        	    x_return_status                 => l_return_status
10867 	  , x_msg_count                     => x_msg_count
10868 	  , x_msg_data                      => x_msg_data
10869           , p_fm_supply_source_type_id      => l_orig_rsv_tbl(1).supply_source_type_id
10870           , p_fm_supply_source_header_id    => l_orig_rsv_tbl(1).supply_source_header_id
10871           , p_fm_supply_source_line_id      => l_orig_rsv_tbl(1).supply_source_line_id
10872           , p_fm_supply_source_line_detail  => l_orig_rsv_tbl(1).supply_source_line_detail
10873           , p_fm_primary_reservation_qty    => l_orig_rsv_tbl(1).primary_reservation_quantity
10874           , p_to_supply_source_type_id      => l_to_rsv_rec.supply_source_type_id
10875           , p_to_supply_source_header_id    => l_to_rsv_rec.supply_source_header_id
10876           , p_to_supply_source_line_id      => l_to_rsv_rec.supply_source_line_id
10877           , p_to_supply_source_line_detail  => l_to_rsv_rec.supply_source_line_detail
10878           , p_to_primary_reservation_qty    => l_to_rsv_rec.primary_reservation_quantity
10879           , p_to_organization_id            => l_to_rsv_rec.organization_id
10880           , p_to_inventory_item_id          => l_to_rsv_rec.inventory_item_id
10881           , p_to_revision                   => l_to_rsv_rec.revision
10882           , p_to_lot_number                 => l_to_rsv_rec.lot_number
10883           , p_to_subinventory_code          => l_to_rsv_rec.subinventory_code
10884           , p_to_locator_id                 => l_to_rsv_rec.locator_id
10885           , p_to_lpn_id                     => l_to_rsv_rec.lpn_id
10886           , p_to_project_id                 => l_to_rsv_rec.project_id
10887           , p_to_task_id                    => l_to_rsv_rec.task_id
10888 	 , x_reservable_qty                 => l_quantity_reserved
10889 	 , x_qty_available                  => l_qty_available
10890 	 );
10891 
10892        IF (l_debug = 1) THEN
10893 	  debug_print('After calling get supply reservable qty ' || l_return_status);
10894 	  debug_print('Available quantity to reserve:  ' || l_quantity_reserved);
10895        END IF;
10896 
10897        --
10898        IF l_return_status = fnd_api.g_ret_sts_error THEN
10899 	  RAISE fnd_api.g_exc_error;
10900        END IF;
10901 
10902        --
10903        IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10904 	  RAISE fnd_api.g_exc_unexpected_error;
10905        END IF;
10906 
10907 
10908        IF ((l_to_rsv_rec.primary_reservation_quantity - l_quantity_reserved)
10909 	 > 0.000005) THEN
10910 
10911           IF (p_partial_reservation_flag = fnd_api.g_false) THEN
10912              IF (l_debug = 1) THEN
10913                 debug_print('The supply document doesnt have enough quantity to be reserved against. error out. ');
10914              END IF;
10915              fnd_message.set_name('INV', 'INV_INVALID_AVAILABLE_QTY');
10916              fnd_msg_pub.ADD;
10917              RAISE fnd_api.g_exc_error;
10918           ELSE
10919              l_to_rsv_rec.primary_reservation_quantity  := l_quantity_reserved;
10920              l_to_rsv_rec.reservation_quantity          := NULL;
10921              IF (l_debug = 1) THEN
10922                 debug_print('l_to_rsv_rec.primary_reservation_quantity: ' || l_to_rsv_rec.primary_reservation_quantity);
10923              END IF;
10924              convert_quantity(x_return_status => l_return_status, px_rsv_rec => l_to_rsv_rec);
10925 
10926              IF (l_debug = 1) THEN
10927                 debug_print('After convert qty ' || l_return_status);
10928              END IF;
10929 
10930              IF l_return_status = fnd_api.g_ret_sts_error THEN
10931                 RAISE fnd_api.g_exc_error;
10932              END IF;
10933 
10934              --
10935              IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10936                 RAISE fnd_api.g_exc_unexpected_error;
10937              END IF;
10938 
10939           END IF;
10940 
10941        END IF;
10942 
10943     END IF;
10944 
10945     IF (l_debug = 1) THEN
10946        debug_print('From record:');
10947        debug_print('demand_source_type_id = ' || l_orig_rsv_tbl(1).demand_source_type_id);
10948        debug_print('demand_source_header_id = ' || l_orig_rsv_tbl(1).demand_source_header_id);
10949        debug_print('demand_source_line_id = ' || l_orig_rsv_tbl(1).demand_source_line_id);
10950        debug_print('demand_source_line_detail = ' || l_orig_rsv_tbl(1).demand_source_line_detail);
10951        debug_print('To record:');
10952        debug_print('demand_source_type_id = ' || l_to_rsv_rec.demand_source_type_id);
10953        debug_print('demand_source_header_id = ' || l_to_rsv_rec.demand_source_header_id);
10954        debug_print('demand_source_line_id = ' || l_to_rsv_rec.demand_source_line_id);
10955        debug_print('demand_source_line_detail = ' || l_to_rsv_rec.demand_source_line_detail);
10956     END IF;
10957 
10958     IF (l_to_rsv_rec.demand_source_type_id IN
10959 	(inv_reservation_global.g_source_type_oe,
10960 	 inv_reservation_global.g_source_type_internal_ord,inv_reservation_global.g_source_type_rma)) THEN
10961       BEGIN
10962 	 SELECT open_flag, booked_flag
10963 	   INTO l_open_flag,
10964 	   l_booked_flag
10965 	   FROM oe_order_lines_all
10966 	   WHERE line_id = l_to_rsv_rec.demand_source_line_id;
10967       EXCEPTION WHEN no_data_found THEN
10968       fnd_message.set_name('INV', 'INV_INVALID_SALES_ORDER');
10969       fnd_msg_pub.ADD;
10970       RAISE fnd_api.g_exc_error;
10971       END;
10972     END IF;
10973 
10974     IF (l_debug = 1) THEN
10975        debug_print('Open flag :' || l_open_flag);
10976        debug_print('booked flag :' || l_booked_flag);
10977     END IF;
10978 
10979     IF (((l_to_rsv_rec.demand_source_type_id IN
10980 	 (inv_reservation_global.g_source_type_oe,
10981 	  inv_reservation_global.g_source_type_internal_ord, inv_reservation_global.g_source_type_rma)) AND
10982 
10983 	(NOT((Nvl(l_orig_rsv_tbl(1).demand_source_type_id,-99) = Nvl(l_to_rsv_rec.demand_source_type_id,-99)) AND
10984 	 (Nvl(l_orig_rsv_tbl(1).demand_source_header_id,-99) = Nvl(l_to_rsv_rec.demand_source_header_id,-99)) AND
10985 	 (Nvl(l_orig_rsv_tbl(1).demand_source_line_id,-99) = Nvl(l_to_rsv_rec.demand_source_line_id,-99)) AND
10986 	  nvl(l_open_flag, 'N') = 'N'))) OR
10987       (l_to_rsv_rec.demand_source_type_id =
10988        inv_reservation_global.g_source_type_wip)) AND
10989        p_over_reservation_flag NOT IN (2,3)  THEN
10990 
10991        -- call the helper procedure to get the reservable qty of the demand
10992        IF (l_debug = 1) THEN
10993 	     debug_print('Inside get demand reservable qty');
10994        END IF;
10995       Inv_Utilities.Get_Inv_Fulfillment_Base(
10996                     p_source_line_id		      => l_to_rsv_rec.demand_source_line_id,
10997 		    p_demand_source_type_id	=> l_to_rsv_rec.demand_source_type_id,
10998 		    p_org_id		              => l_to_rsv_rec.organization_id,
10999                    x_fulfillment_base		    => l_fulfill_base	);
11000 
11001       IF (l_fulfill_base = 'S') THEN
11002       l_org_sec_rsv_qty :=l_orig_rsv_tbl(1).secondary_reservation_quantity;  -- MUOM fulfillment Project
11003       ELSE
11004       l_org_sec_rsv_qty:=null;
11005       END IF;
11006       --
11007       IF (l_debug = 1) THEN
11008          debug_print( ' orginal_secondary_reservation_quantity'||l_orig_rsv_tbl(1).secondary_reservation_quantity);
11009       END IF;
11010        --
11011        -- Bug 5199672: Should pass g_miss_num as default for demand
11012        -- source line detail. Otherwise, high level reservations
11013        -- will not be considered.
11014        get_demand_reservable_qty
11015 	 (
11016 	  x_return_status                 => l_return_status
11017 	  , x_msg_count                     => x_msg_count
11018 	  , x_msg_data                      => x_msg_data
11019           , p_fm_demand_source_type_id      => l_orig_rsv_tbl(1).demand_source_type_id
11020           , p_fm_demand_source_header_id    => l_orig_rsv_tbl(1).demand_source_header_id
11021           , p_fm_demand_source_line_id      => l_orig_rsv_tbl(1).demand_source_line_id
11022           , p_fm_demand_source_line_detail  => l_orig_rsv_tbl(1).demand_source_line_detail
11023           , p_fm_primary_reservation_qty    => l_orig_rsv_tbl(1).primary_reservation_quantity
11024 	   , p_fm_secondary_reservation_qty  => l_org_sec_rsv_qty -- MUOM fulfillment Project
11025           , p_to_demand_source_type_id      => l_to_rsv_rec.demand_source_type_id
11026           , p_to_demand_source_header_id    => l_to_rsv_rec.demand_source_header_id
11027           , p_to_demand_source_line_id      => l_to_rsv_rec.demand_source_line_id
11028           , p_to_demand_source_line_detail  => l_to_rsv_rec.demand_source_line_detail
11029           , p_to_primary_reservation_qty    => l_to_rsv_rec.primary_reservation_quantity
11030 	 , p_to_organization_id            => l_to_rsv_rec.organization_id
11031 	 , p_to_inventory_item_id          => l_to_rsv_rec.inventory_item_id
11032 	 , p_to_primary_uom_code           => l_to_rsv_rec.primary_uom_code
11033 	 , p_to_project_id                 => l_to_rsv_rec.project_id
11034 	 , p_to_task_id                    => l_to_rsv_rec.task_id
11035          /*Fixed for bug#8402349
11036            Variable l_reservable_qty is used because variable l_quantity_reserved hold the
11037            qty that is reserved and using that in this call will override it's value by
11038            reservable qty on demand. result in wrong value returned for qty reserved.
11039          */
11040 	 /*, x_reservable_qty              =>  l_quantity_reserved */
11041           , x_reservable_qty               =>  l_reservable_qty
11042 	 , x_qty_available                 => l_qty_available
11043 	 , x_reservable_qty2               =>  l_reservable_qty2
11044          , x_qty_available2                 => l_qty_available2
11045 	 );
11046 
11047 
11048        IF (l_debug = 1) THEN
11049 	  debug_print('After calling available demand to reserve ' || l_return_status);
11050 	  debug_print('Available quantity to reserve. l_qty_changed: ' || l_quantity_reserved);
11051           debug_print('Available quantity to reserve. l_reservable_qty: ' || l_reservable_qty);
11052           debug_print('Available quantity to reserve. l_reservable_qty2: ' || l_reservable_qty2);
11053        END IF;
11054 
11055        --
11056        IF l_return_status = fnd_api.g_ret_sts_error THEN
11057 	  RAISE fnd_api.g_exc_error;
11058        END IF;
11059 
11060        --
11061        IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11062 	  RAISE fnd_api.g_exc_unexpected_error;
11063        END IF;
11064 
11065         /*Fixed for bug#8402349
11066            Variable l_reservable_qty is used rather than variable l_quantity_reserved because
11067            variable l_reservable_qty is used now in call to API get_demand_reservable_qty
11068          */
11069 
11070            --Bug 12978409 : start
11071            -- Bug 5199672: Removed the condition l_qty_changed > 0
11072           /*  IF ((l_to_rsv_rec.primary_reservation_quantity -
11073                     l_reservable_qty) > 0.000005) THEN*/
11074               get_reservation_qty_lot( p_rsv_rec    => l_to_rsv_rec,
11075                                                    p_reservation_qty_lot  => l_reservation_qty_lot);
11076        -- MUOM fulfillment Project
11077        inv_utilities.get_inv_fulfillment_base(
11078                                      p_source_line_id	 => l_to_rsv_rec.demand_source_line_id,
11079 			             p_demand_source_type_id => l_to_rsv_rec.demand_source_type_id,
11080 			             p_org_id  => l_to_rsv_rec.organization_id,
11081                                      x_fulfillment_base => l_fulfill_base);
11082 
11083       IF (l_fulfill_base = 'S') THEN
11084 
11085         IF((l_reservation_qty_lot-l_reservable_qty2)>0.000005) THEN
11086           IF (l_debug = 1) Then
11087             debug_print('The demand document doesnt have enough quantity to be reserved against. error out. for fulfilment base=S ');
11088              debug_print('l_reservable_qty2:= '||l_reservable_qty2);
11089              debug_print('l_reservation_qty_lot:= '||l_reservation_qty_lot);
11090           END IF;
11091           fnd_message.set_name('INV', 'INV_INVALID_AVAILABLE_QTY');
11092           fnd_msg_pub.add;
11093           raise fnd_api.g_exc_error;
11094         END IF;
11095       ELSE
11096         IF ((l_reservation_qty_lot - l_reservable_qty) > 0.000005) THEN
11097           IF (l_debug = 1) THEN
11098             debug_print('The demand document doesnt have enough quantity to be reserved against. error out. ');
11099             debug_print('l_to_rsv_rec.primary_reservation_quantity: ' || l_to_rsv_rec.primary_reservation_quantity);
11100           END IF;
11101           fnd_message.set_name('INV', 'INV_INVALID_AVAILABLE_QTY');
11102           fnd_msg_pub.add;
11103           raise fnd_api.g_exc_error;
11104         END IF;
11105       END IF;
11106       -- MUOM fulfillment Project ends
11107     END IF;
11108 
11109     /*** End R12 ***/
11110 
11111     --
11112     -- Pre Update CTO Validation
11113     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
11114        --
11115        IF (l_debug = 1) THEN
11116           debug_print('Pre Update CTO validation');
11117        END IF;
11118 
11119        IF l_orig_rsv_tbl(1).primary_reservation_quantity > l_to_rsv_rec.primary_reservation_quantity THEN
11120 	  cto_workflow_api_pk.inventory_unreservation_check
11121 	    (
11122 	     p_order_line_id              => l_orig_rsv_tbl(1).demand_source_line_id
11123 	     , p_rsv_quantity               => l_orig_rsv_tbl(1).primary_reservation_quantity - l_to_rsv_rec.primary_reservation_quantity
11124 	     , x_return_status              => l_return_status
11125 	     , x_msg_count                  => x_msg_count
11126 	     , x_msg_data                   => x_msg_data
11127 	     );
11128 
11129 	ELSE  --Else Condition Added for Bug#2467387.
11130 	  cto_workflow_api_pk.inventory_reservation_check
11131 	    (
11132 	     p_order_line_id => l_orig_rsv_tbl(1).demand_source_line_id
11133 	     ,x_return_status => l_return_status
11134 	     ,x_msg_count => x_msg_count
11135 	     ,x_msg_data => x_msg_data
11136 	     );
11137        END IF;
11138        --
11139        IF l_return_status = fnd_api.g_ret_sts_error THEN
11140 	  debug_print('Pre Update CTO validation returned error');
11141 	  RAISE fnd_api.g_exc_error;
11142        END IF;
11143 
11144        --
11145        IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11146 	  debug_print('Pre Update CTO validation returned unexpected error');
11147 	  RAISE fnd_api.g_exc_unexpected_error;
11148        END IF;
11149        --
11150     END IF;
11151 
11152     --Bug 1838450.  Detailed quantity should never exceed primary
11153     --  reservation quantity.  So, if someone has reduced the
11154     --  reservation quantity, we should reduce the detailed quantity also
11155 
11156     IF l_to_rsv_rec.detailed_quantity > l_to_rsv_rec.primary_reservation_quantity THEN
11157        IF (l_debug = 1) THEN
11158 	  debug_print('Setting detailed quantity the same as the primary reservation quantity');
11159        END IF;
11160        l_to_rsv_rec.detailed_quantity  := l_to_rsv_rec.primary_reservation_quantity;
11161        -- INVCONV BEGIN
11162        IF l_dual_control_flag = 'Y' THEN
11163 	  l_to_rsv_rec.secondary_detailed_quantity  := l_to_rsv_rec.secondary_reservation_quantity;
11164        END IF;
11165        -- INVCONV END
11166     END IF;
11167 
11168     --Bug #2819700
11169     --Adding an extra check to make sure that the update reservations does
11170     --not update the existing record to negative. Raise an exception.
11171     -- Adding this check after the tree is created and
11172     -- modify_tree_for_update_xfer is called and some computations
11173     -- have been made.
11174     IF (l_debug = 1) THEN
11175       debug_print('Primary_reservation_qty before inserting (update)= '
11176 		              || To_char(l_to_rsv_rec.primary_reservation_quantity));
11177       debug_print('Secondary_reservation_qty before inserting (update)= '
11178                               || To_char(l_to_rsv_rec.secondary_reservation_quantity));
11179       debug_print('Reservation_qty before inserting (update)= '
11180 		              || To_char(l_to_rsv_rec.reservation_quantity)); -- INVCONV
11181     END IF;
11182 
11183     IF (  (NVL(l_to_rsv_rec.reservation_quantity,0) < 0) OR
11184 	        (NVL(l_to_rsv_rec.primary_reservation_quantity,0) < 0) ) THEN
11185       fnd_message.set_name('INV', 'INV-INVALID RESERVATION QTY');
11186       fnd_msg_pub.ADD;
11187       RAISE fnd_api.g_exc_error;
11188     END IF;
11189 
11190      -- INVCONV BEGIN
11191     IF (NVL(l_to_rsv_rec.secondary_reservation_quantity,0) < 0) THEN
11192       fnd_message.set_name('INV', 'INV-INVALID NEGATIVE SECONDARY');   -- INVCONV New Message
11193       fnd_msg_pub.ADD;
11194       RAISE fnd_api.g_exc_error;
11195     END IF;
11196     -- INVCONV END
11197 
11198     /**** {{ R12 Enhanced reservations code changes }}****/
11199     -- Check for the serial reservations. All serial related checks will be
11200     -- done here.
11201     IF (l_debug = 1) THEN
11202        debug_print('Original serial count' || l_original_serial_count);
11203        debug_print('To serial count' || l_to_serial_count);
11204     END IF;
11205 
11206     IF (l_original_serial_count = 0 AND l_to_serial_count = 0) THEN
11207        -- both from and to serial tables are empty. They are not passed.
11208        IF (l_debug = 1) THEN
11209 	  debug_print('Inside serial check. Not passed');
11210        END IF;
11211        BEGIN
11212 	  SELECT inventory_item_id, serial_number bulk collect INTO
11213 	    l_serial_number_table FROM
11214 	    mtl_serial_numbers  WHERE reservation_id =
11215 	   l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
11216 	    l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
11217        EXCEPTION
11218 	  WHEN no_data_found THEN
11219 	     IF l_debug=1 THEN
11220 		debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
11221 	     END IF;
11222        END;
11223 
11224        l_total_serials_reserved := l_serial_number_table.COUNT;
11225        IF l_debug=1 THEN
11226 		debug_print('Total reserved serials: ' || l_total_serials_reserved);
11227        END IF;
11228 
11229        IF (l_total_serials_reserved > 0) THEN
11230 	  -- call validate serials for the to record.
11231 	  inv_reservation_validate_pvt.validate_serials
11232 	    (
11233 	     x_return_status              => l_return_status
11234 	     , p_rsv_action_name          => 'UPDATE'
11235 	     , p_orig_rsv_rec             => l_dummy_rsv_rec
11236 	     , p_to_rsv_rec               => l_to_rsv_rec
11237 	     , p_orig_serial_array        => l_dummy_serial_array
11238 	     , p_to_serial_array          => l_serial_number_table
11239 	     );
11240 
11241 	  IF (l_debug = 1) THEN
11242 	     debug_print('After calling validate serials ' || l_return_status);
11243 	  END IF;
11244 
11245 	  --
11246 	  IF l_return_status = fnd_api.g_ret_sts_error THEN
11247 	     RAISE fnd_api.g_exc_error;
11248 	  END IF;
11249 
11250 
11251 	  --
11252 	  IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11253 	     RAISE fnd_api.g_exc_unexpected_error;
11254 	  END IF;
11255 
11256 	  -- there are some serial reserved. Check if the number of serials
11257 	  -- exceed the reservation quantity.
11258 	  IF (l_total_serials_reserved > l_to_rsv_rec.primary_reservation_quantity) THEN
11259 	     -- we have to unreserve some of the serials
11260 	     -- unreserve the extra serials that are more than the primary_reservation_quantity
11261 	     IF (l_debug = 1) THEN
11262 		debug_print('Total serials more than reservation quantity.');
11263 	     END IF;
11264 
11265 	     l_serials_tobe_unreserved := l_total_serials_reserved - l_to_rsv_rec.primary_reservation_quantity;
11266 
11267 	     FOR i IN 1..l_serials_tobe_unreserved
11268 	       LOOP
11269 	         BEGIN
11270 		    UPDATE mtl_serial_numbers SET reservation_id = NULL,
11271 		      group_mark_id = NULL, line_mark_id = NULL,
11272 		      lot_line_mark_id = NULL WHERE
11273 		      serial_number = l_serial_number_table(i).serial_number AND
11274 		      current_organization_id = l_orig_rsv_tbl(1).organization_id AND
11275 		      inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
11276 		 EXCEPTION
11277 		    WHEN no_data_found THEN
11278 		       IF l_debug=1 THEN
11279 			  debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
11280 		       END IF;
11281 		       fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
11282 		       fnd_msg_pub.ADD;
11283 		       RAISE fnd_api.g_exc_error;
11284 		 END;
11285 		 IF l_debug=1 THEN
11286 		    debug_print('Serial being unreserved. serial number: ' || l_serial_number_table(i).serial_number);
11287 		 END IF;
11288 	       END LOOP;
11289 	       -- update the serial reservation quantity to be the primary
11290 	       -- reservation quantity, as it is in excess.
11291 	       l_to_rsv_rec.serial_reservation_quantity := l_to_rsv_rec.primary_reservation_quantity;
11292 
11293 	   ELSE
11294 	       -- we will have to migrate the serials to the new reservation
11295 	       -- Since it is the same record we are working on. We dont have to
11296 	       -- do anything. Just make sure that the group_mark_id and the
11297 		       -- reservation id are populated.
11298 
11299 	       FOR l_serial_index IN l_serial_number_table.first..l_serial_number_table.last
11300 		 LOOP
11301 		    IF l_debug=1 THEN
11302 		     debug_print('Inside serials not more than res qty');
11303 		     debug_print('reservation id' ||
11304 				 l_orig_rsv_tbl(1).reservation_id);
11305 		     debug_print('serial being processed' ||
11306 				 l_serial_number_table(l_serial_index).serial_number);
11307 		     debug_print('org' ||
11308 				 l_orig_rsv_tbl(1).organization_id);
11309 		      debug_print('item' ||
11310 				 l_orig_rsv_tbl(1).inventory_item_id);
11311 		 END IF;
11312 	           BEGIN
11313 		      UPDATE mtl_serial_numbers SET reservation_id = l_orig_rsv_tbl(1).reservation_id,
11314 			group_mark_id = l_orig_rsv_tbl(1).reservation_id WHERE
11315 			serial_number = l_serial_number_table(l_serial_index).serial_number
11316 			AND current_organization_id = l_orig_rsv_tbl(1).organization_id
11317 			AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
11318 		   EXCEPTION
11319 
11320 		      WHEN no_data_found THEN
11321 			 IF l_debug=1 THEN
11322 			    debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
11323 			 END IF;
11324 			 fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
11325 			 fnd_msg_pub.ADD;
11326 			 RAISE fnd_api.g_exc_error;
11327 		   END;
11328 		   IF l_debug=1 THEN
11329 		      debug_print('Serial being migrated. serial number: ' || l_serial_number_table(l_serial_index).serial_number);
11330 		   END IF;
11331 		 END LOOP;
11332 		 -- update the serial reservation quantity
11333 		 l_to_rsv_rec.serial_reservation_quantity := l_total_serials_reserved;
11334 
11335 	  END IF;
11336        END IF;
11337 
11338      ELSIF (l_original_serial_count > 0 OR l_to_serial_count > 0) THEN
11339 
11340        -- One of them is passed.
11341        -- From record is already validated. There are some extra
11342        -- validations that needs to happen for the to record.
11343 
11344        IF (l_to_serial_count > 0) THEN
11345 	  -- The serial has bo be either reserved to the from record or
11346 	  -- not reserved at all.
11347 	  FOR i IN p_to_serial_number.first..p_to_serial_number.last
11348 	    LOOP
11349 	      BEGIN
11350 		 SELECT reservation_id, group_mark_id INTO
11351 		   l_reservation_id,l_group_mark_id
11352 		   FROM mtl_serial_numbers WHERE
11353 		   serial_number = p_to_serial_number(i).serial_number AND
11354 		   current_organization_id = l_orig_rsv_tbl(1).organization_id AND
11355 		   inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
11356 	      EXCEPTION
11357 		 WHEN no_data_found THEN
11358 		    IF l_debug=1 THEN
11359 		       debug_print('No serials found for this data : ' ||
11360 				   p_to_serial_number(i).serial_number);
11361 		    END IF;
11362 		    fnd_message.set_name('INV', 'INV_INVALID_TO_SERIAL');
11363 		    fnd_msg_pub.ADD;
11364 		    RAISE fnd_api.g_exc_error;
11365 	      END;
11366 
11367 	      IF (l_reservation_id IS NOT NULL AND l_reservation_id <>
11368 		  l_orig_rsv_tbl(1).reservation_id) THEN
11369 		 fnd_message.set_name('INV', 'INV_INVALID_TO_SERIAL');
11370 		 fnd_msg_pub.ADD;
11371 		 RAISE fnd_api.g_exc_error;
11372 	      END IF;
11373 
11374 	      -- If the serial is not reserved then it should not have the
11375 	      -- group mark id stamped. It means it is being used by
11376 	      -- another transaction and cant be reserved.
11377 	      IF (l_reservation_id IS NULL) AND (l_group_mark_id IS NOT NULL) AND
11378 		  (l_group_mark_id <> -1)THEN
11379 		 IF (l_debug = 1) THEN
11380 		    debug_print('Group Mark Id is not null for serial ' || p_to_serial_number(i).serial_number);
11381 		 END IF;
11382 		 fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
11383 		 fnd_msg_pub.ADD;
11384 		 RAISE fnd_api.g_exc_error;
11385 	      END IF;
11386 
11387 	    END LOOP;
11388 
11389        END IF;
11390 
11391        -- unreserve the from and reserve the to and make sure that the
11392        -- total doesnt exceed the primary reservation quantity.
11393        IF (l_original_serial_count > 0) THEN
11394 	  FOR i IN
11395 	    p_original_serial_number.first..p_original_serial_number.last
11396 	    LOOP
11397 	      BEGIN
11398 		 UPDATE mtl_serial_numbers SET reservation_id = NULL,
11399 		   group_mark_id = NULL, line_mark_id = NULL,
11400 		      lot_line_mark_id = NULL  WHERE
11401 		   serial_number = p_original_serial_number(i).serial_number AND
11402 		   current_organization_id = l_orig_rsv_tbl(1).organization_id AND
11403 		   inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
11404 	      EXCEPTION
11405 		 WHEN no_data_found THEN
11406 		    IF l_debug=1 THEN
11407 		       debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
11408 		    END IF;
11409 		    fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
11410 		    fnd_msg_pub.ADD;
11411 		    RAISE fnd_api.g_exc_error;
11412 	      END;
11413 	    END LOOP;
11414        END IF;
11415 
11416        IF (l_to_serial_count > 0) THEN
11417 	  FOR i IN p_to_serial_number.first..p_to_serial_number.last
11418 	    LOOP
11419 	       IF (l_debug = 1) THEN
11420 		  debug_print('reservation id' ||
11421 			      l_orig_rsv_tbl(1).reservation_id);
11422 		  debug_print('serial number' ||
11423 			      p_to_serial_number(i).serial_number);
11424 		  debug_print('organization_id' ||
11425 			      l_orig_rsv_tbl(1).organization_id);
11426 		  debug_print('inventory_item_id = ' || l_orig_rsv_tbl(1).inventory_item_id);
11427 	       END IF;
11428 	      BEGIN
11429 		 UPDATE mtl_serial_numbers SET reservation_id = l_orig_rsv_tbl(1).reservation_id,
11430 		   group_mark_id = l_orig_rsv_tbl(1).reservation_id WHERE
11431 		   serial_number = p_to_serial_number(i).serial_number AND
11432 		   current_organization_id = l_orig_rsv_tbl(1).organization_id AND
11433 		   inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
11434 	      EXCEPTION
11435 		 WHEN no_data_found THEN
11436 		    IF l_debug=1 THEN
11437 		       debug_print('No serials found for reservation
11438 				   record. serial number: ' ||  p_to_serial_number(i).serial_number);
11439 		    END IF;
11440 		    fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
11441 		    fnd_msg_pub.ADD;
11442 		    RAISE fnd_api.g_exc_error;
11443 	      END;
11444 	    END LOOP;
11445        END IF;
11446 
11447 	IF (l_debug = 1) THEN
11448 				   debug_print('reservation id' ||
11449 					       l_orig_rsv_tbl(1).reservation_id);
11450 				   debug_print('organization_id' ||
11451 					       l_orig_rsv_tbl(1).organization_id);
11452 				   debug_print('inventory_item_id = ' || l_orig_rsv_tbl(1).inventory_item_id);
11453 				   END IF;
11454 	BEGIN
11455 	  SELECT COUNT(1) INTO l_total_serials_reserved FROM
11456 	    mtl_serial_numbers  WHERE reservation_id =
11457 	    l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
11458 	    l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
11459        EXCEPTION
11460 	  WHEN no_data_found THEN
11461 	     IF l_debug=1 THEN
11462 		debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
11463 	     END IF;
11464        END;
11465      IF (l_debug = 1) THEN
11466 	debug_print('After counting serials' || l_total_serials_reserved);
11467 	debug_print('Total reservation qty' || l_to_rsv_rec.primary_reservation_quantity);
11468      END IF;
11469 
11470        l_to_rsv_rec.serial_reservation_quantity := l_total_serials_reserved;
11471 
11472        IF (l_total_serials_reserved > l_to_rsv_rec.primary_reservation_quantity) THEN
11473 	  -- we have to unreserve some of the serials
11474 	  -- unreserve the extra serials that are more than the primary_reservation_quantity
11475 	  IF (l_debug = 1) THEN
11476 	     debug_print('Inside relieving serials');
11477 	  END IF;
11478 
11479           BEGIN
11480 	     SELECT inventory_item_id, serial_number bulk collect INTO
11481 	       l_serial_number_table FROM
11482 	       mtl_serial_numbers  WHERE reservation_id =
11483 	       l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
11484 	       l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
11485 	  EXCEPTION
11486 	     WHEN no_data_found THEN
11487 		IF l_debug=1 THEN
11488 		   debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
11489 		END IF;
11490 	  END;
11491 	  l_serials_tobe_unreserved := l_total_serials_reserved - l_to_rsv_rec.primary_reservation_quantity;
11492 
11493 	  FOR i IN 1..l_serials_tobe_unreserved
11494 	    LOOP
11495 	       IF (l_debug = 1) THEN
11496 		  debug_print('Serial being unreserved' || l_serial_number_table(i).serial_number);
11497 	       END IF;
11498 	       BEGIN
11499 		  UPDATE mtl_serial_numbers SET reservation_id = NULL,
11500 		    group_mark_id = NULL, line_mark_id = NULL,
11501 		      lot_line_mark_id = NULL WHERE
11502 		    serial_number = l_serial_number_table(i).serial_number AND
11503 		    current_organization_id = l_orig_rsv_tbl(1).organization_id AND
11504 		    inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
11505 	       EXCEPTION
11506 		  WHEN no_data_found THEN
11507 		     IF l_debug=1 THEN
11508 			debug_print('Inside relieve serials.No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
11509 		     END IF;
11510 		     fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
11511 		     fnd_msg_pub.ADD;
11512 		     RAISE fnd_api.g_exc_error;
11513 	       END;
11514 	    END LOOP;
11515 	    -- update the serial reservation quantity to be the primary
11516 	    -- reservation quantity, as it is in excess.
11517 	    l_to_rsv_rec.serial_reservation_quantity := l_to_rsv_rec.primary_reservation_quantity;
11518        END IF;
11519 
11520     END IF;
11521     /*** End R12  ***/
11522 
11523     -- obtain program and user info
11524     l_date := SYSDATE;
11525 
11526     --
11527     l_user_id        := fnd_global.user_id;
11528     l_login_id       := fnd_global.login_id;
11529 
11530     IF l_login_id = -1 THEN
11531       l_login_id  := fnd_global.conc_login_id;
11532     END IF;
11533 
11534     l_request_id     := fnd_global.conc_request_id;
11535     l_prog_appl_id   := fnd_global.prog_appl_id;
11536     l_program_id     := fnd_global.conc_program_id;
11537     --
11538     -- update the table
11539     IF (l_debug = 1) THEN
11540        debug_print('Calling mtl_reservations_pkg.update_row');
11541     END IF;
11542 
11543     -- Bug 3461990: Reservations API should not update reservations with more
11544     -- than 5 decimal places, since the transaction quantity is being
11545     -- rounded to 5 decimal places.
11546 
11547     l_to_rsv_rec.primary_reservation_quantity :=
11548       round(l_to_rsv_rec.primary_reservation_quantity,5);
11549     l_to_rsv_rec.reservation_quantity :=
11550       round(l_to_rsv_rec.reservation_quantity,5);
11551     l_to_rsv_rec.detailed_quantity :=
11552       round(Nvl(l_to_rsv_rec.detailed_quantity,0),5);
11553 
11554      -- INVCONV BEGIN
11555     IF l_dual_control_flag = 'Y' THEN
11556       l_to_rsv_rec.secondary_reservation_quantity :=
11557         round(l_to_rsv_rec.secondary_reservation_quantity,5);
11558       l_to_rsv_rec.secondary_detailed_quantity :=
11559         round(Nvl(l_to_rsv_rec.secondary_detailed_quantity,0),5);
11560     END IF;
11561     -- INVCONV END
11562 
11563     IF (l_debug = 1) THEN
11564        debug_print(' Update: Before updating record');
11565        debug_print(' After rounding reservation is' || l_orig_rsv_tbl(1).reservation_id);
11566        debug_print(' After rounding reservation qty' || l_to_rsv_rec.reservation_quantity);
11567        debug_print(' After rounding reservation pri qty' || l_to_rsv_rec.primary_reservation_quantity);
11568        debug_print(' After rounding reservation sec qty' || l_to_rsv_rec.secondary_reservation_quantity); -- INVCONV
11569        debug_print(' After rounding detailed quantity' || l_to_rsv_rec.detailed_quantity);
11570        debug_print(' After rounding sec detailed quantity' || l_to_rsv_rec.secondary_detailed_quantity); --INVCONV
11571     END IF;
11572 
11573     -- INVCONV - Upgrade to incorporate secondaries
11574     mtl_reservations_pkg.update_row
11575       (
11576        x_reservation_id             => l_orig_rsv_tbl(1).reservation_id
11577        , x_requirement_date           => l_to_rsv_rec.requirement_date
11578        , x_organization_id            => l_to_rsv_rec.organization_id
11579        , x_inventory_item_id          => l_to_rsv_rec.inventory_item_id
11580        , x_demand_source_type_id      => l_to_rsv_rec.demand_source_type_id
11581        , x_demand_source_name         => l_to_rsv_rec.demand_source_name
11582        , x_demand_source_header_id    => l_to_rsv_rec.demand_source_header_id
11583        , x_demand_source_line_id      => l_to_rsv_rec.demand_source_line_id
11584        , x_demand_source_delivery     => l_to_rsv_rec.demand_source_delivery
11585        , x_primary_uom_code           => l_to_rsv_rec.primary_uom_code
11586        , x_primary_uom_id             => l_to_rsv_rec.primary_uom_id
11587        , x_secondary_uom_code         => l_to_rsv_rec.secondary_uom_code
11588        , x_secondary_uom_id           => l_to_rsv_rec.secondary_uom_id
11589        , x_reservation_uom_code       => l_to_rsv_rec.reservation_uom_code
11590       , x_reservation_uom_id         => l_to_rsv_rec.reservation_uom_id
11591       , x_reservation_quantity       => l_to_rsv_rec.reservation_quantity
11592       , x_primary_reservation_quantity=> l_to_rsv_rec.primary_reservation_quantity
11593       , x_second_reservation_quantity=> l_to_rsv_rec.secondary_reservation_quantity
11594       , x_detailed_quantity          => l_to_rsv_rec.detailed_quantity
11595       , x_secondary_detailed_quantity=> l_to_rsv_rec.secondary_detailed_quantity
11596       , x_autodetail_group_id        => l_to_rsv_rec.autodetail_group_id
11597       , x_external_source_code       => l_to_rsv_rec.external_source_code
11598       , x_external_source_line_id    => l_to_rsv_rec.external_source_line_id
11599       , x_supply_source_type_id      => l_to_rsv_rec.supply_source_type_id
11600       , x_supply_source_header_id    => l_to_rsv_rec.supply_source_header_id
11601       , x_supply_source_line_id      => l_to_rsv_rec.supply_source_line_id
11602       , x_supply_source_line_detail  => l_to_rsv_rec.supply_source_line_detail
11603       , x_supply_source_name         => l_to_rsv_rec.supply_source_name
11604       , x_revision                   => l_to_rsv_rec.revision
11605       , x_subinventory_code          => l_to_rsv_rec.subinventory_code
11606       , x_subinventory_id            => l_to_rsv_rec.subinventory_id
11607       , x_locator_id                 => l_to_rsv_rec.locator_id
11608       , x_lot_number                 => l_to_rsv_rec.lot_number
11609       , x_lot_number_id              => l_to_rsv_rec.lot_number_id
11610       , x_serial_number              => NULL
11611       , x_serial_number_id           => NULL
11612       , x_partial_quantities_allowed => NULL
11613       , x_auto_detailed              => NULL
11614       , x_pick_slip_number           => l_to_rsv_rec.pick_slip_number
11615       , x_lpn_id                     => l_to_rsv_rec.lpn_id
11616       , x_last_update_date           => l_date
11617       , x_last_updated_by            => l_user_id
11618       , x_last_update_login          => l_login_id
11619       , x_request_id                 => l_request_id
11620       , x_program_application_id     => l_prog_appl_id
11621       , x_program_id                 => l_program_id
11622       , x_program_update_date        => l_date
11623       , x_attribute_category         => l_to_rsv_rec.attribute_category
11624       , x_attribute1                 => l_to_rsv_rec.attribute1
11625       , x_attribute2                 => l_to_rsv_rec.attribute2
11626       , x_attribute3                 => l_to_rsv_rec.attribute3
11627       , x_attribute4                 => l_to_rsv_rec.attribute4
11628       , x_attribute5                 => l_to_rsv_rec.attribute5
11629       , x_attribute6                 => l_to_rsv_rec.attribute6
11630       , x_attribute7                 => l_to_rsv_rec.attribute7
11631       , x_attribute8                 => l_to_rsv_rec.attribute8
11632       , x_attribute9                 => l_to_rsv_rec.attribute9
11633       , x_attribute10                => l_to_rsv_rec.attribute10
11634       , x_attribute11                => l_to_rsv_rec.attribute11
11635       , x_attribute12                => l_to_rsv_rec.attribute12
11636       , x_attribute13                => l_to_rsv_rec.attribute13
11637       , x_attribute14                => l_to_rsv_rec.attribute14
11638       , x_attribute15                => l_to_rsv_rec.attribute15
11639       , x_ship_ready_flag            => l_to_rsv_rec.ship_ready_flag
11640       , x_staged_flag                => l_to_rsv_rec.staged_flag
11641       /**** {{ R12 Enhanced reservations code changes }}****/
11642       , x_crossdock_flag             => l_to_rsv_rec.crossdock_flag
11643       , x_crossdock_criteria_id      => l_to_rsv_rec.crossdock_criteria_id
11644       , x_demand_source_line_detail  => l_to_rsv_rec.demand_source_line_detail
11645       , x_serial_reservation_quantity => l_to_rsv_rec.serial_reservation_quantity
11646       , x_supply_receipt_date        => l_to_rsv_rec.supply_receipt_date
11647       , x_demand_ship_date           => l_to_rsv_rec.demand_ship_date
11648       , x_project_id                 => l_to_rsv_rec.project_id
11649       , x_task_id                    => l_to_rsv_rec.task_id
11650       /*** End R12 ***/
11651 
11652       );
11653       /*Bug#12665435 */
11654       IF l_quantity_reserved IS NULL THEN
11655            l_quantity_reserved  := l_to_rsv_rec.primary_reservation_quantity;
11656       END IF;
11657 	  -- bug 16434922
11658 	  l_secondary_quantity_reserved  := l_to_rsv_rec.secondary_reservation_quantity;
11659 
11660     -- for data sync b/w mtl_demand and mtl_reservations
11661     IF (l_debug = 1) THEN
11662        debug_print('Calling inv_rsv_synch.for_update');
11663     END IF;
11664     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);
11665 
11666     IF l_return_status = fnd_api.g_ret_sts_error THEN
11667        RAISE fnd_api.g_exc_error;
11668     END IF;
11669 
11670     --
11671     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11672        RAISE fnd_api.g_exc_unexpected_error;
11673     END IF;
11674 
11675     --
11676     -- Post Update CTO Validation
11677     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
11678        --
11679        IF l_orig_rsv_tbl(1).primary_reservation_quantity > l_to_rsv_rec.primary_reservation_quantity THEN
11680 	  IF (l_debug = 1) THEN
11681 	     debug_print('Calling cto_workflow_api_pk.wf_update_after_inv_unreserv');
11682 	  END IF;
11683 	  cto_workflow_api_pk.wf_update_after_inv_unreserv
11684 	    (
11685 	     p_order_line_id => l_to_rsv_rec.demand_source_line_id
11686 	     , x_return_status => l_return_status
11687 	     , x_msg_count => x_msg_count
11688 	     , x_msg_data => x_msg_data
11689 	     );
11690 	ELSE --Else Condition Added for Bug#2467387.
11691 	  IF (l_debug = 1) THEN
11692 	     debug_print('Calling cto_workflow_api_pk.wf_update_after_inv_reserv');
11693 	  END IF;
11694           cto_workflow_api_pk.wf_update_after_inv_reserv
11695 	    (
11696 	     p_order_line_id => l_to_rsv_rec.demand_source_line_id
11697 	     ,x_return_status => l_return_status
11698 	     ,x_msg_count => x_msg_count
11699 	     ,x_msg_data => x_msg_data
11700 	     );
11701        END IF;
11702        --
11703        IF l_return_status = fnd_api.g_ret_sts_error THEN
11704           RAISE fnd_api.g_exc_error;
11705        END IF;
11706 
11707        --
11708        IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11709           RAISE fnd_api.g_exc_unexpected_error;
11710        END IF;
11711        --
11712     END IF;
11713 
11714     /**** {{ R12 Enhanced reservations code changes. Should be
11715     -- releasing the locks. }} *****/
11716     IF l_lock_obtained THEN
11717        inv_reservation_lock_pvt.release_lock
11718 	 (l_supply_lock_handle);
11719        inv_reservation_lock_pvt.release_lock
11720 	 (l_demand_lock_handle);
11721     END IF;
11722     /*** End R12 ***/
11723 
11724     x_return_status  := l_return_status;
11725     x_quantity_reserved:=l_quantity_reserved;
11726     -- INVCONV BEGIN
11727     IF l_dual_control_flag = 'Y' THEN
11728        x_secondary_quantity_reserved:=l_secondary_quantity_reserved;
11729     END IF;
11730     -- INVCONV END
11731     EXCEPTION
11732        WHEN fnd_api.g_exc_error THEN
11733 	  IF (l_debug = 1) THEN
11734 	     debug_print('Exception');
11735 	  END IF;
11736 	  ROLLBACK TO update_reservation_sa;
11737 	  x_return_status  := fnd_api.g_ret_sts_error;
11738 	  /**** {{ R12 Enhanced reservations code changes. Should be
11739 	  -- releasing the locks. }} *****/
11740 	  IF l_lock_obtained THEN
11741 	     inv_reservation_lock_pvt.release_lock
11742 	       (l_supply_lock_handle);
11743 	     inv_reservation_lock_pvt.release_lock
11744 	       (l_demand_lock_handle);
11745 	  END IF;
11746 	  /*** End R12 ***/
11747 	  --  Get message count and data
11748 	  fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
11749        WHEN fnd_api.g_exc_unexpected_error THEN
11750 	  IF (l_debug = 1) THEN
11751 	     debug_print('Exception Unexpected');
11752 	  END IF;
11753 	  ROLLBACK TO update_reservation_sa;
11754 	  x_return_status  := fnd_api.g_ret_sts_unexp_error;
11755 	  /**** {{ R12 Enhanced reservations code changes. Should be
11756 	  -- releasing the locks. }} *****/
11757 	  IF l_lock_obtained THEN
11758 	     inv_reservation_lock_pvt.release_lock
11759 	       (l_supply_lock_handle);
11760 	     inv_reservation_lock_pvt.release_lock
11761 	       (l_demand_lock_handle);
11762 	  END IF;
11763 	  /*** End R12 ***/
11764 	  --  Get message count and data
11765 	  fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
11766        WHEN OTHERS THEN
11767 	  IF (l_debug = 1) THEN
11768 	     debug_print('Exception Others'|| SQLERRM);
11769 	  END IF;
11770 	  ROLLBACK TO update_reservation_sa;
11771 	  x_return_status  := fnd_api.g_ret_sts_unexp_error;
11772 	  /**** {{ R12 Enhanced reservations code changes. Should be
11773 	  -- releasing the locks. }} *****/
11774 	  IF l_lock_obtained THEN
11775 	     inv_reservation_lock_pvt.release_lock
11776 	       (l_supply_lock_handle);
11777 	     inv_reservation_lock_pvt.release_lock
11778 	       (l_demand_lock_handle);
11779 	  END IF;
11780 	  /*** End R12 ***/
11781 	  IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
11782 	     fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
11783 	  END IF;
11784 
11785 	  --  Get message count and data
11786 	  fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
11787     END update_reservation;
11788 
11789   --
11790   PROCEDURE relieve_reservation(
11791     p_api_version_number        IN     NUMBER
11792   , p_init_msg_lst              IN     VARCHAR2 DEFAULT fnd_api.g_false
11793   , x_return_status             OUT    NOCOPY VARCHAR2
11794   , x_msg_count                 OUT    NOCOPY NUMBER
11795   , x_msg_data                  OUT    NOCOPY VARCHAR2
11796   , p_rsv_rec                   IN     inv_reservation_global.mtl_reservation_rec_type
11797   , p_primary_relieved_quantity IN     NUMBER
11798   , p_secondary_relieved_quantity IN     NUMBER                                     -- INVCONV
11799   , p_relieve_all               IN     VARCHAR2 DEFAULT fnd_api.g_true
11800   , p_original_serial_number    IN     inv_reservation_global.serial_number_tbl_type
11801   , p_validation_flag           IN     VARCHAR2 DEFAULT fnd_api.g_true
11802   , x_primary_relieved_quantity OUT    NOCOPY NUMBER
11803   , x_secondary_relieved_quantity  OUT    NOCOPY NUMBER 			    -- INVCONV
11804   , x_primary_remain_quantity   OUT    NOCOPY NUMBER
11805   , x_secondary_remain_quantity OUT    NOCOPY NUMBER                                -- INVCONV
11806   ) IS
11807     l_api_version_number          CONSTANT NUMBER       := 1.0;
11808     l_api_name                    CONSTANT VARCHAR2(30) := 'Relieve_Reservation';
11809     l_return_status               VARCHAR2(1)           := fnd_api.g_ret_sts_success;
11810     l_tmp_rsv_tbl                 inv_reservation_global.mtl_reservation_tbl_type;
11811     l_tmp_rsv_tbl_count           NUMBER;
11812     l_reservation_id              NUMBER;
11813     l_dummy_rsv_rec               inv_reservation_global.mtl_reservation_rec_type;
11814     l_dummy_serial_array          inv_reservation_global.serial_number_tbl_type;
11815     --
11816     l_orig_item_cache_index       INTEGER                                         := NULL;
11817     l_orig_org_cache_index        INTEGER                                         := NULL;
11818     l_orig_demand_cache_index     INTEGER                                         := NULL;
11819     l_orig_supply_cache_index     INTEGER                                         := NULL;
11820     l_orig_sub_cache_index        INTEGER                                         := NULL;
11821     l_to_item_cache_index         INTEGER                                         := NULL;
11822     l_to_org_cache_index          INTEGER                                         := NULL;
11823     l_to_demand_cache_index       INTEGER                                         := NULL;
11824     l_to_supply_cache_index       INTEGER                                         := NULL;
11825     l_to_sub_cache_index          INTEGER                                         := NULL;
11826     l_tree_id                     NUMBER;
11827     l_error_code                  NUMBER;
11828     l_primary_relieved_quantity   NUMBER;
11829     l_secondary_relieved_quantity NUMBER;                                         -- INVCONV
11830     l_primary_remain_quantity     NUMBER;
11831     l_secondary_remain_quantity   NUMBER;                                         -- INVCONV
11832     l_date                        DATE;
11833     l_user_id                     NUMBER;
11834     l_request_id                  NUMBER;
11835     l_login_id                    NUMBER;
11836     l_prog_appl_id                NUMBER;
11837     l_program_id                  NUMBER;
11838     l_qty_changed                 NUMBER;
11839     l_secondary_qty_changed       NUMBER;					 -- INVCONV
11840     l_debug number;
11841     l_lot_divisible_flag          VARCHAR2(1);                                   -- INVCONV
11842     l_dual_control_flag		  VARCHAR2(1)			:= 'N';          -- INVCONV
11843     /*** {{ R12 Enhanced reservations code changes ***/
11844     l_count                       NUMBER;
11845     l_count_to_unrsv_serials      NUMBER :=0;
11846     l_serial_number_table         inv_reservation_global.serial_number_tbl_type;
11847     l_rsv_rec                     inv_reservation_global.mtl_reservation_rec_type;
11848     /*** End R12 }} ***/
11849   BEGIN
11850     --  Standard call to check for call compatibility
11851     IF NOT fnd_api.compatible_api_call(l_api_version_number, p_api_version_number, l_api_name, g_pkg_name) THEN
11852       RAISE fnd_api.g_exc_unexpected_error;
11853     END IF;
11854 
11855     --
11856     --  Initialize message list.
11857     IF fnd_api.to_boolean(p_init_msg_lst) THEN
11858       fnd_msg_pub.initialize;
11859     END IF;
11860 
11861     --
11862     SAVEPOINT relieve_reservation_sa;
11863     --
11864      -- Use cache to get value for l_debug
11865      IF g_is_pickrelease_set IS NULL THEN
11866         g_is_pickrelease_set := 2;
11867         IF INV_CACHE.is_pickrelease THEN
11868            g_is_pickrelease_set := 1;
11869         END IF;
11870      END IF;
11871      IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
11872         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
11873      END IF;
11874 
11875      l_debug := g_debug;
11876 
11877     /**** {{ R12 Enhanced reservations code changes ****/
11878     -- Set the original columns to g_miss_xxx as the user should not be
11879     -- setting these values. Do not query by them
11880     l_rsv_rec := p_rsv_rec;
11881 
11882     l_rsv_rec.orig_supply_source_type_id := fnd_api.g_miss_num;
11883     l_rsv_rec.orig_supply_source_header_id := fnd_api.g_miss_num;
11884     l_rsv_rec.orig_supply_source_line_id := fnd_api.g_miss_num;
11885     l_rsv_rec.orig_supply_source_line_detail := fnd_api.g_miss_num;
11886     l_rsv_rec.orig_demand_source_type_id := fnd_api.g_miss_num;
11887     l_rsv_rec.orig_demand_source_header_id := fnd_api.g_miss_num;
11888     l_rsv_rec.orig_demand_source_line_id := fnd_api.g_miss_num;
11889     l_rsv_rec.orig_demand_source_line_detail := fnd_api.g_miss_num;
11890 
11891     /*** End R12 }} ***/
11892 
11893      IF (l_debug = 1) THEN
11894         debug_print('calling query_reservation');
11895      END IF;
11896     query_reservation(
11897       p_api_version_number         => 1.0
11898     , p_init_msg_lst               => fnd_api.g_false
11899     , x_return_status              => l_return_status
11900     , x_msg_count                  => x_msg_count
11901     , x_msg_data                   => x_msg_data
11902     , p_query_input                => l_rsv_rec         /*** {{ R12 Enhanced reservations code changes ***/
11903     , p_lock_records               => fnd_api.g_true
11904     , x_mtl_reservation_tbl        => l_tmp_rsv_tbl
11905     , x_mtl_reservation_tbl_count  => l_tmp_rsv_tbl_count
11906     , x_error_code                 => l_error_code
11907     );
11908 
11909     --
11910     IF l_return_status = fnd_api.g_ret_sts_error THEN
11911      	IF (l_debug = 1) THEN
11912         	debug_print( 'error from query reservation');
11913      	END IF;
11914       RAISE fnd_api.g_exc_error;
11915     END IF;
11916 
11917     --
11918     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11919       IF (l_debug = 1) THEN
11920          debug_print('error from query reservation');
11921       END IF;
11922       RAISE fnd_api.g_exc_unexpected_error;
11923     END IF;
11924 
11925     --
11926     IF l_tmp_rsv_tbl_count = 0 THEN
11927       IF (l_debug = 1) THEN
11928          debug_print('reservation not found ');
11929       END IF;
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     --
11936     IF l_tmp_rsv_tbl_count > 1 THEN
11937       IF (l_debug = 1) THEN
11938          debug_print('found more than one row reservation');
11939       END IF;
11940       fnd_message.set_name('INV', 'INV-RELIEVE MORE THAN ONE ROW');
11941       fnd_msg_pub.ADD;
11942       RAISE fnd_api.g_exc_error;
11943     END IF;
11944 
11945     --
11946     l_reservation_id             := l_tmp_rsv_tbl(1).reservation_id;
11947     IF (l_debug = 1) THEN
11948        debug_print('reservation id is ' || l_reservation_id);
11949     END IF;
11950 
11951     IF p_validation_flag = fnd_api.g_true THEN
11952          -- validation is needed to get revision, lot,
11953          --serial control information
11954          -- for creating the quantity tree
11955 
11956        -- added for crossdock reservation, pass the to reservation record
11957        -- with values instead of dummy record with the remain quantity
11958        -- populated to the record.
11959        IF ((l_tmp_rsv_tbl(1).crossdock_criteria_id is not null) and
11960 	   (l_tmp_rsv_tbl(1).crossdock_criteria_id <> fnd_api.g_miss_num)) THEN
11961 	  IF (l_debug = 1) THEN
11962 	     debug_print('assigning cross dock to record');
11963 	  END IF;
11964 
11965 	  IF (p_relieve_all = fnd_api.g_false) THEN
11966 	     IF (l_debug = 1) THEN
11967 		debug_print('relieve all is false');
11968 	     END IF;
11969 	     l_primary_remain_quantity :=
11970 	       l_tmp_rsv_tbl(1).primary_reservation_quantity -
11971 	       Nvl(p_primary_relieved_quantity,0);
11972 	   ELSE
11973 	     IF (l_debug = 1) THEN
11974 		debug_print('relieve all is true');
11975 	     END IF;
11976 	     l_primary_remain_quantity := 0;
11977 	  END IF;
11978 
11979 	  l_dummy_rsv_rec := l_tmp_rsv_tbl(1);
11980 	  IF (l_debug = 1) THEN
11981 	     debug_print('from rec primary reservation qty' || l_tmp_rsv_tbl(1).primary_reservation_quantity);
11982 	     debug_print('to rec primary reservation qty' || l_primary_remain_quantity);
11983 	  END IF;
11984           l_dummy_rsv_rec.primary_reservation_quantity := l_primary_remain_quantity;
11985 	  l_dummy_rsv_rec.reservation_quantity := null;
11986 	  --l_dummy_rsv_rec.secondary_reservation_quantity := l_secondary_remain_quantity;
11987        END IF;
11988 
11989        IF (l_debug = 1) THEN
11990           debug_print('calling validate_input_parameters');
11991        END IF;
11992 
11993       inv_reservation_validate_pvt.validate_input_parameters(
11994         x_return_status              => l_return_status
11995       , p_orig_rsv_rec               => l_tmp_rsv_tbl(1)
11996       , p_to_rsv_rec                 => l_dummy_rsv_rec
11997       , p_orig_serial_array          => p_original_serial_number
11998       , p_to_serial_array            => l_dummy_serial_array
11999       , p_rsv_action_name            => 'RELIEVE'
12000       , x_orig_item_cache_index      => l_orig_item_cache_index
12001       , x_orig_org_cache_index       => l_orig_org_cache_index
12002       , x_orig_demand_cache_index    => l_orig_demand_cache_index
12003       , x_orig_supply_cache_index    => l_orig_supply_cache_index
12004       , x_orig_sub_cache_index       => l_orig_sub_cache_index
12005       , x_to_item_cache_index        => l_to_item_cache_index
12006       , x_to_org_cache_index         => l_to_org_cache_index
12007       , x_to_demand_cache_index      => l_to_demand_cache_index
12008       , x_to_supply_cache_index      => l_to_supply_cache_index
12009       , x_to_sub_cache_index         => l_to_sub_cache_index
12010       );
12011 
12012       --
12013       IF l_return_status = fnd_api.g_ret_sts_error THEN
12014          IF (l_debug = 1) THEN
12015             debug_print('Error in validate_input_parameters');
12016          END IF;
12017         RAISE fnd_api.g_exc_error;
12018       END IF;
12019 
12020       --
12021       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12022          IF (l_debug = 1) THEN
12023             debug_print('Unexpected error in validate_input_parameters');
12024          END IF;
12025         RAISE fnd_api.g_exc_unexpected_error;
12026       END IF;
12027 
12028       --
12029        -- INVCONV BEGIN
12030       IF is_lot_divisible(l_orig_item_cache_index) THEN
12031         l_lot_divisible_flag := 'Y';
12032       END IF;
12033       -- INVCONV END
12034       --
12035       -- check quantity
12036       IF  p_primary_relieved_quantity IS NULL
12037           AND p_relieve_all <> fnd_api.g_true THEN
12038         IF (l_debug = 1) THEN
12039            debug_print('relieve_quantity_not_specified');
12040         END IF;
12041         fnd_message.set_name('INV', 'RELIEVE_QUANTITY_NOT_SPECIFIED');
12042         fnd_msg_pub.ADD;
12043         RAISE fnd_api.g_exc_error;
12044       END IF;
12045 
12046       IF p_primary_relieved_quantity > l_tmp_rsv_tbl(1).primary_reservation_quantity THEN
12047         IF (l_debug = 1) THEN
12048            debug_print('relieve_more_than_reserved');
12049         END IF;
12050         fnd_message.set_name('INV', 'RELIEVE_MORE_THAN_RESERVED');
12051         fnd_msg_pub.ADD;
12052         RAISE fnd_api.g_exc_unexpected_error;
12053       END IF;
12054 
12055       IF is_dual_control(l_orig_item_cache_index) THEN
12056         l_dual_control_flag := 'Y';
12057       END IF;
12058 
12059       IF p_relieve_all = fnd_api.g_true THEN
12060         l_primary_relieved_quantity  := l_tmp_rsv_tbl(1).primary_reservation_quantity;
12061         l_primary_remain_quantity    := 0;
12062         -- INVCONV BEGIN
12063         IF l_dual_control_flag = 'Y' THEN
12064           l_secondary_relieved_quantity  := l_tmp_rsv_tbl(1).secondary_reservation_quantity;
12065           l_secondary_remain_quantity    := 0;
12066         END IF;
12067         -- INVCONV END
12068       ELSE
12069         l_primary_relieved_quantity  := p_primary_relieved_quantity;
12070         l_primary_remain_quantity    := l_tmp_rsv_tbl(1).primary_reservation_quantity - l_primary_relieved_quantity;
12071         -- INVCONV BEGIN
12072         IF l_dual_control_flag = 'Y' THEN
12073           l_secondary_relieved_quantity := p_secondary_relieved_quantity;
12074           l_secondary_remain_quantity   := l_tmp_rsv_tbl(1).secondary_reservation_quantity - l_secondary_relieved_quantity;
12075         END IF;
12076         -- INVCONV END
12077       END IF;
12078 
12079       IF l_tmp_rsv_tbl(1).supply_source_type_id = inv_reservation_global.g_source_type_inv THEN
12080            -- call quantity processor to
12081            -- modify the tree so that the quantity tree
12082            -- reflect the deletion
12083         IF (l_debug = 1) THEN
12084            debug_print('calling inv_quantity_tree_pvt.create_tree');
12085         END IF;
12086         inv_quantity_tree_pvt.create_tree(
12087           p_api_version_number         => 1.0
12088         , p_init_msg_lst               => fnd_api.g_true
12089         , x_return_status              => l_return_status
12090         , x_msg_count                  => x_msg_count
12091         , x_msg_data                   => x_msg_data
12092         , p_organization_id            => l_tmp_rsv_tbl(1).organization_id
12093         , p_inventory_item_id          => l_tmp_rsv_tbl(1).inventory_item_id
12094         , p_tree_mode                  => inv_quantity_tree_pvt.g_reservation_mode
12095         , p_is_revision_control        => is_revision_control(l_orig_item_cache_index)
12096         , p_is_lot_control             => is_lot_control(l_orig_item_cache_index)
12097         , p_is_serial_control          => is_serial_control(l_orig_item_cache_index)
12098         , p_asset_sub_only             => FALSE
12099         , p_include_suggestion         => TRUE
12100         , p_demand_source_type_id      => l_tmp_rsv_tbl(1).demand_source_type_id
12101         , p_demand_source_header_id    => l_tmp_rsv_tbl(1).demand_source_header_id
12102         , p_demand_source_line_id      => l_tmp_rsv_tbl(1).demand_source_line_id
12103         , p_demand_source_name         => l_tmp_rsv_tbl(1).demand_source_name
12104         , p_demand_source_delivery     => l_tmp_rsv_tbl(1).demand_source_delivery
12105         , p_lot_expiration_date        => NULL
12106         , x_tree_id                    => l_tree_id
12107         );
12108 
12109         IF l_return_status = fnd_api.g_ret_sts_error THEN
12110          IF (l_debug = 1) THEN
12111             debug_print('error calling inv_quantity_tree_pvt.create_tree');
12112          END IF;
12113           RAISE fnd_api.g_exc_error;
12114         END IF;
12115 
12116         --
12117         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12118             IF (l_debug = 1) THEN
12119                debug_print('unexpected error calling inv_quantity_tree_pvt.create_tree');
12120             END IF;
12121           RAISE fnd_api.g_exc_unexpected_error;
12122         END IF;
12123 
12124         IF (l_debug = 1) THEN
12125            debug_print('calling modify_tree_crt_del_rel');
12126         END IF;
12127         -- INVCONV - Upgrade call to incorporate secondaries
12128         modify_tree_crt_del_rel(
12129           x_return_status              => l_return_status
12130         , p_tree_id                    => l_tree_id
12131         , p_revision                   => l_tmp_rsv_tbl(1).revision
12132         , p_lot_number                 => l_tmp_rsv_tbl(1).lot_number
12133         , p_subinventory_code          => l_tmp_rsv_tbl(1).subinventory_code
12134         , p_locator_id                 => l_tmp_rsv_tbl(1).locator_id
12135         , p_lpn_id                     => l_tmp_rsv_tbl(1).lpn_id
12136         , p_primary_reservation_quantity=> l_tmp_rsv_tbl(1).primary_reservation_quantity
12137         , p_second_reservation_quantity=> l_tmp_rsv_tbl(1).secondary_reservation_quantity
12138         , p_detailed_quantity          => l_tmp_rsv_tbl(1).detailed_quantity
12139         , p_secondary_detailed_quantity=> l_tmp_rsv_tbl(1).secondary_detailed_quantity
12140         , p_relieve_quantity           => l_primary_relieved_quantity
12141         , p_secondary_relieve_quantity => l_secondary_relieved_quantity
12142         , p_partial_reservation_flag   => fnd_api.g_false
12143         , p_force_reservation_flag     => fnd_api.g_false
12144         , p_lot_divisible_flag         => l_lot_divisible_flag      -- INVCONV
12145         , p_action                     => 'RELIEVE'
12146         , x_qty_changed                => l_qty_changed
12147         , x_secondary_qty_changed      => l_secondary_qty_changed
12148 	, p_organization_id            => l_rsv_rec.organization_id
12149         , p_demand_source_line_id      => l_tmp_rsv_tbl(1).demand_source_line_id
12150 	, p_demand_source_type_id      => l_tmp_rsv_tbl(1).demand_source_type_id
12151         );
12152 
12153         --
12154         IF l_return_status = fnd_api.g_ret_sts_error THEN
12155           IF (l_debug = 1) THEN
12156              debug_print('error calling modify_tree_crt_del_rel');
12157           END IF;
12158           RAISE fnd_api.g_exc_error;
12159         END IF;
12160 
12161         --
12162         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12163           IF (l_debug = 1) THEN
12164              debug_print('unexpected error calling modify_tree_crt_del_rel');
12165           END IF;
12166           RAISE fnd_api.g_exc_unexpected_error;
12167         END IF;
12168       --
12169       END IF;
12170     END IF;
12171 
12172      -- delete the reservation from the db table
12173     IF l_primary_remain_quantity = 0 THEN
12174 
12175        --Bug 3830160: Added a call to delete the demand if the entire
12176        --reservation is relieved. Code to delete mtl_demand.
12177 
12178        IF (l_debug = 1) THEN
12179           debug_print('calling inv_rsv_synch.for_delete');
12180        END IF;
12181 
12182        inv_rsv_synch.for_delete
12183          (
12184           p_reservation_id             => l_reservation_id
12185           , x_return_status              => l_return_status
12186           , x_msg_count                  => x_msg_count
12187           , x_msg_data                   => x_msg_data
12188           );
12189 
12190        IF l_return_status = fnd_api.g_ret_sts_error THEN
12191           IF (l_debug = 1) THEN
12192              debug_print('error calling inv_rsv_synch.for_delete');
12193           END IF;
12194            RAISE fnd_api.g_exc_error;
12195        END IF;
12196 
12197        IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12198           IF (l_debug = 1) THEN
12199              debug_print('unexpected error calling inv_rsv_synch.for_delete');
12200           END IF;
12201           RAISE fnd_api.g_exc_unexpected_error;
12202        END IF;
12203        -- End changes for bug 3827307
12204 
12205        /*** {{ R12 Enhanced reservations code changes ***/
12206        -- update the mtl_serial_numbers, null out the reservation_id
12207        -- and group_mark_id of the reserved serials before delete the
12208        -- reservation. There is no need to update the serial_reservation_quantity
12209        -- because the reservation record will be deleted.
12210        BEGIN
12211          update mtl_serial_numbers
12212          set    reservation_id = NULL,
12213 	   group_mark_id = NULL,
12214 	   line_mark_id = NULL,
12215 	   lot_line_mark_id = NULL
12216          where  reservation_id = l_reservation_id;
12217        EXCEPTION
12218          WHEN NO_DATA_FOUND THEN
12219               IF (l_debug = 1) THEN
12220                  debug_print('No serial numbers stamp with reservation_id ' || l_reservation_id);
12221               END IF;
12222        END;
12223        /*** End R12 }} ***/
12224 
12225        IF (l_debug = 1) THEN
12226           debug_print('calling mtl_reservations_pkg.delete_row');
12227        END IF;
12228        mtl_reservations_pkg.delete_row(x_reservation_id => l_reservation_id);
12229        NULL;
12230 
12231      ELSE
12232 
12233        --Bug 3830160: Moved the relieve demand code inside this block
12234        --so that 'for_relieve' will be called only if there are some pending
12235        --demand and if we are not relieving the entire reservation.
12236 
12237         IF (l_debug = 1) THEN
12238            debug_print('calling inv_rsv_synch.for_relieve');
12239         END IF;
12240 
12241         inv_rsv_synch.for_relieve(
12242           p_reservation_id             => l_reservation_id
12243         , p_primary_relieved_quantity  => l_primary_relieved_quantity
12244         , x_return_status              => l_return_status
12245         , x_msg_count                  => x_msg_count
12246         , x_msg_data                   => x_msg_data
12247         );
12248 
12249         IF l_return_status = fnd_api.g_ret_sts_error THEN
12250           IF (l_debug = 1) THEN
12251              debug_print('error calling inv_rsv_synch.for_relieve');
12252           END IF;
12253           RAISE fnd_api.g_exc_error;
12254         END IF;
12255 
12256         --
12257         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12258            IF (l_debug = 1) THEN
12259               debug_print('unexpected error calling inv_rsv_synch.for_relieve');
12260            END IF;
12261           RAISE fnd_api.g_exc_unexpected_error;
12262         END IF;
12263 
12264         -- End changes Bug3830160.
12265 
12266       l_tmp_rsv_tbl(1).primary_reservation_quantity  := l_tmp_rsv_tbl(1).primary_reservation_quantity - l_primary_relieved_quantity;
12267 
12268       -- INVCONV BEGIN
12269       IF l_dual_control_flag = 'Y' THEN
12270         l_tmp_rsv_tbl(1).secondary_reservation_quantity := l_tmp_rsv_tbl(1).secondary_reservation_quantity - l_secondary_relieved_quantity;
12271       END IF;
12272       -- INVCONV END
12273 
12274       --Bug 2116332 - convert_quantity only updates reservation quantity
12275       -- if that field is NULL.  So, we have to make reservation quantity
12276       -- NULL if we want to populate it from primary_reservation_quantity
12277       l_tmp_rsv_tbl(1).reservation_quantity          := NULL;
12278      	IF (l_debug = 1) THEN
12279         	debug_print('calling convert_quantity');
12280      	END IF;
12281       convert_quantity(x_return_status => l_return_status, px_rsv_rec => l_tmp_rsv_tbl(1));
12282 
12283       --
12284       IF l_return_status = fnd_api.g_ret_sts_error THEN
12285         RAISE fnd_api.g_exc_error;
12286       END IF;
12287 
12288       --
12289       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12290         RAISE fnd_api.g_exc_unexpected_error;
12291       END IF;
12292 
12293       -- obtain program and user info
12294       l_date := SYSDATE;
12295 
12296       --
12297       l_user_id                                      := fnd_global.user_id;
12298       l_login_id                                     := fnd_global.login_id;
12299 
12300       IF l_login_id = -1 THEN
12301         l_login_id  := fnd_global.conc_login_id;
12302       END IF;
12303 
12304       l_request_id                                   := fnd_global.conc_request_id;
12305       l_prog_appl_id                                 := fnd_global.prog_appl_id;
12306       l_program_id                                   := fnd_global.conc_program_id;
12307 
12308       --Adding an extra check to make sure that relieve reservations does not
12309       -- update the record to a negative quantity.
12310       IF (l_debug = 1) THEN
12311         debug_print('Primary_reservation_qty before inserting (relieve)= '
12312 		                || To_char(l_tmp_rsv_tbl(1).primary_reservation_quantity) );
12313         debug_print('Secondary_reservation_qty before inserting (relieve)= '
12314                                 || To_char(l_tmp_rsv_tbl(1).secondary_reservation_quantity) );
12315         debug_print('Reservation_qty before inserting (relieve)= '
12316 		                || To_char(l_tmp_rsv_tbl(1).reservation_quantity) );
12317       END IF;
12318 
12319       IF (  (NVL(l_tmp_rsv_tbl(1).reservation_quantity,0) < 0) OR
12320 	          (NVL(l_tmp_rsv_tbl(1).primary_reservation_quantity,0) < 0) ) THEN
12321         fnd_message.set_name('INV', 'INV-INVALID RESERVATION QTY');
12322         fnd_msg_pub.ADD;
12323         RAISE fnd_api.g_exc_error;
12324       END IF;
12325 
12326       -- INVCONV BEGIN
12327       IF (NVL(l_tmp_rsv_tbl(1).secondary_reservation_quantity,0) < 0) THEN
12328         fnd_message.set_name('INV', 'INV-INVALID NEGATIVE SECONDARY');
12329         fnd_msg_pub.ADD;
12330         RAISE fnd_api.g_exc_error;
12331       END IF;
12332       -- INVCONV END
12333 
12334       /*** {{ R12 Enhanced reservations code changes ***/
12335       -- if serial record is not empty, then unmark the group_mark_id and
12336       -- reservation_id in mtl_serial_numbers of the serial numbers pass in the record.
12337       -- also update the serial_reservation_quantity in the mtl_reservations.
12338       -- if serial record is empty, need to get the serials with reservation_id and
12339       -- unmark the serial until the serial_reservation_quantity = primary_reservation_quantity
12340       IF (p_original_serial_number.COUNT > 0) THEN
12341 	 l_count := 0;
12342 	 IF (l_debug = 1) THEN
12343 	    debug_print('Inside relieve serial numbers');
12344 	 END IF;
12345 	 FOR i in 1..p_original_serial_number.COUNT LOOP
12346 	    IF (l_debug = 1) THEN
12347 	       debug_print('serial number' ||
12348 			   p_original_serial_number(i).serial_number);
12349 	       debug_print('item ' || l_tmp_rsv_tbl(1).inventory_item_id);
12350 	    END IF;
12351              BEGIN
12352                 UPDATE mtl_serial_numbers
12353 		  SET reservation_id = NULL,
12354 		  group_mark_id = NULL, line_mark_id = NULL,
12355 		  lot_line_mark_id = NULL
12356 		  WHERE  serial_number = p_original_serial_number(i).serial_number
12357 		  AND    inventory_item_id =
12358 		  l_tmp_rsv_tbl(1).inventory_item_id
12359 		  AND current_organization_id = l_tmp_rsv_tbl(1).organization_id;
12360              EXCEPTION
12361                 WHEN no_data_found THEN
12362                    IF (l_debug = 1) THEN
12363 		      debug_print('No serials found for serial number ' || p_original_serial_number(i).serial_number);
12364                    END IF;
12365              END;
12366              l_count := l_count + 1;
12367 	     IF (l_debug = 1) THEN
12368 		debug_print('relieved serial count: ' || l_count);
12369 	     END IF;
12370 	 END LOOP;
12371 
12372 	 IF (l_tmp_rsv_tbl(1).primary_reservation_quantity >=
12373 	     Nvl(l_tmp_rsv_tbl(1).serial_reservation_quantity,0) - l_count) THEN
12374 	    l_tmp_rsv_tbl(1).serial_reservation_quantity :=
12375 	      l_tmp_rsv_tbl(1).serial_reservation_quantity - l_count;
12376 	    IF (l_debug = 1) THEN
12377 	       debug_print('Inside rsv > serial count: ' ||
12378 			   l_tmp_rsv_tbl(1).primary_reservation_quantity
12379 			   || ':' ||
12380 			   l_tmp_rsv_tbl(1).serial_reservation_quantity ||
12381 			   ':' || l_count);
12382 	    END IF;
12383           ELSE
12384               -- need to unreserved more serials until the serial_reservation_quantity
12385 	     -- is less than or equal to primary_reservation_quantity
12386 	     IF (l_debug = 1) THEN
12387 		debug_print('Inside serial count > rsv : ' ||
12388 			    l_tmp_rsv_tbl(1).primary_reservation_quantity
12389 			    || ':' ||
12390 			    l_tmp_rsv_tbl(1).serial_reservation_quantity ||
12391 			    ':' || l_count);
12392 	     END IF;
12393               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;
12394 
12395 	      IF (l_debug = 1) THEN
12396 		 debug_print('total serials to be unreserved' ||
12397 			     l_count_to_unrsv_serials);
12398 		 debug_print('reservation id ' || l_reservation_id);
12399 	      END IF;
12400 
12401               BEGIN
12402                  SELECT inventory_item_id,serial_number BULK COLLECT
12403                  INTO   l_serial_number_table
12404                  FROM   mtl_serial_numbers
12405                  WHERE  reservation_id = l_reservation_id;
12406               EXCEPTION
12407                  WHEN no_data_found THEN
12408                     IF (l_debug = 1) THEN
12409                         debug_print('No serials found for reservation record. id = ' || l_reservation_id);
12410                     END IF;
12411                     fnd_message.set_name('INV', 'INV_SR_EXCEED_RSV_QTY');
12412                     fnd_msg_pub.add;
12413                     RAISE fnd_api.g_exc_error;
12414               END;
12415 
12416 	      IF (l_debug = 1) THEN
12417 		 debug_print('After query serials');
12418 		 debug_print('l_serial_number_table.COUNT' ||
12419 			     l_serial_number_table.COUNT);
12420 
12421 	      END IF;
12422 
12423               IF (l_serial_number_table.COUNT < l_count_to_unrsv_serials) THEN
12424                   IF (l_debug = 1) THEN
12425                       debug_print('Not enough serials to unreserved for reservation record. id = ' || l_reservation_id);
12426                   END IF;
12427                   fnd_message.set_name('INV', 'INV_SR_EXCEED_RSV_QTY');
12428                   fnd_msg_pub.add;
12429                   RAISE fnd_api.g_exc_error;
12430               END IF;
12431 
12432               FOR i in 1..l_count_to_unrsv_serials LOOP
12433                   BEGIN
12434 		     IF (l_debug = 1) THEN
12435 			debug_print('inside relieve. serial ' ||
12436 				    l_serial_number_table(i).serial_number);
12437 			debug_print('inside relieve. item ' || l_serial_number_table(i).serial_number);
12438 		     END IF;
12439 		     UPDATE mtl_serial_numbers
12440 		       SET reservation_id = NULL,
12441 		       group_mark_id = NULL, line_mark_id = NULL,
12442 		       lot_line_mark_id = NULL
12443 		       WHERE  serial_number = l_serial_number_table(i).serial_number
12444 		       AND inventory_item_id =
12445 		       l_serial_number_table(i).inventory_item_id AND
12446 		       current_organization_id = l_tmp_rsv_tbl(1).organization_id;
12447                   EXCEPTION
12448                      WHEN no_data_found THEN
12449                         IF (l_debug = 1) THEN
12450                             debug_print('No serial found for serial number ' || l_serial_number_table(i).serial_number);
12451                         END IF;
12452                   END;
12453                   l_count := l_count + 1;
12454 		  IF (l_debug = 1) THEN
12455 		     debug_print('l_count' || l_count);
12456 		  END IF;
12457               END LOOP;
12458 
12459               l_tmp_rsv_tbl(1).serial_reservation_quantity :=
12460 		l_tmp_rsv_tbl(1).serial_reservation_quantity - l_count;
12461 	      IF (l_debug = 1) THEN
12462 		 debug_print('final serial count' || l_tmp_rsv_tbl(1).serial_reservation_quantity);
12463 	      END IF;
12464           END IF;
12465       ELSE -- p_original_serial_number is null
12466           IF (l_tmp_rsv_tbl(1).primary_reservation_quantity <
12467                      Nvl(l_tmp_rsv_tbl(1).serial_reservation_quantity,0)) THEN
12468 
12469               -- need to unreserved more serials until the serial_reservation_quantity
12470               -- is less than or equal to primary_reservation_quantity
12471               l_count_to_unrsv_serials :=
12472 		Nvl(l_tmp_rsv_tbl(1).serial_reservation_quantity,0) - l_tmp_rsv_tbl(1).primary_reservation_quantity;
12473 	      IF (l_debug = 1) THEN
12474 		 debug_print('Inside p_original is null. total serials to be unreserved' ||
12475 			     l_count_to_unrsv_serials);
12476 		 debug_print('serial qty ' ||
12477 			     l_tmp_rsv_tbl(1).serial_reservation_quantity);
12478 		 debug_print('rsv qty ' || l_tmp_rsv_tbl(1).primary_reservation_quantity);
12479 	      END IF;
12480 
12481               BEGIN
12482                  SELECT inventory_item_id, serial_number BULK COLLECT
12483                  INTO   l_serial_number_table
12484                  FROM   mtl_serial_numbers
12485                  WHERE  reservation_id = l_reservation_id;
12486               EXCEPTION
12487                  WHEN no_data_found THEN
12488                     IF (l_debug = 1) THEN
12489                         debug_print('No serial found for reservation record. id = ' || l_reservation_id);
12490                     END IF;
12491                     fnd_message.set_name('INV', 'INV_SR_EXCEED_RSV_QTY');
12492                     fnd_msg_pub.add;
12493                     RAISE fnd_api.g_exc_error;
12494               END;
12495 
12496               IF (l_serial_number_table.COUNT < l_count_to_unrsv_serials) THEN
12497                   IF (l_debug = 1) THEN
12498                       debug_print('Not enough serials to unreserved for reservation record. id = ' || l_reservation_id);
12499                   END IF;
12500                   fnd_message.set_name('INV', 'INV_SR_EXCEED_RSV_QTY');
12501                   fnd_msg_pub.add;
12502                   RAISE fnd_api.g_exc_error;
12503               END IF;
12504 
12505               FOR i in 1..l_count_to_unrsv_serials LOOP
12506                   BEGIN
12507                      UPDATE mtl_serial_numbers
12508 		       SET reservation_id = NULL,
12509 		       group_mark_id = NULL, line_mark_id = NULL,
12510 		       lot_line_mark_id = NULL
12511 		       WHERE serial_number = l_serial_number_table(i).serial_number
12512 		       AND inventory_item_id = l_serial_number_table(i).inventory_item_id;
12513                   EXCEPTION
12514                      WHEN no_data_found THEN
12515                         IF (l_debug = 1) THEN
12516 			   debug_print('No serials found for serial number ' || l_serial_number_table(i).serial_number);
12517                         END IF;
12518                   END;
12519               END LOOP;
12520 
12521               l_tmp_rsv_tbl(1).serial_reservation_quantity := l_tmp_rsv_tbl(1).serial_reservation_quantity
12522                                                             - l_count_to_unrsv_serials;
12523           END IF;
12524       END IF;
12525       /*** End R12 }} ***/
12526 
12527          -- update the quantity in a quick and dirty way
12528       IF (l_debug = 1) THEN
12529          debug_print('update reservation');
12530       END IF;
12531 
12532       -- INVCONV - Incorporate secondaries
12533       UPDATE mtl_reservations
12534 	SET primary_reservation_quantity = l_tmp_rsv_tbl(1).primary_reservation_quantity
12535 	, secondary_reservation_quantity = l_tmp_rsv_tbl(1).secondary_reservation_quantity
12536 	, reservation_quantity = l_tmp_rsv_tbl(1).reservation_quantity
12537 	, last_update_date = l_date
12538 	, last_updated_by = l_user_id
12539 	, last_update_login = l_login_id
12540 	, request_id = l_request_id
12541 	, program_application_id = l_prog_appl_id
12542 	, program_id = l_program_id
12543 	, program_update_date = l_date
12544 	/*** {{ R12 Enhanced reservations code changes ***/
12545 	, serial_reservation_quantity =
12546 	l_tmp_rsv_tbl(1).serial_reservation_quantity
12547 	/*** End R12 }} ***/
12548 	WHERE reservation_id = l_reservation_id;
12549     END IF;
12550 
12551     --
12552     x_primary_relieved_quantity  := l_primary_relieved_quantity;
12553     x_primary_remain_quantity    := l_primary_remain_quantity;
12554     -- INVCONV BEGIN
12555     IF l_dual_control_flag = 'Y' THEN
12556       x_secondary_relieved_quantity  := l_secondary_relieved_quantity;
12557       x_secondary_remain_quantity    := l_secondary_remain_quantity;
12558     END IF;
12559     -- INVCONV END
12560     x_return_status              := l_return_status;
12561   --
12562   EXCEPTION
12563     WHEN fnd_api.g_exc_error THEN
12564       ROLLBACK TO relieve_reservation_sa;
12565       x_return_status  := fnd_api.g_ret_sts_error;
12566       --  Get message count and data
12567       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
12568     WHEN fnd_api.g_exc_unexpected_error THEN
12569       ROLLBACK TO relieve_reservation_sa;
12570       x_return_status  := fnd_api.g_ret_sts_unexp_error;
12571       --  Get message count and data
12572       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
12573     WHEN OTHERS THEN
12574       ROLLBACK TO relieve_reservation_sa;
12575       x_return_status  := fnd_api.g_ret_sts_unexp_error;
12576 
12577       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
12578         fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
12579       END IF;
12580 
12581       --  Get message count and data
12582       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
12583   END relieve_reservation;
12584 
12585   --
12586   PROCEDURE delete_reservation(
12587     p_api_version_number     IN     NUMBER
12588   , p_init_msg_lst           IN     VARCHAR2 DEFAULT fnd_api.g_false
12589   , x_return_status          OUT    NOCOPY VARCHAR2
12590   , x_msg_count              OUT    NOCOPY NUMBER
12591   , x_msg_data               OUT    NOCOPY VARCHAR2
12592   , p_rsv_rec                IN     inv_reservation_global.mtl_reservation_rec_type
12593   , p_original_serial_number IN     inv_reservation_global.serial_number_tbl_type
12594   , p_validation_flag        IN     VARCHAR2 DEFAULT fnd_api.g_true
12595   ) IS
12596     l_api_version_number CONSTANT NUMBER                                          := 1.0;
12597     l_api_name           CONSTANT VARCHAR2(30)                                    := 'Delete_Reservation';
12598     l_return_status               VARCHAR2(1)                                     := fnd_api.g_ret_sts_success;
12599     l_tmp_rsv_tbl                 inv_reservation_global.mtl_reservation_tbl_type;
12600     l_tmp_rsv_tbl_count           NUMBER;
12601     l_reservation_id              NUMBER;
12602     l_dummy_rsv_rec               inv_reservation_global.mtl_reservation_rec_type;
12603     l_dummy_serial_array          inv_reservation_global.serial_number_tbl_type;
12604     --
12605     l_orig_item_cache_index       INTEGER                                         := NULL;
12606     l_orig_org_cache_index        INTEGER                                         := NULL;
12607     l_orig_demand_cache_index     INTEGER                                         := NULL;
12608     l_orig_supply_cache_index     INTEGER                                         := NULL;
12609     l_orig_sub_cache_index        INTEGER                                         := NULL;
12610     l_to_item_cache_index         INTEGER                                         := NULL;
12611     l_to_org_cache_index          INTEGER                                         := NULL;
12612     l_to_demand_cache_index       INTEGER                                         := NULL;
12613     l_to_supply_cache_index       INTEGER                                         := NULL;
12614     l_to_sub_cache_index          INTEGER                                         := NULL;
12615     l_tree_id                     NUMBER;
12616     l_error_code                  NUMBER;
12617     l_qty_changed                 NUMBER;
12618     l_secondary_qty_changed       NUMBER;					-- INVCONV
12619     l_debug number;
12620     l_lot_divisible_flag          VARCHAR2(1)                                     :='Y'; -- INVCONV
12621 
12622     /*** {{ R12 Enhanced reservations code changes ***/
12623     l_rsv_rec                     inv_reservation_global.mtl_reservation_rec_type;
12624     /*** End R12 }} ***/
12625   BEGIN
12626      -- Use cache to get value for l_debug
12627      IF g_is_pickrelease_set IS NULL THEN
12628         g_is_pickrelease_set := 2;
12629         IF INV_CACHE.is_pickrelease THEN
12630            g_is_pickrelease_set := 1;
12631         END IF;
12632      END IF;
12633      IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
12634         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
12635      END IF;
12636 
12637      l_debug := g_debug;
12638      IF (l_debug = 1) THEN
12639         debug_print('Inside delete reservation...');
12640      END IF;
12641     --  Standard call to check for call compatibility
12642     IF NOT fnd_api.compatible_api_call(l_api_version_number, p_api_version_number, l_api_name, g_pkg_name) THEN
12643       RAISE fnd_api.g_exc_unexpected_error;
12644     END IF;
12645 
12646     --
12647     --  Initialize message list.
12648     IF fnd_api.to_boolean(p_init_msg_lst) THEN
12649       fnd_msg_pub.initialize;
12650     END IF;
12651 
12652     /**** {{ R12 Enhanced reservations code changes ****/
12653     -- Set the original columns to g_miss_xxx as the user should not be
12654     -- setting these values. Do not query by them
12655     l_rsv_rec := p_rsv_rec;
12656 
12657     l_rsv_rec.orig_supply_source_type_id := fnd_api.g_miss_num;
12658     l_rsv_rec.orig_supply_source_header_id := fnd_api.g_miss_num;
12659     l_rsv_rec.orig_supply_source_line_id := fnd_api.g_miss_num;
12660     l_rsv_rec.orig_supply_source_line_detail := fnd_api.g_miss_num;
12661     l_rsv_rec.orig_demand_source_type_id := fnd_api.g_miss_num;
12662     l_rsv_rec.orig_demand_source_header_id := fnd_api.g_miss_num;
12663     l_rsv_rec.orig_demand_source_line_id := fnd_api.g_miss_num;
12664     l_rsv_rec.orig_demand_source_line_detail := fnd_api.g_miss_num;
12665 
12666     /*** End R12 }} ***/
12667 
12668     IF (l_debug = 1) THEN
12669        debug_print('Before calling query reservation...');
12670     END IF;
12671     --
12672     query_reservation(
12673       p_api_version_number         => 1.0
12674     , p_init_msg_lst               => fnd_api.g_false
12675     , x_return_status              => l_return_status
12676     , x_msg_count                  => x_msg_count
12677     , x_msg_data                   => x_msg_data
12678     , p_query_input                => l_rsv_rec           /*** {{ R12 Enhanced reservations code changes ***/
12679     , p_lock_records               => fnd_api.g_true
12680     , x_mtl_reservation_tbl        => l_tmp_rsv_tbl
12681     , x_mtl_reservation_tbl_count  => l_tmp_rsv_tbl_count
12682     , x_error_code                 => l_error_code
12683     );
12684 
12685     --
12686     IF l_return_status = fnd_api.g_ret_sts_error THEN
12687       RAISE fnd_api.g_exc_error;
12688     END IF;
12689 
12690     --
12691     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12692       RAISE fnd_api.g_exc_unexpected_error;
12693     END IF;
12694 
12695     --
12696     IF l_tmp_rsv_tbl_count = 0 THEN
12697       fnd_message.set_name('INV', 'INV-ROW NOT FOUND');
12698       fnd_msg_pub.ADD;
12699       RAISE fnd_api.g_exc_error;
12700     END IF;
12701 
12702     IF (l_debug = 1) THEN
12703        debug_print('After calling query reservation... ' || l_return_status);
12704        debug_print('No of rows to be deleted '|| l_tmp_rsv_tbl_count);
12705     END IF;
12706     --
12707     IF l_tmp_rsv_tbl_count > 1 THEN
12708       fnd_message.set_name('INV', 'INV-DELETE MORE THAN ONE ROW');
12709       fnd_msg_pub.ADD;
12710       RAISE fnd_api.g_exc_error;
12711     END IF;
12712 
12713     --
12714     l_reservation_id  := l_tmp_rsv_tbl(1).reservation_id;
12715 
12716     --
12717     IF (l_debug = 1) THEN
12718        debug_print('Before calling validate...');
12719     END IF;
12720     -- Bug 2354735: Proceed with Validation if p_validation_flag = 'T' or 'V'
12721      IF p_validation_flag = fnd_api.g_true OR p_validation_flag = 'V' THEN
12722       -- validation is needed to get revision, lot,
12723       --serial control information
12724       -- for creating the quantity tree
12725       inv_reservation_validate_pvt.validate_input_parameters(
12726         x_return_status              => l_return_status
12727       , p_orig_rsv_rec               => l_tmp_rsv_tbl(1)
12728       , p_to_rsv_rec                 => l_dummy_rsv_rec
12729       , p_orig_serial_array          => p_original_serial_number
12730       , p_to_serial_array            => l_dummy_serial_array
12731       , p_rsv_action_name            => 'DELETE'
12732       , x_orig_item_cache_index      => l_orig_item_cache_index
12733       , x_orig_org_cache_index       => l_orig_org_cache_index
12734       , x_orig_demand_cache_index    => l_orig_demand_cache_index
12735       , x_orig_supply_cache_index    => l_orig_supply_cache_index
12736       , x_orig_sub_cache_index       => l_orig_sub_cache_index
12737       , x_to_item_cache_index        => l_to_item_cache_index
12738       , x_to_org_cache_index         => l_to_org_cache_index
12739       , x_to_demand_cache_index      => l_to_demand_cache_index
12740       , x_to_supply_cache_index      => l_to_supply_cache_index
12741       , x_to_sub_cache_index         => l_to_sub_cache_index
12742       );
12743 
12744       --
12745       IF l_return_status = fnd_api.g_ret_sts_error THEN
12746         RAISE fnd_api.g_exc_error;
12747       END IF;
12748 
12749       --
12750       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12751         RAISE fnd_api.g_exc_unexpected_error;
12752       END IF;
12753 
12754       --
12755       -- INVCONV BEGIN
12756       IF NOT is_lot_divisible(l_orig_item_cache_index) THEN
12757         l_lot_divisible_flag := 'N';
12758       END IF;
12759       -- INVCONV END
12760 
12761       -- Bug 2354735: Proceed with Trees only if p_validation_flag = 'T'
12762       IF l_tmp_rsv_tbl(1).supply_source_type_id = inv_reservation_global.g_source_type_inv
12763          AND p_validation_flag = fnd_api.g_true THEN
12764         -- call quantity processor to
12765         -- modify the tree so that the quantity tree
12766         -- reflect the deletion
12767         inv_quantity_tree_pvt.create_tree(
12768           p_api_version_number         => 1.0
12769         , p_init_msg_lst               => fnd_api.g_true
12770         , x_return_status              => l_return_status
12771         , x_msg_count                  => x_msg_count
12772         , x_msg_data                   => x_msg_data
12773         , p_organization_id            => l_tmp_rsv_tbl(1).organization_id
12774         , p_inventory_item_id          => l_tmp_rsv_tbl(1).inventory_item_id
12775         , p_tree_mode                  => inv_quantity_tree_pvt.g_reservation_mode
12776         , p_is_revision_control        => is_revision_control(l_orig_item_cache_index)
12777         , p_is_lot_control             => is_lot_control(l_orig_item_cache_index)
12778         , p_is_serial_control          => is_serial_control(l_orig_item_cache_index)
12779         , p_asset_sub_only             => FALSE
12780         , p_include_suggestion         => TRUE
12781         , p_demand_source_type_id      => l_tmp_rsv_tbl(1).demand_source_type_id
12782         , p_demand_source_header_id    => l_tmp_rsv_tbl(1).demand_source_header_id
12783         , p_demand_source_line_id      => l_tmp_rsv_tbl(1).demand_source_line_id
12784         , p_demand_source_name         => l_tmp_rsv_tbl(1).demand_source_name
12785         , p_demand_source_delivery     => l_tmp_rsv_tbl(1).demand_source_delivery
12786         , p_lot_expiration_date        => NULL
12787         , x_tree_id                    => l_tree_id
12788         );
12789 
12790         IF l_return_status = fnd_api.g_ret_sts_error THEN
12791           RAISE fnd_api.g_exc_error;
12792         END IF;
12793 
12794         --
12795         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12796           RAISE fnd_api.g_exc_unexpected_error;
12797         END IF;
12798 
12799 	IF (l_debug = 1) THEN
12800            debug_print('After calling validate...' || l_return_status);
12801         END IF;
12802 
12803         -- INVCONV - Upgrade call to incorporate secondaries
12804         modify_tree_crt_del_rel(
12805           x_return_status              => l_return_status
12806         , p_tree_id                    => l_tree_id
12807         , p_revision                   => l_tmp_rsv_tbl(1).revision
12808         , p_lot_number                 => l_tmp_rsv_tbl(1).lot_number
12809         , p_subinventory_code          => l_tmp_rsv_tbl(1).subinventory_code
12810         , p_locator_id                 => l_tmp_rsv_tbl(1).locator_id
12811         , p_lpn_id                     => l_tmp_rsv_tbl(1).lpn_id
12812         , p_primary_reservation_quantity=> l_tmp_rsv_tbl(1).primary_reservation_quantity
12813         , p_second_reservation_quantity=> l_tmp_rsv_tbl(1).secondary_reservation_quantity
12814         , p_detailed_quantity          => l_tmp_rsv_tbl(1).detailed_quantity
12815         , p_secondary_detailed_quantity=> l_tmp_rsv_tbl(1).secondary_detailed_quantity
12816         , p_partial_reservation_flag   => fnd_api.g_false
12817         , p_force_reservation_flag     => fnd_api.g_false
12818         , p_lot_divisible_flag         => l_lot_divisible_flag    -- INVCONV
12819         , p_action                     => 'DELETE'
12820         , x_qty_changed                => l_qty_changed
12821         , x_secondary_qty_changed      => l_secondary_qty_changed
12822 	, p_organization_id            => l_rsv_rec.organization_id
12823         , p_demand_source_line_id      => l_tmp_rsv_tbl(1).demand_source_line_id
12824 	, p_demand_source_type_id      => l_tmp_rsv_tbl(1).demand_source_type_id
12825         );
12826 
12827         --
12828         IF l_return_status = fnd_api.g_ret_sts_error THEN
12829           RAISE fnd_api.g_exc_error;
12830         END IF;
12831 
12832         --
12833         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12834           RAISE fnd_api.g_exc_unexpected_error;
12835         END IF;
12836 	IF (l_debug = 1) THEN
12837            debug_print('Return Status...' || l_return_status);
12838         END IF;
12839       --
12840       END IF;
12841     END IF;
12842 
12843 
12844     -- Pre Delete CTO Validation
12845     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
12846       --
12847       cto_workflow_api_pk.inventory_unreservation_check(
12848         p_order_line_id              => l_tmp_rsv_tbl(1).demand_source_line_id
12849       , p_rsv_quantity               => l_tmp_rsv_tbl(1).primary_reservation_quantity
12850       , x_return_status              => l_return_status
12851       , x_msg_count                  => x_msg_count
12852       , x_msg_data                   => x_msg_data
12853       );
12854 
12855       --
12856       IF l_return_status = fnd_api.g_ret_sts_error THEN
12857         RAISE fnd_api.g_exc_error;
12858       END IF;
12859 
12860       --
12861       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12862         RAISE fnd_api.g_exc_unexpected_error;
12863       END IF;
12864     --
12865     END IF;
12866     IF (l_debug = 1) THEN
12867        debug_print('After calling inventory_unreservation_check...' || l_return_status);
12868     END IF;
12869 
12870     --
12871     -- for data sync b/w mtl_demand and mtl_reservations
12872     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);
12873 
12874     IF l_return_status = fnd_api.g_ret_sts_error THEN
12875       RAISE fnd_api.g_exc_error;
12876     END IF;
12877 
12878     --
12879     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12880       RAISE fnd_api.g_exc_unexpected_error;
12881     END IF;
12882 
12883     /*** {{ R12 Enhanced reservations code changes ***/
12884     -- update the mtl_serial_numbers, null out the reservation_id
12885     -- and group_mark_id of the reserved serials before delete the
12886     -- reservation. There is no need to update the serial_reservation_quantity
12887     -- because the reservation record will be deleted.
12888     BEGIN
12889        update mtl_serial_numbers
12890 	 set reservation_id = NULL,
12891 	 group_mark_id = NULL, line_mark_id = NULL,
12892 	 lot_line_mark_id = NULL
12893 	 where reservation_id = l_reservation_id;
12894     EXCEPTION
12895        WHEN NO_DATA_FOUND THEN
12896 	  IF (l_debug = 1) THEN
12897 	     debug_print('No serial numbers stamp with reservation_id ' || l_reservation_id);
12898 	  END IF;
12899     END;
12900     /*** End R12 }} ***/
12901 
12902     --
12903     --
12904     -- delete the reservation from the db table
12905     mtl_reservations_pkg.delete_row(x_reservation_id => l_reservation_id);
12906 
12907     -- Post Delete CTO Validation
12908     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
12909       --
12910       cto_workflow_api_pk.wf_update_after_inv_unreserv(
12911         p_order_line_id              => l_tmp_rsv_tbl(1).demand_source_line_id
12912       , x_return_status              => l_return_status
12913       , x_msg_count                  => x_msg_count
12914       , x_msg_data                   => x_msg_data
12915       );
12916 
12917       --
12918       IF l_return_status = fnd_api.g_ret_sts_error THEN
12919         RAISE fnd_api.g_exc_error;
12920       END IF;
12921 
12922       --
12923       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12924         RAISE fnd_api.g_exc_unexpected_error;
12925       END IF;
12926       --
12927       IF (l_debug = 1) THEN
12928          debug_print('After calling wf_update_after_inv_unreserv...' || l_return_status);
12929       END IF;
12930     END IF;
12931 
12932     x_return_status   := l_return_status;
12933   --
12934   EXCEPTION
12935     WHEN fnd_api.g_exc_error THEN
12936       x_return_status  := fnd_api.g_ret_sts_error;
12937       --  Get message count and data
12938       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
12939     WHEN fnd_api.g_exc_unexpected_error THEN
12940       x_return_status  := fnd_api.g_ret_sts_unexp_error;
12941       --  Get message count and data
12942       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
12943     WHEN OTHERS THEN
12944       x_return_status  := fnd_api.g_ret_sts_unexp_error;
12945 
12946       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
12947         fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
12948       END IF;
12949 
12950       --  Get message count and data
12951       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
12952   END delete_reservation;
12953 
12954 
12955 /**** {{ R12 Enhanced reservations code changes }}****/
12956 -- Description
12957   --   transfer is very similar to update
12958   --   except the to row can be not exist (will be created) or
12959   --   exist (will be add upon)
12960   PROCEDURE transfer_reservation
12961     (
12962      p_api_version_number       IN     NUMBER
12963      , p_init_msg_lst           IN     VARCHAR2 DEFAULT fnd_api.g_false
12964      , x_return_status          OUT    NOCOPY VARCHAR2
12965      , x_msg_count              OUT    NOCOPY NUMBER
12966      , x_msg_data               OUT    NOCOPY VARCHAR2
12967      , p_original_rsv_rec       IN     inv_reservation_global.mtl_reservation_rec_type
12968      , p_to_rsv_rec             IN     inv_reservation_global.mtl_reservation_rec_type
12969      , p_original_serial_number IN     inv_reservation_global.serial_number_tbl_type
12970      , p_validation_flag        IN     VARCHAR2 DEFAULT fnd_api.g_true
12971      , p_over_reservation_flag  IN  NUMBER DEFAULT 0
12972      , x_reservation_id         OUT    NOCOPY NUMBER
12973      ) IS
12974 
12975      l_api_version_number CONSTANT NUMBER :=  1.0;
12976      l_api_name           CONSTANT VARCHAR2(30) := 'Transfrer_Reservation';
12977      l_return_status      VARCHAR2(1) :=  fnd_api.g_ret_sts_success;
12978      l_quantity_reserved  NUMBER;
12979      l_secondary_quantity_reserved NUMBER;          -- INVCONV
12980      l_debug NUMBER;
12981      l_dummy_serial_number inv_reservation_global.serial_number_tbl_type;
12982      l_reservation_id NUMBER;
12983      l_reservation_qty_lot NUMBER := 0; --Bug 12978409
12984 
12985   BEGIN
12986      --  Standard call to check for call compatibility
12987      IF NOT fnd_api.compatible_api_call
12988        (l_api_version_number
12989 	, p_api_version_number
12990 	, l_api_name
12991 	, G_PKG_NAME
12992 	) THEN
12993 	RAISE fnd_api.g_exc_unexpected_error;
12994      END IF;
12995 
12996      --  Initialize message list.
12997      IF fnd_api.to_boolean(p_init_msg_lst) THEN
12998 	fnd_msg_pub.initialize;
12999      END IF;
13000 
13001      -- Use cache to get value for l_debug
13002      IF g_is_pickrelease_set IS NULL THEN
13003         g_is_pickrelease_set := 2;
13004         IF INV_CACHE.is_pickrelease THEN
13005            g_is_pickrelease_set := 1;
13006         END IF;
13007      END IF;
13008      IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
13009         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
13010      END IF;
13011 
13012      l_debug := g_debug;
13013 
13014      IF l_debug=1 THEN
13015 	debug_print('Calling the overloaded procedure transfer_reservation');
13016      END IF;
13017 
13018      inv_reservation_pvt.transfer_reservation
13019        (p_api_version_number          => 1.0,
13020 	p_init_msg_lst                => p_init_msg_lst,
13021 	x_return_status               => l_return_status,
13022 	x_msg_count                   => x_msg_count,
13023 	x_msg_data                    => x_msg_data,
13024 	p_original_rsv_rec            => p_original_rsv_rec,
13025 	p_to_rsv_rec                  => p_to_rsv_rec,
13026 	p_original_serial_number      => p_original_serial_number,
13027 	p_to_serial_number            => l_dummy_serial_number,
13028 	p_validation_flag             => p_validation_flag,
13029 	p_over_reservation_flag       => p_over_reservation_flag,
13030 	x_reservation_id              => l_reservation_id
13031 	);
13032 
13033      IF (l_debug=1) THEN
13034 	debug_print ('Return Status after transfer reservations '||l_return_status);
13035      END IF;
13036 
13037      IF l_return_status = fnd_api.g_ret_sts_error THEN
13038 
13039 	IF l_debug=1 THEN
13040 	   debug_print('Raising expected error'||l_return_status);
13041 	END IF;
13042 
13043 	RAISE fnd_api.g_exc_error;
13044 
13045       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13046 
13047 	IF l_debug=1 THEN
13048 	   debug_print('Raising Unexpected error'||l_return_status);
13049 	END IF;
13050 
13051 	RAISE fnd_api.g_exc_unexpected_error;
13052      END IF;
13053 
13054 
13055      x_return_status := l_return_status;
13056      x_reservation_id := l_reservation_id;
13057 
13058   EXCEPTION
13059 
13060      WHEN fnd_api.g_exc_error THEN
13061 	x_return_status := fnd_api.g_ret_sts_error;
13062 	--  Get message count and data
13063 	fnd_msg_pub.count_and_get
13064 	  (  p_count => x_msg_count
13065 	     , p_data  => x_msg_data
13066 	     );
13067 
13068      WHEN fnd_api.g_exc_unexpected_error THEN
13069 	x_return_status := fnd_api.g_ret_sts_unexp_error ;
13070 
13071 	--  Get message count and data
13072 	fnd_msg_pub.count_and_get
13073 	  (  p_count  => x_msg_count
13074 	     , p_data   => x_msg_data
13075 	     );
13076 
13077      WHEN OTHERS THEN
13078 	x_return_status := fnd_api.g_ret_sts_unexp_error ;
13079 
13080 	IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
13081 	  THEN
13082 	   fnd_msg_pub.add_exc_msg
13083 	     (  g_pkg_name
13084 		, l_api_name
13085 		);
13086 	END IF;
13087 
13088 	--  Get message count and data
13089 	fnd_msg_pub.count_and_get
13090 	  (  p_count  => x_msg_count
13091 	     , p_data   => x_msg_data
13092 	     );
13093 
13094   END transfer_reservation;
13095   /*** End R12 ***/
13096 
13097   -- Overloaded transfer_reservation API
13098   -- Description
13099   --   transfer is very similar to update
13100   --   except the to row can be not exist (will be created) or
13101   --   exist (will be add upon)
13102   PROCEDURE transfer_reservation
13103     (
13104      p_api_version_number     IN     NUMBER
13105      , p_init_msg_lst           IN     VARCHAR2 DEFAULT fnd_api.g_false
13106      , x_return_status          OUT    NOCOPY VARCHAR2
13107      , x_msg_count              OUT    NOCOPY NUMBER
13108      , x_msg_data               OUT    NOCOPY VARCHAR2
13109      , p_original_rsv_rec       IN     inv_reservation_global.mtl_reservation_rec_type
13110      , p_to_rsv_rec             IN     inv_reservation_global.mtl_reservation_rec_type
13111      , p_original_serial_number IN     inv_reservation_global.serial_number_tbl_type
13112      /**** {{ R12 Enhanced reservations code changes }}****/
13113      , p_to_serial_number  IN  inv_reservation_global.serial_number_tbl_type
13114      /*** End R12 ***/
13115      , p_validation_flag        IN     VARCHAR2 DEFAULT fnd_api.g_true
13116      , p_over_reservation_flag  IN  NUMBER DEFAULT 0
13117      , x_reservation_id         OUT    NOCOPY NUMBER
13118      ) IS
13119 	l_api_version_number  CONSTANT NUMBER                                          := 1.0;
13120 	l_api_name            CONSTANT VARCHAR2(30)                                    := 'Transfer_Reservation';
13121 	l_return_status                VARCHAR2(1)                                     := fnd_api.g_ret_sts_success;
13122 	l_original_rsv_rec             inv_reservation_global.mtl_reservation_rec_type;
13123 	l_to_rsv_rec                   inv_reservation_global.mtl_reservation_rec_type;
13124 	l_orig_rsv_tbl                 inv_reservation_global.mtl_reservation_tbl_type;
13125 	l_orig_rsv_tbl_count           NUMBER;
13126 	l_to_rsv_tbl                   inv_reservation_global.mtl_reservation_tbl_type;
13127 	--
13128 	l_to_rsv_tbl_count             NUMBER;
13129 	l_transfer_all                 BOOLEAN;
13130 	l_to_row_exist                 BOOLEAN;
13131 	l_primary_uom_code             VARCHAR2(3);
13132 	l_reservation_uom_code         VARCHAR2(3);
13133 	l_reservation_id               NUMBER;
13134 	l_orig_item_cache_index        INTEGER                                         := NULL;
13135 	l_orig_org_cache_index         INTEGER                                         := NULL;
13136 	l_orig_demand_cache_index      INTEGER                                         := NULL;
13137 	l_orig_supply_cache_index      INTEGER                                         := NULL;
13138 	l_orig_sub_cache_index         INTEGER                                         := NULL;
13139 	l_to_item_cache_index          INTEGER                                         := NULL;
13140 	l_to_org_cache_index           INTEGER                                         := NULL;
13141 	l_to_demand_cache_index        INTEGER                                         := NULL;
13142 	l_to_supply_cache_index        INTEGER                                         := NULL;
13143 	l_to_sub_cache_index           INTEGER                                         := NULL;
13144 	l_primary_reservation_quantity NUMBER;
13145 	l_second_reservation_quantity  NUMBER;                                      -- INVCONV
13146 	l_tree_id1                     NUMBER;
13147 	l_tree_id2                     NUMBER;
13148 	l_date                         DATE;
13149 	l_user_id                      NUMBER;
13150 	l_request_id                   NUMBER;
13151 	l_login_id                     NUMBER;
13152 	l_prog_appl_id                 NUMBER;
13153 	l_program_id                   NUMBER;
13154 	l_qty                          NUMBER;
13155 	l_rowid                        VARCHAR2(2000);
13156 	l_error_code                   NUMBER;
13157 	l_detailed_quantity            NUMBER;
13158 	l_secondary_detailed_quantity  NUMBER;                                       -- INVCONV
13159 	l_new_orig_rsv_qty             NUMBER;
13160 	l_new_orig_prim_qty            NUMBER;
13161 	l_orig_second_rsv_qty          NUMBER;                                       -- INVCONV
13162 	l_primary_rsv_quantity NUMBER;
13163 	l_secondary_rsv_quantity       NUMBER;                                       -- INVCONV
13164 	l_rsv_quantity NUMBER;
13165 	l_debug number;
13166 	l_quantity_reserved  NUMBER;
13167 	l_secondary_quantity_reserved  NUMBER;                                       -- INVCONV
13168 	l_lot_divisible_flag           VARCHAR2(1)  :='Y';                           -- INVCONV
13169 	l_dual_tracking                BOOLEAN := FALSE;                             -- INVCONV
13170 
13171 	/**** {{ R12 Enhanced reservations code changes }}****/
13172 	l_original_serial_number inv_reservation_global.serial_number_tbl_type;
13173 	l_to_serial_number inv_reservation_global.serial_number_tbl_type;
13174 	l_serial_number_table inv_reservation_global.serial_number_tbl_type;
13175 	l_dummy_rsv_rec inv_reservation_global.mtl_reservation_rec_type;
13176 	l_dummy_serial_array  inv_reservation_global.serial_number_tbl_type;
13177 	l_qty_available NUMBER := 0;
13178 	l_original_serial_count NUMBER;
13179 	l_to_serial_count NUMBER;
13180 	l_serial_param NUMBER;
13181 	l_serials_tobe_unreserved NUMBER;
13182 	l_total_serials_reserved NUMBER;
13183 	l_total_to_serials_reserved NUMBER;
13184 	l_from_reservation_id NUMBER;
13185 	l_validate_serials_reserved NUMBER;
13186 	l_validate_serial_number_table inv_reservation_global.serial_number_tbl_type;
13187 	l_serials_unreserved NUMBER;
13188 	l_to_reservation_id NUMBER;
13189 	l_total_from_serials_reserved NUMBER;
13190 	l_from_primary_reservation_qty NUMBER;
13191 	l_to_primary_reservation_qty NUMBER;
13192 	l_supply_lock_handle varchar2(128);
13193 	l_demand_lock_handle varchar2(128);
13194 	l_lock_status NUMBER;
13195         l_reserved_qty NUMBER := 0;
13196         l_requested_qty NUMBER := 0;
13197 	l_group_mark_id NUMBER := NULL;
13198 	l_lock_obtained BOOLEAN := FALSE;
13199 	l_pjm_enabled NUMBER;
13200 	l_project_id NUMBER;
13201 	l_task_id NUMBER;
13202 	l_orig_supply_type_id NUMBER;
13203 	l_supply_source_type_id NUMBER;
13204 	/*** End R12 ***/
13205     l_reservation_qty_lot NUMBER := 0; --Bug 12978409
13206     /* Added for bug 13829182 */
13207     l_wip_entity_id NUMBER;
13208 	l_wip_entity_type NUMBER;
13209 	l_wip_job_type VARCHAR2(15);
13210 	l_maintenance_object_source NUMBER;
13211     /* End of changes for bug 13829182 */
13212     --MUOM Fulfillment Project
13213      l_qty_available2  NUMBER;
13214      l_quantity_reserved2 NUMBER;
13215      l_fulfill_base   VARCHAR2(1) := 'P';
13216      l_org_sec_rsv_qty NUMBER;
13217      l_qoh NUMBER;
13218      l_rqoh NUMBER;
13219      l_qr NUMBER;
13220 	 l_qs NUMBER;
13221 	 l_att NUMBER;
13222 	 l_atr NUMBER;
13223      l_sqoh	NUMBER;
13224 	 l_srqoh NUMBER;
13225 	 l_sqr NUMBER;
13226      l_sqs NUMBER;
13227      l_satt NUMBER;
13228      l_satr NUMBER;
13229 
13230 
13231   BEGIN
13232     -- Use cache to get value for l_debug
13233     IF g_is_pickrelease_set IS NULL THEN
13234        g_is_pickrelease_set := 2;
13235        IF INV_CACHE.is_pickrelease THEN
13236           g_is_pickrelease_set := 1;
13237        END IF;
13238     END IF;
13239     IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
13240        g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
13241     END IF;
13242 
13243     l_debug := g_debug;
13244     IF (l_debug = 1) THEN
13245        debug_print('Inside transfer reservation...');
13246     END IF;
13247 
13248     --  Standard call to check for call compatibility
13249     IF NOT fnd_api.compatible_api_call(l_api_version_number, p_api_version_number, l_api_name, g_pkg_name) THEN
13250       RAISE fnd_api.g_exc_unexpected_error;
13251     END IF;
13252 
13253     IF (l_debug = 1) THEN
13254        debug_print('The original reservation record: ');
13255     END IF;
13256     print_rsv_rec(p_original_rsv_rec);
13257     IF (l_debug = 1) THEN
13258        debug_print('The to reservation record: ');
13259     END IF;
13260     print_rsv_rec(p_to_rsv_rec);
13261 
13262     --
13263     --
13264     -- if the transfer quantity is 0, call delete instead
13265     IF p_to_rsv_rec.primary_reservation_quantity = 0
13266        OR (p_to_rsv_rec.reservation_quantity = 0
13267            AND (p_to_rsv_rec.primary_reservation_quantity IS NULL
13268                 OR p_to_rsv_rec.primary_reservation_quantity = fnd_api.g_miss_num
13269                )
13270           ) THEN
13271       x_return_status  := fnd_api.g_ret_sts_success;
13272       RETURN;
13273     END IF;
13274 
13275     --  Initialize message list.
13276     IF fnd_api.to_boolean(p_init_msg_lst) THEN
13277       fnd_msg_pub.initialize;
13278     END IF;
13279 
13280     /**** {{ R12 Enhanced reservations code changes }}****/
13281 
13282     -- Set the original columns to g_miss_xxx as the user should not be
13283     -- setting these values. Do not query by them
13284     l_original_rsv_rec := p_original_rsv_rec;
13285 
13286     l_original_rsv_rec.orig_supply_source_type_id := fnd_api.g_miss_num;
13287     l_original_rsv_rec.orig_supply_source_header_id := fnd_api.g_miss_num;
13288     l_original_rsv_rec.orig_supply_source_line_id := fnd_api.g_miss_num;
13289     l_original_rsv_rec.orig_supply_source_line_detail := fnd_api.g_miss_num;
13290     l_original_rsv_rec.orig_demand_source_type_id := fnd_api.g_miss_num;
13291     l_original_rsv_rec.orig_demand_source_header_id := fnd_api.g_miss_num;
13292     l_original_rsv_rec.orig_demand_source_line_id := fnd_api.g_miss_num;
13293     l_original_rsv_rec.orig_demand_source_line_detail := fnd_api.g_miss_num;
13294 
13295     /*** End R12 ***/
13296 
13297     SAVEPOINT transfer_reservation_sa;
13298     IF (l_debug = 1) THEN
13299        debug_print('Before calling query rsv for the from record: ' || l_return_status);
13300     END IF;
13301 
13302     -- search for the from row
13303     query_reservation(
13304       p_api_version_number         => 1.0
13305     , p_init_msg_lst               => fnd_api.g_false
13306     , x_return_status              => l_return_status
13307     , x_msg_count                  => x_msg_count
13308     , x_msg_data                   => x_msg_data
13309     , p_query_input                => l_original_rsv_rec
13310     , p_lock_records               => fnd_api.g_true
13311     , x_mtl_reservation_tbl        => l_orig_rsv_tbl
13312     , x_mtl_reservation_tbl_count  => l_orig_rsv_tbl_count
13313     , x_error_code                 => l_error_code
13314     );
13315 
13316     --
13317     IF l_return_status = fnd_api.g_ret_sts_error THEN
13318       RAISE fnd_api.g_exc_error;
13319     END IF;
13320 
13321     --
13322     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13323       RAISE fnd_api.g_exc_unexpected_error;
13324     END IF;
13325 
13326     --
13327     IF l_orig_rsv_tbl_count = 0 THEN
13328       fnd_message.set_name('INV', 'INV-ROW NOT FOUND');
13329       fnd_msg_pub.ADD;
13330       RAISE fnd_api.g_exc_error;
13331     END IF;
13332 
13333     --
13334     IF l_orig_rsv_tbl_count > 1 THEN
13335       fnd_message.set_name('INV', 'TRANSFER MORE THAN ONE ROW');
13336       fnd_msg_pub.ADD;
13337       RAISE fnd_api.g_exc_error;
13338     END IF;
13339 
13340     IF (l_debug = 1) THEN
13341        debug_print('After calling query rsv from record: ' || l_return_status);
13342        debug_print(' orig tbl count' || l_orig_rsv_tbl_count);
13343     END IF;
13344 
13345     construct_to_reservation_row(l_orig_rsv_tbl(1), p_to_rsv_rec, l_to_rsv_rec);
13346 
13347     --
13348     -- if the caller does not specified reservation_id, l_to_rsv_rec will
13349     -- has the same reservation_id as l_orig_rsv_tbl(1) due to the way
13350     -- construct_to_reservation_row works.
13351     -- but we should set it to g_miss_num again
13352     -- otherwise query_reservation will use only the
13353     -- reservation_id to do the search.
13354     IF l_to_rsv_rec.reservation_id = l_orig_rsv_tbl(1).reservation_id THEN
13355       l_to_rsv_rec.reservation_id  := fnd_api.g_miss_num;
13356     END IF;
13357 
13358     --
13359     IF l_orig_rsv_tbl(1).organization_id <> l_to_rsv_rec.organization_id THEN
13360       fnd_message.set_name('INV', 'CANNOT_CHANGE_ORGANIZATION_ID');
13361       fnd_msg_pub.ADD;
13362       RAISE fnd_api.g_exc_error;
13363     END IF;
13364 
13365     --
13366     IF l_orig_rsv_tbl(1).inventory_item_id <> l_to_rsv_rec.inventory_item_id THEN
13367       fnd_message.set_name('INV', 'CANNOT_CHANGE_INVENTORY_ITEM');
13368       fnd_msg_pub.ADD;
13369       RAISE fnd_api.g_exc_error;
13370     END IF;
13371 
13372     -- convert quantity between primary uom and reservation uom
13373     convert_quantity(x_return_status => l_return_status, px_rsv_rec => l_to_rsv_rec);
13374 
13375     --
13376     IF l_return_status = fnd_api.g_ret_sts_error THEN
13377       RAISE fnd_api.g_exc_error;
13378     END IF;
13379 
13380     --
13381     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13382       RAISE fnd_api.g_exc_unexpected_error;
13383     END IF;
13384 
13385     /**** {{ R12 Enhanced reservations code changes }}****/
13386 
13387     -- Set the original columns to g_miss_xxx as the user should not be
13388     -- setting these values. Do not query by them
13389     l_to_rsv_rec.orig_supply_source_type_id := fnd_api.g_miss_num;
13390     l_to_rsv_rec.orig_supply_source_header_id := fnd_api.g_miss_num;
13391     l_to_rsv_rec.orig_supply_source_line_id := fnd_api.g_miss_num;
13392     l_to_rsv_rec.orig_supply_source_line_detail := fnd_api.g_miss_num;
13393     l_to_rsv_rec.orig_demand_source_type_id := fnd_api.g_miss_num;
13394     l_to_rsv_rec.orig_demand_source_header_id := fnd_api.g_miss_num;
13395     l_to_rsv_rec.orig_demand_source_line_id := fnd_api.g_miss_num;
13396     l_to_rsv_rec.orig_demand_source_line_detail := fnd_api.g_miss_num;
13397 
13398     IF (l_to_rsv_rec.project_id IS NULL)  THEN
13399        l_to_rsv_rec.project_id := fnd_api.g_miss_num;
13400     END IF;
13401     IF (l_to_rsv_rec.task_id IS NULL)  THEN
13402        l_to_rsv_rec.task_id := fnd_api.g_miss_num;
13403     END IF;
13404     /*** End R12 ***/
13405 
13406     IF (l_debug = 1) THEN
13407        debug_print('Before query reservation for the to rec...' || l_return_status);
13408     END IF;
13409     --
13410     -- search for the to row
13411     query_reservation(
13412       p_api_version_number         => 1.0
13413     , p_init_msg_lst               => fnd_api.g_false
13414     , x_return_status              => l_return_status
13415     , x_msg_count                  => x_msg_count
13416     , x_msg_data                   => x_msg_data
13417     , p_query_input                => l_to_rsv_rec
13418     , p_lock_records               => fnd_api.g_true
13419     , x_mtl_reservation_tbl        => l_to_rsv_tbl
13420     , x_mtl_reservation_tbl_count  => l_to_rsv_tbl_count
13421     , x_error_code                 => l_error_code
13422     );
13423 
13424     --
13425     IF l_return_status = fnd_api.g_ret_sts_error THEN
13426       RAISE fnd_api.g_exc_error;
13427     END IF;
13428 
13429     --
13430     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13431       RAISE fnd_api.g_exc_unexpected_error;
13432     END IF;
13433 
13434     --
13435     IF l_to_rsv_tbl_count > 1 THEN
13436       -- if there more than one target row, the reservation table
13437       -- must be damaged. We can not do anything but failed
13438       fnd_message.set_name('INV', 'INV-RSV-TOO-MANY-TARGET');
13439       fnd_msg_pub.ADD;
13440       RAISE fnd_api.g_exc_error;
13441     END IF;
13442 
13443     IF (l_debug = 1) THEN
13444        debug_print('After query reservation to rec...' || l_return_status);
13445        debug_print('l_to_rsv_tbl_count ' || l_to_rsv_tbl_count);
13446     END IF;
13447 
13448     IF  l_to_rsv_tbl_count = 1
13449         AND l_to_rsv_tbl(1).reservation_id = l_orig_rsv_tbl(1).reservation_id THEN
13450       -- this is a lazy way to find out that the user
13451       -- is trying to use transfer_reservation
13452       -- to update non primary key fields
13453       -- otherwise we would not have the target row having the same values
13454       -- in the primary key columns (see query_reservation select criteria)
13455       -- as the original reservation.
13456       -- we might want to move that to the validation api soon
13457       fnd_message.set_name('INV', 'MISS_USE_TRANSFER');
13458       fnd_msg_pub.ADD;
13459       RAISE fnd_api.g_exc_error;
13460     END IF;
13461 
13462     l_to_row_exist   := (l_to_rsv_tbl_count > 0);
13463 
13464     --
13465     -- if the target reservation already exists, record its reservation id
13466     IF l_to_row_exist THEN
13467       l_to_rsv_rec.reservation_id  := l_to_rsv_tbl(1).reservation_id;
13468     END IF;
13469 
13470    --
13471     IF l_orig_rsv_tbl(1).primary_reservation_quantity < l_to_rsv_rec.primary_reservation_quantity THEN
13472       fnd_message.set_name('INV', 'TRANSFER MORE THAN RESERVE');
13473       fnd_msg_pub.ADD;
13474       RAISE fnd_api.g_exc_error;
13475     END IF;
13476 
13477     /**** {{ R12 Enhanced reservations code changes.Calling the reservation
13478     -- lock API to create a user-defined lock for non-inventory supplies }} *****/
13479       -- Bug 5199672: Should pass null to supply and demand line detail as
13480       -- we will have to lock the records at the document level and not at
13481       -- the line level. Also, for ASN, pass the source type as PO so that the
13482       -- the lock name would be the same as the PO's
13483 
13484     IF (l_to_rsv_rec.supply_source_type_id =
13485 	inv_reservation_global.g_source_type_asn) THEN
13486        l_supply_source_type_id :=
13487 	 inv_reservation_global.g_source_type_po;
13488      ELSE
13489        l_supply_source_type_id := l_to_rsv_rec.supply_source_type_id;
13490     END IF;
13491 
13492     IF (l_to_rsv_rec.supply_source_type_id <> inv_reservation_global.g_source_type_inv) THEN
13493        inv_reservation_lock_pvt.lock_supply_demand_record
13494 	 (p_organization_id => l_to_rsv_rec.organization_id
13495 	  ,p_inventory_item_id => l_to_rsv_rec.inventory_item_id
13496 	  ,p_source_type_id => l_supply_source_type_id
13497 	  ,p_source_header_id => l_to_rsv_rec.supply_source_header_id
13498 	  ,p_source_line_id =>  l_to_rsv_rec.supply_source_line_id
13499 	  ,p_source_line_detail => NULL
13500 	  ,x_lock_handle => l_supply_lock_handle
13501 	  ,x_lock_status => l_lock_status);
13502 
13503        IF l_lock_status = 0 THEN
13504 	  fnd_message.set_name('INV', 'INV_INVALID_LOCK');
13505 	  fnd_msg_pub.ADD;
13506 	  RAISE fnd_api.g_exc_error;
13507        END if;
13508 
13509        inv_reservation_lock_pvt.lock_supply_demand_record
13510 	 (p_organization_id => l_to_rsv_rec.organization_id
13511 	  ,p_inventory_item_id => l_to_rsv_rec.inventory_item_id
13512 	  ,p_source_type_id => l_to_rsv_rec.demand_source_type_id
13513 	  ,p_source_header_id => l_to_rsv_rec.demand_source_header_id
13514 	  ,p_source_line_id =>  l_to_rsv_rec.demand_source_line_id
13515 	  ,p_source_line_detail => NULL
13516 	  ,x_lock_handle => l_demand_lock_handle
13517 	  ,x_lock_status => l_lock_status);
13518 
13519        IF l_lock_status = 0 THEN
13520 	  fnd_message.set_name('INV', 'INV_INVALID_LOCK');
13521 	  fnd_msg_pub.ADD;
13522 	  RAISE fnd_api.g_exc_error;
13523        END if;
13524 
13525        l_lock_obtained := TRUE;
13526     END IF;
13527 
13528     -- Get the project and task for demands in OE, INT-ORD and RMA
13529     IF l_debug=1 THEN
13530        debug_print('Before Rsv rec project id: ' || l_to_rsv_rec.project_id);
13531        debug_print('Before Rsv rec task id: ' || l_to_rsv_rec.task_id);
13532     END IF;
13533     -- Bug : 5264987 : For Pick Release seeting the l_pjm_enabled flag
13534     -- from INV_CACHE.
13535     IF INV_CACHE.is_pickrelease OR g_is_pickrelease_set = 1 THEN
13536         debug_print ('is_pickrelaese is true');
13537 	-- Query for and cache the org record.
13538 	IF (NOT INV_CACHE.set_org_rec(l_to_rsv_rec.organization_id))
13539 	THEN
13540 		IF (l_debug = 1) THEN
13541 			debug_print('Error caching the org record');
13542 		END IF;
13543 		RAISE fnd_api.g_exc_unexpected_error;
13544 	END IF;
13545 	-- Set the PJM enabled flag.
13546 	l_pjm_enabled := INV_CACHE.org_rec.project_reference_enabled;
13547     ELSE
13548         debug_print ('is_pickrelaese is not true');
13549         BEGIN
13550             SELECT project_reference_enabled
13551 	    INTO l_pjm_enabled
13552 	    FROM   mtl_parameters
13553 	    WHERE  organization_id = l_to_rsv_rec.organization_id;
13554         EXCEPTION
13555             WHEN no_data_found THEN
13556 	       IF l_debug=1 THEN
13557 	       debug_print('Cannot find the project and task information');
13558 	       END IF;
13559         END;
13560      END IF;
13561     IF (l_to_rsv_rec.demand_source_type_id IN
13562 	(inv_reservation_global.g_source_type_oe,
13563 	 inv_reservation_global.g_source_type_internal_ord,
13564 	 inv_reservation_global.g_source_type_rma)) AND
13565       (l_pjm_enabled = 1) THEN
13566 
13567        IF (l_to_rsv_rec.demand_source_line_id IS NOT NULL) AND
13568 	 (l_to_rsv_rec.demand_source_line_id <> fnd_api.g_miss_num)
13569 	 AND ((l_to_rsv_rec.project_id = fnd_api.g_miss_num) OR
13570 	      (l_to_rsv_rec.task_id = fnd_api.g_miss_num)) THEN
13571 	 -- Added the below IF condition for Bug Fix 5264987
13572 	 IF l_to_rsv_rec.demand_source_line_id = g_oe_line_id THEN
13573 		l_project_id := g_project_id;
13574 		l_task_id    := g_task_id;
13575 	 ELSE
13576 		 BEGIN
13577 		    SELECT project_id, task_id INTO l_project_id, l_task_id
13578 		      FROM oe_order_lines_all WHERE
13579 		      line_id = l_to_rsv_rec.demand_source_line_id;
13580 		 EXCEPTION
13581 		    WHEN no_data_found THEN
13582 		       IF l_debug=1 THEN
13583 			  debug_print('Cannot find the project and task information');
13584 		       END IF;
13585 		 END;
13586 	 END IF;
13587 
13588 	 IF (l_to_rsv_rec.project_id = fnd_api.g_miss_num) THEN
13589 	    IF (l_project_id IS NOT NULL) THEN
13590 	       l_to_rsv_rec.project_id := l_project_id;
13591 	     ELSE
13592 	       l_to_rsv_rec.project_id := NULL;
13593 	    END IF;
13594 	 END IF;
13595 
13596 	 IF (l_to_rsv_rec.task_id = fnd_api.g_miss_num) THEN
13597 	    IF (l_task_id IS NOT NULL) THEN
13598 	       l_to_rsv_rec.task_id := l_task_id;
13599 	     ELSE
13600 	       l_to_rsv_rec.task_id := NULL;
13601 	    END IF;
13602 	 END IF;
13603 
13604        END IF;
13605         /* Added for bug 13829182 */
13606     ELSIF ( (l_to_rsv_rec.demand_source_type_id = inv_reservation_global.g_source_type_wip) AND (l_pjm_enabled = 1))  THEN
13607         BEGIN
13608             SELECT we.entity_type, wdj.maintenance_object_source
13609                 INTO l_wip_entity_id, l_maintenance_object_source
13610               FROM wip_entities we, wip_discrete_jobs wdj
13611            WHERE we.wip_entity_id = l_to_rsv_rec.demand_source_header_id
13612                 AND we.wip_entity_id = wdj.wip_entity_id(+);
13613         EXCEPTION
13614             WHEN no_data_found THEN
13615                 IF (l_debug = 1) THEN
13616                     debug_print('No WIP entity record found for the source header passed' );
13617                 END IF;
13618         END;
13619 
13620         IF l_wip_entity_id = 6 and l_maintenance_object_source = 2  then
13621              l_wip_entity_type := inv_reservation_global.g_wip_source_type_cmro;
13622              l_wip_job_type := 'CMRO'; -- AHL
13623         ELSE
13624              l_wip_entity_type := null;
13625              l_wip_job_type := null; -- AHL
13626         END IF;
13627 
13628         IF ( l_wip_job_type = 'CMRO' AND l_to_rsv_rec.demand_source_line_detail IS NOT NULL)
13629                 AND (l_to_rsv_rec.demand_source_line_detail <> fnd_api.g_miss_num)
13630                 AND ((l_to_rsv_rec.project_id = fnd_api.g_miss_num) OR (l_to_rsv_rec.task_id = fnd_api.g_miss_num)) THEN
13631 
13632                     IF l_to_rsv_rec.demand_source_line_detail = g_sch_mat_id THEN
13633                             l_project_id := g_project_id;
13634                             l_task_id    := g_task_id;
13635                     ELSE
13636                         BEGIN
13637                             SELECT wdj.project_id, WDJ.TASK_ID
13638                                 INTO l_project_id, l_task_id
13639                                FROM ahl_schedule_materials asmt, ahl_workorders aw, WIP_DISCRETE_JOBS WDJ
13640                             WHERE asmt.scheduled_material_id = l_to_rsv_rec.demand_source_line_detail
13641                                  AND asmt.visit_task_id           = aw.visit_task_id
13642                                  AND ASMT.VISIT_ID                = AW.VISIT_ID
13643                                  AND aw.wip_entity_id             = wdj.wip_entity_id
13644                                  AND AW.STATUS_CODE              IN ('1','3') -- 1:Unreleased,3:Released
13645                                  AND ASMT.STATUS                  = 'ACTIVE';
13646                         EXCEPTION
13647                             WHEN others THEN
13648                                 IF l_debug=1 THEN
13649                                     debug_print('Cannot find the project and task information from CMRO WO: '||sqlerrm);
13650                                 END IF;
13651                         END;
13652                     END IF;
13653                     IF (l_to_rsv_rec.project_id = fnd_api.g_miss_num) THEN
13654                         IF (l_project_id IS NOT NULL ) THEN
13655                             l_to_rsv_rec.project_id := l_project_id;
13656                         ELSE
13657                             l_to_rsv_rec.project_id := NULL;
13658                         END IF;
13659                     END IF;
13660 
13661                     IF (l_to_rsv_rec.task_id = fnd_api.g_miss_num) THEN
13662                         IF (l_task_id IS NOT NULL) THEN
13663                             l_to_rsv_rec.task_id := l_task_id;
13664                         ELSE
13665                             l_to_rsv_rec.task_id := NULL;
13666                         END IF;
13667                     END IF;
13668         END IF;  -- l_wip_job_type = 'CMRO'
13669         /* End of changes for bug 13829182 */
13670      ELSE -- not project enable
13671 	  l_to_rsv_rec.project_id := NULL;
13672 	  l_to_rsv_rec.task_id := NULL;
13673     END IF;
13674     IF l_debug=1 THEN
13675        debug_print('After Rsv rec project id: ' || l_to_rsv_rec.project_id);
13676        debug_print('After Rsv rec task id: ' || l_to_rsv_rec.task_id);
13677     END IF;
13678     /*** End R12 ***/
13679 
13680 
13681     -- INVCONV - Establish whether item is tracked in dual UOMs
13682     IF l_orig_rsv_tbl(1).secondary_uom_code is NOT NULL THEN
13683       l_dual_tracking := TRUE;
13684     END IF;
13685     -- INVCONV END
13686     --MUOM Fulfillment Project
13687     inv_utilities.get_inv_fulfillment_base(
13688                p_source_line_id        => l_orig_rsv_tbl(1).demand_source_line_id,
13689                p_demand_source_type_id =>l_orig_rsv_tbl(1).demand_source_type_id,
13690                p_org_id                => l_orig_rsv_tbl(1).organization_id,
13691                x_fulfillment_base      => l_fulfill_base
13692     );
13693 
13694     --
13695     -- see whether we are transferring all quantity
13696     -- or just partial
13697     IF l_fulfill_base <> 'S' THEN
13698       l_transfer_all   := (l_orig_rsv_tbl(1).primary_reservation_quantity = l_to_rsv_rec.primary_reservation_quantity);
13699 	ELSE
13700       l_transfer_all   := (l_orig_rsv_tbl(1).secondary_reservation_quantity = l_to_rsv_rec.secondary_reservation_quantity);
13701     END IF;
13702 
13703 
13704     /**** {{ R12 Enhanced reservations code changes }}****/
13705     l_original_serial_count := p_original_serial_number.COUNT;
13706     l_to_serial_count := p_to_serial_number.COUNT;
13707     -- set the parameter value to handle serial numbers.
13708     IF (l_original_serial_count = 0 AND l_to_serial_count = 0) THEN
13709        l_serial_param := 1;
13710      ELSIF (l_original_serial_count > 0 AND l_to_serial_count = 0) THEN
13711        l_serial_param := 2;
13712      ELSIF (l_original_serial_count = 0 AND l_to_serial_count > 0) THEN
13713        l_serial_param := 3;
13714      ELSIF (l_original_serial_count > 0 AND l_to_serial_count > 0) THEN
13715        l_serial_param := 4;
13716      ELSE
13717        -- indeterminate value.error
13718        IF (l_debug = 1) THEN
13719 	  debug_print('Cannot determine what is being passed to the serial tables');
13720        END IF;
13721        fnd_message.set_name('INV', 'INV_INVALID_SERIAL_TABLES');
13722        fnd_msg_pub.ADD;
13723        RAISE fnd_api.g_exc_error;
13724     END IF;
13725 
13726     IF (l_debug = 1) THEN
13727        debug_print('Serial Param' || l_serial_param);
13728        debug_print('Original serial count' || l_original_serial_count);
13729        debug_print('To serial count' || l_to_serial_count);
13730     END IF;
13731 
13732     -- if from and to serials are passed, then pass them to the validate API
13733     IF l_original_serial_count > 0 THEN
13734        l_original_serial_number := p_original_serial_number;
13735     END IF;
13736     IF l_to_serial_count > 0 THEN
13737        l_to_serial_number := p_to_serial_number;
13738     END IF;
13739     /*** End R12 ***/
13740     IF (l_debug = 1) THEN
13741        debug_print('Before calling validate');
13742     END IF;
13743 
13744     IF p_validation_flag = fnd_api.g_true THEN
13745       -- we do validation after the query because
13746       -- for transfer, we might have many input value set to
13747       -- missing. We need to use the actual value to do
13748       -- validation
13749        inv_reservation_validate_pvt.validate_input_parameters
13750 	 (
13751 	  x_return_status              => l_return_status
13752 	  , p_orig_rsv_rec               => l_orig_rsv_tbl(1)
13753 	  , p_to_rsv_rec                 => l_to_rsv_rec
13754 	  /**** {{ R12 Enhanced reservations code changes }}****/
13755 	  , p_orig_serial_array          => l_original_serial_number
13756 	  , p_to_serial_array            => l_to_serial_number
13757 	  /*** End R12 ***/
13758 	  , p_rsv_action_name            => 'TRANSFER'
13759 	  , x_orig_item_cache_index      => l_orig_item_cache_index
13760 	  , x_orig_org_cache_index       => l_orig_org_cache_index
13761 	  , x_orig_demand_cache_index    => l_orig_demand_cache_index
13762 	  , x_orig_supply_cache_index    => l_orig_supply_cache_index
13763 	  , x_orig_sub_cache_index       => l_orig_sub_cache_index
13764 	  , x_to_item_cache_index        => l_to_item_cache_index
13765 	  , x_to_org_cache_index         => l_to_org_cache_index
13766 	  , x_to_demand_cache_index      => l_to_demand_cache_index
13767 	  , x_to_supply_cache_index      => l_to_supply_cache_index
13768 	  , x_to_sub_cache_index         => l_to_sub_cache_index
13769 	  );
13770 
13771        --
13772        IF l_return_status = fnd_api.g_ret_sts_error THEN
13773 	  RAISE fnd_api.g_exc_error;
13774        END IF;
13775 
13776        --
13777        IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13778 	  RAISE fnd_api.g_exc_unexpected_error;
13779        END IF;
13780 
13781        IF (l_debug = 1) THEN
13782 	  debug_print('After calling validate' || l_return_status);
13783        END IF;
13784 
13785        /**** {{ R12 Enhanced reservations code changes }}****/
13786     END IF;
13787 
13788     IF p_validation_flag = fnd_api.g_true AND
13789       (l_to_rsv_rec.supply_source_type_id = inv_reservation_global.g_source_type_inv) THEN
13790 
13791        /*** End R12 ***/
13792        -- INVCONV BEGIN
13793        IF NOT is_lot_divisible(l_orig_item_cache_index) THEN
13794 	  l_lot_divisible_flag := 'N';
13795 	  IF (l_debug = 1) THEN
13796 	     debug_print('Lot indivisible is TRUE ');
13797 	  END IF;
13798        END IF;
13799        -- INVCONV END
13800 
13801        inv_quantity_tree_pvt.create_tree
13802 	 (
13803 	  p_api_version_number         => 1.0
13804 	  , p_init_msg_lst               => fnd_api.g_true
13805 	  , x_return_status              => l_return_status
13806 	  , x_msg_count                  => x_msg_count
13807 	  , x_msg_data                   => x_msg_data
13808 	  , p_organization_id            => l_orig_rsv_tbl(1).organization_id
13809 	  , p_inventory_item_id          => l_orig_rsv_tbl(1).inventory_item_id
13810 	  , p_tree_mode                  => inv_quantity_tree_pvt.g_reservation_mode
13811 	  , p_is_revision_control        => is_revision_control(l_orig_item_cache_index)
13812 	  , p_is_lot_control             => is_lot_control(l_orig_item_cache_index)
13813 	  , p_is_serial_control          => is_serial_control(l_orig_item_cache_index)
13814 	  , p_asset_sub_only             => FALSE
13815 	  , p_include_suggestion         => TRUE
13816 	  , p_demand_source_type_id      => l_orig_rsv_tbl(1).demand_source_type_id
13817 	  , p_demand_source_header_id    => l_orig_rsv_tbl(1).demand_source_header_id
13818 	  , p_demand_source_line_id      => l_orig_rsv_tbl(1).demand_source_line_id
13819 	 , p_demand_source_name         => l_orig_rsv_tbl(1).demand_source_name
13820 	 , p_demand_source_delivery     => l_orig_rsv_tbl(1).demand_source_delivery
13821 	 , p_lot_expiration_date        => SYSDATE -- Bug#2716563
13822 	 , x_tree_id                    => l_tree_id1
13823 	 );
13824 
13825        --
13826        IF l_return_status = fnd_api.g_ret_sts_error THEN
13827 	  RAISE fnd_api.g_exc_error;
13828        END IF;
13829 
13830        --
13831        IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13832 	  RAISE fnd_api.g_exc_unexpected_error;
13833        END IF;
13834 
13835        --
13836        inv_quantity_tree_pvt.create_tree
13837 	 (
13838 	  p_api_version_number         => 1.0
13839 	  , p_init_msg_lst               => fnd_api.g_true
13840 	  , x_return_status              => l_return_status
13841 	  , x_msg_count                  => x_msg_count
13842 	  , x_msg_data                   => x_msg_data
13843 	  , p_organization_id            => l_to_rsv_rec.organization_id
13844 	  , p_inventory_item_id          => l_to_rsv_rec.inventory_item_id
13845 	  , p_tree_mode                  => inv_quantity_tree_pvt.g_reservation_mode
13846 	  , p_is_revision_control        => is_revision_control(l_to_item_cache_index)
13847 	  , p_is_lot_control             => is_lot_control(l_to_item_cache_index)
13848 	  , p_is_serial_control          => is_serial_control(l_to_item_cache_index)
13849 	  , p_asset_sub_only             => FALSE
13850 	  , p_include_suggestion         => TRUE
13851 	  , p_demand_source_type_id      => l_to_rsv_rec.demand_source_type_id
13852 	  , p_demand_source_header_id    => l_to_rsv_rec.demand_source_header_id
13853 	  , p_demand_source_line_id      => l_to_rsv_rec.demand_source_line_id
13854 	  , p_demand_source_name         => l_to_rsv_rec.demand_source_name
13855 	 , p_demand_source_delivery     => l_to_rsv_rec.demand_source_delivery
13856 	 , p_lot_expiration_date        => SYSDATE -- Bug#2716563
13857 	 , x_tree_id                    => l_tree_id2
13858 	 );
13859 
13860        --
13861        IF l_return_status = fnd_api.g_ret_sts_error THEN
13862 	  RAISE fnd_api.g_exc_error;
13863        END IF;
13864 
13865        --
13866        IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13867 	  RAISE fnd_api.g_exc_unexpected_error;
13868        END IF;
13869 
13870 	   -- MUOM Fulfillment project
13871 	  IF  l_orig_rsv_tbl(1).supply_source_type_id = inv_reservation_global.g_source_type_po
13872         AND l_to_rsv_rec.supply_source_type_id = inv_reservation_global.g_source_type_inv
13873         AND l_fulfill_base = 'S' AND l_transfer_all THEN
13874 
13875               inv_quantity_tree_pvt.query_tree(
13876                p_api_version_number         => 1.0
13877              , p_init_msg_lst               => fnd_api.g_true
13878              , x_return_status              => l_return_status
13879              , x_msg_count                  => x_msg_count
13880              , x_msg_data                   => x_msg_data
13881              , p_tree_id                    => l_tree_id2
13882              , p_revision                   => l_to_rsv_rec.revision
13883              , p_lot_number                 => l_to_rsv_rec.lot_number
13884              , p_subinventory_code          => l_to_rsv_rec.subinventory_code
13885              , p_locator_id                 => l_to_rsv_rec.locator_id
13886              , x_qoh                        => l_qoh
13887              , x_rqoh                       => l_rqoh
13888              , x_qr                         => l_qr
13889              , x_qs                         => l_qs
13890              , x_att                        => l_att
13891              , x_atr                        => l_atr
13892              , x_sqoh                       => l_sqoh
13893              , x_srqoh                      => l_srqoh
13894              , x_sqr                        => l_sqr
13895              , x_sqs                        => l_sqs
13896              , x_satt                       => l_satt
13897              , x_satr                       => l_satr
13898              , p_lpn_id                     => l_to_rsv_rec.lpn_id
13899              );
13900 
13901              l_to_rsv_rec.primary_reservation_quantity := l_atr;
13902       END IF;
13903 	  -- End MUOM Fulfillment project
13904 
13905        --
13906        -- INVCONV - upgrade call to incorporate secondaries
13907        modify_tree_for_update_xfer
13908 	 (
13909 	  x_return_status              => l_return_status
13910 	  , x_quantity_reserved          => l_quantity_reserved
13911 	  , x_secondary_quantity_reserved => l_secondary_quantity_reserved
13912 	  , p_from_tree_id               => l_tree_id1
13913 	  , p_from_supply_source_type_id => l_orig_rsv_tbl(1).supply_source_type_id
13914 	  , p_from_revision              => l_orig_rsv_tbl(1).revision
13915 	  , p_from_lot_number            => l_orig_rsv_tbl(1).lot_number
13916 	  , p_from_subinventory_code     => l_orig_rsv_tbl(1).subinventory_code
13917 	  , p_from_locator_id            => l_orig_rsv_tbl(1).locator_id
13918 	  , p_from_lpn_id                => l_orig_rsv_tbl(1).lpn_id
13919 	  , p_from_primary_rsv_quantity  => l_orig_rsv_tbl(1).primary_reservation_quantity
13920 	  , p_from_second_rsv_quantity   => l_orig_rsv_tbl(1).secondary_reservation_quantity
13921 	  , p_from_detailed_quantity     => l_orig_rsv_tbl(1).detailed_quantity
13922 	  , p_from_sec_detailed_quantity => l_orig_rsv_tbl(1).secondary_detailed_quantity
13923 	  , p_to_tree_id                 => l_tree_id2
13924 	 , p_to_supply_source_type_id   => l_to_rsv_rec.supply_source_type_id
13925 	 , p_to_revision                => l_to_rsv_rec.revision
13926 	 , p_to_lot_number              => l_to_rsv_rec.lot_number
13927 	 , p_to_subinventory_code       => l_to_rsv_rec.subinventory_code
13928 	 , p_to_locator_id              => l_to_rsv_rec.locator_id
13929 	 , p_to_lpn_id                  => l_to_rsv_rec.lpn_id
13930 	 , p_to_primary_rsv_quantity    => l_to_rsv_rec.primary_reservation_quantity
13931 	 , p_to_second_rsv_quantity     => l_to_rsv_rec.secondary_reservation_quantity
13932 	 , p_to_detailed_quantity       => l_to_rsv_rec.detailed_quantity
13933 	 , p_to_second_detailed_quantity => l_to_rsv_rec.secondary_detailed_quantity
13934 	 , p_to_revision_control        => is_revision_control(l_to_item_cache_index)
13935 	 , p_to_lot_control             => is_lot_control(l_to_item_cache_index)
13936 	 , p_action                     => 'TRANSFER'
13937 	 , p_lot_divisible_flag         => l_lot_divisible_flag    -- INVCONV
13938 	 , p_partial_reservation_flag   => fnd_api.g_false
13939 	 , p_check_availability         => fnd_api.g_false
13940 	 );
13941 
13942 
13943        IF l_return_status = fnd_api.g_ret_sts_error THEN
13944 	  RAISE fnd_api.g_exc_error;
13945        END IF;
13946 
13947        --
13948        IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13949 	  RAISE fnd_api.g_exc_unexpected_error;
13950        END IF;
13951 
13952        /**** {{ R12 Enhanced reservations code changes }}****/
13953      ELSIF (l_to_rsv_rec.supply_source_type_id IN
13954 	    (inv_reservation_global.g_source_type_wip, inv_reservation_global.g_source_type_po,
13955 	     inv_reservation_global.g_source_type_asn,
13956 	     inv_reservation_global.g_source_type_intransit,
13957 	     inv_reservation_global.g_source_type_internal_req,
13958 	     inv_reservation_global.g_source_type_rcv)) AND
13959        p_over_reservation_flag NOT IN (1,3) THEN
13960 
13961 	   --In xdock case, splitting the reservation calls transfer reservation during deliver operation.
13962 	   --However, RTP has already processed and decremented the receiving supply, so the call to
13963 	   --get_supply_reservable_qty is not needed -- Added for bug 9879753 - Start
13964 
13965 	   	   IF  (l_original_rsv_rec.supply_source_type_id=inv_reservation_global.g_source_type_rcv
13966 			AND l_to_rsv_rec.supply_source_type_id=inv_reservation_global.g_source_type_rcv
13967 			AND l_original_rsv_rec.external_source_code='XDOCK' AND l_to_rsv_rec.external_source_code='XDOCK') THEN
13968 
13969 			NULL;
13970 
13971 		   ELSE
13972 
13973        -- call the helper procedure to get the reservable qty of the supply
13974        -- Bug 5199672: Should pass g_miss_num as default for supply
13975        -- source line detail. Otherwise, high level reservations
13976        -- will not be considered.
13977        get_supply_reservable_qty
13978          (
13979             x_return_status                 => l_return_status
13980           , x_msg_count                     => x_msg_count
13981           , x_msg_data                      => x_msg_data
13982           , p_fm_supply_source_type_id      => l_orig_rsv_tbl(1).supply_source_type_id
13983           , p_fm_supply_source_header_id    => l_orig_rsv_tbl(1).supply_source_header_id
13984           , p_fm_supply_source_line_id      => l_orig_rsv_tbl(1).supply_source_line_id
13985           , p_fm_supply_source_line_detail  => l_orig_rsv_tbl(1).supply_source_line_detail
13986           , p_fm_primary_reservation_qty    => l_orig_rsv_tbl(1).primary_reservation_quantity
13987           , p_to_supply_source_type_id      => l_to_rsv_rec.supply_source_type_id
13988           , p_to_supply_source_header_id    => l_to_rsv_rec.supply_source_header_id
13989           , p_to_supply_source_line_id      => l_to_rsv_rec.supply_source_line_id
13990           , p_to_supply_source_line_detail  => l_to_rsv_rec.supply_source_line_detail
13991           , p_to_primary_reservation_qty    => l_to_rsv_rec.primary_reservation_quantity
13992           , p_to_organization_id            => l_to_rsv_rec.organization_id
13993           , p_to_inventory_item_id          => l_to_rsv_rec.inventory_item_id
13994           , p_to_revision                   => l_to_rsv_rec.revision
13995           , p_to_lot_number                 => l_to_rsv_rec.lot_number
13996           , p_to_subinventory_code          => l_to_rsv_rec.subinventory_code
13997           , p_to_locator_id                 => l_to_rsv_rec.locator_id
13998           , p_to_lpn_id                     => l_to_rsv_rec.lpn_id
13999           , p_to_project_id                 => l_to_rsv_rec.project_id
14000           , p_to_task_id                    => l_to_rsv_rec.task_id
14001 	 , x_reservable_qty                => l_quantity_reserved
14002 	 , x_qty_available                 => l_qty_available
14003          );
14004 
14005        IF (l_debug = 1) THEN
14006 	  debug_print('After calling available supply to reserve ' || l_return_status);
14007 	  debug_print('Available quantity to reserve. l_quantity_reserved: ' || l_quantity_reserved);
14008        END IF;
14009 
14010        --
14011        IF l_return_status = fnd_api.g_ret_sts_error THEN
14012 	  RAISE fnd_api.g_exc_error;
14013        END IF;
14014 
14015        --
14016        IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14017 	  RAISE fnd_api.g_exc_unexpected_error;
14018        END IF;
14019        --MUOM Fulfillment Project
14020        inv_utilities.get_inv_fulfillment_base(
14021                p_source_line_id        => l_orig_rsv_tbl(1).demand_source_line_id,
14022                p_demand_source_type_id =>l_orig_rsv_tbl(1).demand_source_type_id,
14023                p_org_id                => l_orig_rsv_tbl(1).organization_id,
14024                x_fulfillment_base      => l_fulfill_base
14025        );
14026 
14027 	   IF l_fulfill_base = 'S' AND l_to_rsv_rec.supply_source_type_id <> inv_reservation_global.g_source_type_inv
14028 	   AND l_orig_rsv_tbl(1).supply_source_type_id = inv_reservation_global.g_source_type_inv THEN
14029 	       l_to_rsv_rec.primary_reservation_quantity := inv_convert.inv_um_convert(
14030                                   item_id                      => l_to_rsv_rec.inventory_item_id
14031                                 , lot_number                   => NULL
14032                                 , organization_id              => l_to_rsv_rec.organization_id
14033                                 , PRECISION                    => NULL -- use default precision
14034                                 , from_quantity                => l_to_rsv_rec.secondary_reservation_quantity
14035                                 , from_unit                    => l_to_rsv_rec.secondary_uom_code
14036                                 , to_unit                      => l_to_rsv_rec.primary_uom_code
14037                                 , from_name                    => NULL -- from uom name
14038                                 , to_name                      => NULL -- to uom name
14039                                 );
14040 	   END IF;
14041 
14042        IF ((l_to_rsv_rec.primary_reservation_quantity - l_quantity_reserved)
14043 	 > 0.000005) THEN
14044 
14045           IF (l_debug = 1) THEN
14046              debug_print('The supply document doesnt have enough quantity to be reserved against. error out. ');
14047              debug_print('l_to_rsv_rec.primary_reservation_quantity: ' || l_to_rsv_rec.primary_reservation_quantity);
14048           END IF;
14049           fnd_message.set_name('INV', 'INV_INVALID_AVAILABLE_QTY');
14050           fnd_msg_pub.ADD;
14051           RAISE fnd_api.g_exc_error;
14052 
14053        END IF;
14054        END IF; --xdock split - Bug 9879753 End
14055     END IF;
14056 
14057    IF (l_debug = 1) THEN
14058        debug_print('From record:');
14059        debug_print('demand_source_type_id = ' || l_orig_rsv_tbl(1).demand_source_type_id);
14060        debug_print('demand_source_header_id = ' || l_orig_rsv_tbl(1).demand_source_header_id);
14061        debug_print('demand_source_line_id = ' || l_orig_rsv_tbl(1).demand_source_line_id);
14062        debug_print('demand_source_line_detail = ' || l_orig_rsv_tbl(1).demand_source_line_detail);
14063        debug_print('To record:');
14064        debug_print('demand_source_type_id = ' || l_to_rsv_rec.demand_source_type_id);
14065        debug_print('demand_source_header_id = ' || l_to_rsv_rec.demand_source_header_id);
14066        debug_print('demand_source_line_id = ' || l_to_rsv_rec.demand_source_line_id);
14067        debug_print('demand_source_line_detail = ' || l_to_rsv_rec.demand_source_line_detail);
14068     END IF;
14069 
14070     IF (l_to_rsv_rec.demand_source_type_id IN
14071 	(inv_reservation_global.g_source_type_wip,
14072 	 inv_reservation_global.g_source_type_oe,
14073 	 inv_reservation_global.g_source_type_internal_ord,
14074 	 inv_reservation_global.g_source_type_rma)) AND
14075       p_over_reservation_flag NOT IN (2,3) THEN
14076        -- call the helper procedure to get the reservable qty of the demand
14077        -- Bug 5199672: Should pass g_miss_num as default for demand
14078        -- source line detail. Otherwise, high level reservations
14079        -- will not be considered.
14080 
14081        IF (l_fulfill_base = 'S') THEN
14082            l_org_sec_rsv_qty :=l_orig_rsv_tbl(1).secondary_reservation_quantity;  -- MUOM fulfillment Project
14083       ELSE
14084            l_org_sec_rsv_qty:=null;
14085       END IF;
14086        --
14087        IF (l_debug = 1) THEN
14088          debug_print('l_org_sec_rsv_qty : ' || l_org_sec_rsv_qty);
14089        END IF;
14090         --
14091        get_demand_reservable_qty
14092          (
14093             x_return_status                 => l_return_status
14094           , x_msg_count                     => x_msg_count
14095           , x_msg_data                      => x_msg_data
14096           , p_fm_demand_source_type_id      => l_orig_rsv_tbl(1).demand_source_type_id
14097           , p_fm_demand_source_header_id    => l_orig_rsv_tbl(1).demand_source_header_id
14098           , p_fm_demand_source_line_id      => l_orig_rsv_tbl(1).demand_source_line_id
14099           , p_fm_demand_source_line_detail  => l_orig_rsv_tbl(1).demand_source_line_detail
14100           , p_fm_primary_reservation_qty    => l_orig_rsv_tbl(1).primary_reservation_quantity
14101           , p_fm_secondary_reservation_qty  => l_org_sec_rsv_qty
14102           , p_to_demand_source_type_id      => l_to_rsv_rec.demand_source_type_id
14103           , p_to_demand_source_header_id    => l_to_rsv_rec.demand_source_header_id
14104           , p_to_demand_source_line_id      => l_to_rsv_rec.demand_source_line_id
14105           , p_to_demand_source_line_detail  => l_to_rsv_rec.demand_source_line_detail
14106           , p_to_primary_reservation_qty    => l_to_rsv_rec.primary_reservation_quantity
14107           , p_to_organization_id            => l_to_rsv_rec.organization_id
14108           , p_to_inventory_item_id          => l_to_rsv_rec.inventory_item_id
14109           , p_to_primary_uom_code           => l_to_rsv_rec.primary_uom_code
14110           , p_to_project_id                 => l_to_rsv_rec.project_id
14111           , p_to_task_id                    => l_to_rsv_rec.task_id
14112 	  , x_reservable_qty       => l_quantity_reserved
14113 	  , x_qty_available         => l_qty_available
14114 	  , x_reservable_qty2     => l_quantity_reserved2
14115           , x_qty_available2        => l_qty_available2
14116           );
14117 
14118        IF (l_debug = 1) THEN
14119 	  debug_print('After calling available demand to reserve ' || l_return_status);
14120 	  debug_print('Available quantity to reserve. l_quantity_reserved: ' || l_quantity_reserved);
14121 	  debug_print('Available quantity to reserve. l_quantity_reserved2: ' || l_quantity_reserved2);
14122        END IF;
14123 
14124        --
14125        IF l_return_status = fnd_api.g_ret_sts_error THEN
14126 	  RAISE fnd_api.g_exc_error;
14127        END IF;
14128 
14129        --
14130        IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14131 	  RAISE fnd_api.g_exc_unexpected_error;
14132        END IF;
14133 
14134       --Bug 12978409 : start
14135        -- Bug 5199672: Removed the condition l_qty_changed > 0
14136       /*  IF ((l_to_rsv_rec.primary_reservation_quantity - l_quantity_reserved)
14137                  > 0.000005) THEN */
14138           get_reservation_qty_lot(
14139                   p_rsv_rec              => l_to_rsv_rec,
14140                   p_reservation_qty_lot  => l_reservation_qty_lot);
14141       -- MUOM fulfillment Project
14142       IF (l_fulfill_base = 'S') THEN
14143 
14144 	IF((l_reservation_qty_lot-l_quantity_reserved2)>0.000005) THEN
14145 	  --
14146           IF (l_debug = 1) THEN
14147             debug_print('The demand document doesnt have enough quantity to be reserved against. error out. for fulfilment base=S ');
14148             debug_print('l_quantity_reserved2 : ' || l_quantity_reserved2);
14149             debug_print('l_reservation_qty_lot : ' || l_reservation_qty_lot);
14150           END IF;
14151           fnd_message.set_name('INV', 'INV_INVALID_AVAILABLE_QTY');
14152           fnd_msg_pub.add;
14153           raise fnd_api.g_exc_error;
14154         END IF;
14155       ELSE
14156         IF ((l_reservation_qty_lot - l_quantity_reserved) > 0.000005) THEN
14157           IF (l_debug = 1) THEN
14158             debug_print('The demand document doesnt have enough quantity to be reserved against. error out. ');
14159             debug_print('l_to_rsv_rec.primary_reservation_quantity: ' || l_to_rsv_rec.primary_reservation_quantity);
14160           END IF;
14161           fnd_message.set_name('INV', 'INV_INVALID_AVAILABLE_QTY');
14162           fnd_msg_pub.add;
14163           raise fnd_api.g_exc_error;
14164         END IF;
14165       END IF;
14166       -- MUOM fulfillment Project
14167       END IF;
14168     /*** End R12 ***/
14169 
14170     IF (l_debug = 1) THEN
14171        debug_print('After calling create/modify tree ' || l_return_status);
14172     END IF;
14173 
14174     -- obtain program and user info
14175     l_date := SYSDATE;
14176 
14177     --
14178     l_user_id        := fnd_global.user_id;
14179     l_login_id       := fnd_global.login_id;
14180 
14181     IF l_login_id = -1 THEN
14182       l_login_id  := fnd_global.conc_login_id;
14183     END IF;
14184 
14185     l_request_id     := fnd_global.conc_request_id;
14186     l_prog_appl_id   := fnd_global.prog_appl_id;
14187     l_program_id     := fnd_global.conc_program_id;
14188 
14189     --
14190     --  actions based on l_transfer_all and l_to_row_exist
14191     --  l_transfer_all    l_to_row_exist     from row            to row
14192     --    true                true            delete             add qty
14193     --    true                false           update all but id  nothing
14194     --    false               true            reduce qty         add qty
14195     --    false               false           reduce qty         create
14196     --
14197     -- for from row
14198     IF l_transfer_all = FALSE THEN
14199        IF (l_debug = 1) THEN
14200           debug_print('Transfer all is false');
14201        END IF;
14202       -- Pre Update CTO Validation
14203       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
14204 	 --
14205 	IF (l_debug = 1) THEN
14206            debug_print('Before calling cto work flow unresv check');
14207         END IF;
14208         cto_workflow_api_pk.inventory_unreservation_check(
14209           p_order_line_id              => l_orig_rsv_tbl(1).demand_source_line_id
14210         , p_rsv_quantity               => l_to_rsv_rec.primary_reservation_quantity
14211         , x_return_status              => l_return_status
14212         , x_msg_count                  => x_msg_count
14213         , x_msg_data                   => x_msg_data
14214         );
14215 
14216         --
14217         IF l_return_status = fnd_api.g_ret_sts_error THEN
14218           RAISE fnd_api.g_exc_error;
14219         END IF;
14220 
14221         --
14222         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14223           RAISE fnd_api.g_exc_unexpected_error;
14224         END IF;
14225 	--
14226 	IF (l_debug = 1) THEN
14227            debug_print('After calling cto work flow unresv check');
14228         END IF;
14229       END IF;
14230 
14231       -- I don't call the table handler since only the quantities are changed
14232       -- along with who columns
14233 
14234       /*
14235       ** Include detailed quantity too in the update...
14236       */
14237 
14238       IF (l_orig_rsv_tbl(1).detailed_quantity IS NULL) THEN
14239         l_detailed_quantity  := NULL;
14240       END IF;
14241 
14242       IF (l_orig_rsv_tbl(1).detailed_quantity = 0) THEN
14243         l_detailed_quantity  := 0;
14244       END IF;
14245 
14246       IF (l_orig_rsv_tbl(1).detailed_quantity > 0) THEN
14247         l_detailed_quantity  := l_orig_rsv_tbl(1).detailed_quantity - l_to_rsv_rec.primary_reservation_quantity;
14248 
14249         -- INVCONV BEGIN -  populate secondary for dual tracked item
14250         IF l_dual_tracking THEN
14251 
14252           /* Fix for Bug#12837088 . Calculate secondary from primary as mathematical subtraction can be negative or out of deviation */
14253          /* uncommenting the fix done for Bug#12837088 MUOM Fulfillment Project*/
14254 		  l_primary_uom_code   := l_to_rsv_rec.primary_uom_code; -- 13604458
14255           l_secondary_detailed_quantity  :=
14256              l_orig_rsv_tbl(1).secondary_detailed_quantity - l_to_rsv_rec.secondary_reservation_quantity;
14257 
14258 		  --BUG12622871
14259 			IF (l_debug = 1) THEN
14260 			debug_print(' Adding debug msg before UOM convert place 1');
14261 			debug_print(' Value of item id is '||To_char(l_orig_rsv_tbl(1).inventory_item_id));
14262 			debug_print(' Value of lot is '||To_char(l_orig_rsv_tbl(1).lot_number));
14263 			debug_print(' Value of org is '||To_char(l_orig_rsv_tbl(1).organization_id));
14264 			debug_print(' Value of l_detailed_quantity is '||To_char(l_detailed_quantity));
14265 			debug_print(' Value of l_primary_uom_code is '||To_char(l_primary_uom_code));
14266 			debug_print(' Value of l_orig_rsv_tbl(1).secondary_uom_code is '||to_char(l_orig_rsv_tbl(1).secondary_uom_code));
14267 			END IF;
14268 
14269 			IF(NVL(l_detailed_quantity,0) <> 0) THEN
14270 			  IF(l_secondary_detailed_quantity <0) THEN -- MUOM Fulfillment Project
14271 				l_secondary_detailed_quantity  := inv_convert.inv_um_convert(
14272                                  item_id                   => l_orig_rsv_tbl(1).inventory_item_id
14273                                , lot_number             => l_orig_rsv_tbl(1).lot_number
14274                                , organization_id        => l_orig_rsv_tbl(1).organization_id
14275                                , PRECISION            => NULL -- use default precision
14276                                , from_quantity           => l_detailed_quantity
14277                                , from_unit                  => l_primary_uom_code
14278                                , to_unit                      => l_orig_rsv_tbl(1).secondary_uom_code
14279                                , from_name                => NULL -- from uom name
14280                                , to_name                     => NULL -- to uom name
14281                                );
14282 
14283 							   /* Start 13604458 */
14284                            IF (l_secondary_detailed_quantity  = -99999) THEN
14285                                -- conversion failed
14286                               IF (l_debug = 1) THEN
14287                                   debug_print('Conversion to SECONDARY UOM Failed');
14288                               END IF;
14289                               fnd_message.set_name('INV', 'INV_INVALID_UOM_CONV');
14290                               fnd_message.set_token('VALUE1', l_orig_rsv_tbl(1).secondary_uom_code);
14291                               fnd_message.set_token('VALUE2', l_primary_uom_code);
14292                               fnd_msg_pub.ADD;
14293                               RAISE fnd_api.g_exc_error;
14294                            END IF;
14295                            /* End 13604458 */
14296 			  END IF;
14297 			ELSE
14298 				IF(l_detailed_quantity IS NULL) THEN
14299 					l_secondary_detailed_quantity := NULL;
14300 				ELSE
14301 					l_secondary_detailed_quantity := 0;
14302 				END IF;
14303 			END IF;
14304 
14305 			IF (l_debug = 1) THEN
14306 		        	debug_print(' Value of l_secondary_detailed_quantity after uom convert1  is'||To_char(l_secondary_detailed_quantity));
14307 			END IF;
14308         END IF;
14309         -- INVCONV END
14310 
14311         IF (l_detailed_quantity < 0) THEN
14312           l_detailed_quantity  := 0;
14313           -- INVCONV BEGIN
14314           IF l_dual_tracking THEN
14315             l_secondary_detailed_quantity  := 0;
14316           END IF;
14317           -- INVCONV END
14318         END IF;
14319       END IF;
14320 
14321       --bug 2186857
14322       --The update statement below did a straight subtraction of the
14323       -- to rsv qty from the original rsv qty.  However, this did not take
14324       -- into account that the two reservations could have different
14325       -- reservation UOMs.  So, before the update, we must determine
14326       -- the new original reservation qty
14327       l_new_orig_prim_qty  := l_orig_rsv_tbl(1).primary_reservation_quantity - l_to_rsv_rec.primary_reservation_quantity;
14328       l_primary_uom_code   := l_to_rsv_rec.primary_uom_code;
14329 
14330       IF l_orig_rsv_tbl(1).reservation_uom_code IS NULL THEN
14331         l_reservation_uom_code  := l_primary_uom_code;
14332       ELSE
14333         l_reservation_uom_code  := l_orig_rsv_tbl(1).reservation_uom_code;
14334       END IF;
14335 
14336 	   --MUOM Fulfillment Project
14337             inv_utilities.get_inv_fulfillment_base(
14338                       p_source_line_id        => l_orig_rsv_tbl(1).demand_source_line_id,
14339                       p_demand_source_type_id =>l_orig_rsv_tbl(1).demand_source_type_id,
14340                       p_org_id                => l_orig_rsv_tbl(1).organization_id,
14341                       x_fulfillment_base      => l_fulfill_base
14342              );
14343 
14344       IF l_primary_uom_code <> l_reservation_uom_code and l_fulfill_base <> 'S' THEN
14345         -- INVCONV - Upgrade call to inv_um_convert to pass lot and org
14346         l_new_orig_rsv_qty  := inv_convert.inv_um_convert(
14347                                  item_id                      => l_orig_rsv_tbl(1).inventory_item_id
14348                                , lot_number                   => l_orig_rsv_tbl(1).lot_number
14349                                , organization_id              => l_orig_rsv_tbl(1).organization_id
14350                                , PRECISION                    => NULL -- use default precision
14351                                , from_quantity                => l_new_orig_prim_qty
14352                                , from_unit                    => l_primary_uom_code
14353                                , to_unit                      => l_reservation_uom_code
14354                                , from_name                    => NULL -- from uom name
14355                                , to_name                      => NULL -- to uom name
14356                                );
14357 
14358         IF l_new_orig_rsv_qty = -99999 THEN
14359           -- conversion failed
14360           fnd_message.set_name('INV', 'CAN-NOT-CONVERT-TO-PRIMARY-UOM');
14361           fnd_msg_pub.ADD;
14362           RAISE fnd_api.g_exc_error;
14363         END IF;
14364 	   -- MUOM Fulfillment project
14365       ELSIF l_orig_rsv_tbl(1).reservation_uom_code = l_orig_rsv_tbl(1).secondary_uom_code AND l_fulfill_base = 'S' THEN
14366         l_new_orig_rsv_qty  := l_orig_rsv_tbl(1).secondary_reservation_quantity - NVL(l_to_rsv_rec.secondary_reservation_quantity,0);
14367 	  ELSIF l_orig_rsv_tbl(1).reservation_uom_code <> l_primary_uom_code AND l_orig_rsv_tbl(1).reservation_uom_code <> l_orig_rsv_tbl(1).secondary_uom_code AND l_fulfill_base = 'S' THEN
14368              l_new_orig_rsv_qty    := inv_convert.inv_um_convert(
14369                                         item_id          => l_orig_rsv_tbl(1).inventory_item_id
14370                                       , lot_number       => l_orig_rsv_tbl(1).lot_number
14371                                       , organization_id  => l_orig_rsv_tbl(1).organization_id
14372                                       , precision        => null
14373                                       , from_quantity    => l_orig_rsv_tbl(1).secondary_reservation_quantity - NVL(l_to_rsv_rec.secondary_reservation_quantity,0)
14374                                       , from_unit        => l_orig_rsv_tbl(1).secondary_uom_code
14375                                       , to_unit          => l_orig_rsv_tbl(1).reservation_uom_code
14376                                       , from_name        => NULL
14377                                       , to_name          => NULL
14378                                        );
14379       ELSE
14380         l_new_orig_rsv_qty  := l_new_orig_prim_qty;
14381       END IF;
14382 
14383       -- INVCONV BEGIN
14384       IF l_dual_tracking THEN
14385 
14386        /* Fix for Bug#12837088 . Calculate secondary from primary as mathematical subtraction can be negative or out of deviation */
14387        -- 13604458 Calculate secondary only when l_orig_second_rsv_qty is negative
14388        l_orig_second_rsv_qty :=
14389          l_orig_rsv_tbl(1).secondary_reservation_quantity - NVL(l_to_rsv_rec.secondary_reservation_quantity,0);
14390 
14391 		--BUG12622871
14392 			IF (l_debug = 1) THEN
14393 			debug_print(' Adding debug msg before UOM convert place 2');
14394 			debug_print(' Value of item id is '||To_char(l_orig_rsv_tbl(1).inventory_item_id));
14395 			debug_print(' Value of lot is '||To_char(l_orig_rsv_tbl(1).lot_number));
14396 			debug_print(' Value of org is '||To_char(l_orig_rsv_tbl(1).organization_id));
14397 			debug_print(' Value of l_new_orig_rsv_qty is '||To_char(l_new_orig_rsv_qty));
14398 			debug_print(' Value of l_primary_uom_codeis '||To_char(l_primary_uom_code));
14399 			debug_print(' Value of l_orig_rsv_tbl(1).secondary_uom_code is '||To_char(l_orig_rsv_tbl(1).secondary_uom_code));
14400 			END IF;
14401 
14402 		IF(l_orig_rsv_tbl(1).reservation_uom_code = l_orig_rsv_tbl(1).secondary_uom_code) THEN
14403 			l_orig_second_rsv_qty := l_new_orig_rsv_qty;
14404 		ELSE
14405 		             -- 13604458. Calculate secondary only when l_orig_second_rsv_qty  is negative
14406                     IF (l_orig_second_rsv_qty < 0 ) THEN
14407 			         l_orig_second_rsv_qty  := inv_convert.inv_um_convert(
14408                                  item_id                      => l_orig_rsv_tbl(1).inventory_item_id
14409                                , lot_number                   => l_orig_rsv_tbl(1).lot_number
14410                                , organization_id              => l_orig_rsv_tbl(1).organization_id
14411                                , PRECISION                    => NULL -- use default precision
14412                                , from_quantity                => l_new_orig_rsv_qty
14413                                , from_unit                    => l_primary_uom_code
14414                                , to_unit                      => l_orig_rsv_tbl(1).secondary_uom_code
14415                                , from_name                    => NULL -- from uom name
14416                                , to_name                      => NULL -- to uom name
14417                                );
14418 							/* Start 13604458 */
14419                          IF (l_orig_second_rsv_qty  = -99999) THEN
14420                                -- conversion failed
14421                               IF (l_debug = 1) THEN
14422                                   debug_print('Conversion to SECONDARY UOM Failed');
14423                               END IF;
14424                               fnd_message.set_name('INV', 'INV_INVALID_UOM_CONV');
14425                               fnd_message.set_token('VALUE1', l_orig_rsv_tbl(1).secondary_uom_code);
14426                               fnd_message.set_token('VALUE2', l_primary_uom_code);
14427                               fnd_msg_pub.ADD;
14428                               RAISE fnd_api.g_exc_error;
14429                          END IF;
14430                          /* End 13604458 */
14431 				  END IF;
14432 		END IF;
14433 
14434 			IF (l_debug = 1) THEN
14435 			debug_print(' Value of l_orig_second_rsv_qty after uom convert is'||To_char(l_orig_second_rsv_qty));
14436 			END IF;
14437 
14438       END IF;
14439       -- INVCONV END
14440 
14441       --Bug #2819700
14442       --Adding an extra check to make sure that tranfer reservations does not
14443       -- update the original reservation record to a NEGATIVE NUMBER.
14444       IF (l_debug = 1) THEN
14445         debug_print('Primary_reservation_qty before inserting (xfer)= '
14446 		                || To_char(l_new_orig_prim_qty) );
14447         debug_print('Secondary_reservation_qty before inserting (xfer)= '
14448                                 || To_char(l_orig_second_rsv_qty) );        -- INVCONV
14449         debug_print('Reservation_qty before inserting (xfer)= '
14450 		                || To_char(l_new_orig_rsv_qty) );
14451       END IF;
14452 
14453       IF (  (NVL(l_new_orig_rsv_qty,0) < 0) OR
14454 	          (NVL(l_new_orig_prim_qty,0) < 0) ) THEN
14455         fnd_message.set_name('INV', 'INV-INVALID RESERVATION QTY');
14456         fnd_msg_pub.ADD;
14457         RAISE fnd_api.g_exc_error;
14458       END IF;
14459 
14460        -- INVCONV BEGIN
14461       IF (NVL(l_orig_second_rsv_qty,0) < 0) THEN
14462         fnd_message.set_name('INV', 'INV-INVALID NEGATIVE SECONDARY'); -- INVCONV New Message
14463         fnd_msg_pub.ADD;
14464         RAISE fnd_api.g_exc_error;
14465       END IF;
14466       -- INVCONV END
14467       -- Bug 3461990: Reservations API should not update reservations with more
14468       -- than 5 decimal places, since the transaction quantity is being
14469       -- rounded to 5 decimal places.
14470 
14471       l_new_orig_prim_qty :=
14472 	round(l_new_orig_prim_qty,5);
14473       -- INVCONV BEGIN
14474       IF l_dual_tracking THEN
14475         l_orig_second_rsv_qty :=
14476           round(l_orig_second_rsv_qty,5);
14477       END IF;
14478       -- INVCONV END
14479 
14480       l_new_orig_rsv_qty :=
14481 	round(l_new_orig_rsv_qty,5);
14482       l_detailed_quantity  :=
14483 	round(Nvl(l_detailed_quantity,0),5);
14484 
14485 
14486       IF (l_debug = 1) THEN
14487 	 debug_print(' Transfer: Before updating from record');
14488 	 debug_print(' After rounding reservation is' || l_orig_rsv_tbl(1).reservation_id);
14489 	 debug_print(' After rounding reservation qty' || l_new_orig_rsv_qty);
14490 	 debug_print(' After rounding reservation pri qty' || l_new_orig_prim_qty);
14491          debug_print(' After rounding reservation sec qty' || l_orig_second_rsv_qty);
14492 	 debug_print(' After rounding detailed quantity' || l_detailed_quantity);
14493       END IF;
14494 
14495       -- INVCONV - Incorporate secondary_reservation_quantity
14496       UPDATE mtl_reservations
14497          SET primary_reservation_quantity = l_new_orig_prim_qty
14498            , secondary_reservation_quantity = l_orig_second_rsv_qty
14499            , reservation_quantity = l_new_orig_rsv_qty
14500            , detailed_quantity = l_detailed_quantity
14501            , secondary_detailed_quantity = l_secondary_detailed_quantity           --bug 8448053  kbanddyo
14502            , last_update_date = l_date
14503            , last_updated_by = l_user_id
14504            , last_update_login = l_login_id
14505            , request_id = l_request_id
14506            , program_application_id = l_prog_appl_id
14507            , program_id = l_program_id
14508            , program_update_date = l_date
14509        WHERE reservation_id = l_orig_rsv_tbl(1).reservation_id;
14510 
14511       -- for data sync b/w mtl_demand and mtl_reservations
14512       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);
14513 
14514       IF l_return_status = fnd_api.g_ret_sts_error THEN
14515         RAISE fnd_api.g_exc_error;
14516       END IF;
14517 
14518       --
14519       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14520         RAISE fnd_api.g_exc_unexpected_error;
14521       END IF;
14522       --
14523     /** Commented out by request of CTO.  This was causing an extra
14524      *  update to the workflow that was causing problems.
14525      *  Bug 2073768
14526      *  This code was not interacting correctly with CTO.
14527      *  No longer call the wf_update_after_inv_unreserv api from
14528      *  transfer_reservation
14529      *-- Post Update CTO Validation
14530      * IF l_orig_rsv_tbl(1).demand_source_type_id in (
14531      *    inv_reservation_global.g_source_type_oe
14532      *         ,inv_reservation_global.g_source_type_internal_ord
14533      *         ,inv_reservation_global.g_source_type_rma) THEN
14534      *     --
14535      *     cto_workflow_api_pk.wf_update_after_inv_unreserv(
14536      *       p_order_line_id      => l_orig_rsv_tbl(1).demand_source_line_id
14537      *     , x_return_status      => l_return_status
14538      *     , x_msg_count          => x_msg_count
14539      *     , x_msg_data           => x_msg_data
14540      *     );
14541      *     --
14542      *     IF l_return_status = fnd_api.g_ret_sts_error THEN
14543      *      RAISE fnd_api.g_exc_error;
14544      *    END IF ;
14545      *     --
14546      *     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14547      *      RAISE fnd_api.g_exc_unexpected_error;
14548      *    END IF;
14549      *     --
14550      * END IF;
14551      */
14552        ELSIF l_to_row_exist = TRUE THEN
14553        IF (l_debug = 1) THEN
14554           debug_print('To row exists and transfer all is true');
14555        END IF;
14556        -- Pre Delete CTO Validation
14557       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
14558         --
14559         cto_workflow_api_pk.inventory_unreservation_check(
14560           p_order_line_id              => l_orig_rsv_tbl(1).demand_source_line_id
14561         , p_rsv_quantity               => l_to_rsv_rec.primary_reservation_quantity
14562         , x_return_status              => l_return_status
14563         , x_msg_count                  => x_msg_count
14564         , x_msg_data                   => x_msg_data
14565         );
14566 
14567         --
14568         IF l_return_status = fnd_api.g_ret_sts_error THEN
14569           RAISE fnd_api.g_exc_error;
14570         END IF;
14571 
14572         --
14573         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14574           RAISE fnd_api.g_exc_unexpected_error;
14575         END IF;
14576       --
14577       END IF;
14578 
14579       -- for data sync b/w mtl_demand and mtl_reservations
14580       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);
14581 
14582       IF l_return_status = fnd_api.g_ret_sts_error THEN
14583         RAISE fnd_api.g_exc_error;
14584       END IF;
14585 
14586       --
14587       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14588         RAISE fnd_api.g_exc_unexpected_error;
14589       END IF;
14590 
14591       -- 2884492
14592        mtl_reservations_pkg.delete_row
14593 	(x_reservation_id => l_orig_rsv_tbl(1).reservation_id
14594 	,x_to_reservation_id => l_to_rsv_tbl(1).reservation_id);
14595     /** commented out by request of CTO.  The workflow was not processing
14596      *  correctly.
14597      *  Bug 2073768
14598      *  This code was not interacting correctly with CTO.
14599      *  No longer call the wf_update_after_inv_unreserv api from
14600      *  transfer_reservation
14601      *-- Post Delete CTO Validation
14602      * IF l_orig_rsv_tbl(1).demand_source_type_id in (
14603      *    inv_reservation_global.g_source_type_oe
14604      *          ,inv_reservation_global.g_source_type_internal_ord
14605      *          ,inv_reservation_global.g_source_type_rma) THEN
14606      *     --
14607      *     cto_workflow_api_pk.wf_update_after_inv_unreserv(
14608      *       p_order_line_id      => l_orig_rsv_tbl(1).demand_source_line_id
14609      *     , x_return_status      => l_return_status
14610      *     , x_msg_count          => x_msg_count
14611      *     , x_msg_data           => x_msg_data
14612      *     );
14613      *     --
14614      *     IF l_return_status = fnd_api.g_ret_sts_error THEN
14615      *      RAISE fnd_api.g_exc_error;
14616      *    END IF ;
14617      *     --
14618      *     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14619      *      RAISE fnd_api.g_exc_unexpected_error;
14620      *    END IF;
14621      *     --
14622      * END IF;
14623      */
14624        ELSE
14625        IF (l_debug = 1) THEN
14626           debug_print('To row does not exist and transfer all is true');
14627        END IF;
14628       -- Pre Update CTO Validation
14629       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
14630         --
14631         cto_workflow_api_pk.inventory_unreservation_check(
14632           p_order_line_id              => l_orig_rsv_tbl(1).demand_source_line_id
14633         , p_rsv_quantity               => l_orig_rsv_tbl(1).primary_reservation_quantity
14634         , x_return_status              => l_return_status
14635         , x_msg_count                  => x_msg_count
14636         , x_msg_data                   => x_msg_data
14637         );
14638 
14639         --
14640         IF l_return_status = fnd_api.g_ret_sts_error THEN
14641           RAISE fnd_api.g_exc_error;
14642         END IF;
14643 
14644         --
14645         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14646           RAISE fnd_api.g_exc_unexpected_error;
14647         END IF;
14648       --
14649       END IF;
14650 
14651       -- Added for bug 2458523
14652       -- Pre Insert CTO Validation
14653       IF l_to_rsv_rec.demand_source_type_id IN
14654 	(inv_reservation_global.g_source_type_oe,
14655 	 inv_reservation_global.g_source_type_internal_ord,
14656 	 inv_reservation_global.g_source_type_rma) THEN
14657 
14658         --
14659         cto_workflow_api_pk.inventory_reservation_check(
14660           p_order_line_id              => l_to_rsv_rec.demand_source_line_id
14661         , x_return_status              => l_return_status
14662         , x_msg_count                  => x_msg_count
14663         , x_msg_data                   => x_msg_data
14664         );
14665 
14666         --
14667         IF l_return_status = fnd_api.g_ret_sts_error THEN
14668           RAISE fnd_api.g_exc_error;
14669         END IF;
14670 
14671         --
14672         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14673           RAISE fnd_api.g_exc_unexpected_error;
14674         END IF;
14675       --
14676       END IF;
14677 
14678       --Bug #2819700
14679       --Adding an extra check to make sure that tranfer reservations does not
14680       -- update the to reservation record to a NEGATIVE NUMBER.
14681       IF (l_debug = 1) THEN
14682         debug_print('Primary_reservation_qty before inserting (xfer)= '
14683 		                || To_char(l_to_rsv_rec.primary_reservation_quantity) );
14684         debug_print('Secondary_reservation_qty before inserting (xfer)= '
14685                                 || To_char(l_to_rsv_rec.secondary_reservation_quantity) );   -- INVCONV
14686         debug_print('Reservation_qty before inserting (xfer)= '
14687 		                || To_char(l_to_rsv_rec.reservation_quantity) );
14688       END IF;
14689 
14690       IF (  (NVL(l_to_rsv_rec.reservation_quantity,0) < 0) OR
14691 	    (NVL(l_to_rsv_rec.primary_reservation_quantity,0) < 0) ) THEN
14692 	 fnd_message.set_name('INV', 'INV-INVALID RESERVATION QTY');
14693 	 fnd_msg_pub.ADD;
14694 	 RAISE fnd_api.g_exc_error;
14695       END IF;
14696 
14697        -- INVCONV BEGIN
14698       IF (NVL(l_to_rsv_rec.secondary_reservation_quantity,0) < 0) THEN
14699          fnd_message.set_name('INV', 'INV-INVALID NEGATIVE SECONDARY'); -- INVCONV New Message
14700          fnd_msg_pub.ADD;
14701          RAISE fnd_api.g_exc_error;
14702       END IF;
14703       -- INVCONV END
14704 
14705       -- Bug 3461990: Reservations API should not update reservations with more
14706       -- than 5 decimal places, since the transaction quantity is being
14707       -- rounded to 5 decimal places.
14708 
14709       l_to_rsv_rec.primary_reservation_quantity :=
14710 	round(l_to_rsv_rec.primary_reservation_quantity,5);
14711 
14712       -- INVCONV BEGIN
14713       IF l_dual_tracking THEN
14714         l_to_rsv_rec.secondary_reservation_quantity :=
14715           round(l_to_rsv_rec.secondary_reservation_quantity,5);
14716       END IF;
14717       -- INVCONV END
14718 
14719       l_to_rsv_rec.reservation_quantity :=
14720 	round(l_to_rsv_rec.reservation_quantity,5);
14721       l_to_rsv_rec.detailed_quantity  :=
14722 	round(Nvl(l_to_rsv_rec.detailed_quantity,0),5);
14723 
14724 
14725       IF (l_debug = 1) THEN
14726 	 debug_print(' Transfer: Before updating all but id for the from record');
14727 	 debug_print(' After rounding reservation is' || l_orig_rsv_tbl(1).reservation_id);
14728 	 debug_print(' After rounding reservation qty' || l_to_rsv_rec.reservation_quantity);
14729 	 debug_print(' After rounding reservation pri qty' || l_to_rsv_rec.primary_reservation_quantity);
14730 	 debug_print(' After rounding detailed quantity' ||l_to_rsv_rec.detailed_quantity );
14731       END IF;
14732 
14733       -- INVCONV - Incorporate secondaries in update
14734       mtl_reservations_pkg.update_row
14735 	(
14736 	 x_reservation_id             => l_orig_rsv_tbl(1).reservation_id
14737 	 , x_requirement_date           => l_to_rsv_rec.requirement_date
14738 	 , x_organization_id            => l_to_rsv_rec.organization_id
14739 	 , x_inventory_item_id          => l_to_rsv_rec.inventory_item_id
14740 	 , x_demand_source_type_id      => l_to_rsv_rec.demand_source_type_id
14741 	 , x_demand_source_header_id    => l_to_rsv_rec.demand_source_header_id
14742 	 , x_demand_source_line_id      => l_to_rsv_rec.demand_source_line_id
14743 	 , x_demand_source_name         => l_to_rsv_rec.demand_source_name
14744 	 , x_demand_source_delivery     => l_to_rsv_rec.demand_source_delivery
14745 	 , x_primary_uom_code           => l_to_rsv_rec.primary_uom_code
14746 	 , x_primary_uom_id             => l_to_rsv_rec.primary_uom_id
14747 	 , x_secondary_uom_code         => l_to_rsv_rec.secondary_uom_code
14748 	 , x_secondary_uom_id           => l_to_rsv_rec.secondary_uom_id
14749 	 , x_reservation_uom_code       => l_to_rsv_rec.reservation_uom_code
14750 	 , x_reservation_uom_id         => l_to_rsv_rec.reservation_uom_id
14751 	, x_reservation_quantity       => l_to_rsv_rec.reservation_quantity
14752 	, x_primary_reservation_quantity=> l_to_rsv_rec.primary_reservation_quantity
14753 	, x_second_reservation_quantity=> l_to_rsv_rec.secondary_reservation_quantity
14754 	, x_detailed_quantity          => l_to_rsv_rec.detailed_quantity
14755 	, x_secondary_detailed_quantity=> l_to_rsv_rec.secondary_detailed_quantity
14756 	, x_autodetail_group_id        => l_to_rsv_rec.autodetail_group_id
14757 	, x_external_source_code       => l_to_rsv_rec.external_source_code
14758 	, x_external_source_line_id    => l_to_rsv_rec.external_source_line_id
14759 	, x_supply_source_type_id      => l_to_rsv_rec.supply_source_type_id
14760 	, x_supply_source_header_id    => l_to_rsv_rec.supply_source_header_id
14761 	, x_supply_source_line_id      => l_to_rsv_rec.supply_source_line_id
14762 	, x_supply_source_name         => l_to_rsv_rec.supply_source_name
14763 	, x_supply_source_line_detail  => l_to_rsv_rec.supply_source_line_detail
14764 	, x_revision                   => l_to_rsv_rec.revision
14765 	, x_subinventory_code          => l_to_rsv_rec.subinventory_code
14766 	, x_subinventory_id            => l_to_rsv_rec.subinventory_id
14767 	, x_locator_id                 => l_to_rsv_rec.locator_id
14768 	, x_lot_number                 => l_to_rsv_rec.lot_number
14769 	, x_lot_number_id              => l_to_rsv_rec.lot_number_id
14770 	, x_serial_number              => NULL
14771 	, x_serial_number_id           => NULL
14772 	, x_partial_quantities_allowed => NULL
14773 	, x_auto_detailed              => NULL
14774 	, x_pick_slip_number           => l_to_rsv_rec.pick_slip_number
14775 	, x_lpn_id                     => l_to_rsv_rec.lpn_id
14776 	, x_last_update_date           => l_date
14777 	, x_last_updated_by            => l_user_id
14778 	, x_last_update_login          => l_login_id
14779 	, x_request_id                 => l_request_id
14780 	, x_program_application_id     => l_prog_appl_id
14781 	, x_program_id                 => l_program_id
14782 	, x_program_update_date        => l_date
14783 	, x_attribute_category         => l_to_rsv_rec.attribute_category
14784 	, x_attribute1                 => l_to_rsv_rec.attribute1
14785 	, x_attribute2                 => l_to_rsv_rec.attribute2
14786 	, x_attribute3                 => l_to_rsv_rec.attribute3
14787 	, x_attribute4                 => l_to_rsv_rec.attribute4
14788 	, x_attribute5                 => l_to_rsv_rec.attribute5
14789 	, x_attribute6                 => l_to_rsv_rec.attribute6
14790 	, x_attribute7                 => l_to_rsv_rec.attribute7
14791 	, x_attribute8                 => l_to_rsv_rec.attribute8
14792 	, x_attribute9                 => l_to_rsv_rec.attribute9
14793 	, x_attribute10                => l_to_rsv_rec.attribute10
14794 	, x_attribute11                => l_to_rsv_rec.attribute11
14795 	, x_attribute12                => l_to_rsv_rec.attribute12
14796 	, x_attribute13                => l_to_rsv_rec.attribute13
14797 	, x_attribute14                => l_to_rsv_rec.attribute14
14798 	, x_attribute15                => l_to_rsv_rec.attribute15
14799 	, x_ship_ready_flag            => l_to_rsv_rec.ship_ready_flag
14800 	, x_staged_flag                => l_to_rsv_rec.staged_flag
14801 	/**** {{ R12 Enhanced reservations code changes }}****/
14802 	, x_crossdock_flag             => l_to_rsv_rec.crossdock_flag
14803 	, x_crossdock_criteria_id      => l_to_rsv_rec.crossdock_criteria_id
14804 	, x_demand_source_line_detail  => l_to_rsv_rec.demand_source_line_detail
14805 	, x_serial_reservation_quantity => l_to_rsv_rec.serial_reservation_quantity
14806 	, x_supply_receipt_date        => l_to_rsv_rec.supply_receipt_date
14807 	, x_demand_ship_date           => l_to_rsv_rec.demand_ship_date
14808 	, x_project_id                 => l_to_rsv_rec.project_id
14809 	, x_task_id                    => l_to_rsv_rec.task_id
14810 	/*** End R12 ***/
14811 	);
14812       --
14813       -- for data sync b/w mtl_demand and mtl_reservations
14814       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);
14815 
14816       debug_print(' return status after updating row' || l_return_status);
14817 
14818       IF l_return_status = fnd_api.g_ret_sts_error THEN
14819         RAISE fnd_api.g_exc_error;
14820       END IF;
14821 
14822       --
14823       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14824         RAISE fnd_api.g_exc_unexpected_error;
14825       END IF;
14826 
14827       --
14828       -- since to row not exists, transfer all
14829       x_reservation_id  := l_orig_rsv_tbl(1).reservation_id;
14830 
14831       IF l_orig_rsv_tbl(1).demand_source_type_id = 2   -- Bug 6195783 : Begin
14832       AND l_to_rsv_rec.demand_source_type_id = 9
14833       AND l_orig_rsv_tbl(1).primary_reservation_quantity = l_to_rsv_rec.primary_reservation_quantity THEN
14834          cto_workflow_api_pk.wf_update_after_inv_unreserv(
14835          p_order_line_id      => l_orig_rsv_tbl(1).demand_source_line_id
14836          , x_return_status      => l_return_status
14837          , x_msg_count          => x_msg_count
14838          , x_msg_data           => x_msg_data
14839          );
14840       END IF;                                          -- Bug 6195783 : End
14841 
14842     /** commented out by request of CTO.  Their workflow was not
14843      *  progressing properly
14844      *  Bug 2073768
14845      *  This code was not interacting correctly with CTO.
14846      *  No longer call the wf_update_after_inv_unreserv api from
14847      *  transfer_reservation
14848      *-- Post Update CTO Validation
14849      * IF l_orig_rsv_tbl(1).demand_source_type_id in (
14850      *    inv_reservation_global.g_source_type_oe
14851      *          ,inv_reservation_global.g_source_type_internal_ord
14852      *          ,inv_reservation_global.g_source_type_rma) THEN
14853      *     --
14854      *     cto_workflow_api_pk.wf_update_after_inv_unreserv(
14855      *       p_order_line_id      => l_orig_rsv_tbl(1).demand_source_line_id
14856      *     , x_return_status      => l_return_status
14857      *     , x_msg_count          => x_msg_count
14858      *     , x_msg_data           => x_msg_data
14859      *     );
14860      *     --
14861      *     IF l_return_status = fnd_api.g_ret_sts_error THEN
14862      *      RAISE fnd_api.g_exc_error;
14863      *    END IF ;
14864      *     --
14865      *     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14866      *      RAISE fnd_api.g_exc_unexpected_error;
14867      *    END IF;
14868      *     --
14869      * END IF;
14870      */
14871     END IF;
14872 
14873     --
14874      IF l_to_row_exist = TRUE THEN
14875 	IF (l_debug = 1) THEN
14876            debug_print('To row does exists');
14877         END IF;
14878 
14879       /* Commenting out call to CTO inventory_unreservation_check API. This
14880 	API has already been called in the previous if then else. This
14881 	  issue has been reported in bug 2458523
14882       -- Pre Update CTO Validation
14883       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
14884         --
14885         cto_workflow_api_pk.inventory_unreservation_check(
14886           p_order_line_id              => l_to_rsv_rec.demand_source_line_id
14887         , p_rsv_quantity               => l_to_rsv_rec.primary_reservation_quantity
14888         , x_return_status              => l_return_status
14889         , x_msg_count                  => x_msg_count
14890         , x_msg_data                   => x_msg_data
14891         );
14892 
14893         --
14894         IF l_return_status = fnd_api.g_ret_sts_error THEN
14895           RAISE fnd_api.g_exc_error;
14896         END IF;
14897 
14898         --
14899         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14900           RAISE fnd_api.g_exc_unexpected_error;
14901         END IF;
14902       --
14903       END IF;
14904       */
14905 
14906       -- Added for bug 2458523
14907       -- Pre Insert CTO Validation
14908       IF l_to_rsv_rec.demand_source_type_id IN
14909 	  (inv_reservation_global.g_source_type_oe,
14910 	   inv_reservation_global.g_source_type_internal_ord,
14911 	   inv_reservation_global.g_source_type_rma) THEN
14912 
14913         --
14914         cto_workflow_api_pk.inventory_reservation_check(
14915           p_order_line_id              => l_to_rsv_rec.demand_source_line_id
14916         , x_return_status              => l_return_status
14917         , x_msg_count                  => x_msg_count
14918         , x_msg_data                   => x_msg_data
14919         );
14920 
14921         --
14922         IF l_return_status = fnd_api.g_ret_sts_error THEN
14923           RAISE fnd_api.g_exc_error;
14924         END IF;
14925 
14926         --
14927         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
14928           RAISE fnd_api.g_exc_unexpected_error;
14929         END IF;
14930       --
14931       END IF;
14932 
14933       l_primary_rsv_quantity := 0;
14934       l_rsv_quantity := 0;
14935 
14936        -- INVCONV - Retrieve secondaries
14937       SELECT  primary_reservation_quantity, secondary_reservation_quantity, reservation_quantity
14938       INTO    l_primary_rsv_quantity, l_secondary_rsv_quantity, l_rsv_quantity
14939             FROM    mtl_reservations
14940       WHERE   reservation_id = l_to_rsv_tbl(1).reservation_id;
14941 
14942       l_primary_rsv_quantity := l_primary_rsv_quantity + l_to_rsv_rec.primary_reservation_quantity;
14943 
14944        -- INVCONV BEGIN
14945       IF l_dual_tracking THEN
14946         l_secondary_rsv_quantity :=l_secondary_rsv_quantity + l_to_rsv_rec.secondary_reservation_quantity;
14947       END IF;
14948       -- INVCONV END
14949 
14950       l_rsv_quantity :=l_rsv_quantity + l_to_rsv_rec.reservation_quantity;
14951 
14952       --Bug #2819700
14953       --Adding an extra check to make sure that tranfer reservations does not
14954       --update the to reservation record to a NEGATIVE NUMBER.
14955       IF (l_debug = 1) THEN
14956         debug_print('Primary_reservation_qty before inserting (xfer)= ' || To_char(l_primary_rsv_quantity) );
14957         debug_print('Secondary_reservation_qty before inserting (xfer)= ' || To_char(l_secondary_rsv_quantity) ); --INVCONV
14958         debug_print('Rreservation_qty before inserting (xfer)= ' ||To_char(l_rsv_quantity));
14959       END IF;
14960 
14961       IF (  (NVL(l_primary_rsv_quantity,0) < 0) OR
14962 	          (NVL(l_rsv_quantity,0)< 0) ) THEN
14963        fnd_message.set_name('INV', 'INV-INVALID RESERVATION QTY');
14964         fnd_msg_pub.ADD;
14965         RAISE fnd_api.g_exc_error;
14966       END IF;
14967 
14968        -- INVCONV BEGIN
14969       IF (NVL(l_secondary_rsv_quantity,0) < 0) THEN
14970         fnd_message.set_name('INV', 'INV-INVALID NEGATIVE SECONDARY'); -- INVCONV New Message
14971         fnd_msg_pub.ADD;
14972         RAISE fnd_api.g_exc_error;
14973       END IF;
14974       -- INVCONV END
14975 
14976       -- Bug 3461990: Reservations API should not be updated with more
14977       -- than 5 decimal places, since the transaction quantity is being
14978       -- rounded to 5 decimal places.
14979       IF (l_debug = 1) THEN
14980 	 debug_print(' Transfer: Before adding the qty to the to record');
14981       END IF;
14982 
14983       -- wont call table handler since only quantities are changed along
14984       -- with the who column this is simpler to read and understand
14985 
14986       -- INVCONV - Incorporate secondary_reservation_quantity which could be null
14987       UPDATE mtl_reservations
14988          SET primary_reservation_quantity = Round((primary_reservation_quantity + l_to_rsv_rec.primary_reservation_quantity),5)
14989            , secondary_reservation_quantity = Round((secondary_reservation_quantity + l_to_rsv_rec.secondary_reservation_quantity),5)
14990            , reservation_quantity = Round((reservation_quantity + l_to_rsv_rec.reservation_quantity),5)
14991            , detailed_quantity = Round(NVL(detailed_quantity, 0) + NVL(l_to_rsv_rec.detailed_quantity, 0),5)
14992            , secondary_detailed_quantity = Round(NVL(secondary_detailed_quantity, 0) + NVL(l_to_rsv_rec.secondary_detailed_quantity, 0),5)           --bug 8448053  kbanddyo
14993            , last_update_date = l_date
14994            , last_updated_by = l_user_id
14995            , last_update_login = l_login_id
14996            , request_id = l_request_id
14997            , program_application_id = l_prog_appl_id
14998            , program_id = l_program_id
14999            , program_update_date = l_date
15000        WHERE reservation_id = l_to_rsv_tbl(1).reservation_id;
15001 
15002       -- for data sync b/w mtl_demand and mtl_reservations
15003       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);
15004 
15005       IF l_return_status = fnd_api.g_ret_sts_error THEN
15006         RAISE fnd_api.g_exc_error;
15007       END IF;
15008 
15009       --
15010       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
15011         RAISE fnd_api.g_exc_unexpected_error;
15012       END IF;
15013 
15014       --
15015       x_reservation_id  := l_to_rsv_tbl(1).reservation_id;
15016     /** Commented out by request of CTO.  Their workflow was not
15017      *  progressing correctly
15018      *  Bug 2073768
15019      *  This code was not interacting correctly with CTO.
15020      *  No longer call the wf_update_after_inv_unreserv api from
15021      *  transfer_reservation
15022      *-- Post Update CTO Validation
15023      * IF l_to_rsv_rec.demand_source_type_id in (
15024      *   inv_reservation_global.g_source_type_oe
15025      *         ,inv_reservation_global.g_source_type_internal_ord
15026      *        ,inv_reservation_global.g_source_type_rma) THEN
15027      *    --
15028      *    cto_workflow_api_pk.wf_update_after_inv_unreserv(
15029      *      p_order_line_id      => l_to_rsv_rec.demand_source_line_id
15030      *    , x_return_status      => l_return_status
15031      *    , x_msg_count          => x_msg_count
15032      *    , x_msg_data           => x_msg_data
15033      *    );
15034      *    --
15035      *    IF l_return_status = fnd_api.g_ret_sts_error THEN
15036      *     RAISE fnd_api.g_exc_error;
15037      *   END IF ;
15038      *    --
15039      *    IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
15040      *     RAISE fnd_api.g_exc_unexpected_error;
15041      *   END IF;
15042      *    --
15043      *END IF;
15044      */
15045        ELSIF l_transfer_all = FALSE THEN
15046 	IF (l_debug = 1) THEN
15047            debug_print('To row does not exists and transfer all is false');
15048         END IF;
15049       -- Pre Insert CTO Validation
15050       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
15051         --
15052         cto_workflow_api_pk.inventory_reservation_check(
15053           p_order_line_id              => l_to_rsv_rec.demand_source_line_id
15054         , x_return_status              => l_return_status
15055         , x_msg_count                  => x_msg_count
15056         , x_msg_data                   => x_msg_data
15057         );
15058 
15059         --
15060         IF l_return_status = fnd_api.g_ret_sts_error THEN
15061           RAISE fnd_api.g_exc_error;
15062         END IF;
15063 
15064         --
15065         IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
15066           RAISE fnd_api.g_exc_unexpected_error;
15067         END IF;
15068       --
15069       END IF;
15070 
15071       --Bug #2819700
15072       --Adding an extra check to make sure that tranfer reservations does not
15073       -- insert the to reservation record to a NEGATIVE NUMBER.
15074       IF (l_debug = 1) THEN
15075         debug_print('Primary_reservation_qty before inserting (xfer)= '
15076 		                || To_char(l_to_rsv_rec.primary_reservation_quantity) );
15077          debug_print('Secondary_reservation_qty before inserting (xfer)= '
15078                                 || To_char(l_to_rsv_rec.secondary_reservation_quantity) ); --INVCONV
15079         debug_print('Reservation_qty before inserting (xfer)= '
15080 		                || To_char(l_to_rsv_rec.reservation_quantity) );
15081       END IF;
15082 
15083       IF (  (NVL(l_to_rsv_rec.reservation_quantity,0) < 0) OR
15084 	    (NVL(l_to_rsv_rec.primary_reservation_quantity,0) < 0) ) THEN
15085 	 fnd_message.set_name('INV', 'INV-INVALID RESERVATION QTY');
15086 	 fnd_msg_pub.ADD;
15087 	 RAISE fnd_api.g_exc_error;
15088       END IF;
15089 
15090        -- INVCONV BEGIN
15091       IF (NVL(l_to_rsv_rec.secondary_reservation_quantity,0) < 0) THEN
15092          fnd_message.set_name('INV', 'INV-INVALID NEGATIVE SECONDARY'); -- INVCONV New Message
15093          fnd_msg_pub.ADD;
15094          RAISE fnd_api.g_exc_error;
15095       END IF;
15096       -- INVCONV END
15097 
15098 
15099       -- Bug 3461990: Reservations API should not create reservations with more
15100       -- than 5 decimal places, since the transaction quantity is being
15101       -- rounded to 5 decimal places.
15102 
15103       l_to_rsv_rec.primary_reservation_quantity :=
15104 	round(l_to_rsv_rec.primary_reservation_quantity,5);
15105       -- INVCONV BEGIN
15106       IF l_dual_tracking THEN
15107         l_to_rsv_rec.secondary_reservation_quantity :=
15108           round(l_to_rsv_rec.secondary_reservation_quantity,5);
15109       END IF;
15110       -- INVCONV END
15111       l_to_rsv_rec.reservation_quantity  :=
15112 	round(l_to_rsv_rec.reservation_quantity,5);
15113       l_to_rsv_rec.detailed_quantity  :=
15114 	round(Nvl(l_to_rsv_rec.detailed_quantity,0),5);
15115 
15116           -- Added for bug 8851133 -Start
15117             IF l_dual_tracking THEN
15118                        IF (  l_to_rsv_rec.detailed_quantity = 0 ) THEN
15119                                  l_to_rsv_rec.secondary_detailed_quantity := 0;
15120                         END IF;
15121             ELSE
15122                         l_to_rsv_rec.secondary_reservation_quantity := NULL;
15123             END IF;
15124 
15125            -- bug 8851133 - End
15126 
15127 
15128       IF (l_debug = 1) THEN
15129 	 debug_print(' Transfer: Before creating new reservations for the to record');
15130 	 debug_print(' After rounding reservation is' || l_orig_rsv_tbl(1).reservation_id);
15131 	 debug_print(' After rounding reservation qty' || l_to_rsv_rec.reservation_quantity);
15132 	 debug_print(' After rounding reservation pri qty' || l_to_rsv_rec.primary_reservation_quantity);
15133          debug_print(' After rounding reservation sec qty' || l_to_rsv_rec.secondary_reservation_quantity);
15134 	 debug_print(' After rounding detailed quantity' ||l_to_rsv_rec.detailed_quantity );
15135       END IF;
15136 
15137       /**** {{ R12 Enhanced reservations code changes }}****/
15138       -- if the reservation being transferred is from PO to ASN
15139       -- we need to update the original supply as PO, because we
15140       -- will have to transfer the reservation back to ASN while reducing
15141       -- and cancelling ASNs
15142       IF (l_to_rsv_rec.supply_source_type_id =
15143 	  inv_reservation_global.g_source_type_asn) AND
15144 	(l_orig_rsv_tbl(1).supply_source_type_id = inv_reservation_global.g_source_type_po) THEN
15145 
15146 	 l_orig_supply_type_id := inv_reservation_global.g_source_type_po;
15147 	 IF (l_debug = 1) THEN
15148 	    debug_print('The original supply is po and the new supply is asn' ||l_orig_supply_type_id);
15149 	 END IF;
15150 
15151       END IF;
15152 
15153       /*** End R12 ***/
15154       -- create reservation id
15155      /* SELECT mtl_demand_s.NEXTVAL
15156         INTO l_reservation_id
15157         FROM DUAL; */
15158 	-- Bug 55350300 --Selecting the sequence value has been moved to the table handler
15159 	-- in INVRSV6B.pls
15160 
15161 	l_reservation_id := NULL;
15162 
15163   --12362469
15164   IF l_to_rsv_rec.staged_flag = 'Y'
15165   THEN
15166     l_to_rsv_rec.serial_reservation_quantity := 0 ;
15167   END IF;
15168   --12362469
15169 
15170       -- INVCONV - Incorporate secondary columns
15171       mtl_reservations_pkg.insert_row
15172 	(
15173 	 x_rowid                      => l_rowid
15174 	 , x_reservation_id             => l_reservation_id
15175 	 , x_requirement_date           => l_to_rsv_rec.requirement_date
15176 	 , x_organization_id            => l_to_rsv_rec.organization_id
15177 	 , x_inventory_item_id          => l_to_rsv_rec.inventory_item_id
15178 	 , x_demand_source_type_id      => l_to_rsv_rec.demand_source_type_id
15179 	 , x_demand_source_name         => l_to_rsv_rec.demand_source_name
15180 	 , x_demand_source_header_id    => l_to_rsv_rec.demand_source_header_id
15181 	 , x_demand_source_line_id      => l_to_rsv_rec.demand_source_line_id
15182 	 , x_demand_source_delivery     => l_to_rsv_rec.demand_source_delivery
15183 	 , x_primary_uom_code           => l_to_rsv_rec.primary_uom_code
15184 	 , x_primary_uom_id             => l_to_rsv_rec.primary_uom_id
15185 	 , x_secondary_uom_code         => l_to_rsv_rec.secondary_uom_code
15186 	 , x_secondary_uom_id           => l_to_rsv_rec.secondary_uom_id
15187 	 , x_reservation_uom_code       => l_to_rsv_rec.reservation_uom_code
15188 	 , x_reservation_uom_id         => l_to_rsv_rec.reservation_uom_id
15189 	, x_reservation_quantity       => l_to_rsv_rec.reservation_quantity
15190 	, x_primary_reservation_quantity=> l_to_rsv_rec.primary_reservation_quantity
15191 	, x_second_reservation_quantity=> l_to_rsv_rec.secondary_reservation_quantity
15192 	, x_detailed_quantity          => l_to_rsv_rec.detailed_quantity
15193 	, x_secondary_detailed_quantity => l_to_rsv_rec.secondary_detailed_quantity
15194 	, x_autodetail_group_id        => l_to_rsv_rec.autodetail_group_id
15195 	, x_external_source_code       => l_to_rsv_rec.external_source_code
15196 	, x_external_source_line_id    => l_to_rsv_rec.external_source_line_id
15197 	, x_supply_source_type_id      => l_to_rsv_rec.supply_source_type_id
15198 	, x_supply_source_header_id    => l_to_rsv_rec.supply_source_header_id
15199 	, x_supply_source_line_id      => l_to_rsv_rec.supply_source_line_id
15200 	, x_supply_source_line_detail  => l_to_rsv_rec.supply_source_line_detail
15201 	, x_supply_source_name         => l_to_rsv_rec.supply_source_name
15202 	, x_revision                   => l_to_rsv_rec.revision
15203 	, x_subinventory_code          => l_to_rsv_rec.subinventory_code
15204 	, x_subinventory_id            => l_to_rsv_rec.subinventory_id
15205 	, x_locator_id                 => l_to_rsv_rec.locator_id
15206 	, x_lot_number                 => l_to_rsv_rec.lot_number
15207 	, x_lot_number_id              => l_to_rsv_rec.lot_number_id
15208 	, x_serial_number              => NULL
15209 	, x_serial_number_id           => NULL
15210 	, x_partial_quantities_allowed => NULL
15211 	, x_auto_detailed              => NULL
15212 	, x_pick_slip_number           => l_to_rsv_rec.pick_slip_number
15213 	, x_lpn_id                     => l_to_rsv_rec.lpn_id
15214 	, x_last_update_date           => l_date
15215 	, x_last_updated_by            => l_user_id
15216 	, x_creation_date              => l_date
15217 	, x_created_by                 => l_user_id
15218 	, x_last_update_login          => l_login_id
15219 	, x_request_id                 => l_request_id
15220 	, x_program_application_id     => l_prog_appl_id
15221 	, x_program_id                 => l_program_id
15222 	, x_program_update_date        => l_date
15223 	, x_attribute_category         => l_to_rsv_rec.attribute_category
15224 	, x_attribute1                 => l_to_rsv_rec.attribute1
15225 	, x_attribute2                 => l_to_rsv_rec.attribute2
15226 	, x_attribute3                 => l_to_rsv_rec.attribute3
15227 	, x_attribute4                 => l_to_rsv_rec.attribute4
15228 	, x_attribute5                 => l_to_rsv_rec.attribute5
15229 	, x_attribute6                 => l_to_rsv_rec.attribute6
15230 	, x_attribute7                 => l_to_rsv_rec.attribute7
15231 	, x_attribute8                 => l_to_rsv_rec.attribute8
15232 	, x_attribute9                 => l_to_rsv_rec.attribute9
15233 	, x_attribute10                => l_to_rsv_rec.attribute10
15234 	, x_attribute11                => l_to_rsv_rec.attribute11
15235 	, x_attribute12                => l_to_rsv_rec.attribute12
15236 	, x_attribute13                => l_to_rsv_rec.attribute13
15237 	, x_attribute14                => l_to_rsv_rec.attribute14
15238 	, x_attribute15                => l_to_rsv_rec.attribute15
15239 	, x_ship_ready_flag            => l_to_rsv_rec.ship_ready_flag
15240 	, x_staged_flag                => l_to_rsv_rec.staged_flag
15241 	/**** {{ R12 Enhanced reservations code changes }}****/
15242 	, x_crossdock_flag             => l_to_rsv_rec.crossdock_flag
15243 	, x_crossdock_criteria_id      => l_to_rsv_rec.crossdock_criteria_id
15244 	, x_demand_source_line_detail  => l_to_rsv_rec.demand_source_line_detail
15245 	, x_serial_reservation_quantity => l_to_rsv_rec.serial_reservation_quantity
15246 	, x_supply_receipt_date        => l_to_rsv_rec.supply_receipt_date
15247 	, x_demand_ship_date             => l_to_rsv_rec.demand_ship_date
15248 	, x_project_id                   => l_to_rsv_rec.project_id
15249 	, x_task_id                      => l_to_rsv_rec.task_id
15250 	, x_orig_supply_type_id   => l_orig_supply_type_id
15251 	, x_orig_supply_header_id => l_to_rsv_rec.supply_source_header_id
15252 	, x_orig_supply_line_id     => l_to_rsv_rec.supply_source_line_id
15253 	, x_orig_supply_line_detail => l_to_rsv_rec.supply_source_line_detail
15254 	, x_orig_demand_type_id     => l_to_rsv_rec.demand_source_type_id
15255 	, x_orig_demand_header_id   => l_to_rsv_rec.demand_source_header_id
15256 	, x_orig_demand_line_id     => l_to_rsv_rec.demand_source_line_id
15257 	, x_orig_demand_line_detail => l_to_rsv_rec.demand_source_line_detail
15258 	/*** End R12 ***/
15259 	);
15260 
15261       debug_print(' After call to insert_row : reservation_id : ' || l_reservation_id);
15262 
15263       -- insert into mtl_reservations
15264       x_reservation_id  := l_reservation_id;
15265 
15266       -- for data sync b/w mtl_demand and mtl_reservations
15267       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);
15268 
15269       IF l_return_status = fnd_api.g_ret_sts_error THEN
15270         RAISE fnd_api.g_exc_error;
15271       END IF;
15272 
15273       --
15274       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
15275         RAISE fnd_api.g_exc_unexpected_error;
15276       END IF;
15277     --
15278     END IF;
15279 
15280     debug_print(' Before serial number check' || l_return_status);
15281 
15282     -- Bug 2073768
15283     -- The order status was not being updated in correctly in the
15284     -- sales order, becuase we were only calling this cto api for
15285     -- partial reservation transfers.  Now, call this API everytime
15286     -- transfer_reservation is called
15287     -- Post Insert CTO Validation
15288     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
15289       --
15290       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);
15291 
15292       debug_print(' After CTO API' || l_return_status);
15293       --
15294       IF l_return_status = fnd_api.g_ret_sts_error THEN
15295         RAISE fnd_api.g_exc_error;
15296       END IF;
15297 
15298       --
15299       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
15300         RAISE fnd_api.g_exc_unexpected_error;
15301       END IF;
15302     --
15303     END IF;
15304 
15305     /**** {{ R12 Enhanced reservations code changes }}****/
15306     -- Handling of serial numbers starts here. This block has the changes
15307     -- related to serial reservations.
15308 
15309     -- get all the from serials that have been reserved.
15310     --check to see if serial are reserved.
15311 
15312        BEGIN
15313 	  SELECT inventory_item_id, serial_number bulk collect INTO
15314 	    l_serial_number_table FROM
15315 	    mtl_serial_numbers  WHERE reservation_id =
15316 	    l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
15317 	    l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15318        EXCEPTION
15319 	  WHEN no_data_found THEN
15320 	     IF l_debug=1 THEN
15321 		debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
15322 	     END IF;
15323        END;
15324 
15325        l_total_serials_reserved := l_serial_number_table.COUNT;
15326        IF l_debug=1 THEN
15327 	  debug_print('Total reserved serials: ' || l_total_serials_reserved);
15328        END IF;
15329 
15330        -- exceed the reservation quantity.
15331        IF (l_to_row_exist) THEN
15332 	  l_to_reservation_id := l_to_rsv_tbl(1).reservation_id;
15333 	ELSIF (l_transfer_all) THEN
15334 	  l_to_reservation_id := l_orig_rsv_tbl(1).reservation_id;
15335 	ELSE
15336 	  l_to_reservation_id := l_reservation_id;
15337        END IF;
15338 
15339        --check to see if serial are reserved.
15340 	BEGIN
15341 	   SELECT primary_reservation_quantity INTO
15342 	     l_from_primary_reservation_qty FROM mtl_reservations WHERE
15343 	     reservation_id =  l_orig_rsv_tbl(1).reservation_id;
15344 	EXCEPTION
15345 	   WHEN no_data_found THEN
15346 	      IF l_debug=1 THEN
15347 		 debug_print('This is case 1. The from reservation recordhas been deleted. Id: ' || l_orig_rsv_tbl(1).reservation_id);
15348 	      END IF;
15349 	END;
15350 	BEGIN
15351 	   SELECT primary_reservation_quantity INTO
15352 	     l_to_primary_reservation_qty FROM mtl_reservations WHERE
15353 	     reservation_id =  l_to_reservation_id;
15354 	EXCEPTION
15355 	   WHEN no_data_found THEN
15356 	      IF l_debug=1 THEN
15357 		 debug_print('This is case 2. The to reservation record has been deleted. Id: ' || l_to_reservation_id);
15358 	      END IF;
15359 	END;
15360 	l_to_primary_reservation_qty :=
15361 	  Nvl(l_to_primary_reservation_qty,0);
15362 	l_from_primary_reservation_qty := Nvl(l_from_primary_reservation_qty,0);
15363 
15364 	debug_print(' Before Serial param' || l_return_status);
15365 
15366 	--Bug 5198421: If no serials are passed and no serial are reserved
15367 	-- set the serial reservation qty and do nothing.
15368 	IF ((l_serial_param = 1) AND (l_total_serials_reserved = 0)) THEN
15369 	   -- set the serial reservation qty to zero
15370 	   BEGIN
15371 	      UPDATE mtl_reservations SET serial_reservation_quantity = 0
15372 		WHERE reservation_id = l_orig_rsv_tbl(1).reservation_id;
15373 	   EXCEPTION
15374 	      WHEN no_data_found THEN
15375 		 IF l_debug=1 THEN
15376 		    debug_print('Could not find the reservation record: ' || l_orig_rsv_tbl(1).reservation_id);
15377 		 END IF;
15378 	   END;
15379 	END IF;
15380 
15381 
15382 	IF (l_serial_param = 1) AND (l_total_serials_reserved > 0) THEN
15383 	   -- This means that no serials are being passed.
15384 	   -- There are three conditions that we have to handle.
15385 	   --1. If transfer all is true and to row exists, then move everything
15386 	   -- to the to record
15387 	   --2. If transfer all is true and to row does not exist, then we have
15388 	   -- to do nothing as it is the same row that is being updated.
15389 	   --3. If transfer_all is false, then move the excess serials to the
15390 	   -- to record
15391 	   IF l_debug=1 THEN
15392 	      debug_print('Inside param 1');
15393 	      debug_print('Original tbl(1)Org id: ' || l_orig_rsv_tbl(1).organization_id);
15394 	      debug_print('Original tbl(1)Item id: ' || l_orig_rsv_tbl(1).inventory_item_id);
15395 	   END IF;
15396 
15397 	   IF ((l_total_serials_reserved >
15398 		l_from_primary_reservation_qty) OR (l_transfer_all AND
15399 						    (NOT l_to_row_exist))) THEN
15400 	      -- call validate serials for the to record as we may have to
15401 	     -- transfer the serials AT random.
15402 	     inv_reservation_validate_pvt.validate_serials
15403 	       (
15404 		x_return_status              => l_return_status
15405 		 , p_rsv_action_name         => 'TRANSFER'
15406 		, p_orig_rsv_rec             => l_dummy_rsv_rec
15407 		, p_to_rsv_rec               => l_to_rsv_rec
15408 		, p_orig_serial_array        => l_dummy_serial_array
15409 		, p_to_serial_array          => l_serial_number_table
15410 		);
15411 
15412 	     IF (l_debug = 1) THEN
15413 		debug_print('After calling validate serials ' || l_return_status);
15414 	     END IF;
15415 
15416 	     --
15417 	     IF l_return_status = fnd_api.g_ret_sts_error THEN
15418 		RAISE fnd_api.g_exc_error;
15419 	     END IF;
15420 
15421 	     --
15422 	     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
15423 		RAISE fnd_api.g_exc_unexpected_error;
15424 	     END IF;
15425 	  END IF;
15426 
15427 	  IF (l_total_serials_reserved > 0) THEN
15428 	     IF (l_transfer_all AND l_to_row_exist) THEN
15429 		debug_print('Inside param 1. transfer_all and to_row_exists');
15430 		-- validate the serials with the to record and move everything.
15431 		IF (l_debug = 1) THEN
15432 		   debug_print('Original serial count: ' || l_original_serial_count);
15433 		   debug_print('To serial count: ' || l_to_serial_count);
15434 		END IF;
15435 
15436 		-- both from and to serial tables are empty. They are not passed.
15437 		IF (l_debug = 1) THEN
15438 		   debug_print('Inside serial check. Not passed');
15439 		END IF;
15440 
15441 		IF (l_total_serials_reserved > l_to_primary_reservation_qty) THEN
15442 		   fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
15443 		   fnd_msg_pub.ADD;
15444 		   RAISE fnd_api.g_exc_error;
15445 		END IF;
15446 		-- we will have to migrate the serials to the new reservation
15447 		-- Just make sure that the group_mark_id and the
15448 		-- reservation id are populated.
15449 		FOR l_serial_index IN l_serial_number_table.first..l_serial_number_table.last
15450 		  LOOP
15451 	            BEGIN
15452 		       UPDATE mtl_serial_numbers SET reservation_id = l_to_reservation_id,
15453 			 group_mark_id = l_to_reservation_id WHERE
15454 			 serial_number = l_serial_number_table(l_serial_index).serial_number AND
15455 			 inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id AND
15456 			 current_organization_id = l_orig_rsv_tbl(1).organization_id;
15457 		    EXCEPTION
15458 		       WHEN no_data_found THEN
15459 			  IF l_debug=1 THEN
15460 			     debug_print('No serials found for serial number. : ' || l_serial_number_table(l_serial_index).serial_number);
15461 			  END IF;
15462 			  fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
15463 			  fnd_msg_pub.ADD;
15464 			  RAISE fnd_api.g_exc_error;
15465 		    END;
15466 		    IF l_debug=1 THEN
15467 		       debug_print('Serial being migrated. serial number: ' || l_serial_number_table(l_serial_index).serial_number);
15468 		    END IF;
15469 		  END LOOP;
15470 
15471 		  -- update the serial reservation quantity
15472 		  update_serial_rsv_quantity
15473 		    (x_return_status => l_return_status
15474 		     , x_msg_count   => x_msg_count
15475 		     , x_msg_data    => x_msg_data
15476 		     , p_reservation_id  => l_to_reservation_id
15477 		     , p_update_serial_qty => l_total_serials_reserved
15478 		     );
15479 
15480 		  IF (l_debug = 1) THEN
15481 		     debug_print('After calling update serial reservations ' || l_return_status);
15482 		  END IF;
15483 
15484 		  IF l_return_status = fnd_api.g_ret_sts_error THEN
15485 		     RAISE fnd_api.g_exc_error;
15486 		  END IF;
15487 
15488 		  IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
15489 		     RAISE fnd_api.g_exc_unexpected_error;
15490 		  END IF;
15491 
15492 	      ELSIF (NOT l_transfer_all) THEN
15493 	         -- transfer the excess to the to record there are some serial reserved. Check if the
15494 		 -- number of serials
15495 		IF (l_debug = 1) THEN
15496 			debug_print('Inside param 1. Not l_transfer_all');
15497 		END IF;
15498 
15499 		IF (l_total_serials_reserved > l_from_primary_reservation_qty) THEN
15500 		   -- we have to unreserve some of the serials
15501 		   -- unreserve the extra serials that are more than the primary_reservation_quantity
15502 		   IF (l_debug = 1) THEN
15503 		      debug_print('Total serials more than from reservation quantity.');
15504 		   END IF;
15505 
15506 		   l_serials_tobe_unreserved := l_total_serials_reserved - l_from_primary_reservation_qty;
15507 
15508 		   FOR i IN 1..l_serials_tobe_unreserved
15509 		     LOOP
15510 	               BEGIN
15511 			  UPDATE mtl_serial_numbers SET reservation_id = l_to_reservation_id,
15512 			    group_mark_id = l_to_reservation_id WHERE
15513 			    serial_number = l_serial_number_table(i).serial_number AND
15514 			    inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id AND
15515 			    current_organization_id = l_orig_rsv_tbl(1).organization_id;
15516 		       EXCEPTION
15517 			  WHEN no_data_found THEN
15518 			     IF l_debug=1 THEN
15519 				debug_print('No serials found for Serial Number: ' || l_serial_number_table(i).serial_number);
15520 			     END IF;
15521 			     fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
15522 			     fnd_msg_pub.ADD;
15523 			     RAISE fnd_api.g_exc_error;
15524 		       END;
15525 
15526 		       IF l_debug=1 THEN
15527 			  debug_print('Serial being unreserved. serial number: ' || l_serial_number_table(i).serial_number);
15528 		       END IF;
15529 
15530 		     END LOOP;
15531 		     -- update the serial reservation quantity to be the primary
15532 		     -- reservation quantity, as it is in excess.
15533 		     -- update the serial reservation quantity
15534 		     update_serial_rsv_quantity
15535 		       (x_return_status => l_return_status
15536 			, x_msg_count   => x_msg_count
15537 			, x_msg_data    => x_msg_data
15538 			, p_reservation_id  => l_orig_rsv_tbl(1).reservation_id
15539 			, p_update_serial_qty => -l_serials_tobe_unreserved
15540 			);
15541 
15542 		     IF (l_debug = 1) THEN
15543 			debug_print('After calling update serial reservations ' || l_return_status);
15544 		     END IF;
15545 
15546 		     IF l_return_status = fnd_api.g_ret_sts_error THEN
15547 			RAISE fnd_api.g_exc_error;
15548 		     END IF;
15549 
15550 		     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
15551 			RAISE fnd_api.g_exc_unexpected_error;
15552 		     END IF;
15553 
15554 		     update_serial_rsv_quantity
15555 		       (x_return_status => l_return_status
15556 			, x_msg_count   => x_msg_count
15557 			, x_msg_data    => x_msg_data
15558 			, p_reservation_id  => l_to_reservation_id
15559 			, p_update_serial_qty => l_serials_tobe_unreserved
15560 			);
15561 
15562 		     IF (l_debug = 1) THEN
15563 			debug_print('After calling update serial reservations ' || l_return_status);
15564 		     END IF;
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 		     IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
15571 			RAISE fnd_api.g_exc_unexpected_error;
15572 		     END IF;
15573 
15574 		     -- check to see if we are transferring more than the
15575 		     -- reserved qty
15576 		     BEGIN
15577 			SELECT COUNT(1) INTO l_total_to_serials_reserved FROM
15578 			  mtl_serial_numbers  WHERE reservation_id =
15579 			  l_to_reservation_id AND current_organization_id =
15580 			  l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15581 		     EXCEPTION
15582 			WHEN no_data_found THEN
15583 			   IF l_debug=1 THEN
15584 			      debug_print('No serials found for reservation record. id: ' || l_to_reservation_id);
15585 			   END IF;
15586 		     END;
15587 		     IF (l_total_to_serials_reserved > l_to_primary_reservation_qty) THEN
15588 			fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
15589 			fnd_msg_pub.ADD;
15590 			RAISE fnd_api.g_exc_error;
15591 		     END IF;
15592 
15593 		END IF; -- total serials more than orig reservation qty
15594 
15595 	      ELSIF (l_transfer_all AND (NOT l_to_row_exist)) THEN
15596 			     -- make sure that the total serials doesnt
15597 			     -- exceed the to reservation count and they
15598 			     -- are validated.
15599 	        IF (l_total_serials_reserved > l_to_primary_reservation_qty) THEN
15600 		   fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
15601 		   fnd_msg_pub.ADD;
15602 		   RAISE fnd_api.g_exc_error;
15603 		END IF;
15604 	  END IF; -- transfer all and to row exists
15605 
15606        END IF; -- serials reserved
15607     END IF;-- l_serial_param = 1
15608 
15609     IF (l_serial_param = 2) THEN
15610        -- We will have to unreserve the from serials and if the serial count
15611        -- exceeds the from primary qty, transfer them to the to record
15612 
15613        -- validate if the passed serials belong to the from reservation
15614        -- record.
15615        IF l_debug=1 THEN
15616 	  debug_print('Inside param 2');
15617 	  debug_print('Original tbl(1)Org id: ' || l_orig_rsv_tbl(1).organization_id);
15618 	  debug_print('Original tbl(1)Item id: ' || l_orig_rsv_tbl(1).inventory_item_id);
15619        END IF;
15620 
15621        -- The serial has to be reserved to the from record
15622        FOR i IN p_original_serial_number.first..p_original_serial_number.last
15623 	 LOOP
15624 	   BEGIN
15625 	      SELECT reservation_id INTO l_from_reservation_id FROM mtl_serial_numbers WHERE
15626 		serial_number = p_original_serial_number(i).serial_number AND
15627 		current_organization_id = l_orig_rsv_tbl(1).organization_id AND
15628 		inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15629 	   EXCEPTION
15630 	      WHEN no_data_found THEN
15631 		 IF l_debug=1 THEN
15632 		    debug_print('No serials found for this data : ' || p_to_serial_number(i).serial_number);
15633 		 END IF;
15634 	   END;
15635 
15636 	   -- if the serial is not reserved or if the serial belongs to a
15637 	   -- different reservation record, then fail
15638 	   IF (l_from_reservation_id IS NOT NULL AND l_from_reservation_id <>
15639 	       l_orig_rsv_tbl(1).reservation_id) OR (l_from_reservation_id IS NULL) THEN
15640 	      fnd_message.set_name('INV', 'INV_INVALID_FROM_SERIAL');
15641 	      fnd_msg_pub.ADD;
15642 	      RAISE fnd_api.g_exc_error;
15643 	   END IF;
15644 
15645 	   IF (l_total_serials_reserved > 0) THEN
15646 	   -- unreserve the passed serials.
15647 	     BEGIN
15648 		UPDATE mtl_serial_numbers SET reservation_id = NULL,
15649 		  group_mark_id = NULL, line_mark_id = NULL,
15650 		  lot_line_mark_id = NULL WHERE
15651 		  serial_number = p_original_serial_number(i).serial_number AND
15652 		  current_organization_id = l_orig_rsv_tbl(1).organization_id AND
15653 		  inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15654 	     EXCEPTION
15655 		WHEN no_data_found THEN
15656 		   IF l_debug=1 THEN
15657 		      debug_print('No serials found for this data : ' || p_to_serial_number(i).serial_number);
15658 		   END IF;
15659 	     END;
15660 	   END IF;
15661 	 END LOOP;
15662 
15663 	 IF (l_total_serials_reserved > 0) THEN
15664 	    IF (l_transfer_all AND l_to_row_exist) THEN
15665 	       -- if more serials are reserved,
15666 	       -- transfer them to the to record
15667 	       -- validate the serials before transferring them to to
15668 	       -- record.
15669 	       IF (l_debug = 1) THEN
15670 		  debug_print('Inside param 2. transfer_all and to_row_exists');
15671 	       END IF;
15672 
15673 	       BEGIN
15674 		  SELECT inventory_item_id, serial_number bulk collect INTO
15675 		    l_validate_serial_number_table FROM
15676 		    mtl_serial_numbers  WHERE reservation_id =
15677 		    l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
15678 		    l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15679 	       EXCEPTION
15680 		  WHEN no_data_found THEN
15681 		     IF l_debug=1 THEN
15682 			debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
15683 		     END IF;
15684 	       END;
15685 
15686 	       l_validate_serials_reserved := l_validate_serial_number_table.COUNT;
15687 
15688 	       IF l_debug=1 THEN
15689 		  debug_print('Total reserved serials: ' || l_validate_serials_reserved);
15690 	       END IF;
15691 
15692 	       IF (l_validate_serials_reserved > 0) THEN
15693 		  -- from record still has some reserved serials afer
15694 		  -- unreserving the passed serials. transfer them to the to_record
15695 		  inv_reservation_validate_pvt.validate_serials
15696 		    (
15697 		     x_return_status              => l_return_status
15698 		     , p_rsv_action_name          => 'TRANSFER'
15699 		     , p_orig_rsv_rec             => l_dummy_rsv_rec
15700 		     , p_to_rsv_rec               => l_to_rsv_rec
15701 		     , p_orig_serial_array        => l_dummy_serial_array
15702 		     , p_to_serial_array          => l_validate_serial_number_table
15703 		     );
15704 
15705 		  IF (l_debug = 1) THEN
15706 		     debug_print('After calling validate serials ' || l_return_status);
15707 		  END IF;
15708 
15709 		  --
15710 		  IF l_return_status = fnd_api.g_ret_sts_error THEN
15711 		     RAISE fnd_api.g_exc_error;
15712 		  END IF;
15713 
15714 		  --
15715 		  IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
15716 		     RAISE fnd_api.g_exc_unexpected_error;
15717 		  END IF;
15718 	       END IF;
15719 
15720                BEGIN
15721 		  UPDATE mtl_serial_numbers SET reservation_id = l_to_reservation_id,
15722 		    group_mark_id = l_to_reservation_id WHERE
15723 		    reservation_id = l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
15724 		    l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15725 
15726 	       EXCEPTION
15727 		  WHEN no_data_found THEN
15728 		     IF l_debug=1 THEN
15729 			debug_print('No serials found for this data. rsv id: ' || l_orig_rsv_tbl(1).reservation_id);
15730 		     END IF;
15731 		     fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
15732 		     fnd_msg_pub.ADD;
15733 		     RAISE fnd_api.g_exc_error;
15734 	       END;
15735 
15736 	       -- check to see if we have reserved more than the to_record
15737 	       BEGIN
15738 		  SELECT COUNT(1) INTO l_total_to_serials_reserved FROM
15739 		    mtl_serial_numbers  WHERE reservation_id =
15740 		    l_to_reservation_id AND current_organization_id =
15741 		    l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15742 	       EXCEPTION
15743 		  WHEN no_data_found THEN
15744 		     IF l_debug=1 THEN
15745 			debug_print('No serials found for reservation record. id: ' || l_to_reservation_id);
15746 		     END IF;
15747 	       END;
15748 
15749 	       IF (l_total_to_serials_reserved > l_to_primary_reservation_qty) THEN
15750 		  fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
15751 		  fnd_msg_pub.ADD;
15752 		  RAISE fnd_api.g_exc_error;
15753 	       END IF;
15754 
15755 		 -- update with whatever is being moved to the to record
15756 		 update_serial_rsv_quantity
15757 		   (x_return_status => l_return_status
15758 		    , x_msg_count   => x_msg_count
15759 		    , x_msg_data    => x_msg_data
15760 		    , p_reservation_id  => l_to_reservation_id
15761 		    , p_update_serial_qty => l_validate_serial_number_table.count
15762 		    );
15763 
15764 		 IF (l_debug = 1) THEN
15765 		    debug_print('After calling update serial reservations ' || l_return_status);
15766 		 END IF;
15767 
15768 		 IF l_return_status = fnd_api.g_ret_sts_error THEN
15769 		    RAISE fnd_api.g_exc_error;
15770 		 END IF;
15771 
15772 		 IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
15773 		    RAISE fnd_api.g_exc_unexpected_error;
15774 		 END IF;
15775 
15776 	     ELSIF (NOT l_transfer_all) THEN
15777 		 --the from serials are already unreserved. If there are
15778 		 -- more serials, validate and move the excess to the
15779 		 -- to record
15780 		     IF (l_debug = 1) THEN
15781 			debug_print('Inside param 2. not transfer_all');
15782 		     END IF;
15783 		 BEGIN
15784 		    SELECT inventory_item_id, serial_number bulk collect INTO
15785 		      l_validate_serial_number_table FROM
15786 		      mtl_serial_numbers  WHERE reservation_id =
15787 		      l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
15788 		      l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15789 		 EXCEPTION
15790 		    WHEN no_data_found THEN
15791 		       IF l_debug=1 THEN
15792 			  debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
15793 		       END IF;
15794 		 END;
15795 
15796 		 l_validate_serials_reserved := l_validate_serial_number_table.COUNT;
15797 
15798 		 IF l_debug=1 THEN
15799 		    debug_print('Total reserved serials: ' || l_validate_serials_reserved);
15800 		 END IF;
15801 
15802 		 IF (l_validate_serials_reserved > 0) AND (l_validate_serials_reserved > l_from_primary_reservation_qty) THEN
15803 		    inv_reservation_validate_pvt.validate_serials
15804 		      (
15805 		       x_return_status              => l_return_status
15806 		        , p_rsv_action_name         => 'TRANSFER'
15807 		       , p_orig_rsv_rec             => l_dummy_rsv_rec
15808 		       , p_to_rsv_rec               => l_to_rsv_rec
15809 		       , p_orig_serial_array        => l_dummy_serial_array
15810 		       , p_to_serial_array          => l_validate_serial_number_table
15811 		       );
15812 
15813 		    IF (l_debug = 1) THEN
15814 		       debug_print('After calling validate serials ' || l_return_status);
15815 		    END IF;
15816 
15817 		    --
15818 		    IF l_return_status = fnd_api.g_ret_sts_error THEN
15819 		       RAISE fnd_api.g_exc_error;
15820 		    END IF;
15821 
15822 		    --
15823 		    IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
15824 		       RAISE fnd_api.g_exc_unexpected_error;
15825 		    END IF;
15826 
15827 		    -- we have to unreserve some of the serials
15828 		    -- unreserve the extra serials that are more than the primary_reservation_quantity
15829 		    IF (l_debug = 1) THEN
15830 		       debug_print('Total serials more than reservation quantity.');
15831 		    END IF;
15832 
15833 		    l_serials_tobe_unreserved := l_validate_serials_reserved -  l_from_primary_reservation_qty;
15834 
15835 		    FOR i IN 1..l_serials_tobe_unreserved
15836 		      LOOP
15837 	               BEGIN
15838 			  UPDATE mtl_serial_numbers SET reservation_id = l_to_reservation_id,
15839 			    group_mark_id = l_to_reservation_id WHERE
15840 			    serial_number = l_validate_serial_number_table(i).serial_number AND
15841 			    inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id AND
15842 			    current_organization_id = l_orig_rsv_tbl(1).organization_id;
15843 		       EXCEPTION
15844 			  WHEN no_data_found THEN
15845 			     IF l_debug=1 THEN
15846 				debug_print('No serials found for serial number: ' || l_validate_serial_number_table(i).serial_number);
15847 			     END IF;
15848 			     fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
15849 			     fnd_msg_pub.ADD;
15850 			     RAISE fnd_api.g_exc_error;
15851 		       END;
15852 
15853 		       IF l_debug=1 THEN
15854 			  debug_print('Serial being unreserved. serial number: ' || l_serial_number_table(i).serial_number);
15855 		       END IF;
15856 
15857 		      END LOOP;
15858 
15859 		      BEGIN
15860 			 SELECT COUNT(1) INTO l_total_to_serials_reserved FROM
15861 			   mtl_serial_numbers  WHERE reservation_id =
15862 			   l_to_reservation_id AND current_organization_id =
15863 			   l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15864 		      EXCEPTION
15865 			 WHEN no_data_found THEN
15866 			    IF l_debug=1 THEN
15867 			       debug_print('No serials found for reservation record. id: ' || l_to_reservation_id);
15868 			    END IF;
15869 		      END;
15870 
15871 		      IF (l_total_to_serials_reserved > l_to_primary_reservation_qty) THEN
15872 			 fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
15873 			 fnd_msg_pub.ADD;
15874 			 RAISE fnd_api.g_exc_error;
15875 		      END IF;
15876 
15877 		      update_serial_rsv_quantity
15878 			(x_return_status => l_return_status
15879 			 , x_msg_count   => x_msg_count
15880 			 , x_msg_data    => x_msg_data
15881 			 , p_reservation_id  => l_to_reservation_id
15882 			 , p_update_serial_qty => l_serials_tobe_unreserved
15883 			 );
15884 
15885 		      IF (l_debug = 1) THEN
15886 			 debug_print('After calling update serial reservations ' || l_return_status);
15887 		      END IF;
15888 
15889 		      IF l_return_status = fnd_api.g_ret_sts_error THEN
15890 			 RAISE fnd_api.g_exc_error;
15891 		      END IF;
15892 
15893 		      IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
15894 			 RAISE fnd_api.g_exc_unexpected_error;
15895 		      END IF;
15896 		 END IF; -- total serials more than orig reservation qty
15897 		 -- now update the from reservation record as we have to
15898 		 -- update the unreserved serial and the transferred serial
15899 		 l_serials_unreserved := -(p_original_serial_number.COUNT + Nvl(l_serials_tobe_unreserved,0));
15900 		 IF (l_debug = 1) THEN
15901 		    debug_print('Total serials unreserved ' || l_serials_unreserved);
15902 		 END IF;
15903 		 -- update the serial reservation quantity to be the primary
15904 		 -- reservation quantity, as it is in excess.
15905 		 -- update the serial reservation quantity
15906 		 update_serial_rsv_quantity
15907 		   (x_return_status => l_return_status
15908 		    , x_msg_count   => x_msg_count
15909 		    , x_msg_data    => x_msg_data
15910 		    , p_reservation_id  => l_orig_rsv_tbl(1).reservation_id
15911 		    , p_update_serial_qty => l_serials_unreserved
15912 		    );
15913 
15914 		 IF (l_debug = 1) THEN
15915 		    debug_print('After calling update serial reservation qty ' || l_return_status);
15916 		 END IF;
15917 
15918 		 IF l_return_status = fnd_api.g_ret_sts_error THEN
15919 		    RAISE fnd_api.g_exc_error;
15920 		 END IF;
15921 
15922 		 IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
15923 		    RAISE fnd_api.g_exc_unexpected_error;
15924 		 END IF;
15925 
15926 	     ELSIF (l_transfer_all AND (NOT l_to_row_exist)) THEN
15927 		    -- we have already unreserved the serials. Since we are
15928 		    -- working on the same record, we will have to update
15929 		    -- the serial reservation quantity
15930 
15931 		IF (l_debug = 1) THEN
15932 		   debug_print('Inside param 2. transfer_all and not to_row_exist');
15933 		END IF;
15934                 BEGIN
15935 		   SELECT inventory_item_id, serial_number bulk collect INTO
15936 		     l_validate_serial_number_table FROM
15937 		     mtl_serial_numbers  WHERE reservation_id =
15938 		     l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
15939 		     l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
15940 		EXCEPTION
15941 		   WHEN no_data_found THEN
15942 		      IF l_debug=1 THEN
15943 			 debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
15944 		      END IF;
15945 		END;
15946 
15947 		l_validate_serials_reserved := l_validate_serial_number_table.COUNT;
15948 
15949 		IF l_debug=1 THEN
15950 		   debug_print('Total reserved serials: ' || l_validate_serials_reserved);
15951 		END IF;
15952 
15953 		IF (l_validate_serials_reserved > l_to_primary_reservation_qty) THEN
15954 		   fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
15955 		   fnd_msg_pub.ADD;
15956 		   RAISE fnd_api.g_exc_error;
15957 		END IF;
15958 
15959 		IF (l_validate_serials_reserved > 0) THEN
15960 		   inv_reservation_validate_pvt.validate_serials
15961 		     (
15962 		      x_return_status              => l_return_status
15963 		      , p_rsv_action_name         => 'TRANSFER'
15964 		      , p_orig_rsv_rec             => l_dummy_rsv_rec
15965 		      , p_to_rsv_rec               => l_to_rsv_rec
15966 		      , p_orig_serial_array        => l_dummy_serial_array
15967 		      , p_to_serial_array          => l_validate_serial_number_table
15968 		      );
15969 
15970 		   IF (l_debug = 1) THEN
15971 		      debug_print('After calling validate serials ' || l_return_status);
15972 		   END IF;
15973 
15974 		   --
15975 		   IF l_return_status = fnd_api.g_ret_sts_error THEN
15976 		      RAISE fnd_api.g_exc_error;
15977 		   END IF;
15978 
15979 		   --
15980 		   IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
15981 		      RAISE fnd_api.g_exc_unexpected_error;
15982 		   END IF;
15983 
15984 		   -- we have to unreserve some of the serials
15985 		   -- unreserve the extra serials that are more than the primary_reservation_quantity
15986 		   IF (l_debug = 1) THEN
15987 		      debug_print('Total serials more than reservation quantity.');
15988 		   END IF;
15989 		END IF;
15990 
15991 		update_serial_rsv_quantity
15992 		  (x_return_status => l_return_status
15993 		   , x_msg_count   => x_msg_count
15994 		   , x_msg_data    => x_msg_data
15995 		   , p_reservation_id  => l_orig_rsv_tbl(1).reservation_id
15996 		   , p_update_serial_qty => -p_original_serial_number.COUNT
15997 		   );
15998 
15999 		IF (l_debug = 1) THEN
16000 		   debug_print('After calling update serial reservation qty ' || l_return_status);
16001 		END IF;
16002 
16003 		IF l_return_status = fnd_api.g_ret_sts_error THEN
16004 		       RAISE fnd_api.g_exc_error;
16005 		END IF;
16006 
16007 		IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
16008 		   RAISE fnd_api.g_exc_unexpected_error;
16009 		END IF;
16010 
16011 	    END IF;-- transfer all and to row exists
16012 	 END IF ; -- total serials reserved > 0
16013     END IF; -- param = 2
16014 
16015     IF (l_serial_param = 3) THEN
16016        -- to serials are passed but not the from serial.
16017        -- reserve the serials. if the serial belongs to the from record,
16018        -- transfer it to the to record.
16019 
16020        IF l_debug=1 THEN
16021 	  debug_print('Inside param 3');
16022 	  debug_print('Original tbl(1)Org id: ' || l_orig_rsv_tbl(1).organization_id);
16023 	  debug_print('Original tbl(1)Item id: ' || l_orig_rsv_tbl(1).inventory_item_id);
16024        END IF;
16025 
16026        -- validate the serial numbers passed and reserve them
16027 
16028        FOR i IN p_to_serial_number.first..p_to_serial_number.last
16029 	 LOOP
16030 	   BEGIN
16031 	      IF (l_debug = 1) THEN
16032 		 debug_print('Processing serial number' || p_to_serial_number(i).serial_number);
16033 	      END IF;
16034 
16035 	      SELECT reservation_id, group_mark_id INTO
16036 		l_from_reservation_id, l_group_mark_id FROM mtl_serial_numbers WHERE
16037 		serial_number = p_to_serial_number(i).serial_number AND
16038 		current_organization_id = l_orig_rsv_tbl(1).organization_id AND
16039 		inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
16040 	   EXCEPTION
16041 	      WHEN no_data_found THEN
16042 		 IF l_debug=1 THEN
16043 		    debug_print('No serials found for this data : ' ||
16044 				p_to_serial_number(i).serial_number);
16045 		 END IF;
16046 		 fnd_message.set_name('INV', 'INV_INVALID_FROM_SERIAL');
16047 		 fnd_msg_pub.ADD;
16048 		 RAISE fnd_api.g_exc_error;
16049 	   END;
16050 
16051 	   IF (l_from_reservation_id IS NOT NULL AND l_from_reservation_id <>
16052 	       l_orig_rsv_tbl(1).reservation_id) THEN
16053 	      fnd_message.set_name('INV', 'INV_INVALID_FROM_SERIAL');
16054 	      fnd_msg_pub.ADD;
16055 	      RAISE fnd_api.g_exc_error;
16056 	   END IF;
16057 
16058 	   -- If reservation id is null, then we are reserving a new
16059 	   -- serial. we should check to see if the group mark id is null or
16060 	   -- it is not being used by another transaction.
16061 
16062 	   IF (l_from_reservation_id IS NULL) AND (l_group_mark_id IS NOT NULL)  AND (l_group_mark_id <> -1)THEN
16063 	      IF (l_debug = 1) THEN
16064 		 debug_print('Group Mark Id is not null for serial ' || p_to_serial_number(i).serial_number);
16065 	      END IF;
16066 	      fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
16067 	      fnd_msg_pub.ADD;
16068 	      RAISE fnd_api.g_exc_error;
16069 	   END IF;
16070 
16071 	   BEGIN
16072 	      UPDATE mtl_serial_numbers SET reservation_id = l_to_reservation_id,
16073 		group_mark_id = l_to_reservation_id WHERE
16074 		serial_number = p_to_serial_number(i).serial_number AND
16075 		current_organization_id = l_orig_rsv_tbl(1).organization_id AND
16076 		inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
16077 	   EXCEPTION
16078 	      WHEN no_data_found THEN
16079 		 IF l_debug=1 THEN
16080 		    debug_print('No serials found for this data : ' || p_to_serial_number(i).serial_number);
16081 		 END IF;
16082 	   END;
16083 	 END LOOP;
16084 
16085 	 IF (l_transfer_all AND l_to_row_exist) THEN
16086 	    -- validate the from serial with the to record.
16087 	    -- if count + to reserved count exceeds primary qty - fail
16088 	    -- else update from to to
16089 
16090 	    IF (l_debug = 1) THEN
16091 	       debug_print('Inside param 3. transfer_all and to_row_exist');
16092 	    END IF;
16093 
16094 	    -- we need to validate only if there are more serials reserved
16095 	    -- and we need to transfer to the to_record.
16096 	    IF (l_total_serials_reserved > 0) THEN
16097 	       -- call validate serials for the to record.
16098 	       inv_reservation_validate_pvt.validate_serials
16099 		 (
16100 		  x_return_status              => l_return_status
16101 		  , p_rsv_action_name         => 'TRANSFER'
16102 		  , p_orig_rsv_rec             => l_dummy_rsv_rec
16103 		  , p_to_rsv_rec               => l_to_rsv_rec
16104 		  , p_orig_serial_array        => l_dummy_serial_array
16105 		  , p_to_serial_array          => l_serial_number_table
16106 		  );
16107 
16108 	       IF (l_debug = 1) THEN
16109 		  debug_print('After calling validate serials ' || l_return_status);
16110 	       END IF;
16111 
16112 	       --
16113 	       IF l_return_status = fnd_api.g_ret_sts_error THEN
16114 		  RAISE fnd_api.g_exc_error;
16115 	       END IF;
16116 
16117 	       --
16118 	       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
16119 		  RAISE fnd_api.g_exc_unexpected_error;
16120 	       END IF;
16121 
16122 	    END IF;
16123 
16124 	    -- we will have to migrate the serials to the new reservation
16125 	    -- Just make sure that the group_mark_id and the
16126 	    -- reservation id are populated.
16127 	    FOR l_serial_index IN l_serial_number_table.first..l_serial_number_table.last
16128 	      LOOP
16129 	        BEGIN
16130 		   UPDATE mtl_serial_numbers SET reservation_id = l_to_reservation_id,
16131 		     group_mark_id = l_to_reservation_id WHERE
16132 		     serial_number = l_serial_number_table(l_serial_index).serial_number AND
16133 		     inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id AND
16134 		     current_organization_id = l_orig_rsv_tbl(1).organization_id;
16135 		EXCEPTION
16136 		   WHEN no_data_found THEN
16137 		      IF l_debug=1 THEN
16138 			 debug_print('No serials found for the serial number: ' || l_serial_number_table(l_serial_index).serial_number);
16139 		      END IF;
16140 		      fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
16141 		      fnd_msg_pub.ADD;
16142 		      RAISE fnd_api.g_exc_error;
16143 		END;
16144 
16145 		IF l_debug=1 THEN
16146 		   debug_print('Serial being migrated. serial number: ' || l_serial_number_table(l_serial_index).serial_number);
16147 		END IF;
16148 
16149 	      END LOOP;
16150 
16151 	      BEGIN
16152 		 SELECT COUNT(1) INTO l_total_to_serials_reserved FROM
16153 		   mtl_serial_numbers  WHERE reservation_id =
16154 		   l_to_reservation_id AND current_organization_id =
16155 		   l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
16156 	      EXCEPTION
16157 		 WHEN no_data_found THEN
16158 		    IF l_debug=1 THEN
16159 		       debug_print('No serials found for reservation record. id: ' || l_to_reservation_id);
16160 		    END IF;
16161 	      END;
16162 
16163 	      IF (l_total_to_serials_reserved > l_to_primary_reservation_qty) THEN
16164 		 fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
16165 		 fnd_msg_pub.ADD;
16166 		 RAISE fnd_api.g_exc_error;
16167 	      END IF;
16168 
16169 	      -- update the to serial reservation quantity. dont have
16170 	      -- to update from as the from record is deleted.
16171 
16172 	      BEGIN
16173 		 UPDATE mtl_reservations SET serial_reservation_quantity
16174 		   = l_total_to_serials_reserved WHERE reservation_id =
16175 		   l_to_reservation_id;
16176 
16177 	      EXCEPTION
16178 		 WHEN no_data_found THEN
16179 		    IF l_debug=1 THEN
16180 		       debug_print('Update failed for from reservation record. id: ' || l_to_reservation_id);
16181 		    END IF;
16182 		    fnd_message.set_name('INV', 'INV_INVALID_ROW');
16183 		    fnd_msg_pub.ADD;
16184 		    RAISE fnd_api.g_exc_error;
16185 	      END;
16186 
16187 	      IF (l_debug = 1) THEN
16188 		 debug_print('After updating serial count of the to_record.');
16189 	      END IF;
16190 
16191 	      IF l_return_status = fnd_api.g_ret_sts_error THEN
16192 		 RAISE fnd_api.g_exc_error;
16193 	      END IF;
16194 
16195 	      IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
16196 		 RAISE fnd_api.g_exc_unexpected_error;
16197 	      END IF;
16198 
16199 	  ELSIF (l_transfer_all AND (NOT l_to_row_exist)) THEN
16200 		    -- validate the from serial with the to record.
16201 	      -- if count + to reserved count exceeds primary qty - fail
16202 		    -- else update from to to
16203 
16204 	      IF (l_debug = 1) THEN
16205 		 debug_print('Inside param 3. transfer_all and not to_row_exist');
16206 	      END IF;
16207 
16208 	      IF (l_total_serials_reserved > 0) THEN
16209 		 -- call validate serials for the to record.
16210 		 inv_reservation_validate_pvt.validate_serials
16211 		   (
16212 		    x_return_status              => l_return_status
16213 		    , p_rsv_action_name          => 'TRANSFER'
16214 		    , p_orig_rsv_rec             => l_dummy_rsv_rec
16215 		    , p_to_rsv_rec               => l_to_rsv_rec
16216 		    , p_orig_serial_array        => l_dummy_serial_array
16217 		    , p_to_serial_array          => l_serial_number_table
16218 		    );
16219 
16220 		 IF (l_debug = 1) THEN
16221 		    debug_print('After calling validate serials ' || l_return_status);
16222 		 END IF;
16223 
16224 		 --
16225 		 IF l_return_status = fnd_api.g_ret_sts_error THEN
16226 		    RAISE fnd_api.g_exc_error;
16227 		 END IF;
16228 
16229 		 --
16230 		 IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
16231 		    RAISE fnd_api.g_exc_unexpected_error;
16232 		 END IF;
16233 
16234 	      END IF;
16235 
16236 	      -- we have already reserved the serials. Since we are
16237 	      -- working on the same record, we will have to update
16238 	      -- the serial reservation quantity. check to see if the
16239 	      -- count exceeds
16240 	      BEGIN
16241 		 SELECT COUNT(1) INTO l_total_to_serials_reserved FROM
16242 		   mtl_serial_numbers  WHERE reservation_id =
16243 		   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;
16244 	      EXCEPTION
16245 		 WHEN no_data_found THEN
16246 		    IF l_debug=1 THEN
16247 		       debug_print('No serials found for reservation record. id: ' || l_to_reservation_id);
16248 		    END IF;
16249 	      END;
16250 
16251 	      IF (l_total_to_serials_reserved > l_to_primary_reservation_qty) THEN
16252 		 fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
16253 		 fnd_msg_pub.ADD;
16254 		 RAISE fnd_api.g_exc_error;
16255 	      END IF;
16256 
16257               BEGIN
16258 		 UPDATE mtl_reservations SET serial_reservation_quantity
16259 		   = l_total_to_serials_reserved WHERE reservation_id =
16260 		   l_orig_rsv_tbl(1).reservation_id;
16261 
16262 	      EXCEPTION
16263 		 WHEN no_data_found THEN
16264 		    IF l_debug=1 THEN
16265 		       debug_print('Update failed for from reservation record. id: ' || l_to_reservation_id);
16266 		    END IF;
16267 		    fnd_message.set_name('INV', 'INV_INVALID_ROW');
16268 		    fnd_msg_pub.ADD;
16269 		    RAISE fnd_api.g_exc_error;
16270 	      END;
16271 
16272 	      IF (l_debug = 1) THEN
16273 		 debug_print('After calling update serial reservation qty ' || l_return_status);
16274 	      END IF;
16275 
16276 	      IF l_return_status = fnd_api.g_ret_sts_error THEN
16277 		 RAISE fnd_api.g_exc_error;
16278 	      END IF;
16279 
16280 	      IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
16281 		 RAISE fnd_api.g_exc_unexpected_error;
16282 	      END IF;
16283 
16284 	  ELSIF (NOT l_transfer_all) THEN
16285 		      -- serials passed have already been reserved. make sure the
16286 		      -- count doesnt exceed.
16287 	      IF l_debug=1 THEN
16288 		 debug_print('Inside param 3. Not transfer all');
16289 		 debug_print('to reservation id: ' || l_to_reservation_id);
16290 		 debug_print('from reservation id: ' || l_orig_rsv_tbl(1).reservation_id);
16291 	      END IF;
16292 
16293 	      -- check the from serial count. if the from count exceeds the
16294 	      -- from reservation qty, transfer to the to record.
16295 	      IF (l_total_serials_reserved > 0) THEN
16296                BEGIN
16297 		  SELECT inventory_item_id, serial_number bulk collect INTO
16298 		    l_validate_serial_number_table FROM
16299 		    mtl_serial_numbers  WHERE reservation_id =
16300 		    l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
16301 		    l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
16302 	       EXCEPTION
16303 		  WHEN no_data_found THEN
16304 		     IF l_debug=1 THEN
16305 			debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
16306 		     END IF;
16307 	       END;
16308 
16309 	       l_total_from_serials_reserved := l_validate_serial_number_table.COUNT;
16310 
16311 	       IF (l_total_from_serials_reserved > l_from_primary_reservation_qty) THEN
16312 		  -- we have to transfer some of the serials to the to_record
16313 		  -- unreserve the extra serials that are more than the primary_reservation_quantity
16314 		  IF (l_debug = 1) THEN
16315 		     debug_print('Total serials more than from reservation quantity.');
16316 		  END IF;
16317 
16318 		  -- need to validate serials as we are going to transfer
16319 		  -- them at randon
16320 		  -- call validate serials for the to record.
16321 		  inv_reservation_validate_pvt.validate_serials
16322 		    (
16323 		     x_return_status              => l_return_status
16324 		     , p_rsv_action_name          => 'TRANSFER'
16325 		     , p_orig_rsv_rec             => l_dummy_rsv_rec
16326 		     , p_to_rsv_rec               => l_to_rsv_rec
16327 		     , p_orig_serial_array        => l_dummy_serial_array
16328 		     , p_to_serial_array          => l_validate_serial_number_table
16329 		     );
16330 
16331 		  IF (l_debug = 1) THEN
16332 		     debug_print('After calling validate serials ' || l_return_status);
16333 		  END IF;
16334 
16335 		  --
16336 		  IF l_return_status = fnd_api.g_ret_sts_error THEN
16337 		     RAISE fnd_api.g_exc_error;
16338 		  END IF;
16339 
16340 		  --
16341 		  IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
16342 		     RAISE fnd_api.g_exc_unexpected_error;
16343 		  END IF;
16344 
16345 
16346 		  l_serials_tobe_unreserved := l_total_from_serials_reserved - l_from_primary_reservation_qty;
16347 
16348 		  FOR i IN 1..l_serials_tobe_unreserved
16349 		    LOOP
16350 	             BEGIN
16351 			UPDATE mtl_serial_numbers SET
16352 			  reservation_id = l_to_reservation_id,
16353 			  group_mark_id = l_to_reservation_id WHERE
16354 			  serial_number = l_validate_serial_number_table(i).serial_number AND
16355 			  inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id AND
16356 			  current_organization_id = l_orig_rsv_tbl(1).organization_id;
16357 		     EXCEPTION
16358 			WHEN no_data_found THEN
16359 			   IF l_debug=1 THEN
16360 			      debug_print('No serials found for Serial Number: ' || l_serial_number_table(i).serial_number);
16361 			   END IF;
16362 			   fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
16363 			   fnd_msg_pub.ADD;
16364 			   RAISE fnd_api.g_exc_error;
16365 		     END;
16366 
16367 		     IF l_debug=1 THEN
16368 			debug_print('Serial being unreserved. serial number: ' || l_serial_number_table(i).serial_number);
16369 		     END IF;
16370 
16371 		    END LOOP;
16372 	       END IF;
16373 	      END IF;
16374 
16375               BEGIN
16376 		 SELECT COUNT(1) INTO l_total_to_serials_reserved FROM
16377 		   mtl_serial_numbers  WHERE reservation_id =
16378 		   l_to_reservation_id AND current_organization_id =
16379 		   l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
16380 	      EXCEPTION
16381 		 WHEN no_data_found THEN
16382 		    IF l_debug=1 THEN
16383 		       debug_print('No serials found for to reservation record. id: ' || l_to_reservation_id);
16384 		    END IF;
16385 	      END;
16386 
16387 	      IF (l_total_to_serials_reserved > l_to_primary_reservation_qty) THEN
16388 		 fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
16389 		 fnd_msg_pub.ADD;
16390 		 RAISE fnd_api.g_exc_error;
16391 	      END IF;
16392 
16393 	      BEGIN
16394 		 UPDATE mtl_reservations SET serial_reservation_quantity
16395 		   = l_total_to_serials_reserved WHERE reservation_id =
16396 		   l_to_reservation_id;
16397 
16398 	      EXCEPTION
16399 		 WHEN no_data_found THEN
16400 		    IF l_debug=1 THEN
16401 		       debug_print('Update failed for to reservation record. id: ' || l_to_reservation_id);
16402 		    END IF;
16403 		    fnd_message.set_name('INV', 'INV_INVALID_ROW');
16404 		    fnd_msg_pub.ADD;
16405 		    RAISE fnd_api.g_exc_error;
16406 	      END;
16407 
16408 	      BEGIN
16409 		 SELECT COUNT(1) INTO l_total_from_serials_reserved FROM
16410 		   mtl_serial_numbers  WHERE reservation_id =
16411 		   l_orig_rsv_tbl(1).reservation_id AND
16412 		   current_organization_id = l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
16413 	      EXCEPTION
16414 		 WHEN no_data_found THEN
16415 		    IF l_debug=1 THEN
16416 		       debug_print('No serials found for to reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
16417 		    END IF;
16418 	      END;
16419 
16420               BEGIN
16421 		 UPDATE mtl_reservations SET serial_reservation_quantity
16422 		   = l_total_from_serials_reserved WHERE reservation_id =
16423 		   l_orig_rsv_tbl(1).reservation_id;
16424 
16425 	      EXCEPTION
16426 		 WHEN no_data_found THEN
16427 		    IF l_debug=1 THEN
16428 		       debug_print('Update failed for from reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
16429 		    END IF;
16430 		    fnd_message.set_name('INV', 'INV_INVALID_ROW');
16431 		    fnd_msg_pub.ADD;
16432 		    RAISE fnd_api.g_exc_error;
16433 	      END;
16434 
16435 
16436 	 END if; -- transfer all and to row exist
16437     END IF; -- param = 3
16438 
16439 
16440     IF (l_serial_param = 4) THEN
16441 
16442        -- We will have to unreserve the from serials and if the serial count
16443        -- exceeds the from primary qty, transfer them to the to record
16444 
16445        -- validate if the passed serials belong to the from reservation
16446        -- record.
16447        IF l_debug=1 THEN
16448 	  debug_print('Inside param 2');
16449 	  debug_print('Original tbl(1)Org id: ' || l_orig_rsv_tbl(1).organization_id);
16450 	  debug_print('Original tbl(1)Item id: ' || l_orig_rsv_tbl(1).inventory_item_id);
16451        END IF;
16452        -- The serial has to be reserved to the from record
16453        FOR i IN p_original_serial_number.first..p_original_serial_number.last
16454 	 LOOP
16455 	   BEGIN
16456 	      SELECT reservation_id INTO l_from_reservation_id FROM mtl_serial_numbers WHERE
16457 		serial_number = p_original_serial_number(i).serial_number AND
16458 		current_organization_id = l_orig_rsv_tbl(1).organization_id AND
16459 		inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
16460 	   EXCEPTION
16461 	      WHEN no_data_found THEN
16462 		 IF l_debug=1 THEN
16463 		    debug_print('No serials found for this data : ' || p_original_serial_number(i).serial_number);
16464 		 END IF;
16465 	   END;
16466 
16467 	   -- if the serial is not reserved or if the serial belongs to a
16468 	   -- different reservation record, then fail
16469 	   IF (l_from_reservation_id IS NOT NULL AND l_from_reservation_id <>
16470 	       l_orig_rsv_tbl(1).reservation_id) OR (l_from_reservation_id IS NULL) THEN
16471 	      fnd_message.set_name('INV', 'INV_INVALID_FROM_SERIAL');
16472 	      fnd_msg_pub.ADD;
16473 	      RAISE fnd_api.g_exc_error;
16474 	   END IF;
16475 
16476 	   -- unreserve the passed serials.
16477 	     BEGIN
16478 		UPDATE mtl_serial_numbers SET reservation_id = NULL,
16479 		  group_mark_id = NULL, line_mark_id = NULL,
16480 		  lot_line_mark_id = NULL WHERE
16481 		  serial_number = p_original_serial_number(i).serial_number AND
16482 		  current_organization_id = l_orig_rsv_tbl(1).organization_id AND
16483 		  inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
16484 	     EXCEPTION
16485 		WHEN no_data_found THEN
16486 		   IF l_debug=1 THEN
16487 		      debug_print('No serials found for this data : ' || p_original_serial_number(i).serial_number);
16488 		   END IF;
16489 	     END;
16490 	 END LOOP;
16491 
16492 	 -- to serials are passed .
16493 	 -- reserve the serials. if the serial belongs to the from record,
16494 	 -- transfer it to the to record.
16495 
16496 	 -- validate the serial numbers passed and reserve them
16497 
16498 	 FOR i IN p_to_serial_number.first..p_to_serial_number.last
16499 	   LOOP
16500 	     BEGIN
16501 		SELECT reservation_id, group_mark_id INTO
16502 		  l_from_reservation_id, l_group_mark_id FROM mtl_serial_numbers WHERE
16503 		  serial_number = p_to_serial_number(i).serial_number AND
16504 		  current_organization_id = l_orig_rsv_tbl(1).organization_id AND
16505 		  inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
16506 	     EXCEPTION
16507 		WHEN no_data_found THEN
16508 		   IF l_debug=1 THEN
16509 		      debug_print('No serials found for this data : ' || p_to_serial_number(i).serial_number);
16510 		   END IF;
16511 		   fnd_message.set_name('INV', 'INV_INVALID_FROM_SERIAL');
16512 		   fnd_msg_pub.ADD;
16513 		   RAISE fnd_api.g_exc_error;
16514 	     END;
16515 
16516 	     IF (l_from_reservation_id IS NOT NULL AND l_from_reservation_id <>
16517 		 l_orig_rsv_tbl(1).reservation_id) THEN
16518 		fnd_message.set_name('INV', 'INV_INVALID_FROM_SERIAL');
16519 		fnd_msg_pub.ADD;
16520 		RAISE fnd_api.g_exc_error;
16521 	     END IF;
16522 
16523 	     IF (l_from_reservation_id IS NULL) AND (l_group_mark_id IS NOT NULL)  AND (l_group_mark_id <> -1)THEN
16524 		IF (l_debug = 1) THEN
16525 		   debug_print('Group Mark Id is not null for serial ' || p_to_serial_number(i).serial_number);
16526 		END IF;
16527 		fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
16528 		fnd_msg_pub.ADD;
16529 		RAISE fnd_api.g_exc_error;
16530 	     END IF;
16531 
16532 	     BEGIN
16533 		UPDATE mtl_serial_numbers SET reservation_id = l_to_reservation_id,
16534 		  group_mark_id = l_to_reservation_id WHERE
16535 		  serial_number = p_to_serial_number(i).serial_number AND
16536 		  current_organization_id = l_orig_rsv_tbl(1).organization_id AND
16537 		  inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
16538 	     EXCEPTION
16539 		WHEN no_data_found THEN
16540 		   IF l_debug=1 THEN
16541 		      debug_print('No serials found for this data : ' || p_to_serial_number(i).serial_number);
16542 		   END IF;
16543 	     END;
16544 	   END LOOP;
16545 
16546 	   IF (l_transfer_all AND l_to_row_exist) THEN
16547 	      -- if more serials are reserved,
16548 	      -- transfer them to the to record
16549 	      -- validate the serials before transferring them to to
16550 	      -- record.
16551 	      IF (l_debug = 1) THEN
16552 		 debug_print('Inside param 4. transfer_all and to_row_exist');
16553 	      END IF;
16554 
16555 	      IF (l_total_serials_reserved > 0) THEN
16556 	       BEGIN
16557 		  SELECT inventory_item_id, serial_number bulk collect INTO
16558 		    l_validate_serial_number_table FROM
16559 		    mtl_serial_numbers  WHERE reservation_id =
16560 		    l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
16561 		    l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
16562 	       EXCEPTION
16563 		  WHEN no_data_found THEN
16564 		     IF l_debug=1 THEN
16565 			debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
16566 		     END IF;
16567 	       END;
16568 
16569 	       l_validate_serials_reserved := l_validate_serial_number_table.COUNT;
16570 	       IF l_debug=1 THEN
16571 		  debug_print('Total reserved serials: ' || l_validate_serials_reserved);
16572 	       END IF;
16573 
16574 	       IF (l_validate_serials_reserved > 0) THEN
16575 		  inv_reservation_validate_pvt.validate_serials
16576 		    (
16577 		     x_return_status              => l_return_status
16578 		     , p_rsv_action_name         => 'TRANSFER'
16579 		     , p_orig_rsv_rec             => l_dummy_rsv_rec
16580 		     , p_to_rsv_rec               => l_to_rsv_rec
16581 		     , p_orig_serial_array        => l_dummy_serial_array
16582 		     , p_to_serial_array          => l_validate_serial_number_table
16583 		     );
16584 
16585 		  IF (l_debug = 1) THEN
16586 		     debug_print('After calling validate serials ' || l_return_status);
16587 		  END IF;
16588 
16589 		  --
16590 		  IF l_return_status = fnd_api.g_ret_sts_error THEN
16591 		     RAISE fnd_api.g_exc_error;
16592 		  END IF;
16593 
16594 		  --
16595 		  IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
16596 		     RAISE fnd_api.g_exc_unexpected_error;
16597 		  END IF;
16598 
16599 		  BEGIN
16600 		     UPDATE mtl_serial_numbers SET reservation_id = l_to_reservation_id,
16601 		       group_mark_id = l_to_reservation_id WHERE
16602 		       reservation_id = l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
16603 		       l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
16604 
16605 		  EXCEPTION
16606 		     WHEN no_data_found THEN
16607 			IF l_debug=1 THEN
16608 			   debug_print('No serials found for this data. rsv id: ' || l_orig_rsv_tbl(1).reservation_id);
16609 			END IF;
16610 			fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
16611 			fnd_msg_pub.ADD;
16612 			RAISE fnd_api.g_exc_error;
16613 		  END;
16614 	       END IF;
16615 	      END IF;
16616 
16617 	      BEGIN
16618 		 SELECT COUNT(1) INTO l_total_to_serials_reserved FROM
16619 		   mtl_serial_numbers  WHERE reservation_id =
16620 		   l_to_reservation_id AND current_organization_id =
16621 		   l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
16622 	      EXCEPTION
16623 		 WHEN no_data_found THEN
16624 		    IF l_debug=1 THEN
16625 		       debug_print('No serials found for reservation record. id: ' || l_to_reservation_id);
16626 		    END IF;
16627 	      END;
16628 
16629 	      IF (l_total_to_serials_reserved > l_to_primary_reservation_qty) THEN
16630 		 fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
16631 		 fnd_msg_pub.ADD;
16632 		 RAISE fnd_api.g_exc_error;
16633 	      END IF;
16634 
16635 	      BEGIN
16636 		 UPDATE mtl_reservations SET serial_reservation_quantity
16637 		   = l_total_to_serials_reserved WHERE reservation_id =
16638 		   l_orig_rsv_tbl(1).reservation_id;
16639 
16640 	      EXCEPTION
16641 		 WHEN no_data_found THEN
16642 		    IF l_debug=1 THEN
16643 		       debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
16644 		    END IF;
16645 		    fnd_message.set_name('INV', 'INV_INVALID_ROW');
16646 		    fnd_msg_pub.ADD;
16647 		    RAISE fnd_api.g_exc_error;
16648 	      END;
16649 
16650 	    ELSIF (l_transfer_all AND (NOT l_to_row_exist)) THEN
16651 
16652 		    -- validate the from serial with the to record.
16653 		    -- if count + to reserved count exceeds primary qty - fail
16654 		    -- else update from to to
16655 
16656 	      IF (l_debug = 1) THEN
16657 		 debug_print('Inside param 4. transfer_all and not to_row_exist');
16658 	      END IF;
16659 
16660 	      IF (l_total_serials_reserved > 0) THEN
16661 	        BEGIN
16662 		   SELECT inventory_item_id, serial_number bulk collect INTO
16663 		     l_validate_serial_number_table FROM
16664 		     mtl_serial_numbers  WHERE reservation_id =
16665 		     l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
16666 		     l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
16667 		EXCEPTION
16668 		   WHEN no_data_found THEN
16669 		      IF l_debug=1 THEN
16670 			 debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
16671 		      END IF;
16672 		END;
16673 
16674 		l_validate_serials_reserved := l_validate_serial_number_table.COUNT;
16675 		IF l_debug=1 THEN
16676 		   debug_print('Total reserved serials: ' || l_validate_serials_reserved);
16677 		END IF;
16678 
16679 		IF (l_validate_serials_reserved > 0) THEN
16680 		   inv_reservation_validate_pvt.validate_serials
16681 		     (
16682 		      x_return_status              => l_return_status
16683 		      , p_rsv_action_name         => 'TRANSFER'
16684 		      , p_orig_rsv_rec             => l_dummy_rsv_rec
16685 		      , p_to_rsv_rec               => l_to_rsv_rec
16686 		      , p_orig_serial_array        => l_dummy_serial_array
16687 		      , p_to_serial_array          => l_validate_serial_number_table
16688 		      );
16689 
16690 		   IF (l_debug = 1) THEN
16691 		      debug_print('After calling validate serials ' || l_return_status);
16692 		   END IF;
16693 
16694 		   --
16695 		   IF l_return_status = fnd_api.g_ret_sts_error THEN
16696 		      RAISE fnd_api.g_exc_error;
16697 		   END IF;
16698 
16699 		   --
16700 		   IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
16701 		      RAISE fnd_api.g_exc_unexpected_error;
16702 		   END IF;
16703 		END IF;
16704 	      END IF;
16705 
16706               BEGIN
16707 		 SELECT COUNT(1) INTO l_total_to_serials_reserved FROM
16708 		   mtl_serial_numbers  WHERE reservation_id =
16709 		   l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
16710 		   l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
16711 	      EXCEPTION
16712 		 WHEN no_data_found THEN
16713 		    IF l_debug=1 THEN
16714 		       debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
16715 		    END IF;
16716 	      END;
16717 	      IF (l_total_to_serials_reserved > l_to_primary_reservation_qty) THEN
16718 		 fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
16719 		 fnd_msg_pub.ADD;
16720 		 RAISE fnd_api.g_exc_error;
16721 	      END IF;
16722 
16723               BEGIN
16724 		 UPDATE mtl_reservations SET serial_reservation_quantity
16725 		   = l_total_to_serials_reserved WHERE reservation_id =
16726 		   l_orig_rsv_tbl(1).reservation_id;
16727 
16728 	      EXCEPTION
16729 		 WHEN no_data_found THEN
16730 		    IF l_debug=1 THEN
16731 		       debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
16732 		    END IF;
16733 		    fnd_message.set_name('INV', 'INV_INVALID_ROW');
16734 		    fnd_msg_pub.ADD;
16735 		    RAISE fnd_api.g_exc_error;
16736 	      END;
16737 
16738 	    ELSIF (NOT l_transfer_all) THEN
16739 		    -- check to see if there are excess serials in from
16740 		    -- record. if so, validate and move them to to record
16741 		    -- then count and update the serial count
16742 
16743 	      IF (l_debug = 1) THEN
16744 		 debug_print('Inside param 4. not transfer_all');
16745 	      END IF;
16746 
16747 	      IF (l_total_serials_reserved > 0) THEN
16748 	        BEGIN
16749 		   SELECT inventory_item_id, serial_number bulk collect INTO
16750 		     l_validate_serial_number_table FROM
16751 		     mtl_serial_numbers  WHERE reservation_id =
16752 		     l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
16753 		     l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
16754 		EXCEPTION
16755 		   WHEN no_data_found THEN
16756 		      IF l_debug=1 THEN
16757 			 debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
16758 		      END IF;
16759 		END;
16760 
16761 		l_validate_serials_reserved := l_validate_serial_number_table.COUNT;
16762 		IF l_debug=1 THEN
16763 		   debug_print('Total reserved serials: ' || l_validate_serials_reserved);
16764 		END IF;
16765 
16766 		IF (l_validate_serials_reserved > l_from_primary_reservation_qty) THEN
16767 		   -- we have to unreserve some of the serials
16768 		   -- unreserve the extra serials that are more than the primary_reservation_quantity
16769 		   IF (l_debug = 1) THEN
16770 		      debug_print('Total serials more than reservation quantity.');
16771 		   END IF;
16772 
16773 		   IF (l_validate_serials_reserved > 0) THEN
16774 		      inv_reservation_validate_pvt.validate_serials
16775 			(
16776 			 x_return_status              => l_return_status
16777 			 , p_rsv_action_name         => 'TRANSFER'
16778 			 , p_orig_rsv_rec             => l_dummy_rsv_rec
16779 			 , p_to_rsv_rec               => l_to_rsv_rec
16780 			 , p_orig_serial_array        => l_dummy_serial_array
16781 			 , p_to_serial_array          => l_validate_serial_number_table
16782 			 );
16783 
16784 		      IF (l_debug = 1) THEN
16785 			 debug_print('After calling validate serials ' || l_return_status);
16786 		      END IF;
16787 
16788 		      --
16789 		      IF l_return_status = fnd_api.g_ret_sts_error THEN
16790 			 RAISE fnd_api.g_exc_error;
16791 		      END IF;
16792 
16793 		      --
16794 		      IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
16795 			 RAISE fnd_api.g_exc_unexpected_error;
16796 		      END IF;
16797 
16798 		      l_serials_tobe_unreserved := l_validate_serials_reserved -  l_from_primary_reservation_qty;
16799 
16800 		      FOR i IN 1..l_serials_tobe_unreserved
16801 			LOOP
16802 	                  BEGIN
16803 			     UPDATE mtl_serial_numbers SET reservation_id = l_to_reservation_id,
16804 			       group_mark_id = l_to_reservation_id WHERE
16805 			       serial_number = l_validate_serial_number_table(i).serial_number AND
16806 			       inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id AND
16807 			       current_organization_id = l_orig_rsv_tbl(1).organization_id;
16808 			  EXCEPTION
16809 			     WHEN no_data_found THEN
16810 				IF l_debug=1 THEN
16811 				   debug_print('No serials found for serial number: ' || l_validate_serial_number_table(i).serial_number);
16812 				END IF;
16813 				fnd_message.set_name('INV', 'INV_INVALID_SERIAL');
16814 				fnd_msg_pub.ADD;
16815 				RAISE fnd_api.g_exc_error;
16816 			  END;
16817 
16818 			  IF l_debug=1 THEN
16819 			     debug_print('Serial being unreserved. serial number: ' || l_serial_number_table(i).serial_number);
16820 			  END IF;
16821 
16822 			END LOOP;
16823 		   END IF;
16824 		END IF;
16825 	      END IF;
16826 
16827 	      BEGIN
16828 		 SELECT COUNT(1) INTO l_total_to_serials_reserved FROM
16829 		   mtl_serial_numbers  WHERE reservation_id =
16830 		   l_to_reservation_id AND current_organization_id =
16831 		   l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
16832 	      EXCEPTION
16833 		 WHEN no_data_found THEN
16834 		    IF l_debug=1 THEN
16835 		       debug_print('No serials found for reservation record. id: ' || l_to_reservation_id);
16836 		    END IF;
16837 	      END;
16838 	      IF (l_total_to_serials_reserved > l_to_primary_reservation_qty) THEN
16839 		 fnd_message.set_name('INV', 'INV_SERIAL_QTY_MORE_THAN_RSV');
16840 		 fnd_msg_pub.ADD;
16841 		 RAISE fnd_api.g_exc_error;
16842 	      END IF;
16843 
16844 	      BEGIN
16845 		 UPDATE mtl_reservations SET serial_reservation_quantity
16846 		   = l_total_to_serials_reserved WHERE reservation_id =
16847 		   l_to_reservation_id;
16848 
16849 	      EXCEPTION
16850 		 WHEN no_data_found THEN
16851 		    IF l_debug=1 THEN
16852 		       debug_print('No serials found for reservation record. id: ' || l_to_reservation_id);
16853 		    END IF;
16854 		    fnd_message.set_name('INV', 'INV_INVALID_ROW');
16855 		    fnd_msg_pub.ADD;
16856 		    RAISE fnd_api.g_exc_error;
16857 	      END;
16858 
16859               BEGIN
16860 		 SELECT COUNT(1) INTO l_total_from_serials_reserved FROM
16861 		   mtl_serial_numbers  WHERE reservation_id =
16862 		   l_orig_rsv_tbl(1).reservation_id AND current_organization_id =
16863 		   l_orig_rsv_tbl(1).organization_id AND inventory_item_id = l_orig_rsv_tbl(1).inventory_item_id;
16864 	      EXCEPTION
16865 		 WHEN no_data_found THEN
16866 		    IF l_debug=1 THEN
16867 		       debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
16868 		    END IF;
16869 	      END;
16870 
16871               BEGIN
16872 		 UPDATE mtl_reservations SET serial_reservation_quantity
16873 		   = l_total_from_serials_reserved WHERE reservation_id =
16874 		   l_orig_rsv_tbl(1).reservation_id;
16875 
16876 	      EXCEPTION
16877 		 WHEN no_data_found THEN
16878 		    IF l_debug=1 THEN
16879 		       debug_print('No serials found for reservation record. id: ' || l_orig_rsv_tbl(1).reservation_id);
16880 		    END IF;
16881 		    fnd_message.set_name('INV', 'INV_INVALID_ROW');
16882 		    fnd_msg_pub.ADD;
16883 		    RAISE fnd_api.g_exc_error;
16884 	      END;
16885 
16886 	   END IF;-- transfer all and to row exist
16887 
16888     END IF;-- param = 4
16889 
16890     /*** End R12 ***/
16891 
16892     /**** {{ R12 Enhanced reservations code changes. Should be
16893     -- releasing the locks. }} *****/
16894     IF l_lock_obtained THEN
16895        inv_reservation_lock_pvt.release_lock
16896 	 (l_supply_lock_handle);
16897        inv_reservation_lock_pvt.release_lock
16898 	 (l_demand_lock_handle);
16899     END IF;
16900     /*** End R12 ***/
16901 
16902     x_return_status  := l_return_status;
16903   EXCEPTION
16904      WHEN fnd_api.g_exc_error THEN
16905 	ROLLBACK TO transfer_reservation_sa;
16906 	x_return_status  := fnd_api.g_ret_sts_error;
16907 	/**** {{ R12 Enhanced reservations code changes. Should be
16908 	-- releasing the locks. }} *****/
16909 	IF l_lock_obtained THEN
16910 	   inv_reservation_lock_pvt.release_lock
16911 	     (l_supply_lock_handle);
16912 	   inv_reservation_lock_pvt.release_lock
16913 	     (l_demand_lock_handle);
16914 	END IF;
16915 	/*** End R12 ***/
16916 	--  Get message count and data
16917 	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
16918      WHEN fnd_api.g_exc_unexpected_error THEN
16919 	ROLLBACK TO transfer_reservation_sa;
16920 	x_return_status  := fnd_api.g_ret_sts_unexp_error;
16921 	/**** {{ R12 Enhanced reservations code changes. Should be
16922 	-- releasing the locks. }} *****/
16923 	IF l_lock_obtained THEN
16924 	   inv_reservation_lock_pvt.release_lock
16925 	     (l_supply_lock_handle);
16926 	   inv_reservation_lock_pvt.release_lock
16927 	     (l_demand_lock_handle);
16928 	END IF;
16929 	/*** End R12 ***/
16930 	--  Get message count and data
16931 	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
16932      WHEN OTHERS THEN
16933 	ROLLBACK TO transfer_reservation_sa;
16934 	x_return_status  := fnd_api.g_ret_sts_unexp_error;
16935 	/**** {{ R12 Enhanced reservations code changes. Should be
16936 	-- releasing the locks. }} *****/
16937 	IF l_lock_obtained THEN
16938 	   inv_reservation_lock_pvt.release_lock
16939 	     (l_supply_lock_handle);
16940 	   inv_reservation_lock_pvt.release_lock
16941 	     (l_demand_lock_handle);
16942 	END IF;
16943 	/*** End R12 ***/
16944 	IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
16945 	   fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
16946 	END IF;
16947 
16948 	--  Get message count and data
16949 	fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
16950   END transfer_reservation;
16951 
16952   --
16953   -- Procedure
16954   /*
16955   ** ----------------------------------------------------------------------
16956   ** For Order Management(OM) use only. Please read below:
16957   ** MUST PASS DEMAND SOURCE HEADER ID AND DEMAND SOURCE LINE ID
16958   ** ----------------------------------------------------------------------
16959   ** This API has been written exclusively for Order Management, who query
16960   ** reservations extensively.
16961   ** The generic query reservation API, query_reservation(see signature above)
16962   ** builds a dynamic SQL to satisfy all callers as it does not know what the
16963   ** search criteria is, at design time.
16964   ** The dynamic SQL consumes soft parse time, which reduces performance.
16965   ** An excessive use of query_reservation contributes to performance
16966   ** degradation because of soft parse times.
16967   ** Since we know what OM would always use to query reservations
16968   ** - demand source header id and demand source line id, a new API
16969   ** with static SQL would be be effective, with reduced performance impact.
16970   ** ----------------------------------------------------------------------
16971   ** Since OM has been using query_reservation before this, the signature of the
16972   ** new API below remains the same to cause minimal impact.
16973   ** ----------------------------------------------------------------------
16974   **Bug 2872822 Added new constants for p_query_by_req_date and handled two
16975   ** new cursors.Did not use Dynamic SQL for parsing performance issue.
16976   */
16977   PROCEDURE query_reservation_om_hdr_line(
16978     p_api_version_number        IN     NUMBER
16979   , p_init_msg_lst              IN     VARCHAR2 DEFAULT fnd_api.g_false
16980   , x_return_status             OUT    NOCOPY VARCHAR2
16981   , x_msg_count                 OUT    NOCOPY NUMBER
16982   , x_msg_data                  OUT    NOCOPY VARCHAR2
16983   , p_query_input               IN     inv_reservation_global.mtl_reservation_rec_type
16984   , p_lock_records              IN     VARCHAR2 DEFAULT fnd_api.g_false
16985   , p_sort_by_req_date          IN     NUMBER DEFAULT inv_reservation_global.g_query_no_sort
16986   , p_cancel_order_mode         IN     NUMBER DEFAULT inv_reservation_global.g_cancel_order_no
16987   , x_mtl_reservation_tbl       OUT    NOCOPY inv_reservation_global.mtl_reservation_tbl_type
16988   , x_mtl_reservation_tbl_count OUT    NOCOPY NUMBER
16989   , x_error_code                OUT    NOCOPY NUMBER
16990   ) IS
16991     l_api_version_number CONSTANT NUMBER                                          := 1.0;
16992     l_api_name           CONSTANT VARCHAR2(30)                                    := 'Query_Reservation_OM_Hdr_Line';
16993     l_return_status               VARCHAR2(1)                                     := fnd_api.g_ret_sts_success;
16994     l_counter                     INTEGER;
16995     l_rsv_rec                     inv_reservation_global.mtl_reservation_rec_type;
16996 
16997     --   Cursor to fetch MTL_RESERVATION record based on passed
16998     --   demand_source_header_id and demand_source_line_id
16999 
17000     -- INVCONV - Incorporate secondary columns
17001     CURSOR mrc(dmd_source_header_id NUMBER, dmd_source_line_id NUMBER) IS
17002        SELECT
17003 	 mr.reservation_id
17004 	 , mr.requirement_date
17005 	 , mr.organization_id
17006 	 , mr.inventory_item_id
17007 	 , mr.demand_source_type_id
17008 	 , mr.demand_source_name
17009 	 , mr.demand_source_header_id
17010 	 , mr.demand_source_line_id
17011 	 , mr.demand_source_delivery
17012 	 , mr.primary_uom_code
17013 	 , mr.primary_uom_id
17014 	 , mr.secondary_uom_code
17015 	 , mr.secondary_uom_id
17016 	 , mr.reservation_uom_code
17017 	 , mr.reservation_uom_id
17018 	 , mr.reservation_quantity
17019 	 , mr.primary_reservation_quantity
17020 	 , mr.secondary_reservation_quantity
17021 	 , mr.detailed_quantity
17022 	 , mr.secondary_detailed_quantity
17023 	 , mr.autodetail_group_id
17024 	 , mr.external_source_code
17025 	 , mr.external_source_line_id
17026 	 , mr.supply_source_type_id
17027 	 , mr.supply_source_header_id
17028 	 , mr.supply_source_line_id
17029 	 , mr.supply_source_name
17030 	 , mr.supply_source_line_detail
17031 	 , mr.revision
17032 	 , mr.subinventory_code
17033 	 , mr.subinventory_id
17034 	 , mr.locator_id
17035 	 , mr.lot_number
17036 	 , mr.lot_number_id
17037 	 , mr.pick_slip_number
17038 	 , mr.lpn_id
17039 	 , mr.attribute_category
17040 	 , mr.attribute1
17041 	 , mr.attribute2
17042 	 , mr.attribute3
17043 	 , mr.attribute4
17044 	 , mr.attribute5
17045 	 , mr.attribute6
17046 	 , mr.attribute7
17047 	 , mr.attribute8
17048 	 , mr.attribute9
17049 	 , mr.attribute10
17050 	 , mr.attribute11
17051 	 , mr.attribute12
17052 	 , mr.attribute13
17053 	 , mr.attribute14
17054 	 , mr.attribute15
17055 	 , mr.ship_ready_flag
17056 	 , mr.staged_flag
17057 	 /**** {{ R12 Enhanced reservations code changes }}****/
17058 	 , crossdock_flag
17059 	 , crossdock_criteria_id
17060 	 , demand_source_line_detail
17061 	 , serial_reservation_quantity
17062 	 , supply_receipt_date
17063 	 , demand_ship_date
17064 	 , project_id
17065 	 , task_id
17066 	 , orig_supply_source_type_id
17067 	 , orig_supply_source_header_id
17068 	 , orig_supply_source_line_id
17069 	 , orig_supply_source_line_detail
17070 	 , orig_demand_source_type_id
17071 	 , orig_demand_source_header_id
17072 	 , orig_demand_source_line_id
17073 	 , orig_demand_source_line_detail
17074 	 , serial_number
17075 	 /***** End R12 ***/
17076 
17077           FROM mtl_reservations mr
17078          WHERE demand_source_type_id IN (inv_reservation_global.g_source_type_oe
17079                                        , inv_reservation_global.g_source_type_internal_ord
17080                                        , inv_reservation_global.g_source_type_rma
17081                                         )
17082            AND demand_source_header_id = dmd_source_header_id
17083            AND demand_source_line_id = dmd_source_line_id
17084       ORDER BY NVL(mr.revision, ' ')
17085              , NVL(mr.lot_number, ' ')
17086              , NVL(mr.subinventory_code, ' ')
17087              , NVL(mr.locator_id, 0);
17088 
17089     --   Bug#2872822 Cursor to fetch MTL_RESERVATION record based on passed
17090     --   demand_source_header_id and demand_source_line_id
17091     --   the records are obtained in the ascending order of requirement_date
17092     -- INVCONV - Incorporate secondary columns
17093     CURSOR mrc_asc(dmd_source_header_id NUMBER, dmd_source_line_id NUMBER) IS
17094        SELECT
17095 	 mr.reservation_id
17096 	 , mr.requirement_date
17097 	 , mr.organization_id
17098 	 , mr.inventory_item_id
17099 	 , mr.demand_source_type_id
17100 	 , mr.demand_source_name
17101 	 , mr.demand_source_header_id
17102 	 , mr.demand_source_line_id
17103 	 , mr.demand_source_delivery
17104 	 , mr.primary_uom_code
17105 	 , mr.primary_uom_id
17106 	 , mr.reservation_uom_code
17107 	 , mr.reservation_uom_id
17108 	 , mr.secondary_uom_code
17109 	 , mr.secondary_uom_id
17110 	 , mr.reservation_quantity
17111 	 , mr.primary_reservation_quantity
17112 	 , mr.secondary_reservation_quantity
17113 	 , mr.detailed_quantity
17114 	 , mr.secondary_detailed_quantity
17115 	 , mr.autodetail_group_id
17116 	 , mr.external_source_code
17117 	 , mr.external_source_line_id
17118 	 , mr.supply_source_type_id
17119 	 , mr.supply_source_header_id
17120 	 , mr.supply_source_line_id
17121 	 , mr.supply_source_name
17122 	 , mr.supply_source_line_detail
17123 	 , mr.revision
17124 	 , mr.subinventory_code
17125 	 , mr.subinventory_id
17126 	 , mr.locator_id
17127 	 , mr.lot_number
17128 	 , mr.lot_number_id
17129 	 , mr.pick_slip_number
17130 	 , mr.lpn_id
17131 	 , mr.attribute_category
17132 	 , mr.attribute1
17133 	 , mr.attribute2
17134 	 , mr.attribute3
17135 	 , mr.attribute4
17136 	 , mr.attribute5
17137 	 , mr.attribute6
17138 	 , mr.attribute7
17139 	 , mr.attribute8
17140 	 , mr.attribute9
17141 	 , mr.attribute10
17142 	 , mr.attribute11
17143 	 , mr.attribute12
17144 	 , mr.attribute13
17145 	 , mr.attribute14
17146 	 , mr.attribute15
17147 	 , mr.ship_ready_flag
17148 	 , mr.staged_flag
17149 /**** {{ R12 Enhanced reservations code changes }}****/
17150 	 , crossdock_flag
17151 	 , crossdock_criteria_id
17152 	 , demand_source_line_detail
17153 	 , serial_reservation_quantity
17154 	 , supply_receipt_date
17155 	 , demand_ship_date
17156 	 , project_id
17157 	 , task_id
17158 	 , orig_supply_source_type_id
17159 	 , orig_supply_source_header_id
17160 	 , orig_supply_source_line_id
17161 	 , orig_supply_source_line_detail
17162 	 , orig_demand_source_type_id
17163 	 , orig_demand_source_header_id
17164 	 , orig_demand_source_line_id
17165 	 , orig_demand_source_line_detail
17166 	 , serial_number
17167 	 /***** End R12 ***/
17168 
17169           FROM mtl_reservations mr
17170          WHERE demand_source_type_id IN (inv_reservation_global.g_source_type_oe
17171                                        , inv_reservation_global.g_source_type_internal_ord
17172                                        , inv_reservation_global.g_source_type_rma
17173                                         )
17174            AND demand_source_header_id = dmd_source_header_id
17175            AND demand_source_line_id = dmd_source_line_id
17176       ORDER BY mr.requirement_date asc
17177              , NVL(mr.revision, ' ')
17178              , NVL(mr.lot_number, ' ')
17179              , NVL(mr.subinventory_code, ' ')
17180              , NVL(mr.locator_id, 0);
17181 
17182     --   Bug#2872822 Cursor to fetch MTL_RESERVATION record based on passed
17183     --   demand_source_header_id and demand_source_line_id
17184     --   the records are obtained in the descending order of requirement_date
17185     -- INVCONV - Incorporate secondary columns
17186     CURSOR mrc_desc(dmd_source_header_id NUMBER, dmd_source_line_id NUMBER) IS
17187        SELECT
17188 	 mr.reservation_id
17189 	 , mr.requirement_date
17190 	 , mr.organization_id
17191 	 , mr.inventory_item_id
17192 	 , mr.demand_source_type_id
17193 	 , mr.demand_source_name
17194 	 , mr.demand_source_header_id
17195 	 , mr.demand_source_line_id
17196 	 , mr.demand_source_delivery
17197 	 , mr.primary_uom_code
17198 	 , mr.primary_uom_id
17199 	 , mr.secondary_uom_code
17200 	 , mr.secondary_uom_id
17201 	 , mr.reservation_uom_code
17202 	 , mr.reservation_uom_id
17203 	 , mr.reservation_quantity
17204 	 , mr.primary_reservation_quantity
17205 	 , mr.secondary_reservation_quantity
17206 	 , mr.detailed_quantity
17207 	 , mr.secondary_detailed_quantity
17208 	 , mr.autodetail_group_id
17209 	 , mr.external_source_code
17210 	 , mr.external_source_line_id
17211 	 , mr.supply_source_type_id
17212 	 , mr.supply_source_header_id
17213 	 , mr.supply_source_line_id
17214 	 , mr.supply_source_name
17215 	 , mr.supply_source_line_detail
17216 	 , mr.revision
17217 	 , mr.subinventory_code
17218 	 , mr.subinventory_id
17219 	 , mr.locator_id
17220 	 , mr.lot_number
17221 	 , mr.lot_number_id
17222 	 , mr.pick_slip_number
17223 	 , mr.lpn_id
17224 	 , mr.attribute_category
17225 	 , mr.attribute1
17226 	 , mr.attribute2
17227 	 , mr.attribute3
17228 	 , mr.attribute4
17229 	 , mr.attribute5
17230 	 , mr.attribute6
17231 	 , mr.attribute7
17232 	 , mr.attribute8
17233 	 , mr.attribute9
17234 	 , mr.attribute10
17235 	 , mr.attribute11
17236 	 , mr.attribute12
17237 	 , mr.attribute13
17238 	 , mr.attribute14
17239 	 , mr.attribute15
17240 	 , mr.ship_ready_flag
17241 	 , mr.staged_flag
17242 	 /**** {{ R12 Enhanced reservations code changes }}****/
17243 	 , crossdock_flag
17244 	 , crossdock_criteria_id
17245 	 , demand_source_line_detail
17246 	 , serial_reservation_quantity
17247 	 , supply_receipt_date
17248 	 , demand_ship_date
17249 	 , project_id
17250 	 , task_id
17251 	 , orig_supply_source_type_id
17252 	 , orig_supply_source_header_id
17253 	 , orig_supply_source_line_id
17254 	 , orig_supply_source_line_detail
17255 	 , orig_demand_source_type_id
17256 	 , orig_demand_source_header_id
17257 	 , orig_demand_source_line_id
17258 	 , orig_demand_source_line_detail
17259 	 , serial_number
17260 	 /***** End R12 ***/
17261 
17262           FROM mtl_reservations mr
17263          WHERE demand_source_type_id IN (inv_reservation_global.g_source_type_oe
17264                                        , inv_reservation_global.g_source_type_internal_ord
17265                                        , inv_reservation_global.g_source_type_rma
17266                                         )
17267            AND demand_source_header_id = dmd_source_header_id
17268            AND demand_source_line_id = dmd_source_line_id
17269       ORDER BY mr.requirement_date DESC
17270              , NVL(mr.revision, ' ')
17271              , NVL(mr.lot_number, ' ')
17272              , NVL(mr.subinventory_code, ' ')
17273              , NVL(mr.locator_id, 0);
17274   BEGIN
17275     x_error_code                 := inv_reservation_global.g_err_unexpected;
17276 
17277     --
17278     --  Standard call to check for call compatibility
17279     IF NOT fnd_api.compatible_api_call(l_api_version_number, p_api_version_number, l_api_name, g_pkg_name) THEN
17280       RAISE fnd_api.g_exc_unexpected_error;
17281     END IF;
17282 
17283     --
17284     --  Initialize message list.
17285     IF fnd_api.to_boolean(p_init_msg_lst) THEN
17286       fnd_msg_pub.initialize;
17287     END IF;
17288 
17289     --
17290     /*
17291     ** Make sure the required parameters are passed
17292     */
17293     IF (p_query_input.demand_source_header_id = fnd_api.g_miss_num
17294         OR p_query_input.demand_source_header_id IS NULL
17295        ) THEN
17296       fnd_message.set_name('INV', 'INV-RSV-INPUT-MISSING');
17297       fnd_message.set_token('FIELD_NAME', 'demand_source_header_id');
17298       fnd_msg_pub.ADD;
17299       RAISE fnd_api.g_exc_error;
17300     END IF;
17301 
17302     IF (p_query_input.demand_source_line_id = fnd_api.g_miss_num
17303         OR p_query_input.demand_source_line_id IS NULL
17304        ) THEN
17305       fnd_message.set_name('INV', 'INV-RSV-INPUT-MISSING');
17306       fnd_message.set_token('FIELD_NAME', 'demand_source_line_id');
17307       fnd_msg_pub.ADD;
17308       RAISE fnd_api.g_exc_error;
17309     END IF;
17310 
17311     --Bug#2872822/2914726 OM call to this API passes null. This was changed
17312     -- handle null also.
17313     IF (p_sort_by_req_date NOT IN ( inv_reservation_global.g_query_req_date_inv_asc,
17314 				   inv_reservation_global.g_query_req_date_inv_desc)) OR p_sort_by_req_date IS NULL
17315       then
17316        OPEN mrc(p_query_input.demand_source_header_id, p_query_input.demand_source_line_id);
17317        l_counter                    := 0;
17318 
17319        LOOP
17320          FETCH mrc INTO l_rsv_rec;
17321          EXIT WHEN mrc%NOTFOUND;
17322          l_counter                         := l_counter + 1;
17323          x_mtl_reservation_tbl(l_counter)  := l_rsv_rec;
17324        END LOOP;
17325 
17326        CLOSE mrc;
17327     ELSIF p_sort_by_req_date =inv_reservation_global.g_query_req_date_inv_asc then
17328            OPEN mrc_asc(p_query_input.demand_source_header_id,
17329                                     p_query_input.demand_source_line_id);
17330          l_counter                    := 0;
17331         LOOP
17332         FETCH mrc_asc INTO l_rsv_rec;
17333         EXIT WHEN mrc_asc%NOTFOUND;
17334         l_counter                         := l_counter + 1;
17335         x_mtl_reservation_tbl(l_counter)  := l_rsv_rec;
17336         END LOOP;
17337         CLOSE mrc_asc;
17338     ELSIF p_sort_by_req_date =inv_reservation_global.g_query_req_date_inv_desc then
17339          OPEN mrc_desc(p_query_input.demand_source_header_id,
17340                                     p_query_input.demand_source_line_id);
17341             l_counter                    := 0;
17342           LOOP
17343           FETCH mrc_desc INTO l_rsv_rec;
17344           EXIT WHEN mrc_desc%NOTFOUND;
17345           l_counter                         := l_counter + 1;
17346           x_mtl_reservation_tbl(l_counter)  := l_rsv_rec;
17347           END LOOP;
17348 
17349          CLOSE  mrc_desc;
17350 
17351     END IF; --Sort by req date
17352     --
17353     x_mtl_reservation_tbl_count  := l_counter;
17354     x_return_status              := l_return_status;
17355     x_error_code                 := inv_reservation_global.g_err_no_error;
17356   --
17357   EXCEPTION
17358     WHEN fnd_api.g_exc_error THEN
17359       x_return_status  := fnd_api.g_ret_sts_error;
17360       --  Get message count and data
17361       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
17362     WHEN fnd_api.g_exc_unexpected_error THEN
17363       x_return_status  := fnd_api.g_ret_sts_unexp_error;
17364       --  Get message count and data
17365       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
17366     WHEN OTHERS THEN
17367       x_return_status  := fnd_api.g_ret_sts_unexp_error;
17368 
17369       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
17370         fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
17371       END IF;
17372 
17373       --  Get message count and data
17374       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
17375   END query_reservation_om_hdr_line;
17376 
17377   -- Change the signiture of upd_reservation_pup for bug 2879208
17378   -- Create overload porceudre
17379 
17380   PROCEDURE Upd_Reservation_Pup_New(
17381     x_return_status           OUT    NOCOPY VARCHAR2
17382   , x_msg_count               OUT    NOCOPY NUMBER
17383   , x_msg_data                OUT    NOCOPY VARCHAR2
17384   , p_commit                  IN     VARCHAR2 := fnd_api.g_false
17385   , p_init_msg_list           IN     VARCHAR2 := fnd_api.g_false
17386   , p_organization_id         IN     NUMBER
17387   , p_demand_source_header_id IN     NUMBER
17388   , p_demand_source_line_id   IN     NUMBER
17389   , p_from_subinventory_code  IN     VARCHAR2
17390   , p_from_locator_id         IN     NUMBER
17391   , p_to_subinventory_code    IN     VARCHAR2
17392   , p_to_locator_id           IN     NUMBER
17393   , p_inventory_item_id       IN     NUMBER
17394   , p_revision                IN     VARCHAR2
17395   , p_lot_number              IN     VARCHAR2
17396   , p_quantity                IN     NUMBER
17397   , p_uom                     IN     VARCHAR2
17398   , p_lpn_id                  IN     NUMBER   := NULL
17399   , p_validation_flag         IN     VARCHAR2 := fnd_api.g_false
17400   , p_force_reservation_flag  IN     VARCHAR2 := fnd_api.g_false
17401   , p_requirement_date        IN     DATE DEFAULT NULL  -- bug 2879208
17402   , p_source_lpn_id           IN     NUMBER   := NULL -- Bug 4016953/3871066
17403   , p_demand_source_name      IN     VARCHAR2 DEFAULT NULL -- RTV Project
17404   ) IS
17405     l_api_name    CONSTANT VARCHAR2(30)  := 'Upd_Reservation_PUP_New';
17406     l_api_version CONSTANT NUMBER        := 1.0;
17407     l_debug                NUMBER;
17408     l_progress             VARCHAR2(500) := '0';
17409 
17410     -- Variables used by API logic
17411     l_remaining_qty           NUMBER;
17412     l_remaining_qty_uom        VARCHAR2(3);
17413     l_call_xfr_rsv            BOOLEAN := FALSE;
17414     l_reservation_id          NUMBER;
17415     l_demand_source_header_id NUMBER;
17416 
17417     -- MTL_RESERVATIONS record type declarations
17418     l_src_rsv       inv_reservation_global.mtl_reservation_rec_type;
17419     l_xfr_rsv       inv_reservation_global.mtl_reservation_rec_type;
17420     l_serial_number inv_reservation_global.serial_number_tbl_type;
17421 
17422     l_primary_quantity NUMBER;  --The two for 14011079
17423     l_primary_uom_code VARCHAR(3);
17424     -- Cursor to fetch the from reservations
17425     -- INVCONV - Incorporate secondaries into select
17426     CURSOR  rsv_cur(v_quantity NUMBER) IS  --add one parameter for bug 14011079
17427        SELECT
17428 	 reservation_id
17429 	 , requirement_date
17430 	 , organization_id
17431 	 , inventory_item_id
17432 	 , demand_source_type_id
17433 	 , demand_source_name
17434 	 , demand_source_header_id
17435 	 , demand_source_line_id
17436 	 , demand_source_delivery
17437 	 , primary_uom_code
17438 	 , primary_uom_id
17439 	 , secondary_uom_code
17440 	 , secondary_uom_id
17441 	 , reservation_uom_code
17442 	 , reservation_uom_id
17443 	 , reservation_quantity
17444 	 , primary_reservation_quantity
17445 	 , secondary_reservation_quantity
17446 	 , detailed_quantity
17447 	 , secondary_detailed_quantity
17448 	 , autodetail_group_id
17449 	 , external_source_code
17450 	 , external_source_line_id
17451 	 , supply_source_type_id
17452 	 , supply_source_header_id
17453 	 , supply_source_line_id
17454 	 , supply_source_name
17455 	 , supply_source_line_detail
17456 	 , revision
17457 	 , subinventory_code
17458 	 , subinventory_id
17459 	 , locator_id
17460 	 , lot_number
17461 	 , lot_number_id
17462 	 , pick_slip_number
17463 	 , lpn_id
17464 	 , attribute_category
17465 	 , attribute1
17466 	 , attribute2
17467 	 , attribute3
17468 	 , attribute4
17469 	 , attribute5
17470 	 , attribute6
17471 	 , attribute7
17472 	 , attribute8
17473 	 , attribute9
17474 	 , attribute10
17475 	 , attribute11
17476 	 , attribute12
17477 	 , attribute13
17478 	 , attribute14
17479 	 , attribute15
17480 	 , ship_ready_flag
17481 	 , staged_flag
17482 	 /**** {{ R12 Enhanced reservations code changes }}****/
17483 	 , crossdock_flag
17484 	 , crossdock_criteria_id
17485 	 , demand_source_line_detail
17486 	 , serial_reservation_quantity
17487 	 , supply_receipt_date
17488 	 , demand_ship_date
17489 	 , project_id
17490 	 , task_id
17491 	 , orig_supply_source_type_id
17492 	 , orig_supply_source_header_id
17493 	 , orig_supply_source_line_id
17494 	 , orig_supply_source_line_detail
17495 	 , orig_demand_source_type_id
17496 	 , orig_demand_source_header_id
17497 	 , orig_demand_source_line_id
17498 	 , orig_demand_source_line_detail
17499 	 , serial_number
17500 	 /***** End R12 ***/
17501 
17502         FROM mtl_reservations
17503        WHERE organization_id = p_organization_id
17504          AND inventory_item_id = p_inventory_item_id
17505          AND (p_revision IS NULL OR revision = p_revision)
17506          AND (demand_source_header_id = l_demand_source_header_id OR l_demand_source_header_id IS NULL)
17507          AND (demand_source_line_id = p_demand_source_line_id   OR p_demand_source_line_id is NULL)
17508 		 AND (demand_source_name =  p_demand_source_name OR p_demand_source_name IS NULL)  --RTV Changes
17509          AND subinventory_code = p_from_subinventory_code
17510          AND locator_id = p_from_locator_id
17511          AND (p_lot_number IS NULL OR lot_number = p_lot_number)
17512          -- NVL added to prevent dependency on TM to not break existing code
17513          -- by resulting in a no records found if p_source_lpn_id is null.
17514          -- All new calls to this api starting with this bug
17515          -- fix (4016953/3871066) pass the p_source_lpn_id
17516          AND (lpn_id = NVL(p_source_lpn_id, lpn_id) OR lpn_id IS NULL)
17517      ORDER BY lpn_id asc, abs(primary_reservation_quantity-v_quantity); -- for bug 14011079 add the last order
17518 
17519   BEGIN
17520     -- Initialize return status to success
17521     x_return_status := fnd_api.g_ret_sts_success;
17522 
17523     --  Initialize message list.
17524     IF fnd_api.to_boolean(p_init_msg_list) THEN
17525       fnd_msg_pub.initialize;
17526     END IF;
17527 
17528     SAVEPOINT upd_reservation_pup_new;
17529     l_remaining_qty           := p_quantity;
17530     l_remaining_qty_uom       := p_uom;
17531     l_demand_source_header_id := INV_SALESORDER.Get_Salesorder_For_OEHeader(p_demand_source_header_id);
17532 
17533     -- Use cache to get value for l_debug
17534     IF g_is_pickrelease_set IS NULL THEN
17535        g_is_pickrelease_set := 2;
17536        IF INV_CACHE.is_pickrelease THEN
17537           g_is_pickrelease_set := 1;
17538        END IF;
17539     END IF;
17540     IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
17541        g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
17542     END IF;
17543 
17544     l_debug := g_debug;
17545 
17546     IF (l_debug = 1) THEN
17547        debug_print(l_api_name || ' Entered ' || g_pkg_version);
17548        debug_print('orgid='||p_organization_id||' shdid='||p_demand_source_header_id||' slnid='||
17549          p_demand_source_line_id||' fsub='||p_from_subinventory_code||' floc='||p_from_locator_id);
17550        debug_print('tsub='||p_to_subinventory_code||' tloc='||p_to_locator_id||' itm='||p_inventory_item_id||
17551 	  ' rev=' ||p_revision||' lot='||p_lot_number||' qty='||p_quantity||' uom='||p_uom);
17552        debug_print('lpn='||p_lpn_id||' val='||p_validation_flag||' frc='||p_force_reservation_flag||' rdate='||
17553           to_char(p_requirement_date, 'MON-DD-YYYY HH:MI:SS')||' dsrc='||l_demand_source_header_id||' slpn='||p_source_lpn_id);
17554     END IF;
17555     -- add for bug 14011079 convert quantiy to primary_quantity if it is not.
17556     l_primary_quantity :=p_quantity;
17557     l_primary_uom_code :=NULL;
17558 
17559     IF p_organization_id IS NOT NULL AND p_inventory_item_id IS NOT NULL THEN
17560       BEGIN
17561         IF inv_cache.set_item_rec(p_organization_id, p_inventory_item_id) THEN
17562            l_primary_uom_code := inv_cache.item_rec.primary_uom_code;
17563            IF (l_debug = 1) THEN
17564               debug_print('l_primary_uom_code='||l_primary_uom_code);
17565            END IF;
17566         END IF;
17567         IF l_primary_uom_code IS NOT NULL AND l_primary_uom_code<>p_uom THEN
17568            l_primary_quantity := inv_convert.inv_um_convert(item_id   => p_inventory_item_id
17569             , lot_number		=> p_lot_number
17570             , organization_id      => p_organization_id
17571             , PRECISION            => NULL -- use default precision
17572             , from_quantity        => p_quantity
17573             , from_unit            => p_uom
17574             , to_unit              => l_primary_uom_code
17575             , from_name            => NULL -- from uom name
17576             , to_name              => NULL); -- to uom name
17577         END IF;
17578       EXCEPTION
17579       WHEN OTHERS THEN
17580         debug_print('Conversion to Primay UOM Failed for p_quantity and p_uom');
17581         l_primary_quantity :=p_quantity;
17582       END;
17583     END IF;
17584 
17585     IF (l_debug = 1) THEN
17586       debug_print('After Conversion l_primary_quantity='||l_primary_quantity);
17587       debug_print('After Conversion l_primary_uom_code='||l_primary_uom_code);
17588     END IF;
17589     FOR l_src_rsv IN rsv_cur(l_primary_quantity) LOOP
17590     -- end for 14011079
17591       IF (l_debug = 1) THEN
17592         debug_print('Got rsvid= '||l_src_rsv.reservation_id||' qty=
17593                     '||l_src_rsv.primary_reservation_quantity||' uom=
17594                     '||l_src_rsv.primary_uom_code||' sub='||l_src_rsv.subinventory_code||' loc= '||
17595                     l_src_rsv.locator_id||' lpn= '||l_src_rsv.lpn_id);
17596         debug_print('l_remaining_qty: ' || l_remaining_qty || '
17597                    l_remaining_qty_uom ' || l_remaining_qty_uom || '
17598                    l_src_rsv.primary_uom_code:' || l_src_rsv.primary_uom_code);
17599       END IF;
17600 
17601        -- Copy all data from old reservation to the new reservation
17602       l_xfr_rsv := l_src_rsv;
17603 
17604       l_progress := 'Convert l_remaining_qty to rsv uom '||l_remaining_qty;
17605       IF ( l_remaining_qty_uom <> l_src_rsv.primary_uom_code ) THEN
17606 
17607         -- INVCONV - upgrade call to inv_um_convert to pass lot and org
17608         l_remaining_qty := inv_convert.inv_um_convert(
17609            item_id		=> l_src_rsv.inventory_item_id
17610          , lot_number		=> l_src_rsv.lot_number
17611          , organization_id      => l_src_rsv.organization_id
17612          , PRECISION            => NULL -- use default precision
17613          , from_quantity        => l_remaining_qty
17614          , from_unit            => l_remaining_qty_uom
17615          , to_unit              => l_src_rsv.primary_uom_code
17616          , from_name            => NULL -- from uom name
17617          , to_name              => NULL); -- to uom name
17618 
17619          IF l_remaining_qty = -99999 THEN
17620            -- conversion failed
17621            IF (l_debug = 1) THEN
17622              debug_print('Conversion to RSV UOM Failed');
17623            END IF;
17624            fnd_message.set_name('INV', 'CAN-NOT-CONVERT-TO-RSV-UOM');
17625            fnd_msg_pub.ADD;
17626            RAISE fnd_api.g_exc_error;
17627          END IF;
17628 
17629           -- Remember UOM of current remaining quantity
17630           l_remaining_qty_uom := l_src_rsv.primary_uom_code;
17631         END IF;
17632 
17633         -- Even if the reservation does not need to be transfered we count the qty
17634         l_xfr_rsv.primary_reservation_quantity :=
17635           LEAST(l_src_rsv.Primary_reservation_quantity, l_remaining_qty);
17636         --l_xfr_rsv.primary_uom_code := l_src_rsv.primary_uom_code;
17637         l_remaining_qty := l_remaining_qty - l_xfr_rsv.primary_reservation_quantity;
17638         -- INVCONV B4242576 BEGIN
17639         -- compute secondaries as necessary for items tracking in primary and secondary
17640         IF l_src_rsv.secondary_uom_code is not null THEN
17641           IF (l_debug = 1) THEN
17642             debug_print('Dual tracked item so populate secondary quantities for '||l_src_rsv.secondary_uom_code);
17643           END IF;
17644           IF l_xfr_rsv.primary_reservation_quantity < l_src_rsv.primary_reservation_quantity THEN
17645             -- Transferring less than the full qty so determine the equivalent secondary
17646             l_xfr_rsv.secondary_reservation_quantity := inv_convert.inv_um_convert(
17647               item_id              => l_xfr_rsv.inventory_item_id
17648             , lot_number	   => l_xfr_rsv.lot_number
17649             , organization_id      => l_xfr_rsv.organization_id
17650             , PRECISION            => NULL -- use default precision
17651             , from_quantity        => l_xfr_rsv.primary_reservation_quantity
17652             , from_unit            => l_xfr_rsv.primary_uom_code
17653             , to_unit              => l_src_rsv.secondary_uom_code
17654             , from_name            => NULL -- from uom name
17655             , to_name              => NULL); -- to uom name
17656 
17657             IF l_remaining_qty = -99999 THEN
17658               -- conversion failed
17659               IF (l_debug = 1) THEN
17660                 debug_print('Conversion to SECONDARY UOM Failed');
17661               END IF;
17662               fnd_message.set_name('INV', 'CAN-NOT-CONVERT-TO-SECOND-UOM');
17663               fnd_msg_pub.ADD;
17664               RAISE fnd_api.g_exc_error;
17665             END IF;
17666             IF (l_debug = 1) THEN
17667               debug_print('After conversion secondary_qty= '||l_src_rsv.secondary_reservation_quantity); -- KYH
17668             END IF;
17669           END IF;
17670         END IF;
17671         -- INVCONV B4242576 END
17672 
17673       IF (l_debug = 1) THEN
17674          debug_print('l_remaining_qty before xfer: ' || l_remaining_qty || '
17675                      l_xfr_rsv.primary_reservation_quantity ' ||
17676                      l_xfr_rsv.primary_reservation_quantity);
17677       END IF;
17678 
17679       -- Only if the original reservation is at the LPN level should we
17680       -- transfer the reservatoin to the new LPN not applicable if p_lpn_id
17681       -- is null, i.e. whole staged LPN being subxfered
17682       -- Bug 3846145: The transfer should happen only if the source and the
17683       --destination lpns are different.
17684       IF ( l_src_rsv.lpn_id IS NOT NULL AND p_lpn_id IS NOT NULL AND
17685            (l_src_rsv.lpn_id <> p_lpn_id) ) THEN
17686          l_xfr_rsv.lpn_id := p_lpn_id;
17687          l_call_xfr_rsv := TRUE;
17688          IF (l_debug = 1) THEN
17689             debug_print('Inside source lpn and p_lpn are not null');
17690          END IF;
17691       END IF;
17692 
17693       -- If the reservation needs to transfer locations...
17694       IF ( l_src_rsv.subinventory_code <> p_to_subinventory_code OR
17695            l_src_rsv.locator_id        <> p_to_locator_id )
17696       THEN
17697         l_xfr_rsv.subinventory_code := p_to_subinventory_code;
17698         l_xfr_rsv.locator_id        := p_to_locator_id;
17699         l_call_xfr_rsv := TRUE;
17700         IF (l_debug = 1) THEN
17701             debug_print('Inside loc, sub or both are different');
17702          END IF;
17703       END IF;
17704 
17705       IF ( l_call_xfr_rsv ) THEN
17706         l_xfr_rsv.reservation_quantity := NULL;
17707         l_xfr_rsv.reservation_id       := NULL;
17708 
17709         l_progress := 'Call to Transfer_Reservation';
17710         INV_RESERVATION_PVT.Transfer_Reservation (
17711           p_api_version_number     => 1.0
17712         , p_init_msg_lst           => fnd_api.g_false
17713         , x_return_status          => x_return_status
17714         , x_msg_count              => x_msg_count
17715         , x_msg_data               => x_msg_data
17716         , p_original_rsv_rec       => l_src_rsv
17717         , p_to_rsv_rec             => l_xfr_rsv
17718         , p_original_serial_number => l_serial_number
17719         , p_validation_flag        => p_validation_flag
17720         , x_reservation_id         => l_reservation_id );
17721 
17722         IF ( x_return_status = fnd_api.g_ret_sts_error ) THEN
17723           RAISE FND_API.G_EXC_ERROR;
17724         ELSIF ( x_return_status <> fnd_api.g_ret_sts_success ) THEN
17725           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
17726         END IF;
17727 
17728         l_call_xfr_rsv := FALSE;
17729         l_progress := 'Done with reservation loop';
17730       END IF;
17731 
17732       IF (l_debug = 1) THEN
17733         debug_print('new l_remaining_qty='||l_remaining_qty||' last reservation_id='||l_reservation_id);
17734       END IF;
17735 
17736       EXIT WHEN ROUND(l_remaining_qty, 5) <= 0;
17737     END LOOP;
17738     l_progress := 'Done with reservation loop';
17739 
17740     IF ( ROUND(l_remaining_qty, 5) > 0 ) THEN
17741       IF (l_debug = 1) THEN
17742         debug_print('Not enough reserved quantity l_remaining_qty='||l_remaining_qty);
17743       END IF;
17744       fnd_message.set_name('INV', 'INV_UPDATE_RSV_FAILED');
17745       fnd_msg_pub.ADD;
17746       RAISE FND_API.G_EXC_ERROR;
17747     END IF;
17748 
17749     -- Standard check of p_commit.
17750     IF fnd_api.to_boolean(p_commit) THEN
17751       COMMIT WORK;
17752     END IF;
17753   EXCEPTION
17754     WHEN FND_API.G_EXC_ERROR THEN
17755       ROLLBACK TO upd_reservation_pup_new;
17756       x_return_status := fnd_api.g_ret_sts_error;
17757       FND_MSG_PUB.Count_And_Get(p_count => x_msg_count, p_data => x_msg_data);
17758       IF (l_debug = 1) THEN
17759         debug_print(l_api_name ||' Exec Err prog='||l_progress||' SQL error: '|| SQLERRM(SQLCODE));
17760       END IF;
17761     WHEN OTHERS THEN
17762       ROLLBACK TO upd_reservation_pup_new;
17763       x_return_status := fnd_api.g_ret_sts_unexp_error;
17764       FND_MSG_PUB.Count_And_Get(p_count => x_msg_count, p_data => x_msg_data);
17765       IF (l_debug = 1) THEN
17766         debug_print(l_api_name ||' Unexp Err prog='||l_progress||' SQL error: '|| SQLERRM(SQLCODE));
17767       END IF;
17768   END Upd_Reservation_PUP_New;
17769 
17770 
17771 -- Create overload porceudre for bug 2879208
17772    PROCEDURE upd_reservation_pup(
17773     x_return_status           OUT    NOCOPY VARCHAR2
17774   , x_msg_count               OUT    NOCOPY NUMBER
17775   , x_msg_data                OUT    NOCOPY VARCHAR2
17776   , p_commit                  IN     VARCHAR2 := fnd_api.g_false
17777   , p_init_msg_list           IN     VARCHAR2 := fnd_api.g_false
17778   , p_organization_id         IN     NUMBER
17779   , p_demand_source_header_id IN     NUMBER
17780   , p_demand_source_line_id   IN     NUMBER
17781   , p_from_subinventory_code  IN     VARCHAR2
17782   , p_from_locator_id         IN     NUMBER
17783   , p_to_subinventory_code    IN     VARCHAR2
17784   , p_to_locator_id           IN     NUMBER
17785   , p_inventory_item_id       IN     NUMBER
17786   , p_revision                IN     VARCHAR2
17787   , p_lot_number              IN     VARCHAR2
17788   , p_quantity                IN     NUMBER
17789   , p_uom                     IN     VARCHAR2
17790   , p_validation_flag         IN     VARCHAR2 := fnd_api.g_false
17791   , p_force_reservation_flag  IN     VARCHAR2 := fnd_api.g_false
17792   )
17793      IS
17794 
17795    BEGIN
17796       upd_reservation_pup_new
17797 	(
17798 	 x_return_status              => x_return_status
17799 	 , x_msg_count                  => x_msg_count
17800 	 , x_msg_data                   => x_msg_data
17801 	 , p_organization_id            => p_organization_id
17802 	 , p_demand_source_header_id    => p_demand_source_header_id
17803 	 , p_demand_source_line_id      => p_demand_source_line_id
17804 	 , p_from_subinventory_code     => p_from_subinventory_code
17805 	 , p_from_locator_id            => p_from_locator_id
17806 	 , p_to_subinventory_code       => p_to_subinventory_code
17807 	 , p_to_locator_id              => p_to_locator_id
17808 	 , p_inventory_item_id          => p_inventory_item_id
17809 	 , p_revision                   => p_revision
17810 	 , p_lot_number                 => p_lot_number
17811 	 , p_quantity                   => p_quantity
17812 	 , p_uom                        => p_uom
17813 	 , p_force_reservation_flag     => p_force_reservation_flag
17814 	 , p_requirement_date           => NULL
17815         );
17816    END upd_reservation_pup;
17817 
17818 
17819 
17820 
17821   PROCEDURE transfer_lpn_trx_reservation(
17822     x_return_status          OUT    NOCOPY VARCHAR2
17823   , x_msg_count              OUT    NOCOPY NUMBER
17824   , x_msg_data               OUT    NOCOPY VARCHAR2
17825   , p_commit                 IN     VARCHAR2 := fnd_api.g_false
17826   , p_init_msg_list          IN     VARCHAR2 := fnd_api.g_false
17827   , p_transaction_temp_id    IN     NUMBER := 0
17828   , p_organization_id        IN     NUMBER
17829   , p_lpn_id                 IN     NUMBER
17830   , p_from_subinventory_code IN     VARCHAR2
17831   , p_from_locator_id        IN     NUMBER
17832   , p_to_subinventory_code   IN     VARCHAR2
17833   , p_to_locator_id          IN     NUMBER
17834   , p_inventory_item_id      IN     NUMBER := NULL
17835   , p_revision               IN     VARCHAR2 := NULL
17836   , p_lot_number             IN     VARCHAR2 := NULL
17837   , p_trx_quantity           IN     NUMBER := NULL
17838   , p_trx_uom                IN     VARCHAR2 := NULL
17839   ) IS
17840     l_count    NUMBER  := 0;
17841     l_lotfound BOOLEAN := FALSE;
17842 
17843 
17844     l_api_version_number NUMBER := 1.0;
17845 
17846     l_loop_counter NUMBER := 0;   -- bug 2879208
17847 
17848     CURSOR c_lottmp IS
17849       SELECT lot_number
17850            , transaction_quantity
17851         FROM mtl_transaction_lots_temp
17852        WHERE transaction_temp_id = p_transaction_temp_id;
17853 
17854 	    --bug 2648532 - performance changes
17855 	    CURSOR lpn_deliveries IS
17856 	       SELECT wdd.source_header_id
17857 	            , wdd.source_line_id
17858 	            , wdd.inventory_item_id
17859 	            , wdd.revision
17860 	            , wdd.lot_number
17861 	            , wdd.requested_quantity
17862                     , wdd.picked_quantity --9699014
17863 	            , wdd.requested_quantity_uom
17864 	            , wdd.delivery_detail_id
17865 	            , wdd2.lpn_id
17866 	         FROM wsh_delivery_details wdd,
17867 	              wsh_delivery_assignments wda,
17868 	              wsh_delivery_details wdd2
17869 	        WHERE wdd.container_flag = 'N' --bug4639858 Only want wdd lines that represent items
17870 	          AND wdd.delivery_detail_id = wda.delivery_detail_id
17871 	          AND wda.parent_delivery_detail_id = wdd2.delivery_detail_id
17872 	          -- Workaround for performance issue 3631133
17873 	          -- lpn_id for the LPN and it's child lpns will be stored in this global
17874 	          -- temp table under the line_id.  this is to avoid a connect by statment
17875 	          -- in this cursor
17876                   -- Note: Repeating the where condition below to force the db optimizer
17877                   -- to drive the main-query from sub-query. bug: 4145360
17878              AND wdd2.released_status = 'X'    -- For LPN reuse ER : 6845650
17879 	          AND wdd2.lpn_id IN ( SELECT line_id
17880 	                               FROM   WMS_TXN_CONTEXT_TEMP
17881 	                               WHERE  txn_source_name = 'XFER_LPN_RES'
17882 	                                 AND  txn_source_name = 'XFER_LPN_RES' );
17883 
17884     CURSOR item_deliveries(p_lot VARCHAR2) IS
17885       SELECT source_header_id
17886            , source_line_id
17887         FROM wsh_delivery_details
17888        WHERE inventory_item_id = p_inventory_item_id
17889          AND NVL(subinventory, '@@@@') = NVL(p_from_subinventory_code, '@@@@')
17890          AND NVL(revision, '@@@@') = NVL(p_revision, '@@@@')
17891          AND NVL(lot_number, '@@@@') = NVL(p_lot, '@@@@')
17892          AND delivery_detail_id IN
17893 	(SELECT wda.delivery_detail_id
17894 	 FROM wsh_delivery_assignments wda,
17895 	 wsh_delivery_details wdd2,
17896 	 wms_license_plate_numbers wlpn
17897 	 WHERE wda.parent_delivery_detail_id = wdd2.delivery_detail_id
17898 	 AND   wdd2.lpn_id                   = wlpn.lpn_id
17899 	 AND   wlpn.outermost_lpn_id         = p_lpn_id
17900     AND   wdd2.released_status          = 'X');  -- For LPN reuse ER : 6845650
17901 
17902     l_debug number;
17903   BEGIN
17904     -- Standard Start of API savepoint
17905     SAVEPOINT transfer_lpn_trx_reservation;
17906     -- Use cache to get value for l_debug
17907     IF g_is_pickrelease_set IS NULL THEN
17908        g_is_pickrelease_set := 2;
17909        IF INV_CACHE.is_pickrelease THEN
17910           g_is_pickrelease_set := 1;
17911        END IF;
17912     END IF;
17913     IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
17914        g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
17915     END IF;     -- Use cache to get value for l_debug
17916 
17917     l_debug := g_debug;
17918     -- If p_lpn_id parameter has been popluated, then transfer is a consolidation
17919     IF (p_inventory_item_id IS NULL) THEN
17920       -- retrieve all the delivery items for that lpn and call transfer reservatin api
17921       IF (l_debug = 1) THEN
17922          debug_print('Reservation transfer of whole lpn: consolidate', 9);
17923       END IF;
17924 
17925       -- Workaround for performance issue 3631133
17926       -- lpn_id for the LPN and it's child lpns will be stored in this global
17927       -- temp table under the line_id
17928       INSERT INTO WMS_TXN_CONTEXT_TEMP ( line_id, txn_source_name )
17929       SELECT distinct lpn_id, 'XFER_LPN_RES'
17930       FROM   wms_license_plate_numbers
17931       START WITH lpn_id = p_lpn_id
17932       CONNECT BY parent_lpn_id = PRIOR lpn_id;
17933 
17934       FOR lpn_del_rec IN lpn_deliveries LOOP
17935 	 l_loop_counter := l_loop_counter + 1;   -- bug 2879208
17936 
17937         IF (l_debug = 1) THEN
17938            debug_print(
17939              'found rec src hdr id: '
17940           || lpn_del_rec.source_header_id
17941           || ' src line id: '
17942           || lpn_del_rec.source_line_id
17943           || ' item id: '
17944           || lpn_del_rec.inventory_item_id
17945           || ' rev: '
17946           || lpn_del_rec.revision
17947           || ' lot: '
17948           || lpn_del_rec.lot_number
17949 	  || ' ddid='||lpn_del_rec.delivery_detail_id
17950 	  || ' lpn='||lpn_del_rec.lpn_id
17951 	  , 9
17952         );
17953         END IF;
17954         upd_reservation_pup_new(
17955           x_return_status              => x_return_status
17956         , x_msg_count                  => x_msg_count
17957         , x_msg_data                   => x_msg_data
17958         , p_organization_id            => p_organization_id
17959         , p_demand_source_header_id    => lpn_del_rec.source_header_id
17960         , p_demand_source_line_id      => lpn_del_rec.source_line_id
17961         , p_from_subinventory_code     => p_from_subinventory_code
17962         , p_from_locator_id            => p_from_locator_id
17963         , p_to_subinventory_code       => p_to_subinventory_code
17964         , p_to_locator_id              => p_to_locator_id
17965         , p_inventory_item_id          => lpn_del_rec.inventory_item_id
17966         , p_revision                   => lpn_del_rec.revision
17967         , p_lot_number                 => lpn_del_rec.lot_number
17968         , p_quantity                   => NVL(lpn_del_rec.picked_quantity,lpn_del_rec.requested_quantity)  --9699014
17969         , p_uom                        => lpn_del_rec.requested_quantity_uom
17970 	, p_force_reservation_flag     => fnd_api.g_true
17971 	, p_requirement_date           => (Sysdate + l_loop_counter/(24*3600))  -- bug 2879208
17972     , p_source_lpn_id              => lpn_del_rec.lpn_id --bug12722739
17973         );
17974 
17975         IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
17976           IF (l_debug = 1) THEN
17977              debug_print('upd_reservation_pup failed '|| x_msg_data, 1);
17978           END IF;
17979           fnd_message.set_name('WMS', 'UPD_RESERVATION_PUP_FAIL');
17980           fnd_msg_pub.ADD;
17981           RAISE fnd_api.g_exc_unexpected_error;
17982         END IF;
17983       END LOOP;
17984 
17985       -- Workaround for performance issue 3631133
17986       -- need to delete the inserted values in temp table above
17987       DELETE FROM WMS_TXN_CONTEXT_TEMP
17988       WHERE  txn_source_name = 'XFER_LPN_RES';
17989     ELSE
17990       --LPN split, transfer reservation of a single item type
17991       IF (l_debug = 1) THEN
17992          debug_print('reservation transfer of item within an lpn: split', 9);
17993       END IF;
17994 
17995       --Check if item is lot controlled
17996       FOR v_lottmp IN c_lottmp LOOP
17997         l_loop_counter := l_loop_counter + 1;
17998 
17999         IF (l_debug = 1) THEN
18000            debug_print('item is lot controlled found lot= '|| v_lottmp.lot_number, 9);
18001         END IF;
18002         l_lotfound  := TRUE;
18003 
18004         FOR item_del_rec IN item_deliveries(v_lottmp.lot_number) LOOP
18005           IF (l_debug = 1) THEN
18006              debug_print('found rec src hdr id: '|| item_del_rec.source_header_id || ' src line id: ' || item_del_rec.source_line_id, 9);
18007           END IF;
18008 
18009           IF (l_count < 1) THEN
18010             upd_reservation_pup_new(
18011               x_return_status              => x_return_status
18012             , x_msg_count                  => x_msg_count
18013             , x_msg_data                   => x_msg_data
18014             , p_organization_id            => p_organization_id
18015             , p_demand_source_header_id    => item_del_rec.source_header_id
18016             , p_demand_source_line_id      => item_del_rec.source_line_id
18017             , p_from_subinventory_code     => p_from_subinventory_code
18018             , p_from_locator_id            => p_from_locator_id
18019             , p_to_subinventory_code       => p_to_subinventory_code
18020             , p_to_locator_id              => p_to_locator_id
18021             , p_inventory_item_id          => p_inventory_item_id
18022             , p_revision                   => p_revision
18023             , p_lot_number                 => v_lottmp.lot_number
18024             , p_quantity                   => v_lottmp.transaction_quantity
18025             , p_uom                        => p_trx_uom
18026             , p_force_reservation_flag     => fnd_api.g_true
18027             , p_requirement_date           => (Sysdate + l_loop_counter/(24*3600))
18028             );
18029 
18030             IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
18031               IF (l_debug = 1) THEN
18032                  debug_print('upd_reservation_pup failed '|| x_msg_data, 1);
18033               END IF;
18034               fnd_message.set_name('WMS', 'UPD_RESERVATION_PUP_FAIL');
18035               fnd_msg_pub.ADD;
18036               RAISE fnd_api.g_exc_unexpected_error;
18037             END IF;
18038 
18039             l_count  := l_count + 1;
18040           ELSE
18041             IF (l_debug = 1) THEN
18042                debug_print('**Split disallowed for lpns with multiple delivery lines for the same item', 1);
18043             END IF;
18044             fnd_message.set_name('INV', 'INV_MULTI_DEL_SPLIT_ERROR');
18045             fnd_msg_pub.ADD;
18046             RAISE fnd_api.g_exc_error;
18047           END IF;
18048         END LOOP;
18049       END LOOP;
18050 
18051       IF (NOT l_lotfound) THEN
18052         l_loop_counter := l_loop_counter + 1;
18053 
18054         --Non lot controlled item, or single lot entry use given parameters.
18055         IF (l_debug = 1) THEN
18056            debug_print('no rows found in mtlt, processing single row', 9);
18057         END IF;
18058 
18059         FOR item_del_rec IN item_deliveries(p_lot_number) LOOP
18060           IF (l_debug = 1) THEN
18061              debug_print('found rec src hdr id: '|| item_del_rec.source_header_id || ' src line id: ' || item_del_rec.source_line_id, 9);
18062           END IF;
18063 
18064           IF (l_count < 1) THEN
18065             upd_reservation_pup_new(
18066               x_return_status              => x_return_status
18067             , x_msg_count                  => x_msg_count
18068             , x_msg_data                   => x_msg_data
18069             , p_organization_id            => p_organization_id
18070             , p_demand_source_header_id    => item_del_rec.source_header_id
18071             , p_demand_source_line_id      => item_del_rec.source_line_id
18072             , p_from_subinventory_code     => p_from_subinventory_code
18073             , p_from_locator_id            => p_from_locator_id
18074             , p_to_subinventory_code       => p_to_subinventory_code
18075             , p_to_locator_id              => p_to_locator_id
18076             , p_inventory_item_id          => p_inventory_item_id
18077             , p_revision                   => p_revision
18078             , p_lot_number                 => p_lot_number
18079             , p_quantity                   => p_trx_quantity
18080             , p_uom                        => p_trx_uom
18081             , p_force_reservation_flag     => fnd_api.g_true
18082             , p_requirement_date           => (Sysdate + l_loop_counter/(24*3600))
18083             );
18084 
18085             IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
18086               IF (l_debug = 1) THEN
18087                  debug_print('upd_reservation_pup failed '|| x_msg_data, 1);
18088               END IF;
18089               fnd_message.set_name('WMS', 'UPD_RESERVATION_PUP_FAIL');
18090               fnd_msg_pub.ADD;
18091               RAISE fnd_api.g_exc_unexpected_error;
18092             END IF;
18093 
18094             l_count  := l_count + 1;
18095           ELSE
18096             IF (l_debug = 1) THEN
18097                debug_print('**Split disallowed for lpns with multiple delivery lines for the same item', 1);
18098             END IF;
18099             fnd_message.set_name('INV', 'INV_MULTI_DEL_SPLIT_ERROR');
18100             fnd_msg_pub.ADD;
18101             RAISE fnd_api.g_exc_error;
18102           END IF;
18103         END LOOP;
18104       END IF;
18105     END IF;
18106   EXCEPTION
18107     WHEN fnd_api.g_exc_error THEN
18108       fnd_message.set_name('WMS', 'INV_XFR_RSV_FAILURE');
18109       fnd_msg_pub.ADD;
18110       ROLLBACK TO transfer_lpn_trx_reservation;
18111       x_return_status  := fnd_api.g_ret_sts_error;
18112       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
18113     WHEN fnd_api.g_exc_unexpected_error THEN
18114       fnd_message.set_name('WMS', 'INV_XFR_RSV_FAILURE');
18115       fnd_msg_pub.ADD;
18116       ROLLBACK TO transfer_lpn_trx_reservation;
18117       x_return_status  := fnd_api.g_ret_sts_unexp_error;
18118       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
18119     WHEN OTHERS THEN
18120       fnd_message.set_name('WMS', 'INV_XFR_RSV_FAILURE');
18121       fnd_msg_pub.ADD;
18122       ROLLBACK TO transfer_lpn_trx_reservation;
18123       x_return_status  := fnd_api.g_ret_sts_unexp_error;
18124 
18125       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
18126         fnd_msg_pub.add_exc_msg('wms_upd_res_pvt', 'TRANSFER_LPN_TRX_RESERVATION');
18127       END IF;
18128 
18129       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
18130   END transfer_lpn_trx_reservation;
18131 
18132   PROCEDURE insert_rsv_temp(
18133     p_organization_id                  NUMBER
18134   , p_inventory_item_id                NUMBER
18135   , p_primary_reservation_quantity     NUMBER
18136   , p_tree_id                          NUMBER
18137   , p_reservation_id                   NUMBER
18138   , x_return_status                OUT NOCOPY VARCHAR2
18139   , p_demand_source_line_id            NUMBER
18140   , p_demand_source_header_id          NUMBER
18141   , p_demand_source_name               VARCHAR2
18142   ) IS
18143     l_api_name      VARCHAR2(100)  := 'Insert_rsv_temp';
18144     x_msg_count     NUMBER;
18145     x_msg_data      VARCHAR2(1000);
18146     l_return_status VARCHAR2(1)    := fnd_api.g_ret_sts_success;
18147     l_debug         NUMBER := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
18148   BEGIN
18149     INSERT INTO rsv_temp
18150                 (
18151                 p_organization_id
18152               , p_inventory_item_id
18153               , p_primary_reservation_quantity
18154               , p_tree_id
18155               , p_reservation_id
18156               , p_demand_source_line_id
18157               , p_demand_source_header_id
18158               , p_demand_source_name
18159                 )
18160          VALUES (
18161                 p_organization_id
18162               , p_inventory_item_id
18163               , p_primary_reservation_quantity
18164               , p_tree_id
18165               , p_reservation_id
18166               , p_demand_source_line_id
18167               , p_demand_source_header_id
18168               , p_demand_source_name
18169                 );
18170 
18171          IF (l_debug = 1) THEN
18172             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);
18173          END IF;
18174 
18175     x_return_status  := l_return_status;
18176   EXCEPTION
18177     WHEN OTHERS THEN
18178       x_return_status  := fnd_api.g_ret_sts_unexp_error;
18179 
18180       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
18181         fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
18182       END IF;
18183 
18184       --  Get message count and data
18185       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
18186   END insert_rsv_temp;
18187 
18188   --Ref 2132071 This procedure pick up reservations created from last commit issued
18189   --and check if there is any node violation.If there is one then it will clear
18190   --the reservations .This call also clears Quantity cache at the end of call
18191   --to ensure that bad trees are not in place.
18192 
18193   PROCEDURE do_check_for_commit(
18194     p_api_version_number  IN     NUMBER
18195   , p_init_msg_lst        IN     VARCHAR2 DEFAULT fnd_api.g_false
18196   , x_return_status       OUT    NOCOPY VARCHAR2
18197   , x_msg_count           OUT    NOCOPY NUMBER
18198   , x_msg_data            OUT    NOCOPY VARCHAR2
18199   , x_failed_rsv_temp_tbl OUT    NOCOPY inv_reservation_global.mtl_failed_rsv_tbl_type
18200   ) IS
18201     l_api_version_number CONSTANT NUMBER                                          := 1.0;
18202     l_api_name           CONSTANT VARCHAR2(30)                                    := 'Do_check_for_commit';
18203     l_return_status               VARCHAR2(1)                                     := fnd_api.g_ret_sts_success;
18204     l_no_violation                BOOLEAN;
18205     l_root_id                     INTEGER;
18206     l_failed_rsv_temp_tbl         inv_reservation_global.mtl_failed_rsv_tbl_type;
18207     l_failed_rsv_temp_rec         inv_reservation_global.mtl_failed_rsv_rec_type;
18208     l_rsv_rec                     inv_reservation_global.mtl_reservation_rec_type;
18209     p_original_serial_number      inv_reservation_global.serial_number_tbl_type;
18210     l_reservation_id              NUMBER;
18211     l_failed_rsv_temp_tbl_count   INTEGER                                         := 0;
18212     l_error_code                  VARCHAR2(100);
18213     l_demand_source_line_id       NUMBER;
18214     l_demand_source_header_id     NUMBER;
18215     l_demand_source_name          VARCHAR2(1000);
18216     l_organization_id             NUMBER;
18217     l_inventory_item_id           NUMBER;
18218 
18219     l_debug                       NUMBER := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
18220 
18221     -- Bug 3926414, added order by clause to avoid deadlock.
18222     CURSOR tree_csr IS
18223       SELECT p_tree_id
18224         FROM rsv_temp
18225         ORDER BY p_organization_id, p_inventory_item_id;
18226 
18227     --Bug 6812723, changing input from p_tree_id to l_tree_id
18228     CURSOR rsv_csr(l_tree_id NUMBER) IS
18229       SELECT p_reservation_id
18230            , p_organization_id
18231            , p_inventory_item_id
18232            , p_demand_source_line_id
18233            , p_demand_source_header_id
18234            , p_demand_source_name
18235         FROM rsv_temp
18236        WHERE p_tree_id = l_tree_id;
18237   BEGIN
18238     FOR tree_rec IN tree_csr LOOP
18239 
18240       IF (l_debug = 1) THEN
18241             debug_print('Calling do_check for tree_id = '||tree_rec.p_tree_id);
18242       END IF;
18243 
18244       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);
18245 
18246       IF l_return_status = fnd_api.g_ret_sts_error THEN
18247         RAISE fnd_api.g_exc_error;
18248       END IF;
18249 
18250       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
18251         RAISE fnd_api.g_exc_unexpected_error;
18252       END IF;
18253 
18254       IF NOT (l_no_violation) THEN
18255         OPEN rsv_csr(tree_rec.p_tree_id);
18256 
18257         LOOP
18258           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;
18259           EXIT WHEN rsv_csr%NOTFOUND;
18260           l_rsv_rec.reservation_id                            := l_reservation_id;
18261 
18262           IF (l_debug = 1) THEN
18263                 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);
18264              END IF;
18265 
18266           delete_reservation(
18267             p_api_version_number         => 1.0
18268           , p_init_msg_lst               => p_init_msg_lst
18269           , x_return_status              => l_return_status
18270           , x_msg_count                  => x_msg_count
18271           , x_msg_data                   => x_msg_data
18272           , p_rsv_rec                    => l_rsv_rec
18273           , p_original_serial_number     => p_original_serial_number
18274           , p_validation_flag            => fnd_api.g_false
18275           );
18276 
18277           IF l_return_status = fnd_api.g_ret_sts_error THEN
18278             RAISE fnd_api.g_exc_error;
18279           END IF;
18280 
18281           --
18282           IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
18283             RAISE fnd_api.g_exc_unexpected_error;
18284           --  END IF;
18285           END IF;
18286 
18287           l_failed_rsv_temp_tbl_count                         := l_failed_rsv_temp_tbl_count + 1;
18288           l_failed_rsv_temp_rec.reservation_id                := l_reservation_id;
18289           l_failed_rsv_temp_rec.organization_id               := l_organization_id;
18290           l_failed_rsv_temp_rec.inventory_item_id             := l_inventory_item_id;
18291           l_failed_rsv_temp_rec.demand_source_line_id         := l_demand_source_line_id;
18292           l_failed_rsv_temp_rec.demand_source_header_id       := l_demand_source_header_id;
18293           l_failed_rsv_temp_rec.demand_source_name            := l_demand_source_name;
18294           l_failed_rsv_temp_tbl(l_failed_rsv_temp_tbl_count)  := l_failed_rsv_temp_rec;
18295         END LOOP;
18296 
18297         IF rsv_csr%ISOPEN THEN
18298           CLOSE rsv_csr;
18299         END IF;
18300       END IF;
18301     END LOOP;
18302 
18303     x_failed_rsv_temp_tbl  := l_failed_rsv_temp_tbl;
18304     x_return_status        := l_return_status;
18305     inv_quantity_tree_pub.clear_quantity_cache;
18306   EXCEPTION
18307     WHEN fnd_api.g_exc_error THEN
18308       x_return_status  := fnd_api.g_ret_sts_error;
18309       --  Get message count and data
18310       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
18311     WHEN fnd_api.g_exc_unexpected_error THEN
18312       x_return_status  := fnd_api.g_ret_sts_unexp_error;
18313       --  Get message count and data
18314       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
18315     WHEN OTHERS THEN
18316       x_return_status  := fnd_api.g_ret_sts_unexp_error;
18317 
18318       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
18319         fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
18320       END IF;
18321 
18322       --  Get message count and data
18323       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
18324   END do_check_for_commit;
18325 
18326   /**** {{ R12 Enhanced reservations code changes }}****/
18327   PROCEDURE Transfer_Reservation_SubXfer
18328     ( p_api_version_number         IN  NUMBER   DEFAULT 1.0
18329     , p_init_msg_lst               IN  VARCHAR2 DEFAULT fnd_api.g_false
18330     , x_return_status              OUT NOCOPY VARCHAR2
18331     , x_msg_count                  OUT NOCOPY NUMBER
18332     , x_msg_data                   OUT NOCOPY VARCHAR2
18333     , p_Inventory_Item_Id          IN  Number
18334     , p_Organization_id            IN  Number
18335     , p_original_Reservation_Id    IN  Number
18336     , p_From_Serial_Number         IN  Varchar2
18337     , p_to_SubInventory            IN  Varchar2
18338     , p_To_Locator_Id              IN  Number
18339     , p_to_serial_number           IN  Varchar2
18340     , p_validation_flag            IN  VARCHAR2
18341     , x_to_reservation_id          OUT NOCOPY NUMBER)
18342 
18343     IS
18344 
18345         l_api_name    CONSTANT VARCHAR2(30) := 'Transfer_Reservation_SubXfer';
18346         -- Define local variables
18347         l_original_rsv_rec           inv_reservation_global.mtl_reservation_rec_type ;
18348         l_to_rsv_rec                 inv_reservation_global.mtl_reservation_rec_type ;
18349         l_original_serial_number_Tab inv_reservation_global.serial_number_tbl_type ;
18350         l_to_serial_number_Tab       inv_reservation_global.serial_number_tbl_type ;
18351         l_Reservation_Id             Number;
18352         l_return_status              VARCHAR2(1):= fnd_api.g_ret_sts_success;
18353 	l_debug number;
18354 
18355         --Define a cursor that gets ReservationId information for a given org_id,
18356         --item_id and serial number information
18357         Cursor Get_Reservation_ID_Cur_Type
18358                      ( v_Serial_Number     Varchar2
18359                       ,v_Inventory_Item_Id Number
18360                       ,v_Organization_Id   Number ) IS
18361 
18362              Select Reservation_ID
18363                From Mtl_Serial_Numbers
18364               Where Inventory_Item_Id = v_Inventory_Item_Id
18365                 and current_organization_id = v_Organization_id
18366                 and Serial_Number = v_Serial_Number;
18367         BEGIN
18368 
18369            -- Use cache to get value for l_debug
18370            IF g_is_pickrelease_set IS NULL THEN
18371               g_is_pickrelease_set := 2;
18372               IF INV_CACHE.is_pickrelease THEN
18373                  g_is_pickrelease_set := 1;
18374               END IF;
18375            END IF;
18376            IF (g_is_pickrelease_set <> 1) OR (g_debug IS NULL) THEN
18377               g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
18378            END IF;
18379 
18380            l_debug := g_debug;
18381 
18382               -- Check if reservation id parameter has any value if not then get value from mtl_serial_numbers table.
18383 	   If p_original_Reservation_Id IS Null THEN
18384 	      IF (l_debug = 1) THEN
18385 		 debug_print('reservation_id is not passed');
18386 	      END IF;
18387                    Open Get_Reservation_ID_Cur_Type
18388                         (p_From_Serial_Number,
18389                          p_Inventory_Item_Id,
18390                          p_Organization_Id );
18391                    Fetch Get_Reservation_ID_Cur_Type Into l_Reservation_Id;
18392                    Close Get_Reservation_ID_Cur_Type ;
18393 	    ELSE
18394 	      IF (l_debug = 1) THEN
18395 		 debug_print('reservation_id is passed');
18396 	      END IF;
18397                  l_Reservation_Id := p_original_Reservation_Id ;
18398               End If;
18399               If l_Reservation_Id IS NULL  Then
18400                    RAISE fnd_api.g_exc_unexpected_error;
18401               End If;
18402 
18403               l_original_rsv_rec.Reservation_id := l_Reservation_Id ;
18404               l_To_Rsv_Rec.subinventory_code    := p_to_SubInventory ;
18405               l_To_Rsv_Rec.locator_id           := p_To_Locator_Id ;
18406 	      l_to_rsv_rec.primary_reservation_quantity := 1;
18407              -- l_Original_serial_number_Tab(1).Serial_Number := p_from_serial_number;
18408               l_to_serial_number_tab(1).Serial_Number       := p_to_serial_number;
18409               l_to_serial_number_tab(1).inventory_item_id   := p_inventory_item_id;
18410               --Call Reservation Transafer API
18411 
18412               Inv_Reservation_Pvt.transfer_reservation
18413 		(
18414                  p_api_version_number     => p_api_version_number ,
18415                  p_init_msg_lst           => p_init_msg_lst,
18416                  x_return_status          => l_return_status,
18417                  x_msg_count              => x_msg_count,
18418                  x_msg_data               => x_msg_data,
18419                  p_original_rsv_rec       => l_original_rsv_rec,
18420                  p_to_rsv_rec             => l_To_Rsv_Rec ,
18421                  p_original_serial_number => l_original_serial_number_Tab,
18422                  p_to_serial_number       => l_to_serial_number_Tab,
18423                  p_validation_flag        => p_Validation_Flag,
18424 		 x_reservation_id         => x_To_Reservation_Id );
18425 
18426 	      IF (l_debug = 1) THEN
18427 		 debug_print('After calling transfer reservation. Return status: ' ||  l_return_status);
18428 	      END IF;
18429 
18430 	      IF l_return_status = fnd_api.g_ret_sts_error THEN
18431 		 RAISE fnd_api.g_exc_error;
18432 	      END IF ;
18433 
18434            IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
18435               RAISE fnd_api.g_exc_unexpected_error;
18436            END IF;
18437 
18438            x_return_status := l_return_status;
18439 
18440         EXCEPTION
18441 
18442             WHEN fnd_api.g_exc_error THEN
18443                 x_return_status := fnd_api.g_ret_sts_error;
18444 
18445                 --  Get message count and data
18446                 fnd_msg_pub.count_and_get
18447                   (  p_count => x_msg_count
18448                    , p_data  => x_msg_data
18449                    );
18450 
18451            WHEN fnd_api.g_exc_unexpected_error THEN
18452                 x_return_status := fnd_api.g_ret_sts_unexp_error ;
18453 
18454                 --  Get message count and data
18455                 fnd_msg_pub.count_and_get
18456                   (  p_count  => x_msg_count
18457                    , p_data   => x_msg_data
18458                     );
18459 
18460             WHEN OTHERS THEN
18461                 x_return_status := fnd_api.g_ret_sts_unexp_error ;
18462 
18463                 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
18464                   THEN
18465                    fnd_msg_pub.add_exc_msg
18466                      (  g_pkg_name
18467                       , l_api_name
18468                       );
18469                 END IF;
18470 
18471                 --  Get message count and data
18472                 fnd_msg_pub.count_and_get
18473                   (  p_count  => x_msg_count
18474                    , p_data   => x_msg_data
18475                      );
18476 
18477   END transfer_reservation_SubXfer;
18478 
18479   PROCEDURE transfer_serial_rsv_in_LPN(
18480      x_return_status             OUT NOCOPY VARCHAR2
18481    , x_msg_count                 OUT NOCOPY NUMBER
18482    , x_msg_data                  OUT NOCOPY VARCHAR2
18483    , p_organization_id           IN  NUMBER
18484    , p_inventory_item_id         IN  NUMBER DEFAULT NULL
18485    , p_lpn_id                    IN  NUMBER
18486    , p_outermost_lpn_id          IN  NUMBER
18487    , p_to_subinventory_code      IN  VARCHAR2
18488    , p_to_locator_id             IN  NUMBER)
18489   IS
18490     l_api_name                   CONSTANT VARCHAR2(30) := 'transfer_serial_rsv_in_LPN';
18491     l_return_status              VARCHAR2(1)    := fnd_api.g_ret_sts_success;
18492     l_msg_count                  NUMBER;
18493     l_msg_data                   VARCHAR2(1000);
18494     l_debug                      NUMBER;
18495 
18496     TYPE rsv_serials_item_rec    IS RECORD
18497       (
18498          reservation_id           NUMBER
18499        , serial_number            VARCHAR2(30)
18500        , inventory_item_id        NUMBER
18501       );
18502 
18503     TYPE rsv_serials_item_tbl IS TABLE OF rsv_serials_item_rec
18504       INDEX BY BINARY_INTEGER;
18505 
18506     l_rsv_serials_tbl            rsv_serials_item_tbl;
18507     l_serial_number_tbl          inv_reservation_global.serial_number_tbl_type;
18508     l_index                      NUMBER := 0;
18509     l_rsv_serials_tbl_count      NUMBER := 0;
18510     l_original_rsv_rec           inv_reservation_global.mtl_reservation_rec_type;
18511     l_to_rsv_rec                 inv_reservation_global.mtl_reservation_rec_type;
18512     l_reservation_id             NUMBER;
18513 
18514     -- cursor of getting reservations with serial_number reserved in the lpn which
18515     -- outermost_lpn_id of the lpn = p_outermost_lpn_id and the reservation does not
18516     -- have lpn reserved and p_inventory_item_id is passed.
18517     CURSOR serials_outer_lpn_with_item IS
18518        SELECT msn.reservation_id,
18519               msn.serial_number,
18520               msn.inventory_item_id
18521        FROM   mtl_reservations mr,
18522               mtl_serial_numbers msn
18523        WHERE  mr.organization_id = p_organization_id
18524        AND    mr.inventory_item_id = p_inventory_item_id
18525        AND    mr.reservation_id = msn.reservation_id
18526        AND    mr.lpn_id = null
18527        AND    msn.lpn_id IN (SELECT lpn_id
18528                              FROM   wms_license_plate_numbers
18529                              WHERE  outermost_lpn_id = p_outermost_lpn_id)
18530        GROUP BY msn.reservation_id, msn.serial_number, msn.inventory_item_id;
18531 
18532     -- cursor of getting reservations with serial_number reserved in the lpn which
18533     -- outermost_lpn_id of the lpn = p_outermost_lpn_id and the reservation does not
18534     -- have lpn reserved and p_inventory_item_id is not passed.
18535     CURSOR serials_outer_lpn_no_item IS
18536        SELECT msn.reservation_id,
18537               msn.serial_number,
18538               msn.inventory_item_id
18539        FROM   mtl_reservations mr,
18540               mtl_serial_numbers msn
18541        WHERE  mr.organization_id = p_organization_id
18542        AND    mr.reservation_id = msn.reservation_id
18543        AND    mr.lpn_id = null
18544        AND    msn.lpn_id IN (SELECT lpn_id
18545                              FROM   wms_license_plate_numbers
18546                              WHERE  outermost_lpn_id = p_outermost_lpn_id)
18547        GROUP BY msn.reservation_id, msn.serial_number, msn.inventory_item_id;
18548 
18549     -- cursor of getting reservations with serial_number reserved in the lpn
18550     -- which lpn_id = p_lpn_id and the reservation does not have lpn reserved
18551     -- and p_inventory_item_id is passed.
18552     CURSOR serials_lpn_with_item IS
18553        SELECT msn.reservation_id,
18554               msn.serial_number,
18555               msn.inventory_item_id
18556        FROM   mtl_reservations mr,
18557               mtl_serial_numbers msn
18558        WHERE  mr.organization_id = p_organization_id
18559        AND    mr.inventory_item_id = p_inventory_item_id
18560        AND    mr.reservation_id = msn.reservation_id
18561        AND    mr.lpn_id = null
18562        AND    msn.lpn_id = p_lpn_id
18563        GROUP BY msn.reservation_id, msn.serial_number, msn.inventory_item_id;
18564 
18565     -- cursor of getting reservations with serial_number reserved in the lpn
18566     -- which lpn_id = p_lpn_id and the reservation does not have lpn reserved
18567     -- and p_inventory_item_id is not passed.
18568     CURSOR serials_lpn_no_item IS
18569        SELECT msn.reservation_id,
18570               msn.serial_number,
18571               msn.inventory_item_id
18572        FROM   mtl_reservations mr,
18573               mtl_serial_numbers msn
18574        WHERE  mr.organization_id = p_organization_id
18575        AND    mr.reservation_id = msn.reservation_id
18576        AND    mr.lpn_id = null
18577        AND    msn.lpn_id = p_lpn_id
18578        GROUP BY msn.reservation_id, msn.serial_number, msn.inventory_item_id;
18579 
18580   BEGIN
18581     IF (g_debug IS NULL) THEN
18582         g_debug := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
18583     END IF;
18584 
18585     l_debug := g_debug;
18586 
18587     IF (l_debug = 1) THEN
18588         debug_print('In transfer_serial_rsv_in_LPN');
18589         debug_print('p_organization_id = ' || p_organization_id);
18590         debug_print('p_inventory_item_id = ' || p_inventory_item_id);
18591         debug_print('p_lpn_id = ' || p_lpn_id);
18592         debug_print('p_outermost_lpn_id = ' || p_outermost_lpn_id);
18593         debug_print('p_to_subinventory_code = ' || p_to_subinventory_code);
18594         debug_print('p_to_locator_id = ' || p_to_locator_id);
18595     END IF;
18596 
18597     IF (p_outermost_lpn_id is not NULL and p_lpn_id is not NULL) THEN
18598         IF (l_debug = 1) THEN
18599             debug_print('Error: both p_outermost_lpn_id and p_lpn_id are populated');
18600         END IF;
18601     ELSIF (p_outermost_lpn_id is not NULL) THEN
18602         IF (l_debug = 1) THEN
18603             debug_print('p_outermost_lpn_id is not NULL');
18604         END IF;
18605 
18606         IF (p_inventory_item_id is not NULL) THEN
18607             IF (l_debug = 1) THEN
18608                 debug_print('p_inventory_item_id is not NULL');
18609             END IF;
18610 
18611             OPEN serials_outer_lpn_with_item;
18612             FETCH serials_outer_lpn_with_item BULK COLLECT INTO l_rsv_serials_tbl;
18613             CLOSE serials_outer_lpn_with_item;
18614         ELSE
18615             IF (l_debug = 1) THEN
18616                 debug_print('p_inventory_item_id is NULL');
18617             END IF;
18618 
18619             OPEN serials_outer_lpn_no_item;
18620             FETCH serials_outer_lpn_no_item BULK COLLECT INTO l_rsv_serials_tbl;
18621             CLOSE serials_outer_lpn_no_item;
18622         END IF;
18623     ELSIF (p_lpn_id is not NULL) THEN
18624         IF (l_debug = 1) THEN
18625             debug_print('p_lpn_id is not NULL');
18626         END IF;
18627 
18628         IF (p_inventory_item_id is not NULL) THEN
18629             OPEN serials_lpn_with_item;
18630             FETCH serials_lpn_with_item BULK COLLECT INTO l_rsv_serials_tbl;
18631             CLOSE serials_lpn_with_item;
18632         ELSE
18633             OPEN serials_lpn_no_item;
18634             FETCH serials_lpn_no_item BULK COLLECT INTO l_rsv_serials_tbl;
18635             CLOSE serials_lpn_no_item;
18636         END IF;
18637     END IF;
18638 
18639     l_rsv_serials_tbl_count := l_rsv_serials_tbl.COUNT;
18640     l_index := 0;
18641 
18642     -- construct to reservation record if l_rsv_serials_tbl_count > 0
18643     IF (l_rsv_serials_tbl_count > 0) THEN
18644         l_to_rsv_rec.subinventory_code := p_to_subinventory_code;
18645         l_to_rsv_rec.locator_id := p_to_locator_id;
18646 
18647        -- construct original reservation record and serial number table
18648        FOR i in 1..l_rsv_serials_tbl_count LOOP
18649           l_index := l_index + 1;
18650 
18651           -- construct serial number table for same reservation_id
18652           l_serial_number_tbl(l_index).inventory_item_id := l_rsv_serials_tbl(i).inventory_item_id;
18653           l_serial_number_tbl(l_index).serial_number := l_rsv_serials_tbl(i).serial_number;
18654 
18655           -- need to check the next record's reservation id with current reservation_id
18656           -- or if it's the last record of the serial number table
18657           IF ((i+1 <= l_rsv_serials_tbl_count AND
18658                 l_rsv_serials_tbl(i).reservation_id <> l_rsv_serials_tbl(i+1).reservation_id)
18659                   OR i = l_rsv_serials_tbl_count) THEN
18660 
18661              -- if the current reservation_id <> next record's reservation_id,
18662              -- or it is the last record of the serial number table, then
18663              -- finished construct the serial number table, contruct original reservation record
18664              -- and call transfer_reservation API
18665 
18666              l_original_rsv_rec.reservation_id := l_rsv_serials_tbl(i).reservation_id;
18667 
18668              IF (l_debug = 1) THEN
18669                  debug_print('calling transfer_reservations');
18670                  debug_print('original rec rsv id = ' || l_original_rsv_rec.reservation_id);
18671              END IF;
18672 
18673              inv_reservation_pvt.transfer_reservation(
18674                   p_api_version_number     => 1.0
18675                 , p_init_msg_lst           => fnd_api.g_false
18676                 , x_return_status          => l_return_status
18677                 , x_msg_count              => l_msg_count
18678                 , x_msg_data               => l_msg_data
18679                 , p_original_rsv_rec       => l_original_rsv_rec
18680                 , p_to_rsv_rec             => l_to_rsv_rec
18681                 , p_original_serial_number => l_serial_number_tbl
18682                 , p_to_serial_number       => l_serial_number_tbl
18683                 , p_validation_flag        => fnd_api.g_true
18684                 , x_reservation_id         => l_reservation_id
18685                 );
18686 
18687              IF (l_return_status = fnd_api.g_ret_sts_error) THEN
18688                 IF (l_debug = 1) THEN
18689                    debug_print('Error return status from transfer_reservation');
18690                 END IF;
18691 
18692                 RAISE fnd_api.g_exc_error;
18693              ELSIF (l_return_status = fnd_api.g_ret_sts_unexp_error) THEN
18694                 IF (l_debug = 1) THEN
18695                     debug_print('Unexpected return status from transfer_reservation');
18696                 END IF;
18697 
18698                 RAISE fnd_api.g_exc_unexpected_error;
18699              END IF;
18700 
18701              -- delete the content of l_serial_number_tbl and reset the index
18702              l_serial_number_tbl.DELETE;
18703              l_index := 0;
18704           END IF;
18705        END LOOP;
18706     END IF;
18707 
18708     x_return_status := l_return_status;
18709     x_msg_count     := l_msg_count;
18710     x_msg_data      := l_msg_data;
18711 
18712   EXCEPTION
18713     WHEN fnd_api.g_exc_error THEN
18714       x_return_status  := fnd_api.g_ret_sts_error;
18715       --  Get message count and data
18716       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
18717     WHEN fnd_api.g_exc_unexpected_error THEN
18718       x_return_status  := fnd_api.g_ret_sts_unexp_error;
18719 
18720       IF (l_debug = 1) THEN
18721           debug_print('unexpected error: ' || SQLERRM);
18722       END IF;
18723 
18724       --  Get message count and data
18725       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
18726     WHEN OTHERS THEN
18727       x_return_status  := fnd_api.g_ret_sts_unexp_error;
18728 
18729       IF (l_debug = 1) THEN
18730           debug_print('others error: ' || SQLERRM);
18731       END IF;
18732 
18733       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
18734         fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
18735       END IF;
18736 
18737       --  Get message count and data
18738       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
18739   END transfer_serial_rsv_in_LPN;
18740 
18741 /*** End R12 ***/
18742 
18743 END inv_reservation_pvt;
18744