DBA Data[Home] [Help]

PACKAGE BODY: APPS.INV_CYC_LOVS

Source


1 PACKAGE BODY INV_CYC_LOVS AS
2 /* $Header: INVCYCLB.pls 120.69.12020000.6 2013/03/06 03:27:50 tachen ship $ */
3 
4 --  Global constant holding the package name
5    G_PKG_NAME CONSTANT VARCHAR2 ( 30 ) := 'INV_CYC_LOVS';
6 -- Global variables used to store information related to a cycle count entry
7    g_count_quantity NUMBER;
8    g_count_uom VARCHAR2 ( 3 );
9    g_orig_count_uom VARCHAR2 ( 3 ); --10318308 added to store raw value
10    g_orig_count_quantity NUMBER; --10318308 added to store raw value
11    g_count_secondary_quantity NUMBER;         -- INVCONV, NSRIVAST
12    g_count_secondary_uom      VARCHAR2 ( 3 );  -- INVCONV, NSRIVAST
13    g_cc_entry CC_ENTRY;
14    g_cc_serial_entry CC_SERIAL_ENTRY;
15    g_pre_approve_flag VARCHAR2 ( 20 ) := 'FALSE';
16    g_serial_out_tolerance BOOLEAN := FALSE;
17    g_txn_header_id NUMBER;
18    g_txn_proc_mode NUMBER;
19    g_user_id NUMBER;
20    g_login_id NUMBER;
21    g_commit_status_flag NUMBER;
22    g_update_flag NUMBER;
23    g_insert_flag NUMBER;
24    g_serial_number VARCHAR2 ( 30 );
25    g_employee_id NUMBER;
26    g_employee_full_name VARCHAR2 ( 240 );
27 -- These two following values correspond to form checkbox values
28 -- for the current cycle count serial entry used mainly for multiple
29 -- serial counting
30    g_unit_status NUMBER;
31    g_system_present NUMBER;
32    g_serial_entry_status_code NUMBER;
33    g_count_entry_status_code NUMBER;
34 
35    /* Bug 4495880-Added the global parameter and defaulted it to FALSE */
36     g_condition BOOLEAN := FALSE;
37    /* End of fix for Bug 4495880 */
38    g_updated_prior BOOLEAN := FALSE;  -- Bug 6371673
39 
40    g_lpn_summary_count  BOOLEAN := FALSE ; --9452528.To cehck if this is a summary count or not.
41    g_unpack      CONSTANT NUMBER         := 2;--13053297
42 
43 
44 --      Name: GET_CYC_LOV
45 --
46 --      Input parameters:
47 --       p_cycle_count        Restricts LOV SQL to the user input text
48 --       p_organization_id    Organization ID
49 --
50 --      Output parameters:
51 --       x_cyc_lov            Returns LOV rows as a reference cursor
52 --
53 --      Functions: This API returns valid cycle counts
54 --
55 
56    PROCEDURE get_cyc_lov (
57       x_cyc_lov   OUT NOCOPY t_genref,
58       p_cycle_count IN VARCHAR2,
59       p_organization_id IN NUMBER
60    )
61    IS
62       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
63    BEGIN
64       OPEN x_cyc_lov FOR
65          SELECT   cycle_count_header_name,
66                   cycle_count_header_id,
67                   description,
68                   inventory_adjustment_account,
69                   orientation_code,
70                   onhand_visible_flag,
71                   zero_count_flag,
72                   disable_date,
73                   approval_option_code,
74                   automatic_recount_flag,
75                   unscheduled_count_entry,
76                   approval_tolerance_positive,
77                   approval_tolerance_negative,
78                   cost_tolerance_positive,
79                   cost_tolerance_negative,
80                   hit_miss_tolerance_positive,
81                   hit_miss_tolerance_negative,
82                   serial_count_option,
83                   serial_detail_option,
84                   serial_adjustment_option,
85                   serial_discrepancy_option,
86                   container_adjustment_option,
87                   container_discrepancy_option,
88                   container_enabled_flag,
89                   cycle_count_type,
90                   schedule_empty_locations
91          FROM     mtl_cycle_count_headers
92          WHERE    organization_id = p_organization_id
93          AND      trunc(nvl(disable_date, sysdate+1)) > trunc(sysdate)  --Changed for bug 5519506
94          AND      cycle_count_header_name LIKE ( p_cycle_count )
95          AND      (     ( cycle_count_header_id IN (
96                              SELECT UNIQUE cycle_count_header_id
97                              FROM          mtl_cycle_count_entries
98                              WHERE         organization_id = p_organization_id
99                              AND           entry_status_code IN ( 1, 3 ) )
100                         )
101                     OR NVL ( unscheduled_count_entry, 2 ) = 1
102                   )
103          ORDER BY 1;
104    END get_cyc_lov;
105 
106    --Forward Declaration 13053297
107    PROCEDURE call_pack_unpack_container( p_lpn_id NUMBER
108                                       , p_content_lpn_id NUMBER
109                                       , p_org_id NUMBER
110                                       , p_subinv VARCHAR2
111                                       , p_locator NUMBER
112                                       , p_operation NUMBER
113                                       );
114 
115 
116    PROCEDURE print_debug (
117       p_err_msg VARCHAR2
118    )
119    IS
120       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
121    BEGIN
122       IF ( l_debug = 1 ) THEN
123          inv_mobile_helper_functions.tracelog ( p_err_msg           => p_err_msg,
124                                                 p_module            => 'INV_CYC_LOVS',
125                                                 p_level             => 4
126                                               );
127       END IF;
128 --   dbms_output.put_line(p_err_msg);
129    END print_debug;
130 
131 -- This will do an autonomous commit to update the
132 -- cycle count header with the next user count sequence
133 -- value so that one user does not lock up the entire
134 -- table when performing a cycle count
135    PROCEDURE update_count_list_sequence (
136       p_organization_id NUMBER,
137       p_cycle_count_header_id NUMBER,
138       x_count_list_sequence OUT NOCOPY NUMBER
139    )
140    IS
141       PRAGMA AUTONOMOUS_TRANSACTION;
142       l_count_list_sequence NUMBER;
143       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
144    BEGIN
145       -- Calculate the next value for the count list sequence
146       SELECT NVL ( MAX ( count_list_sequence ), 0 ) + 1
147       INTO   l_count_list_sequence
148       FROM   mtl_cycle_count_entries
149       WHERE  cycle_count_header_id = p_cycle_count_header_id
150       AND    organization_id = p_organization_id;
151 
152       -- Update this value for the cycle count header
153       UPDATE mtl_cycle_count_headers
154       SET next_user_count_sequence = l_count_list_sequence + 1
155       WHERE  cycle_count_header_id = p_cycle_count_header_id
156       AND    organization_id = p_organization_id;
157 
158       COMMIT;
159       -- Set the output value
160       x_count_list_sequence := l_count_list_sequence;
161    END update_count_list_sequence;
162 
163    PROCEDURE process_entry (
164       p_cycle_count_header_id IN NUMBER,
165       p_organization_id IN NUMBER,
166       p_subinventory IN VARCHAR2,
167       p_locator_id IN NUMBER,
168       p_parent_lpn_id IN NUMBER,
169       p_inventory_item_id IN NUMBER,
170       p_revision  IN VARCHAR2,
171       p_lot_number IN VARCHAR2,
172       p_from_serial_number IN VARCHAR2,
173       p_to_serial_number IN VARCHAR2,
174       p_count_quantity IN NUMBER,
175       p_count_uom IN VARCHAR2,
176       p_unscheduled_count_entry IN NUMBER,
177       p_user_id   IN NUMBER,
178       p_cost_group_id IN NUMBER
179       ,p_secondary_uom  IN VARCHAR2  -- INVCONV, NSRIVAST
180       ,p_secondary_qty  IN NUMBER  -- INVCONV, NSRIVAST
181 
182    )
183    IS
184       l_current_serial VARCHAR2 ( 30 );
185 
186       CURSOR cc_entry_cursor
187       IS
188          SELECT *
189          FROM   mtl_cycle_count_entries
190          WHERE  cycle_count_header_id = p_cycle_count_header_id
191          AND    organization_id = p_organization_id
192          AND    subinventory = p_subinventory
193          AND    NVL ( locator_id, -99999 ) = NVL ( p_locator_id, -99999 )
194          AND    NVL ( parent_lpn_id, -99999 ) = NVL ( p_parent_lpn_id, -99999 )
195          AND    inventory_item_id = p_inventory_item_id
196          AND    NVL ( revision, '@@@@@' ) = NVL ( p_revision, '@@@@@' )
197          AND    NVL ( lot_number, '@@@@@' ) = NVL ( p_lot_number, '@@@@@' )
198          AND    NVL ( serial_number, '@@@@@' ) =
199                                              NVL ( l_current_serial, '@@@@@' )
200          AND    entry_status_code IN ( 1, 3 );
201 
202       CURSOR cc_multiple_serial_cursor
203       IS
204          SELECT *
205          FROM   mtl_cycle_count_entries
206          WHERE  cycle_count_header_id = p_cycle_count_header_id
207          AND    organization_id = p_organization_id
208          AND    subinventory = p_subinventory
209          AND    NVL ( locator_id, -99999 ) = NVL ( p_locator_id, -99999 )
210          AND    NVL ( parent_lpn_id, -99999 ) = NVL ( p_parent_lpn_id, -99999 )
211          AND    inventory_item_id = p_inventory_item_id
212          AND    NVL ( revision, '@@@@@' ) = NVL ( p_revision, '@@@@@' )
213          AND    NVL ( lot_number, '@@@@@' ) = NVL ( p_lot_number, '@@@@@' )
214          AND    entry_status_code IN ( 1, 3 );
215 
216       CURSOR cc_discrepant_cursor
217       IS
218          SELECT *
219          FROM   mtl_cycle_count_entries
220          WHERE  cycle_count_header_id = p_cycle_count_header_id
221          AND    organization_id = p_organization_id
222          AND    NVL ( parent_lpn_id, -99999 ) = NVL ( p_parent_lpn_id, -99999 )
223          AND    inventory_item_id = p_inventory_item_id
224          AND    NVL ( revision, '@@@@@' ) = NVL ( p_revision, '@@@@@' )
225          AND    NVL ( lot_number, '@@@@@' ) = NVL ( p_lot_number, '@@@@@' )
226          AND    NVL ( serial_number, '@@@@@' ) =
227                                              NVL ( l_current_serial, '@@@@@' )
228          AND    entry_status_code IN ( 1, 3 );
229 
230       CURSOR cc_discrepant_multiple_cursor
231       IS
232          SELECT *
233          FROM   mtl_cycle_count_entries
234          WHERE  cycle_count_header_id = p_cycle_count_header_id
235          AND    organization_id = p_organization_id
236          AND    NVL ( parent_lpn_id, -99999 ) = NVL ( p_parent_lpn_id, -99999 )
237          AND    inventory_item_id = p_inventory_item_id
238          AND    NVL ( revision, '@@@@@' ) = NVL ( p_revision, '@@@@@' )
239          AND    NVL ( lot_number, '@@@@@' ) = NVL ( p_lot_number, '@@@@@' )
240          AND    entry_status_code IN ( 1, 3 );
241 
242 -- Bug# 2708133
243 -- Add this cursor to use for discrepant serials.
244 -- Note that we don't match against sub, loc, rev, lot, or LPN
245 -- to allow for location discrepancies as well as rev, lot, and LPN
246       CURSOR cc_discrepant_serial_cursor
247       IS
248          SELECT *
249          FROM   mtl_cycle_count_entries
250          WHERE  cycle_count_header_id = p_cycle_count_header_id
251          AND    organization_id = p_organization_id
252          AND    inventory_item_id = p_inventory_item_id
253          AND    serial_number = NVL ( l_current_serial, '@@@@@' )
254          AND    entry_status_code IN ( 1, 3 );
255 
256       l_prefix  VARCHAR2 ( 30 );
257       l_quantity NUMBER;
258       l_from_number NUMBER;
259       l_to_number NUMBER;
260       l_errorcode NUMBER;
261       l_length  NUMBER;
262       l_padded_length NUMBER;
263       l_current_number NUMBER;
264       l_serial_discrepancy NUMBER;
265       l_container_discrepancy NUMBER;
266 -- Bug# 2386128
267 -- Don't need this variable anymore
268 --l_count_list_sequence    NUMBER;
269       l_cost_group_id NUMBER;
270       l_dispatched_count NUMBER;
271       l_dispatched_task NUMBER;
272       e_Task_Dispatched EXCEPTION;
273       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
274 
275       /* Bug 4891916-Added the following local variables for the call to label printing */
276       l_print_label   NUMBER := NVL(FND_PROFILE.VALUE('WMS_LABEL_FOR_CYCLE_COUNT'),2);
277       l_business_flow_code NUMBER := 8;
278       l_label_status VARCHAR2 ( 300 ) := NULL;
279       l_return_status  VARCHAR2(1)  := fnd_api.g_ret_sts_success;
280       l_return_result   NUMBER := 2; -- Bug 13992956
281       l_serial_count_option  NUMBER; -- Bug 13992956
282       l_serial_detail_option NUMBER; -- Bug 13992956
283 
284       l_msg_count      NUMBER;
285       l_msg_data       VARCHAR2(240);
286       /* End of fix for Bug 4891916 */
287 
288    BEGIN
289       IF ( l_debug = 1 ) THEN
290          print_debug ( '***Calling process_entry with the following parameters***'
291                      );
292          print_debug (    'p_cycle_count_header_id: ===> '
293                        || p_cycle_count_header_id
294                      );
295          print_debug ( 'p_organization_id: =========> ' || p_organization_id );
296          print_debug ( 'p_subinventory: ============> ' || p_subinventory );
297          print_debug ( 'p_locator_id: ==============> ' || p_locator_id );
298          print_debug ( 'p_parent_lpn_id: ===========> ' || p_parent_lpn_id );
299          print_debug ( 'p_inventory_item_id: =======> ' || p_inventory_item_id );
300          print_debug ( 'p_revision: ================> ' || p_revision );
301          print_debug ( 'p_lot_number: ==============> ' || p_lot_number );
302          print_debug ( 'p_from_serial_number: ======> '
303                        || p_from_serial_number
304                      );
305          print_debug ( 'p_to_serial_number: ========> ' || p_to_serial_number );
306          print_debug ( 'p_count_quantity: ==========> ' || p_count_quantity );
307          print_debug ( 'p_count_uom: ===============> ' || p_count_uom );
308          print_debug (    'p_unscheduled_count_entry: => '
309                        || p_unscheduled_count_entry
310                      );
311          print_debug ( 'p_user_id: =================> ' || p_user_id );
312          print_debug ( 'p_cost_group_id: ===========> ' || p_cost_group_id );
313          print_debug ( 'p_secondary_uom: ===========> ' || p_secondary_uom  );   -- INVCONV, NSRIVAST
314          print_debug ( 'p_secondary_qty: ===========> ' || p_secondary_qty  );   -- INVCONV, NSRIVAST
315 
316 
317       END IF;
318 
319 
320       -- Initialize the message stack
321       FND_MSG_PUB.initialize;
322       -- Set the global variables
323 
324 	  --bug 10318308 get the primary uom, change the quantity and uom to primary--should always do calculations in primary uom
325 
326 	  --first store the original count uom and quantity
327 	  g_orig_count_uom := p_count_uom;
328 	  g_orig_count_quantity := p_count_quantity;
329 
330 		IF ( inv_cache.set_item_rec(
331 			   p_organization_id => p_organization_id
332 			 , p_item_id         => p_inventory_item_id ) )
333 		THEN
334 		  IF (l_debug = 1) THEN
335 			print_debug('Got Item info puom='||inv_cache.item_rec.primary_uom_code);
336 		  END IF;
337 		ELSE
338 		  fnd_message.set_name('WMS', 'WMS_CONT_INVALID_ITEM');
339 		  fnd_msg_pub.ADD;
340 		  RAISE fnd_api.g_exc_error;
341 		END IF;
342 
343 	  if(p_count_uom <> inv_cache.item_rec.primary_uom_code) then
344 		  g_count_uom := inv_cache.item_rec.primary_uom_code;
345 		  g_count_quantity := inv_convert.inv_um_convert ( p_inventory_item_id,
346 		                                               p_lot_number,
347                                                        p_organization_id,
348 													  5,
349 													  p_count_quantity,
350 													  p_count_uom,
351 													  g_count_uom,
352 													  NULL,
353 													  NULL
354 													);
355 	  else
356 		g_count_uom := p_count_uom;
357 		g_count_quantity := p_count_quantity;
358 	  end if;
359 	--end bug 10318308
360 
361       g_user_id   := p_user_id;
362       g_count_secondary_quantity  :=  p_secondary_qty ;   -- INVCONV, NSRIVAST
363       g_count_secondary_uom       :=  p_secondary_uom ;   -- INVCONV, NSRIVAST
364       -- Get the profile values
365       get_profiles ( );
366       -- Get the employee ID information
367       get_employee ( p_organization_id => p_organization_id );
368 
369       IF ( l_debug = 1 ) THEN
370          print_debug ( 'Employee ID: ' || g_employee_id );
371       END IF;
372 
373       BEGIN
374          SELECT MIN ( cycle_count_entry_id )
375          INTO   l_dispatched_task
376          FROM   mtl_cycle_count_entries
377          WHERE  cycle_count_header_id = p_cycle_count_header_id
378          AND    organization_id = p_organization_id
379          AND    subinventory = p_subinventory
380          AND    NVL ( locator_id, -99999 ) = NVL ( p_locator_id, -99999 )
381          AND    inventory_item_id = p_inventory_item_id
382          AND    NVL ( revision, '@@@@@' ) = NVL ( p_revision, '@@@@@' )
383          AND    entry_status_code IN ( 1, 3 );
384       EXCEPTION
385          WHEN NO_DATA_FOUND THEN
386             l_dispatched_task := -99999;
387       END;
388 
389       IF ( l_debug = 1 ) THEN
390          print_debug ( 'Task Temp ID: ' || l_dispatched_task );
391       END IF;
392 
393       SELECT COUNT ( * )
394       INTO   l_dispatched_count
395       FROM   wms_dispatched_tasks
396       WHERE  task_type = 3
397       AND    organization_id = p_organization_id
398       AND    transaction_temp_id = l_dispatched_task
399       AND    person_id <> NVL ( g_employee_id, -999 );
400 
401       IF ( l_debug = 1 ) THEN
402          print_debug ( 'Dispatched Count: ' || l_dispatched_count );
403       END IF;
404 
405       -- Cycle Counting task has already been dispatched
406       IF ( l_dispatched_count <> 0 ) THEN
407          IF ( l_debug = 1 ) THEN
408             print_debug (    'The cycle count entry has already been dispatched as a '
409                           || 'task to another user'
410                         );
411          END IF;
412 
413          RAISE e_Task_Dispatched;
414       END IF;
415 
416       -- Check if the cycle count item is a serial controlled item
417       -- This is for single serial count option only
418       IF (      ( p_from_serial_number IS NOT NULL )
419            AND ( p_to_serial_number IS NOT NULL )
420          ) THEN
421          IF ( l_debug = 1 ) THEN
422             print_debug ( 'Single Serial controlled item' );
423          END IF;
424 
425          -- Call this API to parse the serial numbers into prefixes and numbers
426          IF ( NOT MTL_Serial_Check.inv_serial_info ( p_from_serial_number => p_from_serial_number,
427                                                      p_to_serial_number  => p_to_serial_number,
428                                                      x_prefix            => l_prefix,
429                                                      x_quantity          => l_quantity,
430                                                      x_from_number       => l_from_number,
431                                                      x_to_number         => l_to_number,
432                                                      x_errorcode         => l_errorcode
433                                                    )
434             ) THEN
435             FND_MESSAGE.SET_NAME ( 'WMS', 'WMS_CONT_INVALID_SER' );
436             FND_MSG_PUB.ADD;
437             RAISE FND_API.G_EXC_ERROR;
438          END IF;
439 
440          -- Check that in the case of a range of serial numbers, that the
441          -- inputted p_count_quantity equals the amount of items in the serial range.
442          IF (      ( p_count_quantity <> l_quantity )
443               AND ( p_count_quantity <> 0 )
444             ) THEN
445             FND_MESSAGE.SET_NAME ( 'WMS', 'WMS_CONT_INVALID_X_QTY' );
446             FND_MSG_PUB.ADD;
447             RAISE FND_API.G_EXC_ERROR;
448          END IF;
449 
450          -- Get the serial number length.
451          -- Note that the from and to serial numbers must be of the same length.
452          l_length    := LENGTH ( p_from_serial_number );
453          -- Initialize the current pointer variables
454          l_current_serial := p_from_serial_number;
455          l_current_number := l_from_number;
456 
457          LOOP
458             -- For each serial number check if a cycle count entry for it already
459             -- exists or not
460             OPEN cc_entry_cursor;
461             FETCH cc_entry_cursor INTO g_cc_entry;
462 
463             IF ( cc_entry_cursor%FOUND ) THEN
464                -- Entry already exists so update the row
465                pre_update ( );
466                update_row ( );
467             ELSE
468                -- Get the serial and container discrepancy options for the cycle count
469                SELECT NVL ( serial_discrepancy_option, 2 ),
470                       NVL ( container_discrepancy_option, 2 )
471                INTO   l_serial_discrepancy,
472                       l_container_discrepancy
473                FROM   mtl_cycle_count_headers
474                WHERE  cycle_count_header_id = p_cycle_count_header_id
475                AND    organization_id = p_organization_id;
476 
477                -- Check to see if the serial entry exists but is in
478                -- a discrepant location, i.e. different sub/loc
479                -- Bug# 2708133
480                -- Use cc_discrepant_serial_cursor instead which will also
481                -- allow for rev, lot, and LPN discrepancies for serials
482                OPEN cc_discrepant_serial_cursor;
483                FETCH cc_discrepant_serial_cursor INTO g_cc_entry;
484 
485                -- Allow discrepancies if serial discrepancy is allowed
486                -- Bug# 2708133
487                -- We only care if serial discrepancies are allowed now
488                IF (     cc_discrepant_serial_cursor%FOUND
489                     AND l_serial_discrepancy = 1
490                   ) THEN
491                   -- Discrepant serial entry exists so process it
492                   IF ( l_debug = 1 ) THEN
493                      print_debug ( 'Discrepant single serial entry exists' );
494                   END IF;
495 
496                   -- Update the sub and loc information
497                   -- Bug# 2708133
498                   -- Also update the rev, lot, and LPN
499                   g_cc_entry.subinventory := p_subinventory;
500                   g_cc_entry.locator_id := p_locator_id;
501                   g_cc_entry.revision := p_revision;
502                   g_cc_entry.lot_number := p_lot_number;
503                   g_cc_entry.parent_lpn_id := p_parent_lpn_id;
504                   pre_update ( );
505                   update_row ( );
506                ELSE
507                   -- Entry does not exist at all
508                   IF ( p_unscheduled_count_entry = 1 ) THEN
509                      -- Unscheduled entries are allowed so insert the record
510                      IF ( l_debug = 1 ) THEN
511                         print_debug ( 'Unscheduled single serial entry to be inserted'
512                                     );
513                      END IF;
514 
515                      -- Call this procedure to calculate and update the next
516                      -- value for the count list sequence
517                      --print_debug('Calling update_count_list_sequence');
518                      -- Bug# 2386128
519                      -- Dont call this anymore.  Similar to Bug# 1803246 for
520                      -- the desktop forms, when doing an unscheduled cycle
521                      -- count, we will use the cycle count entry ID as the
522                      -- value for the count list sequence
523                      /*update_count_list_sequence
524                        (p_organization_id        => p_organization_id,
525                         p_cycle_count_header_id  => p_cycle_count_header_id,
526                         x_count_list_sequence    => l_count_list_sequence);*/
527 
528                      -- Get the cost group ID for this entry
529                      get_cost_group_id ( p_organization_id   => p_organization_id,
530                                          p_subinventory      => p_subinventory,
531                                          p_locator_id        => p_locator_id,
532                                          p_parent_lpn_id     => p_parent_lpn_id,
533                                          p_inventory_item_id => p_inventory_item_id,
534                                          p_revision          => p_revision,
535                                          p_lot_number        => p_lot_number,
536                                          p_serial_number     => l_current_serial,
537                                          x_out               => l_cost_group_id
538                                        );
539 
540                           -- Bug# 2607187
541                           -- Do not get the default cost group ID.  If the item is
542                           -- new and does not exist in onhand, pass a NULL value
543                           -- for the cost group ID.  The transaction manager will
544                      -- call the cost group rules engine for that if the
545                      -- cost group ID passed into MMTT is null.
546                      IF ( l_cost_group_id = -999 ) THEN
547                         l_cost_group_id := NULL;
548                      END IF;
549 
550                      -- Get the default cost group ID based on the given org
551                      -- and sub if cost group ID was not retrieved successfully
552                      /*IF (l_cost_group_id = -999) THEN
553                         get_default_cost_group_id
554                           (p_organization_id        =>  p_organization_id,
555                       p_subinventory           =>  p_subinventory,
556                       x_out                    =>  l_cost_group_id);
557                      END IF;
558                      -- Default the cost group ID to 1 if nothing can be found
559                      IF (l_cost_group_id = -999) THEN
560                         l_cost_group_id := 1;
561                      END IF;*/
562 
563                      -- First prepare the entry record
564                      g_cc_entry.cycle_count_entry_id := NULL;
565                      g_cc_entry.cycle_count_header_id :=
566                                                        p_cycle_count_header_id;
567                      g_cc_entry.organization_id := p_organization_id;
568                      g_cc_entry.subinventory := p_subinventory;
569                      g_cc_entry.locator_id := p_locator_id;
570                      g_cc_entry.inventory_item_id := p_inventory_item_id;
571                      g_cc_entry.revision := p_revision;
572                      g_cc_entry.lot_number := p_lot_number;
573                      g_cc_entry.serial_number := l_current_serial;
574                      g_cc_entry.parent_lpn_id := p_parent_lpn_id;
575                      g_cc_entry.cost_group_id := l_cost_group_id;
576                      g_cc_entry.entry_status_code := 1;
577                      g_cc_entry.last_update_date := SYSDATE;
578                      g_cc_entry.last_updated_by := p_user_id;
579                      g_cc_entry.creation_date := SYSDATE;
580                      g_cc_entry.created_by := p_user_id;
581                      g_cc_entry.last_update_login := g_login_id;
582                      g_cc_entry.count_list_sequence := NULL;
583                      g_cc_entry.count_type_code := 2;
584                      g_cc_entry.number_of_counts := 0;
585                      -- Now insert the record
586                      pre_insert ( );
587                      insert_row ( );
588                   ELSE
589                      -- Unscheduled entries are not allowed
590                      IF ( l_debug = 1 ) THEN
591                         print_debug ( 'Unscheduled entries are not allowed' );
592                      END IF;
593                   --FND_MESSAGE.SET_NAME('INV', 'INV_NO_UNSCHED_COUNTS');
594                   --FND_MSG_PUB.ADD;
595                   --RAISE FND_API.G_EXC_ERROR;
596                   END IF;
597                END IF;
598 
599                CLOSE cc_discrepant_serial_cursor;
600             END IF;
601 
602             CLOSE cc_entry_cursor;
603             EXIT WHEN l_current_serial = p_to_serial_number;
604             -- Increment the current serial number
605             l_current_number := l_current_number + 1;
606             l_padded_length := l_length - LENGTH ( l_current_number );
607             l_current_serial :=
608                         RPAD ( l_prefix, l_padded_length, '0' )
609                      || l_current_number;
610          END LOOP;
611       -- This is for multiple serial count option
612       ELSIF ( p_count_quantity IS NULL ) THEN
613          IF ( l_debug = 1 ) THEN
614             print_debug ( 'Multiple serial controlled item' );
615          END IF;
616 
617          -- Set a savepoint
618          SAVEPOINT save_serial_detail;
619 
620          -- For the inputted entries, see if a multiple serial
621          -- cycle count entry for it exists
622          OPEN cc_multiple_serial_cursor;
623          FETCH cc_multiple_serial_cursor INTO g_cc_entry;
624 
625          IF ( cc_multiple_serial_cursor%FOUND ) THEN
626             -- The entry exists so process all of the multiple
627             -- serial cycle count entries associated with it
628 
629             -- Bug 13992956
630 
631             SELECT serial_count_option,
632                    serial_detail_option
633             INTO   l_serial_count_option,
634                    l_serial_detail_option
635             FROM mtl_cycle_count_headers
636             WHERE cycle_count_header_id = p_cycle_count_header_id
637             AND   organization_id       = p_organization_id;
638 
639             IF ( l_debug = 1 ) THEN
640                  print_debug('l_serial_count_option: ' || l_serial_count_option || ', l_serial_detail_option: ' || l_serial_detail_option);
641             END IF;
642 
643             -- if this cycle count is "Multiple Per Request" Count and "Quantity Only" Detail
644             -- calling the procedure process_multiple_qty_only
645             IF l_serial_count_option = 3 AND l_serial_detail_option = 2 THEN
646 
647                  process_multiple_qty_only ( x_return_result => l_return_result );
648 
649                  IF ( l_debug = 1 ) THEN
650                       print_debug('Value of l_return_result after calling process_multiple_qty_only = ' || l_return_result );
651                  END IF;
652 
653             END IF;
654 
655             -- if system quantity is not equal to count qty, process serials one
656             -- by one using existing logic
657 
658             IF l_return_result = 2 THEN
659                  ok_proc ( );
660                  pre_update ( );
661                  update_row ( );
662             END IF;
663 
664          ELSE
665             -- Get the serial and container discrepancy options for the cycle count
666             SELECT NVL ( serial_discrepancy_option, 2 ),
667                    NVL ( container_discrepancy_option, 2 )
668             INTO   l_serial_discrepancy,
669                    l_container_discrepancy
670             FROM   mtl_cycle_count_headers
671             WHERE  cycle_count_header_id = p_cycle_count_header_id
672             AND    organization_id = p_organization_id;
673 
674             -- Check to see if the multiple serial entry exists but
675             -- is in a discrepant location, i.e. different sub/loc
676             OPEN cc_discrepant_multiple_cursor;
677             FETCH cc_discrepant_multiple_cursor INTO g_cc_entry;
678 
679             -- Allow discrepancies if container discrepancy is allowed and
680             -- there is an LPN, or serial discrepancy is allowed and there
681             -- is no LPN
682             IF (     cc_discrepant_multiple_cursor%FOUND
683                  AND (     (     l_container_discrepancy = 1
684                              AND p_parent_lpn_id IS NOT NULL
685                            )
686                        OR (     l_serial_discrepancy = 1
687                             AND p_parent_lpn_id IS NULL
688                           )
689                      )
690                ) THEN
691                -- Discrepant multiple serial entry exists so process it
692                IF ( l_debug = 1 ) THEN
693                   print_debug ( 'Discrepant multiple serial entry exists' );
694                END IF;
695 
696                -- Update the sub and loc information
697                g_cc_entry.subinventory := p_subinventory;
698                g_cc_entry.locator_id := p_locator_id;
699                ok_proc ( );
700                pre_update ( );
701                update_row ( );
702             ELSE
703                -- Unscheduled multiple count entried are
704                -- not being supported right now
705                IF ( l_debug = 1 ) THEN
706                   print_debug ( 'Unscheduled multiple serial count' );
707                END IF;
708             END IF;
709 
710             CLOSE cc_discrepant_multiple_cursor;
711          END IF;
712 
713          CLOSE cc_multiple_serial_cursor;
714       ELSE -- Item is not serial controlled
715          IF ( l_debug = 1 ) THEN
716             print_debug ( 'Non serial controlled item' );
717          END IF;
718 
719          OPEN cc_entry_cursor;
720          FETCH cc_entry_cursor INTO g_cc_entry;
721 
722          IF ( cc_entry_cursor%FOUND ) THEN
723             pre_update ( );
724             update_row ( );
725          ELSE
726             -- Get the container discrepancy option for the cycle count
727             SELECT NVL ( container_discrepancy_option, 2 )
728             INTO   l_container_discrepancy
729             FROM   mtl_cycle_count_headers
730             WHERE  cycle_count_header_id = p_cycle_count_header_id
731             AND    organization_id = p_organization_id;
732 
733             -- Check to see if the entry exists but
734             -- is in a discrepant location, i.e. different sub/loc
735             OPEN cc_discrepant_cursor;
736             FETCH cc_discrepant_cursor INTO g_cc_entry;
737 
738             -- Allow discrepancies if container discrepancy is allowed and
739             -- there is an LPN
740             IF (     cc_discrepant_cursor%FOUND
741                  AND (     l_container_discrepancy = 1
742                        AND p_parent_lpn_id IS NOT NULL
743                      )
744                ) THEN
745                -- Discrepant containerized entry exists so process it
746                IF ( l_debug = 1 ) THEN
747                   print_debug ( 'Discrepant non-serial entry exists' );
748                END IF;
749 
750                -- Update the sub and loc information
751                g_cc_entry.subinventory := p_subinventory;
752                g_cc_entry.locator_id := p_locator_id;
753                pre_update ( );
754                update_row ( );
755             ELSE
756                -- Entry does not exist at all
757                IF ( p_unscheduled_count_entry = 1 ) THEN
758                   -- Unscheduled entries are allowed so insert the record
759                   IF ( l_debug = 1 ) THEN
760                      print_debug ( 'Unscheduled non-serial entry to be inserted'
761                                  );
762                   END IF;
763 
764                   -- Call this procedure to calculate and update the next
765                   -- value for the count list sequence
766                   --print_debug('Calling update_count_list_sequence');
767                   -- Bug# 2386128
768                   -- Don't call this anymore.  Similar to Bug# 1803246 for
769                   -- the desktop forms, when doing an unscheduled cycle
770                   -- count, we will use the cycle count entry ID as the
771                   -- value for the count list sequence
772                   /*update_count_list_sequence
773                     (p_organization_id        => p_organization_id,
774                 p_cycle_count_header_id  => p_cycle_count_header_id,
775                 x_count_list_sequence    => l_count_list_sequence);*/
776 
777                   -- Get the cost group ID for this entry
778                   get_cost_group_id ( p_organization_id   => p_organization_id,
779                                       p_subinventory      => p_subinventory,
780                                       p_locator_id        => p_locator_id,
781                                       p_parent_lpn_id     => p_parent_lpn_id,
782                                       p_inventory_item_id => p_inventory_item_id,
783                                       p_revision          => p_revision,
784                                       p_lot_number        => p_lot_number,
785                                       p_serial_number     => NULL,
786                                       x_out               => l_cost_group_id
787                                     );
788 
789                        -- Bug# 2607187
790                        -- Do not get the default cost group ID.  If the item is
791                        -- new and does not exist in onhand, pass a NULL value
792                   -- for the cost group ID.  The transaction manager will
793                   -- call the cost group rules engine for that if the
794                   -- cost group ID passed into MMTT is null.
795                   IF ( l_cost_group_id = -999 ) THEN
796                      l_cost_group_id := NULL;
797                   END IF;
798 
799                   -- Get the default cost group ID based on the given org
800                   -- and sub if cost group ID was not retrieved successfully
801                   /*IF (l_cost_group_id = -999) THEN
802                 get_default_cost_group_id
803                   (p_organization_id        =>  p_organization_id,
804                    p_subinventory           =>  p_subinventory,
805                    x_out                    =>  l_cost_group_id);
806                   END IF;
807                   -- Default the cost group ID to 1 if nothing can be found
808                   IF (l_cost_group_id = -999) THEN
809                 l_cost_group_id := 1;
810                   END IF;*/
811 
812                   -- First prepare the entry record
813                   g_cc_entry.cycle_count_entry_id := NULL;
814                   g_cc_entry.cycle_count_header_id := p_cycle_count_header_id;
815                   g_cc_entry.organization_id := p_organization_id;
816                   g_cc_entry.subinventory := p_subinventory;
817                   g_cc_entry.locator_id := p_locator_id;
818                   g_cc_entry.inventory_item_id := p_inventory_item_id;
819                   g_cc_entry.revision := p_revision;
820                   g_cc_entry.lot_number := p_lot_number;
821                   g_cc_entry.serial_number := NULL;
822                   g_cc_entry.parent_lpn_id := p_parent_lpn_id;
823                   g_cc_entry.cost_group_id := l_cost_group_id;
824                   g_cc_entry.entry_status_code := 1;
825                   g_cc_entry.last_update_date := SYSDATE;
826                   g_cc_entry.last_updated_by := p_user_id;
827                   g_cc_entry.creation_date := SYSDATE;
828                   g_cc_entry.created_by := p_user_id;
829                   g_cc_entry.last_update_login := g_login_id;
830                   g_cc_entry.count_list_sequence := NULL;
831                   g_cc_entry.count_type_code := 2;
832                   g_cc_entry.number_of_counts := 0;
833                   -- Now insert the record
834                   pre_insert ( );
835                   insert_row ( );
836                ELSE
837                   -- Unscheduled entries are not allowed
838                   IF ( l_debug = 1 ) THEN
839                      print_debug ( 'Unscheduled entries are not allowed' );
840                   END IF;
841                --FND_MESSAGE.SET_NAME('INV', 'INV_NO_UNSCHED_COUNTS');
842                --FND_MSG_PUB.ADD;
843                --RAISE FND_API.G_EXC_ERROR;
844                END IF;
845             END IF;
846 
847             CLOSE cc_discrepant_cursor;
848          END IF;
849 
850          CLOSE cc_entry_cursor;
851       END IF;
852 
853       /*  Bug 4891916 -Calling the label printing api if the profile value corresponds to
854                     At Entry(1) or At Entry and Approval(3)  */
855 
856         IF (l_print_label IN (1,3) ) THEN
857 
858            IF ( l_debug = 1 ) THEN
859             print_debug ( 'Values of parameters passed to label printing API:' );
860             print_debug ( 'Values of l_business_flow_code'|| l_business_flow_code );
861             print_debug ( 'Values of g_cc_entry.cycle_count_entry_id'|| g_cc_entry.cycle_count_entry_id );
862            END IF;
863            /* Calling with the value of p_transaction_identifier as 4.
864               In the label printing code, this value will indicate
865               that the call is at the time of performing a cycle count entry
866               The value of p_transaction_identifier 5 in the label printing api
867               indicates that the call is at the time of approving counts.*/
868 
869           inv_label.print_label_wrap
870             ( x_return_status          =>  l_return_status        ,
871               x_msg_count              =>  l_msg_count            ,
872               x_msg_data               =>  l_msg_data             ,
873               x_label_status           =>  l_label_status         ,
874               p_business_flow_code     =>  l_business_flow_code   ,
875               p_transaction_id         =>  g_cc_entry.cycle_count_entry_id ,
876               p_transaction_identifier =>  4);
877 
878           IF ( l_debug = 1 ) THEN
879              print_debug ( 'Values of l_return_status:'|| l_return_status );
880              print_debug ( 'Values of l_msg_count:'|| l_msg_count );
881              print_debug ( 'Values of l_label_status'|| l_label_status );
882           END IF;
883 
884           IF (l_return_status <> fnd_api.g_ret_sts_success) THEN
885            IF (l_debug = 1) THEN
886              print_debug('**Label Printing returned error:' || l_return_status);
887            END IF;
888          END IF;
889 
890        END IF ; --End of check for l_print_label value
891 
892    /* End of fix for Bug 4891916 */
893 
894 
895       IF ( l_debug = 1 ) THEN
896          print_debug ( '***End of process_entry***' );
897       END IF;
898    EXCEPTION
899       WHEN e_Task_Dispatched THEN
900          FND_MESSAGE.SET_NAME ( 'WMS', 'WMS_TD_CYC_TASK_ERROR' );
901          FND_MSG_PUB.ADD;
902 
903          IF ( l_debug = 1 ) THEN
904             print_debug ( '***End of process_entry***' );
905          END IF;
906    END process_entry;
907 
908     /* start of fix for 4539926 */
909    PROCEDURE delete_wdt(
910       p_cycle_count_header_id    IN    NUMBER            ,
911       p_organization_id          IN    NUMBER            ,
912       p_subinventory             IN    VARCHAR2          ,
913       p_locator_id               IN    NUMBER            ,
914       p_parent_lpn_id            IN    NUMBER            ,
915       p_inventory_item_id        IN    NUMBER            ,
916       p_revision                 IN    VARCHAR2          ,
917       p_lot_number               IN    VARCHAR2          ,
918       p_from_serial_number       IN    VARCHAR2          ,
919       p_to_serial_number         IN    VARCHAR2          ,
920       p_count_quantity           IN    NUMBER            ,
921       p_count_uom                IN    VARCHAR2          ,
922       p_unscheduled_count_entry  IN    NUMBER            ,
923       p_user_id                  IN    NUMBER            ,
924       p_cost_group_id            IN    NUMBER            )
925 
926       IS
927          l_cycle_count_entry_id NUMBER;
928          l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
929 
930          CURSOR cc_entry_cursor
931          IS
932             SELECT  cycle_count_entry_id
933             FROM   mtl_cycle_count_entries mcce, wms_dispatched_tasks wdt
934             WHERE  mcce.cycle_count_header_id = p_cycle_count_header_id
935             AND    mcce.cycle_count_entry_id = wdt.transaction_temp_id
936             AND    mcce.organization_id = p_organization_id
937             AND    mcce.subinventory = p_subinventory
938             AND    mcce.locator_id = p_locator_id
939             AND    NVL (mcce.parent_lpn_id, -99999) = NVL ( p_parent_lpn_id, -99999)
940             AND    mcce.inventory_item_id = NVL (p_inventory_item_id, mcce.inventory_item_id)
941             AND    NVL (mcce.revision, '@@@@@' ) = NVL (p_revision , '@@@@@' )
942             AND    NVL (mcce.lot_number, '@@@@@' ) = NVL ( p_lot_number, '@@@@@' )
943             AND    NVL (mcce.serial_number, '@@@@@' ) = NVL ( p_from_serial_number, '@@@@@' )
944             AND    NVL (mcce.serial_number, '@@@@@' ) = NVL ( p_to_serial_number, '@@@@@' )
945             AND    mcce.entry_status_code IN (2, 4, 5 );
946 
947      BEGIN
948          IF ( l_debug = 1 ) THEN
949             print_debug ( '***In delete_wdt ***');
950             print_debug ( 'p_cycle_count_header_id: ===> '
951                           || p_cycle_count_header_id);
952             print_debug ( 'p_organization_id: =========> ' || p_organization_id );
953             print_debug ( 'p_subinventory: ============> ' || p_subinventory );
954             print_debug ( 'p_locator_id: ==============> ' || p_locator_id );
955             print_debug ( 'p_parent_lpn_id: ===========> ' || p_parent_lpn_id );
956             print_debug ( 'p_inventory_item_id: =======> ' || p_inventory_item_id );
957             print_debug ( 'p_revision: ================> ' || p_revision );
958             print_debug ( 'p_lot_number: ==============> ' || p_lot_number );
959             print_debug ( 'p_from_serial_number: ======> '
960                           || p_from_serial_number);
961             print_debug ( 'p_to_serial_number: ========> ' || p_to_serial_number );
962             print_debug ( 'p_count_quantity: ==========> ' || p_count_quantity );
963             print_debug ( 'p_count_uom: ===============> ' || p_count_uom );
964             print_debug ( 'p_unscheduled_count_entry: => '
965                           || p_unscheduled_count_entry);
966             print_debug ( 'p_user_id: =================> ' || p_user_id );
967             print_debug ( 'p_cost_group_id: ===========> ' || p_cost_group_id );
968          END IF;
969 
970          -- Initialize the message stack
971          FND_MSG_PUB.initialize;
972 
973          -- To fetch the cycle count entry ids which are in status 2,4,5.
974          -- for these entries if a record in wms_dispatched_tasks exists,
975          -- it has to be deleted
976 
977          OPEN cc_entry_cursor;
978            LOOP
979 
980             FETCH cc_entry_cursor INTO l_cycle_count_entry_id;
981              IF ( cc_entry_cursor%FOUND ) THEN
982                 IF ( l_debug = 1 ) THEN
983                    print_debug ( 'Approval Pending/ Rejected/ Completed cycle count entries found' );
984                    print_debug( 'Cycle count Entry Id: ' || l_cycle_count_entry_id);
985                 END IF;
986                 BEGIN
987                    DELETE FROM wms_dispatched_tasks wdt
988                    WHERE wdt.transaction_temp_id = l_cycle_count_entry_id;
989                    IF ( l_debug = 1 ) THEN
990                       print_debug('** Deleted wms_dispatched_tasks record with transaction_temp_id : ' || l_cycle_count_entry_id);
991                    END IF;
992                 EXCEPTION
993                    WHEN OTHERS THEN
994                    IF ( l_debug = 1 ) THEN
995                       print_debug('Deleting wms_dispatched_tasks record failed');
996                    END IF;
997                 END;
998              ELSE
999                 IF ( l_debug = 1 ) THEN
1000                    print_debug ( 'No Approval Pending/ Rejected/ Completed cycle count entries found');
1001                 END IF;
1002                 EXIT;   -- Bug No 5068178, moved this exit outside the if condition.
1003              END IF;
1004            END LOOP;
1005          CLOSE cc_entry_cursor;
1006          IF ( l_debug = 1 ) THEN
1007               print_debug ( 'Exiting delete_wdt');
1008          END IF;
1009 
1010    END delete_wdt;
1011 
1012    /* end of fix for 4539926 */
1013 
1014 
1015 
1016 
1017 
1018 
1019    PROCEDURE insert_row
1020    IS
1021       l_return_status VARCHAR2 ( 300 );
1022       l_msg_count NUMBER;
1023       l_msg_data VARCHAR2 ( 300 );
1024       l_lpn_list WMS_Container_PUB.LPN_Table_Type;
1025       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
1026 	  l_current_quantity number; --10318308
1027 	  l_current_quantity_prim number; --10318308
1028 	  l_adjustment_quantity number; --10318308
1029 	  l_adjustment_quantity_prim number; --10318308
1030 	  l_uom varchar2(3) := g_orig_count_uom; --bug10318308
1031    BEGIN
1032       IF ( l_debug = 1 ) THEN
1033          print_debug ( '***insert_row***' );
1034       END IF;
1035 
1036       -- Get the outermost LPN ID if this entry contains an LPN
1037       IF ( g_cc_entry.parent_lpn_id IS NOT NULL ) THEN
1038          --Bug2935754 starts
1039          /*
1040          WMS_Container_PUB.Get_Outermost_LPN ( p_api_version       => 1.0,
1041                                                x_return_status     => l_return_status,
1042                                                x_msg_count         => l_msg_count,
1043                                                x_msg_data          => l_msg_data,
1044                                                p_lpn_id            => g_cc_entry.parent_lpn_id,
1045                                                x_lpn_list          => l_lpn_list
1046                                              );
1047          g_cc_entry.outermost_lpn_id := l_lpn_list ( 1 ).lpn_id;
1048          */
1049        BEGIN
1050            SELECT outermost_lpn_id
1051            INTO   g_cc_entry.outermost_lpn_id
1052            FROM   WMS_LICENSE_PLATE_NUMBERS
1053            WHERE  lpn_id = g_cc_entry.parent_lpn_id;
1054        EXCEPTION
1055            WHEN OTHERS THEN
1056              IF(l_debug = 1) THEN
1057                print_debug('Unable to get the Outermost LPN ID for: ' || g_cc_entry.parent_lpn_id);
1058              END IF;
1059              RAISE  FND_API.G_EXC_ERROR;
1060        END;
1061           --Bug2935754 ends
1062       ELSE
1063          g_cc_entry.outermost_lpn_id := NULL;
1064       END IF;
1065 
1066 
1067 	  /* bug 10318308 Quantity calculations were all done in primary UOM (q_p)
1068 		 Convert the quantities back to transaction UOM (q_t) then again back to primary UOM (q_p').
1069 
1070 
1071 		 If the primary values match (q_p =q_p') then conversion to transaction UOM is exact, so insert q_t
1072 		 Otherwise, insert q_p */
1073 	  IF g_count_uom <> g_orig_count_uom then
1074 		IF ( l_debug = 1 ) THEN
1075 		 print_debug('g_cc_entry.count_quantity_current: ' || g_cc_entry.count_quantity_current);
1076 		END IF;
1077 
1078 		 l_current_quantity := inv_convert.inv_um_convert ( g_cc_entry.inventory_item_id,
1079 													  5,
1080 													  g_cc_entry.count_quantity_current,
1081 													  g_cc_entry.count_uom_current,  --same as g_count_uom, should be primary_uom
1082 													  g_orig_count_uom, --transaction_uom
1083 													  NULL,
1084 													  NULL
1085 												);
1086 		IF ( l_debug = 1 ) THEN
1087 		 print_debug('l_current_quantity: ' || l_current_quantity);
1088 		 END IF;
1089 		 l_current_quantity_prim := inv_convert.inv_um_convert ( g_cc_entry.inventory_item_id,
1090 													  5,
1091 													  l_current_quantity,
1092 													  g_orig_count_uom, --transaction_uom
1093 													  g_cc_entry.count_uom_current, --same as g_count_uom, should be primary_uom
1094 													  NULL,
1095 													  NULL
1096 													);
1097 		IF ( l_debug = 1 ) THEN
1098 		print_debug('l_current_quantity_prim: ' || l_current_quantity_prim);
1099 		print_debug('g_cc_entry.adjustment_quantity: ' || g_cc_entry.adjustment_quantity);
1100 		END IF;
1101 		 l_adjustment_quantity := inv_convert.inv_um_convert ( g_cc_entry.inventory_item_id,
1102 													  5,
1103 													  g_cc_entry.adjustment_quantity,
1104 													  g_cc_entry.count_uom_current, --same as g_count_uom, should be primary_uom
1105 													  g_orig_count_uom, --transaction_uom
1106 													  NULL,
1107 													  NULL
1108 											);
1109 		IF ( l_debug = 1 ) THEN
1110 		print_debug('l_adjustment_quantity: ' || l_adjustment_quantity);
1111 		END IF;
1112 		 l_adjustment_quantity_prim := inv_convert.inv_um_convert ( g_cc_entry.inventory_item_id,
1113 													  5,
1114 													  l_adjustment_quantity,
1115 													  g_orig_count_uom, --transaction_uom
1116 													  g_cc_entry.count_uom_current, --same as g_count_uom, should be primary_uom
1117 													  NULL,
1118 													  NULL
1119 												);
1120 		IF ( l_debug = 1 ) THEN
1121 		print_debug('l_adjustment_quantity_prim: ' || l_adjustment_quantity_prim);
1122 		END IF;
1123 
1124 		if (l_current_quantity_prim <> g_cc_entry.count_quantity_current)
1125 		    or (l_adjustment_quantity_prim <> g_cc_entry.adjustment_quantity) then --then there is issue converting back to trans uom so use first primary
1126 			l_current_quantity := g_cc_entry.count_quantity_current;
1127 			l_adjustment_quantity := g_cc_entry.adjustment_quantity;
1128 			l_uom := g_cc_entry.count_uom_current;
1129 
1130 	        end if;
1131      else
1132 	         l_current_quantity := g_cc_entry.count_quantity_current; -- 11782517
1133 	         l_adjustment_quantity := g_cc_entry.adjustment_quantity;
1134 		 l_uom := g_cc_entry.count_uom_current;
1135 
1136 	  end if;
1137 		/* end 10318308 */
1138 
1139 		--bug11864694 secondary_adjustment_quantity needs to be rounded to 5 decimals...
1140 		g_cc_entry.SECONDARY_ADJUSTMENT_QUANTITY:=round(g_cc_entry.SECONDARY_ADJUSTMENT_QUANTITY,5);
1141 		g_cc_entry.SECONDARY_SYSTEM_QTY_CURRENT:=round(g_cc_entry.SECONDARY_SYSTEM_QTY_CURRENT,5);
1142 
1143 
1144 --bug fix 7429096 kbanddyo   added few columns in the insert statement related to sec qty .
1145       INSERT INTO mtl_cycle_count_entries
1146                   ( cycle_count_entry_id,
1147                     last_update_date,
1148                     last_updated_by,
1149                     creation_date,
1150                     created_by,
1151                     last_update_login,
1152                     count_list_sequence,
1153                     count_date_first,
1154                     count_date_current,
1155 -- Commented the Line Below for Bug 8333418
1156 --                    count_date_prior,
1157                     count_date_dummy,
1158                     counted_by_employee_id_first,
1159                     counted_by_employee_id_current,
1160 -- Commented the Line Below for Bug 8333418
1161 --                    counted_by_employee_id_prior,
1162                     counted_by_employee_id_dummy,
1163                     count_uom_first,
1164                     count_uom_current,
1165 -- Commented the Line Below for Bug 8333418
1166 --                    count_uom_prior,
1167                     count_quantity_first,
1168                     count_quantity_current,
1169 -- Commented the Line Below for Bug 8333418
1170 --                    count_quantity_prior,
1171                     inventory_item_id,
1172                     subinventory,
1173                     entry_status_code,
1174                     count_due_date,
1175                     organization_id,
1176                     cycle_count_header_id,
1177                     number_of_counts,
1178                     locator_id,
1179                     adjustment_quantity,
1180                     adjustment_date,
1181                     adjustment_amount,
1182                     item_unit_cost,
1183                     inventory_adjustment_account,
1184                     approval_date,
1185                     approver_employee_id,
1186                     revision,
1187                     lot_number,
1188                     lot_control,
1189                     system_quantity_first,
1190                     system_quantity_current,
1191 -- Commented the Line Below for Bug 8333418
1192 --                    system_quantity_prior,
1193                     reference_first,
1194                     reference_current,
1195 -- Commented the Line Below for Bug 8333418
1196 --                    reference_prior,
1197                     primary_uom_quantity_first,
1198                     primary_uom_quantity_current,
1199 -- Commented the Line Below for Bug 8333418
1200 --                    primary_uom_quantity_prior,
1201                     count_type_code,
1202                     transaction_reason_id,
1203                     request_id,
1204                     program_application_id,
1205                     program_id,
1206                     program_update_date,
1207                     approval_type,
1208                     attribute_category,
1209                     attribute1,
1210                     attribute2,
1211                     attribute3,
1212                     attribute4,
1213                     attribute5,
1214                     attribute6,
1215                     attribute7,
1216                     attribute8,
1217                     attribute9,
1218                     attribute10,
1219                     attribute11,
1220                     attribute12,
1221                     attribute13,
1222                     attribute14,
1223                     attribute15,
1224                     serial_number,
1225                     serial_detail,
1226                     approval_condition,
1227                     neg_adjustment_quantity,
1228                     neg_adjustment_amount,
1229                     export_flag,
1230                     task_priority,
1231                     standard_operation_id,
1232                     parent_lpn_id,
1233                     outermost_lpn_id,
1234                     cost_group_id
1235                     -- INVCONV, NSRIVAST
1236                     ,secondary_uom_quantity_first
1237                     ,secondary_uom_quantity_current
1238                     ,secondary_uom_quantity_prior
1239                     ,count_secondary_uom_first
1240                     ,count_secondary_uom_current
1241                     ,count_secondary_uom_prior
1242                     -- INVCONV, NSRIVAST
1243                     ,SECONDARY_ADJUSTMENT_QUANTITY     --bug fix 7429096
1244                     ,SECONDARY_SYSTEM_QTY_FIRST             --bug fix 7429096
1245                     ,SECONDARY_SYSTEM_QTY_CURRENT      --bug fix 7429096
1246                   )
1247            VALUES ( g_cc_entry.cycle_count_entry_id,
1248                     g_cc_entry.last_update_date,
1249                     g_cc_entry.last_updated_by,
1250                     g_cc_entry.creation_date,
1251                     g_cc_entry.created_by,
1252                     g_cc_entry.last_update_login,
1253                     g_cc_entry.count_list_sequence,
1254                     g_cc_entry.count_date_first,
1255                     g_cc_entry.count_date_current,
1256 -- Commented the Line Below for Bug 8333418
1257 --                    g_cc_entry.count_date_prior,
1258                     g_cc_entry.count_date_dummy,
1259                     g_cc_entry.counted_by_employee_id_first,
1260                     g_cc_entry.counted_by_employee_id_current,
1261 -- Commented the Line Below for Bug 8333418
1262 --                    g_cc_entry.counted_by_employee_id_prior,
1263                     g_cc_entry.counted_by_employee_id_dummy,
1264                     g_cc_entry.count_uom_first,
1265                     l_uom, --10318308
1266 -- Commented the Line Below for Bug 8333418
1267 --                    g_cc_entry.count_uom_prior,
1268                     g_cc_entry.count_quantity_first,
1269                     l_current_quantity, --10318308
1270 -- Commented the Line Below for Bug 8333418
1271 --                    g_cc_entry.count_quantity_prior,
1272                     g_cc_entry.inventory_item_id,
1273                     g_cc_entry.subinventory,
1274                     g_cc_entry.entry_status_code,
1275                     g_cc_entry.count_due_date,
1276                     g_cc_entry.organization_id,
1277                     g_cc_entry.cycle_count_header_id,
1278                     g_cc_entry.number_of_counts,
1279                     g_cc_entry.locator_id,
1280                     l_adjustment_quantity, --10318308
1281                     g_cc_entry.adjustment_date,
1282                     g_cc_entry.adjustment_amount,
1283                     g_cc_entry.item_unit_cost,
1284                     g_cc_entry.inventory_adjustment_account,
1285                     g_cc_entry.approval_date,
1286                     g_cc_entry.approver_employee_id,
1287                     g_cc_entry.revision,
1288                     g_cc_entry.lot_number,
1289                     g_cc_entry.lot_control,
1290                     g_cc_entry.system_quantity_first,
1291                     g_cc_entry.system_quantity_current,
1292 -- Commented the Line Below for Bug 8333418
1293 --                    g_cc_entry.system_quantity_prior,
1294                     g_cc_entry.reference_first,
1295                     g_cc_entry.reference_current,
1296 -- Commented the Line Below for Bug 8333418
1297 --                    g_cc_entry.reference_prior,
1298                     g_cc_entry.primary_uom_quantity_first,
1299                     g_cc_entry.primary_uom_quantity_current,
1300 -- Commented the Line Below for Bug 8333418
1301 --                    g_cc_entry.primary_uom_quantity_prior,
1302                     g_cc_entry.count_type_code,
1303                     g_cc_entry.transaction_reason_id,
1304                     g_cc_entry.request_id,
1305                     g_cc_entry.program_application_id,
1306                     g_cc_entry.program_id,
1307                     g_cc_entry.program_update_date,
1308                     g_cc_entry.approval_type,
1309                     g_cc_entry.attribute_category,
1310                     g_cc_entry.attribute1,
1311                     g_cc_entry.attribute2,
1312                     g_cc_entry.attribute3,
1313                     g_cc_entry.attribute4,
1314                     g_cc_entry.attribute5,
1315                     g_cc_entry.attribute6,
1316                     g_cc_entry.attribute7,
1317                     g_cc_entry.attribute8,
1318                     g_cc_entry.attribute9,
1319                     g_cc_entry.attribute10,
1320                     g_cc_entry.attribute11,
1321                     g_cc_entry.attribute12,
1322                     g_cc_entry.attribute13,
1323                     g_cc_entry.attribute14,
1324                     g_cc_entry.attribute15,
1325                     LTRIM ( RTRIM ( g_cc_entry.serial_number ) ),
1326                                                                 /* BUG2842145*/
1327                     g_cc_entry.serial_detail,
1328                     g_cc_entry.approval_condition,
1329                     g_cc_entry.neg_adjustment_quantity,
1330                     g_cc_entry.neg_adjustment_amount,
1331                     g_cc_entry.export_flag,
1332                     g_cc_entry.task_priority,
1333                     g_cc_entry.standard_operation_id,
1334                     g_cc_entry.parent_lpn_id,
1335                     g_cc_entry.outermost_lpn_id,
1336                     g_cc_entry.cost_group_id
1337                     -- INVCONV, NSRIVAST
1338                     ,g_cc_entry.secondary_uom_quantity_first
1339                     ,g_cc_entry.secondary_uom_quantity_current
1340                     ,g_cc_entry.secondary_uom_quantity_prior
1341                     ,g_cc_entry.count_secondary_uom_first
1342                     ,g_cc_entry.count_secondary_uom_current
1343                     ,g_cc_entry.count_secondary_uom_prior
1344                     -- INVCONV, NSRIVAST
1345                     ,g_cc_entry.SECONDARY_ADJUSTMENT_QUANTITY  --bug fix 7429096
1346                     ,g_cc_entry.SECONDARY_SYSTEM_QTY_FIRST          --bug fix 7429096
1347                     ,g_cc_entry.SECONDARY_SYSTEM_QTY_CURRENT   --bug fix 7429096
1348                   );
1349 
1350       IF ( SQL%NOTFOUND ) THEN
1351          RAISE NO_DATA_FOUND;
1352       END IF;
1353    END insert_row;
1354 
1355    PROCEDURE update_row
1356    IS
1357       l_return_status VARCHAR2 ( 300 );
1358       l_msg_count NUMBER;
1359       l_msg_data VARCHAR2 ( 300 );
1360       l_lpn_list WMS_Container_PUB.LPN_Table_Type;
1361       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
1362       --bug 10298222
1363       l_temp_cc_id NUMBER;
1364    BEGIN
1365       IF ( l_debug = 1 ) THEN
1366          print_debug ( '***update_row***' );
1367       END IF;
1368 
1369       -- Set the WHO column information
1370       g_cc_entry.last_update_date := SYSDATE;
1371       g_cc_entry.last_updated_by := g_user_id;
1372       g_cc_entry.last_update_login := g_login_id;
1373 
1374       -- Get the outermost LPN ID if this entry contains an LPN
1375       IF ( g_cc_entry.parent_lpn_id IS NOT NULL ) THEN
1376          --Bug2935754
1377          /*
1378          WMS_Container_PUB.Get_Outermost_LPN ( p_api_version       => 1.0,
1379                                                x_return_status     => l_return_status,
1380                                                x_msg_count         => l_msg_count,
1381                                                x_msg_data          => l_msg_data,
1382                                                p_lpn_id            => g_cc_entry.parent_lpn_id,
1383                                                x_lpn_list          => l_lpn_list
1384                                              );
1385          g_cc_entry.outermost_lpn_id := l_lpn_list ( 1 ).lpn_id;*/
1386        BEGIN
1387            SELECT outermost_lpn_id
1388            INTO   g_cc_entry.outermost_lpn_id
1389            FROM   WMS_LICENSE_PLATE_NUMBERS
1390            WHERE  lpn_id = g_cc_entry.parent_lpn_id;
1391        EXCEPTION
1392            WHEN OTHERS THEN
1393              IF(l_debug = 1) THEN
1394                print_debug('Unable to get the Outermost LPN ID for: ' || g_cc_entry.parent_lpn_id);
1395              END IF;
1396              RAISE  FND_API.G_EXC_ERROR;
1397        END;
1398           --Bug2935754 ends
1399       ELSE
1400          g_cc_entry.outermost_lpn_id := NULL;
1401       END IF;
1402 
1403       UPDATE mtl_cycle_count_entries
1404       SET last_update_date = g_cc_entry.last_update_date,
1405           last_updated_by = g_cc_entry.last_updated_by,
1406           last_update_login = g_cc_entry.last_update_login,
1407           count_list_sequence = g_cc_entry.count_list_sequence,
1408           count_date_first = g_cc_entry.count_date_first,
1409           count_date_current = g_cc_entry.count_date_current,
1410           count_date_prior = g_cc_entry.count_date_prior,
1411           count_date_dummy = g_cc_entry.count_date_dummy,
1412           counted_by_employee_id_first =
1413                                        g_cc_entry.counted_by_employee_id_first,
1414           counted_by_employee_id_current =
1415                                      g_cc_entry.counted_by_employee_id_current,
1416           counted_by_employee_id_prior =
1417                                        g_cc_entry.counted_by_employee_id_prior,
1418           counted_by_employee_id_dummy =
1419                                        g_cc_entry.counted_by_employee_id_dummy,
1420           count_uom_first = g_cc_entry.count_uom_first,
1421           count_uom_current = g_cc_entry.count_uom_current,
1422           count_uom_prior = g_cc_entry.count_uom_prior,
1423           count_quantity_first = g_cc_entry.count_quantity_first,
1424           count_quantity_current = g_cc_entry.count_quantity_current,
1425           count_quantity_prior = g_cc_entry.count_quantity_prior,
1426           inventory_item_id = g_cc_entry.inventory_item_id,
1427           subinventory = g_cc_entry.subinventory,
1428           entry_status_code = g_cc_entry.entry_status_code,
1429           count_due_date = g_cc_entry.count_due_date,
1430           organization_id = g_cc_entry.organization_id,
1431           cycle_count_header_id = g_cc_entry.cycle_count_header_id,
1432           number_of_counts = g_cc_entry.number_of_counts,
1433           locator_id = g_cc_entry.locator_id,
1434           adjustment_quantity = g_cc_entry.adjustment_quantity,
1435           adjustment_date = g_cc_entry.adjustment_date,
1436           adjustment_amount = g_cc_entry.adjustment_amount,
1437           item_unit_cost = g_cc_entry.item_unit_cost,
1438           inventory_adjustment_account =
1439                                        g_cc_entry.inventory_adjustment_account,
1440           approval_date = g_cc_entry.approval_date,
1441           approver_employee_id = g_cc_entry.approver_employee_id,
1442           revision = g_cc_entry.revision,
1443           lot_number = g_cc_entry.lot_number,
1444           lot_control = g_cc_entry.lot_control,
1445           system_quantity_first = g_cc_entry.system_quantity_first,
1446           system_quantity_current = g_cc_entry.system_quantity_current,
1447           system_quantity_prior = g_cc_entry.system_quantity_prior,
1448           reference_first = g_cc_entry.reference_first,
1449           reference_current = g_cc_entry.reference_current,
1450           reference_prior = g_cc_entry.reference_prior,
1451           primary_uom_quantity_first = g_cc_entry.primary_uom_quantity_first,
1452           primary_uom_quantity_current =
1453                                        g_cc_entry.primary_uom_quantity_current,
1454           primary_uom_quantity_prior = g_cc_entry.primary_uom_quantity_prior,
1455           count_type_code = g_cc_entry.count_type_code,
1456           transaction_reason_id = g_cc_entry.transaction_reason_id,
1457           approval_type = g_cc_entry.approval_type,
1458           attribute_category = g_cc_entry.attribute_category,
1459           attribute1 = g_cc_entry.attribute1,
1460           attribute2 = g_cc_entry.attribute2,
1461           attribute3 = g_cc_entry.attribute3,
1462           attribute4 = g_cc_entry.attribute4,
1463           attribute5 = g_cc_entry.attribute5,
1464           attribute6 = g_cc_entry.attribute6,
1465           attribute7 = g_cc_entry.attribute7,
1466           attribute8 = g_cc_entry.attribute8,
1467           attribute9 = g_cc_entry.attribute9,
1468           attribute10 = g_cc_entry.attribute10,
1469           attribute11 = g_cc_entry.attribute11,
1470           attribute12 = g_cc_entry.attribute12,
1471           attribute13 = g_cc_entry.attribute13,
1472           attribute14 = g_cc_entry.attribute14,
1473           attribute15 = g_cc_entry.attribute15,
1474           serial_number = g_cc_entry.serial_number,
1475           serial_detail = g_cc_entry.serial_detail,
1476           approval_condition = g_cc_entry.approval_condition,
1477           neg_adjustment_quantity = g_cc_entry.neg_adjustment_quantity,
1478           neg_adjustment_amount = g_cc_entry.neg_adjustment_amount,
1479           parent_lpn_id = g_cc_entry.parent_lpn_id,
1480           outermost_lpn_id = g_cc_entry.outermost_lpn_id,
1481           cost_group_id = g_cc_entry.cost_group_id
1482            -- INVCONV, NSRIVAST
1483           ,secondary_uom_quantity_first    =   g_cc_entry.secondary_uom_quantity_first ,
1484           secondary_uom_quantity_current  =   g_cc_entry.secondary_uom_quantity_current,
1485           secondary_uom_quantity_prior    =   g_cc_entry.secondary_uom_quantity_prior ,
1486           count_secondary_uom_first       =   g_cc_entry.count_secondary_uom_first,
1487           count_secondary_uom_current     =   g_cc_entry.count_secondary_uom_current,
1488           count_secondary_uom_prior       =   g_cc_entry.count_secondary_uom_prior,
1489           -- INVCONV, NSRIVAST
1490 	  -- nsinghi Bug#6052831 START
1491 	  secondary_adjustment_quantity   =   g_cc_entry.secondary_adjustment_quantity,
1492 	  secondary_system_qty_current    =   g_cc_entry.secondary_system_qty_current,
1493 	  secondary_system_qty_first      =   g_cc_entry.secondary_system_qty_first,
1494 	  secondary_system_qty_prior      =   g_cc_entry.secondary_system_qty_prior
1495 	  -- nsinghi Bug#6052831 END
1496       WHERE  cycle_count_entry_id = g_cc_entry.cycle_count_entry_id;
1497 
1498       IF ( SQL%NOTFOUND ) THEN
1499          RAISE NO_DATA_FOUND;
1500       END IF;
1501 
1502       -- bug 10298222 start
1503        IF ( l_debug = 1 ) THEN
1504           print_debug ( 'Update the wms_picking_pkg.g_previous_task_status table.' );
1505        END IF;
1506        BEGIN
1507         SELECT transaction_temp_id into l_temp_cc_id
1508         FROM wms_dispatched_tasks wdt
1509         WHERE wdt.transaction_temp_id          = g_cc_entry.cycle_count_entry_id;
1510 
1511         wms_picking_pkg.g_previous_task_status(g_cc_entry.cycle_count_entry_id) := 2; --WDT exists, so mark as QUEUED
1512        EXCEPTION
1513         WHEN NO_DATA_FOUND THEN
1514         wms_picking_pkg.g_previous_task_status(g_cc_entry.cycle_count_entry_id) := 1; --no WDT, so mark as PENDING
1515        END;
1516       -- bug 10298222 end
1517    END update_row;
1518 
1519    PROCEDURE current_to_prior
1520    IS
1521       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
1522 	  -- added for bug 14843592 start
1523 	  l_system_quantity          NUMBER;
1524 	  l_sec_system_quantity      NUMBER;
1525       l_adjustment_quantity      NUMBER;
1526 	  l_sec_adjustment_quantity  NUMBER;
1527       l_adjustment_value         NUMBER;
1528 	  -- added for bug 14843592 end
1529    BEGIN
1530       IF ( l_debug = 1 ) THEN
1531          print_debug ( '***current_to_prior***' );
1532       END IF;
1533 
1534       -- Set all of the prior fields equal to the current fields
1535       g_cc_entry.count_date_prior := g_cc_entry.count_date_current;
1536       g_cc_entry.counted_by_employee_id_prior :=
1537                                      g_cc_entry.counted_by_employee_id_current;
1538       g_cc_entry.count_uom_prior := g_cc_entry.count_uom_current;
1539       g_cc_entry.count_quantity_prior := g_cc_entry.count_quantity_current;
1540       g_cc_entry.system_quantity_prior := g_cc_entry.system_quantity_current;
1541       g_cc_entry.secondary_system_qty_prior := g_cc_entry.secondary_system_qty_current; -- nsinghi Bug#6052831 Added this line.
1542       g_cc_entry.reference_prior := g_cc_entry.reference_current;
1543       g_cc_entry.primary_uom_quantity_prior :=
1544                                        g_cc_entry.primary_uom_quantity_current;
1545 
1546       -- added for bug 14843592 start
1547       system_quantity ( x_system_quantity => l_system_quantity
1548 					  , x_sec_system_quantity => l_sec_system_quantity );
1549 
1550       l_adjustment_quantity 					:= g_count_quantity - l_system_quantity;
1551 	  l_sec_adjustment_quantity 				:= g_count_secondary_quantity - l_sec_system_quantity;
1552       g_cc_entry.adjustment_quantity 			:= l_adjustment_quantity;
1553 	  g_cc_entry.secondary_adjustment_quantity 	:= l_sec_adjustment_quantity;
1554       g_cc_entry.adjustment_date 				:= SYSDATE;
1555 
1556       value_variance ( x_value_variance => l_adjustment_value );
1557       g_cc_entry.adjustment_amount 				:= l_adjustment_value;
1558 	  -- added for bug 14843592 end
1559 
1560 
1561       -- INVCONV, NSRIVAST
1562       g_cc_entry.count_secondary_uom_prior     :=   g_cc_entry.count_secondary_uom_current ;
1563       g_cc_entry.secondary_uom_quantity_prior  :=   g_cc_entry.secondary_uom_quantity_current ;
1564       -- INVCONV, NSRIVAST
1565 
1566    END current_to_prior;
1567 
1568    PROCEDURE current_to_first
1569    IS
1570       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
1571    BEGIN
1572       IF ( l_debug = 1 ) THEN
1573          print_debug ( '***current_to_first***' );
1574       END IF;
1575 
1576       -- Set all of the first fields equal to the current fields
1577       g_cc_entry.count_date_first := g_cc_entry.count_date_current;
1578       g_cc_entry.counted_by_employee_id_first :=
1579                                      g_cc_entry.counted_by_employee_id_current;
1580       g_cc_entry.count_uom_first := g_cc_entry.count_uom_current;
1581       g_cc_entry.count_quantity_first := g_cc_entry.count_quantity_current;
1582       g_cc_entry.system_quantity_first := g_cc_entry.system_quantity_current;
1583       g_cc_entry.secondary_system_qty_first := g_cc_entry.secondary_system_qty_current; -- nsinghi Bug#6052831 Added this line.
1584       g_cc_entry.reference_first := g_cc_entry.reference_current;
1585       g_cc_entry.primary_uom_quantity_first :=
1586                                        g_cc_entry.primary_uom_quantity_current;
1587       -- INVCONV, NSRIVAST
1588       g_cc_entry.count_secondary_uom_first     :=   g_cc_entry.count_secondary_uom_current ;
1589       g_cc_entry.secondary_uom_quantity_first  :=   g_cc_entry.secondary_uom_quantity_current ;
1590       -- INVCONV, NSRIVAST
1591    END current_to_first;
1592 
1593 -- nsinghi bug#6052831
1594    PROCEDURE entry_to_current (
1595       p_count_date IN DATE,
1596       p_counted_by_employee_id IN NUMBER,
1597       p_system_quantity IN NUMBER,
1598       p_reference IN VARCHAR2,
1599       p_primary_uom_quantity IN NUMBER,
1600       p_sec_system_quantity IN NUMBER DEFAULT NULL -- nsinghi Bug#6052831 Added this parameter.
1601    )
1602    IS
1603       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
1604    BEGIN
1605       IF ( l_debug = 1 ) THEN
1606          print_debug ( '***entry_to_current***' );
1607       END IF;
1608 
1609       -- Set all of the current fields equal to the given entries
1610       g_cc_entry.count_date_current := p_count_date;
1611       g_cc_entry.counted_by_employee_id_current := p_counted_by_employee_id;
1612       g_cc_entry.count_uom_current := g_count_uom;
1613       g_cc_entry.count_quantity_current := g_count_quantity;
1614       g_cc_entry.system_quantity_current := p_system_quantity;
1615       g_cc_entry.secondary_system_qty_current := p_sec_system_quantity; -- nsinghi Bug#6052831 Added this line.
1616       g_cc_entry.reference_current := p_reference;
1617       g_cc_entry.primary_uom_quantity_current := p_primary_uom_quantity;
1618       -- INVCONV, NSRIVAST
1619       g_cc_entry.count_secondary_uom_current     := g_count_secondary_uom ;
1620       g_cc_entry.secondary_uom_quantity_current  := g_count_secondary_quantity ;
1621       -- INVCONV, NSRIVAST
1622    END entry_to_current;
1623 
1624    PROCEDURE zero_count_logic
1625    IS
1626       l_primary_uom_quantity NUMBER;
1627       l_primary_uom VARCHAR2 ( 3 );
1628       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
1629    BEGIN
1630       IF ( l_debug = 1 ) THEN
1631          print_debug ( '***zero_count_logic***' );
1632       END IF;
1633 
1634       -- Set the default values for a zero count entry
1635       g_cc_entry.system_quantity_current := 0;
1636       g_cc_entry.number_of_counts := 1;
1637       g_cc_entry.entry_status_code := 5;
1638       g_cc_entry.approval_type := 1;
1639       g_cc_entry.approver_employee_id := g_employee_id;
1640       g_cc_entry.approval_date := SYSDATE;
1641 
1642       -- Get the item primary uom code
1643       SELECT primary_uom_code
1644       INTO   l_primary_uom
1645       FROM   MTL_SYSTEM_ITEMS
1646       WHERE  inventory_item_id = g_cc_entry.inventory_item_id
1647       AND    organization_id = g_cc_entry.organization_id;
1648 
1649       -- Convert the count quantity into the item primary uom quantity
1650       l_primary_uom_quantity :=
1651          inv_convert.inv_um_convert ( g_cc_entry.inventory_item_id,
1652                                       6,
1653                                       g_count_quantity,
1654                                       g_count_uom,
1655                                       l_primary_uom,
1656                                       NULL,
1657                                       NULL
1658                                     );
1659       -- Set the entry values to the current fields
1660       entry_to_current ( p_count_date        => SYSDATE,
1661                          p_counted_by_employee_id => g_employee_id,
1662                          p_system_quantity   => 0,
1663                          p_reference         => NULL,
1664                          p_primary_uom_quantity => l_primary_uom_quantity,
1665                          p_sec_system_quantity => 0 -- nsinghi Bug#6052831 Added this line.
1666                        );
1667       -- Set the current values to the first fields.
1668       current_to_first ( );
1669    END zero_count_logic;
1670 
1671 -- Since approval tolerances can be defined at the cycle count header,
1672 -- item, and class level, we need to choose the appropriate one.
1673 -- Similarly, cost tolerances can be defined at the cycle count header
1674 -- and class level.
1675    PROCEDURE get_tolerances (
1676       pre_approve_flag IN VARCHAR2,
1677       x_approval_tolerance_positive OUT NOCOPY NUMBER,
1678       x_approval_tolerance_negative OUT NOCOPY NUMBER,
1679       x_cost_tolerance_positive OUT NOCOPY NUMBER,
1680       x_cost_tolerance_negative OUT NOCOPY NUMBER
1681    )
1682    IS
1683       l_item_app_tol_pos NUMBER;
1684       l_item_app_tol_neg NUMBER;
1685       l_class_app_tol_pos NUMBER;
1686       l_class_app_tol_neg NUMBER;
1687       l_head_app_tol_pos NUMBER;
1688       l_head_app_tol_neg NUMBER;
1689       l_class_cost_tol_pos NUMBER;
1690       l_class_cost_tol_neg NUMBER;
1691       l_head_cost_tol_pos NUMBER;
1692       l_head_cost_tol_neg NUMBER;
1693       l_inventory_item_id NUMBER;
1694       l_organization_id NUMBER;
1695       l_cycle_count_header_id NUMBER;
1696       l_abc_class_id NUMBER;
1697       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
1698    BEGIN
1699       IF ( l_debug = 1 ) THEN
1700          print_debug ( '***get_tolerances***' );
1701       END IF;
1702 
1703       -- Get the required information from the cycle count entry record
1704       -- which will serve as the primary keys to get the tolerances from
1705       -- the various cycle count tables
1706       l_inventory_item_id := g_cc_entry.inventory_item_id;
1707       l_organization_id := g_cc_entry.organization_id;
1708       l_cycle_count_header_id := g_cc_entry.cycle_count_header_id;
1709 
1710       BEGIN
1711          SELECT abc_class_id
1712          INTO   l_abc_class_id
1713          FROM   mtl_cycle_count_items
1714          WHERE  cycle_count_header_id = l_cycle_count_header_id
1715          AND    inventory_item_id = l_inventory_item_id;
1716       EXCEPTION
1717          WHEN NO_DATA_FOUND THEN
1718             l_abc_class_id := NULL;
1719       END;
1720 
1721       -- Get all of the values of the tolerances
1722       IF ( l_abc_class_id IS NOT NULL ) THEN
1723          SELECT approval_tolerance_positive,
1724                 approval_tolerance_negative
1725          INTO   l_item_app_tol_pos,
1726                 l_item_app_tol_neg
1727          FROM   mtl_cycle_count_items
1728          WHERE  cycle_count_header_id = l_cycle_count_header_id
1729          AND    inventory_item_id = l_inventory_item_id;
1730       ELSE
1731          l_item_app_tol_pos := NULL;
1732          l_item_app_tol_neg := NULL;
1733       END IF;
1734 
1735       IF ( l_abc_class_id IS NOT NULL ) THEN
1736          SELECT approval_tolerance_positive,
1737                 approval_tolerance_negative,
1738                 cost_tolerance_positive,
1739                 cost_tolerance_negative
1740          INTO   l_class_app_tol_pos,
1741                 l_class_app_tol_neg,
1742                 l_class_cost_tol_pos,
1743                 l_class_cost_tol_neg
1744          FROM   mtl_cycle_count_classes
1745          WHERE  abc_class_id = l_abc_class_id
1746          AND    cycle_count_header_id = l_cycle_count_header_id;
1747       ELSE
1748          l_class_app_tol_pos := NULL;
1749          l_class_app_tol_neg := NULL;
1750          l_class_cost_tol_pos := NULL;
1751          l_class_cost_tol_neg := NULL;
1752       END IF;
1753 
1754       SELECT NVL ( approval_tolerance_positive, -1 ),
1755              NVL ( approval_tolerance_negative, -1 ),
1756              NVL ( cost_tolerance_positive, -1 ),
1757              NVL ( cost_tolerance_negative, -1 )
1758       INTO   l_head_app_tol_pos,
1759              l_head_app_tol_neg,
1760              l_head_cost_tol_pos,
1761              l_head_cost_tol_neg
1762       FROM   mtl_cycle_count_headers
1763       WHERE  cycle_count_header_id = l_cycle_count_header_id
1764       AND    organization_id = l_organization_id;
1765 
1766       /* Approval Tolerance Positive */
1767       IF l_item_app_tol_pos IS NULL THEN
1768          IF l_class_app_tol_pos IS NULL THEN
1769             x_approval_tolerance_positive := l_head_app_tol_pos;
1770          ELSE
1771             x_approval_tolerance_positive := l_class_app_tol_pos;
1772          END IF;
1773       ELSE
1774          x_approval_tolerance_positive := l_item_app_tol_pos;
1775       END IF;
1776 
1777       /* Approval Tolerance Negative */
1778       IF l_item_app_tol_neg IS NULL THEN
1779          IF l_class_app_tol_neg IS NULL THEN
1780             x_approval_tolerance_negative := l_head_app_tol_neg;
1781          ELSE
1782             x_approval_tolerance_negative := l_class_app_tol_neg;
1783          END IF;
1784       ELSE
1785          x_approval_tolerance_negative := l_item_app_tol_neg;
1786       END IF;
1787 
1788       /* Cost Tolerance Positive */
1789       IF l_class_cost_tol_pos IS NULL THEN
1790          x_cost_tolerance_positive := l_head_cost_tol_pos;
1791       ELSE
1792          x_cost_tolerance_positive := l_class_cost_tol_pos;
1793       END IF;
1794 
1795       /* Cost Tolerance Negative */
1796       IF l_class_cost_tol_neg IS NULL THEN
1797          x_cost_tolerance_negative := l_head_cost_tol_neg;
1798       ELSE
1799          x_cost_tolerance_negative := l_class_cost_tol_neg;
1800       END IF;
1801 
1802       /* Check the status of the pre approve flag */
1803       IF ( l_debug = 1 ) THEN
1804          print_debug ( 'Preapprove flag is: ============>' || pre_approve_flag
1805                      );
1806          print_debug ( 'Tolerances retrieved are:' );
1807          print_debug (    'x_approval_tolerance_positive: => '
1808                        || x_approval_tolerance_positive
1809                      );
1810          print_debug (    'x_approval_tolerance_negative: => '
1811                        || x_approval_tolerance_negative
1812                      );
1813          print_debug (    'x_cost_tolerance_positive: =====> '
1814                        || x_cost_tolerance_positive
1815                      );
1816          print_debug (    'x_cost_tolerance_negative: =====> '
1817                        || x_cost_tolerance_negative
1818                      );
1819       END IF;
1820 
1821       IF pre_approve_flag <> 'SERIAL' THEN
1822          IF pre_approve_flag <> 'TRUE' THEN
1823             recount_logic ( p_approval_tolerance_positive => x_approval_tolerance_positive,
1824                             p_approval_tolerance_negative => x_approval_tolerance_negative,
1825                             p_cost_tolerance_positive => x_cost_tolerance_positive,
1826                             p_cost_tolerance_negative => x_cost_tolerance_negative
1827                           );
1828          ELSE
1829             tolerance_logic ( p_approval_tolerance_positive => x_approval_tolerance_positive,
1830                               p_approval_tolerance_negative => x_approval_tolerance_negative,
1831                               p_cost_tolerance_positive => x_cost_tolerance_positive,
1832                               p_cost_tolerance_negative => x_cost_tolerance_negative
1833                             );
1834          END IF;
1835       END IF;
1836    END get_tolerances;
1837 
1838    PROCEDURE recount_logic (
1839       p_approval_tolerance_positive IN NUMBER,
1840       p_approval_tolerance_negative IN NUMBER,
1841       p_cost_tolerance_positive IN NUMBER,
1842       p_cost_tolerance_negative IN NUMBER
1843    )
1844    IS
1845       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
1846    BEGIN
1847       IF ( l_debug = 1 ) THEN
1848          print_debug ( '***recount_logic***' );
1849       END IF;
1850 
1851       -- IF count entry has a status of RECOUNT, THEN call
1852       -- current_to_prior to set up the next count
1853       IF ( g_cc_entry.entry_status_code = 3 ) AND g_updated_prior = FALSE THEN		-- Modified for Bug 6371673
1854          current_to_prior ( );
1855       END IF;
1856 
1857       tolerance_logic ( p_approval_tolerance_positive => p_approval_tolerance_positive,
1858                         p_approval_tolerance_negative => p_approval_tolerance_negative,
1859                         p_cost_tolerance_positive => p_cost_tolerance_positive,
1860                         p_cost_tolerance_negative => p_cost_tolerance_negative
1861                       );
1862    END recount_logic;
1863 
1864    PROCEDURE tolerance_logic (
1865       p_approval_tolerance_positive IN NUMBER,
1866       p_approval_tolerance_negative IN NUMBER,
1867       p_cost_tolerance_positive IN NUMBER,
1868       p_cost_tolerance_negative IN NUMBER
1869    )
1870    IS
1871       l_adjustment_quantity NUMBER;
1872       l_sec_adjustment_quantity NUMBER; -- nsinghi Bug#6052831
1873       l_system_quantity NUMBER;
1874       l_sec_system_quantity NUMBER; -- nsinghi Bug#6052831
1875       l_pos_meas_err NUMBER;
1876       l_neg_meas_err NUMBER;
1877       l_app_tol_pos NUMBER := p_approval_tolerance_positive;
1878       l_app_tol_neg NUMBER := p_approval_tolerance_negative;
1879       l_cost_tol_pos NUMBER := p_cost_tolerance_positive;
1880       l_cost_tol_neg NUMBER := p_cost_tolerance_negative;
1881       l_adjustment_value NUMBER;
1882       l_approval_option_code NUMBER;
1883       l_parent_lpn_id NUMBER;
1884       l_container_enabled_flag NUMBER;
1885       l_container_adjustment_option NUMBER;
1886       l_container_discrepancy_option NUMBER;
1887       l_primary_uom VARCHAR2 ( 3 );
1888       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
1889    BEGIN
1890       IF ( l_debug = 1 ) THEN
1891          print_debug ( '***tolerance_logic***' );
1892       END IF;
1893 
1894       -- Get the item system quantity
1895       system_quantity ( x_system_quantity => l_system_quantity
1896                         , x_sec_system_quantity => l_sec_system_quantity ); -- nsinghi Bug#6052831. Call overloaded API.
1897 
1898       -- Get the item primary uom code
1899       SELECT primary_uom_code
1900       INTO   l_primary_uom
1901       FROM   MTL_SYSTEM_ITEMS
1902       WHERE  inventory_item_id = g_cc_entry.inventory_item_id
1903       AND    organization_id = g_cc_entry.organization_id;
1904 
1905       -- Convert the system quantity into the count uom
1906       /*2977228l_system_quantity :=
1907          inv_convert.inv_um_convert ( g_cc_entry.inventory_item_id,
1908                                       6,
1909                                       l_system_quantity,
1910                                       l_primary_uom,
1911                                       g_count_uom,
1912                                       NULL,
1913                                       NULL
1914                                     );*/
1915       -- Get the adjustment quantity and adjustment value
1916       l_adjustment_quantity := g_count_quantity - l_system_quantity;
1917       l_sec_adjustment_quantity := g_count_secondary_quantity - l_sec_system_quantity; -- nsinghi Bug#6052831. Added this line.
1918       g_cc_entry.adjustment_quantity := l_adjustment_quantity;
1919       g_cc_entry.secondary_adjustment_quantity := l_sec_adjustment_quantity; -- nsinghi Bug#6052831. Added this line.
1920       g_cc_entry.adjustment_date := SYSDATE;
1921 
1922       value_variance ( x_value_variance => l_adjustment_value );
1923       g_cc_entry.adjustment_amount := l_adjustment_value;
1924 
1925 
1926      /* Bug 4495880 - Checking the value of the global parameter to set the adjustment quantity
1927                       and adjustment value to 0*/
1928 
1929       IF g_condition=TRUE OR g_lpn_summary_count=TRUE  THEN --9452528,for summary count, we need adj qty to zero.
1930 
1931         IF ( l_debug = 1 ) THEN
1932             print_debug ( 'In tolerance_logic in the condition for g_condition=TRUE' );
1933         END IF;
1934        l_adjustment_quantity := 0;
1935        l_adjustment_value := 0;
1936        g_cc_entry.adjustment_quantity := l_adjustment_quantity;
1937        g_cc_entry.adjustment_amount := l_adjustment_value;
1938        -- nsinghi Bug#6052831. START
1939        l_sec_adjustment_quantity := 0;
1940        g_cc_entry.secondary_adjustment_quantity := 0;
1941        -- nsinghi Bug#6052831. END.
1942 
1943       END IF;
1944 
1945      /* End of fix for Bug 4495880 */
1946 
1947 
1948       -- Get the required information from the cycle count record.
1949       -- Need to use the view rather than the table to get the measurement
1950       -- error values
1951       BEGIN
1952          SELECT positive_measurement_error,
1953                 negative_measurement_error,
1954                 parent_lpn_id
1955          INTO   l_pos_meas_err,
1956                 l_neg_meas_err,
1957                 l_parent_lpn_id
1958          FROM   mtl_cycle_count_entries_v
1959          WHERE  cycle_count_entry_id = g_cc_entry.cycle_count_entry_id
1960          AND    organization_id = g_cc_entry.organization_id;
1961       EXCEPTION
1962          WHEN NO_DATA_FOUND THEN
1963             l_pos_meas_err := 0;
1964             l_neg_meas_err := 0;
1965             l_parent_lpn_id := g_cc_entry.parent_lpn_id;
1966       END;
1967 
1968       -- Get the container specific information
1969       SELECT NVL ( container_enabled_flag, 2 ),
1970              NVL ( container_adjustment_option, 2 ),
1971              NVL ( container_discrepancy_option, 2 )
1972       INTO   l_container_enabled_flag,
1973              l_container_adjustment_option,
1974              l_container_discrepancy_option
1975       FROM   mtl_cycle_count_headers
1976       WHERE  cycle_count_header_id = g_cc_entry.cycle_count_header_id
1977       AND    organization_id = g_cc_entry.organization_id;
1978 
1979       -- Get the cycle count header approval option code
1980       SELECT NVL ( approval_option_code, 1 )
1981       INTO   l_approval_option_code
1982       FROM   mtl_cycle_count_headers
1983       WHERE  cycle_count_header_id = g_cc_entry.cycle_count_header_id
1984       AND    organization_id = g_cc_entry.organization_id;
1985 
1986       IF ( l_debug = 1 ) THEN
1987          print_debug ( 'Adjustment quantity: ===> ' || l_adjustment_quantity );
1988          print_debug ( 'System quantity : ======> ' || l_system_quantity );
1989          print_debug ( 'Pos Measurement error: => ' || l_pos_meas_err );
1990          print_debug ( 'Neg Measurement error: => ' || l_neg_meas_err );
1991          print_debug ( 'App Tolerance pos: =====> ' || l_app_tol_pos );
1992          print_debug ( 'App Tolernace neg: =====> ' || l_app_tol_neg );
1993          print_debug ( 'Cost Tolerance pos: ====> ' || l_cost_tol_pos );
1994          print_debug ( 'Cost Tolerance neg: ====> ' || l_cost_tol_neg );
1995          print_debug ( 'Adjustment value:  =====> ' || l_adjustment_value );
1996          print_debug ( 'Approval option code: ==> ' || l_approval_option_code );
1997       END IF;
1998 
1999       -- Approval required for all adjustments
2000       IF    ( l_approval_option_code = 1 AND l_parent_lpn_id IS NULL )
2001          OR ( l_approval_option_code = 1 AND l_parent_lpn_id IS NOT NULL
2002               AND l_container_enabled_flag = 1
2003               AND (    l_container_adjustment_option = 2
2004                     OR l_container_discrepancy_option = 2
2005                   )
2006             ) THEN
2007          IF l_adjustment_quantity <> 0 THEN
2008             IF l_system_quantity <> 0 THEN
2009                IF l_adjustment_quantity < 0 THEN
2010                   IF     l_neg_meas_err IS NOT NULL
2011                      AND   ABS ( l_adjustment_quantity / l_system_quantity )
2012                          * 100 < l_neg_meas_err THEN
2013                      no_adj_req ( );
2014                   ELSE
2015                      out_tolerance ( );
2016                   END IF;
2017                ELSE
2018                   IF     l_pos_meas_err IS NOT NULL
2019                      AND   ABS ( l_adjustment_quantity / l_system_quantity )
2020                          * 100 < l_pos_meas_err THEN
2021                      no_adj_req ( );
2022                   ELSE
2023                      out_tolerance ( );
2024                   END IF;
2025                END IF;
2026             ELSE                                           /* system qty = 0 */
2027                out_tolerance ( );
2028             END IF;
2029          ELSE                                          /* adjustment_qty = 0 */
2030             no_adj_req ( );
2031          END IF;
2032       ELSE              /* IF optional_option = required IF out of tolerance */
2033          IF l_adjustment_quantity <> 0 THEN
2034             IF l_system_quantity <> 0 THEN
2035                IF l_adjustment_quantity < 0 THEN
2036                   IF     l_neg_meas_err IS NOT NULL
2037                      AND   ABS ( l_adjustment_quantity / l_system_quantity )
2038                          * 100 < l_neg_meas_err THEN
2039                      no_adj_req ( );
2040                   ELSE
2041                      IF (      (     l_app_tol_neg IS NOT NULL
2042                                  AND l_app_tol_neg >= 0
2043                                )
2044                           AND ( ABS (    (   l_adjustment_quantity
2045                                            / l_system_quantity
2046                                          )
2047                                       * 100
2048                                     ) > l_app_tol_neg
2049                               )
2050                         ) THEN
2051                         out_tolerance ( );
2052                      ELSE
2053                         IF (      (     l_cost_tol_neg IS NOT NULL
2054                                     AND l_cost_tol_neg >= 0
2055                                   )
2056                              AND ( ABS ( l_adjustment_value ) > l_cost_tol_neg
2057                                  )
2058                            ) THEN
2059                            out_tolerance ( );
2060                         ELSE
2061                            in_tolerance ( );
2062                         END IF;
2063                      END IF;
2064                   END IF;
2065                ELSE                            /* l_adjustment_quantity >= 0 */
2066                   IF     l_pos_meas_err IS NOT NULL
2067                      AND   ABS ( l_adjustment_quantity / l_system_quantity )
2068                          * 100 < l_pos_meas_err THEN
2069                      no_adj_req ( );
2070                   ELSE
2071                      IF (      (     l_app_tol_pos IS NOT NULL
2072                                  AND l_app_tol_pos >= 0
2073                                )
2074                           AND ( ABS (    (   l_adjustment_quantity
2075                                            / l_system_quantity
2076                                          )
2077                                       * 100
2078                                     ) > l_app_tol_pos
2079                               )
2080                         ) THEN
2081                         out_tolerance ( );
2082                      ELSE
2083                         IF (      (     l_cost_tol_pos IS NOT NULL
2084                                     AND l_cost_tol_pos >= 0
2085                                   )
2086                              AND ( ABS ( l_adjustment_value ) > l_cost_tol_pos
2087                                  )
2088                            ) THEN
2089                            out_tolerance ( );
2090                         ELSE
2091                            in_tolerance ( );
2092                         END IF;
2093                      END IF;
2094                   END IF;
2095                END IF;
2096             ELSE                                      /* system quantity = 0 */
2097                IF ( l_app_tol_pos IS NOT NULL AND l_app_tol_pos >= 0 ) THEN
2098                   out_tolerance ( );
2099                ELSE
2100                   IF (      (     l_cost_tol_pos IS NOT NULL
2101                               AND l_cost_tol_pos >= 0 )
2102                        AND ( l_adjustment_value > l_cost_tol_pos )
2103                      ) THEN
2104                      out_tolerance ( );
2105                   ELSE
2106                      in_tolerance ( );
2107                   END IF;
2108                END IF;
2109             END IF;
2110          ELSE                                          /* adjustment qty = 0 */
2111             no_adj_req ( );
2112          END IF;
2113       END IF;
2114    END tolerance_logic;
2115 
2116    PROCEDURE valids
2117    IS
2118       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
2119    BEGIN
2120       IF ( l_debug = 1 ) THEN
2121          print_debug ( '***valids***' );
2122       END IF;
2123        print_debug('calling from valids ');
2124       final_preupdate_logic ( );
2125    END valids;
2126 
2127    PROCEDURE in_tolerance
2128    IS
2129       l_approval_option_code NUMBER;
2130       l_parent_lpn_id NUMBER;
2131       l_container_enabled_flag NUMBER;
2132       l_container_adjustment_option NUMBER;
2133       l_container_discrepancy_option NUMBER;
2134       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
2135    BEGIN
2136       IF ( l_debug = 1 ) THEN
2137          print_debug ( '***in_tolerance***' );
2138       END IF;
2139 
2140       -- Get the required fields
2141       SELECT NVL ( approval_option_code, 1 )
2142       INTO   l_approval_option_code
2143       FROM   mtl_cycle_count_headers
2144       WHERE  cycle_count_header_id = g_cc_entry.cycle_count_header_id
2145       AND    organization_id = g_cc_entry.organization_id;
2146 
2147       l_parent_lpn_id := g_cc_entry.parent_lpn_id;
2148 
2149       -- Get the container specific information
2150       SELECT NVL ( container_enabled_flag, 2 ),
2151              NVL ( container_adjustment_option, 2 ),
2152              NVL ( container_discrepancy_option, 2 )
2153       INTO   l_container_enabled_flag,
2154              l_container_adjustment_option,
2155              l_container_discrepancy_option
2156       FROM   mtl_cycle_count_headers
2157       WHERE  cycle_count_header_id = g_cc_entry.cycle_count_header_id
2158       AND    organization_id = g_cc_entry.organization_id;
2159 
2160       -- Approval is required for all adjustments
2161       IF    ( l_approval_option_code = 1 AND l_parent_lpn_id IS NULL )
2162          OR ( l_approval_option_code = 1 --Bug 5917964 -Added the check for approval code for lpn counts also
2163               AND l_parent_lpn_id IS NOT NULL
2164               AND l_container_enabled_flag = 1
2165               AND l_container_adjustment_option = 2
2166               AND l_container_discrepancy_option = 2
2167             ) THEN
2168          g_cc_entry.entry_status_code := 2;
2169       ELSE
2170          -- Approval is not required so complete the count entry
2171          g_cc_entry.entry_status_code := 5;
2172          g_cc_entry.approval_type := 1;
2173       END IF;
2174 
2175       valids ( );
2176    END in_tolerance;
2177 
2178    PROCEDURE out_tolerance
2179    IS
2180       l_approval_option_code NUMBER;
2181       l_auto_recount_flag NUMBER;
2182       l_max_recounts NUMBER;
2183       l_garbage NUMBER;
2184       l_parent_lpn_id NUMBER := g_cc_entry.parent_lpn_id;
2185       l_container_enabled_flag NUMBER;
2186       l_container_adjustment_option NUMBER;
2187       l_container_discrepancy_option NUMBER;
2188       l_days_until_late NUMBER;
2189       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
2190 
2191       l_serial_count_option NUMBER; --Bug 5186993
2192       l_serial_number_ctrl_code NUMBER; --Bug 5186993
2193 
2194    BEGIN
2195       IF ( l_debug = 1 ) THEN
2196          print_debug ( '***out_tolerance***' );
2197       END IF;
2198 
2199       -- Get the required values
2200       SELECT NVL ( approval_option_code, 1 ),
2201              NVL ( automatic_recount_flag, 2 ),
2202              NVL ( maximum_auto_recounts, 0 ),
2203              NVL ( days_until_late, 0 ),
2204              NVL ( serial_count_option, 1 )
2205       INTO   l_approval_option_code,
2206              l_auto_recount_flag,
2207              l_max_recounts,
2208              l_days_until_late,
2209              l_serial_count_option
2210       FROM   mtl_cycle_count_headers
2211       WHERE  cycle_count_header_id = g_cc_entry.cycle_count_header_id
2212       AND    organization_id = g_cc_entry.organization_id;
2213 
2214       -- Get the container specific information
2215       SELECT NVL ( container_enabled_flag, 2 ),
2216              NVL ( container_adjustment_option, 2 ),
2217              NVL ( container_discrepancy_option, 2 )
2218       INTO   l_container_enabled_flag,
2219              l_container_adjustment_option,
2220              l_container_discrepancy_option
2221       FROM   mtl_cycle_count_headers
2222       WHERE  cycle_count_header_id = g_cc_entry.cycle_count_header_id
2223       AND    organization_id = g_cc_entry.organization_id;
2224 
2225         -- Bug 5186993
2226          SELECT serial_number_control_code
2227          INTO   l_serial_number_ctrl_code
2228          FROM   mtl_system_items
2229          WHERE  inventory_item_id = g_cc_entry.inventory_item_id
2230          AND    organization_id = g_cc_entry.organization_id;
2231 
2232 
2233       -- Do more checks/validations IF the item is serial controlled
2234       is_serial_entered ( event => 'OUT-TOLERANCE', entered => l_garbage );
2235 
2236       -- No automatic recounts for this cycle count
2237       IF l_auto_recount_flag <> 1 THEN
2238          -- Approvals are not required for adjustments
2239          IF    ( l_approval_option_code = 2 AND l_parent_lpn_id IS NULL )
2240             OR ( l_approval_option_code = 2 --Bug 5917964 -Added the check for approval code for lpn counts also
2241                  AND l_parent_lpn_id IS NOT NULL
2242                  AND l_container_enabled_flag = 1
2243                  AND l_container_adjustment_option = 1
2244                  AND l_container_discrepancy_option = 1
2245                ) THEN
2246             -- Complete the count AND automatically approve it
2247             g_cc_entry.entry_status_code := 5;
2248             g_cc_entry.approval_type := 1;
2249          ELSE
2250             -- Approval is required for this adjustment
2251             g_cc_entry.entry_status_code := 2;
2252          END IF;
2253       ELSE
2254          -- Automatic recounts are allowed for this cycle count
2255          -- Bug# 2356835, change the < to <= since in mobile, we are updating
2256          -- the number of counts field in the cycle count entry beforehand
2257          -- in the pre_update method.  In the desktop library, this is done
2258          -- at the final_preupdate_logic stage which occurs after the
2259          -- tolerance/recount logic is done.
2260 
2261          -- Bug 5186993, do not set for recount for serialized items for multiple
2262          -- per request option.
2263          if (l_serial_number_ctrl_code in (1,6) OR l_serial_count_option <> 3) then
2264          IF NVL ( g_cc_entry.number_of_counts, 0 ) <= l_max_recounts THEN
2265             g_cc_entry.entry_status_code := 3;
2266             g_cc_entry.count_due_date := SYSDATE + l_days_until_late;
2267          ELSE
2268             -- Maximum number of recounts has already been met
2269             g_cc_entry.entry_status_code := 2;
2270          END IF;
2271          end if;
2272       END IF;
2273 
2274       valids ( );
2275    END out_tolerance;
2276 
2277    PROCEDURE no_adj_req
2278    IS
2279       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
2280    BEGIN
2281       IF ( l_debug = 1 ) THEN
2282          print_debug ( '***no_adj_req***' );
2283       END IF;
2284 
2285       g_cc_entry.entry_status_code := 5;
2286       g_cc_entry.adjustment_quantity := 0;
2287       g_cc_entry.secondary_adjustment_quantity := 0; -- nsinghi Bug#6052831
2288       g_cc_entry.adjustment_date := NULL;   --Bug#3640622
2289       valids ( );
2290    END no_adj_req;
2291 
2292    PROCEDURE pre_insert
2293    IS
2294       l_number_of_counts NUMBER := NVL ( g_cc_entry.number_of_counts, 0 );
2295       l_count_quantity NUMBER := g_count_quantity;
2296       l_count_type_code NUMBER := g_cc_entry.count_type_code;
2297       l_pre_approve_flag VARCHAR2 ( 20 ) := g_pre_approve_flag;
2298       l_cc_entry_id NUMBER;
2299       l_serial_number_ctrl_code NUMBER;
2300       l_serial_detail NUMBER;
2301       l_serial_entered NUMBER;
2302       l_serial_detail_option NUMBER;
2303       l_serial_count_option NUMBER;
2304       l_entry_status_code NUMBER := g_cc_entry.entry_status_code;
2305       l_total_serial_num_cnt NUMBER;
2306       l_cc_header_id NUMBER := g_cc_entry.cycle_count_header_id;
2307       l_success BOOLEAN;
2308       l_locator_id NUMBER := g_cc_entry.locator_id;
2309       l_approval_tolerance_positive NUMBER;
2310       l_approval_tolerance_negative NUMBER;
2311       l_cost_tolerance_positive NUMBER;
2312       l_cost_tolerance_negative NUMBER;
2313       l_system_quantity NUMBER;
2314       l_sec_system_quantity NUMBER; -- nsinghi Bug#6052831.
2315       l_primary_uom_quantity NUMBER;
2316       l_primary_uom VARCHAR2 ( 3 );
2317       l_adjustment_quantity NUMBER := 0;
2318       l_sec_adjustment_quantity NUMBER := 0; -- nsinghi Bug#6052831
2319       l_adjustment_value NUMBER := 0;
2320       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
2321    BEGIN
2322       IF ( l_debug = 1 ) THEN
2323          print_debug ( '***pre_insert***' );
2324       END IF;
2325       duplicate_entries() ;
2326 
2327       -- Get the required variable values
2328       SELECT serial_number_control_code
2329       INTO   l_serial_number_ctrl_code
2330       FROM   mtl_system_items
2331       WHERE  inventory_item_id = g_cc_entry.inventory_item_id
2332       AND    organization_id = g_cc_entry.organization_id;
2333 
2334       SELECT NVL ( serial_detail_option, 1 ),
2335              NVL ( serial_count_option, 1 )
2336       INTO   l_serial_detail_option,
2337              l_serial_count_option
2338       FROM   mtl_cycle_count_headers
2339       WHERE  cycle_count_header_id = g_cc_entry.cycle_count_header_id
2340       AND    organization_id = g_cc_entry.organization_id;
2341 
2342       -- Set the serial detail option for the new entry
2343       --l_serial_detail := l_serial_detail_option;
2344       --g_cc_entry.serial_detail := l_serial_detail;
2345 
2346       /* Bug 16029691 MSCA unscheduled cycle count entry with serial count
2347          option '1 per request' stuck in recount status, hard code serial detail
2348          option to 2 (Quantity Only) when serial count option is 2 (1 per request)
2349          this is in line with the form code */
2350       IF (l_serial_count_option = 2) THEN
2351          l_serial_detail := 2;
2352       ELSE
2353          l_serial_detail := l_serial_detail_option;
2354       END IF;
2355       g_cc_entry.serial_detail := l_serial_detail;
2356 
2357       IF ( g_cc_entry.cycle_count_entry_id IS NULL ) THEN
2358          SELECT mtl_cycle_count_entries_s.NEXTVAL
2359          INTO   l_cc_entry_id
2360          FROM   DUAL;
2361 
2362          g_cc_entry.cycle_count_entry_id := l_cc_entry_id;
2363          -- Bug# 2386128
2364          -- For unscheduled cycle count entries, the count list sequence
2365          -- number is the same as the cycle count entry ID
2366          g_cc_entry.count_list_sequence := l_cc_entry_id;
2367       END IF;
2368 
2369       IF ( l_count_type_code = 4 ) THEN
2370          -- Zero Count
2371          zero_count_logic;
2372       ELSE
2373          IF ( l_serial_number_ctrl_code IN ( 1, 6 ) ) THEN
2374             -- Not serial controlled
2375             l_number_of_counts := ( NVL ( l_number_of_counts, 0 ) + 1 );
2376             g_cc_entry.number_of_counts := l_number_of_counts;
2377             -- Get the system quantity
2378             system_quantity ( x_system_quantity => l_system_quantity
2379                               , x_sec_system_quantity => l_sec_system_quantity ); -- nsinghi Bug#6052831. Call overloaded API.
2380 
2381             -- Get the item primary uom code
2382             SELECT primary_uom_code
2383             INTO   l_primary_uom
2384             FROM   MTL_SYSTEM_ITEMS
2385             WHERE  inventory_item_id = g_cc_entry.inventory_item_id
2386             AND    organization_id = g_cc_entry.organization_id;
2387 
2388             -- Convert the system quantity into the count uom
2389             /*2977228l_system_quantity :=
2390                inv_convert.inv_um_convert ( g_cc_entry.inventory_item_id,
2391                                             6,
2392                                             l_system_quantity,
2393                                             l_primary_uom,
2394                                             g_count_uom,
2395                                             NULL,
2396                                             NULL
2397                                           );*/
2398             -- Convert the count quantity into the item primary uom quantity
2399             l_primary_uom_quantity :=
2400                inv_convert.inv_um_convert ( g_cc_entry.inventory_item_id,
2401                                             6,
2402                                             g_count_quantity,
2403                                             g_count_uom,
2404                                             l_primary_uom,
2405                                             NULL,
2406                                             NULL
2407                                           );
2408 
2409             IF ( l_number_of_counts = 1 ) THEN
2410                entry_to_current ( p_count_date        => SYSDATE,
2411                                   p_counted_by_employee_id => g_employee_id,
2412                                   p_system_quantity   => l_system_quantity,
2413                                   p_reference         => NULL,
2414                                   p_primary_uom_quantity => l_primary_uom_quantity,
2415 				  p_sec_system_quantity => l_sec_system_quantity -- nsinghi bug#6052831 Pass sec qty.
2416                                 );
2417                current_to_first ( );
2418             ELSE
2419                current_to_prior ( );
2420                entry_to_current ( p_count_date        => SYSDATE,
2421                                   p_counted_by_employee_id => g_employee_id,
2422                                   p_system_quantity   => l_system_quantity,
2423                                   p_reference         => NULL,
2424                                   p_primary_uom_quantity => l_primary_uom_quantity,
2425 				  p_sec_system_quantity => l_sec_system_quantity -- nsinghi bug#6052831 Pass sec qty.
2426                                 );
2427             END IF;
2428 
2429             IF ( l_pre_approve_flag = 'TRUE' ) THEN
2430                g_cc_entry.entry_status_code := 5;
2431                g_cc_entry.approval_type := 3;
2432                g_cc_entry.approver_employee_id := g_employee_id;
2433                print_debug('Called from pre_insert ');
2434                final_preupdate_logic ( );
2435             ELSE
2436                get_tolerances ( pre_approve_flag    => l_pre_approve_flag,
2437                                 x_approval_tolerance_positive => l_approval_tolerance_positive,
2438                                 x_approval_tolerance_negative => l_approval_tolerance_negative,
2439                                 x_cost_tolerance_positive => l_cost_tolerance_positive,
2440                                 x_cost_tolerance_negative => l_cost_tolerance_negative
2441                               );
2442             END IF;
2443          ELSIF ( l_serial_number_ctrl_code IN ( 2, 5 ) ) THEN
2444             -- Item is serial controlled
2445 
2446             IF ( l_serial_count_option = 3 ) THEN
2447                -- Multiple serial count option
2448 
2449                -- If serial details are entered, the adjustment txn should
2450                -- have already handled in the serial detail level or
2451                -- if serial detail does not entered, then get the tolerance
2452                is_serial_entered ( 'WHEN-VALIDATE-RECORD', l_serial_entered );
2453 
2454                IF ( l_serial_entered = 0 ) THEN
2455                   get_tolerances ( pre_approve_flag    => l_pre_approve_flag,
2456                                    x_approval_tolerance_positive => l_approval_tolerance_positive,
2457                                    x_approval_tolerance_negative => l_approval_tolerance_negative,
2458                                    x_cost_tolerance_positive => l_cost_tolerance_positive,
2459                                    x_cost_tolerance_negative => l_cost_tolerance_negative
2460                                  );
2461                ELSIF ( l_serial_entered = 1 ) THEN
2462                   IF ( l_entry_status_code = 5 ) THEN
2463                      -- Completed count entries
2464                      g_cc_entry.approval_date := SYSDATE;
2465                   END IF;
2466 
2467                   l_number_of_counts := ( NVL ( l_number_of_counts, 0 ) + 1 );
2468                   g_cc_entry.number_of_counts := l_number_of_counts;
2469                   -- Get the system quantity
2470                   system_quantity ( x_system_quantity => l_system_quantity
2471                                     , x_sec_system_quantity => l_sec_system_quantity ); -- nsinghi Bug#6052831. Call overloaded API.
2472 
2473                   -- Get the item primary uom code
2474                   SELECT primary_uom_code
2475                   INTO   l_primary_uom
2476                   FROM   MTL_SYSTEM_ITEMS
2477                   WHERE  inventory_item_id = g_cc_entry.inventory_item_id
2478                   AND    organization_id = g_cc_entry.organization_id;
2479 
2480                   -- Convert the system quantity into the count uom
2481                   /*2977228l_system_quantity :=
2482                      inv_convert.inv_um_convert ( g_cc_entry.inventory_item_id,
2483                                                   6,
2484                                                   l_system_quantity,
2485                                                   l_primary_uom,
2486                                                   g_count_uom,
2487                                                   NULL,
2488                                                   NULL
2489                                                 );*/
2490                   -- Convert the count quantity into the item primary uom quantity
2491                   l_primary_uom_quantity :=
2492                      inv_convert.inv_um_convert ( g_cc_entry.inventory_item_id,
2493                                                   6,
2494                                                   g_count_quantity,
2495                                                   g_count_uom,
2496                                                   l_primary_uom,
2497                                                   NULL,
2498                                                   NULL
2499                                                 );
2500 
2501                   IF ( l_number_of_counts = 1 ) THEN
2502                      entry_to_current ( p_count_date        => SYSDATE,
2503                                         p_counted_by_employee_id => g_employee_id,
2504                                         p_system_quantity   => l_system_quantity,
2505                                         p_reference         => NULL,
2506                                         p_primary_uom_quantity => l_primary_uom_quantity,
2507 					p_sec_system_quantity => l_sec_system_quantity -- nsinghi bug#6052831 Pass sec qty.
2508                                       );
2509                      current_to_first ( );
2510                   ELSE
2511                      current_to_prior ( );
2512                      entry_to_current ( p_count_date        => SYSDATE,
2513                                         p_counted_by_employee_id => g_employee_id,
2514                                         p_system_quantity   => l_system_quantity,
2515                                         p_reference         => NULL,
2516                                         p_primary_uom_quantity => l_primary_uom_quantity,
2517 					p_sec_system_quantity => l_sec_system_quantity -- nsinghi bug#6052831 Pass sec qty.
2518                                       );
2519                   END IF;
2520 
2521                   UPDATE MTL_MATERIAL_TRANSACTIONS_TEMP
2522                   SET LOCATOR_ID = l_locator_id
2523                   WHERE  CYCLE_COUNT_ID = l_cc_entry_id
2524                   AND    TRANSACTION_SOURCE_ID = l_cc_header_id
2525                   AND    LOCATOR_ID = -1;
2526 
2527                   -- Bug 5186993, if recount unmarking the serials and re-setting serials in MCSN.
2528                   if (l_entry_status_code = 3) then
2529                          unmark(l_cc_entry_id);
2530                          UPDATE MTL_CC_SERIAL_NUMBERS
2531                          SET
2532                                 UNIT_STATUS_CURRENT = DECODE((NVL(POS_ADJUSTMENT_QTY,0) -
2533                                       NVL(NEG_ADJUSTMENT_QTY,0)), 1, 2, -1, 1, UNIT_STATUS_CURRENT),
2534                                 POS_ADJUSTMENT_QTY = 0,
2535                                 NEG_ADJUSTMENT_QTY = 0,
2536                                 APPROVAL_CONDITION = NULL
2537                          WHERE CYCLE_COUNT_ENTRY_ID = l_cc_entry_id;
2538                   end if;
2539 
2540                END IF;
2541             -- Single serial count
2542             ELSIF ( l_serial_count_option = 2 ) THEN
2543                -- Check if an adjustment txn is necessary
2544                -- Get the system quantity
2545                system_quantity ( x_system_quantity => l_system_quantity
2546                                  , x_sec_system_quantity => l_sec_system_quantity ); -- nsinghi Bug#6052831. Call overloaded API.
2547 
2548                IF ( l_system_quantity <> 0 ) THEN
2549                   g_serial_out_tolerance := FALSE;
2550                ELSE
2551                   g_serial_out_tolerance := TRUE;
2552                END IF;
2553 
2554                -- Get and set the adjustment quantity and adjustment value
2555                l_adjustment_quantity := g_count_quantity - l_system_quantity;
2556                g_cc_entry.adjustment_quantity := l_adjustment_quantity;
2557 	       -- nsinghi bug#6052831 START.
2558 	       IF g_count_secondary_quantity IS NOT NULL AND l_sec_system_quantity IS NOT NULL THEN
2559 	          l_sec_adjustment_quantity := g_count_secondary_quantity - l_sec_system_quantity;
2560                   g_cc_entry.secondary_adjustment_quantity := l_sec_adjustment_quantity;
2561 	       END IF;
2562 	       -- nsinghi bug#6052831 END.
2563                g_cc_entry.adjustment_date := SYSDATE;
2564                value_variance ( x_value_variance => l_adjustment_value );
2565                g_cc_entry.adjustment_amount := l_adjustment_value;
2566                -- Update the number of counts
2567                l_number_of_counts := ( NVL ( l_number_of_counts, 0 ) + 1 );
2568                g_cc_entry.number_of_counts := l_number_of_counts;
2569 
2570                -- Get the item primary uom code
2571                SELECT primary_uom_code
2572                INTO   l_primary_uom
2573                FROM   MTL_SYSTEM_ITEMS
2574                WHERE  inventory_item_id = g_cc_entry.inventory_item_id
2575                AND    organization_id = g_cc_entry.organization_id;
2576 
2577                -- Convert the system quantity into the count uom
2578                /*2977228l_system_quantity :=
2579                   inv_convert.inv_um_convert ( g_cc_entry.inventory_item_id,
2580                                                6,
2581                                                l_system_quantity,
2582                                                l_primary_uom,
2583                                                g_count_uom,
2584                                                NULL,
2585                                                NULL
2586                                              );*/
2587                -- Convert the count quantity into the item primary uom quantity
2588                l_primary_uom_quantity :=
2589                   inv_convert.inv_um_convert ( g_cc_entry.inventory_item_id,
2590                                                6,
2591                                                g_count_quantity,
2592                                                g_count_uom,
2593                                                l_primary_uom,
2594                                                NULL,
2595                                                NULL
2596                                              );
2597 
2598                IF ( l_number_of_counts = 1 ) THEN
2599                   entry_to_current ( p_count_date        => SYSDATE,
2600                                      p_counted_by_employee_id => g_employee_id,
2601                                      p_system_quantity   => l_system_quantity,
2602                                      p_reference         => NULL,
2603                                      p_primary_uom_quantity => l_primary_uom_quantity,
2604 				     p_sec_system_quantity => l_sec_system_quantity -- nsinghi bug#6052831 Pass sec qty.
2605                                    );
2606                   current_to_first ( );
2607                ELSE
2608                   current_to_prior ( );
2609                   entry_to_current ( p_count_date        => SYSDATE,
2610                                      p_counted_by_employee_id => g_employee_id,
2611                                      p_system_quantity   => l_system_quantity,
2612                                      p_reference         => NULL,
2613                                      p_primary_uom_quantity => l_primary_uom_quantity,
2614 				     p_sec_system_quantity => l_sec_system_quantity -- nsinghi bug#6052831 Pass sec qty.
2615                                    );
2616                END IF;
2617 
2618                -- Calling new_serial_number which in turn will call
2619                -- final_preupdate_logic to process any adjustments if necessary
2620                new_serial_number ( );
2621             END IF;
2622          END IF;
2623       END IF;
2624    END pre_insert;
2625 
2626    PROCEDURE pre_update
2627    IS
2628       l_number_of_counts NUMBER := NVL ( g_cc_entry.number_of_counts, 0 );
2629       l_count_quantity NUMBER := g_count_quantity;
2630       l_count_type_code NUMBER := g_cc_entry.count_type_code;
2631       l_pre_approve_flag VARCHAR2 ( 20 ) := g_pre_approve_flag;
2632       l_cc_entry_id NUMBER := g_cc_entry.cycle_count_entry_id;
2633       l_old_num_counts NUMBER;
2634       l_serial_number_ctrl_code NUMBER;
2635       l_serial_detail NUMBER := g_cc_entry.serial_detail;
2636       l_serial_entered NUMBER := 0;
2637       l_serial_detail_option NUMBER;
2638       l_serial_count_option NUMBER;
2639       l_entry_status_code NUMBER := g_cc_entry.entry_status_code;
2640       l_total_serial_num_cnt NUMBER;
2641       l_approval_tolerance_positive NUMBER;
2642       l_approval_tolerance_negative NUMBER;
2643       l_cost_tolerance_positive NUMBER;
2644       l_cost_tolerance_negative NUMBER;
2645       l_system_quantity NUMBER;
2646       l_sec_system_quantity NUMBER; -- nsinghi bug#6052831
2647       l_primary_uom_quantity NUMBER;
2648       l_primary_uom VARCHAR2 ( 3 );
2649       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
2650 
2651 
2652       /* Bug 4495880 -Added the local variables for the lpn details from wlpn*/
2653 
2654       l_lpn_subinv  VARCHAR2(10) ;
2655       l_lpn_locator_id  NUMBER ;
2656       l_lpn_context  NUMBER;
2657 
2658       /* End of fix for Bug 4495880 */
2659 
2660       /* Added tbe below 2 variables for Bug#5604139 */
2661          l_adjustment_quantity NUMBER := 0;
2662          l_adjustment_value NUMBER := 0;
2663       /* End of fix for Bug 5604139 */
2664       l_sec_adjustment_quantity NUMBER; -- nsinghi Bug#6052831
2665 
2666    BEGIN
2667       IF ( l_debug = 1 ) THEN
2668          print_debug ( '***pre_update***' );
2669       END IF;
2670 
2671       /* Bug 4495880-Added the debug messages to check the values of the global constants */
2672 
2673          IF ( l_debug = 1 ) THEN
2674             print_debug ( 'Value of g_cc_entry.subinventory:'|| g_cc_entry.subinventory );
2675             print_debug ( 'Value of g_cc_entry.locator_id:' || g_cc_entry.locator_id );
2676             print_debug ( 'Value of g_cc_entry.parent_lpn_id ' || g_cc_entry.parent_lpn_id  );
2677             print_debug ( 'Value of g_count_quantity ' || g_count_quantity  );
2678          END IF;
2679 
2680          /* End of fix for Bug 4495880 */
2681 
2682          /* Bug 4495880 -For counts with lpns, checking if there is a discrepancy in location
2683                          of the lpn from mcce and wlpn . Further for the context 1 lpns, if
2684                          a count quantity of 0 is entered, setting the global constant to TRUE */
2685 
2686          IF ( g_cc_entry.parent_lpn_id IS NOT NULL ) THEN
2687 
2688             SELECT NVL ( subinventory_code, '###' ),
2689                    NVL ( locator_id, -99 ),
2690                    lpn_context
2691             INTO   l_lpn_subinv,
2692                    l_lpn_locator_id,
2693                    l_lpn_context
2694             FROM   WMS_LICENSE_PLATE_NUMBERS
2695             WHERE  lpn_id = g_cc_entry.parent_lpn_id ;
2696 
2697             IF ( l_debug = 1 ) THEN
2698                  print_debug ( 'l_lpn_subinv: ===> ' || l_lpn_subinv );
2699                  print_debug ( 'l_lpn_locator_id: => ' || l_lpn_locator_id );
2700                  print_debug ( 'l_lpn_context: => ' || l_lpn_context );
2701             END IF;
2702 
2703 
2704             IF (l_lpn_subinv <> g_cc_entry.subinventory
2705                 OR l_lpn_locator_id <> g_cc_entry.locator_id ) THEN
2706 
2707                  IF ( l_debug = 1 ) THEN
2708                       print_debug ( 'Location from wlpn does not match that of mcce' );
2709                  END IF;
2710 
2711                  IF l_lpn_context=1 and g_count_quantity = 0 AND g_lpn_summary_count = FALSE THEN --9452528,added summary check
2712 
2713                       IF ( l_debug = 1 ) THEN
2714                          print_debug ( 'LPN context is 1 and quantity entered is 0 so setting the paramter g_condition to TRUE' );
2715                       END IF;
2716 
2717                       g_condition:=TRUE ;
2718 
2719                  END IF;
2720 
2721             END IF;
2722 
2723          END IF;
2724 
2725          /* End of fix for Bug 4495880  */
2726 
2727 
2728       -- Get the required variable values
2729       SELECT serial_number_control_code
2730       INTO   l_serial_number_ctrl_code
2731       FROM   mtl_system_items
2732       WHERE  inventory_item_id = g_cc_entry.inventory_item_id
2733       AND    organization_id = g_cc_entry.organization_id;
2734 
2735       SELECT NVL ( serial_detail_option, 1 ),
2736              NVL ( serial_count_option, 1 )
2737       INTO   l_serial_detail_option,
2738              l_serial_count_option
2739       FROM   mtl_cycle_count_headers
2740       WHERE  cycle_count_header_id = g_cc_entry.cycle_count_header_id
2741       AND    organization_id = g_cc_entry.organization_id;
2742 
2743       -- Compare the current entry's number of counts with the value
2744       -- in the record stored in the table
2745       SELECT NVL ( number_of_counts, 0 )
2746       INTO   l_old_num_counts
2747       FROM   mtl_cycle_count_entries
2748       WHERE  cycle_count_entry_id = l_cc_entry_id;
2749 
2750       IF ( l_old_num_counts > l_number_of_counts ) THEN
2751          FND_MESSAGE.SET_NAME ( 'INV', 'INV_DUPLICATE_COUNT_UPDATE' );
2752          FND_MSG_PUB.ADD;
2753          RAISE FND_API.G_EXC_ERROR;
2754       END IF;
2755 
2756       IF ( l_count_type_code = 4 ) THEN
2757          -- Zero Count
2758          zero_count_logic ( );
2759       ELSE
2760          IF ( l_serial_number_ctrl_code IN ( 1, 6 ) ) THEN
2761             -- Not serial controlled
2762 
2763             l_number_of_counts := ( NVL ( l_number_of_counts, 0 ) + 1 );
2764             g_cc_entry.number_of_counts := l_number_of_counts;
2765             -- Get the system quantity
2766             -- nsinghi bug#6052831. Call the overloaded procedure.
2767             -- system_quantity ( x_system_quantity => l_system_quantity );
2768             system_quantity (x_system_quantity => l_system_quantity
2769                              , x_sec_system_quantity => l_sec_system_quantity);
2770 
2771             -- Get the item primary uom code
2772             SELECT primary_uom_code
2773             INTO   l_primary_uom
2774             FROM   MTL_SYSTEM_ITEMS
2775             WHERE  inventory_item_id = g_cc_entry.inventory_item_id
2776             AND    organization_id = g_cc_entry.organization_id;
2777 
2778             -- Convert the system quantity into the count uom
2779             /*2977228l_system_quantity :=
2780                inv_convert.inv_um_convert ( g_cc_entry.inventory_item_id,
2781                                             6,
2782                                             l_system_quantity,
2783                                             l_primary_uom,
2784                                             g_count_uom,
2785                                             NULL,
2786                                             NULL
2787                                           );*/
2788             -- Convert the count quantity into the item primary uom quantity
2789             l_primary_uom_quantity :=
2790                inv_convert.inv_um_convert ( g_cc_entry.inventory_item_id,
2791                                             6,
2792                                             g_count_quantity,
2793                                             g_count_uom,
2794                                             l_primary_uom,
2795                                             NULL,
2796                                             NULL
2797                                           );
2798 
2799             IF ( l_number_of_counts = 1 ) THEN
2800                entry_to_current ( p_count_date        => SYSDATE,
2801                                   p_counted_by_employee_id => g_employee_id,
2802                                   p_system_quantity   => l_system_quantity,
2803                                   p_reference         => NULL,
2804                                   p_primary_uom_quantity => l_primary_uom_quantity,
2805                                   p_sec_system_quantity => l_sec_system_quantity -- nsinghi bug#6052831 Pass sec qty.
2806                                 );
2807                current_to_first ( );
2808             ELSE
2809                current_to_prior ( );
2810 	       g_updated_prior := TRUE;				-- Bug 6371673
2811                entry_to_current ( p_count_date        => SYSDATE,
2812                                   p_counted_by_employee_id => g_employee_id,
2813                                   p_system_quantity   => l_system_quantity,
2814                                   p_reference         => NULL,
2815                                   p_primary_uom_quantity => l_primary_uom_quantity,
2816                                   p_sec_system_quantity => l_sec_system_quantity -- nsinghi bug#6052831 Pass sec qty.
2817                                 );
2818             END IF;
2819 
2820             IF ( l_pre_approve_flag = 'TRUE' ) THEN
2821                g_cc_entry.entry_status_code := 5;
2822                g_cc_entry.approval_type := 3;
2823                g_cc_entry.approver_employee_id := g_employee_id;
2824                print_debug('from pre_update : 1');
2825                final_preupdate_logic ( );
2826             ELSE
2827                get_tolerances ( pre_approve_flag    => l_pre_approve_flag,
2828                                 x_approval_tolerance_positive => l_approval_tolerance_positive,
2829                                 x_approval_tolerance_negative => l_approval_tolerance_negative,
2830                                 x_cost_tolerance_positive => l_cost_tolerance_positive,
2831                                 x_cost_tolerance_negative => l_cost_tolerance_negative
2832                               );
2833             END IF;
2834 	    g_updated_prior := FALSE;	-- Bug 6371673
2835          ELSIF ( l_serial_number_ctrl_code IN ( 2, 5 ) ) THEN
2836             -- Serial controlled item
2837             IF ( l_serial_count_option = 3 ) THEN
2838                -- Multiple serial per request
2839                is_serial_entered ( 'WHEN-VALIDATE-RECORD', l_serial_entered );
2840 
2841                 --Added the or condition in the below if statement for bug#4424743
2842                IF ( l_serial_entered = 0 or (l_serial_entered = 1 and g_count_quantity = 0)) THEN
2843                   IF ( l_debug = 1 ) THEN
2844                      print_debug ( 'Serial entered: ' || l_serial_entered );
2845                   END IF;
2846 
2847                   get_tolerances ( pre_approve_flag    => l_pre_approve_flag,
2848                                    x_approval_tolerance_positive => l_approval_tolerance_positive,
2849                                    x_approval_tolerance_negative => l_approval_tolerance_negative,
2850                                    x_cost_tolerance_positive => l_cost_tolerance_positive,
2851                                    x_cost_tolerance_negative => l_cost_tolerance_negative
2852                                  );
2853                ELSIF ( l_serial_entered = 1 ) THEN
2854                   -- If the serial number was entered, then make sure that the number
2855                   -- of serial_number marked present matches the quantity entered.
2856                   IF ( l_debug = 1 ) THEN
2857                      print_debug ( 'Serial entered: ' || l_serial_entered );
2858                   END IF;
2859 
2860                   SELECT SUM ( DECODE ( UNIT_STATUS_CURRENT, 1, 1, 0 ) )
2861                   INTO   l_total_serial_num_cnt
2862                   FROM   MTL_CC_SERIAL_NUMBERS
2863                   WHERE  CYCLE_COUNT_ENTRY_ID = l_cc_entry_id;
2864 
2865                   IF ( l_total_serial_num_cnt <> l_count_quantity ) THEN
2866                      FND_MESSAGE.SET_NAME ( 'INV',
2867                                             'INV_CC_SERIAL_DETAIL_MISMATCH'
2868                                           );
2869                      FND_MSG_PUB.ADD;
2870                      RAISE FND_API.G_EXC_ERROR;
2871                   END IF;
2872 
2873                   IF ( l_entry_status_code = 5 ) THEN
2874                      -- Completed count entries
2875                      g_cc_entry.approval_date := SYSDATE;
2876 
2877                      IF ( l_debug = 1 ) THEN
2878                         print_debug ( 'Multiple entry has been completed so call final_preupdate_logic'
2879                                     );
2880                      END IF;
2881 
2882                      -- Call this to process LPN discrepancies if any
2883                      IF ( l_debug = 1 ) THEN
2884                         print_debug ( 'This is to process LPN discrepancy if any exist'
2885                                     );
2886                      END IF;
2887                      print_debug('from pre_update : 2');
2888                      final_preupdate_logic ( );
2889                   END IF;
2890                END IF;
2891 
2892                -- Bug# 2379128
2893                -- The following code before was only called when
2894                -- l_serial_entered was equal to 1.  We need to do this
2895                -- updating even if l_serial_entered is equal to 0
2896                l_number_of_counts := ( NVL ( l_number_of_counts, 0 ) + 1 );
2897                g_cc_entry.number_of_counts := l_number_of_counts;
2898                -- Get the system quantity
2899                system_quantity ( x_system_quantity => l_system_quantity
2900                                  , x_sec_system_quantity => l_sec_system_quantity ); -- nsinghi Bug#6052831. Call overloaded API.
2901 
2902                -- Get the item primary uom code
2903                SELECT primary_uom_code
2904                INTO   l_primary_uom
2905                FROM   MTL_SYSTEM_ITEMS
2906                WHERE  inventory_item_id = g_cc_entry.inventory_item_id
2907                AND    organization_id = g_cc_entry.organization_id;
2908 
2909                -- Convert the system quantity into the count uom
2910                /*2977228l_system_quantity :=
2911                   inv_convert.inv_um_convert ( g_cc_entry.inventory_item_id,
2912                                                6,
2913                                                l_system_quantity,
2914                                                l_primary_uom,
2915                                                g_count_uom,
2916                                                NULL,
2917                                                NULL
2918                                              );*/
2919                -- Convert the count quantity into the item primary uom quantity
2920                l_primary_uom_quantity :=
2921                   inv_convert.inv_um_convert ( g_cc_entry.inventory_item_id,
2922                                                6,
2923                                                g_count_quantity,
2924                                                g_count_uom,
2925                                                l_primary_uom,
2926                                                NULL,
2927                                                NULL
2928                                              );
2929 
2930                IF ( l_number_of_counts = 1 ) THEN
2931                   entry_to_current ( p_count_date        => SYSDATE,
2932                                      p_counted_by_employee_id => g_employee_id,
2933                                      p_system_quantity   => l_system_quantity,
2934                                      p_reference         => NULL,
2935                                      p_primary_uom_quantity => l_primary_uom_quantity,
2936                                      p_sec_system_quantity => l_sec_system_quantity -- nsinghi bug#6052831 Pass sec qty.
2937                                    );
2938                   current_to_first ( );
2939                ELSE
2940                   current_to_prior ( );
2941                   entry_to_current ( p_count_date        => SYSDATE,
2942                                      p_counted_by_employee_id => g_employee_id,
2943                                      p_system_quantity   => l_system_quantity,
2944                                      p_reference         => NULL,
2945                                      p_primary_uom_quantity => l_primary_uom_quantity,
2946                                      p_sec_system_quantity => l_sec_system_quantity -- nsinghi bug#6052831 Pass sec qty.
2947                                    );
2948                END IF;
2949             -- Bug 5186993, if recount unmarking the serials and re-setting serials in MCSN.
2950              if (l_entry_status_code = 3) then
2951                     unmark(l_cc_entry_id);
2952                      UPDATE MTL_CC_SERIAL_NUMBERS
2953                          SET
2954                                 UNIT_STATUS_CURRENT = DECODE((NVL(POS_ADJUSTMENT_QTY,0) -
2955                                       NVL(NEG_ADJUSTMENT_QTY,0)), 1, 2, -1, 1, UNIT_STATUS_CURRENT),
2956                                 POS_ADJUSTMENT_QTY = 0,
2957                                 NEG_ADJUSTMENT_QTY = 0,
2958                                 APPROVAL_CONDITION = NULL
2959                          WHERE CYCLE_COUNT_ENTRY_ID = l_cc_entry_id;
2960              end if;
2961             ELSIF ( l_serial_count_option = 2 ) THEN
2962                -- Single serial per request
2963                g_serial_out_tolerance := FALSE;
2964                -- Update the number of counts
2965                l_number_of_counts := ( NVL ( l_number_of_counts, 0 ) + 1 );
2966                g_cc_entry.number_of_counts := l_number_of_counts;
2967                -- Get the system quantity
2968                system_quantity ( x_system_quantity => l_system_quantity
2969                                  , x_sec_system_quantity => l_sec_system_quantity ); -- nsinghi Bug#6052831. Call overloaded API.
2970 
2971 
2972             /* Added tbe below code for bug#5604139 */
2973             -- Get and set the adjustment quantity and adjustment value
2974             l_adjustment_quantity := g_count_quantity - l_system_quantity;
2975             g_cc_entry.adjustment_quantity := l_adjustment_quantity;
2976 	    -- nsinghi bug#6052831 START.
2977 	    IF g_count_secondary_quantity IS NOT NULL AND l_sec_system_quantity IS NOT NULL THEN
2978 	       l_sec_adjustment_quantity := g_count_secondary_quantity - l_sec_system_quantity;
2979                g_cc_entry.secondary_adjustment_quantity := l_sec_adjustment_quantity;
2980 	    END IF;
2981 	    -- nsinghi bug#6052831 END.
2982             g_cc_entry.adjustment_date := SYSDATE;
2983             value_variance ( x_value_variance => l_adjustment_value );
2984             g_cc_entry.adjustment_amount := l_adjustment_value;
2985             /* End of fix for Bug 5604139*/
2986 
2987 
2988                -- Get the item primary uom code
2989                SELECT primary_uom_code
2990                INTO   l_primary_uom
2991                FROM   MTL_SYSTEM_ITEMS
2992                WHERE  inventory_item_id = g_cc_entry.inventory_item_id
2993                AND    organization_id = g_cc_entry.organization_id;
2994 
2995                -- Convert the system quantity into the count uom
2996                /*2977228l_system_quantity :=
2997                   inv_convert.inv_um_convert ( g_cc_entry.inventory_item_id,
2998                                                6,
2999                                                l_system_quantity,
3000                                                l_primary_uom,
3001                                                g_count_uom,
3002                                                NULL,
3003                                                NULL
3004                                              );*/
3005                -- Convert the count quantity into the item primary uom quantity
3006                l_primary_uom_quantity :=
3007                   inv_convert.inv_um_convert ( g_cc_entry.inventory_item_id,
3008                                                6,
3009                                                g_count_quantity,
3010                                                g_count_uom,
3011                                                l_primary_uom,
3012                                                NULL,
3013                                                NULL
3014                                              );
3015 
3016                IF ( l_count_quantity <> l_system_quantity ) THEN
3017                   mark ( );
3018                END IF;
3019 
3020                IF ( l_number_of_counts = 1 ) THEN
3021                   entry_to_current ( p_count_date        => SYSDATE,
3022                                      p_counted_by_employee_id => g_employee_id,
3023                                      p_system_quantity   => l_system_quantity,
3024                                      p_reference         => NULL,
3025                                      p_primary_uom_quantity => l_primary_uom_quantity,
3026 				     p_sec_system_quantity => l_sec_system_quantity -- nsinghi bug#6052831 Pass sec qty.
3027                                    );
3028                   current_to_first ( );
3029                ELSE
3030                   current_to_prior ( );
3031                   entry_to_current ( p_count_date        => SYSDATE,
3032                                      p_counted_by_employee_id => g_employee_id,
3033                                      p_system_quantity   => l_system_quantity,
3034                                      p_reference         => NULL,
3035                                      p_primary_uom_quantity => l_primary_uom_quantity,
3036 				     p_sec_system_quantity => l_sec_system_quantity -- nsinghi bug#6052831 Pass sec qty.
3037                                    );
3038                END IF;
3039 
3040                -- Call existing serial number which in turn will call
3041                -- final_preupdate_logic if an adjustment is needed
3042                existing_serial_number ( );
3043 
3044                IF ( l_entry_status_code = 5 ) THEN
3045                   -- Completed count entries
3046                   g_cc_entry.approval_date := SYSDATE;
3047                END IF;
3048             END IF;
3049          END IF;
3050       END IF;
3051    EXCEPTION
3052       WHEN NO_DATA_FOUND THEN
3053          RAISE FND_API.G_EXC_ERROR;
3054    END pre_update;
3055 
3056    PROCEDURE final_preupdate_logic
3057    IS
3058       l_entry_status_code NUMBER := g_cc_entry.entry_status_code;
3059       l_number_of_counts NUMBER := g_cc_entry.number_of_counts;
3060       l_adjustment_quantity NUMBER := g_cc_entry.adjustment_quantity;
3061       l_sec_adjustment_quantity NUMBER := g_cc_entry.secondary_adjustment_quantity; -- nsinghi bug#6052831
3062       l_transaction_id NUMBER;
3063       l_org_id  NUMBER := g_cc_entry.organization_id;
3064       l_cc_header_id NUMBER := g_cc_entry.cycle_count_header_id;
3065       l_item_id NUMBER := g_cc_entry.inventory_item_id;
3066       l_sub     VARCHAR2 ( 10 ) := g_cc_entry.subinventory;
3067       l_txn_quantity NUMBER := g_cc_entry.adjustment_quantity;
3068       l_sec_txn_quantity NUMBER := g_cc_entry.secondary_adjustment_quantity;--Added for bug 7429124
3069       l_txn_uom VARCHAR2 ( 3 ) := g_cc_entry.count_uom_current;
3070       l_lot_num VARCHAR2 ( 80 ) := g_cc_entry.lot_number;--Bug 6120140 Increased lot size to 80
3071       l_lot_exp_date DATE;
3072       l_rev     VARCHAR2 ( 3 ) := g_cc_entry.revision;
3073       l_locator_id NUMBER := g_cc_entry.locator_id;
3074       l_txn_ref VARCHAR2 ( 240 ) := NULL;
3075       l_reason_id NUMBER := g_cc_entry.transaction_reason_id;
3076       l_txn_header_id NUMBER := NVL ( g_txn_header_id, -2 );
3077       l_txn_temp_id NUMBER;
3078       l_user_id NUMBER := g_user_id;
3079       l_login_id NUMBER := g_login_id;
3080       l_txn_proc_mode NUMBER := g_txn_proc_mode;
3081       l_txn_acct_id NUMBER;
3082       l_success_flag NUMBER;
3083       l_p_uom_qty NUMBER;
3084       l_cycle_count_entry_id NUMBER := g_cc_entry.cycle_count_entry_id;
3085       l_from_uom VARCHAR2 ( 3 );
3086       l_to_uom  VARCHAR2 ( 3 );
3087       l_txn_date DATE := SYSDATE;
3088       l_serial_number VARCHAR2 ( 30 ) := g_cc_entry.serial_number;
3089       l_serial_prefix VARCHAR2 ( 30 );
3090       l_lpn_id  NUMBER := g_cc_entry.parent_lpn_id;
3091       l_cost_group_id NUMBER := g_cc_entry.cost_group_id;
3092       l_system_quantity NUMBER;
3093       l_primary_uom_quantity NUMBER;
3094       -- Variables used for handling serial discrepancies
3095       l_msn_subinv VARCHAR2 ( 10 );
3096       l_msn_lot_number VARCHAR2 ( 30 );
3097       l_msn_locator_id NUMBER;
3098       l_msn_revision VARCHAR2 ( 3 );
3099       l_current_status NUMBER;
3100       l_adj_qty NUMBER;
3101       l_msn_lpn_id NUMBER;
3102       l_serial_number_ctrl_code NUMBER;
3103       l_serial_count_option NUMBER;
3104       -- Variables used for handling lpn discrepancies
3105       l_lpn_subinv VARCHAR2 ( 10 );
3106       l_lpn_locator_id NUMBER;
3107       l_lpn_context NUMBER;  --13398739
3108       l_lpn_discrepancy_flag NUMBER := 0;
3109       l_temp_lpn_count NUMBER;
3110       l_item_name VARCHAR2 ( 100 );
3111       -- Bug # 2743382
3112 
3113       v_available_quantity NUMBER;
3114       v_entry_status_code NUMBER;
3115       x_return_status VARCHAR2 ( 10 );
3116       x_qoh     NUMBER;
3117       x_att     NUMBER;
3118       v_ser_code NUMBER;
3119       v_lot_code NUMBER;
3120       v_rev_code NUMBER;
3121       v_is_ser_controlled BOOLEAN := FALSE;
3122       v_is_lot_controlled BOOLEAN := FALSE;
3123       v_is_rev_controlled BOOLEAN := FALSE;
3124       l_rqoh    NUMBER;
3125       l_qr      NUMBER;
3126       l_qs      NUMBER;
3127       l_atr     NUMBER;
3128       l_msg_count NUMBER;
3129       l_msg_data VARCHAR2 ( 2000 );
3130       l_parent_lpn_id NUMBER;
3131       l_neg_inv_rcpt_code NUMBER;
3132       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
3133       l_allow_neg_onhand_prof_val NUMBER;-- 4870490
3134       l_sec_uom VARCHAR2(3)   :=  g_count_secondary_uom      ;   -- INVCONV,NSRIVAST
3135       l_sec_qty NUMBER   :=  g_count_secondary_quantity ;   -- INVCONV,NSRIVAST
3136       -- nsinghi bug#6052831
3137       l_sqoh    NUMBER;
3138       l_srqoh   NUMBER;
3139       l_sqr     NUMBER;
3140       l_sqs     NUMBER;
3141       l_satt    NUMBER;
3142       l_satr    NUMBER;
3143       -- nsinghi bug#6052831
3144       l_outermost_lpn_id NUMBER :=NULL; --13053297
3145 
3146     BEGIN
3147       IF ( l_debug = 1 ) THEN
3148          print_debug ( '***final_preupdate_logic***' );
3149       END IF;
3150 
3151       /* Bug 5704910*/
3152       --Clearing the quantity tree cache
3153         inv_quantity_tree_pub.clear_quantity_cache;
3154 
3155       -- Get the required variable values
3156       -- Get the item primary uom code
3157       SELECT primary_uom_code
3158       INTO   l_to_uom
3159       FROM   MTL_SYSTEM_ITEMS
3160       WHERE  inventory_item_id = g_cc_entry.inventory_item_id
3161       AND    organization_id = g_cc_entry.organization_id;
3162 
3163       SELECT serial_number_control_code
3164       INTO   l_serial_number_ctrl_code
3165       FROM   mtl_system_items
3166       WHERE  inventory_item_id = g_cc_entry.inventory_item_id
3167       AND    organization_id = g_cc_entry.organization_id;
3168 
3169       SELECT NVL ( serial_count_option, 1 ),
3170              NVL ( inventory_adjustment_account, -1 )
3171       INTO   l_serial_count_option,
3172              l_txn_acct_id
3173       FROM   mtl_cycle_count_headers
3174       WHERE  cycle_count_header_id = g_cc_entry.cycle_count_header_id
3175       AND    organization_id = g_cc_entry.organization_id;
3176 
3177       SELECT concatenated_segments
3178       INTO   l_item_name
3179       FROM   mtl_system_items_kfv
3180       WHERE  inventory_item_id = l_item_id AND organization_id = l_org_id;
3181 
3182 -- Bug 3296675, we need to delete cycle count reservations before checking for availability.
3183       IF ( l_entry_status_code = 5 ) THEN
3184           delete_reservation ( );
3185       END IF;
3186 
3187       -- Bug # 2743382
3188 
3189       SELECT negative_inv_receipt_code
3190       INTO   l_neg_inv_rcpt_code --Negative Balance  1:Allowed   2:Disallowed
3191       FROM   mtl_parameters
3192       WHERE  organization_id = l_org_id;
3193 
3194        --4870490
3195          l_allow_neg_onhand_prof_val := NVL(FND_PROFILE.VALUE('INV_ALLOW_CC_TXNS_ONHAND_NEG'),2);
3196 
3197          print_debug ( 'l_neg_inv_rcpt_mode '||l_neg_inv_rcpt_code );
3198          print_debug ( 'l_allow_neg_onhand_prof_val '||l_allow_neg_onhand_prof_val );
3199 
3200 
3201  -- Bug number 4469742 commented the IF clause here AS per the discussion WITH the PM
3202   -- for the complete opinion from the PM please refer to the update in the bug
3203   --*** JSHERMAN  07/01/05 02:44 pm ***
3204   -- after this the check IF (v_available_quantity + v_adjustment_quantity < 0)  will happen
3205   -- irrespective of the the l_neg_inv_rcpt_code flag value
3206 
3207       --IF ( l_neg_inv_rcpt_code = 2 ) THEN
3208 
3209         -- print_debug ( 'l_neg_inv_rcpt_mode = 2' );
3210          SELECT serial_number_control_code,
3211                 lot_control_code,
3212                 revision_qty_control_code
3213          INTO   v_ser_code,
3214                 v_lot_code,
3215                 v_rev_code
3216          FROM   mtl_system_items
3217          WHERE  inventory_item_id = l_item_id AND organization_id = l_org_id;
3218 
3219          IF ( v_ser_code <> 1 ) THEN
3220             v_is_ser_controlled := TRUE;
3221          END IF;
3222 
3223          IF ( v_lot_code <> 1 ) THEN
3224             v_is_lot_controlled := TRUE;
3225          END IF;
3226 
3227          IF ( v_rev_code <> 1 ) THEN
3228             v_is_rev_controlled := TRUE;
3229          END IF;
3230 
3231          /* Bug 5725198-Checking if the count is for an LPN, in that case, query quantity tree
3232                            for the LPN along with it's subinventory/location. */
3233 
3234            IF ( l_lpn_id IS NOT NULL ) THEN
3235              SELECT NVL ( subinventory_code, '###' ),
3236                     NVL ( locator_id, -99 ),
3237 		            outermost_lpn_id,         --13053297
3238 	                NVL(parent_lpn_id,-99) ,   --13053297
3239                     lpn_context
3240                INTO l_lpn_subinv,
3241                     l_lpn_locator_id,
3242 		            l_outermost_lpn_id, --13053297
3243 		            l_parent_lpn_id ,    --13053297
3244                     l_lpn_context   --13398739
3245                FROM WMS_LICENSE_PLATE_NUMBERS
3246               WHERE lpn_id = l_lpn_id;
3247 
3248              IF ( l_debug = 1 ) THEN
3249                   print_debug ( 'l_lpn_subinv: ===> ' || l_lpn_subinv );
3250                   print_debug ( 'l_lpn_locator_id: => ' || l_lpn_locator_id );
3251              END IF;
3252 
3253              IF  ( l_lpn_subinv <> '###' AND l_lpn_locator_id <> -99 ) THEN
3254 
3255                inv_quantity_tree_pub.query_quantities ( p_api_version_number => 1.0,
3256                                                         p_init_msg_lst      => 'F',
3257                                                         x_return_status     => x_return_status,
3258                                                         x_msg_count         => l_msg_count,
3259                                                         x_msg_data          => l_msg_data,
3260                                                         p_organization_id   => l_org_id,
3261                                                         p_inventory_item_id => l_item_id,
3262                                                         p_tree_mode         => 1,
3263                                                         p_is_revision_control => v_is_rev_controlled,
3264                                                         p_is_lot_control    => v_is_lot_controlled,
3265                                                         p_is_serial_control => v_is_ser_controlled,
3266                                                         p_demand_source_type_id => NULL,
3267                                                         p_revision          => l_rev,
3268                                                         p_lot_number        => l_lot_num,
3269                                                         p_lot_expiration_date => l_lot_exp_date,
3270                                                         p_subinventory_code => l_lpn_subinv,
3271                                                         p_locator_id        => l_lpn_locator_id,
3272                                                         p_onhand_source     => 3,
3273                                                         p_lpn_id            => l_lpn_id,
3274                                                         x_qoh               => x_qoh,
3275                                                         x_rqoh              => l_rqoh,
3276                                                         x_qr                => l_qr,
3277                                                         x_qs                => l_qs,
3278                                                         x_att               => x_att,
3279                                                         x_atr               => l_atr
3280                                                       );
3281 
3282                v_available_quantity:= x_att;
3283 
3284              ELSE
3285                v_available_quantity:= 0;
3286 
3287              END IF ;
3288 
3289              print_debug ( 'After querying with lpn and lpn location');
3290              print_debug ( 'v_available_quantity: '||v_available_quantity);
3291              print_debug ( 'x_qoh:'|| x_qoh );
3292              print_debug ( 'l_rqoh:'|| l_rqoh );
3293              print_debug ( 'l_qr:'|| l_qr );
3294              print_debug ( 'l_qs:'|| l_qs );
3295              print_debug ( 'l_atr:'||l_atr );
3296              print_debug ( 'l_adjustment_quantity '||l_adjustment_quantity );
3297              print_debug ( 'l_sec_adjustment_quantity '||l_sec_adjustment_quantity ); -- nsinghi bug#6052831
3298              print_debug ( 'v_entry_status_code '||v_entry_status_code );
3299              print_debug ( 'l_entry_status_code '||l_entry_status_code );
3300 
3301            ELSE --Querying qty tree as before
3302 /*
3303         End of Bug 5725198
3304 */
3305 
3306          inv_quantity_tree_pub.query_quantities ( p_api_version_number => 1.0,
3307                                                   p_init_msg_lst      => 'F',
3308                                                   x_return_status     => x_return_status,
3309                                                   x_msg_count         => l_msg_count,
3310                                                   x_msg_data          => l_msg_data,
3311                                                   p_organization_id   => l_org_id,
3312                                                   p_inventory_item_id => l_item_id,
3313                                                   p_tree_mode         => 1,
3314                                                   p_is_revision_control => v_is_rev_controlled,
3315                                                   p_is_lot_control    => v_is_lot_controlled,
3316                                                   p_is_serial_control => v_is_ser_controlled,
3317                                                   p_demand_source_type_id => NULL,
3318                                                   p_revision          => l_rev,
3319                                                   p_lot_number        => l_lot_num,
3320                                                   p_lot_expiration_date => l_lot_exp_date,
3321                                                   p_subinventory_code => l_sub,
3322                                                   p_locator_id        => l_locator_id,
3323                                                   p_onhand_source     => 3,
3324                                                   x_qoh               => x_qoh,
3325                                                   x_rqoh              => l_rqoh,
3326                                                   x_qr                => l_qr,
3327                                                   x_qs                => l_qs,
3328                                                   x_att               => x_att,
3329                                                   x_atr               => l_atr
3330                                                 );
3331          v_available_quantity := x_att;
3332 
3333         print_debug ( 'v_available_quantity '||v_available_quantity);
3334         print_debug ( 'x_qoh '|| x_qoh);
3335         print_debug ( 'l_rqoh '|| l_rqoh);
3336         print_debug ( 'l_qr '|| l_qr);
3337         print_debug ( 'l_qs '|| l_qs);
3338         print_debug ( 'l_atr '||l_atr );
3339         print_debug ( 'l_adjustment_quantity '||l_adjustment_quantity );
3340         print_debug ( 'l_sec_adjustment_quantity '||l_sec_adjustment_quantity ); -- nsinghi bug#6052831
3341         print_debug ( 'v_entry_status_code '||v_entry_status_code );
3342         print_debug ( 'l_entry_status_code '||l_entry_status_code );
3343 
3344         /* End of fix for Bug 5725198 */
3345          END IF;
3346         /* End of fix for Bug 5725198 */
3347 
3348 
3349          /*Bug Number 4870490
3350          Profile Value : Yes-1
3351                          No/NUll- 2
3352          l_neg_rcpt_code 1- Allow
3353                          2-Disallow
3354 
3355          Approval Option  L-Neg_rcpot Code   Profile Value    Behaviour
3356 
3357          Always             1                 1                Allows Approval
3358          Always             1                 2                On Approval Error is shown
3359          Always             2                 1                On Approval Error is shown
3360          Always             2                 2                On Approval Error is shown
3361 
3362 
3363          Approval Option  L-Neg_rcpot Code   Profile Value    Behaviour
3364 
3365          Never             1                 1                Adjustments happen at entry
3366          Never             1                 2                Adjustments Deferrred to Approval
3367          Never             2                 1                Adjustments Deferrred to Approval
3368          Never             2                 2                Adjustments Deferrred to Approval
3369 
3370          */
3371 
3372 		/*13053297*/
3373 		-- For a Cycle Count for an Inner LPN in a different Sub/Loc, The inner LPN been counted needs to be
3374 		-- un-nested from its parent
3375 		-- We are not bothered of any thing else like the confirmed quantity, no. of items in the LPN etc.
3376 
3377 		 IF ((g_lpn_summary_count = FALSE AND l_outermost_lpn_id <> l_lpn_id AND l_parent_lpn_id <> -99)
3378 		    AND ((l_lpn_subinv <> '###' AND l_lpn_locator_id <> -99) AND
3379 	           (l_lpn_subinv <> g_cc_entry.subinventory OR l_lpn_locator_id <> g_cc_entry.locator_id )))
3380 		 THEN
3381 		 print_debug ( 'This is a Detail Count' );
3382 		 print_debug ( 'There is Location discrepancy. Please unpack me from my parent LPN' );
3383 		 print_debug ( 'l_parent_lpn_id:  '||l_parent_lpn_id );
3384 		 print_debug ( 'l_lpn_id:         '||l_lpn_id );
3385 		 print_debug ( 'l_lpn_subinv:     '||l_lpn_subinv );
3386 		 print_debug ( 'l_lpn_locator_id: '||l_lpn_locator_id );
3387 		 print_debug ( 'Calling Pack/Unpack' );
3388 
3389 		call_pack_unpack_container(
3390 	            p_lpn_id              => l_parent_lpn_id
3391               , p_content_lpn_id      => l_lpn_id
3392               , p_org_id              => l_org_id
3393               , p_subinv              => l_lpn_subinv
3394               , p_locator             => l_lpn_locator_id
3395               , p_operation           => g_unpack
3396               );
3397 		 END IF;
3398 
3399 	--13053297
3400 
3401 	 --Bug 6601010 - Added a condition to check if it is a subxfer of the LPN. Otherwise earlier check
3402          IF  ( (l_lpn_subinv <> '###' AND l_lpn_locator_id <> -99) AND
3403 	     (l_lpn_subinv <> g_cc_entry.subinventory OR l_lpn_locator_id <> g_cc_entry.locator_id ) AND l_adjustment_quantity <> 0) --Added for bug 9559613
3404          THEN
3405 	     print_debug ( 'In condition for discrepancy');
3406 
3407 	     IF  x_qoh <>  v_available_quantity AND l_entry_status_code = 5 THEN
3408 	     g_cc_entry.approval_type := NULL;
3409 	     g_cc_entry.approver_employee_id := NULL;
3410 	     g_cc_entry.approval_date := NULL;
3411 	     -- Reset the entry status code to 2: Approval required
3412 	     -- Do this for both the local variable as well as the global cycle count
3413 	     -- entry record
3414 	     g_cc_entry.entry_status_code := 2;
3415 	     l_entry_status_code := 2;
3416 	     END IF;
3417          ELSE
3418            --Bug 5095970, changing l_atr to x_att since for non-reservable subs l_atr will be 0
3419             IF ( v_available_quantity + l_adjustment_quantity < 0 AND l_entry_status_code = 5 )
3420             AND (l_allow_neg_onhand_prof_val = 2 OR l_neg_inv_rcpt_code =2  )
3421             THEN
3422             -- The cycle count adjustment should not be processed since it will
3423             -- invalidate an existing reservation/allocation.
3424 
3425             -- Reset the approval related colums in the cycle count entry record
3426             g_cc_entry.approval_type := NULL;
3427             g_cc_entry.approver_employee_id := NULL;
3428             g_cc_entry.approval_date := NULL;
3429             -- Reset the entry status code to 2: Approval required
3430             -- Do this for both the local variable as well as the global cycle count
3431             -- entry record
3432             g_cc_entry.entry_status_code := 2;
3433             l_entry_status_code := 2;
3434             END IF;
3435           END IF;  --Bug 6601010- End of check
3436 
3437             -- Bug number 4469742 moved the IF clause here AS per the discussion WITH the PM
3438             -- for the complete opinion from the PM please refer to the update in the bug
3439             --*** JSHERMAN  07/01/05 02:44 pm ***
3440             -- after this the check IF (v_available_quantity + v_adjustment_quantity < 0)  will happen
3441              -- irrespective of the the l_neg_inv_rcpt_code flag value
3442 
3443        IF ( l_neg_inv_rcpt_code = 2 ) THEN
3444 
3445         /* Bug 5725198-Added the check for LPN being counted to update quantity tree */
3446            IF (( l_lpn_id IS NOT NULL ) AND (l_lpn_subinv <> '###' AND l_lpn_locator_id <> -99)) THEN
3447               inv_quantity_tree_pub.update_quantities ( p_api_version_number => 1.0,
3448                                                         p_init_msg_lst      => 'F',
3449                                                         x_return_status     => x_return_status,
3450                                                         x_msg_count         => l_msg_count,
3451                                                         x_msg_data          => l_msg_data,
3452                                                         p_organization_id   => l_org_id,
3453                                                         p_inventory_item_id => l_item_id,
3454                                                         p_tree_mode         => 1,
3455                                                         p_is_revision_control => v_is_rev_controlled,
3456                                                         p_is_lot_control    => v_is_lot_controlled,
3457                                                         p_is_serial_control => v_is_ser_controlled,
3458                                                         p_demand_source_type_id => NULL,
3459                                                         p_revision          => l_rev,
3460                                                         p_lot_number        => l_lot_num,
3461                                                         p_subinventory_code => l_lpn_subinv,
3462                                                         p_locator_id        => l_lpn_locator_id,
3463                                                         p_onhand_source     => 3,
3464                                                         p_containerized     => 0,
3465                                                         p_primary_quantity  => ABS ( l_adjustment_quantity
3466                                                                                    ),
3467                                                         p_secondary_quantity => ABS ( l_sec_adjustment_quantity ), -- nsinghi bug#6052831
3468                                                         p_quantity_type     => 5,
3469                                                         x_qoh               => x_qoh,
3470                                                         x_rqoh              => l_rqoh,
3471                                                         x_qr                => l_qr,
3472                                                         x_qs                => l_qs,
3473                                                         x_att               => x_att,
3474                                                         x_atr               => l_atr,
3475                                                         p_lpn_id            => l_lpn_id,
3476                                                         -- nsinghi bug#6052831 START
3477                                                         x_sqoh              => l_sqoh,
3478                                                         x_srqoh             => l_srqoh,
3479                                                         x_sqr               => l_sqr,
3480                                                         x_sqs               => l_sqs,
3481                                                         x_satt              => l_satt,
3482                                                         x_satr              => l_satr
3483                                                         -- nsinghi bug#6052831 END
3484                                                       );
3485              print_debug ( 'Values after updating quantity tree for LPN ');
3486              print_debug ( 'x_qoh '|| x_qoh );
3487              print_debug ( 'l_rqoh '|| l_rqoh );
3488              print_debug ( 'l_qr '|| l_qr );
3489              print_debug ( 'l_qs '|| l_qs );
3490              print_debug ( 'x_att '||x_att );
3491              print_debug ( 'l_atr '||l_atr );
3492              print_debug ( 'l_adjustment_quantity '||l_adjustment_quantity );
3493              print_debug ( 'l_sec_adjustment_quantity '||l_sec_adjustment_quantity ); -- nsinghi bug#6052831
3494            ELSE
3495 /*
3496         End of Bug 5725198
3497 */
3498          inv_quantity_tree_pub.update_quantities ( p_api_version_number => 1.0,
3499                                                    p_init_msg_lst      => 'F',
3500                                                    x_return_status     => x_return_status,
3501                                                    x_msg_count         => l_msg_count,
3502                                                    x_msg_data          => l_msg_data,
3503                                                    p_organization_id   => l_org_id,
3504                                                    p_inventory_item_id => l_item_id,
3505                                                    p_tree_mode         => 1,
3506                                                    p_is_revision_control => v_is_rev_controlled,
3507                                                    p_is_lot_control    => v_is_lot_controlled,
3508                                                    p_is_serial_control => v_is_ser_controlled,
3509                                                    p_demand_source_type_id => NULL,
3510                                                    p_revision          => l_rev,
3511                                                    p_lot_number        => l_lot_num,
3512                                                    p_subinventory_code => l_sub,
3513                                                    p_locator_id        => l_locator_id,
3514                                                    p_onhand_source     => 3,
3515                                                    p_containerized     => 0,
3516                                                    p_primary_quantity  => ABS ( l_adjustment_quantity
3517                                                                               ),
3518                                                    p_secondary_quantity => ABS ( l_sec_adjustment_quantity ), -- nsinghi bug#6052831
3519                                                    p_quantity_type     => 5,
3520                                                    x_qoh               => x_qoh,
3521                                                    x_rqoh              => l_rqoh,
3522                                                    x_qr                => l_qr,
3523                                                    x_qs                => l_qs,
3524                                                    x_att               => x_att,
3525                                                    x_atr               => l_atr,
3526                                                    p_lpn_id            => NULL, --added for lpn reservation
3527                                                    -- nsinghi bug#6052831 START
3528                                                    x_sqoh              => l_sqoh,
3529                                                    x_srqoh             => l_srqoh,
3530                                                    x_sqr               => l_sqr,
3531                                                    x_sqs               => l_sqs,
3532                                                    x_satt              => l_satt,
3533                                                    x_satr              => l_satr
3534                                                    -- nsinghi bug#6052831 END
3535                                                  );
3536                                                             print_debug ( 'Values after updating quantity tree for loose quantity ');
3537                 /*
3538              For Bug 5725198
3539                 */
3540              print_debug ( 'x_qoh '|| x_qoh );
3541              print_debug ( 'l_rqoh '|| l_rqoh );
3542              print_debug ( 'l_qr '|| l_qr );
3543              print_debug ( 'l_qs '|| l_qs );
3544              print_debug ( 'x_att '||x_att );
3545              print_debug ( 'l_atr '||l_atr );
3546              print_debug ( 'l_adjustment_quantity '||l_adjustment_quantity );
3547              print_debug ( 'l_sec_adjustment_quantity '||l_sec_adjustment_quantity ); -- nsinghi bug#6052831
3548             END IF; --End of condition for counting for LPNs
3549            /* End of fix for Bug 5725198 */
3550 
3551 
3552       END IF;
3553 
3554       IF ( l_entry_status_code = 5 ) THEN
3555          g_cc_entry.approval_date := SYSDATE;
3556          -- Delete the reservation
3557          -- Bug 3296675, moved the delete reservation call to final_preupdate_logic and perform_serial_adj_txn
3558          -- since we are checking for availability
3559          -- delete_reservation ( );
3560       END IF;
3561 
3562       l_from_uom  := g_cc_entry.count_uom_current;
3563       l_txn_uom   := l_from_uom;
3564 
3565       IF ( l_debug = 1 ) THEN
3566          print_debug ( 'Entry Status Code: ===> ' || l_entry_status_code );
3567          print_debug ( 'Adjustment Quantity: => ' || l_adjustment_quantity );
3568          print_debug ( 'l_lpn_id:'||l_lpn_id);
3569       END IF;
3570 
3571       IF ( l_lpn_id IS NOT NULL ) THEN
3572 
3573        /* Bug 5725198-Commenting this part since the LPN sub and loc has already been fetched
3574                     before querying quantity tree */
3575 
3576          -- Check to see if the LPN exists in a discrepant location
3577 /*         SELECT NVL ( subinventory_code, '###' ),
3578                 NVL ( locator_id, -99 )
3579          INTO   l_lpn_subinv,
3580                 l_lpn_locator_id
3581          FROM   WMS_LICENSE_PLATE_NUMBERS
3582          WHERE  lpn_id = l_lpn_id;
3583 */
3584          --Bug 4346071 Added debug message
3585             IF ( l_debug = 1 ) THEN
3586                  print_debug ( 'l_lpn_subinv: ===> ' || l_lpn_subinv );
3587                  print_debug ( 'l_lpn_locator_id: => ' || l_lpn_locator_id );
3588                  print_debug ( 'g_cc_entry.subinventory:'||g_cc_entry.subinventory);
3589                 print_debug ( 'g_cc_entry.locator_id:'||g_cc_entry.locator_id);
3590             END IF;
3591             --End of fix for Bug 4346071
3592 
3593         /*
3594                 End of commented code for Bug 5725198
3595         */
3596 
3597          --Bug 4346071- Added the check for the valus of sub and locator selected from wlpn.
3598          IF   l_lpn_subinv <> '###' AND l_lpn_locator_id <> -99  THEN
3599 
3600           IF ( (   l_lpn_subinv <> g_cc_entry.subinventory
3601                 OR l_lpn_locator_id <> g_cc_entry.locator_id
3602                ) AND (g_condition = FALSE)   -- Bug 4495880- Added the check for g_condition also
3603                AND l_lpn_context = 1 --13398739
3604              ) THEN
3605             l_lpn_discrepancy_flag := 1;
3606           ELSE
3607             l_lpn_discrepancy_flag := 0;
3608           END IF;
3609          END IF; --End of fox for Bug 4346071
3610       END IF;
3611       IF ( l_debug = 1 ) THEN
3612          print_debug ( 'l_lpn_discrepancy_flag:'||l_lpn_discrepancy_flag);
3613       END IF;
3614 
3615       -- Insert into MMTT if the count entry has been completed and
3616       -- either an adjustment needs to be made, or an LPN discrepancy
3617       -- exists in which case we'll need to insert a subinventory transfer
3618       -- record into MMTT
3619       IF (     l_entry_status_code = 5
3620            AND (    l_adjustment_quantity <> 0
3621                  OR l_lpn_discrepancy_flag = 1 )
3622          ) THEN
3623 
3624          IF ( l_txn_header_id = -2 ) THEN
3625             SELECT mtl_material_transactions_s.NEXTVAL
3626             INTO   l_txn_header_id
3627             FROM   DUAL;
3628 
3629             g_txn_header_id := l_txn_header_id;
3630             print_debug ( 'l_txn_header_id '||l_txn_header_id );
3631          END IF;
3632          IF ( l_debug = 1 ) THEN
3633              print_debug ( 'l_txn_header_id '||l_txn_header_id );
3634              print_debug ( 'l_serial_number :'||l_serial_number);
3635          END IF;
3636 
3637          IF ( l_serial_number IS NOT NULL ) THEN
3638             print_debug ( 'l_serial_number '||l_serial_number);
3639             SELECT mtl_material_transactions_s.NEXTVAL
3640             INTO   l_txn_temp_id
3641             FROM   DUAL;
3642 
3643             SELECT auto_serial_alpha_prefix
3644             INTO   l_serial_prefix
3645             FROM   mtl_system_items
3646             WHERE  inventory_item_id = l_item_id
3647                    AND organization_id = l_org_id;
3648          END IF;
3649 
3650          l_p_uom_qty :=
3651             inv_convert.inv_um_convert ( l_item_id,
3652                                          5,
3653                                          l_txn_quantity,
3654                                          l_from_uom,
3655                                          l_to_uom,
3656                                          NULL,
3657                                          NULL
3658                                        );
3659          l_txn_ref   := g_cc_entry.reference_current;
3660 
3661 
3662 
3663 
3664 
3665          -- This loop is for non multiple serial counts for a serial
3666          -- controlled item entry where the serial was found in a
3667          -- discrepant location.  For multiple serials, this logic is
3668          -- taken care of already in the procedure perform_serial_adj_txn
3669          -- The serial number field is always null in the cycle count entries
3670          -- record for multiple serial count option
3671 
3672          IF ( l_serial_number IS NOT NULL ) THEN
3673             -- Check to see if the serial number is found
3674             -- in a discrepant location or not
3675             SELECT NVL ( REVISION, 'XXX' ),
3676                    NVL ( LOT_NUMBER, 'X' ),
3677                    CURRENT_STATUS,
3678                    CURRENT_SUBINVENTORY_CODE,
3679                    NVL ( CURRENT_LOCATOR_ID, 0 ),
3680                    NVL ( LPN_ID, -99 )
3681             INTO   l_msn_revision,
3682                    l_msn_lot_number,
3683                    l_current_status,
3684                    l_msn_subinv,
3685                    l_msn_locator_id,
3686                    l_msn_lpn_id
3687             FROM   MTL_SERIAL_NUMBERS
3688             WHERE  SERIAL_NUMBER = l_serial_number
3689             AND    INVENTORY_ITEM_ID = g_cc_entry.inventory_item_id
3690             AND    CURRENT_ORGANIZATION_ID = g_cc_entry.organization_id;
3691 
3692             -- If serial number exist with status 3 but at a different loc or revision etc.
3693             -- than we first need to issue out the original serial number and then process
3694             -- the receipt transaction.  Additionally, if the serial is found
3695             -- in a different LPN than what is in the system, it will also
3696             -- issue out the serial first before receiving it back into inventory
3697             IF (     l_current_status = 3
3698                  AND l_adjustment_quantity = 1
3699                  AND (    l_msn_lpn_id <> NVL ( g_cc_entry.parent_lpn_id, -99 )
3700                        OR (      (    l_msn_revision <> g_cc_entry.revision
3701                                    OR l_msn_lot_number <>
3702                                                          g_cc_entry.lot_number
3703                                    OR l_msn_subinv <> g_cc_entry.subinventory
3704                                    OR l_msn_locator_id <>
3705                                                          g_cc_entry.locator_id
3706                                  )
3707                             AND l_msn_lpn_id = -99
3708                             AND g_cc_entry.parent_lpn_id IS NULL
3709                           )
3710                      )
3711                ) THEN
3712 
3713                IF ( l_msn_revision = 'XXX' ) THEN
3714                   l_msn_revision := NULL;
3715                END IF;
3716 
3717                IF ( l_msn_lot_number = 'X' ) THEN
3718                   l_msn_lot_number := NULL;
3719                END IF;
3720 
3721                IF ( l_msn_locator_id = 0 ) THEN
3722                   l_msn_locator_id := NULL;
3723                END IF;
3724 
3725                IF ( l_msn_lpn_id = -99 ) THEN
3726                   l_msn_lpn_id := NULL;
3727                END IF;
3728 
3729                l_adj_qty   := -1;
3730 
3731                IF ( l_debug = 1 ) THEN
3732                   print_debug ( 'Serial discrepancy exists so issue out the serial first'
3733                               );
3734                   print_debug ( 'Calling cc_transact with the following parameters: '
3735                               );
3736                   print_debug ( 'org_id: ========> ' || l_org_id );
3737                   print_debug ( 'cc_header_id: ==> ' || l_cc_header_id );
3738                   print_debug ( 'item_id: =======> ' || l_item_id );
3739                   print_debug ( 'sub: ===========> ' || l_msn_subinv );
3740                   print_debug ( 'puomqty: =======> ' || -l_p_uom_qty );
3741                   print_debug ( 'txnqty: ========> ' || l_adj_qty );
3742                   print_debug ( 'txnuom: ========> ' || l_txn_uom );
3743                   print_debug ( 'txndate: =======> ' || l_txn_date );
3744                   print_debug ( 'txnacctid: =====> ' || l_txn_acct_id );
3745                   print_debug ( 'lotnum: ========> ' || l_msn_lot_number );
3746                   print_debug ( 'lotexpdate: ====> ' || l_lot_exp_date );
3747                   print_debug ( 'rev: ===========> ' || l_msn_revision );
3748                   print_debug ( 'locator_id: ====> ' || l_msn_locator_id );
3749                   print_debug ( 'txnref: ========> ' || l_txn_ref );
3750                   print_debug ( 'reasonid: ======> ' || l_reason_id );
3751                   print_debug ( 'userid: ========> ' || l_user_id );
3752                   print_debug ( 'cc_entry_id: ===> ' || l_cycle_count_entry_id );
3753                   print_debug ( 'loginid: =======> ' || l_login_id );
3754                   print_debug ( 'txnprocmode: ===> ' || l_txn_proc_mode );
3755                   print_debug ( 'txnheaderid: ===> ' || l_txn_header_id );
3756                   print_debug ( 'serialnum: =====> ' || l_serial_number );
3757                   print_debug ( 'txntempid: =====> ' || l_txn_temp_id );
3758                   print_debug ( 'serialprefix: ==> ' || l_serial_prefix );
3759                   print_debug ( 'lpn_id: ========> ' || l_msn_lpn_id );
3760                   print_debug ( 'cost_group_id: => ' || l_cost_group_id );
3761                   print_debug ( ' ' );
3762                END IF;
3763 
3764                l_success_flag :=
3765                   mtl_cc_transact_pkg.cc_transact ( org_id              => l_org_id,
3766                                                     cc_header_id        => l_cc_header_id,
3767                                                     item_id             => l_item_id,
3768                                                     sub                 => l_msn_subinv,
3769                                                     puomqty             => -l_p_uom_qty,
3770                                                     txnqty              => l_adj_qty,
3771                                                     txnuom              => l_txn_uom,
3772                                                     txndate             => l_txn_date,
3773                                                     txnacctid           => l_txn_acct_id,
3774                                                     lotnum              => l_msn_lot_number,
3775                                                     lotexpdate          => l_lot_exp_date,
3776                                                     rev                 => l_msn_revision,
3777                                                     locator_id          => l_msn_locator_id,
3778                                                     txnref              => l_txn_ref,
3779                                                     reasonid            => l_reason_id,
3780                                                     userid              => l_user_id,
3781                                                     cc_entry_id         => l_cycle_count_entry_id,
3782                                                     loginid             => l_login_id,
3783                                                     txnprocmode         => l_txn_proc_mode,
3784                                                     txnheaderid         => l_txn_header_id,
3785                                                     serialnum           => l_serial_number,
3786                                                     txntempid           => l_txn_temp_id,
3787                                                     serialprefix        => l_serial_prefix,
3788                                                     lpn_id              => l_msn_lpn_id,
3789                                                     cost_group_id       => l_cost_group_id
3790                                                   );
3791 
3792                IF ( l_debug = 1 ) THEN
3793                   print_debug ( 'success flag: ' || l_success_flag );
3794                END IF;
3795 
3796                --If success flag is 2 or 3 then set the message for invalid
3797                --material status for the lot/serial and the item combination
3798                IF ( NVL ( l_success_flag, -1 ) < 0 ) THEN
3799                   FND_MESSAGE.SET_NAME ( 'INV', 'INV_ADJ_TXN_FAILED' );
3800                   FND_MSG_PUB.ADD;
3801                   RAISE FND_API.G_EXC_ERROR;
3802                ELSIF NVL ( l_success_flag, -1 ) = 2 THEN
3803                   FND_MESSAGE.SET_NAME ( 'INV', 'INV_TRX_LOT_NA_DUE_MS' );
3804                   FND_MESSAGE.SET_TOKEN ( 'TOKEN1', l_msn_lot_number );
3805                   FND_MESSAGE.SET_TOKEN ( 'TOKEN2', l_item_name );
3806                   FND_MSG_PUB.ADD;
3807                   RAISE FND_API.G_EXC_ERROR;
3808                ELSIF NVL ( l_success_flag, -1 ) = 3 THEN
3809                   FND_MESSAGE.SET_NAME ( 'INV', 'INV_TRX_SER_NA_DUE_MS' );
3810                   FND_MESSAGE.SET_TOKEN ( 'TOKEN1', l_serial_number );
3811                   FND_MESSAGE.SET_TOKEN ( 'TOKEN2', l_item_name );
3812                   FND_MSG_PUB.ADD;
3813                   RAISE FND_API.G_EXC_ERROR;
3814                END IF;
3815 
3816                -- Get a new txn temp ID for receiving the serial back into inventory
3817                SELECT mtl_material_transactions_s.NEXTVAL
3818                INTO   l_txn_temp_id
3819                FROM   DUAL;
3820             END IF;
3821          END IF;
3822 
3823          -- This loop deals with processing LPN discrepancies and issuing
3824          -- a sub xfer for the LPN from where it should be on the system to
3825          -- where the LPN was found on the system
3826          IF ( l_debug = 1 ) THEN
3827           print_debug ( 'l_lpn_id:'||l_lpn_id);
3828          END IF;
3829          IF ( l_lpn_id IS NOT NULL ) THEN
3830             -- Check to see if the LPN exists in a discrepant location
3831             SELECT NVL ( subinventory_code, '###' ),
3832                    NVL ( locator_id, -99 )
3833             INTO   l_lpn_subinv,
3834                    l_lpn_locator_id
3835             FROM   WMS_LICENSE_PLATE_NUMBERS
3836             WHERE  lpn_id = l_lpn_id;
3837 
3838             IF ( l_debug = 1 ) THEN
3839                  print_debug ( 'l_lpn_subinv: ===> ' || l_lpn_subinv );
3840                  print_debug ( 'l_lpn_locator_id: => ' || l_lpn_locator_id );
3841                  print_debug ( 'g_cc_entry.subinventory:'||g_cc_entry.subinventory);
3842                 print_debug ( 'g_cc_entry.locator_id:'||g_cc_entry.locator_id);
3843             END IF;
3844 
3845 
3846             --Bug4958692.sub and loc are missing in LPN.So no need of doing sub transfer.
3847              IF l_lpn_subinv <> '###' AND l_lpn_locator_id <> -99  THEN
3848 
3849             -- If the LPN is found in a different location, then we will
3850             -- need to do an LPN subinventory transfer first
3851             IF (    (l_lpn_subinv <> g_cc_entry.subinventory
3852                  OR l_lpn_locator_id <> g_cc_entry.locator_id) AND l_lpn_context =1 --13398739
3853                ) THEN
3854                IF ( l_lpn_subinv = '###' ) THEN
3855                   l_lpn_subinv := NULL;
3856                END IF;
3857 
3858                IF ( l_lpn_locator_id = -99 ) THEN
3859                   l_lpn_locator_id := NULL;
3860                END IF;
3861 
3862 
3863                -- Check to see if a sub transfer record has already been
3864                -- inserted into MMTT for this LPN so that we only do this once
3865                SELECT COUNT ( * )
3866                INTO   l_temp_lpn_count
3867                FROM   mtl_material_transactions_temp
3868                WHERE  transaction_header_id = l_txn_header_id
3869                AND    inventory_item_id = -1
3870                AND    content_lpn_id = l_lpn_id
3871                AND    transaction_source_id = l_cc_header_id
3872                AND    cycle_count_id IS NULL;
3873 
3874                IF ( l_debug = 1 ) THEN
3875                   print_debug ( 'l_temp_lpn_count:'||l_temp_lpn_count);
3876                END IF;
3877 
3878                IF ( l_temp_lpn_count <> 0 ) THEN
3879                   IF ( l_debug = 1 ) THEN
3880                      print_debug ( 'The LPN sub xfer record has already been inserted into MMTT'
3881                                  );
3882                   END IF;
3883                ELSE
3884                   IF ( l_debug = 1 ) THEN
3885                      print_debug ( 'LPN discrepancy exists so transfer the LPN first'
3886                                  );
3887                      print_debug ( 'Calling cc_transact with the following parameters: '
3888                                  );
3889                      print_debug ( 'org_id: ==========> ' || l_org_id );
3890                      print_debug ( 'cc_header_id: ====> ' || l_cc_header_id );
3891                      print_debug ( 'item_id: =========> ' || -1 );
3892                      print_debug ( 'sub: =============> ' || l_lpn_subinv );
3893                      print_debug ( 'puomqty: =========> ' || 1 );
3894                      print_debug ( 'txnqty: ==========> ' || 1 );
3895                      print_debug ( 'txnuom: ==========> ' || 'Ea' );
3896                      print_debug ( 'txndate: =========> ' || l_txn_date );
3897                      print_debug ( 'txnacctid: =======> ' || l_txn_acct_id );
3898                      print_debug ( 'lotnum: ==========> ' || NULL );
3899                      print_debug ( 'lotexpdate: ======> ' || NULL );
3900                      print_debug ( 'rev: =============> ' || NULL );
3901                      print_debug ( 'locator_id: ======> ' || l_lpn_locator_id );
3902                      print_debug ( 'txnref: ==========> ' || l_txn_ref );
3903                      print_debug ( 'reasonid: ========> ' || l_reason_id );
3904                      print_debug ( 'userid: ==========> ' || l_user_id );
3905                      print_debug ( 'cc_entry_id: =====> ' || l_cycle_count_entry_id );
3906                      print_debug ( 'loginid: =========> ' || l_login_id );
3907                      print_debug ( 'txnprocmode: =====> ' || l_txn_proc_mode );
3908                      print_debug ( 'txnheaderid: =====> ' || l_txn_header_id );
3909                      print_debug ( 'serialnum: =======> ' || NULL );
3910                      print_debug ( 'txntempid: =======> ' || l_txn_temp_id );
3911                      print_debug ( 'serialprefix: ====> ' || NULL );
3912                      print_debug ( 'lpn_id: ==========> ' || l_lpn_id );
3913                      print_debug (    'transfer_sub: ====> '
3914                                    || g_cc_entry.subinventory
3915                                  );
3916                      print_debug (    'transfer_loc_id: => '
3917                                    || g_cc_entry.locator_id
3918                                  );
3919                      print_debug ( 'lpn_discrepancy: => ' || 1 );
3920                      print_debug ( ' ' );
3921                   END IF;
3922 
3923                   l_success_flag :=
3924                      mtl_cc_transact_pkg.cc_transact ( org_id              => l_org_id,
3925                                                        cc_header_id        => l_cc_header_id,
3926                                                        item_id             => -1,
3927                                                        sub                 => l_lpn_subinv,
3928                                                        PUOMQty             => 1,
3929                                                        TxnQty              => 1,
3930                                                        TxnUOM              => 'Ea',
3931                                                        TxnDate             => l_txn_date,
3932                                                        TxnAcctId           => l_txn_acct_id,
3933                                                        LotNum              => NULL,
3934                                                        LotExpDate          => NULL,
3935                                                        rev                 => NULL,
3936                                                        locator_id          => l_lpn_locator_id,
3937                                                        TxnRef              => l_txn_ref,
3938                                                        ReasonId            => l_reason_id,
3939                                                        UserId              => l_user_id,
3940                                                        cc_entry_id         => l_cycle_count_entry_id,
3941                                                        LoginId             => l_login_id,
3942                                                        TxnProcMode         => l_txn_proc_mode,
3943                                                        TxnHeaderId         => l_txn_header_id,
3944                                                        SerialNum           => NULL,
3945                                                        TxnTempId           => l_txn_temp_id,
3946                                                        SerialPrefix        => NULL,
3947                                                        Lpn_Id              => l_lpn_id,
3948                                                        transfer_sub        => g_cc_entry.subinventory,
3949                                                        transfer_loc_id     => g_cc_entry.locator_id,
3950                                                        lpn_discrepancy     => 1
3951                                                      );
3952 
3953                   IF ( l_debug = 1 ) THEN
3954                      print_debug ( 'success_flag: ' || l_success_flag );
3955                   END IF;
3956 
3957                   IF ( NVL ( l_success_flag, -1 ) < 0 ) THEN
3958                      FND_MESSAGE.SET_NAME ( 'INV', 'INV_ADJ_TXN_FAILED' );
3959                      FND_MSG_PUB.ADD;
3960                      RAISE FND_API.G_EXC_ERROR;
3961                   END IF;
3962 
3963                   -- Get a new txn temp ID for the next record into MMTT
3964                   SELECT mtl_material_transactions_s.NEXTVAL
3965                   INTO   l_txn_temp_id
3966                   FROM   DUAL;
3967                END IF;
3968             END IF;
3969             END IF; --End of fix for bug#4958692
3970             END IF;
3971 
3972          -- Note, since the procedure  system_quantity doesn't consider the sub
3973          -- or loc when finding the quantity for items packed in an LPN, if
3974          -- the LPN is discrepant, a sub transfer for the LPN will be issued.
3975          -- Thus we can still safely insert into MMTT below since the
3976          -- adjustment was calculated based on what was packed in the LPN and
3977          -- didn't take into account the sub or the loc.
3978 
3979 
3980          IF (     l_txn_quantity <> 0
3981               AND (    l_serial_count_option <> 3
3982                     OR l_serial_number_ctrl_code IN ( 1, 6 )
3983                   )
3984             ) THEN
3985 
3986             -- Insert into MMTT only if an adjustment needs to be made.
3987             -- Do not enter this loop if this was called from a multiple
3988             -- serial count with a serial controlled item that had an LPN
3989             -- discrepancy and needs a sub xfer to be processed for the LPN.
3990             -- The outer loop could have been entered if there was no adjustment
3991             -- to be made but there was an LPN discrepancy and a sub transfer
3992             -- record had to be inserted into MMTT
3993 
3994             IF ( l_debug = 1 ) THEN
3995                print_debug ( 'Calling cc_transact with the following parameters: '
3996                            );
3997                print_debug ( 'org_id: ========> ' || l_org_id );
3998                print_debug ( 'cc_header_id: ==> ' || l_cc_header_id );
3999                print_debug ( 'item_id: =======> ' || l_item_id );
4000                print_debug ( 'sub: ===========> ' || l_sub );
4001                print_debug ( 'puomqty: =======> ' || l_p_uom_qty );
4002                print_debug ( 'txnqty: ========> ' || l_txn_quantity );
4003                print_debug ( 'txnuom: ========> ' || l_txn_uom );
4004                print_debug ( 'txndate: =======> ' || l_txn_date );
4005                print_debug ( 'txnacctid: =====> ' || l_txn_acct_id );
4006                print_debug ( 'lotnum: ========> ' || l_lot_num );
4007                print_debug ( 'lotexpdate: ====> ' || l_lot_exp_date );
4008                print_debug ( 'rev: ===========> ' || l_rev );
4009                print_debug ( 'locator_id: ====> ' || l_locator_id );
4010                print_debug ( 'txnref: ========> ' || l_txn_ref );
4011                print_debug ( 'reasonid: ======> ' || l_reason_id );
4012                print_debug ( 'userid: ========> ' || l_user_id );
4013                print_debug ( 'cc_entry_id: ===> ' || l_cycle_count_entry_id );
4014                print_debug ( 'loginid: =======> ' || l_login_id );
4015                print_debug ( 'txnprocmode: ===> ' || l_txn_proc_mode );
4016                print_debug ( 'txnheaderid: ===> ' || l_txn_header_id );
4017                print_debug ( 'serialnum: =====> ' || l_serial_number );
4018                print_debug ( 'txntempid: =====> ' || l_txn_temp_id );
4019                print_debug ( 'serialprefix: ==> ' || l_serial_prefix );
4020                print_debug ( 'lpn_id: ========> ' || l_lpn_id );
4021                print_debug ( 'cost_group_id: => ' || l_cost_group_id );
4022 	       print_debug ( 'l_sec_uom:  ====> ' || l_sec_uom );
4023                print_debug ( 'l_sec_txn_quantity: => ' || l_sec_txn_quantity );
4024                print_debug ( ' ' );
4025             END IF;
4026 
4027             l_success_flag :=
4028                mtl_cc_transact_pkg.cc_transact ( org_id              => l_org_id,
4029                                                  cc_header_id        => l_cc_header_id,
4030                                                  item_id             => l_item_id,
4031                                                  sub                 => l_sub,
4032                                                  puomqty             => l_p_uom_qty,
4033                                                  txnqty              => l_txn_quantity,
4034                                                  txnuom              => l_txn_uom,
4035                                                  txndate             => l_txn_date,
4036                                                  txnacctid           => l_txn_acct_id,
4037                                                  lotnum              => l_lot_num,
4038                                                  lotexpdate          => l_lot_exp_date,
4039                                                  rev                 => l_rev,
4040                                                  locator_id          => l_locator_id,
4041                                                  txnref              => l_txn_ref,
4042                                                  reasonid            => l_reason_id,
4043                                                  userid              => l_user_id,
4044                                                  cc_entry_id         => l_cycle_count_entry_id,
4045                                                  loginid             => l_login_id,
4046                                                  txnprocmode         => l_txn_proc_mode,
4047                                                  txnheaderid         => l_txn_header_id,
4048                                                  serialnum           => l_serial_number,
4049                                                  txntempid           => l_txn_temp_id,
4050                                                  serialprefix        => l_serial_prefix,
4051                                                  lpn_id              => l_lpn_id,
4052                                                  cost_group_id       => l_cost_group_id
4053                                                 ,secUOM              => l_sec_uom   -- INVCONV,NSRIVAST
4054                                                 ,secQty              => l_sec_txn_quantity   --Added fro bug 7429124-- INVCONV,NSRIVAST
4055                                                );
4056 
4057             IF ( l_debug = 1 ) THEN
4058                print_debug (    'cc_transact API returned a success flag of: '
4059                              || l_success_flag
4060                            );
4061             END IF;
4062 
4063             --If success flag is 2 or 3 then set the message for invalid
4064             --material status for the lot/serial and the item combination
4065             IF (     ( NVL ( l_txn_header_id, -1 ) < 0 )
4066                  OR ( NVL ( l_success_flag, -1 ) < 0 )
4067                ) THEN
4068                FND_MESSAGE.SET_NAME ( 'INV', 'INV_ADJ_TXN_FAILED' );
4069                FND_MSG_PUB.ADD;
4070                RAISE FND_API.G_EXC_ERROR;
4071             ELSIF NVL ( l_success_flag, -1 ) = 2 THEN
4072                FND_MESSAGE.SET_NAME ( 'INV', 'INV_TRX_LOT_NA_DUE_MS' );
4073                FND_MESSAGE.SET_TOKEN ( 'TOKEN1', l_lot_num );
4074                FND_MESSAGE.SET_TOKEN ( 'TOKEN2', l_item_name );
4075                FND_MSG_PUB.ADD;
4076                RAISE FND_API.G_EXC_ERROR;
4077             ELSIF NVL ( l_success_flag, -1 ) = 3 THEN
4078                FND_MESSAGE.SET_NAME ( 'INV', 'INV_TRX_SER_NA_DUE_MS' );
4079                FND_MESSAGE.SET_TOKEN ( 'TOKEN1', l_serial_number );
4080                FND_MESSAGE.SET_TOKEN ( 'TOKEN2', l_item_name );
4081                FND_MSG_PUB.ADD;
4082                RAISE FND_API.G_EXC_ERROR;
4083             END IF;
4084          END IF;
4085          g_cc_entry.adjustment_date := SYSDATE;
4086          -- Set the commit status flag so that the TM will be called in the
4087          -- post commit procedure
4088          g_commit_status_flag := 1;
4089          g_cc_entry.inventory_adjustment_account := l_txn_acct_id;
4090       END IF;
4091    END final_preupdate_logic;
4092 
4093    PROCEDURE delete_reservation
4094    IS
4095       l_mtl_reservation_rec INV_RESERVATION_GLOBAL.MTL_RESERVATION_REC_TYPE
4096                             := INV_CC_RESERVATIONS_PVT.Define_Reserv_Rec_Type;
4097       l_init_msg_lst VARCHAR2 ( 1 );
4098       l_error_code NUMBER;
4099       l_return_status VARCHAR2 ( 1 );
4100       l_msg_count NUMBER;
4101       l_msg_data VARCHAR2 ( 240 );
4102       lmsg      VARCHAR2 ( 2000 );
4103       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
4104 
4105       l_lpn_subinv VARCHAR2 ( 10 );  --Bug 6401621
4106       l_lpn_locator_id NUMBER;	--Bug 6401621
4107    BEGIN
4108       IF ( l_debug = 1 ) THEN
4109          print_debug ( '***delete_reservation***' );
4110       END IF;
4111 
4112       /* Passing input variable */
4113       /* Delete only cycle count reservation */
4114       l_mtl_reservation_rec.demand_source_type_id := 9;
4115       l_mtl_reservation_rec.organization_id := g_cc_entry.organization_id;
4116       l_mtl_reservation_rec.inventory_item_id := g_cc_entry.inventory_item_id;
4117       l_mtl_reservation_rec.subinventory_code := g_cc_entry.subinventory;
4118       l_mtl_reservation_rec.revision := g_cc_entry.revision;
4119       l_mtl_reservation_rec.locator_id := g_cc_entry.locator_id;
4120       l_mtl_reservation_rec.lot_number := g_cc_entry.lot_number;
4121 
4122       --Start Bug 6401621 commented out the following line
4123       --l_mtl_reservation_rec.lpn_id := NULL;
4124 
4125        -- Bug 6401621 Cycle Count reservation not getting deleted if reservation is stamped with lpn_id
4126 	IF g_cc_entry.parent_lpn_id IS NOT NULL THEN
4127 		l_mtl_reservation_rec.lpn_id := g_cc_entry.parent_lpn_id;
4128 
4129 		 SELECT NVL (subinventory_code, '###' ),
4130                  NVL (locator_id, -99 )
4131 		 INTO   l_lpn_subinv,
4132                  l_lpn_locator_id
4133                  FROM   WMS_LICENSE_PLATE_NUMBERS
4134                  WHERE  lpn_id = g_cc_entry.parent_lpn_id;
4135 
4136 		 IF ( l_debug = 1 ) THEN
4137 		         print_debug ( '***l_lpn_subinv***' || l_lpn_subinv );
4138 			 print_debug ( '***l_lpn_locator_id***' || l_lpn_locator_id);
4139 		 END IF;
4140 
4141 		 IF  ( l_lpn_subinv <> '###' AND l_lpn_locator_id <> -99 ) THEN
4142 			 l_mtl_reservation_rec.subinventory_code := l_lpn_subinv;
4143 			 l_mtl_reservation_rec.locator_id := l_lpn_locator_id;
4144 		 END IF;
4145 	ELSE
4146 		l_mtl_reservation_rec.lpn_id := NULL;
4147 	END IF;
4148 	--End Bug 6401621
4149 
4150       -- Delete all the reservations
4151       IF ( l_debug = 1 ) THEN
4152          print_debug ( 'Calling Delete_All_Reservation with the following values for the reservation record:'
4153                      );
4154          print_debug ( 'demand_source_type_id: => ' || 9 );
4155          print_debug (    'organization_id: =======> '
4156                        || g_cc_entry.organization_id
4157                      );
4158          print_debug (    'inventory_item_id: =====> '
4159                        || g_cc_entry.inventory_item_id
4160                      );
4161          print_debug ( 'subinventory_code: =====> ' || g_cc_entry.subinventory );
4162          print_debug ( 'revision: ==============> ' || g_cc_entry.revision );
4163          print_debug ( 'locator_id: ============> ' || g_cc_entry.locator_id );
4164          print_debug ( 'lot_number: ============> ' || g_cc_entry.lot_number );
4165          print_debug ( 'lpn_id: ================> ' || NULL );
4166       END IF;
4167 
4168       INV_CC_RESERVATIONS_PVT.Delete_All_Reservation ( p_api_version_number => 1.0,
4169                                                        p_init_msg_lst      => l_init_msg_lst,
4170                                                        p_mtl_reservation_rec => l_mtl_reservation_rec,
4171                                                        x_error_code        => l_error_code,
4172                                                        x_return_status     => l_return_status,
4173                                                        x_msg_count         => l_msg_count,
4174                                                        x_msg_data          => l_msg_data
4175                                                      );
4176 
4177       IF ( l_return_status <> 'S' ) THEN
4178          FND_MSG_PUB.Count_AND_Get ( p_count             => l_msg_count,
4179                                      p_data              => l_msg_data
4180                                    );
4181          RAISE FND_API.G_EXC_ERROR;
4182       END IF;
4183    END delete_reservation;
4184 
4185    PROCEDURE duplicate_entries
4186    IS
4187       l_count   NUMBER;
4188       l_item_id NUMBER := g_cc_entry.inventory_item_id;
4189       l_revision VARCHAR2 ( 3 ) := g_cc_entry.revision;
4190       l_sub     VARCHAR2 ( 10 ) := g_cc_entry.subinventory;
4191       l_locator_id NUMBER := g_cc_entry.locator_id;
4192       l_cost_group_id NUMBER := g_cc_entry.cost_group_id;
4193       l_lot     VARCHAR2 ( 80 ) := g_cc_entry.lot_number; --Bug 6120140 Increased lot size to 80
4194       l_org_id  NUMBER := g_cc_entry.organization_id;
4195       l_cc_header_id NUMBER := g_cc_entry.cycle_count_header_id;
4196       l_cc_serial_number VARCHAR2 ( 30 ) := g_cc_entry.serial_number;
4197       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
4198       l_lpn_id  NUMBER := g_cc_entry.parent_lpn_id;
4199 
4200    BEGIN
4201       IF ( l_debug = 1 ) THEN
4202          print_debug ( '***duplicate_entries***' );
4203       END IF;
4204 
4205       SELECT COUNT ( * )
4206       INTO   l_count
4207       FROM   mtl_cycle_count_entries
4208       WHERE  cycle_count_header_id = l_cc_header_id
4209       AND    organization_id = l_org_id
4210       AND    inventory_item_id = l_item_id
4211       AND    subinventory = l_sub
4212       AND    entry_status_code IN ( 1, 2, 3 )
4213       --            AND nvl(export_flag,2) = 2
4214       AND    (    l_locator_id IS NULL
4215                OR locator_id = l_locator_id )
4216       AND    (    l_revision IS NULL
4217                OR revision = l_revision )
4218       AND    (    l_lot IS NULL
4219                OR lot_NUMBER = l_lot )
4220       AND    (    l_cc_serial_number IS NULL
4221                OR serial_number = l_cc_serial_number
4222              )
4223       AND    (    l_cost_group_id IS NULL
4224                OR cost_group_id = l_cost_group_id)
4225 
4226        AND    NVL(parent_lpn_id,-1 ) = NVL(l_lpn_id, -1);
4227 
4228       IF ( l_count > 0 ) THEN
4229          FND_MESSAGE.SET_NAME ( 'INV', 'INV_OPEN_REQUEST_EXISTS' );
4230          FND_MSG_PUB.ADD;
4231          RAISE FND_API.G_EXC_ERROR;
4232       END IF;
4233    END duplicate_entries;
4234 
4235    PROCEDURE post_commit
4236    IS
4237       l_commit_status_flag NUMBER := g_commit_status_flag;
4238       l_txn_proc_mode NUMBER := g_txn_proc_mode;
4239       l_req_id  NUMBER;
4240       l_txn_header_id NUMBER := g_txn_header_id;
4241       l_cc_header_id NUMBER := g_cc_entry.cycle_count_header_id;
4242       l_cc_entry_id NUMBER;
4243       l_entry_status_code NUMBER;
4244       l_inventory_item_id NUMBER;
4245       l_wms_installed BOOLEAN;
4246       l_return_status VARCHAR2 ( 3000 );
4247       l_msg_count NUMBER;
4248       l_msg_data VARCHAR2 ( 3000 );
4249       l_org_id  NUMBER := g_cc_entry.organization_id;
4250       l_txn_return_status NUMBER;
4251       l_proc_msg VARCHAR2 ( 3000 );
4252       l_serial_count_option NUMBER;
4253 
4254       CURSOR serial_control_cc_entry
4255       IS
4256          SELECT cycle_count_entry_id,
4257                 entry_status_code,
4258                 inventory_item_id
4259          FROM   MTL_CYCLE_COUNT_ENTRIES_V
4260          WHERE  cycle_count_header_id = l_cc_header_id
4261          --       AND nvl(export_flag,2) = 2
4262          AND    serial_number_control_code IN ( 2, 5 );
4263 
4264       -- Variables needed for calling the label printing API
4265       l_label_status VARCHAR2 ( 300 ) := NULL;
4266       l_business_flow_code NUMBER := 8;
4267       l_temp_count NUMBER;
4268       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
4269    BEGIN
4270       IF ( l_debug = 1 ) THEN
4271          print_debug ( '***post_commit***' );
4272          print_debug ( 'Commit status flag: => ' || g_commit_status_flag );
4273          print_debug ( 'Txn Process mode: ===> ' || g_txn_proc_mode );
4274          print_debug ( 'Txn header ID: ======> ' || g_txn_header_id );
4275          print_debug (    'CC header ID: =======> '
4276                        || g_cc_entry.cycle_count_header_id
4277                      );
4278          print_debug ( 'Organization ID: ====> ' || g_cc_entry.organization_id );
4279       END IF;
4280 
4281       -- First make sure that if the commit status flag is 1,
4282       -- that there does indeed exist a record in MMTT for the transaction
4283       -- manager to process
4284       IF ( l_commit_status_flag = 1 ) THEN
4285          SELECT COUNT ( * )
4286          INTO   l_temp_count
4287          FROM   mtl_material_transactions_temp
4288          WHERE  transaction_header_id = l_txn_header_id;
4289 
4290          IF ( l_temp_count = 0 ) THEN
4291             IF ( l_debug = 1 ) THEN
4292                print_debug ( 'No record exists in MMTT to process so reset the status flag'
4293                            );
4294             END IF;
4295 
4296             g_commit_status_flag := 2;
4297             l_commit_status_flag := 2;
4298          END IF;
4299       END IF;
4300 
4301       -- Bug# 2278521
4302       -- Don't need to unmark the serials anymore since if they were marked,
4303       -- that means they require an adjustment.  The TM will unmark these
4304       -- serials when it has finished processing them.
4305 
4306       -- Get the serial count option
4307       /*SELECT NVL(serial_count_option, 1)
4308         INTO l_serial_count_option
4309         FROM mtl_cycle_count_headers
4310         WHERE cycle_count_header_id = l_cc_header_id
4311         AND organization_id = l_org_id;
4312 
4313       -- Unmark the serials that were marked previously
4314       -- since they will be processed here
4315       OPEN serial_control_cc_entry;
4316       LOOP
4317          FETCH serial_control_cc_entry INTO l_cc_entry_id,
4318       l_entry_status_code, l_inventory_item_id;
4319          IF (serial_control_cc_entry%NOTFOUND) THEN
4320        EXIT;
4321          END IF;
4322          IF (l_entry_status_code = 5 OR l_entry_status_code = 3) THEN
4323        IF (l_debug = 1) THEN
4324           print_debug('Unmarking the serials for cc_entry_id: ' || l_cc_entry_id);
4325        END IF;
4326        unmark(l_cc_entry_id);
4327        -- If the entry is a multiple serial entry,
4328        -- unmark the serial explicitly here since it was marked
4329        -- through a different process compared to single serial
4330        IF (l_serial_count_option = 3) THEN
4331           UPDATE mtl_serial_numbers
4332             SET group_mark_id = NULL
4333             WHERE inventory_item_id = l_inventory_item_id
4334             AND current_organization_id = l_org_id
4335             AND serial_number IN
4336             (SELECT serial_number
4337              FROM mtl_cc_serial_numbers
4338              WHERE cycle_count_entry_id = l_cc_entry_id);
4339        END IF;
4340          END IF;
4341       END LOOP;
4342       CLOSE serial_control_cc_entry;*/
4343       -- End of Bug# 2278521
4344 
4345       IF NVL ( l_commit_status_flag, 2 ) = 1 THEN
4346          IF ( l_txn_proc_mode = 1 ) THEN
4347             /* txn usr exit */
4348 
4349             -- Call the new WMS enabled transaction manager here
4350             -- Bug# 2328371
4351             -- Also pass in the business flow code so the TM will
4352             -- automatically print the labels
4353             IF ( l_debug = 1 ) THEN
4354                print_debug ( 'Calling the online TM here: ' || l_txn_header_id
4355                            );
4356             END IF;
4357 
4358             l_txn_return_status :=
4359                INV_LPN_TRX_PUB.PROCESS_LPN_TRX ( p_trx_hdr_id        => l_txn_header_id,
4360                                                  x_proc_msg          => l_proc_msg,
4361                                                  p_business_flow_code => l_business_flow_code
4362                                                );
4363 
4364             -- Check if the Transaction Manager was successful or not
4365             IF ( l_debug = 1 ) THEN
4366                print_debug ( 'Txn return status: ' || l_txn_return_status );
4367             END IF;
4368 
4369             IF ( l_txn_return_status <> 0 ) THEN
4370                -- This 'Transaction Failed' message is set on the java side
4371                --FND_MESSAGE.SET_NAME('INV', 'INV_FAILED');
4372                --FND_MSG_PUB.ADD;
4373                RAISE FND_API.G_EXC_ERROR;
4374             END IF;
4375 
4376             FND_MESSAGE.SET_NAME ( 'INV', 'INV_ADJUSTMENTS_PROCESSED' );
4377             FND_MESSAGE.SET_TOKEN ( 'ENTITY', 'INV_CYCLE_COUNT', TRUE );
4378             FND_MSG_PUB.ADD;
4379          /* Call the label printing API. */
4380          -- Bug# 2328371
4381          -- Since we are passing in the business flow code to the TM,
4382          -- we don't need to explicitly call the label printing API anymore
4383          --print_debug('Calling print_label_wrap with the following input parameters');
4384          --print_debug('p_business_flow_code: => ' || l_business_flow_code);
4385          --print_debug('p_transaction_id: =====> ' || l_txn_header_id);
4386 
4387          -- Bug# 2301732
4388          -- Make the call to the label printing API more robust
4389          -- by trapping for exceptions when calling it
4390          /*BEGIN
4391             inv_label.print_label_wrap
4392               ( x_return_status        =>  l_return_status        ,
4393            x_msg_count            =>  l_msg_count            ,
4394            x_msg_data             =>  l_msg_data             ,
4395            x_label_status         =>  l_label_status         ,
4396            p_business_flow_code   =>  l_business_flow_code   ,
4397            p_transaction_id       =>  l_txn_header_id );
4398          EXCEPTION
4399             WHEN OTHERS THEN
4400                IF (l_debug = 1) THEN
4401                   print_debug('Error while calling label printing API');
4402                END IF;
4403                FND_MESSAGE.SET_NAME('INV', 'INV_RCV_CRT_PRINT_LABEL_FAILE');
4404                FND_MSG_PUB.ADD;
4405          END;
4406          IF (l_debug = 1) THEN
4407               print_debug('After calling label printing API: ' || l_return_status || ', ' || l_label_status || ', ' || l_msg_data);
4408          END IF;
4409 
4410          IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
4411             FND_MESSAGE.SET_NAME('INV', 'INV_RCV_CRT_PRINT_LABEL_FAILE');
4412             FND_MSG_PUB.ADD;
4413          END IF;*/
4414          ELSIF ( l_txn_proc_mode = 2 ) THEN
4415             /* txn process concurrent program */
4416 
4417             -- Call the new WMS enabled transaction manager here
4418             -- Bug# 2328371
4419             -- Also pass in the business flow code so the TM will
4420             -- automatically print the labels
4421             IF ( l_debug = 1 ) THEN
4422                print_debug (    'Calling the concurrent TM here: '
4423                              || l_txn_header_id
4424                            );
4425             END IF;
4426 
4427             l_txn_return_status :=
4428                INV_LPN_TRX_PUB.PROCESS_LPN_TRX ( p_trx_hdr_id        => l_txn_header_id,
4429                                                  x_proc_msg          => l_proc_msg,
4430                                                  p_business_flow_code => l_business_flow_code
4431                                                );
4432 
4433             -- Check if the Transaction Manager was successful or not
4434             IF ( l_debug = 1 ) THEN
4435                print_debug ( 'Txn return status: ' || l_txn_return_status );
4436             END IF;
4437 
4438             IF ( l_txn_return_status <> 0 ) THEN
4439                -- This 'Transaction Failed' message is set on the java side
4440                --FND_MESSAGE.SET_NAME('INV', 'INV_FAILED');
4441                --FND_MSG_PUB.ADD;
4442                RAISE FND_API.G_EXC_ERROR;
4443             END IF;
4444 
4445             FND_MESSAGE.SET_NAME ( 'INV', 'INV_CONC_SUBMITTED' );
4446             FND_MESSAGE.SET_TOKEN ( 'REQUEST_ID', TO_CHAR ( l_req_id ), FALSE );
4447             FND_MSG_PUB.ADD;
4448          /* Call the label printing API. */
4449          -- Bug# 2328371
4450          -- Since we are passing in the business flow code to the TM,
4451          -- we don't need to explicitly call the label printing API anymore
4452          --print_debug('Calling print_label_wrap with the following input parameters');
4453          --print_debug('p_business_flow_code: => ' || l_business_flow_code);
4454          --print_debug('p_transaction_id: =====> ' || l_txn_header_id);
4455 
4456          -- Bug# 2301732
4457          -- Make the call to the label printing API more robust
4458          -- by trapping for exceptions when calling it
4459               /*BEGIN
4460             inv_label.print_label_wrap
4461               ( x_return_status        =>  l_return_status        ,
4462            x_msg_count            =>  l_msg_count            ,
4463            x_msg_data             =>  l_msg_data             ,
4464            x_label_status         =>  l_label_status         ,
4465            p_business_flow_code   =>  l_business_flow_code   ,
4466            p_transaction_id       =>  l_txn_header_id );
4467          EXCEPTION
4468             WHEN OTHERS THEN
4469                IF (l_debug = 1) THEN
4470                   print_debug('Error while calling label printing API');
4471                END IF;
4472                FND_MESSAGE.SET_NAME('INV', 'INV_RCV_CRT_PRINT_LABEL_FAILE');
4473                FND_MSG_PUB.ADD;
4474          END;
4475          IF (l_debug = 1) THEN
4476             print_debug('After calling label printing API: ' || l_return_status || ', ' || l_label_status || ', ' || l_msg_data);
4477          END IF;
4478 
4479          IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
4480             FND_MESSAGE.SET_NAME('INV', 'INV_RCV_CRT_PRINT_LABEL_FAILE');
4481             FND_MSG_PUB.ADD;
4482          END IF;*/
4483          ELSE
4484             FND_MESSAGE.SET_NAME ( 'INV', 'INV_BACKGROUND_PENDING' );
4485             FND_MSG_PUB.ADD;
4486          END IF;
4487       END IF;
4488 
4489       IF ( l_debug = 1 ) THEN
4490          print_debug ( 'Resetting the global values here after calling post_commit'
4491                      );
4492       END IF;
4493 
4494       -- Reset the global variables
4495       g_update_flag := 2;
4496       g_insert_flag := 2;
4497       g_commit_status_flag := 2;
4498       g_txn_header_id := NULL;
4499      /* Bug 4495880 -Resetting the global paramter to FALSE*/
4500       g_condition := FALSE;
4501      /* End of fix for Bug 4495880 */
4502 
4503    END post_commit;
4504 
4505    PROCEDURE system_quantity (
4506       x_system_quantity OUT NOCOPY NUMBER
4507    )
4508    IS
4509       x_sec_system_quantity NUMBER; -- nsinghi bug#6052831
4510    BEGIN
4511 
4512    -- Bug 13796753. Deleted block comments to improve file readibility
4513       system_quantity ( x_system_quantity => x_system_quantity
4514                         , x_sec_system_quantity => x_sec_system_quantity ); -- nsinghi Bug#6052831. Call overloaded API.
4515    END system_quantity;
4516 
4517 -- nsinghi bug#6052831. Created overloaded procedure to handle secondary qty.
4518    PROCEDURE system_quantity (
4519       x_system_quantity OUT NOCOPY NUMBER
4520       , x_sec_system_quantity OUT NOCOPY NUMBER
4521    )
4522    IS
4523       l_conversion_qty NUMBER := 0;
4524       l_primary_sys_qty NUMBER := 0;
4525       l_secondary_sys_qty NUMBER := 0; -- nsinghi bug#6052831
4526       l_loaded_sec_sys_qty NUMBER := 0; -- nsinghi bug#6052831
4527       l_loaded_sys_qty NUMBER := 0; -- bug 2640378
4528       l_item_id NUMBER := g_cc_entry.inventory_item_id;
4529       l_to_uom  VARCHAR2 ( 3 ) := g_count_uom;
4530       l_from_uom VARCHAR2 ( 3 );
4531       l_org_id  NUMBER := g_cc_entry.organization_id;
4532       l_sub     VARCHAR2 ( 10 ) := g_cc_entry.subinventory;
4533       l_lot     VARCHAR2 ( 80 ) := g_cc_entry.lot_number;--Bug 6120140 Increased lot size to 80
4534       l_rev     VARCHAR2 ( 10 ) := g_cc_entry.revision;
4535       l_loc     NUMBER := g_cc_entry.locator_id;
4536       l_cost_group_id NUMBER := g_cc_entry.cost_group_id;
4537       l_last_updated_by NUMBER := g_cc_entry.last_updated_by;
4538       l_last_update_login NUMBER := g_cc_entry.last_update_login;
4539       l_cycle_count_entry_id NUMBER := g_cc_entry.cycle_count_entry_id;
4540       l_lpn_id  NUMBER := g_cc_entry.parent_lpn_id;
4541       l_current_sys_qty NUMBER := 0;
4542       l_serial_number VARCHAR2 ( 30 ) := g_cc_entry.serial_number;
4543       x_error_code NUMBER := 0;
4544       x_return_status VARCHAR2 ( 1 );
4545       x_init_msg_lst VARCHAR2 ( 1 );
4546       x_commit  VARCHAR2 ( 1 );
4547       x_msg_count NUMBER := 0;
4548       x_msg_data VARCHAR2 ( 240 );
4549       l_serial_number_control_code NUMBER;
4550       l_serial_count_option NUMBER;
4551       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
4552       /* Bug 4886188 -Added the local variables for the lpn details from wlpn*/
4553 
4554        l_lpn_subinv  VARCHAR2(10) ;
4555        l_lpn_locator_id  NUMBER ;
4556        l_lpn_context  NUMBER;
4557 
4558       /* End of fix for Bug 4886188 */
4559    BEGIN
4560       IF ( l_debug = 1 ) THEN
4561          print_debug ( '***system_quantity***' );
4562       END IF;
4563 
4564 
4565           /*
4566               ******  Fix for bug 4886188
4567               ******  If the Lpn Context is 'Issued Out of Stores' or 'Intransit' or 'Packing Context' or 'Loaded to Dock'
4568               ******  system quantity should be shown as 0. Because, ideally the LPN will not be present in that location.
4569            */
4570 
4571             IF ( l_lpn_id IS NOT NULL ) THEN
4572 
4573                SELECT NVL ( subinventory_code, '###' ),
4574                       NVL ( locator_id, -99 ),
4575                       lpn_context
4576                INTO   l_lpn_subinv,
4577                       l_lpn_locator_id,
4578                       l_lpn_context
4579                FROM   WMS_LICENSE_PLATE_NUMBERS
4580                WHERE  lpn_id = l_lpn_id ;
4581 
4582                IF ( l_debug = 1 ) THEN
4583                     print_debug ( 'l_lpn_subinv: ===> ' || l_lpn_subinv );
4584                     print_debug ( 'l_lpn_locator_id: => ' || l_lpn_locator_id );
4585                     print_debug ( 'l_lpn_context: => ' || l_lpn_context );
4586                END IF;
4587 
4588                IF ((l_lpn_context = 8 or l_lpn_context = 9 or l_lpn_context = 4 or l_lpn_context = 6 )
4589 			   --Added below condition for bug# 14068189
4590 			   OR ((NOT g_lpn_summary_count) AND NVL(g_count_quantity,0) = 0
4591 					AND(l_lpn_subinv <> '###' AND l_lpn_locator_id <> -99)
4592 					AND (l_lpn_subinv <> g_cc_entry.subinventory OR l_lpn_locator_id <> g_cc_entry.locator_id))
4593 			   )
4594 			   THEN
4595                   IF ( l_debug = 1 ) THEN
4596                     print_debug ( 'Returning the system quantity as 0' );
4597                   END IF;
4598                   x_system_quantity := 0;
4599                   g_condition:=TRUE ;
4600                   return;
4601                END IF;
4602            END IF;
4603            /*  End of fix for bug number 4886188 */
4604 
4605       -- Bug 13796753 - Deleted block comments to improve file readibility
4606       -- Bug 13652899
4607       IF (inv_cache.set_item_rec(p_organization_id => g_cc_entry.organization_id, p_item_id => g_cc_entry.inventory_item_id))
4608       THEN
4609       	l_from_uom := inv_cache.item_rec.primary_uom_code;
4610         l_serial_number_control_code := inv_cache.item_rec.serial_number_control_code;
4611       END IF;
4612 
4613       SELECT NVL ( serial_count_option, 1 )
4614       INTO   l_serial_count_option
4615       FROM   mtl_cycle_count_headers
4616       WHERE  cycle_count_header_id = g_cc_entry.cycle_count_header_id
4617       AND    organization_id = g_cc_entry.organization_id;
4618 
4619       IF ( l_debug = 1 ) THEN
4620          print_debug ( 'Serial count option: ' || l_serial_count_option );
4621       END IF;
4622 
4623       IF (    l_serial_number_control_code IN ( 1, 6 )
4624            OR l_serial_count_option = 1
4625          ) THEN
4626          IF ( l_debug = 1 ) THEN
4627             print_debug ( 'Non serial controlled item' );
4628          END IF;
4629 
4630          IF l_lpn_id IS NULL THEN
4631             IF ( l_debug = 1 ) THEN
4632                print_debug ( 'LPN ID is null' );
4633             END IF;
4634 
4635             IF wms_is_installed ( l_org_id ) THEN
4636                IF ( l_debug = 1 ) THEN
4637                   print_debug ( 'WMS is installed' );
4638                END IF;
4639 
4640                SELECT NVL ( SUM ( primary_transaction_quantity ), 0 )
4641                       , NVL ( SUM ( secondary_transaction_quantity ), 0 ) -- nsinghi bug#6052831
4642                INTO   l_primary_sys_qty
4643                       , l_secondary_sys_qty  -- nsinghi bug#6052831
4644                FROM   MTL_ONHAND_QUANTITIES_DETAIL
4645                WHERE  inventory_item_id = l_item_id
4646                AND    organization_id = l_org_id
4647                AND    NVL ( containerized_flag, 2 ) = 2
4648                AND    subinventory_code = l_sub
4649                AND    NVL ( lot_number, 'XX' ) = NVL ( l_lot, 'XX' )
4650                AND    NVL ( revision, 'XXX' ) = NVL ( l_rev, 'XXX' )
4651                AND    NVL ( locator_id, -2 ) = NVL ( l_loc, -2 )
4652                AND    NVL ( cost_group_id, -9 ) = NVL ( l_cost_group_id, -9 );
4653 
4654                SELECT NVL ( SUM ( quantity ), 0 )
4655                       , NVL ( SUM ( secondary_quantity ), 0 ) -- nsinghi bug#6052831
4656                INTO   l_loaded_sys_qty
4657                       , l_loaded_sec_sys_qty -- nsinghi bug#6052831
4658                FROM   WMS_LOADED_QUANTITIES_V
4659                WHERE  inventory_item_id = l_item_id
4660                AND    organization_id = l_org_id
4661                AND    NVL ( containerized_flag, 2 ) = 2
4662                AND    subinventory_code = l_sub
4663                AND    NVL ( lot_number, 'XX' ) = NVL ( l_lot, 'XX' )
4664                AND    NVL ( revision, 'XXX' ) = NVL ( l_rev, 'XXX' )
4665                AND    NVL ( locator_id, -2 ) = NVL ( l_loc, -2 )
4666                --Bug# 3071372
4667                --AND    NVL ( cost_group_id, -9 ) = NVL ( l_cost_group_id, -9 )
4668                AND    qty_type = 'LOADED'
4669                AND    lpn_id IS NULL
4670                AND    content_lpn_id IS NULL;                                                       -- bug 2640378
4671                                               -- need lpn_id and content lpn id is null because there could be a
4672                                               -- row in wms_loaded_quantities_v which has these fields populated for the
4673                                               -- same items/sub.. combination and here we are processing loose
4674 
4675                IF ( l_debug = 1 ) THEN
4676                   print_debug ( 'Loaded qty is ' || l_loaded_sys_qty );
4677                END IF;
4678 
4679                IF l_loaded_sys_qty > 0 THEN
4680                   l_primary_sys_qty := l_primary_sys_qty - l_loaded_sys_qty;
4681                END IF; -- bug 2640378
4682             ELSE
4683                IF ( l_debug = 1 ) THEN
4684                   print_debug ( 'WMS is not installed' );
4685                END IF;
4686 
4687                SELECT NVL ( SUM ( primary_transaction_quantity ), 0 )
4688                       , NVL ( SUM ( secondary_transaction_quantity ), 0 ) -- nsinghi bug#6052831
4689                INTO   l_primary_sys_qty
4690                       , l_secondary_sys_qty  -- nsinghi bug#6052831
4691                FROM   MTL_ONHAND_QUANTITIES_DETAIL
4692                WHERE  inventory_item_id = l_item_id
4693                AND    organization_id = l_org_id
4694                AND    NVL ( containerized_flag, 2 ) = 2
4695                AND    subinventory_code = l_sub
4696                AND    NVL ( lot_number, 'XX' ) = NVL ( l_lot, 'XX' )
4697                AND    NVL ( revision, 'XXX' ) = NVL ( l_rev, 'XXX' )
4698                AND    NVL ( locator_id, -2 ) = NVL ( l_loc, -2 );
4699             END IF;
4700          ELSE
4701             IF ( l_debug = 1 ) THEN
4702                print_debug ( 'LPN ID is not null' || l_lpn_id );
4703             END IF;
4704 
4705             MTL_INV_UTIL_GRP.Get_LPN_Item_SysQty ( p_api_version       => 0.9,
4706                                                    p_init_msg_lst      => NULL,
4707                                                    p_commit            => NULL,
4708                                                    x_return_status     => x_return_status,
4709                                                    x_msg_count         => x_msg_count,
4710                                                    x_msg_data          => x_msg_data,
4711                                                    p_organization_id   => l_org_id,
4712                                                    p_lpn_id            => l_lpn_id,
4713                                                    p_inventory_item_id => l_item_id,
4714                                                    p_lot_number        => l_lot,
4715                                                    p_revision          => l_rev,
4716                                                    p_serial_number     => l_serial_number,
4717                                                    p_cost_group_id     => l_cost_group_id,
4718                                                    x_lpn_systemqty     => l_primary_sys_qty,
4719                                                    x_lpn_sec_systemqty => l_secondary_sys_qty -- nsinghi bug#6052831
4720                                                  );
4721          END IF;
4722 
4723       ELSIF (l_serial_number_control_code IN ( 2, 5 ) AND l_serial_count_option > 1) THEN
4724 
4725 	IF ( l_debug = 1 ) THEN
4726 		print_debug ( 'Serial controlled item' );
4727 	END IF;
4728 
4729 	IF ( l_lpn_id IS NULL ) THEN
4730 		IF ( l_debug = 1 ) THEN
4731 			print_debug ( 'No LPN ID' );
4732 		END IF;
4733 
4734 		-- Bug# 2386909
4735 		-- Also make sure you query only serials which are loose
4736 
4737 		-- Bug 13796753
4738 		SELECT COUNT(*)
4739 		INTO l_primary_sys_qty
4740 		FROM
4741 			(SELECT serial_number,
4742 				inventory_item_id,
4743 				current_organization_id
4744 			 FROM mtl_serial_numbers
4745 			 WHERE serial_number = NVL(l_serial_number, serial_number)
4746 			 AND inventory_item_id = l_item_id
4747 			 AND current_organization_id = l_org_id
4748 			 AND current_subinventory_code = l_sub
4749 			 AND NVL(lot_number, 'XX') = NVL(l_lot, 'XX')
4750 			 AND NVL(revision, 'XXX') = NVL(l_rev, 'XXX')
4751 			 AND NVL(current_locator_id, -2 ) = NVL(l_loc, -2)
4752 			 AND current_status = 3
4753 			 AND lpn_id IS NULL
4754 			) msn
4755 		WHERE is_serial_loaded(msn.current_organization_id, msn.inventory_item_id, msn.serial_number, NULL) = 2;
4756 
4757 	ELSE
4758 
4759 		IF ( l_debug = 1 ) THEN
4760 			print_debug ( 'LPN ID' || l_lpn_id );
4761 		END IF;
4762 
4763 		MTL_INV_UTIL_GRP.Get_LPN_Item_SysQty (  p_api_version       => 0.9,
4764 							p_init_msg_lst      => NULL,
4765 							p_commit            => NULL,
4766 							x_return_status     => x_return_status,
4767 							x_msg_count         => x_msg_count,
4768 							x_msg_data          => x_msg_data,
4769 							p_organization_id   => l_org_id,
4770 							p_lpn_id            => l_lpn_id,
4771 							p_inventory_item_id => l_item_id,
4772 							p_lot_number        => l_lot,
4773 							p_revision          => l_rev,
4774 							p_serial_number     => l_serial_number,
4775 							p_cost_group_id     => l_cost_group_id,
4776 							x_lpn_systemqty     => l_primary_sys_qty
4777 						     );
4778 	END IF;
4779 
4780          IF ( l_serial_count_option = 3 ) THEN
4781             IF ( l_cycle_count_entry_id IS NULL ) THEN
4782                SELECT mtl_cycle_count_entries_s.NEXTVAL
4783                INTO   l_cycle_count_entry_id
4784                FROM   DUAL;
4785 
4786                g_cc_entry.cycle_count_entry_id := l_cycle_count_entry_id;
4787             END IF;
4788 
4789             -- Every time you calculate system quantity make sure that we update
4790             -- MTL_CC_SERIAL_NUMBERS table. So that change in system quantity is reflected
4791             -- in SERIAL_NUMBERS also
4792             -- Bug# 2386909
4793             -- Match against the LPN ID also when performing this insert statement
4794 
4795 			-- added index hint /*+ index(MSN MTL_SERIAL_NUMBERS_N2) */ in below SQL query, for Bug 12608056
4796             INSERT INTO MTL_CC_SERIAL_NUMBERS
4797                         ( CYCLE_COUNT_ENTRY_ID,
4798                           SERIAL_NUMBER,
4799                           LAST_UPDATE_DATE,
4800                           LAST_UPDATED_BY,
4801                           CREATION_DATE,
4802                           CREATED_BY,
4803                           LAST_UPDATE_LOGIN
4804                         )
4805                SELECT /*+ index(MSN MTL_SERIAL_NUMBERS_N2) */
4806 					  l_cycle_count_entry_id,
4807                       SERIAL_NUMBER,
4808                       SYSDATE,
4809                       l_last_updated_by,
4810                       SYSDATE,
4811                       l_last_updated_by,
4812                       l_last_update_login
4813                FROM   mtl_serial_numbers msn
4814                WHERE  msn.inventory_item_id = l_item_id
4815                AND    msn.current_organization_id = l_org_id
4816                AND    msn.current_subinventory_code = l_sub
4817                AND    NVL ( msn.lot_number, 'XX' ) = NVL ( l_lot, 'XX' )
4818                AND    NVL ( msn.revision, 'XXX' ) = NVL ( l_rev, 'XXX' )
4819                AND    NVL ( msn.current_locator_id, -2 ) = NVL ( l_loc, -2 )
4820                AND    msn.current_status = 3
4821                AND    NVL ( msn.lpn_id, -99999 ) = NVL ( l_lpn_id, -99999 )
4822                AND    NOT EXISTS (
4823                          SELECT 'x'
4824                          FROM   MTL_CC_SERIAL_NUMBERS
4825                          WHERE  CYCLE_COUNT_ENTRY_ID = l_cycle_count_entry_id
4826                          AND    SERIAL_NUMBER = msn.SERIAL_NUMBER );
4827          END IF;
4828       END IF;
4829 
4830       IF ( l_primary_sys_qty IS NULL ) THEN
4831          l_primary_sys_qty := 0;
4832       END IF;
4833       -- nsinghi bug#6052831 START
4834       IF ( l_secondary_sys_qty IS NULL ) THEN
4835          l_secondary_sys_qty := 0;
4836       END IF;
4837       -- nsinghi bug#6052831 END
4838 
4839       IF (( l_serial_count_option <> 3) OR
4840          ( l_serial_count_option = 3 AND l_serial_number_control_code IN (1, 6) ) )
4841       THEN
4842          l_conversion_qty :=
4843             inv_convert.inv_um_convert ( l_item_id,
4844                                          5,
4845                                          l_primary_sys_qty,
4846                                          l_from_uom,
4847                                          l_to_uom,
4848                                          NULL,
4849                                          NULL
4850                                        );
4851       ELSE
4852          -- Don't need to convert the quantity for multiple
4853          -- serial count option since the serials will be counted
4854          -- in the item's primary UOM code
4855          l_conversion_qty := l_primary_sys_qty;
4856       END IF;
4857 
4858       -- Set the output variable equal to the final converted system quantity
4859       x_system_quantity := l_conversion_qty;
4860       x_sec_system_quantity := l_secondary_sys_qty; -- nsinghi bug#6052831
4861 
4862       IF ( l_debug = 1 ) THEN
4863          print_debug ( 'Quantity returned: ' || x_system_quantity );
4864          print_debug ( 'Secondary Quantity returned: ' || x_sec_system_quantity );
4865       END IF;
4866    END system_quantity;
4867 
4868    PROCEDURE value_variance (
4869       x_value_variance OUT NOCOPY NUMBER
4870    )
4871    IS
4872       l_count_qty NUMBER := g_count_quantity;
4873       l_system_qty NUMBER;
4874       l_item_cost NUMBER;
4875       l_item_id NUMBER := g_cc_entry.inventory_item_id;
4876       l_value_variance NUMBER;
4877       l_conversion_qty NUMBER;
4878       l_from_uom VARCHAR2 ( 3 ) := g_count_uom;
4879       l_to_uom  VARCHAR2 ( 3 );
4880       l_adj_qty NUMBER;
4881       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
4882    BEGIN
4883       IF ( l_debug = 1 ) THEN
4884          print_debug ( '***value_variance***' );
4885       END IF;
4886 
4887       -- Get the item system quantity
4888       system_quantity ( x_system_quantity => l_system_qty );
4889       -- Get the item cost
4890       l_item_cost :=
4891          get_item_cost ( in_org_id           => g_cc_entry.organization_id,
4892                          in_item_id          => g_cc_entry.inventory_item_id,
4893                          in_locator_id       => g_cc_entry.locator_id
4894                        );
4895       g_cc_entry.item_unit_cost := l_item_cost;
4896 
4897       -- Get the item primary uom code
4898       /* -- Bug 13652899
4899       SELECT primary_uom_code
4900       INTO   l_to_uom
4901       FROM   MTL_SYSTEM_ITEMS
4902       WHERE  inventory_item_id = g_cc_entry.inventory_item_id
4903       AND    organization_id = g_cc_entry.organization_id;
4904       */
4905 
4906       -- Bug 13652899
4907       IF (inv_cache.set_item_rec(p_organization_id => g_cc_entry.organization_id, p_item_id => g_cc_entry.inventory_item_id))
4908       THEN
4909       	l_to_uom := inv_cache.item_rec.primary_uom_code;
4910       END IF;
4911 
4912       -- Convert the system quantity into the count uom
4913       /*2977288l_system_qty :=
4914          inv_convert.inv_um_convert ( g_cc_entry.inventory_item_id,
4915                                       6,
4916                                       l_system_qty,
4917                                       l_to_uom,
4918                                       g_count_uom,
4919                                       NULL,
4920                                       NULL
4921                                     );*/
4922       -- Calculate the adjusted quantity
4923       l_adj_qty   := l_count_qty - l_system_qty;
4924       -- Calculate the conversion quantity
4925       l_conversion_qty :=
4926          inv_convert.inv_um_convert ( l_item_id,
4927                                       5,
4928                                       l_adj_qty,
4929                                       l_from_uom,
4930                                       l_to_uom,
4931                                       NULL,
4932                                       NULL
4933                                     );
4934       l_value_variance := l_conversion_qty * l_item_cost;
4935       -- Set the OUT parameters
4936       x_value_variance := l_value_variance;
4937    END value_variance;
4938 
4939    FUNCTION wms_is_installed (
4940       p_organization_id IN NUMBER
4941    )
4942       RETURN BOOLEAN
4943    IS
4944       x_return_status VARCHAR2 ( 1 );
4945       x_msg_count NUMBER;
4946       x_msg_data VARCHAR2 ( 240 );
4947       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
4948    BEGIN
4949       IF ( l_debug = 1 ) THEN
4950          print_debug ( '***wms_is_installed***' );
4951       END IF;
4952 
4953       IF WMS_INSTALL.check_install ( x_return_status,
4954                                      x_msg_count,
4955                                      x_msg_data,
4956                                      p_organization_id
4957                                    ) THEN
4958          RETURN TRUE;
4959       ELSE
4960          RETURN FALSE;
4961       END IF;
4962    END wms_is_installed;
4963 
4964    FUNCTION get_item_cost (
4965       in_org_id NUMBER,
4966       in_item_id NUMBER,
4967       in_locator_id NUMBER
4968    )
4969       RETURN NUMBER
4970    IS
4971       l_item_cost NUMBER;
4972       l_locator_id NUMBER := in_locator_id;
4973       l_cost_group_id NUMBER := g_cc_entry.cost_group_id;
4974       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
4975 
4976       --Begin Bug 9650524
4977       l_process_enabled_flag VARCHAR2(1) := 'N';
4978       l_result_code          VARCHAR2(30);
4979       l_return_status        VARCHAR2(30);
4980       l_msg_count            NUMBER;
4981       l_msg_data             VARCHAR2(2000);
4982       l_inventory_item_id    NUMBER := in_item_id;
4983       l_organization_id      NUMBER := in_org_id;
4984       l_transaction_date     DATE   := NVL(g_cc_entry.adjustment_date, SYSDATE);
4985       l_cost_mthd            VARCHAR2(15);
4986       l_cmpntcls             NUMBER;
4987       l_analysis_code        VARCHAR2(15);
4988       l_no_of_rows           NUMBER;
4989 
4990       CURSOR get_process_enabled_flag IS
4991       SELECT NVL(process_enabled_flag, 'N')
4992       FROM   mtl_parameters
4993       WHERE  organization_id = l_organization_id;
4994       --End Bug 9650524
4995 
4996    BEGIN
4997       IF ( l_debug = 1 ) THEN
4998          print_debug ( '***get_item_cost***' );
4999       END IF;
5000 
5001       -- We are doing this for dynamic locators
5002       IF ( l_locator_id = -1 ) THEN
5003          l_locator_id := NULL;
5004       END IF;
5005 
5006       -- Bug# 2094288
5007       -- For standard costed orgs, get the item cost with the common
5008       -- cost group ID = 1 always.  For average costed orgs, use the
5009       -- cost group ID stamped on the transaction
5010       -- Bug # 2180251: All primary costing methods not equal to 1 should
5011       -- also be considered as an average costed org
5012 
5013     -- Begin Bug 9650524
5014     OPEN  get_process_enabled_flag;
5015     FETCH get_process_enabled_flag INTO l_process_enabled_flag;
5016     CLOSE get_process_enabled_flag;
5017 
5018     IF l_process_enabled_flag = 'Y' THEN
5019       BEGIN
5020       IF(l_debug = 1) THEN
5021          print_debug('Calling GMF_CMCOMMON.Get_Process_Item_Cost');
5022       END IF;
5023        l_result_code := GMF_CMCOMMON.Get_Process_Item_Cost
5024        (   p_api_version             => 1
5025          , p_init_msg_list           => 'F'
5026          , x_return_status           => l_return_status
5027          , x_msg_count               => l_msg_count
5028          , x_msg_data                => l_msg_data
5029          , p_inventory_item_id       => l_inventory_item_id
5030          , p_organization_id         => l_organization_id
5031          , p_transaction_date        => l_transaction_date /* Cost as on date */
5032          , p_detail_flag             => 1                  /* 1 = total cost, 2 = details; 3 = cost for a specific component class/analysis code, etc. */
5033          , p_cost_method             => l_cost_mthd        /* OPM Cost Method */
5034          , p_cost_component_class_id => l_cmpntcls
5035          , p_cost_analysis_code      => l_analysis_code
5036          , x_total_cost              => l_item_cost        /* total cost */
5037          , x_no_of_rows              => l_no_of_rows       /* number of detail rows retrieved */
5038        );
5039 
5040        IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
5041           l_item_cost := 0;
5042        END IF;
5043 
5044       EXCEPTION
5045          WHEN OTHERS THEN
5046             l_item_cost := 0;
5047       END;
5048       IF(l_debug = 1) THEN
5049          print_debug('OPM Item Cost: ' || l_item_cost);
5050       END IF;
5051     ELSE
5052     -- End Bug 9650524
5053       BEGIN
5054          SELECT NVL ( ccicv.item_cost, 0 )
5055          INTO   l_item_cost
5056          FROM   cst_cg_item_costs_view ccicv,
5057                 mtl_parameters mp
5058          WHERE  l_locator_id IS NULL
5059          AND    ccicv.organization_id = in_org_id
5060          AND    ccicv.inventory_item_id = in_item_id
5061          AND    ccicv.organization_id = mp.organization_id
5062          /* Bug 5555367 - Modified the condition
5063          AND    ccicv.cost_group_id =
5064                    DECODE ( mp.primary_cost_method,
5065                             1, 1,
5066                             NVL ( l_cost_group_id, 1 )
5067                           )
5068          */
5069          AND     ccicv.cost_group_id =
5070                       DECODE ( mp.primary_cost_method,
5071                                1, 1,
5072                                NVL ( l_cost_group_id, mp.default_cost_group_id)
5073                              )
5074          UNION ALL
5075          SELECT NVL ( ccicv.item_cost, 0 )
5076          FROM   mtl_item_locations mil,
5077                 cst_cg_item_costs_view ccicv,
5078                 mtl_parameters mp
5079          WHERE  l_locator_id IS NOT NULL
5080          AND    mil.organization_id = in_org_id
5081          AND    mil.inventory_location_id = l_locator_id
5082          AND    mil.project_id IS NULL
5083          AND    ccicv.organization_id = mil.organization_id
5084          AND    ccicv.inventory_item_id = in_item_id
5085          AND    ccicv.organization_id = mp.organization_id
5086          /* Bug 5555367 - Modified the condition
5087          AND    ccicv.cost_group_id =
5088                    DECODE ( mp.primary_cost_method,
5089                             1, 1,
5090                             NVL ( l_cost_group_id, 1 )
5091                           )
5092          */
5093          AND     ccicv.cost_group_id =
5094                       DECODE ( mp.primary_cost_method,
5095                                1, 1,
5096                                NVL ( l_cost_group_id, mp.default_cost_group_id)
5097                              )
5098          UNION ALL
5099          SELECT NVL ( ccicv.item_cost, 0 )
5100          FROM   mtl_item_locations mil,
5101                 mrp_project_parameters mrp,
5102                 cst_cg_item_costs_view ccicv,
5103                 mtl_parameters mp
5104          WHERE  l_locator_id IS NOT NULL
5105          AND    mil.organization_id = in_org_id
5106          AND    mil.inventory_location_id = l_locator_id
5107          AND    mil.project_id IS NOT NULL
5108          AND    mrp.organization_id = mil.organization_id
5109          AND    mrp.project_id = mil.project_id
5110          AND    ccicv.organization_id = mil.organization_id
5111          AND    ccicv.inventory_item_id = in_item_id
5112          AND    ccicv.organization_id = mp.organization_id
5113          AND    ccicv.cost_group_id =
5114                    DECODE ( mp.primary_cost_method,
5115                             1, 1,
5116                             NVL (  mrp.costing_group_id, 1 )
5117                           );
5118       EXCEPTION
5119          WHEN NO_DATA_FOUND THEN
5120             l_item_cost := 0;
5121       END;
5122     END IF;  -- Bug 9650524
5123 
5124       RETURN ( l_item_cost );
5125    END GET_ITEM_COST;
5126 
5127 -- This function returns 2 IF a serial is not loaded ELSE returns 1
5128 -- Added as part of bug 2640378
5129    FUNCTION IS_SERIAL_LOADED (
5130       p_organization_id IN NUMBER,
5131       p_inventory_item_id IN NUMBER,
5132       p_serial_number IN VARCHAR2,
5133       p_lpn_id    IN NUMBER
5134    )
5135       RETURN NUMBER
5136    IS
5137       l_serial_count NUMBER;
5138       l_lot_control_code NUMBER;
5139       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
5140    BEGIN
5141       IF     p_organization_id IS NOT NULL
5142          AND p_serial_number IS NOT NULL
5143          AND p_inventory_item_id IS NOT NULL THEN
5144 
5145          IF ( l_debug = 1 ) THEN
5146             print_debug ( '(IS_SERIAL_LOADED) p_inventory_item_id: ' || p_inventory_item_id || ', p_serial_number: ' || p_serial_number ||  ', p_lpn_id: ' || p_lpn_id);
5147          END IF;
5148 
5149          IF p_lpn_id IS NULL THEN
5150             -- Bug 13796753
5151             IF (inv_cache.set_item_rec(p_organization_id => p_organization_id, p_item_id => p_inventory_item_id))
5152             THEN
5153       	    	l_lot_control_code := inv_cache.item_rec.lot_control_code;
5154       	    END IF;
5155 
5156             IF l_lot_control_code = 1 THEN -- no lot control code
5157                -- just check with msnt
5158                SELECT COUNT ( * )
5159                INTO   l_serial_count
5160                FROM   mtl_serial_numbers_temp s,
5161                       wms_loaded_quantities_v wl
5162                WHERE  s.transaction_temp_id = wl.transaction_temp_id
5163                AND    wl.inventory_item_id  = p_inventory_item_id  -- Bug 13652899
5164                AND    wl.organization_id    = p_organization_id    -- Bug 13796753
5165                AND    p_serial_number BETWEEN s.fm_serial_number
5166                                           AND s.to_serial_number;
5167 
5168                IF l_serial_count = 1 THEN
5169                   RETURN 1;
5170                ELSE
5171                   RETURN 2;
5172                END IF;
5173 
5174             ELSE -- have to join mtlt also
5175                SELECT COUNT ( * )
5176                INTO   l_serial_count
5177                FROM   mtl_serial_numbers_temp s,
5178                       wms_loaded_quantities_v wl,
5179                       mtl_transaction_lots_temp l
5180                WHERE  wl.transaction_temp_id = l.transaction_temp_id
5181                AND    wl.organization_id    = p_organization_id   -- Bug 13796753
5182                AND    s.transaction_temp_id = l.serial_transaction_temp_id
5183                AND    p_serial_number BETWEEN fm_serial_number
5184                                           AND to_serial_number;
5185 
5186                IF l_serial_count >= 1 THEN
5187                   --            print_debug('lot controlled serial ' || p_serial_number || ' already loaded ');
5188                   RETURN 1;
5189                ELSE
5190                   --             print_debug('lot controlled serial ' || p_serial_number || ' not loaded ');
5191                   RETURN 2;
5192                END IF;
5193             END IF; -- lot control code
5194          ELSE -- lpn is not null
5195 
5196 --  Modified for opp cyc count 9248808
5197            SELECT COUNT ( * )
5198             INTO   l_serial_count
5199             FROM   mtl_serial_numbers s,
5200                    wms_loaded_quantities_v wl
5201             WHERE  s.lpn_id = p_lpn_id
5202 --           AND    NVL ( wl.content_lpn_id, NVL ( wl.lpn_id, -1 ) ) = s.lpn_id --  Modified for opp cyc count 9248808
5203             AND    NVL ( wl.content_lpn_id, -1 ) = s.lpn_id
5204             AND    s.serial_number = p_serial_number
5205             AND    s.current_organization_id = p_organization_id
5206             AND    wl.organization_id  = p_organization_id   --Bug 13796753
5207             AND    s.inventory_item_id = p_inventory_item_id;
5208 
5209             IF l_serial_count > 0 THEN -- serial is loaded
5210                RETURN 1;
5211             ELSE
5212 
5213 		SELECT Count(DISTINCT msn.serial_number)
5214 		INTO   l_serial_count
5215         	FROM   mtl_serial_numbers_temp msnt, mtl_material_transactions_temp mmtt,
5216                         mtl_transaction_lots_temp mtlt, mtl_serial_numbers msn, wms_dispatched_tasks wdt
5217 		WHERE  mmtt.transaction_temp_id = mtlt.transaction_temp_id (+)
5218 		AND   ((msnt.transaction_temp_id = mmtt.transaction_temp_id and
5219 								mtlt.lot_number is null) or
5220 						(msnt.transaction_temp_id = mtlt.serial_transaction_temp_id
5221 									and mtlt.lot_number is not null))
5222 		AND    mmtt.inventory_item_id = p_inventory_item_id
5223 		AND    mmtt.organization_id = p_organization_id
5224 		AND    NVL ( mmtt.lpn_id, -1 ) = p_lpn_id
5225 		AND    msn.serial_number = p_serial_number
5226 		AND    msn.serial_number BETWEEN msnt.FM_SERIAL_NUMBER AND msnt.TO_SERIAL_NUMBER
5227 		AND    NVL(msn.revision, '@@@@@') = NVL(mmtt.revision, '@@@@@') --bug#12663976
5228 		AND    msn.inventory_item_id = mmtt.inventory_item_id
5229 		AND    msn.CURRENT_ORGANIZATION_ID=mmtt.organization_id
5230 		AND    wdt.transaction_temp_id = mmtt.transaction_temp_id
5231         	AND    wdt.task_type <> 2
5232 	        AND    wdt.status = 4;
5233 
5234 
5235 	        IF l_serial_count > 0 THEN -- serial is loaded
5236 			 RETURN 1;
5237 		ELSE
5238 			 RETURN 2;
5239 		END IF;
5240            END IF;
5241          END IF; -- lpn id not/null
5242       ELSE -- org or serial or item is null
5243          RETURN 2;
5244       END IF;
5245    EXCEPTION
5246       WHEN OTHERS THEN
5247          RETURN 2;
5248    END;
5249 
5250    PROCEDURE is_serial_entered (
5251       event       IN VARCHAR2,
5252       entered     OUT NOCOPY NUMBER
5253    )
5254    IS
5255       l_serial_count_option NUMBER;
5256       l_serial_detail_option NUMBER;
5257       l_serial_detail NUMBER := g_cc_entry.serial_detail;
5258       l_cycle_count_entry_id NUMBER := g_cc_entry.cycle_count_entry_id;
5259       l_number_of_counts NUMBER;
5260       l_counts  NUMBER := g_cc_entry.number_of_counts;
5261       l_last_updated_by NUMBER := g_cc_entry.last_updated_by;
5262       l_last_update_login NUMBER := g_cc_entry.last_update_login;
5263       -- for unscheduled entries
5264       l_sub     VARCHAR2 ( 10 ) := g_cc_entry.subinventory;
5265       l_lot     VARCHAR2 ( 80 ) := g_cc_entry.lot_number; -- Increased the variable size from 30 to 80 for Bug 8717805
5266       l_rev     VARCHAR2 ( 10 ) := g_cc_entry.revision;
5267       l_loc     NUMBER := g_cc_entry.locator_id;
5268       l_item_id NUMBER := g_cc_entry.inventory_item_id;
5269       l_org_id  NUMBER := g_cc_entry.organization_id;
5270       l_system_quantity NUMBER;
5271       l_serial_number_control_code NUMBER;
5272       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
5273    BEGIN
5274       IF ( l_debug = 1 ) THEN
5275          print_debug ( '***is_serial_entered***' );
5276       END IF;
5277 
5278       -- Get the required values
5279       SELECT NVL ( serial_count_option, 1 ),
5280              NVL ( serial_detail_option, 1 )
5281       INTO   l_serial_count_option,
5282              l_serial_detail_option
5283       FROM   mtl_cycle_count_headers
5284       WHERE  cycle_count_header_id = g_cc_entry.cycle_count_header_id
5285       AND    organization_id = g_cc_entry.organization_id;
5286 
5287       -- Get the serial number control code
5288       SELECT serial_number_control_code
5289       INTO   l_serial_number_control_code
5290       FROM   mtl_system_items
5291       WHERE  inventory_item_id = g_cc_entry.inventory_item_id
5292       AND    organization_id = g_cc_entry.organization_id;
5293 
5294       -- Get the item system quantity
5295       system_quantity ( x_system_quantity => l_system_quantity );
5296       entered     := 0;
5297 
5298       IF ( l_counts IS NULL ) THEN
5299          l_counts    := 0;
5300       END IF;
5301 
5302       IF (    event = 'WHEN-VALIDATE-RECORD'
5303            OR event = 'POPULATE_SERIAL_DETAIL'
5304          ) THEN
5305          IF (      (     (     l_serial_count_option = 3
5306                            AND l_serial_detail = 2
5307                            AND g_count_quantity <> l_system_quantity
5308                          )
5309                      OR ( l_serial_count_option = 3 AND l_serial_detail = 1 )
5310                    )
5311               AND ( l_serial_number_control_code IN ( 2, 5 ) )
5312             ) THEN
5313             BEGIN
5314                SELECT   MIN ( NVL ( number_of_counts, 0 ) )
5315                INTO     l_number_of_counts
5316                FROM     mtl_cc_serial_numbers
5317                WHERE    cycle_count_entry_id = l_cycle_count_entry_id
5318                GROUP BY cycle_count_entry_id;
5319             EXCEPTION
5320                WHEN NO_DATA_FOUND THEN
5321                   l_number_of_counts := 0;
5322             END;
5323 
5324             IF (      (     ( l_number_of_counts = 0 )
5325                         OR ( l_number_of_counts < l_counts )
5326                       )
5327                  AND ( event = 'WHEN-VALIDATE-RECORD' )
5328                  AND (    l_system_quantity <> 0
5329                        OR g_count_quantity <> 0 )
5330                ) THEN
5331                -- FND_MESSAGE.SET_NAME('INV', 'INV_CC_NO_SN_INFO');
5332                -- FND_MSG_PUB.ADD;
5333                -- RAISE FND_API.G_EXC_ERROR;
5334                IF ( l_debug = 1 ) THEN
5335                   print_debug ( 'No SN info!' );
5336                END IF;
5337             ELSE
5338                entered     := 1;
5339             END IF;
5340          END IF;
5341       ELSIF event = 'OUT-TOLERANCE' THEN
5342          IF (      (     (     l_serial_count_option = 3
5343                            AND l_serial_detail = 2
5344                            AND ( g_count_quantity <> l_system_quantity )
5345                          )
5346                      OR ( l_serial_count_option = 3 AND l_serial_detail = 1 )
5347                    )
5348               AND ( l_serial_number_control_code IN ( 2, 5 ) )
5349             ) THEN
5350             BEGIN
5351                SELECT   MIN ( number_of_counts )
5352                INTO     l_number_of_counts
5353                FROM     mtl_cc_serial_numbers
5354                WHERE    cycle_count_entry_id = l_cycle_count_entry_id
5355                GROUP BY cycle_count_entry_id;
5356             EXCEPTION
5357                WHEN NO_DATA_FOUND THEN
5358                   l_number_of_counts := 0;
5359             END;
5360 
5361             IF (    l_number_of_counts = 0
5362                  OR l_number_of_counts < l_counts ) THEN
5363                FND_MESSAGE.SET_NAME ( 'INV', 'INV_CC_OUT_TOL_NO_SN' );
5364                FND_MSG_PUB.ADD;
5365                RAISE FND_API.G_EXC_ERROR;
5366             ELSE
5367                entered     := 1;
5368             END IF;
5369          END IF;
5370       END IF;
5371    END is_serial_entered;
5372 
5373    PROCEDURE new_serial_number
5374    IS
5375       l_serial_number VARCHAR2 ( 30 );
5376       l_serial_count_option NUMBER;
5377       l_item_id NUMBER := g_cc_entry.inventory_item_id;
5378       l_serial_detail NUMBER := g_cc_entry.serial_detail;
5379       l_garbage VARCHAR2 ( 30 );
5380       l_count   NUMBER;
5381       l_success BOOLEAN := FALSE;
5382       l_cycle_count_entry_id NUMBER := g_cc_entry.cycle_count_entry_id;
5383       l_receipt VARCHAR2 ( 1 ) := 'R';
5384       l_issue   VARCHAR2(1)  := 'I';  /* Added by Bug 7229492 */
5385       l_serial_adjustment_option NUMBER;
5386       l_adjustment_quantity NUMBER := g_cc_entry.adjustment_quantity;
5387       l_approval_tolerance_positive NUMBER;
5388       l_approval_tolerance_negative NUMBER;
5389       l_cost_tolerance_positive NUMBER;
5390       l_cost_tolerance_negative NUMBER;
5391       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
5392 
5393       --Bug 5186993
5394       l_automatic_recount_flag NUMBER;
5395       l_maximum_auto_recounts  NUMBER;
5396       l_days_until_late        NUMBER;
5397       --Bug 6978840
5398       l_approval_option_code   NUMBER;
5399 
5400    BEGIN
5401       IF ( l_debug = 1 ) THEN
5402          print_debug ( '***new_serial_number***' );
5403       END IF;
5404 
5405       -- Get the required variable values
5406       SELECT    NVL ( serial_count_option, 1 ),
5407                 NVL ( serial_adjustment_option, 2 ),
5408                 NVL ( automatic_recount_flag, 2 ),
5409                 NVL ( maximum_auto_recounts, 0 ),
5410                 NVL ( days_until_late , 0 ),
5411                 --Bug 6978840
5412                 NVL ( approval_option_code , 3)
5413       INTO   l_serial_count_option,
5414              l_serial_adjustment_option,
5415              l_automatic_recount_flag,
5416              l_maximum_auto_recounts,
5417              l_days_until_late,
5418              l_approval_option_code
5419       FROM   mtl_cycle_count_headers
5420       WHERE  cycle_count_header_id = g_cc_entry.cycle_count_header_id
5421       AND    organization_id = g_cc_entry.organization_id;
5422 
5423       IF ( l_serial_count_option = 3 ) THEN
5424          -- Multiple serial number per count
5425          l_serial_number := g_cc_serial_entry.serial_number;
5426 
5427          SELECT COUNT ( * )
5428          INTO   l_count
5429          FROM   MTL_CC_SERIAL_NUMBERS
5430          WHERE  serial_number = l_serial_number
5431          AND    cycle_count_entry_id = l_cycle_count_entry_id;
5432 
5433          IF ( l_count > 0 ) THEN
5434             FND_MESSAGE.SET_NAME ( 'INV', 'INV_DUP' );
5435             FND_MESSAGE.SET_TOKEN ( 'VALUE1', l_serial_number );
5436             FND_MSG_PUB.ADD;
5437             ROLLBACK TO save_serial_detail;
5438             RAISE FND_API.G_EXC_ERROR;
5439          END IF;
5440       ELSIF ( l_serial_count_option = 2 ) THEN
5441          -- Single serial per count
5442          l_serial_number := g_cc_entry.serial_number;
5443       END IF;
5444 
5445       IF ( l_serial_number IS NULL ) THEN
5446          FND_MESSAGE.SET_NAME ( 'INV', 'INV_CC_NULL_SN' );
5447          FND_MSG_PUB.ADD;
5448          l_success   := FALSE;
5449 
5450          IF ( l_serial_count_option = 3 ) THEN
5451             ROLLBACK TO save_serial_detail;
5452          END IF;
5453 
5454          RAISE FND_API.G_EXC_ERROR;
5455       END IF;
5456 
5457         IF ( l_debug = 1 ) THEN
5458            print_debug ( 'l_automatic_recount_flag = '||l_automatic_recount_flag||', g_cc_entry.number_of_counts = '||g_cc_entry.number_of_counts);
5459            print_debug ( 'l_maximum_auto_recounts = '||l_maximum_auto_recounts||', l_adjustment_quantity = '||l_adjustment_quantity);
5460          END IF;
5461 
5462        -- Bug 5186993, if automatic recount is set, check whether the adjustment has been
5463        -- counted the maximum number of times, if not setting for recount and return
5464        -- Bug 6978840 , checking if the approval option is 'If out of tolerance' and tolerance is not met
5465 
5466        if ( l_automatic_recount_flag = 1 AND l_serial_count_option = 2
5467               AND nvl(g_cc_entry.number_of_counts, 0) <= l_maximum_auto_recounts
5468               AND nvl(l_adjustment_quantity, 0) <> 0 ) THEN
5469                 IF ( l_debug = 1 ) THEN
5470                    print_debug ( 'new_serial_number: Setting to recount' );
5471                 END IF;
5472                 g_serial_entry_status_code := 3;
5473                 count_entry_status_code();
5474                 get_final_count_info();
5475                 g_cc_entry.count_due_date := SYSDATE + l_days_until_late;
5476                 return;
5477        end if;
5478 
5479 
5480      /* If condition Added by 7229492 */
5481     if (l_adjustment_quantity < 0 and l_serial_count_option = 2)
5482 	then
5483     l_success := check_serial_number_location(l_issue);
5484 	else
5485     l_success := check_serial_number_location(l_receipt);
5486     End if;
5487    /* If condition Added by 7229492 */
5488 
5489     IF ( l_success = FALSE ) THEN
5490          IF ( l_debug = 1 ) THEN
5491             print_debug ( 'check serial number: FALSE' );
5492          END IF;
5493 
5494          mark ( );
5495       ELSE
5496          IF ( l_debug = 1 ) THEN
5497             print_debug ( 'check serial number: TRUE' );
5498          END IF;
5499       END IF;
5500 
5501       -- Calculate if the serial is out of tolerance or not
5502       get_tolerances ( pre_approve_flag    => 'SERIAL',
5503                        x_approval_tolerance_positive => l_approval_tolerance_positive,
5504                        x_approval_tolerance_negative => l_approval_tolerance_negative,
5505                        x_cost_tolerance_positive => l_cost_tolerance_positive,
5506                        x_cost_tolerance_negative => l_cost_tolerance_negative
5507                      );
5508       serial_tolerance_logic ( p_serial_adj_qty    => l_adjustment_quantity,
5509                                p_app_tol_pos       => l_approval_tolerance_positive,
5510                                p_app_tol_neg       => l_approval_tolerance_negative,
5511                                p_cost_tol_pos      => l_cost_tolerance_positive,
5512                                p_cost_tol_neg      => l_cost_tolerance_negative
5513                              );
5514 
5515       IF ( g_serial_out_tolerance ) THEN
5516          IF ( l_debug = 1 ) THEN
5517             print_debug ( 'Serial out tolerance: TRUE' );
5518          END IF;
5519       ELSE
5520          IF ( l_debug = 1 ) THEN
5521             print_debug ( 'Serial out tolerance: FALSE' );
5522          END IF;
5523       END IF;
5524 
5525       IF ( l_success = FALSE ) THEN
5526          -- mtl_serial_check.inv_qtybetwn approved our Serial_Number and
5527          -- inserted into MTL_SERIAL_NUMBERS table (If needed).
5528          -- Now we need to perform necessary adjustment transactions.
5529 
5530          IF ( l_debug = 1 ) THEN
5531             print_debug ( 'Serial count option: ' || l_serial_count_option );
5532             print_debug (    'Serial adjustment option: '
5533                           || l_serial_adjustment_option
5534                         );
5535          END IF;
5536 
5537          IF ( l_serial_adjustment_option = 1
5538               AND g_serial_out_tolerance = FALSE
5539             ) THEN
5540             -- Do a serial adjustment if possible
5541             IF ( l_debug = 1 ) THEN
5542                print_debug ( 'Trying to adjust serial' );
5543             END IF;
5544 
5545             IF ( l_serial_count_option = 2 ) THEN
5546                -- Single serial
5547                g_cc_entry.approval_condition := NULL;
5548                g_cc_entry.entry_status_code := 5;
5549                print_debug('from new_serial_number : 1');
5550                final_preupdate_logic ( );
5551             ELSIF ( l_serial_count_option = 3 ) THEN
5552                -- Multiple serial
5553                g_cc_serial_entry.approval_condition := NULL;
5554                g_serial_entry_status_code := 5;
5555                g_cc_serial_entry.pos_adjustment_qty := 1;
5556                g_cc_serial_entry.neg_adjustment_qty := 0;
5557                count_entry_status_code ( );
5558                perform_serial_adj_txn ( );
5559             END IF;
5560          ELSE
5561             -- Approvals required for serial adjustment
5562             IF ( l_debug = 1 ) THEN
5563                print_debug ( 'Approvals required for serial adjustments' );
5564             END IF;
5565 
5566             IF ( l_serial_count_option = 2 ) THEN
5567                -- Single serial
5568                g_cc_entry.approval_condition := 3;
5569                g_cc_entry.entry_status_code := 2;
5570                print_debug('from new_serial_number : 2');
5571                final_preupdate_logic ( );
5572             ELSIF ( l_serial_count_option = 3 ) THEN
5573                -- Multiple serial
5574                g_cc_serial_entry.approval_condition := 3;
5575                g_serial_entry_status_code := 2;
5576                g_cc_serial_entry.pos_adjustment_qty := 1;
5577                g_cc_serial_entry.neg_adjustment_qty := 0;
5578                count_entry_status_code ( );
5579             END IF;
5580          END IF;
5581       ELSE
5582          -- Serial exists so no adjustment is required
5583          IF ( l_serial_count_option = 2 ) THEN
5584             -- Single serial
5585             g_cc_entry.entry_status_code := 5;
5586             print_debug('from new_serial_number : 2');
5587             final_preupdate_logic ( );
5588          ELSIF ( l_serial_count_option = 3 ) THEN
5589             -- Multiple serial
5590             g_cc_serial_entry.pos_adjustment_qty := 1;
5591             g_cc_serial_entry.neg_adjustment_qty := 0;
5592             count_entry_status_code ( );
5593          END IF;
5594       END IF;
5595    END new_serial_number;
5596 
5597    /* Deletes the serial info from mtl_cc_Serial_numbers in case of an Issue transaction */
5598    PROCEDURE delete_Serial_entry(p_serial_number IN VARCHAR2, p_cc_header_id IN NUMBER, p_cycle_count_entry_id IN NUMBER) IS
5599    BEGIN
5600 
5601       DELETE FROM mtl_cc_Serial_numbers
5602        WHERE serial_number = p_serial_number
5603          AND cycle_count_entry_id IN
5604             (SELECT cycle_count_entry_id
5605                FROM mtl_cycle_count_entries
5606               WHERE cycle_Count_header_id =   p_cc_header_id
5607                 AND entry_status_code IN (1,3))
5608                    AND cycle_count_entry_id <> p_cycle_Count_entry_id;
5609 
5610    EXCEPTION
5611       WHEN OTHERS THEN
5612          print_debug('Exception while trying to delete serial number ' || g_cc_Serial_entry.serial_number);
5613    END delete_serial_entry;
5614 
5615 
5616    PROCEDURE existing_serial_number
5617    IS
5618       l_adjustment_quantity NUMBER := g_cc_entry.adjustment_quantity;
5619       l_system_present NUMBER;
5620       l_unit_status NUMBER;
5621       l_ret_value BOOLEAN := FALSE;
5622       l_adj_quantity NUMBER := g_cc_entry.adjustment_quantity;
5623       l_neg_adj_quantity NUMBER := g_cc_entry.neg_adjustment_quantity;
5624       l_serial_detail NUMBER := g_cc_entry.serial_detail;
5625       l_serial_count_option NUMBER;
5626       l_success BOOLEAN;
5627       l_issue   VARCHAR ( 1 ) := 'I';
5628       l_receipt VARCHAR ( 1 ) := 'R';
5629       l_serial_adjustment_option NUMBER;
5630       l_system_quantity NUMBER;
5631       l_primary_uom VARCHAR2 ( 3 );
5632       l_adjustment_value NUMBER;
5633       l_multiple_count NUMBER := 0;
5634       l_approval_tolerance_positive NUMBER;
5635       l_approval_tolerance_negative NUMBER;
5636       l_cost_tolerance_positive NUMBER;
5637       l_cost_tolerance_negative NUMBER;
5638       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
5639       --Bug 5186993
5640       l_automatic_recount_flag NUMBER;
5641       l_maximum_auto_recounts  NUMBER;
5642       l_days_until_late        NUMBER;
5643       l_cycle_count_entry_id NUMBER := g_cc_entry.cycle_count_entry_id;
5644       --Bug 6978840
5645       l_approval_option_code   NUMBER;
5646 
5647    BEGIN
5648       IF ( l_debug = 1 ) THEN
5649          print_debug ( '***existing_serial_number***' );
5650 
5651       END IF;
5652 
5653       -- Get the variable values
5654       SELECT NVL ( serial_count_option, 1 ),
5655              NVL ( serial_adjustment_option, 2 ),
5656              NVL ( automatic_recount_flag, 2 ),
5657              NVL ( maximum_auto_recounts, 0 ),
5658              NVL ( days_until_late , 0 ),
5659              --Bug 6978840
5660              NVL ( approval_option_code , 3)
5661       INTO   l_serial_count_option,
5662              l_serial_adjustment_option,
5663              l_automatic_recount_flag,
5664              l_maximum_auto_recounts,
5665              l_days_until_late,
5666              l_approval_option_code
5667       FROM   mtl_cycle_count_headers
5668       WHERE  cycle_count_header_id = g_cc_entry.cycle_count_header_id
5669       AND    organization_id = g_cc_entry.organization_id;
5670 
5671         IF ( l_debug = 1 ) THEN
5672            print_debug ( 'l_automatic_recount_flag = '||l_automatic_recount_flag||', g_cc_entry.number_of_counts = '||g_cc_entry.number_of_counts);
5673            print_debug ( 'l_maximum_auto_recounts = '||l_maximum_auto_recounts||', l_adjustment_quantity = '||l_adjustment_quantity);
5674          END IF;
5675 
5676          -- Bug 5186993, if automatic recount is set, check whether the adjustment has been
5677          -- counted the maximum number of times, if not setting for recount and return
5678          -- Bug 6978840 , checking if the approval option is 'If out of tolerance' and tolerance is not met
5679           -- Bug 13898802 , checking for 'review all adjustments' and adjustment_quantity not equal to zero
5680      if(l_approval_option_code = 3 and g_serial_out_tolerance = TRUE  )
5681      OR ( l_serial_adjustment_option = 2    and   nvl(g_cc_entry.adjustment_quantity,0) <> 0 ) THEN
5682          if ( l_automatic_recount_flag = 1 AND l_serial_count_option = 2
5683               AND nvl(g_cc_entry.number_of_counts, 0) <= l_maximum_auto_recounts
5684               AND nvl(l_adjustment_quantity, 0) <> 0 ) THEN
5685                 IF ( l_debug = 1 ) THEN
5686                    print_debug ( 'existing_serial_number: Setting to recount' );
5687                 END IF;
5688                 g_serial_entry_status_code := 3;
5689                 count_entry_status_code();
5690                 get_final_count_info();
5691                 g_cc_entry.count_due_date := SYSDATE + l_days_until_late;
5692                 unmark(l_cycle_count_entry_id);
5693                 return;
5694         end if;
5695       END IF;
5696       -- Set the values for g_system_present and g_unit_status
5697       -- Get the item system quantity
5698       system_quantity ( x_system_quantity => l_system_quantity );
5699 
5700 
5701       IF ( l_system_quantity <> 0 ) THEN
5702          g_system_present := 1;
5703       ELSE
5704          g_system_present := 2;
5705       END IF;
5706 
5707       IF ( g_count_quantity IS NOT NULL ) THEN
5708          IF ( g_count_quantity <> 0 ) THEN
5709             g_unit_status := 1;
5710          ELSE
5711             g_unit_status := 2;
5712          END IF;
5713       END IF;
5714 
5715 	IF ( l_serial_count_option = 3 ) THEN
5716 		-- Need to get the value for system present manually
5717 		print_debug('getting the value for the sytem present manually');
5718 
5719 		IF ( g_cc_entry.parent_lpn_id IS NULL ) THEN
5720 			print_debug('parent_lpn_id is null');
5721 			-- No LPN so we want to check if serial is present in the system
5722 			-- with the given sub and loc so that if there is a discrepancy,
5723 			-- it will pick it up and issue out and receive the serial into
5724 			-- the sub/loc where it was found during the counting
5725 
5726 			-- Bug 13796753
5727 			SELECT COUNT(*)
5728 			INTO l_multiple_count
5729 			FROM
5730 				(SELECT serial_number,
5731 					inventory_item_id,
5732 					current_organization_id
5733 				 FROM mtl_serial_numbers
5734 				 WHERE serial_number = g_cc_serial_entry.serial_number
5735 				 AND inventory_item_id = g_cc_entry.inventory_item_id
5736 				 AND current_organization_id = g_cc_entry.organization_id
5737 				 AND current_subinventory_code = g_cc_entry.subinventory
5738 				 AND NVL(lot_number, 'XX') = NVL(g_cc_entry.lot_number, 'XX')
5739 				 AND NVL(revision, 'XXX') = NVL(g_cc_entry.revision, 'XXX')
5740 				 AND NVL(current_locator_id, -2 ) = NVL(g_cc_entry.locator_id, -2)
5741 				 AND current_status =3
5742 				 AND lpn_id IS NULL
5743 				) msn
5744 			WHERE is_serial_loaded(msn.current_organization_id, msn.inventory_item_id, msn.serial_number, NULL) = 2;
5745 
5746 		ELSE
5747 
5748 			print_debug('parent_lpn_id  ' || g_cc_entry.parent_lpn_id);
5749 			-- If the serial is inside an LPN but the LPN is discrepant, then
5750 			-- we do not want the serial to be considered as missing here since
5751 			-- we will do a sub transfer for the discrepant LPN which will
5752 			-- also move the serial packed within it
5753 
5754 			-- Bug 13796753
5755 			SELECT COUNT(*)
5756 			INTO l_multiple_count
5757 			FROM
5758 				(SELECT serial_number,
5759 					inventory_item_id,
5760 					current_organization_id,
5761 					lpn_id
5762 				 FROM mtl_serial_numbers
5763 				 WHERE serial_number = g_cc_serial_entry.serial_number
5764 				 AND inventory_item_id = g_cc_entry.inventory_item_id
5765 				 AND current_organization_id = g_cc_entry.organization_id
5766 				 AND NVL(lot_number, 'XX') = NVL(g_cc_entry.lot_number, 'XX')
5767 				 AND NVL(revision, 'XXX') = NVL(g_cc_entry.revision, 'XXX')
5768 				 AND current_status = 3
5769 				 AND lpn_id = g_cc_entry.parent_lpn_id
5770 				) msn
5771 			WHERE is_serial_loaded(msn.current_organization_id, msn.inventory_item_id, msn.serial_number, msn.lpn_id) = 2;
5772 		END IF;
5773 
5774 		print_debug('l_multiple_count ' ||  l_multiple_count);
5775 
5776 		IF ( l_multiple_count <> 0 ) THEN
5777 			g_system_present := 1;
5778 		ELSE
5779 			g_system_present := 2;
5780 		END IF;
5781 
5782 		g_unit_status := g_cc_serial_entry.unit_status_current;
5783 
5784 	END IF;
5785 
5786       IF ( l_serial_count_option = 3 ) THEN
5787          IF ( l_debug = 1 ) THEN
5788             print_debug ( 'Serial Number: ' || g_cc_serial_entry.serial_number
5789                         );
5790          END IF;
5791       ELSIF ( l_serial_count_option = 2 ) THEN
5792          IF ( l_debug = 1 ) THEN
5793             print_debug ( 'Serial Number: ' || g_cc_entry.serial_number );
5794          END IF;
5795       END IF;
5796 
5797       IF ( l_debug = 1 ) THEN
5798          print_debug ( 'System Present: ' || g_system_present );
5799          print_debug ( 'Unit Status: ' || g_unit_status );
5800       END IF;
5801 
5802       l_system_present := g_system_present;
5803       l_unit_status := g_unit_status;
5804 
5805       IF ( l_debug = 1 ) THEN
5806            print_debug ( 'l_system_present: ' || l_system_present );
5807            print_debug ( 'l_unit_status: ' || l_unit_status );
5808          END IF;
5809 
5810       -- Get the item primary uom code
5811       -- Bug 13652899
5812       IF (inv_cache.set_item_rec(p_organization_id => g_cc_entry.organization_id, p_item_id => g_cc_entry.inventory_item_id))
5813       THEN
5814       	l_primary_uom := inv_cache.item_rec.primary_uom_code;
5815       END IF;
5816 
5817 
5818         IF ( l_debug = 1 ) THEN
5819            print_debug ( 'l_primary_uom: ' || l_primary_uom );
5820            print_debug ( 'g_count_uom: ' || g_count_uom );
5821            print_debug ( 'l_system_quantity: ' || l_system_quantity );
5822            print_debug ( 'g_count_quantity: ' || g_count_quantity );
5823 
5824          END IF;
5825 
5826       -- Convert the system quantity into the primary uom
5827 
5828          l_system_quantity :=
5829          inv_convert.inv_um_convert ( g_cc_entry.inventory_item_id,
5830                                       6,
5831                                       l_system_quantity,
5832                                       g_count_uom,
5833                                       l_primary_uom,
5834                                       NULL,
5835                                       NULL
5836                                     );
5837          IF ( l_debug = 1 ) THEN
5838             print_debug ( 'l_system_quantity: ' || l_system_quantity );
5839             print_debug ( 'g_count_quantity: ' || g_count_quantity );
5840          END IF;
5841 
5842       -- Get and set the adjustment quantity and adjustment value
5843       IF ( l_serial_count_option IN ( 2, 3 ) ) THEN
5844          l_adjustment_quantity := g_count_quantity - l_system_quantity;
5845          g_cc_entry.adjustment_quantity := l_adjustment_quantity;
5846          --Bug# 3640622 Commented out the code.
5847          --g_cc_entry.adjustment_date := SYSDATE;
5848          value_variance ( x_value_variance => l_adjustment_value );
5849          g_cc_entry.adjustment_amount := l_adjustment_value;
5850       END IF;
5851 
5852       -- Calculate if the serial is out of tolerance or not
5853       get_tolerances ( pre_approve_flag    => 'SERIAL',
5854                        x_approval_tolerance_positive => l_approval_tolerance_positive,
5855                        x_approval_tolerance_negative => l_approval_tolerance_negative,
5856                        x_cost_tolerance_positive => l_cost_tolerance_positive,
5857                        x_cost_tolerance_negative => l_cost_tolerance_negative
5858                      );
5859       serial_tolerance_logic ( p_serial_adj_qty    => l_adjustment_quantity,
5860                                 p_app_tol_pos       => l_approval_tolerance_positive,
5861                                p_app_tol_neg       => l_approval_tolerance_negative,
5862                                p_cost_tol_pos      => l_cost_tolerance_positive,
5863                                p_cost_tol_neg      => l_cost_tolerance_negative
5864                              );
5865 
5866       IF ( g_serial_out_tolerance ) THEN
5867          IF ( l_debug = 1 ) THEN
5868             print_debug ( 'Serial out tolerance: TRUE' );
5869          END IF;
5870       ELSE
5871          IF ( l_debug = 1 ) THEN
5872             print_debug ( 'Serial out tolerance: FALSE' );
5873          END IF;
5874       END IF;
5875 
5876       -- Multiple serial count option
5877       IF ( l_serial_count_option = 3 ) THEN
5878          IF ( l_debug = 1 ) THEN
5879             print_debug ( 'Multiple serial: existing_serial_number' );
5880          END IF;
5881 
5882          -- If the s/n is shown on the system, but was counted as missing
5883          -- i.e. l_system_present = 1 and l_unit_status = 2, then execute.
5884          -- The serial no. will not show up in our cycle count if it does
5885          -- not exist in the system.
5886          IF (  ( l_system_present = 1 ) AND ( l_unit_status = 2 ) ) THEN
5887             IF ( l_debug = 1 ) THEN
5888                print_debug (    'Missing serial is: '
5889                              || g_cc_serial_entry.serial_number
5890                            );
5891                print_debug (    'serial adjustment option is: '
5892                              || l_serial_adjustment_option
5893                            );
5894             END IF;
5895 
5896             IF (     l_serial_adjustment_option = 1
5897                  AND g_serial_out_tolerance = FALSE
5898                ) THEN
5899                IF ( l_debug = 1 ) THEN
5900                   print_debug ( 'Trying to make adjustment automatically' );
5901                END IF;
5902 
5903                l_success   := check_serial_number_location ( l_issue );
5904                g_cc_serial_entry.pos_adjustment_qty := 0;
5905                g_cc_serial_entry.neg_adjustment_qty := 1;
5906 
5907                IF ( l_success ) THEN
5908                   IF ( l_debug = 1 ) THEN
5909                      print_debug ( 'check_serial_number_location: TRUE' );
5910                   END IF;
5911                ELSE
5912                   IF ( l_debug = 1 ) THEN
5913                      print_debug ( 'check_serial_number_location: FALSE' );
5914                   END IF;
5915                END IF;
5916 
5917                IF ( l_success = FALSE ) THEN
5918                   g_serial_entry_status_code := 5;
5919                   g_cc_serial_entry.approval_condition := NULL;
5920                END IF;
5921 
5922                count_entry_status_code ( );
5923                perform_serial_adj_txn ( );
5924                -- If serial adjustment option is "review all adjustments", then send to approval
5925             ELSE
5926                IF ( l_debug = 1 ) THEN
5927                   print_debug ( 'Need to review this adjustment' );
5928                END IF;
5929 
5930                l_success   := check_serial_number_location ( l_issue );
5931                g_serial_entry_status_code := 2;
5932                g_cc_serial_entry.approval_condition := 3;
5933                g_cc_serial_entry.pos_adjustment_qty := 0;
5934                g_cc_serial_entry.neg_adjustment_qty := 1;
5935                count_entry_status_code ( );
5936             END IF;
5937          -- If the s/n is missing on the system, but was counted as present:
5938 
5939          -- This part will only be executed if after CC generation some other transaction issued
5940          -- out the Serial Number but the counter finds it at the count serial location.
5941          -- The condition where serial number never existed in the system will be handled by
5942          -- 'NEW' or 'INSERT' record condition
5943          ELSIF (  ( l_system_present = 2 ) AND ( l_unit_status = 1 ) ) THEN
5944             IF ( l_debug = 1 ) THEN
5945                print_debug (    'Serial is counted as present: '
5946                              || g_cc_serial_entry.serial_number
5947                            );
5948             END IF;
5949 
5950             IF (     l_serial_adjustment_option = 1
5951                  AND g_serial_out_tolerance = FALSE
5952                ) THEN
5953                IF ( l_debug = 1 ) THEN
5954                   print_debug ( 'Trying to make adjustment automatically' );
5955                END IF;
5956 
5957                l_success   := check_serial_number_location ( l_receipt );
5958                g_cc_serial_entry.pos_adjustment_qty := 1;
5959                g_cc_serial_entry.neg_adjustment_qty := 0;
5960 
5961                IF ( l_success ) THEN
5962                   IF ( l_debug = 1 ) THEN
5963                      print_debug ( 'check_serial_number_location: TRUE' );
5964                   END IF;
5965                ELSE
5966                   IF ( l_debug = 1 ) THEN
5967                      print_debug ( 'check_serial_number_location: FALSE' );
5968                   END IF;
5969                END IF;
5970 
5971                IF ( l_success = FALSE ) THEN
5972                   g_serial_entry_status_code := 5;
5973                   g_cc_serial_entry.approval_condition := NULL;
5974                END IF;
5975 
5976                count_entry_status_code ( );
5977                perform_serial_adj_txn ( );
5978             -- If serial adjustment option is "review all adjustments", then send to approval
5979             ELSE
5980                IF ( l_debug = 1 ) THEN
5981                   print_debug ( 'Need to review this adjustment' );
5982                END IF;
5983 
5984                l_success   := check_serial_number_location ( l_receipt );
5985                g_serial_entry_status_code := 2;
5986                g_cc_serial_entry.approval_condition := 3;
5987                g_cc_serial_entry.pos_adjustment_qty := 1;
5988                g_cc_serial_entry.neg_adjustment_qty := 0;
5989                count_entry_status_code ( );
5990             END IF;
5991          /* All other cases considered as no problem, no adjustment required */
5992          ELSE
5993             IF ( l_debug = 1 ) THEN
5994                print_debug ( 'All other cases with no adjustments' );
5995             END IF;
5996 
5997             g_serial_entry_status_code := 5;
5998             g_cc_entry.entry_status_code := 5;   /* Added for bug#4926279*/
5999             g_cc_serial_entry.pos_adjustment_qty := NULL;
6000             g_cc_serial_entry.neg_adjustment_qty := NULL;
6001             g_cc_serial_entry.approval_condition := NULL;
6002             count_entry_status_code ( );
6003          END IF;
6004       -- Single serial count option
6005       ELSIF ( l_serial_count_option = 2 ) THEN
6006          IF ( l_debug = 1 ) THEN
6007             print_debug ( 'Single serial: existing_serial_number' );
6008          END IF;
6009 
6010          /* If the s/n is shown on the system, but was counted as missing */
6011          IF ( l_adjustment_quantity = -1 ) THEN
6012             IF ( l_debug = 1 ) THEN
6013                print_debug ( 'Serial is missing: ' || g_cc_entry.serial_number
6014                            );
6015             END IF;
6016 
6017             IF ( l_serial_adjustment_option = 1 ) THEN
6018                IF ( l_debug = 1 ) THEN
6019                   print_debug ( 'Trying to adjust the serial' );
6020                END IF;
6021 
6022                l_success   := check_serial_number_location ( l_issue );
6023                g_cc_entry.neg_adjustment_quantity := 1;
6024 
6025                IF ( l_success = FALSE ) THEN
6026                   IF ( l_debug = 1 ) THEN
6027                      print_debug ( 'check_serial_number_location returned: FALSE'
6028                                  );
6029                   END IF;
6030 
6031                   g_serial_entry_status_code := 5;
6032                   g_cc_entry.entry_status_code := 5;
6033                   g_cc_entry.approval_condition := NULL;
6034                ELSE
6035                   IF ( l_debug = 1 ) THEN
6036                      print_debug ( 'check_serial_number_location returned: TRUE'
6037                                  );
6038                   END IF;
6039                END IF;
6040                print_debug('from existing_Serial_number : 1');
6041                final_preupdate_logic ( );
6042             /* If serial adjustment option is "review all adjustments", then send to approval */
6043             ELSIF ( l_serial_adjustment_option = 2 ) THEN
6044                IF ( l_debug = 1 ) THEN
6045                   print_debug ( 'Serial adjustment needs to be reviewed' );
6046                END IF;
6047 
6048                l_success   := check_serial_number_location ( l_issue );
6049 
6050                IF ( l_success = FALSE ) THEN
6051                   IF ( l_debug = 1 ) THEN
6052                      print_debug ( 'check_serial_number_location returned: FALSE'
6053                                  );
6054                   END IF;
6055                ELSE
6056                   IF ( l_debug = 1 ) THEN
6057                      print_debug ( 'check_serial_number_location returned: TRUE'
6058                                  );
6059                   END IF;
6060                END IF;
6061 
6062                g_serial_entry_status_code := 2;
6063                g_cc_entry.entry_status_code := 2;
6064                g_cc_entry.neg_adjustment_quantity := 1;
6065                g_cc_entry.approval_condition := 3;
6066                print_debug('from existing_Serial_number : 2');
6067                final_preupdate_logic ( );
6068             END IF;
6069          /* IF the s/n is missing on the system, but was count as present */
6070          ELSIF ( l_adjustment_quantity = 1 ) THEN
6071             IF ( l_debug = 1 ) THEN
6072                print_debug ( 'Serial is found: ' || g_cc_entry.serial_number );
6073             END IF;
6074 
6075             IF ( l_serial_adjustment_option = 1 ) THEN
6076                IF ( l_debug = 1 ) THEN
6077                   print_debug ( 'Trying to adjust the serial' );
6078                END IF;
6079 
6080                l_success   := check_serial_number_location ( l_receipt );
6081                g_cc_entry.adjustment_quantity := 1;
6082 
6083                IF ( l_success = FALSE ) THEN
6084                   IF ( l_debug = 1 ) THEN
6085                      print_debug ( 'check_serial_number_location returned: FALSE'
6086                                  );
6087                   END IF;
6088 
6089                   g_serial_entry_status_code := 5;
6090                   g_cc_entry.entry_status_code := 5;
6091                   g_cc_entry.approval_condition := NULL;
6092                ELSE
6093                   IF ( l_debug = 1 ) THEN
6094                      print_debug ( 'check_serial_number_location returned: TRUE'
6095                                  );
6096                   END IF;
6097                END IF;
6098                print_debug('from existing_Serial_number : 3');
6099                final_preupdate_logic ( );
6100             /* if serial adjustment option is "review all adjustments", then send to approval */
6101             ELSIF ( l_serial_adjustment_option = 2 ) THEN
6102                IF ( l_debug = 1 ) THEN
6103                   print_debug ( 'Serial adjustment needs to be reviewed' );
6104                END IF;
6105 
6106                l_success   := check_serial_number_location ( l_receipt );
6107 
6108                IF ( l_success = FALSE ) THEN
6109                   IF ( l_debug = 1 ) THEN
6110                      print_debug ( 'check_serial_number_location returned: FALSE'
6111                                  );
6112                   END IF;
6113                ELSE
6114                   IF ( l_debug = 1 ) THEN
6115                      print_debug ( 'check_serial_number_location returned: TRUE'
6116                                  );
6117                   END IF;
6118                END IF;
6119 
6120                g_serial_entry_status_code := 2;
6121                g_cc_entry.entry_status_code := 2;
6122                g_cc_entry.adjustment_quantity := 1;
6123                g_cc_entry.approval_condition := 3;
6124                print_debug('from existing_Serial_number : 4');
6125                final_preupdate_logic ( );
6126             END IF;
6127          /* all other cases considered as no problem, no adjustment required */
6128          ELSE
6129             IF ( l_debug = 1 ) THEN
6130                print_debug ( 'No serial adjustment needs to be made' );
6131             END IF;
6132 
6133             g_serial_entry_status_code := 5;
6134             g_cc_entry.entry_status_code := 5;
6135             g_cc_entry.neg_adjustment_quantity := NULL;
6136             g_cc_entry.approval_condition := NULL;
6137             print_debug('from existing_Serial_number : 5');
6138             final_preupdate_logic ( );
6139          END IF;
6140       END IF;
6141    END existing_serial_number;
6142 
6143 -- Function check_serial_number_location
6144 --
6145 -- This function checks to see if the Serial Number where this serial number is
6146 -- located in the system.
6147    FUNCTION check_serial_number_location (
6148       issue_receipt VARCHAR2
6149    )
6150       RETURN BOOLEAN
6151    IS
6152       u1        VARCHAR2 ( 30 );
6153       u2        VARCHAR2 ( 30 );
6154       u3        NUMBER;
6155       u4        VARCHAR2 ( 30 );
6156       u5        VARCHAR2 ( 30 );
6157       u6        VARCHAR2 ( 30 );
6158       u7        VARCHAR2 ( 3 );
6159       u8        VARCHAR2 ( 30 );
6160       u9        VARCHAR2 ( 30 );
6161       u10       VARCHAR2 ( 30 );
6162       u11       VARCHAR2 ( 3 );
6163       u12       VARCHAR2 ( 30 );
6164       u13       VARCHAR2 ( 10 );
6165       u14       VARCHAR2 ( 30 );
6166       u15       VARCHAR2 ( 1 );
6167       serial_count NUMBER := 0;
6168       l_serial_count_option NUMBER;
6169       l_serial_number_type NUMBER := 1;                    /* Default value */
6170       l_org_id  NUMBER := g_cc_entry.organization_id;
6171       l_serial_number VARCHAR2 ( 30 ) := g_cc_entry.serial_number;
6172       l_serial_detail NUMBER;
6173       l_serial_discrepancy NUMBER;
6174       l_item_id NUMBER := g_cc_entry.inventory_item_id;
6175       l_subinv  VARCHAR2 ( 10 ) := g_cc_entry.subinventory;
6176       l_revision VARCHAR2 ( 3 ) := g_cc_entry.revision;
6177       l_current_status NUMBER;
6178       l_ret_value BOOLEAN := FALSE;
6179       l_system_quantity NUMBER;
6180       l_dummy_quantity NUMBER;
6181       l_serial_number_control_code NUMBER;
6182       l_return_status VARCHAR2 ( 300 );
6183       l_msg_count NUMBER;
6184       l_msg_data VARCHAR2 ( 300 );
6185       l_error_code NUMBER;
6186       l_quantity NUMBER;
6187       l_prefix  VARCHAR2 ( 240 );
6188       -- Variables used for serial discrepancies
6189       l_msn_subinv VARCHAR2 ( 10 );
6190       l_msn_locator_id NUMBER;
6191       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
6192    BEGIN
6193       IF ( l_debug = 1 ) THEN
6194          print_debug ( '***check_serial_number_location***' );
6195       END IF;
6196 
6197       -- Get the variable values
6198       SELECT NVL ( serial_count_option, 1 ),
6199              NVL ( serial_detail_option, 1 ),
6200              NVL ( serial_discrepancy_option, 2 )
6201       INTO   l_serial_count_option,
6202              l_serial_detail,
6203              l_serial_discrepancy
6204       FROM   mtl_cycle_count_headers
6205       WHERE  cycle_count_header_id = g_cc_entry.cycle_count_header_id
6206       AND    organization_id = g_cc_entry.organization_id;
6207 
6208       -- Get the serial number control code
6209       SELECT serial_number_control_code
6210       INTO   l_serial_number_control_code
6211       FROM   mtl_system_items
6212       WHERE  inventory_item_id = g_cc_entry.inventory_item_id
6213       AND    organization_id = g_cc_entry.organization_id;
6214 
6215       -- Get the item system quantity
6216       system_quantity ( x_system_quantity => l_system_quantity );
6217 
6218       IF (      ( l_system_quantity ) = 1
6219            AND ( g_count_quantity = 1 )
6220            AND ( l_serial_count_option = 2 )
6221          ) THEN
6222          l_ret_value := TRUE;
6223       ELSE
6224          IF ( l_org_id IS NOT NULL ) THEN
6225             SELECT SERIAL_NUMBER_TYPE
6226             INTO   l_serial_number_type
6227             FROM   MTL_PARAMETERS
6228             WHERE  ORGANIZATION_ID = l_org_id;
6229          END IF;
6230 
6231          IF ( l_debug = 1 ) THEN
6232             print_debug (    'Got the serial number type: '
6233                           || l_serial_number_type
6234                         );
6235          END IF;
6236 
6237          IF ( l_serial_count_option = 3 ) THEN
6238             u1          := g_cc_serial_entry.serial_number;
6239             u2          := g_cc_serial_entry.serial_number;
6240             l_serial_number := g_cc_serial_entry.serial_number;
6241          ELSE
6242             u1          := g_cc_entry.serial_number;
6243             u2          := g_cc_entry.serial_number;
6244             l_serial_number := g_cc_entry.serial_number;
6245          END IF;
6246 
6247          u3          := g_cc_entry.system_quantity_current;
6248          u4          := NULL;
6249          u5          := g_cc_entry.inventory_item_id;
6250          u6          := g_cc_entry.organization_id;
6251          u7          := l_serial_number_type;
6252          u8          := g_cc_entry.cycle_count_entry_id;
6253          u9          := TO_CHAR ( 9 );
6254          u10         := l_serial_number_control_code;
6255          u11         := g_cc_entry.revision;
6256          u12         := g_cc_entry.lot_number;
6257          u13         := g_cc_entry.subinventory;
6258          u14         := g_cc_entry.locator_id;
6259          u15         := issue_receipt;
6260 
6261          -- Serial was found as missing so need to issue it out.
6262          IF ( issue_receipt = 'I' ) THEN
6263             IF ( l_debug = 1 ) THEN
6264                print_debug ( 'Need to issue out the serial so reset the sub and loc'
6265                            );
6266             END IF;
6267 
6268             SELECT CURRENT_SUBINVENTORY_CODE,
6269                    NVL ( CURRENT_LOCATOR_ID, 0 )
6270             INTO   l_msn_subinv,
6271                    l_msn_locator_id
6272             FROM   MTL_SERIAL_NUMBERS
6273             WHERE  SERIAL_NUMBER = u1
6274             AND    INVENTORY_ITEM_ID = g_cc_entry.inventory_item_id
6275             AND    CURRENT_ORGANIZATION_ID = g_cc_entry.organization_id;
6276 
6277             -- Reset these values just for calling the validation procedure
6278             u13         := l_msn_subinv;
6279             u14         := l_msn_locator_id;
6280          END IF;
6281 
6282          IF ( l_debug = 1 ) THEN
6283             print_debug ( 'Calling the package mtl_serial_check.inv_qtybetwn' );
6284             print_debug ( 'Inputs to API are: ' );
6285             print_debug ( 'p_from_serial_number: =========> ' || u1 );
6286             print_debug ( 'p_to_serial_number: ===========> ' || u2 );
6287             print_debug ( 'p_item_id: ====================> ' || u5 );
6288             print_debug ( 'p_organization_id: ============> ' || u6 );
6289             print_debug ( 'p_serial_number_type: =========> ' || u7 );
6290             print_debug ( 'p_transaction_action_id: ======> ' || u8 );
6291             print_debug ( 'p_transaction_source_type_id: => ' || u9 );
6292             print_debug ( 'p_serial_control: =============> ' || u10 );
6293             print_debug ( 'p_revision: ===================> ' || u11 );
6294             print_debug ( 'p_lot_number: =================> ' || u12 );
6295             print_debug ( 'p_subinventory: ===============> ' || u13 );
6296             print_debug ( 'p_locator_id: =================> ' || u14 );
6297             print_debug ( 'p_receipt_issue_flag: =========> ' || u15 );
6298          END IF;
6299 
6300          -- Call package in file INVSERLB.PLS instead of a USER_EXIT
6301          mtl_serial_check.inv_qtybetwn ( p_api_version       => 0.9,
6302                                          x_return_status     => l_return_status,
6303                                          x_msg_count         => l_msg_count,
6304                                          x_msg_data          => l_msg_data,
6305                                          x_errorcode         => l_error_code,
6306                                          p_from_serial_number => u1,
6307                                          p_to_serial_number  => u2,
6308                                          x_quantity          => l_quantity,
6309                                          x_prefix            => l_prefix,
6310                                          p_item_id           => u5,
6311                                          p_organization_id   => u6,
6312                                          p_serial_number_type => u7,
6313                                          p_transaction_action_id => u8,
6314                                          p_transaction_source_type_id => u9,
6315                                          p_serial_control    => u10,
6316                                          p_revision          => u11,
6317                                          p_lot_number        => u12,
6318                                          p_subinventory      => u13,
6319                                          p_locator_id        => u14,
6320                                          p_receipt_issue_flag => u15
6321                                        );
6322 
6323          IF ( l_debug = 1 ) THEN
6324             print_debug ( 'Error code is: ' || l_error_code );
6325             print_debug ( 'Issue receipt: ' || issue_receipt );
6326          END IF;
6327 
6328          IF ( l_error_code <> 0 AND issue_receipt = 'R' ) THEN
6329             IF ( l_debug = 1 ) THEN
6330                print_debug ( 'Serial number: ' || l_serial_number );
6331                print_debug ( 'Item ID: ' || l_item_id );
6332                print_debug ( 'Organization ID: ' || l_org_id );
6333             END IF;
6334 
6335             BEGIN
6336                SELECT 1,
6337                       current_status
6338                INTO   serial_count,
6339                       l_current_status
6340                FROM   MTL_SERIAL_NUMBERS
6341                WHERE  SERIAL_NUMBER = l_serial_number
6342                AND    INVENTORY_ITEM_ID = l_item_id
6343                AND    CURRENT_ORGANIZATION_ID = l_org_id
6344                AND    CURRENT_STATUS IN ( 1, 3 );
6345             EXCEPTION
6346                WHEN OTHERS THEN
6347                   IF ( l_debug = 1 ) THEN
6348                      print_debug ( 'SQL Error: ' || SQLCODE );
6349                   END IF;
6350             END;
6351 
6352             IF ( l_debug = 1 ) THEN
6353                print_debug ( 'serial_count: ' || serial_count );
6354                print_debug ( 'serial_discrepancy: ' || l_serial_discrepancy );
6355                print_debug ( 'serial count option: ' || l_serial_count_option );
6356             END IF;
6357 
6358             IF ( serial_count = 1 AND l_serial_discrepancy = 1 ) THEN
6359                IF ( l_serial_count_option = 2 ) THEN
6360                   g_cc_entry.approval_condition := 1;
6361                   g_cc_entry.entry_status_code := 2;
6362 
6363                   IF ( l_debug = 1 ) THEN
6364                      print_debug (    'entry_status_code: '
6365                                    || g_cc_entry.entry_status_code
6366                                  );
6367                   END IF;
6368                ELSIF ( l_serial_count_option = 3 ) THEN
6369                   g_serial_entry_status_code := 2;
6370                   g_cc_serial_entry.approval_condition := 1;
6371 
6372                   IF ( l_debug = 1 ) THEN
6373                      print_debug (    'entry_status_code: '
6374                                    || g_cc_entry.entry_status_code
6375                                  );
6376                   END IF;
6377                END IF;
6378 
6379                FND_MESSAGE.SET_NAME ( 'INV', 'INV_CC_SERIAL_MULTI_TRANSACT2' );
6380                FND_MESSAGE.SET_TOKEN ( 'SERIAL', l_serial_number );
6381                FND_MSG_PUB.ADD;
6382 
6383                IF ( l_debug = 1 ) THEN
6384                   print_debug ( 'l_current_status: ' || l_current_status );
6385                END IF;
6386 
6387                IF ( l_current_status = 1 ) THEN
6388                   l_ret_value := TRUE;
6389                ELSE
6390                   l_ret_value := FALSE;
6391                END IF;
6392             ELSE
6393                FND_MESSAGE.SET_NAME ( 'INV', 'INV_CC_SERIAL_DISCREPANCY' );
6394                FND_MESSAGE.SET_TOKEN ( 'SERIAL', l_serial_number );
6395                FND_MSG_PUB.ADD;
6396 
6397                IF ( l_serial_count_option = 3 ) THEN
6398                   --   clear_block(NO_VALIDATE);
6399                   --   go_block('CC_ENTRIES');
6400                   --   hide_window('CC_SERIAL_ENTRY');
6401                   IF ( l_debug = 1 ) THEN
6402                      print_debug ( 'Multiple serial error' );
6403                   END IF;
6404 
6405                   ROLLBACK TO save_serial_detail;
6406                END IF;
6407 
6408                RAISE FND_API.G_EXC_ERROR;
6409             END IF;
6410          ELSIF ( l_error_code <> 0 ) THEN
6411             FND_MESSAGE.SET_NAME ( 'INV', 'INV_CC_SERIAL_DISCREPANCY' );
6412             FND_MESSAGE.SET_TOKEN ( 'SERIAL', l_serial_number );
6413             FND_MSG_PUB.ADD;
6414 
6415             IF ( l_serial_count_option = 3 ) THEN
6416                IF ( l_debug = 1 ) THEN
6417                   print_debug ( 'Multiple serial error' );
6418                END IF;
6419 
6420                ROLLBACK TO save_serial_detail;
6421             END IF;
6422 
6423             RAISE FND_API.G_EXC_ERROR;
6424          END IF;
6425       END IF;
6426 
6427       RETURN ( l_ret_value );
6428    END check_serial_number_location;
6429 
6430    PROCEDURE perform_serial_adj_txn
6431    IS
6432       l_entry_status_code NUMBER := g_serial_entry_status_code;
6433       l_number_of_counts NUMBER := g_cc_serial_entry.number_of_counts;
6434       l_adjustment_quantity NUMBER;
6435       l_transaction_id NUMBER;
6436       l_org_id  NUMBER := g_cc_entry.organization_id;
6437       l_cc_header_id NUMBER := g_cc_entry.cycle_count_header_id;
6438       l_item_id NUMBER := g_cc_entry.inventory_item_id;
6439       l_sub     VARCHAR2 ( 10 ) := g_cc_entry.subinventory;
6440       l_txn_quantity NUMBER;
6441       l_txn_uom VARCHAR2 ( 3 ) := g_count_uom;
6442       l_lot_num VARCHAR2 ( 80 ) := g_cc_entry.lot_number; --Increased the variable size from 30 to 80 for Bug 8717805
6443       l_lot_exp_date DATE;
6444       l_rev     VARCHAR2 ( 3 ) := g_cc_entry.revision;
6445       l_locator_id NUMBER := g_cc_entry.locator_id;
6446       l_txn_ref VARCHAR2 ( 240 ) := g_cc_entry.reference_current;
6447       l_reason_id NUMBER := g_cc_entry.transaction_reason_id;
6448       l_txn_header_id NUMBER := NVL ( g_txn_header_id, -2 );
6449       l_txn_temp_id NUMBER;
6450       l_user_id NUMBER := g_user_id;
6451       l_login_id NUMBER := g_login_id;
6452       l_txn_proc_mode NUMBER := g_txn_proc_mode;
6453       l_txn_acct_id NUMBER;
6454       l_success_flag NUMBER;
6455       l_p_uom_qty NUMBER;
6456       l_cycle_count_entry_id NUMBER := g_cc_entry.cycle_count_entry_id;
6457       l_from_uom VARCHAR2 ( 3 );
6458       l_to_uom  VARCHAR2 ( 3 );
6459       l_txn_date DATE := SYSDATE;
6460       l_serial_number VARCHAR2 ( 30 ) := g_cc_serial_entry.serial_number;
6461       l_serial_prefix VARCHAR2 ( 30 );
6462       l_lpn_id  NUMBER := g_cc_entry.parent_lpn_id;
6463       l_cost_group_id NUMBER := g_cc_entry.cost_group_id;
6464       -- Variables used for handling serial discrepancies
6465       l_msn_subinv VARCHAR2 ( 10 );
6466       l_msn_lot_number VARCHAR2 ( 30 );
6467       l_msn_locator_id NUMBER;
6468       l_msn_revision VARCHAR2 ( 3 );
6469       l_msn_lpn_id NUMBER;
6470       l_current_status NUMBER;
6471       l_adj_qty NUMBER;
6472       l_temp_subinv VARCHAR2 ( 10 );
6473       l_temp_locator_id NUMBER;
6474       l_item_name VARCHAR2 ( 100 );
6475       -- Bug # 2743382
6476 
6477       v_available_quantity NUMBER;
6478       v_entry_status_code NUMBER;
6479       x_return_status VARCHAR2 ( 10 );
6480       x_qoh     NUMBER;
6481       x_att     NUMBER;
6482       v_ser_code NUMBER;
6483       v_lot_code NUMBER;
6484       v_rev_code NUMBER;
6485       v_is_ser_controlled BOOLEAN := FALSE;
6486       v_is_lot_controlled BOOLEAN := FALSE;
6487       v_is_rev_controlled BOOLEAN := FALSE;
6488       l_rqoh    NUMBER;
6489       l_qr      NUMBER;
6490       l_qs      NUMBER;
6491       l_atr     NUMBER;
6492       l_msg_count NUMBER;
6493       l_msg_data VARCHAR2 ( 2000 );
6494       l_parent_lpn_id NUMBER;
6495       l_neg_inv_rcpt_code NUMBER;
6496       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
6497       l_allow_neg_onhand_prof_val NUMBER;   -- 4870490
6498    BEGIN
6499       IF ( l_debug = 1 ) THEN
6500          print_debug ( '***perform_serial_adj_txn***' );
6501       END IF;
6502 
6503         /*Bug 5704910*/
6504         --Clearing the quantity tree cache
6505         inv_quantity_tree_pub.clear_quantity_cache;
6506 
6507       --Bug# 3640622
6508       g_cc_entry.adjustment_date := SYSDATE;
6509 
6510       -- Get the item primary uom code
6511       SELECT primary_uom_code
6512       INTO   l_to_uom
6513       FROM   MTL_SYSTEM_ITEMS
6514       WHERE  inventory_item_id = g_cc_entry.inventory_item_id
6515       AND    organization_id = g_cc_entry.organization_id;
6516 
6517       -- Get the cycle count header inventory adjustment account
6518       SELECT NVL ( inventory_adjustment_account, -1 )
6519       INTO   l_txn_acct_id
6520       FROM   mtl_cycle_count_headers
6521       WHERE  cycle_count_header_id = g_cc_entry.cycle_count_header_id
6522       AND    organization_id = g_cc_entry.organization_id;
6523 
6524       SELECT concatenated_segments
6525       INTO   l_item_name
6526       FROM   mtl_system_items_kfv
6527       WHERE  inventory_item_id = l_item_id AND organization_id = l_org_id;
6528 
6529       l_from_uom  := g_count_uom;
6530       l_txn_uom   := l_from_uom;
6531 
6532       IF ( l_txn_date IS NULL ) THEN
6533          l_txn_date  := SYSDATE;
6534       END IF;
6535 
6536       IF ( l_debug = 1 ) THEN
6537          print_debug (    'Serial: '
6538                        || g_cc_serial_entry.serial_number
6539                        || ' Pos adj qty: '
6540                        || g_cc_serial_entry.pos_adjustment_qty
6541                        || ' Neg adj qty: '
6542                        || g_cc_serial_entry.neg_adjustment_qty
6543                      );
6544       END IF;
6545 
6546       IF ( g_cc_serial_entry.pos_adjustment_qty = 1 ) THEN
6547          l_adjustment_quantity := 1;
6548          l_txn_quantity := 1;
6549       ELSIF ( g_cc_serial_entry.neg_adjustment_qty = 1 ) THEN
6550          l_adjustment_quantity := -1;
6551          l_txn_quantity := -1;
6552       ELSE
6553          l_adjustment_quantity := 0;
6554          l_txn_quantity := 0;
6555       END IF;
6556 
6557       IF ( l_debug = 1 ) THEN
6558          print_debug (    'Multiple serial entry status code: '
6559                        || l_entry_status_code
6560                      );
6561          print_debug ( 'Adjustment quantity: ' || l_adjustment_quantity );
6562       END IF;
6563 
6564       IF ( l_entry_status_code = 5 AND l_adjustment_quantity <> 0 ) THEN
6565          IF ( l_txn_header_id = -2 ) THEN
6566             SELECT mtl_material_transactions_s.NEXTVAL
6567             INTO   l_txn_header_id
6568             FROM   DUAL;
6569 
6570             g_txn_header_id := l_txn_header_id;
6571          END IF;
6572 
6573          SELECT mtl_material_transactions_s.NEXTVAL
6574          INTO   l_txn_temp_id
6575          FROM   DUAL;
6576 
6577          SELECT auto_serial_alpha_prefix
6578          INTO   l_serial_prefix
6579          FROM   mtl_system_items
6580          WHERE  inventory_item_id = l_item_id AND organization_id = l_org_id;
6581 
6582          l_p_uom_qty :=
6583             inv_convert.inv_um_convert ( l_item_id,
6584                                          5,
6585                                          l_txn_quantity,
6586                                          l_from_uom,
6587                                          l_to_uom,
6588                                          NULL,
6589                                          NULL
6590                                        );
6591 
6592          -- Bug 3296675, we need to delete cycle count reservations before checking for availability.
6593          delete_reservation ( );
6594 
6595          -- Bug # 2743382
6596 
6597          SELECT negative_inv_receipt_code
6598          INTO   l_neg_inv_rcpt_code --Negative Balance  1:Allowed   2:Disallowed
6599          FROM   mtl_parameters
6600          WHERE  organization_id = l_org_id;
6601 
6602          --4870490
6603          l_allow_neg_onhand_prof_val := NVL(FND_PROFILE.VALUE('INV_ALLOW_CC_TXNS_ONHAND_NEG'),2);
6604 
6605 
6606          -- Bug number 4469742 commented the IF clause here AS per the discussion WITH the PM
6607          -- for the complete opinion from the PM please refer to the update in the bug
6608          --*** JSHERMAN  07/01/05 02:44 pm ***
6609          -- after this the check IF (v_available_quantity + v_adjustment_quantity < 0)  will happen
6610          -- irrespective of the the l_neg_inv_rcpt_code flag value
6611 
6612     --  IF ( l_neg_inv_rcpt_code = 2 ) THEN
6613 
6614             SELECT serial_number_control_code,
6615                    lot_control_code,
6616                    revision_qty_control_code
6617             INTO   v_ser_code,
6618                    v_lot_code,
6619                    v_rev_code
6620             FROM   mtl_system_items
6621             WHERE  inventory_item_id = l_item_id
6622                    AND organization_id = l_org_id;
6623 
6624             IF ( v_ser_code <> 1 ) THEN
6625                v_is_ser_controlled := TRUE;
6626             END IF;
6627 
6628             IF ( v_lot_code <> 1 ) THEN
6629                v_is_lot_controlled := TRUE;
6630             END IF;
6631 
6632             IF ( v_rev_code <> 1 ) THEN
6633                v_is_rev_controlled := TRUE;
6634             END IF;
6635 
6636             inv_quantity_tree_pub.query_quantities ( p_api_version_number => 1.0,
6637                                                      p_init_msg_lst      => 'F',
6638                                                      x_return_status     => x_return_status,
6639                                                      x_msg_count         => l_msg_count,
6640                                                      x_msg_data          => l_msg_data,
6641                                                      p_organization_id   => l_org_id,
6642                                                      p_inventory_item_id => l_item_id,
6643                                                      p_tree_mode         => 1,
6644                                                      p_is_revision_control => v_is_rev_controlled,
6645                                                      p_is_lot_control    => v_is_lot_controlled,
6646                                                      p_is_serial_control => v_is_ser_controlled,
6647                                                      p_demand_source_type_id => NULL,
6648                                                      p_revision          => l_rev,
6649                                                      p_lot_number        => l_lot_num,
6650                                                      p_lot_expiration_date => l_lot_exp_date,
6651                                                      p_subinventory_code => l_sub,
6652                                                      p_locator_id        => l_locator_id,
6653                                                      p_onhand_source     => 3,
6654                                                      x_qoh               => x_qoh,
6655                                                      x_rqoh              => l_rqoh,
6656                                                      x_qr                => l_qr,
6657                                                      x_qs                => l_qs,
6658                                                      x_att               => x_att,
6659                                                      x_atr               => l_atr
6660                                                    );
6661             v_available_quantity := x_att;
6662 
6663                /* Bug Number 4690372
6664                Profile Value : Yes-1
6665                No/NUll- 2
6666                l_neg_rcpt_code 1- Allow
6667                2-Disallow
6668 
6669                Approval Option  L-Neg_rcpot Code   Profile Value    Behaviour
6670 
6671                Always             1                 1                Allows Approval
6672                Always             1                 2                On Approval Error is shown
6673                Always             2                 1                On Approval Error is shown
6674                Always             2                 2                On Approval Error is shown
6675 
6676 
6677                Approval Option  L-Neg_rcpot Code   Profile Value    Behaviour
6678 
6679                Never             1                 1                Adjustments happen at entry
6680                Never             1                 2                Adjustments Deferrred to Approval
6681                Never             2                 1                Adjustments Deferrred to Approval
6682                Never             2                 2                Adjustments Deferrred to Approval
6683                */
6684 
6685 
6686                --Bug 5095970, changing l_atr to x_att since for non-reservable subs l_atr will be 0
6687                IF ( v_available_quantity + l_adjustment_quantity < 0 AND l_entry_status_code = 5)
6688                AND (l_allow_neg_onhand_prof_val = 2 OR l_neg_inv_rcpt_code = 2  )
6689                THEN
6690                -- The cycle count adjustment should not be processed since it will
6691                -- invalidate an existing reservation/allocation.
6692 
6693                -- Reset the approval related colums in the cycle count entry record
6694                g_cc_entry.approval_type := NULL;
6695                g_cc_entry.approver_employee_id := NULL;
6696                g_cc_entry.approval_date := NULL;
6697                -- Reset the entry status code to 2: Approval required
6698                -- Do this for both the local variable as well as the global cycle count
6699                -- entry record
6700                g_cc_entry.entry_status_code := 2;
6701                l_entry_status_code := 2;
6702             END IF;
6703 
6704 
6705             -- Bug number 4469742 moved the IF clause here AS per the discussion WITH the PM
6706            -- for the complete opinion from the PM please refer to the update in the bug
6707             --*** JSHERMAN  07/01/05 02:44 pm ***
6708            -- after this the check IF (v_available_quantity + v_adjustment_quantity < 0)  will happen
6709            -- irrespective of the the l_neg_inv_rcpt_code flag value
6710 
6711       IF ( l_neg_inv_rcpt_code = 2 ) THEN
6712 
6713             inv_quantity_tree_pub.update_quantities ( p_api_version_number => 1.0,
6714                                                       p_init_msg_lst      => 'F',
6715                                                       x_return_status     => x_return_status,
6716                                                       x_msg_count         => l_msg_count,
6717                                                       x_msg_data          => l_msg_data,
6718                                                       p_organization_id   => l_org_id,
6719                                                       p_inventory_item_id => l_item_id,
6720                                                       p_tree_mode         => 1,
6721                                                       p_is_revision_control => v_is_rev_controlled,
6722                                                       p_is_lot_control    => v_is_lot_controlled,
6723                                                       p_is_serial_control => v_is_ser_controlled,
6724                                                       p_demand_source_type_id => NULL,
6725                                                       p_revision          => l_rev,
6726                                                       p_lot_number        => l_lot_num,
6727                                                       p_subinventory_code => l_sub,
6728                                                       p_locator_id        => l_locator_id,
6729                                                       p_onhand_source     => 3,
6730                                                       p_containerized     => 0,
6731                                                       p_primary_quantity  => ABS ( l_adjustment_quantity
6732                                                                                  ),
6733                                                       p_quantity_type     => 5,
6734                                                       x_qoh               => x_qoh,
6735                                                       x_rqoh              => l_rqoh,
6736                                                       x_qr                => l_qr,
6737                                                       x_qs                => l_qs,
6738                                                       x_att               => x_att,
6739                                                       x_atr               => l_atr,
6740                                                       p_lpn_id            => NULL --added for lpn reservation
6741                                                     );
6742          END IF;
6743 
6744          -- Check to see if the serial number is found
6745          -- in a discrepant location or not
6746          SELECT NVL ( REVISION, 'XXX' ),
6747                 NVL ( LOT_NUMBER, 'X' ),
6748                 CURRENT_STATUS,
6749                 CURRENT_SUBINVENTORY_CODE,
6750                 NVL ( CURRENT_LOCATOR_ID, 0 ),
6751                 NVL ( LPN_ID, -99 )
6752          INTO   l_msn_revision,
6753                 l_msn_lot_number,
6754                 l_current_status,
6755                 l_msn_subinv,
6756                 l_msn_locator_id,
6757                 l_msn_lpn_id
6758          FROM   MTL_SERIAL_NUMBERS
6759          WHERE  SERIAL_NUMBER = l_serial_number
6760          AND    INVENTORY_ITEM_ID = g_cc_entry.inventory_item_id
6761          AND    CURRENT_ORGANIZATION_ID = g_cc_entry.organization_id;
6762 
6763          -- If serial number exist with status 3 but at a different loc or revision etc.
6764          -- than we first need to issue out the original serial number and then process
6765          -- the receipt transaction.  Additionally, if the serial is found
6766          -- in a different LPN than what is in the system, it will also
6767          -- issue out the serial first before receiving it back into inventory
6768 
6769          IF (     l_current_status = 3
6770               AND l_adjustment_quantity = 1
6771               AND (    l_msn_lpn_id <> NVL ( g_cc_entry.parent_lpn_id, -99 )
6772                     OR (      (    l_msn_revision <> g_cc_entry.revision
6773                                 OR l_msn_lot_number <> g_cc_entry.lot_number
6774                                 OR l_msn_subinv <> g_cc_entry.subinventory
6775                                 OR l_msn_locator_id <> g_cc_entry.locator_id
6776                               )
6777                          AND l_msn_lpn_id = -99
6778                          AND g_cc_entry.parent_lpn_id IS NULL
6779                        )
6780                   )
6781             ) THEN
6782             IF ( l_msn_revision = 'XXX' ) THEN
6783                l_msn_revision := NULL;
6784             END IF;
6785 
6786             IF ( l_msn_lot_number = 'X' ) THEN
6787                l_msn_lot_number := NULL;
6788             END IF;
6789 
6790             IF ( l_msn_locator_id = 0 ) THEN
6791                l_msn_locator_id := NULL;
6792             END IF;
6793 
6794             IF ( l_msn_lpn_id = -99 ) THEN
6795                l_msn_lpn_id := NULL;
6796             END IF;
6797 
6798             l_adj_qty   := -1;
6799 
6800             IF ( l_debug = 1 ) THEN
6801                print_debug ( 'Serial discrepancy exists so issue out the serial first'
6802                            );
6803                print_debug ( 'Calling cc_transact with the following parameters: '
6804                            );
6805                print_debug ( 'org_id: ========> ' || l_org_id );
6806                print_debug ( 'cc_header_id: ==> ' || l_cc_header_id );
6807                print_debug ( 'item_id: =======> ' || l_item_id );
6808                print_debug ( 'sub: ===========> ' || l_msn_subinv );
6809                print_debug ( 'puomqty: =======> ' || -l_p_uom_qty );
6810                print_debug ( 'txnqty: ========> ' || l_adj_qty );
6811                print_debug ( 'txnuom: ========> ' || l_txn_uom );
6812                print_debug ( 'txndate: =======> ' || l_txn_date );
6813                print_debug ( 'txnacctid: =====> ' || l_txn_acct_id );
6814                print_debug ( 'lotnum: ========> ' || l_msn_lot_number );
6815                print_debug ( 'lotexpdate: ====> ' || l_lot_exp_date );
6816                print_debug ( 'rev: ===========> ' || l_msn_revision );
6817                print_debug ( 'locator_id: ====> ' || l_msn_locator_id );
6818                print_debug ( 'txnref: ========> ' || l_txn_ref );
6819                print_debug ( 'reasonid: ======> ' || l_reason_id );
6820                print_debug ( 'userid: ========> ' || l_user_id );
6821                print_debug ( 'cc_entry_id: ===> ' || l_cycle_count_entry_id );
6822                print_debug ( 'loginid: =======> ' || l_login_id );
6823                print_debug ( 'txnprocmode: ===> ' || l_txn_proc_mode );
6824                print_debug ( 'txnheaderid: ===> ' || l_txn_header_id );
6825                print_debug ( 'serialnum: =====> ' || l_serial_number );
6826                print_debug ( 'txntempid: =====> ' || l_txn_temp_id );
6827                print_debug ( 'serialprefix: ==> ' || l_serial_prefix );
6828                print_debug ( 'lpn_id: ========> ' || l_msn_lpn_id );
6829                print_debug ( 'cost_group_id: => ' || l_cost_group_id );
6830                print_debug ( ' ' );
6831             END IF;
6832 
6833             l_success_flag :=
6834                mtl_cc_transact_pkg.cc_transact ( org_id              => l_org_id,
6835                                                  cc_header_id        => l_cc_header_id,
6836                                                  item_id             => l_item_id,
6837                                                  sub                 => l_msn_subinv,
6838                                                  puomqty             => -l_p_uom_qty,
6839                                                  txnqty              => l_adj_qty,
6840                                                  txnuom              => l_txn_uom,
6841                                                  txndate             => l_txn_date,
6842                                                  txnacctid           => l_txn_acct_id,
6843                                                  lotnum              => l_msn_lot_number,
6844                                                  lotexpdate          => l_lot_exp_date,
6845                                                  rev                 => l_msn_revision,
6846                                                  locator_id          => l_msn_locator_id,
6847                                                  txnref              => l_txn_ref,
6848                                                  reasonid            => l_reason_id,
6849                                                  userid              => l_user_id,
6850                                                  cc_entry_id         => l_cycle_count_entry_id,
6851                                                  loginid             => l_login_id,
6852                                                  txnprocmode         => l_txn_proc_mode,
6853                                                  txnheaderid         => l_txn_header_id,
6854                                                  serialnum           => l_serial_number,
6855                                                  txntempid           => l_txn_temp_id,
6856                                                  serialprefix        => l_serial_prefix,
6857                                                  lpn_id              => l_msn_lpn_id,
6858                                                  cost_group_id       => l_cost_group_id
6859                                                );
6860 
6861             IF ( l_debug = 1 ) THEN
6862                print_debug ( 'success_flag: ' || l_success_flag );
6863                print_debug('Calling delete_Serial_entry 1');
6864             END IF;
6865 
6866             delete_serial_entry(l_serial_number,l_cc_header_id,l_cycle_count_entry_id); --3595723 Delete the serial info from mtl_cc_Serial_numbers
6867 
6868 
6869             --If success flag is 2 or 3 then set the message for invalid
6870             --material status for the lot/serial and the item combination
6871             IF ( NVL ( l_success_flag, -1 ) < 0 ) THEN
6872                FND_MESSAGE.SET_NAME ( 'INV', 'INV_ADJ_TXN_FAILED' );
6873                FND_MSG_PUB.ADD;
6874                ROLLBACK TO save_serial_detail;
6875                RAISE FND_API.G_EXC_ERROR;
6876             ELSIF NVL ( l_success_flag, -1 ) = 2 THEN
6877                FND_MESSAGE.SET_NAME ( 'INV', 'INV_TRX_LOT_NA_DUE_MS' );
6878                FND_MESSAGE.SET_TOKEN ( 'TOKEN1', l_msn_lot_number );
6879                FND_MESSAGE.SET_TOKEN ( 'TOKEN2', l_item_name );
6880                FND_MSG_PUB.ADD;
6881                RAISE FND_API.G_EXC_ERROR;
6882             ELSIF NVL ( l_success_flag, -1 ) = 3 THEN
6883                FND_MESSAGE.SET_NAME ( 'INV', 'INV_TRX_SER_NA_DUE_MS' );
6884                FND_MESSAGE.SET_TOKEN ( 'TOKEN1', l_serial_number );
6885                FND_MESSAGE.SET_TOKEN ( 'TOKEN2', l_item_name );
6886                FND_MSG_PUB.ADD;
6887                RAISE FND_API.G_EXC_ERROR;
6888             END IF;
6889 
6890             -- Get a new txn temp ID for receiving the serial back into inventory
6891             SELECT mtl_material_transactions_s.NEXTVAL
6892             INTO   l_txn_temp_id
6893             FROM   DUAL;
6894          END IF;
6895 
6896          IF ( l_adjustment_quantity = 1 ) THEN
6897             -- Receiving the serial into the sub and loc where it was found
6898             l_temp_subinv := l_sub;
6899             l_temp_locator_id := l_locator_id;
6900          ELSIF ( l_adjustment_quantity = -1 ) THEN
6901             -- Issuing out the serial from the sub and loc where
6902             -- the system thinks it currently is
6903             l_temp_subinv := l_msn_subinv;
6904             IF ( l_msn_locator_id = 0 ) THEN --  Start bug 7700461
6905                l_msn_locator_id := NULL;
6906             END IF;                          --  End bug 7700461
6907             l_temp_locator_id := l_msn_locator_id;
6908             IF ( l_debug = 1 ) THEN
6909                print_debug('Calling delete_Serial_entry 2');
6910             END IF;
6911             delete_serial_entry(l_serial_number,l_cc_header_id,l_cycle_count_entry_id); --3595723 Delete the serial info from mtl_cc_Serial_numbers
6912          END IF;
6913 
6914          IF ( l_debug = 1 ) THEN
6915             print_debug ( 'Calling cc_transact with the following parameters: '
6916                         );
6917             print_debug ( 'org_id: ===========> ' || l_org_id );
6918             print_debug ( 'cc_header_id: =====> ' || l_cc_header_id );
6919             print_debug ( 'item_id: ==========> ' || l_item_id );
6920             print_debug ( 'sub: ==============> ' || l_temp_subinv );
6921             print_debug ( 'puomqty: ==========> ' || l_p_uom_qty );
6922             print_debug ( 'txnqty: ===========> ' || l_txn_quantity );
6923             print_debug ( 'txnuom: ===========> ' || l_txn_uom );
6924             print_debug ( 'txndate: ==========> ' || l_txn_date );
6925             print_debug ( 'txnacctid: ========> ' || l_txn_acct_id );
6926             print_debug ( 'lotnum: ===========> ' || l_lot_num );
6927             print_debug ( 'lotexpdate: =======> ' || l_lot_exp_date );
6928             print_debug ( 'rev: ==============> ' || l_rev );
6929             print_debug ( 'locator_id: =======> ' || l_temp_locator_id );
6930             print_debug ( 'txnref: ===========> ' || l_txn_ref );
6931             print_debug ( 'reasonid: =========> ' || l_reason_id );
6932             print_debug ( 'userid: ===========> ' || l_user_id );
6933             print_debug ( 'cc_entry_id: ======> ' || l_cycle_count_entry_id );
6934             print_debug ( 'loginid: ==========> ' || l_login_id );
6935             print_debug ( 'txnprocmode: ======> ' || l_txn_proc_mode );
6936             print_debug ( 'txnheaderid: ======> ' || l_txn_header_id );
6937             print_debug ( 'serialnum: ========> ' || l_serial_number );
6938             print_debug ( 'txntempid: ========> ' || l_txn_temp_id );
6939             print_debug ( 'serialprefix: =====> ' || l_serial_prefix );
6940             print_debug ( 'lpn_id: ===========> ' || l_lpn_id );
6941             print_debug ( 'cost_group_id: ====> ' || l_cost_group_id );
6942             print_debug ( ' ' );
6943          END IF;
6944 
6945          l_success_flag :=
6946             mtl_cc_transact_pkg.cc_transact ( org_id              => l_org_id,
6947                                               cc_header_id        => l_cc_header_id,
6948                                               item_id             => l_item_id,
6949                                               sub                 => l_temp_subinv,
6950                                               puomqty             => l_p_uom_qty,
6951                                               txnqty              => l_txn_quantity,
6952                                               txnuom              => l_txn_uom,
6953                                               txndate             => l_txn_date,
6954                                               txnacctid           => l_txn_acct_id,
6955                                               lotnum              => l_lot_num,
6956                                               lotexpdate          => l_lot_exp_date,
6957                                               rev                 => l_rev,
6958                                               locator_id          => l_temp_locator_id,
6959                                               txnref              => l_txn_ref,
6960                                               reasonid            => l_reason_id,
6961                                               userid              => l_user_id,
6962                                               cc_entry_id         => l_cycle_count_entry_id,
6963                                               loginid             => l_login_id,
6964                                               txnprocmode         => l_txn_proc_mode,
6965                                               txnheaderid         => l_txn_header_id,
6966                                               serialnum           => l_serial_number,
6967                                               txntempid           => l_txn_temp_id,
6968                                               serialprefix        => l_serial_prefix,
6969                                               lpn_id              => l_lpn_id,
6970                                               cost_group_id       => l_cost_group_id
6971                                             );
6972 
6973          IF ( l_debug = 1 ) THEN
6974             print_debug ( 'success_flag: ' || l_success_flag );
6975          END IF;
6976 
6977          --If success flag is 2 or 3 then set the message for invalid
6978          --material status for the lot/serial and the item combination
6979          --IF NVL(l_txn_header_id, -1) < 0 OR
6980          IF NVL ( l_success_flag, -1 ) < 0 THEN
6981             FND_MESSAGE.SET_NAME ( 'INV', 'INV_ADJ_TXN_FAILED' );
6982             FND_MSG_PUB.ADD;
6983             ROLLBACK TO save_serial_detail;
6984             RAISE FND_API.G_EXC_ERROR;
6985          ELSIF NVL ( l_success_flag, -1 ) = 2 THEN
6986             FND_MESSAGE.SET_NAME ( 'INV', 'INV_TRX_LOT_NA_DUE_MS' );
6987             FND_MESSAGE.SET_TOKEN ( 'TOKEN1', l_lot_num );
6988             FND_MESSAGE.SET_TOKEN ( 'TOKEN2', l_item_name );
6989             FND_MSG_PUB.ADD;
6990             RAISE FND_API.G_EXC_ERROR;
6991          ELSIF NVL ( l_success_flag, -1 ) = 3 THEN
6992             FND_MESSAGE.SET_NAME ( 'INV', 'INV_TRX_SER_NA_DUE_MS' );
6993             FND_MESSAGE.SET_TOKEN ( 'TOKEN1', l_serial_number );
6994             FND_MESSAGE.SET_TOKEN ( 'TOKEN2', l_item_name );
6995             FND_MSG_PUB.ADD;
6996             RAISE FND_API.G_EXC_ERROR;
6997          END IF;
6998 
6999          -- Set the commit status flag so that the TM will be called in the
7000          -- post commit procedure
7001          IF ( l_debug = 1 ) THEN
7002             print_debug ( 'Setting the g_commit_status_flag := 1' );
7003          END IF;
7004 
7005          g_commit_status_flag := 1;
7006          g_cc_entry.inventory_adjustment_account := l_txn_acct_id;
7007       END IF;
7008    END perform_serial_adj_txn;
7009 
7010    PROCEDURE count_entry_status_code
7011    IS
7012       l_count_esc NUMBER := g_count_entry_status_code;
7013       l_serial_esc NUMBER := g_serial_entry_status_code;
7014       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
7015    BEGIN
7016       IF ( l_debug = 1 ) THEN
7017          print_debug ( '***count_entry_status_code***' );
7018          print_debug ( 'Count Entry Status: ' || l_count_esc );
7019          print_debug ( 'Serial Entry Status: ' || l_serial_esc );
7020       END IF;
7021 
7022       IF (    l_count_esc IS NULL
7023            OR l_count_esc = 1 ) THEN
7024          g_count_entry_status_code := l_serial_esc;
7025       ELSE
7026          IF ( l_count_esc > l_serial_esc ) THEN
7027             g_count_entry_status_code := l_serial_esc;
7028          END IF;
7029       END IF;
7030    END count_entry_status_code;
7031 
7032    PROCEDURE update_serial_row
7033    IS
7034       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
7035    BEGIN
7036       IF ( l_debug = 1 ) THEN
7037          print_debug ( '***update_serial_row***' );
7038       END IF;
7039 
7040       -- Set the WHO column information
7041       g_cc_serial_entry.last_update_date := SYSDATE;
7042       g_cc_serial_entry.last_updated_by := g_user_id;
7043       g_cc_serial_entry.last_update_login := g_login_id;
7044 
7045       UPDATE MTL_CC_SERIAL_NUMBERS
7046       SET last_update_date = g_cc_serial_entry.last_update_date,
7047           last_updated_by = g_cc_serial_entry.last_updated_by,
7048           last_update_login = g_cc_serial_entry.last_update_login,
7049           number_of_counts = g_cc_serial_entry.number_of_counts,
7050           unit_status_current = g_cc_serial_entry.unit_status_current,
7051           unit_status_prior = g_cc_serial_entry.unit_status_prior,
7052           unit_status_first = g_cc_serial_entry.unit_status_first,
7053           approval_condition = g_cc_serial_entry.approval_condition,
7054           pos_adjustment_qty = g_cc_serial_entry.pos_adjustment_qty,
7055           neg_adjustment_qty = g_cc_serial_entry.neg_adjustment_qty
7056       WHERE  cycle_count_entry_id = g_cc_entry.cycle_count_entry_id
7057       AND    (     ( serial_number = g_cc_serial_entry.serial_number )
7058                OR (     serial_number IS NULL
7059                     AND g_cc_serial_entry.serial_number IS NULL
7060                   )
7061              );
7062 
7063       IF ( SQL%NOTFOUND ) THEN
7064          RAISE NO_DATA_FOUND;
7065       END IF;
7066    END update_serial_row;
7067 
7068    PROCEDURE mark
7069    IS
7070       g1        VARCHAR2 ( 30 );
7071       g2        VARCHAR2 ( 30 );
7072       g3        NUMBER;
7073       g4        NUMBER;
7074       g5        NUMBER;
7075       g6        NUMBER;
7076       g7        NUMBER;
7077       success   NUMBER := 1;
7078       l_serial_number_ctrl_code NUMBER;
7079       l_serial_count_option NUMBER;
7080       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
7081    BEGIN
7082       IF ( l_debug = 1 ) THEN
7083          print_debug ( '***mark***' );
7084       END IF;
7085 
7086       -- Get the required values
7087       SELECT serial_number_control_code
7088       INTO   l_serial_number_ctrl_code
7089       FROM   mtl_system_items
7090       WHERE  inventory_item_id = g_cc_entry.inventory_item_id
7091       AND    organization_id = g_cc_entry.organization_id;
7092 
7093       SELECT NVL ( serial_count_option, 1 )
7094       INTO   l_serial_count_option
7095       FROM   mtl_cycle_count_headers
7096       WHERE  cycle_count_header_id = g_cc_entry.cycle_count_header_id
7097       AND    organization_id = g_cc_entry.organization_id;
7098 
7099       IF ( l_serial_number_ctrl_code IN ( 2, 5 ) ) THEN
7100          IF ( l_serial_count_option = 2 ) THEN
7101             g1          := g_cc_entry.serial_number;
7102             g2          := g_cc_entry.serial_number;
7103          ELSIF ( l_serial_count_option = 3 ) THEN
7104             g1          := g_serial_number;
7105             g2          := g_serial_number;
7106          END IF;
7107       END IF;
7108 
7109       g3          := g_cc_entry.inventory_item_id;
7110       g4          := g_cc_entry.organization_id;
7111       g5          := g_cc_entry.cycle_count_header_id;
7112       g6          := g_cc_entry.cycle_count_entry_id;
7113       g7          := NULL;
7114 
7115       -- Call the procedure to mark the serial only if the entry is serial
7116       -- controlled and the cycle count header is single serial
7117       IF ( l_debug = 1 ) THEN
7118          print_debug (    'Serial number control code: '
7119                        || l_serial_number_ctrl_code
7120                      );
7121          print_debug ( 'Serial count option: ' || l_serial_count_option );
7122       END IF;
7123 
7124       IF (      ( l_serial_number_ctrl_code IN ( 2, 5 ) )
7125            AND ( l_serial_count_option = 2 )
7126          ) THEN
7127          IF ( l_debug = 1 ) THEN
7128             print_debug ( 'Calling serial_check.inv_mark_serial with the following parameters: '
7129                         );
7130             print_debug ( 'from_serial_number: => ' || g1 );
7131             print_debug ( 'to_serial_number: ===> ' || g2 );
7132             print_debug ( 'item_id: ============> ' || g3 );
7133             print_debug ( 'org_id: =============> ' || g4 );
7134             print_debug ( 'hdr_id: =============> ' || g5 );
7135             print_debug ( 'temp_id: ============> ' || g6 );
7136             print_debug ( 'lot_temp_id: ========> ' || g7 );
7137             print_debug ( ' ' );
7138          END IF;
7139 
7140          serial_check.inv_mark_serial ( from_serial_number  => g1,
7141                                         to_serial_number    => g2,
7142                                         item_id             => g3,
7143                                         org_id              => g4,
7144                                         hdr_id              => g5,
7145                                         temp_id             => g6,
7146                                         lot_temp_id         => g7,
7147                                         success             => success
7148                                       );
7149 
7150          IF ( l_debug = 1 ) THEN
7151             print_debug ( 'Return value of success: ' || success );
7152          END IF;
7153 
7154          -- A return success value of -1 means that the serial has already
7155          -- been marked.  We don't need to error out in this case.
7156          IF ( success < -1 ) THEN
7157             FND_MESSAGE.SET_NAME ( 'INV', 'INV_SERIAL_UNAVAILABLE' );
7158             FND_MESSAGE.SET_TOKEN ( 'FIRST-SERIAL', g1 );
7159             FND_MSG_PUB.ADD;
7160 
7161             IF ( l_serial_count_option = 3 ) THEN
7162                ROLLBACK TO save_serial_detail;
7163             END IF;
7164 
7165             RAISE FND_API.G_EXC_ERROR;
7166          END IF;
7167       END IF;
7168    END mark;
7169 
7170    PROCEDURE unmark (
7171       cycle_cnt_entry_id NUMBER
7172    )
7173    IS
7174       g1        VARCHAR2 ( 30 );
7175       g2        VARCHAR2 ( 30 );
7176       g3        NUMBER;
7177       g4        NUMBER;
7178       g5        NUMBER;
7179       g6        NUMBER;
7180       g7        NUMBER;
7181       l_serial_number_ctrl_code NUMBER;
7182       l_serial_count_option NUMBER;
7183       l_current_serial VARCHAR2 ( 30 );
7184       l_current_item NUMBER;
7185       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
7186    BEGIN
7187       IF ( l_debug = 1 ) THEN
7188          print_debug ( '***unmark***' );
7189       END IF;
7190 
7191       -- Get the required values
7192       SELECT serial_number_control_code
7193       INTO   l_serial_number_ctrl_code
7194       FROM   mtl_system_items
7195       WHERE  inventory_item_id = g_cc_entry.inventory_item_id
7196       AND    organization_id = g_cc_entry.organization_id;
7197 
7198       SELECT NVL ( serial_count_option, 1 )
7199       INTO   l_serial_count_option
7200       FROM   mtl_cycle_count_headers
7201       WHERE  cycle_count_header_id = g_cc_entry.cycle_count_header_id
7202       AND    organization_id = g_cc_entry.organization_id;
7203 
7204       SELECT NVL ( serial_number, '@@@@@' ),
7205              inventory_item_id
7206       INTO   l_current_serial,
7207              l_current_item
7208       FROM   mtl_cycle_count_entries
7209       WHERE  cycle_count_entry_id = cycle_cnt_entry_id;
7210 
7211       -- Old call to serial_check.inv_unmark_serial
7212       --g1 := NULL;
7213       --g2 := NULL;
7214       --g3 := l_serial_number_ctrl_code;
7215       --g4 := g_cc_entry.cycle_count_header_id;
7216       --g5 := cycle_cnt_entry_id;
7217       --g6 := NULL;
7218 
7219       -- New call to serial_check.inv_unmark_serial
7220       -- For performance reasons, pass the serial number and item id
7221       -- instead since these are the primary keys for MTL_SERIAL_NUMBERS
7222       IF ( l_serial_number_ctrl_code IN ( 2, 5 ) ) THEN
7223          IF ( l_serial_count_option = 2 ) THEN
7224             g1          := l_current_serial;
7225             g2          := l_current_serial;
7226          ELSIF ( l_serial_count_option = 3 ) THEN
7227             g1          := g_serial_number;
7228             g2          := g_serial_number;
7229          END IF;
7230       END IF;
7231 
7232       g3          := l_serial_number_ctrl_code;
7233       g4          := NULL;
7234       g5          := NULL;
7235       g6          := NULL;
7236       g7          := l_current_item;
7237 
7238       -- Call the procedure to unmark the serial only if the entry is serial
7239       -- controlled and the cycle count header is single serial
7240       IF ( l_debug = 1 ) THEN
7241          print_debug (    'Serial number control code: '
7242                        || l_serial_number_ctrl_code
7243                      );
7244          print_debug ( 'Serial count option: ' || l_serial_count_option );
7245       END IF;
7246 
7247       IF (      ( l_serial_number_ctrl_code IN ( 2, 5 ) )
7248            AND ( l_serial_count_option = 2 )
7249          ) THEN
7250          IF ( l_debug = 1 ) THEN
7251             print_debug ( 'Calling serial_check.inv_unmark_serial with the following parameters: '
7252                         );
7253             print_debug ( 'from_serial_number: ==> ' || g1 );
7254             print_debug ( 'to_serial_number: ====> ' || g2 );
7255             print_debug ( 'serial_code: =========> ' || g3 );
7256             print_debug ( 'hdr_id: ==============> ' || g4 );
7257             print_debug ( 'temp_id: =============> ' || g5 );
7258             print_debug ( 'lot_temp_id: =========> ' || g6 );
7259             print_debug ( 'p_inventory_item_id: => ' || g7 );
7260             print_debug ( ' ' );
7261          END IF;
7262 
7263          serial_check.inv_unmark_serial ( from_serial_number  => g1,
7264                                           to_serial_number    => g2,
7265                                           serial_code         => g3,
7266                                           hdr_id              => g4,
7267                                           temp_id             => g5,
7268                                           lot_temp_id         => g6,
7269                                           p_inventory_item_id => g7
7270                                         );
7271       END IF;
7272    END unmark;
7273 
7274    PROCEDURE get_profiles
7275    IS
7276       v_user_id NUMBER;
7277       v_login_id NUMBER;
7278       profile_name VARCHAR2 ( 60 );
7279       profile_val VARCHAR2 ( 80 );
7280       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
7281    BEGIN
7282       IF ( l_debug = 1 ) THEN
7283          print_debug ( '***get_profiles***' );
7284       END IF;
7285 
7286       v_login_id  := fnd_global.login_id;
7287       g_login_id  := v_login_id;
7288       -- Get profile option for transaction date validation
7289       profile_name := 'TRANSACTION_PROCESS_MODE';
7290       profile_val := FND_PROFILE.VALUE ( profile_name );
7291 
7292       IF ( profile_val = 4 ) THEN
7293          -- Get form level profile
7294          profile_name := 'CYCLE_COUNT_ENTRIES_TXN';
7295          profile_val := FND_PROFILE.VALUE ( profile_name );
7296       END IF;
7297 
7298       g_txn_proc_mode := profile_val;
7299    END get_profiles;
7300 
7301    PROCEDURE get_employee (
7302       p_organization_id IN NUMBER
7303    )
7304    IS
7305       l_employee_id NUMBER;
7306       l_employee_full_name VARCHAR2 ( 240 );
7307       l_user_id NUMBER := g_user_id;
7308       l_org_id  NUMBER := p_organization_id;
7309       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
7310    BEGIN
7311       IF ( l_debug = 1 ) THEN
7312          print_debug ( '***get_employee***' );
7313       END IF;
7314 
7315       BEGIN
7316          SELECT mec.full_name,
7317                 fus.employee_id
7318          INTO   l_employee_full_name,
7319                 l_employee_id
7320          FROM   mtl_employees_current_view mec,
7321                 fnd_user fus
7322          WHERE  fus.user_id = l_user_id
7323          AND    mec.employee_id = fus.employee_id
7324          AND    mec.organization_id = l_org_id;
7325       EXCEPTION
7326          WHEN NO_DATA_FOUND THEN
7327             -- Just get the employee ID if the employee
7328             -- is not properly defined in the MTL_EMPLOYEES_CURRENT_VIEW
7329             SELECT fus.employee_id
7330             INTO   l_employee_id
7331             FROM   fnd_user fus
7332             WHERE  fus.user_id = l_user_id;
7333 
7334             l_employee_full_name := NULL;
7335       END;
7336 
7337       g_employee_id := l_employee_id;
7338       g_employee_full_name := l_employee_full_name;
7339    EXCEPTION
7340       WHEN NO_DATA_FOUND THEN
7341          g_employee_id := NULL;
7342          g_employee_full_name := NULL;
7343    END get_employee;
7344 
7345    PROCEDURE process_summary (
7346       p_cycle_count_header_id IN NUMBER,
7347       p_organization_id IN NUMBER,
7348       p_subinventory IN VARCHAR2,
7349       p_locator_id IN NUMBER,
7350       p_parent_lpn_id IN NUMBER,
7351       p_unscheduled_count_entry IN NUMBER,
7352       p_user_id   IN NUMBER
7353    )
7354    IS
7355       l_current_lpn NUMBER;
7356       l_temp_uom_code VARCHAR2 ( 3 );
7357       --13053297
7358       l_outermost_lpn_id NUMBER;
7359       l_parent_lpn_id NUMBER;
7360       l_sub VARCHAR2(240);
7361       l_locator_id NUMBER;
7362       l_org_id NUMBER;
7363       --13053297
7364 
7365       CURSOR nested_lpn_cursor
7366       IS
7367          SELECT     *
7368          FROM       WMS_LICENSE_PLATE_NUMBERS
7369          START WITH lpn_id = p_parent_lpn_id
7370          CONNECT BY parent_lpn_id = PRIOR lpn_id;
7371 
7372       CURSOR lpn_contents_cursor
7373       IS
7374          SELECT *
7375          FROM   WMS_LPN_CONTENTS
7376          WHERE  parent_lpn_id = l_current_lpn
7377          AND    NVL ( serial_summary_entry, 2 ) = 2;
7378 
7379       CURSOR lpn_serial_contents_cursor
7380       IS
7381          SELECT *
7382          FROM   MTL_SERIAL_NUMBERS
7383          WHERE  lpn_id = l_current_lpn;
7384 
7385       CURSOR lpn_multiple_serial_cursor
7386       IS
7387          SELECT *
7388          FROM   WMS_LPN_CONTENTS
7389          WHERE  parent_lpn_id = l_current_lpn AND serial_summary_entry = 1;
7390 
7391       --Bug#4891370.Added new cursor to query loaded quantity from LPN.
7392          CURSOR lpn_loaded_quantity_cur(p_inventory_item_id NUMBER,p_organization_id NUMBER,p_lpn_id NUMBER)
7393          IS
7394             SELECT NVL ( SUM ( quantity ), 0 )
7395             FROM   WMS_LOADED_QUANTITIES_V WLQV
7396             WHERE  WLQV.inventory_item_id = p_inventory_item_id
7397             AND    WLQV.organization_id = p_organization_id
7398             AND    (lpn_id = p_lpn_id OR content_lpn_id = p_lpn_id )
7399             AND    qty_type = 'LOADED';
7400 
7401 
7402       l_serial_count_option NUMBER;
7403       l_temp_count NUMBER;
7404       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
7405       l_lpn_loaded_qty NUMBER; --Bug#4891370
7406       l_cnt_qty NUMBER; --Added for bug#4886188
7407       l_lpn_context  NUMBER; --Added for bug#4886188
7408 
7409    BEGIN
7410       IF ( l_debug = 1 ) THEN
7411          print_debug ( '***process_summary***' );
7412       END IF;
7413       g_lpn_summary_count := TRUE; --9452528.
7414 
7415       -- Get the serial count option first
7416       SELECT NVL ( serial_count_option, 1 )
7417       INTO   l_serial_count_option
7418       FROM   mtl_cycle_count_headers
7419       WHERE  cycle_count_header_id = p_cycle_count_header_id
7420       AND    organization_id = p_organization_id;
7421 
7422 	/* Check if the LPN being Counted is an Inner LPN.
7423 	   IF Yes, we need to check if the sub/loc is different
7424 	   than the sub/loc in which the LPN resides. If Yes,
7425 	   Unpack(Un-Nest) the same.
7426 	--13053297
7427 	*/
7428 	IF p_parent_lpn_id IS NOT NULL
7429 	THEN
7430 	BEGIN
7431 	  SELECT outermost_lpn_id,
7432 	         NVL(parent_lpn_id,-99),
7433 	         NVL(subinventory_code,'##'),
7434 		     NVL(locator_id,-99),
7435 		     organization_id
7436         INTO l_outermost_lpn_id,
7437 		     l_parent_lpn_id,
7438 		     l_sub,
7439 		     l_locator_id,
7440 		     l_org_id
7441         FROM wms_license_plate_numbers
7442        WHERE lpn_id = p_parent_lpn_id;
7443 
7444 	   IF ( l_debug = 1 ) THEN
7445          print_debug ( 'To Check If we have to call PackUnpack' );
7446 		 print_debug ( 'Parameter Being Passed and extracted from WLPN' );
7447 		 print_debug ( 'p_subinventory   :'||p_subinventory );
7448 		 print_debug ( 'p_locator_id     :'||p_locator_id );
7449 		 print_debug ( 'LPN Counted      :'||p_parent_lpn_id );
7450 		 print_debug ( 'LPN Sub          :'||l_sub);
7451 		 print_debug ( 'LPN Locator      :'||l_locator_id );
7452        END IF;
7453 
7454 	   IF ((l_locator_id <> -99 AND l_sub <> '##') AND(l_locator_id <> p_locator_id OR l_sub <> p_subinventory)
7455 	        AND l_outermost_lpn_id <> p_parent_lpn_id) THEN
7456 	   --unpack;
7457 	   IF ( l_debug = 1 ) THEN
7458            print_debug ( 'We are into Discrepancy The LPN Sub/Loc does not match the CC Sub/Loc' );
7459            print_debug ( 'The LPN being counted is nested, so Un-nesting the same' );
7460           END IF;
7461 	   call_pack_unpack_container(
7462 	        p_lpn_id              => l_parent_lpn_id
7463           , p_content_lpn_id      => p_parent_lpn_id
7464           , p_org_id              => l_org_id
7465           , p_subinv              => l_sub
7466           , p_locator             => l_locator_id
7467           , p_operation           => g_unpack
7468           );
7469 
7470 	   END IF;
7471 
7472 	EXCEPTION
7473 	WHEN NO_DATA_FOUND THEN
7474 	IF ( l_debug = 1 ) THEN
7475          print_debug ( '***process_summary*** No Data Found Exception while querying LPN' );
7476     END IF;
7477 	WHEN OTHERS THEN
7478 	IF ( l_debug = 1 ) THEN
7479          print_debug ( '***process_summary*** Others Exception while querying LPN'||SQLERRM||' ' ||SQLCODE );
7480     END IF;
7481 	END;
7482 	END IF;
7483 --13053297
7484 
7485       /* Use the cursor that searches through all levels in the parent child relationship */
7486       FOR v_lpn_id IN nested_lpn_cursor
7487       LOOP
7488          l_current_lpn := v_lpn_id.lpn_id;
7489 
7490          IF ( l_debug = 1 ) THEN
7491             print_debug ( 'Current LPN: ' || l_current_lpn );
7492          END IF;
7493 
7494          -- Process the count entry for the LPN item itself if it is associated with
7495          -- an inventory item
7496 	 /*
7497 	  --   Commented for the bug 12770212
7498 	  --   We are trying to create MCCE for a container item, which never exists in
7499 	  --   MOQD. It would create variance b/w adjustment qty and System qty
7500 	  --   and approval of it would create un-necessary onhand for container item.
7501 	  --   Also it would block counting of any LPN which has same container item
7502 	  --   as the MCCE created would be in pending approval state.
7503 	  --   Hence commented the below if block
7504 
7505          IF ( v_lpn_id.inventory_item_id IS NOT NULL ) THEN
7506             -- Make sure that this inventory item is defined in the
7507             -- cycle count header item scope
7508             SELECT COUNT ( * )
7509             INTO   l_temp_count
7510             FROM   mtl_cycle_count_items
7511             WHERE  inventory_item_id = v_lpn_id.inventory_item_id
7512             AND    cycle_count_header_id = p_cycle_count_header_id;
7513 
7514             IF ( l_temp_count <> 0 ) THEN
7515                -- Get the primary UOM for the container inventory item
7516                SELECT primary_uom_code
7517                INTO   l_temp_uom_code
7518                FROM   mtl_system_items
7519                WHERE  inventory_item_id = v_lpn_id.inventory_item_id
7520                AND    organization_id = v_lpn_id.organization_id;
7521 
7522                IF ( l_debug = 1 ) THEN
7523                   print_debug ( 'Counting an LPN item itself' );
7524                END IF;
7525 
7526                process_entry ( p_cycle_count_header_id => p_cycle_count_header_id,
7527                                p_organization_id   => p_organization_id,
7528                                p_subinventory      => p_subinventory,
7529                                p_locator_id        => p_locator_id,
7530                                p_parent_lpn_id     => v_lpn_id.parent_lpn_id,
7531                                p_inventory_item_id => v_lpn_id.inventory_item_id,
7532                                p_revision          => v_lpn_id.revision,
7533                                p_lot_number        => v_lpn_id.lot_number,
7534                                p_from_serial_number => v_lpn_id.serial_number,
7535                                p_to_serial_number  => v_lpn_id.serial_number,
7536                                p_count_quantity    => 1,
7537                                p_count_uom         => l_temp_uom_code,
7538                                p_unscheduled_count_entry => p_unscheduled_count_entry,
7539                                p_user_id           => p_user_id,
7540                                p_cost_group_id     => v_lpn_id.cost_group_id
7541                              );
7542             END IF;
7543          END IF;
7544          */
7545          /* Process the count entries for the LPN content items */
7546          FOR v_lpn_content IN lpn_contents_cursor
7547          LOOP
7548             -- Make sure that this inventory item is defined in the
7549             -- cycle count header item scope
7550             SELECT COUNT ( * )
7551             INTO   l_temp_count
7552             FROM   mtl_cycle_count_items
7553             WHERE  inventory_item_id = v_lpn_content.inventory_item_id
7554             AND    cycle_count_header_id = p_cycle_count_header_id;
7555 
7556             IF ( l_temp_count <> 0 ) THEN
7557                IF ( l_debug = 1 ) THEN
7558                   print_debug ( 'Counting an LPN content item' );
7559                END IF;
7560 
7561 
7562         --Bug#4891370.Reduce the loaded quantity from count quantity.
7563                   OPEN lpn_loaded_quantity_cur(v_lpn_content.inventory_item_id,p_organization_id,l_current_lpn);
7564                   FETCH lpn_loaded_quantity_cur INTO l_lpn_loaded_qty;
7565 
7566                   IF (lpn_loaded_quantity_cur%FOUND) THEN
7567                     v_lpn_content.quantity := v_lpn_content.quantity - l_lpn_loaded_qty;
7568                     IF ( l_debug = 1 ) THEN
7569                       print_debug ('For lpn_id:'||l_current_lpn||',Loaded qty:'||l_lpn_loaded_qty
7570                                    ||',count qty:'|| v_lpn_content.quantity  );
7571                     END IF;
7572                   END IF;
7573 
7574                   CLOSE lpn_loaded_quantity_cur; ----End of fix for Bug#4891370
7575 
7576                process_entry ( p_cycle_count_header_id => p_cycle_count_header_id,
7577                                p_organization_id   => p_organization_id,
7578                                p_subinventory      => p_subinventory,
7579                                p_locator_id        => p_locator_id,
7580                                p_parent_lpn_id     => v_lpn_content.parent_lpn_id,
7581                                p_inventory_item_id => v_lpn_content.inventory_item_id,
7582                                p_revision          => v_lpn_content.revision,
7583                                p_lot_number        => v_lpn_content.lot_number,
7584                                p_from_serial_number => NULL,
7585                                p_to_serial_number  => NULL,
7586                                p_count_quantity    => v_lpn_content.quantity,
7587                                p_count_uom         => v_lpn_content.uom_code,
7588                                p_unscheduled_count_entry => p_unscheduled_count_entry,
7589                                p_user_id           => p_user_id,
7590                                p_cost_group_id     => v_lpn_content.cost_group_id
7591                              );
7592             END IF;
7593          END LOOP;
7594 
7595          /* Process the count entries for serialized items */
7596          IF ( l_serial_count_option = 2 ) THEN
7597             -- Single serial
7598             FOR v_lpn_serial_content IN lpn_serial_contents_cursor
7599             LOOP
7600                -- Make sure that this inventory item is defined in the
7601                -- cycle count header item scope
7602                SELECT COUNT ( * )
7603                INTO   l_temp_count
7604                FROM   mtl_cycle_count_items
7605                WHERE  inventory_item_id =
7606                                         v_lpn_serial_content.inventory_item_id
7607                AND    cycle_count_header_id = p_cycle_count_header_id;
7608 
7609                IF ( l_temp_count <> 0 ) THEN
7610                   /* Get the primary UOM for the serialized item */
7611                   SELECT primary_uom_code
7612                   INTO   l_temp_uom_code
7613                   FROM   mtl_system_items
7614                   WHERE  inventory_item_id =
7615                                         v_lpn_serial_content.inventory_item_id
7616                   AND    organization_id =
7617                                   v_lpn_serial_content.current_organization_id;
7618 
7619                   IF ( l_debug = 1 ) THEN
7620                      print_debug ( 'Counting an LPN single serial controlled item'
7621                                  );
7622                   END IF;
7623 
7624                   /*
7625                       ******  Fix for bug 4886188
7626                       ******  If the Lpn Context is 'Issued Out of Stores' or 'Intransit' or 'Packing Context' or 'Loaded to Dock'
7627                       ******  count quantity should be taken as 0.
7628                    */
7629 
7630                     IF ( p_parent_lpn_id IS NOT NULL ) THEN
7631 
7632                        SELECT
7633                               lpn_context
7634                        INTO   l_lpn_context
7635                        FROM   WMS_LICENSE_PLATE_NUMBERS
7636                        WHERE  lpn_id = p_parent_lpn_id ;
7637 
7638                        IF ( l_debug = 1 ) THEN
7639                             print_debug ( 'l_lpn_context: => ' || l_lpn_context );
7640                        END IF;
7641 
7642                        IF l_lpn_context = 8 or l_lpn_context = 9 or l_lpn_context = 4 or l_lpn_context = 6 or l_lpn_context = 11 THEN -- Added 11 status for bug 9559613
7643                           l_cnt_qty := 0 ;
7644                        ELSE
7645                           l_cnt_qty := 1 ;
7646                        END IF;
7647                    END IF;
7648                    /*  End of fix for bug number 4886188 */
7649 
7650 
7651                   process_entry ( p_cycle_count_header_id => p_cycle_count_header_id,
7652                                   p_organization_id   => p_organization_id,
7653                                   p_subinventory      => p_subinventory,
7654                                   p_locator_id        => p_locator_id,
7655                                   p_parent_lpn_id     => v_lpn_serial_content.lpn_id,
7656                                   p_inventory_item_id => v_lpn_serial_content.inventory_item_id,
7657                                   p_revision          => v_lpn_serial_content.revision,
7658                                   p_lot_number        => v_lpn_serial_content.lot_number,
7659                                   p_from_serial_number => v_lpn_serial_content.serial_number,
7660                                   p_to_serial_number  => v_lpn_serial_content.serial_number,
7661                                   p_count_quantity    =>  l_cnt_qty,  --Changed for Bug Number 4886188
7662                                   p_count_uom         => l_temp_uom_code,
7663                                   p_unscheduled_count_entry => p_unscheduled_count_entry,
7664                                   p_user_id           => p_user_id,
7665                                   p_cost_group_id     => v_lpn_serial_content.cost_group_id
7666                                 );
7667                END IF;
7668             END LOOP;
7669          ELSIF ( l_serial_count_option = 3 ) THEN
7670             -- Multiple Serial
7671             FOR v_lpn_multiple_serial IN lpn_multiple_serial_cursor
7672             LOOP
7673                -- Make sure that this inventory item is defined in the
7674                -- cycle count header item scope
7675                SELECT COUNT ( * )
7676                INTO   l_temp_count
7677                FROM   mtl_cycle_count_items
7678                WHERE  inventory_item_id =
7679                                        v_lpn_multiple_serial.inventory_item_id
7680                AND    cycle_count_header_id = p_cycle_count_header_id;
7681 
7682                IF ( l_temp_count <> 0 ) THEN
7683                   -- Mark all of the serials as present
7684                   IF ( l_debug = 1 ) THEN
7685                      print_debug ( 'Marking all of the multiple serials as present'
7686                                  );
7687                   END IF;
7688 
7689                   inv_cyc_serials.mark_all_present ( p_organization_id   => p_organization_id,
7690                                                      p_subinventory      => p_subinventory,
7691                                                      p_locator_id        => p_locator_id,
7692                                                      p_inventory_item_id => v_lpn_multiple_serial.inventory_item_id,
7693                                                      p_revision          => v_lpn_multiple_serial.revision,
7694                                                      p_lot_number        => v_lpn_multiple_serial.lot_number,
7695                                                      p_cycle_count_header_id => p_cycle_count_header_id,
7696                                                      p_parent_lpn_id     => v_lpn_multiple_serial.parent_lpn_id
7697                                                    );
7698 
7699                   IF ( l_debug = 1 ) THEN
7700                      print_debug ( 'Counting an LPN multiple serial controlled item'
7701                                  );
7702                   END IF;
7703 
7704                   process_entry ( p_cycle_count_header_id => p_cycle_count_header_id,
7705                                   p_organization_id   => p_organization_id,
7706                                   p_subinventory      => p_subinventory,
7707                                   p_locator_id        => p_locator_id,
7708                                   p_parent_lpn_id     => v_lpn_multiple_serial.parent_lpn_id,
7709                                   p_inventory_item_id => v_lpn_multiple_serial.inventory_item_id,
7710                                   p_revision          => v_lpn_multiple_serial.revision,
7711                                   p_lot_number        => v_lpn_multiple_serial.lot_number,
7712                                   p_from_serial_number => NULL,
7713                                   p_to_serial_number  => NULL,
7714                                   p_count_quantity    => NULL,
7715                                   p_count_uom         => NULL,
7716                                   p_unscheduled_count_entry => p_unscheduled_count_entry,
7717                                   p_user_id           => p_user_id,
7718                                   p_cost_group_id     => v_lpn_multiple_serial.cost_group_id
7719                                 );
7720                END IF;
7721             END LOOP;
7722          END IF;
7723       END LOOP;
7724       g_lpn_summary_count := FALSE ; --9452528.
7725    EXCEPTION
7726    WHEN OTHERS THEN
7727      IF ( l_debug = 1 ) THEN
7728          print_debug ( 'OTHERs Exception' );
7729       END IF;
7730      g_lpn_summary_count := FALSE; --9452528
7731    END process_summary;
7732 
7733    PROCEDURE inv_serial_info (
7734       p_from_serial_number IN VARCHAR2,
7735       p_to_serial_number IN VARCHAR2,
7736       x_prefix    OUT NOCOPY VARCHAR2,
7737       x_quantity  OUT NOCOPY VARCHAR2,
7738       x_from_number OUT NOCOPY VARCHAR2,
7739       x_to_number OUT NOCOPY VARCHAR2,
7740       x_errorcode OUT NOCOPY NUMBER
7741    )
7742    IS
7743       L_f_alp_part VARCHAR2 ( 30 );
7744       L_t_alp_part VARCHAR2 ( 30 );
7745       L_f_num_part VARCHAR2 ( 30 );
7746       L_t_num_part VARCHAR2 ( 30 );
7747       L_ser_col_val VARCHAR2 ( 30 );
7748       L_ser_col_num NUMBER;
7749       L_from_length NUMBER;
7750       L_to_length NUMBER;
7751       L_f_ser_num VARCHAR2 ( 30 );
7752       L_t_ser_num VARCHAR2 ( 30 );
7753       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
7754    BEGIN
7755       IF ( l_debug = 1 ) THEN
7756          print_debug ( '***inv_serial_info***' );
7757       END IF;
7758 
7759       x_errorcode := 0;
7760       L_f_ser_num := P_FROM_SERIAL_NUMBER;
7761       L_t_ser_num := P_TO_SERIAL_NUMBER;
7762       -- Get the lengths of the two serial numbers. If the to serial
7763       -- number is not specified copy from serial number to it.
7764       L_from_length := NVL ( LENGTH ( L_f_ser_num ), 0 );
7765       L_to_length := NVL ( LENGTH ( L_t_ser_num ), 0 );
7766 
7767       IF ( l_debug = 1 ) THEN
7768          print_debug ( 'L_from_length=' || L_from_length );
7769          print_debug ( 'L_to_length=' || L_to_length );
7770       END IF;
7771 
7772       IF ( L_from_length = 0 ) THEN
7773          FND_MESSAGE.SET_NAME ( 'INV', 'INV_QTYBTWN_NO_SERIAL' );
7774          FND_MSG_PUB.ADD;
7775          x_errorcode := 124;
7776       END IF;
7777 
7778       IF ( L_to_length = 0 ) THEN
7779          L_t_ser_num := L_f_ser_num;
7780          L_to_length := L_from_length;
7781       END IF;
7782 
7783       -- Split the given serial number into alpha
7784       -- prefix and numeric part.
7785 
7786       /* From Serial Number */
7787       L_ser_col_num := L_from_length;
7788 
7789       WHILE ( L_ser_col_num > 0 )
7790       LOOP
7791          L_ser_col_val := SUBSTR ( L_f_ser_num, L_ser_col_num, 1 );
7792 
7793          IF ASCII ( L_ser_col_val ) >= 48 AND ASCII ( L_ser_col_val ) <= 57 THEN
7794             L_f_num_part := L_ser_col_val || L_f_num_part;
7795          ELSE
7796             L_f_alp_part := SUBSTR ( L_f_ser_num, 1, L_ser_col_num );
7797             EXIT;
7798          END IF;
7799 
7800          L_ser_col_num := L_ser_col_num - 1;
7801       END LOOP;
7802 
7803       -- To Serial Number
7804       -- Values for 0 to 9 is corresponds to ASCII value 48 TO 57
7805       -- All other values are Non-numeric value
7806       L_ser_col_num := L_to_length;
7807 
7808       WHILE ( L_ser_col_num > 0 )
7809       LOOP
7810          L_ser_col_val := SUBSTR ( L_t_ser_num, L_ser_col_num, 1 );
7811 
7812          IF ASCII ( L_ser_col_val ) >= 48 AND ASCII ( L_ser_col_val ) <= 57 THEN
7813             L_t_num_part := L_ser_col_val || L_t_num_part;
7814          ELSE
7815             L_t_alp_part := SUBSTR ( L_t_ser_num, 1, L_ser_col_num );
7816             EXIT;
7817          END IF;
7818 
7819          L_ser_col_num := L_ser_col_num - 1;
7820       END LOOP;
7821 
7822       -- We compare the prefixes to see IF they are the same
7823 
7824       IF    ( L_f_alp_part <> L_t_alp_part )
7825          OR ( l_f_alp_part IS NULL AND l_t_alp_part IS NOT NULL )
7826          OR ( l_f_alp_part IS NOT NULL AND l_t_alp_part IS NULL ) THEN
7827          FND_MESSAGE.SET_NAME ( 'INV', 'INV_QTYBTWN_PFX' );
7828          FND_MSG_PUB.ADD;
7829          x_errorcode := 119;
7830       END IF;
7831 
7832       -- Check the lengths of the two serial numbers to make sure they
7833       -- match.
7834       IF ( L_from_length <> L_to_length ) THEN
7835          -- Message Name : INV_QTYBTWN_LGTH
7836          FND_MESSAGE.SET_NAME ( 'INV', 'INV_QTYBTWN_LGTH' );
7837          FND_MSG_PUB.ADD;
7838          x_errorcode := 120;
7839       END IF;
7840 
7841       -- Check whether the serial numbers are matched
7842       -- IF not, check the last character of serial number is character
7843       -- IF yes, return error message
7844 
7845       -- XXX checks only one
7846       IF L_f_ser_num <> L_t_ser_num THEN
7847          IF     ASCII ( SUBSTR ( L_f_ser_num, LENGTH ( L_f_ser_num ), 1 ) ) <
7848                                                                            48
7849             AND ASCII ( SUBSTR ( L_f_ser_num, LENGTH ( L_f_ser_num ), 1 ) ) >
7850                                                                             57 THEN
7851             FND_MESSAGE.SET_NAME ( 'INV', 'INV_QTYBTWN_LAST' );
7852             FND_MSG_PUB.ADD;
7853             x_errorcode := 121;
7854          END IF;
7855       END IF;
7856 
7857       -- Calculate the dIFference of serial numbers
7858       -- How many serial nos are there in the given range
7859       IF ( l_debug = 1 ) THEN
7860          print_debug ( 'L_t_num_part=' || L_t_num_part );
7861          print_debug ( 'L_f_num_part=' || L_f_num_part );
7862       END IF;
7863 
7864       -- Out variables
7865       X_Quantity  :=
7866             NVL ( TO_NUMBER ( L_t_num_part ), 0 )
7867           - NVL ( TO_NUMBER ( L_f_num_part ), 0 )
7868           + 1;
7869 
7870       IF ( X_Quantity <= 0 ) THEN
7871          --  Message Name : INV_QTYBTWN_NUM
7872          FND_MESSAGE.SET_NAME ( 'INV', 'INV_QTYBTWN_NUM' );
7873          FND_MSG_PUB.ADD;
7874          x_errorcode := 122;
7875       END IF;
7876 
7877       -- Check to make sure To serial number is greater than
7878       -- From serial number.
7879 
7880       X_PREFIX    := L_f_alp_part;
7881       X_FROM_NUMBER := L_f_num_part;
7882       X_TO_NUMBER := L_t_num_part;
7883    EXCEPTION
7884       WHEN OTHERS THEN
7885          x_errorcode := -1;
7886    END;
7887 
7888    PROCEDURE get_default_cost_group_id (
7889       p_organization_id IN NUMBER,
7890       p_subinventory IN VARCHAR2,
7891       x_out       OUT NOCOPY NUMBER
7892    )
7893    IS
7894       l_default_cost_group_id NUMBER;
7895       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
7896    BEGIN
7897       IF ( l_debug = 1 ) THEN
7898          print_debug ( '***get_default_cost_group_id***' );
7899       END IF;
7900 
7901       BEGIN
7902          SELECT default_cost_group_id
7903          INTO   l_default_cost_group_id
7904          FROM   mtl_secondary_inventories
7905          WHERE  organization_id = p_organization_id
7906          AND    secondary_inventory_name = p_subinventory;
7907       EXCEPTION
7908          WHEN NO_DATA_FOUND THEN
7909             l_default_cost_group_id := NULL;
7910       END;
7911 
7912       -- If there is no default at the sub level, get it at the org level
7913       IF ( l_default_cost_group_id IS NULL ) THEN
7914          SELECT NVL ( default_cost_group_id, -999 )
7915          INTO   l_default_cost_group_id
7916          FROM   mtl_parameters
7917          WHERE  organization_id = p_organization_id;
7918       END IF;
7919 
7920       -- Set the out parameters
7921       x_out       := l_default_cost_group_id;
7922    END get_default_cost_group_id;
7923 
7924    PROCEDURE get_cost_group_id (
7925       p_organization_id IN NUMBER,
7926       p_subinventory IN VARCHAR2,
7927       p_locator_id IN NUMBER,
7928       p_parent_lpn_id IN NUMBER,
7929       p_inventory_item_id IN NUMBER,
7930       p_revision  IN VARCHAR2,
7931       p_lot_number IN VARCHAR2,
7932       p_serial_number IN VARCHAR2,
7933       x_out       OUT NOCOPY NUMBER
7934    )
7935    IS
7936       l_cost_group_id NUMBER;
7937       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
7938    BEGIN
7939       IF ( l_debug = 1 ) THEN
7940          print_debug ( '***get_cost_group_id***' );
7941       END IF;
7942 
7943       IF ( p_serial_number IS NOT NULL ) THEN
7944          SELECT NVL ( cost_group_id, -999 )
7945          INTO   l_cost_group_id
7946          FROM   mtl_serial_numbers
7947          WHERE  serial_number = p_serial_number
7948          AND    inventory_item_id = p_inventory_item_id
7949          AND    NVL ( revision, '@@@@@' ) = NVL ( p_revision, '@@@@@' )
7950          AND    NVL ( lot_number, '@@@@@' ) = NVL ( p_lot_number, '@@@@@' )
7951          AND    current_organization_id = p_organization_id
7952          --AND current_subinventory_code = p_subinventory
7953          --AND NVL(current_locator_id, -99999) = NVL(p_locator_id, -99999)
7954          AND    NVL ( lpn_id, -99999 ) = NVL ( p_parent_lpn_id, -99999 );
7955       ELSIF ( p_parent_lpn_id IS NOT NULL ) THEN
7956       BEGIN
7957          SELECT DISTINCT NVL ( cost_group_id, -999 ) --bug3687177
7958          INTO   l_cost_group_id
7959          FROM   wms_lpn_contents
7960          WHERE  parent_lpn_id = p_parent_lpn_id
7961          AND    organization_id = p_organization_id
7962          AND    inventory_item_id = p_inventory_item_id
7963          AND    NVL ( revision, '@@@@@' ) = NVL ( p_revision, '@@@@@' )
7964          AND    NVL ( lot_number, '@@@@@' ) = NVL ( p_lot_number, '@@@@@' )
7965          AND    NVL ( serial_summary_entry, 2 ) = 2;
7966       EXCEPTION
7967         WHEN TOO_MANY_ROWS THEN
7968            IF ( l_debug = 1 ) THEN
7969                print_debug ( 'Too many rows returned in call to get_cost_group_id');
7970            END IF;
7971       END;
7972       ELSE
7973          SELECT DISTINCT NVL ( cost_group_id, -999 )
7974          INTO            l_cost_group_id
7975          FROM            MTL_ONHAND_QUANTITIES_DETAIL
7976          WHERE           inventory_item_id = p_inventory_item_id
7977          AND             NVL ( revision, '@@@@@' ) =
7978                                                    NVL ( p_revision, '@@@@@' )
7979          AND             NVL ( lot_number, '@@@@@' ) =
7980                                                  NVL ( p_lot_number, '@@@@@' )
7981          AND             organization_id = p_organization_id
7982          AND             subinventory_code = p_subinventory
7983          AND             NVL ( locator_id, -99999 ) =
7984                                                    NVL ( p_locator_id, -99999 )
7985          AND             NVL ( containerized_flag, 2 ) = 2;
7986       END IF;
7987 
7988       -- Set the out return variable
7989       x_out       := l_cost_group_id;
7990    EXCEPTION
7991       WHEN TOO_MANY_ROWS THEN
7992          IF ( l_debug = 1 ) THEN
7993             print_debug ( 'Too many rows returned in call to get_cost_group_id'
7994                         );
7995          END IF;
7996 
7997          x_out       := -999;
7998       WHEN NO_DATA_FOUND THEN
7999          IF ( l_debug = 1 ) THEN
8000             print_debug ( 'No cost group found for this item' );
8001          END IF;
8002 
8003          x_out       := -999;
8004    END get_cost_group_id;
8005 
8006    PROCEDURE ok_proc
8007    IS
8008       tmp_count NUMBER;
8009       serial_pos_adj_count NUMBER;
8010       serial_neg_adj_count NUMBER;
8011       cur_rec   NUMBER;
8012 
8013       CURSOR cc_multiple_entry_cursor
8014       IS
8015          SELECT *
8016          FROM   mtl_cc_serial_numbers
8017          WHERE  cycle_count_entry_id = g_cc_entry.cycle_count_entry_id;
8018 
8019       l_group_mark_id NUMBER;
8020       l_serial_adjustment_option NUMBER;
8021       l_approval_tolerance_positive NUMBER;
8022       l_approval_tolerance_negative NUMBER;
8023       l_cost_tolerance_positive NUMBER;
8024       l_cost_tolerance_negative NUMBER;
8025       l_number_of_counts NUMBER;
8026       l_unit_status NUMBER;
8027       l_num_counts NUMBER := g_cc_entry.number_of_counts;
8028       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
8029       --Bug 5186993
8030       l_automatic_recount_flag NUMBER;
8031       l_maximum_auto_recounts  NUMBER;
8032       l_days_until_late        NUMBER;
8033       --Bug 6978840
8034       l_approval_option_code   NUMBER;
8035       l_system_quantity NUMBER; --15928706
8036 
8037 
8038    BEGIN
8039       IF ( l_debug = 1 ) THEN
8040          print_debug ( '***ok_proc***' );
8041       END IF;
8042 
8043       tmp_count   := 0;
8044       g_serial_out_tolerance := FALSE;
8045       -- There is never a positive serial adjustment since
8046       -- new serials will never be counted in mobile multiple
8047       -- serial counting.  That is not being allowed at this time.
8048       serial_pos_adj_count := 0;
8049       serial_neg_adj_count := 0;
8050       g_count_entry_status_code := g_cc_entry.entry_status_code;
8051       OPEN cc_multiple_entry_cursor;
8052 
8053       LOOP
8054          -- Note that here, all we are doing is calculating the values for
8055          -- serial_pos_adj_count, serial_neg_adj_count, and tmp_count in this loop.
8056          FETCH cc_multiple_entry_cursor INTO g_cc_serial_entry;
8057          EXIT WHEN cc_multiple_entry_cursor%NOTFOUND;
8058 
8059          IF ( l_debug = 1 ) THEN
8060             print_debug (    'Current serial processing: '
8061                           || g_cc_serial_entry.serial_number
8062                         );
8063          END IF;
8064 
8065          -- For each multiple serial entry, get the
8066          -- serial number's group mark ID to determine if it was
8067          -- counted as present or not
8068          SELECT group_mark_id
8069          INTO   l_group_mark_id
8070          FROM   mtl_serial_numbers
8071          WHERE  serial_number = g_cc_serial_entry.serial_number
8072          AND    current_organization_id = g_cc_entry.organization_id
8073          AND    inventory_item_id = g_cc_entry.inventory_item_id;
8074 
8075          IF ( l_group_mark_id = 1 ) THEN
8076             -- We can not count the serial number towards the total count
8077             -- if the serial number was marked as not present.
8078             tmp_count   := tmp_count + 1;
8079             l_unit_status := 1;
8080          ELSE
8081             l_unit_status := 2;
8082          END IF;
8083 
8084          IF ( l_debug = 1 ) THEN
8085             print_debug (    'Serial status for: '
8086                           || g_cc_serial_entry.serial_number
8087                           || ' = '
8088                           || l_unit_status
8089                         );
8090          END IF;
8091 
8092          -- Find out the number of serial numbers user claims as missing and also
8093          -- find out the number of new serial numbers user finds during the count
8094          -- Note that in mobile multiple cycle counting, we are currently NOT
8095          -- allowing unscheduled multiple serial entries
8096          IF (     ( g_cc_serial_entry.unit_status_current <>
8097                                            g_cc_serial_entry.unit_status_prior
8098                   )
8099               OR ( g_cc_serial_entry.unit_status_first IS NULL )
8100             ) THEN
8101             -- Since we do not allow new multiple serials to be found yet for
8102             -- mobile, serial_pos_adj_count will always be the initial value of
8103             -- 0.  If this serial entry was new, then we would increment
8104             -- the parameter serial_pos_adj_count
8105             IF ( l_unit_status = 2 ) THEN
8106                serial_neg_adj_count := serial_neg_adj_count - 1;
8107             END IF;
8108          END IF;
8109 
8110          -- Update the multiple serial info
8111          -- Modified the following for the Bug 4564346
8112          --l_number_of_counts := NVL ( g_cc_serial_entry.number_of_counts, 0 ) + 1;
8113          l_number_of_counts := NVL ( g_cc_serial_entry.number_of_counts,1);
8114 
8115          IF ( l_number_of_counts = 1 ) THEN
8116             -- First count
8117             g_cc_serial_entry.unit_status_current := l_unit_status;
8118             g_cc_serial_entry.unit_status_first := l_unit_status;
8119          ELSIF ( l_number_of_counts > 1 ) THEN
8120             g_cc_serial_entry.unit_status_prior :=
8121                                         g_cc_serial_entry.unit_status_current;
8122             g_cc_serial_entry.unit_status_current := l_unit_status;
8123          END IF;
8124 
8125          -- Following condition will only be true for new
8126          -- serial numbers created at the recount stage.
8127          IF ( l_number_of_counts <= l_num_counts ) THEN
8128             l_number_of_counts := l_num_counts + 1;
8129          END IF;
8130 
8131          g_cc_serial_entry.number_of_counts := l_number_of_counts;
8132       END LOOP;
8133 
8134       CLOSE cc_multiple_entry_cursor;
8135       -- Set the global count and UOM values for multiple serials
8136       -- since this is not initially set
8137       g_count_quantity := tmp_count;
8138 
8139       SELECT primary_uom_code
8140       INTO   g_count_uom
8141       FROM   mtl_system_items
8142       WHERE  inventory_item_id = g_cc_entry.inventory_item_id
8143       AND    organization_id = g_cc_entry.organization_id;
8144 
8145       -- Get the serial adjustment option for the cycle count header
8146       -- Bug 5186993
8147       SELECT NVL ( serial_adjustment_option, 2 ), NVL ( automatic_recount_flag, 2 ),
8148              NVL ( maximum_auto_recounts, 0 ), NVL ( days_until_late , 0 ),
8149              --Bug 6978840
8150              NVL( approval_option_code , 3)
8151       INTO   l_serial_adjustment_option, l_automatic_recount_flag, l_maximum_auto_recounts, l_days_until_late, l_approval_option_code
8152       FROM   mtl_cycle_count_headers
8153       WHERE  cycle_count_header_id = g_cc_entry.cycle_count_header_id;
8154 
8155       -- The user has selected 'Adjust if Possible' option for the serial items
8156       IF ( l_debug = 1 ) THEN
8157          print_debug (    'Multiple serial adjustment option: '
8158                        || l_serial_adjustment_option
8159                      );
8160       END IF;
8161 
8162       IF ( l_serial_adjustment_option <> 2 ) THEN
8163          -- Populate appropriate cycle count level tolerance information
8164          get_tolerances ( pre_approve_flag    => 'SERIAL',
8165                           x_approval_tolerance_positive => l_approval_tolerance_positive,
8166                           x_approval_tolerance_negative => l_approval_tolerance_negative,
8167                           x_cost_tolerance_positive => l_cost_tolerance_positive,
8168                           x_cost_tolerance_negative => l_cost_tolerance_negative
8169                         );
8170 
8171          -- If user found new serial numbers during his counting then find out
8172          -- if total number of new serial numbers are within allowable tolerance or not.
8173          IF ( l_debug = 1 ) THEN
8174             print_debug ( 'Serial Pos Adj Count: ' || serial_pos_adj_count );
8175          END IF;
8176 
8177          IF ( serial_pos_adj_count <> 0 ) THEN
8178             serial_tolerance_logic ( p_serial_adj_qty    => serial_pos_adj_count,
8179                                      p_app_tol_pos       => l_approval_tolerance_positive,
8180                                      p_app_tol_neg       => l_approval_tolerance_negative,
8181                                      p_cost_tol_pos      => l_cost_tolerance_positive,
8182                                      p_cost_tol_neg      => l_cost_tolerance_negative
8183                                    );
8184          END IF;
8185 
8186          -- If user found some serial numbers missing and we are still within
8187          -- adjustment tolerance then find out if we are within allowable
8188          -- negative tolerance or not.
8189          IF ( l_debug = 1 ) THEN
8190             print_debug ( 'Serial Neg Adj Count: ' || serial_neg_adj_count );
8191          END IF;
8192 
8193          IF ( serial_neg_adj_count <> 0 AND g_serial_out_tolerance = FALSE ) THEN
8194             serial_tolerance_logic ( p_serial_adj_qty    => serial_neg_adj_count,
8195                                      p_app_tol_pos       => l_approval_tolerance_positive,
8196                                      p_app_tol_neg       => l_approval_tolerance_negative,
8197                                      p_cost_tol_pos      => l_cost_tolerance_positive,
8198                                      p_cost_tol_neg      => l_cost_tolerance_negative
8199                                    );
8200          END IF;
8201       END IF;
8202 
8203       -- For existing records
8204       IF ( l_debug = 1 ) THEN
8205          print_debug ( 'Looping again for multiple existing serial entries' );
8206       END IF;
8207 
8208       OPEN cc_multiple_entry_cursor;
8209 
8210       LOOP
8211          FETCH cc_multiple_entry_cursor INTO g_cc_serial_entry;
8212          EXIT WHEN cc_multiple_entry_cursor%NOTFOUND;
8213 
8214          -- For each multiple serial entry, get the
8215          -- serial number's group mark ID to determine if it was
8216          -- counted as present or not.  We have to do this again since
8217          -- the updating in the same cursor used previously does not save
8218          -- and we do not want to necessarily do a commit yet.
8219          SELECT group_mark_id
8220          INTO   l_group_mark_id
8221          FROM   mtl_serial_numbers
8222          WHERE  serial_number = g_cc_serial_entry.serial_number
8223          AND    current_organization_id = g_cc_entry.organization_id
8224          AND    inventory_item_id = g_cc_entry.inventory_item_id;
8225 
8226          IF ( l_group_mark_id = 1 ) THEN
8227             l_unit_status := 1;
8228          ELSE
8229             l_unit_status := 2;
8230          END IF;
8231 
8232          -- Update the multiple serial info
8233          l_number_of_counts :=
8234                                  NVL ( g_cc_serial_entry.number_of_counts, 0 )
8235                                + 1;
8236 
8237          IF ( l_number_of_counts = 1 ) THEN
8238             -- First count
8239             g_cc_serial_entry.unit_status_current := l_unit_status;
8240             g_cc_serial_entry.unit_status_first := l_unit_status;
8241          ELSIF ( l_number_of_counts > 1 ) THEN
8242             g_cc_serial_entry.unit_status_prior :=
8243                                         g_cc_serial_entry.unit_status_current;
8244             g_cc_serial_entry.unit_status_current := l_unit_status;
8245          END IF;
8246 
8247          -- Following condition will only be true for new serial numbers
8248          -- created at the recount stage.
8249          IF ( l_number_of_counts <= l_num_counts ) THEN
8250             l_number_of_counts := l_num_counts + 1;
8251          END IF;
8252 
8253          -- Update the number of counts
8254          g_cc_serial_entry.number_of_counts := l_number_of_counts;
8255 
8256          IF ( l_debug = 1 ) THEN
8257             print_debug (    'Serial number processed: '
8258                           || g_cc_serial_entry.serial_number
8259                         );
8260             print_debug (    'number of counts: '
8261                           || g_cc_serial_entry.number_of_counts
8262                         );
8263             print_debug (    'current status: '
8264                           || g_cc_serial_entry.unit_status_current
8265                         );
8266             print_debug (    'prior status: '
8267                           || g_cc_serial_entry.unit_status_prior
8268                         );
8269             print_debug (    'first status: '
8270                           || g_cc_serial_entry.unit_status_first
8271                         );
8272          END IF;
8273 
8274          IF ( g_cc_serial_entry.unit_status_current =
8275                               NVL ( g_cc_serial_entry.unit_status_prior, -999 )
8276             ) THEN
8277             IF ( l_debug = 1 ) THEN
8278                print_debug ( 'Status of serial is unchanged' );
8279             END IF;
8280 
8281             g_serial_entry_status_code := 5;
8282             count_entry_status_code ( );
8283             update_serial_row ( );
8284          ELSIF ( g_cc_serial_entry.unit_status_current <>
8285                               NVL ( g_cc_serial_entry.unit_status_prior, -999 )
8286                ) THEN
8287             IF ( l_debug = 1 ) THEN
8288                print_debug ( 'Status of serial has changed since last counted'
8289                            );
8290             END IF;
8291 
8292             -- Dont need to mark the serial since the serial has already
8293             -- been marked with a group mark ID of 1 if it was counted as
8294             -- present for multiple serial count option.  I'm also assuming
8295             -- that if the serial is unmarked, it will have a null value or
8296             -- non-positive value for the group mark ID for that serial number
8297             --mark();
8298             existing_serial_number ( );
8299             count_entry_status_code ( );
8300             update_serial_row ( );
8301          END IF;
8302 
8303          -- Bug# 2379201
8304          -- If the serial was counted as present, then we should unmark the
8305          -- serial here since we don't need that information anymore.
8306          -- The group mark ID was marked with a value of 1 only to indicate
8307          -- that it was present and so no adjustment needs to be made for that
8308          -- serial.  Therefore there is no need to mark it.
8309          IF ( g_cc_serial_entry.unit_status_current = 1 ) THEN
8310             IF ( l_debug = 1 ) THEN
8311                print_debug (    'Unmarking the serial found present: '
8312                              || g_cc_serial_entry.serial_number
8313                            );
8314             END IF;
8315 
8316             UPDATE mtl_serial_numbers
8317             SET group_mark_id = NULL
8318             WHERE  serial_number = g_cc_serial_entry.serial_number
8319             AND    current_organization_id = g_cc_entry.organization_id
8320             AND    inventory_item_id = g_cc_entry.inventory_item_id;
8321          END IF;
8322       END LOOP;
8323 
8324       CLOSE cc_multiple_entry_cursor;
8325 
8326       -- This is based on the assumption that only Approver can request a recount in case of
8327       -- Multiple Serial Number per count. In that case after recount is done and no changes
8328       -- are made, we need to send the count back for approval.
8329                  print_debug('g_cc_entry.adjustment_quantity :  '|| g_cc_entry.adjustment_quantity)   ;
8330                  print_debug('g_cc_entry.number_of_counts :  ' || g_cc_entry.number_of_counts)   ;
8331                  print_debug('l_maximum_auto_recounts :  ' || l_maximum_auto_recounts)   ;
8332                  print_debug('g_count_entry_status_code :  ' || g_count_entry_status_code)   ;
8333                  print_debug('g_count_quantity :  '|| g_count_quantity)   ;
8334 
8335                 system_quantity ( x_system_quantity => l_system_quantity );
8336                 print_debug('l_system_quantity :  '|| l_system_quantity)   ;
8337 
8338 --start 15928706
8339        if ( nvl(g_count_quantity,0) = nvl(l_system_quantity,0)
8340            and  nvl(g_cc_entry.number_of_counts, 0) = nvl(l_maximum_auto_recounts, 0)
8341            and  g_count_entry_status_code = 3)  THEN
8342 			    g_count_entry_status_code:=5;
8343           g_serial_entry_status_code:=5;
8344       end if;
8345 --end 15928706
8346 
8347 
8348       IF ( g_count_entry_status_code = 3 ) THEN
8349          g_count_entry_status_code := 2;
8350       END IF;
8351 
8352         IF ( l_debug = 1 ) THEN
8353            print_debug ( 'l_automatic_recount_flag = '||l_automatic_recount_flag||', g_cc_entry.number_of_counts = '||g_cc_entry.number_of_counts||', l_maximum_auto_recounts = '||l_maximum_auto_recounts);
8354          END IF;
8355 
8356          -- Bug 5186993, if automatic recount is set, check whether the adjustment has been
8357          -- counted the maximum number of times, if not setting for recount
8358          -- Bug 6978840 , checking if the approval option is 'If out of tolerance' and tolerance is not met
8359          -- Bug 13898802 , checking for 'review all adjustments' and adjustment_quantity not equal to zero
8360     if(l_approval_option_code = 3 and g_serial_out_tolerance = TRUE  )
8361      OR ( l_serial_adjustment_option = 2    and   nvl(g_cc_entry.adjustment_quantity,0) <> 0 ) THEN
8362          if ( l_automatic_recount_flag = 1 AND nvl(g_cc_entry.number_of_counts, 0) < l_maximum_auto_recounts ) THEN
8363                 IF ( l_debug = 1 ) THEN
8364                    print_debug ( 'ok_proc: Setting to recount' );
8365                 END IF;
8366                 g_count_entry_status_code := 3;
8367                 g_cc_entry.count_due_date := SYSDATE + l_days_until_late;
8368          end if;
8369     end if;
8370 
8371       get_final_count_info ( );
8372    END ok_proc;
8373 
8374    PROCEDURE serial_tolerance_logic (
8375       p_serial_adj_qty IN NUMBER,
8376       p_app_tol_pos IN NUMBER,
8377       p_app_tol_neg IN NUMBER,
8378       p_cost_tol_pos IN NUMBER,
8379       p_cost_tol_neg IN NUMBER
8380    )
8381    IS
8382       l_system_quantity NUMBER;
8383       l_adjustment_value NUMBER;
8384       l_approval_option_code NUMBER;
8385       l_item_cost NUMBER;
8386       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
8387    BEGIN
8388       IF ( l_debug = 1 ) THEN
8389          print_debug ( '***serial_tolerance_logic***' );
8390       END IF;
8391 
8392       -- Get the cycle count headers approval option code
8393       SELECT NVL ( approval_option_code, 1 )
8394       INTO   l_approval_option_code
8395       FROM   mtl_cycle_count_headers
8396       WHERE  cycle_count_header_id = g_cc_entry.cycle_count_header_id
8397       AND    organization_id = g_cc_entry.organization_id;
8398 
8399       -- Get the system quantity
8400       system_quantity ( x_system_quantity => l_system_quantity );
8401       g_serial_out_tolerance := FALSE;
8402       -- Get the item cost
8403       l_item_cost :=
8404          get_item_cost ( in_org_id           => g_cc_entry.organization_id,
8405                          in_item_id          => g_cc_entry.inventory_item_id,
8406                          in_locator_id       => g_cc_entry.locator_id
8407                        );
8408       g_cc_entry.item_unit_cost := l_item_cost;
8409       -- Calculate the adjustment value
8410       l_adjustment_value := l_item_cost * p_serial_adj_qty;
8411 
8412       IF ( l_debug = 1 ) THEN
8413             print_debug ( 'Value : l_system_quantity ' || l_system_quantity );
8414             print_debug ( 'Value : p_serial_adj_qty ' || p_serial_adj_qty );
8415             print_debug ( 'Value : p_app_tol_neg ' || p_app_tol_neg );
8416             print_debug ( 'Value : p_cost_tol_neg ' || p_cost_tol_neg );
8417             print_debug ( 'Value : p_app_tol_pos ' || p_app_tol_pos );
8418       END IF;
8419 
8420 
8421       IF ( l_approval_option_code = 1 ) THEN
8422          -- Approval_option = always
8423          g_serial_out_tolerance := TRUE;
8424       ELSIF ( l_approval_option_code = 2 ) THEN
8425          -- Approval option = never
8426          g_serial_out_tolerance := FALSE;
8427       ELSE
8428          -- Approval option = required if out of tolerance
8429          IF ( l_system_quantity <> 0 ) THEN
8430             IF ( p_serial_adj_qty < 0 ) THEN
8431                IF (      ( p_app_tol_neg IS NOT NULL and p_app_tol_neg <> -1 ) /* added -1 constraint for bug 4926279 */
8432                     AND ( ABS (  ( p_serial_adj_qty / l_system_quantity )
8433                                 * 100 ) > p_app_tol_neg
8434                         )
8435                   ) THEN
8436                   g_serial_out_tolerance := TRUE;
8437                ELSE
8438                   IF (      ( p_cost_tol_neg IS NOT NULL and p_cost_tol_neg <> -1 )  /* added -1 constraint for bug 4926279 */
8439                        AND ( ABS ( l_adjustment_value ) > p_cost_tol_neg )
8440                      ) THEN
8441                      g_serial_out_tolerance := TRUE;
8442                   ELSE
8443                      g_serial_out_tolerance := FALSE;
8444                   END IF;
8445                END IF;
8446             ELSE -- p_serial_adj_qty >= 0
8447                IF (      ( p_app_tol_pos IS NOT NULL  and p_app_tol_pos <> -1)   /* added -1 constraint for bug 4926279 */
8448                     AND ( ABS (  ( p_serial_adj_qty / l_system_quantity )
8449                                 * 100 ) > p_app_tol_pos
8450                         )
8451                   ) THEN
8452                   g_serial_out_tolerance := TRUE;
8453                ELSE
8454                   IF (     p_cost_tol_pos IS NOT NULL  and p_cost_tol_pos <> -1    /* added -1 constraint for bug 4926279 */
8455                        AND ( ABS ( l_adjustment_value ) > p_cost_tol_pos )
8456                      ) THEN
8457                      g_serial_out_tolerance := TRUE;
8458                   ELSE
8459                      g_serial_out_tolerance := FALSE;
8460                   END IF;
8461                END IF;
8462             END IF;
8463          ELSE -- system quantity = 0
8464             IF ( p_app_tol_pos IS NOT NULL and p_app_tol_pos <> -1) THEN    /* added -1 constraint for bug 4926279 */
8465                g_serial_out_tolerance := TRUE;
8466             ELSE
8467                IF (      ( p_cost_tol_pos IS NOT NULL  and p_cost_tol_pos <> -1)  /* added -1 constraint for bug 4926279 */
8468                     AND ( l_adjustment_value > p_cost_tol_pos )
8469                   ) THEN
8470                   g_serial_out_tolerance := TRUE;
8471                ELSE
8472                   g_serial_out_tolerance := FALSE;
8473                END IF;
8474             END IF;
8475          END IF;
8476       END IF;
8477 
8478       IF ( g_serial_out_tolerance ) THEN
8479          IF ( l_debug = 1 ) THEN
8480             print_debug ( 'g_serial_out_tolerance: TRUE' );
8481          END IF;
8482       ELSE
8483          IF ( l_debug = 1 ) THEN
8484             print_debug ( 'g_serial_out_tolerance: FALSE' );
8485          END IF;
8486       END IF;
8487    END serial_tolerance_logic;
8488 
8489    PROCEDURE get_final_count_info
8490    IS
8491       l_entry_status_code NUMBER;
8492       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
8493    BEGIN
8494       IF ( l_debug = 1 ) THEN
8495          print_debug ( '***get_final_count_info***' );
8496       END IF;
8497 
8498       g_cc_entry.entry_status_code := g_count_entry_status_code;
8499       l_entry_status_code := g_cc_entry.entry_status_code;
8500 
8501       IF ( l_entry_status_code = 5 ) THEN
8502          -- Count complete
8503          g_cc_entry.approval_date := SYSDATE;
8504       END IF;
8505    END get_final_count_info;
8506 
8507    PROCEDURE get_scheduled_entry (
8508       p_cycle_count_header_id IN NUMBER,
8509       x_count     OUT NOCOPY NUMBER
8510    )
8511    IS
8512       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
8513    BEGIN
8514       IF ( l_debug = 1 ) THEN
8515          print_debug ( '***get_scheduled_entry***' );
8516       END IF;
8517 
8518       SELECT COUNT ( * )
8519       INTO   x_count
8520       FROM   mtl_cycle_count_entries
8521       WHERE  cycle_count_header_id = p_cycle_count_header_id
8522       AND    entry_status_code IN ( 1, 3 )
8523       AND    NVL ( export_flag, 2 ) = 2;
8524    END get_scheduled_entry;
8525 
8526 -- This is a wrapper to call inventory INV_LOT_API_PUB.insertLot
8527 -- it stores the inserted lot info in a global variable for
8528 -- transaction exception rollback
8529    PROCEDURE insert_dynamic_lot (
8530       p_api_version IN NUMBER,
8531       p_init_msg_list IN VARCHAR2,
8532       p_commit    IN VARCHAR2,
8533       p_validation_level IN NUMBER,
8534       p_inventory_item_id IN NUMBER,
8535       p_organization_id IN NUMBER,
8536       p_lot_number IN VARCHAR2,
8537       p_expiration_date IN OUT NOCOPY DATE,
8538       p_transaction_temp_id IN NUMBER,
8539       p_transaction_action_id IN NUMBER,
8540       p_transfer_organization_id IN NUMBER,
8541       p_status_id IN NUMBER,
8542       p_update_status IN VARCHAR2,
8543       x_object_id OUT NOCOPY NUMBER,
8544       x_return_status OUT NOCOPY VARCHAR2,
8545       x_msg_count OUT NOCOPY NUMBER,
8546       x_msg_data  OUT NOCOPY VARCHAR2
8547    )
8548    IS
8549       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
8550    BEGIN
8551       -- Initialize the return status
8552       x_return_status := FND_API.G_RET_STS_SUCCESS;
8553 
8554       IF ( l_debug = 1 ) THEN
8555          print_debug ( '***Calling insert_dynamic_lot***' );
8556       END IF;
8557 
8558       IF ( l_debug = 1 ) THEN
8559          print_debug ( 'Calling insertlot' );
8560       END IF;
8561 
8562       inv_lot_api_pub.insertlot ( p_api_version       => p_api_version,
8563                                   p_init_msg_list     => p_init_msg_list,
8564                                   p_commit            => p_commit,
8565                                   p_validation_level  => p_validation_level,
8566                                   p_inventory_item_id => p_inventory_item_id,
8567                                   p_organization_id   => p_organization_id,
8568                                   p_lot_number        => p_lot_number,
8569                                   p_expiration_date   => p_expiration_date,
8570                                   p_transaction_temp_id => p_transaction_temp_id,
8571                                   p_transaction_action_id => p_transaction_action_id,
8572                                   p_transfer_organization_id => p_transfer_organization_id,
8573                                   x_object_id         => x_object_id,
8574                                   x_return_status     => x_return_status,
8575                                   x_msg_count         => x_msg_count,
8576                                   x_msg_data          => x_msg_data
8577                                 );
8578 
8579       IF ( x_return_status <> fnd_api.g_ret_sts_success ) THEN
8580          IF ( l_debug = 1 ) THEN
8581             print_debug ( 'insertLot was not called successfully' );
8582          END IF;
8583 
8584          RAISE FND_API.G_EXC_ERROR;
8585       END IF;
8586 
8587       IF ( l_debug = 1 ) THEN
8588          print_debug ( 'insertLot was called successfully' );
8589       END IF;
8590 
8591       IF (      ( x_return_status = FND_API.g_ret_sts_success )
8592            AND ( p_update_status = 'TRUE' )
8593          ) THEN
8594          IF ( l_debug = 1 ) THEN
8595             print_debug ( 'Update the status of the new lot' );
8596          END IF;
8597 
8598          inv_material_status_grp.update_status ( p_api_version_number => p_api_version,
8599                                                  p_init_msg_lst      => NULL,
8600                                                  x_return_status     => x_return_status,
8601                                                  x_msg_count         => x_msg_count,
8602                                                  x_msg_data          => x_msg_data,
8603                                                  p_update_method     => inv_material_status_pub.g_update_method_receive,
8604                                                  p_status_id         => p_status_id,
8605                                                  p_organization_id   => p_organization_id,
8606                                                  p_inventory_item_id => p_inventory_item_id,
8607                                                  p_sub_code          => NULL,
8608                                                  p_locator_id        => NULL,
8609                                                  p_lot_number        => p_lot_number,
8610                                                  p_serial_number     => NULL,
8611                                                  p_to_serial_number  => NULL,
8612                                                  p_object_type       => 'O'
8613                                                );
8614 
8615          IF ( x_return_status <> fnd_api.g_ret_sts_success ) THEN
8616             IF ( l_debug = 1 ) THEN
8617                print_debug ( 'update_status was not called successfully' );
8618             END IF;
8619 
8620             RAISE FND_API.G_EXC_ERROR;
8621          END IF;
8622       END IF;
8623    END insert_dynamic_lot;
8624 
8625    PROCEDURE update_serial_status (
8626       p_api_version IN NUMBER,
8627       p_init_msg_list IN VARCHAR2,
8628       p_commit    IN VARCHAR2,
8629       p_validation_level IN NUMBER,
8630       p_inventory_item_id IN NUMBER,
8631       p_organization_id IN NUMBER,
8632       p_from_serial_number IN VARCHAR2,
8633       p_to_serial_number IN VARCHAR2,
8634       p_current_status IN NUMBER,
8635       p_serial_status_id IN NUMBER,
8636       p_update_serial_status IN VARCHAR2,
8637       p_lot_number IN VARCHAR2,
8638       x_return_status OUT NOCOPY VARCHAR2,
8639       x_msg_count OUT NOCOPY NUMBER,
8640       x_msg_data  OUT NOCOPY VARCHAR2
8641    )
8642    IS
8643       l_from_ser_number NUMBER;
8644       l_to_ser_number NUMBER;
8645       l_range_numbers NUMBER;
8646       l_temp_prefix VARCHAR2 ( 30 );
8647       l_cur_serial_number VARCHAR2 ( 30 );
8648       l_cur_ser_number NUMBER;
8649       l_serial_num_length NUMBER;
8650       l_prefix_length NUMBER;
8651       l_progress VARCHAR2 ( 10 );
8652       l_success NUMBER;
8653       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
8654    BEGIN
8655       IF ( l_debug = 1 ) THEN
8656          print_debug (    'Enter update_serial_status: 10:'
8657                        || TO_CHAR ( SYSDATE, 'YYYY-MM-DD HH:DD:SS' )
8658                      );
8659       END IF;
8660 
8661       l_progress  := '10';
8662       x_return_status := FND_API.G_RET_STS_SUCCESS;
8663       SAVEPOINT count_update_serial_sp;
8664       l_progress  := '20';
8665       -- get the number part of the 'to' serial
8666       inv_validate.number_from_sequence ( p_to_serial_number,
8667                                           l_temp_prefix,
8668                                           l_to_ser_number
8669                                         );
8670       l_progress  := '30';
8671       -- get the number part of the 'from' serial
8672       inv_validate.number_from_sequence ( p_from_serial_number,
8673                                           l_temp_prefix,
8674                                           l_from_ser_number
8675                                         );
8676       l_progress  := '40';
8677       -- total number of serials inserted into mtl_serial_numbers
8678       l_range_numbers := l_to_ser_number - l_from_ser_number + 1;
8679       l_serial_num_length := LENGTH ( p_from_serial_number );
8680       l_prefix_length := LENGTH ( l_temp_prefix );
8681 
8682       FOR i IN 1 .. l_range_numbers
8683       LOOP
8684          l_cur_ser_number := l_from_ser_number + i - 1;
8685          -- concatenate the serial number to be inserted
8686          l_cur_serial_number :=
8687                 l_temp_prefix
8688              || LPAD ( l_cur_ser_number,
8689                         l_serial_num_length - l_prefix_length,
8690                        '0'
8691                      );
8692          l_progress  := '50';
8693 
8694          UPDATE mtl_serial_numbers
8695          SET previous_status = current_status,
8696              current_status = p_current_status,
8697              lot_number = p_lot_number,
8698              current_organization_id = p_organization_id
8699          WHERE  serial_number = l_cur_serial_number
8700          AND    inventory_item_id = p_inventory_item_id;
8701 
8702          l_progress  := '60';
8703 
8704          IF p_update_serial_status = 'TRUE' THEN
8705             l_progress  := '70';
8706             inv_material_status_grp.update_status ( p_api_version_number => p_api_version,
8707                                                     p_init_msg_lst      => NULL,
8708                                                     x_return_status     => x_return_status,
8709                                                     x_msg_count         => x_msg_count,
8710                                                     x_msg_data          => x_msg_data,
8711                                                     p_update_method     => inv_material_status_pub.g_update_method_receive,
8712                                                     p_status_id         => p_serial_status_id,
8713                                                     p_organization_id   => p_organization_id,
8714                                                     p_inventory_item_id => p_inventory_item_id,
8715                                                     p_sub_code          => NULL,
8716                                                     p_locator_id        => NULL,
8717                                                     p_lot_number        => p_lot_number,
8718                                                     p_serial_number     => l_cur_serial_number,
8719                                                     p_to_serial_number  => NULL,
8720                                                     p_object_type       => 'S'
8721                                                   );
8722          END IF;
8723 
8724          l_progress  := '80';
8725 
8726          IF x_return_status <> fnd_api.g_ret_sts_success THEN
8727             RAISE FND_API.G_EXC_ERROR;
8728          END IF;
8729       END LOOP;
8730 
8731       l_progress  := '90';
8732       serial_check.inv_mark_serial ( from_serial_number  => p_from_serial_number,
8733                                      to_serial_number    => p_to_serial_number,
8734                                      item_id             => p_inventory_item_id,
8735                                      org_id              => p_organization_id,
8736                                      hdr_id              => NULL,
8737                                      temp_id             => NULL,
8738                                      lot_temp_id         => NULL,
8739                                      success             => l_success
8740                                    );
8741       l_progress  := '100';
8742 
8743       IF ( l_debug = 1 ) THEN
8744          print_debug (    'Exit update_serial_status 110:'
8745                        || TO_CHAR ( SYSDATE, 'YYYY-MM-DD HH:DD:SS' )
8746                      );
8747       END IF;
8748    EXCEPTION
8749       WHEN FND_API.G_EXC_ERROR THEN
8750          ROLLBACK TO count_update_serial_sp;
8751          x_return_status := FND_API.G_RET_STS_ERROR;
8752 
8753          IF ( l_debug = 1 ) THEN
8754             print_debug (    'Exitting update_serial_status - execution error:'
8755                           || l_progress
8756                           || ' '
8757                           || TO_CHAR ( SYSDATE, 'YYYY-MM-DD HH:DD:SS' )
8758                         );
8759          END IF;
8760 
8761          --  Get message count and data
8762          fnd_msg_pub.count_and_get ( p_encoded           => fnd_api.g_false,
8763                                      p_count             => x_msg_count,
8764                                      p_data              => x_msg_data
8765                                    );
8766       WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8767          ROLLBACK TO count_update_serial_sp;
8768          x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8769 
8770          IF ( l_debug = 1 ) THEN
8771             print_debug (    'Exitting update_serial_status - unexpected error:'
8772                           || l_progress
8773                           || ' '
8774                           || TO_CHAR ( SYSDATE, 'YYYY-MM-DD HH:DD:SS' )
8775                         );
8776          END IF;
8777 
8778          --  Get message count and data
8779          fnd_msg_pub.count_and_get ( p_encoded           => fnd_api.g_false,
8780                                      p_count             => x_msg_count,
8781                                      p_data              => x_msg_data
8782                                    );
8783       WHEN OTHERS THEN
8784          ROLLBACK TO count_update_serial_sp;
8785          x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8786 
8787          IF ( l_debug = 1 ) THEN
8788             print_debug (    'Exitting update_serial_status - other exceptions:'
8789                           || l_progress
8790                           || ' '
8791                           || TO_CHAR ( SYSDATE, 'YYYY-MM-DD HH:DD:SS' )
8792                         );
8793          END IF;
8794 
8795          IF SQLCODE IS NOT NULL THEN
8796             inv_mobile_helper_functions.sql_error ( 'INV_RCV_COMMON_APIS.update_serial_status',
8797                                                     l_progress,
8798                                                     SQLCODE
8799                                                   );
8800          END IF;
8801 
8802          IF fnd_msg_pub.check_msg_level ( fnd_msg_pub.g_msg_lvl_unexp_error ) THEN
8803             fnd_msg_pub.add_exc_msg ( g_pkg_name, 'update_serial_status' );
8804          END IF;
8805 
8806          --  Get message count and data
8807          fnd_msg_pub.count_and_get ( p_encoded           => fnd_api.g_false,
8808                                      p_count             => x_msg_count,
8809                                      p_data              => x_msg_data
8810                                    );
8811    END update_serial_status;
8812 
8813 -- This is a wrapper to call inventory insert_range_serial
8814    PROCEDURE insert_range_serial (
8815       p_api_version IN NUMBER,
8816       p_init_msg_list IN VARCHAR2,
8817       p_commit    IN VARCHAR2,
8818       p_validation_level IN NUMBER,
8819       p_inventory_item_id IN NUMBER,
8820       p_organization_id IN NUMBER,
8821       p_from_serial_number IN VARCHAR2,
8822       p_to_serial_number IN VARCHAR2,
8823       p_revision  IN VARCHAR2,
8824       p_lot_number IN VARCHAR2,
8825       p_current_status IN NUMBER,
8826       p_serial_status_id IN NUMBER,
8827       p_update_serial_status IN VARCHAR2,
8828       x_return_status OUT NOCOPY VARCHAR2,
8829       x_msg_count OUT NOCOPY NUMBER,
8830       x_msg_data  OUT NOCOPY VARCHAR2
8831    )
8832    IS
8833       l_object_id NUMBER;
8834       l_success NUMBER;
8835       l_progress VARCHAR2 ( 10 );
8836       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
8837    BEGIN
8838       IF ( l_debug = 1 ) THEN
8839          print_debug (    'Enter insert_range_serial: 10:'
8840                        || TO_CHAR ( SYSDATE, 'YYYY-MM-DD HH:DD:SS' )
8841                      );
8842       END IF;
8843 
8844       x_return_status := FND_API.G_RET_STS_SUCCESS;
8845       l_progress  := '10';
8846       SAVEPOINT count_insert_range_serial_sp;
8847       l_progress  := '20';
8848       inv_serial_number_pub.insert_range_serial ( p_api_version       => p_api_version,
8849                                                   p_init_msg_list     => p_init_msg_list,
8850                                                   p_commit            => p_commit,
8851                                                   p_validation_level  => p_validation_level,
8852                                                   p_inventory_item_id => p_inventory_item_id,
8853                                                   p_organization_id   => p_organization_id,
8854                                                   p_from_serial_number => p_from_serial_number,
8855                                                   p_to_serial_number  => p_to_serial_number,
8856                                                   p_initialization_date => SYSDATE,
8857                                                   p_completion_date   => NULL,
8858                                                   p_ship_date         => NULL,
8859                                                   p_revision          => p_revision,
8860                                                   p_lot_number        => p_lot_number,
8861                                                   p_current_locator_id => NULL,
8862                                                   p_subinventory_code => NULL,
8863                                                   p_trx_src_id        => NULL,
8864                                                   p_unit_vendor_id    => NULL,
8865                                                   p_vendor_lot_number => NULL,
8866                                                   p_vendor_serial_number => NULL,
8867                                                   p_receipt_issue_type => NULL,
8868                                                   p_txn_src_id        => NULL,
8869                                                   p_txn_src_name      => NULL,
8870                                                   p_txn_src_type_id   => NULL,
8871                                                   p_transaction_id    => NULL,
8872                                                   p_current_status    => p_current_status,
8873                                                   p_parent_item_id    => NULL,
8874                                                   p_parent_serial_number => NULL,
8875                                                   p_cost_group_id     => NULL,
8876                                                   p_transaction_action_id => NULL,
8877                                                   p_transaction_temp_id => NULL,
8878                                                   p_status_id         => NULL,
8879                                                   p_inspection_status => NULL,
8880                                                   x_object_id         => l_object_id,
8881                                                   x_return_status     => x_return_status,
8882                                                   x_msg_count         => x_msg_count,
8883                                                   x_msg_data          => x_msg_data
8884                                                 );
8885 
8886       IF x_return_status <> fnd_api.g_ret_sts_success THEN
8887          FND_MESSAGE.SET_NAME ( 'INV', 'INV_LOT_COMMIT_FAILURE' );
8888          FND_MSG_PUB.ADD;
8889          RAISE FND_API.G_EXC_ERROR;
8890       END IF;
8891 
8892       l_progress  := '30';
8893 
8894       IF p_update_serial_status = 'TRUE' THEN
8895          l_progress  := '40';
8896          inv_material_status_grp.update_status ( p_api_version_number => p_api_version,
8897                                                  p_init_msg_lst      => NULL,
8898                                                  x_return_status     => x_return_status,
8899                                                  x_msg_count         => x_msg_count,
8900                                                  x_msg_data          => x_msg_data,
8901                                                  p_update_method     => inv_material_status_pub.g_update_method_receive,
8902                                                  p_status_id         => p_serial_status_id,
8903                                                  p_organization_id   => p_organization_id,
8904                                                  p_inventory_item_id => p_inventory_item_id,
8905                                                  p_sub_code          => NULL,
8906                                                  p_locator_id        => NULL,
8907                                                  p_lot_number        => p_lot_number,
8908                                                  p_serial_number     => p_from_serial_number,
8909                                                  p_to_serial_number  => p_to_serial_number,
8910                                                  p_object_type       => 'S'
8911                                                );
8912       END IF;
8913 
8914       IF x_return_status <> fnd_api.g_ret_sts_success THEN
8915          RAISE FND_API.G_EXC_ERROR;
8916       END IF;
8917 
8918       l_progress  := '50';
8919       serial_check.inv_mark_serial ( from_serial_number  => p_from_serial_number,
8920                                      to_serial_number    => p_to_serial_number,
8921                                      item_id             => p_inventory_item_id,
8922                                      org_id              => p_organization_id,
8923                                      hdr_id              => NULL,
8924                                      temp_id             => NULL,
8925                                      lot_temp_id         => NULL,
8926                                      success             => l_success
8927                                    );
8928       l_progress  := '60';
8929 
8930       IF ( l_debug = 1 ) THEN
8931          print_debug (    'Exit insert_range_serial 90:'
8932                        || TO_CHAR ( SYSDATE, 'YYYY-MM-DD HH:DD:SS' )
8933                      );
8934       END IF;
8935    EXCEPTION
8936       WHEN FND_API.G_EXC_ERROR THEN
8937          ROLLBACK TO count_insert_range_serial_sp;
8938          x_return_status := fnd_api.g_ret_sts_error;
8939 
8940          IF ( l_debug = 1 ) THEN
8941             print_debug (    'Exitting insert_range_serial - execution error:'
8942                           || l_progress
8943                           || ' '
8944                           || TO_CHAR ( SYSDATE, 'YYYY-MM-DD HH:DD:SS' )
8945                         );
8946          END IF;
8947 
8948          --  Get message count and data
8949          fnd_msg_pub.count_and_get ( p_encoded           => fnd_api.g_false,
8950                                      p_count             => x_msg_count,
8951                                      p_data              => x_msg_data
8952                                    );
8953       WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8954          ROLLBACK TO count_insert_range_serial_sp;
8955 
8956          IF ( l_debug = 1 ) THEN
8957             print_debug (    'Exitting insert_range_serial - unexpected error:'
8958                           || l_progress
8959                           || ' '
8960                           || TO_CHAR ( SYSDATE, 'YYYY-MM-DD HH:DD:SS' )
8961                         );
8962          END IF;
8963 
8964          x_return_status := fnd_api.g_ret_sts_unexp_error;
8965          --  Get message count and data
8966          fnd_msg_pub.count_and_get ( p_encoded           => fnd_api.g_false,
8967                                      p_count             => x_msg_count,
8968                                      p_data              => x_msg_data
8969                                    );
8970       WHEN OTHERS THEN
8971          ROLLBACK TO count_insert_range_serial_sp;
8972 
8973          IF ( l_debug = 1 ) THEN
8974             print_debug (    'Exitting insert_range_serial - other exceptions:'
8975                           || l_progress
8976                           || ' '
8977                           || TO_CHAR ( SYSDATE, 'YYYY-MM-DD HH:DD:SS' )
8978                         );
8979          END IF;
8980 
8981          x_return_status := fnd_api.g_ret_sts_unexp_error;
8982 
8983          IF SQLCODE IS NOT NULL THEN
8984             inv_mobile_helper_functions.sql_error ( 'INV_RCV_COMMON_APIS.insert_range_serial',
8985                                                     l_progress,
8986                                                     SQLCODE
8987                                                   );
8988          END IF;
8989 
8990          IF fnd_msg_pub.check_msg_level ( fnd_msg_pub.g_msg_lvl_unexp_error ) THEN
8991             fnd_msg_pub.add_exc_msg ( g_pkg_name, 'insert_range_serial' );
8992          END IF;
8993 
8994          --  Get message count and data
8995          fnd_msg_pub.count_and_get ( p_encoded           => fnd_api.g_false,
8996                                      p_count             => x_msg_count,
8997                                      p_data              => x_msg_data
8998                                    );
8999    END insert_range_serial;
9000 
9001    PROCEDURE get_system_quantity (
9002       p_organization_id IN NUMBER,
9003       p_subinventory IN VARCHAR2,
9004       p_locator_id IN NUMBER,
9005       p_parent_lpn_id IN NUMBER,
9006       p_inventory_item_id IN NUMBER,
9007       p_revision  IN VARCHAR2,
9008       p_lot_number IN VARCHAR2,
9009       p_uom_code  IN VARCHAR2,
9010       x_system_quantity OUT NOCOPY NUMBER
9011    )
9012    IS
9013       l_primary_uom VARCHAR2 ( 3 );
9014       l_serial_number_control_code NUMBER;
9015       l_progress VARCHAR2 ( 10 );
9016       l_converted_quantity NUMBER;
9017       l_loaded_sys_qty NUMBER; -- bug 2640378
9018       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
9019 
9020       /* Bug 4886188 -Added the local variables for the lpn details from wlpn*/
9021        l_lpn_subinv  VARCHAR2(10) ;
9022        l_lpn_locator_id  NUMBER ;
9023        l_lpn_context  NUMBER;
9024       /* End of fix for Bug 4886188 */
9025    BEGIN
9026       IF ( l_debug = 1 ) THEN
9027          print_debug ( '***get_system_quantity***' );
9028       END IF;
9029 
9030       -- Initialize the output variable
9031       x_system_quantity := 0;
9032       l_progress  := '10';
9033 
9034          /*
9035               ******  Fix for bug 4886188
9036               ******  If the Lpn Context is 'Issued Out of Stores' or 'Intransit' or 'Packing Context' or 'Loaded to Dock'
9037               ******  system quantity should be shown as 0. Because, ideally the LPN will not be present in that location.
9038            */
9039 
9040             IF ( p_parent_lpn_id IS NOT NULL ) THEN
9041 
9042                SELECT NVL ( subinventory_code, '###' ),
9043                       NVL ( locator_id, -99 ),
9044                       lpn_context
9045                INTO   l_lpn_subinv,
9046                       l_lpn_locator_id,
9047                       l_lpn_context
9048                FROM   WMS_LICENSE_PLATE_NUMBERS
9049                WHERE  lpn_id = p_parent_lpn_id ;
9050 
9051                IF ( l_debug = 1 ) THEN
9052                     print_debug ( 'l_lpn_subinv: ===> ' || l_lpn_subinv );
9053                     print_debug ( 'l_lpn_locator_id: => ' || l_lpn_locator_id );
9054                     print_debug ( 'l_lpn_context: => ' || l_lpn_context );
9055                END IF;
9056 
9057                IF ((l_lpn_context = 8 or l_lpn_context = 9 or l_lpn_context = 4 or l_lpn_context = 6 or l_lpn_context = 11 ) -- Added 11 status for Bug 9559613
9058 			   --Added below condition for bug# 14068189
9059 			   OR ((NOT g_lpn_summary_count)
9060 					AND (l_lpn_subinv <> '###' AND l_lpn_locator_id <> -99)
9061 					AND (l_lpn_subinv <> p_subinventory OR l_lpn_locator_id <> p_locator_id))
9062 				)
9063 			   THEN
9064                   IF ( l_debug = 1 ) THEN
9065                     print_debug ( 'Returning the system quantity as 0' );
9066                   END IF;
9067                   g_condition:=TRUE ;
9068                   return;
9069                END IF;
9070            END IF;
9071            /*  End of fix for bug number 4886188 */
9072 
9073 
9074 
9075       SELECT primary_uom_code,
9076              serial_number_control_code
9077       INTO   l_primary_uom,
9078              l_serial_number_control_code
9079       FROM   mtl_system_items
9080       WHERE  inventory_item_id = p_inventory_item_id
9081       AND    organization_id = p_organization_id;
9082 
9083       l_progress  := '20';
9084 
9085       IF ( l_serial_number_control_code IN ( 1, 6 ) ) THEN
9086          IF ( l_debug = 1 ) THEN
9087             print_debug ( 'Non serial controlled item' );
9088          END IF;
9089 
9090          IF ( p_parent_lpn_id IS NULL ) THEN
9091             IF ( l_debug = 1 ) THEN
9092                print_debug ( 'LPN ID is null' );
9093             END IF;
9094 
9095             SELECT NVL ( SUM ( primary_transaction_quantity ), 0 )
9096             INTO   x_system_quantity
9097             FROM   MTL_ONHAND_QUANTITIES_DETAIL
9098             WHERE  inventory_item_id = p_inventory_item_id
9099             AND    organization_id = p_organization_id
9100             AND    NVL ( containerized_flag, 2 ) = 2
9101             AND    subinventory_code = p_subinventory
9102             AND    NVL ( locator_id, -99 ) = NVL ( p_locator_id, -99 )
9103             AND    (    NVL ( lot_number, 'XX' ) = NVL ( p_lot_number, 'XX' )
9104                      OR p_lot_number IS NULL
9105                    ) -- Lot number might not have been entered yet
9106             AND    NVL ( revision, 'XXX' ) = NVL ( p_revision, 'XXX' );
9107 
9108             SELECT NVL ( SUM ( quantity ), 0 )
9109             INTO   l_loaded_sys_qty
9110             FROM   WMS_LOADED_QUANTITIES_V
9111             WHERE  inventory_item_id = p_inventory_item_id
9112             AND    organization_id = p_organization_id
9113             AND    NVL ( containerized_flag, 2 ) = 2
9114             AND    subinventory_code = p_subinventory
9115             AND    NVL ( locator_id, -99 ) = NVL ( p_locator_id, -99 )
9116             AND    (    NVL ( lot_number, 'XX' ) = NVL ( p_lot_number, 'XX' )
9117                      OR p_lot_number IS NULL
9118                    )
9119             -- Lot number might not have been entered yet
9120             AND    NVL ( revision, 'XXX' ) = NVL ( p_revision, 'XXX' )
9121             AND    qty_type = 'LOADED'
9122             AND    lpn_id IS NULL
9123             AND    content_lpn_id IS NULL; -- bug 2640378
9124 
9125             IF ( l_debug = 1 ) THEN
9126                print_debug ( 'Loaded qty is ' || l_loaded_sys_qty );
9127             END IF;
9128 
9129             IF l_loaded_sys_qty > 0 THEN
9130                x_system_quantity := x_system_quantity - l_loaded_sys_qty;
9131             END IF; -- bug 2640378
9132          ELSE
9133             IF ( l_debug = 1 ) THEN
9134                print_debug ( 'LPN ID is not null: ' || p_parent_lpn_id );
9135             END IF;
9136 
9137             BEGIN
9138                --For R12 we need to consider primary_quantity instead of quantity from WLC (bug 6833992)
9139                SELECT nvl(sum(primary_quantity),0)   --BUG3026540
9140                INTO   x_system_quantity
9141                FROM   WMS_LPN_CONTENTS
9142                WHERE  parent_lpn_id = p_parent_lpn_id
9143                AND    organization_id = p_organization_id
9144                AND    inventory_item_id = p_inventory_item_id
9145                AND    (    NVL ( lot_number, 'XX' ) =
9146                                                     NVL ( p_lot_number, 'XX' )
9147                         OR p_lot_number IS NULL
9148                       )
9149                -- Lot number might not have been entered yet
9150                AND    NVL ( revision, 'XXX' ) = NVL ( p_revision, 'XXX' )
9151                AND    NVL ( serial_summary_entry, 2 ) = 2;
9152 
9153                SELECT NVL ( SUM ( quantity ), 0 )
9154                INTO   l_loaded_sys_qty
9155                FROM   wms_loaded_quantities_v
9156                WHERE  NVL ( lpn_id, NVL ( content_lpn_id, -1 ) ) = p_parent_lpn_id
9157                and    inventory_item_id = p_inventory_item_id
9158                and    organization_id = p_organization_id;
9159 
9160                IF l_loaded_sys_qty > 0 THEN
9161                   x_system_quantity := x_system_quantity - l_loaded_sys_qty;
9162                END IF;
9163             -- bug 2640378 does the counter need to know the missing quantity ?
9164 
9165             EXCEPTION
9166                WHEN NO_DATA_FOUND THEN
9167                   x_system_quantity := 0;
9168             END;
9169          END IF;
9170       ELSIF ( l_serial_number_control_code IN ( 2, 5 ) ) THEN
9171          IF ( l_debug = 1 ) THEN
9172             print_debug ( 'Serial controlled item' );
9173          END IF;
9174 
9175          IF ( p_parent_lpn_id IS NULL ) THEN
9176             IF ( l_debug = 1 ) THEN
9177                print_debug ( 'LPN ID is null' );
9178             END IF;
9179 
9180             SELECT NVL ( SUM ( DECODE ( current_status, 3, 1, 0 ) ), 0 )
9181             INTO   x_system_quantity
9182             FROM   mtl_serial_numbers
9183             WHERE  lpn_id IS NULL
9184             AND    inventory_item_id = p_inventory_item_id
9185             AND    current_organization_id = p_organization_id
9186             AND    current_subinventory_code = p_subinventory
9187             AND    NVL ( current_locator_id, -99 ) = NVL ( p_locator_id, -99 )
9188             AND    (    NVL ( lot_number, 'XX' ) = NVL ( p_lot_number, 'XX' )
9189                      OR p_lot_number IS NULL
9190                    )
9191             -- Lot number might not have been entered yet
9192             AND    NVL ( revision, 'XXX' ) = NVL ( p_revision, 'XXX' );
9193 
9194             select count(*)
9195             into   l_loaded_sys_qty
9196                            from   mtl_serial_numbers_temp msnt, wms_loaded_quantities_v wl
9197                            where  ((msnt.transaction_temp_id = wl.transaction_temp_id
9198                      and wl.lot_number is null) or
9199                                    (msnt.transaction_temp_id = wl.serial_transaction_temp_id
9200                      and wl.lot_number is not null)
9201                                        )
9202             and    wl.containerized_flag = 2
9203                            and    wl.inventory_item_id = p_inventory_item_id
9204                            and    wl.subinventory_code = p_subinventory
9205                            and    nvl(wl.locator_id,-99) = nvl(p_locator_id,-99)
9206             and    (nvl(wl.lot_number,'@@@') = nvl(p_lot_number,'@@@')
9207                     or p_lot_number is null)
9208             and    nvl(wl.revision,'##') = nvl(p_revision,'##');
9209 
9210             IF l_loaded_sys_qty > 0 THEN
9211                   x_system_quantity := x_system_quantity - l_loaded_sys_qty;
9212             END IF;
9213          ELSE
9214             IF ( l_debug = 1 ) THEN
9215                print_debug ( 'LPN ID is not null: ' || p_parent_lpn_id );
9216             END IF;
9217 
9218             SELECT COUNT ( * )
9219             INTO   x_system_quantity
9220             FROM   mtl_serial_numbers
9221             WHERE  lpn_id = p_parent_lpn_id
9222             AND    inventory_item_id = p_inventory_item_id
9223             AND    current_organization_id = p_organization_id
9224             AND    (    NVL ( lot_number, 'XX' ) = NVL ( p_lot_number, 'XX' )
9225                      OR p_lot_number IS NULL
9226                    )
9227             -- Lot number might not have been entered yet
9228             AND    NVL ( revision, 'XXX' ) = NVL ( p_revision, 'XXX' );
9229 
9230 			--Bug#12645638
9231             SELECT Count(DISTINCT msn.serial_number)
9232             into   l_loaded_sys_qty
9233                       from   mtl_serial_numbers_temp msnt, mtl_serial_numbers msn, wms_loaded_quantities_v wl
9234                            where  msn.lpn_id = nvl(wl.content_lpn_id,nvl(wl.lpn_id,-1))
9235 						   AND   ((msnt.transaction_temp_id = wl.transaction_temp_id and wl.lot_number is null)
9236 							  or (msnt.transaction_temp_id = wl.serial_transaction_temp_id and wl.lot_number is not null))
9237                            and   wl.containerized_flag = 1
9238                       and    msn.inventory_item_id = wl.inventory_item_id
9239 					  AND    msn.serial_number BETWEEN msnt.FM_SERIAL_NUMBER AND msnt.TO_SERIAL_NUMBER
9240                       and    msn.current_organization_id = wl.ORGANIZATION_ID
9241                       and    wl.inventory_item_id = p_inventory_item_id
9242                            and    wl.organization_id = p_organization_id
9243                            and    msn.lpn_id = p_parent_lpn_id
9244             and    nvl(msn.lot_number,'@@@') = nvl(wl.lot_number,'@@@')
9245 			and    nvl(msn.revision,'XXX') = nvl(wl.revision,'XXX')
9246             and    (msn.lot_number = p_lot_number or
9247                    p_lot_number is null)
9248             AND    NVL ( wl.revision, 'XXX' ) = NVL ( p_revision, 'XXX' );
9249 
9250             /*SELECT SUM(NVL( wl.quantity,0))  --9452528
9251             into   l_loaded_sys_qty
9252                       from   mtl_serial_numbers msn, wms_loaded_quantities_v wl
9253                            where  msn.lpn_id = nvl(wl.content_lpn_id,nvl(wl.lpn_id,-1))
9254                            and   wl.containerized_flag = 1
9255                       and    msn.inventory_item_id = wl.inventory_item_id
9256                       and    msn.current_organization_id = wl.ORGANIZATION_ID
9257                       and    wl.inventory_item_id = p_inventory_item_id
9258                            and    wl.organization_id = p_organization_id
9259                            and    msn.lpn_id = p_parent_lpn_id
9260             and    (nvl(msn.lot_number,'@@@') = nvl(wl.lot_number,'@@@') or
9261                    p_lot_number is null)
9262             AND    NVL ( wl.revision, 'XXX' ) = NVL ( p_revision, 'XXX' );*/
9263 
9264             IF l_loaded_sys_qty > 0 THEN
9265                   x_system_quantity := x_system_quantity - l_loaded_sys_qty;
9266             END IF;
9267          END IF;
9268       END IF;
9269 
9270       l_progress  := '30';
9271       l_converted_quantity :=
9272          inv_convert.inv_um_convert ( p_inventory_item_id,
9273                                       5,
9274                                       x_system_quantity,
9275                                       l_primary_uom,
9276                                       p_uom_code,
9277                                       NULL,
9278                                       NULL
9279                                     );
9280       l_progress  := '40';
9281       x_system_quantity := l_converted_quantity;
9282    EXCEPTION
9283       WHEN OTHERS THEN
9284          IF ( l_debug = 1 ) THEN
9285             print_debug (    'Exiting get_system_quantity - other exceptions:'
9286                           || l_progress
9287                           || ' '
9288                           || TO_CHAR ( SYSDATE, 'YYYY-MM-DD HH:DD:SS' )
9289                         );
9290          END IF;
9291    END get_system_quantity;
9292 
9293 
9294 
9295 -- bug 12822330
9296 -- When the CC header option is -> Multiple per count + Quantity only,
9297 -- 'SN Detail' should not be shown if the schedule quantity matches
9298 -- with what is entered on the mobile page.
9299 -- This is a utility method to get the total scheduled quantity for
9300 -- serial controlled items
9301 PROCEDURE get_schd_qty_for_serial_item
9302   (p_cycle_count_header_id  IN    NUMBER          ,
9303    p_organization_id      IN    NUMBER            ,
9304    p_subinventory         IN    VARCHAR2          ,
9305    p_locator_id           IN    NUMBER            ,
9306    p_parent_lpn_id        IN    NUMBER            ,
9307    p_inventory_item_id    IN    NUMBER            ,
9308    p_lot_number           IN    VARCHAR2 := NULL  ,
9309    p_revision             IN    VARCHAR2 := NULL  ,
9310    p_uom_code             IN    VARCHAR2          ,
9311    x_scheduled_qty      OUT   NOCOPY NUMBER       ,
9312    x_return_status      OUT NOCOPY  VARCHAR2      ,
9313    x_msg_count          OUT NOCOPY  NUMBER        ,
9314    x_msg_data           OUT NOCOPY  VARCHAR2)
9315    IS
9316       l_proc_name                   VARCHAR2(30) :=  'get_schd_qty_for_serial_item';
9317       l_progress                    VARCHAR2(30) :=  '100';
9318       l_system_quantity          NUMBER;
9319   BEGIN
9320     x_return_status  := fnd_api.g_ret_sts_success;
9321     print_debug ('IN : ' || l_proc_name);
9322     print_debug ('p_cycle_count_header_id   : ' || p_cycle_count_header_id  );
9323     print_debug ('p_organization_id : ' || p_organization_id);
9324     print_debug ('p_subinventory   : ' || p_subinventory  );
9325     print_debug ('p_locator_id : ' || p_locator_id);
9326     print_debug ('p_parent_lpn_id   : ' || p_parent_lpn_id  );
9327     print_debug ('p_inventory_item_id : ' || p_inventory_item_id);
9328     print_debug ('p_lot_number : ' || p_lot_number);
9329     print_debug ('p_revision   : ' || p_revision  );
9330 
9331     l_progress := 900;
9332     print_debug('l_progress =  ' || l_progress);
9333 
9334     l_progress := 910;
9335       BEGIN
9336 
9337         -- ER 13481846
9338         -- When a pick task is executed on a locator that is marked for cycle counting,
9339         -- before the count task, the count quantities are compared against the system
9340         -- quantity as of the time the task was created, not the correct system
9341         -- quantity at the time of the count execution.
9342         print_debug('Check system quantity..' );
9343 
9344         get_system_quantity (
9345             p_organization_id,
9346             p_subinventory,
9347             p_locator_id,
9348             p_parent_lpn_id,
9349             p_inventory_item_id,
9350             p_revision,
9351             p_lot_number,
9352             p_uom_code,
9353             l_system_quantity
9354          );
9355         x_scheduled_qty := l_system_quantity;
9356         /*
9357         print_debug('Checking schduled serial CCs..' );
9358          SELECT COUNT(*)
9359          INTO x_scheduled_qty
9360          FROM mtl_serial_numbers msn, mtl_cc_serial_numbers mcsn,
9361          mtl_material_statuses_vl mms, mtl_cycle_count_entries mcce
9362          WHERE msn.inventory_item_id = p_inventory_item_id
9363          AND msn.current_organization_id = p_organization_id
9364          AND msn.current_status IN (1, 3)
9365          AND msn.status_id = mms.status_id(+)
9366          AND msn.serial_number = mcsn.serial_number
9367          AND mcsn.cycle_count_entry_id = mcce.cycle_count_entry_id
9368          AND mcce.cycle_count_header_id = p_cycle_count_header_id
9369          AND mcce.inventory_item_id = p_inventory_item_id
9370          AND mcce.organization_id = p_organization_id
9371          AND mcce.subinventory = p_subinventory
9372          AND NVL(mcce.locator_id, -99999) = NVL(p_locator_id, -99999)
9373          AND NVL(mcce.revision, '@@@@@') = NVL(p_revision, '@@@@@')
9374          AND NVL(mcce.lot_number, '###' ) = NVL(p_lot_number, '###')
9375          AND NVL(mcce.parent_lpn_id, -99999) = NVL(p_parent_lpn_id, -99999)
9376          AND mcce.entry_status_code IN (1, 3)
9377          AND NVL(mcce.export_flag, 2) = 2
9378          AND EXISTS (select 1 from mtl_cycle_count_entries
9379                           where cycle_count_entry_id = mcce.cycle_count_entry_id
9380                           and number_of_counts is null);
9381 
9382          IF x_scheduled_qty is null or x_scheduled_qty = 0 THEN
9383            SELECT mcce.system_quantity_current
9384            INTO x_scheduled_qty
9385            FROM mtl_cycle_count_entries mcce
9386            WHERE mcce.cycle_count_header_id = p_cycle_count_header_id
9387            AND mcce.inventory_item_id = p_inventory_item_id
9388            AND mcce.organization_id = p_organization_id
9389            AND mcce.subinventory = p_subinventory
9390            AND NVL(mcce.locator_id, -99999) = NVL(p_locator_id, -99999)
9391            AND NVL(mcce.revision, '@@@@@') = NVL(p_revision, '@@@@@')
9392            AND NVL(mcce.lot_number, '###' ) = NVL(p_lot_number, '###')
9393            AND NVL(mcce.parent_lpn_id, -99999) = NVL(p_parent_lpn_id, -99999)
9394            AND mcce.entry_status_code IN (1, 3)
9395            AND NVL(mcce.export_flag, 2) = 2;
9396           END IF;
9397       print_debug('x_scheduled_qty:  ' || x_scheduled_qty );
9398       */
9399 
9400       EXCEPTION
9401          WHEN OTHERS THEN
9402             print_debug('Exception while computing the system quantity. ');
9403             x_scheduled_qty := -1;
9404       END;
9405 
9406     EXCEPTION
9407     WHEN fnd_api.g_exc_error THEN
9408         x_return_status  := FND_API.G_RET_STS_UNEXP_ERROR;
9409         fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
9410         print_debug('ROLLBACK ' );
9411         ROLLBACK ;
9412         print_debug('l_progress = ' || l_proc_name || ':'|| l_progress);
9413         print_debug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
9414     WHEN fnd_api.g_exc_unexpected_error THEN
9415         x_return_status  := FND_API.G_RET_STS_UNEXP_ERROR;
9416         fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
9417         print_debug('ROLLBACK ' );
9418         print_debug('l_progress = ' || l_proc_name || ':'|| l_progress);
9419         print_debug('RAISE fnd_api.g_exc_unexpected_error: ' || SQLERRM);
9420         print_debug('x_msg_count/Data = ' || x_msg_count || '/'|| x_msg_data);
9421         ROLLBACK ;
9422     WHEN OTHERS THEN
9423         x_return_status  := FND_API.G_RET_STS_UNEXP_ERROR;
9424         fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
9425         print_debug('ROLLBACK ' );
9426         ROLLBACK ;
9427         print_debug('l_progress = ' || l_proc_name || ':'|| l_progress);
9428         print_debug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
9429 
9430   END;
9431 
9432    PROCEDURE clean_up_tasks (
9433       p_transaction_temp_id IN NUMBER
9434    )
9435    IS
9436       l_employee_id NUMBER;
9437       l_progress VARCHAR2 ( 10 );
9438       l_task_temp_id NUMBER;
9439 
9440       CURSOR completed_tasks_cursor
9441       IS
9442          SELECT wdt.transaction_temp_id
9443          FROM   wms_dispatched_tasks wdt
9444          WHERE  wdt.person_id = l_employee_id
9445          AND    wdt.task_type = 3
9446          AND    NOT EXISTS (
9447                    SELECT 'ACTIVE_TASK'
9448                    FROM   mtl_cycle_count_entries
9449                    WHERE  entry_status_code in (1,3)
9450                    AND    NVL(export_flag,2) = 2
9451                    AND    cycle_count_entry_id = wdt.transaction_temp_id );--bug 12614348
9452 
9453       l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
9454    BEGIN
9455       IF ( l_debug = 1 ) THEN
9456          print_debug ( '***Calling clean_up_tasks***' );
9457       END IF;
9458 
9459       -- Set the savepoint first
9460       SAVEPOINT clean_up_tasks_sp;
9461       l_progress  := '10';
9462 
9463       -- Get the employee ID first for the person that performed the task
9464       SELECT DISTINCT NVL ( person_id, -999 )
9465       INTO            l_employee_id
9466       FROM            wms_dispatched_tasks_history
9467       WHERE           transaction_id = p_transaction_temp_id AND task_type = 3;
9468 
9469       IF ( l_debug = 1 ) THEN
9470          print_debug ( 'The employee ID is: ' || l_employee_id );
9471       END IF;
9472 
9473       IF ( l_employee_id = -999 ) THEN
9474          IF ( l_debug = 1 ) THEN
9475             print_debug ( 'The person ID for the completed task is null' );
9476          END IF;
9477 
9478          RAISE FND_API.G_EXC_ERROR;
9479       END IF;
9480 
9481       l_progress  := '20';
9482 
9483       FOR v_completed_task IN completed_tasks_cursor
9484       LOOP
9485          l_task_temp_id := v_completed_task.transaction_temp_id;
9486 
9487          IF ( l_debug = 1 ) THEN
9488             print_debug ( 'Cleaning up task with temp id: ' || l_task_temp_id );
9489          END IF;
9490 
9491          l_progress  := '30';
9492 
9493          -- Insert a record into the tasks history table
9494          IF ( l_debug = 1 ) THEN
9495             print_debug ( 'Inserting record into tasks history table' );
9496          END IF;
9497 
9498 	 BEGIN
9499 
9500          INSERT INTO WMS_DISPATCHED_TASKS_HISTORY
9501                      ( task_id,
9502                        transaction_id,
9503                        organization_id,
9504                        user_task_type,
9505                        person_id,
9506                        effective_start_date,
9507                        effective_end_date,
9508                        equipment_id,
9509                        equipment_instance,
9510                        person_resource_id,
9511                        machine_resource_id,
9512                        status,
9513                        dispatched_time,
9514                        loaded_time,
9515                        drop_off_time,
9516                        last_update_date,
9517                        last_updated_by,
9518                        creation_date,
9519                        created_by,
9520                        last_update_login,
9521                        attribute_category,
9522                        attribute1,
9523                        attribute2,
9524                        attribute3,
9525                        attribute4,
9526                        attribute5,
9527                        attribute6,
9528                        attribute7,
9529                        attribute8,
9530                        attribute9,
9531                        attribute10,
9532                        attribute11,
9533                        attribute12,
9534                        attribute13,
9535                        attribute14,
9536                        attribute15,
9537                        task_type,
9538                        priority,
9539                        task_group_id
9540                      )
9541             SELECT task_id,
9542                    transaction_temp_id,
9543                    organization_id,
9544                    user_task_type,
9545                    person_id,
9546                    effective_start_date,
9547                    effective_end_date,
9548                    equipment_id,
9549                    equipment_instance,
9550                    person_resource_id,
9551                    machine_resource_id,
9552                    6,
9553                    dispatched_time,
9554                    loaded_time,
9555                    drop_off_time,
9556                    last_update_date,
9557                    last_updated_by,
9558                    creation_date,
9559                    created_by,
9560                    last_update_login,
9561                    attribute_category,
9562                    attribute1,
9563                    attribute2,
9564                    attribute3,
9565                    attribute4,
9566                    attribute5,
9567                    attribute6,
9568                    attribute7,
9569                    attribute8,
9570                    attribute9,
9571                    attribute10,
9572                    attribute11,
9573                    attribute12,
9574                    attribute13,
9575                    attribute14,
9576                    attribute15,
9577                    task_type,
9578                    priority,
9579                    task_group_id
9580             FROM   WMS_DISPATCHED_TASKS
9581             WHERE  TRANSACTION_TEMP_ID = l_task_temp_id AND TASK_TYPE = 3;
9582 
9583          l_progress  := '40';
9584 
9585          -- Now delete the completed task from the dispatched tasks table
9586          IF ( l_debug = 1 ) THEN
9587             print_debug ( 'Deleting the record from the dispatched tasks table'
9588                         );
9589          END IF;
9590 
9591          DELETE FROM WMS_DISPATCHED_TASKS
9592          WHERE       TRANSACTION_TEMP_ID = l_task_temp_id AND TASK_TYPE = 3;
9593 
9594 	 EXCEPTION
9595           WHEN OTHERS THEN
9596             IF ( l_debug = 1 ) THEN
9597                print_debug ( 'Error while inserting into WMS_DISPATCHED_TASKS for l_task_temp_id: ' || l_task_temp_id );
9598             END IF;
9599          END;
9600       END LOOP;
9601 
9602       l_progress  := '50';
9603 
9604       IF ( l_debug = 1 ) THEN
9605          print_debug ( '***End of clean_up_tasks***' );
9606       END IF;
9607    EXCEPTION
9608       WHEN FND_API.G_EXC_ERROR THEN
9609          ROLLBACK TO clean_up_tasks_sp;
9610 
9611          IF ( l_debug = 1 ) THEN
9612             print_debug (    'Exiting clean_up_tasks - execution error:'
9613                           || l_progress
9614                           || ' '
9615                           || TO_CHAR ( SYSDATE, 'YYYY-MM-DD HH:DD:SS' )
9616                         );
9617          END IF;
9618       WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
9619          ROLLBACK TO clean_up_tasks_sp;
9620 
9621          IF ( l_debug = 1 ) THEN
9622             print_debug (    'Exiting clean_up_tasks - unexpected error:'
9623                           || l_progress
9624                           || ' '
9625                           || TO_CHAR ( SYSDATE, 'YYYY-MM-DD HH:DD:SS' )
9626                         );
9627          END IF;
9628       WHEN OTHERS THEN
9629          ROLLBACK TO clean_up_tasks_sp;
9630 
9631          IF ( l_debug = 1 ) THEN
9632             print_debug (    'Exitting clean_up_tasks - other exceptions:'
9633                           || l_progress
9634                           || ' '
9635                           || TO_CHAR ( SYSDATE, 'YYYY-MM-DD HH:DD:SS' )
9636                         );
9637          END IF;
9638    END clean_up_tasks;
9639 
9640 --BUG# 9734316
9641 	PROCEDURE update_cc_status
9642 		(x_result_out						OUT		NOCOPY VARCHAR2,
9643 		 x_cc_id								OUT		NOCOPY VARCHAR2,
9644 		 p_organization_id			IN		NUMBER,
9645 		 p_parent_lpn_id				IN		NUMBER,
9646 		 p_inventory_item_id		IN		NUMBER,
9647 		 p_sub_code							IN		VARCHAR2,
9648 		 p_loc_id								IN		NUMBER,
9649 		 p_cc_header_id					IN		NUMBER,
9650 		 p_task_id							IN		NUMBER,
9651 		 p_revision							IN		VARCHAR2)
9652 
9653     IS
9654     PRAGMA AUTONOMOUS_TRANSACTION;
9655 
9656     l_temp_exists VARCHAR2(1) := 'N';
9657     l_cc_id NUMBER;
9658 	  l_parent_lpn_id NUMBER;
9659     l_debug   NUMBER := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
9660     BEGIN
9661 
9662     x_result_out := 'S';
9663 
9664 		IF p_parent_lpn_id <= 0 THEN
9665 			l_parent_lpn_id := NULL;
9666 		ELSE
9667 			l_parent_lpn_id := p_parent_lpn_id;
9668 		END IF;
9669 
9670     IF ( l_debug = 1 ) THEN
9671             print_debug ( '***Inside update_cc_status***' );
9672             print_debug ( '***Inside update_cc_status p_organization_id : ***'||p_organization_id );
9673             print_debug ( '***Inside update_cc_status p_parent_lpn_id : ***'||p_parent_lpn_id );
9674 						print_debug ( '***Inside update_cc_status l_parent_lpn_id : ***'||l_parent_lpn_id );
9675             print_debug ( '***Inside update_cc_status p_inventory_item_id : ***'||p_inventory_item_id );
9676             print_debug ( '***Inside update_cc_status p_sub_code : ***'||p_sub_code );
9677             print_debug ( '***Inside update_cc_status p_loc_id : ***'||p_loc_id );
9678             print_debug ( '***Inside update_cc_status p_cc_header_id : ***'||p_cc_header_id );
9679             print_debug ( '***Inside update_cc_status p_task_id : ***'||p_task_id );
9680             print_debug ( '***Inside update_cc_status p_revision : ***'||p_revision);
9681     END IF;
9682 
9683 
9684     SELECT Min(CYCLE_COUNT_ENTRY_ID)
9685       INTO l_cc_id
9686       FROM mtl_cycle_count_entries
9687      WHERE organization_id        = p_organization_id
9688        AND cycle_count_header_id  = p_cc_header_id
9689        AND entry_status_code     IN (1,3)
9690        AND inventory_item_id      = p_inventory_item_id
9691        AND Nvl(subinventory,'@@@')= Nvl(p_sub_code,'@@@')
9692        AND Nvl(locator_id,-999)   = Nvl(p_loc_id,-999)
9693        AND NVL ( parent_lpn_id, -99999 ) = NVL ( l_parent_lpn_id, -99999 )
9694        AND Nvl(revision,'###')    = Nvl(p_revision,'###');
9695 
9696     IF ( l_debug = 1 ) THEN
9697      print_debug ( 'l_cc_task_id : '||l_cc_id);
9698     END IF;
9699 
9700     BEGIN
9701     SELECT 'Y'
9702       INTO l_temp_exists
9703       FROM wms_dispatched_tasks
9704      WHERE transaction_temp_id =  l_cc_id
9705        AND status = 9;
9706     EXCEPTION
9707 		WHEN NO_DATA_FOUND THEN
9708      l_temp_exists := 'N';
9709 		 IF ( l_debug = 1 ) THEN
9710       print_debug ( '***Inside update_cc_status Exception Block- No data found while chking the wdt for temp_id***' );
9711      END IF;
9712     WHEN OTHERS THEN
9713      l_temp_exists := 'N';
9714      IF ( l_debug = 1 ) THEN
9715       print_debug ( '***Inside update_cc_status Exception Block- Others while chking the wdt for temp_id***' );
9716       print_debug ('Error Msg : '||SQLERRM||' Error Code: '||SQLCODE);
9717      END IF;
9718     END;
9719 
9720     IF (l_temp_exists = 'N') THEN
9721      IF ( l_debug = 1 ) THEN
9722        print_debug ( 'Updating wdt with temp_id : '||p_task_id||' to '||l_cc_id );
9723      END IF;
9724 
9725     UPDATE wms_dispatched_tasks
9726         SET transaction_temp_id = l_cc_id
9727       WHERE transaction_temp_id = p_task_id;
9728 
9729 		IF ( l_debug = 1 ) THEN
9730      print_debug ( 'Upadated rows in wdt : '||SQL%rowcount );
9731 		END IF;
9732     x_cc_id := l_cc_id;
9733 
9734     ELSE
9735      IF ( l_debug = 1 ) THEN
9736       print_debug ( 'No Need to Update rows in wdt ');
9737      END IF;
9738 
9739     x_cc_id := p_task_id;
9740 
9741     END IF;
9742 
9743   COMMIT;
9744 
9745   EXCEPTION
9746   WHEN No_Data_Found THEN
9747   x_result_out := 'E';
9748   x_cc_id := p_task_id;
9749   IF ( l_debug = 1 ) THEN
9750      print_debug ( '***Inside update_cc_status Exception Block- No Data Found***' );
9751   END IF;
9752   ROLLBACK;
9753 
9754   WHEN OTHERS THEN
9755   x_result_out := 'E';
9756   x_cc_id := p_task_id;
9757   IF ( l_debug = 1 ) THEN
9758      print_debug ( '***Inside update_cc_status Exception Block- Others***' );
9759      print_debug ('SQLERRM : '||SQLERRM||' SQLCODE: '||SQLCODE);
9760   END IF;
9761   ROLLBACK;
9762 
9763 END update_cc_status;
9764 --BUG# 9734316
9765 
9766   -- Bug 10298222
9767   -- Following procedure will get called on
9768   -- F2 or Skipping or Cancel of an dispatched task
9769    PROCEDURE  remove_cc_tasks(
9770                  p_organization_id                 IN          NUMBER
9771                 ,p_user_id                         IN          NUMBER
9772                 ,p_employee_id                     IN          NUMBER
9773                 ,p_transaction_temp_id             IN          NUMBER
9774                 ,x_return_status                   OUT NOCOPY  VARCHAR2
9775                 ,x_msg_count                       OUT NOCOPY  NUMBER
9776                 ,x_msg_data                        OUT NOCOPY  VARCHAR2)
9777   IS
9778       l_proc_name                   VARCHAR2(30) :=  'REMOVE_CC_TASKS';
9779       l_progress                    VARCHAR2(30) :=  '100';
9780       l_prev_task_status            NUMBER := 0;
9781 
9782   BEGIN
9783      x_return_status  := fnd_api.g_ret_sts_success;
9784      print_debug ('IN : ' || l_proc_name);
9785      print_debug ('p_transaction_temp_id   : ' || p_transaction_temp_id  );
9786      print_debug ('p_employee_id : ' || p_employee_id);
9787 
9788      l_progress := 100;
9789      print_debug('l_progress =  ' || l_progress);
9790 
9791      x_return_status := FND_API.G_RET_STS_SUCCESS;
9792 
9793       -- The idea is to delete WDT for task with prev status as 'Pending'
9794       -- and update the WDT for task with prev status as 'Queued'
9795 
9796       BEGIN
9797          l_prev_task_status := wms_picking_pkg.g_previous_task_status(p_transaction_temp_id);
9798          wms_picking_pkg.g_previous_task_status.delete(p_transaction_temp_id);
9799       EXCEPTION
9800       WHEN OTHERS THEN
9801          print_debug('wms_picking_pkg.g_previous_task_status(p_transaction_temp_id) : ' || p_transaction_temp_id
9802                          || ' : not found' );
9803          l_prev_task_status := 1; --PENDING
9804       END ;
9805 
9806       l_progress := 110;
9807       print_debug('l_prev_task_status = ' || l_prev_task_status || ' for p_transaction_temp_id:  ' || p_transaction_temp_id);
9808 
9809       IF l_prev_task_status = 2 THEN --If prev task status was Queued
9810         print_debug('Updating the WDT ..' );
9811         UPDATE  wms_dispatched_tasks
9812            SET  status = l_prev_task_status
9813              ,last_update_date = SYSDATE
9814              ,last_updated_by = p_user_id
9815          WHERE  person_id = p_employee_id
9816            AND task_type = 3
9817            AND (  status IN (3,9) )
9818            AND  transaction_temp_id = p_transaction_temp_id;
9819 
9820         IF SQL%NOTFOUND THEN
9821           print_debug('no WDT to update  for this employee id  for this  Transaction Temp id with status in ( 3,9) ' );
9822         END IF;
9823 
9824         l_progress := 200;
9825         print_debug('l_progress =  ' || l_progress);
9826       ELSIF l_prev_task_status = 1 THEN --If prev task status was Pending
9827         print_debug('Deleting the WDT ..' );
9828         DELETE  wms_dispatched_tasks
9829          WHERE  person_id = p_employee_id
9830           AND task_type = 3
9831           AND (status   IN (1,3,9 )) ;
9832 
9833         IF SQL%NOTFOUND THEN
9834           print_debug('no WDT with status 3,9,1 remaining to delete for this employee id   ' );
9835            --It is OK not to find even one task to delete
9836         ELSE
9837            print_debug('Deleted all WDT with staus 3,9,1 for p_employee_id =  ' || p_employee_id);
9838         END IF;
9839 
9840         l_progress := 300;
9841         print_debug('l_progress =  ' || l_progress);
9842       END IF;
9843        commit;
9844        print_debug('END: ' || l_proc_name);
9845        EXCEPTION
9846   WHEN fnd_api.g_exc_error THEN
9847         x_return_status  := FND_API.G_RET_STS_UNEXP_ERROR;
9848         fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
9849         print_debug('ROLLBACK ' );
9850         ROLLBACK ;
9851         print_debug('l_progress = ' || l_proc_name || ':'|| l_progress);
9852         print_debug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
9853    WHEN fnd_api.g_exc_unexpected_error THEN
9854         x_return_status  := FND_API.G_RET_STS_UNEXP_ERROR;
9855         fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
9856         print_debug('ROLLBACK ' );
9857         print_debug('l_progress = ' || l_proc_name || ':'|| l_progress);
9858         print_debug('RAISE fnd_api.g_exc_unexpected_error: ' || SQLERRM);
9859         print_debug('x_msg_count/Data = ' || x_msg_count || '/'|| x_msg_data);
9860         ROLLBACK ;
9861    WHEN OTHERS THEN
9862         x_return_status  := FND_API.G_RET_STS_UNEXP_ERROR;
9863         fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
9864         print_debug('ROLLBACK ' );
9865         ROLLBACK ;
9866         print_debug('l_progress = ' || l_proc_name || ':'|| l_progress);
9867         print_debug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
9868 
9869   END remove_cc_tasks;
9870 
9871 --13053297
9872   PROCEDURE call_pack_unpack_container( p_lpn_id NUMBER
9873                                       , p_content_lpn_id NUMBER
9874                                       , p_org_id NUMBER
9875                                       , p_subinv VARCHAR2
9876                                       , p_locator NUMBER
9877                                       , p_operation NUMBER
9878                                       )
9879   IS
9880   l_status VARCHAR2(1);
9881   l_msgcnt NUMBER;
9882   l_msgdata VARCHAR2(240);
9883   l_debug   NUMBER := NVL ( FND_PROFILE.VALUE ( 'INV_DEBUG_TRACE' ), 0 );
9884   BEGIN
9885 
9886   IF ( l_debug = 1 ) THEN
9887     print_debug ( '***Inside call_pack_unpack_container ***' );
9888 	print_debug ( 'Calling WMS_Container_PVT.PackUnpack_Container with the following parameters' );
9889 	print_debug ( 'p_lpn_id          : ' ||p_lpn_id);
9890 	print_debug ( 'p_content_lpn_id  : ' ||p_content_lpn_id);
9891 	print_debug ( 'p_organization_id : ' ||p_org_id);
9892 	print_debug ( 'p_operation       : ' ||p_operation);
9893 	print_debug ( '***===***' );
9894 
9895   END IF;
9896   WMS_Container_PVT.PackUnpack_Container(
9897         p_api_version           => 1.0
9898       , p_init_msg_list         => fnd_api.g_false
9899       , p_validation_level      => fnd_api.g_valid_level_none
9900       , x_return_status         => l_status
9901       , x_msg_count             => l_msgcnt
9902       , x_msg_data              => l_msgdata
9903       , p_caller                => 'INV_TRNSACTION'
9904       , p_lpn_id                => p_lpn_id
9905       , p_content_lpn_id        => p_content_lpn_id
9906       , p_organization_id       => p_org_id
9907       , p_operation             => p_operation
9908       );
9909 
9910     IF l_status <> fnd_api.g_ret_sts_success THEN
9911 	fnd_msg_pub.count_and_get(p_count => l_msgcnt, p_data => l_msgdata);
9912 	IF ( l_debug = 1 ) THEN
9913          print_debug ( '***Inside call_pack_unpack_container l_status <> Success***' );
9914         END IF;
9915     END IF;
9916 
9917   END call_pack_unpack_container;
9918 
9919  -- Cycle Count Task Dispatch ER 13252115 added new proecure insert_cc_task
9920  -- to insert WDT for cycle count entry
9921   PROCEDURE insert_cc_task( p_organization_id      IN          NUMBER
9922 				,p_user_id                         IN          NUMBER
9923                 ,p_employee_id                     IN          NUMBER
9924                 ,p_sign_on_equipment_id            IN          NUMBER
9925                 ,p_task_method                     IN          VARCHAR2
9926 				,p_cycle_count_entry_id            IN          NUMBER
9927                 ,p_cc_header_id                    IN          NUMBER
9928                 ,p_item_id                         IN          NUMBER
9929                 ,p_revision                        IN          VARCHAR2
9930                 ,p_sub_code                        IN          VARCHAR2
9931                 ,p_loc_id                          IN          VARCHAR2
9932                 ,x_temp_id                         OUT NOCOPY  NUMBER
9933                 ,x_return_status                   OUT NOCOPY  VARCHAR2
9934 				,x_msg_count                       OUT NOCOPY  NUMBER
9935                 ,x_msg_data                        OUT NOCOPY  VARCHAR2)
9936 IS
9937    l_proc_name               VARCHAR2(30) :=  'INSERT_CC_TASK';
9938    l_progress                VARCHAR2(30) :=  '100';
9939    l_wdt_rowcnt              NUMBER :=0;
9940    l_person_resource_id      NUMBER;
9941    l_machine_resource_id     NUMBER;
9942    l_equipment_id            NUMBER;
9943    l_standard_operation_id   NUMBER;
9944    l_transaction_temp_id     NUMBER;
9945    l_task_priority           NUMBER;
9946  BEGIN
9947 
9948      x_return_status  := fnd_api.g_ret_sts_success;
9949      print_debug ('IN : ' || l_proc_name);
9950      print_debug ('***Inside insert_cc_task p_employee_id : ' || p_employee_id);
9951      print_debug ( '***Inside insert_cc_task p_organization_id : ***'||p_organization_id );
9952      print_debug ( '***Inside insert_cc_task p_item_id : ***'||p_item_id );
9953      print_debug ( '***Inside insert_cc_task p_sub_code : ***'||p_sub_code );
9954      print_debug ( '***Inside insert_cc_task p_loc_id : ***'||p_loc_id );
9955      print_debug ( '***Inside insert_cc_task p_cc_header_id : ***'||p_cc_header_id );
9956      print_debug ( '***Inside insert_cc_task p_revision : ***'||p_revision);
9957 
9958      l_progress := 100;
9959      print_debug('l_progress =  ' || l_progress);
9960 
9961     SELECT min(cycle_count_entry_id)
9962       INTO l_transaction_temp_id
9963       FROM mtl_cycle_count_entries
9964       WHERE organization_id        = p_organization_id
9965        AND cycle_count_header_id   = p_cc_header_id
9966        AND entry_status_code      IN (1,3)
9967        AND inventory_item_id      = p_item_id
9968        AND nvl(revision,'###')    = nvl(p_revision,'###')
9969        AND nvl(subinventory,'@@@')= nvl(p_sub_code,'@@@')
9970        AND nvl(locator_id,-999)   = nvl(p_loc_id,-999)
9971 	   AND standard_operation_id is not null;
9972 
9973 	   IF p_cycle_count_entry_id is not null THEN
9974 	   l_transaction_temp_id  := p_cycle_count_entry_id;
9975 	   END IF;
9976 
9977        SELECT nvl(standard_operation_id,0),nvl(task_priority,0)
9978        INTO l_standard_operation_id,l_task_priority
9979        from mtl_cycle_count_entries
9980        WHERE cycle_count_entry_id = l_transaction_temp_id;
9981 
9982       BEGIN
9983 
9984       SELECT 1
9985          INTO l_wdt_rowcnt
9986          FROM dual
9987          WHERE exists (SELECT 1
9988                          FROM wms_dispatched_tasks t
9989                          WHERE t.transaction_temp_id = l_transaction_temp_id);
9990 
9991          wms_picking_pkg.g_previous_task_status(l_transaction_temp_id) := 2;
9992       EXCEPTION
9993       WHEN NO_DATA_FOUND THEN
9994       l_wdt_rowcnt  := 0;
9995       wms_picking_pkg.g_previous_task_status(l_transaction_temp_id) := 1;
9996       END;
9997 
9998        SELECT "ROLE",
9999                equipment,
10000                eqp_id
10001          INTO  l_person_resource_id,
10002                l_machine_resource_id,
10003                l_equipment_id
10004          FROM  wms_person_resource_utt_v
10005                WHERE emp_id = p_employee_id
10006                AND standard_operation_id = l_standard_operation_id
10007                AND NVL(eqp_id, -999) = NVL(p_sign_on_equipment_id, NVL(eqp_id, -999));
10008 
10009          IF  l_wdt_rowcnt = 0     THEN
10010 
10011           INSERT INTO wms_dispatched_tasks
10012               (task_id,
10013                transaction_temp_id,
10014                organization_id,
10015                user_task_type,
10016                person_id,
10017                effective_start_date,
10018                effective_end_date,
10019                equipment_id,
10020                equipment_instance,
10021                person_resource_id,
10022                machine_resource_id,
10023                status,
10024                dispatched_time,
10025                last_update_date,
10026                last_updated_by,
10027                creation_date,
10028                created_by,
10029                task_type,
10030                operation_plan_id,
10031                move_order_line_id,
10032                priority,
10033                task_method)
10034               VALUES
10035               (wms_dispatched_tasks_s.NEXTVAL,
10036                l_transaction_temp_id,
10037                p_organization_id,
10038                l_standard_operation_id,
10039                p_employee_id,
10040                sysdate,
10041                sysdate,
10042                l_equipment_id,
10043                null,
10044                l_person_resource_id,
10045                l_machine_resource_id,
10046                3, -- Dispatched
10047                sysdate,
10048                sysdate,
10049                p_user_id,
10050                sysdate,
10051                p_user_id,
10052                3, --cycle count task
10053                null,
10054                null,
10055                l_task_priority,
10056                p_task_method);
10057 
10058 			   l_progress := 200;
10059 
10060 			   print_debug('Inserted into WDT'||'l_progress'||l_progress);
10061          END IF;
10062 
10063    		 l_progress := 300;
10064 		 print_debug('Before updating WDT to Active || l_progress'||l_progress);
10065 
10066         UPDATE wms_dispatched_tasks
10067         SET status                = 9, -- Active
10068         last_update_date      = sysdate,
10069         last_updated_by       = p_user_id
10070         WHERE transaction_temp_id = l_transaction_temp_id;
10071 
10072         print_debug('updated WDT to Active');
10073         print_debug('END: ' || l_proc_name);
10074 
10075         x_temp_id := l_transaction_temp_id;
10076 
10077   COMMIT;
10078 
10079   EXCEPTION
10080   WHEN NO_DATA_FOUND THEN
10081   x_return_status := 'E' ;
10082   print_debug('In exception block-no_data_found of : ' || l_proc_name ||'l_progress is' ||l_progress);
10083   ROLLBACK ;
10084 
10085   WHEN OTHERS THEN
10086   x_return_status := 'O';
10087   print_debug('In exception block-others of  : ' || l_proc_name||'l_progress is' ||l_progress);
10088   ROLLBACK ;
10089 
10090 END insert_cc_task;
10091 
10092 
10093 /* Added the FUNCTION validate_serials as part of a bug13835210
10094  * and it is local function which will be called from PROCEDURE validate_serials API.
10095  */
10096 FUNCTION validate_serials (p_organization_id 		IN NUMBER,
10097 							p_inventory_item_id 	IN NUMBER,
10098 							p_from_serial 			IN VARCHAR2
10099 							)
10100 RETURN BOOLEAN
10101 IS
10102 
10103 serial_unique_exp 			EXCEPTION;
10104 l_current_organization_id 	mtl_serial_numbers.current_organization_id%TYPE;
10105 l_inventory_item_id 		mtl_serial_numbers.inventory_item_id%TYPE;
10106 l_current_status 			mtl_serial_numbers.current_status%TYPE;
10107 l_organization_code 		mtl_parameters.organization_code%TYPE;
10108 l_serial_number_type 		mtl_parameters.serial_number_type%TYPE;
10109 l_exp_case            		NUMBER;
10110 l_nothing					VARCHAR2(1);
10111 
10112 	CURSOR c_serials ( p_fserial_num VARCHAR2 ) IS
10113 		SELECT msn.current_organization_id, msn.inventory_item_id, msn.current_status, mp.organization_code
10114 		FROM mtl_serial_numbers msn,mtl_parameters mp
10115 		WHERE  msn.serial_number = p_fserial_num
10116 		AND msn.current_organization_id IS NOT NULL
10117 		AND msn.inventory_item_id IS NOT NULL
10118 		AND msn.current_organization_id = mp.organization_id;
10119 		--ORDER BY DECODE(msn.current_organization_id,p_org_id,1,2);
10120 BEGIN
10121 	print_debug('Entered into function VALIDATE_SERIALS : ');
10122 	print_debug('-------------------------------------- : ');
10123 	print_debug('p_organization_id    >>> : '||p_organization_id);
10124 	print_debug('p_inventory_item_id  >>> : '||p_inventory_item_id);
10125 	print_debug('p_from_serial        >>> : '||p_from_serial);
10126 
10127 	IF (p_from_serial IS NOT NULL ) THEN
10128 
10129 		BEGIN
10130 			SELECT 'x'
10131 			INTO l_nothing
10132      	     FROM MTL_SERIAL_NUMBERS S, MTL_PARAMETERS P
10133      	     WHERE S.CURRENT_ORGANIZATION_ID = P.ORGANIZATION_ID
10134      	     AND S.SERIAL_NUMBER = p_from_serial
10135      	     AND P.SERIAL_NUMBER_TYPE = 3
10136 			 AND P.ORGANIZATION_ID <> p_organization_id;
10137 
10138      	   IF l_nothing IS NOT NULL THEN
10139      	      FND_MESSAGE.set_name('INV','INV_SER_UNIACR');
10140      	      FND_MESSAGE.SET_TOKEN('TOKEN1',p_from_serial);
10141      	      FND_MSG_PUB.Add;
10142      	      RETURN FALSE;
10143      	   END IF;
10144 
10145            EXCEPTION
10146 			 WHEN TOO_MANY_ROWS THEN
10147 			   FND_MESSAGE.set_name('INV','INV_SER_UNIACR');
10148 			   FND_MESSAGE.SET_TOKEN('TOKEN1',p_from_serial);
10149 			   FND_MSG_PUB.Add;
10150 			   RETURN FALSE;
10151 			 WHEN NO_DATA_FOUND THEN
10152 			   null;
10153         END;
10154 
10155 		IF inv_cache.set_org_rec ( p_organization_id) THEN
10156 			l_serial_number_type := inv_cache.org_rec.serial_number_type;
10157 		ELSE
10158 			print_debug('Issue with Organization ');
10159 			RETURN FALSE;
10160 		END IF;
10161 
10162 		OPEN c_serials (p_from_serial);
10163 		print_debug('Cursor is opened. ');
10164 
10165 			LOOP
10166 				FETCH c_serials INTO l_current_organization_id, l_inventory_item_id, l_current_status, l_organization_code;
10167 				EXIT WHEN c_serials%NOTFOUND;
10168 
10169 				IF l_current_organization_id = p_organization_id
10170 				AND l_inventory_item_id = p_inventory_item_id
10171 				AND l_current_status NOT IN (1,3,4)
10172 				THEN
10173 					print_debug(' First IF  ');
10174 					l_exp_case := 1;
10175 					Raise serial_unique_exp;
10176 
10177 				-- within item
10178 				ELSIF l_serial_number_type IN (1,4)
10179 				AND l_current_organization_id <> p_organization_id
10180 				AND l_inventory_item_id = p_inventory_item_id
10181 				THEN
10182 					print_debug(' Second IF  ');
10183 					l_exp_case := 2;
10184 					Raise serial_unique_exp;
10185 
10186 				-- within org
10187 				ELSIF  l_serial_number_type IN (2,3)
10188 				AND l_current_organization_id = p_organization_id
10189 				AND l_inventory_item_id <> p_inventory_item_id
10190 				THEN
10191 					print_debug(' Third IF  ');
10192 					l_exp_case := 3;
10193 					RAISE serial_unique_exp;
10194 
10195 				-- Across Org
10196 				ELSIF  l_serial_number_type = 3
10197 				AND (l_current_organization_id <> p_organization_id
10198 				OR l_inventory_item_id <> p_inventory_item_id)
10199 				THEN
10200 					print_debug(' Fourth IF  ');
10201 					l_exp_case := 4;
10202 					RAISE serial_unique_exp;
10203 				END IF;
10204 
10205 			END LOOP;
10206 
10207 		CLOSE c_serials;
10208 	END IF;
10209 	RETURN TRUE;
10210 
10211 EXCEPTION
10212 WHEN serial_unique_exp THEN
10213   print_debug(' In exception serial_unique_exp ');
10214 	IF c_serials%ISOPEN THEN
10215 		CLOSE c_serials;
10216 	END IF;
10217 
10218 	IF l_exp_case = 1  THEN
10219         FND_MESSAGE.set_name('INV','INV_INVALID_SERIAL_STATUS');
10220 		FND_MESSAGE.SET_TOKEN('ORG_CODE',l_organization_code);
10221         FND_MESSAGE.SET_TOKEN('CURRENT_STATUS',l_current_status);
10222 		FND_MSG_PUB.Add;
10223 
10224 	ELSIF l_exp_case = 2 THEN
10225         FND_MESSAGE.set_name('INV','INV_CCEOI_INVALID_SERIAL');
10226 		FND_MSG_PUB.Add;
10227 
10228 	ELSIF l_exp_case = 3 THEN
10229         FND_MESSAGE.set_name('INV','INV_SER_UNIQ1');
10230 		FND_MESSAGE.SET_TOKEN('TOKEN1',p_from_serial);
10231 		FND_MSG_PUB.Add;
10232 
10233 	ELSIF l_exp_case = 4 THEN
10234 		FND_MESSAGE.set_name('INV','INV_SER_UNIACR');
10235 		FND_MESSAGE.SET_TOKEN('TOKEN1',p_from_serial);
10236 		FND_MSG_PUB.Add;
10237 
10238 	END IF;
10239 	RETURN FALSE;
10240 
10241 WHEN OTHERS THEN
10242 	print_debug(' In FUNCTION validate_serials - exception others - '||SubStr(SQLERRM,1,100));
10243 	IF c_serials%ISOPEN THEN
10244       CLOSE c_serials;
10245 	END IF;
10246 	FND_MESSAGE.set_name('INV','INV_INVALID_SERIAL');
10247 	FND_MSG_PUB.Add;
10248 	RETURN FALSE;
10249 END validate_serials;
10250 
10251 /* Added the PROCEDURE validate_serials as part of a bug# 13835210
10252  * this procedure will be called from CycleCountPage.java and CycleCountSerialFListener.java page.
10253  */
10254 PROCEDURE validate_serials (p_organization_id 		IN NUMBER,
10255 							p_inventory_item_id 	IN NUMBER,
10256 							p_from_serial 			IN VARCHAR2,
10257 							p_to_serial 			IN VARCHAR2,
10258 							p_serial_prefix 		IN VARCHAR2,
10259 							x_return_status     	OUT NOCOPY VARCHAR2
10260 							)
10261 IS
10262 l_temp_serial mtl_serial_numbers.serial_number%TYPE;
10263 l_num_serial 		NUMBER;
10264 l_num_serial_final 	NUMBER;
10265 BEGIN
10266 x_return_status := FND_API.G_RET_STS_SUCCESS;
10267 print_debug('Entered procedure VALIDATE_SERIALS API ');
10268 print_debug('-------------------------------------- : ');
10269 print_debug('p_organization_id    >> : '||p_organization_id);
10270 print_debug('p_inventory_item_id  >> : '||p_inventory_item_id);
10271 print_debug('p_from_serial        >> : '||p_from_serial);
10272 print_debug('p_to_serial          >> : '||p_to_serial);
10273 print_debug('p_serial_prefix      >> : '||p_serial_prefix);
10274 
10275 	-- for a single serial
10276 	IF ( (p_from_serial IS NOT NULL AND p_to_serial IS NULL) OR (p_from_serial = p_to_serial) ) THEN
10277 
10278 		IF NOT(validate_serials (p_organization_id,p_inventory_item_id,p_from_serial)) THEN
10279 			x_return_status := FND_API.G_RET_STS_ERROR;
10280 			RETURN;
10281 		END IF;
10282 
10283 	-- for a range of serials
10284 	ELSIF (p_from_serial IS NOT NULL AND p_to_serial IS NOT NULL AND p_from_serial <> p_to_serial) THEN
10285 
10286 		-- check if the serial range passed having the same serial prefix
10287 		-- if not then throw error message
10288 		BEGIN
10289 			l_num_serial := TO_NUMBER(REPLACE(p_from_serial, p_serial_prefix));
10290 			l_num_serial_final := TO_NUMBER(REPLACE(p_to_serial, p_serial_prefix));
10291 
10292 			IF l_num_serial > l_num_serial_final THEN
10293 				FND_MESSAGE.set_name('INV','INV_FROM_TO_SER_DIFF_LENGTH');
10294 				FND_MESSAGE.SET_TOKEN('FM_SER_NUM',p_from_serial);
10295 				FND_MESSAGE.SET_TOKEN('TO_SER_NUM',p_to_serial);
10296 				FND_MSG_PUB.Add;
10297 				x_return_status := FND_API.G_RET_STS_ERROR;
10298 				RETURN;
10299 			END IF;
10300 
10301 		EXCEPTION
10302 		WHEN VALUE_ERROR THEN
10303 			FND_MESSAGE.set_name('INV','INV_FROM_TO_SER_DIFF_PFX');
10304 			FND_MESSAGE.SET_TOKEN('FM_SER_NUM',p_from_serial);
10305 			FND_MESSAGE.SET_TOKEN('TO_SER_NUM',p_to_serial);
10306 			FND_MSG_PUB.Add;
10307 			x_return_status := FND_API.G_RET_STS_ERROR;
10308 			RETURN;
10309 		END;
10310 
10311 		-- validate each SN for serial uniqueness
10312 		LOOP
10313 			l_temp_serial :=  p_serial_prefix||l_num_serial;
10314 			IF NOT(validate_serials (p_organization_id,p_inventory_item_id,l_temp_serial)) THEN
10315 				x_return_status := FND_API.G_RET_STS_ERROR;
10316 				RETURN;
10317 			END IF;
10318 			EXIT WHEN (l_num_serial = l_num_serial_final);
10319 			l_num_serial := l_num_serial + 1;
10320 		END LOOP;
10321 	END IF;
10322 
10323 EXCEPTION
10324 WHEN OTHERS THEN
10325 	print_debug(' In PROCEDURE validate_serials - exception others - '||SubStr(SQLERRM,1,100));
10326 	x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
10327 END validate_serials;
10328 
10329 -- Bug 13992956
10330 /*
10331 
10332  This procedure will do the processing for a cycle count with header details as "Multiple Per Request"
10333  Count and "Quantity Only" Detail in which the count quantity is equal to system quantity.
10334 
10335  If count quantity is equal to system quantity, the relevant tables like mtl_cc_serial_numbers,
10336  mtl_serial_numbers, mtl_cycle_count_entries will be updated and the out parameter x_return_result will
10337  have the value 1.
10338 
10339  In case count quantity is not equal to system quantity (or there are other discrepancies), this procedure
10340  will not perform any logic and x_return_result will be set equal to 2.
10341 
10342 */
10343 
10344 PROCEDURE process_multiple_qty_only ( x_return_result OUT NOCOPY NUMBER )
10345 IS
10346    l_system_quantity NUMBER;
10347    l_sec_system_quantity NUMBER;
10348    l_count_present_qty NUMBER;
10349    l_lpn_subinventory WMS_LICENSE_PLATE_NUMBERS.SUBINVENTORY_CODE%TYPE;
10350    l_lpn_loc_id WMS_LICENSE_PLATE_NUMBERS.LOCATOR_ID%TYPE;
10351    l_debug NUMBER := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'), 0);
10352 BEGIN
10353    IF l_debug = 1 THEN
10354         print_debug('*** process_multiple_qty_only ***');
10355    END IF;
10356 
10357    IF g_cc_entry.parent_lpn_id IS NOT NULL THEN
10358         SELECT subinventory_code,
10359           locator_id
10360         INTO l_lpn_subinventory,
10361           l_lpn_loc_id
10362         FROM wms_license_plate_numbers
10363         WHERE lpn_id = g_cc_entry.parent_lpn_id;
10364 
10365         IF l_debug = 1 THEN
10366              print_debug('lpn_id: ' || g_cc_entry.parent_lpn_id || ', lpn_subinventory: ' || l_lpn_subinventory || ', lpn_loc_id: ' || l_lpn_loc_id);
10367         END IF;
10368 
10369         IF ( (l_lpn_subinventory <> g_cc_entry.subinventory) OR (l_lpn_loc_id <> g_cc_entry.locator_id) ) THEN
10370 
10371              IF l_debug = 1 THEN
10372                   print_debug('Discrepancy in Location of LPN. Setting x_return_result to 2');
10373              END IF;
10374 
10375              x_return_result := 2;
10376              RETURN;
10377 
10378         END IF;
10379 
10380    END IF;
10381 
10382    system_quantity( x_system_quantity     => l_system_quantity,
10383                     x_sec_system_quantity => l_sec_system_quantity
10384                    );
10385 
10386    BEGIN
10387 
10388         SELECT COUNT(*)
10389         INTO l_count_present_qty
10390         FROM mtl_serial_numbers msn,
10391          mtl_cc_serial_numbers mccsn
10392         WHERE mccsn.cycle_count_entry_id = g_cc_entry.cycle_count_entry_id
10393         AND msn.serial_number            = mccsn.serial_number
10394         AND msn.current_organization_id  = g_cc_entry.organization_id
10395         AND msn.inventory_item_id        = g_cc_entry.inventory_item_id
10396         AND msn.group_mark_id            = 1;
10397 
10398    EXCEPTION
10399      WHEN OTHERS THEN
10400         IF (l_debug = 1) THEN
10401              print_debug('Exception in block 1 in process_multiple_qty_only: ' || SUBSTR(SQLERRM, 1,100));
10402         END IF;
10403         x_return_result := 2;
10404         ROLLBACK TO save_serial_detail;
10405         RAISE FND_API.G_EXC_ERROR;
10406    END;
10407 
10408    IF l_debug = 1 THEN
10409         print_debug('l_system_quantity: ' || l_system_quantity || ', l_count_present_qty: ' || l_count_present_qty);
10410    END IF;
10411 
10412    IF ( l_system_quantity <> l_count_present_qty ) THEN
10413 
10414         IF l_debug = 1 THEN
10415                   print_debug('System quantity is not equal to count quantity. Setting x_return_result to 2');
10416         END IF;
10417 
10418         x_return_result := 2;
10419         RETURN;
10420 
10421    ELSIF ( l_system_quantity = l_count_present_qty ) THEN
10422 
10423         IF l_debug = 1 THEN
10424                   print_debug('System quantity is equal to count quantity...');
10425         END IF;
10426 
10427         g_count_quantity := l_count_present_qty;
10428 
10429         IF l_debug = 1 THEN
10430              print_debug ( 'organization_id: =======> ' || g_cc_entry.organization_id);
10431              print_debug ( 'cycle_count_entry_id: ==> ' || g_cc_entry.cycle_count_entry_id);
10432              print_debug ( 'inventory_item_id: =====> ' || g_cc_entry.inventory_item_id);
10433              print_debug ( 'subinventory_code: =====> ' || g_cc_entry.subinventory);
10434              print_debug ( 'locator_id: ============> ' || g_cc_entry.locator_id);
10435              print_debug ( 'revision: ==============> ' || g_cc_entry.revision);
10436              print_debug ( 'lot_number: ============> ' || g_cc_entry.lot_number);
10437              print_debug ( 'lpn_id: ================> ' || g_cc_entry.parent_lpn_id);
10438              print_debug ( 'count quantity: ========> ' || g_count_quantity);
10439         END IF;
10440 
10441         BEGIN
10442              -- updating the serial records that have been counted as present
10443              UPDATE mtl_cc_serial_numbers
10444              SET last_update_date       = SYSDATE,
10445                last_updated_by          = g_user_id,
10446                last_update_login        = g_login_id,
10447                number_of_counts         = NVL(number_of_counts, 0) + 1,
10448                unit_status_current      = 1,
10449                unit_status_prior        = NVL(unit_status_current, 1),
10450                unit_status_first        = NVL(unit_status_first, 1),
10451                approval_condition       = NULL,
10452                pos_adjustment_qty       = 0,
10453                neg_adjustment_qty       = 0
10454             WHERE cycle_count_entry_id = g_cc_entry.cycle_count_entry_id;
10455 
10456             IF l_debug = 1 THEN
10457                  print_debug('Number of records updated in MTL_CC_SERIAL_NUMBERS table: ' || SQL%ROWCOUNT);
10458             END IF;
10459 
10460             -- Unmarking the serials that are marked as present
10461             UPDATE mtl_serial_numbers msn
10462             SET msn.group_mark_id             = NULL
10463             WHERE msn.current_organization_id = g_cc_entry.organization_id
10464             AND msn.inventory_item_id         = g_cc_entry.inventory_item_id
10465             AND EXISTS
10466               (SELECT 1
10467               FROM mtl_cc_serial_numbers mccsn
10468               WHERE mccsn.cycle_count_entry_id = g_cc_entry.cycle_count_entry_id
10469               AND mccsn.serial_number          = msn.serial_number
10470               )
10471             AND msn.group_mark_id = 1;
10472 
10473             IF l_debug = 1 THEN
10474                  print_debug('Number of records updated in MTL_SERIAL_NUMBERS table: ' || SQL%ROWCOUNT);
10475             END IF;
10476 
10477         EXCEPTION
10478           WHEN OTHERS THEN
10479             IF (l_debug = 1) THEN
10480                  print_debug('Exception in block 2 in process_multiple_qty_only: ' || SUBSTR(SQLERRM, 1, 100));
10481             END IF;
10482 
10483             x_return_result := 2;
10484             ROLLBACK TO save_serial_detail;
10485             RAISE FND_API.G_EXC_ERROR;
10486         END;
10487 
10488         IF ( inv_cache.set_item_rec(p_organization_id => g_cc_entry.organization_id, p_item_id => g_cc_entry.inventory_item_id) ) THEN
10489              g_count_uom := inv_cache.item_rec.primary_uom_code;
10490         END IF;
10491 
10492         g_cc_entry.approval_date := SYSDATE;
10493         g_cc_entry.entry_status_code := 5; -- as there is no adjustment, count will be automatically approved
10494         g_cc_entry.adjustment_quantity := 0;
10495         g_cc_entry.adjustment_amount := 0;
10496         g_cc_entry.secondary_adjustment_quantity := 0;
10497         g_cc_entry.adjustment_date := NULL;
10498 
10499         -- to delete any cycle count reservations
10500         delete_reservation();
10501 
10502         -- incrementing the number of counts by 1
10503         g_cc_entry.number_of_counts := NVL(g_cc_entry.number_of_counts, 0) + 1;
10504 
10505         IF g_cc_entry.number_of_counts = 1 THEN
10506              -- if this is the first count
10507              entry_to_current( p_count_date             => SYSDATE,
10508                                p_counted_by_employee_id => g_employee_id,
10509                                p_system_quantity        => l_system_quantity,
10510                                p_reference              => NULL,
10511                                p_primary_uom_quantity   => g_count_quantity,
10512                                p_sec_system_quantity    => l_sec_system_quantity
10513                              );
10514 
10515              current_to_first();
10516 
10517         ELSE
10518              -- if this count is a recount
10519              current_to_prior();
10520              entry_to_current( p_count_date             => SYSDATE,
10521                                p_counted_by_employee_id => g_employee_id,
10522                                p_system_quantity        => l_system_quantity,
10523                                p_reference              => NULL,
10524                                p_primary_uom_quantity   => g_count_quantity,
10525                                p_sec_system_quantity    => l_sec_system_quantity
10526                              );
10527 
10528         END IF;
10529 
10530         -- to update cycle count's record in mtl_cycle_count_entries table
10531         update_row();
10532         x_return_result := 1;
10533         RETURN;
10534 
10535    END IF;
10536 
10537 EXCEPTION
10538   WHEN OTHERS THEN
10539      IF l_debug = 1 THEN
10540           print_debug('In process_multiple_qty_only - exception others: '|| SUBSTR(SQLERRM, 1, 100));
10541      END IF;
10542      x_return_result := 2;
10543 
10544 END process_multiple_qty_only;
10545 
10546 END INV_CYC_LOVS;