DBA Data[Home] [Help]

PACKAGE BODY: APPS.INV_QUANTITY_TREE_PVT

Source


1 PACKAGE BODY inv_quantity_tree_pvt as
2 /* $Header: INVVQTTB.pls 120.61.12020000.15 2013/04/26 11:04:37 brana ship $*/
3 
4 g_pkg_name CONSTANT VARCHAR2(30) := 'INV_Quantity_Tree_PVT';
5 g_is_lot_control  BOOLEAN := FALSE;
6 
7 --
8 -- synonyms used in this program
9 --     qoh          quantity on hand
10 --     rqoh         reservable quantity on hand
11 --     qr           quantity reserved
12 --     att          available to transact
13 --     atr          available to reserve
14 
15 --    sqoh          secondary quantity on hand
16 --    srqoh         secondary reservable quantity on hand
17 --    sqr           secondary quantity reserved
18 --    satt          secondary available to transact
19 --    satr          secondary available to reserve
20 --------------------------------------------------
21 -- Data Types Definition
22 --------------------------------------------------
23 -- rootinfo_rec_type is the record type for a
24 -- record that points to a tree
25 TYPE rootinfo_rec_type IS RECORD
26   (   organization_id          NUMBER
27    ,  inventory_item_id        NUMBER
28    ,  is_DUOM_control          BOOLEAN
29    ,  is_revision_control      BOOLEAN
30    ,  is_lot_control           BOOLEAN
31    ,  is_serial_control        BOOLEAN
32    ,  is_lot_status_enabled    BOOLEAN
33    ,  neg_inv_allowed          BOOLEAN
34    ,  tree_mode                NUMBER
35    ,  include_suggestion       BOOLEAN
36    ,  asset_sub_only           BOOLEAN
37    ,  demand_source_type_id    NUMBER
38    ,  demand_source_header_id  NUMBER
39    ,  demand_source_line_id    NUMBER
40    ,  demand_source_name       VARCHAR2(80)  -- bug 11879550, increase it from 30 to 80
41    ,  demand_source_delivery   NUMBER
42    ,  asset_inventory_only     BOOLEAN
43    ,  lot_expiration_date      DATE
44    ,  grade_code               VARCHAR2(150)
45    ,  need_refresh             BOOLEAN
46    ,  item_node_index          INTEGER
47    ,  onhand_source            NUMBER
48    ,  pick_release             NUMBER
49    ,  hash_string              VARCHAR2(1000)
50    ,  unit_effective           NUMBER
51    ,  onhand_status_enabled    BOOLEAN              -- Bug 9150005
52    );
53 --
54 TYPE rootinfo_tbl_type IS TABLE OF rootinfo_rec_type
55   INDEX BY BINARY_INTEGER;
56 --
57 -- tree level constants
58 g_item_level       CONSTANT INTEGER := 1;
59 g_revision_level   CONSTANT INTEGER := 2;
60 g_lot_level        CONSTANT INTEGER := 3;
61 g_sub_level        CONSTANT INTEGER := 4;
62 g_locator_level    CONSTANT INTEGER := 5;
63 g_lpn_level        CONSTANT INTEGER := 6;
64 g_cost_group_level CONSTANT INTEGER := 7;
65 --
66 
67 -- node_rec_type is the record type for
68 -- a tree node. It contains a superset of
69 -- attributes for revision,
70 -- invConv change : is_reservable_sub is NOW used as reservable flag for
71 --            each subinv, locator, lot, serial.
72 -- invConv note : this rec_type is not used anymore.
73 TYPE node_rec_type IS RECORD
74   (   node_level          INTEGER      -- qualifies the type of the node
75    ,  revision            VARCHAR2(3)  -- valid if the node is a revision node
76    ,  lot_number          VARCHAR2(80) -- valid if the node is a lot node     (size)
77    ,  subinventory_code   VARCHAR2(10) -- valid if the node is a sub node
78    ,  is_reservable_sub   BOOLEAN      -- valid if the node is reservable    (comment)
79    ,  locator_id          NUMBER       -- valid if the node is a locator node
80    ,  qoh                 NUMBER
81    ,  rqoh                NUMBER
82    ,  qr                  NUMBER
83    ,  qs                  NUMBER
84    ,  att                 NUMBER
85    ,  atr                 NUMBER
86    ,  pqoh                NUMBER     -- packed quantity on hand
87   -- Bug 9644285. Now tracking reservable packed quantity on hand
88    ,  rpqoh               NUMBER
89    ,  sqoh                NUMBER
90    ,  srqoh               NUMBER
91    ,  sqr                 NUMBER
92    ,  sqs                 NUMBER
93    ,  satt                NUMBER
94    ,  satr                NUMBER
95    ,  spqoh               NUMBER
96    ,  srpqoh              NUMBER      -- Bug 9644285
97    ,  check_mark          BOOLEAN
98    ,  next_sibling_index  INTEGER
99    ,  first_child_index   INTEGER
100    ,  last_child_index    INTEGER
101    ,  parent_index        INTEGER
102    ,  lpn_id              NUMBER
103    ,  cost_group_id       NUMBER     -- valid if the node is a cost group node
104    ,  next_hash_record    NUMBER
105    ,  hash_string         VARCHAR2(300)
106    ,  node_index          INTEGER    --used by backup/restore tree
107    ,  qs_adj1             NUMBER     --Bug 4294336
108    ,  sqs_adj1            NUMBER     --Bug 4294336
109    );
110 --
111 TYPE node_tbl_type IS TABLE OF node_rec_type
112   INDEX BY BINARY_INTEGER;
113 
114 --
115 --Bug 1384720 - performance improvement
116 -- record keeps track of demand info only
117 -- Now, each transaction which queries the quantity tree has a
118 --  different demand record.  But the underlying tree (root_id)
119 --  might be the same for all the demand records.  Multiple
120 --  demand records can reference the same tree (represented by root_id).
121 --
122 TYPE demandinfo_rec_type IS RECORD
123   (   root_id                  INTEGER
124    ,  tree_mode                NUMBER
125    ,  pick_release             NUMBER
126    ,  demand_source_type_id    NUMBER
127    ,  demand_source_header_id  NUMBER
128    ,  demand_source_line_id    NUMBER
129    ,  demand_source_name       VARCHAR2(80)   -- bug 11879550, increase it from 30 to 80
130    ,  demand_source_delivery   NUMBER
131    );
132 --
133 TYPE demand_tbl_type IS TABLE OF demandinfo_rec_type
134    INDEX BY BINARY_INTEGER;
135 --
136 --Bug 1384720 - performance improvement,
137 --  This record holds information about the reservations for a transaction.
138 TYPE rsvinfo_rec_type IS RECORD
139   (   revision            VARCHAR2(3)
140    ,  lot_number          VARCHAR2(80)
141    ,  subinventory_code   VARCHAR2(10)
142    ,  locator_id          NUMBER
143    ,  lpn_id              NUMBER
144    ,  quantity            NUMBER
145    ,  secondary_quantity  NUMBER
146    );
147 --
148 TYPE rsvinfo_tbl_type IS TABLE OF rsvinfo_rec_type
149    INDEX BY BINARY_INTEGER;
150 
151 TYPE all_root_rec_type IS RECORD
152    (  root_id    NUMBER
153     , next_root  NUMBER
154     , last_root  NUMBER
155    );
156 
157 TYPE all_root_tbl_type IS TABLE OF all_root_rec_type
158    INDEX BY BINARY_INTEGER;
159 
160 TYPE backup_tree_rec_type IS RECORD
161    (  root_id    NUMBER
162     , first_node NUMBER
163     , last_node  NUMBER
164    );
165 
166 TYPE backup_tree_tbl_type IS TABLE of backup_tree_rec_type
167    INDEX BY BINARY_INTEGER;
168 
169 TYPE item_reservable_rec_type IS RECORD
170    (  value   BOOLEAN
171     , org_id  NUMBER
172    );
173 
174 -- Bug 5535030: PICK RELEASE PERFORMANCE ISSUES
175 TYPE sub_reservable_rec_type IS RECORD
176    (  reservable_type   NUMBER
177     , org_id            VARCHAR2(100)
178     , subinventory_code VARCHAR2(10)
179    );
180 --Bug#13108827
181 TYPE loc_reservable_rec_type IS RECORD
182    (  reservable_type   NUMBER
183     , org_id            VARCHAR2(100)
184     , inventory_location_id NUMBER
185    );
186 
187 TYPE lot_reservable_rec_type IS RECORD
188    (  reservable_type   NUMBER
189     , org_id            VARCHAR2(100)
190     , inventory_item_id NUMBER
191     , lot_number        VARCHAR2(80)
192    );
193 
194 --Bug#13734969
195 TYPE onhand_reservable_rec_type IS RECORD
196    (  reservable_type   NUMBER
197     , org_id            VARCHAR2(100)
198     , inventory_item_id NUMBER
199     , lot_number        VARCHAR2(80)
200     , subinventory_code VARCHAR2(10)
201     , locator_id        NUMBER
202     , lpn_id            NUMBER
203    );
204 
205 TYPE bulk_num_tbl_type IS TABLE OF NUMBER
206    INDEX BY BINARY_INTEGER;
207 TYPE bulk_date_tbl_type IS TABLE OF DATE
208    INDEX BY BINARY_INTEGER;
209 TYPE bulk_varchar_3_tbl_type IS TABLE OF VARCHAR2(3)
210    INDEX BY BINARY_INTEGER;
211 TYPE bulk_varchar_10_tbl_type IS TABLE OF VARCHAR2(10)
212    INDEX BY BINARY_INTEGER;
213 TYPE bulk_varchar_30_tbl_type IS TABLE OF VARCHAR2(30)
214    INDEX BY BINARY_INTEGER;
215 TYPE item_reservable_type IS TABLE OF item_reservable_rec_type
216    INDEX BY BINARY_INTEGER;
217 TYPE bulk_varchar_80_tbl_type IS TABLE OF VARCHAR2(80)
218    INDEX BY BINARY_INTEGER;
219 -- bug 8593965
220 TYPE bulk_varchar_150_tbl_type IS TABLE OF VARCHAR2(150)
221    INDEX BY BINARY_INTEGER;
222 
223 -- Bug 5535030: PICK RELEASE PERFORMANCE ISSUES
224 TYPE sub_reservable_type IS TABLE OF sub_reservable_rec_type
225    INDEX BY BINARY_INTEGER;
226 
227 --Bug#13108827
228 TYPE loc_reservable_type IS TABLE OF loc_reservable_rec_type
229    INDEX BY BINARY_INTEGER;
230 TYPE lot_reservable_type IS TABLE OF lot_reservable_rec_type
231    INDEX BY BINARY_INTEGER;
232 
233 --Bug#13734969
234 TYPE onhand_reservable_type IS TABLE OF onhand_reservable_rec_type
235    INDEX BY BINARY_INTEGER;
236 
237 --------------------------------------------------
238 -- Global Variables
239 --------------------------------------------------
240 --
241 -- rootinfo array, counter and tree node array and counter
242 g_rootinfos                   rootinfo_tbl_type;      -- rootinfo array
243 g_nodes                       node_tbl_type;          -- tree node array
244 g_rootinfo_counter            INTEGER := 0;           -- size of rootinfo arry
245 g_org_item_trees              bulk_num_tbl_type;
246 g_all_roots                   all_root_tbl_type;
247 g_all_roots_counter           NUMBER := 0;
248 g_max_hash_rec                NUMBER := 0;
249 g_saveroots                   rootinfo_tbl_type;      -- savepoint rootinfos
250 g_savenodes                   node_tbl_type;          -- savepoint tree nodes
251 
252 g_save_rsv_tree_id            INTEGER :=0;            -- savepoint rsv tree id -- bug 6683013
253 g_save_rsv_counter            NUMBER := 0;            -- savepoint rsv counter -- bug 6683013
254 g_saversvnode                 rsvinfo_tbl_type;       -- savepoint rsv nodes   -- bug 6683013
255 
256 
257 g_backup_trees                backup_tree_tbl_type;   --used by new backup_tree procedure
258 g_backup_nodes                node_tbl_type;          --used by new backup_tree procedure
259 
260 g_demand_info                 demand_tbl_type;
261 g_demand_counter              INTEGER := 0;
262 g_rsv_info                    rsvinfo_tbl_type;       -- all rsvs which are currently applied to the tree
263 g_rsv_counter                 INTEGER :=0;
264 g_rsv_tree_id                 INTEGER :=0;            -- tree for which rsv_info corresponds
265 
266 g_backup_tree_counter         INTEGER := 0;           --used by new backup_tree procedure
267 g_backup_node_counter         INTEGER := 0;           --used by new backup_tree procedure
268 
269 g_rsv_qty_counter             INTEGER := 0;
270 g_rsv_qty_node_level          bulk_num_tbl_type;
271 g_rsv_qty_revision            bulk_varchar_3_tbl_type;
272 g_rsv_qty_lot_number          bulk_varchar_80_tbl_type;
273 --bug 8593965
274 g_rsv_qty_grade_code          bulk_varchar_150_tbl_type;
275 
276 g_rsv_qty_subinventory_code   bulk_varchar_10_tbl_type;
277 g_rsv_qty_locator_id          bulk_num_tbl_type;
278 g_rsv_qty_lpn_id              bulk_num_tbl_type;
279 g_rsv_qty_cost_group_id       bulk_num_tbl_type;
280 g_rsv_qty_qoh                 bulk_num_tbl_type;
281 g_rsv_qty_atr                 bulk_num_tbl_type;
282 g_rsv_qty_sqoh                bulk_num_tbl_type;
283 g_rsv_qty_satr                bulk_num_tbl_type;
284 --
285 -- pjm support
286 --BENCHMARK - added unit effective info to tree node
287 g_unit_eff_enabled            VARCHAR(1) := NULL;
288 
289 --holds the Packed Quantity on hand for the last node queried using query_tree
290 g_pqoh                        NUMBER := NULL;
291 g_spqoh                       NUMBER := NULL;
292 
293 --9644285 holds the reservable Packed Quantity on hand for the last node queried using
294 --query_tree
295 g_rpqoh                       NUMBER := NULL;
296 g_srpqoh                      NUMBER := NULL;
297 
298 -- dummy lpn_id for Loose quantities
299 g_loose_lpn_id                NUMBER := -99999;
300 
301 g_organization_id             NUMBER := NULL;
302 g_lock_timeout                NUMBER := NULL;
303 
304 g_hash_string                 VARCHAR2(1000) := NULL;
305 g_empty_root_index            NUMBER := NULL;
306 
307 --Variable indicating whether debugging is turned on
308 g_debug                       NUMBER := NULL;
309 
310 --Performance improvement, check whether it is a pick release session
311 g_is_pickrelease              BOOLEAN := FALSE;
312 
313 -- Added to improve performance of check_is_item_reservable procedure
314 g_is_item_reservable          item_reservable_type;
315 
316 -- Bug 5535030: PICK RELEASE PERFORMANCE ISSUES
317 g_is_sub_reservable           sub_reservable_type;
318 
319 --Bug#13108827
320 g_is_loc_reservable           loc_reservable_type;
321 g_is_lot_reservable           lot_reservable_type;
322 
323 --Bug#13734969
324 g_is_onhand_reservable           onhand_reservable_type;
325 
326 -- SRSRIRAN Bug# 4666553
327 -- Added p_level parameter with default as 14
328 PROCEDURE print_debug(p_message IN VARCHAR2, p_level IN NUMBER DEFAULT 14) IS
329 BEGIN
330    IF g_debug IS NULL THEN
331       g_debug :=  NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
332    END IF;
333 
334    IF (g_debug = 1) THEN
335       inv_log_util.trace(p_message, 'INV_QUANTITY_TREE_PVT', p_level);
336    END IF;
337 END;
338 
339 --------------------------------------------------
340 -- Private Procedures and Functions
341 --------------------------------------------------
342 
343 -- Function
344 --   demand_source_equals
345 -- Description
346 --   Compare whether two demand sources are the same.
347 -- Notes
348 --   The following condition is checked for equality
349 --   Point 1 is required, and if point 2 or 3 or 4 is true, the
350 --   answer is yes
351 --   1. p_demand_source_header_id1 and p_demand_source_header_id2
352 --      are equal and both not null
353 --   2. for demand_source_type_id of oe (2) or internal order (8)
354 --      , p_demand_source_header_id and demand_source_header_id are equal
355 ---     both not null, and p_demand_source_line_id and demand_source_line_id
356 --      are equal and both not null and p_demand_source_delivery and
357 --      demand_source_delivery are equals and both not null
358 --      and p_demand_source_name and demand_source_name are equal
359 --      or both null
360 --   or
361 --   3. for demand_source_type_id other than oe and internal order,
362 --      p_demand_source_header_id and demand_source_header_id are both null
363 --      and p_demand_source_name and demand_source_name are equal and both
364 --      not null
365 --   or
366 --   4. for demand_source_type_id other than oe and internal order,
367 --      p_demand_source_header_id and demand_source_header_id are equal
368 --      and both not null and p_demand_source_line_id are equal or both null
369 --      and p_demand_source_name and demand_source_name are equal or both
370 --      null
371 -- Return Value
372 --   'Y' if the two demand sources are the same; 'N' otherwise
373 Function demand_source_equals
374   (  p_demand_source_type_id1   NUMBER
375     ,p_demand_source_header_id1 NUMBER
376     ,p_demand_source_line_id1   NUMBER
377     ,p_demand_source_delivery1  NUMBER
378     ,p_demand_source_name1      VARCHAR2
379     ,p_demand_source_type_id2   NUMBER
380     ,p_demand_source_header_id2 NUMBER
381     ,p_demand_source_line_id2   NUMBER
382     ,p_demand_source_delivery2  NUMBER
383     ,p_demand_source_name2      VARCHAR2
384   ) RETURN VARCHAR2 IS
385      l_true  VARCHAR2(1) := 'Y';
386      l_false VARCHAR2(1) := 'F';
387 BEGIN
388    -- first check source type
389    IF   p_demand_source_type_id1 <> p_demand_source_type_id2
390      OR p_demand_source_type_id1 IS NOT NULL AND p_demand_source_type_id2 IS NULL
391      OR p_demand_source_type_id1 IS NULL     AND p_demand_source_type_id2 IS NOT NULL THEN
392       RETURN l_false;
393    END IF;
394    -- so here we have the same source type
395    -- check the rest
396    IF    p_demand_source_type_id1 IN (2,8)
397      AND p_demand_source_header_id1 = p_demand_source_header_id2
398      AND p_demand_source_line_id1 = p_demand_source_line_id2
399      AND p_demand_source_delivery1 = p_demand_source_delivery2
400      AND (p_demand_source_name1 = p_demand_source_name2
401           OR p_demand_source_name1 IS NULL
402           AND p_demand_source_name2 IS NULL) THEN
403       RETURN l_true;
404    ELSIF p_demand_source_header_id1 IS NULL
405      AND p_demand_source_header_id2 IS NULL
406      AND p_demand_source_name1 = p_demand_source_name2 THEN
407       RETURN l_true;
408    ELSIF p_demand_source_header_id1 = p_demand_source_header_id2
409      AND (p_demand_source_line_id1 = p_demand_source_line_id2
410           OR p_demand_source_line_id1 IS NULL
411           AND p_demand_source_line_id2 IS NULL)
412      AND (p_demand_source_name1 = p_demand_source_name2
413           OR p_demand_source_name1 IS NULL
414           AND p_demand_source_name2 IS NULL) THEN
415       RETURN l_true;
416    END IF;
417    --
418    RETURN l_false;
419 END;
420 --
421 
422 -- Procedure
423 --   print_tree_node
424 -- Description
425 --   print the data in a tree node specified by the input
426 PROCEDURE print_tree_node
427   (p_node_index IN INTEGER
428    ) IS
429    l_node_level         INTEGER;
430    l_node_index         INTEGER;
431 
432    l_found              BOOLEAN;
433    l_root_id            NUMBER;
434    l_return_status      VARCHAR2(1) := fnd_api.g_ret_sts_success;
435    l_reservable         VARCHAR2(10);
436    b_reservable         BOOLEAN;
437    l_status_id          NUMBER;
438    l_zone_control       NUMBER;
439    l_location_control   NUMBER;
440 
441    z_node               VARCHAR2(200);
442    zz_node              VARCHAR2(200);
443    z_reservable         VARCHAR2(200);
444    z_check              VARCHAR2(200);
445    z_lot                VARCHAR2(200);
446    z_sub                VARCHAR2(200);
447    z_loc                VARCHAR2(200);
448    z_rev                VARCHAR2(200);
449    z_qoh                VARCHAR2(200);
450    z_rqoh               VARCHAR2(200);
451    z_qr                 VARCHAR2(200);
452    z_qs                 VARCHAR2(200);
453    z_att                VARCHAR2(200);
454    z_atr                VARCHAR2(200);
455    z_sqr                VARCHAR2(200);
456    z_sqs                VARCHAR2(200);
457    z_satt               VARCHAR2(200);
458    z_satr               VARCHAR2(200);
459    z_qs_adj1            VARCHAR2(200);
460    z_lpn                VARCHAR2(200);
461 BEGIN
462 
463    IF g_debug IS NULL THEN
464       g_debug :=  NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
465    END IF;
466 
467    -- Bug 6332112 : Removed if g_debug and indented the complete procedure
468    l_reservable  := 'Y';
469    IF p_node_index <> 0 THEN
470        l_node_level := g_nodes(p_node_index).node_level;
471 
472        IF l_node_level = g_item_level THEN
473           z_node := rpad('Item', 5);
474           zz_node := 'Item';
475 
476        ELSIF l_node_level = g_revision_level THEN
477           z_node := rpad('Rev', 5);
478           zz_node := 'Revision';
479 
480        ELSIF l_node_level = g_lot_level THEN
481           z_node := rpad('Lot', 5);
482           zz_node := 'Lot';
483 
484        ELSIF l_node_level = g_sub_level THEN
485           z_node := rpad('Sub', 5);
486           zz_node := 'SubInventory';
487 
488        ELSIF l_node_level = g_locator_level THEN
489           z_node := rpad('Loc', 5);
490           zz_node := 'Locator';
491 
492        END IF;   -- l_node_level
493 
494        b_reservable := g_nodes(p_node_index).is_reservable_sub;
495 
496        IF b_reservable THEN
497            l_reservable := ' Y';
498        ELSE
499            l_reservable := ' N';
500        END IF;
501 
502 
503        z_rev        := rpad(NVL(g_nodes(p_node_index).revision, '...'), 5);
504        z_lot        := rpad(NVL(g_nodes(p_node_index).lot_number, '...'), 10);
505        z_sub        := rpad(NVL(g_nodes(p_node_index).subinventory_code, '...'), 10);
506        z_loc        := rpad(NVL(to_char(g_nodes(p_node_index).locator_id), '...'), 7);
507        z_lpn        := rpad(NVL(to_char(g_nodes(p_node_index).lpn_id), '...'), 7);
508        z_qoh        := lpad(to_char(g_nodes(p_node_index).qoh), 7);
509        z_rqoh       := lpad(to_char(g_nodes(p_node_index).rqoh), 7);
510        z_qr         := lpad(to_char(g_nodes(p_node_index).qr), 7);
511        z_qs         := lpad(to_char(g_nodes(p_node_index).qs), 7);
512        z_att        := lpad(to_char(g_nodes(p_node_index).att), 7);
513        z_atr        := lpad(to_char(g_nodes(p_node_index).atr), 7);
514        z_qs_adj1    := lpad(to_char(g_nodes(p_node_index).qs_adj1), 8);
515        z_reservable := rpad( l_reservable, 4);
516 
517        IF g_nodes(p_node_index).check_mark THEN
518           z_check := rpad('TRUE', 6);
519        ELSE
520           z_check := rpad('FALSE', 6);
521        END IF;
522 
523        print_debug('     '||z_node||z_rev||z_lot||z_sub||z_loc||z_lpn||z_qoh||z_rqoh||z_qr||z_qs||z_att||z_atr||z_qs_adj1||z_reservable||z_check,12);
524        print_debug('    -'||RPad('-',105,'-'),12);
525 
526        l_node_index := g_nodes(p_node_index).first_child_index;
527 --
528        WHILE l_node_index <> 0 LOOP
529             print_tree_node(l_node_index);
530             l_node_index := g_nodes(l_node_index).next_sibling_index;
531        END LOOP;
532    END IF;
533 END print_tree_node;
534 
535 PROCEDURE print_tree
536   (p_tree_id IN INTEGER
537    ) IS
538 
539    l_root_id INTEGER;
540 BEGIN
541 
542 --   IF p_tree_id > g_rootinfo_counter THEN
543 --      RETURN;
544 --   END IF;
545    IF g_debug IS NULL THEN
546       g_debug :=  NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
547    END IF;
548 
549    IF (g_debug = 1) THEN
550       l_root_id := g_demand_info(p_tree_id).root_id;
551       print_debug(' ',12);
552       print_debug('  print tree: number='||To_char(p_tree_id)||' id='||l_root_id
553              ||' for item='||g_rootinfos(l_root_id).inventory_item_id||' org='||g_rootinfos(l_root_id).organization_id,12);
554       IF g_rootinfos(l_root_id).organization_id IS NOT NULL THEN
555          print_debug('  _____start of tree '|| To_char(l_root_id),12);
556 
557          print_debug('     '||rpad('Node',5)||rpad('Rev',5)||rpad('Lot',10)||rpad('Sub',10)||rpad('Loc',7)||rpad('Lpn',7)||
558                      lpad('qoh',7)||lpad('rqoh',7)||lpad('qr',7)||lpad('qs',7)||lpad('att',7)||lpad('atr',7)||
559                      lpad('qs_adj1',8)||rpad(' Rsv',4)||rpad(' Marked',7) ,12);
560 
561          print_debug('    -'||RPad('-',105,'-'),12);
562          print_tree_node(g_rootinfos(l_root_id).item_node_index);
563          print_debug('  _____end of tree '||To_char(l_root_id),12);
564        ELSE
565          print_debug ('Error: tree '||To_char(l_root_id) || ' does not exist',12);
566       END IF;
567    END IF;
568 
569 END print_tree;
570 
571 -- Function
572 --  get_ancestor_sub
573 -- Description
574 --  Finds the ancestor sub node for the node passed in to the function
575 -- Returns
576 --  node_index of ancestor sub
577 FUNCTION get_ancestor_sub
578    (p_node_index IN NUMBER) RETURN NUMBER IS
579 
580    l_cur_node NUMBER;
581 BEGIN
582    l_cur_node := p_node_index;
583    LOOP
584       print_debug('in get_ancestor_sub, new loop... cur_node='||l_cur_node||', level='||g_nodes(l_cur_node).node_level);
585       if l_cur_node IS NULL THEN
586          print_debug('in get_ancestor_sub, cur_node=NULL');
587          l_cur_node := -1;
588          EXIT;
589       elsif (g_nodes(l_cur_node).node_level = g_sub_level) THEN
590          print_debug('in get_ancestor_sub, node_level='||g_nodes(l_cur_node).node_level||' = sub_level, parent_index='||g_nodes(l_cur_node).parent_index);
591          EXIT;
592       elsif (g_nodes(l_cur_node).node_level = g_item_level) THEN
593          print_debug('in get_ancestor_sub, node_level='||g_nodes(l_cur_node).node_level||' = item_level, parent_index='||g_nodes(l_cur_node).parent_index);
594          l_cur_node := -1;
595          EXIT;
596       elsif (g_nodes.exists(l_cur_node) = FALSE) THEN
597          print_debug('in get_ancestor_sub, g_nodes.exists= FALSE, node_level='||g_nodes(l_cur_node).node_level);
598          l_cur_node := -1;
599          EXIT;
600       end if;
601       l_cur_node := g_nodes(l_cur_node).parent_index;
602    END LOOP;
603 
604    print_debug('in get_ancestor_sub, returning='||l_cur_node);
605    RETURN l_cur_node;
606 END get_ancestor_sub;
607 
608 -- Procedure
609 --  check_is_reservable_sub
610 -- Description
611 --  check from db tables whether the sub specified in
612 --  the input is a reservable sub or not.
613 -- invConv comment : this procedure check_is_reservable_sub is included in check_is_reservable
614 --                   and used when g_is_mat_status_used =  NO ( == 2)
615 -- end of invConv comment.
616 PROCEDURE check_is_reservable_sub
617   (   x_return_status       OUT NOCOPY VARCHAR2
618     , p_organization_id     IN  VARCHAR2
619     , p_subinventory_code   IN  VARCHAR2
620     , x_is_reservable_sub   OUT NOCOPY BOOLEAN
621       ) IS
622          l_return_status    VARCHAR2(1) := fnd_api.g_ret_sts_success;
623          l_reservable_type  NUMBER := 2;
624          l_hash_value       NUMBER;
625 BEGIN
626    --Bug 4699159. The query is throwing a no data found exception when
627    --p_subinventory_code is being passed as null.
628    IF(p_subinventory_code IS NOT NULL) THEN
629       l_hash_value := DBMS_UTILITY.get_hash_value
630                       ( NAME      => p_organization_id ||'-'|| p_subinventory_code
631                       , base      => 1
632                       , hash_size => POWER(2, 25)
633                       );
634       IF g_is_sub_reservable.EXISTS(l_hash_value) AND
635          g_is_sub_reservable(l_hash_value).org_id = p_organization_id AND
636          g_is_sub_reservable(l_hash_value).subinventory_code = p_subinventory_code
637       THEN
638       print_debug('In check_is_reservable_sub, getting the cache value');
639          l_reservable_type := g_is_sub_reservable(l_hash_value).reservable_type;
640       ELSE
641          SELECT Nvl(reservable_type,1) INTO l_reservable_type
642            FROM mtl_secondary_inventories
643           WHERE organization_id = p_organization_id
644             AND secondary_inventory_name = p_subinventory_code;
645             print_debug('In check_is_reservable_sub, Query fired');
646          g_is_sub_reservable(l_hash_value).reservable_type := l_reservable_type;
647          g_is_sub_reservable(l_hash_value).org_id := p_organization_id;
648          g_is_sub_reservable(l_hash_value).subinventory_code := p_subinventory_code;
649       END IF;
650    END IF;
651    IF (l_reservable_type = 1) THEN
652       x_is_reservable_sub := TRUE;
653    ELSE
654       x_is_reservable_sub := FALSE;
655    END IF;
656 
657    x_return_status := l_return_status;
658 
659 EXCEPTION
660 
661    WHEN OTHERS THEN
662       x_return_status := fnd_api.g_ret_sts_unexp_error ;
663 
664       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
665       THEN
666          fnd_msg_pub.add_exc_msg
667             (  g_pkg_name
668              , 'Check_Is_Reservable_SUB'
669             );
670       END IF;
671 
672 END check_is_reservable_sub;
673 
674 -- Procedure
675 --  check_is_reservable_loc
676 -- Description
677 --  check from db tables whether the loc specified in
678 --  the input is a reservable loc or not.
679 -- invConv comment : this procedure check_is_reservable_loc is included in check_is_reservable
680 --                   and used when g_is_mat_status_used =  NO ( == 2)
681 -- end of invConv comment.
682 PROCEDURE check_is_reservable_loc
683   (   x_return_status       OUT NOCOPY VARCHAR2
684     , p_organization_id     IN  VARCHAR2
685     , p_inventory_location_id   IN  NUMBER
686     , x_is_reservable_loc   OUT NOCOPY BOOLEAN
687       ) IS
688          l_return_status    VARCHAR2(1) := fnd_api.g_ret_sts_success;
689          l_reservable_type  NUMBER := 2;
690          l_hash_value       NUMBER;
691 BEGIN
692    --Bug 4699159. The query is throwing a no data found exception when
693    --p_subinventory_code is being passed as null.
694    IF(p_inventory_location_id IS NOT NULL) THEN
695       l_hash_value := DBMS_UTILITY.get_hash_value
696                       ( NAME      => p_organization_id ||'-'|| p_inventory_location_id
697                       , base      => 1
698                       , hash_size => POWER(2, 25)
699                       );
700       IF g_is_loc_reservable.EXISTS(l_hash_value) AND
701          g_is_loc_reservable(l_hash_value).org_id = p_organization_id AND
702          g_is_loc_reservable(l_hash_value).inventory_location_id = p_inventory_location_id
703       THEN
704       print_debug('In check_is_reservable_loc, getting the cache value');
705          l_reservable_type := g_is_loc_reservable(l_hash_value).reservable_type;
706       ELSE
707          SELECT NVL(reservable_type,1) INTO l_reservable_type
708            FROM mtl_item_locations
709           WHERE organization_id = p_organization_id
710             AND inventory_location_id = p_inventory_location_id;
711             print_debug('In check_is_reservable_loc, Query fired');
712          g_is_loc_reservable(l_hash_value).reservable_type := l_reservable_type;
713          g_is_loc_reservable(l_hash_value).org_id := p_organization_id;
714          g_is_loc_reservable(l_hash_value).inventory_location_id := p_inventory_location_id;
715       END IF;
716    END IF;
717    IF (l_reservable_type = 1) THEN
718       x_is_reservable_loc := TRUE;
719    ELSE
720       x_is_reservable_loc := FALSE;
721    END IF;
722 
723    x_return_status := l_return_status;
724 
725 EXCEPTION
726 
727    WHEN OTHERS THEN
728       x_return_status := fnd_api.g_ret_sts_unexp_error ;
729 
730       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
731       THEN
732          fnd_msg_pub.add_exc_msg
733             (  g_pkg_name
734              , 'Check_Is_Reservable_LOC'
735             );
736       END IF;
737 
738 END check_is_reservable_loc;
739 
740 -- Procedure
741 --  check_is_reservable_lot
742 -- Description
743 --  check from db tables whether the sub specified in
744 --  the input is a reservable lot or not.
745 -- invConv comment : this procedure check_is_reservable_lot is included in check_is_reservable
746 --                   and used when g_is_mat_status_used =  NO ( == 2)
747 -- end of invConv comment.
748 PROCEDURE check_is_reservable_lot
749   (   x_return_status       OUT NOCOPY VARCHAR2
750     , p_organization_id     IN  VARCHAR2
751     , p_inventory_item_id   IN NUMBER
752     , p_lot_number          IN  VARCHAR2
753     , x_is_reservable_lot   OUT NOCOPY BOOLEAN
754       ) IS
755          l_return_status    VARCHAR2(1) := fnd_api.g_ret_sts_success;
756          l_reservable_type  NUMBER := 2;
757          l_hash_value       NUMBER;
758 BEGIN
759    --Bug 4699159. The query is throwing a no data found exception when
760    --p_lot_number is being passed as null.
761    IF(p_lot_number IS NOT NULL) THEN
762       l_hash_value := DBMS_UTILITY.get_hash_value
763                       ( NAME      => p_organization_id ||'-'|| p_inventory_item_id ||'-'|| p_lot_number
764                       , base      => 1
765                       , hash_size => POWER(2, 25)
766                       );
767       IF g_is_lot_reservable.EXISTS(l_hash_value) AND
768          g_is_lot_reservable(l_hash_value).org_id = p_organization_id AND
769          g_is_lot_reservable(l_hash_value).inventory_item_id = p_inventory_item_id AND
770          g_is_lot_reservable(l_hash_value).lot_number = p_lot_number
771       THEN
772       print_debug('In check_is_reservable_lot, getting the cache value');
773          l_reservable_type := g_is_lot_reservable(l_hash_value).reservable_type;
774       ELSE
775          SELECT NVL(reservable_type,1) INTO l_reservable_type
776            FROM mtl_lot_numbers
777           WHERE inventory_item_id = p_inventory_item_id
778 	    AND organization_id = p_organization_id
779             AND lot_number = p_lot_number;
780             print_debug('In check_is_reservable_lot, Query fired');
781          g_is_lot_reservable(l_hash_value).reservable_type := l_reservable_type;
782          g_is_lot_reservable(l_hash_value).org_id := p_organization_id;
783          g_is_lot_reservable(l_hash_value).inventory_item_id := p_inventory_item_id;
784          g_is_lot_reservable(l_hash_value).lot_number := p_lot_number;
785       END IF;
786    END IF;
787    IF (l_reservable_type = 1) THEN
788       x_is_reservable_lot := TRUE;
789    ELSE
790       x_is_reservable_lot := FALSE;
791    END IF;
792 
793    x_return_status := l_return_status;
794 
795 EXCEPTION
796 
797    WHEN OTHERS THEN
798       x_return_status := fnd_api.g_ret_sts_unexp_error ;
799 
800       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
801       THEN
802          fnd_msg_pub.add_exc_msg
803             (  g_pkg_name
804              , 'Check_Is_Reservable_SUB'
805             );
806       END IF;
807 
808 END check_is_reservable_lot;
809 
810 
811 
812 -- Bug 13734969
813 -- Procedure
814 --  check_is_reservable_onhand
815 -- Description
816 --  check from db tables whether the onhand record specified in
817 --  the input is reservable or not.
818 -- invConv comment : this procedure check_is_reservable_onhand is included in check_is_reservable
819 --                   and used when g_is_mat_status_used =  YES ( == 1)
820 -- end of invConv comment.
821 PROCEDURE check_is_reservable_onhand
822   (   x_return_status       OUT NOCOPY VARCHAR2
823     , p_organization_id     IN VARCHAR2
824     , p_inventory_item_id   IN NUMBER
825     , p_lot_number          IN VARCHAR2
826     , p_sub_code            IN VARCHAR2
827     , p_loc_id              IN NUMBER
828     , p_lpn_id              IN NUMBER
829     , x_is_reservable_onhand   OUT NOCOPY BOOLEAN
830       ) IS
831          l_return_status    VARCHAR2(1) := fnd_api.g_ret_sts_success;
832          l_reservable_type  NUMBER := 1;
833          l_hash_value       NUMBER;
834          l_hash_string      VARCHAR2(1000);
835 BEGIN
836    --Bug 4699159. The query is throwing a no data found exception when
837    --p_lot_number is being passed as null.
838 
839    l_hash_string := p_organization_id || '-' || p_inventory_item_id;
840 
841    if (p_lot_number is not null) then
842       l_hash_string := l_hash_string || '-' || p_lot_number;
843    end if;
844 
845    if (p_sub_code is not null) then
846       l_hash_string := l_hash_string || '-' || p_sub_code;
847    end if;
848 
849    if (p_loc_id is not null) then
850       l_hash_string := l_hash_string || '-' || p_loc_id;
851    end if;
852 
853    if (p_lpn_id is not null) then
854       l_hash_string := l_hash_string || '-' || p_lpn_id;
855    end if;
856 
857    print_debug('In check_is_reservable_onhand, hash string built: '||l_hash_string);
858 
859    l_hash_value := DBMS_UTILITY.get_hash_value
860                    ( NAME      => l_hash_string
861                    , base      => 1
862                    , hash_size => POWER(2, 25)
863                    );
864    IF g_is_onhand_reservable.EXISTS(l_hash_value) AND
865        g_is_onhand_reservable(l_hash_value).org_id = p_organization_id AND
866        g_is_onhand_reservable(l_hash_value).inventory_item_id = p_inventory_item_id AND
867        nvl(g_is_onhand_reservable(l_hash_value).lot_number, '@@@@') = nvl(p_lot_number, '@@@@') AND
868        nvl(g_is_onhand_reservable(l_hash_value).subinventory_code, '@@@@') = nvl(p_sub_code, '@@@@') AND
869        nvl(g_is_onhand_reservable(l_hash_value).locator_id, -9999) = nvl(p_loc_id, -9999) AND
870        nvl(g_is_onhand_reservable(l_hash_value).lpn_id, -9999) = nvl(p_lpn_id, -9999)
871    THEN
872       print_debug('In check_is_reservable_onhand, getting the cache value');
873       l_reservable_type := g_is_onhand_reservable(l_hash_value).reservable_type;
874    ELSE
875 
876     BEGIN
877 
878       SELECT NVL(mms.reservable_type, 1) INTO l_reservable_type
879       FROM mtl_onhand_quantities_detail moqd, mtl_material_statuses mms
880       WHERE moqd.status_id = mms.status_id
881       AND moqd.status_id is not null
882       AND moqd.inventory_item_id = p_inventory_item_id
883       AND moqd.organization_id = p_organization_id
884       AND moqd.subinventory_code = p_sub_code
885       AND nvl(moqd.locator_id, -9999) = nvl(p_loc_id, -9999)
886       AND nvl(moqd.lot_number, '@@@@') = nvl(p_lot_number, '@@@@')
887       AND nvl(moqd.lpn_id, -9999) = nvl(p_lpn_id, -9999)
888       AND rownum = 1;
889 
890     EXCEPTION WHEN NO_DATA_FOUND THEN
891        l_reservable_type := 1;
892     END;
893 
894       print_debug('In check_is_reservable_onhand, Query fired');
895       g_is_onhand_reservable(l_hash_value).reservable_type := l_reservable_type;
896       g_is_onhand_reservable(l_hash_value).org_id := p_organization_id;
897       g_is_onhand_reservable(l_hash_value).inventory_item_id := p_inventory_item_id;
898       g_is_onhand_reservable(l_hash_value).lot_number := p_lot_number;
899       g_is_onhand_reservable(l_hash_value).subinventory_code := p_sub_code;
900       g_is_onhand_reservable(l_hash_value).locator_id := p_loc_id;
901       g_is_onhand_reservable(l_hash_value).lpn_id := p_lpn_id;
902 
903    END IF;
904 
905    IF (l_reservable_type = 1) THEN
906       x_is_reservable_onhand := TRUE;
907    ELSE
908       x_is_reservable_onhand := FALSE;
909    END IF;
910 
911    x_return_status := l_return_status;
912 
913 EXCEPTION
914 
915    WHEN OTHERS THEN
916       x_return_status := fnd_api.g_ret_sts_unexp_error ;
917 
918       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
919       THEN
920          fnd_msg_pub.add_exc_msg
921             (  g_pkg_name
922              , 'Check_Is_Reservable_onhand'
923             );
924       END IF;
925 
926 END check_is_reservable_onhand;
927 
928 
929 
930 --Bug 3424532 fix
931 -- Procedure
932 --  check_is_reservable_item
933 -- Description
934 --  check from db tables whether the item specified in
935 --  the input is a reservable or not.
936 PROCEDURE check_is_reservable_item
937   (   x_return_status       OUT NOCOPY VARCHAR2
938     , p_organization_id     IN  NUMBER
939     , p_inventory_item_id   IN  NUMBER
940     , x_is_reservable_item  OUT NOCOPY BOOLEAN
941       ) IS
942          l_return_status    VARCHAR2(1) := fnd_api.g_ret_sts_success;
943          l_reservable_type  NUMBER;
944          l_no_info          BOOLEAN := FALSE;
945 BEGIN
946    IF NOT g_is_item_reservable.exists(p_inventory_item_id) THEN
947       l_no_info := TRUE;
948    ELSE
949       IF g_is_item_reservable(p_inventory_item_id).org_id <> p_organization_id THEN
950         l_no_info := TRUE;
951       END IF;
952    END IF;
953 
954    IF l_no_info THEN
955       SELECT Nvl(reservable_type,2) INTO l_reservable_type
956         FROM mtl_system_items
957         WHERE organization_id = p_organization_id
958         AND inventory_item_id = p_inventory_item_id;
959       g_is_item_reservable(p_inventory_item_id).org_id := p_organization_id;
960       IF (l_reservable_type = 1) THEN
961          g_is_item_reservable(p_inventory_item_id).value := TRUE;
962        ELSE
963          g_is_item_reservable(p_inventory_item_id).value := FALSE;
964       END IF;
965    END IF;
966 
967    x_is_reservable_item := g_is_item_reservable(p_inventory_item_id).value;
968 
969    x_return_status := l_return_status;
970 
971 EXCEPTION
972 
973      WHEN OTHERS THEN
974         x_return_status := fnd_api.g_ret_sts_unexp_error ;
975 
976         IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
977           THEN
978            fnd_msg_pub.add_exc_msg
979              (  g_pkg_name
980               , 'Check_Is_Reservable_item'
981               );
982         END IF;
983 
984 END check_is_reservable_item;
985 
986 -- Procedure
987 --  check_is_reservable
988 -- Description
989 --  check from db tables whether the sub specified in
990 --  the input is a reservable sub or not.
991 PROCEDURE check_is_reservable
992   (   x_return_status        OUT NOCOPY VARCHAR2
993     , p_node_level           IN  INTEGER    DEFAULT NULL
994     , p_inventory_item_id    IN  NUMBER
995     , p_organization_id      IN  NUMBER
996     , p_subinventory_code    IN  VARCHAR2
997     , p_locator_id           IN  NUMBER
998     , p_lot_number           IN  VARCHAR2
999     , p_root_id              IN  NUMBER
1000     , x_is_reservable        OUT NOCOPY BOOLEAN
1001     , p_lpn_id               IN  NUMBER     DEFAULT NULL -- Onhand Material Status Support
1002       ) IS
1003 
1004 l_return_status    VARCHAR2(1) := fnd_api.g_ret_sts_success;
1005 l_reservable_type  NUMBER;
1006 l_default_onhand_status_id   NUMBER; -- Onhand Material Status Support
1007 
1008 /* Bug 6719389 */
1009 l_reservable_type_sub  BOOLEAN := TRUE;
1010 l_reservable_type_loc  BOOLEAN := TRUE;
1011 l_reservable_type_lot  BOOLEAN := TRUE;
1012 
1013 /* Bug 13734969 */
1014 l_reservable_type_onhand  BOOLEAN := TRUE;
1015 
1016 CURSOR is_RSV_subInv( org_id IN NUMBER
1017                     , subinv IN VARCHAR2) IS
1018    SELECT NVL(msinv.reservable_type, 1)
1019    FROM mtl_secondary_inventories msinv
1020    WHERE msinv.organization_id = org_id
1021    AND msinv.secondary_inventory_name = subinv;
1022 
1023 
1024 CURSOR is_RSV_loct( org_id IN NUMBER
1025                   , loct_id IN NUMBER) IS
1026    SELECT NVL(mil.reservable_type, 1)
1027    FROM mtl_item_locations mil
1028    WHERE mil.organization_id = org_id
1029    AND mil.inventory_location_id = loct_id;
1030 
1031 
1032 CURSOR is_RSV_lot( org_id IN NUMBER
1033                  , item_id IN NUMBER
1034                  , lot IN VARCHAR2) IS
1035    SELECT NVL(mln.reservable_type, 1)
1036    FROM mtl_lot_numbers mln
1037    WHERE mln.inventory_item_id = item_id
1038    AND mln.organization_id = org_id
1039    AND mln.lot_number = lot;
1040 
1041 
1042 /* ONHAND MATERIAL STATUS SUPPORT */
1043 /*Bug#13734969 Commented the is_RSV_onhand cursor */
1044 /*CURSOR is_RSV_onhand( org_id IN NUMBER
1045                    , item_id IN NUMBER
1046                    , subinv  IN VARCHAR2
1047                    , loct_id IN NUMBER
1048                    , lot     IN VARCHAR2
1049                    , p_lpn_id  IN NUMBER) IS
1050    SELECT NVL(mms.reservable_type, 1)
1051    FROM mtl_onhand_quantities_detail moqd, mtl_material_statuses mms
1052    WHERE moqd.status_id = mms.status_id
1053    and moqd.status_id is not null
1054    AND moqd.inventory_item_id = item_id
1055    AND moqd.organization_id = org_id
1056    AND moqd.subinventory_code = subinv
1057    and nvl(moqd.locator_id, -9999) = nvl(loct_id, -9999)
1058    AND nvl(moqd.lot_number, '@@@@') = nvl(lot, '@@@@')
1059    AND nvl(moqd.lpn_id, -9999) = nvl(p_lpn_id, -9999)
1060    AND rownum = 1;*/
1061 
1062 BEGIN
1063    print_debug('In check_is_reservable. node_level='||p_node_level||', subinv='
1064       ||p_subinventory_code||', loct='||p_locator_id||', lot='||p_lot_number);
1065 
1066    -- item
1067    IF ( NVL(p_node_level,0) = g_item_level ) THEN
1068        check_is_reservable_item
1069        (  x_return_status      => x_return_status
1070         , p_organization_id    => p_organization_id
1071         , p_inventory_item_id  => p_inventory_item_id
1072         , x_is_reservable_item => x_is_reservable);
1073 
1074    -- revision
1075    ELSIF ( NVL(p_node_level,0) = g_revision_level) THEN
1076       x_is_reservable := g_nodes(g_rootinfos(p_root_id).item_node_index).is_reservable_sub;
1077       x_return_status := l_return_status;
1078 
1079    ELSE
1080 
1081        IF (inv_quantity_tree_pvt.g_is_mat_status_used = 2) THEN
1082           --Bug 4587505. The value returned from here is used to set the is_reservable_sub property of the node
1083           --and for any node that is higher than subinventory(for example LOT node), the is_reservable_sub should
1084           --be set to TRUE, when INV_MATERIAL_STATUS profile is set to 2
1085           IF(p_subinventory_code IS NULL) THEN
1086              --bug 9380420
1087              print_debug('....p_subinventory_code is null.setting x_is_reservable to based on items reservable flag.');
1088              x_is_reservable := g_nodes(g_rootinfos(p_root_id).item_node_index).is_reservable_sub;
1089              x_return_status := l_return_status;
1090           ELSE
1091              print_debug('.... check_is_reservable is calling check_is_reservable_sub...');
1092              check_is_reservable_sub( x_return_status       => x_return_status
1093                                     , p_organization_id     => p_organization_id
1094                                     , p_subinventory_code   => p_subinventory_code
1095                                     , x_is_reservable_sub   => x_is_reservable);
1096           END IF;
1097 
1098        ELSE
1099 
1100           IF ( g_rootinfos(p_root_id).onhand_status_enabled = FALSE ) THEN -- Onhand Material Status Support
1101              print_debug('in check_is_rsv, default org status id is NULL for org id = '||p_organization_id);
1102 
1103              /*Bug #6719389 Changes done for honouring material status */
1104              /*IF (g_rootinfos(p_root_id).is_lot_status_enabled = FALSE)
1105              THEN
1106                 -- Always reservable...
1107                 l_reservable_type := 1;
1108              */
1109 
1110              IF (p_lot_number IS NOT NULL) THEN
1111                 IF (g_rootinfos(p_root_id).is_lot_status_enabled = FALSE) THEN
1112                    l_reservable_type_lot := TRUE;
1113                 ELSE
1114 		   /*Bug#13108827 Commented the cursors and added the below function */
1115 		    print_debug('.... check_is_reservable is calling check_is_reservable_lot...');
1116 		    check_is_reservable_lot( x_return_status       => x_return_status
1117                                     , p_organization_id     => p_organization_id
1118                                     , p_inventory_item_id     => p_inventory_item_id
1119                                     , p_lot_number   => p_lot_number
1120                                     , x_is_reservable_lot   => l_reservable_type_lot);
1121         IF (l_reservable_type_lot) THEN
1122        		      print_debug('l_reservable_type_lot is TRUE');
1123 		    ELSE
1124 		     print_debug('l_reservable_type_lot is FALSE');
1125 		    END IF;
1126 
1127                 /*   OPEN is_RSV_lot( p_organization_id, p_inventory_item_id, p_lot_number);
1128                    FETCH is_RSV_lot
1129                    INTO l_reservable_type_lot;
1130                    IF (is_RSV_lot%NOTFOUND) THEN
1131                       -- bug 3977807 : By default, the reservable flag is set to 1= reservable.
1132                       l_reservable_type_lot := 1;
1133                    END IF;
1134                    CLOSE is_RSV_lot;
1135 		   print_debug('New in RSV reservable='||l_reservable_type_lot||', for lot='||p_lot_number); */
1136 
1137                 END IF;
1138              END IF;
1139 
1140              IF (p_subinventory_code IS NOT NULL) THEN
1141 	       /*Bug#13108827 Commented the cursors and added the below function */
1142 	        print_debug('.... check_is_reservable is calling check_is_reservable_sub...');
1143 	          check_is_reservable_sub( x_return_status       => x_return_status
1144                                     , p_organization_id     => p_organization_id
1145                                     , p_subinventory_code   => p_subinventory_code
1146                                     , x_is_reservable_sub   => l_reservable_type_sub);
1147                     IF (l_reservable_type_sub)    THEN
1148        		      print_debug('l_reservable_type_sub is TRUE');
1149 		    ELSE
1150 		     print_debug('l_reservable_type_sub is FALSE');
1151 		    END IF;
1152 
1153                /* OPEN is_RSV_subInv( p_organization_id, p_subinventory_code);
1154                 FETCH is_RSV_subInv
1155                 INTO l_reservable_type_sub;
1156                 IF (is_RSV_subInv%NOTFOUND) THEN
1157                    -- bug 3977807 : By default, the reservable flag is set to 1= reservable.
1158                    l_reservable_type_sub := 1;
1159                 END IF;
1160                 CLOSE is_RSV_subInv;
1161                 print_debug('New in RSV reservable='||l_reservable_type_sub||', for subInv='||p_subinventory_code); */
1162              END IF;
1163 
1164              IF (p_locator_id IS NOT NULL) THEN
1165 	        /*Bug#13108827 Commented the cursors and added the below function */
1166  	        print_debug('.... check_is_reservable is calling check_is_reservable_loc...');
1167                  check_is_reservable_loc( x_return_status       => x_return_status
1168                                     , p_organization_id     => p_organization_id
1169                                     , p_inventory_location_id   => p_locator_id
1170                                     , x_is_reservable_loc   => l_reservable_type_loc);
1171                     IF (l_reservable_type_loc)    THEN
1172        		      print_debug('l_reservable_type_loc is TRUE');
1173 		    ELSE
1174 		     print_debug('l_reservable_type_loc is FALSE');
1175 		    END IF;
1176 
1177                /* OPEN is_RSV_loct( p_organization_id, p_locator_id);
1178                 FETCH is_RSV_loct
1179                 INTO l_reservable_type_loc;
1180                 IF (is_RSV_loct%NOTFOUND) THEN
1181                    -- bug 3977807 : By default, the reservable flag is set to 1= reservable.
1182                    l_reservable_type_loc := 1;
1183                 END IF;
1184                 CLOSE is_RSV_loct;
1185                 print_debug('New in RSV reservable='||l_reservable_type_loc||', for locator='||p_locator_id); */
1186              END IF;
1187 
1188              IF (l_reservable_type_sub and l_reservable_type_loc and l_reservable_type_lot ) THEN
1189                 l_reservable_type := 1;
1190                 print_debug('New in RSV reservable=TRUE');
1191              ELSE
1192                 l_reservable_type := 2;
1193                 print_debug('New in RSV reservable=FALSE');
1194              END IF;
1195           ELSE
1196              print_debug('in check_is_rsv, default org status id is NOT NULL for org id = '||p_organization_id);
1197              /*Bug#13734969 Commented the cursors and added the below function */
1198 
1199  	        print_debug('.... check_is_reservable is calling check_is_reservable_onhand...');
1200                  check_is_reservable_onhand( x_return_status   => x_return_status
1201                                     , p_organization_id        => p_organization_id
1202                                     , p_inventory_item_id      => p_inventory_item_id
1203                                     , p_lot_number             => p_lot_number
1204                                     , p_sub_code               => p_subinventory_code
1205                                     , p_loc_id                 => p_locator_id
1206                                     , p_lpn_id                 => p_lpn_id
1207                                     , x_is_reservable_onhand   => l_reservable_type_onhand);
1208 
1209 
1210 
1211              IF (l_reservable_type_onhand ) THEN
1212                 l_reservable_type := 1;
1213                 print_debug('New in RSV reservable=TRUE for onhand record');
1214              ELSE
1215                 l_reservable_type := 2;
1216                 print_debug('New in RSV reservable=FALSE for onhand record');
1217              END IF;
1218 
1219              /*
1220              OPEN is_RSV_onhand( p_organization_id, p_inventory_item_id, p_subinventory_code, p_locator_id, p_lot_number, p_lpn_id);
1221              FETCH is_RSV_onhand
1222                 INTO l_reservable_type;
1223              IF (is_RSV_onhand%NOTFOUND) THEN
1224                 -- bug 3977807 : By default, the reservable flag is set to 1= reservable.
1225                 l_reservable_type := 1;
1226                 print_debug('in RSV reservable, onhand record not found');
1227              END IF;
1228              CLOSE is_RSV_onhand;
1229              print_debug('in RSV reservable='||l_reservable_type||', for onhand record');
1230              */
1231 
1232           END IF; -- Onhand Material Status Support End
1233 
1234           /* Bug 6719389 */
1235           IF l_reservable_type = 1 THEN
1236              x_is_reservable := TRUE;
1237              print_debug('in RSV reservable=TRUE');
1238           ELSE
1239              x_is_reservable := FALSE;
1240              print_debug('in RSV reservable=FALSE');
1241           END IF;
1242 
1243           x_return_status := l_return_status;
1244        END IF; --- g_is_mat_status_used.
1245     END IF;
1246 
1247 EXCEPTION
1248 
1249    WHEN OTHERS THEN
1250       print_debug('in check_is_reservable, OTHERS Error='||SQLERRM, 9);
1251       x_return_status := fnd_api.g_ret_sts_unexp_error ;
1252 
1253       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
1254       THEN
1255          fnd_msg_pub.add_exc_msg
1256              (  g_pkg_name
1257               , 'Check_Is_Reservable'
1258               );
1259       END IF;
1260 
1261 END check_is_reservable;
1262 
1263 PROCEDURE zero_tree_node
1264   (x_return_status   OUT NOCOPY VARCHAR2
1265    , p_node_index    IN  INTEGER
1266    ) IS
1267    l_return_status     VARCHAR2(1) := fnd_api.g_ret_sts_success;
1268    l_node_index        INTEGER;
1269 BEGIN
1270    l_node_index := p_node_index;
1271    IF l_node_index <>0 THEN
1272       g_nodes(l_node_index).qoh   := 0;
1273       g_nodes(l_node_index).rqoh  := 0;
1274       g_nodes(l_node_index).qs    := 0;
1275       g_nodes(l_node_index).qr    := 0;
1276       g_nodes(l_node_index).atr   := 0;
1277       g_nodes(l_node_index).att   := 0;
1278       g_nodes(l_node_index).pqoh  := 0;
1279       g_nodes(l_node_index).rpqoh := 0;    -- Bug 9644285
1280 
1281       g_nodes(l_node_index).sqoh  := 0;
1282       g_nodes(l_node_index).srqoh := 0;
1283       g_nodes(l_node_index).sqs   := 0;
1284       g_nodes(l_node_index).sqr   := 0;
1285       g_nodes(l_node_index).satr  := 0;
1286       g_nodes(l_node_index).satt  := 0;
1287       g_nodes(l_node_index).spqoh := 0;
1288       g_nodes(l_node_index).srpqoh := 0;    -- Bug 9644285
1289 
1290       g_nodes(l_node_index).qs_adj1  := 0; -- Bug 8919216
1291       g_nodes(l_node_index).sqs_adj1  := 0; -- Bug 8919216
1292 
1293       l_node_index := g_nodes(l_node_index).first_child_index;
1294       WHILE l_node_index<>0 LOOP
1295          zero_tree_node(l_return_status, l_node_index);
1296 
1297          IF l_return_status = fnd_api.g_ret_sts_error THEN
1298             RAISE fnd_api.g_exc_error;
1299          End IF ;
1300 
1301          IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
1302             RAISE fnd_api.g_exc_unexpected_error;
1303          End IF;
1304 
1305          l_node_index := g_nodes(l_node_index).next_sibling_index;
1306       END LOOP;
1307    END IF;
1308    x_return_status := l_return_status;
1309 
1310 EXCEPTION
1311    WHEN fnd_api.g_exc_error THEN
1312         x_return_status := fnd_api.g_ret_sts_error;
1313 
1314    WHEN fnd_api.g_exc_unexpected_error THEN
1315         x_return_status := fnd_api.g_ret_sts_unexp_error ;
1316 
1317    WHEN OTHERS THEN
1318       x_return_status := fnd_api.g_ret_sts_unexp_error ;
1319 
1320       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
1321         THEN
1322          fnd_msg_pub.add_exc_msg
1323            (  g_pkg_name
1324               , 'Zero_Tree_Node'
1325               );
1326       END IF;
1327 END zero_tree_node;
1328 
1329 
1330 -- Procedure
1331 --   new_tree_node
1332 -- Description
1333 --   create a new tree node and initialize the all attributes.
1334 --   This is the constrcutor for all kinds of tree nodes:
1335 --   item, revision, lot, sub, and locator. The actual type of the
1336 --   node is qualified by input p_node_level. The pointers to
1337 --   and from the parent node and sibling nodes are also set up here.
1338 PROCEDURE new_tree_node
1339   (   x_return_status       OUT NOCOPY VARCHAR2
1340    ,  p_node_level          IN  INTEGER
1341    ,  p_tree_id             IN  INTEGER
1342    ,  p_revision            IN  VARCHAR2 DEFAULT NULL
1343    ,  p_lot_number          IN  VARCHAR2 DEFAULT NULL
1344    ,  p_subinventory_code   IN  VARCHAR2 DEFAULT NULL
1345    ,  p_is_reservable_sub   IN  BOOLEAN  DEFAULT NULL
1346    ,  p_locator_id          IN  NUMBER   DEFAULT NULL
1347    ,  p_lpn_id              IN  NUMBER   DEFAULT NULL
1348    ,  p_cost_group_id       IN  NUMBER   DEFAULT NULL
1349    ,  p_node_index          IN  INTEGER
1350    ,  p_hash_string         IN  VARCHAR2 DEFAULT NULL
1351    ) IS
1352       l_return_status       VARCHAR2(1) := fnd_api.g_ret_sts_success;
1353       l_new_node_index      INTEGER;
1354       l_org_id              NUMBER;
1355       l_parent_index        INTEGER;
1356       l_last_child_index    INTEGER;
1357       l_is_reservable_sub   BOOLEAN;
1358 BEGIN
1359    print_debug('      Creating node at level '|| p_node_level || ' with ' || NVL(p_hash_string,p_tree_id||'::::::'), 12);
1360    -- increment the index for the array
1361    l_new_node_index := p_node_index;
1362 
1363    --initialize the node
1364    g_nodes(l_new_node_index).node_level         := p_node_level;
1365    g_nodes(l_new_node_index).revision           := p_revision;
1366    g_nodes(l_new_node_index).lot_number         := p_lot_number;
1367    g_nodes(l_new_node_index).subinventory_code  := p_subinventory_code;
1368    g_nodes(l_new_node_index).locator_id         := p_locator_id;
1369    g_nodes(l_new_node_index).qoh                := 0;
1370    g_nodes(l_new_node_index).rqoh               := 0;
1371    g_nodes(l_new_node_index).qr                 := 0;
1372    g_nodes(l_new_node_index).qs                 := 0;
1373    g_nodes(l_new_node_index).att                := 0;
1374    g_nodes(l_new_node_index).atr                := 0;
1375    g_nodes(l_new_node_index).pqoh               := 0;
1376    g_nodes(l_new_node_index).rpqoh              := 0;       -- Bug 9644285
1377 
1378    g_nodes(l_new_node_index).sqoh               := 0;
1379    g_nodes(l_new_node_index).srqoh              := 0;
1380    g_nodes(l_new_node_index).sqr                := 0;
1381    g_nodes(l_new_node_index).sqs                := 0;
1382    g_nodes(l_new_node_index).satt               := 0;
1383    g_nodes(l_new_node_index).satr               := 0;
1384    g_nodes(l_new_node_index).spqoh              := 0;
1385    g_nodes(l_new_node_index).srpqoh             := 0;       -- Bug 9644285
1386 
1387    g_nodes(l_new_node_index).next_sibling_index := 0;
1388    g_nodes(l_new_node_index).first_child_index  := 0;
1389    g_nodes(l_new_node_index).last_child_index   := 0;
1390    g_nodes(l_new_node_index).parent_index       := 0;
1391    g_nodes(l_new_node_index).check_mark         := FALSE;
1392    g_nodes(l_new_node_index).lpn_id             := p_lpn_id;
1393    g_nodes(l_new_node_index).cost_group_id      := p_cost_group_id;
1394    g_nodes(l_new_node_index).hash_string        := p_hash_string;
1395    g_nodes(l_new_node_index).next_hash_record   := 0;
1396    --Bug 4294336
1397    g_nodes(l_new_node_index).qs_adj1            := 0;
1398    g_nodes(l_new_node_index).sqs_adj1           := 0;
1399 
1400    --bug 9380420, set the is_reservable_sub while creating the node.
1401    --       All the calls to check_is_reservable should be replaced
1402    --       by checking the node's is_reservable_sub flag
1403    --Bug 9131745: passing p_node_level
1404    IF p_is_reservable_sub IS NULL THEN
1405       check_is_reservable
1406         ( x_return_status       => l_return_status
1407         , p_node_level          => p_node_level
1408         , p_inventory_item_id   => g_rootinfos(p_tree_id).inventory_item_id
1409         , p_organization_id     => g_rootinfos(p_tree_id).organization_id
1410         , p_subinventory_code   => p_subinventory_code
1411         , p_locator_id          => p_locator_id
1412         , p_lot_number          => p_lot_number
1413         , p_root_id             => p_tree_id
1414         , x_is_reservable       => l_is_reservable_sub
1415         , p_lpn_id              => p_lpn_id); -- Onhand Material Status Support
1416 
1417       IF l_return_status = fnd_api.g_ret_sts_error THEN
1418          RAISE fnd_api.g_exc_error;
1419       End IF ;
1420 
1421       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
1422          RAISE fnd_api.g_exc_unexpected_error;
1423       End IF;
1424 
1425 
1426       g_nodes(l_new_node_index).is_reservable_sub := l_is_reservable_sub;
1427    ELSE
1428       g_nodes(l_new_node_index).is_reservable_sub := p_is_reservable_sub;
1429    END IF;
1430 
1431    x_return_status := l_return_status;
1432    print_debug('... normal end of new_tree_node');
1433 
1434 EXCEPTION
1435 
1436    WHEN OTHERS THEN
1437       x_return_status := fnd_api.g_ret_sts_unexp_error ;
1438 
1439       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
1440         THEN
1441          fnd_msg_pub.add_exc_msg
1442            (  g_pkg_name
1443               , 'New_Tree_Node'
1444               );
1445       END IF;
1446 
1447 END new_tree_node;
1448 
1449 
1450 FUNCTION build_hash_string (
1451       x_return_status             OUT NOCOPY VARCHAR2
1452    ,  p_organization_id           IN  NUMBER
1453    ,  p_inventory_item_id         IN  NUMBER
1454    ,  p_tree_mode                 IN  INTEGER
1455    ,  p_is_revision_control       IN  BOOLEAN
1456    ,  p_is_lot_control            IN  BOOLEAN
1457    ,  p_asset_sub_only            IN  BOOLEAN
1458    ,  p_demand_source_type_id     IN  NUMBER
1459    ,  p_demand_source_header_id   IN  NUMBER
1460    ,  p_demand_source_line_id     IN  NUMBER
1461    ,  p_demand_source_name        IN  VARCHAR2
1462    ,  p_lot_expiration_date       IN  DATE
1463    ,  p_onhand_source        IN  NUMBER
1464    ) RETURN VARCHAR2 IS
1465 
1466    l_hash_string VARCHAR2(1000);
1467    l_pjm_enabled VARCHAR2(1);
1468 
1469 BEGIN
1470 
1471    x_return_status := fnd_api.g_ret_sts_success;
1472 
1473    -- Bug 1918356
1474    -- If PJM unit effectivity is enabled, we need to create
1475    -- a new tree for each different sales order line id
1476    -- Bug 2363739 - now call unit_effective_item
1477    l_pjm_enabled := pjm_unit_eff.unit_effective_item(
1478         x_item_id          => p_inventory_item_id
1479       , x_organization_id  => p_organization_id);
1480    IF l_pjm_enabled IS NULL THEN
1481      l_pjm_enabled := 'N';
1482    END IF;
1483    l_hash_string := p_organization_id || ':' || p_inventory_item_id;
1484    IF p_tree_mode IN (1,2) AND l_pjm_enabled <> 'Y' THEN
1485       l_hash_string := l_hash_string || ':1';
1486       -- add place holders for demand info
1487       l_hash_string := l_hash_string || '::::';
1488    ELSE
1489       l_hash_string := l_hash_string || ':' || p_tree_mode;
1490       l_hash_string := l_hash_string
1491         || ':' || p_demand_source_type_id
1492         || ':' || p_demand_source_header_id
1493         || ':' || p_demand_source_line_id
1494         || ':' || p_demand_source_name;
1495    END IF;
1496 
1497    IF p_is_revision_control THEN
1498       l_hash_string := l_hash_string || ':2';
1499    ELSE
1500       l_hash_string := l_hash_string || ':1';
1501    END IF;
1502 
1503    IF p_is_lot_control THEN
1504       l_hash_string := l_hash_string || ':2';
1505    ELSE
1506       l_hash_string := l_hash_string || ':1';
1507    END IF;
1508 
1509    IF p_asset_sub_only THEN
1510       l_hash_string := l_hash_string || ':2';
1511    ELSE
1512       l_hash_string := l_hash_string || ':1';
1513    END IF;
1514 
1515    IF p_lot_expiration_date IS NOT NULL THEN
1516       l_hash_string := l_hash_string || ':' || p_lot_expiration_date;
1517    END IF;
1518 
1519    l_hash_string := l_hash_string || ':' || p_onhand_source;
1520 
1521    RETURN l_hash_string;
1522 EXCEPTION
1523    WHEN OTHERS THEN
1524       x_return_status := fnd_api.g_ret_sts_unexp_error ;
1525 
1526       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
1527         THEN
1528          fnd_msg_pub.add_exc_msg
1529            (  g_pkg_name
1530               , 'Build_Hash_String'
1531               );
1532       END IF;
1533 
1534 END build_hash_string;
1535 
1536 
1537 --bug 8593965, moved new_tree after check_is_reservable
1538 -- Procedure
1539 --   new_tree
1540 -- Description
1541 --   Create a new tree.
1542 --   1. create and init a rootinfo
1543 --   2. find out negative quantity is allowed for the item and org
1544 PROCEDURE new_tree
1545   (   x_return_status             OUT NOCOPY VARCHAR2
1546    ,  p_organization_id           IN  NUMBER
1547    ,  p_inventory_item_id         IN  NUMBER
1548    ,  p_tree_mode                 IN  INTEGER
1549    ,  p_is_revision_control       IN  BOOLEAN
1550    ,  p_is_lot_control            IN  BOOLEAN
1551    ,  p_is_serial_control         IN  BOOLEAN
1552    ,  p_asset_sub_only            IN  BOOLEAN
1553    ,  p_include_suggestion        IN  BOOLEAN
1554    ,  p_demand_source_type_id     IN  NUMBER
1555    ,  p_demand_source_header_id   IN  NUMBER
1556    ,  p_demand_source_line_id     IN  NUMBER
1557    ,  p_demand_source_name        IN  VARCHAR2
1558    ,  p_demand_source_delivery    IN  NUMBER
1559    ,  p_lot_expiration_date       IN  DATE
1560    ,  p_onhand_source             IN  NUMBER
1561    ,  p_pick_release              IN  NUMBER
1562    ,  p_grade_code                IN  VARCHAR2  DEFAULT NULL
1563    ,  x_tree_id                   OUT NOCOPY INTEGER
1564    ) IS
1565       l_return_status             VARCHAR2(1) := fnd_api.g_ret_sts_success;
1566       l_item_node_index           INTEGER;
1567       l_negative_inv_receipt_code NUMBER;
1568       l_disable_flag              NUMBER;
1569 
1570       l_hash_string               VARCHAR2(1000);
1571       l_hash_base                 NUMBER;
1572       l_hash_size                 NUMBER;
1573       l_tree_index                NUMBER;
1574       l_org_item_index            NUMBER;
1575       l_first_tree_index          NUMBER;
1576       l_last_tree_index           NUMBER;
1577       l_original_tree_index       NUMBER;
1578       l_original_hash_string      VARCHAR2(1000);
1579 
1580       l_unit_effective            NUMBER;
1581       l_return_value              BOOLEAN;
1582       --bug 8593965
1583       l_is_reservable_item        BOOLEAN;
1584       l_default_org_status_id     NUMBER;  --Bug 9150005
1585 
1586 BEGIN
1587    print_debug('in new_tree, grade='||p_grade_code);
1588 
1589    l_hash_base := 1;
1590    l_hash_size := power(2, 20);
1591    IF g_hash_string IS NOT NULL THEN
1592      --if not null, we already found hash string in find_rootinfo and don't need to find it again
1593      l_hash_string := g_hash_string;
1594      g_hash_string := NULL;
1595    ELSE
1596      l_hash_string := build_hash_string (
1597         l_return_status
1598       , p_organization_id
1599       , p_inventory_item_id
1600       , p_tree_mode
1601       , p_is_revision_control
1602       , p_is_lot_control
1603       , p_asset_sub_only
1604       , p_demand_source_type_id
1605       , p_demand_source_header_id
1606       , p_demand_source_line_id
1607       , p_demand_source_name
1608       , p_lot_expiration_date
1609       , p_onhand_source);
1610    END IF;
1611 
1612    l_original_hash_string := l_hash_string;
1613    -- Bug 2092207 - hash procedure returning duplicate indices;
1614    --   doubling length of name to try to decrease probability of getting duplicate values
1615    IF g_empty_root_index IS NOT NULL THEN
1616       --if not null, we found an empty slot in find_rootinfo and don't need to look for it again
1617       l_tree_index := g_empty_root_index;
1618       g_empty_root_index := NULL;
1619    ELSE
1620       l_tree_index := dbms_utility.get_hash_value(
1621                           name       => l_hash_string || l_hash_string
1622                         , base       => l_hash_base
1623                         , hash_size  => l_hash_size);
1624 
1625       l_original_tree_index := l_tree_index;
1626 
1627       -- Bug 2092207 - catch duplicate tree indices.  Find next empty tree index
1628       WHILE g_rootinfos.exists(l_tree_index) LOOP
1629          l_tree_index := l_tree_index + 1;
1630          IF l_tree_index >= power(2,20) THEN
1631             l_tree_index := 1;
1632          END IF;
1633          -- if this ever true, we've used all the possible indices. Raise an exception.
1634          IF l_tree_index = l_original_tree_index THEN
1635             RAISE fnd_api.g_exc_unexpected_error;
1636          END IF;
1637       END LOOP;
1638    END IF;
1639 
1640    -- increment the index for the array
1641    --g_rootinfo_counter := g_rootinfo_counter + 1;
1642    g_all_roots_counter := g_all_roots_counter + 1;
1643    g_all_roots(g_all_roots_counter).root_id   := l_tree_index;
1644    g_all_roots(g_all_roots_counter).next_root := 0;
1645    g_all_roots(g_all_roots_counter).last_root := g_all_roots_counter;
1646 
1647    --set values for org/item combo
1648    l_hash_string := p_organization_id || ':' || p_inventory_item_id;
1649    l_org_item_index := dbms_utility.get_hash_value(
1650                          name       => l_hash_string
1651                        , base       => l_hash_base
1652                        , hash_size  => l_hash_size);
1653    IF g_org_item_trees.exists(l_org_item_index) THEN
1654       l_first_tree_index                        := g_org_item_trees(l_org_item_index);
1655       l_last_tree_index                         := g_all_roots(l_first_tree_Index).last_root;
1656       g_all_roots(l_last_tree_index).next_root  := g_all_roots_counter;
1657       g_all_roots(l_first_tree_index).last_root := g_all_roots_counter;
1658    ELSE
1659       g_org_item_trees(l_org_item_index)        := g_all_roots_counter;
1660    END IF;
1661 
1662    -- initialize the rootinfo record
1663    g_rootinfos(l_tree_index).organization_id      := p_organization_id;
1664    g_rootinfos(l_tree_index).inventory_item_id    := p_inventory_item_id;
1665    g_rootinfos(l_tree_index).tree_mode            := p_tree_mode;
1666    g_rootinfos(l_tree_index).is_revision_control  := p_is_revision_control;
1667    g_rootinfos(l_tree_index).is_serial_control    := p_is_serial_control;
1668    g_rootinfos(l_tree_index).is_lot_control       := p_is_lot_control;
1669    g_rootinfos(l_tree_index).asset_sub_only       := p_asset_sub_only    ;
1670    g_rootinfos(l_tree_index).include_suggestion   := p_include_suggestion;
1671    g_rootinfos(l_tree_index).lot_expiration_date  := p_lot_expiration_date;
1672    g_rootinfos(l_tree_index).grade_code           := p_grade_code;
1673 
1674    print_debug('in new_tree org_id ='||g_rootinfos(l_tree_index).organization_id
1675       ||', item='||p_inventory_item_id||', tree_index='||l_tree_index);
1676    print_debug('in new_tree... grade_code='||p_grade_code);
1677 
1678    --only store demand info in tree for loose only mode - otherwise, it's stored in g_demand_info
1679    IF p_tree_mode = g_loose_only_mode THEN
1680       g_rootinfos(l_tree_index).demand_source_type_id   := p_demand_source_type_id;
1681       g_rootinfos(l_tree_index).demand_source_header_id := p_demand_source_header_id;
1682       g_rootinfos(l_tree_index).demand_source_line_id   := p_demand_source_line_id;
1683       g_rootinfos(l_tree_index).demand_source_name      := p_demand_source_name;
1684       g_rootinfos(l_tree_index).demand_source_delivery  := p_demand_source_delivery;
1685    END IF;
1686    g_rootinfos(l_tree_index).onhand_source             := p_onhand_source;
1687    g_rootinfos(l_tree_index).pick_release              := p_pick_release;
1688    g_rootinfos(l_tree_index).need_refresh              := TRUE;
1689    g_rootinfos(l_tree_index).hash_string               := l_original_hash_string;
1690 
1691    -- determine index for item node.  Rather than call the dbms hash utility,
1692    -- we'll just use the value for g_max_hash_rec. If this is the first tree
1693    -- to be created for this session, g_max_hash_rec will not have been
1694    -- initialized.  In that case, we'll use an index of 1.
1695    IF g_max_hash_rec <> 0 THEN
1696       g_max_hash_rec    := g_max_hash_rec + 1;
1697       l_item_node_index := g_max_hash_rec;
1698    ELSE
1699     /* Bug 2901076 : Setting l_item_node_index for the case where the first record
1700     has no onhand and availability */
1701       g_max_hash_rec := power(2,29) + 1;
1702       l_item_node_index := g_max_hash_rec;
1703    END IF;
1704    -- create the corresponding item tree node
1705    --bug 9380420, passing l_tree_index
1706    new_tree_node
1707      (  x_return_status => l_return_status
1708       , p_node_level    => g_item_level
1709       , p_tree_id       => l_tree_index
1710       , p_node_index    => l_item_node_index
1711       );
1712 
1713    IF l_return_status = fnd_api.g_ret_sts_error THEN
1714       RAISE fnd_api.g_exc_error;
1715    End IF ;
1716 
1717    IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
1718       RAISE fnd_api.g_exc_unexpected_error;
1719    End IF;
1720 
1721    g_rootinfos(l_tree_index).item_node_index := l_item_node_index;
1722    --bug 9380420, commenting the code below as new_tree_node will take care of it.
1723    /*
1724    --bug 8593965, calling check_is_reservable_item
1725    --             to set value of g_nodes(l_item_node_index).is_reservable_sub
1726    check_is_reservable_item
1727    (  x_return_status      => l_return_status
1728     , p_organization_id    => p_organization_id
1729     , p_inventory_item_id  => p_inventory_item_id
1730     , x_is_reservable_item => l_is_reservable_item);
1731 
1732    g_nodes(l_item_node_index).is_reservable_sub := l_is_reservable_item;
1733    */
1734 
1735    l_return_value := INV_CACHE.set_org_rec(p_organization_id);
1736    IF NOT l_return_value THEN
1737       RAISE fnd_api.g_exc_unexpected_error;
1738    END IF;
1739    l_negative_inv_receipt_code := INV_CACHE.org_rec.negative_inv_receipt_code;
1740    l_default_org_status_id     := INV_CACHE.org_rec.default_status_id;   --Bug 9150005
1741 
1742    print_debug('In new_tree l_negative_inv_receipt_code: '||l_negative_inv_receipt_code ||' l_default_org_status_id: ' ||l_default_org_status_id);
1743 
1744    IF (l_negative_inv_receipt_code = 1) THEN
1745       g_rootinfos(l_tree_index).neg_inv_allowed := TRUE;
1746    ELSE
1747       g_rootinfos(l_tree_index).neg_inv_allowed := FALSE;
1748    END IF;
1749    --  Bug 9150005 Start
1750    IF (l_default_org_status_id IS NULL) THEN
1751       g_rootinfos(l_tree_index).onhand_status_enabled := FALSE;
1752    ELSE
1753       g_rootinfos(l_tree_index).onhand_status_enabled := TRUE;
1754    END IF;
1755    --  Bug 9150005 End
1756 
1757    --BENCHMARK
1758    --check whether item is unit effective controlled
1759    /* Check item cache instead of PJM function
1760     l_unit_effective := pjm_unit_eff.unit_effective_item(
1761          x_item_id => p_inventory_item_id
1762         ,x_organization_id => p_organization_id);
1763    */
1764    l_return_value := INV_CACHE.set_item_rec(p_organization_id, p_inventory_item_id);
1765    IF NOT l_return_value THEN
1766       RAISE fnd_api.g_exc_unexpected_error;
1767    END IF;
1768    l_unit_effective := INV_CACHE.item_rec.effectivity_control;
1769 
1770    /*Bug#5352407. All EAM asset group items are unit effective. Though they are unit
1771      effective, unit number is not required to perform transactions. So,
1772      they should be treated as non-unit effective otherwise the quantity
1773      tree would return the available qty as 0 even though there is qty
1774      available (Since it calls PJM API for getting onhand qty for
1775      unit effective items and the PJM API returns 0 if the item is of EAM asset group
1776      type).*/
1777    IF  l_unit_effective = 2 THEN
1778       IF INV_CACHE.item_rec.eam_item_type = 1 THEN
1779          l_unit_effective := 1;
1780       END IF;
1781    END IF;
1782 
1783 
1784    --2 is model/unit effective, which is what we care about
1785    IF l_unit_effective = 2 THEN
1786       g_rootinfos(l_tree_index).unit_effective := 1;
1787    ELSE
1788       g_rootinfos(l_tree_index).unit_effective := 2;
1789    END IF;
1790 
1791    --Bug 1384720 -performance
1792    -- Populate the demand table.  The index of this record is what
1793    --  is returned to the user as the tree_id.
1794    g_demand_counter := g_demand_counter + 1;
1795    g_demand_info(g_demand_counter).root_id                  := l_tree_index;
1796    g_demand_info(g_demand_counter).tree_mode                := p_tree_mode;
1797    g_demand_info(g_demand_counter).pick_release             := p_pick_release;
1798    g_demand_info(g_demand_counter).demand_source_type_id    := p_demand_source_type_id;
1799    g_demand_info(g_demand_counter).demand_source_header_id  := p_demand_source_header_id;
1800    g_demand_info(g_demand_counter).demand_source_line_id    := p_demand_source_line_id;
1801    g_demand_info(g_demand_counter).demand_source_name       := p_demand_source_name;
1802    g_demand_info(g_demand_counter).demand_source_delivery   := p_demand_source_delivery;
1803    -- return the tree id
1804    x_tree_id         := g_demand_counter;
1805    x_return_status   := l_return_status;
1806 
1807    print_debug('in new_tree org_id ='||g_rootinfos(l_tree_index).organization_id);
1808 
1809 EXCEPTION
1810 
1811    WHEN fnd_api.g_exc_error THEN
1812       x_return_status := fnd_api.g_ret_sts_error;
1813 
1814    WHEN fnd_api.g_exc_unexpected_error THEN
1815       x_return_status := fnd_api.g_ret_sts_unexp_error ;
1816 
1817    WHEN OTHERS THEN
1818       x_return_status := fnd_api.g_ret_sts_unexp_error ;
1819 
1820       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
1821         THEN
1822          fnd_msg_pub.add_exc_msg
1823            (  g_pkg_name
1824               , 'New_Tree'
1825               );
1826       END IF;
1827 
1828 END new_tree;
1829 
1830 
1831 -- Procedure
1832 --   get_lock_handle
1833 -- Description
1834 --   Helper function which gets the lock handle for a given item/org.
1835 --   Allocate_unique executes a commit, so this function is
1836 --   autonomous to prevent commit of the session's other data.
1837 FUNCTION get_lock_handle (
1838     p_organization_id   IN NUMBER
1839       ,p_inventory_item_id IN NUMBER) RETURN VARCHAR2 IS
1840 
1841    PRAGMA AUTONOMOUS_TRANSACTION;
1842    l_lock_handle VARCHAR2(128);
1843    l_lock_name   VARCHAR2(30);
1844 BEGIN
1845 
1846    l_lock_name := 'INV_QT_' || p_organization_id || '_' || p_inventory_item_id;
1847    dbms_lock.allocate_unique(
1848         lockname   => l_lock_name
1849       , lockhandle => l_lock_handle);
1850    return l_lock_handle;
1851 END get_lock_handle;
1852 
1853 
1854 -- Procedure
1855 --   lock_tree
1856 -- Description
1857 --   this function places a user lock on an item/organization
1858 --   combination.  Once this lock is placed, no other sessions
1859 --   can lock the same item/org combo.  Users who call lock_tree
1860 --   do not always have to call release_lock explicitly.  The lock is
1861 --   released automatically at commit, rollback, or session loss.
1862 
1863 PROCEDURE lock_tree(
1864      p_api_version_number   IN  NUMBER
1865    , p_init_msg_lst         IN  VARCHAR2
1866    , x_return_status        OUT NOCOPY VARCHAR2
1867    , x_msg_count            OUT NOCOPY NUMBER
1868    , x_msg_data             OUT NOCOPY VARCHAR2
1869    , p_organization_id      IN  NUMBER
1870    , p_inventory_item_id    IN  NUMBER)
1871 
1872 IS
1873    l_api_version_number    CONSTANT NUMBER       := 1.0;
1874    l_api_name              CONSTANT VARCHAR2(30) := 'Lock_Tree';
1875    l_return_status         VARCHAR2(1) := fnd_api.g_ret_sts_success;
1876    l_lock_handle           VARCHAR2(128);
1877    l_status                INTEGER;
1878 
1879 BEGIN
1880    print_debug('... in lock_tree... will create a dbms_lock...');
1881    --  Standard call to check for call compatibility
1882    IF NOT fnd_api.compatible_api_call(  l_api_version_number
1883                                       , p_api_version_number
1884                                       , l_api_name
1885                                       , G_PKG_NAME
1886                                       ) THEN
1887       RAISE fnd_api.g_exc_unexpected_error;
1888    END IF;
1889 
1890    --  Initialize message list.
1891    IF fnd_api.to_boolean(p_init_msg_lst) THEN
1892       fnd_msg_pub.initialize;
1893    END IF;
1894 
1895    --validate organization and inventory item
1896    IF (p_organization_id IS NULL OR p_inventory_item_id IS NULL) THEN
1897       --raise error condition
1898       fnd_message.set_name('INV','INV_LOCK_MISSING_ARGS');
1899       fnd_msg_pub.ADD;
1900       RAISE fnd_api.g_exc_error;
1901    END IF;
1902 
1903    IF g_lock_timeout IS NULL THEN
1904       g_lock_timeout := nvl(to_number(fnd_profile.value('INV_QTY_TREE_TIMEOUT')),dbms_lock.maxwait);
1905    END IF;
1906    --get lock handle by calling helper function
1907    l_lock_handle := get_lock_handle(
1908         p_organization_id   => p_organization_id
1909       , p_inventory_item_id => p_inventory_item_id);
1910 
1911    --request lock
1912    l_status := dbms_lock.request(
1913         lockhandle        => l_lock_handle
1914       , lockmode          => dbms_lock.x_mode
1915       , timeout           => nvl(g_lock_timeout,dbms_lock.maxwait)
1916       , release_on_commit => TRUE);
1917 
1918    --check for error cases
1919    IF (l_status NOT IN (0,4)) THEN
1920       if (l_status = 1) then -- timeout
1921          fnd_message.set_name('INV','INV_LOCK_TREE_TIMEOUT');
1922          fnd_msg_pub.ADD;
1923          RAISE fnd_api.g_exc_error;
1924       elsif (l_status = 2) then -- deadlock
1925          fnd_message.set_name('INV','INV_LOCK_TREE_DEADLOCK');
1926          fnd_msg_pub.ADD;
1927          RAISE fnd_api.g_exc_error;
1928       else -- internal error - not fault of user
1929          fnd_message.set_name('INV','INV_LOCK_TREE_ERROR');
1930          fnd_msg_pub.ADD;
1931          RAISE fnd_api.g_exc_error;
1932       end if;
1933    END IF;
1934 
1935    print_debug('QTY TREE LOCK ACQUIRED '||l_lock_handle, 9);
1936 
1937    x_return_status := l_return_status;
1938 
1939 EXCEPTION
1940 
1941    WHEN fnd_api.g_exc_error THEN
1942         x_return_status := fnd_api.g_ret_sts_error;
1943 
1944    WHEN fnd_api.g_exc_unexpected_error THEN
1945         x_return_status := fnd_api.g_ret_sts_unexp_error ;
1946 
1947     WHEN OTHERS THEN
1948         x_return_status := fnd_api.g_ret_sts_unexp_error ;
1949 
1950         IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
1951           THEN
1952            fnd_msg_pub.add_exc_msg
1953              (  g_pkg_name
1954               , 'Lock_Tree'
1955               );
1956         END IF;
1957 
1958 END lock_tree;
1959 
1960 -- Procedure
1961 --   release_lock
1962 -- Description
1963 --   this function releases the user lock on an item/organization
1964 --   combination created by this session.  Users who call lock_tree
1965 --   do not always have to call release_lock explicitly.  The lock is
1966 --   released automatically at commit, rollback, or session loss.
1967 
1968 PROCEDURE release_lock(
1969      p_api_version_number   IN  NUMBER
1970    , p_init_msg_lst         IN  VARCHAR2
1971    , x_return_status        OUT NOCOPY VARCHAR2
1972    , x_msg_count            OUT NOCOPY NUMBER
1973    , x_msg_data             OUT NOCOPY VARCHAR2
1974    , p_organization_id      IN  NUMBER
1975    , p_inventory_item_id    IN  NUMBER)
1976 
1977 IS
1978    l_api_version_number   CONSTANT NUMBER       := 1.0;
1979    l_api_name             CONSTANT VARCHAR2(30) := 'Release_Lock';
1980    l_return_status    VARCHAR2(1) := fnd_api.g_ret_sts_success;
1981    l_lock_handle VARCHAR2(128);
1982    l_status INTEGER;
1983 BEGIN
1984    print_debug('... in release_lock ...');
1985    --  Standard call to check for call compatibility
1986    IF NOT fnd_api.compatible_api_call(l_api_version_number
1987                                       , p_api_version_number
1988                                       , l_api_name
1989                                       , G_PKG_NAME
1990                                       ) THEN
1991       RAISE fnd_api.g_exc_unexpected_error;
1992    END IF;
1993 
1994    --  Initialize message list.
1995    IF fnd_api.to_boolean(p_init_msg_lst) THEN
1996       fnd_msg_pub.initialize;
1997    END IF;
1998 
1999    --validate organization and inventory item
2000    IF (p_organization_id IS NULL OR
2001        p_inventory_item_id IS NULL) THEN
2002    --raise error condition
2003          fnd_message.set_name('INV','INV_LOCK_RELEASE_MISSING_ARGS');
2004          fnd_msg_pub.ADD;
2005          RAISE fnd_api.g_exc_error;
2006    END IF;
2007 
2008 
2009    --get lock handle by calling helper function
2010    l_lock_handle := get_lock_handle(
2011          p_organization_id   => p_organization_id
2012        , p_inventory_item_id => p_inventory_item_id);
2013 
2014 
2015    l_status := dbms_lock.release(l_lock_handle);
2016 
2017    --if success (status = 0) or session does not own lock (status=4), do nothing
2018    --if parameter error or illegal lock handle (internal error)
2019    if l_status IN (3,5) THEN
2020       fnd_message.set_name('INV','INV_LOCK_RELEASE_ERROR');
2021       fnd_msg_pub.ADD;
2022       RAISE fnd_api.g_exc_error;
2023    end if;
2024 
2025    print_debug('... Normal end of release_lock ...');
2026    x_return_status := l_return_status;
2027 
2028 EXCEPTION
2029 
2030    WHEN fnd_api.g_exc_error THEN
2031       print_debug('... release_lock EXP_ERROR='||SQLERRM, 9);
2032       x_return_status := fnd_api.g_ret_sts_error;
2033 
2034    WHEN fnd_api.g_exc_unexpected_error THEN
2035       print_debug('... release_lock UNEXP_ERROR='||SQLERRM, 9);
2036       x_return_status := fnd_api.g_ret_sts_unexp_error ;
2037 
2038    WHEN OTHERS THEN
2039       print_debug('... release_lock others='||SQLERRM, 9);
2040       x_return_status := fnd_api.g_ret_sts_unexp_error ;
2041 
2042      IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
2043      THEN
2044         fnd_msg_pub.add_exc_msg
2045           (  g_pkg_name
2046            , 'Release_Lock'
2047            );
2048      END IF;
2049 
2050 END release_lock;
2051 
2052 
2053 FUNCTION is_tree_valid
2054   (
2055    p_tree_id IN INTEGER
2056    ) RETURN BOOLEAN IS
2057 BEGIN
2058    IF p_tree_id IS NULL THEN
2059       RETURN FALSE;
2060    END IF;
2061 
2062    IF p_tree_id <= 0
2063 --   OR p_tree_id > g_rootinfo_counter
2064    THEN
2065       RETURN FALSE;
2066    END IF;
2067 
2068    IF g_rootinfos.exists(p_tree_id) = FALSE THEN
2069       RETURN FALSE;
2070    END IF;
2071 
2072    IF g_rootinfos(p_tree_id).inventory_item_id IS NULL THEN
2073       RETURN FALSE;
2074    END IF;
2075 
2076    RETURN TRUE;
2077 END is_tree_valid;
2078 
2079 FUNCTION is_saved_tree_valid
2080   (
2081    p_tree_id IN INTEGER
2082    ) RETURN BOOLEAN IS
2083 BEGIN
2084    IF p_tree_id IS NULL THEN
2085       RETURN FALSE;
2086    END IF;
2087 
2088    IF g_saveroots.exists(p_tree_id) = FALSE THEN
2089       RETURN FALSE;
2090    END IF;
2091 
2092    IF g_saveroots(p_tree_id).inventory_item_id IS NULL THEN
2093       RETURN FALSE;
2094    END IF;
2095 
2096    RETURN TRUE;
2097 END is_saved_tree_valid;
2098 
2099 PROCEDURE invalidate_tree
2100   (
2101    p_tree_id IN INTEGER
2102    ) IS
2103 BEGIN
2104    IF is_tree_valid(p_tree_id) THEN
2105       g_rootinfos(p_tree_id).inventory_item_id := NULL;
2106       g_rootinfos(p_tree_id).organization_id := NULL;
2107       g_rootinfos(p_tree_id).need_refresh := TRUE;
2108    END IF;
2109 
2110    IF is_saved_tree_valid(p_tree_id) THEN
2111       g_saveroots(p_tree_id).inventory_item_id := NULL;
2112       g_saveroots(p_tree_id).organization_id := NULL;
2113    END IF;
2114 
2115 
2116 END invalidate_tree;
2117 
2118 
2119 -- Function
2120 --    find_rootinfo
2121 -- Description
2122 --    find a rootinfo record based on input parameters
2123 --  Return
2124 --    0                          if rootinfo not found
2125 --    >0                         index for the rootinfo in the rootinfo array
2126 FUNCTION find_rootinfo
2127   (   x_return_status            OUT NOCOPY VARCHAR2
2128    ,  p_organization_id          IN  NUMBER
2129    ,  p_inventory_item_id        IN  NUMBER
2130    ,  p_tree_mode                IN  INTEGER
2131    ,  p_is_revision_control      IN  BOOLEAN
2132    ,  p_is_lot_control           IN  BOOLEAN
2133    ,  p_is_serial_control        IN  BOOLEAN
2134    ,  p_asset_sub_only           IN  BOOLEAN
2135    ,  p_include_suggestion       IN  BOOLEAN
2136    ,  p_demand_source_type_id    IN  NUMBER
2137    ,  p_demand_source_header_id  IN  NUMBER
2138    ,  p_demand_source_line_id    IN  NUMBER
2139    ,  p_demand_source_name       IN  VARCHAR2
2140    ,  p_demand_source_delivery   IN  NUMBER
2141    ,  p_lot_expiration_date      IN  DATE
2142    ,  p_onhand_source            IN  NUMBER
2143    ,  p_pick_release             IN  NUMBER
2144    ) RETURN INTEGER
2145   IS
2146 
2147      l_return_status            VARCHAR2(1) := fnd_api.g_ret_sts_success;
2148      l_rootinfo_index           INTEGER;
2149      l_hash_base                NUMBER;
2150      l_hash_size                NUMBER;
2151      l_hash_string              VARCHAR2(1000);
2152      l_tree_index               NUMBER;
2153      l_original_tree_index      NUMBER;
2154 
2155 BEGIN
2156    l_rootinfo_index := 1;
2157 
2158    print_debug('    Entering find_rootinfo for :');
2159    print_debug(    ' org ' || p_organization_id
2160                 || ' item ' || p_inventory_item_id
2161                 || ' mode ' || p_tree_mode
2162                 || ' dsrc ' || p_demand_source_type_id
2163                 || ' dhdr ' || p_demand_source_header_id
2164                 || ' dlin ' || p_demand_source_line_id
2165                 || ' dnme ' || p_demand_source_name
2166                 || ' ddel ' || p_demand_source_delivery
2167                 || ' exp ' || p_lot_expiration_date
2168                 || ' onh ' || p_onhand_source
2169                 || ' rel ' || p_pick_release);
2170    IF   p_is_lot_control THEN
2171       print_debug('lot_control=TRUE');
2172    ELSE
2173       print_debug('lot_control=FALSE');
2174    END IF;
2175 
2176 
2177    l_hash_base := 1;
2178    l_hash_size := power(2, 20);
2179    l_hash_string := build_hash_string (
2180         l_return_status
2181       , p_organization_id
2182       , p_inventory_item_id
2183       , p_tree_mode
2184       , p_is_revision_control
2185       , p_is_lot_control
2186       , p_asset_sub_only
2187       , p_demand_source_type_id
2188       , p_demand_source_header_id
2189       , p_demand_source_line_id
2190       , p_demand_source_name
2191       , p_lot_expiration_date
2192       , p_onhand_source);
2193 
2194    print_debug('in find_rootinfo: hashString_1='||l_hash_string);
2195    g_hash_string := l_hash_string;
2196    -- Bug 2092207 - hash procedure returning duplicate indices;
2197    --   doubling length of name to try to decrease probability of
2198    --   getting duplicate values
2199    l_tree_index := dbms_utility.get_hash_value(
2200                name       => l_hash_string || l_hash_string
2201              , base       => l_hash_base
2202              , hash_size  => l_hash_size);
2203 
2204    --Bug 5241485 fix
2205    l_original_tree_index := l_tree_index;
2206 
2207    print_debug('in find_rootinfo: tree_index='||l_tree_index);
2208 
2209    -- Bug 2092207 - catch duplicate tree indices. If hash string
2210    -- does not match the hash string of the found tree index,
2211    -- the get_hash_value procedure is returning duplicate tree indices
2212    -- for different hash strings.  In that case, increment the index and
2213    -- check to see if the tree with the matching hash string is at that value.
2214    -- Exit loop when correct root is found, or when empty root is found
2215 
2216    IF g_rootinfos.exists(l_tree_index) THEN
2217       WHILE g_rootinfos(l_tree_index).hash_string <> l_hash_string LOOP
2218          print_debug('in find_rootinfo: saved_HashString='||g_rootinfos(l_tree_index).hash_string);
2219 
2220          l_tree_index := l_tree_index + 1;
2221          -- if empty root is found, then the tree does not exist yet
2222          If NOT g_rootinfos.exists(l_tree_index) THEN
2223             g_empty_root_index := l_tree_index;
2224             EXIT;
2225          End If;
2226          -- if we reach the end of the possible index values, loop back to 1.
2227          If l_tree_index >= power(2,20) Then
2228             l_tree_index := 1;
2229          End If;
2230          -- if this ever true, we've used all the possible indices. Raise
2231          -- an exception.
2232          If l_tree_index = l_original_tree_index Then
2233             RAISE fnd_api.g_exc_unexpected_error;
2234          End If;
2235       END LOOP;
2236       --Bug 5241485 fix. This one causing no data found exception
2237       print_debug('in find_rootinfo: AFTER LOOP l_tree_index:'||l_tree_index);
2238    END IF;
2239 
2240 
2241    x_return_status := l_return_status;
2242 
2243    IF g_rootinfos.exists(l_tree_index) THEN
2244       --Bug 1384720 - performance improvements
2245       -- Every time this procedure is called, insert a new record
2246       --  in demand_info table.  Return the index for that record as the tree_id
2247       g_demand_counter := g_demand_counter + 1;
2248       g_demand_info(g_demand_counter).root_id                  := l_tree_index;
2249       g_demand_info(g_demand_counter).tree_mode                := p_tree_mode;
2250       g_demand_info(g_demand_counter).pick_release             := p_pick_release;
2251       g_demand_info(g_demand_counter).demand_source_type_id    := p_demand_source_type_id;
2252       g_demand_info(g_demand_counter).demand_source_header_id  := p_demand_source_header_id;
2253       g_demand_info(g_demand_counter).demand_source_line_id    := p_demand_source_line_id;
2254       g_demand_info(g_demand_counter).demand_source_name       := p_demand_source_name;
2255       g_demand_info(g_demand_counter).demand_source_delivery   := p_demand_source_delivery;
2256       l_rootinfo_index := g_demand_counter;
2257       print_debug('odab in find_rootinfo Normal End with index='||l_rootinfo_index||', return='||x_return_status);
2258       RETURN l_rootinfo_index; -- rootinfo node found
2259    ELSE
2260       print_debug('odab in find_rootinfo Normal End With index=0, NOT FOUND, return='||x_return_status);
2261       RETURN 0;                -- rootinfo node not found
2262    END IF;
2263 
2264 EXCEPTION
2265    WHEN OTHERS THEN
2266       x_return_status := fnd_api.g_ret_sts_unexp_error ;
2267       print_debug('odab in find_rootinfo OTHERS='||SQLERRM, 9);
2268       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
2269         THEN
2270          fnd_msg_pub.add_exc_msg
2271            (  g_pkg_name
2272               , 'Find_Rootinfo'
2273               );
2274       END IF;
2275       RETURN 0;
2276 
2277 END find_rootinfo;
2278 
2279 -- Function
2280 --   find_child_node
2281 -- Description
2282 --   search the child linked list of a parent node
2283 --   for a child node that has the value specified in the input
2284 --   and if no such child node, create it. So it should always
2285 --   return true, unlese there is exception occurred
2286 FUNCTION find_child_node
2287   (  x_return_status        OUT NOCOPY VARCHAR2
2288    , p_node_level           IN  INTEGER
2289    , p_tree_id              IN  INTEGER
2290    , p_revision             IN  VARCHAR2  DEFAULT NULL
2291    , p_lot_number           IN  VARCHAR2  DEFAULT NULL
2292    , p_subinventory_code    IN  VARCHAR2  DEFAULT NULL
2293    , p_is_reservable_sub    IN  BOOLEAN   DEFAULT NULL
2294    , p_locator_id           IN  NUMBER    DEFAULT NULL
2295    , p_lpn_id               IN  NUMBER    DEFAULT NULL
2296    , p_cost_group_id        IN  NUMBER    DEFAULT NULL
2297    , x_child_index          OUT NOCOPY INTEGER
2298    ) RETURN BOOLEAN IS
2299       l_return_status       VARCHAR2(1) := fnd_api.g_ret_sts_success;
2300       l_node_index          INTEGER;
2301       l_hash_string         VARCHAR2(300);
2302       l_hash_size           NUMBER;
2303       l_hash_base           NUMBER := 1;
2304       l_prev_node_index     NUMBER;
2305 
2306 BEGIN
2307    IF p_revision IS NULL
2308       AND p_lot_number IS NULL
2309       AND p_subinventory_code IS NULL
2310       AND p_locator_id IS NULL
2311       AND p_lpn_id IS NULL
2312       AND p_cost_group_id IS NULL
2313         THEN
2314       fnd_message.set_name('INV','INV-Cannot find node');
2315       fnd_message.set_token('ROUTINE', 'Find_Child_Node');
2316       fnd_msg_pub.ADD;
2317       RAISE fnd_api.g_exc_error;
2318    END IF;
2319 
2320 
2321    print_debug('... in find_child_node, building the hash string...');
2322    --build hash string
2323    l_hash_size := power(2,29);
2324    IF g_max_hash_rec = 0 then
2325       g_max_hash_rec := l_hash_size + 1;
2326    END IF;
2327    l_hash_string := p_tree_id || ':' || p_revision || ':' ||
2328        p_lot_number || ':' || p_subinventory_code || ':' ||
2329        p_locator_id || ':' || p_lpn_id || ':' ||
2330        p_cost_group_id;
2331 
2332    --get hash value using dbms_utility package
2333    l_node_index := dbms_utility.get_hash_value(
2334                      name       => l_hash_string
2335                     ,base       => l_hash_base
2336                     ,hash_size  => l_hash_size);
2337 
2338    print_debug('... in find_child_node,  after dbms_utility.get_hash_value');
2339 
2340    IF g_nodes.exists(l_node_index) THEN
2341       WHILE g_nodes(l_node_index).hash_string <> l_hash_string LOOP
2342          l_prev_node_index := l_node_index;
2343          l_node_index := g_nodes(l_node_index).next_hash_record;
2344          IF l_node_index = 0 THEN
2345             g_max_hash_rec := g_max_hash_rec + 1;
2346             l_node_index := g_max_hash_rec;
2347             g_nodes(l_prev_node_index).next_hash_record := l_node_index;
2348             EXIT; --exit loop
2349          End IF;
2350       END LOOP;
2351    END IF;
2352 
2353    IF not g_nodes.exists(l_node_index) THEN
2354       new_tree_node(
2355            x_return_status       => l_return_status
2356          , p_node_level          => p_node_level
2357          , p_tree_id             => p_tree_id
2358          , p_revision            => p_revision
2359          , p_lot_number          => p_lot_number
2360          , p_subinventory_code   => p_subinventory_code
2361          , p_is_reservable_sub   => p_is_reservable_sub
2362          , p_locator_id          => p_locator_id
2363          , p_lpn_id              => p_lpn_id
2364          , p_cost_group_id       => p_cost_group_id
2365          , p_node_index          => l_node_index
2366          , p_hash_string         => l_hash_string);
2367 
2368    END IF;
2369 
2370    x_child_index := l_node_index;
2371    x_return_status := l_return_status;
2372 
2373    RETURN TRUE;
2374 
2375 EXCEPTION
2376 
2377    WHEN fnd_api.g_exc_error THEN
2378         x_return_status := fnd_api.g_ret_sts_error;
2379 
2380    WHEN fnd_api.g_exc_unexpected_error THEN
2381         x_return_status := fnd_api.g_ret_sts_unexp_error ;
2382 
2383     WHEN OTHERS THEN
2384         x_return_status := fnd_api.g_ret_sts_unexp_error ;
2385 
2386         -- For performance reasons during pick release only do this ifdebug is on
2387         IF ((NOT g_is_pickrelease) OR nvl(g_debug,2) = 1) THEN
2388           IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
2389             THEN
2390              fnd_msg_pub.add_exc_msg
2391                (  g_pkg_name
2392                 , 'Find_Child_Node'
2393                 );
2394           END IF;
2395         END IF;
2396 
2397 END find_child_node;
2398 
2399 -- Function
2400 --   find_tree_node
2401 -- Description
2402 --   find the tree node based on values of the input. If
2403 --   the node and its ancestors do not exist in the tree
2404 --   , create them
2405 FUNCTION find_tree_node
2406   (   x_return_status        OUT NOCOPY VARCHAR2
2407    ,  p_tree_id              IN  INTEGER
2408    ,  p_revision             IN  VARCHAR2
2409    ,  p_lot_number           IN  VARCHAR2
2410    ,  p_subinventory_code    IN  VARCHAR2
2411    ,  p_is_reservable_sub    IN  BOOLEAN
2412    ,  p_locator_id           IN  NUMBER
2413    ,  x_node_index           OUT NOCOPY INTEGER
2414    ,  p_lpn_id               IN  NUMBER
2415    ,  p_cost_group_id        IN  NUMBER
2416    ) RETURN BOOLEAN IS
2417       l_return_status        VARCHAR2(1) := fnd_api.g_ret_sts_success;
2418       l_current_node_index   INTEGER;
2419       l_child_node_index     INTEGER;
2420       l_found                BOOLEAN;
2421       l_last_child        INTEGER;
2422       l_item_node      INTEGER;
2423 
2424 BEGIN
2425    x_node_index := 0;
2426    l_child_node_index := 0;
2427    l_current_node_index := 0;
2428    IF p_lpn_id IS NOT NULL THEN
2429       -- search for the lpn node
2430       l_found := find_child_node(
2431                           x_return_status      => l_return_status
2432                         , p_node_level         => g_lpn_level
2433                         , p_tree_id            => p_tree_id
2434                         , p_revision           => p_revision
2435                         , p_lot_number         => p_lot_number
2436                         , p_subinventory_code  => p_subinventory_code
2437                         , p_is_reservable_sub  => p_is_reservable_sub
2438                         , p_locator_id         => p_locator_id
2439                         , p_lpn_id             => p_lpn_id
2440                         , p_cost_group_id      => NULL
2441                         , x_child_index        => l_current_node_index
2442                         );
2443 
2444       IF l_return_status = fnd_api.g_ret_sts_error THEN
2445          RAISE fnd_api.g_exc_error;
2446       End IF ;
2447 
2448       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2449          RAISE fnd_api.g_exc_unexpected_error;
2450       End IF;
2451 
2452       IF l_found = FALSE THEN
2453          fnd_message.set_name('INV','INV-Cannot find node');
2454          fnd_message.set_token('ROUTINE', 'Find_Tree_Node');
2455          fnd_msg_pub.ADD;
2456          RAISE fnd_api.g_exc_unexpected_error;
2457       End IF;
2458 
2459       IF l_child_node_index <> 0 THEN
2460          IF g_nodes(l_current_node_index).first_child_index = 0 THEN
2461            g_nodes(l_current_node_index).first_child_index := l_child_node_index;
2462            g_nodes(l_current_node_index).last_child_index := l_child_node_index;
2463          ELSE
2464            l_last_child := g_nodes(l_current_node_index).last_child_index;
2465            g_nodes(l_last_child).next_sibling_Index := l_child_node_index;
2466            g_nodes(l_current_node_index).last_child_index := l_child_node_index;
2467          END IF;
2468          g_nodes(l_child_node_index).parent_index := l_current_node_index;
2469       END IF;
2470       l_child_node_index := l_current_node_index;
2471       IF x_node_index = 0 THEN
2472         x_node_index := l_current_node_index;
2473       End IF;
2474       IF g_nodes(l_current_node_index).parent_index <> 0 THEN
2475           x_return_status := l_return_status;
2476           print_debug('---End of find_tree_node... x_node_index='||x_node_index||', current_node_index='||l_current_node_index||'.');
2477           RETURN TRUE;
2478       END IF;
2479    END IF;
2480 
2481    IF p_locator_id IS NOT NULL THEN
2482       -- search for the locator node
2483       print_debug('... entering find_child_node for loct='||p_locator_id||', lot='||substr(p_lot_number, 1,10) );
2484       l_found := find_child_node(
2485                           x_return_status      => l_return_status
2486                         , p_node_level         => g_locator_level
2487                         , p_tree_id            => p_tree_id
2488                         , p_revision           => p_revision
2489                         , p_lot_number         => p_lot_number
2490                         , p_subinventory_code  => p_subinventory_code
2491                         , p_is_reservable_sub  => p_is_reservable_sub
2492                         , p_locator_id         => p_locator_id
2493                         , p_lpn_id             => NULL
2494                         , p_cost_group_id      => NULL
2495                         , x_child_index        => l_current_node_index
2496                         );
2497 
2498       IF l_return_status = fnd_api.g_ret_sts_error THEN
2499          RAISE fnd_api.g_exc_error;
2500       End IF ;
2501 
2502       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2503          RAISE fnd_api.g_exc_unexpected_error;
2504       End IF;
2505 
2506       IF l_found = FALSE THEN
2507          fnd_message.set_name('INV','INV-Cannot find node');
2508          fnd_message.set_token('ROUTINE', 'Find_Tree_Node');
2509          fnd_msg_pub.ADD;
2510          RAISE fnd_api.g_exc_unexpected_error;
2511       End IF;
2512 
2513       If l_child_node_index <> 0 Then
2514          if g_nodes(l_current_node_index).first_child_index = 0 then
2515            g_nodes(l_current_node_index).first_child_index := l_child_node_index;
2516            g_nodes(l_current_node_index).last_child_index := l_child_node_index;
2517          else
2518            l_last_child := g_nodes(l_current_node_index).last_child_index;
2519            g_nodes(l_last_child).next_sibling_Index := l_child_node_index;
2520            g_nodes(l_current_node_index).last_child_index := l_child_node_index;
2521          end if;
2522          g_nodes(l_child_node_index).parent_index := l_current_node_index;
2523       End If;
2524       l_child_node_index := l_current_node_index;
2525       If x_node_index = 0 Then
2526         x_node_index := l_current_node_index;
2527       End If;
2528       IF g_nodes(l_current_node_index).parent_index <> 0 THEN
2529          x_return_status := l_return_status;
2530          print_debug('--End of find_tree_node... x_node_index='||x_node_index||', current_node_index='||l_current_node_index||'.');
2531          RETURN TRUE;
2532       END IF;
2533    END IF;
2534 
2535    IF p_subinventory_code IS NOT NULL THEN
2536       -- search for the subinventory node
2537       l_found := find_child_node(
2538                           x_return_status      => l_return_status
2539                         , p_node_level         => g_sub_level
2540                         , p_tree_id            => p_tree_id
2541                         , p_revision           => p_revision
2542                         , p_lot_number         => p_lot_number
2543                         , p_subinventory_code  => p_subinventory_code
2544                         , p_is_reservable_sub  => p_is_reservable_sub
2545                         , p_locator_id         => NULL
2546                         , p_lpn_id             => NULL
2547                         , p_cost_group_id      => NULL
2548                         , x_child_index        => l_current_node_index
2549                         );
2550 
2551       IF l_return_status = fnd_api.g_ret_sts_error THEN
2552          RAISE fnd_api.g_exc_error;
2553       End IF ;
2554 
2555       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2556          RAISE fnd_api.g_exc_unexpected_error;
2557       End IF;
2558 
2559       IF l_found = FALSE THEN
2560          fnd_message.set_name('INV','INV-Cannot find node');
2561          fnd_message.set_token('ROUTINE', 'Find_Tree_Node');
2562          fnd_msg_pub.ADD;
2563          RAISE fnd_api.g_exc_unexpected_error;
2564       End IF;
2565 
2566       If l_child_node_index <> 0 Then
2567          if g_nodes(l_current_node_index).first_child_index = 0 then
2568            g_nodes(l_current_node_index).first_child_index := l_child_node_index;
2569            g_nodes(l_current_node_index).last_child_index := l_child_node_index;
2570          else
2571            l_last_child := g_nodes(l_current_node_index).last_child_index;
2572            g_nodes(l_last_child).next_sibling_Index := l_child_node_index;
2573            g_nodes(l_current_node_index).last_child_index := l_child_node_index;
2574          end if;
2575          g_nodes(l_child_node_index).parent_index := l_current_node_index;
2576       End If;
2577       l_child_node_index := l_current_node_index;
2578       If x_node_index = 0 Then
2579         x_node_index := l_current_node_index;
2580       End If;
2581       IF g_nodes(l_current_node_index).parent_index <> 0 THEN
2582          x_return_status := l_return_status;
2583          print_debug('-End of find_tree_node... x_node_index='||x_node_index||', current_node_index='||l_current_node_index||'.');
2584          RETURN TRUE;
2585       END IF;
2586    END IF;
2587 
2588    IF p_lot_number IS NOT NULL THEN
2589       -- search for the lot node
2590       l_found := find_child_node(
2591                           x_return_status      => l_return_status
2592                         , p_node_level         => g_lot_level
2593                         , p_tree_id            => p_tree_id
2594                         , p_revision           => p_revision
2595                         , p_lot_number         => p_lot_number
2596                         , p_subinventory_code  => NULL
2597                         , p_is_reservable_sub  => NULL
2598                         , p_locator_id         => NULL
2599                         , p_lpn_id             => NULL
2600                         , p_cost_group_id      => NULL
2601                         , x_child_index        => l_current_node_index
2602                         );
2603 
2604       IF l_return_status = fnd_api.g_ret_sts_error THEN
2605          RAISE fnd_api.g_exc_error;
2606       End IF ;
2607 
2608       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2609          RAISE fnd_api.g_exc_unexpected_error;
2610       End IF;
2611 
2612       IF l_found = FALSE THEN
2613          fnd_message.set_name('INV','INV-Cannot find node');
2614          fnd_message.set_token('ROUTINE', 'Find_Tree_Node');
2615          fnd_msg_pub.ADD;
2616          RAISE fnd_api.g_exc_unexpected_error;
2617       End IF;
2618 
2619       If l_child_node_index <> 0 Then
2620          if g_nodes(l_current_node_index).first_child_index = 0 then
2621            g_nodes(l_current_node_index).first_child_index := l_child_node_index;
2622            g_nodes(l_current_node_index).last_child_index := l_child_node_index;
2623          else
2624            l_last_child := g_nodes(l_current_node_index).last_child_index;
2625            g_nodes(l_last_child).next_sibling_Index := l_child_node_index;
2626            g_nodes(l_current_node_index).last_child_index := l_child_node_index;
2627          end if;
2628          g_nodes(l_child_node_index).parent_index := l_current_node_index;
2629       End If;
2630       l_child_node_index := l_current_node_index;
2631       If x_node_index = 0 Then
2632         x_node_index := l_current_node_index;
2633       End If;
2634       IF g_nodes(l_current_node_index).parent_index <> 0 THEN
2635          x_return_status := l_return_status;
2636          print_debug('+++End of find_tree_node... x_node_index='||x_node_index||', current_node_index='||l_current_node_index||'.');
2637          RETURN TRUE;
2638       END IF;
2639    END IF;
2640 
2641    IF p_revision IS NOT NULL THEN
2642       -- search for the revision node
2643       l_found := find_child_node(
2644                           x_return_status      => l_return_status
2645                         , p_node_level         => g_revision_level
2646                         , p_tree_id            => p_tree_id
2647                         , p_revision           => p_revision
2648                         , p_lot_number         => NULL
2649                         , p_subinventory_code  => NULL
2650                         , p_is_reservable_sub  => NULL
2651                         , p_locator_id         => NULL
2652                         , p_lpn_id             => NULL
2653                         , p_cost_group_id      => NULL
2654                         , x_child_index        => l_current_node_index
2655                         );
2656 
2657       IF l_return_status = fnd_api.g_ret_sts_error THEN
2658          RAISE fnd_api.g_exc_error;
2659       End IF ;
2660 
2661       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2662          RAISE fnd_api.g_exc_unexpected_error;
2663       End IF;
2664 
2665       IF l_found = FALSE THEN
2666          fnd_message.set_name('INV','INV-Cannot find node');
2667          fnd_message.set_token('ROUTINE', 'Find_Tree_Node');
2668          fnd_msg_pub.ADD;
2669          RAISE fnd_api.g_exc_unexpected_error;
2670       End IF;
2671 
2672       If l_child_node_index <> 0 Then
2673          if g_nodes(l_current_node_index).first_child_index = 0 then
2674            g_nodes(l_current_node_index).first_child_index := l_child_node_index;
2675            g_nodes(l_current_node_index).last_child_index := l_child_node_index;
2676          else
2677            l_last_child := g_nodes(l_current_node_index).last_child_index;
2678            g_nodes(l_last_child).next_sibling_Index := l_child_node_index;
2679            g_nodes(l_current_node_index).last_child_index := l_child_node_index;
2680          end if;
2681          g_nodes(l_child_node_index).parent_index := l_current_node_index;
2682       End If;
2683       l_child_node_index := l_current_node_index;
2684       If x_node_index = 0 Then
2685         x_node_index := l_current_node_index;
2686       End If;
2687       IF g_nodes(l_current_node_index).parent_index <> 0 THEN
2688          x_return_status := l_return_status;
2689          print_debug('++End of find_tree_node... x_node_index='||x_node_index||', current_node_index='||l_current_node_index||'.');
2690          RETURN TRUE;
2691       END IF;
2692    END IF;
2693 
2694    l_item_node := g_rootinfos(p_tree_id).item_node_index;
2695    IF l_current_node_index = 0 THEN
2696       x_node_index := l_item_node;
2697       x_return_status := l_return_status;
2698       print_debug('+End of find_tree_node... x_node_index='||x_node_index||', l_item_node='||l_item_node||', current_node_index='||l_current_node_index||'.');
2699       RETURN TRUE;
2700    END IF;
2701    IF g_nodes(l_item_node).first_child_index = 0 THEN
2702      g_nodes(l_item_node).first_child_index := l_current_node_index;
2703      g_nodes(l_item_node).last_child_index := l_current_node_index;
2704    ELSE
2705      l_last_child := g_nodes(l_item_node).last_child_index;
2706      g_nodes(l_last_child).next_sibling_index := l_current_node_index;
2707      g_nodes(l_item_node).last_child_index := l_current_node_index;
2708    END IF;
2709 
2710    g_nodes(l_current_node_index).parent_index := l_item_node;
2711 
2712    --bug 8593965, copy item's is_reservable_sub into revision's is_reservable_sub
2713    IF p_revision IS NOT NULL THEN
2714      g_nodes(l_current_node_index).is_reservable_sub := g_nodes(l_item_node).is_reservable_sub;
2715    END IF;
2716 
2717    print_debug('End of  find_tree_node... x_node_index='||x_node_index||', l_item_node='
2718       ||l_item_node||', current_node_index='||l_current_node_index||'.');
2719    print_debug('... parent_node='||l_item_node||', next_sibling='||g_nodes(l_current_node_index).next_sibling_index
2720       ||', last_child='||g_nodes(l_current_node_index).last_child_index||'.');
2721 
2722    x_return_status := l_return_status;
2723    RETURN TRUE;
2724 
2725 EXCEPTION
2726 
2727    WHEN fnd_api.g_exc_error THEN
2728         x_return_status := fnd_api.g_ret_sts_error;
2729    RETURN FALSE;
2730    WHEN fnd_api.g_exc_unexpected_error THEN
2731         x_return_status := fnd_api.g_ret_sts_unexp_error ;
2732    RETURN FALSE;
2733 
2734     WHEN OTHERS THEN
2735         x_return_status := fnd_api.g_ret_sts_unexp_error ;
2736 
2737         IF ((NOT g_is_pickrelease) OR nvl(g_debug,2) = 1) THEN
2738           IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
2739             THEN
2740              fnd_msg_pub.add_exc_msg
2741                (  g_pkg_name
2742                 , 'Find_Tree_Node'
2743                 );
2744           END IF;
2745         END IF;
2746         RETURN FALSE;
2747 END find_tree_node;
2748 
2749 -- Procedure
2750 --   add_quantities
2751 -- Description
2752 --   add quantities to the tree based on the input
2753 -- Input Parameters
2754 --   p_tree_id               tree id
2755 --   p_revision              item revision
2756 --   p_lot_number            lot number
2757 --   p_subinventory_code     subinventory code
2758 --   p_is_reservable_sub     whether the sub is a reservable.
2759 --                           this is needed to create the corresponding
2760 --                           sub node
2761 --                           if it has not been created
2762 --   p_locator_id            locator id
2763 --   p_primary_quantity      quantity to add in primary uom
2764 --   p_secondary_quantity    quantity to add in secondary uom
2765 --   p_quantity_type
2766 --   p_set_check_mark        whether to set do_check mark to the appropriate
2767 --                           nodes
2768 -- Output Parameters
2769 --   x_return_status         Standard Output Parameters
2770 
2771 -- Bug 2486318. The do check does not work. Trasactions get committed
2772 -- even if there is a node violation. Added p_check_mark_node_only to mark
2773 -- the nodes.
2774 
2775 PROCEDURE add_quantities
2776   (  x_return_status              OUT NOCOPY VARCHAR2
2777    , p_tree_id                    IN  INTEGER
2778    , p_revision                   IN  VARCHAR2
2779    , p_lot_number                 IN  VARCHAR2
2780    , p_subinventory_code          IN  VARCHAR2
2781    , p_is_reservable_sub          IN  BOOLEAN
2782    , p_locator_id                 IN  NUMBER
2783    , p_primary_quantity           IN  NUMBER
2784    , p_secondary_quantity         IN  NUMBER  DEFAULT NULL
2785    , p_quantity_type              IN  INTEGER
2786    , p_set_check_mark             IN  BOOLEAN
2787    , p_cost_group_id              IN  NUMBER
2788    , p_lpn_id                     IN  NUMBER
2789    , p_check_mark_node_only       IN  VARCHAR2 DEFAULT fnd_api.g_false
2790      --Bug 4294336 --Additional Parameters
2791    , p_transaction_action_id      IN  NUMBER   DEFAULT NULL
2792    , p_transfer_subinventory_code IN  VARCHAR2 DEFAULT NULL
2793    , p_transfer_locator_id        IN  NUMBER   DEFAULT NULL
2794    , p_expiration_date            IN  DATE     DEFAULT NULL -- Bug 7628989
2795    , p_is_reservable_lot          IN  NUMBER   DEFAULT NULL -- Bug 8713821
2796    ) IS
2797       l_return_status            VARCHAR2(1) := fnd_api.g_ret_sts_success;
2798       l_node_index               INTEGER;
2799       l_found                    BOOLEAN;
2800       l_is_reservable_sub        BOOLEAN;
2801       ll_is_reservable_sub       BOOLEAN;
2802       l_loop_index               INTEGER;
2803       l_sub_index                NUMBER;
2804       l_tree_mode                NUMBER;
2805       l_old_factor               NUMBER;
2806       l_old_factor2              NUMBER;
2807       l_new_factor               NUMBER;
2808       l_new_factor2              NUMBER;
2809       l_update_quantity          NUMBER;
2810       l_update_quantity2         NUMBER;
2811       l_atr_old_factor           NUMBER:=0;     -- Bug 9644285
2812       l_atr_new_factor           NUMBER:=0;     -- Bug 9644285
2813       l_atr_update_quantity      NUMBER:=0;     -- Bug 9644285
2814       l_atr_old_factor2          NUMBER:=0;     -- Bug 9644285
2815       l_atr_new_factor2          NUMBER:=0;     -- Bug 9644285
2816       l_atr_update_quantity2     NUMBER:=0;     -- Bug 9644285
2817       l_root_id                  INTEGER;
2818       l_org_item_index           NUMBER;
2819       l_tree_index               NUMBER;
2820       l_hash_string              VARCHAR2(1000);
2821       l_hash_base                NUMBER;
2822       l_hash_size                NUMBER;
2823       l_lpn_id                   NUMBER;
2824       l_qs_factor                NUMBER:= 0;    -- Bug 9644285
2825       l_qs_factor2               NUMBER:= 0;    -- Bug 9644285
2826       l_is_reservable_xfer_sub   BOOLEAN := FALSE;
2827       l_debug_line               VARCHAR2(300);
2828       l_api_name                 VARCHAR2(30) := 'ADD_QUANTITIES';
2829 BEGIN
2830    IF g_debug = 1 THEN
2831       print_debug('    '||l_api_name || ' Entered',9);
2832    END IF;
2833 
2834    IF p_is_reservable_sub THEN
2835       print_debug('... with p_is_reservable=TRUE');
2836    ELSIF p_is_reservable_sub = FALSE THEN
2837       print_debug('... with p_is_reservable=FALSE');
2838    ELSE
2839       print_debug('... with p_is_reservable=other');
2840    END IF;
2841 
2842    -- validate quantity type
2843    IF p_quantity_type <> g_qoh
2844      AND p_quantity_type <> g_qr_same_demand
2845      AND p_quantity_type <> g_qr_other_demand
2846      AND p_quantity_type <> g_qs_txn THEN
2847       -- invalid p_quantity_type. the caller's fault
2848       print_debug('... error=INVALID_QUANTITY_TYPE');
2849       fnd_message.set_name('INV', 'INV-INVALID_QUANTITY_TYPE');
2850       fnd_msg_pub.ADD;
2851       RAISE fnd_api.g_exc_error;
2852    END IF;
2853 
2854    l_hash_base := 1;
2855    l_hash_size := power(2, 20);
2856 
2857    l_found := find_tree_node(
2858                       x_return_status      => l_return_status
2859                     , p_tree_id            => p_tree_id
2860                     , p_revision           => p_revision
2861                     , p_lot_number         => p_lot_number
2862                     , p_subinventory_code  => p_subinventory_code
2863                     , p_is_reservable_sub  => p_is_reservable_sub
2864                     , p_locator_id         => p_locator_id
2865                     , x_node_index         => l_node_index
2866                     , p_cost_group_id      => p_cost_group_id
2867                     , p_lpn_id             => p_lpn_id
2868                     );
2869 
2870    IF l_return_status = fnd_api.g_ret_sts_error THEN
2871       RAISE fnd_api.g_exc_error;
2872    End IF ;
2873 
2874    IF  l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2875       RAISE fnd_api.g_exc_unexpected_error;
2876    END IF;
2877 
2878    IF l_found = FALSE THEN
2879       fnd_message.set_name('INV','INV-Cannot find node');
2880          fnd_message.set_token('ROUTINE', 'Add_Quantities');
2881       fnd_msg_pub.ADD;
2882       RAISE fnd_api.g_exc_unexpected_error;
2883    End IF;
2884 
2885    -- get tree mode
2886    -- only use this value to see if mode is loose or not
2887    --  rsv and txn mode are processed the same in this procedure
2888    l_tree_mode := g_rootinfos(p_tree_id).tree_mode;
2889 
2890    IF g_debug = 1 THEN
2891       l_debug_line := '      '||'for node: rev='||p_revision||' lot='||RPAD(p_lot_number,10)||' sub='||RPAD(p_subinventory_code,10)
2892          ||' loc='||RPAD(p_locator_id,6)||' lpn='||RPAD(p_lpn_id,6)||'..with qty type='||p_quantity_type||' qty='||RPAD(p_primary_quantity,6);
2893       print_debug(l_debug_line||' action='||p_transaction_action_id||' xfrsub='||p_transfer_subinventory_code||' xfrloc='||p_transfer_locator_id,9);
2894    END IF;
2895 
2896    -- process qoh
2897    print_debug('in add_qty, node_index='||l_node_index||', qtyType='||p_quantity_type||', g_qoh='||g_qoh
2898       ||', node_level='||g_nodes(l_node_index).node_level||'...');
2899    IF p_quantity_type = g_qoh THEN
2900       IF g_nodes(l_node_index).node_level <> g_locator_level
2901         AND g_nodes(l_node_index).node_level <> g_cost_group_level
2902         AND g_nodes(l_node_index).node_level <> g_sub_level
2903         AND g_nodes(l_node_index).node_level <> g_lpn_level
2904         THEN
2905          print_debug('... error=INV-WRONG_LEVEL');
2906          fnd_message.set_name('INV', 'INV-WRONG_LEVEL');
2907          fnd_msg_pub.ADD;
2908          RAISE fnd_api.g_exc_error;
2909       End IF;
2910 
2911       --bug 9380420, commenting the code below, as is_reservable_sub should be checked from g_nodes.
2912       /*
2913       l_sub_index := get_ancestor_sub(l_node_index);
2914       print_debug('after get_ancestor_sub... node_index='||l_node_index||', l_sub_index='||l_sub_index||', level='||g_nodes(l_sub_index).node_level);
2915 
2916       IF (INV_QUANTITY_TREE_PVT.g_is_mat_status_used = 2) AND p_is_reservable_sub IS NOT NULL THEN
2917          print_debug('in add_qty, p_is_reservable_sub=NOT NULL');
2918          l_is_reservable_sub := p_is_reservable_sub;
2919       ELSE
2920          print_debug('in add_qty, p_is_reservable_sub=NULL');
2921 
2922          print_debug('in add_qty, tree_id='||p_tree_id);
2923          print_debug('in add_qty, Calling check_is_reservable. item_id='||g_rootinfos(p_tree_id).inventory_item_id||', org='||g_rootinfos(p_tree_id).organization_id||', sub='||p_subinventory_code);
2924          check_is_reservable
2925               ( x_return_status       => l_return_status
2926               , p_inventory_item_id   => g_rootinfos(p_tree_id).inventory_item_id
2927               , p_organization_id     => g_rootinfos(p_tree_id).organization_id
2928               , p_subinventory_code   => p_subinventory_code
2929               , p_locator_id          => p_locator_id
2930               , p_lot_number          => p_lot_number
2931               , p_root_id             => p_tree_id
2932               , x_is_reservable       => l_is_reservable_sub
2933               , p_lpn_id              => p_lpn_id); -- Onhand Material Status Support
2934          print_debug('in add_qty, after check_is_reservable. return_status='||l_return_status);
2935 
2936          IF l_return_status = fnd_api.g_ret_sts_error THEN
2937             RAISE fnd_api.g_exc_error;
2938          End IF ;
2939 
2940          IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2941             RAISE fnd_api.g_exc_unexpected_error;
2942          End IF;
2943       END IF;  -- p_is_reservable_sub = NULL
2944 
2945 
2946       g_nodes(l_sub_index).is_reservable_sub := l_is_reservable_sub;
2947       */
2948       l_is_reservable_sub := g_nodes(l_node_index).is_reservable_sub;
2949 
2950       l_loop_index := l_node_index;
2951       LOOP
2952          IF g_debug = 1 THEN
2953             l_debug_line := '      '||'Old: Node: '||g_nodes(l_loop_index).node_level||' :'||lpad(l_loop_index,10)||':'||lpad(g_nodes(l_loop_index).qoh,8)||':'||lpad(g_nodes(l_loop_index).rqoh,8);
2954             print_debug(l_debug_line||':'||lpad(g_nodes(l_loop_index).qr,8)||':'||lpad(g_nodes(l_loop_index).qs,8)||':'||lpad(g_nodes(l_loop_index).att,8)||':'||lpad(g_nodes(l_loop_index).atr,8),12);
2955          END IF;
2956 
2957          print_debug('loop1, node_level='||g_nodes(l_loop_index).node_level);
2958          print_debug('loop1, node_index='||l_loop_index||' qoh='||g_nodes(l_loop_index).qoh||' rqoh='||g_nodes(l_loop_index).rqoh
2959            ||' qr='||g_nodes(l_loop_index).qr||' qs='||g_nodes(l_loop_index).qs);
2960          print_debug(' ... att='||g_nodes(l_loop_index).att||' atr='||g_nodes(l_loop_index).atr);
2961          print_debug('.. sqoh='||g_nodes(l_loop_index).sqoh||' srqoh='||g_nodes(l_loop_index).srqoh||' sqr='||g_nodes(l_loop_index).sqr
2962            ||' sqs='||g_nodes(l_loop_index).sqs);
2963          print_debug(' .... satt='||g_nodes(l_loop_index).satt||' satr='||g_nodes(l_loop_index).satr);
2964 
2965          -- update qoh
2966          g_nodes(l_loop_index).qoh := g_nodes(l_loop_index).qoh + p_primary_quantity;
2967          g_nodes(l_loop_index).sqoh := NVL(g_nodes(l_loop_index).sqoh, 0) + p_secondary_quantity;
2968 
2969          -- Bug 7211383, 7628989, 8713821, 9150005
2970          IF l_is_reservable_sub = TRUE AND (p_expiration_date IS NULL OR
2971                   g_rootinfos(p_tree_id).lot_expiration_date IS NULL OR
2972                   p_expiration_date > g_rootinfos(p_tree_id).lot_expiration_date)THEN
2973          -- Bug 13387319
2974             IF ((g_rootinfos(p_tree_id).onhand_status_enabled and (p_is_reservable_lot = 1 OR p_is_reservable_lot is null OR g_is_mat_status_used = 2)) OR
2975                   ( (g_is_mat_status_used = 2) OR
2976                     (g_is_mat_status_used = 1 AND p_is_reservable_lot = 1 ) OR
2977                     (g_is_mat_status_used = 1 AND p_is_reservable_lot is null)))
2978             THEN
2979                print_debug('in add_qty,0 reservable_flag=TRUE');
2980                -- update rqoh
2981                g_nodes(l_loop_index).rqoh := g_nodes(l_loop_index).rqoh + p_primary_quantity;
2982                g_nodes(l_loop_index).srqoh := NVL(g_nodes(l_loop_index).srqoh, 0) + p_secondary_quantity;
2983             END IF;
2984          ELSE
2985             print_debug('in add_qty,0 reservable_flag=FALSE');
2986          END IF;
2987 
2988          --update att/atr for containerized items;
2989          --track packed quantity on hand only in loose only mode;
2990          --see design document for detailed info on how att/atr is
2991          -- calculated in loose only mode;
2992          IF l_tree_mode = g_loose_only_mode AND p_lpn_id IS NOT NULL  THEN
2993 
2994             --l_old factor = QS+QR-PQOH
2995             l_old_factor := g_nodes(l_loop_index).qs + g_nodes(l_loop_index).qr - g_nodes(l_loop_index).pqoh;
2996             l_old_factor2 := g_nodes(l_loop_index).sqs + g_nodes(l_loop_index).sqr - g_nodes(l_loop_index).spqoh;
2997 
2998             --l_new factor = QS+QR-(PQOH+(change in PQOH))
2999             l_new_factor := l_old_factor - p_primary_quantity;
3000             l_new_factor2 := NVL(l_old_factor2, 0) - p_secondary_quantity;
3001 
3002 
3003             -- update_quantity is amount to update att/atr by
3004             -- Base calculation is update_quantity =
3005             --    max(old_factor, 0) - max(new_factor, 0)
3006             -- We have to deal with four possible cases:
3007             --  1. old_factor and new_factor >0
3008             --  2. old_factor >0 and new_factor <0
3009             --  3. old_factor <0 and new_factor >0
3010             --  4. old_factor and new_factor <0
3011             -- for these four cases, the att and atr are updated differently
3012             IF l_old_factor > 0 THEN
3013                IF l_new_factor > 0 THEN
3014                   --old_factor - new_factor
3015                   l_update_quantity:= p_primary_quantity;
3016                   l_update_quantity2 := p_secondary_quantity;
3017 
3018                ELSE  -- old factor > 0, new factor <= 0
3019                   --old_factor - 0  (max(new_factor, 0) = 0)
3020                   l_update_quantity := l_old_factor;
3021                   l_update_quantity2 := l_old_factor2;
3022                END IF;
3023             ELSE -- l_old_factor <=0
3024                IF l_new_factor > 0 THEN
3025                   -- -new_factor  (max(old_factor, 0) = 0)
3026                   l_update_quantity:= 0.0 - l_new_factor;
3027                   l_update_quantity2 := 0.0 - l_new_factor2;
3028                ELSE  -- old factor < 0, new factor <= 0
3029                   -- 0-0
3030                   l_update_quantity:= 0;
3031                   l_update_quantity2 := 0;
3032                END IF;
3033             END IF;
3034 
3035              /*********** Bug 9644285 fix ******************/
3036                  -- atr_update_quantity is amount to update atr by
3037                  -- if reservable packed quantity  >= quantity reserved
3038                  --    use existing calculation
3039                  --    old_atr_factor = qs + qr - pqoh
3040                  --    new_atr_factor = qs + qr - pqoh - pq
3041                  -- else
3042                  --    old_atr_factor = (qr-rpqoh) + max(0,qs-(pqoh-rpqoh))
3043                  --    if reservable qty
3044                  --      new_atr_factor = old_atr_factor - pq
3045                  --    else
3046                  --      new_factor = (qr-rpqoh) + max(0,(qs-(pqoh-rpqoh)+pq))
3047                  --    end if
3048                  -- end if
3049                  -- Base calculation is update_quantity =
3050                  -- max(old_factor, 0) - max(new_factor, 0)
3051                  -- We have to deal with four possible cases:
3052                  --    1. old_factor and new_factor >0
3053                  --    2. old_factor >0 and new_factor <0
3054                  --    3. old_factor <0 and new_factor >0
3055                  --    4. old_factor and new_factor <0
3056                  --    for these four cases, the atr is updated differently
3057 
3058 
3059                  IF(g_nodes(l_loop_index).rpqoh >= g_nodes(l_loop_index).qr) THEN
3060                     l_atr_old_factor := g_nodes(l_loop_index).qs +
3061                                         g_nodes(l_loop_index).qr -
3062                                         g_nodes(l_loop_index).pqoh;
3063                     l_atr_new_factor := l_atr_old_factor - p_primary_quantity;
3064 
3065                   ELSE
3066 
3067                     l_qs_factor := 0;
3068                     IF ((g_nodes(l_loop_index).qs -(g_nodes(l_loop_index).pqoh - g_nodes(l_loop_index).rpqoh)) > 0)
3069                       THEN
3070                        l_qs_factor := g_nodes(l_loop_index).qs -(g_nodes(l_loop_index).pqoh - g_nodes(l_loop_index).rpqoh);
3071                     END IF;
3072                     l_atr_old_factor :=
3073                       (g_nodes(l_loop_index).qr - g_nodes(l_loop_index).rpqoh) + l_qs_factor;
3074 
3075 
3076                     IF(l_is_reservable_sub) THEN
3077                       l_atr_new_factor := l_atr_old_factor - p_primary_quantity;
3078                      ELSE
3079                        l_qs_factor := 0;
3080                        IF ((g_nodes(l_loop_index).qs -(g_nodes(l_loop_index).pqoh - g_nodes(l_loop_index).rpqoh + p_primary_quantity)) > 0)
3081                          THEN
3082                           l_qs_factor := g_nodes(l_loop_index).qs -(g_nodes(l_loop_index).pqoh -
3083                                                                     g_nodes(l_loop_index).rpqoh+ p_primary_quantity);
3084                        END IF;
3085                        l_atr_new_factor :=
3086                          (g_nodes(l_loop_index).qr - g_nodes(l_loop_index).rpqoh) + l_qs_factor;
3087                     END IF;
3088                  END IF;
3089 -- inv converge
3090                  IF(g_nodes(l_loop_index).srpqoh >= g_nodes(l_loop_index).sqr) THEN
3091                     l_atr_old_factor2 := g_nodes(l_loop_index).sqs +
3092                                         g_nodes(l_loop_index).sqr -
3093                                         g_nodes(l_loop_index).spqoh;
3094                     l_atr_new_factor2 := l_atr_old_factor2 - p_secondary_quantity;
3095 
3096                   ELSE
3097 
3098                     l_qs_factor2 := 0;
3099                     IF ((g_nodes(l_loop_index).sqs -(g_nodes(l_loop_index).spqoh - g_nodes(l_loop_index).srpqoh)) > 0)
3100                       THEN
3101                        l_qs_factor2 := g_nodes(l_loop_index).sqs -(g_nodes(l_loop_index).spqoh - g_nodes(l_loop_index).srpqoh);
3102                     END IF;
3103                     l_atr_old_factor2 :=
3104                       (g_nodes(l_loop_index).sqr - g_nodes(l_loop_index).srpqoh) + l_qs_factor2;
3105 
3106 
3107                     IF(l_is_reservable_sub) THEN
3108                       l_atr_new_factor2 := l_atr_old_factor2 - p_secondary_quantity;
3109                      ELSE
3110                        l_qs_factor2 := 0;
3111                        IF ((g_nodes(l_loop_index).sqs -(g_nodes(l_loop_index).spqoh - g_nodes(l_loop_index).srpqoh + p_secondary_quantity)) > 0)
3112                          THEN
3113                           l_qs_factor2 := g_nodes(l_loop_index).sqs -(g_nodes(l_loop_index).spqoh -
3114                                                                     g_nodes(l_loop_index).srpqoh+ p_secondary_quantity);
3115                        END IF;
3116                        l_atr_new_factor2 :=
3117                          (g_nodes(l_loop_index).sqr - g_nodes(l_loop_index).srpqoh) + l_qs_factor2;
3118                     END IF;
3119                  END IF;
3120 
3121                  -- update_quantity is amount to update att/atr by
3122                  -- Base calculation is update_quantity =
3123                  --    max(old_factor, 0) - max(new_factor, 0)
3124                  -- We have to deal with four possible cases:
3125                  --  1. old_factor and new_factor >0
3126                  --  2. old_factor >0 and new_factor <0
3127                  --  3. old_factor <0 and new_factor >0
3128                  --  4. old_factor and new_factor <0
3129                  -- for these four cases, the att and atr are updated
3130                  -- differently
3131                  l_atr_update_quantity:=0;
3132                  if l_atr_old_factor > 0 then
3133                     IF l_atr_new_factor > 0 THEN
3134                        --atr_old_factor - atr_new_factor
3135                        l_atr_update_quantity:= l_atr_old_factor - l_atr_new_factor ;
3136                     ELSE  -- atr_old factor > 0, atr_new factor <= 0
3137                        --atr_old_factor - 0  (max(atr_new_factor, 0) = 0)
3138                        l_atr_update_quantity := l_atr_old_factor;
3139                     END IF;
3140                   else -- l_atr_old_factor <=0
3141                     IF l_atr_new_factor > 0 THEN
3142                        -- -atr_new_factor  (max(atr_old_factor, 0) = 0)
3143                        l_atr_update_quantity:= 0.0 - l_atr_new_factor;
3144                     ELSE  -- atr_old factor < 0, atr_new factor <= 0
3145                        -- 0-0
3146                        l_atr_update_quantity:= 0;
3147                     END IF;
3148                  end if;
3149 
3150 -- inv converge
3151                  l_atr_update_quantity2:=0;
3152                  if l_atr_old_factor2 > 0 then
3153                     IF l_atr_new_factor2 > 0 THEN
3154                        --atr_old_factor - atr_new_factor
3155                        l_atr_update_quantity2:= l_atr_old_factor2 - l_atr_new_factor2 ;
3156                     ELSE  -- atr_old factor > 0, atr_new factor <= 0
3157                        --atr_old_factor - 0  (max(atr_new_factor, 0) = 0)
3158                        l_atr_update_quantity2 := l_atr_old_factor2;
3159                     END IF;
3160                   else -- l_atr_old_factor <=0
3161                     IF l_atr_new_factor2 > 0 THEN
3162                        -- -atr_new_factor  (max(atr_old_factor, 0) = 0)
3163                        l_atr_update_quantity2:= 0.0 - l_atr_new_factor2;
3164                     ELSE  -- atr_old factor < 0, atr_new factor <= 0
3165                        -- 0-0
3166                        l_atr_update_quantity2:= 0;
3167                     END IF;
3168                  end if;
3169 
3170                   /*********** Bug 9644285 fix ******************/
3171 
3172             --update pqoh
3173             g_nodes(l_loop_index).pqoh := g_nodes(l_loop_index).pqoh + p_primary_quantity;
3174             g_nodes(l_loop_index).spqoh := NVL(g_nodes(l_loop_index).spqoh, 0) + p_secondary_quantity;
3175 
3176             --update att
3177             g_nodes(l_loop_index).att := g_nodes(l_loop_index).att + l_update_quantity;
3178             g_nodes(l_loop_index).satt := NVL(g_nodes(l_loop_index).satt, 0) + l_update_quantity2;
3179 
3180             --update atr
3181             g_nodes(l_loop_index).atr := g_nodes(l_loop_index).atr + l_atr_update_quantity;     -- Bug 9644285
3182             g_nodes(l_loop_index).satr := g_nodes(l_loop_index).satr + l_atr_update_quantity2;  -- Bug 9644285
3183 
3184             -- Bug 7211383, 7628989, 8713821, 9150005
3185             IF l_is_reservable_sub = TRUE AND (p_expiration_date IS NULL OR
3186                   g_rootinfos(p_tree_id).lot_expiration_date IS NULL OR
3187                   p_expiration_date > g_rootinfos(p_tree_id).lot_expiration_date)THEN
3188             -- Bug 13387319
3189                IF ((g_rootinfos(p_tree_id).onhand_status_enabled and (p_is_reservable_lot = 1 OR p_is_reservable_lot is null OR g_is_mat_status_used = 2)) OR
3190                   ( (g_is_mat_status_used = 2) OR
3191                     (g_is_mat_status_used = 1 AND p_is_reservable_lot = 1 ) OR
3192                     (g_is_mat_status_used = 1 AND p_is_reservable_lot is null)))
3193                THEN
3194                g_nodes(l_loop_index).rpqoh := g_nodes(l_loop_index).rpqoh + p_primary_quantity;                 -- Bug 9644285
3195                g_nodes(l_loop_index).srpqoh := NVL(g_nodes(l_loop_index).srpqoh, 0) + p_secondary_quantity;     -- Bug 9644285  -- invConv change
3196                END IF ;
3197             ELSE
3198                print_debug('in add_qty,1 rsv=FALSE, newATR='||g_nodes(l_loop_index).atr||' + 0');
3199             END IF;
3200          ELSE -- not in loose items only mode, or quantity not containerized
3201             --update att
3202             g_nodes(l_loop_index).att := g_nodes(l_loop_index).att + p_primary_quantity;
3203             g_nodes(l_loop_index).satt := NVL(g_nodes(l_loop_index).satt, 0) + p_secondary_quantity;
3204 
3205             -- Bug 7211383, 7628989, 8713821, 9150005
3206             IF l_is_reservable_sub = TRUE AND (p_expiration_date IS NULL OR
3207                      g_rootinfos(p_tree_id).lot_expiration_date IS NULL OR
3208                      p_expiration_date > g_rootinfos(p_tree_id).lot_expiration_date)THEN
3209             -- Bug 13387319
3210                IF ((g_rootinfos(p_tree_id).onhand_status_enabled and (p_is_reservable_lot = 1 OR p_is_reservable_lot is null OR g_is_mat_status_used = 2)) OR
3211                   ( (g_is_mat_status_used = 2) OR
3212                     (g_is_mat_status_used = 1 AND p_is_reservable_lot = 1 ) OR
3213                     (g_is_mat_status_used = 1 AND p_is_reservable_lot is null)))
3214                THEN
3215                   print_debug('in add_qty,2 rsv=TRUE, newATR='||g_nodes(l_loop_index).atr||' + '||p_primary_quantity);
3216                   --update atr
3217                   g_nodes(l_loop_index).atr := g_nodes(l_loop_index).atr + p_primary_quantity;
3218                   g_nodes(l_loop_index).satr := NVL(g_nodes(l_loop_index).satr, 0) + p_secondary_quantity;
3219                END IF ;
3220             ELSE
3221                print_debug('in add_qty,2 rsv=FALSE, newATR='||g_nodes(l_loop_index).atr||' + 0');
3222             END IF;
3223          END IF;
3224          -- set check mark
3225 
3226          -- Bug 2486318. The do check does not work. Trasactions get committed
3227          -- even if there is a node violation. Added p_check_mark_node_only to mark the nodes.
3228 
3229          IF (p_set_check_mark = TRUE or
3230              p_check_mark_node_only = fnd_api.g_true AND
3231              p_primary_quantity < 0 )THEN
3232             g_nodes(l_loop_index).check_mark := TRUE;
3233          END IF;
3234 
3235          IF g_debug = 1 THEN
3236             l_debug_line := '      '||'New: Node: '||g_nodes(l_loop_index).node_level||' :'||lpad(l_loop_index,10)||':'||lpad(g_nodes(l_loop_index).qoh,8)||':'||lpad(g_nodes(l_loop_index).rqoh,8);
3237             print_debug(l_debug_line||':'||lpad(g_nodes(l_loop_index).qr,8)||':'||lpad(g_nodes(l_loop_index).qs,8)||':'||lpad(g_nodes(l_loop_index).att,8)||':'||lpad(g_nodes(l_loop_index).atr,8),12);
3238          END IF;
3239 
3240          IF (g_nodes(l_loop_index).node_level = g_item_level) THEN
3241             EXIT;
3242          END IF;
3243 
3244          l_loop_index := g_nodes(l_loop_index).parent_index;
3245       END LOOP;
3246 
3247 
3248     --process reservations
3249     ELSIF p_quantity_type = g_qr_same_demand
3250       OR p_quantity_type = g_qr_other_demand THEN
3251       print_debug('in add_qty, process reservation');
3252 
3253         --  l_is_reservable_sub := g_nodes(l_node_index).is_reservable_sub;
3254 	--  added as part of the bug 14372032
3255 
3256       l_loop_index := l_node_index;
3257       LOOP
3258          IF g_debug = 1 THEN
3259             l_debug_line := '      '||'Old: Node: '||g_nodes(l_loop_index).node_level||' :'||lpad(l_loop_index,10)||':'||lpad(g_nodes(l_loop_index).qoh,8)||':'||lpad(g_nodes(l_loop_index).rqoh,8);
3260             print_debug(l_debug_line||':'||lpad(g_nodes(l_loop_index).qr,8)||':'||lpad(g_nodes(l_loop_index).qs,8)||':'||lpad(g_nodes(l_loop_index).att,8)||':'||lpad(g_nodes(l_loop_index).atr,8),12);
3261          END IF;
3262 
3263          print_debug('loop2, node_index='||l_loop_index||' qoh='||g_nodes(l_loop_index).qoh||' rqoh='||g_nodes(l_loop_index).rqoh||' qr='||g_nodes(l_loop_index).qr||' qs='||g_nodes(l_loop_index).qs);
3264          print_debug(' ... att='||g_nodes(l_loop_index).att||' atr='||g_nodes(l_loop_index).atr);
3265          print_debug('.. sqoh='||g_nodes(l_loop_index).sqoh||' srqoh='||g_nodes(l_loop_index).srqoh||' sqr='||g_nodes(l_loop_index).sqr||' sqs='||g_nodes(l_loop_index).sqs);
3266          print_debug(' .... satt='||g_nodes(l_loop_index).satt||' satr='||g_nodes(l_loop_index).satr);
3267          print_debug(' .... pqoh='||g_nodes(l_loop_index).pqoh);
3268 
3269          --update att/atr for containerized items
3270          IF l_tree_mode = g_loose_only_mode THEN
3271             --old factor = QS+QR-PQOH
3272             l_old_factor := g_nodes(l_loop_index).qs + g_nodes(l_loop_index).qr - g_nodes(l_loop_index).pqoh;
3273             l_old_factor2 := g_nodes(l_loop_index).sqs + g_nodes(l_loop_index).sqr - g_nodes(l_loop_index).spqoh;
3274 
3275             --new factor = QS+QR+(change to QR)-PQOH
3276             l_new_factor := l_old_factor + p_primary_quantity;
3277             l_new_factor2 := NVL(l_old_factor2, 0) + p_secondary_quantity;
3278 
3279             if l_old_factor > 0 then
3280                IF l_new_factor > 0 THEN
3281                   l_update_quantity:= 0.0 - p_primary_quantity;
3282                   l_update_quantity2:= 0.0 - p_secondary_quantity;
3283 
3284                ELSE  -- old factor > 0, new factor <= 0
3285                   l_update_quantity := l_old_factor;
3286                   l_update_quantity2 := l_old_factor2;
3287                END IF;
3288             else -- l_old_factor <=0
3289                IF l_new_factor > 0 THEN
3290                   l_update_quantity:= 0.0 - l_new_factor;
3291                   l_update_quantity2:= 0.0 - l_new_factor2;
3292                ELSE  -- old factor < 0, new factor <= 0
3293                   l_update_quantity:= 0;
3294                   l_update_quantity2:= 0;
3295                END IF;
3296             end if;
3297 
3298                  /*********** Bug 9644285 fix ******************/
3299                   -- atr_update_quantity is amount to update atr by
3300                  -- if reservable packed quantity  >= quantity reserved
3301                  --    use existing calculation
3302                  --    old_atr_factor = qs + qr - pqoh
3303                  --    new_atr_factor = qs + qr - pqoh - pq
3304                  -- else
3305                  --    old_atr_factor = (qr-rpqoh) + max(0,qs-(pqoh-rpqoh))
3306                  --    if reservable qty
3307                  --      new_atr_factor = old_atr_factor - pq
3308                  --    else
3309                  --      new_factor = (qr-rpqoh) + max(0,(qs-(pqoh-rpqoh)+pq))
3310                  --    end if
3311                  -- end if
3312                  -- Base calculation is update_quantity =
3313                  -- max(old_factor, 0) - max(new_factor, 0)
3314                  -- We have to deal with four possible cases:
3315                  --    1. old_factor and new_factor >0
3316                  --    2. old_factor >0 and new_factor <0
3317                  --    3. old_factor <0 and new_factor >0
3318                  --    4. old_factor and new_factor <0
3319                  --    for these four cases, the atr is updated differently
3320 
3321                  IF(g_nodes(l_loop_index).rpqoh >= g_nodes(l_loop_index).qr) THEN
3322                     l_atr_old_factor := g_nodes(l_loop_index).qs +
3323                                         g_nodes(l_loop_index).qr -
3324                                         g_nodes(l_loop_index).pqoh;
3325                     l_atr_new_factor := l_atr_old_factor + p_primary_quantity;
3326                   ELSE
3327                     l_qs_factor := 0;
3328                     IF ((g_nodes(l_loop_index).qs - (g_nodes(l_loop_index).pqoh - g_nodes(l_loop_index).rpqoh)) > 0)
3329                          THEN
3330                           l_qs_factor := g_nodes(l_loop_index).qs - (g_nodes(l_loop_index).pqoh - g_nodes(l_loop_index).rpqoh);
3331                        END IF;
3332                     l_atr_old_factor :=
3333                       (g_nodes(l_loop_index).qr - g_nodes(l_loop_index).rpqoh) + l_qs_factor;
3334 
3335                       l_atr_new_factor := l_atr_old_factor + p_primary_quantity;
3336                  END IF;
3337 
3338      -- inv converge
3339                  IF(g_nodes(l_loop_index).srpqoh >= g_nodes(l_loop_index).sqr) THEN
3340                     l_atr_old_factor2 := g_nodes(l_loop_index).sqs +
3341                                         g_nodes(l_loop_index).sqr -
3342                                         g_nodes(l_loop_index).spqoh;
3343                     l_atr_new_factor2 := l_atr_old_factor2 + p_secondary_quantity;
3344                   ELSE
3345                     l_qs_factor2 := 0;
3346                     IF ((g_nodes(l_loop_index).sqs - (g_nodes(l_loop_index).spqoh - g_nodes(l_loop_index).srpqoh)) > 0)
3347                          THEN
3348                           l_qs_factor2 := g_nodes(l_loop_index).sqs - (g_nodes(l_loop_index).spqoh - g_nodes(l_loop_index).srpqoh);
3349                        END IF;
3350                     l_atr_old_factor2 :=
3351                       (g_nodes(l_loop_index).sqr - g_nodes(l_loop_index).srpqoh) + l_qs_factor2;
3352 
3353                       l_atr_new_factor2 := l_atr_old_factor2 + p_secondary_quantity;
3354                  END IF;
3355 
3356                  l_atr_update_quantity:= 0;
3357                  if l_atr_old_factor > 0 then
3358                     IF l_atr_new_factor > 0 THEN
3359                        l_atr_update_quantity:= 0.0 - p_primary_quantity;
3360                     ELSE  -- atr_old factor > 0, atr_new factor <= 0
3361                        l_atr_update_quantity := l_atr_old_factor;
3362                     END IF;
3363                  else -- l_atr_old_factor <=0
3364                     IF l_atr_new_factor > 0 THEN
3365                        l_atr_update_quantity:= 0.0 - l_atr_new_factor;
3366                     ELSE  -- atr_old factor < 0, atr_new factor <= 0
3367                        l_atr_update_quantity:= 0;
3368                     END IF;
3369                  end if;
3370 
3371      -- inv converge
3372                  l_atr_update_quantity2:= 0;
3373                  if l_atr_old_factor2 > 0 then
3374                     IF l_atr_new_factor2 > 0 THEN
3375                        l_atr_update_quantity2:= 0.0 - p_secondary_quantity;
3376                     ELSE  -- atr_old factor > 0, atr_new factor <= 0
3377                        l_atr_update_quantity2 := l_atr_old_factor2;
3378                     END IF;
3379                  else -- l_atr_old_factor <=0
3380                     IF l_atr_new_factor2 > 0 THEN
3381                        l_atr_update_quantity2:= 0.0 - l_atr_new_factor2;
3382                     ELSE  -- atr_old factor < 0, atr_new factor <= 0
3383                        l_atr_update_quantity2:= 0;
3384                     END IF;
3385                  end if;
3386                  /*********** Bug 9644285 fix ******************/
3387 
3388             -- update qr
3389             g_nodes(l_loop_index).qr := g_nodes(l_loop_index).qr + p_primary_quantity;
3390             g_nodes(l_loop_index).sqr := NVL(g_nodes(l_loop_index).sqr, 0) + p_secondary_quantity;
3391 
3392             --update atr
3393             if g_nodes(l_loop_index).is_reservable_sub then
3394                print_debug('in add_qty,X rsv=TRUE, newATR='||g_nodes(l_loop_index).atr||' + '||l_update_quantity);
3395             else
3396                print_debug('in add_qty,X rsv=FALSE, newATR='||g_nodes(l_loop_index).atr||' + '||l_update_quantity);
3397             end if;
3398 
3399   /*          IF l_is_reservable_sub = TRUE AND (p_expiration_date IS NULL OR
3400                         g_rootinfos(p_tree_id).lot_expiration_date IS NULL OR
3401                          p_expiration_date > g_rootinfos(p_tree_id).lot_expiration_date)THEN
3402                          if ((g_rootinfos(p_tree_id).onhand_status_enabled and (p_is_reservable_lot = 1 OR p_is_reservable_lot is null OR g_is_mat_status_used = 2)) OR
3403                             ( (g_is_mat_status_used = 2) OR
3404                              (g_is_mat_status_used = 1 AND p_is_reservable_lot = 1 ) OR
3405                              (g_is_mat_status_used = 1 AND p_is_reservable_lot is null)))
3406                          then                -- if condition added for bug  14372032
3407   */
3408 
3409             g_nodes(l_loop_index).atr := g_nodes(l_loop_index).atr + l_atr_update_quantity;     -- Bug 9644285
3410             g_nodes(l_loop_index).satr := NVL(g_nodes(l_loop_index).satr, 0) + l_atr_update_quantity2;   -- Bug 9644285
3411 
3412   /*                       end if;
3413             END IF;
3414   */
3415             IF p_quantity_type = g_qr_other_demand
3416                -- we should also update att if the reservation qty is negative
3417                OR p_primary_quantity < 0 THEN
3418                -- update att
3419                g_nodes(l_loop_index).att := g_nodes(l_loop_index).att + l_update_quantity;
3420                g_nodes(l_loop_index).satt := NVL(g_nodes(l_loop_index).satt, 0) + l_update_quantity2;
3421             END IF;
3422          ELSE -- not in loose items only mode
3423             -- update qr
3424             g_nodes(l_loop_index).qr := g_nodes(l_loop_index).qr + p_primary_quantity;
3425             g_nodes(l_loop_index).sqr := NVL(g_nodes(l_loop_index).sqr, 0) + p_secondary_quantity;
3426 
3427             --update atr
3428             if g_nodes(l_loop_index).is_reservable_sub then
3429                print_debug('in add_qty,Y rsv=TRUE, newATR='||g_nodes(l_loop_index).atr||' + '||p_primary_quantity);
3430             else
3431                print_debug('in add_qty,Y rsv=FALSE, newATR='||g_nodes(l_loop_index).atr||' + '||p_primary_quantity);
3432             end if;
3433 
3434      /*      IF l_is_reservable_sub = TRUE AND (p_expiration_date IS NULL OR
3435                         g_rootinfos(p_tree_id).lot_expiration_date IS NULL OR
3436                          p_expiration_date > g_rootinfos(p_tree_id).lot_expiration_date)THEN
3437                          if ((g_rootinfos(p_tree_id).onhand_status_enabled and (p_is_reservable_lot = 1 OR p_is_reservable_lot is null OR g_is_mat_status_used = 2)) OR
3438                             ( (g_is_mat_status_used = 2) OR
3439                              (g_is_mat_status_used = 1 AND p_is_reservable_lot = 1 ) OR
3440                              (g_is_mat_status_used = 1 AND p_is_reservable_lot is null)))
3441                          then                    -- if condition added for bug  14372032
3442 
3443     */
3444             g_nodes(l_loop_index).atr := g_nodes(l_loop_index).atr - p_primary_quantity;
3445             g_nodes(l_loop_index).satr := NVL(g_nodes(l_loop_index).satr, 0) - p_secondary_quantity;
3446 
3447 
3448     /*			end if;
3449             END IF;
3450     */
3451 
3452             IF p_quantity_type = g_qr_other_demand
3453             -- we should also update att if the reservation qty is negative
3454                OR p_primary_quantity < 0 THEN
3455                -- update att
3456                g_nodes(l_loop_index).att := g_nodes(l_loop_index).att - p_primary_quantity;
3457                g_nodes(l_loop_index).satt := NVL(g_nodes(l_loop_index).satt, 0) - p_secondary_quantity;
3458             END IF;
3459          END IF;
3460 
3461          -- only set do check mark if we are reserving materials
3462 
3463          -- Bug 2486318. The do check does not work. Trasactions get committed
3464          -- even if there is a node violation. Added p_check_mark_node_only to mark the nodes.
3465 
3466          IF (p_set_check_mark = TRUE or
3467              p_check_mark_node_only = fnd_api.g_true AND p_primary_quantity >0) THEN
3468             g_nodes(l_loop_index).check_mark := TRUE;
3469          END IF;
3470 
3471          IF g_debug = 1 THEN
3472             l_debug_line := '      '||'New: Node: '||g_nodes(l_loop_index).node_level||' :'||lpad(l_loop_index,10)||':'||lpad(g_nodes(l_loop_index).qoh,8)||':'||lpad(g_nodes(l_loop_index).rqoh,8);
3473             print_debug(l_debug_line||':'||lpad(g_nodes(l_loop_index).qr,8)||':'||lpad(g_nodes(l_loop_index).qs,8)||':'||lpad(g_nodes(l_loop_index).att,8)||':'||lpad(g_nodes(l_loop_index).atr,8),12);
3474          END IF;
3475 
3476          EXIT WHEN g_nodes(l_loop_index).node_level = g_item_level;
3477 
3478          l_loop_index := g_nodes(l_loop_index).parent_index;
3479       END LOOP;
3480 
3481 
3482    --process suggestions
3483    ELSIF p_quantity_type = g_qs_txn THEN
3484       IF g_nodes(l_node_index).node_level <> g_locator_level
3485         AND g_nodes(l_node_index).node_level <> g_cost_group_level
3486         AND g_nodes(l_node_index).node_level <> g_sub_level
3487         AND g_nodes(l_node_index).node_level <> g_lpn_level
3488         THEN
3489          fnd_message.set_name('INV', 'INV-WRONG_LEVEL');
3490          fnd_msg_pub.ADD;
3491          RAISE fnd_api.g_exc_error;
3492       End IF;
3493 
3494       -- need to find out whether the sub is reservable or not
3495       -- in order to compute atr
3496       -- we need to look at is_reservable_sub from the sub node
3497       -- since it is possible that p_is_reservable_sub is null
3498       -- and is_reservable_sub at the sub node is not null
3499 
3500       --bug 9380420, commenting the code below, as is_reservable_sub should be checked from g_nodes.
3501       /*
3502       print_debug('in add_qty, process suggestion');
3503       l_sub_index := get_ancestor_sub(l_node_index);
3504       print_debug('..after get_ancestor_sub... node_index='||l_node_index||', l_sub_index='||l_sub_index||', level='||g_nodes(l_sub_index).node_level);
3505       l_is_reservable_sub := g_nodes(l_sub_index).is_reservable_sub;
3506 
3507       IF l_is_reservable_sub IS NULL THEN
3508          IF p_is_reservable_sub IS NOT NULL THEN
3509             -- we did not know whether the sub is reservable
3510             -- when we created the node last time
3511             -- but we know now from p_is_reservable_sub
3512             l_is_reservable_sub := p_is_reservable_sub;
3513          ELSE
3514             check_is_reservable
3515               ( x_return_status       => l_return_status
3516               , p_inventory_item_id   => g_rootinfos(p_tree_id).inventory_item_id
3517               , p_organization_id     => g_rootinfos(p_tree_id).organization_id
3518               , p_subinventory_code   => p_subinventory_code
3519               , p_locator_id          => p_locator_id
3520               , p_lot_number          => p_lot_number
3521               , p_root_id             => p_tree_id
3522               , x_is_reservable       => l_is_reservable_sub
3523               , p_lpn_id              => p_lpn_id); -- Onhand Material Status Support
3524 
3525             IF l_return_status = fnd_api.g_ret_sts_error THEN
3526                RAISE fnd_api.g_exc_error;
3527             End IF ;
3528 
3529             IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3530                RAISE fnd_api.g_exc_unexpected_error;
3531             End IF;
3532          END IF;
3533 
3534          g_nodes(l_sub_index).is_reservable_sub := l_is_reservable_sub;
3535 
3536       END IF;
3537       */
3538       l_is_reservable_sub := g_nodes(l_node_index).is_reservable_sub;
3539 
3540       --Bug 4294336
3541       IF NVL(p_transaction_action_id,-1 ) = 2  THEN
3542          l_is_reservable_xfer_sub := FALSE;
3543          IF p_transfer_subinventory_code IS NULL THEN
3544             print_debug('add_quantities, transaction_action_id = 2 with NULL transfer subinventory ',9);
3545             RAISE fnd_api.g_exc_error;
3546          END IF;
3547          --bug 9380420, we should be calling check_is_reservable to account for transfer locator as well.
3548          /*
3549          check_is_reservable_sub
3550            (
3551               x_return_status     => l_return_status
3552             , p_organization_id   => g_rootinfos(p_tree_id).organization_id
3553             , p_subinventory_code => p_transfer_subinventory_code
3554             , x_is_reservable_sub => l_is_reservable_xfer_sub
3555             );
3556          */
3557 
3558          check_is_reservable
3559            ( x_return_status       => l_return_status
3560            , p_inventory_item_id   => g_rootinfos(p_tree_id).inventory_item_id
3561            , p_organization_id     => g_rootinfos(p_tree_id).organization_id
3562            , p_subinventory_code   => p_transfer_subinventory_code
3563            , p_locator_id          => p_transfer_locator_id
3564            , p_lot_number          => p_lot_number
3565            , p_root_id             => p_tree_id
3566            , x_is_reservable       => l_is_reservable_xfer_sub
3567            , p_lpn_id              => p_lpn_id); -- Onhand Material Status Support
3568 
3569          IF l_return_status = fnd_api.g_ret_sts_error THEN
3570             RAISE fnd_api.g_exc_error;
3571          End IF ;
3572 
3573          IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3574             RAISE fnd_api.g_exc_unexpected_error;
3575          End IF;
3576 
3577       END IF;
3578       --Bug 4294336
3579 
3580       l_loop_index := l_node_index;
3581       LOOP
3582          if g_debug = 1 then
3583             l_debug_line := '      '||'Old: Node: '||g_nodes(l_loop_index).node_level||' :'||lpad(l_loop_index,10)||':'||lpad(g_nodes(l_loop_index).qoh,8)||':'||lpad(g_nodes(l_loop_index).rqoh,8);
3584             print_debug(l_debug_line||':'||lpad(g_nodes(l_loop_index).qr,8)||':'||lpad(g_nodes(l_loop_index).qs,8)||':'||lpad(g_nodes(l_loop_index).att,8)||':'||lpad(g_nodes(l_loop_index).atr,8),12);
3585          end if;
3586 
3587          print_debug('loop3, node_index='||l_loop_index||' qoh='||g_nodes(l_loop_index).qoh||' rqoh='||g_nodes(l_loop_index).rqoh||' qr='||g_nodes(l_loop_index).qr||' qs='||g_nodes(l_loop_index).qs);
3588          print_debug(' ... att='||g_nodes(l_loop_index).att||' atr='||g_nodes(l_loop_index).atr);
3589          print_debug('.. sqoh='||g_nodes(l_loop_index).sqoh||' srqoh='||g_nodes(l_loop_index).srqoh||' sqr='||g_nodes(l_loop_index).sqr||' sqs='||g_nodes(l_loop_index).sqs);
3590          print_debug(' .... satt='||g_nodes(l_loop_index).satt||' satr='||g_nodes(l_loop_index).satr);
3591 
3592          --update att/atr for loose only mode
3593          IF l_tree_mode = g_loose_only_mode THEN
3594             print_debug('... in add_qty,3... mode=loose_only');
3595             --old factor = QS+QR-PQOH
3596             l_old_factor := g_nodes(l_loop_index).qs + g_nodes(l_loop_index).qr - g_nodes(l_loop_index).pqoh;
3597             l_old_factor2 := g_nodes(l_loop_index).sqs + g_nodes(l_loop_index).sqr - g_nodes(l_loop_index).spqoh;
3598 
3599             --new factor = QS+ (change to QS) +QR-PQOH
3600             l_new_factor := l_old_factor + p_primary_quantity;
3601             l_new_factor2 := NVL(l_old_factor2, 0) + p_secondary_quantity;
3602 
3603 
3604             if l_old_factor > 0 then
3605                IF l_new_factor > 0 THEN
3606                   l_update_quantity:= 0.0 - p_primary_quantity;
3607                   l_update_quantity2:= 0.0 - p_secondary_quantity;
3608                ELSE  -- old factor > 0, new factor <= 0
3609                   l_update_quantity := l_old_factor;
3610                   l_update_quantity2 := l_old_factor2;
3611                END IF;
3612             else -- l_old_factor <=0
3613                IF l_new_factor > 0 THEN
3614                   l_update_quantity:= 0.0 - l_new_factor;
3615                   l_update_quantity2:= 0.0 - l_new_factor2;
3616 
3617                ELSE  -- old factor < 0, new factor <= 0
3618                   l_update_quantity:= 0;
3619                   l_update_quantity2:= 0;
3620                END IF;
3621             end if;
3622 
3623                  /*********** Bug 9644285 fix ******************/
3624                   -- atr_update_quantity is amount to update atr by
3625                  -- if reservable packed quantity  >= quantity reserved
3626                  --    use existing calculation
3627                  --    old_atr_factor = qs + qr - pqoh
3628                  --    new_atr_factor = qs + qr - pqoh - pq
3629                  -- else
3630                  --    old_atr_factor = (qr-rpqoh) + max(0,qs-(pqoh-rpqoh))
3631                  --    if reservable qty
3632                  --      new_atr_factor = old_atr_factor - pq
3633                  --    else
3634                  --      new_factor = (qr-rpqoh) + max(0,(qs-(pqoh-rpqoh)+pq))
3635                  --    end if
3636                  -- end if
3637                  -- Base calculation is update_quantity =
3638                  -- max(old_factor, 0) - max(new_factor, 0)
3639                  -- We have to deal with four possible cases:
3640                  --    1. old_factor and new_factor >0
3641                  --    2. old_factor >0 and new_factor <0
3642                  --    3. old_factor <0 and new_factor >0
3643                  --    4. old_factor and new_factor <0
3644                  --    for these four cases, the atr is updated differently
3645 
3646                   IF(g_nodes(l_loop_index).rpqoh >= g_nodes(l_loop_index).qr) THEN
3647                      l_atr_old_factor := g_nodes(l_loop_index).qs +
3648                                          g_nodes(l_loop_index).qr -
3649                                          g_nodes(l_loop_index).pqoh;
3650                      l_atr_new_factor := l_atr_old_factor + p_primary_quantity;
3651                    ELSE
3652                      l_qs_factor := 0;
3653                      IF ((g_nodes(l_loop_index).qs - (g_nodes(l_loop_index).pqoh - g_nodes(l_loop_index).rpqoh)) > 0)
3654                        THEN
3655                         l_qs_factor := g_nodes(l_loop_index).qs - (g_nodes(l_loop_index).pqoh - g_nodes(l_loop_index).rpqoh);
3656                      END IF;
3657                      l_atr_old_factor :=
3658                        (g_nodes(l_loop_index).qr - g_nodes(l_loop_index).rpqoh) + l_qs_factor;
3659 
3660 
3661                      IF(l_is_reservable_sub) THEN
3662                         l_atr_new_factor := l_atr_old_factor + p_primary_quantity;
3663                       ELSE
3664                         l_qs_factor := 0;
3665                         IF ((g_nodes(l_loop_index).qs - (g_nodes(l_loop_index).pqoh - g_nodes(l_loop_index).rpqoh) +
3666                                                          p_primary_quantity) > 0)
3667                           THEN
3668                            l_qs_factor := g_nodes(l_loop_index).qs - (g_nodes(l_loop_index).pqoh -
3669                                                                       g_nodes(l_loop_index).rpqoh) + p_primary_quantity;
3670                         END IF;
3671                        l_atr_new_factor :=
3672                          (g_nodes(l_loop_index).qr - g_nodes(l_loop_index).rpqoh) + l_qs_factor;
3673                     END IF;
3674                   END IF;
3675 
3676      -- inv converge
3677                   IF(g_nodes(l_loop_index).srpqoh >= g_nodes(l_loop_index).sqr) THEN
3678                      l_atr_old_factor2 := g_nodes(l_loop_index).sqs +
3679                                          g_nodes(l_loop_index).sqr -
3680                                          g_nodes(l_loop_index).spqoh;
3681                      l_atr_new_factor2 := l_atr_old_factor2 + p_secondary_quantity;
3682                   ELSE
3683                      l_qs_factor2 := 0;
3684                      IF ((g_nodes(l_loop_index).sqs - (g_nodes(l_loop_index).spqoh - g_nodes(l_loop_index).srpqoh)) > 0)
3685                        THEN
3686                         l_qs_factor2 := g_nodes(l_loop_index).sqs - (g_nodes(l_loop_index).spqoh - g_nodes(l_loop_index).srpqoh);
3687                      END IF;
3688                      l_atr_old_factor2 :=
3689                        (g_nodes(l_loop_index).sqr - g_nodes(l_loop_index).srpqoh) + l_qs_factor2;
3690 
3691 
3692                      IF(l_is_reservable_sub) THEN
3693                         l_atr_new_factor2 := l_atr_old_factor2 + p_secondary_quantity;
3694                       ELSE
3695                         l_qs_factor2 := 0;
3696                         IF ((g_nodes(l_loop_index).sqs - (g_nodes(l_loop_index).spqoh - g_nodes(l_loop_index).srpqoh) +
3697                                                          p_secondary_quantity) > 0)
3698                           THEN
3699                            l_qs_factor2 := g_nodes(l_loop_index).sqs - (g_nodes(l_loop_index).spqoh -
3700                                                                       g_nodes(l_loop_index).srpqoh) + p_secondary_quantity;
3701                         END IF;
3702                        l_atr_new_factor2 :=
3703                          (g_nodes(l_loop_index).sqr - g_nodes(l_loop_index).srpqoh) + l_qs_factor2;
3704                     END IF;
3705                   END IF;
3706 
3707                   l_atr_update_quantity:= 0;
3708                   if l_atr_old_factor > 0 then
3709                     IF l_atr_new_factor > 0 THEN
3710                        l_atr_update_quantity:= l_atr_old_factor - l_atr_new_factor;   -- Bug 9644285
3711                      ELSE  -- atr_old factor > 0, atr_new factor <= 0
3712                        l_atr_update_quantity := l_atr_old_factor;
3713                     END IF;
3714                    else -- l_atr_old_factor <=0
3715                      IF l_atr_new_factor > 0 THEN
3716                         l_atr_update_quantity:= 0.0 - l_atr_new_factor;
3717                       ELSE  -- atr_old factor < 0, atr_new factor <= 0
3718                         l_atr_update_quantity:= 0;
3719                      END IF;
3720                   end if;
3721      -- inv converge
3722                       l_atr_update_quantity2:= 0;
3723                   if l_atr_old_factor2 > 0 then
3724                     IF l_atr_new_factor2 > 0 THEN
3725                        l_atr_update_quantity2:= l_atr_old_factor2 - l_atr_new_factor2;   -- Bug 9644285
3726                      ELSE  -- atr_old factor > 0, atr_new factor <= 0
3727                        l_atr_update_quantity2 := l_atr_old_factor2;
3728                     END IF;
3729                    else -- l_atr_old_factor <=0
3730                      IF l_atr_new_factor2 > 0 THEN
3731                         l_atr_update_quantity2:= 0.0 - l_atr_new_factor2;
3732                       ELSE  -- atr_old factor < 0, atr_new factor <= 0
3733                         l_atr_update_quantity2:= 0;
3734                      END IF;
3735                   end if;
3736 
3737                  /*********** Bug 9644285 fix ******************/
3738 
3739             -- Start of fix for the Bug 4294336
3740             IF NVL(p_transaction_action_id,-1 ) = 2  THEN
3741                IF  g_nodes(l_loop_index).node_level = g_locator_level THEN
3742                   IF  NVL(g_nodes(l_loop_index).locator_id,-99) <> NVL(p_transfer_locator_id,-999) THEN
3743                        -- update qs
3744                        print_debug('... in add_qty,3... updating qs with trx_qty...qs='||g_nodes(l_loop_index).qs||', trx_qty='||p_primary_quantity);
3745                        g_nodes(l_loop_index).qs := g_nodes(l_loop_index).qs + p_primary_quantity;
3746                        g_nodes(l_loop_index).sqs := NVL(g_nodes(l_loop_index).sqs, 0) + p_secondary_quantity;
3747 
3748                        --update att
3749                        g_nodes(l_loop_index).att := g_nodes(l_loop_index).att + l_update_quantity;
3750                        g_nodes(l_loop_index).satt := NVL(g_nodes(l_loop_index).satt, 0) + l_update_quantity2;
3751 
3752                      --Bug 14209371 We need to check for lot expiration date, reservable lot conditions as well along with reservable_sub
3753                      --to determine whether it should be included in atr calculation or not.
3754                      IF l_is_reservable_sub = TRUE AND (p_expiration_date IS NULL OR
3755                         g_rootinfos(p_tree_id).lot_expiration_date IS NULL OR
3756                          p_expiration_date > g_rootinfos(p_tree_id).lot_expiration_date)THEN
3757                          if ((g_rootinfos(p_tree_id).onhand_status_enabled and (p_is_reservable_lot = 1 OR p_is_reservable_lot is null OR g_is_mat_status_used = 2)) OR
3758                             ( (g_is_mat_status_used = 2) OR
3759                              (g_is_mat_status_used = 1 AND p_is_reservable_lot = 1 ) OR
3760                              (g_is_mat_status_used = 1 AND p_is_reservable_lot is null)))
3761                          then
3762                              print_debug('in add_qty,3  newATR='||g_nodes(l_loop_index).atr||' + '||l_atr_update_quantity);  -- Bug 9644285
3763                              --update atr
3764                              g_nodes(l_loop_index).atr := g_nodes(l_loop_index).atr + l_atr_update_quantity;             -- Bug 9644285
3765                              -- invConv Changes begin
3766                              print_debug('in add_qty,3  newSATR='||g_nodes(l_loop_index).satr||' + '||l_atr_update_quantity2);  -- Bug 9644285 invConv change
3767                              g_nodes(l_loop_index).satr := NVL(g_nodes(l_loop_index).satr, 0) + l_atr_update_quantity2;         -- Bug 9644285 invConv change
3768                              -- invConv Changes end
3769                          end if;
3770                      END IF;
3771                      IF l_is_reservable_sub then
3772                       NULL;
3773                      END IF;
3774 
3775                   END IF;
3776                ELSIF g_nodes(l_loop_index).node_level = g_sub_level  THEN
3777                   IF g_nodes(l_loop_index).subinventory_code <> p_transfer_subinventory_code THEN
3778                        -- update qs
3779                        print_debug('... in add_qty,3... updating qs with trx_qty...qs='||g_nodes(l_loop_index).qs||', trx_qty='||p_primary_quantity);
3780                        g_nodes(l_loop_index).qs := g_nodes(l_loop_index).qs + p_primary_quantity;
3781                        g_nodes(l_loop_index).sqs := NVL(g_nodes(l_loop_index).sqs, 0) + p_secondary_quantity;
3782 
3783                        --update att
3784                        g_nodes(l_loop_index).att := g_nodes(l_loop_index).att + l_update_quantity;
3785                        g_nodes(l_loop_index).satt := NVL(g_nodes(l_loop_index).satt, 0) + l_update_quantity2;
3786                      --Bug 14209371 We need to check for lot expiration date, reservable lot conditions as well along with reservable_sub
3787                      --to determine whether it should be included in atr calculation or not.
3788                      IF l_is_reservable_sub = TRUE AND (p_expiration_date IS NULL OR
3789                         g_rootinfos(p_tree_id).lot_expiration_date IS NULL OR
3790                          p_expiration_date > g_rootinfos(p_tree_id).lot_expiration_date)THEN
3791                          if ((g_rootinfos(p_tree_id).onhand_status_enabled and (p_is_reservable_lot = 1 OR p_is_reservable_lot is null OR g_is_mat_status_used = 2)) OR
3792                             ( (g_is_mat_status_used = 2) OR
3793                              (g_is_mat_status_used = 1 AND p_is_reservable_lot = 1 ) OR
3794                              (g_is_mat_status_used = 1 AND p_is_reservable_lot is null)))
3795                          then
3796 
3797                              print_debug('in add_qty,3  newATR='||g_nodes(l_loop_index).atr||' + '||l_atr_update_quantity);  -- Bug 9644285
3798                              --update atr
3799                              g_nodes(l_loop_index).atr := g_nodes(l_loop_index).atr + l_atr_update_quantity;             -- Bug 9644285
3800                              -- invConv Changes begin
3801                              print_debug('in add_qty,3  newSATR='||g_nodes(l_loop_index).satr||' + '||l_atr_update_quantity2);  -- Bug 9644285 invConv change
3802                              g_nodes(l_loop_index).satr := NVL(g_nodes(l_loop_index).satr, 0) + l_atr_update_quantity2;  -- Bug 9644285 invConv change
3803                              -- invConv Changes end
3804                          end if;
3805                      END IF;
3806                      IF l_is_reservable_sub then
3807                       NULL;
3808                      END IF;
3809                   END IF;
3810                ELSIF g_nodes(l_loop_index).node_level = g_lot_level THEN
3811                   IF NVL(g_nodes(l_loop_index).lot_number,'@#$') =  NVL(p_lot_number,'$#@') THEN
3812                        -- update qs
3813                        print_debug('... in add_qty,3... updating qs with trx_qty...qs='||g_nodes(l_loop_index).qs||', trx_qty='||p_primary_quantity);
3814                        g_nodes(l_loop_index).qs := g_nodes(l_loop_index).qs + p_primary_quantity;
3815                        g_nodes(l_loop_index).sqs := NVL(g_nodes(l_loop_index).sqs, 0) + p_secondary_quantity;
3816 
3817                        --update att
3818                        g_nodes(l_loop_index).att := g_nodes(l_loop_index).att + l_update_quantity;
3819                        g_nodes(l_loop_index).satt := NVL(g_nodes(l_loop_index).satt, 0) + l_update_quantity2;
3820 
3821                      --Bug 14209371 We need to check for lot expiration date, reservable lot conditions as well along with reservable_sub
3822                      --to determine whether it should be included in atr calculation or not.
3823                      IF l_is_reservable_sub = TRUE AND (p_expiration_date IS NULL OR
3824                         g_rootinfos(p_tree_id).lot_expiration_date IS NULL OR
3825                          p_expiration_date > g_rootinfos(p_tree_id).lot_expiration_date)THEN
3826                          if ((g_rootinfos(p_tree_id).onhand_status_enabled and (p_is_reservable_lot = 1 OR p_is_reservable_lot is null OR g_is_mat_status_used = 2)) OR
3827                             ( (g_is_mat_status_used = 2) OR
3828                              (g_is_mat_status_used = 1 AND p_is_reservable_lot = 1 ) OR
3829                              (g_is_mat_status_used = 1 AND p_is_reservable_lot is null)))
3830                          then
3831 
3832                              print_debug('in add_qty,3  newATR='||g_nodes(l_loop_index).atr||' + '||l_atr_update_quantity);  -- Bug 9644285
3833                              --update atr
3834                              g_nodes(l_loop_index).atr := g_nodes(l_loop_index).atr + l_atr_update_quantity;             -- Bug 9644285
3835                              -- invConv Changes begin
3836                              print_debug('in add_qty,3  newSATR='||g_nodes(l_loop_index).satr||' + '||l_atr_update_quantity2);  -- Bug 9644285 invConv change
3837                              g_nodes(l_loop_index).satr := NVL(g_nodes(l_loop_index).satr, 0) + l_atr_update_quantity2;  -- Bug 9644285 invConv change
3838                              -- invConv Changes end
3839                          end if;
3840                      END IF;
3841                      IF l_is_reservable_sub then
3842                       NULL;
3843                      END IF;
3844                   END IF;
3845                ELSIF  g_nodes(l_loop_index).node_level = g_revision_level THEN
3846                   IF NVL(g_nodes(l_loop_index).revision,'@#$') =  NVL(p_revision,'$#@') THEN
3847                        -- update qs
3848                        print_debug('... in add_qty,3... updating qs with trx_qty...qs='||g_nodes(l_loop_index).qs||', trx_qty='||p_primary_quantity);
3849                        g_nodes(l_loop_index).qs := g_nodes(l_loop_index).qs + p_primary_quantity;
3850                        g_nodes(l_loop_index).sqs := NVL(g_nodes(l_loop_index).sqs, 0) + p_secondary_quantity;
3851 
3852                        --update att
3853                        g_nodes(l_loop_index).att := g_nodes(l_loop_index).att + l_update_quantity;
3854                        g_nodes(l_loop_index).satt := NVL(g_nodes(l_loop_index).satt, 0) + l_update_quantity2;
3855 
3856                        --Bug 14209371 We need to check for lot expiration date, reservable lot conditions as well along with reservable_sub
3857                        --to determine whether it should be included in atr calculation or not.
3858                      IF l_is_reservable_sub = TRUE AND (p_expiration_date IS NULL OR
3859                         g_rootinfos(p_tree_id).lot_expiration_date IS NULL OR
3860                          p_expiration_date > g_rootinfos(p_tree_id).lot_expiration_date)THEN
3861                          if ((g_rootinfos(p_tree_id).onhand_status_enabled and (p_is_reservable_lot = 1 OR p_is_reservable_lot is null OR g_is_mat_status_used = 2)) OR
3862                             ( (g_is_mat_status_used = 2) OR
3863                              (g_is_mat_status_used = 1 AND p_is_reservable_lot = 1 ) OR
3864                              (g_is_mat_status_used = 1 AND p_is_reservable_lot is null)))
3865                          then
3866 
3867                              print_debug('in add_qty,3  newATR='||g_nodes(l_loop_index).atr||' + '||l_atr_update_quantity);  -- Bug 9644285
3868                              --update atr
3869                              g_nodes(l_loop_index).atr := g_nodes(l_loop_index).atr + l_atr_update_quantity;             -- Bug 9644285
3870                              -- invConv Changes begin
3871                              print_debug('in add_qty,3  newSATR='||g_nodes(l_loop_index).satr||' + '||l_atr_update_quantity2);  -- Bug 9644285 invConv change
3872                              g_nodes(l_loop_index).satr := NVL(g_nodes(l_loop_index).satr, 0) + l_atr_update_quantity2;  -- Bug 9644285 invConv change
3873                              -- invConv Changes end
3874                          end if;
3875                      END IF;
3876                      IF l_is_reservable_sub then
3877                       NULL;
3878                      END IF;
3879                   END IF;
3880                ELSIF g_nodes(l_loop_index).node_level = g_item_level THEN
3881                   NULL;
3882                ELSE
3883                   -- update qs
3884                   print_debug('... in add_qty,3... updating qs with trx_qty...qs='||g_nodes(l_loop_index).qs||', trx_qty='||p_primary_quantity);
3885                   g_nodes(l_loop_index).qs := g_nodes(l_loop_index).qs + p_primary_quantity;
3886                   g_nodes(l_loop_index).sqs := NVL(g_nodes(l_loop_index).sqs, 0) + p_secondary_quantity;
3887 
3888                   --update att
3889                   g_nodes(l_loop_index).att := g_nodes(l_loop_index).att + l_update_quantity;
3890                   g_nodes(l_loop_index).satt := NVL(g_nodes(l_loop_index).satt, 0) + l_update_quantity2;
3891 
3892                     --Bug 14209371 We need to check for lot expiration date, reservable lot conditions as well along with reservable_sub
3893                     --to determine whether it should be included in atr calculation or not.
3894                     IF l_is_reservable_sub = TRUE AND (p_expiration_date IS NULL OR
3895                         g_rootinfos(p_tree_id).lot_expiration_date IS NULL OR
3896                          p_expiration_date > g_rootinfos(p_tree_id).lot_expiration_date)THEN
3897                          if ((g_rootinfos(p_tree_id).onhand_status_enabled and (p_is_reservable_lot = 1 OR p_is_reservable_lot is null OR g_is_mat_status_used = 2)) OR
3898                             ( (g_is_mat_status_used = 2) OR
3899                              (g_is_mat_status_used = 1 AND p_is_reservable_lot = 1 ) OR
3900                              (g_is_mat_status_used = 1 AND p_is_reservable_lot is null)))
3901                          then
3902 
3903                              print_debug('in add_qty,3  newATR='||g_nodes(l_loop_index).atr||' + '||l_atr_update_quantity);  -- Bug 9644285
3904                              --update atr
3905                              g_nodes(l_loop_index).atr := g_nodes(l_loop_index).atr + l_atr_update_quantity;             -- Bug 9644285
3906                              -- invConv Changes begin
3907                              print_debug('in add_qty,3  newSATR='||g_nodes(l_loop_index).satr||' + '||l_atr_update_quantity2);  -- Bug 9644285 invConv change
3908                              g_nodes(l_loop_index).satr := NVL(g_nodes(l_loop_index).satr, 0) + l_atr_update_quantity2;  -- Bug 9644285 invConv change
3909                              -- invConv Changes end
3910                          end if;
3911                     END IF;
3912 
3913                      IF l_is_reservable_sub then
3914                       NULL;
3915                      END IF;
3916                END IF;  --Node Level Check
3917                /*
3918                 IF g_nodes(l_loop_index).node_level = g_item_level AND l_qty_chk_flag THEN
3919                   g_nodes(l_loop_index).qs_adj := g_nodes(l_loop_index).qs_adj + p_primary_quantity;
3920                   l_qty_chk_flag := FALSE;
3921                 END IF;
3922                */
3923                IF g_nodes(l_loop_index).node_level IN ( g_lot_level , g_revision_level , g_item_level)
3924                  AND p_transfer_subinventory_code IS NOT NULL
3925                  AND NOT l_is_reservable_xfer_sub THEN
3926                   g_nodes(l_loop_index).qs_adj1 := g_nodes(l_loop_index).qs_adj1 + p_primary_quantity;
3927                   g_nodes(l_loop_index).sqs_adj1 := g_nodes(l_loop_index).sqs_adj1 + p_secondary_quantity;
3928                    /* Added for bug 7323112 */
3929                   IF p_subinventory_code IS NOT NULL AND NOT l_is_reservable_sub THEN
3930                      g_nodes(l_loop_index).qs_adj1 := g_nodes(l_loop_index).qs_adj1 - p_primary_quantity;
3931                      g_nodes(l_loop_index).sqs_adj1 := g_nodes(l_loop_index).sqs_adj1 - p_secondary_quantity;
3932                   END IF;
3933                    /* End of changes for bug 7323112 */
3934                END IF;
3935 
3936             ELSE
3937                -- update qs
3938                print_debug('... in add_qty,3... updating qs with trx_qty...qs='||g_nodes(l_loop_index).qs||', trx_qty='||p_primary_quantity);
3939                g_nodes(l_loop_index).qs := g_nodes(l_loop_index).qs + p_primary_quantity;
3940                g_nodes(l_loop_index).sqs := NVL(g_nodes(l_loop_index).sqs, 0) + p_secondary_quantity;
3941 
3942                --update att
3943                g_nodes(l_loop_index).att := g_nodes(l_loop_index).att + l_update_quantity;
3944                g_nodes(l_loop_index).satt := NVL(g_nodes(l_loop_index).satt, 0) + l_update_quantity2;
3945 
3946                  --Bug 14209371 We need to check for lot expiration date, reservable lot conditions as well along with reservable_sub
3947                  --to determine whether it should be included in atr calculation or not.
3948                  IF l_is_reservable_sub = TRUE AND (p_expiration_date IS NULL OR
3949                     g_rootinfos(p_tree_id).lot_expiration_date IS NULL OR
3950                      p_expiration_date > g_rootinfos(p_tree_id).lot_expiration_date)THEN
3951                      if ((g_rootinfos(p_tree_id).onhand_status_enabled and (p_is_reservable_lot = 1 OR p_is_reservable_lot is null OR g_is_mat_status_used = 2)) OR
3952                         ( (g_is_mat_status_used = 2) OR
3953                          (g_is_mat_status_used = 1 AND p_is_reservable_lot = 1 ) OR
3954                          (g_is_mat_status_used = 1 AND p_is_reservable_lot is null)))
3955                      then
3956 
3957                          print_debug('in add_qty,3  newATR='||g_nodes(l_loop_index).atr||' + '||l_atr_update_quantity);  -- Bug 9644285
3958                          --update atr
3959                          g_nodes(l_loop_index).atr := g_nodes(l_loop_index).atr + l_atr_update_quantity;             -- Bug 9644285
3960                          -- invConv Changes begin
3961                          print_debug('in add_qty,3  newSATR='||g_nodes(l_loop_index).satr||' + '||l_atr_update_quantity2);  -- Bug 9644285 invConv change
3962                          g_nodes(l_loop_index).satr := NVL(g_nodes(l_loop_index).satr, 0) + l_atr_update_quantity2;  -- Bug 9644285 invConv change
3963                          -- invConv Changes end
3964                      end if;
3965                  END IF;
3966 
3967                  IF l_is_reservable_sub then
3968                   NULL;
3969                  END IF;
3970 
3971             END IF;
3972 
3973          ELSE -- not in loose items only mode
3974             -- Start of fix for the Bug 4294336
3975             IF NVL(p_transaction_action_id,-1 ) = 2  THEN
3976                IF  g_nodes(l_loop_index).node_level = g_locator_level THEN
3977                   IF  NVL(g_nodes(l_loop_index).locator_id,-99) <> NVL(p_transfer_locator_id,-999) THEN
3978                      print_debug('... in add_qty,3... NOT loose mode, updating qs with trx_qty...qs='||g_nodes(l_loop_index).qs||', trx_qty='||p_primary_quantity);
3979                      -- update qs
3980                      g_nodes(l_loop_index).qs := g_nodes(l_loop_index).qs + p_primary_quantity;
3981                      g_nodes(l_loop_index).sqs := NVL(g_nodes(l_loop_index).sqs, 0) + p_secondary_quantity;
3982 
3983                      --update att
3984                      g_nodes(l_loop_index).att := g_nodes(l_loop_index).att - p_primary_quantity;
3985                      g_nodes(l_loop_index).satt := NVL(g_nodes(l_loop_index).satt, 0) - p_secondary_quantity;
3986 
3987                      --Bug 14209371 We need to check for lot expiration date, reservable lot conditions as well along with reservable_sub
3988                      --to determine whether it should be included in atr calculation or not.
3989                      IF l_is_reservable_sub = TRUE AND (p_expiration_date IS NULL OR
3990                         g_rootinfos(p_tree_id).lot_expiration_date IS NULL OR
3991                          p_expiration_date > g_rootinfos(p_tree_id).lot_expiration_date)THEN
3992                          if ((g_rootinfos(p_tree_id).onhand_status_enabled and (p_is_reservable_lot = 1 OR p_is_reservable_lot is null OR g_is_mat_status_used = 2)) OR
3993                             ( (g_is_mat_status_used = 2) OR
3994                              (g_is_mat_status_used = 1 AND p_is_reservable_lot = 1 ) OR
3995                              (g_is_mat_status_used = 1 AND p_is_reservable_lot is null)))
3996                          then
3997                             print_debug('in add_qty,4 rsv=TRUE, newATR='||g_nodes(l_loop_index).atr||' + '||p_primary_quantity);
3998                             --update atr
3999                             g_nodes(l_loop_index).atr := g_nodes(l_loop_index).atr - p_primary_quantity;
4000                             g_nodes(l_loop_index).satr := NVL(g_nodes(l_loop_index).satr, 0) - p_secondary_quantity;
4001                          else
4002                             print_debug('in add_qty,4 rsv=FALSE, newATR='||g_nodes(l_loop_index).atr||' + 0');
4003                          end if;
4004                       END IF;
4005                   END IF;
4006                ELSIF g_nodes(l_loop_index).node_level = g_sub_level  THEN
4007                   IF g_nodes(l_loop_index).subinventory_code <> p_transfer_subinventory_code THEN
4008                      print_debug('... in add_qty,3... NOT loose mode, updating qs with trx_qty...qs='||g_nodes(l_loop_index).qs||', trx_qty='||p_primary_quantity);
4009                      -- update qs
4010                      g_nodes(l_loop_index).qs := g_nodes(l_loop_index).qs + p_primary_quantity;
4011                      g_nodes(l_loop_index).sqs := NVL(g_nodes(l_loop_index).sqs, 0) + p_secondary_quantity;
4012 
4013                      --update att
4014                      g_nodes(l_loop_index).att := g_nodes(l_loop_index).att - p_primary_quantity;
4015                      g_nodes(l_loop_index).satt := NVL(g_nodes(l_loop_index).satt, 0) - p_secondary_quantity;
4016 
4017                      --Bug 14209371 We need to check for lot expiration date, reservable lot conditions as well along with reservable_sub
4018                      --to determine whether it should be included in atr calculation or not
4019                      IF l_is_reservable_sub = TRUE AND (p_expiration_date IS NULL OR
4020                         g_rootinfos(p_tree_id).lot_expiration_date IS NULL OR
4021                          p_expiration_date > g_rootinfos(p_tree_id).lot_expiration_date)THEN
4022                          if ((g_rootinfos(p_tree_id).onhand_status_enabled and (p_is_reservable_lot = 1 OR p_is_reservable_lot is null OR g_is_mat_status_used = 2)) OR
4023                             ( (g_is_mat_status_used = 2) OR
4024                              (g_is_mat_status_used = 1 AND p_is_reservable_lot = 1 ) OR
4025                              (g_is_mat_status_used = 1 AND p_is_reservable_lot is null)))
4026                          then
4027                             print_debug('in add_qty,4 rsv=TRUE, newATR='||g_nodes(l_loop_index).atr||' + '||p_primary_quantity);
4028                             --update atr
4029                             g_nodes(l_loop_index).atr := g_nodes(l_loop_index).atr - p_primary_quantity;
4030                             g_nodes(l_loop_index).satr := NVL(g_nodes(l_loop_index).satr, 0) - p_secondary_quantity;
4031                          else
4032                             print_debug('in add_qty,4 rsv=FALSE, newATR='||g_nodes(l_loop_index).atr||' + 0');
4033                          end if;
4034                          -- l_qty_chk_flag := TRUE;
4035                       END IF;
4036                   END IF;
4037                ELSIF g_nodes(l_loop_index).node_level = g_lot_level THEN
4038                   IF NVL(g_nodes(l_loop_index).lot_number,'@#$') =  NVL(p_lot_number,'$#@') THEN
4039                      print_debug('... in add_qty,3... NOT loose mode, updating qs with trx_qty...qs='||g_nodes(l_loop_index).qs||', trx_qty='||p_primary_quantity);
4040                      -- update qs
4041                      g_nodes(l_loop_index).qs := g_nodes(l_loop_index).qs + p_primary_quantity;
4042                      g_nodes(l_loop_index).sqs := NVL(g_nodes(l_loop_index).sqs, 0) + p_secondary_quantity;
4043 
4044                      --update att
4045                      g_nodes(l_loop_index).att := g_nodes(l_loop_index).att - p_primary_quantity;
4046                      g_nodes(l_loop_index).satt := NVL(g_nodes(l_loop_index).satt, 0) - p_secondary_quantity;
4047 
4048                      --Bug 14209371 We need to check for lot expiration date, reservable lot conditions as well along with reservable_sub
4049                      --to determine whether it should be included in atr calculation or not
4050                       IF l_is_reservable_sub = TRUE AND (p_expiration_date IS NULL OR
4051                           g_rootinfos(p_tree_id).lot_expiration_date IS NULL OR
4052                           p_expiration_date > g_rootinfos(p_tree_id).lot_expiration_date)THEN
4053                          if ((g_rootinfos(p_tree_id).onhand_status_enabled and (p_is_reservable_lot = 1 OR p_is_reservable_lot is null OR g_is_mat_status_used = 2)) OR
4054                             ( (g_is_mat_status_used = 2) OR
4055                              (g_is_mat_status_used = 1 AND p_is_reservable_lot = 1 ) OR
4056                              (g_is_mat_status_used = 1 AND p_is_reservable_lot is null)))
4057                          then
4058                             print_debug('in add_qty,4 rsv=TRUE, newATR='||g_nodes(l_loop_index).atr||' + '||p_primary_quantity);
4059                             --update atr
4060                             g_nodes(l_loop_index).atr := g_nodes(l_loop_index).atr - p_primary_quantity;
4061                             g_nodes(l_loop_index).satr := NVL(g_nodes(l_loop_index).satr, 0) - p_secondary_quantity;
4062                          else
4063                             print_debug('in add_qty,4 rsv=FALSE, newATR='||g_nodes(l_loop_index).atr||' + 0');
4064                          end if;
4065                          -- l_qty_chk_flag := TRUE;
4066                       END IF;
4067                   END IF;
4068                ELSIF  g_nodes(l_loop_index).node_level = g_revision_level THEN
4069                   IF NVL(g_nodes(l_loop_index).revision,'@#$') = NVL(p_revision,'$#@') THEN
4070                      print_debug('... in add_qty,3... NOT loose mode, updating qs with trx_qty...qs='||g_nodes(l_loop_index).qs||', trx_qty='||p_primary_quantity);
4071                      -- update qs
4072                      g_nodes(l_loop_index).qs := g_nodes(l_loop_index).qs + p_primary_quantity;
4073                      g_nodes(l_loop_index).sqs := NVL(g_nodes(l_loop_index).sqs, 0) + p_secondary_quantity;
4074 
4075                      --update att
4076                      g_nodes(l_loop_index).att := g_nodes(l_loop_index).att - p_primary_quantity;
4077                      g_nodes(l_loop_index).satt := NVL(g_nodes(l_loop_index).satt, 0) - p_secondary_quantity;
4078 
4079                      --Bug 14209371 We need to check for lot expiration date, reservable lot conditions as well along with reservable_sub
4080                      --to determine whether it should be included in atr calculation or not
4081                       IF l_is_reservable_sub = TRUE AND (p_expiration_date IS NULL OR
4082                           g_rootinfos(p_tree_id).lot_expiration_date IS NULL OR
4083                           p_expiration_date > g_rootinfos(p_tree_id).lot_expiration_date)THEN
4084                          if ((g_rootinfos(p_tree_id).onhand_status_enabled and (p_is_reservable_lot = 1 OR p_is_reservable_lot is null OR g_is_mat_status_used = 2)) OR
4085                             ( (g_is_mat_status_used = 2) OR
4086                              (g_is_mat_status_used = 1 AND p_is_reservable_lot = 1 ) OR
4087                              (g_is_mat_status_used = 1 AND p_is_reservable_lot is null)))
4088                          then
4089                             print_debug('in add_qty,4 rsv=TRUE, newATR='||g_nodes(l_loop_index).atr||' + '||p_primary_quantity);
4090                             --update atr
4091                             g_nodes(l_loop_index).atr := g_nodes(l_loop_index).atr - p_primary_quantity;
4092                             g_nodes(l_loop_index).satr := NVL(g_nodes(l_loop_index).satr, 0) - p_secondary_quantity;
4093                          else
4094                             print_debug('in add_qty,4 rsv=FALSE, newATR='||g_nodes(l_loop_index).atr||' + 0');
4095                          end if;
4096                          --   l_qty_chk_flag := TRUE;
4097                       END IF;
4098                   END IF;
4099                ELSIF g_nodes(l_loop_index).node_level = g_item_level THEN
4100                   NULL;
4101                ELSE
4102                   print_debug('... in add_qty,3... NOT loose mode, updating qs with trx_qty...qs='||g_nodes(l_loop_index).qs||', trx_qty='||p_primary_quantity);
4103                   -- update qs
4104                   g_nodes(l_loop_index).qs := g_nodes(l_loop_index).qs + p_primary_quantity;
4105                   g_nodes(l_loop_index).sqs := NVL(g_nodes(l_loop_index).sqs, 0) + p_secondary_quantity;
4106 
4107                   --update att
4108                   g_nodes(l_loop_index).att := g_nodes(l_loop_index).att - p_primary_quantity;
4109                   g_nodes(l_loop_index).satt := NVL(g_nodes(l_loop_index).satt, 0) - p_secondary_quantity;
4110 
4111                   --Bug 14209371 We need to check for lot expiration date, reservable lot conditions as well along with reservable_sub
4112                   --to determine whether it should be included in atr calculation or not
4113                   IF l_is_reservable_sub = TRUE AND (p_expiration_date IS NULL OR
4114                      g_rootinfos(p_tree_id).lot_expiration_date IS NULL OR
4115                      p_expiration_date > g_rootinfos(p_tree_id).lot_expiration_date)THEN
4116                      if ((g_rootinfos(p_tree_id).onhand_status_enabled and (p_is_reservable_lot = 1 OR p_is_reservable_lot is null OR g_is_mat_status_used = 2)) OR
4117                         ( (g_is_mat_status_used = 2) OR
4118                          (g_is_mat_status_used = 1 AND p_is_reservable_lot = 1 ) OR
4119                          (g_is_mat_status_used = 1 AND p_is_reservable_lot is null)))
4120                      then
4121                      print_debug('in add_qty,4 rsv=TRUE, newATR='||g_nodes(l_loop_index).atr||' + '||p_primary_quantity);
4122                      --update atr
4123                      g_nodes(l_loop_index).atr := g_nodes(l_loop_index).atr - p_primary_quantity;
4124                      g_nodes(l_loop_index).satr := NVL(g_nodes(l_loop_index).satr, 0) - p_secondary_quantity;
4125                      else
4126                          print_debug('in add_qty,4 rsv=FALSE, newATR='||g_nodes(l_loop_index).atr||' + 0');
4127                      end if;
4128                   -- l_qty_chk_flag := TRUE;
4129                   END IF;
4130                END IF;  --Node Level Check
4131                /*
4132                IF g_nodes(l_loop_index).node_level = g_item_level AND l_qty_chk_flag THEN
4133                   g_nodes(l_loop_index).qs_adj := g_nodes(l_loop_index).qs_adj + p_primary_quantity;
4134                   l_qty_chk_flag := FALSE;
4135                END IF;
4136                 */
4137                IF g_nodes(l_loop_index).node_level IN ( g_lot_level , g_revision_level , g_item_level)
4138                   AND p_transfer_subinventory_code IS NOT NULL
4139                   AND NOT l_is_reservable_xfer_sub THEN
4140                   g_nodes(l_loop_index).qs_adj1 := g_nodes(l_loop_index).qs_adj1 + p_primary_quantity;
4141                   g_nodes(l_loop_index).sqs_adj1 := g_nodes(l_loop_index).sqs_adj1 + p_secondary_quantity;
4142                    /* Added for bug 7323112 */
4143                   IF p_subinventory_code IS NOT NULL AND NOT l_is_reservable_sub THEN
4144                      g_nodes(l_loop_index).qs_adj1 := g_nodes(l_loop_index).qs_adj1 - p_primary_quantity;
4145                      g_nodes(l_loop_index).sqs_adj1 := g_nodes(l_loop_index).sqs_adj1 - p_secondary_quantity;
4146                   END IF;
4147                   /* End of changes for bug 7323112 */
4148 
4149                END IF;
4150 
4151             ELSE
4152                print_debug('... in add_qty,3... NOT loose mode, updating qs with trx_qty...qs='||g_nodes(l_loop_index).qs||', trx_qty='||p_primary_quantity);
4153                -- update qs
4154                g_nodes(l_loop_index).qs := g_nodes(l_loop_index).qs + p_primary_quantity;
4155                g_nodes(l_loop_index).sqs := NVL(g_nodes(l_loop_index).sqs, 0) + p_secondary_quantity;
4156 
4157                --update att
4158                g_nodes(l_loop_index).att := g_nodes(l_loop_index).att - p_primary_quantity;
4159                g_nodes(l_loop_index).satt := NVL(g_nodes(l_loop_index).satt, 0) - p_secondary_quantity;
4160 
4161                  --Bug 14209371 We need to check for lot expiration date, reservable lot conditions as well along with reservable_sub
4162                  --to determine whether it should be included in atr calculation or not
4163                  IF l_is_reservable_sub = TRUE AND (p_expiration_date IS NULL OR
4164                      g_rootinfos(p_tree_id).lot_expiration_date IS NULL OR
4165                      p_expiration_date > g_rootinfos(p_tree_id).lot_expiration_date)THEN
4166                      if ((g_rootinfos(p_tree_id).onhand_status_enabled and (p_is_reservable_lot = 1 OR p_is_reservable_lot is null OR g_is_mat_status_used = 2)) OR
4167                         ( (g_is_mat_status_used = 2) OR
4168                          (g_is_mat_status_used = 1 AND p_is_reservable_lot = 1 ) OR
4169                          (g_is_mat_status_used = 1 AND p_is_reservable_lot is null)))
4170                      then
4171                       print_debug('in add_qty,4 rsv=TRUE, newATR='||g_nodes(l_loop_index).atr||' + '||p_primary_quantity);
4172                       --update atr
4173                       g_nodes(l_loop_index).atr := g_nodes(l_loop_index).atr - p_primary_quantity;
4174                       g_nodes(l_loop_index).satr := NVL(g_nodes(l_loop_index).satr, 0) - p_secondary_quantity;
4175                    else
4176                       print_debug('in add_qty,4 rsv=FALSE, newATR='||g_nodes(l_loop_index).atr||' + 0');
4177                    end if;
4178                    -- l_qty_chk_flag := TRUE;
4179                  END IF;
4180              END IF; --Action Id 2 check
4181 
4182          END IF;
4183 
4184          -- set check mark
4185 
4186          -- Bug 2486318. The do check does not work. Trasactions get committed
4187          -- even if there is a node violation. Added p_check_mark_node_only to mark the nodes.
4188 
4189          IF (p_set_check_mark = TRUE or
4190            p_check_mark_node_only = fnd_api.g_true AND p_primary_quantity < 0 )THEN
4191                g_nodes(l_loop_index).check_mark := TRUE;
4192          END IF;
4193 
4194          IF g_debug = 1 THEN
4195             l_debug_line := '      '||'New: Node: '||g_nodes(l_loop_index).node_level||' :'||lpad(l_loop_index,10)||':'||lpad(g_nodes(l_loop_index).qoh,8)||':'||lpad(g_nodes(l_loop_index).rqoh,8);
4196             print_debug(l_debug_line||':'||lpad(g_nodes(l_loop_index).qr,8)||':'||lpad(g_nodes(l_loop_index).qs,8)||':'||lpad(g_nodes(l_loop_index).att,8)||':'||lpad(g_nodes(l_loop_index).atr,8),12);
4197          END IF;
4198 
4199          IF (g_nodes(l_loop_index).node_level = g_item_level) THEN
4200             EXIT;
4201          END IF;
4202 
4203          l_loop_index := g_nodes(l_loop_index).parent_index;
4204       END LOOP;
4205 
4206 
4207    END IF;
4208 
4209    IF p_set_check_mark = TRUE THEN
4210       -- we will mark other trees with the same org and item id
4211       -- as need refresh since the update may render
4212       -- those trees as outdated
4213       /* Performance - don't loop through all trees
4214       FOR l_rootinfo_index IN 1..g_all_roots_counter LOOP
4215          l_root_id := g_all_roots(l_rootinfo_index);
4216          IF p_tree_id <> l_root_id
4217             AND (g_rootinfos(l_root_id).organization_id = g_rootinfos(p_tree_id).organization_id
4218              AND g_rootinfos(l_root_id).inventory_item_id = g_rootinfos(p_tree_id).inventory_item_id)
4219          THEN
4220             g_rootinfos(l_root_id).need_refresh := TRUE;
4221          END IF;
4222       END LOOP;
4223       */
4224 
4225       l_hash_string := g_rootinfos(p_tree_id).organization_id
4226         || ':' || g_rootinfos(p_tree_id).inventory_item_id;
4227       l_org_item_index := dbms_utility.get_hash_value(
4228                         name       => l_hash_string
4229                        ,base       => l_hash_base
4230                        ,hash_size  => l_hash_size);
4231       If g_org_item_trees.exists(l_org_item_index) Then
4232          l_tree_index  := g_org_item_trees(l_org_item_index);
4233          LOOP
4234             EXIT WHEN l_tree_index = 0;
4235             l_root_id := g_all_roots(l_tree_index).root_id;
4236             if l_root_id <> p_tree_id then
4237                g_rootinfos(l_root_id).need_refresh := TRUE;
4238             end if;
4239             l_tree_index := g_all_roots(l_tree_index).next_root;
4240          END LOOP;
4241       End If;
4242    END IF;
4243 
4244    print_debug('Normal end of add_quantities...');
4245    x_return_status := l_return_status;
4246 
4247 EXCEPTION
4248    WHEN fnd_api.g_exc_error THEN
4249       print_debug('in add_quantities... EXP_ERROR sql='||SQLERRM,9);
4250       x_return_status := fnd_api.g_ret_sts_error;
4251 
4252    WHEN fnd_api.g_exc_unexpected_error THEN
4253       print_debug('in add_quantities... UNEXP_ERROR sql='||SQLERRM,9);
4254       x_return_status := fnd_api.g_ret_sts_unexp_error ;
4255 
4256    WHEN OTHERS THEN
4257       print_debug('in add_quantities... OTHERS ERROR sql='||SQLERRM,9);
4258       x_return_status := fnd_api.g_ret_sts_unexp_error ;
4259 
4260       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
4261       THEN
4262          fnd_msg_pub.add_exc_msg
4263              (  g_pkg_name
4264               , 'Add_Quantities'
4265               ,9);
4266       END IF;
4267 END add_quantities;
4268 
4269 -- Procedure
4270 --   build_tree
4271 -- Description
4272 --   build the tree by querying the database tables and compute quantities
4273 PROCEDURE build_tree
4274   (  x_return_status         OUT NOCOPY VARCHAR2
4275      , p_tree_id             IN  INTEGER
4276     ) IS
4277    l_api_name                    VARCHAR2(30) := 'BUILD_TREE';
4278    l_return_status               VARCHAR2(1) := fnd_api.g_ret_sts_success;
4279    l_is_reservable_sub           BOOLEAN;
4280    l_cursor                      NUMBER;
4281    l_dummy                       INTEGER;
4282    l_index_start                 NUMBER := 0;
4283    l_num_recs_fetch              NUMBER := 100;
4284    l_cur_num_rows                NUMBER := 0;
4285    l_tot_num_rows                NUMBER := 0;
4286    l_index                       NUMBER := 0;
4287 
4288    l_revision_control            NUMBER;
4289    l_no_lpn_reservations         NUMBER;
4290    l_tree_mode                   NUMBER;
4291    l_demand_source_type_id       NUMBER;
4292    l_demand_source_header_id     NUMBER;
4293    l_demand_source_line_id       NUMBER;
4294    l_demand_source_name          VARCHAR2(80);  -- bug 11879550, increase it from 30 to 80
4295    l_demand_source_delivery      NUMBER;
4296    l_organization_id             NUMBER;
4297    l_inventory_item_id           NUMBER;
4298    l_subinventory_code           VARCHAR2(30);
4299    l_locator_id                  NUMBER;
4300    l_lot_number                  VARCHAR2(80);
4301    l_primary_quantity            NUMBER;
4302    l_asset_subs_only             NUMBER;
4303    l_onhand_source               NUMBER;
4304    l_lot_expiration_control      NUMBER;
4305    l_lot_expiration_date         DATE;
4306    lot_control_code               NUMBER;   -- Bug 7628989
4307    l_grade_code                  VARCHAR2(150);
4308 
4309    v_revision                    bulk_varchar_3_tbl_type;
4310    v_lot_number                  bulk_varchar_80_tbl_type;
4311    v_subinventory_code           bulk_varchar_10_tbl_type;
4312    v_lot_expiration_date         bulk_date_tbl_type;
4313    v_reservable_type             bulk_num_tbl_type;
4314    v_primary_quantity            bulk_num_tbl_type;
4315    v_secondary_quantity          bulk_num_tbl_type;
4316    v_quantity_type               bulk_num_tbl_type;
4317    v_locator_id                  bulk_num_tbl_type;
4318    v_inventory_item_id           bulk_num_tbl_type;
4319    v_organization_id             bulk_num_tbl_type;
4320    v_cost_group_id               bulk_num_tbl_type;
4321    v_lpn_id                      bulk_num_tbl_type;
4322 
4323    v_transaction_action_id       bulk_num_tbl_type;
4324    v_transfer_subinventory_code  bulk_varchar_10_tbl_type;
4325    v_transfer_locator_id         bulk_num_tbl_type;
4326 
4327    v_status_id                   bulk_num_tbl_type; -- Onhand Material Status Support
4328    v_is_reservable_lot           bulk_num_tbl_type;   --Bug#8713821
4329 
4330 -- Onhand Material Status Support: Modified the queries to check for availability_type
4331 -- and atpable_type from MOQD's status if the status is tracked at the onhand level.
4332 
4333      CURSOR c_plain IS
4334      SELECT
4335           x.organization_id               organization_id
4336         , x.inventory_item_id             inventory_item_id
4337         , x.revision                      revision
4338         , NULL                            lot_number
4339         , NULL                            lot_expiration_date
4340         , x.subinventory_code             subinventory_code
4341         , sub.reservable_type             reservable_type
4342         , x.locator_id                    locator_id
4343         , SUM(x.primary_quantity)         primary_quantity
4344         , SUM(x.secondary_quantity)       secondary_quantity
4345         , x.quantity_type                 quantity_type
4346         , x.cost_group_id                 cost_group_id
4347         , x.lpn_id                        lpn_id
4348         , x.transaction_action_id         transaction_action_id
4349         , x.transfer_subinventory_code    transfer_subinventory_code
4350         , x.transfer_locator_id           transfer_locator_id
4351         , NULL                          is_reservable_lot       --Bug#8713821
4352      FROM (
4353        SELECT
4354            x.organization_id                                organization_id
4355          , x.inventory_item_id                              inventory_item_id
4356          , decode(l_revision_control, 2, NULL, x.revision)  revision
4357          , NULL                                             lot_number
4358          , x.subinventory_code                              subinventory_code
4359          , x.locator_id                                     locator_id
4360          , SUM(x.primary_quantity)                          primary_quantity
4361          , SUM(x.secondary_quantity)                        secondary_quantity
4362          , x.quantity_type                                  quantity_type
4363          , x.cost_group_id                                  cost_group_id
4364          , x.lpn_id                                         lpn_id
4365          , x.transaction_action_id                          transaction_action_id
4366          , x.transfer_subinventory_code                     transfer_subinventory_code
4367          , x.transfer_locator_id                            transfer_locator_id
4368         FROM (
4369           -- reservations
4370           SELECT
4371              mr.organization_id                                                        organization_id
4372            , mr.inventory_item_id                                                      inventory_item_id
4373            , mr.revision                                                               revision
4374            , mr.lot_number                                                             lot_number
4375            , mr.subinventory_code                                                      subinventory_code
4376            , mr.locator_id                                                             locator_id
4377            , mr.primary_reservation_quantity - Nvl(mr.detailed_quantity,0)             primary_quantity
4378            , mr.secondary_reservation_quantity - Nvl(mr.secondary_detailed_quantity,0) secondary_quantity
4379            , 3                                                                         quantity_type
4380            , to_number(NULL)                                                           cost_group_id
4381            , lpn_id                                                                    lpn_id
4382            , to_number(NULL)                                                           transaction_action_id
4383            , to_char(NULL)                                                             transfer_subinventory_code
4384            , to_number(NULL)                                                           transfer_locator_id
4385         FROM mtl_reservations mr
4386         WHERE
4387              Nvl(mr.supply_source_type_id, 13) = 13
4388          AND mr.primary_reservation_quantity > Nvl(mr.detailed_quantity,0)
4389          AND ((l_no_lpn_reservations <>1) OR (l_no_lpn_reservations = 1 AND mr.lpn_id IS NULL))
4390          AND (l_tree_mode <> 3 OR
4391                 (l_tree_mode = 3
4392                  AND NOT (    l_demand_source_type_id = mr.demand_source_type_id
4393                           AND l_demand_source_header_id = mr.demand_source_header_id
4394                           AND Nvl(l_demand_source_line_id, -9999) = Nvl(mr.demand_source_line_id,-9999)
4395                           AND Nvl(l_demand_source_name, '@@@###@@#') = Nvl(mr.demand_source_name,'@@@###@@#')
4396                           AND Nvl(l_demand_source_delivery,-9999) = Nvl(mr.demand_source_delivery,-9999)
4397                          )
4398                 )
4399              )
4400         UNION ALL
4401           -- onhand quantities
4402           SELECT
4403              moq.organization_id                   organization_id
4404            , moq.inventory_item_id                 inventory_item_id
4405            , moq.revision                          revision
4406            , moq.lot_number                        lot_number
4407            , moq.subinventory_code                 subinventory_code
4408            , moq.locator_id                        locator_id
4409            , moq.primary_transaction_quantity
4410            , moq.secondary_transaction_quantity
4411            , 1                                     quantity_type
4412            , moq.cost_group_id                     cost_group_id
4413            , moq.lpn_id                            lpn_id
4414            , to_number(NULL)                       transaction_action_id
4415            , to_char(NULL)                         transfer_subinventory_code
4416            , to_number(NULL)                       transfer_locator_id
4417           FROM
4418              mtl_onhand_quantities_detail       moq
4419 
4420         UNION ALL
4421           -- pending transactions in mmtt
4422           SELECT
4423                mmtt.organization_id                                                                organization_id
4424              , mmtt.inventory_item_id                                                              inventory_item_id
4425              , mmtt.revision                                                                       revision
4426              , NULL                                                                                lot_number
4427              , mmtt.subinventory_code                                                              subinventory_code
4428              , mmtt.locator_id                                                                     locator_id
4429              --Bug 4185621
4430              --, Decode(mmtt.transaction_status, 2, 1,
4431              , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status)
4432                       , 2, 1, Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,Sign(mmtt.primary_quantity)))
4433                * round(Abs(mmtt.primary_quantity),5)
4434              --Bug 4185621
4435              --, Decode(mmtt.transaction_status, 2, 1,
4436              , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status)
4437                       , 2, 1, Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,Sign(mmtt.secondary_transaction_quantity)))
4438                * round(Abs(mmtt.secondary_transaction_quantity),5)
4439              --Bug 4185621
4440              --, Decode(mmtt.transaction_status, 2, 5, 1)  quantity_type
4441              , Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,1,5), 1)   quantity_type
4442              , mmtt.cost_group_id                                                                  cost_group_id
4443              --14688297
4444              --, NVL(mmtt.allocated_lpn_id,NVL(mmtt.content_lpn_id, mmtt.lpn_id))                    lpn_id
4445              , decode(nvl(mmtt.wms_task_status,-1), 4, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID), NVL(MMTT.ALLOCATED_LPN_ID, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID))) lpn_id
4446 
4447              /* Bug 9938149, Decode(mmtt.transaction_status, 2 , mmtt.transaction_action_id,
4448               * to_number(NULL)) transaction_action_id */
4449              , Decode(mmtt.transaction_status, 2 , decode (mp.process_enabled_flag, 'Y',
4450                                                           decode ((select move_order_type
4451                                                                    from mtl_txn_request_headers mtrh
4452                                                                    where header_id = mmtt.move_order_header_id
4453                                                                    and rownum = 1),5,28,mmtt.transaction_action_id),
4454                                                         mmtt.transaction_action_id), to_number(NULL)) transaction_action_id
4455              , Decode(mmtt.transaction_status, 2 , mmtt.transfer_subinventory, to_char(NULL))      transfer_subinventory_code
4456              , Decode(mmtt.transaction_status, 2 , mmtt.transfer_to_location, to_number(NULL))     transfer_locator_id
4457           FROM
4458                mtl_material_transactions_temp mmtt,
4459                mtl_parameters mp -- Bug 9938149
4460           WHERE
4461                mmtt.posting_flag = 'Y'
4462            AND mmtt.subinventory_code IS NOT NULL
4463            AND (Nvl(mmtt.transaction_status,0) <> 2 OR
4464                 Nvl(mmtt.transaction_status,0) = 2 AND mmtt.transaction_action_id IN (1,2,28,3,21,29,32,34))
4465            -- dont look at scrap and costing txns
4466            -- Bug 3396558 fix. Ignore ownership xfr,planning xfr transactions
4467            AND mmtt.transaction_action_id NOT IN (5,6,24,30)
4468            AND mmtt.organization_id = mp.organization_id -- Bug 9938149
4469 
4470         UNION ALL
4471           -- receiving side of transfers
4472           -- added 5/23/00
4473           -- if quantity is in an lpn, then it is containerized
4474           -- Bug 7658493, If wms task is in loaded status, consider allocation like pending transaction.
4475           SELECT
4476                Decode(mmtt.transaction_action_id, 3, mmtt.transfer_organization, mmtt.organization_id)   organization_id
4477              , mmtt.inventory_item_id                                                                    inventory_item_id
4478              , mmtt.revision                                                                             revision
4479              , NULL                                                                                      lot_number
4480              , mmtt.transfer_subinventory                                                                subinventory_code
4481              , mmtt.transfer_to_location                                                                 locator_id
4482              , round(Abs(mmtt.primary_quantity),5)
4483              , round(Abs(mmtt.secondary_transaction_quantity),5)
4484              , 1                                                                                         quantity_type
4485              , mmtt.transfer_cost_group_id                                                               cost_group_id
4486              , NVL(mmtt.content_lpn_id,mmtt.transfer_lpn_id)                                             lpn_id
4487              , to_number(NULL)                                                                           transaction_action_id
4488              , to_char(NULL)                                                                             transfer_subinventory_code
4489              , to_number(NULL)                                                                           transfer_locator_id
4490           FROM
4491                mtl_material_transactions_temp mmtt
4492           WHERE
4493                mmtt.posting_flag = 'Y'
4494            AND Decode( Nvl(mmtt.transaction_status,0),
4495                        2, decode(nvl(mmtt.wms_task_status,-1), 4, 1, 2),
4496                        1 ) <> 2
4497            AND (mmtt.transaction_action_id IN (2,3) -- Bug 9595634: Removed 28
4498                -- Bug 9595634: Added condition to match demand source if action = 28
4499                 OR (mmtt.transaction_action_id = 28
4500                     AND l_demand_source_header_id = mmtt.transaction_source_id
4501                     AND Nvl(l_demand_source_line_id, -9999) = Nvl(mmtt.TRX_SOURCE_LINE_ID,-9999)
4502                     AND Nvl(l_demand_source_name, '@@@###@@#') = Nvl(mmtt.TRANSACTION_SOURCE_NAME,'@@@###@@#')
4503                     AND Nvl(l_demand_source_delivery,-9999) = Nvl(mmtt.TRX_SOURCE_DELIVERY_ID,-9999)
4504                    )
4505                )
4506            --bug 3581133
4507            AND mmtt.wip_supply_type IS NULL
4508              ) x
4509       WHERE x.organization_id    = l_organization_id
4510         AND x.inventory_item_id  = l_inventory_item_id
4511       GROUP BY
4512            x.organization_id, x.inventory_item_id, x.revision
4513           , x.lot_number, x.subinventory_code, x.locator_id
4514           , x.quantity_type, x.cost_group_id, x.lpn_id
4515           , x.transaction_action_id, x.transfer_subinventory_code
4516           , x.transfer_locator_id
4517              ) x
4518              , mtl_secondary_inventories sub
4519   WHERE
4520         x.organization_id    = sub.organization_id          (+)
4521     AND x.subinventory_code  = sub.secondary_inventory_name (+)
4522     AND (l_asset_subs_only = 2 OR NVL(sub.asset_inventory,1) = 1)
4523     AND (  (l_onhand_source = 1 AND Nvl(sub.inventory_atp_code, 1) = 1)
4524         OR (l_onhand_source = 2 AND Nvl(sub.availability_type, 1) = 1 )
4525         OR l_onhand_source =3
4526         OR (l_onhand_source = 4 AND (nvl(sub.inventory_atp_code,1) = 1 AND nvl(sub.availability_type,1)=1))
4527         )
4528     GROUP BY
4529           x.organization_id
4530         , x.inventory_item_id
4531         , x.revision
4532         , NULL
4533         , NULL
4534         , sub.reservable_type
4535         , x.subinventory_code
4536         , NULL
4537         , x.locator_id
4538         , x.quantity_type
4539         , x.cost_group_id
4540         , x.lpn_id
4541         , x.transaction_action_id
4542         , x.transfer_subinventory_code
4543         , x.transfer_locator_id;
4544 
4545      -- invConv changes begin : c_plain with MMS about ATP/Nettable...
4546      CURSOR c_plain_MMS IS
4547      SELECT
4548           x.organization_id       organization_id
4549         , x.inventory_item_id     inventory_item_id
4550         , x.revision              revision
4551    , NULL           lot_number
4552         , NULL              lot_expiration_date
4553         , x.subinventory_code     subinventory_code
4554         , sub.reservable_type     reservable_type
4555         , x.locator_id            locator_id
4556         , SUM(x.primary_quantity)     primary_quantity
4557         , SUM(x.secondary_quantity)   secondary_quantity
4558         , x.quantity_type         quantity_type
4559         , x.cost_group_id         cost_group_id
4560         , x.lpn_id        lpn_id
4561         , x.transaction_action_id       transaction_action_id
4562         , x.transfer_subinventory_code  transfer_subinventory_code
4563         , x.transfer_locator_id         transfer_locator_id
4564         , x.status_id
4565         , NULL                         is_reservable_lot       --Bug#8713821
4566      FROM (
4567        SELECT
4568            x.organization_id                                organization_id
4569          , x.inventory_item_id                              inventory_item_id
4570          , decode(l_revision_control, 2, NULL, x.revision)  revision
4571          , NULL                                             lot_number
4572          , x.subinventory_code                              subinventory_code
4573          , x.locator_id                                     locator_id
4574          , SUM(x.primary_quantity)                          primary_quantity
4575          , SUM(x.secondary_quantity)                        secondary_quantity
4576          , x.quantity_type                                  quantity_type
4577          , x.cost_group_id                                  cost_group_id
4578          , x.lpn_id                                         lpn_id
4579          , x.transaction_action_id                          transaction_action_id
4580          , x.transfer_subinventory_code                     transfer_subinventory_code
4581          , x.transfer_locator_id                            transfer_locator_id
4582          , x.status_id
4583         FROM (
4584        -- reservations
4585        SELECT
4586           mr.organization_id                                                        organization_id
4587         , mr.inventory_item_id                                                      inventory_item_id
4588         , mr.revision                                                               revision
4589         , mr.lot_number                                                             lot_number
4590         , mr.subinventory_code                                                      subinventory_code
4591         , mr.locator_id                                                             locator_id
4592         , mr.primary_reservation_quantity - Nvl(mr.detailed_quantity,0)             primary_quantity
4593         , mr.secondary_reservation_quantity - Nvl(mr.secondary_detailed_quantity,0) secondary_quantity
4594         , 3                                                                         quantity_type
4595         , to_number(NULL)                                                           cost_group_id
4596         , lpn_id                                                                    lpn_id
4597         , to_number(NULL)                                                           transaction_action_id
4598         , to_char(NULL)                                                             transfer_subinventory_code
4599         , to_number(NULL)                                                           transfer_locator_id
4600         , to_number(NULL)                                                           status_id -- Onhand Material Status Support
4601      FROM mtl_reservations mr
4602      WHERE
4603           Nvl(mr.supply_source_type_id, 13) = 13
4604       AND mr.primary_reservation_quantity >
4605       Nvl(mr.detailed_quantity,0)
4606       AND ((l_no_lpn_reservations <>1)
4607            OR (l_no_lpn_reservations = 1 AND mr.lpn_id IS NULL))
4608       AND (l_tree_mode <> 3 OR
4609           (l_tree_mode = 3
4610       AND NOT (l_demand_source_type_id = mr.demand_source_type_id
4611                 AND l_demand_source_header_id = mr.demand_source_header_id
4612             AND Nvl(l_demand_source_line_id, -9999) =
4613          Nvl(mr.demand_source_line_id,-9999)
4614             AND Nvl(l_demand_source_name, '@@@###@@#') =
4615          Nvl(mr.demand_source_name,'@@@###@@#')
4616                 AND Nvl(l_demand_source_delivery,-9999) =
4617          Nvl(mr.demand_source_delivery,-9999)
4618                    )
4619         ))
4620      UNION ALL
4621        -- onhand quantities
4622        SELECT
4623           moq.organization_id               organization_id
4624         , moq.inventory_item_id             inventory_item_id
4625         , moq.revision                      revision
4626         , moq.lot_number                    lot_number
4627         , moq.subinventory_code             subinventory_code
4628         , moq.locator_id                    locator_id
4629         , moq.primary_transaction_quantity
4630         , moq.secondary_transaction_quantity
4631         , 1                                 quantity_type
4632         , moq.cost_group_id                 cost_group_id
4633         , moq.lpn_id                        lpn_id
4634         , to_number(NULL)                   transaction_action_id
4635         , to_char(NULL)                     transfer_subinventory_code
4636         , to_number(NULL)                   transfer_locator_id
4637         , moq.status_id                     -- Onhand Material Status Support
4638        FROM
4639           mtl_onhand_quantities_detail       moq
4640      UNION ALL
4641        -- pending transactions in mmtt
4642        SELECT
4643             mmtt.organization_id    organization_id
4644           , mmtt.inventory_item_id  inventory_item_id
4645           , mmtt.revision           revision
4646           , NULL              lot_number
4647           , mmtt.subinventory_code  subinventory_code
4648           , mmtt.locator_id         locator_id
4649      --Bug 4185621
4650      --, Decode(mmtt.transaction_status, 2, 1,
4651      , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
4652       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
4653             Sign(mmtt.primary_quantity)))
4654          * round(Abs(mmtt.primary_quantity),5)
4655      --Bug 4185621
4656      --, Decode(mmtt.transaction_status, 2, 1,
4657      , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
4658       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
4659             Sign(mmtt.secondary_transaction_quantity)))
4660          * round(Abs(mmtt.secondary_transaction_quantity),5)
4661      --Bug 4185621
4662      --, Decode(mmtt.transaction_status, 2, 5, 1)  quantity_type
4663      , Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,1,5), 1)  quantity_type
4664           , mmtt.cost_group_id       cost_group_id
4665           --14688297
4666          -- ,NVL(mmtt.allocated_lpn_id,
4667       --NVL(mmtt.content_lpn_id, mmtt.lpn_id)) lpn_id
4668      , decode(nvl(mmtt.wms_task_status,-1), 4, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID), NVL(MMTT.ALLOCATED_LPN_ID, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID))) lpn_id
4669           /* Bug 9938149, Decode(mmtt.transaction_status, 2 , mmtt.transaction_action_id,
4670            * to_number(NULL)) transaction_action_id */
4671           , Decode(mmtt.transaction_status, 2 , decode (mp.process_enabled_flag, 'Y',
4672                                                           decode ((select move_order_type
4673                                                                    from mtl_txn_request_headers mtrh
4674                                                                    where header_id = mmtt.move_order_header_id
4675                                                                    and rownum = 1),5,28,mmtt.transaction_action_id),
4676                                                         mmtt.transaction_action_id), to_number(NULL)) transaction_action_id
4677           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_subinventory, to_char(NULL)) transfer_subinventory_code
4678           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_to_location, to_number(NULL))  transfer_locator_id
4679           , inv_material_status_grp.get_default_status(mmtt.organization_id
4680                                           ,mmtt.inventory_item_id
4681                                           ,mmtt.subinventory_code
4682                                           ,mmtt.locator_id
4683                                           ,null -- lot_number
4684                                           ,NVL(mmtt.allocated_lpn_id,
4685                                  NVL(mmtt.content_lpn_id, mmtt.lpn_id))
4686                                           ) status_id -- Onhand Material Status Support
4687        FROM
4688             mtl_material_transactions_temp mmtt,
4689             mtl_parameters mp -- Bug 9938149
4690        WHERE
4691               mmtt.posting_flag = 'Y'
4692        AND mmtt.subinventory_code IS NOT NULL
4693     AND (Nvl(mmtt.transaction_status,0) <> 2 OR
4694          Nvl(mmtt.transaction_status,0) = 2 AND
4695          mmtt.transaction_action_id IN (1,2,28,3,21,29,32,34)
4696         )
4697       -- dont look at scrap and costing txns
4698       -- Bug 3396558 fix. Ignore ownership xfr,planning xfr transactions
4699          AND mmtt.transaction_action_id NOT IN (5,6,24,30)
4700          AND mmtt.organization_id = mp.organization_id -- Bug 9938149
4701        UNION ALL
4702        -- receiving side of transfers
4703        -- added 5/23/00
4704        -- if quantity is in an lpn, then it is containerized
4705        -- Bug 7658493, If wms task is in loaded status, consider allocation like pending transaction.
4706        SELECT
4707             Decode(mmtt.transaction_action_id
4708                , 3, mmtt.transfer_organization
4709                , mmtt.organization_id)   organization_id
4710           , mmtt.inventory_item_id       inventory_item_id
4711           , mmtt.revision                revision
4712           , NULL                         lot_number
4713           , mmtt.transfer_subinventory   subinventory_code
4714           , mmtt.transfer_to_location    locator_id
4715           , round(Abs(mmtt.primary_quantity),5)
4716           , round(Abs(mmtt.secondary_transaction_quantity),5)
4717           , 1                            quantity_type
4718           , mmtt.transfer_cost_group_id  cost_group_id
4719           , NVL(mmtt.content_lpn_id,mmtt.transfer_lpn_id) lpn_id
4720           , to_number(NULL)                      transaction_action_id
4721           , to_char(NULL)                        transfer_subinventory_code
4722           , to_number(NULL)                      transfer_locator_id
4723           , inv_material_status_grp.get_default_status(Decode(mmtt.transaction_action_id
4724                                                , 3, mmtt.transfer_organization
4725                                                , mmtt.organization_id)
4726                                           ,mmtt.inventory_item_id
4727                                           ,mmtt.transfer_subinventory
4728                                           ,mmtt.transfer_to_location
4729                                           ,null -- lot_number
4730                                           ,NVL(mmtt.content_lpn_id,mmtt.transfer_lpn_id)
4731                                           ,mmtt.transaction_action_id
4732                                           ,inv_material_status_grp.get_default_status(mmtt.organization_id
4733                                                   ,mmtt.inventory_item_id
4734                                                   ,mmtt.subinventory_code
4735                                                   ,mmtt.locator_id
4736                                                   ,null -- lot_number
4737                                                   ,NVL(mmtt.allocated_lpn_id,
4738                                         NVL(mmtt.content_lpn_id, mmtt.lpn_id))
4739                                                   )
4740                                           ) status_id -- Onhand Material Status Support
4741        FROM
4742             mtl_material_transactions_temp mmtt
4743        WHERE
4744              mmtt.posting_flag = 'Y'
4745          AND Decode( Nvl(mmtt.transaction_status,0),
4746                        2, decode(nvl(mmtt.wms_task_status,-1), 4, 1, 2),
4747                        1 ) <> 2
4748            AND (mmtt.transaction_action_id IN (2,3) -- Bug 9595634: Removed 28
4749                -- Bug 9595634: Added condition to match demand source if action = 28
4750                 OR (mmtt.transaction_action_id = 28
4751                     AND l_demand_source_header_id = mmtt.transaction_source_id
4752                     AND Nvl(l_demand_source_line_id, -9999) = Nvl(mmtt.TRX_SOURCE_LINE_ID,-9999)
4753                     AND Nvl(l_demand_source_name, '@@@###@@#') = Nvl(mmtt.TRANSACTION_SOURCE_NAME,'@@@###@@#')
4754                     AND Nvl(l_demand_source_delivery,-9999) = Nvl(mmtt.TRX_SOURCE_DELIVERY_ID,-9999)
4755                    )
4756                )
4757      --bug 3581133
4758        AND mmtt.wip_supply_type IS NULL
4759       ) x
4760       WHERE x.organization_id    = l_organization_id
4761         AND x.inventory_item_id  = l_inventory_item_id
4762       GROUP BY
4763            x.organization_id, x.inventory_item_id, x.revision
4764           , x.lot_number,x.subinventory_code, x.locator_id
4765           , x.quantity_type, x.cost_group_id, x.lpn_id
4766           , x.transaction_action_id, x.transfer_subinventory_code
4767           , x.transfer_locator_id, x.status_id -- Onhand Material Status Support
4768    ) x
4769     , mtl_secondary_inventories sub
4770     , mtl_item_locations loc
4771     , mtl_parameters mp -- Onhand Material Status Support
4772     , mtl_material_statuses_b mms -- Onhand Material Status Support
4773   WHERE
4774         x.organization_id = loc.organization_id  (+)
4775     AND x.locator_id = loc.inventory_location_id (+)
4776     AND x.organization_id    = sub.organization_id         (+)
4777     AND x.subinventory_code = sub.secondary_inventory_name (+)
4778     AND x.organization_id    = mp.organization_id (+) -- Onhand Material Status Support
4779     AND x.status_id = mms.status_id (+) -- Onhand Material Status Support
4780     AND (l_asset_subs_only = 2 OR
4781          NVL(sub.asset_inventory,1) = 1)
4782     AND (
4783          (mp.default_status_id is null and
4784           ( (l_onhand_source =1 AND
4785              Nvl(sub.inventory_atp_code, 1) = 1
4786              AND Nvl(loc.inventory_atp_code, 1) = 1
4787        )
4788             OR (l_onhand_source = 2 AND
4789        Nvl(sub.availability_type, 1) = 1
4790                  AND Nvl(loc.availability_type, 1) = 1
4791           )
4792        OR l_onhand_source =3
4793             OR (l_onhand_source = 4 AND
4794            (nvl(sub.inventory_atp_code,1) = 1
4795                  AND Nvl(loc.inventory_atp_code, 1) = 1
4796                  AND Nvl(loc.availability_type, 1) = 1
4797                  AND nvl(sub.availability_type,1)=1
4798                 )
4799                )
4800           )
4801          )
4802          OR
4803          (
4804           mp.default_status_id is not null and
4805            ( (l_onhand_source =1 AND
4806                Nvl(mms.inventory_atp_code, 1) = 1
4807              )
4808              OR (l_onhand_source = 2 AND
4809                  Nvl(mms.availability_type, 1) = 1
4810            )
4811         OR l_onhand_source =3
4812              OR (l_onhand_source = 4 AND
4813              (nvl(mms.inventory_atp_code,1) = 1
4814               AND nvl(mms.availability_type,1)=1
4815                   )
4816                 )
4817            )
4818          )
4819     )
4820     GROUP BY
4821           x.organization_id
4822         , x.inventory_item_id
4823         , x.revision
4824         , NULL
4825         , NULL
4826         , x.subinventory_code
4827         , sub.reservable_type
4828         , x.locator_id
4829         , x.quantity_type
4830         , x.cost_group_id
4831         , x.lpn_id
4832         , x.transaction_action_id
4833         , x.transfer_subinventory_code
4834         , x.transfer_locator_id
4835         , x.status_id; -- Onhand Material Status Support
4836      -- invConv changes end : c_plain with MMS about ATP/Nettable...
4837 
4838    --Bug#7628989,add new cursor c_no_lot
4839    CURSOR c_no_lot IS
4840       SELECT
4841           x.organization_id       organization_id
4842         , x.inventory_item_id     inventory_item_id
4843         , x.revision              revision
4844         , NULL        lot_number
4845         , lot.expiration_date     lot_expiration_date
4846         , x.subinventory_code     subinventory_code
4847         , sub.reservable_type     reservable_type
4848         , x.locator_id            locator_id
4849         , x.primary_quantity      primary_quantity
4850         , x.secondary_quantity    secondary_quantity
4851         , x.quantity_type         quantity_type
4852         , x.cost_group_id         cost_group_id
4853         , x.lpn_id        lpn_id
4854         , x.transaction_action_id       transaction_action_id
4855         , x.transfer_subinventory_code  transfer_subinventory_code
4856         , x.transfer_locator_id         transfer_locator_id
4857         , lot.reservable_type     lot_reservable_type       --Bug#8713821 to check reservable type
4858      FROM (
4859        SELECT
4860            x.organization_id       organization_id
4861          , x.inventory_item_id     inventory_item_id
4862          , decode(l_revision_control, 2, NULL, x.revision) revision
4863          , x.lot_number            lot_number
4864          , x.subinventory_code     subinventory_code
4865          , x.locator_id            locator_id
4866          , SUM(x.primary_quantity) primary_quantity
4867          , SUM(x.secondary_quantity) secondary_quantity
4868          , x.quantity_type         quantity_type
4869          , x.cost_group_id         cost_group_id
4870          , x.lpn_id        lpn_id
4871          , x.transaction_action_id       transaction_action_id
4872          , x.transfer_subinventory_code  transfer_subinventory_code
4873          , x.transfer_locator_id         transfer_locator_id
4874         FROM (
4875        -- reservations
4876        SELECT
4877           mr.organization_id       organization_id
4878         , mr.inventory_item_id     inventory_item_id
4879         , mr.revision              revision
4880         , mr.lot_number            lot_number
4881         , mr.subinventory_code     subinventory_code
4882         , mr.locator_id            locator_id
4883         , mr.primary_reservation_quantity
4884            - Nvl(mr.detailed_quantity,0)    primary_quantity
4885         , mr.secondary_reservation_quantity
4886            - Nvl(mr.secondary_detailed_quantity,0)    secondary_quantity
4887         , 3                        quantity_type
4888         , to_number(NULL)      cost_group_id
4889         , lpn_id           lpn_id
4890         , to_number(NULL)                      transaction_action_id
4891         , to_char(NULL)                        transfer_subinventory_code
4892         , to_number(NULL)                      transfer_locator_id
4893      FROM mtl_reservations mr
4894      WHERE
4895           Nvl(mr.supply_source_type_id, 13) = 13
4896       AND mr.primary_reservation_quantity >
4897         Nvl(mr.detailed_quantity,0)
4898       AND ((l_no_lpn_reservations <>1)
4899            OR (l_no_lpn_reservations = 1 AND mr.lpn_id IS NULL))
4900       AND (l_tree_mode <> 3 OR
4901           (l_tree_mode = 3
4902        AND NOT
4903             ( l_demand_source_type_id = mr.demand_source_type_id
4904             AND l_demand_source_header_id =  mr.demand_source_header_id
4905             AND Nvl(l_demand_source_line_id, -9999) =
4906         Nvl(mr.demand_source_line_id,-9999)
4907         AND Nvl(l_demand_source_name, '@@@###@@#') =
4908         Nvl(mr.demand_source_name,'@@@###@@#')
4909         AND Nvl(l_demand_source_delivery,-9999) =
4910         Nvl(mr.demand_source_delivery,-9999)
4911               )
4912     ))
4913      UNION ALL
4914        -- onhand quantities
4915        SELECT
4916           moq.organization_id               organization_id
4917         , moq.inventory_item_id             inventory_item_id
4918         , moq.revision                      revision
4919         , moq.lot_number                    lot_number
4920         , moq.subinventory_code             subinventory_code
4921         , moq.locator_id                    locator_id
4922         , moq.primary_transaction_quantity
4923         , moq.secondary_transaction_quantity
4924         , 1                                 quantity_type
4925         , moq.cost_group_id                 cost_group_id
4926         , moq.lpn_id                        lpn_id
4927         , to_number(NULL)                   transaction_action_id
4928         , to_char(NULL)                     transfer_subinventory_code
4929         , to_number(NULL)                   transfer_locator_id
4930        FROM
4931           mtl_onhand_quantities_detail       moq
4932      UNION ALL
4933        -- pending transactions in mmtt, lot in MMTT
4934        SELECT
4935             mmtt.organization_id    organization_id
4936           , mmtt.inventory_item_id  inventory_item_id
4937           , mmtt.revision           revision
4938           , mmtt.lot_number         lot_number
4939           , mmtt.subinventory_code  subinventory_code
4940           , mmtt.locator_id         locator_id
4941       --Bug 4185621
4942           --, Decode(mmtt.transaction_status, 2, 1,
4943           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
4944         Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
4945             Sign(mmtt.primary_quantity)))
4946             * round(Abs(mmtt.primary_quantity),5)
4947       --Bug 4185621
4948           --, Decode(mmtt.transaction_status, 2, 1,
4949           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
4950         Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
4951             Sign(mmtt.secondary_transaction_quantity)))
4952             * round(Abs(mmtt.secondary_transaction_quantity),5)
4953       --Bug 4185621
4954           --, Decode(mmtt.transaction_status, 2, 5, 1) quantity_type
4955           , Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,1,5), 1)  quantity_type
4956           , mmtt.cost_group_id      cost_group_id
4957           --14688297
4958      -- ,NVL(mmtt.allocated_lpn_id,
4959      --   NVL(mmtt.content_lpn_id, mmtt.lpn_id)) lpn_id
4960        , decode(nvl(mmtt.wms_task_status,-1), 4, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID), NVL(MMTT.ALLOCATED_LPN_ID, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID))) lpn_id
4961           /* Bug 9938149, Decode(mmtt.transaction_status, 2 , mmtt.transaction_action_id,
4962            * to_number(NULL)) transaction_action_id */
4963           , Decode(mmtt.transaction_status, 2 , decode (mp.process_enabled_flag, 'Y',
4964                                                           decode ((select move_order_type
4965                                                                    from mtl_txn_request_headers mtrh
4966                                                                    where header_id = mmtt.move_order_header_id
4967                                                                    and rownum = 1),5,28,mmtt.transaction_action_id),
4968                                                         mmtt.transaction_action_id), to_number(NULL)) transaction_action_id
4969           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_subinventory, to_char(NULL)) transfer_subinventory_code
4970           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_to_location, to_number(NULL))  transfer_locator_id
4971        FROM
4972             mtl_material_transactions_temp mmtt,
4973             mtl_parameters mp -- Bug 9938149
4974        WHERE
4975             mmtt.posting_flag = 'Y'
4976      AND mmtt.lot_number IS NOT NULL
4977      AND mmtt.subinventory_code IS NOT NULL
4978      AND (Nvl(mmtt.transaction_status,0) <> 2 OR
4979             Nvl(mmtt.transaction_status,0) = 2 AND
4980             mmtt.transaction_action_id IN (1,2,28,3,21,29,32,34)
4981          )
4982        -- dont look at scrap and costing txns
4983        -- Bug 3396558 fix. Ignore ownership xfr,planning xfr transactions
4984          AND mmtt.transaction_action_id NOT IN (5,6,24,30)
4985          AND mmtt.organization_id = mp.organization_id -- Bug 9938149
4986     UNION ALL
4987        --MMTT records, lot in MTLT
4988        SELECT
4989             mmtt.organization_id    organization_id
4990           , mmtt.inventory_item_id  inventory_item_id
4991           , mmtt.revision           revision
4992           , mtlt.lot_number         lot_number
4993           , mmtt.subinventory_code  subinventory_code
4994           , mmtt.locator_id         locator_id
4995       --Bug 4185621
4996           --, Decode(mmtt.transaction_status, 2, 1,
4997           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
4998         Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
4999         Sign(mmtt.primary_quantity)))
5000         * round(Abs( mtlt.primary_quantity ),5)
5001       --Bug 4185621
5002       --, Decode(mmtt.transaction_status, 2, 1,
5003           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
5004         Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
5005         Sign(mmtt.secondary_transaction_quantity)))
5006         * round(Abs( mtlt.secondary_quantity ),5)
5007       --Bug 4185621
5008           --, Decode(mmtt.transaction_status, 2, 5, 1) quantity_type
5009           , Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,1,5), 1)  quantity_type
5010           , mmtt.cost_group_id      cost_group_id
5011       --14688297
5012      -- ,NVL(mmtt.allocated_lpn_id,
5013      --   NVL(mmtt.content_lpn_id, mmtt.lpn_id)) lpn_id
5014 , decode(nvl(mmtt.wms_task_status,-1), 4, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID), NVL(MMTT.ALLOCATED_LPN_ID, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID))) lpn_id
5015           /* Bug 9938149, Decode(mmtt.transaction_status, 2 , mmtt.transaction_action_id,
5016            * to_number(NULL)) transaction_action_id */
5017           , Decode(mmtt.transaction_status, 2 , decode (mp.process_enabled_flag, 'Y',
5018                                                           decode ((select move_order_type
5019                                                                    from mtl_txn_request_headers mtrh
5020                                                                    where header_id = mmtt.move_order_header_id
5021                                                                    and rownum = 1),5,28,mmtt.transaction_action_id),
5022                                                         mmtt.transaction_action_id), to_number(NULL)) transaction_action_id
5023           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_subinventory, to_char(NULL)) transfer_subinventory_code
5024           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_to_location, to_number(NULL))  transfer_locator_id
5025        FROM
5026            mtl_material_transactions_temp mmtt,
5027        mtl_transaction_lots_temp mtlt,
5028        mtl_parameters mp -- Bug 9938149
5029        WHERE
5030              mmtt.posting_flag = 'Y'
5031          AND mmtt.transaction_temp_id = mtlt.transaction_temp_id
5032          AND mmtt.lot_number IS NULL
5033      AND mmtt.subinventory_code IS NOT NULL
5034      AND (Nvl(mmtt.transaction_status,0) <> 2 OR
5035           Nvl(mmtt.transaction_status,0) = 2 AND
5036          mmtt.transaction_action_id IN (1,2,28,3,21,29,32,34)
5037        )
5038        -- dont look at scrap and costing txns
5039        -- Bug 3396558 fix. Ignore ownership xfr,planning xfr transactions
5040          AND mmtt.transaction_action_id NOT IN (5,6,24,30)
5041          AND mmtt.organization_id = mp.organization_id -- Bug 9938149
5042       --bug 9241240: MMTT with no MTLT
5043       --             Fix for incorrect ATT/ATR shown in WIP comp issue (MMTT inserted without lot number)
5044     UNION ALL
5045        --MMTT records, no lot in MTLT or MMTT
5046        SELECT
5047             mmtt.organization_id    organization_id
5048           , mmtt.inventory_item_id  inventory_item_id
5049           , mmtt.revision           revision
5050           , mmtt.lot_number         lot_number
5051           , mmtt.subinventory_code  subinventory_code
5052           , mmtt.locator_id         locator_id
5053           , round(mmtt.primary_quantity,5)
5054           , round(mmtt.secondary_transaction_quantity,5)             -- invConv change
5055           , Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,1,5), 1)  quantity_type  --FlexiLotAlloc
5056           , mmtt.cost_group_id      cost_group_id
5057          --14688297
5058          -- , NVL(mmtt.allocated_lpn_id, NVL(mmtt.content_lpn_id, mmtt.lpn_id)) lpn_id
5059          , decode(nvl(mmtt.wms_task_status,-1), 4, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID), NVL(MMTT.ALLOCATED_LPN_ID, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID))) lpn_id
5060           , to_number(NULL)         transaction_action_id
5061           , to_char(NULL)           transfer_subinventory_code
5062           , to_number(NULL)         transfer_locator_id
5063        FROM  mtl_material_transactions_temp mmtt
5064       WHERE  mmtt.posting_flag = 'Y'
5065         AND  mmtt.lot_number IS NULL
5066         AND  mmtt.subinventory_code IS NOT NULL
5067         AND  (mmtt.transaction_status IS NULL OR
5068               Nvl(mmtt.transaction_status,0) = 2 AND
5069               mmtt.transaction_action_id IN (1,2,27,28))   --FlexiLotAlloc
5070         AND  mmtt.transaction_action_id in (1,2,27,28) --FlexiLotAlloc and BUG 10070839
5071         AND  mmtt.transaction_source_type_id in (2,4,5,8) --FlexiLotAlloc
5072         AND  nvl(mmtt.wip_entity_type,-1) NOT IN (9,10)
5073         AND  NOT EXISTS (SELECT 1 FROM mtl_transaction_lots_temp mtlt
5074                          WHERE mmtt.transaction_temp_id = mtlt.transaction_temp_id)
5075     UNION ALL
5076        -- receiving side of transfers, lot in MMTT
5077        SELECT
5078             Decode(mmtt.transaction_action_id
5079                , 3, mmtt.transfer_organization
5080                , mmtt.organization_id)   organization_id
5081           , mmtt.inventory_item_id       inventory_item_id
5082           , mmtt.revision                revision
5083           , mmtt.lot_number              lot_number
5084           , mmtt.transfer_subinventory   subinventory_code
5085           , mmtt.transfer_to_location    locator_id
5086           , round(Abs( mmtt.primary_quantity),5)
5087           , round(Abs( mmtt.secondary_transaction_quantity),5)
5088           , 1                            quantity_type
5089           , mmtt.transfer_cost_group_id  cost_group_id
5090           , NVL(mmtt.content_lpn_id,mmtt.transfer_lpn_id) lpn_id
5091           , to_number(NULL)                      transaction_action_id
5092           , to_char(NULL)                        transfer_subinventory_code
5093           , to_number(NULL)                      transfer_locator_id
5094        FROM
5095             mtl_material_transactions_temp mmtt
5096        WHERE
5097              mmtt.posting_flag = 'Y'
5098       AND mmtt.lot_number IS NOT NULL
5099       AND Decode( Nvl(mmtt.transaction_status,0),
5100                        2, decode(nvl(mmtt.wms_task_status,-1), 4, 1, 2),
5101                        1 ) <> 2
5102       AND (mmtt.transaction_action_id IN (2,3) -- Bug 9595634: Removed 28
5103            -- Bug 9595634: Added condition to match demand source if action = 28
5104            OR (mmtt.transaction_action_id = 28
5105                AND l_demand_source_header_id = mmtt.transaction_source_id
5106                AND Nvl(l_demand_source_line_id, -9999) = Nvl(mmtt.TRX_SOURCE_LINE_ID,-9999)
5107                AND Nvl(l_demand_source_name, '@@@###@@#') = Nvl(mmtt.TRANSACTION_SOURCE_NAME,'@@@###@@#')
5108                AND Nvl(l_demand_source_delivery,-9999) = Nvl(mmtt.TRX_SOURCE_DELIVERY_ID,-9999)
5109               )
5110           )
5111   UNION ALL
5112        -- receiving side of transfers, lot in MTLT
5113        SELECT
5114             Decode(mmtt.transaction_action_id
5115                , 3, mmtt.transfer_organization
5116                , mmtt.organization_id)   organization_id
5117           , mmtt.inventory_item_id       inventory_item_id
5118           , mmtt.revision                revision
5119           , mtlt.lot_number              lot_number
5120           , mmtt.transfer_subinventory   subinventory_code
5121           , mmtt.transfer_to_location    locator_id
5122           , round(Abs( mtlt.primary_quantity ),5)
5123           , round(Abs( mtlt.secondary_quantity ),5)
5124           , 1                            quantity_type
5125           , mmtt.transfer_cost_group_id  cost_group_id
5126           , NVL(mmtt.content_lpn_id,mmtt.transfer_lpn_id) lpn_id
5127           , to_number(NULL)                      transaction_action_id
5128           , to_char(NULL)                        transfer_subinventory_code
5129           , to_number(NULL)                      transfer_locator_id
5130        FROM
5131             mtl_material_transactions_temp mmtt,
5132         mtl_transaction_lots_temp mtlt
5133        WHERE
5134              mmtt.posting_flag = 'Y'
5135           AND mmtt.lot_number IS NULL
5136           AND mmtt.transaction_temp_id = mtlt.transaction_temp_id
5137       AND Decode( Nvl(mmtt.transaction_status,0),
5138                        2, decode(nvl(mmtt.wms_task_status,-1), 4, 1, 2),
5139                        1 ) <> 2
5140            AND (mmtt.transaction_action_id IN (2,3) -- Bug 9595634: Removed 28
5141                -- Bug 9595634: Added condition to match demand source if action = 28
5142                 OR (mmtt.transaction_action_id = 28
5143                     AND l_demand_source_header_id = mmtt.transaction_source_id
5144                     AND Nvl(l_demand_source_line_id, -9999) = Nvl(mmtt.TRX_SOURCE_LINE_ID,-9999)
5145                     AND Nvl(l_demand_source_name, '@@@###@@#') = Nvl(mmtt.TRANSACTION_SOURCE_NAME,'@@@###@@#')
5146                     AND Nvl(l_demand_source_delivery,-9999) = Nvl(mmtt.TRX_SOURCE_DELIVERY_ID,-9999)
5147                    )
5148                )
5149       ) x
5150       WHERE x.organization_id    = l_organization_id
5151         AND x.inventory_item_id  = l_inventory_item_id
5152       GROUP BY
5153            x.organization_id, x.inventory_item_id, x.revision
5154           , x.lot_number,x.subinventory_code, x.locator_id
5155           , x.quantity_type, x.cost_group_id, x.lpn_id
5156           , x.transaction_action_id, x.transfer_subinventory_code
5157           , x.transfer_locator_id
5158    ) x
5159     , mtl_secondary_inventories sub
5160     , mtl_lot_numbers lot
5161  WHERE
5162    x.organization_id    = sub.organization_id          (+)
5163    AND x.subinventory_code = sub.secondary_inventory_name (+)
5164    AND x.organization_id   = lot.organization_id   (+)
5165    AND x.inventory_item_id = lot.inventory_item_id (+)
5166    AND x.lot_number        = lot.lot_number        (+)
5167    AND (l_asset_subs_only = 2 OR
5168          NVL(sub.asset_inventory,1) = 1)
5169    AND ((l_onhand_source = 1 AND
5170      Nvl(sub.inventory_atp_code, 1) = 1
5171     ) OR
5172         (l_onhand_source = 2 AND
5173          Nvl(sub.availability_type, 1) = 1
5174     ) OR
5175     l_onhand_source =3
5176     OR
5177     (l_onhand_source = 4 AND
5178      Nvl(sub.inventory_atp_code, 1) = 1 AND
5179      Nvl(sub.availability_type, 1) = 1)
5180       )
5181    ;
5182 
5183 CURSOR c_unit_no_lot IS
5184    SELECT
5185           x.organization_id       organization_id
5186         , x.inventory_item_id     inventory_item_id
5187         , x.revision              revision
5188         , NULL        lot_number
5189         , lot.expiration_date     lot_expiration_date
5190         , x.subinventory_code     subinventory_code
5191         , sub.reservable_type     reservable_type
5192         , x.locator_id            locator_id
5193         , x.primary_quantity      primary_quantity
5194         , x.secondary_quantity    secondary_quantity
5195         , x.quantity_type         quantity_type
5196         , x.cost_group_id         cost_group_id
5197         , x.lpn_id        lpn_id
5198         , x.transaction_action_id       transaction_action_id
5199         , x.transfer_subinventory_code  transfer_subinventory_code
5200         , x.transfer_locator_id         transfer_locator_id
5201         , lot.reservable_type     lot_reservable_type       --Bug#8713821 to check reservable type
5202      FROM (
5203        SELECT
5204            x.organization_id       organization_id
5205          , x.inventory_item_id     inventory_item_id
5206          , decode(l_revision_control, 2, NULL
5207             , x.revision)       revision
5208          , x.lot_number             lot_number
5209          , x.subinventory_code     subinventory_code
5210          , x.locator_id            locator_id
5211          , SUM(x.primary_quantity) primary_quantity
5212          , SUM(x.secondary_quantity) secondary_quantity
5213          , x.quantity_type         quantity_type
5214          , x.cost_group_id         cost_group_id
5215          , x.lpn_id        lpn_id
5216          , x.transaction_action_id       transaction_action_id
5217          , x.transfer_subinventory_code  transfer_subinventory_code
5218          , x.transfer_locator_id         transfer_locator_id
5219         FROM (
5220        -- reservations
5221        SELECT
5222           mr.organization_id       organization_id
5223         , mr.inventory_item_id     inventory_item_id
5224         , mr.revision              revision
5225         , mr.lot_number            lot_number
5226         , mr.subinventory_code     subinventory_code
5227         , mr.locator_id            locator_id
5228         , mr.primary_reservation_quantity
5229            - Nvl(mr.detailed_quantity,0)    primary_quantity
5230         , mr.secondary_reservation_quantity
5231            - Nvl(mr.secondary_detailed_quantity,0)    secondary_quantity
5232         , 3                        quantity_type
5233         , to_number(NULL)         cost_group_id
5234         , lpn_id           lpn_id
5235         , to_number(NULL)                      transaction_action_id
5236         , to_char(NULL)                        transfer_subinventory_code
5237         , to_number(NULL)                      transfer_locator_id
5238      FROM mtl_reservations mr
5239      WHERE
5240           Nvl(mr.supply_source_type_id, 13) = 13
5241       AND mr.primary_reservation_quantity >
5242         Nvl(mr.detailed_quantity,0)
5243       AND ((l_no_lpn_reservations <>1)
5244            OR (l_no_lpn_reservations = 1 AND mr.lpn_id IS NULL))
5245       AND (l_tree_mode <> 3 OR
5246           (l_tree_mode = 3
5247        AND NOT (l_demand_source_type_id = mr.demand_source_type_id
5248         AND l_demand_source_header_id = mr.demand_source_header_id
5249         AND Nvl(l_demand_source_line_id, -9999) =
5250             Nvl(mr.demand_source_line_id,-9999)
5251         AND Nvl(l_demand_source_name, '@@@###@@#') =
5252             Nvl(mr.demand_source_name,'@@@###@@#')
5253         AND Nvl(l_demand_source_delivery,-9999) =
5254             Nvl(mr.demand_source_delivery,-9999)
5255               )
5256       ))
5257      UNION ALL
5258        -- onhand quantities
5259        SELECT
5260           moq.organization_id               organization_id
5261         , moq.inventory_item_id             inventory_item_id
5262         , moq.revision                      revision
5263         , moq.lot_number                    lot_number
5264         , moq.subinventory_code             subinventory_code
5265         , moq.locator_id                    locator_id
5266         , decode(l_demand_source_line_id,
5267             NULL, sum(moq.primary_transaction_quantity),
5268             pjm_ueff_onhand.onhand_quantity(
5269                  l_demand_source_line_id
5270                 ,moq.inventory_item_id
5271                 ,moq.organization_id
5272                 ,moq.revision
5273                 ,moq.subinventory_code
5274                 ,moq.locator_id
5275                 ,moq.lot_number
5276                 ,moq.lpn_id
5277                 ,moq.cost_group_id)
5278           )
5279         , decode(l_demand_source_line_id,
5280             NULL, sum(moq.secondary_transaction_quantity),
5281             pjm_ueff_onhand.onhand_quantity(
5282                  l_demand_source_line_id
5283                 ,moq.inventory_item_id
5284                 ,moq.organization_id
5285                 ,moq.revision
5286                 ,moq.subinventory_code
5287                 ,moq.locator_id
5288                 ,moq.lot_number
5289                 ,moq.lpn_id
5290                 ,moq.cost_group_id)
5291           )
5292         , 1                                 quantity_type
5293         , moq.cost_group_id                 cost_group_id
5294         , moq.lpn_id                        lpn_id
5295         , to_number(NULL)                   transaction_action_id
5296         , to_char(NULL)                     transfer_subinventory_code
5297         , to_number(NULL)                   transfer_locator_id
5298        FROM
5299           mtl_onhand_quantities_detail moq
5300        GROUP BY moq.organization_id,moq.inventory_item_id,moq.revision,
5301             moq.subinventory_code,moq.locator_id,moq.lot_number,
5302             moq.lpn_id,moq.cost_group_id
5303      UNION ALL
5304        -- pending transactions in mmtt, lot in MMTT
5305        SELECT
5306             mmtt.organization_id    organization_id
5307           , mmtt.inventory_item_id  inventory_item_id
5308           , mmtt.revision           revision
5309           , mmtt.lot_number         lot_number
5310           , mmtt.subinventory_code  subinventory_code
5311           , mmtt.locator_id         locator_id
5312       --Bug 4185621
5313           --, Decode(mmtt.transaction_status, 2, 1,
5314           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
5315         Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
5316             Sign(mmtt.primary_quantity))) *
5317         round(Abs(decode(l_demand_source_line_id,
5318             NULL, mmtt.primary_quantity,
5319             Nvl(pjm_ueff_onhand.txn_quantity(
5320                      l_demand_source_line_id
5321                 ,mmtt.transaction_temp_id
5322                 ,mmtt.lot_number
5323                     ,'Y'
5324                 ,mmtt.inventory_item_id
5325                 ,mmtt.organization_id
5326                 ,mmtt.transaction_source_type_id
5327                 ,mmtt.transaction_source_id
5328                 ,mmtt.rcv_transaction_id
5329                     ,sign(mmtt.primary_quantity)
5330                    ),mmtt.primary_quantity
5331             )
5332         )),5)
5333       --Bug 4185621
5334           --, Decode(mmtt.transaction_status, 2, 1,
5335           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
5336         Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
5337             Sign(mmtt.secondary_transaction_quantity))) *
5338         round(Abs(decode(l_demand_source_line_id,
5339             NULL, mmtt.secondary_transaction_quantity,
5340             Nvl(pjm_ueff_onhand.txn_quantity(
5341                      l_demand_source_line_id
5342                 ,mmtt.transaction_temp_id
5343                 ,mmtt.lot_number
5344                     ,'Y'
5345                 ,mmtt.inventory_item_id
5346                 ,mmtt.organization_id
5347                 ,mmtt.transaction_source_type_id
5348                 ,mmtt.transaction_source_id
5349                 ,mmtt.rcv_transaction_id
5350                     ,sign(mmtt.secondary_transaction_quantity)
5351                    ),mmtt.secondary_transaction_quantity
5352             )
5353         )),5)
5354     --Bug 4185621
5355         --, Decode(mmtt.transaction_status, 2, 5, 1) quantity_type
5356         , Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,1,5), 1)  quantity_type
5357         , mmtt.cost_group_id        cost_group_id
5358     --14688297
5359    -- ,NVL(mmtt.allocated_lpn_id,
5360      --   NVL(mmtt.content_lpn_id, mmtt.lpn_id)) lpn_id
5361 , decode(nvl(mmtt.wms_task_status,-1), 4, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID), NVL(MMTT.ALLOCATED_LPN_ID, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID))) lpn_id
5362           /* Bug 9938149, Decode(mmtt.transaction_status, 2 , mmtt.transaction_action_id,
5363            * to_number(NULL)) transaction_action_id */
5364           , Decode(mmtt.transaction_status, 2 , decode (mp.process_enabled_flag, 'Y',
5365                                                           decode ((select move_order_type
5366                                                                    from mtl_txn_request_headers mtrh
5367                                                                    where header_id = mmtt.move_order_header_id
5368                                                                    and rownum = 1),5,28,mmtt.transaction_action_id),
5369                                                         mmtt.transaction_action_id), to_number(NULL)) transaction_action_id
5370           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_subinventory, to_char(NULL)) transfer_subinventory_code
5371           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_to_location, to_number(NULL))  transfer_locator_id
5372        FROM
5373             mtl_material_transactions_temp mmtt,
5374             mtl_parameters mp -- Bug 9938149
5375        WHERE
5376             mmtt.posting_flag = 'Y'
5377      AND mmtt.lot_number IS NOT NULL
5378      AND mmtt.subinventory_code IS NOT NULL
5379      AND (Nvl(mmtt.transaction_status,0) <> 2 OR
5380             Nvl(mmtt.transaction_status,0) = 2 AND
5381         mmtt.transaction_action_id IN (1,2,28,3,21,29,32,34)
5382          )
5383        -- dont look at scrap and costing txns
5384        -- Bug 3396558 fix. Ignore ownership xfr,planning xfr transactions
5385          AND mmtt.transaction_action_id NOT IN (5,6, 24,30)
5386          AND mmtt.organization_id = mp.organization_id -- Bug 9938149
5387     UNION ALL
5388         --MMTT records, lot in MTLT
5389        SELECT
5390             mmtt.organization_id    organization_id
5391           , mmtt.inventory_item_id  inventory_item_id
5392           , mmtt.revision           revision
5393           , mtlt.lot_number         lot_number
5394           , mmtt.subinventory_code  subinventory_code
5395           , mmtt.locator_id         locator_id
5396       --Bug 4185621
5397           --, Decode(mmtt.transaction_status, 2, 1,
5398           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
5399         Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
5400             Sign(mmtt.primary_quantity))) *
5401         round(Abs(decode(l_demand_source_line_id,
5402             NULL, mtlt.primary_quantity,
5403             Nvl(pjm_ueff_onhand.txn_quantity(
5404                      l_demand_source_line_id
5405                 ,mmtt.transaction_temp_id
5406                 ,mtlt.lot_number
5407                     ,'Y'
5408                 ,mmtt.inventory_item_id
5409                 ,mmtt.organization_id
5410                 ,mmtt.transaction_source_type_id
5411                 ,mmtt.transaction_source_id
5412                 ,mmtt.rcv_transaction_id
5413                     ,sign(mmtt.primary_quantity)
5414                    ),mtlt.primary_quantity)
5415         )),5)
5416       --Bug 4185621
5417           --, Decode(mmtt.transaction_status, 2, 1,
5418           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
5419         Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
5420             Sign(mmtt.secondary_transaction_quantity))) *
5421         round(Abs(decode(l_demand_source_line_id,
5422             NULL, mtlt.secondary_quantity,
5423             Nvl(pjm_ueff_onhand.txn_quantity(
5424                      l_demand_source_line_id
5425                 ,mmtt.transaction_temp_id
5426                 ,mtlt.lot_number
5427                     ,'Y'
5428                 ,mmtt.inventory_item_id
5429                 ,mmtt.organization_id
5430                 ,mmtt.transaction_source_type_id
5431                 ,mmtt.transaction_source_id
5432                 ,mmtt.rcv_transaction_id
5433                     ,sign(mmtt.secondary_transaction_quantity)
5434                    ),mtlt.secondary_quantity)
5435         )),5)
5436      --Bug 4185621
5437          --, Decode(mmtt.transaction_status, 2, 5, 1) quantity_type
5438          , Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,1,5), 1)  quantity_type
5439           , mmtt.cost_group_id      cost_group_id
5440        --14688297
5441      -- ,NVL(mmtt.allocated_lpn_id,
5442        -- NVL(mmtt.content_lpn_id, mmtt.lpn_id)) lpn_id
5443 , decode(nvl(mmtt.wms_task_status,-1), 4, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID), NVL(MMTT.ALLOCATED_LPN_ID, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID))) lpn_id
5444           /* Bug 9938149, Decode(mmtt.transaction_status, 2 , mmtt.transaction_action_id,
5445            * to_number(NULL)) transaction_action_id */
5446           , Decode(mmtt.transaction_status, 2 , decode (mp.process_enabled_flag, 'Y',
5447                                                           decode ((select move_order_type
5448                                                                    from mtl_txn_request_headers mtrh
5449                                                                    where header_id = mmtt.move_order_header_id
5450                                                                    and rownum = 1),5,28,mmtt.transaction_action_id),
5451                                                         mmtt.transaction_action_id), to_number(NULL)) transaction_action_id
5452           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_subinventory, to_char(NULL)) transfer_subinventory_code
5453           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_to_location, to_number(NULL))  transfer_locator_id
5454        FROM
5455             mtl_material_transactions_temp mmtt,
5456        mtl_transaction_lots_temp mtlt,
5457        mtl_parameters mp -- bug 9938149
5458        WHERE
5459              mmtt.posting_flag = 'Y'
5460          AND mmtt.transaction_temp_id = mtlt.transaction_temp_id
5461          AND mmtt.lot_number IS NULL
5462      AND mmtt.subinventory_code IS NOT NULL
5463      AND (Nvl(mmtt.transaction_status,0) <> 2 OR
5464             Nvl(mmtt.transaction_status,0) = 2 AND
5465             mmtt.transaction_action_id IN (1,2,28,3,21,29,32,34)
5466          )
5467        -- dont look at scrap and costing txns
5468        -- Bug 3396558 fix. Ignore ownership xfr,planning xfr transactions
5469          AND mmtt.transaction_action_id NOT IN (5,6, 24,30)
5470          AND mmtt.organization_id = mp.organization_id -- Bug 9938149
5471        --bug 9241240: MMTT with no MTLT
5472        --             Fix for incorrect ATT/ATR shown in WIP comp issue (MMTT inserted without lot number)
5473        UNION ALL
5474          --MMTT records, no lot in MTLT or MMTT
5475          SELECT
5476               mmtt.organization_id    organization_id
5477             , mmtt.inventory_item_id  inventory_item_id
5478             , mmtt.revision           revision
5479             , mmtt.lot_number         lot_number
5480             , mmtt.subinventory_code  subinventory_code
5481             , mmtt.locator_id         locator_id
5482             , -1 * round(Abs(decode (l_demand_source_line_id,
5483                                          NULL, mmtt.primary_quantity,
5484                                          Nvl(pjm_ueff_onhand.txn_quantity
5485                                               (l_demand_source_line_id
5486                                               ,mmtt.transaction_temp_id
5487                                               ,mmtt.lot_number
5488                                               ,'Y'
5489                                               ,mmtt.inventory_item_id
5490                                               ,mmtt.organization_id
5491                                               ,mmtt.transaction_source_type_id
5492                                               ,mmtt.transaction_source_id
5493                                               ,mmtt.rcv_transaction_id
5494                                               ,sign(mmtt.primary_quantity)
5495                                               )
5496                                             ,mmtt.primary_quantity)
5497                                     )
5498                             ),5
5499                         )
5500             , -1 * round(Abs(decode (l_demand_source_line_id,
5501                                          NULL, mmtt.secondary_transaction_quantity,
5502                                          Nvl(pjm_ueff_onhand.txn_quantity
5503                                               (l_demand_source_line_id
5504                                               ,mmtt.transaction_temp_id
5505                                               ,mmtt.lot_number
5506                                               ,'Y'
5507                                               ,mmtt.inventory_item_id
5508                                               ,mmtt.organization_id
5509                                               ,mmtt.transaction_source_type_id
5510                                               ,mmtt.transaction_source_id
5511                                               ,mmtt.rcv_transaction_id
5512                                               ,sign(mmtt.secondary_transaction_quantity)
5513                                               )
5514                                            ,mmtt.secondary_transaction_quantity)
5515                                     )
5516                             ),5
5517                         )                                             -- invConv change
5518             , Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,1,5), 1)  quantity_type --FlexiLotAlloc
5519             , mmtt.cost_group_id        cost_group_id
5520            --14688297
5521            -- , NVL(mmtt.allocated_lpn_id, NVL(mmtt.content_lpn_id, mmtt.lpn_id)) lpn_id
5522          , decode(nvl(mmtt.wms_task_status,-1), 4, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID), NVL(MMTT.ALLOCATED_LPN_ID, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID))) lpn_id
5523             , to_number(NULL)           transaction_action_id --5698945
5524             , to_char(NULL)             transfer_subinventory_code --5698945
5525             , to_number(NULL)           transfer_locator_id --5698945
5526          FROM   mtl_material_transactions_temp mmtt
5527          WHERE  mmtt.posting_flag = 'Y'
5528            AND  mmtt.lot_number IS NULL
5529            AND  mmtt.subinventory_code IS NOT NULL
5530            AND  (mmtt.transaction_status IS NULL OR
5531                  Nvl(mmtt.transaction_status,0) = 2 AND
5532                  mmtt.transaction_action_id IN (1,2,27,28)) --FlexiLotAlloc
5533            AND  mmtt.transaction_action_id in (1,2,27,28) --FlexiLotAlloc and BUG 10070839
5534            AND  mmtt.transaction_source_type_id in (2,4,5,8) --FlexiLotAlloc
5535            AND  nvl(mmtt.wip_entity_type,-1) NOT IN (9,10) --FlexiLotAlloc
5536            AND  NOT EXISTS (SELECT 1 FROM mtl_transaction_lots_temp mtlt
5537                             WHERE mmtt.transaction_temp_id = mtlt.transaction_temp_id)
5538        UNION ALL
5539        -- receiving side of transfers lot in MMTT
5540        SELECT
5541             Decode(mmtt.transaction_action_id
5542                , 3, mmtt.transfer_organization
5543                , mmtt.organization_id)   organization_id
5544           , mmtt.inventory_item_id       inventory_item_id
5545           , mmtt.revision                revision
5546           , mmtt.lot_number              lot_number
5547           , mmtt.transfer_subinventory   subinventory_code
5548           , mmtt.transfer_to_location    locator_id
5549       , round(Abs(decode(l_demand_source_line_id,
5550             NULL, mmtt.primary_quantity,
5551             Nvl(pjm_ueff_onhand.txn_quantity(
5552                      l_demand_source_line_id
5553                 ,mmtt.transaction_temp_id
5554                 ,mmtt.lot_number
5555                     ,'Y'
5556                 ,mmtt.inventory_item_id
5557                 ,mmtt.organization_id
5558                 ,mmtt.transaction_source_type_id
5559                 ,mmtt.transaction_source_id
5560                 ,mmtt.rcv_transaction_id
5561                     ,sign(mmtt.primary_quantity)
5562                    ),mmtt.primary_quantity)
5563           )),5)
5564       , round(Abs(decode(l_demand_source_line_id,
5565             NULL, mmtt.secondary_transaction_quantity,
5566             Nvl(pjm_ueff_onhand.txn_quantity(
5567                      l_demand_source_line_id
5568                 ,mmtt.transaction_temp_id
5569                 ,mmtt.lot_number
5570                     ,'Y'
5571                 ,mmtt.inventory_item_id
5572                 ,mmtt.organization_id
5573                 ,mmtt.transaction_source_type_id
5574                 ,mmtt.transaction_source_id
5575                 ,mmtt.rcv_transaction_id
5576                     ,sign(mmtt.secondary_transaction_quantity)
5577                    ),mmtt.secondary_transaction_quantity)
5578           )),5)
5579           , 1                            quantity_type
5580           , mmtt.transfer_cost_group_id  cost_group_id
5581           , NVL(mmtt.content_lpn_id,mmtt.transfer_lpn_id)  lpn_id
5582           , to_number(NULL)                      transaction_action_id
5583           , to_char(NULL)                        transfer_subinventory_code
5584           , to_number(NULL)                      transfer_locator_id
5585        FROM
5586             mtl_material_transactions_temp mmtt
5587        WHERE
5588              mmtt.posting_flag = 'Y'
5589      AND mmtt.lot_number IS NOT NULL
5590      AND Decode( Nvl(mmtt.transaction_status,0),
5591                        2, decode(nvl(mmtt.wms_task_status,-1), 4, 1, 2),
5592                        1 ) <> 2
5593            AND (mmtt.transaction_action_id IN (2,3) -- Bug 9595634: Removed 28
5594                -- Bug 9595634: Added condition to match demand source if action = 28
5595                 OR (mmtt.transaction_action_id = 28
5596                     AND l_demand_source_header_id = mmtt.transaction_source_id
5597                     AND Nvl(l_demand_source_line_id, -9999) = Nvl(mmtt.TRX_SOURCE_LINE_ID,-9999)
5598                     AND Nvl(l_demand_source_name, '@@@###@@#') = Nvl(mmtt.TRANSACTION_SOURCE_NAME,'@@@###@@#')
5599                     AND Nvl(l_demand_source_delivery,-9999) = Nvl(mmtt.TRX_SOURCE_DELIVERY_ID,-9999)
5600                    )
5601                )
5602 
5603  UNION ALL
5604     -- receiving side of transfers  lot in MTLT
5605        SELECT
5606             Decode(mmtt.transaction_action_id
5607                , 3, mmtt.transfer_organization
5608                , mmtt.organization_id)   organization_id
5609           , mmtt.inventory_item_id       inventory_item_id
5610           , mmtt.revision                revision
5611           , mtlt.lot_number              lot_number
5612           , mmtt.transfer_subinventory   subinventory_code
5613           , mmtt.transfer_to_location    locator_id
5614       , round(Abs(decode(l_demand_source_line_id,
5615             NULL, mtlt.primary_quantity,
5616             Nvl(pjm_ueff_onhand.txn_quantity(
5617                      l_demand_source_line_id
5618                 ,mmtt.transaction_temp_id
5619                 ,mtlt.lot_number
5620                     ,'Y'
5621                 ,mmtt.inventory_item_id
5622                 ,mmtt.organization_id
5623                 ,mmtt.transaction_source_type_id
5624                 ,mmtt.transaction_source_id
5625                 ,mmtt.rcv_transaction_id
5626                     ,sign(mmtt.primary_quantity)
5627                    ),mtlt.primary_quantity)
5628         )),5)
5629       , round(Abs(decode(l_demand_source_line_id,
5630             NULL, mtlt.secondary_quantity,
5631             Nvl(pjm_ueff_onhand.txn_quantity(
5632                      l_demand_source_line_id
5633                 ,mmtt.transaction_temp_id
5634                 ,mtlt.lot_number
5635                     ,'Y'
5636                 ,mmtt.inventory_item_id
5637                 ,mmtt.organization_id
5638                 ,mmtt.transaction_source_type_id
5639                 ,mmtt.transaction_source_id
5640                 ,mmtt.rcv_transaction_id
5641                     ,sign(mmtt.secondary_transaction_quantity)
5642                    ),mtlt.secondary_quantity)
5643         )),5)
5644           , 1                            quantity_type
5645           , mmtt.transfer_cost_group_id  cost_group_id
5646           , NVL(mmtt.content_lpn_id,mmtt.transfer_lpn_id) lpn_id
5647           , to_number(NULL)                      transaction_action_id
5648           , to_char(NULL)                        transfer_subinventory_code
5649           , to_number(NULL)                      transfer_locator_id
5650        FROM
5651             mtl_material_transactions_temp mmtt
5652        ,mtl_transaction_lots_temp mtlt
5653        WHERE
5654              mmtt.posting_flag = 'Y'
5655           AND mmtt.lot_number IS NULL
5656           AND mmtt.transaction_temp_id = mtlt.transaction_temp_id
5657       AND Decode( Nvl(mmtt.transaction_status,0),
5658                        2, decode(nvl(mmtt.wms_task_status,-1), 4, 1, 2),
5659                        1 ) <> 2
5660            AND (mmtt.transaction_action_id IN (2,3) -- Bug 9595634: Removed 28
5661                -- Bug 9595634: Added condition to match demand source if action = 28
5662                 OR (mmtt.transaction_action_id = 28
5663                     AND l_demand_source_header_id = mmtt.transaction_source_id
5664                     AND Nvl(l_demand_source_line_id, -9999) = Nvl(mmtt.TRX_SOURCE_LINE_ID,-9999)
5665                     AND Nvl(l_demand_source_name, '@@@###@@#') = Nvl(mmtt.TRANSACTION_SOURCE_NAME,'@@@###@@#')
5666                     AND Nvl(l_demand_source_delivery,-9999) = Nvl(mmtt.TRX_SOURCE_DELIVERY_ID,-9999)
5667                    )
5668                )
5669 
5670       ) x
5671       WHERE x.organization_id    = l_organization_id
5672         AND x.inventory_item_id  = l_inventory_item_id
5673       GROUP BY
5674            x.organization_id, x.inventory_item_id, x.revision
5675           , x.lot_number,x.subinventory_code, x.locator_id
5676           , x.quantity_type, x.cost_group_id, x.lpn_id
5677           , x.transaction_action_id, x.transfer_subinventory_code
5678           , x.transfer_locator_id
5679    ) x
5680     , mtl_secondary_inventories sub
5681     , mtl_lot_numbers lot
5682  WHERE
5683    x.organization_id    = sub.organization_id          (+)
5684    AND x.subinventory_code = sub.secondary_inventory_name (+)
5685    AND x.organization_id   = lot.organization_id   (+)
5686    AND x.inventory_item_id = lot.inventory_item_id (+)
5687    AND x.lot_number        = lot.lot_number        (+)
5688    AND (l_asset_subs_only = 2 OR
5689          NVL(sub.asset_inventory,1) = 1)
5690    AND ((l_onhand_source = 1 AND
5691         Nvl(sub.inventory_atp_code, 1) = 1
5692      ) OR
5693         (l_onhand_source = 2 AND
5694          Nvl(sub.availability_type, 1) = 1
5695      ) OR
5696      l_onhand_source =3
5697        OR
5698      (l_onhand_source = 4 AND
5699         Nvl(sub.inventory_atp_code, 1) = 1 AND
5700                 Nvl(sub.availability_type, 1) = 1
5701      )
5702       )
5703     ;
5704       --Lot Controlled and Not Unit Effective
5705       -- invConv change : grade_code filter is in cursor c_lot_grade.
5706       -- invConv ... no chage here, go and see c_lot_grade
5707       -- invConv ... no MMS specific change here ... see c_lot_MMS and c_lot_grade_MMS
5708 
5709       CURSOR c_lot IS
5710       SELECT
5711           x.organization_id       organization_id
5712         , x.inventory_item_id     inventory_item_id
5713         , x.revision              revision
5714         , x.lot_number       lot_number
5715         , lot.expiration_date     lot_expiration_date
5716         , x.subinventory_code     subinventory_code
5717         , sub.reservable_type     reservable_type
5718         , x.locator_id            locator_id
5719         , x.primary_quantity      primary_quantity
5720         , x.secondary_quantity    secondary_quantity
5721         , x.quantity_type         quantity_type
5722         , x.cost_group_id         cost_group_id
5723         , x.lpn_id        lpn_id
5724         , x.transaction_action_id       transaction_action_id
5725         , x.transfer_subinventory_code  transfer_subinventory_code
5726         , x.transfer_locator_id         transfer_locator_id
5727         , lot.reservable_type     lot_reservable_type       --Bug#8713821 to check reservable type
5728      FROM (
5729        SELECT
5730            x.organization_id       organization_id
5731          , x.inventory_item_id     inventory_item_id
5732          , decode(l_revision_control, 2, NULL, x.revision) revision
5733          , x.lot_number            lot_number
5734          , x.subinventory_code     subinventory_code
5735          , x.locator_id            locator_id
5736          , SUM(x.primary_quantity) primary_quantity
5737          , SUM(x.secondary_quantity) secondary_quantity
5738          , x.quantity_type         quantity_type
5739          , x.cost_group_id         cost_group_id
5740          , x.lpn_id        lpn_id
5741          , x.transaction_action_id       transaction_action_id
5742          , x.transfer_subinventory_code  transfer_subinventory_code
5743          , x.transfer_locator_id         transfer_locator_id
5744         FROM (
5745        -- reservations
5746        SELECT
5747           mr.organization_id       organization_id
5748         , mr.inventory_item_id     inventory_item_id
5749         , mr.revision              revision
5750         , mr.lot_number            lot_number
5751         , mr.subinventory_code     subinventory_code
5752         , mr.locator_id            locator_id
5753         , mr.primary_reservation_quantity
5754            - Nvl(mr.detailed_quantity,0)    primary_quantity
5755         , mr.secondary_reservation_quantity
5756            - Nvl(mr.secondary_detailed_quantity,0)    secondary_quantity
5757         , 3                        quantity_type
5758         , to_number(NULL)     cost_group_id
5759         , lpn_id        lpn_id
5760         , to_number(NULL)                      transaction_action_id
5761         , to_char(NULL)                        transfer_subinventory_code
5762         , to_number(NULL)                      transfer_locator_id
5763      FROM mtl_reservations mr
5764      WHERE
5765           Nvl(mr.supply_source_type_id, 13) = 13
5766       AND mr.primary_reservation_quantity >
5767        Nvl(mr.detailed_quantity,0)
5768       AND ((l_no_lpn_reservations <>1)
5769            OR (l_no_lpn_reservations = 1 AND mr.lpn_id IS NULL))
5770       AND (l_tree_mode <> 3 OR
5771           (l_tree_mode = 3
5772       AND NOT
5773              ( l_demand_source_type_id = mr.demand_source_type_id
5774             AND l_demand_source_header_id =  mr.demand_source_header_id
5775             AND Nvl(l_demand_source_line_id, -9999) =
5776       Nvl(mr.demand_source_line_id,-9999)
5777           AND Nvl(l_demand_source_name, '@@@###@@#') =
5778       Nvl(mr.demand_source_name,'@@@###@@#')
5779        AND Nvl(l_demand_source_delivery,-9999) =
5780       Nvl(mr.demand_source_delivery,-9999)
5781               )
5782    ))
5783      UNION ALL
5784        -- onhand quantities
5785        SELECT
5786           moq.organization_id               organization_id
5787         , moq.inventory_item_id             inventory_item_id
5788         , moq.revision                      revision
5789         , moq.lot_number                    lot_number
5790         , moq.subinventory_code             subinventory_code
5791         , moq.locator_id                    locator_id
5792         , moq.primary_transaction_quantity
5793         , moq.secondary_transaction_quantity
5794         , 1                                 quantity_type
5795         , moq.cost_group_id                 cost_group_id
5796         , moq.lpn_id                        lpn_id
5797         , to_number(NULL)                   transaction_action_id
5798         , to_char(NULL)                     transfer_subinventory_code
5799         , to_number(NULL)                   transfer_locator_id
5800        FROM
5801           mtl_onhand_quantities_detail       moq
5802      UNION ALL
5803        -- pending transactions in mmtt, lot in MMTT
5804        SELECT
5805             mmtt.organization_id    organization_id
5806           , mmtt.inventory_item_id  inventory_item_id
5807           , mmtt.revision           revision
5808           , mmtt.lot_number         lot_number
5809           , mmtt.subinventory_code  subinventory_code
5810           , mmtt.locator_id         locator_id
5811      --Bug 4185621
5812           --, Decode(mmtt.transaction_status, 2, 1,
5813           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
5814       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
5815          Sign(mmtt.primary_quantity)))
5816          * round(Abs(mmtt.primary_quantity),5)
5817      --Bug 4185621
5818           --, Decode(mmtt.transaction_status, 2, 1,
5819           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
5820       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
5821          Sign(mmtt.secondary_transaction_quantity)))
5822          * round(Abs(mmtt.secondary_transaction_quantity),5)
5823      --Bug 4185621
5824           --, Decode(mmtt.transaction_status, 2, 5, 1) quantity_type
5825           , Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,1,5), 1)  quantity_type
5826           , mmtt.cost_group_id       cost_group_id
5827       --14688297
5828     -- ,NVL(mmtt.allocated_lpn_id,
5829      -- NVL(mmtt.content_lpn_id, mmtt.lpn_id)) lpn_id
5830 , decode(nvl(mmtt.wms_task_status,-1), 4, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID), NVL(MMTT.ALLOCATED_LPN_ID, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID))) lpn_id
5831           /* Bug 9938149, Decode(mmtt.transaction_status, 2 , mmtt.transaction_action_id,
5832            * to_number(NULL)) transaction_action_id */
5833           , Decode(mmtt.transaction_status, 2 , decode (mp.process_enabled_flag, 'Y',
5834                                                           decode ((select move_order_type
5835                                                                    from mtl_txn_request_headers mtrh
5836                                                                    where header_id = mmtt.move_order_header_id
5837                                                                    and rownum = 1),5,28,mmtt.transaction_action_id),
5838                                                         mmtt.transaction_action_id), to_number(NULL)) transaction_action_id
5839           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_subinventory, to_char(NULL)) transfer_subinventory_code
5840           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_to_location, to_number(NULL))  transfer_locator_id
5841        FROM
5842             mtl_material_transactions_temp mmtt,
5843             mtl_parameters mp -- Bug 9938149
5844        WHERE
5845             mmtt.posting_flag = 'Y'
5846     AND mmtt.lot_number IS NOT NULL
5847     AND mmtt.subinventory_code IS NOT NULL
5848     AND (Nvl(mmtt.transaction_status,0) <> 2 OR
5849             Nvl(mmtt.transaction_status,0) = 2 AND
5850             mmtt.transaction_action_id IN (1,2,28,3,21,29,32,34)
5851         )
5852       -- dont look at scrap and costing txns
5853       -- Bug 3396558 fix. Ignore ownership xfr,planning xfr transactions
5854          AND mmtt.transaction_action_id NOT IN (5,6,24,30)
5855          AND mmtt.organization_id = mp.organization_id -- Bug 9938149
5856     UNION ALL
5857        --MMTT records, lot in MTLT
5858        SELECT
5859             mmtt.organization_id    organization_id
5860           , mmtt.inventory_item_id  inventory_item_id
5861           , mmtt.revision           revision
5862           , mtlt.lot_number         lot_number
5863           , mmtt.subinventory_code  subinventory_code
5864           , mmtt.locator_id         locator_id
5865      --Bug 4185621
5866           --, Decode(mmtt.transaction_status, 2, 1,
5867           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
5868       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
5869       Sign(mmtt.primary_quantity)))
5870        * round(Abs( mtlt.primary_quantity ),5)
5871      --Bug 4185621
5872      --, Decode(mmtt.transaction_status, 2, 1,
5873           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
5874       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
5875       Sign(mmtt.secondary_transaction_quantity)))
5876        * round(Abs( mtlt.secondary_quantity ),5)
5877      --Bug 4185621
5878           --, Decode(mmtt.transaction_status, 2, 5, 1) quantity_type
5879           , Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,1,5), 1)  quantity_type
5880           , mmtt.cost_group_id       cost_group_id
5881       --14688297
5882     -- ,NVL(mmtt.allocated_lpn_id,
5883     --  NVL(mmtt.content_lpn_id, mmtt.lpn_id)) lpn_id
5884 , decode(nvl(mmtt.wms_task_status,-1), 4, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID), NVL(MMTT.ALLOCATED_LPN_ID, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID))) lpn_id
5885           /* Bug 9938149, Decode(mmtt.transaction_status, 2 , mmtt.transaction_action_id,
5886            * to_number(NULL)) transaction_action_id */
5887           , Decode(mmtt.transaction_status, 2 , decode (mp.process_enabled_flag, 'Y',
5888                                                           decode ((select move_order_type
5889                                                                    from mtl_txn_request_headers mtrh
5890                                                                    where header_id = mmtt.move_order_header_id
5891                                                                    and rownum = 1),5,28,mmtt.transaction_action_id),
5892                                                         mmtt.transaction_action_id), to_number(NULL)) transaction_action_id
5893           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_subinventory, to_char(NULL)) transfer_subinventory_code
5894           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_to_location, to_number(NULL))  transfer_locator_id
5895        FROM
5896            mtl_material_transactions_temp mmtt,
5897       mtl_transaction_lots_temp mtlt,
5898       mtl_parameters mp -- Bug 9938149
5899        WHERE
5900              mmtt.posting_flag = 'Y'
5901          AND mmtt.transaction_temp_id = mtlt.transaction_temp_id
5902          AND mmtt.lot_number IS NULL
5903     AND mmtt.subinventory_code IS NOT NULL
5904     AND (Nvl(mmtt.transaction_status,0) <> 2 OR
5905          Nvl(mmtt.transaction_status,0) = 2 AND
5906         mmtt.transaction_action_id IN (1,2,28,3,21,29,32,34)
5907       )
5908       -- dont look at scrap and costing txns
5909       -- Bug 3396558 fix. Ignore ownership xfr,planning xfr transactions
5910          AND mmtt.transaction_action_id NOT IN (5,6,24,30)
5911          AND mmtt.organization_id = mp.organization_id -- Bug 9938149
5912        UNION ALL
5913        -- receiving side of transfers, lot in MMTT
5914        -- Bug 7658493, If wms task is in loaded status, consider allocation like pending transaction.
5915        SELECT
5916             Decode(mmtt.transaction_action_id
5917                , 3, mmtt.transfer_organization
5918                , mmtt.organization_id)   organization_id
5919           , mmtt.inventory_item_id       inventory_item_id
5920           , mmtt.revision                revision
5921           , mmtt.lot_number              lot_number
5922           , mmtt.transfer_subinventory   subinventory_code
5923           , mmtt.transfer_to_location    locator_id
5924           , round(Abs( mmtt.primary_quantity),5)
5925           , round(Abs( mmtt.secondary_transaction_quantity),5)
5926           , 1                            quantity_type
5927           , mmtt.transfer_cost_group_id  cost_group_id
5928           , NVL(mmtt.content_lpn_id,mmtt.transfer_lpn_id) lpn_id
5929           , to_number(NULL)                      transaction_action_id
5930           , to_char(NULL)                        transfer_subinventory_code
5931           , to_number(NULL)                      transfer_locator_id
5932        FROM
5933             mtl_material_transactions_temp mmtt
5934        WHERE
5935              mmtt.posting_flag = 'Y'
5936      AND mmtt.lot_number IS NOT NULL
5937      AND Decode( Nvl(mmtt.transaction_status,0),
5938                        2, decode(nvl(mmtt.wms_task_status,-1), 4, 1, 2),
5939                        1 ) <> 2
5940                  AND (mmtt.transaction_action_id IN (2,3) -- Bug 9595634: Removed 28
5941                -- Bug 9595634: Added condition to match demand source if action = 28
5942                 OR (mmtt.transaction_action_id = 28
5943                     AND l_demand_source_header_id = mmtt.transaction_source_id
5944                     AND Nvl(l_demand_source_line_id, -9999) = Nvl(mmtt.TRX_SOURCE_LINE_ID,-9999)
5945                     AND Nvl(l_demand_source_name, '@@@###@@#') = Nvl(mmtt.TRANSACTION_SOURCE_NAME,'@@@###@@#')
5946                     AND Nvl(l_demand_source_delivery,-9999) = Nvl(mmtt.TRX_SOURCE_DELIVERY_ID,-9999)
5947                    )
5948                )
5949 
5950   UNION ALL
5951        -- receiving side of transfers, lot in MTLT
5952        -- Bug 7658493, If wms task is in loaded status, consider allocation like pending transaction.
5953        SELECT
5954             Decode(mmtt.transaction_action_id
5955                , 3, mmtt.transfer_organization
5956                , mmtt.organization_id)   organization_id
5957           , mmtt.inventory_item_id       inventory_item_id
5958           , mmtt.revision                revision
5959           , mtlt.lot_number              lot_number
5960           , mmtt.transfer_subinventory   subinventory_code
5961           , mmtt.transfer_to_location    locator_id
5962           , round(Abs( mtlt.primary_quantity ),5)
5963           , round(Abs( mtlt.secondary_quantity ),5)
5964           , 1                            quantity_type
5965           , mmtt.transfer_cost_group_id  cost_group_id
5966           , NVL(mmtt.content_lpn_id,mmtt.transfer_lpn_id) lpn_id
5967           , to_number(NULL)                      transaction_action_id
5968           , to_char(NULL)                        transfer_subinventory_code
5969           , to_number(NULL)                      transfer_locator_id
5970        FROM
5971             mtl_material_transactions_temp mmtt,
5972        mtl_transaction_lots_temp mtlt
5973        WHERE
5974              mmtt.posting_flag = 'Y'
5975           AND mmtt.lot_number IS NULL
5976           AND mmtt.transaction_temp_id = mtlt.transaction_temp_id
5977           AND Decode( Nvl(mmtt.transaction_status,0),
5978                        2, decode(nvl(mmtt.wms_task_status,-1), 4, 1, 2),
5979                        1 ) <> 2
5980                      AND (mmtt.transaction_action_id IN (2,3) -- Bug 9595634: Removed 28
5981                -- Bug 9595634: Added condition to match demand source if action = 28
5982                 OR (mmtt.transaction_action_id = 28
5983                     AND l_demand_source_header_id = mmtt.transaction_source_id
5984                     AND Nvl(l_demand_source_line_id, -9999) = Nvl(mmtt.TRX_SOURCE_LINE_ID,-9999)
5985                     AND Nvl(l_demand_source_name, '@@@###@@#') = Nvl(mmtt.TRANSACTION_SOURCE_NAME,'@@@###@@#')
5986                     AND Nvl(l_demand_source_delivery,-9999) = Nvl(mmtt.TRX_SOURCE_DELIVERY_ID,-9999)
5987                    )
5988                )
5989       ) x
5990       WHERE x.organization_id    = l_organization_id
5991         AND x.inventory_item_id  = l_inventory_item_id
5992       GROUP BY
5993            x.organization_id, x.inventory_item_id, x.revision
5994           , x.lot_number,x.subinventory_code, x.locator_id
5995           , x.quantity_type, x.cost_group_id, x.lpn_id
5996           , x.transaction_action_id, x.transfer_subinventory_code
5997           , x.transfer_locator_id
5998    ) x
5999     , mtl_secondary_inventories sub
6000     , mtl_lot_numbers lot
6001  WHERE
6002    x.organization_id    = sub.organization_id          (+)
6003    AND x.subinventory_code = sub.secondary_inventory_name (+)
6004    AND x.organization_id   = lot.organization_id   (+)
6005    AND x.inventory_item_id = lot.inventory_item_id (+)
6006    AND x.lot_number        = lot.lot_number        (+)
6007    AND (l_asset_subs_only = 2 OR
6008          NVL(sub.asset_inventory,1) = 1)
6009    AND ((l_onhand_source = 1 AND
6010     Nvl(sub.inventory_atp_code, 1) = 1
6011       ) OR
6012         (l_onhand_source = 2 AND
6013        Nvl(sub.availability_type, 1) = 1
6014    ) OR
6015    l_onhand_source =3
6016    OR
6017    (l_onhand_source = 4 AND
6018     Nvl(sub.inventory_atp_code, 1) = 1 AND
6019     Nvl(sub.availability_type, 1) = 1)
6020       )
6021    ;
6022 
6023      CURSOR c_lot_MMS IS
6024       SELECT
6025           x.organization_id       organization_id
6026         , x.inventory_item_id     inventory_item_id
6027         , x.revision              revision
6028         , x.lot_number       lot_number
6029         , lot.expiration_date     lot_expiration_date
6030         , x.subinventory_code     subinventory_code
6031         , sub.reservable_type     reservable_type
6032         , x.locator_id            locator_id
6033         , x.primary_quantity      primary_quantity
6034         , x.secondary_quantity    secondary_quantity
6035         , x.quantity_type         quantity_type
6036         , x.cost_group_id         cost_group_id
6037         , x.lpn_id        lpn_id
6038         , x.transaction_action_id       transaction_action_id
6039         , x.transfer_subinventory_code  transfer_subinventory_code
6040         , x.transfer_locator_id         transfer_locator_id
6041         , x.status_id -- Onhand Material Status Support
6042         --, lot.reservable_type     lot_reservable_type       --Bug#8713821 to check reservable type
6043         , decode(mp.default_status_id, null, lot.reservable_type, mms.reservable_type) lot_reservable_type --Bug 13387319
6044      FROM (
6045        SELECT
6046            x.organization_id       organization_id
6047          , x.inventory_item_id     inventory_item_id
6048          , decode(l_revision_control, 2, NULL, x.revision) revision
6049          , x.lot_number            lot_number
6050          , x.subinventory_code     subinventory_code
6051          , x.locator_id            locator_id
6052          , SUM(x.primary_quantity) primary_quantity
6053          , SUM(x.secondary_quantity) secondary_quantity
6054          , x.quantity_type         quantity_type
6055          , x.cost_group_id         cost_group_id
6056          , x.lpn_id        lpn_id
6057          , x.transaction_action_id       transaction_action_id
6058          , x.transfer_subinventory_code  transfer_subinventory_code
6059          , x.transfer_locator_id         transfer_locator_id
6060          , x.status_id -- Onhand Material Status Support
6061         FROM (
6062        -- reservations
6063        SELECT
6064           mr.organization_id       organization_id
6065         , mr.inventory_item_id     inventory_item_id
6066         , mr.revision              revision
6067         , mr.lot_number            lot_number
6068         , mr.subinventory_code     subinventory_code
6069         , mr.locator_id            locator_id
6070         , mr.primary_reservation_quantity
6071            - Nvl(mr.detailed_quantity,0)    primary_quantity
6072         , mr.secondary_reservation_quantity
6073            - Nvl(mr.secondary_detailed_quantity,0)    secondary_quantity
6074         , 3                        quantity_type
6075         , to_number(NULL)     cost_group_id
6076         , lpn_id        lpn_id
6077         , to_number(NULL)                      transaction_action_id
6078         , to_char(NULL)                        transfer_subinventory_code
6079         , to_number(NULL)                      transfer_locator_id
6080         , to_number(NULL)                      status_id -- Onhand Material Status Support
6081      FROM mtl_reservations mr
6082      WHERE
6083           Nvl(mr.supply_source_type_id, 13) = 13
6084       AND mr.primary_reservation_quantity >
6085        Nvl(mr.detailed_quantity,0)
6086       AND ((l_no_lpn_reservations <>1)
6087            OR (l_no_lpn_reservations = 1 AND mr.lpn_id IS NULL))
6088       AND (l_tree_mode <> 3 OR
6089           (l_tree_mode = 3
6090       AND NOT
6091              ( l_demand_source_type_id = mr.demand_source_type_id
6092             AND l_demand_source_header_id =  mr.demand_source_header_id
6093             AND Nvl(l_demand_source_line_id, -9999) =
6094       Nvl(mr.demand_source_line_id,-9999)
6095           AND Nvl(l_demand_source_name, '@@@###@@#') =
6096       Nvl(mr.demand_source_name,'@@@###@@#')
6097        AND Nvl(l_demand_source_delivery,-9999) =
6098       Nvl(mr.demand_source_delivery,-9999)
6099               )
6100    ))
6101      UNION ALL
6102        -- onhand quantities
6103        SELECT
6104           moq.organization_id               organization_id
6105         , moq.inventory_item_id             inventory_item_id
6106         , moq.revision                      revision
6107         , moq.lot_number                    lot_number
6108         , moq.subinventory_code             subinventory_code
6109         , moq.locator_id                    locator_id
6110         , moq.primary_transaction_quantity
6111         , moq.secondary_transaction_quantity
6112         , 1                                 quantity_type
6113         , moq.cost_group_id                 cost_group_id
6114         , moq.lpn_id                        lpn_id
6115         , to_number(NULL)                   transaction_action_id
6116         , to_char(NULL)                     transfer_subinventory_code
6117         , to_number(NULL)                   transfer_locator_id
6118         , moq.status_id                     -- Onhand Material Status Support
6119        FROM
6120           mtl_onhand_quantities_detail       moq
6121      UNION ALL
6122        -- pending transactions in mmtt, lot in MMTT
6123        SELECT
6124             mmtt.organization_id    organization_id
6125           , mmtt.inventory_item_id  inventory_item_id
6126           , mmtt.revision           revision
6127           , mmtt.lot_number         lot_number
6128           , mmtt.subinventory_code  subinventory_code
6129           , mmtt.locator_id         locator_id
6130      --Bug 4185621
6131           --, Decode(mmtt.transaction_status, 2, 1,
6132           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
6133       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
6134          Sign(mmtt.primary_quantity)))
6135          * round(Abs(mmtt.primary_quantity),5)
6136       --Bug 4185621
6137            --, Decode(mmtt.transaction_status, 2, 1,
6138            , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
6139       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
6140          Sign(mmtt.secondary_transaction_quantity)))
6141          * round(Abs(mmtt.secondary_transaction_quantity),5)
6142      --Bug 4185621
6143           --, Decode(mmtt.transaction_status, 2, 5, 1) quantity_type
6144           , Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,1,5), 1)  quantity_type
6145           , mmtt.cost_group_id       cost_group_id
6146       --14688297
6147     -- ,NVL(mmtt.allocated_lpn_id,
6148     --  NVL(mmtt.content_lpn_id, mmtt.lpn_id)) lpn_id
6149 , decode(nvl(mmtt.wms_task_status,-1), 4, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID), NVL(MMTT.ALLOCATED_LPN_ID, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID))) lpn_id
6150           /* Bug 9938149, Decode(mmtt.transaction_status, 2 , mmtt.transaction_action_id,
6151            * to_number(NULL)) transaction_action_id */
6152           , Decode(mmtt.transaction_status, 2 , decode (mp.process_enabled_flag, 'Y',
6153                                                           decode ((select move_order_type
6154                                                                    from mtl_txn_request_headers mtrh
6155                                                                    where header_id = mmtt.move_order_header_id
6156                                                                    and rownum = 1),5,28,mmtt.transaction_action_id),
6157                                                         mmtt.transaction_action_id), to_number(NULL)) transaction_action_id
6158           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_subinventory, to_char(NULL)) transfer_subinventory_code
6159           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_to_location, to_number(NULL))  transfer_locator_id
6160           , inv_material_status_grp.get_default_status(mmtt.organization_id
6161                                           ,mmtt.inventory_item_id
6162                                           ,mmtt.subinventory_code
6163                                           ,mmtt.locator_id
6164                                           ,mmtt.lot_number -- lot_number
6165                                           ,NVL(mmtt.allocated_lpn_id,
6166                                  NVL(mmtt.content_lpn_id, mmtt.lpn_id))
6167                                           ) status_id -- Onhand Material Status Support
6168        FROM
6169             mtl_material_transactions_temp mmtt
6170           , mtl_parameters mp -- Bug 9938149
6171        WHERE
6172             mmtt.posting_flag = 'Y'
6173 -- invConv bug 4074394   removed the fix in v115.141, because it only works
6174 --       when the ingredient item has no inventory.
6175          AND mmtt.lot_number IS NOT NULL
6176          AND mmtt.subinventory_code IS NOT NULL
6177          AND (Nvl(mmtt.transaction_status,0) <> 2 OR
6178               Nvl(mmtt.transaction_status,0) = 2 AND
6179               mmtt.transaction_action_id IN (1,2,28,3,21,29,32,34)
6180               )
6181       -- dont look at scrap and costing txns
6182       -- Bug 3396558 fix. Ignore ownership xfr,planning xfr transactions
6183          AND mmtt.transaction_action_id NOT IN (5,6,24,30)
6184          AND mmtt.organization_id = mp.organization_id -- Bug 9938149
6185     UNION ALL
6186        --MMTT records, lot in MTLT
6187        SELECT
6188             mmtt.organization_id    organization_id
6189           , mmtt.inventory_item_id  inventory_item_id
6190           , mmtt.revision           revision
6191           , mtlt.lot_number         lot_number
6192           , mmtt.subinventory_code  subinventory_code
6193           , mmtt.locator_id         locator_id
6194      --Bug 4185621
6195           --, Decode(mmtt.transaction_status, 2, 1,
6196           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
6197       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
6198       Sign(mmtt.primary_quantity)))
6199        * round(Abs( mtlt.primary_quantity ),5)
6200      --Bug 4185621
6201           --, Decode(mmtt.transaction_status, 2, 1,
6202           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
6203       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
6204       Sign(mmtt.secondary_transaction_quantity)))
6205        * round(Abs( mtlt.secondary_quantity ),5)
6206      --Bug 4185621
6207           --, Decode(mmtt.transaction_status, 2, 5, 1) quantity_type
6208           , Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,1,5), 1)  quantity_type
6209           , mmtt.cost_group_id       cost_group_id
6210       --14688297
6211        --,NVL(mmtt.allocated_lpn_id,
6212        -- NVL(mmtt.content_lpn_id, mmtt.lpn_id)) lpn_id
6213 , decode(nvl(mmtt.wms_task_status,-1), 4, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID), NVL(MMTT.ALLOCATED_LPN_ID, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID))) lpn_id
6214 
6215           /* Bug 9938149, Decode(mmtt.transaction_status, 2 , mmtt.transaction_action_id,
6216            * to_number(NULL)) transaction_action_id */
6217           , Decode(mmtt.transaction_status, 2 , decode (mp.process_enabled_flag, 'Y',
6218                                                           decode ((select move_order_type
6219                                                                    from mtl_txn_request_headers mtrh
6220                                                                    where header_id = mmtt.move_order_header_id
6221                                                                    and rownum = 1),5,28,mmtt.transaction_action_id),
6222                                                         mmtt.transaction_action_id), to_number(NULL)) transaction_action_id
6223           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_subinventory, to_char(NULL)) transfer_subinventory_code
6224           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_to_location, to_number(NULL))  transfer_locator_id
6225           , inv_material_status_grp.get_default_status(mmtt.organization_id
6226                                           ,mmtt.inventory_item_id
6227                                           ,mmtt.subinventory_code
6228                                           ,mmtt.locator_id
6229                                           ,mtlt.lot_number -- lot_number in MTLT
6230                                           ,NVL(mmtt.allocated_lpn_id,
6231                                  NVL(mmtt.content_lpn_id, mmtt.lpn_id))
6232                                           ) status_id -- Onhand Material Status Support
6233        FROM
6234            mtl_material_transactions_temp mmtt,
6235            mtl_transaction_lots_temp mtlt
6236          , mtl_parameters mp -- Bug 9938149
6237        WHERE
6238              mmtt.posting_flag = 'Y'
6239          AND mmtt.transaction_temp_id = mtlt.transaction_temp_id
6240          AND mmtt.lot_number IS NULL
6241          AND mmtt.subinventory_code IS NOT NULL
6242          AND (Nvl(mmtt.transaction_status,0) <> 2 OR
6243               Nvl(mmtt.transaction_status,0) = 2 AND
6244               mmtt.transaction_action_id IN (1,2,28,3,21,29,32,34)
6245               )
6246           -- dont look at scrap and costing txns
6247           -- Bug 3396558 fix. Ignore ownership xfr,planning xfr transactions
6248          AND mmtt.transaction_action_id NOT IN (5,6,24,30)
6249          AND mmtt.organization_id = mp.organization_id -- Bug 9938149
6250        --bug 9241240: MMTT with no MTLT
6251        --             Fix for incorrect ATT/ATR shown in WIP comp issue (MMTT inserted without lot number)
6252       UNION ALL
6253          -- pending transactions in mmtt, no lot in MMTT and no MTLT
6254        SELECT
6255                mmtt.organization_id    organization_id
6256              , mmtt.inventory_item_id  inventory_item_id
6257              , mmtt.revision           revision
6258              , mmtt.lot_number         lot_number
6259              , mmtt.subinventory_code  subinventory_code
6260              , mmtt.locator_id         locator_id
6261              , round(mmtt.primary_quantity,5)
6262              , round(mmtt.secondary_transaction_quantity,5)             -- invConv change
6263              , Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,1,5), 1) quantity_type --FlexiLotAlloc
6264              , mmtt.cost_group_id      cost_group_id
6265              , NVL(mmtt.allocated_lpn_id, NVL(mmtt.content_lpn_id, mmtt.lpn_id)) lpn_id
6266              , to_number(NULL)         transaction_action_id
6267              , to_char(NULL)           transfer_subinventory_code
6268              , to_number(NULL)         transfer_locator_id
6269              , inv_material_status_grp.get_default_status(mmtt.organization_id
6270                                           ,mmtt.inventory_item_id
6271                                           ,mmtt.subinventory_code
6272                                           ,mmtt.locator_id
6273                                           ,NULL --lot number
6274                                           ,NVL(mmtt.allocated_lpn_id,NVL(mmtt.content_lpn_id, mmtt.lpn_id))
6275                                           ) status_id -- Onhand Material Status Support
6276        FROM
6277             mtl_material_transactions_temp mmtt
6278        WHERE mmtt.posting_flag = 'Y'
6279          AND mmtt.lot_number IS NULL
6280          AND mmtt.subinventory_code IS NOT NULL
6281          AND (mmtt.transaction_status IS NULL OR
6282               Nvl(mmtt.transaction_status,0) = 2 AND
6283               mmtt.transaction_action_id IN (1,2,27,28))--FlexiLotAlloc
6284          AND mmtt.transaction_action_id in (1,2,27,28) --FlexiLotAlloc and BUG 10070839
6285          AND mmtt.transaction_source_type_id in (2,4,5,8)  --FlexiLotAlloc
6286          AND nvl(mmtt.wip_entity_type,-1) NOT IN (9,10) --FlexiLotAlloc
6287          AND NOT EXISTS (SELECT 1 FROM mtl_transaction_lots_temp mtlt
6288                          WHERE transaction_temp_id = mmtt.transaction_temp_id)
6289        UNION ALL
6290        -- receiving side of transfers, lot in MMTT
6291        -- Bug 7658493, If wms task is in loaded status, consider allocation like pending transaction.
6292        SELECT
6293             Decode(mmtt.transaction_action_id
6294                , 3, mmtt.transfer_organization
6295                , mmtt.organization_id)   organization_id
6296           , mmtt.inventory_item_id       inventory_item_id
6297           , mmtt.revision                revision
6298           , mmtt.lot_number              lot_number
6299           , mmtt.transfer_subinventory   subinventory_code
6300           , mmtt.transfer_to_location    locator_id
6301           , round(Abs( mmtt.primary_quantity),5)
6302           , round(Abs( mmtt.secondary_transaction_quantity),5)
6303           , 1                            quantity_type
6304           , mmtt.transfer_cost_group_id  cost_group_id
6305           , NVL(mmtt.content_lpn_id,mmtt.transfer_lpn_id) lpn_id
6306           , to_number(NULL)                      transaction_action_id
6307           , to_char(NULL)                        transfer_subinventory_code
6308           , to_number(NULL)                      transfer_locator_id
6309           , inv_material_status_grp.get_default_status(Decode(mmtt.transaction_action_id
6310                                                , 3, mmtt.transfer_organization
6311                                                , mmtt.organization_id)
6312                                           ,mmtt.inventory_item_id
6313                                           ,mmtt.transfer_subinventory
6314                                           ,mmtt.transfer_to_location
6315                                           ,mmtt.lot_number -- lot_number
6316                                           ,NVL(mmtt.content_lpn_id,mmtt.transfer_lpn_id)
6317                                           ,mmtt.transaction_action_id
6318                                           ,inv_material_status_grp.get_default_status(mmtt.organization_id
6319                                                   ,mmtt.inventory_item_id
6320                                                   ,mmtt.subinventory_code
6321                                                   ,mmtt.locator_id
6322                                                   ,mmtt.lot_number -- lot_number
6323                                                   ,NVL(mmtt.allocated_lpn_id,
6324                                         NVL(mmtt.content_lpn_id, mmtt.lpn_id))
6325                                                   )
6326                                           ) status_id -- Onhand Material Status Support
6327        FROM
6328             mtl_material_transactions_temp mmtt
6329        WHERE
6330              mmtt.posting_flag = 'Y'
6331      AND mmtt.lot_number IS NOT NULL
6332      AND Decode( Nvl(mmtt.transaction_status,0),
6333                        2, decode(nvl(mmtt.wms_task_status,-1), 4, 1, 2),
6334                        1 ) <> 2
6335                 AND (mmtt.transaction_action_id IN (2,3) -- Bug 9595634: Removed 28
6336                -- Bug 9595634: Added condition to match demand source if action = 28
6337                 OR (mmtt.transaction_action_id = 28
6338                     AND l_demand_source_header_id = mmtt.transaction_source_id
6339                     AND Nvl(l_demand_source_line_id, -9999) = Nvl(mmtt.TRX_SOURCE_LINE_ID,-9999)
6340                     AND Nvl(l_demand_source_name, '@@@###@@#') = Nvl(mmtt.TRANSACTION_SOURCE_NAME,'@@@###@@#')
6341                     AND Nvl(l_demand_source_delivery,-9999) = Nvl(mmtt.TRX_SOURCE_DELIVERY_ID,-9999)
6342                    )
6343                )
6344   UNION ALL
6345        -- receiving side of transfers, lot in MTLT
6346        -- Bug 7658493, If wms task is in loaded status, consider allocation like pending transaction.
6347        SELECT
6348             Decode(mmtt.transaction_action_id
6349                , 3, mmtt.transfer_organization
6350                , mmtt.organization_id)   organization_id
6351           , mmtt.inventory_item_id       inventory_item_id
6352           , mmtt.revision                revision
6353           , mtlt.lot_number              lot_number
6354           , mmtt.transfer_subinventory   subinventory_code
6355           , mmtt.transfer_to_location    locator_id
6356           , round(Abs( mtlt.primary_quantity ),5)
6357           , round(Abs( mtlt.secondary_quantity ),5)
6358           , 1                            quantity_type
6359           , mmtt.transfer_cost_group_id  cost_group_id
6360           , NVL(mmtt.content_lpn_id,mmtt.transfer_lpn_id) lpn_id
6361           , to_number(NULL)                      transaction_action_id
6362           , to_char(NULL)                        transfer_subinventory_code
6363           , to_number(NULL)                      transfer_locator_id
6364           , inv_material_status_grp.get_default_status(Decode(mmtt.transaction_action_id
6365                                                , 3, mmtt.transfer_organization
6366                                                , mmtt.organization_id)
6367                                           ,mmtt.inventory_item_id
6368                                           ,mmtt.transfer_subinventory
6369                                           ,mmtt.transfer_to_location
6370                                           ,mtlt.lot_number -- lot_number
6371                                           ,NVL(mmtt.content_lpn_id,mmtt.transfer_lpn_id)
6372                                           ,mmtt.transaction_action_id
6373                                           ,inv_material_status_grp.get_default_status(mmtt.organization_id
6374                                                   ,mmtt.inventory_item_id
6375                                                   ,mmtt.subinventory_code
6376                                                   ,mmtt.locator_id
6377                                                   ,mtlt.lot_number -- lot_number
6378                                                   ,NVL(mmtt.allocated_lpn_id,
6379                                         NVL(mmtt.content_lpn_id, mmtt.lpn_id))
6380                                                   )
6381                                           ) status_id -- Onhand Material Status Support
6382        FROM
6383             mtl_material_transactions_temp mmtt,
6384        mtl_transaction_lots_temp mtlt
6385        WHERE
6386              mmtt.posting_flag = 'Y'
6387           AND mmtt.lot_number IS NULL
6388           AND mmtt.transaction_temp_id = mtlt.transaction_temp_id
6389           AND Decode( Nvl(mmtt.transaction_status,0),
6390                        2, decode(nvl(mmtt.wms_task_status,-1), 4, 1, 2),
6391                        1 ) <> 2
6392                      AND (mmtt.transaction_action_id IN (2,3) -- Bug 9595634: Removed 28
6393                -- Bug 9595634: Added condition to match demand source if action = 28
6394                 OR (mmtt.transaction_action_id = 28
6395                     AND l_demand_source_header_id = mmtt.transaction_source_id
6396                     AND Nvl(l_demand_source_line_id, -9999) = Nvl(mmtt.TRX_SOURCE_LINE_ID,-9999)
6397                     AND Nvl(l_demand_source_name, '@@@###@@#') = Nvl(mmtt.TRANSACTION_SOURCE_NAME,'@@@###@@#')
6398                     AND Nvl(l_demand_source_delivery,-9999) = Nvl(mmtt.TRX_SOURCE_DELIVERY_ID,-9999)
6399                    )
6400                )
6401       ) x
6402       WHERE x.organization_id    = l_organization_id
6403         AND x.inventory_item_id  = l_inventory_item_id
6404       GROUP BY
6405            x.organization_id, x.inventory_item_id, x.revision
6406           , x.lot_number,x.subinventory_code, x.locator_id
6407           , x.quantity_type, x.cost_group_id, x.lpn_id
6408           , x.transaction_action_id, x.transfer_subinventory_code
6409           , x.transfer_locator_id, x.status_id -- Onhand Material Status Support
6410    ) x
6411     , mtl_secondary_inventories sub
6412     , mtl_item_locations loc
6413     , mtl_lot_numbers lot
6414     , mtl_parameters mp -- Onhand Material Status Support
6415     , mtl_material_statuses_b mms -- Onhand Material Status Support
6416  WHERE
6417        x.inventory_item_id = lot.inventory_item_id        (+)
6418    AND x.organization_id = lot.organization_id            (+)
6419    AND x.lot_number = lot.lot_number                      (+)
6420    AND x.organization_id = loc.organization_id            (+)
6421    AND x.locator_id = loc.inventory_location_id           (+)
6422    AND x.organization_id    = sub.organization_id         (+)
6423    AND x.subinventory_code = sub.secondary_inventory_name (+)
6424    AND x.organization_id   = lot.organization_id   (+)
6425    AND x.inventory_item_id = lot.inventory_item_id (+)
6426    AND x.lot_number        = lot.lot_number        (+)
6427    AND x.organization_id    = mp.organization_id   (+) -- Onhand Material Status Support
6428    AND x.status_id = mms.status_id                 (+) -- Onhand Material Status Support
6429    AND (l_asset_subs_only = 2 OR
6430          NVL(sub.asset_inventory,1) = 1)
6431    AND (
6432         (mp.default_status_id is null and
6433         ((l_onhand_source = 1 AND
6434               Nvl(sub.inventory_atp_code, 1) = 1
6435           AND Nvl(loc.inventory_atp_code, 1) = 1
6436           AND Nvl(lot.inventory_atp_code, 1) = 1
6437       ) OR
6438         (l_onhand_source = 2 AND
6439                      Nvl(sub.availability_type, 1) = 1
6440                  AND Nvl(loc.availability_type, 1) = 1
6441                  AND Nvl(lot.availability_type, 1) = 1
6442    ) OR
6443    l_onhand_source =3
6444    OR
6445    (l_onhand_source = 4 AND
6446               Nvl(sub.inventory_atp_code, 1) = 1
6447           AND Nvl(loc.inventory_atp_code, 1) = 1
6448           AND Nvl(lot.inventory_atp_code, 1) = 1
6449           AND Nvl(loc.availability_type, 1) = 1
6450           AND Nvl(lot.availability_type, 1) = 1
6451      AND Nvl(sub.availability_type, 1) = 1
6452         )
6453       )
6454       )
6455       or
6456       (
6457         mp.default_status_id is not null and
6458            ((l_onhand_source =1 AND
6459                Nvl(mms.inventory_atp_code, 1) = 1
6460              )
6461           OR (l_onhand_source = 2 AND
6462          Nvl(mms.availability_type, 1) = 1
6463         )
6464      OR l_onhand_source =3
6465           OR (l_onhand_source = 4 AND
6466          (nvl(mms.inventory_atp_code,1) = 1
6467          AND nvl(mms.availability_type,1)=1
6468               )
6469              )
6470            )
6471        )
6472       )
6473    ;
6474       -- invConv changes end : c_lot with MMS about ATP/Nettable...
6475 
6476       -- invConv change : grade_code filter is here
6477       -- invConv .... no MMS specific change, please see c_lot_grade_MMS
6478 
6479       CURSOR c_lot_grade IS
6480       SELECT
6481           x.organization_id       organization_id
6482         , x.inventory_item_id     inventory_item_id
6483         , x.revision              revision
6484         , x.lot_number       lot_number
6485         , lot.expiration_date     lot_expiration_date
6486         , x.subinventory_code     subinventory_code
6487         , sub.reservable_type     reservable_type
6488         , x.locator_id            locator_id
6489         , x.primary_quantity      primary_quantity
6490         , x.secondary_quantity    secondary_quantity
6491         , x.quantity_type         quantity_type
6492         , x.cost_group_id         cost_group_id
6493         , x.lpn_id        lpn_id
6494         , x.transaction_action_id       transaction_action_id
6495         , x.transfer_subinventory_code  transfer_subinventory_code
6496         , x.transfer_locator_id         transfer_locator_id
6497         , lot.reservable_type     lot_reservable_type       --Bug#8713821 to check reservable type
6498      FROM (
6499        SELECT
6500            x.organization_id       organization_id
6501          , x.inventory_item_id     inventory_item_id
6502          , decode(l_revision_control, 2, NULL, x.revision) revision
6503          , x.lot_number            lot_number
6504          , x.subinventory_code     subinventory_code
6505          , x.locator_id            locator_id
6506          , SUM(x.primary_quantity) primary_quantity
6507          , SUM(x.secondary_quantity) secondary_quantity
6508          , x.quantity_type         quantity_type
6509          , x.cost_group_id         cost_group_id
6510          , x.lpn_id        lpn_id
6511          , x.transaction_action_id       transaction_action_id
6512          , x.transfer_subinventory_code  transfer_subinventory_code
6513          , x.transfer_locator_id         transfer_locator_id
6514         FROM (
6515        -- reservations
6516        SELECT
6517           mr.organization_id       organization_id
6518         , mr.inventory_item_id     inventory_item_id
6519         , mr.revision              revision
6520         , mr.lot_number            lot_number
6521         , mr.subinventory_code     subinventory_code
6522         , mr.locator_id            locator_id
6523         , mr.primary_reservation_quantity
6524            - Nvl(mr.detailed_quantity,0)    primary_quantity
6525         , mr.secondary_reservation_quantity
6526            - Nvl(mr.secondary_detailed_quantity,0)    secondary_quantity
6527         , 3                        quantity_type
6528         , to_number(NULL)     cost_group_id
6529         , lpn_id        lpn_id
6530         , to_number(NULL)                      transaction_action_id
6531         , to_char(NULL)                        transfer_subinventory_code
6532         , to_number(NULL)                      transfer_locator_id
6533      FROM mtl_reservations mr
6534      WHERE
6535           Nvl(mr.supply_source_type_id, 13) = 13
6536       AND mr.primary_reservation_quantity >
6537        Nvl(mr.detailed_quantity,0)
6538       AND ((l_no_lpn_reservations <>1)
6539            OR (l_no_lpn_reservations = 1 AND mr.lpn_id IS NULL))
6540       AND (l_tree_mode <> 3 OR
6541           (l_tree_mode = 3
6542       AND NOT
6543              ( l_demand_source_type_id = mr.demand_source_type_id
6544             AND l_demand_source_header_id =  mr.demand_source_header_id
6545             AND Nvl(l_demand_source_line_id, -9999) =
6546       Nvl(mr.demand_source_line_id,-9999)
6547           AND Nvl(l_demand_source_name, '@@@###@@#') =
6548       Nvl(mr.demand_source_name,'@@@###@@#')
6549        AND Nvl(l_demand_source_delivery,-9999) =
6550       Nvl(mr.demand_source_delivery,-9999)
6551               )
6552    ))
6553      UNION ALL
6554        -- onhand quantities
6555        SELECT
6556           moq.organization_id               organization_id
6557         , moq.inventory_item_id             inventory_item_id
6558         , moq.revision                      revision
6559         , moq.lot_number                    lot_number
6560         , moq.subinventory_code             subinventory_code
6561         , moq.locator_id                    locator_id
6562         , moq.primary_transaction_quantity
6563         , moq.secondary_transaction_quantity
6564         , 1                                 quantity_type
6565         , moq.cost_group_id                 cost_group_id
6566         , moq.lpn_id                        lpn_id
6567         , to_number(NULL)                   transaction_action_id
6568         , to_char(NULL)                     transfer_subinventory_code
6569         , to_number(NULL)                   transfer_locator_id
6570        FROM
6571           mtl_onhand_quantities_detail       moq
6572      UNION ALL
6573        -- pending transactions in mmtt, lot in MMTT
6574        SELECT
6575             mmtt.organization_id    organization_id
6576           , mmtt.inventory_item_id  inventory_item_id
6577           , mmtt.revision           revision
6578           , mmtt.lot_number         lot_number
6579           , mmtt.subinventory_code  subinventory_code
6580           , mmtt.locator_id         locator_id
6581      --Bug 4185621
6582           --, Decode(mmtt.transaction_status, 2, 1,
6583           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
6584       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
6585          Sign(mmtt.primary_quantity)))
6586          * round(Abs(mmtt.primary_quantity),5)
6587       --Bug 4185621
6588            --, Decode(mmtt.transaction_status, 2, 1,
6589            , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
6590       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
6591          Sign(mmtt.secondary_transaction_quantity)))
6592          * round(Abs(mmtt.secondary_transaction_quantity),5)
6593             --Bug 4185621
6594             --, Decode(mmtt.transaction_status, 2, 5, 1) quantity_type
6595             , Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,1,5), 1)  quantity_type
6596           , mmtt.cost_group_id       cost_group_id
6597      --14688297
6598     -- ,NVL(mmtt.allocated_lpn_id,
6599     --  NVL(mmtt.content_lpn_id, mmtt.lpn_id)) lpn_id
6600 , decode(nvl(mmtt.wms_task_status,-1), 4, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID), NVL(MMTT.ALLOCATED_LPN_ID, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID))) lpn_id
6601           /* Bug 9938149, Decode(mmtt.transaction_status, 2 , mmtt.transaction_action_id,
6602            * to_number(NULL)) transaction_action_id */
6603           , Decode(mmtt.transaction_status, 2 , decode (mp.process_enabled_flag, 'Y',
6604                                                           decode ((select move_order_type
6605                                                                    from mtl_txn_request_headers mtrh
6606                                                                    where header_id = mmtt.move_order_header_id
6607                                                                    and rownum = 1),5,28,mmtt.transaction_action_id),
6608                                                         mmtt.transaction_action_id), to_number(NULL)) transaction_action_id
6609           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_subinventory, to_char(NULL)) transfer_subinventory_code
6610           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_to_location, to_number(NULL))  transfer_locator_id
6611        FROM
6612             mtl_material_transactions_temp mmtt
6613             , mtl_parameters mp -- Bug 9938149
6614        WHERE
6615             mmtt.posting_flag = 'Y'
6616     AND mmtt.lot_number IS NOT NULL
6617     AND mmtt.subinventory_code IS NOT NULL
6618     AND (Nvl(mmtt.transaction_status,0) <> 2 OR
6619             Nvl(mmtt.transaction_status,0) = 2 AND
6620             mmtt.transaction_action_id IN (1,2,28,3,21,29,32,34)
6621         )
6622       -- dont look at scrap and costing txns
6623       -- Bug 3396558 fix. Ignore ownership xfr,planning xfr transactions
6624          AND mmtt.transaction_action_id NOT IN (5,6,24,30)
6625          AND mmtt.organization_id = mp.organization_id -- Bug 9938149
6626     UNION ALL
6627        --MMTT records, lot in MTLT
6628        SELECT
6629             mmtt.organization_id    organization_id
6630           , mmtt.inventory_item_id  inventory_item_id
6631           , mmtt.revision           revision
6632           , mtlt.lot_number         lot_number
6633           , mmtt.subinventory_code  subinventory_code
6634           , mmtt.locator_id         locator_id
6635      --Bug 4185621
6636           --, Decode(mmtt.transaction_status, 2, 1,
6637           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
6638       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
6639       Sign(mmtt.primary_quantity)))
6640        * round(Abs( mtlt.primary_quantity ),5)
6641      --Bug 4185621
6642           --, Decode(mmtt.transaction_status, 2, 1,
6643           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
6644       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
6645       Sign(mmtt.secondary_transaction_quantity)))
6646        * round(Abs( mtlt.secondary_quantity ),5)
6647      --Bug 4185621
6648           --, Decode(mmtt.transaction_status, 2, 5, 1) quantity_type
6649           , Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,1,5), 1)  quantity_type
6650           , mmtt.cost_group_id       cost_group_id
6651      --14688297
6652     -- ,NVL(mmtt.allocated_lpn_id,
6653     --  NVL(mmtt.content_lpn_id, mmtt.lpn_id)) lpn_id
6654 , decode(nvl(mmtt.wms_task_status,-1), 4, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID), NVL(MMTT.ALLOCATED_LPN_ID, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID))) lpn_id
6655           /* Bug 9938149, Decode(mmtt.transaction_status, 2 , mmtt.transaction_action_id,
6656            * to_number(NULL)) transaction_action_id */
6657           , Decode(mmtt.transaction_status, 2 , decode (mp.process_enabled_flag, 'Y',
6658                                                           decode ((select move_order_type
6659                                                                    from mtl_txn_request_headers mtrh
6660                                                                    where header_id = mmtt.move_order_header_id
6661                                                                    and rownum = 1),5,28,mmtt.transaction_action_id),
6662                                                         mmtt.transaction_action_id), to_number(NULL)) transaction_action_id
6663           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_subinventory, to_char(NULL)) transfer_subinventory_code
6664           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_to_location, to_number(NULL))  transfer_locator_id
6665        FROM
6666            mtl_material_transactions_temp mmtt,
6667       mtl_transaction_lots_temp mtlt
6668       , mtl_parameters mp -- Bug 9938149
6669        WHERE
6670              mmtt.posting_flag = 'Y'
6671          AND mmtt.transaction_temp_id = mtlt.transaction_temp_id
6672          AND mmtt.lot_number IS NULL
6673     AND mmtt.subinventory_code IS NOT NULL
6674     AND (Nvl(mmtt.transaction_status,0) <> 2 OR
6675          Nvl(mmtt.transaction_status,0) = 2 AND
6676         mmtt.transaction_action_id IN (1,2,28,3,21,29,32,34)
6677       )
6678       -- dont look at scrap and costing txns
6679       -- Bug 3396558 fix. Ignore ownership xfr,planning xfr transactions
6680          AND mmtt.transaction_action_id NOT IN (5,6,24,30)
6681          AND mmtt.organization_id = mp.organization_id -- Bug 9938149
6682        UNION ALL
6683        -- receiving side of transfers, lot in MMTT
6684        -- Bug 7658493, If wms task is in loaded status, consider allocation like pending transaction.
6685        SELECT
6686             Decode(mmtt.transaction_action_id
6687                , 3, mmtt.transfer_organization
6688                , mmtt.organization_id)   organization_id
6689           , mmtt.inventory_item_id       inventory_item_id
6690           , mmtt.revision                revision
6691           , mmtt.lot_number              lot_number
6692           , mmtt.transfer_subinventory   subinventory_code
6693           , mmtt.transfer_to_location    locator_id
6694           , round(Abs( mmtt.primary_quantity),5)
6695           , round(Abs( mmtt.secondary_transaction_quantity),5)
6696           , 1                            quantity_type
6697           , mmtt.transfer_cost_group_id  cost_group_id
6698           , NVL(mmtt.content_lpn_id,mmtt.transfer_lpn_id) lpn_id
6699           , to_number(NULL)                      transaction_action_id
6700           , to_char(NULL)                        transfer_subinventory_code
6701           , to_number(NULL)                      transfer_locator_id
6702        FROM
6703             mtl_material_transactions_temp mmtt
6704        WHERE
6705              mmtt.posting_flag = 'Y'
6706           AND mmtt.lot_number IS NOT NULL
6707           AND Decode( Nvl(mmtt.transaction_status,0),
6708                        2, decode(nvl(mmtt.wms_task_status,-1), 4, 1, 2),
6709                        1 ) <> 2
6710                      AND (mmtt.transaction_action_id IN (2,3) -- Bug 9595634: Removed 28
6711                -- Bug 9595634: Added condition to match demand source if action = 28
6712                 OR (mmtt.transaction_action_id = 28
6713                     AND l_demand_source_header_id = mmtt.transaction_source_id
6714                     AND Nvl(l_demand_source_line_id, -9999) = Nvl(mmtt.TRX_SOURCE_LINE_ID,-9999)
6715                     AND Nvl(l_demand_source_name, '@@@###@@#') = Nvl(mmtt.TRANSACTION_SOURCE_NAME,'@@@###@@#')
6716                     AND Nvl(l_demand_source_delivery,-9999) = Nvl(mmtt.TRX_SOURCE_DELIVERY_ID,-9999)
6717                    )
6718                )
6719        UNION ALL
6720        -- receiving side of transfers, lot in MTLT
6721        -- Bug 7658493, If wms task is in loaded status, consider allocation like pending transaction.
6722        SELECT
6723             Decode(mmtt.transaction_action_id
6724                , 3, mmtt.transfer_organization
6725                , mmtt.organization_id)   organization_id
6726           , mmtt.inventory_item_id       inventory_item_id
6727           , mmtt.revision                revision
6728           , mtlt.lot_number              lot_number
6729           , mmtt.transfer_subinventory   subinventory_code
6730           , mmtt.transfer_to_location    locator_id
6731           , round(Abs( mtlt.primary_quantity ),5)
6732           , round(Abs( mtlt.secondary_quantity ),5)
6733           , 1                            quantity_type
6734           , mmtt.transfer_cost_group_id  cost_group_id
6735           , NVL(mmtt.content_lpn_id,mmtt.transfer_lpn_id) lpn_id
6736           , to_number(NULL)                      transaction_action_id
6737           , to_char(NULL)                        transfer_subinventory_code
6738           , to_number(NULL)                      transfer_locator_id
6739        FROM
6740             mtl_material_transactions_temp mmtt,
6741        mtl_transaction_lots_temp mtlt
6742        WHERE
6743              mmtt.posting_flag = 'Y'
6744           AND mmtt.lot_number IS NULL
6745           AND mmtt.transaction_temp_id = mtlt.transaction_temp_id
6746           AND Decode( Nvl(mmtt.transaction_status,0),
6747                        2, decode(nvl(mmtt.wms_task_status,-1), 4, 1, 2),
6748                        1 ) <> 2
6749                      AND (mmtt.transaction_action_id IN (2,3) -- Bug 9595634: Removed 28
6750                -- Bug 9595634: Added condition to match demand source if action = 28
6751                 OR (mmtt.transaction_action_id = 28
6752                     AND l_demand_source_header_id = mmtt.transaction_source_id
6753                     AND Nvl(l_demand_source_line_id, -9999) = Nvl(mmtt.TRX_SOURCE_LINE_ID,-9999)
6754                     AND Nvl(l_demand_source_name, '@@@###@@#') = Nvl(mmtt.TRANSACTION_SOURCE_NAME,'@@@###@@#')
6755                     AND Nvl(l_demand_source_delivery,-9999) = Nvl(mmtt.TRX_SOURCE_DELIVERY_ID,-9999)
6756                    )
6757                )
6758       ) x
6759       WHERE x.organization_id    = l_organization_id
6760         AND x.inventory_item_id  = l_inventory_item_id
6761       GROUP BY
6762            x.organization_id, x.inventory_item_id, x.revision
6763           , x.lot_number,x.subinventory_code, x.locator_id
6764           , x.quantity_type, x.cost_group_id, x.lpn_id
6765           , x.transaction_action_id, x.transfer_subinventory_code
6766           , x.transfer_locator_id
6767    ) x
6768     , mtl_secondary_inventories sub
6769     , mtl_lot_numbers lot
6770  WHERE
6771    x.organization_id    = sub.organization_id          (+)
6772    AND x.subinventory_code = sub.secondary_inventory_name (+)
6773    AND x.organization_id   = lot.organization_id   (+)
6774    AND x.inventory_item_id = lot.inventory_item_id (+)
6775    AND x.lot_number        = lot.lot_number        (+)
6776    AND l_grade_code = lot.grade_code
6777    AND (l_asset_subs_only = 2 OR
6778          NVL(sub.asset_inventory,1) = 1)
6779    AND ((l_onhand_source = 1 AND
6780     Nvl(sub.inventory_atp_code, 1) = 1
6781       ) OR
6782         (l_onhand_source = 2 AND
6783        Nvl(sub.availability_type, 1) = 1
6784    ) OR
6785    l_onhand_source =3
6786    OR
6787    (l_onhand_source = 4 AND
6788     Nvl(sub.inventory_atp_code, 1) = 1 AND
6789     Nvl(sub.availability_type, 1) = 1)
6790       )
6791    ;
6792 
6793      CURSOR c_lot_grade_MMS IS
6794       SELECT
6795           x.organization_id       organization_id
6796         , x.inventory_item_id     inventory_item_id
6797         , x.revision              revision
6798         , x.lot_number       lot_number
6799         , lot.expiration_date     lot_expiration_date
6800         , x.subinventory_code     subinventory_code
6801         , sub.reservable_type     reservable_type
6802         , x.locator_id            locator_id
6803         , x.primary_quantity      primary_quantity
6804         , x.secondary_quantity    secondary_quantity
6805         , x.quantity_type         quantity_type
6806         , x.cost_group_id         cost_group_id
6807         , x.lpn_id        lpn_id
6808         , x.transaction_action_id       transaction_action_id
6809         , x.transfer_subinventory_code  transfer_subinventory_code
6810         , x.transfer_locator_id         transfer_locator_id
6811         , x.status_id -- Onhand Material Status Support
6812         --, lot.reservable_type     lot_reservable_type       --Bug#8713821 to check reservable type
6813         , decode(mp.default_status_id, null, lot.reservable_type, mms.reservable_type) lot_reservable_type --Bug 13387319
6814      FROM (
6815        SELECT
6816            x.organization_id       organization_id
6817          , x.inventory_item_id     inventory_item_id
6818          , decode(l_revision_control, 2, NULL, x.revision) revision
6819          , x.lot_number            lot_number
6820          , x.subinventory_code     subinventory_code
6821          , x.locator_id            locator_id
6822          , SUM(x.primary_quantity) primary_quantity
6823          , SUM(x.secondary_quantity) secondary_quantity
6824          , x.quantity_type         quantity_type
6825          , x.cost_group_id         cost_group_id
6826          , x.lpn_id        lpn_id
6827          , x.transaction_action_id       transaction_action_id
6828          , x.transfer_subinventory_code  transfer_subinventory_code
6829          , x.transfer_locator_id         transfer_locator_id
6830          , x.status_id -- Onhand Material Status Support
6831         FROM (
6832        -- reservations
6833        SELECT
6834           mr.organization_id       organization_id
6835         , mr.inventory_item_id     inventory_item_id
6836         , mr.revision              revision
6837         , mr.lot_number            lot_number
6838         , mr.subinventory_code     subinventory_code
6839         , mr.locator_id            locator_id
6840         , mr.primary_reservation_quantity
6841            - Nvl(mr.detailed_quantity,0)    primary_quantity
6842         , mr.secondary_reservation_quantity
6843            - Nvl(mr.secondary_detailed_quantity,0)    secondary_quantity
6844         , 3                        quantity_type
6845         , to_number(NULL)     cost_group_id
6846         , lpn_id        lpn_id
6847         , to_number(NULL)                      transaction_action_id
6848         , to_char(NULL)                        transfer_subinventory_code
6849         , to_number(NULL)                      transfer_locator_id
6850         , to_number(NULL)                      status_id -- Onhand Material Status Support
6851      FROM mtl_reservations mr
6852      WHERE
6853           Nvl(mr.supply_source_type_id, 13) = 13
6854       AND mr.primary_reservation_quantity >
6855        Nvl(mr.detailed_quantity,0)
6856       AND ((l_no_lpn_reservations <>1)
6857            OR (l_no_lpn_reservations = 1 AND mr.lpn_id IS NULL))
6858       AND (l_tree_mode <> 3 OR
6859           (l_tree_mode = 3
6860       AND NOT
6861              ( l_demand_source_type_id = mr.demand_source_type_id
6862             AND l_demand_source_header_id =  mr.demand_source_header_id
6863             AND Nvl(l_demand_source_line_id, -9999) =
6864       Nvl(mr.demand_source_line_id,-9999)
6865           AND Nvl(l_demand_source_name, '@@@###@@#') =
6866       Nvl(mr.demand_source_name,'@@@###@@#')
6867        AND Nvl(l_demand_source_delivery,-9999) =
6868       Nvl(mr.demand_source_delivery,-9999)
6869               )
6870    ))
6871      UNION ALL
6872        -- onhand quantities
6873        SELECT
6874           moq.organization_id               organization_id
6875         , moq.inventory_item_id             inventory_item_id
6876         , moq.revision                      revision
6877         , moq.lot_number                    lot_number
6878         , moq.subinventory_code             subinventory_code
6879         , moq.locator_id                    locator_id
6880         , moq.primary_transaction_quantity
6881         , moq.secondary_transaction_quantity
6882         , 1                                 quantity_type
6883         , moq.cost_group_id                 cost_group_id
6884         , moq.lpn_id                        lpn_id
6885         , to_number(NULL)                   transaction_action_id
6886         , to_char(NULL)                     transfer_subinventory_code
6887         , to_number(NULL)                   transfer_locator_id
6888         , moq.status_id                     -- Onhand Material Status Support
6889        FROM
6890           mtl_onhand_quantities_detail       moq
6891      UNION ALL
6892        -- pending transactions in mmtt, lot in MMTT
6893        SELECT
6894             mmtt.organization_id    organization_id
6895           , mmtt.inventory_item_id  inventory_item_id
6896           , mmtt.revision           revision
6897           , mmtt.lot_number         lot_number
6898           , mmtt.subinventory_code  subinventory_code
6899           , mmtt.locator_id         locator_id
6900      --Bug 4185621
6901           --, Decode(mmtt.transaction_status, 2, 1,
6902           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
6903       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
6904          Sign(mmtt.primary_quantity)))
6905          * round(Abs(mmtt.primary_quantity),5)
6906      --Bug 4185621
6907           --, Decode(mmtt.transaction_status, 2, 1,
6908           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
6909       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
6910          Sign(mmtt.secondary_transaction_quantity)))
6911          * round(Abs(mmtt.secondary_transaction_quantity),5)
6912      --Bug 4185621
6913           --, Decode(mmtt.transaction_status, 2, 5, 1) quantity_type
6914           , Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,1,5), 1)  quantity_type
6915           , mmtt.cost_group_id       cost_group_id
6916       --14688297
6917     -- ,NVL(mmtt.allocated_lpn_id,
6918     --  NVL(mmtt.content_lpn_id, mmtt.lpn_id)) lpn_id
6919 , decode(nvl(mmtt.wms_task_status,-1), 4, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID), NVL(MMTT.ALLOCATED_LPN_ID, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID))) lpn_id
6920           /* Bug 9938149, Decode(mmtt.transaction_status, 2 , mmtt.transaction_action_id,
6921            * to_number(NULL)) transaction_action_id */
6922           , Decode(mmtt.transaction_status, 2 , decode (mp.process_enabled_flag, 'Y',
6923                                                           decode ((select move_order_type
6924                                                                    from mtl_txn_request_headers mtrh
6925                                                                    where header_id = mmtt.move_order_header_id
6926                                                                    and rownum = 1),5,28,mmtt.transaction_action_id),
6927                                                         mmtt.transaction_action_id), to_number(NULL)) transaction_action_id
6928           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_subinventory, to_char(NULL)) transfer_subinventory_code
6929           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_to_location, to_number(NULL))  transfer_locator_id
6930           , inv_material_status_grp.get_default_status(mmtt.organization_id
6931                                           ,mmtt.inventory_item_id
6932                                           ,mmtt.subinventory_code
6933                                           ,mmtt.locator_id
6934                                           ,mmtt.lot_number -- lot_number
6935                                           ,NVL(mmtt.allocated_lpn_id,
6936                                  NVL(mmtt.content_lpn_id, mmtt.lpn_id))
6937                                           ) status_id -- Onhand Material Status Support
6938        FROM
6939             mtl_material_transactions_temp mmtt
6940             , mtl_parameters mp -- Bug 9938149
6941        WHERE
6942             mmtt.posting_flag = 'Y'
6943     AND mmtt.lot_number IS NOT NULL
6944     AND mmtt.subinventory_code IS NOT NULL
6945     AND (Nvl(mmtt.transaction_status,0) <> 2 OR
6946             Nvl(mmtt.transaction_status,0) = 2 AND
6947             mmtt.transaction_action_id IN (1,2,28,3,21,29,32,34)
6948         )
6949       -- dont look at scrap and costing txns
6950       -- Bug 3396558 fix. Ignore ownership xfr,planning xfr transactions
6951          AND mmtt.transaction_action_id NOT IN (5,6,24,30)
6952          AND mmtt.organization_id = mp.organization_id -- Bug 9938149
6953     UNION ALL
6954        --MMTT records, lot in MTLT
6955        SELECT
6956             mmtt.organization_id    organization_id
6957           , mmtt.inventory_item_id  inventory_item_id
6958           , mmtt.revision           revision
6959           , mtlt.lot_number         lot_number
6960           , mmtt.subinventory_code  subinventory_code
6961           , mmtt.locator_id         locator_id
6962      --Bug 4185621
6963           --, Decode(mmtt.transaction_status, 2, 1,
6964           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
6965       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
6966       Sign(mmtt.primary_quantity)))
6967        * round(Abs( mtlt.primary_quantity ),5)
6968      --Bug 4185621
6969           --, Decode(mmtt.transaction_status, 2, 1,
6970           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
6971       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
6972       Sign(mmtt.secondary_transaction_quantity)))
6973        * round(Abs( mtlt.secondary_quantity ),5)
6974      --Bug 4185621
6975           --, Decode(mmtt.transaction_status, 2, 5, 1) quantity_type
6976           , Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,1,5), 1)  quantity_type
6977           , mmtt.cost_group_id       cost_group_id
6978      --14688297
6979     -- ,NVL(mmtt.allocated_lpn_id,
6980     --  NVL(mmtt.content_lpn_id, mmtt.lpn_id)) lpn_id
6981 , decode(nvl(mmtt.wms_task_status,-1), 4, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID), NVL(MMTT.ALLOCATED_LPN_ID, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID))) lpn_id
6982           /* Bug 9938149, Decode(mmtt.transaction_status, 2 , mmtt.transaction_action_id,
6983            * to_number(NULL)) transaction_action_id */
6984           , Decode(mmtt.transaction_status, 2 , decode (mp.process_enabled_flag, 'Y',
6985                                                           decode ((select move_order_type
6986                                                                    from mtl_txn_request_headers mtrh
6987                                                                    where header_id = mmtt.move_order_header_id
6988                                                                    and rownum = 1),5,28,mmtt.transaction_action_id),
6989                                                         mmtt.transaction_action_id), to_number(NULL)) transaction_action_id
6990           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_subinventory, to_char(NULL)) transfer_subinventory_code
6991           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_to_location, to_number(NULL))  transfer_locator_id
6992           , inv_material_status_grp.get_default_status(mmtt.organization_id
6993                                           ,mmtt.inventory_item_id
6994                                           ,mmtt.subinventory_code
6995                                           ,mmtt.locator_id
6996                                           ,mtlt.lot_number -- lot_number in MTLT
6997                                           ,NVL(mmtt.allocated_lpn_id,
6998                                  NVL(mmtt.content_lpn_id, mmtt.lpn_id))
6999                                           ) status_id -- Onhand Material Status Support
7000        FROM
7001            mtl_material_transactions_temp mmtt,
7002       mtl_transaction_lots_temp mtlt
7003       , mtl_parameters mp -- Bug 9938149
7004        WHERE
7005              mmtt.posting_flag = 'Y'
7006          AND mmtt.transaction_temp_id = mtlt.transaction_temp_id
7007          AND mmtt.lot_number IS NULL
7008     AND mmtt.subinventory_code IS NOT NULL
7009     AND (Nvl(mmtt.transaction_status,0) <> 2 OR
7010          Nvl(mmtt.transaction_status,0) = 2 AND
7011         mmtt.transaction_action_id IN (1,2,28,3,21,29,32,34)
7012       )
7013       -- dont look at scrap and costing txns
7014       -- Bug 3396558 fix. Ignore ownership xfr,planning xfr transactions
7015          AND mmtt.transaction_action_id NOT IN (5,6,24,30)
7016          AND mmtt.organization_id = mp.organization_id -- Bug 9938149
7017        UNION ALL
7018        -- receiving side of transfers, lot in MMTT
7019        -- Bug 7658493, If wms task is in loaded status, consider allocation like pending transaction.
7020        SELECT
7021             Decode(mmtt.transaction_action_id
7022                , 3, mmtt.transfer_organization
7023                , mmtt.organization_id)   organization_id
7024           , mmtt.inventory_item_id       inventory_item_id
7025           , mmtt.revision                revision
7026           , mmtt.lot_number              lot_number
7027           , mmtt.transfer_subinventory   subinventory_code
7028           , mmtt.transfer_to_location    locator_id
7029           , round(Abs( mmtt.primary_quantity),5)
7030           , round(Abs( mmtt.secondary_transaction_quantity),5)
7031           , 1                            quantity_type
7032           , mmtt.transfer_cost_group_id  cost_group_id
7033           , NVL(mmtt.content_lpn_id,mmtt.transfer_lpn_id) lpn_id
7034           , to_number(NULL)                      transaction_action_id
7035           , to_char(NULL)                        transfer_subinventory_code
7036           , to_number(NULL)                      transfer_locator_id
7037           , inv_material_status_grp.get_default_status(Decode(mmtt.transaction_action_id
7038                                                , 3, mmtt.transfer_organization
7039                                                , mmtt.organization_id)
7040                                           ,mmtt.inventory_item_id
7041                                           ,mmtt.transfer_subinventory
7042                                           ,mmtt.transfer_to_location
7043                                           ,mmtt.lot_number -- lot_number
7044                                           ,NVL(mmtt.content_lpn_id,mmtt.transfer_lpn_id)
7045                                           ,mmtt.transaction_action_id
7046                                           ,inv_material_status_grp.get_default_status(mmtt.organization_id
7047                                                   ,mmtt.inventory_item_id
7048                                                   ,mmtt.subinventory_code
7049                                                   ,mmtt.locator_id
7050                                                   ,mmtt.lot_number -- lot_number
7051                                                   ,NVL(mmtt.allocated_lpn_id,
7052                                         NVL(mmtt.content_lpn_id, mmtt.lpn_id))
7053                                                   )
7054                                           ) status_id -- Onhand Material Status Support
7055        FROM
7056             mtl_material_transactions_temp mmtt
7057        WHERE
7058              mmtt.posting_flag = 'Y'
7059           AND mmtt.lot_number IS NOT NULL
7060           AND Decode( Nvl(mmtt.transaction_status,0),
7061                        2, decode(nvl(mmtt.wms_task_status,-1), 4, 1, 2),
7062                        1 ) <> 2
7063                      AND (mmtt.transaction_action_id IN (2,3) -- Bug 9595634: Removed 28
7064                -- Bug 9595634: Added condition to match demand source if action = 28
7065                 OR (mmtt.transaction_action_id = 28
7066                     AND l_demand_source_header_id = mmtt.transaction_source_id
7067                     AND Nvl(l_demand_source_line_id, -9999) = Nvl(mmtt.TRX_SOURCE_LINE_ID,-9999)
7068                     AND Nvl(l_demand_source_name, '@@@###@@#') = Nvl(mmtt.TRANSACTION_SOURCE_NAME,'@@@###@@#')
7069                     AND Nvl(l_demand_source_delivery,-9999) = Nvl(mmtt.TRX_SOURCE_DELIVERY_ID,-9999)
7070                    )
7071                )
7072         UNION ALL
7073        -- receiving side of transfers, lot in MTLT
7074        -- Bug 7658493, If wms task is in loaded status, consider allocation like pending transaction.
7075        SELECT
7076             Decode(mmtt.transaction_action_id
7077                , 3, mmtt.transfer_organization
7078                , mmtt.organization_id)   organization_id
7079           , mmtt.inventory_item_id       inventory_item_id
7080           , mmtt.revision                revision
7081           , mtlt.lot_number              lot_number
7082           , mmtt.transfer_subinventory   subinventory_code
7083           , mmtt.transfer_to_location    locator_id
7084           , round(Abs( mtlt.primary_quantity ),5)
7085           , round(Abs( mtlt.secondary_quantity ),5)
7086           , 1                            quantity_type
7087           , mmtt.transfer_cost_group_id  cost_group_id
7088           , NVL(mmtt.content_lpn_id,mmtt.transfer_lpn_id) lpn_id
7089           , to_number(NULL)                      transaction_action_id
7090           , to_char(NULL)                        transfer_subinventory_code
7091           , to_number(NULL)                      transfer_locator_id
7092           , inv_material_status_grp.get_default_status(Decode(mmtt.transaction_action_id
7093                                                , 3, mmtt.transfer_organization
7094                                                , mmtt.organization_id)
7095                                           ,mmtt.inventory_item_id
7096                                           ,mmtt.transfer_subinventory
7097                                           ,mmtt.transfer_to_location
7098                                           ,mtlt.lot_number -- lot_number
7099                                           ,NVL(mmtt.content_lpn_id,mmtt.transfer_lpn_id)
7100                                           ,mmtt.transaction_action_id
7101                                           ,inv_material_status_grp.get_default_status(mmtt.organization_id
7102                                                   ,mmtt.inventory_item_id
7103                                                   ,mmtt.subinventory_code
7104                                                   ,mmtt.locator_id
7105                                                   ,mtlt.lot_number -- lot_number
7106                                                   ,NVL(mmtt.allocated_lpn_id,
7107                                         NVL(mmtt.content_lpn_id, mmtt.lpn_id))
7108                                                   )
7109                                           ) status_id -- Onhand Material Status Support
7110        FROM
7111             mtl_material_transactions_temp mmtt,
7112        mtl_transaction_lots_temp mtlt
7113        WHERE
7114              mmtt.posting_flag = 'Y'
7115           AND mmtt.lot_number IS NULL
7116           AND mmtt.transaction_temp_id = mtlt.transaction_temp_id
7117           AND Decode( Nvl(mmtt.transaction_status,0),
7118                        2, decode(nvl(mmtt.wms_task_status,-1), 4, 1, 2),
7119                        1 ) <> 2
7120                      AND (mmtt.transaction_action_id IN (2,3) -- Bug 9595634: Removed 28
7121                -- Bug 9595634: Added condition to match demand source if action = 28
7122                 OR (mmtt.transaction_action_id = 28
7123                     AND l_demand_source_header_id = mmtt.transaction_source_id
7124                     AND Nvl(l_demand_source_line_id, -9999) = Nvl(mmtt.TRX_SOURCE_LINE_ID,-9999)
7125                     AND Nvl(l_demand_source_name, '@@@###@@#') = Nvl(mmtt.TRANSACTION_SOURCE_NAME,'@@@###@@#')
7126                     AND Nvl(l_demand_source_delivery,-9999) = Nvl(mmtt.TRX_SOURCE_DELIVERY_ID,-9999)
7127                    )
7128                )
7129       ) x
7130       WHERE x.organization_id    = l_organization_id
7131         AND x.inventory_item_id  = l_inventory_item_id
7132       GROUP BY
7133            x.organization_id, x.inventory_item_id, x.revision
7134           , x.lot_number,x.subinventory_code, x.locator_id
7135           , x.quantity_type, x.cost_group_id, x.lpn_id
7136           , x.transaction_action_id, x.transfer_subinventory_code
7137           , x.transfer_locator_id, x.status_id -- Onhand Material Status Support
7138    ) x
7139     , mtl_secondary_inventories sub
7140     , mtl_item_locations loc
7141     , mtl_lot_numbers lot
7142     , mtl_parameters mp -- Onhand Material Status Support
7143     , mtl_material_statuses_b mms -- Onhand Material Status Support
7144  WHERE
7145        x.inventory_item_id = lot.inventory_item_id        (+)
7146    AND x.organization_id = lot.organization_id            (+)
7147    AND x.lot_number = lot.lot_number                      (+)
7148    AND x.organization_id = loc.organization_id            (+)
7149    AND x.locator_id = loc.inventory_location_id           (+)
7150    AND x.organization_id    = sub.organization_id         (+)
7151    AND x.subinventory_code = sub.secondary_inventory_name (+)
7152    AND x.organization_id   = lot.organization_id   (+)
7153    AND x.inventory_item_id = lot.inventory_item_id (+)
7154    AND x.lot_number        = lot.lot_number        (+)
7155    AND x.organization_id    = mp.organization_id   (+) -- Onhand Material Status Support
7156    AND x.status_id = mms.status_id                 (+) -- Onhand Material Status Support
7157    AND l_grade_code = lot.grade_code
7158    AND (l_asset_subs_only = 2 OR
7159          NVL(sub.asset_inventory,1) = 1)
7160    AND
7161    (
7162      (mp.default_status_id is null and
7163        ((l_onhand_source = 1 AND
7164                  Nvl(sub.inventory_atp_code, 1) = 1
7165              AND Nvl(loc.inventory_atp_code, 1) = 1
7166              AND Nvl(lot.inventory_atp_code, 1) = 1
7167       ) OR
7168         (l_onhand_source = 2 AND
7169        Nvl(sub.availability_type, 1) = 1
7170              AND Nvl(loc.availability_type, 1) = 1
7171              AND Nvl(lot.availability_type, 1) = 1
7172    ) OR
7173    l_onhand_source =3
7174    OR
7175    (l_onhand_source = 4 AND
7176               Nvl(sub.inventory_atp_code, 1) = 1
7177           AND Nvl(loc.inventory_atp_code, 1) = 1
7178           AND Nvl(lot.inventory_atp_code, 1) = 1
7179           AND Nvl(loc.availability_type, 1) = 1
7180           AND Nvl(lot.availability_type, 1) = 1
7181           AND Nvl(sub.availability_type, 1) = 1)
7182       )
7183      )
7184      OR -- Onhand Material Status Support
7185      (
7186        mp.default_status_id is not null and
7187            ((l_onhand_source =1 AND
7188                Nvl(mms.inventory_atp_code, 1) = 1
7189              )
7190           OR (l_onhand_source = 2 AND
7191          Nvl(mms.availability_type, 1) = 1
7192         )
7193      OR l_onhand_source =3
7194           OR (l_onhand_source = 4 AND
7195          (nvl(mms.inventory_atp_code,1) = 1
7196          AND nvl(mms.availability_type,1)=1
7197               )
7198              )
7199            )
7200      )
7201    )
7202    ;
7203      -- invConv changes end : c_lot_grade with MMS about ATP/Nettable...
7204 
7205      CURSOR c_unit IS
7206      SELECT
7207           x.organization_id       organization_id
7208         , x.inventory_item_id     inventory_item_id
7209         , x.revision              revision
7210         , NULL           lot_number
7211         , NULL         lot_expiration_date
7212         , x.subinventory_code     subinventory_code
7213         , sub.reservable_type     reservable_type
7214         , x.locator_id            locator_id
7215         , x.primary_quantity      primary_quantity
7216         , x.secondary_quantity    secondary_quantity
7217         , x.quantity_type         quantity_type
7218         , x.cost_group_id         cost_group_id
7219         , x.lpn_id        lpn_id
7220         , x.transaction_action_id       transaction_action_id
7221         , x.transfer_subinventory_code  transfer_subinventory_code
7222         , x.transfer_locator_id         transfer_locator_id
7223         , NULL                     is_reservable_lot       --Bug#8713821
7224      FROM (
7225        SELECT
7226            x.organization_id       organization_id
7227          , x.inventory_item_id     inventory_item_id
7228          , decode(l_revision_control, 2, NULL
7229       , x.revision)       revision
7230          , NULL                      lot_number
7231          , x.subinventory_code     subinventory_code
7232          , x.locator_id            locator_id
7233          , SUM(x.primary_quantity) primary_quantity
7234          , SUM(x.secondary_quantity) secondary_quantity
7235          , x.quantity_type         quantity_type
7236          , x.cost_group_id         cost_group_id
7237          , x.lpn_id        lpn_id
7238          , x.transaction_action_id       transaction_action_id
7239          , x.transfer_subinventory_code  transfer_subinventory_code
7240          , x.transfer_locator_id         transfer_locator_id
7241         FROM (
7242        -- reservations
7243        SELECT
7244           mr.organization_id       organization_id
7245         , mr.inventory_item_id     inventory_item_id
7246         , mr.revision              revision
7247         , mr.lot_number            lot_number
7248         , mr.subinventory_code     subinventory_code
7249         , mr.locator_id            locator_id
7250         , mr.primary_reservation_quantity
7251            - Nvl(mr.detailed_quantity,0)    primary_quantity
7252         , mr.secondary_reservation_quantity
7253            - Nvl(mr.secondary_detailed_quantity,0)    secondary_quantity
7254         , 3                        quantity_type
7255         , to_number(NULL)     cost_group_id
7256       , lpn_id       lpn_id
7257         , to_number(NULL)                      transaction_action_id
7258         , to_char(NULL)                        transfer_subinventory_code
7259         , to_number(NULL)                      transfer_locator_id
7260      FROM mtl_reservations mr
7261      WHERE
7262           Nvl(mr.supply_source_type_id, 13) = 13
7263       AND mr.primary_reservation_quantity > Nvl(mr.detailed_quantity,0)
7264       AND ((l_no_lpn_reservations <>1)
7265            OR (l_no_lpn_reservations = 1 AND mr.lpn_id IS NULL))
7266       AND (l_tree_mode <> 3 OR
7267           (l_tree_mode = 3
7268       AND NOT (l_demand_source_type_id = mr.demand_source_type_id
7269                AND  l_demand_source_header_id = mr.demand_source_header_id
7270                AND  Nvl(l_demand_source_line_id, -9999) =
7271          Nvl(mr.demand_source_line_id,-9999)
7272                AND  Nvl(l_demand_source_name, '@@@###@@#') =
7273          Nvl(mr.demand_source_name,'@@@###@@#')
7274           AND Nvl(l_demand_source_delivery,-9999) =
7275          Nvl(mr.demand_source_delivery,-9999)
7276       )
7277           ))
7278      UNION ALL
7279        -- onhand quantities
7280        SELECT
7281           moq.organization_id               organization_id
7282         , moq.inventory_item_id             inventory_item_id
7283         , moq.revision                      revision
7284         , moq.lot_number                    lot_number
7285         , moq.subinventory_code             subinventory_code
7286         , moq.locator_id                    locator_id
7287         , decode(l_demand_source_line_id,
7288       NULL, sum(moq.primary_transaction_quantity),
7289            pjm_ueff_onhand.onhand_quantity(
7290              l_demand_source_line_id
7291              ,moq.inventory_item_id
7292              ,moq.organization_id
7293              ,moq.revision
7294              ,moq.subinventory_code
7295              ,moq.locator_id
7296              ,moq.lot_number
7297              ,moq.lpn_id
7298              ,moq.cost_group_id)
7299           )
7300         , decode(l_demand_source_line_id,
7301       NULL, sum(moq.secondary_transaction_quantity),
7302            pjm_ueff_onhand.onhand_quantity(
7303              l_demand_source_line_id
7304              ,moq.inventory_item_id
7305              ,moq.organization_id
7306              ,moq.revision
7307              ,moq.subinventory_code
7308              ,moq.locator_id
7309              ,moq.lot_number
7310              ,moq.lpn_id
7311              ,moq.cost_group_id)
7312           )
7313         , 1                                 quantity_type
7314         , moq.cost_group_id                 cost_group_id
7315         , moq.lpn_id                        lpn_id
7316         , to_number(NULL)                   transaction_action_id
7317         , to_char(NULL)                     transfer_subinventory_code
7318         , to_number(NULL)                   transfer_locator_id
7319        FROM
7320           mtl_onhand_quantities_detail       moq
7321        GROUP BY moq.organization_id,moq.inventory_item_id,moq.revision,
7322            moq.subinventory_code,moq.locator_id,moq.lot_number,
7323            moq.lpn_id,moq.cost_group_id
7324      UNION ALL
7325        -- pending transactions in mmtt
7326        SELECT
7327             mmtt.organization_id    organization_id
7328           , mmtt.inventory_item_id  inventory_item_id
7329           , mmtt.revision           revision
7330           , NULL                 lot_number
7331           , mmtt.subinventory_code  subinventory_code
7332           , mmtt.locator_id         locator_id
7333      --Bug 4185621
7334           --, Decode(mmtt.transaction_status, 2, 1,
7335           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
7336       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
7337              Sign(mmtt.primary_quantity))) *
7338            round(Abs(decode(l_demand_source_line_id,
7339                   NULL, mmtt.primary_quantity,
7340          Nvl(pjm_ueff_onhand.txn_quantity(
7341                 l_demand_source_line_id
7342             ,mmtt.transaction_temp_id
7343             ,mmtt.lot_number
7344                   ,'Y'
7345             ,mmtt.inventory_item_id
7346             ,mmtt.organization_id
7347             ,mmtt.transaction_source_type_id
7348             ,mmtt.transaction_source_id
7349             ,mmtt.rcv_transaction_id
7350                ,sign(mmtt.primary_quantity)
7351                   ),mmtt.primary_quantity
7352             )
7353       )),5)
7354      --Bug 4185621
7355           --, Decode(mmtt.transaction_status, 2, 1,
7356           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
7357       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
7358              Sign(mmtt.secondary_transaction_quantity))) *
7359            round(Abs(decode(l_demand_source_line_id,
7360                   NULL, mmtt.secondary_transaction_quantity,
7361          Nvl(pjm_ueff_onhand.txn_quantity(
7362                 l_demand_source_line_id
7363             ,mmtt.transaction_temp_id
7364             ,mmtt.lot_number
7365                   ,'Y'
7366             ,mmtt.inventory_item_id
7367             ,mmtt.organization_id
7368             ,mmtt.transaction_source_type_id
7369             ,mmtt.transaction_source_id
7370             ,mmtt.rcv_transaction_id
7371                ,sign(mmtt.secondary_transaction_quantity)
7372                   ),mmtt.secondary_transaction_quantity
7373             )
7374       )),5)
7375      --Bug 4185621
7376           --, Decode(mmtt.transaction_status, 2, 5, 1) quantity_type
7377           , Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,1,5), 1)  quantity_type
7378           , mmtt.cost_group_id       cost_group_id
7379     --14688297
7380     -- ,NVL(mmtt.allocated_lpn_id,
7381     --  NVL(mmtt.content_lpn_id, mmtt.lpn_id)) lpn_id
7382 , decode(nvl(mmtt.wms_task_status,-1), 4, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID), NVL(MMTT.ALLOCATED_LPN_ID, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID))) lpn_id
7383           /* Bug 9938149, Decode(mmtt.transaction_status, 2 , mmtt.transaction_action_id,
7384            * to_number(NULL)) transaction_action_id */
7385           , Decode(mmtt.transaction_status, 2 , decode (mp.process_enabled_flag, 'Y',
7386                                                           decode ((select move_order_type
7387                                                                    from mtl_txn_request_headers mtrh
7388                                                                    where header_id = mmtt.move_order_header_id
7389                                                                    and rownum = 1),5,28,mmtt.transaction_action_id),
7390                                                         mmtt.transaction_action_id), to_number(NULL)) transaction_action_id
7391           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_subinventory, to_char(NULL)) transfer_subinventory_code
7392           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_to_location, to_number(NULL))  transfer_locator_id
7393        FROM
7394             mtl_material_transactions_temp mmtt
7395             , mtl_parameters mp -- Bug 9938149
7396        WHERE
7397              mmtt.posting_flag = 'Y'
7398     AND mmtt.subinventory_code IS NOT NULL
7399     AND (Nvl(mmtt.transaction_status,0) <> 2 OR
7400          Nvl(mmtt.transaction_status,0) = 2 AND
7401          mmtt.transaction_action_id IN (1,2,28,3,21,29,32,34)
7402         )
7403       -- dont look at scrap and costing txns
7404       -- Bug 3396558 fix. Ignore ownership xfr,planning xfr transactions
7405          AND mmtt.transaction_action_id NOT IN (5,6, 24,30)
7406          AND mmtt.organization_id = mp.organization_id -- Bug 9938149
7407        UNION ALL
7408        -- receiving side of transfers
7409        -- added 5/23/00
7410        -- if quantity is in an lpn, then it is containerized
7411        -- Bug 7658493, If wms task is in loaded status, consider allocation like pending transaction.
7412        SELECT
7413             Decode(mmtt.transaction_action_id
7414                , 3, mmtt.transfer_organization
7415                , mmtt.organization_id)   organization_id
7416           , mmtt.inventory_item_id       inventory_item_id
7417           , mmtt.revision                revision
7418           , NULL                         lot_number
7419           , mmtt.transfer_subinventory   subinventory_code
7420           , mmtt.transfer_to_location    locator_id
7421      , round(Abs(decode(l_demand_source_line_id,
7422             NULL, mmtt.primary_quantity,
7423             Nvl(pjm_ueff_onhand.txn_quantity(
7424                    l_demand_source_line_id
7425                ,mmtt.transaction_temp_id
7426                ,mmtt.lot_number
7427                   ,'Y'
7428                ,mmtt.inventory_item_id
7429                ,mmtt.organization_id
7430                ,mmtt.transaction_source_type_id
7431                ,mmtt.transaction_source_id
7432                ,mmtt.rcv_transaction_id
7433                   ,sign(mmtt.primary_quantity)
7434                      ),mmtt.primary_quantity
7435             )
7436       )),5)
7437      , round(Abs(decode(l_demand_source_line_id,
7438             NULL, mmtt.secondary_transaction_quantity,
7439             Nvl(pjm_ueff_onhand.txn_quantity(
7440                    l_demand_source_line_id
7441                ,mmtt.transaction_temp_id
7442                ,mmtt.lot_number
7443                   ,'Y'
7444                ,mmtt.inventory_item_id
7445                ,mmtt.organization_id
7446                ,mmtt.transaction_source_type_id
7447                ,mmtt.transaction_source_id
7448                ,mmtt.rcv_transaction_id
7449                   ,sign(mmtt.secondary_transaction_quantity)
7450                      ),mmtt.secondary_transaction_quantity
7451             )
7452       )),5)
7453           , 1                            quantity_type
7454           , mmtt.transfer_cost_group_id  cost_group_id
7455           , NVL(mmtt.content_lpn_id,mmtt.transfer_lpn_id) lpn_id
7456           , to_number(NULL)                      transaction_action_id
7457           , to_char(NULL)                        transfer_subinventory_code
7458           , to_number(NULL)                      transfer_locator_id
7459        FROM
7460             mtl_material_transactions_temp mmtt
7461        WHERE
7462              mmtt.posting_flag = 'Y'
7463           AND Decode( Nvl(mmtt.transaction_status,0),
7464                        2, decode(nvl(mmtt.wms_task_status,-1), 4, 1, 2),
7465                        1 ) <> 2
7466                      AND (mmtt.transaction_action_id IN (2,3) -- Bug 9595634: Removed 28
7467                -- Bug 9595634: Added condition to match demand source if action = 28
7468                 OR (mmtt.transaction_action_id = 28
7469                     AND l_demand_source_header_id = mmtt.transaction_source_id
7470                     AND Nvl(l_demand_source_line_id, -9999) = Nvl(mmtt.TRX_SOURCE_LINE_ID,-9999)
7471                     AND Nvl(l_demand_source_name, '@@@###@@#') = Nvl(mmtt.TRANSACTION_SOURCE_NAME,'@@@###@@#')
7472                     AND Nvl(l_demand_source_delivery,-9999) = Nvl(mmtt.TRX_SOURCE_DELIVERY_ID,-9999)
7473                    )
7474                )
7475       ) x
7476       WHERE x.organization_id    = l_organization_id
7477         AND x.inventory_item_id  = l_inventory_item_id
7478       GROUP BY
7479            x.organization_id, x.inventory_item_id, x.revision
7480           , x.lot_number, x.subinventory_code, x.locator_id
7481           , x.quantity_type, x.cost_group_id, x.lpn_id
7482           , x.transaction_action_id, x.transfer_subinventory_code
7483           , x.transfer_locator_id
7484    ) x
7485     , mtl_secondary_inventories sub
7486  WHERE
7487    x.organization_id    = sub.organization_id          (+)
7488    AND x.subinventory_code = sub.secondary_inventory_name (+)
7489    AND (l_asset_subs_only = 2 OR
7490          NVL(sub.asset_inventory,1) = 1)
7491    AND ((l_onhand_source = 1 AND
7492    Nvl(sub.inventory_atp_code, 1) = 1
7493         ) OR
7494         (l_onhand_source = 2 AND
7495        Nvl(sub.availability_type, 1) = 1
7496    ) OR
7497     l_onhand_source =3
7498    OR
7499    (l_onhand_source = 4 AND
7500     Nvl(sub.inventory_atp_code, 1) = 1 AND
7501      Nvl(sub.availability_type, 1) = 1)
7502       );
7503 
7504   --Lot Controlled and Unit Effective
7505   -- invConv change : grade_code filter is in cursor c_lot_unit_grade.
7506   -- invConv : no change here... see c_lot_unit_grade
7507   CURSOR c_lot_unit IS
7508    SELECT
7509           x.organization_id       organization_id
7510         , x.inventory_item_id     inventory_item_id
7511         , x.revision              revision
7512         , x.lot_number       lot_number
7513         , lot.expiration_date     lot_expiration_date
7514         , x.subinventory_code     subinventory_code
7515         , sub.reservable_type     reservable_type
7516         , x.locator_id            locator_id
7517         , x.primary_quantity      primary_quantity
7518         , x.secondary_quantity    secondary_quantity
7519         , x.quantity_type         quantity_type
7520         , x.cost_group_id         cost_group_id
7521         , x.lpn_id        lpn_id
7522         , x.transaction_action_id       transaction_action_id
7523         , x.transfer_subinventory_code  transfer_subinventory_code
7524         , x.transfer_locator_id         transfer_locator_id
7525         , lot.reservable_type     lot_reservable_type       --Bug#8713821 to check reservable type
7526      FROM (
7527        SELECT
7528            x.organization_id       organization_id
7529          , x.inventory_item_id     inventory_item_id
7530          , decode(l_revision_control, 2, NULL
7531          , x.revision)       revision
7532          , x.lot_number             lot_number
7533          , x.subinventory_code     subinventory_code
7534          , x.locator_id            locator_id
7535          , SUM(x.primary_quantity) primary_quantity
7536          , SUM(x.secondary_quantity) secondary_quantity
7537          , x.quantity_type         quantity_type
7538          , x.cost_group_id         cost_group_id
7539          , x.lpn_id        lpn_id
7540          , x.transaction_action_id       transaction_action_id
7541          , x.transfer_subinventory_code  transfer_subinventory_code
7542          , x.transfer_locator_id         transfer_locator_id
7543         FROM (
7544        -- reservations
7545        SELECT
7546           mr.organization_id       organization_id
7547         , mr.inventory_item_id     inventory_item_id
7548         , mr.revision              revision
7549         , mr.lot_number            lot_number
7550         , mr.subinventory_code     subinventory_code
7551         , mr.locator_id            locator_id
7552         , mr.primary_reservation_quantity
7553            - Nvl(mr.detailed_quantity,0)    primary_quantity
7554         , mr.secondary_reservation_quantity
7555            - Nvl(mr.secondary_detailed_quantity,0)    secondary_quantity
7556         , 3                        quantity_type
7557         , to_number(NULL)       cost_group_id
7558         , lpn_id        lpn_id
7559         , to_number(NULL)                      transaction_action_id
7560         , to_char(NULL)                        transfer_subinventory_code
7561         , to_number(NULL)                      transfer_locator_id
7562      FROM mtl_reservations mr
7563      WHERE
7564           Nvl(mr.supply_source_type_id, 13) = 13
7565       AND mr.primary_reservation_quantity >
7566       Nvl(mr.detailed_quantity,0)
7567       AND ((l_no_lpn_reservations <>1)
7568            OR (l_no_lpn_reservations = 1 AND mr.lpn_id IS NULL))
7569       AND (l_tree_mode <> 3 OR
7570           (l_tree_mode = 3
7571       AND NOT (l_demand_source_type_id = mr.demand_source_type_id
7572          AND l_demand_source_header_id = mr.demand_source_header_id
7573          AND Nvl(l_demand_source_line_id, -9999) =
7574          Nvl(mr.demand_source_line_id,-9999)
7575          AND Nvl(l_demand_source_name, '@@@###@@#') =
7576          Nvl(mr.demand_source_name,'@@@###@@#')
7577       AND Nvl(l_demand_source_delivery,-9999) =
7578          Nvl(mr.demand_source_delivery,-9999)
7579               )
7580      ))
7581      UNION ALL
7582        -- onhand quantities
7583        SELECT
7584           moq.organization_id               organization_id
7585         , moq.inventory_item_id             inventory_item_id
7586         , moq.revision                      revision
7587         , moq.lot_number                    lot_number
7588         , moq.subinventory_code             subinventory_code
7589         , moq.locator_id                    locator_id
7590         , decode(l_demand_source_line_id,
7591          NULL, sum(moq.primary_transaction_quantity),
7592          pjm_ueff_onhand.onhand_quantity(
7593              l_demand_source_line_id
7594             ,moq.inventory_item_id
7595             ,moq.organization_id
7596             ,moq.revision
7597             ,moq.subinventory_code
7598             ,moq.locator_id
7599             ,moq.lot_number
7600             ,moq.lpn_id
7601             ,moq.cost_group_id)
7602           )
7603         , decode(l_demand_source_line_id,
7604          NULL, sum(moq.secondary_transaction_quantity),
7605          pjm_ueff_onhand.onhand_quantity(
7606              l_demand_source_line_id
7607             ,moq.inventory_item_id
7608             ,moq.organization_id
7609             ,moq.revision
7610             ,moq.subinventory_code
7611             ,moq.locator_id
7612             ,moq.lot_number
7613             ,moq.lpn_id
7614             ,moq.cost_group_id)
7615           )
7616         , 1                                 quantity_type
7617         , moq.cost_group_id                 cost_group_id
7618         , moq.lpn_id                        lpn_id
7619         , to_number(NULL)                   transaction_action_id
7620         , to_char(NULL)                     transfer_subinventory_code
7621         , to_number(NULL)                   transfer_locator_id
7622        FROM
7623           mtl_onhand_quantities_detail moq
7624        GROUP BY moq.organization_id,moq.inventory_item_id,moq.revision,
7625            moq.subinventory_code,moq.locator_id,moq.lot_number,
7626            moq.lpn_id,moq.cost_group_id
7627      UNION ALL
7628        -- pending transactions in mmtt, lot in MMTT
7629        SELECT
7630             mmtt.organization_id    organization_id
7631           , mmtt.inventory_item_id  inventory_item_id
7632           , mmtt.revision           revision
7633           , mmtt.lot_number         lot_number
7634           , mmtt.subinventory_code  subinventory_code
7635           , mmtt.locator_id         locator_id
7636      --Bug 4185621
7637           --, Decode(mmtt.transaction_status, 2, 1,
7638           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
7639       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
7640          Sign(mmtt.primary_quantity))) *
7641       round(Abs(decode(l_demand_source_line_id,
7642          NULL, mmtt.primary_quantity,
7643          Nvl(pjm_ueff_onhand.txn_quantity(
7644                 l_demand_source_line_id
7645             ,mmtt.transaction_temp_id
7646             ,mmtt.lot_number
7647                   ,'Y'
7648             ,mmtt.inventory_item_id
7649             ,mmtt.organization_id
7650             ,mmtt.transaction_source_type_id
7651             ,mmtt.transaction_source_id
7652             ,mmtt.rcv_transaction_id
7653                ,sign(mmtt.primary_quantity)
7654                ),mmtt.primary_quantity
7655          )
7656       )),5)
7657      --Bug 4185621
7658           --, Decode(mmtt.transaction_status, 2, 1,
7659           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
7660       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
7661          Sign(mmtt.secondary_transaction_quantity))) *
7662       round(Abs(decode(l_demand_source_line_id,
7663          NULL, mmtt.secondary_transaction_quantity,
7664          Nvl(pjm_ueff_onhand.txn_quantity(
7665                 l_demand_source_line_id
7666             ,mmtt.transaction_temp_id
7667             ,mmtt.lot_number
7668                   ,'Y'
7669             ,mmtt.inventory_item_id
7670             ,mmtt.organization_id
7671             ,mmtt.transaction_source_type_id
7672             ,mmtt.transaction_source_id
7673             ,mmtt.rcv_transaction_id
7674                ,sign(mmtt.secondary_transaction_quantity)
7675                ),mmtt.secondary_transaction_quantity
7676          )
7677       )),5)
7678    --Bug 4185621
7679         --, Decode(mmtt.transaction_status, 2, 5, 1) quantity_type
7680         , Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,1,5), 1)  quantity_type
7681         , mmtt.cost_group_id      cost_group_id
7682     --14688297
7683   -- ,NVL(mmtt.allocated_lpn_id,
7684     --  NVL(mmtt.content_lpn_id, mmtt.lpn_id)) lpn_id
7685 , decode(nvl(mmtt.wms_task_status,-1), 4, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID), NVL(MMTT.ALLOCATED_LPN_ID, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID))) lpn_id
7686           /* Bug 9938149, Decode(mmtt.transaction_status, 2 , mmtt.transaction_action_id,
7687            * to_number(NULL)) transaction_action_id */
7688           , Decode(mmtt.transaction_status, 2 , decode (mp.process_enabled_flag, 'Y',
7689                                                           decode ((select move_order_type
7690                                                                    from mtl_txn_request_headers mtrh
7691                                                                    where header_id = mmtt.move_order_header_id
7692                                                                    and rownum = 1),5,28,mmtt.transaction_action_id),
7693                                                         mmtt.transaction_action_id), to_number(NULL)) transaction_action_id
7694           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_subinventory, to_char(NULL)) transfer_subinventory_code
7695           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_to_location, to_number(NULL))  transfer_locator_id
7696        FROM
7697             mtl_material_transactions_temp mmtt
7698             , mtl_parameters mp -- Bug 9938149
7699        WHERE
7700             mmtt.posting_flag = 'Y'
7701     AND mmtt.lot_number IS NOT NULL
7702     AND mmtt.subinventory_code IS NOT NULL
7703     AND (Nvl(mmtt.transaction_status,0) <> 2 OR
7704             Nvl(mmtt.transaction_status,0) = 2 AND
7705          mmtt.transaction_action_id IN (1,2,28,3,21,29,32,34)
7706         )
7707       -- dont look at scrap and costing txns
7708       -- Bug 3396558 fix. Ignore ownership xfr,planning xfr transactions
7709          AND mmtt.transaction_action_id NOT IN (5,6, 24,30)
7710          AND mmtt.organization_id = mp.organization_id -- Bug 9938149
7711     UNION ALL
7712        --MMTT records, lot in MTLT
7713        SELECT
7714             mmtt.organization_id    organization_id
7715           , mmtt.inventory_item_id  inventory_item_id
7716           , mmtt.revision           revision
7717           , mtlt.lot_number         lot_number
7718           , mmtt.subinventory_code  subinventory_code
7719           , mmtt.locator_id         locator_id
7720      --Bug 4185621
7721           --, Decode(mmtt.transaction_status, 2, 1,
7722           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
7723       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
7724          Sign(mmtt.primary_quantity))) *
7725       round(Abs(decode(l_demand_source_line_id,
7726          NULL, mtlt.primary_quantity,
7727          Nvl(pjm_ueff_onhand.txn_quantity(
7728                 l_demand_source_line_id
7729             ,mmtt.transaction_temp_id
7730             ,mtlt.lot_number
7731                   ,'Y'
7732             ,mmtt.inventory_item_id
7733             ,mmtt.organization_id
7734             ,mmtt.transaction_source_type_id
7735             ,mmtt.transaction_source_id
7736             ,mmtt.rcv_transaction_id
7737                ,sign(mmtt.primary_quantity)
7738                ),mtlt.primary_quantity)
7739       )),5)
7740      --Bug 4185621
7741           --, Decode(mmtt.transaction_status, 2, 1,
7742           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
7743       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
7744          Sign(mmtt.secondary_transaction_quantity))) *
7745       round(Abs(decode(l_demand_source_line_id,
7746          NULL, mtlt.secondary_quantity,
7747          Nvl(pjm_ueff_onhand.txn_quantity(
7748                 l_demand_source_line_id
7749             ,mmtt.transaction_temp_id
7750             ,mtlt.lot_number
7751                   ,'Y'
7752             ,mmtt.inventory_item_id
7753             ,mmtt.organization_id
7754             ,mmtt.transaction_source_type_id
7755             ,mmtt.transaction_source_id
7756             ,mmtt.rcv_transaction_id
7757                ,sign(mmtt.secondary_transaction_quantity)
7758                ),mtlt.secondary_quantity)
7759       )),5)
7760     --Bug 4185621
7761          --, Decode(mmtt.transaction_status, 2, 5, 1) quantity_type
7762          , Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,1,5), 1)  quantity_type
7763           , mmtt.cost_group_id       cost_group_id
7764      --14688297
7765     -- ,NVL(mmtt.allocated_lpn_id,
7766     --  NVL(mmtt.content_lpn_id, mmtt.lpn_id)) lpn_id
7767 , decode(nvl(mmtt.wms_task_status,-1), 4, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID), NVL(MMTT.ALLOCATED_LPN_ID, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID))) lpn_id
7768           /* Bug 9938149, Decode(mmtt.transaction_status, 2 , mmtt.transaction_action_id,
7769            * to_number(NULL)) transaction_action_id */
7770           , Decode(mmtt.transaction_status, 2 , decode (mp.process_enabled_flag, 'Y',
7771                                                           decode ((select move_order_type
7772                                                                    from mtl_txn_request_headers mtrh
7773                                                                    where header_id = mmtt.move_order_header_id
7774                                                                    and rownum = 1),5,28,mmtt.transaction_action_id),
7775                                                         mmtt.transaction_action_id), to_number(NULL)) transaction_action_id
7776           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_subinventory, to_char(NULL)) transfer_subinventory_code
7777           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_to_location, to_number(NULL))  transfer_locator_id
7778        FROM
7779             mtl_material_transactions_temp mmtt,
7780       mtl_transaction_lots_temp mtlt
7781       , mtl_parameters mp -- Bug 9938149
7782        WHERE
7783              mmtt.posting_flag = 'Y'
7784          AND mmtt.transaction_temp_id = mtlt.transaction_temp_id
7785          AND mmtt.lot_number IS NULL
7786     AND mmtt.subinventory_code IS NOT NULL
7787     AND (Nvl(mmtt.transaction_status,0) <> 2 OR
7788             Nvl(mmtt.transaction_status,0) = 2 AND
7789             mmtt.transaction_action_id IN (1,2,28,3,21,29,32,34)
7790         )
7791       -- dont look at scrap and costing txns
7792       -- Bug 3396558 fix. Ignore ownership xfr,planning xfr transactions
7793          AND mmtt.transaction_action_id NOT IN (5,6, 24,30)
7794          AND mmtt.organization_id = mp.organization_id -- Bug 9938149
7795        UNION ALL
7796        -- receiving side of transfers lot in MMTT
7797        -- Bug 7658493, If wms task is in loaded status, consider allocation like pending transaction.
7798        SELECT
7799             Decode(mmtt.transaction_action_id
7800                , 3, mmtt.transfer_organization
7801                , mmtt.organization_id)   organization_id
7802           , mmtt.inventory_item_id       inventory_item_id
7803           , mmtt.revision                revision
7804           , mmtt.lot_number              lot_number
7805           , mmtt.transfer_subinventory   subinventory_code
7806           , mmtt.transfer_to_location    locator_id
7807      , round(Abs(decode(l_demand_source_line_id,
7808          NULL, mmtt.primary_quantity,
7809          Nvl(pjm_ueff_onhand.txn_quantity(
7810                 l_demand_source_line_id
7811             ,mmtt.transaction_temp_id
7812             ,mmtt.lot_number
7813                ,'Y'
7814             ,mmtt.inventory_item_id
7815             ,mmtt.organization_id
7816             ,mmtt.transaction_source_type_id
7817             ,mmtt.transaction_source_id
7818             ,mmtt.rcv_transaction_id
7819                ,sign(mmtt.primary_quantity)
7820                ),mmtt.primary_quantity)
7821          )),5)
7822      , round(Abs(decode(l_demand_source_line_id,
7823          NULL, mmtt.secondary_transaction_quantity,
7824          Nvl(pjm_ueff_onhand.txn_quantity(
7825                 l_demand_source_line_id
7826             ,mmtt.transaction_temp_id
7827             ,mmtt.lot_number
7828                ,'Y'
7829             ,mmtt.inventory_item_id
7830             ,mmtt.organization_id
7831             ,mmtt.transaction_source_type_id
7832             ,mmtt.transaction_source_id
7833             ,mmtt.rcv_transaction_id
7834                ,sign(mmtt.secondary_transaction_quantity)
7835                ),mmtt.secondary_transaction_quantity)
7836          )),5)
7837           , 1                            quantity_type
7838           , mmtt.transfer_cost_group_id  cost_group_id
7839           , NVL(mmtt.content_lpn_id,mmtt.transfer_lpn_id)  lpn_id
7840           , to_number(NULL)                      transaction_action_id
7841           , to_char(NULL)                        transfer_subinventory_code
7842           , to_number(NULL)                      transfer_locator_id
7843        FROM
7844             mtl_material_transactions_temp mmtt
7845        WHERE
7846              mmtt.posting_flag = 'Y'
7847          AND mmtt.lot_number IS NOT NULL
7848          AND Decode( Nvl(mmtt.transaction_status,0),
7849                        2, decode(nvl(mmtt.wms_task_status,-1), 4, 1, 2),
7850                        1 ) <> 2
7851                     AND (mmtt.transaction_action_id IN (2,3) -- Bug 9595634: Removed 28
7852                -- Bug 9595634: Added condition to match demand source if action = 28
7853                 OR (mmtt.transaction_action_id = 28
7854                     AND l_demand_source_header_id = mmtt.transaction_source_id
7855                     AND Nvl(l_demand_source_line_id, -9999) = Nvl(mmtt.TRX_SOURCE_LINE_ID,-9999)
7856                     AND Nvl(l_demand_source_name, '@@@###@@#') = Nvl(mmtt.TRANSACTION_SOURCE_NAME,'@@@###@@#')
7857                     AND Nvl(l_demand_source_delivery,-9999) = Nvl(mmtt.TRX_SOURCE_DELIVERY_ID,-9999)
7858                    )
7859                )
7860         UNION ALL
7861         -- receiving side of transfers  lot in MTLT
7862         -- Bug 7658493, If wms task is in loaded status, consider allocation like pending transaction.
7863        SELECT
7864             Decode(mmtt.transaction_action_id
7865                , 3, mmtt.transfer_organization
7866                , mmtt.organization_id)   organization_id
7867           , mmtt.inventory_item_id       inventory_item_id
7868           , mmtt.revision                revision
7869           , mtlt.lot_number              lot_number
7870           , mmtt.transfer_subinventory   subinventory_code
7871           , mmtt.transfer_to_location    locator_id
7872      , round(Abs(decode(l_demand_source_line_id,
7873          NULL, mtlt.primary_quantity,
7874          Nvl(pjm_ueff_onhand.txn_quantity(
7875                 l_demand_source_line_id
7876             ,mmtt.transaction_temp_id
7877             ,mtlt.lot_number
7878                ,'Y'
7879             ,mmtt.inventory_item_id
7880             ,mmtt.organization_id
7881             ,mmtt.transaction_source_type_id
7882             ,mmtt.transaction_source_id
7883             ,mmtt.rcv_transaction_id
7884                ,sign(mmtt.primary_quantity)
7885                ),mtlt.primary_quantity)
7886       )),5)
7887      , round(Abs(decode(l_demand_source_line_id,
7888          NULL, mtlt.secondary_quantity,
7889          Nvl(pjm_ueff_onhand.txn_quantity(
7890                 l_demand_source_line_id
7891             ,mmtt.transaction_temp_id
7892             ,mtlt.lot_number
7893                ,'Y'
7894             ,mmtt.inventory_item_id
7895             ,mmtt.organization_id
7896             ,mmtt.transaction_source_type_id
7897             ,mmtt.transaction_source_id
7898             ,mmtt.rcv_transaction_id
7899                ,sign(mmtt.secondary_transaction_quantity)
7900                ),mtlt.secondary_quantity)
7901       )),5)
7902           , 1                            quantity_type
7903           , mmtt.transfer_cost_group_id  cost_group_id
7904           , NVL(mmtt.content_lpn_id,mmtt.transfer_lpn_id) lpn_id
7905           , to_number(NULL)                      transaction_action_id
7906           , to_char(NULL)                        transfer_subinventory_code
7907           , to_number(NULL)                      transfer_locator_id
7908        FROM
7909             mtl_material_transactions_temp mmtt
7910       ,mtl_transaction_lots_temp mtlt
7911        WHERE
7912              mmtt.posting_flag = 'Y'
7913           AND mmtt.lot_number IS NULL
7914           AND mmtt.transaction_temp_id = mtlt.transaction_temp_id
7915           AND Decode( Nvl(mmtt.transaction_status,0),
7916                        2, decode(nvl(mmtt.wms_task_status,-1), 4, 1, 2),
7917                        1 ) <> 2
7918                      AND (mmtt.transaction_action_id IN (2,3) -- Bug 9595634: Removed 28
7919                -- Bug 9595634: Added condition to match demand source if action = 28
7920                 OR (mmtt.transaction_action_id = 28
7921                     AND l_demand_source_header_id = mmtt.transaction_source_id
7922                     AND Nvl(l_demand_source_line_id, -9999) = Nvl(mmtt.TRX_SOURCE_LINE_ID,-9999)
7923                     AND Nvl(l_demand_source_name, '@@@###@@#') = Nvl(mmtt.TRANSACTION_SOURCE_NAME,'@@@###@@#')
7924                     AND Nvl(l_demand_source_delivery,-9999) = Nvl(mmtt.TRX_SOURCE_DELIVERY_ID,-9999)
7925                    )
7926                )
7927       ) x
7928       WHERE x.organization_id    = l_organization_id
7929         AND x.inventory_item_id  = l_inventory_item_id
7930       GROUP BY
7931            x.organization_id, x.inventory_item_id, x.revision
7932           , x.lot_number,x.subinventory_code, x.locator_id
7933           , x.quantity_type, x.cost_group_id, x.lpn_id
7934           , x.transaction_action_id, x.transfer_subinventory_code
7935           , x.transfer_locator_id
7936    ) x
7937     , mtl_secondary_inventories sub
7938     , mtl_lot_numbers lot
7939  WHERE
7940    x.organization_id    = sub.organization_id          (+)
7941    AND x.subinventory_code = sub.secondary_inventory_name (+)
7942    AND x.organization_id   = lot.organization_id   (+)
7943    AND x.inventory_item_id = lot.inventory_item_id (+)
7944    AND x.lot_number        = lot.lot_number        (+)
7945    AND (l_asset_subs_only = 2 OR
7946          NVL(sub.asset_inventory,1) = 1)
7947    AND ((l_onhand_source = 1 AND
7948       Nvl(sub.inventory_atp_code, 1) = 1
7949     ) OR
7950         (l_onhand_source = 2 AND
7951        Nvl(sub.availability_type, 1) = 1
7952     ) OR
7953     l_onhand_source =3
7954       OR
7955     (l_onhand_source = 4 AND
7956       Nvl(sub.inventory_atp_code, 1) = 1 AND
7957                 Nvl(sub.availability_type, 1) = 1
7958     )
7959       )
7960    ;
7961 
7962   -- invConv change : grade_code filter is in here
7963   CURSOR c_lot_unit_grade IS
7964    SELECT
7965           x.organization_id       organization_id
7966         , x.inventory_item_id     inventory_item_id
7967         , x.revision              revision
7968         , x.lot_number       lot_number
7969         , lot.expiration_date     lot_expiration_date
7970         , x.subinventory_code     subinventory_code
7971         , sub.reservable_type     reservable_type
7972         , x.locator_id            locator_id
7973         , x.primary_quantity      primary_quantity
7974         , x.secondary_quantity    secondary_quantity
7975         , x.quantity_type         quantity_type
7976         , x.cost_group_id         cost_group_id
7977         , x.lpn_id        lpn_id
7978         , x.transaction_action_id       transaction_action_id
7979         , x.transfer_subinventory_code  transfer_subinventory_code
7980         , x.transfer_locator_id         transfer_locator_id
7981         , lot.reservable_type     lot_reservable_type       --Bug#8713821 to check reservable type
7982      FROM (
7983        SELECT
7984            x.organization_id       organization_id
7985          , x.inventory_item_id     inventory_item_id
7986          , decode(l_revision_control, 2, NULL
7987          , x.revision)       revision
7988          , x.lot_number             lot_number
7989          , x.subinventory_code     subinventory_code
7990          , x.locator_id            locator_id
7991          , SUM(x.primary_quantity) primary_quantity
7992          , SUM(x.secondary_quantity) secondary_quantity
7993          , x.quantity_type         quantity_type
7994          , x.cost_group_id         cost_group_id
7995          , x.lpn_id        lpn_id
7996          , x.transaction_action_id       transaction_action_id
7997          , x.transfer_subinventory_code  transfer_subinventory_code
7998          , x.transfer_locator_id         transfer_locator_id
7999         FROM (
8000        -- reservations
8001        SELECT
8002           mr.organization_id       organization_id
8003         , mr.inventory_item_id     inventory_item_id
8004         , mr.revision              revision
8005         , mr.lot_number            lot_number
8006         , mr.subinventory_code     subinventory_code
8007         , mr.locator_id            locator_id
8008         , mr.primary_reservation_quantity
8009            - Nvl(mr.detailed_quantity,0)    primary_quantity
8010         , mr.secondary_reservation_quantity
8011            - Nvl(mr.secondary_detailed_quantity,0)    secondary_quantity
8012         , 3                        quantity_type
8013         , to_number(NULL)       cost_group_id
8014         , lpn_id        lpn_id
8015         , to_number(NULL)                      transaction_action_id
8016         , to_char(NULL)                        transfer_subinventory_code
8017         , to_number(NULL)                      transfer_locator_id
8018      FROM mtl_reservations mr
8019      WHERE
8020           Nvl(mr.supply_source_type_id, 13) = 13
8021       AND mr.primary_reservation_quantity >
8022       Nvl(mr.detailed_quantity,0)
8023       AND ((l_no_lpn_reservations <>1)
8024            OR (l_no_lpn_reservations = 1 AND mr.lpn_id IS NULL))
8025       AND (l_tree_mode <> 3 OR
8026           (l_tree_mode = 3
8027       AND NOT (l_demand_source_type_id = mr.demand_source_type_id
8028          AND l_demand_source_header_id = mr.demand_source_header_id
8029          AND Nvl(l_demand_source_line_id, -9999) =
8030          Nvl(mr.demand_source_line_id,-9999)
8031          AND Nvl(l_demand_source_name, '@@@###@@#') =
8032          Nvl(mr.demand_source_name,'@@@###@@#')
8033       AND Nvl(l_demand_source_delivery,-9999) =
8034          Nvl(mr.demand_source_delivery,-9999)
8035               )
8036      ))
8037      UNION ALL
8038        -- onhand quantities
8039        SELECT
8040           moq.organization_id               organization_id
8041         , moq.inventory_item_id             inventory_item_id
8042         , moq.revision                      revision
8043         , moq.lot_number                    lot_number
8044         , moq.subinventory_code             subinventory_code
8045         , moq.locator_id                    locator_id
8046         , decode(l_demand_source_line_id,
8047          NULL, sum(moq.primary_transaction_quantity),
8048          pjm_ueff_onhand.onhand_quantity(
8049              l_demand_source_line_id
8050             ,moq.inventory_item_id
8051             ,moq.organization_id
8052             ,moq.revision
8053             ,moq.subinventory_code
8054             ,moq.locator_id
8055             ,moq.lot_number
8056             ,moq.lpn_id
8057             ,moq.cost_group_id)
8058           )
8059         , decode(l_demand_source_line_id,
8060          NULL, sum(moq.secondary_transaction_quantity),
8061          pjm_ueff_onhand.onhand_quantity(
8062              l_demand_source_line_id
8063             ,moq.inventory_item_id
8064             ,moq.organization_id
8065             ,moq.revision
8066             ,moq.subinventory_code
8067             ,moq.locator_id
8068             ,moq.lot_number
8069             ,moq.lpn_id
8070             ,moq.cost_group_id)
8071           )
8072         , 1                                 quantity_type
8073         , moq.cost_group_id                 cost_group_id
8074         , moq.lpn_id                        lpn_id
8075         , to_number(NULL)                   transaction_action_id
8076         , to_char(NULL)                     transfer_subinventory_code
8077         , to_number(NULL)                   transfer_locator_id
8078        FROM
8079           mtl_onhand_quantities_detail moq
8080        GROUP BY moq.organization_id,moq.inventory_item_id,moq.revision,
8081            moq.subinventory_code,moq.locator_id,moq.lot_number,
8082            moq.lpn_id,moq.cost_group_id
8083      UNION ALL
8084        -- pending transactions in mmtt, lot in MMTT
8085        SELECT
8086             mmtt.organization_id    organization_id
8087           , mmtt.inventory_item_id  inventory_item_id
8088           , mmtt.revision           revision
8089           , mmtt.lot_number         lot_number
8090           , mmtt.subinventory_code  subinventory_code
8091           , mmtt.locator_id         locator_id
8092      --Bug 4185621
8093           --, Decode(mmtt.transaction_status, 2, 1,
8094           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
8095       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
8096          Sign(mmtt.primary_quantity))) *
8097       round(Abs(decode(l_demand_source_line_id,
8098          NULL, mmtt.primary_quantity,
8099          Nvl(pjm_ueff_onhand.txn_quantity(
8100                 l_demand_source_line_id
8101             ,mmtt.transaction_temp_id
8102             ,mmtt.lot_number
8103                   ,'Y'
8104             ,mmtt.inventory_item_id
8105             ,mmtt.organization_id
8106             ,mmtt.transaction_source_type_id
8107             ,mmtt.transaction_source_id
8108             ,mmtt.rcv_transaction_id
8109                ,sign(mmtt.primary_quantity)
8110                ),mmtt.primary_quantity
8111          )
8112       )),5)
8113      --Bug 4185621
8114           --, Decode(mmtt.transaction_status, 2, 1,
8115           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
8116       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
8117          Sign(mmtt.secondary_transaction_quantity))) *
8118       round(Abs(decode(l_demand_source_line_id,
8119          NULL, mmtt.secondary_transaction_quantity,
8120          Nvl(pjm_ueff_onhand.txn_quantity(
8121                 l_demand_source_line_id
8122             ,mmtt.transaction_temp_id
8123             ,mmtt.lot_number
8124                   ,'Y'
8125             ,mmtt.inventory_item_id
8126             ,mmtt.organization_id
8127             ,mmtt.transaction_source_type_id
8128             ,mmtt.transaction_source_id
8129             ,mmtt.rcv_transaction_id
8130                ,sign(mmtt.secondary_transaction_quantity)
8131                ),mmtt.secondary_transaction_quantity
8132          )
8133       )),5)
8134    --Bug 4185621
8135         --, Decode(mmtt.transaction_status, 2, 5, 1) quantity_type
8136         , Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,1,5), 1)  quantity_type
8137         , mmtt.cost_group_id      cost_group_id
8138    --14688297
8139    --,NVL(mmtt.allocated_lpn_id,
8140    --   NVL(mmtt.content_lpn_id, mmtt.lpn_id)) lpn_id
8141 , decode(nvl(mmtt.wms_task_status,-1), 4, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID), NVL(MMTT.ALLOCATED_LPN_ID, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID))) lpn_id
8142           /* Bug 9938149, Decode(mmtt.transaction_status, 2 , mmtt.transaction_action_id,
8143            * to_number(NULL)) transaction_action_id */
8144           , Decode(mmtt.transaction_status, 2 , decode (mp.process_enabled_flag, 'Y',
8145                                                           decode ((select move_order_type
8146                                                                    from mtl_txn_request_headers mtrh
8147                                                                    where header_id = mmtt.move_order_header_id
8148                                                                    and rownum = 1),5,28,mmtt.transaction_action_id),
8149                                                         mmtt.transaction_action_id), to_number(NULL)) transaction_action_id
8150           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_subinventory, to_char(NULL)) transfer_subinventory_code
8151           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_to_location, to_number(NULL))  transfer_locator_id
8152        FROM
8153             mtl_material_transactions_temp mmtt
8154             , mtl_parameters mp -- Bug 9938149
8155        WHERE
8156             mmtt.posting_flag = 'Y'
8157     AND mmtt.lot_number IS NOT NULL
8158     AND mmtt.subinventory_code IS NOT NULL
8159     AND (Nvl(mmtt.transaction_status,0) <> 2 OR
8160             Nvl(mmtt.transaction_status,0) = 2 AND
8161          mmtt.transaction_action_id IN (1,2,28,3,21,29,32,34)
8162         )
8163       -- dont look at scrap and costing txns
8164       -- Bug 3396558 fix. Ignore ownership xfr,planning xfr transactions
8165          AND mmtt.transaction_action_id NOT IN (5,6, 24,30)
8166          AND mmtt.organization_id = mp.organization_id -- Bug 9938149
8167     UNION ALL
8168        --MMTT records, lot in MTLT
8169        SELECT
8170             mmtt.organization_id    organization_id
8171           , mmtt.inventory_item_id  inventory_item_id
8172           , mmtt.revision           revision
8173           , mtlt.lot_number         lot_number
8174           , mmtt.subinventory_code  subinventory_code
8175           , mmtt.locator_id         locator_id
8176      --Bug 4185621
8177           --, Decode(mmtt.transaction_status, 2, 1,
8178           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
8179       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
8180          Sign(mmtt.primary_quantity))) *
8181       round(Abs(decode(l_demand_source_line_id,
8182          NULL, mtlt.primary_quantity,
8183          Nvl(pjm_ueff_onhand.txn_quantity(
8184                 l_demand_source_line_id
8185             ,mmtt.transaction_temp_id
8186             ,mtlt.lot_number
8187                   ,'Y'
8188             ,mmtt.inventory_item_id
8189             ,mmtt.organization_id
8190             ,mmtt.transaction_source_type_id
8191             ,mmtt.transaction_source_id
8192             ,mmtt.rcv_transaction_id
8193                ,sign(mmtt.primary_quantity)
8194                ),mtlt.primary_quantity)
8195       )),5)
8196      --Bug 4185621
8197           --, Decode(mmtt.transaction_status, 2, 1,
8198           , Decode(Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,-1,2), mmtt.transaction_status), 2, 1,
8199       Decode(mmtt.transaction_action_id,1,-1,2,-1,28,-1,3,-1,
8200          Sign(mmtt.secondary_transaction_quantity))) *
8201       round(Abs(decode(l_demand_source_line_id,
8202          NULL, mtlt.secondary_quantity,
8203          Nvl(pjm_ueff_onhand.txn_quantity(
8204                 l_demand_source_line_id
8205             ,mmtt.transaction_temp_id
8206             ,mtlt.lot_number
8207                   ,'Y'
8208             ,mmtt.inventory_item_id
8209             ,mmtt.organization_id
8210             ,mmtt.transaction_source_type_id
8211             ,mmtt.transaction_source_id
8212             ,mmtt.rcv_transaction_id
8213                ,sign(mmtt.secondary_transaction_quantity)
8214                ),mtlt.secondary_quantity)
8215       )),5)
8216     --Bug 4185621
8217          --, Decode(mmtt.transaction_status, 2, 5, 1) quantity_type
8218          , Decode(mmtt.transaction_status, 2, decode(nvl(mmtt.wms_task_status,-1),4,1,5), 1)  quantity_type
8219           , mmtt.cost_group_id       cost_group_id
8220      --14688297
8221     -- ,NVL(mmtt.allocated_lpn_id,
8222     --  NVL(mmtt.content_lpn_id, mmtt.lpn_id)) lpn_id
8223 , decode(nvl(mmtt.wms_task_status,-1), 4, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID), NVL(MMTT.ALLOCATED_LPN_ID, NVL(MMTT.CONTENT_LPN_ID, MMTT.LPN_ID))) lpn_id
8224           /* Bug 9938149, Decode(mmtt.transaction_status, 2 , mmtt.transaction_action_id,
8225            * to_number(NULL)) transaction_action_id */
8226           , Decode(mmtt.transaction_status, 2 , decode (mp.process_enabled_flag, 'Y',
8227                                                           decode ((select move_order_type
8228                                                                    from mtl_txn_request_headers mtrh
8229                                                                    where header_id = mmtt.move_order_header_id
8230                                                                    and rownum = 1),5,28,mmtt.transaction_action_id),
8231                                                         mmtt.transaction_action_id), to_number(NULL)) transaction_action_id
8232           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_subinventory, to_char(NULL)) transfer_subinventory_code
8233           , Decode(mmtt.transaction_status, 2 , mmtt.transfer_to_location, to_number(NULL))  transfer_locator_id
8234        FROM
8235             mtl_material_transactions_temp mmtt,
8236       mtl_transaction_lots_temp mtlt
8237       , mtl_parameters mp -- Bug 9938149
8238        WHERE
8239              mmtt.posting_flag = 'Y'
8240          AND mmtt.transaction_temp_id = mtlt.transaction_temp_id
8241          AND mmtt.lot_number IS NULL
8242     AND mmtt.subinventory_code IS NOT NULL
8243     AND (Nvl(mmtt.transaction_status,0) <> 2 OR
8244             Nvl(mmtt.transaction_status,0) = 2 AND
8245             mmtt.transaction_action_id IN (1,2,28,3,21,29,32,34)
8246         )
8247       -- dont look at scrap and costing txns
8248       -- Bug 3396558 fix. Ignore ownership xfr,planning xfr transactions
8249          AND mmtt.transaction_action_id NOT IN (5,6, 24,30)
8250          AND mmtt.organization_id = mp.organization_id -- Bug 9938149
8251        UNION ALL
8252        -- receiving side of transfers lot in MMTT
8253        -- Bug 7658493, If wms task is in loaded status, consider allocation like pending transaction.
8254        SELECT
8255             Decode(mmtt.transaction_action_id
8256                , 3, mmtt.transfer_organization
8257                , mmtt.organization_id)   organization_id
8258           , mmtt.inventory_item_id       inventory_item_id
8259           , mmtt.revision                revision
8260           , mmtt.lot_number              lot_number
8261           , mmtt.transfer_subinventory   subinventory_code
8262           , mmtt.transfer_to_location    locator_id
8263      , round(Abs(decode(l_demand_source_line_id,
8264          NULL, mmtt.primary_quantity,
8265          Nvl(pjm_ueff_onhand.txn_quantity(
8266                 l_demand_source_line_id
8267             ,mmtt.transaction_temp_id
8268             ,mmtt.lot_number
8269                ,'Y'
8270             ,mmtt.inventory_item_id
8271             ,mmtt.organization_id
8272             ,mmtt.transaction_source_type_id
8273             ,mmtt.transaction_source_id
8274             ,mmtt.rcv_transaction_id
8275                ,sign(mmtt.primary_quantity)
8276                ),mmtt.primary_quantity)
8277          )),5)
8278      , round(Abs(decode(l_demand_source_line_id,
8279          NULL, mmtt.secondary_transaction_quantity,
8280          Nvl(pjm_ueff_onhand.txn_quantity(
8281                 l_demand_source_line_id
8282             ,mmtt.transaction_temp_id
8283             ,mmtt.lot_number
8284                ,'Y'
8285             ,mmtt.inventory_item_id
8286             ,mmtt.organization_id
8287             ,mmtt.transaction_source_type_id
8288             ,mmtt.transaction_source_id
8289             ,mmtt.rcv_transaction_id
8290                ,sign(mmtt.secondary_transaction_quantity)
8291                ),mmtt.secondary_transaction_quantity)
8292          )),5)
8293           , 1                            quantity_type
8294           , mmtt.transfer_cost_group_id  cost_group_id
8295           , NVL(mmtt.content_lpn_id,mmtt.transfer_lpn_id)  lpn_id
8296           , to_number(NULL)                      transaction_action_id
8297           , to_char(NULL)                        transfer_subinventory_code
8298           , to_number(NULL)                      transfer_locator_id
8299        FROM
8300             mtl_material_transactions_temp mmtt
8301        WHERE
8302              mmtt.posting_flag = 'Y'
8303          AND mmtt.lot_number IS NOT NULL
8304          AND Decode( Nvl(mmtt.transaction_status,0),
8305                        2, decode(nvl(mmtt.wms_task_status,-1), 4, 1, 2),
8306                        1 ) <> 2
8307                     AND (mmtt.transaction_action_id IN (2,3) -- Bug 9595634: Removed 28
8308                -- Bug 9595634: Added condition to match demand source if action = 28
8309                 OR (mmtt.transaction_action_id = 28
8310                     AND l_demand_source_header_id = mmtt.transaction_source_id
8311                     AND Nvl(l_demand_source_line_id, -9999) = Nvl(mmtt.TRX_SOURCE_LINE_ID,-9999)
8312                     AND Nvl(l_demand_source_name, '@@@###@@#') = Nvl(mmtt.TRANSACTION_SOURCE_NAME,'@@@###@@#')
8313                     AND Nvl(l_demand_source_delivery,-9999) = Nvl(mmtt.TRX_SOURCE_DELIVERY_ID,-9999)
8314                    )
8315                )
8316        UNION ALL
8317    -- receiving side of transfers  lot in MTLT
8318    -- Bug 7658493, If wms task is in loaded status, consider allocation like pending transaction.
8319        SELECT
8320             Decode(mmtt.transaction_action_id
8321                , 3, mmtt.transfer_organization
8322                , mmtt.organization_id)   organization_id
8323           , mmtt.inventory_item_id       inventory_item_id
8324           , mmtt.revision                revision
8325           , mtlt.lot_number              lot_number
8326           , mmtt.transfer_subinventory   subinventory_code
8327           , mmtt.transfer_to_location    locator_id
8328      , round(Abs(decode(l_demand_source_line_id,
8329          NULL, mtlt.primary_quantity,
8330          Nvl(pjm_ueff_onhand.txn_quantity(
8331                 l_demand_source_line_id
8332             ,mmtt.transaction_temp_id
8333             ,mtlt.lot_number
8334                ,'Y'
8335             ,mmtt.inventory_item_id
8336             ,mmtt.organization_id
8337             ,mmtt.transaction_source_type_id
8338             ,mmtt.transaction_source_id
8339             ,mmtt.rcv_transaction_id
8340                ,sign(mmtt.primary_quantity)
8341                ),mtlt.primary_quantity)
8342       )),5)
8343      , round(Abs(decode(l_demand_source_line_id,
8344          NULL, mtlt.secondary_quantity,
8345          Nvl(pjm_ueff_onhand.txn_quantity(
8346                 l_demand_source_line_id
8347             ,mmtt.transaction_temp_id
8348             ,mtlt.lot_number
8349                ,'Y'
8350             ,mmtt.inventory_item_id
8351             ,mmtt.organization_id
8352             ,mmtt.transaction_source_type_id
8353             ,mmtt.transaction_source_id
8354             ,mmtt.rcv_transaction_id
8355                ,sign(mmtt.secondary_transaction_quantity)
8356                ),mtlt.secondary_quantity)
8357       )),5)
8358           , 1                            quantity_type
8359           , mmtt.transfer_cost_group_id  cost_group_id
8360           , NVL(mmtt.content_lpn_id,mmtt.transfer_lpn_id) lpn_id
8361           , to_number(NULL)                      transaction_action_id
8362           , to_char(NULL)                        transfer_subinventory_code
8363           , to_number(NULL)                      transfer_locator_id
8364        FROM
8365             mtl_material_transactions_temp mmtt
8366            ,mtl_transaction_lots_temp mtlt
8367        WHERE
8368              mmtt.posting_flag = 'Y'
8369           AND mmtt.lot_number IS NULL
8370           AND mmtt.transaction_temp_id = mtlt.transaction_temp_id
8371           AND Decode( Nvl(mmtt.transaction_status,0),
8372                        2, decode(nvl(mmtt.wms_task_status,-1), 4, 1, 2),
8373                        1 ) <> 2
8374                      AND (mmtt.transaction_action_id IN (2,3) -- Bug 9595634: Removed 28
8375                -- Bug 9595634: Added condition to match demand source if action = 28
8376                 OR (mmtt.transaction_action_id = 28
8377                     AND l_demand_source_header_id = mmtt.transaction_source_id
8378                     AND Nvl(l_demand_source_line_id, -9999) = Nvl(mmtt.TRX_SOURCE_LINE_ID,-9999)
8379                     AND Nvl(l_demand_source_name, '@@@###@@#') = Nvl(mmtt.TRANSACTION_SOURCE_NAME,'@@@###@@#')
8380                     AND Nvl(l_demand_source_delivery,-9999) = Nvl(mmtt.TRX_SOURCE_DELIVERY_ID,-9999)
8381                    )
8382                )
8383       ) x
8384       WHERE x.organization_id    = l_organization_id
8385         AND x.inventory_item_id  = l_inventory_item_id
8386       GROUP BY
8387            x.organization_id, x.inventory_item_id, x.revision
8388           , x.lot_number,x.subinventory_code, x.locator_id
8389           , x.quantity_type, x.cost_group_id, x.lpn_id
8390           , x.transaction_action_id, x.transfer_subinventory_code
8391           , x.transfer_locator_id
8392    ) x
8393     , mtl_secondary_inventories sub
8394     , mtl_lot_numbers lot
8395  WHERE
8396    x.organization_id    = sub.organization_id          (+)
8397    AND x.subinventory_code = sub.secondary_inventory_name (+)
8398    AND x.organization_id   = lot.organization_id   (+)
8399    AND x.inventory_item_id = lot.inventory_item_id (+)
8400    AND x.lot_number        = lot.lot_number        (+)
8401    AND l_grade_code = lot.grade_code
8402    AND (l_asset_subs_only = 2 OR
8403          NVL(sub.asset_inventory,1) = 1)
8404    AND ((l_onhand_source = 1 AND
8405       Nvl(sub.inventory_atp_code, 1) = 1
8406     ) OR
8407         (l_onhand_source = 2 AND
8408        Nvl(sub.availability_type, 1) = 1
8409     ) OR
8410     l_onhand_source =3
8411       OR
8412     (l_onhand_source = 4 AND
8413       Nvl(sub.inventory_atp_code, 1) = 1 AND
8414                 Nvl(sub.availability_type, 1) = 1
8415     )
8416       )
8417    ;
8418 
8419 BEGIN
8420    IF g_debug = 1 THEN
8421       print_debug('  ' || l_api_name || ' Entered',9);
8422    END IF;
8423 
8424    -- invConv change (for display)
8425    print_debug('odab entering build_tree grade_code='||g_rootinfos(p_tree_id).grade_code);
8426    if (g_is_lot_control = TRUE)
8427    then
8428               print_debug('In build_tree.   g_is_lot_control=TRUE');
8429    else
8430               print_debug('In build_tree.   g_is_lot_control=FALSE');
8431    end if;
8432 
8433    zero_tree_node(l_return_status, g_rootinfos(p_tree_id).item_node_index);
8434    IF l_return_status = fnd_api.g_ret_sts_error THEN
8435       RAISE fnd_api.g_exc_error;
8436    End IF ;
8437 
8438    IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8439       RAISE fnd_api.g_exc_unexpected_error;
8440    End IF;
8441 
8442    --bug 2668448
8443    --quantity tree returned wrong values if the tree is rebuilt
8444    --but the information stored in g_rsv_info is not deleted.
8445    --This resulted in the tree displaying too little quantity being available.
8446    IF g_rsv_tree_id <> 0 AND
8447       g_demand_info.exists(g_rsv_tree_id) THEN
8448 
8449       If g_demand_info(g_rsv_tree_id).root_id = p_tree_id Then
8450          g_rsv_tree_id := 0;
8451          g_rsv_counter := 0;
8452          g_rsv_info.DELETE;
8453       End If;
8454    END IF;
8455 
8456 
8457    l_organization_id          := g_rootinfos(p_tree_id).organization_id;
8458    l_inventory_item_id        := g_rootinfos(p_tree_id).inventory_item_id;
8459    --Bug 10145998: Initialize l_demand.. variables from g_demand_info if not found from g_rootinfos.
8460    --              To fix pick release for lot indivisible items (MMTT receipt part should be considered for this)
8461    l_demand_source_type_id    := Nvl(g_rootinfos(p_tree_id).demand_source_type_id, g_demand_info(g_demand_counter).demand_source_type_id);
8462    l_demand_source_header_id  := Nvl(g_rootinfos(p_tree_id).demand_source_header_id, g_demand_info(g_demand_counter).demand_source_header_id);
8463    l_demand_source_line_id    := Nvl(g_rootinfos(p_tree_id).demand_source_line_id, g_demand_info(g_demand_counter).demand_source_line_id);
8464    l_demand_source_name       := Nvl(g_rootinfos(p_tree_id).demand_source_name, g_demand_info(g_demand_counter).demand_source_name);
8465    l_demand_source_delivery   := Nvl(g_rootinfos(p_tree_id).demand_source_delivery, g_demand_info(g_demand_counter).demand_source_delivery);
8466    l_tree_mode                := g_rootinfos(p_tree_id).tree_mode;
8467    l_lot_expiration_date      := g_rootinfos(p_tree_id).lot_expiration_date;
8468    l_onhand_source            := g_rootinfos(p_tree_id).onhand_source;
8469    l_grade_code               := g_rootinfos(p_tree_id).grade_code;
8470 
8471    IF g_rootinfos(p_tree_id).is_revision_control THEN
8472       l_revision_control := 1;
8473    ELSE
8474       l_revision_control := 2;
8475    END IF;
8476    IF g_rootinfos(p_tree_id).asset_sub_only THEN
8477       l_asset_subs_only := 1;
8478    ELSE
8479       l_asset_subs_only := 2;
8480    END IF;
8481    IF l_tree_mode = g_no_lpn_rsvs_mode THEN
8482       l_no_lpn_reservations := 1;
8483    ELSE
8484       l_no_lpn_reservations := 2;
8485    END IF;
8486 
8487    print_debug('build_tree: l_revision_control='||l_revision_control);
8488    print_debug('build_tree: l_asset_subs_only='||l_asset_subs_only);
8489    print_debug('build_tree: l_no_lpn_reservations='||l_no_lpn_reservations);
8490    print_debug('build_tree: l_onhand_source='||l_onhand_source);
8491    print_debug('build_tree: l_grade_code='||l_grade_code);
8492    print_debug('build_tree: l_lot_expiration_date='||l_lot_expiration_date);
8493 
8494    -- invConv changes begin : Material Status CTL = need to build the QT with lotCTL
8495    --     Even in the Header Misc UI, BUT the tree is created as prior Convergence :
8496    --     without lot_number (but using the c_lot% cursors)
8497    -- IF g_rootinfos(p_tree_id).is_lot_control THEN
8498    IF g_is_lot_control OR g_rootinfos(p_tree_id).is_lot_control
8499    THEN
8500 
8501       l_lot_expiration_control := 1;
8502       IF g_rootinfos(p_tree_id).unit_effective = 1 THEN
8503          --item is lot controlled and unit effective
8504          IF l_grade_code IS NULL
8505          THEN
8506             print_debug('build_tree: open c_lot_unit');
8507             OPEN c_lot_unit;
8508             FETCH c_lot_unit BULK COLLECT INTO
8509                    v_organization_id
8510                   ,v_inventory_item_id
8511                   ,v_revision
8512                   ,v_lot_number
8513                   ,v_lot_expiration_date
8514                   ,v_subinventory_code
8515                   ,v_reservable_type
8516                   ,v_locator_id
8517                   ,v_primary_quantity
8518                   ,v_secondary_quantity
8519                   ,v_quantity_type
8520                   ,v_cost_group_id
8521                   ,v_lpn_id
8522                   ,v_transaction_action_id
8523                   ,v_transfer_subinventory_code
8524                   ,v_transfer_locator_id
8525                   ,v_is_reservable_lot    --Bug#8713821
8526             ;
8527             CLOSE c_lot_unit;
8528             -- invConv changes begin : adding grade where clause in a separate cursor
8529          ELSE
8530             print_debug('build_tree: open c_lot_unit_grade');
8531             OPEN c_lot_unit_grade;
8532             FETCH c_lot_unit_grade BULK COLLECT INTO
8533                     v_organization_id
8534                    ,v_inventory_item_id
8535                    ,v_revision
8536                    ,v_lot_number
8537                    ,v_lot_expiration_date
8538                    ,v_subinventory_code
8539                    ,v_reservable_type
8540                    ,v_locator_id
8541                    ,v_primary_quantity
8542                    ,v_secondary_quantity
8543                    ,v_quantity_type
8544                    ,v_cost_group_id
8545                    ,v_lpn_id
8546                    ,v_transaction_action_id
8547                    ,v_transfer_subinventory_code
8548                    ,v_transfer_locator_id
8549                    ,v_is_reservable_lot    --Bug#8713821
8550                ;
8551             CLOSE c_lot_unit_grade;
8552          END IF;
8553 
8554       ELSE
8555          --item is lot controlled
8556          IF l_grade_code IS NULL
8557          THEN
8558             IF (g_is_lot_control = FALSE)
8559             THEN
8560                print_debug('build_tree: open c_lot');
8561                OPEN c_lot;
8562                FETCH c_lot BULK COLLECT INTO
8563                       v_organization_id
8564                      ,v_inventory_item_id
8565                      ,v_revision
8566                      ,v_lot_number
8567                      ,v_lot_expiration_date
8568                      ,v_subinventory_code
8569                      ,v_reservable_type
8570                      ,v_locator_id
8571                      ,v_primary_quantity
8572                      ,v_secondary_quantity
8573                      ,v_quantity_type
8574                      ,v_cost_group_id
8575                      ,v_lpn_id
8576                      ,v_transaction_action_id
8577                      ,v_transfer_subinventory_code
8578                      ,v_transfer_locator_id
8579                      ,v_is_reservable_lot    --Bug#8713821
8580                ;
8581                CLOSE c_lot;
8582             ELSE
8583                print_debug('build_tree: open c_lot_MMS');
8584                OPEN c_lot_MMS;
8585                FETCH c_lot_MMS BULK COLLECT INTO
8586                       v_organization_id
8587                      ,v_inventory_item_id
8588                      ,v_revision
8589                      ,v_lot_number
8590                      ,v_lot_expiration_date
8591                      ,v_subinventory_code
8592                      ,v_reservable_type
8593                      ,v_locator_id
8594                      ,v_primary_quantity
8595                      ,v_secondary_quantity
8596                      ,v_quantity_type
8597                      ,v_cost_group_id
8598                      ,v_lpn_id
8599                      ,v_transaction_action_id
8600                      ,v_transfer_subinventory_code
8601                      ,v_transfer_locator_id
8602                      ,v_status_id            -- Onhand Material Status Support
8603                      ,v_is_reservable_lot    --Bug#8713821
8604                ;
8605                CLOSE c_lot_MMS;
8606             END IF;     -- (g_is_lot_control = FALSE)
8607             -- invConv changes begin : adding grade where clause in a separate cursor
8608          ELSE
8609             IF (g_is_lot_control = FALSE)
8610             THEN
8611                print_debug('build_tree: open c_lot_grade');
8612                OPEN c_lot_grade;
8613                FETCH c_lot_grade BULK COLLECT INTO
8614                       v_organization_id
8615                      ,v_inventory_item_id
8616                      ,v_revision
8617                      ,v_lot_number
8618                      ,v_lot_expiration_date
8619                      ,v_subinventory_code
8620                      ,v_reservable_type
8621                      ,v_locator_id
8622                      ,v_primary_quantity
8623                      ,v_secondary_quantity
8624                      ,v_quantity_type
8625                      ,v_cost_group_id
8626                      ,v_lpn_id
8627                      ,v_transaction_action_id
8628                      ,v_transfer_subinventory_code
8629                      ,v_transfer_locator_id
8630                      ,v_is_reservable_lot    --Bug#8713821
8631                ;
8632                CLOSE c_lot_grade;
8633             ELSE
8634                print_debug('build_tree: open c_lot_grade_MMS');
8635                OPEN c_lot_grade_MMS;
8636                FETCH c_lot_grade_MMS BULK COLLECT INTO
8637                       v_organization_id
8638                      ,v_inventory_item_id
8639                      ,v_revision
8640                      ,v_lot_number
8641                      ,v_lot_expiration_date
8642                      ,v_subinventory_code
8643                      ,v_reservable_type
8644                      ,v_locator_id
8645                      ,v_primary_quantity
8646                      ,v_secondary_quantity
8647                      ,v_quantity_type
8648                      ,v_cost_group_id
8649                      ,v_lpn_id
8650                      ,v_transaction_action_id
8651                      ,v_transfer_subinventory_code
8652                      ,v_transfer_locator_id
8653                      ,v_status_id            -- Onhand Material Status Support
8654                      ,v_is_reservable_lot    --Bug#8713821
8655                ;
8656                CLOSE c_lot_grade_MMS;
8657             END IF;    -- (g_is_lot_control = FALSE)
8658          END IF;  -- l_grade_code
8659 
8660       END IF;
8661    ELSE
8662    --Bug7628989
8663         SELECT lot_control_code
8664         INTO   lot_control_code
8665         FROM   mtl_system_items_b
8666         WHERE inventory_item_id = l_inventory_item_id
8667         AND   organization_id = l_organization_id;
8668 
8669         IF lot_control_code = 2 THEN
8670            l_lot_expiration_control := 1;
8671         ELSE
8672           l_lot_expiration_control := 2;
8673           END IF;
8674 
8675       IF g_rootinfos(p_tree_id).unit_effective = 1 THEN
8676          IF l_lot_expiration_control = 1 THEN
8677          print_debug('build_tree: open c_unit_no_lot');
8678          --item is unit effective
8679          OPEN c_unit_no_lot;
8680          FETCH c_unit_no_lot BULK COLLECT INTO
8681                    v_organization_id
8682                   ,v_inventory_item_id
8683                   ,v_revision
8684                   ,v_lot_number
8685                   ,v_lot_expiration_date
8686                   ,v_subinventory_code
8687                   ,v_reservable_type
8688                   ,v_locator_id
8689                   ,v_primary_quantity
8690                   ,v_secondary_quantity
8691                   ,v_quantity_type
8692                   ,v_cost_group_id
8693                   ,v_lpn_id
8694                   ,v_transaction_action_id
8695                   ,v_transfer_subinventory_code
8696                   ,v_transfer_locator_id
8697                   , v_is_reservable_lot    --Bug#8713821
8698          ;
8699          CLOSE c_unit_no_lot;
8700          ELSE
8701                    --item is unit effective
8702                    OPEN c_unit;
8703                    FETCH c_unit BULK COLLECT INTO
8704              v_organization_id
8705             ,v_inventory_item_id
8706             ,v_revision
8707             ,v_lot_number
8708             ,v_lot_expiration_date
8709             ,v_subinventory_code
8710             ,v_reservable_type
8711             ,v_locator_id
8712             ,v_primary_quantity
8713             ,v_secondary_quantity
8714             ,v_quantity_type
8715             ,v_cost_group_id
8716             ,v_lpn_id
8717             ,v_transaction_action_id
8718             ,v_transfer_subinventory_code
8719             ,v_transfer_locator_id
8720             ,v_is_reservable_lot    --Bug#8713821
8721                 ;
8722                    CLOSE c_unit;
8723          END IF;
8724       ELSE
8725          IF l_lot_expiration_control = 1 THEN
8726              OPEN c_no_lot;
8727              print_debug('  Opening Cursor c_no_lot');
8728              FETCH c_no_lot BULK COLLECT INTO
8729                 v_organization_id
8730                ,v_inventory_item_id
8731                ,v_revision
8732                ,v_lot_number
8733                ,v_lot_expiration_date
8734                ,v_subinventory_code
8735                ,v_reservable_type
8736                ,v_locator_id
8737                ,v_primary_quantity
8738                ,v_secondary_quantity
8739                ,v_quantity_type
8740                ,v_cost_group_id
8741                ,v_lpn_id
8742                ,v_transaction_action_id
8743                ,v_transfer_subinventory_code
8744                ,v_transfer_locator_id
8745                ,v_is_reservable_lot    --Bug#8713821
8746               ;
8747              CLOSE c_no_lot;
8748          ELSE
8749          IF (g_is_lot_control = FALSE)
8750            THEN
8751               IF (inv_quantity_tree_pvt.g_is_mat_status_used = 2) /* Bug 7158174 */
8752               THEN
8753                  print_debug('build_tree: open c_plain');
8754                  --item is not lot controlled or unit effective
8755                  OPEN c_plain;
8756                  FETCH c_plain BULK COLLECT INTO
8757                    v_organization_id
8758                   ,v_inventory_item_id
8759                   ,v_revision
8760                   ,v_lot_number
8761                   ,v_lot_expiration_date
8762                   ,v_subinventory_code
8763                   ,v_reservable_type
8764                   ,v_locator_id
8765                   ,v_primary_quantity
8766                   ,v_secondary_quantity
8767                   ,v_quantity_type
8768                   ,v_cost_group_id
8769                   ,v_lpn_id
8770                   ,v_transaction_action_id
8771                   ,v_transfer_subinventory_code
8772                   ,v_transfer_locator_id
8773                   ,v_is_reservable_lot    --Bug#8713821
8774                   ;
8775                   CLOSE c_plain;
8776              ELSE
8777                   print_debug('build_tree: open c_plain_MMS');
8778             --item is not lot controlled or unit effective
8779             OPEN c_plain_MMS;
8780             FETCH c_plain_MMS BULK COLLECT INTO
8781                    v_organization_id
8782                   ,v_inventory_item_id
8783                   ,v_revision
8784                   ,v_lot_number
8785                   ,v_lot_expiration_date
8786                   ,v_subinventory_code
8787                   ,v_reservable_type
8788                   ,v_locator_id
8789                   ,v_primary_quantity
8790                   ,v_secondary_quantity
8791                   ,v_quantity_type
8792                   ,v_cost_group_id
8793                   ,v_lpn_id
8794                   ,v_transaction_action_id
8795                   ,v_transfer_subinventory_code
8796                   ,v_transfer_locator_id
8797                   ,v_status_id            -- Onhand Material Status Support
8798                   ,v_is_reservable_lot    --Bug#8713821
8799             ;
8800             CLOSE c_plain_MMS;
8801             END IF;
8802          END IF;    -- (g_is_lot_control = FALSE)
8803       END IF;
8804    END IF;
8805 END IF;
8806    print_debug('build_tree: l_lot_expiration_control='||l_lot_expiration_control);
8807 
8808    --WHILE (l_index < l_tot_num_rows) LOOP
8809    l_index := v_organization_id.FIRST;
8810    LOOP
8811       print_debug('build_tree: loop index='||l_index||'.');
8812       EXIT WHEN l_index IS NULL;
8813       IF (l_tree_mode = g_loose_only_mode
8814           AND v_quantity_type(l_index) = g_qr_same_demand) THEN
8815          -- the record should not be used to build the tree
8816          -- as it is a reservation for the same demand
8817          NULL;
8818          print_debug('... QT not build because mode=looose_only and qty_type=qr_same_demand...');
8819       ELSE
8820          IF v_reservable_type(l_index) IS NOT NULL THEN
8821             IF v_reservable_type(l_index) = 1 THEN
8822                l_is_reservable_sub := TRUE;
8823              ELSIF v_reservable_type(l_index) = 2 THEN
8824                l_is_reservable_sub := FALSE;
8825              ELSE
8826                l_is_reservable_sub := NULL;
8827             END IF;
8828          ELSE
8829             l_is_reservable_sub := NULL;
8830          END IF;
8831 
8832          --bug 9380420, now we need not call check_is_reservable from here, as logic is shifted to new_tree_node.
8833          --             It will get called from add_quantities -> find_tree_node -> find_child_node.
8834          /*
8835          print_debug('org='||l_organization_id||', item='||l_inventory_item_id||', sub='||v_subinventory_code(l_index)||', loc='||v_locator_id(l_index)||', lot='||substr(v_lot_number(l_index), 1, 10)||', priqty='||v_primary_quantity(l_index));
8836          check_is_reservable
8837               ( x_return_status     => l_return_status
8838               , p_inventory_item_id => l_inventory_item_id
8839               , p_organization_id   => l_organization_id
8840               , p_subinventory_code => v_subinventory_code(l_index)
8841               , p_locator_id        => v_locator_id(l_index)
8842               , p_lot_number        => v_lot_number(l_index)
8843               , p_root_id           => p_tree_id
8844               , x_is_reservable     => l_is_reservable_sub
8845               , p_lpn_id            => v_lpn_id(l_index)); -- Onhand Material Status Support
8846 
8847          if l_is_reservable_sub then
8848              print_debug('after check_is_reservable, rsv=TRUE');
8849          else
8850              print_debug('after check_is_reservable, rsv=FALSE');
8851          end if;
8852          */
8853 
8854          -- invConv changes begin : Only with mat_stat CTL, Create the tree with lotCTL FALSE :
8855          if g_is_lot_control AND g_rootinfos(p_tree_id).is_lot_control = FALSE
8856          then
8857             print_debug(' Calling add_quantities  for summary node ...');
8858             --bug 9380420, passing NULL instead of l_is_reservable_sub to p_is_reservable_sub.
8859             add_quantities
8860               (
8861                  x_return_status     => l_return_status
8862                , p_tree_id           => p_tree_id
8863                , p_revision          => v_revision(l_index)
8864                , p_lot_number        => NULL
8865                , p_subinventory_code => v_subinventory_code(l_index)
8866                , p_is_reservable_sub => NULL
8867                , p_locator_id        => v_locator_id(l_index)
8868                , p_primary_quantity  => v_primary_quantity(l_index)
8869                , p_secondary_quantity=> v_secondary_quantity(l_index)
8870                , p_quantity_type     => v_quantity_type (l_index)
8871                , p_set_check_mark    => FALSE
8872                , p_cost_group_id     => v_cost_group_id(l_index)
8873                , p_lpn_id         => v_lpn_id(l_index)
8874                --Bug 4294336
8875                , p_transaction_action_id => v_transaction_action_id(l_index)
8876                , p_transfer_subinventory_code => v_transfer_subinventory_code(l_index)
8877                , p_transfer_locator_id   =>   v_transfer_locator_id(l_index)
8878                , p_expiration_date   => v_lot_expiration_date(l_index) -- Bug 7628989
8879                , p_is_reservable_lot   => v_is_reservable_lot(l_index) --Bug#8713821
8880                  );
8881 
8882             IF l_return_status = fnd_api.g_ret_sts_error THEN
8883                RAISE fnd_api.g_exc_error;
8884             End IF ;
8885 
8886             IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8887                RAISE fnd_api.g_exc_unexpected_error;
8888             End IF;
8889          ELSE
8890             print_debug(' Calling add_quantities for qty1='||v_primary_quantity(l_index)||', qty2='||v_secondary_quantity(l_index)||'...');
8891             --bug 9380420, passing NULL instead of l_is_reservable_sub to p_is_reservable_sub.
8892             add_quantities
8893               (
8894                  x_return_status     => l_return_status
8895                , p_tree_id           => p_tree_id
8896                , p_revision          => v_revision(l_index)
8897                , p_lot_number        => v_lot_number(l_index)
8898                , p_subinventory_code => v_subinventory_code(l_index)
8899                , p_is_reservable_sub => NULL
8900                , p_locator_id        => v_locator_id(l_index)
8901                , p_primary_quantity  => v_primary_quantity(l_index)
8902                , p_secondary_quantity=> v_secondary_quantity(l_index)
8903                , p_quantity_type     => v_quantity_type (l_index)
8904                , p_set_check_mark    => FALSE
8905                , p_cost_group_id     => v_cost_group_id(l_index)
8906                , p_lpn_id            => v_lpn_id(l_index)
8907                --Bug 4294336
8908                , p_transaction_action_id => v_transaction_action_id(l_index)
8909                , p_transfer_subinventory_code => v_transfer_subinventory_code(l_index)
8910                , p_transfer_locator_id   =>   v_transfer_locator_id(l_index)
8911                , p_expiration_date   => v_lot_expiration_date(l_index) -- Bug 7628989
8912                , p_is_reservable_lot   => v_is_reservable_lot(l_index) --Bug#8713821
8913                  );
8914 
8915             IF l_return_status = fnd_api.g_ret_sts_error THEN
8916                RAISE fnd_api.g_exc_error;
8917             End IF ;
8918 
8919             IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
8920                RAISE fnd_api.g_exc_unexpected_error;
8921             End IF;
8922             -- invConv changes begin : Closing test on g_is_lot_ctl
8923          END IF;
8924       END IF;
8925 
8926       EXIT WHEN l_index = v_organization_id.LAST;
8927       l_index := v_organization_id.NEXT(l_index);
8928    END LOOP;
8929 
8930    print_debug('... end of build_tree loop...');
8931 
8932    -- close cursor
8933    --dbms_sql.CLOSE_cursor(l_cursor);
8934    g_rootinfos(p_tree_id).need_refresh := FALSE;
8935    x_return_status := l_return_status;
8936 
8937    IF g_debug = 1 THEN
8938       print_debug('  '||l_api_name || ' Exited with status = '||l_return_status,9);
8939    END IF;
8940 
8941 EXCEPTION
8942    WHEN fnd_api.g_exc_error THEN
8943         print_debug('build_tree: exc_err='||sqlerrm,9);
8944         x_return_status := fnd_api.g_ret_sts_error;
8945 
8946    WHEN fnd_api.g_exc_unexpected_error THEN
8947         print_debug('build_tree: unexp_err='||sqlerrm,9);
8948         x_return_status := fnd_api.g_ret_sts_unexp_error ;
8949 
8950     WHEN OTHERS THEN
8951         print_debug('build_tree: others='||sqlerrm,9);
8952         x_return_status := fnd_api.g_ret_sts_unexp_error ;
8953 
8954         IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
8955           THEN
8956            fnd_msg_pub.add_exc_msg
8957              (  g_pkg_name
8958               , 'Build_Tree'
8959               );
8960         END IF;
8961 
8962 END build_tree;
8963 
8964 -- Function
8965 --   check_node_violation
8966 -- Description
8967 --   return false if there is violation, true otherwise
8968 FUNCTION check_node_violation
8969   (  x_return_status      OUT NOCOPY VARCHAR2
8970    , p_node_index         IN  INTEGER
8971    , p_tree_mode          IN  INTEGER
8972    , p_negative_allowed   IN  BOOLEAN
8973    , p_item_node_index    IN  INTEGER
8974    ) RETURN BOOLEAN
8975   IS
8976    l_return_status        VARCHAR2(1) := fnd_api.g_ret_sts_success;
8977    l_child_index          INTEGER;
8978    l_no_violation         BOOLEAN;
8979    l_node_index           INTEGER;
8980 BEGIN
8981    print_debug('Entering check_node_violation...');
8982 
8983    -- check this node
8984    print_debug('mode='||p_tree_mode||', att='||g_nodes(p_node_index).att||', qr='||g_nodes(p_item_node_index).qr||' AND atr='||g_nodes(p_item_node_index).atr);
8985    print_debug('Secondaries: satt='||g_nodes(p_node_index).satt||', sqr='||g_nodes(p_item_node_index).sqr||' AND satr='||g_nodes(p_item_node_index).satr);
8986    IF p_tree_mode IN (g_transaction_mode, g_loose_only_mode)
8987      AND g_nodes(p_node_index).check_mark
8988      AND g_nodes(p_node_index).att < 0
8989      AND (p_negative_allowed = FALSE
8990      OR (g_nodes(p_item_node_index).qr > 0 AND g_nodes(p_item_node_index).atr < 0 )) THEN
8991       --OR g_nodes(p_item_node_index).atr < 0 ) THEN
8992       --Bug 3383756 fix, redoing bug 3078863 fix made in 115.121, but
8993       --missing in this version
8994       print_debug('... return=FALSE, with status='||l_return_status||' ... because :');
8995       print_debug('mode='||p_tree_mode||', check_mark=TRUE, att='||g_nodes(p_node_index).att||', (negative_allowed=FALSE OR {qr='||g_nodes(p_item_node_index).qr||' >0 AND atr='||g_nodes(p_item_node_index).atr||' <0}');
8996       RETURN FALSE;
8997    END IF;
8998 
8999    IF p_tree_mode = g_reservation_mode
9000      AND g_nodes(p_node_index).check_mark
9001      AND g_nodes(p_node_index).atr < 0 THEN
9002       print_debug('... return=FALSE, with status='||l_return_status||' ... because :');
9003       print_debug('mode='||p_tree_mode||', check_mark=TRUE, atr='||g_nodes(p_node_index).atr||' <0.');
9004       RETURN FALSE;
9005    END IF;
9006 
9007    -- check child nodes
9008    l_child_index := g_nodes(p_node_index).first_child_index;
9009    l_no_violation := TRUE;
9010    WHILE (l_child_index <> 0) LOOP
9011       l_no_violation := check_node_violation(
9012            x_return_status    => l_return_status
9013          , p_node_index       => l_child_index
9014          , p_tree_mode        => p_tree_mode
9015          , p_negative_allowed => p_negative_allowed
9016          , p_item_node_index  => p_item_node_index
9017          );
9018 
9019       IF l_no_violation = FALSE THEN
9020          print_debug('... exiting the loop with return=FALSE');
9021          EXIT;
9022       END IF;
9023 
9024       l_child_index := g_nodes(l_child_index).next_sibling_index;
9025    END LOOP;
9026 
9027    x_return_status := l_return_status;
9028 
9029    IF l_no_violation
9030    THEN
9031       print_debug('Normal end of check_node_violation. status='||l_return_status||', no_violation=TRUE');
9032    ELSE
9033       print_debug('Normal end of check_node_violation. status='||l_return_status||', no_violation=FALSE');
9034    END IF;
9035    RETURN l_no_violation;
9036 
9037 EXCEPTION
9038    WHEN fnd_api.g_exc_error THEN
9039         x_return_status := fnd_api.g_ret_sts_error;
9040 
9041    WHEN fnd_api.g_exc_unexpected_error THEN
9042         x_return_status := fnd_api.g_ret_sts_unexp_error ;
9043 
9044    WHEN OTHERS THEN
9045       x_return_status := fnd_api.g_ret_sts_unexp_error ;
9046 
9047       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
9048         THEN
9049          fnd_msg_pub.add_exc_msg
9050            (  g_pkg_name
9051               , 'Check_Node_Violation'
9052               );
9053       END IF;
9054       RETURN FALSE;
9055 
9056 END check_node_violation;
9057 
9058 --add_demand_qty
9059 -- When in transaction_mode, this procedure will fix
9060 -- the quantity tree to reflect the correct available quantities.
9061 -- To do this, it queries the mtl_reservations table to find
9062 -- reservations which correspond to the demand info.  For each
9063 -- reservation, it calls add_quantities to subtract those reservations
9064 -- from the quantity tree (since that quantity should be available
9065 -- for this transaction).  We call add_quantities with a negative
9066 -- quantity so that QR is decreased, and ATT is increased.
9067 -- We also add the reservation data to the rsv_info plsql table.
9068 -- Saving the data keeps us from having to query the table again.
9069 -- After this, querying the tree will
9070 -- reflect the actual ATT.
9071 
9072 -- Bug 4194323 While cosidering Reservations against demand information
9073 -- specified we should consider supply type reservations of Inventory only
9074 
9075 PROCEDURE add_demand_qty(
9076     x_return_status  OUT NOCOPY VARCHAR2
9077         ,p_tree_id     IN  INTEGER
9078  ) IS
9079 
9080   l_root_id    INTEGER;
9081   l_revision    VARCHAR2(3);
9082   l_lot_number  VARCHAR2(80);
9083   l_subinventory_code VARCHAR2(10);
9084   l_locator_id  NUMBER;
9085   l_lpn_id  NUMBER;
9086   l_quantity    NUMBER;
9087   l_quantity2    NUMBER;
9088   l_mult NUMBER := -1;
9089 
9090 
9091   --We need 6 possible queries to improve performance and make best
9092   -- use of the indices on mtl_reservations, while avoid dynamic sql.
9093 
9094   -- demand line is not NULL, not pick release
9095   -- Bug 4494038: exclude crossdock reservations
9096   CURSOR c_demand_dl IS
9097   SELECT   revision
9098          , lot_number
9099          , subinventory_code
9100          , locator_id
9101          , lpn_id
9102          , primary_reservation_quantity - NVL(detailed_quantity, 0)
9103          , NVL(secondary_reservation_quantity, 0) - NVL(secondary_detailed_quantity, 0)
9104   FROM mtl_reservations
9105   WHERE organization_id                      = g_rootinfos(l_root_id).organization_id
9106     AND inventory_item_id                    = g_rootinfos(l_root_id).inventory_item_id
9107     AND demand_source_type_id                = g_demand_info(p_tree_id).demand_source_type_id
9108     AND demand_source_header_id              = g_demand_info(p_tree_id).demand_source_header_id
9109     AND demand_source_line_id                = g_demand_info(p_tree_id).demand_source_line_id
9110     AND NVL(demand_source_name, '@@@###@@#') = NVL(g_demand_info(p_tree_id).demand_source_name, '@@@###@@#')
9111     AND NVL(demand_source_delivery, -99999)  = NVL(g_demand_info(p_tree_id).demand_source_delivery, -99999)
9112     AND demand_source_line_detail IS NULL
9113     AND nvl(supply_source_type_id,13) = 13 ;                -- Bug 4194323
9114 
9115   -- demand name is not null, not pick release
9116   -- Bug 4494038: exclude crossdock reservations
9117   CURSOR c_demand_dn IS
9118   SELECT  revision
9119          ,lot_number
9120          ,subinventory_code
9121          ,locator_id
9122          ,lpn_id
9123          ,primary_reservation_quantity - NVL(detailed_quantity, 0)
9124          ,NVL(secondary_reservation_quantity, 0) - NVL(secondary_detailed_quantity, 0)
9125    FROM mtl_reservations
9126   WHERE organization_id                      = g_rootinfos(l_root_id).organization_id
9127     AND inventory_item_id                    = g_rootinfos(l_root_id).inventory_item_id
9128     AND demand_source_type_id                = g_demand_info(p_tree_id).demand_source_type_id
9129     AND demand_source_header_id              = g_demand_info(p_tree_id).demand_source_header_id
9130     AND demand_source_name                   = g_demand_info(p_tree_id).demand_source_name
9131     AND demand_source_line_id IS NULL
9132     AND NVL(demand_source_delivery, -99999)  = NVL(g_demand_info(p_tree_id).demand_source_delivery, -99999)
9133     AND demand_source_line_detail IS NULL
9134     AND nvl(supply_source_type_id,13) = 13 ;                -- Bug 4194323
9135 
9136   -- query based on org and item
9137   -- Bug 4494038: exclude crossdock reservations
9138   CURSOR c_demand IS
9139   SELECT  revision
9140          ,lot_number
9141          ,subinventory_code
9142          ,locator_id
9143          ,lpn_id
9144          ,primary_reservation_quantity - NVL(detailed_quantity, 0)
9145          ,NVL(secondary_reservation_quantity, 0) - NVL(secondary_detailed_quantity, 0)
9146    FROM mtl_reservations
9147   WHERE organization_id                      = g_rootinfos(l_root_id).organization_id
9148     AND inventory_item_id                    = g_rootinfos(l_root_id).inventory_item_id
9149     AND demand_source_type_id                = g_demand_info(p_tree_id).demand_source_type_id
9150     AND demand_source_header_id              = g_demand_info(p_tree_id).demand_source_header_id
9151     AND demand_source_line_id IS NULL
9152     AND demand_source_name IS NULL
9153     AND NVL(demand_source_delivery, -99999)  = NVL(g_demand_info(p_tree_id).demand_source_delivery, -99999)
9154     AND demand_source_line_detail IS NULL
9155     AND nvl(supply_source_type_id,13) = 13 ;                -- Bug 4194323
9156 
9157     -- pick release, query based on demand_source_line_id
9158   -- Bug 4494038: exclude crossdock reservations
9159   CURSOR c_demand_stage_dl IS
9160   SELECT  revision
9161          ,lot_number
9162          ,subinventory_code
9163          ,locator_id
9164          ,lpn_id
9165          ,primary_reservation_quantity - NVL(detailed_quantity, 0)
9166          ,NVL(secondary_reservation_quantity, 0) - NVL(secondary_detailed_quantity, 0)
9167    FROM mtl_reservations
9168    WHERE demand_source_line_id               = g_demand_info(p_tree_id).demand_source_line_id
9169     AND NVL(organization_id, 0)              = g_rootinfos(l_root_id).organization_id
9170     AND NVL(inventory_item_id,0)             = g_rootinfos(l_root_id).inventory_item_id
9171     AND demand_source_type_id                = g_demand_info(p_tree_id).demand_source_type_id
9172     AND demand_source_header_id              = g_demand_info(p_tree_id).demand_source_header_id
9173     AND NVL(demand_source_name, '@@@###@@#') = NVL(g_demand_info(p_tree_id).demand_source_name, '@@@###@@#')
9174     AND NVL(demand_source_delivery, -99999)  = NVL(g_demand_info(p_tree_id).demand_source_delivery, -99999)
9175     AND NVL(staged_flag, 'N') = 'N'
9176     AND demand_source_line_detail IS NULL
9177     AND nvl(supply_source_type_id,13) = 13 ;                -- Bug 4194323
9178 
9179   -- pick release, query based on demand_source_name
9180   -- Bug 4494038: exclude crossdock reservations
9181   CURSOR c_demand_stage_dn IS
9182   SELECT  revision
9183          ,lot_number
9184          ,subinventory_code
9185          ,locator_id
9186          ,lpn_id
9187          ,primary_reservation_quantity - NVL(detailed_quantity, 0)
9188          ,NVL(secondary_reservation_quantity, 0) - NVL(secondary_detailed_quantity, 0)
9189    FROM mtl_reservations
9190   WHERE organization_id                      = g_rootinfos(l_root_id).organization_id
9191     AND inventory_item_id                    = g_rootinfos(l_root_id).inventory_item_id
9192     AND demand_source_type_id                = g_demand_info(p_tree_id).demand_source_type_id
9193     AND demand_source_header_id              = g_demand_info(p_tree_id).demand_source_header_id
9194     AND demand_source_name                   = g_demand_info(p_tree_id).demand_source_name
9195     AND demand_source_line_id IS NULL
9196     AND NVL(demand_source_delivery, -99999)  = NVL(g_demand_info(p_tree_id).demand_source_delivery, -99999)
9197     AND NVL(staged_flag, 'N') = 'N'
9198     AND demand_source_line_detail IS NULL
9199     AND nvl(supply_source_type_id,13) = 13 ;                -- Bug 4194323
9200 
9201   -- pick release, query based on org and item
9202   -- Bug 4494038: exclude crossdock reservations
9203   CURSOR c_demand_stage IS
9204   SELECT  revision
9205          ,lot_number
9206          ,subinventory_code
9207          ,locator_id
9208          ,lpn_id
9209          ,primary_reservation_quantity - NVL(detailed_quantity, 0)
9210          ,NVL(secondary_reservation_quantity, 0) - NVL(secondary_detailed_quantity, 0)
9211    FROM mtl_reservations
9212   WHERE organization_id                      = g_rootinfos(l_root_id).organization_id
9213     AND inventory_item_id                    = g_rootinfos(l_root_id).inventory_item_id
9214     AND demand_source_type_id                = g_demand_info(p_tree_id).demand_source_type_id
9215     AND demand_source_header_id              = g_demand_info(p_tree_id).demand_source_header_id
9216     AND demand_source_line_id IS NULL
9217     AND demand_source_name = NULL
9218     AND NVL(demand_source_delivery, -99999)  = NVL(g_demand_info(p_tree_id).demand_source_delivery, -99999)
9219     AND NVL(staged_flag, 'N') = 'N'
9220     AND demand_source_line_detail IS NULL
9221     AND nvl(supply_source_type_id,13) = 13 ;                -- Bug 4194323
9222   is_lot_control Boolean :=false; -- Bug 4194323
9223 BEGIN
9224 
9225    x_return_status := fnd_api.g_ret_sts_success;
9226    g_rsv_info.DELETE;
9227    g_rsv_counter := 0;
9228    l_root_id := g_demand_info(p_tree_id).root_id;
9229    is_lot_control:=g_rootinfos(l_root_id).is_lot_control;
9230    print_debug('in add_demand_qty, tree='||p_tree_id||', root='||l_root_id||', pick_release:'||g_demand_info(p_tree_id).pick_release||' ?= pick_release_yes:'||g_pick_release_yes);
9231    print_debug('demand_source__line_id='||g_demand_info(p_tree_id).demand_source_line_id);
9232    IF g_demand_info(p_tree_id).pick_release = g_pick_release_yes THEN
9233 
9234       -- have to make sure we don't picking from staging location
9235       if g_demand_info(p_tree_id).demand_source_line_id IS NOT NULL THEN
9236          OPEN c_demand_stage_dl;
9237          LOOP
9238             FETCH c_demand_stage_dl INTO
9239                   l_revision
9240                  ,l_lot_number
9241                  ,l_subinventory_code
9242                  ,l_locator_id
9243                  ,l_lpn_id
9244                  ,l_quantity
9245                  ,l_quantity2;
9246 
9247             EXIT WHEN c_demand_stage_dl%NOTFOUND;
9248 
9249             print_debug('in add_demand_qty, calling add_quantities, lot='||substr(l_lot_number,1,10)||', subinv='||l_subinventory_code||', loct_id='||l_locator_id||', qty1='||(l_mult * l_quantity)||', qty2='||(l_mult * l_quantity2)||'.');
9250             add_quantities(
9251                  x_return_status     => x_return_status
9252                , p_tree_id           => l_root_id
9253                , p_revision          => l_revision
9254                , p_lot_number        => l_lot_number
9255                , p_subinventory_code => l_subinventory_code
9256                , p_is_reservable_sub => TRUE
9257                , p_locator_id        => l_locator_id
9258                , p_primary_quantity  => l_mult * l_quantity
9259                , p_secondary_quantity=> l_mult * l_quantity2
9260                , p_quantity_type     => g_qr_other_demand
9261                , p_set_check_mark    => FALSE
9262                , p_cost_group_id     => NULL
9263                , p_lpn_id            => l_lpn_id
9264                  );
9265 
9266             print_debug('... after add_quantities... x_return_status='||x_return_status||', qty='||l_quantity||', qty2='||l_quantity2);
9267             IF x_return_status = fnd_api.g_ret_sts_error THEN
9268                RAISE fnd_api.g_exc_error;
9269             ELSIF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
9270                RAISE fnd_api.g_exc_unexpected_error;
9271             END IF;
9272 
9273             g_rsv_counter := g_rsv_counter + 1;
9274             g_rsv_info(g_rsv_counter).revision := l_revision;
9275             g_rsv_info(g_rsv_counter).lot_number := l_lot_number;
9276             g_rsv_info(g_rsv_counter).subinventory_code := l_subinventory_code;
9277             g_rsv_info(g_rsv_counter).locator_id := l_locator_id;
9278             g_rsv_info(g_rsv_counter).lpn_id := l_lpn_id;
9279             g_rsv_info(g_rsv_counter).quantity := l_quantity;
9280             g_rsv_info(g_rsv_counter).secondary_quantity := l_quantity2;
9281          END LOOP;
9282          CLOSE c_demand_stage_dl;
9283       ELSIF g_demand_info(p_tree_id).demand_source_name IS NOT NULL then
9284          OPEN c_demand_stage_dn;
9285          LOOP
9286             FETCH c_demand_stage_dn INTO
9287                   l_revision
9288                  ,l_lot_number
9289                  ,l_subinventory_code
9290                  ,l_locator_id
9291                  ,l_lpn_id
9292                  ,l_quantity
9293                  ,l_quantity2;
9294 
9295             EXIT WHEN c_demand_stage_dn%NOTFOUND;
9296 
9297             print_debug('in add_demand_qty, calling add_quantities 2, lot='||substr(l_lot_number,1,10)||', subinv='||l_subinventory_code||', loct_id='||l_locator_id||', qty1='||(l_mult * l_quantity)||', qty2='||(l_mult * l_quantity2)||'.');
9298             add_quantities(
9299                  x_return_status     => x_return_status
9300                , p_tree_id           => l_root_id
9301                , p_revision          => l_revision
9302                , p_lot_number        => l_lot_number
9303                , p_subinventory_code => l_subinventory_code
9304                , p_is_reservable_sub => TRUE
9305                , p_locator_id        => l_locator_id
9306                , p_primary_quantity  => l_mult * l_quantity
9307                , p_secondary_quantity=> l_mult * l_quantity2
9308                , p_quantity_type     => g_qr_other_demand
9309                , p_set_check_mark    => FALSE
9310                , p_cost_group_id     => NULL
9311                , p_lpn_id      => l_lpn_id
9312                  );
9313 
9314             print_debug('... after add_quantities 2... x_return_status='||x_return_status||', qty='||l_quantity||', qty2='||l_quantity2);
9315 
9316             IF x_return_status = fnd_api.g_ret_sts_error THEN
9317                RAISE fnd_api.g_exc_error;
9318             ELSIF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
9319                RAISE fnd_api.g_exc_unexpected_error;
9320             END IF;
9321 
9322             g_rsv_counter := g_rsv_counter + 1;
9323             g_rsv_info(g_rsv_counter).revision := l_revision;
9324             g_rsv_info(g_rsv_counter).lot_number := l_lot_number;
9325             g_rsv_info(g_rsv_counter).subinventory_code := l_subinventory_code;
9326             g_rsv_info(g_rsv_counter).locator_id := l_locator_id;
9327             g_rsv_info(g_rsv_counter).lpn_id := l_lpn_id;
9328             g_rsv_info(g_rsv_counter).quantity := l_quantity;
9329             g_rsv_info(g_rsv_counter).secondary_quantity := l_quantity2;
9330          END LOOP;
9331          CLOSE c_demand_stage_dn;
9332       else
9333          OPEN c_demand_stage;
9334          LOOP
9335             FETCH c_demand_stage INTO
9336                   l_revision
9337                  ,l_lot_number
9338                  ,l_subinventory_code
9339                  ,l_locator_id
9340                  ,l_lpn_id
9341                  ,l_quantity
9342                  ,l_quantity2;
9343 
9344             EXIT WHEN c_demand_stage%NOTFOUND;
9345 
9346             print_debug('in add_demand_qty, calling add_quantities 3, lot='||substr(l_lot_number,1,10)||', subinv='||l_subinventory_code||', loct_id='||l_locator_id||', qty1='||(l_mult * l_quantity)||', qty2='||(l_mult * l_quantity2)||'.');
9347             add_quantities(
9348                  x_return_status     => x_return_status
9349                , p_tree_id           => l_root_id
9350                , p_revision          => l_revision
9351                , p_lot_number        => l_lot_number
9352                , p_subinventory_code => l_subinventory_code
9353                , p_is_reservable_sub => TRUE
9354                , p_locator_id        => l_locator_id
9355                , p_primary_quantity  => l_mult * l_quantity
9356                , p_secondary_quantity=> l_mult * l_quantity2
9357                , p_quantity_type     => g_qr_other_demand
9358                , p_set_check_mark    => FALSE
9359                , p_cost_group_id     => NULL
9360                , p_lpn_id            => l_lpn_id
9361                  );
9362 
9363             print_debug('... after add_quantities 3... x_return_status='||x_return_status||', qty='||l_quantity||', qty2='||l_quantity2);
9364 
9365             IF x_return_status = fnd_api.g_ret_sts_error THEN
9366                RAISE fnd_api.g_exc_error;
9367             ELSIF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
9368                RAISE fnd_api.g_exc_unexpected_error;
9369             END IF;
9370 
9371             g_rsv_counter := g_rsv_counter + 1;
9372             g_rsv_info(g_rsv_counter).revision := l_revision;
9373             g_rsv_info(g_rsv_counter).lot_number := l_lot_number;
9374             g_rsv_info(g_rsv_counter).subinventory_code := l_subinventory_code;
9375             g_rsv_info(g_rsv_counter).locator_id := l_locator_id;
9376             g_rsv_info(g_rsv_counter).lpn_id := l_lpn_id;
9377             g_rsv_info(g_rsv_counter).quantity := l_quantity;
9378             g_rsv_info(g_rsv_counter).secondary_quantity := l_quantity2;
9379          END LOOP;
9380          CLOSE c_demand_stage;
9381       end if;
9382    ELSE
9383       if g_demand_info(p_tree_id).demand_source_line_id IS NOT NULL THEN
9384          OPEN c_demand_dl;
9385          LOOP
9386             FETCH c_demand_dl INTO
9387                   l_revision
9388                  ,l_lot_number
9389                  ,l_subinventory_code
9390                  ,l_locator_id
9391                  ,l_lpn_id
9392                  ,l_quantity
9393                  ,l_quantity2;
9394 
9395             if NOT is_lot_control then  -- Bug 4194323
9396                l_lot_number:=null;
9397             end if;
9398 
9399             EXIT WHEN c_demand_dl%NOTFOUND;
9400 
9401             print_debug('in add_demand_qty, calling add_quantities 4, lot='||substr(l_lot_number,1,10)||', subinv='||l_subinventory_code||', loct_id='||l_locator_id||', qty1='||(l_mult * l_quantity)||', qty2='||(l_mult * l_quantity2)||'.');
9402             add_quantities(
9403                  x_return_status     => x_return_status
9404                , p_tree_id           => l_root_id
9405                , p_revision          => l_revision
9406                , p_lot_number        => l_lot_number
9407                , p_subinventory_code => l_subinventory_code
9408                , p_is_reservable_sub => TRUE
9409                , p_locator_id        => l_locator_id
9410                , p_primary_quantity  => l_mult * l_quantity
9411                , p_secondary_quantity=> l_mult * l_quantity2
9412                , p_quantity_type     => g_qr_other_demand
9413                , p_set_check_mark    => FALSE
9414                , p_cost_group_id     => NULL
9415                , p_lpn_id            => l_lpn_id
9416                  );
9417 
9418             print_debug('... after add_quantities 4... x_return_status='||x_return_status||', qty='||l_quantity||', qty2='||l_quantity2);
9419 
9420             IF x_return_status = fnd_api.g_ret_sts_error THEN
9421                RAISE fnd_api.g_exc_error;
9422             ELSIF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
9423                RAISE fnd_api.g_exc_unexpected_error;
9424             END IF;
9425 
9426             g_rsv_counter := g_rsv_counter + 1;
9427             g_rsv_info(g_rsv_counter).revision := l_revision;
9428             g_rsv_info(g_rsv_counter).lot_number := l_lot_number;
9429             g_rsv_info(g_rsv_counter).subinventory_code := l_subinventory_code;
9430             g_rsv_info(g_rsv_counter).locator_id := l_locator_id;
9431             g_rsv_info(g_rsv_counter).lpn_id := l_lpn_id;
9432             g_rsv_info(g_rsv_counter).quantity := l_quantity;
9433             g_rsv_info(g_rsv_counter).secondary_quantity := l_quantity2;
9434 
9435         END LOOP;
9436         CLOSE c_demand_dl;
9437      elsif g_demand_info(p_tree_id).demand_source_name IS NOT NULL THEN
9438         OPEN c_demand_dn;
9439         LOOP
9440            FETCH c_demand_dn INTO
9441                   l_revision
9442                  ,l_lot_number
9443                  ,l_subinventory_code
9444                  ,l_locator_id
9445                  ,l_lpn_id
9446                  ,l_quantity
9447                  ,l_quantity2;
9448 
9449             EXIT WHEN c_demand_dn%NOTFOUND;
9450 
9451             print_debug('in add_demand_qty, calling add_quantities 5, lot='||substr(l_lot_number,1,10)||', subinv='||l_subinventory_code||', loct_id='||l_locator_id||', qty1='||(l_mult * l_quantity)||', qty2='||(l_mult * l_quantity2)||'.');
9452             add_quantities(
9453                  x_return_status     => x_return_status
9454                , p_tree_id           => l_root_id
9455                , p_revision          => l_revision
9456                , p_lot_number        => l_lot_number
9457                , p_subinventory_code => l_subinventory_code
9458                , p_is_reservable_sub => TRUE
9459                , p_locator_id        => l_locator_id
9460                , p_primary_quantity  => l_mult * l_quantity
9461                , p_secondary_quantity=> l_mult * l_quantity2
9462                , p_quantity_type     => g_qr_other_demand
9463                , p_set_check_mark    => FALSE
9464                , p_cost_group_id     => NULL
9465                , p_lpn_id            => l_lpn_id
9466                  );
9467 
9468             print_debug('... after add_quantities 5... x_return_status='||x_return_status||', qty='||l_quantity||', qty2='||l_quantity2);
9469 
9470             IF x_return_status = fnd_api.g_ret_sts_error THEN
9471                RAISE fnd_api.g_exc_error;
9472             ELSIF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
9473                RAISE fnd_api.g_exc_unexpected_error;
9474             END IF;
9475 
9476             g_rsv_counter := g_rsv_counter + 1;
9477             g_rsv_info(g_rsv_counter).revision := l_revision;
9478             g_rsv_info(g_rsv_counter).lot_number := l_lot_number;
9479             g_rsv_info(g_rsv_counter).subinventory_code := l_subinventory_code;
9480             g_rsv_info(g_rsv_counter).locator_id := l_locator_id;
9481             g_rsv_info(g_rsv_counter).lpn_id := l_lpn_id;
9482             g_rsv_info(g_rsv_counter).quantity := l_quantity;
9483             g_rsv_info(g_rsv_counter).secondary_quantity := l_quantity2;
9484 
9485         END LOOP;
9486         CLOSE c_demand_dn;
9487      else
9488         OPEN c_demand;
9489         LOOP
9490             FETCH  c_demand INTO
9491                   l_revision
9492                  ,l_lot_number
9493                  ,l_subinventory_code
9494                  ,l_locator_id
9495                  ,l_lpn_id
9496                  ,l_quantity
9497                  ,l_quantity2;
9498 
9499             EXIT WHEN c_demand%NOTFOUND;
9500 
9501             print_debug('in add_demand_qty, calling add_quantities 6, lot='||substr(l_lot_number,1,10)||', subinv='||l_subinventory_code||', loct_id='||l_locator_id||', qty1='||(l_mult * l_quantity)||', qty2='||(l_mult * l_quantity2)||'.');
9502             add_quantities(
9503                  x_return_status     => x_return_status
9504                , p_tree_id           => l_root_id
9505                , p_revision          => l_revision
9506                , p_lot_number        => l_lot_number
9507                , p_subinventory_code => l_subinventory_code
9508                , p_is_reservable_sub => TRUE
9509                , p_locator_id        => l_locator_id
9510                , p_primary_quantity  => l_mult * l_quantity
9511                , p_secondary_quantity=> l_mult * l_quantity2
9512                , p_quantity_type     => g_qr_other_demand
9513                , p_set_check_mark    => FALSE
9514                , p_cost_group_id     => NULL
9515                , p_lpn_id            => l_lpn_id
9516                  );
9517 
9518             print_debug('... after add_quantities 6... x_return_status='||x_return_status||', qty='||l_quantity||', qty2='||l_quantity2);
9519 
9520             IF x_return_status = fnd_api.g_ret_sts_error THEN
9521                RAISE fnd_api.g_exc_error;
9522             ELSIF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
9523                RAISE fnd_api.g_exc_unexpected_error;
9524             END IF;
9525 
9526             g_rsv_counter := g_rsv_counter + 1;
9527             g_rsv_info(g_rsv_counter).revision := l_revision;
9528             g_rsv_info(g_rsv_counter).lot_number := l_lot_number;
9529             g_rsv_info(g_rsv_counter).subinventory_code := l_subinventory_code;
9530             g_rsv_info(g_rsv_counter).locator_id := l_locator_id;
9531             g_rsv_info(g_rsv_counter).lpn_id := l_lpn_id;
9532             g_rsv_info(g_rsv_counter).quantity := l_quantity;
9533             g_rsv_info(g_rsv_counter).secondary_quantity := l_quantity2;
9534 
9535         END LOOP;
9536         CLOSE c_demand;
9537      end if;
9538    END IF;
9539    print_debug('... ending add_demand_qty');
9540 
9541 EXCEPTION
9542    WHEN fnd_api.g_exc_error THEN
9543         x_return_status := fnd_api.g_ret_sts_error;
9544 
9545    WHEN fnd_api.g_exc_unexpected_error THEN
9546         x_return_status := fnd_api.g_ret_sts_unexp_error ;
9547 
9548    WHEN OTHERS THEN
9549       x_return_status := fnd_api.g_ret_sts_unexp_error ;
9550       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
9551         THEN
9552          fnd_msg_pub.add_exc_msg
9553            (  g_pkg_name
9554               ,'Add_Demand_Qty'
9555               );
9556       END IF;
9557 END add_demand_qty;
9558 
9559 
9560 --subtract_demand_qty
9561 -- This counteracts the actions of add_demand_qty.
9562 -- For each record in the rsv_info table, add_quantities is called,
9563 -- which increases QR and decrease ATT.
9564 PROCEDURE subtract_demand_qty(
9565     x_return_status  OUT NOCOPY VARCHAR2
9566         ,p_tree_id     IN  INTEGER
9567  ) IS
9568 
9569   l_root_id    INTEGER;
9570 
9571 BEGIN
9572    print_debug('... entering subtract_demand_qty');
9573 
9574    x_return_status := fnd_api.g_ret_sts_success;
9575 
9576    l_root_id := g_demand_info(p_tree_id).root_id;
9577 
9578    FOR i in 1..g_rsv_counter LOOP
9579       print_debug('in subtract_demand_qty, calling add_quantities, qty1='||g_rsv_info(i).quantity||', qty2='||g_rsv_info(i).secondary_quantity||';');
9580 
9581       add_quantities(
9582               x_return_status     => x_return_status
9583             , p_tree_id           => l_root_id
9584             , p_revision          => g_rsv_info(i).revision
9585             , p_lot_number        => g_rsv_info(i).lot_number
9586             , p_subinventory_code => g_rsv_info(i).subinventory_code
9587             , p_is_reservable_sub => TRUE
9588             , p_locator_id        => g_rsv_info(i).locator_id
9589             , p_primary_quantity  => g_rsv_info(i).quantity
9590             , p_secondary_quantity=> g_rsv_info(i).secondary_quantity
9591             , p_quantity_type     => g_qr_other_demand
9592             , p_set_check_mark    => FALSE
9593             , p_cost_group_id     => NULL
9594             , p_lpn_id            => g_rsv_info(i).lpn_id
9595               );
9596 
9597       IF x_return_status = fnd_api.g_ret_sts_error THEN
9598          RAISE fnd_api.g_exc_error;
9599       ELSIF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
9600          RAISE fnd_api.g_exc_unexpected_error;
9601       END IF;
9602 
9603    END LOOP;
9604 
9605 EXCEPTION
9606    WHEN fnd_api.g_exc_error THEN
9607         x_return_status := fnd_api.g_ret_sts_error;
9608 
9609    WHEN fnd_api.g_exc_unexpected_error THEN
9610         x_return_status := fnd_api.g_ret_sts_unexp_error ;
9611 
9612    WHEN OTHERS THEN
9613       x_return_status := fnd_api.g_ret_sts_unexp_error ;
9614       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
9615         THEN
9616          fnd_msg_pub.add_exc_msg
9617            (  g_pkg_name
9618               ,'Subtract_Demand_Qty'
9619               );
9620       END IF;
9621 END subtract_demand_qty;
9622 
9623 --check_demand_trees
9624 --    Returns TRUE if two entries in the demand info
9625 -- plsql table are equivalent.  Used to determine if the
9626 -- demand info needs to be subtracted from the qty tree.
9627 FUNCTION check_demand_trees(
9628          p_rsv_tree    IN  INTEGER
9629    ,p_new_tree   IN  INTEGER
9630  ) RETURN BOOLEAN IS
9631 
9632 BEGIN
9633 
9634   IF (g_demand_info(p_rsv_tree).root_id = g_demand_info(p_new_tree).root_id)
9635     AND
9636      (g_demand_info(p_rsv_tree).demand_source_type_id = g_demand_info(p_new_tree).demand_source_type_id)
9637     AND
9638      ((g_demand_info(p_rsv_tree).demand_source_header_id IS NULL AND
9639        g_demand_info(p_new_tree).demand_source_header_id IS NULL) OR
9640       (g_demand_info(p_rsv_tree).demand_source_header_id = g_demand_info(p_new_tree).demand_source_header_id))
9641     AND
9642      ((g_demand_info(p_rsv_tree).demand_source_line_id IS NULL AND
9643        g_demand_info(p_new_tree).demand_source_line_id IS NULL) OR
9644       (g_demand_info(p_rsv_tree).demand_source_line_id = g_demand_info(p_new_tree).demand_source_line_id))
9645     AND
9646      ((g_demand_info(p_rsv_tree).demand_source_name IS NULL AND
9647        g_demand_info(p_new_tree).demand_source_name IS NULL) OR
9648       (g_demand_info(p_rsv_tree).demand_source_name = g_demand_info(p_new_tree).demand_source_name))
9649     AND
9650      ((g_demand_info(p_rsv_tree).demand_source_delivery IS NULL AND
9651        g_demand_info(p_new_tree).demand_source_delivery IS NULL) OR
9652       (g_demand_info(p_rsv_tree).demand_source_delivery = g_demand_info(p_new_tree).demand_source_delivery))
9653     AND
9654      ((g_demand_info(p_rsv_tree).pick_release IS NULL AND
9655        g_demand_info(p_new_tree).pick_release IS NULL) OR
9656       (g_demand_info(p_rsv_tree).pick_release = g_demand_info(p_new_tree).pick_release))
9657   THEN
9658      return TRUE;
9659   ELSE
9660      return FALSE;
9661   END IF;
9662 END check_demand_trees;
9663 
9664 -- Public Functions and Procedures
9665 
9666 PROCEDURE clear_quantity_cache IS
9667 BEGIN
9668    g_rootinfos.DELETE;
9669    g_nodes.DELETE;
9670    g_rootinfo_counter :=0;
9671    g_org_item_trees.DELETE;
9672    g_all_roots.DELETE;
9673    g_all_roots_counter := 0;
9674    g_saveroots.DELETE;
9675    g_nodes.DELETE;
9676    g_demand_info.DELETE;
9677    g_demand_counter := 0;
9678    g_rsv_info.DELETE;
9679    g_rsv_counter := 0;
9680    g_rsv_tree_id := 0;
9681    g_max_hash_rec := 0;
9682    print_debug('All Quantity Trees distroyed',9);
9683 END clear_quantity_cache;
9684 
9685 -- Procedure
9686 --   query tree
9687 --
9688 --  Version
9689 --   Current version       1.0
9690 --   Initial version       1.0
9691 --
9692 -- Input parameters:
9693 --   p_api_version_number   standard input parameter
9694 --   p_init_msg_lst         standard input parameter
9695 --   p_tree_id              tree_id
9696 --   p_revision             revision
9697 --   p_lot_number           lot_number
9698 --   p_subinventory_code    subinventory code
9699 --   p_locator_id           locator_id
9700 --
9701 -- Output parameters:
9702 --   x_return_status       standard output parameter
9703 --   x_msg_count           standard output parameter
9704 --   x_msg_data            standard output parameter
9705 --   x_qoh                 qoh
9706 --   x_rqoh                rqoh
9707 --   x_qr                  qr
9708 --   x_qs                  qs
9709 --   x_att                 att
9710 --   x_atr                 atr
9711 --
9712 PROCEDURE query_tree
9713   (   p_api_version_number   IN  NUMBER
9714    ,  p_init_msg_lst         IN  VARCHAR2
9715    ,  x_return_status        OUT NOCOPY VARCHAR2
9716    ,  x_msg_count            OUT NOCOPY NUMBER
9717    ,  x_msg_data             OUT NOCOPY VARCHAR2
9718    ,  p_tree_id              IN  INTEGER
9719    ,  p_revision             IN  VARCHAR2
9720    ,  p_lot_number           IN  VARCHAR2
9721    ,  p_subinventory_code    IN  VARCHAR2
9722    ,  p_locator_id           IN  NUMBER
9723    ,  x_qoh                  OUT NOCOPY NUMBER
9724    ,  x_rqoh                 OUT NOCOPY NUMBER
9725    ,  x_qr                   OUT NOCOPY NUMBER
9726    ,  x_qs                   OUT NOCOPY NUMBER
9727    ,  x_att                  OUT NOCOPY NUMBER
9728    ,  x_atr                  OUT NOCOPY NUMBER
9729    ,  p_transfer_subinventory_code IN  VARCHAR2
9730    ,  p_cost_group_id        IN  NUMBER
9731    ,  p_lpn_id               IN  NUMBER
9732    ,  p_transfer_locator_id  IN  NUMBER
9733    ) IS
9734 
9735    l_sqoh    NUMBER;
9736    l_srqoh   NUMBER;
9737    l_sqr     NUMBER;
9738    l_sqs     NUMBER;
9739    l_satt    NUMBER;
9740    l_satr    NUMBER;
9741 
9742 BEGIN
9743    query_tree(
9744       p_api_version_number  =>  p_api_version_number
9745    ,  p_init_msg_lst        =>  p_init_msg_lst
9746    ,  x_return_status       =>  x_return_status
9747    ,  x_msg_count           =>  x_msg_count
9748    ,  x_msg_data            =>  x_msg_data
9749    ,  p_tree_id             =>  p_tree_id
9750    ,  p_revision            =>  p_revision
9751    ,  p_lot_number          =>  p_lot_number
9752    ,  p_subinventory_code   =>  p_subinventory_code
9753    ,  p_locator_id          =>  p_locator_id
9754    ,  x_qoh                 =>  x_qoh
9755    ,  x_rqoh                =>  x_rqoh
9756    ,  x_qr                  =>  x_qr
9757    ,  x_qs                  =>  x_qs
9758    ,  x_att                 =>  x_att
9759    ,  x_atr                 =>  x_atr
9760    ,  x_sqoh                =>  l_sqoh
9761    ,  x_srqoh               =>  l_srqoh
9762    ,  x_sqr                 =>  l_sqr
9763    ,  x_sqs                 =>  l_sqs
9764    ,  x_satt                =>  l_satt
9765    ,  x_satr                =>  l_satr
9766    ,  p_transfer_subinventory_code => p_transfer_subinventory_code
9767    ,  p_cost_group_id       =>  p_cost_group_id
9768    ,  p_lpn_id              =>  p_lpn_id
9769    ,  p_transfer_locator_id =>  p_transfer_locator_id
9770    );
9771 
9772    -- Calling the new signature API.
9773 
9774 END query_tree;
9775 
9776 PROCEDURE query_tree
9777   (   p_api_version_number   IN  NUMBER
9778    ,  p_init_msg_lst         IN  VARCHAR2
9779    ,  x_return_status        OUT NOCOPY VARCHAR2
9780    ,  x_msg_count            OUT NOCOPY NUMBER
9781    ,  x_msg_data             OUT NOCOPY VARCHAR2
9782    ,  p_tree_id              IN  INTEGER
9783    ,  p_revision             IN  VARCHAR2
9784    ,  p_lot_number           IN  VARCHAR2
9785    ,  p_subinventory_code    IN  VARCHAR2
9786    ,  p_locator_id           IN  NUMBER
9787    ,  x_qoh                  OUT NOCOPY NUMBER
9788    ,  x_rqoh                 OUT NOCOPY NUMBER
9789    ,  x_qr                   OUT NOCOPY NUMBER
9790    ,  x_qs                   OUT NOCOPY NUMBER
9791    ,  x_att                  OUT NOCOPY NUMBER
9792    ,  x_atr                  OUT NOCOPY NUMBER
9793    ,  x_sqoh                 OUT NOCOPY NUMBER
9794    ,  x_srqoh                OUT NOCOPY NUMBER
9795    ,  x_sqr                  OUT NOCOPY NUMBER
9796    ,  x_sqs                  OUT NOCOPY NUMBER
9797    ,  x_satt                 OUT NOCOPY NUMBER
9798    ,  x_satr                 OUT NOCOPY NUMBER
9799    ,  p_transfer_subinventory_code IN  VARCHAR2
9800    ,  p_cost_group_id        IN  NUMBER
9801    ,  p_lpn_id               IN  NUMBER
9802    ,  p_transfer_locator_id  IN  NUMBER
9803    ) IS
9804       l_api_version_number   CONSTANT NUMBER       := 1.0;
9805       l_api_name             CONSTANT VARCHAR2(30) := 'QUERY_TREE';
9806       l_return_status        VARCHAR2(1) := fnd_api.g_ret_sts_success;
9807       l_node_index           INTEGER;
9808       l_atr                  NUMBER;
9809       l_att                  NUMBER;
9810       l_satr                 NUMBER;
9811       l_satt                 NUMBER;
9812       l_tree_id              NUMBER;
9813       l_is_reservable_sub    BOOLEAN;
9814       l_is_reservable_transfer_sub BOOLEAN;
9815       l_found                BOOLEAN;
9816       l_loop_index           INTEGER;
9817       l_sub_index            NUMBER;
9818       l_root_id              INTEGER;
9819       l_lpn_id               NUMBER;
9820       l_is_reservable_item   BOOLEAN;
9821       --Bug 4294336
9822       l_atr2                 NUMBER;
9823       l_satr2                NUMBER;
9824       l_debug_line           VARCHAR2(300);
9825 
9826       l_return_value         BOOLEAN; --13961830
9827       l_lot_divisible_flag   VARCHAR2(1);--13961830
9828 
9829       l_return_org_value     BOOLEAN; --16428282
9830       l_wms_enabled          VARCHAR2(1); --16428282
9831       l_allocate_lot_flag    VARCHAR2(1); --16428282
9832       l_exit_level           INTEGER;--16428282
9833 BEGIN
9834    IF g_debug = 1 THEN
9835       print_debug(l_api_name || ' Entered',9);
9836    END IF;
9837 
9838    --  Standard call to check for call compatibility
9839    IF NOT fnd_api.compatible_api_call(l_api_version_number
9840                                       , p_api_version_number
9841                                       , l_api_name
9842                                       , G_PKG_NAME
9843                                       ) THEN
9844       RAISE fnd_api.g_exc_unexpected_error;
9845    END IF;
9846 
9847    --  Initialize message list.
9848    IF fnd_api.to_boolean(p_init_msg_lst) THEN
9849       fnd_msg_pub.initialize;
9850    END IF;
9851 
9852    l_root_id := g_demand_info(p_tree_id).root_id;
9853    -- check if tree id is valid
9854    IF is_tree_valid(l_root_id) = FALSE THEN
9855       fnd_message.set_name('INV', 'INV-Qtyroot not found');
9856       fnd_message.set_token('ROUTINE', 'Query_Tree');
9857       fnd_msg_pub.ADD;
9858       RAISE fnd_api.g_exc_error;
9859    END IF;
9860 
9861    print_debug('>>> In query_tree : mode='||g_demand_info(p_tree_id).tree_mode||', tree_id='||p_tree_id
9862       ||'..for item='||g_rootinfos(l_root_id).inventory_item_id||' rev='||p_revision||' lot='||p_lot_number
9863       ||' sub='||p_subinventory_code||' loc='||p_locator_id||' lpn='||p_lpn_id
9864       ||'   xfrsub='||p_transfer_subinventory_code||' xfrloc='||p_transfer_locator_id,9);
9865 
9866    --subtract out reservations added when tree was queried in txn mode
9867    IF g_demand_info(p_tree_id).tree_mode = g_reservation_mode THEN
9868       if g_rsv_tree_id <> 0 then
9869          print_debug('  Reservation Mode, calling subtract_demand_qty with rsv_tree_id='||g_rsv_tree_id, 12);
9870          subtract_demand_qty(
9871               x_return_status => l_return_status
9872             , p_tree_id       => g_rsv_tree_id
9873          );
9874 
9875          IF l_return_status = fnd_api.g_ret_sts_error THEN
9876             RAISE fnd_api.g_exc_error;
9877          ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9878             RAISE fnd_api.g_exc_unexpected_error;
9879          END IF;
9880          g_rsv_tree_id := 0;
9881          g_rsv_counter := 0;
9882          g_rsv_info.DELETE;
9883       ELSE
9884          print_debug('  Reservation Mode, tree_id=0', 12);
9885       end if;
9886    ELSIF g_demand_info(p_tree_id).tree_mode = g_transaction_mode THEN
9887       --subtract out rsv info if the tree is currently for a diff demand source
9888       if g_rsv_tree_id <> 0 AND check_demand_trees(g_rsv_tree_id, p_tree_id) = FALSE then
9889          print_debug('  Transaction Mode, calling subtract_demand_qty with rsv_tree_id='||g_rsv_tree_id, 12);
9890 
9891          subtract_demand_qty(
9892               x_return_status => l_return_status
9893             , p_tree_id       => g_rsv_tree_id
9894          );
9895 
9896          IF l_return_status = fnd_api.g_ret_sts_error THEN
9897             RAISE fnd_api.g_exc_error;
9898          ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9899             RAISE fnd_api.g_exc_unexpected_error;
9900          END IF;
9901          g_rsv_tree_id := 0;
9902          g_rsv_counter := 0;
9903          g_rsv_info.DELETE;
9904       end if;
9905 
9906       if g_rsv_tree_id = 0 then
9907          print_debug('  Transaction Mode, rsv_tree_id=0, calling add_demand_qty', 12);
9908          add_demand_qty(
9909                x_return_status => l_return_status
9910              , p_tree_id       => p_tree_id
9911              );
9912 
9913          IF l_return_status = fnd_api.g_ret_sts_error THEN
9914             RAISE fnd_api.g_exc_error;
9915          ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9916             RAISE fnd_api.g_exc_unexpected_error;
9917          END IF;
9918          g_rsv_tree_id := p_tree_id;
9919       ELSE
9920          print_debug('  Transaction Mode, rsv_tree_id='||g_rsv_tree_id, 12);
9921       end if;
9922    END IF;   -- tree_mode
9923 
9924    print_debug('QT, calling find_tree_node, tree_id='||l_root_id||', p_tree_id='||p_tree_id);
9925    l_found := find_tree_node(
9926                       x_return_status      => l_return_status
9927                     , p_tree_id            => l_root_id
9928                     , p_revision           => p_revision
9929                     , p_lot_number         => p_lot_number
9930                     , p_subinventory_code  => p_subinventory_code
9931                     , p_is_reservable_sub  => NULL
9932                     , p_locator_id         => p_locator_id
9933                     , x_node_index         => l_node_index
9934                     , p_lpn_id             => p_lpn_id
9935                     , p_cost_group_id      => p_cost_group_id
9936                     );
9937 
9938    IF l_return_status = fnd_api.g_ret_sts_error THEN
9939       RAISE fnd_api.g_exc_error;
9940    End IF ;
9941 
9942    IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
9943       RAISE fnd_api.g_exc_unexpected_error;
9944    End IF;
9945 
9946    IF l_found = FALSE THEN
9947       fnd_message.set_name('INV','INV-Cannot find node');
9948       fnd_message.set_token('ROUTINE', 'Query_Tree');
9949       fnd_msg_pub.ADD;
9950       RAISE fnd_api.g_exc_unexpected_error;
9951    End IF;
9952 
9953    x_qoh  := g_nodes(l_node_index).qoh;
9954    x_rqoh := g_nodes(l_node_index).rqoh;
9955    x_qr   := g_nodes(l_node_index).qr;
9956    x_qs   := g_nodes(l_node_index).qs;
9957    x_sqoh  := g_nodes(l_node_index).sqoh;
9958    x_srqoh := g_nodes(l_node_index).srqoh;
9959    x_sqr   := g_nodes(l_node_index).sqr;
9960    x_sqs   := g_nodes(l_node_index).sqs;
9961 
9962    g_pqoh := g_nodes(l_node_index).pqoh;
9963    g_spqoh := g_nodes(l_node_index).spqoh;
9964    g_rpqoh := g_nodes(l_node_index).rpqoh;      -- Bug 9644285
9965    g_srpqoh := g_nodes(l_node_index).srpqoh;    -- Bug 9644285
9966 
9967   /*********************************************************************
9968    * Logic for finding ATT and ATR:
9969    *  (Secondary quantities follow the same computation)
9970    * If Node is at Sub level or below
9971    *    Get reservable for From Sub and To Sub
9972    *    If From Sub is not reservable
9973    *       ATT = ATT of node; ATR = 0
9974    *    If From Sub = To Sub
9975    *       ATT = min(ATT of node, ATR of ancestor nodes below sub)
9976    *       ATR = min(ATR of node, ATR of ancestor nodes below sub)
9977    *         (don't need to look at upper reservations, since all reservations
9978    *          except those at Locator and lpn level are not affected by move)
9979    *    If To Sub is Reservable
9980    *       ATT = min(ATT of node, ATR of ancestor nodes up to sub)
9981    *       ATR = min(ATR of node, ATR of ancestor nodes up to sub)
9982    *          (don't need to look at upper reservations, since
9983    *           all reservations
9984    *           above Sub level are not affected by move)
9985    *    Else (From Sub is reservable and To Sub is not Reservable)
9986    *       ATT = min (ATT of node, ATR of all ancestor nodes)
9987    *       ATR = min (ATR of node, ATR of all ancestor nodes)
9988    * Else (node above sub level)
9989    *    ATR = min (ATR at node, ATR of all ancestor nodes)
9990    *    ATT = min (ATT at node, ATT of all ancestor nodes)
9991    *********************************************************************/
9992 
9993 
9994    -- find out reservable_type if sub code is not null
9995    IF (p_subinventory_code IS NOT NULL) THEN
9996 
9997       --bug 9380420, commenting the code below, as is_reservable_sub should be checked from g_nodes.
9998       /*
9999       l_sub_index := get_ancestor_sub(l_node_index);
10000       print_debug('--after get_ancestor_sub... node_index='||l_node_index||', l_sub_index='||l_sub_index||', level='||g_nodes(l_sub_index).node_level);
10001       l_is_reservable_sub:=g_nodes(l_sub_index).is_reservable_sub;
10002 
10003       IF l_is_reservable_sub = TRUE
10004       THEN
10005           print_debug('.... is reservable_sub=TRUE');
10006       ELSIF l_is_reservable_sub = FALSE
10007       THEN
10008           print_debug('.... is reservable_sub=FALSES');
10009       ELSE
10010           print_debug('.... is reservable_sub=other value');
10011       END IF;
10012 
10013       -- get that info from db table if not in the node
10014       --     why: because, if reservable_sub=FALSE, everything below will be NON-reservable.
10015       --                   if reservable_sub=TRUE, must check the other level.
10016       IF (l_is_reservable_sub IS NULL OR l_is_reservable_sub = TRUE)
10017       THEN
10018          check_is_reservable
10019               ( x_return_status     => l_return_status
10020               , p_inventory_item_id => g_rootinfos(l_root_id).inventory_item_id
10021               , p_organization_id   => g_rootinfos(l_root_id).organization_id
10022               , p_subinventory_code => p_subinventory_code
10023               , p_locator_id        => p_locator_id
10024               , p_lot_number        => p_lot_number
10025               , p_root_id           => l_root_id
10026               , x_is_reservable     => l_is_reservable_sub
10027               , p_lpn_id            => p_lpn_id); -- Onhand Material Status Support
10028 
10029          IF l_return_status = fnd_api.g_ret_sts_error THEN
10030             RAISE fnd_api.g_exc_error;
10031          End IF ;
10032 
10033          IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10034             RAISE fnd_api.g_exc_unexpected_error;
10035          End IF;
10036 
10037       END IF;   -- l_is_reservable_sub
10038       */
10039 
10040       --bug 16428282 start
10041       l_return_org_value := INV_CACHE.set_org_rec(g_rootinfos(l_root_id).organization_id);
10042       IF NOT l_return_org_value THEN
10043         RAISE fnd_api.g_exc_unexpected_error;
10044       END IF;
10045       l_wms_enabled := INV_CACHE.org_rec.WMS_ENABLED_FLAG;
10046 
10047       --l_allocate_lot_flag := INV_CACHE.org_rec.ALLOCATE_LOT_FLAG;
10048       l_allocate_lot_flag := inv_flex_lot_allocation_pub.get_allocate_lot_flag(
10049                                   p_organization_id     => g_rootinfos(l_root_id).organization_id
10050                                 , p_inventory_item_id   => g_rootinfos(l_root_id).inventory_item_id
10051                                 , p_subinventory_code   => p_subinventory_code
10052                                 , p_locator_id          => p_locator_id
10053                                 , p_revision            => p_revision
10054                                 , p_lpn_id              => p_lpn_id
10055                                 );
10056       print_debug('In query_tree-: WMS Org:'|| l_wms_enabled ||' ALLOCATE_LOT_FLAG: ' ||l_allocate_lot_flag);
10057       --bug 16428282 end
10058 
10059       --bug 13961830 start
10060       l_return_value := INV_CACHE.set_item_rec(g_rootinfos(l_root_id).organization_id, g_rootinfos(l_root_id).inventory_item_id);
10061       IF NOT l_return_value THEN
10062           RAISE fnd_api.g_exc_unexpected_error;
10063       END IF;
10064       l_lot_divisible_flag := INV_CACHE.item_rec.LOT_DIVISIBLE_FLAG;
10065 
10066       IF NVL(l_lot_divisible_flag,'Y') = 'N' THEN
10067       	  print_debug('l_lot_divisible_flag=N');
10068       ELSE
10069          print_debug('l_lot_divisible_flag=Y');
10070       END IF;
10071       --bug 13961830 end
10072 
10073       l_is_reservable_sub := g_nodes(l_node_index).is_reservable_sub;
10074 
10075 
10076       IF l_is_reservable_sub = TRUE THEN
10077           l_debug_line := '  Sub '||p_subinventory_code||' is reservable';
10078       ELSIF l_is_reservable_sub = FALSE THEN
10079           l_debug_line := '  Sub '||p_subinventory_code||' is not reservable';
10080       END IF;
10081 
10082       --get reservable type for to sub
10083       IF (p_transfer_subinventory_code IS NOT NULL) THEN
10084          check_is_reservable
10085               ( x_return_status     => l_return_status
10086               , p_inventory_item_id => g_rootinfos(l_root_id).inventory_item_id
10087               , p_organization_id   => g_rootinfos(l_root_id).organization_id
10088               , p_subinventory_code => p_transfer_subinventory_code
10089               , p_locator_id        => p_transfer_locator_id
10090               , p_lot_number        => p_lot_number
10091               , p_root_id           => l_root_id
10092               , x_is_reservable     => l_is_reservable_transfer_sub
10093               , p_lpn_id            => p_lpn_id); -- Onhand Material Status Support
10094 
10095          IF l_return_status = fnd_api.g_ret_sts_error THEN
10096             RAISE fnd_api.g_exc_error;
10097          End IF ;
10098 
10099          IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10100             RAISE fnd_api.g_exc_unexpected_error;
10101          End IF;
10102 
10103          IF l_is_reservable_transfer_sub = TRUE THEN
10104             l_debug_line := l_debug_line||'  Xfr Sub '||p_transfer_subinventory_code||' is reservable';
10105          ELSIF l_is_reservable_transfer_sub = FALSE THEN
10106             l_debug_line := l_debug_line||'  Xfr Sub '||p_transfer_subinventory_code||' is not reservable';
10107          END IF;
10108 
10109       ELSE
10110          l_is_reservable_transfer_sub := FALSE;
10111       END IF;     -- p_transfer_subinventory_code
10112 
10113       print_debug(l_debug_line, 12);
10114       l_debug_line := '';
10115 
10116       --From sub is not reservable
10117       IF l_is_reservable_sub = FALSE THEN
10118       	   print_debug('Calculating Query Time ATT and ATR,From sub is not reservable');
10119           l_atr := 0;
10120           l_att := g_nodes(l_node_index).att;
10121           l_satr := 0;
10122           l_satt := g_nodes(l_node_index).satt;
10123 
10124       --From Loc = To Loc (LPN unpack)
10125       ELSIF (p_transfer_locator_id IS NOT NULL AND p_transfer_locator_id = p_locator_id) THEN
10126       	   print_debug('Calculating Query Time ATT and ATR,From Loc = To Loc (LPN unpack)');
10127           l_atr := g_nodes(l_node_index).atr;
10128           l_att := g_nodes(l_node_index).att;
10129           l_satr := g_nodes(l_node_index).satr;
10130           l_satt := g_nodes(l_node_index).satt;
10131           print_debug('    Node = '||g_nodes(l_node_index).node_level||'   ATT = '||LPAD(l_att, 10)||' ATR = '||LPAD(l_atr,10), 12);
10132           l_loop_index := g_nodes(l_node_index).parent_index;
10133           LOOP
10134              EXIT WHEN g_nodes(l_loop_index).node_level <= g_locator_level;
10135              IF l_atr > g_nodes(l_loop_index).atr THEN
10136                 l_atr := g_nodes(l_loop_index).atr;
10137                 --l_satr := g_nodes(l_loop_index).satr;
10138              END IF;
10139 
10140              --Bug 16682075
10141              IF l_satr > g_nodes(l_loop_index).satr THEN
10142                 l_satr := g_nodes(l_loop_index).satr;
10143              END IF;
10144              -- bug 9379407
10145              /*
10146              IF l_att > g_nodes(l_loop_index).atr THEN
10147                 l_att := g_nodes(l_loop_index).atr;
10148                 l_satt := g_nodes(l_loop_index).satr;
10149              END IF;
10150              */
10151              --bug 11895245
10152              IF (g_nodes(l_loop_index).att > 0) THEN
10153 	             IF l_att > g_nodes(l_loop_index).att THEN
10154 	                l_att := g_nodes(l_loop_index).att;
10155 	                --l_satt := g_nodes(l_loop_index).satt;
10156 	             END IF;
10157 	         ELSE
10158 	             IF l_att > g_nodes(l_loop_index).atr THEN
10159                 	   l_att := g_nodes(l_loop_index).atr;
10160                     --l_satt := g_nodes(l_loop_index).satr;
10161                 END IF;
10162 	         END IF;
10163 
10164             --Bug 16682075
10165             IF (g_nodes(l_loop_index).satt > 0) THEN
10166                IF l_satt > g_nodes(l_loop_index).satt THEN
10167                      l_satt := g_nodes(l_loop_index).satt;
10168                END IF;
10169             ELSE
10170                IF l_satt > g_nodes(l_loop_index).satr THEN
10171                      l_satt := g_nodes(l_loop_index).satr;
10172                END IF;
10173             END IF;
10174 
10175              print_debug('    Node = '||g_nodes(l_loop_index).node_level||'   ATT = '||LPAD(l_att, 10)||' ATR = '||LPAD(l_atr,10), 12);
10176              l_loop_index :=  g_nodes(l_loop_index).parent_index;
10177           END LOOP;
10178       --From Sub = To Sub
10179       --Bug 14516283 Adding the condition such that both from and to are reservable or non reservable
10180       ELSIF (p_transfer_subinventory_code IS NOT NULL AND p_transfer_subinventory_code = p_subinventory_code
10181                 and l_is_reservable_sub=l_is_reservable_transfer_sub) THEN
10182       	   print_debug('Calculating Query Time ATT and ATR,From Sub = To Sub');
10183           l_atr := g_nodes(l_node_index).atr;
10184           l_att := g_nodes(l_node_index).att;
10185           l_satr := g_nodes(l_node_index).satr;
10186           l_satt := g_nodes(l_node_index).satt;
10187           print_debug('    Node = '||g_nodes(l_node_index).node_level||'   ATT = '||LPAD(l_att, 10)||' ATR = '||LPAD(l_atr,10), 12);
10188           l_loop_index := g_nodes(l_node_index).parent_index;
10189           LOOP
10190              EXIT WHEN g_nodes(l_loop_index).node_level <= g_sub_level;
10191              IF l_atr > g_nodes(l_loop_index).atr THEN
10192                 l_atr := g_nodes(l_loop_index).atr;
10193                 --l_satr := g_nodes(l_loop_index).satr;
10194              END IF;
10195 
10196              --Bug 16682075
10197              IF l_satr > g_nodes(l_loop_index).satr THEN
10198                 l_satr := g_nodes(l_loop_index).satr;
10199              END IF;
10200 
10201              -- bug 9379407
10202              /*
10203              IF l_att > g_nodes(l_loop_index).atr THEN
10204                 l_att := g_nodes(l_loop_index).atr;
10205                 l_satt := g_nodes(l_loop_index).satr;
10206              END IF;
10207              */
10208 
10209              --bug 11895245
10210              IF (g_nodes(l_loop_index).att > 0) THEN
10211 	             IF l_att > g_nodes(l_loop_index).att THEN
10212 	                l_att := g_nodes(l_loop_index).att;
10213 	               -- l_satt := g_nodes(l_loop_index).satt;
10214 	             END IF;
10215 	         ELSE
10216 	             IF l_att > g_nodes(l_loop_index).atr THEN
10217                 	   l_att := g_nodes(l_loop_index).atr;
10218                     --l_satt := g_nodes(l_loop_index).satr;
10219                 END IF;
10220 	         END IF;
10221 
10222              --Bug 16682075
10223              IF (g_nodes(l_loop_index).satt > 0) THEN
10224                   IF l_satt > g_nodes(l_loop_index).satt THEN
10225                      l_satt := g_nodes(l_loop_index).satt;
10226                   END IF;
10227              ELSE
10228                   IF l_satt > g_nodes(l_loop_index).satr THEN
10229                      l_satt := g_nodes(l_loop_index).satr;
10230                  END IF;
10231              END IF;
10232 
10233              print_debug('    Node = '||g_nodes(l_loop_index).node_level||'   ATT = '||LPAD(l_att, 10)||' ATR = '||LPAD(l_atr,10), 12);
10234              l_loop_index :=  g_nodes(l_loop_index).parent_index;
10235           END LOOP;
10236 
10237       -- To Sub is Reservable (FALSE if To Sub is NULL)
10238       ELSIF (l_is_reservable_transfer_sub) THEN
10239       	  print_debug('Calculating Query Time ATT and ATR,To Sub is Reservable (FALSE if To Sub is NULL)');
10240           l_atr := g_nodes(l_node_index).atr;
10241           l_att := g_nodes(l_node_index).att;
10242           l_satr := g_nodes(l_node_index).satr;
10243           l_satt := g_nodes(l_node_index).satt;
10244           print_debug('    Node = '||g_nodes(l_node_index).node_level||'   ATT = '||LPAD(l_att, 10)||' ATR = '||LPAD(l_atr,10), 12);
10245           l_loop_index := g_nodes(l_node_index).parent_index;
10246 
10247           --bug 16428282 start,if org is WMS org and Allocate Lot flag is no, query time ATT/ATR should return Min(current node, all parent node).
10248           IF nvl(l_wms_enabled,'N') = 'Y' and nvl(l_allocate_lot_flag,'Y') = 'N' THEN
10249             print_debug('l_exit_level is at item level');
10250             LOOP
10251               IF l_atr > g_nodes(l_loop_index).atr THEN
10252                 l_atr := g_nodes(l_loop_index).atr;
10253                 --l_satr := g_nodes(l_loop_index).satr;
10254               END IF;
10255 
10256               --Bug 16682075
10257               IF l_satr > g_nodes(l_loop_index).satr THEN
10258                  l_satr := g_nodes(l_loop_index).satr;
10259               END IF;
10260 
10261               IF (l_att > g_nodes(l_loop_index).atr) THEN
10262                 l_att  := g_nodes(l_loop_index).atr;
10263                 --l_satt := g_nodes(l_loop_index).satr;
10264               END IF;
10265 
10266               --Bug 16682075
10267               IF (l_satt > g_nodes(l_loop_index).satr) THEN
10268                 l_satt := g_nodes(l_loop_index).satr;
10269               END IF;
10270 
10271               print_debug('    Node = '||g_nodes(l_loop_index).node_level||'   ATT = '||LPAD(l_att, 10)||' ATR = '||LPAD(l_atr,10), 12);
10272 
10273               EXIT WHEN g_nodes(l_loop_index).node_level = g_item_level;
10274               l_loop_index :=  g_nodes(l_loop_index).parent_index;
10275             END LOOP;
10276 
10277           ELSE
10278           	print_debug('l_exit_level is at sub level');
10279 
10280           	LOOP
10281              EXIT WHEN g_nodes(l_loop_index).node_level < g_sub_level;
10282              IF l_atr > g_nodes(l_loop_index).atr THEN
10283                 l_atr := g_nodes(l_loop_index).atr;
10284                 --l_satr := g_nodes(l_loop_index).satr;
10285              END IF;
10286 
10287              --Bug 16682075
10288              IF l_satr > g_nodes(l_loop_index).satr THEN
10289                  l_satr := g_nodes(l_loop_index).satr;
10290              END IF;
10291              --bug 9379407
10292              /*
10293              IF l_att > g_nodes(l_loop_index).atr THEN
10294                 l_att := g_nodes(l_loop_index).atr;
10295                 l_satt := g_nodes(l_loop_index).satr;
10296              END IF;
10297              */
10298               --bug 11895245
10299              IF (g_nodes(l_loop_index).att > 0) THEN
10300 	             IF l_att > g_nodes(l_loop_index).att THEN
10301 	                l_att := g_nodes(l_loop_index).att;
10302 	                --l_satt := g_nodes(l_loop_index).satt;
10303 	             END IF;
10304 	           ELSE
10305 	             IF l_att > g_nodes(l_loop_index).atr THEN
10306                 	   l_att := g_nodes(l_loop_index).atr;
10307                     --l_satt := g_nodes(l_loop_index).satr;
10308                 END IF;
10309 	            END IF;
10310 
10311               --Bug 16682075
10312               IF (g_nodes(l_loop_index).satt > 0) THEN
10313                   IF l_satt > g_nodes(l_loop_index).satt THEN
10314                      l_satt := g_nodes(l_loop_index).satt;
10315                   END IF;
10316               ELSE
10317                   IF l_satt > g_nodes(l_loop_index).satr THEN
10318                      l_satt := g_nodes(l_loop_index).satr;
10319                  END IF;
10320               END IF;
10321 
10322 
10323               print_debug('    Node = '||g_nodes(l_loop_index).node_level||'   ATT = '||LPAD(l_att, 10)||' ATR = '||LPAD(l_atr,10), 12);
10324               l_loop_index :=  g_nodes(l_loop_index).parent_index;
10325             END LOOP;
10326 
10327           END IF;
10328 
10329 
10330       -- From Sub is reservable and To Sub is Null or Not Reservable
10331       ELSE
10332       	   print_debug('Calculating Query Time ATT and ATR,From Sub is reservable and To Sub is Null or Not Reservable');
10333           l_atr := g_nodes(l_node_index).atr;
10334           l_att := g_nodes(l_node_index).att;
10335           l_satr := g_nodes(l_node_index).satr;
10336           l_satt := g_nodes(l_node_index).satt;
10337           print_debug('    Node = '||g_nodes(l_node_index).node_level||'   ATT = '||LPAD(l_att, 10)||' ATR = '||LPAD(l_atr,10), 12);
10338           l_loop_index := g_nodes(l_node_index).parent_index;
10339           LOOP
10340              IF l_atr > g_nodes(l_loop_index).atr THEN
10341                 l_atr := g_nodes(l_loop_index).atr;
10342                 --l_satr := g_nodes(l_loop_index).satr;
10343              END IF;
10344 
10345              --Bug 16682075
10346              IF l_satr > g_nodes(l_loop_index).satr THEN
10347                  l_satr := g_nodes(l_loop_index).satr;
10348              END IF;
10349 
10350 
10351            -- bug 12971601,For transfer case, Rule:ATT = min (ATT of node, ATR of all ancestor nodes)
10352            -- Bug 13387319
10353            -- bug 14614787, commenting the and condition as it is redundant for the fix in bug 13387319
10354              IF (l_att > g_nodes(l_loop_index).atr) THEN-- and (p_transfer_subinventory_code IS NOT NULL and l_is_reservable_transfer_sub = false)
10355                l_att  := g_nodes(l_loop_index).atr;
10356                --l_satt := g_nodes(l_loop_index).satr;
10357              END IF;
10358 
10359              --Bug 16682075
10360              IF (l_satt > g_nodes(l_loop_index).satr) THEN
10361                 l_satt := g_nodes(l_loop_index).satr;
10362              END IF;
10363 
10364              --bug 11895245
10365              /*
10366               IF (g_nodes(l_loop_index).att > 0) THEN
10367 	             IF l_att > g_nodes(l_loop_index).att THEN
10368 	                l_att := g_nodes(l_loop_index).att;
10369 	                l_satt := g_nodes(l_loop_index).satt;
10370 	             END IF;
10371 	         ELSE
10372 	             IF l_att > g_nodes(l_loop_index).atr THEN
10373                 	   l_att := g_nodes(l_loop_index).atr;
10374                     l_satt := g_nodes(l_loop_index).satr;
10375                 END IF;
10376 	         END IF;
10377 	         */
10378              print_debug('    Node = '||g_nodes(l_loop_index).node_level||'   ATT = '||LPAD(l_att, 10)||' ATR = '||LPAD(l_atr,10), 12);
10379 
10380              EXIT WHEN g_nodes(l_loop_index).node_level = g_item_level;
10381              l_loop_index :=  g_nodes(l_loop_index).parent_index;
10382           END LOOP;
10383       END IF;
10384    --Node level above sub level (Item, Revision, Lot)
10385    ELSE
10386       -- p_subinventory_code IS NULL:
10387       print_debug('QT, p_subinventory_code(null?)='||p_subinventory_code);
10388       l_atr := g_nodes(l_node_index).atr;
10389       l_att := g_nodes(l_node_index).att;
10390       l_satr := g_nodes(l_node_index).satr;
10391       l_satt := g_nodes(l_node_index).satt;
10392 
10393       --Bug 4294336
10394       IF g_nodes(l_node_index).node_level = g_item_level THEN
10395         l_atr := g_nodes(l_node_index).atr - g_nodes(l_node_index).qs_adj1 ;
10396         l_satr := g_nodes(l_node_index).satr - g_nodes(l_node_index).sqs_adj1 ;
10397       END IF;
10398       print_debug('    Node = '||g_nodes(l_node_index).node_level||'   ATT = '||LPAD(l_att, 10)||' ATR = '||LPAD(l_atr,10), 12);
10399 
10400       IF g_nodes(l_node_index).node_level <> g_item_level THEN
10401           l_loop_index := g_nodes(l_node_index).parent_index;
10402           LOOP
10403              --Bug 4294336
10404              l_atr2 := g_nodes(l_loop_index).atr - g_nodes(l_loop_index).qs_adj1 ;
10405              l_satr2 := g_nodes(l_loop_index).satr - g_nodes(l_loop_index).sqs_adj1 ;
10406 
10407              IF l_atr > l_atr2 THEN
10408                 l_atr := l_atr2 ;
10409                 --l_satr := l_satr2;
10410              END IF;
10411 
10412              --Bug 16682075
10413              IF l_satr > l_satr2 THEN
10414                  l_satr := l_satr2;
10415              END IF;
10416 
10417              IF l_att > g_nodes(l_loop_index).att THEN
10418                 l_att := g_nodes(l_loop_index).att;
10419                 --l_satt := g_nodes(l_loop_index).satt;
10420              END IF;
10421 
10422              --Bug 16682075
10423              IF l_satt > g_nodes(l_loop_index).satt THEN
10424                  l_satt := g_nodes(l_loop_index).satt;
10425              END IF;
10426 
10427              print_debug('    Node = '||g_nodes(l_loop_index).node_level||'   ATT = '||LPAD(l_att, 10)||' ATR = '||LPAD(l_atr,10), 12);
10428              EXIT WHEN g_nodes(l_loop_index).node_level = g_item_level;
10429              l_loop_index :=  g_nodes(l_loop_index).parent_index;
10430           END LOOP;
10431       END IF;
10432    END IF;     -- p_subinventory_code
10433 
10434    --bug 9380420, checking the is_reservable flag from g_nodes.
10435    /*
10436    --Bug 3424532 atr should be returned as 0 for non reservable items
10437    check_is_reservable_item
10438        (  x_return_status      => l_return_status
10439         , p_organization_id    => g_rootinfos(l_root_id).organization_id
10440         , p_inventory_item_id  => g_rootinfos(l_root_id).inventory_item_id
10441         , x_is_reservable_item => l_is_reservable_item
10442        );
10443    */
10444    l_is_reservable_item := g_nodes(g_rootinfos(l_root_id).item_node_index).is_reservable_sub;
10445    IF l_is_reservable_item THEN
10446       print_debug('  Item_reservable = YES');
10447       x_atr := l_atr;
10448       x_satr := l_satr;
10449    ELSE
10450       print_debug('  Item_reservable = NO');
10451       x_atr := 0;
10452       x_satr := 0;
10453    END IF;
10454    x_att := l_att;
10455    x_satt := l_satt;
10456 
10457    -- Check whether the item is DUOM
10458    IF (g_rootinfos(l_root_id).is_DUOM_control = FALSE)
10459    THEN
10460       print_debug(' root_id='||l_root_id||', DUOM_control=FALSE');
10461       x_sqoh  := NULL;
10462       x_srqoh := NULL;
10463       x_sqr   := NULL;
10464       x_sqs   := NULL;
10465       g_spqoh := NULL;
10466       x_satr  := NULL;
10467       x_satt  := NULL;
10468    ELSE
10469       print_debug(' root_id='||l_root_id||', DUOM_control=TRUE');
10470    END IF;
10471 
10472    x_return_status := l_return_status;
10473 
10474    IF g_debug = 1 THEN
10475       print_tree(p_tree_id);
10476       print_debug(l_api_name || ' Exited with status = '||l_return_status||', Qty : '
10477                     || LPad(x_qoh,8)||':'||LPad(x_rqoh,8)||':'||LPad(x_qr,8)||':'||LPad(x_qs,8)||':'
10478                     || LPad(x_att,8)||':'||LPad(x_atr, 8)||':'||LPad(g_pqoh,8),9);
10479       print_debug('>> qoh2='||x_sqoh||', rqoh2='||x_srqoh||', qr2='||x_sqr||', qs2='||x_sqs||', pqoh2='||g_spqoh||', atr2='||x_satr||', att2='||x_satt);
10480       print_debug(' ',9);
10481    END IF;
10482 EXCEPTION
10483 
10484     WHEN fnd_api.g_exc_error THEN
10485         print_debug('QT: ending... g_exc_error'||SQLERRM,9);
10486         x_return_status := fnd_api.g_ret_sts_error;
10487 
10488         --  Get message count and data
10489         fnd_msg_pub.count_and_get
10490           (  p_count => x_msg_count
10491            , p_data  => x_msg_data
10492            );
10493 
10494    WHEN fnd_api.g_exc_unexpected_error THEN
10495         print_debug('QT: ending... g_exc_unexpected_error '||SQLERRM,9);
10496         x_return_status := fnd_api.g_ret_sts_unexp_error ;
10497 
10498         --  Get message count and data
10499         fnd_msg_pub.count_and_get
10500           (  p_count  => x_msg_count
10501            , p_data   => x_msg_data
10502             );
10503 
10504     WHEN OTHERS THEN
10505         print_debug('QT: ending... OTHERS.'||SQLERRM,9);
10506         x_return_status := fnd_api.g_ret_sts_unexp_error ;
10507 
10508         IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
10509           THEN
10510            fnd_msg_pub.add_exc_msg
10511              (  g_pkg_name
10512               , l_api_name
10513               );
10514         END IF;
10515 
10516         --  Get message count and data
10517         fnd_msg_pub.count_and_get
10518           (  p_count  => x_msg_count
10519            , p_data   => x_msg_data
10520             );
10521 END query_tree;
10522 
10523 PROCEDURE query_tree
10524   (   p_api_version_number   IN  NUMBER
10525    ,  p_init_msg_lst         IN  VARCHAR2
10526    ,  x_return_status        OUT NOCOPY VARCHAR2
10527    ,  x_msg_count            OUT NOCOPY NUMBER
10528    ,  x_msg_data             OUT NOCOPY VARCHAR2
10529    ,  p_tree_id              IN  INTEGER
10530    ,  p_revision             IN  VARCHAR2
10531    ,  p_lot_number           IN  VARCHAR2
10532    ,  p_subinventory_code    IN  VARCHAR2
10533    ,  p_locator_id           IN  NUMBER
10534    ,  x_qoh                  OUT NOCOPY NUMBER
10535    ,  x_rqoh                 OUT NOCOPY NUMBER
10536    ,  x_pqoh                 OUT NOCOPY NUMBER
10537    ,  x_qr                   OUT NOCOPY NUMBER
10538    ,  x_qs                   OUT NOCOPY NUMBER
10539    ,  x_att                  OUT NOCOPY NUMBER
10540    ,  x_atr                  OUT NOCOPY NUMBER
10541    ,  p_transfer_subinventory_code IN  VARCHAR2
10542    ,  p_cost_group_id        IN  NUMBER
10543    ,  p_lpn_id               IN  NUMBER
10544    ,  p_transfer_locator_id  IN  NUMBER
10545    ) IS
10546 
10547 l_sqoh    NUMBER;
10548 l_srqoh   NUMBER;
10549 l_sqr     NUMBER;
10550 l_sqs     NUMBER;
10551 l_satt    NUMBER;
10552 l_satr    NUMBER;
10553 
10554 BEGIN
10555 query_tree(
10556       p_api_version_number  =>  p_api_version_number
10557    ,  p_init_msg_lst        =>  p_init_msg_lst
10558    ,  x_return_status       =>  x_return_status
10559    ,  x_msg_count           =>  x_msg_count
10560    ,  x_msg_data            =>  x_msg_data
10561    ,  p_tree_id             =>  p_tree_id
10562    ,  p_revision            =>  p_revision
10563    ,  p_lot_number          =>  p_lot_number
10564    ,  p_subinventory_code   =>  p_subinventory_code
10565    ,  p_locator_id          =>  p_locator_id
10566    ,  x_qoh                 =>  x_qoh
10567    ,  x_rqoh                =>  x_rqoh
10568    ,  x_qr                  =>  x_qr
10569    ,  x_qs                  =>  x_qs
10570    ,  x_att                 =>  x_att
10571    ,  x_atr                 =>  x_atr
10572    ,  x_sqoh                =>  l_sqoh
10573    ,  x_srqoh               =>  l_srqoh
10574    ,  x_sqr                 =>  l_sqr
10575    ,  x_sqs                 =>  l_sqs
10576    ,  x_satt                =>  l_satt
10577    ,  x_satr                =>  l_satr
10578    ,  p_transfer_subinventory_code => p_transfer_subinventory_code
10579    ,  p_cost_group_id       =>  p_cost_group_id
10580    ,  p_lpn_id              =>  p_lpn_id
10581    ,  p_transfer_locator_id =>  p_transfer_locator_id
10582 );
10583 
10584    x_pqoh := g_pqoh;
10585 
10586 END query_tree;
10587 
10588 -- invConv changes begin : overloaded(2)
10589 PROCEDURE query_tree
10590   (   p_api_version_number   IN  NUMBER
10591    ,  p_init_msg_lst         IN  VARCHAR2
10592    ,  x_return_status        OUT NOCOPY VARCHAR2
10593    ,  x_msg_count            OUT NOCOPY NUMBER
10594    ,  x_msg_data             OUT NOCOPY VARCHAR2
10595    ,  p_tree_id              IN  INTEGER
10596    ,  p_revision             IN  VARCHAR2
10597    ,  p_lot_number           IN  VARCHAR2
10598    ,  p_subinventory_code    IN  VARCHAR2
10599    ,  p_locator_id           IN  NUMBER
10600    ,  x_qoh                  OUT NOCOPY NUMBER
10601    ,  x_rqoh                 OUT NOCOPY NUMBER
10602    ,  x_pqoh                 OUT NOCOPY NUMBER
10603    ,  x_qr                   OUT NOCOPY NUMBER
10604    ,  x_qs                   OUT NOCOPY NUMBER
10605    ,  x_att                  OUT NOCOPY NUMBER
10606    ,  x_atr                  OUT NOCOPY NUMBER
10607    ,  x_sqoh                 OUT NOCOPY NUMBER
10608    ,  x_srqoh                OUT NOCOPY NUMBER
10609    ,  x_spqoh                OUT NOCOPY NUMBER
10610    ,  x_sqr                  OUT NOCOPY NUMBER
10611    ,  x_sqs                  OUT NOCOPY NUMBER
10612    ,  x_satt                 OUT NOCOPY NUMBER
10613    ,  x_satr                 OUT NOCOPY NUMBER
10614    ,  p_transfer_subinventory_code IN  VARCHAR2
10615    ,  p_cost_group_id        IN  NUMBER
10616    ,  p_lpn_id               IN  NUMBER
10617    ,  p_transfer_locator_id  IN  NUMBER
10618    ) IS
10619 
10620     l_api_name             CONSTANT VARCHAR2(30) := 'Query_Tree';
10621 
10622 
10623 BEGIN
10624 
10625     print_debug('  >>>>>>>>>  In query_tree2');
10626 
10627     query_tree(
10628           p_api_version_number  =>  p_api_version_number
10629        ,  p_init_msg_lst        =>  p_init_msg_lst
10630        ,  x_return_status       =>  x_return_status
10631        ,  x_msg_count           =>  x_msg_count
10632        ,  x_msg_data            =>  x_msg_data
10633        ,  p_tree_id             =>  p_tree_id
10634        ,  p_revision            =>  p_revision
10635        ,  p_lot_number          =>  p_lot_number
10636        ,  p_subinventory_code   =>  p_subinventory_code
10637        ,  p_locator_id          =>  p_locator_id
10638        ,  x_qoh                 =>  x_qoh
10639        ,  x_rqoh                =>  x_rqoh
10640        ,  x_qr                  =>  x_qr
10641        ,  x_qs                  =>  x_qs
10642        ,  x_att                 =>  x_att
10643        ,  x_atr                 =>  x_atr
10644        ,  x_sqoh                =>  x_sqoh
10645        ,  x_srqoh               =>  x_srqoh
10646        ,  x_sqr                 =>  x_sqr
10647        ,  x_sqs                 =>  x_sqs
10648        ,  x_satt                =>  x_satt
10649        ,  x_satr                =>  x_satr
10650        ,  p_transfer_subinventory_code => p_transfer_subinventory_code
10651        ,  p_cost_group_id       =>  p_cost_group_id
10652        ,  p_lpn_id              =>  p_lpn_id
10653        ,  p_transfer_locator_id =>  p_transfer_locator_id
10654     );
10655 
10656     x_pqoh := g_pqoh;
10657     x_spqoh := g_spqoh;
10658 
10659     print_debug('  End of  query_tree2');
10660 
10661 EXCEPTION
10662     WHEN fnd_api.g_exc_error THEN
10663         x_return_status := fnd_api.g_ret_sts_error;
10664         --  Get message count and data
10665         fnd_msg_pub.count_and_get
10666           (  p_count => x_msg_count
10667            , p_data  => x_msg_data
10668            );
10669    WHEN fnd_api.g_exc_unexpected_error THEN
10670         x_return_status := fnd_api.g_ret_sts_unexp_error ;
10671 
10672         --  Get message count and data
10673         fnd_msg_pub.count_and_get
10674           (  p_count  => x_msg_count
10675            , p_data   => x_msg_data
10676             );
10677     WHEN OTHERS THEN
10678         x_return_status := fnd_api.g_ret_sts_unexp_error ;
10679         IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
10680           THEN
10681            fnd_msg_pub.add_exc_msg
10682              (  g_pkg_name
10683               , l_api_name
10684               );
10685         END IF;
10686         --  Get message count and data
10687         fnd_msg_pub.count_and_get
10688           (  p_count  => x_msg_count
10689            , p_data   => x_msg_data
10690             );
10691 END query_tree;
10692 
10693 --
10694 -- Procedure
10695 --   create_tree
10696 -- Description
10697 --   Create a quantity tree
10698 --
10699 --  Version
10700 --   Current version           1.0
10701 --   Initial version           1.0
10702 --
10703 -- Input parameters:
10704 --   p_api_version_number      standard input parameter
10705 --   p_init_msg_lst            standard input parameter
10706 --   p_organization_id         organzation id
10707 --   p_inventory_item_id       inventory_item_id
10708 --   p_tree_mode               tree mode, either g_reservation_mode
10709 --                             or g_transaction_mode
10710 --   p_is_revision_control
10711 --   p_is_lot_control
10712 --   p_is_serial_control
10713 --   p_asset_sub_only
10714 --   p_include_suggestion      should be true only for pick/put engine
10715 --   p_demand_source_type_id   demand_source_type_id
10716 --   p_demand_source_header_id demand_source_header_id
10717 --   p_demand_source_line_id   demand_source_line_id
10718 --   p_demand_source_name      demand_source_name
10719 --   p_demand_source_delivery  demand_source_delivery
10720 --   p_pick_release            whether qty tree called from pick release
10721 --                             process or not
10722 --
10723 -- Output parameters:
10724 --   x_return_status           standard output parameter
10725 --   x_msg_count               standard output parameter
10726 --   x_msg_data                standard output parameter
10727 --   x_tree_id                 used later to refer to the same tree
10728 --
10729 PROCEDURE create_tree
10730   (   p_api_version_number       IN  NUMBER
10731    ,  p_init_msg_lst             IN  VARCHAR2
10732    ,  x_return_status            OUT NOCOPY VARCHAR2
10733    ,  x_msg_count                OUT NOCOPY NUMBER
10734    ,  x_msg_data                 OUT NOCOPY VARCHAR2
10735    ,  p_organization_id          IN  NUMBER
10736    ,  p_inventory_item_id        IN  NUMBER
10737    ,  p_tree_mode                IN  INTEGER
10738    ,  p_is_revision_control      IN  BOOLEAN
10739    ,  p_is_lot_control           IN  BOOLEAN
10740    ,  p_is_serial_control        IN  BOOLEAN
10741    ,  p_asset_sub_only           IN  BOOLEAN
10742    ,  p_include_suggestion       IN  BOOLEAN
10743    ,  p_demand_source_type_id    IN  NUMBER
10744    ,  p_demand_source_header_id  IN  NUMBER
10745    ,  p_demand_source_line_id    IN  NUMBER
10746    ,  p_demand_source_name       IN  VARCHAR2
10747    ,  p_demand_source_delivery   IN  NUMBER
10748    ,  p_lot_expiration_date      IN  DATE
10749    ,  x_tree_id                  OUT NOCOPY INTEGER
10750    ,  p_onhand_source            IN  NUMBER
10751    ,  p_exclusive                IN  NUMBER
10752    ,  p_pick_release             IN  NUMBER
10753 
10754   ) IS
10755 
10756 l_grade_code          VARCHAR2(150);
10757 
10758 BEGIN
10759 create_tree
10760   (   p_api_version_number       => p_api_version_number
10761    ,  p_init_msg_lst             => p_init_msg_lst
10762    ,  x_return_status            => x_return_status
10763    ,  x_msg_count                => x_msg_count
10764    ,  x_msg_data                 => x_msg_data
10765    ,  p_organization_id          => p_organization_id
10766    ,  p_inventory_item_id        => p_inventory_item_id
10767    ,  p_tree_mode                => p_tree_mode
10768    ,  p_is_revision_control      => p_is_revision_control
10769    ,  p_is_lot_control           => p_is_lot_control
10770    ,  p_is_serial_control        => p_is_serial_control
10771    ,  p_grade_code               => l_grade_code
10772    ,  p_asset_sub_only           => p_asset_sub_only
10773    ,  p_include_suggestion       => p_include_suggestion
10774    ,  p_demand_source_type_id    => p_demand_source_type_id
10775    ,  p_demand_source_header_id  => p_demand_source_header_id
10776    ,  p_demand_source_line_id    => p_demand_source_line_id
10777    ,  p_demand_source_name       => p_demand_source_name
10778    ,  p_demand_source_delivery   => p_demand_source_delivery
10779    ,  p_lot_expiration_date      => p_lot_expiration_date
10780    ,  x_tree_id                  => x_tree_id
10781    ,  p_onhand_source            => p_onhand_source
10782    ,  p_exclusive                => p_exclusive
10783    ,  p_pick_release             => p_pick_release
10784   );
10785 
10786 
10787 END create_tree;
10788 
10789 -- invConv changes begin : overload:
10790 PROCEDURE create_tree
10791   (   p_api_version_number       IN  NUMBER
10792    ,  p_init_msg_lst             IN  VARCHAR2
10793    ,  x_return_status            OUT NOCOPY VARCHAR2
10794    ,  x_msg_count                OUT NOCOPY NUMBER
10795    ,  x_msg_data                 OUT NOCOPY VARCHAR2
10796    ,  p_organization_id          IN  NUMBER
10797    ,  p_inventory_item_id        IN  NUMBER
10798    ,  p_tree_mode                IN  INTEGER
10799    ,  p_is_revision_control      IN  BOOLEAN
10800    ,  p_is_lot_control           IN  BOOLEAN
10801    ,  p_is_serial_control        IN  BOOLEAN
10802    ,  p_grade_code               IN  VARCHAR2
10803    ,  p_asset_sub_only           IN  BOOLEAN
10804    ,  p_include_suggestion       IN  BOOLEAN
10805    ,  p_demand_source_type_id    IN  NUMBER
10806    ,  p_demand_source_header_id  IN  NUMBER
10807    ,  p_demand_source_line_id    IN  NUMBER
10808    ,  p_demand_source_name       IN  VARCHAR2
10809    ,  p_demand_source_delivery   IN  NUMBER
10810    ,  p_lot_expiration_date      IN  DATE
10811    ,  x_tree_id                  OUT NOCOPY INTEGER
10812    ,  p_onhand_source            IN  NUMBER
10813    ,  p_exclusive                IN  NUMBER
10814    ,  p_pick_release             IN  NUMBER
10815 
10816   ) IS
10817       l_api_version_number       CONSTANT NUMBER       := 1.0;
10818       l_api_name                 CONSTANT VARCHAR2(30) := 'CREATE_TREE';
10819       l_return_status            VARCHAR2(1) := fnd_api.g_ret_sts_success;
10820       l_rootinfo_index           INTEGER;
10821       l_demand_source_type_id    NUMBER;
10822       l_demand_source_header_id  NUMBER;
10823       l_demand_source_line_id    NUMBER;
10824       l_demand_source_name       VARCHAR2(80); -- bug 11879550, increase it from 30 to 80
10825       l_demand_source_delivery   NUMBER;
10826       l_tree_id                  INTEGER;
10827       l_lot_expiration_date      DATE;
10828 
10829       l_lot_control            NUMBER;
10830       l_grade_control          VARCHAR2(1);
10831       l_grade_code             VARCHAR2(150);
10832       l_DUOM_control           VARCHAR2(3) := 'P';      -- Value = P for primary only
10833                                                         -- Value = PS for primary and secondary
10834       l_lot_status_enabled     VARCHAR2(2);
10835       l_debug_line             VARCHAR2(300);
10836 
10837       CURSOR get_item_details( org_id IN NUMBER, item_id IN NUMBER) IS
10838          SELECT  NVL(grade_control_flag, 'N')
10839                , NVL(lot_control_code, 1)
10840                , tracking_quantity_ind
10841                , lot_status_enabled
10842          FROM mtl_system_items
10843          WHERE inventory_item_id = item_id
10844          AND organization_id = org_id;
10845 
10846 BEGIN
10847    IF g_debug IS NULL THEN
10848       g_debug :=  NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
10849    END IF;
10850 
10851    IF g_is_mat_status_used IS NULL THEN
10852       g_is_mat_status_used := NVL(FND_PROFILE.VALUE('INV_MATERIAL_STATUS'), 2);
10853    END IF;
10854 
10855    IF g_debug = 1 THEN
10856       print_debug(l_api_name || ' Entered',9);
10857       l_debug_line := '>>> In create_tree : mode='||p_tree_mode||':'||p_organization_id||':'||p_inventory_item_id||': item ctrl: ';
10858       if (p_is_revision_control = TRUE)
10859       THEN
10860          l_debug_line := l_debug_line||'TRUE ';
10861       else
10862          l_debug_line := l_debug_line||'FASLE';
10863       end if;
10864       if (p_is_lot_control = TRUE)
10865       then
10866          l_debug_line := l_debug_line||':TRUE ';
10867       else
10868          l_debug_line := l_debug_line||':FALSE';
10869       end if;
10870       if (p_is_serial_control = TRUE)
10871       then
10872          l_debug_line := l_debug_line||':TRUE ';
10873       else
10874          l_debug_line := l_debug_line||':FALSE';
10875       end if;
10876       print_debug(l_debug_line || ': demand: '||p_demand_source_type_id||':'||p_demand_source_header_id||':'||p_demand_source_line_id
10877                     ||'  Profile: Material Status =' || g_is_mat_status_used ,12);
10878    END IF;
10879 
10880 
10881    if (p_is_revision_control = TRUE)
10882    THEN
10883    print_debug('  >>>>>>>>>  In create_tree. mode='||p_tree_mode||', grade='||p_grade_code||', rev_ctl=TRUE. mat_stat='||INV_QUANTITY_TREE_PVT.g_is_mat_status_used);
10884    else
10885    print_debug('  >>>>>>>>>  In create_tree. mode='||p_tree_mode||', grade='||p_grade_code||', rev_ctl=FALSE. mat_stat='||INV_QUANTITY_TREE_PVT.g_is_mat_status_used);
10886    end if;
10887 
10888    --  Standard call to check for call compatibility
10889    IF NOT fnd_api.compatible_api_call(l_api_version_number
10890                                       , p_api_version_number
10891                                       , l_api_name
10892                                       , G_PKG_NAME
10893                                       ) THEN
10894       RAISE fnd_api.g_exc_unexpected_error;
10895    END IF;
10896 
10897    --  Initialize message list.
10898    IF fnd_api.to_boolean(p_init_msg_lst) THEN
10899       fnd_msg_pub.initialize;
10900    END IF;
10901 
10902    g_is_pickrelease := nvl(inv_cache.is_pickrelease,FALSE);
10903 
10904    g_empty_root_index := NULL;
10905    g_hash_string := NULL;
10906 
10907    -- validate demand source info
10908    IF p_tree_mode IN (g_transaction_mode, g_loose_only_mode) THEN
10909       IF p_demand_source_type_id IS NULL THEN
10910          fnd_message.set_name('INV', 'INV-MISSING DEMAND SOURCE TYPE');
10911          fnd_msg_pub.ADD;
10912          RAISE fnd_api.g_exc_error;
10913       END IF;
10914 
10915       IF p_demand_source_header_id IS NULL THEN
10916          IF p_demand_source_name IS NULL THEN
10917             fnd_message.set_name('INV', 'INV-MISSING DEMAND SRC HEADER');
10918             fnd_msg_pub.ADD;
10919             RAISE fnd_api.g_exc_error;
10920          END IF;
10921       END IF;
10922 
10923       IF p_demand_source_header_id IS NULL
10924         AND p_demand_source_line_id IS NOT NULL THEN
10925          fnd_message.set_name('INV', 'INV-MISSING DEMAND SRC HEADER');
10926          fnd_msg_pub.ADD;
10927          RAISE fnd_api.g_exc_error;
10928       END IF;
10929    END IF;
10930 
10931    -- Material Status : Need to know whether the item is lot_control : MANDATORY.
10932    -- Get Item Details:
10933    OPEN get_item_details(p_organization_id, p_inventory_item_id);
10934    FETCH get_item_details
10935     INTO l_grade_control
10936        , l_lot_control
10937        , l_DUOM_control
10938        , l_lot_status_enabled;
10939 
10940    print_debug('DUOM control='||l_DUOM_control);
10941    IF (get_item_details%NOTFOUND)
10942    THEN
10943       print_debug(' get_item_details NOTFOUND for org='||p_organization_id||', item='||p_inventory_item_id);
10944       CLOSE get_item_details;
10945       -- The item doesn't exist under this organization.
10946       FND_MESSAGE.SET_NAME('INV', 'ITEM_NOTFOUND');
10947       FND_MESSAGE.SET_TOKEN('INVENTORY_ITEM_ID', p_inventory_item_id);
10948       FND_MESSAGE.SET_TOKEN('ORGANIZATION_ID', p_organization_id);
10949       FND_MSG_PUB.ADD;
10950       RAISE FND_API.G_EXC_ERROR;
10951    END IF;
10952    -- invConv comment : g_is_mat_status_used = 1 == Material Status is USED
10953    IF (inv_quantity_tree_pvt.g_is_mat_status_used = 1 AND l_lot_control = 2)
10954    THEN
10955       g_is_lot_control := TRUE;
10956    ELSE
10957       g_is_lot_control := FALSE;
10958    END IF;
10959    -- invConv note : the using of is_DUOM_control is made later in the procedure
10960    CLOSE get_item_details;
10961 
10962    IF (p_exclusive = g_exclusive) THEN
10963       lock_tree(
10964               p_api_version_number  => 1.0
10965             , p_init_msg_lst        => fnd_api.g_false
10966             , x_return_status       => l_return_status
10967             , x_msg_count           => x_msg_count
10968             , x_msg_data            => x_msg_data
10969             , p_organization_id     => p_organization_id
10970             , p_inventory_item_id   => p_inventory_item_id
10971             );
10972 
10973       IF l_return_status = fnd_api.g_ret_sts_error THEN
10974          RAISE fnd_api.g_exc_error;
10975       END IF ;
10976 
10977       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10978          RAISE fnd_api.g_exc_unexpected_error;
10979       END IF;
10980    END IF;
10981 
10982    IF p_lot_expiration_date IS NOT NULL THEN
10983       l_lot_expiration_date := trunc(p_lot_expiration_date + 1) - 0.00001;
10984    END IF;
10985 
10986    -- find the rootinfo record
10987    l_rootinfo_index := find_rootinfo(
10988                      x_return_status           => l_return_status
10989                    , p_organization_id         => p_organization_id
10990                    , p_inventory_item_id       => p_inventory_item_id
10991                    , p_tree_mode               => p_tree_mode
10992                    , p_is_revision_control     => p_is_revision_control
10993                    , p_is_lot_control          => p_is_lot_control
10994                    , p_is_serial_control       => p_is_serial_control
10995                    , p_asset_sub_only          => p_asset_sub_only
10996                    , p_include_suggestion      => p_include_suggestion
10997                    , p_demand_source_type_id   => p_demand_source_type_id
10998                    , p_demand_source_header_id => p_demand_source_header_id
10999                    , p_demand_source_line_id   => p_demand_source_line_id
11000                    , p_demand_source_name      => p_demand_source_name
11001                    , p_demand_source_delivery  => p_demand_source_delivery
11002                    , p_lot_expiration_date     => l_lot_expiration_date
11003                    , p_onhand_source        => p_onhand_source
11004                    , p_pick_release         => p_pick_release
11005                    -- odab temp removed  , p_grade_code            => p_grade_code
11006                    );
11007 
11008    IF l_return_status = fnd_api.g_ret_sts_error THEN
11009       RAISE fnd_api.g_exc_error;
11010    End IF ;
11011 
11012    IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11013       RAISE fnd_api.g_exc_unexpected_error;
11014    End IF;
11015    -- create the tree if the rootinfo can not be found
11016    IF (l_rootinfo_index = 0) THEN
11017       new_tree(
11018                  x_return_status           => l_return_status
11019                , p_organization_id         => p_organization_id
11020                , p_inventory_item_id       => p_inventory_item_id
11021                , p_tree_mode               => p_tree_mode
11022                , p_is_revision_control     => p_is_revision_control
11023                , p_is_lot_control          => p_is_lot_control
11024                , p_is_serial_control       => p_is_serial_control
11025                , p_asset_sub_only          => p_asset_sub_only
11026                , p_include_suggestion      => p_include_suggestion
11027                , p_demand_source_type_id   => p_demand_source_type_id
11028                , p_demand_source_header_id => p_demand_source_header_id
11029                , p_demand_source_line_id   => p_demand_source_line_id
11030                , p_demand_source_name      => p_demand_source_name
11031                , p_demand_source_delivery  => p_demand_source_delivery
11032                , p_lot_expiration_date     => l_lot_expiration_date
11033                , p_onhand_source           => p_onhand_source
11034                , p_pick_release            => p_pick_release
11035                , p_grade_code              => p_grade_code
11036                , x_tree_id                 => l_rootinfo_index
11037                );
11038       IF l_return_status = fnd_api.g_ret_sts_error THEN
11039          RAISE fnd_api.g_exc_error;
11040       END IF ;
11041 
11042       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11043          RAISE fnd_api.g_exc_unexpected_error;
11044       END IF;
11045 
11046    END IF;
11047 
11048    print_debug(' l_tree_id='||l_rootinfo_index||', l_rootinfo_index='||g_demand_info(l_rootinfo_index).root_id);
11049    l_tree_id := l_rootinfo_index;
11050    l_rootinfo_index := g_demand_info(l_tree_id).root_id;
11051 
11052    -- set the DUOM indicator
11053    IF (l_DUOM_control = 'P')
11054    THEN
11055       print_debug('DUOM control='||l_DUOM_control||', then FALSE');
11056       g_rootinfos(l_rootinfo_index).is_DUOM_control := FALSE;
11057    ELSE
11058       print_debug('DUOM control='||l_DUOM_control||', then TRUE');
11059       g_rootinfos(l_rootinfo_index).is_DUOM_control := TRUE;
11060    END IF;
11061 
11062    IF (l_lot_status_enabled = 'Y')
11063    THEN
11064       g_rootinfos(l_rootinfo_index).is_lot_status_enabled := TRUE;
11065    ELSE
11066       g_rootinfos(l_rootinfo_index).is_lot_status_enabled := FALSE;
11067    END IF;
11068 
11069    -- (re)build the tree if necessary
11070    IF (g_rootinfos(l_rootinfo_index).need_refresh) THEN
11071       --made change so that invalidated trees are rebuilt if
11072       -- create_tree is called for the same tree
11073       IF g_rootinfos(l_rootinfo_index).inventory_item_id IS NULL THEN
11074          g_rootinfos(l_rootinfo_index).inventory_item_id := p_inventory_item_id;
11075          g_rootinfos(l_rootinfo_index).organization_id := p_organization_id;
11076       END IF;
11077       build_tree(l_return_status, l_rootinfo_index);
11078       IF (l_return_status = fnd_api.g_ret_sts_error OR l_return_status = fnd_api.g_ret_sts_unexp_error) THEN
11079          -- if build_tree failed
11080          -- invalidate the rootinfo record and the index to the record
11081          invalidate_tree(l_rootinfo_index);
11082          l_tree_id := 0;
11083       END IF;
11084 
11085       IF l_return_status = fnd_api.g_ret_sts_error THEN
11086          RAISE fnd_api.g_exc_error;
11087       End IF ;
11088 
11089       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11090          RAISE fnd_api.g_exc_unexpected_error;
11091       End IF;
11092 
11093    END IF;
11094 
11095    x_tree_id := l_tree_id;
11096    x_return_status := l_return_status;
11097 
11098    IF g_debug = 1 THEN
11099       print_tree(l_tree_id);
11100       print_debug(l_api_name || ' Exited with status = '||l_return_status,9);
11101       print_debug(' ',9);
11102    END IF;
11103 
11104 EXCEPTION
11105 
11106     WHEN fnd_api.g_exc_error THEN
11107         print_debug(' CreateTree ending with g_exc_error.'||SQLERRM,9);
11108         x_return_status := fnd_api.g_ret_sts_error;
11109 
11110         --  Get message count and data
11111         fnd_msg_pub.count_and_get
11112           (  p_count => x_msg_count
11113            , p_data  => x_msg_data
11114            );
11115 
11116    WHEN fnd_api.g_exc_unexpected_error THEN
11117         print_debug(' CreateTree ending with g_exc_unexpected_error.'||SQLERRM,9);
11118         x_return_status := fnd_api.g_ret_sts_unexp_error ;
11119 
11120         --  Get message count and data
11121         fnd_msg_pub.count_and_get
11122           (  p_count  => x_msg_count
11123            , p_data   => x_msg_data
11124             );
11125 
11126     WHEN OTHERS THEN
11127         print_debug(' CreateTree ending with OTHERS.'||SQLERRM,9);
11128         x_return_status := fnd_api.g_ret_sts_unexp_error ;
11129 
11130         IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
11131           THEN
11132            fnd_msg_pub.add_exc_msg
11133              (  g_pkg_name
11134               , l_api_name
11135               );
11136         END IF;
11137 
11138         --  Get message count and data
11139         fnd_msg_pub.count_and_get
11140           (  p_count  => x_msg_count
11141            , p_data   => x_msg_data
11142             );
11143 
11144 END create_tree;
11145 
11146 --Traverse
11147 --  Used by prepare_reservation_quantities to
11148 -- quickly traverse the trees and copy all the node information
11149 -- into the tables to be later used for bulk insert.
11150 -- For quantity calculations in reservations mode, the ATR of a node
11151 -- is the smallest ATR for that node and all of that node's ancestors.
11152 -- Instead of looking at every ancestor for every node to determine
11153 -- ATR, this procedure takes advantage of its recursive, downward working
11154 -- nature.  By taking in the ATR of the parent node, the procedure
11155 -- no longer needs to look at every ancestor. It can just compare the
11156 -- current node's value to that of its parent, which had already been
11157 -- compared to its parent's ATR, and so on.
11158 -- A parent node calls traverse only on its first child node. That child
11159 -- will call traverse on each of its siblings, and also on its own children.
11160 
11161 PROCEDURE traverse(
11162          p_node_index      INTEGER
11163        , p_parent_atr      NUMBER
11164        , p_parent_satr     NUMBER
11165    ) IS
11166 
11167    l_atr  NUMBER;
11168    l_satr NUMBER;
11169 BEGIN
11170    IF p_node_index = 0 THEN
11171       return;
11172    END IF;
11173    -- ATR = min(current node's ATR, parent node's ATR)
11174    IF p_parent_atr IS NOT NULL AND p_parent_atr < g_nodes(p_node_index).atr THEN
11175      l_atr   := p_parent_atr;
11176      -- l_satr  := p_parent_satr;
11177    ELSE
11178       l_atr   := g_nodes(p_node_index).atr;
11179       --l_satr  := g_nodes(p_node_index).satr;
11180    END IF;
11181 
11182    -- Bug 16682075
11183    IF p_parent_satr IS NOT NULL AND p_parent_satr < g_nodes(p_node_index).satr THEN
11184       l_satr  := p_parent_satr;
11185    ELSE
11186       l_satr  := g_nodes(p_node_index).satr;
11187    END IF;
11188 
11189    -- nodes with ATR <= 0 are not inserted into the table
11190    -- bug 1643186: We should insert records with ATR = 0
11191    --   if there is onhand qty
11192    -- bug 1990180 - we should always call traverse on the sibling nodes;
11193    --     before, we were only calling traverse on sibling node if
11194    --     qoh was greater than 0
11195    -- bug 2206138 - the onhand quantity should be the reservable quantity onhand
11196    print_debug('in traverse, ATR='||l_atr||', rqoh='||g_nodes(p_node_index).rqoh,9);
11197    print_debug(' node level='||g_nodes(p_node_index).node_level,9);
11198    IF g_nodes(p_node_index).is_reservable_sub
11199    THEN
11200       print_debug('in traverse, sub='||g_nodes(p_node_index).subinventory_code||', loct='||g_nodes(p_node_index).locator_id||', lot='||substr(g_nodes(p_node_index).lot_number,1,10)||', rsv=TRUE');
11201    ELSE
11202       print_debug('in traverse, sub='||g_nodes(p_node_index).subinventory_code||', loct='||g_nodes(p_node_index).locator_id||', lot='||substr(g_nodes(p_node_index).lot_number,1,10)||', rsv=FALSE');
11203    END IF;
11204 
11205 
11206    IF l_atr > 0 OR g_nodes(p_node_index).rqoh >0 THEN
11207 
11208       IF (g_nodes(p_node_index).is_reservable_sub = TRUE)
11209       THEN
11210          g_rsv_qty_counter := g_rsv_qty_counter +1;
11211          g_rsv_qty_revision(g_rsv_qty_counter)           := g_nodes(p_node_index).revision;
11212          g_rsv_qty_lot_number(g_rsv_qty_counter)         := g_nodes(p_node_index).lot_number;
11213          g_rsv_qty_subinventory_code(g_rsv_qty_counter)  := g_nodes(p_node_index).subinventory_code;
11214          g_rsv_qty_locator_id(g_rsv_qty_counter)         := g_nodes(p_node_index).locator_id;
11215          g_rsv_qty_cost_group_id(g_rsv_qty_counter)      := g_nodes(p_node_index).cost_group_id;
11216          g_rsv_qty_lpn_id(g_rsv_qty_counter)             := g_nodes(p_node_index).lpn_id;
11217          g_rsv_qty_node_level(g_rsv_qty_counter)         := g_nodes(p_node_index).node_level;
11218          g_rsv_qty_qoh(g_rsv_qty_counter)                := g_nodes(p_node_index).rqoh;
11219          g_rsv_qty_sqoh(g_rsv_qty_counter)               := g_nodes(p_node_index).srqoh;
11220          g_rsv_qty_atr(g_rsv_qty_counter)                := l_atr;
11221          g_rsv_qty_satr(g_rsv_qty_counter)               := l_satr;
11222          -- invConv change : note that grade_code is hold under rootinfo
11223       END IF;
11224 
11225       -- call traverse on children
11226       -- invConv change : added 3rd parameter
11227       traverse (g_nodes(p_node_index).first_child_index, l_atr, l_satr);
11228    END IF;
11229 
11230    --call traverse on siblings
11231    -- invConv change : added 3rd parameter
11232    traverse (g_nodes(p_node_index).next_sibling_index, p_parent_atr, p_parent_satr);
11233 
11234 END traverse;
11235 
11236 
11237 -- Procedure
11238 --   prepare_reservation_quantities
11239 -- Description
11240 --      This procedure is called from the reservation form to
11241 -- initialize the table used for the LOVs in that form.
11242 --
11243 -- This procedure inserts one record for every node in the tree
11244 -- which has ATR > 0.  The table mtl_rsv_quantities_temp
11245 -- is a session specific global temp table which serves as the
11246 -- basis for the LOVs in the Reservations form.
11247 --
11248 PROCEDURE prepare_reservation_quantities(
11249      x_return_status        OUT NOCOPY VARCHAR2
11250    , p_tree_id              IN  NUMBER
11251 )
11252 IS
11253 
11254    l_root_id      INTEGER;
11255    l_item_node    INTEGER;
11256    i              INTEGER;
11257    l_grade_code   VARCHAR2(150) := NULL;
11258 
11259    CURSOR Get_Grade_From_Lot( org_id  IN NUMBER
11260                             , item_id IN NUMBER
11261                             , lot     IN VARCHAR2) IS
11262       SELECT grade_code
11263       FROM mtl_lot_numbers
11264       WHERE organization_id = org_id
11265       AND inventory_item_id = item_id
11266       AND lot_number = lot;
11267 
11268    l_api_name             CONSTANT VARCHAR2(60) := 'PREPARE_RESERVATION_QUANTITIES';
11269    -- Bug 8593965
11270    l_prev_lot_number       VARCHAR2(80);
11271    l_prev_grade_code       VARCHAR2(150);
11272 BEGIN
11273 
11274    IF g_debug = 1 THEN
11275       print_debug(l_api_name || ' Entered',9);
11276    END IF;
11277 
11278    -- clear out temp table
11279    DELETE FROM MTL_RSV_QUANTITIES_TEMP;
11280 
11281    -- find root node
11282    l_root_id := g_demand_info(p_tree_id).root_id;
11283 
11284    -- find appropriate child node
11285    l_item_node := g_rootinfos(l_root_id).item_node_index;
11286 
11287    -- initialize table for bulk insert
11288    g_rsv_qty_node_level.delete;
11289    g_rsv_qty_revision.delete;
11290    g_rsv_qty_lot_number.delete;
11291    g_rsv_qty_subinventory_code.delete;
11292    g_rsv_qty_locator_id.delete;
11293    g_rsv_qty_cost_group_id.delete;
11294    g_rsv_qty_lpn_id.delete;
11295    g_rsv_qty_qoh.delete;
11296    g_rsv_qty_atr.delete;
11297    g_rsv_qty_sqoh.delete;
11298    g_rsv_qty_satr.delete;
11299    g_rsv_qty_counter := 0;
11300    -- call recursive function on current node
11301    -- invConv change : added 3rd parameter
11302    traverse(l_item_node, NULL, NULL);
11303 
11304    -- Bug 8593965: Using bulk insert to test the performance
11305    if g_debug = 1 then
11306       print_debug('insert into mtl_rsv_quantities_temp, item='||g_rootinfos(l_root_id).inventory_item_id);
11307    end if;
11308 
11309    l_prev_lot_number := '@@@@';
11310    l_prev_grade_code := '@@@@';
11311 
11312    FOR i in 1..g_rsv_qty_counter
11313    LOOP
11314       IF (g_rsv_qty_lot_number(i) IS NOT NULL) THEN
11315         IF (l_prev_lot_number <> g_rsv_qty_lot_number(i) ) then
11316           OPEN Get_Grade_From_Lot( g_rootinfos(l_root_id).organization_id
11317                                 , g_rootinfos(l_root_id).inventory_item_id
11318                                 , g_rsv_qty_lot_number(i));
11319           FETCH Get_Grade_From_Lot
11320           -- Bug 8498256, Commented the below line as we can not keep overwriting
11321           -- the global variable which is used later in the build_tree procedure */
11322           -- INTO g_rootinfos(l_root_id).grade_code;
11323           INTO g_rsv_qty_grade_code(i); --l_grade_code;
11324 
11325           l_prev_grade_code := g_rsv_qty_grade_code(i);
11326            l_prev_lot_number := g_rsv_qty_lot_number(i);
11327           CLOSE Get_Grade_From_Lot;
11328         ELSE
11329           g_rsv_qty_grade_code(i) := l_prev_grade_code;
11330         END IF;
11331       ELSE
11332         g_rsv_qty_grade_code(i) := NULL;
11333       END IF;
11334    END LOOP;
11335 
11336    --insert into table
11337    FORALL i in 1..g_rsv_qty_counter
11338      INSERT INTO MTL_RSV_QUANTITIES_TEMP (
11339           organization_id
11340          ,inventory_item_id
11341          ,node_level
11342          ,revision
11343          ,lot_number
11344          ,subinventory_code
11345          ,locator_id
11346          ,grade_code
11347          ,cost_group_id
11348          ,lpn_id
11349          ,qoh
11350          ,atr
11351          ,sqoh
11352          ,satr
11353    ) VALUES (
11354           g_rootinfos(l_root_id).organization_id
11355          ,g_rootinfos(l_root_id).inventory_item_id
11356          ,g_rsv_qty_node_level(i)
11357          ,g_rsv_qty_revision(i)
11358          ,g_rsv_qty_lot_number(i)
11359          ,g_rsv_qty_subinventory_code(i)
11360          ,g_rsv_qty_locator_id(i)
11361          ,g_rsv_qty_grade_code(i) -- l_grade_code   --g_rootinfos(l_root_id).grade_code
11362          ,g_rsv_qty_cost_group_id(i)
11363          ,g_rsv_qty_lpn_id(i)
11364          ,g_rsv_qty_qoh(i)
11365          ,g_rsv_qty_atr(i)
11366          ,g_rsv_qty_sqoh(i)
11367          ,g_rsv_qty_satr(i)
11368    );
11369    --END LOOP;
11370 
11371    -- return status
11372    x_return_status := fnd_api.g_ret_sts_success;
11373    IF g_debug = 1 THEN
11374       print_debug(l_api_name || ' Exited with status = '||x_return_status,9);
11375       print_debug(' ',9);
11376    END IF;
11377 EXCEPTION
11378    WHEN OTHERS THEN
11379       x_return_status := fnd_api.g_ret_sts_unexp_error;
11380       if g_debug = 1 then
11381         print_debug('prepare_rsv_qty error msg: '||SQLERRM,9);
11382       end if;
11383 END prepare_reservation_quantities;
11384 
11385 -- Get_Total_QOH
11386 --   This API returns the TQOH, or total quantity on hand.
11387 --   This value reflects any negative balances for this
11388 --   item in the organization.  The Total QOH is the minimum
11389 --   of the current node's QOH and all ancestor nodes' QOH.
11390 --   For example,
11391 --   Consider 2 locators in the EACH subinventory:
11392 --   E.1.1 has 10 onhand
11393 --   E.1.2 has -20 onhand
11394 --   Thus, the subinventory Each has -10 onhand.
11395 --
11396 --   Where calling query_tree, qoh for E.1.1 = 10.
11397 --   However, when calling get_total_qoh, the TQOH
11398 --   for E.1.1 = -10, reflecting the value at the subinventory level.
11399 --
11400 --   This procedure is used by the inventory transaction forms.
11401 
11402 PROCEDURE get_total_qoh
11403    (  x_return_status        OUT NOCOPY VARCHAR2
11404    ,  x_msg_count            OUT NOCOPY NUMBER
11405    ,  x_msg_data             OUT NOCOPY VARCHAR2
11406    ,  p_tree_id              IN  INTEGER
11407    ,  p_revision             IN  VARCHAR2
11408    ,  p_lot_number           IN  VARCHAR2
11409    ,  p_subinventory_code    IN  VARCHAR2
11410    ,  p_locator_id           IN  NUMBER
11411    ,  p_cost_group_id        IN  NUMBER
11412    ,  x_tqoh                 OUT NOCOPY NUMBER
11413    ,  p_lpn_id               IN  NUMBER
11414    ) IS
11415 
11416 l_stqoh NUMBER;
11417 
11418 BEGIN
11419 
11420 inv_quantity_tree_pvt.get_total_qoh
11421            ( x_return_status     => x_return_status
11422            , x_msg_count         => x_msg_count
11423            , x_msg_data          => x_msg_data
11424            , p_tree_id           => p_tree_id
11425            , p_revision          => p_revision
11426            , p_lot_number        => p_lot_number
11427            , p_subinventory_code => p_subinventory_code
11428            , p_locator_id        => p_locator_id
11429            , p_cost_group_id     => p_cost_group_id
11430            , x_tqoh              => x_tqoh
11431            , x_stqoh             => l_stqoh
11432            , p_lpn_id            => p_lpn_id) ;
11433 
11434 END get_total_qoh;
11435 
11436 PROCEDURE get_total_qoh
11437    (  x_return_status        OUT NOCOPY VARCHAR2
11438    ,  x_msg_count            OUT NOCOPY NUMBER
11439    ,  x_msg_data             OUT NOCOPY VARCHAR2
11440    ,  p_tree_id              IN  INTEGER
11441    ,  p_revision             IN  VARCHAR2
11442    ,  p_lot_number           IN  VARCHAR2
11443    ,  p_subinventory_code    IN  VARCHAR2
11444    ,  p_locator_id           IN  NUMBER
11445    ,  p_cost_group_id        IN  NUMBER
11446    ,  x_tqoh                 OUT NOCOPY NUMBER
11447    ,  x_stqoh                OUT NOCOPY NUMBER
11448    ,  p_lpn_id               IN  NUMBER
11449    ) IS
11450 
11451    l_found BOOLEAN;
11452    l_return_status VARCHAR2(1) := fnd_api.g_ret_sts_success;
11453    l_node_index NUMBER;
11454    l_tqoh NUMBER;
11455    l_stqoh NUMBER;
11456    l_loop_index NUMBER;
11457    l_root_id NUMBER;
11458    l_is_reservable_sub BOOLEAN;
11459    l_lpn_id NUMBER;
11460    l_sub_index NUMBER := NULL;
11461    l_api_name VARCHAR2(30) := 'GET_TOTAL_QOH';
11462 
11463 BEGIN
11464 
11465    IF g_debug = 1 THEN
11466       print_debug(l_api_name || ' Entered',9);
11467    END IF;
11468 
11469    l_root_id := g_demand_info(p_tree_id).root_id;
11470    print_debug(' in get_total_qoh, tree='||p_tree_id||', root_id='||l_root_id);
11471    print_debug(' ... org_id='||g_rootinfos(l_root_id).organization_id||', item_id='||g_rootinfos(l_root_id).inventory_item_id||'.');
11472    print_debug(' ... lot='||substr(p_lot_number,1,10)||', subInv='||p_subinventory_code||', loct='||p_locator_id||'.');
11473 
11474    -- find the node indicated by the parameters
11475    l_found := find_tree_node(
11476                       x_return_status      => l_return_status
11477                     , p_tree_id            => l_root_id
11478                     , p_revision           => p_revision
11479                     , p_lot_number         => p_lot_number
11480                     , p_subinventory_code  => p_subinventory_code
11481                     , p_is_reservable_sub  => NULL
11482                     , p_locator_id         => p_locator_id
11483                     , x_node_index         => l_node_index
11484                     , p_cost_group_id      => p_cost_group_id
11485                     , p_lpn_id             => p_lpn_id
11486                     );
11487 
11488    IF l_return_status = fnd_api.g_ret_sts_error THEN
11489       RAISE fnd_api.g_exc_error;
11490    End IF ;
11491 
11492 
11493    IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11494       RAISE fnd_api.g_exc_unexpected_error;
11495    End IF;
11496 
11497 
11498    IF l_found = FALSE THEN
11499       fnd_message.set_name('INV','INV-Cannot find node');
11500       fnd_message.set_token('ROUTINE', 'Find_Tree_Node');
11501       fnd_msg_pub.ADD;
11502       RAISE fnd_api.g_exc_unexpected_error;
11503    End IF;
11504 
11505    print_debug('get_total_qoh: node found... node_index='||l_node_index||' level='||g_nodes(l_node_index).node_level||'<>'||g_item_level);
11506 
11507    --Bug 3943215.
11508    --   The logic added here is synchronous with computation logic of runtine att in query_tree.
11509    --   The value returned by this procedure is compared to the runtime att in INVMTXFH.pld
11510 
11511    /*********************************************************************
11512    * Logic for finding TQOH:
11513    *
11514    * If Node is at Sub level or below
11515    *    Get reservable for Sub
11516    *    If Sub is not reservable
11517    *       TQOH = QOH of node;
11518    *    else
11519    *       TQOH = min(QOH of node, RQOH of ancestor nodes)
11520    * Else (node above sub level)
11521    *    TQOH = min(QOH of node, QOH of ancestor nodes)
11522    *********************************************************************/
11523 
11524    IF g_nodes(l_node_index).node_level IN (g_sub_level, g_locator_level,g_lpn_level, g_cost_group_level)
11525      AND (p_subinventory_code IS NOT NULL) THEN
11526 
11527       l_sub_index := get_ancestor_sub(l_node_index);
11528       l_is_reservable_sub := g_nodes(l_sub_index).is_reservable_sub;
11529 
11530       IF NOT l_is_reservable_sub THEN
11531          l_tqoh :=  g_nodes(l_node_index).qoh;
11532          l_stqoh :=  g_nodes(l_node_index).sqoh;
11533       ELSE
11534          -- loop through all ancestors, setting l_tqoh to the minimum rqoh
11535          l_tqoh := g_nodes(l_node_index).qoh;
11536          l_stqoh :=  g_nodes(l_node_index).sqoh;
11537          IF g_nodes(l_node_index).node_level <> g_item_level THEN
11538             l_loop_index := g_nodes(l_node_index).parent_index;
11539             LOOP
11540                IF l_tqoh > g_nodes(l_loop_index).rqoh Then
11541                   l_tqoh := g_nodes(l_loop_index).rqoh;
11542                   l_stqoh := g_nodes(l_loop_index).srqoh;
11543                END IF;
11544                EXIT WHEN g_nodes(l_loop_index).node_level = g_item_level;
11545                l_loop_index :=  g_nodes(l_loop_index).parent_index;
11546             END LOOP;
11547          END IF;
11548       END IF;
11549    ELSE
11550       --org, item or lot level
11551       l_tqoh := g_nodes(l_node_index).qoh;
11552       l_stqoh := g_nodes(l_node_index).sqoh;
11553       IF g_nodes(l_node_index).node_level <> g_item_level THEN
11554          l_loop_index := g_nodes(l_node_index).parent_index;
11555          LOOP
11556             IF l_tqoh > g_nodes(l_loop_index).qoh Then
11557                l_tqoh := g_nodes(l_loop_index).qoh;
11558                l_stqoh := g_nodes(l_node_index).sqoh;
11559             END IF;
11560             EXIT WHEN g_nodes(l_loop_index).node_level = g_item_level;
11561             l_loop_index :=  g_nodes(l_loop_index).parent_index;
11562          END LOOP;
11563       END IF;
11564    END IF;
11565 
11566    x_tqoh  := l_tqoh;
11567 
11568    -- check whether the item is DUOM
11569    IF (g_rootinfos(l_root_id).is_DUOM_control = FALSE)
11570    THEN
11571       print_debug(' root_id='||l_root_id||', tree_id='||p_tree_id||', DUOM_control=FALSE tqoh='||x_tqoh);
11572       x_stqoh := NULL;
11573    ELSE
11574       print_debug(' root_id='||l_root_id||', tree_id='||p_tree_id||', DUOM_control=TRUE tqoh='||x_tqoh||', stqoh='||l_stqoh);
11575       x_stqoh := l_stqoh;
11576    END IF;
11577 
11578    x_return_status := l_return_status;
11579 
11580    IF g_debug = 1 THEN
11581       print_debug(l_api_name || ' Exited with status = '||l_return_status||' tqoh='||l_tqoh,9);
11582       print_debug(' ',9);
11583    END IF;
11584 
11585 EXCEPTION
11586 
11587    WHEN fnd_api.g_exc_error THEN
11588       x_return_status := fnd_api.g_ret_sts_error;
11589 
11590       --  Get message count and data
11591       fnd_msg_pub.count_and_get
11592          ( p_count => x_msg_count
11593          , p_data  => x_msg_data);
11594 
11595    WHEN fnd_api.g_exc_unexpected_error THEN
11596       x_return_status := fnd_api.g_ret_sts_unexp_error ;
11597 
11598       --  Get message count and data
11599       fnd_msg_pub.count_and_get
11600          ( p_count  => x_msg_count
11601          , p_data   => x_msg_data);
11602 
11603    WHEN OTHERS THEN
11604       x_return_status := fnd_api.g_ret_sts_unexp_error ;
11605 
11606 
11607       IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
11608         THEN
11609          fnd_msg_pub.add_exc_msg
11610            ( g_pkg_name
11611            , 'get_total_qoh');
11612       END IF;
11613       --  Get message count and data
11614       fnd_msg_pub.count_and_get
11615           (  p_count  => x_msg_count
11616            , p_data   => x_msg_data);
11617 END get_total_qoh;
11618 
11619 --
11620 -- Procedure
11621 --   do_check_pvt
11622 -- Description
11623 --   Check quantity violation for a given base tree
11624 --
11625 --  Version
11626 --   Current version           1.0
11627 --   Initial version           1.0
11628 --
11629 -- Input parameters:
11630 --   p_api_version_number      standard input parameter
11631 --   p_init_msg_lst            standard input parameter
11632 --   p_tree_id                 tree id
11633 --
11634 -- Output parameters:
11635 --   x_return_status           standard output parameter
11636 --   x_msg_count               standard output parameter
11637 --   x_msg_data                standard output parameter
11638 --   x_no_violation            true if no violation, false otherwise
11639 --
11640 PROCEDURE do_check_pvt
11641   (    p_api_version_number  IN  NUMBER
11642      , p_init_msg_lst        IN  VARCHAR2 DEFAULT fnd_api.g_false
11643      , x_return_status       OUT NOCOPY VARCHAR2
11644      , x_msg_count           OUT NOCOPY NUMBER
11645      , x_msg_data            OUT NOCOPY VARCHAR2
11646      , p_tree_id             IN  INTEGER
11647      , x_no_violation        OUT NOCOPY BOOLEAN
11648      ) IS
11649    l_api_version_number    CONSTANT NUMBER       := 1.0;
11650    l_api_name              CONSTANT VARCHAR2(30) := 'Do_Check_Pvt';
11651    l_return_status         VARCHAR2(1) := fnd_api.g_ret_sts_success;
11652    l_no_violation          BOOLEAN;
11653 BEGIN
11654   print_debug('Entering do_check_pvt. tree_id='||p_tree_id);
11655 
11656   --  Standard call to check for call compatibility
11657    IF NOT fnd_api.compatible_api_call(l_api_version_number
11658                                       , p_api_version_number
11659                                       , l_api_name
11660                                       , G_PKG_NAME
11661                                       ) THEN
11662       RAISE fnd_api.g_exc_unexpected_error;
11663    END IF;
11664 
11665    --  Initialize message list.
11666    IF fnd_api.to_boolean(p_init_msg_lst) THEN
11667       fnd_msg_pub.initialize;
11668    END IF;
11669 
11670    -- check if tree id is valid
11671    IF is_tree_valid(p_tree_id) = FALSE THEN
11672       fnd_message.set_name('INV', 'INV-Qtyroot not found');
11673       fnd_message.set_token('ROUTINE', 'Do_Check');
11674       fnd_msg_pub.ADD;
11675       RAISE fnd_api.g_exc_unexpected_error;
11676    END IF;
11677 
11678 
11679    zero_tree_node(l_return_status, g_rootinfos(p_tree_id).item_node_index);
11680    IF l_return_status = fnd_api.g_ret_sts_error THEN
11681       RAISE fnd_api.g_exc_error;
11682    End IF ;
11683 
11684    IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11685       RAISE fnd_api.g_exc_unexpected_error;
11686    End IF;
11687 
11688    --lock the tree - do_check must acquire tree lock before building tree
11689    print_debug('in do_check_pvt, calling lock_tree ');
11690    lock_tree(
11691               p_api_version_number  => 1.0
11692             , p_init_msg_lst        => fnd_api.g_false
11693             , x_return_status       => l_return_status
11694             , x_msg_count           => x_msg_count
11695             , x_msg_data            => x_msg_data
11696        , p_organization_id     => g_rootinfos(p_tree_id).organization_id
11697        , p_inventory_item_id   => g_rootinfos(p_tree_id).inventory_item_id
11698      );
11699 
11700    IF l_return_status = fnd_api.g_ret_sts_error THEN
11701       RAISE fnd_api.g_exc_error;
11702    END IF ;
11703 
11704    IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11705       RAISE fnd_api.g_exc_unexpected_error;
11706    END IF;
11707 
11708    print_debug('in do_check_pvt, calling build_tree for tree_id='||p_tree_id);
11709    build_tree(l_return_status, p_tree_id);
11710    print_debug('in do_check_pvt, after build_tree... return='||l_return_status);
11711    IF l_return_status = fnd_api.g_ret_sts_error THEN
11712       RAISE fnd_api.g_exc_error;
11713    End IF ;
11714 
11715    IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11716       RAISE fnd_api.g_exc_unexpected_error;
11717    End IF;
11718 
11719    if g_rootinfos(p_tree_id).neg_inv_allowed
11720    then
11721       print_debug('in do_check_pvt, calling check_node_violation for item_node_index='||g_rootinfos(p_tree_id).item_node_index
11722          ||', tree_mode='||g_rootinfos(p_tree_id).tree_mode||', neg_inv_allowed=TRUE');
11723    else
11724       print_debug('in do_check_pvt, calling check_node_violation for item_node_index='||g_rootinfos(p_tree_id).item_node_index
11725          ||', tree_mode='||g_rootinfos(p_tree_id).tree_mode||', neg_inv_allowed=FALSE');
11726    end if;
11727 
11728    l_no_violation :=check_node_violation(
11729        x_return_status     => l_return_status
11730       , p_node_index       => g_rootinfos(p_tree_id).item_node_index
11731       , p_tree_mode        => g_rootinfos(p_tree_id).tree_mode
11732       , p_negative_allowed => g_rootinfos(p_tree_id).neg_inv_allowed
11733       , p_item_node_index  => g_rootinfos(p_tree_id).item_node_index
11734       );
11735 
11736    print_debug('in do_check_pvt, after check_node_violation... return='||l_return_status);
11737    IF l_return_status = fnd_api.g_ret_sts_error THEN
11738       RAISE fnd_api.g_exc_error;
11739    END IF ;
11740 
11741    IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11742       RAISE fnd_api.g_exc_unexpected_error;
11743    END IF;
11744 
11745    x_no_violation := l_no_violation;
11746    x_return_status := l_return_status;
11747 
11748    print_debug('Normal end of do_check_pvt');
11749 EXCEPTION
11750 
11751     WHEN fnd_api.g_exc_error THEN
11752         x_return_status := fnd_api.g_ret_sts_error;
11753 
11754         --  Get message count and data
11755         fnd_msg_pub.count_and_get
11756           (  p_count => x_msg_count
11757            , p_data  => x_msg_data
11758            );
11759 
11760    WHEN fnd_api.g_exc_unexpected_error THEN
11761         x_return_status := fnd_api.g_ret_sts_unexp_error ;
11762 
11763         --  Get message count and data
11764         fnd_msg_pub.count_and_get
11765           (  p_count  => x_msg_count
11766            , p_data   => x_msg_data
11767             );
11768 
11769     WHEN OTHERS THEN
11770         x_return_status := fnd_api.g_ret_sts_unexp_error ;
11771 
11772         IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
11773           THEN
11774            fnd_msg_pub.add_exc_msg
11775              (  g_pkg_name
11776               , l_api_name
11777               );
11778         END IF;
11779 
11780         --  Get message count and data
11781         fnd_msg_pub.count_and_get
11782           (  p_count  => x_msg_count
11783            , p_data   => x_msg_data
11784             );
11785 
11786 END do_check_pvt;
11787 
11788 
11789 --
11790 -- Procedure
11791 --   do_check
11792 -- Description
11793 --   Check quantity violation
11794 --
11795 --  Version
11796 --   Current version           1.0
11797 --   Initial version           1.0
11798 --
11799 -- Input parameters:
11800 --   p_api_version_number      standard input parameter
11801 --   p_init_msg_lst            standard input parameter
11802 --
11803 -- Output parameters:
11804 --   x_return_status           standard output parameter
11805 --   x_msg_count               standard output parameter
11806 --   x_msg_data                standard output parameter
11807 --   x_no_violation            true if no violation, false otherwise
11808 --
11809 PROCEDURE do_check
11810   (    p_api_version_number  IN  NUMBER
11811      , p_init_msg_lst        IN  VARCHAR2 DEFAULT fnd_api.g_false
11812      , x_return_status       OUT NOCOPY VARCHAR2
11813      , x_msg_count           OUT NOCOPY NUMBER
11814      , x_msg_data            OUT NOCOPY VARCHAR2
11815      , p_tree_id             IN  INTEGER
11816      , x_no_violation        OUT NOCOPY BOOLEAN
11817      ) IS
11818    l_api_version_number    CONSTANT NUMBER       := 1.0;
11819    l_api_name              CONSTANT VARCHAR2(30) := 'DO_CHECK';
11820    l_return_status         VARCHAR2(1) := fnd_api.g_ret_sts_success;
11821    l_no_violation          BOOLEAN;
11822    l_root_id         INTEGER;
11823 BEGIN
11824    IF g_debug = 1 THEN
11825       print_debug(l_api_name || ' Entered',9);
11826    END IF;
11827 
11828    print_debug('Entering do_check, tree_id='||p_tree_id, 9);
11829 
11830   --  Standard call to check for call compatibility
11831    IF NOT fnd_api.compatible_api_call(l_api_version_number
11832                                       , p_api_version_number
11833                                       , l_api_name
11834                                       , G_PKG_NAME
11835                                       ) THEN
11836       RAISE fnd_api.g_exc_unexpected_error;
11837    END IF;
11838 
11839    --  Initialize message list.
11840    IF fnd_api.to_boolean(p_init_msg_lst) THEN
11841       fnd_msg_pub.initialize;
11842    END IF;
11843 
11844    --call do_check on the base tree
11845    l_root_id := g_demand_info(p_tree_id).root_id;
11846 
11847    do_check_pvt(
11848               p_api_version_number  => 1.0
11849             , p_init_msg_lst        => fnd_api.g_false
11850             , x_return_status       => l_return_status
11851             , x_msg_count           => x_msg_count
11852             , x_msg_data            => x_msg_data
11853             , p_tree_id             => l_root_id
11854             , x_no_violation        => l_no_violation
11855             );
11856    print_debug('In do_check, after do_check_pvt, return='||l_return_status);
11857    IF l_return_status = fnd_api.g_ret_sts_error THEN
11858             RAISE fnd_api.g_exc_error;
11859    END IF ;
11860 
11861    IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11862             RAISE fnd_api.g_exc_unexpected_error;
11863    END IF;
11864 
11865    -- Bug 7340567, printing the tree to verify node violation
11866    print_tree(p_tree_id);
11867 
11868    x_no_violation := l_no_violation;
11869    x_return_status := l_return_status;
11870 
11871    IF g_debug = 1 THEN
11872      if l_no_violation then
11873        print_debug(l_api_name || ' Exited with no violation detected',9);
11874      else
11875        print_debug(l_api_name || ' Exited with violation detected',9);
11876      end if;
11877      print_debug(' ',9);
11878    END IF;
11879 EXCEPTION
11880 
11881     WHEN fnd_api.g_exc_error THEN
11882         x_return_status := fnd_api.g_ret_sts_error;
11883 
11884         --  Get message count and data
11885         fnd_msg_pub.count_and_get
11886           (  p_count => x_msg_count
11887            , p_data  => x_msg_data
11888            );
11889 
11890    WHEN fnd_api.g_exc_unexpected_error THEN
11891         x_return_status := fnd_api.g_ret_sts_unexp_error ;
11892 
11893         --  Get message count and data
11894         fnd_msg_pub.count_and_get
11895           (  p_count  => x_msg_count
11896            , p_data   => x_msg_data
11897             );
11898 
11899     WHEN OTHERS THEN
11900         x_return_status := fnd_api.g_ret_sts_unexp_error ;
11901 
11902         IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
11903           THEN
11904            fnd_msg_pub.add_exc_msg
11905              (  g_pkg_name
11906               , l_api_name
11907               );
11908         END IF;
11909 
11910         --  Get message count and data
11911         fnd_msg_pub.count_and_get
11912           (  p_count  => x_msg_count
11913            , p_data   => x_msg_data
11914             );
11915 
11916 END do_check;
11917 
11918 
11919 --
11920 -- Procedure
11921 --   do_check
11922 -- Description
11923 --   Check quantity violation
11924 --
11925 --  Version
11926 --   Current version           1.0
11927 --   Initial version           1.0
11928 --
11929 -- Input parameters:
11930 --   p_api_version_number      standard input parameter
11931 --   p_init_msg_lst            standard input parameter
11932 --
11933 -- Output parameters:
11934 --   x_return_status           standard output parameter
11935 --   x_msg_count               standard output parameter
11936 --   x_msg_data                standard output parameter
11937 --   x_no_violation            true if no violation, false otherwise
11938 --
11939 PROCEDURE do_check
11940   (    p_api_version_number  IN  NUMBER
11941      , p_init_msg_lst        IN  VARCHAR2
11942      , x_return_status       OUT NOCOPY VARCHAR2
11943      , x_msg_count           OUT NOCOPY NUMBER
11944      , x_msg_data            OUT NOCOPY VARCHAR2
11945      , x_no_violation        OUT NOCOPY BOOLEAN
11946      ) IS
11947    l_api_version_number    CONSTANT NUMBER       := 1.0;
11948    l_api_name              CONSTANT VARCHAR2(30) := 'DO_CHECK_ALL';
11949    l_return_status         VARCHAR2(1) := fnd_api.g_ret_sts_success;
11950    l_no_violation          BOOLEAN;
11951    l_root_id         NUMBER;
11952    l_org_id       NUMBER;
11953    l_item_id         NUMBER;
11954    l_lot_ctrl        NUMBER;
11955    l_line_id         NUMBER;
11956    l_root_id_tmp           NUMBER;
11957 
11958    CURSOR C1 is
11959    SELECT root_id
11960    FROM   mtl_do_check_temp
11961    ORDER BY organization_id, inventory_item_id;
11962 BEGIN
11963 
11964    IF g_debug = 1 THEN
11965       print_debug(l_api_name || ' Entered',9);
11966    END IF;
11967 
11968    --  Standard call to check for call compatibility
11969    IF NOT fnd_api.compatible_api_call(l_api_version_number
11970                                       , p_api_version_number
11971                                       , l_api_name
11972                                       , G_PKG_NAME
11973                                       ) THEN
11974       RAISE fnd_api.g_exc_unexpected_error;
11975    END IF;
11976 
11977    --  Initialize message list.
11978    IF fnd_api.to_boolean(p_init_msg_lst) THEN
11979       fnd_msg_pub.initialize;
11980    END IF;
11981 
11982    for l_loop_index IN 1..g_all_roots_counter LOOP
11983       l_root_id := g_all_roots(l_loop_index).root_id;
11984       l_org_id := g_rootinfos(l_root_id).organization_id;
11985       l_item_id := g_rootinfos(l_root_id).inventory_item_id;
11986       if (g_rootinfos(l_root_id).is_lot_control) THEN
11987          l_lot_ctrl := 2;
11988       else
11989          l_lot_ctrl := 1;
11990       end if;
11991       l_line_id := g_rootinfos(l_root_id).demand_source_line_id;
11992 
11993       --Now insert into temp table;
11994       insert into mtl_do_check_temp
11995          (   ROOT_ID
11996             ,ORGANIZATION_ID
11997             ,INVENTORY_ITEM_ID
11998             ,LOT_CONTROL
11999             ,LINE_ID)
12000          values
12001          (   l_root_id
12002             ,l_org_id
12003             ,l_item_id
12004             ,l_lot_ctrl
12005             ,l_line_id);
12006    end loop;
12007 
12008    open c1;
12009    loop
12010       fetch c1 into l_root_id_tmp;
12011       EXIT WHEN c1%NOTFOUND;
12012 
12013       IF is_tree_valid(l_root_id_tmp) THEN
12014          do_check_pvt( p_api_version_number  => 1.0
12015                       ,p_init_msg_lst        => fnd_api.g_false
12016                       ,x_return_status       => l_return_status
12017                       ,x_msg_count           => x_msg_count
12018                       ,x_msg_data            => x_msg_data
12019                       ,p_tree_id             => l_root_id_tmp
12020                       ,x_no_violation        => l_no_violation);
12021          IF l_return_status = fnd_api.g_ret_sts_error THEN
12022             RAISE fnd_api.g_exc_error;
12023          END IF ;
12024 
12025          IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12026             RAISE fnd_api.g_exc_unexpected_error;
12027          END IF;
12028 
12029          IF l_no_violation = FALSE THEN
12030             --utl_debug1('do_check_pvt returns false to do_check');
12031             EXIT;
12032          END IF;
12033       END IF;
12034    END LOOP;
12035 
12036    x_no_violation := l_no_violation;
12037    x_return_status := l_return_status;
12038 
12039    IF g_debug = 1 THEN
12040      IF l_no_violation THEN
12041        print_debug(l_api_name || ' Exited with no violation detected',9);
12042      ELSE
12043        print_debug(l_api_name || ' Exited with violation detected',9);
12044      END IF;
12045      print_debug(' ',9);
12046    END IF;
12047 
12048 EXCEPTION
12049 
12050     WHEN fnd_api.g_exc_error THEN
12051         x_return_status := fnd_api.g_ret_sts_error;
12052 
12053         --  Get message count and data
12054         fnd_msg_pub.count_and_get
12055           (  p_count => x_msg_count
12056            , p_data  => x_msg_data
12057            );
12058 
12059    WHEN fnd_api.g_exc_unexpected_error THEN
12060         x_return_status := fnd_api.g_ret_sts_unexp_error ;
12061 
12062         --  Get message count and data
12063         fnd_msg_pub.count_and_get
12064           (  p_count  => x_msg_count
12065            , p_data   => x_msg_data
12066             );
12067 
12068     WHEN OTHERS THEN
12069         x_return_status := fnd_api.g_ret_sts_unexp_error ;
12070 
12071         IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
12072           THEN
12073            fnd_msg_pub.add_exc_msg
12074              (  g_pkg_name
12075               , l_api_name
12076               );
12077         END IF;
12078 
12079         --  Get message count and data
12080         fnd_msg_pub.count_and_get
12081           (  p_count  => x_msg_count
12082            , p_data   => x_msg_data
12083             );
12084 
12085 END do_check;
12086 
12087 --
12088 -- Procedure
12089 --   free_tree_pvt
12090 -- Description
12091 --   free the tree when it is no longer needed
12092 --   This takes the base tree id
12093 --
12094 --  Version
12095 --   Current version           1.0
12096 --   Initial version           1.0
12097 --
12098 -- Input parameters:
12099 --   p_api_version_number      standard input parameter
12100 --   p_init_msg_lst            standard input parameter
12101 --   p_tree_id                 tree id
12102 --
12103 -- Output parameters:
12104 --   x_return_status           standard output parameter
12105 --   x_msg_count               standard output parameter
12106 --   x_msg_data                standard output parameter
12107 --
12108 PROCEDURE free_tree_pvt
12109   (    p_api_version_number  IN  NUMBER
12110      , p_init_msg_lst        IN  VARCHAR2 DEFAULT fnd_api.g_false
12111      , x_return_status       OUT NOCOPY VARCHAR2
12112      , x_msg_count           OUT NOCOPY NUMBER
12113      , x_msg_data            OUT NOCOPY VARCHAR2
12114      , p_tree_id             IN  INTEGER
12115      ) IS
12116    l_api_version_number    CONSTANT NUMBER       := 1.0;
12117    l_api_name              CONSTANT VARCHAR2(30) := 'Free_Tree_Pvt';
12118    l_return_status         VARCHAR2(1) := fnd_api.g_ret_sts_success;
12119 BEGIN
12120 
12121   --  Standard call to check for call compatibility
12122    IF NOT fnd_api.compatible_api_call(l_api_version_number
12123                                       , p_api_version_number
12124                                       , l_api_name
12125                                       , G_PKG_NAME
12126                                       ) THEN
12127       RAISE fnd_api.g_exc_unexpected_error;
12128    END IF;
12129 
12130    --  Initialize message list.
12131    IF fnd_api.to_boolean(p_init_msg_lst) THEN
12132       fnd_msg_pub.initialize;
12133    END IF;
12134    invalidate_tree(p_tree_id);
12135 
12136    x_return_status := l_return_status;
12137 
12138 EXCEPTION
12139 
12140     WHEN fnd_api.g_exc_error THEN
12141         x_return_status := fnd_api.g_ret_sts_error;
12142 
12143         --  Get message count and data
12144         fnd_msg_pub.count_and_get
12145           (  p_count => x_msg_count
12146            , p_data  => x_msg_data
12147            );
12148 
12149     WHEN fnd_api.g_exc_unexpected_error THEN
12150         x_return_status := fnd_api.g_ret_sts_unexp_error ;
12151 
12152         --  Get message count and data
12153         fnd_msg_pub.count_and_get
12154           (  p_count  => x_msg_count
12155            , p_data   => x_msg_data
12156             );
12157 
12158     WHEN OTHERS THEN
12159         x_return_status := fnd_api.g_ret_sts_unexp_error ;
12160 
12161         IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
12162           THEN
12163            fnd_msg_pub.add_exc_msg
12164              (  g_pkg_name
12165               , l_api_name
12166               );
12167         END IF;
12168 
12169         --  Get message count and data
12170         fnd_msg_pub.count_and_get
12171           (  p_count  => x_msg_count
12172            , p_data   => x_msg_data
12173             );
12174 
12175 END free_tree_pvt;
12176 
12177 
12178 --
12179 -- Procedure
12180 --   free_tree
12181 -- Description
12182 --   free all the trees
12183 --   This takes the demand info id
12184 --     (tree_id returned by create_tree)
12185 --
12186 --  Version
12187 --   Current version           1.0
12188 --   Initial version           1.0
12189 --
12190 -- Input parameters:
12191 --   p_api_version_number      standard input parameter
12192 --   p_init_msg_lst            standard input parameter
12193 --
12194 -- Output parameters:
12195 --   x_return_status           standard output parameter
12196 --   x_msg_count               standard output parameter
12197 --   x_msg_data                standard output parameter
12198 --
12199 PROCEDURE free_tree
12200   (  p_api_version_number  IN  NUMBER
12201    , p_init_msg_lst        IN  VARCHAR2
12202    , x_return_status       OUT NOCOPY VARCHAR2
12203    , x_msg_count           OUT NOCOPY NUMBER
12204    , x_msg_data            OUT NOCOPY VARCHAR2
12205    , p_tree_id             IN  INTEGER
12206    )
12207 IS
12208 
12209    l_api_version_number    CONSTANT NUMBER       := 1.0;
12210    l_api_name              CONSTANT VARCHAR2(30) := 'FREE_TREE';
12211    l_return_status         VARCHAR2(1) := fnd_api.g_ret_sts_success;
12212    l_root_id               INTEGER;
12213 
12214 BEGIN
12215 
12216    IF g_debug = 1 THEN
12217       print_debug(l_api_name || ' Entered',9);
12218    END IF;
12219 
12220    --  Standard call to check for call compatibility
12221    IF NOT fnd_api.compatible_api_call(l_api_version_number
12222                                       , p_api_version_number
12223                                       , l_api_name
12224                                       , G_PKG_NAME
12225                                       ) THEN
12226       RAISE fnd_api.g_exc_unexpected_error;
12227    END IF;
12228 
12229    --  Initialize message list.
12230    IF fnd_api.to_boolean(p_init_msg_lst) THEN
12231       fnd_msg_pub.initialize;
12232    END IF;
12233 
12234    --call free_tree on base_tree
12235    l_root_id := g_demand_info(p_tree_id).root_id;
12236 
12237    free_tree_pvt(
12238             p_api_version_number => l_api_version_number
12239           , p_init_msg_lst       => fnd_api.g_false
12240           , x_return_status      => l_return_status
12241           , x_msg_count          => x_msg_count
12242           , x_msg_data           => x_msg_data
12243           , p_tree_id            => l_root_id
12244       );
12245 
12246    IF l_return_status = fnd_api.g_ret_sts_error THEN
12247       RAISE fnd_api.g_exc_error;
12248    End IF ;
12249 
12250    IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12251       RAISE fnd_api.g_exc_unexpected_error;
12252    End IF;
12253 
12254 
12255    x_return_status := l_return_status;
12256 
12257 EXCEPTION
12258 
12259     WHEN fnd_api.g_exc_error THEN
12260         x_return_status := fnd_api.g_ret_sts_error;
12261 
12262         --  Get message count and data
12263         fnd_msg_pub.count_and_get
12264           (  p_count => x_msg_count
12265            , p_data  => x_msg_data
12266            );
12267 
12268     WHEN fnd_api.g_exc_unexpected_error THEN
12269         x_return_status := fnd_api.g_ret_sts_unexp_error ;
12270 
12271         --  Get message count and data
12272         fnd_msg_pub.count_and_get
12273           (  p_count  => x_msg_count
12274            , p_data   => x_msg_data
12275             );
12276 
12277     WHEN OTHERS THEN
12278         x_return_status := fnd_api.g_ret_sts_unexp_error ;
12279 
12280         IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
12281           THEN
12282            fnd_msg_pub.add_exc_msg
12283              (  g_pkg_name
12284               , l_api_name
12285               );
12286         END IF;
12287 
12288         --  Get message count and data
12289         fnd_msg_pub.count_and_get
12290           (  p_count  => x_msg_count
12291            , p_data   => x_msg_data
12292             );
12293 
12294 END free_tree;
12295 
12296 
12297 --
12298 -- Procedure
12299 --   free_all
12300 -- Description
12301 --   free all the trees
12302 --
12303 --  Version
12304 --   Current version           1.0
12305 --   Initial version           1.0
12306 --
12307 -- Input parameters:
12308 --   p_api_version_number      standard input parameter
12309 --   p_init_msg_lst            standard input parameter
12310 --
12311 -- Output parameters:
12312 --   x_return_status           standard output parameter
12313 --   x_msg_count               standard output parameter
12314 --   x_msg_data                standard output parameter
12315 --
12316 PROCEDURE free_all
12317   (  p_api_version_number  IN  NUMBER
12318    , p_init_msg_lst        IN  VARCHAR2
12319    , x_return_status       OUT NOCOPY VARCHAR2
12320    , x_msg_count           OUT NOCOPY NUMBER
12321    , x_msg_data            OUT NOCOPY VARCHAR2
12322    )
12323 IS
12324 
12325    l_api_version_number    CONSTANT NUMBER       := 1.0;
12326    l_api_name              CONSTANT VARCHAR2(30) := 'Free_All';
12327    l_return_status         VARCHAR2(1) := fnd_api.g_ret_sts_success;
12328    l_root_id         NUMBER;
12329 
12330 BEGIN
12331 
12332    --  Standard call to check for call compatibility
12333    IF NOT fnd_api.compatible_api_call(l_api_version_number
12334                                       , p_api_version_number
12335                                       , l_api_name
12336                                       , G_PKG_NAME
12337                                       ) THEN
12338       RAISE fnd_api.g_exc_unexpected_error;
12339    END IF;
12340 
12341    --  Initialize message list.
12342    IF fnd_api.to_boolean(p_init_msg_lst) THEN
12343       fnd_msg_pub.initialize;
12344    END IF;
12345 
12346    FOR l_loop_index IN 1..g_all_roots_counter LOOP
12347       l_root_id := g_all_roots(l_loop_index).root_id;
12348       free_tree_pvt(
12349            p_api_version_number => l_api_version_number
12350           ,p_init_msg_lst      => fnd_api.g_false
12351           ,x_return_status => l_return_status
12352      ,x_msg_count    => x_msg_count
12353           ,x_msg_data      => x_msg_data
12354           ,p_tree_id    => l_root_id
12355       );
12356 
12357       IF l_return_status = fnd_api.g_ret_sts_error THEN
12358          RAISE fnd_api.g_exc_error;
12359       End IF ;
12360 
12361       IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12362          RAISE fnd_api.g_exc_unexpected_error;
12363       End IF;
12364 
12365   END LOOP;
12366 
12367    x_return_status := l_return_status;
12368 
12369 EXCEPTION
12370 
12371     WHEN fnd_api.g_exc_error THEN
12372         x_return_status := fnd_api.g_ret_sts_error;
12373 
12374         --  Get message count and data
12375         fnd_msg_pub.count_and_get
12376           (  p_count => x_msg_count
12377            , p_data  => x_msg_data
12378            );
12379 
12380    WHEN fnd_api.g_exc_unexpected_error THEN
12381         x_return_status := fnd_api.g_ret_sts_unexp_error ;
12382 
12383         --  Get message count and data
12384         fnd_msg_pub.count_and_get
12385           (  p_count  => x_msg_count
12386            , p_data   => x_msg_data
12387             );
12388 
12389     WHEN OTHERS THEN
12390         x_return_status := fnd_api.g_ret_sts_unexp_error ;
12391 
12392         IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
12393           THEN
12394            fnd_msg_pub.add_exc_msg
12395              (  g_pkg_name
12396               , l_api_name
12397               );
12398         END IF;
12399 
12400         --  Get message count and data
12401         fnd_msg_pub.count_and_get
12402           (  p_count  => x_msg_count
12403            , p_data   => x_msg_data
12404             );
12405 
12406 END free_all;
12407 
12408 --
12409 -- Procedure
12410 --   mark_all_for_refresh
12411 -- Description
12412 --   marks all existing trees as needing to be rebuilt. Unlike
12413 --   free_tree and clear_quantity_cache, no quantity trees are deleted.
12414 --
12415 --   This API is needed so that the do_check_for_commit procedure in
12416 --   INVRSV3B.pls will still work.  That procedure stores tree_ids in a
12417 --   temp table. When clear_quantity_cache is called, these tree_ids are
12418 --   no longer valid. When this is called instead of clear_quantity_cache,
12419 --   the tree_ids are still valid to be passed to do_check.
12420 --
12421 --
12422 --  Version
12423 --   Current version           1.0
12424 --   Initial version           1.0
12425 --
12426 -- Input parameters:
12427 --   p_api_version_number      standard input parameter
12428 --   p_init_msg_lst            standard input parameter
12429 --
12430 -- Output parameters:
12431 --   x_return_status           standard output parameter
12432 --   x_msg_count               standard output parameter
12433 --   x_msg_data                standard output parameter
12434 --
12435 PROCEDURE mark_all_for_refresh
12436   (  p_api_version_number  IN  NUMBER
12437    , p_init_msg_lst        IN  VARCHAR2
12438    , x_return_status       OUT NOCOPY VARCHAR2
12439    , x_msg_count           OUT NOCOPY NUMBER
12440    , x_msg_data            OUT NOCOPY VARCHAR2
12441    ) IS
12442 
12443    l_api_version_number    CONSTANT NUMBER       := 1.0;
12444    l_api_name              CONSTANT VARCHAR2(30) := 'Mark_All_For_Refresh';
12445    l_return_status         VARCHAR2(1) := fnd_api.g_ret_sts_success;
12446    l_root_id               NUMBER;
12447 
12448 BEGIN
12449 
12450    --  Standard call to check for call compatibility
12451    IF NOT fnd_api.compatible_api_call(l_api_version_number
12452                                       , p_api_version_number
12453                                       , l_api_name
12454                                       , G_PKG_NAME
12455                                       ) THEN
12456       RAISE fnd_api.g_exc_unexpected_error;
12457    END IF;
12458 
12459    --  Initialize message list.
12460    IF fnd_api.to_boolean(p_init_msg_lst) THEN
12461       fnd_msg_pub.initialize;
12462    END IF;
12463 
12464    FOR l_loop_index IN 1..g_all_roots_counter LOOP
12465       l_root_id := g_all_roots(l_loop_index).root_id;
12466       If is_tree_valid(l_root_id) THEN
12467         g_rootinfos(l_root_id).need_refresh := TRUE;
12468       End If;
12469    END LOOP;
12470 
12471 
12472 EXCEPTION
12473 
12474     WHEN fnd_api.g_exc_error THEN
12475         x_return_status := fnd_api.g_ret_sts_error;
12476 
12477         --  Get message count and data
12478         fnd_msg_pub.count_and_get
12479           (  p_count => x_msg_count
12480            , p_data  => x_msg_data
12481            );
12482 
12483    WHEN fnd_api.g_exc_unexpected_error THEN
12484         x_return_status := fnd_api.g_ret_sts_unexp_error ;
12485 
12486         --  Get message count and data
12487         fnd_msg_pub.count_and_get
12488           (  p_count  => x_msg_count
12489            , p_data   => x_msg_data
12490             );
12491 
12492     WHEN OTHERS THEN
12493         x_return_status := fnd_api.g_ret_sts_unexp_error ;
12494 
12495         IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
12496           THEN
12497            fnd_msg_pub.add_exc_msg
12498              (  g_pkg_name
12499               , l_api_name
12500               );
12501         END IF;
12502 
12503         --  Get message count and data
12504         fnd_msg_pub.count_and_get
12505           (  p_count  => x_msg_count
12506            , p_data   => x_msg_data
12507             );
12508 
12509 END mark_all_for_refresh;
12510 
12511 -- Procedure
12512 --   update_quantities
12513 -- Description
12514 --   Create a quantity tree
12515 --
12516 --  Version
12517 --   Current version        1.0
12518 --   Initial version        1.0
12519 --
12520 -- Input parameters:
12521 --   p_api_version_number   standard input parameter
12522 --   p_init_msg_lst         standard input parameter
12523 --   p_tree_id              tree_id
12524 --   p_revision             revision
12525 --   p_lot_number           lot_number
12526 --   p_subinventory_code    subinventory_code
12527 --   p_locator_id           locator_id
12528 --   p_primary_quantity     primary_quantity
12529 --   p_quantity_type
12530 --
12531 -- Output parameters:
12532 --   x_return_status       standard output parameter
12533 --   x_msg_count           standard output parameter
12534 --   x_msg_data            standard output parameter
12535 --   x_tree_id             used later to refer to the same tree
12536 --   x_qoh                 qoh   after the update
12537 --   x_rqoh                rqoh  after the update
12538 --   x_qr                  qr    after the update
12539 --   x_qs                  qs    after the update
12540 --   x_att                 att   after the update
12541 --   x_atr                 atr   after the update
12542 PROCEDURE update_quantities
12543   (  p_api_version_number    IN  NUMBER
12544    , p_init_msg_lst          IN  VARCHAR2
12545    , x_return_status         OUT NOCOPY VARCHAR2
12546    , x_msg_count             OUT NOCOPY NUMBER
12547    , x_msg_data              OUT NOCOPY VARCHAR2
12548    , p_tree_id               IN  INTEGER
12549    , p_revision              IN  VARCHAR2
12550    , p_lot_number            IN  VARCHAR2
12551    , p_subinventory_code     IN  VARCHAR2
12552    , p_locator_id            IN  NUMBER
12553    , p_primary_quantity      IN  NUMBER
12554    , p_quantity_type         IN  INTEGER
12555    , x_qoh                   OUT NOCOPY NUMBER
12556    , x_rqoh                  OUT NOCOPY NUMBER
12557    , x_qr                    OUT NOCOPY NUMBER
12558    , x_qs                    OUT NOCOPY NUMBER
12559    , x_att                   OUT NOCOPY NUMBER
12560    , x_atr                   OUT NOCOPY NUMBER
12561    , p_transfer_subinventory_code IN  VARCHAR2
12562    , p_cost_group_id         IN  NUMBER
12563    , p_containerized         IN  NUMBER
12564    , p_lpn_id                IN  NUMBER
12565    , p_transfer_locator_id   IN  NUMBER
12566      ) IS
12567 
12568 l_secondary_quantity  NUMBER;
12569 l_sqoh    NUMBER;
12570 l_srqoh   NUMBER;
12571 l_sqr     NUMBER;
12572 l_sqs     NUMBER;
12573 l_satt    NUMBER;
12574 l_satr    NUMBER;
12575 BEGIN
12576 
12577 inv_quantity_tree_pvt.update_quantities
12578   (  p_api_version_number    => p_api_version_number
12579    , p_init_msg_lst          => p_init_msg_lst
12580    , x_return_status         => x_return_status
12581    , x_msg_count             => x_msg_count
12582    , x_msg_data              => x_msg_data
12583    , p_tree_id               => p_tree_id
12584    , p_revision              => p_revision
12585    , p_lot_number            => p_lot_number
12586    , p_subinventory_code     => p_subinventory_code
12587    , p_locator_id            => p_locator_id
12588    , p_primary_quantity      => p_primary_quantity
12589    , p_secondary_quantity    => l_secondary_quantity
12590    , p_quantity_type         => p_quantity_type
12591    , x_qoh                   => x_qoh
12592    , x_rqoh                  => x_rqoh
12593    , x_qr                    => x_qr
12594    , x_qs                    => x_qs
12595    , x_att                   => x_att
12596    , x_atr                   => x_atr
12597    , x_sqoh                  => l_sqoh
12598    , x_srqoh                 => l_srqoh
12599    , x_sqr                   => l_sqr
12600    , x_sqs                   => l_sqs
12601    , x_satt                  => l_satt
12602    , x_satr                  => l_satr
12603    , p_transfer_subinventory_code => p_transfer_subinventory_code
12604    , p_cost_group_id         => p_cost_group_id
12605    , p_containerized         => p_containerized
12606    , p_lpn_id                => p_lpn_id
12607    , p_transfer_locator_id   => p_transfer_locator_id);
12608 
12609 END update_quantities;
12610 
12611 PROCEDURE update_quantities
12612   (  p_api_version_number    IN  NUMBER
12613    , p_init_msg_lst          IN  VARCHAR2
12614    , x_return_status         OUT NOCOPY VARCHAR2
12615    , x_msg_count             OUT NOCOPY NUMBER
12616    , x_msg_data              OUT NOCOPY VARCHAR2
12617    , p_tree_id               IN  INTEGER
12618    , p_revision              IN  VARCHAR2
12619    , p_lot_number            IN  VARCHAR2
12620    , p_subinventory_code     IN  VARCHAR2
12621    , p_locator_id            IN  NUMBER
12622    , p_primary_quantity      IN  NUMBER
12623    , p_secondary_quantity    IN  NUMBER
12624    , p_quantity_type         IN  INTEGER
12625    , x_qoh                   OUT NOCOPY NUMBER
12626    , x_rqoh                  OUT NOCOPY NUMBER
12627    , x_qr                    OUT NOCOPY NUMBER
12628    , x_qs                    OUT NOCOPY NUMBER
12629    , x_att                   OUT NOCOPY NUMBER
12630    , x_atr                   OUT NOCOPY NUMBER
12631    , x_sqoh                  OUT NOCOPY NUMBER
12632    , x_srqoh                 OUT NOCOPY NUMBER
12633    , x_sqr                   OUT NOCOPY NUMBER
12634    , x_sqs                   OUT NOCOPY NUMBER
12635    , x_satt                  OUT NOCOPY NUMBER
12636    , x_satr                  OUT NOCOPY NUMBER
12637    , p_transfer_subinventory_code IN  VARCHAR2
12638    , p_cost_group_id         IN  NUMBER
12639    , p_containerized         IN  NUMBER
12640    , p_lpn_id                IN  NUMBER
12641    , p_transfer_locator_id   IN  NUMBER
12642      ) IS
12643       l_api_version_number   CONSTANT NUMBER       := 1.0;
12644       l_api_name             CONSTANT VARCHAR2(30) := 'UPDATE_QUANTITIES';
12645       l_return_status        VARCHAR2(1) := fnd_api.g_ret_sts_success;
12646       l_root_id        INTEGER;
12647 BEGIN
12648    IF g_debug = 1 THEN
12649       print_debug(l_api_name || ' Entered',9);
12650    END IF;
12651 
12652    print_debug('Entering update_quantities. primQty='||p_primary_quantity||', secQty='||p_secondary_quantity);
12653 
12654    --  Standard call to check for call compatibility
12655    IF NOT fnd_api.compatible_api_call(l_api_version_number
12656                                       , p_api_version_number
12657                                       , l_api_name
12658                                       , G_PKG_NAME
12659                                       ) THEN
12660       RAISE fnd_api.g_exc_unexpected_error;
12661    END IF;
12662 
12663    --  Initialize message list.
12664    IF fnd_api.to_boolean(p_init_msg_lst) THEN
12665       fnd_msg_pub.initialize;
12666    END IF;
12667 
12668    l_root_id := g_demand_info(p_tree_id).root_id;
12669    -- check if tree id is valid
12670    IF is_tree_valid(l_root_id) = FALSE THEN
12671       fnd_message.set_name('INV', 'INV-Qtyroot not found');
12672       fnd_message.set_token('ROUTINE', 'Update_Quantities');
12673       fnd_msg_pub.ADD;
12674       RAISE fnd_api.g_exc_unexpected_error;
12675    END IF;
12676 
12677    print_debug('in update_quantities, calling add_quantities qty1='||p_primary_quantity||', qty2='||p_secondary_quantity||'.');
12678    add_quantities(
12679         x_return_status     => l_return_status
12680       , p_tree_id           => l_root_id
12681       , p_revision          => p_revision
12682       , p_lot_number        => p_lot_number
12683       , p_subinventory_code => p_subinventory_code
12684       , p_is_reservable_sub => NULL
12685       , p_locator_id        => p_locator_id
12686       , p_primary_quantity  => p_primary_quantity
12687       , p_secondary_quantity=> p_secondary_quantity
12688       , p_quantity_type     => p_quantity_type
12689       , p_set_check_mark    => TRUE
12690       , p_cost_group_id     => p_cost_group_id
12691       , p_lpn_id            => p_lpn_id
12692       );
12693 
12694    IF l_return_status = fnd_api.g_ret_sts_error THEN
12695       RAISE fnd_api.g_exc_error;
12696    End IF ;
12697 
12698    IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12699       RAISE fnd_api.g_exc_unexpected_error;
12700    End IF;
12701 
12702    -- query the quantities after the update
12703    query_tree
12704      (
12705         p_api_version_number => 1.0
12706       , p_init_msg_lst       => fnd_api.g_false
12707       , x_return_status      => l_return_status
12708       , x_msg_count          => x_msg_count
12709       , x_msg_data           => x_msg_data
12710       , p_tree_id            => p_tree_id
12711       , p_revision           => p_revision
12712       , p_lot_number         => p_lot_number
12713       , p_subinventory_code  => p_subinventory_code
12714       , p_locator_id         => p_locator_id
12715       , x_qoh                => x_qoh
12716       , x_rqoh               => x_rqoh
12717       , x_qr                 => x_qr
12718       , x_qs                 => x_qs
12719       , x_att                => x_att
12720       , x_atr                => x_atr
12721       , x_sqoh               => x_sqoh
12722       , x_srqoh              => x_srqoh
12723       , x_sqr                => x_sqr
12724       , x_sqs                => x_sqs
12725       , x_satt               => x_satt
12726       , x_satr               => x_satr
12727       , p_transfer_subinventory_code => p_transfer_subinventory_code
12728       , p_cost_group_id      => p_cost_group_id
12729       , p_lpn_id             => p_lpn_id
12730       , p_transfer_locator_id => p_transfer_locator_id
12731       );
12732 
12733    IF l_return_status = fnd_api.g_ret_sts_error THEN
12734       RAISE fnd_api.g_exc_error;
12735    End IF ;
12736 
12737    IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12738       RAISE fnd_api.g_exc_unexpected_error;
12739    End IF;
12740 
12741    x_return_status := l_return_status;
12742 
12743    IF g_debug = 1 THEN
12744       print_debug(l_api_name || ' Exited with status = '||l_return_status,9);
12745       print_debug(' ',9);
12746    END IF;
12747 
12748 EXCEPTION
12749 
12750     WHEN fnd_api.g_exc_error THEN
12751         x_return_status := fnd_api.g_ret_sts_error;
12752 
12753         --  Get message count and data
12754         fnd_msg_pub.count_and_get
12755           (  p_count => x_msg_count
12756            , p_data  => x_msg_data
12757            );
12758 
12759    WHEN fnd_api.g_exc_unexpected_error THEN
12760         x_return_status := fnd_api.g_ret_sts_unexp_error ;
12761 
12762         --  Get message count and data
12763         fnd_msg_pub.count_and_get
12764           (  p_count  => x_msg_count
12765            , p_data   => x_msg_data
12766             );
12767 
12768     WHEN OTHERS THEN
12769         x_return_status := fnd_api.g_ret_sts_unexp_error ;
12770 
12771         IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
12772           THEN
12773            fnd_msg_pub.add_exc_msg
12774              (  g_pkg_name
12775               , l_api_name
12776               );
12777         END IF;
12778 
12779         --  Get message count and data
12780         fnd_msg_pub.count_and_get
12781           (  p_count  => x_msg_count
12782            , p_data   => x_msg_data
12783             );
12784 
12785 END update_quantities;
12786 
12787 
12788 -- Bug 2486318. The do check does not work. Trasactions get committed
12789 -- even if there is a node violation. Added p_check_mark_node_only to mark
12790 -- the nodes. A new procedure update quantities for form was added and
12791 -- called from inldqc.ppc
12792 
12793 --
12794 -- Procedure
12795 --   update_quantities_for_form
12796 -- Description
12797 --   Create a quantity tree
12798 --
12799 --  Version
12800 --   Current version        1.0
12801 --   Initial version        1.0
12802 --
12803 -- Input parameters:
12804 --   p_api_version_number   standard input parameter
12805 --   p_init_msg_lst         standard input parameter
12806 --   p_tree_id              tree_id
12807 --   p_revision             revision
12808 --   p_lot_number           lot_number
12809 --   p_subinventory_code    subinventory_code
12810 --   p_locator_id           locator_id
12811 --   p_primary_quantity     primary_quantity
12812 --   p_quantity_type
12813 --   p_call_for_form        to check if the call is from form
12814 --
12815 -- Output parameters:
12816 --   x_return_status       standard output parameter
12817 --   x_msg_count           standard output parameter
12818 --   x_msg_data            standard output parameter
12819 --   x_tree_id             used later to refer to the same tree
12820 --   x_qoh                 qoh   after the update
12821 --   x_rqoh                rqoh  after the update
12822 --   x_qr                  qr    after the update
12823 --   x_qs                  qs    after the update
12824 --   x_att                 att   after the update
12825 --   x_atr                 atr   after the update
12826 PROCEDURE update_quantities_for_form
12827   (  p_api_version_number    IN  NUMBER
12828    , p_init_msg_lst          IN  VARCHAR2
12829    , x_return_status         OUT NOCOPY VARCHAR2
12830    , x_msg_count             OUT NOCOPY NUMBER
12831    , x_msg_data              OUT NOCOPY VARCHAR2
12832    , p_tree_id               IN  INTEGER
12833    , p_revision              IN  VARCHAR2
12834    , p_lot_number            IN  VARCHAR2
12835    , p_subinventory_code     IN  VARCHAR2
12836    , p_locator_id            IN  NUMBER
12837    , p_primary_quantity      IN  NUMBER
12838    , p_quantity_type         IN  INTEGER
12839    , x_qoh                   OUT NOCOPY NUMBER
12840    , x_rqoh                  OUT NOCOPY NUMBER
12841    , x_qr                    OUT NOCOPY NUMBER
12842    , x_qs                    OUT NOCOPY NUMBER
12843    , x_att                   OUT NOCOPY NUMBER
12844    , x_atr                   OUT NOCOPY NUMBER
12845    , p_transfer_subinventory_code IN VARCHAR2
12846    , p_cost_group_id         IN  NUMBER
12847    , p_containerized         IN  NUMBER
12848    , p_call_for_form         IN  VARCHAR2
12849    , p_lpn_id                IN NUMBER DEFAULT NULL  --added for bug7038890
12850      ) IS
12851 
12852 l_secondary_quantity  NUMBER := NULL;
12853 l_sqoh    NUMBER;
12854 l_srqoh   NUMBER;
12855 l_sqr     NUMBER;
12856 l_sqs     NUMBER;
12857 l_satt    NUMBER;
12858 l_satr    NUMBER;
12859 
12860 BEGIN
12861 
12862 inv_quantity_tree_pvt.update_quantities_for_form
12863   (  p_api_version_number    => p_api_version_number
12864    , p_init_msg_lst          => p_init_msg_lst
12865    , x_return_status         => x_return_status
12866    , x_msg_count             => x_msg_count
12867    , x_msg_data              => x_msg_data
12868    , p_tree_id               => p_tree_id
12869    , p_revision              => p_revision
12870    , p_lot_number            => p_lot_number
12871    , p_subinventory_code     => p_subinventory_code
12872    , p_locator_id            => p_locator_id
12873    , p_primary_quantity      => p_primary_quantity
12874    , p_secondary_quantity    => l_secondary_quantity
12875    , p_quantity_type         => p_quantity_type
12876    , x_qoh                   => x_qoh
12877    , x_rqoh                  => x_rqoh
12878    , x_qr                    => x_qr
12879    , x_qs                    => x_qs
12880    , x_att                   => x_att
12881    , x_atr                   => x_atr
12882    , x_sqoh                  => l_sqoh
12883    , x_srqoh                 => l_srqoh
12884    , x_sqr                   => l_sqr
12885    , x_sqs                   => l_sqs
12886    , x_satt                  => l_satt
12887    , x_satr                  => l_satr
12888    , p_transfer_subinventory_code => p_transfer_subinventory_code
12889    , p_cost_group_id         => p_cost_group_id
12890    , p_containerized         => p_containerized
12891    , p_call_for_form         => p_call_for_form
12892    , p_lpn_id                => p_lpn_id); --added for bug7038890
12893 
12894 
12895 END update_quantities_for_form;
12896 
12897 PROCEDURE update_quantities_for_form
12898   (  p_api_version_number    IN  NUMBER
12899    , p_init_msg_lst          IN  VARCHAR2
12900    , x_return_status         OUT NOCOPY VARCHAR2
12901    , x_msg_count             OUT NOCOPY NUMBER
12902    , x_msg_data              OUT NOCOPY VARCHAR2
12903    , p_tree_id               IN  INTEGER
12904    , p_revision              IN  VARCHAR2
12905    , p_lot_number            IN  VARCHAR2
12906    , p_subinventory_code     IN  VARCHAR2
12907    , p_locator_id            IN  NUMBER
12908    , p_primary_quantity      IN  NUMBER
12909    , p_secondary_quantity    IN  NUMBER
12910    , p_quantity_type         IN  INTEGER
12911    , x_qoh                   OUT NOCOPY NUMBER
12912    , x_rqoh                  OUT NOCOPY NUMBER
12913    , x_qr                    OUT NOCOPY NUMBER
12914    , x_qs                    OUT NOCOPY NUMBER
12915    , x_att                   OUT NOCOPY NUMBER
12916    , x_atr                   OUT NOCOPY NUMBER
12917    , x_sqoh                  OUT NOCOPY NUMBER
12918    , x_srqoh                 OUT NOCOPY NUMBER
12919    , x_sqr                   OUT NOCOPY NUMBER
12920    , x_sqs                   OUT NOCOPY NUMBER
12921    , x_satt                  OUT NOCOPY NUMBER
12922    , x_satr                  OUT NOCOPY NUMBER
12923    , p_transfer_subinventory_code IN VARCHAR2
12924    , p_cost_group_id         IN  NUMBER
12925    , p_containerized         IN  NUMBER
12926    , p_call_for_form         IN  VARCHAR2
12927    , p_lpn_id               IN NUMBER DEFAULT NULL  --added for bug7038890
12928      ) IS
12929       l_api_version_number   CONSTANT NUMBER       := 1.0;
12930       l_api_name             CONSTANT VARCHAR2(30) := 'UPDATE_QUANTITIES_FOR_FORM';
12931       l_return_status        VARCHAR2(1) := fnd_api.g_ret_sts_success;
12932       l_root_id        INTEGER;
12933       l_is_for_form         BOOLEAN;
12934 BEGIN
12935 
12936    IF g_debug = 1 THEN
12937       print_debug(l_api_name || ' Entered',9);
12938    END IF;
12939 
12940    --  Standard call to check for call compatibility
12941    IF NOT fnd_api.compatible_api_call(l_api_version_number
12942                                       , p_api_version_number
12943                                       , l_api_name
12944                                       , G_PKG_NAME
12945                                       ) THEN
12946       RAISE fnd_api.g_exc_unexpected_error;
12947    END IF;
12948 
12949    --  Initialize message list.
12950    IF fnd_api.to_boolean(p_init_msg_lst) THEN
12951       fnd_msg_pub.initialize;
12952    END IF;
12953 
12954    l_root_id := g_demand_info(p_tree_id).root_id;
12955 
12956    -- check if tree id is valid
12957    IF is_tree_valid(l_root_id) = FALSE THEN
12958       fnd_message.set_name('INV', 'INV-Qtyroot not found');
12959       fnd_message.set_token('ROUTINE', 'Update_Quantities');
12960       fnd_msg_pub.ADD;
12961       RAISE fnd_api.g_exc_unexpected_error;
12962    END IF;
12963 
12964    IF p_call_for_form = fnd_api.g_true THEN
12965       l_is_for_form := FALSE;
12966    ELSE
12967       l_is_for_form := TRUE;
12968    END IF;
12969 
12970    print_debug('Before calling add_quantities. qty1='||p_primary_quantity||', qty2='||p_secondary_quantity||', qtyType='||p_quantity_type);
12971    add_quantities(
12972         x_return_status     => l_return_status
12973       , p_tree_id           => l_root_id
12974       , p_revision          => p_revision
12975       , p_lot_number        => p_lot_number
12976       , p_subinventory_code => p_subinventory_code
12977       , p_is_reservable_sub => NULL
12978       , p_locator_id        => p_locator_id
12979       , p_primary_quantity  => p_primary_quantity
12980       , p_secondary_quantity=> p_secondary_quantity
12981       , p_quantity_type     => p_quantity_type
12982       , p_set_check_mark    => l_is_for_form
12983       , p_cost_group_id     => p_cost_group_id
12984       , p_lpn_id     => p_lpn_id                  --replaced null value with p_lpn_id bug7038890
12985       , p_check_mark_node_only     => fnd_api.g_true
12986       );
12987       print_debug('After calling add_quantities. return_status='||l_return_status);
12988 
12989    IF l_return_status = fnd_api.g_ret_sts_error THEN
12990       RAISE fnd_api.g_exc_error;
12991    End IF ;
12992 
12993    IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12994       RAISE fnd_api.g_exc_unexpected_error;
12995    End IF;
12996    print_debug('Before calling Query_Tree');
12997    -- query the quantities after the update
12998    query_tree(
12999         p_api_version_number => 1.0
13000       , p_init_msg_lst       => fnd_api.g_false
13001       , x_return_status      => l_return_status
13002       , x_msg_count          => x_msg_count
13003       , x_msg_data           => x_msg_data
13004       , p_tree_id            => p_tree_id
13005       , p_revision           => p_revision
13006       , p_lot_number         => p_lot_number
13007       , p_subinventory_code  => p_subinventory_code
13008       , p_locator_id         => p_locator_id
13009       , x_qoh                => x_qoh
13010       , x_rqoh               => x_rqoh
13011       , x_qr                 => x_qr
13012       , x_qs                 => x_qs
13013       , x_att                => x_att
13014       , x_atr                => x_atr
13015       , x_sqoh               => x_sqoh
13016       , x_srqoh              => x_srqoh
13017       , x_sqr                => x_sqr
13018       , x_sqs                => x_sqs
13019       , x_satt               => x_satt
13020       , x_satr               => x_satr
13021       , p_transfer_subinventory_code => p_transfer_subinventory_code
13022       , p_cost_group_id      => p_cost_group_id
13023       , p_lpn_id             => p_lpn_id  --added for bug7038890
13024       );
13025    print_debug('After calling Query_Tree return_status='||l_return_status);
13026 
13027    IF l_return_status = fnd_api.g_ret_sts_error THEN
13028       RAISE fnd_api.g_exc_error;
13029    End IF ;
13030 
13031    IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
13032       RAISE fnd_api.g_exc_unexpected_error;
13033    End IF;
13034 
13035    x_return_status := l_return_status;
13036 
13037    IF g_debug = 1 THEN
13038       print_debug(l_api_name || ' Exited with status = '||l_return_status,9);
13039       print_debug(' ',9);
13040    END IF;
13041 
13042 EXCEPTION
13043 
13044     WHEN fnd_api.g_exc_error THEN
13045         x_return_status := fnd_api.g_ret_sts_error;
13046 
13047         --  Get message count and data
13048         fnd_msg_pub.count_and_get
13049           (  p_count => x_msg_count
13050            , p_data  => x_msg_data
13051            );
13052 
13053    WHEN fnd_api.g_exc_unexpected_error THEN
13054         x_return_status := fnd_api.g_ret_sts_unexp_error ;
13055 
13056         --  Get message count and data
13057         fnd_msg_pub.count_and_get
13058           (  p_count  => x_msg_count
13059            , p_data   => x_msg_data
13060             );
13061 
13062     WHEN OTHERS THEN
13063         x_return_status := fnd_api.g_ret_sts_unexp_error ;
13064 
13065         IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error)
13066           THEN
13067            fnd_msg_pub.add_exc_msg
13068              (  g_pkg_name
13069               , l_api_name
13070               );
13071         END IF;
13072 
13073         --  Get message count and data
13074         fnd_msg_pub.count_and_get
13075           (  p_count  => x_msg_count
13076            , p_data   => x_msg_data
13077             );
13078 
13079 END update_quantities_for_form;
13080 
13081 -- save a node and its children into the backup tables
13082 PROCEDURE save_node
13083   (p_node_index IN INTEGER)
13084   IS
13085      l_node_index INTEGER;
13086 BEGIN
13087    g_savenodes(p_node_index) := g_nodes(p_node_index);
13088    l_node_index := g_nodes(p_node_index).first_child_index;
13089    WHILE l_node_index <> 0 LOOP
13090       save_node(l_node_index);
13091       l_node_index := g_nodes(l_node_index).next_sibling_index;
13092    END LOOP;
13093 END save_node;
13094 
13095 -- save a rootinfo and its tree nodes into the backup tables
13096 PROCEDURE save_rootinfo
13097   (p_tree_id IN INTEGER )
13098   IS
13099      l_item_index INTEGER;
13100 BEGIN
13101    g_saveroots(p_tree_id) := g_rootinfos(p_tree_id);
13102    l_item_index := g_rootinfos(p_tree_id).item_node_index;
13103    -- save the item node and all children nodes below
13104    save_node(l_item_index);
13105 END save_rootinfo;
13106 
13107 -- Procedure
13108 --   backup_tree
13109 -- Description
13110 --   backup the current state of a tree
13111 -- Note
13112 --   This is only a one level backup. Calling it twice will
13113 --   overwrite the previous backup
13114 PROCEDURE backup_tree
13115   (
13116      x_return_status OUT NOCOPY VARCHAR2
13117    , p_tree_id       IN  INTEGER
13118    ) IS
13119       l_return_status   VARCHAR2(1) := fnd_api.g_ret_sts_success;
13120       l_root_id INTEGER;
13121       l_api_name        VARCHAR2(30) := 'BACKUP_TREE';
13122 BEGIN
13123    IF g_debug = 1 THEN
13124       print_debug(l_api_name || ' Entered',9);
13125    END IF;
13126    l_root_id := g_demand_info(p_tree_id).root_id;
13127    IF is_tree_valid(l_root_id) = FALSE THEN
13128       fnd_message.set_name('INV', 'INV-Qtyroot not found');
13129       fnd_message.set_token('ROUTINE', 'Backup_Tree');
13130       fnd_msg_pub.ADD;
13131       RAISE fnd_api.g_exc_error;
13132    END IF;
13133 
13134    save_rootinfo(l_root_id);
13135    -- bug 6683013, backing up rsv_tree
13136    g_save_rsv_tree_id := g_rsv_tree_id;
13137     g_save_rsv_counter := g_rsv_counter;
13138     g_saversvnode.DELETE;
13139 
13140     print_debug('backup: rsv_tree_id = ' || g_rsv_tree_id || ' rsv_counter = ' || g_rsv_counter, 11);
13141 
13142     if g_save_rsv_tree_id <> 0 then
13143        --backup rsv_nodes into g_saversvnode.
13144        FOR counter in 1..g_rsv_counter LOOP
13145           g_saversvnode(counter) := g_rsv_info(counter);
13146        END LOOP;
13147     end if;
13148 
13149    x_return_status := l_return_status;
13150 
13151    IF g_debug = 1 THEN
13152       print_debug(l_api_name || ' Exited with status = '||l_return_status,9);
13153       print_debug(' ',9);
13154    END IF;
13155 
13156 EXCEPTION
13157 
13158    WHEN fnd_api.g_exc_error THEN
13159        x_return_status := fnd_api.g_ret_sts_error;
13160 
13161    WHEN fnd_api.g_exc_unexpected_error THEN
13162       x_return_status := fnd_api.g_ret_sts_unexp_error ;
13163 
13164 
13165    WHEN OTHERS THEN
13166       x_return_status := fnd_api.g_ret_sts_unexp_error ;
13167 
13168 END backup_tree;
13169 
13170 -- restore a node and its children using the backup tables
13171 PROCEDURE restore_node
13172   (p_node_index IN INTEGER)
13173   IS
13174      l_node_index INTEGER;
13175 BEGIN
13176    g_nodes(p_node_index) := g_savenodes(p_node_index);
13177    l_node_index := g_savenodes(p_node_index).first_child_index;
13178    WHILE l_node_index <> 0 LOOP
13179       restore_node(l_node_index);
13180       l_node_index := g_savenodes(l_node_index).next_sibling_index;
13181    END LOOP;
13182 END restore_node;
13183 
13184 -- restore a rootinfo and its tree nodes using the backup tables
13185 PROCEDURE restore_rootinfo
13186   (p_tree_id IN INTEGER )
13187   IS
13188      l_item_index INTEGER;
13189 BEGIN
13190    g_rootinfos(p_tree_id) := g_saveroots(p_tree_id);
13191    l_item_index := g_saveroots(p_tree_id).item_node_index;
13192    -- restore the item node and all children nodes below
13193    restore_node(l_item_index);
13194 END restore_rootinfo;
13195 
13196 -- Procedure
13197 --   restore_tree
13198 -- Description
13199 --   restore the current state of a tree to the state
13200 --   at the last time when savepoint_tree is called
13201 -- Note
13202 --   This is only a one level restore. Calling it more than once
13203 --   has the same effect as calling it once.
13204 PROCEDURE restore_tree
13205   (
13206      x_return_status OUT NOCOPY VARCHAR2
13207    , p_tree_id       IN  INTEGER
13208    ) IS
13209       l_return_status   VARCHAR2(1) := fnd_api.g_ret_sts_success;
13210       l_root_id      INTEGER;
13211       l_api_name        VARCHAR2(30) := 'RESTORE_TREE';
13212 BEGIN
13213    IF g_debug = 1 THEN
13214       print_debug(l_api_name || ' Entered',9);
13215    END IF;
13216 
13217    l_root_id := g_demand_info(p_tree_id).root_id;
13218    IF is_saved_tree_valid(l_root_id) = FALSE THEN
13219       fnd_message.set_name('INV', 'INV-Qtyroot not found');
13220       fnd_message.set_token('ROUTINE', 'Restore_Tree');
13221       fnd_msg_pub.ADD;
13222       RAISE fnd_api.g_exc_error;
13223    END IF;
13224 
13225    restore_rootinfo(l_root_id);
13226    -- bug 6683013, restoring back rsv_tree
13227     g_rsv_tree_id := g_save_rsv_tree_id;
13228     g_rsv_counter := g_save_rsv_counter;
13229     g_rsv_info.DELETE;
13230     print_debug('restore: rsv_tree_id = ' || g_rsv_tree_id || ' rsv_counter = ' || g_rsv_counter, 11);
13231 
13232     if g_save_rsv_tree_id <> 0 then
13233        --restore rsv_nodes from g_saversvnode.
13234        FOR counter in 1..g_rsv_counter LOOP
13235           g_rsv_info(counter) := g_saversvnode(counter);
13236        END LOOP;
13237     end if;
13238 
13239    x_return_status := l_return_status;
13240 
13241    IF g_debug = 1 THEN
13242       print_debug(l_api_name || ' Exited with status = '||l_return_status,9);
13243       print_debug(' ',9);
13244    END IF;
13245 
13246 EXCEPTION
13247 
13248    WHEN fnd_api.g_exc_error THEN
13249        x_return_status := fnd_api.g_ret_sts_error;
13250 
13251    WHEN fnd_api.g_exc_unexpected_error THEN
13252       x_return_status := fnd_api.g_ret_sts_unexp_error ;
13253 
13254 
13255    WHEN OTHERS THEN
13256       x_return_status := fnd_api.g_ret_sts_unexp_error ;
13257 
13258 END restore_tree;
13259 
13260 
13261 -- **NEW BACKUP/RESTORE PROCEDURES**
13262 -- Bug 2788807
13263 --    We now need to support multi-level backup and restore capability
13264 -- for the quantity tree.  We'll overload the existing procedures.
13265 
13266 -- Backup_Node
13267 -- recursive function to copy current node, its children, and its siblings
13268 -- to the backup node table
13269 PROCEDURE backup_node
13270   (p_node_index IN INTEGER)
13271   IS
13272      l_node_index INTEGER;
13273 BEGIN
13274    g_backup_node_counter := g_backup_node_counter + 1;
13275    g_backup_nodes(g_backup_node_counter) := g_nodes(p_node_index);
13276    g_backup_nodes(g_backup_node_counter).node_index := p_node_index;
13277 
13278    l_node_index := g_nodes(p_node_index).first_child_index;
13279    WHILE l_node_index <> 0 LOOP
13280       backup_node(l_node_index);
13281       l_node_index := g_nodes(l_node_index).next_sibling_index;
13282    END LOOP;
13283 END backup_node;
13284 
13285 -- Procedure
13286 --   backup_tree
13287 -- Description
13288 --   backup the current state of a tree.  This procedure returns a backup_id
13289 --   which needs to be passed to restore_tree in order to restore the correct
13290 --   version of the quantity tree.  Unlike the older version of backup_tree,
13291 --   this can be called multiple times without overwriting previous backups.
13292 --   The backups dissappear when clear_quantity_cache is called.
13293 --
13294 PROCEDURE backup_tree
13295   (
13296      x_return_status OUT NOCOPY VARCHAR2
13297    , p_tree_id       IN  INTEGER
13298    , x_backup_id     OUT NOCOPY NUMBER
13299    ) IS
13300       l_return_status   VARCHAR2(1) := fnd_api.g_ret_sts_success;
13301       l_root_id INTEGER;
13302       l_backup_id NUMBER;
13303       l_api_name        VARCHAR2(30) := 'BACKUP_TREE';
13304 BEGIN
13305    IF g_debug = 1 THEN
13306       print_debug(l_api_name || ' Entered',9);
13307    END IF;
13308 
13309    l_root_id := g_demand_info(p_tree_id).root_id;
13310    IF is_tree_valid(l_root_id) = FALSE THEN
13311       fnd_message.set_name('INV', 'INV-Qtyroot not found');
13312       fnd_message.set_token('ROUTINE', 'Backup_Tree');
13313       fnd_msg_pub.ADD;
13314       RAISE fnd_api.g_exc_error;
13315    END IF;
13316 
13317    g_backup_tree_counter := g_backup_tree_counter + 1;
13318    l_backup_id := g_backup_tree_counter;
13319 
13320    --populate parent table - g_backup_trees
13321    g_backup_trees(l_backup_id).root_id := l_root_id;
13322    g_backup_trees(l_backup_id).first_node := g_backup_node_counter + 1;
13323 
13324    --populate child table - g_backup_nodes - by calling recursive function
13325    backup_node(g_rootinfos(l_root_id).item_node_index);
13326 
13327    --store id of last record of current tree
13328    g_backup_trees(l_backup_id).last_node := g_backup_node_counter;
13329 
13330    x_return_status := l_return_status;
13331    x_backup_id := l_backup_id;
13332 
13333    IF g_debug = 1 THEN
13334       print_debug(l_api_name || ' Exited with status = '||l_return_status,9);
13335       print_debug(' ',9);
13336    END IF;
13337 
13338 EXCEPTION
13339 
13340    WHEN fnd_api.g_exc_error THEN
13341        x_return_status := fnd_api.g_ret_sts_error;
13342 
13343    WHEN fnd_api.g_exc_unexpected_error THEN
13344       x_return_status := fnd_api.g_ret_sts_unexp_error ;
13345 
13346    WHEN OTHERS THEN
13347       x_return_status := fnd_api.g_ret_sts_unexp_error ;
13348 
13349 END backup_tree;
13350 
13351 -- Procedure
13352 --   restore_tree
13353 -- Description
13354 --   Restores the quantity tree to the point indicated by the backup_id.
13355 --   Tree_id is not strictly needed here, but is kept for overloading and
13356 --   error checking purposes.  Restore_tree can be called multiple times for
13357 --   the same backup_id - a saved quantity tree is not deleted until
13358 --   clear_quantity_cache is called.
13359 PROCEDURE restore_tree
13360   (
13361      x_return_status OUT NOCOPY VARCHAR2
13362    , p_tree_id       IN  INTEGER
13363    , p_backup_id     IN  NUMBER
13364    ) IS
13365       l_return_status   VARCHAR2(1) := fnd_api.g_ret_sts_success;
13366       l_root_id         INTEGER;
13367       l_loop_index   NUMBER;
13368       l_node_index   NUMBER;
13369       l_api_name    VARCHAR2(30) := 'RESTORE_TREE';
13370 BEGIN
13371    IF g_debug = 1 THEN
13372       print_debug(l_api_name || ' Entered',9);
13373    END IF;
13374 
13375    IF NOT g_demand_info.exists(p_tree_id) THEN
13376    raise fnd_api.g_exc_unexpected_error;
13377    END IF;
13378    IF NOT g_backup_trees.exists(p_backup_id) THEN
13379    raise fnd_api.g_exc_unexpected_error;
13380    END IF;
13381 
13382    l_root_id := g_demand_info(p_tree_id).root_id;
13383 
13384    IF l_root_id <> g_backup_trees(p_backup_id).root_id THEN
13385         raise fnd_api.g_exc_unexpected_error;
13386    END IF;
13387 
13388    l_loop_index := g_backup_trees(p_backup_id).first_node;
13389 
13390    LOOP
13391      EXIT when l_loop_index > g_backup_trees(p_backup_id).last_node;
13392      l_node_index := g_backup_nodes(l_loop_index).node_index;
13393      g_nodes(l_node_index) :=g_backup_nodes(l_loop_index);
13394      l_loop_index := l_loop_index + 1;
13395    END LOOP;
13396 
13397    x_return_status := l_return_status;
13398 
13399    IF g_debug = 1 THEN
13400       print_debug(l_api_name || ' Exited with status = '||l_return_status,9);
13401       print_debug(' ',9);
13402    END IF;
13403 
13404 EXCEPTION
13405 
13406    WHEN fnd_api.g_exc_error THEN
13407        x_return_status := fnd_api.g_ret_sts_error;
13408 
13409    WHEN fnd_api.g_exc_unexpected_error THEN
13410       x_return_status := fnd_api.g_ret_sts_unexp_error ;
13411 
13412 
13413    WHEN OTHERS THEN
13414       x_return_status := fnd_api.g_ret_sts_unexp_error ;
13415 
13416 END restore_tree;
13417 
13418 
13419 function do_check_release_locks return boolean IS
13420 cursor C1 is select organization_id,inventory_item_id
13421                from mtl_do_check_temp;
13422 l_return_status varchar2(1);
13423 l_msg_count number;
13424 l_msg_data varchar2(2000);
13425 l_api_number number := 1.0;
13426 
13427 begin
13428   for c1_rec in C1 loop
13429       INV_QUANTITY_TREE_PVT.release_lock(
13430          l_api_number,
13431          fnd_api.g_false,
13432          l_return_status,
13433          l_msg_count,
13434          l_msg_data,
13435          c1_rec.organization_id,
13436          c1_rec.inventory_item_id);
13437   if l_return_status = fnd_api.g_ret_sts_error then
13438     Return FALSE;
13439   End if;
13440   end loop;
13441 return TRUE;
13442 end;
13443 
13444 -------------------------------------------------------------------------------
13445 -- Coding standard used in this program
13446 -- 1. PLSQL business object api coding standard
13447 -- 2. Oracle application developer's guide
13448 -- Note:
13449 -- 1. Data types are not initialized to fnd_api.g_miss_???
13450 -- 2. Procedures or functions not exposed to user do not have the following parameters:
13451 --    p_api_version, p_msg_count, p_msg_data. x_return_status is optional.
13452 -- 3. identation and character case uses plsql mode of emacs
13453 -------------------------------------------------------------------------------
13454 END inv_quantity_tree_pvt;