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