DBA Data[Home] [Help]

PACKAGE BODY: APPS.WMS_TASK_LOAD

Source


1 PACKAGE BODY wms_task_load AS
2   /* $Header: WMSLOADB.pls 120.66.12020000.22 2013/05/23 23:50:22 sahmahes ship $ */
3 
4 --  Global constants
5   g_pkg_name                      CONSTANT VARCHAR2(30) := 'WMS_TASK_LOAD';
6   l_g_ret_sts_error               CONSTANT VARCHAR2(30) := fnd_api.g_ret_sts_error;
7   l_g_ret_sts_unexp_error         CONSTANT VARCHAR2(30) := fnd_api.g_ret_sts_unexp_error;
8   l_g_ret_sts_success             CONSTANT VARCHAR2(30) := fnd_api.g_ret_sts_success;
9 
10   l_g_task_pending                CONSTANT NUMBER  := 1;
11   l_g_task_queued                 CONSTANT NUMBER  := 2;
12   l_g_task_dispatched             CONSTANT NUMBER  := 3;
13   l_g_task_loaded                 CONSTANT NUMBER  := 4;
14   l_g_task_active                 CONSTANT NUMBER  := 9;
15 
16   l_g_decimal_precision           CONSTANT NUMBER  := 5;
17   l_g_exception_short             CONSTANT VARCHAR2(30):= 'SHORT';
18   l_g_exception_over              CONSTANT VARCHAR2(30):= 'OVER';
19   l_g_action_split                CONSTANT VARCHAR2(30):= 'SPLIT';
20 -- Increased lot size to 80 Char - Mercy Thomas - B4625329
21   l_g_action_load_single          CONSTANT VARCHAR2(80):= 'LOAD_SINGLE';
22 -- Increased lot size to 80 Char - Mercy Thomas - B4625329
23   l_g_action_load_multiple        CONSTANT VARCHAR2(80):= 'LOAD_MULTIPLE';
24   l_g_isLotSubstitutionOK	  NUMBER  := 2; -- 1 = yes and 2 = no  /* Bug 9448490 Lot Substitution Project */
25 
26   g_debug                          NUMBER  := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0); --13860546 selecting from profile earlier hard coded to 1
27 
28   --/* Bug 9448490 Lot Substitution Project */ start
29 PROCEDURE proc_decrement_allocated_mtlts
30 			  (p_temp_id			    IN NUMBER
31 			  ,p_substitute_lots        IN VARCHAR2
32 			  ,p_confirmed_uom          IN VARCHAR2
33 			  ,x_return_status          OUT NOCOPY  VARCHAR2);
34 
35   --/* Bug 9448490 Lot Substitution Project */ end
36 
37 -- Forward declarations
38 PROCEDURE proc_insert_update_task
39 			  (p_action                          IN            VARCHAR2
40 			  ,p_insert                          IN            VARCHAR2
41 			  ,p_update                          IN            VARCHAR2
42 			  ,p_temp_id                         IN            NUMBER
43 			  ,p_new_temp_id                     IN            NUMBER
44 			  ,p_merge_temp_id                   IN            NUMBER
45 			  ,p_task_status                     IN            NUMBER
46 			  ,p_user_id                         IN            NUMBER
47 			  ,x_return_status                   OUT NOCOPY    VARCHAR2
48 			  ,x_msg_count                       OUT NOCOPY    NUMBER
49 			  ,x_msg_data                        OUT NOCOPY    VARCHAR2);
50 
51 PROCEDURE proc_insert_update_mmtt
52 			  (p_action                          IN            VARCHAR2
53 			  ,p_insert                          IN            VARCHAR2
54 			  ,p_update                          IN            VARCHAR2
55 			  ,p_organization_id                 IN            NUMBER
56 			  ,p_user_id                         IN            NUMBER
57 			  ,p_transaction_header_id           IN            NUMBER
58 			  ,p_transaction_temp_id             IN            NUMBER
59 			  ,p_transaction_temp_id_to_merge    IN            NUMBER
60 			  ,p_lpn_id                          IN            NUMBER
61 			  ,p_content_lpn_id                  IN            NUMBER
62 			  ,p_transfer_lpn_id                 IN            NUMBER
63 			  ,p_confirmed_sub                   IN            VARCHAR2
64 			  ,p_confirmed_locator_id            IN            NUMBER
65 			  ,p_confirmed_uom                   IN            VARCHAR2
66 			  ,p_suggested_uom                   IN            VARCHAR2
67 			  ,p_primary_uom                     IN            VARCHAR2
68 			  ,p_inventory_item_id               IN            NUMBER
69 			  ,p_revision                        IN            VARCHAR2
70 			  ,p_confirmed_trx_qty               IN            NUMBER
71 			  ,p_confirmed_lots                  IN            VARCHAR2
72 			  ,p_confirmed_lot_trx_qty           IN            VARCHAR2
73 			  ,p_confirmed_sec_uom               IN            VARCHAR2
74 			  ,p_confirmed_sec_qty               IN            VARCHAR2
75 			  ,p_confirmed_serials               IN            VARCHAR2
76 			  ,p_container_item_id               IN            NUMBER
77 			  ,p_wms_task_status                 IN            NUMBER
78 			  ,p_lpn_match                       IN            NUMBER
79 			  ,p_lpn_match_lpn_id                IN            NUMBER
80 			  ,p_serial_allocated_flag           IN            VARCHAR2
81 			  ,p_lot_controlled                  IN            VARCHAR2  -- Y/N
82 			  ,p_serial_controlled               IN            VARCHAR2  -- Y/N
83 			  ,p_exception                       IN            VARCHAR2  -- OVER/SHORT
84 			  ,p_parent_lpn_id                   IN            NUMBER
85 			  ,x_new_transaction_temp_id         OUT NOCOPY    NUMBER
86 			  ,x_return_status                   OUT NOCOPY    VARCHAR2
87 			  ,x_msg_count                       OUT NOCOPY    NUMBER
88 			  ,x_msg_data                        OUT NOCOPY    VARCHAR2
89 			  ,p_substitute_lots                 IN	           VARCHAR2 --/* Bug 9448490 Lot Substitution Project */
90 			  ,p_full_lot_allocation             IN            VARCHAR2 DEFAULT 'Y' ); -- Added for 14699845 (Flexible Lot Allocation)
91 
92 PROCEDURE proc_process_confirmed_lots
93 			 ( p_action                          IN            VARCHAR2
94 			  ,p_insert                          IN            VARCHAR2
95 			  ,p_update                          IN            VARCHAR2
96 			  ,p_organization_id                 IN            NUMBER
97 			  ,p_user_id                         IN            NUMBER
98 			  ,p_transaction_header_id           IN            NUMBER
99 			  ,p_transaction_temp_id             IN            NUMBER
100 			  ,p_new_transaction_temp_id         IN            NUMBER
101 			  ,p_transaction_temp_id_to_merge    IN            NUMBER
102 			  ,p_inventory_item_id               IN            NUMBER
103 			  ,p_revision                        IN            VARCHAR2
104 			  ,p_suggested_uom                   IN            VARCHAR2
105 			  ,p_confirmed_uom                   IN            VARCHAR2
106 			  ,p_primary_uom                     IN            VARCHAR2
107 			  ,p_confirmed_lots                  IN            VARCHAR2
108 			  ,p_confirmed_lot_trx_qty           IN            VARCHAR2
109 			  ,p_confirmed_serials               IN            VARCHAR2
110 			  ,p_serial_allocated_flag           IN            VARCHAR2
111 			  ,p_lpn_match                       IN            NUMBER
112 			  ,p_lpn_match_lpn_id                IN            NUMBER
113 			  ,p_confirmed_sec_uom               IN            VARCHAR2
114 			  ,p_confirmed_sec_qty               IN            VARCHAR2
115 			  ,p_lot_controlled                  IN            VARCHAR2  -- Y/N
116 			  ,p_serial_controlled               IN            VARCHAR2  -- Y/N
117 			  ,p_exception                       IN            VARCHAR2  -- OVER/SHORT
118 			  ,x_return_status                   OUT NOCOPY    VARCHAR2
119 			  ,x_msg_count                       OUT NOCOPY    NUMBER
120 			  ,x_msg_data                        OUT NOCOPY    VARCHAR2
121 			  ,p_substitute_lots		         IN	           VARCHAR2 --/* Bug 9448490 Lot Substitution Project */
122 			  ,p_full_lot_allocation             IN            VARCHAR2 DEFAULT 'Y'
123 			  ,p_fulfillment_base                IN            VARCHAR2
124 			  ,p_orig_mmtt_txn_uom               IN            VARCHAR2); -- Added for 14699845 (Flexible Lot Allocation)
125 
126 PROCEDURE proc_process_confirmed_serials
127 			 ( p_action                          IN            VARCHAR2
128 			  ,p_insert                          IN            VARCHAR2
129 			  ,p_update                          IN            VARCHAR2
130 			  ,p_organization_id                 IN            NUMBER
131 			  ,p_user_id                         IN            NUMBER
132 			  ,p_transaction_header_id           IN            NUMBER
133 			  ,p_transaction_temp_id             IN            NUMBER
134 			  ,p_new_transaction_temp_id         IN            NUMBER
135 			  ,p_transaction_temp_id_to_merge    IN            NUMBER
136 			  ,p_serial_transaction_temp_id      IN            NUMBER
137 			  ,p_mtlt_serial_temp_id             IN            NUMBER
138 			  ,p_inventory_item_id               IN            NUMBER
139 			  ,p_revision                        IN            VARCHAR2
140 			  ,p_suggested_uom                   IN            VARCHAR2
141 			  ,p_confirmed_uom                   IN            VARCHAR2
142 			  ,p_primary_uom                     IN            VARCHAR2
143 			  ,p_serial_lot_number               IN            VARCHAR2
144 			  ,p_confirmed_serials               IN            VARCHAR2
145 			  ,p_serial_allocated_flag           IN            VARCHAR2
146 			  ,p_lpn_match                       IN            NUMBER
147 			  ,p_lpn_match_lpn_id                IN            NUMBER
148 			  ,p_lot_controlled                  IN            VARCHAR2  -- Y/N
149 			  ,p_serial_controlled               IN            VARCHAR2  -- Y/N
150 			  ,x_return_status                   OUT NOCOPY    VARCHAR2
151 			  ,x_msg_count                       OUT NOCOPY    NUMBER
152 			  ,x_msg_data                        OUT NOCOPY    VARCHAR2
153 			  ,p_substitute_lots		         IN	           VARCHAR2 --/* Bug 12670785 Lot Substitution enhancement for Serial Items ER */
154 			  ,p_full_lot_allocation             IN            VARCHAR2); -- Added for 14699845 (Flexible Lot Allocation)
155 
156 PROCEDURE proc_insert_mtlt
157 			  (p_lot_record                      IN      mtl_transaction_lots_temp%ROWTYPE
158 			  ,x_return_status                   OUT NOCOPY    VARCHAR2
159 			  ,x_msg_count                       OUT NOCOPY    NUMBER
160 			  ,x_msg_data                        OUT NOCOPY    VARCHAR2);
161 
162 PROCEDURE proc_insert_msnt
163 			  (p_transaction_temp_id             IN          NUMBER
164 			  ,p_organization_id                 IN          NUMBER
165 			  ,p_inventory_item_id               IN          NUMBER
166 			  ,p_revision                        IN          VARCHAR2
167 			  ,p_confirmed_serials               IN          VARCHAR2
168 			  ,p_serial_number                   IN          VARCHAR2
169 			  ,p_lpn_id                          IN          NUMBER
170 			  ,p_serial_lot_number               IN          VARCHAR2
171 			  ,p_user_id                         IN          NUMBER
172 			  ,x_return_status                   OUT NOCOPY  VARCHAR2
173 			  ,x_msg_count                       OUT NOCOPY  NUMBER
174 			  ,x_msg_data                        OUT NOCOPY  VARCHAR2);
175 
176 PROCEDURE proc_mark_msn
177 			  (p_group_mark_id                   IN          NUMBER
178 			  ,p_organization_id                 IN          NUMBER
179 			  ,p_inventory_item_id               IN          NUMBER
180 			  ,p_Revision                        IN          VARCHAR2
181 			  ,p_confirmed_serials               IN          VARCHAR2
182 			  ,p_serial_lot_number               IN          VARCHAR2
183 			  ,p_serial_number                   IN          VARCHAR2
184 			  ,p_lpn_id                          IN          NUMBER
185 			  ,p_user_id                         IN          NUMBER
186 			  ,x_return_status                   OUT NOCOPY  VARCHAR2
187 			  ,x_msg_count                       OUT NOCOPY  NUMBER
188 			  ,x_msg_data                        OUT NOCOPY  VARCHAR2);
189 
190 PROCEDURE  proc_device_call
191 			  (p_action                          IN          VARCHAR2
192 			  ,p_employee_id                     IN          NUMBER
193 			  ,p_transaction_temp_id             IN          NUMBER
194 			  ,x_return_status                   OUT NOCOPY  VARCHAR2
195 			  ,x_msg_count                       OUT NOCOPY  NUMBER
196 			  ,x_msg_data                        OUT NOCOPY  VARCHAR2 );
197 
198 PROCEDURE  proc_process_cancelled_MOLs
199 			  (p_organization_id                 IN           NUMBER
200 			  ,p_user_id                         IN           NUMBER
201 			  ,p_transaction_header_id           IN           NUMBER
202 			  ,p_transaction_temp_id             IN           NUMBER
203 			  ,x_return_status                   OUT NOCOPY   VARCHAR2
204 			  ,x_msg_count                       OUT NOCOPY   NUMBER
205 			  ,x_msg_data                        OUT NOCOPY   VARCHAR2);
206 
207 PROCEDURE proc_parse_lot_serial_catchwt
208 			  (p_inventory_item_id               IN          NUMBER
209 			  ,p_organization_id				 IN 		 NUMBER --BUG12622871LSC
210 			  ,p_fulfillment_base                IN          VARCHAR2
211 			  ,p_confirmed_lots                  IN          VARCHAR2
212 			  ,p_confirmed_lot_trx_qty           IN          VARCHAR2
213 			  ,p_confirmed_lot_pri_qty           IN          VARCHAR2
214 			  ,p_confirmed_serials               IN          VARCHAR2
215 			  ,p_suggested_uom                   IN          VARCHAR2
216 			  ,p_confirmed_uom                   IN          VARCHAR2
217 			  ,p_primary_uom                     IN          VARCHAR2
218 			  ,p_confirmed_prim_qty              IN          VARCHAR2
219 			  ,p_confirmed_sugg_qty              IN          VARCHAR2
220 			  ,p_confirmed_sec_uom               IN          VARCHAR2
221 			  ,p_confirmed_sec_qty               IN          VARCHAR2
222 			  ,x_return_status                   OUT NOCOPY  VARCHAR2
223 			  ,x_msg_count                       OUT NOCOPY  NUMBER
224 			  ,x_msg_data                        OUT NOCOPY  VARCHAR2);
225 
226 PROCEDURE  proc_reset_lpn_context(
227 			   p_organization_id                 IN          NUMBER
228 			  ,p_user_id                         IN          NUMBER
229 			  ,p_transaction_header_id           IN          NUMBER
230 			  ,p_transaction_temp_id             IN          NUMBER
231 			  ,x_return_status                   OUT NOCOPY  VARCHAR2
232 			  ,x_msg_count                       OUT NOCOPY  NUMBER
233 			  ,x_msg_data                        OUT NOCOPY  VARCHAR2);
234 
235 PROCEDURE  proc_reset_task_status(
236 			   p_action                          IN          VARCHAR2
237 			  ,p_organization_id                 IN          NUMBER
238 			  ,p_user_id                         IN          NUMBER
239 			  ,p_employee_id                     IN          NUMBER
240 			  ,p_transaction_header_id           IN          NUMBER
241 			  ,p_transaction_temp_id             IN          NUMBER
242 			  ,x_return_status                   OUT NOCOPY  VARCHAR2
243 			  ,x_msg_count                       OUT NOCOPY  NUMBER
244 			  ,x_msg_data                        OUT NOCOPY  VARCHAR2);
245 --viks start_over
246 PROCEDURE proc_start_over
247 				(p_transaction_header_id     IN NUMBER
248 					 ,p_transaction_temp_id  IN NUMBER
249 					 ,p_user_id              IN NUMBER
250 					 ,x_start_over_taskno   OUT NOCOPY NUMBER
251 					 ,x_return_status       OUT NOCOPY VARCHAR2
252 					 ,x_msg_count           OUT NOCOPY NUMBER
253 					 ,x_msg_data            OUT NOCOPY VARCHAR2 );
254 
255 
256 
257 procedure validate_loaded_lpn_cg
258 	  ( p_organization_id       IN  NUMBER,
259 		p_inventory_item_id     IN  NUMBER,
260 		p_subinventory_code     IN  VARCHAR2,
261 		p_locator_id            IN  NUMBER,
262 		p_revision              IN  VARCHAR2,
263 		p_lot_number            IN  VARCHAR2,
264 		p_lpn_id                IN  NUMBER,
265 		p_transfer_lpn_id       IN  NUMBER,
266 		p_lot_control           IN  NUMBER,
267 		p_revision_control      IN  NUMBER,
268 		x_commingle_exist       OUT NOCOPY VARCHAR2,
269 		x_return_status         OUT NOCOPY VARCHAR2,
270 		p_trx_type_id           IN  VARCHAR2, -- Bug 4632519
271 		p_trx_action_id         IN  VARCHAR2); -- Bug 4632519
272 
273 --/* Bug 9448490 Lot Substitution Project */ start
274 PROCEDURE insert_mtlt (
275 		p_new_temp_id     IN  NUMBER
276 	  , p_serial_temp_id  IN  NUMBER := NULL
277 	  , p_pri_att_qty     IN  NUMBER
278 	  , p_sec_att_qty     IN  NUMBER := NULL
279 	  , p_trx_att_qty     IN  NUMBER
280 	  , p_lot_number      IN  VARCHAR2
281 	  , p_item_id         IN  NUMBER
282 	  , p_organization_id IN  NUMBER
283 	  ,x_return_status    OUT NOCOPY    VARCHAR2) ;
284 
285 --/* Bug 9448490 Lot Substitution Project */ end
286 
287 --HWSNIssue 13860546
288 PROCEDURE insert_serials_temp (
289    p_transaction_temp_id   IN             NUMBER,
290    p_lpn_id                IN             NUMBER,
291    p_org_id                IN             NUMBER,
292    p_item_id               IN             NUMBER,
293    p_rev                   IN             VARCHAR2,
294    p_confirmed_trx_qty     IN             NUMBER,
295    x_return_status         OUT  NOCOPY    VARCHAR2,
296    x_msg                   OUT  NOCOPY    VARCHAR2
297 );
298 
299 FUNCTION lpn_has_unalloc_lots (
300    p_lpn_id                IN   NUMBER,
301    p_organization_id       IN   NUMBER,
302    p_item_id               IN   NUMBER,
303    p_rev                   IN   VARCHAR2,
304    p_transaction_temp_id   IN   NUMBER
305 )
306 RETURN BOOLEAN ;
307 --HWSNIssue 13860546
308 
309 -- Added for 14699845 (Flexible Lot Allocation)
310 PROCEDURE Cleanup_LS_FlexiAlloc (p_transaction_temp_id IN NUMBER ,
311 								 p_transaction_header_id IN NUMBER,
312 								 p_serial_controlled    IN VARCHAR2,
313 								 x_return_status OUT NOCOPY VARCHAR2);
314 -- Added for 14699845 (Flexible Lot Allocation)
315 
316 PROCEDURE mydebug( p_msg        IN        VARCHAR2)
317 IS
318 BEGIN
319    IF (g_debug = 1) THEN
320 	  inv_mobile_helper_functions.tracelog(
321 							 p_err_msg => p_msg,
322 							 p_module  => g_pkg_name ,
323 							 p_level   => 9);
324 
325    END IF;
326   --    dbms_output.put_line( p_msg );
327 END mydebug;
328 
329 PROCEDURE mydebug(p_message IN VARCHAR2, p_module IN VARCHAR2)
330 IS
331 BEGIN
332    IF (g_debug = 1) THEN
333 	inv_log_util.trace(p_message, p_module, 9);
334    END IF;
335 END mydebug;
336 
337 PROCEDURE update_loaded_part
338   (p_user_id                   IN            NUMBER,
339    p_organization_id 		   IN 			 NUMBER, --BUG12622871LSC
340    p_transaction_temp_id1      IN            NUMBER,
341    p_transaction_temp_id2      IN            NUMBER,
342    p_transfer_lpn_id           IN            NUMBER,
343    p_transaction_uom           IN            VARCHAR2,
344    p_transaction_quantity      IN            NUMBER,
345    p_lot_numbers               IN            VARCHAR2,
346    p_lot_transaction_quantity  IN            VARCHAR2,
347    p_secondary_uom             IN            VARCHAR2,
348    p_secondary_quantity        IN            VARCHAR2,
349    p_serial_numbers            IN            VARCHAR2,
350    p_serial_allocated_flag     IN            VARCHAR2,  -- Y/N
351    p_lot_controlled            IN            VARCHAR2,  -- Y/N
352    p_serial_controlled         IN            VARCHAR2,  -- Y/N
353    x_return_status             OUT NOCOPY    VARCHAR2,
354    x_msg_count                 OUT NOCOPY    NUMBER,
355    x_msg_data                  OUT NOCOPY    VARCHAR2)
356   IS
357 	 l_serial_transaction_temp_id      NUMBER;
358 	 l_inventory_item_id               NUMBER;
359 	 l_primary_uom                     VARCHAR2(3);
360 	 l_primary_quantity                NUMBER;
361 	 l_primary_quantity1               NUMBER;
362 
363 	 l_secondary_uom                   VARCHAR2(3);
364 	 l_secondary_quantity              NUMBER;
365 	 l_secondary_quantity1             NUMBER;
366 
367 	 l_delta_primary_quantity          NUMBER;
368 	 l_delta_secondary_quantity        NUMBER;
369 
370 	 l_transaction_uom1                VARCHAR2(3);
371 	 l_transaction_uom2                VARCHAR2(3);
372 	 l_conversion_factor               NUMBER;
373 	 l_conversion_factor1              NUMBER;
374 	 l_conversion_factor2              NUMBER;
375 	 l_mtlt_rec                        mtl_transaction_lots_temp%ROWTYPE;
376 	 l_row_exists                      NUMBER;
377 	 i                                 NUMBER;
378 	 j                                 NUMBER;
379 	 l_debug                           NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
380 	 l_lpn_id                          NUMBER;
381 
382 	 TYPE lot_record_type IS RECORD
383 	   (lot_number                     mtl_serial_numbers.lot_number%TYPE,
384 		primary_quantity               NUMBER,
385 		delta_primary_quantity         NUMBER,
386 		secondary_quantity             NUMBER,
387 	delta_secondary_quantity       NUMBER,
388 		secondary_uom                  mtl_material_transactions_temp.secondary_uom_code%TYPE,
389 		serial_transaction_temp_id     NUMBER,
390 		update_mtlt                    BOOLEAN,
391 		delete_mtlt                    BOOLEAN);
392 
393 	 TYPE serial_record_type IS RECORD
394 	   (serial_number                  mtl_serial_numbers.serial_number%TYPE,
395 		lot_number                     mtl_serial_numbers.lot_number%TYPE,
396 		transaction_temp_id            NUMBER,
397 		delete_msnt                    BOOLEAN);
398 
399 	 TYPE lot_table_type IS TABLE OF lot_record_type INDEX BY BINARY_INTEGER;
400 	 TYPE serial_table_type IS TABLE OF serial_record_type INDEX BY BINARY_INTEGER;
401 
402 	 l_lot_table1                      lot_table_type;
403 	 l_serial_table1                   serial_table_type;
404 
405 	 CURSOR mmtt_cursor(v_transaction_temp_id1 NUMBER, v_transaction_temp_id2 NUMBER) IS
406 		SELECT inventory_item_id, transaction_temp_id, primary_quantity
407 	 , item_primary_uom_code, transaction_quantity, transaction_uom
408 	 , secondary_transaction_quantity, secondary_uom_code
409 		  FROM mtl_material_transactions_temp
410 		  WHERE transaction_temp_id IN (v_transaction_temp_id1, v_transaction_temp_id2)
411 		  FOR UPDATE;
412 
413 	 CURSOR mtlt_cursor(v_transaction_temp_id NUMBER) IS
414 		SELECT lot_number, primary_quantity, transaction_quantity, secondary_quantity
415 			, serial_transaction_temp_id
416 		  FROM mtl_transaction_lots_temp
417 		  WHERE transaction_temp_id = v_transaction_temp_id
418 		  ORDER BY lot_number
419 		  FOR UPDATE;
420 
421 	 CURSOR msnt_cursor(v_transaction_temp_id NUMBER) IS
422 		SELECT fm_serial_number
423 		  FROM mtl_serial_numbers_temp
424 		  WHERE transaction_temp_id = v_transaction_temp_id
425 		  ORDER BY fm_serial_number
426 		  FOR UPDATE;
427 
428 	 CURSOR confirmed_lot_serial_cursor IS
429 		SELECT
430 		  transaction_temp_id,
431 		  lot_number,
432 		  serial_number,
433 		  SUM(transaction_quantity) transaction_quantity,
434 		  SUM(primary_quantity) primary_quantity,
435 		  SUM(secondary_quantity) secondary_quantity
436 		FROM mtl_allocations_gtmp
437 		WHERE (lot_number IS NOT NULL OR serial_number IS NOT NULL)
438 		GROUP BY transaction_temp_id, lot_number, serial_number, secondary_quantity
439 		ORDER BY transaction_temp_id, lot_number, serial_number;
440 
441 BEGIN
442 
443    x_return_status  := l_g_ret_sts_success;
444    IF (l_debug = 1) THEN
445 	  mydebug('Inside UPDATE_LOADED_PART', 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
446    END IF;
447 
448    FOR mmtt_rec IN mmtt_cursor(p_transaction_temp_id1, p_transaction_temp_id2) LOOP
449 	  IF mmtt_rec.transaction_temp_id = p_transaction_temp_id1 THEN
450 		 l_transaction_uom1 := mmtt_rec.transaction_uom;
451 		 l_conversion_factor1 := mmtt_rec.transaction_quantity/mmtt_rec.primary_quantity;
452 		 l_primary_quantity1 := mmtt_rec.primary_quantity;
453    l_secondary_quantity1 := mmtt_rec.secondary_transaction_quantity;
454 	   ELSE
455 		 l_transaction_uom2 := mmtt_rec.transaction_uom;
456 		 l_conversion_factor2 := mmtt_rec.transaction_quantity/mmtt_rec.primary_quantity;
457 	  END IF;
458 
459 	  l_inventory_item_id := mmtt_rec.inventory_item_id;
460 	  l_primary_uom := mmtt_rec.item_primary_uom_code;
461   l_secondary_uom := mmtt_rec.secondary_uom_code;
462    END LOOP;
463 
464    IF p_transaction_uom = l_transaction_uom1 THEN
465 	  l_conversion_factor := l_conversion_factor1;
466 	ELSE
467 	  l_conversion_factor := inv_convert.inv_um_convert(item_id        => l_inventory_item_id,
468 														precision      => l_g_decimal_precision,
469 														from_quantity  => 1,
470 														from_unit      => l_primary_uom,
471 														to_unit        => p_transaction_uom,
472 														from_name      => NULL,
473 														to_name        => NULL);
474    END IF;
475 
476    IF (l_debug = 1) THEN
477 	  mydebug('Conversion Factor: ' || l_conversion_factor, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
478 	  mydebug('Conversion Factor1: ' || l_conversion_factor1, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
479 	  mydebug('Conversion Factor2: ' || l_conversion_factor2, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
480    END IF;
481 
482    IF p_lot_controlled <> 'Y' AND p_serial_controlled <> 'Y' THEN -- No Lot or serial control
483 		 fnd_message.set_token('ROUTINE', '- proc_insert_update_task' );
484 		 fnd_msg_pub.ADD;
485 	  l_primary_quantity := Round(p_transaction_quantity/l_conversion_factor, l_g_decimal_precision);
486 	  l_secondary_quantity := p_secondary_quantity;
487 	ELSE
488 	  proc_parse_lot_serial_catchwt
489 		(p_inventory_item_id        => l_inventory_item_id,
490 		 p_organization_id 			=> p_organization_id, --BUG12622871LSC
491 		 p_fulfillment_base         => 'P',
492 		 p_confirmed_lots           => p_lot_numbers,
493 		 p_confirmed_lot_trx_qty    => p_lot_transaction_quantity,
494 		 p_confirmed_lot_pri_qty    => NULL,
495 		 p_confirmed_serials        => p_serial_numbers,
496 		 p_suggested_uom            => l_transaction_uom1,
497 		 p_confirmed_uom            => p_transaction_uom,
498 		 p_primary_uom              => l_primary_uom    ,
499 		 p_confirmed_sec_uom        => p_secondary_uom,
500 		 p_confirmed_prim_qty       => l_primary_quantity1,
501 		 p_confirmed_sugg_qty       => l_secondary_quantity1,--need to revisit on testing case for update_loaded_part for fulfill base 'S'
502 		 p_confirmed_sec_qty        => p_secondary_quantity,
503 		 x_return_status            => x_return_status,
504 		 x_msg_count                => x_msg_count,
505 		 x_msg_data                 => x_msg_data);
506 
507 	  IF (l_debug = 1) THEN
508 		 mydebug('Return Status from Lot Serial Parse: ' || x_return_status, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
509 	  END IF;
510 
511 	  IF x_return_status <> l_g_ret_sts_success
512 		THEN
513 		   fnd_message.set_name('WMS', 'WMS_INTERNAL_ERROR'); --NEWMSG
514 		   -- Internal Error $ROUTINE
515 		   fnd_message.set_token('ROUTINE', ' - proc_parse_lot_serial_catchwt API ' );
516 		   mydebug('Error parsing lot/serial/catch weight string' );
517 		   -- "Error reserving Serial Number/s"
518 		   fnd_msg_pub.ADD;
519 		   RAISE fnd_api.g_exc_error;
520 	  END IF;
521 
522 	  IF p_lot_controlled = 'Y' AND p_serial_controlled <> 'Y' THEN -- Lot Controlled only
523 		 IF (l_debug = 1) THEN
524 			mydebug('Loaded...', 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
525 		 END IF;
526 
527 		 i := 1; -- lot table counter
528 		 FOR mtlt_record IN mtlt_cursor(p_transaction_temp_id1) LOOP
529 
530 			IF (l_debug = 1) THEN
531 			   mydebug(i || ' Lot Number: ' || mtlt_record.lot_number, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
532 			END IF;
533 
534 			l_lot_table1(i).lot_number := mtlt_record.lot_number;
535 			l_lot_table1(i).primary_quantity := mtlt_record.primary_quantity;
536 	l_lot_table1(i).secondary_quantity := mtlt_record.secondary_quantity;
537 
538 			i := i + 1;
539 		 END LOOP;
540 
541 		 IF (l_debug = 1) THEN
542 			mydebug('Confirmed...', 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
543 		 END IF;
544 
545 		 i := 1; -- lot table counter
546 		 l_primary_quantity := 0;
547    l_secondary_quantity := 0;
548 		 FOR lot_serial_rec IN confirmed_lot_serial_cursor LOOP
549 
550 			l_primary_quantity := l_primary_quantity + lot_serial_rec.primary_quantity;
551 	l_secondary_quantity := l_secondary_quantity + lot_serial_rec.secondary_quantity;
552 
553 			WHILE l_lot_table1(i).lot_number <> lot_serial_rec.lot_number LOOP
554 			   IF (l_debug = 1) THEN
555 				  mydebug(i || 'Marking Lot Number: ' || l_lot_table1(i).lot_number || ' to be deleted', 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
556 			   END IF;
557 			   l_lot_table1(i).delete_mtlt := TRUE;
558 			   l_lot_table1(i).delta_primary_quantity := l_lot_table1(i).primary_quantity;
559 	 l_lot_table1(i).delta_secondary_quantity := l_lot_table1(i).secondary_quantity;
560 			   i := i + 1;
561 			END LOOP;
562 
563 			IF (l_debug = 1) THEN
564 			   mydebug(i || ' Lot Number: ' || l_lot_table1(i).lot_number, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
565 			END IF;
566 
567 			IF l_lot_table1(i).lot_number = lot_serial_rec.lot_number THEN
568 			   l_lot_table1(i).secondary_quantity := lot_serial_rec.secondary_quantity;
569 
570 			   IF l_lot_table1(i).primary_quantity <> lot_serial_rec.primary_quantity THEN
571 
572 				  IF (l_debug = 1) THEN
573 					 mydebug(i || 'Marking Lot Number: ' || l_lot_table1(i).lot_number || ' to be updated', 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
574 				  END IF;
575 
576 				  l_lot_table1(i).update_mtlt := TRUE;
577 
578 				  l_lot_table1(i).delta_primary_quantity := l_lot_table1(i).primary_quantity - lot_serial_rec.primary_quantity;
579 				  l_lot_table1(i).primary_quantity := lot_serial_rec.primary_quantity;
580 	  l_lot_table1(i).delta_secondary_quantity := l_lot_table1(i).secondary_quantity - lot_serial_rec.secondary_quantity;
581 	  l_lot_table1(i).secondary_quantity := lot_serial_rec.secondary_quantity;
582 
583 			   END IF;
584 			END IF;
585 
586 			i := i + 1;
587 		 END LOOP;
588 
589 		 WHILE i <= l_lot_table1.COUNT LOOP
590 			l_lot_table1(i).delete_mtlt := TRUE;
591 			l_lot_table1(i).delta_primary_quantity := l_lot_table1(i).primary_quantity;
592 	l_lot_table1(i).delta_secondary_quantity := l_lot_table1(i).secondary_quantity;
593 			i := i + 1;
594 		 END LOOP;
595 
596 		 FOR i IN 1..l_lot_table1.COUNT LOOP
597 
598 			BEGIN
599 			   SELECT serial_transaction_temp_id
600 				 INTO l_serial_transaction_temp_id
601 				 FROM mtl_transaction_lots_temp
602 				 WHERE transaction_temp_id = p_transaction_temp_id2
603 				 AND lot_number = l_lot_table1(i).lot_number;
604 
605 			   l_row_exists := 1;
606 			EXCEPTION
607 			   WHEN no_data_found THEN
608 				  l_row_exists := 0;
609 			END;
610 
611 			IF (l_debug = 1) THEN
612 			   IF l_row_exists = 1 THEN
613 				  mydebug(i || 'Lot Number: ' || l_lot_table1(i).lot_number || ' exists in remaining', 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
614 				ELSE
615 				  mydebug(i || 'Lot Number: ' || l_lot_table1(i).lot_number || ' does not exist in remaining', 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
616 			   END IF;
617 			END IF;
618 
619 
620 			IF (l_debug = 1) THEN
621 			   mydebug(i || 'Delta: ' || l_lot_table1(i).delta_primary_quantity, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
622 	 mydebug(i || 'Sec Delta: ' || l_lot_table1(i).delta_secondary_quantity, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
623 			END IF;
624 
625 			IF l_lot_table1(i).delete_mtlt THEN
626 
627 			   IF l_row_exists = 1 THEN
628 
629 				  IF (l_debug = 1) THEN
630 					 mydebug(i || 'Deleting Lot Number: ' || l_lot_table1(i).lot_number, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
631 				  END IF;
632 
633 				  UPDATE mtl_transaction_lots_temp
634 					SET primary_quantity = primary_quantity + l_lot_table1(i).delta_primary_quantity,
635 						transaction_quantity = transaction_quantity +
636 											   Round(l_lot_table1(i).delta_primary_quantity * l_conversion_factor2,
637 													 l_g_decimal_precision),
638 		secondary_quantity = secondary_quantity + l_lot_table1(i).delta_secondary_quantity,
639 						last_update_date     = Sysdate,
640 						last_updated_by      = p_user_id
641 					WHERE transaction_temp_id = p_transaction_temp_id2
642 					AND lot_number = l_lot_table1(i).lot_number;
643 
644 				  DELETE FROM mtl_transaction_lots_temp
645 					WHERE transaction_temp_id = p_transaction_temp_id1
646 					AND lot_number = l_lot_table1(i).lot_number;
647 
648 				ELSE -- row does not exist in 2
649 
650 				  IF (l_debug = 1) THEN
651 					 mydebug(i || 'Transferring Lot Number: ' || l_lot_table1(i).lot_number, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
652 				  END IF;
653 
654 				  UPDATE mtl_transaction_lots_temp
655 					SET transaction_temp_id        = p_transaction_temp_id2,
656 						transaction_quantity       = Round(primary_quantity * l_conversion_factor2, l_g_decimal_precision),
657 						last_update_date     = Sysdate,
658 						last_updated_by      = p_user_id
659 					WHERE transaction_temp_id = p_transaction_temp_id1
660 					AND lot_number = l_lot_table1(i).lot_number;
661 
662 			   END IF;
663 
664 			 ELSIF l_lot_table1(i).update_mtlt THEN
665 
666 			   UPDATE mtl_transaction_lots_temp
667 				 SET primary_quantity     = l_lot_table1(i).primary_quantity,
668 					 transaction_quantity = Round(l_lot_table1(i).primary_quantity * l_conversion_factor, l_g_decimal_precision),
669 					 secondary_quantity   = l_lot_table1(i).secondary_quantity,
670 					 last_update_date     = Sysdate,
671 					 last_updated_by      = p_user_id
672 				 WHERE transaction_temp_id = p_transaction_temp_id1
673 				 AND lot_number = l_lot_table1(i).lot_number;
674 
675 			   IF l_row_exists = 1 THEN
676 
677 				  IF (l_debug = 1) THEN
678 					 mydebug(i || 'Updating Lot Number: ' || l_lot_table1(i).lot_number, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
679 				  END IF;
680 
681 				  UPDATE mtl_transaction_lots_temp
682 					SET primary_quantity     = primary_quantity + l_lot_table1(i).delta_primary_quantity,
683 						transaction_quantity = transaction_quantity + Round(l_lot_table1(i).delta_primary_quantity * l_conversion_factor2,
684 																			l_g_decimal_precision),
685 		secondary_quantity     = secondary_quantity + l_lot_table1(i).delta_secondary_quantity,
686 						last_update_date     = Sysdate,
687 						last_updated_by      = p_user_id
688 					WHERE transaction_temp_id = p_transaction_temp_id2
689 					AND lot_number = l_lot_table1(i).lot_number;
690 				ELSE -- row does not exist in 2
691 
692 				  IF (l_debug = 1) THEN
693 					 mydebug(i || 'Inserting Lot Number: ' || l_lot_table1(i).lot_number, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
694 				  END IF;
695 
696 				  SELECT *
697 					INTO l_mtlt_rec
698 					FROM mtl_transaction_lots_temp
699 					WHERE transaction_temp_id = p_transaction_temp_id1
700 					AND lot_number = l_lot_table1(i).lot_number;
701 
702 				  l_mtlt_rec.transaction_temp_id        := p_transaction_temp_id2;
703 				  l_mtlt_rec.transaction_quantity       := Round(l_mtlt_rec.primary_quantity * l_conversion_factor2, l_g_decimal_precision);
704 
705 				  proc_insert_mtlt
706 				   (p_lot_record         => l_mtlt_rec,
707 					x_return_status      => x_return_status,
708 					x_msg_count          => x_msg_count,
709 					x_msg_data           => x_msg_data);
710 
711 				  mydebug('x_return_status : ' || x_return_status);
712 				  IF x_return_status <> l_g_ret_sts_success THEN
713 					 RAISE fnd_api.g_exc_error;
714 				  END IF;
715 
716 			   END IF; -- row exists
717 
718 			END IF; -- delete/update mtlt
719 
720 		 END LOOP; -- lot loop
721 	   ELSIF p_lot_controlled <> 'Y' AND p_serial_controlled = 'Y' THEN -- Serial controlled only
722 
723 		 IF (l_debug = 1) THEN
724 			mydebug('Loaded...', 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
725 		 END IF;
726 
727 		 l_secondary_quantity := p_secondary_quantity;
728 
729 		 j := 1; -- serial table counter
730 		 FOR msnt_record IN msnt_cursor(p_transaction_temp_id1) LOOP
731 			IF (l_debug = 1) THEN
732 			   mydebug(j || ' Serial Number: ' || msnt_record.fm_serial_number, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
733 			END IF;
734 
735 			l_serial_table1(j).serial_number := msnt_record.fm_serial_number;
736 			j := j + 1;
737 		 END LOOP;
738 
739 		 IF (l_debug = 1) THEN
740 			mydebug('Confirmed...', 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
741 		 END IF;
742 
743 		 j := 1; -- serial table counter
744 		 l_primary_quantity := 0;
745 		 FOR lot_serial_rec IN confirmed_lot_serial_cursor LOOP
746 
747 			l_primary_quantity := l_primary_quantity + 1;
748 
749 			WHILE l_serial_table1(j).serial_number <> lot_serial_rec.serial_number LOOP
750 			   l_serial_table1(j).delete_msnt := TRUE;
751 			   j := j + 1;
752 			END LOOP;
753 
754 			IF (l_debug = 1) THEN
755 			   mydebug(j || ' Serial Number: ' || l_serial_table1(j).serial_number, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
756 			END IF;
757 
758 			j := j + 1;
759 		 END LOOP;
760 
761 
762 		 WHILE j <= l_serial_table1.COUNT LOOP
763 			l_serial_table1(j).delete_msnt := TRUE;
764 			j := j + 1;
765 		 END LOOP;
766 
767 		 j := 1;
768 
769 		 WHILE  j <= l_serial_table1.COUNT LOOP
770 
771 			IF l_serial_table1(j).delete_msnt THEN
772 
773 			   IF p_serial_allocated_flag = 'Y' THEN
774 
775 				  IF (l_debug = 1) THEN
776 					 mydebug(j || 'Transferring Serial Number: ' || l_serial_table1(j).serial_number, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
777 				  END IF;
778 
779 				  UPDATE mtl_serial_numbers_temp
780 					SET transaction_temp_id = p_transaction_temp_id2,
781 						last_update_date    = Sysdate,
782 						last_updated_by     = p_user_id
783 					WHERE transaction_temp_id = p_transaction_temp_id1
784 					AND fm_serial_number = l_serial_table1(j).serial_number;
785 				ELSE
786 
787 				  IF (l_debug = 1) THEN
788 					 mydebug(j || 'Deleting Serial Number: ' || l_serial_table1(j).serial_number, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
789 				  END IF;
790 
791 				  DELETE FROM mtl_serial_numbers_temp
792 					WHERE transaction_temp_id = p_transaction_temp_id1
793 					AND fm_serial_number = l_serial_table1(j).serial_number;
794 
795 				  -- unmark serial
796 				  UPDATE mtl_serial_numbers
797 					SET group_mark_id       = NULL,
798 						last_update_date    = Sysdate,
799 						last_updated_by     = p_user_id
800 					WHERE serial_number = l_serial_table1(j).serial_number
801 					AND inventory_item_id = l_inventory_item_id;
802 
803 			   END IF; -- serial allocated flag
804 			END IF;
805 
806 			j := j + 1;
807 		 END LOOP;
808 
809 	   ELSIF p_lot_controlled = 'Y' AND p_serial_controlled = 'Y' THEN -- Lot and serial controlled
810 
811 		 IF (l_debug = 1) THEN
812 			mydebug('Loaded...', 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
813 		 END IF;
814 
815 		 i := 1; -- lot table counter
816 		 j := 1; -- serial table counter
817 		 FOR mtlt_record IN mtlt_cursor(p_transaction_temp_id1) LOOP
818 
819 			IF (l_debug = 1) THEN
820 			   mydebug(i || ' Lot Number: ' || mtlt_record.lot_number, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
821 			END IF;
822 
823 			l_lot_table1(i).lot_number := mtlt_record.lot_number;
824 			l_lot_table1(i).primary_quantity := mtlt_record.primary_quantity;
825 
826 			FOR msnt_record IN msnt_cursor(mtlt_record.serial_transaction_temp_id) LOOP
827 			   IF (l_debug = 1) THEN
828 				  mydebug(j || ' Serial Number: ' || msnt_record.fm_serial_number, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
829 			   END IF;
830 
831 			   l_serial_table1(j).transaction_temp_id := mtlt_record.serial_transaction_temp_id;
832 			   l_serial_table1(j).serial_number := msnt_record.fm_serial_number;
833 			   l_serial_table1(j).lot_number := mtlt_record.lot_number;
834 
835 			   j := j + 1;
836 			END LOOP;
837 
838 			i := i + 1;
839 		 END LOOP;
840 
841 		 IF (l_debug = 1) THEN
842 			mydebug('Confirmed...', 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
843 		 END IF;
844 
845 		 i := 1; -- lot table counter
846 		 j := 1; -- serial table counter
847 		 l_primary_quantity := 0;
848 		 FOR lot_serial_rec IN confirmed_lot_serial_cursor LOOP
849 
850 			l_primary_quantity := l_primary_quantity + 1;
851 
852 			IF (l_debug = 1) THEN
853 			   mydebug(i || ' Lot Number: ' || l_lot_table1(i).lot_number, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
854 			END IF;
855 
856 			IF l_lot_table1(i).lot_number = lot_serial_rec.lot_number THEN
857 			   l_lot_table1(i).secondary_quantity := lot_serial_rec.secondary_quantity;
858 			END IF;
859 
860 			WHILE l_serial_table1(j).serial_number <> lot_serial_rec.serial_number LOOP
861 
862 			   IF (l_debug = 1) THEN
863 				  mydebug(j || 'Marking Serial Number: ' || l_serial_table1(j).serial_number || ' to be deleted', 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
864 			   END IF;
865 
866 			   l_serial_table1(j).delete_msnt := TRUE;
867 
868 			   l_lot_table1(i).primary_quantity := l_lot_table1(i).primary_quantity - 1;
869 			   l_lot_table1(i).delta_primary_quantity := Nvl(l_lot_table1(i).delta_primary_quantity, 0) + 1;
870 			   l_lot_table1(i).update_mtlt := TRUE;
871 
872 			   IF l_lot_table1(i).primary_quantity = 0 THEN
873 				  l_lot_table1(i).delete_mtlt := TRUE;
874 
875 				 IF (l_debug = 1) THEN
876 					mydebug(i || 'Marking Lot Number: ' || l_lot_table1(i).lot_number || ' to be deleted', 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
877 				 END IF;
878 
879 				ELSE
880 
881 				 IF (l_debug = 1) THEN
882 					mydebug(i || 'Marking Lot Number: ' || l_lot_table1(i).lot_number || ' to be updated', 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
883 				 END IF;
884 
885 			   END IF;
886 
887 			   j := j + 1;
888 
889 			   IF l_serial_table1.COUNT >= j THEN
890 				  IF l_serial_table1(j-1).lot_number = l_lot_table1(i).lot_number AND
891 					l_serial_table1(j).lot_number <> l_lot_table1(i).lot_number THEN
892 					 i := i + 1;
893 					 mydebug('Incrementing i', 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
894 				  END IF;
895 			   END IF;
896 
897 			END LOOP;
898 
899 			IF (l_debug = 1) THEN
900 			   mydebug(j || ' Serial Number: ' || l_serial_table1(j).serial_number, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
901 			END IF;
902 
903 			j := j + 1;
904 
905 		 END LOOP;
906 
907 		 WHILE j <= l_serial_table1.COUNT LOOP
908 
909 			IF (l_debug = 1) THEN
910 			   mydebug(j || 'Marking Serial Number: ' || l_serial_table1(j).serial_number || ' to be deleted', 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
911 			END IF;
912 
913 			l_serial_table1(j).delete_msnt := TRUE;
914 
915 			l_lot_table1(i).primary_quantity := l_lot_table1(i).primary_quantity - 1;
916 			l_lot_table1(i).delta_primary_quantity := Nvl(l_lot_table1(i).delta_primary_quantity, 0) + 1;
917 			l_lot_table1(i).update_mtlt := TRUE;
918 
919 			IF l_lot_table1(i).primary_quantity = 0 THEN
920 			   l_lot_table1(i).delete_mtlt := TRUE;
921 
922 			   IF (l_debug = 1) THEN
923 				  mydebug(i || 'Marking Lot Number: ' || l_lot_table1(i).lot_number || ' to be deleted', 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
924 			   END IF;
925 
926 			 ELSE
927 
928 			   IF (l_debug = 1) THEN
929 				  mydebug(i || 'Marking Lot Number: ' || l_lot_table1(i).lot_number || ' to be updated', 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
930 			   END IF;
931 
932 			END IF;
933 
934 			j := j + 1;
935 
936 			IF l_serial_table1.COUNT >= j THEN
937 			   IF l_serial_table1(j-1).lot_number = l_lot_table1(i).lot_number AND
938 				 l_serial_table1(j).lot_number <> l_lot_table1(i).lot_number THEN
939 				  i := i + 1;
940 				  mydebug('Incrementing i', 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
941 			   END IF;
942 			END IF;
943 
944 		 END LOOP;
945 
946 		 j := 1;
947 		 FOR i IN 1..l_lot_table1.COUNT LOOP
948 
949 			l_serial_transaction_temp_id := NULL;
950 
951 			BEGIN
952 			   SELECT serial_transaction_temp_id
953 				 INTO l_serial_transaction_temp_id
954 				 FROM mtl_transaction_lots_temp
955 				 WHERE transaction_temp_id = p_transaction_temp_id2
956 				 AND lot_number = l_lot_table1(i).lot_number;
957 
958 			   l_row_exists := 1;
959 			EXCEPTION
960 			   WHEN no_data_found THEN
961 				  l_row_exists := 0;
962 
963 				  IF p_serial_allocated_flag = 'Y' THEN
964 					 SELECT mtl_material_transactions_s.NEXTVAL
965 					   INTO l_serial_transaction_temp_id
966 					   FROM dual;
967 				  END IF;
968 			END;
969 
970 			IF (l_debug = 1) THEN
971 			   IF l_row_exists = 1 THEN
972 				  mydebug(i || 'Lot Number: ' || l_lot_table1(i).lot_number || ' exists in remaining', 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
973 				ELSE
974 				  mydebug(i || 'Lot Number: ' || l_lot_table1(i).lot_number || ' does not exist in remaining', 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
975 			   END IF;
976 			END IF;
977 
978 			WHILE j <= l_serial_table1.COUNT AND l_serial_table1(j).lot_number = l_lot_table1(i).lot_number LOOP
979 
980 			   IF l_serial_table1(j).delete_msnt THEN
981 
982 				  IF p_serial_allocated_flag = 'Y' THEN
983 
984 					 IF (l_debug = 1) THEN
985 						mydebug(j || 'Transferring Serial Number: ' || l_serial_table1(j).serial_number, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
986 					 END IF;
987 
988 					 UPDATE mtl_serial_numbers_temp
989 					   SET transaction_temp_id = l_serial_transaction_temp_id,
990 						   last_update_date    = Sysdate,
991 						   last_updated_by     = p_user_id
992 					   WHERE transaction_temp_id = l_serial_table1(j).transaction_temp_id
993 					   AND fm_serial_number = l_serial_table1(j).serial_number;
994 				   ELSE
995 
996 					 IF (l_debug = 1) THEN
997 						mydebug(j || 'Deleting Serial Number: ' || l_serial_table1(j).serial_number, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
998 					 END IF;
999 
1000 					 DELETE FROM mtl_serial_numbers_temp
1001 					   WHERE transaction_temp_id = l_serial_table1(j).transaction_temp_id
1002 					   AND fm_serial_number = l_serial_table1(j).serial_number;
1003 
1004 					 -- unmark serial
1005 					 UPDATE mtl_serial_numbers
1006 					   SET group_mark_id       = NULL,
1007 						   last_update_date    = Sysdate,
1008 						   last_updated_by     = p_user_id
1009 					   WHERE serial_number = l_serial_table1(j).serial_number
1010 					   AND inventory_item_id = l_inventory_item_id;
1011 
1012 				  END IF; -- serial allocated flag
1013 				ELSE
1014 				  IF (l_debug = 1) THEN
1015 					 mydebug(j || ' Serial Number: ' || l_serial_table1(j).serial_number || ' left untouched', 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
1016 				  END IF;
1017 			   END IF;
1018 
1019 			   j := j + 1;
1020 
1021 			END LOOP;
1022 
1023 			IF l_lot_table1(i).delete_mtlt THEN
1024 
1025 			   IF l_row_exists = 1 THEN
1026 
1027 				  IF (l_debug = 1) THEN
1028 					 mydebug(i || 'Deleting Lot Number: ' || l_lot_table1(i).lot_number, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
1029 				  END IF;
1030 
1031 				  UPDATE mtl_transaction_lots_temp
1032 					SET primary_quantity = primary_quantity + l_lot_table1(i).delta_primary_quantity,
1033 						transaction_quantity = transaction_quantity +
1034 											   Round(l_lot_table1(i).delta_primary_quantity * l_conversion_factor2,
1035 													 l_g_decimal_precision),
1036 						last_update_date     = Sysdate,
1037 						last_updated_by      = p_user_id
1038 					WHERE transaction_temp_id = p_transaction_temp_id2
1039 					AND lot_number = l_lot_table1(i).lot_number;
1040 
1041 				  DELETE FROM mtl_transaction_lots_temp
1042 					WHERE transaction_temp_id = p_transaction_temp_id1
1043 					AND lot_number = l_lot_table1(i).lot_number;
1044 
1045 				ELSE -- row does not exist in 2
1046 
1047 				  IF (l_debug = 1) THEN
1048 					 mydebug(i || 'Transferring Lot Number: ' || l_lot_table1(i).lot_number, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
1049 				  END IF;
1050 
1051 				  UPDATE mtl_transaction_lots_temp
1052 					SET transaction_temp_id        = p_transaction_temp_id2,
1053 						serial_transaction_temp_id = l_serial_transaction_temp_id,
1054 						transaction_quantity       = Round(primary_quantity * l_conversion_factor2, l_g_decimal_precision),
1055 						last_update_date           = Sysdate,
1056 						last_updated_by            = p_user_id
1057 					WHERE transaction_temp_id = p_transaction_temp_id1
1058 					AND lot_number = l_lot_table1(i).lot_number;
1059 
1060 			   END IF;
1061 
1062 			 ELSIF l_lot_table1(i).update_mtlt THEN
1063 
1064 			   UPDATE mtl_transaction_lots_temp
1065 				 SET primary_quantity     = l_lot_table1(i).primary_quantity,
1066 					 transaction_quantity = Round(l_lot_table1(i).primary_quantity * l_conversion_factor, l_g_decimal_precision),
1067 					 secondary_quantity   = l_lot_table1(i).secondary_quantity,
1068 					 last_update_date     = Sysdate,
1069 					 last_updated_by      = p_user_id
1070 				 WHERE transaction_temp_id = p_transaction_temp_id1
1071 				 AND lot_number = l_lot_table1(i).lot_number;
1072 
1073 			   IF l_row_exists = 1 THEN
1074 
1075 				  IF (l_debug = 1) THEN
1076 					 mydebug(i || 'Updating Lot Number: ' || l_lot_table1(i).lot_number, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
1077 				  END IF;
1078 
1079 				  UPDATE mtl_transaction_lots_temp
1080 					SET primary_quantity     = primary_quantity + l_lot_table1(i).delta_primary_quantity,
1081 						transaction_quantity = transaction_quantity + Round(l_lot_table1(i).delta_primary_quantity * l_conversion_factor2,
1082 																			l_g_decimal_precision),
1083 						last_update_date     = Sysdate,
1084 						last_updated_by      = p_user_id
1085 					WHERE transaction_temp_id = p_transaction_temp_id2
1086 					AND lot_number = l_lot_table1(i).lot_number;
1087 				ELSE -- row does not exist in 2
1088 
1089 				  IF (l_debug = 1) THEN
1090 					 mydebug(i || 'Inserting Lot Number: ' || l_lot_table1(i).lot_number, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
1091 				  END IF;
1092 
1093 				  SELECT *
1094 					INTO l_mtlt_rec
1095 					FROM mtl_transaction_lots_temp
1096 					WHERE transaction_temp_id = p_transaction_temp_id1
1097 					AND lot_number = l_lot_table1(i).lot_number;
1098 
1099 				  l_mtlt_rec.transaction_temp_id        := p_transaction_temp_id2;
1100 				  l_mtlt_rec.serial_transaction_temp_id := l_serial_transaction_temp_id;
1101 				  l_mtlt_rec.transaction_quantity       := Round(l_mtlt_rec.primary_quantity * l_conversion_factor2, l_g_decimal_precision);
1102 
1103 				  proc_insert_mtlt
1104 				   (p_lot_record         => l_mtlt_rec,
1105 					x_return_status      => x_return_status,
1106 					x_msg_count          => x_msg_count,
1107 					x_msg_data           => x_msg_data);
1108 
1109 				  mydebug('Return status from insert MTLT : ' || x_return_status, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
1110 				  IF x_return_status <> l_g_ret_sts_success THEN
1111 					 RAISE fnd_api.g_exc_error;
1112 				  END IF;
1113 
1114 			   END IF; -- row exists
1115 
1116 			END IF; -- delete/update mtlt
1117 
1118 		 END LOOP; -- lot loop
1119 
1120 	  END IF; -- lot/serial/lot serial
1121    END IF; -- no control/lot/serial/lot serial
1122 
1123    -- Update MMTT records
1124    l_delta_primary_quantity := l_primary_quantity1 - l_primary_quantity;
1125  l_delta_secondary_quantity := l_secondary_quantity1 - l_secondary_quantity;
1126 
1127    IF (l_debug = 1) THEN
1128 	  mydebug('Updating MMTT with delta qty: ' || l_delta_primary_quantity, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
1129   mydebug('Updating MMTT with delta sec qty: ' || l_delta_secondary_quantity, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
1130    END IF;
1131 
1132    UPDATE mtl_material_transactions_temp
1133 	 SET transaction_quantity           = Round(l_primary_quantity*l_conversion_factor, l_g_decimal_precision),
1134 		 transaction_uom                = p_transaction_uom,
1135 		 primary_quantity               = l_primary_quantity,
1136 		 secondary_transaction_quantity = l_secondary_quantity,
1137 		 secondary_uom_code             = p_secondary_uom,
1138 		 lpn_id                         = content_lpn_id,
1139 		 content_lpn_id                 = NULL,
1140 		 transfer_lpn_id                = p_transfer_lpn_id,
1141 		 last_update_date               = Sysdate,
1142 		 last_updated_by                = p_user_id
1143 	 WHERE transaction_temp_id = p_transaction_temp_id1
1144 	 returning lpn_id INTO l_lpn_id;
1145 
1146    -- Bug5659809: update last_update_date and last_update_by as well
1147    UPDATE wms_license_plate_numbers
1148 	 SET lpn_context = 1
1149 	   --, last_update_date = SYSDATE /* Bug 9448490 Lot Substitution Project */
1150 	   --, last_updated_by = fnd_global.user_id /* Bug 9448490 Lot Substitution Project */
1151 	 WHERE lpn_id = l_lpn_id;
1152 
1153    -- Update remaining MMTT record
1154    UPDATE mtl_material_transactions_temp
1155 	 SET transaction_quantity = transaction_quantity +  Round(l_delta_primary_quantity*l_conversion_factor2, l_g_decimal_precision),
1156 		 primary_quantity     = primary_quantity + l_delta_primary_quantity,
1157    secondary_transaction_quantity = secondary_transaction_quantity+l_delta_secondary_quantity,
1158 		 last_update_date     = Sysdate,
1159 		 last_updated_by      = p_user_id
1160 	 WHERE transaction_temp_id = p_transaction_temp_id2;
1161 
1162 EXCEPTION
1163    WHEN fnd_api.g_exc_error THEN
1164 	  x_return_status := l_g_ret_sts_error;
1165 	  fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
1166 
1167 	  IF (l_debug = 1) THEN
1168 		 mydebug('Error', 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
1169 	  END IF;
1170 	  ROLLBACK;
1171 
1172    WHEN OTHERS THEN
1173 	  x_return_status  := l_g_ret_sts_unexp_error;
1174 	  fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
1175 	  IF (l_debug = 1) THEN
1176 		 mydebug('Error: ' || Sqlerrm, 'WMS_TASK_LOAD.UPDATE_LOADED_PART');
1177 	  END IF;
1178 	  ROLLBACK ;
1179 END update_loaded_part;
1180 
1181 PROCEDURE task_merge_split(
1182 			   p_action                    IN            VARCHAR2  -- LOAD_MULTIPLE/LOAD_SINGLE/SPLIT/UPDATE_LOADED
1183 			  ,p_exception                 IN            VARCHAR2  -- SHORT/OVER
1184 			  ,p_organization_id           IN            NUMBER
1185 			  ,p_user_id                   IN            NUMBER
1186 			  ,p_transaction_header_id     IN            NUMBER
1187 			  ,p_transaction_temp_id       IN            NUMBER
1188 			  ,p_parent_line_id            IN            NUMBER
1189 			  ,p_remaining_temp_id         IN            NUMBER
1190 			  ,p_lpn_id                    IN            NUMBER
1191 			  ,p_content_lpn_id            IN            NUMBER
1192 			  ,p_transfer_lpn_id           IN            NUMBER
1193 			  ,p_confirmed_sub             IN            VARCHAR2
1194 			  ,p_confirmed_locator_id      IN            NUMBER
1195 			  ,p_confirmed_uom             IN            VARCHAR2
1196 			  ,p_suggested_uom             IN            VARCHAR2
1197 			  ,p_primary_uom               IN            VARCHAR2
1198 			  ,p_inventory_item_id         IN            NUMBER
1199 			  ,p_revision                  IN            VARCHAR2
1200 			  ,p_confirmed_trx_qty         IN            NUMBER
1201 			  ,p_confirmed_lots            IN            VARCHAR2
1202 			  ,p_confirmed_lot_trx_qty     IN            VARCHAR2
1203 			  ,p_confirmed_sec_uom         IN            VARCHAR2
1204 			  ,p_confirmed_sec_qty         IN            VARCHAR2
1205 			  ,p_confirmed_serials         IN            VARCHAR2
1206 			  ,p_container_item_id         IN            NUMBER
1207 			  ,p_lpn_match                 IN            NUMBER
1208 			  ,p_lpn_match_lpn_id          IN            NUMBER
1209 			  ,p_serial_allocated_flag     IN            VARCHAR2
1210 			  ,p_lot_controlled            IN            VARCHAR2  -- Y/N
1211 			  ,p_serial_controlled         IN            VARCHAR2  -- Y/N
1212 			  ,p_parent_lpn_id             IN            NUMBER
1213 			  ,x_new_transaction_temp_id   OUT NOCOPY    NUMBER
1214 			  ,x_cms_check                 OUT NOCOPY    VARCHAR2  -- FAIL/PASS
1215 			  ,x_return_status             OUT NOCOPY    VARCHAR2
1216 			  ,x_msg_count                 OUT NOCOPY    NUMBER
1217 			  ,x_msg_data                  OUT NOCOPY    VARCHAR2
1218 			  ,p_substitute_lots	       IN	         VARCHAR2--/* Bug 9448490 Lot Substitution Project */
1219 			  ,p_full_lot_allocation       IN            VARCHAR2 DEFAULT 'Y'  --Added for 14699845 (Flexible Lot Allocation)
1220 			  ,p_fulfillment_base          IN            VARCHAR2 DEFAULT 'P') --16070349
1221 IS
1222    --PRAGMA AUTONOMOUS_TRANSACTION;
1223    l_proc_name                      VARCHAR2(30) :=  'TASK_MERGE_SPLIT';
1224    l_debug                          NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
1225    l_progress                       VARCHAR2(30) :=  '100';
1226    l_transaction_temp_id_to_merge   NUMBER       :=  NULL;
1227    l_new_transaction_temp_id        NUMBER       :=  NULL;
1228    l_insert                         VARCHAR2(2)  :=  NULL;
1229    l_update                         VARCHAR2(2)  :=  NULL;
1230    l_action                         VARCHAR2(30) :=  p_action;
1231    l_pick_ok                        VARCHAR2(1)  :=  'Y';
1232 
1233    l_lpn_id                NUMBER := p_lpn_id;
1234    l_content_lpn_id        NUMBER := p_content_lpn_id;
1235    l_parent_lpn_id         NUMBER := p_parent_lpn_id;
1236    l_transfer_lpn_id       NUMBER := p_transfer_lpn_id;
1237    l_container_item_id     NUMBER := p_container_item_id;
1238    l_lpn_match_lpn_id      NUMBER := p_lpn_match_lpn_id;
1239 
1240    l_lpn_context_pregenerated    CONSTANT NUMBER := WMS_Container_PUB.LPN_CONTEXT_PREGENERATED;
1241    l_lpn_context_inv             CONSTANT NUMBER := WMS_Container_PUB.LPN_CONTEXT_INV;
1242    l_lpn_context_picked          CONSTANT NUMBER := WMS_Container_PUB.LPN_CONTEXT_PICKED;
1243    l_lpn_context_packing         CONSTANT NUMBER := WMS_Container_PUB.LPN_CONTEXT_PACKING ;
1244 
1245    CURSOR  cur_split_mmtts
1246    IS
1247 	  SELECT transaction_temp_id
1248 	  FROM   mtl_material_transactions_temp
1249 	  WHERE  transaction_header_id  = p_transaction_header_id
1250 	  AND    transaction_temp_id   <> p_transaction_temp_id
1251 	  AND    inventory_item_id      = p_inventory_item_id
1252 	  AND    nvl(revision,'@@')     = nvl(p_revision,'@@')
1253 	  AND    subinventory_code      = p_confirmed_sub
1254 	  AND    locator_id             = p_confirmed_locator_id
1255 	  AND    transaction_uom        = p_confirmed_uom
1256 	  AND    transfer_lpn_id        = p_transfer_lpn_id
1257 	  AND    nvl(content_lpn_id,0)  = nvl(p_content_lpn_id,0)
1258 	  AND    nvl(lpn_id,0)          = nvl(p_lpn_id,0);
1259 
1260 BEGIN
1261 
1262    --/*
1263    -- P_action can have foll. values  'LOAD_MULTIPLE' , 'LOAD_SINGLE' or 'SPLIT'
1264    -- newly genertated LPN is already in wms_lpn table when PichMore Is pressed
1265    -- if fromLpn = toLPN  -- FULLY COMSUMABLE
1266    --    from = content_lpn_id, to=transfer_lpn_id
1267    --    content_lpn_id is always nested into transafer_lpn_id
1268    --  else  from = lpn_id, to=transfer_lpn_id.
1269    -- l_insert    = 'Y'  means Yes, --insert new MMTT
1270    -- l_update    = 'Y1' means Yes But, 1 update , update original MMTT to reduce qty
1271    -- l_update    = 'Y2' means Yes but, 2 updates
1272    -- update original MMTT to reduce qty and
1273    -- update mergeable MMTT to add qty
1274    /*  MMTT management
1275    --   Action l_insert  L_update  update orginalMMTT  UpdMergeMMTT  InsertNewMMTT    WMS_TASK_STATUS CHANGE
1276    -----------------------------------------------------------------------------------------------
1277    --   SPLIT  Y         Y1        N                   N             Y                new MMTT inserted with loaded status
1278    --   SPLIT  N         Y2        Y                   Y             N                no change
1279    --   LOAD_M N         Y1        Y                   N             N                original MMTT updated to loaded status
1280    --   LOAD_M N         Y2        Y-Delete            Y             N                no change
1281    --   LOAD_S N         Y1        Y                   N             N                original MMTT updated to loaded status
1282    --*/
1283 
1284    x_return_status  := l_g_ret_sts_success;
1285    g_debug := l_debug;
1286 
1287    IF p_lpn_id = 0            THEN l_lpn_id := NULL;             END IF;
1288    IF p_content_lpn_id = 0    THEN l_content_lpn_id := NULL;     END IF;
1289    IF p_parent_lpn_id = 0     THEN l_parent_lpn_id := NULL;      END IF;
1290    IF p_transfer_lpn_id = 0   THEN l_transfer_lpn_id := NULL;    END IF;
1291    IF p_container_item_id = 0 THEN l_container_item_id := NULL;  END IF;
1292    IF p_lpn_match_lpn_id = 0  THEN l_lpn_match_lpn_id := NULL;   END IF;
1293 
1294    mydebug ('p_action                 = ' || p_action                 );
1295    mydebug ('p_exception              = ' || p_exception              );
1296    mydebug ('p_transaction_header_id  = ' || p_transaction_header_id  );
1297    mydebug ('p_transaction_temp_id    = ' || p_transaction_temp_id    );
1298    mydebug ('p_remaining_temp_id      = ' || p_remaining_temp_id    );
1299    mydebug ('p_parent_line_id         = ' || p_parent_line_id         );
1300    mydebug ('p_lpn_id                 = ' || p_lpn_id || ':' || l_lpn_id);
1301    mydebug ('p_content_lpn_id         = ' || p_content_lpn_id || ':' || l_content_lpn_id);
1302    mydebug ('p_parent_lpn_id         = ' || p_parent_lpn_id || ':' ||
1303 																	 l_parent_lpn_id);
1304    mydebug ('p_transfer_lpn_id        = ' || p_transfer_lpn_id  || ':' || l_transfer_lpn_id);
1305    mydebug ('p_confirmed_sub          = ' || p_confirmed_sub          );
1306    mydebug ('p_confirmed_locator_id   = ' || p_confirmed_locator_id   );
1307    mydebug ('p_confirmed_uom          = ' || p_confirmed_uom          );
1308    mydebug ('p_suggested_uom          = ' || p_suggested_uom          );
1309    mydebug ('p_primary_uom            = ' || p_primary_uom            );
1310    mydebug ('p_inventory_item_id      = ' || p_inventory_item_id      );
1311    mydebug ('p_revision               = ' || p_revision               );
1312    mydebug ('p_confirmed_trx_qty      = ' || p_confirmed_trx_qty      );
1313    mydebug ('p_confirmed_lots         = ' || p_confirmed_lots         );
1314    mydebug ('p_confirmed_lot_trx_qty  = ' || p_confirmed_lot_trx_qty  );
1315    mydebug ('p_confirmed_sec_uom      = ' || p_confirmed_sec_uom      );
1316    mydebug ('p_confirmed_sec_qty      = ' || p_confirmed_sec_qty      );
1317    mydebug ('p_confirmed_serials      = ' || p_confirmed_serials      );
1318    mydebug ('p_serial_allocated_flag  = ' || p_serial_allocated_flag  );
1319    mydebug ('p_lpn_match              = ' || p_lpn_match              );
1320    mydebug ('p_lpn_match_lpn_id       = ' || p_lpn_match_lpn_id  || ':' || l_lpn_match_lpn_id);
1321    mydebug ('p_container_item_id      = ' || p_container_item_id || ':' || l_container_item_id);
1322    mydebug ('p_lot_controlled         = ' || p_lot_controlled );
1323    mydebug ('p_serial_controlled      = ' || p_serial_controlled );
1324    mydebug ('p_substitute_lots        = ' || p_substitute_lots );       --HWSNIssue 13860546
1325    mydebug ('p_full_lot_allocation    = ' || p_full_lot_allocation );   --Added for 14699845 (Flexible Lot Allocation)
1326    mydebug ('p_fulfillment_base       = ' || p_fulfillment_base );      --16070349
1327 
1328 
1329 	--Insert the MSNTs HWSNIssue 13860546
1330 	IF (    NVL (p_lot_controlled,'N') = 'Y'
1331 		AND NVL (p_serial_controlled,'N') = 'Y'
1332 		AND NVL (p_serial_allocated_flag, 'N') = 'N'
1333 		AND p_lpn_match IN (1, 3)
1334 		AND lpn_has_unalloc_lots (NVL (l_content_lpn_id,l_lpn_id),
1335 								  p_organization_id,
1336 								  p_inventory_item_id,
1337 								  p_revision,
1338 								  p_transaction_temp_id
1339 								 )
1340 	   )
1341 	THEN
1342 	  --Have to review this condition
1343 	   mydebug ('Inserting msnts');
1344 	   insert_serials_temp (p_transaction_temp_id      => p_transaction_temp_id,
1345 							p_lpn_id                   => NVL (l_content_lpn_id , l_lpn_id),
1346 							p_org_id                   => p_organization_id,
1347 							p_item_id                  => p_inventory_item_id,
1348 							p_rev                      => p_revision,
1349 							p_confirmed_trx_qty        => p_confirmed_trx_qty,
1350 							x_return_status            => x_return_status,
1351 							x_msg                      => x_msg_data
1352 						   );
1353 		  mydebug ('x_return_status '||x_return_status);
1354 	   IF x_return_status <> l_g_ret_sts_success
1355 	   THEN
1356 		  mydebug ('Error while inserting MSNTs');
1357 		  mydebug ('x_msg_data' ||x_msg_data);
1358 		  RAISE fnd_api.g_exc_error;
1359 	   END IF;
1360 	END IF;
1361 	--HWSNIssue 13860546
1362 
1363    IF p_action = 'UPDATE_LOADED' THEN
1364 	  update_loaded_part
1365 		(p_user_id                   => p_user_id,
1366 		 p_organization_id 			 => p_organization_id, --BUG12622871LSC
1367 		 p_transaction_temp_id1      => p_transaction_temp_id,
1368 		 p_transaction_temp_id2      => p_remaining_temp_id,
1369 		 p_transfer_lpn_id           => l_transfer_lpn_id,
1370 		 p_transaction_uom           => p_confirmed_uom,
1371 		 p_transaction_quantity      => p_confirmed_trx_qty,
1372 		 p_lot_numbers               => p_confirmed_lots,
1373 		 p_lot_transaction_quantity  => p_confirmed_lot_trx_qty,
1374 		 p_secondary_uom             => p_confirmed_sec_uom,
1375 		 p_secondary_quantity        => p_confirmed_sec_qty,
1376 		 p_serial_numbers            => p_confirmed_serials,
1377 		 p_serial_allocated_flag     => p_serial_allocated_flag,
1378 		 p_lot_controlled            => p_lot_controlled,
1379 		 p_serial_controlled         => p_serial_controlled,
1380 		 x_return_status             => x_return_status,
1381 		 x_msg_count                 => x_msg_count,
1382 		 x_msg_data                  => x_msg_data);
1383 
1384 	  mydebug('x_return_status : ' || x_return_status);
1385 	  IF x_return_status <> l_g_ret_sts_success THEN
1386 		 RAISE fnd_api.g_exc_error;
1387 	  END IF;
1388 
1389 	ELSE
1390 	   l_progress    :=  '200';
1391 	   mydebug ('l_progress: ' || l_progress  || ' : Call Can_picdrop to check Move order line status' );
1392 	   x_cms_check := 'PASS';
1393 	   -- no need to do CMS check for BULK task
1394 	   IF p_transaction_temp_id <> p_parent_line_id THEN
1395 		  x_cms_check :=  can_pickdrop(p_transaction_temp_id );
1396 		  mydebug ('x_cms_check: ' || x_cms_check );
1397 		  IF  x_cms_check = 'FAIL'
1398 		  THEN
1399 			 --fnd_message.set_name('WMS', 'WMS_CANCELLED_SOURCE');
1400 			 -- Source of the task is cancelled by source.. F2 to rollback
1401 			 --fnd_msg_pub.ADD;
1402 			 RAISE fnd_api.g_exc_error;
1403 		  END IF;
1404 	   END IF;
1405 
1406 
1407 	   /* Update the LPN context to "Packing context" (8).
1408 	   -- if p_lpn_match = 1,3 (exact match or fully consumable LPN) then
1409 	   -- p_lpn_match_lpn_id will be set to Packing context(8) otherwise , we will not
1410 	   -- change the context of from lpn .
1411 	   -- Always set the status of p_transfer_lpn_id = Packing context(8) , whether
1412 	   -- pre-generated or already in Packing context" */
1413 	   --
1414 		l_progress    :=  '200';
1415 		mydebug ('l_progress: ' || l_progress );
1416 		mydebug ('Updating LPN context for: l_lpn_match_lpn_id ' || l_lpn_match_lpn_id || ' And l_transfer_lpn_id ' || l_transfer_lpn_id || ' And l_lpn_id is ' || l_lpn_id);
1417 	  IF p_lpn_match in (1,3) THEN -- fully consumable lpn or exact match
1418 		IF l_lpn_id IS NULL  THEN -- lpn_id column of MMTT
1419 			/*If we are transferring the contents of from lpn (ie. UI had Xfr LPN enabled
1420 			  instead of INTO LPN) then do not change the context of the LPN to packing.
1421 			  TM needs it to be 'resides in inv' to be able to unpack the material */
1422 			BEGIN
1423 			 -- Bug5659809: update last_update_date and last_update_by as well
1424 			  UPDATE wms_license_plate_numbers
1425 				SET lpn_context = l_lpn_context_packing
1426 				   ,catch_weight_flag = NULL --16466288
1427 				 -- , last_update_date = SYSDATE /* Bug 9448490 Lot Substitution Project */
1428 				 -- , last_updated_by = fnd_global.user_id /* Bug 9448490 Lot Substitution Project */
1429 			  WHERE lpn_id = l_lpn_match_lpn_id
1430 				AND lpn_context = l_lpn_context_inv --, l_transfer_lpn_id)
1431 				AND organization_id = p_organization_id;
1432 
1433 			  IF SQL%NOTFOUND THEN
1434 				 mydebug ('Cannot find LPNs to update the context' );
1435 				 fnd_message.set_name('WMS', 'WMS_WRONG_FROM_LPN_CONTEXT');
1436 				 -- FROM LPN Context is not valid '
1437 				 fnd_msg_pub.ADD;
1438 				 RAISE fnd_api.g_exc_error;
1439 
1440 			  END IF;
1441 			 EXCEPTION
1442 				 WHEN OTHERS THEN
1443 					mydebug ('Others exception while updating From LPN context: ' || SQLCODE);
1444 				 RAISE fnd_api.g_exc_error;
1445 			 END ;
1446 		 END IF;
1447 		ELSIF (p_lpn_match = 4 AND l_transfer_lpn_id = l_lpn_match_lpn_id) THEN 	--added following so that lpn context is updated properly after load bug 12595055  OR (l_lpn_id = l_lpn_match_lpn_id))
1448 			 mydebug ('Coming to the elseif portion with lpn match values as ' || p_lpn_match );
1449 			BEGIN
1450 				UPDATE wms_license_plate_numbers
1451 				SET lpn_context = l_lpn_context_packing
1452 				   ,catch_weight_flag = NULL --16466288
1453 			  WHERE lpn_id = l_lpn_match_lpn_id
1454 				AND lpn_context = l_lpn_context_inv --, l_transfer_lpn_id)
1455 				AND organization_id = p_organization_id;
1456 
1457 			  IF SQL%NOTFOUND THEN
1458 				 mydebug ('Cannot find LPNs to update the context' );
1459 				 fnd_message.set_name('WMS', 'WMS_WRONG_FROM_LPN_CONTEXT');
1460 				 -- FROM LPN Context is not valid '
1461 				 fnd_msg_pub.ADD;
1462 				 RAISE fnd_api.g_exc_error;
1463 
1464 			  END IF;
1465 			 EXCEPTION
1466 				 WHEN OTHERS THEN
1467 					mydebug ('Others exception while updating From LPN context: ' || SQLCODE);
1468 				 RAISE fnd_api.g_exc_error;
1469 			 END ;
1470 		END IF;
1471 	  l_progress    :=  '300';
1472 	  mydebug ('l_progress: ' || l_progress );
1473 	  /*If we are transferring the contents of from lpn (fully consumable) (ie. UI had Xfr LPN enabled
1474 		instead of INTO LPN) then do not change the context of the LPN to packing.
1475 		TM needs it to be 'resides in inv' to be able to unpack the material */
1476 	  IF (l_lpn_id IS NOT NULL  AND
1477 		  l_lpn_id <> l_transfer_lpn_id)
1478 		OR
1479 		 (l_lpn_id IS NULL)
1480 	  THEN
1481 		 BEGIN
1482 
1483 		   -- Bug5659809: update last_update_date and last_update_by as well
1484 		   UPDATE wms_license_plate_numbers
1485 			 SET lpn_context = l_lpn_context_packing
1486 				,catch_weight_flag = NULL --16466288
1487 			   --, last_update_date = SYSDATE /* Bug 9448490 Lot Substitution Project */
1488 			   --, last_updated_by = fnd_global.user_id /* Bug 9448490 Lot Substitution Project */
1489 		   WHERE lpn_id = l_transfer_lpn_id
1490 			 AND lpn_context in (l_lpn_context_packing , l_lpn_context_pregenerated)
1491 			 AND organization_id = p_organization_id;
1492 
1493 		   IF SQL%NOTFOUND THEN
1494 			  mydebug ('Cannot find LPNs to update the context' );
1495 			  fnd_message.set_name('WMS', 'WMS_WRONG_TO_LPN_CONTEXT');
1496 			  -- To LPN Context is not valid
1497 			  fnd_msg_pub.ADD;
1498 
1499 			  RAISE fnd_api.g_exc_error;
1500 		   END IF;
1501 		  EXCEPTION
1502 			  WHEN OTHERS THEN
1503 				 mydebug ('Others exception while updating To LPN context: ' || SQLCODE);
1504 				 RAISE fnd_api.g_exc_error;
1505 		  END ;
1506 	  END IF;
1507 	  -- Find a matching MMTT within the given header_id only if
1508 	  -- p_action = LOAD_MULTIPLE. for LOAD_SINGLE, we need not try to find
1509 	  -- a mergeable MMTT
1510 		l_progress    :=  '400';
1511 		mydebug ('l_progress: ' || l_progress );
1512 	  IF p_action = l_g_action_split  OR
1513 		p_action = l_g_action_load_multiple --  ('SPLIT', 'LOAD_MULTIPLE')
1514 		THEN
1515 		 l_progress    :=  '410';
1516 		 mydebug ('l_progress: ' || l_progress );
1517 		 FOR rec_split_mmtts  IN  cur_split_mmtts
1518 		   LOOP
1519 			  l_progress    :=  '420';
1520 			  mydebug ('l_progress: ' || l_progress );
1521 			  l_transaction_temp_id_to_merge := rec_split_mmtts.transaction_temp_id;
1522 			  mydebug('in loop..l_transaction_temp_id_to_merge = ' || l_transaction_temp_id_to_merge );
1523 			  EXIT;
1524 		   END LOOP;
1525 	  END IF;
1526 
1527 
1528 	  l_progress    :=  '500';
1529 	  mydebug ('l_progress: ' || l_progress );
1530 	  mydebug('l_transaction_temp_id_to_merge = ' || l_transaction_temp_id_to_merge );
1531 
1532 	  -- For pick short , it should work as a split so that we can process the workflow
1533 	  IF p_exception = l_g_exception_short
1534 		THEN
1535 		 l_action := l_g_action_split;  -- SPLIT
1536 	   ELSE
1537 		 l_action := p_action;
1538 	  END IF;
1539 
1540 	  mydebug('l_action: ' || l_action);
1541 	  IF  l_transaction_temp_id_to_merge IS NULL
1542 		THEN
1543 		 IF l_action = l_g_action_split  THEN
1544 			l_insert      := 'Y';  --insert new MMTT
1545 			l_update      := 'Y1'; -- update original MMTT to reduce qty
1546 		  ELSE  -- LOAD_SINGLE or LOAD_MULTIPLE
1547 			l_insert      := 'N';  -- do not insert new MMTT
1548 			l_update      := 'Y1'; -- update original MMTT = conmfirmed qty
1549 		 END IF;
1550 	   ELSE
1551 		 l_insert      := 'N';  -- Do not insert new MMTT
1552 		 l_update      := 'Y2'; -- 2 updates. 1- original MMTT to reduce qty
1553 		 --            2- mergeable MMTT to add qty
1554 	  END IF;
1555 	  mydebug('l_insert:' || l_insert || ':l_update:'|| l_update);
1556 	  proc_insert_update_mmtt
1557 		(p_action                             => l_action
1558 		 ,p_insert                             => l_insert
1559 		 ,p_update                             => l_update
1560 		 ,p_organization_id                    => p_organization_id
1561 		 ,p_user_id                            => p_user_id
1562 		 ,p_transaction_header_id              => p_transaction_header_id
1563 		 ,p_transaction_temp_id                => p_transaction_temp_id
1564 		 ,p_transaction_temp_id_to_merge       => l_transaction_temp_id_to_merge
1565 		 ,p_lpn_id                             => l_lpn_id
1566 		 ,p_content_lpn_id                     => l_content_lpn_id
1567 		 ,p_transfer_lpn_id                    => l_transfer_lpn_id
1568 		 ,p_confirmed_sub                      => p_confirmed_sub
1569 		 ,p_confirmed_locator_id               => p_confirmed_locator_id
1570 		 ,p_confirmed_uom                      => p_confirmed_uom
1571 		 ,p_suggested_uom                      => p_suggested_uom
1572 		 ,p_primary_uom                        => p_primary_uom
1573 		 ,p_inventory_item_id                  => p_inventory_item_id
1574 		 ,p_revision                           => p_revision
1575 		 ,p_confirmed_trx_qty                  => p_confirmed_trx_qty
1576 		 ,p_confirmed_lots                     => p_confirmed_lots
1577 		 ,p_confirmed_lot_trx_qty              => p_confirmed_lot_trx_qty
1578 		 ,p_confirmed_sec_uom                  => p_confirmed_sec_uom
1579 		 ,p_confirmed_sec_qty                  => p_confirmed_sec_qty
1580 		 ,p_confirmed_serials                  => p_confirmed_serials
1581 		 ,p_container_item_id                  => l_container_item_id
1582 		 ,p_lpn_match                          => p_lpn_match
1583 		 ,p_lpn_match_lpn_id                   => l_lpn_match_lpn_id
1584 		 ,p_serial_allocated_flag              => p_serial_allocated_flag
1585 		 ,p_lot_controlled                     => p_lot_controlled
1586 		 ,p_serial_controlled                  => p_serial_controlled
1587 		 ,p_wms_task_status                    => l_g_task_loaded
1588 		 ,p_exception                          => p_exception
1589 		 ,p_parent_lpn_id                      => l_parent_lpn_id
1590 		 ,x_new_transaction_temp_id            => l_new_transaction_temp_id
1591 		 ,x_return_status                      => x_return_status
1592 		 ,x_msg_count                          => x_msg_count
1593 		 ,x_msg_data                           => x_msg_data
1594 		 ,p_substitute_lots	                   => p_substitute_lots --/* Bug 9448490 Lot Substitution Project */
1595 		 ,p_full_lot_allocation                => p_full_lot_allocation); -- Added for 14699845 (Flexible Lot Allocation)
1596 
1597 	  IF x_return_status <> l_g_ret_sts_success THEN
1598 		 mydebug('x_return_status : ' || x_return_status);
1599 		 RAISE fnd_api.G_EXC_ERROR;
1600 	  END IF;
1601 	  x_new_transaction_temp_id := l_new_transaction_temp_id;
1602 	  mydebug('x_new_transaction_temp_id : '  || x_new_transaction_temp_id);
1603 
1604 	  /*  Task management
1605 	  Action l_insert   L_update  update orginalTSK UpdMergeTSK   InsertNewTSK
1606 		SPLIT Y   Y1  N   N  Y
1607 		SPLIT N   Y2  Y   Y  N
1608 		LOAD_M N   Y1  Y   N  N
1609 		LOAD_M N   Y2  Y-Delete  Y  N
1610 		LOAD_S N   Y1  Y   N  N
1611 		*/
1612 
1613 		l_progress    :=  '500';
1614 	  mydebug ('l_progress: ' || l_progress );
1615 	  proc_insert_update_task -- new task using p_transaction_temp_id);
1616 		(p_action                    => l_action
1617 		 ,p_insert                    => l_insert
1618 		 ,p_update                    => l_update
1619 		 ,p_temp_id                   => p_transaction_temp_id
1620 		 ,p_new_temp_id               => l_new_transaction_temp_id -- will be notNULL only if p_insert=Y
1621 		 ,p_merge_temp_id             => l_transaction_temp_id_to_merge
1622 		 ,p_task_status               => l_g_task_loaded
1623 		 ,p_user_id                   => p_user_id
1624 		 ,x_return_status             => x_return_status
1625 		 ,x_msg_count                 => x_msg_count
1626 		 ,x_msg_data                  => x_msg_data);
1627 
1628 	  IF x_return_status <> l_g_ret_sts_success THEN
1629 		 --x_return_status  := l_g_ret_sts_success;
1630 		 RAISE fnd_api.G_EXC_ERROR;
1631 	  END IF;
1632    END IF;
1633 
1634    l_progress := 'END';
1635    mydebug('COmmit ' );
1636    COMMIT;
1637    mydebug('End .. ' || l_proc_name);
1638 EXCEPTION
1639    WHEN fnd_api.g_exc_error THEN
1640 	  x_return_status  := l_g_ret_sts_error;
1641 	  fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
1642 	  mydebug('ROLLBACK ' );
1643 	  ROLLBACK ;
1644 	  mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
1645 	  mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
1646 	  mydebug('x_msg_count/Data = ' || x_msg_count || '/'|| x_msg_data);
1647    WHEN fnd_api.g_exc_unexpected_error THEN
1648 	  x_return_status  := fnd_api.g_ret_sts_unexp_error;
1649 	  fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
1650 	  mydebug('ROLLBACK ' );
1651 	  ROLLBACK ;
1652 	  mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
1653 	  mydebug('RAISE fnd_api.g_exc_unexpected_error: ' || SQLERRM);
1654 	  mydebug('x_msg_count/Data = ' || x_msg_count || '/'|| x_msg_data);
1655    WHEN OTHERS THEN
1656 	  x_return_status  := l_g_ret_sts_unexp_error;
1657 	  fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
1658 	  mydebug('ROLLBACK ' );
1659 	  ROLLBACK ;
1660 	  mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
1661 	  mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
1662 	  mydebug('x_msg_count/Data = ' || x_msg_count || '/'|| x_msg_data);
1663 
1664 END task_merge_split;
1665 
1666 
1667 PROCEDURE  proc_insert_update_task
1668   (p_action                    IN            VARCHAR2
1669    ,p_insert                    IN            VARCHAR2
1670    ,p_update                    IN            VARCHAR2
1671    ,p_temp_id                   IN            NUMBER
1672    ,p_new_temp_id               IN            NUMBER
1673    ,p_merge_temp_id             IN            NUMBER
1674    ,p_task_status               IN            NUMBER
1675    ,p_user_id                   IN            NUMBER
1676    ,x_return_status             OUT NOCOPY    VARCHAR2
1677    ,x_msg_count                 OUT NOCOPY    NUMBER
1678    ,x_msg_data                  OUT NOCOPY    VARCHAR2)
1679   IS
1680 	 l_proc_name               VARCHAR2(30) :=  'proc_insert_update_task';
1681 	 l_progress                VARCHAR2(30) :=  '100';
1682 	 l_transaction_temp_id     NUMBER := NULL;
1683 BEGIN
1684    mydebug(l_proc_name || ': Before Insert into WMSDT');
1685 
1686    x_return_status  := l_g_ret_sts_success;
1687 
1688    mydebug ('p_action         = ' || p_action);
1689    mydebug ('p_insert         = ' || p_insert);
1690    mydebug ('p_update         = ' || p_update);
1691    mydebug ('p_temp_id        = ' || p_temp_id);
1692    mydebug ('p_new_temp_id    = ' || p_new_temp_id);
1693    mydebug ('p_merge_temp_id  = ' || p_merge_temp_id);
1694    mydebug ('p_task_status    = ' || p_task_status);
1695 
1696    IF p_insert = 'Y' THEN
1697 	   INSERT INTO wms_dispatched_tasks
1698 						  (task_id
1699 						  ,transaction_temp_id
1700 						  ,organization_id
1701 						  ,user_task_type
1702 						  ,person_id
1703 						  ,effective_start_date
1704 						  ,effective_end_date
1705 						  ,equipment_id
1706 						  ,equipment_instance
1707 						  ,person_resource_id
1708 						  ,machine_resource_id
1709 						  ,status
1710 						  ,dispatched_time
1711 						  ,loaded_time
1712 						  ,drop_off_time
1713 						  ,last_update_date
1714 						  ,last_updated_by
1715 						  ,creation_date
1716 						  ,created_by
1717 						  ,last_update_login
1718 						  ,attribute_category
1719 						  ,attribute1
1720 						  ,attribute2
1721 						  ,attribute3
1722 						  ,attribute4
1723 						  ,attribute5
1724 						  ,attribute6
1725 						  ,attribute7
1726 						  ,attribute8
1727 						  ,attribute9
1728 						  ,attribute10
1729 						  ,attribute11
1730 						  ,attribute12
1731 						  ,attribute13
1732 						  ,attribute14
1733 						  ,attribute15
1734 						  ,task_type
1735 						  ,priority
1736 						  ,task_group_id
1737 						  ,device_id
1738 						  ,device_inVoked
1739 						  ,device_request_id
1740 						  ,suggested_dest_subinventory
1741 						  ,suggested_dest_locator_id
1742 						  ,operation_plan_id
1743 						  ,move_order_line_id
1744 						  ,transfer_lpn_id )
1745 		  (SELECT wms_dispatched_tasks_s.NEXTVAL
1746 						  ,p_new_temp_id             -- parameter
1747 						  ,organization_id
1748 						  ,user_task_type
1749 						  ,person_id
1750 						  ,effective_start_date
1751 						  ,effective_end_date
1752 						  ,equipment_id
1753 						  ,equipment_instance
1754 						  ,person_resource_id
1755 						  ,machine_resource_id
1756 						  ,p_task_status                -- parameter
1757 						  ,dispatched_time
1758 						  ,SYSDATE
1759 						  ,drop_off_time
1760 						  ,SYSDATE
1761 						  ,last_updated_by
1762 						  ,SYSDATE
1763 						  ,p_user_id         -- parameter
1764 						  ,last_update_login
1765 						  ,attribute_category
1766 						  ,attribute1
1767 						  ,attribute2
1768 						  ,attribute3
1769 						  ,attribute4
1770 						  ,attribute5
1771 						  ,attribute6
1772 						  ,attribute7
1773 						  ,attribute8
1774 						  ,attribute9
1775 						  ,attribute10
1776 						  ,attribute11
1777 						  ,attribute12
1778 						  ,attribute13
1779 						  ,attribute14
1780 						  ,attribute15
1781 						  ,task_type
1782 						  ,priority
1783 						  ,task_group_id
1784 						  ,device_id
1785 						  ,device_invoked
1786 						  ,device_request_id
1787 						  ,suggested_dest_subinventory
1788 						  ,suggested_dest_locator_id
1789 						  ,operation_plan_id
1790 						  ,move_order_line_id
1791 						  ,transfer_lpn_id
1792 			FROM   wms_dispatched_tasks
1793 			WHERE transaction_temp_id = p_temp_id);
1794 	  IF SQL%NOTFOUND THEN
1795 		 myDebug('Error inserting a new task using WDT record for : '|| p_temp_id);
1796 		 fnd_message.set_name('WMS', 'WMS_INTERNAL_ERROR'); --NEWMSG
1797 		 -- Internal Error $ROUTINE
1798 		 fnd_message.set_token('ROUTINE', '- proc_insert_update_task' );
1799 		 fnd_msg_pub.ADD;
1800 		 RAISE fnd_api.G_EXC_ERROR;
1801 	  END IF;
1802    ELSE
1803 	  l_progress := '200';
1804 	  mydebug ('l_progress: ' || l_progress );
1805 	  IF p_update = 'Y1' THEN
1806 		 l_transaction_temp_id := p_temp_id; -- update only the original task
1807 	  ELSE
1808 		 l_transaction_temp_id := p_merge_temp_id; -- update the merged task
1809 	  END IF;
1810 	  l_progress := '250';
1811 	  mydebug('l_progress: ' || l_progress );
1812 	  mydebug('l_transaction_temp_id : ' || l_transaction_temp_id);
1813 	  UPDATE  wms_dispatched_tasks
1814 	  SET     status =  p_task_status
1815 			  ,loaded_time = SYSDATE
1816 			  ,last_update_date = SYSDATE
1817 			  ,last_updated_by   = p_user_id
1818 	  WHERE transaction_temp_id = l_transaction_temp_id;
1819 	  IF SQL%NOTFOUND THEN
1820 			mydebug('l_progress : ' || l_progress);
1821 		 myDebug('Error updating task for : '|| l_transaction_temp_id);
1822 		 fnd_message.set_name('WMS', 'WMS_INTERNAL_ERROR'); --NEWMSG
1823 		 -- Internal Error $ROUTINE
1824 		 fnd_message.set_token('ROUTINE', '- proc_insert_update_task' );
1825 		 fnd_msg_pub.ADD;
1826 		 RAISE fnd_api.G_EXC_ERROR;
1827 	  END IF;
1828 	  IF p_update = 'Y2' AND p_action = l_g_action_load_multiple THEN
1829 		 fnd_message.set_token('ROUTINE', '- proc_insert_update_task' );
1830 		 fnd_msg_pub.ADD;
1831 		 l_progress := '300';
1832 		 mydebug('l_progress: ' || l_progress );
1833 		 -- delete the original one with p_transaction_temp_id
1834 		 DELETE  wms_dispatched_tasks
1835 		  WHERE  transaction_temp_id = p_temp_id;
1836 		 IF SQL%NOTFOUND THEN
1837 			myDebug('Error deleting task for : '|| l_transaction_temp_id);
1838 			fnd_message.set_name('WMS', 'WMS_INTERNAL_ERROR'); --NEWMSG
1839 			-- Internal Error $ROUTINE
1840 			fnd_message.set_token('ROUTINE', '- proc_insert_update_task' );
1841 			fnd_msg_pub.ADD;
1842 			RAISE fnd_api.G_EXC_ERROR;
1843 		 END IF;
1844 	  END IF;
1845 
1846    END IF;
1847 
1848    mydebug('End .. ' || l_proc_name);
1849 EXCEPTION
1850    WHEN fnd_api.g_exc_error THEN
1851 		x_return_status  := l_g_ret_sts_error;
1852 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
1853 		mydebug('ROLLBACK ' );
1854 		ROLLBACK ;
1855 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
1856 		mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
1857    WHEN fnd_api.g_exc_unexpected_error THEN
1858 		x_return_status  := fnd_api.g_ret_sts_unexp_error;
1859 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
1860 		mydebug('ROLLBACK ' );
1861 		ROLLBACK ;
1862 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
1863 		mydebug('RAISE fnd_api.g_exc_unexpected_error: ' || SQLERRM);
1864 		mydebug('x_msg_count/Data = ' || x_msg_count || '/'|| x_msg_data);
1865    WHEN OTHERS THEN
1866 		x_return_status  := l_g_ret_sts_unexp_error;
1867 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
1868 		mydebug('ROLLBACK ' );
1869 		ROLLBACK ;
1870 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
1871 		mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
1872 
1873 END proc_insert_update_task;
1874 
1875 --BUG12622871LSC Procedure to convert qty to suggested UoM
1876 PROCEDURE proc_convert_qty_to_sugg_uom
1877 				(p_inventory_item_id            IN          NUMBER
1878 				,p_organization_id              IN          NUMBER
1879 				,p_lpn_match_lpn_id             IN          NUMBER
1880 				,p_confirmed_trx_qty            IN          NUMBER
1881 				,p_lot_controlled               IN          VARCHAR2
1882 				,p_confirmed_lots               IN          VARCHAR2
1883 				,p_confirmed_lot_trx_qty        IN          VARCHAR2
1884 				,p_suggested_uom                IN          VARCHAR2
1885 				,p_confirmed_uom                IN          VARCHAR2
1886 				,p_primary_uom                  IN          VARCHAR2
1887 				,p_confirmed_sec_uom            IN          VARCHAR2
1888 				,p_confirmed_sec_qty            IN          VARCHAR2
1889 				,p_fulfillment_base             IN          VARCHAR2  DEFAULT 'P' --muom:sk
1890 				,p_confirmed_sub                IN          VARCHAR2
1891 				,p_confirmed_locator_id         IN          NUMBER
1892 				,p_revision                     IN          VARCHAR2
1893 				,x_confirmed_prim_qty           OUT NOCOPY  NUMBER
1894 				,x_confirmed_sec_qty            OUT NOCOPY  NUMBER                --muom:sk
1895 				,x_confirmed_sugg_qty           OUT NOCOPY  NUMBER
1896 				,x_confirmed_pri_qty_string     OUT NOCOPY  VARCHAR2
1897 				,x_qoh                          OUT NOCOPY  NUMBER
1898 				,x_sqoh                         OUT NOCOPY  NUMBER
1899 				,x_att                          OUT NOCOPY  NUMBER
1900 				,x_satt                         OUT NOCOPY  NUMBER
1901 				,x_return_status                OUT NOCOPY  VARCHAR2
1902 				,x_msg_count                    OUT NOCOPY  NUMBER
1903 				,x_msg_data                     OUT NOCOPY  VARCHAR2
1904 				) IS --muom:sk
1905 
1906    l_proc_name                   VARCHAR2(30)   :=  'PROC_CONVERT_QTY_TO_SUGG_UOM';
1907    l_progress                    VARCHAR2(30)   :=  '100';
1908    l_delimiter                   VARCHAR2(30)   :=  ':';
1909    l_group_delimiter             VARCHAR2(30)   :=  ';';
1910    l_lot_number                  VARCHAR2(80)   :=  NULL;
1911    l_lot_trx_qty                 NUMBER         :=  NULL;
1912    l_lot_prim_qty                NUMBER         :=  NULL;
1913    l_lot_sec_qty                 NUMBER         :=  NULL; -- muom:sk
1914    l_confirmed_sec_qty           NUMBER         :=  0;    -- muom:sk
1915    l_lot_sugg_qty                NUMBER         :=  NULL;
1916    l_confirmed_prim_qty          NUMBER         := 0;
1917    l_confirmed_sugg_qty          NUMBER         := 0;
1918    l_sec_qty                     NUMBER         :=  NULL;
1919    l_group_number                NUMBER         :=  0;
1920    l_fulfillment_base            VARCHAR2(1)    := p_fulfillment_base; -- muom:sk
1921 
1922    -- To parse lots
1923    m                             NUMBER := 1;  -- position of delimiter
1924    n                             NUMBER := 1;  -- Start position for substr or search for delimiter
1925    i                             NUMBER := 1;  -- position of delimiter
1926    j                             NUMBER := 1;  -- Start position for substr or search for delimiter
1927    -- To parse Serials
1928    x                             NUMBER := 1;  -- position of delimiter
1929    y                             NUMBER := 1;  -- Start position for substr or search for delimiter
1930    s                             NUMBER := 1;  -- position of delimiter
1931    t                             NUMBER := 1;  -- Start position for substr or search for delimiter
1932    -- To parse Secondary qty
1933    k                             NUMBER := 1;  -- position of delimiter
1934    l                             NUMBER := 1;  -- Start position for substr or search for delimiter
1935    l_number_format_mask          VARCHAR2(30) :=   'FM9999999999.99999999999999'; --Bug#6274290
1936 
1937    l_qoh                         NUMBER;
1938    l_rqoh                        NUMBER;
1939    l_qr                          NUMBER;
1940    l_qs                          NUMBER;
1941    l_att                         NUMBER;
1942    l_atr                         NUMBER;
1943    l_sqoh                        NUMBER;
1944    l_srqoh                       NUMBER;
1945    l_sqr                         NUMBER;
1946    l_sqs                         NUMBER;
1947    l_satt                        NUMBER;
1948    l_satr                        NUMBER;
1949    l_is_revision_control         VARCHAR2(30);
1950    b_is_revision_control         BOOLEAN;
1951 
1952    l_msg_cnt                     NUMBER;
1953    l_msg_data                    VARCHAR2(2000);
1954    l_return_status               VARCHAR2(240);
1955    l_confirmed_pri_qty_string    VARCHAR2(2000);
1956 
1957 BEGIN
1958    x_return_status  := l_g_ret_sts_success;
1959 	mydebug ('In  :' || l_proc_name );
1960 	mydebug ('p_fulfillment_base      :' || p_fulfillment_base );
1961 	mydebug ('p_organization_id       :' || p_organization_id );
1962 	mydebug ('p_inventory_item_id     :' || p_inventory_item_id );
1963 	mydebug ('p_lpn_match_lpn_id      :' || p_lpn_match_lpn_id );
1964 	mydebug ('p_confirmed_trx_qty     :' || p_confirmed_trx_qty );
1965 	mydebug ('p_lot_controlled        :' || p_lot_controlled );
1966 	mydebug ('p_confirmed_lots        :' || p_confirmed_lots );
1967 	mydebug ('p_confirmed_lot_trx_qty :' || p_confirmed_lot_trx_qty );
1968 	mydebug ('p_confirmed_uom         :' || p_confirmed_uom );
1969 	mydebug ('p_suggested_uom         :' || p_suggested_uom );
1970 	mydebug ('p_primary_uom           :' || p_primary_uom );
1971 	mydebug ('p_confirmed_sec_uom     :' || p_confirmed_sec_uom );
1972 	mydebug ('p_confirmed_sec_qty     :' || p_confirmed_sec_qty );
1973 	mydebug ('p_confirmed_sub         :' || p_confirmed_sub );
1974 	mydebug ('p_confirmed_locator_id  :' || p_confirmed_locator_id );
1975 	mydebug ('p_revision              :' || p_revision );
1976 
1977 	IF p_revision IS NULL THEN
1978 		l_is_revision_control  := 'FALSE';
1979 		b_is_revision_control  := FALSE;
1980 	ELSE
1981 		l_is_revision_control  := 'TRUE';
1982 		b_is_revision_control  := TRUE;
1983 	END IF;
1984 
1985 	x_confirmed_sec_qty :=0;
1986 
1987 IF(p_lot_controlled = 'Y') THEN
1988    WHILE  (j <> 0 AND n <> 0)
1989    LOOP
1990    -- Parse P_confirmed_lots and  p_confirmed_lot_trx_qty
1991 		  -- N is the delimiter position,
1992 		  -- M is the position from which to start looking for the first delimiter
1993 		  -- for string 'L001:L002:L003' M=1, N=5 for the first search .
1994 		  --             M=5+1=6, N=10 for the next search .
1995 		  --             M=10+1=11, N=0 for the next search because this is the last part of string.
1996 
1997 
1998 		  l_progress    :=  '110';
1999 		  n := INSTR(p_confirmed_lots,l_delimiter,m,1);
2000 		  j := INSTR(p_confirmed_lot_trx_qty,l_delimiter,i,1);
2001 		  l_group_number := l_group_number + 1;
2002 
2003 		  mydebug ('m:' || m||':n:' || n || ':i:' || i||':j:'||j );
2004 		  IF n = 0 THEN -- Last part OF the string
2005 			 l_lot_number :=  substr(p_confirmed_lots,m,length(p_confirmed_lots));
2006 		  ELSE
2007 			 l_lot_number :=  substr(p_confirmed_lots,m,n-m) ;-- start at M get m-n chrs.
2008 			 m := n+1;
2009 		  END IF;
2010 		  mydebug ('l_lot_number:' || l_lot_number);
2011 		  -- Parse  p_confirmed_lot_trx_qty
2012 		  IF j = 0 THEN -- Last part OF the string
2013 			 l_lot_trx_qty :=  to_number(substr(p_confirmed_lot_trx_qty,i,length(p_confirmed_lot_trx_qty)) ,l_number_format_mask ) ; --Bug#6274290.
2014 		  ELSE
2015 			 l_lot_trx_qty :=  to_number(substr(p_confirmed_lot_trx_qty,i,j-i), l_number_format_mask ) ;-- start at i till i-j position
2016 			 i := j+1;
2017 		  END IF;
2018 
2019 		  mydebug ('m:' || m||':n:' || n || ':i:' || i||':j:'||j );
2020 		  mydebug ('l_lot_trx_qty:' || l_lot_trx_qty);
2021 
2022 		  IF l_lot_number IS NOT NULL THEN  --muom:sk
2023 			IF l_fulfillment_base = 'S' THEN --{
2024 
2025 					IF(p_lpn_match_lpn_id IS NOT NULL) THEN
2026 						inv_quantity_tree_pub.query_quantities(
2027 						p_api_version_number           => 1.0
2028 						, p_init_msg_lst               => fnd_api.g_false
2029 						, x_return_status              => l_return_status
2030 						, x_msg_count                  => l_msg_cnt
2031 						, x_msg_data                   => l_msg_data
2032 						, p_organization_id            => p_organization_id
2033 						, p_inventory_item_id          => p_inventory_item_id
2034 						, p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
2035 						, p_is_revision_control        => b_is_revision_control
2036 						, p_is_lot_control             => TRUE
2037 						, p_is_serial_control          => FALSE -- Dual UOM Items cannot be serial controlled and if fulfill base is 'S' item is dual UOM controlled always
2038 						, p_demand_source_type_id      => -9999
2039 						, p_revision                   => NVL(p_revision, NULL)
2040 						, p_lot_number                 => l_lot_number
2041 						, p_subinventory_code          => p_confirmed_sub
2042 						, p_locator_id                 => p_confirmed_locator_id
2043 						, x_qoh                        => l_qoh
2044 						, x_rqoh                       => l_rqoh
2045 						, x_qr                         => l_qr
2046 						, x_qs                         => l_qs
2047 						, x_att                        => l_att
2048 						, x_atr                        => l_atr
2049 						, x_sqoh                       => l_sqoh
2050 						, x_srqoh                      => l_srqoh
2051 						, x_sqr                        => l_sqr
2052 						, x_sqs                        => l_sqs
2053 						, x_satt                       => l_satt
2054 						, x_satr                       => l_satr
2055 						, p_lpn_id                     => p_lpn_match_lpn_id
2056 						, p_grade_code                 => NULL
2057 						);
2058 					ELSE
2059 						inv_quantity_tree_pub.query_quantities(
2060 						p_api_version_number           => 1.0
2061 						, p_init_msg_lst               => fnd_api.g_false
2062 						, x_return_status              => l_return_status
2063 						, x_msg_count                  => l_msg_cnt
2064 						, x_msg_data                   => l_msg_data
2065 						, p_organization_id            => p_organization_id
2066 						, p_inventory_item_id          => p_inventory_item_id
2067 						, p_tree_mode                  => inv_quantity_tree_pub.g_loose_only_mode
2068 						, p_is_revision_control        => b_is_revision_control
2069 						, p_is_lot_control             => TRUE
2070 						, p_is_serial_control          => FALSE -- Dual UOM Items cannot be serial controlled and if fulfill base is 'S' item is dual UOM controlled always
2071 						, p_demand_source_type_id      => -9999
2072 						, p_revision                   => NVL(p_revision, NULL)
2073 						, p_lot_number                 => l_lot_number
2074 						, p_subinventory_code          => p_confirmed_sub
2075 						, p_locator_id                 => p_confirmed_locator_id
2076 						, x_qoh                        => l_qoh
2077 						, x_rqoh                       => l_rqoh
2078 						, x_qr                         => l_qr
2079 						, x_qs                         => l_qs
2080 						, x_att                        => l_att
2081 						, x_atr                        => l_atr
2082 						, x_sqoh                       => l_sqoh
2083 						, x_srqoh                      => l_srqoh
2084 						, x_sqr                        => l_sqr
2085 						, x_sqs                        => l_sqs
2086 						, x_satt                       => l_satt
2087 						, x_satr                       => l_satr
2088 						, p_lpn_id                     => NULL
2089 						, p_grade_code                 => NULL
2090 						);
2091 					END IF;
2092 
2093 				IF (l_return_status = fnd_api.g_ret_sts_success) THEN --{
2094 
2095 					mydebug ('Quantities in MOQD for inventory_item_id ' || p_inventory_item_id || ' are:');
2096 					mydebug ('Primary Qty :' || l_qoh || ' Secondary Qty :' || l_sqoh || ' Primary ATT :' || l_att || ' Secondary ATT :' || l_satt);
2097 
2098 					IF p_confirmed_sec_uom <> p_confirmed_uom THEN
2099 						l_progress    :=  '120';
2100 						l_lot_sec_qty := inv_convert.inv_um_convert
2101 										(item_id          => p_inventory_item_id
2102 										,lot_number		  => l_lot_number
2103 										,organization_id  => p_organization_id
2104 										,precision        => l_g_decimal_precision
2105 										,from_quantity    => l_lot_trx_qty
2106 										,from_unit        => p_confirmed_uom
2107 										,to_unit          => p_confirmed_sec_uom
2108 										,from_name        => NULL
2109 										,to_name          => NULL);
2110 						mydebug ('Inside the if portion using p_confirmed_uom '||p_confirmed_uom||', value of l_lot_sec_qty is ' || l_lot_sec_qty);
2111 					ELSE
2112 						l_progress    := '130';
2113 						l_lot_sec_qty := l_lot_trx_qty;
2114 					END IF;
2115 
2116 					IF(l_lot_sec_qty <= 0) THEN --Can happen as -9999 if UOM setup is improper and conversion is not found
2117 						mydebug ('The calculated l_lot_sec_qty came to be '||l_lot_sec_qty||', which should not happen so restting it to l_lot_trx_qty ' || l_lot_trx_qty);
2118 						l_lot_sec_qty := l_lot_trx_qty;
2119 					END IF;
2120 
2121 					--For secondary based fulfillment if entire secondary onhand in LPN is consumed then, consume entire primary from the onhand.
2122 					--Else for picking from loose or picking from LPN but entire onhand is not consumed then use UOM conversion. Ct Wt entry would correct the primary qty.
2123 					--When calculating based on UOM conversion if Lot Specific conversion exists then that will be used else the standard conversion would get used.
2124 					IF(l_sqoh = l_lot_sec_qty) THEN
2125 						l_lot_prim_qty := l_qoh;
2126 						mydebug ('For fulfillment_base S for lot :'||l_lot_number||' entire secondary is consumed so consuming entire primary qty ' || l_lot_prim_qty);
2127 					ELSE
2128 						l_lot_prim_qty := ROUND(((l_qoh * l_lot_sec_qty)/l_sqoh) , l_g_decimal_precision);
2129 						mydebug ('For fulfillment_base S entire secondary is not consumed so consuming primary qty ' || l_lot_prim_qty || ' derived via calculation');
2130 					END IF;
2131 
2132 					x_qoh   := l_qoh;
2133 					x_sqoh  := l_sqoh;
2134 					x_att   := l_att;
2135 					x_satt  := l_satt;
2136 
2137 					IF(l_confirmed_pri_qty_string IS NOT NULL) THEN
2138 						l_confirmed_pri_qty_string := CONCAT(CONCAT(l_confirmed_pri_qty_string, l_delimiter),TO_CHAR(l_lot_prim_qty));
2139 					ELSE
2140 						l_confirmed_pri_qty_string := TO_CHAR(l_lot_prim_qty);
2141 					END IF;
2142 
2143 					IF(l_lot_number IS NOT NULL) THEN
2144 					l_confirmed_sec_qty  := l_confirmed_sec_qty + NVL(l_lot_sec_qty,0);
2145 					l_confirmed_prim_qty := l_confirmed_prim_qty + NVL(l_lot_prim_qty,0);
2146 					l_progress    :=  '140';
2147 					END IF;
2148 					mydebug ('l_confirmed_sec_qty after adding qty for above lot is :' || l_confirmed_sec_qty);
2149 					mydebug ('l_confirmed_prim_qty after adding qty for above lot is:' || l_confirmed_prim_qty);
2150 
2151 				ELSE --Quantity tree errored out
2152 					fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
2153 					fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
2154 					fnd_msg_pub.ADD;
2155 					RAISE fnd_api.g_exc_error;
2156 				END IF;--}
2157 			ELSE --fulfillment base is 'P'
2158 
2159 				IF p_primary_uom <> p_confirmed_uom THEN
2160 					l_progress    :=  '120';
2161 					l_lot_prim_qty := inv_convert.inv_um_convert
2162 								   (item_id          => p_inventory_item_id
2163 								   ,lot_number		 => l_lot_number
2164 								   ,organization_id  => p_organization_id
2165 								   ,precision        => l_g_decimal_precision
2166 								   ,from_quantity    => l_lot_trx_qty
2167 								   ,from_unit        => p_confirmed_uom
2168 								   ,to_unit          => p_primary_uom
2169 								   ,from_name        => NULL
2170 								   ,to_name          => NULL);
2171 					mydebug ('Inside the if portion using p_confirmed_uom '||p_confirmed_uom||', value of l_lot_prim_qty is ' || l_lot_prim_qty);
2172 				ELSE
2173 					l_progress    :=  '130';
2174 					l_lot_prim_qty := l_lot_trx_qty;
2175 				END IF;
2176 				mydebug ('Value of l_lot_prim_qty for lot :'||l_lot_number||' is ' || l_lot_prim_qty);
2177 				IF(l_lot_number IS NOT NULL) THEN
2178 					l_confirmed_prim_qty := l_confirmed_prim_qty + NVL(l_lot_prim_qty,0);
2179 					l_progress    :=  '140';
2180 				END IF;
2181 				mydebug ('l_confirmed_prim_qty after adding qty for above lot is :' || l_confirmed_prim_qty);
2182 				x_qoh   := NULL; --For P based fulfillment no call to qty tree and these are not required for MMTT manipulation
2183 				x_sqoh  := NULL;
2184 				x_att   := NULL;
2185 				x_satt  := NULL;
2186 			END IF; --}
2187 
2188 
2189 			IF p_suggested_uom <> p_confirmed_uom THEN
2190 				l_progress    :=  '120';
2191 				l_lot_sugg_qty := inv_convert.inv_um_convert
2192 							   (item_id          => p_inventory_item_id
2193 							   ,lot_number		 => l_lot_number
2194 							   ,organization_id  => p_organization_id
2195 							   ,precision        => l_g_decimal_precision
2196 							   ,from_quantity    => l_lot_trx_qty
2197 							   ,from_unit        => p_confirmed_uom
2198 							   ,to_unit          => p_suggested_uom
2199 							   ,from_name        => NULL
2200 							   ,to_name          => NULL);
2201 			ELSE
2202 				l_progress    :=  '130';
2203 				l_lot_sugg_qty := l_lot_trx_qty;
2204 			END IF;
2205 
2206 			mydebug ('Value of l_lot_sugg_qty for lot :'||l_lot_number||' is ' || l_lot_sugg_qty);
2207 		  END IF; --muom:sk
2208 
2209 			IF(l_lot_number IS NOT NULL) THEN
2210 				l_confirmed_sugg_qty := l_confirmed_sugg_qty + NVL(l_lot_sugg_qty,0);
2211 				l_progress    :=  '140';
2212 				mydebug ('l_confirmed_sugg_qty after adding qty for above lot is:' || l_confirmed_sugg_qty);
2213 			END IF;
2214    END LOOP;
2215 
2216    IF l_fulfillment_base = 'S' THEN
2217 	x_confirmed_sec_qty  := l_confirmed_sec_qty;
2218 	x_confirmed_pri_qty_string := l_confirmed_pri_qty_string;
2219    END IF;
2220    x_confirmed_prim_qty := l_confirmed_prim_qty;
2221    x_confirmed_sugg_qty := l_confirmed_sugg_qty;
2222 
2223 
2224    mydebug ('For lot controlled item x_confirmed_pri_qty_string is  :' || x_confirmed_pri_qty_string);
2225    mydebug ('For lot controlled item x_confirmed_prim_qty is set as :' || x_confirmed_prim_qty);
2226    mydebug ('For lot controlled item x_confirmed_sec_qty  is set as :' || x_confirmed_sec_qty);
2227    mydebug ('For lot controlled item x_confirmed_sugg_qty is set as :' || x_confirmed_sugg_qty);
2228    mydebug ('For lot controlled item x_qoh                is set as :' || x_qoh);
2229    mydebug ('For lot controlled item x_sqoh               is set as :' || x_sqoh);
2230    mydebug ('For lot controlled item x_att                is set as :' || x_att);
2231    mydebug ('For lot controlled item x_satt               is set as :' || x_satt);
2232 
2233 ELSE --Non lot controlled item
2234 	IF l_fulfillment_base = 'S' THEN --{
2235 
2236 		IF(p_lpn_match_lpn_id IS NOT NULL) THEN
2237 			inv_quantity_tree_pub.query_quantities(
2238 			p_api_version_number           => 1.0
2239 			, p_init_msg_lst               => fnd_api.g_false
2240 			, x_return_status              => l_return_status
2241 			, x_msg_count                  => l_msg_cnt
2242 			, x_msg_data                   => l_msg_data
2243 			, p_organization_id            => p_organization_id
2244 			, p_inventory_item_id          => p_inventory_item_id
2245 			, p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
2246 			, p_is_revision_control        => b_is_revision_control
2247 			, p_is_lot_control             => FALSE
2248 			, p_is_serial_control          => FALSE -- Dual UOM Items cannot be serial controlled and if fulfill base is 'S' item is dual UOM controlled always
2249 			, p_demand_source_type_id      => -9999
2250 			, p_revision                   => NVL(p_revision, NULL)
2251 			, p_lot_number                 => NULL
2252 			, p_subinventory_code          => p_confirmed_sub
2253 			, p_locator_id                 => p_confirmed_locator_id
2254 			, x_qoh                        => l_qoh
2255 			, x_rqoh                       => l_rqoh
2256 			, x_qr                         => l_qr
2257 			, x_qs                         => l_qs
2258 			, x_att                        => l_att
2259 			, x_atr                        => l_atr
2260 			, x_sqoh                       => l_sqoh
2261 			, x_srqoh                      => l_srqoh
2262 			, x_sqr                        => l_sqr
2263 			, x_sqs                        => l_sqs
2264 			, x_satt                       => l_satt
2265 			, x_satr                       => l_satr
2266 			, p_lpn_id                     => p_lpn_match_lpn_id
2267 			, p_grade_code                 => NULL
2268 			);
2269 		ELSE
2270 			inv_quantity_tree_pub.query_quantities(
2271 			p_api_version_number           => 1.0
2272 			, p_init_msg_lst               => fnd_api.g_false
2273 			, x_return_status              => l_return_status
2274 			, x_msg_count                  => l_msg_cnt
2275 			, x_msg_data                   => l_msg_data
2276 			, p_organization_id            => p_organization_id
2277 			, p_inventory_item_id          => p_inventory_item_id
2278 			, p_tree_mode                  => inv_quantity_tree_pub.g_loose_only_mode
2279 			, p_is_revision_control        => b_is_revision_control
2280 			, p_is_lot_control             => FALSE
2281 			, p_is_serial_control          => FALSE -- Dual UOM Items cannot be serial controlled and if fulfill base is 'S' item is dual UOM controlled always
2282 			, p_demand_source_type_id      => -9999
2283 			, p_revision                   => NVL(p_revision, NULL)
2284 			, p_lot_number                 => NULL
2285 			, p_subinventory_code          => p_confirmed_sub
2286 			, p_locator_id                 => p_confirmed_locator_id
2287 			, x_qoh                        => l_qoh
2288 			, x_rqoh                       => l_rqoh
2289 			, x_qr                         => l_qr
2290 			, x_qs                         => l_qs
2291 			, x_att                        => l_att
2292 			, x_atr                        => l_atr
2293 			, x_sqoh                       => l_sqoh
2294 			, x_srqoh                      => l_srqoh
2295 			, x_sqr                        => l_sqr
2296 			, x_sqs                        => l_sqs
2297 			, x_satt                       => l_satt
2298 			, x_satr                       => l_satr
2299 			, p_lpn_id                     => NULL
2300 			, p_grade_code                 => NULL
2301 			);
2302 		END IF;
2303 
2304 		IF (l_return_status = fnd_api.g_ret_sts_success) THEN --{
2305 
2306 			mydebug ('Quantities in MOQD for inventory_item_id ' || p_inventory_item_id || ' are:');
2307 			mydebug ('Primary Qty :' || l_qoh || ' Secondary Qty :' || l_sqoh || ' Primary ATT :' || l_att || ' Secondary ATT :' || l_satt);
2308 
2309 			IF p_confirmed_sec_uom <> p_confirmed_uom THEN
2310 				x_confirmed_sec_qty := inv_convert.inv_um_convert(
2311 									item_id       => p_inventory_item_id,
2312 									precision     => l_g_decimal_precision,
2313 									from_quantity => p_confirmed_trx_qty,
2314 									from_unit     => p_confirmed_uom,
2315 									to_unit       => p_confirmed_sec_uom,
2316 									from_name     => NULL,
2317 									to_name       => NULL);
2318 			ELSE
2319 				x_confirmed_sec_qty := p_confirmed_trx_qty;
2320 			END IF;
2321 
2322 				--For secondary based fulfillment if entire secondary onhand in LPN is consumed then, consume entire primary from the onhand.
2323 				--Else for picking from loose or picking from LPN but entire onhand is not consumed then use UOM conversion. Ct Wt entry would correct the primary qty.
2324 				IF(l_sqoh = x_confirmed_sec_qty) THEN
2325 					x_confirmed_prim_qty := l_qoh;
2326 					mydebug ('For fulfillment_base S entire secondary is consumed so consuming entire primary qty ' || x_confirmed_prim_qty);
2327 				ELSE
2328 					x_confirmed_prim_qty := ROUND(((l_qoh * x_confirmed_sec_qty)/l_sqoh), l_g_decimal_precision);
2329 					mydebug ('For fulfillment_base S entire secondary is not consumed so consuming primary qty ' || x_confirmed_prim_qty || ' derived via calculation');
2330 				END IF;
2331 				x_qoh   := l_qoh;
2332 				x_sqoh  := l_sqoh;
2333 				x_att   := l_att;
2334 				x_satt  := l_satt;
2335 
2336 		ELSE --Quantity tree errored out
2337 			fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
2338 			fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
2339 			fnd_msg_pub.ADD;
2340 			RAISE fnd_api.g_exc_error;
2341 		END IF;--}
2342 	ELSE --fulfillment base is 'P'
2343 		IF p_primary_uom <> p_confirmed_uom THEN
2344 			x_confirmed_prim_qty := inv_convert.inv_um_convert
2345 							   (item_id          => p_inventory_item_id
2346 							   ,precision        => l_g_decimal_precision
2347 							   ,from_quantity    => p_confirmed_trx_qty
2348 							   ,from_unit        => p_confirmed_uom
2349 							   ,to_unit          => p_primary_uom
2350 							   ,from_name        => NULL
2351 							   ,to_name          => NULL);
2352 		ELSE
2353 			x_confirmed_prim_qty := p_confirmed_trx_qty;
2354 		END IF;
2355 			x_qoh   := NULL; --For P based fulfillment no call to qty tree and these are not required for MMTT manipulation
2356 			x_sqoh  := NULL;
2357 			x_att   := NULL;
2358 			x_satt  := NULL;
2359 	END IF; --}
2360 	x_confirmed_pri_qty_string := NULL; --For non lot controlled items we do not need to track pri qty separately. It is already stored in x_confirmed_prim_qty
2361 
2362 		IF p_suggested_uom <> p_confirmed_uom THEN
2363 			x_confirmed_sugg_qty := inv_convert.inv_um_convert
2364 							   (item_id          => p_inventory_item_id
2365 							   ,precision        => l_g_decimal_precision
2366 							   ,from_quantity    => p_confirmed_trx_qty
2367 							   ,from_unit        => p_confirmed_uom
2368 							   ,to_unit          => p_suggested_uom
2369 							   ,from_name        => NULL
2370 							   ,to_name          => NULL);
2371 		ELSE
2372 			x_confirmed_sugg_qty := p_confirmed_trx_qty;
2373 		END IF;
2374 		mydebug ('For non lot controlled item x_confirmed_prim_qty is set as ' || x_confirmed_prim_qty);
2375 		mydebug ('For non lot controlled item x_confirmed_sec_qty  is set as ' || x_confirmed_sec_qty);
2376 		mydebug ('For non lot controlled item x_confirmed_sugg_qty is set as ' || x_confirmed_sugg_qty);
2377 		mydebug ('For non lot controlled item x_qoh                is set as ' || x_qoh);
2378 		mydebug ('For non lot controlled item x_sqoh               is set as ' || x_sqoh);
2379 		mydebug ('For non lot controlled item x_att                is set as ' || x_att);
2380 		mydebug ('For non lot controlled item x_satt               is set as ' || x_satt);
2381 END IF;
2382 
2383    mydebug('END = ' || l_proc_name );
2384 EXCEPTION
2385 	WHEN fnd_api.g_exc_error THEN
2386 		mydebug('Expected Exception raised');
2387 		x_return_status  := fnd_api.g_ret_sts_error;
2388 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2389 		mydebug(' ROLLBACK' );
2390 		ROLLBACK ;
2391 
2392    WHEN OTHERS THEN
2393 		x_return_status  := l_g_ret_sts_unexp_error;
2394 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
2395 		mydebug('ROLLBACK ' );
2396 		ROLLBACK ;
2397 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
2398 		mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
2399 
2400 END proc_convert_qty_to_sugg_uom;
2401 --BUG12622871LSC end of procedure
2402 
2403 PROCEDURE proc_insert_update_mmtt
2404 			  (p_action                               IN            VARCHAR2
2405 			  ,p_insert                               IN            VARCHAR2
2406 			  ,p_update                               IN            VARCHAR2
2407 			  ,p_organization_id                      IN            NUMBER
2408 			  ,p_user_id                              IN            NUMBER
2409 			  ,p_transaction_header_id                IN            NUMBER
2410 			  ,p_transaction_temp_id                  IN            NUMBER
2411 			  ,p_transaction_temp_id_to_merge         IN            NUMBER
2412 			  ,p_lpn_id                               IN            NUMBER
2413 			  ,p_content_lpn_id                       IN            NUMBER
2414 			  ,p_transfer_lpn_id                      IN            NUMBER
2415 			  ,p_confirmed_sub                        IN            VARCHAR2
2416 			  ,p_confirmed_locator_id                 IN            NUMBER
2417 			  ,p_confirmed_uom                        IN            VARCHAR2
2418 			  ,p_suggested_uom                        IN            VARCHAR2
2419 			  ,p_primary_uom                          IN            VARCHAR2
2420 			  ,p_inventory_item_id                    IN            NUMBER
2421 			  ,p_revision                             IN            VARCHAR2
2422 			  ,p_confirmed_trx_qty                    IN            NUMBER
2423 			  ,p_confirmed_lots                       IN            VARCHAR2
2424 			  ,p_confirmed_lot_trx_qty                IN            VARCHAR2
2425 			  ,p_confirmed_sec_uom                    IN            VARCHAR2
2426 			  ,p_confirmed_sec_qty                    IN            VARCHAR2
2427 			  ,p_confirmed_serials                    IN            VARCHAR2
2428 			  ,p_container_item_id                    IN            NUMBER
2429 			  ,p_wms_task_status                      IN            NUMBER
2430 			  ,p_lpn_match                            IN            NUMBER
2431 			  ,p_lpn_match_lpn_id                     IN            NUMBER
2432 			  ,p_serial_allocated_flag                IN            VARCHAR2
2433 			  ,p_lot_controlled                       IN            VARCHAR2  -- Y/N
2434 			  ,p_serial_controlled                    IN            VARCHAR2  -- Y/N
2435 			  ,p_exception                            IN            VARCHAR2 -- SHORT/OVER
2436 			  ,p_parent_lpn_id                        IN            NUMBER
2437 			  ,x_new_transaction_temp_id              OUT NOCOPY    NUMBER
2438 			  ,x_return_status                        OUT NOCOPY    VARCHAR2
2439 			  ,x_msg_count                            OUT NOCOPY    NUMBER
2440 			  ,x_msg_data                             OUT NOCOPY    VARCHAR2
2441 			  ,p_substitute_lots		              IN	        VARCHAR2 --/* Bug 9448490 Lot Substitution Project */
2442 			  ,p_full_lot_allocation                  IN            VARCHAR2 DEFAULT 'Y')
2443 IS
2444    l_proc_name                   VARCHAR2(30) :=  'PROC_INSERT_UPDATE_MMTT';
2445    l_progress                    VARCHAR2(30) :=  '100';
2446    l_new_transaction_temp_id     NUMBER       := NULL;
2447    l_confirmed_prim_qty          NUMBER       := 0;
2448    l_confirmed_sugg_qty          NUMBER       := 0;
2449    l_confirmed_sec_qty           NUMBER       := 0;
2450    l_confirmed_lot_trx_sec_qty   VARCHAR2(100) := NULL;
2451    n                             NUMBER       := 1;
2452    m                             NUMBER       := 1;
2453    l_delimiter                   VARCHAR(30)  := ':';
2454    l_rem_lot_pri_qty             NUMBER;
2455    l_rem_lot_trx_qty             NUMBER;
2456    l_rem_lot_sec_qty             NUMBER;
2457    l_fulfillment_base            VARCHAR2(1);
2458 
2459    l_qoh                         NUMBER;
2460    l_sqoh                        NUMBER;
2461    l_att                         NUMBER;
2462    l_satt                        NUMBER;
2463    l_orig_mmtt_txn_uom           VARCHAR2(3);
2464    l_orig_mmtt_txn_qty           NUMBER;
2465    l_calc_mmtt_txn_qty           NUMBER;
2466    l_orig_mmtt_pri_qty           NUMBER;
2467    l_calc_mmtt_pri_qty           NUMBER;
2468    l_orig_mmtt_sec_qty           NUMBER;
2469    l_confirmed_pri_qty_string    VARCHAR2(2000);
2470 
2471 BEGIN
2472    mydebug('In ..' || l_proc_name );
2473 
2474 	BEGIN
2475 	  SELECT NVL(fulfillment_base, 'P'),
2476 			 transaction_uom,
2477 			 primary_quantity,
2478 			 transaction_quantity,
2479 			 secondary_transaction_quantity
2480 		INTO l_fulfillment_base,
2481 			 l_orig_mmtt_txn_uom,
2482 			 l_orig_mmtt_pri_qty,
2483 			 l_orig_mmtt_txn_qty,
2484 			 l_orig_mmtt_sec_qty
2485 		FROM mtl_material_transactions_temp
2486 	   WHERE transaction_temp_id = p_transaction_temp_id;
2487 	EXCEPTION
2488 	  WHEN OTHERS THEN
2489 		l_fulfillment_base := 'P';
2490 	END;
2491 	mydebug('l_fulfillment_base ..' || l_fulfillment_base);
2492 
2493    x_return_status  := l_g_ret_sts_success;
2494 
2495    mydebug ('p_action                 = ' || p_action                 );
2496    mydebug ('p_insert                 = ' || p_insert                 );
2497    mydebug ('p_update                 = ' || p_update                 );
2498    mydebug ('p_transaction_header_id  = ' || p_transaction_header_id  );
2499    mydebug ('p_transaction_temp_id    = ' || p_transaction_temp_id    );
2500    mydebug ('p_lpn_id                 = ' || p_lpn_id                 );
2501    mydebug ('p_content_lpn_id         = ' || p_content_lpn_id         );
2502    mydebug ('p_parent_lpn_id          = ' || p_parent_lpn_id          );
2503    mydebug ('p_transfer_lpn_id        = ' || p_transfer_lpn_id        );
2504    mydebug ('p_confirmed_sub          = ' || p_confirmed_sub          );
2505    mydebug ('p_confirmed_locator_id   = ' || p_confirmed_locator_id   );
2506    mydebug ('p_confirmed_uom          = ' || p_confirmed_uom          );
2507    mydebug ('p_suggested_uom          = ' || p_suggested_uom          );
2508    mydebug ('p_primary_uom            = ' || p_primary_uom            );
2509    mydebug ('p_inventory_item_id      = ' || p_inventory_item_id      );
2510    mydebug ('p_revision               = ' || p_revision               );
2511    mydebug ('p_confirmed_trx_qty      = ' || p_confirmed_trx_qty      );
2512    mydebug ('p_confirmed_lots         = ' || p_confirmed_lots         );
2513    mydebug ('p_confirmed_lot_trx_qty  = ' || p_confirmed_lot_trx_qty  );
2514    mydebug ('p_confirmed_sec_uom      = ' || p_confirmed_sec_uom      );
2515    mydebug ('p_confirmed_sec_qty      = ' || p_confirmed_sec_qty      );
2516    mydebug ('p_confirmed_serials      = ' || p_confirmed_serials      );
2517    mydebug ('p_container_item_id      = ' || p_container_item_id      );
2518    mydebug ('p_lpn_match              = ' || p_lpn_match              );
2519    mydebug ('p_lpn_match_lpn_id       = ' || p_lpn_match_lpn_id       );
2520    mydebug ('p_lot_controlled         = ' || p_lot_controlled         );
2521    mydebug ('p_exception              = ' || p_exception              );
2522    mydebug ('check for p_substitute_lots as the MTLT in spl case not deleted = ' || p_substitute_lots);
2523    mydebug ('p_full_lot_allocation  = ' || p_full_lot_allocation); -- Added for 14699845 (Flexible Lot Allocation)
2524    mydebug ('l_fulfillment_base     = ' || l_fulfillment_base);
2525    mydebug ('l_orig_mmtt_pri_qty    = ' || l_orig_mmtt_pri_qty);
2526    mydebug ('l_orig_mmtt_txn_qty    = ' || l_orig_mmtt_txn_qty);
2527    mydebug ('l_orig_mmtt_txn_uom    = ' || l_orig_mmtt_txn_uom);
2528    mydebug ('l_orig_mmtt_sec_qty    = ' || l_orig_mmtt_sec_qty);
2529 
2530 
2531 /* p_suggested_uom is the uom in which allocations are created. In allocation MMTT it is the transaction_uom.
2532  * p_confirmed_uom is the uom confirmed on the UI. This may or may not be equal to suggested UOM p_primary_uom is always static.*/
2533 
2534 --BUG12622871LSC moved the code to new procedure and added the call to get qty in sugg uom here
2535 	mydebug('Before the call to proc_convert_qty_to_sugg_uom');
2536 	proc_convert_qty_to_sugg_uom
2537 			(p_inventory_item_id  => p_inventory_item_id
2538 			,p_organization_id	  => p_organization_id
2539 			,p_lpn_match_lpn_id   => p_lpn_match_lpn_id
2540 			,p_confirmed_trx_qty  => p_confirmed_trx_qty
2541 			,p_lot_controlled	  => p_lot_controlled
2542 			,p_confirmed_lots     => p_confirmed_lots
2543 			,p_confirmed_lot_trx_qty  => p_confirmed_lot_trx_qty
2544 			,p_suggested_uom      => p_suggested_uom
2545 			,p_confirmed_uom      => p_confirmed_uom
2546 			,p_primary_uom        => p_primary_uom
2547 			,p_confirmed_sec_uom  => p_confirmed_sec_uom
2548 			,p_confirmed_sec_qty  => p_confirmed_sec_qty
2549 			,p_fulfillment_base   => l_fulfillment_base  -- muom:sk
2550 			,p_confirmed_sub      => p_confirmed_sub
2551 			,p_confirmed_locator_id => p_confirmed_locator_id
2552 			,p_revision           => p_revision
2553 			,x_confirmed_prim_qty => l_confirmed_prim_qty
2554 			,x_confirmed_sec_qty  => l_confirmed_sec_qty -- muom:sk
2555 			,x_confirmed_sugg_qty => l_confirmed_sugg_qty
2556 			,x_confirmed_pri_qty_string => l_confirmed_pri_qty_string
2557 			,x_qoh                => l_qoh
2558 			,x_sqoh               => l_sqoh
2559 			,x_att                => l_att
2560 			,x_satt               => l_satt
2561 			,x_return_status      => x_return_status
2562 			,x_msg_count          => x_msg_count
2563 			,x_msg_data           => x_msg_data
2564 			);
2565 
2566 	  IF x_return_status <> l_g_ret_sts_success THEN
2567 		 mydebug('x_return_status : ' || x_return_status);
2568 		 RAISE fnd_api.G_EXC_ERROR;
2569 	  END IF;
2570 
2571 	mydebug('After the call to proc_convert_qty_to_sugg_uom l_confirmed_pri_qty_string is '  || l_confirmed_pri_qty_string);
2572 	mydebug('After the call to proc_convert_qty_to_sugg_uom l_confirmed_prim_qty is '  || l_confirmed_prim_qty);
2573 	mydebug('After the call to proc_convert_qty_to_sugg_uom l_confirmed_sugg_qty is '  || l_confirmed_sugg_qty);
2574 	mydebug('After the call to proc_convert_qty_to_sugg_uom l_confirmed_sec_qty  is '  || l_confirmed_sec_qty);
2575 	mydebug('After the call to proc_convert_qty_to_sugg_uom l_qoh                is '  || l_qoh);
2576 	mydebug('After the call to proc_convert_qty_to_sugg_uom l_sqoh               is '  || l_sqoh);
2577 	mydebug('After the call to proc_convert_qty_to_sugg_uom l_att                is '  || l_att);
2578 	mydebug('After the call to proc_convert_qty_to_sugg_uom l_satt               is '  || l_satt);
2579 
2580    -- Create new MMTT line
2581 
2582 		-- muom:sk start
2583 		IF l_fulfillment_base <> 'S' THEN
2584 			IF (p_lot_controlled = 'Y') THEN
2585 				-- For lot and Lot+serial controlled items, secondary quantity will be stored at lot level (MTLT)
2586 				-- for vanilla items and serial controlled items it will be stored at MMTT level
2587 				WHILE  (n <> 0)
2588 				LOOP
2589 					n := INSTR(p_confirmed_sec_qty,l_delimiter,m,1);
2590 					IF n = 0 THEN -- Last part OF the string
2591 						l_confirmed_lot_trx_sec_qty :=  NVL(SUBSTR(p_confirmed_sec_qty,m,LENGTH(p_confirmed_sec_qty)), 0);
2592 					ELSE
2593 						l_confirmed_lot_trx_sec_qty :=  SUBSTR(p_confirmed_sec_qty,m,n-m) ;-- start at M get m-n chrs.
2594 						m := n+1;
2595 					END IF;
2596 						l_confirmed_sec_qty := NVL(l_confirmed_sec_qty,0)
2597 									+ fnd_number.canonical_to_number(l_confirmed_lot_trx_sec_qty);
2598 				END LOOP;
2599 			ELSE
2600 				l_confirmed_sec_qty := fnd_number.canonical_to_number(p_confirmed_sec_qty);
2601 			END IF;
2602 		END IF;
2603 		-- muom:sk end
2604 
2605 		mydebug ('l_confirmed_sec_qty post processing before MMTT insert is ' || l_confirmed_sec_qty);
2606 
2607    IF p_insert = 'Y' THEN
2608 	  l_progress   :=  '110';
2609 	  SELECT mtl_material_transactions_s.NEXTVAL
2610 	  INTO l_new_transaction_temp_id
2611 	  FROM DUAL;
2612 	  x_new_transaction_temp_id := l_new_transaction_temp_id;
2613 
2614 	  mydebug(l_proc_name || ': l_new_transaction_temp_id = ' || l_new_transaction_temp_id);
2615 
2616 	  INSERT INTO mtl_material_transactions_temp
2617 					( TRANSACTION_HEADER_ID
2618 					 ,TRANSACTION_TEMP_ID
2619 					 ,SOURCE_CODE
2620 					 ,SOURCE_LINE_ID
2621 					 ,TRANSACTION_MODE
2622 					 ,LOCK_FLAG
2623 					 ,LAST_UPDATE_DATE
2624 					 ,LAST_UPDATED_BY
2625 					 ,CREATION_DATE
2626 					 ,CREATED_BY
2627 					 ,LAST_UPDATE_LOGIN
2628 					 ,REQUEST_ID
2629 					 ,PROGRAM_APPLICATION_ID
2630 					 ,PROGRAM_ID
2631 					 ,PROGRAM_UPDATE_DATE
2632 					 ,INVENTORY_ITEM_ID
2633 					 ,REVISION
2634 					 ,ORGANIZATION_ID
2635 					 ,SUBINVENTORY_CODE
2636 					 ,LOCATOR_ID
2637 					 ,TRANSACTION_QUANTITY
2638 					 ,PRIMARY_QUANTITY
2639 					 ,TRANSACTION_UOM
2640 					 ,TRANSACTION_COST
2641 					 ,TRANSACTION_TYPE_ID
2642 					 ,TRANSACTION_ACTION_ID
2643 					 ,TRANSACTION_SOURCE_TYPE_ID
2644 					 ,TRANSACTION_SOURCE_ID
2645 					 ,TRANSACTION_SOURCE_NAME
2646 					 ,TRANSACTION_DATE
2647 					 ,ACCT_PERIOD_ID
2648 					 ,DISTRIBUTION_ACCOUNT_ID
2649 					 ,TRANSACTION_REFERENCE
2650 					 ,REQUISITION_LINE_ID
2651 					 ,REQUISITION_DISTRIBUTION_ID
2652 					 ,REASON_ID
2653 					 ,LOT_NUMBER
2654 					 ,LOT_EXPIRATION_DATE
2655 					 ,SERIAL_NUMBER
2656 					 ,RECEIVING_DOCUMENT
2657 					 ,DEMAND_ID
2658 					 ,RCV_TRANSACTION_ID
2659 					 ,MOVE_TRANSACTION_ID
2660 					 ,COMPLETION_TRANSACTION_ID
2661 					 ,WIP_ENTITY_TYPE
2662 					 ,SCHEDULE_ID
2663 					 ,REPETITIVE_LINE_ID
2664 					 ,EMPLOYEE_CODE
2665 					 ,PRIMARY_SWITCH
2666 					 ,SCHEDULE_UPDATE_CODE
2667 					 ,SETUP_TEARDOWN_CODE
2668 					 ,ITEM_ORDERING
2669 					 ,NEGATIVE_REQ_FLAG
2670 					 ,OPERATION_SEQ_NUM
2671 					 ,PICKING_LINE_ID
2672 					 ,TRX_SOURCE_LINE_ID
2673 					 ,TRX_SOURCE_DELIVERY_ID
2674 					 ,PHYSICAL_ADJUSTMENT_ID
2675 					 ,CYCLE_COUNT_ID
2676 					 ,RMA_LINE_ID
2677 					 ,CUSTOMER_SHIP_ID
2678 					 ,CURRENCY_CODE
2679 					 ,CURRENCY_CONVERSION_RATE
2680 					 ,CURRENCY_CONVERSION_TYPE
2681 					 ,CURRENCY_CONVERSION_DATE
2682 					 ,USSGL_TRANSACTION_CODE
2683 					 ,VENDOR_LOT_NUMBER
2684 					 ,ENCUMBRANCE_ACCOUNT
2685 					 ,ENCUMBRANCE_AMOUNT
2686 					 ,SHIP_TO_LOCATION
2687 					 ,SHIPMENT_NUMBER
2688 					 ,TRANSFER_COST
2689 					 ,TRANSPORTATION_COST
2690 					 ,TRANSPORTATION_ACCOUNT
2691 					 ,FREIGHT_CODE
2692 					 ,CONTAINERS
2693 					 ,WAYBILL_AIRBILL
2694 					 ,EXPECTED_ARRIVAL_DATE
2695 					 ,TRANSFER_SUBINVENTORY
2696 					 ,TRANSFER_ORGANIZATION
2697 					 ,TRANSFER_TO_LOCATION
2698 					 ,NEW_AVERAGE_COST
2699 					 ,VALUE_CHANGE
2700 					 ,PERCENTAGE_CHANGE
2701 					 ,MATERIAL_ALLOCATION_TEMP_ID
2702 					 ,DEMAND_SOURCE_HEADER_ID
2703 					 ,DEMAND_SOURCE_LINE
2704 					 ,DEMAND_SOURCE_DELIVERY
2705 					 ,ITEM_SEGMENTS
2706 					 ,ITEM_DESCRIPTION
2707 					 ,ITEM_TRX_ENABLED_FLAG
2708 					 ,ITEM_LOCATION_CONTROL_CODE
2709 					 ,ITEM_RESTRICT_SUBINV_CODE
2710 					 ,ITEM_RESTRICT_LOCATORS_CODE
2711 					 ,ITEM_REVISION_QTY_CONTROL_CODE
2712 					 ,ITEM_PRIMARY_UOM_CODE
2713 					 ,ITEM_UOM_CLASS
2714 					 ,ITEM_SHELF_LIFE_CODE
2715 					 ,ITEM_SHELF_LIFE_DAYS
2716 					 ,ITEM_LOT_CONTROL_CODE
2717 					 ,ITEM_SERIAL_CONTROL_CODE
2718 					 ,ITEM_INVENTORY_ASSET_FLAG
2719 					 ,ALLOWED_UNITS_LOOKUP_CODE
2720 					 ,DEPARTMENT_ID
2721 					 ,DEPARTMENT_CODE
2722 					 ,WIP_SUPPLY_TYPE
2723 					 ,SUPPLY_SUBINVENTORY
2724 					 ,SUPPLY_LOCATOR_ID
2725 					 ,VALID_SUBINVENTORY_FLAG
2726 					 ,VALID_LOCATOR_FLAG
2727 					 ,LOCATOR_SEGMENTS
2728 					 ,CURRENT_LOCATOR_CONTROL_CODE
2729 					 ,NUMBER_OF_LOTS_ENTERED
2730 					 ,WIP_COMMIT_FLAG
2731 					 ,NEXT_LOT_NUMBER
2732 					 ,LOT_ALPHA_PREFIX
2733 					 ,NEXT_SERIAL_NUMBER
2734 					 ,SERIAL_ALPHA_PREFIX
2735 					 ,SHIPPABLE_FLAG
2736 					 ,POSTING_FLAG
2737 					 ,REQUIRED_FLAG
2738 					 ,PROCESS_FLAG
2739 					 ,ERROR_CODE
2740 					 ,ERROR_EXPLANATION
2741 					 ,ATTRIBUTE_CATEGORY
2742 					 ,ATTRIBUTE1
2743 					 ,ATTRIBUTE2
2744 					 ,ATTRIBUTE3
2745 					 ,ATTRIBUTE4
2746 					 ,ATTRIBUTE5
2747 					 ,ATTRIBUTE6
2748 					 ,ATTRIBUTE7
2749 					 ,ATTRIBUTE8
2750 					 ,ATTRIBUTE9
2751 					 ,ATTRIBUTE10
2752 					 ,ATTRIBUTE11
2753 					 ,ATTRIBUTE12
2754 					 ,ATTRIBUTE13
2755 					 ,ATTRIBUTE14
2756 					 ,ATTRIBUTE15
2757 					 ,MOVEMENT_ID
2758 					 ,RESERVATION_QUANTITY
2759 					 ,SHIPPED_QUANTITY
2760 					 ,TRANSACTION_LINE_NUMBER
2761 					 ,TASK_ID
2762 					 ,TO_TASK_ID
2763 					 ,SOURCE_TASK_ID
2764 					 ,PROJECT_ID
2765 					 ,SOURCE_PROJECT_ID
2766 					 ,PA_EXPENDITURE_ORG_ID
2767 					 ,TO_PROJECT_ID
2768 					 ,EXPENDITURE_TYPE
2769 					 ,FINAL_COMPLETION_FLAG
2770 					 ,TRANSFER_PERCENTAGE
2771 					 ,TRANSACTION_SEQUENCE_ID
2772 					 ,MATERIAL_ACCOUNT
2773 					 ,MATERIAL_OVERHEAD_ACCOUNT
2774 					 ,RESOURCE_ACCOUNT
2775 					 ,OUTSIDE_PROCESSING_ACCOUNT
2776 					 ,OVERHEAD_ACCOUNT
2777 					 ,FLOW_SCHEDULE
2778 					 ,COST_GROUP_ID
2779 					 ,TRANSFER_COST_GROUP_ID
2780 					 ,DEMAND_CLASS
2781 					 ,QA_COLLECTION_ID
2782 					 ,KANBAN_CARD_ID
2783 					 ,OVERCOMPLETION_TRANSACTION_QTY
2784 					 ,OVERCOMPLETION_PRIMARY_QTY
2785 					 ,OVERCOMPLETION_TRANSACTION_ID
2786 					 ,END_ITEM_UNIT_NUMBER
2787 					 ,SCHEDULED_PAYBACK_DATE
2788 					 ,LINE_TYPE_CODE
2789 					 ,PARENT_TRANSACTION_TEMP_ID
2790 					 ,PUT_AWAY_STRATEGY_ID
2791 					 ,PUT_AWAY_RULE_ID
2792 					 ,PICK_STRATEGY_ID
2793 					 ,PICK_RULE_ID
2794 					 ,MOVE_ORDER_LINE_ID
2795 					 ,TASK_GROUP_ID
2796 					 ,PICK_SLIP_NUMBER
2797 					 ,RESERVATION_ID
2798 					 ,COMMON_BOM_SEQ_ID
2799 					 ,COMMON_ROUTING_SEQ_ID
2800 					 ,ORG_COST_GROUP_ID
2801 					 ,COST_TYPE_ID
2802 					 ,TRANSACTION_STATUS
2803 					 ,STANDARD_OPERATION_ID
2804 					 ,TASK_PRIORITY
2805 					 ,WMS_TASK_TYPE
2806 					 ,PARENT_LINE_ID
2807 					 ,LPN_ID
2808 					 ,TRANSFER_LPN_ID
2809 					 ,WMS_TASK_STATUS
2810 					 ,CONTENT_LPN_ID
2811 					 ,CONTAINER_ITEM_ID
2812 					 ,CARTONIZATION_ID
2813 					 ,PICK_SLIP_DATE
2814 					 ,REBUILD_ITEM_ID
2815 					 ,REBUILD_SERIAL_NUMBER
2816 					 ,REBUILD_ACTIVITY_ID
2817 					 ,REBUILD_JOB_NAME
2818 					 ,ORGANIZATION_TYPE
2819 					 ,TRANSFER_ORGANIZATION_TYPE
2820 					 ,OWNING_ORGANIZATION_ID
2821 					 ,OWNING_TP_TYPE
2822 					 ,XFR_OWNING_ORGANIZATION_ID
2823 					 ,TRANSFER_OWNING_TP_TYPE
2824 					 ,PLANNING_ORGANIZATION_ID
2825 					 ,PLANNING_TP_TYPE
2826 					 ,XFR_PLANNING_ORGANIZATION_ID
2827 					 ,TRANSFER_PLANNING_TP_TYPE
2828 					 ,SECONDARY_UOM_CODE
2829 					 ,SECONDARY_TRANSACTION_QUANTITY
2830 					 ,TRANSACTION_BATCH_ID
2831 					 ,TRANSACTION_BATCH_SEQ
2832 					 ,ALLOCATED_LPN_ID
2833 					 ,SCHEDULE_NUMBER
2834 					 ,SCHEDULED_FLAG
2835 					 ,CLASS_CODE
2836 					 ,SCHEDULE_GROUP
2837 					 ,BUILD_SEQUENCE
2838 					 ,BOM_REVISION
2839 					 ,ROUTING_REVISION
2840 					 ,BOM_REVISION_DATE
2841 					 ,ROUTING_REVISION_DATE
2842 					 ,ALTERNATE_BOM_DESIGNATOR
2843 					 ,ALTERNATE_ROUTING_DESIGNATOR
2844 					 ,OPERATION_PLAN_ID
2845 					 ,INTRANSIT_ACCOUNT
2846 					 ,FOB_POINT
2847 					 ,MOVE_ORDER_HEADER_ID
2848 					 ,SERIAL_ALLOCATED_FLAG
2849 					 ,FULFILLMENT_BASE
2850 					)
2851 		  (SELECT
2852 					 TRANSACTION_HEADER_ID
2853 					 ,l_new_transaction_temp_id
2854 					 ,SOURCE_CODE
2855 					 ,SOURCE_LINE_ID
2856 					 ,TRANSACTION_MODE
2857 					 ,LOCK_FLAG
2858 					 ,SYSDATE -- it should not copy from original MMTT
2859 					 ,p_user_id -- it should not copy from original MMTT
2860 					 ,SYSDATE
2861 					 ,p_user_id
2862 					 ,LAST_UPDATE_LOGIN
2863 					 ,REQUEST_ID
2864 					 ,PROGRAM_APPLICATION_ID
2865 					 ,PROGRAM_ID
2866 					 ,PROGRAM_UPDATE_DATE
2867 					 ,INVENTORY_ITEM_ID
2868 					 ,REVISION
2869 					 ,ORGANIZATION_ID
2870 					 ,p_confirmed_sub
2871 					 ,p_confirmed_locator_id
2872 					 ,p_confirmed_trx_qty
2873 					 ,l_confirmed_prim_qty
2874 					 ,NVL(p_confirmed_uom, DECODE(fulfillment_base, 'S', secondary_uom_code, item_primary_uom_code))--MUOM the transaction uom should be confirmed uom
2875 																													--or secondary uom for fulfillment_base 'S' else primary uom
2876 					 ,TRANSACTION_COST
2877 					 ,TRANSACTION_TYPE_ID
2878 					 ,TRANSACTION_ACTION_ID
2879 					 ,TRANSACTION_SOURCE_TYPE_ID
2880 					 ,TRANSACTION_SOURCE_ID
2881 					 ,TRANSACTION_SOURCE_NAME
2882 					 ,TRANSACTION_DATE
2883 					 ,ACCT_PERIOD_ID
2884 					 ,DISTRIBUTION_ACCOUNT_ID
2885 					 ,TRANSACTION_REFERENCE
2886 					 ,REQUISITION_LINE_ID
2887 					 ,REQUISITION_DISTRIBUTION_ID
2888 					 ,REASON_ID
2889 					 ,LOT_NUMBER
2890 					 ,LOT_EXPIRATION_DATE
2891 					 ,SERIAL_NUMBER
2892 					 ,RECEIVING_DOCUMENT
2893 					 ,DEMAND_ID
2894 					 ,RCV_TRANSACTION_ID
2895 					 ,MOVE_TRANSACTION_ID
2896 					 ,COMPLETION_TRANSACTION_ID
2897 					 ,WIP_ENTITY_TYPE
2898 					 ,SCHEDULE_ID
2899 					 ,REPETITIVE_LINE_ID
2900 					 ,EMPLOYEE_CODE
2901 					 ,PRIMARY_SWITCH
2902 					 ,SCHEDULE_UPDATE_CODE
2903 					 ,SETUP_TEARDOWN_CODE
2904 					 ,ITEM_ORDERING
2905 					 ,NEGATIVE_REQ_FLAG
2906 					 ,OPERATION_SEQ_NUM
2907 					 ,PICKING_LINE_ID
2908 					 ,TRX_SOURCE_LINE_ID
2909 					 ,TRX_SOURCE_DELIVERY_ID
2910 					 ,PHYSICAL_ADJUSTMENT_ID
2911 					 ,CYCLE_COUNT_ID
2912 					 ,RMA_LINE_ID
2913 					 ,CUSTOMER_SHIP_ID
2914 					 ,CURRENCY_CODE
2915 					 ,CURRENCY_CONVERSION_RATE
2916 					 ,CURRENCY_CONVERSION_TYPE
2917 					 ,CURRENCY_CONVERSION_DATE
2918 					 ,USSGL_TRANSACTION_CODE
2919 					 ,VENDOR_LOT_NUMBER
2920 					 ,ENCUMBRANCE_ACCOUNT
2921 					 ,ENCUMBRANCE_AMOUNT
2922 					 ,SHIP_TO_LOCATION
2923 					 ,SHIPMENT_NUMBER
2924 					 ,TRANSFER_COST
2925 					 ,TRANSPORTATION_COST
2926 					 ,TRANSPORTATION_ACCOUNT
2927 					 ,FREIGHT_CODE
2928 					 ,CONTAINERS
2929 					 ,WAYBILL_AIRBILL
2930 					 ,EXPECTED_ARRIVAL_DATE
2931 					 ,TRANSFER_SUBINVENTORY
2932 					 ,TRANSFER_ORGANIZATION
2933 					 ,TRANSFER_TO_LOCATION
2934 					 ,NEW_AVERAGE_COST
2935 					 ,VALUE_CHANGE
2936 					 ,PERCENTAGE_CHANGE
2937 					 ,MATERIAL_ALLOCATION_TEMP_ID
2938 					 ,DEMAND_SOURCE_HEADER_ID
2939 					 ,DEMAND_SOURCE_LINE
2940 					 ,DEMAND_SOURCE_DELIVERY
2941 					 ,ITEM_SEGMENTS
2942 					 ,ITEM_DESCRIPTION
2943 					 ,ITEM_TRX_ENABLED_FLAG
2944 					 ,ITEM_LOCATION_CONTROL_CODE
2945 					 ,ITEM_RESTRICT_SUBINV_CODE
2946 					 ,ITEM_RESTRICT_LOCATORS_CODE
2947 					 ,ITEM_REVISION_QTY_CONTROL_CODE
2948 					 ,ITEM_PRIMARY_UOM_CODE
2949 					 ,ITEM_UOM_CLASS
2950 					 ,ITEM_SHELF_LIFE_CODE
2951 					 ,ITEM_SHELF_LIFE_DAYS
2952 					 ,ITEM_LOT_CONTROL_CODE
2953 					 ,ITEM_SERIAL_CONTROL_CODE
2954 					 ,ITEM_INVENTORY_ASSET_FLAG
2955 					 ,ALLOWED_UNITS_LOOKUP_CODE
2956 					 ,DEPARTMENT_ID
2957 					 ,DEPARTMENT_CODE
2958 					 ,WIP_SUPPLY_TYPE
2959 					 ,SUPPLY_SUBINVENTORY
2960 					 ,SUPPLY_LOCATOR_ID
2961 					 ,VALID_SUBINVENTORY_FLAG
2962 					 ,VALID_LOCATOR_FLAG
2963 					 ,LOCATOR_SEGMENTS
2964 					 ,CURRENT_LOCATOR_CONTROL_CODE
2965 					 ,NUMBER_OF_LOTS_ENTERED
2966 					 ,WIP_COMMIT_FLAG
2967 					 ,NEXT_LOT_NUMBER
2968 					 ,LOT_ALPHA_PREFIX
2969 					 ,NEXT_SERIAL_NUMBER
2970 					 ,SERIAL_ALPHA_PREFIX
2971 					 ,SHIPPABLE_FLAG
2972 					 ,POSTING_FLAG
2973 					 ,REQUIRED_FLAG
2974 					 ,PROCESS_FLAG
2975 					 ,ERROR_CODE
2976 					 ,ERROR_EXPLANATION
2977 					 ,ATTRIBUTE_CATEGORY
2978 					 ,ATTRIBUTE1
2979 					 ,ATTRIBUTE2
2980 					 ,ATTRIBUTE3
2981 					 ,ATTRIBUTE4
2982 					 ,ATTRIBUTE5
2983 					 ,ATTRIBUTE6
2984 					 ,ATTRIBUTE7
2985 					 ,ATTRIBUTE8
2986 					 ,ATTRIBUTE9
2987 					 ,ATTRIBUTE10
2988 					 ,ATTRIBUTE11
2989 					 ,ATTRIBUTE12
2990 					 ,ATTRIBUTE13
2991 					 ,ATTRIBUTE14
2992 					 ,ATTRIBUTE15
2993 					 ,MOVEMENT_ID
2994 					 ,RESERVATION_QUANTITY
2995 					 ,SHIPPED_QUANTITY
2996 					 ,TRANSACTION_LINE_NUMBER
2997 					 ,TASK_ID
2998 					 ,TO_TASK_ID
2999 					 ,SOURCE_TASK_ID
3000 					 ,PROJECT_ID
3001 					 ,SOURCE_PROJECT_ID
3002 					 ,PA_EXPENDITURE_ORG_ID
3003 					 ,TO_PROJECT_ID
3004 					 ,EXPENDITURE_TYPE
3005 					 ,FINAL_COMPLETION_FLAG
3006 					 ,TRANSFER_PERCENTAGE
3007 					 ,TRANSACTION_SEQUENCE_ID
3008 					 ,MATERIAL_ACCOUNT
3009 					 ,MATERIAL_OVERHEAD_ACCOUNT
3010 					 ,RESOURCE_ACCOUNT
3011 					 ,OUTSIDE_PROCESSING_ACCOUNT
3012 					 ,OVERHEAD_ACCOUNT
3013 					 ,FLOW_SCHEDULE
3014 					 ,COST_GROUP_ID
3015 					 ,TRANSFER_COST_GROUP_ID
3016 					 ,DEMAND_CLASS
3017 					 ,QA_COLLECTION_ID
3018 					 ,KANBAN_CARD_ID
3019 					 ,OVERCOMPLETION_TRANSACTION_QTY
3020 					 ,OVERCOMPLETION_PRIMARY_QTY
3021 					 ,OVERCOMPLETION_TRANSACTION_ID
3022 					 ,END_ITEM_UNIT_NUMBER
3023 					 ,SCHEDULED_PAYBACK_DATE
3024 					 ,LINE_TYPE_CODE
3025 					 ,PARENT_TRANSACTION_TEMP_ID
3026 					 ,PUT_AWAY_STRATEGY_ID
3027 					 ,PUT_AWAY_RULE_ID
3028 					 ,PICK_STRATEGY_ID
3029 					 ,PICK_RULE_ID
3030 					 ,MOVE_ORDER_LINE_ID
3031 					 ,TASK_GROUP_ID
3032 					 ,PICK_SLIP_NUMBER
3033 					 ,reservation_id
3034 					 ,COMMON_BOM_SEQ_ID
3035 					 ,COMMON_ROUTING_SEQ_ID
3036 					 ,ORG_COST_GROUP_ID
3037 					 ,COST_TYPE_ID
3038 					 ,TRANSACTION_STATUS
3039 					 ,STANDARD_OPERATION_ID
3040 					 ,TASK_PRIORITY
3041 					 ,WMS_TASK_TYPE
3042 					 ,DECODE(PARENT_LINE_ID, NULL,NULL,l_new_transaction_temp_id) -- Take care of BULK parent
3043 					 ,NVL(p_lpn_id,p_parent_lpn_id)     -- process the nesting
3044 														-- fully consumble LPN Pick
3045 					 ,p_transfer_lpn_id                 -- Bug4185621: instead of inheriting previous line's status, use loaded as status for new line
3046 					 ,p_wms_task_status                 -- WMS Task Status
3047 					 ,p_content_lpn_id
3048 					 ,NVL(p_container_item_id,container_item_id)
3049 					 ,CARTONIZATION_ID
3050 					 ,PICK_SLIP_DATE
3051 					 ,REBUILD_ITEM_ID
3052 					 ,REBUILD_SERIAL_NUMBER
3053 					 ,REBUILD_ACTIVITY_ID
3054 					 ,REBUILD_JOB_NAME
3055 					 ,ORGANIZATION_TYPE
3056 					 ,TRANSFER_ORGANIZATION_TYPE
3057 					 ,OWNING_ORGANIZATION_ID
3058 					 ,OWNING_TP_TYPE
3059 					 ,XFR_OWNING_ORGANIZATION_ID
3060 					 ,TRANSFER_OWNING_TP_TYPE
3061 					 ,PLANNING_ORGANIZATION_ID
3062 					 ,PLANNING_TP_TYPE
3063 					 ,XFR_PLANNING_ORGANIZATION_ID
3064 					 ,TRANSFER_PLANNING_TP_TYPE
3065 					 ,p_confirmed_sec_uom
3066 					 ,DECODE(p_confirmed_sec_uom, NULL, NULL, l_confirmed_sec_qty)--Bug4576653/MUOM for fulfill base 'S' assumption is confirmed_sec_uom won't be NULL
3067 					 ,TRANSACTION_BATCH_ID
3068 					 ,TRANSACTION_BATCH_SEQ
3069 					 ,ALLOCATED_LPN_ID
3070 					 ,SCHEDULE_NUMBER
3071 					 ,SCHEDULED_FLAG
3072 					 ,CLASS_CODE
3073 					 ,SCHEDULE_GROUP
3074 					 ,BUILD_SEQUENCE
3075 					 ,BOM_REVISION
3076 					 ,ROUTING_REVISION
3077 					 ,BOM_REVISION_DATE
3078 					 ,ROUTING_REVISION_DATE
3079 					 ,ALTERNATE_BOM_DESIGNATOR
3080 					 ,ALTERNATE_ROUTING_DESIGNATOR
3081 					 ,OPERATION_PLAN_ID
3082 					 ,INTRANSIT_ACCOUNT
3083 					 ,FOB_POINT
3084 					 ,MOVE_ORDER_HEADER_ID
3085 					 ,SERIAL_ALLOCATED_FLAG
3086 					 ,FULFILLMENT_BASE
3087 			 FROM mtl_material_transactions_temp
3088 			WHERE transaction_temp_id = p_transaction_temp_id);
3089 		   IF SQL%NOTFOUND THEN
3090 			  mydebug (' p_transaction_temp_id: NOT found : ' || p_transaction_temp_id);
3091 			  fnd_message.set_name('WMS', 'WMS_INSERT_ALLOCATION'); -- NEWMSG
3092 			  -- "Error Inserting Allocation ."
3093 			  fnd_msg_pub.ADD;
3094 			  RAISE fnd_api.G_EXC_ERROR;
3095 		   END IF;
3096 	  l_progress    :=  '120';
3097 	  mydebug ('l_progress: ' || l_progress );
3098    END IF ; -- insert MMTT only if p_insert = 'Y'
3099    l_progress    :=  '130';
3100    mydebug ('l_progress: ' || l_progress );
3101    -- ****Lot Controlled items
3102 
3103    IF p_confirmed_lots     IS NOT NULL  OR
3104 	  p_confirmed_serials  IS NOT NULL
3105    THEN
3106 	  l_progress    :=  '140';
3107 	  mydebug ('l_progress: ' || l_progress );
3108 	   proc_parse_lot_serial_catchwt
3109 			  (p_inventory_item_id        => p_inventory_item_id
3110 			  ,p_organization_id          => p_organization_id  --BUG12622871
3111 			  ,p_fulfillment_base         => l_fulfillment_base
3112 			  ,p_confirmed_lots           => p_confirmed_lots
3113 			  ,p_confirmed_lot_trx_qty    => p_confirmed_lot_trx_qty
3114 			  ,p_confirmed_lot_pri_qty    => l_confirmed_pri_qty_string
3115 			  ,p_confirmed_serials        => p_confirmed_serials
3116 			  ,p_suggested_uom            => p_suggested_uom
3117 			  ,p_confirmed_uom            => p_confirmed_uom
3118 			  ,p_primary_uom              => p_primary_uom
3119 			  ,p_confirmed_prim_qty       => l_confirmed_prim_qty
3120 			  ,p_confirmed_sugg_qty       => l_confirmed_sugg_qty
3121 			  ,p_confirmed_sec_uom        => p_confirmed_sec_uom
3122 			  ,p_confirmed_sec_qty        => p_confirmed_sec_qty
3123 			  ,x_return_status            => x_return_status
3124 			  ,x_msg_count                => x_msg_count
3125 			  ,x_msg_data                 => x_msg_data);
3126 	   IF x_return_status <> l_g_ret_sts_success
3127 	   THEN
3128 		   fnd_message.set_name('WMS', 'WMS_INTERNAL_ERROR');
3129 		   fnd_message.set_token('ROUTINE', ' - proc_parse_lot_serial_catchwt API ' );
3130 		   mydebug('Error parsing lot/serial/catch weight string' );
3131 		   RAISE fnd_api.G_EXC_ERROR;
3132 	   END IF;
3133    END IF;
3134 
3135    -- ****Lot only Controlled items  OR
3136    -- ****Lot and  Serial Controlled items
3137    IF (p_lot_controlled = 'Y' )
3138    THEN
3139 	   l_progress    :=  '140';
3140 	   proc_process_confirmed_lots
3141 				  (p_action                               =>  p_action
3142 				  ,p_insert                               =>  p_insert
3143 				  ,p_update                               =>  p_update
3144 				  ,p_organization_id                      =>  p_organization_id
3145 				  ,p_user_id                              =>  p_user_id
3146 				  ,p_transaction_header_id                =>  p_transaction_header_id
3147 				  ,p_transaction_temp_id                  =>  p_transaction_temp_id
3148 				  ,p_new_transaction_temp_id              =>  l_new_transaction_temp_id
3149 				  ,p_transaction_temp_id_to_merge         =>  p_transaction_temp_id_to_merge
3150 				  ,p_inventory_item_id                    =>  p_inventory_item_id
3151 				  ,p_revision                             =>  p_revision
3152 				  ,p_suggested_uom                        =>  p_suggested_uom
3153 				  ,p_confirmed_uom                        =>  p_confirmed_uom
3154 				  ,p_primary_uom                          =>  p_primary_uom
3155 				  ,p_confirmed_lots                       =>  p_confirmed_lots
3156 				  ,p_confirmed_lot_trx_qty                =>  p_confirmed_lot_trx_qty
3157 				  ,p_confirmed_serials                    =>  p_confirmed_serials
3158 				  ,p_serial_allocated_flag                =>  p_serial_allocated_flag
3159 				  ,p_lpn_match                            =>  p_lpn_match
3160 				  ,p_lpn_match_lpn_id                     =>  p_lpn_match_lpn_id
3161 				  ,p_confirmed_sec_uom                    =>  p_confirmed_sec_uom
3162 				  ,p_confirmed_sec_qty                    =>  p_confirmed_sec_qty
3163 				  ,p_lot_controlled                       =>  p_lot_controlled
3164 				  ,p_serial_controlled                    =>  p_serial_controlled
3165 				  ,p_exception                            =>  p_exception
3166 				  ,x_return_status                        =>  x_return_status
3167 				  ,x_msg_count                            =>  x_msg_count
3168 				  ,x_msg_data                             =>  x_msg_data
3169 				  ,p_substitute_lots			          =>  p_substitute_lots         -- Bug 9448490 Lot Substitution Project
3170 				  ,p_full_lot_allocation                  =>  p_full_lot_allocation     -- Added for 14699845 (Flexible Lot Allocation)
3171 				  ,p_fulfillment_base                     =>  NVL(l_fulfillment_base, 'P')  -- 16070349
3172 				  ,p_orig_mmtt_txn_uom                    =>  l_orig_mmtt_txn_uom);         -- 16070349
3173 	   IF x_return_status <> l_g_ret_sts_success THEN
3174 		   mydebug('proc_process_confirmed_lots.x_return_status : ' || x_return_status);
3175 		   fnd_message.set_name('WMS', 'WMS_INTERNAL_ERROR');
3176 		   fnd_message.set_token('ROUTINE', '- proc_process_confirmed_lots API' );
3177 		   fnd_msg_pub.ADD;
3178 		   RAISE fnd_api.g_exc_error;
3179 	   END IF;
3180 	   mydebug ('Return from proc_process_confirmed_lots ' );
3181    END IF;
3182    l_progress    :=  '150';
3183    -- ****Serial Controlled items
3184    IF (p_lot_controlled = 'N' AND p_serial_controlled = 'Y')
3185    THEN
3186 	   l_progress    :=  '300';
3187 	   mydebug ('l_progress: ' || l_progress );
3188 	   -- may not be necessary to call if p_insert = N , LOAD_SINGLE
3189 	   proc_process_confirmed_serials
3190 				  (p_action                               =>  p_action
3191 				  ,p_insert                               =>  p_insert
3192 				  ,p_update                               =>  p_update
3193 				  ,p_organization_id                      =>  p_organization_id
3194 				  ,p_user_id                              =>  p_user_id
3195 				  ,p_transaction_header_id                =>  p_transaction_header_id
3196 				  ,p_transaction_temp_id                  =>  p_transaction_temp_id
3197 				  ,p_new_transaction_temp_id              =>  l_new_transaction_temp_id
3198 				  ,p_transaction_temp_id_to_merge         =>  p_transaction_temp_id_to_merge
3199 				  ,p_serial_transaction_temp_id           =>  NULL
3200 				  ,p_mtlt_serial_temp_id                  =>  NULL
3201 				  ,p_inventory_item_id                    =>  p_inventory_item_id
3202 				  ,p_revision                             =>  p_revision
3203 				  ,p_suggested_uom                        =>  p_suggested_uom
3204 				  ,p_confirmed_uom                        =>  p_confirmed_uom
3205 				  ,p_primary_uom                          =>  p_primary_uom
3206 				  ,p_serial_lot_number                    =>  NULL
3207 				  ,p_confirmed_serials                    =>  p_confirmed_serials
3208 				  ,p_serial_allocated_flag                =>  p_serial_allocated_flag
3209 				  ,p_lpn_match                            =>  p_lpn_match
3210 				  ,p_lpn_match_lpn_id                     =>  p_lpn_match_lpn_id
3211 				  ,p_lot_controlled                       =>  p_lot_controlled
3212 				  ,p_serial_controlled                    =>  p_serial_controlled
3213 				  ,x_return_status                        =>  x_return_status
3214 				  ,x_msg_count                            =>  x_msg_count
3215 				  ,x_msg_data                             =>  x_msg_data
3216 				  ,p_substitute_lots			          =>  p_substitute_lots       --BUG12670785
3217 				  ,p_full_lot_allocation                  =>  p_full_lot_allocation); -- Added for 14699845 (Flexible Lot Allocation)
3218 	   IF x_return_status <> l_g_ret_sts_success THEN
3219 		  mydebug('proc_process_confirmed_serials.x_return_status : ' || x_return_status);
3220 		  fnd_message.set_name('WMS', 'WMS_INTERNAL_ERROR'); --NEWMSG
3221 		  -- Internal Error $ROUTINE
3222 		  fnd_message.set_token('ROUTINE', '- proc_process_confirmed_serials API' );
3223 		  fnd_msg_pub.ADD;
3224 		  RAISE fnd_api.g_exc_error;
3225 	   END IF;
3226 	   l_progress    :=  '330';
3227 	   mydebug ('l_progress: ' || l_progress );
3228    END IF;
3229 
3230    l_progress    :=  '170';
3231    -- update p_transaction_temp_id (reduce qty) for p_update = Y1 or Y2
3232    mydebug ('l_progress: ' || l_progress );
3233    mydebug ('p_confirmed_trx_qty    : ' || p_confirmed_trx_qty);
3234    mydebug ('l_confirmed_prim_qty   : ' || l_confirmed_prim_qty);
3235    mydebug ('l_confirmed_sugg_qty   : ' || l_confirmed_sugg_qty);
3236    mydebug ('l_confirmed_sec_qty    : ' || l_confirmed_sec_qty);  -- muom:sk
3237    mydebug ('p_confirmed_sec_uom    : ' || p_confirmed_sec_uom);  -- muom:sk
3238    mydebug ('l_fulfillment_base     : ' || l_fulfillment_base);
3239    mydebug ('l_orig_mmtt_txn_qty    : ' || l_orig_mmtt_txn_qty);
3240    mydebug ('l_orig_mmtt_txn_uom    : ' || l_orig_mmtt_txn_uom);
3241    mydebug ('l_orig_mmtt_pri_qty    : ' || l_orig_mmtt_pri_qty);
3242    mydebug ('l_orig_mmtt_sec_qty    : ' || l_orig_mmtt_sec_qty);
3243 
3244    --Compute the new split MMTT quantity for various scenarios. For fulfillment base 'P' use existing logic and for fulfillment_base 'S' compute with new logic
3245    IF(l_fulfillment_base = 'S') THEN
3246 
3247 		l_calc_mmtt_pri_qty := l_orig_mmtt_pri_qty - l_confirmed_prim_qty;
3248 
3249 		IF(l_calc_mmtt_pri_qty <= 0) THEN
3250 			mydebug ('l_calc_mmtt_pri_qty came out to be ' || l_calc_mmtt_pri_qty || 'which is less that zero so recalculating');
3251 			l_calc_mmtt_pri_qty := inv_convert.inv_um_convert(
3252 													item_id       => p_inventory_item_id,
3253 													precision     => l_g_decimal_precision,
3254 													from_quantity => (l_orig_mmtt_sec_qty - l_confirmed_sec_qty),
3255 													from_unit     => p_confirmed_sec_uom,
3256 													to_unit       => p_primary_uom,
3257 													from_name     => NULL,
3258 													to_name       => NULL);
3259 		END IF;
3260 
3261 		IF(p_confirmed_sec_uom = l_orig_mmtt_txn_uom) THEN
3262 			l_calc_mmtt_txn_qty := l_orig_mmtt_txn_qty - l_confirmed_sec_qty;
3263 
3264 		ELSIF(p_primary_uom = l_orig_mmtt_txn_uom) THEN
3265 			l_calc_mmtt_txn_qty := l_calc_mmtt_pri_qty;
3266 
3267 		ELSE
3268 			l_calc_mmtt_txn_qty := l_orig_mmtt_txn_qty - inv_convert.inv_um_convert(
3269 															item_id       => p_inventory_item_id,
3270 															precision     => l_g_decimal_precision,
3271 															from_quantity => l_confirmed_sec_qty,
3272 															from_unit     => p_confirmed_sec_uom,
3273 															to_unit       => l_orig_mmtt_txn_uom,
3274 															from_name     => NULL,
3275 															to_name       => NULL);
3276 		END IF;
3277 
3278 		IF(l_calc_mmtt_txn_qty <= 0) THEN
3279 			mydebug ('l_calc_mmtt_txn_qty came out to be ' || l_calc_mmtt_txn_qty || 'which is less that zero so recalculating - Unless Overpick scenario this should not happen');
3280 			l_calc_mmtt_txn_qty := inv_convert.inv_um_convert(
3281 									   item_id       => p_inventory_item_id,
3282 									   precision     => l_g_decimal_precision,
3283 									   from_quantity => (l_orig_mmtt_sec_qty - l_confirmed_sec_qty),
3284 									   from_unit     => p_confirmed_sec_uom,
3285 									   to_unit       => l_orig_mmtt_txn_uom,
3286 									   from_name     => NULL,
3287 									   to_name       => NULL);
3288 		END IF;
3289 
3290    ELSE --Fulfillment base 'P' use old logic of just decrementing previous qty
3291 		l_calc_mmtt_txn_qty := l_orig_mmtt_txn_qty - l_confirmed_sugg_qty;
3292 		l_calc_mmtt_pri_qty := l_orig_mmtt_pri_qty - l_confirmed_prim_qty;
3293    END IF;
3294 
3295    mydebug ('Final l_calc_mmtt_txn_qty    : ' || l_calc_mmtt_txn_qty);
3296    mydebug ('Final l_calc_mmtt_pri_qty    : ' || l_calc_mmtt_pri_qty);
3297 
3298    IF p_update = 'Y1' -- update p_transaction_temp_id  to reduce primary/trxqty
3299    THEN
3300 	  IF p_action = l_g_action_split  THEN
3301 		 l_progress    :=  '190';
3302 		 mydebug('l_progress for p_update Y1 in split case ' || l_progress);
3303 		 IF ((p_exception='OVER' OR l_fulfillment_base = 'S') AND p_lot_controlled='Y' AND p_full_lot_allocation='Y') THEN --Modified for 14699845 (Flexible Lot Allocation)
3304 
3305 			SELECT SUM(primary_quantity), SUM(transaction_quantity), SUM(SECONDARY_QUANTITY)
3306 			INTO l_rem_lot_pri_qty, l_rem_lot_trx_qty, l_rem_lot_sec_qty
3307 			FROM mtl_transaction_lots_temp
3308 			WHERE transaction_temp_id = p_transaction_temp_id
3309 			GROUP BY transaction_temp_id;
3310 
3311 			 UPDATE  mtl_material_transactions_temp
3312 			 SET     transaction_quantity   = l_rem_lot_trx_qty
3313 			   , primary_quantity           = l_rem_lot_pri_qty
3314 			   , secondary_transaction_quantity = l_rem_lot_sec_qty
3315 			   , last_update_date           =  SYSDATE
3316 			   , last_updated_by            =  p_user_id
3317 			 WHERE   transaction_temp_id    =  p_transaction_temp_id;
3318 
3319 		 ELSE
3320 		   UPDATE mtl_material_transactions_temp
3321 		   SET    transaction_quantity      =  l_calc_mmtt_txn_qty
3322 				, primary_quantity          =  l_calc_mmtt_pri_qty
3323 				, secondary_transaction_quantity  =  secondary_transaction_quantity - l_confirmed_sec_qty
3324 				, last_update_date          =  SYSDATE
3325 				, last_updated_by           =  p_user_id
3326 		   WHERE   transaction_temp_id      =  p_transaction_temp_id;
3327 		 END IF;
3328 
3329 		 IF SQL%NOTFOUND THEN
3330 			RAISE fnd_api.G_EXC_ERROR;
3331 		 END IF;
3332 	  ELSE  -- LOAD
3333 		 l_progress    :=  '200';
3334 		 mydebug('l_progress : ' || l_progress);
3335 		 UPDATE  mtl_material_transactions_temp
3336 		 SET     transaction_quantity   =  p_confirmed_trx_qty
3337 			   , primary_quantity       =  l_confirmed_prim_qty -- muom:sk added decode this is for full pick again..  SSK
3338 			   , secondary_transaction_quantity    =  DECODE(p_confirmed_sec_uom, NULL, NULL, l_confirmed_sec_qty)
3339 			   , secondary_uom_code     =  p_confirmed_sec_uom
3340 			   , lpn_id                 =  NVL(p_lpn_id,p_parent_lpn_id)  -- process the nesting -- fully consumble LPN Pick
3341 			   , content_lpn_id         =  p_content_lpn_id
3342 			   , transfer_lpn_id        =  p_transfer_lpn_id
3343 			   , subinventory_code      =  p_confirmed_sub
3344 			   , locator_id             =  p_confirmed_locator_id
3345 			   , transaction_uom        =  p_confirmed_uom
3346 			   , container_item_id      =  p_container_item_id
3347 			   , last_update_date       =  SYSDATE
3348 			   , last_updated_by        =  p_user_id
3349 			   , wms_task_status        =  p_wms_task_status    -- Bug4185621: update mmtt task status to loaded
3350 		 WHERE  transaction_temp_id     =  p_transaction_temp_id;
3351 
3352 		 IF SQL%NOTFOUND THEN
3353 			RAISE fnd_api.G_EXC_ERROR;
3354 		 END IF;
3355 	  END IF;
3356    END IF;
3357 
3358    l_progress    :=  '180';
3359 
3360    IF p_update = 'Y2' -- and update p_transaction_temp_id_to_merge to add qty)
3361    THEN
3362 		 l_progress    :=  '190';
3363 		 mydebug ('l_progress in Y2 ' || l_progress);
3364 		 UPDATE  mtl_material_transactions_temp
3365 		 SET     transaction_quantity  =  transaction_quantity + p_confirmed_trx_qty
3366 			   , primary_quantity      =  primary_quantity + NVL(l_confirmed_prim_qty  ,0)
3367 			   , secondary_transaction_quantity    =  secondary_transaction_quantity +  NVL(l_confirmed_sec_qty, 0)
3368 			   , secondary_uom_code    =  p_confirmed_sec_uom
3369 			   , last_update_date      = SYSDATE
3370 			   , last_updated_by       = p_user_id
3371 		 WHERE   transaction_temp_id = p_transaction_temp_id_to_merge;
3372 		 IF SQL%NOTFOUND THEN
3373 			RAISE fnd_api.G_EXC_ERROR;
3374 		 END IF;
3375 
3376 		 IF p_action = l_g_action_load_multiple  THEN
3377 			l_progress    :=  '190'; -- Delete the original MMTT, if merging into another MMTT
3378 			mydebug ('l_progress for load multipe in Y2 ' || l_progress);
3379 			DELETE  mtl_material_transactions_temp
3380 			WHERE   transaction_temp_id = p_transaction_temp_id;
3381 			IF SQL%NOTFOUND THEN
3382 			   RAISE fnd_api.G_EXC_ERROR;
3383 			END IF;
3384 
3385 		 ELSE -- 'SPLIT'
3386 			l_progress    :=  '200';
3387 			mydebug('l_progress for split case in p_action Y2 ' || l_progress);
3388 			UPDATE  mtl_material_transactions_temp
3389 			SET     transaction_quantity    =  l_calc_mmtt_txn_qty
3390 				   , primary_quantity       =  l_calc_mmtt_pri_qty
3391 				   , secondary_transaction_quantity  =  secondary_transaction_quantity -  l_confirmed_sec_qty
3392 				   , last_update_date       = SYSDATE
3393 				   , last_updated_by        = p_user_id
3394 			WHERE   transaction_temp_id     = p_transaction_temp_id;
3395 
3396 			IF SQL%NOTFOUND THEN
3397 			   RAISE fnd_api.G_EXC_ERROR;
3398 			END IF;
3399 		 END IF;
3400    END IF;
3401    mydebug ('End of :' || l_proc_name);
3402 
3403 EXCEPTION
3404    WHEN fnd_api.g_exc_error THEN
3405 		x_return_status  := l_g_ret_sts_error;
3406 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3407 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
3408 		mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
3409 		mydebug('ROLLBACK ' );
3410 		ROLLBACK ;
3411    WHEN fnd_api.g_exc_unexpected_error THEN
3412 		x_return_status  := fnd_api.g_ret_sts_unexp_error;
3413 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3414 		mydebug('ROLLBACK ' );
3415 		ROLLBACK ;
3416 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
3417 		mydebug('RAISE fnd_api.g_exc_unexpected_error: ' || SQLERRM);
3418 		mydebug('x_msg_count/Data = ' || x_msg_count || '/'|| x_msg_data);
3419    WHEN OTHERS THEN
3420 		x_return_status  := l_g_ret_sts_unexp_error;
3421 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
3422 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
3423 		mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
3424 		mydebug('ROLLBACK ' );
3425 		ROLLBACK ;
3426 END proc_insert_update_mmtt;
3427 
3428 PROCEDURE proc_process_confirmed_lots
3429 			 ( p_action                          IN            VARCHAR2
3430 			  ,p_insert                          IN            VARCHAR2
3431 			  ,p_update                          IN            VARCHAR2
3432 			  ,p_organization_id                 IN            NUMBER
3433 			  ,p_user_id                         IN            NUMBER
3434 			  ,p_transaction_header_id           IN            NUMBER
3435 			  ,p_transaction_temp_id             IN            NUMBER
3436 			  ,p_new_transaction_temp_id         IN            NUMBER
3437 			  ,p_transaction_temp_id_to_merge    IN            NUMBER
3438 			  ,p_inventory_item_id               IN            NUMBER
3439 			  ,p_revision                        IN            VARCHAR2
3440 			  ,p_suggested_uom                   IN            VARCHAR2
3441 			  ,p_confirmed_uom                   IN            VARCHAR2
3442 			  ,p_primary_uom                     IN            VARCHAR2
3443 			  ,p_confirmed_lots                  IN            VARCHAR2
3444 			  ,p_confirmed_lot_trx_qty           IN            VARCHAR2
3445 			  ,p_confirmed_serials               IN            VARCHAR2
3446 			  ,p_serial_allocated_flag           IN            VARCHAR2
3447 			  ,p_lpn_match                       IN            NUMBER
3448 			  ,p_lpn_match_lpn_id                IN            NUMBER
3449 			  ,p_confirmed_sec_uom               IN            VARCHAR2
3450 			  ,p_confirmed_sec_qty               IN            VARCHAR2
3451 			  ,p_lot_controlled                  IN            VARCHAR2  -- Y/N
3452 			  ,p_serial_controlled               IN            VARCHAR2  -- Y/N
3453 			  ,p_exception                       IN            VARCHAR2 -- SHORT/OVER
3454 			  ,x_return_status                   OUT NOCOPY    VARCHAR2
3455 			  ,x_msg_count                       OUT NOCOPY    NUMBER
3456 			  ,x_msg_data                        OUT NOCOPY    VARCHAR2
3457 			  ,p_substitute_lots		         IN	           VARCHAR2 --/* Bug 9448490 Lot Substitution Project */
3458 			  ,p_full_lot_allocation             IN            VARCHAR2 DEFAULT 'Y' -- Added for 14699845 (Flexible Lot Allocation)
3459 			  ,p_fulfillment_base                IN            VARCHAR2
3460 			  ,p_orig_mmtt_txn_uom               IN            VARCHAR2)
3461 IS
3462    l_proc_name                         VARCHAR2(30) :=  'PROC_PROCESS_CONFIRMED_LOTS';
3463    l_progress                          VARCHAR2(30) :=  '100';
3464    l_delimiter                         VARCHAR2(30) :=  ':';
3465    l_prev_lot_number                   VARCHAR2(80) :=  '@@@';
3466    l_lot_number                        VARCHAR2(80) :=  NULL;
3467    l_lot_trx_qty                       NUMBER       :=  NULL;
3468    l_lot_prim_qty                      NUMBER       :=  NULL;
3469    l_lot_sugg_qty                      NUMBER       :=  NULL;
3470    l_original_lot_prim_qty             NUMBER       :=  NULL;
3471    l_serial_transaction_temp_id        NUMBER       :=  NULL;
3472    l_mtlt_serial_temp_id               NUMBER       :=  NULL;
3473    --/* Bug 9448490 Lot Substitution Project */ start
3474    l_lot_number_ls                     VARCHAR2(30)	 :=  NULL;
3475    l_lot_trx_qty_ls                    NUMBER       :=  NULL;
3476    l_lot_prim_qty_ls                   NUMBER       :=  NULL;
3477    l_lot_sec_qty_ls                    NUMBER       :=  NULL;
3478    l_return_status_ls		           VARCHAR2(30)  :=  NULL;
3479 
3480    l_ls_temp_id			               NUMBER       :=  NULL;
3481    l_ls_lot_number		               VARCHAR2(30) :=  NULL;
3482    l_ls_lot_trx_qty		               NUMBER       :=  NULL;
3483    l_ls_lot_prim_qty		           NUMBER       :=  NULL;
3484    --/* Bug 9448490 Lot Substitution Project */ end
3485 
3486    l_orig_lot_txn_qty                  NUMBER       :=  NULL;
3487    l_orig_lot_sec_qty                  NUMBER       :=  NULL;
3488    l_calc_lot_txn_qty                  NUMBER       :=  NULL;
3489 
3490 
3491    CURSOR  cur_mtlt_to_copy_from (p_lot_number                VARCHAR2
3492 								 ,p_lot_transaction_temp_id   NUMBER )  IS
3493    SELECT  mtlt.*
3494    FROM    mtl_transaction_lots_temp mtlt
3495    WHERE   transaction_temp_id = p_lot_transaction_temp_id
3496    AND     lot_number = p_lot_number;
3497 
3498    l_rec_mtlt_to_copy_from     mtl_transaction_lots_temp%ROWTYPE;
3499 
3500    CURSOR  cur_confirmed_lots_serials  IS
3501    SELECT  DISTINCT              -- so that we get only lot records in case of lot+Serial item control
3502 		   lot_number
3503 		  ,transaction_temp_id
3504 		  ,serial_number
3505 		  ,transaction_quantity
3506 		  ,primary_quantity
3507 		  ,suggested_quantity
3508 		  ,secondary_quantity
3509 	 FROM  mtl_allocations_gtmp
3510 	ORDER BY
3511 		   transaction_temp_id
3512 		  ,lot_number;
3513 
3514   --/* Bug 9448490 Lot Substitution Project */ start
3515 	CURSOR cur_ins_mtlt_lot_sub (p_lot_transaction_temp_id NUMBER)
3516 	IS
3517 	  SELECT DISTINCT lot_number, transaction_quantity,
3518 					  primary_quantity, secondary_quantity  --BUG12670785
3519 	FROM mtl_allocations_gtmp
3520 				WHERE lot_number NOT IN (
3521 						 SELECT DISTINCT lot_number
3522 				 FROM mtl_transaction_lots_temp
3523 				 WHERE transaction_temp_id = p_lot_transaction_temp_id)
3524 	ORDER BY lot_number;
3525 
3526 	CURSOR cur_mtlts_deleted_ls IS
3527 	SELECT mtlt.transaction_temp_id, mtlt.lot_number, mtlt.primary_quantity FROM mtl_transaction_lots_temp mtlt
3528 		WHERE mtlt.lot_number NOT IN (SELECT mag.lot_number FROM mtl_allocations_gtmp mag)
3529 		AND mtlt.transaction_temp_id = p_transaction_temp_id;
3530 
3531   --/* Bug 9448490 Lot Substitution Project */ end
3532 
3533 BEGIN
3534 	 /*  MMTT management
3535 	  Action    l_insert        L_update        update orginalMMTT      UpdMergeMMTT    InsertNewMMTT
3536 	  -----------------------------------------------------------------------------------------------
3537 	  SPLIT     Y               Y1              N                       N               Y
3538 	  SPLIT     N               Y2              Y                       Y               N
3539 	  LOAD_M    N               Y1              Y                       N               N
3540 	  LOAD_M    N               Y2              Y-Delete                Y               N
3541 	  LOAD_S    N               Y1              Y                       N               N
3542 
3543 	  ****MTLT ****
3544 	  Action    l_insert        L_update        update orginalMTLT      UpdMergeMTLT
3545 	  -----------------------------------------------------------------------------------------------
3546 	  SPLIT     Y               Y1              Y-upd original OR       N
3547 												-ins new/upd orig
3548 	  SPLIT     N               Y2              Y                       Y if MTLT exist
3549 																		OR ins new/upd orig
3550 	  LOAD_M    N               Y1              N                       N
3551 	  LOAD_M    N               Y2              Y                       Y if MTLT exist
3552 																		OR ins new/upd orig
3553 	  LOAD_S    N               Y1              N-not necessary         N
3554 
3555 	  */
3556 
3557    x_return_status  := l_g_ret_sts_success;
3558    mydebug ('In  :' || l_proc_name );
3559    mydebug ('p_action                 = ' || p_action                 );
3560    mydebug ('p_insert                 = ' || p_insert                 );
3561    mydebug ('p_update                 = ' || p_update                 );
3562    mydebug ('p_transaction_header_id  = ' || p_transaction_header_id  );
3563    mydebug ('p_transaction_temp_id    = ' || p_transaction_temp_id    );
3564    mydebug ('p_new_transaction_temp_id= ' || p_new_transaction_temp_id);
3565    mydebug ('p_transaction_temp_id_to_merge = ' || p_transaction_temp_id_to_merge    );
3566    mydebug ('p_inventory_item_id      = ' || p_inventory_item_id  );
3567    mydebug ('p_revision               = ' || p_revision           );
3568    mydebug ('p_suggested_uom          = ' || p_suggested_uom      );
3569    mydebug ('p_primary_uom            = ' || p_primary_uom        );
3570    mydebug ('p_confirmed_uom          = ' || p_confirmed_uom      );
3571    mydebug ('p_confirmed_lots         = ' || p_confirmed_lots  );
3572    mydebug ('p_confirmed_lot_trx_qty  = ' || p_confirmed_lot_trx_qty  );
3573    mydebug ('p_confirmed_sec_qty      = ' || p_confirmed_sec_qty  );
3574    mydebug ('p_confirmed_sec_uom      = ' || p_confirmed_sec_uom  );
3575    mydebug ('p_confirmed_serials      = ' || p_confirmed_serials  );
3576    mydebug ('p_serial_allocated_flag  = ' || p_serial_allocated_flag  );
3577    mydebug ('p_lpn_match              = ' || p_lpn_match              );
3578    mydebug ('p_lpn_match_lpn_id       = ' || p_lpn_match_lpn_id         );
3579    mydebug ('p_exception              = ' || p_exception         );
3580    mydebug ('p_substitute_lots        = ' || p_substitute_lots         );
3581    mydebug ('The value of global l_g_isLotSubstitutionOK is  = ' || l_g_isLotSubstitutionOK);
3582    mydebug ('p_full_lot_allocation    = ' || p_full_lot_allocation); -- Added for 14699845 (Flexible Lot Allocation)
3583    mydebug ('p_fulfillment_base       = ' || p_fulfillment_base);
3584    mydebug ('p_orig_mmtt_txn_uom      = ' || p_orig_mmtt_txn_uom);
3585 
3586   --/* Bug 9448490 Lot Substitution Project */ start
3587    IF     p_lot_controlled = 'Y'
3588 	  AND (   (p_serial_controlled = 'N')
3589 		   OR ((    p_serial_controlled = 'Y'
3590 				AND (   p_serial_allocated_flag = 'N'
3591 					 OR (    p_serial_allocated_flag = 'Y'
3592 						 AND p_full_lot_allocation IN('N', 'P') -- Added for 14699845 (Flexible Lot Allocation)
3593 						)
3594 					)
3595 			   )
3596 			  )
3597 		  )THEN  --BUG12670785
3598 		BEGIN
3599 		proc_decrement_allocated_mtlts(p_transaction_temp_id,
3600 										p_substitute_lots,
3601 										p_confirmed_uom,
3602 										x_return_status);
3603 		EXCEPTION
3604 		WHEN OTHERS THEN
3605 		IF l_return_status_ls <> l_g_ret_sts_success THEN
3606 		mydebug('WMS_TASK_LOAD.proc_process_confirmed_lots - Exception is raised while calling proc_decrement_allocated_mtlts');
3607 		RAISE fnd_api.G_EXC_ERROR;
3608 		END IF;
3609 		END;
3610 
3611 		OPEN cur_ins_mtlt_lot_sub(p_transaction_temp_id);
3612 		LOOP
3613 		FETCH cur_ins_mtlt_lot_sub INTO l_lot_number_ls, l_lot_trx_qty_ls, l_lot_prim_qty_ls, l_lot_sec_qty_ls;
3614 		EXIT WHEN cur_ins_mtlt_lot_sub%NOTFOUND;
3615 		l_g_isLotSubstitutionOK := 1;
3616 		mydebug('Inserting into MTLT - p_transaction_temp_id :' || p_transaction_temp_id);
3617 		mydebug('Inserting into MTLT - l_lot_prim_qty_ls     :' || l_lot_prim_qty_ls);
3618 		mydebug('Inserting into MTLT - l_lot_sec_qty_ls      :' || l_lot_sec_qty_ls);
3619 		mydebug('Inserting into MTLT - l_lot_trx_qty_ls      :' || l_lot_trx_qty_ls);
3620 		mydebug('Inserting into MTLT - l_lot_number_ls       :' || l_lot_number_ls);
3621 		mydebug('Inserting into MTLT - p_inventory_item_id   :' || p_inventory_item_id);
3622 		mydebug('Inserting into MTLT - p_organization_id     :' || p_organization_id);
3623 
3624 		insert_mtlt (
3625 		p_new_temp_id => p_transaction_temp_id
3626 		, p_serial_temp_id  => NULL
3627 		, p_pri_att_qty     => l_lot_prim_qty_ls
3628 		, p_sec_att_qty     => l_lot_sec_qty_ls
3629 		, p_trx_att_qty     => l_lot_trx_qty_ls
3630 		, p_lot_number      => l_lot_number_ls
3631 		, p_item_id         => p_inventory_item_id
3632 		, p_organization_id => p_organization_id
3633 		, x_return_status   => l_return_status_ls) ;
3634 		mydebug('Inserting into MTLT - l_return_status_ls-' || l_return_status_ls);
3635 
3636 		IF l_return_status_ls <> l_g_ret_sts_success THEN
3637 		RAISE fnd_api.G_EXC_ERROR;
3638 		END IF;
3639 		END LOOP;
3640 		CLOSE cur_ins_mtlt_lot_sub;
3641 
3642 		mydebug('The value of  p_action :' || p_action);
3643 		mydebug('The value of  p_insert :' || p_insert);
3644 		mydebug('The value of  l_g_isLotSubstitutionOK :' || l_g_isLotSubstitutionOK);
3645 		mydebug('The value of  p_transaction_temp_id   :' || p_transaction_temp_id);
3646 
3647 IF ((p_action in ('LOAD_SINGLE', 'LOAD_MULTIPLE'))  AND p_insert = 'N' ) THEN --12871057
3648 
3649 	--The following loop is only to print debug messages..
3650 		OPEN cur_mtlts_deleted_ls;
3651 		LOOP
3652 			FETCH cur_mtlts_deleted_ls INTO l_ls_temp_id, l_ls_lot_number, l_ls_lot_prim_qty;
3653 			EXIT WHEN cur_mtlts_deleted_ls%NOTFOUND;
3654 			mydebug('proc_process_confirmed_lots - Deleting the following MTLT record ');
3655 			mydebug('proc_process_confirmed_lots - l_ls_temp_id     :' || l_ls_temp_id);
3656 			mydebug('proc_process_confirmed_lots - l_ls_lot_number  :' || l_ls_lot_number);
3657 			mydebug('proc_process_confirmed_lots - l_ls_lot_prim_qty:' || l_ls_lot_prim_qty);
3658 		END LOOP;
3659 		CLOSE cur_mtlts_deleted_ls;
3660 
3661 
3662 	DELETE FROM mtl_transaction_lots_temp mtlt
3663 	WHERE mtlt.lot_number NOT IN (SELECT NVL(lot_number, '@####') FROM mtl_allocations_gtmp)  --Added NVL as in some cases p_lot_numbers had ':' extra causing insert with null values
3664 	AND mtlt.transaction_temp_id = p_transaction_temp_id;
3665 	--Debug stmts
3666 END IF;
3667 END IF;
3668   --/* Bug 9448490 Lot Substitution Project */ end
3669 
3670    FOR  rec_confirmed_lots_serials  IN cur_confirmed_lots_serials
3671    LOOP
3672 		  mydebug('Group_number         : ' || rec_confirmed_lots_serials.transaction_temp_id);
3673 		  mydebug('lot_number           : ' || rec_confirmed_lots_serials.lot_number);
3674 		  mydebug('Serial_number        : ' || rec_confirmed_lots_serials.serial_number);
3675 		  mydebug('Transaction_quantity : ' || rec_confirmed_lots_serials.transaction_quantity);
3676 		  mydebug('Primary_quantity     : ' || rec_confirmed_lots_serials.primary_quantity);
3677 		  mydebug('suggested_quantity   : ' || rec_confirmed_lots_serials.suggested_quantity);
3678 		  mydebug('Secondary_quantity   : ' || rec_confirmed_lots_serials.Secondary_quantity);
3679 		  -- Get lot record details that is attached to the original p_transaction_temp_id
3680 		  -- Only p_insert = y means a new MMTT is created and therefore new MTLT will have to
3681 		  -- be created.
3682 
3683 	  IF l_prev_lot_number <> rec_confirmed_lots_serials.lot_number
3684 	  THEN
3685 		  l_prev_lot_number := rec_confirmed_lots_serials.lot_number ;
3686 		  IF p_insert = 'Y' or p_update = 'Y2'  THEN
3687 
3688 			 -- we need this only if we ever need to create a new MTLT
3689 			 FOR rec_mtlt_to_copy_from  IN cur_mtlt_to_copy_from
3690 						   (p_lot_number                => rec_confirmed_lots_serials.lot_number,
3691 							p_lot_transaction_temp_id   => p_transaction_temp_id)
3692 			 LOOP
3693 				l_progress    :=  '150';
3694 				mydebug ('In rec_mtlt_to_copy_from cursor' );
3695 				l_rec_mtlt_to_copy_from := rec_mtlt_to_copy_from;
3696 				l_orig_lot_txn_qty      := rec_mtlt_to_copy_from.transaction_quantity;
3697 				l_orig_lot_sec_qty      := rec_mtlt_to_copy_from.secondary_quantity;
3698 				EXIT;
3699 			 END LOOP;
3700 
3701 			 l_progress    :=  '160';
3702 			 mydebug ('l_rec_mtlt_to_copy_from.transaction_temp_id          :' ||l_rec_mtlt_to_copy_from.transaction_temp_id);
3703 			 mydebug ('l_rec_mtlt_to_copy_from.serial_transaction_temp_id   :' ||l_rec_mtlt_to_copy_from.serial_transaction_temp_id);
3704 			 IF  l_rec_mtlt_to_copy_from.transaction_temp_id IS NULL
3705 			 THEN
3706 				l_progress    :=  '170';
3707 				-- lot record attached to the original MMTT should have been found
3708 				RAISE fnd_api.G_EXC_ERROR;
3709 			 END IF;
3710 
3711 		  END IF;
3712 		  l_progress    :=  '175';
3713 		  -- For lot + serial controlled items
3714 		  IF   p_serial_controlled     = 'Y'
3715 		  THEN
3716 
3717 			--BUG12670785 BEGIN
3718 			IF(p_serial_allocated_flag = 'N' AND (check_if_lot_is_substituted(p_substitute_lots,rec_confirmed_lots_serials.lot_number , p_full_lot_allocation))) THEN -- Modified for 14699845 (Flexible Lot Allocation)
3719 				l_mtlt_serial_temp_id := p_transaction_temp_id;
3720 				mydebug ('Inside the if condition printing l_mtlt_serial_temp_id ' || l_mtlt_serial_temp_id);
3721 			ELSE
3722 			--BUG12670785 END
3723 			 l_mtlt_serial_temp_id := l_rec_mtlt_to_copy_from.serial_transaction_temp_id;
3724 			END IF; --BUG12670785
3725 
3726 			 SELECT mtl_material_transactions_s.NEXTVAL
3727 			   INTO l_serial_transaction_temp_id
3728 			   FROM DUAL;
3729 
3730 		  ELSE
3731 			 l_serial_transaction_temp_id   := NULL;
3732 			 l_mtlt_serial_temp_id := NULL;
3733 		  END IF;
3734 
3735 		  mydebug ('l_mtlt_serial_temp_id: ' || l_mtlt_serial_temp_id);
3736 		  mydebug ('l_serial_transaction_temp_id: ' || l_serial_transaction_temp_id);
3737 		  l_progress    :=  '180';
3738 
3739 		  IF(p_fulfillment_base = 'P' OR (p_fulfillment_base = 'S' AND p_confirmed_uom = p_orig_mmtt_txn_uom)) THEN
3740 			  l_calc_lot_txn_qty := l_rec_mtlt_to_copy_from.transaction_quantity - rec_confirmed_lots_serials.suggested_quantity;
3741 		  ELSE
3742 			  l_calc_lot_txn_qty := l_rec_mtlt_to_copy_from.transaction_quantity - inv_convert.inv_um_convert(
3743 																					  item_id       => p_inventory_item_id,
3744 																					  lot_number    => rec_confirmed_lots_serials.lot_number,   -- 16070349
3745 																					  organization_id => p_organization_id,                     -- 16070349
3746 																					  precision     => l_g_decimal_precision,
3747 																					  from_quantity => rec_confirmed_lots_serials.suggested_quantity,
3748 																					  from_unit     => p_confirmed_uom,
3749 																					  to_unit       => p_orig_mmtt_txn_uom,
3750 																					  from_name     => NULL,
3751 																					  to_name       => NULL);
3752 			  IF(l_calc_lot_txn_qty <= 0) THEN
3753 				  l_calc_lot_txn_qty := inv_convert.inv_um_convert(
3754 											  item_id       => p_inventory_item_id,
3755 											  lot_number    => rec_confirmed_lots_serials.lot_number,   -- 16070349
3756 											  organization_id => p_organization_id,                     -- 16070349
3757 											  precision     => l_g_decimal_precision,
3758 											  from_quantity => (l_rec_mtlt_to_copy_from.secondary_quantity - rec_confirmed_lots_serials.secondary_quantity),
3759 											  from_unit     => p_confirmed_sec_uom,
3760 											  to_unit       => p_orig_mmtt_txn_uom,
3761 											  from_name     => NULL,
3762 											  to_name       => NULL);
3763 			  END IF;
3764 		  END IF;
3765 
3766 		  mydebug ('p_insert: ' || p_insert);
3767 		  IF p_insert = 'Y' THEN
3768 			 l_progress    :=  '190';
3769 				mydebug ('l_rec_mtlt_to_copy_from.secondary_quantity    : ' || l_rec_mtlt_to_copy_from.secondary_quantity);
3770 				mydebug ('rec_confirmed_lots_serials.secondary_quantity : ' || rec_confirmed_lots_serials.secondary_quantity);
3771 			 IF  ((l_rec_mtlt_to_copy_from.primary_quantity <= rec_confirmed_lots_serials.primary_quantity AND NVL(p_fulfillment_base, 'P') = 'P') OR
3772 					(l_rec_mtlt_to_copy_from.secondary_quantity <= rec_confirmed_lots_serials.secondary_quantity AND p_fulfillment_base = 'S'))THEN
3773 				 -- lot qty in the selected, in the above cursor, MTLT equals qty that is needed for the new MTLT
3774 				 -- update the MTLT with new temp id  instead of inserting a new and then deleting the old one
3775 				 l_progress    :=  '200';
3776 				 mydebug('l_progress: ' || l_progress );
3777 				 UPDATE mtl_transaction_lots_temp
3778 				 SET    transaction_temp_id        = p_new_transaction_temp_id
3779 					  , transaction_quantity       = rec_confirmed_lots_serials.transaction_quantity
3780 					  , primary_quantity           = rec_confirmed_lots_serials.primary_quantity
3781 					  , secondary_quantity         = rec_confirmed_lots_serials.secondary_quantity
3782 					  , secondary_unit_of_measure  = p_confirmed_sec_uom
3783 					  , serial_transaction_temp_id = l_serial_transaction_temp_id
3784 					  , last_update_date           = SYSDATE
3785 					  , last_updated_by            = p_user_id
3786 				 WHERE  transaction_temp_id        = l_rec_mtlt_to_copy_from.transaction_temp_id
3787 				 AND    lot_number                 = rec_confirmed_lots_serials.lot_number;
3788 				 IF SQL%NOTFOUND THEN
3789 					 RAISE fnd_api.G_EXC_ERROR;
3790 				 END IF;
3791 			 ELSE
3792 				 -- insert a new MTLT
3793 				 l_progress    :=  '210';
3794 				 mydebug('l_progress: ' || l_progress );
3795 				 l_rec_mtlt_to_copy_from.transaction_quantity  := rec_confirmed_lots_serials.transaction_quantity ;
3796 				 l_rec_mtlt_to_copy_from.primary_quantity      := rec_confirmed_lots_serials.primary_quantity ;
3797 				 l_rec_mtlt_to_copy_from.secondary_quantity    := rec_confirmed_lots_serials.secondary_quantity ;
3798 				 l_rec_mtlt_to_copy_from.secondary_unit_of_measure    := p_confirmed_sec_uom;
3799 				 l_rec_mtlt_to_copy_from.created_by            := p_user_id;
3800 				 l_rec_mtlt_to_copy_from.transaction_temp_id   := p_new_transaction_temp_id;
3801 				 -- For lot + serial controlled items
3802 				 l_rec_mtlt_to_copy_from.serial_transaction_temp_id := l_serial_transaction_temp_id;
3803 				 proc_insert_mtlt
3804 					 (p_lot_record                      => l_rec_mtlt_to_copy_from
3805 					 ,x_return_status                   => x_return_status
3806 					 ,x_msg_count                       => x_msg_count
3807 					 ,x_msg_data                        => x_msg_data);
3808 				 mydebug('x_return_status : ' || x_return_status);
3809 				 IF x_return_status <> l_g_ret_sts_success THEN
3810 					 RAISE fnd_api.G_EXC_ERROR;
3811 				 END IF;
3812 				 mydebug('l_progress: ' || l_progress );
3813 				 l_progress    :=  '220';
3814 				 -- If new MTLT is inserted for p_new_transaction_temp_id this means
3815 				 -- the original MTLT still has some qty remaining...so update it.
3816 				 -- the original MTLT need not be adjusted for secondary quantity.
3817 				 -- It is not expected to be populated
3818 				 IF(p_fulfillment_base = 'S') THEN
3819 					UPDATE  mtl_transaction_lots_temp
3820 					SET  transaction_quantity     = l_calc_lot_txn_qty
3821 						,primary_quantity         = primary_quantity - rec_confirmed_lots_serials.primary_quantity
3822 						,secondary_quantity       = secondary_quantity - rec_confirmed_lots_serials.secondary_quantity
3823 						,last_update_date         = SYSDATE
3824 						,last_updated_by          = p_user_id
3825 					WHERE   transaction_temp_id   = p_transaction_temp_id
3826 					AND     lot_number = rec_confirmed_lots_serials.lot_number;
3827 					IF SQL%NOTFOUND THEN
3828 						RAISE fnd_api.G_EXC_ERROR;
3829 					END IF;
3830 				 ELSE
3831 					UPDATE  mtl_transaction_lots_temp
3832 					SET  transaction_quantity     = transaction_quantity - rec_confirmed_lots_serials.suggested_quantity
3833 						,primary_quantity         = primary_quantity - rec_confirmed_lots_serials.primary_quantity
3834 						,secondary_quantity       = NVL(secondary_quantity, rec_confirmed_lots_serials.secondary_quantity) - rec_confirmed_lots_serials.secondary_quantity
3835 						,last_update_date         = SYSDATE
3836 						,last_updated_by          = p_user_id
3837 					WHERE   transaction_temp_id   = p_transaction_temp_id
3838 					AND     lot_number = rec_confirmed_lots_serials.lot_number;
3839 					IF SQL%NOTFOUND THEN
3840 						RAISE fnd_api.G_EXC_ERROR;
3841 					END IF;
3842 				 END IF;
3843 				 mydebug('l_progress: ' || l_progress );
3844 			 END IF;
3845 		  END IF;
3846 
3847 		  l_progress    :=  '230';
3848 		  mydebug ('p_update: ' || p_update);
3849 		  -- p_update = 'Y2' means, p_insert = N which means that current MMTT (in this call)
3850 		  -- was merged with another existing MMTT. In this case, it is possible that existing MMTT already has
3851 		  -- MTLT record for this lot number. If this is the case then update the MTLT otherwise ,
3852 		  -- insert a new MTLT  using l_rec_mtlt_to_copy_from from the above cursor
3853 		  IF p_update = 'Y2'
3854 		  THEN
3855 			 l_progress    :=  '240';
3856 			 mydebug('l_progress: ' || l_progress );
3857 			 UPDATE   mtl_transaction_lots_temp
3858 			 SET      transaction_quantity       = transaction_quantity + rec_confirmed_lots_serials.transaction_quantity
3859 					 ,primary_quantity           = primary_quantity     + rec_confirmed_lots_serials.primary_quantity
3860 					 ,secondary_quantity         = secondary_quantity   + rec_confirmed_lots_serials.secondary_quantity
3861 					 ,last_update_date           = SYSDATE
3862 					 ,last_updated_by            = p_user_id
3863 			 WHERE   transaction_temp_id         = p_transaction_temp_id_to_merge
3864 			 AND     lot_number                  = rec_confirmed_lots_serials.lot_number
3865 			 -- For lot + serial controlled items
3866 			 RETURNING serial_transaction_temp_id INTO l_serial_transaction_temp_id;
3867 
3868 			 mydebug ('returned value - l_serial_transaction_temp_id: ' || l_serial_transaction_temp_id);
3869 			 IF SQL%NOTFOUND THEN
3870 				IF ((l_rec_mtlt_to_copy_from.primary_quantity = rec_confirmed_lots_serials.primary_quantity AND NVL(p_fulfillment_base, 'P') = 'P') OR
3871 					(l_rec_mtlt_to_copy_from.secondary_quantity = rec_confirmed_lots_serials.secondary_quantity AND p_fulfillment_base = 'S')) THEN
3872 					--lot qty in the selected MTLT = qty that is needed for the new MTLT
3873 					--update the MTLT with new temp id  instead of inserting a new and then deleting the old one
3874 					l_progress    :=  '250';
3875 					mydebug('l_progress: ' || l_progress );
3876 					UPDATE mtl_transaction_lots_temp
3877 					SET    transaction_temp_id = p_transaction_temp_id_to_merge --p_new_transaction_temp_id
3878 						 , secondary_quantity  = rec_confirmed_lots_serials.secondary_quantity
3879 						 , secondary_unit_of_measure  = p_confirmed_sec_uom
3880 						   -- For lot + serial controlled items
3881 						 , serial_transaction_temp_id = l_serial_transaction_temp_id
3882 						 , last_update_date    = SYSDATE
3883 						 , last_updated_by     = p_user_id
3884 					WHERE  transaction_temp_id = l_rec_mtlt_to_copy_from.transaction_temp_id
3885 					AND    lot_number = rec_confirmed_lots_serials.lot_number;
3886 					IF SQL%NOTFOUND THEN
3887 						RAISE fnd_api.G_EXC_ERROR;
3888 					END IF;
3889 				ELSE
3890 				   l_progress    :=  '260';
3891 				   mydebug('l_progress: ' || l_progress );
3892 				   l_rec_mtlt_to_copy_from.transaction_quantity := rec_confirmed_lots_serials.transaction_quantity ;
3893 				   l_rec_mtlt_to_copy_from.primary_quantity     := rec_confirmed_lots_serials.primary_quantity ;
3894 				   l_rec_mtlt_to_copy_from.secondary_quantity   := rec_confirmed_lots_serials.secondary_quantity;
3895 				   l_rec_mtlt_to_copy_from.transaction_temp_id  := p_transaction_temp_id_to_merge;
3896 				   -- For lot + serial controlled items
3897 				   l_rec_mtlt_to_copy_from.serial_transaction_temp_id  := l_serial_transaction_temp_id;
3898 				   l_rec_mtlt_to_copy_from.created_by        := p_user_id;
3899 				   proc_insert_mtlt
3900 					   (p_lot_record                      => l_rec_mtlt_to_copy_from
3901 					   ,x_return_status                   => x_return_status
3902 					   ,x_msg_count                       => x_msg_count
3903 					   ,x_msg_data                        => x_msg_data);
3904 				   IF x_return_status <> l_g_ret_sts_success THEN
3905 					   mydebug('x_return_status : ' || x_return_status);
3906 					   RAISE fnd_api.G_EXC_ERROR;
3907 				   END IF;
3908 				   L_progress    :=  '270';
3909 				   mydebug('l_progress: ' || l_progress );
3910 				   -- If new MTLT is inserted for p_transaction_temp_id_to_merge this means
3911 				   -- the original MTLT still has some qty remaining...so update it.
3912 				   IF(p_fulfillment_base = 'S') THEN
3913 							UPDATE  mtl_transaction_lots_temp
3914 							SET     transaction_quantity      = l_calc_lot_txn_qty
3915 									,primary_quantity         = primary_quantity - rec_confirmed_lots_serials.primary_quantity
3916 									,secondary_quantity       = secondary_quantity - rec_confirmed_lots_serials.secondary_quantity
3917 									,last_update_date         = SYSDATE
3918 									,last_updated_by          = p_user_id
3919 							WHERE   transaction_temp_id       = p_transaction_temp_id --  l_rec_mtlt_to_copy_from.transaction_temp_id
3920 							AND     lot_number                = rec_confirmed_lots_serials.lot_number;
3921 							IF SQL%NOTFOUND THEN
3922 									RAISE fnd_api.G_EXC_ERROR;
3923 							END IF;
3924 				   ELSE
3925 						UPDATE  mtl_transaction_lots_temp
3926 						SET     transaction_quantity      = transaction_quantity - rec_confirmed_lots_serials.suggested_quantity
3927 								,primary_quantity         = primary_quantity     - rec_confirmed_lots_serials.primary_quantity
3928 								,secondary_quantity       = NVL(secondary_quantity, rec_confirmed_lots_serials.secondary_quantity) - rec_confirmed_lots_serials.secondary_quantity
3929 								,last_update_date         = SYSDATE
3930 								,last_updated_by          = p_user_id
3931 						WHERE   transaction_temp_id = p_transaction_temp_id --  l_rec_mtlt_to_copy_from.transaction_temp_id
3932 						AND     lot_number          = rec_confirmed_lots_serials.lot_number;
3933 						IF SQL%NOTFOUND THEN
3934 								RAISE fnd_api.G_EXC_ERROR;
3935 						END IF;
3936 				   END IF;
3937 				END IF;
3938 			 ELSE  -- found mtlt for the current lot number attached to p_transaction_temp_id_to_merge
3939 				l_progress    :=  '280';
3940 				mydebug('l_progress: ' || l_progress );
3941 				-- if the quantity in mtlt of orginal p_tranaction_temp_id  is equal to the qty confirmed for this lot
3942 				-- and we merged the current lot qty with an existing mtlt record then we do not need this mtlt.
3943 				-- delete it.
3944 				IF ((l_rec_mtlt_to_copy_from.primary_quantity = rec_confirmed_lots_serials.primary_quantity AND NVL(p_fulfillment_base, 'P') = 'P') OR
3945 					(l_rec_mtlt_to_copy_from.secondary_quantity = rec_confirmed_lots_serials.secondary_quantity AND p_fulfillment_base = 'S'))THEN
3946 				   l_progress    :=  '290';
3947 				   mydebug('l_progress: ' || l_progress );
3948 				   -- If all the qty from the original  MTLT is consumed and merged to the
3949 				   -- p_transaction_temp_id_to_merge we do not need the original MTLT ..delete it
3950 				   DELETE  mtl_transaction_lots_temp
3951 				   WHERE   transaction_temp_id = p_transaction_temp_id -- l_rec_mtlt_to_copy_from.transaction_temp_id
3952 				   AND     lot_number          = rec_confirmed_lots_serials.lot_number;
3953 				   IF SQL%NOTFOUND THEN
3954 						 RAISE fnd_api.G_EXC_ERROR;
3955 				   END IF;
3956 				ELSE
3957 				   -- If all the qty from the original MTLT is not consumed then update the original MTLT
3958 				   -- attached to p_transaction_temp_id .. as selected in the cursor above
3959 				   l_progress    :=  '300';
3960 				   mydebug('l_progress: ' || l_progress );
3961 					IF(p_fulfillment_base = 'S') THEN
3962 						UPDATE  mtl_transaction_lots_temp
3963 						SET     transaction_quantity     = l_calc_lot_txn_qty
3964 								,primary_quantity        = primary_quantity - rec_confirmed_lots_serials.primary_quantity
3965 								,secondary_quantity      = rec_confirmed_lots_serials.secondary_quantity - rec_confirmed_lots_serials.secondary_quantity
3966 								,last_update_date        = SYSDATE
3967 								,last_updated_by         = p_user_id
3968 						WHERE   transaction_temp_id = p_transaction_temp_id -- l_rec_mtlt_to_copy_from.transaction_temp_id
3969 						AND     lot_number          = rec_confirmed_lots_serials.lot_number;
3970 						IF SQL%NOTFOUND THEN
3971 							RAISE fnd_api.G_EXC_ERROR;
3972 						END IF;
3973 					ELSE
3974 						UPDATE  mtl_transaction_lots_temp
3975 						SET     transaction_quantity     = transaction_quantity - rec_confirmed_lots_serials.suggested_Quantity
3976 								,primary_quantity        = primary_quantity - rec_confirmed_lots_serials.primary_quantity
3977 								,secondary_quantity      = NVL(secondary_quantity, rec_confirmed_lots_serials.secondary_quantity) - rec_confirmed_lots_serials.secondary_quantity
3978 								,last_update_date        = SYSDATE
3979 								,last_updated_by         = p_user_id
3980 						WHERE   transaction_temp_id = p_transaction_temp_id -- l_rec_mtlt_to_copy_from.transaction_temp_id
3981 						AND     lot_number          = rec_confirmed_lots_serials.lot_number;
3982 						IF SQL%NOTFOUND THEN
3983 							RAISE fnd_api.G_EXC_ERROR;
3984 						END IF;
3985 					END IF;
3986 				END IF;
3987 			 END IF;
3988 		  END IF;
3989 		  /* Usually for p_update = 'Y1' one does not need to update MTLT since nothing would have changed
3990 			 But, for catch weight enabled items, it is necessary */
3991 		  /* If serials are not allocateed and lpn_match = 1/3 , MSNT records need to be populated
3992 			 so, update serial_transaction_temp_id  mtlt*. MSNT gets created in process_confirmed_serials */
3993 
3994 
3995 		  IF (p_update = 'Y1' AND
3996 			  p_insert = 'N'  AND
3997 			  p_serial_controlled     = 'Y'        AND
3998 			  p_serial_allocated_flag = 'N'        AND
3999 			  ((p_lpn_match  IN (1,3)
4000 			  AND(p_substitute_lots IS NULL OR (p_substitute_lots IS NOT NULL
4001 			  AND check_if_lot_is_substituted(p_substitute_lots, rec_confirmed_lots_serials.lot_number, p_full_lot_allocation)))) OR --HWSNIssue 13860546 -- Modified for 14699845 (Flexible Lot Allocation)
4002 			  ((p_lpn_match = 4 or (nvl(p_lpn_match,0) <=0)) AND check_if_lot_is_substituted(p_substitute_lots, rec_confirmed_lots_serials.lot_number,p_full_lot_allocation)))) --BUG 12670785 only in case of lot substitution for serila items
4003 			  -- Modified for 14699845 (Flexible Lot Allocation)
4004 
4005 		  THEN
4006 			  l_progress    :=  '350';
4007 			  mydebug('l_progress: ' || l_progress );
4008 			  UPDATE   mtl_transaction_lots_temp
4009 			  SET
4010 					  transaction_quantity       = rec_confirmed_lots_serials.transaction_quantity
4011 					 ,primary_quantity           = rec_confirmed_lots_serials.primary_quantity
4012 					 ,secondary_quantity         = rec_confirmed_lots_serials.secondary_quantity
4013 					 ,secondary_unit_of_measure  = p_confirmed_sec_uom
4014 					 ,serial_transaction_temp_id = l_serial_transaction_temp_id
4015 					 ,last_update_date           = SYSDATE
4016 					 ,last_updated_by            = p_user_id
4017 			  WHERE   transaction_temp_id = p_transaction_temp_id
4018 			  AND     lot_number          = rec_confirmed_lots_serials.lot_number;
4019 			  IF SQL%NOTFOUND THEN
4020 				 RAISE fnd_api.G_EXC_ERROR;
4021 			  END IF;
4022 		  ELSE
4023 		  /* Usually for p_update = 'Y1' one does not need to update MTLT since nothing would have changed
4024 			 But, for catch weight enabled items, Overpicking or changed TXN-UOM it is necessary
4025 			 */
4026 		  /* following condition is independent of the above condition in that :
4027  *             for serial controlled items, UOM cannot be different from primary
4028  *             UOM (atleast as of this patchset (11.5.10)) .
4029  *             for lpn_match 1,3 , it cannot be a case of overpick. */
4030 		  IF (p_update = 'Y1' AND
4031 			  p_insert = 'N' ) AND
4032 			  (p_confirmed_sec_qty is NOT NULL   OR
4033 			   p_confirmed_uom <> p_primary_uom  OR
4034 			   p_confirmed_uom <> p_suggested_uom OR --BUG13491033
4035 			   p_exception = 'OVER')
4036 		  THEN
4037 			  l_progress    :=  '360';
4038 			  mydebug('l_progress: ' || l_progress );
4039 			  UPDATE   mtl_transaction_lots_temp
4040 			  SET
4041 					  transaction_quantity       = rec_confirmed_lots_serials.transaction_quantity
4042 					 ,primary_quantity           = rec_confirmed_lots_serials.primary_quantity
4043 					 ,secondary_quantity         = rec_confirmed_lots_serials.secondary_quantity
4044 					 ,secondary_unit_of_measure  = p_confirmed_sec_uom
4045 					 ,last_update_date           = SYSDATE
4046 					 ,last_updated_by            = p_user_id
4047 			  WHERE   transaction_temp_id = p_transaction_temp_id
4048 			  AND     lot_number          = rec_confirmed_lots_serials.lot_number;
4049 			  IF SQL%NOTFOUND THEN
4050 				 RAISE fnd_api.G_EXC_ERROR;
4051 			  END IF;
4052 		  END IF;
4053 		  END IF;
4054 
4055 		  -- For lot + serial controlled items, process serials for the given lot now
4056 		  L_progress    :=  '400';
4057 		  mydebug('l_progress: ' || l_progress );
4058 		  mydebug('rec_confirmed_lots_serials.serial_number: ' || rec_confirmed_lots_serials.serial_number );
4059 		  --IF  rec_confirmed_lots_serials.serial_number IS NOT NULL
4060 		  IF  p_serial_controlled     = 'Y'
4061 		  THEN
4062 			  L_progress    :=  '410';
4063 			  mydebug('l_progress: ' || l_progress );
4064 			  -- update all serial records with the serial_transaction_temp_id to be populated
4065 			  UPDATE  mtl_allocations_gtmp
4066 				 SET  child_transaction_temp_id = l_serial_transaction_temp_id
4067 			   WHERE  lot_number          = rec_confirmed_lots_serials.lot_number
4068 				 AND  transaction_temp_id = rec_confirmed_lots_serials.transaction_temp_id  ;
4069 			  IF SQL%NOTFOUND THEN
4070 				 RAISE fnd_api.G_EXC_ERROR;
4071 			  END IF;
4072 			  L_progress    :=  '420';
4073 			  mydebug('l_progress: ' || l_progress );
4074 
4075 			  proc_process_confirmed_serials
4076 					  (p_action                               =>  p_action
4077 					  ,p_insert                               =>  p_insert
4078 					  ,p_update                               =>  p_update
4079 					  ,p_organization_id                      =>  p_organization_id
4080 					  ,p_user_id                              =>  p_user_id
4081 					  ,p_transaction_header_id                =>  p_transaction_header_id
4082 					  ,p_transaction_temp_id                  =>  p_transaction_temp_id
4083 					  ,p_new_transaction_temp_id              =>  p_new_transaction_temp_id --??l_serial_transaction_temp_id
4084 					  ,p_transaction_temp_id_to_merge         =>  p_transaction_temp_id_to_merge
4085 					  ,p_serial_transaction_temp_id           =>  l_serial_transaction_temp_id
4086 					  ,p_mtlt_serial_temp_id                  =>  l_mtlt_serial_temp_id
4087 					  ,p_inventory_item_id                    =>  p_inventory_item_id
4088 					  ,p_revision                             =>  p_revision
4089 					  ,p_suggested_uom                        =>  p_suggested_uom
4090 					  ,p_confirmed_uom                        =>  p_confirmed_uom
4091 					  ,p_primary_uom                          =>  p_primary_uom
4092 					  ,p_serial_lot_number                    =>  rec_confirmed_lots_serials.lot_number
4093 					  ,p_confirmed_serials                    =>  p_confirmed_serials
4094 					  ,p_serial_allocated_flag                =>  p_serial_allocated_flag
4095 					  ,p_lpn_match                            =>  p_lpn_match
4096 					  ,p_lpn_match_lpn_id                     =>  p_lpn_match_lpn_id
4097 					  ,p_lot_controlled                       =>  p_lot_controlled
4098 					  ,p_serial_controlled                    =>  p_serial_controlled
4099 					  ,x_return_status                        =>  x_return_status
4100 					  ,x_msg_count                            =>  x_msg_count
4101 					  ,x_msg_data                             =>  x_msg_data
4102 					  ,p_substitute_lots			          =>  p_substitute_lots--BUG12670785
4103 					  ,p_full_lot_allocation                  =>  p_full_lot_allocation); -- Added for 14699845 (Flexible Lot Allocation)
4104 			  IF x_return_status <> l_g_ret_sts_success THEN
4105 				  RAISE fnd_api.G_EXC_ERROR;
4106 			  END IF;
4107 		  END IF;
4108 	  END IF; --  l_prev_lot_number <> rec_confirmed_lots_serials.lot_number
4109 	  L_progress    :=  '430';
4110 	  mydebug('l_progress: ' || l_progress );
4111    END LOOP; --rec_confirmed_lots_serials  IN cur_confirmed_lots_serials
4112    mydebug('End ..  ' || l_proc_name);
4113 EXCEPTION
4114    WHEN fnd_api.g_exc_error THEN
4115 		x_return_status  := l_g_ret_sts_error;
4116 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
4117 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
4118 		mydebug('fnd_api.g_exc_error: ' || SQLERRM);
4119 		mydebug('ROLLBACK ' );
4120 		ROLLBACK ;
4121    WHEN fnd_api.g_exc_unexpected_error THEN
4122 		x_return_status  := fnd_api.g_ret_sts_unexp_error;
4123 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
4124 		mydebug('ROLLBACK ' );
4125 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
4126 		mydebug('RAISE fnd_api.g_exc_unexpected_error: ' || SQLERRM);
4127 		mydebug('x_msg_count/Data = ' || x_msg_count || '/'|| x_msg_data);
4128 		ROLLBACK ;
4129    WHEN OTHERS THEN
4130 		x_return_status  := l_g_ret_sts_unexp_error;
4131 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
4132 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
4133 		mydebug('fnd_api.g_exc_error: ' || SQLERRM);
4134 		mydebug('ROLLBACK ' );
4135 		ROLLBACK ;
4136 
4137 END proc_process_confirmed_lots;
4138 
4139 PROCEDURE proc_process_confirmed_serials
4140 			 ( p_action                          IN            VARCHAR2
4141 			  ,p_insert                          IN            VARCHAR2
4142 			  ,p_update                          IN            VARCHAR2
4143 			  ,p_organization_id                 IN            NUMBER
4144 			  ,p_user_id                         IN            NUMBER
4145 			  ,p_transaction_header_id           IN            NUMBER
4146 			  ,p_transaction_temp_id             IN            NUMBER
4147 			  ,p_new_transaction_temp_id         IN            NUMBER
4148 			  ,p_transaction_temp_id_to_merge    IN            NUMBER
4149 			  ,p_serial_transaction_temp_id      IN            NUMBER
4150 			  ,p_mtlt_serial_temp_id             IN            NUMBER
4151 			  ,p_inventory_item_id               IN            NUMBER
4152 			  ,p_revision                        IN            VARCHAR2
4153 			  ,p_suggested_uom                   IN            VARCHAR2
4154 			  ,p_confirmed_uom                   IN            VARCHAR2
4155 			  ,p_primary_uom                     IN            VARCHAR2
4156 			  ,p_serial_lot_number               IN            VARCHAR2
4157 			  ,p_confirmed_serials               IN            VARCHAR2
4158 			  ,p_serial_allocated_flag           IN            VARCHAR2
4159 			  ,p_lpn_match                       IN            NUMBER
4160 			  ,p_lpn_match_lpn_id                IN            NUMBER
4161 			  ,p_lot_controlled                  IN            VARCHAR2  -- Y/N
4162 			  ,p_serial_controlled               IN            VARCHAR2  -- Y/N
4163 			  ,x_return_status                   OUT NOCOPY    VARCHAR2
4164 			  ,x_msg_count                       OUT NOCOPY    NUMBER
4165 			  ,x_msg_data                        OUT NOCOPY    VARCHAR2
4166 			  ,p_substitute_lots		         IN	           VARCHAR2 --for BUG12670785
4167 			  ,p_full_lot_allocation             IN            VARCHAR2) -- Added for 14699845 (Flexible Lot Allocation)
4168 IS
4169    l_proc_name                        VARCHAR2(30) :=  'PROC_PROCESS_CONFIRMED_SERIALS';
4170    l_progress                         VARCHAR2(30) :=  '100';
4171    l_delimiter                        VARCHAR2(30) :=  ':';
4172    l_serial_number                    VARCHAR2(30) :=  NULL;
4173    l_n_msnt_transaction_temp_id       NUMBER;
4174    l_o_msnt_transaction_temp_id       NUMBER;
4175    l_msnt_record                      MTL_SERIAL_NUMBERS_TEMP%ROWTYPE;
4176    m                                  NUMBER := 1;  -- position of delimiter
4177    n                                  NUMBER := 1;  -- Start position for substr or search for delimiter
4178    CURSOR  cur_confirmed_serials  IS
4179    SELECT  transaction_temp_id
4180 		  ,lot_number
4181 		  ,serial_number
4182 		  ,transaction_quantity
4183 		  ,primary_quantity
4184 	 FROM  mtl_allocations_gtmp
4185 	WHERE  NVL(lot_number,'@@') = nvl(p_serial_lot_number,'@@')
4186 	ORDER BY
4187 		   transaction_temp_id
4188 		  --,nvl(lot_number,'@@')
4189 		  ,serial_number;
4190 
4191 BEGIN
4192 	   /*  MMTT management
4193 	  Action    l_insert        L_update        update orginalMMTT      UpdMergeMMTT    InsertNewMMTT
4194 	  -----------------------------------------------------------------------------------------------
4195 	  SPLIT     Y               Y1              N                       N               Y
4196 	  SPLIT     N               Y2              Y                       Y               N
4197 	  LOAD_M    N               Y1              Y                       N               N
4198 	  LOAD_M    N               Y2              Y-Delete                Y               N
4199 	  LOAD_S    N               Y1              Y                       N               N
4200 
4201 	  ****MSNT ****
4202 	  Action    l_insert        L_update        update orginalMSNT
4203 	  -----------------------------------------------------------------------------------------------
4204 	  SPLIT     Y               Y1              Y-set temp_id = new temp_id
4205 	  SPLIT     N               Y2              Y-set temp_id= merge temp_id
4206 	  LOAD_M    N               Y1              N
4207 	  LOAD_M    N               Y2              Y-set temp_id= merge temp_id
4208 	  LOAD_S    N               Y1              N-not necessary
4209 	  */
4210 
4211    mydebug ('In : ' || l_proc_name);
4212    mydebug ('p_action                 = ' || p_action                 );
4213    mydebug ('p_insert                 = ' || p_insert                 );
4214    mydebug ('p_update                 = ' || p_update                 );
4215    mydebug ('p_transaction_header_id  = ' || p_transaction_header_id  );
4216    mydebug ('p_transaction_temp_id    = ' || p_transaction_temp_id    );
4217    mydebug ('p_new_transaction_temp_id= ' || p_new_transaction_temp_id);
4218    mydebug ('p_transaction_temp_id_to_merge = ' || p_transaction_temp_id_to_merge    );
4219    mydebug ('p_serial_transaction_temp_id = ' || p_serial_transaction_temp_id    );
4220    mydebug ('p_mtlt_serial_temp_id = ' || p_mtlt_serial_temp_id    );
4221    mydebug ('p_inventory_item_id      = ' || p_inventory_item_id  );
4222    mydebug ('p_revision               = ' || p_revision           );
4223    mydebug ('p_suggested_uom          = ' || p_suggested_uom      );
4224    mydebug ('p_confirmed_uom          = ' || p_confirmed_uom      );
4225    mydebug ('p_primary_uom            = ' || p_primary_uom        );
4226    mydebug ('p_serial_lot_number      = ' || p_serial_lot_number      );
4227    mydebug ('p_confirmed_serials      = ' || p_confirmed_serials  );
4228    mydebug ('p_serial_allocated_flag  = ' || p_serial_allocated_flag  );
4229    mydebug ('p_lpn_match              = ' || p_lpn_match              );
4230    mydebug ('p_lpn_match_lpn_id       = ' || p_lpn_match_lpn_id         );
4231    mydebug ('p_lot_controlled         = ' || p_lot_controlled         );
4232    mydebug ('p_serial_controlled      = ' || p_serial_controlled         );
4233    mydebug ('The value of new p_substitute_lots      = ' || p_substitute_lots         );
4234 
4235 
4236    x_return_status  := l_g_ret_sts_success;
4237    -- No more serials in the string p_confirmed_serials
4238 	mydebug ('process serials: ' );
4239    IF (p_serial_transaction_temp_id IS NOT NULL ) -- it is a case of lot + serial
4240 												  -- and the call came from process_confirmed_lots
4241    THEN
4242 	   l_n_msnt_transaction_temp_id := p_serial_transaction_temp_id;
4243 	   l_o_msnt_transaction_temp_id := p_mtlt_serial_temp_id;
4244    ELSE
4245 	   l_o_msnt_transaction_temp_id :=p_transaction_temp_id;
4246 	  IF (p_insert = 'Y' ) THEN
4247 		  l_n_msnt_transaction_temp_id := p_new_transaction_temp_id;
4248 		  -- if a new mmtt ininserted then the MSNT
4249 		  -- should be attached to p_new_transaction_temp_id
4250 	  ELSE
4251 		 IF p_update = 'Y2' THEN
4252 			 l_n_msnt_transaction_temp_id := p_transaction_temp_id_to_merge;
4253 			 -- if current task is merged to an existing MMTT then the MSNT
4254 			 -- should be attached to p_transaction_temp_id_to_merge
4255 		 END IF;
4256 		 IF (p_update = 'Y1' ) THEN
4257 			   l_n_msnt_transaction_temp_id := p_transaction_temp_id; -- original MMTT
4258 			   --  and this will be used only if we are inserting MSNTs for
4259 			   -- no allocated serIals
4260 		 END IF;
4261 	  END IF;
4262    END IF;
4263    mydebug ('l_o_msnt_transaction_temp_id      = ' || l_o_msnt_transaction_temp_id         );
4264    mydebug ('l_n_msnt_transaction_temp_id      = ' || l_n_msnt_transaction_temp_id         );
4265 
4266    IF  p_confirmed_serials IS NOT NULL     AND
4267 	   ((p_insert = 'Y'  OR p_update = 'Y2') AND (NOT check_if_lot_is_substituted(p_substitute_lots, p_serial_lot_number , p_full_lot_allocation))) --BUG12670785  -- Modified for 14699845 (Flexible Lot Allocation)
4268 
4269    THEN
4270 	  -- AND ( p_serial_allocated_flag = 'Y'))
4271 	  /* If serials are allocted then MSNT records will be associated with p_transaction_temp_id.
4272 		 If serials are not allocated but confirmed_serials is not null that means Java-UI created MSNT
4273 			records and associated them with p_transaction_temp_id. there fore, for a case of SPLIT
4274 			(leading to Merge or split) these MSNT records have to be moved to the confirmed_mmtt created.
4275 			 identified by l_n_msnt_transaction_temp_id */
4276 	   l_progress    :=  '110';
4277 	   mydebug('l_progress: ' || l_progress );
4278 	   -- update the existing msnt record and  set its transaction-temp_id = new_transaction-temp_id
4279 	   UPDATE mtl_serial_numbers_temp
4280 		  SET transaction_temp_id = l_n_msnt_transaction_temp_id
4281 			, last_update_date    = SYSDATE
4282 			, last_updated_by     = p_user_id
4283 		WHERE transaction_temp_id = l_o_msnt_transaction_temp_id
4284 		  AND fm_serial_number IN
4285 			  (SELECT  serial_number
4286 				 FROM  mtl_allocations_gtmp
4287 				WHERE  NVL(lot_number,'@@') = nvl(p_serial_lot_number,'@@'));
4288 
4289 	   IF SQL%NOTFOUND THEN
4290 		  mydebug('11 msnt not updateed..');
4291 		  RAISE fnd_api.G_EXC_ERROR;
4292 	   ELSE
4293 		  l_progress    :=  '140';
4294 		  mydebug('l_progress: ' || l_progress );
4295 		  UPDATE  MTL_SERIAL_NUMBERS
4296 			 SET  group_mark_id   = l_n_msnt_transaction_temp_id
4297 				 ,last_update_date= SYSDATE
4298 				 ,last_updated_by = p_user_id
4299 		   WHERE  current_organization_id = p_organization_id
4300 			 AND  inventory_item_id       = p_inventory_item_id
4301 			 --AND  group_mark_id           IS NULL
4302 			 AND  serial_number           IN
4303 				 (SELECT  serial_number
4304 					FROM  mtl_allocations_gtmp
4305 				   WHERE  NVL(lot_number,'@@') = nvl(p_serial_lot_number,'@@'));
4306 
4307 		  IF SQL%NOTFOUND THEN
4308 			 RAISE fnd_api.G_EXC_ERROR;
4309 		  END IF;
4310 	   END IF;
4311    ELSE
4312 	  l_progress    :=  '170';
4313 	  mydebug('l_progress: ' || l_progress );
4314 	  mydebug('CComing to the else portion 1  ' || l_progress );
4315 	  ---(p_serial_allocated_flag = 'N' AND for allocated serials but lpn_match 1 or 3
4316 	  IF ( p_serial_allocated_flag = 'N'   AND
4317 		   p_confirmed_serials IS NULL     AND
4318 		   p_lpn_match         IN (1,3) )
4319 		 -- for non-catch weight enabled
4320 		 --( p_confirmed_serials    IS NOT NULL   AND p_serial_allocated_flag = 'N')
4321 		 -- Pick Load page will insert these MSNTs
4322 	  THEN
4323 		 l_progress    :=  '180';
4324 		 mydebug('l_progress: ' || l_progress );
4325 
4326 		 --proc_insert_msnt inserts into MSNT using the data from MSN and also marks MSN
4327 		 proc_insert_msnt (p_transaction_temp_id            => l_n_msnt_transaction_temp_id
4328 						   ,p_organization_id               => p_organization_id
4329 						   ,p_inventory_item_id             => p_inventory_item_id
4330 						   ,p_revision                      => p_revision
4331 						   ,p_confirmed_serials             => p_confirmed_serials
4332 						   ,p_serial_number                 => NULL
4333 						   ,p_lpn_id                        => p_lpn_match_lpn_id -- NULL if lpn_match!=(1,3)
4334 						   ,p_serial_lot_number             => p_serial_lot_number
4335 						   ,p_user_id                       => p_user_id
4336 						   ,x_return_status                 => x_return_status
4337 						   ,x_msg_count                     => x_msg_count
4338 						   ,x_msg_data                      => x_msg_data);
4339 		 IF x_return_status <> l_g_ret_sts_success THEN
4340 			mydebug('x_return_status : ' || x_return_status);
4341 			RAISE fnd_api.G_EXC_ERROR;
4342 		 END IF;
4343 	  END IF;
4344 	  /* added on 05/04/04 : fully consumable lpn where serials are allocated,
4345 	   * java does not pass the confirmed serials string . The following logic takes
4346 	   * care of the same. */
4347 	  IF  p_confirmed_serials IS NULL     AND
4348 		  p_serial_allocated_flag = 'Y'   AND
4349 		  p_lpn_match         IN (1,3)    AND
4350 		 (p_insert = 'Y'  OR  p_update = 'Y2')
4351 	  THEN
4352 		 l_progress    :=  '200';
4353 		 mydebug('l_progress: ' || l_progress );
4354 
4355 		  -- update the existing msnt record and  set its transaction-temp_id = new_transaction-temp_id
4356 		  UPDATE mtl_serial_numbers_temp
4357 			 SET transaction_temp_id = l_n_msnt_transaction_temp_id
4358 			   , last_update_date    = SYSDATE
4359 			   , last_updated_by     = p_user_id
4360 		   WHERE transaction_temp_id = l_o_msnt_transaction_temp_id
4361 			 AND fm_serial_number IN
4362 				 (SELECT  serial_number
4363 					FROM  mtl_serial_numbers msn
4364 						 ,mtl_serial_numbers_temp msnt
4365 				   WHERE  msn.serial_number = msnt.fm_serial_number
4366 					 AND  msnt.transaction_temp_id = l_o_msnt_transaction_temp_id
4367 					 AND  NVL(msn.lot_number,'@@') = nvl(p_serial_lot_number,'@@')
4368 					 AND  msn.lpn_id = p_lpn_match_lpn_id);
4369 
4370 		  IF SQL%NOTFOUND THEN
4371 			 mydebug('msnt not updateed..');
4372 			 RAISE fnd_api.G_EXC_ERROR;
4373 		  ELSE
4374 			 l_progress    :=  '300';
4375 			 mydebug('l_progress: ' || l_progress );
4376 			 UPDATE  MTL_SERIAL_NUMBERS
4377 				SET  group_mark_id   = l_n_msnt_transaction_temp_id
4378 					,last_update_date= SYSDATE
4379 					,last_updated_by = p_user_id
4380 			  WHERE  current_organization_id = p_organization_id
4381 				AND  inventory_item_id       = p_inventory_item_id
4382 				AND  serial_number IN
4383 				 (SELECT  serial_number
4384 					FROM  mtl_serial_numbers msn
4385 						 ,mtl_serial_numbers_temp msnt
4386 				   WHERE  msn.serial_number = msnt.fm_serial_number
4387 					 AND  msnt.transaction_temp_id = l_n_msnt_transaction_temp_id
4388 					 AND  NVL(msn.lot_number,'@@') = nvl(p_serial_lot_number,'@@')
4389 					 AND  msn.lpn_id = p_lpn_match_lpn_id);
4390 
4391 			 IF SQL%NOTFOUND THEN
4392 				RAISE fnd_api.G_EXC_ERROR;
4393 			 END IF;
4394 		  END IF;
4395 	  END IF;
4396 
4397 	  --BUG12670785 adding
4398 	   IF  p_confirmed_serials IS NOT NULL     AND
4399 		  p_serial_allocated_flag = 'N'   AND
4400 		  (p_lpn_match         IN (1,2,3,4) OR (nvl(p_lpn_match,0) <=0))   AND   --Adding <=0 for Loose_match and Non LPN controlled Subs --12871057
4401 		 (p_insert = 'N'  OR  p_update = 'Y1') AND
4402 		 (check_if_lot_is_substituted (p_substitute_lots, p_serial_lot_number, p_full_lot_allocation))  --BUG12670785 -- Modified for 14699845 (Flexible Lot Allocation)
4403 	   THEN
4404 		 l_progress    :=  '200111';
4405 		 mydebug('AMO coming here l_progress: ' || l_progress );
4406 		 /*12871057*/
4407 		 mydebug('l_n_msnt_transaction_temp_id: ' || l_n_msnt_transaction_temp_id );
4408 		 mydebug('p_transaction_temp_id: ' || p_transaction_temp_id );
4409 		 mydebug('p_serial_lot_number: ' || p_serial_lot_number );
4410 		 mydebug('p_lpn_match_lpn_id: ' || p_lpn_match_lpn_id );
4411 		 mydebug('p_lpn_match: ' || p_lpn_match );
4412 		/*12871057*/
4413 
4414 		  -- update the existing msnt record and  set its transaction-temp_id = new_transaction-temp_id
4415 		  UPDATE mtl_serial_numbers_temp
4416 			 SET transaction_temp_id = l_n_msnt_transaction_temp_id
4417 			   , last_update_date    = SYSDATE
4418 			   , last_updated_by     = p_user_id
4419 		   WHERE transaction_temp_id = p_transaction_temp_id
4420 			 AND fm_serial_number IN
4421 				 (SELECT  serial_number
4422 					FROM  mtl_serial_numbers msn
4423 						 ,mtl_serial_numbers_temp msnt
4424 				   WHERE  msn.serial_number = msnt.fm_serial_number
4425 					 AND  msnt.transaction_temp_id = p_transaction_temp_id
4426 					 AND  msn.current_organization_id = p_organization_id	--HWSNIssue 13860546(Performance Issue)
4427 					 AND  NVL(msn.lot_number,'@@') = nvl(p_serial_lot_number,'@@')
4428 					 AND  Nvl(msn.lpn_id,'-999') = Nvl(p_lpn_match_lpn_id ,'-999'));--12871057
4429 
4430 		  IF SQL%NOTFOUND THEN
4431 			 mydebug('msnt not updateed coming to AMO code....');
4432 			 RAISE fnd_api.G_EXC_ERROR;
4433 		  ELSE
4434 			 l_progress    :=  '300111';
4435 			 mydebug('AMO coming here l_progress: ' || l_progress );
4436 			 UPDATE  MTL_SERIAL_NUMBERS
4437 				SET  group_mark_id   = l_n_msnt_transaction_temp_id
4438 					,last_update_date= SYSDATE
4439 					,last_updated_by = p_user_id
4440 			  WHERE  current_organization_id = p_organization_id
4441 				AND  inventory_item_id       = p_inventory_item_id
4442 				AND  serial_number IN
4443 				 (SELECT  serial_number
4444 					FROM  mtl_serial_numbers msn
4445 						 ,mtl_serial_numbers_temp msnt
4446 				   WHERE  msn.serial_number = msnt.fm_serial_number
4447 					 AND  msn.current_organization_id = p_organization_id	--HWSNIssue 13860546(Performance Issue)
4448 					 AND  msnt.transaction_temp_id = l_n_msnt_transaction_temp_id
4449 					 AND  NVL(msn.lot_number,'@@') = nvl(p_serial_lot_number,'@@')
4450 					 AND  Nvl(msn.lpn_id,'-999') = Nvl(p_lpn_match_lpn_id ,'-999'));
4451 
4452 			 IF SQL%NOTFOUND THEN
4453 				RAISE fnd_api.G_EXC_ERROR;
4454 			 END IF;
4455 		  END IF;
4456 	  END IF;
4457 	  --BUG12670785 ending
4458 	END IF;
4459 
4460    mydebug('End of processing of confirmed serials..  ' || l_proc_name);
4461 EXCEPTION
4462    WHEN fnd_api.g_exc_error THEN
4463 		x_return_status  := l_g_ret_sts_error;
4464 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
4465 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
4466 		mydebug('fnd_api.g_exc_error: ' || SQLERRM);
4467 		mydebug('ROLLBACK ' );
4468 		ROLLBACK ;
4469    WHEN fnd_api.g_exc_unexpected_error THEN
4470 		x_return_status  := fnd_api.g_ret_sts_unexp_error;
4471 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
4472 		mydebug('ROLLBACK ' );
4473 		ROLLBACK ;
4474 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
4475 		mydebug('RAISE fnd_api.g_exc_unexpected_error: ' || SQLERRM);
4476 		mydebug('x_msg_count/Data = ' || x_msg_count || '/'|| x_msg_data);
4477    WHEN OTHERS THEN
4478 		x_return_status  := l_g_ret_sts_unexp_error;
4479 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
4480 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
4481 		mydebug('fnd_api.g_exc_error: ' || SQLERRM);
4482 		mydebug('ROLLBACK ' );
4483 		ROLLBACK ;
4484 
4485 END proc_process_confirmed_serials;
4486 
4487 PROCEDURE proc_insert_mtlt
4488 			 ( p_lot_record                      IN            mtl_transaction_lots_temp%ROWTYPE
4489 			  ,x_return_status                   OUT NOCOPY    VARCHAR2
4490 			  ,x_msg_count                       OUT NOCOPY    NUMBER
4491 			  ,x_msg_data                        OUT NOCOPY    VARCHAR2)
4492 IS
4493    l_proc_name                   VARCHAR2(30) :=  'PROC_INSERT_MTLT';
4494    l_progress                    VARCHAR2(30) :=  '100';
4495 BEGIN
4496    mydebug('In.. ' || l_proc_name);
4497    x_return_status  := l_g_ret_sts_success;
4498    INSERT  INTO mtl_transaction_lots_temp
4499 			   (transaction_temp_id
4500 			   ,last_update_date
4501 			   ,last_updated_by
4502 			   ,creation_date
4503 			   ,created_by
4504 			   ,last_update_login
4505 			   ,request_id
4506 			   ,program_application_id
4507 			   ,program_id
4508 			   ,program_update_date
4509 			   ,transaction_quantity
4510 			   ,primary_quantity
4511 			   ,lot_number
4512 			   ,lot_expiration_date
4513 			   ,error_code
4514 			   ,serial_transaction_temp_id
4515 			   ,group_header_id
4516 			   ,put_away_rule_id
4517 			   ,pick_rule_id
4518 			   ,description
4519 			   ,vendor_id
4520 			   ,supplier_lot_number
4521 			   ,territory_code
4522 			   ,origination_date
4523 			   ,date_code
4524 			   ,grade_code
4525 			   ,change_date
4526 			   ,maturity_date
4527 			   ,status_id
4528 			   ,retest_date
4529 			   ,age
4530 			   ,item_size
4531 			   ,color
4532 			   ,volume
4533 			   ,volume_uom
4534 			   ,place_of_origin
4535 			   ,best_by_date
4536 			   ,length
4537 			   ,length_uom
4538 			   ,recycled_content
4539 			   ,thickness
4540 			   ,thickness_uom
4541 			   ,width
4542 			   ,width_uom
4543 			   ,curl_wrinkle_fold
4544 			   ,lot_attribute_category
4545 			   ,c_attribute1
4546 			   ,c_attribute2
4547 			   ,c_attribute3
4548 			   ,c_attribute4
4549 			   ,c_attribute5
4550 			   ,c_attribute6
4551 			   ,c_attribute7
4552 			   ,c_attribute8
4553 			   ,c_attribute9
4554 			   ,c_attribute10
4555 			   ,c_attribute11
4556 			   ,c_attribute12
4557 			   ,c_attribute13
4558 			   ,c_attribute14
4559 			   ,c_attribute15
4560 			   ,c_attribute16
4561 			   ,c_attribute17
4562 			   ,c_attribute18
4563 			   ,c_attribute19
4564 			   ,c_attribute20
4565 			   ,d_attribute1
4566 			   ,d_attribute2
4567 			   ,d_attribute3
4568 			   ,d_attribute4
4569 			   ,d_attribute5
4570 			   ,d_attribute6
4571 			   ,d_attribute7
4572 			   ,d_attribute8
4573 			   ,d_attribute9
4574 			   ,d_attribute10
4575 			   ,n_attribute1
4576 			   ,n_attribute2
4577 			   ,n_attribute3
4578 			   ,n_attribute4
4579 			   ,n_attribute5
4580 			   ,n_attribute6
4581 			   ,n_attribute7
4582 			   ,n_attribute8
4583 			   ,n_attribute9
4584 			   ,n_attribute10
4585 			   ,vendor_name
4586 			   ,sublot_num
4587 			   ,secondary_quantity
4588 			   ,secondary_unit_of_measure
4589 			   ,qc_grade
4590 			   ,reason_code
4591 			   ,product_code
4592 			   ,product_transaction_id
4593 			   ,attribute_category
4594 			   ,attribute1
4595 			   ,attribute2
4596 			   ,attribute3
4597 			   ,attribute4
4598 			   ,attribute5
4599 			   ,attribute6
4600 			   ,attribute7
4601 			   ,attribute8
4602 			   ,attribute9
4603 			   ,attribute10
4604 			   ,attribute11
4605 			   ,attribute12
4606 			   ,attribute13
4607 			   ,attribute14
4608 			   ,attribute15)
4609    VALUES (
4610 				p_lot_record.transaction_temp_id
4611 			   ,p_lot_record.last_update_date
4612 			   ,p_lot_record.last_updated_by
4613 			   ,SYSDATE
4614 			   ,p_lot_record.created_by
4615 			   ,p_lot_record.last_update_login
4616 			   ,p_lot_record.request_id
4617 			   ,p_lot_record.program_application_id
4618 			   ,p_lot_record.program_id
4619 			   ,SYSDATE
4620 			   ,p_lot_record.transaction_quantity
4621 			   ,p_lot_record.primary_quantity
4622 			   ,p_lot_record.lot_number
4623 			   ,p_lot_record.lot_expiration_date
4624 			   ,p_lot_record.error_code
4625 			   ,p_lot_record.serial_transaction_temp_id
4626 			   ,p_lot_record.group_header_id
4627 			   ,p_lot_record.put_away_rule_id
4628 			   ,p_lot_record.pick_rule_id
4629 			   ,p_lot_record.description
4630 			   ,p_lot_record.vendor_id
4631 			   ,p_lot_record.supplier_lot_number
4632 			   ,p_lot_record.territory_code
4633 			   ,p_lot_record.origination_date
4634 			   ,p_lot_record.date_code
4635 			   ,p_lot_record.grade_code
4636 			   ,p_lot_record.change_date
4637 			   ,p_lot_record.maturity_date
4638 			   ,p_lot_record.status_id
4639 			   ,p_lot_record.retest_date
4640 			   ,p_lot_record.age
4641 			   ,p_lot_record.item_size
4642 			   ,p_lot_record.color
4643 			   ,p_lot_record.volume
4644 			   ,p_lot_record.volume_uom
4645 			   ,p_lot_record.place_of_origin
4646 			   ,p_lot_record.best_by_date
4647 			   ,p_lot_record.length
4648 			   ,p_lot_record.length_uom
4649 			   ,p_lot_record.recycled_content
4650 			   ,p_lot_record.thickness
4651 			   ,p_lot_record.thickness_uom
4652 			   ,p_lot_record.width
4653 			   ,p_lot_record.width_uom
4654 			   ,p_lot_record.curl_wrinkle_fold
4655 			   ,p_lot_record.lot_attribute_category
4656 			   ,p_lot_record.c_attribute1
4657 			   ,p_lot_record.c_attribute2
4658 			   ,p_lot_record.c_attribute3
4659 			   ,p_lot_record.c_attribute4
4660 			   ,p_lot_record.c_attribute5
4661 			   ,p_lot_record.c_attribute6
4662 			   ,p_lot_record.c_attribute7
4663 			   ,p_lot_record.c_attribute8
4664 			   ,p_lot_record.c_attribute9
4665 			   ,p_lot_record.c_attribute10
4666 			   ,p_lot_record.c_attribute11
4667 			   ,p_lot_record.c_attribute12
4668 			   ,p_lot_record.c_attribute13
4669 			   ,p_lot_record.c_attribute14
4670 			   ,p_lot_record.c_attribute15
4671 			   ,p_lot_record.c_attribute16
4672 			   ,p_lot_record.c_attribute17
4673 			   ,p_lot_record.c_attribute18
4674 			   ,p_lot_record.c_attribute19
4675 			   ,p_lot_record.c_attribute20
4676 			   ,p_lot_record.d_attribute1
4677 			   ,p_lot_record.d_attribute2
4678 			   ,p_lot_record.d_attribute3
4679 			   ,p_lot_record.d_attribute4
4680 			   ,p_lot_record.d_attribute5
4681 			   ,p_lot_record.d_attribute6
4682 			   ,p_lot_record.d_attribute7
4683 			   ,p_lot_record.d_attribute8
4684 			   ,p_lot_record.d_attribute9
4685 			   ,p_lot_record.d_attribute10
4686 			   ,p_lot_record.n_attribute1
4687 			   ,p_lot_record.n_attribute2
4688 			   ,p_lot_record.n_attribute3
4689 			   ,p_lot_record.n_attribute4
4690 			   ,p_lot_record.n_attribute5
4691 			   ,p_lot_record.n_attribute6
4692 			   ,p_lot_record.n_attribute7
4693 			   ,p_lot_record.n_attribute8
4694 			   ,p_lot_record.n_attribute9
4695 			   ,p_lot_record.n_attribute10
4696 			   ,p_lot_record.vendor_name
4697 			   ,p_lot_record.sublot_num
4698 			   ,p_lot_record.secondary_quantity
4699 			   ,p_lot_record.secondary_unit_of_measure
4700 			   ,p_lot_record.qc_grade
4701 			   ,p_lot_record.reason_code
4702 			   ,p_lot_record.product_code
4703 			   ,p_lot_record.product_transaction_id
4704 			   ,p_lot_record.attribute_category
4705 			   ,p_lot_record.attribute1
4706 			   ,p_lot_record.attribute2
4707 			   ,p_lot_record.attribute3
4708 			   ,p_lot_record.attribute4
4709 			   ,p_lot_record.attribute5
4710 			   ,p_lot_record.attribute6
4711 			   ,p_lot_record.attribute7
4712 			   ,p_lot_record.attribute8
4713 			   ,p_lot_record.attribute9
4714 			   ,p_lot_record.attribute10
4715 			   ,p_lot_record.attribute11
4716 			   ,p_lot_record.attribute12
4717 			   ,p_lot_record.attribute13
4718 			   ,p_lot_record.attribute14
4719 			   ,p_lot_record.attribute15);
4720 
4721 mydebug('l_progress: ' || l_progress );
4722 mydebug('End.. ' || l_proc_name);
4723 EXCEPTION
4724    WHEN fnd_api.g_exc_error THEN
4725 		x_return_status  := l_g_ret_sts_error;
4726 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
4727 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
4728 		mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
4729 		mydebug('ROLLBACK ' );
4730 		ROLLBACK ;
4731    WHEN fnd_api.g_exc_unexpected_error THEN
4732 		x_return_status  := fnd_api.g_ret_sts_unexp_error;
4733 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
4734 		mydebug('ROLLBACK ' );
4735 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
4736 		mydebug('RAISE fnd_api.g_exc_unexpected_error: ' || SQLERRM);
4737 		mydebug('x_msg_count/Data = ' || x_msg_count || '/'|| x_msg_data);
4738 		ROLLBACK ;
4739    WHEN OTHERS THEN
4740 		x_return_status  := l_g_ret_sts_unexp_error;
4741 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
4742 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
4743 		mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
4744 		mydebug('ROLLBACK ' );
4745 		ROLLBACK ;
4746 
4747 END proc_insert_mtlt;
4748 
4749 
4750 PROCEDURE proc_insert_msnt
4751 			  (p_transaction_temp_id             IN          NUMBER
4752 			  ,p_organization_id                 IN          NUMBER
4753 			  ,p_inventory_item_id               IN          NUMBER
4754 			  ,p_revision                        IN          VARCHAR2
4755 			  ,p_confirmed_serials               IN          VARCHAR2
4756 			  ,p_serial_number                   IN          VARCHAR2
4757 			  ,p_lpn_id                          IN          NUMBER
4758 			  ,p_serial_lot_number               IN          VARCHAR2
4759 			  ,p_user_id                         IN          NUMBER
4760 			  ,x_return_status                   OUT NOCOPY  VARCHAR2
4761 	   ,x_msg_count                       OUT NOCOPY  NUMBER
4762 			  ,x_msg_data                        OUT NOCOPY  VARCHAR2)
4763 IS
4764    --p_transaction_temp_id := transaction_temp_id of the new MSNT
4765    l_proc_name                VARCHAR2(30) :=  'PROC_INSERT_MSNT';
4766    l_progress                 VARCHAR2(30) :=  '100';
4767    l_serial_prefix            NUMBER;
4768    l_real_serial_prefix       VARCHAR2(30);
4769    l_serial_numeric_frm       NUMBER;
4770    l_serial_numeric_to        NUMBER;
4771    l_number_of_serial_numbers NUMBER;
4772    l_check                    VARCHAR2(1); -- Bug 14089430
4773 BEGIN
4774 	mydebug('In ..  ' || l_proc_name);
4775 	mydebug('p_transaction_temp_id = ' || p_transaction_temp_id);
4776 	mydebug('p_organization_id     = ' || p_organization_id);
4777 	mydebug('p_inventory_item_id   = ' || p_inventory_item_id);
4778 	mydebug('p_serial_number       = ' || p_serial_number);
4779 	mydebug('p_lpn_id              = ' || p_lpn_id);
4780 	mydebug('p_user_id             = ' || p_user_id);
4781 
4782 	x_return_status  := l_g_ret_sts_success;
4783 	l_progress := '110';
4784 
4785 	-- copied the logic from INV_TRX_UTIL_PUB.INSERT_SER_TRX
4786 	l_real_serial_prefix  := RTRIM(p_serial_number, '0123456789');
4787 	l_serial_numeric_frm  := TO_NUMBER(SUBSTR(p_serial_number, NVL(LENGTH(l_real_serial_prefix), 0) + 1));
4788 	l_serial_numeric_to   := TO_NUMBER(SUBSTR(p_serial_number, NVL(LENGTH(l_real_serial_prefix), 0) + 1));
4789 	l_serial_prefix       := (l_serial_numeric_to - l_serial_numeric_frm) + 1;
4790 
4791 	l_progress    :=  '120';
4792 	mydebug ('l_progress: ' || l_progress );
4793 
4794 		-- Start of code fix for Bug 14089430
4795 	IF p_serial_number IS NULL AND p_lpn_id IS NOT NULL	THEN
4796 	BEGIN
4797 	SELECT '1' INTO l_check FROM DUAL WHERE (SELECT Count(1) FROM mtl_serial_numbers
4798 	WHERE inventory_item_id = p_inventory_item_id
4799 	 AND current_organization_id = p_organization_id
4800 	 AND NVL(lot_number,'@@')         = nvl(p_serial_lot_number,'@@')
4801 	 AND lpn_id = p_lpn_id   AND current_status = 3) = (SELECT Sum(primary_transaction_quantity) FROM mtl_onhand_quantities_detail
4802 														WHERE inventory_item_id = p_inventory_item_id
4803 														AND NVL(lot_number,'@@')         = nvl(p_serial_lot_number,'@@')
4804 														AND lpn_id = p_lpn_id AND organization_id = p_organization_id );
4805 
4806 	mydebug ('No MSN and MOQD mismatch !'  );
4807 
4808 	EXCEPTION
4809 	WHEN No_Data_Found THEN
4810 	mydebug ('ERROR !!! MSN and MOQD mismatch exists.'  );
4811 	fnd_message.set_name('WMS', 'WMS_LOT_SER_VALIDATION_FAIL');
4812 	fnd_message.set_token('ROUTINE', '- MSN and MOQD mismatch error !' );
4813 	fnd_msg_pub.ADD;
4814 	RAISE fnd_api.g_exc_error;
4815 	WHEN OTHERS THEN
4816 	mydebug ('ERROR while checking MSN and MOQD mismatch.'  );
4817 	 RAISE fnd_api.g_exc_unexpected_error;
4818 	END;
4819 	END IF;  -- IF p_serial_number IS NULL AND p_lpn_id IS NOT NULL
4820 	  -- End of code fix for Bug 14089430
4821 
4822 
4823 	-- P_serial_number is null means we are inserting a group of serials either
4824 	-- 1. using LPN_ID passed in or serial_lot_number  passed in (p-confirmed_serials is NULL)
4825 	-- OR 2. using the data from the gtmp table (when p_confirmed_serials is nOT NULL)
4826 	IF p_serial_number   IS NULL
4827 	THEN
4828 	   INSERT  INTO  mtl_serial_numbers_temp
4829 			   (transaction_temp_id
4830 			   ,last_update_date
4831 			   ,last_updated_by
4832 			   ,creation_date
4833 			   ,created_by
4834 			   ,last_update_login
4835 			   ,request_id
4836 			   ,program_application_id
4837 			   ,program_id
4838 			   ,program_update_date
4839 			   ,vendor_serial_number
4840 			   ,vendor_lot_number
4841 			   ,fm_serial_number
4842 			   ,to_serial_number
4843 			   ,serial_prefix
4844 			   ,error_code
4845 			   ,parent_serial_number
4846 			   ,group_header_id
4847 			   ,end_item_unit_number
4848 			   ,serial_attribute_category
4849 			   ,territory_code
4850 			   ,origination_date
4851 			   ,c_attribute1
4852 			   ,c_attribute2
4853 			   ,c_attribute3
4854 			   ,c_attribute4
4855 			   ,c_attribute5
4856 			   ,c_attribute6
4857 			   ,c_attribute7
4858 			   ,c_attribute8
4859 			   ,c_attribute9
4860 			   ,c_attribute10
4861 			   ,c_attribute11
4862 			   ,c_attribute12
4863 			   ,c_attribute13
4864 			   ,c_attribute14
4865 			   ,c_attribute15
4866 			   ,c_attribute16
4867 			   ,c_attribute17
4868 			   ,c_attribute18
4869 			   ,c_attribute19
4870 			   ,c_attribute20
4871 			   ,d_attribute1
4872 			   ,d_attribute2
4873 			   ,d_attribute3
4874 			   ,d_attribute4
4875 			   ,d_attribute5
4876 			   ,d_attribute6
4877 			   ,d_attribute7
4878 			   ,d_attribute8
4879 			   ,d_attribute9
4880 			   ,d_attribute10
4881 			   ,n_attribute1
4882 			   ,n_attribute2
4883 			   ,n_attribute3
4884 			   ,n_attribute4
4885 			   ,n_attribute5
4886 			   ,n_attribute6
4887 			   ,n_attribute7
4888 			   ,n_attribute8
4889 			   ,n_attribute9
4890 			   ,n_attribute10
4891 			   ,status_id
4892 			   ,time_since_new
4893 			   ,cycles_since_new
4894 			   ,time_since_overhaul
4895 			   ,cycles_since_overhaul
4896 			   ,time_since_repair
4897 			   ,cycles_since_repair
4898 			   ,time_since_visit
4899 			   ,cycles_since_visit
4900 			   ,time_since_mark
4901 			   ,cycles_since_mark
4902 			   ,number_of_repairs
4903 			   ,product_code
4904 			   ,product_transaction_id )
4905 		 (SELECT
4906 				p_transaction_temp_id
4907 			   ,SYSDATE
4908 			   ,-1
4909 			   ,SYSDATE
4910 			   ,p_user_id
4911 			   ,msn.last_update_login
4912 			   ,msn.request_id
4913 			   ,msn.program_application_id
4914 			   ,msn.program_id
4915 			   ,msn.program_update_date
4916 			   ,msn.vendor_serial_number
4917 			   ,msn.vendor_lot_number
4918 			   ,msn.serial_number
4919 			   ,msn.serial_number
4920 			   ,NVL(l_serial_prefix, 1)
4921 			   ,NULL -- error code
4922 			   ,msn.parent_serial_number
4923 			   ,NULL --group_header_id
4924 			   ,msn.end_item_unit_number
4925 			   ,msn.serial_attribute_category
4926 			   ,msn.territory_code
4927 			   ,msn.origination_date
4928 			   ,msn.c_attribute1
4929 			   ,msn.c_attribute2
4930 			   ,msn.c_attribute3
4931 			   ,msn.c_attribute4
4932 			   ,msn.c_attribute5
4933 			   ,msn.c_attribute6
4934 			   ,msn.c_attribute7
4935 			   ,msn.c_attribute8
4936 			   ,msn.c_attribute9
4937 			   ,msn.c_attribute10
4938 			   ,msn.c_attribute11
4939 			   ,msn.c_attribute12
4940 			   ,msn.c_attribute13
4941 			   ,msn.c_attribute14
4942 			   ,msn.c_attribute15
4943 			   ,msn.c_attribute16
4944 			   ,msn.c_attribute17
4945 			   ,msn.c_attribute18
4946 			   ,msn.c_attribute19
4947 			   ,msn.c_attribute20
4948 			   ,msn.d_attribute1
4949 			   ,msn.d_attribute2
4950 			   ,msn.d_attribute3
4951 			   ,msn.d_attribute4
4952 			   ,msn.d_attribute5
4953 			   ,msn.d_attribute6
4954 			   ,msn.d_attribute7
4955 			   ,msn.d_attribute8
4956 			   ,msn.d_attribute9
4957 			   ,msn.d_attribute10
4958 			   ,msn.n_attribute1
4959 			   ,msn.n_attribute2
4960 			   ,msn.n_attribute3
4961 			   ,msn.n_attribute4
4962 			   ,msn.n_attribute5
4963 			   ,msn.n_attribute6
4964 			   ,msn.n_attribute7
4965 			   ,msn.n_attribute8
4966 			   ,msn.n_attribute9
4967 			   ,msn.n_attribute10
4968 			   ,msn.status_id
4969 			   ,msn.time_since_new
4970 			   ,msn.cycles_since_new
4971 			   ,msn.time_since_overhaul
4972 			   ,msn.cycles_since_overhaul
4973 			   ,msn.time_since_repair
4974 			   ,msn.cycles_since_repair
4975 			   ,msn.time_since_visit
4976 			   ,msn.cycles_since_visit
4977 			   ,msn.time_since_mark
4978 			   ,msn.cycles_since_mark
4979 			   ,msn.number_of_repairs
4980 			   ,NULL --product_code
4981 			   ,NULL --product_transaction_id
4982 	   FROM    mtl_serial_numbers  msn
4983 	   WHERE   msn.current_organization_id  = p_organization_id
4984 	   AND     msn.inventory_item_id        = p_inventory_item_id
4985 	   AND     lpn_id                       = p_lpn_id
4986 	   AND     NVL(lot_number,'@@')         = nvl(p_serial_lot_number,'@@'));
4987 	   --AND     group_mark_id                IS NULL);
4988 
4989 	   IF SQL%NOTFOUND  THEN --- MSN record not found)
4990 		   RAISE fnd_api.G_EXC_ERROR;
4991 	   END IF;
4992 
4993 	   l_progress := '130';
4994 	   mydebug('l_progress: ' || l_progress );
4995 	   proc_mark_msn (p_group_mark_id             => p_transaction_temp_id
4996 					 ,p_organization_id           => p_organization_id
4997 					 ,p_inventory_item_id         => p_inventory_item_id
4998 					 ,p_revision                  => p_revision
4999 					 ,p_confirmed_serials         => p_confirmed_serials
5000 					 ,p_serial_lot_number         => p_serial_lot_number
5001 					 ,p_serial_number             => p_serial_number
5002 					 ,p_lpn_id                    => p_lpn_id
5003 					 ,p_user_id                   => p_user_id
5004 					 ,x_return_status             => x_return_status
5005 			  ,x_msg_count                 => x_msg_count
5006 					 ,x_msg_data                  => x_msg_data);
5007 	   IF x_return_status <> l_g_ret_sts_success THEN
5008 		   mydebug('x_return_status : ' || x_return_status);
5009 		   RAISE fnd_api.G_EXC_ERROR;
5010 	   END IF;
5011 
5012 	ELSE  -- confirmed_serials are no null so get the serials from mtl-allocations_gtmp table
5013 
5014 	   INSERT  INTO  mtl_serial_numbers_temp
5015 			   (transaction_temp_id
5016 			   ,last_update_date
5017 			   ,last_updated_by
5018 			   ,creation_date
5019 			   ,created_by
5020 			   ,last_update_login
5021 			   ,request_id
5022 			   ,program_application_id
5023 			   ,program_id
5024 			   ,program_update_date
5025 			   ,vendor_serial_number
5026 			   ,vendor_lot_number
5027 			   ,fm_serial_number
5028 			   ,to_serial_number
5029 			   ,serial_prefix
5030 			   ,error_code
5031 			   ,parent_serial_number
5032 			   ,group_header_id
5033 			   ,end_item_unit_number
5034 			   ,serial_attribute_category
5035 			   ,territory_code
5036 			   ,origination_date
5037 			   ,c_attribute1
5038 			   ,c_attribute2
5039 			   ,c_attribute3
5040 			   ,c_attribute4
5041 			   ,c_attribute5
5042 			   ,c_attribute6
5043 			   ,c_attribute7
5044 			   ,c_attribute8
5045 			   ,c_attribute9
5046 			   ,c_attribute10
5047 			   ,c_attribute11
5048 			   ,c_attribute12
5049 			   ,c_attribute13
5050 			   ,c_attribute14
5051 			   ,c_attribute15
5052 			   ,c_attribute16
5053 			   ,c_attribute17
5054 			   ,c_attribute18
5055 			   ,c_attribute19
5056 			   ,c_attribute20
5057 			   ,d_attribute1
5058 			   ,d_attribute2
5059 			   ,d_attribute3
5060 			   ,d_attribute4
5061 			   ,d_attribute5
5062 			   ,d_attribute6
5063 			   ,d_attribute7
5064 			   ,d_attribute8
5065 			   ,d_attribute9
5066 			   ,d_attribute10
5067 			   ,n_attribute1
5068 			   ,n_attribute2
5069 			   ,n_attribute3
5070 			   ,n_attribute4
5071 			   ,n_attribute5
5072 			   ,n_attribute6
5073 			   ,n_attribute7
5074 			   ,n_attribute8
5075 			   ,n_attribute9
5076 			   ,n_attribute10
5077 			   ,status_id
5078 			   ,time_since_new
5079 			   ,cycles_since_new
5080 			   ,time_since_overhaul
5081 			   ,cycles_since_overhaul
5082 			   ,time_since_repair
5083 			   ,cycles_since_repair
5084 			   ,time_since_visit
5085 			   ,cycles_since_visit
5086 			   ,time_since_mark
5087 			   ,cycles_since_mark
5088 			   ,number_of_repairs
5089 			   ,product_code
5090 			   ,product_transaction_id )
5091 		 (SELECT
5092 				p_transaction_temp_id
5093 			   ,SYSDATE
5094 			   ,-1
5095 			   ,SYSDATE
5096 			   ,p_user_id
5097 			   ,msn.last_update_login
5098 			   ,msn.request_id
5099 			   ,msn.program_application_id
5100 			   ,msn.program_id
5101 			   ,msn.program_update_date
5102 			   ,msn.vendor_serial_number
5103 			   ,msn.vendor_lot_number
5104 			   ,msn.serial_number
5105 			   ,msn.serial_number
5106 			   ,NVL(l_serial_prefix, 1)
5107 			   ,NULL -- error code
5108 			   ,msn.parent_serial_number
5109 			   ,NULL --group_header_id
5110 			   ,msn.end_item_unit_number
5111 			   ,msn.serial_attribute_category
5112 			   ,msn.territory_code
5113 			   ,msn.origination_date
5114 			   ,msn.c_attribute1
5115 			   ,msn.c_attribute2
5116 			   ,msn.c_attribute3
5117 			   ,msn.c_attribute4
5118 			   ,msn.c_attribute5
5119 			   ,msn.c_attribute6
5120 			   ,msn.c_attribute7
5121 			   ,msn.c_attribute8
5122 			   ,msn.c_attribute9
5123 			   ,msn.c_attribute10
5124 			   ,msn.c_attribute11
5125 			   ,msn.c_attribute12
5126 			   ,msn.c_attribute13
5127 			   ,msn.c_attribute14
5128 			   ,msn.c_attribute15
5129 			   ,msn.c_attribute16
5130 			   ,msn.c_attribute17
5131 			   ,msn.c_attribute18
5132 			   ,msn.c_attribute19
5133 			   ,msn.c_attribute20
5134 			   ,msn.d_attribute1
5135 			   ,msn.d_attribute2
5136 			   ,msn.d_attribute3
5137 			   ,msn.d_attribute4
5138 			   ,msn.d_attribute5
5139 			   ,msn.d_attribute6
5140 			   ,msn.d_attribute7
5141 			   ,msn.d_attribute8
5142 			   ,msn.d_attribute9
5143 			   ,msn.d_attribute10
5144 			   ,msn.n_attribute1
5145 			   ,msn.n_attribute2
5146 			   ,msn.n_attribute3
5147 			   ,msn.n_attribute4
5148 			   ,msn.n_attribute5
5149 			   ,msn.n_attribute6
5150 			   ,msn.n_attribute7
5151 			   ,msn.n_attribute8
5152 			   ,msn.n_attribute9
5153 			   ,msn.n_attribute10
5154 			   ,msn.status_id
5155 			   ,msn.time_since_new
5156 			   ,msn.cycles_since_new
5157 			   ,msn.time_since_overhaul
5158 			   ,msn.cycles_since_overhaul
5159 			   ,msn.time_since_repair
5160 			   ,msn.cycles_since_repair
5161 			   ,msn.time_since_visit
5162 			   ,msn.cycles_since_visit
5163 			   ,msn.time_since_mark
5164 			   ,msn.cycles_since_mark
5165 			   ,msn.number_of_repairs
5166 			   ,NULL --product_code
5167 			   ,NULL --product_transaction_id
5168 		  FROM  mtl_serial_numbers  msn
5169 		  WHERE msn.current_organization_id  = p_organization_id
5170 		  AND   msn.inventory_item_id        = p_inventory_item_id
5171 		  AND   nvl(lpn_id,0)                = nvl(p_lpn_id,0)
5172 		  AND   NVL(lot_number,'@@')         = nvl(p_serial_lot_number,'@@')
5173 		  --AND   group_mark_id                IS NULL
5174 		  AND   msn.serial_number  IN
5175 			   (SELECT serial_number
5176 				  FROM mtl_allocations_gtmp
5177 				 WHERE NVL(lot_number,'@@')         = nvl(p_serial_lot_number,'@@'))
5178 			  );
5179 		  IF SQL%NOTFOUND  THEN --- MSN record not found)
5180 			  RAISE fnd_api.G_EXC_ERROR;
5181 		  END IF;
5182 		  l_progress := '130';
5183 		  mydebug('l_progress: ' || l_progress );
5184 		  proc_mark_msn (p_group_mark_id             => p_transaction_temp_id
5185 						,p_organization_id           => p_organization_id
5186 						,p_inventory_item_id         => p_inventory_item_id
5187 						,p_revision                  => p_revision
5188 						,p_confirmed_serials         => p_confirmed_serials
5189 						,p_serial_lot_number         => p_serial_lot_number
5190 						,p_serial_number             => p_serial_number
5191 						,p_lpn_id                    => p_lpn_id
5192 						,p_user_id                   => p_user_id
5193 						,x_return_status             => x_return_status
5194 				 ,x_msg_count                 => x_msg_count
5195 						,x_msg_data                  => x_msg_data);
5196 		  IF x_return_status <> l_g_ret_sts_success THEN
5197 			  mydebug('x_return_status : ' || x_return_status);
5198 			  RAISE fnd_api.G_EXC_ERROR;
5199 		  END IF;
5200 	END IF;
5201 
5202 	mydebug('End ..  ' || l_proc_name);
5203 EXCEPTION
5204    WHEN fnd_api.g_exc_error THEN
5205 		x_return_status  := l_g_ret_sts_error;
5206 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
5207 		mydebug('ROLLBACK ' );
5208 		ROLLBACK ;
5209 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
5210 		mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
5211    WHEN fnd_api.g_exc_unexpected_error THEN
5212 		x_return_status  := fnd_api.g_ret_sts_unexp_error;
5213 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
5214 		mydebug('ROLLBACK ' );
5215 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
5216 		mydebug('RAISE fnd_api.g_exc_unexpected_error: ' || SQLERRM);
5217 		mydebug('x_msg_count/Data = ' || x_msg_count || '/'|| x_msg_data);
5218 		ROLLBACK ;
5219    WHEN OTHERS THEN
5220 		x_return_status  := l_g_ret_sts_unexp_error;
5221 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
5222 		mydebug('ROLLBACK ' );
5223 		ROLLBACK ;
5224 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
5225 		mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
5226 
5227 END proc_insert_msnt ;
5228 
5229 PROCEDURE proc_mark_msn
5230 			  (p_group_mark_id                   IN          NUMBER
5231 			  ,p_organization_id                 IN          NUMBER
5232 			  ,p_inventory_item_id               IN          NUMBER
5233 			  ,p_Revision                        IN          VARCHAR2
5234 			  ,p_confirmed_serials               IN          VARCHAR2
5235 			  ,p_serial_lot_number               IN          VARCHAR2
5236 			  ,p_serial_number                   IN          VARCHAR2
5237 			  ,p_lpn_id                          IN          NUMBER
5238 			  ,p_user_id                         IN          NUMBER
5239 			  ,x_return_status                   OUT NOCOPY  VARCHAR2
5240 	   ,x_msg_count                       OUT NOCOPY  NUMBER
5241 			  ,x_msg_data                        OUT NOCOPY  VARCHAR2)
5242 IS
5243    --p_transaction_temp_id := transaction_temp_id of the new MSNT
5244    l_proc_name                   VARCHAR2(30) :=  'PROC_MARK_MSN';
5245    l_progress                    VARCHAR2(30) :=  '100';
5246 BEGIN
5247 	mydebug('In ..  ' || l_proc_name);
5248 	mydebug('p_group_mark_id     : ' || p_group_mark_id);
5249 	mydebug('p_organization_id   : ' || p_organization_id);
5250 	mydebug('p_inventory_item_id : ' || p_inventory_item_id);
5251 	mydebug('p_serial_number     : ' || p_serial_number);
5252 	mydebug('p_lpn_id            : ' || p_lpn_id );
5253 	mydebug('p_user_id           : ' || p_user_id);
5254 
5255 	x_return_status  := l_g_ret_sts_success;
5256 	l_progress := '110';
5257 	mydebug('l_progress: ' || l_progress );
5258 	IF p_serial_number IS NULL
5259 	THEN
5260 	   l_progress := '200';
5261 	   mydebug('l_progress: ' || l_progress );
5262 	   UPDATE  MTL_SERIAL_NUMBERS
5263 	   SET     group_mark_id   = p_group_mark_id
5264 			 , last_updated_by = p_user_id
5265 	   WHERE   current_organization_id     = p_organization_id
5266 	   AND     inventory_item_id           = p_inventory_item_id
5267 	   AND     lpn_id                      = p_lpn_id ;
5268 	   --AND     group_mark_id               IS NULL;
5269 
5270 	   IF SQL%NOTFOUND THEN
5271 		   fnd_message.set_name('WMS', 'WMS_ERROR_MARKING_SERIAL'); --NEWMSG
5272 		   -- "Error reserving Serial Number/s"
5273 		   fnd_msg_pub.ADD;
5274 		  RAISE fnd_api.G_EXC_ERROR;
5275 	   END IF;
5276 	ELSE
5277 	   l_progress := '300';
5278 	   mydebug('l_progress: ' || l_progress );
5279 	   UPDATE  MTL_SERIAL_NUMBERS  msn
5280 		  SET  group_mark_id   = p_group_mark_id
5281 			 , last_updated_by = p_user_id
5282 		WHERE  msn.current_organization_id  = p_organization_id
5283 		  AND  msn.inventory_item_id        = p_inventory_item_id
5284 		  AND  nvl(lpn_id,0)                = nvl(p_lpn_id,0)
5285 		  AND  NVL(lot_number,'@@')         = nvl(p_serial_lot_number,'@@')
5286 		  --AND  group_mark_id                IS NULL
5287 		  AND  msn.serial_number  IN
5288 			   (SELECT serial_number
5289 				  FROM mtl_allocations_gtmp
5290 				 WHERE NVL(lot_number,'@@') = nvl(p_serial_lot_number,'@@'));
5291 
5292 	   IF SQL%NOTFOUND THEN
5293 		   fnd_message.set_name('WMS', 'WMS_ERROR_MARKING_SERIAL'); --NEWMSG
5294 		   -- "Error reserving Serial Number/s"
5295 		   fnd_msg_pub.ADD;
5296 		  RAISE fnd_api.G_EXC_ERROR;
5297 	   END IF;
5298 	END IF;
5299 
5300 	mydebug('End ..  ' || l_proc_name);
5301 EXCEPTION
5302    WHEN fnd_api.g_exc_error THEN
5303 		x_return_status  := l_g_ret_sts_error;
5304 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
5305 		mydebug('ROLLBACK ' );
5306 		ROLLBACK ;
5307 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
5308 		mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
5309    WHEN fnd_api.g_exc_unexpected_error THEN
5310 		x_return_status  := fnd_api.g_ret_sts_unexp_error;
5311 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
5312 		mydebug('ROLLBACK ' );
5313 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
5314 		mydebug('RAISE fnd_api.g_exc_unexpected_error: ' || SQLERRM);
5315 		mydebug('x_msg_count/Data = ' || x_msg_count || '/'|| x_msg_data);
5316 		ROLLBACK ;
5317    WHEN OTHERS THEN
5318 		x_return_status  := l_g_ret_sts_unexp_error;
5319 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
5320 		mydebug('ROLLBACK ' );
5321 		ROLLBACK ;
5322 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
5323 		mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
5324 
5325 END  proc_mark_msn ;
5326 
5327 PROCEDURE proc_unmark_msn (p_group_mark_id                IN          NUMBER
5328 						,p_organization_id                IN          NUMBER
5329 						,p_inventory_item_id              IN          NUMBER
5330 						,p_Revision                       IN          VARCHAR2
5331 						,p_serial_number                  IN          VARCHAR2
5332 						,p_lpn_id                         IN          NUMBER
5333 						,p_user_id                        IN          NUMBER
5334 						,x_return_status                  OUT NOCOPY  VARCHAR2
5335 				 ,x_msg_count                      OUT NOCOPY  NUMBER
5336 						,x_msg_data                       OUT NOCOPY  VARCHAR2)
5337 IS
5338    --p_transaction_temp_id := transaction_temp_id of the new MSNT
5339    l_proc_name                   VARCHAR2(30) :=  'PROC_UNMARK_MSN';
5340    l_progress                    VARCHAR2(30) :=  '100';
5341 BEGIN
5342 	mydebug('In ..  ' || l_proc_name);
5343 	mydebug('p_group_mark_id     : ' || p_group_mark_id);
5344 	mydebug('p_organization_id   : ' || p_organization_id);
5345 	mydebug('p_inventory_item_id : ' || p_inventory_item_id);
5346 	mydebug('p_serial_number     : ' || p_serial_number);
5347 	mydebug('p_lpn_id            : ' || p_lpn_id );
5348 	mydebug('p_user_id           : ' || p_user_id);
5349 
5350 	x_return_status  := l_g_ret_sts_success;
5351 	l_progress := '110';
5352 	mydebug('l_progress: ' || l_progress );
5353 	UPDATE  MTL_SERIAL_NUMBERS
5354 	SET     group_mark_id   = p_group_mark_id
5355 		  , last_updated_by = p_user_id
5356 	WHERE   current_organization_id     = p_organization_id
5357 	AND     inventory_item_id           = p_inventory_item_id
5358 	AND     nvl(lpn_id,0)               = nvl(p_lpn_id,0)
5359 	AND     DECODE(p_serial_number,NULL,'@@',serial_number) = nvl(p_serial_number,'@@')  ;
5360 	--AND     group_mark_id  IS NULL;
5361 
5362 	IF SQL%NOTFOUND THEN
5363 	   RAISE fnd_api.G_EXC_ERROR;
5364 	END IF;
5365 
5366 	mydebug('End ..  ' || l_proc_name);
5367 EXCEPTION
5368    WHEN fnd_api.g_exc_error THEN
5369 		x_return_status  := l_g_ret_sts_error;
5370 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
5371 		mydebug('ROLLBACK ' );
5372 		ROLLBACK ;
5373 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
5374 		mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
5375    WHEN fnd_api.g_exc_unexpected_error THEN
5376 		x_return_status  := fnd_api.g_ret_sts_unexp_error;
5377 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
5378 		mydebug('ROLLBACK ' );
5379 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
5380 		mydebug('RAISE fnd_api.g_exc_unexpected_error: ' || SQLERRM);
5381 		mydebug('x_msg_count/Data = ' || x_msg_count || '/'|| x_msg_data);
5382 		ROLLBACK ;
5383    WHEN OTHERS THEN
5384 		x_return_status  := l_g_ret_sts_unexp_error;
5385 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
5386 		mydebug('ROLLBACK ' );
5387 		ROLLBACK ;
5388 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
5389 		mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
5390 
5391 END  proc_unmark_msn ;
5392 
5393 /* WHen F2 is pressed, the MMTT/MTLT/MSNT that was split needs to be rolled back so that,
5394  * task is in its original state that is before it was split. Sice the split commits the
5395  * newly inserted/updated details, the rollback has to be done manually.
5396 */
5397 PROCEDURE process_F2(
5398 			   p_action                 IN            VARCHAR2 -- NULL, CMS
5399 			  ,p_organization_id        IN            NUMBER
5400 			  ,p_user_id                IN            NUMBER
5401 			  ,p_employee_id            IN            NUMBER
5402 			  ,p_transaction_header_id  IN            NUMBER
5403 			  ,p_transaction_temp_id    IN            NUMBER
5404 			  ,p_original_sub           IN            VARCHAR2
5405 			  ,p_original_locator_id    IN            NUMBER
5406 			  ,p_lot_controlled         IN            VARCHAR2  -- Y/N
5407 			  ,p_serial_controlled      IN            VARCHAR2  -- Y/N
5408 			  ,p_serial_allocated_flag  IN            VARCHAR2 -- Y/N
5409 			  ,p_suggested_uom          IN            VARCHAR2  -- original allocation UOM
5410 			  ,p_start_over             IN            VARCHAR2   -- Y/N  start_over
5411 			  ,p_retain_task            IN            VARCHAR2 -- Y/N for bug 4310093
5412 			  ,x_start_over_taskno      OUT NOCOPY    NUMBER   -- start_over task
5413 			  ,x_return_status          OUT NOCOPY    VARCHAR2
5414 			  ,x_msg_count              OUT NOCOPY    NUMBER
5415 			  ,x_msg_data               OUT NOCOPY    VARCHAR2)
5416 IS
5417    --PRAGMA AUTONOMOUS_TRANSACTION;
5418 
5419    l_proc_name                   VARCHAR2(30) :=  'PROCESS_F2';
5420    l_progress                    VARCHAR2(30) :=  '100';
5421    l_debug                       NUMBER       :=  NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
5422    l_new_transaction_temp_id     NUMBER       :=  NULL;
5423    l_serial_transaction_temp_id  NUMBER       :=  NULL;
5424    l_transaction_temp_id         NUMBER       :=  NULL;
5425    l_original_sub_loc            VARCHAR2(1)  :=  'N';
5426    l_rec_mtlt_to_copy_from       mtl_transaction_lots_temp%ROWTYPE;
5427    l_suggested_mmtt_qty          NUMBER       := 0;
5428    l_suggested_mtlt_qty          NUMBER       := 0;
5429    -- bug #4141928 INV CONV
5430    l_suggested_mmtt_sec_qty          NUMBER       := 0;
5431    l_suggested_mtlt_sec_qty          NUMBER       := 0;
5432 
5433    l_op_msnt_to_delete           NUMBER       := 0;
5434    l_start_over_task             NUMBER       := 0;
5435    l_parent_posting_flag         VARCHAR2(1); -- Bug#4185621
5436 
5437    -- Added for 14699845 (Flexible Lot Allocation)
5438    l_lot_alloc_exist             VARCHAR2(1):= 'N';
5439    l_ret_sts                     VARCHAR2(1);
5440    l_fulfillment_base            VARCHAR2(1):= 'P';
5441    -- Added for 14699845 (Flexible Lot Allocation)
5442 
5443    /* cur_mmtt1
5444 	 -- Group MMTTs(including p_transaction_temp_id) with  p_transaction_header_id
5445 	 -- by inventory_item_id,subinventory_code,locator_id.
5446 	 -- update one of the MMTTs(min temp_id) from each group and delete rest from that group */
5447 
5448    CURSOR  cur_mmtt1
5449    IS
5450 	  SELECT   sum(primary_quantity) mmtt_primary_quantity
5451 	  -- bug #4141928 INV CONV
5452 	  , sum(secondary_transaction_quantity) mmtt_secondary_quantity
5453 	  , COUNT(*) mmtt_group_count
5454 	  , MIN(transaction_temp_id) group_temp_id
5455 	  , MIN(parent_line_id) parent_line_id -- Bug#4185621
5456 			 , inventory_item_id
5457 			 , revision
5458 			 , subinventory_code
5459 			 , locator_id
5460 			 , item_primary_uom_code
5461 			 , SECONDARY_UOM_CODE --BUG12622871LSC
5462 			 , fulfillment_base
5463 	  FROM   mtl_material_transactions_temp mmtt
5464 	  WHERE  mmtt.transaction_header_id  = p_transaction_header_id
5465 	  GROUP BY
5466 			  inventory_item_id
5467 			 ,revision
5468 			 ,subinventory_code
5469 			 ,locator_id
5470 			 ,item_primary_uom_code
5471 			 ,SECONDARY_UOM_CODE --BUG12622871LSC
5472 			 ,fulfillment_base;
5473 
5474    CURSOR  cur_mtlt1       ( p_subinventory_code   VARCHAR2
5475 							,p_locator_id          NUMBER
5476 							,p_uom_code            VARCHAR2
5477 							,p_inventory_item_id   VARCHAR2
5478 							,p_revision            VARCHAR2
5479 							,p_group_temp_id       NUMBER)
5480    IS
5481 	  SELECT  sum(mtlt.primary_quantity)      group_lot_primary_quantity
5482 	  -- bug #4141928 INV CONV
5483 	  , sum(mtlt.secondary_quantity)          group_lot_secondary_quantity
5484 	  ,COUNT(*)                               group_lot_count
5485 	  ,MIN(mtlt.transaction_temp_id)   group_lot_temp_id
5486 			 ,mtlt.lot_number
5487 	  FROM    mtl_transaction_lots_temp mtlt
5488 			, mtl_material_transactions_temp mmtt
5489 	  WHERE   mmtt.transaction_header_id =  p_transaction_header_id
5490 	  AND     mmtt.transaction_temp_id   =  mtlt.transaction_temp_id
5491 	  AND     mmtt.subinventory_code     =  p_subinventory_code
5492 	  AND     mmtt.locator_id            =  p_locator_id
5493 	  AND     mmtt.item_primary_uom_code =  p_uom_code
5494 	  AND     mmtt.inventory_item_id     =  p_inventory_item_id
5495 	  AND     nvl(mmtt.revision,'@@')    =  nvl(p_revision,'@@')
5496 	  GROUP BY
5497 			  mtlt.lot_number;
5498 
5499    CURSOR cur_msnt_to_delete ( p_rec_mmtt1_subinventory_code   VARCHAR2
5500 							  ,p_rec_mmtt1_locator_id          NUMBER
5501 							  ,p_rec_mmtt1_item_primary_uom    VARCHAR2
5502 							  ,p_rec_mmtt1_inventory_item_id   NUMBER
5503 							  ,p_rec_mmtt1_revision            VARCHAR2)
5504    IS
5505 			  SELECT   msnt.transaction_temp_id
5506 					  ,msnt.fm_serial_number
5507 					  ,mmtt.organization_id
5508 					  ,mmtt.inventory_item_id
5509 					  ,msnt.creation_date
5510 				FROM   mtl_serial_numbers_temp msnt
5511 					  ,mtl_material_transactions_temp mmtt
5512 			   WHERE   mmtt.transaction_header_id  = p_transaction_header_id
5513 				 AND   mmtt.transaction_temp_id   =  msnt.transaction_temp_id
5514 				 AND   mmtt.subinventory_code     =  p_rec_mmtt1_subinventory_code
5515 				 AND   mmtt.locator_id            =  p_rec_mmtt1_locator_id
5516 				 AND   mmtt.item_primary_uom_code =  p_rec_mmtt1_item_primary_uom
5517 				 AND   mmtt.inventory_item_id     =  p_rec_mmtt1_inventory_item_id
5518 				 AND   nvl(mmtt.revision,'@@')    =  nvl(p_rec_mmtt1_revision,'@@')
5519 			   ORDER BY msnt.creation_date DESC;
5520 
5521    CURSOR cur_msnt_to_delete_LS (p_serial_transaction_temp_id    NUMBER)
5522    IS
5523 			  SELECT   msnt.transaction_temp_id
5524 					  ,msnt.fm_serial_number
5525 					  ,msnt.creation_date
5526 				FROM   mtl_serial_numbers_temp msnt
5527 			   WHERE   msnt.transaction_temp_id   =  p_serial_transaction_temp_id
5528 			   ORDER BY msnt.creation_date DESC;
5529 BEGIN
5530 	mydebug('In ..  ' || l_proc_name);
5531 	x_return_status  := l_g_ret_sts_success;
5532 	g_debug := l_debug;
5533 
5534 	mydebug ('p_action                 = ' || p_action);
5535 	mydebug ('p_organization_id        = ' || p_organization_id);
5536 	mydebug ('p_user_id                = ' || p_user_id);
5537 	mydebug ('p_employee_id            = ' || p_employee_id);
5538 	mydebug ('p_transaction_header_id  = ' || p_transaction_header_id);
5539 	mydebug ('p_transaction_temp_id    = ' || p_transaction_temp_id);
5540 	mydebug ('p_original_sub           = ' || p_original_sub);
5541 	mydebug ('p_original_locator_id    = ' || p_original_locator_id);
5542 	mydebug ('p_lot_controlled         = ' || p_lot_controlled);
5543 	mydebug ('p_serial_controlled      = ' || p_serial_controlled);
5544 	mydebug ('p_serial_allocated_flag  = ' || p_serial_allocated_flag);
5545 	mydebug ('p_suggested_uom          = ' || p_suggested_uom        );
5546 
5547 	l_progress := 110;
5548 	mydebug('l_progress =  ' || l_progress);
5549 	proc_device_call (
5550 			 p_action                 => p_action
5551 		   , p_employee_id            => p_employee_id
5552 		   , p_transaction_temp_id    => p_transaction_temp_id
5553 		   , x_return_status          => x_return_status
5554 		   , x_msg_count              => x_msg_count
5555 		   , x_msg_data               => x_msg_data );
5556 	IF x_return_status <> l_g_ret_sts_success THEN
5557 	   mydebug('x_return_status : ' || x_return_status);
5558 	   --RAISE fnd_api.G_EXC_ERROR; punnet's request
5559 	END IF;
5560 
5561 	l_progress := 200;
5562 	mydebug('l_progress =  ' || l_progress);
5563 
5564   /*{{
5565  *  If user had not pressed start over button we would reset the status of task
5566  *  as done before.All task would return to pending wdd would be deleted.
5567  *}}
5568  */
5569 
5570   IF p_start_over ='N' and p_retain_task='N' THEN --bug 4310093
5571    mydebug('viks start_over button not pressed:');
5572 
5573 	proc_reset_task_status(
5574 			   p_action                 => p_action
5575 			  ,p_organization_id        => p_organization_id
5576 			  ,p_user_id                => p_user_id
5577 			  ,p_employee_id            => p_employee_id
5578 			  ,p_transaction_header_id  => p_transaction_header_id
5579 			  ,p_transaction_temp_id    => p_transaction_temp_id
5580 			  ,x_return_status          => x_return_status
5581 			  ,x_msg_count              => x_msg_count
5582 			  ,x_msg_data               => x_msg_data);
5583 	IF x_return_status <> l_g_ret_sts_success THEN
5584 		  RAISE fnd_api.G_EXC_ERROR;
5585 	END IF;
5586   END IF;
5587 
5588 	l_progress := 300;
5589 	IF l_debug = 1 THEN mydebug('l_progress =  ' || l_progress);  END IF;
5590 	proc_process_cancelled_MOLs (
5591 			   p_organization_id        => p_organization_id
5592 			  ,p_user_id                => p_user_id
5593 			  ,p_transaction_header_id  => p_transaction_header_id
5594 			  ,p_transaction_temp_id    => p_transaction_temp_id
5595 			  ,x_return_status          => x_return_status
5596 			  ,x_msg_count              => x_msg_count
5597 			  ,x_msg_data               => x_msg_data);
5598 	IF x_return_status <> l_g_ret_sts_success THEN
5599 	   mydebug('x_return_status : ' || x_return_status);
5600 	   RAISE fnd_api.G_EXC_ERROR;
5601 	END IF;
5602 
5603 	IF P_ACTION is not NULL AND p_action = 'CMS' THEN
5604 		COMMIT;
5605 		RETURN;
5606 	END IF;
5607 
5608 	-- Reset Lpn context of content_lpn_ids and transfer_lpn_ids
5609 	l_progress := 400;
5610 	IF l_debug = 1 THEN mydebug('l_progress =  ' || l_progress);  END IF;
5611 	IF P_ACTION is NULL OR p_action <> 'CMS' THEN
5612 	   proc_reset_lpn_context(
5613 			   p_organization_id        => p_organization_id
5614 			  ,p_user_id                => p_user_id
5615 			  ,p_transaction_header_id  => p_transaction_header_id
5616 			  ,p_transaction_temp_id    => p_transaction_temp_id
5617 			  ,x_return_status          => x_return_status
5618 			  ,x_msg_count              => x_msg_count
5619 			  ,x_msg_data               => x_msg_data);
5620 	   IF x_return_status <> l_g_ret_sts_success THEN
5621 		  RAISE fnd_api.G_EXC_ERROR;
5622 	   END IF;
5623 	END IF;
5624 
5625 	-- Group MMTTs(including p_transaction_temp_id) with  p_transaction_header_id
5626 	-- by inventory_item_id,subinventory_code,locator_id.
5627 	-- update one of the MMTTs(min temp_id) from each group and delete rest from that group
5628 	l_progress := 500;
5629 	IF l_debug = 1 THEN mydebug('l_progress =  ' || l_progress);  END IF;
5630 
5631 	DELETE  mtl_allocations_gtmp ;
5632 	IF SQL%NOTFOUND THEN
5633 	   null;
5634 	END IF;
5635 
5636 	IF P_ACTION is NULL OR p_action <> 'CMS' THEN
5637 	FOR rec_mmtt1 IN cur_mmtt1
5638 	LOOP
5639 	   l_progress := 510;
5640 	   mydebug('l_progress                      =  ' || l_progress);
5641 	   mydebug('l_original_sub_loc              =  ' || l_original_sub_loc);
5642 	   mydebug('rec_mmtt1.mmtt_group_count      =  ' || rec_mmtt1.mmtt_group_count);
5643 	   mydebug('rec_mmtt1.group_temp_id         =  ' || rec_mmtt1.group_temp_id);
5644 	   mydebug('rec_mmtt1.subinventory_code     =  ' || rec_mmtt1.subinventory_code);
5645 	   mydebug('rec_mmtt1.locator_id            =  ' || rec_mmtt1.locator_id);
5646 	   mydebug('rec_mmtt1.item_primary_uom_code =  ' || rec_mmtt1.item_primary_uom_code);
5647 	   mydebug('rec_mmtt1.mmtt_primary_quantity =  ' || rec_mmtt1.mmtt_primary_quantity);
5648 	   mydebug('rec_mmtt1.mmtt_secondary_transaction_quantity =  ' || rec_mmtt1.mmtt_secondary_quantity);
5649 	   mydebug('rec_mmtt1.SECONDARY_UOM_CODE    =  ' || rec_mmtt1.SECONDARY_UOM_CODE);
5650 	   mydebug('rec_mmtt1.fulfillment_base      =  ' || rec_mmtt1.fulfillment_base);
5651 
5652 	  IF rec_mmtt1.item_primary_uom_code IS NULL THEN
5653 		 fnd_message.set_name('WMS', 'WMS_NULL_PRIM_UOM');
5654 		 -- Item primary UOM is null for this task
5655 		 fnd_msg_pub.ADD;
5656 		 RAISE fnd_api.g_exc_unexpected_error;
5657 	  END IF;
5658 	  -- this global temp table will have a list of transaction_temp_ids of MMTTs that
5659 	  -- are confirmed to stay back in MMTT. Later we will use this list to decide on which
5660 	  -- MMTTs should be deleted that belong to the p_transaction-header_id but are not
5661 	  -- in this list
5662 	  l_progress := 600;
5663 	  INSERT
5664 		INTO mtl_allocations_gtmp
5665 			 (transaction_temp_id)
5666 	  VALUES ( rec_mmtt1.group_temp_id);
5667 	  mydebug('Inserted temp_id into mtl_allocations_gtmp: ' || rec_mmtt1.group_temp_id);
5668 
5669 	  IF p_suggested_uom <> rec_mmtt1.item_primary_uom_code
5670 	  THEN
5671 			IF(p_suggested_uom = rec_mmtt1.SECONDARY_UOM_CODE) THEN
5672 				l_suggested_mmtt_qty := rec_mmtt1.mmtt_secondary_quantity;
5673 			ELSE
5674 				IF(NVL(rec_mmtt1.fulfillment_base, 'P') = 'S') THEN
5675 					l_suggested_mmtt_qty      := inv_convert.inv_um_convert
5676 								  (item_id          => rec_mmtt1.inventory_item_id
5677 								  ,precision        => l_g_decimal_precision
5678 								  ,from_quantity    => rec_mmtt1.mmtt_secondary_quantity
5679 								  ,from_unit        => rec_mmtt1.SECONDARY_UOM_CODE
5680 								  ,to_unit          => p_suggested_uom
5681 								  ,from_name        => NULL
5682 								  ,to_name          => NULL);
5683 				ELSE
5684 					l_suggested_mmtt_qty      := inv_convert.inv_um_convert
5685 								  (item_id          => rec_mmtt1.inventory_item_id
5686 								  ,precision        => l_g_decimal_precision
5687 								  ,from_quantity    => rec_mmtt1.mmtt_primary_quantity
5688 								  ,from_unit        => rec_mmtt1.item_primary_uom_code
5689 								  ,to_unit          => p_suggested_uom
5690 								  ,from_name        => NULL
5691 								  ,to_name          => NULL);
5692 				END IF;
5693 			END IF;
5694 	  ELSE
5695 		 l_suggested_mmtt_qty      := rec_mmtt1.mmtt_primary_quantity;
5696 	  END IF;
5697 		 l_suggested_mmtt_sec_qty  := rec_mmtt1.mmtt_secondary_quantity;
5698 	  mydebug('l_suggested_mmtt_qty : ' || l_suggested_mmtt_qty );
5699 	  mydebug('l_suggested_mmtt_sec_qty : ' || l_suggested_mmtt_sec_qty );
5700 	  l_progress := 650;
5701 
5702 	  -- Bug#4185621: decide whether to update posting flag
5703 	  IF (rec_mmtt1.parent_line_id = rec_mmtt1.group_temp_id) THEN
5704 		  l_parent_posting_flag := 'N';  -- bulk parent, need to update posting flag back to 'N'
5705 	  ELSE
5706 		  l_parent_posting_flag := 'Y';  -- non bulk mmtt, keep posting flag as 'Y'
5707 	  END IF;
5708 	  -- Bug# 4185621: end change
5709 
5710 	   /* Update the MMTT record with transacttion-temp_id = group_temp_id.
5711 		Updating transaction_quantity same as primary_quantity since
5712 		the transaction_qty should be IN same uom AS primary qty FOR this mmtt */
5713 	   UPDATE   mtl_material_transactions_temp
5714 		  SET    primary_quantity     = rec_mmtt1.mmtt_primary_quantity
5715 			   , transaction_quantity = l_suggested_mmtt_qty
5716 			   , secondary_transaction_quantity = DECODE(secondary_transaction_quantity, NULL, NULL, l_suggested_mmtt_sec_qty)
5717 			   , transaction_uom      = p_suggested_uom
5718 			   , transfer_lpn_id      = NULL
5719 			   , lpn_id               = NULL
5720 			   , content_lpn_id       = NULL
5721 			   , last_update_date     = SYSDATE
5722 			   , last_updated_by      = p_user_id
5723 			   , wms_task_status = l_g_task_pending -- Bug4185621: update mmtt task status back to pending
5724 			   , posting_flag = l_parent_posting_flag -- Bug4185621: updating posting flag
5725 		WHERE    transaction_temp_id  = rec_mmtt1.group_temp_id;
5726 		IF SQL%NOTFOUND THEN
5727 		   RAISE fnd_api.G_EXC_ERROR;
5728 		END IF;
5729 
5730 		-- Bug# 4185621: update child line posting flag back to 'Y' for bulk picking
5731 		IF (l_parent_posting_flag = 'N') THEN
5732 			UPDATE mtl_material_transactions_temp mmtt
5733 			   SET posting_flag = 'Y'
5734 			 WHERE parent_line_id = rec_mmtt1.group_temp_id
5735 			   AND parent_line_id <> transaction_temp_id;
5736 		END IF;
5737 		-- Bug# 4185621: end change
5738 
5739 		l_progress := 700;
5740 		mydebug('l_progress ..  ' || l_progress);
5741 
5742 	   -- PROCESS LOTS /SERIALS BEFORE DELETING ..... *****
5743 	   -- Only if the present cursor group has more than one row
5744 	   -- that it is necessary to take care of lots and  serial
5745 	   -- IF    (rec_mmtt1.mmtt_group_count > 1) THEN : for serials not allocated. It has to be processed
5746 	   IF    ((p_lot_controlled = 'Y' AND p_serial_controlled = 'Y')
5747 		   OR (p_lot_controlled = 'Y' AND p_serial_controlled <> 'Y'))
5748 
5749 	   THEN
5750 		  l_progress := 800;
5751 		  mydebug('l_progress ..  ' || l_progress);
5752 
5753 	-- Start changes for 14699845 (Flexible Lot Allocation)
5754 		BEGIN
5755 
5756 		  SELECT 'Y'
5757 			INTO l_lot_alloc_exist
5758 			FROM mtl_transaction_lots_temp
5759 		   WHERE transaction_temp_id = p_transaction_temp_id
5760 			 AND ROWNUM = 1;
5761 
5762 		   mydebug('FlexibleLotAlloc: l_lot_alloc_exist ' || l_lot_alloc_exist);
5763 		  EXCEPTION
5764 		   WHEN NO_DATA_FOUND THEN
5765 		   l_lot_alloc_exist := 'N';
5766 		   WHEN OTHERS THEN
5767 		   l_lot_alloc_exist := 'N';
5768 		END;
5769 		  l_progress := 850;
5770 		  mydebug('l_progress ..  ' || l_progress);
5771 		  mydebug('FlexibleLotAlloc: l_lot_alloc_exist ' || l_lot_alloc_exist);
5772 	-- End changes for 14699845 (Flexible Lot Allocation)
5773 
5774 		  --IF    (rec_mmtt1.mmtt_group_count > 1)
5775 				--OR
5776 				--(rec_mmtt1.mmtt_group_count = 1 AND
5777 				--p_serial_controlled = 'Y' and p_serial_allocated_flag = 'N')
5778 		  --THEN
5779 			 FOR rec_mtlt1 IN cur_mtlt1  (rec_mmtt1.subinventory_code
5780 									 , rec_mmtt1.locator_id
5781 									 , rec_mmtt1.item_primary_uom_code
5782 									 , rec_mmtt1.inventory_item_id
5783 									 , rec_mmtt1.revision
5784 									 , rec_mmtt1.group_temp_id)
5785 			 LOOP
5786 			 l_progress := 1000;
5787 			 mydebug('l_progress ..  ' || l_progress);
5788 			 mydebug('rec_mtlt1.lot_number : ' || rec_mtlt1.lot_number);
5789 			 mydebug('rec_mtlt1.group_lot_primary_quantity : ' || rec_mtlt1.group_lot_primary_quantity);
5790 			 mydebug('rec_mtlt1.group_lot_secondary_quantity : ' || rec_mtlt1.group_lot_secondary_quantity);
5791 			 mydebug('rec_mtlt1.group_lot_count : ' || rec_mtlt1.group_lot_count);
5792 			 mydebug('rec_mtlt1.group_lot_temp_id : ' || rec_mtlt1.group_lot_temp_id);
5793 			 mydebug('rec_mtlt1.group_lot_primary_quantity : ' || rec_mtlt1.group_lot_primary_quantity);
5794 
5795 			 l_serial_transaction_temp_id := NULL;
5796 			 SELECT mtl_material_transactions_s.NEXTVAL
5797 			 INTO   l_serial_transaction_temp_id
5798 			 FROM   DUAL;
5799 			 mydebug('l_serial_transaction_temp_id ..  ' || l_serial_transaction_temp_id);
5800 			 IF p_suggested_uom <> rec_mmtt1.item_primary_uom_code
5801 			 THEN
5802 				IF(p_suggested_uom = rec_mmtt1.SECONDARY_UOM_CODE) THEN
5803 					l_suggested_mtlt_qty := rec_mtlt1.group_lot_secondary_quantity;
5804 				ELSE
5805 					IF(NVL(rec_mmtt1.fulfillment_base, 'P') = 'S') THEN
5806 						l_suggested_mtlt_qty := inv_convert.inv_um_convert
5807 										 (item_id          => rec_mmtt1.inventory_item_id
5808 										 ,lot_number	   => rec_mtlt1.lot_number  --BUG12622871LSC
5809 										 ,organization_id  => p_organization_id
5810 										 ,precision        => l_g_decimal_precision
5811 										 ,from_quantity    => rec_mtlt1.group_lot_secondary_quantity
5812 										 ,from_unit        => rec_mmtt1.SECONDARY_UOM_CODE
5813 										 ,to_unit          => p_suggested_uom
5814 										 ,from_name        => NULL
5815 										 ,to_name          => NULL);
5816 					ELSE
5817 						l_suggested_mtlt_qty := inv_convert.inv_um_convert
5818 										 (item_id          => rec_mmtt1.inventory_item_id
5819 										 ,lot_number	   => rec_mtlt1.lot_number  --BUG12622871LSC
5820 										 ,organization_id  => p_organization_id
5821 										 ,precision        => l_g_decimal_precision
5822 										 ,from_quantity    => rec_mtlt1.group_lot_primary_quantity
5823 										 ,from_unit        => rec_mmtt1.item_primary_uom_code
5824 										 ,to_unit          => p_suggested_uom
5825 										 ,from_name        => NULL
5826 										 ,to_name          => NULL);
5827 					END IF;
5828 				END IF;
5829 			 ELSE
5830 				l_suggested_mtlt_qty := rec_mtlt1.group_lot_primary_quantity;
5831 			 END IF;
5832 				l_suggested_mtlt_sec_qty := rec_mtlt1.group_lot_secondary_quantity;
5833 			 mydebug('l_suggested_mtlt_qty : ' || l_suggested_mtlt_qty);
5834 			 mydebug('l_suggested_mtlt_sec_qty : ' || l_suggested_mtlt_sec_qty);
5835 
5836 			 IF p_serial_controlled = 'Y'  THEN
5837 				IF p_serial_allocated_flag = 'Y' THEN
5838 
5839 				   l_progress := 1100;
5840 				   mydebug('l_progress ..  ' || l_progress);
5841 				   UPDATE  mtl_serial_numbers_temp
5842 					  SET  transaction_temp_id = l_serial_transaction_temp_id
5843 						 , last_update_date    = SYSDATE
5844 						 , last_updated_by     = p_user_id
5845 					WHERE  transaction_temp_id  IN
5846 						   (SELECT  mtlt.serial_transaction_temp_id
5847 							  FROM  mtl_transaction_lots_temp mtlt
5848 								  , mtl_material_transactions_temp mmtt
5849 							  WHERE   mmtt.transaction_header_id =  p_transaction_header_id
5850 							  AND     mmtt.transaction_temp_id   =  mtlt.transaction_temp_id
5851 							  AND     mmtt.subinventory_code     =  rec_mmtt1.subinventory_code
5852 							  AND     mmtt.locator_id            =  rec_mmtt1.locator_id
5853 							  AND     mmtt.item_primary_uom_code =  rec_mmtt1.item_primary_uom_code
5854 							  AND     mmtt.inventory_item_id     =  rec_mmtt1.inventory_item_id
5855 							  AND     mtlt.lot_number            =  rec_mtlt1.lot_number
5856 							  AND     nvl(mmtt.revision,'@@')    =  nvl(rec_mmtt1.revision,'@@') );
5857 				   mydebug ('ROW COUNT : ' || sql%rowcount);
5858 				   IF SQL%NOTFOUND THEN
5859 					  RAISE fnd_api.G_EXC_ERROR;
5860 				   END IF;
5861 				   l_progress := 1200;
5862 				   mydebug('l_progress ..  ' || l_progress);
5863 				   UPDATE  MTL_SERIAL_NUMBERS
5864 					  SET  group_mark_id    = l_serial_transaction_temp_id
5865 						 , last_updated_by  = p_user_id
5866 						 , last_update_date = SYSDATE
5867 					WHERE  current_organization_id = p_organization_id
5868 					  AND  inventory_item_id       = rec_mmtt1.inventory_item_id
5869 					  AND  serial_number          IN
5870 						   (SELECT fm_serial_number
5871 							  FROM mtl_serial_numbers_temp msnt
5872 							 WHERE msnt.transaction_temp_id   =  l_serial_transaction_temp_id);
5873 
5874 				   IF SQL%NOTFOUND THEN
5875 					  RAISE fnd_api.G_EXC_ERROR;
5876 				   END IF;
5877 				ELSE  --IF p_serial_allocated_flag = 'N' THEN
5878 				   l_progress := 1300;
5879 				   mydebug('l_progress ..  ' || l_progress);
5880 				   -- First unmark serials in all MSNTS
5881 				   UPDATE  MTL_SERIAL_NUMBERS
5882 				   SET     group_mark_id   = NULL
5883 						 , last_updated_by = p_user_id
5884 						 , last_update_date = SYSDATE
5885 				   WHERE  (current_organization_id
5886 						  ,inventory_item_id
5887 						  ,serial_number)
5888 					  IN  (SELECT  mmtt.organization_id
5889 								  ,mmtt.inventory_item_id
5890 								  ,msnt.fm_serial_number
5891 							 FROM  mtl_transaction_lots_temp      mtlt
5892 								  ,mtl_serial_numbers_temp        msnt
5893 								  ,mtl_material_transactions_temp mmtt
5894 							WHERE  mmtt.transaction_header_id      = p_transaction_header_id
5895 							  AND  mmtt.transaction_temp_id        = mtlt.transaction_temp_id
5896 							  AND  mtlt.serial_transaction_temp_id = msnt.transaction_temp_id
5897 							  AND  mmtt.subinventory_code          = rec_mmtt1.subinventory_code
5898 							  AND  mmtt.locator_id                 = rec_mmtt1.locator_id
5899 							  AND  mmtt.item_primary_uom_code      = rec_mmtt1.item_primary_uom_code
5900 							  AND  mmtt.inventory_item_id          = rec_mmtt1.inventory_item_id
5901 							  AND  mtlt.lot_number                 =  rec_mtlt1.lot_number
5902 							  AND  nvl(mmtt.revision,'@@')         =  nvl(rec_mmtt1.revision,'@@')
5903 							  UNION --For lot substitution of lot and serial items when serial are confirmed we insert msnt with mmtt.transaction_temp_id
5904 							  --at that time their is no MTLT for substituted lot..
5905 							  (SELECT  mmtt.organization_id
5906 								  ,mmtt.inventory_item_id
5907 								  ,msnt.fm_serial_number
5908 							 FROM  mtl_serial_numbers_temp        msnt
5909 								  ,mtl_material_transactions_temp mmtt
5910 							WHERE  mmtt.transaction_header_id      = p_transaction_header_id
5911 							  AND  mmtt.transaction_temp_id        = msnt.transaction_temp_id
5912 							  AND  mmtt.subinventory_code          = rec_mmtt1.subinventory_code
5913 							  AND  mmtt.locator_id                 = rec_mmtt1.locator_id
5914 							  AND  mmtt.item_primary_uom_code      = rec_mmtt1.item_primary_uom_code
5915 							  AND  mmtt.inventory_item_id          = rec_mmtt1.inventory_item_id
5916 							  AND  nvl(mmtt.revision,'@@')         =  nvl(rec_mmtt1.revision,'@@') )
5917 							  );
5918 
5919 				   IF SQL%NOTFOUND THEN
5920 						mydebug('No MSNT found  ..may be because no serials were confirmed for ' ||
5921 								'serial_allocation= No before pressing F2' );
5922 				   ELSE
5923 					  -- Now delete MSNTs
5924 					  l_progress := 1400;
5925 					  mydebug('l_progress ..  ' || l_progress);
5926 					  DELETE  mtl_serial_numbers_temp
5927 					   WHERE  transaction_temp_id
5928 						  IN
5929 							  (SELECT  mtlt.serial_transaction_temp_id
5930 								 FROM  mtl_transaction_lots_temp mtlt
5931 									 , mtl_material_transactions_temp mmtt
5932 								 WHERE   mmtt.transaction_header_id =  p_transaction_header_id
5933 								 AND     mmtt.transaction_temp_id   =  mtlt.transaction_temp_id
5934 								 AND     mmtt.subinventory_code     =  rec_mmtt1.subinventory_code
5935 								 AND     mmtt.locator_id            =  rec_mmtt1.locator_id
5936 								 AND     mmtt.item_primary_uom_code =  rec_mmtt1.item_primary_uom_code
5937 								 AND     mmtt.inventory_item_id     =  rec_mmtt1.inventory_item_id
5938 								 AND     mtlt.lot_number            =  rec_mtlt1.lot_number
5939 								 AND     nvl(mmtt.revision,'@@')    =  nvl(rec_mmtt1.revision,'@@')
5940 								 UNION --For lot substitution of lot and serial items when serial are confirmed we insert msnt with mmtt.transaction_temp_id
5941 							  --at that time their is no MTLT for substituted lot..
5942 							  (SELECT msnt.transaction_temp_id
5943 							 FROM  mtl_serial_numbers_temp        msnt
5944 								  ,mtl_material_transactions_temp mmtt
5945 							WHERE  mmtt.transaction_header_id      = p_transaction_header_id
5946 							  AND  mmtt.transaction_temp_id        = msnt.transaction_temp_id
5947 							  AND  mmtt.subinventory_code          = rec_mmtt1.subinventory_code
5948 							  AND  mmtt.locator_id                 = rec_mmtt1.locator_id
5949 							  AND  mmtt.item_primary_uom_code      = rec_mmtt1.item_primary_uom_code
5950 							  AND  mmtt.inventory_item_id          = rec_mmtt1.inventory_item_id
5951 							  AND  nvl(mmtt.revision,'@@')         =  nvl(rec_mmtt1.revision,'@@') )
5952 								);
5953 					  IF SQL%NOTFOUND THEN
5954 						 RAISE fnd_api.G_EXC_ERROR;
5955 					  END IF;
5956 				   END IF;
5957 				END IF; --IF p_serial_allocated_flag = 'N' THEN
5958 				l_op_msnt_to_delete  := 0;
5959 				IF p_serial_allocated_flag = 'Y' THEN  -- to delete all overpicked serials
5960 				   l_progress := 4000;
5961 				   mydebug('l_progress : ' || l_progress);
5962 				   SELECT  count(*)
5963 					 INTO  l_op_msnt_to_delete
5964 					 FROM  mtl_serial_numbers_temp msnt
5965 					WHERE  msnt.transaction_temp_id   =  l_serial_transaction_temp_id;
5966 				   mydebug('l_op_msnt_to_delete : ' || l_op_msnt_to_delete
5967 							|| ':' || rec_mtlt1.group_lot_primary_quantity);
5968 
5969 				   -- only if the rec-count in msnt exceeds the total quantity in MMTT
5970 				   --  means the serials have been  overpicked.
5971 				   IF rec_mtlt1.group_lot_primary_quantity < l_op_msnt_to_delete
5972 				   THEN
5973 				   FOR rec_msnt_to_delete_LS IN cur_msnt_to_delete_LS
5974 												( l_serial_transaction_temp_id)
5975 				   LOOP
5976 					  mydebug('rec_msnt_to_delete_ls.fm_serial_number : ' || rec_msnt_to_delete_ls.fm_serial_number);
5977 					  mydebug('rec_msnt_to_delete_ls.transaction_temp_id : ' || rec_msnt_to_delete_ls.transaction_temp_id);
5978 					  /* In this cursor, we are ordering by creation date with the
5979 					   * assumption that the overpicked serials are newly inserted MSNT records and
5980 					   * they will have creation date higher than the originally allocated serials. */
5981 					  IF l_op_msnt_to_delete <= rec_mtlt1.group_lot_primary_quantity
5982 					  THEN
5983 						 mydebug('l_op_msnt_to_delete : ' || l_op_msnt_to_delete);
5984 						 EXIT; -- we want to process only overpicked records
5985 					  ELSE
5986 						 l_op_msnt_to_delete := l_op_msnt_to_delete - 1;
5987 						 mydebug('l_op_msnt_to_delete : ' || l_op_msnt_to_delete);
5988 					  END IF;
5989 
5990 					  l_progress := 4300;
5991 					  mydebug('l_progress : ' || l_progress);
5992 					  UPDATE  MTL_SERIAL_NUMBERS
5993 						 SET  group_mark_id    = NULL
5994 							  ,last_updated_by  = p_user_id
5995 							  ,last_update_date = SYSDATE
5996 					   WHERE  current_organization_id = p_organization_id
5997 						 AND  inventory_item_id       = rec_mmtt1.inventory_item_id
5998 						 AND  serial_number           = rec_msnt_to_delete_ls.fm_serial_number;
5999 					   IF SQL%NOTFOUND THEN
6000 						  mydebug('No MSN found to be updated..not good' );
6001 						  RAISE fnd_api.G_EXC_ERROR;
6002 					   ELSE
6003 							l_progress := 4400;
6004 							mydebug('l_progress : ' || l_progress);
6005 							DELETE  mtl_serial_numbers_temp
6006 							 WHERE  transaction_temp_id = rec_msnt_to_delete_ls.transaction_temp_id
6007 							   AND  fm_serial_number = rec_msnt_to_delete_ls.fm_serial_number;
6008 
6009 							IF SQL%NOTFOUND THEN
6010 								 RAISE fnd_api.G_EXC_ERROR;
6011 							END IF;
6012 					   END IF;
6013 				   END LOOP;
6014 				   END IF;
6015 				END IF; -- serial_allocated_flag = Y and delete overpicked serials
6016 			 END IF; -- p_serial_controlled = 'Y'
6017 
6018 			 /* even if the # of records in MTLT for this lot is 1, it has to be updated
6019 				with serial-transaction-temp_id and user_id, sysdate.
6020 				So, there is no harm is updating qty too */
6021 			 l_progress := 1500;
6022 			 mydebug('l_progress ..  ' || l_progress);
6023 			IF (l_lot_alloc_exist ='Y') THEN -- Added for 14699845 (Flexible Lot Allocation)
6024 			 UPDATE   MTL_transaction_lots_temp mtlt
6025 			 SET      transaction_temp_id  = rec_mmtt1.group_temp_id
6026 					, primary_quantity     = rec_mtlt1.group_lot_primary_quantity
6027 					, transaction_quantity = l_suggested_mtlt_qty
6028 					, secondary_quantity   = decode (secondary_quantity, null, null, l_suggested_mtlt_sec_qty)
6029 					, mtlt.serial_transaction_temp_id = l_serial_transaction_temp_id
6030 					, last_update_date     = SYSDATE
6031 					, last_updated_by      = p_user_id
6032 			 WHERE    lot_number           = rec_mtlt1.lot_number
6033 			 AND      transaction_temp_id  = rec_mtlt1.group_lot_temp_id;
6034 			 IF SQL%NOTFOUND THEN
6035 				 RAISE fnd_api.G_EXC_ERROR;
6036 			 END IF;
6037 
6038 			 l_progress := 1600;
6039 			 mydebug('l_progress ..  ' || l_progress);
6040 			 IF rec_mtlt1.group_lot_count > 1 THEN
6041 				l_progress := 230;
6042 				mydebug('l_progress ..  ' || l_progress);
6043 				DELETE  mtl_transaction_lots_temp
6044 				 WHERE  lot_number  =  rec_mtlt1.lot_number
6045 				   AND  transaction_temp_id
6046 					IN
6047 					   (SELECT  mtlt.transaction_temp_id
6048 						  FROM  mtl_material_transactions_temp  mmtt
6049 							   ,mtl_transaction_lots_temp       mtlt
6050 						 WHERE  mmtt.transaction_header_id =  p_transaction_header_id
6051 						   AND  mmtt.transaction_temp_id   <> rec_mmtt1.group_temp_id
6052 						   AND  mmtt.transaction_temp_id   =  mtlt.transaction_temp_id
6053 						   AND  mtlt.lot_number            =  rec_mtlt1.lot_number
6054 						   AND  mmtt.subinventory_code     =  rec_mmtt1.subinventory_code
6055 						   AND  mmtt.locator_id            =  rec_mmtt1.locator_id
6056 						   AND  mmtt.item_primary_uom_code =  rec_mmtt1.item_primary_uom_code
6057 						   AND  mmtt.inventory_item_id     =  rec_mmtt1.inventory_item_id
6058 						   AND  mtlt.lot_number            =  rec_mtlt1.lot_number
6059 						   AND  nvl(mmtt.revision,'@@')    =  nvl(rec_mmtt1.revision,'@@') );
6060 
6061 				IF SQL%NOTFOUND THEN
6062 				   RAISE fnd_api.G_EXC_ERROR;
6063 				END IF;
6064 			 END IF;
6065 			 END IF; -- Added for 14699845 (Flexible Lot Allocation)
6066 			 END LOOP;
6067 
6068 		  --END IF; -- rec_mmtt1.rec_count > 1 OR
6069 				  -- (rec_mmtt1.mmtt_group_count = 1 AND
6070 				  -- p_serial_controlled = 'Y' and p_serial_allocated_flag = 'N')
6071 	   END IF; --lot controlled or lot +serial controlled
6072 	   l_progress := 1700;
6073 	   mydebug('l_progress : ' || l_progress);
6074 
6075 	   -- Serial Controlled items
6076 	   IF (p_lot_controlled <> 'Y' AND p_serial_controlled = 'Y') THEN
6077 		   -- serial
6078 		  l_progress := 1800;
6079 		  mydebug('l_progress : ' || l_progress);
6080 		  IF (p_serial_allocated_flag     = 'Y') THEN
6081 			 IF rec_mmtt1.mmtt_group_count > 1 THEN
6082 				-- Now update MSNT
6083 				l_progress := 1900;
6084 				mydebug('l_progress : ' || l_progress);
6085 
6086 				UPDATE  mtl_serial_numbers_temp
6087 				   SET  transaction_temp_id = rec_mmtt1.group_temp_id
6088 					  , last_update_date    = SYSDATE
6089 					  , last_updated_by     = p_user_id
6090 				 WHERE  transaction_temp_id
6091 					IN
6092 						(SELECT  msnt.transaction_temp_id
6093 						   FROM  mtl_serial_numbers_temp msnt,
6094 								 mtl_material_transactions_temp mmtt
6095 						  WHERE  mmtt.transaction_header_id = p_transaction_header_id
6096 							AND  mmtt.transaction_temp_id   <> rec_mmtt1.group_temp_id
6097 							AND  mmtt.transaction_temp_id   =  msnt.transaction_temp_id
6098 							AND  mmtt.subinventory_code     =  rec_mmtt1.subinventory_code
6099 							AND  mmtt.locator_id            =  rec_mmtt1.locator_id
6100 							AND  mmtt.item_primary_uom_code =  rec_mmtt1.item_primary_uom_code
6101 							AND  mmtt.inventory_item_id     =  rec_mmtt1.inventory_item_id
6102 							AND  nvl(mmtt.revision,'@@')    =  nvl(rec_mmtt1.revision,'@@') );
6103 
6104 				 IF SQL%NOTFOUND THEN
6105 					 RAISE fnd_api.G_EXC_ERROR;
6106 				 END IF;
6107 			   -- REMARK MSN with new temp_id
6108 
6109 				l_progress := 2000;
6110 				mydebug('l_progress : ' || l_progress);
6111 				UPDATE  MTL_SERIAL_NUMBERS
6112 				SET     group_mark_id       = rec_mmtt1.group_temp_id
6113 					  , last_updated_by     = p_user_id
6114 					  , last_update_date    = SYSDATE
6115 				 WHERE  current_organization_id = p_organization_id
6116 				   AND  inventory_item_id       = rec_mmtt1.inventory_item_id
6117 				   AND  serial_number
6118 					IN
6119 						(SELECT fm_serial_number
6120 						   FROM mtl_serial_numbers_temp
6121 						  WHERE transaction_temp_id   =  rec_mmtt1.group_temp_id);
6122 														  --l_serial_transaction_temp_id);
6123 
6124 				IF SQL%NOTFOUND THEN
6125 				   RAISE fnd_api.G_EXC_ERROR;
6126 				END IF;
6127 			 END IF;
6128 		  ELSE -- (p_serial_allocated_flag     = 'N' ,
6129 				-- delete all msnts and unmark all these serials in MSN.
6130 			 l_progress := 2100;
6131 			 mydebug('l_progress : ' || l_progress);
6132 			 UPDATE  MTL_SERIAL_NUMBERS
6133 			 SET     group_mark_id  = NULL
6134 				   , last_updated_by = p_user_id
6135 				   , last_update_date = SYSDATE
6136 			 WHERE  (current_organization_id
6137 					,inventory_item_id
6138 					,serial_number)
6139 				IN  (SELECT  mmtt.organization_id
6140 							,mmtt.inventory_item_id
6141 							,msnt.fm_serial_number
6142 					   FROM  mtl_serial_numbers_temp        msnt
6143 							,mtl_material_transactions_temp mmtt
6144 					  WHERE   mmtt.transaction_header_id  = p_transaction_header_id
6145 						AND   mmtt.transaction_temp_id   =  msnt.transaction_temp_id
6146 						AND   mmtt.subinventory_code     =  rec_mmtt1.subinventory_code
6147 						AND   mmtt.locator_id            =  rec_mmtt1.locator_id
6148 						AND   mmtt.item_primary_uom_code =  rec_mmtt1.item_primary_uom_code
6149 						AND   mmtt.inventory_item_id     =  rec_mmtt1.inventory_item_id
6150 						AND   nvl(mmtt.revision,'@@')    =  nvl(rec_mmtt1.revision,'@@') );
6151 
6152 			 IF SQL%NOTFOUND THEN
6153 				mydebug('No MSNT found  ..may be because no serials were confirmed for ' ||
6154 						 'serial_allocation= No before pressing F2' );
6155 			 ELSE
6156 				l_progress := 2200;
6157 				mydebug('l_progress : ' || l_progress);
6158 
6159 				DELETE  mtl_serial_numbers_temp
6160 				WHERE   transaction_temp_id  IN
6161 				(SELECT  msnt.transaction_temp_id
6162 				 FROM    mtl_serial_numbers_temp msnt,
6163 						 mtl_material_transactions_temp mmtt
6164 				 WHERE   mmtt.transaction_header_id  = p_transaction_header_id
6165 				   AND   mmtt.transaction_temp_id   =  msnt.transaction_temp_id
6166 				   AND   mmtt.subinventory_code     =  rec_mmtt1.subinventory_code
6167 				   AND   mmtt.locator_id            =  rec_mmtt1.locator_id
6168 				   AND   mmtt.item_primary_uom_code =  rec_mmtt1.item_primary_uom_code
6169 				   AND   mmtt.inventory_item_id     =  rec_mmtt1.inventory_item_id
6170 				   AND   nvl(mmtt.revision,'@@')    =  nvl(rec_mmtt1.revision,'@@') );
6171 
6172 				 IF SQL%NOTFOUND THEN
6173 					 RAISE fnd_api.G_EXC_ERROR;
6174 				 END IF;
6175 			  END IF;
6176 		  END IF;
6177 		  l_op_msnt_to_delete  := 0;
6178 		  IF p_serial_allocated_flag = 'Y' THEN  -- to delete all overpicked serials
6179 			l_progress := 3000;
6180 			mydebug('l_progress : ' || l_progress);
6181 			SELECT count(*)
6182 			INTO l_op_msnt_to_delete
6183 			FROM mtl_serial_numbers_temp
6184 			WHERE (transaction_temp_id   ,
6185 				   fm_serial_number)
6186 			IN
6187 			  (SELECT   msnt.transaction_temp_id
6188 					  ,msnt.fm_serial_number
6189 				FROM   mtl_serial_numbers_temp msnt
6190 					  ,mtl_material_transactions_temp mmtt
6191 			   WHERE   mmtt.transaction_header_id  = p_transaction_header_id
6192 				 AND   mmtt.transaction_temp_id   =  msnt.transaction_temp_id
6193 				 AND   mmtt.subinventory_code     =  rec_mmtt1.subinventory_code
6194 				 AND   mmtt.locator_id            =  rec_mmtt1.locator_id
6195 				 AND   mmtt.item_primary_uom_code =  rec_mmtt1.item_primary_uom_code
6196 				 AND   mmtt.inventory_item_id     =  rec_mmtt1.inventory_item_id
6197 				 AND   nvl(mmtt.revision,'@@')    =  nvl(rec_mmtt1.revision,'@@'));
6198 			mydebug('l_op_msnt_to_delete : ' || l_op_msnt_to_delete);
6199 
6200 			-- only if the rec-count in msnt exceeds the total quantity in MMTT
6201 			--  means the serials have been  overpicked.
6202 			IF rec_mmtt1.mmtt_primary_quantity < l_op_msnt_to_delete
6203 			THEN
6204 			 FOR rec_msnt_to_delete IN cur_msnt_to_delete
6205 							 ( rec_mmtt1.subinventory_code
6206 							  ,rec_mmtt1.locator_id
6207 							  ,rec_mmtt1.item_primary_uom_code
6208 							  ,rec_mmtt1.inventory_item_id
6209 							  ,rec_mmtt1.revision    )
6210 			 LOOP
6211 				mydebug('rec_msnt_to_delete.fm_serial_number : ' || rec_msnt_to_delete.fm_serial_number);
6212 				mydebug('rec_msnt_to_delete.transaction_temp_id : ' || rec_msnt_to_delete.transaction_temp_id);
6213 				mydebug('rec_msnt_to_delete.organization_id : ' || rec_msnt_to_delete.organization_id);
6214 				mydebug('rec_msnt_to_delete.inventory_item_id : ' || rec_msnt_to_delete.inventory_item_id);
6215 				mydebug('rec_msnt_to_delete.creation_date : ' ||
6216 					  to_char(rec_msnt_to_delete.creation_date,'dd:mon-yyyy:hh24:mi:ss'));
6217 
6218 
6219 				/* In this cursor, we are ordering by creation date with the
6220 				 * assumption that the overpicked serials are newly inserted MSNT records and
6221 				 * they will have creation date higher than the originally allocated serials. */
6222 				IF l_op_msnt_to_delete <= rec_mmtt1.mmtt_primary_quantity
6223 				THEN
6224 				   l_progress := 3200;
6225 				   mydebug('l_progress : ' || l_progress);
6226 				   EXIT; -- we want to process only overpicked records
6227 				ELSE
6228 				   l_op_msnt_to_delete := l_op_msnt_to_delete - 1;
6229 				   mydebug('l_op_msnt_to_delete : ' || l_op_msnt_to_delete);
6230 				END IF;
6231 				l_progress := 3300;
6232 				mydebug('l_progress : ' || l_progress);
6233 				UPDATE  MTL_SERIAL_NUMBERS
6234 				   SET  group_mark_id    = NULL
6235 						,last_updated_by  = p_user_id
6236 						,last_update_date = SYSDATE
6237 				 WHERE  current_organization_id = p_organization_id
6238 				   AND  inventory_item_id       = rec_mmtt1.inventory_item_id
6239 				   AND  serial_number           = rec_msnt_to_delete.fm_serial_number;
6240 				 IF SQL%NOTFOUND THEN
6241 					mydebug('No MSN found to be updated..not good' );
6242 					RAISE fnd_api.G_EXC_ERROR;
6243 				 ELSE
6244 					  l_progress := 2200;
6245 					  mydebug('l_progress : ' || l_progress);
6246 					  DELETE  mtl_serial_numbers_temp
6247 					   WHERE  transaction_temp_id = rec_msnt_to_delete.transaction_temp_id
6248 						 AND  fm_serial_number = rec_msnt_to_delete.fm_serial_number;
6249 
6250 					  IF SQL%NOTFOUND THEN
6251 						   RAISE fnd_api.G_EXC_ERROR;
6252 					  END IF;
6253 				 END IF;
6254 			 END LOOP;
6255 			END IF;
6256 		  END IF;
6257 	   END IF; -- Serial only Controlled
6258 	END LOOP;  --cur_mmtt1 loop
6259 	END IF;  --Non CMS
6260 	l_progress := 2300;
6261 	mydebug('l_progress ..  ' || l_progress);
6262 
6263 	  /* {{viks for start_over we need to call proc_start_over to reset the task
6264    *  status to dispatched and sequence  transaction_temp_ids in pl/sql table from global temp tablei
6265    * as they are picked.
6266    * }}
6267    */
6268    -- Start changes for 14699845 (Flexible Lot Allocation)
6269    IF (l_lot_alloc_exist ='N') THEN
6270 	 Cleanup_LS_FlexiAlloc( p_transaction_temp_id
6271 						  , p_transaction_header_id
6272 						  , p_serial_controlled
6273 						  , l_ret_sts);
6274 	 IF (l_ret_sts <> fnd_api.g_ret_sts_success) THEN
6275 		mydebug('Issues while cleaning up Lot Serial Temp records for Flexible Lot Allocation');
6276 	 END IF;
6277    END IF;
6278    -- End changes for 14699845 (Flexible Lot Allocation)
6279  IF p_start_over = 'Y' THEN
6280 
6281 	mydebug('viks start_over button pressed calling proc_start_over:');
6282 	proc_start_over(p_transaction_header_id    => p_transaction_header_id
6283 					 ,p_transaction_temp_id    => p_transaction_temp_id
6284 					 ,p_user_id                => p_user_id
6285 					 ,x_start_over_taskno      => x_start_over_taskno
6286 					 ,x_return_status          => x_return_status
6287 					 ,x_msg_count              => x_msg_count
6288 					 ,x_msg_data               => x_msg_data);
6289 	   IF x_return_status <> l_g_ret_sts_success THEN
6290 		  RAISE fnd_api.G_EXC_ERROR;
6291 	   ELSE
6292 	   l_start_over_task := x_start_over_taskno;
6293 	   END IF;
6294   mydebug('viks l_start_over_task return froom proc_start_over :' ||l_start_over_task);
6295  ELSE
6296    mydebug('viks start_over button not pressed  :');
6297    --only when it is not start over case, the retain task will be used 431009
6298   wms_picking_pkg.g_start_over_tempid.DELETE;
6299   IF p_retain_task = 'Y' THEN --{ bug 4310093
6300 	  mydebug('change the task status to Dispatched if retain_task is Y');
6301 	  UPDATE wms_dispatched_tasks
6302 	  SET status = l_g_task_dispatched
6303 		  ,last_update_date = SYSDATE
6304 		  ,last_updated_by = p_user_id
6305 	  WHERE transaction_temp_id IN
6306 			(SELECT transaction_temp_id
6307 					 FROM mtl_allocations_gtmp);
6308 	  mydebug('nullify certain columns for all the dispatched tasks for this user');
6309 	  UPDATE  wms_dispatched_tasks
6310 	   SET   device_invoked = null
6311 			 -- Following two statement are commnet for bug 4560814
6312 			 --task_method = NULL  -- for cluster picking
6313 			 -- ,task_group_id = NULL
6314 			,last_update_date = SYSDATE
6315 			,last_updated_by = p_user_id
6316 	   WHERE  person_id = p_employee_id
6317 		  AND  status = l_g_task_dispatched;
6318 
6319   END IF; --}
6320   mydebug('viks l_start_over_task return value no :' ||l_start_over_task);
6321 
6322 
6323 
6324  END IF;
6325 
6326 	x_start_over_taskno := l_start_over_task;
6327  mydebug('viks l_start_over_task return final value :' ||l_start_over_task);
6328 
6329 
6330 	  -- this global temp table has a list of transaction_temp_ids of MMTTs that
6331 	  -- are confirmed to stay back in MMTT and WDT. Using this we will delete WDT and MMTT
6332 
6333 	DELETE wms_dispatched_tasks
6334 	WHERE  transaction_temp_id IN
6335 		  (SELECT transaction_temp_id
6336 			 FROM mtl_material_transactions_temp
6337 			WHERE transaction_header_id  = p_transaction_header_id
6338 			  AND transaction_temp_id NOT IN
6339 				  (SELECT transaction_temp_id
6340 					 FROM mtl_allocations_gtmp));
6341 	IF SQL%NOTFOUND THEN
6342 	   mydebug('no extra WDTs to delete :' );
6343 	   NULL;
6344 	END IF;
6345 
6346 	l_progress := 2400;
6347 	mydebug('l_progress ..  ' || l_progress);
6348 	  -- this global temp table has a list of transaction_temp_ids of MMTTs that
6349 	  -- are confirmed to stay back in MMTT and WDT. Using this we will delete WDT and MMTT
6350 	/* {{ If start over is pressed wdd would be deleted .Lines need to stay in
6351  * status dispatched.
6352  * }}
6353  */
6354 
6355    IF p_start_over ='N' and p_retain_task='N' THEN --bug 4310093
6356 
6357 	DELETE wms_dispatched_tasks
6358 	WHERE  transaction_temp_id IN
6359 		  (SELECT transaction_temp_id
6360 			 FROM mtl_allocations_gtmp)
6361 	  AND  status <> l_g_task_queued;
6362 	IF SQL%NOTFOUND THEN
6363 	   mydebug('no non queued WDTs to delete :' );
6364 	   NULL;
6365 	END IF;
6366   END IF;
6367 
6368 	l_progress := 2500;
6369 	mydebug('l_progress ..  ' || l_progress);
6370 	DELETE mtl_material_transactions_temp
6371 	 WHERE transaction_header_id  = p_transaction_header_id
6372 	   AND transaction_temp_id NOT IN
6373 		   (SELECT transaction_temp_id
6374 			  FROM mtl_allocations_gtmp);
6375 	IF SQL%NOTFOUND THEN
6376 	   mydebug('no extra MMTTs to delete :' );
6377 	   NULL;
6378 	END IF;
6379 
6380 	--Added for Case Picking Project start
6381 
6382 	wms_picking_pkg.clear_order_numbers(
6383 					  x_return_status =>x_return_status,
6384 					  x_msg_count =>x_msg_count,
6385 					  x_msg_data => x_msg_data);
6386 	mydebug('wms_picking_pkg.clear_order_numbers x_msg_count/Data = ' || x_msg_count || '/'|| x_msg_data);
6387 	mydebug('wms_picking_pkg.clear_order_numbers x_return_status = ' ||x_return_status );
6388 	wms_picking_pkg.clear_pick_slip_number(
6389 					  x_return_status =>x_return_status,
6390 					  x_msg_count =>x_msg_count,
6391 					  x_msg_data => x_msg_data);
6392 	mydebug('wms_picking_pkg.clear_pick_slip_number x_msg_count/Data = ' || x_msg_count || '/'|| x_msg_data);
6393 	mydebug('wms_picking_pkg.clear_pick_slip_number x_return_status = ' ||x_return_status );
6394 
6395 	--Added for Case Picking Project end
6396 
6397 
6398    mydebug('Commit ' );
6399    COMMIT;
6400    mydebug('End ..  ' || l_proc_name);
6401 EXCEPTION
6402    WHEN fnd_api.g_exc_error THEN
6403 		x_return_status  := l_g_ret_sts_error;
6404 		fnd_message.set_name('WMS', 'WMS_INTERNAL_ERROR'); --NEWMSG
6405 		-- Internal Error $ROUTINE
6406 		fnd_message.set_token('ROUTINE', '- Process_f2 API' );
6407 		fnd_msg_pub.ADD;
6408 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6409 		mydebug('ROLLBACK ' );
6410 		ROLLBACK ;
6411 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
6412 		mydebug('fnd_api.g_exc_error: ' || SQLERRM);
6413    WHEN fnd_api.g_exc_unexpected_error THEN
6414 		x_return_status  := fnd_api.g_ret_sts_unexp_error;
6415 		fnd_message.set_name('WMS', 'WMS_INTERNAL_ERROR'); --NEWMSG
6416 		-- Internal Error $ROUTINE
6417 		fnd_message.set_token('ROUTINE', '- Process_f2 API' );
6418 		fnd_msg_pub.ADD;
6419 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6420 		mydebug('ROLLBACK ' );
6421 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
6422 		mydebug('RAISE fnd_api.g_exc_unexpected_error: ' || SQLERRM);
6423 		mydebug('x_msg_count/Data = ' || x_msg_count || '/'|| x_msg_data);
6424 		ROLLBACK ;
6425    WHEN OTHERS THEN
6426 		x_return_status  := l_g_ret_sts_unexp_error;
6427 		fnd_message.set_name('WMS', 'WMS_INTERNAL_ERROR'); --NEWMSG
6428 		-- Internal Error $ROUTINE
6429 		fnd_message.set_token('ROUTINE', '- Process_f2 API' );
6430 		fnd_msg_pub.ADD;
6431 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6432 		mydebug('ROLLBACK ' );
6433 		ROLLBACK ;
6434 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
6435 		mydebug('fnd_api.g_exc_error: ' || SQLERRM);
6436 
6437 
6438 END process_F2;
6439 
6440 
6441 PROCEDURE  proc_device_call
6442 			  (p_action                          IN          VARCHAR2
6443 			  ,p_employee_id                     IN          NUMBER
6444 			  ,p_transaction_temp_id             IN          NUMBER
6445 			  ,x_return_status                   OUT NOCOPY  VARCHAR2
6446 			  ,x_msg_count                       OUT NOCOPY  NUMBER
6447 			  ,x_msg_data                        OUT NOCOPY  VARCHAR2 )
6448 IS
6449 	l_debug                 NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
6450 	l_proc_name             VARCHAR2(30) :=  'PROC_DEVICE_CALL';
6451 	l_progress              VARCHAR2(30) :=  '100';
6452 	l_dev_request_msg       VARCHAR2(1000) := NULL;
6453 
6454 	  -- select all tasks that belong to this employee
6455 	  CURSOR cur_wdt_for_emp IS
6456 	  SELECT transaction_temp_id
6457 		   , device_request_id
6458 		FROM wms_dispatched_tasks
6459 	   WHERE person_id = p_employee_id
6460 	   AND (  status  <= l_g_task_dispatched  OR
6461 			  status  = l_g_task_active)    -- (<=3, OR 9 ) ;
6462 		 AND device_request_id IS NOT NULL;
6463 
6464 	  -- select task that belong to this employee and p_transaction_temp_id
6465 	  CURSOR cur_wdt_for_temp_id IS
6466 	  SELECT transaction_temp_id
6467 		   , device_request_id
6468 		FROM wms_dispatched_tasks
6469 	   WHERE person_id = p_employee_id
6470 		 AND transaction_temp_id = p_transaction_temp_id
6471 		 AND device_request_id IS NOT NULL;
6472 
6473 BEGIN
6474 	mydebug ('IN : ' || l_proc_name);
6475 	mydebug ('p_action : ' || p_action);
6476 	mydebug ('p_employee_id : ' || p_employee_id);
6477 	x_return_status  := fnd_api.g_ret_sts_success;
6478 
6479 	IF p_action is NULL OR p_action <> 'CMS' THEN
6480 	   For rec_wdt_for_emp IN cur_wdt_for_emp
6481 	   LOOP
6482 		 l_progress := '110';
6483 		 mydebug('transaction_temp_id : ' || rec_wdt_for_emp.transaction_temp_id );
6484 		 mydebug('device_request_id : ' || rec_wdt_for_emp.device_request_id );
6485 		 IF rec_wdt_for_emp.transaction_temp_id IS NOT NULL
6486 		 THEN
6487 			wms_device_integration_pvt.device_request(
6488 					  p_bus_event            => wms_device_integration_pvt.wms_be_task_cancel
6489 					, p_call_ctx             => 'U'
6490 					, p_task_trx_id          => rec_wdt_for_emp.transaction_temp_id
6491 					, p_request_id           => rec_wdt_for_emp.device_request_id
6492 					, x_request_msg          => l_dev_request_msg
6493 					, x_return_status        => x_return_status
6494 					, x_msg_count            => x_msg_count
6495 					, x_msg_data             => x_msg_data );
6496 			IF x_return_status <> l_g_ret_sts_success THEN
6497 			   mydebug('x_return_status : ' || x_return_status);
6498 			   RAISE fnd_api.G_EXC_ERROR;
6499 			END IF;
6500 
6501 		 END IF;
6502 	   END LOOP;
6503 	ELSE
6504 	   For rec_wdt_for_temp_id IN cur_wdt_for_temp_id
6505 	   LOOP
6506 		 l_progress := '210';
6507 		 mydebug('transaction_temp_id : ' || rec_wdt_for_temp_id.transaction_temp_id );
6508 		 mydebug('device_request_id : ' || rec_wdt_for_temp_id.device_request_id );
6509 		 IF rec_wdt_for_temp_id.transaction_temp_id IS NOT NULL
6510 		 THEN
6511 			wms_device_integration_pvt.device_request(
6512 					  p_bus_event            => wms_device_integration_pvt.wms_be_task_cancel
6513 					, p_call_ctx             => 'U'
6514 					, p_task_trx_id          => rec_wdt_for_temp_id.transaction_temp_id
6515 					, p_request_id           => rec_wdt_for_temp_id.device_request_id
6516 					, x_request_msg          => l_dev_request_msg
6517 					, x_return_status        => x_return_status
6518 					, x_msg_count            => x_msg_count
6519 					, x_msg_data             => x_msg_data );
6520 			IF x_return_status <> l_g_ret_sts_success THEN
6521 			   mydebug('x_return_status : ' || x_return_status);
6522 			   RAISE fnd_api.G_EXC_ERROR;
6523 			END IF;
6524 
6525 		 END IF;
6526 	   END LOOP;
6527 	END IF;
6528 	mydebug ('END : ' || l_proc_name);
6529   EXCEPTION
6530   WHEN fnd_api.g_exc_error THEN
6531 		x_return_status  := l_g_ret_sts_error;
6532 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6533 		mydebug('ROLLBACK ' );
6534 		ROLLBACK ;
6535 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
6536 		mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
6537    WHEN fnd_api.g_exc_unexpected_error THEN
6538 		x_return_status  := fnd_api.g_ret_sts_unexp_error;
6539 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6540 		mydebug('ROLLBACK ' );
6541 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
6542 		mydebug('RAISE fnd_api.g_exc_unexpected_error: ' || SQLERRM);
6543 		mydebug('x_msg_count/Data = ' || x_msg_count || '/'|| x_msg_data);
6544 		ROLLBACK ;
6545    WHEN OTHERS THEN
6546 		x_return_status  := l_g_ret_sts_unexp_error;
6547 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6548 		mydebug('ROLLBACK ' );
6549 		ROLLBACK ;
6550 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
6551 		mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
6552 
6553 END   proc_device_call ;
6554 
6555 PROCEDURE  proc_process_cancelled_MOLs (
6556 			   p_organization_id        IN             NUMBER
6557 			  ,p_user_id                IN             NUMBER
6558 			  ,p_transaction_header_id  IN             NUMBER
6559 			  ,p_transaction_temp_id    IN             NUMBER
6560 			  ,x_return_status          OUT NOCOPY     VARCHAR2
6561 			  ,x_msg_count              OUT NOCOPY     NUMBER
6562 			  ,x_msg_data               OUT NOCOPY     VARCHAR2)
6563 IS
6564 	l_debug                 NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
6565 	l_proc_name             VARCHAR2(30) :=  'PROC_PROCESS_CANCELLED_MOLs';
6566 	l_progress              VARCHAR2(30) :=  '100';
6567 
6568 	l_deleted_mmtt_qty      NUMBER       := 0;
6569 	l_deleted_mmtt_sec_qty  NUMBER       := 0;
6570 	l_mmtt_count            NUMBER       := 0;
6571 	-- Select all MOLs that are cancelled , so that cancelled tasks can be reduced
6572 	CURSOR   cur_cancelled_MOLs IS
6573 	  SELECT mtrl.line_id
6574 		   , mtrl.uom_code
6575 		FROM mtl_material_transactions_temp mmtt
6576 		   , mtl_txn_request_lines mtrl
6577 	   WHERE (mmtt.transaction_temp_id = p_transaction_temp_id
6578 		-- shld add : and mmtt.mmtt.transaction_temp_id <> mmtt.parent_line_id
6579 		   OR mmtt.parent_line_id      = p_transaction_temp_id)
6580 		 AND mtrl.line_id = mmtt.move_order_line_id
6581 		 AND mtrl.line_status = INV_GLOBALS.G_TO_STATUS_CANCEL_BY_SOURCE;
6582 
6583 	-- all mmtts for the given MOL
6584 	-- this MMTT  should not have any child records
6585 	-- this MMTT should not have a task in WDT
6586 	CURSOR  c_mmtt_to_del (p_mo_line_id    NUMBER )
6587 	IS
6588 	  SELECT mmtt.transaction_temp_id
6589 		   , mmtt.inventory_item_id
6590 		   , mmtt.primary_quantity
6591 		   , mmtt.item_primary_uom_code
6592 		   , NVL(mmtt.secondary_transaction_quantity, 0) secondary_transaction_quantity
6593 		FROM mtl_material_transactions_temp mmtt
6594 	   WHERE mmtt.move_order_line_id = p_mo_line_id
6595 		 AND NOT EXISTS(SELECT 1
6596 						  FROM mtl_material_transactions_temp t1
6597 						 WHERE t1.parent_line_id = mmtt.transaction_temp_id)
6598 		 AND NOT EXISTS(SELECT 1
6599 						  FROM wms_dispatched_tasks wdt
6600 						 WHERE wdt.transaction_temp_id = mmtt.transaction_temp_id);
6601 
6602 BEGIN
6603 	mydebug ('IN : ' || l_proc_name);
6604 	x_return_status  := fnd_api.g_ret_sts_success;
6605 	FOR rec_cancelled_MOLs in cur_cancelled_MOLs
6606 	LOOP
6607 	  IF (l_debug = 1) THEN mydebug('mo_line_id = ' || rec_cancelled_mols.line_id); END IF;
6608 	  l_deleted_mmtt_qty      := 0;
6609 	  l_deleted_mmtt_sec_qty  := 0;
6610 
6611 	  FOR rec_mmtt_to_del   IN  c_mmtt_to_del (
6612 								p_mo_line_id  => rec_cancelled_MOLs.line_id)
6613 	  LOOP
6614 		 -- it adjusts the bulk parent too
6615 		 inv_trx_util_pub.delete_transaction(
6616 		   x_return_status       => x_return_status
6617 		 , x_msg_data            => x_msg_data
6618 		 , x_msg_count           => x_msg_count
6619 		 , p_transaction_temp_id => rec_mmtt_to_del.transaction_temp_id);
6620 		 IF x_return_status <> fnd_api.g_ret_sts_success THEN
6621 			IF l_debug = 1 THEN
6622 			   mydebug('Not able to delete the Txn = ' || rec_mmtt_to_del.transaction_temp_id);
6623 			END IF;
6624 			RAISE fnd_api.g_exc_unexpected_error;
6625 		 END IF;
6626 
6627 		 IF (rec_mmtt_to_del.item_primary_uom_code <> rec_cancelled_mols.uom_code)
6628 		 THEN
6629 			 l_deleted_mmtt_qty  := l_deleted_mmtt_qty +
6630 									INV_Convert.inv_um_convert
6631 									   ( item_id         => rec_mmtt_to_del.inventory_item_id
6632 										,precision       => 5
6633 										,from_quantity   => rec_mmtt_to_del.primary_quantity
6634 										,from_unit       => rec_mmtt_to_del.item_primary_uom_code
6635 										,to_unit         => rec_cancelled_mols.uom_code
6636 										,from_name       => NULL
6637 										,to_name         => NULL);
6638 
6639 
6640 		 ELSE
6641 			l_deleted_mmtt_qty  := l_deleted_mmtt_qty + rec_mmtt_to_del.primary_quantity;
6642 		 END IF;
6643 			l_deleted_mmtt_sec_qty  := l_deleted_mmtt_sec_qty + rec_mmtt_to_del.secondary_transaction_quantity;
6644 	  END LOOP;
6645 	   -- all MMTTs for the given MOL, this MMTT  should not have any child records
6646 
6647 	  SELECT count(*)
6648 		INTO l_mmtt_count
6649 		FROM mtl_material_transactions_temp mmtt
6650 	   WHERE mmtt.move_order_line_id = rec_cancelled_mols.line_id
6651 		 AND NOT EXISTS ( SELECT 1
6652 							FROM mtl_material_transactions_temp t1
6653 						   WHERE t1.parent_line_id = mmtt.transaction_temp_id);
6654 
6655 	  UPDATE mtl_txn_request_lines
6656 		 SET quantity_detailed =(quantity_detailed - l_deleted_mmtt_qty)
6657 		   , SECONDARY_QUANTITY_DETAILED = (SECONDARY_QUANTITY_DETAILED - l_deleted_mmtt_sec_qty)
6658 		   , line_status = DECODE(l_mmtt_count, 0, INV_GLOBALS.G_TO_STATUS_CLOSED, line_status)
6659 		   , last_update_date = SYSDATE
6660 		   , last_updated_by  = p_user_id
6661 	   WHERE line_id = rec_cancelled_mols.line_id;
6662 	   IF SQL%NOTFOUND THEN
6663 		  fnd_message.set_name('WMS', 'WMS_INTERNAL_ERROR'); --NEWMSG
6664 		  -- Internal Error $ROUTINE
6665 		  fnd_message.set_token('ROUTINE', '-proc_process_cancelled_MOLs' );
6666 		  myDebug('Error updating MTRL in proc_process_cancelled_MOLs for line: ' || rec_cancelled_mols.line_id);
6667 		   fnd_msg_pub.ADD;
6668 	   END IF;
6669 	END LOOP;
6670 	mydebug('END = ' || l_proc_name );
6671   EXCEPTION
6672   WHEN fnd_api.g_exc_error THEN
6673 		x_return_status  := l_g_ret_sts_error;
6674 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6675 		mydebug('ROLLBACK ' );
6676 		ROLLBACK ;
6677 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
6678 		mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
6679    WHEN fnd_api.g_exc_unexpected_error THEN
6680 		x_return_status  := fnd_api.g_ret_sts_unexp_error;
6681 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6682 		mydebug('ROLLBACK ' );
6683 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
6684 		mydebug('RAISE fnd_api.g_exc_unexpected_error: ' || SQLERRM);
6685 		mydebug('x_msg_count/Data = ' || x_msg_count || '/'|| x_msg_data);
6686 		ROLLBACK ;
6687    WHEN OTHERS THEN
6688 		x_return_status  := l_g_ret_sts_unexp_error;
6689 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6690 		mydebug('ROLLBACK ' );
6691 		ROLLBACK ;
6692 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
6693 		mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
6694 
6695 END  proc_process_cancelled_MOLs ;
6696 
6697 PROCEDURE  proc_reset_lpn_context(
6698 			   p_organization_id                 IN          NUMBER
6699 			  ,p_user_id                         IN          NUMBER
6700 			  ,p_transaction_header_id           IN          NUMBER
6701 			  ,p_transaction_temp_id             IN          NUMBER
6702 			  ,x_return_status                   OUT NOCOPY  VARCHAR2
6703 			  ,x_msg_count                       OUT NOCOPY  NUMBER
6704 			  ,x_msg_data                        OUT NOCOPY  VARCHAR2)
6705 IS
6706 	l_proc_name                   VARCHAR2(30) :=  'PROC_RESET_LPN_CONTEXT';
6707 	l_progress                    VARCHAR2(30) :=  '100';
6708 	l_other_tasks                 NUMBER := 0;
6709 	l_lpn_context_pregenerated    CONSTANT NUMBER := WMS_Container_PUB.LPN_CONTEXT_PREGENERATED;
6710 	l_lpn_context_inv             CONSTANT NUMBER := WMS_Container_PUB.LPN_CONTEXT_INV;
6711 	l_lpn_context_picked          CONSTANT NUMBER := WMS_Container_PUB.LPN_CONTEXT_PICKED;
6712 	l_lpn_context_packing         CONSTANT NUMBER := WMS_Container_PUB.LPN_CONTEXT_PACKING ;
6713 
6714 CURSOR cur_from_lpns IS
6715    SELECT DISTINCT lpn_id
6716 	 FROM mtl_material_transactions_temp
6717 	WHERE transaction_header_id = p_transaction_header_id
6718 	  AND lpn_id IS NOT NULL;
6719 
6720 CURSOR cur_content_lpns IS
6721    SELECT DISTINCT content_lpn_id
6722 	 FROM mtl_material_transactions_temp
6723 	WHERE transaction_header_id = p_transaction_header_id
6724 	  AND content_lpn_id IS NOT NULL;
6725 
6726 --modified for bug 6642448
6727  CURSOR cur_transfer_lpns IS
6728    SELECT DISTINCT transfer_lpn_id
6729 	 FROM mtl_material_transactions_temp mmtt
6730 	WHERE transaction_header_id = p_transaction_header_id
6731 	  AND nvl(content_lpn_id , nvl(lpn_id,-999)) <> transfer_lpn_id
6732 	aND not exists ( select 1 from mtl_material_transactions_temp mmtt1
6733 	 where mmtt1.transaction_header_id = p_transaction_header_id
6734 	  and mmtt.transfer_lpn_id=mmtt1.transfer_lpn_id
6735 	 and (mmtt1.lpn_id=mmtt1.transfer_lpn_id or
6736 						   mmtt1.content_lpn_id=mmtt1.transfer_lpn_id  )); --BUG 12803567
6737 
6738 BEGIN
6739    x_return_status  := fnd_api.g_ret_sts_success;
6740    mydebug ('IN : ' || l_proc_name);
6741    l_progress := 110;
6742    mydebug('l_progress =  ' || l_progress);
6743    mydebug ('p_transaction_header_id : ' || p_transaction_header_id);
6744 
6745    FOR rec_transfer_lpns IN cur_transfer_lpns
6746    LOOP
6747 	  l_other_tasks := 0;
6748 	  l_progress := 120;
6749 	  mydebug('rec_transfer_lpns.transfer_lpn_id =  ' || rec_transfer_lpns.transfer_lpn_id);
6750 	  mydebug('l_progress =  ' || l_progress);
6751 	  BEGIN
6752 		   SELECT 1
6753 			INTO l_other_tasks
6754 			FROM DUAL
6755 		   WHERE EXISTS(SELECT 1
6756 						 FROM mtl_material_transactions_temp
6757 						WHERE transaction_header_id <> p_transaction_header_id
6758 						  AND transfer_lpn_id = rec_transfer_lpns.transfer_lpn_id);
6759 	  EXCEPTION
6760 	  WHEN NO_DATA_FOUND THEN
6761 		 l_other_tasks := 0;
6762 	  END ;
6763 	  IF l_other_tasks = 0 THEN
6764 		 l_progress := 130;
6765 		 mydebug('l_progress =  ' || l_progress);
6766 		 -- Bug5659809: update last_update_date and last_update_by as well
6767 		 UPDATE wms_license_plate_numbers
6768 			SET lpn_context = l_lpn_context_PREGENERATED
6769 			 -- , last_update_date = SYSDATE  /* Bug 9448490 Lot Substitution Project */
6770 			 -- , last_updated_by = fnd_global.user_id /* Bug 9448490 Lot Substitution Project */
6771 		  WHERE lpn_id = rec_transfer_lpns.transfer_lpn_id
6772 			AND lpn_context <> l_lpn_context_picked;
6773 
6774 		 IF SQL%NOTFOUND THEN
6775 			mydebug (rec_transfer_lpns.transfer_lpn_id || 'with context <> '
6776 													   || l_lpn_context_picked || ' Not found ');
6777 		 END IF;
6778 	  END IF;
6779    END LOOP;
6780    l_progress := 200;
6781    mydebug('l_progress =  ' || l_progress);
6782    FOR rec_from_lpns IN cur_from_lpns
6783    LOOP
6784 		l_progress := 220;
6785 		mydebug('rec_from_lpns.lpn_id =  ' || rec_from_lpns.lpn_id);
6786 		mydebug('l_progress =  ' || l_progress);
6787 		-- we need to do this since mmtt.lpn_id can be a fully consumable lpn in
6788 		-- case where xferLPN is enabled on pickload UI.
6789 		 -- Bug5659809: update last_update_date and last_update_by as well
6790 		 UPDATE wms_license_plate_numbers
6791 			SET lpn_context = l_lpn_context_INV
6792 			  --, last_update_date = SYSDATE  /* Bug 9448490 Lot Substitution Project */
6793 			  --, last_updated_by = fnd_global.user_id /* Bug 9448490 Lot Substitution Project */
6794 		  WHERE lpn_id = rec_from_lpns.lpn_id
6795 			AND lpn_context = l_lpn_context_packing;
6796 
6797 		 IF SQL%NOTFOUND THEN
6798 			mydebug (rec_from_lpns.lpn_id || 'not found with context = packing ' );
6799 		 END IF;
6800    END LOOP;
6801    l_progress := 300;
6802    mydebug('l_progress =  ' || l_progress);
6803    FOR rec_content_lpns IN cur_content_lpns
6804    LOOP
6805 	  l_other_tasks := 0;
6806 	  l_progress := 310;
6807 	  mydebug('rec_content_lpns.content_lpn_id =  ' || rec_content_lpns.content_lpn_id);
6808 	  mydebug('l_progress =  ' || l_progress);
6809 	  BEGIN
6810 		 SELECT 1
6811 		  INTO l_other_tasks
6812 		  FROM DUAL
6813 		 WHERE EXISTS(SELECT 1
6814 					   FROM mtl_material_transactions_temp
6815 					  WHERE transaction_header_id <> p_transaction_header_id
6816 						AND content_lpn_id = rec_content_lpns.content_lpn_id);
6817 	  EXCEPTION
6818 	  WHEN NO_DATA_FOUND THEN
6819 		 l_other_tasks := 0;
6820 	  END ;
6821 	  IF l_other_tasks = 0 THEN
6822 		   l_progress := 320;
6823 		   mydebug('l_progress =  ' || l_progress);
6824 		   -- Bug5659809: update last_update_date and last_update_by as well
6825 		   UPDATE wms_license_plate_numbers
6826 			  SET lpn_context = l_lpn_context_inv
6827 				--, last_update_date = SYSDATE  /* Bug 9448490 Lot Substitution Project */
6828 				--, last_updated_by = fnd_global.user_id  /* Bug 9448490 Lot Substitution Project */
6829 			WHERE lpn_id = rec_content_lpns.content_lpn_id
6830 			  AND lpn_context <> l_lpn_context_picked;
6831 
6832 		   IF SQL%NOTFOUND THEN
6833 			   mydebug (rec_content_lpns.content_lpn_id || 'with context <> '
6834 													   || l_lpn_context_picked || ' Not found ');
6835 		   END IF;
6836 	  END IF;
6837    END LOOP;
6838 
6839 mydebug('END = ' || l_proc_name );
6840 EXCEPTION
6841   WHEN fnd_api.g_exc_error THEN
6842 		x_return_status  := l_g_ret_sts_error;
6843 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6844 		mydebug('ROLLBACK ' );
6845 		ROLLBACK ;
6846 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
6847 		mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
6848    WHEN fnd_api.g_exc_unexpected_error THEN
6849 		x_return_status  := fnd_api.g_ret_sts_unexp_error;
6850 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6851 		mydebug('ROLLBACK ' );
6852 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
6853 		mydebug('RAISE fnd_api.g_exc_unexpected_error: ' || SQLERRM);
6854 		mydebug('x_msg_count/Data = ' || x_msg_count || '/'|| x_msg_data);
6855 		ROLLBACK ;
6856    WHEN OTHERS THEN
6857 		x_return_status  := l_g_ret_sts_unexp_error;
6858 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
6859 		mydebug('ROLLBACK ' );
6860 		ROLLBACK ;
6861 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
6862 		mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
6863 END proc_reset_lpn_context;
6864 
6865 PROCEDURE  proc_reset_task_status(
6866 			   p_action                          IN          VARCHAR2
6867 			  ,p_organization_id                 IN          NUMBER
6868 			  ,p_user_id                         IN          NUMBER
6869 			  ,p_employee_id                     IN          NUMBER
6870 			  ,p_transaction_header_id           IN          NUMBER
6871 			  ,p_transaction_temp_id             IN          NUMBER
6872 			  ,x_return_status                   OUT NOCOPY  VARCHAR2
6873 			  ,x_msg_count                       OUT NOCOPY  NUMBER
6874 			  ,x_msg_data                        OUT NOCOPY  VARCHAR2)
6875 IS
6876 	l_proc_name                   VARCHAR2(30) :=  'PROC_RESET_TASK_STATUS';
6877 	l_progress                    VARCHAR2(30) :=  '100';
6878 	l_other_tasks                 NUMBER := 0;
6879 	l_prev_task_status            NUMBER := 0;
6880 
6881 CURSOR cur_reset_task_status IS
6882 SELECT transaction_temp_id
6883 	  ,task_id
6884   FROM wms_dispatched_tasks
6885  WHERE  person_id = p_employee_id
6886    AND (  status  = l_g_task_dispatched  OR
6887 		  status  = l_g_task_active);  -- IN (3,9 ) ;
6888 BEGIN
6889    x_return_status  := fnd_api.g_ret_sts_success;
6890    mydebug ('IN : ' || l_proc_name);
6891    mydebug ('p_action : ' || p_action);
6892    mydebug ('p_transaction_header_id : ' || p_transaction_header_id);
6893    mydebug ('p_transaction_temp_id   : ' || p_transaction_temp_id  );
6894    mydebug ('p_employee_id : ' || p_employee_id);
6895    l_progress := 110;
6896    mydebug('l_progress =  ' || l_progress);
6897 
6898    IF p_action = 'CMS' THEN
6899 
6900 	  DELETE wms_dispatched_tasks
6901 	  WHERE  transaction_temp_id IN
6902 			(SELECT transaction_temp_id
6903 			   FROM mtl_material_transactions_temp
6904 			  WHERE transaction_header_id  = p_transaction_header_id);
6905 	  IF SQL%NOTFOUND THEN
6906 		 mydebug('no WDTs to delete for this header:' );
6907 	  ELSE
6908 		 mydebug('WDTs deleted for this header:' );
6909 	  END IF;
6910 	  RETURN;
6911    END IF;
6912 
6913 	-- The idea is to delete all tasks belonging to the same header_id (as in case of splits)
6914 	-- and also to delete all dscpatched and active tasks
6915 	-- Decide to delete all tasks with status = dispatched (3 or Active 9).
6916 	-- There is no need to check for a specific header_id . For F2, we anyway always
6917 	-- delete all dispatched and active tasks
6918 
6919 	BEGIN
6920 	   l_prev_task_status := wms_picking_pkg.g_previous_task_status(p_transaction_temp_id);
6921 	   wms_picking_pkg.g_previous_task_status.delete(p_transaction_temp_id);
6922 	EXCEPTION
6923 	WHEN OTHERS THEN
6924 	   mydebug('wms_picking_pkg.g_previous_task_status(p_transaction_temp_id) : ' || p_transaction_temp_id
6925 					   || ' : not found' );
6926 	   l_prev_task_status := l_g_task_pending;
6927 	END ;
6928 
6929 	l_progress := 110;
6930 	mydebug('l_progress =  ' || l_progress || ' Update status for all temp_ids in thie header_id');
6931 	mydebug('l_prev_task_status = ' || l_prev_task_status || ' for p_transaction_temp_id:  '
6932 														  || p_transaction_temp_id);
6933 	/* this update is seperate because for a pick nmore case, there can be multiple temp_ids (MMTTs)
6934 	   for the given header id ...current task) */
6935 	UPDATE  wms_dispatched_tasks
6936 	   SET  status = l_prev_task_status
6937 		   ,last_update_date = SYSDATE
6938 		   ,last_updated_by = p_user_id
6939 	 WHERE  person_id = p_employee_id
6940 	   AND (  status  = l_g_task_dispatched  OR
6941 			  status  = l_g_task_active)  -- IN (3,9 ) ;
6942 	   AND  transaction_temp_id in (SELECT transaction_temp_id
6943 									  FROM mtl_material_transactions_temp
6944 									 WHERE transaction_header_id = p_transaction_header_id);
6945 
6946 	IF SQL%NOTFOUND THEN
6947 		mydebug('no WDT to update  for this employee id  for this header id with stat in ( 3,9) ' );
6948 	END IF;
6949 
6950 	l_progress := 150;
6951 	mydebug('l_progress =  ' || l_progress || ' Update status of all other tasks in this group' );
6952 	FOR rec_reset_task_status IN cur_reset_task_status
6953 	LOOP
6954 	   BEGIN
6955 		  l_prev_task_status := wms_picking_pkg.g_previous_task_status(rec_reset_task_status.transaction_temp_id);
6956 		  wms_picking_pkg.g_previous_task_status.delete(rec_reset_task_status.transaction_temp_id);
6957 	   EXCEPTION
6958 		  WHEN OTHERS THEN
6959 			  mydebug('wms_picking_pkg.g_previous_task_status(transaction_temp_id) : '
6960 					   || rec_reset_task_status.transaction_temp_id || ' : not found' );
6961 			  l_prev_task_status := l_g_task_pending;
6962 	   END ;
6963 		  mydebug('l_prev_task_status = ' || l_prev_task_status || ' for transaction_temp_id:  '
6964 										  || rec_reset_task_status.transaction_temp_id);
6965 		  UPDATE  wms_dispatched_tasks
6966 			 SET  status =  l_prev_task_status
6967 				 ,last_update_date = SYSDATE
6968 				 ,last_updated_by = p_user_id
6969 		   WHERE  task_id = rec_reset_task_status.task_id;
6970 
6971 		  IF SQL%NOTFOUND THEN
6972 			  mydebug('task_id : ' || rec_reset_task_status.task_id || ' : not found to be updated');
6973 		  END IF;
6974 
6975 	END LOOP ;
6976 
6977 	l_progress := 200;
6978 	mydebug('l_progress =  ' || l_progress);
6979 
6980 	DELETE  wms_dispatched_tasks
6981 	 WHERE  person_id = p_employee_id
6982 	  AND (status  = l_g_task_pending OR
6983 		   status  = l_g_task_dispatched  OR
6984 		   status  = l_g_task_active) ; -- IN (3,9 ) ;
6985 
6986 	IF SQL%NOTFOUND THEN
6987 		mydebug('no WDT with status 3,9,1 remaining to delete for this employee id   ' );
6988 	   --It is OK not to find even one task to delete
6989 	ELSE
6990 	   mydebug('Deleted all WDT with staus 3,9,1 for p_employee_id =  ' || p_employee_id);
6991 	END IF;
6992 
6993 	l_progress := 300;
6994 	mydebug('l_progress =  ' || l_progress);
6995 	UPDATE  wms_dispatched_tasks
6996 	   SET  task_method = NULL  -- for cluster picking
6997 	 WHERE  person_id = p_employee_id
6998 	   AND  status = l_g_task_queued;
6999 
7000 	IF SQL%NOTFOUND THEN
7001 		mydebug('no WDT to update for this employee id to be updated for cluster picking case' );
7002 	   --It is OK not to find even one task to update
7003 	END IF;
7004 
7005 mydebug('END = ' || l_proc_name );
7006 EXCEPTION
7007   WHEN fnd_api.g_exc_error THEN
7008 		x_return_status  := l_g_ret_sts_error;
7009 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7010 		mydebug('ROLLBACK ' );
7011 		ROLLBACK ;
7012 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
7013 		mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
7014    WHEN fnd_api.g_exc_unexpected_error THEN
7015 		x_return_status  := fnd_api.g_ret_sts_unexp_error;
7016 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7017 		mydebug('ROLLBACK ' );
7018 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
7019 		mydebug('RAISE fnd_api.g_exc_unexpected_error: ' || SQLERRM);
7020 		mydebug('x_msg_count/Data = ' || x_msg_count || '/'|| x_msg_data);
7021 		ROLLBACK ;
7022    WHEN OTHERS THEN
7023 		x_return_status  := l_g_ret_sts_unexp_error;
7024 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7025 		mydebug('ROLLBACK ' );
7026 		ROLLBACK ;
7027 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
7028 		mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
7029 END proc_reset_task_status;
7030 
7031  --viks procedure proc_start_over
7032  /*{{
7033  * When startover button is pressed proc_start_over is called which would return
7034  * no of task to be processed
7035  *}}
7036   */
7037 
7038 
7039 PROCEDURE proc_start_over
7040 			 (p_transaction_header_id       IN NUMBER
7041 					 ,p_transaction_temp_id  IN NUMBER
7042 					 ,p_user_id             IN  NUMBER
7043 					 ,x_start_over_taskno   OUT NOCOPY NUMBER
7044 					 ,x_return_status       OUT NOCOPY VARCHAR2
7045 					 ,x_msg_count           OUT NOCOPY NUMBER
7046 					 ,x_msg_data            OUT  NOCOPY VARCHAR2 )
7047 
7048 IS
7049 	l_proc_name                   VARCHAR2(30) :=  'PROC_START_OVER';
7050 	l_progress                    VARCHAR2(30) :=  '100';
7051 	l_debug                       NUMBER       :=  NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
7052 	l_prev_task_status    NUMBER     :=0;
7053 	l_last_index          NUMBER := NULL;
7054 	l_first_index         NUMBER := NULL;
7055 	l_existing_count      NUMBER :=0;
7056 	l_move_index_offset   NUMBER :=0;
7057 	new_mmtt_count            NUMBER := 0;
7058 	m_count               NUMBER :=0;
7059 	i               NUMBER :=0;
7060 	p               NUMBER :=0;
7061 	L              NUMBER :=0;
7062 
7063 
7064  CURSOR   tempid_cur  (v_transaction_temp_id  NUMBER)  IS
7065   SELECT transaction_temp_id from mtl_allocations_gtmp
7066   WHERE transaction_temp_id <> v_transaction_temp_id
7067   ORDER BY  transaction_temp_id;
7068 
7069 
7070 BEGIN
7071 
7072 	x_return_status  := fnd_api.g_ret_sts_success;
7073 	mydebug ('IN : ' || l_proc_name);
7074    mydebug ('p_transaction_header_id : ' || p_transaction_header_id);
7075    mydebug ('p_transaction_temp_id   : ' || p_transaction_temp_id  );
7076    l_progress := 110;
7077 
7078   l_prev_task_status := wms_picking_pkg.g_previous_task_status(p_transaction_temp_id);
7079 
7080  SELECT count(*)  into new_mmtt_count from mtl_allocations_gtmp;
7081    mydebug('Total count in mtl_allocations_gtemp: ' || new_mmtt_count);
7082 
7083   l_first_index := wms_picking_pkg.g_start_over_tempid.first;
7084   l_existing_count := wms_picking_pkg.g_start_over_tempid.count-1;
7085 
7086   mydebug('l_first_index is : ' ||l_first_index);
7087   mydebug('l_existing_count is : ' || l_existing_count);
7088 
7089   IF  l_first_index IS NULL THEN
7090 	  l_first_index  :=0 ;
7091   END IF;
7092 
7093    --setting the sequence space
7094 
7095   IF l_first_index >0 and new_mmtt_count >0 THEN
7096 	  l_move_index_offset := (new_mmtt_count+1) - l_first_index;
7097 	  mydebug('Inl_move_index_offset:'||l_move_index_offset);
7098 	  if (l_move_index_offset >0) then  -- move forward
7099 		for i in REVERSE l_first_index .. (l_first_index+l_existing_count) LOOP
7100 		 wms_picking_pkg.g_start_over_tempid(i+l_move_index_offset) := wms_picking_pkg.g_start_over_tempid(i);
7101 		mydebug('In else index :' ||i);
7102 		END LOOP;
7103 
7104 	  elsif (l_move_index_offset <0) then -- move backward
7105 		  for i in l_first_index..l_first_index+l_existing_count LOOP
7106 		   wms_picking_pkg.g_start_over_tempid(i+l_move_index_offset) := wms_picking_pkg.g_start_over_tempid(i);
7107 		   wms_picking_pkg.g_start_over_tempid.DELETE(i);
7108 		mydebug('In else if index offset is Neg:' ||wms_picking_pkg.g_start_over_tempid(i+l_move_index_offset));
7109 		mydebug('In elseif index :' ||i);
7110 		  END LOOP;
7111 	 end if;
7112   END IF;
7113    mydebug('Cont of table :'||wms_picking_pkg.g_start_over_tempid.COUNT);
7114   -- insert  temp id value into pl/sql table
7115 
7116    p := 1;
7117    FOR tempid_rec IN tempid_cur(v_transaction_temp_id => p_transaction_temp_id)
7118    LOOP
7119 	 wms_picking_pkg.g_start_over_tempid(p) := tempid_rec.transaction_temp_id;
7120 	 mydebug('Temp ids in plsql :'||wms_picking_pkg.g_start_over_tempid(p)||'Index:'||p);
7121 	wms_picking_pkg.g_previous_task_status(wms_picking_pkg.g_start_over_tempid(p)) :=l_prev_task_status;
7122 	 p := p +1;
7123 	  IF SQL%NOTFOUND THEN
7124 		  mydebug('transaction_temp_id  not found only one temp_id present: ');
7125 		   p :=1;
7126 		  END IF;
7127 
7128    END LOOP;
7129 
7130    wms_picking_pkg.g_start_over_tempid(p):= p_transaction_temp_id;
7131    mydebug('Temp id sent last is :'||wms_picking_pkg.g_start_over_tempid(p) ||'Index is::'||p);
7132 
7133   L:=0;
7134 
7135  -- Updating wdd status and printing  final values sent in pl/sql table
7136 
7137 	forall L in wms_picking_pkg.g_start_over_tempid.FIRST .. wms_picking_pkg.g_start_over_tempid.LAST
7138 	   UPDATE  wms_dispatched_tasks
7139 	   SET  status = l_g_task_dispatched
7140 		   ,last_update_date = SYSDATE
7141 		   ,last_updated_by = p_user_id
7142 	   WHERE transaction_temp_id = wms_picking_pkg.g_start_over_tempid(L);
7143 	   IF SQL%NOTFOUND THEN
7144 		  mydebug('transaction_temp_id : ' ||wms_picking_pkg.g_start_over_tempid(L) ||' : not found to be updated');
7145 	   END IF;
7146 
7147 	IF (l_debug = 1) THEN
7148 	 L :=0;
7149 	FOR  L IN  wms_picking_pkg.g_start_over_tempid.FIRST .. wms_picking_pkg.g_start_over_tempid.LAST
7150 	LOOP
7151 	mydebug('values sent are :' || wms_picking_pkg.g_start_over_tempid(L) || 'value of L' ||L);
7152 	  IF SQL%NOTFOUND THEN
7153 		  mydebug('transaction_temp_id : ' ||wms_picking_pkg.g_start_over_tempid(L) ||' : not found to be updated');
7154 		  END IF;
7155 	END LOOP;
7156    END IF;
7157 
7158    x_start_over_taskno := wms_picking_pkg.g_start_over_tempid.count;
7159 	mydebug('x_start_over_taskno in procedure start_over::'||x_start_over_taskno);
7160    mydebug('END = ' || l_proc_name );
7161 EXCEPTION
7162   WHEN fnd_api.g_exc_error THEN
7163 		x_return_status  := l_g_ret_sts_error;
7164 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7165 		mydebug('ROLLBACK ' );
7166 		ROLLBACK ;
7167 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
7168 		mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
7169    WHEN fnd_api.g_exc_unexpected_error THEN
7170 		x_return_status  := fnd_api.g_ret_sts_unexp_error;
7171 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7172 		mydebug('ROLLBACK ' );
7173 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
7174 		mydebug('RAISE fnd_api.g_exc_unexpected_error: ' || SQLERRM);
7175 		mydebug('x_msg_count/Data = ' || x_msg_count || '/'|| x_msg_data);
7176 		ROLLBACK ;
7177    WHEN OTHERS THEN
7178 		x_return_status  := l_g_ret_sts_unexp_error;
7179 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7180 		mydebug('ROLLBACK ' );
7181 		ROLLBACK ;
7182 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
7183 		mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
7184 
7185 END  proc_start_over;
7186 
7187 
7188 
7189 PROCEDURE proc_parse_lot_serial_catchwt
7190 			  (p_inventory_item_id               IN          NUMBER
7191 			  ,p_organization_id 				 IN 		 NUMBER
7192 			  ,p_fulfillment_base                IN          VARCHAR2
7193 			  ,p_confirmed_lots                  IN          VARCHAR2
7194 			  ,p_confirmed_lot_trx_qty           IN          VARCHAR2
7195 			  ,p_confirmed_lot_pri_qty           IN          VARCHAR2
7196 			  ,p_confirmed_serials               IN          VARCHAR2
7197 			  ,p_suggested_uom                   IN          VARCHAR2
7198 			  ,p_confirmed_uom                   IN          VARCHAR2
7199 			  ,p_primary_uom                     IN          VARCHAR2
7200 			  ,p_confirmed_prim_qty              IN          VARCHAR2
7201 			  ,p_confirmed_sugg_qty              IN          VARCHAR2
7202 			  ,p_confirmed_sec_uom               IN          VARCHAR2
7203 			  ,p_confirmed_sec_qty               IN          VARCHAR2
7204 			  ,x_return_status                   OUT NOCOPY  VARCHAR2
7205 			  ,x_msg_count                       OUT NOCOPY  NUMBER
7206 			  ,x_msg_data                        OUT NOCOPY  VARCHAR2)
7207 IS
7208    l_proc_name                   VARCHAR2(30)   :=  'PROC_PARSE_LOT_SERIAL_CATCHWT';
7209    l_progress                    VARCHAR2(30)   :=  '100';
7210    l_delimiter                   VARCHAR2(30)   :=  ':';
7211    l_group_delimiter             VARCHAR2(30)   :=  ';';
7212 -- Increased lot size to 80 Char - Mercy Thomas - B4625329
7213    l_lot_number                  VARCHAR2(80)   :=  NULL;
7214    l_serial_number               VARCHAR2(30)   :=  NULL;
7215    l_group_serials               VARCHAR2(30000) :=  NULL;  -- Bug 7518135
7216    l_lot_trx_qty                 NUMBER         :=  NULL;
7217    l_lot_prim_qty                NUMBER         :=  NULL;
7218    l_lot_sugg_qty                NUMBER         :=  NULL;
7219    l_sec_qty                     NUMBER         :=  NULL;
7220    -- To store a new number for each lot so that all serials for a lot will have the same number
7221    l_group_number                NUMBER         :=  0;
7222    -- To parse lots
7223    m                             NUMBER := 1;  -- position of delimiter
7224    n                             NUMBER := 1;  -- Start position for substr or search for delimiter
7225    i                             NUMBER := 1;  -- position of delimiter
7226    j                             NUMBER := 1;  -- Start position for substr or search for delimiter
7227 
7228    p                             NUMBER := 1;  -- position of delimiter
7229    q                             NUMBER := 1;  -- Start position for substr or search for delimiter
7230 
7231    -- To parse Serials
7232    x                             NUMBER := 1;  -- position of delimiter
7233    y                             NUMBER := 1;  -- Start position for substr or search for delimiter
7234    s                             NUMBER := 1;  -- position of delimiter
7235    t                             NUMBER := 1;  -- Start position for substr or search for delimiter
7236    -- To parse Secondary qty
7237    k                             NUMBER := 1;  -- position of delimiter
7238    l                             NUMBER := 1;  -- Start position for substr or search for delimiter
7239    l_number_format_mask          VARCHAR2(30) :=   'FM9999999999.99999999999999'; --Bug#6274290
7240 
7241 BEGIN
7242    x_return_status  := l_g_ret_sts_success;
7243    mydebug ('In  :' || l_proc_name );
7244 
7245    DELETE  mtl_allocations_gtmp ;
7246    IF SQL%NOTFOUND THEN
7247 	   null;
7248    END IF;
7249 
7250    WHILE  (j <> 0 AND n <> 0)
7251    LOOP
7252    -- Parse P_confirmed_lots and  p_confirmed_lot_trx_qty
7253 		  -- N is the delimiter position,
7254 		  -- M is the position from which to start looking for the first delimiter
7255 		  -- for string 'L001:L002:L003' M=1, N=5 for the first search .
7256 		  --             M=5+1=6, N=10 for the next search .
7257 		  --             M=10+1=11, N=0 for the next search because this is the last part of string.
7258 		  --serial_controlled ..p_confrimed_serials will have serials for a LOT delimited by :
7259 		  --AND  serials for different lots delimited by ';'
7260 		  --Have serials for a lot in l_lot_serials string and parse it
7261 
7262 		  l_progress    :=  '110';
7263 		  n := INSTR(p_confirmed_lots,l_delimiter,m,1);
7264 		  j := INSTR(p_confirmed_lot_trx_qty,l_delimiter,i,1);
7265 		  l_group_number := l_group_number + 1;
7266 
7267 		  mydebug ('m:' || m||':n:' || n || ':i:' || i||':j:'||j );
7268 		  IF n = 0 THEN -- Last part OF the string
7269 			 l_lot_number :=  substr(p_confirmed_lots,m,length(p_confirmed_lots));
7270 		  ELSE
7271 			 l_lot_number :=  substr(p_confirmed_lots,m,n-m) ;-- start at M get m-n chrs.
7272 			 m := n+1;
7273 		  END IF;
7274 		  mydebug ('l_lot_number:' || l_lot_number);
7275 		  -- Parse  p_confirmed_lot_trx_qty
7276 		  IF j = 0 THEN -- Last part OF the string
7277 			 l_lot_trx_qty :=  to_number(substr(p_confirmed_lot_trx_qty,i,length(p_confirmed_lot_trx_qty)) ,l_number_format_mask ) ; --Bug#6274290.
7278 		  ELSE
7279 			 l_lot_trx_qty :=  to_number(substr(p_confirmed_lot_trx_qty,i,j-i), l_number_format_mask ) ;-- start at i till i-j position
7280 			 i := j+1;
7281 		  END IF;
7282 
7283 		  mydebug ('m:' || m||':n:' || n || ':i:' || i||':j:'||j );
7284 		  mydebug ('l_lot_trx_qty:' || l_lot_trx_qty);
7285 
7286 		  IF (p_fulfillment_base = 'S') THEN --Parse p_confirmed_lot_pri_qty for primary quantity
7287 				q := INSTR(p_confirmed_lot_pri_qty,l_delimiter,p,1);
7288 				IF q = 0 THEN -- Last part OF the string
7289 					l_lot_prim_qty :=  to_number(substr(p_confirmed_lot_pri_qty,p,length(p_confirmed_lot_pri_qty)) ,l_number_format_mask ) ;
7290 				ELSE
7291 					l_lot_prim_qty :=  to_number(substr(p_confirmed_lot_pri_qty,p,q-p), l_number_format_mask ) ;
7292 					p := q+1;
7293 				END IF;
7294 		  ELSE
7295 				IF p_primary_uom <> p_confirmed_uom
7296 					THEN
7297 					l_progress    :=  '120';
7298 					l_lot_prim_qty := inv_convert.inv_um_convert
7299 									(item_id          => p_inventory_item_id
7300 									,lot_number		 => l_lot_number
7301 									,organization_id  => p_organization_id
7302 									,precision        => l_g_decimal_precision
7303 									,from_quantity    => l_lot_trx_qty
7304 									,from_unit        => p_confirmed_uom
7305 									,to_unit          => p_primary_uom
7306 									,from_name        => NULL
7307 									,to_name          => NULL);
7308 				ELSE
7309 					l_progress    :=  '130';
7310 					l_lot_prim_qty := l_lot_trx_qty;
7311 				END IF;
7312 		  END IF;
7313 		  l_progress    :=  '140';
7314 		  mydebug ('l_lot_prim_qty:' || l_lot_prim_qty);
7315 
7316 		  IF p_suggested_uom <> p_confirmed_uom
7317 			THEN
7318 			 l_progress    :=  '120';
7319 			 l_lot_sugg_qty := inv_convert.inv_um_convert
7320 							   (item_id          => p_inventory_item_id
7321 							   ,lot_number		 => l_lot_number
7322 							   ,organization_id  => p_organization_id
7323 							   ,precision        => l_g_decimal_precision
7324 							   ,from_quantity    => l_lot_trx_qty
7325 							   ,from_unit        => p_confirmed_uom
7326 							   ,to_unit          => p_suggested_uom
7327 							   ,from_name        => NULL
7328 							   ,to_name          => NULL);
7329 		  ELSE
7330 			 l_progress    :=  '130';
7331 			 l_lot_sugg_qty := l_lot_trx_qty;
7332 		  END IF;
7333 
7334 		  l_progress    :=  '140';
7335 		  mydebug ('l_lot_sugg_qty:' || l_lot_sugg_qty);
7336 
7337 		  IF (p_confirmed_sec_qty  IS NOT NULL)
7338 			AND
7339 			 (p_confirmed_lots     IS NOT NULL)
7340 		  THEN
7341 			 -- Secondary qty is at lot level (MTLT) for lot and Lot+serial controlled item
7342 			 -- Secondary qty is at MMTT level for serial controlled item
7343 			 l := INSTR(p_confirmed_sec_qty,l_delimiter,k,1);
7344 			 l_group_number := l_group_number + 1;
7345 
7346 			 mydebug ('k:' || k||':l:' || l );
7347 			 IF n = 0 THEN -- Last part OF the string
7348 				l_sec_qty := to_number( substr(p_confirmed_sec_qty,k,length(p_confirmed_sec_qty)), l_number_format_mask ); --Bug#6274290.
7349 			 ELSE
7350 				l_sec_qty := to_number( substr(p_confirmed_sec_qty,k,l-k),l_number_format_mask );-- start at k get k-l chrs.
7351 				k := l+1;
7352 			 END IF;
7353 			 mydebug ('l_sec_qty:' || l_sec_qty);
7354 
7355 		  END IF;
7356 
7357 		  IF ( p_confirmed_serials IS NOT NULL)
7358 		  THEN
7359 		-- Parse p_confirmed_serials
7360 			   -- Y is the delimiter position,
7361 			   -- X is the position from which to start looking for the first delimiter l_group_delimiter
7362 			   -- for string 'L001:L002:L003;L004:L001' X=1, Y=15 for the first search .
7363 			   --             X=15+1=16, Y=0 for the next search because this is the last part of string.
7364 			   l_group_serials := NULL;
7365 			   WHILE y <> 0  -- to substr the group
7366 			   LOOP
7367 				  y := instr(p_confirmed_serials, l_group_delimiter, x, 1);
7368 				  IF (y=0) then
7369 					 l_group_serials := substr(p_confirmed_serials,x,length(p_confirmed_serials));
7370 				  ELSE
7371 					 l_group_serials := substr(p_confirmed_serials,x,y-x);
7372 					 x := y+1;
7373 				  END IF;
7374 
7375 				  s := 1;
7376 				  t := 1;
7377 				  WHILE  (t <> 0)
7378 				  LOOP
7379 					   l_progress    :=  '110';
7380 				-- Parse l_group_serials
7381 					   -- T is the delimiter position,
7382 					   -- S is the position from which to start looking for the first delimiter
7383 					   -- for string 'L001:L002:L003' S=1, T=5 for the first search .
7384 					   --             S=5+1=6, T=10 for the next search .
7385 					   --             S=10+1=11, T=0 for the next search because this is the last part of string.
7386 					   t := nvl(INSTR(l_group_serials,l_delimiter,s,1),0);
7387 					   mydebug ('s:' || s||':t:' || t );
7388 					   IF t = 0 THEN -- Last part OF the string
7389 				   l_serial_number :=  substr(l_group_serials,s,length(l_group_serials));
7390 				ELSE
7391 				   l_serial_number :=  substr(l_group_serials,s,t-s) ;-- start at M get s-t chrs.
7392 				   s := t+1;
7393 				END IF;
7394 					   mydebug ('l_serial_number:' || l_serial_number);
7395 					   mydebug ('s:' || s||':t:' || t );
7396 					   INSERT
7397 						 INTO mtl_allocations_gtmp
7398 							   (transaction_temp_id
7399 							  , lot_number
7400 							  , serial_number
7401 							  , transaction_quantity
7402 							  , primary_quantity
7403 							  , suggested_quantity
7404 							  , secondary_quantity)
7405 					   VALUES  (l_group_number
7406 							  , l_lot_number
7407 							  , l_serial_number
7408 							  , l_lot_trx_qty
7409 							  , l_lot_prim_qty
7410 							  , l_lot_sugg_qty
7411 							  , l_sec_qty );
7412 				  END LOOP;
7413 				  EXIT;
7414 			   END LOOP;
7415 		  ELSIF (l_lot_number IS NOT NULL AND NVL(l_lot_trx_qty, -1) > 0) THEN
7416 			 INSERT
7417 			   INTO mtl_allocations_gtmp
7418 					 (transaction_temp_id
7419 					, lot_number
7420 					, serial_number
7421 					, transaction_quantity
7422 					, primary_quantity
7423 					, suggested_quantity
7424 					, secondary_quantity)
7425 			 VALUES  (l_group_number
7426 					, l_lot_number
7427 					, NULL
7428 					, l_lot_trx_qty
7429 					, l_lot_prim_qty
7430 					, l_lot_sugg_qty
7431 					, l_sec_qty );
7432 		  END IF;
7433    END LOOP;
7434 
7435    mydebug('END = ' || l_proc_name );
7436 EXCEPTION
7437    WHEN OTHERS THEN
7438 		x_return_status  := l_g_ret_sts_unexp_error;
7439 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
7440 		mydebug('ROLLBACK ' );
7441 		ROLLBACK ;
7442 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
7443 		mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
7444 
7445 END proc_parse_lot_serial_catchwt;
7446 
7447 
7448 PROCEDURE task_load(
7449 			   p_action                          IN            VARCHAR2
7450 			 , p_organization_id                 IN            NUMBER
7451 			 , p_user_id                         IN            NUMBER
7452 			 , p_person_id                       IN            NUMBER
7453 			 , p_transaction_header_id           IN            NUMBER
7454 			 , p_temp_id                         IN            NUMBER
7455 			 , p_parent_line_id                  IN            NUMBER    -- For bulk parent
7456 			 , p_lpn_id                          IN            NUMBER
7457 			 , p_content_lpn_id                  IN            NUMBER
7458 			 , p_transfer_lpn_id                 IN            NUMBER
7459 			 , p_confirmed_sub                   IN            VARCHAR2
7460 			 , p_confirmed_loc_id                IN            NUMBER
7461 			 , p_confirmed_uom                   IN            VARCHAR2
7462 			 , p_suggested_uom                   IN            VARCHAR2
7463 			 , p_primary_uom                     IN            VARCHAR2
7464 			 , p_item_id                         IN            NUMBER
7465 			 , p_revision                        IN            VARCHAR2
7466 			 , p_confirmed_qty                   IN            NUMBER
7467 			 , p_confirmed_lots                  IN            VARCHAR2
7468 			 , p_confirmed_lot_trx_qty           IN            VARCHAR2
7469 			 , p_confirmed_sec_uom               IN            VARCHAR2
7470 			 , p_confirmed_sec_qty               IN            VARCHAR2
7471 			 , p_confirmed_serials               IN            VARCHAR2
7472 			 , p_container_item_id               IN            NUMBER
7473 			 , p_transaction_type_id             IN            NUMBER
7474 			 , p_transaction_source_type_id      IN            NUMBER
7475 			 , p_lpn_match                       IN            NUMBER
7476 			 , p_lpn_match_lpn_id                IN            NUMBER
7477 			 , p_serial_allocated_flag           IN            VARCHAR2  -- Y/V or NULL
7478 			 , p_lot_controlled                  IN            VARCHAR2  -- Y/N
7479 			 , p_serial_controlled               IN            VARCHAR2  -- Y/N
7480 			 , p_effective_start_date            IN            DATE
7481 			 , p_effective_end_date              IN            DATE
7482 			 , p_exception                       IN            VARCHAR2  -- SHORT, OVER
7483 			 , p_discrepancies                   IN            VARCHAR2
7484 			 , p_qty_rsn_id                      IN            NUMBER
7485 			 , p_parent_lpn_id                   IN            NUMBER
7486 			 , p_lpnpickedasis                   IN            VARCHAR2    --Y/N
7487 			 , x_new_transaction_temp_id         OUT NOCOPY    NUMBER
7488 			 , x_cms_check                       OUT NOCOPY    VARCHAR2
7489 			 , x_return_status                   OUT NOCOPY    VARCHAR2
7490 			 , x_msg_count                       OUT NOCOPY    NUMBER
7491 			 , x_msg_data                        OUT NOCOPY    VARCHAR2
7492 			 , p_substitute_lots                 IN            VARCHAR2--/* Bug 9448490 Lot Substitution Project */
7493 			 , p_full_lot_allocation             IN            VARCHAR2 DEFAULT 'Y'  -- Added for 14699845 (Flexible Lot Allocation)
7494 			 , p_fulfillment_base                IN            VARCHAR2 DEFAULT 'P') -- 16070349
7495 IS
7496   l_return_status         VARCHAR2(1);
7497   l_msg_count             NUMBER;
7498   l_msg_data              VARCHAR2(4000);
7499   l_debug                 NUMBER:= NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
7500   l_proc_name             VARCHAR2(30) :=  'TASK_LOAD';
7501   l_progress              VARCHAR2(30) :=  '100';
7502   l_temp_id               NUMBER;
7503   l_business_flow_code    NUMBER;
7504   l_label_status          VARCHAR2(300);
7505   l_tran_type_id          NUMBER := p_transaction_type_id; -- Bug 3693953
7506   l_tran_source_type_id   NUMBER := p_transaction_source_type_id; -- Bug 3693953
7507   l_out_temp_id           NUMBER;
7508   l_multiple_pick         VARCHAR2(1) := NULL;
7509   l_overpick              VARCHAR2(1) := NULL;
7510   l_new_txn_hdr_id        NUMBER;
7511   l_lpn_quantity	  NUMBER;
7512 
7513   l_parent_line_id        NUMBER := p_parent_line_id;
7514   l_lpn_id                NUMBER := p_lpn_id;
7515   l_content_lpn_id        NUMBER := p_content_lpn_id;
7516   l_transfer_lpn_id       NUMBER := p_transfer_lpn_id;
7517   l_container_item_id     NUMBER := p_container_item_id;
7518   l_lpn_match_lpn_id      NUMBER := p_lpn_match_lpn_id;
7519   l_qty_rsn_id            NUMBER := p_qty_rsn_id;
7520   l_parent_lpn_id         NUMBER := p_parent_lpn_id;
7521   l_lpn_match             NUMBER := p_lpn_match;
7522   l_transaction_temp_id   NUMBER := p_temp_id;
7523   --Bug #4762505
7524 	--Local variables for updating MOL
7525 	l_mo_line_id            NUMBER;       --Move Order Line OD
7526 	l_mol_uom               VARCHAR2(3);  --UOM Code of the MOL
7527 	l_sum_mmtt_qty          NUMBER;       --Total MMTT primary quantity
7528 	l_mmtt_qty_in_mol_uom   NUMBER;       --Total MMTT qty in MOL UOM
7529 	--10120826
7530 	l_sum_mmtt_sec_txn_qty  NUMBER;       --Total MMTT secondary_transaction_quantity
7531 	l_sec_mol_uom           VARCHAR2(3) ; -- muom
7532 	l_mmtt_sec_qty_in_mol_uom  NUMBER;    -- muom
7533 	l_fulfillment_base      VARCHAR2(1) := p_fulfillment_base;  -- muom:16070349
7534 
7535   CURSOR mmtt_csr2(p_transaction_header_id NUMBER) IS
7536 	   SELECT mmtt.transaction_temp_id
7537 		 FROM mtl_material_transactions_temp mmtt
7538 		WHERE mmtt.transaction_header_id = p_transaction_header_id;
7539 
7540   CURSOR lot_csr IS
7541 	 SELECT lot_number, serial_transaction_temp_id
7542 	  FROM  mtl_transaction_lots_temp
7543 	  WHERE transaction_temp_id = p_temp_id;
7544 
7545   CURSOR insert_serial_allocated_csr (p_serial_lot_number  VARCHAR2) IS
7546    SELECT serial_number
7547 	FROM  mtl_serial_numbers  msn
7548 	WHERE msn.current_organization_id  = p_organization_id
7549 	AND   msn.inventory_item_id        = p_item_id
7550 	AND   lpn_id                       =  p_lpn_match_lpn_id
7551 	AND   NVL(msn.lot_number,'@@')     =  NVL(p_serial_lot_number, '@@')
7552 	AND   msn.serial_number  NOT IN
7553 		  ( select msnt.fm_serial_number
7554 			from mtl_serial_numbers_temp  msnt,
7555 				 mtl_transaction_lots_temp mtlt,
7556 				 mtl_material_transactions_temp  mmtt
7557 			where mmtt.inventory_item_id = p_item_id
7558 			 AND mmtt.organization_id = p_organization_id
7559 			 and mtlt.transaction_temp_id(+) = mmtt.transaction_temp_id
7560 			 AND msnt.transaction_temp_id = NVL(mtlt.serial_transaction_temp_id, mmtt.transaction_temp_id)
7561 			 and NVL(mtlt.lot_number, '@@') = NVL(p_serial_lot_number, '@@')
7562 			 and mmtt.transaction_temp_id = p_temp_id);
7563 
7564    CURSOR get_mmtt_qty (p_reservation_id NUMBER) IS
7565 	SELECT NVL(SUM(mtlt.primary_quantity) , SUM(mmtt.primary_quantity))
7566 	  FROM mtl_material_transactions_temp mmtt, mtl_transaction_lots_temp mtlt
7567 	 WHERE mmtt.reservation_id = p_reservation_id
7568 	   AND mmtt.transaction_temp_id = mtlt.transaction_temp_id(+);
7569 
7570 	l_new_mmtt_qty NUMBER;
7571 	l_reservation_id NUMBER;
7572 
7573 	--16448214
7574 
7575 BEGIN
7576 
7577 	  x_return_status   := fnd_api.g_ret_sts_success;
7578 	  l_return_status   := fnd_api.g_ret_sts_success;
7579 	  IF p_parent_line_id = 0    THEN l_parent_line_id := NULL;     END IF;
7580 	  IF p_lpn_id = 0            THEN l_lpn_id := NULL;             END IF;
7581 	  IF p_content_lpn_id = 0    THEN l_content_lpn_id := NULL;     END IF;
7582 	  IF p_parent_lpn_id = 0     THEN l_parent_lpn_id := NULL;      END IF;
7583 	  IF p_transfer_lpn_id = 0   THEN l_transfer_lpn_id := NULL;    END IF;
7584 	  IF p_container_item_id = 0 THEN l_container_item_id := NULL;  END IF;
7585 	  IF p_lpn_match_lpn_id = 0  THEN l_lpn_match_lpn_id := NULL;   END IF;
7586 	  IF p_qty_rsn_id = 0        THEN l_qty_rsn_id := NULL;         END IF;
7587 
7588 	  IF (l_debug = 1) THEN
7589 		mydebug ('*** Entering WMS_TASK_LOAD.task_load ***' );
7590 		mydebug ('l_progress: ' || l_progress );
7591 		mydebug('Entered..... task_Load');
7592 		mydebug('p_action:'||p_action);
7593 		mydebug('p_organization_id:'||p_organization_id);
7594 		mydebug('p_user_id:'||p_user_id);
7595 		mydebug('p_person_id:'||p_person_id);
7596 		mydebug('p_transaction_header_id:'||p_transaction_header_id);
7597 		mydebug('p_temp_id:'||p_temp_id);
7598 		mydebug('p_parent_line_id:'||p_parent_line_id ||':' || l_parent_line_id);
7599 		mydebug('p_lpn_id:' ||p_lpn_id ||':' || l_lpn_id);
7600 		mydebug('p_content_lpn_id:' ||p_content_lpn_id ||':'||l_content_lpn_id);
7601 		mydebug('p_parent_lpn_id:' ||p_parent_lpn_id ||':'||l_parent_lpn_id);
7602 		mydebug('p_transfer_lpn_id:' ||p_transfer_lpn_id ||':' || l_transfer_lpn_id);
7603 		mydebug('p_confirmed_sub:' ||p_confirmed_sub);
7604 		mydebug('p_confirmed_loc_id:' ||p_confirmed_loc_id);
7605 		mydebug('p_confirmed_uom:' ||p_confirmed_uom);
7606 		mydebug('p_suggested_uom:' ||p_suggested_uom);
7607 		mydebug('p_primary_uom  :' ||p_primary_uom  );
7608 		mydebug('p_item_id:' ||p_item_id);
7609 		mydebug('p_revision:' ||p_revision);
7610 		mydebug('p_confirmed_qty:' ||p_confirmed_qty);
7611 		mydebug('p_confirmed_lots:' ||p_confirmed_lots);
7612 		mydebug('p_confirmed_lot_trx_qty:' ||p_confirmed_lot_trx_qty);
7613 		mydebug('p_confirmed_sec_uom:' ||p_confirmed_sec_uom);
7614 		mydebug('p_confirmed_sec_qty:' ||p_confirmed_sec_qty);
7615 		mydebug('p_confirmed_serials:' ||p_confirmed_serials);
7616 		mydebug('p_container_item_id:' ||p_container_item_id ||':' || l_container_item_id);
7617 		mydebug('p_transaction_type_id: ' || p_transaction_type_id);
7618 		mydebug('p_transaction_source_type_id: ' || p_transaction_source_type_id);
7619 		mydebug('p_lpn_match:' ||p_lpn_match);
7620 		mydebug('p_lpn_match_lpn_id:' ||p_lpn_match_lpn_id || ':' || l_lpn_match_lpn_id);
7621 		mydebug('p_serial_allocated_flag:' ||p_serial_allocated_flag);
7622 		mydebug('p_lot_controlled:' ||p_lot_controlled);
7623 		mydebug('p_serial_controlled:' ||p_serial_controlled);
7624 		mydebug('p_effective_start_date:' ||p_effective_start_date);
7625 		mydebug('p_exception:' ||p_exception);
7626 		mydebug('p_discrepancies:' ||p_discrepancies);
7627 		mydebug('p_qty_rsn_id:' ||p_qty_rsn_id || ':' || l_qty_rsn_id);
7628 		mydebug('p_lpnpickedasis:'||p_lpnpickedasis);
7629 
7630 		mydebug('Lot Serial subs p_substitute_lots:'||p_substitute_lots);
7631 		mydebug('p_full_lot_allocation: '||p_full_lot_allocation);
7632 		mydebug('l_fulfillment_base:    '||l_fulfillment_base);
7633 	  END IF;
7634 
7635 	  l_progress    :=  '130';
7636 	  IF (l_debug = 1) THEN mydebug ('l_progress: ' || l_progress ); END IF;
7637 	  IF p_confirmed_qty = 0 AND p_exception <> l_g_exception_short
7638 	  THEN
7639 		 fnd_message.set_name('WMS', 'WMS_PICK_ZERO_QTY');
7640 		 -- Confirmed qty for this task is zero
7641 		 fnd_msg_pub.ADD;
7642 		 RAISE fnd_api.g_exc_unexpected_error;
7643 	  END IF;
7644 	 BEGIN
7645 		wms_picking_pkg.g_previous_task_status.delete(p_temp_id);
7646 	 EXCEPTION
7647 	 WHEN OTHERS THEN
7648 	   null; -- it is ok not to find it.
7649 	 END ;
7650 
7651 	 --Bug #4762505
7652 	 --Get the move_order_line_id for the current task
7653 	 BEGIN
7654 	   SELECT move_order_line_id
7655 	   INTO   l_mo_line_id
7656 	   FROM   mtl_material_transactions_temp
7657 	   WHERE  transaction_temp_id = p_temp_id;
7658 	 EXCEPTION
7659 	   WHEN OTHERS THEN
7660 		 l_mo_line_id := NULL;
7661 	 END;
7662 
7663 	 --8761670/8798363 start
7664 	 BEGIN
7665 		SELECT Sum(quantity) INTO l_lpn_quantity
7666 		FROM wms_lpn_contents
7667 		WHERE parent_lpn_id = p_lpn_id
7668 		AND organization_id = p_organization_id
7669 		AND inventory_item_id = p_item_id ;
7670 
7671 
7672 		IF (l_debug = 1) THEN
7673 			 mydebug ('l_lpn_quantity', l_lpn_quantity);
7674 		 END IF;
7675 
7676 	 EXCEPTION
7677 	 WHEN OTHERS THEN
7678 		NULL;
7679 	 END;
7680 /* Adding AND p_transfer_lpn_id = p_lpn_id for
7681    Bug# 12714013
7682 */
7683 	  IF l_lpn_match = 4 AND p_exception = 'OVER' AND l_lpn_quantity = p_confirmed_qty AND p_transfer_lpn_id = p_lpn_id THEN
7684 		 l_lpn_match := 3;
7685 		 l_content_lpn_id := l_lpn_id;
7686 		 l_lpn_id :=null;
7687 		 IF (l_debug = 1) THEN
7688 			mydebug ('lpn_match is 4 and total qty in lpn is to be consumed thru over picking, so changing match to 3');
7689 		 END IF;
7690 
7691 		 /*Now that LPN is fully consumable, we need to popuate parent_lpn_id
7692 		   if the picked LPN is already nested into another LPN */
7693 		 IF (l_parent_lpn_id IS NULL ) THEN
7694 			 SELECT parent_lpn_id INTO l_parent_lpn_id
7695 			 FROM WMS_LICENSE_PLATE_NUMBERS
7696 			 WHERE lpn_id=l_content_lpn_id ;
7697 		 END IF;
7698 		 IF (l_debug = 1) THEN
7699 			mydebug ('parent_lpn_id:'||l_parent_lpn_id||',content_lpn_id:'||l_content_lpn_id);
7700 		 END IF;
7701 	  END IF;
7702 
7703 	 --8761670/8798363 end
7704 
7705 
7706 	 -- bug 3983704
7707 	 IF l_lpn_match =4 and p_transfer_lpn_id = p_lpn_id THEN
7708 		 l_lpn_match := 1;
7709 		 l_content_lpn_id := l_lpn_id;
7710 		 l_lpn_id :=null;
7711 		 IF (l_debug = 1) THEN
7712 			 mydebug ('lpn_match is 4 and to lpn=from lpn');
7713 		 END IF;
7714 	 END IF;
7715 
7716 	 --jxlu for lpn overpicking
7717 
7718 	 IF   p_lpnpickedasis = 'Y'
7719 	 THEN
7720 		 IF (l_debug = 1) THEN
7721 			mydebug ('lpn_match is 4 and lpnpickedasis is true, change lpn_match to 1 ');
7722 		 END IF;
7723 		 l_lpn_match := 1;
7724 		 IF     p_serial_controlled = 'Y'
7725 			AND p_serial_allocated_flag = 'Y'
7726 		 THEN
7727 			  -- if lot controlled
7728 			  IF p_lot_controlled = 'Y' THEN
7729 					 FOR lot_rec in lot_csr LOOP
7730 						 IF (l_debug = 1) THEN
7731 							 mydebug ('item is lot controlled and current lot is: '||lot_rec.lot_number);
7732 						 END IF;
7733 						 FOR serial_rec in insert_serial_allocated_csr(lot_rec.lot_number) LOOP
7734 							-- insert serial into msnt and mark msn
7735 							  insert_serial(
7736 								  p_serial_transaction_temp_id  => lot_rec.serial_transaction_temp_id,
7737 								  p_organization_id             => p_organization_id,
7738 								  p_item_id                     => p_item_id,
7739 								  p_revision                    => p_revision,
7740 								  p_lot                         => lot_rec.lot_number,
7741 								  p_transaction_temp_id         => p_temp_id,
7742 								  p_created_by                  => p_user_id,
7743 								  p_from_serial                 => serial_rec.serial_number,
7744 								  p_to_serial                   => serial_rec.serial_number,
7745 								  p_status_id                   => NULL,
7746 								  x_return_status               => l_return_status,
7747 								  x_msg_data                    => l_msg_data);
7748 							   IF l_return_status IN ( fnd_api.g_ret_sts_unexp_error, fnd_api.g_ret_sts_error)
7749 							   THEN
7750 									  fnd_message.set_name('WMS', 'WMS_INTERNAL_ERROR');
7751 									  -- Internal Error $ROUTINE
7752 									  fnd_message.set_token('ROUTINE', '-INSERT_SERIAL API - ' || p_action);
7753 									  fnd_msg_pub.ADD;
7754 									  RAISE fnd_api.g_exc_unexpected_error;
7755 							   ELSE
7756 								   IF (l_debug = 1) THEN
7757 										mydebug ('the serial number is:  '|| serial_rec.serial_number);
7758 								   END IF;
7759 							   END IF;
7760 						 END LOOP;
7761 					 END LOOP;
7762 			  ELSE
7763 					 IF (l_debug = 1) THEN
7764 						   mydebug ('only serial controlled item and serial is allocated');
7765 					 END IF;
7766 					 FOR serial_rec IN insert_serial_allocated_csr(NULL) LOOP
7767 						  -- insert serial into msnt and mark msn
7768 						  insert_serial(
7769 								  p_serial_transaction_temp_id  => l_transaction_temp_id,
7770 								  p_organization_id             => p_organization_id,
7771 								  p_item_id                     => p_item_id,
7772 								  p_revision                    => p_revision,
7773 								  p_lot                         => NULL,
7774 								  p_transaction_temp_id         => p_temp_id,
7775 								  p_created_by                  => p_user_id,
7776 								  p_from_serial                 => serial_rec.serial_number,
7777 								  p_to_serial                   => serial_rec.serial_number,
7778 								  p_status_id                   => NULL,
7779 								  x_return_status               => l_return_status,
7780 								  x_msg_data                    => l_msg_data);
7781 						  IF l_return_status IN ( fnd_api.g_ret_sts_unexp_error, fnd_api.g_ret_sts_error)
7782 						  THEN
7783 								 fnd_message.set_name('WMS', 'WMS_INTERNAL_ERROR');
7784 								 -- Internal Error $ROUTINE
7785 								  fnd_message.set_token('ROUTINE', '- INSERT_SERIAL API - ');
7786 								  fnd_msg_pub.ADD;
7787 								  RAISE fnd_api.g_exc_unexpected_error;
7788 						  ELSE
7789 							  IF (l_debug = 1) THEN
7790 								  mydebug ('the serial number is:  '|| serial_rec.serial_number);
7791 							  END IF;
7792 						 END IF;
7793 					 END LOOP;
7794 			  END IF;
7795 		 END IF;
7796 	 END IF;
7797 
7798 	 IF p_confirmed_qty <> 0 THEN
7799 		 l_progress    :=  '140';
7800 		 IF (l_debug = 1) THEN mydebug ('l_progress: ' || l_progress ); END IF;
7801 		task_merge_split(
7802 					  p_action                 => p_action
7803 					 ,p_exception              => p_exception
7804 					 ,p_organization_id        => p_organization_id
7805 					 ,p_user_id                => p_user_id
7806 					 ,p_transaction_header_id  => p_transaction_header_id
7807 					 ,p_transaction_temp_id    => p_temp_id
7808 					 ,p_parent_line_id         => p_parent_line_id
7809 					 ,p_remaining_temp_id      => NULL
7810 					 ,p_lpn_id                 => l_lpn_id
7811 					 ,p_content_lpn_id         => l_content_lpn_id
7812 					 ,p_transfer_lpn_id        => l_transfer_lpn_id
7813 					 ,p_confirmed_sub          => p_confirmed_sub
7814 					 ,p_confirmed_locator_id   => p_confirmed_loc_id
7815 					 ,p_confirmed_uom          => p_confirmed_uom
7816 					 ,p_suggested_uom          => p_suggested_uom
7817 					 ,p_primary_uom            => p_primary_uom
7818 					 ,p_inventory_item_id      => p_item_id
7819 					 ,p_revision               => p_revision
7820 					 ,p_confirmed_trx_qty      => p_confirmed_qty
7821 					 ,p_confirmed_lots         => p_confirmed_lots
7822 					 ,p_confirmed_lot_trx_qty  => p_confirmed_lot_trx_qty
7823 					 ,p_confirmed_sec_uom      => p_confirmed_sec_uom
7824 					 ,p_confirmed_sec_qty      => p_confirmed_sec_qty
7825 					 ,p_confirmed_serials      => p_confirmed_serials
7826 					 ,p_container_item_id      => l_container_item_id
7827 					 ,p_lpn_match              => l_lpn_match
7828 					 ,p_lpn_match_lpn_id       => l_lpn_match_lpn_id
7829 					 ,p_serial_allocated_flag  => p_serial_allocated_flag
7830 					 ,p_lot_controlled         => p_lot_controlled
7831 					 ,p_serial_controlled      => p_serial_controlled
7832 					 ,p_parent_lpn_id          => l_parent_lpn_id
7833 					 --,p_lpnpickedasis          => p_lpnpickedasis
7834 					 ,x_new_transaction_temp_id=> l_out_temp_id
7835 					 ,x_cms_check              => x_cms_check
7836 					 ,x_return_status          => l_return_status
7837 					 ,x_msg_count              => l_msg_count
7838 					 ,x_msg_data               => l_msg_data
7839 					 ,p_substitute_lots	       => p_substitute_lots --/* Bug 9448490 Lot Substitution Project */
7840 					 ,p_full_lot_allocation    => p_full_lot_allocation -- Added for 14699845 (Flexible Lot Allocation)
7841 					 ,p_fulfillment_base       => p_fulfillment_base);  -- 16070349
7842 
7843 		 IF l_return_status IN ( fnd_api.g_ret_sts_unexp_error, fnd_api.g_ret_sts_error)
7844 		 THEN
7845 			fnd_message.set_name('WMS', 'WMS_INTERNAL_ERROR');
7846 			-- Internal Error $ROUTINE
7847 			fnd_message.set_token('ROUTINE', '-Task_Merge_Split API - ' || p_action);
7848 			fnd_msg_pub.ADD;
7849 			IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
7850 				RAISE fnd_api.g_exc_unexpected_error;
7851 			ELSE
7852 				 RAISE fnd_api.g_exc_error;
7853 			END IF;
7854 		 END IF;
7855 	  END IF; -- confirmed_qty <> 0
7856 
7857    -- REASONS ...EXCEPTION HANDLING  will deal with MOL upd'ns , deletion of MMTT and its children etc.
7858    -- NO REASONS ...EXCEPTION HANDLING  o  for BULJK.... Janet will call from bulk API
7859    -- BULK API .. to process children etc
7860    -- No LAbel printing of BULK
7861 
7862    l_progress    :=  '200';
7863    IF (l_debug = 1) THEN    mydebug ('l_progress: ' || l_progress );    END IF;
7864    IF p_action = l_g_action_load_multiple
7865    THEN
7866 	   l_multiple_pick := 'Y';
7867    ELSE
7868 	   l_multiple_pick := 'N';
7869    END IF;
7870    l_progress    :=  '210';
7871    IF (l_debug = 1) THEN    mydebug ('l_progress: ' || l_progress );    END IF;
7872 
7873    /* IF p_discrepancies IS NOT NULL
7874 	 -- there are non quantity discrepencies to be logged
7875 	 -- in wms_exceptions table.
7876 	 -- it also includes overpick and shortpick for LPN/Lot.
7877 	 -- Qty exception is one where total picked < suggested
7878 	 -- and there can be only one qty exception for a task. */
7879 
7880    IF p_discrepancies IS NOT NULL
7881    THEN
7882 	  l_progress    :=  '300';
7883 	  IF (l_debug = 1) THEN    mydebug ('l_progress: ' || l_progress );    END IF;
7884 	  wms_txnrsn_actions_pub.process_exceptions
7885 			   ( p_organization_id          => p_organization_id
7886 				,p_employee_id              => p_person_id
7887 				,p_effective_start_date     => p_effective_start_date
7888 				,p_effective_end_date       => p_effective_end_date
7889 				,p_inventory_item_id        => p_item_id
7890 				,p_revision                 => p_revision
7891 				,p_discrepancies            => p_discrepancies
7892 				,x_return_status            => x_return_status
7893 				,x_msg_count                => x_msg_count
7894 				,x_msg_data                 => x_msg_data );
7895 	  IF (l_debug = 1) THEN
7896 		mydebug ('x_return_status: ' || x_return_status );
7897 	  END IF;
7898 	   IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
7899 		  RAISE fnd_api.g_exc_unexpected_error;
7900 	   ELSIF x_return_status = fnd_api.g_ret_sts_error THEN
7901 		  RAISE fnd_api.g_exc_error;
7902 	   END IF;
7903    END IF;
7904 
7905    IF l_parent_line_id IS NULL  -- non bulk parent or non-bulk-child
7906 	   AND
7907 	  --l_qty_rsn_id  IS NOT NULL      -- There is a Curtail pick exception
7908 	  p_exception = 'SHORT'
7909    THEN
7910 	  -- it should be called only for qty  exceptions where picked quantity < suggested quantity
7911 	  -- and  not for overpicked qty
7912 	  l_progress    :=  '400';
7913 	  IF (l_debug = 1) THEN    mydebug ('l_progress: ' || l_progress );    END IF;
7914 	  wms_txnrsn_actions_pub.cleanup_task
7915 					  ( p_temp_id       => p_temp_id
7916 					  , p_qty_rsn_id    => l_qty_rsn_id
7917 					  , p_user_id       => p_user_id
7918 					  , p_employee_id   => p_person_id
7919 					  , x_return_status => x_return_status
7920 					  , x_msg_count     => x_msg_count
7921 					  , x_msg_data      => x_msg_data);
7922 	  IF (l_debug = 1) THEN
7923 		mydebug ('x_return_status: ' || x_return_status );
7924 	  END IF;
7925 	   IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
7926 		  RAISE fnd_api.g_exc_unexpected_error;
7927 	   ELSIF x_return_status = fnd_api.g_ret_sts_error THEN
7928 		  RAISE fnd_api.g_exc_error;
7929 	   END IF;
7930 
7931    END IF;
7932 
7933    -- Bug #: 6908598
7934 -- Calling label printing both for bulk and independent tasks
7935    BEGIN
7936 	   l_business_flow_code  := inv_label.wms_bf_pick_load;
7937 	   IF l_tran_type_id = 52 THEN -- Picking for sales order
7938 	  l_business_flow_code  := inv_label.wms_bf_pick_load;
7939 	   ELSIF l_tran_type_id = 35 THEN -- WIP issue
7940 	  l_business_flow_code  := inv_label.wms_bf_wip_pick_load;
7941 	   ELSIF l_tran_type_id = 51
7942 	  AND l_tran_source_type_id = 13 THEN --Backflush
7943 	  l_business_flow_code  := inv_label.wms_bf_wip_pick_load;
7944 	   ELSIF l_tran_type_id = 64
7945 	  AND l_tran_source_type_id = 4 THEN --Replenishment
7946 		 l_business_flow_code  := inv_label.wms_bf_replenishment_load;
7947 	   END IF;
7948 	   l_progress    :=  '410';
7949 	   IF (l_debug = 1) THEN mydebug ('l_business_flow_code: ' || l_business_flow_code ); END IF;
7950 
7951 	   OPEN mmtt_csr2(p_transaction_header_id);
7952 	   LOOP
7953 	  FETCH mmtt_csr2 INTO l_temp_id;
7954 	  EXIT WHEN mmtt_csr2%NOTFOUND;
7955 
7956 	  IF (l_debug = 1) THEN
7957 		 mydebug('task_load: Calling label printing for transaction:' || l_temp_id);
7958 	  END IF;
7959 
7960 	  inv_label.print_label_wrap(
7961 	  x_return_status              => x_return_status
7962 	  , x_msg_count                  => x_msg_count
7963 	  , x_msg_data                   => x_msg_data
7964 	  , x_label_status               => l_label_status
7965 	  , p_business_flow_code         => l_business_flow_code
7966 	  , p_transaction_id             => l_temp_id
7967 	  );
7968 
7969 	  IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
7970 		 IF (l_debug = 1) THEN mydebug('Label printing failed. Continue'); END IF;
7971 	  END IF;
7972 	   END LOOP;
7973 
7974 	   CLOSE mmtt_csr2;
7975 	   END;
7976  -- End of Bug #: 6908598
7977 
7978    IF nvl(l_parent_line_id,0) = p_temp_id
7979    THEN
7980 	   -- Call Bulk_API to process bulk children
7981 	   -- This API will also take care of qty exceptions workflow call for children
7982 	   -- affected by picking less than the requested qty
7983 	  l_progress    :=  '500';
7984 	  IF (l_debug = 1) THEN    mydebug ('l_progress: ' || l_progress );    END IF;
7985 	  wms_bulk_pick.bulk_pick(
7986 				 p_temp_id                    => p_temp_id
7987 			   , p_txn_hdr_id                 => p_transaction_header_id
7988 			   , p_org_id                     => p_organization_id
7989 			   , p_multiple_pick              => l_multiple_pick
7990 			   , p_exception                  => p_exception
7991 			   , p_lot_controlled             => p_lot_controlled
7992 			   , p_user_id                    => p_user_id
7993 			   , p_employee_id                => p_person_id
7994 			   , p_reason_id                  => p_qty_rsn_id
7995 			   , x_new_txn_hdr_id             => l_new_txn_hdr_id
7996 			   , x_return_status              => x_return_status
7997 			   , x_msg_count                  => x_msg_count
7998 			   , x_msg_data                   => x_msg_data);
7999 
8000 
8001 	  IF (l_debug = 1) THEN
8002 		mydebug ('l_new_txn_hdr_id: ' || l_new_txn_hdr_id );
8003 		mydebug ('x_return_status: ' || x_return_status );
8004 	  END IF;
8005 	   IF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
8006 		  --fnd_message.set_name('WMS', 'WMS_MULT_LPN_ERROR');
8007 		  --fnd_msg_pub.ADD;
8008 		  RAISE fnd_api.g_exc_unexpected_error;
8009 	   ELSIF x_return_status = fnd_api.g_ret_sts_error THEN
8010 		  --fnd_message.set_name('WMS', 'WMS_MULT_LPN_ERROR');
8011 		  --fnd_msg_pub.ADD;
8012 		  RAISE fnd_api.g_exc_error;
8013 	   END IF;
8014 
8015    /*  -- Bug #: 6908598 Commenting else part as we are calling the label printing irrespective of whether it is a bulk task or not  */
8016    /*
8017    ELSE
8018 	   -- call label printing for all tasks other than bulk tasks.
8019 	  l_progress    :=  '600';
8020 	  IF (l_debug = 1) THEN mydebug ('l_progress: ' || l_progress ); END IF;
8021 	  BEGIN
8022 		 l_business_flow_code  := inv_label.wms_bf_pick_load;
8023 		 IF l_tran_type_id = 52 THEN -- Picking for sales order
8024 			l_business_flow_code  := inv_label.wms_bf_pick_load;
8025 		 ELSIF l_tran_type_id = 35 THEN -- WIP issue
8026 			l_business_flow_code  := inv_label.wms_bf_wip_pick_load;
8027 		 ELSIF l_tran_type_id = 51
8028 			AND l_tran_source_type_id = 13 THEN --Backflush
8029 			l_business_flow_code  := inv_label.wms_bf_wip_pick_load;
8030 		 ELSIF l_tran_type_id = 64
8031 			AND l_tran_source_type_id = 4 THEN --Replenishment
8032 			   l_business_flow_code  := inv_label.wms_bf_replenishment_load;
8033 		 END IF;
8034 		 l_progress    :=  '610';
8035 		 IF (l_debug = 1) THEN mydebug ('l_business_flow_code: ' || l_business_flow_code ); END IF;
8036 
8037 		 OPEN mmtt_csr2(p_transaction_header_id);
8038 		 LOOP
8039 			FETCH mmtt_csr2 INTO l_temp_id;
8040 			EXIT WHEN mmtt_csr2%NOTFOUND;
8041 
8042 			IF (l_debug = 1) THEN
8043 			   mydebug('task_load: Calling label printing for transaction:' || l_temp_id);
8044 			END IF;
8045 
8046 			inv_label.print_label_wrap(
8047 			x_return_status              => x_return_status
8048 			, x_msg_count                  => x_msg_count
8049 			, x_msg_data                   => x_msg_data
8050 			, x_label_status               => l_label_status
8051 			, p_business_flow_code         => l_business_flow_code
8052 			, p_transaction_id             => l_temp_id
8053 			);
8054 
8055 			IF (x_return_status <> fnd_api.g_ret_sts_success) THEN
8056 			   IF (l_debug = 1) THEN mydebug('Label printing failed. Continue'); END IF;
8057 			END IF;
8058 		 END LOOP;
8059 
8060 		 CLOSE mmtt_csr2;
8061 		 END;
8062 
8063 	 */
8064 
8065 
8066 	 END IF; -- Not a bulk task.. finished label printing
8067 
8068 	 --Bug #4762505
8069 	 --At the end of the load process, we need to update the quantity_detailed
8070 	 --of the move order line. Do this for non-bulk tasks
8071 	 IF (l_mo_line_id IS NOT NULL AND l_parent_line_id IS NULL) THEN
8072 
8073 	   IF (l_debug = 1) THEN
8074 		 mydebug('Should update quantity_detailed for MO Line ID: ' || l_mo_line_id);
8075 	   END IF;
8076 
8077 	   --Fetch the UOM code of the MO line and lock the MO line
8078 	   SELECT uom_code, SECONDARY_UOM_CODE  -- muom:sk
8079 	   INTO   l_mol_uom, l_sec_mol_uom -- muom:sk
8080 	   FROM   mtl_txn_request_lines
8081 	   WHERE  line_id = l_mo_line_id
8082 	   FOR UPDATE;
8083 
8084 	   --Fetch the primary quantity for all MMTTs for this MO line
8085 	   --10120826 Fetch secondary_transaction_quantity for all MMTTs for this MO line
8086 	   SELECT ABS(SUM(primary_quantity)), ABS(SUM(secondary_transaction_quantity))
8087 	   INTO   l_sum_mmtt_qty, l_sum_mmtt_sec_txn_qty
8088 	   FROM   mtl_material_transactions_temp
8089 	   WHERE  move_order_line_id = l_mo_line_id;
8090 
8091 	   --Convert the MMTT primary quantity into MOL UOM
8092 	  -- muom:sk
8093 	   IF l_fulfillment_base = 'S' THEN -- muom:sk
8094 		  IF p_confirmed_sec_uom = l_sec_mol_uom THEN
8095 			l_mmtt_sec_qty_in_mol_uom := l_sum_mmtt_sec_txn_qty;
8096 		  ELSE
8097 			l_mmtt_sec_qty_in_mol_uom := inv_convert.inv_um_convert(item_id       => p_item_id,
8098 																precision     => l_g_decimal_precision,
8099 																from_quantity => l_sum_mmtt_sec_txn_qty,
8100 																from_unit     => p_confirmed_sec_uom,
8101 																to_unit       => l_sec_mol_uom,
8102 																from_name     => NULL,
8103 																to_name       => NULL);
8104 		  END IF;
8105 	   ELSE
8106 		  IF p_primary_uom = l_mol_uom THEN
8107 			l_mmtt_qty_in_mol_uom := l_sum_mmtt_qty;
8108 		  ELSE
8109 			l_mmtt_qty_in_mol_uom := inv_convert.inv_um_convert(item_id       => p_item_id,
8110 																precision     => l_g_decimal_precision,
8111 																from_quantity => l_sum_mmtt_qty,
8112 																from_unit     => p_primary_uom,
8113 																to_unit       => l_mol_uom,
8114 																from_name     => NULL,
8115 																to_name       => NULL);
8116 		  END IF;
8117 	   END IF; -- muom:sk
8118 
8119 	   IF (l_debug = 1) THEN
8120 		 mydebug('update quantity_detailed in MOL with: ' || l_mmtt_qty_in_mol_uom);
8121 		 --10120826
8122 		 mydebug('update secondary_quantity_detailed in MOL with: ' || l_sum_mmtt_sec_txn_qty);
8123 	   END IF;
8124 
8125 	   --Now update quantity_detailed as quanity_delivered + sum(mmtt qty)
8126 	   --Bug#10120826 Now update secondary_quantity_detailed as secondary_quanity_delivered + l_sum_mmtt_sec_txn_qty
8127 	   -- muom:sk ssk: this takes care of full pick only..
8128 	  UPDATE mtl_txn_request_lines
8129 		 SET quantity_detailed           = DECODE(l_fulfillment_base, 'S', l_sum_mmtt_qty, (NVL(quantity_delivered, 0) +l_mmtt_qty_in_mol_uom)),
8130 			 secondary_quantity_detailed = DECODE(l_fulfillment_base, 'S', l_mmtt_sec_qty_in_mol_uom, (NVL(secondary_quantity_delivered,0) + l_sum_mmtt_sec_txn_qty)),
8131 			 last_update_date            = SYSDATE,
8132 			 last_updated_by             = p_user_id
8133 	   WHERE line_id = l_mo_line_id;
8134 	 END IF;   --END IF l_mo_line_id IS NOT NULL AND l_parent_line_id IS NULL
8135 	 --End changes for Bug #4762505
8136 
8137 	 --16448214
8138 	IF (NVL(p_fulfillment_base,'P') = 'S') THEN
8139 
8140 	 BEGIN --16760116 Added exception Block
8141 
8142 	  SELECT reservation_id
8143 	    INTO l_reservation_id
8144 	    FROM mtl_material_transactions_temp
8145 	   WHERE transaction_temp_id = p_temp_id
8146 	     FOR UPDATE;
8147 
8148 	   OPEN get_mmtt_qty(l_reservation_id);
8149       FETCH get_mmtt_qty INTO l_new_mmtt_qty;
8150 	  CLOSE get_mmtt_qty;
8151 
8152 
8153 	--Need to sync the MR detailed_quantity with the sum(MMTT pri qty).
8154     --But it cannot exceed the primary_reservatin_quantity.
8155 	  UPDATE mtl_reservations
8156          SET detailed_quantity = LEAST(l_new_mmtt_qty, primary_reservation_quantity)
8157 	   WHERE reservation_id    = l_reservation_id;
8158 
8159      EXCEPTION
8160      WHEN OTHERS THEN
8161 	 IF (l_debug = 1) THEN
8162         mydebug('Could not find MMTT to update the Detailed quantity for FB=S ');
8163         mydebug('SQLCODE '||SQLCODE);
8164         mydebug('SQLERRM '||SQLERRM);
8165      END IF;
8166 	 END;
8167 
8168 
8169 	END IF; --(NVL(p_fulfillment_base,'P') = 'S')
8170         --16448214
8171 
8172 	 IF (l_debug = 1) THEN
8173         mydebug('End of load_pick');
8174         mydebug('x_return_status :'||x_return_status);
8175         mydebug('x_msg_count:'||x_msg_count);
8176         mydebug('x_msg_data:' || x_msg_data);
8177 	 END IF;
8178 
8179 EXCEPTION
8180    WHEN fnd_api.g_exc_error THEN
8181 		x_return_status  := l_g_ret_sts_error;
8182 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8183 		mydebug('ROLLBACK ' );
8184 		ROLLBACK ;
8185 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
8186 		mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
8187 		mydebug('x_msg_count/Data = ' || x_msg_count || '/'|| x_msg_data);
8188    WHEN fnd_api.g_exc_unexpected_error THEN
8189 		x_return_status  := fnd_api.g_ret_sts_unexp_error;
8190 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8191 		mydebug('ROLLBACK ' );
8192 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
8193 		mydebug('RAISE fnd_api.g_exc_unexpected_error: ' || SQLERRM);
8194 		mydebug('x_msg_count/Data = ' || x_msg_count || '/'|| x_msg_data);
8195 		ROLLBACK ;
8196    WHEN OTHERS THEN
8197 		x_return_status  := l_g_ret_sts_unexp_error;
8198 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
8199 		mydebug('ROLLBACK ' );
8200 		ROLLBACK ;
8201 		mydebug('l_progress = ' || l_proc_name || ':'|| l_progress);
8202 		mydebug('RAISE fnd_api.g_exc_error: ' || SQLERRM);
8203 		mydebug('x_msg_count/Data = ' || x_msg_count || '/'|| x_msg_data);
8204 
8205 END TASK_LOAD;
8206 
8207 /*
8208 	 The following table gives the conditions checked by LPN Match
8209 	 and its return values
8210 
8211 	 Condition                            x_match    x_return_status
8212 	 =================================================================
8213 	 LPN already picked                       7               E
8214 	 LPN location is invalid                  6               E
8215 	 LPN SUB is null                         10               E
8216 	 LPN already staged for another SO       12               E
8217 	 Item/Lot/Revision is not in LPN         13               E
8218 	 LPN has multiple items,  item_qty<reqQty 2               S
8219 	 The user has to manually confirm the LPN
8220 	 LPN has requested item but quantity is   4               S
8221 	 more than the allocated quantity
8222 	 The user has to manually confirm the LPN
8223 	 Serial number is not valid for this     11               E
8224 	 transaction.
8225 	 LPN has requested item with sufficient   8               E
8226 	 quantity but LPN content status is
8227 	 invalid
8228 	 Serial Allocation was requested for the  9               E
8229 	 item but it is not allowed/there
8230 	 Everything allright and exact quantity   1               S
8231 	 match
8232 	 Everything allright and quantity in LPN  3               S
8233 	 is less than requested quantity
8234 
8235 	 Although x_match is being set even for error conditions
8236 	 it is used by the calling code ONLY in case of success
8237 
8238   */
8239 
8240 PROCEDURE lpn_match(
8241 	p_fromlpn_id            IN            NUMBER
8242   , p_org_id                IN            NUMBER
8243   , p_item_id               IN            NUMBER
8244   , p_rev                   IN            VARCHAR2
8245   , p_lot                   IN            VARCHAR2
8246   , p_trx_qty               IN            NUMBER
8247   , p_trx_uom               IN            VARCHAR2
8248   , p_sec_qty               IN            NUMBER     -- Bug #4141928
8249   , p_sec_uom               IN            VARCHAR2   -- Bug #4141928
8250   , x_match                 OUT NOCOPY    NUMBER
8251   , x_sub                   OUT NOCOPY    VARCHAR2
8252   , x_loc                   OUT NOCOPY    VARCHAR2
8253   , x_trx_qty               OUT NOCOPY    NUMBER
8254   , x_trx_sec_qty           OUT NOCOPY    NUMBER     -- Bug #4141928
8255   , x_return_status         OUT NOCOPY    VARCHAR2
8256   , x_msg_count             OUT NOCOPY    NUMBER
8257   , x_msg_data              OUT NOCOPY    VARCHAR2
8258   , p_temp_id               IN            NUMBER
8259   , p_parent_line_id        IN            NUMBER
8260   , p_wms_installed         IN            VARCHAR2
8261   , p_transaction_type_id   IN            NUMBER
8262   , p_cost_group_id         IN            NUMBER
8263   , p_is_sn_alloc           IN            VARCHAR2
8264   , p_action                IN            NUMBER
8265   , p_split                 IN            VARCHAR2
8266   , p_user_id               IN            NUMBER
8267   , x_temp_id               OUT NOCOPY    NUMBER
8268   , x_loc_id                OUT NOCOPY    NUMBER
8269   , x_lpn_lot_vector        OUT NOCOPY    VARCHAR2
8270   , x_lpn_lot_vector2       OUT NOCOPY    VARCHAR2
8271   , x_lpn_lot_vector3       OUT NOCOPY    VARCHAR2
8272   , x_lpn_lot_vector4       OUT NOCOPY    VARCHAR2
8273   , x_cms_check             OUT NOCOPY    VARCHAR2
8274   , x_parent_lpn_id         OUT NOCOPY    VARCHAR2
8275   , x_trx_qty_alloc         OUT NOCOPY    NUMBER
8276   , p_transaction_action_id IN            NUMBER
8277   , p_pickOverNoException   IN            VARCHAR2
8278   , p_toLPN_Default         IN            VARCHAR2   -- Bug 3855835
8279   , p_project_id            IN            NUMBER
8280   , p_task_id               IN            NUMBER
8281   , p_confirmed_sub         IN            VARCHAR2
8282   , p_confirmed_loc_id      IN            NUMBER
8283   , p_from_lpn_id           IN            NUMBER
8284   , x_toLPN_status          OUT NOCOPY    VARCHAR2 --Bug 3855835
8285   , x_lpnpickedasis         OUT NOCOPY    VARCHAR2
8286   , x_lpn_qoh               OUT NOCOPY    NUMBER
8287   , p_changelotNoException  IN            VARCHAR2 --/* Bug 9448490 Lot Substitution Project */
8288   , p_full_lot_allocation   IN            VARCHAR2 DEFAULT 'Y' -- Added for 14699845 (Flexible Lot Allocation)
8289   , p_fulfillment_base      IN            VARCHAR2 DEFAULT 'P' -- 16070349
8290   )IS
8291 	l_proc_name              VARCHAR2(30) := 'LPN_MATCH' ;
8292 	l_msg_cnt                NUMBER;
8293 	l_msg_data               VARCHAR2(2000);
8294 	l_return_status          VARCHAR2(240);
8295 	l_exist_qty              NUMBER;
8296 	l_item_cnt               NUMBER;
8297 	l_rev_cnt                NUMBER;
8298 	l_lot_cnt                NUMBER;
8299 	l_item_cnt2              NUMBER;
8300 	l_cg_cnt                 NUMBER;
8301 	l_sub                    VARCHAR2(60);
8302 	l_loc                    VARCHAR2(60);
8303 	l_loaded                 NUMBER         := 0;
8304 	l_allocate_serial_flag   NUMBER         := 0;
8305 	l_temp_serial_trans_temp NUMBER         := 0;
8306 	l_serial_number          VARCHAR2(50);
8307 
8308 	l_lpn_pr_qty             NUMBER;
8309 	l_lpn_trx_qty            NUMBER;
8310 	l_lpn_sec_qty            NUMBER;                 -- Bug #4141928
8311 
8312 	l_pr_qty                 NUMBER;
8313 	l_primary_uom            VARCHAR2(3);
8314 	l_sec_qty                NUMBER;       -- Bug #4141928
8315 	l_secondary_uom          VARCHAR2(3);       -- Bug #4141928
8316 
8317 	l_lot_code               NUMBER;
8318 	l_serial_code            NUMBER;
8319 
8320 	l_mmtt_qty               NUMBER;
8321 	l_mmtt_sec_qty           NUMBER;       -- Bug #4141928
8322 
8323 	l_out_temp_id            NUMBER         := 0;
8324 	l_serial_exist_cnt       NUMBER         := 0;
8325 	l_total_serial_cnt       NUMBER         := 0;
8326 	l_so_cnt                 NUMBER         := 0;
8327 -- Increased lot size to 80 Char - Mercy Thomas - B4625329
8328 	l_mtlt_lot_number        VARCHAR2(80);
8329 
8330 	l_mtlt_primary_qty       NUMBER;
8331 	l_wlc_quantity           NUMBER;
8332 	l_wlc_uom_code           VARCHAR2(3);
8333 	l_mtlt_secondary_qty     NUMBER;       -- Bug #4141928
8334 	l_wlc_sec_quantity       NUMBER;                 -- Bug #4141928
8335 	l_wlc_sec_uom_code       VARCHAR2(3);            -- Bug #4141928
8336 
8337 	l_lot_match              NUMBER;
8338 	l_ok_to_process          VARCHAR2(5);
8339 	l_is_revision_control    VARCHAR2(1);
8340 	l_is_lot_control         VARCHAR2(1);
8341 	l_is_serial_control      VARCHAR2(1);
8342 	b_is_revision_control    BOOLEAN;
8343 	b_is_lot_control         BOOLEAN;
8344 	b_is_serial_control      BOOLEAN;
8345 	l_from_lpn               VARCHAR2(30);
8346 	l_loc_id                 NUMBER;
8347 	l_lpn_context            NUMBER;
8348 	l_lpn_exists             NUMBER;
8349 
8350 	l_qoh                    NUMBER;
8351 	l_rqoh                   NUMBER;
8352 	l_qr                     NUMBER;
8353 	l_qs                     NUMBER;
8354 	l_att                    NUMBER;
8355 	l_atr                    NUMBER;
8356 	l_sqoh                   NUMBER;                  -- Bug #4141928
8357 	l_srqoh                  NUMBER;                  -- Bug #4141928
8358 	l_sqr                    NUMBER;                  -- Bug #4141928
8359 	l_sqs                    NUMBER;                  -- Bug #4141928
8360 	l_satt                   NUMBER;                  -- Bug #4141928
8361 	l_satr                   NUMBER;                  -- Bug #4141928
8362 
8363 	l_allocated_lpn_id       NUMBER;
8364 	l_table_index            NUMBER         := 0;
8365 	l_table_total            NUMBER         := 0;
8366 	l_table_count            NUMBER;
8367 	l_lpn_include_lpn        NUMBER;
8368 	l_xfr_sub_code           VARCHAR2(30);
8369 	l_sub_active             NUMBER         := 0;
8370 	l_loc_active             NUMBER         := 0;
8371 	l_mmtt_proj_id           NUMBER;  --  2774506/2905646
8372 	l_mmtt_task_id           NUMBER;
8373 	l_locator_id             NUMBER;
8374 	l_organization_id        NUMBER;
8375 	l_mil_proj_id            NUMBER;
8376 	l_mil_task_id            NUMBER;   -- 2774506/2905646
8377 	l_transaction_header_id  NUMBER;
8378 	l_transaction_uom        VARCHAR2(3);
8379 	l_sec_transaction_uom    VARCHAR2(3);  -- Bug #4141928
8380 	l_lpn_id                 NUMBER;
8381 	l_content_lpn_id         NUMBER;
8382 
8383 	l_check_tolerance        BOOLEAN;
8384 	l_overpicked_qty         NUMBER ;
8385 	l_lot_string             VARCHAR2(12000);--Bug 6148865
8386 	l_lot_qty_string         VARCHAR2(12000);--Bug 6148865
8387 	l_lot_sec_qty_string     VARCHAR2(12000);  -- Bug #4141928 --Bug 6148865
8388 	l_sec_qty_str            VARCHAR2(12000);  -- Bug #4141928 --Bug 6148865
8389 	l_serial_string          VARCHAR2(2000);
8390 	l_check_overpick_passed  VARCHAR2(1);
8391 	l_overpick_error_code    NUMBER;
8392 	l_match_serials          BOOLEAN  := FALSE;
8393 	l_pick_to_lpn_id         NUMBER;
8394 	l_lot_v                  VARCHAR2(32767) := NULL;  --Bug 6148865
8395 
8396 	--Bug5649056
8397 	l_mmtt_sub               VARCHAR2(60);
8398 	l_mmtt_loc               NUMBER;
8399 	l_lpn_sub                VARCHAR2(60);
8400 	l_lpn_loc                NUMBER;
8401 	--Bug5649056
8402 	l_value                  VARCHAR2(3); --Bug 6651517
8403 	l_transaction_source_type_id NUMBER; --11068325
8404 
8405 	l_lpn_qoh                NUMBER; --HWSNIssue 13860546
8406 	l_lpn_sqoh               NUMBER;
8407 	l_substitute_lot_exist   BOOLEAN := FALSE; --14397366
8408 
8409 	l_lot_divisible_flag     VARCHAR2(1); -- Added for 14699845 (Flexible Lot Allocation)
8410     --16267113
8411 	l_task_pri_qty           NUMBER;
8412 	l_task_sec_qty           NUMBER;
8413     l_lpn_primary_qty        NUMBER;
8414     l_lpn_secondary_qty      NUMBER;
8415     l_lpn_att                NUMBER :=0;
8416     l_lpn_satt               NUMBER :=0;
8417     --16267113
8418 	l_fulfillment_base       VARCHAR2(1) := p_fulfillment_base;
8419 
8420 	CURSOR ser_csr IS
8421 	  SELECT serial_number
8422 		FROM mtl_serial_numbers
8423 	   WHERE lpn_id = p_fromlpn_id
8424 		 AND inventory_item_id = p_item_id
8425 		 AND NVL(lot_number, -999) = NVL(p_lot, -999);
8426 
8427 	CURSOR lot_csr IS
8428 	  SELECT mtlt.primary_quantity
8429 	   , NVL(mtlt.secondary_quantity, 0)   -- Bug #4141928
8430 		   , mtlt.lot_number
8431 		FROM mtl_transaction_lots_temp mtlt
8432 	   WHERE mtlt.transaction_temp_id = p_temp_id
8433 	   --added material status check for lot under bug8398578
8434 	   AND inv_material_status_grp.is_status_applicable(
8435 										p_wms_installed
8436 								,       NULL
8437 								,       p_transaction_type_id
8438 								,       NULL
8439 								,       NULL
8440 								,       p_org_id
8441 								,       p_item_id
8442 								,       NULL
8443 								,       null
8444 								,       mtlt.lot_number
8445 								,       NULL
8446 								,       'O') = 'Y'
8447 	ORDER BY LOT_NUMBER;
8448 
8449 	--jxlu 10/12/04
8450 	CURSOR lot_att IS
8451 	  SELECT lot_number, sum(transaction_quantity) transaction_quantity
8452 		from wms_ALLOCATIONS_GTMP
8453 	GROUP BY LOT_NUMBER
8454 	ORDER BY LOT_NUMBER;
8455 
8456 	--/* Bug 9448490 Lot Substitution Project */ start
8457 	 CURSOR lot_substitution_alloc_csr IS
8458 	   SELECT NVL(SUM(primary_transaction_quantity),0)
8459 		  , lot_number , Nvl(Sum(secondary_transaction_quantity),0) --16267113
8460 			 FROM mtl_onhand_quantities_detail
8461 			 WHERE organization_id = p_org_id
8462 		 AND Nvl(containerized_flag, 2) = 1
8463 		 AND lpn_id = p_fromlpn_id
8464 			 AND subinventory_code = p_confirmed_sub
8465 			 AND locator_id = p_confirmed_loc_id
8466 			 AND inventory_item_id = p_item_id
8467 			 AND (revision = p_rev OR (revision IS NULL AND p_rev IS NULL))
8468 			 AND lot_number NOT IN (
8469 					   SELECT mtlt.lot_number
8470 				   FROM mtl_transaction_lots_temp mtlt
8471 				   WHERE mtlt.transaction_temp_id = p_temp_id
8472 				   )
8473 		AND lot_number IS NOT NULL
8474 		GROUP BY lot_number
8475 		ORDER BY lot_number;
8476 
8477 	--/* Bug 9448490 Lot Substitution Project */ end
8478 
8479 	l_debug                  NUMBER         := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
8480 
8481   BEGIN
8482 	mydebug('changeLotNoException is - ' || p_changelotNoException); --/* Bug 9448490 Lot Substitution Project */
8483 	--HWSNIssue 13860546
8484 	mydebug('p_fromlpn_id:                ' || p_fromlpn_id);
8485 	mydebug('p_org_id:                    ' || p_org_id);
8486 	mydebug('p_item_id:                   ' || p_item_id);
8487 	mydebug('p_rev:                       ' || p_rev);
8488 	mydebug('p_lot:                       ' || p_lot);
8489 	mydebug('p_trx_qty:                   ' || p_trx_qty);
8490 	mydebug('p_trx_uom:                   ' || p_trx_uom);
8491 	mydebug('p_sec_qty:                   ' || p_sec_qty);
8492 	mydebug('p_sec_uom:                   ' || p_sec_uom);
8493 	mydebug('p_temp_id:                   ' || p_temp_id);
8494 	mydebug('p_parent_line_id:            ' || p_parent_line_id);
8495 	mydebug('p_wms_installed:             ' || p_wms_installed);
8496 	mydebug('p_cost_group_id:             ' || p_cost_group_id);
8497 	mydebug('p_is_sn_alloc:               ' || p_is_sn_alloc);
8498 	mydebug('p_action:                    ' || p_action);
8499 	mydebug('p_split:                     ' || p_split);
8500 	mydebug('p_user_id:                   ' || p_user_id);
8501 	mydebug('p_transaction_action_id:     ' || p_transaction_action_id);
8502 	mydebug('p_pickOverNoException:       ' || p_pickOverNoException);
8503 	mydebug('p_toLPN_Default:             ' || p_toLPN_Default);
8504 	mydebug('p_project_id:                ' || p_project_id);
8505 	mydebug('p_task_id:                   ' || p_task_id);
8506 	mydebug('p_confirmed_sub:             ' || p_confirmed_sub);
8507 	mydebug('p_confirmed_loc_id:          ' || p_confirmed_loc_id);
8508 	mydebug('p_from_lpn_id:               ' || p_from_lpn_id);
8509 	mydebug('p_changelotNoException:      ' || p_changelotNoException);
8510 	mydebug('p_full_lot_allocation is -   ' || p_full_lot_allocation); -- Added for 14699845 (Flexible Lot Allocation)
8511 	mydebug('p_fulfillment_base:          ' || p_fulfillment_base);
8512 	--HWSNIssue 13860546
8513 	BEGIN
8514 	   -- this is done to release a lock on LPN that was picked first and later changed
8515 	   -- so that the lock on first one is released and next one will be locked later.
8516 	   ROLLBACK TO LPN_MATCH;
8517 	EXCEPTION
8518 	   WHEN OTHERS THEN
8519 		  IF SQLCODE = -1086 THEN --  savepoint 'XYZ' never established
8520 			 -- Ignore
8521 			  mydebug ('Save point not available.. may be the first visit to this API ' );
8522 		  END IF ;
8523 	END ;
8524 
8525 	SAVEPOINT LPN_MATCH ;
8526 	g_debug := l_debug;
8527 	IF (l_debug = 1) THEN
8528 	  mydebug('In lpn Match');
8529 	  mydebug('lpn_match: l_fulfillment_base is ' || l_fulfillment_base);
8530 	END IF;
8531 
8532 	x_return_status    := fnd_api.g_ret_sts_success;
8533 	l_lpn_exists       := 0;
8534 	--clear the PL/SQL table each time coming in
8535 
8536 	t_lpn_lot_qty_table.DELETE;
8537 
8538 	x_trx_qty_alloc := 0;
8539 	x_lpnpickedasis := 'N';
8540 
8541 	 SELECT primary_uom_code
8542 		  , secondary_uom_code
8543 		  , lot_control_code
8544 		  , serial_number_control_code
8545 	   INTO l_primary_uom
8546 		  , l_secondary_uom
8547 		  , l_lot_code
8548 		  , l_serial_code
8549 	   FROM mtl_system_items
8550 	  WHERE organization_id = p_org_id
8551 		AND inventory_item_id = p_item_id;
8552 
8553 		--bug 6651517
8554 	   select value
8555 	   into l_value
8556 	   from v$nls_parameters
8557 	   where parameter = 'NLS_NUMERIC_CHARACTERS';
8558 
8559 	-- p_trx_qty was passed in transaction_uom, need to convert it to primary_uom
8560 	IF (l_debug = 1) THEN
8561 		  mydebug('p_trx_uom :'|| p_trx_uom);
8562 		  mydebug('p_trx_qty in transaction uom:'|| p_trx_qty);
8563 		  mydebug('l_primary_uom :'|| l_primary_uom);
8564 		  mydebug('p_sec_uom :'|| p_sec_uom);
8565 		  mydebug('p_sec_qty :'|| p_sec_qty);
8566 		  mydebug('l_secondary_uom :'|| l_secondary_uom);
8567 	END IF;
8568 
8569 	IF l_fulfillment_base = 'S' THEN --{
8570 		IF (p_trx_uom <> l_secondary_uom) THEN
8571 			l_sec_qty := inv_convert.inv_um_convert(
8572 				 item_id        => p_item_id
8573 				,precision      => null
8574 				,from_quantity  => p_trx_qty
8575 				,from_unit      => p_trx_uom
8576 				,to_unit        => l_secondary_uom
8577 				,from_name      => null
8578 				,to_name        => null);
8579 
8580 		l_lpn_sec_qty    := l_sec_qty; -- muom:sk
8581 			mydebug('In lpn_match Fulfillment basis is S and transaction UOM is different than secondary UOM');
8582 			mydebug('p_trx_qty/l_sec_qty in secondary uom is :'|| l_sec_qty);
8583 		ELSE
8584 			-- when txn uom = sec uom, we can rely on txn qty.. better take the code as is..
8585 			l_lpn_trx_qty := p_trx_qty; -- muom:sk
8586 			l_sec_qty     := p_trx_qty; -- muom:sk
8587 			l_lpn_sec_qty := p_trx_qty; -- muom:sk
8588 			mydebug('In lpn_match Fulfillment basis is S and transaction UOM is same than secondary UOM');
8589 			mydebug('p_trx_qty/l_sec_qty in secondary uom is :'|| l_sec_qty);
8590 		END IF;
8591 			l_pr_qty     := inv_convert.inv_um_convert(
8592 				 item_id        => p_item_id
8593 				,precision      => null
8594 				,from_quantity  => l_sec_qty
8595 				,from_unit      => l_secondary_uom
8596 				,to_unit        => l_primary_uom
8597 				,from_name      => null
8598 				,to_name        => null);
8599 
8600 			l_lpn_pr_qty :=  l_pr_qty; -- muom:sk
8601 	ELSE
8602 		IF (p_trx_uom <> l_primary_uom) THEN
8603 			l_pr_qty := inv_convert.inv_um_convert(
8604 				 item_id        => p_item_id
8605 				,precision      => null
8606 				,from_quantity  => p_trx_qty
8607 				,from_unit      => p_trx_uom
8608 				,to_unit        => l_primary_uom
8609 				,from_name      => null
8610 				,to_name        => null);
8611 			l_lpn_pr_qty := l_pr_qty;
8612 			IF (l_debug = 1) THEN
8613 			mydebug('In lpn_match Fulfillment basis is P and transaction UOM is different than Primary UOM');
8614 			mydebug('p_trx_qty in primary uom is l_pri_qty:'|| l_pr_qty);
8615 			mydebug('l_lpn_pri_qty in primary uom :'|| l_lpn_pr_qty);
8616 			END IF;
8617 		ELSE
8618 			l_lpn_pr_qty           := p_trx_qty;
8619 			l_lpn_trx_qty          := p_trx_qty;
8620 			l_pr_qty               := p_trx_qty;
8621 			IF (l_debug = 1) THEN
8622 			mydebug('In lpn_match Fulfillment basis is P and transaction UOM is same as Primary UOM');
8623 			mydebug('l_lpn_pr_qty is the same as p_trx_qty  :'|| l_lpn_pr_qty);
8624 			mydebug('l_lpn_trx_qty is the same as p_trx_qty :'|| l_lpn_trx_qty);
8625 			mydebug('l_pr_qty is the same as p_trx_qty      :'|| l_pr_qty);
8626 			END IF;
8627 		END IF;
8628 			l_lpn_sec_qty          := p_sec_qty;  -- Bug #4141928
8629 			l_sec_qty              := p_sec_qty;  -- Bug #4141928
8630 	END IF; --}
8631 
8632 		--16267113
8633 		l_task_pri_qty   := l_pr_qty;
8634 		l_task_sec_qty   := l_sec_qty;
8635 
8636 		IF (l_debug = 1) THEN
8637 		  mydebug('FlexibleLotAlloc: l_task_pri_qty: '|| l_task_pri_qty);
8638 		  mydebug('FlexibleLotAlloc: l_task_sec_qty: '|| l_task_sec_qty);
8639 		END IF;
8640 		--16267113
8641 	BEGIN
8642 	 -- Bug5649056: Added sub and loc in following query.
8643 	 SELECT 1
8644 		   , lpn_context
8645 		   , parent_lpn_id
8646 		   , subinventory_code
8647 		   , locator_id
8648 		INTO l_lpn_exists
8649 		   , l_lpn_context
8650 		   , x_parent_lpn_id
8651 		   , l_lpn_sub
8652 		   , l_lpn_loc
8653 		FROM wms_license_plate_numbers wlpn
8654 	   WHERE wlpn.organization_id = p_org_id
8655 		 AND wlpn.lpn_id = p_fromlpn_id;
8656 	EXCEPTION
8657 	  WHEN NO_DATA_FOUND THEN
8658 		IF (l_debug = 1) THEN
8659 		  mydebug('lpn does not exist in org');
8660 		END IF;
8661 
8662 		fnd_message.set_name('WMS', 'WMS_CONT_INVALID_LPN');
8663 		fnd_msg_pub.ADD;
8664 		RAISE fnd_api.g_exc_error;
8665 	END;
8666 
8667 	IF l_lpn_exists = 0
8668 	   OR p_fromlpn_id = 0
8669 	   OR l_lpn_context <> wms_container_pub.lpn_context_inv THEN
8670 	  IF (l_debug = 1) THEN
8671 		mydebug('LPN does not exist in org');
8672 	  END IF;
8673 
8674 	  fnd_message.set_name('WMS', 'WMS_CONT_INVALID_LPN');
8675 	  fnd_msg_pub.ADD;
8676 	  RAISE fnd_api.g_exc_error;
8677 	END IF;
8678 
8679 	IF (l_debug = 1) THEN
8680 	  mydebug('Checking if lpn has been picked already');
8681 	END IF;
8682 
8683 	x_match            := 0;
8684 
8685 	BEGIN
8686 	  -- Bug#2742860 The from LPN should not be loaded,
8687 	  -- this check should not be restricted to that particular transaction header id
8688 
8689 	  SELECT 1
8690 		INTO l_loaded
8691 		FROM DUAL
8692 	   WHERE EXISTS(SELECT 1
8693 					  FROM mtl_material_transactions_temp
8694 					 WHERE (transfer_lpn_id = p_fromlpn_id
8695 							OR content_lpn_id = p_fromlpn_id)
8696 							AND organization_id = p_org_id); -- this AND organization_id condition is added for Bug# 12541060
8697 	EXCEPTION
8698 	  WHEN NO_DATA_FOUND THEN
8699 		l_loaded  := 0;
8700 	END;
8701 
8702 	IF l_loaded > 0 THEN
8703 	  x_match  := 7;
8704 	  fnd_message.set_name('WMS', 'WMS_LOADED_ERROR');
8705 	  fnd_msg_pub.ADD;
8706 	  RAISE fnd_api.g_exc_error;
8707 	END IF;
8708 
8709 	-- Check if locator is valid
8710 	IF (l_debug = 1) THEN
8711 	  mydebug('Fetch sub/loc for LPN ');
8712 	END IF;
8713 
8714 	BEGIN
8715 	  -- WMS PJM Integration, Selecting the resolved concatenated segments instead of concatenated segments
8716 	  SELECT w.subinventory_code
8717 		   , inv_project.get_locsegs(w.locator_id, w.organization_id)
8718 		   , w.license_plate_number
8719 		   , w.locator_id
8720 		   , w.lpn_context
8721 		INTO l_sub
8722 		   , l_loc
8723 		   , l_from_lpn
8724 		   , l_loc_id
8725 		   , l_lpn_context
8726 		FROM wms_license_plate_numbers w
8727 	   WHERE w.lpn_id = p_fromlpn_id
8728 		 AND w.locator_id IS NOT NULL;
8729 
8730 	  IF l_sub IS NULL THEN
8731 		-- The calling java code treats this condition as an error
8732 
8733 		x_match  := 10;
8734 		fnd_message.set_name('WMS', 'WMS_CONT_INVALID_SUB');
8735 		fnd_msg_pub.ADD;
8736 		RAISE fnd_api.g_exc_error;
8737 	  END IF;
8738 
8739 	  -- bug 2398247
8740 	  -- verify if sub is active
8741 	  SELECT COUNT(*)
8742 		INTO l_sub_active
8743 		FROM mtl_secondary_inventories
8744 	   WHERE NVL(disable_date, SYSDATE + 1) > SYSDATE
8745 		 AND organization_id = p_org_id
8746 		 AND secondary_inventory_name = l_sub;
8747 
8748 	  IF l_sub_active = 0 THEN
8749 		x_match  := 10;
8750 		fnd_message.set_name('WMS', 'WMS_CONT_INVALID_SUB');
8751 		fnd_msg_pub.ADD;
8752 		RAISE fnd_api.g_exc_error;
8753 	  END IF;
8754 
8755 	  -- verify if locator is active
8756 	  SELECT COUNT(*)
8757 		INTO l_loc_active
8758 		FROM mtl_item_locations_kfv
8759 	   WHERE NVL(disable_date, SYSDATE + 1) > SYSDATE
8760 		 AND organization_id = p_org_id
8761 		 AND subinventory_code = l_sub
8762 		 AND inventory_location_id = l_loc_id;
8763 
8764 	  IF l_loc_active = 0 THEN
8765 		x_match  := 10;
8766 		fnd_message.set_name('WMS', 'WMS_CONT_INVALID_LOC');
8767 		fnd_msg_pub.ADD;
8768 		RAISE fnd_api.g_exc_error;
8769 	  END IF;
8770 	   -- Begin fix for 2774506
8771 
8772 	   SELECT locator_id,organization_id,
8773 			  transaction_header_id,
8774 			  transaction_uom,
8775 			  SECONDARY_UOM_CODE,         -- Bug #4141928
8776 			  transaction_source_type_id  --11068325
8777 		 INTO l_locator_id, l_organization_id,
8778 			  l_transaction_header_id,
8779 			  l_transaction_uom,
8780 			  l_sec_transaction_uom,      -- Bug #4141928
8781 			  l_transaction_source_type_id  --11068325
8782 		from mtl_material_transactions_temp
8783 		where transaction_temp_id = p_temp_id;
8784 
8785 		 select nvl(project_id ,-999) , nvl(task_id ,-999)
8786 		into  l_mmtt_proj_id , l_mmtt_task_id
8787 		from  mtl_item_locations
8788 		where inventory_location_id = l_locator_id
8789 		and organization_id = l_organization_id ;
8790 
8791 	  select nvl(project_id, -999) , nvl(task_id ,-999)
8792 		into l_mil_proj_id , l_mil_task_id
8793 		from mtl_item_locations
8794 		where inventory_location_id = l_loc_id
8795 		and organization_id = p_org_id ;
8796 
8797 	  mydebug('mmtt project id =  '||l_mmtt_proj_id);
8798 	  mydebug('mmtt task id =  '||l_mmtt_task_id);
8799 	  mydebug('mil project id =  '||l_mil_proj_id);
8800 	  mydebug('mil task id =  '||l_mil_task_id);
8801 
8802 		IF ((l_mil_proj_id <> l_mmtt_proj_id ) or ( l_mil_task_id <> l_mmtt_task_id )) THEN
8803 			mydebug('lpn : the project/tak information does not match');
8804 			FND_MESSAGE.SET_NAME('WMS','WMS_CONT_INVALID_LPN');
8805 			FND_MSG_PUB.ADD;
8806 			RAISE FND_API.G_EXC_ERROR;
8807 		END IF;
8808 
8809 	 -- End fix for 2774506
8810 
8811 
8812 	  x_sub     := l_sub;
8813 	  x_loc     := l_loc;
8814 	  x_loc_id  := l_loc_id;
8815 	EXCEPTION
8816 	  WHEN NO_DATA_FOUND THEN
8817 		x_match  := 6;
8818 		fnd_message.set_name('WMS', 'WMS_TD_LPN_LOC_NOT_FOUND');
8819 		fnd_msg_pub.ADD;
8820 		RAISE fnd_api.g_exc_error;
8821 	END;
8822 
8823 	IF (l_debug = 1) THEN
8824 	  mydebug('sub is ' || l_sub);
8825 	  mydebug('loc is ' || l_loc);
8826 	END IF;
8827 
8828 	-- Check if LPN has already been allocated for any Sales order
8829 	-- If LPN has been picked for a sales order then it cannot be picked
8830 
8831 	IF (l_debug = 1) THEN
8832 	  mydebug('Checking SO for lpn');
8833 	END IF;
8834 
8835 	BEGIN
8836 	  SELECT 1
8837 		INTO l_so_cnt
8838 		FROM wms_license_plate_numbers
8839 	   WHERE lpn_context = 11
8840 		 AND lpn_id = p_fromlpn_id
8841 		 AND organization_id = p_org_id;
8842 	EXCEPTION
8843 	  WHEN NO_DATA_FOUND THEN
8844 		l_so_cnt  := 0;
8845 	END;
8846 
8847 	IF l_so_cnt > 0 THEN
8848 	  x_match  := 12;
8849 	  fnd_message.set_name('WMS', 'WMS_LPN_STAGED');
8850 	  fnd_msg_pub.ADD;
8851 	  RAISE fnd_api.g_exc_error;
8852 	END IF;
8853 
8854    -- SELECT primary_uom_code
8855    --      , lot_control_code
8856    ----      , serial_number_control_code
8857    --   INTO l_primary_uom
8858    --      , l_lot_code
8859    --      , l_serial_code
8860    --   FROM mtl_system_items
8861    --  WHERE organization_id = p_org_id
8862    --    AND inventory_item_id = p_item_id;
8863 
8864    --Bug5649056: added sub and locator below
8865    SELECT mmtt.transfer_subinventory
8866 		 , mmtt.subinventory_code
8867 		 , mmtt.locator_id
8868 	  INTO l_xfr_sub_code
8869 		 , l_mmtt_sub
8870 		 , l_mmtt_loc
8871 	  FROM mtl_material_transactions_temp mmtt
8872 	 WHERE mmtt.transaction_temp_id = p_temp_id;
8873 
8874 	-- Check to see if the item is in the LPN
8875 	IF (l_debug = 1) THEN
8876 	  mydebug('Checking to see if required  item,cg,rev,lot exist in lpn..');
8877 	END IF;
8878 
8879 	l_item_cnt         := 0;
8880 
8881 	IF (l_debug = 1) THEN
8882 	  mydebug('item - ' || p_item_id || '  LPN - ' || p_fromlpn_id || '  Org - ' || p_org_id || ' Lot - ' || p_lot || ' Rev -' || p_rev);
8883 	END IF;
8884 
8885 	BEGIN
8886 	  SELECT 1
8887 		INTO l_item_cnt
8888 		FROM DUAL
8889 	   WHERE EXISTS(
8890 			   SELECT 1
8891 				 FROM wms_lpn_contents wlc
8892 				WHERE wlc.parent_lpn_id = p_fromlpn_id
8893 				  AND wlc.organization_id = p_org_id
8894 				  AND wlc.inventory_item_id = p_item_id
8895 				  AND NVL(wlc.revision, '-999') = NVL(p_rev, '-999'));
8896 	EXCEPTION
8897 	  -- Item/lot/rev combo does not exist in LPN
8898 
8899 	  WHEN NO_DATA_FOUND THEN
8900 		IF (l_debug = 1) THEN
8901 		  mydebug('item lot rev combo does not exist');
8902 		END IF;
8903 
8904 		x_match  := 13;
8905 		fnd_message.set_name('WMS', 'WMS_CONT_INVALID_LPN');
8906 		fnd_msg_pub.ADD;
8907 		RAISE fnd_api.g_exc_error;
8908 	END;
8909 
8910 	-- Modified for 14699845 (Flexible Lot Allocation)
8911 	IF l_item_cnt > 0
8912 	   AND l_lot_code > 1 THEN
8913 	  --Do this only for lot controlled items
8914 
8915 	  BEGIN
8916 		SELECT 1
8917 		  INTO l_item_cnt
8918 		  FROM DUAL
8919 		WHERE EXISTS( SELECT 1
8920 						FROM wms_lpn_contents wlc
8921 					   WHERE wlc.parent_lpn_id = p_fromlpn_id
8922 						 AND wlc.organization_id = p_org_id
8923 						 AND wlc.inventory_item_id = p_item_id
8924 						 AND NVL(wlc.revision, '-999') = NVL(p_rev, '-999')
8925 						 AND (p_changelotNoException <> 'N'
8926 						  OR (p_full_lot_allocation IN ('N', 'P'))
8927 						  OR EXISTS (SELECT 1
8928 									   FROM mtl_transaction_lots_temp mtlt
8929 									  WHERE mtlt.transaction_temp_id = p_temp_id
8930 										AND mtlt.lot_number = wlc.lot_number)
8931 							  )); -- Modified for 14699845 (Flexible Lot Allocation)
8932 
8933 	  EXCEPTION
8934 		-- Item/lot/rev combo does not exist in LPN
8935 
8936 		WHEN NO_DATA_FOUND THEN
8937 		  IF (l_debug = 1) THEN
8938 			mydebug('lot rev combo for the item does not exist');
8939 		  END IF;
8940 
8941 		  x_match  := 5;
8942 		  fnd_message.set_name('WMS', 'WMS_CONT_INVALID_LOT_LPN');
8943 		  fnd_msg_pub.ADD;
8944 		  RAISE fnd_api.g_exc_error;
8945 	  END;
8946 	END IF;
8947 
8948 	-- Item with the correct lot/revision exists in LPN
8949 	IF p_is_sn_alloc = 'Y' AND p_full_lot_allocation IN ('Y', 'P') -- Modified for 14699845 (Flexible Lot Allocation)
8950 	   AND p_action = 4 THEN
8951 	  b_is_serial_control  := TRUE;
8952 	ELSE
8953 	  b_is_serial_control  := FALSE;
8954 	END IF;
8955 
8956 	IF p_action = 4 THEN
8957 	   l_is_serial_control := 'Y';
8958 	ELSE
8959 	   l_is_serial_control := 'N';
8960 	END IF;
8961 
8962 	IF l_lot_code > 1 THEN
8963 	  b_is_lot_control  := TRUE;
8964 	  l_is_lot_control  := 'Y';
8965 	ELSE
8966 	  b_is_lot_control  := FALSE;
8967 	  l_is_lot_control  := 'N';
8968 	END IF;
8969 
8970 	IF p_rev IS NULL THEN
8971 	  b_is_revision_control  := FALSE;
8972 	  l_is_revision_control  := 'N';
8973 	ELSE
8974 	  b_is_revision_control  := TRUE;
8975 	  l_is_revision_control  := 'Y';
8976 	END IF;
8977 
8978 	IF (l_debug = 1) THEN
8979 	  mydebug('is_serial_control  :' || l_is_serial_control);
8980 	  mydebug('is_lot_control     :' || l_is_lot_control);
8981 	  mydebug('is_revision_control:' || l_is_revision_control);
8982 	END IF;
8983 
8984 	BEGIN
8985 	  SELECT allocated_lpn_id
8986 		INTO l_allocated_lpn_id
8987 		FROM mtl_material_transactions_temp
8988 	   WHERE transaction_temp_id = p_temp_id;
8989 	EXCEPTION
8990 	  WHEN NO_DATA_FOUND THEN
8991 		IF (l_debug = 1) THEN
8992 		  mydebug('transaction does not exist in mmtt');
8993 		END IF;
8994 
8995 		fnd_message.set_name('INV', 'INV_INVALID_TRANSACTION');
8996 		fnd_msg_pub.ADD;
8997 		RAISE fnd_api.g_exc_error;
8998 	END;
8999 
9000 	-- clear quantity cache before we create qty tree.
9001 	inv_quantity_tree_pub.clear_quantity_cache;
9002 
9003 	-- Check if LPN has items other than the one requested
9004 
9005 	IF (l_debug = 1) THEN
9006 	  mydebug('lpn has the requested item ');
9007 	END IF;
9008 
9009 	l_item_cnt2        := 0;
9010 	l_lot_cnt          := 0;
9011 	l_rev_cnt          := 0;
9012 	l_cg_cnt           := 0;
9013 	l_item_cnt2        := 0;
9014 	l_lot_cnt          := 0;
9015 	l_rev_cnt          := 0;
9016 	l_cg_cnt           := 0;
9017 	l_lpn_include_lpn  := 0;
9018 
9019 	SELECT COUNT(DISTINCT inventory_item_id)
9020 		 , COUNT(DISTINCT lot_number)
9021 		 , COUNT(DISTINCT revision)
9022 		 , COUNT(DISTINCT cost_group_id)
9023 	  INTO l_item_cnt2
9024 		 , l_lot_cnt
9025 		 , l_rev_cnt
9026 		 , l_cg_cnt
9027 	  FROM wms_lpn_contents
9028 	 WHERE parent_lpn_id = p_fromlpn_id
9029 	   AND organization_id = p_org_id;
9030 
9031 	SELECT COUNT(*)
9032 	  INTO l_lpn_include_lpn
9033 	  FROM wms_license_plate_numbers
9034 	 WHERE outermost_lpn_id = p_fromlpn_id
9035 	   AND organization_id = p_org_id;
9036 
9037 	IF l_item_cnt2 > 1
9038 	   OR l_rev_cnt > 1
9039 	   OR l_lpn_include_lpn > 1 THEN --{
9040 	  -- LPN has multiple items
9041 	  -- Such LPN's can be picked but in such cases the user has to
9042 	  -- manually confirm the LPN.
9043 	  -- No validation for LPN contents in such a case.
9044 
9045 	  IF (l_debug = 1) THEN
9046 		mydebug('lpn has items other than requested item ');
9047 	  END IF;
9048 
9049 	  x_match  := 2;
9050 
9051 	IF l_lot_code > 1 THEN --{
9052 		-- adding serial allocation checking for lot+serial item
9053 		 IF p_is_sn_alloc = 'Y' AND p_full_lot_allocation IN ('Y', 'P') -- Modified for 14699845 (Flexible Lot Allocation)
9054 			 AND p_action = 4 THEN
9055 			IF (l_debug = 1) THEN
9056 			  mydebug('SN control and SN allocation on');
9057 			END IF;
9058 
9059 			SELECT COUNT(fm_serial_number)
9060 			  INTO l_serial_exist_cnt
9061 			  FROM mtl_serial_numbers_temp msnt, mtl_transaction_lots_temp mtlt
9062 			 WHERE mtlt.transaction_temp_id = p_temp_id
9063 			   AND msnt.transaction_temp_id = mtlt.serial_transaction_temp_id
9064 			   AND msnt.fm_serial_number IN(
9065 										  SELECT serial_number
9066 											FROM mtl_serial_numbers
9067 										   WHERE lpn_id = p_fromlpn_id
9068 											 AND inventory_item_id = p_item_id
9069 											 AND NVL(revision, '-999') = NVL(p_rev, '-999'));
9070 
9071 			IF (l_debug = 1) THEN
9072 			  mydebug('SN exist count' || l_serial_exist_cnt);
9073 			END IF;
9074 
9075 			IF (l_serial_exist_cnt = 0) THEN
9076 			  IF (l_debug = 1) THEN
9077 				mydebug('No serial allocations have occured or LPN does not have the allocated serials ');
9078 			  END IF;
9079 
9080 			  -- Serial numbers missing for the transaction
9081 			  x_match  := 9;
9082 			  fnd_message.set_name('INV', 'INV_INT_SERMISEXP');
9083 			  fnd_msg_pub.ADD;
9084 			  RAISE fnd_api.g_exc_error;
9085 			END IF;
9086 		 END IF;
9087 
9088 		l_lpn_pr_qty    := 0;
9089 		l_lpn_sec_qty   := 0;    -- Bug #4141928
9090 		mydebug('Opening lot_csr cursor in lpn_match procedure ');
9091 		OPEN lot_csr;
9092 
9093 		LOOP
9094 			FETCH lot_csr INTO l_mtlt_primary_qty, l_mtlt_secondary_qty, l_mtlt_lot_number; -- Bug #4141928
9095 			EXIT WHEN lot_csr%NOTFOUND;
9096 
9097 			IF (l_debug = 1) THEN
9098 			mydebug('l_mtlt_lot_number      : ' || l_mtlt_lot_number);
9099 			mydebug('l_mtlt_primary_qty     : ' || l_mtlt_primary_qty);
9100 			mydebug('l_mtlt_secondary_qty   : ' || l_mtlt_secondary_qty);  -- Bug #4141928
9101 			END IF;
9102 
9103 		IF NVL(l_allocated_lpn_id, 0) = p_fromlpn_id THEN --{
9104 			--from lpn is the same as allocated_lpn, we need to update qty tree as negative qty
9105 			--in order to get correct att.
9106 			inv_quantity_tree_pub.update_quantities(
9107 			  p_api_version_number         => 1.0
9108 			, p_init_msg_lst               => fnd_api.g_false
9109 			, x_return_status              => l_return_status
9110 			, x_msg_count                  => l_msg_cnt
9111 			, x_msg_data                   => l_msg_data
9112 			, p_organization_id            => p_org_id
9113 			, p_inventory_item_id          => p_item_id
9114 			, p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
9115 			, p_is_revision_control        => b_is_revision_control
9116 			, p_is_lot_control             => TRUE
9117 			, p_is_serial_control          => b_is_serial_control
9118 			, p_revision                   => NVL(p_rev, NULL)
9119 			, p_lot_number                 => l_mtlt_lot_number
9120 			, p_subinventory_code          => l_sub
9121 			, p_locator_id                 => l_loc_id
9122 			, p_primary_quantity           => -l_mtlt_primary_qty
9123 			, p_secondary_quantity         => -l_mtlt_secondary_qty -- Bug #4141928
9124 			, p_quantity_type              => inv_quantity_tree_pvt.g_qs_txn
9125 			, x_qoh                        => l_qoh
9126 			, x_rqoh                       => l_rqoh
9127 			, x_qr                         => l_qr
9128 			, x_qs                         => l_qs
9129 			, x_att                        => l_att
9130 			, x_atr                        => l_atr
9131 			, x_sqoh                       => l_sqoh        -- Bug #4141928
9132 			, x_srqoh                      => l_srqoh                -- Bug #4141928
9133 			, x_sqr                        => l_sqr                  -- Bug #4141928
9134 			, x_sqs                        => l_sqs                  -- Bug #4141928
9135 			, x_satt                       => l_satt                 -- Bug #4141928
9136 			, x_satr                       => l_satr                 -- Bug #4141928
9137 			, p_lpn_id                     => p_fromlpn_id
9138 			--, p_transfer_subinventory_code => l_xfr_sub_code       -- Bug #14753999
9139 			);
9140 
9141 			IF (l_return_status = fnd_api.g_ret_sts_success) THEN
9142 			  IF (l_debug = 1) THEN
9143 				mydebug('after update qty tree for lpn l_att :' || l_att || ' for lot:' || l_mtlt_lot_number);
9144 				mydebug('after update qty tree for lpn l_satt:' || l_satt || ' for lot:' || l_mtlt_lot_number);
9145 			  END IF;
9146 			ELSE
9147 			  IF (l_debug = 1) THEN
9148 				mydebug('calling update qty tree with lpn 1st time failed ');
9149 			  END IF;
9150 
9151 			  fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
9152 			  fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
9153 			  fnd_msg_pub.ADD;
9154 			  RAISE fnd_api.g_exc_error;
9155 			END IF;
9156 		  --Bug#5649056: only update if subinventory and locator match
9157 		ELSIF ( l_lpn_sub = l_mmtt_sub AND l_lpn_loc = l_mmtt_loc ) THEN
9158 			inv_quantity_tree_pub.update_quantities(
9159 			  p_api_version_number         => 1.0
9160 			, p_init_msg_lst               => fnd_api.g_false
9161 			, x_return_status              => l_return_status
9162 			, x_msg_count                  => l_msg_cnt
9163 			, x_msg_data                   => l_msg_data
9164 			, p_organization_id            => p_org_id
9165 			, p_inventory_item_id          => p_item_id
9166 			, p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
9167 			, p_is_revision_control        => b_is_revision_control
9168 			, p_is_lot_control             => TRUE
9169 			, p_is_serial_control          => b_is_serial_control
9170 			, p_revision                   => NVL(p_rev, NULL)
9171 			, p_lot_number                 => l_mtlt_lot_number
9172 			, p_subinventory_code          => l_sub
9173 			, p_locator_id                 => l_loc_id
9174 			, p_primary_quantity           => -l_mtlt_primary_qty
9175 			, p_secondary_quantity         => -l_mtlt_secondary_qty -- Bug #4141928
9176 			, p_quantity_type              => inv_quantity_tree_pvt.g_qs_txn
9177 			, x_qoh                        => l_qoh
9178 			, x_rqoh                       => l_rqoh
9179 			, x_qr                         => l_qr
9180 			, x_qs                         => l_qs
9181 			, x_att                        => l_att
9182 			, x_atr                        => l_atr
9183 			, x_sqoh                       => l_sqoh        -- Bug #4141928
9184 			, x_srqoh                      => l_srqoh                -- Bug #4141928
9185 			, x_sqr                        => l_sqr                  -- Bug #4141928
9186 			, x_sqs                        => l_sqs                  -- Bug #4141928
9187 			, x_satt                       => l_satt                 -- Bug #4141928
9188 			, x_satr                       => l_satr                 -- Bug #4141928
9189 			--  , p_lpn_id                =>   p_fromlpn_id      withour lpn_id, only to locator level
9190 		   -- , p_transfer_subinventory_code => l_xfr_sub_code       -- Bug #14753999
9191 			);
9192 
9193 			IF (l_return_status = fnd_api.g_ret_sts_success) THEN
9194 			  IF (l_debug = 1) THEN
9195 				mydebug('after update qty tree without lpn l_att :' || l_att || ' for lot:' || l_mtlt_lot_number);
9196 				mydebug('after update qty tree without lpn l_satt:' || l_satt || ' for lot:' || l_mtlt_lot_number);
9197 			  END IF;
9198 			ELSE
9199 			  IF (l_debug = 1) THEN
9200 				mydebug('calling update qty tree back without lpn 1st time failed ');
9201 			  END IF;
9202 
9203 			  fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
9204 			  fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
9205 			  fnd_msg_pub.ADD;
9206 			  RAISE fnd_api.g_exc_error;
9207 			END IF;
9208 		END IF; --}
9209 
9210 		  inv_quantity_tree_pub.query_quantities(
9211 			p_api_version_number         => 1.0
9212 		  , p_init_msg_lst               => fnd_api.g_false
9213 		  , x_return_status              => l_return_status
9214 		  , x_msg_count                  => l_msg_cnt
9215 		  , x_msg_data                   => l_msg_data
9216 		  , p_organization_id            => p_org_id
9217 		  , p_inventory_item_id          => p_item_id
9218 		  , p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
9219 		  , p_is_revision_control        => b_is_revision_control
9220 		  , p_is_lot_control             => TRUE
9221 		  , p_is_serial_control          => b_is_serial_control
9222 		  , p_demand_source_type_id      => -9999
9223 		  , p_revision                   => NVL(p_rev, NULL)
9224 		  , p_lot_number                 => l_mtlt_lot_number
9225 		  , p_subinventory_code          => l_sub
9226 		  , p_locator_id                 => l_loc_id
9227 		  , x_qoh                        => l_qoh
9228 		  , x_rqoh                       => l_rqoh
9229 		  , x_qr                         => l_qr
9230 		  , x_qs                         => l_qs
9231 		  , x_att                        => l_att
9232 		  , x_atr                        => l_atr
9233 		  , x_sqoh                       => l_sqoh                 -- Bug #4141928
9234 		  , x_srqoh                      => l_srqoh                -- Bug #4141928
9235 		  , x_sqr                        => l_sqr                  -- Bug #4141928
9236 		  , x_sqs                        => l_sqs                  -- Bug #4141928
9237 		  , x_satt                       => l_satt                 -- Bug #4141928
9238 		  , x_satr                       => l_satr                 -- Bug #4141928
9239 		  , p_lpn_id                     => p_fromlpn_id
9240 	   -- , p_transfer_subinventory_code => l_xfr_sub_code         -- Bug #14753999
9241 		  , p_grade_code                 => NULL                   -- Bug #4141928
9242 		  );
9243 
9244 		  IF (l_return_status = fnd_api.g_ret_sts_success) THEN --{
9245 			IF ((l_fulfillment_base = 'P' AND l_att > 0) OR (l_fulfillment_base = 'S' AND l_satt > 0)) THEN
9246 			  l_table_index  := l_table_index + 1;
9247 
9248 			  IF (l_debug = 1) THEN
9249 				  mydebug('l_table_index:' || l_table_index || ' lot_number:' || l_mtlt_lot_number || ' qty: ' || l_att || ' and sec qty: ' || l_satt);
9250 			  END IF;
9251 			  -- bug 3547725, now no matter what relation it is between l_mtlt_primary_qty and l_att
9252 
9253 				l_lpn_pr_qty                                    := l_lpn_pr_qty + l_att;
9254 				l_lpn_sec_qty                                   := l_lpn_sec_qty + l_satt;
9255 				t_lpn_lot_qty_table(l_table_index).lpn_id       := p_fromlpn_id;
9256 				t_lpn_lot_qty_table(l_table_index).lot_number   := l_mtlt_lot_number;
9257 				t_lpn_lot_qty_table(l_table_index).pri_qty      := l_att;
9258 				t_lpn_lot_qty_table(l_table_index).sec_qty      := l_satt;
9259 
9260 			IF(l_fulfillment_base = 'S') THEN
9261 					IF (l_secondary_uom = p_trx_uom) THEN
9262 						t_lpn_lot_qty_table(l_table_index).trx_qty := l_satt;
9263 					ELSIF(l_primary_uom = p_trx_uom) THEN
9264 						t_lpn_lot_qty_table(l_table_index).trx_qty := l_att;
9265 					ELSE
9266 						t_lpn_lot_qty_table(l_table_index).trx_qty := inv_convert.inv_um_convert(
9267 											   item_id        => p_item_id
9268 											  ,lot_number	  => l_mtlt_lot_number
9269 											  ,organization_id=> p_org_id
9270 											  ,precision      => null
9271 											  ,from_quantity  => l_satt
9272 											  ,from_unit      => l_secondary_uom
9273 											  ,to_unit        => p_trx_uom
9274 											  ,from_name      => null
9275 											  ,to_name        => null);
9276 					END IF;
9277 			ELSE
9278 				IF (l_primary_uom = p_trx_uom) THEN
9279 					t_lpn_lot_qty_table(l_table_index).trx_qty := l_att;
9280 				ELSE
9281 					IF(p_trx_uom = l_secondary_uom) THEN
9282 						t_lpn_lot_qty_table(l_table_index).trx_qty := l_satt;
9283 					ELSE
9284 						t_lpn_lot_qty_table(l_table_index).trx_qty := inv_convert.inv_um_convert(
9285 											   item_id        => p_item_id
9286 											  ,lot_number	  => l_mtlt_lot_number
9287 											  ,organization_id=> p_org_id
9288 											  ,precision      => null
9289 											  ,from_quantity  => l_att
9290 											  ,from_unit      => l_primary_uom
9291 											  ,to_unit        => p_trx_uom
9292 											  ,from_name      => null
9293 											  ,to_name        => null);
9294 					END IF;
9295 				END IF;
9296 			END IF;
9297 
9298 			ELSE
9299 			  IF (l_debug = 1) THEN
9300 				mydebug('LPN does not have lot ' || l_mtlt_lot_number);
9301 			  END IF;
9302 			END IF;
9303 		  ELSE
9304 			IF (l_debug = 1) THEN
9305 			  mydebug('calling qty tree 1st time failed ');
9306 			END IF;
9307 
9308 			fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
9309 			fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
9310 			fnd_msg_pub.ADD;
9311 			RAISE fnd_api.g_exc_error;
9312 		  END IF; --}
9313 
9314 		IF NVL(l_allocated_lpn_id, 0) = p_fromlpn_id THEN --{
9315 			--from lpn is the same as allocated_lpn, we need to update qty tree as negative qty
9316 			-- in order to get correct att.
9317 			inv_quantity_tree_pub.update_quantities(
9318 			  p_api_version_number         => 1.0
9319 			, p_init_msg_lst               => fnd_api.g_false
9320 			, x_return_status              => l_return_status
9321 			, x_msg_count                  => l_msg_cnt
9322 			, x_msg_data                   => l_msg_data
9323 			, p_organization_id            => p_org_id
9324 			, p_inventory_item_id          => p_item_id
9325 			, p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
9326 			, p_is_revision_control        => b_is_revision_control
9327 			, p_is_lot_control             => TRUE
9328 			, p_is_serial_control          => b_is_serial_control
9329 			, p_revision                   => NVL(p_rev, NULL)
9330 			, p_lot_number                 => l_mtlt_lot_number
9331 			, p_subinventory_code          => l_sub
9332 			, p_locator_id                 => l_loc_id
9333 			, p_primary_quantity           => l_mtlt_primary_qty
9334 			, p_secondary_quantity         => l_mtlt_secondary_qty -- Bug #4141928
9335 			, p_quantity_type              => inv_quantity_tree_pvt.g_qs_txn
9336 			, x_qoh                        => l_qoh
9337 			, x_rqoh                       => l_rqoh
9338 			, x_qr                         => l_qr
9339 			, x_qs                         => l_qs
9340 			, x_att                        => l_att
9341 			, x_atr                        => l_atr
9342 			, x_sqoh                       => l_sqoh                 -- Bug #4141928
9343 			, x_srqoh                      => l_srqoh                -- Bug #4141928
9344 			, x_sqr                        => l_sqr                  -- Bug #4141928
9345 			, x_sqs                        => l_sqs                  -- Bug #4141928
9346 			, x_satt                       => l_satt                 -- Bug #4141928
9347 			, x_satr                       => l_satr                 -- Bug #4141928
9348 			, p_lpn_id                     => p_fromlpn_id
9349 			--, p_transfer_subinventory_code => l_xfr_sub_code       -- Bug #14753999
9350 			);
9351 
9352 			IF (l_return_status = fnd_api.g_ret_sts_success) THEN
9353 			  IF (l_debug = 1) THEN
9354 				mydebug('after update qty tree back for lpn l_att :' || l_att || ' for lot:' || l_mtlt_lot_number);
9355 				mydebug('after update qty tree back for lpn l_satt:' || l_satt || ' for lot:' || l_mtlt_lot_number);
9356 			  END IF;
9357 			ELSE
9358 			  IF (l_debug = 1) THEN
9359 				mydebug('calling update qty tree back with lpn 1st time failed ');
9360 			  END IF;
9361 
9362 			  fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
9363 			  fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
9364 			  fnd_msg_pub.ADD;
9365 			  RAISE fnd_api.g_exc_error;
9366 			END IF;
9367 		  --Bug#5649056: only update if subinventory and locator match
9368 		ELSIF ( l_lpn_sub = l_mmtt_sub AND l_lpn_loc = l_mmtt_loc ) THEN
9369 			inv_quantity_tree_pub.update_quantities(
9370 			  p_api_version_number         => 1.0
9371 			, p_init_msg_lst               => fnd_api.g_false
9372 			, x_return_status              => l_return_status
9373 			, x_msg_count                  => l_msg_cnt
9374 			, x_msg_data                   => l_msg_data
9375 			, p_organization_id            => p_org_id
9376 			, p_inventory_item_id          => p_item_id
9377 			, p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
9378 			, p_is_revision_control        => b_is_revision_control
9379 			, p_is_lot_control             => TRUE
9380 			, p_is_serial_control          => b_is_serial_control
9381 			, p_revision                   => NVL(p_rev, NULL)
9382 			, p_lot_number                 => l_mtlt_lot_number
9383 			, p_subinventory_code          => l_sub
9384 			, p_locator_id                 => l_loc_id
9385 			, p_primary_quantity           => l_mtlt_primary_qty
9386 			, p_secondary_quantity         => l_mtlt_secondary_qty -- Bug #4141928
9387 			, p_quantity_type              => inv_quantity_tree_pvt.g_qs_txn
9388 			, x_qoh                        => l_qoh
9389 			, x_rqoh                       => l_rqoh
9390 			, x_qr                         => l_qr
9391 			, x_qs                         => l_qs
9392 			, x_att                        => l_att
9393 			, x_atr                        => l_atr
9394 			, x_sqoh                       => l_sqoh        -- Bug #4141928
9395 			, x_srqoh                      => l_srqoh                -- Bug #4141928
9396 			, x_sqr                        => l_sqr                  -- Bug #4141928
9397 			, x_sqs                        => l_sqs                  -- Bug #4141928
9398 			, x_satt                       => l_satt                 -- Bug #4141928
9399 			, x_satr                       => l_satr                 -- Bug #4141928
9400 			--  , p_lpn_id                =>   p_fromlpn_id      withour lpn_id, only to locator level
9401 			--, p_transfer_subinventory_code => l_xfr_sub_code       -- Bug #14753999
9402 			);
9403 
9404 			IF (l_return_status = fnd_api.g_ret_sts_success) THEN
9405 			  IF (l_debug = 1) THEN
9406 				mydebug('after update qty tree back without lpn l_att :' || l_att || ' for lot:' || l_mtlt_lot_number);
9407 				mydebug('after update qty tree back without lpn l_satt:' || l_satt || ' for lot:' || l_mtlt_lot_number);
9408 			  END IF;
9409 			ELSE
9410 			  IF (l_debug = 1) THEN
9411 				mydebug('calling update qty tree back without lpn 1st time failed ');
9412 			  END IF;
9413 
9414 			  fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
9415 			  fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
9416 			  fnd_msg_pub.ADD;
9417 			  RAISE fnd_api.g_exc_error;
9418 			END IF;
9419 		END IF; --}
9420 
9421 		END LOOP;
9422 
9423 		CLOSE lot_csr;
9424 		-- Bug #4141928. No changes required for OPM convergence.
9425 
9426 		IF l_fulfillment_base = 'S' THEN
9427 			-- Let secondary qty drive the lpn_match for secondary based fulfillment
9428 			IF  (l_lpn_sec_qty >= l_sec_qty) THEN
9429 				x_match := 5;
9430 			ELSE
9431 				x_match := 2;
9432 			END IF;
9433 		ELSE
9434 			-- Let primary qty drive the lpn_match for primary based fulfillment
9435 			IF  (l_lpn_pr_qty >= l_pr_qty) THEN
9436 				x_match := 5;
9437 			ELSE
9438 				x_match := 2;
9439 			END IF;
9440 		END IF;
9441 
9442 	ELSE -- Plain item or revision controlled item or serial controlled
9443 		-- bug 4277869
9444 		IF p_is_sn_alloc = 'Y'
9445 				 AND p_action = 4 THEN
9446 			 IF (l_debug = 1) THEN
9447 			   mydebug('SN control and SN allocation on');
9448 			 END IF;
9449 
9450 			 SELECT COUNT(fm_serial_number)
9451 			   INTO l_serial_exist_cnt
9452 			   FROM mtl_serial_numbers_temp msnt
9453 			  WHERE msnt.transaction_temp_id = p_temp_id
9454 				AND msnt.fm_serial_number IN(
9455 								   SELECT serial_number
9456 									 FROM mtl_serial_numbers
9457 									WHERE lpn_id = p_fromlpn_id
9458 									  AND inventory_item_id = p_item_id
9459 									  AND NVL(revision, '-999') = NVL(p_rev, '-999'));
9460 
9461 			 IF (l_debug = 1) THEN
9462 			   mydebug('SN exist count' || l_serial_exist_cnt);
9463 			 END IF;
9464 
9465 			 IF (l_serial_exist_cnt = 0) THEN
9466 			   IF (l_debug = 1) THEN
9467 				 mydebug('LPN does not have the allocated serials ');
9468 			   END IF;
9469 
9470 			   -- Serial numbers missing for the transaction
9471 			   x_match  := 9;
9472 			   fnd_message.set_name('INV', 'INV_INT_SERMISEXP');
9473 			   fnd_msg_pub.ADD;
9474 			   RAISE fnd_api.g_exc_error;
9475 			 END IF;
9476 		END IF;
9477 		-- end of bug 4277869
9478 
9479 		IF (l_debug = 1) THEN
9480 		  mydebug('Getting total qty in user entered uom..');
9481 		END IF;
9482 
9483 		IF NVL(l_allocated_lpn_id, 0) = p_fromlpn_id THEN --{
9484 		  --from lpn is the same as allocated_lpn, we need to update qty tree as negative qty
9485 		  -- in order to get correct att.
9486 		  inv_quantity_tree_pub.update_quantities(
9487 			p_api_version_number         => 1.0
9488 		  , p_init_msg_lst               => fnd_api.g_false
9489 		  , x_return_status              => l_return_status
9490 		  , x_msg_count                  => l_msg_cnt
9491 		  , x_msg_data                   => l_msg_data
9492 		  , p_organization_id            => p_org_id
9493 		  , p_inventory_item_id          => p_item_id
9494 		  , p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
9495 		  , p_is_revision_control        => b_is_revision_control
9496 		  , p_is_lot_control             => FALSE
9497 		  , p_is_serial_control          => b_is_serial_control
9498 		  , p_revision                   => NVL(p_rev, NULL)
9499 		  , p_lot_number                 => NULL
9500 		  , p_subinventory_code          => l_sub
9501 		  , p_locator_id                 => l_loc_id
9502 		  , p_primary_quantity           => -l_pr_qty
9503 		  , p_secondary_quantity         => -l_sec_qty -- Bug #4141928
9504 		  , p_quantity_type              => inv_quantity_tree_pvt.g_qs_txn
9505 		  , x_qoh                        => l_qoh
9506 		  , x_rqoh                       => l_rqoh
9507 		  , x_qr                         => l_qr
9508 		  , x_qs                         => l_qs
9509 		  , x_att                        => l_att
9510 		  , x_atr                        => l_atr
9511 		  , x_sqoh                       => l_sqoh                 -- Bug #4141928
9512 		  , x_srqoh                      => l_srqoh                -- Bug #4141928
9513 		  , x_sqr                        => l_sqr                  -- Bug #4141928
9514 		  , x_sqs                        => l_sqs                  -- Bug #4141928
9515 		  , x_satt                       => l_satt                 -- Bug #4141928
9516 		  , x_satr                       => l_satr                 -- Bug #4141928
9517 		  , p_lpn_id                     => p_fromlpn_id
9518 		 -- , p_transfer_subinventory_code => l_xfr_sub_code  -- Bug #14753999
9519 		  );
9520 
9521 			IF (l_return_status = fnd_api.g_ret_sts_success) THEN
9522 				IF (l_debug = 1) THEN
9523 				mydebug('update qty tree with lpn 2nd time: l_att :' || l_att);
9524 				mydebug('update qty tree with lpn 2nd time: l_satt:' || l_satt); -- Bug #4141928
9525 				END IF;
9526 			ELSE
9527 				IF (l_debug = 1) THEN
9528 				mydebug('calling update qty tree with lpn 2nd time failed ');
9529 				END IF;
9530 
9531 				fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
9532 				fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
9533 				fnd_msg_pub.ADD;
9534 				RAISE fnd_api.g_exc_error;
9535 			END IF;
9536 		--Bug#5649056: only update if subinventory and locator match
9537 		ELSIF ( l_lpn_sub = l_mmtt_sub AND l_lpn_loc = l_mmtt_loc ) THEN
9538 		  inv_quantity_tree_pub.update_quantities(
9539 			p_api_version_number         => 1.0
9540 		  , p_init_msg_lst               => fnd_api.g_false
9541 		  , x_return_status              => l_return_status
9542 		  , x_msg_count                  => l_msg_cnt
9543 		  , x_msg_data                   => l_msg_data
9544 		  , p_organization_id            => p_org_id
9545 		  , p_inventory_item_id          => p_item_id
9546 		  , p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
9547 		  , p_is_revision_control        => b_is_revision_control
9548 		  , p_is_lot_control             => FALSE
9549 		  , p_is_serial_control          => b_is_serial_control
9550 		  , p_revision                   => NVL(p_rev, NULL)
9551 		  , p_lot_number                 => NULL
9552 		  , p_subinventory_code          => l_sub
9553 		  , p_locator_id                 => l_loc_id
9554 		  , p_primary_quantity           => -l_pr_qty
9555 		  , p_secondary_quantity         => -l_sec_qty -- Bug #4141928
9556 		  , p_quantity_type              => inv_quantity_tree_pvt.g_qs_txn
9557 		  , x_qoh                        => l_qoh
9558 		  , x_rqoh                       => l_rqoh
9559 		  , x_qr                         => l_qr
9560 		  , x_qs                         => l_qs
9561 		  , x_att                        => l_att
9562 		  , x_atr                        => l_atr
9563 		  , x_sqoh                       => l_sqoh         -- Bug #4141928
9564 		  , x_srqoh                      => l_srqoh                -- Bug #4141928
9565 		  , x_sqr                        => l_sqr                  -- Bug #4141928
9566 		  , x_sqs                        => l_sqs                  -- Bug #4141928
9567 		  , x_satt                       => l_satt                 -- Bug #4141928
9568 		  , x_satr                       => l_satr                 -- Bug #4141928
9569 	   -- , p_lpn_id                     => p_fromlpn_id      withour lpn_id, only to locator level
9570 	   -- , p_transfer_subinventory_code => l_xfr_sub_code   -- Bug #14753999
9571 		  );
9572 
9573 			IF (l_return_status = fnd_api.g_ret_sts_success) THEN
9574 				IF (l_debug = 1) THEN
9575 				mydebug('update qty tree without lpn 2nd time: l_att :' || l_att);
9576 				mydebug('update qty tree without lpn 2nd time: l_satt:' || l_satt);
9577 				END IF;
9578 			ELSE
9579 				IF (l_debug = 1) THEN
9580 				mydebug('calling update qty tree back without lpn 2nd time failed ');
9581 				END IF;
9582 
9583 				fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
9584 				fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
9585 				fnd_msg_pub.ADD;
9586 				RAISE fnd_api.g_exc_error;
9587 			END IF;
9588 		END IF; --}
9589 
9590 		inv_quantity_tree_pub.query_quantities(
9591 		  p_api_version_number         => 1.0
9592 		, p_init_msg_lst               => fnd_api.g_false
9593 		, x_return_status              => l_return_status
9594 		, x_msg_count                  => l_msg_cnt
9595 		, x_msg_data                   => l_msg_data
9596 		, p_organization_id            => p_org_id
9597 		, p_inventory_item_id          => p_item_id
9598 		, p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
9599 		, p_is_revision_control        => b_is_revision_control
9600 		, p_is_lot_control             => FALSE
9601 		, p_is_serial_control          => b_is_serial_control
9602 		, p_demand_source_type_id      => -9999
9603 		, p_revision                   => NVL(p_rev, NULL)
9604 		, p_lot_number                 => NULL
9605 		, p_subinventory_code          => l_sub
9606 		, p_locator_id                 => l_loc_id
9607 		, x_qoh                        => l_qoh
9608 		, x_rqoh                       => l_rqoh
9609 		, x_qr                         => l_qr
9610 		, x_qs                         => l_qs
9611 		, x_att                        => l_att
9612 		, x_atr                        => l_atr
9613 		, x_sqoh                       => l_sqoh       -- Bug #4141928
9614 		, x_srqoh                      => l_srqoh                -- Bug #4141928
9615 		, x_sqr                        => l_sqr                  -- Bug #4141928
9616 		, x_sqs                        => l_sqs                  -- Bug #4141928
9617 		, x_satt                       => l_satt                 -- Bug #4141928
9618 		, x_satr                       => l_satr                 -- Bug #4141928
9619 		, p_lpn_id                     => p_fromlpn_id
9620 	   -- , p_transfer_subinventory_code => l_xfr_sub_code   -- Bug #14753999
9621 		, p_grade_code                 => NULL               -- Bug #4141928
9622 		);
9623 
9624 		IF (l_return_status = fnd_api.g_ret_sts_success) THEN
9625 				l_lpn_pr_qty    := l_att;
9626 				l_lpn_sec_qty   := l_satt;
9627 
9628 			IF (l_debug = 1) THEN
9629 				mydebug('l_att        :' || l_att);
9630 				mydebug('l_lpn_pr_qty :' || l_lpn_pr_qty);
9631 				mydebug('l_satt       :' || l_satt);
9632 				mydebug('l_lpn_sec_qty:' || l_lpn_sec_qty);
9633 				mydebug('l_pr_qty     :' || l_pr_qty);
9634 				mydebug('l_sec_qty    :' || l_sec_qty);
9635 			END IF;
9636 
9637 			IF l_fulfillment_base = 'S' THEN
9638 				IF (l_lpn_sec_qty >= l_sec_qty) THEN
9639 					x_match := 5;
9640 				ELSE
9641 					x_match := 2;
9642 				END IF;
9643 			ELSE
9644 				IF (l_lpn_pr_qty >= l_pr_qty) THEN
9645 					x_match := 5;
9646 				ELSE
9647 					x_match := 2;
9648 				END IF;
9649 			END IF;
9650 
9651 		ELSE
9652 		  IF (l_debug = 1) THEN
9653 			mydebug('calling qty tree 2nd time failed ');
9654 		  END IF;
9655 
9656 		  fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
9657 		  fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
9658 		  fnd_msg_pub.ADD;
9659 		  RAISE fnd_api.g_exc_error;
9660 		END IF;
9661 
9662 		IF NVL(l_allocated_lpn_id, 0) = p_fromlpn_id THEN --{
9663 		  --from lpn is the same as allocated_lpn, we need to update qty tree as negative qty
9664 		  -- in order to get correct att.
9665 		  inv_quantity_tree_pub.update_quantities(
9666 			p_api_version_number         => 1.0
9667 		  , p_init_msg_lst               => fnd_api.g_false
9668 		  , x_return_status              => l_return_status
9669 		  , x_msg_count                  => l_msg_cnt
9670 		  , x_msg_data                   => l_msg_data
9671 		  , p_organization_id            => p_org_id
9672 		  , p_inventory_item_id          => p_item_id
9673 		  , p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
9674 		  , p_is_revision_control        => b_is_revision_control
9675 		  , p_is_lot_control             => FALSE
9676 		  , p_is_serial_control          => b_is_serial_control
9677 		  , p_revision                   => NVL(p_rev, NULL)
9678 		  , p_lot_number                 => NULL
9679 		  , p_subinventory_code          => l_sub
9680 		  , p_locator_id                 => l_loc_id
9681 		  , p_primary_quantity           => l_pr_qty
9682 		  , p_secondary_quantity         => l_sec_qty   -- Bug #4141928
9683 		  , p_quantity_type              => inv_quantity_tree_pvt.g_qs_txn
9684 		  , x_qoh                        => l_qoh
9685 		  , x_rqoh                       => l_rqoh
9686 		  , x_qr                         => l_qr
9687 		  , x_qs                         => l_qs
9688 		  , x_att                        => l_att
9689 		  , x_atr                        => l_atr
9690 		  , x_sqoh                       => l_sqoh         -- Bug #4141928
9691 		  , x_srqoh                      => l_srqoh                -- Bug #4141928
9692 		  , x_sqr                        => l_sqr                  -- Bug #4141928
9693 		  , x_sqs                        => l_sqs                  -- Bug #4141928
9694 		  , x_satt                       => l_satt                 -- Bug #4141928
9695 		  , x_satr                       => l_satr                 -- Bug #4141928
9696 		  , p_lpn_id                     => p_fromlpn_id
9697 		 -- , p_transfer_subinventory_code => l_xfr_sub_code  -- Bug #14753999
9698 		  );
9699 
9700 		  IF (l_return_status = fnd_api.g_ret_sts_success) THEN
9701 			IF (l_debug = 1) THEN
9702 				mydebug('update qty tree back with lpn 2nd time: l_att :' || l_att);
9703 				mydebug('update qty tree back with lpn 2nd time: l_satt:' || l_satt);
9704 			END IF;
9705 		  ELSE
9706 			IF (l_debug = 1) THEN
9707 			  mydebug('calling update qty tree with lpn 2nd time failed ');
9708 			END IF;
9709 
9710 			fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
9711 			fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
9712 			fnd_msg_pub.ADD;
9713 			RAISE fnd_api.g_exc_error;
9714 		  END IF;
9715 		--Bug#5649056: only update if subinventory and locator match
9716 		ELSIF ( l_lpn_sub = l_mmtt_sub AND l_lpn_loc = l_mmtt_loc ) THEN
9717 		  inv_quantity_tree_pub.update_quantities(
9718 			p_api_version_number         => 1.0
9719 		  , p_init_msg_lst               => fnd_api.g_false
9720 		  , x_return_status              => l_return_status
9721 		  , x_msg_count                  => l_msg_cnt
9722 		  , x_msg_data                   => l_msg_data
9723 		  , p_organization_id            => p_org_id
9724 		  , p_inventory_item_id          => p_item_id
9725 		  , p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
9726 		  , p_is_revision_control        => b_is_revision_control
9727 		  , p_is_lot_control             => FALSE
9728 		  , p_is_serial_control          => b_is_serial_control
9729 		  , p_revision                   => NVL(p_rev, NULL)
9730 		  , p_lot_number                 => NULL
9731 		  , p_subinventory_code          => l_sub
9732 		  , p_locator_id                 => l_loc_id
9733 		  , p_primary_quantity           => l_pr_qty
9734 		  , p_secondary_quantity         => l_sec_qty   -- Bug #4141928
9735 		  , p_quantity_type              => inv_quantity_tree_pvt.g_qs_txn
9736 		  , x_qoh                        => l_qoh
9737 		  , x_rqoh                       => l_rqoh
9738 		  , x_qr                         => l_qr
9739 		  , x_qs                         => l_qs
9740 		  , x_att                        => l_att
9741 		  , x_atr                        => l_atr
9742 		  , x_sqoh                       => l_sqoh         -- Bug #4141928
9743 		  , x_srqoh                      => l_srqoh                -- Bug #4141928
9744 		  , x_sqr                        => l_sqr                  -- Bug #4141928
9745 		  , x_sqs                        => l_sqs                  -- Bug #4141928
9746 		  , x_satt                       => l_satt                 -- Bug #4141928
9747 		  , x_satr                       => l_satr                 -- Bug #4141928
9748 		 -- , p_lpn_id                =>   p_fromlpn_id      withour lpn_id, only to locator level
9749 		 -- , p_transfer_subinventory_code => l_xfr_sub_code  -- Bug #14753999
9750 		  );
9751 
9752 		  IF (l_return_status = fnd_api.g_ret_sts_success) THEN
9753 			IF (l_debug = 1) THEN
9754 				mydebug('update qty tree back without lpn 2nd time:l_att :' || l_att);
9755 				mydebug('update qty tree back without lpn 2nd time:l_satt:' || l_satt);
9756 			END IF;
9757 		  ELSE
9758 			IF (l_debug = 1) THEN
9759 			  mydebug('calling update qty tree back without lpn 2nd time failed ');
9760 			END IF;
9761 
9762 			fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
9763 			fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
9764 			fnd_msg_pub.ADD;
9765 			RAISE fnd_api.g_exc_error;
9766 		  END IF;
9767 		END IF; --}
9768 	  END IF; --}
9769 
9770 	ELSE
9771 	  -- LPN has just the item requested
9772 	  -- See if quantity/details it has will match the quantity allocated
9773 	  -- Find out if the item is lot/serial controlled and UOM of item
9774 	  -- and compare with transaction details
9775 
9776 		IF (l_debug = 1) THEN
9777 		mydebug('lpn has only the requested item ');
9778 		END IF;
9779 
9780 		SELECT primary_quantity, NVL(secondary_transaction_quantity, 0) -- Bug #4141928
9781 		INTO l_mmtt_qty, l_mmtt_sec_qty           -- Bug #4141928
9782 		FROM mtl_material_transactions_temp
9783 		WHERE transaction_temp_id = p_temp_id;
9784 
9785 	  -- If item is lot controlled then validate the lots
9786 
9787 	IF l_lot_code > 1 THEN --{
9788 			IF (l_debug = 1) THEN
9789 			mydebug('item is lot controlled');
9790 			END IF;
9791 
9792 		-- initialize
9793 		l_check_tolerance := TRUE;
9794 		-- If item is also serial controlled and serial allocation is
9795 		-- on then count the number of serials allocated which exist
9796 		-- in the LPN.
9797 		-- If the count is 0 then raise an error
9798 
9799 		IF p_is_sn_alloc = 'Y' AND p_full_lot_allocation IN ('Y', 'P') --{ Modified for 14699845 (Flexible Lot Allocation)
9800 		   AND p_action = 4 THEN
9801 		  IF (l_debug = 1) THEN
9802 			mydebug('SN control and SN allocation on');
9803 			mydebug('FlexibleLotAlloc: p_is_sn_alloc         '||p_is_sn_alloc);-- Added for 14699845 (Flexible Lot Allocation)
9804 			mydebug('FlexibleLotAlloc: p_full_lot_allocation '||p_full_lot_allocation);-- Added for 14699845 (Flexible Lot Allocation)
9805 		  END IF;
9806 
9807 		  SELECT COUNT(fm_serial_number)
9808 			INTO l_serial_exist_cnt
9809 			FROM mtl_serial_numbers_temp msnt, mtl_transaction_lots_temp mtlt
9810 		   WHERE mtlt.transaction_temp_id = p_temp_id
9811 			 AND msnt.transaction_temp_id = mtlt.serial_transaction_temp_id
9812 			 AND msnt.fm_serial_number IN(
9813 										SELECT serial_number
9814 										  FROM mtl_serial_numbers
9815 										 WHERE lpn_id = p_fromlpn_id
9816 										   AND inventory_item_id = p_item_id
9817 										   AND NVL(revision, '-999') = NVL(p_rev, '-999'));
9818 
9819 		  IF (l_debug = 1) THEN
9820 			mydebug('SN exist count' || l_serial_exist_cnt);
9821 		  END IF;
9822 
9823 		  IF (l_serial_exist_cnt = 0) THEN
9824 			IF (l_debug = 1) THEN
9825 			  mydebug('No serial allocations have occured or LPN does not have the allocated serials ');
9826 			END IF;
9827 
9828 			-- Serial numbers missing for the transaction
9829 			x_match  := 9;
9830 			fnd_message.set_name('INV', 'INV_INT_SERMISEXP');
9831 			fnd_msg_pub.ADD;
9832 			RAISE fnd_api.g_exc_error;
9833 		  END IF;
9834 		END IF; --}
9835 
9836 		-- Check whether the Lots allocated are all in the LPN
9837 		-- An LPN can have many lots and items/revisions, check if the
9838 		-- lots allocated for the item exist in the LPN and if any of
9839 		-- them has quantity less/more than what was suggested.
9840 
9841 		IF (l_debug = 1) THEN
9842 		  mydebug('Check whether the LPN has any lot whose quantity exceeds allocated quantity');
9843 		END IF;
9844 
9845 		l_lpn_pr_qty     := 0;
9846 		l_lpn_sec_qty    := 0;  -- Bug #4141928
9847 		OPEN lot_csr;
9848 
9849 	LOOP
9850 		  FETCH lot_csr INTO l_mtlt_primary_qty, l_mtlt_secondary_qty, l_mtlt_lot_number; -- Bug #4141928
9851 		  EXIT WHEN lot_csr%NOTFOUND;
9852 		  l_lot_match  := 0;
9853 
9854 			IF (l_debug = 1) THEN
9855 			mydebug('l_mtlt_lot_number      : ' || l_mtlt_lot_number);
9856 			mydebug('l_mtlt_primary_qty     : ' || l_mtlt_primary_qty);
9857 			mydebug('l_mtlt_secondary_qty   : ' || l_mtlt_secondary_qty);
9858 			END IF;
9859 
9860 		  l_lot_cnt    := l_lot_cnt - 1;
9861 
9862 		IF NVL(l_allocated_lpn_id, 0) = p_fromlpn_id THEN --{
9863 			--from lpn is the same as allocated_lpn, we need to update qty tree as negative qty
9864 			-- in order to get correct att.
9865 			inv_quantity_tree_pub.update_quantities(
9866 			  p_api_version_number         => 1.0
9867 			, p_init_msg_lst               => fnd_api.g_false
9868 			, x_return_status              => l_return_status
9869 			, x_msg_count                  => l_msg_cnt
9870 			, x_msg_data                   => l_msg_data
9871 			, p_organization_id            => p_org_id
9872 			, p_inventory_item_id          => p_item_id
9873 			, p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
9874 			, p_is_revision_control        => b_is_revision_control
9875 			, p_is_lot_control             => TRUE
9876 			, p_is_serial_control          => b_is_serial_control
9877 			, p_revision                   => NVL(p_rev, NULL)
9878 			, p_lot_number                 => l_mtlt_lot_number
9879 			, p_subinventory_code          => l_sub
9880 			, p_locator_id                 => l_loc_id
9881 			, p_primary_quantity           => -l_mtlt_primary_qty
9882 			, p_secondary_quantity         => -l_mtlt_secondary_qty -- Bug #4141928
9883 			, p_quantity_type              => inv_quantity_tree_pvt.g_qs_txn
9884 			, x_qoh                        => l_qoh
9885 			, x_rqoh                       => l_rqoh
9886 			, x_qr                         => l_qr
9887 			, x_qs                         => l_qs
9888 			, x_att                        => l_att
9889 			, x_atr                        => l_atr
9890 			, x_sqoh                       => l_sqoh        -- Bug #4141928
9891 			, x_srqoh                      => l_srqoh                -- Bug #4141928
9892 			, x_sqr                        => l_sqr                  -- Bug #4141928
9893 			, x_sqs                        => l_sqs                  -- Bug #4141928
9894 			, x_satt                       => l_satt                 -- Bug #4141928
9895 			, x_satr                       => l_satr                 -- Bug #4141928
9896 			, p_lpn_id                     => p_fromlpn_id
9897 		   -- , p_transfer_subinventory_code => l_xfr_sub_code       -- Bug #14753999
9898 			);
9899 
9900 			IF (l_return_status = fnd_api.g_ret_sts_success) THEN
9901 				IF (l_debug = 1) THEN
9902 				mydebug('update qty tree 3rd time for lpn l_att :' || l_att || ' for lot:' || l_mtlt_lot_number);
9903 				mydebug('update qty tree 3rd time for lpn l_satt:' || l_satt || ' for lot:' || l_mtlt_lot_number);  -- Bug #4141928
9904 				END IF;
9905 			ELSE
9906 				IF (l_debug = 1) THEN
9907 				mydebug('calling update qty tree with lpn 3rd time failed ');
9908 				END IF;
9909 
9910 				fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
9911 				fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
9912 				fnd_msg_pub.ADD;
9913 				RAISE fnd_api.g_exc_error;
9914 			END IF;
9915 		  --Bug#5649056: only update if subinventory and locator match
9916 		ELSIF ( l_lpn_sub = l_mmtt_sub AND l_lpn_loc = l_mmtt_loc ) THEN
9917 			inv_quantity_tree_pub.update_quantities(
9918 			  p_api_version_number         => 1.0
9919 			, p_init_msg_lst               => fnd_api.g_false
9920 			, x_return_status              => l_return_status
9921 			, x_msg_count                  => l_msg_cnt
9922 			, x_msg_data                   => l_msg_data
9923 			, p_organization_id            => p_org_id
9924 			, p_inventory_item_id          => p_item_id
9925 			, p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
9926 			, p_is_revision_control        => b_is_revision_control
9927 			, p_is_lot_control             => TRUE
9928 			, p_is_serial_control          => b_is_serial_control
9929 			, p_revision                   => NVL(p_rev, NULL)
9930 			, p_lot_number                 => l_mtlt_lot_number
9931 			, p_subinventory_code          => l_sub
9932 			, p_locator_id                 => l_loc_id
9933 			, p_primary_quantity           => -l_mtlt_primary_qty
9934 			, p_secondary_quantity         => -l_mtlt_secondary_qty -- Bug #4141928
9935 			, p_quantity_type              => inv_quantity_tree_pvt.g_qs_txn
9936 			, x_qoh                        => l_qoh
9937 			, x_rqoh                       => l_rqoh
9938 			, x_qr                         => l_qr
9939 			, x_qs                         => l_qs
9940 			, x_att                        => l_att
9941 			, x_atr                        => l_atr
9942 			, x_sqoh                       => l_sqoh        -- Bug #4141928
9943 			, x_srqoh                      => l_srqoh                -- Bug #4141928
9944 			, x_sqr                        => l_sqr                  -- Bug #4141928
9945 			, x_sqs                        => l_sqs                  -- Bug #4141928
9946 			, x_satt                       => l_satt                 -- Bug #4141928
9947 			, x_satr                       => l_satr                 -- Bug #4141928
9948 		   -- , p_lpn_id                =>   p_fromlpn_id      withour lpn_id, only to locator level
9949 		   -- , p_transfer_subinventory_code => l_xfr_sub_code       -- Bug #14753999
9950 			);
9951 
9952 			IF (l_return_status = fnd_api.g_ret_sts_success) THEN
9953 				IF (l_debug = 1) THEN
9954 				mydebug('after update without lpn 3rd time l_att :' || l_att || ' for lot:' || l_mtlt_lot_number);
9955 				mydebug('after update without lpn 3rd time l_satt:' || l_satt || ' for lot:' || l_mtlt_lot_number);
9956 			  END IF;
9957 			ELSE
9958 			  IF (l_debug = 1) THEN
9959 				mydebug('calling update qty tree back 3rd time without lpn 3rd time failed ');
9960 			  END IF;
9961 
9962 			  fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
9963 			  fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
9964 			  fnd_msg_pub.ADD;
9965 			  RAISE fnd_api.g_exc_error;
9966 			END IF;
9967 		END IF;--}
9968 
9969 		  inv_quantity_tree_pub.query_quantities(
9970 			p_api_version_number         => 1.0
9971 		  , p_init_msg_lst               => fnd_api.g_false
9972 		  , x_return_status              => l_return_status
9973 		  , x_msg_count                  => l_msg_cnt
9974 		  , x_msg_data                   => l_msg_data
9975 		  , p_organization_id            => p_org_id
9976 		  , p_inventory_item_id          => p_item_id
9977 		  , p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
9978 		  , p_is_revision_control        => b_is_revision_control
9979 		  , p_is_lot_control             => TRUE
9980 		  , p_is_serial_control          => b_is_serial_control
9981 		  , p_demand_source_type_id      => -9999
9982 		  , p_revision                   => NVL(p_rev, NULL)
9983 		  , p_lot_number                 => l_mtlt_lot_number
9984 		  , p_subinventory_code          => l_sub
9985 		  , p_locator_id                 => l_loc_id
9986 		  , x_qoh                        => l_qoh
9987 		  , x_rqoh                       => l_rqoh
9988 		  , x_qr                         => l_qr
9989 		  , x_qs                         => l_qs
9990 		  , x_att                        => l_att
9991 		  , x_atr                        => l_atr
9992 		  , x_sqoh                       => l_sqoh         -- Bug #4141928
9993 		  , x_srqoh                      => l_srqoh                -- Bug #4141928
9994 		  , x_sqr                        => l_sqr                  -- Bug #4141928
9995 		  , x_sqs                        => l_sqs                  -- Bug #4141928
9996 		  , x_satt                       => l_satt                 -- Bug #4141928
9997 		  , x_satr                       => l_satr                 -- Bug #4141928
9998 		  , p_lpn_id                     => p_fromlpn_id
9999 		 -- , p_transfer_subinventory_code => l_xfr_sub_code       -- Bug #14753999
10000 		  , p_grade_code                 => NULL                   -- Bug #4141928
10001 		  );
10002 
10003 		IF (l_return_status = fnd_api.g_ret_sts_success) THEN --{
10004 			l_lot_match  := 1;
10005 
10006 			IF ((l_fulfillment_base = 'P' AND l_att > 0) OR (l_fulfillment_base = 'S' AND l_satt > 0)) THEN
10007 				l_table_index  := l_table_index + 1;
10008 				-- bug 3547725, now no matter what relation it is between l_mtlt_primary_qty and l_att
10009 
10010 				l_lpn_pr_qty  := l_lpn_pr_qty + l_att;
10011 				l_lpn_sec_qty := l_lpn_sec_qty + l_satt;
10012 				IF ((l_fulfillment_base = 'P' AND l_att < l_qoh) OR (l_fulfillment_base = 'S' AND l_satt < l_sqoh)) THEN
10013 					l_check_tolerance := FALSE;
10014 					IF (l_debug = 1) THEN
10015 					mydebug('l_att < l_qoh: set l_check_tolerance to false');
10016 					END IF;
10017 				END IF;
10018 
10019 				IF (l_debug = 1) THEN
10020 				  mydebug('l_table_index:' || l_table_index || ' lot_number:' || l_mtlt_lot_number || ' qty:' || l_att || ' secondary qty:' || l_satt);
10021 				END IF;
10022 
10023 				t_lpn_lot_qty_table(l_table_index).lpn_id      := p_fromlpn_id;
10024 				t_lpn_lot_qty_table(l_table_index).lot_number  := l_mtlt_lot_number;
10025 				t_lpn_lot_qty_table(l_table_index).pri_qty := l_att;
10026 				t_lpn_lot_qty_table(l_table_index).sec_qty := l_satt;
10027 
10028 				IF(l_fulfillment_base = 'S') THEN
10029 					IF (l_secondary_uom = p_trx_uom) THEN
10030 						t_lpn_lot_qty_table(l_table_index).trx_qty := l_satt;
10031 					ELSIF (l_primary_uom = p_trx_uom) THEN
10032 						t_lpn_lot_qty_table(l_table_index).trx_qty := l_att;
10033 					ELSE
10034 						t_lpn_lot_qty_table(l_table_index).trx_qty := inv_convert.inv_um_convert(
10035 												 item_id        => p_item_id
10036 												,lot_number		=> l_mtlt_lot_number
10037 												,organization_id=> p_org_id
10038 												,precision      => null
10039 												,from_quantity  => l_satt
10040 												,from_unit      => l_secondary_uom
10041 												,to_unit        => p_trx_uom
10042 												,from_name      => null
10043 												,to_name        => null);
10044 					END IF;
10045 				ELSE
10046 					IF (l_primary_uom = p_trx_uom) THEN
10047 						t_lpn_lot_qty_table(l_table_index).trx_qty := l_att;
10048 					ELSIF (l_secondary_uom = p_trx_uom) THEN
10049 						t_lpn_lot_qty_table(l_table_index).trx_qty := l_satt;
10050 					ELSE
10051 						t_lpn_lot_qty_table(l_table_index).trx_qty := inv_convert.inv_um_convert(
10052 												 item_id        => p_item_id
10053 												,lot_number		=> l_mtlt_lot_number
10054 												,organization_id=> p_org_id
10055 												,precision      => null
10056 												,from_quantity  => l_att
10057 												,from_unit      => l_primary_uom
10058 												,to_unit        => p_trx_uom
10059 												,from_name      => null
10060 												,to_name        => null);
10061 					END IF;
10062 				END IF;
10063 			ELSE
10064 				IF (l_debug = 1) THEN
10065 				mydebug('LPN does not have lot ' || l_mtlt_lot_number);
10066 				END IF;
10067 
10068 				IF x_match <> 4 THEN
10069 					x_match  := 3;
10070 				END IF;
10071 
10072 			  l_lot_match  := 0;
10073 			  l_lot_cnt    := l_lot_cnt + 1;
10074 			  l_check_tolerance := FALSE;
10075 			  IF (l_debug = 1) THEN
10076 				  mydebug('LPN does not have lot ' || l_mtlt_lot_number);
10077 				  mydebug('set l_check_tolerance to false');
10078 			  END IF;
10079 			END IF;
10080 		ELSE
10081 			IF (l_debug = 1) THEN
10082 			  mydebug('calling qty tree 3rd time failed ');
10083 			END IF;
10084 
10085 			fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
10086 			fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
10087 			fnd_msg_pub.ADD;
10088 			RAISE fnd_api.g_exc_error;
10089 		END IF; --}
10090 
10091 			IF (l_debug = 1) THEN
10092 			mydebug('x_match ' ||x_match);
10093 			END IF;
10094 
10095 			IF (l_lot_match <> 0) AND (x_match <> 4) THEN --{
10096 
10097 				IF ((l_fulfillment_base = 'P' AND l_mtlt_primary_qty < l_att) OR (l_fulfillment_base = 'S' AND l_mtlt_secondary_qty < l_satt)) THEN --{
10098 					IF (l_debug = 1) THEN
10099 					mydebug('Qty in LPN for lot ' || l_mtlt_lot_number || ' is more than transaction qty for that lot');
10100 					END IF;
10101 					x_match  := 4;
10102 
10103 				ELSIF ((l_fulfillment_base = 'P' AND l_mtlt_primary_qty > l_att) OR (l_fulfillment_base = 'S' AND l_mtlt_secondary_qty > l_satt)) THEN
10104 
10105 					IF ((l_fulfillment_base = 'P' AND l_qoh = l_att) OR (l_fulfillment_base = 'S' AND l_sqoh = l_satt)) THEN --{
10106 						IF (l_debug = 1) THEN
10107 						mydebug('Qty in LPN for lot ' || l_mtlt_lot_number || ' is less than transaction qty for that lot');
10108 						END IF;
10109 						x_match  := 3;
10110 
10111 						IF (l_lot_string IS NULL) THEN
10112 							l_lot_string := l_mtlt_lot_number;
10113 						ELSE
10114 							l_lot_string := l_lot_string ||':'||l_mtlt_lot_number;
10115 						END IF;
10116 
10117 						IF (l_lot_qty_string IS NULL ) THEN
10118 							l_lot_qty_string := l_att;
10119 						ELSE
10120 							l_lot_qty_string := l_lot_qty_string || ':'||l_att;
10121 						END IF;
10122 
10123 						-- Bug #4141928. Build the sec lot qty string
10124 						IF (l_lot_sec_qty_string IS NULL ) THEN
10125 							l_lot_sec_qty_string := l_satt;
10126 						ELSE
10127 							l_lot_sec_qty_string := l_lot_sec_qty_string || ':'||l_satt;
10128 						END IF;
10129 
10130 						l_sec_qty_str := l_lot_sec_qty_string;
10131 
10132 						IF (l_debug = 1) THEN
10133 						mydebug('l_lot_string           :'||l_lot_string);
10134 						mydebug('l_lot_qty_string       :'||l_lot_qty_string);
10135 						mydebug('l_lot_sec_qty_string   :'||l_lot_sec_qty_string);
10136 						END IF;
10137 
10138 					ELSE  --l_qoh > l_att for primary based fulfillment or l_sqoh > l_satt for secondary based fulfillment
10139 						IF (l_debug = 1) THEN
10140 						mydebug('Qty in LPN for lot '|| l_mtlt_lot_number|| ' is less than transaction qty for that lot and ATT < onhand');
10141 						END IF;
10142 						x_match  := 4;
10143 					END IF;--}
10144 				ELSE
10145 					IF x_match <> 3 THEN
10146 						IF (l_debug = 1) THEN
10147 						mydebug('Qty in LPN for lot ' || l_mtlt_lot_number || ' is equal to transaction qty for that lot');
10148 						END IF;
10149 
10150 						IF ((l_fulfillment_base = 'P' AND l_qoh = l_att) OR (l_fulfillment_base = 'S' AND l_sqoh = l_satt)) THEN
10151 							IF (l_debug = 1) THEN
10152 							mydebug('LPN qoh is equal to att. Exact match');
10153 							END IF;
10154 							l_sec_qty_str := l_lot_sec_qty_string;
10155 							x_match  := 1;
10156 
10157 						ELSE
10158 							IF (l_debug = 1) THEN
10159 							mydebug('LPN qoh is great than att. part of lpn is match');
10160 							END IF;
10161 							x_match  := 4;
10162 
10163 						END IF;
10164 					END IF;
10165 				END IF; --}
10166 		  END IF; --}
10167 
10168 			IF x_match <> 4 THEN
10169 				IF (l_debug = 1) THEN
10170 				mydebug('x_match is not 4.');
10171 				END IF;
10172 
10173 				IF x_match <> 1 THEN
10174 					l_check_tolerance := FALSE;
10175 					IF (l_debug = 1) THEN
10176 					mydebug('x_match is not 1, set l_check_tolerance to false');
10177 					END IF;
10178 				ELSE
10179 					IF (l_debug = 1) THEN
10180 					mydebug('x_match is 1 so far');
10181 					END IF;
10182 				END IF;
10183 			ELSE
10184 				IF (l_debug = 1) THEN
10185 				mydebug('x_match is 4');
10186 				END IF;
10187 
10188 				IF l_check_tolerance THEN
10189 					IF (l_debug = 1) THEN
10190 					mydebug('l_check_tolerance is true');
10191 					END IF;
10192 					IF ((l_fulfillment_base = 'P' AND l_mtlt_primary_qty > l_att) OR (l_fulfillment_base = 'S' AND l_mtlt_secondary_qty > l_satt))THEN
10193 						l_check_tolerance := FALSE;
10194 
10195 						IF (l_debug = 1) THEN
10196 						mydebug('lpn has less qty than transaction qty for that lot. set l_check_tolerance to false');
10197 						END IF;
10198 					ELSE    -- in multiple lots case, since l_check_tolerance is for each lot, we will not
10199 							-- set l_check_tolerance to false when lpn_lot_qty = allocated_lot_qty.
10200 						IF (l_debug = 1) THEN
10201 						mydebug('LPN has more or equal qty than transaction qty for that lot');
10202 						END IF;
10203 
10204 						IF ((l_fulfillment_base = 'P' AND l_qoh > l_att) OR (l_fulfillment_base = 'S' AND l_sqoh > l_satt))  THEN
10205 							l_check_tolerance := FALSE;
10206 							IF (l_debug = 1) THEN
10207 							mydebug('Either l_qoh > l_att for primary based fulfillment or l_sqoh > l_satt for sec based fulfillment so set l_check_tolerance to false');
10208 							END IF;
10209 						END IF;
10210 					END IF;
10211 				END IF;
10212 			END IF;
10213 
10214 		  IF NVL(l_allocated_lpn_id, 0) = p_fromlpn_id THEN --{
10215 			--from lpn is the same as allocated_lpn, we need to update qty tree as negative qty
10216 			--in order to get correct att.
10217 			inv_quantity_tree_pub.update_quantities(
10218 			  p_api_version_number         => 1.0
10219 			, p_init_msg_lst               => fnd_api.g_false
10220 			, x_return_status              => l_return_status
10221 			, x_msg_count                  => l_msg_cnt
10222 			, x_msg_data                   => l_msg_data
10223 			, p_organization_id            => p_org_id
10224 			, p_inventory_item_id          => p_item_id
10225 			, p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
10226 			, p_is_revision_control        => b_is_revision_control
10227 			, p_is_lot_control             => TRUE
10228 			, p_is_serial_control          => b_is_serial_control
10229 			, p_revision                   => NVL(p_rev, NULL)
10230 			, p_lot_number                 => l_mtlt_lot_number
10231 			, p_subinventory_code          => l_sub
10232 			, p_locator_id                 => l_loc_id
10233 			, p_primary_quantity           => l_mtlt_primary_qty
10234 			, p_secondary_quantity         => l_mtlt_secondary_qty -- Bug #4141928
10235 			, p_quantity_type              => inv_quantity_tree_pvt.g_qs_txn
10236 			, x_qoh                        => l_qoh
10237 			, x_rqoh                       => l_rqoh
10238 			, x_qr                         => l_qr
10239 			, x_qs                         => l_qs
10240 			, x_att                        => l_att
10241 			, x_atr                        => l_atr
10242 			, x_sqoh                       => l_sqoh                 -- Bug #4141928
10243 			, x_srqoh                      => l_srqoh                -- Bug #4141928
10244 			, x_sqr                        => l_sqr                  -- Bug #4141928
10245 			, x_sqs                        => l_sqs                  -- Bug #4141928
10246 			, x_satt                       => l_satt                 -- Bug #4141928
10247 			, x_satr                       => l_satr                 -- Bug #4141928
10248 			, p_lpn_id                     => p_fromlpn_id
10249 			--, p_transfer_subinventory_code => l_xfr_sub_code       -- Bug #14753999
10250 			);
10251 
10252 			IF (l_return_status = fnd_api.g_ret_sts_success) THEN
10253 				IF (l_debug = 1) THEN
10254 				mydebug('update qty tree back 3rd time for lpn l_att :' || l_att || ' for lot:' || l_mtlt_lot_number);
10255 				mydebug('update qty tree back 3rd time for lpn l_satt:' || l_satt || ' for lot:' || l_mtlt_lot_number);  -- Bug #4141928
10256 				END IF;
10257 			ELSE
10258 				IF (l_debug = 1) THEN
10259 				mydebug('calling update qty tree with lpn 3rd time failed ');
10260 				END IF;
10261 
10262 				fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
10263 				fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
10264 				fnd_msg_pub.ADD;
10265 				RAISE fnd_api.g_exc_error;
10266 			END IF;
10267 		  --Bug#5649056: only update if subinventory and locator match
10268 		ELSIF ( l_lpn_sub = l_mmtt_sub AND l_lpn_loc = l_mmtt_loc ) THEN
10269 			inv_quantity_tree_pub.update_quantities(
10270 			  p_api_version_number         => 1.0
10271 			, p_init_msg_lst               => fnd_api.g_false
10272 			, x_return_status              => l_return_status
10273 			, x_msg_count                  => l_msg_cnt
10274 			, x_msg_data                   => l_msg_data
10275 			, p_organization_id            => p_org_id
10276 			, p_inventory_item_id          => p_item_id
10277 			, p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
10278 			, p_is_revision_control        => b_is_revision_control
10279 			, p_is_lot_control             => TRUE
10280 			, p_is_serial_control          => b_is_serial_control
10281 			, p_revision                   => NVL(p_rev, NULL)
10282 			, p_lot_number                 => l_mtlt_lot_number
10283 			, p_subinventory_code          => l_sub
10284 			, p_locator_id                 => l_loc_id
10285 			, p_primary_quantity           => l_mtlt_primary_qty
10286 			, p_secondary_quantity         => l_mtlt_secondary_qty -- Bug #4141928
10287 			, p_quantity_type              => inv_quantity_tree_pvt.g_qs_txn
10288 			, x_qoh                        => l_qoh
10289 			, x_rqoh                       => l_rqoh
10290 			, x_qr                         => l_qr
10291 			, x_qs                         => l_qs
10292 			, x_att                        => l_att
10293 			, x_atr                        => l_atr
10294 			, x_sqoh                       => l_sqoh        -- Bug #4141928
10295 			, x_srqoh                      => l_srqoh                -- Bug #4141928
10296 			, x_sqr                        => l_sqr                  -- Bug #4141928
10297 			, x_sqs                        => l_sqs                  -- Bug #4141928
10298 			, x_satt                       => l_satt                 -- Bug #4141928
10299 			, x_satr                       => l_satr                 -- Bug #4141928
10300 			--  , p_lpn_id                =>   p_fromlpn_id      withour lpn_id, only to locator level
10301 			--, p_transfer_subinventory_code => l_xfr_sub_code       -- Bug #14753999
10302 			);
10303 
10304 			IF (l_return_status = fnd_api.g_ret_sts_success) THEN
10305 			  IF (l_debug = 1) THEN
10306 				mydebug('after update qty tree back without lpn 3rd time l_att :' || l_att || ' for lot:' || l_mtlt_lot_number);
10307 				mydebug('after update qty tree back without lpn 3rd time l_satt:' || l_satt || ' for lot:' || l_mtlt_lot_number);   -- Bug #4141928
10308 			  END IF;
10309 			ELSE
10310 			  IF (l_debug = 1) THEN
10311 				mydebug('calling update qty tree back without lpn 3rd time failed ');
10312 			  END IF;
10313 
10314 			  fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
10315 			  fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
10316 			  fnd_msg_pub.ADD;
10317 			  RAISE fnd_api.g_exc_error;
10318 			END IF;
10319 		END IF; --}
10320 
10321 	END LOOP;
10322 
10323 		CLOSE lot_csr;
10324 
10325 		IF l_lot_cnt > 0 AND p_changelotNoException = 'N' THEN --/* Bug 9448490 Lot Substitution Project */
10326 			IF (l_debug = 1) THEN
10327 				mydebug('l_lot_cnt: '||l_lot_cnt||' l_lot_cnt > 0');
10328 				mydebug('set l_check_tolerance to false');
10329 			END IF;
10330 		   l_check_tolerance := FALSE;
10331 		   x_match  := 4;
10332 		END IF;
10333 
10334 		-- Now that all the lots have been validated, check whether the serial
10335 		-- numbers allocated match the ones in the lpn.
10336 
10337 	-- Bug #4141928. No changes required for OPM convergence.
10338 	-- since this is a serial context
10339 		IF p_is_sn_alloc = 'Y' AND p_full_lot_allocation IN ('Y', 'P') -- Modified for 14699845 (Flexible Lot Allocation)
10340 		   AND p_action = 4    THEN
10341 
10342 		   SELECT COUNT(fm_serial_number)
10343 			 INTO l_total_serial_cnt
10344 			 FROM mtl_serial_numbers_temp msnt, mtl_transaction_lots_temp mtlt
10345 			WHERE mtlt.transaction_temp_id = p_temp_id
10346 			  AND msnt.transaction_temp_id = mtlt.serial_transaction_temp_id;
10347 
10348 		   IF (l_debug = 1) THEN
10349 				mydebug('SN tot count' || l_total_serial_cnt);
10350 		   END IF;
10351 
10352 		   IF (x_match = 1
10353 			   OR x_match = 3 ) THEN
10354 				IF (l_total_serial_cnt = l_serial_exist_cnt) THEN
10355 				  IF (l_debug = 1) THEN
10356 					mydebug('LPN matches exactly');
10357 				  END IF;
10358 				  x_match  := 1;
10359 				ELSIF(l_total_serial_cnt > l_serial_exist_cnt) THEN
10360 				  IF (l_debug = 1) THEN
10361 					mydebug('LPN has less');
10362 				  END IF;
10363 				  x_match  := 3;
10364 				ELSE
10365 				  IF (l_debug = 1) THEN
10366 					mydebug('LPN has extra serials');
10367 				  END IF;
10368 				  x_match  := 4;
10369 				END IF;
10370 			END IF;
10371 			IF (l_check_tolerance) THEN
10372 				IF (l_total_serial_cnt > l_serial_exist_cnt) THEN
10373 					IF (l_debug = 1) THEN
10374 						 mydebug('There are serials which is not inside the lpn. set l_check_tolerance to false');
10375 					END IF;
10376 					l_check_tolerance := false;
10377 				END IF;
10378 			END IF;
10379 		END IF;
10380 
10381 			IF l_check_tolerance THEN
10382 				IF(l_fulfillment_base = 'S') THEN
10383 					l_overpicked_qty := l_lpn_sec_qty - l_sec_qty;
10384 				ELSE
10385 					l_overpicked_qty := l_lpn_pr_qty - l_pr_qty;
10386 				END IF;
10387 
10388 				IF (l_debug = 1) THEN
10389 				mydebug('end of mutiple lots, l_check_tolerance is true and l_overpicked_qty: '||l_overpicked_qty);
10390 				END IF;
10391 			END IF;
10392 
10393 	ELSE -- Item is not lot controlled
10394 			IF (l_debug = 1) THEN
10395 			mydebug('Not Lot controlled ..');
10396 			END IF;
10397 
10398 			l_check_tolerance := FALSE;
10399 			-- Check serial numbers if serial controlled and serial
10400 			-- allocation is turned on
10401 
10402 			IF p_is_sn_alloc = 'Y' AND p_action = 4 THEN --{
10403 				IF (l_debug = 1) THEN
10404 				mydebug('SN control and SN allocation on');
10405 				END IF;
10406 
10407 				SELECT COUNT(fm_serial_number)
10408 				INTO l_serial_exist_cnt
10409 				FROM mtl_serial_numbers_temp msnt
10410 				WHERE msnt.transaction_temp_id = p_temp_id
10411 				AND msnt.fm_serial_number IN(
10412 										SELECT serial_number
10413 										  FROM mtl_serial_numbers
10414 										 WHERE lpn_id = p_fromlpn_id
10415 										   AND inventory_item_id = p_item_id
10416 										   AND NVL(revision, '-999') = NVL(p_rev, '-999'));
10417 
10418 
10419 				IF (l_debug = 1) THEN
10420 				mydebug('SN exist count' || l_serial_exist_cnt);
10421 				END IF;
10422 
10423 				IF (l_serial_exist_cnt = 0) THEN
10424 					IF (l_debug = 1) THEN
10425 					mydebug('LPN does not have the allocated serials ');
10426 					END IF;
10427 
10428 					-- Serial numbers missing for the transaction
10429 					x_match  := 9;
10430 					fnd_message.set_name('INV', 'INV_INT_SERMISEXP');
10431 					fnd_msg_pub.ADD;
10432 					RAISE fnd_api.g_exc_error;
10433 				END IF;
10434 			END IF; --}
10435 
10436 
10437 
10438 			IF (l_debug = 1) THEN
10439 			mydebug('get lpn quantity ');
10440 			END IF;
10441 
10442 			IF NVL(l_allocated_lpn_id, 0) = p_fromlpn_id THEN --{
10443 			--from lpn is the same as allocated_lpn, we need to update qty tree as negative qty
10444 			-- in order to get correct att.
10445 			inv_quantity_tree_pub.update_quantities(
10446 			p_api_version_number         => 1.0
10447 		  , p_init_msg_lst               => fnd_api.g_false
10448 		  , x_return_status              => l_return_status
10449 		  , x_msg_count                  => l_msg_cnt
10450 		  , x_msg_data                   => l_msg_data
10451 		  , p_organization_id            => p_org_id
10452 		  , p_inventory_item_id          => p_item_id
10453 		  , p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
10454 		  , p_is_revision_control        => b_is_revision_control
10455 		  , p_is_lot_control             => FALSE
10456 		  , p_is_serial_control          => b_is_serial_control
10457 		  , p_revision                   => NVL(p_rev, NULL)
10458 		  , p_lot_number                 => NULL
10459 		  , p_subinventory_code          => l_sub
10460 		  , p_locator_id                 => l_loc_id
10461 		  , p_primary_quantity           => -l_mmtt_qty
10462 		  , p_secondary_quantity         => -l_mmtt_sec_qty  -- Bug #4141928
10463 		  , p_quantity_type              => inv_quantity_tree_pvt.g_qs_txn
10464 		  , x_qoh                        => l_qoh
10465 		  , x_rqoh                       => l_rqoh
10466 		  , x_qr                         => l_qr
10467 		  , x_qs                         => l_qs
10468 		  , x_att                        => l_att
10469 		  , x_atr                        => l_atr
10470 		  , x_sqoh                       => l_sqoh                 -- Bug #4141928
10471 		  , x_srqoh                      => l_srqoh                -- Bug #4141928
10472 		  , x_sqr                        => l_sqr                  -- Bug #4141928
10473 		  , x_sqs                        => l_sqs                  -- Bug #4141928
10474 		  , x_satt                       => l_satt                 -- Bug #4141928
10475 		  , x_satr                       => l_satr                 -- Bug #4141928
10476 		  , p_lpn_id                     => p_fromlpn_id
10477 		  --, p_transfer_subinventory_code => l_xfr_sub_code    -- Bug #14753999
10478 		  );
10479 
10480 				IF (l_return_status = fnd_api.g_ret_sts_success) THEN
10481 					IF (l_debug = 1) THEN
10482 					mydebug('update qty tree with lpn 4th time: l_att :' || l_att);
10483 					mydebug('update qty tree with lpn 4th time: l_satt:' || l_satt);
10484 					END IF;
10485 				ELSE
10486 					IF (l_debug = 1) THEN
10487 					mydebug('calling update qty tree with lpn 4th time failed ');
10488 					END IF;
10489 
10490 					fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
10491 					fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
10492 					fnd_msg_pub.ADD;
10493 					RAISE fnd_api.g_exc_error;
10494 				END IF;
10495 			--Bug#5649056: only update if subinventory and locator match
10496 			ELSIF ( l_lpn_sub = l_mmtt_sub AND l_lpn_loc = l_mmtt_loc ) THEN
10497 
10498 			inv_quantity_tree_pub.update_quantities(
10499 			p_api_version_number         => 1.0
10500 		  , p_init_msg_lst               => fnd_api.g_false
10501 		  , x_return_status              => l_return_status
10502 		  , x_msg_count                  => l_msg_cnt
10503 		  , x_msg_data                   => l_msg_data
10504 		  , p_organization_id            => p_org_id
10505 		  , p_inventory_item_id          => p_item_id
10506 		  , p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
10507 		  , p_is_revision_control        => b_is_revision_control
10508 		  , p_is_lot_control             => FALSE
10509 		  , p_is_serial_control          => b_is_serial_control
10510 		  , p_revision                   => NVL(p_rev, NULL)
10511 		  , p_lot_number                 => NULL
10512 		  , p_subinventory_code          => l_sub
10513 		  , p_locator_id                 => l_loc_id
10514 		  , p_primary_quantity           => -l_mmtt_qty
10515 		  , p_secondary_quantity         => -l_mmtt_sec_qty  -- Bug #4141928
10516 		  , p_quantity_type              => inv_quantity_tree_pvt.g_qs_txn
10517 		  , x_qoh                        => l_qoh
10518 		  , x_rqoh                       => l_rqoh
10519 		  , x_qr                         => l_qr
10520 		  , x_qs                         => l_qs
10521 		  , x_att                        => l_att
10522 		  , x_atr                        => l_atr
10523 		  , x_sqoh                       => l_sqoh                 -- Bug #4141928
10524 		  , x_srqoh                      => l_srqoh                -- Bug #4141928
10525 		  , x_sqr                        => l_sqr                  -- Bug #4141928
10526 		  , x_sqs                        => l_sqs                  -- Bug #4141928
10527 		  , x_satt                       => l_satt                 -- Bug #4141928
10528 		  , x_satr                       => l_satr                 -- Bug #4141928
10529 		  -- , p_lpn_id                =>   p_fromlpn_id      withour lpn_id, only to locator level
10530 		  -- , p_transfer_subinventory_code => l_xfr_sub_code     -- Bug #14753999
10531 		  );
10532 
10533 			IF (l_return_status = fnd_api.g_ret_sts_success) THEN
10534 				IF (l_debug = 1) THEN
10535 				mydebug('update qty tree without lpn 4th time:l_att :' || l_att);
10536 				mydebug('update qty tree without lpn 4th time:l_satt:' || l_satt);  -- Bug #4141928
10537 				END IF;
10538 			ELSE
10539 				IF (l_debug = 1) THEN
10540 				mydebug('calling update qty tree without lpn 4th time failed ');
10541 				END IF;
10542 
10543 				fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
10544 				fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
10545 				fnd_msg_pub.ADD;
10546 				RAISE fnd_api.g_exc_error;
10547 			END IF;
10548 		END IF; --}
10549 
10550 		inv_quantity_tree_pub.query_quantities(
10551 		  p_api_version_number         => 1.0
10552 		, p_init_msg_lst               => fnd_api.g_false
10553 		, x_return_status              => l_return_status
10554 		, x_msg_count                  => l_msg_cnt
10555 		, x_msg_data                   => l_msg_data
10556 		, p_organization_id            => p_org_id
10557 		, p_inventory_item_id          => p_item_id
10558 		, p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode --??
10559 		, p_is_revision_control        => b_is_revision_control
10560 		, p_is_lot_control             => FALSE
10561 		, p_is_serial_control          => b_is_serial_control
10562 		, p_demand_source_type_id      => -9999
10563 		, p_revision                   => NVL(p_rev, NULL)
10564 		, p_lot_number                 => NULL
10565 		, p_subinventory_code          => l_sub
10566 		, p_locator_id                 => l_loc_id
10567 		, x_qoh                        => l_qoh
10568 		, x_rqoh                       => l_rqoh
10569 		, x_qr                         => l_qr
10570 		, x_qs                         => l_qs
10571 		, x_att                        => l_att
10572 		, x_atr                        => l_atr
10573 		, x_sqoh                       => l_sqoh         -- Bug #4141928
10574 		, x_srqoh                      => l_srqoh                -- Bug #4141928
10575 		, x_sqr                        => l_sqr                  -- Bug #4141928
10576 		, x_sqs                        => l_sqs                  -- Bug #4141928
10577 		, x_satt                       => l_satt                 -- Bug #4141928
10578 		, x_satr                       => l_satr                 -- Bug #4141928
10579 		, p_lpn_id                     => p_fromlpn_id
10580 	   -- , p_transfer_subinventory_code => l_xfr_sub_code    -- Bug #14753999
10581 		, p_grade_code                 => NULL                   -- Bug #4141928
10582 		);
10583 
10584 		IF (l_return_status = fnd_api.g_ret_sts_success) THEN --{
10585 			IF (l_debug = 1) THEN
10586 			mydebug('lpn quantity = ' || l_att);
10587 			mydebug('lpn sec quantity = ' || l_satt);
10588 			END IF;
10589 
10590 			IF ((l_fulfillment_base = 'P' AND l_mmtt_qty = l_att) OR (l_fulfillment_base = 'S' AND l_mmtt_sec_qty = l_satt)) THEN
10591 				IF ((l_fulfillment_base = 'P' AND l_qoh = l_att) OR (l_fulfillment_base = 'S' AND l_sqoh = l_satt)) THEN
10592 					-- LPN is a match!
10593 					IF (l_debug = 1) THEN
10594 					mydebug('LPN matched');
10595 					END IF;
10596 					l_sec_qty_str := l_lpn_sec_qty;
10597 					x_match  := 1;
10598 
10599 				ELSE
10600 					-- LPN is for multiple task
10601 					IF (l_debug = 1) THEN
10602 					mydebug('LPN has multiple task.');
10603 					END IF;
10604 					x_match  := 4;
10605 				END IF;
10606 
10607 			ELSIF ((l_fulfillment_base = 'P' AND l_mmtt_qty > l_att) OR (l_fulfillment_base = 'S' AND l_mmtt_sec_qty > l_satt)) THEN
10608 				IF ((l_fulfillment_base = 'P' AND l_qoh = l_att) OR (l_fulfillment_base = 'S' AND l_sqoh = l_satt)) THEN
10609 					IF (l_debug = 1) THEN
10610 					mydebug('lpn has less requested qty and lpn is whole allocation');
10611 					END IF;
10612 
10613 					l_sec_qty_str := l_lpn_sec_qty;
10614 					x_match  := 3;
10615 				ELSE
10616 					IF (l_debug = 1) THEN
10617 					mydebug('lpn has less than requested qty and lpn is partial allocation');
10618 					END IF;
10619 					x_match  := 4;
10620 				END IF;
10621 
10622 				l_lpn_pr_qty  := l_att;
10623 				l_lpn_sec_qty  := l_satt;
10624 			ELSE
10625 				x_match  := 4;
10626 
10627 				--bug 3547725
10628 				l_lpn_pr_qty := l_att;
10629 				l_lpn_sec_qty  := l_satt;
10630 
10631 				IF ((l_fulfillment_base = 'P' AND l_qoh = l_att) OR (l_fulfillment_base = 'S' AND l_sqoh = l_satt)) THEN
10632 					--{{ calculate l_overpicked_qty for vanilla and serial item}}
10633 					l_check_tolerance := TRUE;
10634 					IF l_fulfillment_base = 'S' THEN
10635 						l_overpicked_qty := l_lpn_sec_qty -  l_mmtt_sec_qty;
10636 					ELSE
10637 						l_overpicked_qty := l_lpn_pr_qty -  l_mmtt_qty;
10638 					END IF;
10639 					IF (l_debug = 1) THEN
10640 					mydebug('lpn has more than requested qty for loose or serial controlled item.');
10641 					mydebug('l_over_picked_qty is: '||l_overpicked_qty);
10642 					END IF;
10643 				END IF;
10644 			END IF;
10645 		ELSE
10646 			IF (l_debug = 1) THEN
10647 			mydebug('calling qty tree 4th time failed');
10648 			END IF;
10649 
10650 			fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
10651 			fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
10652 			fnd_msg_pub.ADD;
10653 			RAISE fnd_api.g_exc_error;
10654 		END IF; --}
10655 
10656 		IF NVL(l_allocated_lpn_id, 0) = p_fromlpn_id THEN --{
10657 		  --from lpn is the same as allocated_lpn, we need to update qty tree as negative qty
10658 		  -- in order to get correct att.
10659 		  inv_quantity_tree_pub.update_quantities(
10660 			p_api_version_number         => 1.0
10661 		  , p_init_msg_lst               => fnd_api.g_false
10662 		  , x_return_status              => l_return_status
10663 		  , x_msg_count                  => l_msg_cnt
10664 		  , x_msg_data                   => l_msg_data
10665 		  , p_organization_id            => p_org_id
10666 		  , p_inventory_item_id          => p_item_id
10667 		  , p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
10668 		  , p_is_revision_control        => b_is_revision_control
10669 		  , p_is_lot_control             => FALSE
10670 		  , p_is_serial_control          => b_is_serial_control
10671 		  , p_revision                   => NVL(p_rev, NULL)
10672 		  , p_lot_number                 => NULL
10673 		  , p_subinventory_code          => l_sub
10674 		  , p_locator_id                 => l_loc_id
10675 		  , p_primary_quantity           => l_mmtt_qty
10676 		  , p_secondary_quantity         => l_mmtt_sec_qty -- Bug #4141928
10677 		  , p_quantity_type              => inv_quantity_tree_pvt.g_qs_txn
10678 		  , x_qoh                        => l_qoh
10679 		  , x_rqoh                       => l_rqoh
10680 		  , x_qr                         => l_qr
10681 		  , x_qs                         => l_qs
10682 		  , x_att                        => l_att
10683 		  , x_atr                        => l_atr
10684 		  , x_sqoh                       => l_sqoh         -- Bug #4141928
10685 		  , x_srqoh                      => l_srqoh                -- Bug #4141928
10686 		  , x_sqr                        => l_sqr                  -- Bug #4141928
10687 		  , x_sqs                        => l_sqs                  -- Bug #4141928
10688 		  , x_satt                       => l_satt                 -- Bug #4141928
10689 		  , x_satr                       => l_satr                 -- Bug #4141928
10690 		  , p_lpn_id                     => p_fromlpn_id
10691 		  --, p_transfer_subinventory_code => l_xfr_sub_code  -- Bug #14753999
10692 		  );
10693 
10694 			IF (l_return_status = fnd_api.g_ret_sts_success) THEN
10695 				IF (l_debug = 1) THEN
10696 				mydebug('update qty tree back with lpn 4th time: l_att :' || l_att);
10697 				mydebug('update qty tree back with lpn 4th time: l_satt:' || l_satt);  -- Bug #4141928
10698 				END IF;
10699 			ELSE
10700 				IF (l_debug = 1) THEN
10701 				mydebug('calling update qty tree back with lpn 4th time failed ');
10702 				END IF;
10703 
10704 				fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
10705 				fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
10706 				fnd_msg_pub.ADD;
10707 				RAISE fnd_api.g_exc_error;
10708 			END IF;
10709 		--Bug#5649056: only update if subinventory and locator match
10710 		ELSIF ( l_lpn_sub = l_mmtt_sub AND l_lpn_loc = l_mmtt_loc ) THEN
10711 		  inv_quantity_tree_pub.update_quantities(
10712 			p_api_version_number         => 1.0
10713 		  , p_init_msg_lst               => fnd_api.g_false
10714 		  , x_return_status              => l_return_status
10715 		  , x_msg_count                  => l_msg_cnt
10716 		  , x_msg_data                   => l_msg_data
10717 		  , p_organization_id            => p_org_id
10718 		  , p_inventory_item_id          => p_item_id
10719 		  , p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
10720 		  , p_is_revision_control        => b_is_revision_control
10721 		  , p_is_lot_control             => FALSE
10722 		  , p_is_serial_control          => b_is_serial_control
10723 		  , p_revision                   => NVL(p_rev, NULL)
10724 		  , p_lot_number                 => NULL
10725 		  , p_subinventory_code          => l_sub
10726 		  , p_locator_id                 => l_loc_id
10727 		  , p_primary_quantity           => l_mmtt_qty
10728 		  , p_secondary_quantity         => l_mmtt_sec_qty -- Bug #4141928
10729 		  , p_quantity_type              => inv_quantity_tree_pvt.g_qs_txn
10730 		  , x_qoh                        => l_qoh
10731 		  , x_rqoh                       => l_rqoh
10732 		  , x_qr                         => l_qr
10733 		  , x_qs                         => l_qs
10734 		  , x_att                        => l_att
10735 		  , x_atr                        => l_atr
10736 		  , x_sqoh                       => l_sqoh                 -- Bug #4141928
10737 		  , x_srqoh                      => l_srqoh                -- Bug #4141928
10738 		  , x_sqr                        => l_sqr                  -- Bug #4141928
10739 		  , x_sqs                        => l_sqs                  -- Bug #4141928
10740 		  , x_satt                       => l_satt                 -- Bug #4141928
10741 		  , x_satr                       => l_satr                 -- Bug #4141928
10742 		  --  , p_lpn_id                =>   p_fromlpn_id      withour lpn_id, only to locator level
10743 		  --, p_transfer_subinventory_code => l_xfr_sub_code   -- Bug #14753999
10744 		  );
10745 
10746 			IF (l_return_status = fnd_api.g_ret_sts_success) THEN
10747 				IF (l_debug = 1) THEN
10748 				mydebug('update qty tree back without lpn 4th time l_att :' || l_att);
10749 				mydebug('update qty tree back without lpn 4th time l_satt:' || l_satt);
10750 				END IF;
10751 			ELSE
10752 				IF (l_debug = 1) THEN
10753 				mydebug('calling update qty tree back without lpn 4th time failed ');
10754 				END IF;
10755 
10756 				fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
10757 				fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
10758 				fnd_msg_pub.ADD;
10759 				RAISE fnd_api.g_exc_error;
10760 			END IF;
10761 		END IF; --}
10762 
10763 		-- If the LPN quantity exactly matches/ has less than, the requested
10764 		-- quantity then match the serial numbers also
10765 
10766 			-- Bug #4141928. No changes required for OPM convergence.
10767 			-- Since this is a serial context
10768 			IF p_is_sn_alloc = 'Y' AND p_action = 4 THEN --{
10769 
10770 				SELECT COUNT(fm_serial_number)
10771 				INTO l_total_serial_cnt
10772 				FROM mtl_serial_numbers_temp msnt
10773 				WHERE msnt.transaction_temp_id = p_temp_id;
10774 
10775 					IF (l_debug = 1) THEN
10776 					mydebug('SN tot count' || l_total_serial_cnt);
10777 					END IF;
10778 
10779 				IF (x_match = 1  OR x_match = 3 ) THEN
10780 
10781 					IF (l_total_serial_cnt = l_serial_exist_cnt) THEN
10782 						IF (l_debug = 1) THEN
10783 						mydebug('LPN matches exactly.');
10784 						END IF;
10785 						x_match  := 1;
10786 
10787 					ELSIF(l_total_serial_cnt > l_serial_exist_cnt) THEN
10788 						IF (l_debug = 1) THEN
10789 						mydebug('LPN has less.');
10790 						END IF;
10791 
10792 						x_match    := 3;
10793 						l_lpn_pr_qty  := l_serial_exist_cnt;
10794 					ELSE
10795 						IF (l_debug = 1) THEN
10796 						mydebug('LPN has extra serials.');
10797 						END IF;
10798 						x_match  := 4;
10799 					END IF;
10800 				END IF;
10801 
10802 				IF (l_check_tolerance) THEN
10803 				IF l_total_serial_cnt > l_serial_exist_cnt THEN
10804 				   IF (l_debug = 1) THEN
10805 					   mydebug('There are serials which is not inside the lpn. set l_check_tolerance to false');
10806 				   END IF;
10807 				   l_check_tolerance := false;
10808 				END IF;
10809 				END IF;
10810 			END IF; --}
10811 
10812 
10813 			IF (l_debug = 1) THEN
10814 			mydebug('After 4');
10815 			END IF;
10816 
10817 	END IF; --} lot control check
10818 
10819 END IF; --} lpn has only one item
10820 
10821 	--/* Bug 9448490 Lot Substitution Project */ start
10822 	mydebug('lpn_match - lot_substitution_alloc_csr - p_transaction_action_id   ' ||p_transaction_action_id);
10823 	mydebug('lpn_match - lot_substitution_alloc_csr - l_is_lot_control          ' || l_is_lot_control);
10824 	mydebug('lpn_match - lot_substitution_alloc_csr - p_is_sn_alloc             ' || p_is_sn_alloc);
10825 	mydebug('lpn_match - lot_substitution_alloc_csr - p_changelotNoException    ' || p_changelotNoException);
10826 	mydebug('lpn_match - lot_substitution_csr - x_match - ' || x_match);--HWSNIssue 13860546
10827 	mydebug('lpn_match - lot_substitution_alloc_csr - p_full_lot_allocation     ' || p_full_lot_allocation); -- Added for 14699845 (Flexible Lot Allocation)
10828 
10829 	l_substitute_lot_exist := FALSE; --14397366
10830 	-- 11068325
10831 	IF (p_transaction_action_id = 28
10832 	OR (l_transaction_source_type_id =4 AND p_transaction_action_id =1)
10833 	OR (l_transaction_source_type_id =4 AND p_transaction_action_id =2)
10834 	OR (l_transaction_source_type_id =13 AND p_transaction_action_id =2 AND p_full_lot_allocation IN ('N', 'P'))--BackFlush Transfer Added for 14699845 (Flexible Lot Allocation)
10835 	OR (l_transaction_source_type_id = 5 AND p_transaction_action_id =1 AND p_full_lot_allocation IN ('N', 'P'))) --WIP Component Issue Added for 14699845 (Flexible Lot Allocation)
10836 	   AND l_is_lot_control = 'Y'
10837 	   AND p_is_sn_alloc ='N'
10838 	   AND (p_changelotNoException = 'Y' OR p_full_lot_allocation IN ('N', 'P')) -- Modified for 14699845 (Flexible Lot Allocation)
10839 	THEN
10840 	/* In Case of Flexible Lot Allocation, we would need to update the Qty Tree
10841 	   with Negative values before query tree since we are not passing the demand information 16267113 */
10842 
10843 --*******************************************************--
10844 -- Start for 16267113 Flexible Lot Allocation
10845 
10846 		IF (p_full_lot_allocation IN ('N', 'P')) THEN
10847 		  IF NVL(l_allocated_lpn_id, 0) = p_fromlpn_id THEN
10848 			--from lpn is the same as allocated_lpn, we need to update qty tree as negative qty
10849 			-- in order to get correct att.
10850 			inv_quantity_tree_pub.update_quantities(
10851 			  p_api_version_number         => 1.0
10852 			, p_init_msg_lst               => fnd_api.g_false
10853 			, x_return_status              => l_return_status
10854 			, x_msg_count                  => l_msg_cnt
10855 			, x_msg_data                   => l_msg_data
10856 			, p_organization_id            => p_org_id
10857 			, p_inventory_item_id          => p_item_id
10858 			, p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
10859 			, p_is_revision_control        => b_is_revision_control
10860 			, p_is_lot_control             => TRUE
10861 			, p_is_serial_control          => b_is_serial_control
10862 			, p_revision                   => NVL(p_rev, NULL)
10863 			, p_lot_number                 => NULL
10864 			, p_subinventory_code          => l_sub
10865 			, p_locator_id                 => l_loc_id
10866 			, p_primary_quantity           => -l_task_pri_qty
10867 			, p_secondary_quantity         => -l_task_sec_qty
10868 			, p_quantity_type              => inv_quantity_tree_pvt.g_qs_txn
10869 			, x_qoh                        => l_qoh
10870 			, x_rqoh                       => l_rqoh
10871 			, x_qr                         => l_qr
10872 			, x_qs                         => l_qs
10873 			, x_att                        => l_att
10874 			, x_atr                        => l_atr
10875 			, x_sqoh                       => l_sqoh
10876 			, x_srqoh                      => l_srqoh
10877 			, x_sqr                        => l_sqr
10878 			, x_sqs                        => l_sqs
10879 			, x_satt                       => l_satt
10880 			, x_satr                       => l_satr
10881 			, p_lpn_id                     => p_fromlpn_id
10882 			);
10883 
10884 			IF (l_return_status = fnd_api.g_ret_sts_success) THEN
10885 			  IF (l_debug = 1) THEN
10886 				mydebug('update qty tree for FlexiLotAlloc lpn l_att:'  || l_att  || ' at Locator Level');
10887 				mydebug('update qty tree for FlexiLotAlloc lpn l_satt:' || l_satt || ' at Locator Level');
10888 			  END IF;
10889 			ELSE
10890 			  IF (l_debug = 1) THEN
10891 				mydebug('calling update qty tree with lpn for FlexiLotAlloc failed ');
10892 			  END IF;
10893 
10894 			  fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
10895 			  fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
10896 			  fnd_msg_pub.ADD;
10897 			  RAISE fnd_api.g_exc_error;
10898 			END IF;
10899 
10900 		  ELSIF ( l_lpn_sub = l_mmtt_sub AND l_lpn_loc = l_mmtt_loc ) THEN
10901 			inv_quantity_tree_pub.update_quantities(
10902 			  p_api_version_number         => 1.0
10903 			, p_init_msg_lst               => fnd_api.g_false
10904 			, x_return_status              => l_return_status
10905 			, x_msg_count                  => l_msg_cnt
10906 			, x_msg_data                   => l_msg_data
10907 			, p_organization_id            => p_org_id
10908 			, p_inventory_item_id          => p_item_id
10909 			, p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
10910 			, p_is_revision_control        => b_is_revision_control
10911 			, p_is_lot_control             => TRUE
10912 			, p_is_serial_control          => b_is_serial_control
10913 			, p_revision                   => NVL(p_rev, NULL)
10914 			, p_lot_number                 => NULL
10915 			, p_subinventory_code          => l_sub
10916 			, p_locator_id                 => l_loc_id
10917 			, p_primary_quantity           => -l_task_pri_qty
10918 			, p_secondary_quantity         => -l_task_sec_qty -- Bug #4141928
10919 			, p_quantity_type              => inv_quantity_tree_pvt.g_qs_txn
10920 			, x_qoh                        => l_qoh
10921 			, x_rqoh                       => l_rqoh
10922 			, x_qr                         => l_qr
10923 			, x_qs                         => l_qs
10924 			, x_att                        => l_att
10925 			, x_atr                        => l_atr
10926 			, x_sqoh                       => l_sqoh
10927 			, x_srqoh                      => l_srqoh
10928 			, x_sqr                        => l_sqr
10929 			, x_sqs                        => l_sqs
10930 			, x_satt                       => l_satt
10931 			, x_satr                       => l_satr
10932 			);
10933 
10934 			IF (l_return_status = fnd_api.g_ret_sts_success) THEN
10935 			  IF (l_debug = 1) THEN
10936 				mydebug('after update without lpn for FlexiLotAlloc l_att:'  || l_att  || ' at Locator Level');
10937 				mydebug('after update without lpn for FlexiLotAlloc l_satt:' || l_satt || ' at Locator Level');
10938 			  END IF;
10939 			ELSE
10940 			  IF (l_debug = 1) THEN
10941 				mydebug('calling update qty tree for FlexiLotAlloc without lpn failed ');
10942 			  END IF;
10943 
10944 			  fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
10945 			  fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
10946 			  fnd_msg_pub.ADD;
10947 			  RAISE fnd_api.g_exc_error;
10948 			END IF;
10949 		  END IF;
10950 		END IF;
10951 -- End for 16267113 Flexible Lot Allocation
10952 --*******************************************************--
10953 
10954 
10955 		IF (l_debug = 1) THEN
10956 		mydebug('before opening lot_substitution_alloc_csr');
10957 		END IF;
10958 		OPEN lot_substitution_alloc_csr;
10959 		LOOP
10960 			FETCH lot_substitution_alloc_csr INTO l_mtlt_primary_qty, l_mtlt_lot_number , l_mtlt_secondary_qty;--16267113
10961 			EXIT WHEN lot_substitution_alloc_csr%NOTFOUND;
10962 
10963 			l_substitute_lot_exist := TRUE; --14397366
10964 			mydebug('Sub Lots exists' );--14397366
10965 			mydebug(' x_match : ' || x_match);-- Added for 14699845 (Flexible Lot Allocation)
10966 
10967 			IF (x_match = 1 OR x_match = 3) THEN
10968 			mydebug('lpn_match - lot_substitution_csr - x_match' || x_match);--HWSNIssue 13860546
10969 			x_match :=4 ;
10970 			END IF;
10971 
10972 			IF (l_debug = 1) THEN
10973 			  mydebug(' Unallocated l_mtlt_lot_number : ' || l_mtlt_lot_number);
10974 			  mydebug(' Unallocated l_mtlt_primary_qty: ' || l_mtlt_primary_qty);
10975 			  mydebug(' Unallocated l_mtlt_secondary_qty: ' || l_mtlt_secondary_qty);
10976 			END IF;
10977 
10978 
10979 			inv_quantity_tree_pub.query_quantities(
10980 			p_api_version_number         => 1.0
10981 		  , p_init_msg_lst               => fnd_api.g_false
10982 		  , x_return_status              => l_return_status
10983 		  , x_msg_count                  => l_msg_cnt
10984 		  , x_msg_data                   => l_msg_data
10985 		  , p_organization_id            => p_org_id
10986 		  , p_inventory_item_id          => p_item_id
10987 		  , p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
10988 		  , p_is_revision_control        => b_is_revision_control
10989 		  , p_is_lot_control             => TRUE
10990 		  , p_is_serial_control          => b_is_serial_control
10991 		  , p_demand_source_type_id      => -9999
10992 		  , p_revision                   => NVL(p_rev, NULL)
10993 		  , p_lot_number                 => l_mtlt_lot_number
10994 		  , p_subinventory_code          => l_sub
10995 		  , p_locator_id                 => l_loc_id
10996 		  , x_qoh                        => l_qoh
10997 		  , x_rqoh                       => l_rqoh
10998 		  , x_qr                         => l_qr
10999 		  , x_qs                         => l_qs
11000 		  , x_att                        => l_att
11001 		  , x_atr                        => l_atr
11002 		  , x_sqoh                       => l_sqoh         --14846817
11003 		  , x_srqoh                      => l_srqoh        --14846817
11004 		  , x_sqr                        => l_sqr          --14846817
11005 		  , x_sqs                        => l_sqs          --14846817
11006 		  , x_satt                       => l_satt         --14846817
11007 		  , x_satr                       => l_satr
11008 		  , p_lpn_id                     => p_fromlpn_id
11009 		  , p_transfer_subinventory_code => l_xfr_sub_code
11010 		  , p_grade_code                 => NULL           --14846817
11011 		  );
11012 
11013 		  IF (l_return_status = fnd_api.g_ret_sts_success) THEN --{
11014 				IF ((l_fulfillment_base = 'P' AND l_att > 0) OR (l_fulfillment_base = 'S' AND l_satt > 0)) THEN --{
11015 				l_table_index  := l_table_index + 1;
11016 				IF (l_debug = 1) THEN
11017 					mydebug('Unallocated  l_att:' || l_att || ' for lot:' || l_mtlt_lot_number);
11018 					mydebug('Unallocated  l_qoh:' || l_qoh || ' for lot:' || l_mtlt_lot_number);
11019 				END IF;
11020 
11021 				IF ((l_fulfillment_base = 'P' AND l_att < l_qoh) OR (l_fulfillment_base = 'S' AND l_satt < l_sqoh)) THEN
11022 					l_check_tolerance := FALSE;
11023 					IF (l_debug = 1) THEN
11024 					mydebug(' Unallocated Lots l_att < l_qoh: set l_check_tolerance to false');
11025 					END IF;
11026 				END IF;
11027 
11028 			  IF (l_debug = 1) THEN
11029 				  mydebug(' Unallocated l_table_index:' || l_table_index || ' lot_number:' || l_mtlt_lot_number || ' l_att: ' || l_att || ' l_satt: ' || l_satt);
11030 				  mydebug(' Unallocated l_table_index:' || l_table_index || ' lot_number:' || l_mtlt_lot_number || ' l_lpn_pr_qty: ' || l_lpn_pr_qty || ' l_lpn_sec_qty: ' || l_lpn_sec_qty);
11031 			  END IF;
11032 			  l_lpn_pr_qty                                      := l_lpn_pr_qty + l_att;
11033 			  l_lpn_sec_qty                                     := l_lpn_sec_qty + l_satt; --14846817
11034 			  t_lpn_lot_qty_table(l_table_index).lpn_id         := p_fromlpn_id;
11035 			  t_lpn_lot_qty_table(l_table_index).lot_number     := l_mtlt_lot_number;
11036 			  t_lpn_lot_qty_table(l_table_index).pri_qty        := l_att;
11037 			  t_lpn_lot_qty_table(l_table_index).sec_qty        := l_satt; --BUG14846817
11038 
11039 			  -- Added for 14699845 (Flexible Lot Allocation)
11040 			  IF (l_debug = 1) THEN
11041 				  mydebug(' lot_number:' || l_mtlt_lot_number || ' l_att: ' || l_att);
11042 				  mydebug(' lot_number:' || l_mtlt_lot_number || ' l_lpn_pr_qty: ' || l_lpn_pr_qty);
11043 				  mydebug(' lot_number:' || l_mtlt_lot_number || ' l_lpn_sec_qty: ' || l_lpn_sec_qty);
11044 			  END IF;
11045 			  -- Added for 14699845 (Flexible Lot Allocation)
11046 
11047 			  IF(l_fulfillment_base = 'S') THEN
11048 				IF (l_secondary_uom = p_trx_uom) THEN
11049 					 t_lpn_lot_qty_table(l_table_index).trx_qty := l_satt;
11050 				ELSE
11051 					 t_lpn_lot_qty_table(l_table_index).trx_qty := inv_convert.inv_um_convert(
11052 											   item_id        => p_item_id
11053 											  ,lot_number	  => l_mtlt_lot_number
11054 											  ,organization_id=> p_org_id
11055 											  ,precision      => null
11056 											  ,from_quantity  => l_satt
11057 											  ,from_unit      => l_secondary_uom
11058 											  ,to_unit        => p_trx_uom
11059 											  ,from_name      => null
11060 											  ,to_name        => null);
11061 				END IF;
11062 			  ELSE
11063 				IF (l_primary_uom = p_trx_uom) THEN
11064 					 t_lpn_lot_qty_table(l_table_index).trx_qty := l_att;
11065 				ELSE
11066 					 t_lpn_lot_qty_table(l_table_index).trx_qty := inv_convert.inv_um_convert(
11067 											   item_id        => p_item_id
11068 											  ,lot_number	  => l_mtlt_lot_number
11069 											  ,organization_id=> p_org_id
11070 											  ,precision      => null
11071 											  ,from_quantity  => l_att
11072 											  ,from_unit      => l_primary_uom
11073 											  ,to_unit        => p_trx_uom
11074 											  ,from_name      => null
11075 											  ,to_name        => null);
11076 				END IF;
11077 			  END IF;
11078 
11079 			ELSE
11080 			   IF (l_debug = 1) THEN
11081 				  mydebug('Unallocated - LPN does not have any available qty for lot ' || l_mtlt_lot_number);
11082 				  mydebug('Unallocated - set l_check_tolerance to false');
11083 			   END IF;
11084 			  l_check_tolerance := FALSE;
11085 			END IF; --}
11086 		END IF; --}
11087 
11088 	END LOOP;
11089 	CLOSE lot_substitution_alloc_csr;
11090 
11091 --***********************************************************************--
11092 -- Start for 16267113 Flexible Lot Allocation
11093 		IF (p_full_lot_allocation IN ('N', 'P')) THEN
11094 		  IF NVL(l_allocated_lpn_id, 0) = p_fromlpn_id THEN
11095 
11096 			inv_quantity_tree_pub.update_quantities(
11097 			  p_api_version_number         => 1.0
11098 			, p_init_msg_lst               => fnd_api.g_false
11099 			, x_return_status              => l_return_status
11100 			, x_msg_count                  => l_msg_cnt
11101 			, x_msg_data                   => l_msg_data
11102 			, p_organization_id            => p_org_id
11103 			, p_inventory_item_id          => p_item_id
11104 			, p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
11105 			, p_is_revision_control        => b_is_revision_control
11106 			, p_is_lot_control             => TRUE
11107 			, p_is_serial_control          => b_is_serial_control
11108 			, p_revision                   => NVL(p_rev, NULL)
11109 			, p_lot_number                 => NULL
11110 			, p_subinventory_code          => l_sub
11111 			, p_locator_id                 => l_loc_id
11112 			, p_primary_quantity           => l_task_pri_qty
11113 			, p_secondary_quantity         => l_task_sec_qty
11114 			, p_quantity_type              => inv_quantity_tree_pvt.g_qs_txn
11115 			, x_qoh                        => l_qoh
11116 			, x_rqoh                       => l_rqoh
11117 			, x_qr                         => l_qr
11118 			, x_qs                         => l_qs
11119 			, x_att                        => l_att
11120 			, x_atr                        => l_atr
11121 			, x_sqoh                       => l_sqoh
11122 			, x_srqoh                      => l_srqoh
11123 			, x_sqr                        => l_sqr
11124 			, x_sqs                        => l_sqs
11125 			, x_satt                       => l_satt
11126 			, x_satr                       => l_satr
11127 			, p_lpn_id                     => p_fromlpn_id
11128 			);
11129 
11130 			IF (l_return_status = fnd_api.g_ret_sts_success) THEN
11131 			  IF (l_debug = 1) THEN
11132 				mydebug('update qty tree back with +ve qty for FlexiLotAlloc lpn l_att:'  || l_att  || ' at Locator Level');
11133 				mydebug('update qty tree back with +ve qty for FlexiLotAlloc lpn l_satt:' || l_satt || ' at Locator Level');  -- Bug #4141928
11134 			  END IF;
11135 			ELSE
11136 			  IF (l_debug = 1) THEN
11137 				mydebug('calling update qty tree with lpn for FlexiLotAlloc failed ');
11138 			  END IF;
11139 
11140 			  fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
11141 			  fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
11142 			  fnd_msg_pub.ADD;
11143 			  RAISE fnd_api.g_exc_error;
11144 			END IF;
11145 		  ELSIF ( l_lpn_sub = l_mmtt_sub AND l_lpn_loc = l_mmtt_loc ) THEN
11146 			inv_quantity_tree_pub.update_quantities(
11147 			  p_api_version_number         => 1.0
11148 			, p_init_msg_lst               => fnd_api.g_false
11149 			, x_return_status              => l_return_status
11150 			, x_msg_count                  => l_msg_cnt
11151 			, x_msg_data                   => l_msg_data
11152 			, p_organization_id            => p_org_id
11153 			, p_inventory_item_id          => p_item_id
11154 			, p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
11155 			, p_is_revision_control        => b_is_revision_control
11156 			, p_is_lot_control             => TRUE
11157 			, p_is_serial_control          => b_is_serial_control
11158 			, p_revision                   => NVL(p_rev, NULL)
11159 			, p_lot_number                 => l_mtlt_lot_number
11160 			, p_subinventory_code          => l_sub
11161 			, p_locator_id                 => l_loc_id
11162 			, p_primary_quantity           => l_task_pri_qty
11163 			, p_secondary_quantity         => l_task_sec_qty
11164 			, p_quantity_type              => inv_quantity_tree_pvt.g_qs_txn
11165 			, x_qoh                        => l_qoh
11166 			, x_rqoh                       => l_rqoh
11167 			, x_qr                         => l_qr
11168 			, x_qs                         => l_qs
11169 			, x_att                        => l_att
11170 			, x_atr                        => l_atr
11171 			, x_sqoh                       => l_sqoh
11172 			, x_srqoh                      => l_srqoh
11173 			, x_sqr                        => l_sqr
11174 			, x_sqs                        => l_sqs
11175 			, x_satt                       => l_satt
11176 			, x_satr                       => l_satr
11177 			);
11178 
11179 			IF (l_return_status = fnd_api.g_ret_sts_success) THEN
11180 			  IF (l_debug = 1) THEN
11181 				mydebug('after update qty tree back for +ve qty without lpn for FlexiLotAlloc l_att:'  || l_att  || ' at Locator Level');
11182 				mydebug('after update qty tree back for +ve qty without lpn for FlexiLotAlloc l_satt:' || l_satt || ' at Locator Level');   -- Bug #4141928
11183 			  END IF;
11184 			ELSE
11185 			  IF (l_debug = 1) THEN
11186 				mydebug('calling update qty tree back for +ve qty without lpn for FlexiLotAlloc failed ');
11187 			  END IF;
11188 
11189 			  fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
11190 			  fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
11191 			  fnd_msg_pub.ADD;
11192 			  RAISE fnd_api.g_exc_error;
11193 			END IF;
11194 		  END IF;
11195 		END IF;
11196 
11197 -- End for 16267113 Flexible Lot Allocation
11198 --***********************************************************************--
11199 
11200 
11201 	--HWSNIssue 13860546
11202 	mydebug('l_item_cnt2   '||l_item_cnt2);
11203 	mydebug('p_is_sn_alloc '||p_is_sn_alloc);
11204 	mydebug('x_match       '||x_match);
11205 	mydebug('l_lot_code    '||l_lot_code);
11206 	mydebug('l_serial_code '||l_serial_code);
11207 
11208 	IF (l_item_cnt2 = 1 AND x_match = 4
11209 		AND ((l_lot_code> 1
11210 			 AND l_serial_code NOT IN (1,6)
11211 			 AND NVL(p_is_sn_alloc,'N') <>'Y')
11212 			  OR (l_lot_code>1)) AND l_substitute_lot_exist) THEN --14397366
11213 
11214 		BEGIN
11215 			SELECT NVL(SUM(primary_quantity),0), NVL(SUM(secondary_quantity),0)
11216 			INTO l_lpn_qoh, l_lpn_sqoh
11217 			FROM wms_lpn_contents
11218 			WHERE parent_lpn_id = p_fromlpn_id
11219 		   AND inventory_item_id = p_item_id;
11220 		EXCEPTION
11221 			WHEN OTHERS THEN
11222 			IF (l_debug = 1) THEN
11223 		   mydebug('Ideally we shouldnt have come here ');
11224 			END IF;
11225 		END;
11226 
11227 		IF (l_debug = 1) THEN
11228 		mydebug('l_lpn_pr_qty       : '||l_lpn_pr_qty);
11229 		mydebug('l_lpn_qoh          : '||l_lpn_qoh);
11230 		mydebug('l_lpn_sec_qty      : '||l_lpn_sec_qty);
11231 		mydebug('l_lpn_sqoh         : '||l_lpn_sqoh);
11232 		END IF;
11233 
11234 		IF (l_fulfillment_base = 'S') THEN
11235 				IF l_lpn_sec_qty = l_lpn_sqoh THEN
11236 				IF (l_debug = 1) THEN
11237 				mydebug('The LPN can be fully transacted for sec based fulfillment');
11238 				END IF;
11239 
11240 				IF (NVL((l_mmtt_sec_qty),0) > l_lpn_sec_qty) THEN
11241 					IF (l_debug = 1) THEN
11242 					mydebug('Total Task qty > Total LPN qty ');
11243 					END IF;
11244 					x_match := 3;
11245 				ELSIF (NVL((l_mmtt_sec_qty),0) = l_lpn_sec_qty) THEN
11246 					IF (l_debug = 1) THEN
11247 					mydebug('Total Task qty = Total LPN qty ');
11248 					END IF;
11249 					x_match := 1;
11250 				END IF;
11251 			END IF;
11252 		ELSE
11253 			IF l_lpn_pr_qty = l_lpn_qoh THEN
11254 				IF (l_debug = 1) THEN
11255 				mydebug('The LPN can be fully transacted for primary based fulfillment');
11256 				END IF;
11257 
11258 				IF (NVL((l_mmtt_qty),0) > l_lpn_pr_qty) THEN
11259 					IF (l_debug = 1) THEN
11260 					mydebug('Total Task qty > Total LPN qty ');
11261 					END IF;
11262 					x_match := 3;
11263 				ELSIF (NVL((l_mmtt_qty),0) = l_lpn_pr_qty) THEN
11264 					IF (l_debug = 1) THEN
11265 					mydebug('Total Task qty = Total LPN qty ');
11266 					END IF;
11267 					x_match := 1;
11268 				END IF;
11269 			END IF;
11270 		END IF;
11271 	END IF;
11272 
11273 	--HWSNIssue 13860546
11274 		IF(l_fulfillment_base = 'S') THEN
11275 			l_overpicked_qty := l_lpn_sec_qty - l_sec_qty;
11276 		ELSE
11277 			l_overpicked_qty := l_lpn_pr_qty - l_pr_qty;
11278 		END IF;
11279 	   IF l_overpicked_qty > 0  THEN
11280 				l_check_tolerance := TRUE ;
11281 			END IF;
11282 		   IF (l_debug = 1) THEN
11283 			 mydebug(' Unallocated :end of mutiple lots, l_check_tolerance is true and l_overpicked_qty: '||l_overpicked_qty);
11284 		   END IF;
11285 	  END IF;
11286 
11287 -- Lot Substitution
11288 	--/* Bug 9448490 Lot Substitution Project */ end
11289 
11290 	--check ship tolerance
11291 
11292 		IF inv_cache.set_item_rec( p_org_id , p_item_id) THEN
11293 			l_lot_divisible_flag := NVL(inv_cache.item_rec.lot_divisible_flag,'Y');
11294 		ELSE
11295 			IF (l_debug = 1) THEN
11296 			mydebug('item_rec not set in inv_cache');
11297 			END IF;
11298 			RAISE fnd_api.g_exc_unexpected_error;
11299 		END IF;
11300 
11301 		IF (l_debug = 1) THEN
11302 		mydebug('l_lot_divisible_flag: '||l_lot_divisible_flag);
11303 		END IF;
11304 
11305 		IF (p_pickOverNoException = 'Y' OR (p_full_lot_allocation IN ('N', 'P') AND l_lot_divisible_flag = 'N')) -- Modified for 14699845 (Flexible Lot Allocation)
11306 			AND l_check_tolerance
11307 			AND (p_transaction_action_id = 28 OR (l_transaction_source_type_id =4 AND p_transaction_action_id =1) OR (l_transaction_source_type_id =4 AND p_transaction_action_id =2))
11308 			AND x_match in (1, 4) --/* Bug 9448490 Lot Substitution Project */
11309 			THEN
11310 				IF (l_debug = 1) THEN
11311 					   mydebug('calling INV_Replenish_Detail_PUB.check_overpick');
11312 					   mydebug('p_transaction_temp_id :'||p_temp_id);
11313 					   mydebug('p_overpicked_qty      :'||l_overpicked_qty);
11314 					   mydebug('p_item_id             :'||p_item_id);
11315 					   mydebug('p_rev                 :'||p_rev);
11316 					   mydebug('p_lot_num     : NULL');
11317 					   mydebug('p_lot_exp_date: NULL');
11318 					   mydebug('p_sub                 :'||l_sub);
11319 					   mydebug('p_locator_id          :'||l_locator_id);
11320 					   mydebug('p_lpn_id              :'||p_fromlpn_id);
11321 
11322 				END IF;
11323 
11324 	 -- muom:sk
11325 	 IF l_fulfillment_base = 'S' THEN
11326 		 INV_Replenish_Detail_PUB.check_overpick(
11327 		   p_transaction_temp_id   => p_temp_id
11328 		 , p_overpicked_qty        => null
11329 		 , p_overpicked_qty2       => l_overpicked_qty
11330 		 , p_item_id               => p_item_id
11331 		 , p_rev                   => p_rev
11332 		 , p_lot_num               => NULL
11333 		 , p_lot_exp_date          => NULL
11334 		 , p_sub                   => l_sub
11335 		 , p_locator_id            => l_locator_id
11336 		 , p_lpn_id                => p_fromlpn_id
11337 		 , x_check_overpick_passed => l_check_overpick_passed   --OUT NOCOPY    VARCHAR
11338 		 , x_ovpk_error_code       => l_overpick_error_code     --OUT NOCOPY    NUMBER
11339 		 , x_return_status         => l_return_status
11340 		 , x_msg_count             => l_msg_cnt
11341 		 , x_msg_data              => l_msg_data
11342 		 );
11343 	 ELSE
11344 		 INV_Replenish_Detail_PUB.check_overpick(
11345 		   p_transaction_temp_id   => p_temp_id
11346 		 , p_overpicked_qty        => l_overpicked_qty
11347 		 , p_overpicked_qty2       => null
11348 		 , p_item_id               => p_item_id
11349 		 , p_rev                   => p_rev
11350 		 , p_lot_num               => NULL
11351 		 , p_lot_exp_date          => NULL
11352 		 , p_sub                   => l_sub
11353 		 , p_locator_id            => l_locator_id
11354 		 , p_lpn_id                => p_fromlpn_id
11355 		 , x_check_overpick_passed => l_check_overpick_passed   --OUT NOCOPY    VARCHAR
11356 		 , x_ovpk_error_code       => l_overpick_error_code     --OUT NOCOPY    NUMBER
11357 		 , x_return_status         => l_return_status
11358 		 , x_msg_count             => l_msg_cnt
11359 		 , x_msg_data              => l_msg_data
11360 		 );
11361 	 END IF;
11362 	  -- muom:sk
11363 
11364 		 IF x_return_status = fnd_api.g_ret_sts_error THEN
11365 			   RAISE fnd_api.g_exc_error;
11366 		 ELSIF x_return_status = fnd_api.g_ret_sts_unexp_error THEN
11367 			   RAISE fnd_api.g_exc_unexpected_error;
11368 		 END IF;
11369 
11370 		 IF l_check_overpick_passed = 'Y' THEN
11371 			 x_lpnpickedasis := 'Y';
11372 		 ELSE
11373 			 IF (l_debug = 1) THEN
11374 				 mydebug('over picking is not passed. the Error code is: ' || l_overpick_error_code);
11375 			 END IF;
11376 		 END IF;
11377 
11378 		END IF;
11379 
11380 		IF x_match = 1 OR x_match = 3 THEN
11381 			IF p_action = 4 THEN
11382 				-- serial controlled - CHECK serial status
11383 				IF (l_debug = 1) THEN
11384 				mydebug('x_match is ' || x_match || ' and item is serial controlled ');
11385 				END IF;
11386 
11387 		OPEN ser_csr;
11388 
11389 		LOOP
11390 		  FETCH ser_csr INTO l_serial_number;
11391 		  EXIT WHEN ser_csr%NOTFOUND;
11392 
11393 		  IF inv_material_status_grp.is_status_applicable(
11394 			   p_wms_installed              => p_wms_installed
11395 			 , p_trx_status_enabled         => NULL
11396 			 , p_trx_type_id                => p_transaction_type_id
11397 			 , p_lot_status_enabled         => NULL
11398 			 , p_serial_status_enabled      => NULL
11399 			 , p_organization_id            => p_org_id
11400 			 , p_inventory_item_id          => p_item_id
11401 			 , p_sub_code                   => x_sub
11402 			 , p_locator_id                 => NULL
11403 			 , p_lot_number                 => p_lot
11404 			 , p_serial_number              => l_serial_number
11405 			 , p_object_type                => 'A'
11406 			 ) = 'N' THEN
11407 			IF (l_debug = 1) THEN
11408 			  mydebug('After 6');
11409 			END IF;
11410 
11411 			x_match  := 11;
11412 			CLOSE ser_csr;
11413 			fnd_message.set_name('INV', 'INV_SER_STATUS_NA');
11414 			fnd_message.set_token('TOKEN', l_serial_number);
11415 			fnd_msg_pub.ADD;
11416 			RAISE fnd_api.g_exc_error;
11417 		  END IF;
11418 		END LOOP;
11419 
11420 		CLOSE ser_csr;
11421 	  ELSE
11422 		l_serial_number  := NULL;
11423 
11424 		-- Check whether the LPN status is applicable for this transaction
11425 		IF inv_material_status_grp.is_status_applicable(
11426 			 p_wms_installed              => p_wms_installed
11427 		   , p_trx_status_enabled         => NULL
11428 		   , p_trx_type_id                => p_transaction_type_id
11429 		   , p_lot_status_enabled         => NULL
11430 		   , p_serial_status_enabled      => NULL
11431 		   , p_organization_id            => p_org_id
11432 		   , p_inventory_item_id          => p_item_id
11433 		   , p_sub_code                   => x_sub
11434 		   , p_locator_id                 => NULL
11435 		   , p_lot_number                 => p_lot
11436 		   , p_serial_number              => l_serial_number
11437 		   , p_object_type                => 'A'
11438 		   ) = 'N' THEN
11439 		  x_match  := 8;
11440 		  -- LPN status is invalid for this operation
11441 
11442 		  fnd_message.set_name('INV', 'INV_INVALID_LPN_STATUS');
11443 		  fnd_message.set_token('TOKEN1', TO_CHAR(p_fromlpn_id));
11444 		  fnd_msg_pub.ADD;
11445 		  RAISE fnd_api.g_exc_error;
11446 		END IF;
11447 	  END IF;
11448 	END IF;
11449 
11450 	IF (l_debug = 1) THEN
11451 	  mydebug('x_match          : ' || x_match);
11452 	  mydebug('p_is_sn_alloc    : ' || p_is_sn_alloc);
11453 	  mydebug('p_action         : ' || p_action);
11454 	  mydebug('l_lpn_pr_qty (in primary uom): ' || l_lpn_pr_qty);
11455 	  mydebug('l_lpn_sec_qty    : ' || l_lpn_sec_qty);
11456 	END IF;
11457 
11458 	IF l_fulfillment_base = 'S' THEN            --We are not comparing trx uom with primary for fulfillment basis 'S' as deviation quantities might result in irrelvant primary.
11459 		IF (p_trx_uom <> l_secondary_uom) THEN  --for fulfillment based on secondary we care only about the transactable sec present in LPN no matter what primary is.
11460 			l_lpn_trx_qty := inv_convert.inv_um_convert(
11461 					 item_id        => p_item_id
11462 					,lot_number	  	=> l_mtlt_lot_number
11463 					,organization_id=> p_org_id
11464 					,precision      => null
11465 					,from_quantity  => l_lpn_sec_qty
11466 					,from_unit      => l_secondary_uom
11467 					,to_unit        => p_trx_uom
11468 					,from_name      => null
11469 					,to_name        => null);
11470 		ELSE
11471 			l_lpn_trx_qty := l_lpn_sec_qty;
11472 		END IF;
11473 	ELSE
11474 		-- Now l_lpn_pr_qty is in primary uom, need to convert l_lpn_trx_qty in transaction uom (p_trx_uom)
11475 		-- if they are different
11476 		IF (p_trx_uom <> l_primary_uom) THEN
11477 			IF(p_trx_uom = l_secondary_uom) THEN
11478 				l_lpn_trx_qty := l_lpn_sec_qty;
11479 			ELSE
11480 				l_lpn_trx_qty := inv_convert.inv_um_convert(
11481 					 item_id        => p_item_id
11482 					,lot_number	  	=> l_mtlt_lot_number
11483 					,organization_id=> p_org_id
11484 					,precision      => null
11485 					,from_quantity  => l_lpn_pr_qty
11486 					,from_unit      => l_primary_uom
11487 					,to_unit        => p_trx_uom
11488 					,from_name      => null
11489 					,to_name        => null);
11490 			END IF;
11491 			IF (l_debug = 1) THEN
11492 			  mydebug('l_lpn_trx_qty :' || l_lpn_trx_qty);
11493 			END IF;
11494 		ELSE
11495 			l_lpn_trx_qty :=  l_lpn_pr_qty;
11496 		END IF;
11497 	END IF;
11498 
11499 	-- populate the temp table to be used in lot and serial processing
11500 	-- ideally this should be done during above process for each case, need
11501 	-- revisit them later on
11502 
11503 	DELETE FROM wms_allocations_gtmp;
11504 
11505 	-- Bug #4141928. No changes required for OPM convergence.
11506 	-- Since this is a serial context
11507 	IF p_is_sn_alloc = 'Y'
11508 	   AND p_action = 4 THEN
11509 		IF (l_debug = 1) THEN
11510 		  mydebug('SN control and SN allocation on');
11511 		END IF;
11512 
11513 		IF l_lot_code > 1 AND p_full_lot_allocation IN ('Y', 'P') THEN -- Modified for 14699845 (Flexible Lot Allocation)
11514 
11515 			 INSERT INTO WMS_ALLOCATIONS_GTMP
11516 			 (lot_number,
11517 			  serial_number,
11518 			  transaction_quantity,
11519 			  primary_quantity)
11520 			  SELECT mtlt.lot_number,fm_serial_number,1,1
11521 			  FROM mtl_serial_numbers_temp msnt,
11522 				   mtl_transaction_lots_temp mtlt,
11523 				   mtl_serial_numbers msn
11524 			  WHERE mtlt.transaction_temp_id = p_temp_id
11525 				AND msnt.transaction_temp_id = mtlt.serial_transaction_temp_id
11526 				AND msnt.fm_serial_number = msn.serial_number
11527 				AND msn.lpn_id = p_fromlpn_id
11528 				AND msn.inventory_item_id = p_item_id;
11529 
11530 			 -- jxlu 10/12/04 start
11531 			 IF (x_lpnpickedasis= 'Y')  THEN
11532 				   x_trx_qty_alloc := l_lpn_trx_qty;
11533 				   IF (l_debug = 1) THEN
11534 						  mydebug('lot controlled.SN allocated and x_lpnpickedasis is Y. ');
11535 						  mydebug(' x_trx_qty_alloc:'||x_trx_qty_alloc);
11536 				   END IF;
11537 			 ELSE
11538 				   x_trx_qty_alloc := SQL%ROWCOUNT;
11539 				   IF (l_debug = 1) THEN
11540 						  mydebug('lot controlled.SN allocated and x_lpnpickedasis is N. ');
11541 						  mydebug(' x_trx_qty_alloc:'||x_trx_qty_alloc);
11542 				   END IF;
11543 			END IF;
11544 
11545 			 -- populate the lot vector
11546 			 l_table_index := 0;
11547 			 FOR lot_ATT_rec in lot_att LOOP
11548 					-- Modified for 14699845 (Flexible Lot Allocation) l_table_index := l_table_index + 1;
11549 					IF (l_debug = 1) THEN
11550 						   mydebug('lot_ATT_rec.lot_number: '||lot_ATT_rec.lot_number);
11551 						   mydebug('lot_ATT_rec.transaction_quantity: '||lot_ATT_rec.transaction_quantity);
11552 					END IF;
11553 					-- since trx_qty already populated before, we can only use
11554 					-- non_alloc_qty to temporarily hold the value. later when generating
11555 					-- string, then properly place them in the string passed back to java
11556 					IF(t_lpn_lot_qty_table.EXISTS(l_table_index + 1) AND t_lpn_lot_qty_table(l_table_index + 1).lot_number = lot_ATT_rec.lot_number) THEN -- Modified for 14699845 (Flexible Lot Allocation)
11557 					  l_table_index := l_table_index + 1; -- Modified for 14699845 (Flexible Lot Allocation)
11558 					t_lpn_lot_qty_table(l_table_index).non_alloc_qty := lot_ATT_rec.transaction_quantity;
11559 					END IF;
11560 
11561 			 END LOOP;
11562 
11563 			 IF (l_debug = 1) THEN
11564 				   mydebug('lot controlled. x_trx_qty_alloc:'||x_trx_qty_alloc);
11565 			 END IF;
11566 			 --jxlu 10/12/04 end
11567 		ELSIF l_lot_code <= 1 THEN -- Added for 14699845 (Flexible Lot Allocation -- not lot controlled
11568 			INSERT INTO WMS_ALLOCATIONS_GTMP
11569 			 (serial_number,
11570 			  transaction_quantity,
11571 			  primary_quantity)
11572 			  SELECT fm_serial_number,1,1
11573 			  FROM mtl_serial_numbers_temp msnt,
11574 				   mtl_serial_numbers msn
11575 			  WHERE  msnt.transaction_temp_id = p_temp_id
11576 				AND msnt.fm_serial_number = msn.serial_number
11577 				AND msn.lpn_id = p_fromlpn_id
11578 				AND msn.inventory_item_id = p_item_id;
11579 
11580 			--jxlu 10/12/04 start
11581 			IF (x_lpnpickedasis ='Y')  THEN
11582 				x_trx_qty_alloc := l_lpn_trx_qty;
11583 				IF (l_debug = 1) THEN
11584 					  mydebug('NOT lot controlled.SN allocated and x_lpnpickedasis is Y. ');
11585 					  mydebug(' x_trx_qty_alloc:'||x_trx_qty_alloc);
11586 				END IF;
11587 			ELSE
11588 				x_trx_qty_alloc := SQL%ROWCOUNT;
11589 				IF (l_debug = 1) THEN
11590 					  mydebug('NOT lot controlled.SN allocated and x_lpnpickedasis is N. ');
11591 					  mydebug(' x_trx_qty_alloc:'||x_trx_qty_alloc);
11592 				END IF;
11593 			END IF;
11594 			-- jxlu 10/12/04 end
11595 	   END IF;
11596 	ELSIF p_is_sn_alloc = 'N' AND p_action = 4 AND x_match = 3 THEN
11597 
11598 		  IF (l_debug = 1) THEN
11599 			 mydebug('SN control and SN allocation off and x_match=3');
11600 		  END IF;
11601 
11602 		  IF l_lot_code > 1 AND p_full_lot_allocation IN ('Y', 'P') THEN -- Modified for 14699845 (Flexible Lot Allocation)
11603 				INSERT INTO wms_allocations_gtmp
11604 				(lot_number,
11605 				 serial_number,
11606 				 transaction_quantity,
11607 				 primary_quantity)
11608 				SELECT mtlt.lot_number, serial_number, 1, 1
11609 				FROM mtl_transaction_lots_temp mtlt,
11610 					 mtl_serial_numbers msn
11611 				WHERE mtlt.transaction_temp_id = p_temp_id
11612 				  AND msn.lpn_id = p_fromlpn_id
11613 				  AND mtlt.lot_number = msn.lot_number
11614 				  AND msn.inventory_item_id = p_item_id
11615 				  AND Nvl(msn.group_mark_id, -1) = -1;
11616 		  ELSIF l_lot_code <= 1 THEN -- Modified for 14699845 (Flexible Lot Allocation) -- not lot controlled
11617 			   INSERT INTO wms_allocations_gtmp
11618 				(serial_number,
11619 				 transaction_quantity,
11620 				 primary_quantity)
11621 				SELECT serial_number,1,1
11622 				FROM mtl_serial_numbers msn
11623 				WHERE msn.lpn_id = p_fromlpn_id
11624 				AND msn.inventory_item_id = p_item_id
11625 				AND Nvl(msn.group_mark_id, -1) = -1;
11626 		  END IF;
11627 	ELSIF l_lot_code > 1 THEN -- lot controlled
11628 
11629 	   IF (l_debug = 1) THEN
11630 			 mydebug('lot controlled....');
11631 	   END IF;
11632 
11633 	   l_table_total      := t_lpn_lot_qty_table.COUNT;
11634 	   IF l_table_total > 0 THEN
11635 		 IF (l_debug = 1) THEN
11636 		   mydebug('building lpn lot vector for ' || l_table_total || '
11637 				   records');
11638 		 END IF;
11639 
11640 		FOR l_table_count IN 1 .. l_table_total LOOP
11641 			IF (l_debug = 1) THEN
11642 			  mydebug('index is : ' || l_table_count);
11643 			END IF;
11644 
11645 			INSERT INTO wms_allocations_gtmp(lot_number, primary_quantity,
11646 											 transaction_quantity, secondary_quantity)  -- Bug #4141928
11647 				   values(t_lpn_lot_qty_table(l_table_count).lot_number,
11648 						  t_lpn_lot_qty_table(l_table_count).pri_qty,
11649 						  t_lpn_lot_qty_table(l_table_count).trx_qty,
11650 		  t_lpn_lot_qty_table(l_table_count).sec_qty
11651 		  );                     -- Bug #4141928
11652 
11653 		  END LOOP;
11654 	   END IF;
11655 
11656 	END IF; -- done populating the lot
11657 
11658 
11659 	--populate the lot in lpn vector
11660 
11661 	l_table_total      := t_lpn_lot_qty_table.COUNT;
11662 
11663 	IF l_table_total > 0 THEN
11664 		  IF (l_debug = 1) THEN
11665 		   mydebug('building lpn lot vector for ' || l_table_total || 'records');
11666 		  END IF;
11667 
11668 
11669 		  FOR l_table_count IN 1 .. l_table_total LOOP
11670 				IF (l_debug = 1) THEN
11671 					  mydebug('index is : ' || l_table_count);
11672 				END IF;
11673 				IF p_is_sn_alloc = 'Y'  THEN  -- serial allocated
11674 					IF (l_debug = 1) THEN
11675 						  mydebug('serial is allocated');
11676 					END IF;
11677 					IF (x_lpnpickedasis ='Y') THEN
11678 					   IF (l_debug = 1) THEN
11679 						   mydebug('x_lpnpickedasis is Y');
11680 					   END IF;
11681 						 IF l_value = '.,' THEN   --bug 6651517 added if on the basis of l_value
11682 
11683 					   x_lpn_lot_vector := x_lpn_lot_vector
11684 							 ||t_lpn_lot_qty_table(l_table_count).lot_number ||'@@@@@'
11685 							 ||t_lpn_lot_qty_table(l_table_count).trx_qty||'@@@@@'
11686 							 ||t_lpn_lot_qty_table(l_table_count).trx_qty
11687 							 || '&&&&&'
11688 							 || t_lpn_lot_qty_table(l_table_count).sec_qty   -- Bug #4141928
11689 							 || '#####';                                     -- Bug #4141928
11690 						ELSE
11691 							  x_lpn_lot_vector := x_lpn_lot_vector
11692 							 ||t_lpn_lot_qty_table(l_table_count).lot_number ||'@@@@@'
11693 							 ||TO_CHAR(t_lpn_lot_qty_table(l_table_count).trx_qty,'9999999999999999999999.9999999999')||'@@@@@'
11694 							 ||TO_CHAR(t_lpn_lot_qty_table(l_table_count).trx_qty,'9999999999999999999999.9999999999')
11695 							 || '&&&&&'
11696 							 || TO_CHAR(t_lpn_lot_qty_table(l_table_count).sec_qty,'9999999999999999999999.9999999999')   -- Bug #4141928
11697 							 || '#####';
11698 						 END IF;
11699 						 -- end of bug 6651517
11700 						l_lot_v   := l_lot_v
11701 								||t_lpn_lot_qty_table(l_table_count).lot_number||':'; --Bug 3855835
11702 					ELSE
11703 					  IF (l_debug = 1) THEN
11704 						   mydebug('x_lpnpickedasis is N');
11705 					  END IF;
11706 						IF l_value = '.,' THEN   --bug 6651517 added if on the basis of l_value
11707 						   x_lpn_lot_vector := x_lpn_lot_vector
11708 							 ||t_lpn_lot_qty_table(l_table_count).lot_number ||'@@@@@'
11709 							 ||t_lpn_lot_qty_table(l_table_count).non_alloc_qty||'@@@@@'
11710 							 ||t_lpn_lot_qty_table(l_table_count).trx_qty
11711 							 || '&&&&&'
11712 							 || t_lpn_lot_qty_table(l_table_count).sec_qty   -- Bug #4141928
11713 							 || '#####';                                     -- Bug #4141928
11714 						  else
11715 							x_lpn_lot_vector := x_lpn_lot_vector
11716 							 ||t_lpn_lot_qty_table(l_table_count).lot_number ||'@@@@@'
11717 							 ||TO_CHAR(t_lpn_lot_qty_table(l_table_count).non_alloc_qty,'9999999999999999999999.9999999999')||'@@@@@'
11718 							 ||TO_CHAR(t_lpn_lot_qty_table(l_table_count).trx_qty,'9999999999999999999999.9999999999')
11719 							 || '&&&&&'
11720 						   || TO_CHAR(t_lpn_lot_qty_table(l_table_count).sec_qty,'9999999999999999999999.9999999999')   -- Bug #4141928
11721 							 || '#####';                                     -- Bug #4141928
11722 
11723 						   END IF;
11724 							--end of bug 6651517
11725 						l_lot_v   := l_lot_v
11726 								||t_lpn_lot_qty_table(l_table_count).lot_number||':'; --Bug 3855835
11727 					END IF;
11728 					IF (l_debug = 1) THEN
11729 						  mydebug('x_lpn_lot_vector:'||x_lpn_lot_vector);
11730 					END IF;
11731 				ELSE  -- serial is not allocated
11732 					IF (l_debug = 1) THEN
11733 						mydebug('serial is NOT allocated');
11734 					END IF;
11735 					--bug 6651517 added if on the basis of l_value
11736 				   IF l_value = '.,'    THEN
11737 					x_lpn_lot_vector := x_lpn_lot_vector
11738 							 ||t_lpn_lot_qty_table(l_table_count).lot_number ||'@@@@@'
11739 							 ||t_lpn_lot_qty_table(l_table_count).trx_qty||'@@@@@'
11740 							 ||t_lpn_lot_qty_table(l_table_count).trx_qty
11741 							 || '&&&&&'
11742 							 || t_lpn_lot_qty_table(l_table_count).sec_qty   -- Bug #4141928
11743 							 || '#####';                                     -- Bug #4141928
11744 				   ELSE
11745 					 x_lpn_lot_vector := x_lpn_lot_vector
11746 							 ||t_lpn_lot_qty_table(l_table_count).lot_number ||'@@@@@'
11747 							 ||TO_CHAR(t_lpn_lot_qty_table(l_table_count).trx_qty,'9999999999999999999999.9999999999')||'@@@@@'
11748 							 ||TO_CHAR(t_lpn_lot_qty_table(l_table_count).trx_qty,'9999999999999999999999.9999999999')
11749 							 || '&&&&&'
11750 							 || TO_CHAR(t_lpn_lot_qty_table(l_table_count).sec_qty,'9999999999999999999999.9999999999')   -- Bug #4141928
11751 							 || '#####';                                     -- Bug #4141928
11752 					END IF; --end of bug 6651517
11753 					  l_lot_v   := l_lot_v
11754 								||t_lpn_lot_qty_table(l_table_count).lot_number||':'; --Bug 3855835
11755 					 IF (l_debug = 1) THEN
11756 							mydebug('l_lot_v:'||l_lot_v);
11757 					 END IF;
11758 				END IF;
11759 		  END LOOP;
11760 	ELSE
11761 		  IF (l_debug = 1) THEN
11762 			   mydebug('it is not lot controlled and lot in lpn vector is null... ' );
11763 		  END IF;
11764 		   x_lpn_lot_vector  := NULL;
11765 	END IF;
11766 
11767 	--Changes starting here for Bug 14003388
11768 		BEGIN
11769 		IF(LENGTH(x_lpn_lot_vector) > 4000) THEN
11770 		x_lpn_lot_vector2 := SUBSTR(x_lpn_lot_vector, 4001, 8000);
11771 		x_lpn_lot_vector3 := SUBSTR(x_lpn_lot_vector, 8001, 12000);
11772 		x_lpn_lot_vector4 := SUBSTR(x_lpn_lot_vector, 12001);
11773 		x_lpn_lot_vector := SUBSTR(x_lpn_lot_vector, 1, 4000);
11774 			IF (l_debug = 1) THEN
11775 				mydebug('x_lpn_lot_vector  : '||x_lpn_lot_vector);
11776 				mydebug('x_lpn_lot_vector2 : '||x_lpn_lot_vector2);
11777 				mydebug('x_lpn_lot_vector3 : '||x_lpn_lot_vector3);
11778 				mydebug('x_lpn_lot_vector4 ::'||x_lpn_lot_vector4);
11779 			END IF;
11780 		ELSE
11781 			IF (l_debug = 1) THEN
11782 			mydebug('x_lot_att_vector string length less that 4000 chars and needs no separating Length is: '||LENGTH(l_lot_v));
11783 			END IF;
11784 		END IF;
11785 		EXCEPTION
11786 			WHEN OTHERS THEN
11787 			IF (l_debug = 1) THEN
11788 			mydebug('x_lot_att_vector  string trim had issues Length is : '||LENGTH(l_lot_v));
11789 			END IF;
11790 		END;
11791 	--Changes ending here for Bug 14003388
11792 
11793 
11794 	IF (l_debug = 1) THEN
11795 	  mydebug('LPN QTY in primary uom       :' || l_lpn_pr_qty);
11796 	  mydebug('LPN QTY in transaction uom   :' || l_lpn_trx_qty);
11797 	  mydebug('LPN Secondary QTY            :' || l_lpn_sec_qty);
11798 	  mydebug('x_temp_id                    :' || l_out_temp_id);
11799 	  mydebug('lot number value             :' || l_mtlt_lot_number);
11800 	END IF;
11801 
11802 	x_temp_id          := l_out_temp_id;
11803 
11804 	x_trx_qty          := l_lpn_trx_qty;
11805 	x_trx_sec_qty      := l_lpn_sec_qty;   -- Bug #4141928
11806    -- bug 3983704
11807 	-- get the packed qoh since the qty tree always return for both loosepack
11808 	SELECT NVL(SUM(primary_transaction_quantity),0), NVL(SUM(secondary_transaction_quantity),0)
11809 	INTO l_qoh, l_sqoh
11810 	FROM mtl_onhand_quantities_detail
11811 	WHERE lpn_id = p_fromlpn_id
11812 	AND organization_id = p_org_id;
11813 
11814 	IF l_fulfillment_base = 'S' THEN
11815 		IF(p_trx_uom = l_secondary_uom) THEN
11816 			x_lpn_qoh := l_sqoh;
11817 		ELSE
11818 			x_lpn_qoh := inv_convert.inv_um_convert(
11819 					 item_id        => p_item_id
11820 					,precision      => null
11821 					,from_quantity  => l_sqoh
11822 					,from_unit      => l_secondary_uom
11823 					,to_unit        => p_trx_uom
11824 					,from_name      => null
11825 					,to_name        => null);
11826 		END IF;
11827 	ELSE
11828 		IF (p_trx_uom <> l_primary_uom) THEN
11829 			IF(p_trx_uom = l_secondary_uom) THEN
11830 				--x_lpn_qoh := l_lpn_sec_qty;--Bug 14127418 --commented this code as l_lpn_sec_qty is the Sec ATT
11831 				--and we need to assign the sec on hand which is l_sqoh returned from above query
11832 				x_lpn_qoh := l_sqoh;
11833 			ELSE
11834 				x_lpn_qoh := inv_convert.inv_um_convert(
11835 					 item_id        => p_item_id
11836 					,precision      => null
11837 					,from_quantity  => l_qoh
11838 					,from_unit      => l_primary_uom
11839 					,to_unit        => p_trx_uom
11840 					,from_name      => null
11841 					,to_name        => null);
11842 			END IF;
11843 		ELSE
11844 			x_lpn_qoh := l_qoh;
11845 		END IF;
11846 	END IF;
11847 
11848 	x_return_status    := fnd_api.g_ret_sts_success;
11849 
11850 	IF (l_debug = 1) THEN
11851 		mydebug('x_match value at end of lpn_match :' || x_match);
11852 		mydebug('x_lpn_qoh :' ||  x_lpn_qoh);
11853 	END IF;
11854 
11855   /*  Mrana: 12/10/03: we need not change the lpn_context here . We will do it in
11856  *  LOAD/split merge API . to avoid hassles of setting and resetting in case
11857  *  users picks one lpn and then using cursor key goes up to change it .
11858  *  Also, with this approach , we do not need to reset lpn context in case of F2
11859  *  We will select this LPN for Update , so that otehr processes cannot get it.
11860  *  yes, there is a possibility that
11861  */
11862 	IF (x_match = 3) Or (x_match = 1) THEN   -- added x_match=1
11863 	   IF (l_debug = 1) THEN
11864 		 mydebug('Lock lpn_ID : ' || p_fromlpn_id);
11865 	   END IF;
11866 	  BEGIN
11867 
11868 		 SELECT lpn_context
11869 		   INTO l_lpn_context
11870 		   FROM wms_license_plate_numbers
11871 		  WHERE lpn_id = p_fromlpn_id
11872 			FOR UPDATE NOWAIT;
11873 	  EXCEPTION
11874 		 WHEN OTHERS THEN
11875 			IF SQLCODE  = -54 THEN  -- ORA-0054: resource busy and acquire with NOWAIT specified
11876 			   mydebug('LPN record is locked by another user... cannot pick this LPN' );
11877 			   fnd_message.set_name('WMS', 'WMS_LPN_LOCKED_ERROR');
11878 								  -- LPN is in use  by another user
11879 			   fnd_msg_pub.ADD;
11880 			   RAISE fnd_api.g_exc_error;
11881 			ELSE
11882 			   RAISE fnd_api.g_exc_unexpected_error;
11883 			END IF;
11884 
11885 	   END ;
11886 	END IF;
11887 
11888 	IF l_lot_v IS NOT NULL THEN
11889 	 l_lot_v := SUBSTR(l_lot_v,1,LENGTH(l_lot_v)-1);
11890 	END IF;
11891 
11892 	IF p_toLPN_Default IS NOT NULL  THEN
11893 
11894 	validate_pick_to_lpn
11895 	( p_api_version_number          =>   1.0
11896 	, p_init_msg_lst                =>   NULL
11897 	, x_return_status               =>   x_return_status
11898 	, x_msg_count                   =>   l_msg_cnt
11899 	, x_msg_data                    =>   l_msg_data
11900 	, p_organization_id             =>   p_org_id
11901 	, p_pick_to_lpn                 =>   p_toLPN_Default
11902 	, p_temp_id                     =>   p_temp_id
11903 	, p_project_id                  =>   p_project_id
11904 	, p_task_id                     =>   p_task_id
11905 	, p_container_item              =>   NULL
11906 	, p_container_item_id           =>   NULL
11907 	, p_suggested_container_item    =>   NULL
11908 	, p_suggested_container_item_id =>   NULL
11909 	, p_suggested_carton_name       =>   NULL
11910 	, p_suggested_tolpn_id          =>   NULL
11911 	, x_pick_to_lpn_id              =>   l_pick_to_lpn_id
11912 	, p_inventory_item_id           =>   p_item_id
11913 	, p_confirmed_sub               =>   p_confirmed_sub
11914 	, p_confirmed_loc_id            =>   p_confirmed_loc_id
11915 	, p_revision                    =>   p_rev
11916 	, p_confirmed_lots              =>   l_lot_v
11917 	, p_from_lpn_id                 =>   p_from_lpn_id
11918 	, p_lot_control                 =>   l_is_lot_control
11919 	, p_revision_control            =>   l_is_revision_control
11920 	, p_serial_control              =>   l_is_serial_control
11921 	-- Bug 4632519
11922 	, p_trx_type_id                 =>   to_char(p_transaction_type_id)
11923 	, p_trx_action_id               =>   to_char(p_transaction_action_id)
11924 	-- Bug 4632519
11925    );
11926 
11927 
11928    IF x_return_status <> fnd_api.g_ret_sts_success THEN
11929 	   x_toLPN_status := 'F';
11930 	 IF (l_debug = 1) THEN
11931 	  mydebug('Validate_pick_to_lpn could not validate toLPNDefault:');
11932 	 END IF;
11933 	 x_return_status := fnd_api.g_ret_sts_success;
11934    ELSE
11935 	x_toLPN_status := 'T';
11936 	IF (l_debug = 1) THEN
11937 	  mydebug('Validate_pick_to_lpn  validated  toLPNDefault:');
11938 	 END IF;
11939   END IF;
11940  END IF;
11941 
11942 --Added for bug 8205743 start
11943 
11944 IF (l_debug = 1) THEN
11945 	mydebug('lpn_match  l_lpn_pr_qty    '|| l_lpn_pr_qty);
11946 	mydebug('lpn_match  x_lpn_qoh       '|| x_lpn_qoh);
11947 	mydebug('lpn_match  x_match         '|| x_match);
11948 	mydebug('lpn_match  x_lpnpickedasis '|| x_lpnpickedasis);
11949 	mydebug('lpn_match  l_lpn_trx_qty   '|| l_lpn_trx_qty);
11950 	mydebug('lpn_match  l_lpn_sec_qty   '|| l_lpn_sec_qty);
11951 END IF;
11952 
11953 IF ((x_match = 1 OR x_match=3 OR x_match=4) AND
11954 	(x_lpn_qoh <> l_lpn_trx_qty)) THEN--14121089
11955 	x_match:=4 ;
11956 	x_lpnpickedasis :='N';  --10129781
11957 END IF;
11958 
11959 --Added for bug 8205743 end
11960 
11961   EXCEPTION
11962 	WHEN fnd_api.g_exc_error THEN
11963 	  IF (l_debug = 1) THEN
11964 		mydebug(' Expected Exception raised');
11965 	  END IF;
11966 	  x_return_status  := fnd_api.g_ret_sts_error;
11967 	  fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
11968 	WHEN fnd_api.g_exc_unexpected_error THEN
11969 	  IF (l_debug = 1) THEN
11970 		  mydebug(' Unexpected Exception raised');
11971 	  END IF;
11972 	  x_return_status  := fnd_api.g_ret_sts_unexp_error;
11973 	  fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
11974 	WHEN OTHERS THEN
11975 	  IF (l_debug = 1) THEN
11976 		mydebug('Other exception raised : ' || SQLERRM);
11977 	  END IF;
11978 
11979 	  x_return_status  := fnd_api.g_ret_sts_unexp_error;
11980 	  fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
11981 END lpn_match;
11982 
11983 
11984 --  during the picking process. If the user does not specifies
11985   -- a from lpn, this procedure will figure out if the loose quantity  will
11986   -- satisfy the pick in question, the temp table mtl_allocations_gtmp
11987  -- will store the available lot and serial numbers for this pick
11988 
11989 PROCEDURE loose_match(
11990 		p_org_id              IN            NUMBER
11991 	  , p_item_id             IN            NUMBER
11992 	  , p_rev                 IN            VARCHAR2
11993 	  , p_trx_qty             IN            NUMBER
11994 	  , p_trx_uom             IN            VARCHAR2
11995 	  , p_pri_uom             IN            VARCHAR2
11996 	  , p_sec_uom             IN            VARCHAR2    -- Bug #4141928
11997 	  , p_sec_qty             IN            NUMBER      -- Bug #4141928
11998 	  , p_temp_id             IN            NUMBER
11999 	  , p_suggested_locator   IN            NUMBER
12000 	  , p_confirmed_locator   IN            NUMBER
12001 	  , p_confirmed_sub       IN            VARCHAR2
12002 	  , p_is_sn_alloc         IN            VARCHAR2
12003 	  , p_is_revision_control IN            VARCHAR2
12004 	  , p_is_lot_control      IN            VARCHAR2
12005 	  , p_is_serial_control   IN            VARCHAR2
12006 	  , p_is_negbal_allowed   IN            VARCHAR2
12007 	  , p_toLPN_Default       IN            VARCHAR2    --Bug 3855835
12008 	  , p_project_id          IN            NUMBER
12009 	  , p_task_id             IN            NUMBER
12010 	  , x_trx_qty             OUT NOCOPY    NUMBER
12011 	  , x_trx_sec_qty         OUT NOCOPY    NUMBER      -- Bug #4141928
12012 	  , x_return_status       OUT NOCOPY    VARCHAR2
12013 	  , x_msg_count           OUT NOCOPY    NUMBER
12014 	  , x_msg_data            OUT NOCOPY    VARCHAR2
12015 	  , x_toLPN_status        OUT NOCOPY    VARCHAR2    --Bug 3855835
12016 	  , x_lot_att_vector      OUT NOCOPY    VARCHAR2
12017 	  , x_lot_att_vector2     OUT NOCOPY    VARCHAR2
12018 	  , x_lot_att_vector3     OUT NOCOPY    VARCHAR2
12019 	  , x_lot_att_vector4     OUT NOCOPY    VARCHAR2
12020 	  , x_trx_qty_alloc       OUT NOCOPY    NUMBER  -- jxlu 10/6/04
12021 	  , p_transaction_type_id IN            NUMBER  -- Bug 4632519
12022 	  , p_transaction_action_id IN          NUMBER  -- Bug 4632519
12023 	  , p_changelotNoException  IN          VARCHAR2 --/* Bug 9448490 Lot Substitution Project */
12024 	  , p_full_lot_allocation   IN          VARCHAR2 DEFAULT 'Y' -- Added for 14699845 (Flexible Lot Allocation)
12025 	  , p_fulfillment_base      IN          VARCHAR2 DEFAULT 'P' -- 16070349
12026   )IS
12027 	l_proc_name               VARCHAR2(30) := 'LOOSE_MATCH' ;
12028 	l_msg_cnt                 NUMBER;
12029 	l_msg_data                VARCHAR2(2000);
12030 	l_return_status           VARCHAR2(1);
12031 	l_lot_primary_qty         NUMBER;
12032 	l_att_trx_qty             NUMBER;
12033 	l_trx_lot_qty             NUMBER;
12034 	l_qoh                     NUMBER;
12035 	l_trx_qoh                 NUMBER;
12036 	l_att                     NUMBER;
12037 
12038 	l_lot_sec_qty             NUMBER;   -- Bug #4141928
12039 	l_att_trx_sec_qty         NUMBER;   -- Bug #4141928
12040 	l_trx_lot_sec_qty         NUMBER;   -- Bug #4141928
12041 	l_sqoh                    NUMBER;   -- Bug #4141928
12042 	l_trx_sec_qoh             NUMBER;   -- Bug #4141928
12043 	l_satt                    NUMBER;   -- Bug #4141928
12044 
12045 -- Increased lot size to 80 Char - Mercy Thomas - B4625329
12046 	l_lot_number              VARCHAR2(80) := NULL;
12047 	l_debug                   NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
12048 	l_table_index             NUMBER := 0;
12049 	l_pick_to_lpn_id          NUMBER;
12050 	l_lot_v                   VARCHAR2(32767) :=NULL ;-- 14003388 changing 2000 to 32767
12051 	l_is_lot_control          VARCHAR2(1);
12052 	l_is_revision_control     VARCHAR2(1);
12053 	l_is_serial_control       VARCHAR2(1);
12054 
12055 	l_transfer_subinventory   VARCHAR2(10) := NULL; -- Bug #7257709
12056 	l_dual_uom_item           BOOLEAN := FALSE;     --MUOM
12057 	l_fulfillment_base        VARCHAR2(1) := p_fulfillment_base; --MUOM
12058 
12059 	CURSOR lot_csr IS
12060 	  SELECT mtlt.primary_quantity
12061 		   , mtlt.transaction_quantity
12062 		   , NVL(mtlt.secondary_quantity, 0)    -- Bug #4141928
12063 		   , mtlt.lot_number
12064 		FROM mtl_transaction_lots_temp mtlt
12065 	   WHERE mtlt.transaction_temp_id = p_temp_id
12066 	   --added material status check for lot under bug8398578
12067 	   AND inv_material_status_grp.is_status_applicable(
12068 										NULL
12069 								,       NULL
12070 								,       p_transaction_type_id
12071 								,       NULL
12072 								,       NULL
12073 								,       p_org_id
12074 								,       p_item_id
12075 								,       NULL
12076 								,       null
12077 								,       mtlt.lot_number
12078 								,       NULL
12079 								,       'O') = 'Y'
12080 	ORDER BY LOT_NUMBER;
12081 
12082 	CURSOR lot_att IS
12083 	  SELECT lot_number, sum(transaction_quantity) transaction_quantity
12084 	  from wms_ALLOCATIONS_GTMP
12085 	  GROUP BY LOT_NUMBER
12086 	  ORDER BY LOT_NUMBER;
12087 
12088 	CURSOR mmtt_csr IS
12089 	  SELECT transfer_subinventory
12090 	  FROM mtl_material_transactions_temp
12091 	  WHERE transaction_temp_id = p_temp_id; -- Bug #7257709
12092 
12093 	  --/* Bug 9448490 Lot Substitution Project */ start
12094 	  CURSOR lot_substitution_att IS
12095 	   SELECT NVL(SUM(primary_transaction_quantity),0)
12096 		  , NVL(SUM(transaction_quantity), 0)
12097 		  , lot_number
12098 			 FROM mtl_onhand_quantities_detail
12099 			 WHERE organization_id = p_org_id
12100 		 AND Nvl(containerized_flag, 2) = 2 -- different from lpn_match
12101 			 AND subinventory_code = p_confirmed_sub
12102 			 AND locator_id = p_confirmed_locator
12103 			 AND inventory_item_id = p_item_id
12104 			 AND (revision = p_rev OR (revision IS NULL AND p_rev IS NULL))
12105 			 AND lot_number NOT IN (
12106 					   SELECT mtlt.lot_number
12107 				   FROM mtl_transaction_lots_temp mtlt
12108 				   WHERE mtlt.transaction_temp_id = p_temp_id
12109 				   )
12110 		AND lot_number IS NOT NULL
12111 		GROUP BY lot_number
12112 	  UNION
12113 	  SELECT mtlt.primary_quantity
12114 		   , mtlt.transaction_quantity
12115 		   , mtlt.lot_number
12116 		FROM mtl_transaction_lots_temp mtlt
12117 	   WHERE mtlt.transaction_temp_id = p_temp_id
12118 			ORDER BY lot_number;
12119 	  --/* Bug 9448490 Lot Substitution Project */ end
12120 
12121 BEGIN
12122 
12123 	SAVEPOINT LOOSE_MATCH ;
12124 
12125 	IF (l_debug = 1) THEN
12126 	  mydebug('In procedure loose_match');
12127 	  mydebug('loose_match p_org_id   : ' || p_org_id);
12128 	  mydebug('loose_match p_item_id  : ' || p_item_id);
12129 	  mydebug('loose_match p_temp_id  : ' || p_temp_id);
12130 	  mydebug('loose_match p_trx_qty  : ' || p_trx_qty);
12131 	  mydebug('loose_match p_pri_uom  : ' || p_pri_uom);
12132 	  mydebug('loose_match p_sec_uom  : ' || p_sec_uom);
12133 	  mydebug('loose_match Fulfillment Base : ' || l_fulfillment_base);
12134 	END IF;
12135 
12136 	x_return_status     := fnd_api.g_ret_sts_success;
12137 	x_lot_att_vector    := null;
12138 
12139 	DELETE wms_allocations_gtmp;
12140 	t_lpn_lot_qty_table.DELETE;
12141 
12142 	x_trx_qty       := 0;
12143 	x_trx_sec_qty := 0;  -- Bug #4141928
12144 	x_trx_qty_alloc := 0;
12145 
12146 	 -- Bug #7257709: pass destination sub code to INV_TXN_VALIDATIONS.get_available_quantity
12147 	 -- in case of move order tasks
12148 	IF (p_transaction_type_id = 64 AND p_transaction_action_id = 2) THEN
12149 		   OPEN mmtt_csr;
12150 		   FETCH mmtt_csr INTO l_transfer_subinventory;
12151 		   CLOSE mmtt_csr;
12152 	END IF;
12153 
12154 		IF p_is_sn_alloc = 'Y' THEN --{ serial is allocated
12155 		-- create the lot vector and populate the temp table for serial allocated case
12156 
12157 			 IF (l_debug = 1) THEN
12158 				  mydebug('SN control and SN allocation on');
12159 			 END IF;
12160 
12161 			IF p_is_lot_control ='true' THEN --{ lot controlled
12162 				 INSERT INTO wms_allocations_gtmp
12163 				 (lot_number,
12164 				  serial_number,
12165 				  transaction_quantity,
12166 				  primary_quantity)
12167 				  SELECT mtlt.lot_number,fm_serial_number,1,1
12168 				  FROM mtl_serial_numbers_temp msnt,
12169 					   mtl_transaction_lots_temp mtlt,
12170 					   mtl_serial_numbers msn
12171 				  WHERE mtlt.transaction_temp_id = p_temp_id
12172 					AND msnt.transaction_temp_id = mtlt.serial_transaction_temp_id
12173 					AND msnt.fm_serial_number = msn.serial_number
12174 					AND msn.lpn_id is null -- make sure it is loose pick
12175 					AND msn.inventory_item_id = p_item_id;
12176 
12177 				  x_trx_qty_alloc := SQL%ROWCOUNT;
12178 				 -- populate the lot vector
12179 				 FOR lot_ATT_rec in lot_att LOOP
12180 					  IF (l_debug = 1) THEN
12181 						  mydebug('lot_ATT_rec.lot_number: '||lot_ATT_rec.lot_number);
12182 						  mydebug('lot_ATT_rec.transaction_quantity: '||lot_ATT_rec.transaction_quantity);
12183 					  END IF;
12184 					  l_table_index := l_table_index + 1;
12185 					  t_lpn_lot_qty_table(l_table_index).lot_number  := lot_ATT_rec.lot_number;
12186 					  t_lpn_lot_qty_table(l_table_index).trx_qty     := lot_ATT_rec.transaction_quantity;
12187 				 END LOOP;
12188 			ELSE -- not lot controlled
12189 				INSERT INTO wms_ALLOCATIONS_GTMP
12190 				 (serial_number,
12191 				  transaction_quantity,
12192 				  primary_quantity)
12193 				  SELECT fm_serial_number,1,1
12194 				  FROM mtl_serial_numbers_temp msnt,
12195 					   mtl_serial_numbers msn
12196 				  WHERE  msnt.transaction_temp_id = p_temp_id
12197 					AND msnt.fm_serial_number = msn.serial_number
12198 					AND msn.lpn_id is null
12199 					AND msn.inventory_item_id = p_item_id;
12200 
12201 				  x_trx_qty_alloc := SQL%ROWCOUNT;
12202 			END IF; --} lot controlled
12203 		END IF; --} serial controlled
12204 
12205 	IF (l_debug = 1) THEN
12206 		   mydebug('x_trx_qty_alloc: '||x_trx_qty_alloc);
12207 	END IF;
12208 
12209 	l_table_index := 0;
12210 	 mydebug('Opening  lot_csr cursor in loose_match procedure ');
12211 	 --/* Bug 9448490 Lot Substitution Project */ start
12212 	 mydebug('p_changelotNoException : ' || p_changelotNoException);
12213 	 mydebug('p_full_lot_allocation : ' || p_full_lot_allocation);-- Added for 14699845 (Flexible Lot Allocation)
12214 
12215 	IF (p_changelotNoException = 'Y' OR p_full_lot_allocation IN ('N', 'P') )THEN -- Modified for 14699845 (Flexible Lot Allocation)
12216 		OPEN lot_substitution_att;
12217 	ELSE
12218 		OPEN lot_csr;
12219 	END IF;
12220 	--   OPEN lot_csr;
12221 	 --/* Bug 9448490 Lot Substitution Project */ end
12222 
12223 			IF inv_cache.set_item_rec(p_org_id,p_item_id) THEN
12224 				IF inv_cache.item_rec.tracking_quantity_ind = 'PS' THEN
12225 					l_dual_uom_item := TRUE;
12226 					IF (l_debug = 1) THEN
12227 					mydebug('Item is dual UOM');
12228 					END IF;
12229 				ELSE
12230 					l_dual_uom_item := FALSE;
12231 					IF (l_debug = 1) THEN
12232 					mydebug('Item is NOT dual UOM');
12233 					END IF;
12234 				END IF;
12235 			ELSE
12236 				IF (l_debug = 1) THEN
12237 				mydebug('Error from inv_cache.set_item_rec');
12238 				END IF;
12239 			RAISE fnd_api.g_exc_unexpected_error;
12240 			END IF;
12241 
12242 	LOOP
12243 
12244 		IF (p_is_lot_control ='true' ) THEN
12245 		--/* Bug 9448490 Lot Substitution Project */ start
12246 			IF (p_changelotNoException = 'Y' OR p_full_lot_allocation IN ('N', 'P') )THEN -- Modified for 14699845 (Flexible Lot Allocation)
12247 				FETCH  lot_substitution_att INTO l_lot_primary_qty, l_trx_lot_qty,l_lot_number;
12248 				EXIT WHEN lot_substitution_att%NOTFOUND;
12249 			ELSE
12250 				FETCH  lot_csr INTO l_lot_primary_qty, l_trx_lot_qty, l_lot_sec_qty, l_lot_number; -- Bug #4141928
12251 				EXIT WHEN lot_csr%NOTFOUND;
12252 			END IF; --/* Bug 9448490 Lot Substitution Project */
12253 			 IF (l_debug = 1) THEN
12254 				 mydebug('l_mtlt_lot_number   : ' || l_lot_number);
12255 				 mydebug('l_mtlt_primary_qty  : ' || l_lot_primary_qty);
12256 				 mydebug('l_mtlt_secondary_qty: ' || l_lot_sec_qty);
12257 			 END IF;
12258 
12259 		END IF;
12260 
12261 		IF (p_suggested_locator = p_confirmed_locator) THEN
12262 			UPDATE mtl_material_transactions_temp mmtt
12263 			SET posting_flag = 'N'
12264 			WHERE transaction_temp_id = p_temp_id;
12265 		END IF;
12266 
12267 		-- End change - Bug 4185621
12268 		-- always do one query at least for non lot controlled items
12269 
12270 		INV_TXN_VALIDATIONS.get_available_quantity
12271 		  (x_return_status => l_return_status,
12272 		   p_tree_mode  => inv_quantity_tree_pub.g_loose_only_mode,
12273 		   p_organization_id =>p_org_id,
12274 		   p_inventory_item_id => p_item_id,
12275 		   p_is_revision_control =>p_is_revision_control,
12276 		   p_is_lot_control =>p_is_lot_control,
12277 		   p_is_serial_control =>p_is_serial_control,
12278 		   p_revision =>p_rev,
12279 		   p_lot_number =>l_lot_number,
12280 		   p_grade_code          =>    NULL,        -- Bug #4141928
12281 		   p_lot_expiration_date =>null,
12282 		   p_subinventory_code =>p_confirmed_sub,
12283 		   p_locator_id =>p_confirmed_locator,
12284 		   p_source_type_id =>-9999,
12285 		   p_cost_group_id => NULL,
12286 		   p_to_subinventory_code => l_transfer_subinventory,
12287 		   x_qoh                 =>    l_qoh,
12288 		   x_att                 =>    l_att,
12289 		   x_sqoh                =>    l_sqoh,     -- Bug #4141928
12290 		   x_satt                =>    l_satt      -- Bug #4141928
12291 		 );
12292 
12293 			--Bug 4185621: restore posting flag in mmtt
12294 
12295 				 IF (p_suggested_locator = p_confirmed_locator) THEN
12296 					 UPDATE mtl_material_transactions_temp mmtt
12297 						SET posting_flag = 'Y'
12298 					  WHERE transaction_temp_id = p_temp_id;
12299 				 END IF;
12300 			-- End change - Bug 4185621
12301 
12302 		IF (l_return_status = fnd_api.g_ret_sts_success) THEN --{
12303 
12304 			-- convert the qty from primary UOM to transaction UOM
12305 			IF (l_debug = 1) THEN
12306 				   mydebug('before the vector string l_att_trx_qty: '||l_att_trx_qty);
12307 				   mydebug('before the vector string l_trx_qoh    : '||l_trx_qoh);
12308 				   mydebug('before the vector string l_att        : '||l_att);
12309 				   mydebug('before the vector string l_qoh        : '||l_qoh);
12310 				   mydebug('before the vector string l_satt       : '||l_satt);
12311 				   mydebug('before the vector string l_sqoh       : '||l_sqoh);
12312 				   mydebug('before the vector string p_pri_uom    : '||p_pri_uom);
12313 				   mydebug('before the vector string p_sec_uom    : '||p_sec_uom);
12314 				   mydebug('before the vector string p_trx_uom    : '||p_trx_uom);
12315 			END IF;
12316 
12317 			IF (p_pri_uom = p_trx_uom) THEN --{
12318 				IF (l_debug = 1) THEN
12319 					   mydebug('primary uom is same as transaction uom');
12320 				END IF;
12321 				 l_att_trx_qty := l_att;
12322 				 l_trx_qoh     := l_qoh;
12323 
12324 			ELSIF (p_sec_uom = p_trx_uom) THEN
12325 				IF (l_debug = 1) THEN
12326 					   mydebug('secondary uom is same as transaction uom');
12327 				END IF;
12328 				 l_att_trx_qty := l_satt;
12329 				 l_trx_qoh     := l_sqoh;
12330 			ELSE
12331 				IF (l_fulfillment_base = 'S') THEN
12332 					l_att_trx_qty := inv_convert.inv_um_convert(
12333 													item_id        => p_item_id
12334 												   ,precision      => null
12335 												   ,from_quantity  => l_satt
12336 												   ,from_unit      => p_sec_uom
12337 												   ,to_unit        => p_trx_uom
12338 												   ,from_name      => null
12339 												   ,to_name        => null);
12340 					l_trx_qoh := inv_convert.inv_um_convert(
12341 													item_id        => p_item_id
12342 												   ,precision      => null
12343 												   ,from_quantity  => l_sqoh
12344 												   ,from_unit      => p_sec_uom
12345 												   ,to_unit        => p_trx_uom
12346 												   ,from_name      => null
12347 												   ,to_name        => null);
12348 
12349 				ELSE
12350 
12351 					l_att_trx_qty := inv_convert.inv_um_convert(
12352 													item_id        => p_item_id
12353 												   ,precision      => null
12354 												   ,from_quantity  => l_att
12355 												   ,from_unit      => p_pri_uom
12356 												   ,to_unit        => p_trx_uom
12357 												   ,from_name      => null
12358 												   ,to_name        => null);
12359 					l_trx_qoh := inv_convert.inv_um_convert(
12360 													item_id        => p_item_id
12361 												   ,precision      => null
12362 												   ,from_quantity  => l_qoh
12363 												   ,from_unit      => p_pri_uom
12364 												   ,to_unit        => p_trx_uom
12365 												   ,from_name      => null
12366 												   ,to_name        => null);
12367 				END IF;
12368 			END IF;--}
12369 
12370 
12371 			IF l_dual_uom_item THEN
12372 				l_att_trx_sec_qty := l_satt;   -- Bug #4141928
12373 				l_trx_sec_qoh     := l_sqoh;   -- Bug #4141928
12374 			END IF;
12375 
12376 			IF (l_debug = 1) THEN
12377 				mydebug('l_att_trx_qty    : '||l_att_trx_qty);
12378 				mydebug('l_trx_qoh        : '||l_trx_qoh);
12379 				mydebug('l_att_trx_sec_qty: '||l_att_trx_sec_qty);
12380 				mydebug('l_trx_sec_qoh    : '||l_trx_sec_qoh);
12381 			 END IF;
12382 
12383 
12384 			IF (p_is_lot_control ='true' ) THEN --{
12385 			-- populate the lot vector
12386 			-- Modified for 14699845 (Flexible Lot Allocation) l_table_index := l_table_index + 1;
12387 				 IF (p_is_sn_alloc = 'Y' AND t_lpn_lot_qty_table.EXISTS(l_table_index + 1) AND t_lpn_lot_qty_table(l_table_index + 1).lot_number = l_lot_number) THEN  -- Modified for 14699845 (Flexible Lot Allocation)
12388 					l_table_index := l_table_index + 1;
12389 					x_lot_att_vector  := x_lot_att_vector
12390 									  ||l_lot_number || '@@@@@'
12391 									  ||t_lpn_lot_qty_table(l_table_index).trx_qty|| '@@@@@'
12392 									  ||l_att_trx_qty
12393 									  || '&&&&&'
12394 									  || l_att_trx_sec_qty     -- Bug #4141928
12395 									  || '#####';              -- Bug #4141928
12396 					 l_lot_v   := l_lot_v
12397 								||l_lot_number||':'; --Bug 3855835
12398 					IF (l_debug = 1) THEN
12399 						   mydebug('l_table_index: '||l_table_index);
12400 						   mydebug('x_lot_att_vector: '||x_lot_att_vector);
12401 					END IF;
12402 				 ELSE -- serial number is not allocated,
12403 					x_lot_att_vector  := x_lot_att_vector
12404 									  ||l_lot_number || '@@@@@'
12405 									  ||l_att_trx_qty|| '@@@@@'
12406 									  ||l_att_trx_qty
12407 									  || '&&&&&'
12408 									  || l_att_trx_sec_qty     -- Bug #4141928
12409 									  || '#####';              -- Bug #4141928
12410 					 mydebug('PRINTING x_lot_att_vector::'||x_lot_att_vector);  -- bug 14003388
12411 					 mydebug('PRINTING L_LOT_V LENGTH'||Length(l_lot_v));       -- bug 14003388
12412 					 l_lot_v   := l_lot_v
12413 								||l_lot_number||':';
12414 					IF (l_debug = 1) THEN
12415 						   mydebug('inserting into global temp table for serial is non allocated....');
12416 					END IF;
12417 					-- If negative Balance allowed then update vikas 09/07/04 V1
12418 					IF (p_is_negbal_allowed ='true') THEN
12419 						INSERT INTO wms_ALLOCATIONS_GTMP
12420 						 (lot_number,
12421 						  primary_quantity,
12422 						  transaction_quantity,
12423 						  secondary_quantity) -- Bug #4141928
12424 						VALUES( l_lot_number,
12425 								l_lot_primary_qty,
12426 								l_trx_lot_qty,
12427 								l_trx_lot_sec_qty);
12428 
12429 					 -- vikas 09/07/04 end
12430 					ELSE
12431 						INSERT INTO wms_ALLOCATIONS_GTMP
12432 						  (lot_number,
12433 						   primary_quantity,
12434 						   transaction_quantity,
12435 						   secondary_quantity) -- Bug #4141928
12436 						   VALUES( l_lot_number,
12437 						   LEAST(l_lot_primary_qty,l_att),
12438 						   LEAST(l_trx_lot_qty,l_att_trx_qty),
12439 						   LEAST(l_trx_lot_sec_qty,l_att_trx_sec_qty));
12440 					END IF;
12441 				 END IF;
12442 
12443 				 x_trx_qty := x_trx_qty + l_att_trx_qty;
12444 				 x_trx_sec_qty := x_trx_sec_qty + l_att_trx_sec_qty;
12445 
12446 
12447 			ELSE   --non lot controlled items
12448 				 x_trx_qty := l_att_trx_qty;
12449 				 x_trx_sec_qty :=  l_att_trx_sec_qty;       -- Bug #4141928
12450 			END IF; --}
12451 
12452 		ELSE
12453 			IF (l_debug = 1) THEN
12454 				mydebug('calling qty tree 1st time failed ');
12455 			END IF;
12456 			  fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
12457 			  fnd_message.set_token('ROUTINE','INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
12458 			  fnd_msg_pub.ADD;
12459 			RAISE fnd_api.g_exc_error;
12460 		END IF; --}
12461 
12462 	EXIT WHEN  p_is_lot_control ='false';
12463 	END LOOP;
12464 
12465 	IF (l_debug = 1) THEN
12466 		  mydebug('After loop loose_match x_trx_qty    : '||x_trx_qty);
12467 		  mydebug('After loop loose_match x_trx_sec_qty: '||x_trx_sec_qty);
12468 	END IF;
12469 
12470 	--Changes starting here for Bug 14003388
12471 	IF (p_is_lot_control = 'true' ) THEN
12472 		BEGIN
12473 		IF(LENGTH(x_lot_att_vector) > 4000) THEN
12474 		x_lot_att_vector2 := SUBSTR(x_lot_att_vector, 4001, 8000);
12475 		x_lot_att_vector3 := SUBSTR(x_lot_att_vector, 8001, 12000);
12476 		x_lot_att_vector4 := SUBSTR(x_lot_att_vector, 12001);
12477 		x_lot_att_vector := SUBSTR(x_lot_att_vector, 1, 4000);
12478 			IF (l_debug = 1) THEN
12479 				mydebug('x_lot_att_vector  : '||x_lot_att_vector);
12480 				mydebug('x_lot_att_vector2 : '||x_lot_att_vector2);
12481 				mydebug('x_lot_att_vector3 : '||x_lot_att_vector3);
12482 				mydebug('x_lot_att_vector4 ::'||x_lot_att_vector4);
12483 			END IF;
12484 		ELSE
12485 			IF (l_debug = 1) THEN
12486 			mydebug('x_lot_att_vector string length less that 4000 chars and needs no trimming: '||x_lot_att_vector);
12487 			END IF;
12488 		END IF;
12489 		EXCEPTION
12490 			WHEN OTHERS THEN
12491 			IF (l_debug = 1) THEN
12492 			mydebug('x_lot_att_vector  string trim had issues: '||x_lot_att_vector);
12493 			END IF;
12494 		END;
12495 	END IF;
12496 	--Changes ending here for Bug 14003388
12497 
12498 	--/* Bug 9448490 Lot Substitution Project */ start
12499 	IF lot_substitution_att%isopen THEN
12500 	  CLOSE lot_substitution_att;
12501 	END IF;
12502 	IF lot_csr%isopen THEN
12503 		CLOSE lot_csr;
12504 	END IF;
12505 	--/* Bug 9448490 Lot Substitution Project */ end
12506 
12507 	IF l_lot_v IS NOT NULL THEN
12508 	 l_lot_v := SUBSTR(l_lot_v,1,LENGTH(l_lot_v)-1);
12509    END IF;
12510 
12511 	  --v1 calling validate_pick_to_lpn for validating toLPNdefault and if it
12512 	  --returns success then allow to set to LPN field with toLPNDefault on
12513 	  --MainPickPage. Bug 3855835
12514 
12515   IF p_toLPN_Default IS NOT NULL  THEN
12516 		IF p_is_lot_control ='true' THEN
12517 			l_is_lot_control :='Y';
12518 		ELSE
12519 			l_is_lot_control :='N';
12520 		END If;
12521 
12522 		IF p_is_serial_control ='true' THEN
12523 			l_is_serial_control := 'Y';
12524 		ELSE
12525 			l_is_serial_control := 'N';
12526 		END IF;
12527 
12528 		IF p_is_revision_control ='true' THEN
12529 			l_is_revision_control := 'Y';
12530 		ELSE
12531 			l_is_revision_control :='N';
12532 		END IF;
12533 
12534 	validate_pick_to_lpn
12535 	( p_api_version_number          =>   1.0
12536 	, p_init_msg_lst                =>   NULL
12537 	, x_return_status               =>   x_return_status
12538 	, x_msg_count                   =>   l_msg_cnt
12539 	, x_msg_data                    =>   l_msg_data
12540 	, p_organization_id             =>   p_org_id
12541 	, p_pick_to_lpn                 =>   p_toLPN_Default
12542 	, p_temp_id                     =>   p_temp_id
12543 	, p_project_id                  =>   p_project_id
12544 	, p_task_id                     =>   p_task_id
12545 	, p_container_item              =>   NULL
12546 	, p_container_item_id           =>   NULL
12547 	, p_suggested_container_item    =>   NULL
12548 	, p_suggested_container_item_id =>   NULL
12549 	, p_suggested_carton_name       =>   NULL
12550 	, p_suggested_tolpn_id          =>   NULL
12551 	, x_pick_to_lpn_id              =>   l_pick_to_lpn_id
12552 	, p_inventory_item_id           =>   p_item_id
12553 	, p_confirmed_sub               =>   p_confirmed_sub
12554 	, p_confirmed_loc_id            =>   p_confirmed_locator
12555 	, p_revision                    =>   p_rev
12556 	, p_confirmed_lots              =>   l_lot_v
12557 	, p_from_lpn_id                 =>   NULL
12558 	, p_lot_control                 =>   l_is_lot_control
12559 	, p_revision_control            =>   l_is_revision_control
12560 	, p_serial_control              =>   l_is_serial_control
12561 	-- Bug 4632519
12562 	, p_trx_type_id                 =>   to_char(p_transaction_type_id)
12563 	, p_trx_action_id               =>   to_char(p_transaction_action_id)
12564 	-- Bug 4632519
12565    );
12566 
12567 
12568 	IF x_return_status <> fnd_api.g_ret_sts_success THEN
12569 		x_toLPN_status := 'F';
12570 		IF (l_debug = 1) THEN
12571 		mydebug('Validate_pick_to_lpn could not validate toLPNDefault:');
12572 		END IF;
12573 		x_return_status := fnd_api.g_ret_sts_success;
12574 	ELSE
12575 		x_toLPN_status := 'T';
12576 		IF (l_debug = 1) THEN
12577 		mydebug('Validate_pick_to_lpn  validated  toLPNDefault:');
12578 		END IF;
12579 	END IF;
12580  END IF;
12581 
12582 	EXCEPTION
12583 		WHEN fnd_api.g_exc_error THEN
12584 		  IF (l_debug = 1) THEN
12585 			mydebug('Exception raised');
12586 		  END IF;
12587 
12588 		  x_return_status  := fnd_api.g_ret_sts_error;
12589 		  fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
12590 		WHEN OTHERS THEN
12591 		  IF (l_debug = 1) THEN
12592 			mydebug('Other exception raised : ' || SQLERRM);
12593 		  END IF;
12594 
12595 		  x_return_status  := fnd_api.g_ret_sts_unexp_error;
12596 		  fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
12597 END LOOSE_MATCH;
12598 
12599 
12600 FUNCTION can_pickdrop(p_transaction_temp_id IN NUMBER) RETURN VARCHAR2 IS
12601 	  l_ret       VARCHAR2(10) := 'PASS';
12602 	  l_debug     NUMBER      := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
12603 	CURSOR c_cancelled_tasks IS
12604 		--SELECT decode(mmtt.transaction_type_id, 35,'N',51,'N','Y')
12605 		SELECT 'FAIL'
12606 		  FROM mtl_material_transactions_temp mmtt, mtl_txn_request_lines mol
12607 		 WHERE mmtt.transaction_temp_id = p_transaction_temp_id
12608 		   AND mmtt.move_order_line_id = mol.line_id
12609 		   AND mol.line_status = inv_globals.g_to_status_cancel_by_source
12610 		   AND ROWNUM = 1;
12611 BEGIN
12612    g_debug := l_debug;
12613    mydebug('In CAN_PICKDROP for Transaction Temp ID = ' || p_transaction_temp_id);
12614 
12615    OPEN c_cancelled_tasks;
12616 	  FETCH c_cancelled_tasks INTO l_ret;
12617 	  IF c_cancelled_tasks%NOTFOUND THEN
12618 		  mydebug('Found no Cancelled Task' );
12619 	  ELSE
12620 		  mydebug('Found Cancelled Tasks');
12621 	  END IF;
12622    CLOSE c_cancelled_tasks;
12623    mydebug('l_ret : ' || l_ret);
12624    RETURN l_ret;
12625 END  can_pickdrop;
12626 
12627 
12628 PROCEDURE check_pack_lpn
12629 	( p_lpn                IN             VARCHAR2
12630 	, p_org_id             IN             NUMBER
12631 	, p_container_item_id  IN             NUMBER
12632 	, p_temp_id            IN             NUMBER --Bug7120019
12633 	, x_lpn_id        OUT NOCOPY          NUMBER
12634 	, x_lpn_context   OUT NOCOPY          NUMBER
12635 	, x_outermost_lpn_id   OUT NOCOPY     NUMBER
12636 	, x_pick_to_lpn_exists OUT NOCOPY     BOOLEAN
12637 	, x_return_status OUT NOCOPY    VARCHAR2
12638 	, x_msg_count     OUT NOCOPY    NUMBER
12639 	, x_msg_data      OUT NOCOPY    VARCHAR2
12640 	) IS
12641 	  lpn_cont        NUMBER         := 0;
12642 	  create_lpn      VARCHAR2(1)    := 'N';
12643 	  l_return_status VARCHAR2(1);
12644 	  l_msg_count     NUMBER;
12645 	  l_msg_data      VARCHAR2(4000);
12646 	  l_exist         NUMBER;
12647 	  p_lpn_id        NUMBER;
12648 	  l_org_id        NUMBER;
12649 	  l_locator_id    NUMBER;
12650 	  l_debug         NUMBER         := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
12651 	  honor_case_pick_count NUMBER   := 0; --Bug7120019
12652 	  l_CursorStmt          VARCHAR2(32000);
12653 	  l_CursorID            INTEGER;
12654 	  l_Dummy               INTEGER;
12655 
12656 	BEGIN
12657 	  IF (l_debug = 1) THEN
12658 		mydebug('check_pack_lpn: check_pack_lpn begins');
12659 	  END IF;
12660 
12661 	  l_return_status  := fnd_api.g_ret_sts_success;
12662 
12663 	  IF ((p_lpn IS NULL)
12664 		  OR(p_lpn = '')) THEN
12665 		x_return_status  := fnd_api.g_ret_sts_success;
12666 		RETURN;
12667 	  END IF;
12668 
12669 	  BEGIN
12670 		SELECT lpn_context
12671 			 , organization_id
12672 			 , locator_id
12673 			 , lpn_id
12674 			 , outermost_lpn_id
12675 		  INTO lpn_cont
12676 			 , l_org_id
12677 			 , l_locator_id
12678 			 , x_lpn_id
12679 			 , x_outermost_lpn_id
12680 		  FROM wms_license_plate_numbers
12681 		 WHERE license_plate_number = p_lpn;
12682 		 x_pick_to_lpn_exists := TRUE;
12683 		 x_lpn_context := lpn_cont;
12684 	  EXCEPTION
12685 		WHEN NO_DATA_FOUND THEN
12686 		  create_lpn  := 'Y';
12687 		  x_pick_to_lpn_exists := FALSE;
12688 	  END;
12689 
12690 	  IF (create_lpn = 'N'
12691 		 AND (l_org_id is not null and l_org_id <> p_org_id)
12692 			) THEN
12693 		   IF (l_debug = 1) THEN
12694 			 mydebug('check_pack_lpn: LPN already exists but with different context or Org');
12695 		 END IF;--bug9165521
12696 		 fnd_message.set_name('WMS', 'WMS_INVLD_PICKTO_LPN_ORG');
12697 			 fnd_msg_pub.ADD;
12698 
12699 
12700 		   x_return_status  := fnd_api.g_ret_sts_error;
12701 		   RETURN;
12702 	  END IF;
12703 
12704 
12705 	  --Bug7120019
12706 	  IF wms_control.g_current_release_level >= 120001 THEN
12707 		 l_CursorStmt := 'SELECT count (*) FROM mtl_material_transactions_temp mmtt, wms_user_task_type_attributes wutta '||
12708 						 'WHERE mmtt.transaction_temp_id = :x_temp_id ' ||
12709 						 ' AND mmtt.standard_operation_id = wutta.user_task_type_id '||
12710 						 ' AND mmtt.organization_id = wutta.organization_id '||
12711 						 ' AND wutta.honor_case_pick_flag = ''Y'' ';  --Added for Bug#7584906
12712 		 l_CursorID := DBMS_SQL.OPEN_CURSOR;
12713 		 DBMS_SQL.PARSE(l_CursorID, l_CursorStmt, DBMS_SQL.V7);
12714 		 DBMS_SQL.DEFINE_COLUMN(l_CursorID, 1, honor_case_pick_count);
12715 		 DBMS_SQL.BIND_VARIABLE(l_CursorID, ':x_temp_id', p_temp_id);
12716 		 l_Dummy := DBMS_SQL.EXECUTE(l_CursorID);
12717 		 l_Dummy := DBMS_SQL.FETCH_ROWS(l_CursorID);
12718 		 DBMS_SQL.COLUMN_VALUE(l_CursorID, 1, honor_case_pick_count);
12719 		 DBMS_SQL.CLOSE_CURSOR(l_CursorID);
12720 	  END IF;
12721 
12722 	  IF (create_lpn = 'N') THEN
12723 		IF (wms_control.g_current_release_level >= 120001 AND honor_case_pick_count > 0) THEN
12724 		  IF (
12725 			   lpn_cont = wms_container_pub.lpn_context_wip
12726 			   OR lpn_cont = wms_container_pub.lpn_context_rcv
12727 			   OR lpn_cont = wms_container_pub.lpn_context_stores
12728 			   OR lpn_cont = wms_container_pub.lpn_context_intransit
12729 			   OR lpn_cont = wms_container_pub.lpn_context_vendor
12730 			   OR lpn_cont = wms_container_pub.lpn_loaded_for_shipment
12731 			   OR lpn_cont = wms_container_pub.lpn_prepack_for_wip
12732 			   OR lpn_cont = wms_container_pub.lpn_context_picked
12733 			   OR lpn_cont = wms_container_pub.LPN_CONTEXT_INV
12734 		   OR lpn_cont = wms_container_pub.LPN_CONTEXT_PACKING
12735 			  ) THEN
12736 			  --OR (l_org_id is not null and l_org_id <> p_org_id)
12737 
12738 			 IF (l_debug = 1) THEN
12739 			   mydebug('check_pack_lpn: LPN already exists but with different context or Org');
12740 		   END IF;--bug9165521
12741 		   fnd_message.set_name('WMS', 'WMS_INVLD_PICKTO_LPN_CONTEXT');
12742 			   fnd_msg_pub.ADD;
12743 
12744 
12745 			 x_return_status  := fnd_api.g_ret_sts_error;
12746 			 RETURN;
12747 		  END IF;
12748 
12749 	ELSE -- Not Honor Case Pick
12750 		  IF (
12751 			   lpn_cont = wms_container_pub.lpn_context_wip
12752 			   OR lpn_cont = wms_container_pub.lpn_context_rcv
12753 			   OR lpn_cont = wms_container_pub.lpn_context_stores
12754 			   OR lpn_cont = wms_container_pub.lpn_context_intransit
12755 			   OR lpn_cont = wms_container_pub.lpn_context_vendor
12756 			   OR lpn_cont = wms_container_pub.lpn_loaded_for_shipment
12757 			   OR lpn_cont = wms_container_pub.lpn_prepack_for_wip
12758 			   OR lpn_cont = wms_container_pub.lpn_context_picked
12759 			   OR lpn_cont = wms_container_pub.LPN_CONTEXT_INV
12760 			  ) THEN
12761 
12762 			  IF (l_debug = 1) THEN
12763 			  mydebug('check_pack_lpn: LPN already exists but with different context or Org');
12764 		  END IF;--bug9165521
12765 		  fnd_message.set_name('WMS', 'WMS_INVLD_PICKTO_LPN_CONTEXT');
12766 			  fnd_msg_pub.ADD;
12767 
12768 
12769 			  x_return_status  := fnd_api.g_ret_sts_error;
12770 			  RETURN;
12771 		   END IF;
12772 		 END IF;
12773 	   END IF; --Bug7120019
12774 
12775 	  IF create_lpn = 'Y' THEN
12776 		IF (l_debug = 1) THEN
12777 		  mydebug('check_pack_lpn: calling wms_container_pub.create_lpn');
12778 		END IF;
12779 
12780 		wms_container_pub.create_lpn
12781 		( p_api_version                => 1.0
12782 		, x_return_status              => l_return_status
12783 		, x_msg_count                  => l_msg_count
12784 		, x_msg_data                   => x_msg_data
12785 		, p_lpn                        => p_lpn
12786 		, p_organization_id            => p_org_id
12787 		, p_container_item_id          => p_container_item_id
12788 		, x_lpn_id                     => x_lpn_id
12789 		, p_source                     => 5
12790 		);
12791 
12792 		IF (l_msg_count = 0) THEN
12793 		  IF (l_debug = 1) THEN
12794 			mydebug('check_pack_lpn: Successful');
12795 		  END IF;
12796 		ELSIF(l_msg_count = 1) THEN
12797 		  IF (l_debug = 1) THEN
12798 			mydebug('check_pack_lpn: Not Successful');
12799 			mydebug(REPLACE(x_msg_data, fnd_global.local_chr(0), ' '));
12800 		  END IF;
12801 		ELSE
12802 		  IF (l_debug = 1) THEN
12803 			mydebug('check_pack_lpn: Not Successful2');
12804 		  END IF;
12805 
12806 		  FOR i IN 1 .. l_msg_count LOOP
12807 			x_msg_data  := fnd_msg_pub.get(i, 'F');
12808 
12809 			IF (l_debug = 1) THEN
12810 			  mydebug(REPLACE(x_msg_data, fnd_global.local_chr(0), ' '));
12811 			END IF;
12812 		  END LOOP;
12813 		END IF;
12814 
12815 		IF l_return_status = fnd_api.g_ret_sts_unexp_error
12816 		   OR l_return_status = fnd_api.g_ret_sts_error THEN
12817 		   fnd_message.set_name('WMS', 'WMS_TD_CREATE_LPN_ERROR');
12818 		   fnd_msg_pub.ADD;
12819 		   RAISE fnd_api.g_exc_unexpected_error;
12820 		END IF;
12821 
12822 	  END IF;
12823 
12824 	  x_return_status  := fnd_api.g_ret_sts_success;
12825 
12826 	  IF (l_debug = 1) THEN
12827 		mydebug('check_pack_lpn: check_pack_lpn ends');
12828 	  END IF;
12829 	EXCEPTION
12830 	  WHEN fnd_api.g_exc_error THEN
12831 		x_return_status  := fnd_api.g_ret_sts_error;
12832 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
12833 		IF DBMS_SQL.IS_Open(l_cursorID) THEN
12834 		 DBMS_SQL.Close_Cursor(l_cursorID);
12835 		END IF;
12836 	  WHEN OTHERS THEN
12837 		x_return_status  := fnd_api.g_ret_sts_unexp_error;
12838 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
12839 	 IF DBMS_SQL.IS_Open(l_cursorID) THEN
12840 		 DBMS_SQL.Close_Cursor(l_cursorID);
12841 		END IF;
12842 END check_pack_lpn;
12843 
12844 
12845 PROCEDURE validate_pick_to_lpn
12846 	( p_api_version_number          IN            NUMBER
12847 	, p_init_msg_lst                IN            VARCHAR2
12848 	, x_return_status               OUT NOCOPY    VARCHAR2
12849 	, x_msg_count                   OUT NOCOPY    NUMBER
12850 	, x_msg_data                    OUT NOCOPY    VARCHAR2
12851 	, p_organization_id             IN            NUMBER
12852 	, p_pick_to_lpn                 IN            VARCHAR2   --New LPN
12853 	, p_temp_id                     IN            NUMBER
12854 	, p_project_id                  IN            NUMBER
12855 	, p_task_id                     IN            NUMBER
12856 	, p_container_item              IN            VARCHAR2
12857 	, p_container_item_id           IN            NUMBER
12858 	, p_suggested_container_item    IN            VARCHAR2
12859 	, p_suggested_container_item_id IN            NUMBER
12860 	, p_suggested_carton_name       IN            VARCHAR2
12861 	, p_suggested_tolpn_id          IN            NUMBER
12862 	, x_pick_to_lpn_id              OUT NOCOPY    NUMBER
12863 	, p_inventory_item_id           IN            NUMBER
12864 	, p_confirmed_sub               IN            VARCHAR2
12865 	, p_confirmed_loc_id            IN            NUMBER
12866 	, p_revision                    IN            VARCHAR2
12867 	, p_confirmed_lots              IN            VARCHAR2
12868 	, p_from_lpn_id                 IN            NUMBER
12869 	, p_lot_control                 IN            VARCHAR2
12870 	, p_revision_control            IN            VARCHAR2
12871 	, p_serial_control              IN            VARCHAR2
12872 	, p_trx_type_id                 IN            VARCHAR2 -- Bug 4632519
12873 	, p_trx_action_id               IN            VARCHAR2 -- Bug 4632519
12874 	) IS
12875 
12876 	  l_api_version_number CONSTANT NUMBER                      := 1.0;
12877 	  l_api_name           CONSTANT VARCHAR2(30)                := 'validate_pick_to_lpn';
12878 	  l_pick_to_lpn_exists          BOOLEAN                     := FALSE;
12879 	  l_current_mmtt_delivery_id    NUMBER                      := NULL;
12880 	  l_pick_to_lpn_delivery_id     NUMBER                      := NULL;
12881 	  l_pick_to_lpn_delivery_id2    NUMBER                      := -999;
12882 	  l_outermost_lpn_id            NUMBER                      := NULL;
12883 
12884 	  --Added for PJM Integration
12885 	  l_project_id                  NUMBER                      := NULL;
12886 	  l_task_id                     NUMBER                      := NULL;
12887 
12888 	  -- ********************* Start of bug fix 2078002 ********************
12889 	  l_mmtt_mo_type                NUMBER                      := NULL;
12890 	  l_mo_type_in_lpn              NUMBER                      := NULL;
12891 	  l_mmtt_wip_entity_type        NUMBER;
12892 	  l_mmtt_txn_type_id            NUMBER;
12893 	  l_wip_entity_type_in_lpn      NUMBER;
12894 	  -- ********************* End of bug fix 2078002 ********************
12895 
12896 	  l_xfr_sub                     VARCHAR2(30);
12897 	  l_xfr_to_location             NUMBER;
12898 	  l_lpn_controlled_flag         NUMBER;
12899 	  l_count                       NUMBER                      := 0;
12900 	  l_item_id                     NUMBER;
12901 	  l_operation_plan_id           NUMBER;
12902 	  l_current_carton_grouping_id  NUMBER                      := -999;
12903 	  l_carton_grouping_id          NUMBER                      := -999;
12904 	  l_parent_line_id              NUMBER;
12905 	  l_transaction_header_id       NUMBER;
12906 	  l_multiple_pick               VARCHAR2(1);
12907 	  l_bulk_task_exist             VARCHAR2(1);
12908 -- Increased lot size to 80 Char - Mercy Thomas - B4625329
12909 	  l_lot_number                  VARCHAR2(80) := null;
12910 	  l_debug                       NUMBER                      := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
12911 	  --************added for cartonized check******************
12912 	  l_container_item_id           NUMBER;
12913 	  l_concatenated_segments       VARCHAR2(40);
12914 	  --***
12915 	  l_lot_control                 NUMBER  := 1;
12916 	  l_revision_control            NUMBER  := 1;
12917 	  l_commingle_exist             VARCHAR2(1);
12918 
12919 	  -- the delimiter for lot string
12920 	  l_delimiter                   VARCHAR2(30)   :=  ':';
12921 		 -- To parse lots
12922 	  m                             NUMBER := 1;  -- position of delimiter
12923 	  n                             NUMBER := 1;  -- Start position for substr or
12924 	  --
12925 	  -- Bug 4454837,this change will be removed after discussion
12926 	  -- Uncommenting for bug#10062741
12927 	  l_line_rows                   WSH_UTIL_CORE.id_tab_type;
12928 	  l_grouping_rows               WSH_UTIL_CORE.id_tab_type;
12929 	  l_same_carton_grouping        BOOLEAN := FALSE;
12930 	  l_return_status               VARCHAR2(2) ;
12931 	  --
12932 
12933 
12934 	  --Bug 6168447-Start
12935 	  l_lpn_name VARCHAR2(30);
12936 	  l_status_code VARCHAR2(1);
12937 	  l_delivery_id NUMBER;
12938 	  l_delivery_detail_id number;
12939 	  l_wsh_dd_upd_rec  WSH_GLBL_VAR_STRCT_GRP.Delivery_Details_Rec_Type;
12940 	  wsh_update_tbl  WSH_GLBL_VAR_STRCT_GRP.delivery_details_Attr_tbl_Type;
12941 	  l_IN_rec        WSH_GLBL_VAR_STRCT_GRP.detailInRecType;
12942 	  l_OUT_rec       WSH_GLBL_VAR_STRCT_GRP.detailOutRecType;
12943 	  --Bug 6168447-End
12944 
12945 	  l_fb_comingle VARCHAR2(1) :='N'; --MUoM
12946 
12947 	  TYPE lpn_rectype IS RECORD
12948 	  ( lpn_id           wms_license_plate_numbers.lpn_id%TYPE
12949 	  , lpn_context      wms_license_plate_numbers.lpn_context%TYPE
12950 	  , outermost_lpn_id wms_license_plate_numbers.outermost_lpn_id%TYPE
12951 	  );
12952 
12953 	  pick_to_lpn_rec               lpn_rectype;
12954 
12955 	  TYPE pjm_rectype IS RECORD
12956 	  ( prj_id mtl_item_locations.project_id%TYPE
12957 	  , tsk_id mtl_item_locations.task_id%TYPE
12958 	  );
12959 
12960 	  mtl_pjm_prj_tsk_rec           pjm_rectype;
12961 	  lpn_pjm_prj_tsk_rec           pjm_rectype;
12962 
12963 	  CURSOR others_in_mmtt_delivery_cursor(l_lpn_id IN NUMBER) IS
12964 		SELECT wda.delivery_id
12965 		  FROM wsh_delivery_assignments_v        wda
12966 			 , wsh_delivery_details_ob_grp_v   wdd
12967 			 , mtl_material_transactions_temp  mmtt
12968 		 WHERE mmtt.transfer_lpn_id   = l_lpn_id
12969 		   AND wda.delivery_detail_id = wdd.delivery_detail_id
12970 		   AND wdd.move_order_line_id = mmtt.move_order_line_id
12971 		   AND wdd.organization_id    = mmtt.organization_id;
12972 		   --AND wdd.released_status    = 'X';  -- For LPN reusability ER : 6845650 Commented for Bug#7430264
12973 
12974 	  CURSOR child_lpns_cursor(l_lpn_id IN NUMBER) IS
12975 		SELECT lpn_id
12976 		FROM   wms_license_plate_numbers
12977 		START  WITH lpn_id = l_lpn_id
12978 		CONNECT BY parent_lpn_id = PRIOR lpn_id;
12979 
12980 	  child_lpns_rec  child_lpns_cursor%ROWTYPE;
12981 
12982 	  CURSOR current_delivery_cursor IS
12983 		SELECT wda.delivery_id
12984 		  FROM wsh_delivery_assignments_v        wda
12985 			 , wsh_delivery_details_ob_grp_v   wdd
12986 			 , mtl_material_transactions_temp  mmtt
12987 		 WHERE wda.delivery_detail_id   = wdd.delivery_detail_id
12988 		   AND wdd.move_order_line_id   = mmtt.move_order_line_id
12989 		   AND wdd.organization_id      = mmtt.organization_id
12990 		   AND mmtt.transaction_temp_id = p_temp_id
12991 		   AND mmtt.organization_id     = p_organization_id;
12992 
12993 	  CURSOR drop_delivery_cursor(l_lpn_id IN NUMBER) IS
12994 		SELECT wda.delivery_id
12995 		  FROM wsh_delivery_assignments_v       wda
12996 			 , wsh_delivery_details_ob_grp_v  wdd
12997 		 WHERE wda.parent_delivery_detail_id = wdd.delivery_detail_id
12998 		   AND wdd.lpn_id = l_lpn_id
12999 		   AND wdd.organization_id = p_organization_id
13000 		   AND wdd.released_status = 'X';  -- For LPN reusability ER : 6845650
13001 
13002 	  --
13003 	  -- This cursor gets the project and task id for the lpn to be
13004 	  -- loaded into
13005 	  --
13006 	  CURSOR lpn_project_task_cursor(p_pick_to_lpn_id NUMBER) IS
13007 		SELECT NVL(mil.project_id, -1)
13008 			 , NVL(mil.task_id, -1)
13009 		  FROM mtl_item_locations mil, mtl_material_transactions_temp mmtt
13010 		 WHERE mil.inventory_location_id = mmtt.transfer_to_location
13011 		   AND mil.organization_id       = mmtt.organization_id
13012 		   AND mmtt.transfer_lpn_id      = p_pick_to_lpn_id
13013 		   AND mmtt.organization_id      = p_organization_id;
13014 
13015 	  --
13016 	  -- This cursor gets the project and task id of the task that is about
13017 	  -- to be packed
13018 	  --
13019 	  CURSOR mtl_project_task_cursor IS
13020 		SELECT NVL(mil.project_id, -1)
13021 			 , NVL(mil.task_id, -1)
13022 		  FROM mtl_item_locations mil, mtl_material_transactions_temp mmtt
13023 		 WHERE mil.inventory_location_id = mmtt.transfer_to_location
13024 		   AND mil.organization_id       = mmtt.organization_id
13025 		   AND mmtt.organization_id      = p_organization_id
13026 		   AND mmtt.transaction_temp_id  = p_temp_id;
13027 
13028 	  CURSOR current_carton_grouping_cursor IS
13029 		SELECT mol.carton_grouping_id
13030 		  FROM mtl_txn_request_lines mol, mtl_material_transactions_temp mmtt
13031 		 WHERE mmtt.transaction_temp_id = p_temp_id
13032 		   AND mmtt.organization_id     = mol.organization_id
13033 		   AND mmtt.move_order_line_id  = mol.line_id;
13034 
13035 	  CURSOR others_carton_grouping_cursor(p_lpn_id IN NUMBER) IS
13036 		SELECT DISTINCT mol.carton_grouping_id
13037 				   FROM mtl_txn_request_lines mol, mtl_material_transactions_temp mmtt
13038 				  WHERE mmtt.transfer_lpn_id = p_lpn_id
13039 					AND mmtt.organization_id = mol.organization_id
13040 					AND mmtt.move_order_line_id = mol.line_id;
13041 
13042 	  -- Bug 6168447 : Cursot to check if there are any wdd present for the LPN
13043 
13044 	  CURSOR c_wdd_exists(p_lpn_id NUMBER,p_organization_id NUMBER) is
13045 	  SELECT distinct wda.delivery_id
13046 	  FROM wsh_delivery_details wdd, wsh_delivery_assignments wda
13047 	  WHERE wdd.lpn_id IN (select lpn_id from wms_license_plate_numbers
13048 						where organization_id = p_organization_id
13049 						and (lpn_id = p_lpn_id
13050 						or parent_lpn_id = p_lpn_id
13051 						or outermost_lpn_id = p_lpn_id))
13052 	  AND wda.parent_delivery_detail_id = wdd.delivery_detail_id
13053 	  AND wdd.released_status = 'X';  -- For LPN reusability ER : 6845650
13054 
13055 	BEGIN
13056 
13057 
13058 	  IF (l_debug = 1) THEN
13059 		 mydebug('validate_pick_to_lpn: Start Validate_pick_to_lpn.');
13060 	  END IF;
13061 
13062 	  --
13063 	  -- Standard call to check for call compatibility
13064 	  --
13065 	  IF NOT fnd_api.compatible_api_call(l_api_version_number, p_api_version_number, l_api_name, g_pkg_name) THEN
13066 		 fnd_message.set_name('WMS', 'WMS_CONT_INCOMPATIBLE_API_CALL');
13067 		 fnd_msg_pub.ADD;
13068 		 RAISE fnd_api.g_exc_error;
13069 	  END IF;
13070 
13071 	  --
13072 	  --  Initialize message list.
13073 	  --
13074 	  IF fnd_api.to_boolean(p_init_msg_lst) THEN
13075 		 fnd_msg_pub.initialize;
13076 	  END IF;
13077 
13078 	  --
13079 	  -- Initialize API return status to success
13080 	  --
13081 	  x_return_status        := fnd_api.g_ret_sts_success;
13082 
13083 	  --
13084 	  -- Begin validation process:
13085 	  -- Check if drop lpn exists by trying to retrieve its lpn ID.
13086 	  -- If it does not exist, no further validations required
13087 	  -- so return success.
13088 	  --
13089 	  -- jali changed the following: If the LPN doesn't exists then create the LPN.
13090 	  -- this will resolve the issue why the key in LPN name is not working
13091 	  -- also the following change will only query the WLPN once.
13092 	  IF (p_container_item_id = -1 OR p_container_item_id = 0) THEN -- no cartonization --Bug 8810402
13093 		  l_container_item_id := NULL;
13094 	  ELSE
13095 		  l_container_item_id := p_container_item_id;
13096 	  END IF;
13097 
13098 	   check_pack_lpn
13099 	  ( p_lpn                => p_pick_to_lpn
13100 	  , p_org_id             => p_organization_id
13101 	  , p_container_item_id  => l_container_item_id    --new IN parameter
13102 	  , p_temp_id            => p_temp_id              --Bug7120019
13103 	  , x_lpn_id             => pick_to_lpn_rec.lpn_id
13104 	  , x_lpn_context        => pick_to_lpn_rec.lpn_context
13105 	  , x_outermost_lpn_id   => pick_to_lpn_rec.outermost_lpn_id
13106 	  , x_pick_to_lpn_exists => l_pick_to_lpn_exists
13107 	  , x_return_status      => x_return_status
13108 	  , x_msg_count          => x_msg_count
13109 	  , x_msg_data           => x_msg_data
13110 	  );
13111 
13112 	  IF x_return_status = fnd_api.g_ret_sts_unexp_error
13113 		 OR x_return_status = fnd_api.g_ret_sts_error THEN
13114 		 RAISE fnd_api.g_exc_error;
13115 	  END IF;
13116 	  x_pick_to_lpn_id := pick_to_lpn_rec.lpn_id;
13117 
13118 	  IF (l_debug = 1) THEN
13119 		  mydebug('validate_pick_to_lpn:x_pick_to_lpn_id:'||x_pick_to_lpn_id);
13120 	  END IF;
13121 
13122 	  IF NOT l_pick_to_lpn_exists THEN
13123 		 IF (l_debug = 1) THEN
13124 			mydebug('validate_pick_to_lpn: Drop LPN is a new LPN, no checking required.');
13125 		 END IF;
13126 		 RETURN;
13127 	  --Bug 6168477 -start : Added the following ELSIF condition.
13128 	  ELSIF pick_to_lpn_rec.lpn_context = wms_container_pub.lpn_context_pregenerated THEN
13129 		 IF (l_debug = 1) THEN
13130 			mydebug('validate_pick_to_lpn: Drop LPN is in context 5,check if associated records are presetn in WDD.');
13131 		  END IF;
13132 		 OPEN c_wdd_exists(pick_to_lpn_rec.lpn_id,p_organization_id);
13133 		 FETCH c_wdd_exists into l_delivery_id;
13134 		 IF c_wdd_exists%NOTFOUND THEN
13135 			CLOSE c_wdd_exists;
13136 		 ELSE
13137 		  IF (l_delivery_id IS NOT NULL )THEN
13138 				   BEGIN
13139 						   SELECT wlpn.LICENSE_PLATE_NUMBER
13140 						   INTO l_lpn_name
13141 						   FROM Wms_License_Plate_Numbers wlpn
13142 						   WHERE organization_id = p_organization_id
13143 						   and (lpn_id = pick_to_lpn_rec.lpn_id
13144 						   or parent_lpn_id = pick_to_lpn_rec.lpn_id
13145 						   or outermost_lpn_id = pick_to_lpn_rec.lpn_id);
13146 
13147 						   SELECT wdd.released_status,wdd.delivery_detail_id
13148 						   INTO l_status_code,l_delivery_detail_id
13149 						   FROM wsh_delivery_details_ob_grp_v wdd
13150 							WHERE wdd.container_name = l_lpn_name
13151 						   AND wdd.released_status = 'X';      -- For LPN reusability ER : 6845650
13152 
13153 			   /* Release 12(K): LPN Synchronization
13154 			   1. Uniqueness constraint on WDD.container_name is removed
13155 				  So it is not required to append characters to the LPNs
13156 				  to get a new containers name
13157 			   2. Replace API call to wsh_container_grp.update_container
13158 				  with new API call WSH_WMS_LPN_GRP.Create_Update_Containers
13159 			   */
13160 
13161 			   IF l_status_code = 'C' THEN
13162 
13163 				  l_wsh_dd_upd_rec.delivery_detail_id := l_delivery_detail_id;
13164 				  l_wsh_dd_upd_rec.lpn_id := pick_to_lpn_rec.lpn_id;
13165 
13166 				  wsh_update_tbl(1) := l_wsh_dd_upd_rec;
13167 
13168 				  l_IN_rec.caller      := 'WMS';
13169 				  l_IN_rec.action_code := 'UPDATE_NULL';
13170 
13171 				  WSH_WMS_LPN_GRP.Create_Update_Containers (
13172 					  p_api_version     => 1.0
13173 					, p_init_msg_list   => fnd_api.g_false
13174 					, p_commit          => fnd_api.g_false
13175 					, x_return_status   => x_return_status
13176 					, x_msg_count       => x_msg_count
13177 					, x_msg_data        => x_msg_data
13178 					, p_detail_info_tab => wsh_update_tbl
13179 					, p_IN_rec          => l_IN_rec
13180 					, x_OUT_rec         => l_OUT_rec );
13181 
13182 				 IF x_return_status = fnd_api.g_ret_sts_unexp_error
13183 				OR x_return_status = fnd_api.g_ret_sts_error THEN
13184 				RAISE fnd_api.g_exc_error;
13185 				 END IF;
13186 			   ELSE
13187 							 fnd_message.set_name('WMS','WMS_INVALID_PACK_DELIVERY');
13188 				 fnd_msg_pub.ADD;
13189 						 RAISE fnd_api.g_exc_error;
13190 			   END IF;
13191 		   END;
13192 			 CLOSE c_wdd_exists;
13193 			  END IF;
13194 	 END IF;
13195 		 --6168477 End
13196 	  END IF;
13197 
13198 
13199 	  /******** check for cartonized task***************/
13200 	  IF (p_container_item is not NULL) THEN
13201 		 IF (l_debug = 1) THEN
13202 				mydebug('validate_pick_to_lpn: p_container_item :'||p_container_item);
13203 				mydebug('validate_pick_to_lpn: p_container_item_id :'||p_container_item_id);
13204 				mydebug('validate_pick_to_lpn: p_suggested_container_item :'||p_suggested_container_item);
13205 				mydebug('validate_pick_to_lpn: p_suggested_container_item_id :'||p_suggested_container_item_id);
13206 				mydebug('validate_pick_to_lpn: p_pick_to_lpn--new LPN:'||p_pick_to_lpn);
13207 				mydebug('validate_pick_to_lpn: p_suggested_carton_name--old LPN:'||p_suggested_carton_name);
13208 				mydebug('validate_pick_to_lpn: p_suggested_tolpn_id--old LPN_id:'||p_suggested_tolpn_id);
13209 		 END IF;
13210 
13211 		 IF (p_pick_to_lpn <> p_suggested_carton_name) THEN
13212 				IF (l_debug = 1) THEN
13213 					mydebug('tolpn changed from:'||p_suggested_carton_name||' to:'||p_pick_to_lpn);
13214 				END IF;
13215 				BEGIN
13216 					 SELECT nvl(inventory_item_id, -999)
13217 					   INTO l_container_item_id
13218 					  FROM  wms_license_plate_numbers
13219 					  WHERE license_plate_number = p_pick_to_lpn
13220 						AND organization_id = p_organization_id
13221 						AND lpn_context IN (wms_container_pub.lpn_context_packing, wms_container_pub.LPN_CONTEXT_PREGENERATED);
13222 				EXCEPTION
13223 					  WHEN no_data_found THEN
13224 					  -- error out and assuming it should exist
13225 					  -- tolpn doesn't existing or if existing, but has wrong context
13226 					   fnd_message.set_name('WMS', 'WMS_LPN_NOT_FOUND');
13227 					   fnd_msg_pub.ADD;
13228 					   RAISE fnd_api.g_exc_error;
13229 				END;
13230 				IF (l_debug = 1) THEN
13231 					  mydebug('l_container_item_id:'||l_container_item_id);
13232 				END IF;
13233 				IF (l_container_item_id = -999) THEN  --lpn does not use any container
13234 					  IF (l_debug = 1) THEN
13235 						   mydebug('LPN does not use any container');
13236 					  END IF;
13237 					  fnd_message.set_name('WMS', 'WMS_LPN_NOT_LINKTO_CONT');
13238 					  fnd_message.set_token('LPN', p_pick_to_lpn );
13239 					  fnd_msg_pub.ADD;
13240 					  RAISE fnd_api.g_exc_error;
13241 				ELSIF (l_container_item_id <> p_container_item_id) THEN
13242 					 IF (l_debug = 1) THEN
13243 						   mydebug('The container, with which LPN associated, is different from confirmed container');
13244 					 END IF;
13245 					 BEGIN
13246 						  SELECT CONCATENATED_SEGMENTS
13247 							INTO  l_concatenated_segments
13248 							FROM  MTL_SYSTEM_ITEMS_KFV
13249 						   WHERE  inventory_item_id = l_container_item_id
13250 							 AND  organization_id = p_organization_id;
13251 					 EXCEPTION
13252 						  WHEN OTHERS THEN
13253 							  l_concatenated_segments := '';
13254 					 END;
13255 					 fnd_message.set_name('WMS', 'WMS_LPN_ASSOC_WITH_CONT');
13256 					 fnd_message.set_token('LPN', p_pick_to_lpn );
13257 					 fnd_message.set_token('CONTAINER',  l_concatenated_segments);
13258 					 fnd_msg_pub.ADD;
13259 					 RAISE fnd_api.g_exc_error;
13260 				END IF;
13261 		  ELSE
13262 			 IF (l_debug = 1) THEN
13263 			   mydebug('tolpn is not changed.');
13264 			 END IF;
13265 			 IF (p_container_item = p_suggested_container_item) THEN
13266 				  IF (l_debug = 1) THEN
13267 					  mydebug('Container is not changed. tolpn is not change, do nothing');
13268 				  END IF;
13269 			 ELSE
13270 				  IF (l_debug = 1) THEN
13271 					  mydebug('Container changed. error out');
13272 				  END IF;
13273 				  fnd_message.set_name('WMS', 'WMS_LPN_ASSOC_WITH_CONT');
13274 				  fnd_message.set_token('LPN', p_pick_to_lpn );
13275 				  fnd_message.set_token('CONTAINER', p_suggested_container_item );
13276 				  fnd_msg_pub.ADD;
13277 				  RAISE fnd_api.g_exc_error;
13278 			 END IF;
13279 		 END IF;
13280 	END IF;
13281 
13282 	 /********* end of checking for cartonized task ********/
13283 
13284 	/* check for cost group commingle   */
13285 
13286 	 IF p_serial_control = 'N' THEN
13287 
13288 		 IF p_lot_control = 'Y' THEN
13289 			l_lot_control := 2;
13290 		 ELSE l_lot_control := 1;
13291 		 END IF;
13292 
13293 		 IF p_revision_control = 'Y' THEN
13294 			l_revision_control := 2;
13295 		 ELSE l_revision_control := 1;
13296 		 END IF;
13297 
13298 
13299 		 WHILE  ( n <> 0)
13300 		 LOOP
13301 		   IF p_lot_control = 'Y' THEN
13302 			 n := INSTR(p_confirmed_lots,l_delimiter,m,1);
13303 			 IF n = 0 THEN -- Last part OF the string
13304 				 l_lot_number :=
13305 				   substr(p_confirmed_lots,m,length(p_confirmed_lots));
13306 			 ELSE
13307 				 l_lot_number :=  substr(p_confirmed_lots,m,n-m) ;
13308 						  -- start at M get m-n chrs
13309 				 m := n+1;
13310 			 END IF;
13311 			 mydebug ('l_lot_number:' || l_lot_number);
13312 		   ELSE
13313 			 n := 0;
13314 			 mydebug ('not lot controlled');
13315 		   END IF;
13316 
13317 		   validate_loaded_lpn_cg(
13318 				  p_organization_id      => p_organization_id,
13319 				  p_inventory_item_id    => p_inventory_item_id,
13320 				  p_subinventory_code    => p_confirmed_sub,
13321 				  p_locator_id           => p_confirmed_loc_id,
13322 				  p_revision             => p_revision,
13323 				  p_lot_number           => l_lot_number,
13324 				  p_lpn_id               => p_from_lpn_id,
13325 				  p_transfer_lpn_id      => x_pick_to_lpn_id,
13326 				  p_lot_control          => l_lot_control, --IN  NUMBER,
13327 				  p_revision_control     => l_revision_control, --IN  NUMBER,
13328 				  x_commingle_exist      => l_commingle_exist,
13329 				  x_return_status        => x_return_status,
13330 				  p_trx_type_id          => p_trx_type_id,
13331 				  p_trx_action_id        => p_trx_action_id);
13332 
13333 		  IF x_return_status = fnd_api.g_ret_sts_unexp_error
13334 			 OR x_return_status = fnd_api.g_ret_sts_error THEN
13335 			 RAISE fnd_api.g_exc_error;
13336 		  END IF;
13337 
13338 		  IF l_commingle_exist = 'Y' THEN
13339 				  IF (l_debug = 1) THEN
13340 					  mydebug('Cost group commigle exist.');
13341 				  END IF;
13342 				  fnd_message.set_name('WMS', 'WMS_CG_COMMINGLE');
13343 				  fnd_msg_pub.ADD;
13344 				  RAISE fnd_api.g_exc_error;
13345 		  ELSE
13346 				  IF (l_debug = 1) THEN
13347 					  mydebug('passed cost group commigle check.');
13348 				  END IF;
13349 		  END IF;
13350 	   END LOOP;
13351 
13352 	END IF;
13353 	/*end of check for cost group commingle   */
13354 
13355 	 /**********patchset J bulk picking                **********************/
13356 	  -- move the following query up to here, so that we can query the parent line id to see if
13357 	  -- it is bulk picking or not   -----
13358 	  /***********************************************************************/
13359 	  IF (l_debug = 1) THEN
13360 			mydebug('validate_pick_to_lpn: patchset J bulk picking started');
13361 	  END IF;
13362 
13363 	  SELECT mmtt.transfer_subinventory
13364 	, mmtt.transfer_to_location
13365 	, mmtt.inventory_item_id
13366 	, mmtt.operation_plan_id
13367 	, nvl(mmtt.parent_line_id,-1)
13368 	, mmtt.transaction_header_id
13369 	  INTO l_xfr_sub
13370 	, l_xfr_to_location
13371 	, l_item_id
13372 	, l_operation_plan_id
13373 	, l_parent_line_id
13374 	, l_transaction_header_id
13375 	  FROM mtl_material_transactions_temp mmtt
13376 	  WHERE mmtt.transaction_temp_id = p_temp_id;
13377 
13378 	  IF (l_debug = 1) THEN
13379 		   mydebug('validate_pick_to_lpn: parent line id:'||l_parent_line_id);
13380 	  END IF;
13381 
13382 
13383 	  IF l_parent_line_id = p_temp_id THEN -- bulk picking task
13384 					   -- check to see if this is for multiple pick of the same task or not
13385 					   -- If yes, no problem, otherwise raise error
13386 					   -- program can come to here which means the LPN is not pregenerated LPN
13387 					   BEGIN
13388 		  select 'N'
13389 		  into l_multiple_pick
13390 		  from dual
13391 		  where exists (select 1
13392 			from mtl_material_transactions_temp
13393 			where transfer_lpn_id = pick_to_lpn_rec.lpn_id
13394 	   and transaction_header_id <>l_transaction_header_id);
13395 
13396 		  fnd_message.set_name('WMS', 'WMS_INVLD_PICKTO_LPN_NOT_NEW'); -- new message
13397 		  fnd_msg_pub.ADD;
13398 		  RAISE fnd_api.g_exc_error;
13399 		  EXCEPTION
13400 	   WHEN NO_DATA_FOUND THEN RETURN; -- this lpn is fine and no need to do all
13401 		   -- the following checks
13402 		 END;
13403 					ELSE -- regular task but maybe the lpn has contains bulk tasks
13404 					   BEGIN
13405 							   select 'Y'
13406 							   into l_bulk_task_exist
13407 							   from dual
13408 							   where exists (select 1
13409 											 from mtl_material_transactions_temp
13410 											 where transfer_lpn_id = pick_to_lpn_rec.lpn_id
13411 											 and transaction_temp_id = parent_line_id  -- bulk task
13412 											 );
13413 							   fnd_message.set_name('WMS', 'WMS_INVLD_PICKTO_LPN_BULK');
13414 							   fnd_msg_pub.ADD;
13415 							   RAISE fnd_api.g_exc_error;
13416 							   EXCEPTION
13417 								   WHEN NO_DATA_FOUND THEN null;
13418 					   END;
13419 	  END IF;
13420 
13421 	  IF (l_debug = 1) THEN
13422 		  mydebug('validate_pick_to_lpn: patchset J bulk picking ended');
13423 	  END IF;
13424 
13425 	  /******** end of patchset J bulk picking  ***************************/
13426 
13427 
13428 	  --
13429 	  -- *********************Start of bug fix 2078002,2095080 ********************
13430 	  -- Check if the task that is about to pack into the LPN has the same
13431 	  -- move order type as the tasks already packed into the same LPN
13432 	  --
13433 	  SELECT mtrh.move_order_type
13434 		   , mmtt.transaction_type_id
13435 		   , mmtt.wip_entity_type
13436 		INTO l_mmtt_mo_type
13437 		   , l_mmtt_txn_type_id
13438 		   , l_mmtt_wip_entity_type
13439 		FROM mtl_txn_request_headers         mtrh
13440 		   , mtl_txn_request_lines           mtrl
13441 		   , mtl_material_transactions_temp  mmtt
13442 	   WHERE mtrh.header_id           = mtrl.header_id
13443 		 AND mtrl.line_id             = mmtt.move_order_line_id
13444 		 AND mmtt.transaction_temp_id = p_temp_id;
13445 
13446 	  BEGIN
13447 		 SELECT mtrh.move_order_type
13448 			  , mmtt.wip_entity_type
13449 		   INTO l_mo_type_in_lpn
13450 			  , l_wip_entity_type_in_lpn
13451 		   FROM mtl_txn_request_headers         mtrh
13452 			  , mtl_txn_request_lines           mtrl
13453 			  , mtl_material_transactions_temp  mmtt
13454 		  WHERE mtrh.header_id       = mtrl.header_id
13455 			AND mtrl.line_id         = mmtt.move_order_line_id
13456 			AND mmtt.transfer_lpn_id = pick_to_lpn_rec.lpn_id
13457 			AND ROWNUM < 2;
13458 	  EXCEPTION
13459 			 WHEN NO_DATA_FOUND THEN
13460 				 l_mo_type_in_lpn := null;
13461 	  END;
13462 
13463 	  IF (l_mo_type_in_lpn is not null ) THEN
13464 		 IF l_mo_type_in_lpn <> l_mmtt_mo_type THEN
13465 			IF (l_debug = 1) THEN
13466 			   mydebug('validate_pick_to_lpn: Picked LPN and current MMTT have different MO type.');
13467 			   mydebug('  p_temp_id => ' || p_temp_id);
13468 			   mydebug('  lpn_id => ' || pick_to_lpn_rec.lpn_id);
13469 			   mydebug('  l_mmtt_mo_type => ' || l_mmtt_mo_type);
13470 			   mydebug('  l_mo_type_in_lpn => ' || l_mo_type_in_lpn);
13471 			END IF;
13472 
13473 			fnd_message.set_name('WMS', 'WMS_LPN_FB_COMINGLE');
13474 			fnd_msg_pub.ADD;
13475 			RAISE fnd_api.g_exc_error;
13476 		 ELSIF l_mmtt_txn_type_id = 35
13477 			   OR l_mmtt_txn_type_id = 51 THEN -- Mfg pick
13478 			   IF l_mmtt_wip_entity_type <> l_wip_entity_type_in_lpn THEN
13479 				  IF (l_debug = 1) THEN
13480 					 mydebug('validate_pick_to_lpn: This is a manufacturing component pick.');
13481 					 mydebug('WIP entity type IS NOT the same AS that OF the old mmtt RECORD');
13482 				  END IF;
13483 				  fnd_message.set_name('WMS', 'WMS_INVLD_PICKTO_LPN_MFG_MODE');
13484 				  fnd_msg_pub.ADD;
13485 				  RAISE fnd_api.g_exc_error;
13486 			   END IF;
13487 		 END IF;
13488 	  END IF;
13489 	  -- *********************End of bug fix 2078002,2095080 ********************
13490 
13491 	  --
13492 	  -- Bug 2355453: Check to see if the LPN is already going to some other lpn
13493 	  -- controlled sub. In that case, do not allow material to be picked into
13494 	  -- this LPN
13495 	  --
13496 	  IF (l_debug = 1) THEN
13497 		 mydebug('validate_pick_to_lpn: Check to see if LPN is already going to some other sub/loc');
13498 	  END IF;
13499 
13500 	  /* moved up already
13501 					  SELECT mmtt.transfer_subinventory
13502 		   , mmtt.transfer_to_location
13503 		   , mmtt.inventory_item_id
13504 		   , mmtt.operation_plan_id
13505 		INTO l_xfr_sub
13506 		   , l_xfr_to_location
13507 		   , l_item_id
13508 		   , l_operation_plan_id
13509 		FROM mtl_material_transactions_temp mmtt
13510 	   WHERE mmtt.transaction_temp_id = p_temp_id; */
13511 
13512 	  l_lpn_controlled_flag  := wms_globals.g_non_lpn_controlled_sub;
13513 
13514 	  IF l_xfr_sub IS NOT NULL THEN
13515 		 SELECT lpn_controlled_flag
13516 		   INTO l_lpn_controlled_flag
13517 		   FROM mtl_secondary_inventories
13518 		  WHERE organization_id = p_organization_id
13519 			AND secondary_inventory_name = l_xfr_sub;
13520 	  END IF;
13521 
13522 	  IF l_xfr_sub IS NOT NULL
13523 		 AND l_lpn_controlled_flag = wms_globals.g_lpn_controlled_sub THEN
13524 		 IF (l_debug = 1) THEN
13525 			mydebug('validate_pick_to_lpn: Transfer Sub is LPN Controlled');
13526 		 END IF;
13527 
13528 		 --
13529 		 -- Ensure that all remaining picks on the LPN are also for the same sub
13530 		 --
13531 		 l_count  := 0;
13532 
13533 		 BEGIN
13534 			SELECT COUNT(*)
13535 			  INTO l_count
13536 			  FROM mtl_material_transactions_temp mmtt
13537 			 WHERE mmtt.transaction_temp_id <> p_temp_id
13538 			   AND mmtt.transfer_lpn_id = pick_to_lpn_rec.lpn_id
13539 			   AND ( NVL(mmtt.transfer_subinventory, 0) <> l_xfr_sub
13540 					 OR
13541 					 NVL(mmtt.transfer_to_location, 0)  <> l_xfr_to_location
13542 				   );
13543 		 EXCEPTION
13544 			WHEN NO_DATA_FOUND THEN
13545 				 l_count  := 0;
13546 		 END;
13547 
13548 		 IF l_count > 0 THEN
13549 			IF (l_debug = 1) THEN
13550 			   mydebug('validate_pick_to_lpn: Drop LPN is going to an LPN controlled sub');
13551 			   mydebug('validate_pick_to_lpn: Cannot add picks not going to the same sub');
13552 			END IF;
13553 
13554 			fnd_message.set_name('WMS', 'WMS_INVLD_PICKTO_LPN_SUBINV');
13555 			fnd_msg_pub.ADD;
13556 			RAISE fnd_api.g_exc_error;
13557 		 END IF;
13558 	  ELSE
13559 		 --
13560 		 -- Current temp ID has a NULL xfer sub (issue txn)
13561 		 -- or the xfer sub is non LPN-controlled.
13562 		 -- Ensure that no other picks on the same LPN are to
13563 		 -- LPN controlled subs
13564 		 --
13565 
13566 		 IF (l_debug = 1) THEN
13567 			mydebug('validate_pick_to_lpn: Transfer Sub is non LPN Controlled or null.');
13568 		 END IF;
13569 
13570 		 l_count  := 0;
13571 		 BEGIN
13572 			SELECT 1
13573 			  INTO l_count
13574 			  FROM DUAL
13575 			 WHERE EXISTS
13576 				 ( SELECT 'x'
13577 					 FROM mtl_material_transactions_temp  mmtt
13578 						, mtl_secondary_inventories       msi
13579 					WHERE mmtt.transaction_temp_id    <> p_temp_id
13580 					  AND mmtt.transfer_lpn_id         = pick_to_lpn_rec.lpn_id
13581 					  AND msi.organization_id          = p_organization_id
13582 					  AND msi.secondary_inventory_name = mmtt.transfer_subinventory
13583 					  AND msi.lpn_controlled_flag      = wms_globals.g_lpn_controlled_sub
13584 				 );
13585 		 EXCEPTION
13586 			WHEN NO_DATA_FOUND THEN
13587 				 l_count  := 0;
13588 		 END;
13589 
13590 		 IF l_count > 0 THEN
13591 			IF (l_debug = 1) THEN
13592 			   mydebug('validate_pick_to_lpn: Drop LPN has pick(s) for an LPN-controlled sub');
13593 			END IF;
13594 
13595 			fnd_message.set_name('WMS', 'WMS_INVLD_PICKTO_LPN_SUBINV');
13596 			fnd_msg_pub.ADD;
13597 			RAISE fnd_api.g_exc_error;
13598 		 END IF;
13599 	  END IF;
13600 
13601 	--Disallow mixed fulfillment base into same txfer LPN for multiUOM
13602 	l_fb_comingle := 'N';
13603 	BEGIN
13604 
13605 	   SELECT 'Y'
13606 		 INTO l_fb_comingle
13607 		 FROM mtl_material_transactions_temp mmtt
13608 		WHERE mmtt.transaction_temp_id = p_temp_id
13609 		  AND mmtt.organization_id = p_organization_id
13610 		  AND EXISTS(SELECT 1
13611 					   FROM mtl_material_transactions_temp mmtt1
13612 					  WHERE mmtt1.transaction_temp_id <> p_temp_id
13613 						AND mmtt1.organization_id = p_organization_id
13614 						AND mmtt1.inventory_item_id = p_inventory_item_id
13615 						AND (mmtt1.transfer_lpn_id = pick_to_lpn_rec.lpn_id
13616 						  OR mmtt1.content_lpn_id = pick_to_lpn_rec.lpn_id)
13617 						AND NVL(mmtt.fulfillment_base, 'P') <> NVL(mmtt1.fulfillment_base,'P'));
13618 	EXCEPTION
13619 		WHEN NO_DATA_FOUND THEN
13620 		  l_fb_comingle := 'N';
13621 	END;
13622 
13623 	  IF l_fb_comingle <> 'N' THEN
13624 		 IF (l_debug = 1) THEN
13625 			mydebug('validate_pick_to_lpn: Txfer LPN is associated with material FOR a different fulfillment base');
13626 		 END IF;
13627 
13628 		 fnd_message.set_name('WMS', 'WMS_LPN_FB_COMINGLE');
13629 		 fnd_msg_pub.ADD;
13630 		 RAISE fnd_api.g_exc_error;
13631 	  END IF;
13632 
13633 
13634 	  --
13635 	  IF (l_debug = 1) THEN
13636 		 mydebug('validate_pick_to_lpn: Check to see if LPN is associated with material' ||
13637 				 ' FOR a different operation plan');
13638 	  END IF;
13639 
13640 	  l_count := 0;
13641 	  BEGIN
13642 		SELECT COUNT(1)
13643 		  INTO l_count
13644 		  FROM mtl_material_transactions_temp mmtt
13645 		 WHERE mmtt.transaction_temp_id <> p_temp_id
13646 		   AND mmtt.transfer_lpn_id      = pick_to_lpn_rec.lpn_id
13647 		   AND mmtt.operation_plan_id   <> l_operation_plan_id;
13648 	  EXCEPTION
13649 		WHEN NO_DATA_FOUND THEN
13650 		  l_count := 0;
13651 	  END;
13652 
13653 	  IF l_count > 0 THEN
13654 		 IF (l_debug = 1) THEN
13655 			mydebug('validate_pick_to_lpn: Drop LPN is associated with material FOR a different operation plan');
13656 		 END IF;
13657 
13658 		 fnd_message.set_name('WMS', 'WMS_INVLD_PICKTO_LPN_OPER_PLAN');
13659 		 fnd_msg_pub.ADD;
13660 		 RAISE fnd_api.g_exc_error;
13661 	  END IF;
13662 
13663 
13664 	  --
13665 	  -- No further checks required if LPN contains manufacturing picks
13666 	  -- The checks after this are related to delivery ID and PJM orgs
13667 	  --
13668 	  -- actually no further checks if it is replenishment or others except pick wave
13669 	  IF l_mmtt_mo_type <> 3 THEN
13670 		 RETURN;
13671 	  END IF;
13672 
13673 	  -- Now check if the picked LPN
13674 	  -- belongs to delivery which is different from current delivery
13675 	  --
13676 	  OPEN current_delivery_cursor;
13677 
13678 	  LOOP
13679 		FETCH current_delivery_cursor INTO l_current_mmtt_delivery_id;
13680 		EXIT WHEN l_current_mmtt_delivery_id IS NOT NULL
13681 			  OR current_delivery_cursor%NOTFOUND;
13682 	  END LOOP;
13683 
13684 	  CLOSE current_delivery_cursor;
13685 
13686 	  IF (l_debug = 1) THEN
13687 		mydebug('validate_pick_to_lpn: l_current_mmtt_delivery_id:' || l_current_mmtt_delivery_id);
13688 	  END IF;
13689 
13690 	  --
13691 	  -- If the current MMTT is not associated with a delivery yet
13692 	  -- then no further checking required, return success
13693 	  --
13694 	  IF l_current_mmtt_delivery_id IS NULL THEN
13695 		IF (l_debug = 1) THEN
13696 		  mydebug('validate_pick_to_lpn: Current MMTT is not associated with a delivery');
13697 		END IF;
13698 
13699 		OPEN current_carton_grouping_cursor;
13700 		FETCH current_carton_grouping_cursor INTO l_current_carton_grouping_id;
13701 		CLOSE current_carton_grouping_cursor;
13702 
13703 		IF (l_current_carton_grouping_id = -999) THEN
13704 		  IF (l_debug = 1) THEN
13705 			mydebug('validate_pick_to_lpn: can NOT find move order line for current task');
13706 		  END IF;
13707 
13708 		  fnd_message.set_name('WMS', 'WMS_NO_MOL');
13709 		  fnd_msg_pub.ADD;
13710 		  RAISE fnd_api.g_exc_error;
13711 		END IF;
13712 
13713 		IF l_current_carton_grouping_id IS NOT NULL THEN -- found carton_grouping_id
13714 		  OPEN others_carton_grouping_cursor(pick_to_lpn_rec.lpn_id);
13715 
13716 		  LOOP
13717 			FETCH others_carton_grouping_cursor INTO l_carton_grouping_id;
13718 			EXIT WHEN l_current_carton_grouping_id = NVL(l_carton_grouping_id, 0)
13719 				  OR others_carton_grouping_cursor%NOTFOUND;
13720 		  END LOOP;
13721 
13722 		  CLOSE others_carton_grouping_cursor;
13723 
13724 		  IF l_carton_grouping_id = -999 THEN -- it is the first task in the lpn
13725 			mydebug('validate_pick_to_lpn: This is the first task for the lpn ' ||
13726 					'and the task without delivery, so ok..');
13727 			RETURN;
13728 		  END IF;
13729 
13730 		  IF l_carton_grouping_id IS NOT NULL THEN
13731 			IF l_carton_grouping_id = l_current_carton_grouping_id THEN --the same carton_grouping_id
13732 			  IF (l_debug = 1) THEN
13733 				mydebug('validate_pick_to_lpn: found the task in lpn which has ' ||
13734 						'the same carton_grouping_id as current task');
13735 			  END IF;
13736 
13737 			  OPEN others_in_mmtt_delivery_cursor(pick_to_lpn_rec.lpn_id);
13738 			  l_pick_to_lpn_delivery_id  := -999;
13739 
13740 			  LOOP
13741 				FETCH others_in_mmtt_delivery_cursor INTO l_pick_to_lpn_delivery_id;
13742 				EXIT WHEN l_pick_to_lpn_delivery_id IS NULL
13743 					  OR others_in_mmtt_delivery_cursor%NOTFOUND;
13744 			  END LOOP;
13745 
13746 			  CLOSE others_in_mmtt_delivery_cursor;
13747 
13748 			  IF l_pick_to_lpn_delivery_id = -999 THEN --there is mol, but no wdd or wda, raise error
13749 				IF (l_debug = 1) THEN
13750 				  mydebug('validate_pick_to_lpn: can NOT find either wdd or wda for tasks in the lpn');
13751 				END IF;
13752 
13753 				fnd_message.set_name('WMS', 'WMS_NO_WDD_WDA');
13754 				fnd_msg_pub.ADD;
13755 				RAISE fnd_api.g_exc_error;
13756 			  END IF;
13757 
13758 			  IF l_pick_to_lpn_delivery_id IS NULL THEN
13759 				IF (l_debug = 1) THEN
13760 				  mydebug('validate_pick_to_lpn: found a task which has ' ||
13761 						  'the same carton_grouping_id as current task, and also no delivery.');
13762 				END IF;
13763 
13764 				RETURN;
13765 			  ELSE
13766 				IF (l_debug = 1) THEN
13767 				  mydebug('validate_pick_to_lpn: other tasks in lpn have different deliveries');
13768 				END IF;
13769 
13770 				fnd_message.set_name('WMS', 'WMS_PICK_TO_LPN_DIFF_DELIV');
13771 				fnd_msg_pub.ADD;
13772 				RAISE fnd_api.g_exc_error;
13773 			  END IF;
13774 			ELSE -- they have different carton_grouping_id
13775 			  --{
13776 			  IF (l_debug = 1) THEN
13777 				mydebug('validate_pick_to_lpn: other tasks in lpn have different carton grouping id');
13778 			  END IF;
13779 			  --
13780 			  -- Start : R12 Bug 4454837, this change will be removed
13781 			  --
13782 			  -- uncommenting for bug#10062741
13783 			  BEGIN
13784 				--{
13785 				SELECT wdd.delivery_detail_id INTO  l_line_rows(1)
13786 				FROM wsh_delivery_details    wdd
13787 					, mtl_material_transactions_temp  mmtt
13788 				WHERE mmtt.transaction_temp_id = p_temp_id
13789 				AND wdd.move_order_line_id = mmtt.move_order_line_id
13790 				AND wdd.organization_id    = mmtt.organization_id;
13791 				--
13792 				SELECT wdd.delivery_detail_id  INTO  l_line_rows(2)
13793 				FROM wsh_delivery_details  wdd
13794 					 , mtl_material_transactions_temp  mmtt
13795 				WHERE mmtt.transfer_lpn_id   = pick_to_lpn_rec.lpn_id
13796 				AND wdd.move_order_line_id = mmtt.move_order_line_id
13797 				AND wdd.organization_id    = mmtt.organization_id
13798 				AND rownum<2;
13799 				--
13800 				IF (l_debug = 1) THEN
13801 				  mydebug('validate_pick_to_lpn: Before calling WSH_DELIVERY_DETAILS_GRP.Get_Carton_Grouping() to decide if we can load into this LPN');
13802 				  mydebug('Parameters : delivery_detail_id(1):'|| l_line_rows(1) ||' , delivery_detail_id(2) :'||l_line_rows(2));
13803 				END IF;
13804 				--
13805 				-- call to the shipping API.
13806 				--
13807 				WSH_DELIVERY_DETAILS_GRP.Get_Carton_Grouping(
13808 						   p_line_rows      => l_line_rows,
13809 						   x_grouping_rows  => l_grouping_rows,
13810 						   x_return_status  => l_return_status);
13811 				--
13812 				IF (l_return_status = FND_API.G_RET_STS_SUCCESS
13813 				   AND l_grouping_rows (1) = l_grouping_rows(2) )  THEN
13814 					 l_same_carton_grouping := TRUE;
13815 				ELSE
13816 					 l_same_carton_grouping := FALSE;
13817 				END IF;
13818 				--
13819 			  EXCEPTION
13820 				WHEN NO_DATA_FOUND THEN
13821 				 IF (l_debug = 1) THEN
13822 					mydebug('No Data found Exception raised when matching delivery grouping attributes');
13823 					l_same_carton_grouping := FALSE;
13824 				 END IF;
13825 				WHEN OTHERS THEN
13826 				 IF (l_debug = 1) THEN
13827 				   mydebug('Other Exception raised when matching for delivery grouping attributes');
13828 				   l_same_carton_grouping := FALSE;
13829 				 END IF;
13830 				--}
13831 			  END;
13832 			  --
13833 			  IF (l_same_carton_grouping = FALSE) then
13834 			   fnd_message.set_name('WMS', 'WMS_DIFF_CARTON_GROUP');
13835 			   fnd_msg_pub.ADD;
13836 			   RAISE fnd_api.g_exc_error;
13837 			  END IF;
13838 			  --
13839 			  -- End : R12 bug 4454837.
13840 			  --} --end bug#10062741
13841 			END IF;
13842 		  ELSE -- some of carton_grouping_id is null
13843 			IF (l_debug = 1) THEN
13844 			  mydebug('validate_pick_to_lpn: some of tasks in lpn have NULL carton_grouping_id');
13845 			END IF;
13846 			fnd_message.set_name('WMS', 'WMS_CARTON_GROUP_NULL');
13847 			fnd_msg_pub.ADD;
13848 			RAISE fnd_api.g_exc_error;
13849 		  END IF;
13850 		ELSE --carton_grouping_id is null
13851 		  IF (l_debug = 1) THEN
13852 			mydebug('validate_pick_to_lpn: carton_grouping_id of current task is null');
13853 		  END IF;
13854 		   --bug3481923 only fail if it is not requisition on repl mo
13855 			if (l_mmtt_mo_type not in(1,2)) then
13856 			   fnd_message.set_name('WMS', 'WMS_CARTON_GROUP_NULL');
13857 			   fnd_msg_pub.ADD;
13858 			   RAISE fnd_api.g_exc_error;
13859 			end if;
13860 		END IF;
13861 	  END IF;
13862 
13863 	  -- Check if picked LPN has been picked_to in previous tasks, tasks that
13864 	  -- are still IN MMTT and shipping tables do not have the drop lpn yet
13865 
13866 	  OPEN others_in_mmtt_delivery_cursor(pick_to_lpn_rec.lpn_id);
13867 
13868 	  LOOP
13869 		FETCH others_in_mmtt_delivery_cursor INTO l_pick_to_lpn_delivery_id2;
13870 		EXIT WHEN l_pick_to_lpn_delivery_id2 IS NOT NULL
13871 			  OR others_in_mmtt_delivery_cursor%NOTFOUND;
13872 	  END LOOP;
13873 
13874 	  CLOSE others_in_mmtt_delivery_cursor;
13875 
13876 	  IF (l_debug = 1) THEN
13877 		mydebug('validate_pick_to_lpn: l_pick_to_lpn_delivery_id2' || l_pick_to_lpn_delivery_id2);
13878 	  END IF;
13879 
13880 	  mydebug('l_pick_to_lpn_delivery_id2 : '||l_pick_to_lpn_delivery_id2);
13881 	  mydebug('l_current_mmtt_delivery_id : '||l_current_mmtt_delivery_id);
13882 
13883 	  IF (l_pick_to_lpn_delivery_id2 IS NOT NULL) AND (l_pick_to_lpn_delivery_id2 <> -999)  THEN
13884 		IF l_pick_to_lpn_delivery_id2 <> l_current_mmtt_delivery_id THEN
13885 		  IF (l_debug = 1) THEN
13886 			mydebug('validate_pick_to_lpn: Picked LPN and current MMTT go to different deliveries.');
13887 		  END IF;
13888 
13889 		  fnd_message.set_name('WMS', 'WMS_PICK_TO_LPN_DIFF_DELIV');
13890 		  fnd_msg_pub.ADD;
13891 		  RAISE fnd_api.g_exc_error;
13892 		END IF;
13893 	  ELSIF l_pick_to_lpn_delivery_id2 IS NULL THEN
13894 		IF (l_debug = 1) THEN
13895 		  mydebug('validate_pick_to_lpn: Picked LPN does not have deliveries.');
13896 		END IF;
13897 
13898 		IF l_current_mmtt_delivery_id IS NOT NULL THEN
13899 		  IF (l_debug = 1) THEN
13900 			mydebug('validate_pick_to_lpn: Current task has delivery.');
13901 			mydebug('validate_pick_to_lpn: Picked LPN does not have delivery and current task has delivery.');
13902 		  END IF;
13903 
13904 		  fnd_message.set_name('WMS', 'WMS_PICK_TO_LPN_DIFF_DELIV');
13905 		  fnd_msg_pub.ADD;
13906 		  RAISE fnd_api.g_exc_error;
13907 		END IF;
13908 	  ELSIF l_pick_to_lpn_delivery_id2 = -999 THEN
13909 		IF (l_debug = 1) THEN
13910 		  mydebug('validate_pick_to_lpn: LPN does not contain other tasks. This is the first task, so ok.');
13911 		END IF;
13912 	  END IF;
13913 
13914 	  IF pick_to_lpn_rec.outermost_lpn_id IS NOT NULL THEN
13915 		-- We need to check delivery for outermost lpn or drill down if needed
13916 		l_outermost_lpn_id  := pick_to_lpn_rec.outermost_lpn_id;
13917 	  ELSE
13918 		-- We need to check delivery for pick_to_lpn or drill down if needed
13919 		l_outermost_lpn_id  := pick_to_lpn_rec.lpn_id;
13920 	  END IF;
13921 
13922 	  --
13923 	  -- Find the outermost LPN's delivery ID
13924 	  --
13925 	  OPEN drop_delivery_cursor(l_outermost_lpn_id);
13926 	  FETCH drop_delivery_cursor INTO l_pick_to_lpn_delivery_id;
13927 	  CLOSE drop_delivery_cursor;
13928 
13929 	  mydebug('l_pick_to_lpn_delivery_id : '||l_pick_to_lpn_delivery_id);
13930 	  mydebug('l_current_mmtt_delivery_id : '||l_current_mmtt_delivery_id);
13931 
13932 	  IF l_pick_to_lpn_delivery_id IS NOT NULL THEN
13933 		IF l_pick_to_lpn_delivery_id <> l_current_mmtt_delivery_id THEN
13934 		  IF (l_debug = 1) THEN
13935 			mydebug('validate_pick_to_lpn: Picked LPN and current MMTT go to different deliveries.');
13936 		  END IF;
13937 
13938 		  fnd_message.set_name('WMS', 'WMS_PICK_TO_LPN_DIFF_DELIV');
13939 		  fnd_msg_pub.ADD;
13940 		  RAISE fnd_api.g_exc_error;
13941 			NULL;
13942 		ELSE
13943 		  --
13944 		  -- Picked LPN and current MMTT are on the same delivery
13945 		  -- return success
13946 		  --
13947 		  IF (l_debug = 1) THEN
13948 			mydebug('validate_pick_to_lpn: Picked LPN and current MMTT go to same delivery: ' ||
13949 					 l_pick_to_lpn_delivery_id);
13950 		  END IF;
13951 
13952 		  RETURN;
13953 		END IF;
13954 	  ELSE
13955 		IF (l_debug = 1) THEN
13956 		  mydebug('validate_pick_to_lpn: Drop LPN does not have a delivery ID, checking child LPNs');
13957 		END IF;
13958 
13959 		OPEN child_lpns_cursor(l_outermost_lpn_id);
13960 
13961 		LOOP
13962 		  FETCH child_lpns_cursor INTO child_lpns_rec;
13963 		  EXIT WHEN child_lpns_cursor%NOTFOUND;
13964 
13965 		  IF child_lpns_cursor%FOUND THEN
13966 			OPEN drop_delivery_cursor(child_lpns_rec.lpn_id);
13967 			FETCH drop_delivery_cursor INTO l_pick_to_lpn_delivery_id;
13968 			CLOSE drop_delivery_cursor;
13969 		  END IF;
13970 
13971 		  EXIT WHEN l_pick_to_lpn_delivery_id IS NOT NULL;
13972 		END LOOP;
13973 
13974 		CLOSE child_lpns_cursor;
13975 
13976 		--
13977 		-- If the child LPNs also don't have a delivery ID
13978 		-- then ok to deposit
13979 		--
13980 		IF l_pick_to_lpn_delivery_id IS NOT NULL THEN
13981 		  IF l_pick_to_lpn_delivery_id <> l_current_mmtt_delivery_id THEN
13982 			IF (l_debug = 1) THEN
13983 			  mydebug('validate_pick_to_lpn: LPNs are on diff deliveries.');
13984 			END IF;
13985 
13986 			fnd_message.set_name('WMS', 'WMS_PICK_TO_LPN_DIFF_DELIV');
13987 			fnd_msg_pub.ADD;
13988 			RAISE fnd_api.g_exc_error;
13989 		  ELSE
13990 			--
13991 			-- Child LPN has the  delivery as the current MMTT, return success
13992 			--
13993 			IF (l_debug = 1) THEN
13994 			  mydebug('validate_pick_to_lpn: A child LPN is on the same delivery ' ||
13995 					  'as that OF the CURRENT MMTT, return success.');
13996 			END IF;
13997 
13998 			RETURN;
13999 		  END IF;
14000 		ELSE
14001 		  --
14002 		  -- No child LPNs have a delivery ID yet
14003 		  -- return success
14004 		  --
14005 		  IF (l_debug = 1) THEN
14006 			mydebug('validate_pick_to_lpn: Child LPNs do not have a delivery ID either, return success.');
14007 		  END IF;
14008 
14009 		  RETURN;
14010 		END IF;
14011 	  END IF;
14012 
14013 	  --
14014 	  -- Fetch the Project/Task id associated with the LPN passed
14015 	  --
14016 	  -- PJM Integration:
14017 	  -- Check if the task that is about to pack into the LPN has the same
14018 	  -- transfer project_id and task_id as the lpn to which it is going to
14019 	  -- be loaded into.
14020 	  -- If yes, proceed, else return
14021 	  --
14022 	  IF (p_project_id IS NOT NULL) THEN
14023 		OPEN lpn_project_task_cursor( pick_to_lpn_rec.lpn_id);
14024 
14025 		LOOP
14026 		  FETCH lpn_project_task_cursor INTO lpn_pjm_prj_tsk_rec;
14027 		  EXIT WHEN lpn_project_task_cursor%NOTFOUND;
14028 		  OPEN mtl_project_task_cursor;
14029 
14030 		  LOOP
14031 			FETCH mtl_project_task_cursor INTO mtl_pjm_prj_tsk_rec;
14032 			EXIT WHEN mtl_project_task_cursor%NOTFOUND;
14033 			-- project and task both should be the same as
14034 			IF ((mtl_pjm_prj_tsk_rec.prj_id <> lpn_pjm_prj_tsk_rec.prj_id)
14035 				 OR (mtl_pjm_prj_tsk_rec.tsk_id <> lpn_pjm_prj_tsk_rec.tsk_id)) THEN
14036 			  RAISE fnd_api.g_exc_error;
14037 			END IF;
14038 		  END LOOP;
14039 
14040 		  CLOSE mtl_project_task_cursor;
14041 		END LOOP;
14042 
14043 		CLOSE lpn_project_task_cursor;
14044 	  END IF;
14045 	EXCEPTION
14046 	  WHEN fnd_api.g_exc_error THEN
14047 		x_return_status  := fnd_api.g_ret_sts_error;
14048 		--  Get message count and data
14049 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
14050 
14051 		IF (l_debug = 1) THEN
14052 		  mydebug('validate_pick_to_lpn: @' || x_msg_data || '@');
14053 		END IF;
14054 
14055 		IF others_in_mmtt_delivery_cursor%ISOPEN THEN
14056 		  CLOSE others_in_mmtt_delivery_cursor;
14057 		END IF;
14058 
14059 		IF child_lpns_cursor%ISOPEN THEN
14060 		  CLOSE child_lpns_cursor;
14061 		END IF;
14062 
14063 		IF current_delivery_cursor%ISOPEN THEN
14064 		  CLOSE current_delivery_cursor;
14065 		END IF;
14066 
14067 		IF drop_delivery_cursor%ISOPEN THEN
14068 		  CLOSE drop_delivery_cursor;
14069 		END IF;
14070 
14071 		IF lpn_project_task_cursor%ISOPEN THEN
14072 		  CLOSE lpn_project_task_cursor;
14073 		END IF;
14074 
14075 		IF mtl_project_task_cursor%ISOPEN THEN
14076 		  CLOSE mtl_project_task_cursor;
14077 		END IF;
14078 
14079 		IF current_carton_grouping_cursor%ISOPEN THEN
14080 		  CLOSE current_carton_grouping_cursor;
14081 		END IF;
14082 
14083 		IF others_carton_grouping_cursor%ISOPEN THEN
14084 		  CLOSE others_carton_grouping_cursor;
14085 		END IF;
14086 	  WHEN OTHERS THEN
14087 		x_return_status  := fnd_api.g_ret_sts_unexp_error;
14088 
14089 		IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
14090 		  fnd_msg_pub.add_exc_msg(g_pkg_name, l_api_name);
14091 		END IF;
14092 
14093 		--  Get message count and data
14094 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
14095 
14096 		IF others_in_mmtt_delivery_cursor%ISOPEN THEN
14097 		  CLOSE others_in_mmtt_delivery_cursor;
14098 		END IF;
14099 
14100 		IF child_lpns_cursor%ISOPEN THEN
14101 		  CLOSE child_lpns_cursor;
14102 		END IF;
14103 
14104 		IF current_delivery_cursor%ISOPEN THEN
14105 		  CLOSE current_delivery_cursor;
14106 		END IF;
14107 
14108 		IF drop_delivery_cursor%ISOPEN THEN
14109 		  CLOSE drop_delivery_cursor;
14110 		END IF;
14111 
14112 		IF lpn_project_task_cursor%ISOPEN THEN
14113 		  CLOSE lpn_project_task_cursor;
14114 		END IF;
14115 
14116 		IF mtl_project_task_cursor%ISOPEN THEN
14117 		  CLOSE mtl_project_task_cursor;
14118 		END IF;
14119 
14120 		IF current_carton_grouping_cursor%ISOPEN THEN
14121 		  CLOSE current_carton_grouping_cursor;
14122 		END IF;
14123 
14124 		IF others_carton_grouping_cursor%ISOPEN THEN
14125 		  CLOSE others_carton_grouping_cursor;
14126 		END IF;
14127 
14128 		IF (l_debug = 1) THEN
14129 		  mydebug('validate_pick_to_lpn: @' || x_msg_data || '@');
14130 		END IF;
14131   END validate_pick_to_lpn;
14132 
14133 
14134 PROCEDURE validate_sub_loc_status(
14135 	  p_wms_installed    IN            VARCHAR2
14136 	, p_temp_id          IN            NUMBER
14137 	, p_confirmed_sub    IN            VARCHAR2
14138 	, p_confirmed_loc_id IN            NUMBER
14139 	, x_return_status    OUT NOCOPY    VARCHAR2
14140 	, x_msg_count        OUT NOCOPY    NUMBER
14141 	, x_msg_data         OUT NOCOPY    VARCHAR2
14142 	, x_result           OUT NOCOPY    NUMBER
14143 	) IS
14144 	  l_transaction_type_id NUMBER;
14145 	  l_org_id              NUMBER;
14146 	  l_item_id             NUMBER;
14147 	  l_debug               NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
14148 	BEGIN
14149 	  IF (l_debug = 1) THEN
14150 		mydebug('validate_sub_loc_status: validate_sub_loc_status begins');
14151 	  END IF;
14152 
14153 	  x_return_status  := fnd_api.g_ret_sts_success;
14154 
14155 	  SELECT mmtt.transaction_type_id
14156 		   , mmtt.organization_id
14157 		   , mmtt.inventory_item_id
14158 		INTO l_transaction_type_id
14159 		   , l_org_id
14160 		   , l_item_id
14161 		FROM mtl_material_transactions_temp mmtt
14162 	   WHERE mmtt.transaction_temp_id = p_temp_id;
14163 
14164 	  IF inv_material_status_grp.is_status_applicable(
14165 		   p_wms_installed              => p_wms_installed
14166 		 , p_trx_status_enabled         => NULL
14167 		 , p_trx_type_id                => l_transaction_type_id
14168 		 , p_lot_status_enabled         => NULL
14169 		 , p_serial_status_enabled      => NULL
14170 		 , p_organization_id            => l_org_id
14171 		 , p_inventory_item_id          => l_item_id
14172 		 , p_sub_code                   => p_confirmed_sub
14173 		 , p_locator_id                 => p_confirmed_loc_id
14174 		 , p_lot_number                 => NULL
14175 		 , p_serial_number              => NULL
14176 		 , p_object_type                => 'Z'
14177 		 ) = 'Y'
14178 		 AND inv_material_status_grp.is_status_applicable(
14179 			  p_wms_installed              => p_wms_installed
14180 			, p_trx_status_enabled         => NULL
14181 			, p_trx_type_id                => l_transaction_type_id
14182 			, p_lot_status_enabled         => NULL
14183 			, p_serial_status_enabled      => NULL
14184 			, p_organization_id            => l_org_id
14185 			, p_inventory_item_id          => l_item_id
14186 			, p_sub_code                   => p_confirmed_sub
14187 			, p_locator_id                 => p_confirmed_loc_id
14188 			, p_lot_number                 => NULL
14189 			, p_serial_number              => NULL
14190 			, p_object_type                => 'L'
14191 			) = 'Y' THEN
14192 		x_result  := 1;
14193 
14194 		IF (l_debug = 1) THEN
14195 		  mydebug('validate_sub_loc_status: Material status is correct. x_result = 1');
14196 		END IF;
14197 	  ELSE
14198 		x_result  := 0;
14199 
14200 		IF (l_debug = 1) THEN
14201 		  mydebug('validate_sub_loc_status: Material status is incorrect. x_result = 0');
14202 		END IF;
14203 	  END IF;
14204 
14205 	  IF (l_debug = 1) THEN
14206 		mydebug('validate_sub_loc_status: End of validate_sub_loc_status');
14207 	  END IF;
14208 	EXCEPTION
14209 	  WHEN fnd_api.g_exc_error THEN
14210 		x_return_status  := fnd_api.g_ret_sts_error;
14211 
14212 		IF (l_debug = 1) THEN
14213 		  mydebug('validate_sub_loc_status: Error - ' || SQLERRM);
14214 		END IF;
14215 
14216 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
14217 	  WHEN OTHERS THEN
14218 		x_return_status  := fnd_api.g_ret_sts_unexp_error;
14219 
14220 		IF (l_debug = 1) THEN
14221 		  mydebug('validate_sub_loc_status: Unexpected Error - ' || SQLERRM);
14222 		END IF;
14223 
14224 		fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
14225 END validate_sub_loc_status;
14226 
14227 PROCEDURE insert_serial(
14228 	p_serial_transaction_temp_id IN OUT NOCOPY NUMBER,
14229 	p_organization_id            IN     NUMBER,
14230 	p_item_id                    IN     NUMBER,
14231 	p_revision                   IN     VARCHAR2,
14232 	p_lot                        IN     VARCHAR2,
14233 	p_transaction_temp_id        IN     NUMBER,
14234 	p_created_by                 IN     NUMBER,
14235 	p_from_serial                IN     VARCHAR2,
14236 	p_to_serial                  IN     VARCHAR2,
14237 	p_status_id                  IN     NUMBER := NULL,
14238 	x_return_status              OUT    NOCOPY VARCHAR2,
14239 	x_msg_data                   OUT    NOCOPY VARCHAR2
14240   ) IS
14241 
14242 	  PRAGMA AUTONOMOUS_TRANSACTION;
14243 
14244 	  l_return    NUMBER;
14245 	  l_to_serial VARCHAR2(30);
14246 	  l_progress  VARCHAR2(10);
14247 	  l_msg_count NUMBER;
14248 	  l_success   NUMBER := 0;
14249 	  l_count     NUMBER := 0;
14250 	  l_temp_qty  NUMBER :=0 ;
14251 	  l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
14252 BEGIN
14253 	  x_return_status  := fnd_api.g_ret_sts_success;
14254 	  l_progress       := '10';
14255 	  IF (l_debug = 1) THEN
14256 		 mydebug('Enter insert_serial: 10:'|| TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
14257 	  END IF;
14258 	  --SAVEPOINT rcv_insert_serial_sp;
14259 	  l_to_serial      := p_to_serial;
14260 	  l_return :=2;
14261 
14262 
14263 	  l_progress := '20';
14264 	  l_count := 0;
14265 	  BEGIN
14266 			 SELECT 1
14267 			 INTO l_count
14268 			 FROM mtl_serial_numbers_temp msnt, mtl_transaction_lots_temp mtlt,
14269 				  mtl_material_transactions_temp mmtt
14270 			 WHERE (p_from_serial BETWEEN msnt.fm_serial_number AND msnt.to_serial_number
14271 				OR p_to_serial BETWEEN msnt.fm_serial_number AND msnt.to_serial_number)
14272 			   AND mmtt.inventory_item_id = p_item_id
14273 			   AND mmtt.organization_id = p_organization_id
14274 			   AND mtlt.transaction_temp_id(+) = mmtt.transaction_temp_id
14275 			   AND msnt.transaction_temp_id = nvl(mtlt.serial_transaction_temp_id, mmtt.transaction_temp_id);
14276 	  EXCEPTION
14277 		   WHEN OTHERS THEN
14278 			l_count := 0;
14279 	  END;
14280 
14281 	  IF l_count <> 0 THEN
14282 			fnd_message.set_name('INV', 'INVALID_SERIAL_NUMBER');
14283 			fnd_msg_pub.ADD;
14284 			RAISE fnd_api.g_exc_error;
14285 	  END IF;
14286 
14287 	  l_progress       := '30';
14288 
14289 	  IF p_serial_transaction_temp_id IS NULL THEN
14290 		 l_progress  := '40';
14291 		 --BUG12670785
14292 		 begin
14293 
14294 		 SELECT serial_transaction_temp_id
14295 		   INTO p_serial_transaction_temp_id
14296 		   FROM mtl_transaction_lots_temp
14297 		  WHERE transaction_temp_id = p_transaction_temp_id
14298 			AND lot_number= p_lot;
14299 		exception
14300 		 when others then
14301 			 IF (l_debug = 1) THEN
14302 		  mydebug('No MTLT exists ', 4);
14303 	   END IF;
14304 		end ;
14305 
14306 		 IF p_serial_transaction_temp_id IS NULL THEN
14307 
14308 			  SELECT mtl_material_transactions_s.NEXTVAL
14309 				INTO p_serial_transaction_temp_id
14310 				FROM DUAL;
14311 
14312 			  l_progress  := '50';
14313 
14314 			  UPDATE mtl_transaction_lots_temp
14315 				 SET serial_transaction_temp_id = p_serial_transaction_temp_id
14316 			   WHERE transaction_temp_id = p_transaction_temp_id
14317 				 AND lot_number= p_lot;
14318 
14319 		 END IF;
14320 
14321 	  END IF;
14322 
14323 	   l_progress       := '60';
14324 	   l_return         := inv_trx_util_pub.insert_ser_trx(
14325 							 p_trx_tmp_id => p_serial_transaction_temp_id,
14326 							 p_user_id    => p_created_by,
14327 							 p_fm_ser_num => p_from_serial,
14328 							 p_to_ser_num => p_to_serial,
14329 							 p_status_id  => p_status_id,
14330 							 x_proc_msg   => x_msg_data
14331 						   );
14332 	   l_progress       := '70';
14333 
14334 	   BEGIN
14335 		 UPDATE mtl_serial_numbers
14336 			SET group_mark_id = p_serial_transaction_temp_id
14337 		  WHERE inventory_item_id = p_item_id
14338 			AND serial_number BETWEEN p_from_serial AND p_to_serial
14339 			AND LENGTH(serial_number) = LENGTH(p_from_serial);
14340 	   EXCEPTION
14341 		 WHEN OTHERS THEN
14342 		   IF (l_debug = 1) THEN
14343 			  mydebug('Exception updating grp. id', 4);
14344 		   END IF;
14345 	   END;
14346 
14347 	   IF (l_debug = 1) THEN
14348 		  mydebug('Insert serial vals'|| p_item_id || ':' || p_from_serial || ':' || p_to_serial, 4);
14349 		  mydebug('Insert serial, inserted with '|| p_serial_transaction_temp_id || ':' || l_success, 4);
14350 	   END IF;
14351 
14352 	   -- if the trx manager returned a 1 then it could not insert the row
14353 	   IF l_return = 1 THEN
14354 		 RAISE fnd_api.g_exc_error;
14355 	   END IF;
14356 
14357 	   l_progress       := '80';
14358 	   IF (l_debug = 1) THEN
14359 		  mydebug('Exitting insert_serial : 90  '|| TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
14360 	   END IF;
14361 
14362 	   commit;
14363 
14364 	 EXCEPTION
14365 	   WHEN fnd_api.g_exc_error THEN
14366 		 --ROLLBACK TO rcv_insert_serial_sp;
14367 		 ROLLBACK;
14368 		 x_return_status  := fnd_api.g_ret_sts_error;
14369 		 IF (l_debug = 1) THEN
14370 			mydebug('Exitting insert_serial - execution error:'|| l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
14371 		 END IF;
14372 		 --  Get message count and data
14373 		 fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => l_msg_count, p_data => x_msg_data);
14374 	   WHEN OTHERS THEN
14375 		 x_return_status  := fnd_api.g_ret_sts_unexp_error;
14376 
14377 		 IF SQLCODE IS NOT NULL THEN
14378 		   inv_mobile_helper_functions.sql_error('wms_task_load.insert_serial', l_progress, SQLCODE);
14379 		 END IF;
14380 
14381 		 IF (l_debug = 1) THEN
14382 			mydebug('Exitting insert_serial - other exception:'|| l_progress || ' ' || TO_CHAR(SYSDATE, 'YYYY-MM-DD HH:DD:SS'), 1);
14383 		 END IF;
14384 
14385 		 --
14386 		 IF fnd_msg_pub.check_msg_level(fnd_msg_pub.g_msg_lvl_unexp_error) THEN
14387 		   fnd_msg_pub.add_exc_msg(g_pkg_name, 'insert_serial');
14388 		 END IF;
14389 
14390 END insert_serial;
14391 
14392 /*Added to validate cost group comingle bug 3858907 */
14393 procedure validate_loaded_lpn_cg( p_organization_id       IN  NUMBER,
14394 		p_inventory_item_id     IN  NUMBER,
14395 		p_subinventory_code     IN  VARCHAR2,
14396 		p_locator_id            IN  NUMBER,
14397 		p_revision              IN  VARCHAR2,
14398 		p_lot_number            IN  VARCHAR2,
14399 		p_lpn_id                IN  NUMBER,
14400 		p_transfer_lpn_id       IN  NUMBER,
14401 		p_lot_control           IN  NUMBER,
14402 		p_revision_control      IN  NUMBER,
14403 		x_commingle_exist       OUT NOCOPY VARCHAR2,
14404 		x_return_status         OUT NOCOPY VARCHAR2,
14405 		p_trx_type_id           IN  VARCHAR2, -- Bug 4632519
14406 		p_trx_action_id        IN  VARCHAR2) -- Bug 4632519
14407 IS
14408 	l_debug number := NVL(FND_PROFILE.VALUE('INV_DEBUG_TRACE'),0);
14409 	l_cur_cost_group_id NUMBER := NULL;
14410 	l_exist_cost_group_id NUMBER := NULL;
14411 	l_sub VARCHAR2(20);
14412 	l_loc NUMBER;
14413 	l_rev VARCHAR2(4);
14414 	l_lpn NUMBER;
14415 	l_ser VARCHAR2(20);
14416 	l_lot VARCHAR2(20);
14417 	-- Bug 4632519
14418 	l_transaction_action_id NUMBER;
14419 	l_transaction_type_id NUMBER;
14420 	l_is_bf  boolean;
14421 	-- Bug 4632519
14422 BEGIN
14423    IF (l_debug = 1) THEN
14424 	  mydebug( 'In check_cg_commingle... ');
14425 	  mydebug('p_organization_id'||p_organization_id);
14426 	  mydebug('p_inventory_item_id'||p_inventory_item_id);
14427 	  mydebug('p_subinventory_code'||p_subinventory_code);
14428 	  mydebug('p_locator_id'||p_locator_id);
14429 	  mydebug('p_revision'||p_revision);
14430 	  mydebug('p_lot_number'||p_lot_number);
14431 	  mydebug('p_transfer_lpn_id'||p_transfer_lpn_id);
14432 	  mydebug('p_lpn_id'||p_lpn_id);
14433 	  mydebug('p_lot_control'||p_lot_control);
14434 	  mydebug('p_revision_control'||p_revision_control);
14435 	  mydebug('p_trx_souce_type_id' ||p_trx_type_id);
14436 	  mydebug('p_trx_action_id ' || p_trx_action_id);
14437    END IF;
14438 
14439    x_return_status  := fnd_api.g_ret_sts_success;
14440    x_commingle_exist := 'N';
14441 
14442 	 IF p_lot_control = 1 THEN
14443 		 select mmtt.subinventory_code,
14444 				mmtt.locator_id,
14445 				mmtt.revision,
14446 				mmtt.lpn_id,
14447 				null,
14448 				null,
14449 				mmtt.transaction_action_id, -- Bug 4632519
14450 				mmtt.transaction_type_id -- Bug 4632519
14451 		   INTO l_sub,
14452 				l_loc,
14453 				l_rev,
14454 				l_lpn,
14455 				l_ser,l_lot,
14456 				l_transaction_action_id, -- Bug 4632519
14457 				l_transaction_type_id -- Bug 4632519
14458 		   from mtl_material_Transactions_temp mmtt
14459 		  where mmtt.inventory_item_id = p_inventory_item_id
14460 			and mmtt.organization_id = p_organization_id
14461 			and mmtt.transfer_lpn_id = p_transfer_lpn_id
14462 			and mmtt.content_lpn_id is null
14463 			and decode(p_revision_control,2,mmtt.revision,1,'~~') = nvl(p_revision,'~~')
14464 			and rownum<2;
14465 	ELSE
14466 		 select mmtt.subinventory_code,
14467 				mmtt.locator_id,
14468 				mmtt.revision,
14469 				mmtt.lpn_id,
14470 				null,
14471 				mtlt.lot_number,
14472 				mmtt.transaction_action_id,
14473 				mmtt.transaction_type_id
14474 		 INTO l_sub,
14475 			  l_loc,
14476 			  l_rev,
14477 			  l_lpn,
14478 			  l_ser,
14479 			  l_lot,
14480 			  l_transaction_action_id, -- Bug 4632519
14481 			  l_transaction_type_id -- Bug 4632519
14482 		 from mtl_material_Transactions_temp mmtt,
14483 			  mtl_transaction_lots_temp mtlt
14484 		where mmtt.inventory_item_id = p_inventory_item_id
14485 		  and mmtt.organization_id = p_organization_id
14486 		  and mmtt.transfer_lpn_id = p_transfer_lpn_id
14487 		  and mmtt.content_lpn_id is null
14488 		  and decode(p_revision_control,2,mmtt.revision,1,'~~') = nvl(p_revision,'~~')
14489 		  and mmtt.transaction_temp_id = mtlt.transaction_temp_id
14490 		  and mtlt.lot_number = p_lot_number
14491 		  and rownum<2;
14492 	END IF;
14493 
14494 	IF (l_debug = 1) THEN
14495 		mydebug( 'Loaded LPN data From MMTT');
14496 		mydebug('l_subinventory_code'||l_sub);
14497 		mydebug('l_locator_id'||l_loc);
14498 		mydebug('l_revision'||l_rev);
14499 		mydebug('l_lot_number'||l_lot);
14500 		mydebug('l_serial_number'||l_ser);
14501 		mydebug('l_lpn_id'||l_lpn);
14502 	END IF;
14503 	--
14504 	-- Bug 4632519
14505 	if (p_trx_type_id='51') then
14506 		l_is_bf := true;
14507 	end if;
14508 	--
14509 	 inv_cost_group_update.proc_determine_costgroup(
14510 		p_organization_id       => p_organization_id,
14511 		p_inventory_item_id     => p_inventory_item_id,
14512 		p_subinventory_code     => p_subinventory_code,
14513 		p_locator_id            => p_locator_id,
14514 		p_revision              => p_revision,
14515 		p_lot_number            => p_lot_number,
14516 		p_serial_number         => null,
14517 		p_containerized_flag    => null,
14518 		p_lpn_id                => p_lpn_id,
14519 		p_transaction_action_id => p_trx_action_id,
14520 		p_is_backflush_txn      => l_is_bf,
14521 		x_cost_group_id         => l_cur_cost_group_id,
14522 		x_return_status         => x_return_status);
14523 
14524 	 IF x_return_status <> fnd_api.g_ret_sts_success THEN
14525 		RAISE fnd_api.g_exc_unexpected_error;
14526 	 END IF;
14527 
14528 	 if (l_transaction_type_id=51) then
14529 		l_is_bf := true;
14530 	 end if;
14531 	 inv_cost_group_update.proc_determine_costgroup(
14532 		   p_organization_id       => p_organization_id,
14533 		   p_inventory_item_id     => p_inventory_item_id,
14534 		   p_subinventory_code     => l_sub,
14535 		   p_locator_id            => l_loc,
14536 		   p_revision              => l_rev,
14537 		   p_lot_number            => l_lot,
14538 		   p_serial_number         => l_ser,
14539 		   p_containerized_flag    => null,
14540 		   p_lpn_id                => l_lpn,
14541 		   p_transaction_action_id => l_transaction_action_id,
14542 		   p_is_backflush_txn      => l_is_bf,
14543 		   x_cost_group_id         => l_exist_cost_group_id,
14544 		   x_return_status         => x_return_status);
14545 
14546    IF x_return_status <> fnd_api.g_ret_sts_success THEN
14547 		   RAISE fnd_api.g_exc_unexpected_error;
14548    END IF;
14549 
14550    IF l_exist_cost_group_id <> l_cur_cost_group_id THEN
14551 	  x_return_status := fnd_api.g_ret_sts_success;
14552 	  x_commingle_exist := 'Y';
14553    END IF;
14554    --
14555    -- Bug 4632519
14556    --
14557 EXCEPTION
14558    WHEN NO_DATA_FOUND THEN
14559 	   IF (l_debug = 1) THEN
14560 		 mydebug('First record being loaded into LPN');
14561 	   END IF;
14562 	   x_return_status := fnd_api.g_ret_sts_success;
14563 	   x_commingle_exist := 'N';
14564    WHEN OTHERS THEN
14565 		x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
14566 		x_commingle_exist := 'Y';
14567 END validate_loaded_lpn_cg;
14568 
14569 --/* Bug 9448490 Lot Substitution Project */ start
14570 PROCEDURE insert_mtlt (
14571 		p_new_temp_id     IN  NUMBER
14572 	  , p_serial_temp_id  IN  NUMBER := NULL
14573 	  , p_pri_att_qty         IN  NUMBER
14574 	  , p_sec_att_qty         IN  NUMBER := NULL
14575 	  , p_trx_att_qty         IN  NUMBER
14576 	  , p_lot_number      IN  VARCHAR2
14577 	  , p_item_id         IN  NUMBER
14578 	  , p_organization_id IN  NUMBER
14579 	  , x_return_status   OUT NOCOPY VARCHAR2)  IS
14580 
14581   BEGIN
14582 	x_return_status  := fnd_api.g_ret_sts_success;
14583 	mydebug('  Inside insert mtlt' );
14584 	INSERT INTO mtl_transaction_lots_temp
14585 				(
14586 				 transaction_temp_id
14587 			   , last_update_date
14588 			   , last_updated_by
14589 			   , creation_date
14590 			   , created_by
14591 			   , transaction_quantity
14592 			   , primary_quantity
14593 			   , secondary_quantity
14594 			   , lot_number
14595 			   , lot_expiration_date
14596 			   , serial_transaction_temp_id
14597 			   , description
14598 			   , vendor_name
14599 			   , supplier_lot_number
14600 			   , origination_date
14601 			   , date_code
14602 			   , grade_code
14603 			   , change_date
14604 			   , maturity_date
14605 			   , retest_date
14606 			   , age
14607 			   , item_size
14608 			   , color
14609 			   , volume
14610 			   , volume_uom
14611 			   , place_of_origin
14612 			   , best_by_date
14613 			   , LENGTH
14614 			   , length_uom
14615 			   , recycled_content
14616 			   , thickness
14617 			   , thickness_uom
14618 			   , width
14619 			   , width_uom
14620 			   , curl_wrinkle_fold
14621 			   , lot_attribute_category
14622 			   , c_attribute1
14623 			   , c_attribute2
14624 			   , c_attribute3
14625 			   , c_attribute4
14626 			   , c_attribute5
14627 			   , c_attribute6
14628 			   , c_attribute7
14629 			   , c_attribute8
14630 			   , c_attribute9
14631 			   , c_attribute10
14632 			   , c_attribute11
14633 			   , c_attribute12
14634 			   , c_attribute13
14635 			   , c_attribute14
14636 			   , c_attribute15
14637 			   , c_attribute16
14638 			   , c_attribute17
14639 			   , c_attribute18
14640 			   , c_attribute19
14641 			   , c_attribute20
14642 			   , d_attribute1
14643 			   , d_attribute2
14644 			   , d_attribute3
14645 			   , d_attribute4
14646 			   , d_attribute5
14647 			   , d_attribute6
14648 			   , d_attribute7
14649 			   , d_attribute8
14650 			   , d_attribute9
14651 			   , d_attribute10
14652 			   , n_attribute1
14653 			   , n_attribute2
14654 			   , n_attribute3
14655 			   , n_attribute4
14656 			   , n_attribute5
14657 			   , n_attribute6
14658 			   , n_attribute7
14659 			   , n_attribute8
14660 			   , n_attribute9
14661 			   , n_attribute10
14662 			   , vendor_id
14663 			   , territory_code
14664 				)
14665 	  (SELECT p_new_temp_id
14666 			, sysdate
14667 			, -9999
14668 			, sysdate
14669 			, -9999
14670 			, p_trx_att_qty
14671 			, p_pri_att_qty
14672 			, p_sec_att_qty
14673 			, p_lot_number
14674 			, mln.expiration_date
14675 			, p_serial_temp_id
14676 			, mln.description
14677 			, mln.vendor_name
14678 			, mln.supplier_lot_number
14679 			, mln.origination_date
14680 			, mln.date_code
14681 			, mln.grade_code
14682 			, mln.change_date
14683 			, mln.maturity_date
14684 			, mln.retest_date
14685 			, mln.age
14686 			, mln.item_size
14687 			, mln.color
14688 			, mln.volume
14689 			, mln.volume_uom
14690 			, mln.place_of_origin
14691 			, mln.best_by_date
14692 			, mln.LENGTH
14693 			, mln.length_uom
14694 			, mln.recycled_content
14695 			, mln.thickness
14696 			, mln.thickness_uom
14697 			, mln.width
14698 			, mln.width_uom
14699 			, mln.curl_wrinkle_fold
14700 			, mln.lot_attribute_category
14701 			, mln.c_attribute1
14702 			, mln.c_attribute2
14703 			, mln.c_attribute3
14704 			, mln.c_attribute4
14705 			, mln.c_attribute5
14706 			, mln.c_attribute6
14707 			, mln.c_attribute7
14708 			, mln.c_attribute8
14709 			, mln.c_attribute9
14710 			, mln.c_attribute10
14711 			, mln.c_attribute11
14712 			, mln.c_attribute12
14713 			, mln.c_attribute13
14714 			, mln.c_attribute14
14715 			, mln.c_attribute15
14716 			, mln.c_attribute16
14717 			, mln.c_attribute17
14718 			, mln.c_attribute18
14719 			, mln.c_attribute19
14720 			, mln.c_attribute20
14721 			, mln.d_attribute1
14722 			, mln.d_attribute2
14723 			, mln.d_attribute3
14724 			, mln.d_attribute4
14725 			, mln.d_attribute5
14726 			, mln.d_attribute6
14727 			, mln.d_attribute7
14728 			, mln.d_attribute8
14729 			, mln.d_attribute9
14730 			, mln.d_attribute10
14731 			, mln.n_attribute1
14732 			, mln.n_attribute2
14733 			, mln.n_attribute3
14734 			, mln.n_attribute4
14735 			, mln.n_attribute5
14736 			, mln.n_attribute6
14737 			, mln.n_attribute7
14738 			, mln.n_attribute8
14739 			, mln.n_attribute9
14740 			, mln.n_attribute10
14741 			, mln.vendor_id
14742 			, mln.territory_code
14743 	   FROM    mtl_lot_numbers mln
14744 	   WHERE   mln.lot_number = p_lot_number
14745 	   AND    mln.inventory_item_id = p_item_id
14746 	   AND    mln.organization_id = p_organization_id);
14747 
14748  EXCEPTION
14749 	WHEN OTHERS THEN
14750 	x_return_status  := l_g_ret_sts_error;
14751 	mydebug(' Insert mtlt returns exception' );
14752 	mydebug ('Others exception while updating From LPN context: ' || SQLCODE);
14753 END;
14754 
14755 
14756 
14757 PROCEDURE populate_lot_lov(
14758 	p_fromlpn_id            IN            NUMBER
14759   , p_org_id                IN            NUMBER
14760   , p_item_id               IN            NUMBER
14761   , p_rev                   IN            VARCHAR2
14762   , p_lot                   IN            VARCHAR2
14763   , p_trx_qty               IN            NUMBER
14764   , p_trx_uom               IN            VARCHAR2
14765   , x_match                 OUT NOCOPY    NUMBER
14766   , x_return_status         OUT NOCOPY    VARCHAR2
14767   , p_temp_id               IN            NUMBER
14768   , p_transaction_type_id   IN            NUMBER
14769   , p_cost_group_id         IN            NUMBER
14770   , p_is_sn_alloc           IN            VARCHAR2
14771   , p_user_id               IN            NUMBER
14772   , x_lpn_lot_vector        OUT NOCOPY    VARCHAR2
14773   , x_lpn_lot_vector2       OUT NOCOPY    VARCHAR2 --BUG14003388
14774   , x_lpn_lot_vector3       OUT NOCOPY    VARCHAR2 --BUG14003388
14775   , x_lpn_lot_vector4       OUT NOCOPY    VARCHAR2 --BUG14003388
14776   , p_transaction_action_id IN            NUMBER
14777   , p_confirmed_sub         IN            VARCHAR2
14778   , p_confirmed_loc_id      IN            NUMBER
14779   , p_from_lpn_id           IN            NUMBER
14780   ) IS
14781 	l_proc_name              VARCHAR2(30) := 'populate_lot_lov' ;
14782 	l_msg_cnt                NUMBER;
14783 	l_msg_data               VARCHAR2(2000);
14784 	l_return_status          VARCHAR2(240);
14785 	l_exist_qty              NUMBER;
14786 	l_item_cnt               NUMBER;
14787 	l_rev_cnt                NUMBER;
14788 	l_lot_cnt                NUMBER;
14789 	l_item_cnt2              NUMBER;
14790 	l_cg_cnt                 NUMBER;
14791 	l_sub                    VARCHAR2(60);
14792 	l_loc                    VARCHAR2(60);
14793 	l_loaded                 NUMBER         := 0;
14794 	l_allocate_serial_flag   NUMBER         := 0;
14795 	l_temp_serial_trans_temp NUMBER         := 0;
14796 	l_serial_number          VARCHAR2(50);
14797 	l_lpn_pr_qty             NUMBER;
14798 	l_lpn_trx_qty            NUMBER;
14799 	l_pr_qty                 NUMBER;
14800 	l_primary_uom            VARCHAR2(3);
14801 	l_lot_code               NUMBER;
14802 	l_serial_code            NUMBER;
14803 	l_mmtt_qty               NUMBER;
14804 	l_out_temp_id            NUMBER         := 0;
14805 	l_serial_exist_cnt       NUMBER         := 0;
14806 	l_total_serial_cnt       NUMBER         := 0;
14807 	l_so_cnt                 NUMBER         := 0;
14808 	l_mtlt_lot_number        VARCHAR2(30);
14809 	l_mtlt_primary_qty       NUMBER;
14810 	l_wlc_quantity           NUMBER;
14811 	l_wlc_uom_code           VARCHAR2(3);
14812 	l_lot_match              NUMBER;
14813 	l_ok_to_process          VARCHAR2(5);
14814 	l_is_revision_control    VARCHAR2(1);
14815 	l_is_lot_control         VARCHAR2(1);
14816 	l_is_serial_control      VARCHAR2(1);
14817 	b_is_revision_control    BOOLEAN;
14818 	b_is_lot_control         BOOLEAN;
14819 	b_is_serial_control      BOOLEAN;
14820 	l_from_lpn               VARCHAR2(30);
14821 	l_loc_id                 NUMBER;
14822 	l_lpn_context            NUMBER;
14823 	l_lpn_exists             NUMBER;
14824 	l_qoh                    NUMBER;
14825 	l_rqoh                   NUMBER;
14826 	l_qr                     NUMBER;
14827 	l_qs                     NUMBER;
14828 	l_att                    NUMBER;
14829 	l_atr                    NUMBER;
14830 	l_allocated_lpn_id       NUMBER;
14831 	l_table_index            NUMBER         := 0;
14832 	l_table_total            NUMBER         := 0;
14833 	l_table_count            NUMBER;
14834 	l_lpn_include_lpn        NUMBER;
14835 	l_xfr_sub_code           VARCHAR2(30);
14836 	l_sub_active             NUMBER         := 0;
14837 	l_loc_active             NUMBER         := 0;
14838 	l_mmtt_proj_id NUMBER ;  --  2774506/2905646
14839 	l_mmtt_task_id NUMBER ;
14840 	l_locator_id NUMBER;
14841 	l_organization_id NUMBER;
14842 	l_mil_proj_id NUMBER ;
14843 	l_mil_task_id NUMBER ;   -- 2774506/2905646
14844 	l_transaction_header_id   NUMBER;
14845 	l_transaction_uom         VARCHAR2(3);
14846 	l_lpn_id          NUMBER;
14847 	l_content_lpn_id  NUMBER;
14848 	--l_transfer_lpn_id NUMBER;
14849 	l_check_tolerance   Boolean;
14850 	l_overpicked_qty   NUMBER ;
14851 	l_check_overpick_passed VARCHAR2(1);
14852 	l_overpick_error_code  NUMBER;
14853 	l_match_serials      Boolean  := false;
14854 	l_pick_to_lpn_id      NUMBER;
14855 	l_lot_v              VARCHAR2(2000) := null;
14856 	l_value VARCHAR2(3);  --bug 6012428
14857 
14858 	l_transaction_action_id         NUMBER; --12871057
14859 	l_txn_source_type_id            NUMBER; --12871057
14860 
14861 	CURSOR lot_substitution_alloc_csr IS
14862 	   SELECT NVL(SUM(primary_transaction_quantity),0)
14863 		  , lot_number
14864 			 FROM mtl_onhand_quantities_detail
14865 			 WHERE organization_id = p_org_id
14866 		 AND Nvl(containerized_flag, 2) = 1 -- different from loose_match
14867 		 AND lpn_id = p_fromlpn_id
14868 			 AND subinventory_code = p_confirmed_sub
14869 			 AND locator_id = p_confirmed_loc_id
14870 			 AND inventory_item_id = p_item_id
14871 			 AND (revision = p_rev OR (revision IS NULL AND p_rev IS NULL))
14872 			 AND lot_number NOT IN (
14873 					   SELECT mtlt.lot_number
14874 				   FROM mtl_transaction_lots_temp mtlt
14875 				   WHERE mtlt.transaction_temp_id = p_temp_id
14876 				   )
14877 		AND lot_number IS NOT NULL
14878 		GROUP BY lot_number
14879 		ORDER BY lot_number;
14880 
14881 	CURSOR lot_substitution_loose_csr IS
14882 	   SELECT NVL(SUM(primary_transaction_quantity),0)
14883 		  , lot_number
14884 			 FROM mtl_onhand_quantities_detail
14885 			 WHERE organization_id = p_org_id
14886 		 AND Nvl(containerized_flag, 2) <> 1
14887 			 AND subinventory_code = p_confirmed_sub
14888 			 AND locator_id = p_confirmed_loc_id
14889 			 AND inventory_item_id = p_item_id
14890 			 AND (revision = p_rev OR (revision IS NULL AND p_rev IS NULL))
14891 			 AND lot_number NOT IN (
14892 					   SELECT mtlt.lot_number
14893 				   FROM mtl_transaction_lots_temp mtlt
14894 				   WHERE mtlt.transaction_temp_id = p_temp_id
14895 				   )
14896 		AND lot_number IS NOT NULL
14897 		GROUP BY lot_number
14898 		ORDER BY lot_number;
14899 	l_debug                  NUMBER         := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
14900 
14901   BEGIN
14902 	x_return_status    := fnd_api.g_ret_sts_success;
14903 	t_lpn_lot_qty_table.DELETE;
14904 
14905 	 SELECT primary_uom_code
14906 		  , lot_control_code
14907 		  , serial_number_control_code
14908 	   INTO l_primary_uom
14909 		  , l_lot_code
14910 		  , l_serial_code
14911 	   FROM mtl_system_items
14912 	  WHERE organization_id = p_org_id
14913 		AND inventory_item_id = p_item_id;
14914 
14915 	select value
14916 	into l_value
14917 	from v$nls_parameters
14918 	where parameter = 'NLS_NUMERIC_CHARACTERS';
14919 
14920 	-- p_trx_qty was passed in transaction_uom, need to convert it to primary_uom
14921 	IF (l_debug = 1) THEN
14922 		  mydebug('p_trx_uom :'|| p_trx_uom);
14923 		  mydebug('l_primary_uom :'|| l_primary_uom);
14924 		  mydebug('p_trx_qty in transaction uom:'|| p_trx_qty);
14925 	END IF;
14926 
14927 --12871057 Begin Fetching the action and source type from cache for the txn type id
14928 			IF inv_cache.set_mtt_rec(p_transaction_type_id) THEN
14929 			  l_transaction_action_id         :=  inv_cache.mtt_rec.transaction_action_id;
14930 			  l_txn_source_type_id            :=  inv_cache.mtt_rec.transaction_source_type_id;
14931 			ELSE
14932 			  RAISE fnd_api.g_exc_unexpected_error;
14933 			END IF;
14934 
14935 			IF(l_debug=1) THEN
14936 			inv_log_util.trace( 'Value of p_transaction_type_id          :  '  ||p_transaction_type_id, 'Inside check_exp_lot_txn_allowed  ',   9);
14937 			inv_log_util.trace( 'Value of l_transaction_action_id        :  '  ||l_transaction_action_id, 'Inside check_exp_lot_txn_allowed  ',   9);
14938 			inv_log_util.trace( 'Value of l_txn_source_type_id           :  '  ||l_txn_source_type_id, 'Inside check_exp_lot_txn_allowed  ',   9);
14939 			END IF;
14940 --12871057 End Fetching the action and source type from cache for the txn type id
14941 
14942 	IF  (p_transaction_action_id = 28 OR (l_txn_source_type_id =4 AND l_transaction_action_id =1) OR (l_txn_source_type_id =4 AND l_transaction_action_id =2)) --12871057
14943 	THEN
14944 	IF p_fromlpn_id IS NOT NULL THEN
14945 	OPEN lot_substitution_alloc_csr;
14946 	 LOOP
14947 	   FETCH lot_substitution_alloc_csr INTO l_mtlt_primary_qty, l_mtlt_lot_number;
14948 	   EXIT WHEN lot_substitution_alloc_csr%NOTFOUND;
14949 
14950 		   IF (l_debug = 1) THEN
14951 			  mydebug(' Unallocated l_mtlt_lot_number : ' || l_mtlt_lot_number);
14952 			  mydebug(' Unallocated l_mtlt_primary_qty: ' || l_mtlt_primary_qty);
14953 			  mydebug(' Unallocated p_org_id ' || p_org_id);
14954 			  mydebug(' Unallocated p_item_id ' || p_item_id);
14955 			  mydebug(' Unallocated NVL(p_rev, NULL) '|| p_rev);
14956 			  mydebug(' Unallocated l_mtlt_lot_number ' || l_mtlt_lot_number);
14957 			  mydebug(' Unallocated p_confirmed_sub '|| p_confirmed_sub);
14958 			  mydebug(' Unallocated p_confirmed_loc_id ' || p_confirmed_loc_id);
14959 			  mydebug(' Unallocated p_fromlpn_id ' || p_fromlpn_id);
14960 			  mydebug(' Unallocated l_xfr_sub_code ' || l_xfr_sub_code);
14961 			 END IF;
14962 
14963 	 inv_quantity_tree_pub.query_quantities(
14964 			p_api_version_number         => 1.0
14965 		  , p_init_msg_lst               => fnd_api.g_false
14966 		  , x_return_status              => l_return_status
14967 		  , x_msg_count                  => l_msg_cnt
14968 		  , x_msg_data                   => l_msg_data
14969 		  , p_organization_id            => p_org_id
14970 		  , p_inventory_item_id          => p_item_id
14971 		  , p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
14972 		  , p_is_revision_control        => b_is_revision_control
14973 		  , p_is_lot_control             => TRUE
14974 		  , p_is_serial_control          => b_is_serial_control
14975 		  , p_demand_source_type_id      => -9999
14976 		  , p_revision                   => NVL(p_rev, NULL)
14977 		  , p_lot_number                 => l_mtlt_lot_number
14978 		  , p_subinventory_code          => p_confirmed_sub
14979 		  , p_locator_id                 => p_confirmed_loc_id
14980 		  , x_qoh                        => l_qoh
14981 		  , x_rqoh                       => l_rqoh
14982 		  , x_qr                         => l_qr
14983 		  , x_qs                         => l_qs
14984 		  , x_att                        => l_att
14985 		  , x_atr                        => l_atr
14986 		  , p_lpn_id                     => p_fromlpn_id
14987 		  , p_transfer_subinventory_code => l_xfr_sub_code
14988 		  );
14989 
14990 		  IF (l_return_status = fnd_api.g_ret_sts_success) THEN
14991 			IF (l_att > 0) THEN
14992 			  l_table_index  := l_table_index + 1;
14993 			  IF (l_debug = 1) THEN
14994 				mydebug('Unallocated  l_att:' || l_att || ' for lot:' || l_mtlt_lot_number);
14995 				mydebug('Unallocated  l_qoh:' || l_qoh || ' for lot:' || l_mtlt_lot_number);
14996 			  END IF;
14997 
14998 			  IF l_att < l_qoh THEN
14999 				  l_check_tolerance := false;
15000 				  IF (l_debug = 1) THEN
15001 					mydebug(' Unallocated Lots l_att < l_qoh: set l_check_tolerance to false');
15002 				  END IF;
15003 			  END IF;
15004 
15005 			  IF (l_debug = 1) THEN
15006 				  mydebug(' Unallocated l_table_index:' || l_table_index || ' lot_number:' || l_mtlt_lot_number || ' qty: ' || l_att);
15007 			  END IF;
15008 			  l_lpn_pr_qty                                   := l_lpn_pr_qty + l_att;
15009 			  t_lpn_lot_qty_table(l_table_index).lpn_id      := p_fromlpn_id;
15010 			  t_lpn_lot_qty_table(l_table_index).lot_number  := l_mtlt_lot_number;
15011 			  t_lpn_lot_qty_table(l_table_index).pri_qty := l_att;
15012 
15013 			  IF (l_primary_uom = p_trx_uom) THEN
15014 					 t_lpn_lot_qty_table(l_table_index).trx_qty := l_att;
15015 			  ELSE
15016 					 t_lpn_lot_qty_table(l_table_index).trx_qty := inv_convert.inv_um_convert(
15017 											   item_id        => p_item_id
15018 											  ,precision      => null
15019 											  ,from_quantity  => l_att
15020 											  ,from_unit      => l_primary_uom
15021 											  ,to_unit        => p_trx_uom
15022 											  ,from_name      => null
15023 											  ,to_name        => null);
15024 			  END IF;
15025 			  ELSE
15026 			   IF (l_debug = 1) THEN
15027 				  mydebug('Unallocated -- LPN does not have any available qty for lot ' || l_mtlt_lot_number);
15028 				  mydebug('Unallocated -- set l_check_tolerance to false');
15029 			   END IF;
15030 			  l_check_tolerance := false;
15031 		END IF;
15032 	  END IF;
15033 	  END LOOP;
15034 	CLOSE lot_substitution_alloc_csr;
15035 	ELSE
15036 	OPEN lot_substitution_loose_csr;
15037 	 LOOP
15038 	   FETCH lot_substitution_loose_csr INTO l_mtlt_primary_qty, l_mtlt_lot_number;
15039 	   EXIT WHEN lot_substitution_loose_csr%NOTFOUND;
15040 
15041 		   IF (l_debug = 1) THEN
15042 			  mydebug(' Unallocated l_mtlt_lot_number : ' || l_mtlt_lot_number);
15043 			  mydebug(' Unallocated l_mtlt_primary_qty: ' || l_mtlt_primary_qty);
15044 		   END IF;
15045 
15046 	 inv_quantity_tree_pub.query_quantities(
15047 			p_api_version_number         => 1.0
15048 		  , p_init_msg_lst               => fnd_api.g_false
15049 		  , x_return_status              => l_return_status
15050 		  , x_msg_count                  => l_msg_cnt
15051 		  , x_msg_data                   => l_msg_data
15052 		  , p_organization_id            => p_org_id
15053 		  , p_inventory_item_id          => p_item_id
15054 		  , p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
15055 		  , p_is_revision_control        => b_is_revision_control
15056 		  , p_is_lot_control             => TRUE
15057 		  , p_is_serial_control          => b_is_serial_control
15058 		  , p_demand_source_type_id      => -9999
15059 		  , p_revision                   => NVL(p_rev, NULL)
15060 		  , p_lot_number                 => l_mtlt_lot_number
15061 		  , p_subinventory_code          => p_confirmed_sub
15062 		  , p_locator_id                 => p_confirmed_loc_id
15063 		  , x_qoh                        => l_qoh
15064 		  , x_rqoh                       => l_rqoh
15065 		  , x_qr                         => l_qr
15066 		  , x_qs                         => l_qs
15067 		  , x_att                        => l_att
15068 		  , x_atr                        => l_atr
15069 		  , p_lpn_id                     => p_fromlpn_id
15070 		  , p_transfer_subinventory_code => l_xfr_sub_code
15071 		  );
15072 
15073 		  IF (l_debug = 1) THEN
15074 			 mydebug('Unallocated  l_att:' || l_att || ' for lot:' || l_mtlt_lot_number);
15075 			 mydebug('Unallocated  l_qoh:' || l_qoh || ' for lot:' || l_mtlt_lot_number);
15076 		  END IF;
15077 		  IF (l_return_status = fnd_api.g_ret_sts_success) THEN
15078 			IF (l_att > 0) THEN
15079 			  l_table_index  := l_table_index + 1;
15080 			  IF l_att < l_qoh THEN
15081 				  l_check_tolerance := false;
15082 				  IF (l_debug = 1) THEN
15083 					mydebug(' Unallocated Lots l_att < l_qoh: set l_check_tolerance to false');
15084 				  END IF;
15085 			  END IF;
15086 
15087 			  IF (l_debug = 1) THEN
15088 				  mydebug(' Unallocated l_table_index:' || l_table_index || ' lot_number:' || l_mtlt_lot_number || ' qty: ' || l_att);
15089 			  END IF;
15090 			  l_lpn_pr_qty                                   := l_lpn_pr_qty + l_att;
15091 			  t_lpn_lot_qty_table(l_table_index).lpn_id      := p_fromlpn_id;
15092 			  t_lpn_lot_qty_table(l_table_index).lot_number  := l_mtlt_lot_number;
15093 			  t_lpn_lot_qty_table(l_table_index).pri_qty := l_att;
15094 
15095 			  IF (l_primary_uom = p_trx_uom) THEN
15096 					 t_lpn_lot_qty_table(l_table_index).trx_qty := l_att;
15097 			  ELSE
15098 					 t_lpn_lot_qty_table(l_table_index).trx_qty := inv_convert.inv_um_convert(
15099 											   item_id        => p_item_id
15100 											  ,precision      => null
15101 											  ,from_quantity  => l_att
15102 											  ,from_unit      => l_primary_uom
15103 											  ,to_unit        => p_trx_uom
15104 											  ,from_name      => null
15105 											  ,to_name        => null);
15106 			  END IF;
15107 
15108 
15109 			ELSE
15110 			   IF (l_debug = 1) THEN
15111 				  mydebug('Unallocated --- LPN does not have any available qty for lot ' || l_mtlt_lot_number);
15112 				  mydebug('Unallocated --- set l_check_tolerance to false');
15113 			   END IF;
15114 			  l_check_tolerance := false;
15115 		END IF;
15116 	  END IF;
15117 	  END LOOP;
15118 	CLOSE lot_substitution_loose_csr;
15119 	END IF;
15120 	  END IF;
15121 
15122 	IF l_lot_code > 1 THEN -- lot controlled
15123 	   l_table_total      := t_lpn_lot_qty_table.COUNT;
15124 	   IF l_table_total > 0 THEN
15125 		 IF (l_debug = 1) THEN
15126 		   mydebug('building lpn lot vector for ' || l_table_total || '
15127 				   records');
15128 		 END IF;
15129 		FOR l_table_count IN 1 .. l_table_total LOOP
15130 			IF (l_debug = 1) THEN
15131 			  mydebug('index is : ' || l_table_count);
15132 			END IF;
15133 
15134 			INSERT INTO wms_allocations_gtmp(lot_number, primary_quantity,
15135 											 transaction_quantity)
15136 				   values(t_lpn_lot_qty_table(l_table_count).lot_number,
15137 						  t_lpn_lot_qty_table(l_table_count).pri_qty,
15138 						  t_lpn_lot_qty_table(l_table_count).trx_qty);
15139 
15140 		  END LOOP;
15141 	   END IF;
15142 	END IF; -- done populating the lot
15143 
15144 	--populate the lot in lpn vector
15145 	l_table_total      := t_lpn_lot_qty_table.COUNT;
15146 	IF l_table_total > 0 THEN
15147 		  IF (l_debug = 1) THEN
15148 		   mydebug('building lpn lot vector for ' || l_table_total || 'records');
15149 		  END IF;
15150 		  FOR l_table_count IN 1 .. l_table_total LOOP
15151 		  IF l_value = '.,'  THEN
15152 				x_lpn_lot_vector := x_lpn_lot_vector
15153 					 ||t_lpn_lot_qty_table(l_table_count).lot_number ||'@@@@@'
15154 					 ||t_lpn_lot_qty_table(l_table_count).trx_qty||'@@@@@'
15155 					 ||t_lpn_lot_qty_table(l_table_count).trx_qty
15156 					 || '&&&&&';
15157 					ELSE  --bug 6012428
15158 					x_lpn_lot_vector := x_lpn_lot_vector
15159 							 ||t_lpn_lot_qty_table(l_table_count).lot_number ||'@@@@@'
15160 							 ||TO_CHAR(t_lpn_lot_qty_table(l_table_count).trx_qty,'9999999999999999999999.9999999999')||'@@@@@'
15161 							 ||TO_CHAR(t_lpn_lot_qty_table(l_table_count).trx_qty,'9999999999999999999999.9999999999')
15162 							 || '&&&&&';
15163 		 END IF;--bug 6012428
15164 				 l_lot_v   := l_lot_v ||t_lpn_lot_qty_table(l_table_count).lot_number||':'; --Bug 3855835
15165 				 IF (l_debug = 1) THEN
15166 						mydebug('l_lot_v:'||l_lot_v);
15167 				 END IF;
15168 		  END LOOP;
15169 	ELSE
15170 		  x_lpn_lot_vector  := NULL;
15171 	END IF;
15172 
15173 		--Changes starting here for Bug 14003388
15174 		BEGIN
15175 		IF(LENGTH(x_lpn_lot_vector) > 4000) THEN
15176 		x_lpn_lot_vector2 := SUBSTR(x_lpn_lot_vector, 4001, 8000);
15177 		x_lpn_lot_vector3 := SUBSTR(x_lpn_lot_vector, 8001, 12000);
15178 		x_lpn_lot_vector4 := SUBSTR(x_lpn_lot_vector, 12001);
15179 		x_lpn_lot_vector := SUBSTR(x_lpn_lot_vector, 1, 4000);
15180 			IF (l_debug = 1) THEN
15181 				mydebug('x_lpn_lot_vector  : '||x_lpn_lot_vector);
15182 				mydebug('x_lpn_lot_vector2 : '||x_lpn_lot_vector2);
15183 				mydebug('x_lpn_lot_vector3 : '||x_lpn_lot_vector3);
15184 				mydebug('x_lpn_lot_vector4 ::'||x_lpn_lot_vector4);
15185 			END IF;
15186 		ELSE
15187 			IF (l_debug = 1) THEN
15188 			mydebug('populate_lot_lov x_lpn_lot_vector string length less that 4000 chars and needs no trimming: '||LENGTH(x_lpn_lot_vector));
15189 			END IF;
15190 		END IF;
15191 		EXCEPTION
15192 			WHEN OTHERS THEN
15193 			IF (l_debug = 1) THEN
15194 			mydebug('populate_lot_lov x_lpn_lot_vector  string trim had issues: '||LENGTH(x_lpn_lot_vector));
15195 			END IF;
15196 		END;
15197 	--Changes ending here for Bug 14003388
15198 
15199    x_return_status    := fnd_api.g_ret_sts_success;
15200 
15201   EXCEPTION
15202 	WHEN OTHERS THEN
15203 	  IF (l_debug = 1) THEN
15204 		mydebug('Other exception raised : ' || SQLERRM);
15205 	  END IF;
15206 	  x_return_status  := fnd_api.g_ret_sts_unexp_error;
15207 END populate_lot_lov;
15208 
15209 PROCEDURE proc_decrement_allocated_mtlts
15210 			  (p_temp_id			    IN NUMBER
15211 			  ,p_substitute_lots        IN VARCHAR2
15212 			  ,p_confirmed_uom          IN VARCHAR2
15213 			  ,x_return_status          OUT NOCOPY  VARCHAR2)
15214 IS
15215    l_delimiter                   VARCHAR2(30)   :=  ':';
15216    l_lot_number                  VARCHAR2(30)   :=  NULL;
15217    l_subs_lot_qty                NUMBER         :=  NULL;
15218    m                             NUMBER := 1;  -- position of delimiter
15219    n                             NUMBER := 1;  -- Start position for substr or search for delimiter
15220    l_number_format_mask          VARCHAR2(30) :=   'FM9999999999.99999999999999'; --Bug#6244146
15221    -- for Huawei
15222    l_puom                        VARCHAR2(10);
15223    l_tuom                        VARCHAR2(10);
15224    l_suom                        VARCHAR2(3);
15225    l_prim_qty                    NUMBER;
15226    l_sec_qty                     NUMBER;
15227    l_txn_qty                     NUMBER;
15228    l_item_id                     NUMBER;
15229    l_fulfillment_base            VARCHAR2(1);
15230    l_organization_id             NUMBER;
15231 
15232 BEGIN
15233    x_return_status  := l_g_ret_sts_success;
15234    mydebug ('Entered proc_decrement_allocated_mtlts');
15235    mydebug ('Inside proc_decrement_allocated_mtlts p_temp_id        :' || p_temp_id);
15236    mydebug ('Inside proc_decrement_allocated_mtlts p_substitute_lots:' || p_substitute_lots);
15237 
15238    WHILE  (n <> 0)
15239    LOOP
15240 	  n := INSTR(p_substitute_lots,l_delimiter,m,1);
15241 		  mydebug ('A-m:' || m||':A-n:' || n );
15242 		  IF n = 0 THEN -- Last part OF the string
15243 			 EXIT;
15244 		  ELSE
15245 			 l_lot_number :=  substr(p_substitute_lots,m,n-m) ;-- start at M get m-n chrs.
15246 			 IF l_lot_number IS NOT NULL THEN -- Added for 14699845 (Flexible Lot Allocation)
15247 			  m := n+1;
15248 			  n := INSTR(p_substitute_lots,l_delimiter,m,1);
15249 			  IF n = 0 THEN -- Last part OF the string
15250 			   l_subs_lot_qty :=to_number(substr(p_substitute_lots,m,length(p_substitute_lots)) ,l_number_format_mask ) ;
15251 			  ELSE
15252 			   l_subs_lot_qty :=to_number(substr(p_substitute_lots,m,n-m), l_number_format_mask ) ;
15253 			  END IF;
15254 			  m := n+1;
15255 			 END IF; -- Added for 14699845 (Flexible Lot Allocation)
15256 		  END IF;
15257 		  mydebug ('l_lot_number:' || l_lot_number);
15258 
15259 		  mydebug ('B-m:' || m||':B-n:' || n );
15260 		  mydebug ('l_subs_lot_qty:' || l_subs_lot_qty);
15261 
15262 	IF l_lot_number IS NOT NULL THEN
15263 		BEGIN
15264 		IF l_subs_lot_qty >=0 THEN
15265 			SELECT mmtt.ITEM_PRIMARY_UOM_CODE, mmtt.TRANSACTION_UOM, mmtt.SECONDARY_UOM_CODE, mmtt.INVENTORY_ITEM_ID,
15266 					DECODE(mmtt.fulfillment_base, NULL, 'P', mmtt.fulfillment_base), mmtt.organization_id
15267 			INTO l_puom, l_tuom, l_suom, l_item_id, l_fulfillment_base, l_organization_id
15268 			FROM mtl_material_transactions_temp mmtt
15269 			WHERE transaction_temp_id = p_temp_id;
15270 
15271 			mydebug ('l_puom :' || l_puom);
15272 			mydebug ('l_tuom :' || l_tuom);
15273 			mydebug ('l_suom :' || l_suom);
15274 			mydebug ('p_confirmed_uom  :' || p_confirmed_uom);
15275 			mydebug ('fulfillment_base :' || l_fulfillment_base);
15276 
15277 		IF l_fulfillment_base = 'S' THEN --{
15278 			IF p_confirmed_uom <> l_suom THEN
15279 				l_sec_qty := inv_convert.inv_um_convert
15280 						(item_id          => l_item_id
15281 						,precision        => l_g_decimal_precision
15282 						,from_quantity    => l_subs_lot_qty
15283 						,from_unit        => p_confirmed_uom
15284 						,to_unit          => l_suom
15285 						,from_name        => NULL
15286 						,to_name          => NULL);
15287 			ELSE
15288 				l_sec_qty := l_subs_lot_qty;
15289 			END IF;
15290 
15291 				l_prim_qty := inv_convert.inv_um_convert
15292 						(item_id          => l_item_id
15293 						,lot_number		  => l_lot_number
15294 						,organization_id  => l_organization_id
15295 						,precision        => l_g_decimal_precision
15296 						,from_quantity    => l_sec_qty
15297 						,from_unit        => l_suom
15298 						,to_unit          => l_puom
15299 						,from_name        => NULL
15300 						,to_name          => NULL);
15301 
15302 				IF(l_tuom = l_puom) THEN
15303 					l_txn_qty := l_prim_qty;
15304 				ELSIF (l_tuom = l_suom) THEN
15305 					l_txn_qty := l_sec_qty;
15306 				ELSE
15307 					l_txn_qty := inv_convert.inv_um_convert
15308 							(item_id          => l_item_id
15309 							,precision        => l_g_decimal_precision
15310 							,from_quantity    => l_subs_lot_qty
15311 							,from_unit        => p_confirmed_uom
15312 							,to_unit          => l_tuom
15313 							,from_name        => NULL
15314 							,to_name          => NULL);
15315 				END IF;
15316 
15317 		ELSE
15318 			IF p_confirmed_uom <> l_puom THEN
15319 				l_prim_qty := inv_convert.inv_um_convert
15320 							(item_id           => l_item_id
15321 							,precision        => l_g_decimal_precision
15322 							,from_quantity    => l_subs_lot_qty
15323 							,from_unit        => p_confirmed_uom
15324 							,to_unit          => l_puom
15325 							,from_name        => NULL
15326 							,to_name          => NULL);
15327 			ELSE
15328 				l_prim_qty := l_subs_lot_qty;
15329 			END IF;
15330 
15331 			IF(l_suom IS NOT NULL) THEN
15332 				IF p_confirmed_uom <> l_suom THEN
15333 					l_sec_qty := inv_convert.inv_um_convert
15334 								(item_id           => l_item_id
15335 								,precision        => l_g_decimal_precision
15336 								,from_quantity    => l_subs_lot_qty
15337 								,from_unit        => p_confirmed_uom
15338 								,to_unit          => l_suom
15339 								,from_name        => NULL
15340 								,to_name          => NULL);
15341 				ELSE
15342 					l_sec_qty := l_subs_lot_qty;
15343 				END IF;
15344 			END IF;
15345 
15346 			IF p_confirmed_uom <> l_tuom THEN
15347 				IF(l_tuom = l_puom) THEN
15348 					l_txn_qty := l_prim_qty;
15349 				ELSIF (l_tuom = l_suom) THEN
15350 					l_txn_qty := l_sec_qty;
15351 				ELSE
15352 					l_txn_qty := inv_convert.inv_um_convert
15353 							(item_id          => l_item_id
15354 							,precision        => l_g_decimal_precision
15355 							,from_quantity    => l_subs_lot_qty
15356 							,from_unit        => p_confirmed_uom
15357 							,to_unit          => l_tuom
15358 							,from_name        => NULL
15359 							,to_name          => NULL);
15360 				END IF;
15361 			ELSE
15362 				l_txn_qty := l_subs_lot_qty;
15363 			END IF;
15364 		END IF; --}
15365 
15366 		mydebug ('Inside proc_decrement_allocated_mtlts before l_puom :' || l_puom);
15367 		mydebug ('Inside proc_decrement_allocated_mtlts before update to MTLT l_prim_qty :' || l_prim_qty);
15368 		mydebug ('Inside proc_decrement_allocated_mtlts before l_suom :' || l_suom);
15369 		mydebug ('Inside proc_decrement_allocated_mtlts before update to MTLT l_sec_qty  :' || l_sec_qty);
15370 		mydebug ('Inside proc_decrement_allocated_mtlts before l_tuom :' || l_tuom);
15371 		mydebug ('Inside proc_decrement_allocated_mtlts before update to MTLT l_txn_qty  :' || l_txn_qty);
15372 
15373 	  UPDATE mtl_transaction_lots_temp
15374 	  SET transaction_quantity = l_txn_qty,
15375 	   primary_quantity = l_prim_qty,
15376 	   secondary_quantity = l_sec_qty
15377 	  WHERE transaction_temp_id = p_temp_id
15378 	  AND lot_number = l_lot_number;
15379 
15380 		  DELETE FROM mtl_transaction_lots_temp
15381 		  WHERE transaction_quantity = 0
15382 		  AND transaction_temp_id = p_temp_id
15383 		  AND lot_number = l_lot_number;
15384 		  END IF;
15385 	  EXCEPTION
15386 	  WHEN OTHERS THEN
15387 	  mydebug('Exception while updating MTLT in proc_decrement_allocated_mtlts ' || SQLCODE);
15388 	  END;
15389   END IF;
15390    END LOOP;
15391 
15392 EXCEPTION
15393    WHEN OTHERS THEN
15394 		x_return_status  := l_g_ret_sts_unexp_error;
15395 		mydebug('Exception in proc_decrement_allocated_mtlts' );
15396 END proc_decrement_allocated_mtlts;
15397 
15398 --/* Bug 9448490 Lot Substitution Project */ end
15399 --BUG12670785 BEGIN
15400 FUNCTION check_if_lot_is_substituted (p_substitute_lots VARCHAR2, p_current_lot VARCHAR2 , p_full_lot_allocation VARCHAR2)-- Modified for 14699845 (Flexible Lot Allocation) Added a new parameter
15401 RETURN BOOLEAN IS
15402 
15403    l_delimiter                   VARCHAR2(30)   :=  ':';
15404    l_lot_number                  VARCHAR2(30)   :=  NULL;
15405    l_lot_prim_qty                NUMBER         :=  NULL;
15406    l_number_format_mask          VARCHAR2(30) :=   'FM9999999999.99999999999999';
15407    m                             NUMBER := 1;  -- position of delimiter
15408    n                             NUMBER := 1;  -- Start position for substr or search for delimiter
15409 
15410 BEGIN
15411 	mydebug ('Inside check_if_lot_is_substituted Printing the lot vecotr as ' || p_substitute_lots||' and value of current lot in question ' || p_current_lot );
15412 
15413    -- Start changes for 14699845 (Flexible Lot Allocation)
15414    IF (p_substitute_lots IS NULL AND NOT (p_full_lot_allocation ='Y')) THEN
15415 	mydebug ('Inside check_if_lot_is_substituted p_substitute_lots IS NULL and Full Lot Allocation is not Y. Hence returning true ');
15416 	RETURN TRUE;
15417    END IF;
15418    -- End changes for 14699845 (Flexible Lot Allocation)
15419    WHILE  (n <> 0)
15420 	LOOP
15421 	  n := INSTR(p_substitute_lots,l_delimiter,m,1);
15422 		  mydebug ('check_if_lot_is_substituted The value of A-m:' || m||' and value of :A-n:' || n );
15423 		  IF n = 0 THEN -- Last part OF the string
15424 			 EXIT;
15425 		  ELSE
15426 			 l_lot_number :=  substr(p_substitute_lots,m,n-m) ;-- start at M get m-n chrs.
15427 			 IF (l_lot_number = p_current_lot or l_lot_number IS NULL) THEN
15428 				mydebug ('A value of lot number in the substitute lot matches the current lot number so returning falsea' || l_lot_number);
15429 				RETURN FALSE;
15430 			 END IF;
15431 			 m := n+1;
15432 			 n := INSTR(p_substitute_lots,l_delimiter,m,1);
15433 			 IF n = 0 THEN -- Last part OF the string
15434 			 l_lot_prim_qty :=to_number(substr(p_substitute_lots,m,length(p_substitute_lots)) ,l_number_format_mask ) ;
15435 
15436 			 ELSE
15437 			 l_lot_prim_qty :=to_number(substr(p_substitute_lots,m,n-m), l_number_format_mask ) ;
15438 			 END IF;
15439 			 m := n+1;
15440 		  END IF;
15441 		  mydebug ('The value of l_lot_number:' || l_lot_number);
15442 
15443 		  mydebug ('In check_if_lot_is_substituted B-m:' || m||':B-n:' || n );
15444 	END LOOP;
15445 
15446 	mydebug ('The LOOP finished looping through the vector and did not find the lot in question so returning true');
15447 	RETURN TRUE;
15448 END check_if_lot_is_substituted;
15449 --BUG12670785 END
15450 
15451 --HWSNIssue 13860546
15452 PROCEDURE insert_serials_temp (
15453    p_transaction_temp_id   IN             NUMBER,
15454    p_lpn_id                IN             NUMBER,
15455    p_org_id                IN             NUMBER,
15456    p_item_id               IN             NUMBER,
15457    p_rev                   IN             VARCHAR2,
15458    p_confirmed_trx_qty     IN             NUMBER,
15459    x_return_status         OUT  NOCOPY    VARCHAR2,
15460    x_msg                   OUT  NOCOPY    VARCHAR2
15461 )
15462 IS
15463    CURSOR c_lots_in_lpn
15464    IS
15465 	  SELECT mtlt.lot_number, 'Y' allocated_lot
15466 		FROM wms_lpn_contents wlc, mtl_transaction_lots_temp mtlt
15467 	   WHERE wlc.parent_lpn_id = p_lpn_id
15468 		 AND wlc.organization_id = p_org_id
15469 		 AND wlc.inventory_item_id = p_item_id
15470 		 AND NVL (wlc.revision, '-999') = NVL (p_rev, '-999')
15471 		 AND mtlt.transaction_temp_id = p_transaction_temp_id
15472 		 AND mtlt.lot_number = wlc.lot_number
15473 	  UNION ALL
15474 	  SELECT lot_number, 'N' allocated_lot
15475 		FROM wms_lpn_contents wlc
15476 	   WHERE wlc.parent_lpn_id = p_lpn_id
15477 		 AND wlc.organization_id = p_org_id
15478 		 AND wlc.inventory_item_id = p_item_id
15479 		 AND NVL (wlc.revision, '-999') = NVL (p_rev, '-999')
15480 		 AND NOT EXISTS (
15481 				SELECT 1
15482 				  FROM mtl_transaction_lots_temp mtlt
15483 				 WHERE mtlt.transaction_temp_id = p_transaction_temp_id
15484 				   AND mtlt.lot_number = wlc.lot_number);
15485 
15486    CURSOR c_serials_in_lpn (v_lpn_id NUMBER, v_lot_number VARCHAR2)
15487    IS
15488 	  SELECT serial_number
15489 		FROM mtl_serial_numbers
15490 	   WHERE lpn_id = v_lpn_id
15491 		 AND lot_number = v_lot_number
15492 		 AND inventory_item_id = p_item_id
15493 		 AND NVL (revision, '-999') = NVL (p_rev, '-999')
15494 		 AND current_organization_id = p_org_id
15495 		 AND (group_mark_id IS NULL
15496 		  OR group_mark_id = -1
15497 		 ); --Added this Condition as while inserting serials, the same is not checked
15498 
15499    l_serial_cnt          NUMBER := 0;
15500    l_serial_temp_id      NUMBER;
15501    l_ser_return_status   VARCHAR2 (1);
15502    l_debug               NUMBER    := NVL (fnd_profile.VALUE ('INV_DEBUG_TRACE'), 0);
15503    l_ser_msg             VARCHAR2 (240);
15504 BEGIN
15505    x_return_status := l_g_ret_sts_success;
15506 	  mydebug ('In insert_serials_temp');
15507 	  mydebug ('insert_serials_temp: p_transaction_temp_id : ' || p_transaction_temp_id);
15508 	  mydebug ('insert_serials_temp: p_lpn_id : ' || p_lpn_id);
15509 	  mydebug ('insert_serials_temp: p_org_id : ' || p_org_id);
15510 	  mydebug ('insert_serials_temp: p_item_id : ' || p_item_id);
15511 	  mydebug ('insert_serials_temp: p_rev : ' || p_rev);
15512 	/* Loop through all the Lots in the LPN and insert serials associated with the Lots.
15513 	   For lots which are allocated, we pass the serial_transaction_temp_id as NULL and the api itself
15514 	   generates one. In case we have an unallocated Lot in the LPN, the serial_transaction_temp_id
15515 	   is passed as the transaction_temp_id of the task itself.
15516 	*/
15517    FOR rec_lots_in_lpn IN c_lots_in_lpn
15518    LOOP
15519 	  FOR rec_serials_in_lpn IN c_serials_in_lpn (p_lpn_id, rec_lots_in_lpn.lot_number)
15520 	  LOOP
15521 		 IF (rec_lots_in_lpn.allocated_lot = 'Y')
15522 		 THEN
15523 		 mydebug ('insert_serials_temp: Its an allocated Lot');
15524 		 l_serial_temp_id := NULL;
15525 		 ELSE
15526 		 mydebug ('insert_serials_temp: Its an unallocated Lot');
15527 		 l_serial_temp_id := p_transaction_temp_id;
15528 		 END IF;
15529 
15530 			mydebug ('insert_serials_temp: Inside Loop');
15531 			mydebug ('insert_serials_temp: Lot Number : ' || rec_lots_in_lpn.lot_number);
15532 			mydebug ('insert_serials_temp: Serial Number : ' || rec_serials_in_lpn.serial_number);
15533 			mydebug ('insert_serials_temp: l_serial_temp_id : ' || l_serial_temp_id);
15534 			mydebug ('insert_serials_temp: p_rev : ' || p_rev);
15535 
15536 		 insert_serial (p_serial_transaction_temp_id      => l_serial_temp_id,
15537 						p_organization_id                 => p_org_id,
15538 						p_item_id                         => p_item_id,
15539 						p_revision                        => p_rev,
15540 						p_lot                             => rec_lots_in_lpn.lot_number,
15541 						p_transaction_temp_id             => p_transaction_temp_id,
15542 						p_created_by                      => fnd_global.user_id,
15543 						p_from_serial                     => rec_serials_in_lpn.serial_number,
15544 						p_to_serial                       => rec_serials_in_lpn.serial_number,
15545 						p_status_id                       => NULL,
15546 						x_return_status                   => l_ser_return_status,
15547 						x_msg_data                        => l_ser_msg
15548 					   );
15549 		 l_serial_cnt := l_serial_cnt + 1;
15550 
15551 		 IF l_ser_return_status <> l_g_ret_sts_success
15552 		 THEN
15553 			x_return_status := fnd_api.g_ret_sts_error;
15554 			x_msg := 'Error while calling insert_serial';
15555 
15556 			   mydebug ('insert_serials_temp: Error while calling insert_serial');
15557 			   mydebug ('insert_serials_temp: l_ser_return_status ' || l_ser_return_status);
15558 			   mydebug ('insert_serials_temp: l_ser_msg ' || l_ser_msg);
15559 
15560 			RAISE fnd_api.g_exc_unexpected_error;
15561 		 END IF;
15562 	  END LOOP;
15563    END LOOP;
15564 
15565    IF l_serial_cnt <> p_confirmed_trx_qty
15566    THEN
15567 	  x_return_status := fnd_api.g_ret_sts_error;
15568 	  x_msg := 'The total qty does not match the number of serials';
15569 
15570 		 mydebug ('insert_serials_temp: p_confirmed_trx_qty ' || p_confirmed_trx_qty);
15571 		 mydebug ('insert_serials_temp: l_serial_cnt ' || l_serial_cnt);
15572 
15573 	  RAISE fnd_api.g_exc_unexpected_error;
15574    END IF;
15575 END insert_serials_temp;
15576 
15577 FUNCTION lpn_has_unalloc_lots (
15578    p_lpn_id                IN   NUMBER,
15579    p_organization_id       IN   NUMBER,
15580    p_item_id               IN   NUMBER,
15581    p_rev                   IN   VARCHAR2,
15582    p_transaction_temp_id   IN   NUMBER
15583 )
15584    RETURN BOOLEAN
15585 IS
15586    l_unalloc_lots   NUMBER;
15587 BEGIN
15588 	  mydebug ('Inside lpn_has_unalloc_lots ');
15589 	  mydebug ('lpn_has_unalloc_lots: p_lpn_id: ' || p_lpn_id);
15590 	  mydebug ('lpn_has_unalloc_lots: p_organization_id: ' || p_organization_id);
15591 	  mydebug ('lpn_has_unalloc_lots: p_transaction_temp_id: ' || p_transaction_temp_id);
15592 
15593    SELECT 1
15594 	 INTO l_unalloc_lots
15595 	 FROM wms_lpn_contents wlc
15596 	WHERE wlc.parent_lpn_id = p_lpn_id -- add revision I think No need to add rev since x_match should take care.
15597 	  AND wlc.inventory_item_id = p_item_id
15598 	  AND NVL(wlc.revision,'-999') = NVL(p_rev,'-999')
15599 	  AND wlc.organization_id = p_organization_id
15600 	  AND NOT EXISTS (
15601 			 SELECT 1
15602 			   FROM mtl_transaction_lots_temp mtlt
15603 			  WHERE mtlt.transaction_temp_id = p_transaction_temp_id
15604 				AND mtlt.lot_number = wlc.lot_number);
15605 
15606 	  mydebug ('lpn_has_unalloc_lots: Returning TRUE ');
15607 
15608    RETURN TRUE;
15609 EXCEPTION
15610    WHEN TOO_MANY_ROWS
15611    THEN
15612 	  mydebug ('lpn_has_unalloc_lots: Returning TRUE ');
15613 	  RETURN TRUE;
15614    WHEN OTHERS
15615    THEN
15616 	  mydebug ('lpn_has_unalloc_lots: Returning FALSE ');
15617 	  RETURN FALSE;
15618 END lpn_has_unalloc_lots;
15619 --HWSNIssue 13860546
15620 
15621 -- Start changes for 14699845 (Flexible Lot Allocation)
15622 PROCEDURE Cleanup_LS_FlexiAlloc (p_transaction_temp_id IN NUMBER ,
15623 								 p_transaction_header_id IN NUMBER,
15624 								 p_serial_controlled    IN VARCHAR2,
15625 								 x_return_status OUT NOCOPY VARCHAR2)
15626 IS
15627 p_api_name VARCHAR2(50) := 'Cleanup_LS_FlexiAlloc';
15628 l_progress NUMBER;
15629 
15630 BEGIN
15631 x_return_status := fnd_api.g_ret_sts_success;
15632 
15633 mydebug (p_api_name||' Starting to revert Picked Lots and Serials');
15634 
15635   IF (p_serial_controlled = 'Y') THEN
15636   mydebug (p_api_name||' Item is Serial Controlled');
15637 -- Unmark the Serials
15638 				   UPDATE  MTL_SERIAL_NUMBERS
15639 				   SET     group_mark_id   = NULL
15640 						 , last_updated_by = fnd_global.user_id
15641 						 , last_update_date = SYSDATE
15642 				   WHERE  (current_organization_id
15643 						  ,inventory_item_id
15644 						  ,serial_number)
15645 					  IN  (SELECT  mmtt.organization_id
15646 								  ,mmtt.inventory_item_id
15647 								  ,msnt.fm_serial_number
15648 							 FROM  mtl_transaction_lots_temp      mtlt
15649 								  ,mtl_serial_numbers_temp        msnt
15650 								  ,mtl_material_transactions_temp mmtt
15651 							WHERE  mmtt.transaction_header_id      = p_transaction_header_id
15652 							  AND  mmtt.transaction_temp_id        = mtlt.transaction_temp_id
15653 							  AND  mtlt.serial_transaction_temp_id = msnt.transaction_temp_id
15654 							  UNION --For lot substitution of lot and serial items when serial are confirmed we insert msnt with mmtt.transaction_temp_id
15655 									--at that time their is no MTLT for substituted lot..
15656 							  SELECT  mmtt.organization_id
15657 								  ,mmtt.inventory_item_id
15658 								  ,msnt.fm_serial_number
15659 							 FROM  mtl_serial_numbers_temp        msnt
15660 								  ,mtl_material_transactions_temp mmtt
15661 							WHERE  mmtt.transaction_header_id      = p_transaction_header_id
15662 							  AND  mmtt.transaction_temp_id        = msnt.transaction_temp_id
15663 							  );
15664   mydebug (p_api_name||' Updated '||SQL%ROWCOUNT||' rows in MSN');
15665 
15666 	DELETE FROM mtl_serial_numbers_temp
15667 	 WHERE transaction_temp_id IN ((SELECT serial_transaction_temp_id
15668 									  FROM mtl_transaction_lots_temp
15669 									 WHERE transaction_temp_id IN (SELECT transaction_temp_id
15670 																	 FROM mtl_material_transactions_temp
15671 																	WHERE transaction_header_id = p_transaction_header_id))
15672 								   UNION
15673 								   (SELECT transaction_temp_id
15674 									  FROM mtl_material_transactions_temp
15675 									 WHERE transaction_header_id = p_transaction_header_id));
15676 
15677   mydebug (p_api_name||' Deleted '||SQL%ROWCOUNT||' rows from MSNT');
15678   END IF;
15679 
15680  DELETE FROM mtl_transaction_lots_temp
15681   WHERE transaction_temp_id IN (SELECT transaction_temp_id
15682 								  FROM mtl_material_transactions_temp
15683 								 WHERE transaction_header_id = p_transaction_header_id);
15684 
15685 EXCEPTION
15686 WHEN OTHERS THEN
15687 NULL;
15688 END;
15689 -- End changes for 14699845 (Flexible Lot Allocation)
15690 
15691 PROCEDURE item_lot_divisible_flag (
15692 	 p_org_id IN NUMBER
15693 	,p_inventory_item_id IN NUMBER
15694 	,x_lot_divisible_flag OUT NOCOPY VARCHAR2)
15695 	IS
15696 
15697 l_lot_divisible_flag    VARCHAR2(1);
15698 
15699 CURSOR get_item_details( org_id IN NUMBER
15700 					   , item_id IN NUMBER) IS
15701 SELECT  NVL(lot_divisible_flag, 'Y')
15702 FROM mtl_system_items_b
15703 WHERE organization_id = org_id
15704 AND inventory_item_id = item_id;
15705 
15706 BEGIN
15707 
15708 	OPEN get_item_details(p_org_id
15709 						 ,p_inventory_item_id);
15710 
15711 	FETCH get_item_details INTO l_lot_divisible_flag;
15712 	IF (get_item_details%NOTFOUND) THEN
15713 		CLOSE get_item_details;
15714 		FND_MESSAGE.SET_NAME('INV','ITEM_NOT_FOUND');
15715 		FND_MESSAGE.SET_TOKEN('ORGANIZATION_ID', p_org_id);
15716 		FND_MESSAGE.SET_TOKEN('INVENTORY_ITEM_ID', p_inventory_item_id);
15717 		FND_MSG_PUB.ADD;
15718 		RAISE FND_API.G_EXC_ERROR;
15719 	END IF;
15720 
15721 	CLOSE get_item_details;
15722 	x_lot_divisible_flag := l_lot_divisible_flag;
15723 
15724 EXCEPTION
15725 WHEN FND_API.G_EXC_ERROR THEN
15726 	mydebug('item_lot_divisible_flag: FND_API.G_EXC_ERROR ');
15727 	x_lot_divisible_flag := 'N';
15728 END item_lot_divisible_flag;
15729 
15730 --16267113
15731 PROCEDURE Update_Tree_Flex_Lot (p_fromlpn_id            IN NUMBER,
15732 								p_org_id                IN NUMBER,
15733 								p_item_id               IN NUMBER,
15734 								p_rev                   IN VARCHAR2,
15735 								p_trx_qty               IN NUMBER,
15736 								p_trx_uom               IN VARCHAR2,
15737 								p_sec_qty               IN NUMBER,
15738 								p_sec_uom               IN VARCHAR2,
15739 								p_lot                   IN VARCHAR2,
15740 								p_temp_id               IN NUMBER,
15741 								p_transaction_type_id   IN NUMBER,
15742 								p_user_id               IN NUMBER,
15743 								p_transaction_action_id IN NUMBER ,
15744 								p_confirmed_sub         IN VARCHAR2,
15745 								p_confirmed_loc_id      IN NUMBER,
15746 								p_full_lot_allocation   IN VARCHAR2,
15747 								p_fulfillment_base      IN VARCHAR2,
15748 								x_lpn_lot_vector        OUT NOCOPY VARCHAR2,
15749 								x_lpn_lot_vector2       OUT NOCOPY VARCHAR2,
15750 								x_lpn_lot_vector3       OUT NOCOPY VARCHAR2,
15751 								x_lpn_lot_vector4       OUT NOCOPY VARCHAR2,
15752 								x_return_status         OUT NOCOPY VARCHAR2)
15753 IS
15754 
15755 	l_proc_name              VARCHAR2(30) := 'Update_Tree_Flex_Lot' ;
15756 	l_msg_cnt                NUMBER;
15757 	l_msg_data               VARCHAR2(2000);
15758 	l_return_status          VARCHAR2(240);
15759 	l_pr_qty                 NUMBER;
15760 	l_primary_uom            VARCHAR2(3);
15761 	l_mmtt_qty               NUMBER;
15762 
15763 
15764 	l_qoh                    NUMBER;
15765 	l_rqoh                   NUMBER;
15766 	l_qr                     NUMBER;
15767 	l_qs                     NUMBER;
15768 	l_att                    NUMBER;
15769 	l_atr                    NUMBER;
15770 	l_allocated_lpn_id       NUMBER;
15771 	l_table_index            NUMBER         := 0;
15772 	l_table_total            NUMBER         := 0;
15773 	l_table_count            NUMBER;
15774 	l_value VARCHAR2(3);
15775 
15776 	l_transaction_action_id         NUMBER;
15777 	l_txn_source_type_id            NUMBER;
15778 	l_sqoh                   NUMBER;
15779 	l_srqoh                  NUMBER;
15780 	l_sqr                    NUMBER;
15781 	l_sqs                    NUMBER;
15782 	l_satt                   NUMBER;
15783 	l_satr                   NUMBER;
15784 	l_lpn_sub                VARCHAR2(60);
15785 	l_lpn_loc                NUMBER;
15786 	l_mmtt_sub               VARCHAR2(60);
15787 	l_mmtt_loc               VARCHAR2(60);
15788     l_sec_qty                NUMBER;
15789     b_is_revision_control    BOOLEAN;
15790 	b_is_serial_control      BOOLEAN := FALSE;
15791 
15792 
15793 	l_debug                  NUMBER         := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
15794 
15795   BEGIN
15796 	x_return_status    := fnd_api.g_ret_sts_success;
15797 	t_lpn_lot_qty_table.DELETE;
15798 
15799     IF p_rev IS NULL THEN
15800 		b_is_revision_control  := FALSE;
15801 	ELSE
15802 		b_is_revision_control  := TRUE;
15803 	END IF;
15804 
15805 	inv_quantity_tree_pub.clear_quantity_cache;
15806 
15807 	 SELECT primary_uom_code
15808 	   INTO l_primary_uom
15809 	   FROM mtl_system_items
15810 	  WHERE organization_id = p_org_id
15811 		AND inventory_item_id = p_item_id;
15812 
15813 
15814 	   SELECT mmtt.subinventory_code
15815 			 ,mmtt.locator_id
15816 			 ,allocated_lpn_id
15817 	  INTO l_mmtt_sub
15818 		 , l_mmtt_loc
15819 		 , l_allocated_lpn_id
15820 	  FROM mtl_material_transactions_temp mmtt
15821 	 WHERE mmtt.transaction_temp_id = p_temp_id;
15822 
15823 			  mydebug('p_fromlpn_id:            '|| p_fromlpn_id);
15824 			  mydebug('p_org_id:                '|| p_org_id);
15825 			  mydebug('p_item_id:               '|| p_item_id);
15826 			  mydebug('p_rev:                   '|| p_rev);
15827 			  mydebug('p_trx_qty:               '|| p_trx_qty);
15828 			  mydebug('p_trx_uom:               '|| p_trx_uom);
15829 			  mydebug('p_sec_qty:               '|| p_sec_qty);
15830 			  mydebug('p_sec_uom:               '|| p_sec_uom);
15831 			  mydebug('Lot Number :             '|| p_lot);
15832 			  mydebug('p_temp_id:               '|| p_temp_id);
15833 			  mydebug('p_transaction_type_id:   '|| p_transaction_type_id);
15834 			  mydebug('p_transaction_action_id: '|| p_transaction_action_id);
15835 			  mydebug('p_confirmed_sub:         '|| p_confirmed_sub);
15836 			  mydebug('p_confirmed_loc_id:      '|| p_confirmed_loc_id);
15837 			  mydebug('p_full_lot_allocation:   '|| p_full_lot_allocation);
15838 			  mydebug('p_fulfillment_base:      '|| p_fulfillment_base);
15839 
15840 	 IF(p_fromlpn_id IS NOT NULL) THEN
15841 		  SELECT subinventory_code
15842 		   , locator_id
15843 		INTO l_lpn_sub
15844 		   , l_lpn_loc
15845 		FROM wms_license_plate_numbers wlpn
15846 	   WHERE wlpn.organization_id = p_org_id
15847 		 AND wlpn.lpn_id = p_fromlpn_id;
15848 	 END IF;
15849 
15850 	select value
15851 	into l_value
15852 	from v$nls_parameters
15853 	where parameter = 'NLS_NUMERIC_CHARACTERS';
15854 
15855 	IF p_fulfillment_base = 'S' THEN --{
15856 		IF (p_trx_uom <> p_sec_uom) THEN
15857 			l_sec_qty := inv_convert.inv_um_convert(
15858 				 item_id        => p_item_id
15859 				,precision      => null
15860 				,from_quantity  => p_trx_qty
15861 				,from_unit      => p_trx_uom
15862 				,to_unit        => p_sec_uom
15863 				,from_name      => null
15864 				,to_name        => null);
15865 
15866 			mydebug('In Update_Tree_Flex_Lot Fulfillment basis is S and transaction UOM is different than secondary UOM');
15867 			mydebug('p_trx_qty/l_sec_qty in secondary uom is :'|| l_sec_qty);
15868 		ELSE
15869 			l_sec_qty     := p_trx_qty;
15870 			mydebug('In Update_Tree_Flex_Lot Fulfillment basis is S and transaction UOM is same than secondary UOM');
15871 			mydebug('p_trx_qty/l_sec_qty in secondary uom is :'|| l_sec_qty);
15872 
15873 		END IF;
15874 			l_pr_qty     := inv_convert.inv_um_convert(
15875 				 item_id        => p_item_id
15876 				,precision      => null
15877 				,from_quantity  => l_sec_qty
15878 				,from_unit      => p_sec_uom
15879 				,to_unit        => l_primary_uom
15880 				,from_name      => null
15881 				,to_name        => null);
15882 
15883 	ELSE
15884 		IF (p_trx_uom <> l_primary_uom) THEN
15885 			l_pr_qty := inv_convert.inv_um_convert(
15886 				 item_id        => p_item_id
15887 				,precision      => null
15888 				,from_quantity  => p_trx_qty
15889 				,from_unit      => p_trx_uom
15890 				,to_unit        => l_primary_uom
15891 				,from_name      => null
15892 				,to_name        => null);
15893 
15894 			IF (l_debug = 1) THEN
15895 			 mydebug('In Update_Tree_Flex_Lot Fulfillment basis is P and transaction UOM is different than Primary UOM');
15896 			 mydebug('p_trx_qty in primary uom is l_pri_qty:'|| l_pr_qty);
15897 			END IF;
15898 		ELSE
15899 			l_pr_qty               := p_trx_qty;
15900 			IF (l_debug = 1) THEN
15901 			mydebug('In Update_Tree_Flex_Lot Fulfillment basis is P and transaction UOM is same as Primary UOM');
15902 			mydebug('l_pr_qty is the same as p_trx_qty      :'|| l_pr_qty);
15903 			END IF;
15904 		END IF;
15905 			l_sec_qty              := inv_convert.inv_um_convert(
15906 				 item_id        => p_item_id
15907 				,precision      => null
15908 				,from_quantity  => l_pr_qty
15909 				,from_unit      => l_primary_uom
15910 				,to_unit        => p_sec_uom
15911 				,from_name      => null
15912 				,to_name        => null);
15913 	END IF;
15914 
15915 
15916 
15917 	-- p_trx_qty was passed in transaction_uom, need to convert it to primary_uom
15918 	IF (l_debug = 1) THEN
15919 		  mydebug('p_trx_uom :'|| p_trx_uom);
15920 		  mydebug('l_primary_uom :'|| l_primary_uom);
15921 		  mydebug('p_trx_qty in transaction uom:'|| p_trx_qty);
15922 	END IF;
15923 
15924 			IF inv_cache.set_mtt_rec(p_transaction_type_id) THEN
15925 			  l_transaction_action_id         :=  inv_cache.mtt_rec.transaction_action_id;
15926               l_txn_source_type_id            :=  inv_cache.mtt_rec.transaction_source_type_id;
15927             ELSE
15928 			  RAISE fnd_api.g_exc_unexpected_error;
15929             END IF;
15930 
15931 			IF(l_debug=1) THEN
15932 			inv_log_util.trace( 'Value of p_transaction_type_id          :  '  ||p_transaction_type_id,   'Inside Update_Tree_Flex_Lot  ',   9);
15933 			inv_log_util.trace( 'Value of l_transaction_action_id        :  '  ||l_transaction_action_id, 'Inside Update_Tree_Flex_Lot  ',   9);
15934 			inv_log_util.trace( 'Value of l_txn_source_type_id           :  '  ||l_txn_source_type_id,    'Inside Update_Tree_Flex_Lot  ',   9);
15935 			END IF;
15936 
15937 	IF (p_transaction_action_id = 28
15938 	OR (l_txn_source_type_id = 4  AND p_transaction_action_id =1)
15939 	OR (l_txn_source_type_id = 4  AND p_transaction_action_id =2)
15940 	OR (l_txn_source_type_id = 13 AND p_transaction_action_id =2 AND p_full_lot_allocation IN ('N', 'P'))  --BackFlush Transfer Added for 14699845 (Flexible Lot Allocation)
15941 	OR (l_txn_source_type_id = 5  AND p_transaction_action_id =1 AND p_full_lot_allocation IN ('N', 'P'))) --WIP Component Issue Added for 14699845 (Flexible Lot Allocation)
15942 
15943 	THEN
15944 
15945 		   IF (l_debug = 1) THEN
15946 			  mydebug('Lot Number : ' || p_lot);
15947 			  mydebug('p_org_id ' || p_org_id);
15948 			  mydebug('p_item_id ' || p_item_id);
15949 			  mydebug('NVL(p_rev, NULL) '|| p_rev);
15950 			  mydebug('p_confirmed_sub '|| p_confirmed_sub);
15951 			  mydebug('p_confirmed_loc_id ' || p_confirmed_loc_id);
15952 			  mydebug('p_fromlpn_id ' || p_fromlpn_id);
15953 		   END IF;
15954 
15955 		IF (p_full_lot_allocation IN ('N', 'P')) THEN
15956 		  IF NVL(l_allocated_lpn_id, 0) = p_fromlpn_id THEN
15957 			--from lpn is the same as allocated_lpn, we need to update qty tree as negative qty
15958 			-- in order to get correct att.
15959 			inv_quantity_tree_pub.update_quantities(
15960 			  p_api_version_number         => 1.0
15961 			, p_init_msg_lst               => fnd_api.g_false
15962 			, x_return_status              => l_return_status
15963 			, x_msg_count                  => l_msg_cnt
15964 			, x_msg_data                   => l_msg_data
15965 			, p_organization_id            => p_org_id
15966 			, p_inventory_item_id          => p_item_id
15967 			, p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
15968 			, p_is_revision_control        => b_is_revision_control
15969 			, p_is_lot_control             => TRUE
15970 			, p_is_serial_control          => b_is_serial_control
15971 			, p_revision                   => NVL(p_rev, NULL)
15972 			, p_lot_number                 => NULL
15973 			, p_subinventory_code          => l_lpn_sub
15974 			, p_locator_id                 => l_lpn_loc
15975 			, p_primary_quantity           => -l_pr_qty
15976 			, p_secondary_quantity         => -l_sec_qty
15977 			, p_quantity_type              => inv_quantity_tree_pvt.g_qs_txn
15978 			, x_qoh                        => l_qoh
15979 			, x_rqoh                       => l_rqoh
15980 			, x_qr                         => l_qr
15981 			, x_qs                         => l_qs
15982 			, x_att                        => l_att
15983 			, x_atr                        => l_atr
15984 			, x_sqoh                       => l_sqoh
15985 			, x_srqoh                      => l_srqoh
15986 			, x_sqr                        => l_sqr
15987 			, x_sqs                        => l_sqs
15988 			, x_satt                       => l_satt
15989 			, x_satr                       => l_satr
15990 			, p_lpn_id                     => p_fromlpn_id
15991 			);
15992 
15993 			IF (l_return_status = fnd_api.g_ret_sts_success) THEN
15994 			  IF (l_debug = 1) THEN
15995 				mydebug('update qty tree for FlexiLotAlloc lpn l_att:'  || l_att  || ' at Locator Level');
15996 				mydebug('update qty tree for FlexiLotAlloc lpn l_satt:' || l_satt || ' at Locator Level');
15997 			  END IF;
15998 			ELSE
15999 			  IF (l_debug = 1) THEN
16000 				mydebug('calling update qty tree with lpn for FlexiLotAlloc failed ');
16001 			  END IF;
16002 
16003 			  fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
16004 			  fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
16005 			  fnd_msg_pub.ADD;
16006 			  RAISE fnd_api.g_exc_error;
16007 			END IF;
16008 
16009 		  ELSIF ( Nvl(l_lpn_sub, p_confirmed_sub) = l_mmtt_sub AND Nvl(l_lpn_loc, p_confirmed_loc_id) = l_mmtt_loc ) THEN
16010 			inv_quantity_tree_pub.update_quantities(
16011 			  p_api_version_number         => 1.0
16012 			, p_init_msg_lst               => fnd_api.g_false
16013 			, x_return_status              => l_return_status
16014 			, x_msg_count                  => l_msg_cnt
16015 			, x_msg_data                   => l_msg_data
16016 			, p_organization_id            => p_org_id
16017 			, p_inventory_item_id          => p_item_id
16018 			, p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
16019 			, p_is_revision_control        => b_is_revision_control
16020 			, p_is_lot_control             => TRUE
16021 			, p_is_serial_control          => b_is_serial_control
16022 			, p_revision                   => NVL(p_rev, NULL)
16023 			, p_lot_number                 => NULL
16024 			, p_subinventory_code          => l_mmtt_sub
16025 			, p_locator_id                 => l_mmtt_loc
16026 			, p_primary_quantity           => -l_pr_qty
16027 			, p_secondary_quantity         => -l_sec_qty
16028 			, p_quantity_type              => inv_quantity_tree_pvt.g_qs_txn
16029 			, x_qoh                        => l_qoh
16030 			, x_rqoh                       => l_rqoh
16031 			, x_qr                         => l_qr
16032 			, x_qs                         => l_qs
16033 			, x_att                        => l_att
16034 			, x_atr                        => l_atr
16035 			, x_sqoh                       => l_sqoh
16036 			, x_srqoh                      => l_srqoh
16037 			, x_sqr                        => l_sqr
16038 			, x_sqs                        => l_sqs
16039 			, x_satt                       => l_satt
16040 			, x_satr                       => l_satr
16041 			);
16042 
16043 			IF (l_return_status = fnd_api.g_ret_sts_success) THEN
16044 			  IF (l_debug = 1) THEN
16045 				mydebug('after update without lpn for FlexiLotAlloc l_att:'  || l_att  || ' at Locator Level');
16046 				mydebug('after update without lpn for FlexiLotAlloc l_satt:' || l_satt || ' at Locator Level');
16047 			  END IF;
16048 			ELSE
16049 			  IF (l_debug = 1) THEN
16050 				mydebug('calling update qty tree for FlexiLotAlloc without lpn failed ');
16051 			  END IF;
16052 
16053 			  fnd_message.set_name('INV', 'INV_INVALID_QUANTITY_TYPE');
16054 			  fnd_message.set_token('ROUTINE', 'INV_QUANTITY_TREE_PUB.QUERY_QUANTITIES');
16055 			  fnd_msg_pub.ADD;
16056 			  RAISE fnd_api.g_exc_error;
16057 			END IF;
16058 		  END IF;
16059 		END IF;
16060 
16061 		  inv_quantity_tree_pub.query_quantities(
16062 			p_api_version_number         => 1.0
16063 		  , p_init_msg_lst               => fnd_api.g_false
16064 		  , x_return_status              => l_return_status
16065 		  , x_msg_count                  => l_msg_cnt
16066 		  , x_msg_data                   => l_msg_data
16067 		  , p_organization_id            => p_org_id
16068 		  , p_inventory_item_id          => p_item_id
16069 		  , p_tree_mode                  => inv_quantity_tree_pub.g_transaction_mode
16070 		  , p_is_revision_control        => b_is_revision_control
16071 		  , p_is_lot_control             => TRUE
16072 		  , p_is_serial_control          => b_is_serial_control
16073 		  , p_demand_source_type_id      => -9999
16074 		  , p_revision                   => NVL(p_rev, NULL)
16075 		  , p_lot_number                 => p_lot
16076 		  , p_subinventory_code          => p_confirmed_sub
16077 		  , p_locator_id                 => p_confirmed_loc_id
16078 		  , x_qoh                        => l_qoh
16079 		  , x_rqoh                       => l_rqoh
16080 		  , x_qr                         => l_qr
16081 		  , x_qs                         => l_qs
16082 		  , x_att                        => l_att
16083 		  , x_atr                        => l_atr
16084 		  , x_sqoh                       => l_sqoh
16085 		  , x_srqoh                      => l_srqoh
16086 		  , x_sqr                        => l_sqr
16087 		  , x_sqs                        => l_sqs
16088 		  , x_satt                       => l_satt
16089 		  , x_satr                       => l_satr
16090 		  , p_lpn_id                     => p_fromlpn_id
16091 		  , p_grade_code                 => NULL
16092 		  );
16093 
16094 		  IF (l_return_status = fnd_api.g_ret_sts_success) THEN
16095 			IF (((NVL(p_fulfillment_base,'P') = 'P') AND l_att > 0) OR ((NVL(p_fulfillment_base,'P') = 'S' )AND l_satt > 0)) THEN
16096 			  l_table_index  := l_table_index + 1;
16097 			  IF (l_debug = 1) THEN
16098 				mydebug('FlexiLotAlloc  l_att:' || l_att || ' for lot:' || p_lot);
16099 				mydebug('FlexiLotAlloc  l_qoh:' || l_qoh || ' for lot:' || p_lot);
16100 			  END IF;
16101 
16102 			  IF (l_debug = 1) THEN
16103 				  mydebug(' FlexiLotAlloc l_table_index:' || l_table_index || ' lot_number:' || p_lot || ' qty: ' || l_att);
16104 			  END IF;
16105 			  t_lpn_lot_qty_table(l_table_index).lpn_id      := p_fromlpn_id;
16106 			  t_lpn_lot_qty_table(l_table_index).lot_number  := p_lot;
16107 			  t_lpn_lot_qty_table(l_table_index).pri_qty     := l_att;
16108               t_lpn_lot_qty_table(l_table_index).sec_qty     := l_satt;
16109 
16110 
16111 			IF(NVL(p_fulfillment_base,'P') = 'S') THEN
16112 					IF (p_sec_uom = p_trx_uom) THEN
16113 						t_lpn_lot_qty_table(l_table_index).trx_qty := l_satt;
16114 					ELSIF(l_primary_uom = p_trx_uom) THEN
16115 						t_lpn_lot_qty_table(l_table_index).trx_qty := l_att;
16116 					ELSE
16117 						t_lpn_lot_qty_table(l_table_index).trx_qty := inv_convert.inv_um_convert(
16118 											   item_id        => p_item_id
16119 											  ,lot_number	  => p_lot
16120 											  ,organization_id=> p_org_id
16121 											  ,precision      => null
16122 											  ,from_quantity  => l_satt
16123 											  ,from_unit      => p_sec_uom
16124 											  ,to_unit        => p_trx_uom
16125 											  ,from_name      => null
16126 											  ,to_name        => null);
16127 					END IF;
16128 			ELSE
16129 				IF (l_primary_uom = p_trx_uom) THEN
16130 					t_lpn_lot_qty_table(l_table_index).trx_qty := l_att;
16131 				ELSE
16132 					IF(p_trx_uom = p_sec_uom) THEN
16133 						t_lpn_lot_qty_table(l_table_index).trx_qty := l_satt;
16134 					ELSE
16135 						t_lpn_lot_qty_table(l_table_index).trx_qty := inv_convert.inv_um_convert(
16136 											   item_id        => p_item_id
16137 											  ,lot_number	  => p_lot
16138 											  ,organization_id=> p_org_id
16139 											  ,precision      => null
16140 											  ,from_quantity  => l_att
16141 											  ,from_unit      => l_primary_uom
16142 											  ,to_unit        => p_trx_uom
16143 											  ,from_name      => null
16144 											  ,to_name        => null);
16145 					END IF;
16146 				END IF;
16147 			END IF;
16148 			ELSE
16149 			   IF (l_debug = 1) THEN
16150 				  mydebug('FlexiLotAlloc -- LPN does not have any available qty for lot ' || p_lot);
16151 			   END IF;
16152 			  END IF;
16153 		  END IF;
16154 	END IF;
16155 
16156 	l_table_total      := t_lpn_lot_qty_table.COUNT;
16157 	IF l_table_total > 0 THEN
16158 		  IF (l_debug = 1) THEN
16159 		   mydebug('building lpn lot vector for ' || l_table_total || 'records');
16160 		  END IF;
16161 		  FOR l_table_count IN 1 .. l_table_total LOOP
16162 		  IF l_value = '.,'  THEN
16163 				x_lpn_lot_vector := x_lpn_lot_vector
16164 					 ||t_lpn_lot_qty_table(l_table_count).lot_number ||'@@@@@'
16165 					 ||t_lpn_lot_qty_table(l_table_count).trx_qty||'@@@@@'
16166 					 ||t_lpn_lot_qty_table(l_table_count).trx_qty
16167 					 || '&&&&&'
16168 					 || t_lpn_lot_qty_table(l_table_count).sec_qty
16169 					 || '#####';
16170 					ELSE
16171 					x_lpn_lot_vector := x_lpn_lot_vector
16172 							 ||t_lpn_lot_qty_table(l_table_count).lot_number ||'@@@@@'
16173 							 ||TO_CHAR(t_lpn_lot_qty_table(l_table_count).trx_qty,'9999999999999999999999.9999999999')||'@@@@@'
16174 							 ||TO_CHAR(t_lpn_lot_qty_table(l_table_count).trx_qty,'9999999999999999999999.9999999999')
16175 							 || '&&&&&'
16176 							 || To_Char(t_lpn_lot_qty_table(l_table_count).sec_qty,'9999999999999999999999.9999999999')
16177 							 || '#####';
16178 		 END IF;
16179 		  END LOOP;
16180 	ELSE
16181 		  x_lpn_lot_vector  := NULL;
16182 	END IF;
16183 
16184 		BEGIN
16185 		IF(LENGTH(x_lpn_lot_vector) > 4000) THEN
16186 		x_lpn_lot_vector2 := SUBSTR(x_lpn_lot_vector, 4001, 8000);
16187 		x_lpn_lot_vector3 := SUBSTR(x_lpn_lot_vector, 8001, 12000);
16188 		x_lpn_lot_vector4 := SUBSTR(x_lpn_lot_vector, 12001);
16189 		x_lpn_lot_vector := SUBSTR(x_lpn_lot_vector, 1, 4000);
16190 			IF (l_debug = 1) THEN
16191 				mydebug('x_lpn_lot_vector  : '||x_lpn_lot_vector);
16192 				mydebug('x_lpn_lot_vector2 : '||x_lpn_lot_vector2);
16193 				mydebug('x_lpn_lot_vector3 : '||x_lpn_lot_vector3);
16194 				mydebug('x_lpn_lot_vector4 ::'||x_lpn_lot_vector4);
16195 			END IF;
16196 		ELSE
16197 			IF (l_debug = 1) THEN
16198 			mydebug(' x_lpn_lot_vector string length less that 4000 chars and needs no trimming: '||LENGTH(x_lpn_lot_vector));
16199 			END IF;
16200 		END IF;
16201 		EXCEPTION
16202 			WHEN OTHERS THEN
16203 			IF (l_debug = 1) THEN
16204 			mydebug('x_lpn_lot_vector  string trim had issues: '||LENGTH(x_lpn_lot_vector));
16205 			END IF;
16206 		END;
16207 
16208    x_return_status    := fnd_api.g_ret_sts_success;
16209 
16210   EXCEPTION
16211 	WHEN OTHERS THEN
16212 	  IF (l_debug = 1) THEN
16213 		mydebug('Other exception raised : ' || SQLERRM);
16214 	  END IF;
16215 	  x_return_status  := fnd_api.g_ret_sts_unexp_error;
16216 
16217 END Update_Tree_Flex_Lot;
16218 
16219 
16220 END wms_task_load;