DBA Data[Home] [Help]

PACKAGE BODY: APPS.CSI_INV_ISO_PKG

Source


1 package body CSI_INV_ISO_PKG as
2 -- $Header: csiintsb.pls 120.9.12020000.3 2012/11/09 11:26:20 mvaradam ship $
3 
4 l_debug NUMBER := csi_t_gen_utility_pvt.g_debug_level;
5 
6    PROCEDURE iso_shipment(p_transaction_id     IN  NUMBER,
7                           p_message_id         IN  NUMBER,
8                           x_return_status      OUT NOCOPY VARCHAR2,
9                           x_trx_error_rec      OUT NOCOPY CSI_DATASTRUCTURES_PUB.TRANSACTION_ERROR_REC)
10    IS
11 
12    l_mtl_item_tbl               CSI_INV_TRXS_PKG.MTL_ITEM_TBL_TYPE;
13    l_api_name                   VARCHAR2(100)   := 'CSI_INV_TRXS_PKG.ISO_SHIPMENT';
17    l_validation_level           NUMBER          := FND_API.G_VALID_LEVEL_FULL;
14    l_api_version                NUMBER          := 1.0;
15    l_commit                     VARCHAR2(1)     := FND_API.G_FALSE;
16    l_init_msg_list              VARCHAR2(1)     := FND_API.G_TRUE;
18    l_active_instance_only       VARCHAR2(10)    := FND_API.G_TRUE;
19    l_inactive_instance_only     VARCHAR2(10)    := FND_API.G_FALSE;
20    l_expire_children            VARCHAR2(1)     := FND_API.G_FALSE;
21    l_transaction_id             NUMBER          := NULL;
22    l_resolve_id_columns         VARCHAR2(10)    := FND_API.G_FALSE;
23    l_object_version_number      NUMBER          := 1;
24    l_sysdate                    DATE            := SYSDATE;
25    l_master_organization_id     NUMBER;
26    l_depreciable                VARCHAR2(1);
27    l_instance_query_rec         CSI_DATASTRUCTURES_PUB.INSTANCE_QUERY_REC;
28    l_dest_instance_query_rec    CSI_DATASTRUCTURES_PUB.INSTANCE_QUERY_REC;
29    l_update_instance_rec        CSI_DATASTRUCTURES_PUB.INSTANCE_REC;
30    l_upd_src_dest_instance_rec  CSI_DATASTRUCTURES_PUB.INSTANCE_REC;
31    l_update_dest_instance_rec   CSI_DATASTRUCTURES_PUB.INSTANCE_REC;
32    l_new_instance_rec           CSI_DATASTRUCTURES_PUB.INSTANCE_REC;
33    l_new_dest_instance_rec      CSI_DATASTRUCTURES_PUB.INSTANCE_REC;
34    l_new_src_instance_rec       CSI_DATASTRUCTURES_PUB.INSTANCE_REC;
35    l_expire_instance_rec        CSI_DATASTRUCTURES_PUB.INSTANCE_REC;
36    l_txn_rec                    CSI_DATASTRUCTURES_PUB.TRANSACTION_REC;
37    l_exp_txn_rec                CSI_DATASTRUCTURES_PUB.TRANSACTION_REC;
38    l_return_status              VARCHAR2(1);
39    l_error_code                 VARCHAR2(50);
40    l_error_message              VARCHAR2(4000);
41    l_instance_id_lst            CSI_DATASTRUCTURES_PUB.ID_TBL;
42    l_party_query_rec            CSI_DATASTRUCTURES_PUB.PARTY_QUERY_REC;
43    l_account_query_rec          CSI_DATASTRUCTURES_PUB.PARTY_ACCOUNT_QUERY_REC;
44    l_instance_header_tbl        CSI_DATASTRUCTURES_PUB.INSTANCE_HEADER_TBL;
45    l_src_instance_header_tbl    CSI_DATASTRUCTURES_PUB.INSTANCE_HEADER_TBL;
46    l_dest_instance_header_tbl   CSI_DATASTRUCTURES_PUB.INSTANCE_HEADER_TBL;
47    l_ext_attrib_values_tbl      CSI_DATASTRUCTURES_PUB.EXTEND_ATTRIB_VALUES_TBL;
48    l_party_tbl                  CSI_DATASTRUCTURES_PUB.PARTY_TBL;
49    l_account_tbl                CSI_DATASTRUCTURES_PUB.PARTY_ACCOUNT_TBL;
50    l_pricing_attrib_tbl         CSI_DATASTRUCTURES_PUB.PRICING_ATTRIBS_TBL;
51    l_org_assignments_tbl        CSI_DATASTRUCTURES_PUB.ORGANIZATION_UNITS_TBL;
52    l_asset_assignment_tbl       CSI_DATASTRUCTURES_PUB.INSTANCE_ASSET_TBL;
53    l_sub_inventory              VARCHAR2(10);
54    l_location_type              VARCHAR2(20);
55    l_trx_action_type            VARCHAR2(50);
56    l_fnd_success                VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
57    l_fnd_warning                VARCHAR2(1) := 'W';
58    l_fnd_error                  VARCHAR2(1) := FND_API.G_RET_STS_ERROR;
59    l_fnd_unexpected             VARCHAR2(1) := FND_API.G_RET_STS_UNEXP_ERROR;
60    l_fnd_g_num                  NUMBER      := FND_API.G_MISS_NUM;
61    l_fnd_g_char                 VARCHAR2(1) := FND_API.G_MISS_CHAR;
62    l_fnd_g_date                 DATE        := FND_API.G_MISS_DATE;
63    l_in_inventory               VARCHAR2(25) := CSI_INV_TRXS_PKG.G_IN_INVENTORY;
64    l_in_transit                 VARCHAR2(25) := CSI_INV_TRXS_PKG.G_IN_TRANSIT;
65    l_returned                   VARCHAR2(25) := 'RETURNED';
66    l_out_of_enterprise          VARCHAR2(25) := 'OUT_OF_ENTERPRISE';
67    l_instance_usage_code        VARCHAR2(25);
68    l_organization_id            NUMBER;
69    l_subinventory_name          VARCHAR2(10);
70    l_locator_id                 NUMBER;
71    l_transaction_error_id       NUMBER;
72    l_trx_type_id                NUMBER;
73    l_trans_type_code            VARCHAR2(25);
74    l_trans_app_code             VARCHAR2(5);
75    l_employee_id                NUMBER;
76    l_file                       VARCHAR2(500);
77    l_msg_count                  NUMBER;
78    l_msg_data                   VARCHAR2(2000);
79    l_sql_error                  VARCHAR2(2000);
80    l_msg_index                  NUMBER;
81    j                            PLS_INTEGER := 1;
82    i                            PLS_INTEGER := 1;
83    l_tbl_count                  NUMBER := 0;
84    l_neg_code                   NUMBER := 0;
85    l_instance_status            VARCHAR2(1);
86    l_sr_control                 NUMBER;
87    l_mfg_flag                   VARCHAR2(1)  := NULL;
88    l_serial_number              VARCHAR2(30) := NULL;
89    l_trans_quantity             NUMBER := 0;
90    l_quantity                   NUMBER := 0;
91    l_redeploy_flag              VARCHAR2(1);
92    l_upd_error_instance_id      NUMBER := NULL;
93    l_receipt_action_flag        VARCHAR2(1) := NULL;
94    l_curr_object_vers_61_id     NUMBER := NULL;
95 
96    cursor c_id is
97      SELECT instance_status_id
98      FROM   csi_instance_statuses
99      WHERE  name = FND_PROFILE.VALUE('CSI_DEFAULT_INSTANCE_STATUS');
100 
101    r_id     c_id%rowtype;
102 
103    cursor c_inst (pc_instance_id in NUMBER) is
104      SELECT instance_usage_code
105      FROM   csi_item_instances
106      WHERE  instance_id = pc_instance_id;
107 
108    r_inst   c_inst%rowtype;
109 
110    CURSOR c_so_info (pc_line_id in NUMBER) is
111      SELECT oeh.header_id,
112             oel.line_id,
113             oeh.order_number,
114             oel.line_number
115      FROM   oe_order_headers_all oeh,
116             oe_order_lines_all oel
117      WHERE oeh.header_id = oel.header_id
118      AND   oel.line_id = pc_line_id;
119 
120    r_so_info     c_so_info%rowtype;
121 
125      SELECT serial_number_control_code,
122    CURSOR c_item_control (pc_item_id in number,
123                           pc_org_id in number,
124 					 p_transaction_id in number) is -- Added for bug#14835893
126            -- lot_control_code,
127 		  nvl(csi_utl_pkg.get_lot_ctrl_code(p_transaction_id),lot_control_code) lot_control_code,-- Added for bug#14835893
128             revision_qty_control_code,
129             location_control_code,
130             comms_nl_trackable_flag
134 
131      FROM mtl_system_items_b
132      WHERE inventory_item_id = pc_item_id
133      AND organization_id = pc_org_id;
135    r_item_control     c_item_control%rowtype;
136 
137    BEGIN
138 
139      x_return_status := l_fnd_success;
140      l_error_message := NULL;
141 
142      IF (l_debug > 0) THEN
143            csi_t_gen_utility_pvt.add('******Start of csi_inv_iso_pkg.iso_shipment Transaction procedure******');
144            csi_t_gen_utility_pvt.add('Start time: '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SS'));
145            csi_t_gen_utility_pvt.add('csiintsb.pls 115.27');
146      END IF;
147 
148      IF (l_debug > 0) THEN
149        csi_t_gen_utility_pvt.add('Transaction ID with is: '||p_transaction_id);
150        csi_t_gen_utility_pvt.add('l_sysdate set to: '||to_char(l_sysdate,'DD-MON-YYYY HH24:MI:SS'));
151      END IF;
152 
153      -- This procedure queries all of the Inventory Transaction Records and
154      -- returns them as a table.
155 
156      csi_inv_trxs_pkg.get_transaction_recs(p_transaction_id,
157                                            l_mtl_item_tbl,
158                                            l_return_status,
159                                            l_error_message);
160 
161      l_tbl_count := 0;
162      l_tbl_count := l_mtl_item_tbl.count;
163      IF (l_debug > 0) THEN
164            csi_t_gen_utility_pvt.add('Inventory Records Found: '||l_tbl_count);
165      END IF;
166 
167      IF NOT l_return_status = l_fnd_success THEN
168        IF (l_debug > 0) THEN
169              csi_t_gen_utility_pvt.add('You have encountered an error in CSI_INV_TRXS_PKG.get_transaction_recs, Transaction ID: '||p_transaction_id);
170        END IF;
171        RAISE fnd_api.g_exc_error;
172      END IF;
173 
174      IF (l_debug > 0) THEN
175       	  csi_t_gen_utility_pvt.add('Transaction Action ID: '||l_mtl_item_tbl(i).transaction_action_id);
176       	  csi_t_gen_utility_pvt.add('Transaction Source Type ID: '||l_mtl_item_tbl(i).transaction_source_type_id);
177       	  csi_t_gen_utility_pvt.add('Transaction Quantity: '||l_mtl_item_tbl(i).transaction_quantity);
178      END IF;
179 
180      -- Get the Master Organization ID
181      CSI_INV_TRXS_PKG.get_master_organization(l_mtl_item_tbl(i).organization_id,
182                                               l_master_organization_id,
183                                               l_return_status,
184                                               l_error_message);
185 
186      IF (l_debug > 0) THEN
187            csi_t_gen_utility_pvt.add('Master Org ID: '||l_master_organization_id);
188      END IF;
189 
190      IF NOT l_return_status = l_fnd_success THEN
191        IF (l_debug > 0) THEN
192              csi_t_gen_utility_pvt.add('You have encountered an error in csi_inv_trxs_pkg.get_master_organization, Organization ID: '||l_mtl_item_tbl(i).organization_id);
193        END IF;
194        RAISE fnd_api.g_exc_error;
195      END IF;
196 
197      -- Call get_fnd_employee_id and get the employee id
198      l_employee_id := csi_inv_trxs_pkg.get_fnd_employee_id(l_mtl_item_tbl(i).last_updated_by);
199 
200      IF l_employee_id = -1 THEN
201        IF (l_debug > 0) THEN
202              csi_t_gen_utility_pvt.add('The person who last updated this record: '||l_mtl_item_tbl(i).last_updated_by||' does not exist as a valid employee');
203        END IF;
204      END IF;
205      IF (l_debug > 0) THEN
206            csi_t_gen_utility_pvt.add('The Employee that is processing this Transaction is: '||l_employee_id);
207      END IF;
208 
209      -- See if this is a depreciable Item to set the status of the transaction record
210      csi_inv_trxs_pkg.check_depreciable(l_mtl_item_tbl(i).inventory_item_id,
211      	                                l_depreciable,
212 										l_mtl_item_tbl(i).organization_id); --Added for Bug 13988660
213 
214      IF (l_debug > 0) THEN
215            csi_t_gen_utility_pvt.add('Is this Item ID: '||l_mtl_item_tbl(i).inventory_item_id||', Depreciable :'||l_depreciable);
216      END IF;
217 
218 	-- Get the Negative Receipt Code to see if this org allows Negative
219 	-- Quantity Records 1 = Yes, 2 = No
220 
221 	l_neg_code := csi_inv_trxs_pkg.get_neg_inv_code(
222                                         l_mtl_item_tbl(i).organization_id);
223 
227 	 l_instance_status := FND_API.G_TRUE;
224      IF l_neg_code = 1 AND l_mtl_item_tbl(i).serial_number_control_code in (1,6) THEN
225 	 l_instance_status := FND_API.G_FALSE;
226      ELSE
228      END IF;
229 
230      IF (l_debug > 0) THEN
231            csi_t_gen_utility_pvt.add('Negative Code is - 1 = Yes, 2 = No: '||l_neg_code);
232      END IF;
233 
234      -- Determine Transaction Type for this
235 
236      l_trans_type_code := 'ISO_SHIPMENT';
237      l_trans_app_code := 'INV';
238 
239      IF (l_debug > 0) THEN
240            csi_t_gen_utility_pvt.add('Trans Type Code: '||l_trans_type_code);
241            csi_t_gen_utility_pvt.add('Trans App Code: '||l_trans_app_code);
242      END IF;
243 
244      -- Now loop through the PL/SQL Table.
245      j := 1;
246 
247      -- Added so that the SO_HEADER_ID and SO_LINE_ID can be added to
248      -- the transaction record.
249 
250      OPEN c_so_info (l_mtl_item_tbl(j).trx_source_line_id);
251      FETCH c_so_info into r_so_info;
252      CLOSE c_so_info;
253 
254      IF (l_debug > 0) THEN
255            csi_t_gen_utility_pvt.add('Sales Order Header: '||r_so_info.header_id);
256            csi_t_gen_utility_pvt.add('Sales Order Line: '||r_so_info.line_id);
257            csi_t_gen_utility_pvt.add('Order Number: '||r_so_info.order_number);
258            csi_t_gen_utility_pvt.add('Line Number: '||r_so_info.line_number);
259      END IF;
260 
261      IF (l_debug > 0) THEN
262            csi_t_gen_utility_pvt.add('Starting to loop through Material Transaction Records');
263      END IF;
264 
265     -- Get Default Profile Instance Status
266 
267     OPEN c_id;
268     FETCH c_id into r_id;
269     CLOSE c_id;
270 
271     IF (l_debug > 0) THEN
272       csi_t_gen_utility_pvt.add('Default Profile Status: '||r_id.instance_status_id);
273     END IF;
274 
275      -- Initialize Transaction Record
276      l_txn_rec                          := csi_inv_trxs_pkg.init_txn_rec;
277 
278      -- Set Status based on redeployment
279      IF l_depreciable = 'N' THEN
280        IF l_mtl_item_tbl(i).serial_number is NOT NULL THEN
281          csi_inv_trxs_pkg.get_redeploy_flag(l_mtl_item_tbl(i).inventory_item_id,
282                                             l_mtl_item_tbl(i).serial_number,
283                                             l_sysdate,
284                                             l_redeploy_flag,
288        IF l_redeploy_flag = 'Y' THEN
285                                             l_return_status,
286                                             l_error_message);
287        END IF;
289          l_txn_rec.transaction_status_code := csi_inv_trxs_pkg.g_pending;
290        ELSE
291          l_txn_rec.transaction_status_code := csi_inv_trxs_pkg.g_complete;
292        END IF;
293      ELSE
294        l_txn_rec.transaction_status_code := csi_inv_trxs_pkg.g_pending;
295      END IF;
296 
297      IF NOT l_return_status = l_fnd_success THEN
298        IF (l_debug > 0) THEN
299           csi_t_gen_utility_pvt.add('Redeploy Flag: '||l_redeploy_flag);
300           csi_t_gen_utility_pvt.add('You have encountered an error in csi_inv_trxs_pkg.get_redeploy_flag: '||l_error_message);
301        END IF;
302        RAISE fnd_api.g_exc_error;
303      END IF;
304 
305      IF (l_debug > 0) THEN
306         csi_t_gen_utility_pvt.add('Redeploy Flag: '||l_redeploy_flag);
307         csi_t_gen_utility_pvt.add('Trans Status Code: '||l_txn_rec.transaction_status_code);
308      END IF;
309 
310      -- Create CSI Transaction to be used
311      l_txn_rec.source_transaction_date  := l_mtl_item_tbl(i).transaction_date;
312      l_txn_rec.transaction_date         := l_sysdate;
313      l_txn_rec.transaction_type_id      :=
314           csi_inv_trxs_pkg.get_txn_type_id(l_trans_type_code,l_trans_app_code);
315      l_txn_rec.transaction_quantity     :=
316           l_mtl_item_tbl(i).transaction_quantity;
317      l_txn_rec.transaction_uom_code     :=  l_mtl_item_tbl(i).transaction_uom;
318      l_txn_rec.transacted_by            :=  l_employee_id;
319      l_txn_rec.transaction_action_code  :=  NULL;
320      l_txn_rec.message_id               :=  p_message_id;
321      l_txn_rec.inv_material_transaction_id  :=  p_transaction_id;
322      l_txn_rec.object_version_number    :=  l_object_version_number;
323      l_txn_rec.source_header_ref_id     :=  r_so_info.header_id;
324      l_txn_rec.source_line_ref_id       :=  r_so_info.line_id;
325      l_txn_rec.source_header_ref        :=  to_char(r_so_info.order_number);
326      l_txn_rec.source_line_ref          :=  substr(to_char(r_so_info.line_number)||'.'||l_mtl_item_tbl(i).shipment_number,1,50);
327 
331 
328      csi_inv_trxs_pkg.create_csi_txn(l_txn_rec,
329                                      l_error_message,
330                                      l_return_status);
332      IF (l_debug > 0) THEN
333         csi_t_gen_utility_pvt.add('CSI Transaction Created: '||l_txn_rec.transaction_id);
334      END IF;
335 
336      IF NOT l_return_status = l_fnd_success THEN
337        IF (l_debug > 0) THEN
338           csi_t_gen_utility_pvt.add('You have encountered an error in csi_inv_trxs_pkg.create_csi_txn: '||p_transaction_id);
339        END IF;
340        RAISE fnd_api.g_exc_error;
341      END IF;
342 
343      FOR j in l_mtl_item_tbl.FIRST .. l_mtl_item_tbl.LAST LOOP
344 
345      -- Get Receiving Organization Item Master Control Codes
346      OPEN c_item_control (l_mtl_item_tbl(j).inventory_item_id,
347                           l_mtl_item_tbl(j).transfer_organization_id
348 					 ,p_transaction_id);--Added for bug# 14835893
349      FETCH c_item_control into r_item_control;
350      CLOSE c_item_control;
351 
352      IF (l_debug > 0) THEN
353         csi_t_gen_utility_pvt.add('Serial Number : '||l_mtl_item_tbl(j).serial_number);
354         csi_t_gen_utility_pvt.add('Shipping Org Serial Number Control Code: '||l_mtl_item_tbl(j).serial_number_control_code);
355         csi_t_gen_utility_pvt.add('Receiving Org Serial Number Control Code: '||r_item_control.serial_number_control_code);
356         csi_t_gen_utility_pvt.add('Shipping Org Lot Control Code: '||l_mtl_item_tbl(j).lot_control_code);
357         csi_t_gen_utility_pvt.add('Receiving Org Lot Control Code: '||r_item_control.lot_control_code);
358         csi_t_gen_utility_pvt.add('Shipping Org Loction Control Code: '||l_mtl_item_tbl(j).location_control_code);
359         csi_t_gen_utility_pvt.add('Receiving Org Location Control Code: '||r_item_control.location_control_code);
360         csi_t_gen_utility_pvt.add('Shipping Org Revision Control Code: '||l_mtl_item_tbl(j).revision_qty_control_code);
361         csi_t_gen_utility_pvt.add('Receiving Org Revision Control Code: '||r_item_control.revision_qty_control_code);
362         csi_t_gen_utility_pvt.add('Receiving Org Trackable Flag: '||r_item_control.comms_nl_trackable_flag);
363        csi_t_gen_utility_pvt.add('Primary UOM: '||l_mtl_item_tbl(j).primary_uom_code);
364        csi_t_gen_utility_pvt.add('Primary Qty: '||l_mtl_item_tbl(j).primary_quantity);
365        csi_t_gen_utility_pvt.add('Transaction UOM: '||l_mtl_item_tbl(j).transaction_uom);
366        csi_t_gen_utility_pvt.add('Transaction Qty: '||l_mtl_item_tbl(j).transaction_quantity);
367        csi_t_gen_utility_pvt.add('Organization ID: '||l_mtl_item_tbl(j).organization_id);
368        csi_t_gen_utility_pvt.add('Transfer Org ID: '||l_mtl_item_tbl(j).transfer_organization_id);
369      END IF;
370 
371      l_instance_query_rec                                 :=  csi_inv_trxs_pkg.init_instance_query_rec;
372      l_instance_usage_code                                :=  l_fnd_g_char;
373 
374      IF l_mtl_item_tbl(j).serial_number_control_code in (1,6) THEN
375 
376      --In Transit Shipment
377        l_instance_query_rec.inventory_item_id               :=  l_mtl_item_tbl(j).inventory_item_id;
378        l_instance_query_rec.inv_organization_id             :=  l_mtl_item_tbl(j).organization_id;
379        l_instance_query_rec.serial_number                   :=  NULL;
380        l_instance_query_rec.lot_number                      :=  l_mtl_item_tbl(j).lot_number;
381        l_instance_query_rec.inventory_revision              :=  l_mtl_item_tbl(j).revision;
382        l_instance_query_rec.inv_subinventory_name           :=  l_mtl_item_tbl(j).subinventory_code;
383        l_instance_query_rec.inv_locator_id                  :=  l_mtl_item_tbl(j).locator_id;
384        l_instance_query_rec.instance_usage_code             :=  l_in_inventory;
385        l_sub_inventory   :=  NULL;
386        l_trx_action_type := 'IN_TRANSIT_SHIPMENT';
387        l_instance_usage_code := l_instance_query_rec.instance_usage_code;
388 
389          IF (l_debug > 0) THEN
390            csi_t_gen_utility_pvt.add('Set Serial Number to NULL since the shipping org is 1 or 6 and we are looking for a NON serial In Inventory Instance');
391          END IF;
392 
393        ELSIF l_mtl_item_tbl(j).serial_number_control_code in (2,5) THEN
394          l_instance_query_rec.inventory_item_id               :=  l_mtl_item_tbl(j).inventory_item_id;
395          l_instance_query_rec.inv_organization_id             :=  l_mtl_item_tbl(j).organization_id;
396          l_instance_query_rec.serial_number                   :=  l_mtl_item_tbl(j).serial_number;
397        l_sub_inventory   :=  NULL;
398        l_trx_action_type := 'IN_TRANSIT_SHIPMENT';
399        l_instance_usage_code := l_instance_query_rec.instance_usage_code;
400 
401          IF (l_debug > 0) THEN
402            csi_t_gen_utility_pvt.add('Set Serial Number to what is passed in since the shipping org is 2,5 and we are looking for a serialized instance In Inventory Instance');
403          END IF;
404 
405        END IF;
406 
407        IF (l_debug > 0) THEN
408              csi_t_gen_utility_pvt.add('Transaction Action Type:'|| l_trx_action_type);
409              csi_t_gen_utility_pvt.add('Before Get Item Instance-1 ');
410        END IF;
411 
412        csi_item_instance_pub.get_item_instances(l_api_version,
413                                                 l_commit,
414                                                 l_init_msg_list,
415                                                 l_validation_level,
416                                                 l_instance_query_rec,
417                                                 l_party_query_rec,
418                                                 l_account_query_rec,
419                                                 l_transaction_id,
420                                                 l_resolve_id_columns,
424                                                 l_msg_count,
421                                                 l_instance_status,
422                                                 l_src_instance_header_tbl,
423                                                 l_return_status,
425                                                 l_msg_data);
426 
427        IF (l_debug > 0) THEN
428              csi_t_gen_utility_pvt.add('After Get Item Instance-2');
429        END IF;
430 
431        l_tbl_count := 0;
432        l_tbl_count := l_src_instance_header_tbl.count;
433        IF (l_debug > 0) THEN
434              csi_t_gen_utility_pvt.add('Source Records Found: '||l_tbl_count);
435        END IF;
436 
437        -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
438        IF NOT l_return_status = l_fnd_success then
439          IF (l_debug > 0) THEN
440                csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.get_item_instance API '||l_msg_data);
441          END IF;
442          l_msg_index := 1;
443            WHILE l_msg_count > 0 loop
444 	     l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
445 	     l_msg_index := l_msg_index + 1;
446              l_msg_count := l_msg_count - 1;
447   	   END LOOP;
448 	   RAISE fnd_api.g_exc_error;
449        END IF;
450 
451        IF (l_debug > 0) THEN
452              csi_t_gen_utility_pvt.add('Before checking to see if Source records Exist');
453        END IF;
454 
455        IF l_mtl_item_tbl(j).serial_number_control_code in (2,5) THEN
456           IF l_src_instance_header_tbl.count > 0 THEN
457             IF r_item_control.serial_number_control_code <> 1 THEN -- Do Regular Processing move to In Transit
458 
459           IF (l_debug > 0) THEN
460                csi_t_gen_utility_pvt.add('Updating Serial Number: '||l_mtl_item_tbl(j).serial_number);
461           END IF;
462 
463           l_update_instance_rec                              :=  csi_inv_trxs_pkg.init_instance_update_rec;
464           l_update_instance_rec.instance_id                  :=  l_src_instance_header_tbl(i).instance_id;
465           l_update_instance_rec.inv_subinventory_name        :=  NULL;
466           l_update_instance_rec.inv_locator_id               :=  NULL;
467 	  -- Added for Bug 5975739
468 	  l_update_instance_rec.inv_master_organization_id   :=  l_master_organization_id;
469           l_update_instance_rec.vld_organization_id          :=  l_mtl_item_tbl(j).organization_id;
470           l_update_instance_rec.location_type_code           :=  csi_inv_trxs_pkg.get_location_type_code('In_Transit');
471           l_update_instance_rec.in_transit_order_line_id    :=  r_so_info.line_id;
472           l_update_instance_rec.instance_usage_code          :=  l_in_transit;
473           l_update_instance_rec.last_oe_order_line_id        :=  r_so_info.line_id;
474           l_update_instance_rec.object_version_number        :=  l_src_instance_header_tbl(i).object_version_number;
475 
476            IF (l_debug > 0) THEN
477                  csi_t_gen_utility_pvt.add('After you initialize the Transaction Record Values');
478            END IF;
479 
480            l_party_tbl.delete;
481            l_account_tbl.delete;
482            l_pricing_attrib_tbl.delete;
483            l_org_assignments_tbl.delete;
484            l_asset_assignment_tbl.delete;
485 
486            IF (l_debug > 0) THEN
487                  csi_t_gen_utility_pvt.add('Before Update Item Instance-3');
488            END IF;
489 
490            csi_item_instance_pub.update_item_instance(l_api_version,
491                                                       l_commit,
492                                                       l_init_msg_list,
493                                                       l_validation_level,
494                                                       l_update_instance_rec,
495                                                       l_ext_attrib_values_tbl,
496                                                       l_party_tbl,
497                                                       l_account_tbl,
498                                                       l_pricing_attrib_tbl,
499                                                       l_org_assignments_tbl,
500                                                       l_asset_assignment_tbl,
501                                                       l_txn_rec,
502                                                       l_instance_id_lst,
503                                                       l_return_status,
504                                                       l_msg_count,
505                                                       l_msg_data);
506 
507            l_upd_error_instance_id := NULL;
508            l_upd_error_instance_id := l_update_instance_rec.instance_id;
509 
510            IF (l_debug > 0) THEN
511              csi_t_gen_utility_pvt.add('After Update Item Instance-4');
512              csi_t_gen_utility_pvt.add('You are updating Instance: '||l_update_instance_rec.instance_id);
513              csi_t_gen_utility_pvt.add('l_upd_error_instance_id is: '||l_upd_error_instance_id);
514            END IF;
515 
516            -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
517            IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
518              IF (l_debug > 0) THEN
519                    csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.update_item_instance API '||l_msg_data);
520              END IF;
521              l_msg_index := 1;
522                WHILE l_msg_count > 0 loop
523 	         l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
527 	       RAISE fnd_api.g_exc_error;
524 	         l_msg_index := l_msg_index + 1;
525                  l_msg_count := l_msg_count - 1;
526   	       END LOOP;
528            END IF;
529 
530            ELSE -- Serial Control is 1 ( No Control ) so set to Out Of Enterprise
531 
532             IF (l_debug > 0) THEN
533                  csi_t_gen_utility_pvt.add('Updating Serial Number: '||l_mtl_item_tbl(j).serial_number);
534                  csi_t_gen_utility_pvt.add('Setting to OUT OF ENTERPRISE');
535             END IF;
536 
537             l_update_instance_rec                              :=  csi_inv_trxs_pkg.init_instance_update_rec;
538             l_update_instance_rec.instance_id                  :=  l_src_instance_header_tbl(i).instance_id;
539             l_update_instance_rec.inv_subinventory_name        :=  NULL;
540             l_update_instance_rec.inv_locator_id               :=  NULL;
541             -- Added for Bug 5975739
542 	    l_update_instance_rec.inv_master_organization_id   :=  l_master_organization_id;
543 	    l_update_instance_rec.inv_organization_id          :=  NULL;
544             l_update_instance_rec.vld_organization_id          :=  l_mtl_item_tbl(j).organization_id;
545             -- Bug 5253131
546             l_update_instance_rec.location_type_code           :=  csi_inv_trxs_pkg.get_location_type_code('INTERNAL_SITE');
547             l_update_instance_rec.last_oe_order_line_id        :=  r_so_info.line_id;
548             l_update_instance_rec.active_end_date              :=  l_sysdate;
549             l_update_instance_rec.instance_usage_code          :=  l_out_of_enterprise;
550             l_update_instance_rec.object_version_number        :=  l_src_instance_header_tbl(i).object_version_number;
551 
552              IF (l_debug > 0) THEN
553                    csi_t_gen_utility_pvt.add('After you initialize the Transaction Record Values');
554                    csi_t_gen_utility_pvt.add(l_update_instance_rec.location_id);
555                    csi_t_gen_utility_pvt.add(l_update_instance_rec.location_type_code);
556              END IF;
557 
558              l_party_tbl.delete;
559              l_account_tbl.delete;
560              l_pricing_attrib_tbl.delete;
561              l_org_assignments_tbl.delete;
562              l_asset_assignment_tbl.delete;
563 
564              IF (l_debug > 0) THEN
565                    csi_t_gen_utility_pvt.add('Before Update Item Instance-3');
566              END IF;
567 
568              csi_item_instance_pub.update_item_instance(l_api_version,
569                                                         l_commit,
570                                                         l_init_msg_list,
571                                                         l_validation_level,
572                                                         l_update_instance_rec,
573                                                         l_ext_attrib_values_tbl,
574                                                         l_party_tbl,
575                                                         l_account_tbl,
576                                                         l_pricing_attrib_tbl,
577                                                         l_org_assignments_tbl,
578                                                         l_asset_assignment_tbl,
579                                                         l_txn_rec,
580                                                         l_instance_id_lst,
581                                                         l_return_status,
582                                                         l_msg_count,
583                                                         l_msg_data);
584 
585              l_upd_error_instance_id := NULL;
586              l_upd_error_instance_id := l_update_instance_rec.instance_id;
587 
588              IF (l_debug > 0) THEN
589                csi_t_gen_utility_pvt.add('After Update Item Instance-4');
590                csi_t_gen_utility_pvt.add('You are updating Instance: '||l_update_instance_rec.instance_id);
591                csi_t_gen_utility_pvt.add('l_upd_error_instance_id is: '||l_upd_error_instance_id);
592              END IF;
593 
594              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
595              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
596                IF (l_debug > 0) THEN
597                      csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.update_item_instance API '||l_msg_data);
598                END IF;
599                l_msg_index := 1;
600                  WHILE l_msg_count > 0 loop
601     	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
605 	         RAISE fnd_api.g_exc_error;
602   	           l_msg_index := l_msg_index + 1;
603                    l_msg_count := l_msg_count - 1;
604       	         END LOOP;
606              END IF;
607 
608          IF j = 1 THEN -- Look for IN Transit Non Serial If not there create or Update only 1 time
609            l_instance_query_rec                               :=  csi_inv_trxs_pkg.init_instance_query_rec;
610            l_instance_query_rec.inventory_item_id             :=  l_mtl_item_tbl(j).inventory_item_id;
611            l_instance_query_rec.serial_number                 :=  NULL;
612            l_instance_query_rec.inventory_revision            :=  l_mtl_item_tbl(j).revision;
613            l_instance_query_rec.lot_number                    :=  l_mtl_item_tbl(j).lot_number;
614            l_instance_query_rec.in_transit_order_line_id      :=  r_so_info.line_id;
615            l_instance_query_rec.instance_usage_code           :=  l_in_transit;
616 
617            l_instance_usage_code                              :=  l_in_transit;
618            l_subinventory_name                                :=  NULL;
619            l_organization_id                                  :=  l_mtl_item_tbl(j).organization_id;
620            l_locator_id                                       :=  NULL;
621 
622            l_mfg_flag := NULL;
623            l_serial_number := NULL;
624            l_quantity := abs(l_mtl_item_tbl(j).transaction_quantity);
625 
626             IF (l_debug > 0) THEN
627                  csi_t_gen_utility_pvt.add('Since the Shipping Code is 2 or 5 and the Receiving is 1 Look for Non Serial In Transit');
628                  csi_t_gen_utility_pvt.dump_instance_query_rec(l_instance_query_rec);
629            END IF;
630 
631          IF (l_debug > 0) THEN
632            csi_t_gen_utility_pvt.add('Before Get Item Instance for Dest Non Serialized Instance-5');
633          END IF;
634 
635            csi_item_instance_pub.get_item_instances(l_api_version,
636                                                     l_commit,
637                                                     l_init_msg_list,
638                                                     l_validation_level,
642                                                     l_transaction_id,
639                                                     l_instance_query_rec,
640                                                     l_party_query_rec,
641                                                     l_account_query_rec,
643                                                     l_resolve_id_columns,
644                                                     l_inactive_instance_only,
645                                                     l_dest_instance_header_tbl,
646                                                     l_return_status,
647                                                     l_msg_count,
648                                                     l_msg_data);
649 
650            IF (l_debug > 0) THEN
651                  csi_t_gen_utility_pvt.add('After Get Item Instance-6');
652            END IF;
653            l_tbl_count := 0;
654            l_tbl_count :=  l_dest_instance_header_tbl.count;
655            IF (l_debug > 0) THEN
656                  csi_t_gen_utility_pvt.add('Source Records Found: '||l_tbl_count);
657            END IF;
658 
659            -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
660            IF NOT l_return_status = l_fnd_success then
661              IF (l_debug > 0) THEN
662                    csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.get_item_instance API '||l_msg_data);
663              END IF;
664              l_msg_index := 1;
665 	         WHILE l_msg_count > 0 loop
666 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
667 	           l_msg_index := l_msg_index + 1;
668                l_msg_count := l_msg_count - 1;
669   	         END LOOP;
670 	         RAISE fnd_api.g_exc_error;
671             END IF;
672 
673            IF l_dest_instance_header_tbl.count < 1 THEN  -- Installed Base Destination Records are not found so create a new record
674 
675              IF (l_debug > 0) THEN
676                    csi_t_gen_utility_pvt.add('Creating New Dest dest Instance-7');
677              END IF;
678 
679              l_new_dest_instance_rec                              :=  csi_inv_trxs_pkg.init_instance_create_rec;
680              l_new_dest_instance_rec.inventory_item_id            :=  l_mtl_item_tbl(j).inventory_item_id;
681              l_new_dest_instance_rec.inventory_revision           :=  l_mtl_item_tbl(j).revision;
682              l_new_dest_instance_rec.inv_master_organization_id   :=  l_master_organization_id;
686              l_new_dest_instance_rec.quantity                     :=  l_quantity;
683              l_new_dest_instance_rec.mfg_serial_number_flag       :=  l_mfg_flag;
684              l_new_dest_instance_rec.serial_number                :=  l_serial_number;
685              l_new_dest_instance_rec.lot_number                   :=  l_mtl_item_tbl(j).lot_number;
687              l_new_dest_instance_rec.unit_of_measure              :=  l_mtl_item_tbl(j).transaction_uom;
688              l_new_dest_instance_rec.location_type_code           :=  csi_inv_trxs_pkg.get_location_type_code('In_Transit');
689              l_new_dest_instance_rec.in_transit_order_line_id     :=  r_so_info.line_id;
690              l_new_dest_instance_rec.last_oe_order_line_id        :=  r_so_info.line_id;
691              l_new_dest_instance_rec.instance_usage_code          :=  l_instance_usage_code;
692              --l_new_dest_instance_rec.vld_organization_id          :=  l_organization_id;
693              l_new_dest_instance_rec.vld_organization_id          :=  l_mtl_item_tbl(j).transfer_organization_id;
694              l_new_dest_instance_rec.inv_subinventory_name        :=  l_subinventory_name;
695              l_new_dest_instance_rec.inv_locator_id               :=  l_locator_id;
696              l_new_dest_instance_rec.customer_view_flag           :=  'N';
697              l_new_dest_instance_rec.merchant_view_flag           :=  'Y';
698              l_new_dest_instance_rec.operational_status_code      :=  'NOT_USED';
699              l_new_dest_instance_rec.object_version_number        :=  l_object_version_number;
700              l_new_dest_instance_rec.active_start_date            :=  l_sysdate;
701              l_new_dest_instance_rec.active_end_date              :=  NULL;
702 
703              l_ext_attrib_values_tbl                              :=  csi_inv_trxs_pkg.init_ext_attrib_values_tbl;
704              l_party_tbl                                          :=  csi_inv_trxs_pkg.init_party_tbl;
705              l_account_tbl                                        :=  csi_inv_trxs_pkg.init_account_tbl;
706              l_pricing_attrib_tbl                                 :=  csi_inv_trxs_pkg.init_pricing_attribs_tbl;
707              l_org_assignments_tbl                                :=  csi_inv_trxs_pkg.init_org_assignments_tbl;
708              l_asset_assignment_tbl                               :=  csi_inv_trxs_pkg.init_asset_assignment_tbl;
709 
710              IF (l_debug > 0) THEN
711                    csi_t_gen_utility_pvt.add('Before Create Item Instance-8');
712              END IF;
713 
714              csi_item_instance_pub.create_item_instance(l_api_version,
715                                                         l_commit,
716                                                         l_init_msg_list,
717                                                         l_validation_level,
718                                                         l_new_dest_instance_rec,
719                                                         l_ext_attrib_values_tbl,
720                                                         l_party_tbl,
721                                                         l_account_tbl,
722                                                         l_pricing_attrib_tbl,
723                                                         l_org_assignments_tbl,
724                                                         l_asset_assignment_tbl,
725                                                         l_txn_rec,
726                                                         l_return_status,
727                                                         l_msg_count,
728                                                         l_msg_data);
729 
730              IF (l_debug > 0) THEN
731                    csi_t_gen_utility_pvt.add('After Create Item Instance-9');
732              END IF;
733              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
734 
735              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
736                IF (l_debug > 0) THEN
737                      csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.create_item_instance API '||l_msg_data);
738                END IF;
739                l_msg_index := 1;
740                  WHILE l_msg_count > 0 loop
741 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
745 	         RAISE fnd_api.g_exc_error;
742 	           l_msg_index := l_msg_index + 1;
743                    l_msg_count := l_msg_count - 1;
744   	         END LOOP;
746              END IF;
747 
748            ELSIF l_dest_instance_header_tbl.count = 1 THEN -- Installed Base Destination Records Found
749 
750              IF (l_debug > 0) THEN
754              END IF;
751                csi_t_gen_utility_pvt.add('Instance Usage Code: '||l_dest_instance_header_tbl(i).instance_usage_code);
752                csi_t_gen_utility_pvt.add('Item ID: '||l_dest_instance_header_tbl(i).inventory_item_id);
753                csi_t_gen_utility_pvt.add('Instance ID: '||l_dest_instance_header_tbl(i).instance_id);
755 
756             l_update_dest_instance_rec                          :=  csi_inv_trxs_pkg.init_instance_update_rec;
757             l_update_dest_instance_rec.instance_id              :=  l_dest_instance_header_tbl(i).instance_id;
758             l_update_dest_instance_rec.quantity                 :=  l_dest_instance_header_tbl(i).quantity + l_quantity;
759             l_update_dest_instance_rec.location_type_code       :=  csi_inv_trxs_pkg.get_location_type_code('In_Transit');
760             l_update_dest_instance_rec.in_transit_order_line_id :=  r_so_info.line_id;
761 	    -- Added for Bug 5975739
762 	    l_update_dest_instance_rec.inv_master_organization_id   :=  l_master_organization_id;
763             l_update_dest_instance_rec.last_oe_order_line_id        :=  r_so_info.line_id;
764             l_update_dest_instance_rec.inv_organization_id      :=  NULL;
765             l_update_dest_instance_rec.inv_subinventory_name    :=  l_subinventory_name;
766             l_update_dest_instance_rec.inv_locator_id           :=  l_locator_id;
767             l_update_dest_instance_rec.instance_usage_code      :=  l_in_transit;
768             l_update_dest_instance_rec.active_end_date          :=  NULL;
769             l_update_dest_instance_rec.active_end_date          :=  NULL;
770             l_update_dest_instance_rec.object_version_number    :=  l_dest_instance_header_tbl(i).object_version_number;
771 
772             l_party_tbl.delete;
773             l_account_tbl.delete;
774             l_pricing_attrib_tbl.delete;
775             l_org_assignments_tbl.delete;
776             l_asset_assignment_tbl.delete;
777 
778                l_update_dest_instance_rec.instance_status_id := nvl(csi_inv_trxs_pkg.get_default_status_id(l_txn_rec.transaction_type_id),r_id.instance_status_id);
779 
780                IF (l_debug > 0) THEN
781                      csi_t_gen_utility_pvt.add('Before Update Item Instance-10');
782                      csi_t_gen_utility_pvt.add('Instance Status-11: '||l_update_dest_instance_rec.instance_status_id);
783 
784                END IF;
785 
786                csi_item_instance_pub.update_item_instance(l_api_version,
787                                                           l_commit,
788                                                           l_init_msg_list,
789                                                           l_validation_level,
790                                                           l_update_dest_instance_rec,
791                                                           l_ext_attrib_values_tbl,
792                                                           l_party_tbl,
793                                                           l_account_tbl,
794                                                           l_pricing_attrib_tbl,
795                                                           l_org_assignments_tbl,
796                                                           l_asset_assignment_tbl,
797                                                           l_txn_rec,
798                                                           l_instance_id_lst,
799                                                           l_return_status,
800                                                           l_msg_count,
801                                                           l_msg_data);
802 
803              l_upd_error_instance_id := NULL;
804              l_upd_error_instance_id := l_update_dest_instance_rec.instance_id;
805 
806              IF (l_debug > 0) THEN
807                csi_t_gen_utility_pvt.add('After Update Item Instance-12');
808                csi_t_gen_utility_pvt.add('l_upd_error_instance_id is: '||l_upd_error_instance_id);
809              END IF;
810 
811              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
812              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
813                IF (l_debug > 0) THEN
814                      csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.c API '||l_msg_data);
815                END IF;
816                l_msg_index := 1;
817                  WHILE l_msg_count > 0 loop
818 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
819 	           l_msg_index := l_msg_index + 1;
820                    l_msg_count := l_msg_count - 1;
821                  END LOOP;
822 	         RAISE fnd_api.g_exc_error;
823              END IF;
824 
825            ELSE -- Error No dest non serial recs round
826             IF (l_debug > 0) THEN
827               csi_t_gen_utility_pvt.add('No Records were found in Install Base but the usage is not correct-14, The Usage is: '||l_dest_instance_header_tbl(i).instance_usage_code);
828             END IF;
829             fnd_message.set_name('CSI','CSI_IB_RECORD_NOTFOUND');
830             fnd_message.set_token('ITEM',l_mtl_item_tbl(j).inventory_item_id);
831             fnd_message.set_token('SUBINVENTORY',l_mtl_item_tbl(j).subinventory_code);
832             fnd_message.set_token('ORG_ID',l_mtl_item_tbl(j).organization_id);
833             fnd_message.set_token('ITEM',l_mtl_item_tbl(j).inventory_item_id);
834             l_error_message := fnd_message.get;
835             RAISE fnd_api.g_exc_error;
836           END IF;    -- End of Destination Record If
837 
838          END IF; -- End of j=1 for Control Code 1
839        END IF; -- serial control <> 1
840 
841        ELSE -- No Serialized Instances found so Error.
842          IF (l_debug > 0) THEN
846          fnd_message.set_name('CSI','CSI_IB_RECORD_NOTFOUND');
843                csi_t_gen_utility_pvt.add('No Records were found in Install Base-13');
844          END IF;
845 
847          fnd_message.set_token('ITEM',l_mtl_item_tbl(j).inventory_item_id);
848          fnd_message.set_token('SUBINVENTORY',l_mtl_item_tbl(j).subinventory_code);
849          fnd_message.set_token('ORG_ID',l_mtl_item_tbl(j).organization_id);
850          l_error_message := fnd_message.get;
851          RAISE fnd_api.g_exc_error;
852 
853        END IF; -- SRC Table Count
854 
855        ELSIF l_mtl_item_tbl(j).serial_number_control_code in (1,6) THEN
856 
857          IF l_mtl_item_tbl(j).serial_number_control_code = 6 AND
858             r_item_control.serial_number_control_code = 1 THEN -- Set Flag serial Numbers need to be Out of Enterprise
859            l_receipt_action_flag := 'Y';
860          ELSE
861            l_receipt_action_flag := 'N';
862          END IF;
863 
864 	 IF j = 1 THEN
865 
866          IF l_src_instance_header_tbl.count = 0 THEN
867          IF l_neg_code = 1 THEN -- Negative Records Allowed so Create/Update
868 
869            IF (l_debug > 0) THEN
870                  csi_t_gen_utility_pvt.add('No Source records were found and Neg Code is 1 so create a new Source Instance Record');
871            END IF;
872 
873            l_new_src_instance_rec                              :=  csi_inv_trxs_pkg.init_instance_create_rec;
874            l_new_src_instance_rec.inventory_item_id            :=  l_mtl_item_tbl(j).inventory_item_id;
875            l_new_src_instance_rec.inventory_revision           :=  l_mtl_item_tbl(j).revision;
876            l_new_src_instance_rec.inv_master_organization_id   :=  l_master_organization_id;
877            l_new_src_instance_rec.mfg_serial_number_flag       :=  'N';
878            l_new_src_instance_rec.lot_number                   :=  l_mtl_item_tbl(j).lot_number;
879            l_new_src_instance_rec.quantity                     :=  l_mtl_item_tbl(j).transaction_quantity;
880            l_new_src_instance_rec.unit_of_measure              :=  l_mtl_item_tbl(j).transaction_uom;
881            l_new_src_instance_rec.location_id                  :=  nvl(l_mtl_item_tbl(j).subinv_location_id,l_mtl_item_tbl(j).hr_location_id);
882            l_new_src_instance_rec.location_type_code           :=  csi_inv_trxs_pkg.get_location_type_code('Inventory');
883            l_new_src_instance_rec.instance_usage_code          :=  l_instance_usage_code;
884            l_new_src_instance_rec.inv_organization_id          :=  l_mtl_item_tbl(j).organization_id;
885            l_new_src_instance_rec.vld_organization_id          :=  l_mtl_item_tbl(j).organization_id;
886            l_new_src_instance_rec.inv_subinventory_name        :=  l_mtl_item_tbl(j).subinventory_code;
887            l_new_src_instance_rec.inv_locator_id               :=  l_mtl_item_tbl(j).locator_id;
888            l_new_src_instance_rec.customer_view_flag           :=  'N';
889            l_new_src_instance_rec.merchant_view_flag           :=  'Y';
890            l_new_src_instance_rec.operational_status_code      :=  'NOT_USED';
891            l_new_src_instance_rec.object_version_number        :=  l_object_version_number;
892            l_new_src_instance_rec.active_start_date            :=  l_sysdate;
893            l_new_src_instance_rec.active_end_date              :=  NULL;
894            l_new_src_instance_rec.last_oe_order_line_id        :=  r_so_info.line_id;
895 
896            l_ext_attrib_values_tbl                             :=  csi_inv_trxs_pkg.init_ext_attrib_values_tbl;
897            l_party_tbl                                         :=  csi_inv_trxs_pkg.init_party_tbl;
898            l_account_tbl                                       :=  csi_inv_trxs_pkg.init_account_tbl;
899            l_pricing_attrib_tbl                                :=  csi_inv_trxs_pkg.init_pricing_attribs_tbl;
900            l_org_assignments_tbl                               :=  csi_inv_trxs_pkg.init_org_assignments_tbl;
901            l_asset_assignment_tbl                              :=  csi_inv_trxs_pkg.init_asset_assignment_tbl;
902 
903            IF (l_debug > 0) THEN
904                  csi_t_gen_utility_pvt.add('Before Create Source Item Instance-14');
905            END IF;
906 
907            csi_item_instance_pub.create_item_instance(l_api_version,
908                                                       l_commit,
909                                                       l_init_msg_list,
910                                                       l_validation_level,
911                                                       l_new_src_instance_rec,
912                                                       l_ext_attrib_values_tbl,
913                                                       l_party_tbl,
914                                                       l_account_tbl,
915                                                       l_pricing_attrib_tbl,
916                                                       l_org_assignments_tbl,
917                                                       l_asset_assignment_tbl,
918                                                       l_txn_rec,
919                                                       l_return_status,
920                                                       l_msg_count,
921                                                       l_msg_data);
922 
923              IF (l_debug > 0) THEN
924                    csi_t_gen_utility_pvt.add('After Create Source Item Instance-15');
925              END IF;
926 
927              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
928 
929              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
930                IF (l_debug > 0) THEN
931                      csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.create_item_instance API '||l_msg_data);
932                END IF;
933                l_msg_index := 1;
934                  WHILE l_msg_count > 0 loop
938   	         END LOOP;
935 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
936 	           l_msg_index := l_msg_index + 1;
937                    l_msg_count := l_msg_count - 1;
939 	         RAISE fnd_api.g_exc_error;
940              END IF;
941 
942          ELSE -- Inv Does not allowe neg qty and source is not found
943            IF (l_debug > 0) THEN
944              csi_t_gen_utility_pvt.add('No Records were found in Install Base-16');
945            END IF;
946            fnd_message.set_name('CSI','CSI_IB_RECORD_NOTFOUND');
947            fnd_message.set_token('ITEM',l_mtl_item_tbl(j).inventory_item_id);
948            fnd_message.set_token('SUBINVENTORY',l_mtl_item_tbl(j).subinventory_code);
949            fnd_message.set_token('ORG_ID',l_mtl_item_tbl(j).organization_id);
950            l_error_message := fnd_message.get;
951            RAISE fnd_api.g_exc_error;
952 
953        END IF; -- End of Neg Qty IF
954     ELSIF l_src_instance_header_tbl.count = 1 THEN -- Source Records are found
955 
956          IF (l_debug > 0) THEN
957            csi_t_gen_utility_pvt.add('1 Source Record Found so we will update it.');
958            csi_t_gen_utility_pvt.add('You will update instance: '||l_src_instance_header_tbl(i).instance_id);
959                  csi_t_gen_utility_pvt.add('End Date is: '||l_src_instance_header_tbl(i).active_end_date);
960          END IF;
961 
962            l_upd_src_dest_instance_rec                        :=  csi_inv_trxs_pkg.init_instance_update_rec;
963            l_upd_src_dest_instance_rec.instance_id            :=  l_src_instance_header_tbl(i).instance_id;
964            l_upd_src_dest_instance_rec.active_end_date        :=  NULL;
968 
965            l_upd_src_dest_instance_rec.quantity               :=  l_src_instance_header_tbl(i).quantity - abs(l_mtl_item_tbl(j).primary_quantity);
966            l_upd_src_dest_instance_rec.last_oe_order_line_id  :=  r_so_info.line_id;
967            l_upd_src_dest_instance_rec.object_version_number  :=  l_src_instance_header_tbl(i).object_version_number;
969            l_party_tbl.delete;
970            l_account_tbl.delete;
971            l_pricing_attrib_tbl.delete;
972            l_org_assignments_tbl.delete;
973            l_asset_assignment_tbl.delete;
974 
975            IF (l_debug > 0) THEN
976               csi_t_gen_utility_pvt.add('Before Update Source Item Instance-17');
977            END IF;
978 
979            l_upd_src_dest_instance_rec.instance_status_id := nvl(csi_inv_trxs_pkg.get_default_status_id(l_txn_rec.transaction_type_id),r_id.instance_status_id);
980 
981            IF (l_debug > 0) THEN
982              csi_t_gen_utility_pvt.add('Instance Status Id: '||l_upd_src_dest_instance_rec.instance_status_id);
983            END IF;
984 
985            csi_item_instance_pub.update_item_instance(l_api_version,
986                                                       l_commit,
987                                                       l_init_msg_list,
988                                                       l_validation_level,
989                                                       l_upd_src_dest_instance_rec,
990                                                       l_ext_attrib_values_tbl,
991                                                       l_party_tbl,
992                                                       l_account_tbl,
993                                                       l_pricing_attrib_tbl,
994                                                       l_org_assignments_tbl,
995                                                       l_asset_assignment_tbl,
996                                                       l_txn_rec,
997                                                       l_instance_id_lst,
998                                                       l_return_status,
999                                                       l_msg_count,
1000                                                       l_msg_data);
1001 
1002            l_upd_error_instance_id := NULL;
1003            l_upd_error_instance_id := l_upd_src_dest_instance_rec.instance_id;
1004 
1005            IF (l_debug > 0) THEN
1006              csi_t_gen_utility_pvt.add('After Update Item Instance-18');
1007              csi_t_gen_utility_pvt.add('l_upd_error_instance_id is: '||l_upd_error_instance_id);
1008            END IF;
1009 
1010            -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
1011            IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
1012              IF (l_debug > 0) THEN
1013                    csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.update_item_instance API '||l_msg_data);
1014              END IF;
1015              l_msg_index := 1;
1016                WHILE l_msg_count > 0 loop
1017 	         l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
1021 	       RAISE fnd_api.g_exc_error;
1018 	         l_msg_index := l_msg_index + 1;
1019                  l_msg_count := l_msg_count - 1;
1020   	       END LOOP;
1022            END IF;
1023 
1024     ELSIF l_src_instance_header_tbl.count > 1 THEN
1025       -- Multiple Instances were found so throw error
1026       IF (l_debug > 0) THEN
1027         csi_t_gen_utility_pvt.add('Multiple Instances were Found in Install BaseBase-19');
1028       END IF;
1029       fnd_message.set_name('CSI','CSI_TXN_MULT_INST_FOUND');
1030       fnd_message.set_token('INV_ITEM_ID',l_mtl_item_tbl(j).inventory_item_id);
1031       fnd_message.set_token('SUBINV',l_mtl_item_tbl(j).subinventory_code);
1032       fnd_message.set_token('INV_ORG_ID',l_mtl_item_tbl(j).organization_id);
1033       fnd_message.set_token('LOCATOR',l_mtl_item_tbl(j).locator_id);
1034       l_error_message := fnd_message.get;
1035       RAISE fnd_api.g_exc_error;
1036     END IF;  -- End of If for Main Source
1037   END IF;  -- End of J If
1038 
1039 	   -- Get Destination Records
1040 	   -- We will query for serialized In Transit Instances
1041 -- JUNE22
1042 -- Changed query fields for serial control = 1
1043 
1044          IF l_mtl_item_tbl(j).serial_number_control_code = 1 THEN
1045            l_instance_query_rec                               :=  csi_inv_trxs_pkg.init_instance_query_rec;
1046            l_instance_query_rec.serial_number                 :=  NULL;
1047            l_instance_query_rec.inventory_revision            :=  l_mtl_item_tbl(j).revision;
1048            l_instance_query_rec.lot_number                    :=  l_mtl_item_tbl(j).lot_number;
1049            l_instance_query_rec.inv_locator_id                :=  l_mtl_item_tbl(j).locator_id;
1050            l_instance_query_rec.location_type_code            :=  csi_inv_trxs_pkg.get_location_type_code('In_Transit');
1051            l_instance_query_rec.instance_usage_code           :=  l_in_transit;
1052            l_instance_query_rec.in_transit_order_line_id      :=  r_so_info.line_id;
1053 
1054            l_instance_usage_code                              :=  l_in_transit;
1055            l_subinventory_name                                :=  NULL;
1056            l_organization_id                                  :=  l_mtl_item_tbl(j).organization_id;
1057            l_locator_id                                       :=  NULL;
1058 
1059             l_mfg_flag := NULL;
1060             l_serial_number := NULL;
1061             l_quantity := abs(l_mtl_item_tbl(j).transaction_quantity);
1062 
1063             IF (l_debug > 0) THEN
1064                  csi_t_gen_utility_pvt.add('Ship Serial Code is 1 so we want to query for Non Serialized, Setting Serial Number to NULL for Dest Query for In Transit');
1065                  csi_t_gen_utility_pvt.add('Serial Number: '||l_mtl_item_tbl(j).serial_number);
1066            END IF;
1067 
1068          ELSIF l_mtl_item_tbl(j).serial_number_control_code = 6 THEN
1069 
1070            l_instance_query_rec                               :=  csi_inv_trxs_pkg.init_instance_query_rec;
1071            l_instance_query_rec.inventory_item_id             :=  l_mtl_item_tbl(j).inventory_item_id;
1072            l_instance_query_rec.serial_number                 :=  l_mtl_item_tbl(j).serial_number;
1073            l_instance_usage_code                              :=  l_in_transit;
1074            l_subinventory_name                                :=  NULL;
1075            l_organization_id                                  :=  l_mtl_item_tbl(j).organization_id;
1076            l_locator_id                                       :=  NULL;
1077 
1078            l_mfg_flag := 'Y';
1079            l_serial_number := l_mtl_item_tbl(j).serial_number;
1080            l_quantity := 1;
1081            IF (l_debug > 0) THEN
1085            END IF;
1082              csi_t_gen_utility_pvt.add('Ship Serial Code is 6 so we want to query for Serialized, Setting Serial Number to Trans Serial Number for Dest Query');
1083              csi_t_gen_utility_pvt.add('Setting l_mfg_flag to Y and l_serial_number to serial_number since we will create non serialized records');
1084              csi_t_gen_utility_pvt.add('Serial Number: '||l_mtl_item_tbl(j).serial_number);
1086 
1087          END IF;
1088 
1089          IF (l_debug > 0) THEN
1090            csi_t_gen_utility_pvt.add('Before Get Item Instance for Dest Serialized Instance-20');
1091            csi_t_gen_utility_pvt.add('Serial Number: '||l_mtl_item_tbl(j).serial_number);
1092          END IF;
1093 
1094            csi_item_instance_pub.get_item_instances(l_api_version,
1095                                                     l_commit,
1096                                                     l_init_msg_list,
1097                                                     l_validation_level,
1098                                                     l_instance_query_rec,
1099                                                     l_party_query_rec,
1100                                                     l_account_query_rec,
1101                                                     l_transaction_id,
1102                                                     l_resolve_id_columns,
1103                                                     l_inactive_instance_only,
1104                                                     l_dest_instance_header_tbl,
1105                                                     l_return_status,
1106                                                     l_msg_count,
1107                                                     l_msg_data);
1108 
1109            IF (l_debug > 0) THEN
1110                  csi_t_gen_utility_pvt.add('After Get Item Instance-21');
1111            END IF;
1112            l_tbl_count := 0;
1113            l_tbl_count :=  l_dest_instance_header_tbl.count;
1114            IF (l_debug > 0) THEN
1115                  csi_t_gen_utility_pvt.add('Source Records Found: '||l_tbl_count);
1116            END IF;
1117 
1118            -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
1119            IF NOT l_return_status = l_fnd_success then
1120              IF (l_debug > 0) THEN
1121                    csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.get_item_instance API '||l_msg_data);
1122              END IF;
1123              l_msg_index := 1;
1124 	         WHILE l_msg_count > 0 loop
1125 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
1126 	           l_msg_index := l_msg_index + 1;
1127                l_msg_count := l_msg_count - 1;
1128   	         END LOOP;
1129 	         RAISE fnd_api.g_exc_error;
1130            END IF;
1131 
1132            IF l_receipt_action_flag = 'N' THEN -- Do regular processing to In Transit
1133              IF l_dest_instance_header_tbl.count < 1 THEN  -- Installed Base Destination Records are not found so create a new record
1134 
1135 
1136              IF (l_debug > 0) THEN
1137                    csi_t_gen_utility_pvt.add('Creating New Dest dest Instance-22');
1138              END IF;
1139 
1140              l_new_dest_instance_rec                              :=  csi_inv_trxs_pkg.init_instance_create_rec;
1141              l_new_dest_instance_rec.inventory_item_id            :=  l_mtl_item_tbl(j).inventory_item_id;
1142              l_new_dest_instance_rec.inventory_revision           :=  l_mtl_item_tbl(j).revision;
1143              l_new_dest_instance_rec.inv_master_organization_id   :=  l_master_organization_id;
1144              l_new_dest_instance_rec.mfg_serial_number_flag       :=  l_mfg_flag;
1145              l_new_dest_instance_rec.serial_number                :=  l_serial_number;
1146              l_new_dest_instance_rec.lot_number                   :=  l_mtl_item_tbl(j).lot_number;
1147              l_new_dest_instance_rec.quantity                     :=  l_quantity;
1148              l_new_dest_instance_rec.unit_of_measure              :=  l_mtl_item_tbl(j).transaction_uom;
1149              l_new_dest_instance_rec.location_type_code           := csi_inv_trxs_pkg.get_location_type_code('In_Transit');
1150              l_new_dest_instance_rec.in_transit_order_line_id     := r_so_info.line_id;
1151              l_new_dest_instance_rec.last_oe_order_line_id  :=  r_so_info.line_id;
1152              l_new_dest_instance_rec.instance_usage_code          :=  l_instance_usage_code;
1153              l_new_dest_instance_rec.vld_organization_id          :=  l_organization_id;
1154              l_new_dest_instance_rec.inv_subinventory_name        :=  l_subinventory_name;
1155              l_new_dest_instance_rec.inv_locator_id               :=  l_locator_id;
1156              l_new_dest_instance_rec.customer_view_flag           :=  'N';
1157              l_new_dest_instance_rec.merchant_view_flag           :=  'Y';
1158              l_new_dest_instance_rec.operational_status_code      :=  'NOT_USED';
1159              l_new_dest_instance_rec.object_version_number        :=  l_object_version_number;
1160              l_new_dest_instance_rec.active_start_date            :=  l_sysdate;
1161              l_new_dest_instance_rec.active_end_date              :=  NULL;
1162              --Added the below code for bug 5897127 Base Bug 5758860--
1166              l_party_tbl                                          :=  csi_inv_trxs_pkg.init_party_tbl;
1163 	     l_new_dest_instance_rec.instance_status_id := nvl(csi_inv_trxs_pkg.get_default_status_id(l_txn_rec.transaction_type_id),r_id.instance_status_id);
1164 
1165              l_ext_attrib_values_tbl                              :=  csi_inv_trxs_pkg.init_ext_attrib_values_tbl;
1167              l_account_tbl                                        :=  csi_inv_trxs_pkg.init_account_tbl;
1168              l_pricing_attrib_tbl                                 :=  csi_inv_trxs_pkg.init_pricing_attribs_tbl;
1169              l_org_assignments_tbl                                :=  csi_inv_trxs_pkg.init_org_assignments_tbl;
1170              l_asset_assignment_tbl                               :=  csi_inv_trxs_pkg.init_asset_assignment_tbl;
1171 
1172              IF (l_debug > 0) THEN
1173                    csi_t_gen_utility_pvt.add('Before Create Item Instance-23');
1174              END IF;
1175 
1176              csi_item_instance_pub.create_item_instance(l_api_version,
1177                                                         l_commit,
1178                                                         l_init_msg_list,
1179                                                         l_validation_level,
1180                                                         l_new_dest_instance_rec,
1181                                                         l_ext_attrib_values_tbl,
1182                                                         l_party_tbl,
1183                                                         l_account_tbl,
1184                                                         l_pricing_attrib_tbl,
1185                                                         l_org_assignments_tbl,
1186                                                         l_asset_assignment_tbl,
1187                                                         l_txn_rec,
1188                                                         l_return_status,
1189                                                         l_msg_count,
1190                                                         l_msg_data);
1191 
1192              IF (l_debug > 0) THEN
1193                    csi_t_gen_utility_pvt.add('After Create Item Instance-24');
1194              END IF;
1195              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
1196 
1197              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
1198                IF (l_debug > 0) THEN
1199                      csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.create_item_instance API '||l_msg_data);
1200                END IF;
1201                l_msg_index := 1;
1202                  WHILE l_msg_count > 0 loop
1203 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
1204 	           l_msg_index := l_msg_index + 1;
1205                    l_msg_count := l_msg_count - 1;
1206   	         END LOOP;
1207 	         RAISE fnd_api.g_exc_error;
1208              END IF;
1209 
1210            ELSIF l_dest_instance_header_tbl.count = 1 THEN -- Installed Base Destination Records Found
1211 
1212              IF (l_debug > 0) THEN
1213                csi_t_gen_utility_pvt.add('Instance Usage Code: '||l_dest_instance_header_tbl(i).instance_usage_code);
1214                csi_t_gen_utility_pvt.add('Item ID: '||l_dest_instance_header_tbl(i).inventory_item_id);
1215                csi_t_gen_utility_pvt.add('Instance ID: '||l_dest_instance_header_tbl(i).instance_id);
1216              END IF;
1217 
1218              IF l_dest_instance_header_tbl(i).instance_usage_code IN (l_in_transit,l_returned) THEN
1219                 -- Update Non Serialized / Serialized Item
1220 
1221 
1222          l_trans_quantity := 0;
1223 
1224          IF l_mtl_item_tbl(j).serial_number_control_code = 1 THEN
1225 
1226             l_trans_quantity := l_dest_instance_header_tbl(i).quantity + abs(l_mtl_item_tbl(j).primary_quantity);
1227 
1228             IF (l_debug > 0) THEN
1229                  csi_t_gen_utility_pvt.add('Setting Trans Qty: '||l_trans_quantity);
1230            END IF;
1231          ELSIF l_mtl_item_tbl(j).serial_number_control_code = 6 THEN
1232             l_trans_quantity := 1;
1233             IF (l_debug > 0) THEN
1234                  csi_t_gen_utility_pvt.add('Setting Trans Qty: '||l_trans_quantity);
1235            END IF;
1236          END IF;
1237 
1238             l_update_dest_instance_rec                          :=  csi_inv_trxs_pkg.init_instance_update_rec;
1239             l_update_dest_instance_rec.instance_id              :=  l_dest_instance_header_tbl(i).instance_id;
1240             l_update_dest_instance_rec.quantity                 :=  l_trans_quantity;
1241             l_update_dest_instance_rec.location_type_code       :=  csi_inv_trxs_pkg.get_location_type_code('In_Transit');
1242             l_update_dest_instance_rec.in_transit_order_line_id :=  r_so_info.line_id;
1243             -- Added for Bug 5975739
1244 	    l_update_dest_instance_rec.inv_master_organization_id :=  l_master_organization_id;
1245 	    l_update_dest_instance_rec.last_oe_order_line_id  :=  r_so_info.line_id;
1246             l_update_dest_instance_rec.inv_organization_id          :=  NULL;
1247             l_update_dest_instance_rec.inv_subinventory_name        :=  l_subinventory_name;
1248             l_update_dest_instance_rec.inv_locator_id               :=  l_locator_id;
1249             l_update_dest_instance_rec.instance_usage_code          :=  l_in_transit;
1250             l_update_dest_instance_rec.active_end_date            := NULL;
1251             l_update_dest_instance_rec.active_end_date          :=  NULL;
1252             l_update_dest_instance_rec.object_version_number    :=  l_dest_instance_header_tbl(i).object_version_number;
1253 
1254                l_party_tbl.delete;
1255                l_account_tbl.delete;
1256                l_pricing_attrib_tbl.delete;
1257                l_org_assignments_tbl.delete;
1258                l_asset_assignment_tbl.delete;
1259 
1263                      csi_t_gen_utility_pvt.add('Before Update Item Instance-25');
1260                l_update_dest_instance_rec.instance_status_id := nvl(csi_inv_trxs_pkg.get_default_status_id(l_txn_rec.transaction_type_id),r_id.instance_status_id);
1261 
1262                IF (l_debug > 0) THEN
1264                      csi_t_gen_utility_pvt.add('Instance Status-26: '||l_update_dest_instance_rec.instance_status_id);
1265 
1266                END IF;
1267 
1268                csi_item_instance_pub.update_item_instance(l_api_version,
1269                                                           l_commit,
1270                                                           l_init_msg_list,
1271                                                           l_validation_level,
1272                                                           l_update_dest_instance_rec,
1273                                                           l_ext_attrib_values_tbl,
1274                                                           l_party_tbl,
1275                                                           l_account_tbl,
1276                                                           l_pricing_attrib_tbl,
1277                                                           l_org_assignments_tbl,
1278                                                           l_asset_assignment_tbl,
1279                                                           l_txn_rec,
1280                                                           l_instance_id_lst,
1281                                                           l_return_status,
1282                                                           l_msg_count,
1283                                                           l_msg_data);
1284 
1285              l_upd_error_instance_id := NULL;
1286              l_upd_error_instance_id := l_update_dest_instance_rec.instance_id;
1287 
1288              IF (l_debug > 0) THEN
1289                csi_t_gen_utility_pvt.add('After Update Item Instance-27');
1290                csi_t_gen_utility_pvt.add('l_upd_error_instance_id is: '||l_upd_error_instance_id);
1291              END IF;
1292 
1293              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
1294              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
1295                IF (l_debug > 0) THEN
1296                      csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.c API '||l_msg_data);
1297                END IF;
1298                l_msg_index := 1;
1299                  WHILE l_msg_count > 0 loop
1300 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
1301 	           l_msg_index := l_msg_index + 1;
1302                    l_msg_count := l_msg_count - 1;
1303                  END LOOP;
1304 	         RAISE fnd_api.g_exc_error;
1305              END IF;
1306 
1307            ELSE -- Error No Src Recs with usage of In Transit or Returned
1308             IF (l_debug > 0) THEN
1309               csi_t_gen_utility_pvt.add('No Records were found in Install Base but the usage is not correct-20, The Usage is: '||l_dest_instance_header_tbl(i).instance_usage_code);
1310             END IF;
1311             fnd_message.set_name('CSI','CSI_IB_RECORD_NOTFOUND');
1312             fnd_message.set_token('ITEM',l_mtl_item_tbl(j).inventory_item_id);
1313             fnd_message.set_token('SUBINVENTORY',l_mtl_item_tbl(j).subinventory_code);
1314             fnd_message.set_token('ORG_ID',l_mtl_item_tbl(j).organization_id);
1315             fnd_message.set_token('ITEM',l_mtl_item_tbl(j).inventory_item_id);
1316             l_error_message := fnd_message.get;
1317             RAISE fnd_api.g_exc_error;
1318             END IF;  -- In Transit and Returned Usage Code Check
1319           END IF;    -- End of Destination Record If
1320 
1321           ELSIF l_receipt_action_flag = 'Y' THEN -- Set Serial Numbers to be Out Of Enterprise
1322 
1323           IF l_mtl_item_tbl(j).serial_number_control_code = 1 THEN
1324 
1325           IF l_dest_instance_header_tbl.count < 1 THEN -- Installed Base Destination Records Not Found
1326 
1327              IF (l_debug > 0) THEN
1328                    csi_t_gen_utility_pvt.add('Creating New Dest dest Instance-28');
1329              END IF;
1330 
1331              l_new_dest_instance_rec                              :=  csi_inv_trxs_pkg.init_instance_create_rec;
1332              l_new_dest_instance_rec.inventory_item_id            :=  l_mtl_item_tbl(j).inventory_item_id;
1333              l_new_dest_instance_rec.inventory_revision           :=  l_mtl_item_tbl(j).revision;
1334              l_new_dest_instance_rec.inv_master_organization_id   :=  l_master_organization_id;
1335              l_new_dest_instance_rec.mfg_serial_number_flag       :=  NULL;
1336              l_new_dest_instance_rec.serial_number                :=  NULL;
1337              l_new_dest_instance_rec.lot_number                   :=  l_mtl_item_tbl(j).lot_number;
1338              l_new_dest_instance_rec.quantity                     :=  abs(l_mtl_item_tbl(j).transaction_quantity);
1339              l_new_dest_instance_rec.unit_of_measure              :=  l_mtl_item_tbl(j).transaction_uom;
1340              l_new_dest_instance_rec.location_type_code           :=  csi_inv_trxs_pkg.get_location_type_code('In_Transit');
1341              l_new_dest_instance_rec.in_transit_order_line_id     :=  r_so_info.line_id;
1342              l_new_dest_instance_rec.last_oe_order_line_id  :=  r_so_info.line_id;
1343              l_new_dest_instance_rec.instance_usage_code          :=  l_in_transit;
1344              l_new_dest_instance_rec.vld_organization_id          :=  l_mtl_item_tbl(j).organization_id;
1345              l_new_dest_instance_rec.inv_subinventory_name        :=  NULL;
1346              l_new_dest_instance_rec.inv_locator_id               :=  NULL;
1347              l_new_dest_instance_rec.customer_view_flag           :=  'N';
1348              l_new_dest_instance_rec.merchant_view_flag           :=  'Y';
1349              l_new_dest_instance_rec.operational_status_code      :=  'NOT_USED';
1353 
1350              l_new_dest_instance_rec.object_version_number        :=  l_object_version_number;
1351              l_new_dest_instance_rec.active_start_date            :=  l_sysdate;
1352              l_new_dest_instance_rec.active_end_date              :=  NULL;
1354              l_ext_attrib_values_tbl                              :=  csi_inv_trxs_pkg.init_ext_attrib_values_tbl;
1355              l_party_tbl                                          :=  csi_inv_trxs_pkg.init_party_tbl;
1356              l_account_tbl                                        :=  csi_inv_trxs_pkg.init_account_tbl;
1357              l_pricing_attrib_tbl                                 :=  csi_inv_trxs_pkg.init_pricing_attribs_tbl;
1358              l_org_assignments_tbl                                :=  csi_inv_trxs_pkg.init_org_assignments_tbl;
1359              l_asset_assignment_tbl                               :=  csi_inv_trxs_pkg.init_asset_assignment_tbl;
1360 
1361              IF (l_debug > 0) THEN
1362                    csi_t_gen_utility_pvt.add('Before Create Item Instance-29');
1363              END IF;
1364 
1365              csi_item_instance_pub.create_item_instance(l_api_version,
1366                                                         l_commit,
1367                                                         l_init_msg_list,
1368                                                         l_validation_level,
1369                                                         l_new_dest_instance_rec,
1370                                                         l_ext_attrib_values_tbl,
1371                                                         l_party_tbl,
1372                                                         l_account_tbl,
1373                                                         l_pricing_attrib_tbl,
1374                                                         l_org_assignments_tbl,
1375                                                         l_asset_assignment_tbl,
1376                                                         l_txn_rec,
1377                                                         l_return_status,
1378                                                         l_msg_count,
1379                                                         l_msg_data);
1380 
1381              IF (l_debug > 0) THEN
1382                    csi_t_gen_utility_pvt.add('After Create Item Instance-30');
1383              END IF;
1384              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
1385 
1386              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
1387                IF (l_debug > 0) THEN
1388                      csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.create_item_instance API '||l_msg_data);
1389                END IF;
1390                l_msg_index := 1;
1391                  WHILE l_msg_count > 0 loop
1392 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
1393 	           l_msg_index := l_msg_index + 1;
1394                    l_msg_count := l_msg_count - 1;
1395   	         END LOOP;
1396 	         RAISE fnd_api.g_exc_error;
1397              END IF;
1398 
1399            ELSIF l_dest_instance_header_tbl.count = 1 THEN -- Installed Base Destination Records Found
1400 
1401              IF (l_debug > 0) THEN
1402                csi_t_gen_utility_pvt.add('Instance Usage Code: '||l_dest_instance_header_tbl(i).instance_usage_code);
1403                csi_t_gen_utility_pvt.add('Item ID: '||l_dest_instance_header_tbl(i).inventory_item_id);
1404                csi_t_gen_utility_pvt.add('Instance ID: '||l_dest_instance_header_tbl(i).instance_id);
1405              END IF;
1406 
1407             l_update_dest_instance_rec                          :=  csi_inv_trxs_pkg.init_instance_update_rec;
1408             l_update_dest_instance_rec.instance_id              :=  l_dest_instance_header_tbl(i).instance_id;
1409             l_update_dest_instance_rec.quantity                 :=  l_dest_instance_header_tbl(i).quantity + abs(l_mtl_item_tbl(j).primary_quantity);
1410             l_update_dest_instance_rec.location_type_code       :=  csi_inv_trxs_pkg.get_location_type_code('In_Transit');
1411             l_update_dest_instance_rec.in_transit_order_line_id :=  r_so_info.line_id;
1412 	    -- Added for Bug 5975739
1413 	    l_update_dest_instance_rec.inv_master_organization_id :=  l_master_organization_id;
1414             l_update_dest_instance_rec.last_oe_order_line_id  :=  r_so_info.line_id;
1415             l_update_dest_instance_rec.inv_organization_id      :=  NULL;
1416             l_update_dest_instance_rec.inv_subinventory_name    :=  l_subinventory_name;
1417             l_update_dest_instance_rec.inv_locator_id           :=  l_locator_id;
1418             l_update_dest_instance_rec.instance_usage_code      :=  l_in_transit;
1419             l_update_dest_instance_rec.active_end_date          :=  NULL;
1420             l_update_dest_instance_rec.active_end_date          :=  NULL;
1421             l_update_dest_instance_rec.object_version_number    :=  l_dest_instance_header_tbl(i).object_version_number;
1422 
1423                l_party_tbl.delete;
1424                l_account_tbl.delete;
1425                l_pricing_attrib_tbl.delete;
1426                l_org_assignments_tbl.delete;
1427                l_asset_assignment_tbl.delete;
1428 
1429                l_update_dest_instance_rec.instance_status_id := nvl(csi_inv_trxs_pkg.get_default_status_id(l_txn_rec.transaction_type_id),r_id.instance_status_id);
1430 
1431                IF (l_debug > 0) THEN
1432                      csi_t_gen_utility_pvt.add('Before Update Item Instance-31');
1433                      csi_t_gen_utility_pvt.add('Instance Status-32: '||l_update_dest_instance_rec.instance_status_id);
1434 
1435                END IF;
1436 
1437                csi_item_instance_pub.update_item_instance(l_api_version,
1438                                                           l_commit,
1439                                                           l_init_msg_list,
1443                                                           l_party_tbl,
1440                                                           l_validation_level,
1441                                                           l_update_dest_instance_rec,
1442                                                           l_ext_attrib_values_tbl,
1444                                                           l_account_tbl,
1445                                                           l_pricing_attrib_tbl,
1446                                                           l_org_assignments_tbl,
1447                                                           l_asset_assignment_tbl,
1448                                                           l_txn_rec,
1449                                                           l_instance_id_lst,
1450                                                           l_return_status,
1451                                                           l_msg_count,
1452                                                           l_msg_data);
1453 
1454              l_upd_error_instance_id := NULL;
1455              l_upd_error_instance_id := l_update_dest_instance_rec.instance_id;
1456 
1457              IF (l_debug > 0) THEN
1458                csi_t_gen_utility_pvt.add('After Update Item Instance-33');
1459                csi_t_gen_utility_pvt.add('l_upd_error_instance_id is: '||l_upd_error_instance_id);
1460              END IF;
1461 
1462              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
1463              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
1464                IF (l_debug > 0) THEN
1465                      csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.c API '||l_msg_data);
1466                END IF;
1467                l_msg_index := 1;
1468                  WHILE l_msg_count > 0 loop
1469 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
1470 	           l_msg_index := l_msg_index + 1;
1471                    l_msg_count := l_msg_count - 1;
1472                  END LOOP;
1473 	         RAISE fnd_api.g_exc_error;
1474              END IF;
1475           END IF;    -- End of Destination Record If
1476 
1477           ELSIF l_mtl_item_tbl(j).serial_number_control_code = 6 THEN
1478 
1479            IF l_dest_instance_header_tbl.count < 1 THEN -- Installed Base Destination Records Not Found
1480 
1481              IF (l_debug > 0) THEN
1482                    csi_t_gen_utility_pvt.add('Creating New Dest dest Instance-34');
1483              END IF;
1484 
1485              l_new_dest_instance_rec                              :=  csi_inv_trxs_pkg.init_instance_create_rec;
1486              l_new_dest_instance_rec.inventory_item_id            :=  l_mtl_item_tbl(j).inventory_item_id;
1487              l_new_dest_instance_rec.inventory_revision           :=  l_mtl_item_tbl(j).revision;
1488              l_new_dest_instance_rec.inv_master_organization_id   :=  l_master_organization_id;
1489              l_new_dest_instance_rec.mfg_serial_number_flag       :=  'Y';
1490              l_new_dest_instance_rec.serial_number                :=  l_mtl_item_tbl(j).serial_number;
1491              l_new_dest_instance_rec.lot_number                   :=  l_mtl_item_tbl(j).lot_number;
1492              l_new_dest_instance_rec.quantity                     :=  1;
1493              l_new_dest_instance_rec.unit_of_measure              :=  l_mtl_item_tbl(j).transaction_uom;
1494              -- Bug 5253131
1495              l_new_dest_instance_rec.location_type_code           :=  csi_inv_trxs_pkg.get_location_type_code('INTERNAL_SITE');
1496              l_new_dest_instance_rec.last_oe_order_line_id        :=  r_so_info.line_id;
1497              l_new_dest_instance_rec.instance_usage_code          :=  l_out_of_enterprise;
1498              l_new_dest_instance_rec.vld_organization_id          :=  l_mtl_item_tbl(j).organization_id;
1499              l_new_dest_instance_rec.inv_subinventory_name        :=  NULL;
1500              l_new_dest_instance_rec.inv_locator_id               :=  NULL;
1501              l_new_dest_instance_rec.inv_organization_id          :=  NULL;
1502              l_new_dest_instance_rec.customer_view_flag           :=  'N';
1503              l_new_dest_instance_rec.merchant_view_flag           :=  'Y';
1504              l_new_dest_instance_rec.operational_status_code      :=  'NOT_USED';
1505              l_new_dest_instance_rec.object_version_number        :=  l_object_version_number;
1506              l_new_dest_instance_rec.active_start_date            :=  l_sysdate;
1507 
1508              l_ext_attrib_values_tbl                              :=  csi_inv_trxs_pkg.init_ext_attrib_values_tbl;
1509              l_party_tbl                                          :=  csi_inv_trxs_pkg.init_party_tbl;
1510              l_account_tbl                                        :=  csi_inv_trxs_pkg.init_account_tbl;
1511              l_pricing_attrib_tbl                                 :=  csi_inv_trxs_pkg.init_pricing_attribs_tbl;
1512              l_org_assignments_tbl                                :=  csi_inv_trxs_pkg.init_org_assignments_tbl;
1513              l_asset_assignment_tbl                               :=  csi_inv_trxs_pkg.init_asset_assignment_tbl;
1514 
1515              IF (l_debug > 0) THEN
1516                    csi_t_gen_utility_pvt.add('Before Create Item Instance-35');
1517              END IF;
1518 
1519              csi_item_instance_pub.create_item_instance(l_api_version,
1520                                                         l_commit,
1521                                                         l_init_msg_list,
1522                                                         l_validation_level,
1523                                                         l_new_dest_instance_rec,
1524                                                         l_ext_attrib_values_tbl,
1525                                                         l_party_tbl,
1526                                                         l_account_tbl,
1530                                                         l_txn_rec,
1527                                                         l_pricing_attrib_tbl,
1528                                                         l_org_assignments_tbl,
1529                                                         l_asset_assignment_tbl,
1531                                                         l_return_status,
1532                                                         l_msg_count,
1533                                                         l_msg_data);
1534 
1535              IF (l_debug > 0) THEN
1536                    csi_t_gen_utility_pvt.add('After Create Item Instance-36');
1537              END IF;
1538              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
1539 
1540              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
1541                IF (l_debug > 0) THEN
1542                      csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.create_item_instance API '||l_msg_data);
1543                END IF;
1544                l_msg_index := 1;
1545                  WHILE l_msg_count > 0 loop
1546 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
1547 	           l_msg_index := l_msg_index + 1;
1548                    l_msg_count := l_msg_count - 1;
1549   	         END LOOP;
1550 	         RAISE fnd_api.g_exc_error;
1551              END IF;
1552 
1553              l_expire_instance_rec                          :=  csi_inv_trxs_pkg.init_instance_update_rec;
1554              l_expire_instance_rec.instance_id              :=  l_new_dest_instance_rec.instance_id;
1555              l_expire_instance_rec.active_end_date          :=  sysdate;
1556 
1557              l_exp_txn_rec.source_transaction_date  := l_mtl_item_tbl(i).transaction_date;
1558              l_exp_txn_rec.transaction_date         :=  l_sysdate;
1559              l_exp_txn_rec.transaction_type_id      :=  csi_inv_trxs_pkg.get_txn_type_id(l_trans_type_code,l_trans_app_code);
1560              l_exp_txn_rec.transaction_quantity     :=  l_mtl_item_tbl(i).transaction_quantity;
1561              l_exp_txn_rec.transaction_uom_code     :=  l_mtl_item_tbl(i).transaction_uom;
1562              l_exp_txn_rec.transacted_by            :=  l_employee_id;
1563              l_exp_txn_rec.transaction_action_code  :=  NULL;
1564              l_exp_txn_rec.message_id               :=  p_message_id;
1565              l_exp_txn_rec.inv_material_transaction_id  :=  p_transaction_id;
1566              l_exp_txn_rec.object_version_number    :=  l_object_version_number;
1567              l_exp_txn_rec.source_header_ref_id     :=  r_so_info.header_id;
1568              l_exp_txn_rec.source_line_ref_id       :=  r_so_info.line_id;
1569              l_exp_txn_rec.source_header_ref        :=  to_char(r_so_info.order_number);
1570              l_exp_txn_rec.source_line_ref          :=  substr(to_char(r_so_info.line_number)||'.'||l_mtl_item_tbl(i).shipment_number,1,50);
1571 
1572              -- Current Object Version ID since the instance was Just Created
1573              SELECT object_version_number
1574              INTO l_curr_object_vers_61_id
1575              FROM csi_item_instances
1576              WHERE instance_id = l_expire_instance_rec.instance_id;
1577 
1578              l_expire_instance_rec.object_version_number   :=  l_curr_object_vers_61_id;
1579 
1580                IF (l_debug > 0) THEN
1581                  csi_t_gen_utility_pvt.add('Before Expiring Item Instance-36.1: '||l_new_dest_instance_rec.instance_id);
1582                  csi_t_gen_utility_pvt.add('l_upd_error_instance_id is: '||l_upd_error_instance_id);
1583                  csi_t_gen_utility_pvt.add('l_curr_object_vers_61_id is: '||l_curr_object_vers_61_id);
1584                END IF;
1585 
1586                csi_item_instance_pub.expire_item_instance(l_api_version,
1587                                                           l_commit,
1588                                                           l_init_msg_list,
1589                                                           l_validation_level,
1590                                                           l_expire_instance_rec,
1591                                                           l_expire_children,
1592                                                           l_exp_txn_rec,
1593                                                           l_instance_id_lst,
1594                                                           l_return_status,
1595                                                           l_msg_count,
1596                                                           l_msg_data);
1597 
1598              IF (l_debug > 0) THEN
1599                csi_t_gen_utility_pvt.add('After Expire Item Instance-36.2');
1600                csi_t_gen_utility_pvt.add('l_upd_error_instance_id is: '||l_upd_error_instance_id);
1601              END IF;
1602 
1603              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
1604              IF NOT l_return_status = l_fnd_success then
1605                IF (l_debug > 0) THEN
1606                      csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.c API '||l_msg_data);
1607                END IF;
1608                l_msg_index := 1;
1609                  WHILE l_msg_count > 0 loop
1610 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
1611 	           l_msg_index := l_msg_index + 1;
1612                    l_msg_count := l_msg_count - 1;
1613                  END LOOP;
1614 	         RAISE fnd_api.g_exc_error;
1615              END IF;
1616 
1617            ELSIF l_dest_instance_header_tbl.count = 1 THEN -- Installed Base Destination Records Found
1618 
1619              IF (l_debug > 0) THEN
1623              END IF;
1620                csi_t_gen_utility_pvt.add('Instance Usage Code: '||l_dest_instance_header_tbl(i).instance_usage_code);
1621                csi_t_gen_utility_pvt.add('Item ID: '||l_dest_instance_header_tbl(i).inventory_item_id);
1622                csi_t_gen_utility_pvt.add('Instance ID: '||l_dest_instance_header_tbl(i).instance_id);
1624 
1625             l_update_dest_instance_rec                          :=  csi_inv_trxs_pkg.init_instance_update_rec;
1626             l_update_dest_instance_rec.instance_id              :=  l_dest_instance_header_tbl(i).instance_id;
1627             l_update_dest_instance_rec.quantity                 :=  1;
1628             -- Bug 5253131
1629             l_update_dest_instance_rec.location_type_code       :=  csi_inv_trxs_pkg.get_location_type_code('INTERNAL_SITE');
1630             l_update_dest_instance_rec.last_oe_order_line_id    :=  r_so_info.line_id;
1631 	    -- Added for Bug 5975739
1632 	    l_update_dest_instance_rec.inv_master_organization_id   :=  l_master_organization_id;
1633             l_update_dest_instance_rec.inv_organization_id      :=  NULL;
1634             l_update_dest_instance_rec.vld_organization_id      :=  l_mtl_item_tbl(j).organization_id;
1635             l_update_dest_instance_rec.inv_subinventory_name    :=  NULL;
1636             l_update_dest_instance_rec.inv_locator_id           :=  NULL;
1637             l_update_dest_instance_rec.instance_usage_code      :=  l_out_of_enterprise;
1638             l_update_dest_instance_rec.active_end_date          :=  l_sysdate;
1639             l_update_dest_instance_rec.object_version_number    :=  l_dest_instance_header_tbl(i).object_version_number;
1640 
1641             l_party_tbl.delete;
1642             l_account_tbl.delete;
1643             l_pricing_attrib_tbl.delete;
1644             l_org_assignments_tbl.delete;
1645             l_asset_assignment_tbl.delete;
1646 
1647             l_update_dest_instance_rec.instance_status_id := nvl(csi_inv_trxs_pkg.get_default_status_id(l_txn_rec.transaction_type_id),r_id.instance_status_id);
1648 
1649 
1650                IF (l_debug > 0) THEN
1651                      csi_t_gen_utility_pvt.add('Before Update Item Instance-37');
1652                      csi_t_gen_utility_pvt.add('Instance Status-38: '||l_update_dest_instance_rec.instance_status_id);
1653 
1654                END IF;
1655 
1656                csi_item_instance_pub.update_item_instance(l_api_version,
1657                                                           l_commit,
1658                                                           l_init_msg_list,
1659                                                           l_validation_level,
1660                                                           l_update_dest_instance_rec,
1661                                                           l_ext_attrib_values_tbl,
1662                                                           l_party_tbl,
1663                                                           l_account_tbl,
1664                                                           l_pricing_attrib_tbl,
1665                                                           l_org_assignments_tbl,
1666                                                           l_asset_assignment_tbl,
1667                                                           l_txn_rec,
1668                                                           l_instance_id_lst,
1669                                                           l_return_status,
1670                                                           l_msg_count,
1671                                                           l_msg_data);
1672 
1673              l_upd_error_instance_id := NULL;
1674              l_upd_error_instance_id := l_update_dest_instance_rec.instance_id;
1675 
1676              IF (l_debug > 0) THEN
1677                csi_t_gen_utility_pvt.add('After Update Item Instance-39');
1678                csi_t_gen_utility_pvt.add('l_upd_error_instance_id is: '||l_upd_error_instance_id);
1679              END IF;
1680 
1681              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
1682              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
1683                IF (l_debug > 0) THEN
1684                      csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.c API '||l_msg_data);
1685                END IF;
1686                l_msg_index := 1;
1687                  WHILE l_msg_count > 0 loop
1688 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
1689 	           l_msg_index := l_msg_index + 1;
1690                    l_msg_count := l_msg_count - 1;
1691                  END LOOP;
1692 	         RAISE fnd_api.g_exc_error;
1693              END IF;
1694 
1695           END IF; -- Destination IF
1696 
1697          IF j = 1 THEN -- Look for IN Transit Non Serial If not there create or Update only 1 time
1698            l_instance_query_rec                               :=  csi_inv_trxs_pkg.init_instance_query_rec;
1699            l_instance_query_rec.inventory_item_id             :=  l_mtl_item_tbl(j).inventory_item_id;
1700            l_instance_query_rec.serial_number                 :=  NULL;
1701            l_instance_query_rec.lot_number                    :=  l_mtl_item_tbl(j).lot_number;
1702            l_instance_query_rec.inv_locator_id                :=  l_mtl_item_tbl(j).locator_id;
1703            l_instance_query_rec.instance_usage_code           :=  l_in_transit;
1704            l_instance_query_rec.in_transit_order_line_id      :=  r_so_info.line_id;
1705 
1706            l_instance_usage_code                              :=  l_in_transit;
1707            l_subinventory_name                                :=  NULL;
1708            l_organization_id                                  :=  l_mtl_item_tbl(j).organization_id;
1709            l_locator_id                                       :=  NULL;
1710 
1711            l_mfg_flag := NULL;
1712            l_serial_number := NULL;
1716                  csi_t_gen_utility_pvt.add('Since the Shipping Code is 6 and the Receiving is 1 Look for Non Serial In Transit');
1713            l_quantity := abs(l_mtl_item_tbl(j).transaction_quantity);
1714 
1715             IF (l_debug > 0) THEN
1717            END IF;
1718 
1719          IF (l_debug > 0) THEN
1720            csi_t_gen_utility_pvt.add('Before Get Item Instance for Dest Serialized Instance-40');
1721          END IF;
1722 
1723            csi_item_instance_pub.get_item_instances(l_api_version,
1724                                                     l_commit,
1725                                                     l_init_msg_list,
1726                                                     l_validation_level,
1727                                                     l_instance_query_rec,
1728                                                     l_party_query_rec,
1729                                                     l_account_query_rec,
1730                                                     l_transaction_id,
1731                                                     l_resolve_id_columns,
1732                                                     l_inactive_instance_only,
1733                                                     l_dest_instance_header_tbl,
1734                                                     l_return_status,
1735                                                     l_msg_count,
1736                                                     l_msg_data);
1737 
1738            IF (l_debug > 0) THEN
1739                  csi_t_gen_utility_pvt.add('After Get Item Instance-41');
1740            END IF;
1741            l_tbl_count := 0;
1742            l_tbl_count :=  l_dest_instance_header_tbl.count;
1743            IF (l_debug > 0) THEN
1744                  csi_t_gen_utility_pvt.add('Source Records Found: '||l_tbl_count);
1745            END IF;
1746 
1747            -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
1748            IF NOT l_return_status = l_fnd_success then
1749              IF (l_debug > 0) THEN
1750                    csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.get_item_instance API '||l_msg_data);
1751              END IF;
1752              l_msg_index := 1;
1753 	         WHILE l_msg_count > 0 loop
1754 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
1755 	           l_msg_index := l_msg_index + 1;
1756                l_msg_count := l_msg_count - 1;
1757   	         END LOOP;
1758 	         RAISE fnd_api.g_exc_error;
1759             END IF;
1760 
1761            IF l_dest_instance_header_tbl.count < 1 THEN  -- Installed Base Destination Records are not found so create a new record
1762 
1763              IF (l_debug > 0) THEN
1764                    csi_t_gen_utility_pvt.add('Creating New Dest dest Instance-42');
1765              END IF;
1766 
1767              l_new_dest_instance_rec                              :=  csi_inv_trxs_pkg.init_instance_create_rec;
1768              l_new_dest_instance_rec.inventory_item_id            :=  l_mtl_item_tbl(j).inventory_item_id;
1769              l_new_dest_instance_rec.inventory_revision           :=  l_mtl_item_tbl(j).revision;
1770              l_new_dest_instance_rec.inv_master_organization_id   :=  l_master_organization_id;
1771              l_new_dest_instance_rec.mfg_serial_number_flag       :=  l_mfg_flag;
1772              l_new_dest_instance_rec.serial_number                :=  l_serial_number;
1773              l_new_dest_instance_rec.lot_number                   :=  l_mtl_item_tbl(j).lot_number;
1774              l_new_dest_instance_rec.quantity                     :=  l_quantity;
1775              l_new_dest_instance_rec.unit_of_measure              :=  l_mtl_item_tbl(j).transaction_uom;
1776              l_new_dest_instance_rec.location_type_code           :=  csi_inv_trxs_pkg.get_location_type_code('In_Transit');
1777              l_new_dest_instance_rec.in_transit_order_line_id     :=  r_so_info.line_id;
1778              l_new_dest_instance_rec.last_oe_order_line_id  :=  r_so_info.line_id;
1779              l_new_dest_instance_rec.instance_usage_code          :=  l_instance_usage_code;
1780              l_new_dest_instance_rec.vld_organization_id          :=  l_mtl_item_tbl(j).transfer_organization_id;
1781              l_new_dest_instance_rec.inv_subinventory_name        :=  l_subinventory_name;
1782              l_new_dest_instance_rec.inv_locator_id               :=  l_locator_id;
1783              l_new_dest_instance_rec.customer_view_flag           :=  'N';
1784              l_new_dest_instance_rec.merchant_view_flag           :=  'Y';
1785              l_new_dest_instance_rec.operational_status_code      :=  'NOT_USED';
1786              l_new_dest_instance_rec.object_version_number        :=  l_object_version_number;
1787              l_new_dest_instance_rec.active_start_date            :=  l_sysdate;
1788              l_new_dest_instance_rec.active_end_date              :=  NULL;
1789 
1790              l_ext_attrib_values_tbl                              :=  csi_inv_trxs_pkg.init_ext_attrib_values_tbl;
1791              l_party_tbl                                          :=  csi_inv_trxs_pkg.init_party_tbl;
1792              l_account_tbl                                        :=  csi_inv_trxs_pkg.init_account_tbl;
1793              l_pricing_attrib_tbl                                 :=  csi_inv_trxs_pkg.init_pricing_attribs_tbl;
1794              l_org_assignments_tbl                                :=  csi_inv_trxs_pkg.init_org_assignments_tbl;
1795              l_asset_assignment_tbl                               :=  csi_inv_trxs_pkg.init_asset_assignment_tbl;
1796 
1797              IF (l_debug > 0) THEN
1798                    csi_t_gen_utility_pvt.add('Before Create Item Instance-43');
1799              END IF;
1800 
1801              csi_item_instance_pub.create_item_instance(l_api_version,
1802                                                         l_commit,
1803                                                         l_init_msg_list,
1807                                                         l_party_tbl,
1804                                                         l_validation_level,
1805                                                         l_new_dest_instance_rec,
1806                                                         l_ext_attrib_values_tbl,
1808                                                         l_account_tbl,
1809                                                         l_pricing_attrib_tbl,
1810                                                         l_org_assignments_tbl,
1811                                                         l_asset_assignment_tbl,
1812                                                         l_txn_rec,
1813                                                         l_return_status,
1814                                                         l_msg_count,
1815                                                         l_msg_data);
1816 
1817              IF (l_debug > 0) THEN
1818                    csi_t_gen_utility_pvt.add('After Create Item Instance-44');
1819              END IF;
1820              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
1821 
1822              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
1823                IF (l_debug > 0) THEN
1824                      csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.create_item_instance API '||l_msg_data);
1825                END IF;
1826                l_msg_index := 1;
1827                  WHILE l_msg_count > 0 loop
1828 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
1829 	           l_msg_index := l_msg_index + 1;
1830                    l_msg_count := l_msg_count - 1;
1831   	         END LOOP;
1832 	         RAISE fnd_api.g_exc_error;
1833              END IF;
1834 
1835            ELSIF l_dest_instance_header_tbl.count = 1 THEN -- Installed Base Destination Records Found
1836 
1837              IF (l_debug > 0) THEN
1838                csi_t_gen_utility_pvt.add('Instance Usage Code: '||l_dest_instance_header_tbl(i).instance_usage_code);
1839                csi_t_gen_utility_pvt.add('Item ID: '||l_dest_instance_header_tbl(i).inventory_item_id);
1840                csi_t_gen_utility_pvt.add('Instance ID: '||l_dest_instance_header_tbl(i).instance_id);
1841              END IF;
1842 
1843             l_update_dest_instance_rec                          :=  csi_inv_trxs_pkg.init_instance_update_rec;
1844             l_update_dest_instance_rec.instance_id              :=  l_dest_instance_header_tbl(i).instance_id;
1845             l_update_dest_instance_rec.quantity                 :=  l_dest_instance_header_tbl(i).quantity + l_quantity;
1846             l_update_dest_instance_rec.location_type_code       :=  csi_inv_trxs_pkg.get_location_type_code('In_Transit');
1847             l_update_dest_instance_rec.in_transit_order_line_id :=  r_so_info.line_id;
1848 	    -- Added for Bug 5975739
1849 	    l_update_dest_instance_rec.inv_master_organization_id   :=  l_master_organization_id;
1850             l_update_dest_instance_rec.last_oe_order_line_id  :=  r_so_info.line_id;
1851             l_update_dest_instance_rec.inv_organization_id      :=  NULL;
1852             l_update_dest_instance_rec.inv_subinventory_name    :=  l_subinventory_name;
1853             l_update_dest_instance_rec.inv_locator_id           :=  l_locator_id;
1854             l_update_dest_instance_rec.instance_usage_code      :=  l_in_transit;
1855             l_update_dest_instance_rec.active_end_date          :=  NULL;
1856             l_update_dest_instance_rec.active_end_date          :=  NULL;
1857             l_update_dest_instance_rec.object_version_number    :=  l_dest_instance_header_tbl(i).object_version_number;
1858 
1859             l_party_tbl.delete;
1860             l_account_tbl.delete;
1861             l_pricing_attrib_tbl.delete;
1862             l_org_assignments_tbl.delete;
1863             l_asset_assignment_tbl.delete;
1864 
1865                l_update_dest_instance_rec.instance_status_id := nvl(csi_inv_trxs_pkg.get_default_status_id(l_txn_rec.transaction_type_id),r_id.instance_status_id);
1866 
1867                IF (l_debug > 0) THEN
1868                      csi_t_gen_utility_pvt.add('Before Update Item Instance-45');
1869                      csi_t_gen_utility_pvt.add('Instance Status-46: '||l_update_dest_instance_rec.instance_status_id);
1870 
1871                END IF;
1872 
1873                csi_item_instance_pub.update_item_instance(l_api_version,
1874                                                           l_commit,
1875                                                           l_init_msg_list,
1876                                                           l_validation_level,
1877                                                           l_update_dest_instance_rec,
1878                                                           l_ext_attrib_values_tbl,
1879                                                           l_party_tbl,
1880                                                           l_account_tbl,
1881                                                           l_pricing_attrib_tbl,
1882                                                           l_org_assignments_tbl,
1883                                                           l_asset_assignment_tbl,
1884                                                           l_txn_rec,
1885                                                           l_instance_id_lst,
1886                                                           l_return_status,
1887                                                           l_msg_count,
1888                                                           l_msg_data);
1889 
1890              l_upd_error_instance_id := NULL;
1891              l_upd_error_instance_id := l_update_dest_instance_rec.instance_id;
1892 
1893              IF (l_debug > 0) THEN
1894                csi_t_gen_utility_pvt.add('After Update Item Instance-47');
1895                csi_t_gen_utility_pvt.add('l_upd_error_instance_id is: '||l_upd_error_instance_id);
1896              END IF;
1897 
1901                      csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.c API '||l_msg_data);
1898              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
1899              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
1900                IF (l_debug > 0) THEN
1902                END IF;
1903                l_msg_index := 1;
1904                  WHILE l_msg_count > 0 loop
1905 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
1906 	           l_msg_index := l_msg_index + 1;
1907                    l_msg_count := l_msg_count - 1;
1908                  END LOOP;
1909 	         RAISE fnd_api.g_exc_error;
1910              END IF;
1911 
1912            ELSE -- Error No dest non serial recs round
1913             IF (l_debug > 0) THEN
1914               csi_t_gen_utility_pvt.add('No Records were found in Install Base but the usage is not correct-20, The Usage is: '||l_dest_instance_header_tbl(i).instance_usage_code);
1915             END IF;
1916             fnd_message.set_name('CSI','CSI_IB_RECORD_NOTFOUND');
1917             fnd_message.set_token('ITEM',l_mtl_item_tbl(j).inventory_item_id);
1918             fnd_message.set_token('SUBINVENTORY',l_mtl_item_tbl(j).subinventory_code);
1919             fnd_message.set_token('ORG_ID',l_mtl_item_tbl(j).organization_id);
1920             fnd_message.set_token('ITEM',l_mtl_item_tbl(j).inventory_item_id);
1921             l_error_message := fnd_message.get;
1922             RAISE fnd_api.g_exc_error;
1923 
1924             end if; --End of Destination Record If
1925 
1926           END IF;    -- End of j=1 for Control Code 1
1927          END IF;     -- serial Control 1 or 6 IF
1928         END IF;      -- l_receipt_action_flag
1929        END IF;       -- Serial If Statement
1930      END LOOP;       -- End of For Loop
1931 
1932      IF (l_debug > 0) THEN
1933            csi_t_gen_utility_pvt.add('End time: '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SS'));
1934            csi_t_gen_utility_pvt.add('******End of csi_inv_iso_pkg.iso_shipment Transaction******');
1935      END IF;
1936 
1937     EXCEPTION
1938      WHEN fnd_api.g_exc_error THEN
1939        IF (l_debug > 0) THEN
1940              csi_t_gen_utility_pvt.add('You have encountered a "fnd_api.g_exc_error" exception in the Internal Sales Order Transaction - In Transit Shipment');
1941        END IF;
1942        x_return_status := l_fnd_error;
1943 
1944        IF l_mtl_item_tbl.count > 0 THEN
1945          x_trx_error_rec.serial_number := l_mtl_item_tbl(j).serial_number;
1946          x_trx_error_rec.lot_number := l_mtl_item_tbl(j).lot_number;
1947          x_trx_error_rec.instance_id := l_upd_error_instance_id;
1948          x_trx_error_rec.inventory_item_id := l_mtl_item_tbl(j).inventory_item_id;
1949          x_trx_error_rec.src_serial_num_ctrl_code := l_mtl_item_tbl(j).serial_number_control_code;
1950          x_trx_error_rec.src_location_ctrl_code := l_mtl_item_tbl(j).location_control_code;
1951          x_trx_error_rec.src_lot_ctrl_code := l_mtl_item_tbl(j).lot_control_code;
1952          x_trx_error_rec.src_rev_qty_ctrl_code := l_mtl_item_tbl(j).revision_qty_control_code;
1953          x_trx_error_rec.dst_serial_num_ctrl_code := r_item_control.serial_number_control_code;
1954          x_trx_error_rec.dst_location_ctrl_code := r_item_control.location_control_code;
1955          x_trx_error_rec.dst_lot_ctrl_code := r_item_control.lot_control_code;
1956          x_trx_error_rec.dst_rev_qty_ctrl_code := r_item_control.revision_qty_control_code;
1957          x_trx_error_rec.comms_nl_trackable_flag := l_mtl_item_tbl(j).comms_nl_trackable_flag;
1958          x_trx_error_rec.transaction_error_date := l_sysdate ;
1959        END IF;
1960 
1961        x_trx_error_rec.error_text := l_error_message;
1962        x_trx_error_rec.transaction_id       := NULL;
1963        x_trx_error_rec.source_type          := 'CSIINTSS';
1964        x_trx_error_rec.source_id            := p_transaction_id;
1965        x_trx_error_rec.processed_flag       := csi_inv_trxs_pkg.g_txn_error;
1966        x_trx_error_rec.transaction_type_id  := csi_inv_trxs_pkg.get_txn_type_id(l_trans_type_code,l_trans_app_code);
1967        x_trx_error_rec.inv_material_transaction_id  := p_transaction_id;
1968        x_trx_error_rec.error_stage          := csi_inv_trxs_pkg.g_ib_update;
1969 
1970      WHEN others THEN
1971         l_sql_error := SQLERRM;
1972         IF (l_debug > 0) THEN
1973               csi_t_gen_utility_pvt.add('You have encountered a "when others" exception in the Internal Sales Order Transaction - In Transit Shipment');
1974               csi_t_gen_utility_pvt.add('SQL Error: '||l_sql_error);
1975         END IF;
1976         fnd_message.set_name('CSI','CSI_UNEXP_SQL_ERROR');
1977         fnd_message.set_token('API_NAME',l_api_name);
1978         fnd_message.set_token('SQL_ERROR',SQLERRM);
1979         x_return_status := l_fnd_unexpected;
1980 
1981         IF l_mtl_item_tbl.count > 0 THEN
1982           x_trx_error_rec.serial_number := l_mtl_item_tbl(j).serial_number;
1983           x_trx_error_rec.lot_number := l_mtl_item_tbl(j).lot_number;
1984           x_trx_error_rec.instance_id := l_upd_error_instance_id;
1985           x_trx_error_rec.inventory_item_id := l_mtl_item_tbl(j).inventory_item_id;
1986           x_trx_error_rec.src_serial_num_ctrl_code := l_mtl_item_tbl(j).serial_number_control_code;
1987           x_trx_error_rec.src_location_ctrl_code := l_mtl_item_tbl(j).location_control_code;
1988           x_trx_error_rec.src_lot_ctrl_code := l_mtl_item_tbl(j).lot_control_code;
1989           x_trx_error_rec.src_rev_qty_ctrl_code := l_mtl_item_tbl(j).revision_qty_control_code;
1990           x_trx_error_rec.dst_serial_num_ctrl_code := r_item_control.serial_number_control_code;
1991           x_trx_error_rec.dst_location_ctrl_code := r_item_control.location_control_code;
1992           x_trx_error_rec.dst_lot_ctrl_code := r_item_control.lot_control_code;
1996         END IF;
1993           x_trx_error_rec.dst_rev_qty_ctrl_code := r_item_control.revision_qty_control_code;
1994           x_trx_error_rec.comms_nl_trackable_flag := l_mtl_item_tbl(j).comms_nl_trackable_flag;
1995           x_trx_error_rec.transaction_error_date := l_sysdate ;
1997 
1998         x_trx_error_rec.error_text := fnd_message.get;
1999         x_trx_error_rec.transaction_id       := NULL;
2000         x_trx_error_rec.source_type          := 'CSIINTSS';
2001         x_trx_error_rec.source_id            := p_transaction_id;
2002         x_trx_error_rec.processed_flag       := csi_inv_trxs_pkg.g_txn_error;
2003         x_trx_error_rec.transaction_type_id  := csi_inv_trxs_pkg.get_txn_type_id(l_trans_type_code,l_trans_app_code);
2004         x_trx_error_rec.inv_material_transaction_id  := p_transaction_id;
2005         x_trx_error_rec.error_stage          := csi_inv_trxs_pkg.g_ib_update;
2006 
2007    END iso_shipment;
2008 
2009    PROCEDURE iso_receipt(p_transaction_id     IN  NUMBER,
2010                          p_message_id         IN  NUMBER,
2011                          x_return_status      OUT NOCOPY VARCHAR2,
2012                          x_trx_error_rec      OUT NOCOPY CSI_DATASTRUCTURES_PUB.TRANSACTION_ERROR_REC)
2013    IS
2014 
2015    l_mtl_item_tbl               CSI_INV_TRXS_PKG.MTL_ITEM_TBL_TYPE;
2016    l_api_name                   VARCHAR2(100)   := 'CSI_INV_ISO_PKG.ISO_RECEIPT';
2017    l_api_version                NUMBER          := 1.0;
2018    l_commit                     VARCHAR2(1)     := FND_API.G_FALSE;
2019    l_init_msg_list              VARCHAR2(1)     := FND_API.G_TRUE;
2020    l_validation_level           NUMBER          := FND_API.G_VALID_LEVEL_FULL;
2021    l_active_instance_only       VARCHAR2(10)    := FND_API.G_TRUE;
2022    l_inactive_instance_only     VARCHAR2(10)    := FND_API.G_FALSE;
2023    l_transaction_id             NUMBER          := NULL;
2024    l_resolve_id_columns         VARCHAR2(10)    := FND_API.G_FALSE;
2025    l_object_version_number      NUMBER          := 1;
2026    l_sysdate                    DATE            := SYSDATE;
2027    l_master_organization_id     NUMBER;
2028    l_depreciable                VARCHAR2(1);
2029    l_instance_query_rec         CSI_DATASTRUCTURES_PUB.INSTANCE_QUERY_REC;
2030    l_dest_instance_query_rec    CSI_DATASTRUCTURES_PUB.INSTANCE_QUERY_REC;
2031    l_update_instance_rec        CSI_DATASTRUCTURES_PUB.INSTANCE_REC;
2032    l_upd_src_dest_instance_rec  CSI_DATASTRUCTURES_PUB.INSTANCE_REC;
2033    l_update_dest_instance_rec   CSI_DATASTRUCTURES_PUB.INSTANCE_REC;
2034    l_new_instance_rec           CSI_DATASTRUCTURES_PUB.INSTANCE_REC;
2035    l_new_dest_instance_rec      CSI_DATASTRUCTURES_PUB.INSTANCE_REC;
2036    l_new_src_instance_rec       CSI_DATASTRUCTURES_PUB.INSTANCE_REC;
2037    l_txn_rec                    CSI_DATASTRUCTURES_PUB.TRANSACTION_REC;
2038    l_return_status              VARCHAR2(1);
2039    l_error_code                 VARCHAR2(50);
2040    l_error_message              VARCHAR2(4000);
2041    l_instance_id_lst            CSI_DATASTRUCTURES_PUB.ID_TBL;
2042    l_party_query_rec            CSI_DATASTRUCTURES_PUB.PARTY_QUERY_REC;
2043    l_account_query_rec          CSI_DATASTRUCTURES_PUB.PARTY_ACCOUNT_QUERY_REC;
2044    l_instance_header_tbl        CSI_DATASTRUCTURES_PUB.INSTANCE_HEADER_TBL;
2045    l_src_instance_header_tbl    CSI_DATASTRUCTURES_PUB.INSTANCE_HEADER_TBL;
2046    l_dest_instance_header_tbl   CSI_DATASTRUCTURES_PUB.INSTANCE_HEADER_TBL;
2047    l_ext_attrib_values_tbl      CSI_DATASTRUCTURES_PUB.EXTEND_ATTRIB_VALUES_TBL;
2048    l_party_tbl                  CSI_DATASTRUCTURES_PUB.PARTY_TBL;
2049    l_account_tbl                CSI_DATASTRUCTURES_PUB.PARTY_ACCOUNT_TBL;
2050    l_pricing_attrib_tbl         CSI_DATASTRUCTURES_PUB.PRICING_ATTRIBS_TBL;
2051    l_org_assignments_tbl        CSI_DATASTRUCTURES_PUB.ORGANIZATION_UNITS_TBL;
2052    l_asset_assignment_tbl       CSI_DATASTRUCTURES_PUB.INSTANCE_ASSET_TBL;
2053    l_sub_inventory              VARCHAR2(10);
2054    l_location_type              VARCHAR2(20);
2055    l_trx_action_type            VARCHAR2(50);
2056    l_fnd_success                VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
2057    l_fnd_warning                VARCHAR2(1) := 'W';
2058    l_fnd_error                  VARCHAR2(1) := FND_API.G_RET_STS_ERROR;
2059    l_fnd_unexpected             VARCHAR2(1) := FND_API.G_RET_STS_UNEXP_ERROR;
2060    l_fnd_g_num                  NUMBER      := FND_API.G_MISS_NUM;
2061    l_fnd_g_char                 VARCHAR2(1) := FND_API.G_MISS_CHAR;
2062    l_fnd_g_date                 DATE        := FND_API.G_MISS_DATE;
2063    l_in_inventory               VARCHAR2(25) := CSI_INV_TRXS_PKG.G_IN_INVENTORY;
2064    l_in_transit                 VARCHAR2(25) := CSI_INV_TRXS_PKG.G_IN_TRANSIT;
2065    l_in_relationship            VARCHAR2(25) := 'IN_RELATIONSHIP';
2066    l_out_of_enterprise          VARCHAR2(25) := 'OUT_OF_ENTERPRISE';
2067    l_returned                   VARCHAR2(25) := 'RETURNED';
2068    l_instance_usage_code        VARCHAR2(25);
2069    l_organization_id            NUMBER;
2070    l_subinventory_name          VARCHAR2(10);
2071    l_locator_id                 NUMBER;
2072    l_transaction_error_id       NUMBER;
2073    l_trx_type_id                NUMBER;
2074    l_trans_type_code            VARCHAR2(25);
2075    l_trans_app_code             VARCHAR2(5);
2076    l_employee_id                NUMBER;
2077    l_file                       VARCHAR2(500);
2078    l_msg_count                  NUMBER;
2079    l_msg_data                   VARCHAR2(2000);
2080    l_sql_error                  VARCHAR2(2000);
2081    l_msg_index                  NUMBER;
2082    j                            PLS_INTEGER :=1;
2083    i                            PLS_INTEGER :=1;
2084    k                            PLS_INTEGER :=1;
2085    m                            PLS_INTEGER :=1;
2086    l_tbl_count                  NUMBER := 0;
2087    l_neg_code                   NUMBER := 0;
2088    l_instance_status            VARCHAR2(1);
2089    l_inv_org_iso                NUMBER;
2090    l_sr_control                 NUMBER := 0;
2091    l_12_loop                    NUMBER := 0;
2092    l_redeploy_flag              VARCHAR2(1);
2093    l_upd_error_instance_id      NUMBER := NULL;
2094 
2095    l_instance_header_rec     csi_datastructures_pub.instance_header_rec;
2096    l_party_header_tbl        csi_datastructures_pub.party_header_tbl;
2097    l_account_header_tbl      csi_datastructures_pub.party_account_header_tbl;
2098    l_org_header_tbl          csi_datastructures_pub.org_units_header_tbl;
2099    l_pricing_header_tbl      csi_datastructures_pub.pricing_attribs_tbl;
2100    l_ext_attrib_header_tbl   csi_datastructures_pub.extend_attrib_values_tbl;
2101    l_ext_attrib_def_tbl      csi_datastructures_pub.extend_attrib_tbl;
2102    l_asset_header_tbl        csi_datastructures_pub.instance_asset_header_tbl;
2103 
2104    cursor c_id is
2105      SELECT instance_status_id
2106      FROM   csi_instance_statuses
2107      WHERE  name = FND_PROFILE.VALUE('CSI_DEFAULT_INSTANCE_STATUS');
2108 
2109    r_id     c_id%rowtype;
2110 
2111    CURSOR c_item_control (pc_item_id in number,
2112                           pc_org_id in number,
2113 					 p_transaction_id in number) is -- Added for bug#14835893
2114      SELECT serial_number_control_code,
2115            -- lot_control_code,
2116 		  nvl(csi_utl_pkg.get_lot_ctrl_code(p_transaction_id),lot_control_code) lot_control_code,-- Added for bug#14835893
2117             revision_qty_control_code,
2118             location_control_code,
2119             comms_nl_trackable_flag
2120      FROM mtl_system_items_b
2121      WHERE inventory_item_id = pc_item_id
2122      AND organization_id = pc_org_id;
2123 
2124    r_item_control     c_item_control%rowtype;
2125 
2126      --Cursor modified for bug 5023673--
2127    CURSOR c_intransit_line_id (pc_transaction_id IN NUMBER) IS
2128      SELECT m2.trx_source_line_id
2129      FROM mtl_material_transactions m1, mtl_material_transactions m2
2130      WHERE m1.transaction_id = pc_transaction_id
2131      AND m1.transfer_transaction_id = m2.transaction_id;
2132 
2133    r_intransit_line_id     c_intransit_line_id%rowtype;
2134 
2135    CURSOR c_obj_version (pc_instance_id IN NUMBER) is
2136      SELECT object_version_number
2137      FROM   csi_item_instances
2138      WHERE  instance_id = pc_instance_id;
2139 
2140    CURSOR c_so_info (pc_line_id in NUMBER) is
2141      SELECT oeh.header_id,
2142             oel.line_id,
2143             oeh.order_number,
2144             oel.line_number
2145      FROM   oe_order_headers_all oeh,
2146             oe_order_lines_all oel
2147      WHERE oeh.header_id = oel.header_id
2148      AND   oel.line_id = pc_line_id;
2149 
2150 --   CURSOR c_so_info (pc_line_id in NUMBER) is
2151 --     SELECT oeh.header_id,
2152 --            oel.line_id,
2153 --            oeh.order_number,
2154 --            oel.line_number
2155 --     FROM   oe_order_headers_all oeh,
2156 --            oe_order_lines_all oel
2157 --     WHERE oeh.header_id = oel.header_id
2158 --     AND   oel.source_document_id = pc_line_id;
2159 
2160    r_so_info     c_so_info%rowtype;
2161 
2162    CURSOR c_xfer_trans_id (pc_xfer_id IN NUMBER) IS
2163      SELECT trx_source_line_id
2164      from mtl_material_transactions
2165      WHERE transaction_id = pc_xfer_id;
2166 
2167    r_xfer_trans_id     c_xfer_trans_id%rowtype;
2168 
2169    BEGIN
2170 
2171      x_return_status := l_fnd_success;
2172      l_error_message := NULL;
2173 
2174      IF (l_debug > 0) THEN
2175            csi_t_gen_utility_pvt.add('******Start of csi_inv_iso_pkg.iso_receipt Transaction procedure******');
2176            csi_t_gen_utility_pvt.add('Start time: '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SS'));
2177            csi_t_gen_utility_pvt.add('csiintsb.pls 115.27');
2178      END IF;
2179 
2180      IF (l_debug > 0) THEN
2181        csi_t_gen_utility_pvt.add('Transaction ID with is: '||p_transaction_id);
2182        csi_t_gen_utility_pvt.add('l_sysdate set to: '||to_char(l_sysdate,'DD-MON-YYYY HH24:MI:SS'));
2183      END IF;
2184 
2185      -- This procedure queries all of the Inventory Transaction Records and
2186      -- returns them as a table.
2187 
2188      csi_inv_trxs_pkg.get_transaction_recs(p_transaction_id,
2189                                            l_mtl_item_tbl,
2190                                            l_return_status,
2191                                            l_error_message);
2192 
2193      l_tbl_count := 0;
2194      l_tbl_count := l_mtl_item_tbl.count;
2195      IF (l_debug > 0) THEN
2196            csi_t_gen_utility_pvt.add('Inventory Records Found: '||l_tbl_count);
2197      END IF;
2198 
2199      IF NOT l_return_status = l_fnd_success THEN
2200        IF (l_debug > 0) THEN
2201              csi_t_gen_utility_pvt.add('You have encountered an error in CSI_INV_TRXS_PKG.get_transaction_recs, Transaction ID: '||p_transaction_id);
2202        END IF;
2203        RAISE fnd_api.g_exc_error;
2204      END IF;
2205 
2206      IF (l_debug > 0) THEN
2207       	  csi_t_gen_utility_pvt.add('Transaction Action ID: '||l_mtl_item_tbl(i).transaction_action_id);
2208       	  csi_t_gen_utility_pvt.add('Transaction Source Type ID: '||l_mtl_item_tbl(i).transaction_source_type_id);
2209       	  csi_t_gen_utility_pvt.add('Transaction Quantity: '||l_mtl_item_tbl(i).transaction_quantity);
2210      END IF;
2211 
2212      -- Get the Master Organization ID
2213      csi_inv_trxs_pkg.get_master_organization(l_mtl_item_tbl(i).organization_id,
2214                                               l_master_organization_id,
2215                                               l_return_status,
2216                                               l_error_message);
2217 
2218      IF NOT l_return_status = l_fnd_success THEN
2219        IF (l_debug > 0) THEN
2220              csi_t_gen_utility_pvt.add('You have encountered an error in csi_inv_trxs_pkg.get_master_organization, Organization ID: '||l_mtl_item_tbl(i).organization_id);
2221        END IF;
2222        RAISE fnd_api.g_exc_error;
2223      END IF;
2224 
2225      -- Call get_fnd_employee_id and get the employee id
2226      l_employee_id := csi_inv_trxs_pkg.get_fnd_employee_id(l_mtl_item_tbl(i).last_updated_by);
2227 
2228      IF l_employee_id = -1 THEN
2229        IF (l_debug > 0) THEN
2230              csi_t_gen_utility_pvt.add('The person who last updated this record: '||l_mtl_item_tbl(i).last_updated_by||' does not exist as a valid employee');
2231        END IF;
2232      END IF;
2233      IF (l_debug > 0) THEN
2234            csi_t_gen_utility_pvt.add('The Employee that is processing this Transaction is: '||l_employee_id);
2235      END IF;
2236 
2237      -- See if this is a depreciable Item to set the status of the transaction record
2238      csi_inv_trxs_pkg.check_depreciable(l_mtl_item_tbl(i).inventory_item_id,
2239      	                               l_depreciable,
2240 									   l_mtl_item_tbl(i).organization_id); --Added for Bug 13988660
2241 
2242      IF (l_debug > 0) THEN
2243            csi_t_gen_utility_pvt.add('Is this Item ID: '||l_mtl_item_tbl(i).inventory_item_id||', Depreciable :'||l_depreciable);
2244      END IF;
2245 
2246      -- Set so we only Query Valid Records.
2247      l_instance_status := FND_API.G_TRUE;
2248 
2249 	-- Determine Transaction Type for this
2250 
2251        l_trans_type_code := 'ISO_REQUISITION_RECEIPT';
2252        l_trans_app_code := 'INV';
2253 
2254      IF (l_debug > 0) THEN
2255            csi_t_gen_utility_pvt.add('Trans Type Code: '||l_trans_type_code);
2256            csi_t_gen_utility_pvt.add('Trans App Code: '||l_trans_app_code);
2257      END IF;
2258 
2259     -- Get Default Profile Instance Status
2260 
2261     OPEN c_id;
2262     FETCH c_id into r_id;
2263     CLOSE c_id;
2264 
2265     IF (l_debug > 0) THEN
2266       csi_t_gen_utility_pvt.add('Default Profile Status: '||r_id.instance_status_id);
2267     END IF;
2268 
2269      -- Added so that the SO_HEADER_ID and SO_LINE_ID can be added to
2270      -- the transaction record.
2271 
2272      IF (l_debug > 0) THEN
2273            csi_t_gen_utility_pvt.add('Transfer Transaction ID: '||l_mtl_item_tbl(j).transfer_transaction_id);
2274      END IF;
2275 
2276      OPEN c_xfer_trans_id (l_mtl_item_tbl(j).transfer_transaction_id);
2277      FETCH c_xfer_trans_id into r_xfer_trans_id;
2278      CLOSE c_xfer_trans_id;
2279 
2280      IF (l_debug > 0) THEN
2281            csi_t_gen_utility_pvt.add('Trx Source Line ID: '||r_xfer_trans_id.trx_source_line_id);
2282      END IF;
2283 
2284      OPEN c_so_info (r_xfer_trans_id.trx_source_line_id);
2285      --OPEN c_so_info (l_mtl_item_tbl(j).transaction_source_id);
2286      FETCH c_so_info into r_so_info;
2287      CLOSE c_so_info;
2288 
2289      IF (l_debug > 0) THEN
2290            csi_t_gen_utility_pvt.add('Sales Order Header: '||r_so_info.header_id);
2291            csi_t_gen_utility_pvt.add('Sales Order Line: '||r_so_info.line_id);
2292            csi_t_gen_utility_pvt.add('Order Number: '||r_so_info.order_number);
2293            csi_t_gen_utility_pvt.add('Line Number: '||r_so_info.line_number);
2294      END IF;
2295      -- Initialize Transaction Record
2296      l_txn_rec                          := csi_inv_trxs_pkg.init_txn_rec;
2297 
2298      -- Set Status based on redeployment
2299      IF l_depreciable = 'N' THEN
2300        IF l_mtl_item_tbl(i).serial_number is NOT NULL THEN
2301          csi_inv_trxs_pkg.get_redeploy_flag(l_mtl_item_tbl(i).inventory_item_id,
2302                                             l_mtl_item_tbl(i).serial_number,
2303                                             l_sysdate,
2304                                             l_redeploy_flag,
2305                                             l_return_status,
2306                                             l_error_message);
2307        END IF;
2308        IF l_redeploy_flag = 'Y' THEN
2309          l_txn_rec.transaction_status_code := csi_inv_trxs_pkg.g_pending;
2310        ELSE
2311          l_txn_rec.transaction_status_code := csi_inv_trxs_pkg.g_complete;
2312        END IF;
2313      ELSE
2314        l_txn_rec.transaction_status_code := csi_inv_trxs_pkg.g_pending;
2315      END IF;
2316 
2317      IF NOT l_return_status = l_fnd_success THEN
2318        IF (l_debug > 0) THEN
2319           csi_t_gen_utility_pvt.add('Redeploy Flag: '||l_redeploy_flag);
2320           csi_t_gen_utility_pvt.add('You have encountered an error in csi_inv_trxs_pkg.get_redeploy_flag: '||l_error_message);
2321        END IF;
2322        RAISE fnd_api.g_exc_error;
2323      END IF;
2324 
2325      IF (l_debug > 0) THEN
2326         csi_t_gen_utility_pvt.add('Redeploy Flag: '||l_redeploy_flag);
2327         csi_t_gen_utility_pvt.add('Trans Status Code: '||l_txn_rec.transaction_status_code);
2328      END IF;
2329 
2330      -- Create CSI Transaction to be used
2331      l_txn_rec.source_transaction_date  := l_mtl_item_tbl(i).transaction_date;
2332      l_txn_rec.transaction_date         := l_sysdate;
2333      l_txn_rec.transaction_type_id      :=
2334           csi_inv_trxs_pkg.get_txn_type_id(l_trans_type_code,l_trans_app_code);
2335      l_txn_rec.transaction_quantity     :=
2336           l_mtl_item_tbl(i).transaction_quantity;
2337      l_txn_rec.transaction_uom_code     :=  l_mtl_item_tbl(i).transaction_uom;
2338      l_txn_rec.transacted_by            :=  l_employee_id;
2339      l_txn_rec.transaction_action_code  :=  NULL;
2340      l_txn_rec.message_id               :=  p_message_id;
2341      l_txn_rec.inv_material_transaction_id  :=  p_transaction_id;
2342      l_txn_rec.object_version_number    :=  l_object_version_number;
2343      l_txn_rec.source_header_ref_id     :=  r_so_info.header_id;
2344      l_txn_rec.source_line_ref_id       :=  r_so_info.line_id;
2345      l_txn_rec.source_header_ref        :=  to_char(r_so_info.order_number);
2346      l_txn_rec.source_line_ref          :=  substr(to_char(r_so_info.line_number)||'.'||l_mtl_item_tbl(i).shipment_number,1,50);
2347 
2348      csi_inv_trxs_pkg.create_csi_txn(l_txn_rec,
2349                                      l_error_message,
2350                                      l_return_status);
2351 
2352      IF (l_debug > 0) THEN
2353         csi_t_gen_utility_pvt.add('CSI Transaction Created: '||l_txn_rec.transaction_id);
2354      END IF;
2355 
2356      IF NOT l_return_status = l_fnd_success THEN
2357        IF (l_debug > 0) THEN
2358           csi_t_gen_utility_pvt.add('You have encountered an error in csi_inv_trxs_pkg.create_csi_txn: '||p_transaction_id);
2359        END IF;
2360        RAISE fnd_api.g_exc_error;
2361      END IF;
2362 
2363      -- Now loop through the PL/SQL Table.
2364      j := 1;
2365 
2366      IF (l_debug > 0) THEN
2367            csi_t_gen_utility_pvt.add('Starting to loop through Material Transaction Records');
2368      END IF;
2369 
2370      FOR j in l_mtl_item_tbl.FIRST .. l_mtl_item_tbl.LAST LOOP
2371 
2372     -- Get Line ID from Shipment Number
2373 
2374     OPEN c_intransit_line_id(p_transaction_id); --Modified for bug 5023673--
2375     FETCH c_intransit_line_id into r_intransit_line_id;
2376     CLOSE c_intransit_line_id;
2377 
2378     IF (l_debug > 0) THEN
2379       csi_t_gen_utility_pvt.add('Shipment Number: '||l_mtl_item_tbl(j).shipment_number);
2380       csi_t_gen_utility_pvt.add('Previous Line ID for Shipment: '||r_intransit_line_id.trx_source_line_id);
2381     END IF;
2382 
2383      IF (l_debug > 0) THEN
2384        csi_t_gen_utility_pvt.add('Primary UOM: '||l_mtl_item_tbl(j).primary_uom_code);
2385        csi_t_gen_utility_pvt.add('Primary Qty: '||l_mtl_item_tbl(j).primary_quantity);
2386        csi_t_gen_utility_pvt.add('Transaction UOM: '||l_mtl_item_tbl(j).transaction_uom);
2387        csi_t_gen_utility_pvt.add('Transaction Qty: '||l_mtl_item_tbl(j).transaction_quantity);
2388        csi_t_gen_utility_pvt.add('Organization ID: '||l_mtl_item_tbl(j).organization_id);
2389        csi_t_gen_utility_pvt.add('Transfer Org ID: '||l_mtl_item_tbl(j).transfer_organization_id);
2390      END IF;
2391 
2392      -- Get Shipping Organization Serial Control Code
2393      OPEN c_item_control (l_mtl_item_tbl(j).inventory_item_id,
2394                           l_mtl_item_tbl(j).transfer_organization_id
2395 					 ,p_transaction_id);--Added for bug# 14835893
2396      FETCH c_item_control into r_item_control;
2397      CLOSE c_item_control;
2398 
2399      l_sr_control := r_item_control.serial_number_control_code;
2400 
2401      IF (l_debug > 0) THEN
2402         csi_t_gen_utility_pvt.add('Serial Number : '||l_mtl_item_tbl(j).serial_number);
2403         csi_t_gen_utility_pvt.add('l_sr_control is: '||l_sr_control);
2404         csi_t_gen_utility_pvt.add('Serial Number Control Code: '||l_mtl_item_tbl(j).serial_number_control_code);
2405         csi_t_gen_utility_pvt.add('Receiving Org Serial Number Control Code: '||l_mtl_item_tbl(j).serial_number_control_code);
2406         csi_t_gen_utility_pvt.add('Shipping Org Serial Number Control Code: '||r_item_control.serial_number_control_code);
2407         csi_t_gen_utility_pvt.add('Receiving Org Lot Control Code: '||l_mtl_item_tbl(j).lot_control_code);
2408         csi_t_gen_utility_pvt.add('Shipping Org Lot Control Code: '||r_item_control.lot_control_code);
2409         csi_t_gen_utility_pvt.add('Receiving Org Loction Control Code: '||l_mtl_item_tbl(j).location_control_code);
2410         csi_t_gen_utility_pvt.add('Shipping Org Location Control Code: '||r_item_control.location_control_code);
2414      END IF;
2411         csi_t_gen_utility_pvt.add('Receiving Org Revision Control Code: '||l_mtl_item_tbl(j).revision_qty_control_code);
2412         csi_t_gen_utility_pvt.add('Shipping Org Revision Control Code: '||r_item_control.revision_qty_control_code);
2413         csi_t_gen_utility_pvt.add('Shipping Org Trackable Flag: '||r_item_control.comms_nl_trackable_flag);
2415 
2416          l_instance_query_rec                                 :=  csi_inv_trxs_pkg.init_instance_query_rec;
2417          l_instance_usage_code                                :=  l_fnd_g_char;
2418 
2419        --In Transit Receipt
2420          l_instance_query_rec.inventory_item_id               :=  l_mtl_item_tbl(j).inventory_item_id;
2421          l_instance_query_rec.serial_number                   :=  l_mtl_item_tbl(j).serial_number;
2422           --Added this IF construct for bug 6137231
2423          IF r_item_control.lot_control_code = 2 AND l_mtl_item_tbl(j).lot_control_code = 2 THEN
2424             l_instance_query_rec.lot_number                      :=  l_mtl_item_tbl(j).lot_number;
2425 	 END IF;
2426          l_instance_query_rec.inventory_revision              :=  l_mtl_item_tbl(j).revision;
2427          l_instance_query_rec.instance_usage_code             :=  l_in_transit;
2428          l_instance_query_rec.location_type_code              :=  csi_inv_trxs_pkg.get_location_type_code('In_Transit');
2429          l_trx_action_type := 'IN_TRANSIT_RECEIPT';
2430          l_instance_usage_code := l_instance_query_rec.instance_usage_code;
2431 
2432       IF (l_mtl_item_tbl(j).serial_number_control_code in (2,5) AND
2433           l_sr_control = 6) OR
2434          (l_mtl_item_tbl(j).serial_number_control_code in (2,5) AND
2435           l_sr_control in (2,5)) OR
2436          (l_mtl_item_tbl(j).serial_number_control_code = 6 AND
2437           l_sr_control in (2,5))
2438           --OR (l_mtl_item_tbl(j).serial_number_control_code = 6 AND l_sr_control = 6) -- Bug 7836468
2439           THEN
2440 
2441          --l_instance_query_rec.inv_organization_id := l_mtl_item_tbl(j).organization_id;
2442          l_instance_query_rec.serial_number := l_mtl_item_tbl(j).serial_number;
2443          IF (l_debug > 0) THEN
2444                csi_t_gen_utility_pvt.add('Set Serial Number to Trans Record');
2445          END IF;
2446 
2447        -- Bug 7836468
2448        ELSIF (l_mtl_item_tbl(j).serial_number_control_code = 6 AND
2449           l_sr_control = 6) THEN
2450 
2451          csi_t_gen_utility_pvt.add('Serial Control 6 at Shipping and Receiving Org');
2452          csi_t_gen_utility_pvt.add('Setting - l_instance_query_rec.in_transit_order_line_id - ' || r_intransit_line_id.trx_source_line_id);
2453          l_instance_query_rec.serial_number := l_fnd_g_char;
2454          l_instance_query_rec.in_transit_order_line_id := r_intransit_line_id.trx_source_line_id;
2455        -- End Bug 7836468
2456 
2457        ELSIF (l_mtl_item_tbl(j).serial_number_control_code in (6,1) AND
2458               l_sr_control = 1) OR
2459              (l_mtl_item_tbl(j).serial_number_control_code in (2,5) AND
2460               l_sr_control = 1) THEN
2461          l_instance_query_rec.serial_number := NULL;
2462 	 l_instance_query_rec.in_transit_order_line_id := r_intransit_line_id.trx_source_line_id;
2463          --l_instance_query_rec.inv_organization_id := l_mtl_item_tbl(j).transfer_organization_id;
2464          IF (l_debug > 0) THEN
2465                csi_t_gen_utility_pvt.add('Set Serial Number to NULL');
2466          END IF;
2467        ELSIF (l_mtl_item_tbl(j).serial_number_control_code = 1 AND
2468               l_sr_control = 6) OR
2469              (l_mtl_item_tbl(j).serial_number_control_code = 1 AND
2470               l_sr_control in (2,5)) THEN
2471             l_instance_query_rec.serial_number := NULL;
2472 	    l_instance_query_rec.in_transit_order_line_id := r_intransit_line_id.trx_source_line_id;
2473          IF (l_debug > 0) THEN
2474                csi_t_gen_utility_pvt.add('Set Serial Number to NULL');
2475                csi_t_gen_utility_pvt.add('Pass into get item instance the Previous In Transit id ('||l_instance_query_rec.in_transit_order_line_id||')');
2476          END IF;
2477        END IF;
2478 
2479        IF (l_debug > 0) THEN
2480              csi_t_gen_utility_pvt.add('l_12_loop is:'|| l_12_loop);
2481              csi_t_gen_utility_pvt.add('If Count is 1 then bypass Get Item Instance');
2482        END IF;
2483 
2484 	  IF l_12_loop = 0 THEN
2485 
2486        IF (l_debug > 0) THEN
2487              csi_t_gen_utility_pvt.add('Transaction Action Type:'|| l_trx_action_type);
2488              csi_t_gen_utility_pvt.add('Before Get Item Instance-1');
2489        END IF;
2490 
2491        csi_item_instance_pub.get_item_instances(l_api_version,
2492                                                 l_commit,
2493                                                 l_init_msg_list,
2494                                                 l_validation_level,
2495                                                 l_instance_query_rec,
2496                                                 l_party_query_rec,
2497                                                 l_account_query_rec,
2498                                                 l_transaction_id,
2499                                                 l_resolve_id_columns,
2500                                                 l_instance_status,
2501                                                 l_src_instance_header_tbl,
2502                                                 l_return_status,
2503                                                 l_msg_count,
2504                                                 l_msg_data);
2505        END IF;
2506 
2507        IF (l_debug > 0) THEN
2508              csi_t_gen_utility_pvt.add('After Get Item Instance-2');
2509        END IF;
2510        l_tbl_count := 0;
2511        l_tbl_count := l_src_instance_header_tbl.count;
2512        IF (l_debug > 0) THEN
2513              csi_t_gen_utility_pvt.add('Source Records Found: '||l_tbl_count);
2517        IF NOT l_return_status = l_fnd_success then
2514        END IF;
2515 
2516        -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
2518          IF (l_debug > 0) THEN
2519                csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.get_item_instance API '||l_msg_data);
2520          END IF;
2521          l_msg_index := 1;
2522            WHILE l_msg_count > 0 loop
2523 	     l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
2524 	     l_msg_index := l_msg_index + 1;
2525              l_msg_count := l_msg_count - 1;
2526   	   END LOOP;
2527 	   RAISE fnd_api.g_exc_error;
2528        END IF;
2529 
2530      IF l_src_instance_header_tbl.count > 0 OR
2531 	   l_12_loop = 1 THEN -- Installed Base Records Found
2532        IF (l_debug > 0) THEN
2533              csi_t_gen_utility_pvt.add('Records exists so now check both Shipping and Rec Serial Control');
2534        END IF;
2535 
2536 
2537       IF (l_mtl_item_tbl(j).serial_number_control_code in (2,5) AND
2538           l_sr_control in (2,5)) OR
2539          (l_mtl_item_tbl(j).serial_number_control_code in (2,5) AND
2540           l_sr_control = 6) THEN
2541 
2542              IF (l_debug > 0) THEN
2543                    csi_t_gen_utility_pvt.add('Serial Control at Shipping is 2,5 or 6 and Receiving are 2,5');
2544                    csi_t_gen_utility_pvt.add('Instance being updated: '||l_src_instance_header_tbl(i).instance_id);
2545              END IF;
2546 
2547              l_update_instance_rec                              :=  csi_inv_trxs_pkg.init_instance_update_rec;
2548              l_update_instance_rec.instance_id                  :=  l_src_instance_header_tbl(i).instance_id;
2549 	     -- Added for Bug 5975739
2550 	      l_update_instance_rec.inv_master_organization_id   :=  l_master_organization_id;
2551              l_update_instance_rec.inv_organization_id          :=  l_mtl_item_tbl(j).organization_id;
2552              l_update_instance_rec.vld_organization_id          :=  l_mtl_item_tbl(j).organization_id;
2553              l_update_instance_rec.inv_subinventory_name        :=  l_mtl_item_tbl(j).subinventory_code;
2554              l_update_instance_rec.inv_locator_id               :=  l_mtl_item_tbl(j).locator_id;
2555              l_update_instance_rec.location_id                  :=  nvl(l_mtl_item_tbl(j).subinv_location_id,l_mtl_item_tbl(j).hr_location_id);
2556              l_update_instance_rec.in_transit_order_line_id     :=  NULL;
2557              l_update_instance_rec.location_type_code           :=  csi_inv_trxs_pkg.get_location_type_code('Inventory');
2558              l_update_instance_rec.instance_usage_code          :=  l_in_inventory;
2559              l_update_instance_rec.object_version_number        :=  l_src_instance_header_tbl(i).object_version_number;
2560 	     --start of code for bug 6137231--
2561              IF r_item_control.lot_control_code = 2 AND l_mtl_item_tbl(j).lot_control_code = 1 THEN
2562                l_update_instance_rec.lot_number                   :=  NULL;
2563                csi_t_gen_utility_pvt.add('Lot control 2 and 1');
2564              ELSIF r_item_control.lot_control_code = 2 AND l_mtl_item_tbl(j).lot_control_code = 2 THEN
2565                l_update_instance_rec.lot_number                   :=  l_mtl_item_tbl(j).lot_number;
2566                csi_t_gen_utility_pvt.add('Lot control 2 and 2');
2567              ELSIF r_item_control.lot_control_code = 1 AND l_mtl_item_tbl(j).lot_control_code = 2 THEN
2568                l_update_instance_rec.lot_number                   :=  l_mtl_item_tbl(j).lot_number;
2569                csi_t_gen_utility_pvt.add('Lot control 1 and 2');
2570              END IF;---lot check
2571 	     --End of code for bug 6137231--
2572 
2573            IF (l_debug > 0) THEN
2574              csi_t_gen_utility_pvt.add('After you initialize the Update Record Values');
2575            END IF;
2576 
2577            l_party_tbl.delete;
2578            l_account_tbl.delete;
2579            l_pricing_attrib_tbl.delete;
2580            l_org_assignments_tbl.delete;
2581            l_asset_assignment_tbl.delete;
2582 
2583            IF (l_debug > 0) THEN
2584                  csi_t_gen_utility_pvt.add('Before Update Item Instance-3');
2585            END IF;
2586 
2587            csi_item_instance_pub.update_item_instance(l_api_version,
2588                                                       l_commit,
2589                                                       l_init_msg_list,
2590                                                       l_validation_level,
2591                                                       l_update_instance_rec,
2592                                                       l_ext_attrib_values_tbl,
2593                                                       l_party_tbl,
2594                                                       l_account_tbl,
2595                                                       l_pricing_attrib_tbl,
2596                                                       l_org_assignments_tbl,
2597                                                       l_asset_assignment_tbl,
2598                                                       l_txn_rec,
2599                                                       l_instance_id_lst,
2600                                                       l_return_status,
2601                                                       l_msg_count,
2602                                                       l_msg_data);
2603 
2604            l_upd_error_instance_id := NULL;
2605            l_upd_error_instance_id := l_update_instance_rec.instance_id;
2606 
2607            IF (l_debug > 0) THEN
2608                  csi_t_gen_utility_pvt.add('After Update Item Instance-4');
2609                  csi_t_gen_utility_pvt.add('You are updating Instance: '||l_update_instance_rec.instance_id);
2610                  csi_t_gen_utility_pvt.add('l_upd_error_instance_id is: '||l_upd_error_instance_id);
2611            END IF;
2612 
2616                    csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.update_item_instance API '||l_msg_data);
2613            -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
2614 	   IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
2615              IF (l_debug > 0) THEN
2617              END IF;
2618              l_msg_index := 1;
2619                WHILE l_msg_count > 0 loop
2620 	         l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
2621 	         l_msg_index := l_msg_index + 1;
2622                  l_msg_count := l_msg_count - 1;
2623   	       END LOOP;
2624 	       RAISE fnd_api.g_exc_error;
2625            END IF;
2626 
2627 	 -- Added case 2,4,6 for serial in transit being RETURNED - JPW
2628 
2629       ELSIF (l_mtl_item_tbl(j).serial_number_control_code = 1 AND
2630              l_sr_control = 6) OR
2631             (l_mtl_item_tbl(j).serial_number_control_code = 1 AND
2632              l_sr_control in (2,5)) THEN
2633 
2634              IF (l_debug > 0) THEN
2635                 csi_t_gen_utility_pvt.add('Serial Control at Shipping is 6,5 or 2 and Receiving is 1');
2636                 csi_t_gen_utility_pvt.add('Subtract Trans Qty from In Transit Non Serial Instance');
2637                 csi_t_gen_utility_pvt.add('Instance being updated: '||l_src_instance_header_tbl(i).instance_id);
2638              END IF;
2639 
2640              l_update_instance_rec                              :=  csi_inv_trxs_pkg.init_instance_update_rec;
2641              l_update_instance_rec.instance_id                  :=  l_src_instance_header_tbl(i).instance_id;
2642              l_update_instance_rec.quantity                     :=  l_src_instance_header_tbl(i).quantity - abs(l_mtl_item_tbl(j).primary_quantity);
2643              l_update_instance_rec.object_version_number        :=  l_src_instance_header_tbl(i).object_version_number;
2644 
2645            IF (l_debug > 0) THEN
2646               csi_t_gen_utility_pvt.add('After you initialize the Update Record Values');
2647               csi_t_gen_utility_pvt.add('Instance Updated: '||l_update_instance_rec.instance_id);
2648               csi_t_gen_utility_pvt.add('Object Version: '||l_update_instance_rec.object_version_number);
2649               csi_t_gen_utility_pvt.add('New Quantity: '||l_update_instance_rec.quantity);
2650            END IF;
2651 
2652            IF (l_debug > 0) THEN
2653               csi_t_gen_utility_pvt.add('After you initialize the Transaction Record Values');
2654            END IF;
2655 
2656            l_party_tbl.delete;
2657            l_account_tbl.delete;
2658            l_pricing_attrib_tbl.delete;
2659            l_org_assignments_tbl.delete;
2660            l_asset_assignment_tbl.delete;
2661 
2662            IF (l_debug > 0) THEN
2663               csi_t_gen_utility_pvt.add('Before Update Item Instance-5');
2664            END IF;
2665 
2666            csi_item_instance_pub.update_item_instance(l_api_version,
2667                                                       l_commit,
2668                                                       l_init_msg_list,
2669                                                       l_validation_level,
2670                                                       l_update_instance_rec,
2671                                                       l_ext_attrib_values_tbl,
2672                                                       l_party_tbl,
2673                                                       l_account_tbl,
2674                                                       l_pricing_attrib_tbl,
2675                                                       l_org_assignments_tbl,
2676                                                       l_asset_assignment_tbl,
2677                                                       l_txn_rec,
2678                                                       l_instance_id_lst,
2679                                                       l_return_status,
2680                                                       l_msg_count,
2681                                                       l_msg_data);
2682 
2683            l_upd_error_instance_id := NULL;
2684            l_upd_error_instance_id := l_update_instance_rec.instance_id;
2685 
2686            IF (l_debug > 0) THEN
2687               csi_t_gen_utility_pvt.add('After Update Item Instance-10.9');
2688               csi_t_gen_utility_pvt.add('You are updating Instance: '||l_update_instance_rec.instance_id);
2689               csi_t_gen_utility_pvt.add('You are updating Serial Number: '||l_update_instance_rec.serial_number);
2690               csi_t_gen_utility_pvt.add('l_upd_error_instance_id is: '||l_upd_error_instance_id);
2691            END IF;
2692 
2693            -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
2694            IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
2695              IF (l_debug > 0) THEN
2696                 csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.update_item_instance API '||l_msg_data);
2697              END IF;
2698              l_msg_index := 1;
2699                WHILE l_msg_count > 0 loop
2700 	         l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
2701 	         l_msg_index := l_msg_index + 1;
2702                 l_msg_count := l_msg_count - 1;
2703   	       END LOOP;
2704 	       RAISE fnd_api.g_exc_error;
2705            END IF;
2706 
2707          --  IF j = 1 THEN
2708            -- Now Query for Non Serialized In Inventory Record 1 Time Only
2709              l_instance_query_rec                               :=  csi_inv_trxs_pkg.init_instance_query_rec;
2710              l_instance_query_rec.inventory_item_id             :=  l_mtl_item_tbl(j).inventory_item_id;
2714              l_instance_query_rec.instance_usage_code           :=  l_in_inventory;
2711              l_instance_query_rec.inventory_revision            :=  l_mtl_item_tbl(j).revision;
2712              l_instance_query_rec.lot_number                    :=  l_mtl_item_tbl(j).lot_number;
2713              l_instance_query_rec.serial_number                 :=  NULL;
2715              l_instance_query_rec.inv_subinventory_name         :=  l_mtl_item_tbl(j).subinventory_code;
2716              l_instance_query_rec.inv_organization_id           :=  l_mtl_item_tbl(j).organization_id;
2717              l_instance_query_rec.inv_locator_id                :=  l_mtl_item_tbl(j).locator_id;
2718              l_instance_usage_code                              :=  l_instance_query_rec.instance_usage_code;
2719              l_subinventory_name                                :=  l_mtl_item_tbl(j).subinventory_code;
2720              l_organization_id                                  :=  l_mtl_item_tbl(j).organization_id;
2721              l_locator_id                                       :=  l_mtl_item_tbl(j).locator_id;
2722 
2723            IF (l_debug > 0) THEN
2724               csi_t_gen_utility_pvt.add('Before Get Dest Item Instance-7');
2725            END IF;
2726 
2727            csi_item_instance_pub.get_item_instances(l_api_version,
2728                                                     l_commit,
2729                                                     l_init_msg_list,
2730                                                     l_validation_level,
2731                                                     l_instance_query_rec,
2732                                                     l_party_query_rec,
2733                                                     l_account_query_rec,
2734                                                     l_transaction_id,
2735                                                     l_resolve_id_columns,
2736                                                     l_inactive_instance_only,
2737                                                     l_dest_instance_header_tbl,
2738                                                     l_return_status,
2739                                                     l_msg_count,
2740                                                     l_msg_data);
2741 
2742            IF (l_debug > 0) THEN
2743               csi_t_gen_utility_pvt.add('After Get Item Instance-8');
2744            END IF;
2745            l_tbl_count := 0;
2746            l_tbl_count :=  l_dest_instance_header_tbl.count;
2747            IF (l_debug > 0) THEN
2748               csi_t_gen_utility_pvt.add('Source Records Found: '||l_tbl_count);
2749            END IF;
2750 
2751            -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
2752            IF NOT l_return_status = l_fnd_success then
2753              IF (l_debug > 0) THEN
2754                 csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.get_item_instance API '||l_msg_data);
2755              END IF;
2756              l_msg_index := 1;
2757 	         WHILE l_msg_count > 0 loop
2758 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
2759 	           l_msg_index := l_msg_index + 1;
2760                l_msg_count := l_msg_count - 1;
2761   	         END LOOP;
2762 	         RAISE fnd_api.g_exc_error;
2763            END IF;
2764 
2765            IF l_dest_instance_header_tbl.count = 0 THEN  -- Installed Base Destination Records are not found so create a new record
2766 
2767              l_new_dest_instance_rec                              :=  csi_inv_trxs_pkg.init_instance_create_rec;
2768              l_new_dest_instance_rec.inventory_item_id            :=  l_mtl_item_tbl(j).inventory_item_id;
2769              l_new_dest_instance_rec.inventory_revision           :=  l_mtl_item_tbl(j).revision;
2770              l_new_dest_instance_rec.inv_master_organization_id   :=  l_master_organization_id;
2771              l_new_dest_instance_rec.mfg_serial_number_flag       :=  'N';
2772              l_new_dest_instance_rec.lot_number                   :=  l_mtl_item_tbl(j).lot_number;
2773              l_new_dest_instance_rec.quantity                     :=  abs(l_mtl_item_tbl(j).transaction_quantity);
2774              l_new_dest_instance_rec.unit_of_measure              :=  l_mtl_item_tbl(j).transaction_uom;
2775              l_new_dest_instance_rec.location_id                  :=  nvl(l_mtl_item_tbl(j).subinv_location_id,l_mtl_item_tbl(j).hr_location_id);
2776              l_new_dest_instance_rec.location_type_code           :=  csi_inv_trxs_pkg.get_location_type_code('Inventory');
2777              l_new_dest_instance_rec.instance_usage_code          :=  l_instance_usage_code;
2778              l_new_dest_instance_rec.inv_organization_id          :=  l_organization_id;
2779              l_new_dest_instance_rec.vld_organization_id          :=  l_organization_id;
2780              l_new_dest_instance_rec.inv_subinventory_name        :=  l_subinventory_name;
2781              l_new_dest_instance_rec.inv_locator_id               :=  l_locator_id;
2782              l_new_dest_instance_rec.customer_view_flag           :=  'N';
2783              l_new_dest_instance_rec.merchant_view_flag           :=  'Y';
2784              l_new_dest_instance_rec.operational_status_code      :=  'NOT_USED';
2785              l_new_dest_instance_rec.object_version_number        :=  l_object_version_number;
2786              l_new_dest_instance_rec.active_start_date            :=  l_sysdate;
2787              l_new_dest_instance_rec.active_end_date              :=  NULL;
2788              l_new_dest_instance_rec.last_oe_order_line_id        :=  l_src_instance_header_tbl(i).in_transit_order_line_id;
2789 
2790              l_ext_attrib_values_tbl                              :=  csi_inv_trxs_pkg.init_ext_attrib_values_tbl;
2791              l_party_tbl                                          :=  csi_inv_trxs_pkg.init_party_tbl;
2792              l_account_tbl                                        :=  csi_inv_trxs_pkg.init_account_tbl;
2796 
2793              l_pricing_attrib_tbl                                 :=  csi_inv_trxs_pkg.init_pricing_attribs_tbl;
2794              l_org_assignments_tbl                                :=  csi_inv_trxs_pkg.init_org_assignments_tbl;
2795              l_asset_assignment_tbl                               :=  csi_inv_trxs_pkg.init_asset_assignment_tbl;
2797              l_new_dest_instance_rec.instance_status_id := nvl(csi_inv_trxs_pkg.get_default_status_id(l_txn_rec.transaction_type_id),r_id.instance_status_id);
2798 
2799              IF (l_debug > 0) THEN
2800                csi_t_gen_utility_pvt.add('Instance Status Id: '||l_new_dest_instance_rec.instance_status_id);
2801              END IF;
2802 
2803              IF (l_debug > 0) THEN
2804                 csi_t_gen_utility_pvt.add('Before Create Item Instance-9');
2805                 csi_t_gen_utility_pvt.add('In Transit Order Line ID on Dest Rec: '||l_new_dest_instance_rec.last_oe_order_line_id);
2806              END IF;
2807 
2808              csi_item_instance_pub.create_item_instance(l_api_version,
2809                                                         l_commit,
2810                                                         l_init_msg_list,
2811                                                         l_validation_level,
2812                                                         l_new_dest_instance_rec,
2813                                                         l_ext_attrib_values_tbl,
2814                                                         l_party_tbl,
2815                                                         l_account_tbl,
2816                                                         l_pricing_attrib_tbl,
2817                                                         l_org_assignments_tbl,
2818                                                         l_asset_assignment_tbl,
2819                                                         l_txn_rec,
2820                                                         l_return_status,
2821                                                         l_msg_count,
2822                                                         l_msg_data);
2823 
2824              IF (l_debug > 0) THEN
2825                 csi_t_gen_utility_pvt.add('After Create Item Instance-10');
2826              END IF;
2827              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
2828              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
2829                IF (l_debug > 0) THEN
2830                   csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.create_item_instance API '||l_msg_data);
2831                END IF;
2832                l_msg_index := 1;
2833                  WHILE l_msg_count > 0 loop
2834 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
2835 	           l_msg_index := l_msg_index + 1;
2836                    l_msg_count := l_msg_count - 1;
2837   	         END LOOP;
2838 	         RAISE fnd_api.g_exc_error;
2839              END IF;
2840 
2841            ELSIF l_dest_instance_header_tbl.count = 1 THEN -- Installed Base Destination Records Found
2842 
2843                l_update_dest_instance_rec                         :=  csi_inv_trxs_pkg.init_instance_update_rec;
2844                l_update_dest_instance_rec.instance_id             :=  l_dest_instance_header_tbl(i).instance_id;
2845                l_update_dest_instance_rec.quantity                :=  l_dest_instance_header_tbl(i).quantity + abs(l_mtl_item_tbl(j).primary_quantity);
2846                l_update_dest_instance_rec.active_end_date         :=  NULL;
2847                l_update_dest_instance_rec.object_version_number   :=  l_dest_instance_header_tbl(i).object_version_number;
2848                l_update_dest_instance_rec.last_oe_order_line_id   :=  l_src_instance_header_tbl(i).in_transit_order_line_id;
2849 
2850                l_party_tbl.delete;
2851                l_account_tbl.delete;
2852                l_pricing_attrib_tbl.delete;
2853                l_org_assignments_tbl.delete;
2854                l_asset_assignment_tbl.delete;
2855 
2856                l_update_dest_instance_rec.instance_status_id := nvl(csi_inv_trxs_pkg.get_default_status_id(l_txn_rec.transaction_type_id),r_id.instance_status_id);
2857 
2858              IF (l_debug > 0) THEN
2859                csi_t_gen_utility_pvt.add('Instance Status Id: '||l_update_dest_instance_rec.instance_status_id);
2860              END IF;
2861 
2862                IF (l_debug > 0) THEN
2863                   csi_t_gen_utility_pvt.add('Before Update Item Instance-12');
2864                   csi_t_gen_utility_pvt.add('In Transit Order Line ID in Updated Instance: '||l_update_dest_instance_rec.last_oe_order_line_id);
2865                   csi_t_gen_utility_pvt.add('Transaction Type ID: '||l_txn_rec.transaction_type_id);
2866                END IF;
2867 
2868                csi_item_instance_pub.update_item_instance(l_api_version,
2869                                                           l_commit,
2870                                                           l_init_msg_list,
2871                                                           l_validation_level,
2872                                                           l_update_dest_instance_rec,
2873                                                           l_ext_attrib_values_tbl,
2874                                                           l_party_tbl,
2875                                                           l_account_tbl,
2876                                                           l_pricing_attrib_tbl,
2877                                                           l_org_assignments_tbl,
2878                                                           l_asset_assignment_tbl,
2879                                                           l_txn_rec,
2880                                                           l_instance_id_lst,
2881                                                           l_return_status,
2882                                                           l_msg_count,
2883                                                           l_msg_data);
2884 
2888              IF (l_debug > 0) THEN
2885            l_upd_error_instance_id := NULL;
2886            l_upd_error_instance_id := l_update_dest_instance_rec.instance_id;
2887 
2889                 csi_t_gen_utility_pvt.add('After Update Item Instance-13');
2890                 csi_t_gen_utility_pvt.add('l_upd_error_instance_id is: '||l_upd_error_instance_id);
2891              END IF;
2892 
2893              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
2894              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
2895                IF (l_debug > 0) THEN
2896                   csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.c API '||l_msg_data);
2897                END IF;
2898                l_msg_index := 1;
2899                  WHILE l_msg_count > 0 loop
2900 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
2901 	           l_msg_index := l_msg_index + 1;
2902                    l_msg_count := l_msg_count - 1;
2903                  END LOOP;
2904 	         RAISE fnd_api.g_exc_error;
2905              END IF;
2906 
2907            ELSIF l_dest_instance_header_tbl.count > 1 THEN
2908              -- Multiple Instances were found so throw error
2909              IF (l_debug > 0) THEN
2910                csi_t_gen_utility_pvt.add('Multiple Instances were Found in Install Base-14');
2911              END IF;
2912              fnd_message.set_name('CSI','CSI_TXN_MULT_INST_FOUND');
2913              fnd_message.set_token('INV_ITEM_ID',l_mtl_item_tbl(j).inventory_item_id);
2914              fnd_message.set_token('SUBINV',l_mtl_item_tbl(j).subinventory_code);
2915              fnd_message.set_token('INV_ORG_ID',l_mtl_item_tbl(j).organization_id);
2916              fnd_message.set_token('LOCATOR',l_mtl_item_tbl(j).locator_id);
2917              l_error_message := fnd_message.get;
2918              RAISE fnd_api.g_exc_error;
2919 
2920            END IF;    -- End of Destination Record If
2921          --END IF;      -- End of J Index Loop
2922 
2923 	 -- Added case 1,3,5 for serial in transit being RETURNED - JPW
2924 
2925       ELSIF (l_mtl_item_tbl(j).serial_number_control_code = 6 AND
2926              l_sr_control in (2,5)) OR
2927             (l_mtl_item_tbl(j).serial_number_control_code = 6 AND
2928              l_sr_control = 6) THEN
2929 
2930             IF (l_debug > 0) THEN
2931                   csi_t_gen_utility_pvt.add('Serial Control at Shipping is 2,5 or 6 and Receiving is 6');
2932                    csi_t_gen_utility_pvt.add('Instance being updated: '||l_src_instance_header_tbl(i).instance_id);
2933             END IF;
2934 
2935              l_update_instance_rec                              :=  csi_inv_trxs_pkg.init_instance_update_rec;
2936              l_update_instance_rec.instance_id                  :=  l_src_instance_header_tbl(i).instance_id;
2937              l_update_instance_rec.active_end_date              :=  l_sysdate;
2938 
2939              l_update_instance_rec.object_version_number        :=  l_src_instance_header_tbl(i).object_version_number;
2940 
2941            IF (l_debug > 0) THEN
2942               csi_t_gen_utility_pvt.add('After you initialize the Update Record Values');
2943               csi_t_gen_utility_pvt.add('Instance Updated: '||l_update_instance_rec.instance_id);
2944               csi_t_gen_utility_pvt.add('End Date Passed in: '||to_char(l_update_instance_rec.active_end_date,'DD-MON-YYYY HH24:MI:SS'));
2945               csi_t_gen_utility_pvt.add('Object Version: '||l_update_instance_rec.object_version_number);
2946            END IF;
2947 
2948 ---- BEGIN New Added
2949 	     --Added for Bug 5975739
2950 	     l_update_instance_rec.inv_master_organization_id   :=  l_master_organization_id;
2951              --bug#13815023
2952 	    /* l_update_instance_rec.inv_organization_id          :=  l_mtl_item_tbl(j).organization_id;
2953              l_update_instance_rec.vld_organization_id          :=  l_mtl_item_tbl(j).organization_id;
2954              l_update_instance_rec.inv_subinventory_name        :=  l_mtl_item_tbl(j).subinventory_code;
2955              l_update_instance_rec.location_id                  :=  nvl(l_mtl_item_tbl(j).subinv_location_id,l_mtl_item_tbl(j).hr_location_id);
2956              l_update_instance_rec.location_type_code           :=  csi_inv_trxs_pkg.get_location_type_code('Inventory');
2957              l_update_instance_rec.instance_usage_code          :=  l_returned; */
2958 	     --bug#13815023
2959 
2960              IF (l_debug > 0) THEN
2961                 csi_t_gen_utility_pvt.add('Setting In Transit Serialized Instance to be RETURNED usage');
2962                 csi_t_gen_utility_pvt.add('Usage: '||l_update_instance_rec.instance_usage_code);
2963                 csi_t_gen_utility_pvt.add('VLD Org: '||l_update_instance_rec.vld_organization_id);
2964                 csi_t_gen_utility_pvt.add('INV Org: '||l_update_instance_rec.inv_organization_id);
2965                 csi_t_gen_utility_pvt.add('Subinv Code: '||l_update_instance_rec.inv_subinventory_name);
2966              END IF;
2967 
2968 ---- END NEW ADDED
2969 
2970            l_party_tbl.delete;
2971            l_account_tbl.delete;
2972            l_pricing_attrib_tbl.delete;
2973            l_org_assignments_tbl.delete;
2974            l_asset_assignment_tbl.delete;
2975 
2976            IF (l_debug > 0) THEN
2977                  csi_t_gen_utility_pvt.add('Before Update Item Instance-18');
2978            END IF;
2979 
2980            csi_item_instance_pub.update_item_instance(l_api_version,
2981                                                       l_commit,
2982                                                       l_init_msg_list,
2983                                                       l_validation_level,
2984                                                       l_update_instance_rec,
2985                                                       l_ext_attrib_values_tbl,
2986                                                       l_party_tbl,
2990                                                       l_asset_assignment_tbl,
2987                                                       l_account_tbl,
2988                                                       l_pricing_attrib_tbl,
2989                                                       l_org_assignments_tbl,
2991                                                       l_txn_rec,
2992                                                       l_instance_id_lst,
2993                                                       l_return_status,
2994                                                       l_msg_count,
2995                                                       l_msg_data);
2996 
2997            l_upd_error_instance_id := NULL;
2998            l_upd_error_instance_id := l_update_instance_rec.instance_id;
2999 
3000            IF (l_debug > 0) THEN
3001                  csi_t_gen_utility_pvt.add('After Update Item Instance-19');
3002                  csi_t_gen_utility_pvt.add('You are updating Instance: '||l_update_instance_rec.instance_id);
3003                  csi_t_gen_utility_pvt.add('l_upd_error_instance_id is: '||l_upd_error_instance_id);
3004            END IF;
3005 
3006            -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
3007            IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
3008              IF (l_debug > 0) THEN
3009                    csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.update_item_instance API '||l_msg_data);
3010              END IF;
3011              l_msg_index := 1;
3012                WHILE l_msg_count > 0 loop
3013 	         l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
3014 	         l_msg_index := l_msg_index + 1;
3015                  l_msg_count := l_msg_count - 1;
3016   	       END LOOP;
3017 	       RAISE fnd_api.g_exc_error;
3018           END IF;
3019 
3020           IF j = 1 THEN -- Update Source Since its Non Serialized 1 Time
3021 
3022             IF (l_debug > 0) THEN
3023                   csi_t_gen_utility_pvt.add('Update/Create Non Serial Dest 1 time with Transaction Quantity');
3024             END IF;
3025 
3026          -- Now Query for Dest Non Serialized Instances and Update (Unexpire)/ Create Instances
3027              l_instance_query_rec                               :=  csi_inv_trxs_pkg.init_instance_query_rec;
3028              l_instance_query_rec.inventory_item_id             :=  l_mtl_item_tbl(j).inventory_item_id;
3029              l_instance_query_rec.inventory_revision            :=  l_mtl_item_tbl(j).revision;
3030              l_instance_query_rec.lot_number                    :=  l_mtl_item_tbl(j).lot_number;
3031              l_instance_query_rec.serial_number                 :=  NULL;
3032              l_instance_query_rec.instance_usage_code           :=  l_in_inventory;
3033              l_instance_query_rec.inv_subinventory_name         :=  l_mtl_item_tbl(j).subinventory_code;
3034              l_instance_query_rec.inv_organization_id           :=  l_mtl_item_tbl(j).organization_id;
3035              l_instance_query_rec.inv_locator_id                :=  l_mtl_item_tbl(j).locator_id;
3036              l_instance_usage_code                              :=  l_instance_query_rec.instance_usage_code;
3037              l_subinventory_name                                :=  l_mtl_item_tbl(j).subinventory_code;
3038              l_organization_id                                  :=  l_mtl_item_tbl(j).organization_id;
3039              l_locator_id                                       :=  l_mtl_item_tbl(j).locator_id;
3040 
3041            IF (l_debug > 0) THEN
3042                  csi_t_gen_utility_pvt.add('Before Get Dest Item Instance-20');
3043            END IF;
3044 
3045            csi_item_instance_pub.get_item_instances(l_api_version,
3046                                                     l_commit,
3047                                                     l_init_msg_list,
3048                                                     l_validation_level,
3049                                                     l_instance_query_rec,
3050                                                     l_party_query_rec,
3051                                                     l_account_query_rec,
3052                                                     l_transaction_id,
3053                                                     l_resolve_id_columns,
3054                                                     l_inactive_instance_only,
3055                                                     l_dest_instance_header_tbl,
3056                                                     l_return_status,
3057                                                     l_msg_count,
3058                                                     l_msg_data);
3059 
3060            IF (l_debug > 0) THEN
3061                  csi_t_gen_utility_pvt.add('After Get Item Instance-21');
3062            END IF;
3063            l_tbl_count := 0;
3064            l_tbl_count :=  l_dest_instance_header_tbl.count;
3065            IF (l_debug > 0) THEN
3066                  csi_t_gen_utility_pvt.add('Source Records Found: '||l_tbl_count);
3067            END IF;
3068 
3069            -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
3070            IF NOT l_return_status = l_fnd_success then
3071              IF (l_debug > 0) THEN
3072                    csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.get_item_instance API '||l_msg_data);
3073              END IF;
3074              l_msg_index := 1;
3075 	         WHILE l_msg_count > 0 loop
3076 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
3077 	           l_msg_index := l_msg_index + 1;
3078                l_msg_count := l_msg_count - 1;
3079   	         END LOOP;
3080 	         RAISE fnd_api.g_exc_error;
3081            END IF;
3082 
3086              l_new_dest_instance_rec.inventory_item_id            :=  l_mtl_item_tbl(j).inventory_item_id;
3083            IF l_dest_instance_header_tbl.count = 0 THEN  -- Installed Base Destination Records are not found so create a new record
3084 
3085              l_new_dest_instance_rec                              :=  csi_inv_trxs_pkg.init_instance_create_rec;
3087              l_new_dest_instance_rec.inventory_revision           :=  l_mtl_item_tbl(j).revision;
3088              l_new_dest_instance_rec.inv_master_organization_id   :=  l_master_organization_id;
3089              l_new_dest_instance_rec.mfg_serial_number_flag       :=  'N';
3090              l_new_dest_instance_rec.serial_number                :=  NULL;
3091              l_new_dest_instance_rec.lot_number                   :=  l_mtl_item_tbl(j).lot_number;
3092              l_new_dest_instance_rec.quantity                     :=  abs(l_mtl_item_tbl(j).transaction_quantity);
3093              l_new_dest_instance_rec.unit_of_measure              :=  l_mtl_item_tbl(j).transaction_uom;
3094              l_new_dest_instance_rec.location_id                  :=  nvl(l_mtl_item_tbl(j).subinv_location_id,l_mtl_item_tbl(j).hr_location_id);
3095              l_new_dest_instance_rec.location_type_code           :=  csi_inv_trxs_pkg.get_location_type_code('Inventory');
3096              l_new_dest_instance_rec.instance_usage_code          :=  l_instance_usage_code;
3097              l_new_dest_instance_rec.inv_organization_id          :=  l_organization_id;
3098              l_new_dest_instance_rec.vld_organization_id          :=  l_organization_id;
3099              l_new_dest_instance_rec.inv_subinventory_name        :=  l_subinventory_name;
3100              l_new_dest_instance_rec.inv_locator_id               :=  l_locator_id;
3101              l_new_dest_instance_rec.customer_view_flag           :=  'N';
3102              l_new_dest_instance_rec.merchant_view_flag           :=  'Y';
3103              l_new_dest_instance_rec.operational_status_code      :=  'NOT_USED';
3104              l_new_dest_instance_rec.object_version_number        :=  l_object_version_number;
3105              l_new_dest_instance_rec.active_start_date            :=  l_sysdate;
3106              l_new_dest_instance_rec.active_end_date              :=  NULL;
3107              l_new_dest_instance_rec.last_oe_order_line_id        :=  l_src_instance_header_tbl(i).in_transit_order_line_id;
3108 
3109              l_ext_attrib_values_tbl                              :=  csi_inv_trxs_pkg.init_ext_attrib_values_tbl;
3110              l_party_tbl                                          :=  csi_inv_trxs_pkg.init_party_tbl;
3111              l_account_tbl                                        :=  csi_inv_trxs_pkg.init_account_tbl;
3112              l_pricing_attrib_tbl                                 :=  csi_inv_trxs_pkg.init_pricing_attribs_tbl;
3113              l_org_assignments_tbl                                :=  csi_inv_trxs_pkg.init_org_assignments_tbl;
3114              l_asset_assignment_tbl                               :=  csi_inv_trxs_pkg.init_asset_assignment_tbl;
3115 
3116              l_new_dest_instance_rec.instance_status_id := nvl(csi_inv_trxs_pkg.get_default_status_id(l_txn_rec.transaction_type_id),r_id.instance_status_id);
3117 
3118              IF (l_debug > 0) THEN
3119                csi_t_gen_utility_pvt.add('Instance Status Id: '||l_new_dest_instance_rec.instance_status_id);
3120              END IF;
3121 
3122              IF (l_debug > 0) THEN
3123                csi_t_gen_utility_pvt.add('Before Create Item Instance-22');
3124                csi_t_gen_utility_pvt.add('In Transit Order Line ID on Dest Rec: '||l_new_dest_instance_rec.last_oe_order_line_id);
3125              END IF;
3126 
3127              csi_item_instance_pub.create_item_instance(l_api_version,
3128                                                         l_commit,
3129                                                         l_init_msg_list,
3130                                                         l_validation_level,
3131                                                         l_new_dest_instance_rec,
3132                                                         l_ext_attrib_values_tbl,
3133                                                         l_party_tbl,
3134                                                         l_account_tbl,
3135                                                         l_pricing_attrib_tbl,
3136                                                         l_org_assignments_tbl,
3137                                                         l_asset_assignment_tbl,
3138                                                         l_txn_rec,
3139                                                         l_return_status,
3140                                                         l_msg_count,
3141                                                         l_msg_data);
3142 
3143              IF (l_debug > 0) THEN
3144                csi_t_gen_utility_pvt.add('After Create Item Instance-23');
3145              END IF;
3146 
3147              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
3148 
3149              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
3150                IF (l_debug > 0) THEN
3151                      csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.create_item_instance API '||l_msg_data);
3152                END IF;
3153                l_msg_index := 1;
3154                  WHILE l_msg_count > 0 loop
3155 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
3156 	           l_msg_index := l_msg_index + 1;
3157                    l_msg_count := l_msg_count - 1;
3158   	         END LOOP;
3159 	         RAISE fnd_api.g_exc_error;
3160              END IF;
3161 
3162            ELSIF l_dest_instance_header_tbl.count = 1 THEN -- Installed Base Destination Records Found
3163 
3164                l_update_dest_instance_rec                         :=  csi_inv_trxs_pkg.init_instance_update_rec;
3168                l_update_dest_instance_rec.object_version_number   :=  l_dest_instance_header_tbl(i).object_version_number;
3165                l_update_dest_instance_rec.instance_id             :=  l_dest_instance_header_tbl(i).instance_id;
3166                l_update_dest_instance_rec.quantity                :=  l_dest_instance_header_tbl(i).quantity + abs(l_mtl_item_tbl(j).primary_quantity);
3167                l_update_dest_instance_rec.active_end_date         :=  NULL;
3169                l_update_dest_instance_rec.last_oe_order_line_id   :=  l_src_instance_header_tbl(i).in_transit_order_line_id;
3170 
3171                l_party_tbl.delete;
3172                l_account_tbl.delete;
3173                l_pricing_attrib_tbl.delete;
3174                l_org_assignments_tbl.delete;
3175                l_asset_assignment_tbl.delete;
3176 
3177                l_update_dest_instance_rec.instance_status_id := nvl(csi_inv_trxs_pkg.get_default_status_id(l_txn_rec.transaction_type_id),r_id.instance_status_id);
3178 
3179                IF (l_debug > 0) THEN
3180                  csi_t_gen_utility_pvt.add('Instance Status Id: '||l_update_dest_instance_rec.instance_status_id);
3181                END IF;
3182 
3183                IF (l_debug > 0) THEN
3184                   csi_t_gen_utility_pvt.add('Before Update Item Instance-25');
3185                   csi_t_gen_utility_pvt.add('In Transit Order Line ID in Updated Instance: '||l_update_dest_instance_rec.last_oe_order_line_id);
3186                END IF;
3187 
3188                csi_item_instance_pub.update_item_instance(l_api_version,
3189                                                           l_commit,
3190                                                           l_init_msg_list,
3191                                                           l_validation_level,
3192                                                           l_update_dest_instance_rec,
3193                                                           l_ext_attrib_values_tbl,
3194                                                           l_party_tbl,
3195                                                           l_account_tbl,
3196                                                           l_pricing_attrib_tbl,
3197                                                           l_org_assignments_tbl,
3198                                                           l_asset_assignment_tbl,
3199                                                           l_txn_rec,
3200                                                           l_instance_id_lst,
3201                                                           l_return_status,
3202                                                           l_msg_count,
3203                                                           l_msg_data);
3204 
3205              l_upd_error_instance_id := NULL;
3206              l_upd_error_instance_id := l_update_dest_instance_rec.instance_id;
3207 
3208              IF (l_debug > 0) THEN
3209                  csi_t_gen_utility_pvt.add('After Update Item Instance-26');
3210                  csi_t_gen_utility_pvt.add('l_upd_error_instance_id is: '||l_upd_error_instance_id);
3211              END IF;
3212 
3213              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
3214              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
3215                IF (l_debug > 0) THEN
3216                      csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.c API '||l_msg_data);
3217                END IF;
3218                l_msg_index := 1;
3219                  WHILE l_msg_count > 0 loop
3220 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
3221 	           l_msg_index := l_msg_index + 1;
3222                    l_msg_count := l_msg_count - 1;
3223                  END LOOP;
3224 	         RAISE fnd_api.g_exc_error;
3225              END IF;
3226 
3227            ELSIF l_dest_instance_header_tbl.count > 1 THEN
3228              -- Multiple Instances were found so throw error
3229              IF (l_debug > 0) THEN
3230                csi_t_gen_utility_pvt.add('Multiple Instances were Found in Install Base-28');
3231              END IF;
3232              fnd_message.set_name('CSI','CSI_TXN_MULT_INST_FOUND');
3233              fnd_message.set_token('INV_ITEM_ID',l_mtl_item_tbl(j).inventory_item_id);
3234              fnd_message.set_token('SUBINV',l_mtl_item_tbl(j).subinventory_code);
3235              fnd_message.set_token('INV_ORG_ID',l_mtl_item_tbl(j).organization_id);
3236              fnd_message.set_token('LOCATOR',l_mtl_item_tbl(j).locator_id);
3237              l_error_message := fnd_message.get;
3238              RAISE fnd_api.g_exc_error;
3239 
3240            END IF;    -- End of Destination Record If
3241          END IF;    -- End J Loop IF
3242 
3243       ELSIF (l_mtl_item_tbl(j).serial_number_control_code in (6,1) AND
3244              l_sr_control = 1) THEN
3245 
3246             IF (l_debug > 0) THEN
3247                   csi_t_gen_utility_pvt.add('Serial Control at Shipping is 1 and Receiving is 6 or 1');
3248                    csi_t_gen_utility_pvt.add('Source Instance being updated: '||l_src_instance_header_tbl(i).instance_id);
3249             END IF;
3250 
3251            l_upd_src_dest_instance_rec                        :=  csi_inv_trxs_pkg.init_instance_update_rec;
3252            l_upd_src_dest_instance_rec.instance_id            :=  l_src_instance_header_tbl(i).instance_id;
3253            l_upd_src_dest_instance_rec.quantity               :=  l_src_instance_header_tbl(i).quantity - abs(l_mtl_item_tbl(j).primary_quantity);
3254            l_upd_src_dest_instance_rec.object_version_number  :=  l_src_instance_header_tbl(i).object_version_number;
3255 
3256            l_party_tbl.delete;
3257            l_account_tbl.delete;
3258            l_pricing_attrib_tbl.delete;
3259            l_org_assignments_tbl.delete;
3260            l_asset_assignment_tbl.delete;
3261 
3265 
3262            IF (l_debug > 0) THEN
3263                  csi_t_gen_utility_pvt.add('Before Update Item Instance-31');
3264            END IF;
3266            csi_item_instance_pub.update_item_instance(l_api_version,
3267                                                       l_commit,
3268                                                       l_init_msg_list,
3269                                                       l_validation_level,
3270                                                       l_upd_src_dest_instance_rec,
3271                                                       l_ext_attrib_values_tbl,
3272                                                       l_party_tbl,
3273                                                       l_account_tbl,
3274                                                       l_pricing_attrib_tbl,
3275                                                       l_org_assignments_tbl,
3276                                                       l_asset_assignment_tbl,
3277                                                       l_txn_rec,
3278                                                       l_instance_id_lst,
3279                                                       l_return_status,
3280                                                       l_msg_count,
3281                                                       l_msg_data);
3282 
3283            l_upd_error_instance_id := NULL;
3284            l_upd_error_instance_id := l_upd_src_dest_instance_rec.instance_id;
3285 
3286            IF (l_debug > 0) THEN
3287                  csi_t_gen_utility_pvt.add('After Update Item Instance-32');
3288                  csi_t_gen_utility_pvt.add('l_upd_error_instance_id is: '||l_upd_error_instance_id);
3289            END IF;
3290 
3291            -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
3292            IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
3293              IF (l_debug > 0) THEN
3294                    csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.update_item_instance API '||l_msg_data);
3295              END IF;
3296              l_msg_index := 1;
3297                WHILE l_msg_count > 0 loop
3298 	         l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
3299 	         l_msg_index := l_msg_index + 1;
3300                  l_msg_count := l_msg_count - 1;
3301   	       END LOOP;
3302 	       RAISE fnd_api.g_exc_error;
3303            END IF;
3304 
3305          -- Now Query for Dest Non Serialized Instances and Update (Unexpire)/ Create Instances
3306              l_instance_query_rec                               :=  csi_inv_trxs_pkg.init_instance_query_rec;
3307              l_instance_query_rec.inventory_item_id             :=  l_mtl_item_tbl(j).inventory_item_id;
3308              l_instance_query_rec.inventory_revision            :=  l_mtl_item_tbl(j).revision;
3309              l_instance_query_rec.lot_number                    :=  l_mtl_item_tbl(j).lot_number;
3310              l_instance_query_rec.serial_number                 :=  NULL;
3311              l_instance_query_rec.instance_usage_code           :=  l_in_inventory;
3312              l_instance_query_rec.inv_subinventory_name         :=  l_mtl_item_tbl(j).subinventory_code;
3313              l_instance_query_rec.inv_organization_id           :=  l_mtl_item_tbl(j).organization_id;
3314              l_instance_query_rec.inv_locator_id                :=  l_mtl_item_tbl(j).locator_id;
3315              l_instance_usage_code                              :=  l_instance_query_rec.instance_usage_code;
3316              l_subinventory_name                                :=  l_mtl_item_tbl(j).subinventory_code;
3317              l_organization_id                                  :=  l_mtl_item_tbl(j).organization_id;
3318              l_locator_id                                       :=  l_mtl_item_tbl(j).locator_id;
3319 
3320            IF (l_debug > 0) THEN
3321                  csi_t_gen_utility_pvt.add('Before Get Dest Item Instance-33');
3322            END IF;
3323 
3324            csi_item_instance_pub.get_item_instances(l_api_version,
3325                                                     l_commit,
3326                                                     l_init_msg_list,
3327                                                     l_validation_level,
3328                                                     l_instance_query_rec,
3329                                                     l_party_query_rec,
3330                                                     l_account_query_rec,
3331                                                     l_transaction_id,
3332                                                     l_resolve_id_columns,
3333                                                     l_inactive_instance_only,
3334                                                     l_dest_instance_header_tbl,
3335                                                     l_return_status,
3336                                                     l_msg_count,
3337                                                     l_msg_data);
3338 
3339            IF (l_debug > 0) THEN
3340                  csi_t_gen_utility_pvt.add('After Get Item Instance-34');
3341            END IF;
3342            l_tbl_count := 0;
3343            l_tbl_count :=  l_dest_instance_header_tbl.count;
3344            IF (l_debug > 0) THEN
3345                  csi_t_gen_utility_pvt.add('Source Records Found: '||l_tbl_count);
3346            END IF;
3347 
3348            -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
3349            IF NOT l_return_status = l_fnd_success then
3350              IF (l_debug > 0) THEN
3351                    csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.get_item_instance API '||l_msg_data);
3352              END IF;
3353              l_msg_index := 1;
3354 	         WHILE l_msg_count > 0 loop
3358   	         END LOOP;
3355 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
3356 	           l_msg_index := l_msg_index + 1;
3357                l_msg_count := l_msg_count - 1;
3359 	         RAISE fnd_api.g_exc_error;
3360            END IF;
3361 
3362            IF l_dest_instance_header_tbl.count = 0 THEN  -- Installed Base Destination Records are not found so create a new record
3363 
3364              l_new_dest_instance_rec                              :=  csi_inv_trxs_pkg.init_instance_create_rec;
3365              l_new_dest_instance_rec.inventory_item_id            :=  l_mtl_item_tbl(j).inventory_item_id;
3366              l_new_dest_instance_rec.inventory_revision           :=  l_mtl_item_tbl(j).revision;
3367              l_new_dest_instance_rec.inv_master_organization_id   :=  l_master_organization_id;
3368              l_new_dest_instance_rec.mfg_serial_number_flag       :=  'N';
3369              l_new_dest_instance_rec.lot_number                   :=  l_mtl_item_tbl(j).lot_number;
3370              l_new_dest_instance_rec.quantity                     :=  l_mtl_item_tbl(j).transaction_quantity;
3371              l_new_dest_instance_rec.unit_of_measure              :=  l_mtl_item_tbl(j).transaction_uom;
3372              l_new_dest_instance_rec.location_id                  :=  nvl(l_mtl_item_tbl(j).subinv_location_id,l_mtl_item_tbl(j).hr_location_id);
3373              l_new_dest_instance_rec.location_type_code           :=  csi_inv_trxs_pkg.get_location_type_code('Inventory');
3374              l_new_dest_instance_rec.instance_usage_code          :=  l_instance_usage_code;
3375              l_new_dest_instance_rec.inv_organization_id          :=  l_organization_id;
3376              l_new_dest_instance_rec.vld_organization_id          :=  l_organization_id;
3377              l_new_dest_instance_rec.inv_subinventory_name        :=  l_subinventory_name;
3378              l_new_dest_instance_rec.inv_locator_id               :=  l_locator_id;
3379              l_new_dest_instance_rec.customer_view_flag           :=  'N';
3380              l_new_dest_instance_rec.merchant_view_flag           :=  'Y';
3381              l_new_dest_instance_rec.operational_status_code      :=  'NOT_USED';
3382              l_new_dest_instance_rec.object_version_number        :=  l_object_version_number;
3383              l_new_dest_instance_rec.active_start_date            :=  l_sysdate;
3384              l_new_dest_instance_rec.active_end_date              :=  NULL;
3385              l_new_dest_instance_rec.last_oe_order_line_id        :=  l_src_instance_header_tbl(i).in_transit_order_line_id;
3386 
3387              l_ext_attrib_values_tbl                              :=  csi_inv_trxs_pkg.init_ext_attrib_values_tbl;
3388              l_party_tbl                                          :=  csi_inv_trxs_pkg.init_party_tbl;
3389              l_account_tbl                                        :=  csi_inv_trxs_pkg.init_account_tbl;
3390              l_pricing_attrib_tbl                                 :=  csi_inv_trxs_pkg.init_pricing_attribs_tbl;
3391              l_org_assignments_tbl                                :=  csi_inv_trxs_pkg.init_org_assignments_tbl;
3392              l_asset_assignment_tbl                               :=  csi_inv_trxs_pkg.init_asset_assignment_tbl;
3393 
3394              l_new_dest_instance_rec.instance_status_id := nvl(csi_inv_trxs_pkg.get_default_status_id(l_txn_rec.transaction_type_id),r_id.instance_status_id);
3395 
3396              IF (l_debug > 0) THEN
3397                csi_t_gen_utility_pvt.add('Instance Status Id: '||l_new_dest_instance_rec.instance_status_id);
3398              END IF;
3399 
3400              IF (l_debug > 0) THEN
3401                  csi_t_gen_utility_pvt.add('Before Create Item Instance-35');
3402                  csi_t_gen_utility_pvt.add('In Transit Order Line ID on Dest Rec: '||l_new_dest_instance_rec.last_oe_order_line_id);
3403              END IF;
3404 
3405              csi_item_instance_pub.create_item_instance(l_api_version,
3406                                                         l_commit,
3407                                                         l_init_msg_list,
3408                                                         l_validation_level,
3409                                                         l_new_dest_instance_rec,
3410                                                         l_ext_attrib_values_tbl,
3411                                                         l_party_tbl,
3412                                                         l_account_tbl,
3413                                                         l_pricing_attrib_tbl,
3414                                                         l_org_assignments_tbl,
3415                                                         l_asset_assignment_tbl,
3416                                                         l_txn_rec,
3417                                                         l_return_status,
3418                                                         l_msg_count,
3419                                                         l_msg_data);
3420 
3421              IF (l_debug > 0) THEN
3422                    csi_t_gen_utility_pvt.add('After Create Item Instance-36');
3423              END IF;
3424              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
3425 
3426              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
3427                IF (l_debug > 0) THEN
3428                      csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.create_item_instance API '||l_msg_data);
3429                END IF;
3430                l_msg_index := 1;
3431                  WHILE l_msg_count > 0 loop
3432 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
3433 	           l_msg_index := l_msg_index + 1;
3434                    l_msg_count := l_msg_count - 1;
3435   	         END LOOP;
3436 	         RAISE fnd_api.g_exc_error;
3437              END IF;
3438 
3442                l_update_dest_instance_rec.instance_id             :=  l_dest_instance_header_tbl(i).instance_id;
3439            ELSIF l_dest_instance_header_tbl.count = 1 THEN -- Installed Base Destination Records Found
3440 
3441                l_update_dest_instance_rec                         :=  csi_inv_trxs_pkg.init_instance_update_rec;
3443                l_update_dest_instance_rec.quantity                :=  l_dest_instance_header_tbl(i).quantity + abs(l_mtl_item_tbl(j).primary_quantity);
3444                l_update_dest_instance_rec.active_end_date         :=  NULL;
3445                l_update_dest_instance_rec.object_version_number   :=  l_dest_instance_header_tbl(i).object_version_number;
3446                l_update_dest_instance_rec.last_oe_order_line_id   :=  l_src_instance_header_tbl(i).in_transit_order_line_id;
3447 
3448                l_party_tbl.delete;
3449                l_account_tbl.delete;
3450                l_pricing_attrib_tbl.delete;
3451                l_org_assignments_tbl.delete;
3452                l_asset_assignment_tbl.delete;
3453 
3454                l_update_dest_instance_rec.instance_status_id := nvl(csi_inv_trxs_pkg.get_default_status_id(l_txn_rec.transaction_type_id),r_id.instance_status_id);
3455 
3456                IF (l_debug > 0) THEN
3457                  csi_t_gen_utility_pvt.add('Instance Status Id: '||l_update_dest_instance_rec.instance_status_id);
3458                END IF;
3459 
3460                IF (l_debug > 0) THEN
3461                   csi_t_gen_utility_pvt.add('Before Update Item Instance-37');
3462                   csi_t_gen_utility_pvt.add('In Transit Order Line ID in Updated Instance: '||l_update_dest_instance_rec.last_oe_order_line_id);
3463                END IF;
3464 
3465                csi_item_instance_pub.update_item_instance(l_api_version,
3466                                                           l_commit,
3467                                                           l_init_msg_list,
3468                                                           l_validation_level,
3469                                                           l_update_dest_instance_rec,
3470                                                           l_ext_attrib_values_tbl,
3471                                                           l_party_tbl,
3472                                                           l_account_tbl,
3473                                                           l_pricing_attrib_tbl,
3474                                                           l_org_assignments_tbl,
3475                                                           l_asset_assignment_tbl,
3476                                                           l_txn_rec,
3477                                                           l_instance_id_lst,
3478                                                           l_return_status,
3479                                                           l_msg_count,
3480                                                           l_msg_data);
3481 
3482              l_upd_error_instance_id := NULL;
3483              l_upd_error_instance_id := l_update_dest_instance_rec.instance_id;
3484 
3485              IF (l_debug > 0) THEN
3486                  csi_t_gen_utility_pvt.add('After Update Item Instance-38');
3487                  csi_t_gen_utility_pvt.add('l_upd_error_instance_id is: '||l_upd_error_instance_id);
3488              END IF;
3489 
3490              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
3491              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
3492                IF (l_debug > 0) THEN
3493                      csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.c API '||l_msg_data);
3494                END IF;
3495                l_msg_index := 1;
3496                  WHILE l_msg_count > 0 loop
3497 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
3498 	           l_msg_index := l_msg_index + 1;
3499                    l_msg_count := l_msg_count - 1;
3500                  END LOOP;
3501 	         RAISE fnd_api.g_exc_error;
3502              END IF;
3503 
3504            ELSIF l_dest_instance_header_tbl.count > 1 THEN
3505            -- Multiple Instances were found so throw error
3506 
3507              IF (l_debug > 0) THEN
3508                csi_t_gen_utility_pvt.add('Multiple Instances were Found in Install Base-43');
3509              END IF;
3510              fnd_message.set_name('CSI','CSI_TXN_MULT_INST_FOUND');
3511              fnd_message.set_token('INV_ITEM_ID',l_mtl_item_tbl(j).inventory_item_id);
3512              fnd_message.set_token('SUBINV',l_mtl_item_tbl(j).subinventory_code);
3513              fnd_message.set_token('INV_ORG_ID',l_mtl_item_tbl(j).organization_id);
3514              fnd_message.set_token('LOCATOR',l_mtl_item_tbl(j).locator_id);
3515              l_error_message := fnd_message.get;
3516              RAISE fnd_api.g_exc_error;
3517            END IF;    -- End of Destination Record If
3518 
3519 
3520       ELSIF (l_mtl_item_tbl(j).serial_number_control_code in (2,5) AND
3521              l_sr_control = 1) THEN
3522 
3523 
3524             l_12_loop := 1;
3525 
3526             IF (l_debug > 0) THEN
3527               csi_t_gen_utility_pvt.add('Setting l_12_loop: '||l_12_loop);
3528               csi_t_gen_utility_pvt.add('Serial Control at Shipping is 1 and Receiving is 2,5');
3529             END IF;
3530 
3531           IF j = 1 THEN -- Update Source Since its Non Serialized 1 Time
3532 
3533             IF (l_debug > 0) THEN
3534               csi_t_gen_utility_pvt.add('Source Instance being updated 1 time: '||l_src_instance_header_tbl(i).instance_id);
3535 
3536             END IF;
3537 
3538            l_upd_src_dest_instance_rec                        :=  csi_inv_trxs_pkg.init_instance_update_rec;
3539            l_upd_src_dest_instance_rec.instance_id            :=  l_src_instance_header_tbl(i).instance_id;
3543            l_party_tbl.delete;
3540            l_upd_src_dest_instance_rec.quantity               :=  l_src_instance_header_tbl(i).quantity - abs(l_mtl_item_tbl(j).primary_quantity);
3541            l_upd_src_dest_instance_rec.object_version_number  :=  l_src_instance_header_tbl(i).object_version_number;
3542 
3544            l_account_tbl.delete;
3545            l_pricing_attrib_tbl.delete;
3546            l_org_assignments_tbl.delete;
3547            l_asset_assignment_tbl.delete;
3548 
3549            IF (l_debug > 0) THEN
3550              csi_t_gen_utility_pvt.add('Before Update Item Instance-43');
3551            END IF;
3552 
3553            csi_item_instance_pub.update_item_instance(l_api_version,
3554                                                       l_commit,
3555                                                       l_init_msg_list,
3556                                                       l_validation_level,
3557                                                       l_upd_src_dest_instance_rec,
3558                                                       l_ext_attrib_values_tbl,
3559                                                       l_party_tbl,
3560                                                       l_account_tbl,
3561                                                       l_pricing_attrib_tbl,
3562                                                       l_org_assignments_tbl,
3563                                                       l_asset_assignment_tbl,
3564                                                       l_txn_rec,
3565                                                       l_instance_id_lst,
3566                                                       l_return_status,
3567                                                       l_msg_count,
3568                                                       l_msg_data);
3569 
3570            l_upd_error_instance_id := NULL;
3571            l_upd_error_instance_id := l_upd_src_dest_instance_rec.instance_id;
3572 
3573            IF (l_debug > 0) THEN
3574              csi_t_gen_utility_pvt.add('After Update Item Instance-44');
3575              csi_t_gen_utility_pvt.add('l_upd_error_instance_id is: '||l_upd_error_instance_id);
3576            END IF;
3577 
3578            -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
3579            IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
3580              IF (l_debug > 0) THEN
3581                    csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.update_item_instance API '||l_msg_data);
3582              END IF;
3583              l_msg_index := 1;
3584                WHILE l_msg_count > 0 loop
3585 	         l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
3586 	         l_msg_index := l_msg_index + 1;
3587                  l_msg_count := l_msg_count - 1;
3588   	       END LOOP;
3589 	       RAISE fnd_api.g_exc_error;
3590            END IF;
3591          END IF; -- End of J = 1 If to update Source 1 time
3592 
3593          -- Now Query for Dest Serialized Instances and Update (Unexpire)/ Create Instances
3594              l_instance_query_rec                               :=  csi_inv_trxs_pkg.init_instance_query_rec;
3595              l_instance_query_rec.inventory_item_id             :=  l_mtl_item_tbl(j).inventory_item_id;
3596              --l_instance_query_rec.inventory_revision            :=  l_mtl_item_tbl(j).revision;
3597              --l_instance_query_rec.lot_number                    :=  l_mtl_item_tbl(j).lot_number;
3598              l_instance_query_rec.serial_number                 :=  l_mtl_item_tbl(j).serial_number;
3599              --l_instance_query_rec.instance_usage_code           :=  l_in_inventory;
3600              --l_instance_query_rec.inv_subinventory_name         :=  l_mtl_item_tbl(j).subinventory_code;
3601              --l_instance_query_rec.inv_organization_id           :=  l_mtl_item_tbl(j).organization_id;
3602              --l_instance_query_rec.inv_locator_id                :=  l_mtl_item_tbl(j).locator_id;
3603              --l_instance_usage_code                              :=  l_instance_query_rec.instance_usage_code;
3604              l_instance_usage_code                              :=  l_in_inventory;
3605              l_subinventory_name                                :=  l_mtl_item_tbl(j).subinventory_code;
3606              l_organization_id                                  :=  l_mtl_item_tbl(j).organization_id;
3607              l_locator_id                                       :=  l_mtl_item_tbl(j).locator_id;
3608 
3609            IF (l_debug > 0) THEN
3610                  csi_t_gen_utility_pvt.add('Before Get Dest Item Instance-45');
3611            END IF;
3612 
3613            csi_item_instance_pub.get_item_instances(l_api_version,
3614                                                     l_commit,
3615                                                     l_init_msg_list,
3616                                                     l_validation_level,
3617                                                     l_instance_query_rec,
3618                                                     l_party_query_rec,
3619                                                     l_account_query_rec,
3620                                                     l_transaction_id,
3621                                                     l_resolve_id_columns,
3622                                                     l_inactive_instance_only,
3623                                                     l_dest_instance_header_tbl,
3624                                                     l_return_status,
3625                                                     l_msg_count,
3626                                                     l_msg_data);
3627 
3628            IF (l_debug > 0) THEN
3629                  csi_t_gen_utility_pvt.add('After Get Item Instance-46');
3630            END IF;
3631            l_tbl_count := 0;
3632            l_tbl_count :=  l_dest_instance_header_tbl.count;
3633            IF (l_debug > 0) THEN
3634                  csi_t_gen_utility_pvt.add('Source Records Found: '||l_tbl_count);
3635            END IF;
3636 
3637            -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
3638            IF NOT l_return_status = l_fnd_success then
3639              IF (l_debug > 0) THEN
3640                    csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.get_item_instance API '||l_msg_data);
3641              END IF;
3642              l_msg_index := 1;
3643 	         WHILE l_msg_count > 0 loop
3644 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
3645 	           l_msg_index := l_msg_index + 1;
3646                l_msg_count := l_msg_count - 1;
3647   	         END LOOP;
3648 	         RAISE fnd_api.g_exc_error;
3649            END IF;
3650 
3651            IF l_dest_instance_header_tbl.count < 1 THEN  -- Installed Base Destination Records are not found so create a new record
3652 
3653              l_new_dest_instance_rec                              :=  csi_inv_trxs_pkg.init_instance_create_rec;
3654              l_new_dest_instance_rec.inventory_item_id            :=  l_mtl_item_tbl(j).inventory_item_id;
3655              l_new_dest_instance_rec.inventory_revision           :=  l_mtl_item_tbl(j).revision;
3656              l_new_dest_instance_rec.inv_master_organization_id   :=  l_master_organization_id;
3657              l_new_dest_instance_rec.mfg_serial_number_flag       :=  'Y';
3658              l_new_dest_instance_rec.serial_number                :=  l_mtl_item_tbl(j).serial_number;
3659              l_new_dest_instance_rec.lot_number                   :=  l_mtl_item_tbl(j).lot_number;
3660              l_new_dest_instance_rec.quantity                     :=  1;
3661              l_new_dest_instance_rec.unit_of_measure              :=  l_mtl_item_tbl(j).transaction_uom;
3662              l_new_dest_instance_rec.location_id                  :=  nvl(l_mtl_item_tbl(j).subinv_location_id,l_mtl_item_tbl(j).hr_location_id);
3663              l_new_dest_instance_rec.location_type_code           :=  csi_inv_trxs_pkg.get_location_type_code('Inventory');
3664              l_new_dest_instance_rec.instance_usage_code          :=  l_instance_usage_code;
3665              l_new_dest_instance_rec.inv_organization_id          :=  l_organization_id;
3666              l_new_dest_instance_rec.vld_organization_id          :=  l_organization_id;
3667              l_new_dest_instance_rec.inv_subinventory_name        :=  l_subinventory_name;
3668              l_new_dest_instance_rec.inv_locator_id               :=  l_locator_id;
3669              l_new_dest_instance_rec.customer_view_flag           :=  'N';
3670              l_new_dest_instance_rec.merchant_view_flag           :=  'Y';
3671              l_new_dest_instance_rec.operational_status_code      :=  'NOT_USED';
3672              l_new_dest_instance_rec.object_version_number        :=  l_object_version_number;
3673              l_new_dest_instance_rec.active_start_date            :=  l_sysdate;
3674              l_new_dest_instance_rec.active_end_date              :=  NULL;
3675              l_new_dest_instance_rec.last_oe_order_line_id        :=  l_src_instance_header_tbl(i).in_transit_order_line_id;
3676 
3677              l_ext_attrib_values_tbl                              :=  csi_inv_trxs_pkg.init_ext_attrib_values_tbl;
3678              l_party_tbl                                          :=  csi_inv_trxs_pkg.init_party_tbl;
3679              l_account_tbl                                        :=  csi_inv_trxs_pkg.init_account_tbl;
3680              l_pricing_attrib_tbl                                 :=  csi_inv_trxs_pkg.init_pricing_attribs_tbl;
3681              l_org_assignments_tbl                                :=  csi_inv_trxs_pkg.init_org_assignments_tbl;
3682              l_asset_assignment_tbl                               :=  csi_inv_trxs_pkg.init_asset_assignment_tbl;
3683 
3684              l_new_dest_instance_rec.instance_status_id := nvl(csi_inv_trxs_pkg.get_default_status_id(l_txn_rec.transaction_type_id),r_id.instance_status_id);
3685 
3686              IF (l_debug > 0) THEN
3687                csi_t_gen_utility_pvt.add('Instance Status Id: '||l_new_dest_instance_rec.instance_status_id);
3688              END IF;
3689 
3690              IF (l_debug > 0) THEN
3691                  csi_t_gen_utility_pvt.add('Before Create Item Instance-47');
3692                  csi_t_gen_utility_pvt.add('In Transit Order Line ID on Dest Rec: '||l_new_dest_instance_rec.last_oe_order_line_id);
3693              END IF;
3694 
3695              csi_item_instance_pub.create_item_instance(l_api_version,
3696                                                         l_commit,
3697                                                         l_init_msg_list,
3698                                                         l_validation_level,
3699                                                         l_new_dest_instance_rec,
3700                                                         l_ext_attrib_values_tbl,
3701                                                         l_party_tbl,
3702                                                         l_account_tbl,
3703                                                         l_pricing_attrib_tbl,
3704                                                         l_org_assignments_tbl,
3705                                                         l_asset_assignment_tbl,
3706                                                         l_txn_rec,
3707                                                         l_return_status,
3708                                                         l_msg_count,
3709                                                         l_msg_data);
3710 
3711              IF (l_debug > 0) THEN
3712                    csi_t_gen_utility_pvt.add('After Create Item Instance-48');
3713              END IF;
3714              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
3715 
3716              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
3717                IF (l_debug > 0) THEN
3718                      csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.create_item_instance API '||l_msg_data);
3719                END IF;
3720                l_msg_index := 1;
3721                  WHILE l_msg_count > 0 loop
3722 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
3723 	           l_msg_index := l_msg_index + 1;
3724                    l_msg_count := l_msg_count - 1;
3725   	         END LOOP;
3726 	         RAISE fnd_api.g_exc_error;
3727              END IF;
3728 
3729            ELSIF l_dest_instance_header_tbl.count = 1 THEN -- Installed Base Destination Records Found
3730 
3731              IF (l_debug > 0) THEN
3732                    csi_t_gen_utility_pvt.add('Serialized Instance found-48.1');
3733              END IF;
3734 
3735              IF l_dest_instance_header_tbl(i).instance_usage_code IN (l_in_transit,l_in_inventory,l_in_relationship,l_out_of_enterprise) THEN
3736 
3737              IF (l_debug > 0) THEN
3738                    csi_t_gen_utility_pvt.add('Usage Code is: '||l_dest_instance_header_tbl(i).instance_usage_code);
3739              END IF;
3740 
3741              IF l_dest_instance_header_tbl(i).instance_usage_code = l_in_relationship THEN
3742                csi_t_gen_utility_pvt.add('Check and Break Relationship for Instance :'||l_dest_instance_header_tbl(i).instance_id);
3743 
3744                csi_process_txn_pvt.check_and_break_relation(l_dest_instance_header_tbl(i).instance_id,
3745                                                             l_txn_rec,
3746                                                             l_return_status);
3747 
3748               IF NOT l_return_status = l_fnd_success then
3749                 csi_t_gen_utility_pvt.add('You encountered an error in the se_inv_trxs_pkg.check_and_break_relation');
3750                 l_error_message := csi_t_gen_utility_pvt.dump_error_stack;
3751                 RAISE fnd_api.g_exc_error;
3752               END IF;
3753 
3754               csi_t_gen_utility_pvt.add('Object Version originally from instance: '||l_dest_instance_header_tbl(i).object_version_number);
3755 
3756               OPEN c_obj_version (l_dest_instance_header_tbl(i).instance_id);
3757               FETCH c_obj_version into l_dest_instance_header_tbl(i).object_version_number;
3758               CLOSE c_obj_version;
3759 
3760               csi_t_gen_utility_pvt.add('Current Object Version after check and break :'||l_dest_instance_header_tbl(i).object_version_number);
3761 
3762               END IF; -- Check and Break
3763 
3764 
3765                l_update_dest_instance_rec                             :=  csi_inv_trxs_pkg.init_instance_update_rec;
3766                l_update_dest_instance_rec.instance_id                 :=  l_dest_instance_header_tbl(i).instance_id;
3767                l_update_dest_instance_rec.inventory_revision           :=  l_mtl_item_tbl(j).revision;
3768                l_update_dest_instance_rec.lot_number                   :=  l_mtl_item_tbl(j).lot_number;
3769                l_update_dest_instance_rec.quantity                     :=  1;
3770                l_update_dest_instance_rec.location_id                  :=  nvl(l_mtl_item_tbl(j).subinv_location_id,l_mtl_item_tbl(j).hr_location_id);
3771                l_update_dest_instance_rec.location_type_code           :=  csi_inv_trxs_pkg.get_location_type_code('Inventory');
3772                l_update_dest_instance_rec.instance_usage_code          :=  l_instance_usage_code;
3773 	       -- Added for Bug 5975739
3774                l_update_dest_instance_rec.inv_master_organization_id   :=  l_master_organization_id;
3775                l_update_dest_instance_rec.inv_organization_id          :=  l_organization_id;
3776                l_update_dest_instance_rec.vld_organization_id          :=  l_organization_id;
3777                l_update_dest_instance_rec.inv_subinventory_name        :=  l_subinventory_name;
3778                l_update_dest_instance_rec.inv_locator_id               :=  l_locator_id;
3779                l_update_dest_instance_rec.quantity                :=  1;
3780                l_update_dest_instance_rec.active_end_date         :=  NULL;
3781                l_update_dest_instance_rec.object_version_number   :=  l_dest_instance_header_tbl(i).object_version_number;
3782                l_update_dest_instance_rec.last_oe_order_line_id   :=  l_src_instance_header_tbl(i).in_transit_order_line_id;
3783 
3784              IF l_dest_instance_header_tbl(i).instance_usage_code IN (l_in_relationship,l_out_of_enterprise) THEN
3785 
3786 	       -- We want to change the party of this back
3787      	       -- to the Internal Party
3788 
3789                 IF (l_debug > 0) THEN
3790                   csi_t_gen_utility_pvt.add('Usage is '||l_dest_instance_header_tbl(i).instance_usage_code||' So we need to bring this back into Inventory and change the Owner Party back to the Internal Party');
3791                 END IF;
3792 
3793   	        -- Set Instance ID so it will query the child recs for this
3794 	        -- Instance.
3795 
3796 	        l_instance_header_rec.instance_id := l_dest_instance_header_tbl(i).instance_id;
3797 	         -- Call details to get Party Information
3798 
3799                 csi_item_instance_pub.get_item_instance_details
3800                                                (l_api_version,
3801                                                l_commit,
3802                                                l_init_msg_list,
3803                                                l_validation_level,
3804                                                l_instance_header_rec,
3805                                                fnd_api.g_true,  -- Get Parties
3806                                                l_party_header_tbl,
3807                                                fnd_api.g_false,  -- Get Accounts
3808                                                l_account_header_tbl,
3809                                                fnd_api.g_false,  -- Get Org Assi.
3810                                                l_org_header_tbl,
3811                                                fnd_api.g_false,  -- Get Price Att
3812                                                l_pricing_header_tbl,
3813                                                fnd_api.g_false,  -- Get Ext Attr
3814                                                l_ext_attrib_header_tbl,
3815                                                l_ext_attrib_def_tbl,
3816                                                fnd_api.g_false, -- Get Asset Assi
3817                                                l_asset_header_tbl,
3818                                                fnd_api.g_false, -- Resolve IDs
3819                                                NULL,            -- Time Stamp
3820                                                l_return_status,
3821                                                l_msg_count,
3822                                                l_msg_data);
3823 
3824                 -- Now create a new owner record that will be used to create
3825                 -- the new owner party and set it back to an internal party owner
3826 	        -- The PL/SQL Table will now be set so that it can be passed into
3827 	        -- the next procedure.
3828 
3829                 FOR p in l_party_header_tbl.FIRST .. l_party_header_tbl.LAST LOOP
3830                   IF l_party_header_tbl(p).relationship_type_code = 'OWNER' THEN
3831                     IF (l_debug > 0) THEN
3832                       csi_t_gen_utility_pvt.add('Found the OWNER party so updating this back to the Internal Party ID');
3833                     END IF;
3834 
3835                   l_party_tbl                   :=  csi_inv_trxs_pkg.init_party_tbl;
3836                   l_party_tbl(i).instance_id    :=  l_dest_instance_header_tbl(i).instance_id;
3837                   l_party_tbl(i).instance_party_id :=  l_party_header_tbl(p).instance_party_id;
3838                   l_party_tbl(i).object_version_number := l_party_header_tbl(p).object_version_number;
3839                   IF (l_debug > 0) THEN
3840                     csi_t_gen_utility_pvt.add('After finding the OWNER party and updating this back to the Internal Party ID');
3841                   END IF;
3842  	         END IF;
3843                 END LOOP;
3844 
3845                 IF (l_debug > 0) THEN
3846                   csi_t_gen_utility_pvt.add('Inst Party ID :'||l_party_tbl(i).instance_party_id);
3847                   csi_t_gen_utility_pvt.add('Party Inst ID :'||l_party_tbl(i).instance_id);
3848                   csi_t_gen_utility_pvt.add('Party Source Table :'||l_party_tbl(i).party_source_table);
3849                   csi_t_gen_utility_pvt.add('Party ID :'||l_party_tbl(i).party_id);
3850                   csi_t_gen_utility_pvt.add('Rel Type Code :'||l_party_tbl(i).relationship_type_code);
3851                   csi_t_gen_utility_pvt.add('Contact Flag :'||l_party_tbl(i).contact_flag);
3852                   csi_t_gen_utility_pvt.add('Object Version Number:' ||l_party_tbl(i).object_version_number);
3853                 END IF;
3854 
3855                 ELSE
3856 
3857                   l_party_tbl.delete;
3858 
3859                 END IF;
3860 
3861                l_account_tbl.delete;
3862                l_pricing_attrib_tbl.delete;
3863                l_org_assignments_tbl.delete;
3864                l_asset_assignment_tbl.delete;
3865 
3866                l_update_dest_instance_rec.instance_status_id := nvl(csi_inv_trxs_pkg.get_default_status_id(l_txn_rec.transaction_type_id),r_id.instance_status_id);
3867 
3868                IF (l_debug > 0) THEN
3869                  csi_t_gen_utility_pvt.add('Instance Status Id: '||l_update_dest_instance_rec.instance_status_id);
3870                END IF;
3871 
3872                IF (l_debug > 0) THEN
3873                   csi_t_gen_utility_pvt.add('Before Update Item Instance-49');
3874                   csi_t_gen_utility_pvt.add('In Transit Order Line ID in Updated Instance: '||l_update_dest_instance_rec.last_oe_order_line_id);
3875                END IF;
3876 
3877                csi_item_instance_pub.update_item_instance(l_api_version,
3878                                                           l_commit,
3879                                                           l_init_msg_list,
3880                                                           l_validation_level,
3881                                                           l_update_dest_instance_rec,
3882                                                           l_ext_attrib_values_tbl,
3883                                                           l_party_tbl,
3884                                                           l_account_tbl,
3885                                                           l_pricing_attrib_tbl,
3886                                                           l_org_assignments_tbl,
3887                                                           l_asset_assignment_tbl,
3888                                                           l_txn_rec,
3889                                                           l_instance_id_lst,
3890                                                           l_return_status,
3891                                                           l_msg_count,
3892                                                           l_msg_data);
3893 
3894            l_upd_error_instance_id := NULL;
3895            l_upd_error_instance_id := l_update_dest_instance_rec.instance_id;
3896 
3897              IF (l_debug > 0) THEN
3898                csi_t_gen_utility_pvt.add('After Update Item Instance-50');
3899                csi_t_gen_utility_pvt.add('l_upd_error_instance_id is: '||l_upd_error_instance_id);
3900              END IF;
3901 
3902              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
3903              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
3904                IF (l_debug > 0) THEN
3905                      csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.c API '||l_msg_data);
3906                END IF;
3907                l_msg_index := 1;
3908                  WHILE l_msg_count > 0 loop
3909 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
3910 	           l_msg_index := l_msg_index + 1;
3911                    l_msg_count := l_msg_count - 1;
3912                  END LOOP;
3913 	         RAISE fnd_api.g_exc_error;
3914              END IF;
3915 
3916              ELSE -- No Records Found So throw Error
3917              IF (l_debug > 0) THEN
3918                csi_t_gen_utility_pvt.add('No Records were found in Install Base-55');
3919              END IF;
3920 
3921              fnd_message.set_name('CSI','CSI_IB_RECORD_NOTFOUND');
3922              fnd_message.set_token('ITEM',l_mtl_item_tbl(j).inventory_item_id);
3923              fnd_message.set_token('SUBINVENTORY',l_mtl_item_tbl(j).subinventory_code);
3924              fnd_message.set_token('ORG_ID',l_mtl_item_tbl(j).organization_id);
3925              l_error_message := fnd_message.get;
3926              RAISE fnd_api.g_exc_error;
3927            END IF; -- Usage Code If
3928 
3929          ELSIF l_dest_instance_header_tbl.count > 1 THEN
3930 
3931          -- Multiple Instances were found so throw error
3932            IF (l_debug > 0) THEN
3933              csi_t_gen_utility_pvt.add('Multiple Instances were Found in Install Base-54');
3934            END IF;
3935            fnd_message.set_name('CSI','CSI_TXN_MULT_INST_FOUND');
3936            fnd_message.set_token('INV_ITEM_ID',l_mtl_item_tbl(j).inventory_item_id);
3937            fnd_message.set_token('SUBINV',l_mtl_item_tbl(j).subinventory_code);
3938            fnd_message.set_token('INV_ORG_ID',l_mtl_item_tbl(j).organization_id);
3939            fnd_message.set_token('LOCATOR',l_mtl_item_tbl(j).locator_id);
3940            l_error_message := fnd_message.get;
3941            RAISE fnd_api.g_exc_error;
3942 
3943          END IF;    -- End of Destination Record If
3944 
3945       END IF; -- Serial Control IF
3946 
3947      ELSE -- No IB Records Found So throw Error
3948            IF (l_debug > 0) THEN
3949                  csi_t_gen_utility_pvt.add('No Records were found in Install Base to receive-55');
3950            END IF;
3951            fnd_message.set_name('CSI','CSI_IB_RECORD_NOTFOUND');
3952            fnd_message.set_token('ITEM',l_mtl_item_tbl(j).inventory_item_id);
3953            fnd_message.set_token('SUBINVENTORY',l_mtl_item_tbl(j).subinventory_code);
3954            fnd_message.set_token('ORG_ID',l_mtl_item_tbl(j).organization_id);
3955            l_error_message := fnd_message.get;
3956            RAISE fnd_api.g_exc_error;
3957      END IF; -- End of Main Source Header Tbl IF
3958      END LOOP;        -- End of For Loop
3959 
3960      IF (l_debug > 0) THEN
3961            csi_t_gen_utility_pvt.add('End time: '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SS'));
3962            csi_t_gen_utility_pvt.add('******End of csi_inv_iso_pkg.iso_receipt Transaction******');
3963      END IF;
3964 
3965     EXCEPTION
3966      WHEN fnd_api.g_exc_error THEN
3967        IF (l_debug > 0) THEN
3968              csi_t_gen_utility_pvt.add('You have encountered a "fnd_api.g_exc_error" exception in the Internal Order In Transit Receipt');
3969        END IF;
3970        x_return_status := l_fnd_error;
3971 
3972        IF l_mtl_item_tbl.count > 0 THEN
3973          x_trx_error_rec.serial_number := l_mtl_item_tbl(j).serial_number;
3974          x_trx_error_rec.lot_number := l_mtl_item_tbl(j).lot_number;
3975          x_trx_error_rec.instance_id := l_upd_error_instance_id;
3976          x_trx_error_rec.inventory_item_id := l_mtl_item_tbl(j).inventory_item_id;
3977          x_trx_error_rec.dst_serial_num_ctrl_code := l_mtl_item_tbl(j).serial_number_control_code;
3978          x_trx_error_rec.dst_location_ctrl_code := l_mtl_item_tbl(j).location_control_code;
3979          x_trx_error_rec.dst_lot_ctrl_code := l_mtl_item_tbl(j).lot_control_code;
3980          x_trx_error_rec.dst_rev_qty_ctrl_code := l_mtl_item_tbl(j).revision_qty_control_code;
3981          x_trx_error_rec.src_serial_num_ctrl_code := r_item_control.serial_number_control_code;
3982          x_trx_error_rec.src_location_ctrl_code := r_item_control.location_control_code;
3983          x_trx_error_rec.src_lot_ctrl_code := r_item_control.lot_control_code;
3984          x_trx_error_rec.src_rev_qty_ctrl_code := r_item_control.revision_qty_control_code;
3985          x_trx_error_rec.comms_nl_trackable_flag := l_mtl_item_tbl(j).comms_nl_trackable_flag;
3986          x_trx_error_rec.transaction_error_date := l_sysdate ;
3987        END IF;
3988 
3989        x_trx_error_rec.error_text := l_error_message;
3990        x_trx_error_rec.transaction_id       := NULL;
3991        x_trx_error_rec.source_type          := 'CSIINTSR';
3992        x_trx_error_rec.source_id            := p_transaction_id;
3993        x_trx_error_rec.processed_flag       := csi_inv_trxs_pkg.g_txn_error;
3994        x_trx_error_rec.transaction_type_id  := csi_inv_trxs_pkg.get_txn_type_id(l_trans_type_code,l_trans_app_code);
3995        x_trx_error_rec.inv_material_transaction_id  := p_transaction_id;
3996        x_trx_error_rec.error_stage          := csi_inv_trxs_pkg.g_ib_update;
3997 
3998      WHEN others THEN
3999         l_sql_error := SQLERRM;
4000         IF (l_debug > 0) THEN
4001              csi_t_gen_utility_pvt.add('You have encountered a "when others" exception in the Internal Order In Transit Receipt');
4002               csi_t_gen_utility_pvt.add('SQL Error: '||l_sql_error);
4003         END IF;
4004         fnd_message.set_name('CSI','CSI_UNEXP_SQL_ERROR');
4005         fnd_message.set_token('API_NAME',l_api_name);
4006         fnd_message.set_token('SQL_ERROR',SQLERRM);
4007         x_return_status := l_fnd_unexpected;
4008 
4009         IF l_mtl_item_tbl.count > 0 THEN
4010           x_trx_error_rec.serial_number := l_mtl_item_tbl(j).serial_number;
4011           x_trx_error_rec.lot_number := l_mtl_item_tbl(j).lot_number;
4012           x_trx_error_rec.instance_id := l_upd_error_instance_id;
4013           x_trx_error_rec.inventory_item_id := l_mtl_item_tbl(j).inventory_item_id;
4014           x_trx_error_rec.dst_serial_num_ctrl_code := l_mtl_item_tbl(j).serial_number_control_code;
4015           x_trx_error_rec.dst_location_ctrl_code := l_mtl_item_tbl(j).location_control_code;
4016           x_trx_error_rec.dst_lot_ctrl_code := l_mtl_item_tbl(j).lot_control_code;
4017           x_trx_error_rec.dst_rev_qty_ctrl_code := l_mtl_item_tbl(j).revision_qty_control_code;
4018           x_trx_error_rec.src_serial_num_ctrl_code := r_item_control.serial_number_control_code;
4019           x_trx_error_rec.src_location_ctrl_code := r_item_control.location_control_code;
4020           x_trx_error_rec.src_lot_ctrl_code := r_item_control.lot_control_code;
4021           x_trx_error_rec.src_rev_qty_ctrl_code := r_item_control.revision_qty_control_code;
4022           x_trx_error_rec.comms_nl_trackable_flag := l_mtl_item_tbl(j).comms_nl_trackable_flag;
4023           x_trx_error_rec.transaction_error_date := l_sysdate ;
4024         END IF;
4025 
4026         x_trx_error_rec.error_text := fnd_message.get;
4027         x_trx_error_rec.transaction_id       := NULL;
4028         x_trx_error_rec.source_type          := 'CSIINTSR';
4029         x_trx_error_rec.source_id            := p_transaction_id;
4030         x_trx_error_rec.processed_flag       := csi_inv_trxs_pkg.g_txn_error;
4031         csi_t_gen_utility_pvt.add('ID_ISO1: '||csi_inv_trxs_pkg.get_txn_type_id(l_trans_type_code,l_trans_app_code));
4032         x_trx_error_rec.transaction_type_id  := csi_inv_trxs_pkg.get_txn_type_id(l_trans_type_code,l_trans_app_code);
4033         csi_t_gen_utility_pvt.add('ID_ISO2: '||csi_inv_trxs_pkg.get_txn_type_id(l_trans_type_code,l_trans_app_code));
4034         x_trx_error_rec.inv_material_transaction_id  := p_transaction_id;
4035         x_trx_error_rec.error_stage          := csi_inv_trxs_pkg.g_ib_update;
4036 
4037    END iso_receipt;
4038 
4039    PROCEDURE iso_direct(p_transaction_id     IN  NUMBER,
4040                         p_message_id         IN  NUMBER,
4041                         x_return_status      OUT NOCOPY VARCHAR2,
4042                         x_trx_error_rec      OUT NOCOPY CSI_DATASTRUCTURES_PUB.TRANSACTION_ERROR_REC)
4043    IS
4044 
4045    l_mtl_item_tbl               CSI_INV_TRXS_PKG.MTL_ITEM_TBL_TYPE;
4046    l_api_name                   VARCHAR2(100)   := 'CSI_INV_ISO_PKG.ISO_DIRECT';
4047    l_api_version                NUMBER          := 1.0;
4048    l_commit                     VARCHAR2(1)     := FND_API.G_FALSE;
4049    l_init_msg_list              VARCHAR2(1)     := FND_API.G_TRUE;
4050    l_validation_level           NUMBER          := FND_API.G_VALID_LEVEL_FULL;
4051    l_active_instance_only       VARCHAR2(10)    := FND_API.G_TRUE;
4052    l_inactive_instance_only     VARCHAR2(10)    := FND_API.G_FALSE;
4053    l_transaction_id             NUMBER          := NULL;
4054    l_resolve_id_columns         VARCHAR2(10)    := FND_API.G_FALSE;
4055    l_object_version_number      NUMBER          := 1;
4056    l_sysdate                    DATE            := SYSDATE;
4057    l_master_organization_id     NUMBER;
4058    l_depreciable                VARCHAR2(1);
4059    l_instance_query_rec         CSI_DATASTRUCTURES_PUB.INSTANCE_QUERY_REC;
4060    l_dest_instance_query_rec    CSI_DATASTRUCTURES_PUB.INSTANCE_QUERY_REC;
4061    l_update_instance_rec        CSI_DATASTRUCTURES_PUB.INSTANCE_REC;
4062    l_upd_src_dest_instance_rec  CSI_DATASTRUCTURES_PUB.INSTANCE_REC;
4063    l_update_dest_instance_rec   CSI_DATASTRUCTURES_PUB.INSTANCE_REC;
4064    l_new_instance_rec           CSI_DATASTRUCTURES_PUB.INSTANCE_REC;
4065    l_new_dest_instance_rec      CSI_DATASTRUCTURES_PUB.INSTANCE_REC;
4066    l_new_src_instance_rec       CSI_DATASTRUCTURES_PUB.INSTANCE_REC;
4067    l_txn_rec                    CSI_DATASTRUCTURES_PUB.TRANSACTION_REC;
4068    l_return_status              VARCHAR2(1);
4069    l_error_code                 VARCHAR2(50);
4070    l_error_message              VARCHAR2(4000);
4071    l_instance_id_lst            CSI_DATASTRUCTURES_PUB.ID_TBL;
4072    l_party_query_rec            CSI_DATASTRUCTURES_PUB.PARTY_QUERY_REC;
4073    l_account_query_rec          CSI_DATASTRUCTURES_PUB.PARTY_ACCOUNT_QUERY_REC;
4074    l_instance_header_tbl        CSI_DATASTRUCTURES_PUB.INSTANCE_HEADER_TBL;
4075    l_src_instance_header_tbl    CSI_DATASTRUCTURES_PUB.INSTANCE_HEADER_TBL;
4076    l_dest_instance_header_tbl   CSI_DATASTRUCTURES_PUB.INSTANCE_HEADER_TBL;
4077    l_ext_attrib_values_tbl      CSI_DATASTRUCTURES_PUB.EXTEND_ATTRIB_VALUES_TBL;
4078    l_party_tbl                  CSI_DATASTRUCTURES_PUB.PARTY_TBL;
4079    l_account_tbl                CSI_DATASTRUCTURES_PUB.PARTY_ACCOUNT_TBL;
4080    l_pricing_attrib_tbl         CSI_DATASTRUCTURES_PUB.PRICING_ATTRIBS_TBL;
4081    l_org_assignments_tbl        CSI_DATASTRUCTURES_PUB.ORGANIZATION_UNITS_TBL;
4082    l_asset_assignment_tbl       CSI_DATASTRUCTURES_PUB.INSTANCE_ASSET_TBL;
4083    l_sub_inventory              VARCHAR2(10);
4084    l_location_type              VARCHAR2(20);
4085    l_trx_action_type            VARCHAR2(50);
4086    l_fnd_success                VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
4087    l_fnd_warning                VARCHAR2(1) := 'W';
4088    l_fnd_error                  VARCHAR2(1) := FND_API.G_RET_STS_ERROR;
4089    l_fnd_unexpected             VARCHAR2(1) := FND_API.G_RET_STS_UNEXP_ERROR;
4090    l_in_inventory               VARCHAR2(25) := CSI_INV_TRXS_PKG.G_IN_INVENTORY;
4091    l_in_transit                 VARCHAR2(25) := CSI_INV_TRXS_PKG.G_IN_TRANSIT;
4092    l_in_relationship            VARCHAR2(25) := 'IN_RELATIONSHIP';
4093    l_fnd_g_num                  NUMBER      := FND_API.G_MISS_NUM;
4094    l_fnd_g_char                 VARCHAR2(1) := FND_API.G_MISS_CHAR;
4095    l_fnd_g_date                 DATE        := FND_API.G_MISS_DATE;
4096    l_instance_usage_code        VARCHAR2(25);
4097    l_organization_id            NUMBER;
4098    l_subinventory_name          VARCHAR2(10);
4099    l_locator_id                 NUMBER;
4100    l_transaction_error_id       NUMBER;
4101    l_trx_type_id                NUMBER;
4102    l_trans_type_code            VARCHAR2(25);
4103    l_trans_app_code             VARCHAR2(5);
4104    l_employee_id                NUMBER;
4105    l_file                       VARCHAR2(500);
4106    l_msg_count                  NUMBER;
4107    l_msg_data                   VARCHAR2(2000);
4108    l_sql_error                  VARCHAR2(2000);
4109    l_msg_index                  NUMBER;
4110    j                            PLS_INTEGER := 1;
4111    k                            PLS_INTEGER := 1;
4112    i                            PLS_INTEGER := 1;
4113    l_tbl_count                  NUMBER := 0;
4114    l_neg_code                   NUMBER := 0;
4115    l_instance_status            VARCHAR2(1);
4116    l_sr_control                 NUMBER := 0;
4117    l_12_loop                    NUMBER := 0;
4118    l_66_flag                    NUMBER := 0;
4119    l_redeploy_flag              VARCHAR2(1);
4120    l_upd_error_instance_id      NUMBER := NULL;
4121 
4122    cursor c_id is
4123      SELECT instance_status_id
4124      FROM   csi_instance_statuses
4125      WHERE  name = FND_PROFILE.VALUE('CSI_DEFAULT_INSTANCE_STATUS');
4126 
4127    r_id     c_id%rowtype;
4128 
4129    -- Get the Transaction ID for the (-) quantity transaction and pass that
4130    -- instead of the (+) transaction ID. This is done so that the hook will be
4131    -- called after the second transaction is processed with the (+) qty and
4132    -- will prevent any timing issues with the transaction manager
4133 
4134    CURSOR c_mtl is
4135       SELECT transfer_transaction_id,
4136              transaction_action_id,
4137              transaction_type_id,
4138              transaction_quantity
4139 	 FROM mtl_material_transactions
4140 	 WHERE transaction_id = p_transaction_id;
4141 
4142    r_mtl     c_mtl%rowtype;
4143 
4144    CURSOR c_item_control (pc_item_id in number,
4145                           pc_org_id in number,
4146 					 p_transaction_id in number) is -- Added for bug#14835893
4147      SELECT serial_number_control_code,
4148            -- lot_control_code,
4149 		  nvl(csi_utl_pkg.get_lot_ctrl_code(p_transaction_id),lot_control_code) lot_control_code,-- Added for bug#14835893
4150             revision_qty_control_code,
4151             location_control_code,
4152             comms_nl_trackable_flag
4153      FROM mtl_system_items_b
4154      WHERE inventory_item_id = pc_item_id
4155      AND organization_id = pc_org_id;
4156 
4157    r_item_control     c_item_control%rowtype;
4158 
4159    CURSOR c_so_info (pc_line_id in NUMBER) is
4160      SELECT oeh.header_id,
4161             oel.line_id,
4162             oeh.order_number,
4163             oel.line_number
4164      FROM   oe_order_headers_all oeh,
4165             oe_order_lines_all oel
4166      WHERE oeh.header_id = oel.header_id
4167      AND   oel.line_id = pc_line_id;
4168 
4169    r_so_info     c_so_info%rowtype;
4170 
4171    CURSOR c_obj_version (pc_instance_id IN NUMBER) is
4172      SELECT object_version_number
4173      FROM   csi_item_instances
4174      WHERE  instance_id = pc_instance_id;
4175 
4176   CURSOR c_loc_ids (pc_org_id IN NUMBER,
4177                     pc_subinv_name IN VARCHAR2) is
4178     SELECT haou.location_id hr_location_id,
4179            msi.location_id  subinv_location_id
4180     FROM hr_all_organization_units haou,
4181          mtl_secondary_inventories msi
4182     WHERE haou.organization_id = pc_org_id
4183     AND msi.organization_id = pc_org_id
4184     AND msi.secondary_inventory_name = pc_subinv_name;
4185 
4186     r_loc_ids     c_loc_ids%rowtype;
4187 
4188    BEGIN
4189 
4190      x_return_status := l_fnd_success;
4191      l_error_message := NULL;
4192 
4193      IF (l_debug > 0) THEN
4194         csi_t_gen_utility_pvt.add('******Start of csi_inv_iso_pkg.iso_direct Transaction procedure******');
4195         csi_t_gen_utility_pvt.add('Start time: '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SS'));
4196         csi_t_gen_utility_pvt.add('csiintsb.pls 115.27');
4197      END IF;
4198 
4199 	-- This will open the cursor and fetch the (-) transaction ID
4200      OPEN c_mtl;
4201      FETCH c_mtl into r_mtl;
4202      CLOSE c_mtl;
4203 
4204      IF (l_debug > 0) THEN
4208         csi_t_gen_utility_pvt.add('l_sysdate set to: '||to_char(l_sysdate,'DD-MON-YYYY HH24:MI:SS'));
4205         csi_t_gen_utility_pvt.add('Direct ISO using Trasfer Trans ID');
4206         csi_t_gen_utility_pvt.add('Transaction ID with (+) is: '||p_transaction_id);
4207         csi_t_gen_utility_pvt.add('Transaction ID with (-) is: '||r_mtl.transfer_transaction_id);
4209      END IF;
4210 
4211      -- This procedure queries all of the Inventory Transaction Records and
4212 	-- returns them as a table.
4213 
4214      csi_inv_trxs_pkg.get_transaction_recs(r_mtl.transfer_transaction_id,
4215                                            l_mtl_item_tbl,
4216                                            l_return_status,
4217                                            l_error_message);
4218 
4219      l_tbl_count := 0;
4220      l_tbl_count := l_mtl_item_tbl.count;
4221      IF (l_debug > 0) THEN
4222         csi_t_gen_utility_pvt.add('Inventory Records Found: '||l_tbl_count);
4223      END IF;
4224 
4225      IF NOT l_return_status = l_fnd_success THEN
4226        IF (l_debug > 0) THEN
4227           csi_t_gen_utility_pvt.add('You have encountered an error in CSI_INV_TRXS_PKG.get_transaction_recs, Transaction ID: '||r_mtl.transfer_transaction_id);
4228        END IF;
4229        RAISE fnd_api.g_exc_error;
4230      END IF;
4231 
4232      -- Get the Master Organization ID
4233      csi_inv_trxs_pkg.get_master_organization(l_mtl_item_tbl(i).organization_id,
4234                                               l_master_organization_id,
4235                                               l_return_status,
4236                                               l_error_message);
4237 
4238      IF NOT l_return_status = l_fnd_success THEN
4239        IF (l_debug > 0) THEN
4240           csi_t_gen_utility_pvt.add('You have encountered an error in csi_inv_trxs_pkg.get_master_organization, Organization ID: '||l_mtl_item_tbl(i).organization_id);
4241        END IF;
4242        RAISE fnd_api.g_exc_error;
4243      END IF;
4244 
4245      -- Call get_fnd_employee_id and get the employee id
4246      l_employee_id := csi_inv_trxs_pkg.get_fnd_employee_id(l_mtl_item_tbl(i).last_updated_by);
4247 
4248      IF l_employee_id = -1 THEN
4249        IF (l_debug > 0) THEN
4250           csi_t_gen_utility_pvt.add('The person who last updated this record: '||l_mtl_item_tbl(i).last_updated_by||' does not exist as a valid employee');
4251        END IF;
4252      END IF;
4253      IF (l_debug > 0) THEN
4254         csi_t_gen_utility_pvt.add('The Employee that is processing this Transaction is: '||l_employee_id);
4255      END IF;
4256 
4257      -- See if this is a depreciable Item to set the status of the transaction record
4258      csi_inv_trxs_pkg.check_depreciable(l_mtl_item_tbl(i).inventory_item_id,
4259      	                               l_depreciable,
4260 									   l_mtl_item_tbl(i).organization_id); --Added for Bug 13988660
4261 
4262      IF (l_debug > 0) THEN
4263         csi_t_gen_utility_pvt.add('Is this Item ID: '||l_mtl_item_tbl(i).inventory_item_id||', Depreciable :'||l_depreciable);
4264      END IF;
4265 
4266 	-- Get the Negative Receipt Code to see if this org allows Negative
4267 	-- Quantity Records 1 = Yes, 2 = No
4268 
4269 	l_neg_code := csi_inv_trxs_pkg.get_neg_inv_code(
4270 						  l_mtl_item_tbl(i).organization_id);
4271 
4272 
4273      IF (l_debug > 0) THEN
4274         csi_t_gen_utility_pvt.add('Negative Code is - 1 = Yes, 2 = No: '||l_neg_code);
4275      END IF;
4276 
4277      -- Determine Transaction Type for this
4278 
4279      l_trans_type_code := 'ISO_DIRECT_SHIP';
4280      l_trans_app_code := 'INV';
4281 
4282      IF (l_debug > 0) THEN
4283            csi_t_gen_utility_pvt.add('Trans Type Code: '||l_trans_type_code);
4284            csi_t_gen_utility_pvt.add('Trans App Code: '||l_trans_app_code);
4285      END IF;
4286 
4287     -- Get Default Profile Instance Status
4288 
4289     OPEN c_id;
4290     FETCH c_id into r_id;
4291     CLOSE c_id;
4292 
4293     IF (l_debug > 0) THEN
4294       csi_t_gen_utility_pvt.add('Default Profile Status: '||r_id.instance_status_id);
4295     END IF;
4296 
4297      -- Now loop through the PL/SQL Table.
4298      j := 1;
4299 
4300      -- Added so that the SO_HEADER_ID and SO_LINE_ID can be added to
4301      -- the transaction record.
4302 
4303      OPEN c_so_info (l_mtl_item_tbl(j).trx_source_line_id);
4304      FETCH c_so_info into r_so_info;
4305      CLOSE c_so_info;
4306 
4307      IF (l_debug > 0) THEN
4308            csi_t_gen_utility_pvt.add('Sales Order Header: '||r_so_info.header_id);
4309            csi_t_gen_utility_pvt.add('Sales Order Line: '||r_so_info.line_id);
4310            csi_t_gen_utility_pvt.add('Order Number: '||r_so_info.order_number);
4311            csi_t_gen_utility_pvt.add('Line Number: '||r_so_info.line_number);
4312      END IF;
4313 
4314      IF (l_debug > 0) THEN
4315         csi_t_gen_utility_pvt.add('Starting to loop through Material Transaction Records');
4316      END IF;
4317 
4318      -- Initialize Transaction Record
4319      l_txn_rec                          := csi_inv_trxs_pkg.init_txn_rec;
4320 
4321      -- Set Status based on redeployment
4322      IF l_depreciable = 'N' THEN
4323        IF l_mtl_item_tbl(i).serial_number is NOT NULL THEN
4324          csi_inv_trxs_pkg.get_redeploy_flag(l_mtl_item_tbl(i).inventory_item_id,
4325                                             l_mtl_item_tbl(i).serial_number,
4326                                             l_sysdate,
4327                                             l_redeploy_flag,
4328                                             l_return_status,
4329                                             l_error_message);
4330        END IF;
4331        IF l_redeploy_flag = 'Y' THEN
4335        END IF;
4332          l_txn_rec.transaction_status_code := csi_inv_trxs_pkg.g_pending;
4333        ELSE
4334          l_txn_rec.transaction_status_code := csi_inv_trxs_pkg.g_complete;
4336      ELSE
4337        l_txn_rec.transaction_status_code := csi_inv_trxs_pkg.g_pending;
4338      END IF;
4339 
4340      IF NOT l_return_status = l_fnd_success THEN
4341        IF (l_debug > 0) THEN
4342           csi_t_gen_utility_pvt.add('Redeploy Flag: '||l_redeploy_flag);
4343           csi_t_gen_utility_pvt.add('You have encountered an error in csi_inv_trxs_pkg.get_redeploy_flag: '||l_error_message);
4344        END IF;
4345        RAISE fnd_api.g_exc_error;
4346      END IF;
4347 
4348      IF (l_debug > 0) THEN
4349         csi_t_gen_utility_pvt.add('Redeploy Flag: '||l_redeploy_flag);
4350         csi_t_gen_utility_pvt.add('Trans Status Code: '||l_txn_rec.transaction_status_code);
4351      END IF;
4352 
4353      -- Create CSI Transaction to be used
4354      l_txn_rec.source_transaction_date  := l_mtl_item_tbl(i).transaction_date;
4355      l_txn_rec.transaction_date         := l_sysdate;
4356      l_txn_rec.transaction_type_id      :=
4357           csi_inv_trxs_pkg.get_txn_type_id(l_trans_type_code,l_trans_app_code);
4358      l_txn_rec.transaction_quantity     :=
4359           l_mtl_item_tbl(i).transaction_quantity;
4360      l_txn_rec.transaction_uom_code     :=  l_mtl_item_tbl(i).transaction_uom;
4361      l_txn_rec.transacted_by            :=  l_employee_id;
4362      l_txn_rec.transaction_action_code  :=  NULL;
4363      l_txn_rec.message_id               :=  p_message_id;
4364      l_txn_rec.inv_material_transaction_id  :=  p_transaction_id;
4365      l_txn_rec.object_version_number    :=  l_object_version_number;
4366      l_txn_rec.source_header_ref_id     :=  r_so_info.header_id;
4367      l_txn_rec.source_line_ref_id       :=  r_so_info.line_id;
4368      l_txn_rec.source_header_ref        :=  to_char(r_so_info.order_number);
4369      l_txn_rec.source_line_ref          :=  substr(to_char(r_so_info.line_number)||'.'||l_mtl_item_tbl(i).shipment_number,1,50);
4370 
4371      csi_inv_trxs_pkg.create_csi_txn(l_txn_rec,
4372                                      l_error_message,
4373                                      l_return_status);
4374 
4375      IF (l_debug > 0) THEN
4376         csi_t_gen_utility_pvt.add('CSI Transaction Created: '||l_txn_rec.transaction_id);
4377      END IF;
4378 
4379      IF NOT l_return_status = l_fnd_success THEN
4380        IF (l_debug > 0) THEN
4381           csi_t_gen_utility_pvt.add('You have encountered an error in csi_inv_trxs_pkg.create_csi_txn: '||p_transaction_id);
4382        END IF;
4383        RAISE fnd_api.g_exc_error;
4384      END IF;
4385 
4386      FOR j in l_mtl_item_tbl.FIRST .. l_mtl_item_tbl.LAST LOOP
4387 
4388      IF (l_debug > 0) THEN
4389        csi_t_gen_utility_pvt.add('Primary UOM: '||l_mtl_item_tbl(j).primary_uom_code);
4390        csi_t_gen_utility_pvt.add('Primary Qty: '||l_mtl_item_tbl(j).primary_quantity);
4391        csi_t_gen_utility_pvt.add('Transaction UOM: '||l_mtl_item_tbl(j).transaction_uom);
4392        csi_t_gen_utility_pvt.add('Transaction Qty: '||l_mtl_item_tbl(j).transaction_quantity);
4393        csi_t_gen_utility_pvt.add('Organization ID: '||l_mtl_item_tbl(j).organization_id);
4394        csi_t_gen_utility_pvt.add('Transfer Org ID: '||l_mtl_item_tbl(j).transfer_organization_id);
4395        csi_t_gen_utility_pvt.add('Transfer Subinv: '||l_mtl_item_tbl(j).transfer_subinventory);
4396      END IF;
4397 
4398      -- Get Receiving Organization Serial Control Code
4399      OPEN c_item_control (l_mtl_item_tbl(j).inventory_item_id,
4400                         l_mtl_item_tbl(j).transfer_organization_id
4401 				    ,p_transaction_id);--Added for bug# 14835893
4402      FETCH c_item_control into r_item_control;
4403      CLOSE c_item_control;
4404 
4405      l_sr_control := r_item_control.serial_number_control_code;
4406 
4407      IF (l_debug > 0) THEN
4408         csi_t_gen_utility_pvt.add('Serial Number : '||l_mtl_item_tbl(j).serial_number);
4409         csi_t_gen_utility_pvt.add('l_sr_control is: '||l_sr_control);
4410         csi_t_gen_utility_pvt.add('Shipping Org Serial Number Control Code: '||l_mtl_item_tbl(j).serial_number_control_code);
4411         csi_t_gen_utility_pvt.add('Receiving Org Serial Number Control Code: '||r_item_control.serial_number_control_code);
4412         csi_t_gen_utility_pvt.add('Shipping Org Lot Control Code: '||l_mtl_item_tbl(j).lot_control_code);
4413         csi_t_gen_utility_pvt.add('Receiving Org Lot Control Code: '||r_item_control.lot_control_code);
4414         csi_t_gen_utility_pvt.add('Shipping Org Loction Control Code: '||l_mtl_item_tbl(j).location_control_code);
4415         csi_t_gen_utility_pvt.add('Receiving Org Location Control Code: '||r_item_control.location_control_code);
4416         csi_t_gen_utility_pvt.add('Shipping Org Revision Control Code: '||l_mtl_item_tbl(j).revision_qty_control_code);
4417         csi_t_gen_utility_pvt.add('Receiving Org Revision Control Code: '||r_item_control.revision_qty_control_code);
4418         csi_t_gen_utility_pvt.add('Receiving Org Trackable Flag: '||r_item_control.comms_nl_trackable_flag);
4419      END IF;
4420 
4421 	-- Set Query Instance Status
4422 	IF l_neg_code = 1 AND l_mtl_item_tbl(j).serial_number_control_code = 1  THEN
4423 	 l_instance_status := FND_API.G_FALSE;
4424      ELSE
4425 	 l_instance_status := FND_API.G_TRUE;
4426 	END IF;
4427 
4428      IF (l_debug > 0) THEN
4429            csi_t_gen_utility_pvt.add('Query Inst Status : '||l_instance_status);
4430      END IF;
4431 
4432      -- Get the Location Ids for Receiving Org
4433      OPEN c_loc_ids (l_mtl_item_tbl(j).transfer_organization_id,
4434                      l_mtl_item_tbl(j).transfer_subinventory);
4435      FETCH c_loc_ids into r_loc_ids;
4436      CLOSE c_loc_ids;
4437 
4438      csi_t_gen_utility_pvt.add('Transfer Subinv Location: '||r_loc_ids.subinv_location_id);
4442          l_instance_usage_code                                :=  l_fnd_g_char;
4439      csi_t_gen_utility_pvt.add('Transfer HR Location    : '||r_loc_ids.hr_location_id);
4440 
4441          l_instance_query_rec                                 :=  csi_inv_trxs_pkg.init_instance_query_rec;
4443 
4444        --Direct Shipment Item
4445 
4446          l_instance_query_rec.inventory_item_id               :=  l_mtl_item_tbl(j).inventory_item_id;
4447          l_instance_query_rec.inv_organization_id             :=  l_mtl_item_tbl(j).organization_id;
4448          l_instance_query_rec.serial_number                   :=  l_mtl_item_tbl(j).serial_number;
4449          l_instance_query_rec.lot_number                      :=  l_mtl_item_tbl(j).lot_number;
4450          l_instance_query_rec.inventory_revision              :=  l_mtl_item_tbl(j).revision;
4451          l_instance_query_rec.inv_subinventory_name           :=  l_mtl_item_tbl(j).subinventory_code;
4452          --l_instance_query_rec.unit_of_measure                 :=  l_mtl_item_tbl(j).transaction_uom;
4453          l_instance_query_rec.inv_locator_id                  :=  l_mtl_item_tbl(j).locator_id;
4454          l_instance_query_rec.instance_usage_code             :=  l_in_inventory;
4455          l_trx_action_type := 'DIRECT_SHIPMENT';
4456          l_instance_usage_code:= l_instance_query_rec.instance_usage_code;
4457 
4458        IF l_mtl_item_tbl(j).serial_number_control_code in (1,6) THEN
4459          l_instance_query_rec.serial_number := NULL;
4460          IF (l_debug > 0) THEN
4461                csi_t_gen_utility_pvt.add('Shipping org is 1,6 so set to NULL');
4462          END IF;
4463        END IF;
4464 
4465        IF (l_debug > 0) THEN
4466           csi_t_gen_utility_pvt.add('Transaction Action Type:'|| l_trx_action_type);
4467           csi_t_gen_utility_pvt.add('Before Get Item Instance');
4468        END IF;
4469 
4470        IF (l_debug > 0) THEN
4471              csi_t_gen_utility_pvt.add('l_12_loop is:'|| l_12_loop);
4472              csi_t_gen_utility_pvt.add('If Count is 1 then bypass Get Item Instance');
4473        END IF;
4474 
4475        IF l_12_loop = 0 THEN
4476 
4477        csi_item_instance_pub.get_item_instances(l_api_version,
4478                                                 l_commit,
4479                                                 l_init_msg_list,
4480                                                 l_validation_level,
4481                                                 l_instance_query_rec,
4482                                                 l_party_query_rec,
4483                                                 l_account_query_rec,
4484                                                 l_transaction_id,
4485                                                 l_resolve_id_columns,
4486 					        l_instance_status,
4487                                                 l_src_instance_header_tbl,
4488                                                 l_return_status,
4489                                                 l_msg_count,
4490                                                 l_msg_data);
4491        END IF; -- End of l_12_loop
4492 
4493        IF (l_debug > 0) THEN
4494           csi_t_gen_utility_pvt.add('After Get Item Instance');
4495        END IF;
4496        l_tbl_count := 0;
4497        l_tbl_count := l_src_instance_header_tbl.count;
4498        IF (l_debug > 0) THEN
4499           csi_t_gen_utility_pvt.add('Source Records Found: '||l_tbl_count);
4500 
4501          IF l_tbl_count > 0 THEN
4502            csi_t_gen_utility_pvt.add('In Transit Order Line ID: '||l_src_instance_header_tbl(i).in_transit_order_line_id);
4503          END IF;
4504 
4505        END IF;
4506 
4507        -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
4508        IF NOT l_return_status = l_fnd_success then
4509          IF (l_debug > 0) THEN
4510             csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.get_item_instance API '||l_msg_data);
4511          END IF;
4512          l_msg_index := 1;
4513            WHILE l_msg_count > 0 loop
4514 	     l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
4515 	     l_msg_index := l_msg_index + 1;
4516              l_msg_count := l_msg_count - 1;
4517   	   END LOOP;
4518 	   RAISE fnd_api.g_exc_error;
4519        END IF;
4520 
4521        IF (l_debug > 0) THEN
4522           csi_t_gen_utility_pvt.add('Before checking to see if Source records Exist');         END IF;
4523 
4524        IF (l_mtl_item_tbl(j).serial_number_control_code = 5 AND -- Ship
4525 	   l_sr_control = 5) OR -- Rec
4526           (l_mtl_item_tbl(j).serial_number_control_code = 5 AND -- Ship
4527 	   l_sr_control = 2) OR -- Rec
4528           (l_mtl_item_tbl(j).serial_number_control_code = 2 AND -- Ship
4529 	   l_sr_control = 5) OR -- Rec
4530           (l_mtl_item_tbl(j).serial_number_control_code = 2 AND -- Ship
4531 	   l_sr_control = 2) OR -- Rec
4532           (l_mtl_item_tbl(j).serial_number_control_code = 6 AND -- Ship
4533 	   l_sr_control = 2) OR -- Rec
4534           (l_mtl_item_tbl(j).serial_number_control_code = 6 AND -- Ship
4535 	   l_sr_control = 5) THEN -- Rec
4536       --    (l_mtl_item_tbl(j).serial_number_control_code = 6 AND -- Ship
4537 --	   l_sr_control = 6) THEN -- Rec
4538           IF l_src_instance_header_tbl.count > 0 THEN
4539 
4540           IF (l_debug > 0) THEN
4541                csi_t_gen_utility_pvt.add('Shipping and Rec Serial Control are both 2,5');
4542                csi_t_gen_utility_pvt.add('Updating Serialized Instance: '||l_mtl_item_tbl(j).serial_number);
4543                csi_t_gen_utility_pvt.add('After you determine this is a Direct Shipment');
4544                csi_t_gen_utility_pvt.add('Instance being updated: '||l_src_instance_header_tbl(i).instance_id);
4548           l_update_instance_rec                              :=  csi_inv_trxs_pkg.init_instance_update_rec;
4545                csi_t_gen_utility_pvt.add('In Transit Order line ID: '||l_src_instance_header_tbl(i).in_transit_order_line_id);
4546           END IF;
4547 
4549           l_update_instance_rec.instance_id                  :=  l_src_instance_header_tbl(i).instance_id;
4550           l_update_instance_rec.inv_organization_id          :=  l_mtl_item_tbl(j).transfer_organization_id;
4551           l_update_instance_rec.vld_organization_id          :=  l_mtl_item_tbl(j).transfer_organization_id;
4552           l_update_instance_rec.inv_subinventory_name        :=  l_mtl_item_tbl(j).transfer_subinventory;
4553           l_update_instance_rec.inv_locator_id               :=  l_mtl_item_tbl(j).transfer_locator_id;
4554           l_update_instance_rec.location_id                  :=  nvl(r_loc_ids.subinv_location_id,r_loc_ids.hr_location_id);
4555           l_update_instance_rec.location_type_code           :=  csi_inv_trxs_pkg.get_location_type_code('Inventory');
4556           l_update_instance_rec.last_oe_order_line_id        :=  r_so_info.line_id;
4557           l_update_instance_rec.instance_usage_code          :=  l_in_inventory;
4558           l_update_instance_rec.object_version_number        :=  l_src_instance_header_tbl(i).object_version_number;
4559 
4560           IF (l_debug > 0) THEN
4561              csi_t_gen_utility_pvt.add('After you initialize the Transaction Record Values');
4562              csi_t_gen_utility_pvt.add('After the update for Direct Shipment is set.');
4563              csi_t_gen_utility_pvt.add('Transfer Org: '||l_update_instance_rec.inv_organization_id);
4564              csi_t_gen_utility_pvt.add('Source Org: '||l_mtl_item_tbl(j).organization_id);
4565           END IF;
4566 
4567           l_party_tbl.delete;
4568           l_account_tbl.delete;
4569           l_pricing_attrib_tbl.delete;
4570           l_org_assignments_tbl.delete;
4571           l_asset_assignment_tbl.delete;
4572 
4573           IF (l_debug > 0) THEN
4574              csi_t_gen_utility_pvt.add('Before Update Item Instance');
4575           END IF;
4576 
4577           csi_item_instance_pub.update_item_instance(l_api_version,
4578                                                      l_commit,
4579                                                      l_init_msg_list,
4580                                                      l_validation_level,
4581                                                      l_update_instance_rec,
4582                                                      l_ext_attrib_values_tbl,
4583                                                      l_party_tbl,
4584                                                      l_account_tbl,
4585                                                      l_pricing_attrib_tbl,
4586                                                      l_org_assignments_tbl,
4587                                                      l_asset_assignment_tbl,
4588                                                      l_txn_rec,
4589                                                      l_instance_id_lst,
4590                                                      l_return_status,
4591                                                      l_msg_count,
4592                                                      l_msg_data);
4593 
4594            l_upd_error_instance_id := NULL;
4595            l_upd_error_instance_id := l_update_instance_rec.instance_id;
4596 
4597            IF (l_debug > 0) THEN
4598               csi_t_gen_utility_pvt.add('After Update Item Instance');
4599               csi_t_gen_utility_pvt.add('You are updating Instance: '||l_update_instance_rec.instance_id);
4600               csi_t_gen_utility_pvt.add('l_upd_error_instance_id is: '||l_upd_error_instance_id);
4601            END IF;
4602 
4603            -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
4604            IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
4605              IF (l_debug > 0) THEN
4606                 csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.update_item_instance API '||l_msg_data);
4607              END IF;
4608              l_msg_index := 1;
4609                WHILE l_msg_count > 0 loop
4610 	         l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
4611 	         l_msg_index := l_msg_index + 1;
4612                  l_msg_count := l_msg_count - 1;
4613   	       END LOOP;
4614 	       RAISE fnd_api.g_exc_error;
4615            END IF;
4616 
4617 	    ELSE -- No Src Records found so error
4618            IF (l_debug > 0) THEN
4619               csi_t_gen_utility_pvt.add('No Records were found in Install Base');
4620            END IF;
4621 
4622            fnd_message.set_name('CSI','CSI_IB_RECORD_NOTFOUND');
4623            fnd_message.set_token('ITEM',l_mtl_item_tbl(j).inventory_item_id);
4624            fnd_message.set_token('SUBINVENTORY',l_mtl_item_tbl(j).subinventory_code);
4625            fnd_message.set_token('ORG_ID',l_mtl_item_tbl(j).organization_id);
4626            fnd_message.set_token('ITEM',l_mtl_item_tbl(j).inventory_item_id);
4627            l_error_message := fnd_message.get;
4628            RAISE fnd_api.g_exc_error;
4629 	    END IF;  -- End of 2,5 and 2,5 IF
4630 
4631        ELSIF (l_mtl_item_tbl(j).serial_number_control_code = 1 AND -- Ship
4632               l_sr_control = 1) OR -- Rec
4633              (l_mtl_item_tbl(j).serial_number_control_code = 6 AND -- Ship
4634               l_sr_control = 6) OR -- Rec
4635              (l_mtl_item_tbl(j).serial_number_control_code = 6 AND -- Ship
4636               l_sr_control = 1) OR -- Rec
4637              (l_mtl_item_tbl(j).serial_number_control_code = 1 AND -- Ship
4638               l_sr_control = 6) THEN -- Rec
4639 
4640             IF (l_debug > 0) THEN
4641                csi_t_gen_utility_pvt.add('Shipping and Rec Serial Control are both 1,6');
4645              l_sr_control = 6) OR   -- Rec
4642           END IF;
4643 
4644          IF (l_mtl_item_tbl(j).serial_number_control_code = 6 AND -- Ship
4646             (l_mtl_item_tbl(j).serial_number_control_code = 6 AND -- Ship
4647              l_sr_control = 1) THEN -- Rec
4648            l_66_flag := 1;
4649            IF (l_debug > 0) THEN
4650              csi_t_gen_utility_pvt.add('l_66_flag is :'||l_66_flag);
4651            END IF;
4652          END IF;
4653 
4654 
4655          IF l_src_instance_header_tbl.count = 0 THEN
4656            IF l_neg_code = 1 THEN  -- Allow Neg Qtys on NON Serial Items ONLY
4657 
4658 
4659          IF (l_debug > 0) THEN
4660             csi_t_gen_utility_pvt.add('No Source Recs found so create Serial Instance ');
4661          END IF;
4662 
4663 
4664            l_new_src_instance_rec                              :=  csi_inv_trxs_pkg.init_instance_create_rec;
4665            l_new_src_instance_rec.inventory_item_id            :=  l_mtl_item_tbl(j).inventory_item_id;
4666            l_new_src_instance_rec.inventory_revision           :=  l_mtl_item_tbl(j).revision;
4667            l_new_src_instance_rec.inv_master_organization_id   :=  l_master_organization_id;
4668            l_new_src_instance_rec.mfg_serial_number_flag       :=  'N';
4669            l_new_src_instance_rec.lot_number                   :=  l_mtl_item_tbl(j).lot_number;
4670            l_new_src_instance_rec.quantity                     :=  l_mtl_item_tbl(j).transaction_quantity;
4671            l_new_src_instance_rec.unit_of_measure              :=  l_mtl_item_tbl(j).transaction_uom;
4672            l_new_src_instance_rec.location_id                  :=  nvl(l_mtl_item_tbl(j).subinv_location_id,l_mtl_item_tbl(j).hr_location_id);
4673            l_new_src_instance_rec.location_type_code           :=  csi_inv_trxs_pkg.get_location_type_code('Inventory');
4674            l_new_src_instance_rec.instance_usage_code          :=  l_instance_usage_code;
4675            l_new_src_instance_rec.inv_organization_id          :=  l_mtl_item_tbl(j).organization_id;
4676            l_new_src_instance_rec.vld_organization_id          :=  l_mtl_item_tbl(j).organization_id;
4677            l_new_src_instance_rec.inv_subinventory_name        :=  l_mtl_item_tbl(j).subinventory_code;
4678            l_new_src_instance_rec.inv_locator_id               :=  l_mtl_item_tbl(j).locator_id;
4679            l_new_src_instance_rec.customer_view_flag           :=  'N';
4680            l_new_src_instance_rec.merchant_view_flag           :=  'Y';
4681            l_new_src_instance_rec.operational_status_code      :=  'NOT_USED';
4682            l_new_src_instance_rec.object_version_number        :=  l_object_version_number;
4683            l_new_src_instance_rec.active_start_date            :=  l_sysdate;
4684            l_new_src_instance_rec.active_end_date              :=  NULL;
4685 
4686            l_ext_attrib_values_tbl  :=  csi_inv_trxs_pkg.init_ext_attrib_values_tbl;
4687            l_party_tbl              :=  csi_inv_trxs_pkg.init_party_tbl;
4688            l_account_tbl            :=  csi_inv_trxs_pkg.init_account_tbl;
4689            l_pricing_attrib_tbl     :=  csi_inv_trxs_pkg.init_pricing_attribs_tbl;
4690            l_org_assignments_tbl    :=  csi_inv_trxs_pkg.init_org_assignments_tbl;
4691            l_asset_assignment_tbl   :=  csi_inv_trxs_pkg.init_asset_assignment_tbl;
4692 
4693            IF (l_debug > 0) THEN
4694               csi_t_gen_utility_pvt.add('Before Create Source Item Instance');
4695            END IF;
4696 
4697            csi_item_instance_pub.create_item_instance(l_api_version,
4698                                                       l_commit,
4699                                                       l_init_msg_list,
4700                                                       l_validation_level,
4701                                                       l_new_src_instance_rec,
4702                                                       l_ext_attrib_values_tbl,
4703                                                       l_party_tbl,
4704                                                       l_account_tbl,
4705                                                       l_pricing_attrib_tbl,
4706                                                       l_org_assignments_tbl,
4707                                                       l_asset_assignment_tbl,
4708                                                       l_txn_rec,
4709                                                       l_return_status,
4710                                                       l_msg_count,
4711                                                       l_msg_data);
4712 
4713              IF (l_debug > 0) THEN
4714                 csi_t_gen_utility_pvt.add('After Create Source Item Instance');
4715              END IF;
4716 
4717              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
4718 
4719              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
4720                IF (l_debug > 0) THEN
4721                   csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.create_item_instance API '||l_msg_data);
4722                END IF;
4723                l_msg_index := 1;
4724                  WHILE l_msg_count > 0 loop
4725 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
4726 	           l_msg_index := l_msg_index + 1;
4727                 l_msg_count := l_msg_count - 1;
4728   	         END LOOP;
4729 	         RAISE fnd_api.g_exc_error;
4730              END IF;
4731 
4732            ELSE -- Neg Code is <> 1 so Neg Qtys are not allowed so error
4733              IF (l_debug > 0) THEN
4734                csi_t_gen_utility_pvt.add('No Records were found in Install Base - 11');
4735              END IF;
4736 
4737              fnd_message.set_name('CSI','CSI_IB_RECORD_NOTFOUND');
4738              fnd_message.set_token('ITEM',l_mtl_item_tbl(j).inventory_item_id);
4739              fnd_message.set_token('SUBINVENTORY',l_mtl_item_tbl(j).subinventory_code);
4740              fnd_message.set_token('ORG_ID',l_mtl_item_tbl(j).organization_id);
4741              fnd_message.set_token('ITEM',l_mtl_item_tbl(j).inventory_item_id);
4742              l_error_message := fnd_message.get;
4743              RAISE fnd_api.g_exc_error;
4744 
4745            END IF;  -- End of Neg Qty If
4746 
4747          ELSIF l_src_instance_header_tbl.count = 1 THEN
4748 
4749          IF (l_debug > 0) THEN
4750             csi_t_gen_utility_pvt.add('You will update instance: '||l_src_instance_header_tbl(i).instance_id);
4751             csi_t_gen_utility_pvt.add('End Date is: '||l_src_instance_header_tbl(i).active_end_date);
4752          END IF;
4753 
4754 
4755            l_upd_src_dest_instance_rec                        :=  csi_inv_trxs_pkg.init_instance_update_rec;
4756            l_upd_src_dest_instance_rec.instance_id            :=  l_src_instance_header_tbl(i).instance_id;
4757            l_upd_src_dest_instance_rec.active_end_date        :=  NULL;
4758            l_upd_src_dest_instance_rec.quantity               :=  l_src_instance_header_tbl(i).quantity - abs(l_mtl_item_tbl(j).primary_quantity);
4759            l_upd_src_dest_instance_rec.last_oe_order_line_id  :=  r_so_info.line_id;
4760            l_upd_src_dest_instance_rec.object_version_number  :=  l_src_instance_header_tbl(i).object_version_number;
4761 
4762            l_party_tbl.delete;
4763            l_account_tbl.delete;
4764            l_pricing_attrib_tbl.delete;
4765            l_org_assignments_tbl.delete;
4766            l_asset_assignment_tbl.delete;
4767 
4768            IF (l_debug > 0) THEN
4769               csi_t_gen_utility_pvt.add('Before Update Source Item Instance - Neg Qty');
4770            END IF;
4771 
4772            l_upd_src_dest_instance_rec.instance_status_id := nvl(csi_inv_trxs_pkg.get_default_status_id(l_txn_rec.transaction_type_id),r_id.instance_status_id);
4773 
4774            IF (l_debug > 0) THEN
4775              csi_t_gen_utility_pvt.add('Instance Status Id: '||l_upd_src_dest_instance_rec.instance_status_id);
4776            END IF;
4777 
4778            csi_item_instance_pub.update_item_instance(l_api_version,
4779                                                       l_commit,
4780                                                       l_init_msg_list,
4781                                                       l_validation_level,
4782                                                       l_upd_src_dest_instance_rec,
4783                                                       l_ext_attrib_values_tbl,
4784                                                       l_party_tbl,
4785                                                       l_account_tbl,
4786                                                       l_pricing_attrib_tbl,
4787                                                       l_org_assignments_tbl,
4788                                                       l_asset_assignment_tbl,
4789                                                       l_txn_rec,
4790                                                       l_instance_id_lst,
4791                                                       l_return_status,
4792                                                       l_msg_count,
4793                                                       l_msg_data);
4794 
4795            l_upd_error_instance_id := NULL;
4796            l_upd_error_instance_id := l_upd_src_dest_instance_rec.instance_id;
4797 
4798            IF (l_debug > 0) THEN
4799               csi_t_gen_utility_pvt.add('After Update Item Instance');
4800               csi_t_gen_utility_pvt.add('l_upd_error_instance_id is: '||l_upd_error_instance_id);
4801            END IF;
4802 
4803            -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
4804            IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
4805              IF (l_debug > 0) THEN
4806                 csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.update_item_instance API '||l_msg_data);
4807              END IF;
4808              l_msg_index := 1;
4809                WHILE l_msg_count > 0 loop
4810 	         l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
4811 	         l_msg_index := l_msg_index + 1;
4812                  l_msg_count := l_msg_count - 1;
4813   	       END LOOP;
4814 	       RAISE fnd_api.g_exc_error;
4815            END IF;
4816 
4817            ELSIF l_src_instance_header_tbl.count > 1 THEN
4818            -- Multiple Instances were found so throw error
4819              IF (l_debug > 0) THEN
4820                csi_t_gen_utility_pvt.add('Multiple Instances were Found in Install BaseBase-11');
4821              END IF;
4822              fnd_message.set_name('CSI','CSI_TXN_MULT_INST_FOUND');
4823              fnd_message.set_token('INV_ITEM_ID',l_mtl_item_tbl(j).inventory_item_id);
4824              fnd_message.set_token('SUBINV',l_mtl_item_tbl(j).subinventory_code);
4825              fnd_message.set_token('INV_ORG_ID',l_mtl_item_tbl(j).organization_id);
4826              fnd_message.set_token('LOCATOR',l_mtl_item_tbl(j).locator_id);
4827              l_error_message := fnd_message.get;
4828              RAISE fnd_api.g_exc_error;
4829 
4830          END IF;  -- End of If for Source Count
4831 
4832 	    -- Get Destination Records
4833 
4834              l_instance_query_rec                               :=  csi_inv_trxs_pkg.init_instance_query_rec;
4835 
4836              l_instance_query_rec.instance_usage_code           :=  l_in_inventory;
4837              l_instance_query_rec.inventory_item_id               :=  l_mtl_item_tbl(j).inventory_item_id;
4838              l_instance_query_rec.inventory_revision              :=  l_mtl_item_tbl(j).revision;
4839              l_instance_query_rec.lot_number                      :=  l_mtl_item_tbl(j).lot_number;
4840              l_instance_query_rec.inv_subinventory_name         :=  l_mtl_item_tbl(j).transfer_subinventory;
4841              l_instance_query_rec.inv_organization_id           :=  l_mtl_item_tbl(j).transfer_organization_id;
4842              l_instance_query_rec.inv_locator_id                :=  l_mtl_item_tbl(j).transfer_locator_id;
4843              l_instance_usage_code                              :=  l_instance_query_rec.instance_usage_code;
4844              l_subinventory_name                                :=  l_mtl_item_tbl(j).transfer_subinventory;
4845              l_organization_id                                  :=  l_mtl_item_tbl(j).transfer_organization_id;
4846              l_locator_id                                       :=  l_mtl_item_tbl(j).transfer_locator_id;
4847 
4848            IF (l_debug > 0) THEN
4849               csi_t_gen_utility_pvt.add('Before Get Item Instance');
4850            END IF;
4851 
4852            csi_item_instance_pub.get_item_instances(l_api_version,
4853                                                     l_commit,
4854                                                     l_init_msg_list,
4855                                                     l_validation_level,
4856                                                     l_instance_query_rec,
4857                                                     l_party_query_rec,
4858                                                     l_account_query_rec,
4859                                                     l_transaction_id,
4860                                                     l_resolve_id_columns,
4861                                                     l_inactive_instance_only,
4862                                                     l_dest_instance_header_tbl,
4863                                                     l_return_status,
4864                                                     l_msg_count,
4865                                                     l_msg_data);
4866 
4867            IF (l_debug > 0) THEN
4868               csi_t_gen_utility_pvt.add('After Get Item Instance');
4869            END IF;
4870            l_tbl_count := 0;
4871            l_tbl_count :=  l_dest_instance_header_tbl.count;
4872            IF (l_debug > 0) THEN
4873               csi_t_gen_utility_pvt.add('Source Records Found: '||l_tbl_count);
4874            END IF;
4875 
4876            -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
4877            IF NOT l_return_status = l_fnd_success then
4878              IF (l_debug > 0) THEN
4879                 csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.get_item_instance API '||l_msg_data);
4880              END IF;
4881              l_msg_index := 1;
4882 	         WHILE l_msg_count > 0 loop
4883 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
4884 	           l_msg_index := l_msg_index + 1;
4885                l_msg_count := l_msg_count - 1;
4886   	         END LOOP;
4887 	         RAISE fnd_api.g_exc_error;
4888            END IF;
4889 
4890            IF l_dest_instance_header_tbl.count = 0 THEN  -- Installed Base Destination Records are not found so create a new record
4891 
4892              IF (l_debug > 0) THEN
4893                 csi_t_gen_utility_pvt.add('Creating New Dest dest Instance - Neg Qty');
4894              END IF;
4895 
4896              l_new_dest_instance_rec                              :=  csi_inv_trxs_pkg.init_instance_create_rec;
4897              l_new_dest_instance_rec.inventory_item_id            :=  l_mtl_item_tbl(j).inventory_item_id;
4898              l_new_dest_instance_rec.inventory_revision           :=  l_mtl_item_tbl(j).revision;
4899              l_new_dest_instance_rec.inv_master_organization_id   :=  l_master_organization_id;
4900              l_new_dest_instance_rec.mfg_serial_number_flag       :=  'N';
4901              l_new_dest_instance_rec.lot_number                   :=  l_mtl_item_tbl(j).lot_number;
4905              l_new_dest_instance_rec.location_type_code           :=  csi_inv_trxs_pkg.get_location_type_code('Inventory');
4902              l_new_dest_instance_rec.quantity                     :=  abs(l_mtl_item_tbl(j).transaction_quantity);
4903              l_new_dest_instance_rec.unit_of_measure              :=  l_mtl_item_tbl(j).transaction_uom;
4904              l_new_dest_instance_rec.location_id                  :=  nvl(r_loc_ids.subinv_location_id,r_loc_ids.hr_location_id);
4906              l_new_dest_instance_rec.instance_usage_code          :=  l_instance_usage_code;
4907              l_new_dest_instance_rec.inv_organization_id          :=  l_organization_id;
4908              l_new_dest_instance_rec.vld_organization_id          :=  l_organization_id;
4909              l_new_dest_instance_rec.inv_subinventory_name        :=  l_subinventory_name;
4910              l_new_dest_instance_rec.inv_locator_id               :=  l_locator_id;
4911              l_new_dest_instance_rec.customer_view_flag           :=  'N';
4912              l_new_dest_instance_rec.merchant_view_flag           :=  'Y';
4913              l_new_dest_instance_rec.operational_status_code      :=  'NOT_USED';
4914              l_new_dest_instance_rec.object_version_number        :=  l_object_version_number;
4915              l_new_dest_instance_rec.active_start_date            :=  l_sysdate;
4916              l_new_dest_instance_rec.active_end_date              :=  NULL;
4917              l_new_dest_instance_rec.last_oe_order_line_id        :=  r_so_info.line_id;
4918 
4919              l_ext_attrib_values_tbl                              :=  csi_inv_trxs_pkg.init_ext_attrib_values_tbl;
4920              l_party_tbl                                          :=  csi_inv_trxs_pkg.init_party_tbl;
4921              l_account_tbl                                        :=  csi_inv_trxs_pkg.init_account_tbl;
4922              l_pricing_attrib_tbl                                 :=  csi_inv_trxs_pkg.init_pricing_attribs_tbl;
4923              l_org_assignments_tbl                                :=  csi_inv_trxs_pkg.init_org_assignments_tbl;
4924              l_asset_assignment_tbl                               :=  csi_inv_trxs_pkg.init_asset_assignment_tbl;
4925 
4926              IF (l_debug > 0) THEN
4927                csi_t_gen_utility_pvt.add('Before Create Item Instance - Neg Qty');
4928                csi_t_gen_utility_pvt.add('In Transit Order Line ID on Dest Rec: '||l_new_dest_instance_rec.last_oe_order_line_id);
4929              END IF;
4930 
4931              csi_item_instance_pub.create_item_instance(l_api_version,
4932                                                         l_commit,
4933                                                         l_init_msg_list,
4934                                                         l_validation_level,
4935                                                         l_new_dest_instance_rec,
4936                                                         l_ext_attrib_values_tbl,
4937                                                         l_party_tbl,
4938                                                         l_account_tbl,
4939                                                         l_pricing_attrib_tbl,
4940                                                         l_org_assignments_tbl,
4941                                                         l_asset_assignment_tbl,
4942                                                         l_txn_rec,
4943                                                         l_return_status,
4944                                                         l_msg_count,
4945                                                         l_msg_data);
4946 
4947              IF (l_debug > 0) THEN
4948                 csi_t_gen_utility_pvt.add('After Create Item Instance - Neg Qty');
4949              END IF;
4950              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
4951 
4952              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
4953                IF (l_debug > 0) THEN
4954                   csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.create_item_instance API '||l_msg_data);
4955                END IF;
4956                l_msg_index := 1;
4957                  WHILE l_msg_count > 0 loop
4958 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
4959 	           l_msg_index := l_msg_index + 1;
4960                    l_msg_count := l_msg_count - 1;
4961   	         END LOOP;
4962 	         RAISE fnd_api.g_exc_error;
4963              END IF;
4964 
4965            ELSIF l_dest_instance_header_tbl.count = 1 THEN -- Installed Base Destination Records Found
4966 
4967                l_update_dest_instance_rec                         :=  csi_inv_trxs_pkg.init_instance_update_rec;
4968                l_update_dest_instance_rec.instance_id             :=  l_dest_instance_header_tbl(i).instance_id;
4969                l_update_dest_instance_rec.quantity                :=  l_dest_instance_header_tbl(i).quantity + abs(l_mtl_item_tbl(j).primary_quantity);
4970                l_update_dest_instance_rec.active_end_date         :=  NULL;
4971                l_update_dest_instance_rec.object_version_number   :=  l_dest_instance_header_tbl(i).object_version_number;
4972                l_update_dest_instance_rec.last_oe_order_line_id   :=  r_so_info.line_id;
4973 
4974                l_party_tbl.delete;
4975                l_account_tbl.delete;
4976                l_pricing_attrib_tbl.delete;
4977                l_org_assignments_tbl.delete;
4978                l_asset_assignment_tbl.delete;
4979 
4980                l_update_dest_instance_rec.instance_status_id := nvl(csi_inv_trxs_pkg.get_default_status_id(l_txn_rec.transaction_type_id),r_id.instance_status_id);
4981 
4982                IF (l_debug > 0) THEN
4983                  csi_t_gen_utility_pvt.add('Instance Status Id: '||l_update_dest_instance_rec.instance_status_id);
4984                END IF;
4985 
4986                IF (l_debug > 0) THEN
4987                   csi_t_gen_utility_pvt.add('Before Update Item Instance');
4988                   csi_t_gen_utility_pvt.add('In Transit Order Line ID in Updated Instance: '||l_update_dest_instance_rec.last_oe_order_line_id);
4989                END IF;
4990 
4991                csi_item_instance_pub.update_item_instance(l_api_version,
4992                                                           l_commit,
4993                                                           l_init_msg_list,
4994                                                           l_validation_level,
4995                                                           l_update_dest_instance_rec,
4996                                                           l_ext_attrib_values_tbl,
4997                                                           l_party_tbl,
4998                                                           l_account_tbl,
4999                                                           l_pricing_attrib_tbl,
5000                                                           l_org_assignments_tbl,
5001                                                           l_asset_assignment_tbl,
5002                                                           l_txn_rec,
5003                                                           l_instance_id_lst,
5004                                                           l_return_status,
5005                                                           l_msg_count,
5006                                                           l_msg_data);
5007 
5008              l_upd_error_instance_id := NULL;
5009              l_upd_error_instance_id := l_update_dest_instance_rec.instance_id;
5010 
5011              IF (l_debug > 0) THEN
5012                 csi_t_gen_utility_pvt.add('After Update Item Instance');
5013                 csi_t_gen_utility_pvt.add('l_upd_error_instance_id is: '||l_upd_error_instance_id);
5014              END IF;
5015 
5016              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
5017              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
5018                IF (l_debug > 0) THEN
5019                   csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.c API '||l_msg_data);
5020                END IF;
5021                l_msg_index := 1;
5022                  WHILE l_msg_count > 0 loop
5023 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
5024 	           l_msg_index := l_msg_index + 1;
5025                 l_msg_count := l_msg_count - 1;
5026                  END LOOP;
5027 	         RAISE fnd_api.g_exc_error;
5028              END IF;
5029 
5030            ELSIF l_dest_instance_header_tbl.count > 1 THEN
5031              -- Multiple Instances were found so throw error
5032              IF (l_debug > 0) THEN
5033                csi_t_gen_utility_pvt.add('Multiple Instances were Found in Install Base-60');
5034              END IF;
5035              fnd_message.set_name('CSI','CSI_TXN_MULT_INST_FOUND');
5036              fnd_message.set_token('INV_ITEM_ID',l_mtl_item_tbl(j).inventory_item_id);
5037              fnd_message.set_token('SUBINV',l_mtl_item_tbl(j).subinventory_code);
5038              fnd_message.set_token('INV_ORG_ID',l_mtl_item_tbl(j).organization_id);
5039              fnd_message.set_token('LOCATOR',l_mtl_item_tbl(j).locator_id);
5040              l_error_message := fnd_message.get;
5041              RAISE fnd_api.g_exc_error;
5042 
5043            END IF;    -- End of Destination Record If
5044 
5045        ELSIF (l_mtl_item_tbl(j).serial_number_control_code = 5 AND -- Ship
5046 	      l_sr_control = 1) OR -- Rec
5047              (l_mtl_item_tbl(j).serial_number_control_code = 5 AND -- Ship
5048 	      l_sr_control = 6) OR -- Rec
5049              (l_mtl_item_tbl(j).serial_number_control_code = 2 AND -- Ship
5050 	      l_sr_control = 1) OR -- Rec
5051              (l_mtl_item_tbl(j).serial_number_control_code = 2 AND -- Ship
5052 	      l_sr_control = 6) THEN -- Rec
5053 
5054           IF (l_debug > 0) THEN
5055                csi_t_gen_utility_pvt.add('Shipping is 2,5,6 and Rec Serial Control is 1,6');
5056           END IF;
5057 
5058 -- Bug 3880731 - Take out the loop
5059 
5060         --FOR k in l_src_instance_header_tbl.FIRST .. abs(l_mtl_item_tbl(j).primary_quantity) LOOP
5061              IF (l_debug > 0) THEN
5062                    csi_t_gen_utility_pvt.add('Serial Control at Shipping is 2,5,6 and Receiving is 1,6');
5063                    csi_t_gen_utility_pvt.add('Expire The Serialized Instance First');
5064                    csi_t_gen_utility_pvt.add('Instance being updated: '||l_src_instance_header_tbl(i).instance_id);
5065              END IF;
5066 
5067              l_update_instance_rec                              :=  csi_inv_trxs_pkg.init_instance_update_rec;
5068              l_update_instance_rec.instance_id                  :=  l_src_instance_header_tbl(i).instance_id;
5069              l_update_instance_rec.active_end_date              :=  l_sysdate;
5070              l_update_instance_rec.last_oe_order_line_id        :=  r_so_info.line_id;
5071              l_update_instance_rec.object_version_number        :=  l_src_instance_header_tbl(i).object_version_number;
5072 
5073            IF (l_debug > 0) THEN
5074               csi_t_gen_utility_pvt.add('After you initialize the Update Record Values');
5075               csi_t_gen_utility_pvt.add('Instance Updated: '||l_update_instance_rec.instance_id);
5076               csi_t_gen_utility_pvt.add('End Date Passed in: '||to_char(l_update_instance_rec.active_end_date,'DD-MON-YYYY HH24:MI:SS'));
5077               csi_t_gen_utility_pvt.add('Object Version: '||l_update_instance_rec.object_version_number);
5078            END IF;
5079 
5080            l_party_tbl.delete;
5081            l_account_tbl.delete;
5082            l_pricing_attrib_tbl.delete;
5083            l_org_assignments_tbl.delete;
5084            l_asset_assignment_tbl.delete;
5085 
5086            IF (l_debug > 0) THEN
5087                  csi_t_gen_utility_pvt.add('Before Update Item Instance');
5088            END IF;
5089 
5090            csi_item_instance_pub.update_item_instance(l_api_version,
5091                                                       l_commit,
5092                                                       l_init_msg_list,
5093                                                       l_validation_level,
5094                                                       l_update_instance_rec,
5095                                                       l_ext_attrib_values_tbl,
5096                                                       l_party_tbl,
5097                                                       l_account_tbl,
5098                                                       l_pricing_attrib_tbl,
5099                                                       l_org_assignments_tbl,
5100                                                       l_asset_assignment_tbl,
5101                                                       l_txn_rec,
5102                                                       l_instance_id_lst,
5103                                                       l_return_status,
5104                                                       l_msg_count,
5105                                                       l_msg_data);
5106 
5107            l_upd_error_instance_id := NULL;
5108            l_upd_error_instance_id := l_update_instance_rec.instance_id;
5109 
5110            IF (l_debug > 0) THEN
5111                  csi_t_gen_utility_pvt.add('After Update Item Instance');
5112                  csi_t_gen_utility_pvt.add('You are updating Instance: '||l_update_instance_rec.instance_id);
5113                  csi_t_gen_utility_pvt.add('You are updating Serial Number: '||l_update_instance_rec.serial_number);
5114                  csi_t_gen_utility_pvt.add('l_upd_error_instance_id is: '||l_upd_error_instance_id);
5115            END IF;
5116 
5117            -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
5118            IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
5119              IF (l_debug > 0) THEN
5120                    csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.update_item_instance API '||l_msg_data);
5121              END IF;
5122              l_msg_index := 1;
5123                WHILE l_msg_count > 0 loop
5124 	         l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
5125 	         l_msg_index := l_msg_index + 1;
5126                  l_msg_count := l_msg_count - 1;
5127   	       END LOOP;
5128 	       RAISE fnd_api.g_exc_error;
5129            END IF;
5130 
5131 -- Bug 3880731 Take out the loop
5132        -- END LOOP; -- End For Loop for Update of Sr Instances
5133 
5134         -- Now Query for Non Serialized In Inventory Record 1 Time Only
5135         IF j = 1 THEN
5136 
5137              l_instance_query_rec                               :=  csi_inv_trxs_pkg.init_instance_query_rec;
5138 
5139              l_instance_query_rec.instance_usage_code           :=  l_in_inventory;
5140              l_instance_query_rec.inventory_item_id               :=  l_mtl_item_tbl(j).inventory_item_id;
5141              l_instance_query_rec.inventory_revision              :=  l_mtl_item_tbl(j).revision;
5142              l_instance_query_rec.lot_number                      :=  l_mtl_item_tbl(j).lot_number;
5143              l_instance_query_rec.inv_subinventory_name         :=  l_mtl_item_tbl(j).transfer_subinventory;
5144              l_instance_query_rec.inv_organization_id           :=  l_mtl_item_tbl(j).transfer_organization_id;
5145              l_instance_query_rec.inv_locator_id                :=  l_mtl_item_tbl(j).transfer_locator_id;
5146              l_instance_usage_code                              :=  l_instance_query_rec.instance_usage_code;
5147              l_subinventory_name                                :=  l_mtl_item_tbl(j).transfer_subinventory;
5148              l_organization_id                                  :=  l_mtl_item_tbl(j).transfer_organization_id;
5149              l_locator_id                                       :=  l_mtl_item_tbl(j).transfer_locator_id;
5150 
5151            IF (l_debug > 0) THEN
5152               csi_t_gen_utility_pvt.add('Before Get Item Instance');
5153            END IF;
5154 
5155            csi_item_instance_pub.get_item_instances(l_api_version,
5156                                                     l_commit,
5157                                                     l_init_msg_list,
5158                                                     l_validation_level,
5159                                                     l_instance_query_rec,
5160                                                     l_party_query_rec,
5161                                                     l_account_query_rec,
5162                                                     l_transaction_id,
5163                                                     l_resolve_id_columns,
5164                                                     l_inactive_instance_only,
5165                                                     l_dest_instance_header_tbl,
5166                                                     l_return_status,
5167                                                     l_msg_count,
5168                                                     l_msg_data);
5169 
5170            IF (l_debug > 0) THEN
5171               csi_t_gen_utility_pvt.add('After Get Item Instance');
5172            END IF;
5173            l_tbl_count := 0;
5174            l_tbl_count :=  l_dest_instance_header_tbl.count;
5175            IF (l_debug > 0) THEN
5176               csi_t_gen_utility_pvt.add('Source Records Found: '||l_tbl_count);
5177            END IF;
5178 
5179            -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
5180            IF NOT l_return_status = l_fnd_success then
5181              IF (l_debug > 0) THEN
5182                 csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.get_item_instance API '||l_msg_data);
5183              END IF;
5184              l_msg_index := 1;
5185 	         WHILE l_msg_count > 0 loop
5186 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
5187 	           l_msg_index := l_msg_index + 1;
5188                l_msg_count := l_msg_count - 1;
5189   	         END LOOP;
5190 	         RAISE fnd_api.g_exc_error;
5191            END IF;
5192 
5193            IF l_dest_instance_header_tbl.count = 0 THEN  -- Installed Base Destination Records are not found so create a new record
5194 
5195              IF (l_debug > 0) THEN
5196                 csi_t_gen_utility_pvt.add('Creating New Dest dest Instance - Neg Qty');
5197              END IF;
5198 
5202              l_new_dest_instance_rec.inv_master_organization_id   :=  l_master_organization_id;
5199              l_new_dest_instance_rec                              :=  csi_inv_trxs_pkg.init_instance_create_rec;
5200              l_new_dest_instance_rec.inventory_item_id            :=  l_mtl_item_tbl(j).inventory_item_id;
5201              l_new_dest_instance_rec.inventory_revision           :=  l_mtl_item_tbl(j).revision;
5203              l_new_dest_instance_rec.mfg_serial_number_flag       :=  'N';
5204              l_new_dest_instance_rec.lot_number                   :=  l_mtl_item_tbl(j).lot_number;
5205              l_new_dest_instance_rec.quantity                     :=  abs(l_mtl_item_tbl(j).transaction_quantity);
5206              l_new_dest_instance_rec.unit_of_measure              :=  l_mtl_item_tbl(j).transaction_uom;
5207              l_new_dest_instance_rec.location_id                  :=  nvl(r_loc_ids.subinv_location_id,r_loc_ids.hr_location_id);
5208              l_new_dest_instance_rec.location_type_code           :=  csi_inv_trxs_pkg.get_location_type_code('Inventory');
5209              l_new_dest_instance_rec.instance_usage_code          :=  l_instance_usage_code;
5210              l_new_dest_instance_rec.inv_organization_id          :=  l_organization_id;
5211              l_new_dest_instance_rec.vld_organization_id          :=  l_organization_id;
5212              l_new_dest_instance_rec.inv_subinventory_name        :=  l_subinventory_name;
5213              l_new_dest_instance_rec.inv_locator_id               :=  l_locator_id;
5214              l_new_dest_instance_rec.customer_view_flag           :=  'N';
5215              l_new_dest_instance_rec.merchant_view_flag           :=  'Y';
5216              l_new_dest_instance_rec.operational_status_code      :=  'NOT_USED';
5217              l_new_dest_instance_rec.object_version_number        :=  l_object_version_number;
5218              l_new_dest_instance_rec.active_start_date            :=  l_sysdate;
5219              l_new_dest_instance_rec.active_end_date              :=  NULL;
5220              l_new_dest_instance_rec.last_oe_order_line_id        :=  r_so_info.line_id;
5221 
5222              l_ext_attrib_values_tbl                              :=  csi_inv_trxs_pkg.init_ext_attrib_values_tbl;
5223              l_party_tbl                                          :=  csi_inv_trxs_pkg.init_party_tbl;
5224              l_account_tbl                                        :=  csi_inv_trxs_pkg.init_account_tbl;
5225              l_pricing_attrib_tbl                                 :=  csi_inv_trxs_pkg.init_pricing_attribs_tbl;
5226              l_org_assignments_tbl                                :=  csi_inv_trxs_pkg.init_org_assignments_tbl;
5227              l_asset_assignment_tbl                               :=  csi_inv_trxs_pkg.init_asset_assignment_tbl;
5228 
5229              IF (l_debug > 0) THEN
5230                 csi_t_gen_utility_pvt.add('Before Create Item Instance - Neg Qty');
5231                 csi_t_gen_utility_pvt.add('In Transit Order Line ID on Dest Rec: '||l_new_dest_instance_rec.last_oe_order_line_id);
5232              END IF;
5233 
5234              csi_item_instance_pub.create_item_instance(l_api_version,
5235                                                         l_commit,
5236                                                         l_init_msg_list,
5237                                                         l_validation_level,
5238                                                         l_new_dest_instance_rec,
5239                                                         l_ext_attrib_values_tbl,
5240                                                         l_party_tbl,
5241                                                         l_account_tbl,
5242                                                         l_pricing_attrib_tbl,
5243                                                         l_org_assignments_tbl,
5244                                                         l_asset_assignment_tbl,
5245                                                         l_txn_rec,
5246                                                         l_return_status,
5247                                                         l_msg_count,
5248                                                         l_msg_data);
5249 
5250              IF (l_debug > 0) THEN
5251                 csi_t_gen_utility_pvt.add('After Create Item Instance - Neg Qty');
5252              END IF;
5253              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
5254 
5255              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
5256                IF (l_debug > 0) THEN
5257                   csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.create_item_instance API '||l_msg_data);
5258                END IF;
5259                l_msg_index := 1;
5260                  WHILE l_msg_count > 0 loop
5261 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
5262 	           l_msg_index := l_msg_index + 1;
5263                    l_msg_count := l_msg_count - 1;
5264   	         END LOOP;
5265 	         RAISE fnd_api.g_exc_error;
5266              END IF;
5267 
5268            ELSIF l_dest_instance_header_tbl.count = 1 THEN -- Installed Base Destination Records Found
5269 
5270                l_update_dest_instance_rec                         :=  csi_inv_trxs_pkg.init_instance_update_rec;
5271                l_update_dest_instance_rec.instance_id             :=  l_dest_instance_header_tbl(i).instance_id;
5272                l_update_dest_instance_rec.quantity                :=  l_dest_instance_header_tbl(i).quantity + abs(l_mtl_item_tbl(j).primary_quantity);
5273                l_update_dest_instance_rec.active_end_date         :=  NULL;
5274                l_update_dest_instance_rec.object_version_number   :=  l_dest_instance_header_tbl(i).object_version_number;
5275                l_update_dest_instance_rec.last_oe_order_line_id   :=  r_so_info.line_id;
5276 
5277                l_party_tbl.delete;
5278                l_account_tbl.delete;
5279                l_pricing_attrib_tbl.delete;
5280                l_org_assignments_tbl.delete;
5284 
5281                l_asset_assignment_tbl.delete;
5282 
5283                l_update_dest_instance_rec.instance_status_id := nvl(csi_inv_trxs_pkg.get_default_status_id(l_txn_rec.transaction_type_id),r_id.instance_status_id);
5285                IF (l_debug > 0) THEN
5286                  csi_t_gen_utility_pvt.add('Instance Status Id: '||l_update_dest_instance_rec.instance_status_id);
5287                END IF;
5288 
5289                IF (l_debug > 0) THEN
5290                   csi_t_gen_utility_pvt.add('Before Update Item Instance');
5291                   csi_t_gen_utility_pvt.add('In Transit Order Line ID in Updated Instance: '||l_update_dest_instance_rec.last_oe_order_line_id);
5292                END IF;
5293 
5294                csi_item_instance_pub.update_item_instance(l_api_version,
5295                                                           l_commit,
5296                                                           l_init_msg_list,
5297                                                           l_validation_level,
5298                                                           l_update_dest_instance_rec,
5299                                                           l_ext_attrib_values_tbl,
5300                                                           l_party_tbl,
5301                                                           l_account_tbl,
5302                                                           l_pricing_attrib_tbl,
5303                                                           l_org_assignments_tbl,
5304                                                           l_asset_assignment_tbl,
5305                                                           l_txn_rec,
5306                                                           l_instance_id_lst,
5307                                                           l_return_status,
5308                                                           l_msg_count,
5309                                                           l_msg_data);
5310 
5311              l_upd_error_instance_id := NULL;
5312              l_upd_error_instance_id := l_update_dest_instance_rec.instance_id;
5313 
5314              IF (l_debug > 0) THEN
5315                 csi_t_gen_utility_pvt.add('After Update Item Instance');
5316                 csi_t_gen_utility_pvt.add('l_upd_error_instance_id is: '||l_upd_error_instance_id);
5317              END IF;
5318 
5319              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
5320              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
5321                IF (l_debug > 0) THEN
5322                   csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.c API '||l_msg_data);
5323                END IF;
5324                l_msg_index := 1;
5325                  WHILE l_msg_count > 0 loop
5326 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
5327 	           l_msg_index := l_msg_index + 1;
5328                 l_msg_count := l_msg_count - 1;
5329                  END LOOP;
5330 	         RAISE fnd_api.g_exc_error;
5331              END IF;
5332 
5333           ELSIF l_dest_instance_header_tbl.count > 1 THEN
5334             -- Multiple Instances were found so throw error
5335             IF (l_debug > 0) THEN
5336               csi_t_gen_utility_pvt.add('Multiple Instances were Found in Install Base-61');
5337             END IF;
5338             fnd_message.set_name('CSI','CSI_TXN_MULT_INST_FOUND');
5339             fnd_message.set_token('INV_ITEM_ID',l_mtl_item_tbl(j).inventory_item_id);
5340             fnd_message.set_token('SUBINV',l_mtl_item_tbl(j).subinventory_code);
5341             fnd_message.set_token('INV_ORG_ID',l_mtl_item_tbl(j).organization_id);
5342             fnd_message.set_token('LOCATOR',l_mtl_item_tbl(j).locator_id);
5343             l_error_message := fnd_message.get;
5344             RAISE fnd_api.g_exc_error;
5345 
5346            END IF;    -- End of Destination Record If
5347          END IF;      -- End of J Index Loop
5348 
5349        ELSIF (l_mtl_item_tbl(j).serial_number_control_code = 1 AND -- Ship
5350 	      l_sr_control = 5) OR -- Rec
5351              (l_mtl_item_tbl(j).serial_number_control_code = 1 AND -- Ship
5352 	      l_sr_control = 2) THEN -- Rec
5353 
5354           IF (l_debug > 0) THEN
5355                csi_t_gen_utility_pvt.add('Shipping is 1 and and Rec Serial Control is 2,5,6');
5356            END IF;
5357 
5358          l_12_loop := 1;
5359 
5360          IF (l_debug > 0) THEN
5361            csi_t_gen_utility_pvt.add('Setting l_12_loop: '||l_12_loop);
5362            csi_t_gen_utility_pvt.add('Serial Control at Shipping is 1,6 and Receiving is 2,5');
5363          END IF;
5364 
5365           IF j = 1 THEN -- Update Source Since its Non Serialized 1 Time
5366             IF (l_debug > 0) THEN
5367                csi_t_gen_utility_pvt.add('Serial Control at Shipping is 1,6 and Receiving is 2,5');
5368                csi_t_gen_utility_pvt.add('Update Source 1 time with Transaction Quantity');
5369                csi_t_gen_utility_pvt.add('Instance being updated: '||l_src_instance_header_tbl(i).instance_id);
5370             END IF;
5371 
5372            l_upd_src_dest_instance_rec                        :=  csi_inv_trxs_pkg.init_instance_update_rec;
5373            l_upd_src_dest_instance_rec.instance_id            :=  l_src_instance_header_tbl(i).instance_id;
5374            l_upd_src_dest_instance_rec.quantity               :=  l_src_instance_header_tbl(i).quantity - abs(l_mtl_item_tbl(j).primary_quantity);
5375            l_upd_src_dest_instance_rec.last_oe_order_line_id  :=  r_so_info.line_id;
5376            l_upd_src_dest_instance_rec.object_version_number  :=  l_src_instance_header_tbl(i).object_version_number;
5377 
5378            l_party_tbl.delete;
5379            l_account_tbl.delete;
5380            l_pricing_attrib_tbl.delete;
5381            l_org_assignments_tbl.delete;
5382            l_asset_assignment_tbl.delete;
5383 
5384            IF (l_debug > 0) THEN
5388            csi_item_instance_pub.update_item_instance(l_api_version,
5385               csi_t_gen_utility_pvt.add('Before Update Item Instance - 6');
5386            END IF;
5387 
5389                                                       l_commit,
5390                                                       l_init_msg_list,
5391                                                       l_validation_level,
5392                                                       l_upd_src_dest_instance_rec,
5393                                                       l_ext_attrib_values_tbl,
5394                                                       l_party_tbl,
5395                                                       l_account_tbl,
5396                                                       l_pricing_attrib_tbl,
5397                                                       l_org_assignments_tbl,
5398                                                       l_asset_assignment_tbl,
5399                                                       l_txn_rec,
5400                                                       l_instance_id_lst,
5401                                                       l_return_status,
5402                                                       l_msg_count,
5403                                                       l_msg_data);
5404 
5405            l_upd_error_instance_id := NULL;
5406            l_upd_error_instance_id := l_upd_src_dest_instance_rec.instance_id;
5407 
5408            IF (l_debug > 0) THEN
5409               csi_t_gen_utility_pvt.add('After Update Item Instance - 7');
5410               csi_t_gen_utility_pvt.add('l_upd_error_instance_id is: '||l_upd_error_instance_id);
5411            END IF;
5412 
5413            -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
5414            IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
5415              IF (l_debug > 0) THEN
5416                 csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.update_item_instance API '||l_msg_data);
5417              END IF;
5418              l_msg_index := 1;
5419                WHILE l_msg_count > 0 loop
5420 	         l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
5421 	         l_msg_index := l_msg_index + 1;
5422                  l_msg_count := l_msg_count - 1;
5423   	       END LOOP;
5424 	       RAISE fnd_api.g_exc_error;
5425            END IF;
5426          END IF; -- End of J = 1 If to update Source 1 time
5427 
5428          -- Now Query for Dest Serialized Instances and Update (Unexpire)/ Create Instances
5429              l_instance_query_rec                               :=  csi_inv_trxs_pkg.init_instance_query_rec;
5430              l_instance_query_rec.inventory_item_id             :=  l_mtl_item_tbl(j).inventory_item_id;
5431              --l_instance_query_rec.inventory_revision            :=  l_mtl_item_tbl(j).revision;
5432              --l_instance_query_rec.lot_number                    :=  l_mtl_item_tbl(j).lot_number;
5433              l_instance_query_rec.serial_number                 :=  l_mtl_item_tbl(j).serial_number;
5434              --l_instance_query_rec.instance_usage_code           :=  l_in_inventory;
5435              --l_instance_query_rec.inv_subinventory_name         :=  l_mtl_item_tbl(j).transfer_subinventory;
5436              --l_instance_query_rec.inv_organization_id           :=  l_mtl_item_tbl(j).transfer_organization_id;
5437              --l_instance_query_rec.inv_locator_id                :=  l_mtl_item_tbl(j).transfer_locator_id;
5438              l_instance_usage_code                              :=  l_in_inventory; --l_instance_query_rec.instance_usage_code;
5439              l_subinventory_name                                :=  l_mtl_item_tbl(j).transfer_subinventory;
5440              l_organization_id                                  :=  l_mtl_item_tbl(j).transfer_organization_id;
5441              l_locator_id                                       :=  l_mtl_item_tbl(j).transfer_locator_id;
5442 
5443            IF (l_debug > 0) THEN
5444               csi_t_gen_utility_pvt.add('Before Get Dest Item Instance - 8');
5445            END IF;
5446 
5447            csi_item_instance_pub.get_item_instances(l_api_version,
5448                                                     l_commit,
5449                                                     l_init_msg_list,
5450                                                     l_validation_level,
5451                                                     l_instance_query_rec,
5452                                                     l_party_query_rec,
5453                                                     l_account_query_rec,
5454                                                     l_transaction_id,
5455                                                     l_resolve_id_columns,
5456                                                     l_inactive_instance_only,
5457                                                     l_dest_instance_header_tbl,
5458                                                     l_return_status,
5459                                                     l_msg_count,
5460                                                     l_msg_data);
5461 
5462            IF (l_debug > 0) THEN
5463               csi_t_gen_utility_pvt.add('After Get Item Instance - 9');
5464            END IF;
5465 
5466            l_tbl_count := 0;
5467            l_tbl_count :=  l_dest_instance_header_tbl.count;
5468            IF (l_debug > 0) THEN
5469               csi_t_gen_utility_pvt.add('Source Records Found: '||l_tbl_count);
5470            END IF;
5471 
5472            -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
5473            IF NOT l_return_status = l_fnd_success then
5474              IF (l_debug > 0) THEN
5475                 csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.get_item_instance API '||l_msg_data);
5476              END IF;
5477              l_msg_index := 1;
5481                l_msg_count := l_msg_count - 1;
5478 	         WHILE l_msg_count > 0 loop
5479 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
5480 	           l_msg_index := l_msg_index + 1;
5482   	         END LOOP;
5483 	         RAISE fnd_api.g_exc_error;
5484            END IF;
5485 
5486            IF l_dest_instance_header_tbl.count = 0 THEN  -- Installed Base Destination Records are not found so create a new record
5487 
5488              l_new_dest_instance_rec                              :=  csi_inv_trxs_pkg.init_instance_create_rec;
5489              l_new_dest_instance_rec.inventory_item_id            :=  l_mtl_item_tbl(j).inventory_item_id;
5490              l_new_dest_instance_rec.inventory_revision           :=  l_mtl_item_tbl(j).revision;
5491              l_new_dest_instance_rec.inv_master_organization_id   :=  l_master_organization_id;
5492              l_new_dest_instance_rec.mfg_serial_number_flag       :=  'Y';
5493              l_new_dest_instance_rec.serial_number                :=  l_mtl_item_tbl(j).serial_number;
5494              l_new_dest_instance_rec.lot_number                   :=  l_mtl_item_tbl(j).lot_number;
5495              l_new_dest_instance_rec.quantity                     :=  1;
5496              l_new_dest_instance_rec.unit_of_measure              :=  l_mtl_item_tbl(j).transaction_uom;
5497              l_new_dest_instance_rec.location_id                  :=  nvl(r_loc_ids.subinv_location_id,r_loc_ids.hr_location_id);
5498              l_new_dest_instance_rec.location_type_code           :=  csi_inv_trxs_pkg.get_location_type_code('Inventory');
5499              l_new_dest_instance_rec.instance_usage_code          :=  l_instance_usage_code;
5500              l_new_dest_instance_rec.inv_organization_id          :=  l_organization_id;
5501              l_new_dest_instance_rec.vld_organization_id          :=  l_organization_id;
5502              l_new_dest_instance_rec.inv_subinventory_name        :=  l_subinventory_name;
5503              l_new_dest_instance_rec.inv_locator_id               :=  l_locator_id;
5504              l_new_dest_instance_rec.customer_view_flag           :=  'N';
5505              l_new_dest_instance_rec.merchant_view_flag           :=  'Y';
5506              l_new_dest_instance_rec.operational_status_code      :=  'NOT_USED';
5507              l_new_dest_instance_rec.object_version_number        :=  l_object_version_number;
5508              l_new_dest_instance_rec.active_start_date            :=  l_sysdate;
5509              l_new_dest_instance_rec.active_end_date              :=  NULL;
5510              l_new_dest_instance_rec.last_oe_order_line_id        :=  r_so_info.line_id;
5511 
5512              l_ext_attrib_values_tbl                              :=  csi_inv_trxs_pkg.init_ext_attrib_values_tbl;
5513              l_party_tbl                                          :=  csi_inv_trxs_pkg.init_party_tbl;
5514              l_account_tbl                                        :=  csi_inv_trxs_pkg.init_account_tbl;
5515              l_pricing_attrib_tbl                                 :=  csi_inv_trxs_pkg.init_pricing_attribs_tbl;
5516              l_org_assignments_tbl                                :=  csi_inv_trxs_pkg.init_org_assignments_tbl;
5517              l_asset_assignment_tbl                               :=  csi_inv_trxs_pkg.init_asset_assignment_tbl;
5518 
5519              l_new_dest_instance_rec.instance_status_id := nvl(csi_inv_trxs_pkg.get_default_status_id(l_txn_rec.transaction_type_id),r_id.instance_status_id);
5520 
5521              IF (l_debug > 0) THEN
5522                csi_t_gen_utility_pvt.add('Instance Status Id: '||l_new_dest_instance_rec.instance_status_id);
5523              END IF;
5524 
5525              IF (l_debug > 0) THEN
5526                 csi_t_gen_utility_pvt.add('Before Create Item Instance - 10');
5527                 csi_t_gen_utility_pvt.add('In Transit Order Line ID on Dest Rec: '||l_new_dest_instance_rec.last_oe_order_line_id);
5528              END IF;
5529 
5530              csi_item_instance_pub.create_item_instance(l_api_version,
5531                                                         l_commit,
5532                                                         l_init_msg_list,
5533                                                         l_validation_level,
5534                                                         l_new_dest_instance_rec,
5535                                                         l_ext_attrib_values_tbl,
5536                                                         l_party_tbl,
5537                                                         l_account_tbl,
5538                                                         l_pricing_attrib_tbl,
5539                                                         l_org_assignments_tbl,
5540                                                         l_asset_assignment_tbl,
5541                                                         l_txn_rec,
5542                                                         l_return_status,
5543                                                         l_msg_count,
5544                                                         l_msg_data);
5545 
5546              IF (l_debug > 0) THEN
5547                 csi_t_gen_utility_pvt.add('After Create Item Instance - 11');
5548              END IF;
5549              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
5550 
5551              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
5552                IF (l_debug > 0) THEN
5553                   csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.create_item_instance API '||l_msg_data);
5554                END IF;
5555                l_msg_index := 1;
5556                  WHILE l_msg_count > 0 loop
5557 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
5558 	           l_msg_index := l_msg_index + 1;
5559                    l_msg_count := l_msg_count - 1;
5560   	         END LOOP;
5561 	         RAISE fnd_api.g_exc_error;
5562              END IF;
5566 
5563 
5564            ELSIF l_dest_instance_header_tbl.count > 0 THEN -- Installed Base Destination Records Found
5565 
5567             IF l_dest_instance_header_tbl(i).instance_usage_code in (l_in_inventory,l_in_relationship) THEN
5568 
5569               IF l_dest_instance_header_tbl(i).instance_usage_code = l_in_relationship THEN
5570                 csi_t_gen_utility_pvt.add('Check and Break Relationship for Instance :'||l_dest_instance_header_tbl(i).instance_id);
5571 
5572                 csi_process_txn_pvt.check_and_break_relation(l_dest_instance_header_tbl(i).instance_id,
5573                                                              l_txn_rec,
5574                                                              l_return_status);
5575 
5576                 IF NOT l_return_status = l_fnd_success then
5577                   csi_t_gen_utility_pvt.add('You encountered an error in the se_inv_trxs_pkg.check_and_break_relation');
5578                   l_error_message := csi_t_gen_utility_pvt.dump_error_stack;
5579                   RAISE fnd_api.g_exc_error;
5580                 END IF;
5581 
5582                 csi_t_gen_utility_pvt.add('Object Version originally from instance: '||l_dest_instance_header_tbl(i).object_version_number);
5583 
5584                 OPEN c_obj_version (l_dest_instance_header_tbl(i).instance_id);
5585                 FETCH c_obj_version into l_dest_instance_header_tbl(i).object_version_number;
5586                 CLOSE c_obj_version;
5587 
5588                 csi_t_gen_utility_pvt.add('Current Object Version after check and break :'||l_dest_instance_header_tbl(i).object_version_number);
5589 
5590               END IF; -- Check and Break
5591 
5592 
5593                l_update_dest_instance_rec                         :=  csi_inv_trxs_pkg.init_instance_update_rec;
5594                l_update_dest_instance_rec.instance_id             :=  l_dest_instance_header_tbl(i).instance_id;
5595                l_update_dest_instance_rec.quantity                :=  1;
5596                l_update_dest_instance_rec.active_end_date         :=  NULL;
5597                l_update_dest_instance_rec.object_version_number   :=  l_dest_instance_header_tbl(i).object_version_number;
5598                l_update_dest_instance_rec.last_oe_order_line_id   :=  r_so_info.line_id;
5599 
5600                l_party_tbl.delete;
5601                l_account_tbl.delete;
5602                l_pricing_attrib_tbl.delete;
5603                l_org_assignments_tbl.delete;
5604                l_asset_assignment_tbl.delete;
5605 
5606                l_update_dest_instance_rec.instance_status_id := nvl(csi_inv_trxs_pkg.get_default_status_id(l_txn_rec.transaction_type_id),r_id.instance_status_id);
5607 
5608                IF (l_debug > 0) THEN
5609                  csi_t_gen_utility_pvt.add('Instance Status Id: '||l_update_dest_instance_rec.instance_status_id);
5610                END IF;
5611 
5612                IF (l_debug > 0) THEN
5613                   csi_t_gen_utility_pvt.add('Before Update Item Instance - 13');
5614                   csi_t_gen_utility_pvt.add('In Transit Order Line ID in Updated Instance: '||l_update_dest_instance_rec.last_oe_order_line_id);
5615                END IF;
5616 
5617                csi_item_instance_pub.update_item_instance(l_api_version,
5618                                                           l_commit,
5619                                                           l_init_msg_list,
5620                                                           l_validation_level,
5621                                                           l_update_dest_instance_rec,
5622                                                           l_ext_attrib_values_tbl,
5623                                                           l_party_tbl,
5624                                                           l_account_tbl,
5625                                                           l_pricing_attrib_tbl,
5626                                                           l_org_assignments_tbl,
5627                                                           l_asset_assignment_tbl,
5628                                                           l_txn_rec,
5629                                                           l_instance_id_lst,
5630                                                           l_return_status,
5631                                                           l_msg_count,
5632                                                           l_msg_data);
5633              l_upd_error_instance_id := NULL;
5634              l_upd_error_instance_id := l_update_dest_instance_rec.instance_id;
5635 
5636              IF (l_debug > 0) THEN
5637                 csi_t_gen_utility_pvt.add('After Update Item Instance - 14');
5638                 csi_t_gen_utility_pvt.add('l_upd_error_instance_id is: '||l_upd_error_instance_id);
5639              END IF;
5640 
5641              -- Check for any errors and add them to the message stack to pass out to be put into the error log table.
5642              IF NOT l_return_status in (l_fnd_success,l_fnd_warning) then
5643                IF (l_debug > 0) THEN
5644                   csi_t_gen_utility_pvt.add('You encountered an error in the csi_item_instance_pub.c API '||l_msg_data);
5645                END IF;
5646                l_msg_index := 1;
5647                  WHILE l_msg_count > 0 loop
5648 	           l_error_message := l_error_message || fnd_msg_pub.get(l_msg_index,FND_API.G_FALSE);
5649 	           l_msg_index := l_msg_index + 1;
5650                    l_msg_count := l_msg_count - 1;
5651                  END LOOP;
5652 	         RAISE fnd_api.g_exc_error;
5653              END IF;
5654 
5655            ELSE -- No Serialized Instances with In Inventory or In Relationship Exist
5656 
5657              IF (l_debug > 0) THEN
5658                  csi_t_gen_utility_pvt.add('No Records were found in Install Base-5');
5659              END IF;
5660 
5664              fnd_message.set_token('ORG_ID',l_mtl_item_tbl(j).organization_id);
5661              fnd_message.set_name('CSI','CSI_IB_RECORD_NOTFOUND');
5662              fnd_message.set_token('ITEM',l_mtl_item_tbl(j).inventory_item_id);
5663              fnd_message.set_token('SUBINVENTORY',l_mtl_item_tbl(j).subinventory_code);
5665              l_error_message := fnd_message.get;
5666              RAISE fnd_api.g_exc_error;
5667 
5668            END IF; -- End of inv or in rel IF
5669 
5670            ELSIF l_dest_instance_header_tbl.count > 1 THEN
5671              -- Multiple Instances were found so throw error
5672              IF (l_debug > 0) THEN
5673                csi_t_gen_utility_pvt.add('Multiple Instances were Found in Install Base-62');
5674              END IF;
5675              fnd_message.set_name('CSI','CSI_TXN_MULT_INST_FOUND');
5676              fnd_message.set_token('INV_ITEM_ID',l_mtl_item_tbl(j).inventory_item_id);
5677              fnd_message.set_token('SUBINV',l_mtl_item_tbl(j).subinventory_code);
5678              fnd_message.set_token('INV_ORG_ID',l_mtl_item_tbl(j).organization_id);
5679              fnd_message.set_token('LOCATOR',l_mtl_item_tbl(j).locator_id);
5680              l_error_message := fnd_message.get;
5681              RAISE fnd_api.g_exc_error;
5682 
5683            END IF;    -- End of Destination Record If
5684 
5685 	  END IF;  -- End of Serial Control IF
5686 
5687        IF l_66_flag = 1 THEN
5688          IF (l_debug > 0) THEN
5689            csi_t_gen_utility_pvt.add('Exiting Loop :'||l_66_flag);
5690            csi_t_gen_utility_pvt.add('Ship Control :'||l_mtl_item_tbl(j).serial_number_control_code);
5691            csi_t_gen_utility_pvt.add('Rec Control :'||l_sr_control);
5692          END IF;
5693          EXIT;
5694        END IF;
5695 
5696     END LOOP;   -- End of main For Inv Loop
5697 
5698     IF (l_debug > 0) THEN
5699        csi_t_gen_utility_pvt.add('End time: '||to_char(sysdate,'DD-MON-YYYY HH24:MI:SS'));
5700        csi_t_gen_utility_pvt.add('******End of csi_inv_iso_pkg.iso_direct Transaction******');
5701     END IF;
5702 
5703     EXCEPTION
5704      WHEN fnd_api.g_exc_error THEN
5705        IF (l_debug > 0) THEN
5706           csi_t_gen_utility_pvt.add('You have encountered a "fnd_api.g_exc_error" exception in the Direct ISO Transaction');
5707        END IF;
5708        x_return_status := l_fnd_error;
5709 
5710        IF l_mtl_item_tbl.count > 0 THEN
5711          x_trx_error_rec.serial_number := l_mtl_item_tbl(j).serial_number;
5712          x_trx_error_rec.lot_number := l_mtl_item_tbl(j).lot_number;
5713          x_trx_error_rec.instance_id := l_upd_error_instance_id;
5714          x_trx_error_rec.inventory_item_id := l_mtl_item_tbl(j).inventory_item_id;
5715          x_trx_error_rec.src_serial_num_ctrl_code := l_mtl_item_tbl(j).serial_number_control_code;
5716          x_trx_error_rec.src_location_ctrl_code := l_mtl_item_tbl(j).location_control_code;
5717          x_trx_error_rec.src_lot_ctrl_code := l_mtl_item_tbl(j).lot_control_code;
5718          x_trx_error_rec.src_rev_qty_ctrl_code := l_mtl_item_tbl(j).revision_qty_control_code;
5719          x_trx_error_rec.dst_serial_num_ctrl_code := r_item_control.serial_number_control_code;
5720          x_trx_error_rec.dst_location_ctrl_code := r_item_control.location_control_code;
5721          x_trx_error_rec.dst_lot_ctrl_code := r_item_control.lot_control_code;
5722          x_trx_error_rec.dst_rev_qty_ctrl_code := r_item_control.revision_qty_control_code;
5723          x_trx_error_rec.comms_nl_trackable_flag := l_mtl_item_tbl(j).comms_nl_trackable_flag;
5724          x_trx_error_rec.transaction_error_date := l_sysdate ;
5725        END IF;
5726 
5727        x_trx_error_rec.error_text := l_error_message;
5728        x_trx_error_rec.transaction_id       := NULL;
5729        x_trx_error_rec.source_type          := 'CSIINTDS';
5730        x_trx_error_rec.source_id            := p_transaction_id;
5731        x_trx_error_rec.processed_flag       := csi_inv_trxs_pkg.g_txn_error;
5732        x_trx_error_rec.transaction_type_id  := csi_inv_trxs_pkg.get_txn_type_id(l_trans_type_code,l_trans_app_code);
5733        x_trx_error_rec.inv_material_transaction_id  := p_transaction_id;
5734        x_trx_error_rec.error_stage          := csi_inv_trxs_pkg.g_ib_update;
5735 
5736      WHEN others THEN
5737         l_sql_error := SQLERRM;
5738         IF (l_debug > 0) THEN
5739            csi_t_gen_utility_pvt.add('You have encountered a "when others" exception in the Direct ISO Transaction');
5740            csi_t_gen_utility_pvt.add('SQL Error: '||l_sql_error);
5741         END IF;
5742         fnd_message.set_name('CSI','CSI_UNEXP_SQL_ERROR');
5743         fnd_message.set_token('API_NAME',l_api_name);
5744         fnd_message.set_token('SQL_ERROR',SQLERRM);
5745         x_return_status := l_fnd_unexpected;
5746 
5747         IF l_mtl_item_tbl.count > 0 THEN
5748           x_trx_error_rec.serial_number := l_mtl_item_tbl(j).serial_number;
5749           x_trx_error_rec.lot_number := l_mtl_item_tbl(j).lot_number;
5750           x_trx_error_rec.instance_id := l_upd_error_instance_id;
5751           x_trx_error_rec.inventory_item_id := l_mtl_item_tbl(j).inventory_item_id;
5752           x_trx_error_rec.src_serial_num_ctrl_code := l_mtl_item_tbl(j).serial_number_control_code;
5753           x_trx_error_rec.src_location_ctrl_code := l_mtl_item_tbl(j).location_control_code;
5754           x_trx_error_rec.src_lot_ctrl_code := l_mtl_item_tbl(j).lot_control_code;
5755           x_trx_error_rec.src_rev_qty_ctrl_code := l_mtl_item_tbl(j).revision_qty_control_code;
5756           x_trx_error_rec.dst_serial_num_ctrl_code := r_item_control.serial_number_control_code;
5757           x_trx_error_rec.dst_location_ctrl_code := r_item_control.location_control_code;
5758           x_trx_error_rec.dst_lot_ctrl_code := r_item_control.lot_control_code;
5759           x_trx_error_rec.dst_rev_qty_ctrl_code := r_item_control.revision_qty_control_code;
5760           x_trx_error_rec.comms_nl_trackable_flag := l_mtl_item_tbl(j).comms_nl_trackable_flag;
5761           x_trx_error_rec.transaction_error_date := l_sysdate ;
5762         END IF;
5763 
5764         x_trx_error_rec.error_text := fnd_message.get;
5765         x_trx_error_rec.transaction_id       := NULL;
5766         x_trx_error_rec.source_type          := 'CSIINTDS';
5767         x_trx_error_rec.source_id            := p_transaction_id;
5768         x_trx_error_rec.processed_flag       := csi_inv_trxs_pkg.g_txn_error;
5769         x_trx_error_rec.transaction_type_id  := csi_inv_trxs_pkg.get_txn_type_id(l_trans_type_code,l_trans_app_code);
5770         x_trx_error_rec.inv_material_transaction_id  := p_transaction_id;
5771         x_trx_error_rec.error_stage          := csi_inv_trxs_pkg.g_ib_update;
5772 
5773   END iso_direct;
5774 END csi_inv_iso_pkg;