DBA Data[Home] [Help]

PACKAGE BODY: APPS.INV_LPN_TRX_PUB

Source


1 PACKAGE BODY inv_lpn_trx_pub AS
2   /* $Header: INVTRXWB.pls 120.46.12010000.7 2008/10/07 11:37:53 kkesavar ship $ */
3 
4   --      Name: PROCESS_LPN_TRX
5   --
6   --      Input parameters:
7   --
8   --      Output parameters:
9   --       x_proc_msg         Message from the Process-Manager
10   --       return_status      0 on Success, 1 on Error
11   --
12   --
13   --  Global constant holding the package name
14   g_pkg_name    CONSTANT VARCHAR2(30)   := 'INV_LPN_TRX_PUB';
15   g_pkg_version CONSTANT VARCHAR2(100)  := '$Header: INVTRXWB.pls 120.46.12010000.7 2008/10/07 11:37:53 kkesavar ship $';
16   ret_status             VARCHAR2(512);
17   ret_msgcnt             NUMBER         := 0;
18   ret_msgdata            VARCHAR2(1000);
19   g_pack        CONSTANT NUMBER         := 1;
20   g_unpack      CONSTANT NUMBER         := 2;
21   g_adjust      CONSTANT NUMBER         := 3;
22   g_unpack_all  CONSTANT NUMBER         := 4;
23   g_precision   CONSTANT NUMBER         := 5;
24   -- HVERDDIN ERES START
25   g_eres_event_name      VARCHAR2(60);
26 
27   TYPE eres_rec IS RECORD(
28     event_id                   NUMBER
29   , transaction_action_id      NUMBER
30   , transaction_source_type_id NUMBER
31   );
32 
33   TYPE eres_tbl IS TABLE OF eres_rec
34     INDEX BY BINARY_INTEGER;
35 
36   -- HVERDDIN  ERES END
37 
38   /******************************************************************************************************************
39   -- TRANS_ERES_ENABLED
40   -- Validate if this transaction type is being supported.
41   -- Only the following are being supported in this release
42 
43   -- TRANSACTION_SOURCE_TYPE                                 TRANSACTION_ACTION
44   ------------I----------------------------------            ------------------------
45   -- G_SOURCETYPE_ACCOUNT           := 3;                    G_ACTION_ISSUE   := 1   G_ACTION_RECEIPT  := 27;
46   -- G_SOURCETYPE_ACCOUNTALIAS      := 6;                    G_ACTION_ISSUE   := 1   G_ACTION_RECEIPT  := 27;
47   -- G_SOURCETYPE_INVENTORY         := 13;                   G_ACTION_ISSUE   := 1   G_ACTION_RECEIPT  := 27;
48   --
49   --
50   -- Created H.Verdding  - Added to support ERES FDA requirements.
51   ******************************************************************************************************************/
52   FUNCTION trans_eres_enabled(p_trans_action_id IN NUMBER, p_trans_source_type_id IN NUMBER)
53     RETURN BOOLEAN IS
54     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
55   BEGIN
56     IF (l_debug = 1) THEN
57       inv_log_util.TRACE('** Trans Eres Enabled trx_action_id = ' || p_trans_action_id, 'INV_LPN_TRX_PUB', 9);
58       inv_log_util.TRACE('** Trans Eres Enabled trx_soource_id =' || p_trans_source_type_id, 'INV_LPN_TRX_PUB', 9);
59     END IF;
60 
61     IF (p_trans_action_id NOT IN(inv_globals.g_action_issue, inv_globals.g_action_receipt,INV_GLOBALS.G_ACTION_SUBXFR,
62                                  INV_GLOBALS.G_ACTION_ORGXFR, INV_GLOBALS.G_ACTION_INTRANSITSHIPMENT)) THEN
63       inv_log_util.TRACE(
64         '** Transactions Not ERES Supported trx_action_id =' || p_trans_action_id || ',trx_source_type_id =' || p_trans_source_type_id
65       , 'INV_LPN_TRX_PUB'
66       , 9
67       );
68       RETURN FALSE;
69     ELSIF(p_trans_action_id = inv_globals.g_action_issue) THEN
70       -- These are Issue trans types
71       IF (p_trans_source_type_id = inv_globals.g_sourcetype_account) THEN
72         g_eres_event_name  := 'oracle.apps.inv.acctIssue';
73       ELSIF(p_trans_source_type_id = inv_globals.g_sourcetype_accountalias) THEN
74         g_eres_event_name  := 'oracle.apps.inv.acctAliasIssue';
75       ELSIF(p_trans_source_type_id = inv_globals.g_sourcetype_inventory) THEN
76         g_eres_event_name  := 'oracle.apps.inv.miscIssue';
77       ELSE
78         RETURN FALSE;
79       END IF;
80     ELSIF(p_trans_action_id = inv_globals.g_action_receipt) THEN
81       -- These are Reciept trans types
82       IF (p_trans_source_type_id = inv_globals.g_sourcetype_account) THEN
83         g_eres_event_name  := 'oracle.apps.inv.acctReceipt';
84       ELSIF(p_trans_source_type_id = inv_globals.g_sourcetype_accountalias) THEN
85         g_eres_event_name  := 'oracle.apps.inv.acctAliasReceipt';
86       ELSIF(p_trans_source_type_id = inv_globals.g_sourcetype_inventory) THEN
87         g_eres_event_name  := 'oracle.apps.inv.miscReceipt';
88       ELSE
89         RETURN FALSE;
90       END IF;
91     -- fabdi invconv start
92 	 ELSIF (p_trans_action_id = INV_GLOBALS.G_ACTION_SUBXFR) THEN
93        -- These are Inter-Org Transfers trans types
94        IF (p_trans_source_type_id = INV_GLOBALS.G_SOURCETYPE_INVENTORY) THEN
95             g_eres_event_name := 'oracle.apps.inv.subinvTransfer';
96        ELSE
97             RETURN FALSE;
98        END IF;
99      ELSIF (p_trans_action_id in (INV_GLOBALS.G_ACTION_ORGXFR, INV_GLOBALS.G_ACTION_INTRANSITSHIPMENT)) THEN
100        -- These are Subinventory Transfers trans types
101        IF (p_trans_source_type_id = INV_GLOBALS.G_SOURCETYPE_INVENTORY) THEN
102             g_eres_event_name := 'oracle.apps.inv.interorgTransfer';
103        ELSE
104             RETURN FALSE;
105        END IF;
106 	-- fabdi invconv end
107     ELSE
108       RETURN FALSE;
109     END IF;
110 
111     IF (l_debug = 1) THEN
112       inv_log_util.TRACE('**  ERES EVENT  = ' || g_eres_event_name, 'INV_LPN_TRX_PUB', 9);
113     END IF;
114 
115     RETURN TRUE;
116   EXCEPTION
117     WHEN OTHERS THEN
118       IF (l_debug = 1) THEN
119         inv_log_util.TRACE('** Function TRANS_ERES_ENABLED - raised when Others', 'INV_LPN_TRX_PUB', 9);
120       END IF;
121 
122       fnd_message.set_name('INV', 'INV_ERES_ENABLED');
123       fnd_msg_pub.ADD;
124       RAISE fnd_api.g_exc_error;
125       RETURN FALSE;
126   END trans_eres_enabled;
127 
128 --3978111 CHANGES START
129 /********************************************************************
130 * Code to insert in cst_comp_snap_temp in case of WIP LPN completion
131 * and complete LPN putaway
132 *******************************************************************/
133 procedure create_snapshot(p_temp_id NUMBER, p_org_id NUMBER)
134 IS
135     l_errNum                       NUMBER;
136     l_errCode                      VARCHAR2(1);
137     l_errMsg                       VARCHAR2(241);
138     l_cst_ret                      NUMBER(1):=0;
139     l_primary_cost_method          NUMBER;
140     l_debug			   NUMBER      := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
141 BEGIN
142 	select primary_cost_method
143 	into   l_primary_cost_method
144 	from   mtl_parameters
145 	where  organization_id = p_org_id;
146 
147 	IF (l_debug = 1) THEN
148 		inv_log_util.TRACE('INVTRXWB: cost method of org'||p_org_id||' is '||l_primary_cost_method,'INVTRXWB',1);
149 	END IF;
150 
151 	IF l_primary_cost_method in (2,5,6) THEN
152 		IF (l_debug = 1) THEN
153 			inv_log_util.TRACE('PRIMARY COST METHOD IS AVG OR FIFO OR LIFO CALLING CSTACOSN.op_snapshot'||p_temp_id,'INVTRXWB',1);
154 		END IF;
155 
156 		l_cst_ret := CSTACOSN.op_snapshot(i_txn_temp_id => p_temp_id,
157 						err_num => l_errNum,
158 						err_code => l_errCode,
159 						err_msg => l_errMsg);
160 		IF(l_cst_ret <> 1) THEN
161 			fnd_message.set_name('BOM', 'CST_SNAPSHOT_FAILED');
162 			fnd_msg_pub.ADD;
163 			IF (l_debug = 1) THEN
164 				inv_log_util.TRACE('INVTRXWB: Error from CSTACOSN.op_snapshot ','INVTRXWB',1);
165 			END IF;
166 			raise fnd_api.g_exc_unexpected_error;
167 		ELSE
168 			inv_log_util.TRACE('INVTRXWB: CALL TO CSTACOSN.op_snapshot SUCCESSFULL','INVTRXWB',1);
169 		END IF;
170 
171 	END IF;
172 END create_snapshot;
173 --3978111 CHANGES END
174 
175   /********************************************************************
176    * Insert a row into MTL_MATERIAL_TRANSACTION_TEMP
177    *  If the transaction is an InterOrg transfer type, call the CostGroup
178    *  API to determine cost groups.
179    *******************************************************************/
180   PROCEDURE insert_line_trx(
181     curlpnrec                     wms_container_pub.wms_container_content_rec_type
182   , v_trxtempid                   NUMBER
183   , v_trxaction                   NUMBER
184   , v_orgid                       NUMBER
185   , v_subinv                      VARCHAR2
186   , v_locatorid                   NUMBER
187   , v_trxqty                      NUMBER
188   , v_cost_group_id               NUMBER := NULL
189   , v_mmtt_rec      IN OUT NOCOPY mtl_material_transactions_temp%ROWTYPE
190   , x_trxtempid     OUT NOCOPY    NUMBER
191   , v_sectrxqty                   NUMBER := NULL --INVCONV kkillams
192   ) IS
193     l_cst_grp_id     NUMBER;
194     l_xfr_cst_grp_id NUMBER     := v_cost_group_id;
195     l_temp_uom_code  VARCHAR(3);
196     l_debug          NUMBER     := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
197     l_sign	     NUMBER     := 1; --Added bug3984746
198   BEGIN
199     SELECT mtl_material_transactions_s.NEXTVAL
200       INTO x_trxtempid
201       FROM DUAL;
202 
203     v_mmtt_rec.transaction_temp_id   := x_trxtempid;
204     v_mmtt_rec.subinventory_code     := v_subinv;
205     v_mmtt_rec.locator_id            := v_locatorid;
206     v_mmtt_rec.inventory_item_id     := curlpnrec.content_item_id;
207     v_mmtt_rec.content_lpn_id        := NVL(curlpnrec.parent_lpn_id, v_mmtt_rec.content_lpn_id);
208     v_mmtt_rec.transaction_quantity  := v_trxqty;
209     v_mmtt_rec.transaction_uom       := curlpnrec.uom;
210     v_mmtt_rec.secondary_transaction_quantity  := v_sectrxqty;      --INVCONV kkillams
211     v_mmtt_rec.secondary_uom_code              := curlpnrec.sec_uom;--INVCONV kkillams
212 
213     SELECT primary_uom_code
214       INTO l_temp_uom_code
215       FROM mtl_system_items
216      WHERE inventory_item_id = v_mmtt_rec.inventory_item_id
217        AND organization_id = v_mmtt_rec.organization_id;
218 
219    IF v_mmtt_rec.transaction_uom <> l_temp_uom_code THEN  --Added bug 3984746
220     IF v_mmtt_rec.primary_quantity < 0 THEN
221     	l_sign := -1;
222     END IF;   --End of bug fix3984746
223     v_mmtt_rec.primary_quantity      :=
224       inv_convert.inv_um_convert(v_mmtt_rec.inventory_item_id, 6, abs(v_mmtt_rec.transaction_quantity), v_mmtt_rec.transaction_uom
225       , l_temp_uom_code, NULL, NULL);
226 
227     IF (v_mmtt_rec.primary_quantity = -99999) THEN  --Changed from '<=' bug 3984746
228       fnd_message.set_name('INV', 'INV_UOM_CONVERSION_ERROR');
229       fnd_message.set_token('uom1', v_mmtt_rec.transaction_uom);
230       fnd_message.set_token('uom2', l_temp_uom_code);
231       fnd_message.set_token('module', 'INSERT_LINE_TRX');
232       fnd_msg_pub.ADD;
233       RAISE fnd_api.g_exc_error;
234     END IF;
235 
236     v_mmtt_rec.primary_quantity := v_mmtt_rec.primary_quantity * l_sign;  --Added bug3984746
237     ELSE
238     v_mmtt_rec.primary_quantity := v_mmtt_rec.transaction_quantity; --Added bug 4080241
239     END IF;
240 
241     v_mmtt_rec.revision              := curlpnrec.revision;
242     v_mmtt_rec.cost_group_id         := NVL(v_cost_group_id, v_mmtt_rec.cost_group_id);
243 
244     -- If the transaction action is OrgXfr, IntrShipment or IntrRcpt
245     -- then set the cost_group_id to null. The CostGroup API will
246     -- determine the cost-group-id in this case. Insert into MMTT using
247     -- the passed MMTT_Record type.
248     IF (v_trxaction = inv_globals.g_action_orgxfr)
249        OR(v_trxaction = inv_globals.g_action_intransitshipment)
250        OR(v_trxaction = inv_globals.g_action_intransitreceipt) THEN
251       l_xfr_cst_grp_id  := NULL;
252     END IF;
253 
254     -- Added two columns to MMTT for International drop shipments -
255     -- trx_flow_header_id and logical_trx_type_code
256     -- Added original_transaction_temp_id to MMTT as part of ERES changes
257     -- Added source_line_id and source_code for bug 4931515
258     -- Added SOURCE_PROJECT_ID and SOURCE_TASK_ID for bug#5224902
259     INSERT INTO mtl_material_transactions_temp
260                 (
261                  transaction_header_id
262                , transaction_temp_id
263 	       , source_code
264 	       , source_line_id
265                , process_flag
266                , creation_date
267                , created_by
268                , last_update_date
269                , last_updated_by
270                , last_update_login
271                , inventory_item_id
272                , organization_id
273                , subinventory_code
274                , locator_id
275                , transfer_to_location
276                , transaction_quantity
277                , primary_quantity
278                , transaction_uom
279                , transaction_type_id
280                , transaction_action_id
281                , transaction_source_type_id
282                , transaction_date
283                , acct_period_id
284                , transfer_organization
285                , transfer_subinventory
286                , reason_id
287                , shipment_number
288                , distribution_account_id
289                , waybill_airbill
290                , expected_arrival_date
291                , freight_code
292                , revision
293                , lpn_id
294                , content_lpn_id
295                , transfer_lpn_id
296                , cost_group_id
297                , transaction_source_id
298                , trx_source_line_id
299                , demand_source_header_id
300                , demand_source_line
301                , posting_flag
302                , pick_rule_id
303                , pick_strategy_id
304                , put_away_rule_id
305                , put_away_strategy_id
306                , move_order_line_id
307                , pick_slip_number
308                , reservation_id
309                , transaction_status
310                , standard_operation_id
311                , task_priority
312                , wms_task_type
313                , transfer_cost_group_id
314                , wip_entity_type
315                , repetitive_line_id
316                , operation_seq_num
317                , department_id
318                , department_code
319                , lock_flag
320                , primary_switch
321                , wip_supply_type
322                , negative_req_flag
323                , required_flag
324                , completion_transaction_id
325                , flow_schedule
326                , transaction_batch_id
327                , transaction_batch_seq
328                , transaction_mode
329                , owning_organization_id
330                , owning_tp_type
331                , xfr_owning_organization_id
332                , transfer_owning_tp_type
333                , planning_organization_id
334                , planning_tp_type
335                , xfr_planning_organization_id
336                , transfer_planning_tp_type
337                , fob_point
338                , intransit_account
339                , trx_flow_header_id
340                , logical_trx_type_code
341                , original_transaction_temp_id
342                , secondary_uom_code             --kkillams
343                , secondary_transaction_quantity --kkillams
344                , ship_to_location --eIB Build; Bug# 4348541
345                , relieve_reservations_flag      /*** {{ R12 Enhanced reservations code changes ***/
346                , relieve_high_level_rsv_flag    /*** {{ R12 Enhanced reservations code changes ***/
347                , cycle_count_id                 -- bug 5060715
348 	       , source_project_id              -- bug 5224902
349 	       , source_task_id
350                , MATERIAL_ALLOCATION_TEMP_ID  -- Added for bug # 5689491
351                 )
352          VALUES (
353                  v_mmtt_rec.transaction_header_id
354                , v_mmtt_rec.transaction_temp_id
355 	       , v_mmtt_rec.source_code
356 	       , v_mmtt_rec.source_line_id
357                , v_mmtt_rec.process_flag
358                , v_mmtt_rec.creation_date
359                , v_mmtt_rec.created_by
360                , v_mmtt_rec.last_update_date
361                , v_mmtt_rec.last_updated_by
362                , v_mmtt_rec.last_update_login
363                , v_mmtt_rec.inventory_item_id
364                , v_mmtt_rec.organization_id
365                , v_mmtt_rec.subinventory_code
366                , v_mmtt_rec.locator_id
367                , v_mmtt_rec.transfer_to_location
368                , v_mmtt_rec.transaction_quantity
369                , v_mmtt_rec.primary_quantity
370                , v_mmtt_rec.transaction_uom
371                , v_mmtt_rec.transaction_type_id
372                , v_mmtt_rec.transaction_action_id
373                , v_mmtt_rec.transaction_source_type_id
374                , v_mmtt_rec.transaction_date
375                , v_mmtt_rec.acct_period_id
376                , v_mmtt_rec.transfer_organization
377                , v_mmtt_rec.transfer_subinventory
378                , v_mmtt_rec.reason_id
379                , v_mmtt_rec.shipment_number
380                , v_mmtt_rec.distribution_account_id
381                , v_mmtt_rec.waybill_airbill
382                , v_mmtt_rec.expected_arrival_date
383                , v_mmtt_rec.freight_code
384                , v_mmtt_rec.revision
385                , v_mmtt_rec.lpn_id
386                , v_mmtt_rec.content_lpn_id
387                , v_mmtt_rec.transfer_lpn_id
388                , NVL(v_cost_group_id, v_mmtt_rec.cost_group_id)
389                , v_mmtt_rec.transaction_source_id
390                , v_mmtt_rec.trx_source_line_id
391                , v_mmtt_rec.demand_source_header_id
392                , v_mmtt_rec.demand_source_line
393                , v_mmtt_rec.posting_flag
394                , v_mmtt_rec.pick_rule_id
395                , v_mmtt_rec.pick_strategy_id
396                , v_mmtt_rec.put_away_rule_id
397                , v_mmtt_rec.put_away_strategy_id
398                , v_mmtt_rec.move_order_line_id
399                , v_mmtt_rec.pick_slip_number
400                , v_mmtt_rec.reservation_id
401                , v_mmtt_rec.transaction_status
402                , v_mmtt_rec.standard_operation_id
403                , v_mmtt_rec.task_priority
404                , v_mmtt_rec.wms_task_type
405                , l_xfr_cst_grp_id
406                , v_mmtt_rec.wip_entity_type
407                , v_mmtt_rec.repetitive_line_id
408                , v_mmtt_rec.operation_seq_num
409                , v_mmtt_rec.department_id
410                , v_mmtt_rec.department_code
411                , v_mmtt_rec.lock_flag
412                , v_mmtt_rec.primary_switch
413                , v_mmtt_rec.wip_supply_type
414                , v_mmtt_rec.negative_req_flag
415                , v_mmtt_rec.required_flag
416                , v_mmtt_rec.completion_transaction_id
417                , v_mmtt_rec.flow_schedule
418                , v_mmtt_rec.transaction_batch_id
419                , v_mmtt_rec.transaction_batch_seq
420                , v_mmtt_rec.transaction_mode
421                , v_mmtt_rec.owning_organization_id
422                , v_mmtt_rec.owning_tp_type
423                , v_mmtt_rec.xfr_owning_organization_id
424                , v_mmtt_rec.transfer_owning_tp_type
425                , v_mmtt_rec.planning_organization_id
426                , v_mmtt_rec.planning_tp_type
427                , v_mmtt_rec.xfr_planning_organization_id
428                , v_mmtt_rec.transfer_planning_tp_type
429                , v_mmtt_rec.fob_point
430                , v_mmtt_rec.intransit_account
431                , v_mmtt_rec.trx_flow_header_id
432                , v_mmtt_rec.logical_trx_type_code
433                , v_mmtt_rec.original_transaction_temp_id
434                , v_mmtt_rec.secondary_uom_code             --kkillams
435                , v_mmtt_rec.secondary_transaction_quantity --kkillams
436                , v_mmtt_rec.ship_to_location --eIB Build; Bug# 4348541
437                , v_mmtt_rec.relieve_reservations_flag      /*** {{ R12 Enhanced reservations code changes ***/
438                , v_mmtt_rec.relieve_high_level_rsv_flag    /*** {{ R12 Enhanced reservations code changes ***/
439                , v_mmtt_rec.cycle_count_id                 --bug 5060715
440 	       , v_mmtt_rec.source_project_id              --bug 5224902
441 	       , v_mmtt_rec.source_task_id
442                , v_mmtt_rec.MATERIAL_ALLOCATION_TEMP_ID  -- Added for bug # 5689491
443                 );
444 
445 --Added bug 3978111
446 		IF v_mmtt_rec.content_lpn_id is not null
447 		AND v_mmtt_rec.transaction_action_id=31
448 		AND v_mmtt_rec.transaction_type_id=44 THEN
449 			create_snapshot(x_trxtempid,v_mmtt_rec.organization_id);
450 		END IF;
451 --Added bug 3978111
452   END;
453 
454   /********************************************************************
455    * Insert a row into MTL_TRANSACTION_LOTS_TEMP
456    *******************************************************************/
457   FUNCTION insert_lot_trx(curlpnrec wms_container_pub.wms_container_content_rec_type, trxtmpid NUMBER)
458     RETURN NUMBER IS
459     lotobjid      NUMBER;
460     sertrxid      NUMBER;
461     retval        NUMBER;
462     l_exp_date    DATE;
463     l_primary_uom VARCHAR2(3);
464     l_primary_qty NUMBER;
465     l_debug       NUMBER      := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
466   BEGIN
467     -- retrieve the expiration-date for the lot number
468     -- being inserted
469     BEGIN
470       SELECT expiration_date
471         INTO l_exp_date
472         FROM mtl_lot_numbers
473        WHERE organization_id = curlpnrec.organization_id
474          AND inventory_item_id = curlpnrec.content_item_id
475          AND lot_number = curlpnrec.lot_number;
476     EXCEPTION
477       WHEN OTHERS THEN
478         l_exp_date  := NULL;
479     END;
480 
481     SELECT primary_uom_code
482       INTO l_primary_uom
483       FROM mtl_system_items
484      WHERE inventory_item_id = curlpnrec.content_item_id
485        AND organization_id = curlpnrec.organization_id;
486 
487     l_primary_qty  := inv_convert.inv_um_convert(curlpnrec.content_item_id, 6, curlpnrec.quantity, curlpnrec.uom, l_primary_uom, NULL, NULL);
488 
489     IF (l_primary_qty <= -99999) THEN
490       fnd_message.set_name('INV', 'INV_UOM_CONVERSION_ERROR');
491       fnd_message.set_token('uom1', curlpnrec.uom);
492       fnd_message.set_token('uom2', l_primary_uom);
493       fnd_message.set_token('module', 'INSERT_LOT_TRX');
494       fnd_msg_pub.ADD;
495       RAISE fnd_api.g_exc_error;
496     END IF;
497 
498     retval         :=
499       inv_trx_util_pub.insert_lot_trx(
500         p_trx_tmp_id                 => trxtmpid
501       , p_user_id                    => 1
502       , p_lot_number                 => curlpnrec.lot_number
503       , p_trx_qty                    => curlpnrec.quantity
504       , p_pri_qty                    => l_primary_qty
505       , p_secondary_qty              => curlpnrec.sec_quantity --INVCONV kkillams
506       , p_secondary_uom              => curlpnrec.sec_uom      --INVCONV kkillams
507       , x_ser_trx_id                 => sertrxid
508       , x_proc_msg                   => ret_msgdata
509       , p_exp_date                   => l_exp_date
510       );
511 
512     IF (retval <> 0) THEN
513       IF (l_debug = 1) THEN
514         inv_log_util.TRACE('**Error from insertLot :' || ret_msgdata, 'INV_LPN_TRX_PUB', 1);
515       END IF;
516 
517       fnd_message.set_name('INV', 'INV_FAILED');
518       fnd_msg_pub.ADD;
519       RAISE fnd_api.g_exc_error;
520     END IF;
521 
522     IF (l_debug = 1) THEN
523       inv_log_util.TRACE('*Inserted Lot :' || curlpnrec.lot_number, 'INV_LPN_TRX_PUB', 9);
524     END IF;
525 
526     RETURN sertrxid;
527   END;
528 
529   /********************************************************************
530    * Insert a row into MTL_SERIAL_NUMBERS_TEMP
531    *******************************************************************/
532   FUNCTION insert_ser_trx(p_ser_number VARCHAR2, p_sertrxid NUMBER)
533     RETURN NUMBER IS
534     retval  NUMBER;
535     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
536   BEGIN
537     retval  :=
538       inv_trx_util_pub.insert_ser_trx(p_trx_tmp_id => p_sertrxid, p_user_id => 1, p_fm_ser_num => p_ser_number
539       , p_to_ser_num                 => p_ser_number, x_proc_msg => ret_msgdata);
540 
541     IF (retval <> 0) THEN
542       IF (l_debug = 1) THEN
543         inv_log_util.TRACE('**Error from insertSerial :' || ret_msgdata, 'INV_LPN_TRX_PUB', 1);
544       END IF;
545 
546       fnd_message.set_name('INV', 'INV_FAILED');
547       fnd_msg_pub.ADD;
548       RAISE fnd_api.g_exc_error;
549     END IF;
550 
551     IF (l_debug = 1) THEN
552       inv_log_util.TRACE('*Inserted Serial:' || p_ser_number || ',trxid=' || p_sertrxid, 'INV_LPN_TRX_PUB', 9);
553     END IF;
554 
555     RETURN 0;
556   END;
557   /*  Bug#5486052. Added the below function. This function takes itemID, quantity and 2 UOM codes as input parameters.
558    *  It returns the quantity, which was expressed in the 1st UOM, converted to the 2nd UOM.*/
559   FUNCTION get_converted_qty(p_inventory_item_id NUMBER, p_qty NUMBER, p_uom1 VARCHAR2, p_uom2 VARCHAR2)
560   RETURN NUMBER IS
561     l_converted_qty NUMBER;
562   BEGIN
563     l_converted_qty := inv_convert.inv_um_convert(   p_inventory_item_id
564                                                    , 6
565                                                    , p_qty
566                                                    , p_uom1
567                                                    , p_uom2
568                                                    , NULL
569                                                    , NULL);
570     IF ( l_converted_qty = -99999 ) THEN
571       fnd_message.set_name('INV', 'INV_UOM_CONVERSION_ERROR');
572       fnd_message.set_token('uom1', p_uom1);
573       fnd_message.set_token('uom2', p_uom2);
574       fnd_message.set_token('module', 'GET_CONVERTED_QTY');
575       fnd_msg_pub.ADD;
576       RAISE fnd_api.g_exc_error;
577     END IF;
578 
579     RETURN l_converted_qty;
580   END;
581 
582   /********************************************************************
583    * Explode the contents of the lpn and insert into MMTT, MSNT and MTLT
584    *******************************************************************/
585   FUNCTION explode_and_insert(p_lpn_id NUMBER, p_hdr_id NUMBER, p_mmtt IN OUT NOCOPY mtl_material_transactions_temp%ROWTYPE)
586     RETURN NUMBER IS
587     tb_lpn_cnts           wms_container_pub.wms_container_tbl_type;
588     lpnitndx              NUMBER;
589     curlpnrec             wms_container_pub.wms_container_content_rec_type;
590     insrowcnt             NUMBER                                           := 0;
591     trxtmpid              NUMBER;
592     sertrxid              NUMBER;
593     l_pre_sertrxid        NUMBER;
594     retval                NUMBER;
595     itemqty               NUMBER;
596     itemsecqty            NUMBER;  --INVCONV kkillams
597     orgid                 NUMBER;
598     subinv                VARCHAR2(32);
599     locatorid             NUMBER;
600     lastitemid            NUMBER                                           := NULL;
601     lastrevison           VARCHAR2(3);
602     lastcostgroupid       NUMBER                                           := NULL;
603     lastparentlpnid       NUMBER                                           := NULL;
604     lotexpdate            DATE;
605     lotobjid              NUMBER;
606     v_lasttrxtmpid        NUMBER;
607     v_lastitemqty         NUMBER;
608     v_lastitemsecqty      NUMBER;  ---INVCONV KKILLAMS
609     lastlotnum            mtl_transaction_lots_temp.lot_number%TYPE;
610     itemqtychanged        BOOLEAN                                          := FALSE;
611     lotqtychanged         BOOLEAN                                          := FALSE;
612     lotqty                NUMBER;
613     lotsecqty             NUMBER;  --invconv kkillams
614     l_lpn_subinv          VARCHAR2(32)                                     := p_mmtt.subinventory_code;
615     l_lpn_locator_id      NUMBER                                           := p_mmtt.locator_id;
616     l_cst_grp_id          NUMBER;
617     l_xfr_cst_grp_id      NUMBER;
618     l_debug               NUMBER                                           := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
619     /* Bug 2712046, add cost group comingle check if the transfer location is non-LPN controlled
620        new variable to check LPN controlled flag of the transfer subinventory */
621     l_skip_comingle_check NUMBER                                           := 0;
622     --l_lpn_controlled_flag number := 0;
623     ret_status            VARCHAR2(10);
624     ret_msgcnt            NUMBER;
625     ret_msgdata           VARCHAR2(2000);
626     l_comingling_occurs   VARCHAR2(1);
627     l_count               NUMBER;
628     l_cst_grp             VARCHAR2(30);
629     lastuom               VARCHAR2(3); /*Bug#5486052*/
630     l_conv_fact           NUMBER                                           := 1; /*Bug#5486052*/
631     l_primary_uom         VARCHAR2(3); /*Bug#5486052*/
632     l_converted_qty       NUMBER; /*Bug#5486052*/
633   BEGIN
634     IF (l_debug = 1) THEN
635       inv_log_util.TRACE('** exploding lpn_id =' || p_lpn_id || ',qty=' || p_mmtt.primary_quantity, 'INV_LPN_TRX_PUB', 9);
636     END IF;
637 
638     -- Bug 5103408 Add check for explode_lpn failure status.
639     -- Transaction should not continue if an error occurs during explosion
640     WMS_Container_PVT.Explode_LPN(1.0, fnd_api.g_false, fnd_api.g_false, ret_status, ret_msgcnt, ret_msgdata, p_lpn_id, 0, tb_lpn_cnts);
641 
642     IF ( ret_status <> fnd_api.g_ret_sts_success) THEN
643       IF (l_debug = 1) THEN
644         inv_log_util.TRACE('**Error: Failed in wms_container_pub.explode_lpn API :' || ret_msgdata, 'INV_LPN_TRX_PUB', 1);
645       END IF;
646 
647       RAISE fnd_api.g_exc_error;
648     END IF;
649 
650     -- Retrieve LPN's SUB and Locator
651     SELECT subinventory_code
652          , locator_id
653       INTO l_lpn_subinv
654          , l_lpn_locator_id
655       FROM wms_license_plate_numbers
656      WHERE lpn_id = p_lpn_id;
657 
658     -- If No Subinventory associated with LPN, then pick sub and locator from MMT
659     IF (l_lpn_subinv IS NULL) THEN
660       l_lpn_subinv      := p_mmtt.subinventory_code;
661       l_lpn_locator_id  := p_mmtt.locator_id;
662     END IF;
663 
664     IF (l_debug = 1) THEN
665       inv_log_util.TRACE('LPN sub : ' || l_lpn_subinv || ',loc=' || l_lpn_locator_id, 'INV_LPN_TRX_PUB', 9);
666     END IF;
667 
668     --Check for batch ID and batch sequence.  If they do not exist, populate them
669     --Also set inventory_item_id to -1 to ensure this record is processed first
670     --in the batch
671     IF (l_debug = 1) THEN
672       inv_log_util.TRACE('batch id=' || p_mmtt.transaction_batch_id || 'seq_id =' || p_mmtt.transaction_batch_seq, 'INV_LPN_TRX_PUB', 9);
673     END IF;
674 
675     IF (p_mmtt.transaction_batch_id IS NULL OR p_mmtt.transaction_batch_seq IS NULL) THEN
676       -- Batch id and sequence pair should be unique for this header_id.
677       -- Going to used the transaction_temp_id for the batch and 1 for the sequence.
678       p_mmtt.transaction_batch_id  := NVL(p_mmtt.transaction_batch_id, p_mmtt.transaction_temp_id);
679       p_mmtt.transaction_batch_seq := NVL(p_mmtt.transaction_batch_seq, 1);
680 
681       UPDATE mtl_material_transactions_temp
682          SET inventory_item_id = -1
683            , transaction_batch_id = p_mmtt.transaction_batch_id
684            , transaction_batch_seq = p_mmtt.transaction_batch_seq
685            , subinventory_code = NVL(subinventory_code, l_lpn_subinv)
686            , locator_id = NVL(locator_id, l_lpn_locator_id)
687        WHERE transaction_temp_id = p_mmtt.transaction_temp_id;
688 
689       --  If transaction came from MTI, need to also update MTI with
690       -- the same batch and sequence so java tm can update delete MTIs
691       IF ( p_mmtt.transaction_mode = inv_txn_manager_pub.proc_mode_mti ) THEN
692         UPDATE mtl_transactions_interface
693            SET transaction_batch_id = p_mmtt.transaction_batch_id
694              , transaction_batch_seq = p_mmtt.transaction_batch_seq
695          WHERE transaction_interface_id = p_mmtt.transaction_temp_id;
696 
697         IF (l_debug = 1) THEN
698           inv_log_util.TRACE('From MTI, try update MTI batch and seq rowcount='||sql%rowcount, 'INV_LPN_TRX_PUB', 9);
699         END IF;
700 
701         IF ( sql%rowcount = 0 ) THEN
702           fnd_message.set_name('INV', 'INV_NO_RECORDS');
703           fnd_message.set_token('ENTITY', 'MTL_TRANSACTIONS_INTERFACE');
704           fnd_msg_pub.ADD;
705           RAISE fnd_api.g_exc_error;
706         END IF;
707       END IF;
708     ELSIF(p_mmtt.inventory_item_id <> -1) THEN
709       UPDATE mtl_material_transactions_temp
710          SET inventory_item_id = -1
711        WHERE transaction_temp_id = p_mmtt.transaction_temp_id;
712     END IF;
713 
714     -- Bug 2712046, add cost group comingle check if transfer location is non-LPN controlled
715     -- Set value for l_check_cg_comingle, this only need to be checked once before the Loop
716     IF (p_mmtt.transaction_action_id <> inv_globals.g_action_orgxfr)
717        AND(p_mmtt.transaction_action_id <> inv_globals.g_action_intransitshipment)
718        AND(p_mmtt.transaction_action_id <> inv_globals.g_action_intransitreceipt)
719        AND(NVL(p_mmtt.transfer_subinventory, p_mmtt.subinventory_code) IS NOT NULL) THEN
720       SELECT NVL(lpn_controlled_flag, 2)
721         INTO l_skip_comingle_check
722         FROM mtl_secondary_inventories
723        WHERE organization_id = NVL(p_mmtt.transfer_organization, p_mmtt.organization_id)
724          AND secondary_inventory_name = NVL(p_mmtt.transfer_subinventory, p_mmtt.subinventory_code);
725 
726       IF SQL%NOTFOUND THEN
727         IF (l_debug = 1) THEN
728           inv_log_util.TRACE('No sub found when checking lpn_controlled_flag', 'INV_LPN_TRX_PUB', 5);
729         END IF;
730 
731         fnd_message.set_name('INV', 'INV_INT_XSUBCODE');
732         fnd_msg_pub.ADD;
733         RAISE fnd_api.g_exc_error;
734       END IF;
735 
736       IF (l_debug = 1) THEN
737         inv_log_util.TRACE(
738              'Checked lpn_controlled_flag for txnAction '
739           || p_mmtt.transaction_action_id
740           || ' and Sub '
741           || NVL(p_mmtt.transfer_subinventory, p_mmtt.subinventory_code)
742           || ', result='
743           || l_skip_comingle_check
744         , 'INV_LPN_TRX_PUB'
745         , 9
746         );
747       END IF;
748 
749       -- If sub in not subinventory controlled check locator to see if is
750       -- a pjm locator.  If it is no comingle check is requires since the
751       -- locators will be changed anyway.
752       IF (l_skip_comingle_check = 2) THEN
753         BEGIN
754           SELECT 1
755             INTO l_skip_comingle_check
756             FROM mtl_item_locations
757            WHERE project_id IS NOT NULL
758              AND inventory_location_id = NVL(p_mmtt.transfer_to_location, p_mmtt.locator_id)
759              AND subinventory_code = NVL(p_mmtt.transfer_subinventory, p_mmtt.subinventory_code)
760              AND organization_id = NVL(p_mmtt.transfer_organization, p_mmtt.organization_id);
761         EXCEPTION
762           WHEN NO_DATA_FOUND THEN
763             l_skip_comingle_check  := 2;
764             inv_log_util.TRACE('NOTFOUND l_skip_comingle_check=' || l_skip_comingle_check, 'INV_LPN_TRX_PUB', 5);
765         END;
766 
767         IF (l_debug = 1) THEN
768           inv_log_util.TRACE('Checked if is a pjm locator l_skip_comingle_check=' || l_skip_comingle_check, 'INV_LPN_TRX_PUB', 5);
769         END IF;
770       END IF;
771     ELSE
772       l_skip_comingle_check  := 1;
773     END IF;
774 
775     -- Loop and Insert all the LPN contents into MMTT
776     lpnitndx  := tb_lpn_cnts.FIRST;
777 
778     LOOP
779       curlpnrec  := tb_lpn_cnts(lpnitndx);
780 
781       IF (l_debug = 1) THEN
782         inv_log_util.TRACE(
783              ' cntlpnid = '
784           || curlpnrec.content_lpn_id
785           || ',par lpnid='
786           || curlpnrec.parent_lpn_id
787           || ', item_id='
788           || curlpnrec.content_item_id
789           || ',qty='
790           || curlpnrec.quantity
791           || ',sec_qty='
792           || curlpnrec.sec_quantity
793           || ',rev='
794           || curlpnrec.revision
795         , 'INV_LPN_TRX_PUB'
796         , 9
797         );
798       END IF;
799 
800       -- We need to pupulate new MMTTs only for items within LPN. We need to
801       -- weed-out the curlpnrec which stands for these cases :
802       --  *  LPN inside another LPN
803       --  *  LPN which has item associated with it
804       --  *  LPN which has no item inside it
805       IF (curlpnrec.content_item_id IS NOT NULL)
806          AND(curlpnrec.content_lpn_id IS NULL) THEN
807         insrowcnt        := insrowcnt + 1;
808 
809         -- Check if itemid and revision is same as last processed record.
810         -- If it is, then this is for a Lot or Serial attribute
811         IF (
812             curlpnrec.content_item_id = lastitemid
813             AND curlpnrec.parent_lpn_id = lastparentlpnid
814             AND curlpnrec.cost_group_id = lastcostgroupid
815             AND NVL(curlpnrec.revision, '@@') = NVL(lastrevison, '@@')
816            ) THEN
817           sertrxid        := v_lasttrxtmpid;
818           itemqtychanged  := TRUE;
819           /*Bug#5486052. If the UOM of the current record is not same as that of the last record,
820             then convert the qty of the current record into the UOM of the last record.*/
821           IF ( lastuom <> curlpnrec.uom ) THEN
822             l_converted_qty := get_converted_qty(  curlpnrec.content_item_id
823                                                  , curlpnrec.quantity
824                                                  , curlpnrec.uom
825                                                  , lastuom);
826             curlpnrec.quantity := l_converted_qty;
827             curlpnrec.uom := lastuom;
828           END IF;
829           /*Bug#5486052. End*/
830           v_lastitemqty   := v_lastitemqty + curlpnrec.quantity;
831           v_lastitemsecqty := NVL(v_lastitemsecqty,0) + NVL(curlpnrec.sec_quantity,0);   --INVCONV kkillams
832           --Check Lot number
833           IF (curlpnrec.lot_number IS NOT NULL) THEN
834             IF (curlpnrec.lot_number <> lastlotnum) THEN
835               IF (lotqtychanged) THEN
836                 IF (l_debug = 1) THEN
837                   inv_log_util.TRACE('** Going to update last Lot rec1 qty = ' || lotqty, 'INV_LPN_TRX_PUB', 9);
838                 END IF;
839 
840                 UPDATE mtl_transaction_lots_temp
841                    SET transaction_quantity = lotqty
842                      , primary_quantity = lotqty * l_conv_fact /*Bug#5486052.*/
843                      , secondary_quantity   = lotsecqty  --INVCONV kkillams
844                  WHERE transaction_temp_id = v_lasttrxtmpid
845                    AND lot_number = lastlotnum;
846 
847                 lotqtychanged  := FALSE;
848               END IF;
849 
850               IF (l_debug = 1) THEN
851                 inv_log_util.TRACE('** Inserting Lot Number ' || curlpnrec.lot_number, 'INV_LPN_TRX_PUB', 9);
852               END IF;
853 
854               -- Bug 2795134/2712046 Comingle check failed when sub-transfer LPN to non-LPN controlled sub
855               -- Need to add check here as well for same item but different lot numbers
856               -- Bug 2795042, add condition of curlpnrec.cost_group_id IS NOT NULL
857               -- because for WIP putaway where cost group will be derived later, so
858               -- comingle_check will fail if cost_group_id is NULL
859               -- Bug 2886342. Add Or condition of p_mmtt.cost_group_id IS NOT NULL
860               --  This is the cases when cost group suggestion has been stamped on MMTT
861               --  which will be transferred to MOQD or WMS_LPN_CONTENTS later.
862               --  assign_cost_group will not be called, so need to do the comingling check here
863               IF (l_skip_comingle_check = 2)
864                  AND((curlpnrec.cost_group_id IS NOT NULL)
865                      OR(p_mmtt.cost_group_id IS NOT NULL)) THEN
866                 -- Non LPN Controlled, check cost group comingle
867                 IF (l_debug = 1) THEN
868                   inv_log_util.TRACE(
869                        'Transfer to non-LPN controlled location, calling comingle_check with '
870                     || 'p_org_id='
871                     || NVL(p_mmtt.transfer_organization, p_mmtt.organization_id)
872                     || ',p_item_id='
873                     || curlpnrec.content_item_id
874                     || ',p_rev='
875                     || curlpnrec.revision
876                     || ',p_lot='
877                     || curlpnrec.lot_number
878                     || ',sub='
879                     || NVL(p_mmtt.transfer_subinventory, p_mmtt.subinventory_code)
880                     || ',p_loc='
881                     || NVL(p_mmtt.transfer_to_location, p_mmtt.locator_id)
882                     || ',p_cg_id='
883                     || NVL(curlpnrec.cost_group_id, p_mmtt.cost_group_id)
884                   , 'INV_LPN_TRX_PUB'
885                   , 9
886                   );
887                 END IF;
888 
889                 l_comingling_occurs  := 'N';
890                 inv_comingling_utils.comingle_check(
891                   x_return_status              => ret_status
892                 , x_msg_count                  => ret_msgcnt
893                 , x_msg_data                   => ret_msgdata
894                 , x_comingling_occurs          => l_comingling_occurs
895                 , x_count                      => l_count
896                 , p_organization_id            => NVL(p_mmtt.transfer_organization, p_mmtt.organization_id)
897                 , p_inventory_item_id          => curlpnrec.content_item_id
898                 , p_revision                   => curlpnrec.revision
899                 , p_lot_number                 => curlpnrec.lot_number
900                 , p_subinventory_code          => NVL(p_mmtt.transfer_subinventory, p_mmtt.subinventory_code)
901                 , p_locator_id                 => NVL(p_mmtt.transfer_to_location, p_mmtt.locator_id)
902                 , p_lpn_id                     => NULL
903                 , p_cost_group_id              => NVL(curlpnrec.cost_group_id, p_mmtt.cost_group_id)
904                 );
905 
906                 IF (ret_status <> fnd_api.g_ret_sts_success) THEN
907                   IF (l_debug = 1) THEN
908                     inv_log_util.TRACE('**Error: Failed in comingling_check API :' || ret_msgdata, 'INV_LPN_TRX_PUB', 1);
909                   END IF;
910 
911                   RAISE fnd_api.g_exc_error;
912                 END IF;
913 
914                 IF (l_comingling_occurs = 'Y') THEN
915                   IF (l_debug = 1) THEN
916                     inv_log_util.TRACE('**Error: Transaction results in co-mingling :' || ret_msgdata, 'INV_LPN_TRX_PUB', 1);
917                   END IF;
918 
919                   SELECT cost_group
920                     INTO l_cst_grp
921                     FROM cst_cost_groups
922                    WHERE cost_group_id = NVL(curlpnrec.cost_group_id, p_mmtt.cost_group_id);
923 
924                   fnd_message.set_name('INV', 'INV_COMINGLE_FAIL');
925                   fnd_message.set_token('CG', l_cst_grp);
926                   fnd_msg_pub.ADD;
927                   RAISE fnd_api.g_exc_error;
928                 END IF;
929               END IF;
930 
931               sertrxid    := insert_lot_trx(curlpnrec, v_lasttrxtmpid);
932               lastlotnum  := curlpnrec.lot_number;
933               lotqty      := curlpnrec.quantity;
934               lotsecqty   := curlpnrec.sec_quantity; --INVCONV kkillams
935             ELSE
936               -- Item and LotNumber same as previous. Use the transactionId
937               -- used by the last serial insertions.
938               sertrxid       := l_pre_sertrxid;
939               lotqty         := lotqty + curlpnrec.quantity;
940               lotsecqty      := NVL(lotsecqty,0) + NVL(curlpnrec.sec_quantity,0);  --INVCONV kkillams
941               lotqtychanged  := TRUE;
942             END IF;
943           ELSE
944             IF (lotqtychanged) THEN
945               IF (l_debug = 1) THEN
946                 inv_log_util.TRACE('** Going to update last Lot rec2 qty = ' || lotqty, 'INV_LPN_TRX_PUB', 9);
947               END IF;
948 
949               UPDATE mtl_transaction_lots_temp
950                  SET transaction_quantity = lotqty
951                    , primary_quantity = lotqty * l_conv_fact /*Bug#5486052.*/
952                    , secondary_quantity   = lotsecqty  --INVCONV kkillams
953                WHERE transaction_temp_id = v_lasttrxtmpid
954                  AND lot_number = lastlotnum;
955 
956               lotqtychanged  := FALSE;
957             END IF;
958           END IF;
959 
960           IF (curlpnrec.serial_number IS NOT NULL) THEN
961             IF (l_debug = 1) THEN
962               inv_log_util.TRACE('** Inserting Serial Number ' || curlpnrec.serial_number, 'INV_LPN_TRX_PUB', 9);
963             END IF;
964 
965             retval          := insert_ser_trx(curlpnrec.serial_number, sertrxid);
966             l_pre_sertrxid  := sertrxid;
967           END IF;
968         ELSE   -- New item record
969                 -- Bug 2712046 Need to check cost group comingling if the transfer location
970                 -- is non-LPN controlled.
971                 -- Bug 2795042, add condition of curlpnrec.cost_group_id IS NOT NULL
972 		          -- because for WIP putaway where cost group will be derived later, so
973                 -- comingle_check will fail if cost_group_id is NULL
974                 -- Bug 2886342. Add Or condition of p_mmtt.cost_group_id IS NOT NULL
975                 --  This is the cases when cost group suggestion has been stamped on MMTT
976                 --  which will be transferred to MOQD or WMS_LPN_CONTENTS later.
977                 --  assign_cost_group will not be called, so need to do the comingling check here
978 
979           IF (l_skip_comingle_check = 2)
980              AND((curlpnrec.cost_group_id IS NOT NULL)
981                  OR(p_mmtt.cost_group_id IS NOT NULL)) THEN
982             -- Non LPN Controlled, check cost group comingle
983             IF (l_debug = 1) THEN
984               inv_log_util.TRACE(
985                    'Transfer to non-LPN controlled location, calling comingle_check with '
986                 || 'p_org_id='
987                 || NVL(p_mmtt.transfer_organization, p_mmtt.organization_id)
988                 || ',p_item_id='
989                 || curlpnrec.content_item_id
990                 || ',p_rev='
991                 || curlpnrec.revision
992                 || ',p_lot='
993                 || curlpnrec.lot_number
994                 || ',sub='
995                 || NVL(p_mmtt.transfer_subinventory, p_mmtt.subinventory_code)
996                 || ',p_loc='
997                 || NVL(p_mmtt.transfer_to_location, p_mmtt.locator_id)
998                 || ',p_cg_id='
999                 || NVL(curlpnrec.cost_group_id, p_mmtt.cost_group_id)
1000               , 'INV_LPN_TRX_PUB'
1001               , 9
1002               );
1003             END IF;
1004 
1005             inv_comingling_utils.comingle_check(
1006               x_return_status              => ret_status
1007             , x_msg_count                  => ret_msgcnt
1008             , x_msg_data                   => ret_msgdata
1009             , x_comingling_occurs          => l_comingling_occurs
1010             , x_count                      => l_count
1011             , p_organization_id            => NVL(p_mmtt.transfer_organization, p_mmtt.organization_id)
1012             , p_inventory_item_id          => curlpnrec.content_item_id
1013             , p_revision                   => curlpnrec.revision
1014             , p_lot_number                 => curlpnrec.lot_number
1015             , p_subinventory_code          => NVL(p_mmtt.transfer_subinventory, p_mmtt.subinventory_code)
1016             , p_locator_id                 => NVL(p_mmtt.transfer_to_location, p_mmtt.locator_id)
1017             , p_lpn_id                     => NULL
1018             , p_cost_group_id              => NVL(curlpnrec.cost_group_id, p_mmtt.cost_group_id)
1019             );
1020 
1021             IF (ret_status <> fnd_api.g_ret_sts_success) THEN
1022               IF (l_debug = 1) THEN
1023                 inv_log_util.TRACE('**Error: Failed in comingling_check API :' || ret_msgdata, 'INV_LPN_TRX_PUB', 1);
1024               END IF;
1025 
1026               RAISE fnd_api.g_exc_error;
1027             END IF;
1028 
1029             IF (l_comingling_occurs = 'Y') THEN
1030               IF (l_debug = 1) THEN
1031                 inv_log_util.TRACE('**Error: Transaction results in co-mingling :' || ret_msgdata, 'INV_LPN_TRX_PUB', 1);
1032               END IF;
1033 
1034               SELECT cost_group
1035                 INTO l_cst_grp
1036                 FROM cst_cost_groups
1037                WHERE cost_group_id = NVL(curlpnrec.cost_group_id, p_mmtt.cost_group_id);
1038 
1039               fnd_message.set_name('INV', 'INV_COMINGLE_FAIL');
1040               fnd_message.set_token('CG', l_cst_grp);
1041               fnd_msg_pub.ADD;
1042               RAISE fnd_api.g_exc_error;
1043             END IF;
1044           END IF;
1045 
1046           -- End Bug 2712046
1047 
1048           -- if the previous item's qty was changed, then update record
1049           IF (itemqtychanged) THEN
1050             IF (p_mmtt.transaction_action_id = inv_globals.g_action_issue)
1051                OR(p_mmtt.transaction_action_id = inv_globals.g_action_intransitshipment) THEN
1052               v_lastitemqty  := -1 * v_lastitemqty;
1053 
1054               IF v_lastitemsecqty <> 0 THEN -- INVCONV KKILLAMS
1055                 v_lastitemsecqty  := -1 * v_lastitemsecqty;
1056               END IF;
1057             END IF;
1058 
1059             IF (l_debug = 1) THEN
1060               inv_log_util.TRACE('** Going to upd lst rec1. qty = ' || v_lastitemqty, 'INV_LPN_TRX_PUB', 9);
1061             END IF;
1062 
1063             UPDATE mtl_material_transactions_temp
1064                SET transaction_quantity = v_lastitemqty
1065                  , primary_quantity = v_lastitemqty * l_conv_fact /*Bug#5486052.*/
1066                  , secondary_transaction_quantity   =  CASE WHEN v_lastitemsecqty <> 0 THEN v_lastitemsecqty ELSE secondary_transaction_quantity END --INVCONV kkillams
1067              WHERE transaction_temp_id = v_lasttrxtmpid;
1068 
1069             itemqtychanged               := FALSE;
1070             p_mmtt.transaction_quantity  := v_lastitemqty;
1071             p_mmtt.primary_quantity      := v_lastitemqty;
1072             p_mmtt.secondary_transaction_quantity  := CASE WHEN v_lastitemsecqty <> 0 THEN v_lastitemsecqty ELSE p_mmtt.secondary_transaction_quantity END; --INVCONV kkillams
1073 
1074             IF (lotqtychanged) THEN
1075               IF (l_debug = 1) THEN
1076                 inv_log_util.TRACE('** Going to upd lst lot3. qty = ' || lotqty, 'INV_LPN_TRX_PUB', 9);
1077               END IF;
1078 
1079               UPDATE mtl_transaction_lots_temp
1080                  SET transaction_quantity = lotqty
1081                      , primary_quantity = lotqty * l_conv_fact /*Bug#5486052.*/
1082                      , secondary_quantity   = lotsecqty  --INVCONV kkillams
1083                WHERE transaction_temp_id = v_lasttrxtmpid
1084                  AND lot_number = lastlotnum;
1085             END IF;
1086           END IF;
1087 
1088           -- If this an InterOrg Xfer transaction, then call the CostGroup API
1089           -- for the previously entered content Item. At this stage all the
1090           -- rows for this item would have been entered to the temp tables.
1091           IF (lastitemid IS NOT NULL) THEN
1092             IF (p_mmtt.transaction_action_id = inv_globals.g_action_orgxfr)
1093                OR(p_mmtt.transaction_action_id = inv_globals.g_action_intransitshipment)
1094                OR(p_mmtt.transaction_action_id = inv_globals.g_action_intransitreceipt)
1095                OR
1096                  -- Bug 2712046 Cost group and comingling check is not executed when putaway a whole LPN for WIP
1097                  -- Changed to add the condition that if cost group ID is null then also need to call cost group API
1098                (  p_mmtt.cost_group_id IS NULL) THEN
1099               inv_cost_group_pvt.assign_cost_group(
1100                 x_return_status              => ret_status
1101               , x_msg_count                  => ret_msgcnt
1102               , x_msg_data                   => ret_msgdata
1103               , p_organization_id            => p_mmtt.organization_id
1104               , p_mmtt_rec                   => p_mmtt
1105               , p_fob_point                  => NULL
1106               , p_line_id                    => p_mmtt.transaction_temp_id
1107               , p_input_type                 => inv_cost_group_pub.g_input_mmtt
1108               , x_cost_group_id              => l_cst_grp_id
1109               , x_transfer_cost_group_id     => l_xfr_cst_grp_id
1110               );
1111 
1112               IF (ret_status <> fnd_api.g_ret_sts_success) THEN
1113                 IF (l_debug = 1) THEN
1114                   inv_log_util.TRACE(' Error from CostGrpAPI:' || ret_msgdata, 'INV_LPN_TRX_PUB', 1);
1115                 END IF;
1116 
1117                 fnd_message.set_name('INV', 'INV_COST_GROUP_FAILURE');
1118                 fnd_msg_pub.ADD;
1119                 RAISE fnd_api.g_exc_error;
1120               END IF;
1121             -- If this is for an OrgXfr transaction, update the cost_group_id
1122             -- of the content item for this LPN as this could have been
1123             -- changed by the CG API.
1124             END IF;
1125           END IF;
1126 
1127           itemqtychanged  := FALSE;
1128           lotqtychanged   := FALSE;
1129           lotqty          := 0;
1130           lotsecqty           := 0;  --INVCONV kkillams
1131           v_lastitemqty   := curlpnrec.quantity;
1132           v_lastitemsecqty    := curlpnrec.sec_quantity; --INVCONV kkillams
1133 
1134           -- insert a record into MMTT.
1135           IF (p_mmtt.transaction_action_id = inv_globals.g_action_issue)
1136              OR(p_mmtt.transaction_action_id = inv_globals.g_action_intransitshipment)
1137              OR((p_mmtt.transaction_action_id = inv_globals.g_action_inv_lot_translate)
1138                 AND(p_mmtt.transaction_quantity < 0)) THEN
1139             itemqty  := -1 * curlpnrec.quantity;
1140             itemsecqty := -1 * curlpnrec.sec_quantity; --INVCONV kkillams
1141           ELSE
1142             itemqty  := curlpnrec.quantity;
1143             itemsecqty := -1 * curlpnrec.sec_quantity;  --INVCONV kkillams
1144           END IF;
1145 
1146           IF (l_debug = 1) THEN
1147             inv_log_util.TRACE(
1148               '** inserting into MMTT. Mode-Normal. Qty=' || itemqty || ',sub=' || l_lpn_subinv || ',loc=' || l_lpn_locator_id
1149             , 'INV_LPN_TRX_PUB'
1150             , 9
1151             );
1152           END IF;
1153 
1154           insert_line_trx(
1155             curlpnrec
1156           , p_mmtt.transaction_temp_id
1157           , p_mmtt.transaction_action_id
1158           , p_mmtt.organization_id
1159           , l_lpn_subinv
1160           , l_lpn_locator_id
1161           , itemqty
1162           , curlpnrec.cost_group_id
1163           , p_mmtt
1164           , v_lasttrxtmpid
1165           , itemsecqty    --INCONV kkillams
1166           );
1167           sertrxid        := v_lasttrxtmpid;
1168 
1169           /*Bug#5486052. Added the below code to get the primary UOM code and the conversion factor
1170             between the primary UOM and current U0M for the current item.*/
1171           BEGIN
1172             SELECT primary_uom_code
1173             INTO l_primary_uom
1174             FROM mtl_system_items_b
1175             WHERE inventory_item_id = curlpnrec.content_item_id
1176               AND organization_id = curlpnrec.organization_id;
1177           EXCEPTION WHEN OTHERS THEN
1178               NULL;
1179           END;
1180           If ( curlpnrec.uom <> l_primary_uom ) THEN
1181             l_conv_fact := get_converted_qty(  curlpnrec.content_item_id
1182                                              , 1
1183                                              , curlpnrec.uom
1184                                              , l_primary_uom);
1185           ELSE
1186             l_conv_fact := 1;
1187           END IF;
1188           /*Bug#5486052. End.*/
1189 
1190 
1191           IF (l_debug = 1) THEN
1192             inv_log_util.TRACE('MMTT.retval=' || retval || ',lasttrxtmpid=' || v_lasttrxtmpid, 'INV_LPN_TRX_PUB', 9);
1193           END IF;
1194 
1195           IF (curlpnrec.lot_number IS NOT NULL) THEN
1196             sertrxid    := insert_lot_trx(curlpnrec, v_lasttrxtmpid);
1197             lastlotnum  := curlpnrec.lot_number;
1198             lotqty      := v_lastitemqty;
1199             lotsecqty   := v_lastitemsecqty;  --INVCONV kkillams
1200           END IF;
1201 
1202           IF (curlpnrec.serial_number IS NOT NULL) THEN
1203             retval          := insert_ser_trx(curlpnrec.serial_number, sertrxid);
1204             l_pre_sertrxid  := sertrxid;
1205           END IF;
1206         END IF;
1207 
1208         lastitemid       := curlpnrec.content_item_id;
1209         lastrevison      := curlpnrec.revision;
1210         lastcostgroupid  := curlpnrec.cost_group_id;
1211         lastparentlpnid  := curlpnrec.parent_lpn_id;
1212 	lastuom          := curlpnrec.uom; /*Bug#5486052.*/
1213       END IF;   -- curlpnrec.content_item_id is not null
1214 
1215       EXIT WHEN lpnitndx = tb_lpn_cnts.LAST;
1216       lpnitndx   := tb_lpn_cnts.NEXT(lpnitndx);
1217     END LOOP;
1218 
1219     -- Any item or lot qty need to be updated ??
1220     IF (itemqtychanged) THEN
1221       IF (p_mmtt.transaction_action_id = inv_globals.g_action_issue)
1222          OR(p_mmtt.transaction_action_id = inv_globals.g_action_intransitshipment)
1223          OR((p_mmtt.transaction_action_id = inv_globals.g_action_inv_lot_translate)
1224             AND(p_mmtt.transaction_quantity < 0)) THEN
1225         v_lastitemqty  := -1 * v_lastitemqty;
1226         IF v_lastitemsecqty IS NOT NULL THEN
1227            v_lastitemsecqty  := -1 * v_lastitemsecqty;  --INVCONV kkillams
1228         END IF;
1229       END IF;
1230 
1231 
1232       UPDATE mtl_material_transactions_temp
1233          SET transaction_quantity = v_lastitemqty
1234            , primary_quantity = v_lastitemqty * l_conv_fact /*Bug#5486052.*/
1235 -- nsinghi bug#5553546 v_lastitemsecqty is being wrongly assigned as v_lastitemqty in THEN part. change it to v_lastitemsecqty
1236 --	   , secondary_transaction_quantity    = CASE WHEN v_lastitemsecqty <> 0 THEN v_lastitemqty ELSE secondary_transaction_quantity END
1237            , secondary_transaction_quantity    = CASE WHEN v_lastitemsecqty <> 0 THEN v_lastitemsecqty ELSE secondary_transaction_quantity END
1238        WHERE transaction_temp_id = v_lasttrxtmpid;
1239 
1240       p_mmtt.transaction_quantity  := v_lastitemqty;
1241       p_mmtt.primary_quantity      := v_lastitemqty;
1242       p_mmtt.secondary_transaction_quantity  := CASE WHEN v_lastitemsecqty <> 0 THEN v_lastitemsecqty ELSE p_mmtt.secondary_transaction_quantity END; --INVCONV kkillams
1243       itemqtychanged               := FALSE;
1244 
1245       IF (lotqtychanged) THEN
1246         UPDATE mtl_transaction_lots_temp
1247            SET transaction_quantity = lotqty
1248              , primary_quantity = lotqty * l_conv_fact /*Bug#5486052.*/
1249              , secondary_quantity     = lotsecqty  --INVCONV kkillams
1250          WHERE transaction_temp_id = v_lasttrxtmpid
1251            AND lot_number = lastlotnum;
1252       END IF;
1253     END IF;
1254 
1255     -- If this an InterOrg Xfer transaction, then call the CostGroup API
1256     -- for the previously entered content Item. At this stage all the
1257     -- rows for this item would have been entered to the temp tables.
1258     IF (p_mmtt.transaction_action_id = inv_globals.g_action_orgxfr)
1259        OR(p_mmtt.transaction_action_id = inv_globals.g_action_intransitshipment)
1260        OR(p_mmtt.transaction_action_id = inv_globals.g_action_intransitreceipt)
1261        OR
1262          -- Bug 2712046 Cost group and comingling check is not executed when putaway a whole LPN for WIP
1263          -- Changed to add the condition that if cost group ID is null then also need to call cost group API
1264        (  p_mmtt.cost_group_id IS NULL) THEN
1265       inv_cost_group_pvt.assign_cost_group(
1266         x_return_status              => ret_status
1267       , x_msg_count                  => ret_msgcnt
1268       , x_msg_data                   => ret_msgdata
1269       , p_organization_id            => p_mmtt.organization_id
1270       , p_mmtt_rec                   => p_mmtt
1271       , p_fob_point                  => NULL
1272       , p_line_id                    => p_mmtt.transaction_temp_id
1273       , p_input_type                 => inv_cost_group_pub.g_input_mmtt
1274       , x_cost_group_id              => l_cst_grp_id
1275       , x_transfer_cost_group_id     => l_xfr_cst_grp_id
1276       );
1277 
1278       IF (ret_status <> fnd_api.g_ret_sts_success) THEN
1279         IF (l_debug = 1) THEN
1280           inv_log_util.TRACE(' Error from CostGrpAPI:' || ret_msgdata, 'INV_LPN_TRX_PUB', 1);
1281         END IF;
1282 
1283         fnd_message.set_name('INV', 'INV_COST_GROUP_FAILURE');
1284         fnd_msg_pub.ADD;
1285         RAISE fnd_api.g_exc_error;
1286       END IF;
1287     -- If this is for an OrgXfr transaction, update the cost_group_id
1288     -- of the content item for this LPN as this could have been
1289     -- changed by the CG API.
1290     END IF;
1291 
1292     RETURN insrowcnt;
1293   END;
1294 
1295   --FOB changes. Patchset J. This API is only called wif PO pathset J is installed
1296   -- We track the FOB info in MMTT + MMT.
1297   PROCEDURE update_fob_point(
1298     v_mmtt          IN OUT NOCOPY mtl_material_transactions_temp%ROWTYPE
1299   , x_return_status IN OUT NOCOPY VARCHAR2
1300   , x_msg_data      IN OUT NOCOPY VARCHAR2
1301   , x_msg_count     IN OUT NOCOPY NUMBER
1302   ) IS
1303     l_fob_point               NUMBER;
1304     l_intransit_inv_account   NUMBER;
1305     l_transfer_transaction_id NUMBER;
1306     l_shipment_line_id        NUMBER;
1307     l_mmt_transaction_id      NUMBER;
1308     l_debug                   NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
1309   BEGIN
1310     x_return_status  := fnd_api.g_ret_sts_success;
1311 
1312     IF (l_debug = 1) THEN
1313       inv_log_util.TRACE('PO J installed', 'INV_LPN_TRX_PUB', 1);
1314       inv_log_util.TRACE('Updating FOB_POINT', 'INV_LPN_TRX_PUB', 1);
1315       inv_log_util.TRACE('organization_id' || v_mmtt.organization_id, 'INV_LPN_TRX_PUB', 1);
1316       inv_log_util.TRACE('xfr_organization_id' || v_mmtt.transfer_organization, 'INV_LPN_TRX_PUB', 1);
1317       inv_log_util.TRACE('trx_action_id' || v_mmtt.transaction_action_id, 'INV_LPN_TRX_PUB', 1);
1318       inv_log_util.TRACE('trx_temp_id' || v_mmtt.transaction_temp_id, 'INV_LPN_TRX_PUB', 1);
1319     END IF;
1320 
1321     IF (v_mmtt.transaction_temp_id IS NULL) THEN
1322       x_msg_data  := 'Trx_temp_id is null return success';
1323 
1324       IF (l_debug = 1) THEN
1325         inv_log_util.TRACE('Trx_temp_id is null return success', 'INV_LPN_TRX_PUB', 1);
1326       END IF;
1327 
1328       RETURN;
1329     END IF;
1330 
1331     IF (v_mmtt.transaction_action_id = inv_globals.g_action_intransitshipment) THEN
1332       BEGIN
1333         SELECT fob_point
1334              , intransit_inv_account
1335           INTO l_fob_point
1336              , l_intransit_inv_account
1337           FROM mtl_interorg_parameters
1338          WHERE from_organization_id = v_mmtt.organization_id
1339            AND to_organization_id = v_mmtt.transfer_organization;
1340       EXCEPTION
1341         WHEN NO_DATA_FOUND THEN
1342           IF (l_debug = 1) THEN
1343             inv_log_util.TRACE('no data found INV_FOB_NOT_DEFINED', 'INV_LPN_TRX_PUB', 1);
1344           END IF;
1345 
1346           fnd_message.set_name('INV', 'INV_FOB_NOT_DEFINED');
1347           fnd_message.set_token('ENTITY1', v_mmtt.organization_id);
1348           fnd_msg_pub.ADD;
1349           RAISE fnd_api.g_exc_error;
1350       END;
1351 
1352       UPDATE mtl_material_transactions_temp
1353          SET fob_point = l_fob_point
1354            , intransit_account = l_intransit_inv_account
1355        WHERE transaction_temp_id = v_mmtt.transaction_temp_id;
1356 
1357       /**bug 3371548 assign values to the cursor*/
1358       v_mmtt.fob_point          := l_fob_point;
1359       v_mmtt.intransit_account  := l_intransit_inv_account;
1360     ELSIF(v_mmtt.transaction_action_id = inv_globals.g_action_intransitreceipt) THEN
1361       BEGIN
1362         SELECT shipment_line_id
1363           INTO l_shipment_line_id
1364           FROM rcv_transactions
1365          WHERE transaction_id =v_mmtt.rcv_transaction_id;
1366       EXCEPTION
1367         WHEN NO_DATA_FOUND THEN
1368           IF (l_debug = 1) THEN
1369             inv_log_util.TRACE('no data found rcv_transactions', 'INV_LPN_TRX_PUB', 1);
1370           END IF;
1371 
1372           RAISE fnd_api.g_exc_error;
1373       END;
1374 
1375         SELECT Nvl(mmt_transaction_id,-1)
1376           INTO l_mmt_transaction_id
1377           FROM rcv_shipment_lines
1378          WHERE shipment_line_id = l_shipment_line_id;
1379 
1380       IF (l_mmt_transaction_id = -1) then
1381 	  /**bug 3527331
1382 	  we have an issue on FOB point backward
1383 	  compatibility when deliver a shipment created before FOB point
1384 	    feature was enabled. Inv trxn mgr should take care of this
1385 	    corner case.
1386 	  In case rcv record does not have the reference to the
1387 	  correspoding shipment transaction, we shoudl populate the
1388 	  rcpt transaction with the fob_point by running a fresh query
1389 	  int mtl_interorg parameters*/
1390 	    BEGIN
1391 	       SELECT fob_point
1392 		 , intransit_inv_account
1393 		 INTO l_fob_point
1394 		 , l_intransit_inv_account
1395 		 FROM mtl_interorg_parameters
1396 		 WHERE from_organization_id = v_mmtt.transfer_organization
1397 		 AND to_organization_id = v_mmtt.organization_id;
1398 	    EXCEPTION
1399 	       WHEN NO_DATA_FOUND THEN
1400 		  IF (l_debug = 1) THEN
1401 		     inv_log_util.TRACE('no data found INV_FOB_NOT_DEFINED', 'INV_LPN_TRX_PUB', 1);
1402 		  END IF;
1403 		  fnd_message.set_name('INV', 'INV_FOB_NOT_DEFINED');
1404 		  fnd_message.set_token('ENTITY1', v_mmtt.organization_id);
1405 		  fnd_msg_pub.ADD;
1406 		  RAISE fnd_api.g_exc_error;
1407 	    END;
1408        else
1409       BEGIN
1410 	 SELECT fob_point
1411 	   , intransit_account
1412 	   INTO l_fob_point
1413 	   , l_intransit_inv_account
1414 	   FROM mtl_material_transactions
1415 	   WHERE transaction_id = l_mmt_transaction_id;
1416       EXCEPTION
1417 	 WHEN NO_DATA_FOUND THEN
1418 	    IF (l_debug = 1) THEN
1419 	       inv_log_util.TRACE('no data found mtl_material_transactions', 'INV_LPN_TRX_PUB', 1);
1420 	    END IF;
1421 	    RAISE fnd_api.g_exc_error;
1422       END;
1423       END IF;
1424       UPDATE mtl_material_transactions_temp
1425 	SET fob_point = l_fob_point
1426 	, intransit_account = l_intransit_inv_account
1427 	WHERE transaction_temp_id = v_mmtt.transaction_temp_id;
1428 
1429       /**bug 3371548 assign values to the cursor*/
1430       v_mmtt.fob_point          := l_fob_point;
1431       v_mmtt.intransit_account  := l_intransit_inv_account;
1432     END IF;   -- intransit Rcpt
1433   EXCEPTION
1434     WHEN fnd_api.g_exc_error THEN
1435       x_return_status  := fnd_api.g_ret_sts_error;
1436       fnd_msg_pub.count_and_get(p_count => x_msg_count, p_data => x_msg_data);
1437     WHEN OTHERS THEN
1438       x_return_status  := fnd_api.g_ret_sts_unexp_error;
1439   END update_fob_point;
1440 
1441   /********************************************************************
1442    * Pack or Unpack item/lpn. If lot or serial controlled, then
1443    *  query corresponding lot or serial records
1444    *******************************************************************/
1445   PROCEDURE Call_Pack_Unpack (
1446     p_tempid           NUMBER
1447   , p_content_lpn      NUMBER
1448   , p_lpn              NUMBER
1449   , p_item_rec         inv_validate.item
1450   , p_revision         VARCHAR2
1451   , p_primary_qty      NUMBER
1452   , p_qty              NUMBER
1453   , p_uom              VARCHAR2
1454   , p_org_id           NUMBER
1455   , p_subinv           VARCHAR2
1456   , p_locator          NUMBER
1457   , p_operation        NUMBER
1458   , p_cost_grp_id      NUMBER
1459   , p_trx_action       NUMBER
1460   , p_source_header_id NUMBER
1461   , p_source_name      VARCHAR2
1462   , p_source_type_id   NUMBER   --bug 3158847
1463   , p_sec_qty          NUMBER   --INVCONV kkillams
1464   , p_sec_uom          VARCHAR2 --INVCONV kkillams
1465   , p_source_trx_id    NUMBER
1466   ) IS
1467     v_sertrxid  NUMBER;
1468     v_lotfound  BOOLEAN      := FALSE;
1469     v_serfound  BOOLEAN      := FALSE;
1470     item_id     NUMBER       := NULL;
1471     v_lotnum    mtl_transaction_lots_temp.lot_number%TYPE;
1472     v_lotqty    NUMBER;
1473     v_lotsecqty    NUMBER;
1474     v_serqty    NUMBER;
1475     l_operation NUMBER       := p_operation;
1476     l_ignore_item_controls NUMBER;
1477 
1478     CURSOR c_sertmp IS
1479       SELECT fm_serial_number
1480            , to_serial_number
1481         FROM mtl_serial_numbers_temp
1482        WHERE transaction_temp_id = v_sertrxid;
1483 
1484     CURSOR c_lottmp IS
1485       SELECT lot_number
1486            , primary_quantity
1487            , transaction_quantity
1488            , secondary_quantity
1489            , serial_transaction_temp_id
1490         FROM mtl_transaction_lots_temp
1491        WHERE transaction_temp_id = p_tempid;
1492 
1493     l_debug     NUMBER       := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
1494   BEGIN
1495     IF ( p_operation = g_unpack AND p_content_lpn IS NULL
1496          AND ( (p_source_type_id = inv_globals.g_sourcetype_purchaseorder AND p_trx_action = inv_globals.g_action_receipt)
1497             OR (p_source_type_id = inv_globals.g_sourcetype_rma AND p_trx_action = inv_globals.g_action_receipt)
1498             OR (p_source_type_id = inv_globals.g_sourcetype_intreq AND p_trx_action = inv_globals.g_action_intransitreceipt)
1499             OR (p_source_type_id = inv_globals.g_sourcetype_inventory AND p_trx_action = inv_globals.g_action_intransitreceipt)))
1500     THEN
1501       -- For the above types ignore the item controls
1502       l_ignore_item_controls := 1;
1503     ELSE
1504       -- Enforce item controls
1505       l_ignore_item_controls := 2;
1506     END IF;
1507 
1508     /*IF ( l_lpn.lpn_context = 4 AND p_trx_action_id = 8 ) THEN
1509       -- Change operation to new Adjust type
1510       l_operation := g_adjust;
1511     END IF;*/
1512 
1513     IF (p_content_lpn IS NOT NULL) THEN
1514       -- If content_lpn_id column has value then discard the item_id
1515       -- If content_lpn_id is Not NULL then we are packing/unpacking a
1516       -- whole LPN to/from another LPN, so no need to consider Lots and Serials
1517       IF (l_debug = 1) THEN
1518         inv_log_util.TRACE('** pack/unpack of whole LPN **', 'INV_LPN_TRX_PUB', 9);
1519       END IF;
1520 
1521       WMS_Container_PVT.PackUnpack_Container(
1522         p_api_version           => 1.0
1523       , p_init_msg_list         => fnd_api.g_false
1524       , p_validation_level      => fnd_api.g_valid_level_none
1525       , x_return_status         => ret_status
1526       , x_msg_count             => ret_msgcnt
1527       , x_msg_data              => ret_msgdata
1528       , p_caller                => 'INV_TRNSACTION'
1529       , p_lpn_id                => p_lpn
1530       , p_content_lpn_id        => p_content_lpn
1531       , p_content_item_id       => item_id
1532       , p_revision              => p_revision
1533       , p_primary_quantity      => ABS(p_primary_qty)
1534       , p_quantity              => ABS(p_qty)
1535       , p_uom                   => p_uom
1536       , p_sec_quantity          => ABS(p_sec_qty)  --INVCONV kkillams
1537       , p_sec_uom               => p_sec_uom       --INVCONV kkillams
1538       , p_organization_id       => p_org_id
1539       , p_subinventory          => p_subinv
1540       , p_locator_id            => p_locator
1541       , p_operation             => l_operation
1542       , p_cost_group_id         => p_cost_grp_id
1543       , p_source_header_id      => p_source_header_id
1544       , p_source_name           => p_source_name
1545       , p_source_transaction_id => p_source_trx_id
1546       );
1547 
1548       IF (ret_status <> fnd_api.g_ret_sts_success) THEN
1549         IF (l_debug = 1) THEN
1550           inv_log_util.TRACE('**Error from LPN pack/unpack :' || ret_status, 'INV_LPN_TRX_PUB', 1);
1551           inv_log_util.TRACE(
1552                '**p_lpn='
1553             || p_lpn
1554             || ',p_content_lpn='
1555             || p_content_lpn
1556             || ',item_id='
1557             || item_id
1558             || ',p_revision='
1559             || p_revision
1560             || ',p_org_id='
1561             || p_org_id
1562           , 'INV_LPN_TRX_PUB'
1563           , 1
1564           );
1565         END IF;
1566 
1567         IF (l_debug = 1) THEN
1568           inv_log_util.TRACE(
1569             ',p_subinv=' || p_subinv || ',p_locator=' || p_locator || ',p_operation=' || p_operation || ',p_cost_grp_id=' || p_cost_grp_id
1570           , 'INV_LPN_TRX_PUB'
1571           , 1
1572           );
1573         END IF;
1574 
1575         fnd_message.set_name('INV', 'INV_PACKUNPACK_FAILURE');
1576         fnd_msg_pub.ADD;
1577         RAISE fnd_api.g_exc_error;
1578       END IF;
1579     ELSE
1580       -- We are packing/unpacking an Item. Check for Lot and Serials
1581       item_id := p_item_rec.inventory_item_id;
1582 
1583       -- retrieve corresponding lot numbers
1584       FOR v_lottmp IN c_lottmp LOOP
1585         v_lotfound  := TRUE;
1586         v_sertrxid  := v_lottmp.serial_transaction_temp_id;
1587         v_lotnum    := v_lottmp.lot_number;
1588         v_lotqty    := v_lottmp.primary_quantity;
1589         v_lotsecqty := v_lottmp.secondary_quantity; --INVCONV kkillams
1590         v_serfound  := FALSE;
1591 
1592         FOR v_sertmp IN c_sertmp LOOP
1593           IF (l_debug = 1) THEN
1594             inv_log_util.TRACE('** Lot and Serial controlled**', 'INV_LPN_TRX_PUB', 9);
1595           END IF;
1596 
1597           v_serfound  := TRUE;
1598 
1599           IF (v_sertmp.to_serial_number IS NULL)
1600              OR(v_sertmp.fm_serial_number = v_sertmp.to_serial_number) THEN
1601             v_serqty  := 1;
1602           ELSE
1603             v_serqty  := inv_serial_number_pub.get_serial_diff(v_sertmp.fm_serial_number, v_sertmp.to_serial_number);
1604           END IF;
1605 
1606           /*3158847*/
1607           IF (
1608               p_item_rec.serial_number_control_code = 6 /*so issue*/
1609               AND p_source_type_id = 12 /*rma*/
1610               AND p_trx_action = 27 /*receipt*/
1611               AND p_operation = 1 /*pack*/
1612              ) THEN   /*pack as vanilla*/
1613             inv_log_util.TRACE('** Packing rma so issue serial as vanilla for receipt**', 'INV_LPN_TRX_PUB', 9);
1614             WMS_Container_PVT.PackUnpack_Container (
1615               p_api_version           => 1.0
1616             , p_init_msg_list         => fnd_api.g_false
1617             , x_return_status         => ret_status
1618             , p_validation_level      => fnd_api.g_valid_level_none
1619             , x_msg_count             => ret_msgcnt
1620             , x_msg_data              => ret_msgdata
1621             , p_caller                => 'INV_TRNSACTION'
1622             , p_lpn_id                => p_lpn
1623             , p_content_lpn_id        => p_content_lpn
1624             , p_content_item_id       => item_id
1625             , p_revision              => p_revision
1626             , p_lot_number            => v_lotnum
1627             , p_from_serial_number    => NULL --3158847
1628             , p_to_serial_number      => NULL --3158847v_sertmp.to_serial_number
1629             , p_primary_quantity      => v_serqty
1630             , p_quantity              => v_serqty
1631             , p_uom                   => p_item_rec.primary_uom_code
1632             , p_organization_id       => p_org_id
1633             , p_subinventory          => p_subinv
1634             , p_locator_id            => p_locator
1635             , p_operation             => l_operation
1636             , p_cost_group_id         => p_cost_grp_id
1637             , p_source_header_id      => p_source_header_id
1638             , p_source_name           => p_source_name
1639             , p_source_transaction_id => p_source_trx_id
1640             );
1641           ELSE
1642             WMS_Container_PVT.PackUnpack_Container (
1643               p_api_version           => 1.0
1644             , p_init_msg_list         => fnd_api.g_false
1645             , x_return_status         => ret_status
1646             , p_validation_level      => fnd_api.g_valid_level_none
1647             , x_msg_count             => ret_msgcnt
1648             , x_msg_data              => ret_msgdata
1649             , p_caller                => 'INV_TRNSACTION'
1650             , p_lpn_id                => p_lpn
1651             , p_content_lpn_id        => p_content_lpn
1652             , p_content_item_id       => item_id
1653             , p_revision              => p_revision
1654             , p_lot_number            => v_lotnum
1655             , p_from_serial_number    => v_sertmp.fm_serial_number
1656             , p_to_serial_number      => v_sertmp.to_serial_number
1657             , p_primary_quantity      => v_serqty
1658             , p_quantity              => v_serqty
1659             , p_uom                   => p_item_rec.primary_uom_code
1660             , p_organization_id       => p_org_id
1661             , p_subinventory          => p_subinv
1662             , p_locator_id            => p_locator
1663             , p_operation             => l_operation
1664             , p_cost_group_id         => p_cost_grp_id
1665             , p_source_header_id      => p_source_header_id
1666             , p_source_name           => p_source_name
1667             , p_source_transaction_id => p_source_trx_id
1668             );
1669           END IF;   /*end of 3158847*/
1670 
1671           IF (ret_status <> fnd_api.g_ret_sts_success) THEN
1672             IF (l_debug = 1) THEN
1673               inv_log_util.TRACE('**Error from pack/unpack :' || ret_status, 'INV_LPN_TRX_PUB', 1);
1674               inv_log_util.TRACE(
1675                    '**p_lpn='
1676                 || p_lpn
1677                 || ',p_content_lpn='
1678                 || p_content_lpn
1679                 || ',item_id='
1680                 || item_id
1681                 || ',p_revision='
1682                 || p_revision
1683                 || ',v_lotnum='
1684                 || v_lotnum
1685                 || ',ser='
1686                 || v_sertmp.fm_serial_number
1687               , 'INV_LPN_TRX_PUB'
1688               , 1
1689               );
1690               inv_log_util.TRACE(
1691                    ',p_org_id='
1692                 || p_org_id
1693                 || ',p_subinv='
1694                 || p_subinv
1695                 || ',p_locator='
1696                 || p_locator
1697                 || ',p_operation='
1698                 || p_operation
1699                 || ',p_cost_grp_id='
1700                 || p_cost_grp_id
1701               , 'INV_LPN_TRX_PUB'
1702               , 1
1703               );
1704             END IF;
1705 
1706             fnd_message.set_name('INV', 'INV_PACKUNPACK_FAILURE');
1707             fnd_msg_pub.ADD;
1708             RAISE fnd_api.g_exc_error;
1709           END IF;
1710         END LOOP;
1711 
1712         IF (NOT v_serfound) THEN
1713           IF (l_debug = 1) THEN
1714             inv_log_util.TRACE('**** Lot controlled **', 'INV_LPN_TRX_PUB', 9);
1715           END IF;
1716 
1717           WMS_Container_PVT.PackUnpack_Container (
1718             p_api_version           => 1.0
1719           , p_init_msg_list         => fnd_api.g_false
1720           , p_validation_level      => fnd_api.g_valid_level_none
1721           , x_return_status         => ret_status
1722           , x_msg_count             => ret_msgcnt
1723           , x_msg_data              => ret_msgdata
1724           , p_caller                => 'INV_TRNSACTION'
1725           , p_lpn_id                => p_lpn
1726           , p_content_lpn_id        => p_content_lpn
1727           , p_content_item_id       => item_id
1728           , p_revision              => p_revision
1729           , p_lot_number            => v_lotnum
1730           , p_primary_quantity      => ABS(v_lottmp.primary_quantity)
1731           , p_quantity              => ABS(v_lottmp.transaction_quantity)
1732           , p_uom                   => p_uom
1733           , p_sec_quantity          => ABS(v_lotsecqty)              --INVCONV kkillams
1734           , p_sec_uom               => p_item_rec.secondary_uom_code --INVCONV kkillams
1735           , p_organization_id       => p_org_id
1736           , p_subinventory          => p_subinv
1737           , p_locator_id            => p_locator
1738           , p_operation             => l_operation
1739           , p_cost_group_id         => p_cost_grp_id
1740           , p_source_header_id      => p_source_header_id
1741           , p_source_name           => p_source_name
1742           , p_source_transaction_id => p_source_trx_id
1743           );
1744 
1745           IF (ret_status <> fnd_api.g_ret_sts_success) THEN
1746             IF (l_debug = 1) THEN
1747               inv_log_util.TRACE('**Error from pack/unpack :' || ret_status, 'INV_LPN_TRX_PUB', 1);
1748               inv_log_util.TRACE(
1749                    '**p_lpn='
1750                 || p_lpn
1751                 || ',p_content_lpn='
1752                 || p_content_lpn
1753                 || ',item_id='
1754                 || item_id
1755                 || ',p_revision='
1756                 || p_revision
1757                 || 'lot='
1758                 || v_lotnum
1759                 || ',p_org_id='
1760                 || p_org_id
1761               , 'INV_LPN_TRX_PUB'
1762               , 1
1763               );
1764               inv_log_util.TRACE(
1765                 ',p_subinv=' || p_subinv || ',p_locator=' || p_locator || ',p_operation=' || p_operation || ',p_cost_grp_id='
1766                 || p_cost_grp_id
1767               , 'INV_LPN_TRX_PUB'
1768               , 1
1769               );
1770             END IF;
1771 
1772             fnd_message.set_name('INV', 'INV_PACKUNPACK_FAILURE');
1773             fnd_msg_pub.ADD;
1774             RAISE fnd_api.g_exc_error;
1775           END IF;
1776         END IF;
1777       END LOOP;
1778 
1779       IF (NOT v_lotfound) THEN
1780         v_sertrxid  := p_tempid;
1781         v_serfound  := FALSE;
1782 
1783         FOR v_sertmp IN c_sertmp LOOP
1784           IF (l_debug = 1) THEN
1785             inv_log_util.TRACE('** Serial controlled**.fmser=' || v_sertmp.fm_serial_number || ', toser=' || v_sertmp.to_serial_number
1786             , 'INV_LPN_TRX_PUB', 9);
1787           END IF;
1788 
1789           v_serfound  := TRUE;
1790 
1791           IF (v_sertmp.to_serial_number IS NULL)
1792              OR(v_sertmp.fm_serial_number = v_sertmp.to_serial_number) THEN
1793             v_serqty  := 1;
1794           ELSE
1795             v_serqty  := inv_serial_number_pub.get_serial_diff(v_sertmp.fm_serial_number, v_sertmp.to_serial_number);
1796           END IF;
1797 
1798           /*3158847*/
1799           IF (
1800               p_item_rec.serial_number_control_code = 6 /*so issue*/
1801               AND p_source_type_id = 12 /*rma*/
1802               AND p_trx_action = 27 /*receipt*/
1803               AND p_operation = 1 /*pack*/
1804              ) THEN   /*pack as vanilla*/
1805             inv_log_util.TRACE('** Packing rma so issue serial as vanilla for receipt**', 'INV_LPN_TRX_PUB', 9);
1806             WMS_Container_PVT.PackUnpack_Container (
1807               p_api_version           => 1.0
1808             , p_init_msg_list         => fnd_api.g_false
1809             , x_return_status         => ret_status
1810             , p_validation_level      => fnd_api.g_valid_level_none
1811             , x_msg_count             => ret_msgcnt
1812             , x_msg_data              => ret_msgdata
1813             , p_caller                => 'INV_TRNSACTION'
1814             , p_lpn_id                => p_lpn
1815             , p_content_lpn_id        => p_content_lpn
1816             , p_content_item_id       => item_id
1817             , p_revision              => p_revision
1818             , p_lot_number            => v_lotnum
1819             , p_from_serial_number    => NULL --3158847
1820             , p_to_serial_number      => NULL --3158847v_sertmp.to_serial_number
1821             , p_primary_quantity      => v_serqty
1822             , p_quantity              => v_serqty
1823             , p_uom                   => p_item_rec.primary_uom_code
1824             , p_organization_id       => p_org_id
1825             , p_subinventory          => p_subinv
1826             , p_locator_id            => p_locator
1827             , p_operation             => l_operation
1828             , p_cost_group_id         => p_cost_grp_id
1829             , p_source_header_id      => p_source_header_id
1830             , p_source_name           => p_source_name
1831             , p_source_transaction_id => p_source_trx_id
1832             );
1833           ELSE
1834             WMS_Container_PVT.PackUnpack_Container (
1835               p_api_version           => 1.0
1836             , p_init_msg_list         => fnd_api.g_false
1837             , x_return_status         => ret_status
1838             , x_msg_count             => ret_msgcnt
1839             , x_msg_data              => ret_msgdata
1840             , p_validation_level      => fnd_api.g_valid_level_none
1841             , p_caller                => 'INV_TRNSACTION'
1842             , p_lpn_id                => p_lpn
1843             , p_content_lpn_id        => p_content_lpn
1844             , p_content_item_id       => item_id
1845             , p_revision              => p_revision
1846             , p_from_serial_number    => v_sertmp.fm_serial_number
1847             , p_to_serial_number      => v_sertmp.to_serial_number
1848             , p_primary_quantity      => v_serqty
1849             , p_quantity              => v_serqty
1850             , p_uom                   => p_item_rec.primary_uom_code
1851             , p_organization_id       => p_org_id
1852             , p_subinventory          => p_subinv
1853             , p_locator_id            => p_locator
1854             , p_operation             => l_operation
1855             , p_cost_group_id         => p_cost_grp_id
1856             , p_source_header_id      => p_source_header_id
1857             , p_source_name           => p_source_name
1858             , p_source_transaction_id => p_source_trx_id
1859             );
1860           END IF;   /*3158847*/
1861 
1862           IF (ret_status <> fnd_api.g_ret_sts_success) THEN
1863             IF (l_debug = 1) THEN
1864               inv_log_util.TRACE(
1865                    '**Error from pack/unpack :'
1866                 || ret_status
1867                 || ',p_lpn='
1868                 || p_lpn
1869                 || ',p_content_lpn='
1870                 || p_content_lpn
1871                 || ',item_id='
1872                 || item_id
1873                 || ',fmser='
1874                 || v_sertmp.fm_serial_number
1875               , 'INV_LPN_TRX_PUB'
1876               , 1
1877               );
1878               inv_log_util.TRACE(
1879                 'toser=' || v_sertmp.to_serial_number || ',org=' || p_org_id || ',p_subinv=' || p_subinv || ',p_locator=' || p_locator
1880               , 'INV_LPN_TRX_PUB'
1881               , 9
1882               );
1883             END IF;
1884 
1885             fnd_message.set_name('INV', 'INV_PACKUNPACK_FAILURE');
1886             fnd_msg_pub.ADD;
1887             RAISE fnd_api.g_exc_error;
1888           END IF;
1889         END LOOP;
1890 
1891         IF (NOT v_serfound) THEN
1892           IF (l_debug = 1) THEN
1893             inv_log_util.TRACE('** NOT Lot OR Serial controlled**', 'INV_LPN_TRX_PUB', 9);
1894           END IF;
1895 
1896           WMS_Container_PVT.PackUnpack_Container (
1897             p_api_version           => 1.0
1898           , p_init_msg_list         => fnd_api.g_false
1899           , x_return_status         => ret_status
1900           , x_msg_count             => ret_msgcnt
1901           , x_msg_data              => ret_msgdata
1902           , p_caller                => 'INV_TRNSACTION'
1903           , p_lpn_id                => p_lpn
1904           , p_validation_level      => fnd_api.g_valid_level_none
1905           , p_content_lpn_id        => p_content_lpn
1906           , p_content_item_id       => item_id
1907           , p_revision              => p_revision
1908           , p_primary_quantity      => ABS(p_primary_qty)
1909           , p_quantity              => ABS(p_qty)
1910           , p_uom                   => p_uom
1911           , p_sec_quantity          => ABS(p_sec_qty)                --INVCONV kkillams
1912           , p_sec_uom               => p_item_rec.secondary_uom_code --INVCONV kkillams
1913           , p_organization_id       => p_org_id
1914           , p_subinventory          => p_subinv
1915           , p_locator_id            => p_locator
1916           , p_operation             => l_operation
1917           , p_cost_group_id         => p_cost_grp_id
1918           , p_source_header_id      => p_source_header_id
1919           , p_source_name           => p_source_name
1920           , p_source_transaction_id => p_source_trx_id
1921           );
1922 
1923           IF (ret_status <> fnd_api.g_ret_sts_success) THEN
1924             IF (l_debug = 1) THEN
1925               inv_log_util.TRACE('**Error from pack/unpack :' || ret_status, 'INV_LPN_TRX_PUB', 1);
1926               inv_log_util.TRACE(
1927                    '**p_lpn='
1928                 || p_lpn
1929                 || ',p_content_lpn='
1930                 || p_content_lpn
1931                 || ',item_id='
1932                 || item_id
1933                 || ',p_revision='
1934                 || p_revision
1935                 || ',p_org_id='
1936                 || p_org_id
1937               , 'INV_LPN_TRX_PUB'
1938               , 1
1939               );
1940             END IF;
1941 
1942             IF (l_debug = 1) THEN
1943               inv_log_util.TRACE(
1944                 ',p_subinv=' || p_subinv || ',p_locator=' || p_locator || ',p_operation=' || p_operation || ',p_cost_grp_id='
1945                 || p_cost_grp_id
1946               , 'INV_LPN_TRX_PUB'
1947               , 1
1948               );
1949             END IF;
1950 
1951             fnd_message.set_name('INV', 'INV_PACKUNPACK_FAILURE');
1952             fnd_msg_pub.ADD;
1953             RAISE fnd_api.g_exc_error;
1954           END IF;
1955         END IF;
1956       END IF;
1957     END IF; -- p_content_lpn is NULL
1958 
1959     IF (p_operation = g_pack) THEN
1960       IF (l_debug = 1) THEN
1961         inv_log_util.TRACE('** Pack OK', 'INV_LPN_TRX_PUB', 9);
1962       END IF;
1963     ELSIF(p_operation = g_unpack) THEN
1964       IF (l_debug = 1) THEN
1965         inv_log_util.TRACE('** Unpack OK', 'INV_LPN_TRX_PUB', 9);
1966       END IF;
1967     END IF;
1968   END;
1969 
1970   /********************************************************************
1971    * Update the status of the LPN
1972    *******************************************************************/
1973   PROCEDURE update_lpn_status(v_lpn wms_container_pub.lpn) IS
1974     l_debug NUMBER := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
1975   BEGIN
1976     WMS_Container_PVT.Modify_LPN (
1977       p_api_version      => 1.0
1978     , p_init_msg_list    => fnd_api.g_true
1979     , p_commit           => fnd_api.g_false
1980     , p_validation_level => fnd_api.g_valid_level_none
1981     , x_return_status    => ret_status
1982     , x_msg_count        => ret_msgcnt
1983     , x_msg_data         => ret_msgdata
1984     , p_caller           => 'INV_TRNSACTION'
1985     , p_lpn              => v_lpn
1986     );
1987 
1988     IF (ret_status <> fnd_api.g_ret_sts_success) THEN
1989       IF (l_debug = 1) THEN
1990         inv_log_util.TRACE('Error from modify_lpn :' || ret_status, 'INV_LPN_TRX_PUB', 1);
1991         inv_log_util.TRACE('Error msg :' || ret_msgdata || 'msgcnt=' || ret_msgcnt, 'INV_LPN_TRX_PUB', 1);
1992       END IF;
1993 
1994       fnd_message.set_name('INV', 'INV_LPN_UPDATE_FAILURE');
1995       fnd_msg_pub.ADD;
1996       RAISE fnd_api.g_exc_error;
1997     END IF;
1998   END;
1999 
2000   /********************************************************************
2001    * Insert a row into MTL_SERIAL_NUMBERS_TEMP which is a copy of another msnt row
2002    *******************************************************************/
2003   PROCEDURE copy_msnt(p_source_row_id ROWID, p_new_sertrxid NUMBER, p_new_fm_serial VARCHAR2, p_new_to_serial VARCHAR2) IS
2004     l_api_name    CONSTANT VARCHAR2(30)                      := 'COPY_MSNT';
2005     l_api_version CONSTANT NUMBER                            := 1.0;
2006     l_debug                NUMBER                            := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
2007     l_sertmp_rec           mtl_serial_numbers_temp%ROWTYPE;
2008   BEGIN
2009     IF (l_debug = 1) THEN
2010       inv_log_util.TRACE('Call to Copy_MSNT rowid=' || p_source_row_id, 'INV_LPN_TRX_PUB', 9);
2011       inv_log_util.TRACE('newsertrxid=' || p_new_sertrxid || ' newfsn=' || p_new_fm_serial || ' newtsn=' || p_new_to_serial
2012       , 'INV_LPN_TRX_PUB', 9);
2013     END IF;
2014 
2015     SELECT *
2016       INTO l_sertmp_rec
2017       FROM mtl_serial_numbers_temp
2018      WHERE ROWID = p_source_row_id;
2019 
2020     --Insert new record for split serials
2021     INSERT INTO mtl_serial_numbers_temp
2022                 (
2023                  transaction_temp_id
2024                , last_update_date
2025                , last_updated_by
2026                , creation_date
2027                , created_by
2028                , last_update_login
2029                , request_id
2030                , program_application_id
2031                , program_id
2032                , program_update_date
2033                , vendor_serial_number
2034                , vendor_lot_number
2035                , fm_serial_number
2036                , to_serial_number
2037                , serial_prefix
2038                , ERROR_CODE
2039                , group_header_id
2040                , parent_serial_number
2041                , end_item_unit_number
2042                , serial_attribute_category
2043                , origination_date
2044                , c_attribute1
2045                , c_attribute2
2046                , c_attribute3
2047                , c_attribute4
2048                , c_attribute5
2049                , c_attribute6
2050                , c_attribute7
2051                , c_attribute8
2052                , c_attribute9
2053                , c_attribute10
2054                , c_attribute11
2055                , c_attribute12
2056                , c_attribute13
2057                , c_attribute14
2058                , c_attribute15
2059                , c_attribute16
2060                , c_attribute17
2061                , c_attribute18
2062                , c_attribute19
2063                , c_attribute20
2064                , d_attribute1
2065                , d_attribute2
2066                , d_attribute3
2067                , d_attribute4
2068                , d_attribute5
2069                , d_attribute6
2070                , d_attribute7
2071                , d_attribute8
2072                , d_attribute9
2073                , d_attribute10
2074                , n_attribute1
2075                , n_attribute2
2076                , n_attribute3
2077                , n_attribute4
2078                , n_attribute5
2079                , n_attribute6
2080                , n_attribute7
2081                , n_attribute8
2082                , n_attribute9
2083                , n_attribute10
2084                , status_id
2085                , territory_code
2086                , time_since_new
2087                , cycles_since_new
2088                , time_since_overhaul
2089                , cycles_since_overhaul
2090                , time_since_repair
2091                , cycles_since_repair
2092                , time_since_visit
2093                , cycles_since_visit
2094                , time_since_mark
2095                , cycles_since_mark
2096                , number_of_repairs
2097                 )
2098          VALUES (
2099                  p_new_sertrxid
2100                , SYSDATE
2101                , l_sertmp_rec.last_updated_by
2102                , SYSDATE
2103                , l_sertmp_rec.created_by
2104                , l_sertmp_rec.last_update_login
2105                , l_sertmp_rec.request_id
2106                , l_sertmp_rec.program_application_id
2107                , l_sertmp_rec.program_id
2108                , l_sertmp_rec.program_update_date
2109                , l_sertmp_rec.vendor_serial_number
2110                , l_sertmp_rec.vendor_lot_number
2111                , p_new_fm_serial
2112                , p_new_to_serial
2113                , l_sertmp_rec.serial_prefix
2114                , l_sertmp_rec.ERROR_CODE
2115                , l_sertmp_rec.group_header_id
2116                , l_sertmp_rec.parent_serial_number
2117                , l_sertmp_rec.end_item_unit_number
2118                , l_sertmp_rec.serial_attribute_category
2119                , l_sertmp_rec.origination_date
2120                , l_sertmp_rec.c_attribute1
2121                , l_sertmp_rec.c_attribute2
2122                , l_sertmp_rec.c_attribute3
2123                , l_sertmp_rec.c_attribute4
2124                , l_sertmp_rec.c_attribute5
2125                , l_sertmp_rec.c_attribute6
2126                , l_sertmp_rec.c_attribute7
2127                , l_sertmp_rec.c_attribute8
2128                , l_sertmp_rec.c_attribute9
2129                , l_sertmp_rec.c_attribute10
2130                , l_sertmp_rec.c_attribute11
2131                , l_sertmp_rec.c_attribute12
2132                , l_sertmp_rec.c_attribute13
2133                , l_sertmp_rec.c_attribute14
2134                , l_sertmp_rec.c_attribute15
2135                , l_sertmp_rec.c_attribute16
2136                , l_sertmp_rec.c_attribute17
2137                , l_sertmp_rec.c_attribute18
2138                , l_sertmp_rec.c_attribute19
2139                , l_sertmp_rec.c_attribute20
2140                , l_sertmp_rec.d_attribute1
2141                , l_sertmp_rec.d_attribute2
2142                , l_sertmp_rec.d_attribute3
2143                , l_sertmp_rec.d_attribute4
2144                , l_sertmp_rec.d_attribute5
2145                , l_sertmp_rec.d_attribute6
2146                , l_sertmp_rec.d_attribute7
2147                , l_sertmp_rec.d_attribute8
2148                , l_sertmp_rec.d_attribute9
2149                , l_sertmp_rec.d_attribute10
2150                , l_sertmp_rec.n_attribute1
2151                , l_sertmp_rec.n_attribute2
2152                , l_sertmp_rec.n_attribute3
2153                , l_sertmp_rec.n_attribute4
2154                , l_sertmp_rec.n_attribute5
2155                , l_sertmp_rec.n_attribute6
2156                , l_sertmp_rec.n_attribute7
2157                , l_sertmp_rec.n_attribute8
2158                , l_sertmp_rec.n_attribute9
2159                , l_sertmp_rec.n_attribute10
2160                , l_sertmp_rec.status_id
2161                , l_sertmp_rec.territory_code
2162                , l_sertmp_rec.time_since_new
2163                , l_sertmp_rec.cycles_since_new
2164                , l_sertmp_rec.time_since_overhaul
2165                , l_sertmp_rec.cycles_since_overhaul
2166                , l_sertmp_rec.time_since_repair
2167                , l_sertmp_rec.cycles_since_repair
2168                , l_sertmp_rec.time_since_visit
2169                , l_sertmp_rec.cycles_since_visit
2170                , l_sertmp_rec.time_since_mark
2171                , l_sertmp_rec.cycles_since_mark
2172                , l_sertmp_rec.number_of_repairs
2173                 );
2174   EXCEPTION
2175     WHEN OTHERS THEN
2176       IF (l_debug = 1) THEN
2177         inv_log_util.TRACE(l_api_name || ' Error', l_api_name, 1);
2178 
2179         IF (SQLCODE IS NOT NULL) THEN
2180           inv_log_util.TRACE('SQL error: ' || SQLERRM(SQLCODE), l_api_name, 1);
2181         END IF;
2182       END IF;
2183 
2184       RAISE fnd_api.g_exc_unexpected_error;
2185   END copy_msnt;
2186 
2187   /********************************************************************
2188    *  Split Delivery Details. If serial controlled, then
2189    *  query corresponding lot or serial records
2190    *******************************************************************/
2191   PROCEDURE split_delivery_details(
2192     p_organization_id        NUMBER
2193   , p_lpn_id                 NUMBER
2194   , p_xfr_lpn_id             NUMBER
2195   , p_item_rec               inv_validate.item
2196   , p_revision               VARCHAR2
2197   , p_lot_number             VARCHAR2
2198   , p_quantity               NUMBER
2199   , p_uom_code               VARCHAR2
2200   , p_secondary_trx_quantity NUMBER := NULL
2201   , p_secondary_uom_code     VARCHAR2 := NULL
2202   , p_serial_trx_temp_id     NUMBER
2203   , p_subinventory_code      VARCHAR2
2204   , p_locator_id             NUMBER
2205   , p_xfr_subinventory       VARCHAR2 := NULL
2206   , p_xfr_to_location        NUMBER := NULL
2207   , p_transaction_source_id  NUMBER := NULL
2208   , p_trx_source_line_id     NUMBER := NULL
2209   ) IS
2210     l_api_name    CONSTANT VARCHAR2(30)  := 'Split_Delivery_Details';
2211     l_debug                NUMBER        := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
2212     l_progress             VARCHAR2(500) := '0';
2213 
2214     -- Vaiables for call to Update_Shipping_Attributes
2215     l_shipping_attr        wsh_interface.changedattributetabtype;
2216     l_invpcinrectype       wsh_integration.invpcinrectype;
2217 
2218     -- Types needed for WSH_WMS_LPN_GRP.Delivery_Detail_Action
2219     l_wsh_lpn_id_tbl     wsh_util_core.id_tab_type;
2220     l_wsh_del_det_id_tbl wsh_util_core.id_tab_type;
2221     l_wsh_action_prms    WSH_GLBL_VAR_STRCT_GRP.dd_action_parameters_rec_type;
2222     l_wsh_defaults       WSH_GLBL_VAR_STRCT_GRP.dd_default_parameters_rec_type;
2223     l_wsh_action_out_rec WSH_GLBL_VAR_STRCT_GRP.dd_action_out_rec_type;
2224 
2225     -- New shipping API table types for Create_Update_Delivery_Detail
2226     l_del_det_attr         wsh_interface_ext_grp.delivery_details_attr_tbl_type;
2227     l_del_det_in_rec       wsh_interface_ext_grp.detailinrectype;
2228     l_del_det_out_rec      wsh_interface_ext_grp.detailoutrectype;
2229     l_dd_ct                NUMBER                                               := 1;
2230     l_msg_details          VARCHAR2(3000);
2231 
2232     -- New shipping API table types for New Delivery_Detail_Action
2233     l_detail_id_tab        wsh_util_core.id_tab_type;
2234     l_action_prms          wsh_interface_ext_grp.det_action_parameters_rec_type;
2235     l_action_out_rec       wsh_interface_ext_grp.det_action_out_rec_type;
2236 
2237     CURSOR delivery_detail_cursor(lot VARCHAR2) IS
2238          /*Bug 7037834: Replaced the NVL statements with their equivalents in the where clause of below query for performance*/
2239      /* SELECT wdd2.delivery_detail_id
2240            , wdd2.src_requested_quantity_uom
2241            , NVL(wdd2.picked_quantity, wdd2.requested_quantity) requested_quantity
2242            , wdd2.requested_quantity_uom
2243            , wdd1.delivery_detail_id lpn_detail_id
2244            , wdd2.picked_quantity2
2245            , wdd2.requested_quantity_uom2
2246            , wdd2.transaction_temp_id
2247            , wdd2.serial_number
2248            , wdd2.source_header_id
2249            , wdd2.source_line_id
2250         FROM wsh_delivery_details wdd1, wsh_delivery_details wdd2, wsh_delivery_assignments_v wda
2251        WHERE wdd1.organization_id = p_organization_id
2252          AND wdd1.lpn_id = p_lpn_id
2253 	 AND wdd1.released_status = 'X'  -- For LPN reuse ER : 6845650
2254          AND wda.parent_delivery_detail_id = wdd1.delivery_detail_id
2255          AND wdd2.delivery_detail_id = wda.delivery_detail_id
2256          AND wdd2.inventory_item_id = p_item_rec.inventory_item_id
2257          AND NVL(wdd2.revision, '@') = NVL(p_revision, '@')
2258          AND NVL(wdd2.lot_number, -999) = NVL(lot, -999)
2259          AND wdd2.source_header_id = NVL(p_transaction_source_id, wdd2.source_header_id)
2260          AND wdd2.source_line_id = NVL(p_trx_source_line_id, wdd2.source_line_id); */
2261 
2262 	  SELECT wdd2.delivery_detail_id
2263            , wdd2.src_requested_quantity_uom
2264            , NVL(wdd2.picked_quantity, wdd2.requested_quantity) requested_quantity
2265            , wdd2.requested_quantity_uom
2266            , wdd1.delivery_detail_id lpn_detail_id
2267            , wdd2.picked_quantity2
2268            , wdd2.requested_quantity_uom2
2269            , wdd2.transaction_temp_id
2270            , wdd2.serial_number
2271            , wdd2.source_header_id
2272            , wdd2.source_line_id
2273         FROM wsh_delivery_details wdd1, wsh_delivery_details wdd2, wsh_delivery_assignments wda
2274        WHERE wdd1.organization_id = p_organization_id
2275          AND wdd1.lpn_id = p_lpn_id
2276          AND wda.parent_delivery_detail_id = wdd1.delivery_detail_id
2277          AND wdd2.delivery_detail_id = wda.delivery_detail_id
2278          AND wdd2.inventory_item_id = p_item_rec.inventory_item_id
2279 	 AND ((WDD2.REVISION IS NULL AND p_revision IS NULL ) OR WDD2.REVISION = p_revision )
2280 
2281          --AND NVL(wdd2.revision, '@') = NVL(p_revision, '@')
2282 	   AND ((WDD2.LOT_NUMBER IS NULL AND lot IS null) OR  WDD2.LOT_NUMBER =lot )
2283 
2284          --AND NVL(wdd2.lot_number, -999) = NVL(lot, -999)
2285 	   AND ((p_transaction_source_id IS NULL AND WDD2.SOURCE_HEADER_ID=wdd2.source_header_id) OR  WDD2.SOURCE_HEADER_ID=p_transaction_source_id)
2286 
2287          --AND wdd2.source_header_id = NVL(p_transaction_source_id, wdd2.source_header_id)
2288            AND ((p_trx_source_line_id IS NULL AND WDD2.SOURCE_LINE_ID =wdd2.source_line_id)  OR WDD2.SOURCE_LINE_ID=p_trx_source_line_id );
2289 
2290 	 --AND wdd2.source_line_id = NVL(p_trx_source_line_id, wdd2.source_line_id);
2291 
2292  -- Bug 7037834
2293 
2294     l_dd_rec               delivery_detail_cursor%ROWTYPE;
2295 
2296     CURSOR del_serial_cursor(p_wdd_trx_tmp_id NUMBER) IS
2297       SELECT DISTINCT msn.serial_number
2298                  FROM mtl_serial_numbers msn, mtl_serial_numbers_temp wddmsnt, mtl_serial_numbers_temp trxmsnt
2299                 WHERE msn.current_organization_id = p_organization_id
2300                   AND msn.inventory_item_id = p_item_rec.inventory_item_id
2301                   AND wddmsnt.transaction_temp_id = p_wdd_trx_tmp_id
2302                   AND LENGTH(msn.serial_number) = LENGTH(wddmsnt.fm_serial_number)
2303                   AND msn.serial_number BETWEEN wddmsnt.fm_serial_number AND NVL(wddmsnt.to_serial_number, wddmsnt.fm_serial_number)
2304                   AND trxmsnt.transaction_temp_id = p_serial_trx_temp_id
2305                   AND LENGTH(msn.serial_number) = LENGTH(trxmsnt.fm_serial_number)
2306                   AND msn.serial_number BETWEEN trxmsnt.fm_serial_number AND NVL(trxmsnt.to_serial_number, trxmsnt.fm_serial_number)
2307              ORDER BY serial_number;
2308 
2309     TYPE delserialtabtype IS TABLE OF VARCHAR2(30)
2310       INDEX BY BINARY_INTEGER;
2311 
2312     ser_tbl                delserialtabtype;
2313     l_tbl_cnt              NUMBER;
2314 
2315     CURSOR wdd_ser_cur(p_wdd_trx_tmp_id NUMBER, p_start_serial VARCHAR2) IS
2316       SELECT DISTINCT ROWID
2317                     , fm_serial_number
2318                     , to_serial_number
2319                  FROM mtl_serial_numbers_temp msnt
2320                 WHERE transaction_temp_id = p_wdd_trx_tmp_id
2321                   AND fm_serial_number >= p_start_serial
2322                   AND LENGTH(p_start_serial) = LENGTH(fm_serial_number)
2323              ORDER BY fm_serial_number;
2324 
2325     -- Variables for call to Inv_Serial_Info
2326     l_serial_def_flag      BOOLEAN;
2327     l_new_to_serial        VARCHAR2(30);
2328     l_new_fm_serial        VARCHAR2(30);
2329     l_serial_prefix        VARCHAR2(30);
2330     l_fm_serial_suffix     NUMBER;
2331     l_to_serial_suffix     NUMBER;
2332     l_serial_suffix_length NUMBER                                               := 0;
2333     x_error_code           NUMBER;
2334     l_temp_num             NUMBER;
2335     l_current_serial       VARCHAR2(30);
2336     l_split_quantity       NUMBER;
2337     l_split_quantity2      NUMBER                                               := NULL;
2338     l_remaining_quantity   NUMBER                                               := p_quantity;
2339     l_remaining_qty_uom    VARCHAR2(3)                                          := p_uom_code;
2340     l_remaining_quantity2  NUMBER                                               := p_secondary_trx_quantity;
2341     l_total_split_qty2     NUMBER                                               := 0;
2342     qty2_remainder         NUMBER                                               := 0;
2343     l_done_with_range      BOOLEAN                                              := FALSE;
2344     l_loop_counter         NUMBER := 0;
2345   BEGIN
2346     --initalizing l_InvPCInRecType to use for updating wdd with transaction_temp_id
2347     l_invpcinrectype.transaction_id       := NULL;
2348     l_invpcinrectype.transaction_temp_id  := NULL;
2349     l_invpcinrectype.source_code          := 'INV';
2350     l_invpcinrectype.api_version_number   := 1.0;
2351     --initalizing params for Delivery_Detail_Action
2352     l_action_prms.caller                  := 'WMS';
2353     l_action_prms.action_code             := 'SPLIT-LINE';
2354     --initalizing params for Update_Shipping_Attributes
2355     l_shipping_attr(1).transfer_lpn_id    := p_xfr_lpn_id;
2356     l_shipping_attr(1).action_flag        := 'U';
2357 
2358     IF (l_debug = 1) THEN
2359       inv_log_util.TRACE(l_api_name || ' Entered ' || g_pkg_version, l_api_name, 1);
2360       inv_log_util.TRACE('orgid='||p_organization_id||' lpn='||p_lpn_id||' xfrlpn='||p_xfr_lpn_id||' item='||p_item_rec.inventory_item_id||' rev='||p_revision||' lot='||p_lot_number, l_api_name, 9);
2361       inv_log_util.TRACE('sctl='||p_item_rec.serial_number_control_code||' qty='||p_quantity||' uom='||p_uom_code||' qty2='||p_secondary_trx_quantity||' uom2='||p_secondary_uom_code||' strxtmp='||p_serial_trx_temp_id, l_api_name, 4);
2362       inv_log_util.TRACE('xfrsub='||p_xfr_subinventory||' xfrloc='||p_xfr_to_location||' srcid='||p_transaction_source_id||' srcln='||p_trx_source_line_id, l_api_name, 4);
2363     END IF;
2364 
2365     FOR dd_rec IN delivery_detail_cursor(p_lot_number) LOOP
2366       IF (l_debug = 1) THEN
2367         inv_log_util.TRACE('Got WDD: dd_id='||dd_rec.delivery_detail_id||' pckqty='||dd_rec.requested_quantity||' requom='||dd_rec.requested_quantity_uom||' sn='||dd_rec.serial_number||' trxtmpid='|| dd_rec.transaction_temp_id, l_api_name, 9);
2368         inv_log_util.TRACE('lpnddid='||dd_rec.lpn_detail_id||' pckqty2='||dd_rec.picked_quantity2||' requom2='||dd_rec.requested_quantity_uom2||' shdrid='||dd_rec.source_header_id||' slnid='||dd_rec.source_line_id, l_api_name, 9);
2369         inv_log_util.TRACE('remqty='||l_remaining_quantity||' remuom='||l_remaining_qty_uom, l_api_name, 9);
2370       END IF;
2371 
2372       -- Check to see if there needs to be any serial handling
2373       IF (dd_rec.serial_number IS NOT NULL) THEN
2374         -- Only one serial in this line check to see if it is part of split.
2375         BEGIN
2376           SELECT 1
2377             INTO l_split_quantity
2378             FROM mtl_serial_numbers_temp
2379            WHERE transaction_temp_id = p_serial_trx_temp_id
2380              AND dd_rec.serial_number BETWEEN fm_serial_number AND NVL(to_serial_number, fm_serial_number)
2381              AND LENGTH(fm_serial_number) = LENGTH(dd_rec.serial_number)
2382              AND ROWNUM < 2;
2383         EXCEPTION
2384           WHEN NO_DATA_FOUND THEN
2385             l_split_quantity  := 0;
2386         END;
2387       ELSIF(dd_rec.transaction_temp_id IS NOT NULL) THEN
2388         -- WDD lines have serials need to find the number of serial that need to be split
2389         OPEN del_serial_cursor(dd_rec.transaction_temp_id);
2390 
2391         FETCH del_serial_cursor
2392         BULK COLLECT INTO ser_tbl;
2393 
2394         l_split_quantity  := ser_tbl.COUNT;
2395 
2396         IF (l_debug = 1) THEN
2397           inv_log_util.TRACE('ser_tbl.COUNT=' || l_split_quantity, l_api_name, 9);
2398         END IF;
2399 
2400         -- Is some split serials were found for this WDD. Otherwise bypass
2401         -- If not all serials in this detail are part of split. no need to split ranges
2402         IF (l_split_quantity > 0
2403             AND l_split_quantity < dd_rec.requested_quantity) THEN
2404           -- If only a single serial is being split from this delivery line, shipping expects
2405           -- a serial not a transaction_temp_id.  delete MSNT line and populate wdd.serial_number
2406           IF (l_split_quantity = 1) THEN
2407             IF (l_debug = 1) THEN
2408               inv_log_util.TRACE('Split qty is 1 no need for msnt record, instead stamp sn ' || ser_tbl(1) || ' on wdd', l_api_name, 9);
2409             END IF;
2410 
2411             l_invpcinrectype.transaction_temp_id  := NULL;
2412             l_shipping_attr(1).serial_number      := ser_tbl(1);
2413           ELSE
2414             -- More than on serial needs to be split from this MSNT record
2415             SELECT mtl_material_transactions_s.NEXTVAL
2416               INTO l_invpcinrectype.transaction_temp_id
2417               FROM DUAL;
2418 
2419             IF (l_debug = 1) THEN
2420               inv_log_util.TRACE('Created new trxtmpid=' || l_invpcinrectype.transaction_temp_id || ' ser_tbl.count=' || l_split_quantity
2421               , l_api_name, 9);
2422             END IF;
2423 
2424             l_shipping_attr(1).serial_number  := NULL;
2425           END IF;
2426 
2427           l_new_fm_serial  := ser_tbl(1);
2428           l_new_to_serial  := ser_tbl(1);
2429           l_tbl_cnt        := 1;
2430 
2431           FOR wdd_ser_rec IN wdd_ser_cur(dd_rec.transaction_temp_id, l_new_fm_serial) LOOP
2432             -- If a serial exists in this range of serials from WDD
2433             -- Go through each SN until it is beyond the rec range or not contiguous
2434             IF (l_new_fm_serial <= wdd_ser_rec.to_serial_number) THEN
2435               LOOP
2436                 l_tbl_cnt  := l_tbl_cnt + 1;
2437 
2438                 IF (l_tbl_cnt <= ser_tbl.COUNT) THEN
2439                   l_current_serial  := ser_tbl(l_tbl_cnt);
2440                 END IF;
2441 
2442                 IF (l_debug = 1) THEN
2443                   inv_log_util.TRACE('Got new sn='||l_current_serial||' nfmsn='||l_new_fm_serial||' ntosn='||l_new_to_serial||' wfmsn='||wdd_ser_rec.fm_serial_number||' wtosn='||wdd_ser_rec.to_serial_number, l_api_name, 9);
2444                 END IF;
2445 
2446                 -- If the the to and from serials consume the whole range from MSNT
2447                 -- or the current serial is outside the range, We are done with the MSNT
2448                 -- line and can bypass parsing the serials
2449                 IF (
2450                     l_current_serial > wdd_ser_rec.to_serial_number
2451                     OR(l_new_fm_serial = wdd_ser_rec.fm_serial_number
2452                        AND l_new_to_serial = wdd_ser_rec.to_serial_number)
2453                    ) THEN
2454                   -- Current serial is not in the this MSNT line
2455                   l_done_with_range  := TRUE;
2456                 ELSE   -- Serial still within serial range. See if it is contiguous
2457                   IF (l_serial_prefix IS NULL) THEN
2458                     l_serial_def_flag       :=
2459                       mtl_serial_check.inv_serial_info(l_new_fm_serial, l_new_fm_serial, l_serial_prefix, l_temp_num, l_fm_serial_suffix
2460                       , l_to_serial_suffix, x_error_code);
2461 
2462                     IF (x_error_code <> 0) THEN
2463                       IF (l_debug = 1) THEN
2464                         inv_log_util.TRACE('Failed MTL_SERIAL_CHECK.Inv_Serial_Info', l_api_name, 1);
2465                       END IF;
2466 
2467                       fnd_message.set_name('WMS', 'WMS_CONT_INVALID_SER');
2468                       fnd_msg_pub.ADD;
2469                       RAISE fnd_api.g_exc_error;
2470                     END IF;
2471 
2472                     -- calculate the length of the serial number suffix
2473                     --l_serial_suffix_length := LENGTH(l_new_fm_serial) - LENGTH(l_serial_prefix);
2474                     l_serial_suffix_length  := LENGTH(l_new_fm_serial) - NVL(LENGTH(l_serial_prefix), 0);
2475                     /* 3406810 */
2476                     l_to_serial_suffix      := l_to_serial_suffix + 1;
2477 
2478                     IF (l_debug = 1) THEN
2479                       inv_log_util.TRACE('New prefix='||l_serial_prefix||' suffix='||l_fm_serial_suffix||' sfxlgth='||l_serial_suffix_length, l_api_name, 1);
2480                     END IF;
2481                   END IF;
2482 
2483                   -- Check if Serial is contiguous. If so, make current serial the new to_serial
2484                   IF (l_current_serial = l_serial_prefix || LPAD(TO_CHAR(l_to_serial_suffix), l_serial_suffix_length, '0')) THEN
2485                     l_to_serial_suffix  := l_to_serial_suffix + 1;
2486                     l_new_to_serial     := l_current_serial;
2487                   ELSE   -- Serial is not contiguous.  Range is done.
2488                     l_done_with_range  := TRUE;
2489                   END IF;
2490                 END IF;
2491 
2492                 IF (l_done_with_range) THEN
2493                   -- If we are done with the range separate it from the rest
2494                   IF (l_new_fm_serial = wdd_ser_rec.fm_serial_number
2495                       AND l_new_to_serial = wdd_ser_rec.to_serial_number) THEN
2496                     -- Whole serial range in wdd is part of split.
2497                     IF (l_invpcinrectype.transaction_temp_id IS NOT NULL) THEN
2498                       -- More than one serial to be put new temp id on it
2499                       UPDATE mtl_serial_numbers_temp
2500                          SET transaction_temp_id = l_invpcinrectype.transaction_temp_id
2501                        WHERE ROWID = wdd_ser_rec.ROWID;
2502                     ELSE
2503                       -- Single serial being split, remove record from MSNT
2504                       DELETE FROM mtl_serial_numbers_temp
2505                             WHERE ROWID = wdd_ser_rec.ROWID;
2506                     END IF;
2507                   ELSE   -- Create records for the new serial sub ranges
2508                     -- If not a single serial being split, create an new MSNT line for new split WDD line
2509                     IF (l_invpcinrectype.transaction_temp_id IS NOT NULL) THEN
2510                       copy_msnt(
2511                         p_source_row_id              => wdd_ser_rec.ROWID
2512                       , p_new_sertrxid               => l_invpcinrectype.transaction_temp_id
2513                       , p_new_fm_serial              => l_new_fm_serial
2514                       , p_new_to_serial              => l_new_to_serial
2515                       );
2516                     END IF;
2517 
2518                     --Update the delivery's msnt line with the non split ranges
2519                     IF (l_new_fm_serial > wdd_ser_rec.fm_serial_number) THEN
2520                       --If split serial range came from the middle of the delivery range,
2521                       --a new msnt line needs to be created to represent the second half
2522                       -- of the unsplit wdd serials.
2523                       IF (l_new_to_serial < wdd_ser_rec.to_serial_number) THEN
2524                         copy_msnt(
2525                           p_source_row_id              => wdd_ser_rec.ROWID
2526                         , p_new_sertrxid               => dd_rec.transaction_temp_id
2527                         , p_new_fm_serial              => l_serial_prefix || LPAD(l_to_serial_suffix, l_serial_suffix_length, '0')
2528                         , p_new_to_serial              => wdd_ser_rec.to_serial_number
2529                         );
2530                       END IF;
2531 
2532                       --Update the to serial one less than the beginning of the split serial range
2533                       wdd_ser_rec.to_serial_number  := l_serial_prefix || LPAD(l_fm_serial_suffix - 1, l_serial_suffix_length, '0');
2534 
2535                       UPDATE mtl_serial_numbers_temp
2536                          SET to_serial_number = wdd_ser_rec.to_serial_number
2537                        WHERE ROWID = wdd_ser_rec.ROWID;
2538                     ELSIF(l_new_to_serial < wdd_ser_rec.to_serial_number) THEN
2539                       --Update the from serial one greater than the end of the split serial range
2540                       wdd_ser_rec.fm_serial_number  := l_serial_prefix || LPAD(l_to_serial_suffix, l_serial_suffix_length, '0');
2541 
2542                       UPDATE mtl_serial_numbers_temp
2543                          SET fm_serial_number = wdd_ser_rec.fm_serial_number
2544                        WHERE ROWID = wdd_ser_rec.ROWID;
2545                     END IF;
2546                   END IF;
2547 
2548                   -- Finished processing range, reset params
2549                   l_new_fm_serial    := l_current_serial;
2550                   l_new_to_serial    := l_current_serial;
2551                   l_serial_prefix    := NULL;
2552                   l_done_with_range  := FALSE;
2553                 END IF;
2554 
2555                 EXIT WHEN l_tbl_cnt > ser_tbl.COUNT
2556                       OR l_current_serial > wdd_ser_rec.to_serial_number;
2557               END LOOP;
2558             END IF;   -- Done finding all the serials for this WDD-MSNT line
2559 
2560             EXIT WHEN l_tbl_cnt > ser_tbl.COUNT;
2561           END LOOP;
2562 
2563           --Call shipping api to set transaction_temp_id global variable
2564           IF (l_debug = 1) THEN
2565             inv_log_util.TRACE('Calling Set_Inv_PC_Attributes transaction_temp_id=' || l_invpcinrectype.transaction_temp_id, l_api_name);
2566           END IF;
2567 
2568           wsh_integration.set_inv_pc_attributes(p_in_attributes => l_invpcinrectype, x_return_status => ret_status
2569           , x_msg_count                  => ret_msgcnt, x_msg_data => ret_msgdata);
2570 
2571           IF (ret_status <> fnd_api.g_ret_sts_success) THEN
2572             IF (l_debug = 1) THEN
2573               inv_log_util.TRACE('return error from Set_Inv_PC_Attributes', l_api_name);
2574             END IF;
2575 
2576             RAISE fnd_api.g_exc_error;
2577           END IF;
2578         END IF;
2579 
2580         -- End of serial number handling code
2581         CLOSE del_serial_cursor;
2582 
2583         -- Convert split quantity from primary UOM into the UOM of the WDD line if different
2584         IF (p_uom_code <> dd_rec.requested_quantity_uom) THEN
2585           l_split_quantity  :=
2586             inv_convert.inv_um_convert(p_item_rec.inventory_item_id, 6, l_split_quantity, p_uom_code, dd_rec.requested_quantity_uom, NULL
2587             , NULL);
2588 
2589           IF (l_remaining_quantity < 0) THEN
2590             fnd_message.set_name('INV', 'INV_UOM_CONVERSION_ERROR');
2591             fnd_message.set_token('uom1', p_uom_code);
2592             fnd_message.set_token('uom2', dd_rec.requested_quantity_uom);
2593             fnd_message.set_token('module', l_api_name);
2594             fnd_msg_pub.ADD;
2595             RAISE fnd_api.g_exc_error;
2596           END IF;
2597         END IF;
2598       ELSE   -- Not serial controlled
2599         l_split_quantity  := dd_rec.requested_quantity;
2600       END IF;
2601 
2602       IF (l_split_quantity > 0) THEN
2603         IF (l_remaining_qty_uom <> dd_rec.requested_quantity_uom) THEN
2604           -- convert remaining quantity into the UOM of the WDD line
2605           l_remaining_quantity  :=
2606             inv_convert.inv_um_convert(p_item_rec.inventory_item_id, 6, l_remaining_quantity, l_remaining_qty_uom
2607             , dd_rec.requested_quantity_uom, NULL, NULL);
2608 
2609           IF (l_remaining_quantity < 0) THEN
2610             fnd_message.set_name('INV', 'INV_UOM_CONVERSION_ERROR');
2611             fnd_message.set_token('uom1', l_remaining_qty_uom);
2612             fnd_message.set_token('uom2', dd_rec.requested_quantity_uom);
2613             fnd_message.set_token('module', l_api_name);
2614             fnd_msg_pub.ADD;
2615             RAISE fnd_api.g_exc_error;
2616           END IF;
2617 
2618           l_remaining_qty_uom   := dd_rec.requested_quantity_uom;
2619         END IF;
2620 
2621         IF (l_remaining_quantity < dd_rec.requested_quantity) THEN
2622           l_split_quantity  := ROUND(l_remaining_quantity, g_precision);
2623         ELSE   -- need to round split qty
2624           l_split_quantity  := ROUND(l_split_quantity, g_precision);
2625         END IF;
2626 
2627         IF (l_remaining_quantity2 > 0) THEN
2628           IF (dd_rec.picked_quantity2 IS NULL) THEN
2629             -- If the from lpn has items without catch weights defined, must null
2630             -- secondary quantity and uom for all WDD records for this lpn
2631             l_remaining_quantity2  := 0;
2632           ELSIF(dd_rec.requested_quantity_uom2 <> p_secondary_uom_code) THEN
2633             -- Sanity check to make sure that we are transacting in the same UOM
2634             fnd_message.set_name('WMS', 'WMS_SEC_UOM_MISMATCH_ERROR');
2635             fnd_message.set_token('UOM1', p_secondary_uom_code);
2636             fnd_message.set_token('UOM2', dd_rec.requested_quantity_uom2);
2637             fnd_msg_pub.ADD;
2638             RAISE fnd_api.g_exc_unexpected_error;
2639           ELSE
2640             -- Everything checks out determine the amount of qty2 to split
2641             -- Calculate the theoretical proportionate quantity2 to be split
2642             --l_split_quantity2 := p_secondary_trx_quantity*dd_rec.requested_quantity/p_quantity;
2643             l_split_quantity2      := l_remaining_quantity2 * l_split_quantity / l_remaining_quantity;
2644             l_remaining_quantity2  := l_remaining_quantity2 - l_split_quantity2;
2645             -- Keep track of the total quantity2 removed from soruce LPN
2646             l_total_split_qty2     := l_total_split_qty2 + LEAST(dd_rec.picked_quantity2, l_split_quantity2);
2647 
2648             IF (l_split_quantity = dd_rec.requested_quantity) THEN
2649               -- Whole WDD line will be split. Add record to table type to update catch weights
2650               l_del_det_attr(l_dd_ct).delivery_detail_id       := dd_rec.delivery_detail_id;
2651               l_del_det_attr(l_dd_ct).picked_quantity2         := l_split_quantity2;
2652               l_del_det_attr(l_dd_ct).requested_quantity_uom2  := p_secondary_uom_code;
2653               l_dd_ct                                          := l_dd_ct + 1;
2654             END IF;
2655 
2656             IF (l_debug = 1) THEN
2657               inv_log_util.TRACE(
2658                 'new split_qty2=' || l_split_quantity2 || ' rem_qty2=' || l_remaining_quantity2 || ' splitfmqty=' || l_total_split_qty2
2659               , l_api_name
2660               , 9
2661               );
2662             END IF;
2663           END IF;
2664         END IF;
2665 
2666         IF (l_split_quantity = ROUND(dd_rec.requested_quantity, g_precision)) THEN
2667           -- Disassociate existing delivery line with old lpn
2668           l_shipping_attr(1).delivery_detail_id  := dd_rec.delivery_detail_id;
2669         ELSIF(l_split_quantity < dd_rec.requested_quantity) THEN
2670           -- We need only part of this WDD line need to split it
2671           l_detail_id_tab(1)                     := dd_rec.delivery_detail_id;
2672           l_action_prms.split_quantity           := l_split_quantity;
2673           l_action_prms.split_quantity2          := LEAST(dd_rec.picked_quantity2, l_split_quantity2);
2674 
2675           -- Call new Shipping API to split line
2676           IF (l_debug = 1) THEN
2677             inv_log_util.TRACE('Calling Delivery_Detail_Action ddid='||l_detail_id_tab(1)||' qty='||l_action_prms.split_quantity||' qty2='||l_action_prms.split_quantity2, l_api_name, 1);
2678           END IF;
2679 
2680           wsh_interface_ext_grp.delivery_detail_action(
2681             p_api_version_number         => 1.0
2682           , p_init_msg_list              => fnd_api.g_false
2683           , p_commit                     => fnd_api.g_false
2684           , x_return_status              => ret_status
2685           , x_msg_count                  => ret_msgcnt
2686           , x_msg_data                   => ret_msgdata
2687           , p_detail_id_tab              => l_detail_id_tab
2688           , p_action_prms                => l_action_prms
2689           , x_action_out_rec             => l_action_out_rec
2690           );
2691 
2692           IF (ret_status <> fnd_api.g_ret_sts_success) THEN
2693             IF (l_debug = 1) THEN
2694               inv_log_util.TRACE('**Error Delivery_Detail_Action: ' || ret_msgdata, l_api_name, 1);
2695             END IF;
2696 
2697             fnd_message.set_name('INV', 'INV_SPLIT_LINE_FAILURE');
2698             fnd_msg_pub.ADD;
2699             RAISE fnd_api.g_exc_error;
2700           END IF;
2701 
2702           l_shipping_attr(1).delivery_detail_id := l_action_out_rec.result_id_tab(1);
2703 
2704           IF (l_debug = 1) THEN
2705             inv_log_util.TRACE('Created new delivery line: dd_id=' || l_shipping_attr(1).delivery_detail_id, l_api_name, 9);
2706           END IF;
2707         ELSE   -- Split qty is > dd_rec.delivery_detail_id logic error;
2708           IF (l_debug = 1) THEN
2709             inv_log_util.TRACE('Split qty ' || l_split_quantity || ' is greater than what is available on WDD '
2710               || dd_rec.delivery_detail_id, l_api_name, 9);
2711           END IF;
2712 
2713           RAISE fnd_api.g_exc_unexpected_error;
2714         END IF;
2715 
2716         IF (l_debug = 1) THEN
2717           inv_log_util.TRACE('Call to WSH Delivery_Detail_Action unassign delivery from lpn', l_api_name, 4);
2718         END IF;
2719 
2720         l_wsh_action_prms.caller      := 'WMS';
2721         l_wsh_action_prms.action_code := 'UNPACK';
2722         l_wsh_del_det_id_tbl(1)       := l_shipping_attr(1).delivery_detail_id;
2723 
2724         WSH_WMS_LPN_GRP.Delivery_Detail_Action (
2725           p_api_version_number => 1.0
2726         , p_init_msg_list      => fnd_api.g_false
2727         , p_commit             => fnd_api.g_false
2728         , x_return_status      => ret_status
2729         , x_msg_count          => ret_msgcnt
2730         , x_msg_data           => ret_msgdata
2731         , p_lpn_id_tbl         => l_wsh_lpn_id_tbl
2732         , p_del_det_id_tbl     => l_wsh_del_det_id_tbl
2733         , p_action_prms        => l_wsh_action_prms
2734         , x_defaults           => l_wsh_defaults
2735         , x_action_out_rec     => l_wsh_action_out_rec );
2736 
2737         IF (ret_status <> fnd_api.g_ret_sts_success) THEN
2738           IF (l_debug = 1) THEN
2739             inv_log_util.TRACE('Error WSH_WMS_LPN_GRP.Delivery_Detail_Action' || ret_status, l_api_name, 1);
2740           END IF;
2741 
2742           fnd_message.set_name('INV', 'INV_UNASSIGN_DEL_FAILURE');
2743           fnd_msg_pub.ADD;
2744           RAISE fnd_api.g_exc_error;
2745         ELSIF (l_debug = 1) THEN
2746           inv_log_util.TRACE('Done with call to WSH Create_Update_Containers', l_api_name, 4);
2747         END IF;
2748 
2749         --Associate New Delivery Line with transfer LPN
2750         IF (p_xfr_subinventory IS NOT NULL) THEN
2751           IF (l_debug = 1) THEN
2752             inv_log_util.TRACE('Replacing sub: ' || l_shipping_attr(1).subinventory || ' with xfr sub: ' || p_xfr_subinventory, l_api_name
2753             , 9);
2754           END IF;
2755 
2756           l_shipping_attr(1).subinventory  := p_xfr_subinventory;
2757         END IF;
2758 
2759         IF (p_xfr_to_location <> 0) THEN
2760           IF (l_debug = 1) THEN
2761             inv_log_util.TRACE('Replacing loc: ' || l_shipping_attr(1).locator_id || ' with xfr loc: ' || p_xfr_to_location, l_api_name, 9);
2762           END IF;
2763 
2764           l_shipping_attr(1).locator_id  := p_xfr_to_location;
2765         END IF;
2766 
2767         -- Bug 3386829: Need to repopualte the picked quantity for shipping api
2768         l_shipping_attr(1).picked_quantity   := l_split_quantity;
2769         l_shipping_attr(1).picked_quantity2  := l_split_quantity2;
2770         wsh_interface.update_shipping_attributes(p_source_code => 'INV', p_changed_attributes => l_shipping_attr
2771         , x_return_status              => ret_status);
2772 
2773         IF (ret_status <> fnd_api.g_ret_sts_success) THEN
2774           IF (l_debug = 1) THEN
2775             inv_log_util.TRACE('***Error in update shipping attribures for split trx', l_api_name, 9);
2776           END IF;
2777 
2778           fnd_message.set_name('WMS', 'WMS_TD_UPD_SHP_ERROR');
2779           fnd_msg_pub.ADD;
2780           RAISE fnd_api.g_exc_error;
2781         END IF;
2782 
2783         -- Need to also to transfer the item reservation
2784         IF (l_debug = 1) THEN
2785           inv_log_util.TRACE('Calling Upd_Reservation_PUP_New qty='||l_split_quantity||' uom='||dd_rec.requested_quantity_uom, l_api_name, 9);
2786         END IF;
2787         l_loop_counter := l_loop_counter + 1;
2788 
2789         INV_RESERVATION_PVT.Upd_Reservation_PUP_New (
2790           x_return_status           => ret_status
2791         , x_msg_count               => ret_msgcnt
2792         , x_msg_data                => ret_msgdata
2793         , p_organization_id         => p_organization_id
2794         , p_demand_source_header_id => dd_rec.source_header_id
2795         , p_demand_source_line_id   => dd_rec.source_line_id
2796         , p_from_subinventory_code  => p_subinventory_code
2797         , p_from_locator_id         => p_locator_id
2798         , p_to_subinventory_code    => NVL(p_xfr_subinventory, p_subinventory_code)
2799         , p_to_locator_id           => NVL(p_xfr_to_location, p_locator_id)
2800         , p_inventory_item_id       => p_item_rec.inventory_item_id
2801         , p_revision                => p_revision
2802         , p_lot_number              => p_lot_number
2803         , p_quantity                => l_split_quantity
2804         , p_uom                     => dd_rec.requested_quantity_uom
2805         , p_lpn_id                  => p_xfr_lpn_id
2806         , p_force_reservation_flag  => fnd_api.g_true
2807         , p_requirement_date        => (Sysdate + l_loop_counter/(24*3600))
2808         , p_source_lpn_id           => p_lpn_id );
2809 
2810         IF ( ret_status <> fnd_api.g_ret_sts_success ) THEN
2811           IF (l_debug = 1) THEN
2812             inv_log_util.TRACE('upd_reservation_pup failed '||ret_msgdata, l_api_name, 1);
2813           END IF;
2814           fnd_message.set_name('WMS', 'UPD_RESERVATION_PUP_FAIL');
2815           fnd_msg_pub.ADD;
2816           RAISE fnd_api.g_exc_error;
2817         END IF;
2818         l_progress := 'Done with call to Upd_Reservation_PUP_New';
2819 
2820         -- Subtract the split quantity from the remaining quantity
2821         l_remaining_quantity                 := l_remaining_quantity - l_split_quantity;
2822 
2823         IF (l_debug = 1) THEN
2824           inv_log_util.TRACE(
2825             'Assoc new line remqty=' || l_remaining_quantity || ' remqty2=' || l_remaining_quantity2 || ' tsplt2=' || l_total_split_qty2
2826           , l_api_name
2827           , 9
2828           );
2829         END IF;
2830       END IF;   -- l_split_quantity > 0
2831 
2832       EXIT WHEN ROUND(l_remaining_quantity, g_precision) <= 0;
2833     END LOOP;
2834 
2835     IF (l_remaining_quantity > 0) THEN
2836       inv_log_util.TRACE('***Error while splittng delivery not enough quantity found in wdd', l_api_name, 1);
2837       fnd_message.set_name('WMS', 'INV_INSUFFICIENT_WDD_QTY');
2838       fnd_msg_pub.ADD;
2839       RAISE fnd_api.g_exc_error;
2840     END IF;
2841 
2842     IF (p_secondary_trx_quantity IS NOT NULL) THEN
2843       -- We have consumed all of the primary quantity.  Calculate the amount of qty2
2844       -- That needs to be added back or removed from soruce LPN.
2845       qty2_remainder  := p_secondary_trx_quantity - l_total_split_qty2;
2846 
2847       IF (l_debug = 1) THEN
2848         inv_log_util.TRACE('qty2_remainder=' || qty2_remainder, l_api_name, 9);
2849       END IF;
2850 
2851       IF (qty2_remainder <> 0) THEN
2852         OPEN delivery_detail_cursor(p_lot_number);
2853 
2854         FETCH delivery_detail_cursor
2855          INTO l_dd_rec;
2856 
2857         IF (delivery_detail_cursor%FOUND
2858             AND l_dd_rec.picked_quantity2 IS NOT NULL) THEN
2859           WHILE(delivery_detail_cursor%FOUND) LOOP
2860             IF (l_debug = 1) THEN
2861               inv_log_util.TRACE(
2862                    'Got WDD: dd_id='
2863                 || l_dd_rec.delivery_detail_id
2864                 || ' pkqty='
2865                 || l_dd_rec.requested_quantity
2866                 || ' requom='
2867                 || l_dd_rec.requested_quantity_uom
2868                 || ' sn='
2869                 || l_dd_rec.serial_number
2870                 || ' txtmpid='
2871                 || l_dd_rec.transaction_temp_id
2872               , l_api_name
2873               , 9
2874               );
2875               inv_log_util.TRACE(
2876                    'lpnddid='
2877                 || l_dd_rec.lpn_detail_id
2878                 || ' pckqty2='
2879                 || l_dd_rec.picked_quantity2
2880                 || ' requom2='
2881                 || l_dd_rec.requested_quantity_uom2
2882                 || ' remqty='
2883                 || l_remaining_quantity
2884                 || ' remuom='
2885                 || l_remaining_qty_uom
2886               , l_api_name
2887               , 9
2888               );
2889             END IF;
2890 
2891             IF (l_dd_rec.requested_quantity_uom2 <> p_secondary_uom_code) THEN
2892               -- Sanity check to make sure that we are transacting in the same UOM
2893               fnd_message.set_name('WMS', 'WMS_SEC_UOM_MISMATCH_ERROR');
2894               fnd_message.set_token('UOM1', p_secondary_uom_code);
2895               fnd_message.set_token('UOM2', l_dd_rec.requested_quantity_uom2);
2896               fnd_msg_pub.ADD;
2897               RAISE fnd_api.g_exc_unexpected_error;
2898             ELSIF(l_dd_rec.picked_quantity2 > qty2_remainder) THEN
2899               -- This WDD record can consume the entire remaining qty.  subtract that
2900               -- amount from the line and set remaining to zero
2901               l_del_det_attr(l_dd_ct).picked_quantity2         := l_dd_rec.picked_quantity2 - qty2_remainder;
2902               l_del_det_attr(l_dd_ct).delivery_detail_id       := l_dd_rec.delivery_detail_id;
2903               l_del_det_attr(l_dd_ct).requested_quantity_uom2  := p_secondary_uom_code;
2904               l_dd_ct                                          := l_dd_ct + 1;
2905               qty2_remainder                                   := 0;
2906             ELSIF(l_dd_rec.picked_quantity2 <> 0) THEN
2907               -- This WDD record cannot consume the entire remaining qty. consume all
2908               -- of the qty2 and subtract the amount from the total remaining
2909               l_del_det_attr(l_dd_ct).picked_quantity2         := 0;
2910               l_del_det_attr(l_dd_ct).delivery_detail_id       := l_dd_rec.delivery_detail_id;
2911               l_del_det_attr(l_dd_ct).requested_quantity_uom2  := p_secondary_uom_code;
2912               l_dd_ct                                          := l_dd_ct + 1;
2913               qty2_remainder                                   := qty2_remainder - l_dd_rec.picked_quantity2;
2914             END IF;
2915 
2916             IF (l_debug = 1) THEN
2917               inv_log_util.TRACE('new qty2_remainder=' || qty2_remainder, l_api_name, 9);
2918             END IF;
2919 
2920             FETCH delivery_detail_cursor
2921              INTO l_dd_rec;
2922           END LOOP;
2923 
2924           IF (qty2_remainder <> 0) THEN
2925             inv_log_util.TRACE('***Error while splittng delivery not enough quantity2 found in wdd', l_api_name, 9);
2926             fnd_message.set_name('WMS', 'INV_INSUFFICIENT_WDD_QTY');
2927             fnd_msg_pub.ADD;
2928             RAISE fnd_api.g_exc_error;
2929           END IF;
2930         ELSIF(l_debug = 1) THEN
2931           inv_log_util.TRACE('no WDD rows left in source lpn...ok', l_api_name, 9);
2932         END IF;
2933 
2934         CLOSE delivery_detail_cursor;
2935       END IF;
2936     END IF;
2937 
2938     -- For catch weights update delivery lines with
2939     IF (l_del_det_attr.COUNT > 0) THEN
2940       l_del_det_in_rec.caller       := 'WMS';
2941       l_del_det_in_rec.action_code  := 'UPDATE';
2942 
2943       IF (l_debug = 1) THEN
2944         inv_log_util.TRACE('Calling Create_Update_Delivery_Detail count=' || l_del_det_attr.COUNT, l_api_name, 9);
2945       END IF;
2946 
2947       wsh_interface_ext_grp.create_update_delivery_detail(
2948         p_api_version_number         => 1.0
2949       , p_init_msg_list              => fnd_api.g_false
2950       , p_commit                     => fnd_api.g_false
2951       , x_return_status              => ret_status
2952       , x_msg_count                  => ret_msgcnt
2953       , x_msg_data                   => ret_msgdata
2954       , p_detail_info_tab            => l_del_det_attr
2955       , p_in_rec                     => l_del_det_in_rec
2956       , x_out_rec                    => l_del_det_out_rec
2957       );
2958 
2959       IF (ret_status <> fnd_api.g_ret_sts_success) THEN
2960         --Get error messages from shipping
2961         wsh_util_core.get_messages('Y', ret_msgdata, l_msg_details, ret_msgcnt);
2962 
2963         IF (ret_msgcnt > 1) THEN
2964           ret_msgdata  := ret_msgdata || l_msg_details;
2965         END IF;
2966 
2967         IF (l_debug = 1) THEN
2968           inv_log_util.TRACE('Error calling Create_Update_Delivery_Detail: ' || ret_msgdata, l_api_name, 1);
2969         END IF;
2970 
2971         fnd_message.set_name('WMS', 'WMS_UPD_DELIVERY_ERROR');
2972         fnd_message.set_token('MSG1', ret_msgdata);
2973         fnd_msg_pub.ADD;
2974         RAISE fnd_api.g_exc_unexpected_error;
2975       END IF;
2976     END IF;
2977   END split_delivery_details;
2978 
2979   PROCEDURE split_delivery(
2980     p_tempid                 NUMBER
2981   , p_lpn_id                 NUMBER
2982   , p_xfr_lpn_id             NUMBER
2983   , p_item_rec               inv_validate.item
2984   , p_revision               VARCHAR2
2985   , p_qty                    NUMBER
2986   , p_uom                    VARCHAR2
2987   , p_secondary_trx_quantity NUMBER := NULL
2988   , p_secondary_uom_code     VARCHAR2 := NULL
2989   , p_org_id                 NUMBER
2990   , p_subinventory_code      VARCHAR2
2991   , p_locator_id             NUMBER
2992   , p_xfr_subinventory       VARCHAR2 := NULL
2993   , p_xfr_to_location        NUMBER := NULL
2994   , p_transaction_source_id  NUMBER := NULL
2995   , p_trx_source_line_id     NUMBER := NULL
2996   ) IS
2997     l_api_name      CONSTANT VARCHAR2(30)                         := 'Split_Delivery';
2998     l_api_version   CONSTANT NUMBER                               := 1.0;
2999     l_debug                  NUMBER                               := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3000     l_progress               VARCHAR2(10)                         := '0';
3001     l_lotfound               BOOLEAN                              := FALSE;
3002     l_prev_lot               VARCHAR2(30)                         := -999;
3003     l_secondary_trx_quantity NUMBER                               := p_secondary_trx_quantity;
3004     l_secondary_uom_code     VARCHAR2(3)                          := p_secondary_uom_code;
3005     l_pricing_ind            VARCHAR2(30);
3006     l_valid_sec_qty_split    NUMBER;
3007 
3008     CURSOR lottmp_cur IS
3009       SELECT   lot_number
3010              , primary_quantity
3011              , serial_transaction_temp_id
3012              , secondary_quantity
3013              , secondary_unit_of_measure
3014           FROM mtl_transaction_lots_temp
3015          WHERE transaction_temp_id = p_tempid
3016       ORDER BY lot_number;
3017   BEGIN
3018     -- Check if pricing is tracked by secondary qty
3019     l_pricing_ind  :=
3020       wms_catch_weight_pvt.get_ont_pricing_qty_source(
3021         p_api_version                => 1.0
3022       , x_return_status              => ret_status
3023       , x_msg_count                  => ret_msgcnt
3024       , x_msg_data                   => ret_msgdata
3025       , p_organization_id            => p_org_id
3026       , p_inventory_item_id          => p_item_rec.inventory_item_id
3027       );
3028 
3029     IF (ret_status <> fnd_api.g_ret_sts_success) THEN
3030       fnd_message.set_name('INV', 'WMS_GET_CATCH_WEIGHT_ATT_FAIL');
3031       fnd_msg_pub.ADD;
3032       RAISE fnd_api.g_exc_unexpected_error;
3033     END IF;
3034 
3035     -- retrieve corresponding lot numbers
3036     FOR lottmp_rec IN lottmp_cur LOOP
3037       l_lotfound  := TRUE;
3038 
3039       IF (l_pricing_ind = wms_catch_weight_pvt.g_price_secondary) THEN
3040         IF (l_prev_lot <> lottmp_rec.lot_number) THEN
3041           l_prev_lot  := lottmp_rec.lot_number;
3042           wms_catch_weight_pvt.show_ct_wt_for_split(
3043             x_return_status              => ret_status
3044           , x_msg_data                   => ret_msgcnt
3045           , x_msg_count                  => ret_msgdata
3046           , p_org_id                     => p_org_id
3047           , p_from_lpn_id                => p_lpn_id
3048           , p_from_item_id               => p_item_rec.inventory_item_id
3049           , p_from_item_revision         => p_revision
3050           , p_from_item_lot_number       => lottmp_rec.lot_number
3051           , p_to_lpn_id                  => p_xfr_lpn_id
3052           , x_show_ct_wt                 => l_valid_sec_qty_split
3053           );
3054 
3055           IF (ret_status <> fnd_api.g_ret_sts_success) THEN
3056             fnd_message.set_name('INV', 'WMS_GET_CATCH_WEIGHT_ATT_FAIL');
3057             fnd_msg_pub.ADD;
3058             RAISE fnd_api.g_exc_unexpected_error;
3059           END IF;
3060 
3061           IF (l_debug = 1) THEN
3062             inv_log_util.TRACE('l_valid_sec_qty_split=' || l_valid_sec_qty_split, l_api_name, 9);
3063           END IF;
3064 
3065           IF (l_valid_sec_qty_split <> 1) THEN
3066             --Clear the secondary quantity fields for the from LPN
3067             wms_catch_weight_pvt.update_lpn_secondary_quantity(
3068               p_api_version                => 1.0
3069             , x_return_status              => ret_status
3070             , x_msg_count                  => ret_msgcnt
3071             , x_msg_data                   => ret_msgdata
3072             , p_record_source              => 'WDD'
3073             , p_organization_id            => p_org_id
3074             , p_lpn_id                     => p_lpn_id
3075             , p_inventory_item_id          => p_item_rec.inventory_item_id
3076             , p_revision                   => p_revision
3077             , p_lot_number                 => lottmp_rec.lot_number
3078             , p_quantity                   => NULL
3079             , p_uom_code                   => NULL
3080             , p_secondary_quantity         => NULL
3081             , p_secondary_uom_code         => NULL
3082             );
3083 
3084             IF (ret_status <> fnd_api.g_ret_sts_success) THEN
3085               IF (l_debug = 1) THEN
3086                 inv_log_util.TRACE('Error calling Update_LPN_Secondary_Quantity: ' || ret_msgdata, l_api_name, 1);
3087               END IF;
3088 
3089               RAISE fnd_api.g_exc_unexpected_error;
3090             END IF;
3091 
3092             --Clear the secondary quantity fields for the to LPN
3093             wms_catch_weight_pvt.update_lpn_secondary_quantity(
3094               p_api_version                => 1.0
3095             , x_return_status              => ret_status
3096             , x_msg_count                  => ret_msgcnt
3097             , x_msg_data                   => ret_msgdata
3098             , p_record_source              => 'WDD'
3099             , p_organization_id            => p_org_id
3100             , p_lpn_id                     => p_xfr_lpn_id
3101             , p_inventory_item_id          => p_item_rec.inventory_item_id
3102             , p_revision                   => p_revision
3103             , p_lot_number                 => lottmp_rec.lot_number
3104             , p_quantity                   => NULL
3105             , p_uom_code                   => NULL
3106             , p_secondary_quantity         => NULL
3107             , p_secondary_uom_code         => NULL
3108             );
3109 
3110             IF (ret_status <> fnd_api.g_ret_sts_success) THEN
3111               IF (l_debug = 1) THEN
3112                 inv_log_util.TRACE('Error calling Update_LPN_Secondary_Quantity: ' || ret_msgdata, l_api_name, 1);
3113               END IF;
3114 
3115               RAISE fnd_api.g_exc_unexpected_error;
3116             END IF;
3117           END IF;
3118         END IF;
3119 
3120         -- Secondary quantities are not allowed do not pass it to
3121         -- the Split_Delivery_Details API
3122         IF (l_valid_sec_qty_split <> 1) THEN
3123           lottmp_rec.secondary_quantity         := NULL;
3124           lottmp_rec.secondary_unit_of_measure  := NULL;
3125         END IF;
3126       ELSIF(lottmp_rec.secondary_quantity IS NOT NULL) THEN
3127         -- secondary qty populated even though item is not catch weight
3128         -- enabled.  Do not pass values to split delivery details api
3129         lottmp_rec.secondary_quantity         := NULL;
3130         lottmp_rec.secondary_unit_of_measure  := NULL;
3131       END IF;
3132 
3133       -- Call Split_Delivery_Details API for each lot
3134       split_delivery_details(
3135         p_organization_id            => p_org_id
3136       , p_lpn_id                     => p_lpn_id
3137       , p_xfr_lpn_id                 => p_xfr_lpn_id
3138       , p_item_rec                   => p_item_rec
3139       , p_revision                   => p_revision
3140       , p_lot_number                 => lottmp_rec.lot_number
3141       , p_quantity                   => lottmp_rec.primary_quantity
3142       , p_uom_code                   => p_uom
3143       , p_secondary_trx_quantity     => ABS(lottmp_rec.secondary_quantity)
3144       , p_secondary_uom_code         => lottmp_rec.secondary_unit_of_measure
3145       , p_serial_trx_temp_id         => lottmp_rec.serial_transaction_temp_id
3146       , p_subinventory_code          => p_subinventory_code
3147       , p_locator_id                 => p_locator_id
3148       , p_xfr_subinventory           => p_xfr_subinventory
3149       , p_xfr_to_location            => p_xfr_to_location
3150       , p_transaction_source_id      => p_transaction_source_id
3151       , p_trx_source_line_id         => p_trx_source_line_id
3152       );
3153     END LOOP;
3154 
3155     IF (NOT l_lotfound) THEN
3156       IF (l_pricing_ind = wms_catch_weight_pvt.g_price_secondary) THEN
3157         wms_catch_weight_pvt.show_ct_wt_for_split(
3158           x_return_status              => ret_status
3159         , x_msg_data                   => ret_msgcnt
3160         , x_msg_count                  => ret_msgdata
3161         , p_org_id                     => p_org_id
3162         , p_from_lpn_id                => p_lpn_id
3163         , p_from_item_id               => p_item_rec.inventory_item_id
3164         , p_from_item_revision         => p_revision
3165         , p_from_item_lot_number       => NULL
3166         , p_to_lpn_id                  => p_xfr_lpn_id
3167         , x_show_ct_wt                 => l_valid_sec_qty_split
3168         );
3169 
3170         IF (ret_status <> fnd_api.g_ret_sts_success) THEN
3171           fnd_message.set_name('INV', 'WMS_GET_CATCH_WEIGHT_ATT_FAIL');
3172           fnd_msg_pub.ADD;
3173           RAISE fnd_api.g_exc_unexpected_error;
3174         END IF;
3175 
3176         IF (l_debug = 1) THEN
3177           inv_log_util.TRACE('l_valid_sec_qty_split=' || l_valid_sec_qty_split, l_api_name, 9);
3178         END IF;
3179 
3180         -- Secondary quantities are not allowed do not pass it to
3181         -- the Split_Delivery_Details API
3182         IF (l_valid_sec_qty_split <> 1) THEN
3183           l_secondary_trx_quantity  := NULL;
3184           l_secondary_uom_code      := NULL;
3185           --Clear the secondary quantity fields for the from LPN
3186           wms_catch_weight_pvt.update_lpn_secondary_quantity(
3187             p_api_version                => 1.0
3188           , x_return_status              => ret_status
3189           , x_msg_count                  => ret_msgcnt
3190           , x_msg_data                   => ret_msgdata
3191           , p_record_source              => 'WDD'
3192           , p_organization_id            => p_org_id
3193           , p_lpn_id                     => p_lpn_id
3194           , p_inventory_item_id          => p_item_rec.inventory_item_id
3195           , p_revision                   => p_revision
3196           , p_quantity                   => NULL
3197           , p_uom_code                   => NULL
3198           , p_secondary_quantity         => NULL
3199           , p_secondary_uom_code         => NULL
3200           );
3201 
3202           IF (ret_status <> fnd_api.g_ret_sts_success) THEN
3203             IF (l_debug = 1) THEN
3204               inv_log_util.TRACE('Error calling Update_LPN_Secondary_Quantity: ' || ret_msgdata, l_api_name, 1);
3205             END IF;
3206 
3207             RAISE fnd_api.g_exc_unexpected_error;
3208           END IF;
3209 
3210           --Clear the secondary quantity fields for the to LPN
3211           wms_catch_weight_pvt.update_lpn_secondary_quantity(
3212             p_api_version                => 1.0
3213           , x_return_status              => ret_status
3214           , x_msg_count                  => ret_msgcnt
3215           , x_msg_data                   => ret_msgdata
3216           , p_record_source              => 'WDD'
3217           , p_organization_id            => p_org_id
3218           , p_lpn_id                     => p_xfr_lpn_id
3219           , p_inventory_item_id          => p_item_rec.inventory_item_id
3220           , p_revision                   => p_revision
3221           , p_quantity                   => NULL
3222           , p_uom_code                   => NULL
3223           , p_secondary_quantity         => NULL
3224           , p_secondary_uom_code         => NULL
3225           );
3226 
3227           IF (ret_status <> fnd_api.g_ret_sts_success) THEN
3228             IF (l_debug = 1) THEN
3229               inv_log_util.TRACE('Error calling Update_LPN_Secondary_Quantity: ' || ret_msgdata, l_api_name, 1);
3230             END IF;
3231 
3232             RAISE fnd_api.g_exc_unexpected_error;
3233           END IF;
3234         END IF;
3235       ELSIF(l_secondary_trx_quantity IS NOT NULL) THEN
3236         -- secondary qty populated even though item is not catch weight
3237         -- enabled.  Do not pass values to split delivery details api
3238         l_secondary_trx_quantity  := NULL;
3239         l_secondary_uom_code      := NULL;
3240       END IF;
3241 
3242       -- Call Split_Delivery_Details API once for the MMTT record
3243       split_delivery_details(
3244         p_organization_id            => p_org_id
3245       , p_lpn_id                     => p_lpn_id
3246       , p_xfr_lpn_id                 => p_xfr_lpn_id
3247       , p_item_rec                   => p_item_rec
3248       , p_revision                   => p_revision
3249       , p_lot_number                 => NULL
3250       , p_quantity                   => p_qty
3251       , p_uom_code                   => p_uom
3252       , p_secondary_trx_quantity     => ABS(l_secondary_trx_quantity)
3253       , p_secondary_uom_code         => l_secondary_uom_code
3254       , p_serial_trx_temp_id         => p_tempid
3255       , p_subinventory_code          => p_subinventory_code
3256       , p_locator_id                 => p_locator_id
3257       , p_xfr_subinventory           => p_xfr_subinventory
3258       , p_xfr_to_location            => p_xfr_to_location
3259       , p_transaction_source_id      => p_transaction_source_id
3260       , p_trx_source_line_id         => p_trx_source_line_id
3261       );
3262     END IF;
3263 
3264     IF (l_debug = 1) THEN
3265       inv_log_util.TRACE('** Split Delivery OK', l_api_name, 1);
3266     END IF;
3267   END split_delivery;
3268 
3269 
3270    /********************************************************************
3271    *
3272    * Transaction Manager Wrapper
3273    * This function does the LPN-related processing and then calls the
3274    * Java Transaction manager (replacement of inltpu) based on the
3275    * parameter p_process_trx
3276    *
3277    *******************************************************************/
3278 
3279    FUNCTION process_lpn_trx(
3280      p_trx_hdr_id         IN            NUMBER
3281    , p_commit             IN            VARCHAR2 := fnd_api.g_false
3282    , x_proc_msg           OUT NOCOPY    VARCHAR2
3283    , p_proc_mode          IN            NUMBER := NULL
3284    , p_process_trx        IN            VARCHAR2 := fnd_api.g_true
3285    , p_atomic             IN            VARCHAR2 := fnd_api.g_false
3286    , p_business_flow_code IN            NUMBER := NULL
3287    )
3288      RETURN NUMBER IS
3289    BEGIN
3290      RETURN process_lpn_trx(p_trx_hdr_id, p_commit, x_proc_msg, p_proc_mode, p_process_trx, p_atomic, p_business_flow_code, TRUE);
3291    END process_lpn_trx;
3292 
3293 
3294 
3295   /********************************************************************
3296    *
3297    * Transaction Manager Wrapper
3298    * This function does the LPN-related processing and then calls the
3299    * Java Transaction manager (replacement of inltpu) based on the
3300    * parameter p_process_trx
3301    * The message stack is initialized only if the new parameter
3302    * p_init_msg_list is true.
3303    *
3304    *******************************************************************/
3305   FUNCTION process_lpn_trx(
3306     p_trx_hdr_id         IN            NUMBER
3307   , p_commit             IN            VARCHAR2 := fnd_api.g_false
3308   , x_proc_msg           OUT NOCOPY    VARCHAR2
3309   , p_proc_mode          IN            NUMBER := NULL
3310   , p_process_trx        IN            VARCHAR2 := fnd_api.g_true
3311   , p_atomic             IN            VARCHAR2 := fnd_api.g_false
3312   , p_business_flow_code IN            NUMBER := NULL
3313   , p_init_msg_list      IN            BOOLEAN
3314   )
3315     RETURN NUMBER IS
3316 
3317     -- Bug# 7435480 Added trasanction_batch_seq in the order by clause
3318     CURSOR c_mmtt IS
3319       SELECT   *
3320           FROM mtl_material_transactions_temp
3321          WHERE transaction_header_id = p_trx_hdr_id
3322            AND NVL(transaction_status, 1) <> 2   -- don't consider suggestions
3323            AND process_flag = 'Y'
3324       ORDER BY transaction_batch_id,transaction_batch_seq;
3325 
3326     /* Jalaj Srivastava Bug 4634410
3327        Added cursor c_mtlt */
3328     /* Jalaj Srivastava Bug 5446542
3329        Cursor c_mtlt is no longer used */
3330     /* *****************************************************
3331     CURSOR c_mtlt (p_transaction_temp_id NUMBER) IS
3332       SELECT  lot_number
3333              ,primary_quantity
3334       FROM    mtl_transaction_lots_temp
3335       WHERE   transaction_temp_id = p_transaction_temp_id;
3336        *****************************************************
3337 
3338 
3339      /* Jalaj Srivastava
3340        Cursor to select transactions
3341        for which OPM-QM workflow event will be raised */
3342     CURSOR cur_get_txn_for_opm_qm IS
3343       SELECT  mmt.transaction_id
3344              ,mmt.transaction_action_id
3345              ,mmt.transaction_source_type_id
3346       FROM    mtl_material_transactions mmt
3347       WHERE   mmt.transaction_set_id   = p_trx_hdr_id
3348       AND     mmt.transaction_quantity > 0
3349       AND     exists (select 1
3350                       from   mtl_parameters mp
3351                       where  mp.organization_id   = mmt.organization_id
3352                       and    process_enabled_flag = 'Y')
3353       AND     exists (select 1
3354                       from   mtl_system_items_b msib
3355                       where  msib.inventory_item_id            = mmt.inventory_item_id
3356                       and    msib.organization_id              = mmt.organization_id
3357                       and    msib.process_quality_enabled_flag = 'Y')
3358       AND     (      (     (    (     (mmt.transaction_action_id IN ( inv_globals.g_action_issue
3359                                                           ,inv_globals.g_action_receipt
3360                                                           ,inv_globals.g_action_subxfr
3361                                                           ,inv_globals.g_action_orgxfr
3362                                                           ,inv_globals.g_action_intransitshipment
3363                                                           ,inv_globals.g_action_intransitreceipt))
3364                                   --Bug#6509707.Included Account Alias Receipt.
3365                                   AND (mmt.transaction_source_type_id IN (inv_globals.g_sourcetype_inventory,inv_globals.g_sourcetype_accountalias)
3366                                ) )
3367                              -- Pawan Kumar added bug 5533472
3368                              OR (
3369                                    (mmt.transaction_action_id         = inv_globals.g_action_assycomplete)
3370                                AND (mmt.transaction_source_type_id    = inv_globals.g_sourcetype_wip)
3371                                  )
3372                              OR (
3373                                       (mmt.transaction_action_id      = inv_globals.g_action_cyclecountadj)
3374                                   AND (mmt.transaction_source_type_id = inv_globals.g_sourcetype_cyclecount)
3375                                 )
3376                              OR (
3377                                       (mmt.transaction_action_id      = inv_globals.g_action_physicalcountadj)
3378                                   AND (mmt.transaction_source_type_id = inv_globals.g_sourcetype_physicalcount)
3379                                 )
3380 
3381                            )
3382                        AND (exists (select gisv.spec_id
3383                                     from   gmd_inventory_spec_vrs gisv, gmd_specifications_b gsb
3384                                     where  gsb.inventory_item_id =  mmt.inventory_item_id
3385                                     and    gsb.spec_status       in (400,700)
3386                                     and    gsb.delete_mark       =  0
3387                                     and    gisv.spec_id          = gsb.spec_id
3388                                     and    gisv.delete_mark      = 0
3389                                     and    (gisv.organization_id is null OR gisv.organization_id = mmt.organization_id)))
3390                      )
3391                  OR  (     (mmt.transaction_action_id      =  inv_globals.g_action_receipt)
3392                        AND (mmt.transaction_source_type_id in ( inv_globals.g_sourcetype_purchaseorder
3393                                                                ,inv_globals.g_sourcetype_rma
3394                                                                ,inv_globals.g_sourcetype_intreq))
3395                        AND (     (exists (select gssv.spec_id
3396                                           from   gmd_supplier_spec_vrs gssv, gmd_specifications_b gsb
3397                                           where  gsb.inventory_item_id =  mmt.inventory_item_id
3398                                           and    gsb.spec_status       in (400,700)
3399                                           and    gsb.delete_mark       =  0
3400                                           and    gssv.spec_id          = gsb.spec_id
3401                                           and    gssv.delete_mark      = 0
3402                                           and    (gssv.organization_id is null OR gssv.organization_id = mmt.organization_id)))
3403                              OR  (exists (select gisv.spec_id
3404                                           from   gmd_inventory_spec_vrs gisv, gmd_specifications_b gsb
3405                                           where  gsb.inventory_item_id =  mmt.inventory_item_id
3406                                           and    gsb.spec_status       in (400,700)
3407                                           and    gsb.delete_mark       =  0
3408                                           and    gisv.spec_id          = gsb.spec_id
3409                                           and    gisv.delete_mark      = 0
3410                                           and    (gisv.organization_id is null OR gisv.organization_id = mmt.organization_id)))
3411 
3412                            )
3413                      )
3414               );
3415    /* BUG 5361705 - raise opm qm event for every distinct lot */
3416    Cursor cr_get_distinct_lots(l_txn_id NUMBER) IS
3417    select  distinct mtln.product_transaction_id,mln.gen_object_id
3418    from    mtl_transaction_lot_numbers mtln,mtl_lot_numbers mln
3419    where   transaction_id = l_txn_id
3420    and     mln.lot_number = mtln.lot_number
3421    and     mln.inventory_item_id = mtln.inventory_item_id
3422    and     mln.organization_id = mtln.organization_id;
3423 
3424     l_trx_temp_id          NUMBER;
3425     l_lotfound             BOOLEAN                                  := FALSE;
3426     insrowcnt              NUMBER                                   := 0;
3427     failedrowcnt           NUMBER                                   := 0;
3428     expldrowcnt            NUMBER                                   := 0;
3429     retval                 NUMBER                                   := 0;
3430     v_mmtt                 mtl_material_transactions_temp%ROWTYPE;
3431     v_lpn                  wms_container_pub.lpn;
3432     v_deleterow            BOOLEAN                                  := FALSE;   -- Should the original row in MMTT be deleted ?
3433     l_cst_grp              VARCHAR2(30);
3434     l_req_id               NUMBER;
3435     l_proc_mode            NUMBER                                   := p_proc_mode;
3436     l_process_trx          VARCHAR2(1)                              := p_process_trx;
3437     l_atomic               NUMBER                                   := 0;   -- Do not treat all rows with same HdrId as one unit.
3438     l_commit               NUMBER                                   := 0;   -- Do not commit yet
3439     l_comingling_occurs    VARCHAR2(1);
3440     l_count                NUMBER;
3441     l_wms_org_flag         BOOLEAN;
3442     l_comingle_sub         VARCHAR2(64);
3443     l_comingle_loc         NUMBER;
3444     l_comingle_org         NUMBER;
3445     l_comingle_cg          NUMBER;
3446     label_status           VARCHAR2(512);
3447     --l_call_tm              BOOLEAN                                  := FALSE;
3448     l_wms_installed        BOOLEAN                                  := FALSE;
3449     l_org                  NUMBER;
3450     l_containers           NUMBER;
3451     l_is_cartonization     NUMBER;
3452     l_prev_trx_batch_id    NUMBER;
3453     l_current_group_status VARCHAR2(512);
3454     l_error_code           VARCHAR2(256);
3455     l_num_ret_rows         NUMBER;
3456     l_is_from_mti          NUMBER;
3457     l_err_msg              VARCHAR2(2000)                           := NULL;
3458     l_cst_grp_id           NUMBER;
3459     l_xfr_cst_grp_id       NUMBER;
3460     -- for bug 2726323
3461     l_return_status        BOOLEAN                                  := FALSE;
3462     --fob enhencement for J
3463     l_fob_ret_sts          VARCHAR2(1);
3464     l_fob_ret_msg          VARCHAR2(2000);
3465     l_fob_msg_count        NUMBER;
3466     l_debug                NUMBER                                   := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
3467     -- Nested lpn changes
3468     l_putway_explode_req   VARCHAR2(1)                              := 'N';
3469     -- HVERDDIN ERES START
3470     l_erecord_id           NUMBER;
3471     l_trans_status         VARCHAR2(30)                             := 'SUCCESS';
3472     l_eres_enabled         VARCHAR2(3)                              := NVL(fnd_profile.VALUE('EDR_ERES_ENABLED'), 'N');
3473     tbl_index              BINARY_INTEGER                           := 1;
3474     l_eres_tbl             eres_tbl;
3475   -- HVERDDIN ERES END
3476     l_process              NUMBER                                   := 0;
3477     l_dcp_profile	   NUMBER;
3478     l_dcp_return_status    VARCHAR2(1);
3479     l_lot_indiv_trx_valid  boolean;
3480     l_secondary_txn_quantity number;
3481   BEGIN
3482     IF (l_debug = 1) THEN
3483       inv_log_util.TRACE(
3484         'Call to PROCESS_LPN_TRX trxhdr=' || p_trx_hdr_id || ',procmode=' || p_proc_mode || ',bflow=' || p_business_flow_code
3485       , 'INV_LPN_TRX_PUB'
3486       , 9
3487       );
3488     END IF;
3489 
3490     --Bug 4338316
3491     --Setting the local variable l_commit to honor the p_commit, if passed as true.
3492     IF fnd_api.to_boolean(p_commit) THEN
3493  	    l_commit := 1;
3494  	  END IF;
3495 
3496     -- Release 12 - Call to Enhanced Diagnostics Check Implemented
3497     -- Enhanced Diagnostics Check API is called if Profile is set.
3498     -- Errors are ignored since this should not stop the txn flow.
3499    BEGIN   --{
3500     l_dcp_profile := INV_DCP_PVT.G_CHECK_DCP;
3501     IF l_dcp_profile IS NULL THEN
3502        l_dcp_profile := inv_dcp_pvt.is_dcp_enabled;
3503     END IF;
3504     IF (l_debug = 1) THEN
3505       inv_log_util.TRACE('l_dcp_profile :'||l_dcp_profile,'INV_LPN_TRX_PUB',9);
3506     END IF;
3507     IF (l_dcp_profile =1) then
3508      INV_DCP_PVT.validate_data(
3509                         p_dcp_event       => 'Validate MMTT',
3510                         p_trx_hdr_id      => p_trx_hdr_id,
3511                         p_temp_id         => Null,
3512 		        p_batch_id        => Null,
3513                         p_raise_exception => 'N',
3514 		 	x_return_status   => l_dcp_return_status);
3515     END IF;
3516     IF (l_debug = 1) THEN
3517       inv_log_util.TRACE('After DCP Call -'||l_dcp_return_status,'INV_LPN_TRX_PUB',9);
3518     END IF;
3519    EXCEPTION
3520     WHEN OTHERS THEN
3521      IF (l_debug = 1) THEN
3522       inv_log_util.TRACE('DCP Error :'||substr(sqlerrm,1,240),'INV_LPN_TRX_PUB',9);
3523      END IF;
3524    END; ---}
3525 
3526 
3527     -- For BUG 2919763, initializing the message stack only if the new parameter
3528     -- p_init_msg_list is true.
3529     IF (p_init_msg_list = TRUE) THEN
3530       IF (l_debug = 1) THEN
3531         inv_log_util.TRACE('Initializing the message list', 'INV_LPN_TRX_PUB', 9);
3532       END IF;
3533       fnd_msg_pub.initialize;
3534     END IF;
3535 
3536 
3537 
3538     -- Set a save point. In case of error, rollback to this point
3539     -- we would still have the exploded contents in MMTT
3540     SAVEPOINT process_lpn_trx;
3541 
3542     -- If l_proc_mode is not filled, then check mode from profile
3543     IF (l_proc_mode IS NULL) THEN
3544       l_proc_mode  := fnd_profile.VALUE('TRANSACTION_PROCESS_MODE');
3545 
3546       IF (l_debug = 1) THEN
3547         inv_log_util.TRACE('TRANSACTION_PROCESS_MODE for ProcMode= ' || l_proc_mode, 'INV_LPN_TRX_PUB', 9);
3548       END IF;
3549 
3550       -- if trx_process_mode is set for control at form level, then query
3551       -- form level profiles
3552       IF (l_proc_mode = 4) THEN
3553         l_proc_mode  := NULL;
3554       END IF;
3555     END IF;
3556 
3557 
3558 
3559       -- for bug 2726323 : Calling this funtion so as the variable
3560     --  WMS_INSTALL.G_WMS_INSTALLATION_STATUS gets set properly.
3561     l_return_status  :=
3562       wms_install.check_install(x_return_status => ret_status, x_msg_count => ret_msgcnt, x_msg_data => ret_msgdata
3563       , p_organization_id            => NULL);
3564 
3565 
3566 
3567     -- Check if WMS is installed, if so check for cartonization transaction
3568     IF (wms_install.g_wms_installation_status = 'I') THEN
3569       BEGIN
3570         SELECT 1
3571           INTO l_is_cartonization
3572           FROM DUAL
3573          WHERE EXISTS(
3574                  SELECT 1
3575                    FROM mtl_material_transactions_temp
3576                   WHERE transaction_action_id = inv_globals.g_action_containerpack
3577                     AND transfer_lpn_id IS NULL
3578                     AND transaction_header_id = p_trx_hdr_id);
3579       EXCEPTION
3580         WHEN NO_DATA_FOUND THEN
3581           l_is_cartonization  := 0;
3582       END;
3583 
3584       IF (l_debug = 1) THEN
3585         inv_log_util.TRACE('l_is_cartonization=' || l_is_cartonization, 'INV_LPN_TRX_PUB', 9);
3586       END IF;
3587 
3588       IF (l_is_cartonization = 1) THEN
3589         -- Cartonization request call cartonization process
3590         IF (l_debug = 1) THEN
3591           inv_log_util.TRACE('**** Cartonization transaction', 'INV_LPN_TRX_PUB', 9);
3592         END IF;
3593 
3594         -- Retrieve level and organization information for cartonization
3595         -- If there is a mix of contaner levels, take the maxium level.  If
3596         -- all levels are null, cartonize until end.
3597         BEGIN
3598           SELECT   organization_id
3599                  , MAX(containers)
3600               INTO l_org
3601                  , l_containers
3602               FROM mtl_material_transactions_temp
3603              WHERE transaction_header_id = p_trx_hdr_id
3604                AND NVL(transaction_status, 1) <> 2
3605                AND process_flag = 'Y'
3606            GROUP BY organization_id;
3607         EXCEPTION
3608           WHEN NO_DATA_FOUND THEN
3609             IF (l_debug = 1) THEN
3610               inv_log_util.TRACE('no rows retrieve when finding org and level for Cartonization', 'INV_LPN_TRX_PUB', 1);
3611             END IF;
3612 
3613             RAISE fnd_api.g_exc_unexpected_error;
3614           WHEN OTHERS THEN
3615             IF (l_debug = 1) THEN
3616               inv_log_util.TRACE('Unexpected error when finding org and level for Cartonization', 'INV_LPN_TRX_PUB', 1);
3617             END IF;
3618 
3619             RAISE fnd_api.g_exc_unexpected_error;
3620         END;
3621 
3622         wms_cartnzn_wrap.cartonize(
3623           p_api_version                => 1.0
3624         , p_init_msg_list              => fnd_api.g_false
3625         , p_commit                     => fnd_api.g_false
3626         ,
3627           --p_validation_level      => ,
3628           x_return_status              => ret_status
3629         , x_msg_count                  => ret_msgcnt
3630         , x_msg_data                   => ret_msgdata
3631         ,
3632           --p_out_bound             => ,
3633           p_org_id                     => l_org
3634         ,
3635           --p_move_order_header_id    => ,
3636           --p_disable_cartonization    => ,
3637           p_transaction_header_id      => p_trx_hdr_id
3638         , p_stop_level                 => l_containers
3639         , p_packaging_mode             => 2
3640         );
3641 
3642         IF (ret_status = fnd_api.g_ret_sts_error) THEN
3643           IF (l_debug = 1) THEN
3644             inv_log_util.TRACE('Error in calling Cartonization', 'INV_LPN_TRX_PUB', 1);
3645           END IF;
3646 
3647           RAISE fnd_api.g_exc_error;
3648         ELSIF(ret_status = fnd_api.g_ret_sts_unexp_error) THEN
3649           IF (l_debug = 1) THEN
3650             inv_log_util.TRACE('Unexpectied error in calling Cartonization', 'INV_LPN_TRX_PUB', 1);
3651           END IF;
3652 
3653           RAISE fnd_api.g_exc_unexpected_error;
3654         ELSIF(ret_status <> fnd_api.g_ret_sts_success) THEN
3655           IF (l_debug = 1) THEN
3656             inv_log_util.TRACE('Undefined error in calling Cartonization', 'INV_LPN_TRX_PUB', 1);
3657           END IF;
3658 
3659           RAISE fnd_api.g_exc_unexpected_error;
3660         END IF;
3661 
3662         IF (l_debug = 1) THEN
3663           inv_log_util.TRACE('Done with call to Cartonization', 'INV_LPN_TRX_PUB', 9);
3664         END IF;
3665 
3666         --Need to transfer cartonization_id to transfer_lpn_id to pack into
3667         --suggested LPN
3668         IF (l_debug = 1) THEN
3669           inv_log_util.TRACE('Transfering Cartonization suggestions', 'INV_LPN_TRX_PUB', 9);
3670         END IF;
3671 
3672         UPDATE mtl_material_transactions_temp
3673            SET transfer_lpn_id = cartonization_id
3674          WHERE transaction_header_id = p_trx_hdr_id;
3675 
3676         IF (l_debug = 1) THEN
3677           inv_log_util.TRACE('Done transfering Cartonization suggestions', 'INV_LPN_TRX_PUB', 9);
3678         END IF;
3679       END IF;
3680     END IF;
3681 
3682 
3683 
3684     -- For atomic transactions create a single savepoint for the batch
3685     IF fnd_api.to_boolean(p_atomic) THEN
3686       SAVEPOINT group_savepoint;
3687     END IF;
3688 
3689     l_process := 0;
3690 
3691     --************* Open cursor into MMTT  *************
3692     FOR v_mmtt IN c_mmtt LOOP
3693       IF (l_debug = 1) THEN
3694         inv_log_util.TRACE('trxtempid='||v_mmtt.transaction_temp_id||'srctypeid='||v_mmtt.transaction_source_type_id||'actid='||v_mmtt.transaction_action_id||',batchid='||v_mmtt.transaction_batch_id, 'INV_LPN_TRX_PUB', 9);
3695         inv_log_util.TRACE('seqid='||v_mmtt.transaction_sequence_id||',lpnid='||v_mmtt.lpn_id||',cntlpnid='||v_mmtt.content_lpn_id||'xfrlpnid='||v_mmtt.transfer_lpn_id, 'INV_LPN_TRX_PUB', 9);
3696         inv_log_util.TRACE('item_id='||v_mmtt.inventory_item_id||'qty='||v_mmtt.transaction_quantity||'uom='||v_mmtt.transaction_uom||'CG='||v_mmtt.cost_group_id||',XCG='||v_mmtt.transfer_cost_group_id, 'INV_LPN_TRX_PUB', 9);
3697         inv_log_util.TRACE('ccid='||v_mmtt.cycle_count_id, 'INV_LPN_TRX_PUB', 9);
3698       END IF;
3699 
3700       BEGIN
3701         /* Jalaj Srivastava Bug 4634410
3702            Check lot indivisibility.
3703            check is needed here since same lot could be
3704            created in two locations within the same session.
3705            quantity tree built/updated in forms session was not getting
3706            picked up in pl/sql layer which would
3707            have enabled to catch the error earlier than this point */
3708            --Jalaj Srivastava Bug 5515181
3709            --pass primary_quantity instead of txn qty
3710         /* Jalaj Srivastava Bug 5446542
3711            Lot indivisibility check will not be done
3712            here. it will be done through the forms
3713            or by the txn group layer */
3714         /* ********************************************************************
3715         IF (v_mmtt.lot_number IS NOT NULL) THEN
3716           l_lot_indiv_trx_valid := INV_LOT_API_PUB.VALIDATE_LOT_INDIVISIBLE
3717             ( p_api_version          => 1.0
3718             , p_init_msg_list        => fnd_api.g_false
3719             , p_transaction_type_id  => v_mmtt.transaction_type_id
3720             , p_organization_id      => v_mmtt.organization_id
3721             , p_inventory_item_id    => v_mmtt.inventory_item_id
3722             , p_revision             => v_mmtt.revision
3723             , p_subinventory_code    => v_mmtt.subinventory_code
3724             , p_locator_id           => v_mmtt.locator_id
3725             , p_lot_number           => v_mmtt.lot_number
3726             , p_primary_quantity     => v_mmtt.primary_quantity
3727             , p_qoh                  => NULL
3728             , p_atr                  => NULL
3729             , x_return_status        => ret_status
3730             , x_msg_count            => ret_msgcnt
3731             , x_msg_data             => ret_msgdata);
3732 
3733           IF (NOT l_lot_indiv_trx_valid) THEN
3734           -- the transaction is not valid regarding lot indivisible:
3735             IF (l_debug = 1) THEN
3736               inv_log_util.trace('in proc process_lpn_trx. lot indivisibility error', 'INV_LPN_TRX_PUB', 9);
3737             END IF;
3738             RAISE FND_API.G_EXC_ERROR;
3739           END IF;
3740         ELSE
3741           FOR v_mtlt in c_mtlt(v_mmtt.transaction_temp_id) LOOP
3742             l_lot_indiv_trx_valid := INV_LOT_API_PUB.VALIDATE_LOT_INDIVISIBLE
3743               ( p_api_version          => 1.0
3744               , p_init_msg_list        => fnd_api.g_false
3745               , p_transaction_type_id  => v_mmtt.transaction_type_id
3746               , p_organization_id      => v_mmtt.organization_id
3747               , p_inventory_item_id    => v_mmtt.inventory_item_id
3748               , p_revision             => v_mmtt.revision
3749               , p_subinventory_code    => v_mmtt.subinventory_code
3750               , p_locator_id           => v_mmtt.locator_id
3751               , p_lot_number           => v_mtlt.lot_number
3752               , p_primary_quantity     => v_mtlt.primary_quantity
3753               , p_qoh                  => NULL
3754               , p_atr                  => NULL
3755               , x_return_status        => ret_status
3756               , x_msg_count            => ret_msgcnt
3757               , x_msg_data             => ret_msgdata);
3758 
3759             IF (NOT l_lot_indiv_trx_valid) THEN
3760             -- the transaction is not valid regarding lot indivisible:
3761               IF (l_debug = 1) THEN
3762                 inv_log_util.trace('in proc process_lpn_trx. lot indivisibility error', 'INV_LPN_TRX_PUB', 9);
3763               END IF;
3764               RAISE FND_API.G_EXC_ERROR;
3765             END IF;
3766 
3767           END LOOP;
3768         END IF;
3769         ******************************************************************** */
3770 
3771         /* HVERDDIN ADDED Following code to support ERES */
3772         /* Only execute code if ERES enabled */
3773         IF l_eres_enabled <> 'N' THEN
3774           l_eres_tbl(tbl_index).event_id                    := v_mmtt.original_transaction_temp_id;
3775           l_eres_tbl(tbl_index).transaction_action_id       := v_mmtt.transaction_action_id;
3776           l_eres_tbl(tbl_index).transaction_source_type_id  := v_mmtt.transaction_source_type_id;
3777 
3778           IF (l_debug = 1) THEN
3779             inv_log_util.TRACE('ERES event id               => ' || l_eres_tbl(tbl_index).event_id, 'INV_LPN_TRX_PUB', 9);
3780             inv_log_util.TRACE('Transaction Action  Id      => ' || l_eres_tbl(tbl_index).transaction_action_id, 'INV_LPN_TRX_PUB', 9);
3781             inv_log_util.TRACE('Transaction Source Type Id  => ' || l_eres_tbl(tbl_index).transaction_source_type_id, 'INV_LPN_TRX_PUB', 9);
3782           END IF;
3783         END IF;
3784 
3785         /*------------------------------------------------------+
3786         | Validate Logical Transactions.
3787         |========================================================
3788         | Add a check to prevent processing logical transactions
3789         | that are populated directly to the transactions table.
3790         | No new logical transactions should go through MMTT.
3791         | Opened up the logical po receipt and logical delivery
3792         | adjustment to go through TM for lot serial support
3793         +------------------------------------------------------*/
3794         IF (
3795             (
3796              v_mmtt.transaction_source_type_id = inv_globals.g_sourcetype_inventory
3797              AND v_mmtt.transaction_action_id IN
3798                   (
3799                    inv_globals.g_action_logicalissue
3800                  , inv_globals.g_action_logicalicsales
3801                  , inv_globals.g_action_logicalicreceipt
3802                  , inv_globals.g_action_logicalicrcptreturn
3803                  , inv_globals.g_action_logicalicsalesreturn
3804                  , inv_globals.g_action_logicalreceipt
3805                   )
3806             )
3807             OR(
3808                v_mmtt.transaction_source_type_id = inv_globals.g_sourcetype_rma
3809                AND v_mmtt.transaction_action_id = inv_globals.g_action_logicalreceipt
3810               )
3811             OR(
3812                v_mmtt.transaction_source_type_id = inv_globals.g_sourcetype_purchaseorder
3813                AND v_mmtt.transaction_action_id IN(inv_globals.g_action_logicalissue,
3814                                                                                      --     inv_globals.G_action_logicaldeladj,
3815                                                                                      --     inv_globals.G_action_logicalreceipt,
3816                                                                                      inv_globals.g_action_retropriceupdate)
3817               )
3818             OR(
3819                v_mmtt.transaction_source_type_id = inv_globals.g_sourcetype_rma
3820                AND v_mmtt.transaction_action_id = inv_globals.g_action_logicalreceipt
3821               )
3822             OR(
3823                v_mmtt.transaction_source_type_id = inv_globals.g_sourcetype_intreq
3824                AND v_mmtt.transaction_action_id = inv_globals.g_action_logicalexpreqreceipt
3825               )
3826             OR(
3827                v_mmtt.transaction_source_type_id = inv_globals.g_sourcetype_salesorder
3828                AND v_mmtt.transaction_action_id = inv_globals.g_action_logicalissue
3829               )
3830            ) THEN
3831           fnd_message.set_name('INV', 'INV_INT_TRXACTCODE');
3832           fnd_msg_pub.ADD;
3833           RAISE fnd_api.g_exc_error;
3834         END IF;
3835 
3836         -- For new or null transaction groups that are non atomic, create a new savepoint
3837         IF (NVL(v_mmtt.transaction_batch_id, -88) <> NVL(l_prev_trx_batch_id, -99)
3838             AND NOT fnd_api.to_boolean(p_atomic)) THEN
3839           SAVEPOINT group_savepoint;
3840           l_prev_trx_batch_id     := v_mmtt.transaction_batch_id;
3841           l_current_group_status  := fnd_api.g_ret_sts_success;
3842 
3843           IF (l_debug = 1) THEN
3844             inv_log_util.TRACE('new group savepoint prevbatchid=' || l_prev_trx_batch_id, 'INV_LPN_TRX_PUB', 9);
3845           END IF;
3846         END IF;
3847 
3848         -- Skip records that are of the same trx group where a record has failed
3849         -- Else run records normally
3850         IF NOT(
3851                v_mmtt.transaction_batch_id = l_prev_trx_batch_id
3852                AND NVL(l_current_group_status, fnd_api.g_ret_sts_success) <> fnd_api.g_ret_sts_success
3853               ) THEN
3854           insrowcnt    := insrowcnt + 1;
3855 
3856           --For planning xfr and consigned xfr transactions, make sure the
3857           --transactions are always processed in online mode. bug 3453619
3858 	  --For any transaction in mmtt which is an explicit transaction,
3859 	  --implying that the owning_org or planning_org is populated in
3860 	  --mmtt make sure the transactions are alwaya processed in online
3861 	  -- mode bug 3691234
3862           IF ((v_mmtt.transaction_action_id = inv_globals.g_action_planxfr)
3863               OR(v_mmtt.transaction_action_id = inv_globals.g_action_ownxfr)
3864 	      OR (Nvl(v_mmtt.owning_organization_id,v_mmtt.organization_id)
3865 		      <> v_mmtt.organization_id)
3866 		  OR (Nvl(v_mmtt.planning_organization_id,v_mmtt.organization_id)
3867 		      <> v_mmtt.organization_id)) THEN
3868             l_proc_mode  := 1;
3869           END IF;
3870 
3871           -- If l_proc_mode is not set at this stage, it means control is at
3872           -- Forms-level Transaction-process profiles based on Transaction
3873           IF (l_proc_mode IS NULL) THEN
3874             IF (
3875                 (v_mmtt.transaction_source_type_id = inv_globals.g_sourcetype_inventory)
3876                 AND(v_mmtt.transaction_action_id = inv_globals.g_action_subxfr)
3877                ) THEN
3878               l_proc_mode  := fnd_profile.VALUE('SUBINV_TRANS_TXN');
3879 
3880               IF (l_debug = 1) THEN
3881                 inv_log_util.TRACE('Using SUBINV_TRANS_TXN to set process_mode. ProcMode=' || l_proc_mode, 'INV_LPN_TRX_PUB', 9);
3882               END IF;
3883             ELSIF(
3884                   (
3885                    (v_mmtt.transaction_source_type_id = inv_globals.g_sourcetype_inventory)
3886                    OR(v_mmtt.transaction_source_type_id = inv_globals.g_sourcetype_account)
3887                   )
3888                   AND(
3889                       (v_mmtt.transaction_action_id = inv_globals.g_action_issue)
3890                       OR(v_mmtt.transaction_action_id = inv_globals.g_action_receipt)
3891                      )
3892                  ) THEN
3893               l_proc_mode  := fnd_profile.VALUE('ACCOUNT_ISSUE_TXN');
3894 
3895               IF (l_debug = 1) THEN
3896                 inv_log_util.TRACE('Using ACCOUNT_ISSUE_TXN to set process_mode. ProcMode = ' || l_proc_mode, 'INV_LPN_TRX_PUB', 9);
3897               END IF;
3898             ELSIF(
3899                   (v_mmtt.transaction_source_type_id = inv_globals.g_sourcetype_moveorder)
3900                   AND(
3901                       (v_mmtt.transaction_action_id = inv_globals.g_action_receipt)
3902                       OR(v_mmtt.transaction_action_id = inv_globals.g_action_issue)
3903                      )
3904                  ) THEN
3905               l_proc_mode  := fnd_profile.VALUE('TRANSFER_ORDER_TXN');
3906 
3907               IF (l_debug = 1) THEN
3908                 inv_log_util.TRACE('Using TRANSFER_ORDER_TXN to set process_mode. ProcMode = ' || l_proc_mode, 'INV_LPN_TRX_PUB', 9);
3909               END IF;
3910             ELSIF(
3911                   (v_mmtt.transaction_source_type_id = inv_globals.g_sourcetype_inventory)
3912                   AND(
3913                       (v_mmtt.transaction_action_id = inv_globals.g_action_orgxfr)
3914                       OR(v_mmtt.transaction_action_id = inv_globals.g_action_intransitshipment)
3915                      )
3916                  ) THEN
3917               l_proc_mode  := fnd_profile.VALUE('INTER_ORG_SHIP_TXN');
3918 
3919               IF (l_debug = 1) THEN
3920                 inv_log_util.TRACE('Using INTER_ORG_SHIP_TXN to set process_mode. ProcMode = ' || l_proc_mode, 'INV_LPN_TRX_PUB', 9);
3921               END IF;
3922             END IF;
3923           END IF;
3924 
3925           -- Sanity check. If the transaction-process mode is still Not set, then
3926           -- perform an online transaction
3927           IF (l_proc_mode IS NULL) THEN
3928             IF (l_debug = 1) THEN
3929               inv_log_util.TRACE('Warning: TrxProcess profile not set ', 'INV_LPN_TRX_PUB', 2);
3930             END IF;
3931 
3932             l_proc_mode  := 1;
3933           END IF;
3934 
3935           -- If this transaction involves any LPN based processing, then
3936           -- should ensure that the process-mode is not 'background'
3937           -- This is because pending LPN transactions in MMTT
3938           -- are currently not handled properly. Check bug 3027283 for details.
3939           -- For this case, change the process_mode to asynchronous .
3940           IF ((v_mmtt.lpn_id IS NOT NULL)
3941               OR(v_mmtt.content_lpn_id IS NOT NULL)
3942               OR(v_mmtt.transfer_lpn_id IS NOT NULL))
3943              AND(l_proc_mode = inv_txn_manager_pub.proc_mode_mmtt_bgrnd) THEN
3944             l_proc_mode  := inv_txn_manager_pub.proc_mode_mmtt_async;
3945           END IF;
3946 
3947           -- Changes added to Patchet J. This is only to be done if PO pathset J
3948           -- is installed. API update_fob_point, will derive and update
3949           -- mmtt record with the fob_point and inv_intransit_account in
3950           -- case of an intransit Rcpt or intransit Shipment transaction
3951           IF (po_code_release_grp.current_release >= po_code_release_grp.prc_11i_family_pack_j) THEN
3952             IF (
3953                 (v_mmtt.transaction_action_id = inv_globals.g_action_intransitshipment)
3954                 OR(v_mmtt.transaction_action_id = inv_globals.g_action_intransitreceipt)
3955                ) THEN
3956               update_fob_point(v_mmtt        => v_mmtt, x_return_status => l_fob_ret_sts, x_msg_data => l_fob_ret_msg
3957               , x_msg_count                  => l_fob_msg_count);
3958 
3959               IF (l_fob_ret_sts <> fnd_api.g_ret_sts_success) THEN
3960                 IF (l_debug = 1) THEN
3961                   inv_log_util.TRACE(' Error from update_fob_point:' || l_fob_ret_msg, 'INV_LPN_TRX_PUB', 1);
3962                 END IF;
3963 
3964                 fnd_message.set_name('INV', 'INV_FOB_NOT_DEFINED');
3965                 fnd_message.set_token('ENTITY1', v_mmtt.organization_id);
3966                 fnd_msg_pub.ADD;
3967                 RAISE fnd_api.g_exc_error;
3968               END IF;
3969             END IF;
3970           END IF;   --(PO pathset J installed)
3971 
3972           --Changes made for WIP TM Integration: Patchet J.
3973           --We call a WIP API which tells us wether to update the mmtt
3974           --record, With the MATERIAL_ALLOCATION_TEMP_ID with the next
3975           -- sequence. This will only be done in case of WIP.J being
3976           --installed.
3977           IF (v_mmtt.transaction_source_type_id = 5
3978               AND wip_constants.dmf_patchset_level >= wip_constants.dmf_patchset_j_value) THEN
3979             IF (wip_mtltempproc_grp.istxnidrequired(v_mmtt.transaction_temp_id)) THEN
3980               inv_log_util.TRACE('Going to update mmtt with material_allocation_temp_id', 'INV_LPN_TRX_PUB', 1);
3981 
3982               UPDATE mtl_material_transactions_temp
3983                  SET material_allocation_temp_id = mtl_material_transactions_s.NEXTVAL
3984                WHERE transaction_temp_id = v_mmtt.transaction_temp_id;
3985             END IF;
3986           END IF;
3987 
3988           --End patchset J changes for WIP.
3989 
3990           -- Call the CostGroup API for this line
3991           -- Call CostGroupAPI which will update the cost_group_id and
3992           -- xfr_cost_group_id in MMTT for this record.
3993           -- Note: This API could create additional rows into MMTT if lots/serials
3994           --  are specified which have different cost-groups., that is why our
3995           -- MMTT  cursor (c_mmtt) is being opened after calling this API
3996 
3997           -- Call CG API for all cases except when content_lpn_id is not null AND transaction_type
3998           -- is neither S.O nor I.O
3999           IF NOT(
4000                  (v_mmtt.content_lpn_id IS NOT NULL)
4001                  AND
4002                      v_mmtt.transaction_source_type_id <> inv_globals.g_sourcetype_intorder
4003                  AND
4004                      v_mmtt.transaction_source_type_id <> inv_globals.g_sourcetype_salesorder
4005                 ) THEN
4006             inv_cost_group_pvt.assign_cost_group(
4007               x_return_status              => ret_status
4008             , x_msg_count                  => ret_msgcnt
4009             , x_msg_data                   => ret_msgdata
4010             , p_organization_id            => v_mmtt.organization_id
4011             , p_mmtt_rec                   => v_mmtt
4012             , p_fob_point                  => NULL
4013             , p_line_id                    => v_mmtt.transaction_temp_id
4014             , p_input_type                 => inv_cost_group_pub.g_input_mmtt
4015             , x_cost_group_id              => l_cst_grp_id
4016             , x_transfer_cost_group_id     => l_xfr_cst_grp_id
4017             );
4018 
4019             IF (ret_status <> fnd_api.g_ret_sts_success) THEN
4020               IF (l_debug = 1) THEN
4021                 inv_log_util.TRACE(' Error from CostGrpAPI:' || ret_msgdata, 'INV_LPN_TRX_PUB', 1);
4022               END IF;
4023 	      --Bug 3804314: The message from the cost group API is
4024 	      --overwritten by INV_COST_GROUP_FAILURE. Commenting the message.
4025               --fnd_message.set_name('INV', 'INV_COST_GROUP_FAILURE');
4026               --fnd_msg_pub.ADD;
4027               RAISE fnd_api.g_exc_error;
4028             END IF;
4029           END IF;
4030 
4031           -- Continue with the process based on the value of l_proc_mode
4032           IF l_proc_mode = 2 THEN   --  Concurrent Mode
4033             -- Concurrent request. Submit request and return to caller
4034             UPDATE mtl_material_transactions_temp
4035                SET transaction_mode = inv_txn_manager_pub.proc_mode_mmtt_async
4036              WHERE transaction_header_id = p_trx_hdr_id;
4037 
4038             l_req_id  := fnd_request.submit_request(application => 'INV', program => 'INVMBTRX', argument1 => p_trx_hdr_id);
4039             COMMIT;   -- Need to commit for the request to be submitted
4040 
4041             IF (l_req_id = 0) THEN
4042               -- display error message
4043               fnd_message.set_name(application => 'INV', NAME => 'INV_SUBMIT_FAIL');
4044               fnd_message.set_token(token => 'TOKEN', VALUE => TO_CHAR(p_trx_hdr_id), TRANSLATE => FALSE);
4045               fnd_msg_pub.ADD;
4046               x_proc_msg  := fnd_msg_pub.get(fnd_msg_pub.g_last, 'F');
4047               RETURN -1;
4048             ELSE
4049               -- display request ID
4050               fnd_message.set_name(application => 'INV', NAME => 'INV_CONC_TRANSACT');
4051               fnd_message.set_token(token => 'REQUEST_ID', VALUE => TO_CHAR(l_req_id), TRANSLATE => FALSE);
4052               fnd_msg_pub.ADD;
4053               x_proc_msg  := fnd_msg_pub.get(fnd_msg_pub.g_last, 'F');
4054 
4055               IF (l_debug = 1) THEN
4056                 inv_log_util.TRACE('Concurrent Req. submitted: ' || l_req_id, 'INV_LPN_TRX_PUB', 9);
4057               END IF;
4058 
4059               RETURN 0;
4060             END IF;
4061           ELSIF l_proc_mode = 3 THEN   -- Background mode
4062             IF (l_debug = 1) THEN
4063               inv_log_util.TRACE('** Background request submitted **', 'INV_LPN_TRX_PUB', 9);
4064             END IF;
4065 
4066             -- In this case, the background transaction manager would pick
4067             -- up this row from MMTT. Update transaction_mode and return to caller
4068             UPDATE mtl_material_transactions_temp
4069                SET transaction_mode = inv_txn_manager_pub.proc_mode_mmtt_bgrnd
4070              WHERE transaction_header_id = p_trx_hdr_id;
4071 
4072             fnd_message.set_name('INV', 'INV_TXN_REQ_QUEUED');
4073             fnd_msg_pub.ADD;
4074             x_proc_msg  := fnd_msg_pub.get(fnd_msg_pub.g_last, 'F');
4075 
4076             IF fnd_api.to_boolean(p_commit) THEN
4077               COMMIT WORK;
4078             END IF;
4079 
4080             RETURN 0;
4081           END IF;
4082 
4083           -- If here, it means that this transaction is to be processed 'on-line'
4084           v_deleterow  := FALSE;
4085 
4086           -- If the transaction action is either Issue or Intransit Shipment, and
4087           -- the sign of quantity is not -ve, make it -ve.
4088           -- Bug 3736797, added update to MMTT, since MMTT was never getting updated with sign changed.
4089           IF (
4090               (
4091                (v_mmtt.transaction_action_id = inv_globals.g_action_issue)
4092                OR(v_mmtt.transaction_action_id = inv_globals.g_action_intransitshipment)
4093               )
4094               AND(v_mmtt.primary_quantity > 0)
4095              ) THEN
4096 
4097             IF (l_debug = 1) THEN
4098               inv_log_util.trace('sign of quantity is not -ve for temp_id '||v_mmtt.transaction_temp_id, 'INV_LPN_TRX_PUB',9);
4099             END IF;
4100 
4101             v_mmtt.primary_quantity  := -1 * v_mmtt.primary_quantity;
4102             v_mmtt.transaction_quantity := -1 * ABS(v_mmtt.transaction_quantity);
4103             --INVCONV kkillams
4104              IF v_mmtt.secondary_transaction_quantity IS NOT NULL THEN
4105                 --Jalaj Srivastava Bug 5138445
4106                 --get -ve of absolute
4107                 v_mmtt.secondary_transaction_quantity  := -1 * abs(v_mmtt.secondary_transaction_quantity);
4108              END IF;
4109             --END INVCONV kkillams
4110             UPDATE MTL_MATERIAL_TRANSACTIONS_TEMP
4111             SET PRIMARY_QUANTITY = v_mmtt.primary_quantity,
4112                 TRANSACTION_QUANTITY            = v_mmtt.transaction_quantity,
4113                 SECONDARY_TRANSACTION_QUANTITY  = CASE WHEN v_mmtt.secondary_uom_code IS NOT NULL THEN v_mmtt.secondary_transaction_quantity
4114                                                                                                   ELSE SECONDARY_TRANSACTION_QUANTITY
4115                                                                                                   END --INVCONV kkillams
4116             WHERE TRANSACTION_TEMP_ID = v_mmtt.transaction_temp_id
4117             AND   PRIMARY_QUANTITY > 0;
4118 
4119             IF (l_debug = 1) THEN
4120               inv_log_util.trace('Made the sign -ve for temp_id '||v_mmtt.transaction_temp_id, 'INV_LPN_TRX_PUB',9);
4121             END IF;
4122 
4123           END IF;
4124 
4125           IF (wms_control.get_current_release_level >= inv_release.get_j_release_level) THEN
4126             BEGIN
4127               SELECT 'Y'
4128                 INTO l_putway_explode_req
4129                 FROM mtl_txn_request_headers mtrh, mtl_txn_request_lines mtrl
4130                WHERE v_mmtt.move_order_line_id IS NOT NULL
4131                  AND mtrl.line_id = v_mmtt.move_order_line_id
4132                  AND mtrh.header_id = mtrl.header_id
4133                  AND v_mmtt.transaction_source_type_id IN(4, 5)
4134                  AND mtrh.move_order_type = 6
4135                  AND ROWNUM < 2;
4136 
4137               inv_log_util.TRACE(' Patchset J code. Do not explode LPN for Putaway ', 'INV_LPN_TRX_PUB', 1);
4138             EXCEPTION
4139               WHEN OTHERS THEN
4140                 inv_log_util.TRACE(' Exception while exploding ', 'INV_LPN_TRX_PUB', 1);
4141                 l_putway_explode_req  := 'N';
4142             END;
4143           END IF;
4144 
4145           -- If contentLPN is not NULL, then need to explode and insert contents
4146           -- into MMTT , unless this is a Pack/Unpack/split/SO/IO Transaction.
4147           -- Note: For SO and IO  transactions, there is no need
4148           --  to explode the LPN as there will be a seperate MMTT  line for each
4149           --  item inside the LPN
4150           IF (v_mmtt.content_lpn_id IS NOT NULL) THEN
4151             IF ( v_mmtt.transaction_action_id <> inv_globals.g_action_containerpack AND
4152                  v_mmtt.transaction_action_id <> inv_globals.g_action_containerunpack AND
4153                  v_mmtt.transaction_action_id <> inv_globals.g_action_containersplit AND
4154                  v_mmtt.transaction_source_type_id <> inv_globals.g_sourcetype_intorder AND
4155                  v_mmtt.transaction_source_type_id <> inv_globals.g_sourcetype_salesorder AND
4156                  l_putway_explode_req = 'N' )
4157             THEN
4158               expldrowcnt  := explode_and_insert(v_mmtt.content_lpn_id, p_trx_hdr_id, v_mmtt);
4159 
4160               IF (expldrowcnt = -1) THEN
4161                 IF (l_debug = 1) THEN
4162                   inv_log_util.TRACE(' Failed in EXPLODE_AND_INSERT!!', 'INV_LPN_TRX_PUB', 1);
4163                 END IF;
4164 
4165                 fnd_message.set_name('INV', 'INV_FAILED');
4166                 fnd_msg_pub.ADD;
4167                 x_proc_msg  := fnd_msg_pub.get(fnd_msg_pub.g_last, 'F');
4168                 RETURN -1;
4169               END IF;
4170 
4171               insrowcnt := insrowcnt + expldrowcnt;
4172 
4173               IF (l_debug = 1) THEN
4174                 inv_log_util.TRACE('Done exploding new lpn expldrowcnt=' || expldrowcnt || ' insrowcnt=' || insrowcnt, 'INV_LPN_TRX_PUB', 9);
4175               END IF;
4176             ELSIF ( l_putway_explode_req = 'Y' AND v_mmtt.cost_group_id IS NULL ) THEN
4177             	-- bug4475607 For WIP putaway transactions contents are not exploded, but the
4178             	-- cost group id may not exist on the record.  Call cost group api
4179               inv_cost_group_pvt.assign_cost_group(
4180                 x_return_status              => ret_status
4181               , x_msg_count                  => ret_msgcnt
4182               , x_msg_data                   => ret_msgdata
4183               , p_organization_id            => v_mmtt.organization_id
4184               , p_mmtt_rec                   => v_mmtt
4185               , p_fob_point                  => NULL
4186               , p_line_id                    => v_mmtt.transaction_temp_id
4187               , p_input_type                 => inv_cost_group_pub.g_input_mmtt
4188               , x_cost_group_id              => l_cst_grp_id
4189               , x_transfer_cost_group_id     => l_xfr_cst_grp_id );
4190 
4191               IF (ret_status <> fnd_api.g_ret_sts_success) THEN
4192                 IF (l_debug = 1) THEN
4193                   inv_log_util.TRACE(' Error from CostGrpAPI:' || ret_msgdata, 'INV_LPN_TRX_PUB', 1);
4194                 END IF;
4195                 RAISE fnd_api.g_exc_error;
4196               END IF;
4197             END IF;
4198           END IF;  -- content_lpn_id populated
4199 
4200           -- Logic to determine whether JavaTM is to be called. We don't need to
4201           -- call the JavaTM if this batch in MMTT contained no records OR if
4202           -- contained only one record which was later removed by EXPLODE API
4203           -- because it had no contents.
4204           /** bug 3618385, don't use l_call_tm because it doesn't determine correctly whether JavaTM
4205               needs to be called.
4206           IF (NOT l_call_tm) THEN
4207             IF (insrowcnt > 0) THEN
4208               l_call_tm  := TRUE;
4209             END IF;
4210           END IF;
4211           **/
4212         END IF;   -- not same or failed transactions group
4213 
4214 
4215       EXCEPTION
4216         WHEN OTHERS THEN
4217           IF (l_debug = 1) THEN
4218             inv_log_util.TRACE(
4219               '***Error g_exec in c_mmtt loop trxtmpid=' || v_mmtt.transaction_temp_id || ',Sqlerrm:' || SUBSTR(SQLERRM, 1, 100)
4220             , 'INV_LPN_TRX_PUB'
4221             , 1
4222             );
4223           END IF;
4224 
4225           l_current_group_status  := fnd_api.g_ret_sts_error;
4226           l_error_code            := fnd_msg_pub.get(fnd_msg_pub.g_last, 'F');
4227           x_proc_msg              := fnd_msg_pub.get(fnd_msg_pub.g_previous, 'F');
4228           failedrowcnt            := failedrowcnt + 1;
4229 
4230           IF (l_debug = 1) THEN
4231             inv_log_util.TRACE('failedrowcnt=' || failedrowcnt || ' msg=' || x_proc_msg, 'INV_LPN_TRX_PUB', 1);
4232           END IF;
4233 
4234           ROLLBACK TO group_savepoint;
4235 
4236           IF fnd_api.to_boolean(p_atomic) THEN
4237 	     --Set message in MMTT line based in header id
4238 	     -- Bug 3804314: Changed the where clause to transaction_header_id
4239             BEGIN
4240               -- Update MMTT records with error message and set status to error
4241               UPDATE mtl_material_transactions_temp
4242                  SET ERROR_CODE = l_error_code
4243                    , error_explanation = x_proc_msg
4244                    , process_flag = 'E'
4245                    , lock_flag = 'N'
4246                WHERE transaction_header_id = p_trx_hdr_id;
4247             EXCEPTION
4248               WHEN OTHERS THEN
4249                 IF (l_debug = 1) THEN
4250                   inv_log_util.TRACE('Error in updating MMTT for trxhdrid=' || p_trx_hdr_id, 1);
4251                 END IF;
4252             END;
4253 
4254             -- Batch Failed exit from loop
4255             EXIT;
4256           ELSIF(v_mmtt.transaction_mode = inv_txn_manager_pub.proc_mode_mti) THEN
4257             BEGIN
4258               IF (v_mmtt.transaction_batch_id IS NOT NULL) THEN
4259                 -- This record was part of a batch and it originated from MTI
4260                 -- Update MTI records with error message and set status to error
4261                 -- and delete all rows belonging to this group from MMTT/MTLT/MSNT
4262                 UPDATE mtl_transactions_interface
4263                    SET ERROR_CODE = substrb(l_error_code,1,240)/*added substrb for 3632722*/
4264                      , error_explanation = substrb(x_proc_msg,1,240)/*added substrb for 3632722*/
4265                      , process_flag = 3
4266                      , lock_flag = 2
4267                  WHERE transaction_header_id = p_trx_hdr_id -- Bug 5748351
4268                  and transaction_batch_id = v_mmtt.transaction_batch_id;
4269 
4270                 l_num_ret_rows  := SQL%ROWCOUNT;
4271 
4272                 IF (l_debug = 1) THEN
4273                   inv_log_util.TRACE('Updated error msg for ' || l_num_ret_rows || ' rows form MTI with txnbatchid='
4274                     || v_mmtt.transaction_batch_id, 1);
4275                 END IF;
4276 
4277                 -- Remove from MSNT rows with same MMTT transaction_temp_id
4278                 DELETE FROM mtl_serial_numbers_temp
4279                       WHERE transaction_temp_id IN(SELECT transaction_temp_id
4280                                                      FROM mtl_material_transactions_temp
4281                                                     WHERE transaction_header_id = p_trx_hdr_id -- Bug 5748351
4282                                                     AND transaction_batch_id = v_mmtt.transaction_batch_id);
4283 
4284                 -- Remove from MSNT rows with same MTNT transaction_temp_id
4285                 DELETE FROM mtl_serial_numbers_temp
4286                       WHERE transaction_temp_id IN(SELECT serial_transaction_temp_id
4287                                                      FROM mtl_transaction_lots_temp
4288                                                     WHERE transaction_temp_id IN(SELECT transaction_temp_id
4289                                                                                    FROM mtl_material_transactions_temp
4290                                                                                   WHERE transaction_header_id = p_trx_hdr_id -- Bug 5748351
4291                                                                                   AND transaction_batch_id = v_mmtt.transaction_batch_id));
4292 
4293                 -- Remove from MTLT rows with same MMTT transaction_temp_id
4294                 DELETE FROM mtl_transaction_lots_temp
4295                       WHERE transaction_temp_id IN(SELECT transaction_temp_id
4296                                                      FROM mtl_material_transactions_temp
4297                                                     WHERE transaction_header_id = p_trx_hdr_id -- Bug 5748351
4298                                                     AND transaction_batch_id = v_mmtt.transaction_batch_id);
4299 
4300                 -- Remove from MMTT rows with same transaction group id
4301                 DELETE FROM mtl_material_transactions_temp
4302                 WHERE transaction_header_id = p_trx_hdr_id -- Bug 5748351
4303                 AND transaction_batch_id = v_mmtt.transaction_batch_id;
4304 
4305               ELSE   -- IF v_mmtt.transaction_batch_id IS NULL
4306                 -- This record was not part of a batch and it originated from MTI
4307                 -- Update MTI record with error message and set status to error
4308                 -- and delete the corresponding record from MMTT/MTLT/MSNT
4309                 UPDATE mtl_transactions_interface
4310                    SET ERROR_CODE = substrb(l_error_code,1,240)/*added substrb for 3632722*/
4311                      , error_explanation = substrb(x_proc_msg,1,240)/*added substrb for 3632722*/
4312                      , process_flag = 3
4313                      , lock_flag = 2
4314                  WHERE transaction_interface_id = v_mmtt.transaction_temp_id;
4315 
4316                 l_num_ret_rows  := SQL%ROWCOUNT;
4317 
4318                 IF (l_debug = 1) THEN
4319                   inv_log_util.TRACE('Updated error msg for ' || l_num_ret_rows || ' rows form MTI with txntempid='
4320                     || v_mmtt.transaction_temp_id, 1);
4321                 END IF;
4322 
4323                 -- Remove from MSNT rows with same MMTT transaction_temp_id
4324                 DELETE FROM mtl_serial_numbers_temp
4325                       WHERE transaction_temp_id = v_mmtt.transaction_temp_id;
4326 
4327                 -- Remove from MSNT rows with same MTNT transaction_temp_id
4328                 DELETE FROM mtl_serial_numbers_temp
4329                       WHERE transaction_temp_id IN(SELECT serial_transaction_temp_id
4330                                                      FROM mtl_transaction_lots_temp
4331                                                     WHERE transaction_temp_id = v_mmtt.transaction_temp_id);
4332 
4333                 -- Remove from MTLT rows with same MMTT transaction_temp_id
4334                 DELETE FROM mtl_transaction_lots_temp
4335                       WHERE transaction_temp_id = v_mmtt.transaction_temp_id;
4336 
4337                 -- Remove from MMTT rows with same transaction group id
4338                 DELETE FROM mtl_material_transactions_temp
4339                       WHERE transaction_temp_id = v_mmtt.transaction_temp_id;
4340               END IF;
4341             EXCEPTION
4342               WHEN OTHERS THEN
4343                 IF (l_debug = 1) THEN
4344                   inv_log_util.TRACE('Error in updating MTI or deleting MMTT for trxbatchid=' || v_mmtt.transaction_batch_id, 1);
4345                 END IF;
4346             END;
4347           ELSE   -- record originated in MMTT
4348             IF (v_mmtt.transaction_batch_id IS NOT NULL) THEN
4349               -- This record belongs to a batch. Fail all records of this batch
4350               -- stamp error-explanation only on the row that caused error
4351               UPDATE mtl_material_transactions_temp
4352                  SET ERROR_CODE = l_error_code
4353                    , error_explanation = x_proc_msg
4354                    , process_flag = 'E'
4355                    , lock_flag = 'N'
4356                WHERE transaction_temp_id = v_mmtt.transaction_temp_id;
4357 
4358               -- stamp error-code on all records of the batch
4359               UPDATE mtl_material_transactions_temp
4360                  SET ERROR_CODE = l_error_code
4361                    , process_flag = 'E'
4362                    , lock_flag = 'N'
4363                WHERE transaction_header_id = p_trx_hdr_id -- Bug 5748351
4364                AND transaction_batch_id = v_mmtt.transaction_batch_id;
4365             ELSE
4366               -- This record does not belong to a batch.
4367                -- Update MMTT records with error message and set status to error
4368               UPDATE mtl_material_transactions_temp
4369                  SET ERROR_CODE = l_error_code
4370                    , error_explanation = x_proc_msg
4371                    , process_flag = 'E'
4372                    , lock_flag = 'N'
4373                WHERE transaction_temp_id = v_mmtt.transaction_temp_id;
4374             END IF;
4375           END IF;
4376       END;   -- WHEN OTHERS THEN
4377 
4378       -- HVERDDIN ERES START
4379       tbl_index  := tbl_index + 1;
4380     -- HVERDDIN ERES END
4381       --Jalaj Srivastava Bug 5138445
4382       --update secondary transaction quantity with the sign of transaction quantity
4383       l_secondary_txn_quantity := v_mmtt.secondary_transaction_quantity;
4384       IF    (v_mmtt.secondary_transaction_quantity IS NOT NULL)
4385         AND (v_mmtt.secondary_transaction_quantity <> 0)
4386         AND (sign(v_mmtt.secondary_transaction_quantity) <> sign(v_mmtt.transaction_quantity))
4387         --Bug# 5453879 - if trx qty is 0 leave the sec qty as it is.
4388         AND (v_mmtt.transaction_quantity <> 0)  THEN
4389         l_secondary_txn_quantity := sign(v_mmtt.transaction_quantity) * abs(v_mmtt.secondary_transaction_quantity);
4390         update mtl_material_transactions_temp
4391         set    secondary_transaction_quantity = l_secondary_txn_quantity
4392         where  transaction_temp_id = v_mmtt.transaction_temp_id;
4393       END IF;
4394       IF (l_debug = 1) THEN
4395         inv_log_util.trace('before calling java TM transaction quantity='||v_mmtt.transaction_quantity, 'INV_LPN_TRX_PUB',9);
4396         inv_log_util.trace('before calling java TM secondary transaction quantity='||l_secondary_txn_quantity, 'INV_LPN_TRX_PUB',9);
4397         inv_log_util.trace('before calling java TM primary quantity='||v_mmtt.primary_quantity, 'INV_LPN_TRX_PUB',9);
4398       END IF;
4399     END LOOP;
4400 
4401     --************* Close cursor loop into MMTT  *************
4402     IF (l_debug = 1) THEN
4403       inv_log_util.TRACE('Done with MMTT cursor insrowcnt=' || insrowcnt || ' failedrowcnt=' || failedrowcnt, 'INV_LPN_TRX_PUB', 9);
4404     END IF;
4405 
4406     -- Bug 3618385
4407     -- For a transaction_header_id, when there's only one batch and a record is failed in the batch
4408     -- the whole batch should fail and should not call the java TM, but l_call_tm is not set probably
4409     -- to check if we need to call the java TM. We'll call the java TM only if there's any record
4410     -- exist for the transaction_header_id in MMTT.
4411     -- Bug 3804314: Included process_flag = 'Y' as the rows in MMTT are not
4412     -- deleted for transactions coming thorugh MMTT. We will have to check
4413     -- by process code also. Otherwise, we will still call the TM.
4414     BEGIN
4415        SELECT COUNT(1)
4416 	 INTO   l_process
4417 	 FROM   mtl_material_transactions_temp
4418 	 WHERE  transaction_header_id = p_trx_hdr_id
4419 	 AND process_flag = 'Y'
4420 	 AND    ROWNUM < 2;
4421        IF (l_debug = 1) THEN
4422           inv_log_util.TRACE('l_process: ' || l_process, 'INV_LPN_TRX_PUB', 9);
4423        END IF;
4424     EXCEPTION
4425        WHEN NO_DATA_FOUND THEN
4426             IF (l_debug = 1) THEN
4427                inv_log_util.TRACE('No records in MMTT for trx_header_id: ' || p_trx_hdr_id || ' ,will not call JAVATM', 'INV_LPN_TRX_PUB', 9);
4428             END IF;
4429             l_process := 0;
4430     END;
4431 
4432 
4433     -- All preprocessing done. Call the Inventory Transactions Manager
4434     IF (fnd_api.to_boolean(l_process_trx))
4435       AND (l_process <> 0) THEN
4436       -- AND(l_call_tm) THEN
4437       -- Should all the rows in MMTT with the specified TrxHdrId be considered
4438       -- as one unit
4439       IF fnd_api.to_boolean(p_atomic) THEN
4440         l_atomic  := 1;
4441       END IF;
4442 
4443       IF (l_debug = 1) THEN
4444         inv_log_util.TRACE('***Calling TM **', 'INV_LPN_TRX_PUB', 9);
4445       END IF;
4446 
4447       retval  := inv_trx_mgr.process_trx_batch(p_trx_hdr_id, l_commit, l_atomic, NVL(p_business_flow_code, 0), ret_msgdata);
4448 
4449       IF (l_debug = 1) THEN
4450         inv_log_util.TRACE('retval=' || retval || ', regmsg=' || ret_msgdata, 'INV_LPN_TRX_PUB', 9);
4451       END IF;
4452       --------------------
4453       -- Bug no 3540288
4454       -- Adding call to clear picked quantity.
4455       --------------------
4456       IF (l_debug = 1) THEN
4457           inv_log_util.trace('calling API to clear the picked quantity', 'INV_LPN_TRX_PUB',9);
4458       END IF;
4459       BEGIN
4460           inv_transfer_order_pvt.clear_picked_quantity;
4461       END;
4462       IF (l_debug = 1) THEN
4463           inv_log_util.trace('API call to clear picked quantity successful', 'INV_LPN_TRX_PUB',9);
4464       END IF;
4465     END IF;
4466 
4467     IF (retval <> 0) THEN
4468       x_proc_msg  := ret_msgdata;
4469 
4470       IF (failedrowcnt > 0) THEN
4471         RETURN -1 * failedrowcnt;
4472       ELSE
4473         RETURN -1;
4474       END IF;
4475     END IF;
4476 
4477     -- If cartonization was called make call to print
4478     -- cartonzation flow labels
4479     IF (l_is_cartonization = 1) THEN
4480       inv_label.print_label(
4481         x_return_status              => ret_status
4482       , x_msg_count                  => ret_msgcnt
4483       , x_msg_data                   => ret_msgdata
4484       , x_label_status               => label_status
4485       , p_api_version                => 1.0
4486       , p_print_mode                 => 1
4487       , p_business_flow_code         => 22
4488       , p_transaction_id             => wms_cartnzn_wrap.get_lpns_generated_tb
4489       );
4490 
4491       IF (ret_status <> fnd_api.g_ret_sts_success) THEN
4492         IF (l_debug = 1) THEN
4493           inv_log_util.TRACE('**Error Label Printing :' || ret_status, 'INV_LPN_TRX_PUB', 1);
4494         END IF;
4495       END IF;
4496     END IF;
4497 
4498     -- If any of the rows in MMTT fail return number of failed rows.
4499     IF (failedrowcnt > 0) THEN
4500       x_proc_msg  := fnd_msg_pub.get(fnd_msg_pub.g_last, 'F');
4501       RETURN -1 * failedrowcnt;
4502     END IF;
4503 
4504     IF fnd_api.to_boolean(p_commit) THEN
4505       COMMIT WORK;
4506     END IF;
4507 
4508     --
4509     -- HVERDDIN START OF ERES CHANGES
4510     --
4511     IF l_eres_enabled <> 'N' THEN
4512       -- ERES is Enabled, Verify if Transaction type is supported
4513       IF l_eres_tbl.COUNT > 0 THEN
4514         IF (l_debug = 1) THEN
4515           inv_log_util.TRACE('TBL COUNT =' || l_eres_tbl.COUNT, 'INV_LPN_TRX_PUB', 9);
4516         END IF;
4517 
4518         FOR tbl_index IN 1 .. l_eres_tbl.COUNT LOOP
4519           IF (l_debug = 1) THEN
4520             inv_log_util.TRACE('counter =' || tbl_index, 'INV_LPN_TRX_PUB', 9);
4521           END IF;
4522 
4523           IF (
4524               trans_eres_enabled(
4525                 p_trans_action_id            => l_eres_tbl(tbl_index).transaction_action_id
4526               , p_trans_source_type_id       => l_eres_tbl(tbl_index).transaction_source_type_id
4527               )
4528              ) THEN
4529             IF (l_debug = 1) THEN
4530               inv_log_util.TRACE('This is an ERES enabled Event', 'INV_LPN_TRX_PUB', 1);
4531               inv_log_util.TRACE(' EVENT_NAME => ' || g_eres_event_name, 'INV_LPN_TRX_PUB', 1);
4532               inv_log_util.TRACE(' EVENT_ID => ' || l_eres_tbl(tbl_index).event_id, 'INV_LPN_TRX_PUB', 1);
4533             END IF;
4534 
4535             -- Get Erecord Id
4536             qa_edr_standard.get_erecord_id(
4537               p_api_version                => 1.0
4538             , p_init_msg_list              => 'F'
4539             , p_event_name                 => g_eres_event_name
4540             , p_event_key                  => l_eres_tbl(tbl_index).event_id
4541             , x_return_status              => ret_status
4542             , x_msg_count                  => ret_msgcnt
4543             , x_msg_data                   => ret_msgdata
4544             , x_erecord_id                 => l_erecord_id
4545             );
4546 
4547             IF (l_debug = 1) THEN
4548               inv_log_util.TRACE('Status of Get_Erecord_Id => ' || ret_status, 'INV_LPN_TRX_PUB', 1);
4549               inv_log_util.TRACE(' Return Message => ' || ret_status, 'INV_LPN_TRX_PUB', 1);
4550               inv_log_util.TRACE('ERECORD  => ' || ret_status, 'INV_LPN_TRX_PUB', 1);
4551             END IF;
4552 
4553             IF (ret_status <> fnd_api.g_ret_sts_success) THEN
4554               IF (l_debug = 1) THEN
4555                 inv_log_util.TRACE(' ERROR generated from getErecordIdAPI:' || ret_msgdata, 'INV_LPN_TRX_PUB', 1);
4556               END IF;
4557 
4558               fnd_message.set_name('INV', 'INV_ERECORD_INVALID');
4559               fnd_msg_pub.ADD;
4560               RAISE fnd_api.g_exc_error;
4561             END IF;
4562 
4563             /* If an eRecord exists, then acknowledge it
4564             ===========================================*/
4565             IF l_erecord_id IS NOT NULL THEN
4566               qa_edr_standard.send_ackn(
4567                 p_api_version                => 1.0
4568               , p_init_msg_list              => 'F'
4569               , p_event_name                 => g_eres_event_name
4570               , p_event_key                  => l_eres_tbl(tbl_index).event_id
4571               , p_erecord_id                 => l_erecord_id
4572               , p_trans_status               => l_trans_status
4573               , p_ackn_by                    => 'Inventory Transaction Manager'
4574               , p_ackn_note                  => 'SERVER SIDE'
4575               , p_autonomous_commit          => 'F'
4576               , x_return_status              => ret_status
4577               , x_msg_count                  => ret_msgcnt
4578               , x_msg_data                   => ret_msgdata
4579               );
4580 
4581               IF (ret_status <> fnd_api.g_ret_sts_success) THEN
4582                 IF (l_debug = 1) THEN
4583                   inv_log_util.TRACE(' Error from Send ERES ACKNOWLEDGE API:' || ret_msgdata, 'INV_LPN_TRX_PUB', 1);
4584                 END IF;
4585 
4586                 fnd_message.set_name('INV', 'INV_ERECORD_INVALID');
4587                 fnd_msg_pub.ADD;
4588                 RAISE fnd_api.g_exc_error;
4589               END IF;
4590             END IF;   /* IF ERECORD ID not NULL */
4591           END IF;   /* IF TRANS NOT ERES SUPPORTED */
4592         END LOOP;   /* LOOP THROUGH ALL ERECORDS in TBL*/
4593       END IF;   /* IF ERES TBL has more than 1 row*/
4594     END IF;   /* IF ERES NOT SUPPORTED */
4595 
4596     /* Jalaj Srivastava
4597        Raise workflow event for OPM-QM */
4598     FOR c1_rec IN cur_get_txn_for_opm_qm
4599     LOOP
4600       wf_event.raise2( p_event_name => 'oracle.apps.gmi.inventory.created'
4601                       ,p_event_key  =>  c1_rec.transaction_id);
4602       IF c1_rec.transaction_source_type_id in ( inv_globals.g_sourcetype_purchaseorder
4603                                                ,inv_globals.g_sourcetype_rma
4604                                                ,inv_globals.g_sourcetype_intreq) THEN
4605          /* Bug 5361705 - Raise the opm qm quality event for every distinct lot */
4606          FOR c2_rec IN cr_get_distinct_lots(c1_rec.transaction_id)
4607          LOOP
4608 
4609             wf_event.raise2( p_event_name => 'oracle.apps.gmi.inv.po.receipt'
4610                             ,p_event_key  =>  to_char(c2_rec.product_transaction_id)||'-'||to_Char(c2_rec.gen_object_id));
4611          END LOOP;
4612 
4613       END IF;
4614     END LOOP;
4615     --
4616     -- HVERDDIN END OF ERES CHANGES
4617     --
4618 
4619     fnd_message.set_name('WMS', 'WMS_TXN_SUCCESS');
4620     fnd_msg_pub.ADD;
4621     x_proc_msg       := fnd_msg_pub.get(fnd_msg_pub.g_last, 'F');
4622     RETURN 0;
4623   EXCEPTION
4624     WHEN OTHERS THEN
4625       -- rollback to the savepoint
4626       IF (l_debug = 1) THEN
4627         inv_log_util.TRACE('ERROR : Code' || SQLCODE || ',Msg:' || SUBSTR(SQLERRM, 1, 100), 'INV_LPN_TRX_PUB', 1);
4628       END IF;
4629 
4630       ROLLBACK TO process_lpn_trx;
4631       --bug 2894323 fix made changes to return all the messages in the
4632       --stack in x_proc_msg out variable
4633       fnd_msg_pub.count_and_get(p_encoded => fnd_api.g_false, p_count => ret_msgcnt, p_data => x_proc_msg);
4634 
4635       IF ret_msgcnt IS NULL THEN
4636         ret_msgcnt  := 0;
4637       END IF;
4638 
4639       FOR x IN 1 .. ret_msgcnt LOOP
4640         ret_msgdata  := SUBSTR(fnd_msg_pub.get(ret_msgcnt - x + 1, 'F'), 0, 200);
4641 
4642         IF (l_debug = 1) THEN
4643           inv_log_util.TRACE(x || ':' || ret_msgdata, 'INV_LPN_TRX_PUB', 1);
4644         END IF;
4645 
4646         IF x <> 1 THEN
4647           ret_msgdata  := ' | ' || ret_msgdata;
4648         END IF;
4649 
4650         l_err_msg    := SUBSTR(l_err_msg || ret_msgdata, 1, 2000);
4651       END LOOP;
4652 
4653       x_proc_msg    := SUBSTR(l_err_msg, 1, 170);
4654       -- Update MMTT with error_code
4655       fnd_message.set_name('INV', 'INV_INT_PROCCODE');
4656       fnd_msg_pub.ADD;
4657       l_error_code  := fnd_msg_pub.get(fnd_msg_pub.g_last, 'F');
4658 
4659       BEGIN
4660         SELECT 1
4661           INTO l_is_from_mti
4662           FROM DUAL
4663          WHERE EXISTS(SELECT 1
4664                         FROM mtl_material_transactions_temp
4665                        WHERE transaction_header_id = p_trx_hdr_id
4666                          AND transaction_mode = inv_txn_manager_pub.proc_mode_mti);
4667       EXCEPTION
4668         WHEN NO_DATA_FOUND THEN
4669           IF (l_debug = 1) THEN
4670             inv_log_util.TRACE('no record found in mmtt', 'INV_LPN_TRX_PUB', 1);
4671           END IF;
4672       END;
4673 
4674       IF (l_is_from_mti = 1) THEN
4675         -- record originated from MTI. Update all records in MTI for this batch
4676         -- Delete from MMTT, MTLT and MSNT. Records from MTI get copied to MMTT
4677         -- in tmpinsert() in INV_TXN_MANAGER_PUB
4678         UPDATE mtl_transactions_interface
4679            SET ERROR_CODE = substrb(l_error_code,1,240)/*added substrb for 3632722*/
4680              , error_explanation = substrb(x_proc_msg,1,240)/*added substrb for 3632722*/
4681              , process_flag = 3
4682              , lock_flag = 2
4683          WHERE transaction_header_id = p_trx_hdr_id;
4684 
4685         -- Remove from MSNT rows with same MMTT Transaction_header_id
4686         DELETE FROM mtl_serial_numbers_temp
4687               WHERE transaction_temp_id IN(SELECT transaction_temp_id
4688                                              FROM mtl_material_transactions_temp
4689                                             WHERE transaction_header_id = p_trx_hdr_id);
4690 
4691         -- Remove from MSNT rows with same MTNT Transaction_header_id
4692         DELETE FROM mtl_serial_numbers_temp
4693               WHERE transaction_temp_id IN(SELECT serial_transaction_temp_id
4694                                              FROM mtl_transaction_lots_temp
4695                                             WHERE transaction_temp_id IN(SELECT transaction_temp_id
4696                                                                            FROM mtl_material_transactions_temp
4697                                                                           WHERE transaction_header_id = p_trx_hdr_id));
4698 
4699         -- Remove from MTLT rows with same MMTT Transaction_header_id
4700         DELETE FROM mtl_transaction_lots_temp
4701               WHERE transaction_temp_id IN(SELECT transaction_temp_id
4702                                              FROM mtl_material_transactions_temp
4703                                             WHERE transaction_header_id = p_trx_hdr_id);
4704 
4705         -- Remove from MMTT rows with same Transaction_header_id
4706         DELETE FROM mtl_material_transactions_temp
4707               WHERE transaction_header_id = p_trx_hdr_id;
4708       ELSE
4709         UPDATE mtl_material_transactions_temp
4710            SET ERROR_CODE = l_error_code
4711              , error_explanation = x_proc_msg
4712              , process_flag = 'E'
4713              , lock_flag = 'N'
4714          WHERE transaction_header_id = p_trx_hdr_id;
4715       END IF;
4716 
4717       IF (l_debug = 1) THEN
4718         inv_log_util.TRACE('Exiting PROCESS_LPN_TRX with exception from_mti=' || l_is_from_mti || ' trx_hdr_id=' || p_trx_hdr_id
4719         , 'INV_LPN_TRX_PUB', 1);
4720       END IF;
4721 
4722       RETURN -1;
4723   END process_lpn_trx;
4724 
4725   /********************************************************************
4726    *
4727    * PROCESS_LPN_TRX_LINE
4728    * This procedure does all the LPN related processing on an MMTT record.
4729    * The logic in this API is based on the transaction_action and the contents
4730    * of columns LPN_ID, CONTENT_LPN_ID, and TRANSFER_LPN_ID
4731    *  LPN_ID : LPN from where item/other-LPN should be unpacked
4732    *  CONTENT_LPN : If the transaction is based on a whole LPN
4733    *  TRANSFER_LPN : LPN to which item/other-LPN is to be packed
4734    *******************************************************************/
4735   PROCEDURE process_lpn_trx_line(
4736     x_return_status              OUT NOCOPY    VARCHAR2
4737   , x_proc_msg                   OUT NOCOPY    VARCHAR2
4738   , p_transaction_temp_id        IN            NUMBER
4739   , p_business_flow_code         IN            NUMBER := NULL
4740   , p_transaction_source_type_id IN            NUMBER
4741   , p_transaction_action_id      IN            NUMBER
4742   , p_lpn_id                     IN            NUMBER := NULL
4743   , p_content_lpn_id             IN            NUMBER := NULL
4744   , p_transfer_lpn_id            IN            NUMBER := NULL
4745   , p_organization_id            IN            NUMBER
4746   , p_subinventory_code          IN            VARCHAR2
4747   , p_locator_id                 IN            NUMBER := NULL
4748   , p_transfer_organization      IN            NUMBER := NULL
4749   , p_transfer_subinventory      IN            VARCHAR2 := NULL
4750   , p_transfer_to_location       IN            NUMBER := NULL
4751   , p_primary_quantity           IN            NUMBER
4752   , p_primary_uom                IN            VARCHAR2 := NULL
4753   , p_transaction_quantity       IN            NUMBER
4754   , p_transaction_uom            IN            VARCHAR2
4755   , p_secondary_trx_quantity     IN            NUMBER := NULL
4756   , p_secondary_uom_code         IN            VARCHAR2 := NULL
4757   , p_inventory_item_id          IN            NUMBER
4758   , p_revision                   IN            VARCHAR2 := NULL
4759   , p_lot_number                 IN            VARCHAR2 := NULL
4760   , p_cost_group_id              IN            NUMBER
4761   , p_transfer_cost_group_id     IN            NUMBER := NULL
4762   , p_rcv_transaction_id         IN            NUMBER := NULL
4763   , p_shipment_number            IN            VARCHAR2 := NULL
4764   , p_transaction_source_id      IN            NUMBER := NULL
4765   , p_trx_source_line_id         IN            NUMBER := NULL
4766   , p_serial_control_code        IN            NUMBER := NULL
4767   , p_po_dest_expense            IN            NUMBER := NULL
4768   , p_manual_receipt_expense     IN            VARCHAR2 := NULL
4769   , p_source_transaction_id      IN            NUMBER := NULL
4770   ) IS
4771     l_api_name    CONSTANT VARCHAR2(30)          := 'Process_LPN_Trx_Line';
4772     l_api_version CONSTANT NUMBER                := 1.0;
4773     l_debug                NUMBER                := NVL(fnd_profile.VALUE('INV_DEBUG_TRACE'), 0);
4774     l_progress             VARCHAR2(500)         := '0';
4775     l_msgdata              VARCHAR2(1000);
4776     l_item_rec             inv_validate.item;
4777     v_lpn                  wms_container_pub.lpn;
4778     v_lpn_ctx              NUMBER;
4779     v_cnt_lpn_ctx          NUMBER;
4780     v_xfrlpn_ctx           NUMBER;
4781     v_autounpack           NUMBER                := 1;
4782     v_xfr_org              NUMBER;
4783     l_primary_quantity     NUMBER                := p_primary_quantity;
4784     l_transaction_quantity NUMBER                := p_transaction_quantity;
4785     l_secondary_trx_quantity NUMBER                := p_secondary_trx_quantity;  --INVCONV kkillams
4786     l_xfr_sub              VARCHAR2(64);
4787     l_xfr_loc              NUMBER;
4788     l_temp_num             NUMBER;
4789     l_rcv_interface_txn_id NUMBER;
4790     l_rcv_txn_type         VARCHAR2(64);
4791     label_status           VARCHAR2(512);
4792     l_system_task_type     NUMBER;   -- bug 2879208
4793     l_ret_bool             BOOLEAN;
4794     --Start of fix for Bug 4891916
4795     --Added the following variables to check for the profile value and
4796     --to pass the transaction_identifer to the label label printing api
4797     l_print_label   NUMBER := NVL(FND_PROFILE.VALUE('WMS_LABEL_FOR_CYCLE_COUNT'),2);
4798     l_transaction_identifier NUMBER := NULL ;
4799     --End of fix for Bug 4891916
4800 
4801     l_cyclpn_id NUMBER := NULL;  --Bug#6043776
4802     l_lpn_ctx_tmp            NUMBER ; --Bug 6007873
4803 
4804     --Bug 6374764
4805     l_sub  VARCHAR2(30);
4806     l_loc_id number;
4807   BEGIN
4808     IF (l_debug = 1) THEN
4809       inv_log_util.TRACE(
4810            'Call to PROCESS_LPN_TRX_LINE trxtmpid='
4811         || p_transaction_temp_id
4812         || ' flwcode='
4813         || p_business_flow_code
4814         || ' srctype='
4815         || p_transaction_source_type_id
4816         || ' actid='
4817         || p_transaction_action_id
4818       , 'INV_LPN_TRX_PUB'
4819       , 9
4820       );
4821       inv_log_util.TRACE(
4822            'lpn='
4823         || p_lpn_id
4824         || ' cntlpn='
4825         || p_content_lpn_id
4826         || ' xfrlpn='
4827         || p_transfer_lpn_id
4828         || ' orgid='
4829         || p_organization_id
4830         || ' sub='
4831         || p_subinventory_code
4832         || ' loc='
4833         || p_locator_id
4834       , 'INV_LPN_TRX_PUB'
4835       , 9
4836       );
4837       inv_log_util.TRACE(
4838            'xfrorg='
4839         || p_transfer_organization
4840         || ' xfrsub='
4841         || p_transfer_subinventory
4842         || ' xfrloc='
4843         || p_transfer_to_location
4844         || ' priqty='
4845         || p_primary_quantity
4846         || ' trxqty='
4847         || p_transaction_quantity
4848         || ' uom='
4849         || p_transaction_uom
4850       , 'INV_LPN_TRX_PUB'
4851       , 9
4852       );
4853       inv_log_util.TRACE(
4854            'item='
4855         || p_inventory_item_id
4856         || ' rev='
4857         || p_revision
4858         || ' lot='
4859         || p_lot_number
4860         || ' cg='
4861         || p_cost_group_id
4862         || ' rcvtrxid='
4863         || p_rcv_transaction_id
4864         || ' shipnum='
4865         || p_shipment_number
4866       , 'INV_LPN_TRX_PUB'
4867       , 9
4868       );
4869       inv_log_util.TRACE(
4870            'srcid='
4871         || p_transaction_source_id
4872         || ' slnid='
4873         || p_trx_source_line_id
4874         || ' qty2='
4875         || p_secondary_trx_quantity
4876         || ' uom2='
4877         || p_secondary_uom_code
4878         || ' podst='
4879         || p_po_dest_expense
4880         || ' rcptex='
4881         || p_manual_receipt_expense
4882         || ' strxid='
4883         || p_source_transaction_id
4884       , 'INV_LPN_TRX_PUB'
4885       , 9
4886       );
4887     END IF;
4888 
4889     x_return_status  := fnd_api.g_ret_sts_success;
4890 
4891     --SAVEPOINT PROCESS_LPN_TRX_LINE;
4892 
4893     -- Continue with this record ONLY if atleast one of the field
4894     -- lpn_id, content_lpn_id, transfer_lpn_id is NOT NULL
4895     IF NOT((p_lpn_id IS NULL)
4896            AND(p_content_lpn_id IS NULL)
4897            AND(p_transfer_lpn_id IS NULL)) THEN
4898       -- Retrieve item properties if not passed by TM: used by bug 3158847 as well
4899       IF (p_inventory_item_id > 0) THEN
4900         IF (p_primary_uom IS NULL
4901             OR p_serial_control_code IS NULL) THEN
4902           l_progress  := 'Calling INV_CACHE.Set_Item_Rec to get item values';
4903           l_ret_bool  := inv_cache.set_item_rec(p_organization_id => p_organization_id, p_item_id => p_inventory_item_id);
4904 
4905           IF (l_ret_bool) THEN
4906             l_progress  := 'Found item assigning it to l_item_rec';
4907             l_item_rec  := inv_cache.item_rec;
4908 
4909             IF (l_debug = 1) THEN
4910               inv_log_util.TRACE(
4911                 'Got item properties puom=' || l_item_rec.primary_uom_code || ' sctl=' || l_item_rec.serial_number_control_code
4912               , 'INV_LPN_TRX_PUB'
4913               , 9
4914               );
4915             END IF;
4916           ELSE   --failed to get item info
4917             l_progress  := 'Error calling INV_CACHE.Set_Item_Rec to get item info';
4918             RAISE fnd_api.g_exc_unexpected_error;
4919           END IF;
4920         ELSE   --Values already passed, assign to item rec type
4921           l_item_rec.organization_id             := p_organization_id;
4922           l_item_rec.inventory_item_id           := p_inventory_item_id;
4923           l_item_rec.primary_uom_code            := p_primary_uom;
4924           l_item_rec.serial_number_control_code  := p_serial_control_code;
4925           l_item_rec.secondary_uom_code          := p_secondary_uom_code; -- INVCONV kkillams
4926         END IF;
4927       END IF;
4928 
4929       -- If the transaction action is either Issue or Intransit Shipment, and
4930       -- the sign of quantity is not -ve, make it -ve.
4931       IF (
4932           (
4933            (p_transaction_action_id = inv_globals.g_action_issue)
4934            OR(p_transaction_action_id = inv_globals.g_action_intransitshipment)
4935            OR(p_transaction_action_id = inv_globals.g_action_intransitshipment)
4936            OR(p_transaction_action_id = inv_globals.g_action_intransitshipment)
4937            OR(p_transaction_action_id = inv_globals.g_action_intransitshipment)
4938           )
4939           AND(p_primary_quantity > 0)
4940          ) THEN
4941         l_primary_quantity      := -1 * p_primary_quantity;
4942         l_transaction_quantity  := -1 * p_transaction_quantity;
4943         l_secondary_trx_quantity := CASE WHEN l_secondary_trx_quantity IS NOT NULL THEN  -1 * l_secondary_trx_quantity
4944                                                                                     ELSE l_secondary_trx_quantity
4945                                                                                     END;  --INVCONV kkillams
4946       END IF;
4947 
4948       IF (
4949           (
4950            (p_transaction_action_id = inv_globals.g_action_subxfr)
4951            OR(p_transaction_action_id = inv_globals.g_action_orgxfr)
4952            OR(p_transaction_action_id = inv_globals.g_action_stgxfr)
4953            OR(p_transaction_action_id = inv_globals.g_action_costgroupxfr)
4954            OR(p_transaction_action_id = inv_globals.g_action_planxfr)
4955            OR(p_transaction_action_id = inv_globals.g_action_containerpack)
4956            OR(p_transaction_action_id = inv_globals.g_action_containerunpack)
4957            OR(p_transaction_action_id = inv_globals.g_action_containersplit)
4958           )
4959           AND(p_primary_quantity < 0)
4960          ) THEN
4961         l_primary_quantity      := -1 * p_primary_quantity;
4962         l_transaction_quantity  := -1 * p_transaction_quantity;
4963         l_secondary_trx_quantity := CASE WHEN l_secondary_trx_quantity IS NOT NULL THEN  -1 * l_secondary_trx_quantity
4964                                                                                     ELSE l_secondary_trx_quantity
4965                                                                                     END;  --INVCONV kkillams
4966       END IF;
4967 
4968       -- WMS Correction changes.
4969       -- MMTT will have a transaction_action_id of
4970       -- G_Action_DeliveryAdj for a source type of PO or RMA
4971       -- IDS: Logical Transactions for lot and serials - Opened up TM to
4972       --  handle logical receipts and logical corrections.
4973       IF (
4974           (
4975            p_transaction_action_id = inv_globals.g_action_deliveryadj
4976            AND(
4977                p_transaction_source_type_id = inv_globals.g_sourcetype_purchaseorder
4978                OR p_transaction_source_type_id = inv_globals.g_sourcetype_rma
4979               )
4980           )
4981           OR(
4982              p_transaction_action_id = inv_globals.g_action_logicaldeladj
4983              AND p_transaction_source_type_id = inv_globals.g_sourcetype_purchaseorder
4984             )
4985          ) THEN
4986         -- Corrections.  Need to retrieve interface_transaction_id and transaction_type
4987         BEGIN
4988           SELECT interface_transaction_id
4989                , transaction_type
4990             INTO l_rcv_interface_txn_id
4991                , l_rcv_txn_type
4992             FROM rcv_transactions
4993            WHERE transaction_id = p_rcv_transaction_id;
4994         EXCEPTION
4995           WHEN OTHERS THEN
4996             l_rcv_interface_txn_id  := NULL;
4997             l_rcv_txn_type          := NULL;
4998         END;
4999 
5000         IF (l_debug = 1) THEN
5001           inv_log_util.TRACE('** transaction_type=' || l_rcv_txn_type, 'INV_LPN_TRX_PUB', 9);
5002         END IF;
5003 
5004         IF (l_rcv_txn_type = 'CORRECT') THEN
5005           IF (l_primary_quantity < 0) THEN   -- -ve correction on delivery
5006             IF (l_debug = 1) THEN
5007               inv_log_util.TRACE('** -ve correction on delivery unpacking', 'INV_LPN_TRX_PUB', 9);
5008             END IF;
5009 
5010             -- Unpack items from lpn
5011             IF p_transfer_lpn_id IS NOT NULL THEN
5012               -- delivery could have been done as loose
5013               Call_Pack_Unpack (
5014                  p_tempid           => p_transaction_temp_id
5015                , p_content_lpn      => p_content_lpn_id
5016                , p_lpn              => p_transfer_lpn_id
5017                , p_item_rec         => l_item_rec
5018                , p_revision         => p_revision
5019                , p_primary_qty      => ABS(l_primary_quantity)
5020                , p_qty              => ABS(l_transaction_quantity)
5021                , p_uom              => p_transaction_uom
5022                , p_org_id           => p_organization_id
5023                , p_subinv           => p_subinventory_code
5024                , p_locator          => p_locator_id
5025                , p_operation        => g_unpack
5026                , p_cost_grp_id      => p_cost_group_id
5027                , p_trx_action       => p_transaction_action_id
5028                , p_source_header_id => NULL
5029                , p_source_name      => NULL
5030                , p_source_type_id   => NULL
5031                , p_sec_qty          => ABS(l_secondary_trx_quantity) --INVCONV kkillams
5032                , p_sec_uom          => p_secondary_uom_code --INVCONV kkillams
5033                , p_source_trx_id    => p_source_transaction_id
5034               );
5035             END IF;
5036 
5037             IF (l_debug = 1) THEN
5038               inv_log_util.TRACE('** -ve correction on delivery packing', 'INV_LPN_TRX_PUB', 9);
5039             END IF;
5040 
5041             -- Repack items into transfer lpn nulling cost group,sub and locator
5042             Call_Pack_Unpack (
5043               p_tempid           => p_transaction_temp_id
5044             , p_content_lpn      => p_content_lpn_id
5045             , p_lpn              => p_lpn_id
5046             , p_item_rec         => l_item_rec
5047             , p_revision         => p_revision
5048             , p_primary_qty      => ABS(l_primary_quantity)
5049             , p_qty              => ABS(l_transaction_quantity)
5050             , p_uom              => p_transaction_uom
5051             , p_org_id           => p_organization_id
5052             , p_subinv           => NULL
5053             , p_locator          => NULL
5054             , p_operation        => g_pack
5055             , p_cost_grp_id      => NULL
5056             , p_trx_action       => p_transaction_action_id
5057             , p_source_header_id => NULL
5058             , p_source_name      => NULL
5059             , p_source_type_id   => NULL
5060             , p_sec_qty          => ABS(l_secondary_trx_quantity) --INVCONV kkillams
5061             , p_sec_uom          => p_secondary_uom_code --INVCONV kkillams
5062             , p_source_trx_id    => p_source_transaction_id
5063             );
5064 
5065             -- Update the context of lpn to resides in receiving
5066             -- if it's a adj of whole lpn. lpn_id = transfer_lpn_id
5067             IF (p_lpn_id = p_transfer_lpn_id) THEN
5068               UPDATE wms_license_plate_numbers
5069                  SET lpn_context = wms_container_pub.lpn_context_rcv
5070                WHERE lpn_id = p_lpn_id;
5071             END IF;
5072           ELSE
5073             IF (l_debug = 1) THEN
5074               inv_log_util.TRACE('** +ve correction on delivery', 'INV_LPN_TRX_PUB', 9);
5075             END IF;
5076 
5077             -- Unpack items from lpn with null sub, locator and cost group
5078             Call_Pack_Unpack (
5079               p_tempid           => p_transaction_temp_id
5080             , p_content_lpn      => p_content_lpn_id
5081             , p_lpn              => p_lpn_id
5082             , p_item_rec         => l_item_rec
5083             , p_revision         => p_revision
5084             , p_primary_qty      => ABS(l_primary_quantity)
5085             , p_qty              => ABS(l_transaction_quantity)
5086             , p_uom              => p_transaction_uom
5087             , p_org_id           => p_organization_id
5088             , p_subinv           => NULL
5089             , p_locator          => NULL
5090             , p_operation        => g_unpack
5091             , p_cost_grp_id      => NULL
5092             , p_trx_action       => p_transaction_action_id
5093             , p_source_header_id => NULL
5094             , p_source_name      => NULL
5095             , p_source_type_id   => NULL
5096             , p_sec_qty          => ABS(l_secondary_trx_quantity) --INVCONV kkillams
5097             , p_sec_uom          => p_secondary_uom_code --INVCONV kkillams
5098             , p_source_trx_id    => p_source_transaction_id
5099             );
5100 
5101             IF (l_debug = 1) THEN
5102               inv_log_util.TRACE('** +ve correction on delivery packing', 'INV_LPN_TRX_PUB', 9);
5103             END IF;
5104 
5105             IF p_transfer_lpn_id IS NOT NULL THEN
5106               -- Repack items into transfer lpn if it is not null
5107               Call_Pack_Unpack (
5108                 p_tempid           => p_transaction_temp_id
5109               , p_content_lpn      => p_content_lpn_id
5110               , p_lpn              => p_transfer_lpn_id
5111               , p_item_rec         => l_item_rec
5112               , p_revision         => p_revision
5113               , p_primary_qty      => ABS(l_primary_quantity)
5114               , p_qty              => ABS(l_transaction_quantity)
5115               , p_uom              => p_transaction_uom
5116               , p_org_id           => p_organization_id
5117               , p_subinv           => p_subinventory_code
5118               , p_locator          => p_locator_id
5119               , p_operation        => g_pack
5120               , p_cost_grp_id      => p_cost_group_id
5121               , p_trx_action       => p_transaction_action_id
5122               , p_source_header_id => NULL
5123               , p_source_name      => NULL
5124               , p_source_type_id   => NULL
5125               , p_sec_qty          => abs(l_secondary_trx_quantity) --INVCONV kkillams.
5126               , p_sec_uom          => p_secondary_uom_code --INVCONV kkillams
5127               , p_source_trx_id    => p_source_transaction_id
5128               );
5129             END IF;
5130           END IF;
5131         END IF;
5132       END IF;
5133 
5134       /***** ISSUE/LotSplit/LotMerge/Cycle,PhysicalCnt/IntransitShipment trx  ****/
5135       IF (p_transaction_action_id = inv_globals.g_action_issue)
5136          OR((p_transaction_action_id = inv_globals.g_action_inv_lot_split)
5137             AND(l_primary_quantity < 0))
5138          OR((p_transaction_action_id = inv_globals.g_action_inv_lot_translate)
5139             AND(l_primary_quantity < 0))
5140          OR((p_transaction_action_id = inv_globals.g_action_inv_lot_merge)
5141             AND(l_primary_quantity < 0))
5142          OR(p_transaction_action_id = inv_globals.g_action_intransitshipment)
5143          OR((p_transaction_action_id = inv_globals.g_action_cyclecountadj)
5144             AND l_primary_quantity < 0)
5145          OR((p_transaction_action_id = inv_globals.g_action_physicalcountadj)
5146             AND l_primary_quantity < 0) THEN
5147         IF (l_debug = 1) THEN
5148           inv_log_util.TRACE(' Action = Issue, lpn=' || p_lpn_id || ',qty=' || l_primary_quantity, 'INV_LPN_TRX_PUB', 9);
5149         END IF;
5150 
5151         IF (p_content_lpn_id IS NOT NULL) THEN
5152           -- This is an Issue of LPN from system
5153           IF (l_debug = 1) THEN
5154             inv_log_util.TRACE('** content_lpn_id Not null:' || p_content_lpn_id, 'INV_LPN_TRX_PUB', 9);
5155           END IF;
5156 
5157           v_lpn.lpn_id             := p_content_lpn_id;
5158 
5159           IF (
5160               p_transaction_action_id = inv_globals.g_action_intransitshipment
5161               OR(p_transaction_action_id = inv_globals.g_action_issue
5162                  AND p_transaction_source_type_id = inv_globals.g_sourcetype_intorder)
5163              ) THEN
5164             IF (p_po_dest_expense = 1 AND p_manual_receipt_expense <> 'Y') THEN
5165             	-- Bug4663000 instead of unpacking expense contents.
5166             	-- Change context of LPN to issued out of stores, but only if the
5167             	-- context for the LPN is not already "intransit".  Assuming that if
5168             	-- LPN is in intransit state, that the LPN has non expense items in them
5169             	IF ( v_cnt_lpn_ctx IS NULL ) THEN
5170                 SELECT lpn_context
5171                   INTO v_cnt_lpn_ctx
5172                   FROM wms_license_plate_numbers
5173                  WHERE lpn_id = p_content_lpn_id;
5174 
5175                 IF (l_debug = 1) THEN
5176                   inv_log_util.TRACE('Got v_cnt_lpn_ctx='||v_cnt_lpn_ctx, 'INV_LPN_TRX_PUB', 9);
5177                 END IF;
5178               END IF;
5179 
5180             	IF ( v_cnt_lpn_ctx <> wms_container_pub.lpn_context_intransit ) THEN
5181             	  v_lpn.lpn_context := wms_container_pub.lpn_context_stores;
5182             	END IF;
5183             ELSE
5184               -- For manual rcpt, both Inventory and Expense Internal orders lpns context
5185               -- should be marked as intransit for recieving
5186               v_lpn.lpn_context := wms_container_pub.lpn_context_intransit;
5187             END IF;
5188           ELSIF(p_transaction_action_id = inv_globals.g_action_issue
5189                 AND p_transaction_source_type_id = inv_globals.g_sourcetype_wip) THEN
5190             -- For WIP Issues the LPN set to be defined but not used
5191             v_lpn.lpn_context  := wms_container_pub.lpn_context_pregenerated;
5192 
5193             IF (l_debug = 1) THEN
5194               inv_log_util.TRACE('WIP Issue, unpack all items', 'INV_LPN_TRX_PUB', 9);
5195             END IF;
5196 
5197             -- LPN must be unpacked into the current sub
5198             WMS_Container_PVT.PackUnpack_Container(
5199               p_api_version           => 1.0
5200             , p_init_msg_list         => fnd_api.g_false
5201             , p_commit                => fnd_api.g_false
5202             , p_validation_level      => fnd_api.g_valid_level_none
5203             , x_return_status         => ret_status
5204             , x_msg_count             => ret_msgcnt
5205             , x_msg_data              => ret_msgdata
5206             , p_caller                => 'INV_TRNSACTION'
5207             , p_lpn_id                => p_content_lpn_id
5208             , p_organization_id       => p_organization_id
5209             , p_subinventory          => p_subinventory_code
5210             , p_locator_id            => p_locator_id
5211             , p_operation             => g_unpack_all
5212             , p_source_transaction_id => p_source_transaction_id );
5213 
5214             IF (ret_status <> fnd_api.g_ret_sts_success) THEN
5215               IF (l_debug = 1) THEN
5216                 inv_log_util.TRACE('**Error Full unpack for wip issue:' || ret_status, 'INV_LPN_TRX_PUB', 1);
5217               END IF;
5218 
5219               fnd_message.set_name('INV', 'INV_PACKUNPACK_FAILURE');
5220               fnd_msg_pub.ADD;
5221               RAISE fnd_api.g_exc_error;
5222             END IF;
5223           ELSE
5224             v_lpn.lpn_context  := wms_container_pub.lpn_context_stores;   --ISSUED OUT
5225           END IF;
5226 
5227           -- For an Intransit Shipment, update the LPN with the shipmentNumber
5228           -- so that this LPN can be tracked during the corresponding receipt
5229           IF (p_transaction_action_id = inv_globals.g_action_intransitshipment) THEN
5230             v_lpn.source_name  := p_shipment_number;
5231           END IF;
5232 
5233           inv_log_util.TRACE('** 3361969 setting lpn org id to : ' || p_organization_id, 'INV_LPN_TRX_PUB', 9);
5234           v_lpn.organization_id    := p_organization_id;
5235           v_lpn.subinventory_code  := NULL;
5236           v_lpn.locator_id         := NULL;
5237           v_lpn.inventory_item_id  := NULL;
5238           update_lpn_status(v_lpn);
5239 
5240           IF (l_debug = 1) THEN
5241             inv_log_util.TRACE('*** Issued out LPN :' || p_content_lpn_id, 'INV_LPN_TRX_PUB', 9);
5242           END IF;
5243         ELSIF(
5244               p_transaction_action_id = inv_globals.g_action_issue
5245               AND(
5246                   p_transaction_source_type_id = inv_globals.g_sourcetype_purchaseorder
5247                   OR p_transaction_source_type_id = inv_globals.g_sourcetype_rma
5248                  )
5249              ) THEN
5250           -- Return or corrections.  Need to retrieve interface_transaction_id and transaction_type
5251           BEGIN
5252             SELECT interface_transaction_id
5253                  , transaction_type
5254               INTO l_rcv_interface_txn_id
5255                  , l_rcv_txn_type
5256               FROM (SELECT   interface_transaction_id
5257                            , transaction_type
5258                            , creation_date
5259                         FROM rcv_transactions
5260                        WHERE interface_transaction_id = (SELECT interface_transaction_id
5261                                                            FROM rcv_transactions
5262                                                           WHERE transaction_id = p_rcv_transaction_id)
5263                     ORDER BY transaction_id DESC)
5264              WHERE ROWNUM < 2;
5265           --SELECT interface_transaction_id, transaction_type
5266           --INTO l_rcv_interface_txn_id, l_rcv_txn_type
5267           --FROM rcv_transactions
5268           --WHERE transaction_id = p_rcv_transaction_id;
5269           EXCEPTION
5270             WHEN OTHERS THEN
5271               l_rcv_interface_txn_id  := NULL;
5272               l_rcv_txn_type          := NULL;
5273           END;
5274 
5275           IF (l_rcv_txn_type = 'RETURN TO RECEIVING') THEN
5276 	      --6374764:While returning to receiving, stamp lpn with the sub and loc
5277 	    --stamped on the corresponding RT record.
5278 	    --p_rcv_transaction_id corresponds to the Return transaction.
5279     	    select subinventory,locator_id
5280 	    into l_sub,l_loc_id
5281 	    from rcv_transactions
5282 	    where transaction_id = p_rcv_transaction_id;
5283 
5284             IF (p_lpn_id IS NULL
5285                 AND p_transfer_lpn_id IS NOT NULL) THEN
5286               -- Change context of transfer lpn to recieving
5287               UPDATE wms_license_plate_numbers
5288                  SET lpn_context = wms_container_pub.lpn_context_rcv
5289                    , subinventory_code = nvl(l_sub,subinventory_code) --6374764
5290                    , locator_id = nvl(l_loc_id,locator_id)	      --6374764
5291                WHERE lpn_id = p_transfer_lpn_id;
5292 
5293               -- Pack items into transfer lpn
5294               Call_Pack_Unpack (
5295                 p_tempid           => p_transaction_temp_id
5296               , p_content_lpn      => p_content_lpn_id
5297               , p_lpn              => p_transfer_lpn_id
5298               , p_item_rec         => l_item_rec
5299               , p_revision         => p_revision
5300               , p_primary_qty      => ABS(l_primary_quantity)
5301               , p_qty              => ABS(l_transaction_quantity)
5302               , p_uom              => p_transaction_uom
5303               , p_org_id           => p_organization_id
5304               , p_subinv           => NULL
5305               , p_locator          => NULL
5306               , p_operation        => g_pack
5307               , p_cost_grp_id      => NULL
5308               , p_trx_action       => p_transaction_action_id
5309               , p_source_header_id => NULL
5310               , p_source_name      => NULL
5311               , p_source_type_id   => NULL
5312               , p_sec_qty          => abs(l_secondary_trx_quantity) --INVCONV kkillams
5313               , p_sec_uom          => p_secondary_uom_code --INVCONV kkillams
5314               , p_source_trx_id    => p_source_transaction_id
5315               );
5316             ELSIF(p_lpn_id <> p_transfer_lpn_id) THEN
5317               -- Unpack items from lpn with appropriate source name and trx action id
5318               Call_Pack_Unpack (
5319                 p_tempid           => p_transaction_temp_id
5320               , p_content_lpn      => p_content_lpn_id
5321               , p_lpn              => p_lpn_id
5322               , p_item_rec         => l_item_rec
5323               , p_revision         => p_revision
5324               , p_primary_qty      => ABS(l_primary_quantity)
5325               , p_qty              => ABS(l_transaction_quantity)
5326               , p_uom              => p_transaction_uom
5327               , p_org_id           => p_organization_id
5328               , p_subinv           => p_subinventory_code
5329               , p_locator          => p_locator_id
5330               , p_operation        => g_unpack
5331               , p_cost_grp_id      => p_cost_group_id
5332               , p_trx_action       => p_transaction_action_id
5333               , p_source_header_id => l_rcv_interface_txn_id
5334               , p_source_name      => l_rcv_txn_type
5335               , p_source_type_id   => NULL
5336               , p_sec_qty          => abs(l_secondary_trx_quantity) --INVCONV kkillams
5337               , p_sec_uom          => p_secondary_uom_code --INVCONV kkillams
5338               , p_source_trx_id    => p_source_transaction_id
5339               );
5340 
5341               -- Change context of transfer lpn to recieving
5342               UPDATE wms_license_plate_numbers
5343                  SET lpn_context = wms_container_pub.lpn_context_rcv
5344                    , subinventory_code = nvl(l_sub,subinventory_code) --6374764
5345                    , locator_id = nvl(l_loc_id,locator_id)            --6374764
5346                WHERE lpn_id = p_transfer_lpn_id;
5347 
5348               -- Repack items into transfer lpn
5349               -- changing source name, souce header, and cost group id to null
5350               -- Also changing the sub AND locator
5351               Call_Pack_Unpack (
5352                 p_tempid           => p_transaction_temp_id
5353               , p_content_lpn      => p_content_lpn_id
5354               , p_lpn              => p_transfer_lpn_id
5355               , p_item_rec         => l_item_rec
5356               , p_revision         => p_revision
5357               , p_primary_qty      => ABS(l_primary_quantity)
5358               , p_qty              => ABS(l_transaction_quantity)
5359               , p_uom              => p_transaction_uom
5360               , p_org_id           => p_organization_id
5361               , p_subinv           => NULL
5362               , p_locator          => NULL
5363               , p_operation        => g_pack
5364               , p_cost_grp_id      => NULL
5365               , p_trx_action       => p_transaction_action_id
5366               , p_source_header_id => NULL
5367               , p_source_name      => NULL
5368               , p_source_type_id   => NULL
5369               , p_sec_qty          => abs(l_secondary_trx_quantity) --INVCONV kkillams
5370               , p_sec_uom          => p_secondary_uom_code --INVCONV kkillams
5371               , p_source_trx_id    => p_source_transaction_id
5372               );
5373             ELSE
5374               --lpn is same as transfer lpn just need to null the source name,
5375               --source header id and cost group columns for all items inside
5376               --that lpn.
5377               UPDATE wms_lpn_contents
5378                  SET source_name = NULL
5379                    , source_header_id = NULL
5380                    , cost_group_id = NULL
5381                WHERE parent_lpn_id = p_lpn_id;
5382 
5383               -- Change context of transfer lpn to recieving
5384               UPDATE wms_license_plate_numbers
5385                  SET lpn_context = wms_container_pub.lpn_context_rcv
5386                    , subinventory_code = nvl(l_sub,subinventory_code) --6374764
5387                    , locator_id = nvl(l_loc_id,locator_id)	      --6374764
5388                WHERE lpn_id = p_lpn_id;
5389 
5390               --Same needs to be done in the serial numbers table
5391               UPDATE mtl_serial_numbers
5392                  SET cost_group_id = NULL
5393                WHERE lpn_id = p_lpn_id;
5394             END IF;
5395           ELSIF(l_rcv_txn_type = 'RETURN TO VENDOR'
5396                 OR l_rcv_txn_type = 'RETURN TO CUSTOMER') THEN
5397             -- Unpack items from lpn with appropriate source name and trx action id
5398             Call_Pack_Unpack (
5399               p_tempid           => p_transaction_temp_id
5400             , p_content_lpn      => p_content_lpn_id
5401             , p_lpn              => p_lpn_id
5402             , p_item_rec         => l_item_rec
5403             , p_revision         => p_revision
5404             , p_primary_qty      => ABS(l_primary_quantity)
5405             , p_qty              => ABS(l_transaction_quantity)
5406             , p_uom              => p_transaction_uom
5407             , p_org_id           => p_organization_id
5408             , p_subinv           => p_subinventory_code
5409             , p_locator          => p_locator_id
5410             , p_operation        => g_unpack
5411             , p_cost_grp_id      => p_cost_group_id
5412             , p_trx_action       => p_transaction_action_id
5413             , p_source_header_id => l_rcv_interface_txn_id
5414             , p_source_name      => l_rcv_txn_type
5415             , p_source_type_id   => NULL
5416             , p_sec_qty          => abs(l_secondary_trx_quantity) --INVCONV kkillams
5417             , p_sec_uom          => p_secondary_uom_code --INVCONV kkillams
5418             , p_source_trx_id    => p_source_transaction_id
5419             );
5420           ELSIF(l_rcv_txn_type = 'CORRECT') THEN
5421             IF (p_lpn_id <> p_transfer_lpn_id) THEN
5422               -- Unpack items from lpn with null source name and trx action id
5423               Call_Pack_Unpack (
5424                 p_tempid           => p_transaction_temp_id
5425               , p_content_lpn      => p_content_lpn_id
5426               , p_lpn              => p_lpn_id
5427               , p_item_rec         => l_item_rec
5428               , p_revision         => p_revision
5429               , p_primary_qty      => ABS(l_primary_quantity)
5430               , p_qty              => ABS(l_transaction_quantity)
5431               , p_uom              => p_transaction_uom
5432               , p_org_id           => p_organization_id
5433               , p_subinv           => p_subinventory_code
5434               , p_locator          => p_locator_id
5435               , p_operation        => g_unpack
5436               , p_cost_grp_id      => p_cost_group_id
5437               , p_trx_action       => p_transaction_action_id
5438               , p_source_header_id => NULL
5439               , p_source_name      => NULL
5440               , p_source_type_id   => NULL
5441               , p_sec_qty          => abs(l_secondary_trx_quantity) --INVCONV kkillams
5442               , p_sec_uom          => p_secondary_uom_code --INVCONV kkillams
5443               , p_source_trx_id    => p_source_transaction_id
5444               );
5445               -- Repack items into transfer lpn nulling cost group column
5446               Call_Pack_Unpack (
5447                 p_tempid           => p_transaction_temp_id
5448               , p_content_lpn      => p_content_lpn_id
5449               , p_lpn              => p_transfer_lpn_id
5450               , p_item_rec         => l_item_rec
5451               , p_revision         => p_revision
5452               , p_primary_qty      => ABS(l_primary_quantity)
5453               , p_qty              => ABS(l_transaction_quantity)
5454               , p_uom              => p_transaction_uom
5455               , p_org_id           => p_organization_id
5456               , p_subinv           => p_subinventory_code
5457               , p_locator          => p_locator_id
5458               , p_operation        => g_pack
5459               , p_cost_grp_id      => NULL
5460               , p_trx_action       => p_transaction_action_id
5461               , p_source_header_id => NULL
5462               , p_source_name      => NULL
5463               , p_source_type_id   => NULL
5464               , p_sec_qty          => abs(l_secondary_trx_quantity) --INVCONV kkillams
5465               , p_sec_uom          => p_secondary_uom_code --INVCONV kkillams
5466               , p_source_trx_id    => p_source_transaction_id
5467               );
5468             ELSE
5469               --lpn is same as transfer lpn just need to null the cost group column
5470               --for all items inside that lpn.
5471               UPDATE wms_lpn_contents
5472                  SET cost_group_id = NULL
5473                WHERE parent_lpn_id = p_lpn_id;
5474 
5475               --Same needs to be done in the serial numbers table
5476               UPDATE mtl_serial_numbers
5477                  SET cost_group_id = NULL
5478                WHERE lpn_id = p_lpn_id;
5479 
5480               -- Since correction is in same lpn, need to change context
5481               -- of lpn to recieving
5482               -- Change context of transfer lpn to recieving
5483               UPDATE wms_license_plate_numbers
5484                  SET lpn_context = wms_container_pub.lpn_context_rcv
5485                WHERE lpn_id = p_lpn_id;
5486             END IF;
5487           ELSE
5488             -- Default Action
5489             Call_Pack_Unpack (
5490               p_tempid           => p_transaction_temp_id
5491             , p_content_lpn      => p_content_lpn_id
5492             , p_lpn              => p_lpn_id
5493             , p_item_rec         => l_item_rec
5494             , p_revision         => p_revision
5495             , p_primary_qty      => ABS(l_primary_quantity)
5496             , p_qty              => ABS(l_transaction_quantity)
5497             , p_uom              => p_transaction_uom
5498             , p_org_id           => p_organization_id
5499             , p_subinv           => p_subinventory_code
5500             , p_locator          => p_locator_id
5501             , p_operation        => g_unpack
5502             , p_cost_grp_id      => p_cost_group_id
5503             , p_trx_action       => p_transaction_action_id
5504             , p_source_header_id => NULL
5505             , p_source_name      => NULL
5506             , p_source_type_id   => NULL
5507             , p_sec_qty          => abs(l_secondary_trx_quantity) --INVCONV kkillams
5508             , p_sec_uom          => p_secondary_uom_code --INVCONV kkillams
5509             , p_source_trx_id    => p_source_transaction_id
5510             );
5511           END IF;
5512         ELSIF(p_lpn_id IS NOT NULL) THEN
5513           -- this is an Issue of Item from LPN. Collect all the lot and
5514           -- serial records associated with this item and call packAPI
5515           Call_Pack_Unpack (
5516             p_tempid           => p_transaction_temp_id
5517           , p_content_lpn      => p_content_lpn_id
5518           , p_lpn              => p_lpn_id
5519           , p_item_rec         => l_item_rec
5520           , p_revision         => p_revision
5521           , p_primary_qty      => ABS(l_primary_quantity)
5522           , p_qty              => ABS(l_transaction_quantity)
5523           , p_uom              => p_transaction_uom
5524           , p_org_id           => p_organization_id
5525           , p_subinv           => p_subinventory_code
5526           , p_locator          => p_locator_id
5527           , p_operation        => g_unpack
5528           , p_cost_grp_id      => p_cost_group_id
5529           , p_trx_action       => p_transaction_action_id
5530           , p_source_header_id => NULL
5531           , p_source_name      => NULL
5532           , p_source_type_id   => NULL
5533           , p_sec_qty          => abs(l_secondary_trx_quantity) --INVCONV kkillams
5534           , p_sec_uom          => p_secondary_uom_code --INVCONV kkillams
5535           , p_source_trx_id    => p_source_transaction_id
5536           );
5537         END IF;
5538       /***** RECEIPT/LotSplit/LotMerge/Cycle/PhysicalCount/IntransitRcpt trx ****/
5539       ELSIF (p_transaction_action_id = inv_globals.g_action_receipt)
5540             OR(p_transaction_action_id = inv_globals.g_action_inv_lot_split)
5541             OR((p_transaction_action_id = inv_globals.g_action_inv_lot_translate)
5542                AND(l_primary_quantity > 0))
5543             OR(p_transaction_action_id = inv_globals.g_action_inv_lot_merge)
5544             OR(p_transaction_action_id = inv_globals.g_action_intransitreceipt)
5545             OR(p_transaction_action_id = inv_globals.g_action_assycomplete)
5546             OR((p_transaction_action_id = inv_globals.g_action_cyclecountadj)
5547                AND l_primary_quantity > 0)
5548             OR((p_transaction_action_id = inv_globals.g_action_physicalcountadj)
5549                AND l_primary_quantity > 0)
5550             OR(p_transaction_action_id = inv_globals.g_action_logicalreceipt)
5551                                                                              -- IDS: Logical Transactions for lot serial support
5552                                                                              -- IDS: Logical Transactions for lot and serials - Opened up TM to
5553                                                                              --  handle logical receipts and logical corrections.
5554       THEN
5555         IF (l_debug = 1) THEN
5556           inv_log_util.TRACE(' Action = Receipt', 'INV_LPN_TRX_PUB', 9);
5557         END IF;
5558 
5559         -- Check if a receipt with PO or RMA source types check to see if it is a correction
5560         IF (
5561             p_transaction_action_id = inv_globals.g_action_receipt
5562             AND(
5563                 p_transaction_source_type_id = inv_globals.g_sourcetype_purchaseorder
5564                 OR p_transaction_source_type_id = inv_globals.g_sourcetype_rma
5565                )
5566            )
5567            OR(
5568               p_transaction_action_id = inv_globals.g_action_logicalreceipt
5569               AND p_transaction_source_type_id = inv_globals.g_sourcetype_purchaseorder
5570              )
5571               --  IDS:Logical Transaction lot serial support
5572               -- IDS: Logical Transactions for lot and serials - Opened up TM to
5573               --  handle logical receipts and logical corrections.
5574         THEN
5575           BEGIN
5576             SELECT transaction_type
5577               INTO l_rcv_txn_type
5578               FROM rcv_transactions
5579              WHERE transaction_id = p_rcv_transaction_id;
5580           EXCEPTION
5581             WHEN OTHERS THEN
5582               l_rcv_txn_type  := NULL;
5583           END;
5584         END IF;
5585 
5586         IF (l_debug = 1) THEN
5587           inv_log_util.TRACE('Got l_rcv_txn_typ=' || l_rcv_txn_type, 'INV_LPN_TRX_PUB', 9);
5588         END IF;
5589 
5590         -- If receipt is a correction, then special logic is needed
5591         -- otherwise use default behavior
5592         IF (
5593             (
5594              (
5595               p_transaction_action_id = inv_globals.g_action_receipt
5596               AND(
5597                   p_transaction_source_type_id = inv_globals.g_sourcetype_purchaseorder
5598                   OR p_transaction_source_type_id = inv_globals.g_sourcetype_rma
5599                  )
5600              )
5601              OR(
5602                 p_transaction_action_id = inv_globals.g_action_logicalreceipt
5603                 AND p_transaction_source_type_id = inv_globals.g_sourcetype_purchaseorder
5604                )
5605             )
5606             -- IDS: Logical Transactions for lot and serials - Opened up TM to
5607             --  handle logical receipts and logical corrections.
5608             AND l_rcv_txn_type = 'CORRECT'
5609            ) THEN
5610           -- Unpack items from lpn with null source name and trx action id
5611           Call_Pack_Unpack (
5612             p_tempid           => p_transaction_temp_id
5613           , p_content_lpn      => p_content_lpn_id
5614           , p_lpn              => p_lpn_id
5615           , p_item_rec         => l_item_rec
5616           , p_revision         => p_revision
5617           , p_primary_qty      => ABS(l_primary_quantity)
5618           , p_qty              => ABS(l_transaction_quantity)
5619           , p_uom              => p_transaction_uom
5620           , p_org_id           => p_organization_id
5621           , p_subinv           => p_subinventory_code
5622           , p_locator          => p_locator_id
5623           , p_operation        => g_unpack
5624           , p_cost_grp_id      => p_cost_group_id
5625           , p_trx_action       => p_transaction_action_id
5626           , p_source_header_id => NULL
5627           , p_source_name      => NULL
5628           , p_source_type_id   => p_transaction_source_type_id
5629           , p_sec_qty          => abs(l_secondary_trx_quantity) --INVCONV kkillams
5630           , p_sec_uom          => p_secondary_uom_code --INVCONV kkillams
5631           , p_source_trx_id    => p_source_transaction_id
5632           );
5633           -- Repack items into transfer lpn with null source name and trx action id
5634           Call_Pack_Unpack (
5635             p_tempid           => p_transaction_temp_id
5636           , p_content_lpn      => p_content_lpn_id
5637           , p_lpn              => p_transfer_lpn_id
5638           , p_item_rec         => l_item_rec
5639           , p_revision         => p_revision
5640           , p_primary_qty      => ABS(l_primary_quantity)
5641           , p_qty              => ABS(l_transaction_quantity)
5642           , p_uom              => p_transaction_uom
5643           , p_org_id           => p_organization_id
5644           , p_subinv           => p_subinventory_code
5645           , p_locator          => p_locator_id
5646           , p_operation        => g_pack
5647           , p_cost_grp_id      => p_cost_group_id
5648           , p_trx_action       => p_transaction_action_id
5649           , p_source_header_id => NULL
5650           , p_source_name      => NULL
5651           , p_source_type_id   => NULL
5652           , p_sec_qty          => abs(l_secondary_trx_quantity) --INVCONV kkillams
5653           , p_sec_uom          => p_secondary_uom_code --INVCONV kkillams
5654           , p_source_trx_id    => p_source_transaction_id
5655           );
5656         ELSE
5657           -- Do an unpack if (from)LPN_ID column is not null and (from)LPN_ID
5658           -- is not equal to Xfr_LPN_ID
5659           -- also need to unpack if its receipt of RMA so issue
5660           IF (p_lpn_id IS NOT NULL)
5661              AND(
5662                  (p_lpn_id <> NVL(p_transfer_lpn_id, 0))
5663                  OR
5664                    /*added for 3158847*/
5665                  (  l_item_rec.serial_number_control_code = 6
5666                     AND p_transaction_action_id = 27
5667                     AND p_transaction_source_type_id = 12)
5668                 ) THEN
5669             -- Do an unpack of the item/LPN. Disregard the cost group.
5670             -- This is because when the item was prepacked, it could n't
5671             -- have had a cost group
5672             Call_Pack_Unpack (
5673               p_tempid           => p_transaction_temp_id
5674             , p_content_lpn      => p_content_lpn_id
5675             , p_lpn              => p_lpn_id
5676             , p_item_rec         => l_item_rec
5677             , p_revision         => p_revision
5678             , p_primary_qty      => ABS(l_primary_quantity)
5679             , p_qty              => ABS(l_transaction_quantity)
5680             , p_uom              => p_transaction_uom
5681             , p_org_id           => p_organization_id
5682             , p_subinv           => p_subinventory_code
5683             , p_locator          => p_locator_id
5684             , p_operation        => g_unpack
5685             , p_cost_grp_id      => NULL
5686             , p_trx_action       => p_transaction_action_id
5687             , p_source_header_id => /*3158847*/NULL
5688             , p_source_name      => /*3158847*/NULL
5689             , p_source_type_id   => /*3158847*/p_transaction_source_type_id
5690             , p_sec_qty          => abs(l_secondary_trx_quantity) --INVCONV kkillams
5691             , p_sec_uom          => p_secondary_uom_code --INVCONV kkillams
5692             , p_source_trx_id    => p_source_transaction_id
5693             );
5694           END IF;
5695 
5696           IF (p_transfer_lpn_id IS NOT NULL)
5697              AND(
5698                  (NVL(p_lpn_id, 0) <> p_transfer_lpn_id)
5699                  OR
5700                    /*added for 3158847*/
5701                  (  l_item_rec.serial_number_control_code = 6
5702                     AND p_transaction_action_id = 27
5703                     AND p_transaction_source_type_id = 12)
5704                 ) THEN
5705             --  Transfering to an existing LPN. need to do a pack operation here
5706             Call_Pack_Unpack (
5707               p_tempid           => p_transaction_temp_id
5708             , p_content_lpn      => p_content_lpn_id
5709             , p_lpn              => p_transfer_lpn_id
5710             , p_item_rec         => l_item_rec
5711             , p_revision         => p_revision
5712             , p_primary_qty      => ABS(l_primary_quantity)
5713             , p_qty              => ABS(l_transaction_quantity)
5714             , p_uom              => p_transaction_uom
5715             , p_org_id           => p_organization_id
5716             , p_subinv           => p_subinventory_code
5717             , p_locator          => p_locator_id
5718             , p_operation        => g_pack
5719             , p_cost_grp_id      => p_cost_group_id
5720             , p_trx_action       => p_transaction_action_id
5721             , p_source_header_id => /*3158847*/NULL
5722             , p_source_name      => /*3158847*/NULL
5723             , p_source_type_id   => /*3158847*/p_transaction_source_type_id
5724             , p_sec_qty          => abs(l_secondary_trx_quantity) --INVCONV kkillams
5725             , p_sec_uom          => p_secondary_uom_code --INVCONV kkillams
5726             , p_source_trx_id    => p_source_transaction_id
5727             );
5728           END IF;
5729 
5730           -- For the case where the from_lpn_id = to_lpn_id, then need
5731           -- to manually update COST_GROUP_ID, as we are not calling Pack in this case
5732           IF (p_lpn_id = p_transfer_lpn_id) THEN
5733             UPDATE wms_lpn_contents
5734                SET cost_group_id = p_cost_group_id
5735              WHERE organization_id = p_organization_id
5736                AND inventory_item_id = p_inventory_item_id
5737                AND parent_lpn_id = p_lpn_id
5738                AND SERIAL_SUMMARY_ENTRY=2 ;
5739            /* Bug 3910656- Added the last condition to the query to update only those
5740                            records with the serial_summary_entry as 2. */
5741 
5742             -- If this item is Serial controlled, set cost_group_id for Serials
5743             UPDATE mtl_serial_numbers
5744                SET cost_group_id = p_cost_group_id
5745              WHERE inventory_item_id = p_inventory_item_id
5746                AND lpn_id = p_lpn_id;
5747 
5748             IF (l_debug = 1) THEN
5749               inv_log_util.TRACE('**Updated WMS_Contents cost_grp_id for lpn=' || p_lpn_id || ' and cg=' || p_cost_group_id
5750               , 'INV_LPN_TRX_PUB', 9);
5751             END IF;
5752           END IF;
5753 
5754           -- Update the status of the received LPN, or the LPN to which the item
5755           -- is received
5756           IF (p_content_lpn_id IS NOT NULL)
5757              OR(p_transfer_lpn_id IS NOT NULL) THEN
5758             IF (p_content_lpn_id IS NOT NULL) THEN
5759               -- If receiving a complete LPN, then check for the autoUnpack
5760               -- status of the subinventory to which it is received
5761               SELECT lpn_controlled_flag
5762                 INTO v_autounpack
5763                 FROM mtl_secondary_inventories
5764                WHERE organization_id = p_organization_id
5765                  AND secondary_inventory_name = p_subinventory_code;
5766 
5767               v_lpn.lpn_id  := p_content_lpn_id;
5768             ELSE
5769               v_lpn.lpn_id  := p_transfer_lpn_id;
5770             END IF;
5771 
5772             -- Update the LPN status and location
5773             SELECT lpn_context
5774               INTO v_lpn_ctx
5775               FROM wms_license_plate_numbers
5776              WHERE lpn_id = v_lpn.lpn_id;
5777 
5778             IF (v_lpn_ctx <> wms_container_pub.lpn_context_picked) THEN
5779               v_lpn.lpn_context  := wms_container_pub.lpn_context_inv;
5780             ELSE
5781               v_lpn.lpn_context  := wms_container_pub.lpn_context_picked;
5782             END IF;
5783 
5784             v_lpn.organization_id    := p_organization_id;
5785             v_lpn.subinventory_code  := p_subinventory_code;
5786             v_lpn.locator_id         := p_locator_id;
5787             update_lpn_status(v_lpn);
5788 
5789             -- If the autounpack is set for the receiving sub, then unpack the
5790             -- contents of the LPN.
5791             IF (v_autounpack = 2) THEN
5792               WMS_Container_PVT.PackUnpack_Container (
5793                 p_api_version           => 1.0
5794               , p_init_msg_list         => fnd_api.g_false
5795               , p_commit                => fnd_api.g_false
5796               , p_validation_level      => fnd_api.g_valid_level_none
5797               , x_return_status         => ret_status
5798               , x_msg_count             => ret_msgcnt
5799               , x_msg_data              => ret_msgdata
5800               , p_caller                => 'INV_TRNSACTION'
5801               , p_lpn_id                => p_content_lpn_id
5802               , p_organization_id       => p_organization_id
5803               , p_subinventory          => p_subinventory_code
5804               , p_locator_id            => p_locator_id
5805               , p_operation             => g_unpack_all
5806               , p_source_transaction_id => p_source_transaction_id );
5807 
5808               IF (ret_status <> fnd_api.g_ret_sts_success) THEN
5809                 IF (l_debug = 1) THEN
5810                   inv_log_util.TRACE('**Error Full unpack :' || ret_status, 'INV_LPN_TRX_PUB', 1);
5811                 END IF;
5812 
5813                 fnd_message.set_name('INV', 'INV_PACKUNPACK_FAILURE');
5814                 fnd_msg_pub.ADD;
5815                 RAISE fnd_api.g_exc_error;
5816               END IF;
5817             END IF;
5818           END IF;
5819         END IF;
5820       /***** Actions for SUBXFR / ORGXFR / STGXFR transactions ****/
5821       ELSIF (p_transaction_action_id = inv_globals.g_action_subxfr)
5822             OR(p_transaction_action_id = inv_globals.g_action_planxfr)
5823             OR(p_transaction_action_id = inv_globals.g_action_orgxfr)
5824             OR(p_transaction_action_id = inv_globals.g_action_stgxfr)
5825             OR(p_transaction_action_id = inv_globals.g_action_costgroupxfr) THEN
5826         IF (l_debug = 1) THEN
5827           inv_log_util.TRACE(' Action = SubXfr/OrgXfr/StgXfr/CGXfr', 'INV_LPN_TRX_PUB', 9);
5828         END IF;
5829 
5830         -- sanity check. If CostGroupXfr, then both cost_group_id and
5831         -- xfr_cost_group_id should be filled
5832         IF (p_transaction_action_id = inv_globals.g_action_costgroupxfr) THEN
5833           IF (p_cost_group_id IS NULL)
5834              OR(p_transfer_cost_group_id IS NULL) THEN
5835             x_proc_msg  := 'Error. Cost Groups not specified. CG=' || p_cost_group_id || ',XfrCG=' || p_transfer_cost_group_id;
5836 
5837             IF (l_debug = 1) THEN
5838               inv_log_util.TRACE('Error. Cost Groups not specified. CG=' || p_cost_group_id || ',XfrCG=' || p_transfer_cost_group_id
5839               , 'INV_LPN_TRX_PUB', 1);
5840             END IF;
5841 
5842             fnd_message.set_name('INV', 'BAD_INPUT_ARGUMENTS');
5843             fnd_msg_pub.ADD;
5844             RAISE fnd_api.g_exc_error;
5845           END IF;
5846         END IF;
5847 
5848         -- If transaction is subtransfer
5849         IF ( p_transaction_action_id = inv_globals.g_action_subxfr OR
5850              p_transaction_action_id = inv_globals.g_action_planxfr )
5851         THEN
5852           IF(p_transfer_lpn_id IS NOT NULL AND p_lpn_id IS NOT NULL) THEN
5853           	 IF ( v_lpn_ctx IS NULL ) THEN
5854           	   SELECT lpn_context
5855                INTO   v_lpn_ctx
5856                FROM   wms_license_plate_numbers
5857                WHERE  lpn_id = p_lpn_id;
5858              END IF;
5859 
5860              IF (l_debug = 1) THEN
5861                inv_log_util.TRACE('SUBXFER from LPN context=' || v_lpn_ctx, 'INV_LPN_TRX_PUB', 9);
5862              END IF;
5863 
5864              IF (v_lpn_ctx = wms_container_pub.lpn_context_picked) THEN
5865                split_delivery(
5866                  p_tempid                     => p_transaction_temp_id
5867                , p_lpn_id                     => p_lpn_id
5868                , p_xfr_lpn_id                 => p_transfer_lpn_id
5869                , p_item_rec                   => l_item_rec
5870                , p_revision                   => p_revision
5871                , p_qty                        => l_primary_quantity
5872                , p_uom                        => l_item_rec.primary_uom_code
5873                , p_secondary_trx_quantity     => l_secondary_trx_quantity  --INVCONV kkillams
5874                , p_secondary_uom_code         => p_secondary_uom_code
5875                , p_org_id                     => p_organization_id
5876                , p_subinventory_code          => p_subinventory_code
5877                , p_locator_id                 => p_locator_id
5878                , p_xfr_subinventory           => p_transfer_subinventory
5879                , p_xfr_to_location            => p_transfer_to_location
5880                , p_transaction_source_id      => p_transaction_source_id
5881                , p_trx_source_line_id         => p_trx_source_line_id );
5882              END IF;
5883            END IF;
5884          END IF;
5885 
5886         -- If the (from)_lpn_id is not NULL, then unpack the item/lpn from the
5887         -- this LPN, but only if not packing back to the same LPN,
5888         -- unless this is a CostGroup Transfer transaction
5889         IF (p_lpn_id IS NOT NULL)
5890            AND((p_lpn_id <> NVL(p_transfer_lpn_id, 0))
5891                OR(p_transaction_action_id = inv_globals.g_action_costgroupxfr)) THEN
5892           -- Verify that the context of this LPN is 'Resides in Inventory'
5893           SELECT lpn_context
5894             INTO v_lpn_ctx
5895             FROM wms_license_plate_numbers
5896            WHERE lpn_id = p_lpn_id;
5897 
5898           IF (v_lpn_ctx <> wms_container_pub.lpn_context_inv
5899               AND v_lpn_ctx <> wms_container_pub.lpn_context_picked) THEN
5900             x_proc_msg  := 'Error. Invalid LPN context for Transfer Trx:' || v_lpn_ctx;
5901 
5902             IF (l_debug = 1) THEN
5903               inv_log_util.TRACE('Error. Invalid LPN context for Transfer Trx:' || v_lpn_ctx, 'INV_LPN_TRX_PUB', 1);
5904             END IF;
5905 
5906             fnd_message.set_name('INV', 'INV_INVALID_LPN_CONTEXT');
5907             fnd_msg_pub.ADD;
5908             RAISE fnd_api.g_exc_error;
5909           END IF;
5910 
5911           -- An item or LPN is being unpacked from this LPN.
5912           Call_Pack_Unpack (
5913             p_tempid           => p_transaction_temp_id
5914           , p_content_lpn      => p_content_lpn_id
5915           , p_lpn              => p_lpn_id
5916           , p_item_rec         => l_item_rec
5917           , p_revision         => p_revision
5918           , p_primary_qty      => ABS(l_primary_quantity)
5919           , p_qty              => ABS(l_transaction_quantity)
5920           , p_uom              => p_transaction_uom
5921           , p_org_id           => p_organization_id
5922           , p_subinv           => p_subinventory_code
5923           , p_locator          => p_locator_id
5924           , p_operation        => g_unpack
5925           , p_cost_grp_id      => p_cost_group_id
5926           , p_trx_action       => NULL
5927           , p_source_header_id => NULL
5928           , p_source_name      => NULL
5929           , p_source_type_id   => NULL
5930           , p_sec_qty          => abs(l_secondary_trx_quantity) --INVCONV kkillams
5931           , p_sec_uom          => p_secondary_uom_code --INVCONV kkillams
5932           , p_source_trx_id    => p_source_transaction_id
5933           );
5934 
5935           --bug2747049
5936           IF (p_transaction_action_id = inv_globals.g_action_costgroupxfr) THEN
5937             UPDATE wms_license_plate_numbers
5938                SET lpn_context = v_lpn_ctx
5939                  , subinventory_code = p_subinventory_code
5940                  , locator_id = p_locator_id
5941              WHERE lpn_id = p_lpn_id
5942                AND lpn_context = wms_container_pub.lpn_context_pregenerated;
5943           END IF;
5944         END IF;
5945 
5946         IF (p_transaction_action_id = inv_globals.g_action_orgxfr) THEN
5947           v_xfr_org  := p_transfer_organization;
5948         ELSE
5949           v_xfr_org  := p_organization_id;
5950         END IF;
5951 
5952         IF (p_transaction_action_id <> inv_globals.g_action_costgroupxfr) THEN
5953           -- Check transfer org to see if it is wms enabled
5954           IF (
5955               wms_install.check_install(x_return_status => ret_status, x_msg_count => ret_msgcnt, x_msg_data => ret_msgdata
5956               , p_organization_id            => v_xfr_org)
5957              ) THEN
5958             -- Get the LPN_CONTROLLED_FLAG status of the transfer subinventory
5959             SELECT lpn_controlled_flag
5960               INTO v_autounpack
5961               FROM mtl_secondary_inventories
5962              WHERE organization_id = v_xfr_org
5963                AND secondary_inventory_name = p_transfer_subinventory;
5964 
5965             IF (l_debug = 1) THEN
5966               inv_log_util.TRACE(
5967                 'orgid=' || v_xfr_org || ' sub=' || p_transfer_subinventory || ' lpn_controlled_flag=' || v_autounpack
5968               , 'INV_LPN_TRX_PUB'
5969               , 9
5970               );
5971             END IF;
5972           ELSE   -- Not a WMS organization unpack all lpns
5973             IF (l_debug = 1) THEN
5974               inv_log_util.TRACE('orgid=' || v_xfr_org || ' not wms enabled unpacking all', 'INV_LPN_TRX_PUB', 9);
5975             END IF;
5976 
5977             v_autounpack  := 2;
5978             -- Also need to prevent the LPN from being assigned to a non wms org
5979             v_xfr_org := p_organization_id;
5980           END IF;
5981         END IF;
5982 
5983         -- bug 2879208
5984         SELECT wms_task_type
5985           INTO l_system_task_type
5986           FROM mtl_material_transactions_temp
5987          WHERE transaction_temp_id = p_transaction_temp_id;
5988 
5989         IF (l_debug = 1) THEN
5990           inv_log_util.TRACE('l_system_task_type = ' || l_system_task_type, 'INV_LPN_TRX_PUB', 9);
5991         END IF;
5992 
5993         -- bug 2879208
5994 
5995 	--Bug 6007873
5996 	IF (p_lpn_id IS NOT NULL) THEN
5997 
5998 	 SELECT lpn_context
5999             INTO l_lpn_ctx_tmp
6000             FROM wms_license_plate_numbers
6001            WHERE lpn_id = p_lpn_id;
6002 
6003           -- For Sub transfers reservations should be transfered
6004           -- for content lpn of who's context is not picked
6005           IF (p_transaction_action_id = inv_globals.g_action_subxfr
6006               AND(l_lpn_ctx_tmp <> wms_container_pub.lpn_context_picked
6007                   AND NVL(l_system_task_type, -1) <> 7)) THEN
6008             IF (l_debug = 1) THEN
6009               inv_log_util.TRACE('Sub Xfer of LPN, calling Transfer_LPN_Reservations ', 'INV_LPN_TRX_PUB', 9);
6010             END IF;
6011 
6012             inv_lpn_reservations_pvt.transfer_lpn_reservations(
6013               x_return_status              => ret_status
6014             , x_msg_count                  => ret_msgcnt
6015             , x_msg_data                   => ret_msgdata
6016             , p_organization_id            => p_organization_id
6017             , p_lpn_id                     => p_lpn_id
6018             , p_to_subinventory_code       => p_transfer_subinventory
6019             , p_to_locator_id              => p_transfer_to_location
6020             );
6021 
6022             IF (ret_status <> fnd_api.g_ret_sts_success) THEN
6023               IF (l_debug = 1) THEN
6024                 inv_log_util.TRACE('Undefined error in calling Transfer_LPN_Reservations', 'INV_LPN_TRX_PUB', 1);
6025               END IF;
6026 
6027               RAISE fnd_api.g_exc_error;
6028             END IF;
6029 
6030             IF (l_debug = 1) THEN
6031               inv_log_util.TRACE('Done with call to Transfer_LPN_Reservations', 'INV_LPN_TRX_PUB', 9);
6032             END IF;
6033           END IF;
6034 	END IF;
6035 	 -- bug 6007873
6036 
6037         -- Is this is a transfer of a complete LPN
6038         IF (p_content_lpn_id IS NOT NULL) THEN
6039           -- Update the LPN status and location
6040           SELECT lpn_context
6041             INTO v_cnt_lpn_ctx
6042             FROM wms_license_plate_numbers
6043            WHERE lpn_id = p_content_lpn_id;
6044 
6045           -- For Sub transfers reservations should be transfered
6046           -- for content lpn of who's context is not picked
6047           IF (
6048               p_transaction_action_id = inv_globals.g_action_subxfr
6049               AND(v_cnt_lpn_ctx <> wms_container_pub.lpn_context_picked
6050                   AND NVL(l_system_task_type, -1) <> 7)
6051              )   -- bug 2879208 added nv for 3240617
6052               THEN
6053             IF (l_debug = 1) THEN
6054               inv_log_util.TRACE('Sub Xfer of LPN, calling Transfer_LPN_Reservations', 'INV_LPN_TRX_PUB', 9);
6055             END IF;
6056 
6057          /*Added for Bug#6043776*/
6058             l_cyclpn_id:=p_content_lpn_id;
6059                 /*if the transaction is cyclecount use outer LPN to update reservations*/
6060                 IF(p_transaction_source_type_id=inv_globals.G_SOURCETYPE_CYCLECOUNT) THEN
6061                   IF (l_debug = 1) THEN
6062                         inv_log_util.TRACE('entered here cyclecount.. Bug#6043776','INV_LPN_TRX_PUB', 9);
6063                   END IF;
6064                   SELECT OUTERMOST_LPN_ID into l_cyclpn_id
6065                   from wms_license_plate_numbers
6066                   where lpn_id=p_content_lpn_id;
6067                 END IF;
6068             /*End of Bug#6043776*/
6069 
6070             inv_lpn_reservations_pvt.transfer_lpn_reservations(
6071               x_return_status              => ret_status
6072             , x_msg_count                  => ret_msgcnt
6073             , x_msg_data                   => ret_msgdata
6074             , p_organization_id            => p_organization_id
6075 --          , p_lpn_id                     => p_content_lpn_id    commented for Bug 6043776
6076             , p_lpn_id                     => l_cyclpn_id --Bug # 6043776
6077             , p_to_subinventory_code       => p_transfer_subinventory
6078             , p_to_locator_id              => p_transfer_to_location
6079             );
6080 
6081             IF (ret_status <> fnd_api.g_ret_sts_success) THEN
6082               IF (l_debug = 1) THEN
6083                 inv_log_util.TRACE('Undefined error in calling Transfer_LPN_Reservations', 'INV_LPN_TRX_PUB', 1);
6084               END IF;
6085 
6086               RAISE fnd_api.g_exc_error;
6087             END IF;
6088 
6089             IF (l_debug = 1) THEN
6090               inv_log_util.TRACE('Done with call to Transfer_LPN_Reservations', 'INV_LPN_TRX_PUB', 9);
6091             END IF;
6092           END IF;
6093 
6094           /*if ( v_lpn_ctx <> WMS_Container_PUB.LPN_CONTEXT_PICKED ) then
6095             v_lpn.lpn_context := WMS_Container_PUB.LPN_CONTEXT_INV;
6096           else
6097             v_lpn.lpn_context := WMS_Container_PUB.LPN_CONTEXT_PICKED;
6098           end if;
6099           */
6100           v_lpn.lpn_id           := p_content_lpn_id;
6101           v_lpn.organization_id  := v_xfr_org;
6102 
6103           --v_lpn.SUBINVENTORY_CODE := p_transfer_subinventory;
6104           --v_lpn.LOCATOR_ID := p_transfer_to_location;
6105           --UPDATE_LPN_STATUS(v_lpn);
6106 
6107           -- If the autounpack is set for the transfer sub, then unpack the
6108           -- contents of the LPN.
6109           IF (v_autounpack = 2) THEN
6110             IF (l_debug = 1) THEN
6111               inv_log_util.TRACE('**Calling fullunpack lpn:' || p_content_lpn_id, 'INV_LPN_TRX_PUB', 9);
6112             END IF;
6113 
6114             -- bug 5531237 LPN org has not changed yet need to use original org not xfer org
6115             WMS_Container_PVT.PackUnpack_Container (
6116               p_api_version           => 1.0
6117             , p_init_msg_list         => fnd_api.g_false
6118             , p_commit                => fnd_api.g_false
6119             , p_validation_level      => fnd_api.g_valid_level_none
6120             , x_return_status         => ret_status
6121             , x_msg_count             => ret_msgcnt
6122             , x_msg_data              => ret_msgdata
6123             , p_caller                => 'INV_TRNSACTION'
6124             , p_lpn_id                => p_content_lpn_id
6125             , p_organization_id       => p_organization_id
6126             , p_subinventory          => NULL
6127             , p_locator_id            => NULL
6128             , p_operation             => g_unpack_all
6129             , p_source_transaction_id => p_source_transaction_id );
6130 
6131             IF (ret_status <> fnd_api.g_ret_sts_success) THEN
6132               IF (l_debug = 1) THEN
6133                 inv_log_util.TRACE('**Error Full unpack :' || ret_status, 'INV_LPN_TRX_PUB', 1);
6134               END IF;
6135 
6136               fnd_message.set_name('INV', 'INV_PACKUNPACK_FAILURE');
6137               fnd_msg_pub.ADD;
6138               RAISE fnd_api.g_exc_error;
6139             END IF;
6140           ELSE
6141             IF ( l_system_task_type = 7 AND
6142                  v_cnt_lpn_ctx = wms_container_pub.lpn_loaded_in_stage )
6143             THEN
6144               -- Bug 4247497 Staging Move. Context should be changed to pick
6145               v_lpn.lpn_context  := wms_container_pub.lpn_context_picked;
6146             ELSIF (v_cnt_lpn_ctx <> wms_container_pub.lpn_context_picked) THEN
6147               v_lpn.lpn_context  := wms_container_pub.lpn_context_inv;
6148             ELSE
6149 	      --Bug 5509764
6150 	      --For direct org transfer against an internal req, set the lpn_context
6151               --to "In Inventory" from Picked to reflect the correct status
6152               IF ( p_transaction_source_type_id = inv_globals.G_SOURCETYPE_INTORDER AND
6153                    p_transaction_action_id = inv_globals.G_ACTION_ORGXFR) THEN
6154                 IF (l_debug = 1) THEN
6155                   inv_log_util.trace('int req direct org xfr. setting lpn_ctxt to in inventory','INV_LPN_TRX_PUB',1);
6156                 END IF;
6157                 v_lpn.lpn_context := wms_container_pub.lpn_context_inv;
6158 	      ELSE
6159                 v_lpn.lpn_context  := wms_container_pub.lpn_context_picked;
6160               END IF; --END IF check txn_source_type intorder and action direct org xfr
6161             END IF; --END IF LPN context is picked
6162 
6163             v_lpn.subinventory_code  := p_transfer_subinventory;
6164             v_lpn.locator_id         := p_transfer_to_location;
6165           END IF;
6166 
6167           -- Bug 4247497 Moving update of context to TM. If this is a stagexfr transaction
6168           -- then lpn-context should be updated to 'picked'
6169           IF ( p_transaction_action_id = inv_globals.G_ACTION_STGXFR ) THEN
6170             v_lpn.lpn_context := wms_container_pub.LPN_CONTEXT_PICKED;
6171           END IF;
6172 
6173           /** moved update here to fix bug3299521, this way we would avoid updating
6174               the parent_lpn context for unpacked inner lpns**/
6175           update_lpn_status(v_lpn);
6176         END IF;
6177 
6178         -- If the transfer_lpn_id is not NULL, then pack the item/LPN to
6179         -- this LPN, but only if not unpacked from the same LPN,
6180         -- unless this is a CostGroup Transfer transaction
6181         IF (p_transfer_lpn_id IS NOT NULL)
6182            AND((NVL(p_lpn_id, 0) <> p_transfer_lpn_id)
6183                OR(p_transaction_action_id = inv_globals.g_action_costgroupxfr)) THEN
6184           SELECT lpn_context
6185             INTO v_xfrlpn_ctx
6186             FROM wms_license_plate_numbers
6187            WHERE lpn_id = p_transfer_lpn_id;
6188 
6189           -- If the item/lpn is transfered to another LPN, then update the
6190           -- status of that LPN, provided the context is 2, 3, or 8
6191           -- and xfr sub is not non-LPN controlled sub
6192           IF (
6193               (v_xfrlpn_ctx = wms_container_pub.lpn_context_wip)
6194               OR(v_xfrlpn_ctx = wms_container_pub.lpn_context_rcv)
6195               OR(v_xfrlpn_ctx = wms_container_pub.lpn_context_packing)
6196               OR(v_xfrlpn_ctx = wms_container_pub.lpn_context_pregenerated)
6197              )
6198              AND(v_autounpack <> 2) THEN
6199             -- If context is pre-generated, then inherit the context of the contentLPN or fromLPN
6200             --  if that context is INV or PICKED
6201             IF (v_xfrlpn_ctx = wms_container_pub.lpn_context_pregenerated)
6202                AND NVL(v_cnt_lpn_ctx, v_lpn_ctx) NOT IN
6203                     (
6204                      wms_container_pub.lpn_context_wip
6205                    , wms_container_pub.lpn_context_rcv
6206                    , wms_container_pub.lpn_context_packing
6207                    , wms_container_pub.lpn_context_vendor
6208                     ) THEN
6209               v_lpn.lpn_context  := NVL(v_cnt_lpn_ctx, v_lpn_ctx);
6210 
6211               IF (l_debug = 1) THEN
6212                 inv_log_util.TRACE('Changing context of xfrlpn cntlpnctx=' || v_cnt_lpn_ctx || ' lpnctx=' || v_lpn_ctx, 'INV_LPN_TRX_PUB'
6213                 , 9);
6214               END IF;
6215             ELSE
6216               -- Bug 4247497 Moving update of context to TM
6217               IF ( p_transaction_action_id = inv_globals.g_action_stgxfr ) THEN
6218                 v_lpn.lpn_context := wms_container_pub.lpn_context_picked;
6219               ELSE
6220                 v_lpn.lpn_context := wms_container_pub.lpn_context_inv;
6221               END IF;
6222             END IF;
6223 
6224             v_lpn.lpn_id             := p_transfer_lpn_id;
6225             v_lpn.organization_id    := v_xfr_org;
6226             v_lpn.subinventory_code  := p_transfer_subinventory;
6227             v_lpn.locator_id         := p_transfer_to_location;
6228             update_lpn_status(v_lpn);
6229           ELSIF(
6230                 v_xfrlpn_ctx <> wms_container_pub.lpn_context_inv
6231                 AND v_xfrlpn_ctx <> wms_container_pub.lpn_context_picked
6232                 AND v_xfrlpn_ctx <> wms_container_pub.lpn_context_pregenerated
6233                 AND v_xfrlpn_ctx <> wms_container_pub.lpn_context_packing /*added for 3160462*/
6234                ) THEN
6235             -- Verify that the context of this LPN is 'Resides in Inventory'
6236             x_proc_msg  := 'Error. Invalid LPN context for XFR_LPN:' || v_xfrlpn_ctx;
6237 
6238             IF (l_debug = 1) THEN
6239               inv_log_util.TRACE('Error. Invalid LPN context for XFR_LPN :' || v_xfrlpn_ctx, 'INV_LPN_TRX_PUB', 1);
6240             END IF;
6241 
6242             fnd_message.set_name('INV', 'INV_INVALID_LPN_CONTEXT');
6243             fnd_msg_pub.ADD;
6244             RAISE fnd_api.g_exc_error;
6245           END IF;
6246 
6247           -- For CostGroupTransfer transactions, use the xfr_cost_group_id
6248           -- column from MMTT. For all other transactions, use the cost_group_id
6249           -- For CostGrpTrx, sub and location are same as that of source side.
6250           IF (p_transaction_action_id = inv_globals.g_action_costgroupxfr) THEN
6251             l_xfr_sub  := p_subinventory_code;
6252             l_xfr_loc  := p_locator_id;
6253           ELSE
6254             l_xfr_sub  := p_transfer_subinventory;
6255             l_xfr_loc  := p_transfer_to_location;
6256           END IF;
6257 
6258 	   -- ER 7307189 changes start
6259 	    IF (l_debug = 1) THEN
6260 		    inv_log_util.TRACE('transfer_reserved_lpn_contents p_lpn_id :' || p_lpn_id, 'INV_LPN_TRX_PUB', 1);
6261 		    inv_log_util.TRACE('transfer_reserved_lpn_contents p_transfer_lpn_id :' || p_transfer_lpn_id, 'INV_LPN_TRX_PUB', 1);
6262 		    inv_log_util.TRACE('transfer_reserved_lpn_contents p_transfer_subinventory :' || p_transfer_subinventory, 'INV_LPN_TRX_PUB', 1);
6263 		    inv_log_util.TRACE('transfer_reserved_lpn_contents p_transfer_to_location :' ||p_transfer_to_location , 'INV_LPN_TRX_PUB', 1);
6264 		    inv_log_util.TRACE('transfer_reserved_lpn_contents p_inventory_item_id :' || p_inventory_item_id , 'INV_LPN_TRX_PUB', 1);
6265 	    END IF;
6266 
6267 
6268 
6269 		  inv_lpn_reservations_pvt.transfer_reserved_lpn_contents(
6270 		    x_return_status              => ret_status
6271 		  , x_msg_count                  => ret_msgcnt
6272 		  , x_msg_data                   => ret_msgdata
6273 		  , p_organization_id            => p_organization_id
6274                   , p_inventory_item_id          => p_inventory_item_id
6275 		  , p_lpn_id                     => p_lpn_id
6276 		  , p_transfer_lpn_id            => p_transfer_lpn_id
6277 		  , p_to_subinventory_code       => p_transfer_subinventory
6278 		  , p_to_locator_id              => p_transfer_to_location
6279 		  );
6280 
6281           IF (ret_status <> fnd_api.g_ret_sts_success) THEN
6282             IF (l_debug = 1) THEN
6283               inv_log_util.TRACE('Undefined error in calling transfer_reserved_lpn_contents', 'INV_LPN_TRX_PUB', 1);
6284             END IF;
6285 
6286             RAISE fnd_api.g_exc_error;
6287           END IF;
6288 
6289           IF (l_debug = 1) THEN
6290             inv_log_util.TRACE('Done with call to transfer_reserved_lpn_contents', 'INV_LPN_TRX_PUB', 9);
6291           END IF;
6292 
6293           -- ER 7307189 changes end
6294 
6295           -- Because if the Destination Sub Inventory is not LPN controlled we would skip the call to pack_unpack....bug # 1869761
6296           IF (v_autounpack <> 2) THEN
6297             -- An item or LPN is being packed to another LPN.
6298             Call_Pack_Unpack (
6299               p_tempid           => p_transaction_temp_id
6300             , p_content_lpn      => p_content_lpn_id
6301             , p_lpn              => p_transfer_lpn_id
6302             , p_item_rec         => l_item_rec
6303             , p_revision         => p_revision
6304             , p_primary_qty      => ABS(l_primary_quantity)
6305             , p_qty              => ABS(l_transaction_quantity)
6306             , p_uom              => p_transaction_uom
6307             , p_org_id           => v_xfr_org
6308             , p_subinv           => l_xfr_sub
6309             , p_locator          => l_xfr_loc
6310             , p_operation        => g_pack
6311             , p_cost_grp_id      => NVL(p_transfer_cost_group_id, p_cost_group_id)
6312             , p_trx_action       => NULL
6313             , p_source_header_id => NULL
6314             , p_source_name      => NULL
6315             , p_source_type_id   => NULL
6316             , p_sec_qty          => abs(l_secondary_trx_quantity) --INVCONV kkillams
6317             , p_sec_uom          => p_secondary_uom_code --INVCONV kkillams
6318             , p_source_trx_id    => p_source_transaction_id
6319             );
6320           ELSIF(v_xfrlpn_ctx = wms_container_pub.lpn_context_packing) THEN
6321             -- Since the transfer sub is non lpn controlled, change xfer lpn
6322             -- is not packed into and should become defined but not used again.
6323             v_lpn.organization_id  := v_xfr_org;
6324             v_lpn.lpn_id           := p_transfer_lpn_id;
6325             v_lpn.lpn_context      := wms_container_pub.lpn_context_pregenerated;
6326             update_lpn_status(v_lpn);
6327           END IF;
6328         END IF;
6329 
6330         -- For cases of bulk picking the lpn_id and xfr_lpn_id will be same
6331         -- need to change the sub and locator information to the xfer location
6332         IF (p_transaction_action_id = inv_globals.g_action_stgxfr
6333             AND p_lpn_id = p_transfer_lpn_id) THEN
6334           SELECT lpn_context
6335             INTO v_xfrlpn_ctx
6336             FROM wms_license_plate_numbers
6337            WHERE lpn_id = p_transfer_lpn_id;
6338 
6339 					-- bug 5620764: add debug
6340           IF (l_debug = 1) THEN
6341             inv_log_util.TRACE('Got xfrlpn ctx=' || v_xfrlpn_ctx, 'INV_LPN_TRX_PUB', 5);
6342           END IF;
6343 
6344           -- If the lpn is transfered update the status of that LPN
6345           -- provided the context is 2, 3, or 8
6346           IF (p_transaction_source_type_id <> inv_globals.g_sourcetype_salesorder)
6347              AND(
6348                  (v_xfrlpn_ctx = wms_container_pub.lpn_context_wip)
6349                  OR(v_xfrlpn_ctx = wms_container_pub.lpn_context_rcv)
6350                  OR(v_xfrlpn_ctx = wms_container_pub.lpn_context_packing)
6351                  OR(v_xfrlpn_ctx = wms_container_pub.lpn_context_pregenerated)
6352                 ) THEN
6353             v_lpn.lpn_context  := wms_container_pub.lpn_context_inv;
6354           ELSE
6355           	-- bug 5620764
6356           	-- For staging transfer transactions change context to picked
6357             v_lpn.lpn_context := wms_container_pub.lpn_context_picked;
6358             --v_lpn.lpn_context  := NULL;
6359 
6360             -- Verify that the context of this LPN is 'Resides in Inventory'
6361             IF (
6362                 v_xfrlpn_ctx <> wms_container_pub.lpn_context_inv
6363                 AND v_xfrlpn_ctx <> wms_container_pub.lpn_context_picked
6364                 AND v_xfrlpn_ctx <> wms_container_pub.lpn_context_packing
6365                ) THEN
6366               x_proc_msg  := 'Error. Invalid LPN context for XFR_LPN:' || v_xfrlpn_ctx;
6367 
6368               IF (l_debug = 1) THEN
6369                 inv_log_util.TRACE('Error. Invalid LPN context for XFR_LPN :' || v_xfrlpn_ctx, 'INV_LPN_TRX_PUB', 1);
6370               END IF;
6371 
6372               fnd_message.set_name('INV', 'INV_INVALID_LPN_CONTEXT');
6373               fnd_msg_pub.ADD;
6374               RAISE fnd_api.g_exc_error;
6375             END IF;
6376           END IF;
6377 
6378           IF (l_debug = 1) THEN
6379             inv_log_util.TRACE('Updating lpn=' || p_transfer_lpn_id || ' ctx=' || v_lpn.lpn_context || ' org=' || v_xfr_org
6380             , 'INV_LPN_TRX_PUB', 1);
6381           END IF;
6382 
6383           v_lpn.lpn_id             := p_transfer_lpn_id;
6384           v_lpn.organization_id    := v_xfr_org;
6385           v_lpn.subinventory_code  := p_transfer_subinventory;
6386           v_lpn.locator_id         := p_transfer_to_location;
6387           update_lpn_status(v_lpn);
6388         END IF;
6389 
6390         -- If transaction is subtransfer
6391         IF ( p_transaction_action_id = inv_globals.g_action_subxfr OR
6392              p_transaction_action_id = inv_globals.g_action_planxfr )
6393         THEN
6394           IF (p_content_lpn_id IS NOT NULL) THEN
6395             SELECT lpn_context
6396               INTO v_cnt_lpn_ctx
6397               FROM wms_license_plate_numbers
6398              WHERE lpn_id = p_content_lpn_id;
6399 
6400             IF (l_debug = 1) THEN
6401               inv_log_util.TRACE('SUBXFER cont LPN context=' || v_cnt_lpn_ctx, 'INV_LPN_TRX_PUB', 9);
6402             END IF;
6403 
6404             IF  (v_cnt_lpn_ctx = wms_container_pub.lpn_context_picked)
6405               OR (NVL(l_system_task_type, -1) = 7) THEN  --Bug 3620318
6406               --A call to transfer the reservation is also needed if LPN is of the picked context
6407               --Consolidating lpn across subinventories, must transfer reservations
6408               inv_reservation_pvt.transfer_lpn_trx_reservation(
6409                 x_return_status              => ret_status
6410               , x_msg_count                  => ret_msgcnt
6411               , x_msg_data                   => ret_msgdata
6412               , p_transaction_temp_id        => p_transaction_temp_id
6413               , p_organization_id            => p_organization_id
6414               , p_lpn_id                     => p_content_lpn_id
6415               , p_from_subinventory_code     => p_subinventory_code
6416               , p_from_locator_id            => p_locator_id
6417               , p_to_subinventory_code       => p_transfer_subinventory
6418               , p_to_locator_id              => p_transfer_to_location
6419               );
6420 
6421               IF (ret_status <> fnd_api.g_ret_sts_success) THEN
6422                 IF (l_debug = 1) THEN
6423                   inv_log_util.TRACE('**Error from transfer_lpn_trx_reservation :' || ret_status, 'INV_LPN_TRX_PUB', 1);
6424                 END IF;
6425 
6426                 fnd_message.set_name('INV', 'INV_XFR_RSV_FAILURE');
6427                 fnd_msg_pub.ADD;
6428                 RAISE fnd_api.g_exc_error;
6429               END IF;
6430             END IF;
6431           END IF;
6432         END IF;
6433       /***** Actions for PACK transactions  ****/
6434       ELSIF(p_transaction_action_id = inv_globals.g_action_containerpack) THEN
6435         IF (l_debug = 1) THEN
6436           inv_log_util.TRACE(' Action = Pack', 'INV_LPN_TRX_PUB', 9);
6437         END IF;
6438 
6439         Call_Pack_Unpack (
6440           p_tempid           => p_transaction_temp_id
6441         , p_content_lpn      => p_content_lpn_id
6442         , p_lpn              => p_transfer_lpn_id
6443         , p_item_rec         => l_item_rec
6444         , p_revision         => p_revision
6445         , p_primary_qty      => ABS(l_primary_quantity)
6446         , p_qty              => ABS(l_transaction_quantity)
6447         , p_uom              => p_transaction_uom
6448         , p_org_id           => p_organization_id
6449         , p_subinv           => p_subinventory_code
6450         , p_locator          => p_locator_id
6451         , p_operation        => g_pack
6452         , p_cost_grp_id      => p_cost_group_id
6453         , p_trx_action       => NULL
6454         , p_source_header_id => NULL
6455         , p_source_name      => NULL
6456         , p_source_type_id   => NULL
6457         , p_sec_qty          => abs(l_secondary_trx_quantity) --INVCONV kkillams
6458         , p_sec_uom          => p_secondary_uom_code --INVCONV kkillams
6459         , p_source_trx_id    => p_source_transaction_id
6460         );
6461 
6462         -- It is possible that the LPN to which the packing is done could be in
6463         -- Receiving. However, after packing, the LPN has to be Inventory.
6464         -- Note: Since only the context of LPN is changed, directly modifying on Table
6465         -- instead of calling  modify_LPN API except when the context is picking
6466         UPDATE wms_license_plate_numbers
6467            SET lpn_context = wms_container_pub.lpn_context_inv
6468          WHERE lpn_id = p_transfer_lpn_id
6469            AND lpn_context <> wms_container_pub.lpn_context_picked;
6470       /***** Actions for UNPACK transactions  ****/
6471       ELSIF(p_transaction_action_id = inv_globals.g_action_containerunpack) THEN
6472         IF (l_debug = 1) THEN
6473           inv_log_util.TRACE(' Action = UnPack', 'INV_LPN_TRX_PUB', 9);
6474         END IF;
6475 
6476         Call_Pack_Unpack (
6477           p_tempid           => p_transaction_temp_id
6478         , p_content_lpn      => p_content_lpn_id
6479         , p_lpn              => p_lpn_id
6480         , p_item_rec         => l_item_rec
6481         , p_revision         => p_revision
6482         , p_primary_qty      => ABS(l_primary_quantity)
6483         , p_qty              => ABS(l_transaction_quantity)
6484         , p_uom              => p_transaction_uom
6485         , p_org_id           => p_organization_id
6486         , p_subinv           => p_subinventory_code
6487         , p_locator          => p_locator_id
6488         , p_operation        => g_unpack
6489         , p_cost_grp_id      => p_cost_group_id
6490         , p_trx_action       => NULL
6491         , p_source_header_id => NULL
6492         , p_source_name      => NULL
6493         , p_source_type_id   => NULL
6494         , p_sec_qty          => abs(l_secondary_trx_quantity) --INVCONV kkillams
6495         , p_sec_uom          => p_secondary_uom_code --INVCONV kkillams
6496         , p_source_trx_id    => p_source_transaction_id
6497         );
6498       -- If LPN is item-tracked, then need to change container status. TODO
6499 
6500       /***** Actions for SLPIT transactions  ****/
6501       ELSIF(p_transaction_action_id = inv_globals.g_action_containersplit) THEN
6502         IF (l_debug = 1) THEN
6503           inv_log_util.TRACE(' Action = Split, First Unpack', 'INV_LPN_TRX_PUB', 9);
6504         END IF;
6505 
6506         IF (p_content_lpn_id IS NULL) THEN
6507           SELECT lpn_context
6508             INTO v_lpn_ctx
6509             FROM wms_license_plate_numbers
6510            WHERE lpn_id = p_lpn_id;
6511 
6512           IF (v_lpn_ctx = wms_container_pub.lpn_context_picked) THEN
6513             split_delivery(
6514               p_tempid                     => p_transaction_temp_id
6515             , p_lpn_id                     => p_lpn_id
6516             , p_xfr_lpn_id                 => p_transfer_lpn_id
6517             , p_item_rec                   => l_item_rec
6518             , p_revision                   => p_revision
6519             , p_qty                        => l_primary_quantity
6520             , p_uom                        => l_item_rec.primary_uom_code
6521             , p_secondary_trx_quantity     => l_secondary_trx_quantity  --INVCONV kkillams
6522             , p_secondary_uom_code         => p_secondary_uom_code
6523             , p_org_id                     => p_organization_id
6524             , p_subinventory_code          => p_subinventory_code
6525             , p_locator_id                 => p_locator_id
6526             , p_transaction_source_id      => p_transaction_source_id
6527             , p_trx_source_line_id         => p_trx_source_line_id
6528             );
6529           END IF;
6530 
6531           -- It is possible that the LPN to which the packing is done could be in
6532           -- Receiving. However, after packing, the LPN has to be Inventory.
6533           -- Note: Since only the context of LPN is changed, directly modifying on Table
6534           -- instead of calling  modify_LPN API
6535           SELECT lpn_context
6536             INTO v_xfrlpn_ctx
6537             FROM wms_license_plate_numbers
6538            WHERE lpn_id = p_transfer_lpn_id;
6539 
6540           -- If context is pre-generated, then inherit the context of the contentLPN or fromLPN
6541           -- If that context is INV or PICKED
6542           IF (
6543               v_xfrlpn_ctx = wms_container_pub.lpn_context_pregenerated
6544               AND v_lpn_ctx NOT IN
6545                    (
6546                     wms_container_pub.lpn_context_wip
6547                   , wms_container_pub.lpn_context_rcv
6548                   , wms_container_pub.lpn_context_packing
6549                   , wms_container_pub.lpn_context_vendor
6550                    )
6551              ) THEN
6552             v_lpn.lpn_id       := p_transfer_lpn_id;
6553             v_lpn.lpn_context  := v_lpn_ctx;
6554             update_lpn_status(v_lpn);
6555           END IF;
6556         END IF;
6557 
6558         Call_Pack_Unpack (
6559           p_tempid           => p_transaction_temp_id
6560         , p_content_lpn      => p_content_lpn_id
6561         , p_lpn              => p_lpn_id
6562         , p_item_rec         => l_item_rec
6563         , p_revision         => p_revision
6564         , p_primary_qty      => ABS(l_primary_quantity)
6565         , p_qty              => ABS(l_transaction_quantity)
6566         , p_uom              => p_transaction_uom
6567         , p_org_id           => p_organization_id
6568         , p_subinv           => p_subinventory_code
6569         , p_locator          => p_locator_id
6570         , p_operation        => g_unpack
6571         , p_cost_grp_id      => p_cost_group_id
6572         , p_trx_action       => NULL
6573         , p_source_header_id => NULL
6574         , p_source_name      => NULL
6575         , p_source_type_id   => NULL
6576         , p_sec_qty          => abs(l_secondary_trx_quantity) --INVCONV kkillams
6577         , p_sec_uom          => p_secondary_uom_code --INVCONV kkillams
6578         , p_source_trx_id    => p_source_transaction_id
6579         );
6580         Call_Pack_Unpack (
6581           p_tempid           => p_transaction_temp_id
6582         , p_content_lpn      => p_content_lpn_id
6583         , p_lpn              => p_transfer_lpn_id
6584         , p_item_rec         => l_item_rec
6585         , p_revision         => p_revision
6586         , p_primary_qty      => ABS(l_primary_quantity)
6587         , p_qty              => ABS(l_transaction_quantity)
6588         , p_uom              => p_transaction_uom
6589         , p_org_id           => p_organization_id
6590         , p_subinv           => p_subinventory_code
6591         , p_locator          => p_locator_id
6592         , p_operation        => g_pack
6593         , p_cost_grp_id      => p_cost_group_id
6594         , p_trx_action       => NULL
6595         , p_source_header_id => NULL
6596         , p_source_name      => NULL
6597         , p_source_type_id   => NULL
6598         , p_sec_qty          => abs(l_secondary_trx_quantity) --INVCONV kkillams
6599         , p_sec_uom          => p_secondary_uom_code --INVCONV kkillams
6600         , p_source_trx_id    => p_source_transaction_id
6601         );
6602       END IF;   -- end of if loop based on transaction_type
6603      -- Bug 2392622 removed for now cannot be updated before TM call should be done in MMT
6604      -- If Sales Order Issue or Int.Order Issue/SubXfr/Intransit Shipment
6605      -- need to popluate the content_lpn in MMTT with the outermost lpn
6606      -- for shipping
6607      /*if p_content_lpn_id is not NULL AND
6608       ((p_transaction_source_type_id = inv_globals.G_SourceType_SalesOrder AND
6609        p_transaction_action_id = inv_globals.G_Action_Issue) OR
6610       (p_transaction_source_type_id = inv_globals.G_SourceType_IntOrder AND
6611       (p_transaction_action_id = inv_globals.G_Action_Issue OR
6612        p_transaction_action_id =
6613        inv_globals.G_Action_Subxfr OR
6614      p_transaction_action_id = inv_globals.G_Action_Planxfr OR
6615      p_transaction_action_id = inv_globals.G_Action_Orgxfr OR
6616     p_transaction_action_id = inv_globals.G_Action_IntransitShipment))) then
6617      BEGIN
6618          UPDATE mtl_material_transactions_temp
6619          SET content_lpn_id = (SELECT outermost_lpn_id
6620                    FROM wms_license_plate_numbers
6621                    WHERE lpn_id = p_content_lpn_id
6622                    AND rownum < 2)
6623          WHERE transaction_temp_id = p_transaction_temp_id;
6624      EXCEPTION
6625       when others then
6626            IF (l_debug = 1) THEN
6627               inv_log_util.trace('Error updating content_lpn_id in MMTT line ttid='||p_transaction_temp_id, 'INV_LPN_TRX_PUB', 9);
6628            END IF;
6629          END;
6630      end if;*/
6631     END IF;   --lpn_id = null AND content_lpn_id = null and xfr_lpn_id = null
6632 
6633     l_progress := 'Call to Label Printing API if p_business_flow_code is passed';
6634     IF (NVL(p_business_flow_code, 0) <> 0) THEN
6635 
6636     -- Start of fix for Bug: 4891916
6637     IF (l_debug = 1) THEN
6638        inv_log_util.TRACE('Value of Business flow:'|| p_business_flow_code || 'l_print_label:'
6639                                                    || l_print_label, 'INV_LPN_TRX_PUB', 1);
6640     END IF;
6641     -- Checking for the business flow and the value of the profile
6642     -- and setting the value of the transaction identifier accordingly.
6643 
6644     IF ((p_business_flow_code = 8) AND (l_print_label IN (2,3))) THEN
6645          l_transaction_identifier:= 5;
6646     END IF;
6647 
6648     -- End of fix for Bug: 4891916
6649 
6650     /* Calling with the value of p_transaction_identifier as 5. In the label printing code,
6651        this value will indicate that the call is at the time of approving the cycle count
6652        entry. The value of p_transaction_identifier 4 in the label printing api indicates
6653        that the call is at the time of performing the cycle count entry.*/
6654 
6655       inv_label.print_label_wrap(
6656         x_return_status              => ret_status
6657       , x_msg_count                  => ret_msgcnt
6658       , x_msg_data                   => ret_msgdata
6659       , x_label_status               => label_status
6660       , p_business_flow_code         => p_business_flow_code
6661       , p_transaction_id             => p_transaction_temp_id
6662       , p_transaction_identifier     => l_transaction_identifier  --Added for Bug 4891916
6663       );
6664 
6665       IF (ret_status <> fnd_api.g_ret_sts_success) THEN
6666         IF (l_debug = 1) THEN
6667           inv_log_util.TRACE('**Error Label Printing :' || ret_status, 'INV_LPN_TRX_PUB', 1);
6668         END IF;
6669       --RAISE FND_API.G_EXC_ERROR; Since TM should not fail, even if
6670       --this call fails bug#2555226
6671       END IF;
6672     END IF;
6673 
6674     -- Bug 2767841. Pick Drop is causing delivery lines to split.
6675     --  The reason is that after the content LPN is exploded, the original MMTT line
6676     --  is not deleted, so finalize_pick_confirm counts it with the child record so
6677     --  the requested quantity doubled and then causing the delivery line to split
6678     --  After all the LPN related txn is done, delete the original MMTT record that
6679     --  has inventory_item_id = -1
6680     IF p_inventory_item_id = -1 THEN
6681       IF (l_debug = 1) THEN
6682         inv_log_util.TRACE('* Done with process_lpn_trx_line, deleting MMTT record for tmpID=' || p_transaction_temp_id, 'INV_LPN_TRX_PUB'
6683         , 1);
6684       END IF;
6685 
6686       DELETE FROM mtl_serial_numbers_temp
6687             WHERE transaction_temp_id = (SELECT transaction_temp_id
6688                                            FROM mtl_material_transactions_temp
6689                                           WHERE transaction_temp_id = p_transaction_temp_id
6690                                             AND inventory_item_id = -1);
6691 
6692       IF SQL%ROWCOUNT = 0 THEN
6693         DELETE FROM mtl_serial_numbers_temp
6694               WHERE transaction_temp_id IN(
6695                                    SELECT serial_transaction_temp_id
6696                                      FROM mtl_transaction_lots_temp
6697                                     WHERE transaction_temp_id =
6698                                                                (SELECT transaction_temp_id
6699                                                                   FROM mtl_material_transactions_temp
6700                                                                  WHERE transaction_temp_id = p_transaction_temp_id
6701                                                                    AND inventory_item_id = -1));
6702       END IF;
6703 
6704       IF (l_debug = 1) THEN
6705         inv_log_util.TRACE('* No of MSNT records deleted =' || SQL%ROWCOUNT, 'INV_LPN_TRX_PUB', 1);
6706       END IF;
6707 
6708       DELETE FROM mtl_transaction_lots_temp
6709             WHERE transaction_temp_id = (SELECT transaction_temp_id
6710                                            FROM mtl_material_transactions_temp
6711                                           WHERE transaction_temp_id = p_transaction_temp_id
6712                                             AND inventory_item_id = -1);
6713 
6714       IF (l_debug = 1) THEN
6715         inv_log_util.TRACE('* No of MTLT records deleted =' || SQL%ROWCOUNT, 'INV_LPN_TRX_PUB', 1);
6716       END IF;
6717 
6718       DELETE FROM mtl_material_transactions_temp
6719             WHERE transaction_temp_id = p_transaction_temp_id
6720               AND inventory_item_id = -1;
6721 
6722       IF (l_debug = 1) THEN
6723         inv_log_util.TRACE('* No. of MMTT record deleted ' || SQL%ROWCOUNT, 'INV_LPN_TRX_PUB', 1);
6724       END IF;
6725     END IF;
6726   EXCEPTION
6727     WHEN fnd_api.g_exc_error THEN
6728       x_return_status  := fnd_api.g_ret_sts_error;
6729       x_proc_msg       := fnd_msg_pub.get(fnd_msg_pub.g_previous, 'F');
6730 
6731       IF (l_debug = 1) THEN
6732         inv_log_util.TRACE(l_api_name || ' Exc err prog=' || l_progress || ' SQL err: ' || SQLERRM(SQLCODE), l_api_name, 1);
6733         fnd_msg_pub.count_and_get(p_count => ret_msgcnt, p_data => ret_msgdata);
6734 
6735         FOR i IN 1 .. ret_msgcnt LOOP
6736           l_msgdata  := SUBSTR(l_msgdata || ' | ' || SUBSTR(fnd_msg_pub.get(ret_msgcnt - i + 1, 'F'), 0, 200), 1, 2000);
6737         END LOOP;
6738 
6739         inv_log_util.TRACE('msg: ' || l_msgdata, l_api_name, 1);
6740       END IF;
6741     WHEN OTHERS THEN
6742       x_return_status  := fnd_api.g_ret_sts_unexp_error;
6743       x_proc_msg       := fnd_msg_pub.get(fnd_msg_pub.g_previous, 'F');
6744 
6745       IF (l_debug = 1) THEN
6746         inv_log_util.TRACE(l_api_name || ' Unexp err prog=' || l_progress || ' SQL err: ' || SQLERRM(SQLCODE), l_api_name, 1);
6747         inv_log_util.TRACE('msg=' || x_proc_msg, l_api_name, 1);
6748       END IF;
6749   END process_lpn_trx_line;
6750 
6751 END inv_lpn_trx_pub;