DBA Data[Home] [Help]

PACKAGE BODY: APPS.CSI_DIAGNOSTICS_PKG

Source


1 PACKAGE BODY csi_diagnostics_pkg as
2 /* $Header: csidiagb.pls 120.20.12020000.3 2013/03/20 12:03:41 mvaradam ship $ */
3 
4   g_no_lot constant number := 1;
5   g_lot    constant number := 2;
6 
7   CURSOR error_cur is
8     SELECT /*+ parallel(cte) */
9            cte.inv_material_transaction_id mtl_txn_id,
10            cte.transaction_error_id        txn_error_id
11     FROM   csi_txn_errors cte
12     WHERE  cte.processed_flag in ('E', 'R')
13     AND    cte.inv_material_transaction_id is not null;
14 
15   -- this CURSOR returns the serial numbers that are in the transaction
16   CURSOR srl_cur(
17     p_mtl_txn_id     in number)
18   IS
19     SELECT mut.serial_number           serial_number,
20            mut.inventory_item_id       item_id,
21            mut.organization_id         organization_id
22     FROM   mtl_unit_transactions mut
23     WHERE  mut.transaction_id    = p_mtl_txn_id
24     UNION
25     SELECT mut.serial_number           serial_number,
26            mut.inventory_item_id       item_id,
27            mut.organization_id         organization_id
28     FROM   mtl_transaction_lot_numbers mtln,
29            mtl_unit_transactions       mut
30     WHERE  mtln.transaction_id   = p_mtl_txn_id
31     AND    mut.transaction_id    = mtln.serial_transaction_id;
32 
33   CURSOR all_txn_cur(
34     p_serial_number  in varchar2,
35     p_item_id        in number)
36   IS
37     SELECT mmt.creation_date               mtl_creation_date,
38            mmt.transaction_id              mtl_txn_id,
39            mmt.transaction_date            mtl_txn_date,
40            mmt.inventory_item_id           item_id,
41            mmt.organization_id             organization_id,
42            mmt.transaction_type_id         mtl_type_id,
43            mtt.transaction_type_name       mtl_txn_name,
44            mmt.transaction_action_id       mtl_action_id,
45            mmt.transaction_source_type_id  mtl_source_type_id,
46            mmt.transaction_source_id       mtl_source_id,
47            mmt.trx_source_line_id          mtl_source_line_id,
48            mmt.transaction_quantity        mtl_txn_qty,
49            mtt.type_class                  mtl_type_class,
50            mmt.transfer_transaction_id     mtl_xfer_txn_id,
51            mmt.revision                    mtl_revision,
52            to_char(null)                   lot_number,
53            to_char(mmt.transaction_date,'dd-mm-yy hh24:mi:ss') mtl_txn_char_date
54     FROM   mtl_unit_transactions     mut,
55            mtl_material_transactions mmt,
56            mtl_transaction_types     mtt
57     WHERE  mut.serial_number       = p_serial_number
58     AND    mut.inventory_item_id   = p_item_id
59     AND    mmt.transaction_id      = mut.transaction_id
60     AND    mtt.transaction_type_id = mmt.transaction_type_id
61     UNION
62     SELECT mmt.creation_date               mtl_creation_date,
63            mmt.transaction_id              mtl_txn_id,
64            mmt.transaction_date            mtl_txn_date,
65            mmt.inventory_item_id           item_id,
66            mmt.organization_id             organization_id,
67            mmt.transaction_type_id         mtl_type_id,
68            mtt.transaction_type_name       mtl_txn_name,
69            mmt.transaction_action_id       mtl_action_id,
70            mmt.transaction_source_type_id  mtl_source_type_id,
71            mmt.transaction_source_id       mtl_source_id,
72            mmt.trx_source_line_id          mtl_source_line_id,
73            mmt.transaction_quantity        mtl_txn_qty,
74            mtt.type_class                  mtl_type_class,
75            mmt.transfer_transaction_id     mtl_xfer_txn_id,
76            mmt.revision                    mtl_revision,
77            mtln.lot_number                 lot_number,
78            to_char(mmt.transaction_date,'dd-mm-yy hh24:mi:ss') mtl_txn_char_date
79     FROM   mtl_unit_transactions       mut,
80            mtl_transaction_lot_numbers mtln,
81            mtl_material_transactions   mmt,
82            mtl_transaction_types       mtt
83     WHERE  mut.serial_number          = p_serial_number
84     AND    mut.inventory_item_id      = p_item_id
85     AND    mtln.organization_id       = mut.organization_id
86     AND    mtln.transaction_date      = mut.transaction_date
87     AND    mtln.serial_transaction_id = mut.transaction_id
88     AND    mmt.transaction_id         = mtln.transaction_id
89     AND    mtt.transaction_type_id    = mmt.transaction_type_id
90     ORDER BY 1 desc, 2 desc;
91 
92   CURSOR inv_cur(p_mtl_txn_id in number) IS
93     SELECT mmt.inventory_item_id        item_id,
94            mmt.organization_id          organization_id,
95            mmt.transfer_organization_id xfer_organization_id,
96            mmt.subinventory_code        subinv_code,
97            mmt.locator_id               locator_id,
98            mmt.revision                 revision,
99            abs(mmt.primary_quantity)    quantity,
100            mmt.transaction_date         mtl_txn_date,
101            mmt.transaction_id           mtl_txn_id,
102            mmt.transaction_action_id    mtl_action_id,
103            mmt.trx_source_line_id       trx_source_line_id,
104            mmt.transaction_source_id    mtl_source_id,
105            mmt.source_project_id        source_project_id,
106            mmt.source_task_id           source_task_id
107     FROM   mtl_material_transactions mmt
108     WHERE  mmt.transaction_id = p_mtl_txn_id;
109 
110   CURSOR from_sixfer_cur(
111     p_mtl_txn_id       in number,
112     p_mtl_xfer_txn_id  in number)
113   IS
114     SELECT mmt.inventory_item_id      item_id,
115            mmt.organization_id        organization_id,
116            mmt.subinventory_code      subinv_code,
117            mmt.locator_id             locator_id,
118            mmt.revision               revision,
119            to_char(null)              lot_number,
120            abs(mmt.primary_quantity)  quantity,
121            mmt.transaction_date       mtl_txn_date,
122            mmt.transaction_id         mtl_txn_id,
123            mmt.trx_source_line_id     trx_source_line_id
124     FROM   mtl_material_transactions mmt
125     WHERE  (mmt.transaction_id = p_mtl_txn_id
126              OR
127             mmt.transaction_id = p_mtl_xfer_txn_id)
128     AND    mmt.transaction_quantity < 0
129     UNION
130     SELECT mmt.inventory_item_id      item_id,
131            mmt.organization_id        organization_id,
132            mmt.subinventory_code      subinv_code,
133            mmt.locator_id             locator_id,
134            mmt.revision               revision,
135            mtln.lot_number            lot_number,
136            abs(mtln.primary_quantity) quantity,
137            mmt.transaction_date       mtl_txn_date,
138            mmt.transaction_id         mtl_txn_id,
139            mmt.trx_source_line_id     trx_source_line_id
140     FROM   mtl_material_transactions   mmt,
141            mtl_transaction_lot_numbers mtln
142     WHERE  (mmt.transaction_id  = p_mtl_txn_id
143             OR
144             mmt.transaction_id  = p_mtl_xfer_txn_id)
145     AND    mmt.transaction_quantity < 0
146     AND    mtln.transaction_id  = mmt.transaction_id;
147 
148   --
149   PROCEDURE log(
150     p_message       in varchar2)
151   IS
152   BEGIN
153 
154     fnd_file.put_line(fnd_file.log, p_message);
155 
156     csi_t_gen_utility_pvt.g_debug_level := 10;
157 
158     csi_t_gen_utility_pvt.build_file_name(
159       p_file_segment1 => 'csisynclog',
160       p_file_segment2 => to_char(sysdate, 'mmddyy'));
161 
162     csi_t_gen_utility_pvt.add(p_message);
163 
164     csi_t_gen_utility_pvt.g_debug_level := 0;
165 
166   END log;
167 
168   --
169   PROCEDURE out(
170     p_message       in varchar2)
171   IS
172   BEGIN
173 
174     fnd_file.put_line(fnd_file.output, p_message);
175 
176     csi_t_gen_utility_pvt.g_debug_level := 10;
177 
178     csi_t_gen_utility_pvt.build_file_name(
179       p_file_segment1 => 'csisyncout',
180       p_file_segment2 => to_char(sysdate, 'mmddyy'));
181 
182     csi_t_gen_utility_pvt.add(p_message);
183 
184     csi_t_gen_utility_pvt.g_debug_level := 0;
185 
186   END out;
187 
188   --
189   FUNCTION date_time_stamp RETURN varchar2
190   IS
191     l_date_time_stamp varchar2(30);
192   BEGIN
193 
194     SELECT to_char(sysdate,'DD-MON-YYYY HH24:MI:SS : ')
195     INTO   l_date_time_stamp
196     FROM   sys.dual;
197 
198     RETURN l_date_time_stamp;
199   END date_time_stamp;
200 
201   --
202   PROCEDURE debug_off IS
203   BEGIN
204     fnd_profile.put('CSI_DEBUG_LEVEL',0);
205     fnd_profile.put('CSE_DEBUG_OPTION','N');
206     fnd_profile.put('OKS_DEBUG','N');
207     csi_t_gen_utility_pvt.g_debug_level := 0;
208   END debug_off;
209 
210   --
211   PROCEDURE stack_message(
212     p_message       in varchar2)
213   IS
214   BEGIN
215     fnd_msg_pub.initialize;
216     fnd_message.set_name('FND','FND_GENERIC_MESSAGE');
217     fnd_message.set_token('MESSAGE',p_message);
218     fnd_msg_pub.add;
219   END stack_message;
220   --
221   PROCEDURE Update_Lookup(p_routine_name in varchar2) IS
222      l_type        VARCHAR2(30) := 'CSI_CORRECTION_ROUTINES';
223   BEGIN
224      UPDATE FND_LOOKUP_VALUES
225      SET enabled_flag = 'N',
226          last_updated_by = -1,
227          last_update_date = sysdate
228      WHERE lookup_type = l_type
229      AND   lookup_code = p_routine_name;
230   END Update_Lookup;
231   --
232   FUNCTION correction_txn_type_id RETURN NUMBER
233   IS
234     l_txn_type_id  number;
235   BEGIN
236 
237     BEGIN
238       SELECT transaction_type_id
239       INTO   l_txn_type_id
240       FROM   csi_txn_types
241       WHERE  source_transaction_type = 'DATA_CORRECTION';
242     EXCEPTION
243       WHEN no_data_found THEN
244         l_txn_type_id := 2;
245     END;
246 
247     RETURN l_txn_type_id;
248 
249   END correction_txn_type_id;
250 
251   PROCEDURE get_lot_number(
252     p_lot_code        IN  number,
253     p_mtl_txn_id      IN  number,
254     p_serial_number   IN  varchar2,
255     x_lot_number      OUT nocopy varchar2)
256   IS
257     CURSOR lot_cur(cp_mtl_txn_id IN number, cp_serial_number IN varchar2) IS
258       SELECT mtln.lot_number
259       FROM   mtl_transaction_lot_numbers mtln,
260              mtl_unit_transactions       mut
261       WHERE  mtln.transaction_id       = cp_mtl_txn_id
262       AND    mut.transaction_id        = mtln.serial_transaction_id
263       AND    mut.serial_number         = cp_serial_number;
264   BEGIN
265 
266     IF p_lot_code = 1 THEN
267      x_lot_number := fnd_api.g_miss_char;
268     END IF;
269 
270     IF p_lot_code = 2 THEN
271       x_lot_number := fnd_api.g_miss_char;
272       FOR lot_rec IN lot_cur(p_mtl_txn_id, p_serial_number)
273       LOOP
274         x_lot_number := lot_rec.lot_number;
275       END LOOP;
276     END IF;
277 
278   END get_lot_number;
279 
280   --
281   PROCEDURE Build_Inst_Rec_of_Table
282      (
283        p_inst_tbl           IN      csi_datastructures_pub.instance_tbl
284       ,p_inst_rec_tab       IN OUT NOCOPY   csi_diagnostics_pkg.instance_rec_tab
285       ,p_inst_hist_tbl      IN OUT NOCOPY csi_diagnostics_pkg.T_NUM
286      ) IS
287   BEGIN
288      FOR i in p_inst_tbl.FIRST .. p_inst_tbl.LAST LOOP
289         select CSI_ITEM_INSTANCES_H_S.nextval
290         into p_inst_hist_tbl(i) from dual;
291         --
292 	p_inst_rec_tab.INSTANCE_ID(i)                := p_inst_tbl(i).INSTANCE_ID;
293 	p_inst_rec_tab.INSTANCE_NUMBER(i)            := p_inst_tbl(i).INSTANCE_NUMBER;
294 	p_inst_rec_tab.EXTERNAL_REFERENCE(i)         := p_inst_tbl(i).EXTERNAL_REFERENCE;
295 	p_inst_rec_tab.INVENTORY_ITEM_ID(i)          := p_inst_tbl(i).INVENTORY_ITEM_ID;
296 	p_inst_rec_tab.VLD_ORGANIZATION_ID(i)        := p_inst_tbl(i).VLD_ORGANIZATION_ID;
297 	p_inst_rec_tab.INVENTORY_REVISION(i)         := p_inst_tbl(i).INVENTORY_REVISION;
298 	p_inst_rec_tab.INV_MASTER_ORGANIZATION_ID(i) :=	p_inst_tbl(i).INV_MASTER_ORGANIZATION_ID;
299 	p_inst_rec_tab.SERIAL_NUMBER(i)              := p_inst_tbl(i).SERIAL_NUMBER;
300 	p_inst_rec_tab.MFG_SERIAL_NUMBER_FLAG(i)     :=	p_inst_tbl(i).MFG_SERIAL_NUMBER_FLAG;
301 	p_inst_rec_tab.LOT_NUMBER(i)                 := p_inst_tbl(i).LOT_NUMBER;
302 	p_inst_rec_tab.QUANTITY(i)                   := p_inst_tbl(i).QUANTITY;
303 	p_inst_rec_tab.UNIT_OF_MEASURE(i)       :=	p_inst_tbl(i).UNIT_OF_MEASURE;
304 	p_inst_rec_tab.ACCOUNTING_CLASS_CODE(i)       :=	p_inst_tbl(i).ACCOUNTING_CLASS_CODE;
305 	p_inst_rec_tab.INSTANCE_CONDITION_ID(i)       :=	p_inst_tbl(i).INSTANCE_CONDITION_ID;
306 	p_inst_rec_tab.INSTANCE_STATUS_ID(i)       :=	p_inst_tbl(i).INSTANCE_STATUS_ID;
307 	p_inst_rec_tab.CUSTOMER_VIEW_FLAG(i)       :=	p_inst_tbl(i).CUSTOMER_VIEW_FLAG;
308 	p_inst_rec_tab.MERCHANT_VIEW_FLAG(i)       :=	p_inst_tbl(i).MERCHANT_VIEW_FLAG;
309 	p_inst_rec_tab.SELLABLE_FLAG(i)       :=	p_inst_tbl(i).SELLABLE_FLAG;
310 	p_inst_rec_tab.SYSTEM_ID(i)       :=	p_inst_tbl(i).SYSTEM_ID;
311 	p_inst_rec_tab.INSTANCE_TYPE_CODE(i)       :=	p_inst_tbl(i).INSTANCE_TYPE_CODE;
312 	p_inst_rec_tab.ACTIVE_START_DATE(i)       :=	p_inst_tbl(i).ACTIVE_START_DATE;
313 	p_inst_rec_tab.ACTIVE_END_DATE(i)       :=	p_inst_tbl(i).ACTIVE_END_DATE;
314 	p_inst_rec_tab.LOCATION_TYPE_CODE(i)       :=	p_inst_tbl(i).LOCATION_TYPE_CODE;
315 	p_inst_rec_tab.LOCATION_ID(i)       :=	p_inst_tbl(i).LOCATION_ID;
316 	p_inst_rec_tab.INV_ORGANIZATION_ID(i)       :=	p_inst_tbl(i).INV_ORGANIZATION_ID;
317 	p_inst_rec_tab.INV_SUBINVENTORY_NAME(i)       :=	p_inst_tbl(i).INV_SUBINVENTORY_NAME;
318 	p_inst_rec_tab.INV_LOCATOR_ID(i)       :=	p_inst_tbl(i).INV_LOCATOR_ID;
319 	p_inst_rec_tab.PA_PROJECT_ID(i)       :=	p_inst_tbl(i).PA_PROJECT_ID;
320 	p_inst_rec_tab.PA_PROJECT_TASK_ID(i)       :=	p_inst_tbl(i).PA_PROJECT_TASK_ID;
321 	p_inst_rec_tab.IN_TRANSIT_ORDER_LINE_ID(i)       :=	p_inst_tbl(i).IN_TRANSIT_ORDER_LINE_ID;
322 	p_inst_rec_tab.WIP_JOB_ID(i)       :=	p_inst_tbl(i).WIP_JOB_ID;
323 	p_inst_rec_tab.PO_ORDER_LINE_ID(i)       :=	p_inst_tbl(i).PO_ORDER_LINE_ID;
324 	p_inst_rec_tab.LAST_OE_ORDER_LINE_ID(i)       :=	p_inst_tbl(i).LAST_OE_ORDER_LINE_ID;
325 	p_inst_rec_tab.LAST_OE_RMA_LINE_ID(i)       :=	p_inst_tbl(i).LAST_OE_RMA_LINE_ID;
326 	p_inst_rec_tab.LAST_PO_PO_LINE_ID(i)       :=	p_inst_tbl(i).LAST_PO_PO_LINE_ID;
327 	p_inst_rec_tab.LAST_OE_PO_NUMBER(i)       :=	p_inst_tbl(i).LAST_OE_PO_NUMBER;
328 	p_inst_rec_tab.LAST_WIP_JOB_ID(i)       :=	p_inst_tbl(i).LAST_WIP_JOB_ID;
329 	p_inst_rec_tab.LAST_PA_PROJECT_ID(i)       :=	p_inst_tbl(i).LAST_PA_PROJECT_ID;
330 	p_inst_rec_tab.LAST_PA_TASK_ID(i)       :=	p_inst_tbl(i).LAST_PA_TASK_ID;
331 	p_inst_rec_tab.LAST_OE_AGREEMENT_ID(i)       :=	p_inst_tbl(i).LAST_OE_AGREEMENT_ID;
332 	p_inst_rec_tab.INSTALL_DATE(i)       :=	p_inst_tbl(i).INSTALL_DATE;
333 	p_inst_rec_tab.MANUALLY_CREATED_FLAG(i)       :=	p_inst_tbl(i).MANUALLY_CREATED_FLAG;
334 	p_inst_rec_tab.RETURN_BY_DATE(i)       :=	p_inst_tbl(i).RETURN_BY_DATE;
335 	p_inst_rec_tab.ACTUAL_RETURN_DATE(i)       :=	p_inst_tbl(i).ACTUAL_RETURN_DATE;
336 	p_inst_rec_tab.CREATION_COMPLETE_FLAG(i)       :=	p_inst_tbl(i).CREATION_COMPLETE_FLAG;
337 	p_inst_rec_tab.COMPLETENESS_FLAG(i)       :=	p_inst_tbl(i).COMPLETENESS_FLAG;
338 	p_inst_rec_tab.VERSION_LABEL(i)       :=	p_inst_tbl(i).VERSION_LABEL;
339 	p_inst_rec_tab.VERSION_LABEL_DESCRIPTION(i)       :=	p_inst_tbl(i).VERSION_LABEL_DESCRIPTION;
340 	p_inst_rec_tab.CONTEXT(i)       :=	p_inst_tbl(i).CONTEXT;
341 	p_inst_rec_tab.ATTRIBUTE1(i)       :=	p_inst_tbl(i).ATTRIBUTE1;
342 	p_inst_rec_tab.ATTRIBUTE2(i)       :=	p_inst_tbl(i).ATTRIBUTE2;
343 	p_inst_rec_tab.ATTRIBUTE3(i)       :=	p_inst_tbl(i).ATTRIBUTE3;
344 	p_inst_rec_tab.ATTRIBUTE4(i)       :=	p_inst_tbl(i).ATTRIBUTE4;
345 	p_inst_rec_tab.ATTRIBUTE5(i)       :=	p_inst_tbl(i).ATTRIBUTE5;
346 	p_inst_rec_tab.ATTRIBUTE6(i)       :=	p_inst_tbl(i).ATTRIBUTE6;
347 	p_inst_rec_tab.ATTRIBUTE7(i)       :=	p_inst_tbl(i).ATTRIBUTE7;
348 	p_inst_rec_tab.ATTRIBUTE8(i)       :=	p_inst_tbl(i).ATTRIBUTE8;
349 	p_inst_rec_tab.ATTRIBUTE9(i)       :=	p_inst_tbl(i).ATTRIBUTE9;
350 	p_inst_rec_tab.ATTRIBUTE10(i)       :=	p_inst_tbl(i).ATTRIBUTE10;
351 	p_inst_rec_tab.ATTRIBUTE11(i)       :=	p_inst_tbl(i).ATTRIBUTE11;
352 	p_inst_rec_tab.ATTRIBUTE12(i)       :=	p_inst_tbl(i).ATTRIBUTE12;
353 	p_inst_rec_tab.ATTRIBUTE13(i)       :=	p_inst_tbl(i).ATTRIBUTE13;
354 	p_inst_rec_tab.ATTRIBUTE14(i)       :=	p_inst_tbl(i).ATTRIBUTE14;
355 	p_inst_rec_tab.ATTRIBUTE15(i)       :=	p_inst_tbl(i).ATTRIBUTE15;
356 	p_inst_rec_tab.OBJECT_VERSION_NUMBER(i)       :=	p_inst_tbl(i).OBJECT_VERSION_NUMBER;
357 	p_inst_rec_tab.LAST_TXN_LINE_DETAIL_ID(i)       :=	p_inst_tbl(i).LAST_TXN_LINE_DETAIL_ID;
358 	p_inst_rec_tab.INSTALL_LOCATION_TYPE_CODE(i)       :=	p_inst_tbl(i).INSTALL_LOCATION_TYPE_CODE;
359 	p_inst_rec_tab.INSTALL_LOCATION_ID(i)       :=	p_inst_tbl(i).INSTALL_LOCATION_ID;
360 	p_inst_rec_tab.INSTANCE_USAGE_CODE(i)       :=	p_inst_tbl(i).INSTANCE_USAGE_CODE;
361 	p_inst_rec_tab.CONFIG_INST_HDR_ID(i)       :=	p_inst_tbl(i).CONFIG_INST_HDR_ID;
362 	p_inst_rec_tab.CONFIG_INST_REV_NUM(i)       :=	p_inst_tbl(i).CONFIG_INST_REV_NUM;
363 	p_inst_rec_tab.CONFIG_INST_ITEM_ID(i)       :=	p_inst_tbl(i).CONFIG_INST_ITEM_ID;
364 	p_inst_rec_tab.CONFIG_VALID_STATUS(i)       :=	p_inst_tbl(i).CONFIG_VALID_STATUS;
365 	p_inst_rec_tab.INSTANCE_DESCRIPTION(i)       :=	p_inst_tbl(i).INSTANCE_DESCRIPTION;
366      END LOOP;
367   END Build_Inst_Rec_of_Table;
368   --
369   PROCEDURE Build_Rel_Rec_of_Table
370      (
371        p_ii_relationship_tbl     IN csi_datastructures_pub.ii_relationship_tbl
372       ,p_ii_relationship_rec_tab IN OUT NOCOPY csi_diagnostics_pkg.ii_relationship_rec_tab
373       ,p_rel_hist_tbl            IN OUT NOCOPY csi_diagnostics_pkg.T_NUM
374      ) IS
375   BEGIN
376      FOR i in p_ii_relationship_tbl.FIRST .. p_ii_relationship_tbl.LAST LOOP
377         select CSI_II_RELATIONSHIPS_H_S.nextval
378         into p_rel_hist_tbl(i) from dual;
379         --
380 	p_ii_relationship_rec_tab.RELATIONSHIP_ID(i)	:= p_ii_relationship_tbl(i).RELATIONSHIP_ID;
381 	p_ii_relationship_rec_tab.RELATIONSHIP_TYPE_CODE(i)	:= p_ii_relationship_tbl(i).RELATIONSHIP_TYPE_CODE;
382 	p_ii_relationship_rec_tab.OBJECT_ID(i)	:= p_ii_relationship_tbl(i).OBJECT_ID;
383 	p_ii_relationship_rec_tab.SUBJECT_ID(i)	:= p_ii_relationship_tbl(i).SUBJECT_ID;
384 	p_ii_relationship_rec_tab.SUBJECT_HAS_CHILD(i)	:= p_ii_relationship_tbl(i).SUBJECT_HAS_CHILD;
385 	p_ii_relationship_rec_tab.POSITION_REFERENCE(i)	:= p_ii_relationship_tbl(i).POSITION_REFERENCE;
386 	p_ii_relationship_rec_tab.ACTIVE_START_DATE(i)	:= p_ii_relationship_tbl(i).ACTIVE_START_DATE;
387 	p_ii_relationship_rec_tab.ACTIVE_END_DATE(i)	:= p_ii_relationship_tbl(i).ACTIVE_END_DATE;
388 	p_ii_relationship_rec_tab.DISPLAY_ORDER(i)	:= p_ii_relationship_tbl(i).DISPLAY_ORDER;
389 	p_ii_relationship_rec_tab.MANDATORY_FLAG(i)	:= p_ii_relationship_tbl(i).MANDATORY_FLAG;
390 	p_ii_relationship_rec_tab.CONTEXT(i)	:= p_ii_relationship_tbl(i).CONTEXT;
391 	p_ii_relationship_rec_tab.ATTRIBUTE1(i)	:= p_ii_relationship_tbl(i).ATTRIBUTE1;
392 	p_ii_relationship_rec_tab.ATTRIBUTE2(i)	:= p_ii_relationship_tbl(i).ATTRIBUTE2;
393 	p_ii_relationship_rec_tab.ATTRIBUTE3(i)	:= p_ii_relationship_tbl(i).ATTRIBUTE3;
394 	p_ii_relationship_rec_tab.ATTRIBUTE4(i)	:= p_ii_relationship_tbl(i).ATTRIBUTE4;
395 	p_ii_relationship_rec_tab.ATTRIBUTE5(i)	:= p_ii_relationship_tbl(i).ATTRIBUTE5;
396 	p_ii_relationship_rec_tab.ATTRIBUTE6(i)	:= p_ii_relationship_tbl(i).ATTRIBUTE6;
397 	p_ii_relationship_rec_tab.ATTRIBUTE7(i)	:= p_ii_relationship_tbl(i).ATTRIBUTE7;
398 	p_ii_relationship_rec_tab.ATTRIBUTE8(i)	:= p_ii_relationship_tbl(i).ATTRIBUTE8;
399 	p_ii_relationship_rec_tab.ATTRIBUTE9(i)	:= p_ii_relationship_tbl(i).ATTRIBUTE9;
400 	p_ii_relationship_rec_tab.ATTRIBUTE10(i)	:= p_ii_relationship_tbl(i).ATTRIBUTE10;
401 	p_ii_relationship_rec_tab.ATTRIBUTE11(i)	:= p_ii_relationship_tbl(i).ATTRIBUTE11;
402 	p_ii_relationship_rec_tab.ATTRIBUTE12(i)	:= p_ii_relationship_tbl(i).ATTRIBUTE12;
403 	p_ii_relationship_rec_tab.ATTRIBUTE13(i)	:= p_ii_relationship_tbl(i).ATTRIBUTE13;
404 	p_ii_relationship_rec_tab.ATTRIBUTE14(i)	:= p_ii_relationship_tbl(i).ATTRIBUTE14;
405 	p_ii_relationship_rec_tab.ATTRIBUTE15(i)	:= p_ii_relationship_tbl(i).ATTRIBUTE15;
406 	p_ii_relationship_rec_tab.OBJECT_VERSION_NUMBER(i)	:= p_ii_relationship_tbl(i).OBJECT_VERSION_NUMBER;
407      END LOOP;
408   END Build_Rel_Rec_of_Table;
409   --
410   PROCEDURE Build_Ver_Label_Rec_of_Table
411      (
412        p_version_label_tbl     IN     csi_datastructures_pub.version_label_tbl
413       ,p_version_label_rec_tab IN OUT NOCOPY  csi_diagnostics_pkg.version_label_rec_tab
414       ,p_ver_label_hist_tbl    IN OUT NOCOPY csi_diagnostics_pkg.T_NUM
415      ) IS
416   BEGIN
417      FOR i in p_version_label_tbl.FIRST .. p_version_label_tbl.LAST LOOP
418         select CSI_I_VERSION_LABELS_H_S.nextval
419         into p_ver_label_hist_tbl(i) from dual;
420         --
421 	p_version_label_rec_tab.version_label_id(i)	:= p_version_label_tbl(i).version_label_id;
422 	p_version_label_rec_tab.instance_id(i)	:= p_version_label_tbl(i).instance_id;
423 	p_version_label_rec_tab.version_label(i)	:= p_version_label_tbl(i).version_label;
424 	p_version_label_rec_tab.description(i)	:= p_version_label_tbl(i).description;
425 	p_version_label_rec_tab.date_time_stamp(i)	:= p_version_label_tbl(i).date_time_stamp;
426 	p_version_label_rec_tab.active_start_date(i)	:= p_version_label_tbl(i).active_start_date;
427 	p_version_label_rec_tab.active_end_date(i)	:= p_version_label_tbl(i).active_end_date;
428 	p_version_label_rec_tab.context(i)	:= p_version_label_tbl(i).context;
429 	p_version_label_rec_tab.attribute1(i)	:= p_version_label_tbl(i).attribute1;
430 	p_version_label_rec_tab.attribute2(i)	:= p_version_label_tbl(i).attribute2;
431 	p_version_label_rec_tab.attribute3(i)	:= p_version_label_tbl(i).attribute3;
432 	p_version_label_rec_tab.attribute4(i)	:= p_version_label_tbl(i).attribute4;
433 	p_version_label_rec_tab.attribute5(i)	:= p_version_label_tbl(i).attribute5;
434 	p_version_label_rec_tab.attribute6(i)	:= p_version_label_tbl(i).attribute6;
435 	p_version_label_rec_tab.attribute7(i)	:= p_version_label_tbl(i).attribute7;
436 	p_version_label_rec_tab.attribute8(i)	:= p_version_label_tbl(i).attribute8;
437 	p_version_label_rec_tab.attribute9(i)	:= p_version_label_tbl(i).attribute9;
438 	p_version_label_rec_tab.attribute10(i)	:= p_version_label_tbl(i).attribute10;
439 	p_version_label_rec_tab.attribute11(i)	:= p_version_label_tbl(i).attribute11;
440 	p_version_label_rec_tab.attribute12(i)	:= p_version_label_tbl(i).attribute12;
441 	p_version_label_rec_tab.attribute13(i)	:= p_version_label_tbl(i).attribute13;
442 	p_version_label_rec_tab.attribute14(i)	:= p_version_label_tbl(i).attribute14;
443 	p_version_label_rec_tab.attribute15(i)	:= p_version_label_tbl(i).attribute15;
444 	p_version_label_rec_tab.object_version_number(i)	:= p_version_label_tbl(i).object_version_number;
445      END LOOP;
446   END Build_Ver_Label_Rec_of_Table;
447   --
448   PROCEDURE Build_Party_Rec_of_Table
449      ( p_party_tbl          IN   csi_datastructures_pub.party_tbl
450       ,p_party_rec_tab      IN OUT NOCOPY  csi_diagnostics_pkg.party_rec_tab
451       ,p_party_hist_tbl     IN OUT NOCOPY  csi_diagnostics_pkg.T_NUM
452      ) IS
453   BEGIN
454      FOR i in p_party_tbl.FIRST .. p_party_tbl.LAST LOOP
455         select CSI_I_PARTIES_H_S.nextval
456         into p_party_hist_tbl(i) from dual;
457         --
458 	p_party_rec_tab.instance_party_id(i)	  := p_party_tbl(i).instance_party_id;
459 	p_party_rec_tab.instance_id(i)	  := p_party_tbl(i).instance_id;
460 	p_party_rec_tab.party_source_table(i)	  := p_party_tbl(i).party_source_table;
461 	p_party_rec_tab.party_id(i)	  := p_party_tbl(i).party_id;
462 	p_party_rec_tab.relationship_type_code(i)	  := p_party_tbl(i).relationship_type_code;
463 	p_party_rec_tab.contact_flag(i)	  := p_party_tbl(i).contact_flag;
464 	p_party_rec_tab.contact_ip_id(i)	  := p_party_tbl(i).contact_ip_id;
465 	p_party_rec_tab.active_start_date(i)	  := p_party_tbl(i).active_start_date;
466 	p_party_rec_tab.active_end_date(i)	  := p_party_tbl(i).active_end_date;
467 	p_party_rec_tab.context(i)	  := p_party_tbl(i).context;
468 	p_party_rec_tab.attribute1(i)	  := p_party_tbl(i).attribute1;
469 	p_party_rec_tab.attribute2(i)	  := p_party_tbl(i).attribute2;
470 	p_party_rec_tab.attribute3(i)	  := p_party_tbl(i).attribute3;
471 	p_party_rec_tab.attribute4(i)	  := p_party_tbl(i).attribute4;
472 	p_party_rec_tab.attribute5(i)	  := p_party_tbl(i).attribute5;
473 	p_party_rec_tab.attribute6(i)	  := p_party_tbl(i).attribute6;
474 	p_party_rec_tab.attribute7(i)	  := p_party_tbl(i).attribute7;
475 	p_party_rec_tab.attribute8(i)	  := p_party_tbl(i).attribute8;
476 	p_party_rec_tab.attribute9(i)	  := p_party_tbl(i).attribute9;
477 	p_party_rec_tab.attribute10(i)	  := p_party_tbl(i).attribute10;
478 	p_party_rec_tab.attribute11(i)	  := p_party_tbl(i).attribute11;
479 	p_party_rec_tab.attribute12(i)	  := p_party_tbl(i).attribute12;
480 	p_party_rec_tab.attribute13(i)	  := p_party_tbl(i).attribute13;
481 	p_party_rec_tab.attribute14(i)	  := p_party_tbl(i).attribute14;
482 	p_party_rec_tab.attribute15(i)	  := p_party_tbl(i).attribute15;
483 	p_party_rec_tab.object_version_number(i)	  := p_party_tbl(i).object_version_number;
484 	p_party_rec_tab.primary_flag(i)	  := p_party_tbl(i).primary_flag;
485 	p_party_rec_tab.preferred_flag(i)	  := p_party_tbl(i).preferred_flag;
486      END LOOP;
487   END Build_Party_Rec_of_Table;
488   --
489   PROCEDURE Build_Acct_Rec_of_Table
490      (
491        p_account_tbl        IN     csi_datastructures_pub.party_account_tbl
492       ,p_account_rec_tab    IN OUT NOCOPY  csi_diagnostics_pkg.account_rec_tab
493       ,p_account_hist_tbl   IN OUT NOCOPY  csi_diagnostics_pkg.T_NUM
494      ) IS
495   BEGIN
496      FOR i in p_account_tbl.FIRST .. p_account_tbl.LAST LOOP
497         select CSI_IP_ACCOUNTS_H_S.nextval
498         into p_account_hist_tbl(i) from dual;
499         --
500 	p_account_rec_tab.ip_account_id(i)	 := p_account_tbl(i).ip_account_id;
501 	p_account_rec_tab.parent_tbl_index(i)	 := p_account_tbl(i).parent_tbl_index;
502 	p_account_rec_tab.instance_party_id(i)	 := p_account_tbl(i).instance_party_id;
503 	p_account_rec_tab.party_account_id(i)	 := p_account_tbl(i).party_account_id;
504 	p_account_rec_tab.relationship_type_code(i)	 := p_account_tbl(i).relationship_type_code;
505 	p_account_rec_tab.bill_to_address(i)	 := p_account_tbl(i).bill_to_address;
506 	p_account_rec_tab.ship_to_address(i)	 := p_account_tbl(i).ship_to_address;
507 	p_account_rec_tab.active_start_date(i)	 := p_account_tbl(i).active_start_date;
508 	p_account_rec_tab.active_end_date(i)	 := p_account_tbl(i).active_end_date;
509 	p_account_rec_tab.context(i)	 := p_account_tbl(i).context;
510 	p_account_rec_tab.attribute1(i)	 := p_account_tbl(i).attribute1;
511 	p_account_rec_tab.attribute2(i)	 := p_account_tbl(i).attribute2;
512 	p_account_rec_tab.attribute3(i)	 := p_account_tbl(i).attribute3;
513 	p_account_rec_tab.attribute4(i)	 := p_account_tbl(i).attribute4;
514 	p_account_rec_tab.attribute5(i)	 := p_account_tbl(i).attribute5;
515 	p_account_rec_tab.attribute6(i)	 := p_account_tbl(i).attribute6;
516 	p_account_rec_tab.attribute7(i)	 := p_account_tbl(i).attribute7;
517 	p_account_rec_tab.attribute8(i)	 := p_account_tbl(i).attribute8;
518 	p_account_rec_tab.attribute9(i)	 := p_account_tbl(i).attribute9;
519 	p_account_rec_tab.attribute10(i)	 := p_account_tbl(i).attribute10;
520 	p_account_rec_tab.attribute11(i)	 := p_account_tbl(i).attribute11;
521 	p_account_rec_tab.attribute12(i)	 := p_account_tbl(i).attribute12;
522 	p_account_rec_tab.attribute13(i)	 := p_account_tbl(i).attribute13;
523 	p_account_rec_tab.attribute14(i)	 := p_account_tbl(i).attribute14;
524 	p_account_rec_tab.attribute15(i)	 := p_account_tbl(i).attribute15;
525 	p_account_rec_tab.object_version_number(i)	 := p_account_tbl(i).object_version_number;
526 	p_account_rec_tab.call_contracts(i)	 := p_account_tbl(i).call_contracts;
527 	p_account_rec_tab.vld_organization_id(i)	 := p_account_tbl(i).vld_organization_id;
528 	p_account_rec_tab.expire_flag(i)	 := p_account_tbl(i).expire_flag;
529      END LOOP;
530   END Build_Acct_Rec_of_Table;
531   --
532   PROCEDURE Build_Org_Rec_of_Table
533      (
534        p_org_tbl                 IN      csi_datastructures_pub.organization_units_tbl
535       ,p_org_units_rec_tab       IN OUT NOCOPY   csi_diagnostics_pkg.org_units_rec_tab
536      ,p_org_hist_tbl             IN OUT NOCOPY   csi_diagnostics_pkg.T_NUM
537      ) IS
538   BEGIN
539      FOR i in p_org_tbl.FIRST .. p_org_tbl.LAST LOOP
540        select CSI_I_ORG_ASSIGNMENTS_H_S.nextval
541        into p_org_hist_tbl(i) from dual;
542        --
543        p_org_units_rec_tab.instance_ou_id(i)          := p_org_tbl(i).instance_ou_id;
544        p_org_units_rec_tab.instance_id(i)	            := p_org_tbl(i).instance_id;
545        p_org_units_rec_tab.operating_unit_id(i)       := p_org_tbl(i).operating_unit_id;
546        p_org_units_rec_tab.relationship_type_code(i)  := p_org_tbl(i).relationship_type_code;
547        p_org_units_rec_tab.active_start_date(i)       := p_org_tbl(i).active_start_date;
548        p_org_units_rec_tab.active_end_date(i)         := p_org_tbl(i).active_end_date;
549        p_org_units_rec_tab.context(i)	            := p_org_tbl(i).context;
550        p_org_units_rec_tab.attribute1(i)	            := p_org_tbl(i).attribute1;
551        p_org_units_rec_tab.attribute2(i)	            := p_org_tbl(i).attribute2;
552        p_org_units_rec_tab.attribute3(i)	            := p_org_tbl(i).attribute3;
553        p_org_units_rec_tab.attribute4(i)	            := p_org_tbl(i).attribute4;
554        p_org_units_rec_tab.attribute5(i)	            := p_org_tbl(i).attribute5;
555        p_org_units_rec_tab.attribute6(i)	            := p_org_tbl(i).attribute6;
556        p_org_units_rec_tab.attribute7(i)	            := p_org_tbl(i).attribute7;
557        p_org_units_rec_tab.attribute8(i)	            := p_org_tbl(i).attribute8;
558        p_org_units_rec_tab.attribute9(i)	            := p_org_tbl(i).attribute9;
559        p_org_units_rec_tab.attribute10(i)	            := p_org_tbl(i).attribute10;
560        p_org_units_rec_tab.attribute11(i)	            := p_org_tbl(i).attribute11;
561        p_org_units_rec_tab.attribute12(i)	            := p_org_tbl(i).attribute12;
562        p_org_units_rec_tab.attribute13(i)	            := p_org_tbl(i).attribute13;
563        p_org_units_rec_tab.attribute14(i)	            := p_org_tbl(i).attribute14;
564        p_org_units_rec_tab.attribute15(i)	            := p_org_tbl(i).attribute15;
565        p_org_units_rec_tab.object_version_number(i)   := p_org_tbl(i).object_version_number;
566      END LOOP;
567   END Build_Org_Rec_of_Table;
568   --
569   PROCEDURE Build_pricing_Rec_of_Table
570      (
571        p_pricing_tbl           IN      csi_datastructures_pub.pricing_attribs_tbl
572       ,p_pricing_rec_tab       IN OUT NOCOPY   csi_diagnostics_pkg.pricing_attribs_rec_tab
573       ,p_pricing_hist_tbl      IN OUT NOCOPY   csi_diagnostics_pkg.T_NUM
574      ) IS
575   BEGIN
576      FOR i in p_pricing_tbl.FIRST .. p_pricing_tbl.LAST LOOP
577         select CSI_I_PRICING_ATTRIBS_H_S.nextval
578         into p_pricing_hist_tbl(i) from dual;
579         --
580 	 p_pricing_rec_tab.pricing_attribute_id(i)    := p_pricing_tbl(i).pricing_attribute_id;
581 	 p_pricing_rec_tab.instance_id(i)	            := p_pricing_tbl(i).instance_id;
582 	 p_pricing_rec_tab.active_start_date(i)	    := p_pricing_tbl(i).active_start_date;
583 	 p_pricing_rec_tab.active_end_date(i)	        := p_pricing_tbl(i).active_end_date;
584 	 p_pricing_rec_tab.pricing_context(i)	        := p_pricing_tbl(i).pricing_context;
585 	 p_pricing_rec_tab.pricing_attribute1(i)	    := p_pricing_tbl(i).pricing_attribute1;
586 	 p_pricing_rec_tab.pricing_attribute2(i)	    := p_pricing_tbl(i).pricing_attribute2;
587 	 p_pricing_rec_tab.pricing_attribute3(i)	    := p_pricing_tbl(i).pricing_attribute3;
588 	 p_pricing_rec_tab.pricing_attribute4(i)	    := p_pricing_tbl(i).pricing_attribute4;
589 	 p_pricing_rec_tab.pricing_attribute5(i)	    := p_pricing_tbl(i).pricing_attribute5;
590 	 p_pricing_rec_tab.pricing_attribute6(i)	    := p_pricing_tbl(i).pricing_attribute6;
591 	 p_pricing_rec_tab.pricing_attribute7(i)	    := p_pricing_tbl(i).pricing_attribute7;
592 	 p_pricing_rec_tab.pricing_attribute8(i) 	    := p_pricing_tbl(i).pricing_attribute8;
593 	 p_pricing_rec_tab.pricing_attribute9(i)	    := p_pricing_tbl(i).pricing_attribute9;
594 	 p_pricing_rec_tab.pricing_attribute10(i)	    := p_pricing_tbl(i).pricing_attribute10;
595 	 p_pricing_rec_tab.pricing_attribute11(i)	    := p_pricing_tbl(i).pricing_attribute11;
596 	 p_pricing_rec_tab.pricing_attribute12(i)	    := p_pricing_tbl(i).pricing_attribute12;
597 	 p_pricing_rec_tab.pricing_attribute13(i)	    := p_pricing_tbl(i).pricing_attribute13;
598 	 p_pricing_rec_tab.pricing_attribute14(i)	    := p_pricing_tbl(i).pricing_attribute14;
599 	 p_pricing_rec_tab.pricing_attribute15(i)	    := p_pricing_tbl(i).pricing_attribute15;
600 	 p_pricing_rec_tab.pricing_attribute16(i)	    := p_pricing_tbl(i).pricing_attribute16;
601 	 p_pricing_rec_tab.pricing_attribute17(i)	    := p_pricing_tbl(i).pricing_attribute17;
602 	 p_pricing_rec_tab.pricing_attribute18(i)	    := p_pricing_tbl(i).pricing_attribute18;
603 	 p_pricing_rec_tab.pricing_attribute19(i)	    := p_pricing_tbl(i).pricing_attribute19;
604 	 p_pricing_rec_tab.pricing_attribute20(i)	    := p_pricing_tbl(i).pricing_attribute20;
605 	 p_pricing_rec_tab.pricing_attribute21(i)	    := p_pricing_tbl(i).pricing_attribute21;
606 	 p_pricing_rec_tab.pricing_attribute22(i)	    := p_pricing_tbl(i).pricing_attribute22;
607 	 p_pricing_rec_tab.pricing_attribute23(i)	    := p_pricing_tbl(i).pricing_attribute23;
608 	 p_pricing_rec_tab.pricing_attribute24(i)	    := p_pricing_tbl(i).pricing_attribute24;
609 	 p_pricing_rec_tab.pricing_attribute25(i)	    := p_pricing_tbl(i).pricing_attribute25;
610 	 p_pricing_rec_tab.pricing_attribute26(i)	    := p_pricing_tbl(i).pricing_attribute26;
611 	 p_pricing_rec_tab.pricing_attribute27(i)	    := p_pricing_tbl(i).pricing_attribute27;
612 	 p_pricing_rec_tab.pricing_attribute28(i)	    := p_pricing_tbl(i).pricing_attribute28;
613 	 p_pricing_rec_tab.pricing_attribute29(i)	    := p_pricing_tbl(i).pricing_attribute29;
614 	 p_pricing_rec_tab.pricing_attribute30(i)	    := p_pricing_tbl(i).pricing_attribute30;
615 	 p_pricing_rec_tab.pricing_attribute31(i)	    := p_pricing_tbl(i).pricing_attribute31;
616 	 p_pricing_rec_tab.pricing_attribute32(i)	    := p_pricing_tbl(i).pricing_attribute32;
617 	 p_pricing_rec_tab.pricing_attribute33(i)     := p_pricing_tbl(i).pricing_attribute33;
618 	 p_pricing_rec_tab.pricing_attribute34(i)	    := p_pricing_tbl(i).pricing_attribute34;
619 	 p_pricing_rec_tab.pricing_attribute35(i)	    := p_pricing_tbl(i).pricing_attribute35;
620 	 p_pricing_rec_tab.pricing_attribute36(i)	    := p_pricing_tbl(i).pricing_attribute36;
621 	 p_pricing_rec_tab.pricing_attribute37(i)	    := p_pricing_tbl(i).pricing_attribute37;
622 	 p_pricing_rec_tab.pricing_attribute38(i)	    := p_pricing_tbl(i).pricing_attribute38;
623 	 p_pricing_rec_tab.pricing_attribute39(i)	    := p_pricing_tbl(i).pricing_attribute39;
624 	 p_pricing_rec_tab.pricing_attribute40(i)	    := p_pricing_tbl(i).pricing_attribute40;
625 	 p_pricing_rec_tab.pricing_attribute41(i)	    := p_pricing_tbl(i).pricing_attribute41;
626 	 p_pricing_rec_tab.pricing_attribute42(i)	    := p_pricing_tbl(i).pricing_attribute42;
627 	 p_pricing_rec_tab.pricing_attribute43(i)     := p_pricing_tbl(i).pricing_attribute43;
628 	 p_pricing_rec_tab.pricing_attribute44(i)	    := p_pricing_tbl(i).pricing_attribute44;
629 	 p_pricing_rec_tab.pricing_attribute45(i)	    := p_pricing_tbl(i).pricing_attribute45;
630 	 p_pricing_rec_tab.pricing_attribute46(i)	    := p_pricing_tbl(i).pricing_attribute46;
631 	 p_pricing_rec_tab.pricing_attribute47(i)	    := p_pricing_tbl(i).pricing_attribute47;
632 	 p_pricing_rec_tab.pricing_attribute48(i)	    := p_pricing_tbl(i).pricing_attribute48;
633 	 p_pricing_rec_tab.pricing_attribute49(i)	    := p_pricing_tbl(i).pricing_attribute49;
634 	 p_pricing_rec_tab.pricing_attribute50(i)	    := p_pricing_tbl(i).pricing_attribute50;
635 	 p_pricing_rec_tab.pricing_attribute51(i)	    := p_pricing_tbl(i).pricing_attribute51;
636 	 p_pricing_rec_tab.pricing_attribute52(i)	    := p_pricing_tbl(i).pricing_attribute52;
637 	 p_pricing_rec_tab.pricing_attribute53(i)	    := p_pricing_tbl(i).pricing_attribute53;
638 	 p_pricing_rec_tab.pricing_attribute54(i)	    := p_pricing_tbl(i).pricing_attribute54;
639 	 p_pricing_rec_tab.pricing_attribute55(i)	    := p_pricing_tbl(i).pricing_attribute55;
640 	 p_pricing_rec_tab.pricing_attribute56(i)	    := p_pricing_tbl(i).pricing_attribute56;
641 	 p_pricing_rec_tab.pricing_attribute57(i)	    := p_pricing_tbl(i).pricing_attribute57;
642 	 p_pricing_rec_tab.pricing_attribute58(i)	    := p_pricing_tbl(i).pricing_attribute58;
643 	 p_pricing_rec_tab.pricing_attribute59(i)	    := p_pricing_tbl(i).pricing_attribute59;
644 	 p_pricing_rec_tab.pricing_attribute60(i)	    := p_pricing_tbl(i).pricing_attribute60;
645 	 p_pricing_rec_tab.pricing_attribute61(i)	    := p_pricing_tbl(i).pricing_attribute61;
646 	 p_pricing_rec_tab.pricing_attribute62(i)	    := p_pricing_tbl(i).pricing_attribute62;
647 	 p_pricing_rec_tab.pricing_attribute63(i)	    := p_pricing_tbl(i).pricing_attribute63;
648 	 p_pricing_rec_tab.pricing_attribute64(i)	    := p_pricing_tbl(i).pricing_attribute64;
649 	 p_pricing_rec_tab.pricing_attribute65(i)	    := p_pricing_tbl(i).pricing_attribute65;
650 	 p_pricing_rec_tab.pricing_attribute66(i)	    := p_pricing_tbl(i).pricing_attribute66;
651 	 p_pricing_rec_tab.pricing_attribute67(i)	    := p_pricing_tbl(i).pricing_attribute67;
652 	 p_pricing_rec_tab.pricing_attribute68(i)	    := p_pricing_tbl(i).pricing_attribute68;
653 	 p_pricing_rec_tab.pricing_attribute69(i)	    := p_pricing_tbl(i).pricing_attribute69;
654 	 p_pricing_rec_tab.pricing_attribute70(i)	    := p_pricing_tbl(i).pricing_attribute70;
655 	 p_pricing_rec_tab.pricing_attribute71(i)	    := p_pricing_tbl(i).pricing_attribute71;
656 	 p_pricing_rec_tab.pricing_attribute72(i)	    := p_pricing_tbl(i).pricing_attribute72;
657 	 p_pricing_rec_tab.pricing_attribute73(i)	    := p_pricing_tbl(i).pricing_attribute73;
658 	 p_pricing_rec_tab.pricing_attribute74(i)	    := p_pricing_tbl(i).pricing_attribute74;
659 	 p_pricing_rec_tab.pricing_attribute75(i)	    := p_pricing_tbl(i).pricing_attribute75;
660 	 p_pricing_rec_tab.pricing_attribute76(i)	    := p_pricing_tbl(i).pricing_attribute76;
661 	 p_pricing_rec_tab.pricing_attribute77(i)	    := p_pricing_tbl(i).pricing_attribute77;
662 	 p_pricing_rec_tab.pricing_attribute78(i)	    := p_pricing_tbl(i).pricing_attribute78;
663 	 p_pricing_rec_tab.pricing_attribute79(i)	    := p_pricing_tbl(i).pricing_attribute79;
664 	 p_pricing_rec_tab.pricing_attribute80(i)	    := p_pricing_tbl(i).pricing_attribute80;
665 	 p_pricing_rec_tab.pricing_attribute81(i)	    := p_pricing_tbl(i).pricing_attribute81;
666 	 p_pricing_rec_tab.pricing_attribute82(i)	    := p_pricing_tbl(i).pricing_attribute82;
667 	 p_pricing_rec_tab.pricing_attribute83(i)	    := p_pricing_tbl(i).pricing_attribute83;
668 	 p_pricing_rec_tab.pricing_attribute84(i)	    := p_pricing_tbl(i).pricing_attribute84;
669 	 p_pricing_rec_tab.pricing_attribute85(i)	    := p_pricing_tbl(i).pricing_attribute85;
670 	 p_pricing_rec_tab.pricing_attribute86(i)	    := p_pricing_tbl(i).pricing_attribute86;
671 	 p_pricing_rec_tab.pricing_attribute87(i)	    := p_pricing_tbl(i).pricing_attribute87;
672 	 p_pricing_rec_tab.pricing_attribute88(i)	    := p_pricing_tbl(i).pricing_attribute88;
673 	 p_pricing_rec_tab.pricing_attribute89(i)	    := p_pricing_tbl(i).pricing_attribute89;
674 	 p_pricing_rec_tab.pricing_attribute90(i)	    := p_pricing_tbl(i).pricing_attribute90;
675 	 p_pricing_rec_tab.pricing_attribute91(i)	    := p_pricing_tbl(i).pricing_attribute91;
676 	 p_pricing_rec_tab.pricing_attribute92(i)	    := p_pricing_tbl(i).pricing_attribute92;
677 	 p_pricing_rec_tab.pricing_attribute93(i)	    := p_pricing_tbl(i).pricing_attribute93;
678 	 p_pricing_rec_tab.pricing_attribute94(i)	    := p_pricing_tbl(i).pricing_attribute94;
679 	 p_pricing_rec_tab.pricing_attribute95(i)	    := p_pricing_tbl(i).pricing_attribute95;
680 	 p_pricing_rec_tab.pricing_attribute96(i)	    := p_pricing_tbl(i).pricing_attribute96;
681 	 p_pricing_rec_tab.pricing_attribute97(i)	    := p_pricing_tbl(i).pricing_attribute97;
682 	 p_pricing_rec_tab.pricing_attribute98(i)	    := p_pricing_tbl(i).pricing_attribute98;
683 	 p_pricing_rec_tab.pricing_attribute99(i)	    := p_pricing_tbl(i).pricing_attribute99;
684 	 p_pricing_rec_tab.pricing_attribute100(i)    := p_pricing_tbl(i).pricing_attribute100;
685 	 p_pricing_rec_tab.context(i)	                := p_pricing_tbl(i).context;
686 	 p_pricing_rec_tab.attribute1(i)	            := p_pricing_tbl(i).attribute1;
687 	 p_pricing_rec_tab.attribute2(i)	            := p_pricing_tbl(i).attribute2;
688 	 p_pricing_rec_tab.attribute3(i)	            := p_pricing_tbl(i).attribute3;
689 	 p_pricing_rec_tab.attribute4(i)	            := p_pricing_tbl(i).attribute4;
690 	 p_pricing_rec_tab.attribute5(i)	            := p_pricing_tbl(i).attribute5;
691 	 p_pricing_rec_tab.attribute6(i)	            := p_pricing_tbl(i).attribute6;
692 	 p_pricing_rec_tab.attribute7(i)	            := p_pricing_tbl(i).attribute7;
693 	 p_pricing_rec_tab.attribute8(i)	            := p_pricing_tbl(i).attribute8;
694 	 p_pricing_rec_tab.attribute9(i)	            := p_pricing_tbl(i).attribute9;
695 	 p_pricing_rec_tab.attribute10(i)	            := p_pricing_tbl(i).attribute10;
696 	 p_pricing_rec_tab.attribute11(i)	            := p_pricing_tbl(i).attribute11;
697 	 p_pricing_rec_tab.attribute12(i)	            := p_pricing_tbl(i).attribute12;
698 	 p_pricing_rec_tab.attribute13(i)	            := p_pricing_tbl(i).attribute13;
699 	 p_pricing_rec_tab.attribute14(i)	            := p_pricing_tbl(i).attribute14;
700 	 p_pricing_rec_tab.attribute15(i)	            := p_pricing_tbl(i).attribute15;
701 	 p_pricing_rec_tab.object_version_number(i)   := p_pricing_tbl(i).object_version_number;
702      END LOOP;
703   END Build_pricing_Rec_of_Table;
704   --
705   PROCEDURE Build_Ext_Attr_Rec_Table
706      (
707        p_ext_attr_tbl     IN     csi_datastructures_pub.extend_attrib_values_tbl
708       ,p_ext_attr_rec_tab IN OUT NOCOPY  csi_diagnostics_pkg.extend_attrib_values_rec_tab
709      ,p_ext_hist_tbl      IN OUT NOCOPY  csi_diagnostics_pkg.T_NUM
710      ) IS
711 
712   BEGIN
713      FOR i in p_ext_attr_tbl.FIRST .. p_ext_attr_tbl.LAST LOOP
714        select CSI_IEA_VALUES_H_S.nextval
715        into p_ext_hist_tbl(i) from dual;
716        --
717        p_ext_attr_rec_tab.attribute_value_id(i)      :=  p_ext_attr_tbl(i).attribute_value_id;
718        p_ext_attr_rec_tab.instance_id(i)             :=  p_ext_attr_tbl(i).instance_id;
719        p_ext_attr_rec_tab.attribute_id(i)            :=  p_ext_attr_tbl(i).attribute_id;
720        p_ext_attr_rec_tab.attribute_code(i)          :=  p_ext_attr_tbl(i).attribute_code;
721        p_ext_attr_rec_tab.attribute_value(i)         :=  p_ext_attr_tbl(i).attribute_value;
722        p_ext_attr_rec_tab.active_start_date(i)       :=  p_ext_attr_tbl(i).active_start_date;
723        p_ext_attr_rec_tab.active_end_date(i)         :=  p_ext_attr_tbl(i).active_end_date;
724        p_ext_attr_rec_tab.context(i)                 :=  p_ext_attr_tbl(i).context;
725        p_ext_attr_rec_tab.attribute1(i)              :=  p_ext_attr_tbl(i).attribute1;
726        p_ext_attr_rec_tab.attribute2 (i)             :=  p_ext_attr_tbl(i).attribute2;
727        p_ext_attr_rec_tab.attribute3(i)              :=  p_ext_attr_tbl(i).attribute3;
728        p_ext_attr_rec_tab.attribute4(i)              :=  p_ext_attr_tbl(i).attribute4;
729        p_ext_attr_rec_tab.attribute5(i)              :=  p_ext_attr_tbl(i).attribute5;
730        p_ext_attr_rec_tab.attribute6(i)              :=  p_ext_attr_tbl(i).attribute6;
731        p_ext_attr_rec_tab.attribute7(i)              :=  p_ext_attr_tbl(i).attribute7;
732        p_ext_attr_rec_tab.attribute8(i)              :=  p_ext_attr_tbl(i).attribute8;
733        p_ext_attr_rec_tab.attribute9(i)              :=  p_ext_attr_tbl(i).attribute9;
734        p_ext_attr_rec_tab.attribute10(i)             :=  p_ext_attr_tbl(i).attribute10;
735        p_ext_attr_rec_tab.attribute11(i)             :=  p_ext_attr_tbl(i).attribute11;
736        p_ext_attr_rec_tab.attribute12(i)             :=  p_ext_attr_tbl(i).attribute12;
737        p_ext_attr_rec_tab.attribute13(i)             :=  p_ext_attr_tbl(i).attribute13;
738        p_ext_attr_rec_tab.attribute14(i)             :=  p_ext_attr_tbl(i).attribute14;
739        p_ext_attr_rec_tab.attribute15(i)             :=  p_ext_attr_tbl(i).attribute15;
740        p_ext_attr_rec_tab.object_version_number(i)   :=  p_ext_attr_tbl(i).object_version_number;
741      END LOOP;
742   END Build_Ext_Attr_Rec_Table;
743   --
744   PROCEDURE Build_Asset_Rec_Table
745      (
746        p_asset_tbl     IN     csi_datastructures_pub.instance_asset_tbl
747       ,p_asset_rec_tab IN OUT NOCOPY  csi_diagnostics_pkg.instance_asset_rec_tab
748       ,p_asset_hist_tbl IN OUT NOCOPY  csi_diagnostics_pkg.T_NUM
749      ) IS
750 
751   BEGIN
752      FOR i in p_asset_tbl.FIRST .. p_asset_tbl.LAST LOOP
753        select CSI_I_ASSETS_H_S.nextval
754        into p_asset_hist_tbl(i) from dual;
755        --
756        p_asset_rec_tab.instance_asset_id(i)          := p_asset_tbl(i).instance_asset_id;
757        p_asset_rec_tab.instance_id(i)                := p_asset_tbl(i).instance_id;
758        p_asset_rec_tab.fa_asset_id(i)                := p_asset_tbl(i).fa_asset_id;
759        p_asset_rec_tab.fa_book_type_code(i)          := p_asset_tbl(i).fa_book_type_code;
760        p_asset_rec_tab.fa_location_id(i)             := p_asset_tbl(i).fa_location_id;
761        p_asset_rec_tab.asset_quantity(i)             := p_asset_tbl(i).asset_quantity;
762        p_asset_rec_tab.update_status(i)              := p_asset_tbl(i).update_status;
763        p_asset_rec_tab.active_start_date(i)          := p_asset_tbl(i).active_start_date;
764        p_asset_rec_tab.active_end_date(i)            := p_asset_tbl(i).active_end_date;
765        p_asset_rec_tab.object_version_number(i)      := p_asset_tbl(i).object_version_number;
766        p_asset_rec_tab.check_for_instance_expiry(i)  := p_asset_tbl(i).check_for_instance_expiry;
767 
768      END LOOP;
769      --
770   END Build_Asset_Rec_Table;
771   --
772 
773   PROCEDURE make_non_header(
774     p_inst_h_rec       IN  csi_datastructures_pub.instance_header_rec,
775     p_pty_h_tbl        IN  csi_datastructures_pub.party_header_tbl,
776     p_pa_h_tbl         IN  csi_datastructures_pub.party_account_header_tbl,
777     p_ou_h_tbl         IN  csi_datastructures_pub.org_units_header_tbl,
778     p_asset_h_tbl      IN  csi_datastructures_pub.instance_asset_header_tbl,
779     x_inst_rec         OUT nocopy csi_datastructures_pub.instance_rec,
780     x_pty_tbl          OUT nocopy csi_datastructures_pub.party_tbl,
781     x_pa_tbl           OUT nocopy csi_datastructures_pub.party_account_tbl,
782     x_ou_tbl           OUT nocopy csi_datastructures_pub.organization_units_tbl,
783     x_asset_tbl        OUT nocopy csi_datastructures_pub.instance_asset_tbl,
784     x_return_status    OUT nocopy varchar2)
785   IS
786   BEGIN
787 
788     x_return_status := fnd_api.g_ret_sts_success;
789 
790     x_inst_rec.instance_id                := p_inst_h_rec.instance_id;
791     x_inst_rec.instance_number            := p_inst_h_rec.instance_number;
792     x_inst_rec.external_reference         := p_inst_h_rec.external_reference;
793     x_inst_rec.inventory_item_id          := p_inst_h_rec.inventory_item_id;
794     x_inst_rec.inventory_revision         := p_inst_h_rec.inventory_revision;
795     x_inst_rec.inv_master_organization_id := p_inst_h_rec.inv_master_organization_id;
796     x_inst_rec.serial_number              := p_inst_h_rec.serial_number;
797     x_inst_rec.mfg_serial_number_flag     := p_inst_h_rec.mfg_serial_number_flag;
798     x_inst_rec.lot_number                 := p_inst_h_rec.lot_number;
799     x_inst_rec.quantity                   := p_inst_h_rec.quantity;
800     x_inst_rec.unit_of_measure            := p_inst_h_rec.unit_of_measure;
801     x_inst_rec.accounting_class_code      := p_inst_h_rec.accounting_class_code;
802     x_inst_rec.instance_condition_id      := p_inst_h_rec.instance_condition_id;
803     x_inst_rec.instance_usage_code        := p_inst_h_rec.instance_usage_code;
804     x_inst_rec.instance_status_id         := p_inst_h_rec.instance_status_id;
805     x_inst_rec.customer_view_flag         := p_inst_h_rec.customer_view_flag;
806     x_inst_rec.merchant_view_flag         := p_inst_h_rec.merchant_view_flag;
807     x_inst_rec.sellable_flag              := p_inst_h_rec.sellable_flag;
808     x_inst_rec.system_id                  := p_inst_h_rec.system_id;
809     x_inst_rec.instance_type_code         := p_inst_h_rec.instance_type_code;
810     x_inst_rec.active_start_date          := p_inst_h_rec.active_start_date;
811     x_inst_rec.active_end_date            := p_inst_h_rec.active_end_date;
812     x_inst_rec.location_type_code         := p_inst_h_rec.location_type_code;
813     x_inst_rec.location_id                := p_inst_h_rec.location_id;
814     x_inst_rec.install_location_type_code := p_inst_h_rec.install_location_type_code;
815     x_inst_rec.install_location_id        := p_inst_h_rec.install_location_id;
816     x_inst_rec.inv_organization_id        := p_inst_h_rec.inv_organization_id;
817     x_inst_rec.inv_subinventory_name      := p_inst_h_rec.inv_subinventory_name;
818     x_inst_rec.inv_locator_id             := p_inst_h_rec.inv_locator_id;
819     x_inst_rec.pa_project_id              := p_inst_h_rec.pa_project_id;
820     x_inst_rec.pa_project_task_id         := p_inst_h_rec.pa_project_task_id;
821     x_inst_rec.in_transit_order_line_id   := p_inst_h_rec.in_transit_order_line_id;
822     x_inst_rec.wip_job_id                 := p_inst_h_rec.wip_job_id;
823     x_inst_rec.po_order_line_id           := p_inst_h_rec.po_order_line_id;
824     x_inst_rec.last_oe_order_line_id      := p_inst_h_rec.last_oe_order_line_id;
825     x_inst_rec.last_oe_rma_line_id        := p_inst_h_rec.last_oe_rma_line_id;
826     x_inst_rec.last_po_po_line_id         := p_inst_h_rec.last_po_po_line_id;
827     x_inst_rec.last_oe_po_number          := p_inst_h_rec.last_oe_po_number;
828     x_inst_rec.last_wip_job_id            := p_inst_h_rec.last_wip_job_id;
829     x_inst_rec.last_pa_project_id         := p_inst_h_rec.last_pa_project_id;
830     x_inst_rec.last_pa_task_id            := p_inst_h_rec.last_pa_task_id;
831     x_inst_rec.last_oe_agreement_id       := p_inst_h_rec.last_oe_agreement_id;
832     x_inst_rec.install_date               := p_inst_h_rec.install_date;
833     x_inst_rec.manually_created_flag      := p_inst_h_rec.manually_created_flag;
834     x_inst_rec.return_by_date             := p_inst_h_rec.return_by_date;
835     x_inst_rec.actual_return_date         := p_inst_h_rec.actual_return_date;
836     x_inst_rec.creation_complete_flag     := p_inst_h_rec.creation_complete_flag;
837     x_inst_rec.completeness_flag          := p_inst_h_rec.completeness_flag;
838     x_inst_rec.context                    := p_inst_h_rec.context;
839     x_inst_rec.attribute1                 := p_inst_h_rec.attribute1;
840     x_inst_rec.attribute2                 := p_inst_h_rec.attribute2;
841     x_inst_rec.attribute3                 := p_inst_h_rec.attribute3;
842     x_inst_rec.attribute4                 := p_inst_h_rec.attribute4;
843     x_inst_rec.attribute5                 := p_inst_h_rec.attribute5;
844     x_inst_rec.attribute6                 := p_inst_h_rec.attribute6;
845     x_inst_rec.attribute7                 := p_inst_h_rec.attribute7;
846     x_inst_rec.attribute8                 := p_inst_h_rec.attribute8;
847     x_inst_rec.attribute9                 := p_inst_h_rec.attribute9;
848     x_inst_rec.attribute10                := p_inst_h_rec.attribute10;
849     x_inst_rec.attribute11                := p_inst_h_rec.attribute11;
850     x_inst_rec.attribute12                := p_inst_h_rec.attribute12;
851     x_inst_rec.attribute13                := p_inst_h_rec.attribute13;
852     x_inst_rec.attribute14                := p_inst_h_rec.attribute14;
853     x_inst_rec.attribute15                := p_inst_h_rec.attribute15;
854     x_inst_rec.object_version_number      := p_inst_h_rec.object_version_number;
855     x_inst_rec.config_inst_hdr_id         := p_inst_h_rec.config_inst_hdr_id;
856     x_inst_rec.config_inst_rev_num        := p_inst_h_rec.config_inst_rev_num;
857     x_inst_rec.config_inst_item_id        := p_inst_h_rec.config_inst_item_id;
858     x_inst_rec.config_valid_status        := p_inst_h_rec.config_valid_status;
859     x_inst_rec.instance_description       := p_inst_h_rec.instance_description;
860 
861 
862     IF p_pty_h_tbl.COUNT > 0 THEN
863       FOR l_ind IN p_pty_h_tbl.FIRST .. p_pty_h_tbl.LAST
864       LOOP
865         x_pty_tbl(l_ind).instance_party_id        := p_pty_h_tbl(l_ind).instance_party_id;
866         x_pty_tbl(l_ind).instance_id              := p_pty_h_tbl(l_ind).instance_id;
867         x_pty_tbl(l_ind).party_source_table       := p_pty_h_tbl(l_ind).party_source_table;
868         x_pty_tbl(l_ind).party_id                 := p_pty_h_tbl(l_ind).party_id;
869         x_pty_tbl(l_ind).relationship_type_code   := p_pty_h_tbl(l_ind).relationship_type_code;
870         x_pty_tbl(l_ind).contact_flag             := p_pty_h_tbl(l_ind).contact_flag;
871         x_pty_tbl(l_ind).contact_ip_id            := p_pty_h_tbl(l_ind).contact_ip_id;
872         x_pty_tbl(l_ind).active_start_date        := p_pty_h_tbl(l_ind).active_start_date;
873         x_pty_tbl(l_ind).active_end_date          := p_pty_h_tbl(l_ind).active_end_date;
874         x_pty_tbl(l_ind).context                  := p_pty_h_tbl(l_ind).context;
875         x_pty_tbl(l_ind).attribute1               := p_pty_h_tbl(l_ind).attribute1;
876         x_pty_tbl(l_ind).attribute2               := p_pty_h_tbl(l_ind).attribute2;
877         x_pty_tbl(l_ind).attribute3               := p_pty_h_tbl(l_ind).attribute3;
878         x_pty_tbl(l_ind).attribute4               := p_pty_h_tbl(l_ind).attribute4;
879         x_pty_tbl(l_ind).attribute5               := p_pty_h_tbl(l_ind).attribute5;
880         x_pty_tbl(l_ind).attribute6               := p_pty_h_tbl(l_ind).attribute6;
881         x_pty_tbl(l_ind).attribute7               := p_pty_h_tbl(l_ind).attribute7;
882         x_pty_tbl(l_ind).attribute8               := p_pty_h_tbl(l_ind).attribute8;
883         x_pty_tbl(l_ind).attribute9               := p_pty_h_tbl(l_ind).attribute9;
884         x_pty_tbl(l_ind).attribute10              := p_pty_h_tbl(l_ind).attribute10;
885         x_pty_tbl(l_ind).attribute11              := p_pty_h_tbl(l_ind).attribute11;
886         x_pty_tbl(l_ind).attribute12              := p_pty_h_tbl(l_ind).attribute12;
887         x_pty_tbl(l_ind).attribute13              := p_pty_h_tbl(l_ind).attribute13;
888         x_pty_tbl(l_ind).attribute14              := p_pty_h_tbl(l_ind).attribute14;
889         x_pty_tbl(l_ind).attribute15              := p_pty_h_tbl(l_ind).attribute15;
890         x_pty_tbl(l_ind).primary_flag             := p_pty_h_tbl(l_ind).primary_flag;
891         x_pty_tbl(l_ind).preferred_flag           := p_pty_h_tbl(l_ind).preferred_flag;
892         SELECT object_version_number
893         INTO   x_pty_tbl(l_ind).object_version_number
894         FROM   csi_i_parties
895         WHERE  instance_party_id = p_pty_h_tbl(l_ind).instance_party_id;
896       END LOOP;
897     END IF;
898 
899     IF p_pa_h_tbl.count > 0 THEN
900       FOR l_ind IN p_pa_h_tbl.FIRST .. p_pa_h_tbl.LAST
901       LOOP
902 
903         x_pa_tbl(l_ind).ip_account_id          := p_pa_h_tbl(l_ind).ip_account_id;
904         x_pa_tbl(l_ind).instance_party_id      := p_pa_h_tbl(l_ind).instance_party_id;
905         x_pa_tbl(l_ind).party_account_id       := p_pa_h_tbl(l_ind).party_account_id;
906         x_pa_tbl(l_ind).relationship_type_code := p_pa_h_tbl(l_ind).relationship_type_code;
907         x_pa_tbl(l_ind).bill_to_address        := p_pa_h_tbl(l_ind).bill_to_address;
908         x_pa_tbl(l_ind).ship_to_address        := p_pa_h_tbl(l_ind).ship_to_address;
909         x_pa_tbl(l_ind).active_start_date      := p_pa_h_tbl(l_ind).active_start_date;
910         x_pa_tbl(l_ind).active_end_date        := p_pa_h_tbl(l_ind).active_end_date;
911         x_pa_tbl(l_ind).context                := p_pa_h_tbl(l_ind).context;
912         x_pa_tbl(l_ind).attribute1             := p_pa_h_tbl(l_ind).attribute1;
913         x_pa_tbl(l_ind).attribute2             := p_pa_h_tbl(l_ind).attribute2;
914         x_pa_tbl(l_ind).attribute3             := p_pa_h_tbl(l_ind).attribute3;
915         x_pa_tbl(l_ind).attribute4             := p_pa_h_tbl(l_ind).attribute4;
916         x_pa_tbl(l_ind).attribute5             := p_pa_h_tbl(l_ind).attribute5;
917         x_pa_tbl(l_ind).attribute6             := p_pa_h_tbl(l_ind).attribute6;
918         x_pa_tbl(l_ind).attribute7             := p_pa_h_tbl(l_ind).attribute7;
919         x_pa_tbl(l_ind).attribute8             := p_pa_h_tbl(l_ind).attribute8;
920         x_pa_tbl(l_ind).attribute9             := p_pa_h_tbl(l_ind).attribute9;
921         x_pa_tbl(l_ind).attribute10            := p_pa_h_tbl(l_ind).attribute10;
922         x_pa_tbl(l_ind).attribute11            := p_pa_h_tbl(l_ind).attribute11;
923         x_pa_tbl(l_ind).attribute12            := p_pa_h_tbl(l_ind).attribute12;
924         x_pa_tbl(l_ind).attribute13            := p_pa_h_tbl(l_ind).attribute13;
925         x_pa_tbl(l_ind).attribute14            := p_pa_h_tbl(l_ind).attribute14;
926         x_pa_tbl(l_ind).attribute15            := p_pa_h_tbl(l_ind).attribute15;
927         SELECT object_version_number
928         INTO   x_pa_tbl(l_ind).object_version_number
929         FROM   csi_ip_accounts
930         WHERE  ip_account_id = p_pa_h_tbl(l_ind).ip_account_id;
931 
932       END LOOP;
933     END IF;
934 
935     IF p_ou_h_tbl.count > 0 THEN
936       FOR l_ind IN p_ou_h_tbl.FIRST .. p_ou_h_tbl.LAST
937       LOOP
938 
939         x_ou_tbl(l_ind).instance_ou_id         := p_ou_h_tbl(l_ind).instance_ou_id;
940         x_ou_tbl(l_ind).instance_id            := p_ou_h_tbl(l_ind).instance_id;
941         x_ou_tbl(l_ind).operating_unit_id      := p_ou_h_tbl(l_ind).operating_unit_id;
942         x_ou_tbl(l_ind).relationship_type_code := p_ou_h_tbl(l_ind).relationship_type_code;
943         x_ou_tbl(l_ind).active_start_date      := p_ou_h_tbl(l_ind).active_start_date;
944         x_ou_tbl(l_ind).active_end_date        := p_ou_h_tbl(l_ind).active_end_date;
945         x_ou_tbl(l_ind).context                := p_ou_h_tbl(l_ind).context;
946         x_ou_tbl(l_ind).attribute1             := p_ou_h_tbl(l_ind).attribute1;
947         x_ou_tbl(l_ind).attribute2             := p_ou_h_tbl(l_ind).attribute2;
948         x_ou_tbl(l_ind).attribute3             := p_ou_h_tbl(l_ind).attribute3;
949         x_ou_tbl(l_ind).attribute4             := p_ou_h_tbl(l_ind).attribute4;
950         x_ou_tbl(l_ind).attribute5             := p_ou_h_tbl(l_ind).attribute5;
951         x_ou_tbl(l_ind).attribute6             := p_ou_h_tbl(l_ind).attribute6;
952         x_ou_tbl(l_ind).attribute7             := p_ou_h_tbl(l_ind).attribute7;
953         x_ou_tbl(l_ind).attribute8             := p_ou_h_tbl(l_ind).attribute8;
954         x_ou_tbl(l_ind).attribute9             := p_ou_h_tbl(l_ind).attribute9;
955         x_ou_tbl(l_ind).attribute10            := p_ou_h_tbl(l_ind).attribute10;
956         x_ou_tbl(l_ind).attribute11            := p_ou_h_tbl(l_ind).attribute11;
957         x_ou_tbl(l_ind).attribute12            := p_ou_h_tbl(l_ind).attribute12;
958         x_ou_tbl(l_ind).attribute13            := p_ou_h_tbl(l_ind).attribute13;
959         x_ou_tbl(l_ind).attribute14            := p_ou_h_tbl(l_ind).attribute14;
960         x_ou_tbl(l_ind).attribute15            := p_ou_h_tbl(l_ind).attribute15;
961         SELECT object_version_number
962         INTO   x_ou_tbl(l_ind).object_version_number
963         FROM   csi_i_org_assignments
964         WHERE  instance_ou_id = p_ou_h_tbl(l_ind).instance_ou_id;
965 
966       END LOOP;
967     END IF;
968 
969     IF p_asset_h_tbl.count > 0 THEN
970       FOR l_ind IN p_asset_h_tbl.FIRST .. p_asset_h_tbl.LAST
971       LOOP
972 
973         x_asset_tbl(l_ind).instance_asset_id     := p_asset_h_tbl(l_ind).instance_asset_id;
974         x_asset_tbl(l_ind).instance_id           := p_asset_h_tbl(l_ind).instance_id;
975         x_asset_tbl(l_ind).fa_asset_id           := p_asset_h_tbl(l_ind).fa_asset_id;
976         x_asset_tbl(l_ind).fa_book_type_code     := p_asset_h_tbl(l_ind).fa_book_type_code;
977         x_asset_tbl(l_ind).fa_location_id        := p_asset_h_tbl(l_ind).fa_location_id;
978         x_asset_tbl(l_ind).asset_quantity        := p_asset_h_tbl(l_ind).asset_quantity;
979         x_asset_tbl(l_ind).update_status         := p_asset_h_tbl(l_ind).update_status;
980         x_asset_tbl(l_ind).active_start_date     := p_asset_h_tbl(l_ind).active_start_date;
981         x_asset_tbl(l_ind).active_end_date       := p_asset_h_tbl(l_ind).active_end_date;
982         SELECT object_version_number
983         INTO   x_asset_tbl(l_ind).object_version_number
984         FROM   csi_i_assets
985         WHERE  instance_asset_id = p_asset_h_tbl(l_ind).instance_asset_id;
986 
987       END LOOP;
988     END IF;
989 
990   END make_non_header;
991   --
992   PROCEDURE forward_sync IS
993 
994     CURSOR fs_cur IS
995       SELECT instance_id,
996              date_time_stamp,
997              inventory_item_id,
998              serial_number,
999              lot_control_code,
1000              mtl_txn_id,
1001              mtl_txn_creation_date
1002       FROM   csi_ii_forward_sync_temp
1003       WHERE  process_flag <> 'P';
1004 
1005     l_txn_type_id          number;
1006     l_error_exists         varchar2(1) := 'N';
1007     l_csi_txn_rec          csi_datastructures_pub.transaction_rec;
1008 
1009     -- get instance details variables
1010     g_inst_rec             csi_datastructures_pub.instance_header_rec;
1011     g_pty_tbl              csi_datastructures_pub.party_header_tbl;
1012     g_pa_tbl               csi_datastructures_pub.party_account_header_tbl;
1013     g_ou_tbl               csi_datastructures_pub.org_units_header_tbl;
1014     g_prc_tbl              csi_datastructures_pub.pricing_attribs_tbl;
1015     g_eav_tbl              csi_datastructures_pub.extend_attrib_values_tbl;
1016     g_ea_tbl               csi_datastructures_pub.extend_attrib_tbl;
1017     g_asset_tbl            csi_datastructures_pub.instance_asset_header_tbl;
1018 
1019 
1020     -- get version label variables
1021     g_vl_qry_rec           csi_datastructures_pub.version_label_query_rec;
1022     g_vl_tbl               csi_datastructures_pub.version_label_tbl;
1023 
1024     -- get relationship variables
1025     l_iir_qry_rec          csi_datastructures_pub.relationship_query_rec;
1026     g_iir_qry_rec          csi_datastructures_pub.relationship_query_rec;
1027     l_iir_tbl              csi_datastructures_pub.ii_relationship_tbl;
1028     g_iir_tbl              csi_datastructures_pub.ii_relationship_tbl;
1029     iir_ind                binary_integer := 0;
1030 
1031 
1032     -- update item instance variables
1033     l_inst_rec             csi_datastructures_pub.instance_rec;
1034     l_pty_tbl              csi_datastructures_pub.party_tbl;
1035     l_pa_tbl               csi_datastructures_pub.party_account_tbl;
1036     l_ou_tbl               csi_datastructures_pub.organization_units_tbl;
1037     l_eav_tbl              csi_datastructures_pub.extend_attrib_values_tbl;
1038     l_prc_tbl              csi_datastructures_pub.pricing_attribs_tbl;
1039     l_asset_tbl            csi_datastructures_pub.instance_asset_tbl;
1040     l_inst_ids_tbl         csi_datastructures_pub.id_tbl;
1041     l_txn_rec              csi_datastructures_pub.transaction_rec;
1042     l_vl_tbl               csi_datastructures_pub.version_label_tbl;
1043 
1044     l_return_status        varchar2(1);
1045     l_msg_data             varchar2(2000);
1046     l_msg_count            number;
1047 
1048     PROCEDURE init_struct(
1049       px_inst_rec     IN OUT nocopy csi_datastructures_pub.instance_header_rec,
1050       px_pty_tbl      IN OUT nocopy csi_datastructures_pub.party_header_tbl,
1051       px_pa_tbl       IN OUT nocopy csi_datastructures_pub.party_account_header_tbl,
1052       px_ou_tbl       IN OUT nocopy csi_datastructures_pub.org_units_header_tbl,
1053       px_prc_tbl      IN OUT nocopy csi_datastructures_pub.pricing_attribs_tbl,
1054       px_eav_tbl      IN OUT nocopy csi_datastructures_pub.extend_attrib_values_tbl,
1055       px_ea_tbl       IN OUT nocopy csi_datastructures_pub.extend_attrib_tbl,
1056       px_asset_tbl    IN OUT nocopy csi_datastructures_pub.instance_asset_header_tbl,
1057       px_vl_qry_rec   IN OUT nocopy csi_datastructures_pub.version_label_query_rec,
1058       px_vl_tbl       IN OUT nocopy csi_datastructures_pub.version_label_tbl,
1059       px_iir_qry_rec  IN OUT nocopy csi_datastructures_pub.relationship_query_rec,
1060       px_iir_tbl      IN OUT nocopy csi_datastructures_pub.ii_relationship_tbl)
1061     IS
1062       l_inst_rec      csi_datastructures_pub.instance_header_rec;
1063       l_vl_qry_rec    csi_datastructures_pub.version_label_query_rec;
1064       l_iir_qry_rec   csi_datastructures_pub.relationship_query_rec;
1065     BEGIN
1066       px_inst_rec     := l_inst_rec;
1067       px_vl_qry_rec   := l_vl_qry_rec;
1068       px_iir_qry_rec  := l_iir_qry_rec;
1069       px_pty_tbl.delete;
1070       px_pa_tbl.delete;
1071       px_ou_tbl.delete;
1072       px_prc_tbl.delete;
1073       px_eav_tbl.delete;
1074       px_ea_tbl.delete;
1075       px_asset_tbl.delete;
1076       px_vl_tbl.delete;
1077       px_iir_tbl.delete;
1078     END init_struct;
1079   BEGIN
1080 
1081     l_csi_txn_rec.transaction_type_id     := correction_txn_type_id;
1082     l_csi_txn_rec.transaction_date        := sysdate;
1083     l_csi_txn_rec.source_transaction_date := sysdate;
1084     l_csi_txn_rec.source_header_ref       := 'Forward Synch';
1085 
1086     SELECT csi_ii_forward_sync_temp_s.nextval
1087     INTO   l_csi_txn_rec.source_line_ref_id
1088     FROM   sys.dual;
1089 
1090     FOR fs_rec IN fs_cur
1091     LOOP
1092 
1093       iir_ind := 0;
1094 
1095       -- check for pending errors
1096       FOR all_txn in all_txn_cur(fs_rec.serial_number,fs_rec.inventory_item_id)
1097       LOOP
1098 
1099         IF all_txn.mtl_creation_date < fs_rec.mtl_txn_creation_date THEN
1100           BEGIN
1101             SELECT 'Y'
1102             INTO   l_error_exists
1103             FROM   csi_txn_errors
1104             WHERE  processed_flag in ('E','R')
1105             AND    inv_material_transaction_id = all_txn.mtl_txn_id
1106             AND    rownum < 2;
1107             EXIT;
1108           EXCEPTION
1109             WHEN no_data_found then
1110               l_error_exists := 'N';
1111           END;
1112         END IF;
1113       END LOOP;
1114 
1115       IF l_error_exists = 'N' THEN
1116 
1117         init_struct(
1118           px_inst_rec     => g_inst_rec,
1119           px_pty_tbl      => g_pty_tbl,
1120           px_pa_tbl       => g_pa_tbl,
1121           px_ou_tbl       => g_ou_tbl,
1122           px_prc_tbl      => g_prc_tbl,
1123           px_eav_tbl      => g_eav_tbl,
1124           px_ea_tbl       => g_ea_tbl,
1125           px_asset_tbl    => g_asset_tbl,
1126           px_vl_qry_rec   => g_vl_qry_rec,
1127           px_vl_tbl       => g_vl_tbl,
1128           px_iir_qry_rec  => g_iir_qry_rec,
1129           px_iir_tbl      => g_iir_tbl);
1130 
1131         BEGIN
1132 
1133           savepoint forward_sync;
1134 
1135           g_inst_rec.instance_id := fs_rec.instance_id;
1136 
1137           log('time stamp for old fetch : '||to_char(fs_rec.date_time_stamp, 'dd-mon-yyyy hh24:mi:ss'));
1138 
1139           csi_item_instance_pub.get_item_instance_details (
1140             p_api_version           => 1.0,
1141             p_commit                => fnd_api.g_false,
1142             p_init_msg_list         => fnd_api.g_true,
1143             p_validation_level      => fnd_api.g_valid_level_full,
1144             p_instance_rec          => g_inst_rec,
1145             p_get_parties           => fnd_api.g_true,
1146             p_party_header_tbl      => g_pty_tbl,
1147             p_get_accounts          => fnd_api.g_true,
1148             p_account_header_tbl    => g_pa_tbl,
1149             p_get_org_assignments   => fnd_api.g_true,
1150             p_org_header_tbl        => g_ou_tbl,
1151             p_get_pricing_attribs   => fnd_api.g_true,
1152             p_pricing_attrib_tbl    => g_prc_tbl,
1153             p_get_ext_attribs       => fnd_api.g_true,
1154             p_ext_attrib_tbl        => g_eav_tbl,
1155             p_ext_attrib_def_tbl    => g_ea_tbl,
1156             p_get_asset_assignments => fnd_api.g_true,
1157             p_asset_header_tbl      => g_asset_tbl,
1158             p_resolve_id_columns    => fnd_api.g_false,
1159             p_time_stamp            => fs_rec.date_time_stamp,
1160             x_return_status         => l_return_status,
1161             x_msg_count             => l_msg_count,
1162             x_msg_data              => l_msg_data);
1163 
1164           IF l_return_status <> fnd_api.g_ret_sts_success THEN
1165             RAISE fnd_api.g_exc_error;
1166           END IF;
1167 
1168           -- some missed out or wrong data columns - re-query and populate
1169           SELECT customer_view_flag,
1170                  merchant_view_flag,
1171                  quantity,
1172                  object_version_number
1173           INTO   g_inst_rec.customer_view_flag,
1174                  g_inst_rec.merchant_view_flag,
1175                  g_inst_rec.quantity,
1176                  g_inst_rec.object_version_number
1177           FROM   csi_item_instances
1178           WHERE  instance_id = g_inst_rec.instance_id;
1179 
1180           g_vl_qry_rec.instance_id := g_inst_rec.instance_id;
1181 
1182           -- get the version label on that day
1183           csi_item_instance_pub.get_version_labels(
1184             p_api_version             => 1.0,
1185             p_commit                  => fnd_api.g_false,
1186             p_init_msg_list           => fnd_api.g_true,
1187             p_validation_level        => fnd_api.g_valid_level_full,
1188             p_version_label_query_rec => g_vl_qry_rec,
1189             p_time_stamp              => fs_rec.date_time_stamp,
1190             x_version_label_tbl       => g_vl_tbl,
1191             x_return_status           => l_return_status,
1192             x_msg_count               => l_msg_count,
1193             x_msg_data                => l_msg_data);
1194 
1195           IF l_return_status <> fnd_api.g_ret_sts_success THEN
1196             RAISE fnd_api.g_exc_error;
1197           END IF;
1198 
1199           make_non_header(
1200             p_inst_h_rec            => g_inst_rec,
1201             p_pty_h_tbl             => g_pty_tbl,
1202             p_pa_h_tbl              => g_pa_tbl,
1203             p_ou_h_tbl              => g_ou_tbl,
1204             p_asset_h_tbl           => g_asset_tbl,
1205             x_inst_rec              => l_inst_rec,
1206             x_pty_tbl               => l_pty_tbl,
1207             x_pa_tbl                => l_pa_tbl,
1208             x_ou_tbl                => l_ou_tbl,
1209             x_asset_tbl             => l_asset_tbl,
1210             x_return_status         => l_return_status);
1211 
1212           l_csi_txn_rec.inv_material_transaction_id := fs_rec.mtl_txn_id;
1213 
1214           csi_item_instance_pub.update_item_instance(
1215             p_api_version           => 1.0,
1216             p_commit                => fnd_api.g_false,
1217             p_init_msg_list         => fnd_api.g_true,
1218             p_validation_level      => fnd_api.g_valid_level_full,
1219             p_instance_rec          => l_inst_rec,
1220             p_party_tbl             => l_pty_tbl,
1221             p_account_tbl           => l_pa_tbl,
1222             p_org_assignments_tbl   => l_ou_tbl,
1223             p_ext_attrib_values_tbl => l_eav_tbl,
1224             p_pricing_attrib_tbl    => l_prc_tbl,
1225             p_asset_assignment_tbl  => l_asset_tbl,
1226             p_txn_rec               => l_csi_txn_rec,
1227             x_instance_id_lst       => l_inst_ids_tbl,
1228             x_return_status         => l_return_status,
1229             x_msg_count             => l_msg_count,
1230             x_msg_data              => l_msg_data);
1231 
1232           IF l_return_status not in (fnd_api.g_ret_sts_success, 'W') THEN
1233             RAISE fnd_api.g_exc_error;
1234           END IF;
1235 
1236           l_vl_tbl := g_vl_tbl;
1237 
1238           IF l_vl_tbl.count > 0 THEN
1239 
1240             FOR l_ind IN l_vl_tbl.FIRST .. l_vl_tbl.LAST
1241             LOOP
1242               SELECT object_version_number
1243               INTO   l_vl_tbl(l_ind).object_version_number
1244               FROM   csi_i_version_labels
1245               WHERE  version_label_id = l_vl_tbl(l_ind).version_label_id;
1246             END LOOP;
1247 
1248             csi_item_instance_pub.update_version_label(
1249               p_api_version         => 1.0,
1250               p_commit              => fnd_api.g_false,
1251               p_init_msg_list       => fnd_api.g_true,
1252               p_validation_level    => fnd_api.g_valid_level_full,
1253               p_version_label_tbl   => l_vl_tbl,
1254               p_txn_rec             => l_csi_txn_rec,
1255               x_return_status       => l_return_status,
1256               x_msg_count           => l_msg_count,
1257               x_msg_data            => l_msg_data);
1258 
1259             IF l_return_status <> fnd_api.g_ret_sts_success THEN
1260               RAISE fnd_api.g_exc_error;
1261             END IF;
1262 
1263           END IF;
1264 
1265           g_iir_qry_rec.object_id              := g_inst_rec.instance_id;
1266           g_iir_qry_rec.relationship_type_code := 'COMPONENT-OF';
1267 
1268           csi_ii_relationships_pub.get_relationships(
1269             p_api_version                => 1.0,
1270             p_commit                     => fnd_api.g_false,
1271             p_init_msg_list              => fnd_api.g_true,
1272             p_validation_level           => fnd_api.g_valid_level_full,
1273             p_relationship_query_rec     => g_iir_qry_rec,
1274             p_depth                      => null,
1275             p_time_stamp                 => fs_rec.date_time_stamp,
1276             p_active_relationship_only   => fnd_api.g_false,
1277             x_relationship_tbl           => g_iir_tbl,
1278             x_return_status              => l_return_status,
1279             x_msg_count                  => l_msg_count,
1280             x_msg_data                   => l_msg_data);
1281 
1282           IF l_return_status <> fnd_api.g_ret_sts_success THEN
1283             RAISE fnd_api.g_exc_error;
1284           END IF;
1285 
1286           IF g_iir_tbl.count > 0 THEN
1287             FOR l_ind IN g_iir_tbl.FIRST .. g_iir_tbl.LAST
1288             LOOP
1289               iir_ind := iir_ind + 1;
1290               l_iir_tbl(iir_ind) := g_iir_tbl(l_ind);
1291 
1292               SELECT object_version_number
1293               INTO   l_iir_tbl(iir_ind).object_version_number
1294               FROM   csi_ii_relationships
1295               WHERE  relationship_id = l_iir_tbl(iir_ind).relationship_id;
1296             END LOOP;
1297           END IF;
1298 
1299           g_iir_qry_rec                        := l_iir_qry_rec;
1300           g_iir_tbl.delete;
1301 
1302           g_iir_qry_rec.subject_id             := g_inst_rec.instance_id;
1303           g_iir_qry_rec.relationship_type_code := 'COMPONENT-OF';
1304 
1305           csi_ii_relationships_pub.get_relationships(
1306             p_api_version                => 1.0,
1307             p_commit                     => fnd_api.g_false,
1308             p_init_msg_list              => fnd_api.g_true,
1309             p_validation_level           => fnd_api.g_valid_level_full,
1310             p_relationship_query_rec     => g_iir_qry_rec,
1311             p_depth                      => NULL,
1312             p_time_stamp                 => fs_rec.date_time_stamp,
1313             p_active_relationship_only   => fnd_api.g_false,
1314             x_relationship_tbl           => g_iir_tbl,
1315             x_return_status              => l_return_status,
1316             x_msg_count                  => l_msg_count,
1317             x_msg_data                   => l_msg_data);
1318 
1319           IF l_return_status <> fnd_api.g_ret_sts_success THEN
1320             RAISE fnd_api.g_exc_error;
1321           END IF;
1322 
1323           IF g_iir_tbl.count > 0 THEN
1324             FOR l_ind IN g_iir_tbl.FIRST .. g_iir_tbl.LAST
1325             LOOP
1326               iir_ind := iir_ind + 1;
1327               l_iir_tbl(iir_ind) := g_iir_tbl(l_ind);
1328               SELECT object_version_number
1329               INTO   l_iir_tbl(iir_ind).object_version_number
1330               FROM   csi_ii_relationships
1331               WHERE  relationship_id = l_iir_tbl(iir_ind).relationship_id;
1332 
1333             END LOOP;
1334           END IF;
1335 
1336           IF l_iir_tbl.COUNT > 0 THEN
1337             csi_ii_relationships_pub.update_relationship (
1338               p_api_version             => 1.0,
1339               p_commit                  => fnd_api.g_false,
1340               p_init_msg_list           => fnd_api.g_true,
1341               p_validation_level        => fnd_api.g_valid_level_full,
1342               p_relationship_tbl        => l_iir_tbl,
1343               p_txn_rec                 => l_csi_txn_rec,
1344               x_return_status           => l_return_status,
1345               x_msg_count               => l_msg_count,
1346               x_msg_data                => l_msg_data);
1347 
1348             IF l_return_status not in (fnd_api.g_ret_sts_success, 'W') THEN
1349               RAISE fnd_api.g_exc_error;
1350             END IF;
1351           END IF;
1352 
1353           UPDATE csi_ii_forward_sync_temp
1354           SET    process_flag = 'P'
1355           WHERE  instance_id = fs_rec.instance_id;
1356 
1357         EXCEPTION
1358           WHEN fnd_api.g_exc_error THEN
1359             rollback to forward_sync;
1360             l_msg_data := csi_t_gen_utility_pvt.dump_error_stack;
1361             log('Error in forward_sync : '||fs_rec.instance_id||' : '||l_msg_data);
1362         END;
1363       END IF;
1364 
1365     END LOOP;
1366 
1367   EXCEPTION
1368     when others then
1369       log('OTHERS exception from forward sync : '||sqlerrm);
1370   END forward_sync;
1371   --
1372   PROCEDURE insert_full_dump(p_instance_id    IN NUMBER) IS
1373     p_instance_rec       CSI_DATASTRUCTURES_PUB.INSTANCE_HEADER_REC;
1374     p_temp_instance_rec  CSI_DATASTRUCTURES_PUB.INSTANCE_HEADER_REC;
1375     p_party_header_tbl   CSI_DATASTRUCTURES_PUB.PARTY_HEADER_TBL;
1376     p_account_header_tbl CSI_DATASTRUCTURES_PUB.PARTY_ACCOUNT_HEADER_TBL;
1377     p_org_header_tbl     CSI_DATASTRUCTURES_PUB.ORG_UNITS_HEADER_TBL;
1378     p_pricing_attrib_tbl CSI_DATASTRUCTURES_PUB.PRICING_ATTRIBS_TBL;
1379     p_ext_attrib_tbl     CSI_DATASTRUCTURES_PUB.EXTEND_ATTRIB_VALUES_TBL;
1380     p_ext_attrib_def_tbl CSI_DATASTRUCTURES_PUB.EXTEND_ATTRIB_TBL;
1381     p_asset_header_tbl   CSI_DATASTRUCTURES_PUB.INSTANCE_ASSET_HEADER_TBL;
1382     p_ver_label_query_rec CSI_DATASTRUCTURES_PUB.VERSION_LABEL_QUERY_REC;
1383     p_rel_query_rec      CSI_DATASTRUCTURES_PUB.RELATIONSHIP_QUERY_REC;
1384     p_temp_rel_query_rec CSI_DATASTRUCTURES_PUB.RELATIONSHIP_QUERY_REC;
1385     x_version_label_tbl  csi_datastructures_pub.version_label_tbl;
1386     x_relationship_tbl   csi_datastructures_pub.ii_relationship_tbl;
1387     l_init_msg_list      VARCHAR2(2000);
1388     x_return_status      VARCHAR2(2000);
1389     x_msg_count          NUMBER;
1390     x_msg_data           VARCHAR2(2000);
1391     l_msg_index          NUMBER;
1392     l_msg_count          NUMBER;
1393      --
1394     l_instance_tbl          csi_datastructures_pub.instance_tbl;
1395     l_party_tbl             csi_datastructures_pub.party_tbl;
1396     l_party_account_tbl     csi_datastructures_pub.party_account_tbl;
1397     l_org_units_tbl         csi_datastructures_pub.organization_units_tbl;
1398     l_pricing_attribs_tbl   csi_datastructures_pub.pricing_attribs_tbl;
1399     l_ext_attrib_values_tbl csi_datastructures_pub.extend_attrib_values_tbl;
1400     l_instance_asset_tbl    csi_datastructures_pub.instance_asset_tbl;
1401     l_version_label_tbl     csi_datastructures_pub.version_label_tbl;
1402     l_ii_relationship_tbl   csi_datastructures_pub.ii_relationship_tbl;
1403     --
1404     l_inst_rec_tab                csi_diagnostics_pkg.instance_rec_tab;
1405     l_version_label_rec_tab       csi_diagnostics_pkg.version_label_rec_tab;
1406     l_party_rec_tab               csi_diagnostics_pkg.party_rec_tab;
1407     l_account_rec_tab             csi_diagnostics_pkg.account_rec_tab;
1408     l_ext_attr_rec_tab            csi_diagnostics_pkg.extend_attrib_values_rec_tab;
1409     l_asset_rec_tab               csi_diagnostics_pkg.instance_asset_rec_tab;
1410     l_org_units_rec_tab           csi_diagnostics_pkg.org_units_rec_tab;
1411     l_pricing_rec_tab             csi_diagnostics_pkg.pricing_attribs_rec_tab;
1412     l_ii_relationship_rec_tab     csi_diagnostics_pkg.ii_relationship_rec_tab;
1413     --
1414     l_inst_hist_tbl               csi_diagnostics_pkg.T_NUM;
1415     l_party_hist_tbl              csi_diagnostics_pkg.T_NUM;
1416     l_account_hist_tbl            csi_diagnostics_pkg.T_NUM;
1417     l_org_hist_tbl                csi_diagnostics_pkg.T_NUM;
1418     l_ext_hist_tbl                csi_diagnostics_pkg.T_NUM;
1419     l_asset_hist_tbl              csi_diagnostics_pkg.T_NUM;
1420     l_pricing_hist_tbl            csi_diagnostics_pkg.T_NUM;
1421     l_rel_hist_tbl                csi_diagnostics_pkg.T_NUM;
1422     l_ver_label_hist_tbl          csi_diagnostics_pkg.T_NUM;
1423     --
1424     l_owner_src_table             VARCHAR2(30);
1425     l_owner_party                 NUMBER;
1426     l_owner_account               NUMBER;
1427     l_user_id                     NUMBER := fnd_global.user_id;
1428     l_txn_id                      NUMBER;
1429     v_txn_type_id                 NUMBER;
1430     l_char_ins_id                 VARCHAR2(50);
1431     --
1432     l_ctr                 NUMBER;
1433     l_exists              VARCHAR2(1);
1434     --
1435     Process_next          EXCEPTION;
1436     Comp_error            EXCEPTION;
1437     --
1438   BEGIN
1439     savepoint Insert_Full_Dump;
1440     IF p_instance_id IS NULL OR p_instance_id = FND_API.G_MISS_NUM THEN
1441       Raise comp_error;
1442     END IF;
1443     -- Get the Transaction Type ID for Txn Type MIGRATED
1444     v_txn_type_id := correction_txn_type_id;
1445 
1446     SELECT csi_transactions_s.nextval
1447     INTO   l_txn_id
1448     FROM   sys.dual;
1449 
1450     --
1451     BEGIN
1452       p_instance_rec := p_temp_instance_rec;
1453       p_party_header_tbl.DELETE;
1454       p_account_header_tbl.DELETE;
1455       p_org_header_tbl.DELETE;
1456       p_pricing_attrib_tbl.DELETE;
1457       p_ext_attrib_tbl.DELETE;
1458       p_ext_attrib_def_tbl.DELETE;
1459       p_asset_header_tbl.DELETE;
1460       x_version_label_tbl.DELETE;
1461       x_relationship_tbl.DELETE;
1462       --
1463       l_exists := 'N';
1464       p_instance_rec.instance_id := p_instance_id;
1465       l_char_ins_id := to_char(p_instance_id);
1466 
1467       -- Call Get API with the time stamp
1468       csi_item_instance_pub.get_item_instance_details (
1469         p_api_version           => 1.0,
1470         p_commit                => fnd_api.g_false,
1471         p_init_msg_list         => fnd_api.g_true,
1472         p_validation_level      => fnd_api.g_valid_level_full,
1473         p_instance_rec          => p_instance_rec,
1474         p_get_parties           => fnd_api.g_true,
1475         p_party_header_tbl      => p_party_header_tbl,
1476         p_get_accounts          => fnd_api.g_true,
1477         p_account_header_tbl    => p_account_header_tbl,
1478         p_get_org_assignments   => fnd_api.g_true,
1479         p_org_header_tbl        => p_org_header_tbl,
1480         p_get_pricing_attribs   => fnd_api.g_true,
1481         p_pricing_attrib_tbl    => p_pricing_attrib_tbl,
1482         p_get_ext_attribs       => fnd_api.g_true,
1483         p_ext_attrib_tbl        => p_ext_attrib_tbl,
1484         p_ext_attrib_def_tbl    => p_ext_attrib_def_tbl,
1485         p_get_asset_assignments => fnd_api.g_true,
1486         p_asset_header_tbl      => p_asset_header_tbl,
1487         p_resolve_id_columns    => fnd_api.g_false,
1488         p_time_stamp            => sysdate,
1489         x_return_status         => x_return_status,
1490         x_msg_count             => x_msg_count,
1491         x_msg_data              => x_msg_data);
1492       --
1493       IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1494         l_msg_index := 1;
1495         l_msg_count := x_msg_count;
1496         WHILE l_msg_count > 0 LOOP
1497           x_msg_data := FND_MSG_PUB.GET (  l_msg_index, FND_API.G_FALSE );
1498           l_msg_index := l_msg_index + 1;
1499           l_msg_count := l_msg_count - 1;
1500         END LOOP;
1501         log('Error in Get_Item_Instance Details for Instance : '||l_char_ins_id||'  '||x_msg_data);
1502         Raise Process_next;
1503       END IF;
1504       -- Build the Regular tables using the respective header tables
1505       -- Get the Customer and Merchant view flags
1506 
1507       SELECT customer_view_flag,
1508              merchant_view_flag,
1509              quantity
1510       INTO   p_instance_rec.customer_view_flag,
1511              p_instance_rec.merchant_view_flag,
1512              p_instance_rec.quantity
1513       FROM   csi_item_instances
1514       WHERE  instance_id = p_instance_rec.instance_id;
1515       --
1516       l_ctr := l_instance_tbl.count;
1517       l_ctr := l_ctr + 1;
1518       --
1519       l_instance_tbl(l_ctr).INSTANCE_ID  := p_instance_rec.INSTANCE_ID;
1520       l_instance_tbl(l_ctr).INSTANCE_NUMBER  := p_instance_rec.INSTANCE_NUMBER;
1521       l_instance_tbl(l_ctr).EXTERNAL_REFERENCE  := p_instance_rec.EXTERNAL_REFERENCE;
1522       l_instance_tbl(l_ctr).INVENTORY_ITEM_ID  := p_instance_rec.INVENTORY_ITEM_ID;
1523       l_instance_tbl(l_ctr).VLD_ORGANIZATION_ID  := p_instance_rec.VLD_ORGANIZATION_ID;
1524       l_instance_tbl(l_ctr).INVENTORY_REVISION  := p_instance_rec.INVENTORY_REVISION;
1525       l_instance_tbl(l_ctr).INV_MASTER_ORGANIZATION_ID  := p_instance_rec.INV_MASTER_ORGANIZATION_ID;
1526       l_instance_tbl(l_ctr).SERIAL_NUMBER  := p_instance_rec.SERIAL_NUMBER;
1527       l_instance_tbl(l_ctr).MFG_SERIAL_NUMBER_FLAG  := p_instance_rec.MFG_SERIAL_NUMBER_FLAG;
1528       l_instance_tbl(l_ctr).LOT_NUMBER  := p_instance_rec.LOT_NUMBER;
1529       l_instance_tbl(l_ctr).QUANTITY  := p_instance_rec.QUANTITY;
1530       l_instance_tbl(l_ctr).UNIT_OF_MEASURE  := p_instance_rec.UNIT_OF_MEASURE;
1531       l_instance_tbl(l_ctr).ACCOUNTING_CLASS_CODE  := p_instance_rec.ACCOUNTING_CLASS_CODE;
1532       l_instance_tbl(l_ctr).INSTANCE_CONDITION_ID  := p_instance_rec.INSTANCE_CONDITION_ID;
1533       l_instance_tbl(l_ctr).INSTANCE_STATUS_ID  := p_instance_rec.INSTANCE_STATUS_ID;
1534       l_instance_tbl(l_ctr).CUSTOMER_VIEW_FLAG  := p_instance_rec.CUSTOMER_VIEW_FLAG;
1535       l_instance_tbl(l_ctr).MERCHANT_VIEW_FLAG  := p_instance_rec.MERCHANT_VIEW_FLAG;
1536       l_instance_tbl(l_ctr).SELLABLE_FLAG  := p_instance_rec.SELLABLE_FLAG;
1537       l_instance_tbl(l_ctr).SYSTEM_ID  := p_instance_rec.SYSTEM_ID;
1538       l_instance_tbl(l_ctr).INSTANCE_TYPE_CODE  := p_instance_rec.INSTANCE_TYPE_CODE;
1539       l_instance_tbl(l_ctr).ACTIVE_START_DATE  := p_instance_rec.ACTIVE_START_DATE;
1540       l_instance_tbl(l_ctr).ACTIVE_END_DATE  := p_instance_rec.ACTIVE_END_DATE;
1541       l_instance_tbl(l_ctr).LOCATION_TYPE_CODE  := p_instance_rec.LOCATION_TYPE_CODE;
1542       l_instance_tbl(l_ctr).LOCATION_ID  := p_instance_rec.LOCATION_ID;
1543       l_instance_tbl(l_ctr).INV_ORGANIZATION_ID  := p_instance_rec.INV_ORGANIZATION_ID;
1544       l_instance_tbl(l_ctr).INV_SUBINVENTORY_NAME  := p_instance_rec.INV_SUBINVENTORY_NAME;
1545       l_instance_tbl(l_ctr).INV_LOCATOR_ID  := p_instance_rec.INV_LOCATOR_ID;
1546       l_instance_tbl(l_ctr).PA_PROJECT_ID  := p_instance_rec.PA_PROJECT_ID;
1547       l_instance_tbl(l_ctr).PA_PROJECT_TASK_ID  := p_instance_rec.PA_PROJECT_TASK_ID;
1548       l_instance_tbl(l_ctr).IN_TRANSIT_ORDER_LINE_ID  := p_instance_rec.IN_TRANSIT_ORDER_LINE_ID;
1549       l_instance_tbl(l_ctr).WIP_JOB_ID  := p_instance_rec.WIP_JOB_ID;
1550       l_instance_tbl(l_ctr).PO_ORDER_LINE_ID  := p_instance_rec.PO_ORDER_LINE_ID;
1551       l_instance_tbl(l_ctr).LAST_OE_ORDER_LINE_ID  := p_instance_rec.LAST_OE_ORDER_LINE_ID;
1552       l_instance_tbl(l_ctr).LAST_OE_RMA_LINE_ID  := p_instance_rec.LAST_OE_RMA_LINE_ID;
1553       l_instance_tbl(l_ctr).LAST_PO_PO_LINE_ID  := p_instance_rec.LAST_PO_PO_LINE_ID;
1554       l_instance_tbl(l_ctr).LAST_OE_PO_NUMBER  := p_instance_rec.LAST_OE_PO_NUMBER;
1555       l_instance_tbl(l_ctr).LAST_WIP_JOB_ID  := p_instance_rec.LAST_WIP_JOB_ID;
1556       l_instance_tbl(l_ctr).LAST_PA_PROJECT_ID  := p_instance_rec.LAST_PA_PROJECT_ID;
1557       l_instance_tbl(l_ctr).LAST_PA_TASK_ID  := p_instance_rec.LAST_PA_TASK_ID;
1558       l_instance_tbl(l_ctr).LAST_OE_AGREEMENT_ID  := p_instance_rec.LAST_OE_AGREEMENT_ID;
1559       l_instance_tbl(l_ctr).INSTALL_DATE  := p_instance_rec.INSTALL_DATE;
1560       l_instance_tbl(l_ctr).MANUALLY_CREATED_FLAG  := p_instance_rec.MANUALLY_CREATED_FLAG;
1561       l_instance_tbl(l_ctr).RETURN_BY_DATE  := p_instance_rec.RETURN_BY_DATE;
1562       l_instance_tbl(l_ctr).ACTUAL_RETURN_DATE  := p_instance_rec.ACTUAL_RETURN_DATE;
1563       l_instance_tbl(l_ctr).CREATION_COMPLETE_FLAG  := p_instance_rec.CREATION_COMPLETE_FLAG;
1564       l_instance_tbl(l_ctr).COMPLETENESS_FLAG  := p_instance_rec.COMPLETENESS_FLAG;
1565       l_instance_tbl(l_ctr).CONTEXT  := p_instance_rec.CONTEXT;
1566       l_instance_tbl(l_ctr).ATTRIBUTE1  := p_instance_rec.ATTRIBUTE1;
1567       l_instance_tbl(l_ctr).ATTRIBUTE2  := p_instance_rec.ATTRIBUTE2;
1568       l_instance_tbl(l_ctr).ATTRIBUTE3  := p_instance_rec.ATTRIBUTE3;
1569       l_instance_tbl(l_ctr).ATTRIBUTE4  := p_instance_rec.ATTRIBUTE4;
1570       l_instance_tbl(l_ctr).ATTRIBUTE5  := p_instance_rec.ATTRIBUTE5;
1571       l_instance_tbl(l_ctr).ATTRIBUTE6  := p_instance_rec.ATTRIBUTE6;
1572       l_instance_tbl(l_ctr).ATTRIBUTE7  := p_instance_rec.ATTRIBUTE7;
1573       l_instance_tbl(l_ctr).ATTRIBUTE8  := p_instance_rec.ATTRIBUTE8;
1574       l_instance_tbl(l_ctr).ATTRIBUTE9  := p_instance_rec.ATTRIBUTE9;
1575       l_instance_tbl(l_ctr).ATTRIBUTE10  := p_instance_rec.ATTRIBUTE10;
1576       l_instance_tbl(l_ctr).ATTRIBUTE11  := p_instance_rec.ATTRIBUTE11;
1577       l_instance_tbl(l_ctr).ATTRIBUTE12  := p_instance_rec.ATTRIBUTE12;
1578       l_instance_tbl(l_ctr).ATTRIBUTE13  := p_instance_rec.ATTRIBUTE13;
1579       l_instance_tbl(l_ctr).ATTRIBUTE14  := p_instance_rec.ATTRIBUTE14;
1580       l_instance_tbl(l_ctr).ATTRIBUTE15  := p_instance_rec.ATTRIBUTE15;
1581       l_instance_tbl(l_ctr).OBJECT_VERSION_NUMBER  := p_instance_rec.OBJECT_VERSION_NUMBER;
1582       l_instance_tbl(l_ctr).LAST_TXN_LINE_DETAIL_ID  := p_instance_rec.LAST_TXN_LINE_DETAIL_ID;
1583       l_instance_tbl(l_ctr).INSTALL_LOCATION_TYPE_CODE  := p_instance_rec.INSTALL_LOCATION_TYPE_CODE;
1584       l_instance_tbl(l_ctr).INSTALL_LOCATION_ID  := p_instance_rec.INSTALL_LOCATION_ID;
1585       l_instance_tbl(l_ctr).INSTANCE_USAGE_CODE  := p_instance_rec.INSTANCE_USAGE_CODE;
1586       l_instance_tbl(l_ctr).CONFIG_INST_HDR_ID  := p_instance_rec.CONFIG_INST_HDR_ID;
1587       l_instance_tbl(l_ctr).CONFIG_INST_REV_NUM  := p_instance_rec.CONFIG_INST_REV_NUM;
1588       l_instance_tbl(l_ctr).CONFIG_INST_ITEM_ID  := p_instance_rec.CONFIG_INST_ITEM_ID;
1589       l_instance_tbl(l_ctr).CONFIG_VALID_STATUS  := p_instance_rec.CONFIG_VALID_STATUS;
1590       l_instance_tbl(l_ctr).INSTANCE_DESCRIPTION  := p_instance_rec.INSTANCE_DESCRIPTION;
1591       -- Build Party Table
1592       IF p_party_header_tbl.count > 0 THEN
1593         l_ctr := l_party_tbl.count;
1594         FOR i in p_party_header_tbl.FIRST .. p_party_header_tbl.LAST LOOP
1595           l_ctr := l_ctr + 1;
1596           --
1597           l_party_tbl(l_ctr).instance_party_id   := p_party_header_tbl(i).instance_party_id;
1598           l_party_tbl(l_ctr).instance_id   := p_party_header_tbl(i).instance_id;
1599           l_party_tbl(l_ctr).party_source_table   := p_party_header_tbl(i).party_source_table;
1600           l_party_tbl(l_ctr).party_id   := p_party_header_tbl(i).party_id;
1601           l_party_tbl(l_ctr).relationship_type_code   := p_party_header_tbl(i).relationship_type_code;
1602           l_party_tbl(l_ctr).contact_flag   := p_party_header_tbl(i).contact_flag;
1603           l_party_tbl(l_ctr).contact_ip_id   := p_party_header_tbl(i).contact_ip_id;
1604           l_party_tbl(l_ctr).active_start_date   := p_party_header_tbl(i).active_start_date;
1605           l_party_tbl(l_ctr).active_end_date   := p_party_header_tbl(i).active_end_date;
1606           l_party_tbl(l_ctr).context   := p_party_header_tbl(i).context;
1607           l_party_tbl(l_ctr).attribute1   := p_party_header_tbl(i).attribute1;
1608           l_party_tbl(l_ctr).attribute2   := p_party_header_tbl(i).attribute2;
1609           l_party_tbl(l_ctr).attribute3   := p_party_header_tbl(i).attribute3;
1610           l_party_tbl(l_ctr).attribute4   := p_party_header_tbl(i).attribute4;
1611           l_party_tbl(l_ctr).attribute5   := p_party_header_tbl(i).attribute5;
1612           l_party_tbl(l_ctr).attribute6   := p_party_header_tbl(i).attribute6;
1613           l_party_tbl(l_ctr).attribute7   := p_party_header_tbl(i).attribute7;
1614           l_party_tbl(l_ctr).attribute8   := p_party_header_tbl(i).attribute8;
1615           l_party_tbl(l_ctr).attribute9   := p_party_header_tbl(i).attribute9;
1616           l_party_tbl(l_ctr).attribute10   := p_party_header_tbl(i).attribute10;
1617           l_party_tbl(l_ctr).attribute11   := p_party_header_tbl(i).attribute11;
1618           l_party_tbl(l_ctr).attribute12   := p_party_header_tbl(i).attribute12;
1619           l_party_tbl(l_ctr).attribute13   := p_party_header_tbl(i).attribute13;
1620           l_party_tbl(l_ctr).attribute14   := p_party_header_tbl(i).attribute14;
1621           l_party_tbl(l_ctr).attribute15   := p_party_header_tbl(i).attribute15;
1622           l_party_tbl(l_ctr).object_version_number   := p_party_header_tbl(i).object_version_number;
1623           l_party_tbl(l_ctr).primary_flag   := p_party_header_tbl(i).primary_flag;
1624           l_party_tbl(l_ctr).preferred_flag   := p_party_header_tbl(i).preferred_flag;
1625         END LOOP;
1626       END IF;
1627       --
1628       -- Build Account Table from Account Header Table
1629       IF p_account_header_tbl.count > 0 THEN
1630         l_ctr := l_party_account_tbl.count;
1631         FOR i in p_account_header_tbl.FIRST .. p_account_header_tbl.LAST LOOP
1632           l_ctr := l_ctr + 1;
1633           --
1634           l_party_account_tbl(l_ctr).ip_account_id := p_account_header_tbl(i).ip_account_id;
1635           l_party_account_tbl(l_ctr).instance_party_id := p_account_header_tbl(i).instance_party_id;
1636           l_party_account_tbl(l_ctr).party_account_id := p_account_header_tbl(i).party_account_id;
1637           l_party_account_tbl(l_ctr).relationship_type_code := p_account_header_tbl(i).relationship_type_code;
1638           l_party_account_tbl(l_ctr).bill_to_address := p_account_header_tbl(i).bill_to_address;
1639           l_party_account_tbl(l_ctr).ship_to_address := p_account_header_tbl(i).ship_to_address;
1640           l_party_account_tbl(l_ctr).active_start_date := p_account_header_tbl(i).active_start_date;
1641           l_party_account_tbl(l_ctr).active_end_date := p_account_header_tbl(i).active_end_date;
1642           l_party_account_tbl(l_ctr).context := p_account_header_tbl(i).context;
1643           l_party_account_tbl(l_ctr).attribute1 := p_account_header_tbl(i).attribute1;
1644           l_party_account_tbl(l_ctr).attribute2 := p_account_header_tbl(i).attribute2;
1645           l_party_account_tbl(l_ctr).attribute3 := p_account_header_tbl(i).attribute3;
1646           l_party_account_tbl(l_ctr).attribute4 := p_account_header_tbl(i).attribute4;
1647           l_party_account_tbl(l_ctr).attribute5 := p_account_header_tbl(i).attribute5;
1648           l_party_account_tbl(l_ctr).attribute6 := p_account_header_tbl(i).attribute6;
1649           l_party_account_tbl(l_ctr).attribute7 := p_account_header_tbl(i).attribute7;
1650           l_party_account_tbl(l_ctr).attribute8 := p_account_header_tbl(i).attribute8;
1651           l_party_account_tbl(l_ctr).attribute9 := p_account_header_tbl(i).attribute9;
1652           l_party_account_tbl(l_ctr).attribute10 := p_account_header_tbl(i).attribute10;
1653           l_party_account_tbl(l_ctr).attribute11 := p_account_header_tbl(i).attribute11;
1654           l_party_account_tbl(l_ctr).attribute12 := p_account_header_tbl(i).attribute12;
1655           l_party_account_tbl(l_ctr).attribute13 := p_account_header_tbl(i).attribute13;
1656           l_party_account_tbl(l_ctr).attribute14 := p_account_header_tbl(i).attribute14;
1657           l_party_account_tbl(l_ctr).attribute15 := p_account_header_tbl(i).attribute15;
1658           l_party_account_tbl(l_ctr).object_version_number := p_account_header_tbl(i).object_version_number;
1659         END LOOP;
1660       END IF;
1661       -- Build org Assignments table
1662       IF p_org_header_tbl.count > 0 THEN
1663         l_ctr := l_org_units_tbl.count;
1664         FOR i in p_org_header_tbl.FIRST .. p_org_header_tbl.LAST LOOP
1665           l_ctr := l_ctr + 1;
1666           --
1667           l_org_units_tbl(l_ctr).instance_ou_id := p_org_header_tbl(i).instance_ou_id;
1668           l_org_units_tbl(l_ctr).instance_id := p_org_header_tbl(i).instance_id;
1669           l_org_units_tbl(l_ctr).operating_unit_id := p_org_header_tbl(i).operating_unit_id;
1670           l_org_units_tbl(l_ctr).relationship_type_code := p_org_header_tbl(i).relationship_type_code;
1671           l_org_units_tbl(l_ctr).active_start_date := p_org_header_tbl(i).active_start_date;
1672           l_org_units_tbl(l_ctr).active_end_date := p_org_header_tbl(i).active_end_date;
1673           l_org_units_tbl(l_ctr).context := p_org_header_tbl(i).context;
1674           l_org_units_tbl(l_ctr).attribute1 := p_org_header_tbl(i).attribute1;
1675           l_org_units_tbl(l_ctr).attribute2 := p_org_header_tbl(i).attribute2;
1676           l_org_units_tbl(l_ctr).attribute3 := p_org_header_tbl(i).attribute3;
1677           l_org_units_tbl(l_ctr).attribute4 := p_org_header_tbl(i).attribute4;
1678           l_org_units_tbl(l_ctr).attribute5 := p_org_header_tbl(i).attribute5;
1679           l_org_units_tbl(l_ctr).attribute6 := p_org_header_tbl(i).attribute6;
1680           l_org_units_tbl(l_ctr).attribute7 := p_org_header_tbl(i).attribute7;
1681           l_org_units_tbl(l_ctr).attribute8 := p_org_header_tbl(i).attribute8;
1682           l_org_units_tbl(l_ctr).attribute9 := p_org_header_tbl(i).attribute9;
1683           l_org_units_tbl(l_ctr).attribute10 := p_org_header_tbl(i).attribute10;
1684           l_org_units_tbl(l_ctr).attribute11 := p_org_header_tbl(i).attribute11;
1685           l_org_units_tbl(l_ctr).attribute12 := p_org_header_tbl(i).attribute12;
1686           l_org_units_tbl(l_ctr).attribute13 := p_org_header_tbl(i).attribute13;
1687           l_org_units_tbl(l_ctr).attribute14 := p_org_header_tbl(i).attribute14;
1688           l_org_units_tbl(l_ctr).attribute15 := p_org_header_tbl(i).attribute15;
1689           l_org_units_tbl(l_ctr).object_version_number := p_org_header_tbl(i).object_version_number;
1690         END LOOP;
1691       END IF;
1692       -- Build Pricing Attrib Table
1693       IF p_pricing_attrib_tbl.count > 0 THEN
1694         l_ctr := l_pricing_attribs_tbl.count;
1695         FOR i in p_pricing_attrib_tbl.FIRST .. p_pricing_attrib_tbl.LAST LOOP
1696           l_ctr := l_ctr + 1;
1697           l_pricing_attribs_tbl(l_ctr) := p_pricing_attrib_tbl(i);
1698         END LOOP;
1699       END IF;
1700 
1701       -- Build Extended Attributes Table
1702       IF p_ext_attrib_tbl.count > 0 THEN
1703         l_ctr := l_ext_attrib_values_tbl.count;
1704         FOR i in p_ext_attrib_tbl.FIRST .. p_ext_attrib_tbl.LAST LOOP
1705           l_ctr := l_ctr + 1;
1706           l_ext_attrib_values_tbl(l_ctr) := p_ext_attrib_tbl(i);
1707         END LOOP;
1708       END IF;
1709 
1710       -- Build Instance Asset Table
1711       IF p_asset_header_tbl.count > 0 THEN
1712         l_ctr := l_instance_asset_tbl.count;
1713         FOR i in p_asset_header_tbl.FIRST .. p_asset_header_tbl.LAST LOOP
1714           l_ctr := l_ctr + 1;
1715           --
1716           l_instance_asset_tbl(l_ctr).instance_asset_id := p_asset_header_tbl(i).instance_asset_id;
1717           l_instance_asset_tbl(l_ctr).instance_id := p_asset_header_tbl(i).instance_id;
1718           l_instance_asset_tbl(l_ctr).fa_asset_id := p_asset_header_tbl(i).fa_asset_id;
1719           l_instance_asset_tbl(l_ctr).fa_book_type_code := p_asset_header_tbl(i).fa_book_type_code;
1720           l_instance_asset_tbl(l_ctr).fa_location_id := p_asset_header_tbl(i).fa_location_id;
1721           l_instance_asset_tbl(l_ctr).asset_quantity := p_asset_header_tbl(i).asset_quantity;
1722           l_instance_asset_tbl(l_ctr).update_status := p_asset_header_tbl(i).update_status;
1723           l_instance_asset_tbl(l_ctr).active_start_date := p_asset_header_tbl(i).active_start_date;
1724           l_instance_asset_tbl(l_ctr).active_end_date := p_asset_header_tbl(i).active_end_date;
1725           l_instance_asset_tbl(l_ctr).object_version_number := p_asset_header_tbl(i).object_version_number;
1726         END LOOP;
1727       END IF;
1728 
1729       --   Add version Label. Use Get_Version_Label API
1730       p_ver_label_query_rec.instance_id := p_instance_id;
1731       csi_item_instance_pub.get_version_labels (
1732         p_api_version             => 1.0,
1733         p_commit                  => fnd_api.g_false,
1734         p_init_msg_list           => l_init_msg_list,
1735         p_validation_level        => fnd_api.g_valid_level_full,
1736         p_version_label_query_rec => p_ver_label_query_rec,
1737         p_time_stamp              => fnd_api.g_miss_date,
1738         x_version_label_tbl       => x_version_label_tbl,
1739         x_return_status           => x_return_status,
1740         x_msg_count               => x_msg_count,
1741         x_msg_data                => x_msg_data);
1742 
1743       IF x_return_status = FND_API.G_RET_STS_SUCCESS THEN
1744         IF x_version_label_tbl.count > 0 THEN
1745           l_ctr := l_version_label_tbl.count;
1746           FOR i in x_version_label_tbl.FIRST .. x_version_label_tbl.LAST LOOP
1747             l_ctr := l_ctr + 1;
1748             l_version_label_tbl(l_ctr) := x_version_label_tbl(i);
1749           END LOOP;
1750         END IF;
1751       END IF;
1752 
1753       --
1754       -- Add Get Relationships API
1755       p_rel_query_rec := p_temp_rel_query_rec;
1756       p_rel_query_rec.object_id := p_instance_id;
1757       p_rel_query_rec.relationship_type_code := 'COMPONENT-OF';
1758       csi_ii_relationships_pub.get_relationships(
1759         p_api_version                => 1.0,
1760         p_commit                     => fnd_api.g_false,
1761         p_init_msg_list              => l_init_msg_list,
1762         p_validation_level           => fnd_api.g_valid_level_full,
1763         p_relationship_query_rec     => p_rel_query_rec,
1764         p_depth                      => NULL,
1765         p_time_stamp                 => fnd_api.g_miss_date,
1766         p_active_relationship_only   => fnd_api.g_true, -- BUG#5897084
1767         x_relationship_tbl           => x_relationship_tbl,
1768         x_return_status              => x_return_status,
1769         x_msg_count                  => x_msg_count,
1770         x_msg_data                   => x_msg_data);
1771       IF x_return_status = FND_API.G_RET_STS_SUCCESS THEN
1772         IF x_relationship_tbl.count > 0 THEN
1773           FOR i in x_relationship_tbl.FIRST .. x_relationship_tbl.LAST LOOP
1774             l_exists := 'N';
1775             IF l_ii_relationship_tbl.count > 0 THEN
1776               FOR k in l_ii_relationship_tbl.FIRST .. l_ii_relationship_tbl.LAST LOOP
1777                 IF l_ii_relationship_tbl(k).relationship_id = x_relationship_tbl(i).relationship_id THEN
1778                   l_exists := 'Y';
1779                   exit;
1780                 END IF;
1781               END LOOP;
1782             END IF;
1783             IF l_exists <> 'Y' THEN
1784               l_ctr := l_ii_relationship_tbl.count;
1785               l_ctr := l_ctr + 1;
1786               l_ii_relationship_tbl(l_ctr) := x_relationship_tbl(i);
1787             END IF;
1788           END LOOP;
1789         END IF;
1790       END IF;
1791       --
1792       p_rel_query_rec := p_temp_rel_query_rec;
1793       p_rel_query_rec.subject_id := p_instance_id;
1794       p_rel_query_rec.relationship_type_code := 'COMPONENT-OF';
1795       csi_ii_relationships_pub.get_relationships(
1796         p_api_version                => 1.0,
1797         p_commit                     => fnd_api.g_false,
1798         p_init_msg_list              => l_init_msg_list,
1799         p_validation_level           => fnd_api.g_valid_level_full,
1800         p_relationship_query_rec     => p_rel_query_rec,
1801         p_depth                      => NULL,
1802         p_time_stamp                 => fnd_api.g_miss_date,
1803         p_active_relationship_only   => fnd_api.g_true,  -- BUG#5897084
1804         x_relationship_tbl           => x_relationship_tbl,
1805         x_return_status              => x_return_status,
1806         x_msg_count                  => x_msg_count,
1807         x_msg_data                   => x_msg_data);
1808       IF x_return_status = FND_API.G_RET_STS_SUCCESS THEN
1809         IF x_relationship_tbl.count > 0 THEN
1810           FOR i in x_relationship_tbl.FIRST .. x_relationship_tbl.LAST LOOP
1811             l_exists := 'N';
1812             IF l_ii_relationship_tbl.count > 0 THEN
1813               FOR k in l_ii_relationship_tbl.FIRST .. l_ii_relationship_tbl.LAST LOOP
1814                 IF l_ii_relationship_tbl(k).relationship_id = x_relationship_tbl(i).relationship_id THEN
1815                   l_exists := 'Y';
1816                   exit;
1817                 END IF;
1818               END LOOP;
1819             END IF;
1820             IF l_exists <> 'Y' THEN
1821               l_ctr := l_ii_relationship_tbl.count;
1822               l_ctr := l_ctr + 1;
1823               l_ii_relationship_tbl(l_ctr) := x_relationship_tbl(i);
1824             END IF;
1825           END LOOP;
1826         END IF;
1827       END IF;
1828       --
1829     EXCEPTION
1830        when Process_next then
1831          null;
1832     End;
1833 
1834     --
1835     IF l_instance_tbl.count > 0 THEN
1836       log('Before Build_Inst_Rec_of_Table');
1837       build_inst_rec_of_table (
1838         p_inst_tbl      => l_instance_tbl ,
1839         p_inst_rec_tab  => l_inst_rec_tab ,
1840         p_inst_hist_tbl => l_inst_hist_tbl);
1841       --
1842       l_ctr := l_inst_rec_tab.instance_id.count;
1843       -- Insert into History
1844       log('Before Inserting into Instances history..');
1845       FORALL i in 1 .. l_inst_rec_tab.instance_id.count
1846         INSERT INTO CSI_ITEM_INSTANCES_H(
1847           INSTANCE_HISTORY_ID,
1848           INSTANCE_ID,
1849           TRANSACTION_ID,
1850           NEW_INSTANCE_NUMBER,
1851           NEW_EXTERNAL_REFERENCE,
1852           NEW_INVENTORY_ITEM_ID,
1853           NEW_INVENTORY_REVISION,
1854           NEW_INV_MASTER_ORGANIZATION_ID,
1855           NEW_SERIAL_NUMBER ,
1856           NEW_MFG_SERIAL_NUMBER_FLAG,
1857           NEW_LOT_NUMBER,
1858           NEW_QUANTITY,
1859           NEW_UNIT_OF_MEASURE,
1860           NEW_ACCOUNTING_CLASS_CODE,
1861           NEW_INSTANCE_CONDITION_ID,
1862           NEW_INSTANCE_STATUS_ID,
1863           NEW_CUSTOMER_VIEW_FLAG,
1864           NEW_MERCHANT_VIEW_FLAG,
1865           NEW_SELLABLE_FLAG,
1866           NEW_SYSTEM_ID,
1867           NEW_INSTANCE_TYPE_CODE,
1868           NEW_ACTIVE_START_DATE,
1869           NEW_ACTIVE_END_DATE,
1870           NEW_LOCATION_TYPE_CODE,
1871           NEW_LOCATION_ID,
1872           NEW_INV_ORGANIZATION_ID,
1873           NEW_INV_SUBINVENTORY_NAME,
1874           NEW_INV_LOCATOR_ID,
1875           NEW_PA_PROJECT_ID,
1876           NEW_PA_PROJECT_TASK_ID,
1877           NEW_IN_TRANSIT_ORDER_LINE_ID,
1878           NEW_WIP_JOB_ID,
1879           NEW_PO_ORDER_LINE_ID,
1880           NEW_COMPLETENESS_FLAG,
1881           FULL_DUMP_FLAG,
1882           NEW_CONTEXT,
1883           NEW_ATTRIBUTE1,
1884           NEW_ATTRIBUTE2,
1885           NEW_ATTRIBUTE3,
1886           NEW_ATTRIBUTE4,
1887           NEW_ATTRIBUTE5,
1888           NEW_ATTRIBUTE6,
1889           NEW_ATTRIBUTE7,
1890           NEW_ATTRIBUTE8,
1891           NEW_ATTRIBUTE9,
1892           NEW_ATTRIBUTE10,
1893           NEW_ATTRIBUTE11,
1894           NEW_ATTRIBUTE12,
1895           NEW_ATTRIBUTE13,
1896           NEW_ATTRIBUTE14,
1897           NEW_ATTRIBUTE15,
1898           CREATED_BY,
1899           CREATION_DATE,
1900           LAST_UPDATED_BY,
1901           LAST_UPDATE_DATE,
1902           LAST_UPDATE_LOGIN,
1903           OBJECT_VERSION_NUMBER,
1904           NEW_INST_LOC_TYPE_CODE,
1905           NEW_INST_LOC_ID,
1906           NEW_INST_USAGE_CODE,
1907           NEW_last_vld_organization_id,
1908           NEW_CONFIG_INST_REV_NUM,
1909           NEW_CONFIG_VALID_STATUS,
1910           NEW_INSTANCE_DESCRIPTION)
1911         VALUES(
1912           l_inst_hist_tbl(i),
1913           l_inst_rec_tab.INSTANCE_ID(i),
1914           l_txn_id,
1915           l_inst_rec_tab.INSTANCE_NUMBER(i),
1916           l_inst_rec_tab.EXTERNAL_REFERENCE(i),
1917           l_inst_rec_tab.INVENTORY_ITEM_ID(i),
1918           l_inst_rec_tab.INVENTORY_REVISION(i),
1919           l_inst_rec_tab.INV_MASTER_ORGANIZATION_ID(i),
1920           l_inst_rec_tab.SERIAL_NUMBER (i),
1921           l_inst_rec_tab.MFG_SERIAL_NUMBER_FLAG(i),
1922           l_inst_rec_tab.LOT_NUMBER(i),
1923           l_inst_rec_tab.QUANTITY(i),
1924           l_inst_rec_tab.UNIT_OF_MEASURE(i),
1925           l_inst_rec_tab.ACCOUNTING_CLASS_CODE(i),
1926           l_inst_rec_tab.INSTANCE_CONDITION_ID(i),
1927           l_inst_rec_tab.INSTANCE_STATUS_ID(i),
1928           l_inst_rec_tab.CUSTOMER_VIEW_FLAG(i),
1929           l_inst_rec_tab.MERCHANT_VIEW_FLAG(i),
1930           l_inst_rec_tab.SELLABLE_FLAG(i),
1931           l_inst_rec_tab.SYSTEM_ID(i),
1932           l_inst_rec_tab.INSTANCE_TYPE_CODE(i),
1933           l_inst_rec_tab.ACTIVE_START_DATE(i),
1934           l_inst_rec_tab.ACTIVE_END_DATE(i),
1935           l_inst_rec_tab.LOCATION_TYPE_CODE(i),
1936           l_inst_rec_tab.LOCATION_ID(i),
1937           l_inst_rec_tab.INV_ORGANIZATION_ID(i),
1938           l_inst_rec_tab.INV_SUBINVENTORY_NAME(i),
1939           l_inst_rec_tab.INV_LOCATOR_ID(i),
1940           l_inst_rec_tab.PA_PROJECT_ID(i),
1941           l_inst_rec_tab.PA_PROJECT_TASK_ID(i),
1942           l_inst_rec_tab.IN_TRANSIT_ORDER_LINE_ID(i),
1943           l_inst_rec_tab.WIP_JOB_ID(i),
1944           l_inst_rec_tab.PO_ORDER_LINE_ID(i),
1945           l_inst_rec_tab.COMPLETENESS_FLAG(i),
1946           'Y',
1947           l_inst_rec_tab.CONTEXT(i),
1948           l_inst_rec_tab.ATTRIBUTE1(i),
1949           l_inst_rec_tab.ATTRIBUTE2(i),
1950           l_inst_rec_tab.ATTRIBUTE3(i),
1951           l_inst_rec_tab.ATTRIBUTE4(i),
1952           l_inst_rec_tab.ATTRIBUTE5(i),
1953           l_inst_rec_tab.ATTRIBUTE6(i),
1954           l_inst_rec_tab.ATTRIBUTE7(i),
1955           l_inst_rec_tab.ATTRIBUTE8(i),
1956           l_inst_rec_tab.ATTRIBUTE9(i),
1957           l_inst_rec_tab.ATTRIBUTE10(i),
1958           l_inst_rec_tab.ATTRIBUTE11(i),
1959           l_inst_rec_tab.ATTRIBUTE12(i),
1960           l_inst_rec_tab.ATTRIBUTE13(i),
1961           l_inst_rec_tab.ATTRIBUTE14(i),
1962           l_inst_rec_tab.ATTRIBUTE15(i),
1963           l_user_id,
1964           sysdate,
1965           l_user_id,
1966           sysdate,
1967           -1,
1968           1,
1969           l_inst_rec_tab.INSTALL_LOCATION_TYPE_CODE(i), --fix for bug4881769
1970           l_inst_rec_tab.INSTALL_LOCATION_ID(i),
1971           l_inst_rec_tab.INSTANCE_USAGE_CODE(i),
1972           l_inst_rec_tab.vld_organization_id(i),
1973           l_inst_rec_tab.CONFIG_INST_REV_NUM(i),
1974           l_inst_rec_tab.CONFIG_VALID_STATUS(i),
1975           l_inst_rec_tab.INSTANCE_DESCRIPTION(i));
1976     END IF;
1977       --
1978     IF l_version_label_tbl.count > 0 THEN
1979       log('Before Build_Ver_Label_Rec_of_Table');
1980       Build_Ver_Label_Rec_of_Table (
1981         p_version_label_tbl     => l_version_label_tbl,
1982         p_version_label_rec_tab => l_version_label_rec_tab,
1983         p_ver_label_hist_tbl    => l_ver_label_hist_tbl);
1984       --
1985       l_ctr := l_version_label_rec_tab.version_label_id.count;
1986       -- Insert into History
1987       log('Before Inserting into Version Labels history ..');
1988       FORALL i in 1 .. l_version_label_rec_tab.version_label_id.count
1989         INSERT INTO CSI_I_VERSION_LABELS_H(
1990           VERSION_LABEL_HISTORY_ID,
1991           VERSION_LABEL_ID,
1992           TRANSACTION_ID,
1993           NEW_VERSION_LABEL,
1994           NEW_DESCRIPTION,
1995           NEW_DATE_TIME_STAMP,
1996           NEW_ACTIVE_START_DATE,
1997           NEW_ACTIVE_END_DATE,
1998           NEW_CONTEXT,
1999           NEW_ATTRIBUTE1,
2000           NEW_ATTRIBUTE2,
2001           NEW_ATTRIBUTE3,
2002           NEW_ATTRIBUTE4,
2003           NEW_ATTRIBUTE5,
2004           NEW_ATTRIBUTE6,
2005           NEW_ATTRIBUTE7,
2006           NEW_ATTRIBUTE8,
2007           NEW_ATTRIBUTE9,
2008           NEW_ATTRIBUTE10,
2009           NEW_ATTRIBUTE11,
2010           NEW_ATTRIBUTE12,
2011           NEW_ATTRIBUTE13,
2012           NEW_ATTRIBUTE14,
2013           NEW_ATTRIBUTE15,
2014           FULL_DUMP_FLAG,
2015           CREATED_BY,
2016           CREATION_DATE,
2017           LAST_UPDATED_BY,
2018           LAST_UPDATE_DATE,
2019           LAST_UPDATE_LOGIN,
2020           OBJECT_VERSION_NUMBER)
2021         VALUES(
2022           l_ver_label_hist_tbl(i),
2023           l_version_label_rec_tab.VERSION_LABEL_ID(i),
2024           l_txn_id,
2025           l_version_label_rec_tab.VERSION_LABEL(i),
2026           l_version_label_rec_tab.DESCRIPTION(i),
2027           l_version_label_rec_tab.DATE_TIME_STAMP(i),
2028           l_version_label_rec_tab.ACTIVE_START_DATE(i),
2029           l_version_label_rec_tab.ACTIVE_END_DATE(i),
2030           l_version_label_rec_tab.CONTEXT(i),
2031           l_version_label_rec_tab.ATTRIBUTE1(i),
2032           l_version_label_rec_tab.ATTRIBUTE2(i),
2033           l_version_label_rec_tab.ATTRIBUTE3(i),
2034           l_version_label_rec_tab.ATTRIBUTE4(i),
2035           l_version_label_rec_tab.ATTRIBUTE5(i),
2036           l_version_label_rec_tab.ATTRIBUTE6(i),
2037           l_version_label_rec_tab.ATTRIBUTE7(i),
2038           l_version_label_rec_tab.ATTRIBUTE8(i),
2039           l_version_label_rec_tab.ATTRIBUTE9(i),
2040           l_version_label_rec_tab.ATTRIBUTE10(i),
2041           l_version_label_rec_tab.ATTRIBUTE11(i),
2042           l_version_label_rec_tab.ATTRIBUTE12(i),
2043           l_version_label_rec_tab.ATTRIBUTE13(i),
2044           l_version_label_rec_tab.ATTRIBUTE14(i),
2045           l_version_label_rec_tab.ATTRIBUTE15(i),
2046           'Y',
2047           l_user_id,
2048           sysdate,
2049           l_user_id,
2050           sysdate,
2051           -1,
2052           1);
2053     END IF;
2054     --
2055     IF l_party_tbl.count > 0 THEN
2056       log('Before Build_Party_Rec_of_Table');
2057       build_party_rec_of_table(
2058         p_party_tbl    => l_party_tbl,
2059         p_party_rec_tab => l_party_rec_tab,
2060         p_party_hist_tbl => l_party_hist_tbl);
2061       --
2062       l_ctr := l_party_rec_tab.instance_party_id.count;
2063       --
2064       -- Insert into History
2065       log('Before inserting into Parties history..');
2066       FORALL i in 1 .. l_party_rec_tab.instance_party_id.count
2067         INSERT INTO CSI_I_PARTIES_H(
2068           INSTANCE_PARTY_HISTORY_ID,
2069           INSTANCE_PARTY_ID,
2070           TRANSACTION_ID,
2071           NEW_PARTY_SOURCE_TABLE,
2072           NEW_PARTY_ID,
2073           NEW_RELATIONSHIP_TYPE_CODE,
2074           NEW_CONTACT_FLAG,
2075           NEW_CONTACT_IP_ID,
2076           NEW_ACTIVE_START_DATE,
2077           NEW_ACTIVE_END_DATE,
2078           NEW_CONTEXT,
2079           NEW_ATTRIBUTE1,
2080           NEW_ATTRIBUTE2,
2081           NEW_ATTRIBUTE3,
2082           NEW_ATTRIBUTE4,
2083           NEW_ATTRIBUTE5,
2084           NEW_ATTRIBUTE6,
2085           NEW_ATTRIBUTE7,
2086           NEW_ATTRIBUTE8,
2087           NEW_ATTRIBUTE9,
2088           NEW_ATTRIBUTE10,
2089           NEW_ATTRIBUTE11,
2090           NEW_ATTRIBUTE12,
2091           NEW_ATTRIBUTE13,
2092           NEW_ATTRIBUTE14,
2093           NEW_ATTRIBUTE15,
2094           NEW_PRIMARY_FLAG,
2095           NEW_PREFERRED_FLAG,
2096           FULL_DUMP_FLAG,
2097           CREATED_BY,
2098           CREATION_DATE,
2099           LAST_UPDATED_BY,
2100           LAST_UPDATE_DATE,
2101           LAST_UPDATE_LOGIN,
2102           OBJECT_VERSION_NUMBER)
2103         VALUES(
2104           l_party_hist_tbl(i),
2105           l_party_rec_tab.INSTANCE_PARTY_ID(i),
2106           l_txn_id,
2107           l_party_rec_tab.PARTY_SOURCE_TABLE(i),
2108           l_party_rec_tab.PARTY_ID(i),
2109           l_party_rec_tab.RELATIONSHIP_TYPE_CODE(i),
2110           l_party_rec_tab.CONTACT_FLAG(i),
2111           l_party_rec_tab.CONTACT_IP_ID(i),
2112           l_party_rec_tab.ACTIVE_START_DATE(i),
2113           l_party_rec_tab.ACTIVE_END_DATE(i),
2114           l_party_rec_tab.CONTEXT(i),
2115           l_party_rec_tab.ATTRIBUTE1(i),
2116           l_party_rec_tab.ATTRIBUTE2(i),
2117           l_party_rec_tab.ATTRIBUTE3(i),
2118           l_party_rec_tab.ATTRIBUTE4(i),
2119           l_party_rec_tab.ATTRIBUTE5(i),
2120           l_party_rec_tab.ATTRIBUTE6(i),
2121           l_party_rec_tab.ATTRIBUTE7(i),
2122           l_party_rec_tab.ATTRIBUTE8(i),
2123           l_party_rec_tab.ATTRIBUTE9(i),
2124           l_party_rec_tab.ATTRIBUTE10(i),
2125           l_party_rec_tab.ATTRIBUTE11(i),
2126           l_party_rec_tab.ATTRIBUTE12(i),
2127           l_party_rec_tab.ATTRIBUTE13(i),
2128           l_party_rec_tab.ATTRIBUTE14(i),
2129           l_party_rec_tab.ATTRIBUTE15(i),
2130           l_party_rec_tab.PRIMARY_FLAG(i),
2131           l_party_rec_tab.PREFERRED_FLAG(i),
2132           'Y',
2133           l_user_id,
2134           sysdate,
2135           l_user_id,
2136           sysdate,
2137           -1,
2138           1);
2139     END IF;
2140     --
2141    IF l_party_account_tbl.count > 0 THEN
2142      log('Before Build_Acct_Rec_of_Table');
2143      Build_Acct_Rec_of_Table(
2144        p_account_tbl       => l_party_account_tbl,
2145        p_account_rec_tab   => l_account_rec_tab,
2146        p_account_hist_tbl  => l_account_hist_tbl);
2147      --
2148      l_ctr := l_account_rec_tab.ip_account_id.count;
2149      --
2150      -- Insert into History
2151      log('Before Inserting into Party Accounts history');
2152      FORALL i in 1 .. l_account_rec_tab.ip_account_id.count
2153        INSERT INTO CSI_IP_ACCOUNTS_H(
2154           IP_ACCOUNT_HISTORY_ID,
2155           IP_ACCOUNT_ID,
2156           TRANSACTION_ID,
2157           NEW_PARTY_ACCOUNT_ID,
2158           NEW_RELATIONSHIP_TYPE_CODE,
2159           NEW_ACTIVE_START_DATE,
2160           NEW_ACTIVE_END_DATE,
2161           NEW_CONTEXT,
2162           NEW_ATTRIBUTE1,
2163           NEW_ATTRIBUTE2,
2164           NEW_ATTRIBUTE3,
2165           NEW_ATTRIBUTE4,
2166           NEW_ATTRIBUTE5,
2167           NEW_ATTRIBUTE6,
2168           NEW_ATTRIBUTE7,
2169           NEW_ATTRIBUTE8,
2170           NEW_ATTRIBUTE9,
2171           NEW_ATTRIBUTE10,
2172           NEW_ATTRIBUTE11,
2173           NEW_ATTRIBUTE12,
2174           NEW_ATTRIBUTE13,
2175           NEW_ATTRIBUTE14,
2176           NEW_ATTRIBUTE15,
2177           NEW_BILL_TO_ADDRESS,
2178           NEW_SHIP_TO_ADDRESS,
2179           FULL_DUMP_FLAG,
2180           CREATED_BY,
2181           CREATION_DATE,
2182           LAST_UPDATED_BY,
2183           LAST_UPDATE_DATE,
2184           LAST_UPDATE_LOGIN,
2185           OBJECT_VERSION_NUMBER)
2186         VALUES(
2187           l_account_hist_tbl(i),
2188           l_account_rec_tab.IP_ACCOUNT_ID(i),
2189           l_txn_id,
2190           l_account_rec_tab.PARTY_ACCOUNT_ID(i),
2191           l_account_rec_tab.RELATIONSHIP_TYPE_CODE(i),
2192           l_account_rec_tab.ACTIVE_START_DATE(i),
2193           l_account_rec_tab.ACTIVE_END_DATE(i),
2194           l_account_rec_tab.CONTEXT(i),
2195           l_account_rec_tab.ATTRIBUTE1(i),
2196           l_account_rec_tab.ATTRIBUTE2(i),
2197           l_account_rec_tab.ATTRIBUTE3(i),
2198           l_account_rec_tab.ATTRIBUTE4(i),
2199           l_account_rec_tab.ATTRIBUTE5(i),
2200           l_account_rec_tab.ATTRIBUTE6(i),
2201           l_account_rec_tab.ATTRIBUTE7(i),
2202           l_account_rec_tab.ATTRIBUTE8(i),
2203           l_account_rec_tab.ATTRIBUTE9(i),
2204           l_account_rec_tab.ATTRIBUTE10(i),
2205           l_account_rec_tab.ATTRIBUTE11(i),
2206           l_account_rec_tab.ATTRIBUTE12(i),
2207           l_account_rec_tab.ATTRIBUTE13(i),
2208           l_account_rec_tab.ATTRIBUTE14(i),
2209           l_account_rec_tab.ATTRIBUTE15(i),
2210           l_account_rec_tab.BILL_TO_ADDRESS(i),
2211           l_account_rec_tab.SHIP_TO_ADDRESS(i),
2212           'Y',
2213           l_user_id,
2214           sysdate,
2215           l_user_id,
2216           sysdate,
2217           -1,
2218           1);
2219     END IF;
2220     --
2221     IF l_org_units_tbl.count > 0 THEN
2222       log('Before Build_Org_Rec_of_Table');
2223       Build_Org_Rec_of_Table (
2224         p_org_tbl           => l_org_units_tbl,
2225         p_org_units_rec_tab => l_org_units_rec_tab,
2226         p_org_hist_tbl      => l_org_hist_tbl);
2227       --
2228       l_ctr := l_org_units_rec_tab.instance_ou_id.count;
2229       --
2230       -- Insert into History
2231       log('Before Inserting into Org Assignments history');
2232       FORALL i in 1 .. l_org_units_rec_tab.instance_ou_id.count
2233         INSERT INTO CSI_I_ORG_ASSIGNMENTS_H(
2234           INSTANCE_OU_HISTORY_ID,
2235           INSTANCE_OU_ID,
2236           TRANSACTION_ID,
2237           NEW_OPERATING_UNIT_ID,
2238           NEW_RELATIONSHIP_TYPE_CODE,
2239           NEW_ACTIVE_START_DATE,
2240           NEW_ACTIVE_END_DATE,
2241           NEW_CONTEXT,
2242           NEW_ATTRIBUTE1,
2243           NEW_ATTRIBUTE2,
2244           NEW_ATTRIBUTE3,
2245           NEW_ATTRIBUTE4,
2246           NEW_ATTRIBUTE5,
2247           NEW_ATTRIBUTE6,
2248           NEW_ATTRIBUTE7,
2249           NEW_ATTRIBUTE8,
2250           NEW_ATTRIBUTE9,
2251           NEW_ATTRIBUTE10,
2252           NEW_ATTRIBUTE11,
2253           NEW_ATTRIBUTE12,
2254           NEW_ATTRIBUTE13,
2255           NEW_ATTRIBUTE14,
2256           NEW_ATTRIBUTE15,
2257           FULL_DUMP_FLAG,
2258           CREATED_BY,
2259           CREATION_DATE,
2260           LAST_UPDATED_BY,
2261           LAST_UPDATE_DATE,
2262           LAST_UPDATE_LOGIN,
2263           OBJECT_VERSION_NUMBER)
2264        VALUES(
2265           l_org_hist_tbl(i),
2266           l_org_units_rec_tab.INSTANCE_OU_ID(i),
2267           l_txn_id,
2268           l_org_units_rec_tab.OPERATING_UNIT_ID(i),
2269           l_org_units_rec_tab.RELATIONSHIP_TYPE_CODE(i),
2270           l_org_units_rec_tab.ACTIVE_START_DATE(i),
2271           l_org_units_rec_tab.ACTIVE_END_DATE(i),
2272           l_org_units_rec_tab.CONTEXT(i),
2273           l_org_units_rec_tab.ATTRIBUTE1(i),
2274           l_org_units_rec_tab.ATTRIBUTE2(i),
2275           l_org_units_rec_tab.ATTRIBUTE3(i),
2276           l_org_units_rec_tab.ATTRIBUTE4(i),
2277           l_org_units_rec_tab.ATTRIBUTE5(i),
2278           l_org_units_rec_tab.ATTRIBUTE6(i),
2279           l_org_units_rec_tab.ATTRIBUTE7(i),
2280           l_org_units_rec_tab.ATTRIBUTE8(i),
2281           l_org_units_rec_tab.ATTRIBUTE9(i),
2282           l_org_units_rec_tab.ATTRIBUTE10(i),
2283           l_org_units_rec_tab.ATTRIBUTE11(i),
2284           l_org_units_rec_tab.ATTRIBUTE12(i),
2285           l_org_units_rec_tab.ATTRIBUTE13(i),
2286           l_org_units_rec_tab.ATTRIBUTE14(i),
2287           l_org_units_rec_tab.ATTRIBUTE15(i),
2288           'Y',
2289           l_user_id,
2290           sysdate,
2291           l_user_id,
2292           sysdate,
2293           -1,
2294           1);
2295     END IF;
2296     --
2297     IF l_pricing_attribs_tbl.count > 0 THEN
2298       log('Before Build_pricing_Rec_of_Table');
2299       build_pricing_rec_of_table (
2300         p_pricing_tbl       => l_pricing_attribs_tbl,
2301         p_pricing_rec_tab   => l_pricing_rec_tab,
2302         p_pricing_hist_tbl  => l_pricing_hist_tbl);
2303       --
2304       l_ctr := l_pricing_rec_tab.pricing_attribute_id.count;
2305       --
2306       -- Insert into History
2307       log('Before Inserting into Pricing Attribs history');
2308       FORALL i in 1 .. l_pricing_rec_tab.pricing_attribute_id.count
2309         INSERT INTO CSI_I_PRICING_ATTRIBS_H(
2310           PRICE_ATTRIB_HISTORY_ID,
2311           PRICING_ATTRIBUTE_ID,
2312           TRANSACTION_ID,
2313           NEW_ACTIVE_START_DATE,
2314           NEW_ACTIVE_END_DATE,
2315           NEW_CONTEXT,
2316           NEW_ATTRIBUTE1,
2317           NEW_ATTRIBUTE2,
2318           NEW_ATTRIBUTE3,
2319           NEW_ATTRIBUTE4,
2320           NEW_ATTRIBUTE5,
2321           NEW_ATTRIBUTE6,
2322           NEW_ATTRIBUTE7,
2323           NEW_ATTRIBUTE8,
2324           NEW_ATTRIBUTE9,
2325           NEW_ATTRIBUTE10,
2326           NEW_ATTRIBUTE11,
2327           NEW_ATTRIBUTE12,
2328           NEW_ATTRIBUTE13,
2329           NEW_ATTRIBUTE14,
2330           NEW_ATTRIBUTE15,
2331           NEW_PRICING_CONTEXT,
2332           NEW_PRICING_ATTRIBUTE1,
2333           NEW_PRICING_ATTRIBUTE2,
2334           NEW_PRICING_ATTRIBUTE3,
2335           NEW_PRICING_ATTRIBUTE4,
2336           NEW_PRICING_ATTRIBUTE5,
2337           NEW_PRICING_ATTRIBUTE6,
2338           NEW_PRICING_ATTRIBUTE7,
2339           NEW_PRICING_ATTRIBUTE8,
2340           NEW_PRICING_ATTRIBUTE9,
2341           NEW_PRICING_ATTRIBUTE10,
2342           NEW_PRICING_ATTRIBUTE11,
2343           NEW_PRICING_ATTRIBUTE12,
2344           NEW_PRICING_ATTRIBUTE13,
2345           NEW_PRICING_ATTRIBUTE14,
2346           NEW_PRICING_ATTRIBUTE15,
2347           NEW_PRICING_ATTRIBUTE16,
2348           NEW_PRICING_ATTRIBUTE17,
2349           NEW_PRICING_ATTRIBUTE18,
2350           NEW_PRICING_ATTRIBUTE19,
2351           NEW_PRICING_ATTRIBUTE20,
2352           NEW_PRICING_ATTRIBUTE21,
2353           NEW_PRICING_ATTRIBUTE22,
2354           NEW_PRICING_ATTRIBUTE23,
2355           NEW_PRICING_ATTRIBUTE24,
2356           NEW_PRICING_ATTRIBUTE25,
2357           NEW_PRICING_ATTRIBUTE26,
2358           NEW_PRICING_ATTRIBUTE27,
2359           NEW_PRICING_ATTRIBUTE28,
2360           NEW_PRICING_ATTRIBUTE29,
2361           NEW_PRICING_ATTRIBUTE30,
2362           NEW_PRICING_ATTRIBUTE31,
2363           NEW_PRICING_ATTRIBUTE32,
2364           NEW_PRICING_ATTRIBUTE33,
2365           NEW_PRICING_ATTRIBUTE34,
2366           NEW_PRICING_ATTRIBUTE35,
2367           NEW_PRICING_ATTRIBUTE36,
2368           NEW_PRICING_ATTRIBUTE37,
2369           NEW_PRICING_ATTRIBUTE38,
2370           NEW_PRICING_ATTRIBUTE39,
2371           NEW_PRICING_ATTRIBUTE40,
2372           NEW_PRICING_ATTRIBUTE41,
2373           NEW_PRICING_ATTRIBUTE42,
2374           NEW_PRICING_ATTRIBUTE43,
2375           NEW_PRICING_ATTRIBUTE44,
2376           NEW_PRICING_ATTRIBUTE45,
2377           NEW_PRICING_ATTRIBUTE46,
2378           NEW_PRICING_ATTRIBUTE47,
2379           NEW_PRICING_ATTRIBUTE48,
2380           NEW_PRICING_ATTRIBUTE49,
2381           NEW_PRICING_ATTRIBUTE50,
2382           NEW_PRICING_ATTRIBUTE51,
2383           NEW_PRICING_ATTRIBUTE52,
2384           NEW_PRICING_ATTRIBUTE53,
2385           NEW_PRICING_ATTRIBUTE54,
2386           NEW_PRICING_ATTRIBUTE55,
2387           NEW_PRICING_ATTRIBUTE56,
2388           NEW_PRICING_ATTRIBUTE57,
2389           NEW_PRICING_ATTRIBUTE58,
2390           NEW_PRICING_ATTRIBUTE59,
2391           NEW_PRICING_ATTRIBUTE60,
2392           NEW_PRICING_ATTRIBUTE61,
2393           NEW_PRICING_ATTRIBUTE62,
2394           NEW_PRICING_ATTRIBUTE63,
2395           NEW_PRICING_ATTRIBUTE64,
2396           NEW_PRICING_ATTRIBUTE65,
2397           NEW_PRICING_ATTRIBUTE66,
2398           NEW_PRICING_ATTRIBUTE67,
2399           NEW_PRICING_ATTRIBUTE68,
2400           NEW_PRICING_ATTRIBUTE69,
2401           NEW_PRICING_ATTRIBUTE70,
2402           NEW_PRICING_ATTRIBUTE71,
2403           NEW_PRICING_ATTRIBUTE72,
2404           NEW_PRICING_ATTRIBUTE73,
2405           NEW_PRICING_ATTRIBUTE74,
2406           NEW_PRICING_ATTRIBUTE75,
2407           NEW_PRICING_ATTRIBUTE76,
2408           NEW_PRICING_ATTRIBUTE77,
2409           NEW_PRICING_ATTRIBUTE78,
2410           NEW_PRICING_ATTRIBUTE79,
2411           NEW_PRICING_ATTRIBUTE80,
2412           NEW_PRICING_ATTRIBUTE81,
2413           NEW_PRICING_ATTRIBUTE82,
2414           NEW_PRICING_ATTRIBUTE83,
2415           NEW_PRICING_ATTRIBUTE84,
2416           NEW_PRICING_ATTRIBUTE85,
2417           NEW_PRICING_ATTRIBUTE86,
2418           NEW_PRICING_ATTRIBUTE87,
2419           NEW_PRICING_ATTRIBUTE88,
2420           NEW_PRICING_ATTRIBUTE89,
2421           NEW_PRICING_ATTRIBUTE90,
2422           NEW_PRICING_ATTRIBUTE91,
2423           NEW_PRICING_ATTRIBUTE92,
2424           NEW_PRICING_ATTRIBUTE93,
2425           NEW_PRICING_ATTRIBUTE94,
2426           NEW_PRICING_ATTRIBUTE95,
2427           NEW_PRICING_ATTRIBUTE96,
2428           NEW_PRICING_ATTRIBUTE97,
2429           NEW_PRICING_ATTRIBUTE98,
2430           NEW_PRICING_ATTRIBUTE99,
2431           NEW_PRICING_ATTRIBUTE100,
2432           FULL_DUMP_FLAG,
2433           CREATED_BY,
2434           CREATION_DATE,
2435           LAST_UPDATED_BY,
2436           LAST_UPDATE_DATE,
2437           LAST_UPDATE_LOGIN,
2438           OBJECT_VERSION_NUMBER)
2439        VALUES(
2440           l_pricing_hist_tbl(i),
2441           l_pricing_rec_tab.PRICING_ATTRIBUTE_ID(i),
2442           l_txn_id,
2443           l_pricing_rec_tab.ACTIVE_START_DATE(i),
2444           l_pricing_rec_tab.ACTIVE_END_DATE(i),
2445           l_pricing_rec_tab.CONTEXT(i),
2446           l_pricing_rec_tab.ATTRIBUTE1(i),
2447           l_pricing_rec_tab.ATTRIBUTE2(i),
2448           l_pricing_rec_tab.ATTRIBUTE3(i),
2449           l_pricing_rec_tab.ATTRIBUTE4(i),
2450           l_pricing_rec_tab.ATTRIBUTE5(i),
2451           l_pricing_rec_tab.ATTRIBUTE6(i),
2452           l_pricing_rec_tab.ATTRIBUTE7(i),
2453           l_pricing_rec_tab.ATTRIBUTE8(i),
2454           l_pricing_rec_tab.ATTRIBUTE9(i),
2455           l_pricing_rec_tab.ATTRIBUTE10(i),
2456           l_pricing_rec_tab.ATTRIBUTE11(i),
2457           l_pricing_rec_tab.ATTRIBUTE12(i),
2458           l_pricing_rec_tab.ATTRIBUTE13(i),
2459           l_pricing_rec_tab.ATTRIBUTE14(i),
2460           l_pricing_rec_tab.ATTRIBUTE15(i),
2461           l_pricing_rec_tab.PRICING_CONTEXT(i),
2462           l_pricing_rec_tab.PRICING_ATTRIBUTE1(i),
2463           l_pricing_rec_tab.PRICING_ATTRIBUTE2(i),
2464           l_pricing_rec_tab.PRICING_ATTRIBUTE3(i),
2465           l_pricing_rec_tab.PRICING_ATTRIBUTE4(i),
2466           l_pricing_rec_tab.PRICING_ATTRIBUTE5(i),
2467           l_pricing_rec_tab.PRICING_ATTRIBUTE6(i),
2468           l_pricing_rec_tab.PRICING_ATTRIBUTE7(i),
2469           l_pricing_rec_tab.PRICING_ATTRIBUTE8(i),
2470           l_pricing_rec_tab.PRICING_ATTRIBUTE9(i),
2471           l_pricing_rec_tab.PRICING_ATTRIBUTE10(i),
2472           l_pricing_rec_tab.PRICING_ATTRIBUTE11(i),
2473           l_pricing_rec_tab.PRICING_ATTRIBUTE12(i),
2474           l_pricing_rec_tab.PRICING_ATTRIBUTE13(i),
2475           l_pricing_rec_tab.PRICING_ATTRIBUTE14(i),
2476           l_pricing_rec_tab.PRICING_ATTRIBUTE15(i),
2477           l_pricing_rec_tab.PRICING_ATTRIBUTE16(i),
2478           l_pricing_rec_tab.PRICING_ATTRIBUTE17(i),
2479           l_pricing_rec_tab.PRICING_ATTRIBUTE18(i),
2480           l_pricing_rec_tab.PRICING_ATTRIBUTE19(i),
2481           l_pricing_rec_tab.PRICING_ATTRIBUTE20(i),
2482           l_pricing_rec_tab.PRICING_ATTRIBUTE21(i),
2483           l_pricing_rec_tab.PRICING_ATTRIBUTE22(i),
2484           l_pricing_rec_tab.PRICING_ATTRIBUTE23(i),
2485           l_pricing_rec_tab.PRICING_ATTRIBUTE24(i),
2486           l_pricing_rec_tab.PRICING_ATTRIBUTE25(i),
2487           l_pricing_rec_tab.PRICING_ATTRIBUTE26(i),
2488           l_pricing_rec_tab.PRICING_ATTRIBUTE27(i),
2489           l_pricing_rec_tab.PRICING_ATTRIBUTE28(i),
2490           l_pricing_rec_tab.PRICING_ATTRIBUTE29(i),
2491           l_pricing_rec_tab.PRICING_ATTRIBUTE30(i),
2492           l_pricing_rec_tab.PRICING_ATTRIBUTE31(i),
2493           l_pricing_rec_tab.PRICING_ATTRIBUTE32(i),
2494           l_pricing_rec_tab.PRICING_ATTRIBUTE33(i),
2495           l_pricing_rec_tab.PRICING_ATTRIBUTE34(i),
2496           l_pricing_rec_tab.PRICING_ATTRIBUTE35(i),
2497           l_pricing_rec_tab.PRICING_ATTRIBUTE36(i),
2498           l_pricing_rec_tab.PRICING_ATTRIBUTE37(i),
2499           l_pricing_rec_tab.PRICING_ATTRIBUTE38(i),
2500           l_pricing_rec_tab.PRICING_ATTRIBUTE39(i),
2501           l_pricing_rec_tab.PRICING_ATTRIBUTE40(i),
2502           l_pricing_rec_tab.PRICING_ATTRIBUTE41(i),
2503           l_pricing_rec_tab.PRICING_ATTRIBUTE42(i),
2504           l_pricing_rec_tab.PRICING_ATTRIBUTE43(i),
2505           l_pricing_rec_tab.PRICING_ATTRIBUTE44(i),
2506           l_pricing_rec_tab.PRICING_ATTRIBUTE45(i),
2507           l_pricing_rec_tab.PRICING_ATTRIBUTE46(i),
2508           l_pricing_rec_tab.PRICING_ATTRIBUTE47(i),
2509           l_pricing_rec_tab.PRICING_ATTRIBUTE48(i),
2510           l_pricing_rec_tab.PRICING_ATTRIBUTE49(i),
2511           l_pricing_rec_tab.PRICING_ATTRIBUTE50(i),
2512           l_pricing_rec_tab.PRICING_ATTRIBUTE51(i),
2513           l_pricing_rec_tab.PRICING_ATTRIBUTE52(i),
2514           l_pricing_rec_tab.PRICING_ATTRIBUTE53(i),
2515           l_pricing_rec_tab.PRICING_ATTRIBUTE54(i),
2516           l_pricing_rec_tab.PRICING_ATTRIBUTE55(i),
2517           l_pricing_rec_tab.PRICING_ATTRIBUTE56(i),
2518           l_pricing_rec_tab.PRICING_ATTRIBUTE57(i),
2519           l_pricing_rec_tab.PRICING_ATTRIBUTE58(i),
2520           l_pricing_rec_tab.PRICING_ATTRIBUTE59(i),
2521           l_pricing_rec_tab.PRICING_ATTRIBUTE60(i),
2522           l_pricing_rec_tab.PRICING_ATTRIBUTE61(i),
2523           l_pricing_rec_tab.PRICING_ATTRIBUTE62(i),
2524           l_pricing_rec_tab.PRICING_ATTRIBUTE63(i),
2525           l_pricing_rec_tab.PRICING_ATTRIBUTE64(i),
2526           l_pricing_rec_tab.PRICING_ATTRIBUTE65(i),
2527           l_pricing_rec_tab.PRICING_ATTRIBUTE66(i),
2528           l_pricing_rec_tab.PRICING_ATTRIBUTE67(i),
2529           l_pricing_rec_tab.PRICING_ATTRIBUTE68(i),
2530           l_pricing_rec_tab.PRICING_ATTRIBUTE69(i),
2531           l_pricing_rec_tab.PRICING_ATTRIBUTE70(i),
2532           l_pricing_rec_tab.PRICING_ATTRIBUTE71(i),
2533           l_pricing_rec_tab.PRICING_ATTRIBUTE72(i),
2534           l_pricing_rec_tab.PRICING_ATTRIBUTE73(i),
2535           l_pricing_rec_tab.PRICING_ATTRIBUTE74(i),
2536           l_pricing_rec_tab.PRICING_ATTRIBUTE75(i),
2537           l_pricing_rec_tab.PRICING_ATTRIBUTE76(i),
2538           l_pricing_rec_tab.PRICING_ATTRIBUTE77(i),
2539           l_pricing_rec_tab.PRICING_ATTRIBUTE78(i),
2540           l_pricing_rec_tab.PRICING_ATTRIBUTE79(i),
2541           l_pricing_rec_tab.PRICING_ATTRIBUTE80(i),
2542           l_pricing_rec_tab.PRICING_ATTRIBUTE81(i),
2543           l_pricing_rec_tab.PRICING_ATTRIBUTE82(i),
2544           l_pricing_rec_tab.PRICING_ATTRIBUTE83(i),
2545           l_pricing_rec_tab.PRICING_ATTRIBUTE84(i),
2546           l_pricing_rec_tab.PRICING_ATTRIBUTE85(i),
2547           l_pricing_rec_tab.PRICING_ATTRIBUTE86(i),
2548           l_pricing_rec_tab.PRICING_ATTRIBUTE87(i),
2549           l_pricing_rec_tab.PRICING_ATTRIBUTE88(i),
2550           l_pricing_rec_tab.PRICING_ATTRIBUTE89(i),
2551           l_pricing_rec_tab.PRICING_ATTRIBUTE90(i),
2552           l_pricing_rec_tab.PRICING_ATTRIBUTE91(i),
2553           l_pricing_rec_tab.PRICING_ATTRIBUTE92(i),
2554           l_pricing_rec_tab.PRICING_ATTRIBUTE93(i),
2555           l_pricing_rec_tab.PRICING_ATTRIBUTE94(i),
2556           l_pricing_rec_tab.PRICING_ATTRIBUTE95(i),
2557           l_pricing_rec_tab.PRICING_ATTRIBUTE96(i),
2558           l_pricing_rec_tab.PRICING_ATTRIBUTE97(i),
2559           l_pricing_rec_tab.PRICING_ATTRIBUTE98(i),
2560           l_pricing_rec_tab.PRICING_ATTRIBUTE99(i),
2561           l_pricing_rec_tab.PRICING_ATTRIBUTE100(i),
2562           'Y',
2563           l_user_id,
2564           sysdate,
2565           l_user_id,
2566           sysdate,
2567           -1,
2568           1);
2569     END IF;
2570     --
2571     IF l_ext_attrib_values_tbl.count > 0 THEN
2572       log('Before Build_Ext_Attr_Rec_Table');
2573       Build_Ext_Attr_Rec_Table (
2574        p_ext_attr_tbl     => l_ext_attrib_values_tbl,
2575        p_ext_attr_rec_tab => l_ext_attr_rec_tab,
2576        p_ext_hist_tbl     => l_ext_hist_tbl);
2577       --
2578       l_ctr := l_ext_attr_rec_tab.attribute_value_id.count;
2579       --
2580       -- Insert into History
2581       log('Before Inserting into Ext Attribs history');
2582       FORALL i in 1 .. l_ext_attr_rec_tab.attribute_value_id.count
2583         INSERT INTO CSI_IEA_VALUES_H(
2584           ATTRIBUTE_VALUE_HISTORY_ID,
2585           ATTRIBUTE_VALUE_ID,
2586           TRANSACTION_ID,
2587           NEW_ATTRIBUTE_VALUE,
2588           NEW_ACTIVE_START_DATE,
2589           NEW_ACTIVE_END_DATE,
2590           NEW_CONTEXT,
2591           NEW_ATTRIBUTE1,
2592           NEW_ATTRIBUTE2,
2593           NEW_ATTRIBUTE3,
2594           NEW_ATTRIBUTE4,
2595           NEW_ATTRIBUTE5,
2596           NEW_ATTRIBUTE6,
2597           NEW_ATTRIBUTE7,
2598           NEW_ATTRIBUTE8,
2599           NEW_ATTRIBUTE9,
2600           NEW_ATTRIBUTE10,
2601           NEW_ATTRIBUTE11,
2602           NEW_ATTRIBUTE12,
2603           NEW_ATTRIBUTE13,
2604           NEW_ATTRIBUTE14,
2605           NEW_ATTRIBUTE15,
2606           FULL_DUMP_FLAG,
2607           CREATED_BY,
2608           CREATION_DATE,
2609           LAST_UPDATED_BY,
2610           LAST_UPDATE_DATE,
2611           LAST_UPDATE_LOGIN,
2612           OBJECT_VERSION_NUMBER)
2613        VALUES(
2614           l_ext_hist_tbl(i),
2615           l_ext_attr_rec_tab.ATTRIBUTE_VALUE_ID(i),
2616           l_txn_id,
2617           l_ext_attr_rec_tab.ATTRIBUTE_VALUE(i),
2618           l_ext_attr_rec_tab.ACTIVE_START_DATE(i),
2619           l_ext_attr_rec_tab.ACTIVE_END_DATE(i),
2620           l_ext_attr_rec_tab.CONTEXT(i),
2621           l_ext_attr_rec_tab.ATTRIBUTE1(i),
2622           l_ext_attr_rec_tab.ATTRIBUTE2(i),
2623           l_ext_attr_rec_tab.ATTRIBUTE3(i),
2624           l_ext_attr_rec_tab.ATTRIBUTE4(i),
2625           l_ext_attr_rec_tab.ATTRIBUTE5(i),
2626           l_ext_attr_rec_tab.ATTRIBUTE6(i),
2627           l_ext_attr_rec_tab.ATTRIBUTE7(i),
2628           l_ext_attr_rec_tab.ATTRIBUTE8(i),
2629           l_ext_attr_rec_tab.ATTRIBUTE9(i),
2630           l_ext_attr_rec_tab.ATTRIBUTE10(i),
2631           l_ext_attr_rec_tab.ATTRIBUTE11(i),
2632           l_ext_attr_rec_tab.ATTRIBUTE12(i),
2633           l_ext_attr_rec_tab.ATTRIBUTE13(i),
2634           l_ext_attr_rec_tab.ATTRIBUTE14(i),
2635           l_ext_attr_rec_tab.ATTRIBUTE15(i),
2636           'Y',
2637           l_user_id,
2638           sysdate,
2639           l_user_id,
2640           sysdate,
2641           -1,
2642           1);
2643     END IF;
2644     --
2645     IF l_instance_asset_tbl.count > 0 THEN
2646       log('Before Build_Asset_Rec_Table');
2647       Build_Asset_Rec_Table (
2648         p_asset_tbl     => l_instance_asset_tbl,
2649         p_asset_rec_tab => l_asset_rec_tab,
2650         p_asset_hist_tbl => l_asset_hist_tbl);
2651       --
2652       l_ctr := l_asset_rec_tab.instance_asset_id.count;
2653       --
2654       -- Insert into History
2655       log('Before Inserting into Assets history');
2656       FORALL i in 1 .. l_asset_rec_tab.instance_asset_id.count
2657         INSERT INTO CSI_I_ASSETS_H(
2658           INSTANCE_ASSET_HISTORY_ID,
2659           INSTANCE_ASSET_ID,
2660           TRANSACTION_ID,
2661           NEW_INSTANCE_ID,
2662           NEW_FA_ASSET_ID,
2663           NEW_ASSET_QUANTITY,
2664           NEW_FA_BOOK_TYPE_CODE,
2665           NEW_FA_LOCATION_ID,
2666           NEW_UPDATE_STATUS,
2667           NEW_ACTIVE_START_DATE,
2668           NEW_ACTIVE_END_DATE,
2669           FULL_DUMP_FLAG,
2670           CREATED_BY,
2671           CREATION_DATE,
2672           LAST_UPDATED_BY,
2673           LAST_UPDATE_DATE,
2674           LAST_UPDATE_LOGIN,
2675           OBJECT_VERSION_NUMBER)
2676        VALUES(
2677           l_asset_hist_tbl(i),
2678           l_asset_rec_tab.INSTANCE_ASSET_ID(i),
2679           l_txn_id,
2680           l_asset_rec_tab.INSTANCE_ID(i),
2681           l_asset_rec_tab.FA_ASSET_ID(i),
2682           l_asset_rec_tab.ASSET_QUANTITY(i),
2683           l_asset_rec_tab.FA_BOOK_TYPE_CODE(i),
2684           l_asset_rec_tab.FA_LOCATION_ID(i),
2685           l_asset_rec_tab.UPDATE_STATUS(i),
2686           l_asset_rec_tab.ACTIVE_START_DATE(i),
2687           l_asset_rec_tab.ACTIVE_END_DATE(i),
2688           'Y',
2689           l_user_id,
2690           sysdate,
2691           l_user_id,
2692           sysdate,
2693           -1,
2694           1);
2695     END IF;
2696     --
2697     IF l_ii_relationship_tbl.count > 0 THEN
2698       log('Before Build_Rel_Rec_of_Table');
2699       Build_Rel_Rec_of_Table (
2700         p_ii_relationship_tbl     => l_ii_relationship_tbl,
2701         p_ii_relationship_rec_tab => l_ii_relationship_rec_tab,
2702         p_rel_hist_tbl            => l_rel_hist_tbl);
2703       --
2704       l_ctr := l_ii_relationship_rec_tab.relationship_id.count;
2705       --
2706       -- Insert into History
2707       log('Before inserting into Relationships history');
2708       FORALL i in 1 .. l_ii_relationship_rec_tab.relationship_id.count
2709         INSERT INTO CSI_II_RELATIONSHIPS_H(
2710           RELATIONSHIP_HISTORY_ID
2711          ,RELATIONSHIP_ID
2712          ,TRANSACTION_ID
2713          ,NEW_SUBJECT_ID
2714          ,NEW_POSITION_REFERENCE
2715          ,NEW_ACTIVE_START_DATE
2716          ,NEW_ACTIVE_END_DATE
2717          ,NEW_MANDATORY_FLAG
2718          ,NEW_CONTEXT
2719          ,NEW_ATTRIBUTE1
2720          ,NEW_ATTRIBUTE2
2721          ,NEW_ATTRIBUTE3
2722          ,NEW_ATTRIBUTE4
2723          ,NEW_ATTRIBUTE5
2724          ,NEW_ATTRIBUTE6
2725          ,NEW_ATTRIBUTE7
2726          ,NEW_ATTRIBUTE8
2727          ,NEW_ATTRIBUTE9
2728          ,NEW_ATTRIBUTE10
2729          ,NEW_ATTRIBUTE11
2730          ,NEW_ATTRIBUTE12
2731          ,NEW_ATTRIBUTE13
2732          ,NEW_ATTRIBUTE14
2733          ,NEW_ATTRIBUTE15
2734          ,FULL_DUMP_FLAG
2735          ,CREATED_BY
2736          ,CREATION_DATE
2737          ,LAST_UPDATED_BY
2738          ,LAST_UPDATE_DATE
2739          ,LAST_UPDATE_LOGIN
2740          ,OBJECT_VERSION_NUMBER)
2741        VALUES(
2742           l_rel_hist_tbl(i)
2743          ,l_ii_relationship_rec_tab.RELATIONSHIP_ID(i)
2744          ,l_txn_id
2745          ,l_ii_relationship_rec_tab.SUBJECT_ID(i)
2746          ,l_ii_relationship_rec_tab.POSITION_REFERENCE(i)
2747          ,l_ii_relationship_rec_tab.ACTIVE_START_DATE(i)
2748          ,l_ii_relationship_rec_tab.ACTIVE_END_DATE(i)
2749          ,l_ii_relationship_rec_tab.MANDATORY_FLAG(i)
2750          ,l_ii_relationship_rec_tab.CONTEXT(i)
2751          ,l_ii_relationship_rec_tab.ATTRIBUTE1(i)
2752          ,l_ii_relationship_rec_tab.ATTRIBUTE2(i)
2753          ,l_ii_relationship_rec_tab.ATTRIBUTE3(i)
2754          ,l_ii_relationship_rec_tab.ATTRIBUTE4(i)
2755          ,l_ii_relationship_rec_tab.ATTRIBUTE5(i)
2756          ,l_ii_relationship_rec_tab.ATTRIBUTE6(i)
2757          ,l_ii_relationship_rec_tab.ATTRIBUTE7(i)
2758          ,l_ii_relationship_rec_tab.ATTRIBUTE8(i)
2759          ,l_ii_relationship_rec_tab.ATTRIBUTE9(i)
2760          ,l_ii_relationship_rec_tab.ATTRIBUTE10(i)
2761          ,l_ii_relationship_rec_tab.ATTRIBUTE11(i)
2762          ,l_ii_relationship_rec_tab.ATTRIBUTE12(i)
2763          ,l_ii_relationship_rec_tab.ATTRIBUTE13(i)
2764          ,l_ii_relationship_rec_tab.ATTRIBUTE14(i)
2765          ,l_ii_relationship_rec_tab.ATTRIBUTE15(i)
2766          ,'Y'
2767          ,l_user_id
2768          ,sysdate
2769          ,l_user_id
2770          ,sysdate
2771          ,-1
2772          ,1);
2773     END IF;
2774     --
2775     IF l_instance_tbl.count > 0 THEN
2776       INSERT INTO CSI_TRANSACTIONS(
2777         TRANSACTION_ID
2778        ,TRANSACTION_DATE
2779        ,SOURCE_TRANSACTION_DATE
2780        ,SOURCE_HEADER_REF
2781        ,TRANSACTION_TYPE_ID
2782        ,CREATED_BY
2783        ,CREATION_DATE
2784        ,LAST_UPDATED_BY
2785        ,LAST_UPDATE_DATE
2786        ,LAST_UPDATE_LOGIN
2787        ,OBJECT_VERSION_NUMBER)
2788       VALUES(
2789         l_txn_id             -- TRANSACTION_ID
2790        ,SYSDATE              -- TRANSACTION_DATE
2791        ,SYSDATE              -- SOURCE_TRANSACTION_DATE
2792        ,'Full Dump'          -- SOURCE_HEADER_REF
2793        ,v_txn_type_id        -- TRANSACTION_TYPE_ID
2794        ,l_user_id
2795        ,sysdate
2796        ,l_user_id
2797        ,sysdate
2798        ,-1
2799        ,1);
2800        --
2801     END IF;
2802     commit;
2803     log('Insert_Full_Dump Successfully completed...');
2804   EXCEPTION
2805     when comp_error then
2806       log('Comp error in Insert_Full_Dump..');
2807       ROLLBACK TO Insert_Full_Dump;
2808     when others then
2809       log(sqlerrm);
2810       ROLLBACK TO Insert_Full_Dump;
2811   END Insert_Full_Dump;
2812 
2813   FUNCTION is_sfm_active RETURN boolean
2814   IS
2815 
2816     l_applid      number;
2817     l_managerid   number;
2818 
2819     l_targetp     number;
2820     l_activep     number;
2821     l_pmon_method varchar2(30);
2822     l_callstat    number;
2823 
2824   BEGIN
2825 
2826     SELECT application_id, concurrent_queue_id
2827     INTO   l_applid, l_managerid
2828     FROM   fnd_concurrent_queues
2829     WHERE concurrent_queue_name = 'XDP_Q_EVENT_SVC';
2830 
2831     fnd_concurrent.get_manager_status(
2832       applid      => l_applid,
2833       managerid   => l_managerid,
2834       targetp     => l_targetp,
2835       activep     => l_activep,
2836       pmon_method => l_pmon_method,
2837       callstat    => l_callstat);
2838 
2839     IF (l_targetp > 0) or (l_activep > 0) THEN
2840       RETURN (TRUE);
2841     ELSE
2842       RETURN (FALSE);
2843     END IF;
2844 
2845   END is_sfm_active;
2846 
2847   PROCEDURE get_schema_name(
2848     p_product_short_name  IN  varchar2,
2849     x_schema_name         OUT nocopy varchar2,
2850     x_return_status       OUT nocopy varchar2)
2851   IS
2852     l_status        varchar2(1);
2853     l_industry      varchar2(1);
2854     l_oracle_schema varchar2(30);
2855     l_return        boolean;
2856   BEGIN
2857 
2858     x_return_status := fnd_api.g_ret_sts_success;
2859 
2860     l_return := fnd_installation.get_app_info(
2861                   application_short_name => p_product_short_name,
2862                   status                 => l_status,
2863                   industry               => l_industry,
2864                   oracle_schema          => l_oracle_schema);
2865 
2866     IF NOT l_return THEN
2867       fnd_message.set_name('CSI', 'CSI_FND_INVALID_SCHEMA_ERROR');
2868       fnd_msg_pub.add;
2869       RAISE fnd_api.g_exc_error;
2870     END IF;
2871 
2872     x_schema_name := l_oracle_schema;
2873 
2874   EXCEPTION
2875     WHEN fnd_api.g_exc_error THEN
2876       x_return_status := fnd_api.g_ret_sts_error;
2877   END get_schema_name;
2878 
2879   PROCEDURE truncate_table(
2880     p_table_name    in varchar2)
2881   IS
2882     l_num_of_rows      number;
2883     l_truncate_handle  pls_integer := dbms_sql.open_cursor;
2884     l_statement        varchar2(200);
2885   BEGIN
2886     l_statement := 'truncate table '||p_table_name;
2887     dbms_sql.parse(l_truncate_handle, l_statement, dbms_sql.native);
2888     l_num_of_rows := dbms_sql.execute(l_truncate_handle);
2889     dbms_sql.close_cursor(l_truncate_handle);
2890   EXCEPTION
2891     WHEN others THEN
2892       null;
2893   END truncate_table;
2894 
2895   PROCEDURE get_source_type(
2896     p_mtl_txn_id         IN number,
2897     p_mtl_type_id        IN number,
2898     p_mtl_action_id      IN number,
2899     p_mtl_source_type_id IN number,
2900     p_mtl_type_class     IN number,
2901     p_mtl_txn_qty        IN number,
2902     p_release            IN varchar2,
2903     x_source_type        OUT nocopy varchar2,
2904     x_csi_txn_type_id    OUT nocopy number)
2905   IS
2906   BEGIN
2907 
2908     -- Move order issue to project
2909     IF p_mtl_action_id = 1 AND p_mtl_source_type_id = 4 AND p_mtl_type_class = 1 THEN
2910       x_source_type := 'CSIISUPT';
2911       x_csi_txn_type_id := 113;
2912       return;
2913     END IF;
2914 
2915     -- Miscellaneous issue to project
2916     IF p_mtl_action_id = 1 AND p_mtl_source_type_id in (3,6,13) AND p_mtl_type_class = 1 THEN
2917       x_source_type := 'CSIMSIPT';
2918       x_csi_txn_type_id := 121;
2919       return;
2920     END IF;
2921 
2922     -- Miscellaneous Receipt from project
2923     IF p_mtl_action_id = 27 AND p_mtl_source_type_id in (3,6,13) AND p_mtl_type_class = 1 THEN
2924       x_source_type := 'CSIMSRPT';
2925       x_csi_txn_type_id := 120;
2926       return;
2927     END IF;
2928 
2929     -- project contract issue
2930     IF p_mtl_action_id = 1 AND p_mtl_source_type_id = 16 THEN
2931       x_source_type     := 'CSIOKSHP';
2932       x_csi_txn_type_id := 326;
2933       return;
2934     END IF;
2935 
2936     -- Sales Order Shipment
2937     IF p_mtl_action_id = 1 AND p_mtl_source_type_id = 2 THEN
2938       x_source_type     := 'CSISOSHP';
2939       x_csi_txn_type_id := 51;
2940       return;
2941     END IF;
2942 
2943     -- RMA receipt
2944     IF p_mtl_action_id = 27 AND p_mtl_source_type_id = 12 THEN
2945       x_source_type     := 'CSIRMARC';
2946       x_csi_txn_type_id := 53;
2947       return;
2948     END IF;
2949 
2950     -- Subinventory Transfers
2951     IF(p_mtl_txn_qty > 0 AND   p_mtl_action_id = 2)
2952         OR
2953        (p_mtl_action_id = 28 AND p_mtl_source_type_id = 2 AND p_mtl_txn_qty > 0)
2954         OR
2955        (p_mtl_action_id = 28 AND p_mtl_source_type_id = 8 AND p_mtl_txn_qty > 0)
2956     THEN
2957       x_source_type := 'CSISUBTR';
2958       x_csi_txn_type_id := 114;
2959       return;
2960     END IF;
2961 
2962     --Interorg transit receipt
2963     IF p_mtl_action_id = 12 AND p_mtl_source_type_id = 13 THEN
2964       x_source_type := 'CSIORGTR';
2965       x_csi_txn_type_id := 144;
2966       return;
2967     END IF;
2968 
2969     --Interorg transit shipment
2970     IF p_mtl_action_id = 21 AND p_mtl_source_type_id = 13 THEN
2971       x_source_type := 'CSIORGTS';
2972       x_csi_txn_type_id := 145;
2973       return;
2974     END IF;
2975 
2976     --Interorg Direct Shipment
2977     IF p_mtl_action_id = 3 AND  p_mtl_source_type_id = 13 AND p_mtl_txn_qty > 0 THEN
2978       x_source_type := 'CSIORGDS';
2979       x_csi_txn_type_id := 143;
2980       return;
2981     END IF;
2982 
2983     -- ISO requisition receipt
2984     IF p_mtl_action_id = 12 AND p_mtl_source_type_id = 7 THEN
2985       x_source_type := 'CSIINTSR';
2986       x_csi_txn_type_id := 131;
2987       return;
2988     END IF;
2989 
2990     -- ISO shipment
2991     IF p_mtl_action_id = 21 AND p_mtl_source_type_id = 8 THEN
2992       x_source_type := 'CSIINTSS';
2993       x_csi_txn_type_id := 130;
2994       return;
2995     END IF;
2996 
2997     -- ISO direct shipment
2998     IF p_mtl_action_id = 3 AND p_mtl_source_type_id = 7 AND p_mtl_txn_qty > 0 THEN
2999       x_source_type := 'CSIINTDS';
3000       x_csi_txn_type_id := 142;
3001       return;
3002     END IF;
3003 
3004     -- PO receipt
3005     IF p_mtl_action_id = 27 AND p_mtl_source_type_id = 1 THEN
3006       x_source_type := 'CSIPOINV';
3007       x_csi_txn_type_id := 112;
3008       return;
3009     END IF;
3010 
3011     -- cycle count
3012     IF p_mtl_action_id = 4 THEN
3013       x_source_type := 'CSICYCNT';
3014       x_csi_txn_type_id := 119;
3015       return;
3016     END IF;
3017 
3018     --physical inventory
3019     IF p_mtl_action_id = 8 THEN
3020       x_source_type := 'CSIPHYIN';
3021       x_csi_txn_type_id := 118;
3022       return;
3023     END IF;
3024 
3025     -- miscellaneous receipt
3026     --
3027     IF(p_mtl_action_id = 27 AND p_mtl_type_id NOT IN (15,123,43,94) AND
3028          (p_mtl_type_class is null OR p_mtl_type_class <> 1))
3029         OR
3030        (p_mtl_action_id = 29 AND p_mtl_txn_qty > 0 AND p_mtl_source_type_id = 1)
3031         OR
3032        (p_mtl_action_id = 29 AND p_mtl_txn_qty > 0 AND p_mtl_source_type_id = 13)
3033         OR
3034        (p_mtl_action_id = 29 AND p_mtl_txn_qty > 0 AND p_mtl_source_type_id = 7)
3035     THEN
3036       x_source_type := 'CSIMSRCV';
3037       x_csi_txn_type_id := 117;
3038       return;
3039     END IF;
3040 
3041     -- miscellaneous issue
3042     IF (p_mtl_action_id = 1 AND p_mtl_source_type_id in (4,13,6,3,8)
3043        AND
3044        p_mtl_type_id NOT IN (33,122,35,37,93)
3045        AND
3046        (p_mtl_type_class is null OR p_mtl_type_class <> 1))
3047       OR
3048       (p_mtl_action_id = 29 AND p_mtl_txn_qty < 0 AND p_mtl_source_type_id = 1)
3049       OR
3050       (p_mtl_action_id = 1 AND p_mtl_txn_qty < 0 AND p_mtl_source_type_id = 1)
3051         OR
3052        (p_mtl_action_id = 29 AND p_mtl_txn_qty < 0 AND p_mtl_source_type_id = 13)
3053         OR
3054        (p_mtl_action_id = 29 AND p_mtl_txn_qty < 0 AND p_mtl_source_type_id = 7)
3055     THEN
3056       x_source_type := 'CSIMSISU';
3057       x_csi_txn_type_id := 116;
3058       return;
3059     END IF;
3060 
3061     --wip assy return
3062     IF p_mtl_action_id = 32 AND p_mtl_source_type_id = 5 THEN
3063       x_source_type := 'CSIWIPAR';
3064       x_csi_txn_type_id := 71;
3065       return;
3066     END IF;
3067 
3068     -- wip component issue
3069     IF p_mtl_action_id = 1 AND p_mtl_source_type_id = 5 THEN
3070       x_source_type := 'CSIWIPCI';
3071       x_csi_txn_type_id := 71;
3072       return;
3073     END IF;
3074 
3075     -- wip negative comp issue
3076     IF p_mtl_action_id = 33 AND p_mtl_source_type_id = 5 THEN
3077       x_source_type := 'CSIWIPNI';
3078       x_csi_txn_type_id := 72;
3079       return;
3080     END IF;
3081 
3082     -- wip component return
3083     IF p_mtl_action_id = 27 AND p_mtl_source_type_id = 5 THEN
3084       x_source_type := 'CSIWIPCR';
3085       x_csi_txn_type_id := 72;
3086       return;
3087     END IF;
3088 
3089     -- wip assembly completion
3090     IF p_mtl_action_id = 31 AND p_mtl_source_type_id = 5 THEN
3091       x_source_type := 'CSIWIPAC';
3092       x_csi_txn_type_id := 73;
3093       return;
3094     END IF;
3095 
3096     -- wip negative comp return
3097     IF p_mtl_action_id = 34 AND p_mtl_source_type_id = 5 THEN
3098       x_source_type := 'CSIWIPNR';
3099       x_csi_txn_type_id := 71;
3100       return;
3101     END IF;
3102 
3103     x_source_type     := 'NONE';
3104     x_csi_txn_type_id := -1;
3105 
3106   END get_source_type;
3107 
3108   PROCEDURE pump_txn_error(
3109     p_mtl_txn_id         IN number,
3110     p_mtl_type_id        IN number,
3111     p_mtl_action_id      IN number,
3112     p_mtl_source_type_id IN number,
3113     p_mtl_type_class     IN number,
3114     p_mtl_txn_qty        IN number,
3115     p_dequeue_flag       IN varchar2 default 'N',
3116     p_release            IN varchar2)
3117   IS
3118 
3119     l_error_rec          csi_datastructures_pub.transaction_error_rec;
3120     l_error_id           number;
3121     l_return_status      varchar2(1) := fnd_api.g_ret_sts_success;
3122     l_msg_count          number;
3123     l_msg_data           varchar2(2000);
3124     l_source_type        varchar2(20);
3125     l_csi_txn_type_id    number;
3126     l_message_string     varchar2(2000);
3127 
3128     l_error_message      varchar2(2000);
3129   BEGIN
3130 
3131      savepoint pump_txn_error;
3132 
3133      IF p_dequeue_flag = 'Y' THEN
3134        l_error_rec.processed_flag            := 'R';
3135      ELSE
3136        l_error_rec.processed_flag            := 'E';
3137      END IF;
3138 
3139      l_error_rec.error_text                  := 'Missing transaction in Install Base.';
3140      l_error_rec.error_stage                 := 'IB_UPDATE';
3141      l_error_rec.inv_material_transaction_id := p_mtl_txn_id;
3142      l_error_rec.source_id                   := p_mtl_txn_id;
3143      cse_util_pkg.build_error_string(l_message_string,'MTL_TRANSACTION_ID',p_mtl_txn_id);
3144      l_error_rec.message_string              := l_message_string;
3145 
3146      get_source_type(
3147        p_mtl_txn_id         => p_mtl_txn_id,
3148        p_mtl_type_id        => p_mtl_type_id,
3149        p_mtl_action_id      => p_mtl_action_id,
3150        p_mtl_source_type_id => p_mtl_source_type_id,
3151        p_mtl_type_class     => p_mtl_type_class,
3152        p_mtl_txn_qty        => p_mtl_txn_qty,
3153        p_release            => p_release,
3154        x_source_type        => l_source_type,
3155        x_csi_txn_type_id    => l_csi_txn_type_id);
3156 
3157      l_error_rec.source_type                 := l_source_type;
3158      l_error_rec.transaction_type_id         := l_csi_txn_type_id;
3159 
3160      log('  '||l_error_rec.inv_material_transaction_id||' '||l_error_rec.source_type||' '||
3161            l_error_rec.transaction_type_id||' '||p_mtl_type_id||' '||p_mtl_action_id||' '||
3162            p_mtl_source_type_id);
3163 
3164      csi_transactions_pvt.create_txn_error (
3165        p_api_version          => 1.0,
3166        p_init_msg_list        => fnd_api.g_true,
3167        p_commit               => fnd_api.g_false,
3168        p_validation_level     => fnd_api.g_valid_level_full,
3169        p_txn_error_rec        => l_error_rec,
3170        x_transaction_error_id => l_error_id,
3171        x_return_status        => l_return_status,
3172        x_msg_count            => l_msg_count,
3173        x_msg_data             => l_msg_data);
3174 
3175      IF l_return_status <> fnd_api.g_ret_sts_success THEN
3176        RAISE fnd_api.g_exc_error;
3177      END IF;
3178 
3179      commit;
3180 
3181   EXCEPTION
3182     WHEN fnd_api.g_exc_error THEN
3183       rollback to pump_txn_error;
3184       l_error_message := csi_t_gen_utility_pvt.dump_error_stack;
3185       log('  Error: '||l_error_message);
3186   END pump_txn_error;
3187 
3188   PROCEDURE decode_queue is
3189     CURSOR msg_cur(p_freeze_date IN date) IS
3190       SELECT msg_id,
3191              msg_code,
3192              msg_status,
3193              body_text,
3194              creation_date,
3195              description
3196       FROM   xnp_msgs
3197       WHERE  (msg_code like 'CSI%' OR msg_code like 'CSE%')
3198       AND    recipient_name is null
3199       AND    msg_status IN ('READY', 'FAILED', 'REJECTED')
3200       AND    msg_creation_date > p_freeze_date;
3201 
3202     l_amount        integer;
3203     l_msg_text      varchar2(32767);
3204     l_source_id     varchar2(200);
3205     l_source_type   varchar2(30);
3206 
3207     l_schema_name   varchar2(30);
3208     l_object_name   varchar2(80);
3209     l_freeze_date   date;
3210     l_return_status varchar2(1) := fnd_api.g_ret_sts_success;
3211 
3212   BEGIN
3213 
3214     log(date_time_stamp||'  begin decode_queue');
3215 
3216     get_schema_name(
3217       p_product_short_name  => 'CSI',
3218       x_schema_name         => l_schema_name,
3219       x_return_status       => l_return_status);
3220 
3221     IF l_return_status <> fnd_api.g_ret_sts_success THEN
3222       RAISE fnd_api.g_exc_error;
3223     END IF;
3224 
3225     l_object_name := l_schema_name||'.csi_xnp_msgs_temp';
3226 
3227     truncate_table(l_object_name);
3228 
3229     IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
3230       csi_gen_utility_pvt.populate_install_param_rec;
3231     END IF;
3232 
3233     l_freeze_date := csi_datastructures_pub.g_install_param_rec.freeze_date;
3234 
3235     FOR msg_rec in msg_cur(l_freeze_date)
3236     LOOP
3237 
3238       l_amount := null;
3239       l_amount := dbms_lob.getlength(msg_rec.body_text);
3240       l_msg_text := null;
3241 
3242       dbms_lob.read(
3243         lob_loc => msg_rec.body_text,
3244         amount  => l_amount,
3245         offset  => 1,
3246         buffer  => l_msg_text );
3247 
3248       l_source_id := null;
3249 
3250       IF msg_rec.msg_code in ('CSISOFUL', 'CSIRMAFL') THEN
3251         xnp_xml_utils.decode(l_msg_text, 'ORDER_LINE_ID', l_source_id);
3252         l_source_type := 'ORDER_LINE_ID';
3253       ELSE
3254         xnp_xml_utils.decode(l_msg_text, 'MTL_TRANSACTION_ID', l_source_id);
3255         l_source_type := 'MTL_TRANSACTION_ID';
3256       END IF;
3257 
3258       INSERT INTO csi_xnp_msgs_temp(
3259         msg_id,
3260         msg_code,
3261         msg_text,
3262         msg_status,
3263         source_id,
3264         source_type,
3265         creation_date,
3266         description,
3267         process_flag)
3268       VALUES(
3269         msg_rec.msg_id,
3270         msg_rec.msg_code,
3271         msg_rec.body_text, --l_msg_text, --Changed for Bug 13384784,
3272         msg_rec.msg_status,
3273         l_source_id,
3274         l_source_type,
3275         msg_rec.creation_date,
3276         msg_rec.description,
3277         'Y');
3278       IF mod(msg_cur%rowcount, 100) = 0 THEN
3279         commit;
3280       END IF;
3281     END LOOP;
3282 
3283     log(date_time_stamp||'  end decode_queue');
3284   END decode_queue;
3285 
3286   PROCEDURE dequeue_messages_as_errors
3287   IS
3288 
3289     CURSOR q_cur IS
3290       SELECT msg_id,
3291              msg_code,
3292              msg_text,
3293              msg_status,
3294              source_id,
3295              source_type,
3296              creation_date,
3297              description,
3298              process_flag
3299       FROM   csi_xnp_msgs_temp
3300       WHERE  source_type = 'MTL_TRANSACTION_ID'
3301       AND    source_id   is not null;
3302 
3303       l_release             number;
3304       l_mtl_txn_id          number;
3305       l_mtl_type_id         number;
3306       l_mtl_action_id       number;
3307       l_mtl_source_type_id  number;
3308       l_mtl_type_class      number;
3309       l_mtl_txn_qty         number;
3310 
3311       skip_message          exception;
3312 
3313   BEGIN
3314 
3315     SELECT fnd_Profile.value('csi_upgrading_from_release')
3316     INTO   l_release
3317     FROM   sys.dual;
3318 
3319     decode_queue;
3320 
3321     FOR q_rec IN q_cur
3322     LOOP
3323 
3324       BEGIN
3325 
3326         BEGIN
3327 
3328           SELECT mmt.transaction_id,
3329                  mmt.transaction_type_id ,
3330                  mmt.transaction_action_id,
3331                  mmt.transaction_source_type_id,
3332                  mtt.type_class,
3333                  mmt.transaction_quantity
3334           INTO   l_mtl_txn_id,
3335                  l_mtl_type_id,
3336                  l_mtl_action_id,
3337                  l_mtl_source_type_id,
3338                  l_mtl_type_class,
3339                  l_mtl_txn_qty
3340           FROM   mtl_material_transactions mmt,
3341                  mtl_transaction_types     mtt
3342           WHERE  mmt.transaction_id      = q_rec.source_id
3343           AND    mtt.transaction_type_id = mmt.transaction_type_id;
3344 
3345         EXCEPTION
3346           WHEN no_data_found THEN
3347             RAISE skip_message;
3348           WHEN too_many_rows THEN
3349             RAISE skip_message;
3350         END;
3351 
3352         pump_txn_error(
3353           p_mtl_txn_id         => l_mtl_txn_id,
3354           p_mtl_type_id        => l_mtl_type_id,
3355           p_mtl_action_id      => l_mtl_action_id,
3356           p_mtl_source_type_id => l_mtl_source_type_id,
3357           p_mtl_type_class     => l_mtl_type_class,
3358           p_mtl_txn_qty        => l_mtl_txn_qty,
3359           p_dequeue_flag       => 'Y',
3360           p_release            => l_release);
3361 
3362         -- this code needs to be changed to appropriate call in xnp
3363         UPDATE xnp_msgs
3364         SET    msg_status = 'PROCESSED'
3365         WHERE  msg_id     = q_rec.msg_id;
3366 
3367       EXCEPTION
3368         WHEN skip_message THEN
3369           log('  bad mtl_txn_id for message in sfm queue message_id :'||q_rec.msg_id);
3370       END;
3371     END LOOP;
3372 
3373   END dequeue_messages_as_errors;
3374 
3375   PROCEDURE pump_prior_wip_missing_txns(
3376     p_mtl_creation_date IN date,
3377     p_mtl_txn_id        IN number,
3378     p_wip_job_id        IN number)
3379   IS
3380     CURSOR prior_cur(pc_mtl_creation_date IN date, pc_wip_job_id IN number, p_freeze_date in date) IS
3381       SELECT transaction_id                  mtl_txn_id,
3382              mmt.transaction_action_id       mtl_action_id,
3383              mmt.transaction_source_type_id  mtl_source_type_id,
3384              mmt.transaction_source_id       mtl_source_id,
3385              mmt.transaction_type_id         mtl_type_id,
3386              mtt.type_class                  mtl_type_class,
3387              mmt.transaction_quantity        mtl_txn_qty
3388       FROM   mtl_system_items          msi,
3389              mtl_transaction_types     mtt,
3390              mtl_material_transactions mmt
3391       WHERE  mmt.transaction_source_type_id = 5
3392       AND    mmt.transaction_action_id IN (1, 27, 31, 32, 33, 34)
3393       AND    mmt.transaction_date        > p_freeze_date
3394       AND    mmt.transaction_source_id   = pc_wip_job_id
3395       AND    mmt.creation_date           < pc_mtl_creation_date
3396       AND    mtt.transaction_type_id     = mmt.transaction_type_id
3397       AND    msi.organization_id         = mmt.organization_id
3398       AND    msi.inventory_item_id       = mmt.inventory_item_id
3399       AND    nvl(msi.comms_nl_trackable_flag, 'N') = 'Y';
3400 
3401     l_release             varchar2(80);
3402     l_in_error            boolean;
3403     l_txn_error_flag      varchar2(1);
3404 
3405     l_in_queue            boolean;
3406     l_pending_msg_found   varchar2(1);
3407 
3408     l_processed           boolean;
3409     l_processed_flag      varchar2(1);
3410 
3411     TYPE missing_txn_rec is RECORD(
3412       mtl_txn_id          number,
3413       mtl_type_id         number,
3414       mtl_action_id       number,
3415       mtl_source_type_id  number,
3416       mtl_type_class      number,
3417       mtl_txn_qty         number);
3418 
3419     TYPE missing_txn_tbl is TABLE OF missing_txn_rec INDEX BY BINARY_INTEGER;
3420 
3421     l_missing_tbl         missing_txn_tbl;
3422     l_m_ind               binary_integer := 0;
3423 
3424     l_freeze_date         date;
3425 
3426   BEGIN
3427 
3428     l_missing_tbl.delete;
3429 
3430     SELECT fnd_Profile.value('CSI_UPGRADING_FROM_RELEASE')
3431     INTO   l_release
3432     FROM   sys.dual;
3433     --
3434     IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
3435        csi_gen_utility_pvt.populate_install_param_rec;
3436      END IF;
3437      --
3438     l_freeze_date := csi_datastructures_pub.g_install_param_rec.freeze_date;
3439     --
3440     FOR wip_txn_rec IN prior_cur(p_mtl_creation_date, p_wip_job_id, l_freeze_date)
3441     LOOP
3442 
3443       -- check if processed
3444       BEGIN
3445         SELECT 'Y' INTO l_processed_flag
3446         FROM   sys.dual
3447         WHERE  exists(
3448           SELECT '1' FROM  csi_transactions
3449           WHERE  inv_material_transaction_id = wip_txn_rec.mtl_txn_id);
3450         l_processed := true;
3451       EXCEPTION
3452         WHEN no_data_found THEN
3453           l_processed := false;
3454       END;
3455 
3456       IF NOT(l_processed) THEN
3457         -- check if errors
3458         l_in_error := false;
3459         BEGIN
3460           SELECT 'Y' INTO l_txn_error_flag
3461           FROM   sys.dual
3462           WHERE  EXISTS (
3463             SELECT '1' FROM csi_txn_errors
3464             WHERE  inv_material_transaction_id = wip_txn_rec.mtl_txn_id
3465             AND    processed_flag in ('E', 'R'));
3466           l_in_error := true;
3467         EXCEPTION
3468           WHEN no_data_found THEN
3469             l_in_error := false;
3470         END;
3471 
3472         -- check if pending in queue
3473         IF NOT(l_in_error) THEN
3474           BEGIN
3475             SELECT 'Y' INTO l_pending_msg_found
3476             FROM   sys.dual
3477             WHERE  exists (
3478               SELECT '1'
3479               FROM   csi_xnp_msgs_temp
3480               WHERE  source_type = 'MTL_TRANSACTION_ID'
3481               AND    source_id   = wip_txn_rec.mtl_txn_id
3482               AND    nvl(msg_status, 'READY') <> 'PROCESSED');
3483             l_in_queue := true;
3484           EXCEPTION
3485             WHEN no_data_found THEN
3486               l_in_queue := false;
3487           END;
3488         END IF;
3489 
3490         IF NOT(l_in_error) and NOT(l_in_queue) THEN
3491 
3492           l_m_ind := l_m_ind + 1;
3493 
3494           l_missing_tbl(l_m_ind).mtl_txn_id         := wip_txn_rec.mtl_txn_id;
3495           l_missing_tbl(l_m_ind).mtl_type_id        := wip_txn_rec.mtl_type_id;
3496           l_missing_tbl(l_m_ind).mtl_action_id      := wip_txn_rec.mtl_action_id;
3497           l_missing_tbl(l_m_ind).mtl_source_type_id := wip_txn_rec.mtl_source_type_id;
3498           l_missing_tbl(l_m_ind).mtl_type_class     := wip_txn_rec.mtl_type_class;
3499           l_missing_tbl(l_m_ind).mtl_txn_qty        := wip_txn_rec.mtl_txn_qty;
3500 
3501         END IF;
3502 
3503       END IF; -- not processed
3504     END LOOP;
3505 
3506     IF l_missing_tbl.COUNT > 0 THEN
3507       FOR l_ind IN l_missing_tbl.FIRST .. l_missing_tbl.LAST
3508       LOOP
3509         pump_txn_error(
3510           p_mtl_txn_id         => l_missing_tbl(l_ind).mtl_txn_id,
3511           p_mtl_type_id        => l_missing_tbl(l_ind).mtl_type_id,
3512           p_mtl_action_id      => l_missing_tbl(l_ind).mtl_action_id,
3513           p_mtl_source_type_id => l_missing_tbl(l_ind).mtl_source_type_id,
3514           p_mtl_type_class     => l_missing_tbl(l_ind).mtl_type_class,
3515           p_mtl_txn_qty        => l_missing_tbl(l_ind).mtl_txn_qty,
3516           p_dequeue_flag       => 'N',
3517           p_release            => l_release);
3518       END LOOP;
3519     END IF;
3520 
3521     commit;
3522 
3523   END pump_prior_wip_missing_txns;
3524 
3525   PROCEDURE pump_srl_missing_txns(
3526     p_serial_number       IN varchar2,
3527     p_item_id             IN number,
3528     p_serial_code         IN number,
3529     p_lot_code            IN number,
3530     p_freeze_date         IN date)
3531   IS
3532 
3533     l_release             varchar2(80);
3534 
3535     l_serial_code         number;
3536     l_lot_code            number;
3537 
3538     l_in_error            boolean;
3539     l_txn_error_flag      varchar2(1);
3540 
3541     l_in_queue            boolean;
3542     l_pending_msg_found   varchar2(1);
3543 
3544     l_processed           boolean;
3545     l_processed_flag      varchar2(1);
3546 
3547     TYPE missing_txn_rec is RECORD(
3548       mtl_txn_id          number,
3549       mtl_type_id         number,
3550       mtl_action_id       number,
3551       mtl_source_type_id  number,
3552       mtl_type_class      number,
3553       mtl_txn_qty         number);
3554 
3555     TYPE missing_txn_tbl is TABLE OF missing_txn_rec INDEX BY BINARY_INTEGER;
3556 
3557     l_missing_tbl         missing_txn_tbl;
3558     l_m_ind               binary_integer := 0;
3559 
3560   BEGIN
3561 
3562 
3563     l_missing_tbl.delete;
3564 
3565     SELECT fnd_Profile.value('csi_upgrading_from_release')
3566     INTO   l_release
3567     FROM   sys.dual;
3568 
3569     FOR all_txn_rec in all_txn_cur(
3570       p_serial_number => p_serial_number,
3571       p_item_id       => p_item_id)
3572     LOOP
3573 
3574       IF all_txn_rec.mtl_txn_date > p_freeze_date THEN
3575 
3576         IF  csi_inv_trxs_pkg.valid_ib_txn(all_txn_rec.mtl_txn_id) THEN
3577 
3578           -- check if processed
3579           BEGIN
3580             IF all_txn_rec.mtl_action_id in (2,3,28) THEN
3581               SELECT 'Y' INTO l_processed_flag
3582               FROM   sys.dual
3583               WHERE  exists(
3584                 SELECT '1' FROM  csi_transactions
3585                 WHERE  (inv_material_transaction_id = all_txn_rec.mtl_txn_id
3586                        OR
3587                        inv_material_transaction_id = all_txn_rec.mtl_xfer_txn_id));
3588             ELSE
3589               SELECT 'Y' INTO l_processed_flag
3590               FROM   sys.dual
3591               WHERE  exists(
3592                 SELECT '1' FROM  csi_transactions
3593                 WHERE  inv_material_transaction_id = all_txn_rec.mtl_txn_id);
3594             END IF;
3595             l_processed := true;
3596           EXCEPTION
3597             WHEN no_data_found THEN
3598               l_processed := false;
3599           END;
3600 
3601           IF l_processed THEN
3602             EXIT;
3603           END IF;
3604 
3605           -- check if errors
3606           l_in_error := false;
3607           BEGIN
3608             IF all_txn_rec.mtl_action_id in (2,3,28) THEN
3609               SELECT 'Y' INTO l_txn_error_flag
3610               FROM   sys.dual
3611               WHERE  EXISTS (
3612                 SELECT '1' FROM csi_txn_errors
3613                 WHERE  (inv_material_transaction_id = all_txn_rec.mtl_txn_id
3614                         OR
3615                         inv_material_transaction_id = all_txn_rec.mtl_xfer_txn_id)
3616                 AND    processed_flag in ('E', 'R'));
3617             ELSE
3618               SELECT 'Y' INTO l_txn_error_flag
3619               FROM   sys.dual
3620               WHERE  EXISTS (
3621                 SELECT '1' FROM csi_txn_errors
3622                 WHERE  inv_material_transaction_id = all_txn_rec.mtl_txn_id
3623                 AND    processed_flag in ('E', 'R'));
3624             END IF;
3625             l_in_error := true;
3626           EXCEPTION
3627             WHEN no_data_found THEN
3628               l_in_error := false;
3629           END;
3630 
3631           -- check if pending in queue
3632           IF NOT(l_in_error) THEN
3633             BEGIN
3634               SELECT 'Y' INTO l_pending_msg_found
3635               FROM   sys.dual
3636               WHERE  exists (
3637                 SELECT '1'
3638                 FROM   csi_xnp_msgs_temp
3639                 WHERE  source_type = 'MTL_TRANSACTION_ID'
3640                 AND    source_id   = all_txn_rec.mtl_txn_id
3641                 AND    nvl(msg_status, 'READY') <> 'PROCESSED');
3642               l_in_queue := true;
3643             EXCEPTION
3644               WHEN no_data_found THEN
3645                 l_in_queue := false;
3646             END;
3647           END IF;
3648 
3649           IF NOT(l_in_error) and NOT(l_in_queue) THEN
3650 
3651             l_m_ind := l_m_ind + 1;
3652 
3653             l_missing_tbl(l_m_ind).mtl_txn_id         := all_txn_rec.mtl_txn_id;
3654             l_missing_tbl(l_m_ind).mtl_type_id        := all_txn_rec.mtl_type_id;
3655             l_missing_tbl(l_m_ind).mtl_action_id      := all_txn_rec.mtl_action_id;
3656             l_missing_tbl(l_m_ind).mtl_source_type_id := all_txn_rec.mtl_source_type_id;
3657             l_missing_tbl(l_m_ind).mtl_type_class     := all_txn_rec.mtl_type_class;
3658             l_missing_tbl(l_m_ind).mtl_txn_qty        := all_txn_rec.mtl_txn_qty;
3659 
3660           END IF;
3661 
3662         END IF; -- valid ib transaction
3663 
3664       END IF;
3665     END LOOP;
3666 
3667     IF l_missing_tbl.COUNT > 0 THEN
3668       FOR l_ind IN l_missing_tbl.FIRST .. l_missing_tbl.LAST
3669       LOOP
3670         pump_txn_error(
3671           p_mtl_txn_id         => l_missing_tbl(l_ind).mtl_txn_id,
3672           p_mtl_type_id        => l_missing_tbl(l_ind).mtl_type_id,
3673           p_mtl_action_id      => l_missing_tbl(l_ind).mtl_action_id,
3674           p_mtl_source_type_id => l_missing_tbl(l_ind).mtl_source_type_id,
3675           p_mtl_type_class     => l_missing_tbl(l_ind).mtl_type_class,
3676           p_mtl_txn_qty        => l_missing_tbl(l_ind).mtl_txn_qty,
3677           p_dequeue_flag       => 'N',
3678           p_release            => l_release);
3679       END LOOP;
3680     END IF;
3681 
3682     commit;
3683 
3684   END pump_srl_missing_txns;
3685 
3686   PROCEDURE pump_all_missing_txns
3687   IS
3688 
3689     TYPE NumTabType is    varray(10000) of number;
3690     TYPE VarTabType is    varray(10000) of varchar2(80);
3691 
3692     l_serial_number_tab   VarTabType;
3693     l_item_id_tab         NumTabType;
3694     l_organization_id_tab NumTabType;
3695 
3696     MAX_BUFFER_SIZE       number := 1000;
3697 
3698     CURSOR all_srl_cur  IS
3699       SELECT msn.serial_number              serial_number,
3700              msn.inventory_item_id          item_id,
3701              msn.current_organization_id    organization_id
3702       FROM   mtl_serial_numbers msn
3703       WHERE  exists (
3704         SELECT '1'
3705         FROM   mtl_system_items msi,
3706                mtl_parameters   mp
3707         WHERE  mp.organization_id    = msn.current_organization_id
3708         AND    msi.organization_id   = mp.master_organization_id
3709         AND    msi.inventory_item_id = msn.inventory_item_id
3710         AND    nvl(msi.comms_nl_trackable_flag, 'N') = 'Y');
3711 
3712     l_freeze_date         date;
3713     l_serial_code         number;
3714     l_lot_code            number;
3715 
3716   BEGIN
3717 
3718     IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
3719        csi_gen_utility_pvt.populate_install_param_rec;
3720     END IF;
3721     --
3722     l_freeze_date := csi_datastructures_pub.g_install_param_rec.freeze_date;
3723     --
3724     OPEN all_srl_cur;
3725     LOOP
3726 
3727       FETCH all_srl_cur BULK COLLECT
3728       INTO  l_serial_number_tab,
3729             l_item_id_tab,
3730             l_organization_id_tab
3731       LIMIT MAX_BUFFER_SIZE;
3732 
3733       FOR ind IN 1 .. l_serial_number_tab.COUNT
3734       LOOP
3735 
3736         log(' Serial Number : '||l_serial_number_tab(ind));
3737 
3738         SELECT serial_number_control_code,
3739                lot_control_code
3740         INTO   l_serial_code,
3741                l_lot_code
3742         FROM   mtl_system_items
3743         WHERE  inventory_item_id = l_item_id_tab(ind)
3744         AND    organization_id   = l_organization_id_tab(ind);
3745 
3746         pump_srl_missing_txns(
3747           p_serial_number       => l_serial_number_tab(ind),
3748           p_item_id             => l_item_id_tab(ind),
3749           p_serial_code         => l_serial_code,
3750           p_lot_code            => l_lot_code,
3751           p_freeze_date         => l_freeze_date);
3752 
3753       END LOOP;
3754 
3755       EXIT when all_srl_cur%NOTFOUND;
3756 
3757     END LOOP;
3758 
3759     IF all_srl_cur%ISOPEN THEN
3760       CLOSE all_srl_cur;
3761     END IF;
3762 
3763   END pump_all_missing_txns;
3764 
3765   PROCEDURE pump_err_missing_txns
3766   IS
3767 
3768     CURSOR tld_inst_cur(p_order_line_id in number) IS
3769       SELECT tld.instance_id,
3770              tld.serial_number,
3771              tld.inventory_item_id,
3772              tld.inv_organization_id
3773       FROM   csi_t_txn_line_details tld,
3774              csi_t_transaction_lines tl
3775       WHERE  tl.source_transaction_table = 'OE_ORDER_LINES_ALL'
3776       AND    tl.source_transaction_id    = p_order_line_id
3777       AND    tld.transaction_line_id     = tl.transaction_line_id
3778       AND    tld.instance_id is not null;
3779 
3780     l_mtl_source_type_id  number;
3781     l_mtl_action_id       number;
3782     l_wip_job_id          number;
3783 
3784     TYPE NumTabType is    varray(10000) of number;
3785     l_mtl_txn_id_tab      NumTabType;
3786     l_txn_error_id_tab    NumTabType;
3787     max_buffer_size       number := 1000;
3788 
3789     l_freeze_date         date;
3790     l_inv_item_id         number;
3791     l_inv_org_id          number;
3792     l_serial_code         number;
3793     l_lot_code            number;
3794 
3795     skip_error            exception;
3796 
3797     l_err_txn_date        date;
3798     l_order_line_id       number;
3799     l_inst_serial_number  varchar2(80);
3800     l_inst_item_id        number;
3801     l_inst_vld_org_id     number;
3802     l_mtl_creation_date   date;
3803 
3804   BEGIN
3805 
3806     log(date_time_stamp||'  begin pump_err_missing_txn');
3807 
3808     IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
3809        csi_gen_utility_pvt.populate_install_param_rec;
3810     END IF;
3811     --
3812     l_freeze_date := csi_datastructures_pub.g_install_param_rec.freeze_date;
3813     --
3814     OPEN error_cur;
3815     LOOP
3816 
3817       FETCH error_cur BULK COLLECT
3818       INTO  l_mtl_txn_id_tab,
3819             l_txn_error_id_tab
3820       LIMIT max_buffer_size;
3821 
3822       FOR ind IN 1 .. l_mtl_txn_id_tab.COUNT
3823       LOOP
3824 
3825         BEGIN
3826           BEGIN
3827             SELECT inventory_item_id,
3828                    organization_id,
3829                    transaction_source_type_id,
3830                    transaction_action_id,
3831                    transaction_source_id,
3832                    trx_source_line_id,
3833                    transaction_date,
3834                    creation_date
3835             INTO   l_inv_item_id,
3836                    l_inv_org_id,
3837                    l_mtl_source_type_id,
3838                    l_mtl_action_id,
3839                    l_wip_job_id,
3840                    l_order_line_id,
3841                    l_err_txn_date,
3842                    l_mtl_creation_date
3843             FROM   mtl_material_transactions
3844             WHERE  transaction_id = l_mtl_txn_id_tab(ind);
3845           EXCEPTION
3846             WHEN no_data_found THEN
3847               RAISE skip_error;
3848             WHEN too_many_rows THEN
3849               RAISE skip_error;
3850           END;
3851 
3852           IF l_err_txn_date < l_freeze_date THEN
3853             RAISE skip_error;
3854           END IF;
3855 
3856           SELECT serial_number_control_code,
3857                  lot_control_code
3858           INTO   l_serial_code,
3859                  l_lot_code
3860           FROM   mtl_system_items
3861           WHERE  inventory_item_id = l_inv_item_id
3862           AND    organization_id   = l_inv_org_id;
3863 
3864           FOR srl_rec in srl_cur(l_mtl_txn_id_tab(ind))
3865           LOOP
3866 
3867             pump_srl_missing_txns(
3868               p_serial_number       => srl_rec.serial_number,
3869               p_item_id             => srl_rec.item_id,
3870               p_serial_code         => l_serial_code,
3871               p_lot_code            => l_lot_code,
3872               p_freeze_date         => l_freeze_date);
3873 
3874           END LOOP;
3875 
3876           IF l_mtl_action_id = 31 AND l_mtl_source_type_id = 5 THEN
3877 
3878             /* this routine pumps the prior wip transactions even if a txn is
3879                successfully processed later than the prior transaction
3880                forced pump for prior WIP transaction
3881             */
3882             pump_prior_wip_missing_txns(
3883               p_mtl_creation_date => l_mtl_creation_date,
3884               p_mtl_txn_id        => l_mtl_txn_id_tab(ind),
3885               p_wip_job_id        => l_wip_job_id);
3886 
3887           END IF;
3888 
3889           IF (l_mtl_action_id = 1 AND l_mtl_source_type_id = 2) OR -- sales order issue
3890              (l_mtl_action_id = 1 AND l_mtl_source_type_id = 8) OR -- int order issue
3891              (l_mtl_action_id = 27 AND l_mtl_source_type_id = 12)  -- rma receipt
3892           THEN
3893 
3894             FOR tld_inst_rec IN tld_inst_cur(l_order_line_id)
3895             LOOP
3896 
3897               l_inst_serial_number := tld_inst_rec.serial_number;
3898               l_inst_item_id       := tld_inst_rec.inventory_item_id;
3899               l_inst_vld_org_id    := tld_inst_rec.inv_organization_id;
3900 
3901               IF l_inst_serial_number is null THEN
3902                 SELECT serial_number,
3903                        inventory_item_id,
3904                        last_vld_organization_id
3905                 INTO   l_inst_serial_number,
3906                        l_inst_item_id,
3907                        l_inst_vld_org_id
3908                 FROM   csi_item_instances
3909                 WHERE  instance_id = tld_inst_rec.instance_id;
3910               END IF;
3911 
3912               IF l_inst_serial_number is not null THEN
3913 
3914                 SELECT serial_number_control_code,
3915                        lot_control_code
3916                 INTO   l_serial_code,
3917                        l_lot_code
3918                 FROM   mtl_system_items
3919                 WHERE  inventory_item_id = l_inst_item_id
3920                 AND    organization_id   = l_inst_vld_org_id;
3921 
3922                 pump_srl_missing_txns(
3923                   p_serial_number       => l_inst_serial_number,
3924                   p_item_id             => l_inst_item_id,
3925                   p_serial_code         => l_serial_code,
3926                   p_lot_code            => l_lot_code,
3927                   p_freeze_date         => l_freeze_date);
3928 
3929               END IF;
3930 
3931             END LOOP;
3932           END IF;
3933 
3934 
3935         EXCEPTION
3936           WHEN skip_error THEN
3937             log('  bad csi_txn_errors.mtl_txn_id : '||l_mtl_txn_id_tab(ind));
3938         END;
3939       END LOOP;
3940 
3941       EXIT when error_cur%NOTFOUND;
3942 
3943     END LOOP;
3944 
3945     IF error_cur%ISOPEN THEN
3946       CLOSE error_cur;
3947     END IF;
3948 
3949     log(date_time_stamp||'  end pump_err_missing_txn');
3950 
3951   END pump_err_missing_txns;
3952 
3953   /* main routine for populating the error stage of the serial transaction */
3954   PROCEDURE get_srldata(
3955     p_single_error_flag  IN  varchar2,
3956     p_mtl_txn_id         IN  number)
3957   IS
3958 
3959     CURSOR single_error_cur(pc_mtl_txn_id IN number) is
3960       SELECT cte.inv_material_transaction_id mtl_txn_id,
3961              cte.transaction_error_id        txn_error_id
3962       FROM   csi_txn_errors cte
3963       WHERE  cte.inv_material_transaction_id = pc_mtl_txn_id
3964       AND    cte.processed_flag in ('E', 'R');
3965 
3966     TYPE NumTabType is       varray(10000) of number;
3967     l_mtl_txn_id_tab         NumTabType;
3968     l_txn_error_id_tab       NumTabType;
3969     max_buffer_size          number := 1000;
3970 
3971     l_release                varchar2(80);
3972 
3973     l_diag_seq_id            number := 1000;
3974     l_freeze_date            date;
3975     l_inv_item_id            number;
3976     l_inv_org_id             number;
3977 
3978     l_unit_txn_found         boolean;
3979     l_knock_err_txn          boolean;
3980     l_err_txn_serial_code    number;
3981     l_err_txn_lot_code       number;
3982     l_err_txn_source_type_id number;
3983     l_err_txn_action_id      number;
3984     l_err_txn_date           date;
3985 
3986     l_serial_code            number;
3987     l_lot_code               number;
3988     l_revision_code          number;
3989 
3990     l_inst_id                number;
3991     l_inst_vld_org_id        number;
3992     l_inst_location          varchar2(80);
3993     l_inst_usage_code        varchar2(30);
3994     l_owner_ip_id            number;
3995     l_owner_ipa_id           number;
3996     l_inst_owner_pty_id      number;
3997     l_inst_owner_acct_id     number;
3998     l_inst_end_date          date;
3999     l_inst_org_id            number;
4000     l_inst_subinv_name       varchar2(30);
4001     l_inst_rev_num           varchar2(10);
4002 
4003     l_csi_txn_id             number;
4004     l_csi_txn_type_id        number;
4005     l_order_line_id          number;
4006     l_rma_line_id            number;
4007     l_wip_job_id             number;
4008     l_create_flag            varchar2(1) := 'N';
4009     l_error_flag             varchar2(1);
4010     l_last_txn               boolean;
4011     l_last_txn_flag          varchar2(1);
4012 
4013     l_error                  varchar2(2000);
4014 
4015     l_already_inserted       varchar2(1);
4016     l_inst_mig_flag          varchar2(1);
4017     l_internal_party_id      number;
4018     l_process_flag           varchar2(1);
4019     l_temp_message           varchar2(540);
4020     l_source_type            varchar2(30);
4021 
4022     l_txn_rec                csi_datastructures_pub.transaction_rec;
4023     l_return_status          varchar2(1) := fnd_api.g_ret_sts_success;
4024     l_msg_count              number;
4025     l_msg_data               varchar2(2000);
4026 
4027     skip_insert              exception;
4028     skip_error               exception;
4029 
4030     l_schema_name            varchar2(30);
4031     l_object_name            varchar2(80);
4032 
4033   BEGIN
4034 
4035     -- dumps the xnp_msgs in to csi_xnp_msgs_tmp with the decoded value
4036     decode_queue;
4037 
4038     -- pump missing transaction for the serial numbers that are in error condition
4039     pump_err_missing_txns;
4040 
4041     get_schema_name(
4042       p_product_short_name  => 'CSI',
4043       x_schema_name         => l_schema_name,
4044       x_return_status       => l_return_status);
4045 
4046     IF l_return_status <> fnd_api.g_ret_sts_success THEN
4047       RAISE fnd_api.g_exc_error;
4048     END IF;
4049 
4050     l_object_name := l_schema_name||'.csi_diagnostics_temp';
4051 
4052     -- cleans up the temporary process table
4053     truncate_table(l_object_name);
4054 
4055     log(date_time_stamp||'  begin get_srldata');
4056 
4057     SELECT fnd_Profile.value('csi_upgrading_from_release')
4058     INTO   l_release
4059     FROM   sys.dual;
4060     --
4061     IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
4062       csi_gen_utility_pvt.populate_install_param_rec;
4063     END IF;
4064     --
4065     l_internal_party_id := csi_datastructures_pub.g_install_param_rec.internal_party_id;
4066     l_freeze_date       := csi_datastructures_pub.g_install_param_rec.freeze_date;
4067     --
4068     /* bulk fetch all the txn errors, 1000 records at a time */
4069     IF p_single_error_flag = 'Y' THEN
4070       OPEN single_error_cur(p_mtl_txn_id);
4071     ELSE
4072       /* bulk fetch all the txn errors, 1000 records at a time */
4073       OPEN error_cur;
4074     END IF;
4075     LOOP
4076 
4077       IF p_single_error_flag = 'Y' THEN
4078         FETCH single_error_cur BULK COLLECT
4079         INTO  l_mtl_txn_id_tab,
4080               l_txn_error_id_tab
4081         LIMIT max_buffer_size;
4082       ELSE
4083         FETCH error_cur BULK COLLECT
4084         INTO  l_mtl_txn_id_tab,
4085               l_txn_error_id_tab
4086         LIMIT max_buffer_size;
4087       END IF;
4088 
4089       FOR ind IN 1 .. l_mtl_txn_id_tab.COUNT
4090       LOOP
4091 
4092         BEGIN
4093 
4094           BEGIN
4095             SELECT inventory_item_id,
4096                    organization_id,
4097                    transaction_source_type_id,
4098                    transaction_action_id,
4099                    transaction_date
4100             INTO   l_inv_item_id,
4101                    l_inv_org_id,
4102                    l_err_txn_source_type_id,
4103                    l_err_txn_action_id,
4104                    l_err_txn_date
4105             FROM   mtl_material_transactions
4106             WHERE  transaction_id = l_mtl_txn_id_tab(ind);
4107           EXCEPTION
4108             WHEN no_data_found THEN
4109               RAISE skip_error;
4110             WHEN too_many_rows THEN
4111               RAISE skip_error;
4112           END;
4113 
4114           IF l_err_txn_date < l_freeze_date THEN
4115 
4116             UPDATE csi_txn_errors
4117             SET    processed_flag    = 'D',
4118                    error_text        = 'Transaction prior to freeze_date in install parameter',
4119                    last_update_login = fnd_global.login_id,
4120                    last_update_date  = sysdate,
4121                    last_updated_by   = fnd_global.user_id
4122             WHERE  transaction_error_id = l_txn_error_id_tab(ind);
4123 
4124             RAISE skip_error;
4125           END IF;
4126 
4127           SELECT serial_number_control_code,
4128                  lot_control_code
4129           INTO   l_err_txn_serial_code,
4130                  l_err_txn_lot_code
4131           FROM   mtl_system_items
4132           WHERE  inventory_item_id = l_inv_item_id
4133           AND    organization_id   = l_inv_org_id;
4134 
4135           l_unit_txn_found := FALSE;
4136 
4137           FOR srl_rec in srl_cur(
4138             p_mtl_txn_id  => l_mtl_txn_id_tab(ind))
4139           LOOP
4140 
4141             l_unit_txn_found      := TRUE;
4142 
4143             l_inst_id             := null;
4144             l_inst_vld_org_id     := null;
4145             l_inst_location       := null;
4146             l_inst_usage_code     := null;
4147             l_owner_ip_id         := null;
4148             l_owner_ipa_id        := null;
4149             l_inst_owner_pty_id   := null;
4150             l_inst_owner_acct_id  := null;
4151             l_inst_end_date       := null;
4152             l_inst_org_id         := null;
4153             l_inst_subinv_name    := null;
4154             l_inst_mig_flag       := null;
4155             l_process_flag        := 'N';
4156             l_temp_message        := null;
4157             l_create_flag         := 'N';
4158 
4159             BEGIN
4160               SELECT instance_id ,
4161                      last_vld_organization_id,
4162                      owner_party_id,
4163                      owner_party_account_id,
4164                      location_type_code,
4165                      instance_usage_code,
4166                      active_end_date,
4167                      inv_organization_id,
4168                      inv_subinventory_name,
4169                      inventory_revision,
4170                      nvl(migrated_flag, 'N')
4171               INTO   l_inst_id,
4172                      l_inst_vld_org_id,
4173                      l_inst_owner_pty_id,
4174                      l_inst_owner_acct_id,
4175                      l_inst_location,
4176                      l_inst_usage_code,
4177                      l_inst_end_date,
4178                      l_inst_org_id,
4179                      l_inst_subinv_name,
4180                      l_inst_rev_num,
4181                      l_inst_mig_flag
4182               FROM   csi_item_instances
4183               WHERE  inventory_item_id = srl_rec.item_id
4184               AND    serial_number     = srl_rec.serial_number;
4185 
4186               l_create_flag := 'N';
4187 
4188               BEGIN
4189                 SELECT instance_party_id
4190                 INTO   l_owner_ip_id
4191                 FROM   csi_i_parties
4192                 WHERE  instance_id            = l_inst_id
4193                 AND    relationship_type_code = 'OWNER'
4194                 AND    rownum                 = 1;
4195 
4196                 BEGIN
4197                   SELECT ip_account_id
4198                   INTO   l_owner_ipa_id
4199                   FROM   csi_ip_accounts
4200                   WHERE  instance_party_id      = l_owner_ip_id
4201                   AND    relationship_type_code = 'OWNER';
4202                 EXCEPTION
4203                   WHEN no_data_found THEN
4204                     l_owner_ipa_id := null;
4205                 END;
4206 
4207               EXCEPTION
4208                 WHEN no_data_found THEN
4209                   l_owner_ip_id := null;
4210               END;
4211 
4212             EXCEPTION
4213               WHEN no_data_found THEN
4214                 l_create_flag := 'Y';
4215                 null;
4216               WHEN too_many_rows THEN
4217                 l_create_flag  := 'N';
4218                 l_process_flag := 'E';
4219                 l_temp_message := 'Multiple serialized instances found';
4220             END;
4221 
4222             l_last_txn := TRUE;
4223 
4224             FOR txn_rec in all_txn_cur (
4225               p_serial_number => srl_rec.serial_number,
4226               p_item_id       => srl_rec.item_id)
4227             LOOP
4228 
4229               IF txn_rec.mtl_txn_date > l_freeze_date THEN
4230 
4231                 BEGIN
4232 
4233                   IF not csi_inv_trxs_pkg.valid_ib_txn(txn_rec.mtl_txn_id) THEN
4234                     raise skip_insert;
4235                   END IF;
4236 
4237                   IF l_last_txn THEN
4238                     l_last_txn_flag := 'Y';
4239                   ELSE
4240                     l_last_txn_flag := 'N';
4241                   END IF;
4242 
4243                   l_last_txn := FALSE;
4244 
4245                   l_order_line_id := null;
4246                   l_rma_line_id   := null;
4247                   l_wip_job_id    := null;
4248 
4249                   -- sales order shipment -- pick release
4250                   IF txn_rec.mtl_type_id in (33,52)  THEN
4251                     l_order_line_id := txn_rec.mtl_source_line_id;
4252                   END IF;
4253 
4254                   -- rma receipt
4255                   IF txn_rec.mtl_type_id = 15 THEN
4256                     l_rma_line_id := txn_rec.mtl_source_line_id;
4257                   END IF;
4258 
4259                   -- wip transactions
4260                   IF txn_rec.mtl_source_type_id = 5 THEN
4261                     l_wip_job_id  := txn_rec.mtl_source_id;
4262                   END IF;
4263 
4264                   l_csi_txn_id      := null;
4265                   l_csi_txn_type_id := null;
4266 
4267                   BEGIN
4268                     IF txn_rec.mtl_action_id in (2,3,28) THEN
4269                       SELECT cte.transaction_type_id,
4270                              cte.error_text
4271                       INTO   l_csi_txn_type_id,
4272                              l_error
4273                       FROM   csi_txn_errors cte
4274                       WHERE  (cte.inv_material_transaction_id = txn_rec.mtl_txn_id
4275                               OR
4276                               cte.inv_material_transaction_id = txn_rec.mtl_xfer_txn_id)
4277                       AND    cte.processed_flag  in ('E', 'R')
4278                       AND    rownum = 1;
4279                     ELSE
4280                       SELECT cte.transaction_type_id,
4281                              cte.error_text
4282                       INTO   l_csi_txn_type_id,
4283                              l_error
4284                       FROM   csi_txn_errors cte
4285                       WHERE  cte.inv_material_transaction_id = txn_rec.mtl_txn_id
4286                       AND    cte.processed_flag  in ('E', 'R')
4287                       AND    rownum = 1;
4288                     END IF;
4289 
4290                     l_error_flag := 'E';
4291                   EXCEPTION
4292                     WHEN no_data_found THEN
4293                       l_error_flag := 'M';
4294                       l_error := null;
4295                   END;
4296 
4297                   BEGIN
4298                     IF txn_rec.mtl_action_id in (2,3,28) THEN
4299                       SELECT transaction_id,
4300                              transaction_type_id
4301                       INTO   l_csi_txn_id,
4302                              l_csi_txn_type_id
4303                       FROM   csi_transactions
4304                       WHERE (inv_material_transaction_id = txn_rec.mtl_txn_id
4305                              OR
4306                              inv_material_transaction_id = txn_rec.mtl_xfer_txn_id)
4307                       AND    rownum = 1;
4308                     ELSE
4309                       SELECT transaction_id,
4310                              transaction_type_id
4311                       INTO   l_csi_txn_id,
4312                              l_csi_txn_type_id
4313                       FROM   csi_transactions
4314                       WHERE  inv_material_transaction_id = txn_rec.mtl_txn_id
4315                       AND    rownum = 1;
4316                     END IF;
4317                   EXCEPTION
4318                     WHEN no_data_found THEN
4319                       l_csi_txn_id := null;
4320                   END;
4321 
4322                   IF nvl(l_error_flag,'P') = 'E' THEN
4323                     l_error_flag := 'E';
4324                   ELSE
4325                     IF l_csi_txn_id is not null THEN
4326                       l_error_flag := 'P';
4327                     END IF;
4328                   END IF;
4329 
4330                   BEGIN
4331                     SELECT 'Y' INTO l_already_inserted
4332                     FROM   csi_diagnostics_temp
4333                     WHERE  serial_number      = srl_rec.serial_number
4334                     AND    inventory_item_id  = srl_rec.item_id
4335                     AND    mtl_txn_id         = txn_rec.mtl_txn_id;
4336                   EXCEPTION
4337                     WHEN no_data_found THEN
4338 
4339                       SELECT serial_number_control_code,
4340                              lot_control_code ,
4341                              revision_qty_control_code
4342                       INTO   l_serial_code,
4343                              l_lot_code,
4344                              l_revision_code
4345                       FROM   mtl_system_items
4346                       WHERE  inventory_item_id = txn_rec.item_id
4347                       AND    organization_id   = txn_rec.organization_id;
4348 
4349                       get_source_type(
4350                         p_mtl_txn_id         => txn_rec.mtl_txn_id,
4351                         p_mtl_type_id        => txn_rec.mtl_type_id,
4352                         p_mtl_action_id      => txn_rec.mtl_action_id,
4353                         p_mtl_source_type_id => txn_rec.mtl_source_type_id,
4354                         p_mtl_type_class     => txn_rec.mtl_type_class,
4355                         p_mtl_txn_qty        => txn_rec.mtl_txn_qty,
4356                         p_release            => l_release,
4357                         x_source_type        => l_source_type,
4358                         x_csi_txn_type_id    => l_csi_txn_type_id);
4359 
4360                       l_diag_seq_id := l_diag_seq_id + 1;
4361 
4362                       INSERT INTO csi_diagnostics_temp(
4363                         diag_seq_id,
4364                         serial_number,
4365                         lot_number,
4366                         serial_control_code,
4367                         lot_control_code,
4368                         revision_control_code,
4369                         mtl_creation_date,
4370                         mtl_txn_date,
4371                         mtl_txn_id,
4372                         mtl_txn_name,
4373                         mtl_txn_qty,
4374                         mtl_xfer_txn_id,
4375                         mtl_item_revision,
4376                         inventory_item_id,
4377                         organization_id,
4378                         error_flag,
4379                         error_text,
4380                         mtl_type_id,
4381                         mtl_action_id,
4382                         mtl_src_type_id,
4383                         mtl_type_class,
4384                         source_type,
4385                         csi_txn_type_id,
4386                         csi_txn_id,
4387                         oe_order_line_id,
4388                         wip_job_id,
4389                         oe_rma_line_id,
4390                         instance_id,
4391                         instance_vld_organization_id,
4392                         instance_location,
4393                         instance_usage_code,
4394                         instance_organization_id,
4395                         instance_subinv_name,
4396                         instance_revision,
4397                         instance_end_date,
4398                         instance_owner_party_id,
4399                         instance_owner_account_id,
4400                         instance_mig_flag,
4401                         internal_party_id,
4402                         last_transaction_flag,
4403                         create_flag,
4404                         process_flag,
4405                         temporary_message)
4406                       VALUES(
4407                         l_diag_seq_id,
4408                         srl_rec.serial_number,
4409                         txn_rec.lot_number,
4410                         l_serial_code,
4411                         l_lot_code,
4412                         l_revision_code,
4413                         txn_rec.mtl_creation_date,
4414                         txn_rec.mtl_txn_date,
4415                         txn_rec.mtl_txn_id,
4416                         txn_rec.mtl_txn_name,
4417                         txn_rec.mtl_txn_qty,
4418                         txn_rec.mtl_xfer_txn_id,
4419                         txn_rec.mtl_revision,
4420                         srl_rec.item_id,
4421                         txn_rec.organization_id,
4422                         l_error_flag,
4423                         l_error,
4424                         txn_rec.mtl_type_id,
4425                         txn_rec.mtl_action_id,
4426                         txn_rec.mtl_source_type_id,
4427                         txn_rec.mtl_type_class,
4428                         l_source_type,
4429                         l_csi_txn_type_id,
4430                         l_csi_txn_id,
4431                         l_order_line_id,
4432                         l_wip_job_id,
4433                         l_rma_line_id,
4434                         l_inst_id,
4435                         l_inst_vld_org_id,
4436                         l_inst_location,
4437                         l_inst_usage_code,
4438                         l_inst_org_id,
4439                         l_inst_subinv_name,
4440                         l_inst_rev_num,
4441                         l_inst_end_date,
4442                         l_inst_owner_pty_id,
4443                         l_inst_owner_acct_id,
4444                         l_inst_mig_flag,
4445                         l_internal_party_id,
4446                         l_last_txn_flag,
4447                         l_create_flag,
4448                         l_process_flag,
4449                         l_temp_message);
4450 
4451                       commit;
4452 
4453                   END;
4454 
4455                 EXCEPTION
4456                   WHEN skip_insert THEN
4457                     null;
4458                 END;
4459               END IF;
4460             END LOOP;
4461           END LOOP;
4462 
4463           l_knock_err_txn  := FALSE;
4464 
4465           -- serialized and no unit txn
4466           IF l_err_txn_serial_code IN (2, 5) AND NOT(l_unit_txn_found) THEN
4467             l_knock_err_txn := TRUE;
4468           END IF;
4469 
4470           -- non serial and having unit txn
4471           IF l_err_txn_serial_code = 1 AND l_unit_txn_found THEN
4472             l_knock_err_txn := TRUE;
4473           END IF;
4474 
4475           IF l_err_txn_serial_code = 6 THEN
4476             -- exclude rma and shipments from knocking
4477             IF (l_err_txn_action_id = 27 AND l_err_txn_source_type_id = 12) -- RMA
4478                 OR
4479                (l_err_txn_action_id = 1  AND l_err_txn_source_type_id = 2)  -- Ship
4480             THEN
4481               IF NOT(l_unit_txn_found)  THEN
4482                 l_knock_err_txn := TRUE;
4483               ELSE
4484                 l_knock_err_txn := FALSE;
4485               END IF;
4486             ELSE
4487               IF l_unit_txn_found  THEN
4488                 IF (l_err_txn_action_id = 3  AND l_err_txn_source_type_id = 8) -- ISO Direct Ship
4489                     OR
4490                    (l_err_txn_action_id = 12 AND l_err_txn_source_type_id = 7) -- ISO Intr Receipt
4491                     OR
4492                    (l_err_txn_action_id = 21 AND l_err_txn_source_type_id = 8) -- ISO Intr Ship
4493                     OR
4494                    (l_err_txn_action_id = 1 AND l_err_txn_source_type_id = 8)  -- Internal Order Issue
4495                     OR
4496                    (l_err_txn_action_id = 1 AND l_err_txn_source_type_id = 16) -- Proj Contract Issue
4497                 THEN
4498                   l_knock_err_txn := FALSE;
4499                 ELSE
4500                   l_knock_err_txn := TRUE;
4501                 END IF;
4502               ELSE
4503                 l_knock_err_txn := FALSE;
4504               END IF;
4505             END IF;
4506           END IF;
4507 
4508           IF l_knock_err_txn THEN
4509             UPDATE csi_txn_errors
4510             SET    processed_flag = 'D',
4511                    error_text     = 'Serial control is now inappropriate for this txn. Knocking this',
4512                    last_update_date  = sysdate,
4513                    last_update_login = fnd_global.login_id,
4514                    last_updated_by   = fnd_global.user_id
4515             WHERE  inv_material_transaction_id = l_mtl_txn_id_tab(ind)
4516             AND    processed_flag in ('E', 'R');
4517 
4518             DELETE FROM csi_diagnostics_temp
4519             WHERE  mtl_txn_id = l_mtl_txn_id_tab(ind)
4520             AND    error_flag = 'E';
4521 
4522             -- for wip errors that are knocked we write a csi transaction '
4523             -- to make the completion goes thru
4524             IF l_err_txn_source_type_id = 5 OR l_unit_txn_found THEN
4525 
4526               l_txn_rec.transaction_id                := fnd_api.g_miss_num;
4527               l_txn_rec.transaction_type_id           := correction_txn_type_id;
4528               l_txn_rec.source_header_ref             := 'DATAFIX';
4529               l_txn_rec.source_line_ref               := 'SRLCONTROL TXN MISMATCH';
4530               l_txn_rec.source_transaction_date       := l_err_txn_date;
4531               l_txn_rec.transaction_date              := sysdate;
4532               l_txn_rec.inv_material_transaction_id   := l_mtl_txn_id_tab(ind);
4533 
4534               csi_transactions_pvt.create_transaction (
4535                 p_api_version             => 1.0,
4536                 p_commit                  => fnd_api.g_false,
4537                 p_init_msg_list           => fnd_api.g_true,
4538                 p_validation_level        => fnd_api.g_valid_level_full,
4539                 p_success_if_exists_flag  => 'Y',
4540                 p_transaction_rec         => l_txn_rec,
4541                 x_return_status           => l_return_status,
4542                 x_msg_count               => l_msg_count,
4543                 x_msg_data                => l_msg_data  );
4544 
4545             END IF;
4546           END IF;
4547 
4548         EXCEPTION
4549           WHEN skip_error THEN
4550             log('  bad csi_txn_errors.mtl_txn_id : '||l_mtl_txn_id_tab(ind));
4551         END;
4552       END LOOP; -- error txn loop
4553 
4554       IF p_single_error_flag = 'Y' THEN
4555         EXIT when single_error_cur%NOTFOUND;
4556       ELSE
4557         EXIT when error_cur%NOTFOUND;
4558       END IF;
4559 
4560     END LOOP;
4561 
4562     IF p_single_error_flag = 'Y' THEN
4563       IF single_error_cur%ISOPEN THEN
4564         CLOSE single_error_cur;
4565       END IF;
4566     ELSE
4567       IF error_cur%ISOPEN THEN
4568         CLOSE error_cur;
4569       END IF;
4570     END IF;
4571 
4572     log(date_time_stamp||'  end get_srldata');
4573 
4574   END get_srldata;
4575 
4576   PROCEDURE preprocess_shipment(
4577     p_diag_txn_rec      IN diag_txn_rec)
4578   IS
4579     l_expire_flag       varchar2(1) := 'N';
4580     l_change_owner_flag varchar2(1) := 'Y';
4581 
4582     l_owner_party_id    number;
4583     l_owner_account_id  number;
4584 
4585   BEGIN
4586 
4587     IF  p_diag_txn_rec.serial_code = 6 THEN
4588       l_expire_flag := 'N';
4589       IF p_diag_txn_rec.create_flag = 'N' THEN
4590 
4591         get_rma_owner(
4592           p_serial_number     => p_diag_txn_rec.serial_number,
4593           p_inventory_item_id => p_diag_txn_rec.item_id,
4594           p_organization_id   => p_diag_txn_rec.organization_id,
4595           x_change_owner_flag => l_change_owner_flag,
4596           x_owner_party_id    => l_owner_party_id,
4597           x_owner_account_id  => l_owner_account_id);
4598 
4599         IF l_change_owner_flag = 'Y' THEN
4600           l_expire_flag := 'Y';
4601         ELSE
4602           l_expire_flag := 'N';
4603         END IF;
4604 
4605         UPDATE csi_diagnostics_temp
4606         SET    process_flag  = 'M',
4607                process_code  = 'SOISHIP',
4608                expire_flag   = l_expire_flag,
4609                temporary_message  = 'Updating the srlsoi instance with returned RMA info.'
4610         WHERE  diag_seq_id   = p_diag_txn_rec.diag_seq_id;
4611 
4612       ELSE
4613 
4614         UPDATE csi_diagnostics_temp
4615         SET    process_flag  = 'M',
4616                process_code  = 'SOISHIP',
4617                temporary_message  = 'Creating/Updating a non serial instance with staging info'
4618         WHERE  diag_seq_id   = p_diag_txn_rec.diag_seq_id;
4619 
4620       END IF;
4621 
4622     ELSE
4623 
4624       UPDATE csi_diagnostics_temp
4625       SET    process_flag  = 'M',
4626              process_code  = 'SHIP',
4627              temporary_message  = 'Creating/Updating the instance with staging info'
4628       WHERE  diag_seq_id   = p_diag_txn_rec.diag_seq_id;
4629 
4630     END IF;
4631 
4632   END preprocess_shipment;
4633 
4634   PROCEDURE preprocess_rma(
4635     p_diag_txn_rec      IN diag_txn_rec)
4636   IS
4637   BEGIN
4638 
4639     UPDATE csi_diagnostics_temp
4640     SET    process_flag  = 'M',
4641            process_code  = 'RMA',
4642            temporary_message  = 'Just mark error transaction to be re-processed'
4643     WHERE  diag_seq_id   = p_diag_txn_rec.diag_seq_id;
4644 
4645   END preprocess_rma;
4646 
4647   PROCEDURE preprocess_wipissue(
4648     p_diag_txn_rec      IN diag_txn_rec)
4649   IS
4650   BEGIN
4651     UPDATE csi_diagnostics_temp
4652     SET    process_flag  = 'M',
4653            process_code  = 'WIPISSUE',
4654            temporary_message  = 'Stamp the inv location from serial and mark it FOR processing'
4655     WHERE  diag_seq_id   = p_diag_txn_rec.diag_seq_id;
4656   END preprocess_wipissue;
4657 
4658   PROCEDURE preprocess_wipreturn(
4659     p_diag_txn_rec      IN diag_txn_rec)
4660   IS
4661   BEGIN
4662 
4663     UPDATE csi_diagnostics_temp
4664     SET    process_flag  = 'M',
4665            process_code  = 'WIPRETURN',
4666            temporary_message  = 'Make it a WIP instance and allow re-processing'
4667     WHERE  diag_seq_id   = p_diag_txn_rec.diag_seq_id;
4668 
4669   END preprocess_wipreturn;
4670 
4671   PROCEDURE preprocess_wipcompletion(
4672     p_diag_txn_rec      IN diag_txn_rec)
4673   IS
4674   BEGIN
4675 
4676     UPDATE csi_diagnostics_temp
4677     SET    process_flag  = 'M',
4678            process_code  = 'WIPCOMPL',
4679            temporary_message  = 'Mark the error FOR re-processing.'
4680     WHERE  diag_seq_id   = p_diag_txn_rec.diag_seq_id;
4681 
4682   END preprocess_wipcompletion;
4683 
4684   PROCEDURE preprocess_miscreceipt(
4685     p_diag_txn_rec      IN diag_txn_rec)
4686   IS
4687   BEGIN
4688 
4689     UPDATE csi_diagnostics_temp
4690     SET    process_flag  = 'M',
4691            process_code  = 'MISCRCPT',
4692            temporary_message  = 'Stamp the INV location from serial'
4693     WHERE  diag_seq_id   = p_diag_txn_rec.diag_seq_id;
4694 
4695   END preprocess_miscreceipt;
4696 
4697   PROCEDURE preprocess_miscissue(
4698     p_diag_txn_rec      IN diag_txn_rec)
4699   IS
4700   BEGIN
4701 
4702     UPDATE csi_diagnostics_temp
4703     SET    process_flag  = 'M',
4704            process_code  = 'MISCISSUE',
4705            temporary_message  = 'Stamp the INV location from serial'
4706     WHERE  diag_seq_id   = p_diag_txn_rec.diag_seq_id;
4707 
4708   END preprocess_miscissue;
4709 
4710   PROCEDURE preprocess_sixfer(
4711     p_diag_txn_rec      IN diag_txn_rec)
4712   IS
4713   BEGIN
4714 
4715     UPDATE csi_diagnostics_temp
4716     SET    process_flag  = 'M',
4717            process_code  = 'SIXFER',
4718            temporary_message  = 'Stamp the inv location from serial and mark it FOR processing'
4719     WHERE  diag_seq_id   = p_diag_txn_rec.diag_seq_id;
4720 
4721   END preprocess_sixfer;
4722 
4723 
4724   PROCEDURE preprocess_projreceipt(
4725     p_diag_txn_rec      IN diag_txn_rec)
4726   IS
4727   BEGIN
4728     UPDATE csi_diagnostics_temp
4729     SET    process_flag  = 'M',
4730            process_code  = 'PROJRCPT',
4731            temporary_message  = 'Stamp the project location from serial and mark it for processing'
4732     WHERE  diag_seq_id   = p_diag_txn_rec.diag_seq_id;
4733   END preprocess_projreceipt;
4734 
4735   PROCEDURE preprocess_interorgreceipt(
4736     p_diag_txn_rec      IN diag_txn_rec)
4737   IS
4738   BEGIN
4739     UPDATE csi_diagnostics_temp
4740     SET    process_flag  = 'M',
4741            process_code  = 'IORGRCPT',
4742            temporary_message  = 'Stamp the intransit location from serial and mark it for processing'
4743     WHERE  diag_seq_id   = p_diag_txn_rec.diag_seq_id;
4744   END preprocess_interorgreceipt;
4745 
4746   PROCEDURE preprocess_isoreceipt(
4747     p_diag_txn_rec      IN diag_txn_rec)
4748   IS
4749   BEGIN
4750     UPDATE csi_diagnostics_temp
4751     SET    process_flag  = 'M',
4752            process_code  = 'ISORCPT',
4753            temporary_message  = 'Stamp the intransit location from serial and mark it for processing'
4754     WHERE  diag_seq_id   = p_diag_txn_rec.diag_seq_id;
4755   END preprocess_isoreceipt;
4756 
4757   PROCEDURE preprocess_intransitship(
4758     p_diag_txn_rec      IN diag_txn_rec)
4759   IS
4760   BEGIN
4761     UPDATE csi_diagnostics_temp
4762     SET    process_flag  = 'M',
4763            process_code  = 'INTRSHIP',
4764            temporary_message  = 'Stamp the inv location from serial and mark it for processing'
4765     WHERE  diag_seq_id   = p_diag_txn_rec.diag_seq_id;
4766   END preprocess_intransitship;
4767 
4768   PROCEDURE preprocess_unidentified(
4769     p_diag_txn_rec      IN diag_txn_rec)
4770   IS
4771   BEGIN
4772     UPDATE csi_diagnostics_temp
4773     SET    process_flag   = 'R',
4774            temporary_message   = 'Marking this for re-process.'
4775     WHERE  diag_seq_id   = p_diag_txn_rec.diag_seq_id;
4776   END preprocess_unidentified;
4777 
4778   PROCEDURE preprocess_all(
4779     p_diag_txn_tbl        IN diag_txn_tbl)
4780   IS
4781 
4782     l_diag_txn_rec        diag_txn_rec;
4783     l_error_found         boolean := FALSE;
4784     l_error_ind           binary_integer := 0;
4785     l_diag_txn_r_tbl      diag_txn_tbl;
4786     l_preprocessed        boolean := false;
4787 
4788   BEGIN
4789 
4790     IF p_diag_txn_tbl.COUNT > 0 THEN
4791 
4792       l_preprocessed  := FALSE;
4793       l_error_found   := FALSE;
4794 
4795       FOR l_rev_ind IN reverse p_diag_txn_tbl.FIRST .. p_diag_txn_tbl.LAST
4796       LOOP
4797 
4798         l_diag_txn_rec := p_diag_txn_tbl(l_rev_ind);
4799 
4800         IF nvl(p_diag_txn_tbl(l_rev_ind).error_flag,'P') = 'E' THEN
4801           l_error_found := TRUE;
4802           l_error_ind   := l_rev_ind;
4803           exit;
4804         END IF;
4805 
4806       END LOOP;
4807 
4808       IF l_error_found THEN
4809 
4810         log('    '||l_diag_txn_rec.source_type||'  '||l_diag_txn_rec.mtl_txn_id||
4811             '  '||l_diag_txn_rec.process_code);
4812 
4813         IF l_diag_txn_rec.source_type = 'CSISOSHP' THEN -- shipping
4814           preprocess_shipment(
4815             p_diag_txn_rec => l_diag_txn_rec);
4816           l_preprocessed := TRUE;
4817         END IF;
4818 
4819         IF l_diag_txn_rec.source_type = 'CSIOKSHP' THEN -- project contract shipment
4820           preprocess_shipment(
4821             p_diag_txn_rec => l_diag_txn_rec);
4822           l_preprocessed := TRUE;
4823         END IF;
4824 
4825         IF l_diag_txn_rec.source_type = 'CSIRMARC' THEN -- rma
4826           preprocess_rma(
4827             p_diag_txn_rec => l_diag_txn_rec);
4828           l_preprocessed := TRUE;
4829         END IF;
4830 
4831         IF l_diag_txn_rec.source_type IN (
4832                  'CSIWIPCI', -- wip component issue
4833                  'CSIWIPNR', -- wip negative component return
4834                  'CSIWIPAR') -- wip assembly return
4835         THEN
4836           preprocess_wipissue(
4837             p_diag_txn_rec => l_diag_txn_rec);
4838           l_preprocessed := TRUE;
4839         END IF;
4840 
4841         IF l_diag_txn_rec.source_type IN (
4842                  'CSIWIPCR', -- wip component return
4843                  'CSIWIPNI') -- wip negative component issue
4844         THEN  -- wip returns
4845           preprocess_wipreturn(
4846             p_diag_txn_rec => l_diag_txn_rec);
4847           l_preprocessed := TRUE;
4848         END IF;
4849 
4850         IF l_diag_txn_rec.source_type = 'CSIWIPAC' THEN -- wip completion
4851           preprocess_wipcompletion(
4852             p_diag_txn_rec => l_diag_txn_rec);
4853           l_preprocessed := TRUE;
4854         END IF;
4855 
4856         IF (l_diag_txn_rec.source_type = 'CSIMSRCV') -- misc receipt
4857             OR
4858            (l_diag_txn_rec.source_type = 'CSICYCNT' AND l_diag_txn_rec.mtl_txn_qty > 0)
4859             OR
4860            (l_diag_txn_rec.source_type = 'CSIPHYIN' AND l_diag_txn_rec.mtl_txn_qty > 0)
4861             OR
4862            (l_diag_txn_rec.source_type = 'CSIPOINV')
4863         THEN
4864           preprocess_miscreceipt(
4865             p_diag_txn_rec => l_diag_txn_rec);
4866           l_preprocessed := TRUE;
4867         END IF;
4868 
4869         IF (l_diag_txn_rec.source_type IN (
4870                  'CSIMSISU', -- misc issue
4871                  'CSIISUPT', -- move order issue to project
4872                  'CSIMSIPT') -- misc issue to project
4873             OR
4874            (l_diag_txn_rec.source_type = 'CSICYCNT' AND l_diag_txn_rec.mtl_txn_qty < 0)
4875             OR
4876            (l_diag_txn_rec.source_type = 'CSIPHYIN' AND l_diag_txn_rec.mtl_txn_qty < 0))
4877         THEN
4878           preprocess_miscissue(
4879             p_diag_txn_rec => l_diag_txn_rec);
4880           l_preprocessed := TRUE;
4881         END IF;
4882 
4883         IF l_diag_txn_rec.source_type IN (
4884              'CSISUBTR', -- subinventory transfer
4885              'CSIORGDS', -- interorg direct shipment
4886              'CSIINTDS') -- iso direct shipment
4887         THEN
4888           preprocess_sixfer(
4889             p_diag_txn_rec => l_diag_txn_rec);
4890           l_preprocessed := TRUE;
4891         END IF;
4892 
4893         -- receipt from project
4894         IF l_diag_txn_rec.source_type = 'CSIMSRPT' THEN
4895           preprocess_projreceipt(
4896             p_diag_txn_rec => l_diag_txn_rec);
4897           l_preprocessed := TRUE;
4898         END IF;
4899 
4900         -- interorg intransit receipt
4901         IF l_diag_txn_rec.source_type = 'CSIORGTR' THEN
4902           preprocess_interorgreceipt(
4903             p_diag_txn_rec => l_diag_txn_rec);
4904           l_preprocessed := TRUE;
4905         END IF;
4906 
4907         -- Internal sales order requisition receipt
4908         IF l_diag_txn_rec.source_type = 'CSIINTSR' THEN
4909           preprocess_isoreceipt(
4910             p_diag_txn_rec => l_diag_txn_rec);
4911           l_preprocessed := TRUE;
4912         END IF;
4913 
4914         IF l_diag_txn_rec.source_type IN (
4915           'CSIORGTS', -- interorg intransit shipment
4916           'CSIINTSS') -- internal sales order intransit shipment
4917         THEN
4918           preprocess_intransitship(
4919             p_diag_txn_rec => l_diag_txn_rec);
4920           l_preprocessed := TRUE;
4921         END IF;
4922 
4923         IF l_diag_txn_rec.source_type = 'NONE' THEN
4924           l_preprocessed := FALSE;
4925         END IF;
4926 
4927         IF l_preprocessed THEN
4928           FOR l_ind IN 1 .. l_error_ind-1
4929           LOOP
4930             IF nvl(p_diag_txn_tbl(l_ind).error_flag,'P') = 'E' THEN
4931               UPDATE csi_diagnostics_temp
4932               SET    process_flag = 'R',
4933                      temporary_message = 'Marking it for re-process'
4934               WHERE  diag_seq_id = p_diag_txn_tbl(l_ind).diag_seq_id;
4935             END IF;
4936           END LOOP;
4937         END IF;
4938 
4939       END IF;
4940     END IF;
4941 
4942   END preprocess_all;
4943 
4944   PROCEDURE knock_all(
4945     p_diag_txn_tbl IN diag_txn_tbl)
4946   IS
4947   BEGIN
4948     IF p_diag_txn_tbl.COUNT > 0 THEN
4949       FOR l_ind IN  p_diag_txn_tbl.FIRST .. p_diag_txn_tbl.LAST
4950       LOOP
4951 
4952         IF nvl(p_diag_txn_tbl(l_ind).error_flag,'P') = 'E' THEN
4953 
4954           UPDATE csi_diagnostics_temp
4955           SET    process_flag = 'X',
4956                  temporary_message = 'Succeeding txn processed in IB for this serial'
4957           WHERE  diag_seq_id = p_diag_txn_tbl(l_ind).diag_seq_id;
4958 
4959         END IF;
4960 
4961       END LOOP;
4962     END IF;
4963   END knock_all;
4964 
4965   PROCEDURE init_diag_temp
4966   IS
4967     CURSOR diag_cur IS
4968       SELECT rowid row_id
4969       FROM   csi_diagnostics_temp;
4970 
4971   BEGIN
4972 
4973     FOR diag_rec IN diag_cur
4974     LOOP
4975 
4976       UPDATE csi_diagnostics_temp
4977       SET    process_flag   = 'N',
4978              process_code   = null,
4979              entangled_flag = null,
4980              expire_flag    = null,
4981              temporary_message   = null
4982       WHERE  rowid = diag_rec.row_id;
4983 
4984       IF mod(diag_cur%rowcount, 100) = 0 THEN
4985         commit;
4986       END IF;
4987 
4988     END LOOP;
4989     commit;
4990 
4991   END init_diag_temp;
4992 
4993   FUNCTION check_contracts(
4994     p_instance_id IN number)
4995   RETURN BOOLEAN
4996   IS
4997     l_return_status  varchar2(1) := fnd_api.g_ret_sts_success;
4998     l_msg_count      number;
4999     l_msg_data       varchar2(2000);
5000     l_contracts_tbl  oks_entitlements_pub.output_tbl_ib;
5001     l_inp_rec        oks_entitlements_pub.input_rec_ib;
5002   BEGIN
5003 
5004     l_inp_rec.validate_flag      := 'Y'; -- show all contracts
5005     l_inp_rec.product_id         := p_instance_id;
5006     l_inp_rec.calc_resptime_flag := 'N';
5007 
5008     oks_entitlements_pub.get_contracts(
5009       p_api_version     => 1.0,
5010       p_init_msg_list   => 'T',
5011       p_inp_rec         => l_inp_rec,
5012       x_ent_contracts   => l_contracts_tbl,
5013       x_return_status   => l_return_status,
5014       x_msg_count       => l_msg_count,
5015       x_msg_data        => l_msg_data);
5016     --
5017     IF l_contracts_tbl.count > 0 THEN
5018       RETURN (TRUE);
5019     ELSE
5020       RETURN (FALSE);
5021     END IF;
5022 
5023   END check_contracts;
5024 
5025   PROCEDURE identify_progressed(
5026     x_progressed OUT nocopy boolean)
5027   IS
5028 
5029     l_progressed          boolean := FALSE;
5030     l_process_code        varchar2(20);
5031     l_p_diag_seq_id       number;
5032     l_p_mtl_txn_id        number;
5033     l_p_mtl_creation_date date;
5034     l_p_csi_txn_id        number;
5035     l_contracts_flag      varchar2(1);
5036     l_earlier_txn_marked  boolean := FALSE;
5037 
5038     l_process_flag        varchar2(1);
5039     l_current_date        date;
5040 
5041     CURSOR m_cur IS
5042       SELECT distinct mtl_creation_date, mtl_txn_id
5043       FROM   csi_diagnostics_temp
5044       WHERE  nvl(process_flag, 'N') = 'M'
5045       ORDER BY mtl_creation_date asc, mtl_txn_id asc;
5046 
5047     CURSOR m_srl_cur(p_mtl_txn_id IN number) IS
5048       SELECT diag_seq_id,
5049              serial_number,
5050              inventory_item_id,
5051              instance_id,
5052              lot_control_code,
5053              process_flag
5054       FROM   csi_diagnostics_temp
5055       WHERE  mtl_txn_id = p_mtl_txn_id
5056       AND    nvl(process_flag, 'N') <> 'M';
5057 
5058     -- already marked earlier transactions
5059     CURSOR p_et_cur(
5060       p_serial_number     IN varchar2,
5061       p_item_id           IN number,
5062       p_mtl_creation_date IN date)
5063     IS
5064       SELECT diag_seq_id
5065       FROM   csi_diagnostics_temp
5066       WHERE  serial_number         = p_serial_number
5067       AND    inventory_item_id     = p_item_id
5068       AND    mtl_creation_date     < p_mtl_creation_date
5069       AND    nvl(process_flag,'N') = 'M';
5070 
5071     -- progressed marked transactions
5072     CURSOR p_lt_cur(
5073       p_serial_number     IN varchar2,
5074       p_item_id           IN number,
5075       p_mtl_creation_date IN date)
5076     IS
5077       SELECT diag_seq_id
5078       FROM   csi_diagnostics_temp
5079       WHERE  serial_number         = p_serial_number
5080       AND    inventory_item_id     = p_item_id
5081       AND    mtl_creation_date     > p_mtl_creation_date
5082       AND    nvl(process_flag,'N') = 'M'
5083       ORDER by mtl_creation_date asc, mtl_txn_id asc;
5084 
5085     CURSOR missing_csi_cur(
5086       p_serial_number     IN varchar2,
5087       p_item_id           IN number,
5088       p_mtl_creation_date IN date)
5089     IS
5090       SELECT diag_seq_id,
5091              mtl_txn_id,
5092              csi_txn_id,
5093              mtl_txn_date
5094       FROM   csi_diagnostics_temp
5095       WHERE  serial_number     = p_serial_number
5096       AND    inventory_item_id = p_item_id
5097       AND    mtl_creation_date < p_mtl_creation_date
5098       ORDER BY mtl_creation_date desc, mtl_txn_id desc;
5099 
5100     skip_txn           exception;
5101     l_return_status    varchar2(1) := fnd_api.g_ret_sts_success;
5102     l_msg_data         varchar2(2000);
5103     l_msg_count        number;
5104     l_txn_rec          csi_datastructures_pub.transaction_rec;
5105 
5106   BEGIN
5107 
5108     FOR m_rec IN m_cur -- read all the distinct inv txn records that are marked for update
5109     LOOP
5110 
5111       BEGIN
5112 
5113         -- check if this txn is still there to be processed in the loop
5114         BEGIN
5115           SELECT process_code
5116           INTO   l_process_code
5117           FROM   csi_diagnostics_temp
5118           WHERE  mtl_txn_id            = m_rec.mtl_txn_id
5119           AND    nvl(process_flag,'N') = 'M' -- marked for update
5120           AND    rownum                = 1;
5121         EXCEPTION
5122           WHEN no_data_found THEN
5123             RAISE skip_txn;
5124         END;
5125 
5126         l_earlier_txn_marked := FALSE;
5127 
5128         -- get all the serials that are in the txn that are not marked as 'M'
5129         FOR m_srl_rec IN m_srl_cur(m_rec.mtl_txn_id)
5130         LOOP
5131 
5132           -- check if the earlier txn is marked for re-processing
5133           FOR p_et_rec in p_et_cur(
5134             p_serial_number     => m_srl_rec.serial_number,
5135             p_item_id           => m_srl_rec.inventory_item_id,
5136             p_mtl_creation_date => m_rec.mtl_creation_date)
5137           LOOP
5138             l_earlier_txn_marked := TRUE;
5139             exit;
5140           END LOOP;
5141         END LOOP;
5142 
5143         IF l_earlier_txn_marked THEN
5144           --skip it for marking this batch
5145           FOR m_srl_rec IN m_srl_cur(m_rec.mtl_txn_id)
5146           LOOP
5147             IF nvl(m_srl_rec.process_flag, 'N') = 'X' THEN
5148               UPDATE csi_diagnostics_temp
5149               SET    process_flag      = 'N',
5150                      temporary_message = 'updating for entanglement processing'
5151               WHERE  diag_seq_id       = m_srl_rec.diag_seq_id;
5152             END IF;
5153           END LOOP;
5154           RAISE skip_txn;
5155         END IF;
5156 
5157         -- get all the other serials in this transaction to be marked for processing
5158         -- the same way as the current serial
5159         FOR m_srl_rec IN m_srl_cur(m_rec.mtl_txn_id)
5160         LOOP
5161 
5162           UPDATE csi_diagnostics_temp
5163           SET    process_code = l_process_code,
5164                  process_flag = 'M',
5165                  temporary_message = 'updating for entanglement processing'
5166           WHERE  diag_seq_id  = m_srl_rec.diag_seq_id;
5167 
5168           -- for this serial check if there is any progressed transaction
5169           --
5170 
5171           IF m_srl_rec.instance_id is not null THEN
5172 
5173             -- get the max processed material transaction id
5174             SELECT nvl(min(diag_seq_id), -999999)
5175             INTO   l_p_diag_seq_id
5176             FROM   csi_diagnostics_temp
5177             WHERE  serial_number       = m_srl_rec.serial_number
5178             AND    inventory_item_id   = m_srl_rec.inventory_item_id
5179             AND    mtl_creation_date   > m_rec.mtl_creation_date
5180             AND    csi_txn_id          is not null;
5181 
5182             IF l_p_diag_seq_id <> -999999 THEN
5183 
5184               SELECT mtl_txn_id,
5185                      mtl_creation_date
5186               INTO   l_p_mtl_txn_id,
5187                      l_p_mtl_creation_date
5188               FROM   csi_diagnostics_temp
5189               WHERE  diag_seq_id = l_p_diag_seq_id;
5190 
5191               IF check_contracts(m_srl_rec.instance_id) THEN
5192                 l_contracts_flag := 'Y';
5193               ELSE
5194                 l_contracts_flag := 'N';
5195               END IF;
5196 
5197               SELECT csi_txn_id
5198               INTO   l_p_csi_txn_id
5199               FROM   csi_diagnostics_temp
5200               WHERE  serial_number     = m_srl_rec.serial_number
5201               AND    inventory_item_id = m_srl_rec.inventory_item_id
5202               AND    mtl_txn_id        = l_p_mtl_txn_id;
5203 
5204               l_progressed := TRUE;
5205 
5206               BEGIN
5207 
5208                 SELECT process_flag,
5209                        date_time_stamp
5210                 INTO   l_process_flag,
5211                        l_current_date
5212                 FROM   csi_ii_forward_sync_temp
5213                 WHERE  instance_id = m_srl_rec.instance_id;
5214 
5215                 IF l_process_flag = 'P' THEN
5216                   Insert_Full_Dump(p_instance_id => m_srl_rec.instance_id);
5217                   l_current_date := sysdate;
5218                 ELSE
5219                   l_current_date := l_current_date;
5220                 END IF;
5221 
5222                 UPDATE csi_ii_forward_sync_temp
5223                 SET    date_time_stamp       = l_current_date,
5224                        mtl_txn_id            = l_p_mtl_txn_id,
5225                        mtl_txn_creation_date = l_p_mtl_creation_date,
5226                        contracts_flag        = l_contracts_flag,
5227                        process_flag          = 'N'
5228                 WHERE  instance_id           =  m_srl_rec.instance_id;
5229 
5230               EXCEPTION
5231                 WHEN no_data_found THEN
5232                   Insert_Full_Dump(p_instance_id => m_srl_rec.instance_id);
5233                   --
5234                   INSERT INTO csi_ii_forward_sync_temp(
5235                     instance_id,
5236                     serial_number,
5237                     inventory_item_id,
5238                     lot_control_code,
5239                     date_time_stamp,
5240                     mtl_txn_id,
5241                     mtl_txn_creation_date,
5242                     csi_txn_id,
5243                     contracts_flag,
5244                     process_flag,
5245                     error_message)
5246                   VALUES(
5247                     m_srl_rec.instance_id,
5248                     m_srl_rec.serial_number,
5249                     m_srl_rec.inventory_item_id,
5250                     m_srl_rec.lot_control_code,
5251                     sysdate,
5252                     l_p_mtl_txn_id,
5253                     l_p_mtl_creation_date,
5254                     l_p_csi_txn_id,
5255                     l_contracts_flag,
5256                     'N',
5257                     null);
5258               END;
5259             END IF;
5260           END IF;
5261 
5262           -- get the progressed marked serial and unmark them
5263           FOR p_lt_rec IN p_lt_cur(
5264             p_serial_number     => m_srl_rec.serial_number,
5265             p_item_id           => m_srl_rec.inventory_item_id,
5266             p_mtl_creation_date => m_rec.mtl_creation_date)
5267           LOOP
5268 
5269             UPDATE csi_diagnostics_temp
5270             SET    process_flag = 'R',
5271                    temporary_message = 'unmarking the serial to be processed after entanglement.'
5272             WHERE  diag_seq_id  = p_lt_rec.diag_seq_id;
5273 
5274           END LOOP;
5275 
5276           -- put the code here to create csi_transaction
5277           FOR missing_csi_rec IN missing_csi_cur(
5278             p_serial_number     => m_srl_rec.serial_number,
5279             p_item_id           => m_srl_rec.inventory_item_id,
5280             p_mtl_creation_date => m_rec.mtl_creation_date)
5281           LOOP
5282 
5283             IF missing_csi_rec.csi_txn_id is not null THEN
5284               exit;
5285             END IF;
5286 
5287             BEGIN
5288               SELECT transaction_id INTO l_txn_rec.transaction_id
5289               FROM   csi_transactions
5290               WHERE  inv_material_transaction_id = missing_csi_rec.mtl_txn_id;
5291             EXCEPTION
5292               WHEN no_data_found THEN
5293 
5294                 l_txn_rec.transaction_id                := fnd_api.g_miss_num;
5295                 l_txn_rec.transaction_type_id           := correction_txn_type_id;
5296                 l_txn_rec.source_header_ref             := 'DATAFIX';
5297                 l_txn_rec.source_line_ref               := 'TXNPUMP FOR PROGRESSED';
5298                 l_txn_rec.source_transaction_date       := missing_csi_rec.mtl_txn_date;
5299                 l_txn_rec.transaction_date              := sysdate;
5300                 l_txn_rec.inv_material_transaction_id   := missing_csi_rec.mtl_txn_id;
5301 
5302                 csi_transactions_pvt.create_transaction (
5303                   p_api_version             => 1.0,
5304                   p_commit                  => fnd_api.g_false,
5305                   p_init_msg_list           => fnd_api.g_true,
5306                   p_validation_level        => fnd_api.g_valid_level_full,
5307                   p_success_if_exists_flag  => 'Y',
5308                   p_transaction_rec         => l_txn_rec,
5309                   x_return_status           => l_return_status,
5310                   x_msg_count               => l_msg_count,
5311                   x_msg_data                => l_msg_data  );
5312 
5313                 UPDATE csi_diagnostics_temp
5314                 SET    csi_txn_id  = l_txn_rec.transaction_id,
5315                        error_flag  = 'P'
5316                 WHERE  diag_seq_id = missing_csi_rec.diag_seq_id;
5317 
5318               WHEN too_many_rows THEN null;
5319             END;
5320 
5321           END LOOP;
5322 
5323         END LOOP;
5324 
5325       EXCEPTION
5326         WHEN skip_txn THEN
5327           null;
5328       END;
5329     END LOOP;
5330     x_progressed := l_progressed;
5331   END identify_progressed;
5332 
5333   PROCEDURE preprocess_srldata is
5334 
5335     TYPE NumTabType is    varray(10000) of number;
5336     TYPE VarTabType is    varray(10000) of varchar2(80);
5337 
5338     l_serial_number_tab   VarTabType;
5339     l_item_id_tab         NumTabType;
5340 
5341     MAX_BUFFER_SIZE       number := 1000;
5342 
5343     CURSOR diag_srl_cur IS
5344       SELECT serial_number,
5345              inventory_item_id
5346       FROM   csi_diagnostics_temp
5347       GROUP BY serial_number, inventory_item_id;
5348 
5349     CURSOR diag_txn_cur(p_serial_number IN varchar2, p_item_id IN number) IS
5350       SELECT diag_seq_id,
5351              serial_number,
5352              inventory_item_id,
5353              organization_id,
5354              mtl_txn_id,
5355              mtl_txn_qty,
5356              mtl_creation_date,
5357              serial_control_code,
5358              lot_control_code,
5359              revision_control_code,
5360              csi_txn_id,
5361              instance_id,
5362              create_flag,
5363              source_type,
5364              error_flag ,
5365              mtl_item_revision
5366       FROM   csi_diagnostics_temp
5367       WHERE  serial_number      = p_serial_number
5368       AND    inventory_item_id  = p_item_id
5369       ORDER by mtl_creation_date desc, mtl_txn_id desc;
5370 
5371     l_freeze_date         date;
5372 
5373     l_diag_txn_tbl        diag_txn_tbl;
5374     l_d_ind               binary_integer := 0;
5375 
5376     l_diag_txn_x_tbl      diag_txn_tbl;
5377     l_dx_ind              binary_integer := 0;
5378 
5379     l_txn_success         boolean;
5380     l_progressed          boolean := FALSE;
5381     l_seq_value           number;
5382 
5383   BEGIN
5384 
5385     init_diag_temp;
5386 
5387     log(date_time_stamp||'  begin preprocess_srldata');
5388 
5389     OPEN diag_srl_cur;
5390     LOOP
5391 
5392       FETCH diag_srl_cur BULK COLLECT
5393       INTO  l_serial_number_tab,
5394             l_item_id_tab
5395       LIMIT MAX_BUFFER_SIZE;
5396 
5397       FOR ind IN 1 .. l_serial_number_tab.COUNT
5398       LOOP
5399 
5400         l_diag_txn_tbl.delete;
5401         l_diag_txn_x_tbl.delete;
5402 
5403         l_d_ind  := 0;
5404         l_dx_ind := 0;
5405 
5406         l_txn_success := FALSE;
5407 
5408         FOR diag_txn_rec IN diag_txn_cur(
5409           p_serial_number => l_serial_number_tab(ind),
5410           p_item_id       => l_item_id_tab(ind))
5411         LOOP
5412 
5413           IF diag_txn_rec.csi_txn_id IS not null THEN
5414             l_txn_success := TRUE;
5415           END IF;
5416 
5417           IF NOT(l_txn_success) THEN
5418 
5419             l_d_ind := l_d_ind + 1;
5420 
5421             l_diag_txn_tbl(l_d_ind).diag_seq_id   := diag_txn_rec.diag_seq_id;
5422             l_diag_txn_tbl(l_d_ind).serial_number := diag_txn_rec.serial_number;
5423             l_diag_txn_tbl(l_d_ind).item_id       := diag_txn_rec.inventory_item_id;
5424             l_diag_txn_tbl(l_d_ind).organization_id := diag_txn_rec.organization_id;
5425             l_diag_txn_tbl(l_d_ind).mtl_txn_id    := diag_txn_rec.mtl_txn_id;
5426             l_diag_txn_tbl(l_d_ind).mtl_txn_qty   := diag_txn_rec.mtl_txn_qty;
5427             l_diag_txn_tbl(l_d_ind).mtl_creation_date := diag_txn_rec.mtl_creation_date;
5428             l_diag_txn_tbl(l_d_ind).serial_code   := diag_txn_rec.serial_control_code;
5429             l_diag_txn_tbl(l_d_ind).lot_code      := diag_txn_rec.lot_control_code;
5430             l_diag_txn_tbl(l_d_ind).revision_code := diag_txn_rec.revision_control_code;
5431             l_diag_txn_tbl(l_d_ind).inst_id       := diag_txn_rec.instance_id;
5432             l_diag_txn_tbl(l_d_ind).create_flag   := diag_txn_rec.create_flag;
5433             l_diag_txn_tbl(l_d_ind).source_type   := diag_txn_rec.source_type;
5434             l_diag_txn_tbl(l_d_ind).error_flag    := diag_txn_rec.error_flag;
5435             l_diag_txn_tbl(l_d_ind).marked_flag   := 'N';
5436             l_diag_txn_tbl(l_d_ind).process_flag  := 'N';
5437             l_diag_txn_tbl(l_d_ind).temp_message  := null;
5438 
5439           ELSE
5440 
5441             l_dx_ind := l_dx_ind + 1;
5442 
5443             l_diag_txn_x_tbl(l_dx_ind).diag_seq_id   := diag_txn_rec.diag_seq_id;
5444             l_diag_txn_x_tbl(l_dx_ind).serial_number := diag_txn_rec.serial_number;
5445             l_diag_txn_x_tbl(l_dx_ind).item_id       := diag_txn_rec.inventory_item_id;
5446             l_diag_txn_x_tbl(l_dx_ind).organization_id := diag_txn_rec.organization_id;
5447             l_diag_txn_x_tbl(l_dx_ind).mtl_txn_id    := diag_txn_rec.mtl_txn_id;
5448             l_diag_txn_x_tbl(l_dx_ind).mtl_txn_qty   := diag_txn_rec.mtl_txn_qty;
5449             l_diag_txn_x_tbl(l_dx_ind).mtl_creation_date := diag_txn_rec.mtl_creation_date;
5450             l_diag_txn_x_tbl(l_dx_ind).serial_code   := diag_txn_rec.serial_control_code;
5451             l_diag_txn_x_tbl(l_dx_ind).lot_code      := diag_txn_rec.lot_control_code;
5452             l_diag_txn_x_tbl(l_dx_ind).revision_code := diag_txn_rec.revision_control_code;
5453             l_diag_txn_x_tbl(l_dx_ind).inst_id       := diag_txn_rec.instance_id;
5454             l_diag_txn_x_tbl(l_dx_ind).create_flag   := diag_txn_rec.create_flag;
5455             l_diag_txn_x_tbl(l_dx_ind).source_type   := diag_txn_rec.source_type;
5456             l_diag_txn_x_tbl(l_dx_ind).error_flag    := diag_txn_rec.error_flag;
5457             l_diag_txn_x_tbl(l_dx_ind).marked_flag   := 'N';
5458             l_diag_txn_x_tbl(l_dx_ind).process_flag  := 'N';
5459             l_diag_txn_x_tbl(l_dx_ind).temp_message  := null;
5460 
5461           END IF;
5462         END LOOP;
5463 
5464         log('  '||l_serial_number_tab(ind)||
5465             ' P Count:'||l_diag_txn_tbl.COUNT||
5466             ' X Count:'||l_diag_txn_x_tbl.COUNT);
5467 
5468         IF l_diag_txn_tbl.COUNT > 0 THEN
5469           preprocess_all(
5470             p_diag_txn_tbl => l_diag_txn_tbl);
5471         END IF;
5472 
5473         IF l_diag_txn_x_tbl.COUNT > 0 THEN
5474           knock_all(
5475             p_diag_txn_tbl => l_diag_txn_x_tbl);
5476         END IF;
5477 
5478         IF mod(l_serial_number_tab.COUNT, 100) = 0 THEN
5479           commit;
5480         END IF;
5481 
5482       END LOOP;
5483 
5484       EXIT when diag_srl_cur%NOTFOUND;
5485 
5486     END LOOP;
5487 
5488     IF diag_srl_cur%ISOPEN THEN
5489       CLOSE diag_srl_cur;
5490     END IF;
5491 
5492     commit;
5493 
5494     identify_progressed(
5495       x_progressed => l_progressed);
5496 
5497     IF l_progressed THEN
5498       SELECT csi_ii_forward_sync_temp_s.nextval
5499       INTO   l_seq_value
5500       FROM   sys.dual;
5501     END IF;
5502 
5503     commit;
5504 
5505     log(date_time_stamp||'  end preprocess_srldata');
5506   END preprocess_srldata;
5507 
5508   FUNCTION fill(
5509     p_column in varchar2,
5510     p_width  in number,
5511     p_side   in varchar2 default 'R')
5512   RETURN varchar2 IS
5513     l_column varchar2(2000);
5514   BEGIN
5515     l_column := nvl(p_column, ' ');
5516     IF p_side = 'L' THEN
5517       return(lpad(l_column, p_width, ' '));
5518     ELSIF p_side = 'R' THEN
5519       return(rpad(l_column, p_width, ' '));
5520     END IF;
5521   END fill;
5522 
5523   --
5524   PROCEDURE report_header
5525   is
5526     l_header        varchar2(4000);
5527   BEGIN
5528     l_header := fill('SerialNumber', 15)||
5529                 fill('Item/Org(M/I/V)', 20)||
5530                 fill('InstID', 10)||
5531                 fill('InstLoc', 15)||
5532                 fill('Usage', 15)||
5533                 fill('ISubinv', 14)||
5534                 fill('Pty/Acct', 10)||
5535                 fill('M', 1);
5536 
5537     out(l_header);
5538 
5539     l_header := fill('------------', 15)||
5540                 fill('---------------', 20)||
5541                 fill('------', 10)||
5542                 fill('-------', 15)||
5543                 fill('-----', 15)||
5544                 fill('-------', 14)||
5545                 fill('--------', 10)||
5546                 fill('-', 1);
5547 
5548     out(l_header);
5549 
5550     l_header := fill(' ', 2)||
5551                 fill('TxnName', 22)||
5552                 fill('TxnID', 9)||
5553                 fill('TxnDate', 11)||
5554                 fill('CsiTxnID', 9)||
5555                 fill('Code', 8)||
5556                 fill('P', 2);
5557 
5558     out(l_header);
5559 
5560     l_header := fill(' ', 2)||
5561                 fill('-------', 22)||
5562                 fill('-----', 9)||
5563                 fill('-------', 13)||
5564                 fill('--------', 9)||
5565                 fill('----', 8)||
5566                 fill('-', 2);
5567 
5568     out(l_header);
5569 
5570   END report_header;
5571 
5572   PROCEDURE report_body(
5573     p_rec            in     csi_diagnostics_temp%rowtype,
5574     px_serial_number in out nocopy varchar2,
5575     px_item_id       in out nocopy number)
5576   is
5577     l_mtl_txn_date  date;
5578     l_srl_data      varchar2(4000);
5579     l_txn_data      varchar2(4000);
5580   BEGIN
5581 
5582     IF (p_rec.serial_number <> px_serial_number
5583         OR
5584         p_rec.inventory_item_id <> px_item_id)
5585     THEN
5586 
5587       out(' ');
5588 
5589       -- print header;
5590       l_srl_data := fill(p_rec.serial_number, 15)||
5591                     fill(to_char(p_rec.inventory_item_id)||'/'||
5592                          to_char(p_rec.organization_id)||'/'||
5593                          nvl(to_char(p_rec.instance_organization_id), ' ')||'/'||
5594                          nvl(to_char(p_rec.instance_vld_organization_id), ' '), 20)||
5595                     fill(p_rec.instance_id, 10)||
5596                     fill(p_rec.instance_location, 15)||
5597                     fill(p_rec.instance_usage_code, 15)||
5598                     fill(p_rec.instance_subinv_name, 14)||
5599                     fill(nvl(to_char(p_rec.instance_owner_party_id), ' ')||'/'||
5600                          nvl(to_char(p_rec.instance_owner_account_id), ' '), 10)||
5601                     fill(nvl(p_rec.instance_mig_flag,'N'), 1);
5602 
5603       out(l_srl_data);
5604 
5605     END IF;
5606 
5607     l_txn_data := fill(' ', 2)||
5608                   fill(p_rec.mtl_txn_name, 22)||
5609                   fill(p_rec.mtl_txn_id, 9)||
5610                   fill(to_char(p_rec.mtl_txn_date, 'ddmmyyhh24miss'), 13)||
5611                   fill(p_rec.csi_txn_id, 9)||
5612                   fill(p_rec.process_code, 8)||
5613                   fill(p_rec.process_flag, 2);
5614 
5615     out(l_txn_data);
5616 
5617     IF p_rec.error_flag = 'E' THEN
5618       out('    '||rtrim(fill(p_rec.error_text, 96)));
5619     END IF;
5620 
5621     IF p_rec.process_flag = 'E' THEN
5622       out('    '||rtrim(fill(p_rec.temporary_message, 96)));
5623     END IF;
5624 
5625     px_serial_number := p_rec.serial_number;
5626     px_item_id       := p_rec.inventory_item_id;
5627 
5628   END report_body;
5629 
5630   PROCEDURE spool_srldata(p_mode in varchar2 default 'ALL' ) is
5631 
5632     CURSOR dump_all_cur is
5633       SELECT *
5634       FROM csi_diagnostics_temp
5635       ORDER BY serial_number, inventory_item_id, mtl_creation_date desc, mtl_txn_id desc;
5636 
5637     CURSOR dump_fixable_cur is
5638       SELECT * FROM csi_diagnostics_temp A
5639       WHERE exists (
5640         SELECT 'X' FROM csi_diagnostics_temp B
5641         WHERE  B.serial_number = A.serial_number
5642         AND    B.inventory_item_id = A.inventory_item_id
5643         AND    B.process_flag in ('M', 'X', 'R'))
5644       ORDER BY A.serial_number, a.inventory_item_id, a.mtl_creation_date desc, a.mtl_txn_id desc;
5645 
5646     CURSOR dump_nonfixable_cur is
5647       SELECT * FROM csi_diagnostics_temp A
5648       WHERE not exists (
5649         SELECT 'X' FROM csi_diagnostics_temp B
5650         WHERE  B.serial_number = A.serial_number
5651         AND    B.inventory_item_id = A.inventory_item_id
5652         AND    B.process_flag in ('M', 'X', 'R'))
5653       ORDER BY A.serial_number, a.inventory_item_id, a.mtl_creation_date desc, a.mtl_txn_id desc;
5654 
5655     CURSOR dump_errors_cur is
5656       SELECT * FROM csi_diagnostics_temp A
5657       WHERE exists (
5658         SELECT 'X' FROM csi_diagnostics_temp B
5659         WHERE  B.serial_number = A.serial_number
5660         AND    B.inventory_item_id = A.inventory_item_id
5661         AND    B.process_flag = 'E')
5662       ORDER BY A.serial_number, a.inventory_item_id, a.mtl_creation_date desc, a.mtl_txn_id desc;
5663 
5664     l_old_serial    varchar2(80)  := '##**$$**##';
5665     l_old_item_id   number        := -.9999999999;
5666 
5667   BEGIN
5668 
5669     IF p_mode = 'ALL' THEN
5670 
5671       csi_t_gen_utility_pvt.build_file_name(
5672         p_file_segment1 => 'csisrl',
5673         p_file_segment2 => to_char(sysdate, 'hh24miss'));
5674 
5675       out(' *****************************************************************************');
5676       out('                Serialized error correction -  breakup report                 ');
5677       out(' *****************************************************************************');
5678 
5679       report_header;
5680 
5681       FOR dump_rec in dump_all_cur
5682       LOOP
5683         report_body (
5684           p_rec            => dump_rec,
5685           px_serial_number => l_old_serial,
5686           px_item_id       => l_old_item_id);
5687       END LOOP;
5688 
5689     ELSIF p_mode = 'FIXABLE' THEN
5690 
5691       csi_t_gen_utility_pvt.build_file_name(
5692         p_file_segment1 => 'csisrlfix',
5693         p_file_segment2 => to_char(sysdate, 'hh24miss'));
5694 
5695       report_header;
5696 
5697       FOR dump_rec in dump_fixable_cur
5698       LOOP
5699         report_body (
5700           p_rec            => dump_rec,
5701           px_serial_number => l_old_serial,
5702           px_item_id       => l_old_item_id);
5703       END LOOP;
5704 
5705     ELSIF p_mode = 'NONFIXABLE' THEN
5706 
5707       csi_t_gen_utility_pvt.build_file_name(
5708         p_file_segment1 => 'csisrlnofix',
5709         p_file_segment2 => to_char(sysdate, 'hh24miss'));
5710 
5711       report_header;
5712 
5713       FOR dump_rec in dump_nonfixable_cur
5714       LOOP
5715         report_body (
5716           p_rec            => dump_rec,
5717           px_serial_number => l_old_serial,
5718           px_item_id       => l_old_item_id);
5719       END LOOP;
5720 
5721     ELSIF p_mode = 'ERRORS' THEN
5722 
5723       csi_t_gen_utility_pvt.build_file_name(
5724         p_file_segment1 => 'csisrlerr',
5725         p_file_segment2 => to_char(sysdate, 'hh24miss'));
5726 
5727       out(' *****************************************************************************');
5728       out('               Errors reported in serialized error correction                 ');
5729       out(' *****************************************************************************');
5730 
5731       report_header;
5732 
5733       FOR dump_rec in dump_errors_cur
5734       LOOP
5735         report_body (
5736           p_rec            => dump_rec,
5737           px_serial_number => l_old_serial,
5738           px_item_id       => l_old_item_id);
5739       END LOOP;
5740 
5741 
5742     END IF;
5743 
5744   END spool_srldata;
5745 
5746   PROCEDURE dump_diff(
5747     p_instance_rec      in csi_datastructures_pub.instance_rec)
5748   IS
5749 
5750     l_vld_organization_id    number;
5751     l_inv_organization_id    number;
5752     l_inv_subinventory_name  varchar2(30);
5753     l_inventory_revision     varchar2(8);
5754     l_inv_locator_id         number;
5755     l_location_type_code     varchar(30);
5756     l_instance_usage_code    varchar(30);
5757     l_location_id            number;
5758 
5759   BEGIN
5760     SELECT last_vld_organization_id,
5761            inv_organization_id,
5762            inv_subinventory_name,
5763            inventory_revision,
5764            inv_locator_id,
5765            location_type_code,
5766            instance_usage_code,
5767            location_id
5768     INTO   l_vld_organization_id,
5769            l_inv_organization_id,
5770            l_inv_subinventory_name,
5771            l_inventory_revision,
5772            l_inv_locator_id,
5773            l_location_type_code,
5774            l_instance_usage_code,
5775            l_location_id
5776     FROM csi_item_instances
5777     WHERE instance_id = p_instance_rec.instance_id;
5778 
5779     out('  Instance: '||
5780         fill(l_location_type_code, 15)||
5781         fill(to_char(l_location_id), 6)||
5782         fill(l_instance_usage_code, 15)||
5783         fill(to_char(l_vld_organization_id),5)||
5784         fill(to_char(l_inv_organization_id),5)||
5785         fill(l_inv_subinventory_name,15)||
5786         fill(l_inventory_revision,5)||
5787         fill(to_char(l_inv_locator_id), 6));
5788 
5789     out('  Serial  : '||
5790         fill(p_instance_rec.location_type_code, 15)||
5791         fill(to_char(p_instance_rec.location_id), 6)||
5792         fill(p_instance_rec.instance_usage_code, 15)||
5793         fill(to_char(p_instance_rec.vld_organization_id),5)||
5794         fill(to_char(p_instance_rec.inv_organization_id),5)||
5795         fill(p_instance_rec.inv_subinventory_name,15)||
5796         fill(p_instance_rec.inventory_revision,5)||
5797         fill(to_char(p_instance_rec.inv_locator_id), 6));
5798 
5799   END dump_diff;
5800 
5801   FUNCTION not_the_same(
5802     p_instance_rec      in csi_datastructures_pub.instance_rec)
5803   RETURN boolean
5804   IS
5805 
5806     l_not_the_same    boolean := TRUE;
5807 
5808     l_vld_organization_id    number;
5809     l_inv_organization_id    number;
5810     l_inv_subinventory_name  varchar2(30);
5811     l_inventory_revision     varchar2(8);
5812     l_inv_locator_id         number;
5813     l_location_type_code     varchar(30);
5814     l_instance_usage_code    varchar(30);
5815     l_lot_number             varchar2(80);
5816     l_location_id            number;
5817 
5818     l_pa_project_id          number;
5819     l_pa_project_task_id     number;
5820     l_accounting_class_code  varchar2(30);
5821 
5822     l_wip_job_id             number;
5823 
5824     l_in_transit_order_line_id number;
5825 
5826   BEGIN
5827 
5828     SELECT last_vld_organization_id,
5829            inv_organization_id,
5830            inv_subinventory_name,
5831            inventory_revision,
5832            inv_locator_id,
5833            location_type_code,
5834            instance_usage_code,
5835            location_id,
5836            lot_number,
5837            pa_project_id,
5838            pa_project_task_id,
5839            accounting_class_code,
5840            wip_job_id,
5841            in_transit_order_line_id
5842     INTO   l_vld_organization_id,
5843            l_inv_organization_id,
5844            l_inv_subinventory_name,
5845            l_inventory_revision,
5846            l_inv_locator_id,
5847            l_location_type_code,
5848            l_instance_usage_code,
5849            l_location_id,
5850            l_lot_number,
5851            l_pa_project_id,
5852            l_pa_project_task_id,
5853            l_accounting_class_code,
5854            l_wip_job_id,
5855            l_in_transit_order_line_id
5856     FROM csi_item_instances
5857     WHERE instance_id = p_instance_rec.instance_id;
5858 
5859     IF p_instance_rec.location_type_code = 'INVENTORY' THEN
5860 
5861       IF (nvl(p_instance_rec.vld_organization_id, fnd_api.g_miss_num) =
5862           nvl(l_vld_organization_id, fnd_api.g_miss_num))
5863           AND
5864          (nvl(p_instance_rec.inv_organization_id,fnd_api.g_miss_num) =
5865           nvl(l_inv_organization_id, fnd_api.g_miss_num))
5866           AND
5867          (nvl(p_instance_rec.inv_subinventory_name, fnd_api.g_miss_char) =
5868           nvl(l_inv_subinventory_name, fnd_api.g_miss_char))
5869           AND
5870          (nvl(p_instance_rec.inventory_revision, fnd_api.g_miss_char) =
5871           nvl(l_inventory_revision, fnd_api.g_miss_char))
5872           AND
5873          (nvl(p_instance_rec.inv_locator_id, fnd_api.g_miss_num) =
5874           nvl(l_inv_locator_id, fnd_api.g_miss_num))
5875           AND
5876          (nvl(p_instance_rec.location_type_code, fnd_api.g_miss_char) =
5877           nvl(l_location_type_code, fnd_api.g_miss_char))
5878           AND
5879          (nvl(p_instance_rec.location_id, fnd_api.g_miss_num) =
5880           nvl(l_location_id, fnd_api.g_miss_num))
5881           AND
5882          (nvl(p_instance_rec.instance_usage_code, fnd_api.g_miss_char) =
5883           nvl(l_instance_usage_code, fnd_api.g_miss_char))
5884           AND
5885          (nvl(p_instance_rec.lot_number, fnd_api.g_miss_char) =
5886           nvl(l_lot_number, fnd_api.g_miss_char))
5887       THEN
5888         l_not_the_same := FALSE;
5889       END IF;
5890 
5891     ELSIF p_instance_rec.location_type_code = 'PROJECT' THEN
5892 
5893       IF (nvl(p_instance_rec.accounting_class_code, fnd_api.g_miss_char) =
5894           nvl(l_accounting_class_code, fnd_api.g_miss_char))
5895           AND
5896          (nvl(p_instance_rec.location_type_code, fnd_api.g_miss_char) =
5897           nvl(l_location_type_code, fnd_api.g_miss_char))
5898           AND
5899          (nvl(p_instance_rec.location_id, fnd_api.g_miss_num) =
5900           nvl(l_location_id, fnd_api.g_miss_num))
5901           AND
5902          (nvl(p_instance_rec.pa_project_id, fnd_api.g_miss_num) =
5903           nvl(l_pa_project_id, fnd_api.g_miss_num))
5904           AND
5905          (nvl(p_instance_rec.pa_project_task_id, fnd_api.g_miss_num) =
5906           nvl(l_pa_project_task_id, fnd_api.g_miss_num))
5907           AND
5908          (nvl(p_instance_rec.instance_usage_code, fnd_api.g_miss_char) =
5909           nvl(l_instance_usage_code, fnd_api.g_miss_char))
5910       THEN
5911         l_not_the_same := FALSE;
5912       END IF;
5913 
5914     ELSIF p_instance_rec.location_type_code = 'WIP' THEN
5915 
5916       IF (nvl(p_instance_rec.location_type_code, fnd_api.g_miss_char) =
5917           nvl(l_location_type_code, fnd_api.g_miss_char))
5918           AND
5919          (nvl(p_instance_rec.location_id, fnd_api.g_miss_num) =
5920           nvl(l_location_id, fnd_api.g_miss_num))
5921           AND
5922          (nvl(p_instance_rec.instance_usage_code, fnd_api.g_miss_char) =
5923           nvl(l_instance_usage_code, fnd_api.g_miss_char))
5924           AND
5925          (nvl(p_instance_rec.lot_number, fnd_api.g_miss_char) =
5926           nvl(l_lot_number, fnd_api.g_miss_char))
5927           AND
5928          (nvl(p_instance_rec.inv_organization_id, fnd_api.g_miss_num) =
5929           nvl(l_inv_organization_id, fnd_api.g_miss_num))
5930           AND
5931          (nvl(p_instance_rec.inv_subinventory_name, fnd_api.g_miss_char) =
5932           nvl(l_inv_subinventory_name, fnd_api.g_miss_char))
5933           AND
5934          (nvl(p_instance_rec.inventory_revision, fnd_api.g_miss_char) =
5935           nvl(l_inventory_revision, fnd_api.g_miss_char))
5936           AND
5937          (nvl(p_instance_rec.inv_locator_id, fnd_api.g_miss_num) =
5938           nvl(l_inv_locator_id, fnd_api.g_miss_num))
5939           AND
5940          (nvl(p_instance_rec.wip_job_id, fnd_api.g_miss_num) =
5941           nvl(l_wip_job_id, fnd_api.g_miss_num))
5942       THEN
5943         l_not_the_same := FALSE;
5944       END IF;
5945 
5946     ELSIF p_instance_rec.location_type_code = 'IN_TRANSIT' THEN
5947 
5948       IF (nvl(p_instance_rec.location_type_code, fnd_api.g_miss_char) =
5949           nvl(l_location_type_code, fnd_api.g_miss_char))
5950           AND
5951          (nvl(p_instance_rec.location_id, fnd_api.g_miss_num) =
5952           nvl(l_location_id, fnd_api.g_miss_num))
5953           AND
5954          (nvl(p_instance_rec.instance_usage_code, fnd_api.g_miss_char) =
5955           nvl(l_instance_usage_code, fnd_api.g_miss_char))
5956           AND
5957          (nvl(p_instance_rec.lot_number, fnd_api.g_miss_char) =
5958           nvl(l_lot_number, fnd_api.g_miss_char))
5959           AND
5960          (nvl(p_instance_rec.inv_organization_id, fnd_api.g_miss_num) =
5961           nvl(l_inv_organization_id, fnd_api.g_miss_num))
5962           AND
5963          (nvl(p_instance_rec.inv_subinventory_name, fnd_api.g_miss_char) =
5964           nvl(l_inv_subinventory_name, fnd_api.g_miss_char))
5965           AND
5966          (nvl(p_instance_rec.inventory_revision, fnd_api.g_miss_char) =
5967           nvl(l_inventory_revision, fnd_api.g_miss_char))
5968           AND
5969          (nvl(p_instance_rec.inv_locator_id, fnd_api.g_miss_num) =
5970           nvl(l_inv_locator_id, fnd_api.g_miss_num))
5971           AND
5972          (nvl(p_instance_rec.in_transit_order_line_id, fnd_api.g_miss_num) =
5973           nvl(l_in_transit_order_line_id, fnd_api.g_miss_num))
5974       THEN
5975         l_not_the_same := FALSE;
5976       END IF;
5977 
5978     END IF;
5979 
5980     return l_not_the_same;
5981 
5982   END not_the_same;
5983 
5984   PROCEDURE init_plsql_tables(
5985     px_instance_rec  in out nocopy csi_datastructures_pub.instance_rec,
5986     px_parties_tbl   in out nocopy csi_datastructures_pub.party_tbl,
5987     px_pty_accts_tbl in out nocopy csi_datastructures_pub.party_account_tbl,
5988     px_org_units_tbl in out nocopy csi_datastructures_pub.organization_units_tbl,
5989     px_ea_values_tbl in out nocopy csi_datastructures_pub.extend_attrib_values_tbl,
5990     px_pricing_tbl   in out nocopy csi_datastructures_pub.pricing_attribs_tbl,
5991     px_assets_tbl    in out nocopy csi_datastructures_pub.instance_asset_tbl)
5992   is
5993     l_instance_rec   csi_datastructures_pub.instance_rec;
5994 
5995   BEGIN
5996     px_instance_rec  := l_instance_rec;
5997     px_parties_tbl.delete;
5998     px_pty_accts_tbl.delete;
5999     px_org_units_tbl.delete;
6000     px_ea_values_tbl.delete;
6001     px_pricing_tbl.delete;
6002     px_assets_tbl.delete;
6003   END init_plsql_tables;
6004 
6005   PROCEDURE knock_the_rest IS
6006     CURSOR knock_cur is
6007       SELECT mtl_txn_id,
6008              mtl_src_type_id,
6009              mtl_txn_date
6010       FROM   csi_diagnostics_temp
6011       WHERE  process_flag = 'X';
6012 
6013     l_txn_rec         csi_datastructures_pub.transaction_rec;
6014     l_return_status   varchar2(1) := fnd_api.g_ret_sts_success;
6015     l_msg_count       number;
6016     l_msg_data        varchar2(2000);
6017 
6018   BEGIN
6019     FOR knock_rec in knock_cur
6020     LOOP
6021       UPDATE csi_txn_errors
6022       SET    processed_flag   = 'D',
6023              error_text       = 'A future transaction is processed. So not processing this.',
6024              last_update_date = sysdate,
6025              last_update_login = fnd_global.login_id,
6026              last_updated_by   = fnd_global.user_id
6027       WHERE  inv_material_transaction_id = knock_rec.mtl_txn_id
6028       AND    processed_flag in ('E', 'R');
6029 
6030       BEGIN
6031         SELECT transaction_id INTO l_txn_rec.transaction_id
6032         FROM   csi_transactions
6033         WHERE  inv_material_transaction_id = knock_rec.mtl_txn_id;
6034       EXCEPTION
6035         WHEN no_data_found THEN
6036 
6037           l_txn_rec.transaction_id              := fnd_api.g_miss_num;
6038           l_txn_rec.transaction_type_id         := correction_txn_type_id;
6039           l_txn_rec.source_header_ref           := 'DATAFIX';
6040           l_txn_rec.source_line_ref             := 'FUTURE TXN IS PROCESSED';
6041           l_txn_rec.source_transaction_date     := knock_rec.mtl_txn_date;
6042           l_txn_rec.transaction_date            := sysdate;
6043           l_txn_rec.inv_material_transaction_id := knock_rec.mtl_txn_id;
6044 
6045           csi_transactions_pvt.create_transaction (
6046             p_api_version             => 1.0,
6047             p_commit                  => fnd_api.g_false,
6048             p_init_msg_list           => fnd_api.g_true,
6049             p_validation_level        => fnd_api.g_valid_level_full,
6050             p_success_if_exists_flag  => 'Y',
6051             p_transaction_rec         => l_txn_rec,
6052             x_return_status           => l_return_status,
6053             x_msg_count               => l_msg_count,
6054             x_msg_data                => l_msg_data);
6055 
6056       WHEN too_many_rows THEN
6057         null;
6058       END;
6059 
6060     END LOOP;
6061   END knock_the_rest;
6062 
6063   PROCEDURE create_instance(
6064     p_txn_rec        IN  csi_datastructures_pub.transaction_rec,
6065     p_instance_rec   IN  csi_datastructures_pub.instance_rec,
6066     p_parties_tbl    IN  csi_datastructures_pub.party_tbl,
6067     p_pty_accts_tbl  IN  csi_datastructures_pub.party_account_tbl,
6068     x_return_status  OUT nocopy varchar2,
6069     x_error_message  OUT nocopy varchar2)
6070   IS
6071 
6072     l_instance_rec         csi_datastructures_pub.instance_rec;
6073     l_parties_tbl          csi_datastructures_pub.party_tbl;
6074     l_pty_accts_tbl        csi_datastructures_pub.party_account_tbl;
6075     l_org_units_tbl        csi_datastructures_pub.organization_units_tbl;
6076     l_ea_values_tbl        csi_datastructures_pub.extend_attrib_values_tbl;
6077     l_pricing_tbl          csi_datastructures_pub.pricing_attribs_tbl;
6078     l_assets_tbl           csi_datastructures_pub.instance_asset_tbl;
6079     l_instance_ids_list    csi_datastructures_pub.id_tbl;
6080     l_txn_rec              csi_datastructures_pub.transaction_rec;
6081 
6082     l_error_message        varchar2(2000);
6083     l_msg_data             varchar2(2000);
6084     l_msg_count            number;
6085     l_return_status        varchar2(1) := fnd_api.g_ret_sts_success;
6086 
6087   BEGIN
6088 
6089     x_return_status := fnd_api.g_ret_sts_success;
6090 
6091     l_txn_rec       := p_txn_rec;
6092     l_instance_rec  := p_instance_rec;
6093     l_parties_tbl   := p_parties_tbl;
6094     l_pty_accts_tbl := p_pty_accts_tbl;
6095 
6096     savepoint create_instance;
6097 
6098     csi_transactions_pvt.create_transaction (
6099       p_api_version             => 1.0,
6100       p_commit                  => fnd_api.g_false,
6101       p_init_msg_list           => fnd_api.g_true,
6102       p_validation_level        => fnd_api.g_valid_level_full,
6103       p_success_if_exists_flag  => 'Y',
6104       p_transaction_rec         => l_txn_rec,
6105       x_return_status           => l_return_status,
6106       x_msg_count               => l_msg_count,
6107       x_msg_data                => l_msg_data  );
6108 
6109     IF l_return_status <> fnd_api.g_ret_sts_success THEN
6110       raise fnd_api.g_exc_error;
6111     END IF;
6112 
6113     l_instance_rec.mfg_serial_number_flag := 'Y';
6114 
6115     IF l_instance_rec.location_type_code = 'INVENTORY'
6116        AND
6117        l_instance_rec.instance_usage_code = 'IN_INVENTORY'
6118     THEN
6119       SELECT nvl(mssi.location_id, haou.location_id)
6120       INTO   l_instance_rec.location_id
6121       FROM   mtl_secondary_inventories mssi,
6122              hr_all_organization_units haou
6123       WHERE  mssi.organization_id          = l_instance_rec.inv_organization_id
6124       AND    mssi.secondary_inventory_name = l_instance_rec.inv_subinventory_name
6125       AND    haou.organization_id          = mssi.organization_id;
6126     ELSIF l_instance_rec.location_type_code = 'WIP' THEN
6127       IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
6128         csi_gen_utility_pvt.populate_install_param_rec;
6129       END IF;
6130       --
6131       l_instance_rec.location_id := csi_datastructures_pub.g_install_param_rec.wip_location_id;
6132     END IF;
6133 
6134     SELECT primary_uom_code
6135     INTO   l_instance_rec.unit_of_measure
6136     FROM   mtl_system_items
6137     WHERE  inventory_item_id = l_instance_rec.inventory_item_id
6138     AND    organization_id   = l_instance_rec.vld_organization_id;
6139 
6140     csi_item_instance_pub.create_item_instance(
6141       p_api_version           => 1.0,
6142       p_commit                => fnd_api.g_false,
6143       p_init_msg_list         => fnd_api.g_true,
6144       p_validation_level      => fnd_api.g_valid_level_full,
6145       p_instance_rec          => l_instance_rec,
6146       p_party_tbl             => l_parties_tbl,
6147       p_account_tbl           => l_pty_accts_tbl,
6148       p_org_assignments_tbl   => l_org_units_tbl,
6149       p_ext_attrib_values_tbl => l_ea_values_tbl,
6150       p_pricing_attrib_tbl    => l_pricing_tbl,
6151       p_asset_assignment_tbl  => l_assets_tbl,
6152       p_txn_rec               => l_txn_rec,
6153       x_return_status         => l_return_status,
6154       x_msg_count             => l_msg_count,
6155       x_msg_data              => l_msg_data );
6156 
6157     l_msg_data := csi_t_gen_utility_pvt.dump_error_stack;
6158     log('    create status :'||l_return_status||' '||l_msg_data);
6159 
6160     -- For Bug 4057183
6161     -- IF l_return_status <> fnd_api.g_ret_sts_success THEN
6162     IF l_return_status not in (fnd_api.g_ret_sts_success,'W') THEN
6163       raise fnd_api.g_exc_error;
6164     END IF;
6165 
6166     IF nvl(l_txn_rec.transaction_id, fnd_api.g_miss_num) <> fnd_api.g_miss_num THEN
6167       UPDATE csi_transactions
6168       SET    inv_material_transaction_id = null
6169       WHERE  transaction_id              = l_txn_rec.transaction_id;
6170     END IF;
6171 
6172     commit;
6173 
6174   EXCEPTION
6175     WHEN fnd_api.g_exc_error THEN
6176       rollback to create_instance;
6177       x_return_status := fnd_api.g_ret_sts_error;
6178       x_error_message := csi_t_gen_utility_pvt.dump_error_stack;
6179   END create_instance;
6180 
6181   PROCEDURE update_instance(
6182     p_txn_rec        IN  csi_datastructures_pub.transaction_rec,
6183     p_instance_rec   IN  csi_datastructures_pub.instance_rec,
6184     p_parties_tbl    IN  csi_datastructures_pub.party_tbl,
6185     p_pty_accts_tbl  IN  csi_datastructures_pub.party_account_tbl,
6186     x_return_status  OUT nocopy varchar2,
6187     x_error_message  OUT nocopy varchar2)
6188   IS
6189 
6190     l_not_the_same         boolean := TRUE;
6191 
6192     l_instance_rec         csi_datastructures_pub.instance_rec;
6193     l_parties_tbl          csi_datastructures_pub.party_tbl;
6194     l_pty_accts_tbl        csi_datastructures_pub.party_account_tbl;
6195     l_org_units_tbl        csi_datastructures_pub.organization_units_tbl;
6196     l_ea_values_tbl        csi_datastructures_pub.extend_attrib_values_tbl;
6197     l_pricing_tbl          csi_datastructures_pub.pricing_attribs_tbl;
6198     l_assets_tbl           csi_datastructures_pub.instance_asset_tbl;
6199     l_instance_ids_list    csi_datastructures_pub.id_tbl;
6200     l_txn_rec              csi_datastructures_pub.transaction_rec;
6201 
6202     l_error_message        varchar2(2000);
6203     l_msg_data             varchar2(2000);
6204     l_msg_count            number;
6205     l_return_status        varchar2(1) := fnd_api.g_ret_sts_success;
6206 
6207   BEGIN
6208 
6209     x_return_status := fnd_api.g_ret_sts_success;
6210 
6211     l_txn_rec       := p_txn_rec;
6212     l_instance_rec  := p_instance_rec;
6213     l_parties_tbl   := p_parties_tbl;
6214     l_pty_accts_tbl := p_pty_accts_tbl;
6215 
6216     IF l_instance_rec.location_type_code = 'INVENTORY'
6217        AND
6218        l_instance_rec.instance_usage_code = 'IN_INVENTORY'
6219     THEN
6220 
6221       SELECT nvl(mssi.location_id, haou.location_id)
6222       INTO   l_instance_rec.location_id
6223       FROM   mtl_secondary_inventories mssi,
6224              hr_all_organization_units haou
6225       WHERE  mssi.organization_id          = l_instance_rec.inv_organization_id
6226       AND    mssi.secondary_inventory_name = l_instance_rec.inv_subinventory_name
6227       AND    haou.organization_id          = mssi.organization_id;
6228 
6229     END IF;
6230 
6231     IF l_instance_rec.location_type_code = 'WIP' THEN
6232       IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
6233         csi_gen_utility_pvt.populate_install_param_rec;
6234       END IF;
6235       --
6236       l_instance_rec.location_id := csi_datastructures_pub.g_install_param_rec.wip_location_id;
6237     END IF;
6238 
6239     savepoint update_instance;
6240 
6241     l_not_the_same := not_the_same(l_instance_rec);
6242 
6243     IF l_not_the_same THEN
6244 
6245       log('    not the same');
6246 
6247       csi_transactions_pvt.create_transaction (
6248         p_api_version             => 1.0,
6249         p_commit                  => fnd_api.g_false,
6250         p_init_msg_list           => fnd_api.g_true,
6251         p_validation_level        => fnd_api.g_valid_level_full,
6252         p_success_if_exists_flag  => 'Y',
6253         p_transaction_rec         => l_txn_rec,
6254         x_return_status           => l_return_status,
6255         x_msg_count               => l_msg_count,
6256         x_msg_data                => l_msg_data  );
6257 
6258       IF l_return_status <> fnd_api.g_ret_sts_success THEN
6259         raise fnd_api.g_exc_error;
6260       END IF;
6261 
6262       UPDATE csi_item_instances
6263       SET    last_vld_organization_id = l_instance_rec.vld_organization_id,
6264              active_end_date          = null
6265       WHERE  instance_id = l_instance_rec.instance_id;
6266 
6267       csi_process_txn_pvt.check_and_break_relation(
6268         p_instance_id   => l_instance_rec.instance_id,
6269         p_csi_txn_rec   => l_txn_rec,
6270         x_return_status => l_return_status);
6271 
6272       IF l_return_status <> fnd_api.g_ret_sts_success THEN
6273         raise fnd_api.g_exc_error;
6274       END IF;
6275 
6276       IF nvl(l_instance_rec.active_end_date, fnd_api.g_miss_date) <> fnd_api.g_miss_date THEN
6277         l_parties_tbl.delete;
6278         l_pty_accts_tbl.delete;
6279       END IF;
6280 
6281       IF l_parties_tbl.count > 0 THEN
6282         FOR lp_ind in l_parties_tbl.FIRST .. l_parties_tbl.LAST
6283         LOOP
6284           SELECT instance_party_id,
6285                  object_version_number
6286           INTO   l_parties_tbl(lp_ind).instance_party_id,
6287                  l_parties_tbl(lp_ind).object_version_number
6288           FROM   csi_i_parties
6289           WHERE  instance_id = l_instance_rec.instance_id
6290           AND    relationship_type_code = l_parties_tbl(lp_ind).relationship_type_code
6291           AND    rownum = 1;
6292         END LOOP;
6293       END IF;
6294 
6295       SELECT object_version_number
6296       INTO   l_instance_rec.object_version_number
6297       FROM   csi_item_instances
6298       WHERE  instance_id = l_instance_rec.instance_id;
6299 
6300       csi_item_instance_pub.update_item_instance(
6301         p_api_version           => 1.0,
6302         p_commit                => fnd_api.g_false,
6303         p_init_msg_list         => fnd_api.g_true,
6304         p_validation_level      => fnd_api.g_valid_level_full,
6305         p_instance_rec          => l_instance_rec,
6306         p_party_tbl             => l_parties_tbl,
6307         p_account_tbl           => l_pty_accts_tbl,
6308         p_org_assignments_tbl   => l_org_units_tbl,
6309         p_ext_attrib_values_tbl => l_ea_values_tbl,
6310         p_pricing_attrib_tbl    => l_pricing_tbl,
6311         p_asset_assignment_tbl  => l_assets_tbl,
6312         p_txn_rec               => l_txn_rec,
6313         x_instance_id_lst       => l_instance_ids_list,
6314         x_return_status         => l_return_status,
6315         x_msg_count             => l_msg_count,
6316         x_msg_data              => l_msg_data);
6317 
6318       l_msg_data := csi_t_gen_utility_pvt.dump_error_stack;
6319       log('    update status :'||l_return_status||' '||l_msg_data);
6320 
6321       -- For Bug 4057183
6322       -- IF l_return_status <> fnd_api.g_ret_sts_success THEN
6323       IF l_return_status not in (fnd_api.g_ret_sts_success,'W') THEN
6324         raise fnd_api.g_exc_error;
6325       END IF;
6326 
6327       IF nvl(l_txn_rec.transaction_id, fnd_api.g_miss_num) <> fnd_api.g_miss_num THEN
6328         UPDATE csi_transactions
6329         SET    inv_material_transaction_id = null
6330         WHERE  transaction_id              = l_txn_rec.transaction_id;
6331       END IF;
6332 
6333     ELSE
6334       log('    the same');
6335       IF nvl(l_instance_rec.active_end_date, fnd_api.g_miss_date) <> fnd_api.g_miss_date THEN
6336         UPDATE csi_item_instances
6337         SET    active_end_date = l_instance_rec.active_end_date
6338         WHERE  instance_id     = l_instance_rec.instance_id;
6339       END IF;
6340 
6341       IF nvl(l_instance_rec.active_end_date, fnd_api.g_miss_date) = fnd_api.g_miss_date THEN
6342         UPDATE csi_item_instances
6343         SET    active_end_date = null
6344         WHERE  instance_id     = l_instance_rec.instance_id;
6345       END IF;
6346     END IF;
6347 
6348     commit;
6349 
6350   EXCEPTION
6351     WHEN fnd_api.g_exc_error THEN
6352       rollback to update_instance;
6353       x_return_status := fnd_api.g_ret_sts_error;
6354       x_error_message := csi_t_gen_utility_pvt.dump_error_stack;
6355   END update_instance;
6356 
6357   PROCEDURE fix_soiship(
6358     p_diag_txn_rec  IN diag_txn_rec,
6359     p_txn_rec       IN csi_datastructures_pub.transaction_rec,
6360     x_return_status OUT nocopy varchar2,
6361     x_error_message OUT nocopy varchar2)
6362   IS
6363 
6364     l_txn_rec              csi_datastructures_pub.transaction_rec;
6365 
6366     l_instance_rec         csi_datastructures_pub.instance_rec;
6367     l_parties_tbl          csi_datastructures_pub.party_tbl;
6368     l_pty_accts_tbl        csi_datastructures_pub.party_account_tbl;
6369 
6370     l_return_status        varchar2(1) := fnd_api.g_ret_sts_success;
6371     l_error_message        varchar2(2000);
6372 
6373   BEGIN
6374 
6375     x_return_status  := fnd_api.g_ret_sts_success;
6376 
6377     log('  '||p_diag_txn_rec.serial_number||
6378         '  '||p_diag_txn_rec.mtl_txn_id||
6379         '  '||p_diag_txn_rec.source_type||
6380         '  '||p_diag_txn_rec.process_code||
6381         '  '||p_diag_txn_rec.inst_id);
6382 
6383     FOR inv_rec IN inv_cur (p_diag_txn_rec.mtl_txn_id)
6384     LOOP
6385 
6386       l_txn_rec := p_txn_rec;
6387 
6388       l_instance_rec.instance_id              := nvl(p_diag_txn_rec.inst_id,fnd_api.g_miss_num);
6389       l_instance_rec.location_type_code       := 'INVENTORY';
6390       l_instance_rec.instance_usage_code      := 'RETURNED';
6391       l_instance_rec.inventory_item_id        := inv_rec.item_id;
6392       l_instance_rec.serial_number            := p_diag_txn_rec.serial_number;
6393       l_instance_rec.inv_organization_id      := inv_rec.organization_id;
6394       l_instance_rec.quantity                 := 1;
6395       l_instance_rec.inv_subinventory_name    := inv_rec.subinv_code;
6396       l_instance_rec.inv_locator_id           := inv_rec.locator_id;
6397       get_lot_number(
6398         p_lot_code        => p_diag_txn_rec.lot_code,
6399         p_mtl_txn_id      => p_diag_txn_rec.mtl_txn_id,
6400         p_serial_number   => p_diag_txn_rec.serial_number,
6401         x_lot_number      => l_instance_rec.lot_number);
6402       l_instance_rec.inventory_revision       := inv_rec.revision;
6403       l_instance_rec.vld_organization_id      := inv_rec.organization_id;
6404       l_instance_rec.object_version_number    := 1.0;
6405 
6406       SELECT nvl(mssi.location_id, haou.location_id)
6407       INTO   l_instance_rec.location_id
6408       FROM   mtl_secondary_inventories mssi,
6409              hr_all_organization_units haou
6410       WHERE  mssi.organization_id          = l_instance_rec.inv_organization_id
6411       AND    mssi.secondary_inventory_name = l_instance_rec.inv_subinventory_name
6412       AND    haou.organization_id          = mssi.organization_id;
6413 
6414       IF p_diag_txn_rec.expire_flag = 'Y' THEN
6415         l_instance_rec.active_end_date        := sysdate;
6416       END IF;
6417 
6418     END LOOP;
6419 
6420     IF p_diag_txn_rec.inst_id is not null THEN
6421       l_txn_rec.inv_material_transaction_id   := p_diag_txn_rec.mtl_txn_id;
6422       update_instance(
6423         p_txn_rec        => l_txn_rec,
6424         p_instance_rec   => l_instance_rec,
6425         p_parties_tbl    => l_parties_tbl,
6426         p_pty_accts_tbl  => l_pty_accts_tbl,
6427         x_return_status  => l_return_status,
6428         x_error_message  => l_error_message);
6429 
6430     END IF;
6431 
6432     IF l_return_status <> fnd_api.g_ret_sts_success THEN
6433       x_return_status  := l_return_status;
6434       x_error_message  := l_error_message;
6435     END IF;
6436 
6437   END fix_soiship;
6438 
6439   PROCEDURE stage_soiship_instances
6440   IS
6441 
6442     CURSOR txn_cur is
6443       SELECT distinct mtl_txn_id ,
6444              inventory_item_id,
6445              organization_id
6446       FROM   csi_diagnostics_temp
6447       WHERE  process_flag = 'M'
6448       AND    process_code = 'SOISHIP';
6449 
6450     CURSOR returned_serial_cur(p_mtl_txn_id IN number) is
6451       SELECT cdt.diag_seq_id,
6452              cdt.process_code,
6453              cdt.mtl_txn_id,
6454              cdt.mtl_txn_date mtl_txn_date,
6455              cdt.serial_number,
6456              cdt.inventory_item_id,
6457              cdt.instance_id,
6458              cdt.csi_txn_id,
6459              cdt.csi_txn_type_id,
6460              cdt.wip_job_id,
6461              cdt.internal_party_id,
6462              nvl(cdt.create_flag, 'Y')  create_flag,
6463              nvl(cdt.expire_flag, 'N')  expire_flag,
6464              cdt.serial_control_code,
6465              cdt.lot_control_code
6466       FROM   csi_diagnostics_temp cdt
6467       WHERE  cdt.mtl_txn_id            = p_mtl_txn_id
6468       AND    cdt.process_flag          = 'M'
6469       AND    nvl(cdt.create_flag, 'Y') = 'N'; -- marked FOR processing
6470 
6471     CURSOR stage_cur(p_mtl_txn_id in number, p_lot_code in number) IS
6472       SELECT mmt.inventory_item_id      item_id,
6473              mmt.organization_id        organization_id,
6474              mmt.subinventory_code      subinv_code,
6475              mmt.locator_id             locator_id,
6476              mmt.revision               revision,
6477              to_char(null)              lot_number,
6478              abs(mmt.primary_quantity)  quantity,
6479              mmt.transaction_date       mtl_txn_date,
6480              mmt.transaction_id         mtl_txn_id,
6481              mmt.trx_source_line_id     trx_source_line_id
6482       FROM   mtl_material_transactions mmt
6483       WHERE  mmt.transaction_id = p_mtl_txn_id
6484       AND    p_lot_code = 1
6485       UNION
6486       SELECT mmt.inventory_item_id      item_id,
6487              mmt.organization_id        organization_id,
6488              mmt.subinventory_code      subinv_code,
6489              mmt.locator_id             locator_id,
6490              mmt.revision               revision,
6491              mtln.lot_number            lot_number,
6492              abs(mtln.primary_quantity) quantity,
6493              mmt.transaction_date       mtl_txn_date,
6494              mmt.transaction_id         mtl_txn_id,
6495              mmt.trx_source_line_id     trx_source_line_id
6496       FROM   mtl_material_transactions   mmt,
6497              mtl_transaction_lot_numbers mtln
6498       WHERE  mmt.transaction_id  = p_mtl_txn_id
6499       AND    mtln.transaction_id = mmt.transaction_id
6500       AND    p_lot_code <> 1;
6501 
6502     l_order_line_id         number;
6503     l_mtl_txn_date          date;
6504 
6505     l_serials_fixed         boolean;
6506     l_lot_code              number;
6507     l_uom_code              varchar2(10);
6508 
6509     l_instance_id           number;
6510     l_quantity              number;
6511     l_object_version_number number;
6512 
6513     l_internal_party_id    number;
6514     l_instance_rec         csi_datastructures_pub.instance_rec;
6515     l_parties_tbl          csi_datastructures_pub.party_tbl;
6516     l_pty_accts_tbl        csi_datastructures_pub.party_account_tbl;
6517     l_org_units_tbl        csi_datastructures_pub.organization_units_tbl;
6518     l_ea_values_tbl        csi_datastructures_pub.extend_attrib_values_tbl;
6519     l_pricing_tbl          csi_datastructures_pub.pricing_attribs_tbl;
6520     l_assets_tbl           csi_datastructures_pub.instance_asset_tbl;
6521     l_instance_ids_list    csi_datastructures_pub.id_tbl;
6522     l_txn_rec              csi_datastructures_pub.transaction_rec;
6523     l_diag_txn_rec         diag_txn_rec;
6524 
6525     l_return_status        varchar2(1) := fnd_api.g_ret_sts_success;
6526     l_msg_count            number;
6527     l_msg_data             varchar2(2000);
6528 
6529     l_error_message        varchar2(2000);
6530     srl_upd_error          exception;
6531 
6532   BEGIN
6533 
6534     log(date_time_stamp||'  begin stage_soiship_instances');
6535 
6536     FOR txn_rec IN txn_cur
6537     LOOP
6538 
6539       SELECT transaction_date,
6540              trx_source_line_id
6541       INTO   l_mtl_txn_date,
6542              l_order_line_id
6543       FROM   mtl_material_transactions
6544       WHERE  transaction_id = txn_rec.mtl_txn_id;
6545 
6546       l_txn_rec.transaction_id              := fnd_api.g_miss_num;
6547       l_txn_rec.transaction_type_id         := correction_txn_type_id;
6548       l_txn_rec.source_header_ref           := 'DATAFIX';
6549       l_txn_rec.source_line_ref             := 'SOISHIP';
6550       l_txn_rec.source_line_ref_id          := l_order_line_id;
6551       l_txn_rec.source_transaction_date     := l_mtl_txn_date;
6552       l_txn_rec.transaction_date            := l_mtl_txn_date;
6553 
6554       l_serials_fixed := TRUE;
6555 
6556       -- first fix all the serial numbers as returned
6557       FOR returned_serial_rec IN returned_serial_cur(txn_rec.mtl_txn_id)
6558       LOOP
6559 
6560         l_internal_party_id := returned_serial_rec.internal_party_id;
6561 
6562         l_diag_txn_rec.diag_seq_id     := returned_serial_rec.diag_seq_id;
6563         l_diag_txn_rec.process_code    := returned_serial_rec.process_code;
6564         l_diag_txn_rec.mtl_txn_id      := returned_serial_rec.mtl_txn_id;
6565         l_diag_txn_rec.mtl_txn_date    := returned_serial_rec.mtl_txn_date;
6566         l_diag_txn_rec.serial_number   := returned_serial_rec.serial_number;
6567         l_diag_txn_rec.item_id         := returned_serial_rec.inventory_item_id;
6568         l_diag_txn_rec.inst_id         := returned_serial_rec.instance_id;
6569         l_diag_txn_rec.csi_txn_id      := returned_serial_rec.csi_txn_id;
6570         l_diag_txn_rec.csi_txn_type_id := returned_serial_rec.csi_txn_type_id;
6571         l_diag_txn_rec.wip_job_id      := returned_serial_rec.wip_job_id;
6572         l_diag_txn_rec.create_flag     := returned_serial_rec.create_flag;
6573         l_diag_txn_rec.expire_flag     := returned_serial_rec.expire_flag;
6574         l_diag_txn_rec.serial_code     := returned_serial_rec.serial_control_code;
6575         l_diag_txn_rec.lot_code        := returned_serial_rec.lot_control_code;
6576 
6577         savepoint soiship_srl_upd;
6578 
6579         fix_soiship(
6580           p_diag_txn_rec  => l_diag_txn_rec,
6581           p_txn_rec       => l_txn_rec,
6582           x_return_status => l_return_status,
6583           x_error_message => l_error_message);
6584 
6585         IF l_return_status <> fnd_api.g_ret_sts_success THEN
6586 
6587           l_serials_fixed := FALSE;
6588           rollback to soiship_srl_upd;
6589 
6590           UPDATE csi_diagnostics_temp
6591           SET    process_flag  = 'E',
6592                  temporary_message  = l_error_message
6593           WHERE  diag_seq_id   = returned_serial_rec.diag_seq_id;
6594 
6595         END IF;
6596 
6597         commit;
6598 
6599       END LOOP;
6600 
6601       IF l_serials_fixed THEN
6602 
6603         SELECT lot_control_code,
6604                primary_uom_code
6605         INTO   l_lot_code,
6606                l_uom_code
6607         FROM   mtl_system_items
6608         WHERE  inventory_item_id = txn_rec.inventory_item_id
6609         AND    organization_id   = txn_rec.organization_id;
6610 
6611         FOR stage_rec IN stage_cur (txn_rec.mtl_txn_id, l_lot_code)
6612         LOOP
6613 
6614           savepoint stage_soiship_instances;
6615 
6616           BEGIN
6617 
6618             init_plsql_tables(
6619               px_instance_rec  => l_instance_rec,
6620               px_parties_tbl   => l_parties_tbl,
6621               px_pty_accts_tbl => l_pty_accts_tbl,
6622               px_org_units_tbl => l_org_units_tbl,
6623               px_ea_values_tbl => l_ea_values_tbl,
6624               px_pricing_tbl   => l_pricing_tbl,
6625               px_assets_tbl    => l_assets_tbl);
6626 
6627             BEGIN
6628 
6629               SELECT instance_id,
6630                      quantity,
6631                      object_version_number
6632               INTO   l_instance_id,
6633                      l_quantity,
6634                      l_object_version_number
6635               FROM   csi_item_instances
6636               WHERE  location_type_code               = 'INVENTORY'
6637               AND    instance_usage_code              = 'IN_INVENTORY'
6638               AND    inventory_item_id                = stage_rec.item_id
6639               AND    inv_organization_id              = stage_rec.organization_id
6640               AND    inv_subinventory_name            = stage_rec.subinv_code
6641               AND    nvl(inv_locator_id,-9999)        = nvl(stage_rec.locator_id,-9999)
6642               AND    nvl(lot_number,'$$##$$')         = nvl(stage_rec.lot_number,'$$##$$')
6643               AND    nvl(inventory_revision,'$$##$$') = nvl(stage_rec.revision,'$$##$$')
6644               AND    serial_number is null;
6645 
6646             EXCEPTION
6647               WHEN no_data_found THEN
6648                 l_instance_id := fnd_api.g_miss_num;
6649               WHEN too_many_rows THEN
6650                 stack_message('Too many inventory instances for this non srl item.');
6651                 raise fnd_api.g_exc_error;
6652             END;
6653 
6654             IF nvl(l_instance_id,fnd_api.g_miss_num) <> fnd_api.g_miss_num THEN
6655 
6656               l_instance_rec.instance_id           := l_instance_id;
6657               l_instance_rec.quantity              := l_quantity + stage_rec.quantity;
6658               l_instance_rec.object_version_number := l_object_version_number;
6659               l_instance_rec.active_end_date       := null;
6660               l_instance_rec.instance_status_id    := 3;
6661 
6662               csi_item_instance_pub.update_item_instance(
6663                 p_api_version           => 1.0,
6664                 p_commit                => fnd_api.g_false,
6665                 p_init_msg_list         => fnd_api.g_true,
6666                 p_validation_level      => fnd_api.g_valid_level_full,
6667                 p_instance_rec          => l_instance_rec,
6668                 p_party_tbl             => l_parties_tbl,
6669                 p_account_tbl           => l_pty_accts_tbl,
6670                 p_org_assignments_tbl   => l_org_units_tbl,
6671                 p_ext_attrib_values_tbl => l_ea_values_tbl,
6672                 p_pricing_attrib_tbl    => l_pricing_tbl,
6673                 p_asset_assignment_tbl  => l_assets_tbl,
6674                 p_txn_rec               => l_txn_rec,
6675                 x_instance_id_lst       => l_instance_ids_list,
6676                 x_return_status         => l_return_status,
6677                 x_msg_count             => l_msg_count,
6678                 x_msg_data              => l_msg_data);
6679 
6680               -- For Bug 4057183
6681               -- IF l_return_status <> fnd_api.g_ret_sts_success THEN
6682               IF l_return_status not in (fnd_api.g_ret_sts_success,'W') THEN
6683                 raise fnd_api.g_exc_error;
6684               END IF;
6685 
6686             ELSE
6687 
6688               l_instance_rec.instance_id              := fnd_api.g_miss_num;
6689               l_instance_rec.location_type_code       := 'INVENTORY';
6690               l_instance_rec.instance_usage_code      := 'IN_INVENTORY';
6691               l_instance_rec.inventory_item_id        := stage_rec.item_id;
6692               l_instance_rec.inv_organization_id      := stage_rec.organization_id;
6693               l_instance_rec.quantity                 := stage_rec.quantity;
6694               l_instance_rec.unit_of_measure          := l_uom_code;
6695               l_instance_rec.inv_subinventory_name    := stage_rec.subinv_code;
6696               l_instance_rec.inv_locator_id           := stage_rec.locator_id;
6697               l_instance_rec.lot_number               := stage_rec.lot_number;
6698               l_instance_rec.inventory_revision       := stage_rec.revision;
6699               l_instance_rec.vld_organization_id      := stage_rec.organization_id;
6700               l_instance_rec.object_version_number    := 1.0;
6701 
6702               SELECT nvl(mssi.location_id, haou.location_id)
6703               INTO   l_instance_rec.location_id
6704               FROM   mtl_secondary_inventories mssi,
6705                      hr_all_organization_units haou
6706               WHERE  mssi.organization_id          = l_instance_rec.inv_organization_id
6707               AND    mssi.secondary_inventory_name = l_instance_rec.inv_subinventory_name
6708               AND    haou.organization_id          = mssi.organization_id;
6709 
6710               l_parties_tbl(1).instance_party_id      := fnd_api.g_miss_num;
6711               l_parties_tbl(1).party_source_table     := 'HZ_PARTIES';
6712               l_parties_tbl(1).party_id               := l_internal_party_id;
6713               l_parties_tbl(1).relationship_type_code := 'OWNER';
6714               l_parties_tbl(1).contact_flag           := 'N';
6715               l_parties_tbl(1).object_version_number  := 1.0;
6716 
6717               csi_item_instance_pub.create_item_instance(
6718                 p_api_version           => 1.0,
6719                 p_commit                => fnd_api.g_false,
6720                 p_init_msg_list         => fnd_api.g_true,
6721                 p_validation_level      => fnd_api.g_valid_level_full,
6722                 p_instance_rec          => l_instance_rec,
6723                 p_party_tbl             => l_parties_tbl,
6724                 p_account_tbl           => l_pty_accts_tbl,
6725                 p_org_assignments_tbl   => l_org_units_tbl,
6726                 p_ext_attrib_values_tbl => l_ea_values_tbl,
6727                 p_pricing_attrib_tbl    => l_pricing_tbl,
6728                 p_asset_assignment_tbl  => l_assets_tbl,
6729                 p_txn_rec               => l_txn_rec,
6730                 x_return_status         => l_return_status,
6731                 x_msg_count             => l_msg_count,
6732                 x_msg_data              => l_msg_data );
6733 
6734               -- For Bug 4057183
6735               -- IF l_return_status <> fnd_api.g_ret_sts_success THEN
6736               IF l_return_status not in (fnd_api.g_ret_sts_success,'W') THEN
6737                 raise fnd_api.g_exc_error;
6738               END IF;
6739 
6740             END IF;
6741 
6742             UPDATE csi_diagnostics_temp
6743             SET    process_flag = 'P',
6744                    temporary_message = 'Sales order issue instance staged.'
6745             WHERE  mtl_txn_id   = txn_rec.mtl_txn_id;
6746 
6747             UPDATE csi_txn_errors
6748             SET    processed_flag    = 'R',
6749                    last_update_date  = sysdate,
6750                    last_update_login = fnd_global.login_id,
6751                    last_updated_by   = fnd_global.user_id
6752             WHERE  inv_material_transaction_id = txn_rec.mtl_txn_id
6753             AND    processed_flag = 'E';
6754 
6755           EXCEPTION
6756             WHEN fnd_api.g_exc_error THEN
6757               rollback to stage_soiship_instances;
6758               l_error_message := csi_t_gen_utility_pvt.dump_error_stack;
6759               UPDATE csi_diagnostics_temp
6760               SET    process_flag = 'E',
6761                      temporary_message = l_error_message
6762               WHERE  mtl_txn_id   = txn_rec.mtl_txn_id;
6763           END;
6764           commit;
6765         END LOOP;
6766       END IF;
6767 
6768     END LOOP;
6769 
6770     log(date_time_stamp||'  end stage_soiship_instances');
6771 
6772   END stage_soiship_instances;
6773 
6774   PROCEDURE fix_shipment(
6775     p_diag_txn_rec  IN  diag_txn_rec,
6776     x_return_status OUT nocopy varchar2,
6777     x_error_message OUT nocopy varchar2)
6778   IS
6779 
6780     l_txn_rec              csi_datastructures_pub.transaction_rec;
6781 
6782     l_instance_rec         csi_datastructures_pub.instance_rec;
6783     l_parties_tbl          csi_datastructures_pub.party_tbl;
6784     l_pty_accts_tbl        csi_datastructures_pub.party_account_tbl;
6785 
6786     l_return_status        varchar2(1) := fnd_api.g_ret_sts_success;
6787     l_error_message        varchar2(2000);
6788 
6789   BEGIN
6790 
6791     x_return_status := fnd_api.g_ret_sts_success;
6792 
6793     FOR inv_rec IN inv_cur (p_diag_txn_rec.mtl_txn_id)
6794     LOOP
6795 
6796       l_txn_rec.transaction_id                := fnd_api.g_miss_num;
6797       l_txn_rec.transaction_type_id           := correction_txn_type_id;
6798       l_txn_rec.source_header_ref             := 'DATAFIX';
6799       l_txn_rec.source_line_ref               := p_diag_txn_rec.process_code;
6800       l_txn_rec.source_line_ref_id            := inv_rec.trx_source_line_id;
6801       l_txn_rec.source_transaction_date       := inv_rec.mtl_txn_date;
6802       l_txn_rec.transaction_date              := inv_rec.mtl_txn_date;
6803 
6804       l_instance_rec.instance_id              := nvl(p_diag_txn_rec.inst_id,fnd_api.g_miss_num);
6805       l_instance_rec.location_type_code       := 'INVENTORY';
6806       l_instance_rec.instance_usage_code      := 'IN_INVENTORY';
6807       l_instance_rec.inventory_item_id        := inv_rec.item_id;
6808       l_instance_rec.serial_number            := p_diag_txn_rec.serial_number;
6809       l_instance_rec.inv_organization_id      := inv_rec.organization_id;
6810       l_instance_rec.quantity                 := 1;
6811       l_instance_rec.inv_subinventory_name    := inv_rec.subinv_code;
6812       l_instance_rec.inv_locator_id           := inv_rec.locator_id;
6813       get_lot_number(
6814         p_lot_code        => p_diag_txn_rec.lot_code,
6815         p_mtl_txn_id      => p_diag_txn_rec.mtl_txn_id,
6816         p_serial_number   => p_diag_txn_rec.serial_number,
6817         x_lot_number      => l_instance_rec.lot_number);
6818       l_instance_rec.inventory_revision       := inv_rec.revision;
6819       l_instance_rec.vld_organization_id      := inv_rec.organization_id;
6820       l_instance_rec.object_version_number    := 1.0;
6821 
6822     END LOOP;
6823 
6824     l_txn_rec.inv_material_transaction_id   := p_diag_txn_rec.mtl_txn_id;
6825     IF p_diag_txn_rec.inst_id is not null THEN
6826       update_instance(
6827         p_txn_rec        => l_txn_rec,
6828         p_instance_rec   => l_instance_rec,
6829         p_parties_tbl    => l_parties_tbl,
6830         p_pty_accts_tbl  => l_pty_accts_tbl,
6831         x_return_status  => l_return_status,
6832         x_error_message  => l_error_message);
6833     ELSE
6834 
6835       l_parties_tbl(1).instance_party_id      := fnd_api.g_miss_num;
6836       l_parties_tbl(1).party_source_table     := 'HZ_PARTIES';
6837       l_parties_tbl(1).party_id               := p_diag_txn_rec.internal_party_id;
6838       l_parties_tbl(1).relationship_type_code := 'OWNER';
6839       l_parties_tbl(1).contact_flag           := 'N';
6840       l_parties_tbl(1).object_version_number  := 1.0;
6841 
6842       create_instance(
6843         p_txn_rec        => l_txn_rec,
6844         p_instance_rec   => l_instance_rec,
6845         p_parties_tbl    => l_parties_tbl,
6846         p_pty_accts_tbl  => l_pty_accts_tbl,
6847         x_return_status  => l_return_status,
6848         x_error_message  => l_error_message);
6849     END IF;
6850 
6851     IF l_return_status <> fnd_api.g_ret_sts_success THEN
6852       x_return_status  := l_return_status;
6853       x_error_message  := l_error_message;
6854     END IF;
6855 
6856   END fix_shipment;
6857 
6858   PROCEDURE fix_rma(
6859     p_diag_txn_rec  IN  diag_txn_rec,
6860     x_return_status OUT nocopy varchar2,
6861     x_error_message OUT nocopy varchar2)
6862   IS
6863 
6864     l_location_type_code     varchar2(30);
6865     l_intransit_location_id  number;
6866 
6867     l_txn_rec              csi_datastructures_pub.transaction_rec;
6868 
6869     l_instance_rec         csi_datastructures_pub.instance_rec;
6870     l_parties_tbl          csi_datastructures_pub.party_tbl;
6871     l_pty_accts_tbl        csi_datastructures_pub.party_account_tbl;
6872 
6873     CURSOR prev_txn_cur(
6874       p_item_id       IN number,
6875       p_serial_number IN varchar2,
6876       p_diag_seq_id   IN number)
6877     IS
6878       SELECT mtl_txn_id,
6879              mtl_action_id,
6880              mtl_src_type_id,
6881              mtl_type_id
6882       FROM   csi_diagnostics_temp
6883       WHERE  inventory_item_id = p_item_id
6884       AND    serial_number     = p_serial_number
6885       AND    diag_seq_id       > p_diag_seq_id
6886       ORDER  by diag_seq_id asc;
6887 
6888     l_prev_txn_rec         prev_txn_cur%rowtype;
6889     l_return_status        varchar2(1) := fnd_api.g_ret_sts_success;
6890     l_error_message        varchar2(2000);
6891 
6892   BEGIN
6893 
6894     x_return_status := fnd_api.g_ret_sts_success;
6895 
6896     IF p_diag_txn_rec.inst_id is not null THEN
6897        IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
6898           csi_gen_utility_pvt.populate_install_param_rec;
6899        END IF;
6900        --
6901        l_intransit_location_id := csi_datastructures_pub.g_install_param_rec.in_transit_location_id;
6902        --
6903 
6904       FOR prev_txn_rec IN prev_txn_cur(
6905         p_item_id       => p_diag_txn_rec.item_id,
6906         p_serial_number => p_diag_txn_rec.serial_number,
6907         p_diag_seq_id   => p_diag_txn_rec.diag_seq_id)
6908       LOOP
6909         l_prev_txn_rec := prev_txn_rec;
6910         exit;
6911       END LOOP;
6912 
6913       IF l_prev_txn_rec.mtl_action_id = 21 and l_prev_txn_rec.mtl_src_type_id = 8 THEN
6914         FOR inv_rec IN inv_cur (p_diag_txn_rec.mtl_txn_id)
6915         LOOP
6916 
6917           l_txn_rec.transaction_id                := fnd_api.g_miss_num;
6918           l_txn_rec.transaction_type_id           := correction_txn_type_id;
6919           l_txn_rec.source_header_ref             := 'DATAFIX';
6920           l_txn_rec.source_line_ref               := p_diag_txn_rec.process_code;
6921           l_txn_rec.source_line_ref_id            := inv_rec.trx_source_line_id;
6922           l_txn_rec.source_transaction_date       := inv_rec.mtl_txn_date;
6923           l_txn_rec.transaction_date              := inv_rec.mtl_txn_date;
6924 
6925           l_instance_rec.instance_id              := p_diag_txn_rec.inst_id;
6926           l_instance_rec.location_id              := l_intransit_location_id;
6927           l_instance_rec.location_type_code       := 'HZ_LOCATIONS';
6928           l_instance_rec.instance_usage_code      := 'OUT_OF_ENTERPRISE';
6929           l_instance_rec.inventory_item_id        := inv_rec.item_id;
6930           l_instance_rec.serial_number            := p_diag_txn_rec.serial_number;
6931           l_instance_rec.quantity                 := 1;
6932           l_instance_rec.inventory_revision       := inv_rec.revision;
6933           l_instance_rec.vld_organization_id      := inv_rec.organization_id;
6934           l_instance_rec.active_end_date          := sysdate;
6935 
6936         END LOOP;
6937 
6938         l_txn_rec.inv_material_transaction_id   := p_diag_txn_rec.mtl_txn_id;
6939         update_instance(
6940           p_txn_rec        => l_txn_rec,
6941           p_instance_rec   => l_instance_rec,
6942           p_parties_tbl    => l_parties_tbl,
6943           p_pty_accts_tbl  => l_pty_accts_tbl,
6944           x_return_status  => l_return_status,
6945           x_error_message  => l_error_message);
6946 
6947         IF l_return_status <> fnd_api.g_ret_sts_success THEN
6948           x_return_status  := l_return_status;
6949           x_error_message  := l_error_message;
6950         END IF;
6951       ELSE
6952 
6953         SELECT location_type_code
6954         INTO   l_location_type_code
6955         FROM   csi_item_instances
6956         WHERE  instance_id = p_diag_txn_rec.inst_id;
6957 
6958         IF l_location_type_code IN ('INVENTORY', 'WIP') THEN
6959 
6960           FOR inv_rec IN inv_cur (p_diag_txn_rec.mtl_txn_id)
6961           LOOP
6962 
6963             l_txn_rec.transaction_id                := fnd_api.g_miss_num;
6964             l_txn_rec.transaction_type_id           := correction_txn_type_id;
6965             l_txn_rec.source_header_ref             := 'DATAFIX';
6966             l_txn_rec.source_line_ref               := p_diag_txn_rec.process_code;
6967             l_txn_rec.source_line_ref_id            := inv_rec.trx_source_line_id;
6968             l_txn_rec.source_transaction_date       := inv_rec.mtl_txn_date;
6969             l_txn_rec.transaction_date              := inv_rec.mtl_txn_date;
6970 
6971             l_instance_rec.instance_id              := nvl(p_diag_txn_rec.inst_id,fnd_api.g_miss_num);
6972             l_instance_rec.location_type_code       := 'INVENTORY';
6973 
6974             IF p_diag_txn_rec.serial_code = 6 THEN
6975               l_instance_rec.instance_usage_code    := 'RETURNED';
6976             ELSE
6977               l_instance_rec.instance_usage_code    := 'IN_INVENTORY';
6978             END IF;
6979 
6980             l_instance_rec.inventory_item_id        := inv_rec.item_id;
6981             l_instance_rec.serial_number            := p_diag_txn_rec.serial_number;
6982             l_instance_rec.inv_organization_id      := inv_rec.organization_id;
6983             l_instance_rec.quantity                 := 1;
6984             l_instance_rec.inv_subinventory_name    := inv_rec.subinv_code;
6985             l_instance_rec.inv_locator_id           := inv_rec.locator_id;
6986             get_lot_number(
6987               p_lot_code        => p_diag_txn_rec.lot_code,
6988               p_mtl_txn_id      => p_diag_txn_rec.mtl_txn_id,
6989               p_serial_number   => p_diag_txn_rec.serial_number,
6990               x_lot_number      => l_instance_rec.lot_number);
6991             l_instance_rec.inventory_revision       := inv_rec.revision;
6992             l_instance_rec.vld_organization_id      := inv_rec.organization_id;
6993             l_instance_rec.active_end_date          := sysdate;
6994             l_instance_rec.object_version_number    := 1.0;
6995 
6996           END LOOP;
6997 
6998           l_txn_rec.inv_material_transaction_id   := p_diag_txn_rec.mtl_txn_id;
6999 
7000           update_instance(
7001             p_txn_rec        => l_txn_rec,
7002             p_instance_rec   => l_instance_rec,
7003             p_parties_tbl    => l_parties_tbl,
7004             p_pty_accts_tbl  => l_pty_accts_tbl,
7005             x_return_status  => l_return_status,
7006             x_error_message  => l_error_message);
7007 
7008           IF l_return_status <> fnd_api.g_ret_sts_success THEN
7009             x_return_status  := l_return_status;
7010             x_error_message  := l_error_message;
7011           END IF;
7012 
7013         END IF;
7014       END IF;
7015     END IF;
7016   END fix_rma;
7017 
7018   PROCEDURE fix_wipissue(
7019     p_diag_txn_rec  IN  diag_txn_rec,
7020     x_return_status OUT nocopy varchar2,
7021     x_error_message OUT nocopy varchar2)
7022   IS
7023 
7024     l_txn_rec              csi_datastructures_pub.transaction_rec;
7025 
7026     l_instance_rec         csi_datastructures_pub.instance_rec;
7027     l_parties_tbl          csi_datastructures_pub.party_tbl;
7028     l_pty_accts_tbl        csi_datastructures_pub.party_account_tbl;
7029 
7030     l_return_status        varchar2(1) := fnd_api.g_ret_sts_success;
7031     l_error_message        varchar2(2000);
7032 
7033   BEGIN
7034 
7035     x_return_status := fnd_api.g_ret_sts_success;
7036 
7037     FOR inv_rec IN inv_cur (p_diag_txn_rec.mtl_txn_id)
7038     LOOP
7039 
7040       l_txn_rec.transaction_id                := fnd_api.g_miss_num;
7041       l_txn_rec.transaction_type_id           := correction_txn_type_id;
7042       l_txn_rec.source_header_ref             := 'DATAFIX';
7043       l_txn_rec.source_line_ref               := p_diag_txn_rec.process_code;
7044       l_txn_rec.source_line_ref_id            := inv_rec.trx_source_line_id;
7045       l_txn_rec.source_transaction_date       := inv_rec.mtl_txn_date;
7046       l_txn_rec.transaction_date              := inv_rec.mtl_txn_date;
7047 
7048       l_instance_rec.instance_id              := nvl(p_diag_txn_rec.inst_id,fnd_api.g_miss_num);
7049       l_instance_rec.location_type_code       := 'INVENTORY';
7050       l_instance_rec.instance_usage_code      := 'IN_INVENTORY';
7051       l_instance_rec.inventory_item_id        := inv_rec.item_id;
7052       l_instance_rec.serial_number            := p_diag_txn_rec.serial_number;
7053       l_instance_rec.inv_organization_id      := inv_rec.organization_id;
7054       l_instance_rec.quantity                 := 1;
7055       l_instance_rec.inv_subinventory_name    := inv_rec.subinv_code;
7056       l_instance_rec.inv_locator_id           := inv_rec.locator_id;
7057       get_lot_number(
7058         p_lot_code        => p_diag_txn_rec.lot_code,
7059         p_mtl_txn_id      => p_diag_txn_rec.mtl_txn_id,
7060         p_serial_number   => p_diag_txn_rec.serial_number,
7061         x_lot_number      => l_instance_rec.lot_number);
7062       l_instance_rec.inventory_revision       := inv_rec.revision;
7063       l_instance_rec.vld_organization_id      := inv_rec.organization_id;
7064       l_instance_rec.object_version_number    := 1.0;
7065 
7066     END LOOP;
7067 
7068     l_txn_rec.inv_material_transaction_id   := p_diag_txn_rec.mtl_txn_id;
7069     IF p_diag_txn_rec.inst_id is not null THEN
7070       update_instance(
7071         p_txn_rec        => l_txn_rec,
7072         p_instance_rec   => l_instance_rec,
7073         p_parties_tbl    => l_parties_tbl,
7074         p_pty_accts_tbl  => l_pty_accts_tbl,
7075         x_return_status  => l_return_status,
7076         x_error_message  => l_error_message);
7077     ELSE
7078 
7079       l_parties_tbl(1).instance_party_id      := fnd_api.g_miss_num;
7080       l_parties_tbl(1).party_source_table     := 'HZ_PARTIES';
7081       l_parties_tbl(1).party_id               := p_diag_txn_rec.internal_party_id;
7082       l_parties_tbl(1).relationship_type_code := 'OWNER';
7083       l_parties_tbl(1).contact_flag           := 'N';
7084       l_parties_tbl(1).object_version_number  := 1.0;
7085 
7086       create_instance(
7087         p_txn_rec        => l_txn_rec,
7088         p_instance_rec   => l_instance_rec,
7089         p_parties_tbl    => l_parties_tbl,
7090         p_pty_accts_tbl  => l_pty_accts_tbl,
7091         x_return_status  => l_return_status,
7092         x_error_message  => l_error_message);
7093     END IF;
7094 
7095     IF l_return_status <> fnd_api.g_ret_sts_success THEN
7096       x_return_status  := l_return_status;
7097       x_error_message  := l_error_message;
7098     END IF;
7099 
7100   END fix_wipissue;
7101 
7102   PROCEDURE fix_wipreturn(
7103     p_diag_txn_rec  IN  diag_txn_rec,
7104     x_return_status OUT nocopy varchar2,
7105     x_error_message OUT nocopy varchar2)
7106   IS
7107 
7108     l_txn_rec              csi_datastructures_pub.transaction_rec;
7109 
7110     l_instance_rec         csi_datastructures_pub.instance_rec;
7111     l_parties_tbl          csi_datastructures_pub.party_tbl;
7112     l_pty_accts_tbl        csi_datastructures_pub.party_account_tbl;
7113 
7114     l_return_status        varchar2(1) := fnd_api.g_ret_sts_success;
7115     l_error_message        varchar2(2000);
7116 
7117   BEGIN
7118 
7119     x_return_status := fnd_api.g_ret_sts_success;
7120 
7121     FOR inv_rec IN inv_cur (p_diag_txn_rec.mtl_txn_id)
7122     LOOP
7123 
7124       l_txn_rec.transaction_id                := fnd_api.g_miss_num;
7125       l_txn_rec.transaction_type_id           := correction_txn_type_id;
7126       l_txn_rec.source_header_ref             := 'DATAFIX';
7127       l_txn_rec.source_line_ref               := p_diag_txn_rec.process_code;
7128       l_txn_rec.source_line_ref_id            := inv_rec.mtl_source_id;
7129       l_txn_rec.source_transaction_date       := inv_rec.mtl_txn_date;
7130       l_txn_rec.transaction_date              := inv_rec.mtl_txn_date;
7131 
7132       l_instance_rec.instance_id              := nvl(p_diag_txn_rec.inst_id,fnd_api.g_miss_num);
7133       l_instance_rec.location_type_code       := 'WIP';
7134       l_instance_rec.instance_usage_code      := 'IN_WIP';
7135       l_instance_rec.inventory_item_id        := inv_rec.item_id;
7136       l_instance_rec.serial_number            := p_diag_txn_rec.serial_number;
7137       l_instance_rec.inv_organization_id      := null;
7138       l_instance_rec.quantity                 := 1;
7139       l_instance_rec.inv_subinventory_name    := null;
7140       l_instance_rec.inv_locator_id           := null;
7141       get_lot_number(
7142         p_lot_code        => p_diag_txn_rec.lot_code,
7143         p_mtl_txn_id      => p_diag_txn_rec.mtl_txn_id,
7144         p_serial_number   => p_diag_txn_rec.serial_number,
7145         x_lot_number      => l_instance_rec.lot_number);
7146       l_instance_rec.inventory_revision       := inv_rec.revision;
7147       l_instance_rec.vld_organization_id      := inv_rec.organization_id;
7148       l_instance_rec.wip_job_id               := inv_rec.mtl_source_id;
7149       l_instance_rec.object_version_number    := 1.0;
7150 
7151     END LOOP;
7152 
7153     IF p_diag_txn_rec.inst_id is not null THEN
7154       l_txn_rec.inv_material_transaction_id  := p_diag_txn_rec.mtl_txn_id;
7155       update_instance(
7156         p_txn_rec        => l_txn_rec,
7157         p_instance_rec   => l_instance_rec,
7158         p_parties_tbl    => l_parties_tbl,
7159         p_pty_accts_tbl  => l_pty_accts_tbl,
7160         x_return_status  => l_return_status,
7161         x_error_message  => l_error_message);
7162     END IF;
7163 
7164     IF l_return_status <> fnd_api.g_ret_sts_success THEN
7165       x_return_status  := l_return_status;
7166       x_error_message  := l_error_message;
7167     END IF;
7168 
7169   END fix_wipreturn;
7170 
7171   PROCEDURE fix_wipcompletion(
7172     p_diag_txn_rec  IN  diag_txn_rec,
7173     x_return_status OUT nocopy varchar2,
7174     x_error_message OUT nocopy varchar2)
7175   IS
7176 
7177     l_txn_rec              csi_datastructures_pub.transaction_rec;
7178 
7179     l_instance_rec         csi_datastructures_pub.instance_rec;
7180     l_parties_tbl          csi_datastructures_pub.party_tbl;
7181     l_pty_accts_tbl        csi_datastructures_pub.party_account_tbl;
7182 
7183     l_return_status        varchar2(1) := fnd_api.g_ret_sts_success;
7184     l_error_message        varchar2(2000);
7185 
7186   BEGIN
7187 
7188     x_return_status := fnd_api.g_ret_sts_success;
7189 
7190     FOR inv_rec IN inv_cur (p_diag_txn_rec.mtl_txn_id)
7191     LOOP
7192 
7193       l_txn_rec.transaction_id                := fnd_api.g_miss_num;
7194       l_txn_rec.transaction_type_id           := correction_txn_type_id;
7195       l_txn_rec.source_header_ref             := 'DATAFIX';
7196       l_txn_rec.source_line_ref               := p_diag_txn_rec.process_code;
7197       l_txn_rec.source_line_ref_id            := inv_rec.mtl_source_id;
7198       l_txn_rec.source_transaction_date       := inv_rec.mtl_txn_date;
7199       l_txn_rec.transaction_date              := inv_rec.mtl_txn_date;
7200 
7201       l_instance_rec.instance_id              := nvl(p_diag_txn_rec.inst_id,fnd_api.g_miss_num);
7202       l_instance_rec.location_type_code       := 'WIP';
7203       l_instance_rec.instance_usage_code      := 'IN_WIP';
7204       l_instance_rec.inventory_item_id        := inv_rec.item_id;
7205       l_instance_rec.serial_number            := p_diag_txn_rec.serial_number;
7206       l_instance_rec.inv_organization_id      := null;
7207       l_instance_rec.quantity                 := 1;
7208       l_instance_rec.inv_subinventory_name    := null;
7209       l_instance_rec.inv_locator_id           := null;
7210       get_lot_number(
7211         p_lot_code        => p_diag_txn_rec.lot_code,
7212         p_mtl_txn_id      => p_diag_txn_rec.mtl_txn_id,
7213         p_serial_number   => p_diag_txn_rec.serial_number,
7214         x_lot_number      => l_instance_rec.lot_number);
7215       l_instance_rec.inventory_revision       := inv_rec.revision;
7216       l_instance_rec.vld_organization_id      := inv_rec.organization_id;
7217       l_instance_rec.wip_job_id               := inv_rec.mtl_source_id;
7218       l_instance_rec.object_version_number    := 1.0;
7219 
7220     END LOOP;
7221 
7222     IF p_diag_txn_rec.inst_id is not null THEN
7223       l_txn_rec.inv_material_transaction_id  := p_diag_txn_rec.mtl_txn_id;
7224       update_instance(
7225         p_txn_rec        => l_txn_rec,
7226         p_instance_rec   => l_instance_rec,
7227         p_parties_tbl    => l_parties_tbl,
7228         p_pty_accts_tbl  => l_pty_accts_tbl,
7229         x_return_status  => l_return_status,
7230         x_error_message  => l_error_message);
7231     END IF;
7232 
7233     IF l_return_status <> fnd_api.g_ret_sts_success THEN
7234       x_return_status  := l_return_status;
7235       x_error_message  := l_error_message;
7236     END IF;
7237 
7238   END fix_wipcompletion;
7239 
7240   PROCEDURE fix_miscissue(
7241     p_diag_txn_rec  IN  diag_txn_rec,
7242     x_return_status OUT nocopy varchar2,
7243     x_error_message OUT nocopy varchar2)
7244   IS
7245 
7246     l_txn_rec              csi_datastructures_pub.transaction_rec;
7247 
7248     l_instance_rec         csi_datastructures_pub.instance_rec;
7249     l_parties_tbl          csi_datastructures_pub.party_tbl;
7250     l_pty_accts_tbl        csi_datastructures_pub.party_account_tbl;
7251 
7252     l_return_status        varchar2(1) := fnd_api.g_ret_sts_success;
7253     l_error_message        varchar2(2000);
7254 
7255   BEGIN
7256 
7257     x_return_status := fnd_api.g_ret_sts_success;
7258 
7259     FOR inv_rec IN inv_cur (p_diag_txn_rec.mtl_txn_id)
7260     LOOP
7261 
7262       l_txn_rec.transaction_id                := fnd_api.g_miss_num;
7263       l_txn_rec.transaction_type_id           := correction_txn_type_id;
7264       l_txn_rec.source_header_ref             := 'DATAFIX';
7265       l_txn_rec.source_line_ref               := p_diag_txn_rec.process_code;
7266       l_txn_rec.source_line_ref_id            := inv_rec.trx_source_line_id;
7267       l_txn_rec.source_transaction_date       := inv_rec.mtl_txn_date;
7268       l_txn_rec.transaction_date              := inv_rec.mtl_txn_date;
7269 
7270       l_instance_rec.instance_id              := nvl(p_diag_txn_rec.inst_id,fnd_api.g_miss_num);
7271       l_instance_rec.location_type_code       := 'INVENTORY';
7272       l_instance_rec.instance_usage_code      := 'IN_INVENTORY';
7273       l_instance_rec.inventory_item_id        := inv_rec.item_id;
7274       l_instance_rec.serial_number            := p_diag_txn_rec.serial_number;
7275       l_instance_rec.inv_organization_id      := inv_rec.organization_id;
7276       l_instance_rec.quantity                 := 1;
7277       l_instance_rec.inv_subinventory_name    := inv_rec.subinv_code;
7278       l_instance_rec.inv_locator_id           := inv_rec.locator_id;
7279       get_lot_number(
7280         p_lot_code        => p_diag_txn_rec.lot_code,
7281         p_mtl_txn_id      => p_diag_txn_rec.mtl_txn_id,
7282         p_serial_number   => p_diag_txn_rec.serial_number,
7283         x_lot_number      => l_instance_rec.lot_number);
7284       l_instance_rec.inventory_revision       := inv_rec.revision;
7285       l_instance_rec.vld_organization_id      := inv_rec.organization_id;
7286       l_instance_rec.object_version_number    := 1.0;
7287 
7288     END LOOP;
7289 
7290     l_txn_rec.inv_material_transaction_id  := p_diag_txn_rec.mtl_txn_id;
7291     IF p_diag_txn_rec.inst_id is not null THEN
7292       update_instance(
7293         p_txn_rec        => l_txn_rec,
7294         p_instance_rec   => l_instance_rec,
7295         p_parties_tbl    => l_parties_tbl,
7296         p_pty_accts_tbl  => l_pty_accts_tbl,
7297         x_return_status  => l_return_status,
7298         x_error_message  => l_error_message);
7299     ELSE
7300 
7301       l_parties_tbl(1).instance_party_id      := fnd_api.g_miss_num;
7302       l_parties_tbl(1).party_source_table     := 'HZ_PARTIES';
7303       l_parties_tbl(1).party_id               := p_diag_txn_rec.internal_party_id;
7304       l_parties_tbl(1).relationship_type_code := 'OWNER';
7305       l_parties_tbl(1).contact_flag           := 'N';
7306       l_parties_tbl(1).object_version_number  := 1.0;
7307 
7308       create_instance(
7309         p_txn_rec        => l_txn_rec,
7310         p_instance_rec   => l_instance_rec,
7311         p_parties_tbl    => l_parties_tbl,
7312         p_pty_accts_tbl  => l_pty_accts_tbl,
7313         x_return_status  => l_return_status,
7314         x_error_message  => l_error_message);
7315     END IF;
7316 
7317     IF l_return_status <> fnd_api.g_ret_sts_success THEN
7318       x_return_status  := l_return_status;
7319       x_error_message  := l_error_message;
7320     END IF;
7321 
7322   END fix_miscissue;
7323 
7324   PROCEDURE fix_miscreceipt(
7325     p_diag_txn_rec  in  diag_txn_rec,
7326     x_return_status OUT nocopy varchar2,
7327     x_error_message OUT nocopy varchar2)
7328   IS
7329 
7330     l_txn_rec              csi_datastructures_pub.transaction_rec;
7331 
7332     l_instance_rec         csi_datastructures_pub.instance_rec;
7333     l_parties_tbl          csi_datastructures_pub.party_tbl;
7334     l_pty_accts_tbl        csi_datastructures_pub.party_account_tbl;
7335 
7336     l_location_type_code   varchar2(30);
7337 
7338     l_return_status        varchar2(1) := fnd_api.g_ret_sts_success;
7339     l_error_message        varchar2(2000);
7340 
7341   BEGIN
7342 
7343     x_return_status := fnd_api.g_ret_sts_success;
7344 
7345     IF p_diag_txn_rec.inst_id is not null THEN
7346 
7347       SELECT location_type_code
7348       INTO   l_location_type_code
7349       FROM   csi_item_instances
7350       WHERE  instance_id = p_diag_txn_rec.inst_id;
7351 
7352       FOR inv_rec IN inv_cur (p_diag_txn_rec.mtl_txn_id)
7353       LOOP
7354 
7355         l_txn_rec.transaction_id                := fnd_api.g_miss_num;
7356         l_txn_rec.transaction_type_id           := correction_txn_type_id;
7357         l_txn_rec.source_header_ref             := 'DATAFIX';
7358         l_txn_rec.source_line_ref               := p_diag_txn_rec.process_code;
7359         l_txn_rec.source_line_ref_id            := inv_rec.trx_source_line_id;
7360         l_txn_rec.source_transaction_date       := inv_rec.mtl_txn_date;
7361         l_txn_rec.transaction_date              := inv_rec.mtl_txn_date;
7362 
7363         l_instance_rec.instance_id              := p_diag_txn_rec.inst_id;
7364         l_instance_rec.location_type_code       := 'INVENTORY';
7365         l_instance_rec.instance_usage_code      := 'IN_INVENTORY';
7366         l_instance_rec.inventory_item_id        := inv_rec.item_id;
7367         l_instance_rec.serial_number            := p_diag_txn_rec.serial_number;
7368         l_instance_rec.inv_organization_id      := inv_rec.organization_id;
7369         l_instance_rec.quantity                 := 1;
7370         l_instance_rec.inv_subinventory_name    := inv_rec.subinv_code;
7371         l_instance_rec.inv_locator_id           := inv_rec.locator_id;
7372       get_lot_number(
7373         p_lot_code        => p_diag_txn_rec.lot_code,
7374         p_mtl_txn_id      => p_diag_txn_rec.mtl_txn_id,
7375         p_serial_number   => p_diag_txn_rec.serial_number,
7376         x_lot_number      => l_instance_rec.lot_number);
7377         l_instance_rec.inventory_revision       := inv_rec.revision;
7378         l_instance_rec.vld_organization_id      := inv_rec.organization_id;
7379         l_instance_rec.active_end_date          := sysdate;
7380         l_instance_rec.object_version_number    := 1.0;
7381 
7382       END LOOP;
7383 
7384       l_txn_rec.inv_material_transaction_id  := p_diag_txn_rec.mtl_txn_id;
7385       update_instance(
7386         p_txn_rec        => l_txn_rec,
7387         p_instance_rec   => l_instance_rec,
7388         p_parties_tbl    => l_parties_tbl,
7389         p_pty_accts_tbl  => l_pty_accts_tbl,
7390         x_return_status  => l_return_status,
7391         x_error_message  => l_error_message);
7392 
7393       IF l_return_status <> fnd_api.g_ret_sts_success THEN
7394         x_return_status  := l_return_status;
7395         x_error_message  := l_error_message;
7396       END IF;
7397 
7398     END IF;
7399 
7400   END fix_miscreceipt;
7401 
7402   PROCEDURE fix_sixfer(
7403     p_diag_txn_rec    IN  diag_txn_rec,
7404     x_return_status   OUT nocopy varchar2,
7405     x_error_message   OUT nocopy varchar2)
7406   IS
7407 
7408     l_mtl_xfer_txn_id      number;
7409     l_txn_rec              csi_datastructures_pub.transaction_rec;
7410 
7411     l_instance_rec         csi_datastructures_pub.instance_rec;
7412     l_parties_tbl          csi_datastructures_pub.party_tbl;
7413     l_pty_accts_tbl        csi_datastructures_pub.party_account_tbl;
7414 
7415     l_return_status        varchar2(1) := fnd_api.g_ret_sts_success;
7416     l_error_message        varchar2(2000);
7417 
7418   BEGIN
7419 
7420     x_return_status := fnd_api.g_ret_sts_success;
7421 
7422     SELECT transfer_transaction_id
7423     INTO   l_mtl_xfer_txn_id
7424     FROM   mtl_material_transactions
7425     WHERE  transaction_id = p_diag_txn_rec.mtl_txn_id;
7426 
7427     FOR inv_rec IN from_sixfer_cur(
7428       p_diag_txn_rec.mtl_txn_id,
7429       l_mtl_xfer_txn_id)
7430     LOOP
7431 
7432       l_txn_rec.transaction_id                := fnd_api.g_miss_num;
7433       l_txn_rec.transaction_type_id           := correction_txn_type_id;
7434       l_txn_rec.source_header_ref             := 'DATAFIX';
7435       l_txn_rec.source_line_ref               := p_diag_txn_rec.process_code;
7436       l_txn_rec.source_line_ref_id            := inv_rec.trx_source_line_id;
7437       l_txn_rec.source_transaction_date       := inv_rec.mtl_txn_date;
7438       l_txn_rec.transaction_date              := inv_rec.mtl_txn_date;
7439 
7440       l_instance_rec.instance_id              := nvl(p_diag_txn_rec.inst_id,fnd_api.g_miss_num);
7441       l_instance_rec.location_type_code       := 'INVENTORY';
7442       l_instance_rec.instance_usage_code      := 'IN_INVENTORY';
7443       l_instance_rec.inventory_item_id        := inv_rec.item_id;
7444       l_instance_rec.serial_number            := p_diag_txn_rec.serial_number;
7445       l_instance_rec.inv_organization_id      := inv_rec.organization_id;
7446       l_instance_rec.quantity                 := 1;
7447       l_instance_rec.inv_subinventory_name    := inv_rec.subinv_code;
7448       l_instance_rec.inv_locator_id           := inv_rec.locator_id;
7449       get_lot_number(
7450         p_lot_code        => p_diag_txn_rec.lot_code,
7451         p_mtl_txn_id      => p_diag_txn_rec.mtl_txn_id,
7452         p_serial_number   => p_diag_txn_rec.serial_number,
7453         x_lot_number      => l_instance_rec.lot_number);
7454       l_instance_rec.inventory_revision       := inv_rec.revision;
7455       l_instance_rec.vld_organization_id      := inv_rec.organization_id;
7456       l_instance_rec.object_version_number    := 1.0;
7457 
7458     END LOOP;
7459 
7460     l_txn_rec.inv_material_transaction_id  := p_diag_txn_rec.mtl_txn_id;
7461     IF p_diag_txn_rec.inst_id is not null THEN
7462       update_instance(
7463         p_txn_rec        => l_txn_rec,
7464         p_instance_rec   => l_instance_rec,
7465         p_parties_tbl    => l_parties_tbl,
7466         p_pty_accts_tbl  => l_pty_accts_tbl,
7467         x_return_status  => l_return_status,
7468         x_error_message  => l_error_message);
7469     ELSE
7470 
7471       l_parties_tbl(1).instance_party_id      := fnd_api.g_miss_num;
7472       l_parties_tbl(1).party_source_table     := 'HZ_PARTIES';
7473       l_parties_tbl(1).party_id               := p_diag_txn_rec.internal_party_id;
7474       l_parties_tbl(1).relationship_type_code := 'OWNER';
7475       l_parties_tbl(1).contact_flag           := 'N';
7476       l_parties_tbl(1).object_version_number  := 1.0;
7477 
7478       create_instance(
7479         p_txn_rec        => l_txn_rec,
7480         p_instance_rec   => l_instance_rec,
7481         p_parties_tbl    => l_parties_tbl,
7482         p_pty_accts_tbl  => l_pty_accts_tbl,
7483         x_return_status  => l_return_status,
7484         x_error_message  => l_error_message);
7485     END IF;
7486 
7487     IF l_return_status <> fnd_api.g_ret_sts_success THEN
7488       x_return_status  := l_return_status;
7489       x_error_message  := l_error_message;
7490     END IF;
7491 
7492   END fix_sixfer;
7493 
7494   PROCEDURE fix_projreceipt(
7495     p_diag_txn_rec  IN  diag_txn_rec,
7496     x_return_status OUT nocopy varchar2,
7497     x_error_message OUT nocopy varchar2)
7498   IS
7499 
7500     l_txn_rec              csi_datastructures_pub.transaction_rec;
7501 
7502     l_instance_rec         csi_datastructures_pub.instance_rec;
7503     l_parties_tbl          csi_datastructures_pub.party_tbl;
7504     l_pty_accts_tbl        csi_datastructures_pub.party_account_tbl;
7505 
7506     l_project_location_id  number;
7507 
7508     l_return_status        varchar2(1) := fnd_api.g_ret_sts_success;
7509     l_error_message        varchar2(2000);
7510 
7511   BEGIN
7512 
7513     x_return_status := fnd_api.g_ret_sts_success;
7514 
7515     IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
7516       csi_gen_utility_pvt.populate_install_param_rec;
7517     END IF;
7518     --
7519     l_project_location_id := csi_datastructures_pub.g_install_param_rec.project_location_id;
7520     --
7521 
7522     FOR inv_rec IN inv_cur (p_diag_txn_rec.mtl_txn_id)
7523     LOOP
7524 
7525       l_txn_rec.transaction_id                := fnd_api.g_miss_num;
7526       l_txn_rec.transaction_type_id           := correction_txn_type_id;
7527       l_txn_rec.source_header_ref             := 'DATAFIX';
7528       l_txn_rec.source_line_ref               := p_diag_txn_rec.process_code;
7529       l_txn_rec.source_line_ref_id            := inv_rec.trx_source_line_id;
7530       l_txn_rec.source_transaction_date       := inv_rec.mtl_txn_date;
7531       l_txn_rec.transaction_date              := inv_rec.mtl_txn_date;
7532 
7533 
7534       l_instance_rec.instance_id              := nvl(p_diag_txn_rec.inst_id,fnd_api.g_miss_num);
7535       l_instance_rec.accounting_class_code    := 'PROJECT';
7536       l_instance_rec.location_type_code       := 'PROJECT';
7537       l_instance_rec.location_id              := l_project_location_id;
7538       l_instance_rec.pa_project_id            := inv_rec.source_project_id;
7539       l_instance_rec.pa_project_task_id       := inv_rec.source_task_id;
7540       l_instance_rec.instance_usage_code      := 'IN_PROCESS';
7541       l_instance_rec.inventory_item_id        := inv_rec.item_id;
7542       l_instance_rec.serial_number            := p_diag_txn_rec.serial_number;
7543       l_instance_rec.quantity                 := 1;
7544       l_instance_rec.inv_organization_id      := null;
7545       l_instance_rec.inv_subinventory_name    := null;
7546       l_instance_rec.inv_locator_id           := null;
7547       l_instance_rec.lot_number               := null;
7548       l_instance_rec.inventory_revision       := inv_rec.revision;
7549       l_instance_rec.vld_organization_id      := inv_rec.organization_id;
7550       l_instance_rec.object_version_number    := 1.0;
7551 
7552     END LOOP;
7553 
7554     l_txn_rec.inv_material_transaction_id  := p_diag_txn_rec.mtl_txn_id;
7555     IF p_diag_txn_rec.inst_id is not null THEN
7556       update_instance(
7557         p_txn_rec        => l_txn_rec,
7558         p_instance_rec   => l_instance_rec,
7559         p_parties_tbl    => l_parties_tbl,
7560         p_pty_accts_tbl  => l_pty_accts_tbl,
7561         x_return_status  => l_return_status,
7562         x_error_message  => l_error_message);
7563     ELSE
7564 
7565       l_parties_tbl(1).instance_party_id      := fnd_api.g_miss_num;
7566       l_parties_tbl(1).party_source_table     := 'HZ_PARTIES';
7567       l_parties_tbl(1).party_id               := p_diag_txn_rec.internal_party_id;
7568       l_parties_tbl(1).relationship_type_code := 'OWNER';
7569       l_parties_tbl(1).contact_flag           := 'N';
7570       l_parties_tbl(1).object_version_number  := 1.0;
7571 
7572       create_instance(
7573         p_txn_rec        => l_txn_rec,
7574         p_instance_rec   => l_instance_rec,
7575         p_parties_tbl    => l_parties_tbl,
7576         p_pty_accts_tbl  => l_pty_accts_tbl,
7577         x_return_status  => l_return_status,
7578         x_error_message  => l_error_message);
7579     END IF;
7580 
7581     IF l_return_status <> fnd_api.g_ret_sts_success THEN
7582       x_return_status  := l_return_status;
7583       x_error_message  := l_error_message;
7584     END IF;
7585 
7586   END fix_projreceipt;
7587 
7588   PROCEDURE fix_interorgreceipt(
7589     p_diag_txn_rec  IN  diag_txn_rec,
7590     x_return_status OUT nocopy varchar2,
7591     x_error_message OUT nocopy varchar2)
7592   IS
7593 
7594     l_txn_rec              csi_datastructures_pub.transaction_rec;
7595 
7596     l_internal_party_id    number;
7597     l_src_serial_code      number;
7598     l_lot_number           varchar2(80);
7599     l_inv_location_id      number;
7600 
7601     l_instance_quantity    number;
7602     l_instance_rec_tmp     csi_datastructures_pub.instance_rec;
7603     l_instance_rec         csi_datastructures_pub.instance_rec;
7604     l_parties_tbl          csi_datastructures_pub.party_tbl;
7605     l_pty_accts_tbl        csi_datastructures_pub.party_account_tbl;
7606     l_org_units_tbl         csi_datastructures_pub.organization_units_tbl;
7607     l_ea_values_tbl         csi_datastructures_pub.extend_attrib_values_tbl;
7608     l_pricing_tbl           csi_datastructures_pub.pricing_attribs_tbl;
7609     l_assets_tbl            csi_datastructures_pub.instance_asset_tbl;
7610     l_instance_ids_list     csi_datastructures_pub.id_tbl;
7611 
7612     l_return_status        varchar2(1) := fnd_api.g_ret_sts_success;
7613     l_msg_data             varchar2(2000);
7614     l_msg_count            number;
7615     l_error_message        varchar2(2000);
7616 
7617     user_error             exception;
7618 
7619   BEGIN
7620 
7621     x_return_status := fnd_api.g_ret_sts_success;
7622 
7623     IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
7624        csi_gen_utility_pvt.populate_install_param_rec;
7625     END IF;
7626     --
7627     l_internal_party_id := csi_datastructures_pub.g_install_param_rec.internal_party_id;
7628     --
7629     FOR inv_rec IN inv_cur (p_diag_txn_rec.mtl_txn_id)
7630     LOOP
7631 
7632       l_txn_rec.transaction_id                := fnd_api.g_miss_num;
7633       l_txn_rec.transaction_type_id           := correction_txn_type_id;
7634       l_txn_rec.source_header_ref             := 'DATAFIX';
7635       l_txn_rec.source_line_ref               := p_diag_txn_rec.process_code;
7636       l_txn_rec.source_line_ref_id            := inv_rec.trx_source_line_id;
7637       l_txn_rec.source_transaction_date       := inv_rec.mtl_txn_date;
7638       l_txn_rec.transaction_date              := inv_rec.mtl_txn_date;
7639 
7640       SELECT serial_number_control_code
7641       INTO   l_src_serial_code
7642       FROM   mtl_system_items_b
7643       WHERE  inventory_item_id = inv_rec.item_id
7644       AND    organization_id   = inv_rec.xfer_organization_id;
7645 
7646       SELECT nvl(mssi.location_id, haou.location_id)
7647       INTO   l_inv_location_id
7648       FROM   mtl_secondary_inventories mssi,
7649              hr_all_organization_units haou
7650       WHERE  mssi.organization_id          = inv_rec.organization_id
7651       AND    mssi.secondary_inventory_name = inv_rec.subinv_code
7652       AND    haou.organization_id          = mssi.organization_id;
7653 
7654       IF l_src_serial_code in (2, 5) THEN
7655 
7656         l_instance_rec.instance_id              := nvl(p_diag_txn_rec.inst_id,fnd_api.g_miss_num);
7657         l_instance_rec.location_type_code       := 'INVENTORY';
7658         l_instance_rec.location_id              := l_inv_location_id;
7659         l_instance_rec.instance_usage_code      := 'IN_TRANSIT';
7660         l_instance_rec.inventory_item_id        := inv_rec.item_id;
7661         l_instance_rec.serial_number            := p_diag_txn_rec.serial_number;
7662         l_instance_rec.inv_organization_id      := inv_rec.xfer_organization_id;
7663         l_instance_rec.quantity                 := 1;
7664         l_instance_rec.inv_subinventory_name    := null;
7665         l_instance_rec.inv_locator_id           := null;
7666         l_instance_rec.lot_number               := null;
7667         l_instance_rec.inventory_revision       := inv_rec.revision;
7668         l_instance_rec.vld_organization_id      := inv_rec.xfer_organization_id;
7669         l_instance_rec.object_version_number    := 1.0;
7670 
7671         l_txn_rec.inv_material_transaction_id  := p_diag_txn_rec.mtl_txn_id;
7672         IF p_diag_txn_rec.inst_id is not null THEN
7673           update_instance(
7674             p_txn_rec        => l_txn_rec,
7675             p_instance_rec   => l_instance_rec,
7676             p_parties_tbl    => l_parties_tbl,
7677             p_pty_accts_tbl  => l_pty_accts_tbl,
7678             x_return_status  => l_return_status,
7679             x_error_message  => l_error_message);
7680         ELSE
7681 
7682           l_parties_tbl(1).instance_party_id      := fnd_api.g_miss_num;
7683           l_parties_tbl(1).party_source_table     := 'HZ_PARTIES';
7684           l_parties_tbl(1).party_id               := l_internal_party_id;
7685           l_parties_tbl(1).relationship_type_code := 'OWNER';
7686           l_parties_tbl(1).contact_flag           := 'N';
7687           l_parties_tbl(1).object_version_number  := 1.0;
7688 
7689           create_instance(
7690             p_txn_rec        => l_txn_rec,
7691             p_instance_rec   => l_instance_rec,
7692             p_parties_tbl    => l_parties_tbl,
7693             p_pty_accts_tbl  => l_pty_accts_tbl,
7694             x_return_status  => l_return_status,
7695             x_error_message  => l_error_message);
7696         END IF;
7697 
7698         IF l_return_status <> fnd_api.g_ret_sts_success THEN
7699           x_return_status  := l_return_status;
7700           x_error_message  := l_error_message;
7701         END IF;
7702       ELSIF l_src_serial_code in (1,6) THEN
7703 
7704         l_instance_rec := l_instance_rec_tmp;
7705         l_parties_tbl.delete;
7706 
7707         -- create/update a non serial intransit instance
7708         get_lot_number(
7709           p_lot_code        => p_diag_txn_rec.lot_code,
7710           p_mtl_txn_id      => p_diag_txn_rec.mtl_txn_id,
7711           p_serial_number   => p_diag_txn_rec.serial_number,
7712           x_lot_number      => l_lot_number);
7713 
7714         BEGIN
7715           SELECT instance_id,
7716                  object_version_number,
7717                  quantity
7718           INTO   l_instance_rec.instance_id,
7719                  l_instance_rec.object_version_number,
7720                  l_instance_quantity
7721           FROM   csi_item_instances
7722           WHERE  inventory_item_id                = inv_rec.item_id
7723           AND    nvl(inventory_revision,'$$##$$') = nvl(inv_rec.revision, '$$##$$')
7724           AND    nvl(lot_number,'$$##$$')         = nvl(l_lot_number, '$$##$$')
7725           AND    location_type_code               = 'INVENTORY'
7726           AND    instance_usage_code              = 'IN_TRANSIT'
7727           AND    serial_number                    is null;
7728 
7729           l_instance_rec.quantity := l_instance_quantity + 1;
7730 
7731         EXCEPTION
7732           WHEN no_data_found THEN
7733             l_instance_rec.instance_id := fnd_api.g_miss_num;
7734             l_instance_rec.quantity    := 1;
7735           WHEN too_many_rows THEN
7736             x_return_status := fnd_api.g_ret_sts_error;
7737             x_error_message := 'Multiple non serial in_transit instances found.';
7738             RAISE user_error;
7739         END;
7740 
7741         IF nvl(l_instance_rec.instance_id, fnd_api.g_miss_num) <> fnd_api.g_miss_num THEN
7742 
7743           l_instance_rec.active_end_date       := null;
7744           l_instance_rec.instance_status_id    := 3;
7745 
7746           csi_item_instance_pub.update_item_instance(
7747             p_api_version           => 1.0,
7748             p_commit                => fnd_api.g_false,
7749             p_init_msg_list         => fnd_api.g_true,
7750             p_validation_level      => fnd_api.g_valid_level_full,
7751             p_instance_rec          => l_instance_rec,
7752             p_party_tbl             => l_parties_tbl,
7753             p_account_tbl           => l_pty_accts_tbl,
7754             p_org_assignments_tbl   => l_org_units_tbl,
7755             p_ext_attrib_values_tbl => l_ea_values_tbl,
7756             p_pricing_attrib_tbl    => l_pricing_tbl,
7757             p_asset_assignment_tbl  => l_assets_tbl,
7758             p_txn_rec               => l_txn_rec,
7759             x_instance_id_lst       => l_instance_ids_list,
7760             x_return_status         => l_return_status,
7761             x_msg_count             => l_msg_count,
7762             x_msg_data              => l_msg_data);
7763 
7764           -- For Bug 4057183
7765           -- IF l_return_status <> fnd_api.g_ret_sts_success THEN
7766           IF l_return_status not in (fnd_api.g_ret_sts_success,'W') THEN
7767             x_return_status  := l_return_status;
7768             x_error_message  := csi_t_gen_utility_pvt.dump_error_stack;
7769             RAISE user_error;
7770           END IF;
7771 
7772         ELSE
7773 
7774           l_instance_rec.inventory_item_id        := inv_rec.item_id;
7775           l_instance_rec.inventory_revision       := inv_rec.revision;
7776           l_instance_rec.vld_organization_id      := inv_rec.xfer_organization_id;
7777           l_instance_rec.instance_usage_code      := 'IN_TRANSIT';
7778           l_instance_rec.location_type_code       := 'INVENTORY';
7779           l_instance_rec.location_id              := l_inv_location_id;
7780           l_instance_rec.active_end_date          := null;
7781           l_instance_rec.inv_organization_id      := null;
7782           l_instance_rec.inv_subinventory_name    := null;
7783           l_instance_rec.inv_locator_id           := null;
7784           l_instance_rec.lot_number               := l_lot_number;
7785 
7786           l_parties_tbl(1).instance_party_id      := fnd_api.g_miss_num;
7787           l_parties_tbl(1).instance_id            := fnd_api.g_miss_num;
7788           l_parties_tbl(1).party_source_table     := 'HZ_PARTIES';
7789           l_parties_tbl(1).party_id               := l_internal_party_id;
7790           l_parties_tbl(1).relationship_type_code := 'OWNER';
7791           l_parties_tbl(1).contact_flag           := 'N';
7792           l_parties_tbl(1).object_version_number  := 1.0;
7793 
7794           create_instance(
7795             p_txn_rec        => l_txn_rec,
7796             p_instance_rec   => l_instance_rec,
7797             p_parties_tbl    => l_parties_tbl,
7798             p_pty_accts_tbl  => l_pty_accts_tbl,
7799             x_return_status  => l_return_status,
7800             x_error_message  => l_error_message);
7801 
7802           IF l_return_status <> fnd_api.g_ret_sts_success THEN
7803             x_return_status  := l_return_status;
7804             x_error_message  := l_error_message;
7805             RAISE user_error;
7806           END IF;
7807         END IF;
7808 
7809         -- update the destination in the returned status for 6
7810         IF l_src_serial_code = 6 and p_diag_txn_rec.inst_id is not null THEN
7811 
7812           l_instance_rec := l_instance_rec_tmp;
7813           l_parties_tbl.delete;
7814 
7815           l_instance_rec.instance_id          := p_diag_txn_rec.inst_id;
7816           l_instance_rec.location_type_code   := 'INVENTORY';
7817           l_instance_rec.inv_organization_id  := inv_rec.organization_id;
7818           l_instance_rec.inv_subinventory_name:= inv_rec.subinv_code;
7819 
7820           SELECT nvl(mssi.location_id, haou.location_id)
7821           INTO   l_instance_rec.location_id
7822           FROM   mtl_secondary_inventories mssi,
7823                  hr_all_organization_units haou
7824           WHERE  mssi.organization_id          = l_instance_rec.inv_organization_id
7825           AND    mssi.secondary_inventory_name = l_instance_rec.inv_subinventory_name
7826           AND    haou.organization_id          = mssi.organization_id;
7827 
7828           l_instance_rec.instance_usage_code    := 'RETURNED';
7829           l_instance_rec.inventory_item_id      := inv_rec.item_id;
7830           l_instance_rec.serial_number          := p_diag_txn_rec.serial_number;
7831           l_instance_rec.mfg_serial_number_flag := 'Y';
7832           l_instance_rec.quantity               := 1;
7833           l_instance_rec.inv_locator_id         := inv_rec.locator_id;
7834           l_instance_rec.creation_complete_flag := 'N';
7835 
7836           get_lot_number(
7837             p_lot_code        => p_diag_txn_rec.lot_code,
7838             p_mtl_txn_id      => p_diag_txn_rec.mtl_txn_id,
7839             p_serial_number   => p_diag_txn_rec.serial_number,
7840             x_lot_number      => l_instance_rec.lot_number);
7841 
7842           l_instance_rec.inventory_revision       := inv_rec.revision;
7843           l_instance_rec.vld_organization_id      := inv_rec.organization_id;
7844 
7845           l_txn_rec.inv_material_transaction_id  := p_diag_txn_rec.mtl_txn_id;
7846 
7847           update_instance(
7848             p_txn_rec        => l_txn_rec,
7849             p_instance_rec   => l_instance_rec,
7850             p_parties_tbl    => l_parties_tbl,
7851             p_pty_accts_tbl  => l_pty_accts_tbl,
7852             x_return_status  => l_return_status,
7853             x_error_message  => l_error_message);
7854 
7855           IF l_return_status <> fnd_api.g_ret_sts_success THEN
7856             x_return_status := l_return_status;
7857             x_error_message := l_error_message;
7858           END IF;
7859         END IF;
7860       END IF;
7861 
7862     END LOOP;
7863 
7864   EXCEPTION
7865     WHEN user_error THEN
7866       -- just to bring the control to the end of the routine
7867       null;
7868   END fix_interorgreceipt;
7869 
7870   PROCEDURE fix_isoreceipt(
7871     p_diag_txn_rec  IN  diag_txn_rec,
7872     x_return_status OUT nocopy varchar2,
7873     x_error_message OUT nocopy varchar2)
7874   IS
7875 
7876     l_txn_rec               csi_datastructures_pub.transaction_rec;
7877 
7878     l_instance_rec          csi_datastructures_pub.instance_rec;
7879     l_parties_tbl           csi_datastructures_pub.party_tbl;
7880     l_pty_accts_tbl         csi_datastructures_pub.party_account_tbl;
7881     l_org_units_tbl         csi_datastructures_pub.organization_units_tbl;
7882     l_ea_values_tbl         csi_datastructures_pub.extend_attrib_values_tbl;
7883     l_pricing_tbl           csi_datastructures_pub.pricing_attribs_tbl;
7884     l_assets_tbl            csi_datastructures_pub.instance_asset_tbl;
7885     l_instance_ids_list     csi_datastructures_pub.id_tbl;
7886 
7887     l_src_serial_code       number;
7888     l_dest_serial_code      number;
7889 
7890     l_instance_quantity     number;
7891     l_internal_party_id     number;
7892     l_intransit_location_id number;
7893     l_lot_number            varchar2(80);
7894 
7895     l_return_status         varchar2(1) := fnd_api.g_ret_sts_success;
7896     l_msg_count             number;
7897     l_msg_data              varchar2(2000);
7898 
7899     l_error_message         varchar2(2000);
7900 
7901   BEGIN
7902 
7903     x_return_status := fnd_api.g_ret_sts_success;
7904 
7905     IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
7906        csi_gen_utility_pvt.populate_install_param_rec;
7907     END IF;
7908     --
7909     l_internal_party_id := csi_datastructures_pub.g_install_param_rec.internal_party_id;
7910     l_intransit_location_id := csi_datastructures_pub.g_install_param_rec.in_transit_location_id;
7911     --
7912     FOR inv_rec IN inv_cur (p_diag_txn_rec.mtl_txn_id)
7913     LOOP
7914 
7915       l_txn_rec.transaction_id                := fnd_api.g_miss_num;
7916       l_txn_rec.transaction_type_id           := correction_txn_type_id;
7917       l_txn_rec.source_header_ref             := 'DATAFIX';
7918       l_txn_rec.source_line_ref               := p_diag_txn_rec.process_code;
7919       l_txn_rec.source_line_ref_id            := inv_rec.trx_source_line_id;
7920       l_txn_rec.source_transaction_date       := inv_rec.mtl_txn_date;
7921       l_txn_rec.transaction_date              := inv_rec.mtl_txn_date;
7922 
7923       SELECT serial_number_control_code
7924       INTO   l_src_serial_code
7925       FROM   mtl_system_items_b
7926       WHERE  inventory_item_id = inv_rec.item_id
7927       AND    organization_id   = inv_rec.xfer_organization_id;
7928 
7929       SELECT serial_number_control_code
7930       INTO   l_dest_serial_code
7931       FROM   mtl_system_items_b
7932       WHERE  inventory_item_id = inv_rec.item_id
7933       AND    organization_id   = inv_rec.organization_id;
7934 
7935       IF l_src_serial_code in (2, 5, 6) THEN
7936         l_instance_rec.instance_id              := nvl(p_diag_txn_rec.inst_id,fnd_api.g_miss_num);
7937 
7938         IF l_dest_serial_code = 1 THEN
7939           l_instance_rec.location_type_code     := 'HZ_LOCATIONS';
7940           l_instance_rec.location_type_code     := 'OUT_OF_ENTERPRISE';
7941         ELSE
7942           l_instance_rec.location_type_code     := 'IN_TRANSIT';
7943           l_instance_rec.instance_usage_code    := 'IN_TRANSIT';
7944         END IF;
7945 
7946         l_instance_rec.location_id              := l_intransit_location_id;
7947         l_instance_rec.inventory_item_id        := inv_rec.item_id;
7948         l_instance_rec.serial_number            := p_diag_txn_rec.serial_number;
7949         l_instance_rec.quantity                 := 1;
7950         l_instance_rec.inv_organization_id      := null;
7951         l_instance_rec.inv_subinventory_name    := null;
7952         l_instance_rec.inv_locator_id           := null;
7953         l_instance_rec.lot_number               := null;
7954         l_instance_rec.inventory_revision       := inv_rec.revision;
7955 
7956         -- read the order line id from the shipment transaction prior to the ISO receipt
7957         -- and stamp it in in_transit_order_line_id
7958         BEGIN
7959           SELECT trx_source_line_id
7960           INTO   l_instance_rec.in_transit_order_line_id
7961           FROM   mtl_material_transactions
7962           WHERE  transaction_action_id      = 21
7963           AND    transaction_source_type_id = 8
7964           AND   (shipment_number, inventory_item_id) IN (
7965                  SELECT shipment_number, inventory_item_id
7966                  FROM   mtl_material_transactions
7967                  WHERE  transaction_id = inv_rec.mtl_txn_id)
7968           AND rownum = 1;
7969         EXCEPTION
7970           WHEN no_data_found THEN
7971             l_instance_rec.in_transit_order_line_id := inv_rec.trx_source_line_id;
7972         END;
7973 
7974         l_instance_rec.vld_organization_id      := inv_rec.organization_id;
7975         l_instance_rec.object_version_number    := 1.0;
7976 
7977         l_txn_rec.inv_material_transaction_id  := p_diag_txn_rec.mtl_txn_id;
7978         IF p_diag_txn_rec.inst_id is not null THEN
7979           update_instance(
7980             p_txn_rec        => l_txn_rec,
7981             p_instance_rec   => l_instance_rec,
7982             p_parties_tbl    => l_parties_tbl,
7983             p_pty_accts_tbl  => l_pty_accts_tbl,
7984             x_return_status  => l_return_status,
7985             x_error_message  => l_error_message);
7986         ELSE
7987 
7988           l_parties_tbl(1).instance_party_id      := fnd_api.g_miss_num;
7989           l_parties_tbl(1).party_source_table     := 'HZ_PARTIES';
7990           l_parties_tbl(1).party_id               := l_internal_party_id;
7991           l_parties_tbl(1).relationship_type_code := 'OWNER';
7992           l_parties_tbl(1).contact_flag           := 'N';
7993           l_parties_tbl(1).object_version_number  := 1.0;
7994 
7995           create_instance(
7996             p_txn_rec        => l_txn_rec,
7997             p_instance_rec   => l_instance_rec,
7998             p_parties_tbl    => l_parties_tbl,
7999             p_pty_accts_tbl  => l_pty_accts_tbl,
8000             x_return_status  => l_return_status,
8001             x_error_message  => l_error_message);
8002         END IF;
8003 
8004         IF l_return_status <> fnd_api.g_ret_sts_success THEN
8005           x_return_status  := l_return_status;
8006           x_error_message  := l_error_message;
8007         END IF;
8008 
8009       ELSIF l_src_serial_code = 1 THEN
8010         BEGIN
8011 
8012           get_lot_number(
8013             p_lot_code        => p_diag_txn_rec.lot_code,
8014             p_mtl_txn_id      => p_diag_txn_rec.mtl_txn_id,
8015             p_serial_number   => p_diag_txn_rec.serial_number,
8016             x_lot_number      => l_lot_number);
8017 
8018           SELECT instance_id,
8019                  object_version_number,
8020                  quantity
8021           INTO   l_instance_rec.instance_id,
8022                  l_instance_rec.object_version_number,
8023                  l_instance_quantity
8024           FROM   csi_item_instances
8025           WHERE  inventory_item_id                = inv_rec.item_id
8026           AND    nvl(inventory_revision,'$$##$$') = nvl(inv_rec.revision, '$$##$$')
8027           AND    nvl(lot_number,'$$##$$')         = nvl(l_lot_number, '$$##$$')
8028           AND    location_type_code               = 'IN_TRANSIT'
8029           AND    instance_usage_code              = 'IN_TRANSIT'
8030           AND    serial_number                    is null;
8031 
8032           l_instance_rec.quantity := l_instance_quantity + inv_rec.quantity;
8033 
8034         EXCEPTION
8035           WHEN no_data_found THEN
8036             l_instance_rec.instance_id           := fnd_api.g_miss_num;
8037             l_instance_rec.object_version_number := 1;
8038             l_instance_rec.quantity              := inv_rec.quantity;
8039         END;
8040 
8041         BEGIN
8042           SELECT trx_source_line_id
8043           INTO   l_instance_rec.in_transit_order_line_id
8044           FROM   mtl_material_transactions
8045           WHERE  transaction_action_id      = 21
8046           AND    transaction_source_type_id = 8
8047           AND   (shipment_number, inventory_item_id) IN (
8048                  SELECT shipment_number, inventory_item_id
8049                  FROM   mtl_material_transactions
8050                  WHERE  transaction_id = inv_rec.mtl_txn_id)
8051           AND rownum = 1;
8052         EXCEPTION
8053           WHEN no_data_found THEN
8054             l_instance_rec.in_transit_order_line_id := inv_rec.trx_source_line_id;
8055         END;
8056 
8057         IF nvl(l_instance_rec.instance_id, fnd_api.g_miss_num) <> fnd_api.g_miss_num THEN
8058 
8059           l_instance_rec.active_end_date       := null;
8060           l_instance_rec.instance_status_id    := 3;
8061 
8062           csi_item_instance_pub.update_item_instance(
8063             p_api_version           => 1.0,
8064             p_commit                => fnd_api.g_false,
8065             p_init_msg_list         => fnd_api.g_true,
8066             p_validation_level      => fnd_api.g_valid_level_full,
8067             p_instance_rec          => l_instance_rec,
8068             p_party_tbl             => l_parties_tbl,
8069             p_account_tbl           => l_pty_accts_tbl,
8070             p_org_assignments_tbl   => l_org_units_tbl,
8071             p_ext_attrib_values_tbl => l_ea_values_tbl,
8072             p_pricing_attrib_tbl    => l_pricing_tbl,
8073             p_asset_assignment_tbl  => l_assets_tbl,
8074             p_txn_rec               => l_txn_rec,
8075             x_instance_id_lst       => l_instance_ids_list,
8076             x_return_status         => l_return_status,
8077             x_msg_count             => l_msg_count,
8078             x_msg_data              => l_msg_data);
8079 
8080           -- For Bug 4057183
8081           -- IF l_return_status <> fnd_api.g_ret_sts_success THEN
8082           IF l_return_status not in (fnd_api.g_ret_sts_success,'W') THEN
8083             x_return_status  := l_return_status;
8084             x_error_message  := csi_t_gen_utility_pvt.dump_error_stack;
8085           END IF;
8086 
8087         ELSE
8088 
8089           l_instance_rec.inventory_item_id        := inv_rec.item_id;
8090           l_instance_rec.inventory_revision       := inv_rec.revision;
8091           l_instance_rec.vld_organization_id      := inv_rec.xfer_organization_id;
8092           l_instance_rec.instance_usage_code      := 'IN_TRANSIT';
8093           l_instance_rec.location_type_code       := 'IN_TRANSIT';
8094           l_instance_rec.location_id              := l_intransit_location_id;
8095           l_instance_rec.active_end_date          := null;
8096           l_instance_rec.inv_organization_id      := null;
8097           l_instance_rec.inv_subinventory_name    := null;
8098           l_instance_rec.inv_locator_id           := null;
8099           l_instance_rec.lot_number               := null;
8100 
8101           l_parties_tbl(1).instance_party_id      := fnd_api.g_miss_num;
8102           l_parties_tbl(1).instance_id            := fnd_api.g_miss_num;
8103           l_parties_tbl(1).party_source_table     := 'HZ_PARTIES';
8104           l_parties_tbl(1).party_id               := l_internal_party_id;
8105           l_parties_tbl(1).relationship_type_code := 'OWNER';
8106           l_parties_tbl(1).contact_flag           := 'N';
8107           l_parties_tbl(1).object_version_number  := 1.0;
8108 
8109           create_instance(
8110             p_txn_rec        => l_txn_rec,
8111             p_instance_rec   => l_instance_rec,
8112             p_parties_tbl    => l_parties_tbl,
8113             p_pty_accts_tbl  => l_pty_accts_tbl,
8114             x_return_status  => l_return_status,
8115             x_error_message  => l_error_message);
8116         END IF;
8117 
8118         IF l_return_status <> fnd_api.g_ret_sts_success THEN
8119           x_return_status  := l_return_status;
8120           x_error_message  := l_error_message;
8121         END IF;
8122 
8123       END IF;
8124 
8125     END LOOP;
8126 
8127   END fix_isoreceipt;
8128 
8129   PROCEDURE fix_intransitship(
8130     p_diag_txn_rec  IN  diag_txn_rec,
8131     x_return_status OUT nocopy varchar2,
8132     x_error_message OUT nocopy varchar2)
8133   IS
8134 
8135     CURSOR stage_cur(p_mtl_txn_id in number, p_lot_code in number) IS
8136       SELECT mmt.inventory_item_id      item_id,
8137              mmt.organization_id        organization_id,
8138              mmt.subinventory_code      subinv_code,
8139              mmt.locator_id             locator_id,
8140              mmt.revision               revision,
8141              to_char(null)              lot_number,
8142              abs(mmt.primary_quantity)  quantity,
8143              mmt.transaction_date       mtl_txn_date,
8144              mmt.transaction_id         mtl_txn_id,
8145              mmt.trx_source_line_id     trx_source_line_id
8146       FROM   mtl_material_transactions mmt
8147       WHERE  mmt.transaction_id = p_mtl_txn_id
8148       AND    p_lot_code = 1
8149       UNION
8150       SELECT mmt.inventory_item_id      item_id,
8151              mmt.organization_id        organization_id,
8152              mmt.subinventory_code      subinv_code,
8153              mmt.locator_id             locator_id,
8154              mmt.revision               revision,
8155              mtln.lot_number            lot_number,
8156              abs(mtln.primary_quantity) quantity,
8157              mmt.transaction_date       mtl_txn_date,
8158              mmt.transaction_id         mtl_txn_id,
8159              mmt.trx_source_line_id     trx_source_line_id
8160       FROM   mtl_material_transactions   mmt,
8161              mtl_transaction_lot_numbers mtln
8162       WHERE  mmt.transaction_id  = p_mtl_txn_id
8163       AND    mtln.transaction_id = mmt.transaction_id
8164       AND    p_lot_code <> 1;
8165 
8166     l_txn_rec                csi_datastructures_pub.transaction_rec;
8167 
8168     l_instance_rec           csi_datastructures_pub.instance_rec;
8169     l_parties_tbl            csi_datastructures_pub.party_tbl;
8170     l_pty_accts_tbl          csi_datastructures_pub.party_account_tbl;
8171     l_org_units_tbl          csi_datastructures_pub.organization_units_tbl;
8172     l_ea_values_tbl          csi_datastructures_pub.extend_attrib_values_tbl;
8173     l_pricing_tbl            csi_datastructures_pub.pricing_attribs_tbl;
8174     l_assets_tbl             csi_datastructures_pub.instance_asset_tbl;
8175     l_instance_ids_list      csi_datastructures_pub.id_tbl;
8176 
8177     l_internal_party_id      number;
8178     l_intransit_location_id  number;
8179 
8180     l_src_serial_code        number;
8181     l_src_lot_code           number;
8182     l_primary_uom_code       varchar2(8);
8183 
8184     l_dest_serial_code       number;
8185     l_dest_lot_code          number;
8186 
8187     l_instance_id            number;
8188     l_quantity               number;
8189     l_object_version_number  number;
8190 
8191     l_return_status          varchar2(1) := fnd_api.g_ret_sts_success;
8192     l_error_message          varchar2(2000);
8193     l_msg_data               varchar2(2000);
8194     l_msg_count              number;
8195 
8196   BEGIN
8197 
8198     x_return_status := fnd_api.g_ret_sts_success;
8199 
8200     IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
8201        csi_gen_utility_pvt.populate_install_param_rec;
8202     END IF;
8203     --
8204     l_internal_party_id := csi_datastructures_pub.g_install_param_rec.internal_party_id;
8205     l_intransit_location_id := csi_datastructures_pub.g_install_param_rec.in_transit_location_id;
8206     --
8207     FOR inv_rec IN inv_cur (p_diag_txn_rec.mtl_txn_id)
8208     LOOP
8209 
8210       SELECT serial_number_control_code,
8211              lot_control_code,
8212              primary_uom_code
8213       INTO   l_src_serial_code,
8214              l_src_lot_code,
8215              l_primary_uom_code
8216       FROM   mtl_system_items
8217       WHERE  inventory_item_id = inv_rec.item_id
8218       AND    organization_id   = inv_rec.organization_id;
8219 
8220       SELECT serial_number_control_code,
8221              lot_control_code
8222       INTO   l_dest_serial_code,
8223              l_dest_lot_code
8224       FROM   mtl_system_items
8225       WHERE  inventory_item_id = inv_rec.item_id
8226       AND    organization_id   = inv_rec.xfer_organization_id;
8227 
8228       l_txn_rec.transaction_id                := fnd_api.g_miss_num;
8229       l_txn_rec.transaction_type_id           := correction_txn_type_id;
8230       l_txn_rec.source_header_ref             := 'DATAFIX';
8231       l_txn_rec.source_line_ref               := p_diag_txn_rec.process_code;
8232       l_txn_rec.source_line_ref_id            := inv_rec.trx_source_line_id;
8233       l_txn_rec.source_transaction_date       := inv_rec.mtl_txn_date;
8234       l_txn_rec.transaction_date              := inv_rec.mtl_txn_date;
8235 
8236       IF l_src_serial_code in (2, 5) THEN
8237 
8238         l_instance_rec.instance_id              := nvl(p_diag_txn_rec.inst_id,fnd_api.g_miss_num);
8239         l_instance_rec.location_type_code       := 'INVENTORY';
8240         l_instance_rec.instance_usage_code      := 'IN_INVENTORY';
8241         l_instance_rec.inventory_item_id        := inv_rec.item_id;
8242         l_instance_rec.serial_number            := p_diag_txn_rec.serial_number;
8243         l_instance_rec.inv_organization_id      := inv_rec.organization_id;
8244         l_instance_rec.quantity                 := 1;
8245         l_instance_rec.inv_subinventory_name    := inv_rec.subinv_code;
8246         l_instance_rec.inv_locator_id           := inv_rec.locator_id;
8247 
8248         get_lot_number(
8249           p_lot_code        => p_diag_txn_rec.lot_code,
8250           p_mtl_txn_id      => p_diag_txn_rec.mtl_txn_id,
8251           p_serial_number   => p_diag_txn_rec.serial_number,
8252           x_lot_number      => l_instance_rec.lot_number);
8253 
8254         l_instance_rec.inventory_revision       := inv_rec.revision;
8255         l_instance_rec.vld_organization_id      := inv_rec.organization_id;
8256         l_instance_rec.object_version_number    := 1.0;
8257 
8258         l_txn_rec.inv_material_transaction_id  := p_diag_txn_rec.mtl_txn_id;
8259         IF p_diag_txn_rec.inst_id is not null THEN
8260           update_instance(
8261             p_txn_rec        => l_txn_rec,
8262             p_instance_rec   => l_instance_rec,
8263             p_parties_tbl    => l_parties_tbl,
8264             p_pty_accts_tbl  => l_pty_accts_tbl,
8265             x_return_status  => l_return_status,
8266             x_error_message  => l_error_message);
8267         ELSE
8268 
8269           l_parties_tbl(1).instance_party_id      := fnd_api.g_miss_num;
8270           l_parties_tbl(1).party_source_table     := 'HZ_PARTIES';
8271           l_parties_tbl(1).party_id               := l_internal_party_id;
8272           l_parties_tbl(1).relationship_type_code := 'OWNER';
8273           l_parties_tbl(1).contact_flag           := 'N';
8274           l_parties_tbl(1).object_version_number  := 1.0;
8275 
8276           create_instance(
8277             p_txn_rec        => l_txn_rec,
8278             p_instance_rec   => l_instance_rec,
8279             p_parties_tbl    => l_parties_tbl,
8280             p_pty_accts_tbl  => l_pty_accts_tbl,
8281             x_return_status  => l_return_status,
8282             x_error_message  => l_error_message);
8283         END IF;
8284 
8285         IF l_return_status <> fnd_api.g_ret_sts_success THEN
8286           x_return_status  := l_return_status;
8287           x_error_message  := l_error_message;
8288         END IF;
8289       ELSE
8290 
8291         -- handle source
8292         FOR stage_rec IN stage_cur (inv_rec.mtl_txn_id, l_src_lot_code)
8293         LOOP
8294 
8295           BEGIN
8296 
8297             init_plsql_tables(
8298               px_instance_rec  => l_instance_rec,
8299               px_parties_tbl   => l_parties_tbl,
8300               px_pty_accts_tbl => l_pty_accts_tbl,
8301               px_org_units_tbl => l_org_units_tbl,
8302               px_ea_values_tbl => l_ea_values_tbl,
8303               px_pricing_tbl   => l_pricing_tbl,
8304               px_assets_tbl    => l_assets_tbl);
8305 
8306             BEGIN
8307 
8308               SELECT instance_id,
8309                      quantity,
8310                      object_version_number
8311               INTO   l_instance_id,
8312                      l_quantity,
8313                      l_object_version_number
8314               FROM   csi_item_instances
8315               WHERE  location_type_code               = 'INVENTORY'
8316               AND    instance_usage_code              = 'IN_INVENTORY'
8317               AND    inventory_item_id                = stage_rec.item_id
8318               AND    inv_organization_id              = stage_rec.organization_id
8319               AND    inv_subinventory_name            = stage_rec.subinv_code
8320               AND    nvl(inv_locator_id,-9999)        = nvl(stage_rec.locator_id,-9999)
8321               AND    nvl(lot_number,'$$##$$')         = nvl(stage_rec.lot_number,'$$##$$')
8322               AND    nvl(inventory_revision,'$$##$$') = nvl(stage_rec.revision,'$$##$$')
8323               AND    serial_number is null;
8324 
8325             EXCEPTION
8326               WHEN no_data_found THEN
8327                 l_instance_id := fnd_api.g_miss_num;
8328               WHEN too_many_rows THEN
8329                 stack_message('Too many inventory instances for this non srl item.');
8330                 raise fnd_api.g_exc_error;
8331             END;
8332 
8333             IF nvl(l_instance_id,fnd_api.g_miss_num) <> fnd_api.g_miss_num THEN
8334 
8335               l_instance_rec.instance_id           := l_instance_id;
8336               l_instance_rec.quantity              := l_quantity + stage_rec.quantity;
8337               l_instance_rec.object_version_number := l_object_version_number;
8338               l_instance_rec.active_end_date       := null;
8339               l_instance_rec.instance_status_id    := 3;
8340 
8341               csi_item_instance_pub.update_item_instance(
8342                 p_api_version           => 1.0,
8343                 p_commit                => fnd_api.g_false,
8344                 p_init_msg_list         => fnd_api.g_true,
8345                 p_validation_level      => fnd_api.g_valid_level_full,
8346                 p_instance_rec          => l_instance_rec,
8347                 p_party_tbl             => l_parties_tbl,
8348                 p_account_tbl           => l_pty_accts_tbl,
8349                 p_org_assignments_tbl   => l_org_units_tbl,
8350                 p_ext_attrib_values_tbl => l_ea_values_tbl,
8351                 p_pricing_attrib_tbl    => l_pricing_tbl,
8352                 p_asset_assignment_tbl  => l_assets_tbl,
8353                 p_txn_rec               => l_txn_rec,
8354                 x_instance_id_lst       => l_instance_ids_list,
8355                 x_return_status         => l_return_status,
8356                 x_msg_count             => l_msg_count,
8357                 x_msg_data              => l_msg_data);
8358 
8359               log('  update_item_instance:nsrl: '||l_return_status);
8360 
8361               -- For Bug 4057183
8362               -- IF l_return_status <> fnd_api.g_ret_sts_success THEN
8363               IF l_return_status not in (fnd_api.g_ret_sts_success,'W') THEN
8364                 raise fnd_api.g_exc_error;
8365               END IF;
8366 
8367             ELSE
8368 
8369               l_instance_rec.instance_id              := fnd_api.g_miss_num;
8370               l_instance_rec.location_type_code       := 'INVENTORY';
8371               l_instance_rec.instance_usage_code      := 'IN_INVENTORY';
8372               l_instance_rec.inventory_item_id        := stage_rec.item_id;
8373               l_instance_rec.inv_organization_id      := stage_rec.organization_id;
8374               l_instance_rec.quantity                 := stage_rec.quantity;
8375               l_instance_rec.unit_of_measure          := l_primary_uom_code;
8376               l_instance_rec.inv_subinventory_name    := stage_rec.subinv_code;
8377               l_instance_rec.inv_locator_id           := stage_rec.locator_id;
8378               l_instance_rec.lot_number               := stage_rec.lot_number;
8379               l_instance_rec.inventory_revision       := stage_rec.revision;
8380               l_instance_rec.vld_organization_id      := stage_rec.organization_id;
8381               l_instance_rec.object_version_number    := 1.0;
8382 
8383               SELECT nvl(mssi.location_id, haou.location_id)
8384               INTO   l_instance_rec.location_id
8385               FROM   mtl_secondary_inventories mssi,
8386                      hr_all_organization_units haou
8387               WHERE  mssi.organization_id          = l_instance_rec.inv_organization_id
8388               AND    mssi.secondary_inventory_name = l_instance_rec.inv_subinventory_name
8389               AND    haou.organization_id          = mssi.organization_id;
8390 
8391               l_parties_tbl(1).instance_party_id      := fnd_api.g_miss_num;
8392               l_parties_tbl(1).party_source_table     := 'HZ_PARTIES';
8393               l_parties_tbl(1).party_id               := l_internal_party_id;
8394               l_parties_tbl(1).relationship_type_code := 'OWNER';
8395               l_parties_tbl(1).contact_flag           := 'N';
8396               l_parties_tbl(1).object_version_number  := 1.0;
8397 
8398               csi_item_instance_pub.create_item_instance(
8399                 p_api_version           => 1.0,
8400                 p_commit                => fnd_api.g_false,
8401                 p_init_msg_list         => fnd_api.g_true,
8402                 p_validation_level      => fnd_api.g_valid_level_full,
8403                 p_instance_rec          => l_instance_rec,
8404                 p_party_tbl             => l_parties_tbl,
8405                 p_account_tbl           => l_pty_accts_tbl,
8406                 p_org_assignments_tbl   => l_org_units_tbl,
8407                 p_ext_attrib_values_tbl => l_ea_values_tbl,
8408                 p_pricing_attrib_tbl    => l_pricing_tbl,
8409                 p_asset_assignment_tbl  => l_assets_tbl,
8410                 p_txn_rec               => l_txn_rec,
8411                 x_return_status         => l_return_status,
8412                 x_msg_count             => l_msg_count,
8413                 x_msg_data              => l_msg_data );
8414 
8415               log('  create_item_instance:nsrl: '||l_return_status);
8416 
8417               -- For Bug 4057183
8418               -- IF l_return_status <> fnd_api.g_ret_sts_success THEN
8419               IF l_return_status not in (fnd_api.g_ret_sts_success,'W') THEN
8420                 raise fnd_api.g_exc_error;
8421               END IF;
8422 
8423             END IF;
8424           EXCEPTION
8425             WHEN fnd_api.g_exc_error THEN
8426               x_return_status := fnd_api.g_ret_sts_error;
8427               x_error_message := csi_t_gen_utility_pvt.dump_error_stack;
8428               log('  error: '||x_error_message);
8429           END;
8430         END LOOP;
8431 
8432         -- handle soi destination instance
8433         IF nvl(p_diag_txn_rec.inst_id,fnd_api.g_miss_num) <> fnd_api.g_miss_num THEN
8434           IF l_src_serial_code = 6 THEN
8435 
8436             init_plsql_tables(
8437               px_instance_rec  => l_instance_rec,
8438               px_parties_tbl   => l_parties_tbl,
8439               px_pty_accts_tbl => l_pty_accts_tbl,
8440               px_org_units_tbl => l_org_units_tbl,
8441               px_ea_values_tbl => l_ea_values_tbl,
8442               px_pricing_tbl   => l_pricing_tbl,
8443               px_assets_tbl    => l_assets_tbl);
8444 
8445             l_instance_rec.instance_id          := p_diag_txn_rec.inst_id;
8446             l_instance_rec.location_type_code   := 'INVENTORY';
8447             l_instance_rec.inv_organization_id  := inv_rec.organization_id;
8448             l_instance_rec.inv_subinventory_name:= inv_rec.subinv_code;
8449 
8450             SELECT nvl(mssi.location_id, haou.location_id)
8451             INTO   l_instance_rec.location_id
8452             FROM   mtl_secondary_inventories mssi,
8453                    hr_all_organization_units haou
8454             WHERE  mssi.organization_id          = l_instance_rec.inv_organization_id
8455             AND    mssi.secondary_inventory_name = l_instance_rec.inv_subinventory_name
8456             AND    haou.organization_id          = mssi.organization_id;
8457 
8458             l_instance_rec.instance_usage_code  := 'RETURNED';
8459             l_instance_rec.inventory_item_id    := inv_rec.item_id;
8460             l_instance_rec.serial_number        := p_diag_txn_rec.serial_number;
8461             l_instance_rec.mfg_serial_number_flag := 'Y';
8462             l_instance_rec.quantity             := 1;
8463             l_instance_rec.inv_locator_id       := inv_rec.locator_id;
8464             l_instance_rec.creation_complete_flag := 'N';
8465 
8466             get_lot_number(
8467               p_lot_code        => p_diag_txn_rec.lot_code,
8468               p_mtl_txn_id      => p_diag_txn_rec.mtl_txn_id,
8469               p_serial_number   => p_diag_txn_rec.serial_number,
8470               x_lot_number      => l_instance_rec.lot_number);
8471 
8472             l_instance_rec.inventory_revision   := inv_rec.revision;
8473             l_instance_rec.vld_organization_id  := inv_rec.organization_id;
8474 
8475             l_txn_rec.inv_material_transaction_id  := p_diag_txn_rec.mtl_txn_id;
8476 
8477             update_instance(
8478               p_txn_rec        => l_txn_rec,
8479               p_instance_rec   => l_instance_rec,
8480               p_parties_tbl    => l_parties_tbl,
8481               p_pty_accts_tbl  => l_pty_accts_tbl,
8482               x_return_status  => l_return_status,
8483               x_error_message  => l_error_message);
8484 
8485             IF l_return_status <> fnd_api.g_ret_sts_success THEN
8486               x_return_status := l_return_status;
8487               x_error_message := l_error_message;
8488             END IF;
8489 
8490           END IF;
8491         END IF;
8492 
8493       END IF;
8494     END LOOP;
8495 
8496   END fix_intransitship;
8497 
8498   PROCEDURE fix_all(
8499     p_diag_txn_rec IN diag_txn_rec)
8500   IS
8501     l_return_status    varchar2(1) := fnd_api.g_ret_sts_success;
8502     l_error_message    varchar2(2000);
8503     l_fixed            boolean := FALSE;
8504   BEGIN
8505 
8506     log('  '||p_diag_txn_rec.serial_number||
8507         '  '||p_diag_txn_rec.mtl_txn_id||
8508         '  '||p_diag_txn_rec.source_type||
8509         '  '||p_diag_txn_rec.process_code||
8510         '  '||p_diag_txn_rec.inst_id);
8511 
8512     IF p_diag_txn_rec.process_code = 'SHIP' THEN
8513 
8514       fix_shipment(
8515         p_diag_txn_rec  => p_diag_txn_rec,
8516         x_return_status => l_return_status,
8517         x_error_message => l_error_message);
8518 
8519     END IF;
8520 
8521     IF p_diag_txn_rec.process_code = 'RMA' THEN
8522       fix_rma(
8523         p_diag_txn_rec  => p_diag_txn_rec,
8524         x_return_status => l_return_status,
8525         x_error_message => l_error_message);
8526     END IF;
8527 
8528     IF p_diag_txn_rec.process_code = 'WIPISSUE' THEN
8529       fix_wipissue(
8530         p_diag_txn_rec  => p_diag_txn_rec,
8531         x_return_status => l_return_status,
8532         x_error_message => l_error_message);
8533     END IF;
8534 
8535     IF p_diag_txn_rec.process_code = 'WIPRETURN' THEN
8536       fix_wipreturn(
8537         p_diag_txn_rec  => p_diag_txn_rec,
8538         x_return_status => l_return_status,
8539         x_error_message => l_error_message);
8540     END IF;
8541 
8542     IF p_diag_txn_rec.process_code = 'WIPCOMPL' THEN
8543       fix_wipcompletion(
8544         p_diag_txn_rec  => p_diag_txn_rec,
8545         x_return_status => l_return_status,
8546         x_error_message => l_error_message);
8547     END IF;
8548 
8549     IF p_diag_txn_rec.process_code = 'MISCISSUE' THEN
8550       fix_miscissue(
8551         p_diag_txn_rec  => p_diag_txn_rec,
8552         x_return_status => l_return_status,
8553         x_error_message => l_error_message);
8554     END IF;
8555 
8556     IF p_diag_txn_rec.process_code = 'MISCRCPT' THEN
8557       fix_miscreceipt(
8558         p_diag_txn_rec  => p_diag_txn_rec,
8559         x_return_status => l_return_status,
8560         x_error_message => l_error_message);
8561     END IF;
8562 
8563     IF p_diag_txn_rec.process_code = 'SIXFER' THEN
8564       fix_sixfer(
8565         p_diag_txn_rec  => p_diag_txn_rec,
8566         x_return_status => l_return_status,
8567         x_error_message => l_error_message);
8568     END IF;
8569 
8570     IF p_diag_txn_rec.process_code = 'PROJRCPT' THEN
8571       fix_projreceipt(
8572         p_diag_txn_rec  => p_diag_txn_rec,
8573         x_return_status => l_return_status,
8574         x_error_message => l_error_message);
8575     END IF;
8576 
8577     IF p_diag_txn_rec.process_code = 'IORGRCPT' THEN
8578       fix_interorgreceipt(
8579         p_diag_txn_rec  => p_diag_txn_rec,
8580         x_return_status => l_return_status,
8581         x_error_message => l_error_message);
8582     END IF;
8583 
8584     IF p_diag_txn_rec.process_code = 'ISORCPT' THEN
8585       fix_isoreceipt(
8586         p_diag_txn_rec  => p_diag_txn_rec,
8587         x_return_status => l_return_status,
8588         x_error_message => l_error_message);
8589     END IF;
8590 
8591     IF p_diag_txn_rec.process_code = 'INTRSHIP' THEN
8592       fix_intransitship(
8593         p_diag_txn_rec  => p_diag_txn_rec,
8594         x_return_status => l_return_status,
8595         x_error_message => l_error_message);
8596     END IF;
8597 
8598     IF l_return_status = fnd_api.g_ret_sts_success THEN
8599       UPDATE csi_diagnostics_temp
8600       SET    process_flag = 'P'
8601       WHERE  diag_seq_id  = p_diag_txn_rec.diag_seq_id;
8602 
8603       UPDATE csi_txn_errors
8604       SET    processed_flag    = 'R',
8605              last_update_date  = sysdate,
8606              last_update_login = fnd_global.login_id,
8607              last_updated_by   = fnd_global.user_id
8608       WHERE  inv_material_transaction_id = p_diag_txn_rec.mtl_txn_id
8609       AND    processed_flag              = 'E';
8610     ELSE
8611       UPDATE csi_diagnostics_temp
8612       SET    process_flag = 'E',
8613              temporary_message = l_error_message
8614       WHERE  diag_seq_id  = p_diag_txn_rec.diag_seq_id;
8615     END IF;
8616 
8617   END fix_all;
8618 
8619   PROCEDURE fix_srldata is
8620 
8621     CURSOR process_cur is
8622       SELECT cdt.diag_seq_id,
8623              nvl(cdt.process_code,'NONE') process_code,
8624              cdt.mtl_txn_id,
8625              cdt.mtl_txn_date mtl_txn_date,
8626              cdt.serial_number,
8627              cdt.inventory_item_id,
8628              cdt.instance_id,
8629              cdt.csi_txn_id,
8630              cdt.csi_txn_type_id,
8631              cdt.wip_job_id,
8632              nvl(cdt.create_flag, 'N')  create_flag,
8633              nvl(cdt.expire_flag, 'N')  expire_flag,
8634              cdt.serial_control_code,
8635              cdt.lot_control_code,
8636              cdt.source_type,
8637              cdt.internal_party_id
8638       FROM   csi_diagnostics_temp cdt
8639       WHERE  nvl(cdt.process_flag ,'N') = 'M'; -- marked FOR processing
8640 
8641     CURSOR reprocess_cur IS
8642       SELECT distinct mtl_txn_id
8643       FROM   csi_diagnostics_temp
8644       WHERE  process_flag = 'R';
8645 
8646     l_diag_txn_rec         diag_txn_rec;
8647 
8648   BEGIN
8649 
8650     -- stage the non serial instance quantity and update the returned serial instance
8651     stage_soiship_instances;
8652 
8653     log(date_time_stamp||'  begin fix_srldata');
8654 
8655     FOR process_rec in process_cur
8656     LOOP
8657 
8658       IF process_rec.process_code <> 'SOISHIP' THEN
8659 
8660         l_diag_txn_rec.diag_seq_id       := process_rec.diag_seq_id;
8661         l_diag_txn_rec.process_code      := process_rec.process_code;
8662         l_diag_txn_rec.mtl_txn_id        := process_rec.mtl_txn_id;
8663         l_diag_txn_rec.mtl_txn_date      := process_rec.mtl_txn_date;
8664         l_diag_txn_rec.serial_number     := process_rec.serial_number;
8665         l_diag_txn_rec.item_id           := process_rec.inventory_item_id;
8666         l_diag_txn_rec.inst_id           := process_rec.instance_id;
8667         l_diag_txn_rec.csi_txn_id        := process_rec.csi_txn_id;
8668         l_diag_txn_rec.csi_txn_type_id   := process_rec.csi_txn_type_id;
8669         l_diag_txn_rec.wip_job_id        := process_rec.wip_job_id;
8670         l_diag_txn_rec.create_flag       := process_rec.create_flag;
8671         l_diag_txn_rec.expire_flag       := process_rec.expire_flag;
8672         l_diag_txn_rec.serial_code       := process_rec.serial_control_code;
8673         l_diag_txn_rec.lot_code          := process_rec.lot_control_code;
8674         l_diag_txn_rec.source_type       := process_rec.source_type;
8675         l_diag_txn_rec.internal_party_id := process_rec.internal_party_id;
8676 
8677         fix_all(
8678           p_diag_txn_rec => l_diag_txn_rec);
8679 
8680       END IF;
8681 
8682     END LOOP;
8683 
8684     FOR reprocess_rec in reprocess_cur
8685     LOOP
8686       update csi_txn_errors
8687       SET    processed_flag              = 'R',
8688              last_update_date            = sysdate,
8689              last_update_login           = fnd_global.login_id,
8690              last_updated_by             = fnd_global.user_id
8691       WHERE  inv_material_transaction_id = reprocess_rec.mtl_txn_id
8692       AND    processed_flag              = 'E';
8693     END LOOP;
8694 
8695     knock_the_rest;
8696 
8697     log(date_time_stamp||'  end fix_srldata');
8698 
8699   END fix_srldata;
8700 
8701   PROCEDURE get_rma_owner(
8702     p_serial_number     in  varchar2,
8703     p_inventory_item_id in  number,
8704     p_organization_id   in  number,
8705     x_change_owner_flag out nocopy varchar2,
8706     x_owner_party_id    out nocopy number,
8707     x_owner_account_id  out nocopy number)
8708   IS
8709 
8710     CURSOR rma_txn_cur(
8711       p_serial      in varchar2,
8712       p_item_id     in number,
8713       p_freeze_date in date)
8714     IS
8715       SELECT mmt.creation_date         mtl_creation_date,
8716              mut.transaction_id        mtl_txn_id,
8717              mut.transaction_date      mtl_txn_date,
8718              mmt.trx_source_line_id    rma_line_id
8719       FROM   mtl_unit_transactions     mut,
8720              mtl_material_transactions mmt
8721       WHERE  mut.serial_number         = p_serial
8722       AND    mut.inventory_item_id     = p_item_id
8723       AND    mut.transaction_date      > p_freeze_date
8724       AND    mmt.transaction_id        = mut.transaction_id
8725       AND    mmt.transaction_type_id   = 15
8726       UNION
8727       SELECT mmt.creation_date         mtl_creation_date,
8728              mtln.transaction_id       mtl_txn_id,
8729              mtln.transaction_date     mtl_txn_date,
8730              mmt.trx_source_line_id    rma_line_id
8731       FROM   mtl_unit_transactions       mut,
8732              mtl_transaction_lot_numbers mtln,
8733              mtl_material_transactions   mmt
8734       WHERE  mut.serial_number          = p_serial
8735       AND    mut.inventory_item_id      = p_item_id
8736       AND    mtln.serial_transaction_id = mut.transaction_id
8737       AND    mtln.transaction_date      > p_freeze_date
8738       AND    mmt.transaction_id         = mtln.transaction_id
8739       AND    mmt.transaction_type_id    = 15
8740       ORDER by 1 desc, 2 desc;
8741 
8742    CURSOR tld_cur(p_rma_line_id in number) is
8743      SELECT ctld.sub_type_id
8744      FROM   csi_t_transaction_lines ctl,
8745             csi_t_txn_line_details ctld
8746      WHERE  ctl.source_transaction_table = 'OE_ORDER_LINES_ALL'
8747      AND    ctl.source_transaction_type_id  = 53
8748      AND    ctl.source_transaction_id    = p_rma_line_id
8749      AND    ctld.transaction_line_id     = ctl.transaction_line_id
8750      AND    ctld.source_transaction_flag = 'Y';
8751 
8752     l_dflt_sub_type_id   number;
8753     l_rma_line_id        number;
8754     l_sub_type_id        number;
8755     l_owner_party_id     number;
8756     l_owner_account_id   number;
8757     l_change_owner_flag  varchar2(1) := 'Y';
8758 
8759     l_freeze_date        date;
8760 
8761   BEGIN
8762 
8763     SELECT sub_type_id
8764     INTO   l_dflt_sub_type_id
8765     FROM   csi_txn_sub_types
8766     WHERE  transaction_type_id = 53
8767     AND    default_flag = 'Y';
8768     --
8769     IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
8770        csi_gen_utility_pvt.populate_install_param_rec;
8771     END IF;
8772     --
8773     l_freeze_date := csi_datastructures_pub.g_install_param_rec.freeze_date;
8774 
8775     l_change_owner_flag := 'Y';
8776     l_rma_line_id       := null;
8777     l_sub_type_id       := null;
8778     l_owner_party_id    := null;
8779     l_owner_account_id  := null;
8780 
8781     FOR rma_txn_rec in rma_txn_cur (
8782       p_serial      => p_serial_number,
8783       p_item_id     => p_inventory_item_id,
8784       p_freeze_date => l_freeze_date)
8785     LOOP
8786       l_rma_line_id  := rma_txn_rec.rma_line_id;
8787       exit;
8788     END LOOP;
8789 
8790     IF l_rma_line_id is not null THEN
8791 
8792       FOR tld_rec in tld_cur(l_rma_line_id)
8793       LOOP
8794         l_sub_type_id := tld_rec.sub_type_id;
8795         exit;
8796       END LOOP;
8797 
8798       IF l_sub_type_id is not null THEN
8799         null;
8800       ELSE
8801         l_sub_type_id := l_dflt_sub_type_id;
8802       END IF;
8803 
8804       SELECT nvl(src_change_owner, 'N')
8805       INTO   l_change_owner_flag
8806       FROM   csi_txn_sub_types
8807       WHERE  transaction_type_id = 53
8808       AND    sub_type_id         = l_sub_type_id;
8809 
8810 
8811       IF l_change_owner_flag = 'N' THEN
8812 
8813         SELECT nvl(oel.sold_to_org_id, oeh.sold_to_org_id)
8814         INTO   l_owner_account_id
8815         FROM   oe_order_lines_all oel,
8816                oe_order_headers_all oeh
8817         WHERE  oel.line_id = l_rma_line_id
8818         AND    oeh.header_id = oel.header_id;
8819 
8820         SELECT party_id
8821         INTO   l_owner_party_id
8822         FROM   hz_cust_accounts
8823         WHERE  cust_account_id = l_owner_account_id;
8824 
8825       END IF;
8826 
8827     END IF;
8828 
8829     x_change_owner_flag := l_change_owner_flag;
8830     x_owner_party_id    := l_owner_party_id;
8831     x_owner_account_id  := l_owner_account_id;
8832 
8833   END get_rma_owner;
8834 
8835 
8836   /* routine to sync inventory and instances */
8837   PROCEDURE sync_inv_serials IS
8838 
8839     CURSOR inv_srl_cur IS
8840       SELECT msn.serial_number              serial_number,
8841              msn.inventory_item_id          inventory_item_id,
8842              msn.current_organization_id    organization_id,
8843              msn.revision                   revision,
8844              msn.current_subinventory_code  subinventory_code,
8845              msn.current_locator_id         locator_id,
8846              msn.lot_number                 lot_number,
8847              msi.primary_uom_code           uom_code,
8848              msi.serial_number_control_code serial_code,
8849              msi.lot_control_code           lot_code
8850       FROM   mtl_system_items   msi,
8851              mtl_serial_numbers msn
8852       WHERE  msi.inventory_item_id = msn.inventory_item_id
8853       AND    msi.organization_id   = msn.current_organization_id
8854       AND    msi.serial_number_control_code in (2,5)
8855       AND    msn.current_status    = 3
8856       AND    EXISTS (
8857                SELECT '1'
8858                FROM  mtl_parameters   mp,
8859                      mtl_system_items msi_mast
8860                WHERE mp.organization_id         = msi.organization_id
8861                AND   msi_mast.inventory_item_id = msi.inventory_item_id
8862                AND   msi_mast.organization_id   = mp.master_organization_id
8863                AND   nvl(msi_mast.comms_nl_trackable_flag,'N') = 'Y')
8864       AND    EXISTS (
8865                SELECT '1'
8866                FROM  mtl_onhand_quantities moq
8867                WHERE moq.inventory_item_id     = msn.inventory_item_id
8868                AND   moq.organization_id       = msn.current_organization_id
8869                AND   moq.subinventory_code     = msn.current_subinventory_code
8870                AND   nvl(moq.locator_id,-999)  = nvl(msn.current_locator_id,-999)
8871                AND   nvl(moq.lot_number,'$#$') = nvl(msn.lot_number,'$#$')
8872                AND   nvl(moq.revision,'$#$')   = nvl(msn.revision,'$#$') );
8873 
8874     l_release            varchar2(80);
8875 
8876     l_internal_party_id  number;
8877     l_instance           varchar2(30);
8878     l_instance_found     boolean := TRUE;
8879     l_not_the_same       boolean := TRUE;
8880 
8881     l_instance_rec       csi_datastructures_pub.instance_rec;
8882     l_parties_tbl        csi_datastructures_pub.party_tbl;
8883     l_pty_accts_tbl      csi_datastructures_pub.party_account_tbl;
8884     l_org_units_tbl      csi_datastructures_pub.organization_units_tbl;
8885     l_ea_values_tbl      csi_datastructures_pub.extend_attrib_values_tbl;
8886     l_pricing_tbl        csi_datastructures_pub.pricing_attribs_tbl;
8887     l_assets_tbl         csi_datastructures_pub.instance_asset_tbl;
8888     l_instance_ids_list  csi_datastructures_pub.id_tbl;
8889     l_txn_rec            csi_datastructures_pub.transaction_rec;
8890     l_freeze_date        date;
8891     l_latest_txn         boolean := TRUE;
8892     l_pending_msg_found  varchar2(1);
8893     l_pending_err_found  varchar2(1);
8894 
8895     l_owner_party_id     number;
8896     l_owner_account_id   number;
8897     l_change_owner_flag  varchar2(1);
8898     l_txn_is_in_csi      varchar2(1);
8899     l_fs_found           varchar2(1);
8900 
8901     skip_serial          exception;
8902 
8903     l_error_message      varchar2(2000);
8904     l_msg_data           varchar2(2000);
8905     l_msg_count          number;
8906     l_return_status      varchar2(1);
8907 
8908     l_skip_error         varchar2(2000);
8909 
8910   BEGIN
8911 
8912     decode_queue;
8913 
8914     SELECT fnd_Profile.value('csi_upgrading_from_release')
8915     INTO   l_release
8916     FROM   sys.dual;
8917     --
8918     IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
8919        csi_gen_utility_pvt.populate_install_param_rec;
8920     END IF;
8921     --
8922     l_internal_party_id := csi_datastructures_pub.g_install_param_rec.internal_party_id;
8923     l_freeze_date := csi_datastructures_pub.g_install_param_rec.freeze_date;
8924 
8925     FOR srl_rec IN inv_srl_cur
8926     LOOP
8927 
8928       BEGIN
8929 
8930         init_plsql_tables(
8931           px_instance_rec  => l_instance_rec,
8932           px_parties_tbl   => l_parties_tbl,
8933           px_pty_accts_tbl => l_pty_accts_tbl,
8934           px_org_units_tbl => l_org_units_tbl,
8935           px_ea_values_tbl => l_ea_values_tbl,
8936           px_pricing_tbl   => l_pricing_tbl,
8937           px_assets_tbl    => l_assets_tbl);
8938 
8939         BEGIN
8940 
8941           SELECT 'Y'
8942           INTO   l_fs_found
8943           FROM   csi_ii_forward_sync_temp
8944           WHERE  inventory_item_id = srl_rec.inventory_item_id
8945           AND    serial_number     = srl_rec.serial_number
8946           AND    process_flag     <> 'P';
8947 
8948           l_skip_error := 'serial awaiting to be forward synched';
8949           RAISE skip_serial;
8950 
8951         EXCEPTION
8952           WHEN no_data_found THEN
8953             null;
8954         END;
8955 
8956         l_instance_rec.instance_id            := fnd_api.g_miss_num;
8957         l_instance_rec.inventory_item_id      := srl_rec.inventory_item_id;
8958         l_instance_rec.serial_number          := srl_rec.serial_number;
8959         l_instance_rec.lot_number             := srl_rec.lot_number;
8960         l_instance_rec.mfg_serial_number_flag := 'Y';
8961         l_instance_rec.quantity               := 1;
8962         l_instance_rec.unit_of_measure        := srl_rec.uom_code;
8963 
8964         l_instance_rec.vld_organization_id    := srl_rec.organization_id;
8965         l_instance_rec.inv_organization_id    := srl_rec.organization_id;
8966         l_instance_rec.inv_subinventory_name  := srl_rec.subinventory_code;
8967         l_instance_rec.inv_locator_id         := srl_rec.locator_id;
8968         l_instance_rec.inventory_revision     := srl_rec.revision;
8969 
8970         l_instance_rec.location_type_code     := 'INVENTORY';
8971         l_instance_rec.instance_usage_code    := 'IN_INVENTORY';
8972 
8973         -- get inv location id from the subinv/org definition
8974         BEGIN
8975           SELECT location_id
8976           INTO   l_instance_rec.location_id
8977           FROM   mtl_secondary_inventories
8978           WHERE  secondary_inventory_name = l_instance_rec.inv_subinventory_name
8979           AND    organization_id          = l_instance_rec.vld_organization_id;
8980 
8981           IF l_instance_rec.location_id is null THEN
8982             SELECT location_id
8983             INTO   l_instance_rec.location_id
8984             FROM   hr_all_organization_units
8985             WHERE  organization_id = l_instance_rec.vld_organization_id;
8986           END IF;
8987         END;
8988 
8989         BEGIN
8990           SELECT instance_id ,
8991                  object_version_number
8992           INTO   l_instance_rec.instance_id,
8993                  l_instance_rec.object_version_number
8994           FROM   csi_item_instances
8995           WHERE  inventory_item_id = srl_rec.inventory_item_id
8996           AND    serial_number     = srl_rec.serial_number;
8997 
8998           l_instance := to_char(l_instance_rec.instance_id);
8999           l_instance_found := TRUE;
9000         EXCEPTION
9001           WHEN no_data_found THEN
9002             l_instance := 'NONE';
9003             l_instance_found := FALSE;
9004           WHEN too_many_rows THEN
9005             l_instance_found := TRUE;
9006             l_skip_error := '  Too Many Instances for this serial number';
9007             raise skip_serial;
9008         END;
9009 
9010         IF l_instance_found THEN
9011           -- check if the instance inv location attributes are the same as the serial attribute
9012           l_not_the_same := not_the_same(l_instance_rec);
9013         END IF;
9014 
9015         -- fixable candidates  (no serial found or not the same)
9016         IF l_not_the_same OR NOT(l_instance_found) THEN
9017 
9018           log(fill(srl_rec.serial_number, 15)||
9019             fill(to_char(srl_rec.inventory_item_id), 9)||
9020             fill(to_char(srl_rec.organization_id), 9)||
9021             fill(to_char(srl_rec.serial_code), 2)||
9022             fill(to_char(srl_rec.lot_code), 2)||
9023             fill(l_instance,9));
9024 
9025           IF l_not_the_same AND l_instance_found THEN
9026             dump_diff(l_instance_rec);
9027           END IF;
9028 
9029           /* loop thru all the material transactions after freeze date and check if
9030              there is any pending in xnp_msgs.
9031 
9032              also check if there is any  errors in csi_txn_errors. if found then skip
9033              the serial.
9034           */
9035 
9036           l_latest_txn := TRUE;
9037 
9038           l_txn_rec.transaction_id              := fnd_api.g_miss_num;
9039           l_txn_rec.transaction_type_id         := correction_txn_type_id;
9040           l_txn_rec.source_header_ref           := 'DATAFIX';
9041           l_txn_rec.source_line_ref             := 'SRLSYNC';
9042           l_txn_rec.source_transaction_date     := sysdate;
9043           l_txn_rec.transaction_date            := sysdate;
9044 
9045           FOR all_txn_rec in all_txn_cur (
9046             p_serial_number => srl_rec.serial_number,
9047             p_item_id       => srl_rec.inventory_item_id)
9048           LOOP
9049 
9050             IF all_txn_rec.mtl_txn_date > l_freeze_date THEN
9051 
9052             IF csi_inv_trxs_pkg.valid_ib_txn(all_txn_rec.mtl_txn_id) THEN
9053 
9054               IF l_latest_txn THEN
9055 
9056                 log('  latest mtl_txn_id : '||all_txn_rec.mtl_txn_id);
9057 
9058                 l_txn_rec.source_transaction_date     := all_txn_rec.mtl_txn_date;
9059                 l_txn_rec.transaction_date            := all_txn_rec.mtl_txn_date;
9060 
9061                 BEGIN
9062                   SELECT 'X' INTO l_txn_is_in_csi
9063                   FROM   sys.dual
9064                   WHERE  exists (
9065                     SELECT 'Y' FROM csi_transactions
9066                     WHERE  inv_material_transaction_id = all_txn_rec.mtl_txn_id);
9067                   l_txn_rec.inv_material_transaction_id := null;
9068                 EXCEPTION
9069                   WHEN no_data_found THEN
9070                     l_txn_rec.inv_material_transaction_id := all_txn_rec.mtl_txn_id;
9071                 END;
9072 
9073                 l_latest_txn := FALSE;
9074 
9075               END IF;
9076 
9077               BEGIN
9078                 SELECT 'Y' INTO l_pending_msg_found
9079                 FROM   sys.dual
9080                 WHERE  exists (
9081                   SELECT '1'
9082                   FROM   csi_xnp_msgs_temp
9083                   WHERE  source_type = 'MTL_TRANSACTION_ID'
9084                   AND    source_id   = all_txn_rec.mtl_txn_id
9085                   AND    nvl(msg_status, 'READY') <> 'PROCESSED');
9086 
9087                 l_skip_error := '  Unprocessed Message in XNP_MSGS for MTL_TXN_ID: '||
9088                                  all_txn_rec.mtl_txn_id;
9089                 RAISE skip_serial;
9090 
9091               EXCEPTION
9092                 WHEN no_data_found THEN
9093                   null;
9094               END;
9095 
9096               BEGIN
9097                 IF all_txn_rec.mtl_action_id in (2,3,28) THEN
9098 
9099                   SELECT 'Y' INTO l_pending_err_found
9100                   FROM   sys.dual
9101                   WHERE  exists (
9102                     SELECT '1'
9103                     FROM   csi_txn_errors
9104                     WHERE  (inv_material_transaction_id = all_txn_rec.mtl_txn_id
9105                             OR
9106                             inv_material_transaction_id = all_txn_rec.mtl_txn_id)
9107                     AND    processed_flag in ('E', 'R'));
9108 
9109                 ELSE
9110 
9111                   SELECT 'Y' INTO l_pending_err_found
9112                   FROM sys.dual
9113                   WHERE exists (
9114                     SELECT '1'
9115                     FROM   csi_txn_errors
9116                     WHERE  inv_material_transaction_id = all_txn_rec.mtl_txn_id
9117                     AND    processed_flag in ('E', 'R'));
9118 
9119                 END IF;
9120                 l_skip_error := '  Pending Error in CSI_TXN_ERRORS for MTL_TXN_ID: '||
9121                                  all_txn_rec.mtl_txn_id;
9122                 RAISE skip_serial;
9123 
9124               EXCEPTION
9125                 WHEN no_data_found THEN
9126                   null;
9127               END;
9128 
9129             END IF; -- valid ib txb
9130             END IF; -- > freeze_date
9131 
9132           END LOOP;
9133 
9134           l_change_owner_flag := 'Y';
9135           l_owner_party_id    := null;
9136           l_owner_account_id  := null;
9137 
9138           get_rma_owner(
9139             p_serial_number     => srl_rec.serial_number,
9140             p_inventory_item_id => srl_rec.inventory_item_id,
9141             p_organization_id   => srl_rec.organization_id,
9142             x_change_owner_flag => l_change_owner_flag,
9143             x_owner_party_id    => l_owner_party_id,
9144             x_owner_account_id  => l_owner_account_id);
9145 
9146           IF l_change_owner_flag = 'Y' THEN
9147             l_owner_party_id := l_internal_party_id;
9148           END IF;
9149 
9150           log('  Change Owner    : '||l_change_owner_flag);
9151           log('  Owner Party ID  : '||l_owner_party_id);
9152           log('  Owner Account ID: '||l_owner_account_id);
9153 
9154           IF l_change_owner_flag = 'Y' THEN
9155             l_parties_tbl(1).instance_party_id      := fnd_api.g_miss_num;
9156             l_parties_tbl(1).party_source_table     := 'HZ_PARTIES';
9157             l_parties_tbl(1).party_id               := l_internal_party_id;
9158             l_parties_tbl(1).relationship_type_code := 'OWNER';
9159             l_parties_tbl(1).contact_flag           := 'N';
9160             l_parties_tbl(1).object_version_number  := 1.0;
9161           ELSE
9162             l_parties_tbl(1).instance_party_id      := fnd_api.g_miss_num;
9163             l_parties_tbl(1).party_source_table     := 'HZ_PARTIES';
9164             l_parties_tbl(1).party_id               := l_owner_party_id;
9165             l_parties_tbl(1).relationship_type_code := 'OWNER';
9166             l_parties_tbl(1).contact_flag           := 'N';
9167             l_parties_tbl(1).object_version_number  := 1.0;
9168 
9169             l_pty_accts_tbl(1).ip_account_id          := fnd_api.g_miss_num;
9170             l_pty_accts_tbl(1).party_account_id       := l_owner_account_id;
9171             l_pty_accts_tbl(1).relationship_type_code := 'OWNER';
9172             l_pty_accts_tbl(1).bill_to_address        := fnd_api.g_miss_num;
9173             l_pty_accts_tbl(1).ship_to_address        := fnd_api.g_miss_num;
9174             l_pty_accts_tbl(1).instance_party_id      := fnd_api.g_miss_num;
9175             l_pty_accts_tbl(1).parent_tbl_index       := 1;
9176 
9177           END IF;
9178 
9179           IF l_instance_found THEN
9180 
9181             SELECT instance_party_id,
9182                    object_version_number
9183             INTO   l_parties_tbl(1).instance_party_id,
9184                    l_parties_tbl(1).object_version_number
9185             FROM   csi_i_parties
9186             WHERE  instance_id = l_instance_rec.instance_id
9187             AND    relationship_type_code = 'OWNER';
9188 
9189             log('  csi_process_txn_pvt.check_and_break_relation');
9190 
9191             -- qualifieis for update
9192             csi_process_txn_pvt.check_and_break_relation(
9193               p_instance_id   => l_instance_rec.instance_id,
9194               p_csi_txn_rec   => l_txn_rec,
9195               x_return_status => l_return_status);
9196 
9197             SELECT object_version_number
9198             INTO   l_instance_rec.object_version_number
9199             FROM   csi_item_instances
9200             WHERE  instance_id = l_instance_rec.instance_id;
9201 
9202             log('  csi_item_instance_pub.update_item_instance');
9203 
9204             csi_item_instance_pub.update_item_instance(
9205               p_api_version           => 1.0,
9206               p_commit                => fnd_api.g_false,
9207               p_init_msg_list         => fnd_api.g_true,
9208               p_validation_level      => fnd_api.g_valid_level_full,
9209               p_instance_rec          => l_instance_rec,
9210               p_party_tbl             => l_parties_tbl,
9211               p_account_tbl           => l_pty_accts_tbl,
9212               p_org_assignments_tbl   => l_org_units_tbl,
9213               p_ext_attrib_values_tbl => l_ea_values_tbl,
9214               p_pricing_attrib_tbl    => l_pricing_tbl,
9215               p_asset_assignment_tbl  => l_assets_tbl,
9216               p_txn_rec               => l_txn_rec,
9217               x_instance_id_lst       => l_instance_ids_list,
9218               x_return_status         => l_return_status,
9219               x_msg_count             => l_msg_count,
9220               x_msg_data              => l_msg_data);
9221 
9222             -- For Bug 4057183
9223             -- IF l_return_status <> fnd_api.g_ret_sts_success THEN
9224             IF l_return_status not in (fnd_api.g_ret_sts_success,'W') THEN
9225               raise fnd_api.g_exc_error;
9226             END IF;
9227 
9228           ELSE -- instance not found
9229 
9230             log('  csi_item_instance_pub.create_item_instance');
9231 
9232             csi_item_instance_pub.create_item_instance(
9233               p_api_version           => 1.0,
9234               p_commit                => fnd_api.g_false,
9235               p_init_msg_list         => fnd_api.g_true,
9236               p_validation_level      => fnd_api.g_valid_level_full,
9237               p_instance_rec          => l_instance_rec,
9238               p_party_tbl             => l_parties_tbl,
9239               p_account_tbl           => l_pty_accts_tbl,
9240               p_org_assignments_tbl   => l_org_units_tbl,
9241               p_ext_attrib_values_tbl => l_ea_values_tbl,
9242               p_pricing_attrib_tbl    => l_pricing_tbl,
9243               p_asset_assignment_tbl  => l_assets_tbl,
9244               p_txn_rec               => l_txn_rec,
9245               x_return_status         => l_return_status,
9246               x_msg_count             => l_msg_count,
9247               x_msg_data              => l_msg_data );
9248 
9249             -- For Bug 4057183
9250             -- IF l_return_status <> fnd_api.g_ret_sts_success THEN
9251             IF l_return_status not in (fnd_api.g_ret_sts_success,'W') THEN
9252               raise fnd_api.g_exc_error;
9253             END IF;
9254 
9255           END IF;
9256 
9257         END IF;
9258       EXCEPTION
9259         WHEN skip_serial THEN
9260           log(fill(srl_rec.serial_number, 15)||
9261               fill(to_char(srl_rec.inventory_item_id), 9)||
9262               fill(to_char(srl_rec.organization_id), 9)||
9263               fill(to_char(srl_rec.serial_code), 2)||
9264               fill(to_char(srl_rec.lot_code), 2));
9265           log(l_skip_error);
9266         WHEN fnd_api.g_exc_error THEN
9267           l_error_message :=  csi_t_gen_utility_pvt.dump_error_stack;
9268           log(fill(srl_rec.serial_number, 15)||
9269               fill(to_char(srl_rec.inventory_item_id), 9)||
9270               fill(to_char(srl_rec.organization_id), 9)||
9271               fill(to_char(srl_rec.serial_code), 2)||
9272               fill(to_char(srl_rec.lot_code), 2));
9273           log('  Error : '||l_error_message);
9274       END;
9275 
9276       IF mod(inv_srl_cur%rowcount,100) = 0 THEN
9277         commit;
9278       END IF;
9279 
9280     END LOOP;
9281 
9282   END sync_inv_serials;
9283   --
9284   PROCEDURE Get_Next_Level
9285     (p_object_id                 IN  NUMBER,
9286      p_rel_tbl                   OUT NOCOPY csi_datastructures_pub.ii_relationship_tbl
9287     ) IS
9288     --
9289     l_rel_type_code          VARCHAR2(30) := 'COMPONENT-OF';
9290     --
9291      CURSOR REL_CUR IS
9292      select relationship_id,relationship_type_code,object_id,subject_id,position_reference,
9293             active_start_date,active_end_date,display_order,mandatory_flag,context,
9294             attribute1,attribute2,attribute3,attribute4,attribute5,attribute6,attribute7,attribute8,
9295             attribute9,attribute10,attribute11,attribute12,attribute13,attribute14,attribute15,
9296             object_version_number
9297      from CSI_II_RELATIONSHIPS cir
9298      where cir.object_id = p_object_id
9299      and   cir.relationship_type_code = l_rel_type_code
9300      and   ((cir.active_end_date is null) or (cir.active_end_date > sysdate))
9301      and   EXISTS (select 'x'
9302                    from CSI_ITEM_INSTANCES cii
9303                    where cii.instance_id = cir.subject_id
9304                    and  ((active_end_date is null) or (active_end_date > sysdate)));
9305      --
9306      l_ctr      NUMBER := 0;
9307   BEGIN
9308      FOR rel in REL_CUR LOOP
9309 	l_ctr := l_ctr + 1;
9310 	p_rel_tbl(l_ctr).relationship_id := rel.relationship_id;
9311 	p_rel_tbl(l_ctr).relationship_type_code := rel.relationship_type_code;
9312 	p_rel_tbl(l_ctr).object_id := rel.object_id;
9313 	p_rel_tbl(l_ctr).subject_id := rel.subject_id;
9314 	p_rel_tbl(l_ctr).position_reference := rel.position_reference;
9315 	p_rel_tbl(l_ctr).active_start_date := rel.active_start_date;
9316 	p_rel_tbl(l_ctr).active_end_date := rel.active_end_date;
9317 	p_rel_tbl(l_ctr).display_order := rel.display_order;
9318 	p_rel_tbl(l_ctr).mandatory_flag := rel.mandatory_flag;
9319 	p_rel_tbl(l_ctr).context := rel.context;
9320 	p_rel_tbl(l_ctr).attribute1 := rel.attribute1;
9321 	p_rel_tbl(l_ctr).attribute2 := rel.attribute2;
9322 	p_rel_tbl(l_ctr).attribute3 := rel.attribute3;
9323 	p_rel_tbl(l_ctr).attribute4 := rel.attribute4;
9324 	p_rel_tbl(l_ctr).attribute5 := rel.attribute5;
9325 	p_rel_tbl(l_ctr).attribute6 := rel.attribute6;
9326 	p_rel_tbl(l_ctr).attribute7 := rel.attribute7;
9327 	p_rel_tbl(l_ctr).attribute8 := rel.attribute8;
9328 	p_rel_tbl(l_ctr).attribute9 := rel.attribute9;
9329 	p_rel_tbl(l_ctr).attribute10 := rel.attribute10;
9330 	p_rel_tbl(l_ctr).attribute11 := rel.attribute11;
9331 	p_rel_tbl(l_ctr).attribute12 := rel.attribute12;
9332 	p_rel_tbl(l_ctr).attribute13 := rel.attribute13;
9333 	p_rel_tbl(l_ctr).attribute14 := rel.attribute14;
9334 	p_rel_tbl(l_ctr).attribute15 := rel.attribute15;
9335 	p_rel_tbl(l_ctr).object_version_number := rel.object_version_number;
9336      END LOOP;
9337   END Get_Next_Level;
9338   --
9339   PROCEDURE missing_mtl_txn_id_in_csi IS
9340 
9341     TYPE NumTabType is  varray(10000) of number;
9342 
9343     l_csi_txn_id_tab     NumTabType;
9344     l_rma_line_id_tab    NumTabType;
9345     l_mtl_txn_id_tab     NumTabType;
9346 
9347     MAX_BUFFER_SIZE      number := 1000;
9348 
9349     CURSOR rma_txn_cur IS
9350       SELECT transaction_id,
9351              source_line_ref_id,
9352              inv_material_transaction_id
9353       FROM   csi_transactions
9354       WHERE  transaction_type_id = 53
9355       AND    inv_material_transaction_id is null;
9356 
9357     l_inventory_item_id  number;
9358     l_organization_id    number;
9359     l_mtl_txn_id         number;
9360 
9361     CURSOR mmt_cur(p_line_id in number, p_item_id in number, p_organization_id in number) IS
9362       SELECT transaction_id
9363       FROM   mtl_material_transactions
9364       WHERE  transaction_type_id = 15 -- RMA Transaction
9365       AND    inventory_item_id   = p_item_id
9366       AND    organization_id     = p_organization_id
9367       AND    trx_source_line_id  = p_line_id
9368       order by creation_date desc, transaction_id desc;
9369 
9370   BEGIN
9371 
9372     OPEN rma_txn_cur;
9373     LOOP
9374 
9375       FETCH rma_txn_cur BULK COLLECT
9376       INTO  l_csi_txn_id_tab,
9377             l_rma_line_id_tab,
9378             l_mtl_txn_id_tab
9379       LIMIT MAX_BUFFER_SIZE;
9380 
9381       FOR ind IN 1 .. l_csi_txn_id_tab.COUNT
9382       LOOP
9383 
9384         IF l_rma_line_id_tab(ind) is not null THEN
9385 
9386           SELECT inventory_item_id,
9387                  ship_from_org_id
9388           INTO   l_inventory_item_id,
9389                  l_organization_id
9390           FROM   oe_order_lines_all
9391           WHERE  line_id = l_rma_line_id_tab(ind);
9392 
9393           l_mtl_txn_id  := null;
9394 
9395           FOR mmt_rec IN mmt_cur(l_rma_line_id_tab(ind), l_inventory_item_id, l_organization_id)
9396           LOOP
9397             l_mtl_txn_id := mmt_rec.transaction_id;
9398             exit;
9399           END LOOP;
9400 
9401           IF l_mtl_txn_id is not null THEN
9402             l_mtl_txn_id_tab(ind) := l_mtl_txn_id;
9403           END IF;
9404 
9405         END IF;
9406       END LOOP;
9407 
9408       FORALL u_ind in 1 .. l_csi_txn_id_tab.count
9409         UPDATE csi_transactions
9410         SET    inv_material_transaction_id = l_mtl_txn_id_tab(u_ind)
9411         WHERE  transaction_id              = l_csi_txn_id_tab(u_ind);
9412 
9413       commit;
9414 
9415       EXIT when rma_txn_cur%NOTFOUND;
9416 
9417     END LOOP;
9418 
9419     commit;
9420 
9421     IF rma_txn_cur%ISOPEN THEN
9422       CLOSE rma_txn_cur;
9423     END IF;
9424 
9425   END  missing_mtl_txn_id_in_csi;
9426 
9427   --
9428   --
9429   PROCEDURE Delete_Dup_Srl_Inv_Instance IS
9430      CURSOR CHECK_CUR IS
9431      select count(*)
9432      from CSI_ITEM_INSTANCES
9433      where location_type_code = 'INVENTORY'
9434      and   instance_usage_code = 'IN_INVENTORY'
9435      and   creation_date = last_update_date
9436      and   serial_number is not null
9437      and   lot_number is null
9438      group by inventory_item_id,serial_number
9439      having count(*) > 1 ;
9440      --
9441      CURSOR CSI_CUR(p_min IN NUMBER,p_max IN NUMBER) IS
9442      select instance_id,
9443 	    inventory_item_id,
9444 	    inv_organization_id,
9445 	    inventory_revision,
9446 	    inv_subinventory_name,
9447 	    inv_locator_id,
9448 	    serial_number,
9449 	    last_update_date
9450      from csi_item_instances
9451      where instance_id between p_min and p_max
9452      and   serial_number is not null
9453      and   lot_number is null
9454      and   location_type_code = 'INVENTORY'
9455      and   instance_usage_code = 'IN_INVENTORY'
9456      and   migrated_flag = 'Y'
9457      and   mfg_serial_number_flag = 'Y'
9458      and   last_update_date = creation_date;
9459      --
9460      CURSOR MIN_CSI IS
9461      select min(instance_id)
9462      from csi_item_instances
9463      where serial_number is not null
9464      and   lot_number is null
9465      and   location_type_code = 'INVENTORY'
9466      and   instance_usage_code = 'IN_INVENTORY'
9467      and   migrated_flag = 'Y'
9468      and   mfg_serial_number_flag = 'Y'
9469      and   last_update_date = creation_date;
9470      --
9471      CURSOR MAX_CSI IS
9472      select max(instance_id)
9473      from csi_item_instances
9474      where serial_number is not null
9475      and   lot_number is null
9476      and   location_type_code = 'INVENTORY'
9477      and   instance_usage_code = 'IN_INVENTORY'
9478      and   migrated_flag = 'Y'
9479      and   mfg_serial_number_flag = 'Y'
9480      and   last_update_date = creation_date;
9481      --
9482      CURSOR TXN_CUR(p_instance_id IN NUMBER) IS
9483      SELECT transaction_id
9484      from CSI_INST_TRANSACTIONS_V
9485      where instance_id = p_instance_id;
9486      --
9487      TYPE NUMLIST is TABLE of NUMBER INDEX BY BINARY_INTEGER;
9488      l_instance_tbl         NUMLIST;
9489      l_txn_tbl              NUMLIST;
9490      --
9491      v_min                  NUMBER;
9492      v_max                  NUMBER;
9493      v_low                  NUMBER;
9494      v_high                 NUMBER;
9495      v_batch                NUMBER := nvl(FND_PROFILE.VALUE('CS_UPG_BATCHSIZE'),100);
9496      v_start                NUMBER;
9497      v_end                  NUMBER;
9498      v_diff                 NUMBER;
9499      v_batch_counter        NUMBER := 0;
9500      v_ins_count            NUMBER := 0;
9501      v_txn_count            NUMBER := 0;
9502      v_commit_counter       NUMBER := 0;
9503      v_subinv               VARCHAR2(10);
9504      v_locator_id           NUMBER;
9505      v_revision             VARCHAR2(3);
9506      v_org_id               NUMBER;
9507      v_recount              NUMBER;
9508      --
9509      Process_next           EXCEPTION;
9510      comp_error             EXCEPTION;
9511   BEGIN
9512      v_recount := 0;
9513      OPEN CHECK_CUR;
9514      FETCH CHECK_CUR INTO v_recount;
9515      CLOSE CHECK_CUR;
9516      --
9517      IF nvl(v_recount,0) = 0 THEN
9518 	Raise comp_error;
9519      END IF;
9520      --
9521      OPEN MIN_CSI;
9522      FETCH MIN_CSI into v_low;
9523      CLOSE MIN_CSI;
9524      --
9525      OPEN MAX_CSI;
9526      FETCH MAX_CSI into v_high;
9527      CLOSE MAX_CSI;
9528      --
9529      v_diff  := ceil((v_high - v_low)/v_batch);
9530      v_start := v_low;
9531      v_end   := v_low + v_diff;
9532      v_batch_counter := 1;
9533      --
9534      l_instance_tbl.DELETE;
9535      l_txn_tbl.DELETE;
9536      LOOP
9537 	For csi_rec in CSI_CUR(v_start,v_end)
9538 	Loop
9539 	   Begin
9540 	      Begin
9541 		 select mmt.subinventory_code,mmt.locator_id,mmt.organization_id,mmt.revision
9542 		 into v_subinv,v_locator_id,v_org_id,v_revision
9543 		 from MTL_MATERIAL_TRANSACTIONS mmt
9544 		 where mmt.transaction_id = (select max(transaction_id)
9545 					     from MTL_UNIT_TRANSACTIONS
9546 					     where serial_number = csi_rec.serial_number
9547 					     and   inventory_item_id = csi_rec.inventory_item_id
9548 					     and   last_update_date <= csi_rec.last_update_date);
9549 	      Exception
9550 		 when others then
9551 		    Raise Process_next;
9552 	      End;
9553 	      --
9554 	      if ((v_subinv <> csi_rec.inv_subinventory_name) OR
9555 		  (nvl(v_locator_id,-999) <> nvl(csi_rec.inv_locator_id,-999)) OR
9556 		  (nvl(v_revision,'$#$') <> nvl(csi_rec.inventory_revision,'$#$')) OR
9557 		  (v_org_id <> csi_rec.inv_organization_id)) then
9558 		 v_ins_count := v_ins_count + 1;
9559 		 l_instance_tbl(v_ins_count) := csi_rec.instance_id;
9560 		 For txn in TXN_CUR(csi_rec.instance_id) Loop
9561 		    v_txn_count := v_txn_count + 1;
9562 		    l_txn_tbl(v_txn_count) := txn.transaction_id;
9563 		 End Loop;
9564 	      end if;
9565 	      --
9566 	   Exception
9567 	      when Process_next then
9568 		 null;
9569 	   End;
9570 	End Loop;
9571 	--
9572 	v_batch_counter := v_batch_counter + 1;
9573 	EXIT WHEN v_min = v_max;
9574 	EXIT WHEN v_end = v_max;
9575 	EXIT WHEN v_batch_counter > v_batch;
9576 	v_start := v_end + 1;
9577 	--
9578 	if v_batch_counter <> v_batch then
9579 	   v_end := v_end + v_diff;
9580 	else
9581 	   v_end := v_high;
9582 	end if;
9583 	--
9584 	if v_start > v_max then
9585 	   v_start := v_max;
9586 	end if;
9587 	--
9588 	if v_end > v_max then
9589 	   v_end := v_max;
9590 	end if;
9591 	--
9592 	commit;
9593      END LOOP;
9594      commit;
9595      --
9596      IF l_instance_tbl.count > 0 THEN
9597        BEGIN
9598 	  FORALL j in l_instance_tbl.FIRST .. l_instance_tbl.LAST
9599 	     DELETE FROM CSI_ITEM_INSTANCES WHERE instance_id = l_instance_tbl(j);
9600 	  FORALL j in l_instance_tbl.FIRST .. l_instance_tbl.LAST
9601 	     DELETE FROM CSI_I_PARTIES WHERE instance_id = l_instance_tbl(j);
9602 	  FORALL j in l_instance_tbl.FIRST .. l_instance_tbl.LAST
9603 	     DELETE FROM CSI_I_VERSION_LABELS WHERE instance_id = l_instance_tbl(j);
9604 	  FORALL j in l_txn_tbl.FIRST .. l_txn_tbl.LAST
9605 	     DELETE FROM CSI_ITEM_INSTANCES_H WHERE transaction_id = l_txn_tbl(j);
9606 	  FORALL j in l_txn_tbl.FIRST .. l_txn_tbl.LAST
9607 	     DELETE FROM CSI_I_PARTIES_H WHERE transaction_id = l_txn_tbl(j);
9608 	  FORALL j in l_txn_tbl.FIRST .. l_txn_tbl.LAST
9609 	     DELETE FROM CSI_I_VERSION_LABELS_H WHERE transaction_id = l_txn_tbl(j);
9610 	  FORALL j in l_txn_tbl.FIRST .. l_txn_tbl.LAST
9611 	     DELETE FROM CSI_TRANSACTIONS WHERE transaction_id = l_txn_tbl(j);
9612        END;
9613      END IF;
9614      commit;
9615      --
9616   EXCEPTION
9617      when comp_error then
9618 	null;
9619   END Delete_Dup_Srl_Inv_Instance;
9620   --
9621   PROCEDURE Update_Instance_Usage IS
9622     CURSOR CSI_CUR IS
9623     Select instance_id,location_type_code
9624 	  ,serial_number_control_code,serial_number,null usage_code
9625     from CSI_ITEM_INSTANCES cii
9626 	,MTL_SYSTEM_ITEMS_B msi
9627     Where cii.instance_usage_code is NULL
9628     and   msi.inventory_item_id = cii.inventory_item_id
9629     and   msi.organization_id = cii.last_vld_organization_id;
9630     --
9631     l_exists                 VARCHAR2(1);
9632     l_rel_type_code          VARCHAR2(30) := 'COMPONENT-OF';
9633     --
9634     Type NumTabType is VARRAY(10000) of NUMBER;
9635     instance_id_mig          NumTabType;
9636     serial_code_mig          NumTabType;
9637     --
9638     Type V30TabType is VARRAY(10000) of VARCHAR2(30);
9639     location_type_mig        V30TabType;
9640     serial_number_mig        V30TabType;
9641     usage_code_mig           V30TabType;
9642     --
9643     MAX_BUFFER_SIZE          NUMBER := 1000;
9644  BEGIN
9645     OPEN CSI_CUR;
9646     LOOP
9647        FETCH CSI_CUR BULK COLLECT INTO
9648        instance_id_mig,
9649        location_type_mig,
9650        serial_code_mig,
9651        serial_number_mig,
9652        usage_code_mig
9653        LIMIT MAX_BUFFER_SIZE;
9654        --
9655        FOR i in 1 .. instance_id_mig.count LOOP
9656 	  If serial_code_mig(i) = 6 and
9657 	     serial_number_mig(i) is not NULL and
9658 	     location_type_mig(i) = 'INVENTORY' Then
9659 	     usage_code_mig(i) := 'RETURNED';
9660 	  Else
9661 	     if location_type_mig(i) = 'INVENTORY' then
9662 		usage_code_mig(i) := 'IN_INVENTORY';
9663              elsif location_type_mig(i) = 'WIP' then
9664                 usage_code_mig(i) := 'IN_WIP';
9665 	     else
9666 		usage_code_mig(i) := 'OUT_OF_ENTERPRISE';
9667 	     end if;
9668 	  End if;
9669 	  Begin
9670 	     select 'x'
9671 	     into l_exists
9672 	     from CSI_II_RELATIONSHIPS
9673 	     where subject_id = instance_id_mig(i)
9674 	     and   relationship_type_code = l_rel_type_code
9675              and   ((active_end_date is null) or (active_end_date > sysdate));
9676 	     usage_code_mig(i) := 'IN_RELATIONSHIP';
9677 	  Exception
9678 	     when no_data_found then
9679 		null;
9680 	  End;
9681        --
9682        END LOOP;
9683        FORALL j in 1 .. instance_id_mig.count
9684 	  UPDATE CSI_ITEM_INSTANCES
9685 	  set instance_usage_code = usage_code_mig(j)
9686 	     ,last_update_date = sysdate
9687 	  where instance_id = instance_id_mig(j);
9688        commit;
9689        EXIT WHEN CSI_CUR%NOTFOUND;
9690     END LOOP;
9691     commit;
9692     CLOSE CSI_CUR;
9693   END Update_Instance_Usage;
9694   --
9695   PROCEDURE Update_Full_dump_flag IS
9696     CURSOR CSI_REL_CUR IS
9697     SELECT relationship_history_id
9698     FROM CSI_II_RELATIONSHIPS_H
9699     WHERE nvl(MIGRATED_FLAG,'N') = 'Y'
9700     AND   full_dump_flag <> 'Y';
9701     --
9702     CURSOR CSI_SYS_CUR IS
9703     SELECT system_id,min(system_history_id) system_history_id
9704     FROM CSI_SYSTEMS_H
9705     WHERE nvl(MIGRATED_FLAG,'N') = 'Y'
9706     AND   full_dump_flag <> 'Y'
9707     group by system_id;
9708     --
9709     Type NumTabType is VARRAY(10000) of NUMBER;
9710     rel_history_id_mig     NumTabType;
9711     system_id_mig          NumTabType;
9712     system_history_id_mig  NumTabType;
9713     --
9714     MAX_BUFFER_SIZE        NUMBER := 1000;
9715  BEGIN
9716     OPEN CSI_REL_CUR;
9717     LOOP
9718        FETCH CSI_REL_CUR BULK COLLECT INTO
9719        rel_history_id_mig
9720        LIMIT MAX_BUFFER_SIZE;
9721        FORALL j in 1 .. rel_history_id_mig.count
9722 	  update CSI_II_RELATIONSHIPS_H
9723 	  set full_dump_flag = 'Y'
9724 	  where relationship_history_id = rel_history_id_mig(j);
9725        commit;
9726        --
9727        EXIT WHEN CSI_REL_CUR%NOTFOUND;
9728     END LOOP;
9729     commit;
9730     CLOSE CSI_REL_CUR;
9731     --
9732     OPEN CSI_SYS_CUR;
9733     LOOP
9734        FETCH CSI_SYS_CUR BULK COLLECT INTO
9735        system_id_mig,
9736        system_history_id_mig
9737        LIMIT MAX_BUFFER_SIZE;
9738        FORALL j in 1 .. system_id_mig.count
9739 	  UPDATE CSI_SYSTEMS_H
9740 	  set full_dump_flag = 'Y'
9741 	  where system_history_id = system_history_id_mig(j);
9742        commit;
9743        --
9744        EXIT WHEN CSI_SYS_CUR%NOTFOUND;
9745     END LOOP;
9746     commit;
9747     CLOSE CSI_SYS_CUR;
9748  END Update_Full_dump_flag;
9749  --
9750  PROCEDURE Del_API_Dup_Srl_Instance IS
9751     cursor check_count is
9752     select count(*)
9753     from   csi_item_instances
9754     where  serial_number is not null
9755     group  by serial_number, inventory_item_id
9756     having count(*) > 1;
9757     --
9758     cursor c1 is
9759     select count(*), serial_number, inventory_item_id
9760     from   csi_item_instances
9761     where  serial_number is not null
9762     group  by serial_number, inventory_item_id
9763     having count(*) > 1;
9764     --
9765     cursor c4(p_serial_number varchar, p_inventory_item_id number) is
9766     select instance_id,instance_status_id
9767     from   csi_item_instances
9768     where  serial_number = p_serial_number
9769     and    inventory_item_id = p_inventory_item_id
9770     order  by instance_id desc;
9771     --
9772     v_commit_count       NUMBER := 0;
9773     v_recount            NUMBER;
9774     v_instance_status_id NUMBER;
9775     v_duplicate_count    NUMBER;
9776     v_instance_id        NUMBER;
9777     v_status_id          NUMBER;
9778     --
9779     comp_error           EXCEPTION;
9780  BEGIN
9781     v_recount := 0;
9782     OPEN check_count;
9783     FETCH check_count into v_recount;
9784     CLOSE check_count;
9785     --
9786     IF nvl(v_recount,0) = 0 THEN
9787        Raise comp_error;
9788     END IF;
9789     --
9790     begin
9791        select instance_status_id
9792        into   v_instance_status_id
9793        from   CSI_INSTANCE_STATUSES
9794        where  name = 'EXPIRED';
9795     exception
9796        when no_data_found then
9797 	  raise_application_error(-20000,'You need to setup an Expired Instance Status');
9798 	  return;
9799        when too_many_rows then
9800 	  raise_application_error(-20000,'Too many definition for Expired Instance Status');
9801 	  return;
9802     end;
9803 
9804     for i in c1
9805     loop
9806        v_instance_id := null;
9807        v_commit_count := v_commit_count + 1;
9808        --
9809        v_duplicate_count := 0;
9810        --
9811        Begin
9812 	  select max(instance_id)
9813 	  into v_instance_id
9814 	  from csi_item_instances
9815 	  where inventory_item_id = i.inventory_item_id
9816 	  and   serial_number = i.serial_number;
9817        End;
9818        --
9819        for j in c4(i.serial_number, i.inventory_item_id)
9820        loop
9821 	  if v_instance_id <> j.instance_id then
9822 	     v_duplicate_count := v_duplicate_count + 1;
9823 
9824 	     update csi_item_instances_h
9825 	     set    old_serial_number = i.serial_number
9826 		    ,new_serial_number = new_serial_number||'-DUP'||to_char(v_duplicate_count)
9827 		    ,last_updated_by = fnd_global.user_id
9828 		    ,last_update_date = sysdate
9829 		    ,old_instance_status_id = j.instance_status_id
9830 		    ,new_instance_status_id = v_instance_status_id
9831 	     where  instance_history_id = (select max(instance_history_id)
9832 					   from csi_item_instances_h
9833 					   where instance_id = j.instance_id);
9834 	     --
9835 	     update csi_item_instances
9836 	     set    serial_number = serial_number||'-DUP'||to_char(v_duplicate_count)
9837 		    ,active_end_date = sysdate
9838 		    ,instance_status_id = v_instance_status_id
9839 		    ,last_updated_by = fnd_global.user_id
9840 		    ,last_update_date = sysdate
9841 	     where  instance_id = j.instance_id;
9842 	  end if;
9843        end loop;
9844        --
9845        if v_commit_count >= 500 then
9846 	  commit;
9847 	  v_commit_count := 0;
9848        end if;
9849        --
9850     end loop;
9851     commit;
9852  EXCEPTION
9853     when comp_error then
9854        null;
9855  END Del_API_Dup_Srl_Instance;
9856  --
9857  PROCEDURE Update_Vld_Organization IS
9858    CURSOR CSI_INS_CUR IS
9859    SELECT instance_id,inventory_item_id,inv_organization_id,last_vld_organization_id
9860          ,serial_number,lot_number
9861          ,mfg_serial_number_flag,creation_complete_flag
9862          ,inventory_revision
9863          ,instance_usage_code
9864    FROM CSI_ITEM_INSTANCES
9865    WHERE location_type_code = 'INVENTORY'
9866    AND   inv_organization_id is not null
9867    AND   nvl(last_vld_organization_id,-999) <> inv_organization_id;
9868    --
9869    l_instance_rec      csi_datastructures_pub.instance_rec;
9870    l_temp_instance_rec csi_datastructures_pub.instance_rec;
9871    l_txn_rec           csi_datastructures_pub.transaction_rec;
9872    l_return_value      BOOLEAN;
9873    --
9874    Type NumTabType is VARRAY(10000) of NUMBER;
9875    instance_id_mig          NumTabType;
9876    item_id_mig              NumTabType;
9877    inv_org_id_mig           NumTabType;
9878    vld_org_id_mig           NumTabType;
9879    --
9880    Type V30Type is VARRAY(10000) of VARCHAR2(30);
9881    serial_number_mig        V30Type;
9882    usage_mig                V30Type;
9883 
9884    Type V80Type is VARRAY(10000) of VARCHAR2(80);
9885    lot_number_mig           V80Type;
9886    --
9887    Type V3Type is VARRAY(10000) of VARCHAR2(3);
9888    revision_mig             V3Type;
9889    --
9890    Type V1Type is VARRAY(10000) of VARCHAR2(1);
9891    mfg_srl_flag_mig         V1Type;
9892    complete_flag_mig        V1Type;
9893    --
9894    MAX_BUFFER_SIZE          NUMBER := 1000;
9895    --
9896    Process_Next             EXCEPTION;
9897 BEGIN
9898    OPEN CSI_INS_CUR;
9899    LOOP
9900       FETCH CSI_INS_CUR BULK COLLECT INTO
9901       instance_id_mig,
9902       item_id_mig,
9903       inv_org_id_mig,
9904       vld_org_id_mig,
9905       serial_number_mig,
9906       lot_number_mig,
9907       mfg_srl_flag_mig,
9908       complete_flag_mig,
9909       revision_mig,
9910       usage_mig
9911       LIMIT MAX_BUFFER_SIZE;
9912       --
9913       FOR i in 1 .. instance_id_mig.count LOOP
9914          l_instance_rec := l_temp_instance_rec;
9915          --
9916          -- Need to validate against inv_organization_id since we update the instance with this org.
9917          --
9918          l_instance_rec.instance_id := instance_id_mig(i);
9919          l_instance_rec.inventory_item_id := item_id_mig(i);
9920          l_instance_rec.vld_organization_id := inv_org_id_mig(i);
9921          l_instance_rec.serial_number := serial_number_mig(i);
9922          l_instance_rec.lot_number := lot_number_mig(i);
9923          l_instance_rec.location_type_code := 'INVENTORY';
9924          l_instance_rec.mfg_serial_number_flag := mfg_srl_flag_mig(i);
9925          l_instance_rec.creation_complete_flag := complete_flag_mig(i);
9926          l_instance_rec.inventory_revision := revision_mig(i);
9927          l_instance_rec.instance_usage_code := usage_mig(i);
9928          --
9929          csi_Item_Instance_Vld_pvt.Validate_org_dependent_params
9930             ( p_instance_rec   => l_instance_rec
9931              ,p_txn_rec        => l_txn_rec
9932              ,l_return_value   => l_return_value
9933             );
9934          --
9935          IF l_return_value = TRUE THEN
9936             vld_org_id_mig(i) := inv_org_id_mig(i);
9937          END IF;
9938          --
9939       END LOOP;
9940       FORALL j in 1 .. instance_id_mig.count
9941          Update CSI_ITEM_INSTANCES
9942          set last_vld_organization_id = vld_org_id_mig(j)
9943          where instance_id = instance_id_mig(j);
9944       --
9945       commit;
9946       --
9947       FORALL j in 1 .. instance_id_mig.count
9948          Update CSI_ITEM_INSTANCES_H
9949          set new_last_vld_organization_id = vld_org_id_mig(j)
9950          where instance_history_id = (select max(instance_history_id) from CSI_ITEM_INSTANCES_H
9951                                       where instance_id = instance_id_mig(j));
9952       --
9953       commit;
9954       --
9955       EXIT WHEN CSI_INS_CUR%NOTFOUND;
9956    END LOOP;
9957    commit;
9958    CLOSE CSI_INS_CUR;
9959  END Update_Vld_Organization;
9960  --
9961  PROCEDURE Update_Revision IS
9962     CURSOR c1 IS
9963     SELECT a.instance_id
9964 	  ,a.inventory_item_id
9965 	  ,a.last_vld_organization_id
9966 	  ,a.last_oe_order_line_id
9967 	  ,a.serial_number
9968 	  ,a.inventory_revision
9969     FROM  csi_item_instances a
9970 	 ,mtl_system_items_b b
9971     WHERE a.inventory_item_id = b.inventory_item_id
9972     AND   a.last_vld_organization_id = b.organization_id
9973     AND   a.creation_complete_flag = 'Y'
9974     AND   a.inventory_revision IS NULL
9975     AND   a.migrated_flag = 'Y'
9976     AND   b.revision_qty_control_code = 2;
9977 
9978     Type NumTabType is VARRAY(10000) of NUMBER;
9979     instance_id_mig           NumTabType;
9980     item_id_mig               NumTabType;
9981     vld_org_id_mig            NumTabType;
9982     order_line_id_mig         NumTabType;
9983     l_schema_name             varchar2(30);
9984     l_object_name             varchar2(80);
9985     l_return_status           varchar2(1) := fnd_api.g_ret_sts_success;
9986     l_sql_stmt                varchar2(2000);
9987     --
9988     Type V30Type is VARRAY(10000) of VARCHAR2(30);
9989     serial_number_mig        V30Type;
9990     --
9991     Type V3Type is VARRAY(10000) of VARCHAR2(3);
9992     revision_mig             V3Type;
9993     --
9994     MAX_BUFFER_SIZE          NUMBER := 1000;
9995  BEGIN
9996     get_schema_name(
9997       p_product_short_name  => 'CS',
9998       x_schema_name         => l_schema_name,
9999       x_return_status       => l_return_status);
10000 
10001     IF l_return_status <> fnd_api.g_ret_sts_success THEN
10002        RETURN;
10003     END IF;
10004     --
10005     l_object_name := l_schema_name||'.CS_CUSTOMER_PRODUCTS_ALL_OLD cp';
10006     --
10007     l_sql_stmt := 'select cr.revision from CS_CP_REVISIONS cr, '||l_object_name || ' where cp.customer_product_id = :inst_id and cr.cp_revision_id = cp.current_cp_revision_id';
10008     --
10009     OPEN c1;
10010     LOOP
10011        FETCH c1 BULK COLLECT INTO
10012        instance_id_mig,
10013        item_id_mig,
10014        vld_org_id_mig,
10015        order_line_id_mig,
10016        serial_number_mig,
10017        revision_mig
10018        LIMIT MAX_BUFFER_SIZE;
10019        --
10020        FOR i in 1 .. instance_id_mig.count LOOP
10021 	  Begin
10022              EXECUTE IMMEDIATE l_sql_stmt INTO revision_mig(i) USING instance_id_mig(i);
10023 	  Exception
10024 	     when others then
10025 		revision_mig(i) := null;
10026 	  End;
10027 	  --
10028 	  IF revision_mig(i) is null and order_line_id_mig(i) is not null THEN
10029 	     Begin
10030 		select revision
10031 		into revision_mig(i)
10032 		from MTL_MATERIAL_TRANSACTIONS
10033 		where transaction_type_id = 33
10034 		and   transaction_action_id = 1
10035 		and   trx_source_line_id = order_line_id_mig(i)
10036 		and   rownum < 2;
10037 	     Exception
10038 		when no_data_found then
10039 		   Begin
10040 		      select item_revision
10041 		      into revision_mig(i)
10042 		      from OE_ORDER_LINES_ALL
10043 		      where line_id = order_line_id_mig(i);
10044 		   Exception
10045 		      when no_data_found then
10046 			 revision_mig(i) := null;
10047 		   End;
10048 	     End;
10049 	  END IF;
10050 	  --
10051 	  IF revision_mig(i) is null AND serial_number_mig(i) is not null THEN
10052 	     Begin
10053 		select revision
10054 		into revision_mig(i)
10055 		from MTL_SERIAL_NUMBERS
10056 		where inventory_item_id = item_id_mig(i)
10057 		and   serial_number = serial_number_mig(i);
10058 	     Exception
10059 		when no_data_found then
10060 		   revision_mig(i) := null;
10061 	     End;
10062 	  END IF;
10063        END LOOP;
10064        --
10065        FORALL j in 1 .. instance_id_mig.count
10066 	  UPDATE csi_item_instances
10067 	  SET    inventory_revision = revision_mig(j)
10068 	  WHERE  instance_id = instance_id_mig(j);
10069        commit;
10070        --
10071        FORALL j in 1 .. instance_id_mig.count
10072 	  Update CSI_ITEM_INSTANCES_H
10073 	  set new_inventory_revision = revision_mig(j)
10074 	  where instance_history_id = (select max(instance_history_id) from CSI_ITEM_INSTANCES_H
10075 				       where instance_id = instance_id_mig(j));
10076        --
10077        commit;
10078        --
10079        EXIT WHEN c1%NOTFOUND;
10080     END LOOP;
10081     commit;
10082     CLOSE c1;
10083  END Update_Revision;
10084  --
10085  PROCEDURE Update_Dup_Srl_Instance IS
10086     CURSOR CSI_CUR IS -- Cursor to delete the Inv instances emerged from wrong WHERE clause in Mig
10087     select cii.instance_id
10088     from CSI_ITEM_INSTANCES cii
10089     where cii.location_type_code = 'INVENTORY'
10090     and   cii.instance_usage_code = 'IN_INVENTORY'
10091     and   mfg_serial_number_flag = 'Y'
10092     and   cii.serial_number is not null
10093     and   cii.lot_number is not null
10094     and   cii.migrated_flag = 'Y'
10095     and   cii.creation_date = cii.last_update_date
10096     and   not exists (select 'X' from mtl_serial_numbers msn
10097 		      where msn.inventory_item_id = cii.inventory_item_id
10098 		      and   msn.serial_number = cii.serial_number
10099 		      and   msn.lot_number = cii.lot_number);
10100     --
10101     CURSOR TXN_CUR(p_instance_id IN NUMBER) IS
10102     SELECT transaction_id
10103     from CSI_INST_TRANSACTIONS_V
10104     where instance_id = p_instance_id;
10105     --
10106     l_ins_count        NUMBER := 0;
10107     l_txn_count        NUMBER := 0;
10108     --
10109     Type NumTabType is VARRAY(10000) of NUMBER;
10110     instance_id_mig     NumTabType;
10111     --
10112     MAX_BUFFER_SIZE     NUMBER := 1000;
10113     TYPE NUMLIST IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
10114     l_del_inst_tbl      NUMLIST;
10115     l_del_txn_tbl       NUMLIST;
10116     --
10117     cursor c1 is
10118     select count(*), serial_number, inventory_item_id
10119     from   csi_item_instances
10120     where  migrated_flag = 'Y'
10121     and    serial_number is not null
10122     group  by serial_number, inventory_item_id
10123     having count(*) > 1;
10124 
10125     cursor c2(p_serial_number varchar, p_inventory_item_id number) is
10126     select max(instance_id)
10127     from   csi_item_instances
10128     where  serial_number = p_serial_number
10129     and    inventory_item_id = p_inventory_item_id
10130     and    location_type_code = 'INVENTORY'
10131     and    instance_usage_code = 'IN_INVENTORY'
10132     and    inv_subinventory_name IS NOT NULL
10133     and    inv_organization_id IS NOT NULL;
10134 
10135     cursor c3(p_serial_number varchar, p_inventory_item_id number) is
10136     select max(instance_id)
10137     from   csi_item_instances
10138     where  serial_number = p_serial_number
10139     and    inventory_item_id = p_inventory_item_id;
10140 
10141     cursor c5(p_serial_number varchar, p_inventory_item_id number) is
10142     select max(instance_id)
10143     from   csi_item_instances
10144     where  serial_number = p_serial_number
10145     and    inventory_item_id = p_inventory_item_id
10146     and    last_oe_rma_line_id is not null
10147     and    migrated_flag = 'Y';
10148 
10149     cursor c4(p_serial_number varchar, p_inventory_item_id number) is
10150     select instance_id,instance_status_id
10151     from   csi_item_instances
10152     where  serial_number = p_serial_number
10153     and    inventory_item_id = p_inventory_item_id
10154     order  by instance_id desc;
10155     --
10156     cursor c6(p_inst_id1 number,p_inst_id2 number) is
10157     select transaction_id
10158     from csi_item_instances_h
10159     where instance_id = p_inst_id1
10160     intersect
10161     select transaction_id
10162     from csi_item_instances_h
10163     where instance_id = p_inst_id2;
10164 
10165     v_commit_count       NUMBER := 0;
10166     v_instance_status_id NUMBER;
10167     v_duplicate_count    NUMBER;
10168     v_instance_id        NUMBER;
10169     v_inv_instance_id    NUMBER;
10170     v_cp_instance_id     NUMBER;
10171     v_inv_exists         VARCHAR2(1) := 'N';
10172     v_terminated_flag    VARCHAR2(1);
10173     v_ret_instance_id    NUMBER;
10174     v_srl_control        NUMBER;
10175     v_inst_usage_code    VARCHAR2(30);
10176     v_org_id             NUMBER;
10177     v_subinv             VARCHAR2(10);
10178     v_locator            NUMBER;
10179     v_loc_id             NUMBER;
10180     v_status_id          NUMBER;
10181     v_status             VARCHAR2(50);
10182     v_txn_id             NUMBER;
10183     v_freeze_date        DATE;
10184     v_rev                VARCHAR2(3);
10185     l_schema_name        varchar2(30);
10186     l_object_name        varchar2(80);
10187     l_return_status      varchar2(1) := fnd_api.g_ret_sts_success;
10188     l_sql_stmt           varchar2(2000);
10189     --
10190  BEGIN
10191     get_schema_name(
10192       p_product_short_name  => 'CS',
10193       x_schema_name         => l_schema_name,
10194       x_return_status       => l_return_status);
10195     --
10196     IF l_return_status <> fnd_api.g_ret_sts_success THEN
10197        RETURN;
10198     END IF;
10199     --
10200     l_object_name := l_schema_name||'.CS_CUSTOMER_PRODUCTS_ALL_OLD cp';
10201     --
10202     l_sql_stmt := 'select terminated_flag,cp.customer_product_status_id from CSI_INSTANCE_STATUSES cis, '||l_object_name||' where cp.customer_product_id = :inst_id and cis.instance_status_id = cp.customer_product_status_id';
10203     --
10204     BEGIN -- First
10205        OPEN CSI_CUR;
10206        LOOP
10207 	  FETCH CSI_CUR BULK COLLECT INTO
10208 	  instance_id_mig
10209 	  LIMIT MAX_BUFFER_SIZE;
10210 	  --
10211 	  FOR i in 1 .. instance_id_mig.count LOOP
10212 	     l_ins_count := l_ins_count + 1;
10213 	     l_del_inst_tbl(l_ins_count) := instance_id_mig(i);
10214 	     FOR txn_rec in TXN_CUR(instance_id_mig(i)) LOOP
10215 		l_txn_count := l_txn_count + 1;
10216 		l_del_txn_tbl(l_txn_count) := txn_rec.transaction_id;
10217 	     END LOOP;
10218 	  END LOOP;
10219 	  EXIT WHEN CSI_CUR%NOTFOUND;
10220        END LOOP;
10221        CLOSE CSI_CUR;
10222        --
10223        IF l_del_inst_tbl.count > 0 THEN
10224 	 BEGIN
10225 	    FORALL j in l_del_inst_tbl.FIRST .. l_del_inst_tbl.LAST
10226 	       DELETE FROM CSI_ITEM_INSTANCES WHERE instance_id = l_del_inst_tbl(j);
10227 	    FORALL j in l_del_inst_tbl.FIRST .. l_del_inst_tbl.LAST
10228 	       DELETE FROM CSI_I_PARTIES WHERE instance_id = l_del_inst_tbl(j);
10229 	    FORALL j in l_del_inst_tbl.FIRST .. l_del_inst_tbl.LAST
10230 	       DELETE FROM CSI_I_VERSION_LABELS WHERE instance_id = l_del_inst_tbl(j);
10231 	    FORALL j in l_del_txn_tbl.FIRST .. l_del_txn_tbl.LAST
10232 	       DELETE FROM CSI_ITEM_INSTANCES_H WHERE transaction_id = l_del_txn_tbl(j);
10233 	    FORALL j in l_del_txn_tbl.FIRST .. l_del_txn_tbl.LAST
10234 	       DELETE FROM CSI_I_PARTIES_H WHERE transaction_id = l_del_txn_tbl(j);
10235 	    FORALL j in l_del_txn_tbl.FIRST .. l_del_txn_tbl.LAST
10236 	       DELETE FROM CSI_I_VERSION_LABELS_H WHERE transaction_id = l_del_txn_tbl(j);
10237 	    FORALL j in l_del_txn_tbl.FIRST .. l_del_txn_tbl.LAST
10238 	       DELETE FROM CSI_TRANSACTIONS WHERE transaction_id = l_del_txn_tbl(j);
10239 	 END;
10240        END IF;
10241        commit;
10242     END; -- First
10243     commit;
10244     --
10245     BEGIN -- Second Update
10246        begin
10247 	  select instance_status_id
10248 	  into   v_instance_status_id
10249 	  from   CSI_INSTANCE_STATUSES
10250 	  where  name = 'EXPIRED';
10251        exception
10252 	  when no_data_found then
10253 	     raise_application_error(-20000,'You need to setup an Expired Instance Status');
10254 	     return;
10255 	  when too_many_rows then
10256 	     raise_application_error(-20000,'Too many definition for Expired Instance Status');
10257 	     return;
10258        end;
10259        --
10260        IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
10261          csi_gen_utility_pvt.populate_install_param_rec;
10262        END IF;
10263        --
10264        v_freeze_date := csi_datastructures_pub.g_install_param_rec.freeze_date;
10265        --
10266        for i in c1
10267        loop
10268 	  v_inv_instance_id := null;
10269 	  v_cp_instance_id := null;
10270 	  v_instance_id := null;
10271 	  v_ret_instance_id := null;
10272 	  v_terminated_flag := 'X';
10273 	  v_org_id := null;
10274 	  v_subinv := null;
10275 	  v_locator := null;
10276 	  v_loc_id := null;
10277 	  v_status_id := null;
10278           v_status := null;
10279 	  v_commit_count := v_commit_count + 1;
10280 
10281 	  open c2(i.serial_number, i.inventory_item_id);
10282 	  fetch c2 into v_inv_instance_id;
10283 	  close c2;
10284 	  --
10285 	  if v_inv_instance_id is null then
10286 	     open c3(i.serial_number, i.inventory_item_id);
10287 	     fetch c3 into v_cp_instance_id;
10288 	     close c3;
10289 	     --
10290 	     v_instance_id := v_cp_instance_id;
10291 	  else -- INV instance found
10292 	     -- Try to get the last RMA instance
10293 	     open c5(i.serial_number, i.inventory_item_id);
10294 	     fetch c5 into v_ret_instance_id;
10295 	     close c5;
10296 	     --
10297 	     if v_ret_instance_id is not null then -- RMA instance found
10298 		Begin
10299                    EXECUTE IMMEDIATE l_sql_stmt INTO v_terminated_flag,v_status_id USING v_ret_instance_id;
10300 		Exception
10301 		   when others then
10302 		      null;
10303 		End;
10304 		--
10305 		if v_terminated_flag = 'Y' then -- Instance will be owned by Internal
10306 		   v_instance_id := v_inv_instance_id;
10307 		else
10308 		   v_instance_id := v_ret_instance_id;
10309 		   Begin
10310 		      select location_id,inv_organization_id
10311 			    ,inv_subinventory_name,inv_locator_id,inventory_revision
10312 		      into v_loc_id,v_org_id,v_subinv,v_locator,v_rev
10313 		      from CSI_ITEM_INSTANCES
10314 		      where instance_id = v_inv_instance_id;
10315 		      --
10316 		   Exception
10317 		      when no_data_found then
10318 			 null;
10319 		   End;
10320 		   --
10321 		   Begin
10322 		      select serial_number_control_code
10323 		      into v_srl_control
10324 		      from MTL_SYSTEM_ITEMS_B
10325 		      where inventory_item_id = i.inventory_item_id
10326 		      and   organization_id = v_org_id;
10327 		   Exception
10328 		      when others then
10329 			 null;
10330 		   End;
10331 		   --
10332 		   if v_srl_control = 6 then
10333 		      v_inst_usage_code := 'RETURNED';
10334 		   else
10335 		      v_inst_usage_code := 'IN_INVENTORY';
10336 		   end if;
10337 		   --
10338 		  -- Added a check for the case of status id being null. shegde
10339 		   if v_status_id is NULL then
10340 		      v_status := nvl(FND_PROFILE.VALUE('CSI_DEFAULT_INSTANCE_STATUS'), 'Latest');
10341                       select instance_status_id
10342                       into v_status_id
10343                       from CSI_INSTANCE_STATUSES
10344                       where name = v_status;
10345 		   end if;
10346 
10347 		   update CSI_ITEM_INSTANCES
10348 		   set location_type_code = 'INVENTORY'
10349 		      ,location_id = v_loc_id
10350 		      ,accounting_class_code = 'CUST_PROD'
10351 		      ,instance_usage_code = v_inst_usage_code
10352 		      ,last_vld_organization_id = v_org_id
10353 		      ,inv_organization_id = v_org_id
10354 		      ,inv_subinventory_name = v_subinv
10355 		      ,inv_locator_id = v_locator
10356 		      ,instance_status_id = v_status_id
10357 		      ,inventory_revision = v_rev
10358 		   where instance_id = v_ret_instance_id;
10359 		end if; -- Terminated_flag check
10360 	     else
10361 		-- There is no RMA instance
10362 		v_instance_id := v_inv_instance_id;
10363 	     end if;
10364 	  end if; -- check for Inv Instance
10365 	  --
10366 	  v_duplicate_count := 0;
10367 
10368 	  for j in c4(i.serial_number, i.inventory_item_id)
10369 	  loop
10370 	     if v_instance_id <> j.instance_id then
10371 		v_duplicate_count := v_duplicate_count + 1;
10372 		v_txn_id := null;
10373 		open c6(v_instance_id,j.instance_id);
10374 		fetch c6 into v_txn_id;
10375 		close c6;
10376 		--
10377                 -- While suffixing with DUP, we use the instance_id in serial_number column to avoid serial #
10378                 -- becoming more than 30 chars. The old serial_number is stored in the external_reference column.
10379 		update csi_item_instances
10380 		set     external_reference = serial_number
10381                        , serial_number = to_char(j.instance_id)||'-DUP'||to_char(v_duplicate_count)
10382 		       ,active_end_date = sysdate
10383 		       ,instance_status_id = v_instance_status_id
10384 		       ,last_updated_by = fnd_global.user_id
10385 		       ,last_update_date = sysdate
10386 		where  instance_id = j.instance_id;
10387 		--
10388 		if v_txn_id is null then
10389 		   update csi_item_instances_h
10390 		   set    instance_id = v_instance_id
10391                           ,new_external_reference = i.serial_number
10392 			  ,old_serial_number = nvl(new_serial_number,i.serial_number)
10393 			  ,new_serial_number = to_char(j.instance_id)||'-DUP'||to_char(v_duplicate_count)
10394 			  ,last_updated_by = fnd_global.user_id
10395 			  ,last_update_date = sysdate
10396 		   where  instance_id = j.instance_id;
10397 		else -- common txn found
10398 		   update csi_item_instances_h
10399 		   set  new_external_reference = i.serial_number
10400 		       ,old_serial_number = nvl(new_serial_number,i.serial_number)
10401 		       ,new_serial_number = to_char(j.instance_id)||'-DUP'||to_char(v_duplicate_count)
10402 		       ,last_updated_by = fnd_global.user_id
10403 		       ,last_update_date = sysdate
10404 		   where  instance_history_id = (select max(instance_history_id)
10405 						 from csi_item_instances_h
10406 						 where instance_id = j.instance_id
10407 						 and   creation_date < v_freeze_date);
10408 		end if;
10409 	     end if;
10410 	  end loop;
10411 
10412 	  v_duplicate_count := 0;
10413 	  v_instance_id := null;
10414 
10415 	  if v_commit_count >= 500 then
10416 	     commit;
10417 	     v_commit_count := 0;
10418 	  end if;
10419 
10420        end loop;
10421        commit;
10422     END; -- Second Update
10423     commit;
10424  END Update_Dup_Srl_Instance;
10425  --
10426   PROCEDURE Delete_Dup_Account IS
10427      CURSOR IP_CUR IS
10428      select cia.instance_party_id
10429      from csi_ip_accounts cia
10430      where cia.relationship_type_code = 'OWNER'
10431      group by cia.instance_party_id
10432      having count(*) > 1;
10433      --
10434      CURSOR ACCT_CUR(p_inst_party_id IN NUMBER,p_ip_acct_id IN NUMBER) IS
10435      select ip_account_id
10436      from csi_ip_accounts
10437      where instance_party_id = p_inst_party_id
10438      and   relationship_type_code = 'OWNER'
10439      and   ip_account_id <> p_ip_acct_id
10440      order by ip_account_id asc;
10441      --
10442      v_max_ip_acct_id      NUMBER;
10443      v_min_ret_id          NUMBER;
10444      v_old_account_id      NUMBER;
10445      v_commit_counter      NUMBER := 0;
10446      v_min_history_id      NUMBER;
10447      --
10448      Process_next          EXCEPTION;
10449   BEGIN
10450      FOR party in IP_CUR LOOP
10451 	Begin
10452 	   v_max_ip_acct_id := NULL;
10453 	   v_min_ret_id := null;
10454 	   --
10455 	   select max(ip_account_id)
10456 	   into v_max_ip_acct_id
10457 	   from csi_ip_accounts
10458 	   where instance_party_id = party.instance_party_id
10459 	   and   relationship_type_code = 'OWNER'
10460 	   and   ((active_end_date is null) or (active_end_date > sysdate));
10461 	   --
10462 	   IF v_max_ip_acct_id IS NULL THEN
10463 	      Raise Process_next;
10464 	   END IF;
10465 	   --
10466 	   select min(ip_account_history_id)
10467 	   into v_min_ret_id
10468 	   from CSI_IP_ACCOUNTS_H
10469 	   where ip_account_id = v_max_ip_acct_id;
10470 	   --
10471 	   v_old_account_id := NULL;
10472 	   --
10473 	   FOR acct in ACCT_CUR(party.instance_party_id,v_max_ip_acct_id) LOOP
10474 	      v_min_history_id := NULL;
10475 	      select min(ip_account_history_id)
10476 	      into v_min_history_id
10477 	      from CSI_IP_ACCOUNTS_H
10478 	      where ip_account_id = acct.ip_account_id;
10479 	      --
10480 	      IF v_min_history_id IS NULL THEN
10481 		 Raise Process_next;
10482 	      END IF;
10483 	      --
10484 	      IF ACCT_CUR%ROWCOUNT > 1 THEN
10485 		 Update CSI_IP_ACCOUNTS_H
10486 		 set old_party_account_id = v_old_account_id
10487 		 where ip_account_history_id = v_min_history_id;
10488 	      END IF;
10489 	      --
10490 	      DELETE from CSI_IP_ACCOUNTS_H
10491 	      where ip_account_id = acct.ip_account_id
10492 	      and   ip_account_history_id <> v_min_history_id;
10493 	      --
10494 	      v_old_account_id := NULL;
10495 	      Begin
10496 		 select new_party_account_id
10497 		 into v_old_account_id
10498 		 from CSI_IP_ACCOUNTS_H
10499 		 where ip_account_history_id = v_min_history_id;
10500 	      Exception
10501 		 when no_data_found then
10502 		    v_old_account_id := NULL;
10503 	      End;
10504 	      --
10505 	      Update CSI_IP_ACCOUNTS_H
10506 	      set ip_account_id = v_max_ip_acct_id
10507 	      where ip_account_history_id = v_min_history_id;
10508 	      --
10509 	      Delete from CSI_IP_ACCOUNTS
10510 	      where ip_account_id = acct.ip_account_id;
10511 	      --
10512 	      v_commit_counter := v_commit_counter + 1;
10513 	      IF v_commit_counter = 100 THEN
10514 		 v_commit_counter := 0;
10515 		 commit;
10516 	      END IF;
10517 	   END LOOP;
10518 	   Update CSI_IP_ACCOUNTS_H
10519 	   set old_party_account_id = v_old_account_id
10520 	   where ip_account_history_id = v_min_ret_id;
10521 	Exception
10522 	   when Process_next Then
10523 	      null;
10524 	End;
10525      END LOOP;
10526      commit;
10527   END Delete_Dup_Account;
10528   --
10529   PROCEDURE Update_Instance_Party_Source IS
10530      CURSOR CSI_INS_CUR IS
10531      SELECT instance_id,null
10532      FROM   csi_item_instances
10533      WHERE  (owner_party_source_table is null
10534      OR      owner_party_source_table  not in ('HZ_PARTIES','PO_VENDORS','EMPLOYEE','TEAM','GROUP'));
10535      --
10536      Type NumTabType is VARRAY(10000) of NUMBER;
10537      instance_id_mig         NumTabType;
10538      --
10539      Type V30TabType is VARRAY(10000) of VARCHAR2(30);
10540      party_src_table_mig     V30TabType;
10541      --
10542      MAX_BUFFER_SIZE         NUMBER := 1000;
10543 
10544   BEGIN
10545      OPEN CSI_INS_CUR;
10546      LOOP
10547 	FETCH CSI_INS_CUR BULK COLLECT INTO
10548 	instance_id_mig,
10549 	party_src_table_mig
10550 	LIMIT MAX_BUFFER_SIZE;
10551 	--
10552 	FOR i IN 1 .. instance_id_mig.count LOOP
10553 	   BEGIN
10554 	      SELECT party_source_table
10555 	      INTO   party_src_table_mig(i)
10556 	      FROM   csi_i_parties
10557 	      WHERE  relationship_type_code = 'OWNER'
10558 	      AND    instance_id = instance_id_mig(i);
10559 	   EXCEPTION
10560 	      WHEN no_data_found THEN
10561 		 party_src_table_mig(i) := null;
10562 	      WHEN OTHERS THEN
10563 		 party_src_table_mig(i) := NULL;
10564 	   END;
10565 	   --
10566 	END LOOP;
10567 	--
10568 	FORALL j in 1 .. instance_id_mig.count
10569 	   UPDATE CSI_ITEM_INSTANCES
10570 	   SET OWNER_PARTY_SOURCE_TABLE = party_src_table_mig(j)
10571 	   WHERE instance_id = instance_id_mig(j);
10572 	commit;
10573 	EXIT WHEN CSI_INS_CUR%NOTFOUND;
10574      END LOOP;
10575      CLOSE CSI_INS_CUR;
10576      COMMIT;
10577   END Update_Instance_Party_Source;
10578   --
10579   PROCEDURE Update_Contact_Party_Record IS
10580      CURSOR CSI_PARTY_CUR IS
10581      select instance_id,instance_party_id,contact_ip_id,
10582      contact_flag,relationship_type_code
10583      from CSI_I_PARTIES
10584      where contact_ip_id IS NOT NULL;
10585      --
10586      l_ins_party_id       NUMBER;
10587      TYPE NUMLIST IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
10588      l_upd_pty_tbl        NUMLIST;
10589      l_ctr                NUMBER := 0;
10590      l_exp_pty_tbl        NUMLIST;
10591      l_exp                NUMBER := 0;
10592      l_exists             VARCHAR2(1);
10593      --
10594      Type NumTabType is VARRAY(10000) of NUMBER;
10595      instance_id_mig         NumTabType;
10596      instance_party_id_mig   NumTabType;
10597      contact_ip_id_mig       NumTabType;
10598      --
10599      Type v1TabType is VARRAY(10000) of VARCHAR2(1);
10600      contact_flag_mig        v1TabType;
10601      --
10602      Type v30TabType is VARRAY(10000) of VARCHAR2(30);
10603      rel_type_code_mig       v30TabType;
10604      --
10605      MAX_BUFFER_SIZE         NUMBER := 1000;
10606   BEGIN
10607      OPEN CSI_PARTY_CUR;
10608      LOOP
10609 	FETCH CSI_PARTY_CUR BULK COLLECT INTO
10610 	instance_id_mig,
10611 	instance_party_id_mig,
10612 	contact_ip_id_mig,
10613 	contact_flag_mig,
10614 	rel_type_code_mig
10615 	LIMIT MAX_BUFFER_SIZE;
10616 	--
10617 	l_ctr := 0;
10618 	l_upd_pty_tbl.DELETE;
10619 	l_exp := 0;
10620 	l_exp_pty_tbl.DELETE;
10621 	--
10622 	FOR i in 1 .. instance_id_mig.count LOOP
10623 	   IF ((nvl(contact_flag_mig(i),'N') <> 'Y') OR
10624 	       (rel_type_code_mig(i) = 'OWNER')) THEN
10625 	      l_ctr := l_ctr + 1;
10626 	      l_upd_pty_tbl(l_ctr) := instance_party_id_mig(i);
10627 	   ELSE
10628 	      Begin
10629 		 select 'x'
10630 		 into l_exists
10631 		 from CSI_I_PARTIES
10632 		 where instance_party_id = contact_ip_id_mig(i)
10633 		 and   instance_id = instance_id_mig(i);
10634 	      Exception
10635 		 when no_data_found then
10636 		    l_exp := l_exp + 1;
10637 		    l_exp_pty_tbl(l_exp) := instance_party_id_mig(i);
10638 	      End;
10639 	   END IF;
10640 	END LOOP;
10641 	IF l_upd_pty_tbl.count > 0 THEN
10642 	   FORALL j in l_upd_pty_tbl.FIRST .. l_upd_pty_tbl.LAST
10643 	      UPDATE CSI_I_PARTIES
10644 	      set contact_ip_id = null
10645 	      where instance_party_id = l_upd_pty_tbl(j);
10646 	   --
10647 	   commit;
10648 	END IF;
10649 	--
10650 	IF l_exp_pty_tbl.count > 0 THEN
10651 	   FORALL j in l_exp_pty_tbl.FIRST .. l_exp_pty_tbl.LAST
10652 	      UPDATE CSI_I_PARTIES
10653 	      set contact_ip_id = null,
10654 		  active_end_date = sysdate
10655 	      where instance_party_id = l_exp_pty_tbl(j);
10656 	   --
10657 	   commit;
10658 	END IF;
10659 	EXIT WHEN CSI_PARTY_CUR%NOTFOUND;
10660      END LOOP;
10661      CLOSE CSI_PARTY_CUR;
10662      commit;
10663   END Update_Contact_Party_Record;
10664   --
10665   PROCEDURE Revert_Party_Rel_Type_Update IS
10666      CURSOR INS_CUR IS
10667      select instance_id,relationship_type_code,count(*)
10668      from CSI_I_PARTIES
10669      where relationship_type_code = 'OWNER'
10670      group by instance_id,relationship_type_code
10671      having count(*) > 1;
10672      --
10673      CURSOR PARTY_HIST_CUR IS
10674      select a.instance_party_history_id instance_party_history_id,a.instance_party_id instance_party_id,
10675 	    a.old_relationship_type_code old_rel_type,
10676 	    a.old_party_id old_party_id,a.new_party_id new_party_id
10677      from CSI_I_PARTIES_H a
10678      where a.old_relationship_type_code is not null
10679      and   a.old_relationship_type_code <> 'OWNER'
10680      and   a.new_relationship_type_code is not null
10681      and   a.new_relationship_type_code = 'OWNER'
10682      and   a.new_party_id is not null
10683      and   a.old_party_id is not null
10684      and   exists (select 'x' from CSI_I_PARTIES b
10685 		   where b.instance_id = (select c.instance_id from CSI_I_PARTIES c
10686 					  where c.instance_party_id = a.instance_party_id)
10687 		   and   b.relationship_type_code = 'OWNER')
10688      and   a.instance_party_history_id = (select max(instance_party_history_id)
10689 					  from CSI_I_PARTIES_H d
10690 					  where d.instance_party_id = a.instance_party_id);
10691      --
10692      TYPE NUMLIST IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
10693      l_inst_tbl        NUMLIST;
10694      l_ctr             NUMBER := 0;
10695      l_internal_party_id NUMBER;
10696      l_party_id        NUMBER;
10697      l_ip_id           NUMBER;
10698      l_ip_account_id   NUMBER;
10699      l_vld_org_id      NUMBER;
10700      x_msg_data        VARCHAR2(2000);
10701      x_msg_count       NUMBER;
10702      x_return_status   VARCHAR2(1);
10703      l_txn_id          NUMBER;
10704      l_user_id         NUMBER := FND_GLOBAL.user_id;
10705      l_ins_flag        VARCHAR2(1) := 'N';
10706      v_txn_type_id     NUMBER;
10707      px_oks_txn_inst_tbl oks_ibint_pub.txn_instance_tbl;
10708      --
10709      Process_next      EXCEPTION;
10710      comp_error        EXCEPTION;
10711    BEGIN
10712       savepoint DATA_FIX;
10713       --
10714       Begin
10715 	 select transaction_type_id
10716 	 into v_txn_type_id
10717 	 from CSI_TXN_TYPES
10718 	 where SOURCE_TRANSACTION_TYPE = 'DATA_CORRECTION';
10719       Exception
10720 	 when no_data_found then
10721 	    Raise comp_error;
10722 	 when others then
10723 	    Raise comp_error;
10724       End;
10725       --
10726       FOR ins in INS_CUR LOOP
10727 	 l_ctr := l_ctr + 1;
10728 	 l_inst_tbl(l_ctr) := ins.instance_id;
10729       END LOOP;
10730       --
10731       IF l_inst_tbl.count = 0 THEN
10732 	 RAISE Comp_error;
10733       END IF;
10734       --
10735       select CSI_TRANSACTIONS_S.nextval
10736       into l_txn_id from dual;
10737       --
10738       INSERT INTO CSI_TRANSACTIONS(
10739 	       TRANSACTION_ID
10740 	      ,TRANSACTION_DATE
10741 	      ,SOURCE_TRANSACTION_DATE
10742 	      ,SOURCE_HEADER_REF
10743 	      ,TRANSACTION_TYPE_ID
10744 	      ,CREATED_BY
10745 	      ,CREATION_DATE
10746 	      ,LAST_UPDATED_BY
10747 	      ,LAST_UPDATE_DATE
10748 	      ,LAST_UPDATE_LOGIN
10749 	      ,OBJECT_VERSION_NUMBER
10750 	      )
10751       VALUES(
10752 	     l_txn_id                             -- TRANSACTION_ID
10753 	    ,SYSDATE                              -- TRANSACTION_DATE
10754 	    ,SYSDATE                              -- SOURCE_TRANSACTION_DATE
10755 	    ,'Update Dup Owner'                   -- SOURCE_HEADER_REF
10756 	    ,v_txn_type_id                        -- TRANSACTION_TYPE_ID
10757 	    ,l_user_id
10758 	    ,sysdate
10759 	    ,l_user_id
10760 	    ,sysdate
10761 	    ,-1
10762 	    ,1
10763 	   );
10764       --
10765       IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
10766          csi_gen_utility_pvt.populate_install_param_rec;
10767       END IF;
10768       --
10769       l_internal_party_id := csi_datastructures_pub.g_install_param_rec.internal_party_id;
10770       --
10771       FOR party_rec in PARTY_HIST_CUR LOOP
10772 	 Update CSI_I_PARTIES
10773 	 set relationship_type_code = party_rec.old_rel_type,
10774 	     party_id = party_rec.old_party_id
10775 	 where instance_party_id = party_rec.instance_party_id;
10776 	 --
10777 	 Update CSI_I_PARTIES
10778 	 set party_id = party_rec.new_party_id
10779 	 where instance_id = (select instance_id from CSI_I_PARTIES
10780 			      where instance_party_id = party_rec.instance_party_id)
10781 	 and   relationship_type_code = 'OWNER';
10782 	 --
10783 	 Update CSI_I_PARTIES_H
10784 	 set new_relationship_type_code = party_rec.old_rel_type,
10785 	     new_party_id = party_rec.old_party_id
10786 	 where instance_party_history_id = party_rec.instance_party_history_id;
10787 	 --
10788       END LOOP;
10789       --
10790       IF l_inst_tbl.count > 0 THEN
10791 	 FOR j in l_inst_tbl.FIRST .. l_inst_tbl.LAST LOOP
10792 	    Begin
10793 	       Begin
10794 		  select instance_party_id
10795 		  into l_ip_id
10796 		  from CSI_I_PARTIES
10797 		  where instance_id = l_inst_tbl(j)
10798 		  and   relationship_type_code = 'OWNER'
10799 		  and   party_id = l_internal_party_id;
10800 	       Exception
10801 		  when no_data_found then
10802 		     Raise Process_next;
10803 	       End;
10804 	       --
10805 	       Update CSI_IP_ACCOUNTS
10806 	       set active_end_date = sysdate
10807 	       where instance_party_id = l_ip_id
10808 	       and   relationship_type_code = 'OWNER';
10809 	       --
10810 	       -- Insert into IP Accounts history
10811 	       select ip_account_id
10812 	       into l_ip_account_id
10813 	       from CSI_IP_ACCOUNTS
10814 	       where instance_party_id = l_ip_id
10815 	       and   relationship_type_code = 'OWNER';
10816 	       --
10817 	       INSERT INTO CSI_IP_ACCOUNTS_H
10818 		  ( IP_ACCOUNT_HISTORY_ID
10819 		   ,IP_ACCOUNT_ID
10820 		   ,TRANSACTION_ID
10821 		   ,OLD_ACTIVE_END_DATE
10822 		   ,NEW_ACTIVE_END_DATE
10823 		   ,CREATED_BY
10824 		   ,CREATION_DATE
10825 		   ,LAST_UPDATED_BY
10826 		   ,LAST_UPDATE_DATE
10827 		   ,LAST_UPDATE_LOGIN
10828 		   ,OBJECT_VERSION_NUMBER
10829 		  )
10830 	       VALUES
10831 		  ( CSI_IP_ACCOUNTS_H_S.nextval
10832 		   ,l_ip_account_id
10833 		   ,l_txn_id
10834 		   ,NULL
10835 		   ,SYSDATE
10836 		   ,l_user_id
10837 		   ,sysdate
10838 		   ,l_user_id
10839 		   ,sysdate
10840 		   ,-1
10841 		   ,1
10842 		  );
10843 	       --
10844 	       Update CSI_ITEM_INSTANCES
10845 	       set owner_party_id = l_internal_party_id,
10846 		   owner_party_account_id = null
10847 	       where instance_id = l_inst_tbl(j);
10848 	       --
10849 	       select last_vld_organization_id
10850 	       into l_vld_org_id
10851 	       from CSI_ITEM_INSTANCES
10852 	       where instance_id = l_inst_tbl(j);
10853 	       --
10854 	       CSI_Item_Instance_Pvt.Call_to_Contracts
10855 		  ( p_transaction_type    => 'TRM'
10856 		   ,p_instance_id         => l_inst_tbl(j)
10857 		   ,p_new_instance_id     => NULL
10858 		   ,p_vld_org_id          => l_vld_org_id
10859 		   ,p_quantity            => NULL
10860 		   ,p_party_account_id1   => NULL
10861 		   ,p_party_account_id2   => NULL
10862                    ,p_oks_txn_inst_tbl    => px_oks_txn_inst_tbl
10863 		   ,x_return_status       => x_return_status
10864 		   ,x_msg_count           => x_msg_count
10865 		   ,x_msg_data            => x_msg_data
10866 		 );
10867 	       --
10868 	       IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
10869 		  Raise Comp_error;
10870 	       END IF;
10871 	    Exception
10872 	       when Process_next then
10873 		  null;
10874 	    End;
10875 	 END LOOP;
10876       END IF;
10877       --
10878       IF px_oks_txn_inst_tbl.count > 0 THEN
10879          UPDATE CSI_TRANSACTIONS
10880          set contracts_invoked = 'Y'
10881          where transaction_id = l_txn_id;
10882          --
10883 	 OKS_IBINT_PUB.IB_interface
10884 	    (
10885 	      P_Api_Version           =>  1.0,
10886 	      P_init_msg_list         =>  fnd_api.g_true,
10887 	      P_single_txn_date_flag  =>  'Y',
10888 	      P_Batch_type            =>  NULL,
10889 	      P_Batch_ID              =>  NULL,
10890 	      P_OKS_Txn_Inst_tbl      =>  px_oks_txn_inst_tbl,
10891 	      x_return_status         =>  x_return_status,
10892 	      x_msg_count             =>  x_msg_count,
10893 	      x_msg_data              =>  x_msg_data
10894 	   );
10895 	 --
10896 	 IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
10897             Raise Comp_error;
10898 	 END IF;
10899       END IF;
10900       commit;
10901   EXCEPTION
10902      when comp_error then
10903         Rollback to DATA_FIX;
10904      when others then
10905         Rollback to DATA_FIX;
10906   END Revert_Party_Rel_Type_Update;
10907   --
10908   PROCEDURE Update_Master_Organization_ID IS
10909      CURSOR CSI_CUR IS
10910      select cii.instance_id,cii.inv_master_organization_id,
10911             cii.last_vld_organization_id
10912      from CSI_ITEM_INSTANCES cii
10913      where not exists (select 'x'
10914                        from MTL_PARAMETERS msi
10915                        where organization_id = inv_master_organization_id
10916                        and   master_organization_id = inv_master_organization_id);
10917      --
10918      Type NumTabType is VARRAY(10000) of NUMBER;
10919      instance_id_mig       NumTabType;
10920      inv_master_org_id_mig NumTabType;
10921      last_vld_org_id_mig   NumTabType;
10922      --
10923      MAX_BUFFER_SIZE NUMBER := 1000;
10924   BEGIN
10925      OPEN CSI_CUR;
10926      LOOP
10927         FETCH CSI_CUR BULK COLLECT INTO
10928         instance_id_mig,
10929         inv_master_org_id_mig,
10930         last_vld_org_id_mig
10931         LIMIT MAX_BUFFER_SIZE;
10932         --
10933         FOR j in 1..instance_id_mig.count LOOP
10934            Begin
10935               select master_organization_id
10936               into inv_master_org_id_mig(j)
10937               from MTL_PARAMETERS
10938               where organization_id = last_vld_org_id_mig(j);
10939            Exception
10940               when no_data_found then
10941                  inv_master_org_id_mig(j) := inv_master_org_id_mig(j);
10942            End;
10943         END LOOP;
10944         --
10945         FORALL i in 1..instance_id_mig.count
10946            UPDATE CSI_ITEM_INSTANCES
10947            set inv_master_organization_id = inv_master_org_id_mig(i)
10948            where instance_id = instance_id_mig(i);
10949         commit;
10950         --
10951         FORALL i in 1..instance_id_mig.count
10952            UPDATE CSI_ITEM_INSTANCES_H
10953            set old_inv_master_organization_id = decode(old_inv_master_organization_id,null,null,inv_master_org_id_mig(i)),
10954                new_inv_master_organization_id = decode(new_inv_master_organization_id,null,null,inv_master_org_id_mig(i))
10955            where instance_id = instance_id_mig(i);
10956         commit;
10957         --
10958         EXIT WHEN CSI_CUR%NOTFOUND;
10959      END LOOP;
10960      commit;
10961      CLOSE CSI_CUR;
10962   END Update_Master_Organization_ID;
10963   --
10964   PROCEDURE Get_Children
10965     (p_object_id     IN  NUMBER,
10966      p_rel_tbl       OUT NOCOPY csi_datastructures_pub.ii_relationship_tbl
10967     ) IS
10968     --
10969     l_rel_tbl                 csi_datastructures_pub.ii_relationship_tbl;
10970     l_rel_tbl_next_lvl        csi_datastructures_pub.ii_relationship_tbl;
10971     l_rel_tbl_temp            csi_datastructures_pub.ii_relationship_tbl;
10972     l_rel_tbl_final           csi_datastructures_pub.ii_relationship_tbl;
10973     l_next_ind                NUMBER := 0;
10974     l_final_ind               NUMBER := 0;
10975     l_ctr                     NUMBER := 0;
10976     l_found                   NUMBER;
10977   BEGIN
10978      Get_Next_Level
10979        ( p_object_id                 => p_object_id,
10980 	 p_rel_tbl                   => l_rel_tbl
10981        );
10982 
10983      <<Next_Level>>
10984 
10985      l_rel_tbl_next_lvl.delete;
10986      l_next_ind := 0;
10987      --
10988      IF l_rel_tbl.count > 0 THEN
10989 	FOR l_ind IN l_rel_tbl.FIRST .. l_rel_tbl.LAST LOOP
10990 	   l_final_ind := l_final_ind + 1;
10991 	   l_rel_tbl_final(l_final_ind) := l_rel_tbl(l_ind);
10992 	   /* get the next level using this Subject ID as the parent */
10993 	   Get_Next_Level
10994 	     ( p_object_id                 => l_rel_tbl(l_ind).subject_id,
10995 	       p_rel_tbl                   => l_rel_tbl_temp
10996 	     );
10997 	   --
10998 	   IF l_rel_tbl_temp.count > 0 THEN
10999 	      FOR l_temp_ind IN l_rel_tbl_temp.FIRST .. l_rel_tbl_temp.LAST LOOP
11000                  IF l_rel_tbl_final.count > 0 THEN
11001                     l_found := 0;
11002                     FOR i IN l_rel_tbl_final.FIRST .. l_rel_tbl_final.LAST LOOP
11003                        IF l_rel_tbl_final(i).object_id = l_rel_tbl_temp(l_temp_ind).object_id THEN
11004                           l_found := 1;
11005                           exit;
11006                        END IF;
11007                     END LOOP;
11008                  END IF;
11009                  IF l_found = 0 THEN
11010 		    l_next_ind := l_next_ind + 1;
11011 		    l_rel_tbl_next_lvl(l_next_ind) := l_rel_tbl_temp(l_temp_ind);
11012                  END IF;
11013 	      END LOOP;
11014 	   END IF;
11015 	END LOOP;
11016 	--
11017 	IF l_rel_tbl_next_lvl.count > 0 THEN
11018 	   l_rel_tbl.DELETE;
11019 	   l_rel_tbl := l_rel_tbl_next_lvl;
11020 	   --
11021 	   goto Next_Level;
11022 	END IF;
11023      END IF;
11024      --
11025      p_rel_tbl := l_rel_tbl_final;
11026      --
11027      -- The output of l_rel_tbl_final will be Breadth first search Order.
11028   END Get_Children;
11029   --
11030   PROCEDURE Delete_Dup_Relationship IS
11031      CURSOR CSI_REL_CUR IS
11032      select object_id,subject_id,relationship_type_code,count(*)
11033      from CSI_II_RELATIONSHIPS
11034      where active_end_date is null
11035      and   nvl(migrated_flag,'N') = 'Y'
11036      group by object_id,subject_id,relationship_type_code
11037      having count(*) > 1;
11038      --
11039      -- Delete the ones that are not having any non-migrated txns
11040      CURSOR CSI_REL_DEL_CUR(p_rel_id IN NUMBER
11041 			   ,p_object_id IN NUMBER
11042 			   ,p_subject_id IN NUMBER
11043 			   ,p_rel_type IN VARCHAR2) IS
11044      select cir.relationship_id
11045      from CSI_II_RELATIONSHIPS cir
11046      where cir.object_id = p_object_id
11047      and   cir.subject_id = p_subject_id
11048      and   cir.relationship_type_code = p_rel_type
11049      and   cir.active_end_date is null
11050      and   nvl(cir.migrated_flag,'N') = 'Y'
11051      and   cir.relationship_id <> p_rel_id
11052      and   not exists (select 'x' from CSI_II_RELATIONSHIPS_H cirh
11053                        where cirh.relationship_id = cir.relationship_id
11054                        and   nvl(cirh.migrated_flag,'N') = 'N');
11055      --
11056      l_ret_relationship_id      NUMBER;
11057      TYPE NUMLIST IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
11058      l_rel_id_tbl     NUMLIST;
11059      l_ctr            NUMBER := 0;
11060      --
11061      Type NumTabType is VARRAY(10000) of NUMBER;
11062      Type V30TabType is VARRAY(10000) of VARCHAR2(30);
11063      l_object_id_mig            NumTabType;
11064      l_subject_id_mig           NumTabType;
11065      l_count_mig                NumTabType;
11066      l_rel_type_code_mig        V30TabType;
11067      --
11068      MAX_BUFFER_SIZE         NUMBER := 1000;
11069   BEGIN
11070      OPEN CSI_REL_CUR;
11071      LOOP
11072         FETCH CSI_REL_CUR BULK COLLECT INTO
11073            l_object_id_mig,
11074            l_subject_id_mig,
11075            l_rel_type_code_mig,
11076            l_count_mig
11077         LIMIT MAX_BUFFER_SIZE;
11078         --
11079         FOR k in 1..l_object_id_mig.count LOOP
11080            l_ret_relationship_id := -9999;
11081            -- Try to get the relationship which has non-migrated txns
11082    	   Begin
11083 	      select max(cir.relationship_id)
11084 	      into l_ret_relationship_id
11085 	      from CSI_II_RELATIONSHIPS cir
11086 	      where cir.object_id = l_object_id_mig(k)
11087 	      and   cir.subject_id = l_subject_id_mig(k)
11088 	      and   cir.relationship_type_code = l_rel_type_code_mig(k)
11089 	      and   cir.active_end_date is null
11090 	      and   nvl(cir.migrated_flag,'N') = 'Y'
11091               and   exists (select 'x' from CSI_II_RELATIONSHIPS_H cirh
11092                             where cirh.relationship_id = cir.relationship_id
11093                             and   nvl(cirh.migrated_flag,'N') = 'N');
11094 	   End;
11095            --
11096            IF nvl(l_ret_relationship_id,-9999) = -9999 THEN
11097 	      Begin
11098 	         select max(relationship_id)
11099 	         into l_ret_relationship_id
11100 	         from CSI_II_RELATIONSHIPS
11101 	         where object_id = l_object_id_mig(k)
11102 	         and   subject_id = l_subject_id_mig(k)
11103 	         and   relationship_type_code = l_rel_type_code_mig(k)
11104 	         and   active_end_date is null
11105 	         and   nvl(migrated_flag,'N') = 'Y';
11106 	      End;
11107            END IF;
11108 	   --
11109            -- The above l_ret_relationship_id will be retained
11110            --
11111 	   FOR del_rec in CSI_REL_DEL_CUR(l_ret_relationship_id,
11112 	   			          l_object_id_mig(k),
11113 				          l_subject_id_mig(k),
11114                                           l_rel_type_code_mig(k)) LOOP
11115 	      l_ctr := l_ctr + 1;
11116 	      l_rel_id_tbl(l_ctr) := del_rec.relationship_id;
11117 	   END LOOP;
11118         END LOOP;
11119         EXIT WHEN CSI_REL_CUR%NOTFOUND;
11120      END LOOP;
11121      --
11122      IF CSI_REL_CUR%ISOPEN THEN
11123         CLOSE CSI_REL_CUR;
11124      END IF;
11125      --
11126      IF l_rel_id_tbl.count > 0 THEN
11127 	FORALL j in l_rel_id_tbl.FIRST .. l_rel_id_tbl.LAST
11128 	   DELETE FROM CSI_II_RELATIONSHIPS_H
11129 	   WHERE relationship_id = l_rel_id_tbl(j);
11130 	commit;
11131 	--
11132 	FORALL j in l_rel_id_tbl.FIRST .. l_rel_id_tbl.LAST
11133 	   DELETE FROM CSI_II_RELATIONSHIPS
11134 	   WHERE relationship_id = l_rel_id_tbl(j);
11135 	commit;
11136      END IF;
11137   END Delete_Dup_Relationship;
11138   --
11139   PROCEDURE Call_Parallel_Expire
11140       (errbuf          OUT NOCOPY VARCHAR2,
11141        retcode         OUT NOCOPY NUMBER,
11142        p_process_code  IN VARCHAR2
11143       ) IS
11144      --
11145      CURSOR EXP_CUR IS
11146      select instance_id,rowid
11147      from CSI_EXPIRE_INSTANCES_TEMP
11148      where process_code = p_process_code
11149      and   processed_flag in ('E','R');
11150      --
11151      l_vld_org          NUMBER;
11152      v_commit_count     NUMBER := 0;
11153      --
11154      x_return_status          VARCHAR2(1);
11155      x_msg_count              NUMBER;
11156      x_msg_data               VARCHAR2(2000);
11157      l_msg_index              NUMBER;
11158      l_msg_count              NUMBER;
11159      px_oks_txn_inst_tbl      oks_ibint_pub.txn_instance_tbl;
11160      --
11161      Process_next       EXCEPTION;
11162   BEGIN
11163      FOR exp IN EXP_CUR LOOP
11164         BEGIN
11165            Begin
11166               select last_vld_organization_id
11167               into l_vld_org
11168               from CSI_ITEM_INSTANCES
11169               where instance_id = exp.instance_id;
11170            Exception
11171               when no_data_found then
11172                  Raise Process_next;
11173            End;
11174            --
11175 	   CSI_Item_Instance_Pvt.Call_to_Contracts
11176 	      ( p_transaction_type    => 'TRM'
11177 	       ,p_instance_id         => exp.instance_id
11178 	       ,p_new_instance_id     => NULL
11179 	       ,p_vld_org_id          => l_vld_org
11180 	       ,p_quantity            => NULL
11181 	       ,p_party_account_id1   => NULL
11182 	       ,p_party_account_id2   => NULL
11183                ,p_oks_txn_inst_tbl    => px_oks_txn_inst_tbl
11184 	       ,x_return_status       => x_return_status
11185 	       ,x_msg_count           => x_msg_count
11186 	       ,x_msg_data            => x_msg_data
11187 	      );
11188 	      --
11189            IF x_return_status <> FND_API.G_RET_STS_SUCCESS THEN
11190               UPDATE CSI_EXPIRE_INSTANCES_TEMP
11191               set processed_flag = 'E',
11192                   error_message = x_msg_data
11193               where rowid = exp.rowid;
11194            ELSE
11195               UPDATE CSI_EXPIRE_INSTANCES_TEMP
11196               set processed_flag = 'P',
11197                   error_message = null
11198               where rowid = exp.rowid;
11199            END IF;
11200            --
11201            v_commit_count := v_commit_count + 1;
11202            IF v_commit_count = 500 THEN
11203               v_commit_count := 0;
11204               commit;
11205            END IF;
11206         EXCEPTION
11207            WHEN Process_next THEN
11208               NULL;
11209         END;
11210      END LOOP;
11211      commit;
11212      --
11213      IF px_oks_txn_inst_tbl.count > 0 THEN
11214 	OKS_IBINT_PUB.IB_interface
11215 	   (
11216 	     P_Api_Version           =>  1.0,
11217 	     P_init_msg_list         =>  fnd_api.g_true,
11218 	     P_single_txn_date_flag  =>  'Y',
11219 	     P_Batch_type            =>  NULL,
11220 	     P_Batch_ID              =>  NULL,
11221 	     P_OKS_Txn_Inst_tbl      =>  px_oks_txn_inst_tbl,
11222 	     x_return_status         =>  x_return_status,
11223 	     x_msg_count             =>  x_msg_count,
11224 	     x_msg_data              =>  x_msg_data
11225 	  );
11226 	--
11227      END IF;
11228   END Call_Parallel_Expire;
11229   --
11230   PROCEDURE Expire_Non_Trackable_Instance IS
11231      CURSOR CSI_CUR IS
11232      select cii.instance_id,cii.inventory_item_id,cii.inv_master_organization_id
11233      from CSI_ITEM_INSTANCES cii
11234      where nvl(cii.active_end_date,(sysdate+1)) > sysdate;
11235      --
11236      CURSOR REL_CUR IS
11237      select cii.instance_id,cii.inventory_item_id,cii.inv_master_organization_id,
11238             cir.relationship_id
11239      from   CSI_ITEM_INSTANCES cii,
11240             CSI_II_RELATIONSHIPS cir
11241      where  nvl(cii.active_end_date,(sysdate+1)) > sysdate
11242      and    cir.subject_id = cii.instance_id
11243      and   cir.relationship_type_code = 'COMPONENT-OF'
11244      and   nvl(cir.active_end_date,(sysdate+1)) > sysdate;
11245      --
11246      CURSOR CSI_MAT_ERROR IS
11247      select cii.transaction_error_id,
11248      mmt.inventory_item_id,mmt.organization_id
11249      from CSI_TXN_ERRORS cii,
11250           MTL_MATERIAL_TRANSACTIONS mmt
11251      where cii.processed_flag in ('E', 'R')
11252      and   cii.inv_material_transaction_id is not null
11253      and   mmt.transaction_id = cii.inv_material_transaction_id;
11254      --
11255      CURSOR CSI_NON_MAT_ERROR IS
11256      select transaction_error_id,source_id
11257      from CSI_TXN_ERRORS cii
11258      where cii.inv_material_transaction_id is null
11259      and   cii.source_id is not null
11260      and   cii.processed_flag in ('E', 'R');
11261      --
11262      v_commit_count           NUMBER := 0;
11263      v_err_msg                VARCHAR2(2000);
11264      v_txn_type_id            NUMBER;
11265      v_txn_id                 NUMBER;
11266      v_user_id                NUMBER := fnd_global.user_id;
11267      l_nl                     VARCHAR2(1);
11268      l_item_id                NUMBER;
11269      l_org_id                 NUMBER;
11270      l_organization_id        NUMBER;
11271      --
11272      TYPE NUMLIST IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
11273      l_upd_txn_tbl           NUMLIST;
11274      l_upd_count             NUMBER := 0;
11275      l_rel_count             NUMBER := 0;
11276      l_rel_tbl               NUMLIST;
11277      l_rel_code              VARCHAR2(30) := 'COMPONENT-OF';
11278      l_ctr                   NUMBER := 0;
11279      l_inst_tbl              NUMLIST;
11280      l_inst_hist_tbl         NUMLIST;
11281      l_req_count             NUMBER := 0;
11282      l_req_tbl               NUMLIST;
11283      v_request_id            NUMBER;
11284      l_child_tbl             csi_datastructures_pub.ii_relationship_tbl;
11285      l_contracts_status      VARCHAR2(3);
11286      l_ins_flag              VARCHAR2(1) := 'N';
11287      --
11288      Type NumTabType is VARRAY(10000) of NUMBER;
11289      instance_id_mig         NumTabType;
11290      inventory_item_id_mig   NumTabType;
11291      mast_org_id_mig         NumTabType;
11292      relationship_id_mig     NumTabType;
11293      organization_id_mig     NumTabType;
11294      txn_error_id_mig        NumTabType;
11295      source_id_mig           NumTabType;
11296      --
11297      MAX_BUFFER_SIZE         NUMBER := 1000;
11298      INST_BUFFER_SIZE        NUMBER := 1000;
11299      --
11300      comp_error              EXCEPTION;
11301      Process_next            EXCEPTION;
11302   BEGIN
11303      DELETE FROM CSI_EXPIRE_INSTANCES_TEMP
11304      WHERE processed_flag = 'P'
11305      AND   process_code = 'EXPNL';
11306      commit;
11307      --
11308      -- Get the Transaction Type ID for Txn Type MIGRATED
11309      Begin
11310         select transaction_type_id
11311         into v_txn_type_id
11312         from CSI_TXN_TYPES
11313         where SOURCE_TRANSACTION_TYPE = 'DATA_CORRECTION';
11314      Exception
11315         when no_data_found then
11316            v_err_msg := 'Txn Type DATA_CORRECTION not defined in CSI_TXN_TYPES';
11317            Raise comp_error;
11318         when others then
11319            v_err_msg := 'Unable to get the ID for Txn Type DATA_CORRECTION from CSI_TXN_TYPES';
11320            Raise comp_error;
11321      End;
11322      -- End data the relationships
11323      OPEN REL_CUR;
11324      LOOP
11325         FETCH REL_CUR BULK COLLECT INTO
11326         instance_id_mig,
11327         inventory_item_id_mig,
11328         mast_org_id_mig,
11329         relationship_id_mig
11330         LIMIT MAX_BUFFER_SIZE;
11331         --
11332         FOR j in 1..instance_id_mig.count LOOP
11333            Begin
11334               Begin
11335                  select nvl(comms_nl_trackable_flag,'N')
11336                  into l_nl
11337                  from MTL_SYSTEM_ITEMS
11338                  where inventory_item_id = inventory_item_id_mig(j)
11339                  and   organization_id = mast_org_id_mig(j);
11340               Exception
11341                  when no_data_found then
11342                     Raise Process_next;
11343               End;
11344               IF l_nl <> 'Y' THEN
11345                  l_rel_count := l_rel_count + 1;
11346                  l_rel_tbl(l_rel_count) := relationship_id_mig(j);
11347               END IF;
11348            Exception
11349               when Process_next then
11350                  null;
11351            End;
11352         END LOOP;
11353         EXIT WHEN REL_CUR%NOTFOUND;
11354      END LOOP;
11355      CLOSE REL_CUR;
11356      --
11357      IF REL_CUR%ISOPEN THEN
11358         CLOSE REL_CUR;
11359      END IF;
11360      -- Update the Relationship end_date.
11361      IF l_rel_tbl.count > 0 THEN
11362         FORALL i in l_rel_tbl.FIRST .. l_rel_tbl.LAST
11363            UPDATE CSI_II_RELATIONSHIPS
11364            set active_end_date = sysdate
11365            where relationship_id = l_rel_tbl(i);
11366         --
11367         commit;
11368      END IF;
11369      --
11370      select CSI_TRANSACTIONS_S.nextval
11371      into v_txn_id from dual;
11372      --
11373      OPEN CSI_CUR;
11374      LOOP
11375         FETCH CSI_CUR BULK COLLECT INTO
11376         instance_id_mig,
11377         inventory_item_id_mig,
11378         mast_org_id_mig
11379         LIMIT INST_BUFFER_SIZE;
11380         --
11381         l_ctr := 0;
11382         l_inst_tbl.DELETE;
11383         l_inst_hist_tbl.DELETE;
11384         --
11385         FOR i in 1 .. instance_id_mig.count LOOP
11386            Begin
11387               Begin
11388                  select nvl(comms_nl_trackable_flag,'N')
11389                  into l_nl
11390                  from MTL_SYSTEM_ITEMS
11391                  where inventory_item_id = inventory_item_id_mig(i)
11392                  and   organization_id = mast_org_id_mig(i);
11393               Exception
11394                  when no_data_found then
11395                     Raise Process_next;
11396               End;
11397               IF l_nl = 'Y' THEN
11398                  Raise Process_next;
11399               END IF;
11400               --
11401               l_ctr := l_ctr + 1;
11402               l_inst_tbl(l_ctr) := instance_id_mig(i);
11403               --
11404               select CSI_ITEM_INSTANCES_H_S.nextval
11405               into l_inst_hist_tbl(l_ctr) from dual;
11406               --
11407               -- For this instance_id get the children
11408               l_child_tbl.DELETE;
11409               --
11410               Get_Children
11411                  ( p_object_id     => instance_id_mig(i),
11412                    p_rel_tbl       => l_child_tbl
11413                  );
11414               --
11415               IF l_child_tbl.count > 0 THEN
11416                  FOR rel_count in l_child_tbl.FIRST .. l_child_tbl.LAST LOOP
11417                     l_ctr := l_ctr + 1;
11418                     l_inst_tbl(l_ctr) := l_child_tbl(rel_count).subject_id;
11419                     --
11420                     select CSI_ITEM_INSTANCES_H_S.nextval
11421                     into l_inst_hist_tbl(l_ctr) from dual;
11422                  END LOOP;
11423               END IF;
11424            Exception
11425               when Process_next then
11426                  null;
11427            End;
11428         END LOOP;
11429            IF l_inst_tbl.count > 0 THEN
11430               l_ins_flag := 'Y';
11431               FORALL j in l_inst_tbl.FIRST .. l_inst_tbl.LAST
11432                  INSERT INTO CSI_EXPIRE_INSTANCES_TEMP
11433                     ( INSTANCE_ID,
11434                       PROCESSED_FLAG,
11435                       PROCESS_CODE
11436                     )
11437                  VALUES
11438                    ( l_inst_tbl(j),
11439                      'R',
11440                      'EXPNL'
11441                    );
11442               -- Bulk Update Instances
11443               FORALL j in l_inst_tbl.FIRST .. l_inst_tbl.LAST
11444                  UPDATE CSI_ITEM_INSTANCES
11445                  set active_end_date = sysdate,
11446                      instance_status_id = 1,
11447                      last_update_date = sysdate,
11448                      last_updated_by = v_user_id
11449                  where instance_id = l_inst_tbl(j);
11450               --
11451               -- Tie the Transaction to the history
11452               FORALL j in l_inst_tbl.FIRST .. l_inst_tbl.LAST
11453 		 INSERT INTO CSI_ITEM_INSTANCES_H
11454 		     (
11455 		      INSTANCE_HISTORY_ID
11456 		     ,TRANSACTION_ID
11457 		     ,INSTANCE_ID
11458 		     ,CREATION_DATE
11459 		     ,LAST_UPDATE_DATE
11460 		     ,CREATED_BY
11461 		     ,LAST_UPDATED_BY
11462 		     ,LAST_UPDATE_LOGIN
11463 		     ,OBJECT_VERSION_NUMBER
11464 		    )
11465 		 VALUES
11466 		    (
11467 		      l_inst_hist_tbl(j)
11468 		     ,v_txn_id
11469 		     ,l_inst_tbl(j)
11470 		     ,SYSDATE
11471 		     ,SYSDATE
11472 		     ,v_user_id
11473 		     ,v_user_id
11474 		     ,-1
11475 		     ,1
11476 		    );
11477            END IF;
11478            commit;
11479            EXIT WHEN CSI_CUR%NOTFOUND;
11480      END LOOP;
11481      CLOSE CSI_CUR;
11482      -- Insert one record into CSI_TRANSACTIONS
11483      IF l_ins_flag = 'Y' THEN
11484 	   INSERT INTO CSI_TRANSACTIONS(
11485 	    TRANSACTION_ID
11486 	   ,TRANSACTION_DATE
11487 	   ,SOURCE_TRANSACTION_DATE
11488 	   ,SOURCE_HEADER_REF
11489 	   ,SOURCE_LINE_REF
11490 	   ,TRANSACTION_TYPE_ID
11491 	   ,CREATED_BY
11492 	   ,CREATION_DATE
11493 	   ,LAST_UPDATED_BY
11494 	   ,LAST_UPDATE_DATE
11495 	   ,LAST_UPDATE_LOGIN
11496 	   ,OBJECT_VERSION_NUMBER
11497 	  )
11498 	  VALUES(
11499 	    v_txn_id                             -- TRANSACTION_ID
11500 	   ,SYSDATE                              -- TRANSACTION_DATE
11501 	   ,SYSDATE                              -- SOURCE_TRANSACTION_DATE
11502 	   ,'COMMS_NL_TRACKABLE_FLAG got switched off'   -- SOURCE_HEADER_REF
11503 	   ,'DATAFIX By Expire Non_NL Trackable' -- SOURCE_LINE_REF
11504 	   ,v_txn_type_id                        -- TRANSACTION_TYPE_ID
11505 	   ,v_user_id
11506 	   ,sysdate
11507 	   ,v_user_id
11508 	   ,sysdate
11509 	   ,-1
11510 	   ,1
11511 	  );
11512      END IF;
11513      commit;
11514      --
11515      IF CSI_CUR%ISOPEN THEN
11516         CLOSE CSI_CUR;
11517      END IF;
11518      -- Call Expire_Item_Instance wraper to Expire Contracts
11519      l_contracts_status := FND_PROFILE.VALUE('CSI_CONTRACTS_ENABLED');
11520      IF UPPER(l_contracts_status) = 'Y'  THEN
11521         UPDATE CSI_TRANSACTIONS
11522         set contracts_invoked = 'Y'
11523         where transaction_id = v_txn_id;
11524         --
11525         v_request_id := 0;
11526         v_request_id := fnd_request.submit_request
11527                            (
11528                               'CSI'
11529                              ,'CSIEXPIR'
11530                              ,NULL
11531                              ,SYSDATE
11532                              ,FALSE
11533                              ,'EXPNL'
11534                            );
11535         commit;
11536      END IF; -- Contracts enabled check
11537      commit;
11538      -- Update the Relationship end_date back to null.
11539      IF l_rel_tbl.count > 0 THEN
11540         FORALL i in l_rel_tbl.FIRST .. l_rel_tbl.LAST
11541            UPDATE CSI_II_RELATIONSHIPS
11542            set active_end_date = null
11543            where relationship_id = l_rel_tbl(i);
11544         --
11545         commit;
11546      END IF;
11547      commit;
11548      --
11549      OPEN CSI_MAT_ERROR;
11550      LOOP
11551         FETCH CSI_MAT_ERROR BULK COLLECT INTO
11552         txn_error_id_mig,
11553         inventory_item_id_mig,
11554         organization_id_mig
11555         LIMIT MAX_BUFFER_SIZE;
11556         --
11557         FOR i in 1 .. txn_error_id_mig.count LOOP
11558            select comms_nl_trackable_flag
11559            into l_nl
11560            from MTL_SYSTEM_ITEMS msi,
11561                 MTL_PARAMETERS mp
11562            where mp.organization_id = organization_id_mig(i)
11563            and   msi.inventory_item_id = inventory_item_id_mig(i)
11564            and   msi.organization_id = mp.master_organization_id;
11565            --
11566            IF nvl(l_nl,'N') <> 'Y' THEN
11567               l_upd_count := l_upd_count + 1;
11568               l_upd_txn_tbl(l_upd_count) := txn_error_id_mig(i);
11569            END IF;
11570         END LOOP;
11571         EXIT WHEN CSI_MAT_ERROR%NOTFOUND;
11572      END LOOP;
11573      CLOSE CSI_MAT_ERROR;
11574      --
11575      -- Perform a Bulk Update
11576      IF l_upd_txn_tbl.count > 0 THEN
11577        FORALL i in l_upd_txn_tbl.FIRST .. l_upd_txn_tbl.LAST
11578          UPDATE csi_txn_errors
11579          SET    processed_flag   = 'D',
11580                 error_text       = 'COMMS_NL_TRACKABLE_FLAG got switched off',
11581                 last_update_date = sysdate
11582          WHERE transaction_error_id = l_upd_txn_tbl(i);
11583        commit;
11584      END IF;
11585      --
11586      --
11587      l_upd_txn_tbl.DELETE;
11588      l_upd_count := 0;
11589      --
11590      OPEN CSI_NON_MAT_ERROR;
11591      LOOP
11592         FETCH CSI_NON_MAT_ERROR BULK COLLECT INTO
11593         txn_error_id_mig,source_id_mig
11594         LIMIT MAX_BUFFER_SIZE;
11595         --
11596         FOR i in 1 .. txn_error_id_mig.count LOOP
11597            BEGIN
11598 	      Begin
11599 		 select oel.inventory_item_id,oel.org_id
11600 		 into l_item_id, l_org_id
11601 		 from OE_ORDER_LINES_ALL oel
11602 		 where oel.line_id = source_id_mig(i);
11603 
11604                  l_organization_id := oe_sys_parameters.value(
11605                                         param_name => 'MASTER_ORGANIZATION_ID',
11606                                         p_org_id   => l_org_id);
11607 
11608 	      Exception
11609 		 when no_data_found then
11610 		    Raise Process_next;
11611 	      End;
11612 	      --
11613 	      Begin
11614 		 select comms_nl_trackable_flag
11615 		 into l_nl
11616 		 from MTL_SYSTEM_ITEMS msi,
11617                       MTL_PARAMETERS mp
11618 		 where mp.organization_id = l_organization_id
11619                  and   msi.inventory_item_id = l_item_id
11620 		 and   msi.organization_id = mp.master_organization_id;
11621 	      Exception
11622 		 when no_data_found then
11623 		    Raise Process_next;
11624 	      End;
11625 	      --
11626 	      IF nvl(l_nl,'N') <> 'Y' THEN
11627 	         l_upd_count := l_upd_count + 1;
11628 		 l_upd_txn_tbl(l_upd_count) := txn_error_id_mig(i);
11629 	      END IF;
11630            EXCEPTION
11631               WHEN Process_next then
11632                  NULL;
11633            END;
11634         END LOOP;
11635         EXIT WHEN CSI_NON_MAT_ERROR%NOTFOUND;
11636      END LOOP;
11637      CLOSE CSI_NON_MAT_ERROR;
11638      -- Perform a Bulk Update
11639      IF l_upd_txn_tbl.count > 0 THEN
11640         FORALL i in l_upd_txn_tbl.FIRST .. l_upd_txn_tbl.LAST
11641            UPDATE CSI_TXN_ERRORS
11642            set processed_flag = 'D',
11643            error_text = 'COMMS_NL_TRACKABLE_FLAG got switched off',
11644            last_update_date = sysdate
11645            where transaction_error_id = l_upd_txn_tbl(i);
11646         commit;
11647      END IF;
11648      --
11649      IF CSI_NON_MAT_ERROR%ISOPEN THEN
11650         CLOSE CSI_NON_MAT_ERROR;
11651      END IF;
11652      commit;
11653   EXCEPTION
11654      WHEN comp_error then
11655         NULL;
11656   END Expire_Non_Trackable_Instance;
11657   --
11658   -- The following procedure identifies the instances that got a srl/lot number without the
11659   -- respective control and nullify the srl/lot number.
11660   -- The history will be populated with the old srl/lot number and new value will be null.
11661   -- For Inventory instances, we just expire them
11662   --
11663   PROCEDURE Update_No_ctl_Srl_Lot_Inst IS
11664   /* commenting the following cursors for the bug 5989350 ,Since there is
11665      no need to nullify the serial and lot number for the instances
11666      which are already shipped to customers */
11667   /* CURSOR CSI_SRL_CUR IS
11668      select cii.instance_id,cii.serial_number,
11669             cii.last_vld_organization_id,cii.inventory_item_id
11670      from CSI_ITEM_INSTANCES cii
11671      where cii.serial_number is not null
11672      and   cii.last_vld_organization_id is not null
11673      and   cii.inv_organization_id is null
11674      and   cii.inv_subinventory_name is null; -- to filter Inventory instances
11675      --
11676      CURSOR CSI_LOT_CUR IS
11677      select cii.instance_id,cii.lot_number,
11678             cii.last_vld_organization_id,cii.inventory_item_id
11679      from CSI_ITEM_INSTANCES cii
11680      where cii.lot_number is not null
11681      and   cii.last_vld_organization_id is not null
11682      and   cii.inv_organization_id is null
11683      and   cii.inv_subinventory_name is null; -- to filter Inventory instances
11684      --
11685   */
11686      CURSOR INV_CSI_SRL_CUR IS
11687      select cii.instance_id,cii.serial_number,
11688             cii.last_vld_organization_id,cii.inventory_item_id
11689      from CSI_ITEM_INSTANCES cii
11690      where cii.serial_number is not null
11691      and   cii.last_vld_organization_id is not null
11692      and   cii.location_type_code = 'INVENTORY'
11693      and   cii.instance_usage_code = 'IN_INVENTORY'
11694      and   nvl(cii.active_end_date,(sysdate+1)) > sysdate
11695      and   cii.inv_organization_id is not null
11696      and   cii.inv_subinventory_name is not null; -- to handle Inventory instances
11697      --
11698 	  -- Commented for bug#14835893
11699     /* CURSOR INV_CSI_LOT_CUR IS
11700      select cii.instance_id,cii.lot_number,
11701             cii.last_vld_organization_id,cii.inventory_item_id
11702      from CSI_ITEM_INSTANCES cii
11703      where cii.lot_number is not null
11704      and   cii.last_vld_organization_id is not null
11705      and   cii.inv_organization_id is not null
11706      and   cii.location_type_code = 'INVENTORY'
11707      and   cii.instance_usage_code = 'IN_INVENTORY'
11708      and   nvl(cii.active_end_date,(sysdate+1)) > sysdate
11709      and   cii.inv_subinventory_name is not null; */ -- to handle Inventory instances
11710      --
11711      v_commit_count         NUMBER := 0;
11712      v_err_msg              VARCHAR2(2000);
11713      v_txn_type_id          NUMBER;
11714      v_srl_txn_id           NUMBER;
11715      v_lot_txn_id           NUMBER;
11716      v_user_id              NUMBER := fnd_global.user_id;
11717      v_srl_ctl              NUMBER;
11718      v_lot_ctl              NUMBER;
11719      --
11720      TYPE NUMLIST IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
11721      TYPE T_V30 IS TABLE OF VARCHAR2(30) INDEX BY BINARY_INTEGER;
11722      TYPE T_V80 IS TABLE OF VARCHAR2(80) INDEX BY BINARY_INTEGER;
11723      --
11724      l_ctr                   NUMBER := 0;
11725      l_inst_tbl              NUMLIST;
11726      l_srl_tbl               T_V30;
11727      l_lot_tbl               T_V80;
11728      l_inst_hist_tbl         NUMLIST;
11729      l_srl_ins_flag          VARCHAR2(1) := 'N';
11730      l_lot_ins_flag          VARCHAR2(1) := 'N';
11731      --
11732      Type NumTabType is VARRAY(10000) of NUMBER;
11733      Type V30TabType is VARRAY(10000) of VARCHAR2(30);
11734      Type V80TabType is VARRAY(10000) of VARCHAR2(80); --bnarayan added for lot number change
11735      --
11736      instance_id_mig         NumTabType;
11737      vld_org_id_mig          NumTabType;
11738      inventory_item_id_mig   NumTabType;
11739      serial_number_mig       V30TabType;
11740      lot_number_mig          V80TabType;
11741      relationship_id_mig     NumTabType;
11742      organization_id_mig     NumTabType;
11743      --
11744      MAX_BUFFER_SIZE         NUMBER := 1000;
11745      INST_BUFFER_SIZE        NUMBER := 1000;
11746      --
11747      comp_error              EXCEPTION;
11748   BEGIN
11749      -- Get the Transaction Type ID for Txn Type DATA_CORRECTION
11750      Begin
11751 	select transaction_type_id
11752 	into v_txn_type_id
11753 	from CSI_TXN_TYPES
11754 	where SOURCE_TRANSACTION_TYPE = 'DATA_CORRECTION';
11755      Exception
11756 	when no_data_found then
11757 	   v_err_msg := 'Txn Type DATA_CORRECTION not defined in CSI_TXN_TYPES';
11758 	   Raise comp_error;
11759 	when others then
11760 	   v_err_msg := 'Unable to get the ID for Txn Type DATA_CORRECTION from CSI_TXN_TYPES';
11761 	   Raise comp_error;
11762      End;
11763      --
11764      select CSI_TRANSACTIONS_S.nextval
11765      into v_srl_txn_id from dual;
11766      --
11767      select CSI_TRANSACTIONS_S.nextval
11768      into v_lot_txn_id from dual;
11769      --
11770      /* commenting the following cursors for the bug 5989350 ,Since there is
11771      no need to nullify the serial and lot number for the instances
11772      which are already shipped to customers */
11773      /*OPEN CSI_SRL_CUR;
11774      LOOP
11775         FETCH CSI_SRL_CUR BULK COLLECT INTO
11776         instance_id_mig,
11777         serial_number_mig,
11778         vld_org_id_mig,
11779         inventory_item_id_mig
11780         LIMIT INST_BUFFER_SIZE;
11781         --
11782         l_ctr := 0;
11783         l_inst_tbl.DELETE;
11784         l_srl_tbl.DELETE;
11785         l_inst_hist_tbl.DELETE;
11786         --
11787         FOR i in 1 .. instance_id_mig.count LOOP
11788            v_srl_ctl := null;
11789            Begin
11790               select serial_number_control_code
11791               into v_srl_ctl
11792               from MTL_SYSTEM_ITEMS
11793               where inventory_item_id = inventory_item_id_mig(i)
11794               and   organization_id = vld_org_id_mig(i);
11795            Exception
11796               when others then
11797                  v_srl_ctl := null;
11798            End;
11799            IF nvl(v_srl_ctl,1) = 1 THEN
11800               l_ctr := l_ctr + 1;
11801               l_inst_tbl(l_ctr) := instance_id_mig(i);
11802               l_srl_tbl(l_ctr) := serial_number_mig(i);
11803               --
11804 	         select CSI_ITEM_INSTANCES_H_S.nextval
11805 	         into l_inst_hist_tbl(l_ctr) from dual;
11806            END IF;
11807 	      --
11808         END LOOP;
11809         IF l_inst_tbl.count > 0 THEN
11810 	   -- Bulk Update Instances
11811            l_srl_ins_flag := 'Y';
11812 	      FORALL j in l_inst_tbl.FIRST .. l_inst_tbl.LAST
11813 	      UPDATE CSI_ITEM_INSTANCES
11814 	      set serial_number = null,
11815                mfg_serial_number_flag = 'N',
11816 		     last_update_date = sysdate,
11817 		     last_updated_by = v_user_id
11818 	      where instance_id = l_inst_tbl(j);
11819 	      --
11820 	      -- Tie the Transaction to the history
11821 	      FORALL j in l_inst_tbl.FIRST .. l_inst_tbl.LAST
11822 	      INSERT INTO CSI_ITEM_INSTANCES_H
11823 		  (
11824 		   INSTANCE_HISTORY_ID
11825 		  ,TRANSACTION_ID
11826 		  ,INSTANCE_ID
11827             ,OLD_SERIAL_NUMBER
11828             ,NEW_SERIAL_NUMBER
11829 		  ,CREATION_DATE
11830 		  ,LAST_UPDATE_DATE
11831 		  ,CREATED_BY
11832 		  ,LAST_UPDATED_BY
11833 		  ,LAST_UPDATE_LOGIN
11834 		  ,OBJECT_VERSION_NUMBER
11835 		 )
11836 	      VALUES
11837 		 (
11838 		   l_inst_hist_tbl(j)
11839 		  ,v_srl_txn_id
11840 		  ,l_inst_tbl(j)
11841             ,l_srl_tbl(j)
11842             ,NULL
11843 		  ,SYSDATE
11844 		  ,SYSDATE
11845 		  ,v_user_id
11846 		  ,v_user_id
11847 		  ,-1
11848 		  ,1
11849 		 );
11850         END IF;
11851         commit;
11852         EXIT WHEN CSI_SRL_CUR%NOTFOUND;
11853      END LOOP;
11854      commit;
11855      CLOSE CSI_SRL_CUR;
11856      --
11857      OPEN CSI_LOT_CUR;
11858      LOOP
11859         FETCH CSI_LOT_CUR BULK COLLECT INTO
11860         instance_id_mig,
11861         lot_number_mig,
11862         vld_org_id_mig,
11863         inventory_item_id_mig
11864         LIMIT INST_BUFFER_SIZE;
11865         --
11866         l_ctr := 0;
11867         l_inst_tbl.DELETE;
11868         l_lot_tbl.DELETE;
11869         l_inst_hist_tbl.DELETE;
11870         --
11871         FOR i in 1 .. instance_id_mig.count LOOP
11872            v_lot_ctl := null;
11873            Begin
11874               select lot_control_code
11875               into v_lot_ctl
11876               from MTL_SYSTEM_ITEMS
11877               where inventory_item_id = inventory_item_id_mig(i)
11878               and   organization_id = vld_org_id_mig(i);
11879            Exception
11880               when others then
11881                  v_lot_ctl := null;
11882            End;
11883            IF nvl(v_lot_ctl,1) = 1 THEN
11884               l_ctr := l_ctr + 1;
11885               l_inst_tbl(l_ctr) := instance_id_mig(i);
11886               l_lot_tbl(l_ctr) := lot_number_mig(i);
11887 	         --
11888 	         select CSI_ITEM_INSTANCES_H_S.nextval
11889 	         into l_inst_hist_tbl(l_ctr) from dual;
11890            END IF;
11891         END LOOP;
11892         --
11893         IF l_inst_tbl.count > 0 THEN
11894            l_lot_ins_flag := 'Y';
11895 	      -- Bulk Update Instances
11896 	      FORALL j in l_inst_tbl.FIRST .. l_inst_tbl.LAST
11897 	      UPDATE CSI_ITEM_INSTANCES
11898 	      set lot_number = null,
11899 		     last_update_date = sysdate,
11900 		     last_updated_by = v_user_id
11901 	      where instance_id = l_inst_tbl(j);
11902 	      --
11903 	      -- Tie the Transaction to the history
11904 	      FORALL j in l_inst_tbl.FIRST .. l_inst_tbl.LAST
11905 	      INSERT INTO CSI_ITEM_INSTANCES_H
11906 		  (
11907 		   INSTANCE_HISTORY_ID
11908 		  ,TRANSACTION_ID
11909 		  ,INSTANCE_ID
11910                   ,OLD_LOT_NUMBER
11911                   ,NEW_LOT_NUMBER
11912 		  ,CREATION_DATE
11913 		  ,LAST_UPDATE_DATE
11914 		  ,CREATED_BY
11915 		  ,LAST_UPDATED_BY
11916 		  ,LAST_UPDATE_LOGIN
11917 		  ,OBJECT_VERSION_NUMBER
11918 		 )
11919 	      VALUES
11920 		 (
11921 		   l_inst_hist_tbl(j)
11922 		  ,v_lot_txn_id
11923 		  ,l_inst_tbl(j)
11924                   ,l_lot_tbl(j)
11925                   ,NULL
11926 		  ,SYSDATE
11927 		  ,SYSDATE
11928 		  ,v_user_id
11929 		  ,v_user_id
11930 		  ,-1
11931 		  ,1
11932 		 );
11933         END IF;
11934         commit;
11935         EXIT WHEN CSI_LOT_CUR%NOTFOUND;
11936      END LOOP;
11937      commit;
11938      CLOSE CSI_LOT_CUR;*/
11939      --
11940      OPEN INV_CSI_SRL_CUR;
11941      LOOP
11942         FETCH INV_CSI_SRL_CUR BULK COLLECT INTO
11943         instance_id_mig,
11944         serial_number_mig,
11945         vld_org_id_mig,
11946         inventory_item_id_mig
11947         LIMIT INST_BUFFER_SIZE;
11948         --
11949         l_ctr := 0;
11950         l_inst_tbl.DELETE;
11951         l_inst_hist_tbl.DELETE;
11952         --
11953         FOR i in 1 .. instance_id_mig.count LOOP
11954            v_srl_ctl := null;
11955            Begin
11956               select serial_number_control_code
11957               into v_srl_ctl
11958               from MTL_SYSTEM_ITEMS
11959               where inventory_item_id = inventory_item_id_mig(i)
11960               and   organization_id = vld_org_id_mig(i);
11961            Exception
11962               when others then
11963                  v_srl_ctl := null;
11964            End;
11965            IF nvl(v_srl_ctl,1) = 1 THEN
11966               l_ctr := l_ctr + 1;
11967               l_inst_tbl(l_ctr) := instance_id_mig(i);
11968 	         --
11969 	         select CSI_ITEM_INSTANCES_H_S.nextval
11970 	         into l_inst_hist_tbl(l_ctr) from dual;
11971            END IF;
11972         END LOOP;
11973         IF l_inst_tbl.count > 0 THEN
11974 	   -- Bulk Update Instances
11975            l_srl_ins_flag := 'Y';
11976 	      FORALL j in l_inst_tbl.FIRST .. l_inst_tbl.LAST
11977 	      UPDATE CSI_ITEM_INSTANCES
11978 	      set active_end_date = sysdate,
11979                instance_status_id = 1,
11980 		     last_update_date = sysdate,
11981 		     last_updated_by = v_user_id
11982 	      where instance_id = l_inst_tbl(j);
11983 	      --
11984 	      -- Tie the Transaction to the history
11985 	      FORALL j in l_inst_tbl.FIRST .. l_inst_tbl.LAST
11986 	      INSERT INTO CSI_ITEM_INSTANCES_H
11987 		  (
11988 		   INSTANCE_HISTORY_ID
11989 		  ,TRANSACTION_ID
11990 		  ,INSTANCE_ID
11991 		  ,CREATION_DATE
11992 		  ,LAST_UPDATE_DATE
11993 		  ,CREATED_BY
11994 		  ,LAST_UPDATED_BY
11995 		  ,LAST_UPDATE_LOGIN
11996 		  ,OBJECT_VERSION_NUMBER
11997 		 )
11998 	      VALUES
11999 		 (
12000 		   l_inst_hist_tbl(j)
12001 		  ,v_srl_txn_id
12002 		  ,l_inst_tbl(j)
12003 		  ,SYSDATE
12004 		  ,SYSDATE
12005 		  ,v_user_id
12006 		  ,v_user_id
12007 		  ,-1
12008 		  ,1
12009 		 );
12010         END IF;
12011         commit;
12012         EXIT WHEN INV_CSI_SRL_CUR%NOTFOUND;
12013      END LOOP;
12014      commit;
12015      CLOSE INV_CSI_SRL_CUR;
12016      --
12017 	 -- Commented for bug#14835893
12018   /*   OPEN INV_CSI_LOT_CUR;
12019      LOOP
12020         FETCH INV_CSI_LOT_CUR BULK COLLECT INTO
12021         instance_id_mig,
12022         lot_number_mig,
12023         vld_org_id_mig,
12024         inventory_item_id_mig
12025         LIMIT INST_BUFFER_SIZE;
12026         --
12027         l_ctr := 0;
12028         l_inst_tbl.DELETE;
12029         l_inst_hist_tbl.DELETE;
12030         --
12031         FOR i in 1 .. instance_id_mig.count LOOP
12032            v_lot_ctl := null;
12033            Begin
12034               select lot_control_code
12035               into v_lot_ctl
12036               from MTL_SYSTEM_ITEMS
12037               where inventory_item_id = inventory_item_id_mig(i)
12038               and   organization_id = vld_org_id_mig(i);
12039            Exception
12040               when others then
12041                  v_lot_ctl := null;
12042            End;
12043            IF nvl(v_lot_ctl,1) = 1 THEN
12044               l_ctr := l_ctr + 1;
12045               l_inst_tbl(l_ctr) := instance_id_mig(i);
12046 	         --
12047 	         select CSI_ITEM_INSTANCES_H_S.nextval
12048 	         into l_inst_hist_tbl(l_ctr) from dual;
12049            END IF;
12050         END LOOP;
12051         --
12052         IF l_inst_tbl.count > 0 THEN
12053            l_lot_ins_flag := 'Y';
12054 	      -- Bulk Update Instances
12055 	      FORALL j in l_inst_tbl.FIRST .. l_inst_tbl.LAST
12056 	      UPDATE CSI_ITEM_INSTANCES
12057 	      set active_end_date = sysdate,
12058                instance_status_id = 1,
12059 		     last_update_date = sysdate,
12060 		     last_updated_by = v_user_id
12061 	      where instance_id = l_inst_tbl(j);
12062 	      --
12063 	      -- Tie the Transaction to the history
12064 	      FORALL j in l_inst_tbl.FIRST .. l_inst_tbl.LAST
12065 	      INSERT INTO CSI_ITEM_INSTANCES_H
12066 		  (
12067 		   INSTANCE_HISTORY_ID
12068 		  ,TRANSACTION_ID
12069 		  ,INSTANCE_ID
12070 		  ,CREATION_DATE
12071 		  ,LAST_UPDATE_DATE
12072 		  ,CREATED_BY
12073 		  ,LAST_UPDATED_BY
12074 		  ,LAST_UPDATE_LOGIN
12075 		  ,OBJECT_VERSION_NUMBER
12076 		 )
12077 	      VALUES
12078 		 (
12079 		   l_inst_hist_tbl(j)
12080 		  ,v_lot_txn_id
12081 		  ,l_inst_tbl(j)
12082 		  ,SYSDATE
12083 		  ,SYSDATE
12084 		  ,v_user_id
12085 		  ,v_user_id
12086 		  ,-1
12087 		  ,1
12088 		 );
12089         END IF;
12090         commit;
12091         EXIT WHEN INV_CSI_LOT_CUR%NOTFOUND;
12092      END LOOP;
12093      commit;
12094      CLOSE INV_CSI_LOT_CUR; */
12095 	 -- Commented for bug#14835893
12096      --
12097      -- Insert one record into CSI_TRANSACTIONS
12098      IF l_srl_ins_flag = 'Y' THEN
12099 	   INSERT INTO CSI_TRANSACTIONS(
12100 	     TRANSACTION_ID
12101 	    ,TRANSACTION_DATE
12102 	    ,SOURCE_TRANSACTION_DATE
12103 	    ,SOURCE_HEADER_REF
12104 	    ,SOURCE_LINE_REF
12105 	    ,TRANSACTION_TYPE_ID
12106 	    ,CREATED_BY
12107 	    ,CREATION_DATE
12108 	    ,LAST_UPDATED_BY
12109 	    ,LAST_UPDATE_DATE
12110 	    ,LAST_UPDATE_LOGIN
12111 	    ,OBJECT_VERSION_NUMBER
12112 	   )
12113 	   VALUES(
12114 	     v_srl_txn_id                             -- TRANSACTION_ID
12115 	    ,SYSDATE                              -- TRANSACTION_DATE
12116 	    ,SYSDATE                              -- SOURCE_TRANSACTION_DATE
12117 	    ,'Serial or Lot Control got switched off'   -- SOURCE_HEADER_REF
12118 	    ,'DATAFIX By Update_No_ctl_Srl_Lot_Inst' -- SOURCE_LINE_REF
12119 	    ,v_txn_type_id                        -- TRANSACTION_TYPE_ID
12120 	    ,v_user_id
12121 	    ,sysdate
12122 	    ,v_user_id
12123 	    ,sysdate
12124 	    ,-1
12125 	    ,1
12126 	   );
12127      END IF;
12128      --
12129 	-- Commented for bug#14835893
12130    /*  IF l_lot_ins_flag = 'Y' THEN
12131 	   INSERT INTO CSI_TRANSACTIONS(
12132 	     TRANSACTION_ID
12133 	    ,TRANSACTION_DATE
12134 	    ,SOURCE_TRANSACTION_DATE
12135 	    ,SOURCE_HEADER_REF
12136 	    ,SOURCE_LINE_REF
12137 	    ,TRANSACTION_TYPE_ID
12138 	    ,CREATED_BY
12139 	    ,CREATION_DATE
12140 	    ,LAST_UPDATED_BY
12141 	    ,LAST_UPDATE_DATE
12142 	    ,LAST_UPDATE_LOGIN
12143 	    ,OBJECT_VERSION_NUMBER
12144 	   )
12145 	   VALUES(
12146 	     v_lot_txn_id                             -- TRANSACTION_ID
12147 	    ,SYSDATE                              -- TRANSACTION_DATE
12148 	    ,SYSDATE                              -- SOURCE_TRANSACTION_DATE
12149 	    ,'Serial or Lot Control got switched off'   -- SOURCE_HEADER_REF
12150 	    ,'DATAFIX By Update_No_ctl_Srl_Lot_Inst' -- SOURCE_LINE_REF
12151 	    ,v_txn_type_id                        -- TRANSACTION_TYPE_ID
12152 	    ,v_user_id
12153 	    ,sysdate
12154 	    ,v_user_id
12155 	    ,sysdate
12156 	    ,-1
12157 	    ,1
12158 	   );
12159      END IF;
12160      commit; */
12161 	-- Commented for bug#14835893
12162   EXCEPTION
12163      when comp_error then
12164         null;
12165   END Update_No_ctl_Srl_Lot_Inst;
12166   --
12167   -- This following Procedure identifies the failed shipping Txn and Bumpup or Create the
12168   -- necessary Shipping subinventory.
12169   -- We also consider WIP issues that need to be bumped up.
12170   PROCEDURE Create_or_Update_Shipping_Inst IS
12171      CURSOR CSI_CUR IS
12172      select cii.transaction_error_id,cii.inv_material_transaction_id,
12173             null,null,null,null,null,null
12174      from csi_txn_errors cii
12175      where cii.processed_flag in ('E', 'R')
12176      and   cii.inv_material_transaction_id is not null;
12177      --
12178      CURSOR LOT_CUR(p_txn_id in number) IS
12179      select lot_number,ABS(primary_quantity) transaction_quantity
12180      from mtl_transaction_lot_numbers
12181      where transaction_id = p_txn_id;
12182      --
12183      v_txn_id                           NUMBER;
12184      l_upg_profile                      VARCHAR2(30) := fnd_Profile.value('CSI_UPGRADING_FROM_RELEASE');
12185      v_freeze_date                      DATE;
12186      v_txn_type_id                      NUMBER;
12187      v_nl_trackable                     VARCHAR2(1);
12188      v_mast_org_id                      NUMBER;
12189      v_location_id                      NUMBER;
12190      v_ins_condition_id                 NUMBER;
12191      v_mfg_srl_flag                     VARCHAR2(1);
12192      v_ins_status_id                    NUMBER;
12193      v_instance_id                      NUMBER;
12194      v_ins_history_id                   NUMBER;
12195      v_created_by                       NUMBER := fnd_global.user_id;
12196      v_last_updated_by                  NUMBER := fnd_global.user_id;
12197      v_ins_ou_id                        NUMBER;
12198      v_ins_ou_history_id                NUMBER;
12199      v_ins_party_id                     NUMBER;
12200      v_ins_party_history_id             NUMBER;
12201      v_party_id                         NUMBER;
12202      v_source_reference_id              NUMBER;
12203      v_err_msg                          VARCHAR2(2000);
12204      v_exists                           VARCHAR2(1);
12205      v_ins_qty                          NUMBER;
12206      v_srl_ctl                          NUMBER;
12207      v_lot_ctl                          NUMBER;
12208      v_pri_uom                          VARCHAR2(3);
12209      --
12210      Type NumTabType is VARRAY(10000) of NUMBER;
12211      txn_error_id_mig         NumTabType;
12212      mat_txn_id_mig           NumTabType;
12213      organization_id_mig      NumTabType;
12214      inventory_item_id_mig    NumTabType;
12215      locator_id_mig           NumTabType;
12216      quantity_mig             NumTabType;
12217      --
12218      Type V3Type is VARRAY(10000) of VARCHAR2(3);
12219      revision_mig             V3Type;
12220      --
12221      Type V10Type is VARRAY(10000) of VARCHAR2(10);
12222      subinv_mig               V10Type;
12223      --
12224      MAX_BUFFER_SIZE          NUMBER := 1000;
12225      --
12226      Comp_error               EXCEPTION;
12227      Process_next             EXCEPTION;
12228   BEGIN
12229      IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
12230         csi_gen_utility_pvt.populate_install_param_rec;
12231      END IF;
12232      --
12233      v_freeze_date := csi_datastructures_pub.g_install_param_rec.freeze_date;
12234      --
12235      -- Get the Transaction Type ID for Txn Type MIGRATED
12236      Begin
12237 	   select transaction_type_id
12238 	   into v_txn_type_id
12239 	   from CSI_TXN_TYPES
12240 	   where SOURCE_TRANSACTION_TYPE = 'DATA_CORRECTION';
12241      Exception
12242 	   when no_data_found then
12243 	      v_err_msg := 'Txn Type DATA_CORRECTION not defined in CSI_TXN_TYPES';
12244 	      Raise comp_error;
12245 	   when others then
12246 	      v_err_msg := 'Unable to get the ID for Txn Type DATA_CORRECTION from CSI_TXN_TYPES';
12247 	      Raise comp_error;
12248      End;
12249      --
12250      -- Get the LATEST Status ID. This will be used for all INV records.
12251      Begin
12252 	   select instance_status_id
12253 	   into v_ins_status_id
12254 	   from CSI_INSTANCE_STATUSES
12255 	   where name = 'Latest';
12256      Exception
12257 	   when no_data_found then
12258 	      v_err_msg := 'Status ID not found in CSI for Latest Status';
12259 	      Raise comp_error;
12260 	   when too_many_rows then
12261 	      v_err_msg := 'Too many rows fouund in CSI for Latest Status';
12262 	      Raise comp_error;
12263 	   when others then
12264 	      v_err_msg := 'Error in getting the Status ID in CSI for Latest Status';
12265 	      Raise comp_error;
12266      End;
12267      -- Get the Internal Party ID
12268      IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
12269         csi_gen_utility_pvt.populate_install_param_rec;
12270      END IF;
12271      --
12272      v_party_id := csi_datastructures_pub.g_install_param_rec.internal_party_id;
12273      --
12274      if v_party_id is null then
12275 	   v_err_msg := 'Internal Party ID not found in CSI_INSTALL_PARAMETERS';
12276 	   Raise comp_error;
12277      end if;
12278      --
12279      OPEN CSI_CUR;
12280      LOOP
12281         FETCH CSI_CUR BULK COLLECT INTO
12282         txn_error_id_mig,
12283         mat_txn_id_mig,
12284         inventory_item_id_mig,
12285         revision_mig,
12286         organization_id_mig,
12287         subinv_mig,
12288         locator_id_mig,
12289         quantity_mig
12290         LIMIT MAX_BUFFER_SIZE;
12291         FOR i in 1..txn_error_id_mig.count LOOP
12292 	   Begin
12293               Begin
12294                  select inventory_item_id,organization_id,revision,
12295 	         subinventory_code,locator_id,abs(primary_quantity) transaction_quantity
12296                  into inventory_item_id_mig(i),organization_id_mig(i),revision_mig(i),
12297                  subinv_mig(i),locator_id_mig(i),quantity_mig(i)
12298                  from MTL_MATERIAL_TRANSACTIONS
12299                  where transaction_id = mat_txn_id_mig(i)
12300                  and   ( (transaction_type_id = 33) OR
12301                          (transaction_source_type_id = 5 AND transaction_quantity < 0) );
12302               Exception
12303                  when others then
12304                     Raise Process_next;
12305               End;
12306 	      Begin
12307 	         select primary_uom_code,serial_number_control_code,lot_control_code
12308 	         into v_pri_uom,v_srl_ctl,v_lot_ctl
12309 	         from MTL_SYSTEM_ITEMS
12310 	         where inventory_item_id = inventory_item_id_mig(i)
12311 	         and   organization_id = organization_id_mig(i);
12312 	      Exception
12313 	         when no_data_found then
12314 	            Raise Process_next;
12315 	      End;
12316 	      --
12317 	      IF v_srl_ctl <> 1 THEN
12318 	         Raise Process_next;
12319 	      END IF;
12320 	      --
12321 	      -- Get the Master Organization ID
12322 	      Begin
12323 	         select master_organization_id
12324 	         into v_mast_org_id
12325 	         from MTL_PARAMETERS
12326 	         where organization_id = organization_id_mig(i);
12327 	      Exception
12328 	         when no_data_found then
12329 	            Raise Process_next;
12330 	      End;
12331 	      --
12332 	      v_nl_trackable := 'N';
12333 	      Begin
12334 	         select comms_nl_trackable_flag
12335 	         into v_nl_trackable
12336 	         from MTL_SYSTEM_ITEMS
12337 	         where inventory_item_id = inventory_item_id_mig(i)
12338 	         and   organization_id = v_mast_org_id;
12339 	         Exception
12340 	            when no_data_found then
12341 		       Raise Process_next;
12342 	         End;
12343 	         --
12344 	         IF NVL(v_nl_trackable,'N') <> 'Y' THEN
12345 	            Update CSI_TXN_ERRORS
12346 	            set processed_flag = 'D'
12347 		          ,error_text = 'COMMS_NL_TRACKABLE_FLAG got switched off'
12348                     ,last_update_date = sysdate
12349 	            where transaction_error_id = txn_error_id_mig(i);
12350 	            --
12351 	            Raise Process_next;
12352 	         END IF;
12353 	         --
12354 	         IF v_lot_ctl = 1 THEN
12355 	            v_exists := 'N';
12356 	            Begin
12357 		       select quantity,instance_id
12358 		       into v_ins_qty,v_instance_id
12359 		       from CSI_ITEM_INSTANCES
12360 		       where inventory_item_id = inventory_item_id_mig(i)
12361 		       and   inv_organization_id = organization_id_mig(i)
12362                        and   serial_number is null
12363                        and   lot_number is null
12364 		       and   location_type_code = 'INVENTORY'
12365 		       and   instance_usage_code = 'IN_INVENTORY'
12366 		       and   inv_subinventory_name = subinv_mig(i)
12367 		       and   nvl(inv_locator_id,-999) = nvl(locator_id_mig(i),-999)
12368 		       and   nvl(inventory_revision,'$#$') = nvl(revision_mig(i),'$#$');
12369 		       v_exists := 'Y';
12370 	            Exception
12371 		       when no_data_found then
12372 		          v_exists := 'N';
12373 		       when too_many_rows then
12374 		          Raise Process_next;
12375 	            End;
12376 	            --
12377 	      Begin
12378 		 select CSI_TRANSACTIONS_S.nextval
12379 		 into v_txn_id
12380 		 from DUAL;
12381 	      End;
12382 	      --
12383 	      Begin
12384 		 INSERT INTO CSI_TRANSACTIONS(
12385 		      TRANSACTION_ID
12386 		     ,TRANSACTION_DATE
12387 		     ,SOURCE_TRANSACTION_DATE
12388 		     ,SOURCE_HEADER_REF
12389 		     ,TRANSACTION_TYPE_ID
12390 		     ,CREATED_BY
12391 		     ,CREATION_DATE
12392 		     ,LAST_UPDATED_BY
12393 		     ,LAST_UPDATE_DATE
12394 		     ,LAST_UPDATE_LOGIN
12395 		     ,OBJECT_VERSION_NUMBER
12396 		    )
12397 		    VALUES(
12398 		      v_txn_id                             -- TRANSACTION_ID
12399 		     ,SYSDATE                              -- TRANSACTION_DATE
12400 		     ,SYSDATE                              -- SOURCE_TRANSACTION_DATE
12401 		     ,'DATAFIX by STAGING Bump'            -- SOURCE_HEADER_REF
12402 		     ,v_txn_type_id                        -- TRANSACTION_TYPE_ID
12403 		     ,v_created_by
12404 		     ,sysdate
12405 		     ,v_last_updated_by
12406 		     ,sysdate
12407 		     ,-1
12408 		     ,1
12409 		    );
12410 	       Exception
12411 		  when others then
12412 		     v_err_msg := 'Error while Inserting into CSI_TRANSACTIONS '||substr(sqlerrm,1,1000);
12413 		     raise_application_error(-20000, v_err_msg );
12414 		     Raise;
12415 	       End;
12416 	      --
12417 	      IF v_exists = 'Y' THEN
12418 		 UPDATE CSI_ITEM_INSTANCES
12419 		 set quantity = quantity + quantity_mig(i),
12420 		     active_end_date = null,
12421 		     instance_status_id = v_ins_status_id,
12422 		     last_update_date = sysdate,
12423 		     last_updated_by = v_last_updated_by,
12424                      last_vld_organization_id = organization_id_mig(i)
12425 		 where instance_id = v_instance_id;
12426 		 --
12427 		 -- Tie the Transaction to the history
12428 		 INSERT INTO CSI_ITEM_INSTANCES_H
12429 		     (
12430 		      INSTANCE_HISTORY_ID
12431 		     ,TRANSACTION_ID
12432 		     ,INSTANCE_ID
12433 		     ,CREATION_DATE
12434 		     ,LAST_UPDATE_DATE
12435 		     ,CREATED_BY
12436 		     ,LAST_UPDATED_BY
12437 		     ,LAST_UPDATE_LOGIN
12438 		     ,OBJECT_VERSION_NUMBER
12439 		    )
12440 		 VALUES
12441 		    (
12442 		      CSI_ITEM_INSTANCES_H_S.nextval
12443 		     ,v_txn_id
12444 		     ,v_instance_id
12445 		     ,SYSDATE
12446 		     ,SYSDATE
12447 		     ,v_created_by
12448 		     ,v_last_updated_by
12449 		     ,-1
12450 		     ,1
12451 		    );
12452 		 --
12453 		 Update CSI_TXN_ERRORS
12454 		 set processed_flag = 'R',
12455                      last_update_date = sysdate
12456 		 where transaction_error_id = txn_error_id_mig(i);
12457 		 --
12458 		 Raise Process_next;
12459 	      END IF;
12460 	      -- If instance is not found then Create the Instance
12461 	      -- Get the Location ID from MTL_SECONDARY_INVENTORIES
12462 	      v_location_id := NULL;
12463 	      Begin
12464 		 select location_id
12465 		 into v_location_id
12466 		 from MTL_SECONDARY_INVENTORIES
12467 		 where organization_id = organization_id_mig(i)
12468 		 and   secondary_inventory_name = subinv_mig(i);
12469 	      Exception
12470 		 when no_data_found then
12471 		    Raise Process_next;
12472 	      End;
12473 	      -- Get the Location ID from HR_ORGANIZATION_UNITS
12474 	      IF v_location_id IS NULL THEN
12475 		 Begin
12476 		    select location_id
12477 		    into v_location_id
12478 		    from HR_ORGANIZATION_UNITS
12479 		    where organization_id = organization_id_mig(i);
12480 		 Exception
12481 		    when no_data_found then
12482 		       Raise Process_next;
12483 		 End;
12484 	      END IF;
12485 	      --
12486 	      Begin
12487 		 select csi_item_instances_s.nextval
12488 		 into v_instance_id
12489 		 from DUAL;
12490 	      End;
12491 	      --
12492 	      -- Insert into CSI_ITEM_INSTANCES
12493 	      Begin
12494 		 INSERT INTO CSI_ITEM_INSTANCES(
12495 		      INSTANCE_ID
12496 		     ,INSTANCE_NUMBER
12497 		     ,EXTERNAL_REFERENCE
12498 		     ,INVENTORY_ITEM_ID
12499 		     ,INVENTORY_REVISION
12500 		     ,INV_MASTER_ORGANIZATION_ID
12501 		     ,QUANTITY
12502 		     ,UNIT_OF_MEASURE
12503 		     ,ACCOUNTING_CLASS_CODE
12504 		     ,INSTANCE_STATUS_ID
12505 		     ,CUSTOMER_VIEW_FLAG
12506 		     ,MERCHANT_VIEW_FLAG
12507 		     ,SELLABLE_FLAG
12508 		     ,SYSTEM_ID
12509 		     ,INSTANCE_TYPE_CODE
12510 		     ,ACTIVE_START_DATE
12511 		     ,ACTIVE_END_DATE
12512 		     ,LOCATION_TYPE_CODE
12513 		     ,LOCATION_ID
12514 		     ,INV_ORGANIZATION_ID
12515 		     ,INV_SUBINVENTORY_NAME
12516 		     ,INV_LOCATOR_ID
12517 		     ,INSTALL_DATE
12518 		     ,MANUALLY_CREATED_FLAG
12519 		     ,CREATION_COMPLETE_FLAG
12520 		     ,COMPLETENESS_FLAG
12521 		     ,CREATED_BY
12522 		     ,CREATION_DATE
12523 		     ,LAST_UPDATED_BY
12524 		     ,LAST_UPDATE_DATE
12525 		     ,LAST_UPDATE_LOGIN
12526 		     ,OBJECT_VERSION_NUMBER
12527 		     ,SECURITY_GROUP_ID
12528 		     ,INSTANCE_USAGE_CODE
12529 		     ,OWNER_PARTY_SOURCE_TABLE
12530 		     ,OWNER_PARTY_ID
12531 		     ,LAST_VLD_ORGANIZATION_ID
12532 		    )
12533 		    VALUES(
12534 		      v_instance_id                        -- INSTANCE_ID
12535 		     ,v_instance_id                        -- INSTANCE_NUMBER
12536 		     ,NULL                                 -- EXTERNAL_REFERENCE
12537 		     ,inventory_item_id_mig(i)              -- INVENTORY_ITEM_ID
12538 		     ,revision_mig(i)                       -- INVENTORY_REVISION
12539 		     ,v_mast_org_id                        -- INV_MASTER_ORGANIZATION_ID
12540 		     ,quantity_mig(i)           -- QUANTITY
12541 		     ,v_pri_uom                            -- UNIT_OF_MEASURE (PRIMARY)
12542 		     ,'INV'                                -- ACCOUNTING_CLASS_CODE
12543 		     ,v_ins_status_id                      -- INSTANCE_STATUS_ID
12544 		     ,'N'                                  -- CUSTOMER_VIEW_FLAG
12545 		     ,'Y'                                  -- MERCHANT_VIEW_FLAG
12546 		     ,'Y'                                  -- SELLABLE_FLAG
12547 		     ,NULL                                 -- SYSTEM_ID
12548 		     ,NULL                                 -- INSTANCE_TYPE_CODE
12549 		     ,SYSDATE                              -- ACTIVE_START_DATE
12550 		     ,NULL                                 -- ACTIVE_END_DATE
12551 		     ,'INVENTORY'                          -- LOCATION_TYPE_CODE
12552 		     ,v_location_id                        -- LOCATION_ID
12553 		     ,organization_id_mig(i)                -- INV_ORGANIZATION_ID
12554 		     ,subinv_mig(i)              -- INV_SUBINVENTORY_NAME
12555 		     ,locator_id_mig(i)                     -- INV_LOCATOR_ID
12556 		     ,NULL                                 -- INSTALL_DATE
12557 		     ,'N'                                  -- MANUALLY_CREATED_FLAG
12558 		     ,'Y'                                  -- CREATION_COMPLETE_FLAG
12559 		     ,'Y'                                  -- COMPLETENESS_FLAG
12560 		     ,v_created_by                         -- CREATED_BY
12561 		     ,sysdate                              -- CREATION_DATE
12562 		     ,v_last_updated_by                    -- LAST_UPDATED_BY
12563 		     ,sysdate                              -- LAST_UPDATE_DATE
12564 		     ,-1                                   -- LAST_UPDATE_LOGIN
12565 		     ,1                                    -- OBJECT_VERSION_NUMBER
12566 		     ,NULL                                 -- SECURITY_GROUP_ID
12567 		     ,'IN_INVENTORY'                       -- INSTANCE_USAGE_CODE
12568 		     ,'HZ_PARTIES'                         -- OWNER_PARTY_SOURCE_TABLE
12569 		     ,v_party_id                           -- OWNER_PARTY_ID
12570 		     ,organization_id_mig(i)                -- LAST_VLD_ORGANIZATION_ID
12571 		    );
12572 	      Exception
12573 		 when others then
12574 		    v_err_msg := 'Unable to Insert Item ID '||to_char(inventory_item_id_mig(i))
12575 					  ||' into CSI_ITEM_INSTANCES '||SUBSTR(sqlerrm,1,1000);
12576 		    raise_application_error(-20000, v_err_msg );
12577 		    Raise;
12578 	      End;
12579 	      -- Use the same instance record to create the history
12580 	      Begin
12581 		    select CSI_ITEM_INSTANCES_H_S.nextval
12582 		    into v_ins_history_id
12583 		    from DUAL;
12584 	      End;
12585 	      --
12586 	      Begin
12587 		    INSERT INTO CSI_ITEM_INSTANCES_H(
12588 		      INSTANCE_HISTORY_ID
12589 		     ,INSTANCE_ID
12590 		     ,TRANSACTION_ID
12591 		     ,OLD_INSTANCE_NUMBER
12592 		     ,NEW_INSTANCE_NUMBER
12593 		     ,OLD_EXTERNAL_REFERENCE
12594 		     ,NEW_EXTERNAL_REFERENCE
12595 		     ,OLD_INVENTORY_ITEM_ID
12596 		     ,NEW_INVENTORY_ITEM_ID
12597 		     ,OLD_INVENTORY_REVISION
12598 		     ,NEW_INVENTORY_REVISION
12599 		     ,OLD_INV_MASTER_ORGANIZATION_ID
12600 		     ,NEW_INV_MASTER_ORGANIZATION_ID
12601 		     ,OLD_QUANTITY
12602 		     ,NEW_QUANTITY
12603 		     ,OLD_UNIT_OF_MEASURE
12604 		     ,NEW_UNIT_OF_MEASURE
12605 		     ,OLD_ACCOUNTING_CLASS_CODE
12606 		     ,NEW_ACCOUNTING_CLASS_CODE
12607 		     ,OLD_INSTANCE_STATUS_ID
12608 		     ,NEW_INSTANCE_STATUS_ID
12609 		     ,OLD_CUSTOMER_VIEW_FLAG
12610 		     ,NEW_CUSTOMER_VIEW_FLAG
12611 		     ,OLD_MERCHANT_VIEW_FLAG
12612 		     ,NEW_MERCHANT_VIEW_FLAG
12613 		     ,OLD_SELLABLE_FLAG
12614 		     ,NEW_SELLABLE_FLAG
12615 		     ,OLD_SYSTEM_ID
12616 		     ,NEW_SYSTEM_ID
12617 		     ,OLD_INSTANCE_TYPE_CODE
12618 		     ,NEW_INSTANCE_TYPE_CODE
12619 		     ,OLD_ACTIVE_START_DATE
12620 		     ,NEW_ACTIVE_START_DATE
12621 		     ,OLD_ACTIVE_END_DATE
12622 		     ,NEW_ACTIVE_END_DATE
12623 		     ,OLD_LOCATION_TYPE_CODE
12624 		     ,NEW_LOCATION_TYPE_CODE
12625 		     ,OLD_LOCATION_ID
12626 		     ,NEW_LOCATION_ID
12627 		     ,OLD_INV_ORGANIZATION_ID
12628 		     ,NEW_INV_ORGANIZATION_ID
12629 		     ,OLD_INV_SUBINVENTORY_NAME
12630 		     ,NEW_INV_SUBINVENTORY_NAME
12631 		     ,OLD_INV_LOCATOR_ID
12632 		     ,NEW_INV_LOCATOR_ID
12633 		     ,OLD_COMPLETENESS_FLAG
12634 		     ,NEW_COMPLETENESS_FLAG
12635 		     ,CREATED_BY
12636 		     ,CREATION_DATE
12637 		     ,LAST_UPDATED_BY
12638 		     ,LAST_UPDATE_DATE
12639 		     ,LAST_UPDATE_LOGIN
12640 		     ,OBJECT_VERSION_NUMBER
12641 		     ,SECURITY_GROUP_ID
12642 		     ,FULL_DUMP_FLAG
12643 		     ,OLD_INST_USAGE_CODE
12644 		     ,NEW_INST_USAGE_CODE
12645 		     ,OLD_LAST_VLD_ORGANIZATION_ID
12646 		     ,NEW_LAST_VLD_ORGANIZATION_ID
12647 		    )
12648 		    VALUES(
12649 		      v_ins_history_id                                    -- INSTANCE_HISTORY_ID
12650 		     ,v_instance_id                                       -- INSTANCE_ID
12651 		     ,v_txn_id                                            -- TRANSACTION_ID
12652 		     ,NULL                                                -- OLD_INSTANCE_NUMBER
12653 		     ,v_instance_id                                       -- NEW_INSTANCE_NUMBER
12654 		     ,NULL                                                -- OLD_EXTERNAL_REFERENCE
12655 		     ,NULL                                                -- NEW_EXTERNAL_REFERENCE
12656 		     ,NULL                                                -- OLD_INVENTORY_ITEM_ID
12657 		     ,inventory_item_id_mig(i)                             -- NEW_INVENTORY_ITEM_ID
12658 		     ,NULL                                                -- OLD_INVENTORY_REVISION
12659 		     ,revision_mig(i)                                      -- NEW_INVENTORY_REVISION
12660 		     ,NULL                                                -- OLD_INV_MASTER_ORGANIZATION_ID
12661 		     ,v_mast_org_id                                       -- NEW_INV_MASTER_ORGANIZATION_ID
12662 		     ,NULL                                                -- OLD_QUANTITY
12663 		     ,quantity_mig(i)                          -- NEW_QUANTITY
12664 		     ,NULL                                                -- OLD_UNIT_OF_MEASURE
12665 		     ,v_pri_uom                                           -- NEW_UNIT_OF_MEASURE
12666 		     ,NULL                                                -- OLD_ACCOUNTING_CLASS_CODE
12667 		     ,'INV'                                               -- NEW_ACCOUNTING_CLASS_CODE
12668 		     ,NULL                                                -- OLD_INSTANCE_STATUS_ID
12669 		     ,v_ins_status_id                                     -- NEW_INSTANCE_STATUS_ID
12670 		     ,NULL                                                -- OLD_CUSTOMER_VIEW_FLAG
12671 		     ,'N'                                                 -- NEW_CUSTOMER_VIEW_FLAG
12672 		     ,NULL                                                -- OLD_MERCHANT_VIEW_FLAG
12673 		     ,'Y'                                                 -- NEW_MERCHANT_VIEW_FLAG
12674 		     ,NULL                                                -- OLD_SELLABLE_FLAG
12675 		     ,NULL                                                -- NEW_SELLABLE_FLAG
12676 		     ,NULL                                                -- OLD_SYSTEM_ID
12677 		     ,NULL                                                -- NEW_SYSTEM_ID
12678 		     ,NULL                                                -- OLD_INSTANCE_TYPE_CODE
12679 		     ,NULL                                                -- NEW_INSTANCE_TYPE_CODE
12680 		     ,NULL                                                -- OLD_ACTIVE_START_DATE
12681 		     ,SYSDATE                                             -- NEW_ACTIVE_START_DATE
12682 		     ,NULL                                                -- OLD_ACTIVE_END_DATE
12683 		     ,NULL                                                -- NEW_ACTIVE_END_DATE
12684 		     ,NULL                                                -- OLD_LOCATION_TYPE_CODE
12685 		     ,'INVENTORY'                                         -- NEW_LOCATION_TYPE_CODE
12686 		     ,NULL                                                -- OLD_LOCATION_ID
12687 		     ,v_location_id                                       -- NEW_LOCATION_ID
12688 		     ,NULL                                                -- OLD_INV_ORGANIZATION_ID
12689 		     ,organization_id_mig(i)                               -- NEW_INV_ORGANIZATION_ID
12690 		     ,NULL                                                -- OLD_INV_SUBINVENTORY_NAME
12691 		     ,subinv_mig(i)                             -- NEW_INV_SUBINVENTORY_NAME
12692 		     ,NULL                                                -- OLD_INV_LOCATOR_ID
12693 		     ,locator_id_mig(i)                                    -- NEW_INV_LOCATOR_ID
12694 		     ,NULL                                                -- OLD_COMPLETENESS_FLAG
12695 		     ,'Y'                                                 -- NEW_COMPLETENESS_FLAG
12696 		     ,v_created_by                                        -- CREATED_BY
12697 		     ,sysdate                                             -- CREATION_DATE
12698 		     ,v_last_updated_by                                   -- LAST_UPDATED_BY
12699 		     ,sysdate                                             -- LAST_UPDATE_DATE
12700 		     ,-1                                                  -- LAST_UPDATE_LOGIN
12701 		     ,1                                                   -- OBJECT_VERSION_NUMBER
12702 		     ,NULL                                                -- SECURITY_GROUP_ID
12703 		     ,'N'                                                 -- FULL_DUMP_FLAG
12704 		     ,NULL                                                -- OLD_INST_USAGE_CODE
12705 		     ,'IN_INVENTORY'                                      -- NEW_INST_USAGE_CODE
12706 		     ,NULL                                                -- OLD_LAST_VLD_ORGANIZATION_ID
12707 		     ,organization_id_mig(i)                               -- NEW_LAST_VLD_ORGANIZATION_ID
12708 		    );
12709 	      Exception
12710 		    when others then
12711 			  v_err_msg := 'Unable to Insert Item ID '||to_char(inventory_item_id_mig(i))
12712 				   ||' into CSI_ITEM_INSTANCES_H Using the Same Instance '||SUBSTR(sqlerrm,1,1000);
12713 			     raise_application_error(-20000, v_err_msg );
12714 			  Raise;
12715 	      End;
12716 	      --
12717 	      Begin
12718 		 select CSI_I_PARTIES_S.nextval
12719 		 into v_ins_party_id
12720 		 from DUAL;
12721 	      End;
12722 	      Begin
12723 		    INSERT INTO CSI_I_PARTIES(
12724 		      INSTANCE_PARTY_ID
12725 		     ,INSTANCE_ID
12726 		     ,PARTY_SOURCE_TABLE
12727 		     ,PARTY_ID
12728 		     ,RELATIONSHIP_TYPE_CODE
12729 		     ,CONTACT_FLAG
12730 		     ,CONTACT_IP_ID
12731 		     ,ACTIVE_START_DATE
12732 		     ,ACTIVE_END_DATE
12733 		     ,CREATED_BY
12734 		     ,CREATION_DATE
12735 		     ,LAST_UPDATED_BY
12736 		     ,LAST_UPDATE_DATE
12737 		     ,LAST_UPDATE_LOGIN
12738 		     ,OBJECT_VERSION_NUMBER
12739 		     ,SECURITY_GROUP_ID
12740 		    )
12741 		    VALUES(
12742 		      v_ins_party_id                   -- INSTANCE_PARTY_ID
12743 		     ,v_instance_id                    -- INSTANCE_ID
12744 		     ,'HZ_PARTIES'                     -- PARTY_SOURCE_TABLE
12745 		     ,v_party_id                       -- PARTY_ID
12746 		     ,'OWNER'                          -- RELATIONSHIP_TYPE_CODE
12747 		     ,'N'                              -- CONTACT_FLAG
12748 		     ,NULL                             -- CONTACT_IP_ID
12749 		     ,SYSDATE                          -- ACTIVE_START_DATE
12750 		     ,NULL                             -- ACTIVE_END_DATE
12751 		     ,v_created_by                     -- CREATED_BY
12752 		     ,sysdate                          -- CREATION_DATE
12753 		     ,v_last_updated_by                -- LAST_UPDATED_BY
12754 		     ,sysdate                          -- LAST_UPDATE_DATE
12755 		     ,-1                               -- LAST_UPDATE_LOGIN
12756 		     ,1                                -- OBJECT_VERSION_NUMBER
12757 		     ,NULL                             -- SECURITY_GROUP_ID
12758 		    );
12759 		    Exception
12760 			  when others then
12761 			  v_err_msg := 'Unable to Insert Item ID '||to_char(inventory_item_id_mig(i))
12762 						  ||' into CSI_I_PARTIES '||SUBSTR(sqlerrm,1,1000);
12763 			     raise_application_error(-20000, v_err_msg );
12764 			  Raise;
12765 		    End;
12766 		    -- Insert into CSI_I_PARTIES_H
12767 	      Begin
12768 		 select CSI_I_PARTIES_H_S.nextval
12769 		 into v_ins_party_history_id
12770 		 from DUAL;
12771 	      End;
12772 	      -- Insert into CSI_I_PARTIES_H
12773 	      Begin
12774 		    INSERT INTO CSI_I_PARTIES_H(
12775 		      INSTANCE_PARTY_HISTORY_ID
12776 		     ,INSTANCE_PARTY_ID
12777 		     ,TRANSACTION_ID
12778 		     ,OLD_PARTY_SOURCE_TABLE
12779 		     ,NEW_PARTY_SOURCE_TABLE
12780 		     ,OLD_PARTY_ID
12781 		     ,NEW_PARTY_ID
12782 		     ,OLD_RELATIONSHIP_TYPE_CODE
12783 		     ,NEW_RELATIONSHIP_TYPE_CODE
12784 		     ,OLD_CONTACT_FLAG
12785 		     ,NEW_CONTACT_FLAG
12786 		     ,OLD_CONTACT_IP_ID
12787 		     ,NEW_CONTACT_IP_ID
12788 		     ,OLD_ACTIVE_START_DATE
12789 		     ,NEW_ACTIVE_START_DATE
12790 		     ,OLD_ACTIVE_END_DATE
12791 		     ,NEW_ACTIVE_END_DATE
12792 		     ,FULL_DUMP_FLAG
12793 		     ,CREATED_BY
12794 		     ,CREATION_DATE
12795 		     ,LAST_UPDATED_BY
12796 		     ,LAST_UPDATE_DATE
12797 		     ,LAST_UPDATE_LOGIN
12798 		     ,OBJECT_VERSION_NUMBER
12799 		     ,SECURITY_GROUP_ID
12800 		    )
12801 		    VALUES(
12802 		      v_ins_party_history_id               -- INSTANCE_PARTY_HISTORY_ID
12803 		     ,v_ins_party_id                       -- INSTANCE_PARTY_ID
12804 		     ,v_txn_id                             -- TRANSACTION_ID
12805 		     ,NULL                                 -- OLD_PARTY_SOURCE_TABLE
12806 		     ,'HZ_PARTIES'                         -- NEW_PARTY_SOURCE_TABLE
12807 		     ,NULL                                 -- OLD_PARTY_ID
12808 		     ,v_party_id                           -- NEW_PARTY_ID
12809 		     ,NULL                                 -- OLD_RELATIONSHIP_TYPE_CODE
12810 		     ,'OWNER'                              -- NEW_RELATIONSHIP_TYPE_CODE
12811 		     ,NULL                                 -- OLD_CONTACT_FLAG
12812 		     ,'N'                                  -- NEW_CONTACT_FLAG
12813 		     ,NULL                                 -- OLD_CONTACT_IP_ID
12814 		     ,NULL                                 -- NEW_CONTACT_IP_ID
12815 		     ,NULL                                 -- OLD_ACTIVE_START_DATE
12816 		     ,SYSDATE                              -- NEW_ACTIVE_START_DATE
12817 		     ,NULL                                 -- OLD_ACTIVE_END_DATE
12818 		     ,NULL                                 -- NEW_ACTIVE_END_DATE
12819 		     ,'N'                                  -- FULL_DUMP_FLAG
12820 		     ,v_created_by
12821 		     ,sysdate
12822 		     ,v_last_updated_by
12823 		     ,sysdate
12824 		     ,-1
12825 		     ,1
12826 		     ,NULL
12827 		    );
12828 	      Exception
12829 		    when others then
12830 			  v_err_msg := 'Unable to Insert Item ID '||to_char(inventory_item_id_mig(i))
12831 				   ||' into CSI_I_PARTIES_H using the same Instance '||SUBSTR(sqlerrm,1,1000);
12832 			     raise_application_error(-20000, v_err_msg );
12833 			  Raise;
12834 	      End;
12835 	   ELSE -- Lot controlled item
12836 	      FOR lot_rec in LOT_CUR(mat_txn_id_mig(i)) LOOP
12837 	      v_exists := 'N';
12838 		 Begin
12839 		    select quantity,instance_id
12840 		    into v_ins_qty,v_instance_id
12841 		    from CSI_ITEM_INSTANCES
12842 		    where inventory_item_id = inventory_item_id_mig(i)
12843 		    and   inv_organization_id = organization_id_mig(i)
12844                     and   serial_number is null
12845 		    and   location_type_code = 'INVENTORY'
12846 		    and   instance_usage_code = 'IN_INVENTORY'
12847 		    and   inv_subinventory_name = subinv_mig(i)
12848 		    and   nvl(inv_locator_id,-999) = nvl(locator_id_mig(i),-999)
12849 		    and   nvl(inventory_revision,'$#$') = nvl(revision_mig(i),'$#$')
12850 		    and   lot_number = lot_rec.lot_number;
12851 		    v_exists := 'Y';
12852 		 Exception
12853 		    when no_data_found then
12854 		       v_exists := 'N';
12855 		    when too_many_rows then
12856 		       Raise Process_next;
12857 		 End;
12858 		 --
12859 		 Begin
12860 		    select CSI_TRANSACTIONS_S.nextval
12861 		    into v_txn_id
12862 		    from DUAL;
12863 		 End;
12864 		 --
12865 		 Begin
12866 		    INSERT INTO CSI_TRANSACTIONS(
12867 			 TRANSACTION_ID
12868 			,TRANSACTION_DATE
12869 			,SOURCE_TRANSACTION_DATE
12870 			,SOURCE_HEADER_REF
12871 			,TRANSACTION_TYPE_ID
12872 			,CREATED_BY
12873 			,CREATION_DATE
12874 			,LAST_UPDATED_BY
12875 			,LAST_UPDATE_DATE
12876 			,LAST_UPDATE_LOGIN
12877 			,OBJECT_VERSION_NUMBER
12878 		       )
12879 		       VALUES(
12880 			 v_txn_id                             -- TRANSACTION_ID
12881 			,SYSDATE                              -- TRANSACTION_DATE
12882 			,SYSDATE                              -- SOURCE_TRANSACTION_DATE
12883 			,'DATAFIX By STAGING Bump'            -- SOURCE_HEADER_REF
12884 			,v_txn_type_id                        -- TRANSACTION_TYPE_ID
12885 			,v_created_by
12886 			,sysdate
12887 			,v_last_updated_by
12888 			,sysdate
12889 			,-1
12890 			,1
12891 		       );
12892 		  Exception
12893 		     when others then
12894 			v_err_msg := 'Error while Inserting into CSI_TRANSACTIONS '||substr(sqlerrm,1,1000);
12895 			raise_application_error(-20000, v_err_msg );
12896 			Raise;
12897 		  End;
12898 		 --
12899 		 IF v_exists = 'Y' THEN
12900 		    UPDATE CSI_ITEM_INSTANCES
12901 		    set quantity = quantity + lot_rec.transaction_quantity,
12902 			active_end_date = null,
12903 			instance_status_id = v_ins_status_id,
12904 			last_update_date = sysdate,
12905 			last_updated_by = v_last_updated_by,
12906                         last_vld_organization_id = organization_id_mig(i)
12907 		    where instance_id = v_instance_id;
12908 		    --
12909 		    -- Tie the Transaction to the history
12910 		    INSERT INTO CSI_ITEM_INSTANCES_H
12911 			(
12912 			 INSTANCE_HISTORY_ID
12913 			,TRANSACTION_ID
12914 			,INSTANCE_ID
12915 			,CREATION_DATE
12916 			,LAST_UPDATE_DATE
12917 			,CREATED_BY
12918 			,LAST_UPDATED_BY
12919 			,LAST_UPDATE_LOGIN
12920 			,OBJECT_VERSION_NUMBER
12921 		       )
12922 		    VALUES
12923 		       (
12924 			 CSI_ITEM_INSTANCES_H_S.nextval
12925 			,v_txn_id
12926 			,v_instance_id
12927 			,SYSDATE
12928 			,SYSDATE
12929 			,v_created_by
12930 			,v_last_updated_by
12931 			,-1
12932 			,1
12933 		       );
12934 		    --
12935 		    Update CSI_TXN_ERRORS
12936 		    set processed_flag = 'R',
12937                         last_update_date = sysdate
12938 		    where transaction_error_id = txn_error_id_mig(i);
12939 		    --
12940 		    Raise Process_next;
12941 		 END IF;
12942 		 -- If instance not found then Create the Instance
12943 		 -- Get the Location ID from MTL_SECONDARY_INVENTORIES
12944 		 v_location_id := NULL;
12945 		 Begin
12946 		    select location_id
12947 		    into v_location_id
12948 		    from MTL_SECONDARY_INVENTORIES
12949 		    where organization_id = organization_id_mig(i)
12950 		    and   secondary_inventory_name = subinv_mig(i);
12951 		 Exception
12952 		    when no_data_found then
12953 		       Raise Process_next;
12954 		 End;
12955 		 -- Get the Location ID from HR_ORGANIZATION_UNITS
12956 		 IF v_location_id IS NULL THEN
12957 		    Begin
12958 		       select location_id
12959 		       into v_location_id
12960 		       from HR_ORGANIZATION_UNITS
12961 		       where organization_id = organization_id_mig(i);
12962 		    Exception
12963 		       when no_data_found then
12964 			  Raise Process_next;
12965 		    End;
12966 		 END IF;
12967 		 --
12968 		 Begin
12969 		    select csi_item_instances_s.nextval
12970 		    into v_instance_id
12971 		    from DUAL;
12972 		 End;
12973 		 --
12974 		 -- Insert into CSI_ITEM_INSTANCES
12975 		 Begin
12976 		    INSERT INTO CSI_ITEM_INSTANCES(
12977 			 INSTANCE_ID
12978 			,INSTANCE_NUMBER
12979 			,EXTERNAL_REFERENCE
12980 			,INVENTORY_ITEM_ID
12981 			,INVENTORY_REVISION
12982 			,INV_MASTER_ORGANIZATION_ID
12983 			,MFG_SERIAL_NUMBER_FLAG
12984 			,LOT_NUMBER
12985 			,QUANTITY
12986 			,UNIT_OF_MEASURE
12987 			,ACCOUNTING_CLASS_CODE
12988 			,INSTANCE_STATUS_ID
12989 			,CUSTOMER_VIEW_FLAG
12990 			,MERCHANT_VIEW_FLAG
12991 			,SELLABLE_FLAG
12992 			,SYSTEM_ID
12993 			,INSTANCE_TYPE_CODE
12994 			,ACTIVE_START_DATE
12995 			,ACTIVE_END_DATE
12996 			,LOCATION_TYPE_CODE
12997 			,LOCATION_ID
12998 			,INV_ORGANIZATION_ID
12999 			,INV_SUBINVENTORY_NAME
13000 			,INV_LOCATOR_ID
13001 			,INSTALL_DATE
13002 			,MANUALLY_CREATED_FLAG
13003 			,CREATION_COMPLETE_FLAG
13004 			,COMPLETENESS_FLAG
13005 			,CREATED_BY
13006 			,CREATION_DATE
13007 			,LAST_UPDATED_BY
13008 			,LAST_UPDATE_DATE
13009 			,LAST_UPDATE_LOGIN
13010 			,OBJECT_VERSION_NUMBER
13011 			,SECURITY_GROUP_ID
13012 			,INSTANCE_USAGE_CODE
13013 			,OWNER_PARTY_SOURCE_TABLE
13014 			,OWNER_PARTY_ID
13015 			,LAST_VLD_ORGANIZATION_ID
13016 		       )
13017 		       VALUES(
13018 			 v_instance_id                        -- INSTANCE_ID
13019 			,v_instance_id                        -- INSTANCE_NUMBER
13020 			,NULL                                 -- EXTERNAL_REFERENCE
13021 			,inventory_item_id_mig(i)              -- INVENTORY_ITEM_ID
13022 			,revision_mig(i)                       -- INVENTORY_REVISION
13023 			,v_mast_org_id                        -- INV_MASTER_ORGANIZATION_ID
13024 			,'Y'                                  -- MFG_SERIAL_NUMBER_FLAG
13025 			,lot_rec.lot_number                   -- LOT_NUMBER
13026 			,lot_rec.transaction_quantity         -- QUANTITY
13027 			,v_pri_uom                            -- UNIT_OF_MEASURE (PRIMARY)
13028 			,'INV'                                -- ACCOUNTING_CLASS_CODE
13029 			,v_ins_status_id                      -- INSTANCE_STATUS_ID
13030 			,'N'                                  -- CUSTOMER_VIEW_FLAG
13031 			,'Y'                                  -- MERCHANT_VIEW_FLAG
13032 			,'Y'                                  -- SELLABLE_FLAG
13033 			,NULL                                 -- SYSTEM_ID
13034 			,NULL                                 -- INSTANCE_TYPE_CODE
13035 			,SYSDATE                              -- ACTIVE_START_DATE
13036 			,NULL                                 -- ACTIVE_END_DATE
13037 			,'INVENTORY'                          -- LOCATION_TYPE_CODE
13038 			,v_location_id                        -- LOCATION_ID
13039 			,organization_id_mig(i)                -- INV_ORGANIZATION_ID
13040 			,subinv_mig(i)              -- INV_SUBINVENTORY_NAME
13041 			,locator_id_mig(i)                     -- INV_LOCATOR_ID
13042 			,NULL                                 -- INSTALL_DATE
13043 			,'N'                                  -- MANUALLY_CREATED_FLAG
13044 			,'Y'                                  -- CREATION_COMPLETE_FLAG
13045 			,'Y'                                  -- COMPLETENESS_FLAG
13046 			,v_created_by                         -- CREATED_BY
13047 			,sysdate                              -- CREATION_DATE
13048 			,v_last_updated_by                    -- LAST_UPDATED_BY
13049 			,sysdate                              -- LAST_UPDATE_DATE
13050 			,-1                                   -- LAST_UPDATE_LOGIN
13051 			,1                                    -- OBJECT_VERSION_NUMBER
13052 			,NULL                                 -- SECURITY_GROUP_ID
13053 			,'IN_INVENTORY'                       -- INSTANCE_USAGE_CODE
13054 			,'HZ_PARTIES'                         -- OWNER_PARTY_SOURCE_TABLE
13055 			,v_party_id                           -- OWNER_PARTY_ID
13056 			,organization_id_mig(i)                -- LAST_VLD_ORGANIZATION_ID
13057 		       );
13058 		 Exception
13059 		       when others then
13060 			     v_err_msg := 'Unable to Insert Item ID '||to_char(inventory_item_id_mig(i))
13061 						     ||' into CSI_ITEM_INSTANCES '||SUBSTR(sqlerrm,1,1000);
13062 				raise_application_error(-20000, v_err_msg );
13063 			     Raise;
13064 		 End;
13065 		 -- Use the same instance record to create the history
13066 		 Begin
13067 		       select CSI_ITEM_INSTANCES_H_S.nextval
13068 		       into v_ins_history_id
13069 		       from DUAL;
13070 		 End;
13071 		 --
13072 		 Begin
13073 		       INSERT INTO CSI_ITEM_INSTANCES_H(
13074 			 INSTANCE_HISTORY_ID
13075 			,INSTANCE_ID
13076 			,TRANSACTION_ID
13077 			,OLD_INSTANCE_NUMBER
13078 			,NEW_INSTANCE_NUMBER
13079 			,OLD_EXTERNAL_REFERENCE
13080 			,NEW_EXTERNAL_REFERENCE
13081 			,OLD_INVENTORY_ITEM_ID
13082 			,NEW_INVENTORY_ITEM_ID
13083 			,OLD_INVENTORY_REVISION
13084 			,NEW_INVENTORY_REVISION
13085 			,OLD_INV_MASTER_ORGANIZATION_ID
13086 			,NEW_INV_MASTER_ORGANIZATION_ID
13087 			,OLD_MFG_SERIAL_NUMBER_FLAG
13088 			,NEW_MFG_SERIAL_NUMBER_FLAG
13089 			,OLD_LOT_NUMBER
13090 			,NEW_LOT_NUMBER
13091 			,OLD_QUANTITY
13092 			,NEW_QUANTITY
13093 			,OLD_UNIT_OF_MEASURE
13094 			,NEW_UNIT_OF_MEASURE
13095 			,OLD_ACCOUNTING_CLASS_CODE
13096 			,NEW_ACCOUNTING_CLASS_CODE
13097 			,OLD_INSTANCE_STATUS_ID
13098 			,NEW_INSTANCE_STATUS_ID
13099 			,OLD_CUSTOMER_VIEW_FLAG
13100 			,NEW_CUSTOMER_VIEW_FLAG
13101 			,OLD_MERCHANT_VIEW_FLAG
13102 			,NEW_MERCHANT_VIEW_FLAG
13103 			,OLD_SELLABLE_FLAG
13104 			,NEW_SELLABLE_FLAG
13105 			,OLD_SYSTEM_ID
13106 			,NEW_SYSTEM_ID
13107 			,OLD_INSTANCE_TYPE_CODE
13108 			,NEW_INSTANCE_TYPE_CODE
13109 			,OLD_ACTIVE_START_DATE
13110 			,NEW_ACTIVE_START_DATE
13111 			,OLD_ACTIVE_END_DATE
13112 			,NEW_ACTIVE_END_DATE
13113 			,OLD_LOCATION_TYPE_CODE
13114 			,NEW_LOCATION_TYPE_CODE
13115 			,OLD_LOCATION_ID
13116 			,NEW_LOCATION_ID
13117 			,OLD_INV_ORGANIZATION_ID
13118 			,NEW_INV_ORGANIZATION_ID
13119 			,OLD_INV_SUBINVENTORY_NAME
13120 			,NEW_INV_SUBINVENTORY_NAME
13121 			,OLD_INV_LOCATOR_ID
13122 			,NEW_INV_LOCATOR_ID
13123 			,OLD_COMPLETENESS_FLAG
13124 			,NEW_COMPLETENESS_FLAG
13125 			,CREATED_BY
13126 			,CREATION_DATE
13127 			,LAST_UPDATED_BY
13128 			,LAST_UPDATE_DATE
13129 			,LAST_UPDATE_LOGIN
13130 			,OBJECT_VERSION_NUMBER
13131 			,SECURITY_GROUP_ID
13132 			,FULL_DUMP_FLAG
13133 			,OLD_INST_USAGE_CODE
13134 			,NEW_INST_USAGE_CODE
13135 			,OLD_LAST_VLD_ORGANIZATION_ID
13136 			,NEW_LAST_VLD_ORGANIZATION_ID
13137 		       )
13138 		       VALUES(
13139 			 v_ins_history_id                                    -- INSTANCE_HISTORY_ID
13140 			,v_instance_id                                       -- INSTANCE_ID
13141 			,v_txn_id                                            -- TRANSACTION_ID
13142 			,NULL                                                -- OLD_INSTANCE_NUMBER
13143 			,v_instance_id                                       -- NEW_INSTANCE_NUMBER
13144 			,NULL                                                -- OLD_EXTERNAL_REFERENCE
13145 			,NULL                                                -- NEW_EXTERNAL_REFERENCE
13146 			,NULL                                                -- OLD_INVENTORY_ITEM_ID
13147 			,inventory_item_id_mig(i)                             -- NEW_INVENTORY_ITEM_ID
13148 			,NULL                                                -- OLD_INVENTORY_REVISION
13149 			,revision_mig(i)                                      -- NEW_INVENTORY_REVISION
13150 			,NULL                                                -- OLD_INV_MASTER_ORGANIZATION_ID
13151 			,v_mast_org_id                                       -- NEW_INV_MASTER_ORGANIZATION_ID
13152 			,NULL                                                -- OLD_MFG_SERIAL_NUMBER_FLAG
13153 			,'Y'                                                 -- NEW_MFG_SERIAL_NUMBER_FLAG
13154 			,NULL                                                -- OLD_LOT_NUMBER
13155 			,lot_rec.lot_number                                  -- NEW_LOT_NUMBER
13156 			,NULL                                                -- OLD_QUANTITY
13157 			,lot_rec.transaction_quantity                        -- NEW_QUANTITY
13158 			,NULL                                                -- OLD_UNIT_OF_MEASURE
13159 			,v_pri_uom                                           -- NEW_UNIT_OF_MEASURE
13160 			,NULL                                                -- OLD_ACCOUNTING_CLASS_CODE
13161 			,'INV'                                               -- NEW_ACCOUNTING_CLASS_CODE
13162 			,NULL                                                -- OLD_INSTANCE_STATUS_ID
13163 			,v_ins_status_id                                     -- NEW_INSTANCE_STATUS_ID
13164 			,NULL                                                -- OLD_CUSTOMER_VIEW_FLAG
13165 			,'N'                                                 -- NEW_CUSTOMER_VIEW_FLAG
13166 			,NULL                                                -- OLD_MERCHANT_VIEW_FLAG
13167 			,'Y'                                                 -- NEW_MERCHANT_VIEW_FLAG
13168 			,NULL                                                -- OLD_SELLABLE_FLAG
13169 			,NULL                                                -- NEW_SELLABLE_FLAG
13170 			,NULL                                                -- OLD_SYSTEM_ID
13171 			,NULL                                                -- NEW_SYSTEM_ID
13172 			,NULL                                                -- OLD_INSTANCE_TYPE_CODE
13173 			,NULL                                                -- NEW_INSTANCE_TYPE_CODE
13174 			,NULL                                                -- OLD_ACTIVE_START_DATE
13175 			,SYSDATE                                             -- NEW_ACTIVE_START_DATE
13176 			,NULL                                                -- OLD_ACTIVE_END_DATE
13177 			,NULL                                                -- NEW_ACTIVE_END_DATE
13178 			,NULL                                                -- OLD_LOCATION_TYPE_CODE
13179 			,'INVENTORY'                                         -- NEW_LOCATION_TYPE_CODE
13180 			,NULL                                                -- OLD_LOCATION_ID
13181 			,v_location_id                                       -- NEW_LOCATION_ID
13182 			,NULL                                                -- OLD_INV_ORGANIZATION_ID
13183 			,organization_id_mig(i)                               -- NEW_INV_ORGANIZATION_ID
13184 			,NULL                                                -- OLD_INV_SUBINVENTORY_NAME
13185 			,subinv_mig(i)                             -- NEW_INV_SUBINVENTORY_NAME
13186 			,NULL                                                -- OLD_INV_LOCATOR_ID
13187 			,locator_id_mig(i)                                    -- NEW_INV_LOCATOR_ID
13188 			,NULL                                                -- OLD_COMPLETENESS_FLAG
13189 			,'Y'                                                 -- NEW_COMPLETENESS_FLAG
13190 			,v_created_by                                        -- CREATED_BY
13191 			,sysdate                                             -- CREATION_DATE
13192 			,v_last_updated_by                                   -- LAST_UPDATED_BY
13193 			,sysdate                                             -- LAST_UPDATE_DATE
13194 			,-1                                                  -- LAST_UPDATE_LOGIN
13195 			,1                                                   -- OBJECT_VERSION_NUMBER
13196 			,NULL                                                -- SECURITY_GROUP_ID
13197 			,'N'                                                 -- FULL_DUMP_FLAG
13198 			,NULL                                                -- OLD_INST_USAGE_CODE
13199 			,'IN_INVENTORY'                                      -- NEW_INST_USAGE_CODE
13200 			,NULL                                                -- OLD_LAST_VLD_ORGANIZATION_ID
13201 			,organization_id_mig(i)                               -- NEW_LAST_VLD_ORGANIZATION_ID
13202 		       );
13203 		 Exception
13204 		       when others then
13205 			     v_err_msg := 'Unable to Insert Item ID '||to_char(inventory_item_id_mig(i))
13206 				      ||' into CSI_ITEM_INSTANCES_H Using the Same Instance '||SUBSTR(sqlerrm,1,1000);
13207 				raise_application_error(-20000, v_err_msg );
13208 			     Raise;
13209 		 End;
13210 		 --
13211 		 Begin
13212 		    select CSI_I_PARTIES_S.nextval
13213 		    into v_ins_party_id
13214 		    from DUAL;
13215 		 End;
13216 		 Begin
13217 		       INSERT INTO CSI_I_PARTIES(
13218 			 INSTANCE_PARTY_ID
13219 			,INSTANCE_ID
13220 			,PARTY_SOURCE_TABLE
13221 			,PARTY_ID
13222 			,RELATIONSHIP_TYPE_CODE
13223 			,CONTACT_FLAG
13224 			,CONTACT_IP_ID
13225 			,ACTIVE_START_DATE
13226 			,ACTIVE_END_DATE
13227 			,CREATED_BY
13228 			,CREATION_DATE
13229 			,LAST_UPDATED_BY
13230 			,LAST_UPDATE_DATE
13231 			,LAST_UPDATE_LOGIN
13232 			,OBJECT_VERSION_NUMBER
13233 			,SECURITY_GROUP_ID
13234 		       )
13235 		       VALUES(
13236 			 v_ins_party_id                   -- INSTANCE_PARTY_ID
13237 			,v_instance_id                    -- INSTANCE_ID
13238 			,'HZ_PARTIES'                     -- PARTY_SOURCE_TABLE
13239 			,v_party_id                       -- PARTY_ID
13240 			,'OWNER'                          -- RELATIONSHIP_TYPE_CODE
13241 			,'N'                              -- CONTACT_FLAG
13242 			,NULL                             -- CONTACT_IP_ID
13243 			,SYSDATE                          -- ACTIVE_START_DATE
13244 			,NULL                             -- ACTIVE_END_DATE
13245 			,v_created_by                     -- CREATED_BY
13246 			,sysdate                          -- CREATION_DATE
13247 			,v_last_updated_by                -- LAST_UPDATED_BY
13248 			,sysdate                          -- LAST_UPDATE_DATE
13249 			,-1                               -- LAST_UPDATE_LOGIN
13250 			,1                                -- OBJECT_VERSION_NUMBER
13251 			,NULL                             -- SECURITY_GROUP_ID
13252 		       );
13253 		       Exception
13254 			     when others then
13255 			     v_err_msg := 'Unable to Insert Item ID '||to_char(inventory_item_id_mig(i))
13256 						     ||' into CSI_I_PARTIES '||SUBSTR(sqlerrm,1,1000);
13257 				raise_application_error(-20000, v_err_msg );
13258 			     Raise;
13259 		       End;
13260 		       -- Insert into CSI_I_PARTIES_H
13261 		 Begin
13262 		    select CSI_I_PARTIES_H_S.nextval
13263 		    into v_ins_party_history_id
13264 		    from DUAL;
13265 		 End;
13266 		 -- Insert into CSI_I_PARTIES_H
13267 		 Begin
13268 		       INSERT INTO CSI_I_PARTIES_H(
13269 			 INSTANCE_PARTY_HISTORY_ID
13270 			,INSTANCE_PARTY_ID
13271 			,TRANSACTION_ID
13272 			,OLD_PARTY_SOURCE_TABLE
13273 			,NEW_PARTY_SOURCE_TABLE
13274 			,OLD_PARTY_ID
13275 			,NEW_PARTY_ID
13276 			,OLD_RELATIONSHIP_TYPE_CODE
13277 			,NEW_RELATIONSHIP_TYPE_CODE
13278 			,OLD_CONTACT_FLAG
13279 			,NEW_CONTACT_FLAG
13280 			,OLD_CONTACT_IP_ID
13281 			,NEW_CONTACT_IP_ID
13282 			,OLD_ACTIVE_START_DATE
13283 			,NEW_ACTIVE_START_DATE
13284 			,OLD_ACTIVE_END_DATE
13285 			,NEW_ACTIVE_END_DATE
13286 			,FULL_DUMP_FLAG
13287 			,CREATED_BY
13288 			,CREATION_DATE
13289 			,LAST_UPDATED_BY
13290 			,LAST_UPDATE_DATE
13291 			,LAST_UPDATE_LOGIN
13292 			,OBJECT_VERSION_NUMBER
13293 			,SECURITY_GROUP_ID
13294 		       )
13295 		       VALUES(
13296 			 v_ins_party_history_id               -- INSTANCE_PARTY_HISTORY_ID
13297 			,v_ins_party_id                       -- INSTANCE_PARTY_ID
13298 			,v_txn_id                             -- TRANSACTION_ID
13299 			,NULL                                 -- OLD_PARTY_SOURCE_TABLE
13300 			,'HZ_PARTIES'                         -- NEW_PARTY_SOURCE_TABLE
13301 			,NULL                                 -- OLD_PARTY_ID
13302 			,v_party_id                           -- NEW_PARTY_ID
13303 			,NULL                                 -- OLD_RELATIONSHIP_TYPE_CODE
13304 			,'OWNER'                              -- NEW_RELATIONSHIP_TYPE_CODE
13305 			,NULL                                 -- OLD_CONTACT_FLAG
13306 			,'N'                                  -- NEW_CONTACT_FLAG
13307 			,NULL                                 -- OLD_CONTACT_IP_ID
13308 			,NULL                                 -- NEW_CONTACT_IP_ID
13309 			,NULL                                 -- OLD_ACTIVE_START_DATE
13310 			,SYSDATE                              -- NEW_ACTIVE_START_DATE
13311 			,NULL                                 -- OLD_ACTIVE_END_DATE
13312 			,NULL                                 -- NEW_ACTIVE_END_DATE
13313 			,'N'                                  -- FULL_DUMP_FLAG
13314 			,v_created_by
13315 			,sysdate
13316 			,v_last_updated_by
13317 			,sysdate
13318 			,-1
13319 			,1
13320 			,NULL
13321 		       );
13322 		 Exception
13323 		       when others then
13324 			     v_err_msg := 'Unable to Insert Item ID '||to_char(inventory_item_id_mig(i))
13325 				      ||' into CSI_I_PARTIES_H using the same Instance '||SUBSTR(sqlerrm,1,1000);
13326 				raise_application_error(-20000, v_err_msg );
13327 			     Raise;
13328 		 End;
13329 	      END LOOP;
13330 	   END IF; -- Lot control check
13331 	   Update csi_txn_errors
13332 	   set processed_flag = 'R',
13333                last_update_date = sysdate
13334 	   where transaction_error_id = txn_error_id_mig(i);
13335 	   --
13336 	Exception
13337 	   when process_next then
13338 	      null;
13339 	End;
13340       END LOOP;
13341       commit;
13342       EXIT WHEN CSI_CUR%NOTFOUND;
13343     END LOOP;
13344     commit;
13345     CLOSE CSI_CUR;
13346   EXCEPTION
13347      When comp_error then
13348 	null;
13349   END Create_or_Update_Shipping_Inst;
13350   --
13351   PROCEDURE fix_srlsoi_returned_serials
13352   IS
13353 
13354     TYPE NumTabType is    varray(1000) of number;
13355     TYPE VarTabType is    varray(1000) of varchar2(80);
13356 
13357     l_serial_number_tab   VarTabType;
13358     l_item_id_tab         NumTabType;
13359     l_organization_id_tab NumTabType;
13360     l_lot_code_tab        NumTabType;
13361 
13362     MAX_BUFFER_SIZE       number := 1000;
13363 
13364     l_last_mtl_txn_id         number;
13365     l_last_mtl_action_id      number;
13366     l_last_mtl_source_type_id number;
13367     l_last_mtl_type_id        number;
13368     l_last_rma_processed      varchar2(1);
13369 
13370     l_change_owner_flag       varchar2(1);
13371     l_owner_party_id          number;
13372     l_owner_account_id        number;
13373     l_internal_party_id       number;
13374 
13375     l_txn_rec                 csi_datastructures_pub.transaction_rec;
13376     l_instance_rec            csi_datastructures_pub.instance_rec;
13377     l_parties_tbl             csi_datastructures_pub.party_tbl;
13378     l_pty_accts_tbl           csi_datastructures_pub.party_account_tbl;
13379 
13380     l_error_message           varchar2(2000);
13381 
13382     l_msg_count               number;
13383     l_msg_data                varchar2(2000);
13384     l_return_status           varchar2(1) := fnd_api.g_ret_sts_success;
13385 
13386     CURSOR soisrl_cur IS
13387       SELECT msn.serial_number,
13388              msn.inventory_item_id,
13389              msn.current_organization_id,
13390              msi.lot_control_code
13391       FROM   mtl_serial_numbers msn,
13392              mtl_system_items   msi
13393       WHERE  msn.current_status             = 1   -- predefined state (for rma'ed serials)
13394       AND    msi.inventory_item_id          = msn.inventory_item_id
13395       AND    msi.organization_id            = msn.current_organization_id
13396       AND    msi.serial_number_control_code = 6   -- serialized at so issue
13397       AND    nvl(msi.comms_nl_trackable_flag, 'N') = 'Y';
13398 
13399   BEGIN
13400 
13401     IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
13402        csi_gen_utility_pvt.populate_install_param_rec;
13403     END IF;
13404     --
13405     l_internal_party_id := csi_datastructures_pub.g_install_param_rec.internal_party_id;
13406     --
13407     l_txn_rec.transaction_id                := fnd_api.g_miss_num;
13408     l_txn_rec.transaction_type_id           := correction_txn_type_id;
13409     l_txn_rec.source_header_ref             := 'DATAFIX';
13410     l_txn_rec.source_line_ref               := 'SRLSOI RETURNED FIX';
13411     l_txn_rec.source_transaction_date       := sysdate;
13412     l_txn_rec.transaction_date              := sysdate;
13413 
13414     OPEN soisrl_cur;
13415     LOOP
13416 
13417       FETCH soisrl_cur BULK COLLECT
13418       INTO  l_serial_number_tab,
13419             l_item_id_tab,
13420             l_organization_id_tab,
13421             l_lot_code_tab
13422       LIMIT MAX_BUFFER_SIZE;
13423 
13424       FOR l_ind IN 1 .. l_serial_number_tab.COUNT
13425       LOOP
13426 
13427         l_last_mtl_txn_id := null;
13428 
13429         FOR all_txn_rec IN  all_txn_cur(
13430           p_serial_number  => l_serial_number_tab(l_ind),
13431           p_item_id        => l_item_id_tab(l_ind))
13432         LOOP
13433           l_last_mtl_txn_id         := all_txn_rec.mtl_txn_id;
13434           l_last_mtl_action_id      := all_txn_rec.mtl_action_id;
13435           l_last_mtl_source_type_id := all_txn_rec.mtl_source_type_id;
13436           l_last_mtl_type_id        := all_txn_rec.mtl_type_id;
13437           EXIT;
13438         END LOOP;
13439 
13440         IF l_last_mtl_txn_id is not null AND l_last_mtl_type_id = 15 THEN
13441           BEGIN
13442             SELECT 'Y'
13443             INTO   l_last_rma_processed
13444             FROM   sys.dual
13445             WHERE  exists
13446               (SELECT '1' FROM csi_transactions
13447                WHERE  inv_material_transaction_id = l_last_mtl_txn_id);
13448           EXCEPTION
13449             WHEN no_data_found THEN
13450               l_last_rma_processed := 'N';
13451           END;
13452 
13453           IF l_last_rma_processed = 'Y' THEN
13454 
13455             BEGIN
13456 
13457               SELECT instance_id,
13458                      object_version_number,
13459                      location_type_code,
13460                      instance_usage_code
13461               INTO   l_instance_rec.instance_id,
13462                      l_instance_rec.object_version_number,
13463                      l_instance_rec.location_type_code,
13464                      l_instance_rec.instance_usage_code
13465               FROM   csi_item_instances
13466               WHERE  inventory_item_id = l_item_id_tab(l_ind)
13467               AND    serial_number     = l_serial_number_tab(l_ind);
13468 
13469               IF l_instance_rec.instance_usage_code <> 'RETURNED' OR
13470                  l_instance_rec.location_type_code  <> 'INVENTORY'
13471               THEN
13472 
13473                 csi_process_txn_pvt.check_and_break_relation(
13474                   p_instance_id   => l_instance_rec.instance_id,
13475                   p_csi_txn_rec   => l_txn_rec,
13476                   x_return_status => l_return_status);
13477 
13478                 get_rma_owner(
13479                   p_serial_number        => l_serial_number_tab(l_ind),
13480                   p_inventory_item_id    => l_item_id_tab(l_ind),
13481                   p_organization_id      => l_organization_id_tab(l_ind),
13482                   x_change_owner_flag    => l_change_owner_flag,
13483                   x_owner_party_id       => l_owner_party_id,
13484                   x_owner_account_id     => l_owner_account_id);
13485 
13486                 FOR inv_rec IN inv_cur (l_last_mtl_txn_id)
13487                 LOOP
13488 
13489                   -- build instance rec
13490                   SELECT object_version_number
13491                   INTO   l_instance_rec.object_version_number
13492                   FROM   csi_item_instances
13493                   WHERE  instance_id = l_instance_rec.instance_id;
13494 
13495                   l_instance_rec.location_type_code    := 'INVENTORY';
13496                   l_instance_rec.instance_usage_code   := 'RETURNED';
13497                   l_instance_rec.inv_organization_id   := inv_rec.organization_id;
13498                   l_instance_rec.inv_subinventory_name := inv_rec.subinv_code;
13499                   l_instance_rec.inv_locator_id        := inv_rec.locator_id;
13500                   l_instance_rec.active_end_date       := null;
13501 
13502                   get_lot_number(
13503                     p_lot_code        => l_lot_code_tab(l_ind),
13504                     p_mtl_txn_id      => l_last_mtl_txn_id,
13505                     p_serial_number   => l_serial_number_tab(l_ind),
13506                     x_lot_number      => l_instance_rec.lot_number);
13507 
13508                   l_instance_rec.inventory_revision       := inv_rec.revision;
13509                   l_instance_rec.vld_organization_id      := inv_rec.organization_id;
13510                   l_instance_rec.object_version_number    := 1.0;
13511 
13512                   SELECT nvl(mssi.location_id, haou.location_id)
13513                   INTO   l_instance_rec.location_id
13514                   FROM   mtl_secondary_inventories mssi,
13515                          hr_all_organization_units haou
13516                   WHERE  mssi.organization_id          = l_instance_rec.inv_organization_id
13517                   AND    mssi.secondary_inventory_name = l_instance_rec.inv_subinventory_name
13518                   AND    haou.organization_id          = mssi.organization_id;
13519 
13520                 END LOOP;
13521 
13522                 IF l_change_owner_flag = 'Y' THEN
13523                   l_instance_rec.active_end_date := sysdate;
13524 
13525                   -- build internal party record
13526                   l_parties_tbl(1).instance_party_id      := fnd_api.g_miss_num;
13527                   l_parties_tbl(1).party_source_table     := 'HZ_PARTIES';
13528                   l_parties_tbl(1).party_id               := l_internal_party_id;
13529                   l_parties_tbl(1).relationship_type_code := 'OWNER';
13530                   l_parties_tbl(1).contact_flag           := 'N';
13531                   l_parties_tbl(1).object_version_number  := 1.0;
13532 
13533                 END IF;
13534 
13535                 log('  serial :'||l_serial_number_tab(l_ind)||'  Change Owner :'||l_change_owner_flag);
13536 
13537                 update_instance(
13538                   p_txn_rec        => l_txn_rec,
13539                   p_instance_rec   => l_instance_rec,
13540                   p_parties_tbl    => l_parties_tbl,
13541                   p_pty_accts_tbl  => l_pty_accts_tbl,
13542                   x_return_status  => l_return_status,
13543                   x_error_message  => l_error_message);
13544 
13545               END IF;
13546 
13547             EXCEPTION
13548               WHEN no_data_found THEN
13549                 null;
13550               WHEN too_many_rows THEN
13551                 log('too many instances for item '||l_item_id_tab(l_ind)||
13552                     ' serial '||l_serial_number_tab(l_ind));
13553             END;
13554           END IF;
13555         END IF;
13556 
13557         IF mod(l_ind, 100) = 0 THEN
13558           commit;
13559         END IF;
13560 
13561       END LOOP;
13562 
13563       EXIT when soisrl_cur%NOTFOUND;
13564 
13565     END LOOP;
13566 
13567     IF soisrl_cur%ISOPEN THEN
13568       CLOSE soisrl_cur;
13569     END IF;
13570 
13571   EXCEPTION
13572     WHEN others THEN
13573       log('Error(O): fix_srlsoi_returned_serials '||sqlerrm);
13574       close soisrl_cur;
13575   END fix_srlsoi_returned_serials;
13576 
13577 
13578   PROCEDURE IB_INV_Synch_Non_srl IS
13579      CURSOR INV_ONH_BAL_CUR IS
13580      select   moq.organization_id organization_id
13581      ,           moq.inventory_item_id inventory_item_id
13582      ,           moq.revision revision
13583      ,           moq.subinventory_code subinventory_code
13584      ,           moq.locator_id locator_id
13585      ,           moq.lot_number lot_number
13586      ,           msi.primary_uom_code primary_uom_code
13587      ,           sum(moq.transaction_quantity) onhand_qty
13588      from
13589 		 mtl_system_items msi
13590      ,           mtl_onhand_quantities moq
13591      where       msi.inventory_item_id = moq.inventory_item_id
13592      and         msi.organization_id = moq.organization_id
13593      and         msi.serial_number_control_code in (1,6) -- No Serial control and at SO Issue Items
13594      group by
13595 		 moq.organization_id
13596      ,           moq.inventory_item_id
13597      ,           moq.revision
13598      ,           moq.subinventory_code
13599      ,           moq.locator_id
13600      ,           moq.lot_number
13601      ,           msi.primary_uom_code;
13602      --
13603      v_txn_id                           NUMBER;
13604      v_freeze_date                      DATE;
13605      v_txn_type_id                      NUMBER;
13606      l_upg_profile                      VARCHAR2(30) := fnd_Profile.value('CSI_UPGRADING_FROM_RELEASE');
13607      v_mast_org_id                      NUMBER;
13608      v_nl_trackable                     VARCHAR2(1);
13609      v_location_id                      NUMBER;
13610      v_ins_condition_id                 NUMBER;
13611      v_mfg_srl_flag                     VARCHAR2(1);
13612      v_ins_status_id                    NUMBER;
13613      v_instance_id                      NUMBER;
13614      v_end_date                         DATE;
13615      v_ins_history_id                   NUMBER;
13616      v_created_by                       NUMBER := fnd_global.user_id;
13617      v_last_updated_by                  NUMBER := fnd_global.user_id;
13618      v_ins_ou_id                        NUMBER;
13619      v_ins_ou_history_id                NUMBER;
13620      v_ins_party_id                     NUMBER;
13621      v_ins_party_history_id             NUMBER;
13622      v_party_id                         NUMBER;
13623      v_source_reference_id              NUMBER;
13624      v_err_msg                          VARCHAR2(2000);
13625      v_exists                           VARCHAR2(1);
13626      v_ins_qty                          NUMBER;
13627      v_ins_obj_nbr                      NUMBER;
13628      l_error_count                      NUMBER := 0;
13629      --
13630      Type NumTabType is VARRAY(10000) of NUMBER;
13631      organization_id_mig      NumTabType;
13632      inventory_item_id_mig    NumTabType;
13633      locator_id_mig           NumTabType;
13634      quantity_mig             NumTabType;
13635      --
13636      Type V3Type is VARRAY(10000) of VARCHAR2(3);
13637      uom_code_mig             V3Type;
13638      revision_mig             V3Type;
13639      --
13640      Type V10Type is VARRAY(10000) of VARCHAR2(10);
13641      subinv_mig               V10Type;
13642      --
13643      Type V80Type is VARRAY(10000) of VARCHAR2(80);
13644      lot_mig                  V80Type;
13645      --
13646      MAX_BUFFER_SIZE        NUMBER := 1000;
13647      x_return_status          VARCHAR2(1);
13648   --
13649      process_next                       EXCEPTION;
13650      comp_error                         EXCEPTION;
13651   --
13652   BEGIN
13653      csi_t_gen_utility_pvt.build_file_name(
13654          p_file_segment1 => 'csinonsy',
13655          p_file_segment2 => to_char(sysdate, 'hh24miss'));
13656      --
13657      IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
13658         csi_gen_utility_pvt.populate_install_param_rec;
13659      END IF;
13660      --
13661      v_freeze_date := csi_datastructures_pub.g_install_param_rec.freeze_date;
13662      --
13663      -- Get the Transaction Type ID for Txn Type MIGRATED
13664      Begin
13665 	select transaction_type_id
13666 	into v_txn_type_id
13667 	from CSI_TXN_TYPES
13668 	where SOURCE_TRANSACTION_TYPE = 'DATA_CORRECTION';
13669      Exception
13670 	when no_data_found then
13671 	   v_err_msg := 'Txn Type DATA_CORRECTION not defined in CSI_TXN_TYPES';
13672 	   Raise comp_error;
13673 	when others then
13674 	   v_err_msg := 'Unable to get the ID for Txn Type DATA_CORRECTION from CSI_TXN_TYPES';
13675 	   Raise comp_error;
13676      End;
13677      --
13678      -- Get the LATEST Status ID. This will be used for all INV records.
13679      Begin
13680 	select instance_status_id
13681 	into v_ins_status_id
13682 	from CSI_INSTANCE_STATUSES
13683 	where name = 'Latest';
13684      Exception
13685 	when no_data_found then
13686 	   v_err_msg := 'Status ID not found in CSI for Latest Status';
13687 	   Raise comp_error;
13688 	when too_many_rows then
13689 	   v_err_msg := 'Too many rows fouund in CSI for Latest Status';
13690 	   Raise comp_error;
13691 	when others then
13692 	   v_err_msg := 'Error in getting the Status ID in CSI for Latest Status';
13693 	   Raise comp_error;
13694      End;
13695      -- Get the Internal Party ID
13696      IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
13697         csi_gen_utility_pvt.populate_install_param_rec;
13698      END IF;
13699      --
13700      v_party_id := csi_datastructures_pub.g_install_param_rec.internal_party_id;
13701      --
13702      if v_party_id is null then
13703 	v_err_msg := 'Internal Party ID not found in CSI_INSTALL_PARAMETERS';
13704 	Raise comp_error;
13705      end if;
13706      --
13707      --
13708      OPEN INV_ONH_BAL_CUR;
13709      LOOP
13710         FETCH INV_ONH_BAL_CUR BULK COLLECT INTO
13711         organization_id_mig,
13712         inventory_item_id_mig,
13713         revision_mig,
13714         subinv_mig,
13715         locator_id_mig,
13716         lot_mig,
13717         uom_code_mig,
13718         quantity_mig
13719         LIMIT MAX_BUFFER_SIZE;
13720         --
13721         FOR i in 1 .. organization_id_mig.count LOOP
13722            Begin
13723 	       -- Assign NULL to all variables getting values from sequence. This is used at the later stage
13724 	       -- to delete the records if the Insert fails. Assigning NULL will ensure that we do not delete
13725 	       -- wrong set of previous records if the insert fails for the next.
13726 	       v_txn_id := -99999;
13727 	       v_instance_id := -99999;
13728 	       v_ins_history_id := -99999;
13729 	       v_ins_ou_id := -99999;
13730 	       v_ins_ou_history_id := -99999;
13731 	       v_ins_party_id := -99999;
13732 	       v_ins_party_history_id := -99999;
13733 	       --
13734 	      -- Get the Master Organization ID
13735 	      Begin
13736 		 select master_organization_id
13737 		 into v_mast_org_id
13738 		 from MTL_PARAMETERS
13739 		 where organization_id = organization_id_mig(i);
13740 	      Exception
13741 		 when no_data_found then
13742 		    Raise Process_next;
13743 	      End;
13744 	      -- Check for IB trackable
13745 	      v_nl_trackable := 'N';
13746 	      Begin
13747 		 select comms_nl_trackable_flag
13748 		 into v_nl_trackable
13749 		 from MTL_SYSTEM_ITEMS
13750 		 where inventory_item_id = inventory_item_id_mig(i)
13751 		 and   organization_id = v_mast_org_id;
13752 	      Exception
13753 		 when no_data_found then
13754 		    Raise Process_next;
13755 	      End;
13756 	      -- Ignore if not Trackable
13757 	      IF NVL(v_nl_trackable,'N') <> 'Y' THEN
13758 		 Raise Process_next;
13759 	      END IF; -- nl_trackable check
13760 	      --
13761               -- Check if there are any errors in CSI_TXN_ERRORS
13762               l_error_count := 0;
13763               Begin
13764                  select count(*)
13765                  into l_error_count
13766                  from CSI_TXN_ERRORS cii,
13767                       MTL_MATERIAL_TRANSACTIONS mmt
13768                  where cii.inv_material_transaction_id is not null
13769                  and   cii.inv_material_transaction_id = mmt.transaction_id
13770                  and   cii.processed_flag in ('E','R')
13771                  and   mmt.inventory_item_id = inventory_item_id_mig(i)
13772                  and   mmt.organization_id = organization_id_mig(i);
13773               End;
13774               --
13775               IF nvl(l_error_count,0) > 0 THEN
13776                  v_err_msg := 'Unable to Synch Item ID '||to_char(inventory_item_id_mig(i))||
13777                               '  Under Organization '||to_char(organization_id_mig(i));
13778                  Out(v_err_msg);
13779                  Raise Process_next;
13780               END IF;
13781 	      -- Get the Location ID from MTL_SECONDARY_INVENTORIES
13782 	      Begin
13783 		 select location_id
13784 		 into v_location_id
13785 		 from MTL_SECONDARY_INVENTORIES
13786 		 where organization_id = organization_id_mig(i)
13787 		 and   secondary_inventory_name = subinv_mig(i);
13788 	      Exception
13789 		 when no_data_found then
13790 		    Raise Process_next;
13791 	      End;
13792 	      -- Get the Location ID from HR_ORGANIZATION_UNITS
13793 	      IF v_location_id IS NULL THEN
13794 		 Begin
13795 		    select location_id
13796 		    into v_location_id
13797 		    from HR_ORGANIZATION_UNITS
13798 		    where organization_id = organization_id_mig(i);
13799 		 Exception
13800 		    when no_data_found then
13801 		       Raise Process_next;
13802 		 End;
13803 	      END IF;
13804 	      --
13805 	      if lot_mig(i) is not NULL then
13806 		 v_mfg_srl_flag := 'Y';
13807 	      else
13808 		 v_mfg_srl_flag := NULL;
13809 	      end if;
13810 	      --
13811 	      v_exists := 'N';
13812 	      v_end_date := NULL;
13813 	      Begin
13814 		 select quantity,instance_id,active_end_date
13815 		 into v_ins_qty,v_instance_id,v_end_date
13816 		 from CSI_ITEM_INSTANCES
13817 		 where inventory_item_id = inventory_item_id_mig(i)
13818 		 and   inv_organization_id = organization_id_mig(i)
13819                  and   serial_number is null
13820 		 and   location_type_code = 'INVENTORY'
13821 		 and   instance_usage_code = 'IN_INVENTORY'
13822 		 and   inv_subinventory_name = subinv_mig(i)
13823 		 and   nvl(inv_locator_id,-999) = nvl(locator_id_mig(i),-999)
13824 		 and   nvl(inventory_revision,'$#$') = nvl(revision_mig(i),'$#$')
13825 		 and   nvl(lot_number,'$#$') = nvl(lot_mig(i),'$#$');
13826 		 v_exists := 'Y';
13827 	      Exception
13828 		 when no_data_found then
13829 		    v_exists := 'N';
13830 		 when too_many_rows then
13831 		    Raise Process_next;
13832 	      End;
13833 	      --
13834 	      IF v_exists = 'Y' THEN
13835 		 IF v_ins_qty <> quantity_mig(i) OR
13836 		    NVL(v_end_date,(sysdate+1)) <= sysdate THEN
13837 		    UPDATE CSI_ITEM_INSTANCES
13838 		    set quantity = quantity_mig(i)
13839 		       ,active_end_date = decode(quantity_mig(i),0,sysdate,null)
13840 		       ,instance_status_id = decode(quantity_mig(i),0,1,v_ins_status_id)
13841 		       ,last_update_date = sysdate
13842 		       ,last_updated_by = v_last_updated_by
13843                        ,last_vld_organization_id = organization_id_mig(i)
13844 		    where instance_id = v_instance_id;
13845 		    --
13846 		    Begin
13847 		       select CSI_TRANSACTIONS_S.nextval
13848 		       into v_txn_id
13849 		       from DUAL;
13850 		    End;
13851 		    --
13852 		    Begin
13853 		       INSERT INTO CSI_TRANSACTIONS(
13854 			    TRANSACTION_ID
13855 			   ,TRANSACTION_DATE
13856 			   ,SOURCE_TRANSACTION_DATE
13857 			   ,SOURCE_HEADER_REF
13858 			   ,TRANSACTION_TYPE_ID
13859 			   ,CREATED_BY
13860 			   ,CREATION_DATE
13861 			   ,LAST_UPDATED_BY
13862 			   ,LAST_UPDATE_DATE
13863 			   ,LAST_UPDATE_LOGIN
13864 			   ,OBJECT_VERSION_NUMBER
13865 			  )
13866 			  VALUES(
13867 			    v_txn_id                             -- TRANSACTION_ID
13868 			   ,SYSDATE                              -- TRANSACTION_DATE
13869 			   ,SYSDATE                              -- SOURCE_TRANSACTION_DATE
13870 			   ,'DATAFIX By IB-INV Synch'            -- SOURCE_HEADER_REF
13871 			   ,v_txn_type_id                        -- TRANSACTION_TYPE_ID
13872 			   ,v_created_by
13873 			   ,sysdate
13874 			   ,v_last_updated_by
13875 			   ,sysdate
13876 			   ,-1
13877 			   ,1
13878 			  );
13879 		    Exception
13880 		       when others then
13881 			  v_err_msg := 'Error while Inserting into CSI_TRANSACTIONS '||substr(sqlerrm,1,1000);
13882 			  raise_application_error(-20000, v_err_msg );
13883 			  Raise;
13884 		    End;
13885 		    -- Tie the Transaction with the instance history
13886 		    INSERT INTO CSI_ITEM_INSTANCES_H
13887 			(
13888 			 INSTANCE_HISTORY_ID
13889 			,TRANSACTION_ID
13890 			,INSTANCE_ID
13891 			,OLD_QUANTITY
13892 			,NEW_QUANTITY
13893 			,CREATION_DATE
13894 			,LAST_UPDATE_DATE
13895 			,CREATED_BY
13896 			,LAST_UPDATED_BY
13897 			,LAST_UPDATE_LOGIN
13898 			,OBJECT_VERSION_NUMBER
13899 		       )
13900 		    VALUES
13901 		       (
13902 			 CSI_ITEM_INSTANCES_H_S.nextval
13903 			,v_txn_id
13904 			,v_instance_id
13905 			,v_ins_qty
13906 			,quantity_mig(i)
13907 			,SYSDATE
13908 			,SYSDATE
13909 			,v_created_by
13910 			,v_last_updated_by
13911 			,-1
13912 			,1
13913 		       );
13914 		 END IF;
13915 		 Raise Process_next;
13916 	      END IF; -- Check for instance existance
13917 	      --
13918 	      -- If instance is not found then create the INV instance
13919 	      -- For each record, we insert a record into CSI_TRANSACTIONS. This Transaction ID will be used
13920 	      -- to populate the History Record.
13921 	      Begin
13922 		 select CSI_TRANSACTIONS_S.nextval
13923 		 into v_txn_id
13924 		 from DUAL;
13925 	      End;
13926 	      --
13927 	      Begin
13928 		 INSERT INTO CSI_TRANSACTIONS(
13929 		      TRANSACTION_ID
13930 		     ,TRANSACTION_DATE
13931 		     ,SOURCE_TRANSACTION_DATE
13932 		     ,SOURCE_HEADER_REF
13933 		     ,TRANSACTION_TYPE_ID
13934 		     ,CREATED_BY
13935 		     ,CREATION_DATE
13936 		     ,LAST_UPDATED_BY
13937 		     ,LAST_UPDATE_DATE
13938 		     ,LAST_UPDATE_LOGIN
13939 		     ,OBJECT_VERSION_NUMBER
13940 		    )
13941 		    VALUES(
13942 		      v_txn_id                             -- TRANSACTION_ID
13943 		     ,SYSDATE                              -- TRANSACTION_DATE
13944 		     ,SYSDATE                              -- SOURCE_TRANSACTION_DATE
13945 		     ,'DATAFIX By IB-INV Synch'            -- SOURCE_HEADER_REF
13946 		     ,v_txn_type_id                        -- TRANSACTION_TYPE_ID
13947 		     ,v_created_by
13948 		     ,sysdate
13949 		     ,v_last_updated_by
13950 		     ,sysdate
13951 		     ,-1
13952 		     ,1
13953 		    );
13954 	      Exception
13955 		 when others then
13956 		    v_err_msg := 'Error while Inserting into CSI_TRANSACTIONS '||substr(sqlerrm,1,1000);
13957 		    raise_application_error(-20000, v_err_msg );
13958 		    Raise;
13959 	      End;
13960 	      --
13961 	      Begin
13962 		 select csi_item_instances_s.nextval
13963 		 into v_instance_id
13964 		 from DUAL;
13965 	      End;
13966 	      --
13967 	      -- Insert into CSI_ITEM_INSTANCES
13968 	      Begin
13969 		    INSERT INTO CSI_ITEM_INSTANCES(
13970 		      INSTANCE_ID
13971 		     ,INSTANCE_NUMBER
13972 		     ,EXTERNAL_REFERENCE
13973 		     ,INVENTORY_ITEM_ID
13974 		     ,INVENTORY_REVISION
13975 		     ,INV_MASTER_ORGANIZATION_ID
13976 		     ,MFG_SERIAL_NUMBER_FLAG
13977 		     ,LOT_NUMBER
13978 		     ,QUANTITY
13979 		     ,UNIT_OF_MEASURE
13980 		     ,ACCOUNTING_CLASS_CODE
13981 		     ,INSTANCE_CONDITION_ID
13982 		     ,INSTANCE_STATUS_ID
13983 		     ,CUSTOMER_VIEW_FLAG
13984 		     ,MERCHANT_VIEW_FLAG
13985 		     ,SELLABLE_FLAG
13986 		     ,SYSTEM_ID
13987 		     ,INSTANCE_TYPE_CODE
13988 		     ,ACTIVE_START_DATE
13989 		     ,ACTIVE_END_DATE
13990 		     ,LOCATION_TYPE_CODE
13991 		     ,LOCATION_ID
13992 		     ,INV_ORGANIZATION_ID
13993 		     ,INV_SUBINVENTORY_NAME
13994 		     ,INV_LOCATOR_ID
13995 		     ,PA_PROJECT_ID
13996 		     ,PA_PROJECT_TASK_ID
13997 		     ,IN_TRANSIT_ORDER_LINE_ID
13998 		     ,WIP_JOB_ID
13999 		     ,PO_ORDER_LINE_ID
14000 		     ,LAST_OE_ORDER_LINE_ID
14001 		     ,LAST_OE_RMA_LINE_ID
14002 		     ,LAST_PO_PO_LINE_ID
14003 		     ,LAST_OE_PO_NUMBER
14004 		     ,LAST_WIP_JOB_ID
14005 		     ,LAST_PA_PROJECT_ID
14006 		     ,LAST_PA_TASK_ID
14007 		     ,LAST_OE_AGREEMENT_ID
14008 		     ,INSTALL_DATE
14009 		     ,MANUALLY_CREATED_FLAG
14010 		     ,RETURN_BY_DATE
14011 		     ,ACTUAL_RETURN_DATE
14012 		     ,CREATION_COMPLETE_FLAG
14013 		     ,COMPLETENESS_FLAG
14014 		     ,CREATED_BY
14015 		     ,CREATION_DATE
14016 		     ,LAST_UPDATED_BY
14017 		     ,LAST_UPDATE_DATE
14018 		     ,LAST_UPDATE_LOGIN
14019 		     ,OBJECT_VERSION_NUMBER
14020 		     ,SECURITY_GROUP_ID
14021 		     ,INSTANCE_USAGE_CODE
14022 		     ,OWNER_PARTY_SOURCE_TABLE
14023 		     ,OWNER_PARTY_ID
14024 		     ,LAST_VLD_ORGANIZATION_ID
14025 		    )
14026 		    VALUES(
14027 		      v_instance_id                        -- INSTANCE_ID
14028 		     ,v_instance_id                        -- INSTANCE_NUMBER
14029 		     ,NULL                                 -- EXTERNAL_REFERENCE
14030 		     ,INVENTORY_ITEM_ID_mig(i)             -- INVENTORY_ITEM_ID
14031 		     ,revision_mig(i)                      -- INVENTORY_REVISION
14032 		     ,v_mast_org_id                        -- INV_MASTER_ORGANIZATION_ID
14033 		     ,v_mfg_srl_flag                       -- MFG_SERIAL_NUMBER_FLAG
14034 		     ,LOT_mig(i)                           -- LOT_NUMBER
14035 		     ,quantity_mig(i)                      -- QUANTITY
14036 		     ,uom_code_mig(i)                      -- UNIT_OF_MEASURE (PRIMARY)
14037 		     ,'INV'                                -- ACCOUNTING_CLASS_CODE
14038 		     ,v_ins_condition_id                   -- INSTANCE_CONDITION_ID
14039 		     ,v_ins_status_id                      -- INSTANCE_STATUS_ID
14040 		     ,'N'                                  -- CUSTOMER_VIEW_FLAG
14041 		     ,'Y'                                  -- MERCHANT_VIEW_FLAG
14042 		     ,'Y'                                  -- SELLABLE_FLAG
14043 		     ,NULL                                 -- SYSTEM_ID
14044 		     ,NULL                                 -- INSTANCE_TYPE_CODE
14045 		     ,SYSDATE                              -- ACTIVE_START_DATE
14046 		     ,NULL                                 -- ACTIVE_END_DATE
14047 		     ,'INVENTORY'                          -- LOCATION_TYPE_CODE
14048 		     ,v_location_id                        -- LOCATION_ID
14049 		     ,ORGANIZATION_ID_mig(i)               -- INV_ORGANIZATION_ID
14050 		     ,subinv_mig(i)                        -- INV_SUBINVENTORY_NAME
14051 		     ,LOCATOR_ID_mig(i)                    -- INV_LOCATOR_ID
14052 		     ,NULL                                 -- PA_PROJECT_ID
14053 		     ,NULL                                 -- PA_PROJECT_TASK_ID
14054 		     ,NULL                                 -- IN_TRANSIT_ORDER_LINE_ID
14055 		     ,NULL                                 -- WIP_JOB_ID
14056 		     ,NULL                                 -- PO_ORDER_LINE_ID
14057 		     ,NULL                                 -- LAST_OE_ORDER_LINE_ID
14058 		     ,NULL                                 -- LAST_OE_RMA_LINE_ID
14059 		     ,NULL                                 -- LAST_PO_PO_LINE_ID
14060 		     ,NULL                                 -- LAST_OE_PO_NUMBER
14061 		     ,NULL                                 -- LAST_WIP_JOB_ID
14062 		     ,NULL                                 -- LAST_PA_PROJECT_ID
14063 		     ,NULL                                 -- LAST_PA_TASK_ID
14064 		     ,NULL                                 -- LAST_OE_AGREEMENT_ID
14065 		     ,NULL                                 -- INSTALL_DATE
14066 		     ,'N'                                  -- MANUALLY_CREATED_FLAG
14067 		     ,NULL                                 -- RETURN_BY_DATE
14068 		     ,NULL                                 -- ACTUAL_RETURN_DATE
14069 		     ,'Y'                                  -- CREATION_COMPLETE_FLAG
14070 		     ,'Y'                                  -- COMPLETENESS_FLAG
14071 		     ,v_created_by                         -- CREATED_BY
14072 		     ,sysdate                              -- CREATION_DATE
14073 		     ,v_last_updated_by                    -- LAST_UPDATED_BY
14074 		     ,sysdate                              -- LAST_UPDATE_DATE
14075 		     ,-1                                   -- LAST_UPDATE_LOGIN
14076 		     ,1                                    -- OBJECT_VERSION_NUMBER
14077 		     ,NULL                                 -- SECURITY_GROUP_ID
14078 		     ,'IN_INVENTORY'                       -- INSTANCE_USAGE_CODE
14079 		     ,'HZ_PARTIES'                         -- OWNER_PARTY_SOURCE_TABLE
14080 		     ,v_party_id                           -- OWNER_PARTY_ID
14081 		     ,ORGANIZATION_ID_mig(i)               -- LAST_VLD_ORGANIZATION_ID
14082 		    );
14083 	      Exception
14084 		    when others then
14085 			  v_err_msg := 'Unable to Insert Item ID '||to_char(inventory_item_id_mig(i))
14086 						  ||' into CSI_ITEM_INSTANCES '||SUBSTR(sqlerrm,1,1000);
14087 			     raise_application_error(-20000, v_err_msg );
14088 			  Raise;
14089 	      End;
14090 		    -- Use the same instance record to create the history
14091 	      Begin
14092 		    select CSI_ITEM_INSTANCES_H_S.nextval
14093 		    into v_ins_history_id
14094 		    from DUAL;
14095 	      End;
14096 	      --
14097 	      Begin
14098 		    INSERT INTO CSI_ITEM_INSTANCES_H(
14099 		      INSTANCE_HISTORY_ID
14100 		     ,INSTANCE_ID
14101 		     ,TRANSACTION_ID
14102 		     ,OLD_INSTANCE_NUMBER
14103 		     ,NEW_INSTANCE_NUMBER
14104 		     ,OLD_EXTERNAL_REFERENCE
14105 		     ,NEW_EXTERNAL_REFERENCE
14106 		     ,OLD_INVENTORY_ITEM_ID
14107 		     ,NEW_INVENTORY_ITEM_ID
14108 		     ,OLD_INVENTORY_REVISION
14109 		     ,NEW_INVENTORY_REVISION
14110 		     ,OLD_INV_MASTER_ORGANIZATION_ID
14111 		     ,NEW_INV_MASTER_ORGANIZATION_ID
14112 		     ,OLD_MFG_SERIAL_NUMBER_FLAG
14113 		     ,NEW_MFG_SERIAL_NUMBER_FLAG
14114 		     ,OLD_LOT_NUMBER
14115 		     ,NEW_LOT_NUMBER
14116 		     ,OLD_QUANTITY
14117 		     ,NEW_QUANTITY
14118 		     ,OLD_UNIT_OF_MEASURE
14119 		     ,NEW_UNIT_OF_MEASURE
14120 		     ,OLD_ACCOUNTING_CLASS_CODE
14121 		     ,NEW_ACCOUNTING_CLASS_CODE
14122 		     ,OLD_INSTANCE_CONDITION_ID
14123 		     ,NEW_INSTANCE_CONDITION_ID
14124 		     ,OLD_INSTANCE_STATUS_ID
14125 		     ,NEW_INSTANCE_STATUS_ID
14126 		     ,OLD_CUSTOMER_VIEW_FLAG
14127 		     ,NEW_CUSTOMER_VIEW_FLAG
14128 		     ,OLD_MERCHANT_VIEW_FLAG
14129 		     ,NEW_MERCHANT_VIEW_FLAG
14130 		     ,OLD_SELLABLE_FLAG
14131 		     ,NEW_SELLABLE_FLAG
14132 		     ,OLD_SYSTEM_ID
14133 		     ,NEW_SYSTEM_ID
14134 		     ,OLD_INSTANCE_TYPE_CODE
14135 		     ,NEW_INSTANCE_TYPE_CODE
14136 		     ,OLD_ACTIVE_START_DATE
14137 		     ,NEW_ACTIVE_START_DATE
14138 		     ,OLD_ACTIVE_END_DATE
14139 		     ,NEW_ACTIVE_END_DATE
14140 		     ,OLD_LOCATION_TYPE_CODE
14141 		     ,NEW_LOCATION_TYPE_CODE
14142 		     ,OLD_LOCATION_ID
14143 		     ,NEW_LOCATION_ID
14144 		     ,OLD_INV_ORGANIZATION_ID
14145 		     ,NEW_INV_ORGANIZATION_ID
14146 		     ,OLD_INV_SUBINVENTORY_NAME
14147 		     ,NEW_INV_SUBINVENTORY_NAME
14148 		     ,OLD_INV_LOCATOR_ID
14149 		     ,NEW_INV_LOCATOR_ID
14150 		     ,OLD_PA_PROJECT_ID
14151 		     ,NEW_PA_PROJECT_ID
14152 		     ,OLD_PA_PROJECT_TASK_ID
14153 		     ,NEW_PA_PROJECT_TASK_ID
14154 		     ,OLD_IN_TRANSIT_ORDER_LINE_ID
14155 		     ,NEW_IN_TRANSIT_ORDER_LINE_ID
14156 		     ,OLD_WIP_JOB_ID
14157 		     ,NEW_WIP_JOB_ID
14158 		     ,OLD_PO_ORDER_LINE_ID
14159 		     ,NEW_PO_ORDER_LINE_ID
14160 		     ,OLD_COMPLETENESS_FLAG
14161 		     ,NEW_COMPLETENESS_FLAG
14162 		     ,CREATED_BY
14163 		     ,CREATION_DATE
14164 		     ,LAST_UPDATED_BY
14165 		     ,LAST_UPDATE_DATE
14166 		     ,LAST_UPDATE_LOGIN
14167 		     ,OBJECT_VERSION_NUMBER
14168 		     ,SECURITY_GROUP_ID
14169 		     ,FULL_DUMP_FLAG
14170 		     ,OLD_INST_USAGE_CODE
14171 		     ,NEW_INST_USAGE_CODE
14172 		     ,OLD_LAST_VLD_ORGANIZATION_ID
14173 		     ,NEW_LAST_VLD_ORGANIZATION_ID
14174 		    )
14175 		    VALUES(
14176 		      v_ins_history_id                                    -- INSTANCE_HISTORY_ID
14177 		     ,v_instance_id                                       -- INSTANCE_ID
14178 		     ,v_txn_id                                            -- TRANSACTION_ID
14179 		     ,NULL                                                -- OLD_INSTANCE_NUMBER
14180 		     ,v_instance_id                                       -- NEW_INSTANCE_NUMBER
14181 		     ,NULL                                                -- OLD_EXTERNAL_REFERENCE
14182 		     ,NULL                                                -- NEW_EXTERNAL_REFERENCE
14183 		     ,NULL                                                -- OLD_INVENTORY_ITEM_ID
14184 		     ,INVENTORY_ITEM_ID_mig(i)                            -- NEW_INVENTORY_ITEM_ID
14185 		     ,NULL                                                -- OLD_INVENTORY_REVISION
14186 		     ,revision_mig(i)                                     -- NEW_INVENTORY_REVISION
14187 		     ,NULL                                                -- OLD_INV_MASTER_ORGANIZATION_ID
14188 		     ,v_mast_org_id                                       -- NEW_INV_MASTER_ORGANIZATION_ID
14189 		     ,NULL                                                -- OLD_MFG_SERIAL_NUMBER_FLAG
14190 		     ,v_mfg_srl_flag                                      -- NEW_MFG_SERIAL_NUMBER_FLAG
14191 		     ,NULL                                                -- OLD LOT
14192 		     ,LOT_mig(i)                                          -- NEW LOT
14193 		     ,NULL                                                -- OLD_QUANTITY
14194 		     ,quantity_mig(i)                                     -- NEW_QUANTITY
14195 		     ,NULL                                                -- OLD_UNIT_OF_MEASURE
14196 		     ,uom_code_mig(i)                                     -- NEW_UNIT_OF_MEASURE
14197 		     ,NULL                                                -- OLD_ACCOUNTING_CLASS_CODE
14198 		     ,'INV'                                               -- NEW_ACCOUNTING_CLASS_CODE
14199 		     ,NULL                                                -- OLD_INSTANCE_CONDITION_ID
14200 		     ,v_ins_condition_id                                  -- NEW_INSTANCE_CONDITION_ID
14201 		     ,NULL                                                -- OLD_INSTANCE_STATUS_ID
14202 		     ,v_ins_status_id                                     -- NEW_INSTANCE_STATUS_ID
14203 		     ,NULL                                                -- OLD_CUSTOMER_VIEW_FLAG
14204 		     ,'N'                                                 -- NEW_CUSTOMER_VIEW_FLAG
14205 		     ,NULL                                                -- OLD_MERCHANT_VIEW_FLAG
14206 		     ,'Y'                                                 -- NEW_MERCHANT_VIEW_FLAG
14207 		     ,NULL                                                -- OLD_SELLABLE_FLAG
14208 		     ,NULL                                                -- NEW_SELLABLE_FLAG
14209 		     ,NULL                                                -- OLD_SYSTEM_ID
14210 		     ,NULL                                                -- NEW_SYSTEM_ID
14211 		     ,NULL                                                -- OLD_INSTANCE_TYPE_CODE
14212 		     ,NULL                                                -- NEW_INSTANCE_TYPE_CODE
14213 		     ,NULL                                                -- OLD_ACTIVE_START_DATE
14214 		     ,SYSDATE                                             -- NEW_ACTIVE_START_DATE
14215 		     ,NULL                                                -- OLD_ACTIVE_END_DATE
14216 		     ,NULL                                                -- NEW_ACTIVE_END_DATE
14217 		     ,NULL                                                -- OLD_LOCATION_TYPE_CODE
14218 		     ,'INVENTORY'                                         -- NEW_LOCATION_TYPE_CODE
14219 		     ,NULL                                                -- OLD_LOCATION_ID
14220 		     ,v_location_id                                       -- NEW_LOCATION_ID
14221 		     ,NULL                                                -- OLD_INV_ORGANIZATION_ID
14222 		     ,ORGANIZATION_ID_mig(i)                              -- NEW_INV_ORGANIZATION_ID
14223 		     ,NULL                                                -- OLD_INV_SUBINVENTORY_NAME
14224 		     ,subinv_mig(i)                                       -- NEW_INV_SUBINVENTORY_NAME
14225 		     ,NULL                                                -- OLD_INV_LOCATOR_ID
14226 		     ,LOCATOR_ID_mig(i)                                   -- NEW_INV_LOCATOR_ID
14227 		     ,NULL                                                -- OLD_PA_PROJECT_ID
14228 		     ,NULL                                                -- NEW_PA_PROJECT_ID
14229 		     ,NULL                                                -- OLD_PA_PROJECT_TASK_ID
14230 		     ,NULL                                                -- NEW_PA_PROJECT_TASK_ID
14231 		     ,NULL                                                -- OLD_IN_TRANSIT_ORDER_LINE_ID
14232 		     ,NULL                                                -- NEW_IN_TRANSIT_ORDER_LINE_ID
14233 		     ,NULL                                                -- OLD_WIP_JOB_ID
14234 		     ,NULL                                                -- NEW_WIP_JOB_ID
14235 		     ,NULL                                                -- OLD_PO_ORDER_LINE_ID
14236 		     ,NULL                                                -- NEW_PO_ORDER_LINE_ID
14237 		     ,NULL                                                -- OLD_COMPLETENESS_FLAG
14238 		     ,'Y'                                                 -- NEW_COMPLETENESS_FLAG
14239 		     ,v_created_by                                        -- CREATED_BY
14240 		     ,sysdate                                             -- CREATION_DATE
14241 		     ,v_last_updated_by                                   -- LAST_UPDATED_BY
14242 		     ,sysdate                                             -- LAST_UPDATE_DATE
14243 		     ,-1                                                  -- LAST_UPDATE_LOGIN
14244 		     ,1                                                   -- OBJECT_VERSION_NUMBER
14245 		     ,NULL                                                -- SECURITY_GROUP_ID
14246 		     ,'N'                                                 -- FULL_DUMP_FLAG
14247 		     ,NULL                                                -- OLD_INST_USAGE_CODE
14248 		     ,'IN_INVENTORY'                                      -- NEW_INST_USAGE_CODE
14249 		     ,NULL                                                -- OLD_LAST_VLD_ORGANIZATION_ID
14250 		     ,ORGANIZATION_ID_mig(i)                              -- NEW_LAST_VLD_ORGANIZATION_ID
14251 		    );
14252 	      Exception
14253 		    when others then
14254 			  v_err_msg := 'Unable to Insert Item ID '||to_char(inventory_item_id_mig(i))
14255 				   ||' into CSI_ITEM_INSTANCES_H Using the Same Instance '||SUBSTR(sqlerrm,1,1000);
14256 			     raise_application_error(-20000, v_err_msg );
14257 			  Raise;
14258 	      End;
14259 	      --
14260 	      Begin
14261 		 select CSI_I_PARTIES_S.nextval
14262 		 into v_ins_party_id
14263 		 from DUAL;
14264 	      End;
14265 	      Begin
14266 		    INSERT INTO CSI_I_PARTIES(
14267 		      INSTANCE_PARTY_ID
14268 		     ,INSTANCE_ID
14269 		     ,PARTY_SOURCE_TABLE
14270 		     ,PARTY_ID
14271 		     ,RELATIONSHIP_TYPE_CODE
14272 		     ,CONTACT_FLAG
14273 		     ,CONTACT_IP_ID
14274 		     ,ACTIVE_START_DATE
14275 		     ,ACTIVE_END_DATE
14276 		     ,CREATED_BY
14277 		     ,CREATION_DATE
14278 		     ,LAST_UPDATED_BY
14279 		     ,LAST_UPDATE_DATE
14280 		     ,LAST_UPDATE_LOGIN
14281 		     ,OBJECT_VERSION_NUMBER
14282 		     ,SECURITY_GROUP_ID
14283 		    )
14284 		    VALUES(
14285 		      v_ins_party_id                   -- INSTANCE_PARTY_ID
14286 		     ,v_instance_id                    -- INSTANCE_ID
14287 		     ,'HZ_PARTIES'                     -- PARTY_SOURCE_TABLE
14288 		     ,v_party_id                       -- PARTY_ID
14289 		     ,'OWNER'                          -- RELATIONSHIP_TYPE_CODE
14290 		     ,'N'                              -- CONTACT_FLAG
14291 		     ,NULL                             -- CONTACT_IP_ID
14292 		     ,SYSDATE                          -- ACTIVE_START_DATE
14293 		     ,NULL                             -- ACTIVE_END_DATE
14294 		     ,v_created_by                     -- CREATED_BY
14295 		     ,sysdate                          -- CREATION_DATE
14296 		     ,v_last_updated_by                -- LAST_UPDATED_BY
14297 		     ,sysdate                          -- LAST_UPDATE_DATE
14298 		     ,-1                               -- LAST_UPDATE_LOGIN
14299 		     ,1                                -- OBJECT_VERSION_NUMBER
14300 		     ,NULL                             -- SECURITY_GROUP_ID
14301 		    );
14302 		    Exception
14303 			  when others then
14304 			  v_err_msg := 'Unable to Insert Item ID '||to_char(inventory_item_id_mig(i))
14305 						  ||' into CSI_I_PARTIES '||SUBSTR(sqlerrm,1,1000);
14306 			     raise_application_error(-20000, v_err_msg );
14307 			  Raise;
14308 		    End;
14309 		    -- Insert into CSI_I_PARTIES_H
14310 	      Begin
14311 		 select CSI_I_PARTIES_H_S.nextval
14312 		 into v_ins_party_history_id
14313 		 from DUAL;
14314 	      End;
14315 	      -- Insert into CSI_I_PARTIES_H
14316 	      Begin
14317 		    INSERT INTO CSI_I_PARTIES_H(
14318 		      INSTANCE_PARTY_HISTORY_ID
14319 		     ,INSTANCE_PARTY_ID
14320 		     ,TRANSACTION_ID
14321 		     ,OLD_PARTY_SOURCE_TABLE
14322 		     ,NEW_PARTY_SOURCE_TABLE
14323 		     ,OLD_PARTY_ID
14324 		     ,NEW_PARTY_ID
14325 		     ,OLD_RELATIONSHIP_TYPE_CODE
14326 		     ,NEW_RELATIONSHIP_TYPE_CODE
14327 		     ,OLD_CONTACT_FLAG
14328 		     ,NEW_CONTACT_FLAG
14329 		     ,OLD_CONTACT_IP_ID
14330 		     ,NEW_CONTACT_IP_ID
14331 		     ,OLD_ACTIVE_START_DATE
14332 		     ,NEW_ACTIVE_START_DATE
14333 		     ,OLD_ACTIVE_END_DATE
14334 		     ,NEW_ACTIVE_END_DATE
14335 		     ,FULL_DUMP_FLAG
14336 		     ,CREATED_BY
14337 		     ,CREATION_DATE
14338 		     ,LAST_UPDATED_BY
14339 		     ,LAST_UPDATE_DATE
14340 		     ,LAST_UPDATE_LOGIN
14341 		     ,OBJECT_VERSION_NUMBER
14342 		     ,SECURITY_GROUP_ID
14343 		    )
14344 		    VALUES(
14345 		      v_ins_party_history_id               -- INSTANCE_PARTY_HISTORY_ID
14346 		     ,v_ins_party_id                       -- INSTANCE_PARTY_ID
14347 		     ,v_txn_id                             -- TRANSACTION_ID
14348 		     ,NULL                                 -- OLD_PARTY_SOURCE_TABLE
14349 		     ,'HZ_PARTIES'                         -- NEW_PARTY_SOURCE_TABLE
14350 		     ,NULL                                 -- OLD_PARTY_ID
14351 		     ,v_party_id                           -- NEW_PARTY_ID
14352 		     ,NULL                                 -- OLD_RELATIONSHIP_TYPE_CODE
14353 		     ,'OWNER'                              -- NEW_RELATIONSHIP_TYPE_CODE
14354 		     ,NULL                                 -- OLD_CONTACT_FLAG
14355 		     ,'N'                                  -- NEW_CONTACT_FLAG
14356 		     ,NULL                                 -- OLD_CONTACT_IP_ID
14357 		     ,NULL                                 -- NEW_CONTACT_IP_ID
14358 		     ,NULL                                 -- OLD_ACTIVE_START_DATE
14359 		     ,SYSDATE                              -- NEW_ACTIVE_START_DATE
14360 		     ,NULL                                 -- OLD_ACTIVE_END_DATE
14361 		     ,NULL                                 -- NEW_ACTIVE_END_DATE
14362 		     ,'N'                                  -- FULL_DUMP_FLAG
14363 		     ,v_created_by
14364 		     ,sysdate
14365 		     ,v_last_updated_by
14366 		     ,sysdate
14367 		     ,-1
14368 		     ,1
14369 		     ,NULL
14370 		    );
14371 	      Exception
14372 		    when others then
14373 			  v_err_msg := 'Unable to Insert Item ID '||to_char(inventory_item_id_mig(i))
14374 				   ||' into CSI_I_PARTIES_H using the same Instance '||SUBSTR(sqlerrm,1,1000);
14375 			     raise_application_error(-20000, v_err_msg );
14376 			  Raise;
14377 	      End;
14378 	   Exception
14379 	      when process_next then
14380 		 null;
14381 	      when others then
14382 		 v_err_msg := substr(sqlerrm,1,1000);
14383 		 raise_application_error(-20000, v_err_msg );
14384 		 Raise;
14385 	   End;
14386         END LOOP; -- for loop
14387         commit;
14388         EXIT WHEN INV_ONH_BAL_CUR%NOTFOUND;
14389      END LOOP; -- Open loop
14390      commit;
14391      CLOSE INV_ONH_BAL_CUR;
14392   EXCEPTION
14393      when comp_error then
14394 	null;
14395   END IB_INV_Synch_Non_srl;
14396 
14397   --
14398   PROCEDURE mark_error_transactions IS
14399     CURSOR CSI_CUR IS
14400       SELECT cii.transaction_error_id,
14401              mmt.inventory_item_id,
14402              mmt.organization_id,
14403              mmt.transaction_action_id,
14404              mmt.transaction_source_type_id,
14405              mmt.transaction_id
14406       FROM   csi_txn_errors cii,
14407              mtl_material_transactions mmt
14408       WHERE  cii.processed_flag in ('E', 'R')
14409       AND    cii.inv_material_transaction_id is not null
14410       AND    mmt.transaction_id =  cii.inv_material_transaction_id;
14411 
14412     TYPE NumTabType is varray(10000) of number;
14413 
14414     txn_error_id          NumTabType;
14415     item_id               NumTabType;
14416     inv_org_id            NumTabType;
14417     mmt_action_id         NumTabType;
14418     mmt_source_type_id    NumTabType;
14419     mat_txn_id            NumTabType;
14420     --
14421     max_buffer_size       number := 1000;
14422     --
14423     l_user_id             number := fnd_global.user_id;
14424     v_srl_ctl             number;
14425     v_txn_type_id         number;
14426     v_lot_ctl             number;
14427     l_ctr                 number := 0;
14428     --
14429 
14430     TYPE numlist is TABLE of number INDEX BY binary_integer;
14431     l_upd_txn_tbl         numlist;
14432     --
14433     process_next          exception;
14434     comp_error            exception;
14435 
14436   BEGIN
14437 
14438     -- Get the Transaction Type ID for Txn Type MIGRATED
14439     BEGIN
14440       SELECT transaction_type_id
14441       INTO   v_txn_type_id
14442       FROM   csi_txn_types
14443       WHERE  source_transaction_type = 'DATA_CORRECTION';
14444     EXCEPTION
14445       WHEN no_data_found THEN
14446         RAISE comp_error;
14447       WHEN others THEN
14448 	RAISE comp_error;
14449     END;
14450     --
14451 
14452     OPEN CSI_CUR;
14453     LOOP
14454       FETCH CSI_CUR BULK COLLECT INTO
14455         txn_error_id,
14456         item_id,
14457         inv_org_id,
14458         mmt_action_id,
14459         mmt_source_type_id,
14460         mat_txn_id
14461       LIMIT MAX_BUFFER_SIZE;
14462       --
14463       FOR i in 1 .. txn_error_id.count
14464       LOOP
14465         BEGIN
14466 
14467           -- skip wip errors
14468           IF mmt_source_type_id(i) = 5 THEN
14469             RAISE process_next;
14470           END IF;
14471 
14472           -- skip shipment and RMA errors
14473           IF (mmt_action_id(i) = 1  AND mmt_source_type_id(i) = 2)  -- Sales Order Issue
14474               OR
14475              (mmt_action_id(i) = 27 AND mmt_source_type_id(i) = 12) -- RMA Receipt
14476           THEN
14477             RAISE process_next;
14478           END IF;
14479 
14480           --
14481           BEGIN
14482             SELECT serial_number_control_code,
14483                    lot_control_code
14484             INTO   v_srl_ctl,
14485                    v_lot_ctl
14486             FROM   mtl_system_items
14487             WHERE  inventory_item_id = item_id(i)
14488             AND    organization_id   = inv_org_id(i);
14489           EXCEPTION
14490             WHEN no_data_found THEN
14491               RAISE process_next;
14492           END;
14493 
14494           --
14495           IF v_srl_ctl not in (1, 6) THEN
14496             RAISE process_next;
14497           END IF;
14498           --
14499 
14500           IF v_srl_ctl = 6 THEN
14501 
14502             -- skip iso shipments for srlsoi items
14503             IF (mmt_action_id(i) = 21 AND mmt_source_type_id(i) = 8)  -- Int Order Intr Ship
14504                 OR
14505                (mmt_action_id(i) = 12 AND mmt_source_type_id(i) = 7)  -- Int Req Intr Rcpt
14506                 OR
14507                (mmt_action_id(i) = 3  AND mmt_source_type_id(i) = 8)  -- Int Order Direct Ship
14508                 OR
14509                (mmt_action_id(i) = 1  AND mmt_source_type_id(i) = 8)  -- Internal order issue
14510             THEN
14511               RAISE process_next;
14512             END IF;
14513           END IF;
14514 
14515           l_ctr := l_ctr + 1;
14516           l_upd_txn_tbl(l_ctr) := txn_error_id(i);
14517 
14518         EXCEPTION
14519           WHEN process_next THEN
14520             null;
14521         END;
14522       END LOOP;
14523 
14524       EXIT WHEN csi_cur%notfound;
14525 
14526     END LOOP;
14527 
14528     CLOSE csi_cur;
14529     --
14530     IF l_upd_txn_tbl.count > 0 THEN
14531       FORALL i in l_upd_txn_tbl.FIRST .. l_upd_txn_tbl.LAST
14532         UPDATE csi_txn_errors
14533         SET    processed_flag = 'D',
14534                error_text = 'Data fix done - mark_error_transactions',
14535                last_update_date = sysdate
14536         WHERE  transaction_error_id = l_upd_txn_tbl(i);
14537         commit;
14538     END IF;
14539   EXCEPTION
14540     WHEN comp_error THEN
14541       null;
14542   END mark_error_transactions;
14543 
14544   --
14545   PROCEDURE Reverse_IB_INV_Synch IS
14546      CURSOR CSI_CUR IS
14547      select cii.instance_id,cii.inventory_item_id,cii.inv_organization_id,
14548      cii.inv_subinventory_name,cii.inv_locator_id,
14549      cii.inventory_revision,cii.lot_number,cii.quantity
14550      from CSI_ITEM_INSTANCES cii,
14551           MTL_SYSTEM_ITEMS msi
14552      where cii.location_type_code = 'INVENTORY'
14553      and   cii.instance_usage_code = 'IN_INVENTORY'
14554      and   cii.serial_number is NULL
14555      and   msi.inventory_item_id = cii.inventory_item_id
14556      and   msi.organization_id = cii.inv_master_organization_id
14557      and   nvl(msi.comms_nl_trackable_flag,'N') = 'Y';
14558      --
14559      v_qty                        NUMBER;
14560      l_txn_id                     NUMBER;
14561      l_user_id                    NUMBER := fnd_global.user_id;
14562      l_error_count                NUMBER;
14563      l_ins_flag                   VARCHAR2(1) := 'N';
14564      v_txn_type_id                NUMBER;
14565      v_err_msg                    VARCHAR2(2000);
14566      --
14567      Type NumTabType is VARRAY(10000) of NUMBER;
14568      instance_id_mig          NumTabType;
14569      organization_id_mig      NumTabType;
14570      inventory_item_id_mig    NumTabType;
14571      locator_id_mig           NumTabType;
14572      quantity_mig             NumTabType;
14573      --
14574      Type V3Type is VARRAY(10000) of VARCHAR2(3);
14575      revision_mig             V3Type;
14576      --
14577      Type V10Type is VARRAY(10000) of VARCHAR2(10);
14578      subinv_mig               V10Type;
14579      --
14580      Type V80Type is VARRAY(10000) of VARCHAR2(80);
14581      lot_mig                  V80Type;
14582      --
14583      MAX_BUFFER_SIZE        NUMBER := 1000;
14584      --
14585      process_next           EXCEPTION;
14586      comp_error             EXCEPTION;
14587   BEGIN
14588      csi_t_gen_utility_pvt.build_file_name(
14589          p_file_segment1 => 'csinonsy',
14590          p_file_segment2 => to_char(sysdate, 'hh24miss'));
14591      --
14592      -- Get the Transaction Type ID for Txn Type MIGRATED
14593      Begin
14594 	select transaction_type_id
14595 	into v_txn_type_id
14596 	from CSI_TXN_TYPES
14597 	where SOURCE_TRANSACTION_TYPE = 'DATA_CORRECTION';
14598      Exception
14599 	when no_data_found then
14600 	   Raise comp_error;
14601 	when others then
14602 	   Raise comp_error;
14603      End;
14604      select CSI_TRANSACTIONS_S.nextval
14605      into l_txn_id from dual;
14606      --
14607      OPEN CSI_CUR;
14608      LOOP
14609         FETCH CSI_CUR BULK COLLECT INTO
14610         instance_id_mig,
14611         inventory_item_id_mig,
14612         organization_id_mig,
14613         subinv_mig,
14614         locator_id_mig,
14615         revision_mig,
14616         lot_mig,
14617         quantity_mig
14618         LIMIT MAX_BUFFER_SIZE;
14619         --
14620         FOR i in 1 .. instance_id_mig.count LOOP
14621 	   Begin
14622               l_error_count := 0;
14623               Begin
14624                  select count(*)
14625                  into l_error_count
14626                  from CSI_TXN_ERRORS cii,
14627                       MTL_MATERIAL_TRANSACTIONS mmt
14628                  where cii.inv_material_transaction_id is not null
14629                  and   cii.inv_material_transaction_id = mmt.transaction_id
14630                  and   cii.processed_flag in ('E','R')
14631                  and   mmt.inventory_item_id = inventory_item_id_mig(i)
14632                  and   mmt.organization_id = organization_id_mig(i);
14633               End;
14634               --
14635               IF nvl(l_error_count,0) > 0 THEN
14636                  v_err_msg := 'Unable to Reverse Synch Item ID '||to_char(inventory_item_id_mig(i))||
14637                               '  Under Organization '||to_char(organization_id_mig(i)||
14638 						' as there are transaction errors for this item. '); -- added for bug 14282043
14639                  Out(v_err_msg);
14640                  Raise Process_next;
14641               END IF;
14642               --
14643 	      v_qty := 0;
14644 	      Begin
14645 	         select NVL(sum(transaction_quantity),0)
14646 	         into v_qty
14647 	         from MTL_ONHAND_QUANTITIES
14648 	         where inventory_item_id = inventory_item_id_mig(i)
14649 	         and   organization_id = organization_id_mig(i)
14650 	         and   subinventory_code = subinv_mig(i)
14651 	         and   nvl(locator_id,-999) = nvl(locator_id_mig(i),-999)
14652 	         and   nvl(revision,'$#$') = nvl(revision_mig(i),'$#$')
14653 	         and   nvl(lot_number,'$#$') = nvl(lot_mig(i),'$#$');
14654 		     --Added for bug 14282043
14655 		  EXCEPTION
14656 			WHEN NO_DATA_FOUND THEN
14657 			  v_qty := 0;
14658 		    --Added for bug 14282043
14659 	      End;
14660 	      --
14661 	      IF v_qty <> quantity_mig(i) THEN
14662                  l_ins_flag := 'Y';
14663 	         UPDATE CSI_ITEM_INSTANCES
14664 	         set quantity = v_qty,
14665 	   	     active_end_date = decode(v_qty,0,sysdate,active_end_date),
14666 		     instance_status_id = decode(v_qty,0,1,instance_status_id),
14667                      last_update_date = sysdate,
14668                      last_updated_by = l_user_id
14669 	         where instance_id = instance_id_mig(i);
14670                  --
14671                  -- Tie this instance with the transaction
14672 		 INSERT INTO CSI_ITEM_INSTANCES_H
14673 		     (
14674 		      INSTANCE_HISTORY_ID
14675 		     ,TRANSACTION_ID
14676 		     ,INSTANCE_ID
14677 		     ,CREATION_DATE
14678 		     ,LAST_UPDATE_DATE
14679 		     ,CREATED_BY
14680 		     ,LAST_UPDATED_BY
14681 		     ,LAST_UPDATE_LOGIN
14682 		     ,OBJECT_VERSION_NUMBER
14683 		    )
14684 		 VALUES
14685 		    (
14686                       CSI_ITEM_INSTANCES_H_S.nextval
14687 		     ,l_txn_id
14688 		     ,instance_id_mig(i)
14689 		     ,SYSDATE
14690 		     ,SYSDATE
14691 		     ,l_user_id
14692 		     ,l_user_id
14693 		     ,-1
14694 		     ,1
14695 		    );
14696 	      END IF;
14697            Exception
14698               when Process_next then
14699                  null;
14700 	   End;
14701         END LOOP; -- for loop
14702         commit;
14703         EXIT WHEN CSI_CUR%NOTFOUND;
14704      END LOOP;
14705      CLOSE CSI_CUR;
14706      --
14707      IF l_ins_flag = 'Y' THEN
14708         INSERT INTO CSI_TRANSACTIONS(
14709 	  TRANSACTION_ID
14710 	 ,TRANSACTION_DATE
14711 	 ,SOURCE_TRANSACTION_DATE
14712 	 ,SOURCE_HEADER_REF
14713 	 ,TRANSACTION_TYPE_ID
14714 	 ,CREATED_BY
14715 	 ,CREATION_DATE
14716 	 ,LAST_UPDATED_BY
14717 	 ,LAST_UPDATE_DATE
14718 	 ,LAST_UPDATE_LOGIN
14719 	 ,OBJECT_VERSION_NUMBER
14720 	)
14721 	VALUES(
14722 	  l_txn_id                             -- TRANSACTION_ID
14723 	 ,SYSDATE                              -- TRANSACTION_DATE
14724 	 ,SYSDATE                              -- SOURCE_TRANSACTION_DATE
14725 	 ,'Reverse Synch'                      -- SOURCE_HEADER_REF
14726 	 ,v_txn_type_id                        -- TRANSACTION_TYPE_ID
14727 	 ,l_user_id
14728 	 ,sysdate
14729 	 ,l_user_id
14730 	 ,sysdate
14731 	 ,-1
14732 	 ,1
14733 	);
14734      END IF;
14735      --
14736      commit;
14737   EXCEPTION
14738      when comp_error then
14739         null;
14740   END Reverse_IB_INV_Synch;
14741   --
14742   PROCEDURE get_nl_trackable_report
14743   IS
14744     CURSOR all_item_cur IS
14745       SELECT m_msi.concatenated_segments,
14746              m_msi.comms_nl_trackable_flag,
14747              c_msi.comms_nl_trackable_flag,
14748              m_mp.organization_code,
14749              c_mp.organization_code
14750       FROM   mtl_system_items_b   c_msi, -- Child Items
14751              mtl_parameters       c_mp,  -- Child Parameters
14752              mtl_system_items_kfv m_msi, -- Master Items
14753              mtl_parameters       m_mp   -- Master Parameters
14754       WHERE  m_mp.organization_id        = m_mp.master_organization_id
14755       AND    m_msi.organization_id       = m_mp.organization_id
14756       AND    m_msi.organization_id       = c_mp.master_organization_id
14757       AND    c_mp.master_organization_id = m_mp.organization_id
14758       AND    c_msi.organization_id       = c_mp.organization_id
14759       AND    m_msi.organization_id      <> c_mp.organization_id
14760       AND    c_msi.organization_id      <> m_msi.organization_id
14761       AND    c_msi.inventory_item_id     = m_msi.inventory_item_id
14762       AND    nvl(m_msi.comms_nl_trackable_flag,'N') <> nvl(c_msi.comms_nl_trackable_flag,'N')
14763       AND  EXISTS (SELECT 1 from  HR_ALL_ORGANIZATION_UNITS haou
14764       WHERE date_to IS NULL
14765       AND haou.organization_id =m_msi.organization_id)
14766       AND  EXISTS (SELECT 1 from  HR_ALL_ORGANIZATION_UNITS haou
14767       WHERE date_to IS NULL
14768       AND haou.organization_id =c_msi.organization_id)
14769       ORDER BY m_msi.inventory_item_id;
14770     --
14771     l_printheader  boolean := TRUE;
14772     l_printfooter  boolean := FALSE;
14773 
14774     v_msg          varchar2(4000);
14775     --
14776     Type V1TabType is VARRAY(10000) of VARCHAR2(1);
14777     l_master_flag_mig        V1TabType;
14778     l_child_flag_mig         V1TabType;
14779     Type V3TabType is VARRAY(10000) of VARCHAR2(3);
14780     l_master_org_mig         V3TabType;
14781     l_child_org_mig          V3TabType;
14782     Type V240TabType is VARRAY(10000) of MTL_SYSTEM_ITEMS_KFV.CONCATENATED_SEGMENTS%TYPE;
14783     l_item_segment_mig       V240TabType;
14784     --
14785     MAX_BUFFER_SIZE          NUMBER := 1000;
14786   BEGIN
14787     OPEN ALL_ITEM_CUR;
14788     LOOP
14789       FETCH ALL_ITEM_CUR BULK COLLECT INTO
14790         l_item_segment_mig,
14791         l_master_flag_mig,
14792         l_child_flag_mig,
14793         l_master_org_mig,
14794         l_child_org_mig
14795       LIMIT MAX_BUFFER_SIZE;
14796       --
14797       FOR i in 1..l_item_segment_mig.count LOOP
14798         IF l_printheader THEN
14799           l_printheader := FALSE;
14800 	  l_printfooter := TRUE;
14801 	  out('-----------------------------------------------------------------------');
14802 	  out('Inconsistent IB trackable setting within master and child organizations');
14803 	  out('-----------------------------------------------------------------------');
14804         END IF;
14805         l_global_warning_flag := 'Y';
14806         v_msg := 'Item : '||l_item_segment_mig(i)||
14807  	         '  is set as  '||l_master_flag_mig(i)||
14808 	         '  in Master Org '||l_master_org_mig(i)||
14809 	         '  and  '|| l_child_flag_mig(i)||
14810 	         '  in Child Org '||l_child_org_mig(i);
14811         out(v_msg);
14812       END LOOP;
14813       EXIT WHEN ALL_ITEM_CUR%NOTFOUND;
14814     END LOOP;
14815      --
14816     IF ALL_ITEM_CUR%ISOPEN THEN
14817       CLOSE ALL_ITEM_CUR;
14818     END IF;
14819     --
14820     IF l_printfooter THEN
14821        out('***************************END OF REPORT**********************************');
14822     END IF;
14823   END get_nl_trackable_report;
14824   --
14825   PROCEDURE MERGE_NON_SRL_INV_INSTANCE IS
14826    CURSOR c1 IS
14827    SELECT /*+ parallel(a) parallel(c) */
14828          a.instance_id,
14829          a.inventory_item_id,
14830          a.location_type_code,
14831          a.location_id,
14832          a.inv_organization_id,
14833          a.inv_subinventory_name,
14834          a.instance_usage_code,
14835          a.quantity,
14836          a.active_end_date,
14837          a.inventory_revision,
14838          a.inv_locator_id,
14839          a.lot_number,
14840          a.owner_party_id
14841    FROM   csi_item_instances a,
14842          mtl_system_items_b c
14843    WHERE a.ROWID > (SELECT MIN(b.ROWID)
14844                    FROM csi_item_instances b
14845                    WHERE b.inventory_item_id = a.inventory_item_id
14846                    AND   b.location_type_code = a.location_type_code
14847                  --  AND   b.location_id = a.location_id
14848                    AND   b.serial_number is null
14849                    AND   b.inv_organization_id = a.inv_organization_id
14850                    AND   b.inv_subinventory_name = a.inv_subinventory_name
14851                    AND   b.instance_usage_code = a.instance_usage_code
14852                    AND   nvl(b.inventory_revision,'$#$') = nvl(a.inventory_revision,'$#$')
14853                    AND   nvl(b.inv_locator_id,-999) = nvl(a.inv_locator_id,-999)
14854                    AND   nvl(b.lot_number,'$#$')= nvl(a.lot_number,'$#$')
14855                    AND   b.owner_party_id = a.owner_party_id
14856                    AND   b.location_type_code = 'INVENTORY'
14857                    AND   b.instance_usage_code = 'IN_INVENTORY')
14858                  --  AND   b.active_end_date IS NULL)
14859  -- AND a.active_end_date IS NULL
14860   AND a.inventory_item_id = c.inventory_item_id
14861   AND a.inv_organization_id = c.organization_id
14862   AND a.serial_number is null
14863   AND c.serial_number_control_code IN (1,6);
14864 
14865   CURSOR c2  IS
14866   SELECT a.instance_id,
14867          a.inventory_item_id,
14868          a.location_type_code,
14869          a.location_id,
14870          a.inv_organization_id,
14871          a.inv_subinventory_name,
14872          a.instance_usage_code,
14873          a.quantity,
14874          a.active_end_date,
14875          a.inventory_revision,
14876          a.inv_locator_id,
14877          a.lot_number,
14878          a.owner_party_id
14879   FROM   csi_item_instances a,
14880          mtl_system_items_b c
14881   WHERE  a.ROWID = (SELECT MIN(b.ROWID)
14882                    FROM csi_item_instances b
14883                    WHERE b.inventory_item_id = a.inventory_item_id
14884                    AND   b.location_type_code = a.location_type_code
14885                  --  AND   b.location_id = a.location_id
14886                    AND   b.serial_number is null
14887                    AND   b.inv_organization_id = a.inv_organization_id
14888                    AND   b.inv_subinventory_name = a.inv_subinventory_name
14889                    AND   b.instance_usage_code = a.instance_usage_code
14890                    AND   nvl(b.inventory_revision,'$#$') = nvl(a.inventory_revision,'$#$')
14891                    AND   nvl(b.inv_locator_id,-999) = nvl(a.inv_locator_id,-999)
14892                    AND   nvl(b.lot_number,'$#$')= nvl(a.lot_number,'$#$')
14893                    AND   b.owner_party_id = a.owner_party_id
14894                    AND   b.location_type_code = 'INVENTORY'
14895                    AND   b.instance_usage_code = 'IN_INVENTORY')
14896                  --  AND   b.active_end_date IS NULL)
14897   AND a.inventory_item_id = c.inventory_item_id
14898   AND a.inv_organization_id = c.organization_id
14899   AND a.serial_number is null
14900   AND c.serial_number_control_code IN (1,6);
14901 
14902   p_instance_tbl     csi_datastructures_pub.instance_tbl;
14903   l_count            NUMBER;
14904   m                  NUMBER;
14905   temp_quantity      NUMBER;
14906   l_status_id        NUMBER;
14907   l_active_end_date  DATE;
14908  BEGIN
14909     m := 1;
14910     FOR i IN c1 LOOP
14911          p_instance_tbl(m).inventory_item_id :=i.inventory_item_id;
14912          p_instance_tbl(m).location_type_code :=i.location_type_code;
14913          p_instance_tbl(m).inv_organization_id :=i.inv_organization_id;
14914          p_instance_tbl(m).inv_subinventory_name :=i.inv_subinventory_name;
14915          p_instance_tbl(m).instance_usage_code :=i.instance_usage_code;
14916          p_instance_tbl(m).location_id :=i.location_id;
14917          p_instance_tbl(m).quantity:=i.quantity;
14918          p_instance_tbl(m).inventory_revision:=i.inventory_revision;
14919          p_instance_tbl(m).inv_locator_id:=i.inv_locator_id;
14920          p_instance_tbl(m).lot_number:=i.lot_number;
14921          p_instance_tbl(m).attribute1:=i.owner_party_id;
14922 
14923          DELETE FROM csi_item_instances_h
14924          WHERE  instance_id=i.instance_id;
14925          --
14926          DELETE FROM csi_i_parties_h
14927          WHERE instance_party_id in (select instance_party_id from csi_i_parties
14928                                      WHERE  instance_id=i.instance_id);
14929          --
14930          DELETE FROM csi_i_parties
14931          WHERE  instance_id=i.instance_id;
14932          --
14933          DELETE FROM csi_item_instances
14934          WHERE  instance_id=i.instance_id;
14935 
14936          m := m+1;
14937 
14938      END LOOP;
14939      l_count:=p_instance_tbl.COUNT;
14940      IF l_count > 0 THEN
14941         FOR j IN c2 LOOP
14942           temp_quantity:=0;
14943            FOR k in 1..l_count
14944            LOOP
14945               IF   p_instance_tbl(k).inventory_item_id =j.inventory_item_id
14946                AND p_instance_tbl(k).location_type_code =j.location_type_code
14947                AND p_instance_tbl(k).inv_organization_id =j.inv_organization_id
14948                AND p_instance_tbl(k).inv_subinventory_name =j.inv_subinventory_name
14949                AND p_instance_tbl(k).instance_usage_code =j.instance_usage_code
14950              --  AND p_instance_tbl(k).location_id =j.location_id
14951                AND nvl(p_instance_tbl(k).inventory_revision,'$#$')=nvl(j.inventory_revision,'$#$')
14952                AND nvl(p_instance_tbl(k).inv_locator_id,-999)=nvl(j.inv_locator_id,-999)
14953                AND nvl(p_instance_tbl(k).lot_number,'$#$')=nvl(j.lot_number,'$#$')
14954                AND p_instance_tbl(k).attribute1 = j.owner_party_id
14955              --  AND j.active_end_date IS NULL
14956               THEN
14957                 temp_quantity:=temp_quantity+p_instance_tbl(k).quantity;
14958               END IF;
14959            END LOOP;
14960 
14961            IF j.quantity + temp_quantity = 0 THEN
14962               l_status_id := 1;
14963               l_active_end_date := sysdate;
14964            ELSE
14965               l_status_id := 510;
14966               l_active_end_date := null;
14967            END IF;
14968            --
14969            UPDATE csi_item_instances
14970            SET quantity=j.quantity+temp_quantity,
14971                instance_status_id = l_status_id,
14972                active_end_date = l_active_end_date
14973            WHERE instance_id=j.instance_id;
14974 
14975         END LOOP;
14976      END IF;
14977      commit;
14978   END MERGE_NON_SRL_INV_INSTANCE;
14979 
14980   PROCEDURE get_non_srl_rma_report(
14981     p_show_instances      IN varchar2)
14982   IS
14983 
14984     l_ownership_override  varchar2(1);
14985 
14986     CURSOR CSI_ERROR IS
14987       SELECT cii.transaction_error_id,
14988              cii.inv_material_transaction_id,
14989              cii.error_text,
14990              mmt.inventory_item_id,
14991              mmt.organization_id,
14992              mmt.transaction_quantity,
14993              mmt.trx_source_line_id,
14994              mmt.revision
14995       FROM   csi_txn_errors cii,
14996              mtl_material_transactions mmt
14997       WHERE  cii.processed_flag = 'E'
14998       AND    cii.inv_material_transaction_id is not null
14999       AND    mmt.transaction_id = cii.inv_material_transaction_id
15000       AND    mmt.transaction_type_id = 15; -- RMA
15001 
15002     --
15003     CURSOR tld_cur(p_rma_line_id in number) is
15004       SELECT ctld.sub_type_id, ctld.instance_id
15005       FROM   csi_t_transaction_lines ctl,
15006              csi_t_txn_line_details ctld
15007       WHERE  ctl.source_transaction_table = 'OE_ORDER_LINES_ALL'
15008       AND    ctl.source_transaction_id    = p_rma_line_id
15009       AND    ctld.transaction_line_id     = ctl.transaction_line_id
15010       AND    ctld.source_transaction_flag = 'Y';
15011 
15012     --
15013     CURSOR lot_cur(p_txn_id in number) IS
15014       SELECT lot_number,transaction_quantity
15015       FROM   mtl_transaction_lot_numbers
15016       WHERE  transaction_id = p_txn_id;
15017 
15018     --
15019     CURSOR csi_cur(
15020       p_item_id     in number,
15021       p_chg_owner   in varchar2,
15022       p_customer_id in number,
15023       p_revision    in varchar2,
15024       p_lot_number  in varchar2)
15025     IS
15026       SELECT instance_id,
15027 		   instance_number,
15028              last_vld_organization_id,
15029              quantity,
15030              lot_number,
15031              inventory_revision ,
15032              owner_party_account_id account_id,
15033              owner_party_id         party_id
15034       FROM   csi_item_instances,
15035              hz_parties
15036       WHERE  inventory_item_id      = p_item_id
15037       AND    owner_party_account_id = decode(p_chg_owner,'Y',owner_party_account_id,p_customer_id)
15038       AND    instance_usage_code    = 'OUT_OF_ENTERPRISE'
15039       AND    nvl(lot_number,'$#$')  = nvl(p_lot_number,'$#$')
15040       AND    nvl(inventory_revision,'$#$') = nvl(p_revision,'$#$')
15041       AND    party_id = owner_party_id
15042       ORDER BY party_name asc, quantity desc;
15043     --
15044     l_ib_flag             varchar2(1) := 'N';
15045     l_customer_id         number;
15046     l_message             varchar2(32767);
15047     l_srl_ctl             number;
15048     l_lot_ctl             number;
15049     l_rma_num             varchar2(50) ;
15050     l_rma_line_num        varchar2(50) ;
15051     l_loop_count          number := 0;
15052     l_inst_ref            number;
15053     l_org_code            varchar2(30);
15054 
15055     --
15056     l_item                varchar2(240);
15057     l_customer_name       varchar2(240);
15058     l_instances_found     boolean := FALSE;
15059     Process_next          exception;
15060 
15061     FUNCTION get_customer(
15062       p_account_id IN number,
15063       p_party_id   IN number)
15064     RETURN varchar2 IS
15065       l_customer_name varchar2(240);
15066     BEGIN
15067 
15068       IF p_account_id is not null THEN
15069         SELECT hp.party_name
15070         INTO   l_customer_name
15071         FROM   hz_cust_accounts hca,
15072                hz_parties       hp
15073         WHERE  hca.cust_account_id = p_account_id
15074         AND    hp.party_id         = hca.party_id;
15075       ELSE
15076         IF p_party_id is not null THEN
15077           SELECT party_name
15078           INTO   l_customer_name
15079           FROM   hz_parties
15080           WHERE  party_id = p_party_id;
15081         END IF;
15082       END IF;
15083 
15084       return l_customer_name;
15085 
15086     EXCEPTION
15087       WHEN others THEN
15088         return l_customer_name;
15089     END get_customer;
15090 
15091     FUNCTION fill(
15092       p_column in varchar2,
15093       p_width  in number,
15094       p_side   in varchar2 default 'R')
15095     RETURN varchar2 is
15096       l_column varchar2(2000);
15097       l_width  number;
15098     BEGIN
15099       l_width  := p_width - 1;
15100       l_column := nvl(p_column, ' ');
15101       IF p_side = 'L' THEN
15102         return(lpad(l_column, l_width, ' ')||',');
15103       ELSIF p_side = 'R' THEN
15104         return(rpad(l_column, l_width, ' ')||',');
15105       END IF;
15106     END fill;
15107 
15108   BEGIN
15109 
15110     out('  ');
15111     out('********************************************************************************');
15112     out('               Non serialized RMAs without installation details                 ');
15113     out('********************************************************************************');
15114 
15115     l_message := fill('MTLTxnID', 10)||
15116                  fill('RMA#', 10)||
15117                  fill('Line', 6)||
15118                  fill('Instance', 10)||
15119                  fill('Owner', 35)||
15120                  fill('Quantity', 9);
15121 
15122     out(l_message);
15123 
15124     --
15125     IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
15126        csi_gen_utility_pvt.populate_install_param_rec;
15127     END IF;
15128     --
15129     l_ownership_override := csi_datastructures_pub.g_install_param_rec.ownership_override_at_txn;
15130     --
15131     FOR csi_rec IN csi_error
15132     LOOP
15133 
15134       BEGIN
15135 
15136         SELECT concatenated_segments,
15137                serial_number_control_code,
15138                lot_control_code
15139         INTO   l_item, l_srl_ctl,l_lot_ctl
15140         FROM   mtl_system_items_kfv mtl
15141         WHERE  mtl.inventory_item_id = csi_rec.inventory_item_id
15142         AND    mtl.organization_id = csi_rec.organization_id;
15143 
15144         SELECT comms_nl_trackable_flag,
15145                organization_code
15146         INTO   l_ib_flag,
15147                l_org_code
15148         FROM   mtl_system_items_b msi ,
15149                mtl_parameters     mp
15150         WHERE  msi.inventory_item_id = csi_rec.inventory_item_id
15151         AND    msi.organization_id   = mp.master_organization_id
15152         AND    mp.organization_id    = csi_rec.organization_id;
15153         --
15154         IF nvl(l_ib_flag, 'N') <> 'Y' THEN
15155           RAISE Process_next;
15156         END IF;
15157         --
15158 
15159         SELECT nvl(line.sold_to_org_id, hdr.sold_to_org_id),
15160                hdr.order_number,
15161                line.line_number||'.'||line.shipment_number
15162         INTO   l_customer_id,
15163                l_rma_num,
15164                l_rma_line_num
15165         FROM   oe_order_lines_all   line,
15166                oe_order_headers_all hdr
15167         WHERE  line.line_id = csi_rec.trx_source_line_id
15168         AND    hdr.header_id = line.header_id;
15169 
15170         l_customer_name := get_customer(l_customer_id, null);
15171 
15172         --
15173         IF l_srl_ctl = 1 THEN
15174           l_loop_count := 0;
15175           l_inst_ref := 0;
15176           FOR tld_rec in tld_cur(csi_rec.trx_source_line_id)
15177           LOOP
15178             IF nvl(tld_rec.instance_id, fnd_api.g_miss_num) = fnd_api.g_miss_num THEN
15179               l_inst_ref := 1;
15180               --
15181             END IF;
15182             l_loop_count := l_loop_count + 1;
15183           END LOOP;
15184 
15185           IF ((l_loop_count = 0) OR (l_inst_ref = 1)) THEN
15186 
15187             l_customer_name := null;
15188 
15189             --
15190             IF l_lot_ctl = 1 THEN
15191 
15192               l_instances_found := FALSE;
15193 
15194               IF p_show_instances = 'Y' THEN
15195                 FOR ins in csi_cur (csi_rec.inventory_item_id,l_ownership_override,
15196                   l_customer_id,csi_rec.revision,null)
15197                 LOOP
15198 
15199                   l_instances_found := TRUE;
15200 
15201                   IF csi_cur%rowcount = 1 THEN
15202                     out('---------,---------,-----,---------,'||
15203                         '----------------------------------,--------,');
15204                   END IF;
15205 
15206                   l_customer_name := get_customer(ins.account_id, ins.party_id);
15207 
15208                   l_message := fill(csi_rec.inv_material_transaction_id, 10)||
15209                                fill(l_rma_num, 10)||
15210 					      fill(l_rma_line_num, 6)||
15211                                fill(ins.instance_number, 10)||
15212                                fill(l_customer_name, 35)||
15213                                fill(ins.quantity, 9);
15214                   out(l_message);
15215                 END LOOP;
15216               END IF;
15217 
15218               IF NOT(l_instances_found) THEN
15219                 out('---------,---------,-----,---------,'||
15220                     '----------------------------------,--------,');
15221                 l_message := fill(csi_rec.inv_material_transaction_id, 10)||
15222                              fill(l_rma_num, 10)||
15223                              fill(l_rma_line_num, 6);
15224                 out(l_message);
15225               END IF;
15226 
15227             ELSE
15228 
15229               l_instances_found := FALSE;
15230 
15231               IF p_show_instances = 'Y' THEN
15232 
15233                 FOR lot IN lot_cur(csi_rec.inv_material_transaction_id)
15234                 LOOP
15235 
15236                   FOR ins in CSI_CUR (csi_rec.inventory_item_id, l_ownership_override,l_customer_id
15237                                      ,csi_rec.revision,lot.lot_number)
15238                   LOOP
15239 
15240                     l_instances_found := TRUE;
15241 
15242                     IF csi_cur%rowcount = 1 THEN
15243                       out('---------,---------,-----,---------,'||
15244                           '----------------------------------,--------,');
15245                     END IF;
15246 
15247                     l_customer_name := get_customer(ins.account_id, ins.party_id);
15248 
15249                     l_message := fill(csi_rec.inv_material_transaction_id, 10)||
15250                                  fill(l_rma_num, 10)||
15251                                  fill(l_rma_line_num, 6)||
15252                                  fill(ins.instance_number, 10)||
15253                                  fill(l_customer_name, 35)||
15254                                  fill(ins.quantity, 9);
15255                     out(l_message);
15256                   END LOOP;
15257                 END LOOP;
15258               END IF;
15259 
15260               IF NOT(l_instances_found) THEN
15261                 out('---------,---------,-----,---------,'||
15262                     '----------------------------------,--------,');
15263                 l_message := fill(csi_rec.inv_material_transaction_id, 10)||
15264                              fill(l_rma_num, 10)||
15265                              fill(l_rma_line_num, 6);
15266                 out(l_message);
15267               END IF;
15268 
15269             END IF;
15270           END IF;
15271         END IF;
15272       EXCEPTION
15273         WHEN Process_next then
15274           null;
15275       END;
15276 
15277     END LOOP;
15278     commit;
15279   EXCEPTION
15280     WHEN others THEN
15281      out('  Error in report:'||sqlerrm);
15282   END get_non_srl_rma_report;
15283 
15284   PROCEDURE check_org_uniqueness IS
15285     CURSOR uniq_cur IS
15286       SELECT a.instance_id,
15287              a.serial_number,
15288              a.inventory_item_id inst_item_id,
15289              d.inventory_item_id serial_item_id,
15290              d.current_status,
15291              d.current_organization_id,
15292              a.manually_created_flag,
15293              a.instance_usage_code,
15294              a.location_type_code,
15295              a.active_end_date
15296       FROM   csi_item_instances a,
15297              mtl_serial_numbers d,
15298              mtl_parameters    e
15299       WHERE  a.serial_number is not null
15300       AND    d.serial_number      = a.serial_number
15301       AND    d.inventory_item_id  <> a.inventory_item_id
15302       AND    e.organization_id    = nvl(a.last_vld_organization_id, a.inv_master_organization_id)
15303       AND    e.serial_number_type = 3;
15304   BEGIN
15305    csi_t_gen_utility_pvt.build_file_name(
15306        p_file_segment1 => 'csisrlun',
15307        p_file_segment2 => to_char(sysdate, 'hh24miss'));
15308    --
15309     FOR uniq_rec IN uniq_cur
15310     LOOP
15311 
15312       l_global_sync_flag := 'Y';
15313 
15314       IF uniq_cur%rowcount = 1 THEN
15315         out('Serial Uniqueness Report - Across Organization');
15316         out('------------------------------------------------------------------');
15317       END IF;
15318 
15319       out(to_char(uniq_rec.instance_id)||
15320                        '  '||uniq_rec.serial_number||
15321                        '  '||to_char(uniq_rec.inst_item_id)||
15322                        '  '||to_char(uniq_rec.serial_item_id)||
15323                        '  '||to_char(uniq_rec.current_status)||
15324                        '  '||to_char(uniq_rec.current_organization_id)||
15325                        '  '||uniq_rec.manually_created_flag||
15326                        '  '||uniq_rec.instance_usage_code||
15327                        '  '||uniq_rec.location_type_code);
15328 
15329     END LOOP;
15330   END check_org_uniqueness;
15331 
15332 
15333 PROCEDURE CREATE_NSRL_RMA_TLD IS
15334 
15335   l_inventory_item_id    number;
15336   l_organization_id      number;
15337   l_serial_code          number;
15338   l_owner_pty_id         number;
15339   l_owner_acct_id        number;
15340   l_order_qty            number;
15341   l_order_uom            varchar2(9);
15342   l_txn_line_detail_id   number;
15343   l_transaction_line_id  number;
15344   l_instance_count       number;
15345   l_tld_count            number;
15346   l_sub_type_id          number;
15347   l_change_owner         varchar2(1);
15348   l_internal_party_id    number;
15349 
15350   l_instance_id          number;
15351   l_instance_qty         number;
15352   l_rma_line_id          number;
15353   l_primary_qty          number;
15354   l_processed_flag       varchar2(1) := 'N';
15355 
15356   l_line_rec       csi_t_datastructures_grp.txn_line_rec;
15357   l_line_dtl_tbl   csi_t_datastructures_grp.txn_line_detail_tbl;
15358   l_pty_dtl_tbl    csi_t_datastructures_grp.txn_party_detail_tbl;
15359   l_pty_acct_tbl   csi_t_datastructures_grp.txn_pty_acct_detail_tbl;
15360   l_ii_rltns_tbl   csi_t_datastructures_grp.txn_ii_rltns_tbl;
15361   l_oa_tbl         csi_t_datastructures_grp.txn_org_assgn_tbl;
15362   l_ea_tbl         csi_t_datastructures_grp.txn_ext_attrib_vals_tbl;
15363   l_sys_tbl        csi_t_datastructures_grp.txn_systems_tbl;
15364 
15365   l_return_status  varchar2(1);
15366   l_msg_count      number;
15367   l_msg_data       varchar2(2000);
15368 
15369   l_mtl_txn_date   date;
15370   l_mtl_cre_date   date;
15371 
15372   skip_error       exception;
15373   i                number;
15374   j                number;
15375 
15376    --added for bug 5248037--
15377   MAX_BUFFER_SIZE          number := 1000;
15378   l_rec_change             BOOLEAN;
15379   l_rma_txn_tbl           csi_diagnostics_pkg.rma_txn_tbl;
15380   l_instance_tbl          csi_diagnostics_pkg.instance_tbl;
15381 
15382   --Modified rma_cur and inst_cur for 5248037--
15383   CURSOR rma_cur
15384   IS
15385   SELECT cte.transaction_error_id transaction_error_id,
15386          cte.inv_material_transaction_id inv_material_transaction_id,
15387          mmt.inventory_item_id item_id,
15388          mmt.organization_id organization_id,
15389          mmt.trx_source_line_id mtl_src_line_id,
15390          mmt.creation_date mtl_creation_date,
15391          abs(mmt.primary_quantity) mtl_txn_qty,
15392          msi.serial_number_control_code serial_code,
15393          oel.sold_to_org_id owner_acct,
15394          oel.ordered_quantity ordered_qty,
15395          oel.order_quantity_uom ordered_uom,
15396          hca.party_id party_id
15397   FROM   csi_txn_errors cte,mtl_material_transactions mmt,mtl_system_items msi,oe_order_lines_all oel,hz_cust_accounts hca
15398   WHERE  cte.processed_flag in ('E', 'R')
15399   AND    cte.transaction_type_id = 53
15400   AND    cte.inv_material_transaction_id = mmt.transaction_id
15401   AND    msi.inventory_item_id = mmt.inventory_item_id
15402   AND    msi.organization_id = mmt.organization_id
15403   AND    msi.serial_number_control_code = 1
15404   AND    oel.line_id = mmt.trx_source_line_id
15405   AND    hca.cust_account_id = oel.sold_to_org_id
15406   ORDER BY item_id, party_id, owner_acct,mtl_txn_qty;
15407 
15408   CURSOR inst_cur (p_item_id NUMBER,
15409                    p_owner_party_id NUMBER,
15410                    p_owner_acct_id NUMBER)
15411   IS
15412   SELECT cii.instance_id instance_id,
15413          cii.quantity quantity,
15414          cii.active_start_date active_start_date
15415   FROM   csi_item_instances cii
15416   WHERE  cii.inventory_item_id      = p_item_id
15417   AND    cii.accounting_class_code  = 'CUST_PROD'
15418   AND    cii.instance_usage_code    = 'OUT_OF_ENTERPRISE'
15419   AND    cii.owner_party_id         = p_owner_party_id
15420   AND    cii.owner_party_account_id = p_owner_acct_id
15421   AND    sysdate between nvl(cii.active_start_date, sysdate-1) and nvl(cii.active_end_date, sysdate+1)
15422   ORDER BY quantity;
15423 
15424   CURSOR tld_cur(p_transaction_line_id IN number)
15425   IS
15426   SELECT txn_line_detail_id ,
15427          instance_id,
15428          quantity
15429   FROM   csi_t_txn_line_details
15430   WHERE  transaction_line_id = p_transaction_line_id
15431   AND    source_transaction_flag = 'Y';
15432 
15433 
15434   PROCEDURE debug(p_message IN varchar2)
15435   IS
15436   BEGIN
15437     csi_t_gen_utility_pvt.add(p_message);
15438   END debug;
15439 
15440   BEGIN
15441 
15442     SELECT sub_type_id ,
15443            src_change_owner
15444     INTO   l_sub_type_id,
15445            l_change_owner
15446     FROM   csi_txn_sub_types
15447     WHERE  transaction_type_id = 53
15448     AND    default_flag        = 'Y';
15449 
15450     IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
15451        csi_gen_utility_pvt.populate_install_param_rec;
15452     END IF;
15453     l_internal_party_id := csi_datastructures_pub.g_install_param_rec.internal_party_id;
15454 
15455     --Changed to use the BULK COLLECT for cursor for bug5248037--
15456     OPEN rma_cur;
15457     LOOP
15458       FETCH rma_cur BULK COLLECT
15459          INTO l_rma_txn_tbl
15460             LIMIT MAX_BUFFER_SIZE;
15461 
15462       l_rec_change := TRUE;
15463 
15464       FOR i IN  1..l_rma_txn_tbl.COUNT
15465       LOOP
15466 	l_processed_flag := 'N';
15467 
15468         IF l_rec_change THEN
15469           OPEN inst_cur(l_rma_txn_tbl(i).item_id,
15470                         l_rma_txn_tbl(i).party_id,
15471                         l_rma_txn_tbl(i).owner_acct);
15472           LOOP
15473             l_instance_tbl.delete;
15474             FETCH inst_cur BULK COLLECT
15475             INTO l_instance_tbl
15476             LIMIT MAX_BUFFER_SIZE;
15477             EXIT WHEN inst_cur%NOTFOUND;
15478          END LOOP;
15479 
15480 	 IF inst_cur%ISOPEN THEN
15481             CLOSE inst_cur;
15482          END IF;
15483         END IF;
15484 
15485     IF l_instance_tbl.count > 0 THEN
15486      FOR j IN l_instance_tbl.FIRST..l_instance_tbl.LAST
15487      LOOP
15488       IF l_instance_tbl.EXISTS(j) THEN
15489        IF l_instance_tbl(j).quantity >= l_rma_txn_tbl(i).mtl_txn_qty
15490         AND l_instance_tbl(j).active_start_date <= l_rma_txn_tbl(i).mtl_creation_date THEN
15491 
15492           -- check for existance of txn details
15493           l_transaction_line_id := null;
15494           BEGIN
15495               SELECT transaction_line_id
15496               INTO   l_transaction_line_id
15497               FROM   csi_t_transaction_lines
15498               WHERE  source_transaction_table = 'OE_ORDER_LINES_ALL'
15499               AND    source_transaction_id    = l_rma_txn_tbl(i).mtl_src_line_id
15500               AND    source_transaction_type_id = 53;
15501           EXCEPTION
15502               WHEN no_data_found THEN
15503                 l_transaction_line_id := null;
15504           END;
15505 
15506           -- if not found then
15507 
15508           IF l_transaction_line_id is null THEN
15509              -- create transaction details
15510 	      l_line_rec.transaction_line_id            := fnd_api.g_miss_num;
15511               l_line_rec.source_transaction_type_id     := 53;
15512               l_line_rec.source_transaction_id          := l_rma_txn_tbl(i).mtl_src_line_id;
15513               l_line_rec.source_transaction_table       := 'OE_ORDER_LINES_ALL';
15514               l_line_rec.inv_material_txn_flag          := 'Y';
15515               l_line_rec.object_version_number          := 1.0;
15516 
15517               -- transaction line details table
15518               l_line_dtl_tbl(1).transaction_line_id     := fnd_api.g_miss_num;
15519               l_line_dtl_tbl(1).txn_line_detail_id      := fnd_api.g_miss_num;
15520               l_line_dtl_tbl(1).sub_type_id             := l_sub_type_id;
15521               l_line_dtl_tbl(1).instance_exists_flag    := 'Y';
15522               l_line_dtl_tbl(1).instance_id             := l_instance_tbl(j).instance_id;
15523               l_line_dtl_tbl(1).source_transaction_flag := 'Y';
15524               l_line_dtl_tbl(1).quantity                := l_rma_txn_tbl(i).ordered_qty;
15525               l_line_dtl_tbl(1).inventory_item_id       := l_rma_txn_tbl(i).item_id;
15526               l_line_dtl_tbl(1).inv_organization_id     := l_rma_txn_tbl(i).organization_id;
15527               l_line_dtl_tbl(1).unit_of_measure         := l_rma_txn_tbl(i).ordered_uom;
15528               l_line_dtl_tbl(1).mfg_serial_number_flag  := 'N';
15529               l_line_dtl_tbl(1).active_start_date       := sysdate;
15530               l_line_dtl_tbl(1).preserve_detail_flag    := 'Y';
15531               l_line_dtl_tbl(1).object_version_number   := 1.0;
15532 
15533                IF l_change_owner = 'Y' THEN
15534                   l_pty_dtl_tbl(1).txn_party_detail_id    := fnd_api.g_miss_num;
15535                   l_pty_dtl_tbl(1).txn_line_detail_id     := fnd_api.g_miss_num;
15536                   l_pty_dtl_tbl(1).party_source_table     := 'HZ_PARTIES';
15537                   l_pty_dtl_tbl(1).party_source_id        := l_internal_party_id;
15538                   l_pty_dtl_tbl(1).relationship_type_code := 'OWNER';
15539                   l_pty_dtl_tbl(1).contact_flag           := 'N';
15540                   l_pty_dtl_tbl(1).active_start_date      := sysdate;
15541                   l_pty_dtl_tbl(1).preserve_detail_flag   := 'Y';
15542                   l_pty_dtl_tbl(1).txn_line_details_index := 1;
15543 
15544 		  BEGIN
15545                     SELECT instance_party_id
15546                     INTO   l_pty_dtl_tbl(1).instance_party_id
15547                     FROM   csi_i_parties
15548                     WHERE  instance_id            = l_instance_tbl(j).instance_id
15549                     AND    relationship_type_code = 'OWNER';
15550                   EXCEPTION
15551                     WHEN no_data_found THEN
15552                        l_pty_dtl_tbl(1).instance_party_id := fnd_api.g_miss_num;
15553                   END;
15554                END IF;
15555 
15556               -- api call
15557               csi_t_txn_details_grp.create_transaction_dtls(
15558                                p_api_version              => 1.0,
15559                                p_commit                   => fnd_api.g_false,
15560                                p_init_msg_list            => fnd_api.g_true,
15561                                p_validation_level         => fnd_api.g_valid_level_full,
15562                                px_txn_line_rec            => l_line_rec,
15563                                px_txn_line_detail_tbl     => l_line_dtl_tbl,
15564                                px_txn_party_detail_tbl    => l_pty_dtl_tbl,
15565                                px_txn_pty_acct_detail_tbl => l_pty_acct_tbl,
15566                                px_txn_ii_rltns_tbl        => l_ii_rltns_tbl,
15567                                px_txn_org_assgn_tbl       => l_oa_tbl,
15568                                px_txn_ext_attrib_vals_tbl => l_ea_tbl,
15569                                px_txn_systems_tbl         => l_sys_tbl,
15570                                x_return_status            => l_return_status,
15571                                x_msg_count                => l_msg_count,
15572                                x_msg_data                 => l_msg_data);
15573 
15574              IF l_return_status <> fnd_api.g_ret_sts_success THEN
15575                RAISE fnd_api.g_exc_error;
15576              END IF;
15577 
15578              l_processed_flag := 'Y';
15579              -- else check if count of source line detail
15580 
15581         ELSE
15582            FOR tld_rec IN tld_cur(l_transaction_line_id)
15583            LOOP
15584              l_tld_count := tld_cur%rowcount;
15585              l_txn_line_detail_id := tld_rec.txn_line_detail_id;
15586              IF l_tld_count > 1 THEN
15587                  exit;
15588              END IF;
15589            END LOOP;
15590 
15591            -- if only one then
15592            IF l_tld_count = 1 THEN
15593                 UPDATE csi_t_txn_line_details
15594                 SET    instance_id = l_instance_tbl(j).instance_id,
15595                        instance_exists_flag = 'Y'
15596                 WHERE  txn_line_detail_id = l_txn_line_detail_id;
15597                        l_processed_flag := 'Y';
15598            END IF;
15599 
15600         END IF;
15601 
15602         IF l_processed_flag = 'Y' THEN
15603            UPDATE csi_txn_errors
15604            SET    processed_flag       = 'R'
15605            WHERE  transaction_error_id = l_rma_txn_tbl(i).Txn_error_id;
15606 
15607           l_instance_tbl(j).quantity:=l_instance_tbl(j).quantity-l_rma_txn_tbl(i).ordered_qty;
15608 
15609           --If an instance is matched for the error'd record then that instance is removed from
15610 	  --table
15611           IF l_instance_tbl(j).quantity = 0 THEN
15612             l_instance_tbl.DELETE(j);
15613           END IF;
15614           EXIT;
15615         END IF;
15616       END IF;
15617      END IF; --for record existing check
15618    END LOOP; -- instance for loop
15619   END IF; --instance tbl cnt greater than zero
15620 
15621    --This IF loop avoids the re-query if the next error record is for the same item,party and owner--
15622    IF i < l_rma_txn_tbl.count THEN
15623      IF (l_rma_Txn_tbl(i).item_id <> l_rma_txn_tbl(i+1).item_id)
15624      OR (l_rma_Txn_tbl(i).party_id <> l_rma_txn_tbl(i+1).party_id)
15625      OR (l_rma_Txn_tbl(i).owner_acct <> l_rma_txn_tbl(i+1).owner_acct) THEN
15626         l_rec_change := TRUE;
15627      ELSE
15628       l_rec_change := FALSE;
15629      END IF;
15630    END IF;
15631 
15632    END LOOP;
15633 
15634    COMMIT;
15635 
15636    EXIT WHEN rma_cur%NOTFOUND;
15637 
15638   END LOOP; --rma tbl
15639 
15640   IF rma_cur%ISOPEN THEN
15641    CLOSE rma_cur;
15642   END IF;
15643 
15644   EXCEPTION
15645     WHEN fnd_api.g_exc_error THEN
15646       null;
15647     WHEN others THEN
15648       null;
15649       log('  Other Error:'||substr(sqlerrm, 1,250));
15650   END create_nsrl_rma_tld;
15651 
15652   PROCEDURE fix_txn_error_rec
15653   IS
15654 
15655     CURSOR err_cur IS
15656       SELECT transaction_error_id,
15657              inv_material_transaction_id
15658       FROM   csi_txn_errors
15659       WHERE  processed_flag in ('E', 'R')
15660       AND    inv_material_transaction_id is not null
15661       AND    (source_type is null OR transaction_type_id is null);
15662 
15663     l_release             varchar2(30);
15664     l_mtl_txn_id          number;
15665     l_mtl_type_id         number;
15666     l_mtl_action_id       number;
15667     l_mtl_source_type_id  number;
15668     l_mtl_type_class      number;
15669     l_mtl_txn_qty         number;
15670 
15671     l_source_type         varchar2(30);
15672     l_csi_txn_type_id     number;
15673     l_txn_processed       varchar2(1) := 'N';
15674 
15675 
15676   BEGIN
15677 
15678     SELECT fnd_Profile.value('csi_upgrading_from_release')
15679     INTO   l_release
15680     FROM   sys.dual;
15681 
15682     FOR err_rec IN err_cur
15683     LOOP
15684 
15685       BEGIN
15686         SELECT 'Y' INTO l_txn_processed
15687         FROM   sys.dual
15688         WHERE  exists (
15689           SELECT 'X' FROM csi_transactions
15690           WHERE  inv_material_transaction_id = err_rec.inv_material_transaction_id);
15691 
15692         UPDATE csi_txn_errors
15693         SET    processed_flag = 'D'
15694         WHERE  transaction_error_id = err_rec.transaction_error_id;
15695 
15696       EXCEPTION
15697         WHEN no_data_found THEN
15698 
15699           BEGIN
15700             SELECT mmt.transaction_id,
15701                    mmt.transaction_type_id,
15702                    mmt.transaction_action_id,
15703                    mmt.transaction_source_type_id,
15704                    mtt.type_class,
15705                    mmt.transaction_quantity
15706             INTO   l_mtl_txn_id,
15707                    l_mtl_type_id,
15708                    l_mtl_action_id,
15709                    l_mtl_source_type_id,
15710                    l_mtl_type_class,
15711                    l_mtl_txn_qty
15712             FROM   mtl_material_transactions mmt,
15713                    mtl_transaction_types     mtt
15714             WHERE  mmt.transaction_id      = err_rec.inv_material_transaction_id
15715             AND    mtt.transaction_type_id = mmt.transaction_type_id;
15716 
15717             get_source_type(
15718               p_mtl_txn_id         => l_mtl_txn_id,
15719               p_mtl_type_id        => l_mtl_type_id,
15720               p_mtl_action_id      => l_mtl_action_id,
15721               p_mtl_source_type_id => l_mtl_source_type_id,
15722               p_mtl_type_class     => l_mtl_type_class,
15723               p_mtl_txn_qty        => l_mtl_txn_qty,
15724               p_release            => l_release,
15725               x_source_type        => l_source_type,
15726               x_csi_txn_type_id    => l_csi_txn_type_id);
15727 
15728             UPDATE csi_txn_errors
15729             SET    source_type          = l_source_type,
15730                    transaction_type_id  = l_csi_txn_type_id
15731             WHERE  transaction_error_id = err_rec.transaction_error_id;
15732 
15733           EXCEPTION
15734             WHEN no_data_found THEN
15735               null;
15736           END;
15737       END ;
15738     END LOOP;
15739     commit;
15740   END fix_txn_error_rec;
15741 
15742   PROCEDURE fix_wip_usage IS
15743 
15744     TYPE NumTabType is       varray(10000) of number;
15745     l_instance_id_tab        NumTabType;
15746     MAX_BUFFER_SIZE          number := 1000;
15747 
15748     CURSOR wip_cur IS
15749       SELECT cii.instance_id
15750       FROM   csi_item_instances cii
15751       WHERE  cii.location_type_code = 'WIP'
15752       AND    cii.instance_usage_code <> 'IN_RELATIONSHIP'
15753       AND    exists (
15754         SELECT 'X' FROM mtl_system_items msi
15755         WHERE  msi.inventory_item_id = cii.inventory_item_id
15756         AND    msi.organization_id   = cii.last_vld_organization_id
15757         AND    msi.serial_number_control_code in (1, 6));
15758 
15759   BEGIN
15760 
15761     OPEN wip_cur;
15762     LOOP
15763 
15764       FETCH wip_cur BULK COLLECT
15765       INTO  l_instance_id_tab
15766       LIMIT MAX_BUFFER_SIZE;
15767 
15768       FOR ind IN 1 .. l_instance_id_tab.COUNT
15769       LOOP
15770         UPDATE csi_item_instances
15771         SET    instance_usage_code = 'IN_WIP'
15772         WHERE  instance_id         = l_instance_id_tab(ind);
15773       END LOOP;
15774       commit;
15775 
15776       EXIT when wip_cur%NOTFOUND;
15777 
15778     END LOOP;
15779 
15780     IF wip_cur%ISOPEN THEN
15781       CLOSE wip_cur;
15782     END IF;
15783 
15784   END fix_wip_usage;
15785 
15786   PROCEDURE delete_dup_nsrl_wip_instances IS
15787 
15788     l_keep_instance_id       number;
15789     TYPE NumTabType is       varray(10000) of number;
15790     TYPE VarTabType is       varray(10000) of varchar2(100);
15791 
15792     l_item_id_tab            NumTabType;
15793     l_revision_tab           VarTabType;
15794     l_lot_number_tab         VarTabType;
15795     l_wip_job_id_tab         NumTabType;
15796 
15797     MAX_BUFFER_SIZE          number := 1000;
15798 
15799     CURSOR dup_wip_cur IS
15800       SELECT cii.inventory_item_id,
15801              cii.inventory_revision,
15802              cii.lot_number,
15803              cii.wip_job_id
15804       FROM   csi_item_instances cii,
15805              mtl_system_items   msi
15806       WHERE  cii.location_type_code  = 'WIP'
15807       AND    cii.instance_usage_code = 'IN_WIP'
15808       AND    msi.inventory_item_id = cii.inventory_item_id
15809       AND    msi.organization_id   = cii.last_vld_organization_id
15810       AND    msi.serial_number_control_code in (1, 6)
15811       GROUP BY cii.inventory_item_id,
15812                cii.inventory_revision,
15813                cii.lot_number,
15814                cii.wip_job_id
15815       HAVING   count(*) > 1;
15816 
15817     CURSOR dup_inst_cur(
15818       p_item_id    IN number,
15819       p_revision   IN varchar,
15820       p_lot_number IN varchar2,
15821       p_wip_job_id IN number)
15822     IS
15823       SELECT cii.instance_id,
15824              cii.quantity
15825       FROM   csi_item_instances cii
15826       WHERE  cii.location_type_code  = 'WIP'
15827       AND    cii.instance_usage_code = 'IN_WIP'
15828       AND    cii.inventory_item_id                = p_item_id
15829       AND    nvl(cii.inventory_revision, '#*#*#') = nvl(p_revision, '#*#*#')
15830       AND    nvl(cii.lot_number,'#*#*#')          = nvl(p_lot_number, '#*#*#')
15831       AND    cii.wip_job_id                       = p_wip_job_id;
15832 
15833     CURSOR ip_cur(p_instance_id IN number) IS
15834       SELECT instance_party_id
15835       FROM   csi_i_parties
15836       WHERE  instance_id = p_instance_id;
15837 
15838   BEGIN
15839 
15840     OPEN dup_wip_cur;
15841     LOOP
15842 
15843       FETCH dup_wip_cur BULK COLLECT
15844       INTO  l_item_id_tab,
15845             l_revision_tab,
15846             l_lot_number_tab,
15847             l_wip_job_id_tab
15848       LIMIT MAX_BUFFER_SIZE;
15849 
15850       FOR ind IN 1 .. l_item_id_tab.COUNT
15851       LOOP
15852 
15853         l_keep_instance_id := null;
15854 
15855         FOR dup_inst_rec IN dup_inst_cur(
15856           p_item_id    => l_item_id_tab(ind),
15857           p_revision   => l_revision_tab(ind),
15858           p_lot_number => l_lot_number_tab(ind),
15859           p_wip_job_id => l_wip_job_id_tab(ind))
15860         LOOP
15861 
15862           IF dup_inst_cur%rowcount = 1 THEN
15863             l_keep_instance_id := dup_inst_rec.instance_id;
15864           ELSE
15865 
15866             -- preserve one instance for the wip job component and cumulate quantity
15867             UPDATE csi_item_instances
15868             SET    quantity    = quantity + dup_inst_rec.quantity
15869             WHERE  instance_id = l_keep_instance_id;
15870 
15871             -- delete the rest of the instances (party and accounts)
15872             FOR ip_rec IN ip_cur(dup_inst_rec.instance_id)
15873             LOOP
15874 
15875               -- there may not be an account, but just in case
15876               DELETE FROM csi_ip_accounts
15877               WHERE  instance_party_id = ip_rec.instance_party_id;
15878 
15879               DELETE FROM csi_i_parties_h
15880               WHERE  instance_party_id = ip_rec.instance_party_id;
15881 
15882             END LOOP;
15883 
15884             DELETE FROM csi_i_parties
15885             WHERE  instance_id = dup_inst_rec.instance_id;
15886 
15887             DELETE FROM csi_item_instances
15888             WHERE  instance_id = dup_inst_rec.instance_id;
15889 
15890             DELETE FROM csi_item_instances_h
15891             WHERE  instance_id = dup_inst_rec.instance_id;
15892 
15893           END IF;
15894         END LOOP;
15895 
15896       END LOOP;
15897 
15898       commit;
15899 
15900       EXIT when dup_wip_cur%NOTFOUND;
15901 
15902     END LOOP;
15903 
15904     IF dup_wip_cur%ISOPEN THEN
15905       CLOSE dup_wip_cur;
15906     END IF;
15907 
15908   END delete_dup_nsrl_wip_instances;
15909   --
15910   PROCEDURE Delete_Dup_Org_Assignments IS
15911      cursor csi_dup_cur is
15912      select instance_id,relationship_type_code
15913      from csi_i_org_assignments
15914      group by instance_id,relationship_type_code
15915      having count(*) > 1;
15916      --
15917      cursor csi_org_cur(p_instance_id in number,p_rel_type_code in varchar2) is
15918      select *
15919      from csi_i_org_assignments
15920      where instance_id = p_instance_id
15921      and   relationship_type_code = p_rel_type_code
15922      order by creation_date asc;
15923      --
15924      cursor csi_org_hist_cur(p_instance_id in number,p_rel_type_code in varchar2) is
15925      select coah.* from csi_i_org_assignments_h coah,
15926 	    csi_i_org_assignments coa
15927      where coa.instance_id = p_instance_id
15928      and   coa.relationship_type_code = p_rel_type_code
15929      and   coah.instance_ou_id = coa.instance_ou_id
15930      order by coah.transaction_id,coah.last_update_date asc;
15931      --
15932      l_hist_rec             csi_org_hist_cur%ROWTYPE;
15933      v_min_ou_id            NUMBER;
15934      v_min_org_id           NUMBER;
15935      v_max_ou_id            NUMBER;
15936      v_max_org_id           NUMBER;
15937      l_row_count            NUMBER;
15938      l_del_count            NUMBER := 0;
15939      TYPE NumList IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
15940      l_del_tbl              NumList;
15941      TYPE ORG_HIST_TBL IS TABLE OF CSI_I_ORG_ASSIGNMENTS_H%ROWTYPE INDEX BY BINARY_INTEGER;
15942      l_org_hist_tbl         ORG_HIST_TBL;
15943      l_hist_count           NUMBER;
15944      v_ret_ou_id            NUMBER;
15945      l_prev_rec             csi_org_hist_cur%ROWTYPE;
15946      l_ou_hist_count        NUMBER;
15947      l_del_ou_hist_tbl      NumList;
15948      l_flag                 VARCHAR2(1);
15949      --
15950      Process_next           EXCEPTION;
15951   BEGIN
15952      FOR dup_rec in csi_dup_cur LOOP
15953 	Begin
15954 	   -- Get the latest org assignment. This org_id will be retained
15955 	   v_max_ou_id := -9999;
15956 	   Begin
15957 	      select instance_ou_id
15958 	      into v_max_ou_id
15959 	      from csi_i_org_assignments
15960 	      where instance_id = dup_rec.instance_id
15961 	      and   relationship_type_code = dup_rec.relationship_type_code
15962 	      and   creation_date = ( select max(creation_date)
15963 				      from csi_i_org_assignments
15964 				      where instance_id = dup_rec.instance_id
15965 				      and   relationship_type_code = dup_rec.relationship_type_code
15966 				      and   nvl(active_end_date,(sysdate+1)) > sysdate)
15967 	      and   nvl(active_end_date,(sysdate+1)) > sysdate
15968 	      and   rownum < 2;
15969 	   Exception
15970 	      when no_data_found then
15971 		 select max(instance_ou_id)
15972 		 into  v_max_ou_id
15973 		 from csi_i_org_assignments
15974 		 where instance_id = dup_rec.instance_id
15975 		 and   relationship_type_code = dup_rec.relationship_type_code;
15976 	   End;
15977 	   --
15978 	   l_row_count := 0;
15979 	   l_del_count := 0;
15980 	   l_del_tbl.DELETE;
15981 	   l_hist_count := 0;
15982 	   l_org_hist_tbl.DELETE;
15983 	   v_ret_ou_id := -99999;
15984 	   --
15985 	   FOR org_rec in csi_org_cur(dup_rec.instance_id,dup_rec.relationship_type_code) LOOP
15986 	      l_row_count := l_row_count + 1;
15987 	      IF l_row_count = 1 THEN
15988 		 v_ret_ou_id := org_rec.instance_ou_id;
15989 		 UPDATE CSI_I_ORG_ASSIGNMENTS
15990 		 set (operating_unit_id,active_end_date,context,attribute1,attribute2,attribute3,
15991 		      attribute4,attribute5,attribute6,attribute7,attribute8,attribute9,attribute10,
15992 		      attribute11,attribute12,attribute13,attribute14,attribute15,last_update_date) =
15993 		    (select operating_unit_id,active_end_date,context,attribute1,attribute2,attribute3,
15994 		     attribute4,attribute5,attribute6,attribute7,attribute8,attribute9,attribute10,
15995 		     attribute11,attribute12,attribute13,attribute14,attribute15,sysdate
15996 		     from csi_i_org_assignments
15997 		     where instance_ou_id = v_max_ou_id)
15998 		  where instance_ou_id = org_rec.instance_ou_id;
15999 	      ELSE
16000 		 l_del_count := l_del_count + 1;
16001 		 l_del_tbl(l_del_count) := org_rec.instance_ou_id;
16002 	      END IF;
16003 	   END LOOP; -- end of instance_id, relationship type combination
16004 	   --
16005 	   -- For this Instance - Relationship type, get all the history records ordered by txn_id
16006 	   open csi_org_hist_cur(dup_rec.instance_id,dup_rec.relationship_type_code);
16007 	   LOOP
16008 	      fetch csi_org_hist_cur into l_hist_rec;
16009 	      IF csi_org_hist_cur%FOUND THEN
16010 		 l_hist_count := l_hist_count + 1;
16011 		 l_org_hist_tbl(l_hist_count) := l_hist_rec;
16012 	      END IF;
16013 	      EXIT WHEN csi_org_hist_cur%NOTFOUND;
16014 	   END LOOP;
16015 	   close csi_org_hist_cur;
16016 	   --
16017 	   -- Merge the history belonging to the same transaction
16018 	   l_flag := 'N';
16019 	   l_del_ou_hist_tbl.DELETE;
16020 	   --
16021 	   IF l_org_hist_tbl.count > 0 THEN
16022 	      FOR j IN l_org_hist_tbl.FIRST .. l_org_hist_tbl.LAST LOOP
16023 		 IF j = 1 THEN
16024 		    l_prev_rec := l_org_hist_tbl(j);
16025 		 ELSE
16026 		    IF l_org_hist_tbl(j).transaction_id = l_prev_rec.transaction_id THEN
16027 		       l_flag := 'Y';
16028 		       l_ou_hist_count := l_del_ou_hist_tbl.count + 1;
16029 		       l_del_ou_hist_tbl(l_ou_hist_count) := l_org_hist_tbl(j).instance_ou_history_id;
16030 		       IF nvl(l_org_hist_tbl(j).old_operating_unit_id,-999) <>
16031 			  nvl(l_org_hist_tbl(j).new_operating_unit_id,-999) THEN
16032 			  l_prev_rec.old_operating_unit_id := l_org_hist_tbl(j).old_operating_unit_id;
16033 			  l_prev_rec.new_operating_unit_id := l_org_hist_tbl(j).new_operating_unit_id;
16034 		       END IF;
16035 		       --
16036 		       IF nvl(l_org_hist_tbl(j).old_relationship_type_code,'$#$') <>
16037 			  nvl(l_org_hist_tbl(j).new_relationship_type_code,'$#$') THEN
16038 			  l_prev_rec.old_relationship_type_code := l_org_hist_tbl(j).old_relationship_type_code;
16039 			  l_prev_rec.new_relationship_type_code := l_org_hist_tbl(j).new_relationship_type_code;
16040 		       END IF;
16041 		       --
16042 		       IF nvl(l_org_hist_tbl(j).old_active_start_date,fnd_api.g_miss_date) <>
16043 			  nvl(l_org_hist_tbl(j).new_active_start_date,fnd_api.g_miss_date) THEN
16044 			  l_prev_rec.old_active_start_date := l_org_hist_tbl(j).old_active_start_date;
16045 			  l_prev_rec.new_active_start_date := l_org_hist_tbl(j).new_active_start_date;
16046 		       END IF;
16047 		       --
16048 		       IF nvl(l_org_hist_tbl(j).old_active_end_date,fnd_api.g_miss_date) <>
16049 			  nvl(l_org_hist_tbl(j).new_active_end_date,fnd_api.g_miss_date) THEN
16050 			  l_prev_rec.old_active_end_date := l_org_hist_tbl(j).old_active_end_date;
16051 			  l_prev_rec.new_active_end_date := l_org_hist_tbl(j).new_active_end_date;
16052 		       END IF;
16053 		       --
16054 		       IF nvl(l_org_hist_tbl(j).old_context,'$#$') <>
16055 			  nvl(l_org_hist_tbl(j).new_context,'$#$') THEN
16056 			  l_prev_rec.old_context := l_org_hist_tbl(j).old_context;
16057 			  l_prev_rec.new_context := l_org_hist_tbl(j).new_context;
16058 		       END IF;
16059 		       --
16060 		       IF nvl(l_org_hist_tbl(j).old_attribute1,'$#$') <>
16061 			  nvl(l_org_hist_tbl(j).new_attribute1,'$#$') THEN
16062 			  l_prev_rec.old_attribute1 := l_org_hist_tbl(j).old_attribute1;
16063 			  l_prev_rec.new_attribute1 := l_org_hist_tbl(j).new_attribute1;
16064 		       END IF;
16065 		       --
16066 		       IF nvl(l_org_hist_tbl(j).old_attribute2,'$#$') <>
16067 			  nvl(l_org_hist_tbl(j).new_attribute2,'$#$') THEN
16068 			  l_prev_rec.old_attribute2 := l_org_hist_tbl(j).old_attribute2;
16069 			  l_prev_rec.new_attribute2 := l_org_hist_tbl(j).new_attribute2;
16070 		       END IF;
16071 		       --
16072 		       IF nvl(l_org_hist_tbl(j).old_attribute3,'$#$') <>
16073 			  nvl(l_org_hist_tbl(j).new_attribute3,'$#$') THEN
16074 			  l_prev_rec.old_attribute3 := l_org_hist_tbl(j).old_attribute3;
16075 			  l_prev_rec.new_attribute3 := l_org_hist_tbl(j).new_attribute3;
16076 		       END IF;
16077 		       --
16078 		       IF nvl(l_org_hist_tbl(j).old_attribute4,'$#$') <>
16079 			  nvl(l_org_hist_tbl(j).new_attribute4,'$#$') THEN
16080 			  l_prev_rec.old_attribute4 := l_org_hist_tbl(j).old_attribute4;
16081 			  l_prev_rec.new_attribute4 := l_org_hist_tbl(j).new_attribute4;
16082 		       END IF;
16083 		       --
16084 		       IF nvl(l_org_hist_tbl(j).old_attribute5,'$#$') <>
16085 			  nvl(l_org_hist_tbl(j).new_attribute5,'$#$') THEN
16086 			  l_prev_rec.old_attribute5 := l_org_hist_tbl(j).old_attribute5;
16087 			  l_prev_rec.new_attribute5 := l_org_hist_tbl(j).new_attribute5;
16088 		       END IF;
16089 		       --
16090 		       IF nvl(l_org_hist_tbl(j).old_attribute6,'$#$') <>
16091 			  nvl(l_org_hist_tbl(j).new_attribute6,'$#$') THEN
16092 			  l_prev_rec.old_attribute6 := l_org_hist_tbl(j).old_attribute6;
16093 			  l_prev_rec.new_attribute6 := l_org_hist_tbl(j).new_attribute6;
16094 		       END IF;
16095 		       --
16096 		       IF nvl(l_org_hist_tbl(j).old_attribute7,'$#$') <>
16097 			  nvl(l_org_hist_tbl(j).new_attribute7,'$#$') THEN
16098 			  l_prev_rec.old_attribute7 := l_org_hist_tbl(j).old_attribute7;
16099 			  l_prev_rec.new_attribute7 := l_org_hist_tbl(j).new_attribute7;
16100 		       END IF;
16101 		       --
16102 		       IF nvl(l_org_hist_tbl(j).old_attribute8,'$#$') <>
16103 			  nvl(l_org_hist_tbl(j).new_attribute8,'$#$') THEN
16104 			  l_prev_rec.old_attribute8 := l_org_hist_tbl(j).old_attribute8;
16105 			  l_prev_rec.new_attribute8 := l_org_hist_tbl(j).new_attribute8;
16106 		       END IF;
16107 		       --
16108 		       IF nvl(l_org_hist_tbl(j).old_attribute9,'$#$') <>
16109 			  nvl(l_org_hist_tbl(j).new_attribute9,'$#$') THEN
16110 			  l_prev_rec.old_attribute9 := l_org_hist_tbl(j).old_attribute9;
16111 			  l_prev_rec.new_attribute9 := l_org_hist_tbl(j).new_attribute9;
16112 		       END IF;
16113 		       --
16114 		       IF nvl(l_org_hist_tbl(j).old_attribute10,'$#$') <>
16115 			  nvl(l_org_hist_tbl(j).new_attribute10,'$#$') THEN
16116 			  l_prev_rec.old_attribute10 := l_org_hist_tbl(j).old_attribute10;
16117 			  l_prev_rec.new_attribute10 := l_org_hist_tbl(j).new_attribute10;
16118 		       END IF;
16119 		       --
16120 		       IF nvl(l_org_hist_tbl(j).old_attribute11,'$#$') <>
16121 			  nvl(l_org_hist_tbl(j).new_attribute11,'$#$') THEN
16122 			  l_prev_rec.old_attribute11 := l_org_hist_tbl(j).old_attribute11;
16123 			  l_prev_rec.new_attribute11 := l_org_hist_tbl(j).new_attribute11;
16124 		       END IF;
16125 		       --
16126 		       IF nvl(l_org_hist_tbl(j).old_attribute12,'$#$') <>
16127 			  nvl(l_org_hist_tbl(j).new_attribute12,'$#$') THEN
16128 			  l_prev_rec.old_attribute12 := l_org_hist_tbl(j).old_attribute12;
16129 			  l_prev_rec.new_attribute12 := l_org_hist_tbl(j).new_attribute12;
16130 		       END IF;
16131 		       --
16132 		       IF nvl(l_org_hist_tbl(j).old_attribute13,'$#$') <>
16133 			  nvl(l_org_hist_tbl(j).new_attribute13,'$#$') THEN
16134 			  l_prev_rec.old_attribute13 := l_org_hist_tbl(j).old_attribute13;
16135 			  l_prev_rec.new_attribute13 := l_org_hist_tbl(j).new_attribute13;
16136 		       END IF;
16137 		       --
16138 		       IF nvl(l_org_hist_tbl(j).old_attribute14,'$#$') <>
16139 			  nvl(l_org_hist_tbl(j).new_attribute14,'$#$') THEN
16140 			  l_prev_rec.old_attribute14 := l_org_hist_tbl(j).old_attribute14;
16141 			  l_prev_rec.new_attribute14 := l_org_hist_tbl(j).new_attribute14;
16142 		       END IF;
16143 		       --
16144 		       IF nvl(l_org_hist_tbl(j).old_attribute15,'$#$') <>
16145 			  nvl(l_org_hist_tbl(j).new_attribute15,'$#$') THEN
16146 			  l_prev_rec.old_attribute15 := l_org_hist_tbl(j).old_attribute15;
16147 			  l_prev_rec.new_attribute15 := l_org_hist_tbl(j).new_attribute15;
16148 		       END IF;
16149 		       --
16150 		    ELSE -- Txn id not same
16151 		       IF l_flag = 'Y' THEN
16152 			  update csi_i_org_assignments_h
16153 			  set old_operating_unit_id = l_prev_rec.old_operating_unit_id,
16154 			      new_operating_unit_id = l_prev_rec.new_operating_unit_id,
16155 			      old_relationship_type_code = l_prev_rec.old_relationship_type_code,
16156 			      new_relationship_type_code = l_prev_rec.new_relationship_type_code,
16157 			      old_active_start_date = l_prev_rec.old_active_start_date,
16158 			      new_active_start_date = l_prev_rec.new_active_start_date,
16159 			      old_active_end_date = l_prev_rec.old_active_end_date,
16160 			      new_active_end_date = l_prev_rec.new_active_end_date,
16161 			      old_context = l_prev_rec.old_context,
16162 			      new_context = l_prev_rec.new_context,
16163 			      old_attribute1 = l_prev_rec.old_attribute1,
16164 			      new_attribute1= l_prev_rec.new_attribute1,
16165 			      old_attribute2 = l_prev_rec.old_attribute2,
16166 			      new_attribute2= l_prev_rec.new_attribute2,
16167 			      old_attribute3 = l_prev_rec.old_attribute3,
16168 			      new_attribute3= l_prev_rec.new_attribute3,
16169 			      old_attribute4 = l_prev_rec.old_attribute4,
16170 			      new_attribute4= l_prev_rec.new_attribute4,
16171 			      old_attribute5 = l_prev_rec.old_attribute5,
16172 			      new_attribute5= l_prev_rec.new_attribute5,
16173 			      old_attribute6 = l_prev_rec.old_attribute6,
16174 			      new_attribute6= l_prev_rec.new_attribute6,
16175 			      old_attribute7 = l_prev_rec.old_attribute7,
16176 			      new_attribute7= l_prev_rec.new_attribute7,
16177 			      old_attribute8 = l_prev_rec.old_attribute8,
16178 			      new_attribute8= l_prev_rec.new_attribute8,
16179 			      old_attribute9 = l_prev_rec.old_attribute9,
16180 			      new_attribute9= l_prev_rec.new_attribute9,
16181 			      old_attribute10 = l_prev_rec.old_attribute10,
16182 			      new_attribute10= l_prev_rec.new_attribute10,
16183 			      old_attribute11 = l_prev_rec.old_attribute11,
16184 			      new_attribute11= l_prev_rec.new_attribute11,
16185 			      old_attribute12 = l_prev_rec.old_attribute12,
16186 			      new_attribute12= l_prev_rec.new_attribute12,
16187 			      old_attribute13 = l_prev_rec.old_attribute13,
16188 			      new_attribute13= l_prev_rec.new_attribute13,
16189 			      old_attribute14 = l_prev_rec.old_attribute14,
16190 			      new_attribute14= l_prev_rec.new_attribute14,
16191 			      old_attribute15 = l_prev_rec.old_attribute15,
16192 			      new_attribute15= l_prev_rec.new_attribute15,
16193 			      last_update_date = sysdate
16194 			  where instance_ou_history_id = l_prev_rec.instance_ou_history_id;
16195 			  --
16196 			  FORALL i in 1..l_del_ou_hist_tbl.count
16197 			  delete from csi_i_org_assignments_h
16198 			  where instance_ou_history_id = l_del_ou_hist_tbl(i);
16199 			  --
16200 			  l_flag := 'N';
16201 			  l_del_ou_hist_tbl.DELETE;
16202 		       END IF;
16203 		       l_prev_rec := l_org_hist_tbl(j);
16204 		    END IF;
16205 		 END IF;
16206 	      END LOOP;
16207 	      -- Update the instance_ou_id with the one that is retained
16208 	      IF l_flag = 'Y' THEN -- Just in case the last record in the loop matches with prev txn
16209 		 update csi_i_org_assignments_h
16210 		 set old_operating_unit_id = l_prev_rec.old_operating_unit_id,
16211 		     new_operating_unit_id = l_prev_rec.new_operating_unit_id,
16212 		     old_relationship_type_code = l_prev_rec.old_relationship_type_code,
16213 		     new_relationship_type_code = l_prev_rec.new_relationship_type_code,
16214 		     old_active_start_date = l_prev_rec.old_active_start_date,
16215 		     new_active_start_date = l_prev_rec.new_active_start_date,
16216 		     old_active_end_date = l_prev_rec.old_active_end_date,
16217 		     new_active_end_date = l_prev_rec.new_active_end_date,
16218 		     old_context = l_prev_rec.old_context,
16219 		     new_context = l_prev_rec.new_context,
16220 		     old_attribute1 = l_prev_rec.old_attribute1,
16221 		     new_attribute1= l_prev_rec.new_attribute1,
16222 		     old_attribute2 = l_prev_rec.old_attribute2,
16223 		     new_attribute2= l_prev_rec.new_attribute2,
16224 		     old_attribute3 = l_prev_rec.old_attribute3,
16225 		     new_attribute3= l_prev_rec.new_attribute3,
16226 		     old_attribute4 = l_prev_rec.old_attribute4,
16227 		     new_attribute4= l_prev_rec.new_attribute4,
16228 		     old_attribute5 = l_prev_rec.old_attribute5,
16229 		     new_attribute5= l_prev_rec.new_attribute5,
16230 		     old_attribute6 = l_prev_rec.old_attribute6,
16231 		     new_attribute6= l_prev_rec.new_attribute6,
16232 		     old_attribute7 = l_prev_rec.old_attribute7,
16233 		     new_attribute7= l_prev_rec.new_attribute7,
16234 		     old_attribute8 = l_prev_rec.old_attribute8,
16235 		     new_attribute8= l_prev_rec.new_attribute8,
16236 		     old_attribute9 = l_prev_rec.old_attribute9,
16237 		     new_attribute9= l_prev_rec.new_attribute9,
16238 		     old_attribute10 = l_prev_rec.old_attribute10,
16239 		     new_attribute10= l_prev_rec.new_attribute10,
16240 		     old_attribute11 = l_prev_rec.old_attribute11,
16241 		     new_attribute11= l_prev_rec.new_attribute11,
16242 		     old_attribute12 = l_prev_rec.old_attribute12,
16243 		     new_attribute12= l_prev_rec.new_attribute12,
16244 		     old_attribute13 = l_prev_rec.old_attribute13,
16245 		     new_attribute13= l_prev_rec.new_attribute13,
16246 		     old_attribute14 = l_prev_rec.old_attribute14,
16247 		     new_attribute14= l_prev_rec.new_attribute14,
16248 		     old_attribute15 = l_prev_rec.old_attribute15,
16249 		     new_attribute15= l_prev_rec.new_attribute15,
16250 		     last_update_date = sysdate
16251 		 where instance_ou_history_id = l_prev_rec.instance_ou_history_id;
16252 		 --
16253 		 FORALL i in 1..l_del_ou_hist_tbl.count
16254 		 delete from csi_i_org_assignments_h
16255 		 where instance_ou_history_id = l_del_ou_hist_tbl(i);
16256 		 --
16257 		 l_flag := 'N';
16258 		 l_del_ou_hist_tbl.DELETE;
16259 	      END IF;
16260 	      --
16261 	      FOR x in l_org_hist_tbl.FIRST .. l_org_hist_tbl.LAST LOOP
16262 		 update csi_i_org_assignments_h
16263 		 set instance_ou_id = v_ret_ou_id,
16264 		     last_update_date = sysdate
16265 		 where instance_ou_history_id = l_org_hist_tbl(x).instance_ou_history_id;
16266 	      END LOOP;
16267 	   END IF;
16268 	   --
16269 	   -- Delete the Duplicate Org Assignments
16270 	   FORALL x in l_del_tbl.FIRST .. l_del_tbl.LAST
16271 	      DELETE FROM CSI_I_ORG_ASSIGNMENTS
16272 	      where instance_ou_id = l_del_tbl(x);
16273 	   commit;
16274 	Exception
16275 	   when Process_next then
16276 	      null;
16277 	End;
16278      END LOOP;
16279      commit;
16280   END Delete_Dup_Org_Assignments;
16281   --
16282   PROCEDURE dump_unprocessed_fs_serials IS
16283 
16284     CURSOR fs_cur IS
16285       SELECT inventory_item_id,
16286              serial_number,
16287              instance_id,
16288              date_time_stamp,
16289              mtl_txn_id,
16290              error_message
16291       FROM   csi_ii_forward_sync_temp
16292       WHERE  process_flag <> 'P';
16293 
16294     CURSOR err_txn_cur (p_item_id IN number, p_serial_number IN varchar2) IS
16295       SELECT cdt.mtl_txn_name,
16296              cdt.mtl_txn_id,
16297              cdt.mtl_txn_date,
16298              cte.error_text
16299       FROM   csi_diagnostics_temp cdt,
16300              csi_txn_errors       cte
16301       WHERE  cdt.inventory_item_id = p_item_id
16302       AND    cdt.serial_number = p_serial_number
16303       AND    cte.inv_material_transaction_id = cdt.mtl_txn_id
16304       AND    cte.processed_flag in ('E', 'R')
16305       ORDER BY diag_seq_id;
16306 
16307     l_out varchar2(2000);
16308 
16309   BEGIN
16310     FOR fs_rec IN fs_cur
16311     LOOP
16312 
16313       IF fs_cur%rowcount = 1 THEN
16314         l_out := fill('item_id', 10)||
16315                  fill('serial_number', 20)||
16316                  fill('inst_id',10)||
16317                  fill('time_stamp',12)||
16318                  fill('mtl_txn_id',10)||
16319                  fill('error_message', 18);
16320         log(l_out);
16321         l_out := fill('-------', 10)||
16322                  fill('-------------', 20)||
16323                  fill('-------',10)||
16324                  fill('----------',12)||
16325                  fill('----------',10)||
16326                  fill('-------------', 18);
16327         log(l_out);
16328       END IF;
16329 
16330       l_out := fill(fs_rec.inventory_item_id, 10)||
16331                fill(fs_rec.serial_number, 20)||
16332                fill(fs_rec.instance_id,10)||
16333                fill(fs_rec.date_time_stamp,12)||
16334                fill(fs_rec.mtl_txn_id,10)||
16335                fill(fs_rec.error_message, 18);
16336 
16337       FOR err_txn_rec IN err_txn_cur(fs_rec.inventory_item_id, fs_rec.serial_number)
16338       LOOP
16339         l_out := '  '||
16340                  fill(err_txn_rec.mtl_txn_name, 25)||
16341                  fill(err_txn_rec.mtl_txn_id, 10)||
16342                  fill(err_txn_rec.mtl_txn_date, 12)||
16343                  fill(err_txn_rec.error_text, 30);
16344         log(l_out);
16345       END LOOP;
16346     END LOOP;
16347   END dump_unprocessed_fs_serials;
16348 
16349   PROCEDURE populate_mtl_txn_creation_date
16350   IS
16351     l_creation_date date;
16352 
16353     CURSOR fs_cur IS
16354       SELECT mtl_txn_id
16355       FROM   csi_ii_forward_sync_temp
16356       WHERE  process_flag <> 'P'
16357       AND    mtl_txn_creation_date is null
16358       FOR UPDATE OF mtl_txn_creation_date;
16359   BEGIN
16360     FOR fs_rec IN fs_cur
16361     LOOP
16362       SELECT creation_date
16363       INTO   l_creation_date
16364       FROM   mtl_material_transactions
16365       WHERE  transaction_id = fs_rec.mtl_txn_id;
16366 
16367       UPDATE csi_ii_forward_sync_temp
16368       SET    mtl_txn_creation_date = l_creation_date
16369       WHERE  current of fs_cur;
16370 
16371     END LOOP;
16372     commit;
16373   END populate_mtl_txn_creation_date;
16374 
16375   --
16376   PROCEDURE create_mmt_trigger IS
16377   BEGIN
16378 
16379     log(date_time_stamp||'creating mmt trigger csi_block_mat_txn_trg');
16380 
16381     EXECUTE IMMEDIATE
16382      'CREATE OR REPLACE TRIGGER CSI_BLOCK_MAT_TXN_TRG
16383       BEFORE INSERT ON MTL_MATERIAL_TRANSACTIONS
16384       REFERENCING NEW AS NEW OLD AS OLD
16385       FOR EACH ROW
16386       DECLARE
16387         v_nl_trackable   VARCHAR2(1);
16388       BEGIN
16389         BEGIN
16390           SELECT comms_nl_trackable_flag
16391           INTO v_nl_trackable
16392           FROM MTL_SYSTEM_ITEMS msi,
16393                MTL_PARAMETERS mp
16394           where mp.organization_id = :new.organization_id
16395           and   msi.inventory_item_id = :new.inventory_item_id
16396           and   msi.organization_id = mp.master_organization_id;
16397         EXCEPTION
16398           WHEN no_data_found THEN
16399             NULL;
16400         END;
16401         IF nvl(v_nl_trackable,''N'') = ''Y'' THEN
16402           :new.last_updated_by := null;
16403         END IF;
16404       END;';
16405   EXCEPTION
16406     WHEN OTHERS THEN
16407       NULL;
16408   END create_mmt_trigger;
16409 
16410   PROCEDURE drop_mmt_trigger IS
16411   BEGIN
16412     log(date_time_stamp||'dropping the trigger csi_block_mat_txn_trg');
16413     EXECUTE IMMEDIATE 'DROP TRIGGER CSI_BLOCK_MAT_TXN_TRG';
16414   EXCEPTION
16415     WHEN others THEN
16416       null;
16417   END drop_mmt_trigger;
16418 
16419   PROCEDURE ib_sync(
16420     errbuf            OUT NOCOPY VARCHAR2,
16421     retcode           OUT NOCOPY NUMBER,
16422     p_show_instances  IN         VARCHAR2,
16423     p_mode            IN         VARCHAR2,
16424     p_force_data_fix  IN         VARCHAR2)
16425   IS
16426     l_errbuf           varchar2(2000);
16427     l_retcode          number;
16428     l_recount          number;
16429     comp_error         exception;
16430     l_auto_populate_allowed varchar2(3); --bug 5248037--
16431 
16432     --
16433     TYPE LOOKUP_REC IS RECORD
16434        ( lookup_code       VARCHAR2(30),
16435          enabled_flag      VARCHAR2(1)
16436        );
16437     TYPE LOOKUP_TBL IS TABLE OF LOOKUP_REC INDEX BY BINARY_INTEGER;
16438     --
16439     l_lookup_tbl          LOOKUP_TBL;
16440     l_ctr                 NUMBER := 0;
16441     l_type                VARCHAR2(30) := 'CSI_CORRECTION_ROUTINES';
16442     --
16443     CURSOR CSI_LOOKUP_CUR IS
16444     select lookup_code,enabled_flag
16445     from CSI_LOOKUPS
16446     where lookup_type = l_type;
16447     --
16448     FUNCTION Is_Routine_Enabled(
16449       p_lookup_tbl       in lookup_tbl,
16450       p_routine_name     in varchar2)
16451     RETURN BOOLEAN
16452     IS
16453        l_ret_value   BOOLEAN := TRUE;
16454        l_flag        VARCHAR2(1);
16455     BEGIN
16456        l_flag := 'Y'; -- No data will qualify for Procedure execution
16457        --
16458        IF p_lookup_tbl.count > 0 THEN
16459 	  FOR J IN p_lookup_tbl.FIRST .. p_lookup_tbl.LAST LOOP
16460 	     IF p_lookup_tbl(J).lookup_code = p_routine_name THEN
16461 		l_flag := p_lookup_tbl(J).enabled_flag;
16462 		exit;
16463 	     END IF;
16464 	  END LOOP;
16465        END IF;
16466        --
16467        IF l_flag = 'Y' THEN
16468 	  l_ret_value := TRUE;
16469        ELSE
16470 	  l_ret_value := FALSE;
16471        END IF;
16472        --
16473        RETURN l_ret_value;
16474     END Is_Routine_Enabled;
16475     --
16476   BEGIN
16477 
16478     debug_off;
16479 
16480     log('------------------------------------------------------------------------');
16481     log(date_time_stamp||'start ib_sync ');
16482     log('  parameter - Show Instances    :'||p_show_instances);
16483     log('  parameter - Mode of execution :'||p_mode);
16484     --
16485     IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
16486        csi_gen_utility_pvt.populate_install_param_rec;
16487     END IF;
16488     --
16489     IF csi_datastructures_pub.g_install_param_rec.fetch_flag = 'N' OR
16490        csi_datastructures_pub.g_install_param_rec.freeze_flag = 'N' THEN
16491        log('Install Parameters no set. Process terminating...');
16492        errbuf := 'CSI Install Parameters not set...';
16493        retcode := 1;
16494        RAISE comp_error;
16495     END IF;
16496     -- checks block -- show stoppers
16497     log(date_time_stamp||'get_nl_trackable_report');
16498     csi_diagnostics_pkg.get_nl_trackable_report;
16499 
16500     IF l_global_warning_flag = 'Y' THEN
16501       log('Inconsistent IB trackable flag setup in org items and master items detected.');
16502       log('Please fix the items in the report csinonl.<mmddyy>.dbg and then rerun the program.');
16503       log('Process terminating...');
16504       errbuf := 'Inconsistent IB trackability in org items and master items detected';
16505       retcode := 1;
16506       RAISE comp_error;
16507     END IF;
16508 
16509     -- reports block
16510     log(date_time_stamp||'check_org_uniqueness');
16511     csi_diagnostics_pkg.check_org_uniqueness;
16512 
16513     log(date_time_stamp||'get_non_srl_rma_report');
16514     csi_diagnostics_pkg.get_non_srl_rma_report(
16515       p_show_instances  => p_show_instances);
16516     --
16517     -- Enable the Lookup Values that contain the Data Fix routines so that they all get executed.
16518     --
16519     IF nvl(p_force_data_fix,'N') = 'Y' THEN
16520        UPDATE FND_LOOKUP_VALUES
16521        SET enabled_flag = 'Y',
16522 	   last_updated_by = -1,
16523 	   last_update_date = sysdate
16524        WHERE lookup_type = l_type;
16525        --
16526        commit;
16527     END IF;
16528     --
16529     IF p_mode IN ('C', 'S') THEN
16530       -- datafix block -
16531       FOR csi_rec IN CSI_LOOKUP_CUR LOOP
16532          l_ctr := l_ctr + 1;
16533          l_lookup_tbl(l_ctr).lookup_code := csi_rec.lookup_code;
16534          l_lookup_tbl(l_ctr).enabled_flag := csi_rec.enabled_flag;
16535       END LOOP;
16536       --
16537       IF Is_Routine_Enabled(l_lookup_tbl,'DELETE_DUP_RELATIONSHIP') THEN
16538          Update_Lookup('DELETE_DUP_RELATIONSHIP');
16539          log(date_time_stamp||'delete_dup_relationship');
16540          csi_diagnostics_pkg.Delete_Dup_Relationship;
16541          commit;
16542       ELSE
16543          log(date_time_stamp||'delete_dup_relationship already executed...');
16544       END IF;
16545 
16546       IF Is_Routine_Enabled(l_lookup_tbl,'UPDATE_NO_CTL_SRL_LOT_INST') THEN
16547          Update_Lookup('UPDATE_NO_CTL_SRL_LOT_INST');
16548          log(date_time_stamp||'update_no_ctl_srl_lot_inst');
16549          csi_diagnostics_pkg.Update_No_Ctl_Srl_Lot_Inst;
16550          commit;
16551       ELSE
16552          log(date_time_stamp||'Update_No_Ctl_Srl_Lot_Inst already executed...');
16553       END IF;
16554 
16555       IF Is_Routine_Enabled(l_lookup_tbl,'DELETE_DUP_SRL_INV_INSTANCE') THEN
16556          Update_Lookup('DELETE_DUP_SRL_INV_INSTANCE');
16557          log(date_time_stamp||'delete_dup_srl_inv_instance');
16558          csi_diagnostics_pkg.Delete_Dup_Srl_Inv_Instance;
16559       ELSE
16560          log(date_time_stamp||'Delete_Dup_Srl_Inv_Instance already executed...');
16561       END IF;
16562 
16563       IF Is_Routine_Enabled(l_lookup_tbl,'UPDATE_DUP_SRL_INSTANCE') THEN
16564          Update_Lookup('UPDATE_DUP_SRL_INSTANCE');
16565          log(date_time_stamp||'update_dup_srl_instance');
16566          csi_diagnostics_pkg.Update_Dup_Srl_Instance;
16567          commit;
16568       ELSE
16569          log(date_time_stamp||'Update_Dup_Srl_Instance already executed...');
16570       END IF;
16571 
16572       IF Is_Routine_Enabled(l_lookup_tbl,'DEL_API_DUP_SRL_INSTANCE') THEN
16573          Update_Lookup('DEL_API_DUP_SRL_INSTANCE');
16574          log(date_time_stamp||'del_api_dup_srl_instance');
16575          csi_diagnostics_pkg.Del_API_Dup_Srl_Instance;
16576          commit;
16577       ELSE
16578          log(date_time_stamp||'Del_API_Dup_Srl_Instance already executed...');
16579       END IF;
16580 
16581       IF Is_Routine_Enabled(l_lookup_tbl,'UPDATE_INSTANCE_USAGE') THEN
16582          Update_Lookup('UPDATE_INSTANCE_USAGE');
16583          log(date_time_stamp||'update_instance_usage');
16584          csi_diagnostics_pkg.Update_Instance_Usage;
16585          commit;
16586       ELSE
16587          log(date_time_stamp||'Update_Instance_Usage already executed...');
16588       END IF;
16589 
16590       IF Is_Routine_Enabled(l_lookup_tbl,'UPDATE_FULL_DUMP_FLAG') THEN
16591          Update_Lookup('UPDATE_FULL_DUMP_FLAG');
16592          log(date_time_stamp||'update_full_dump_flag');
16593          csi_diagnostics_pkg.Update_Full_dump_flag;
16594          commit;
16595       ELSE
16596          log(date_time_stamp||'Update_Full_dump_flag already executed...');
16597       END IF;
16598 
16599       IF Is_Routine_Enabled(l_lookup_tbl,'UPDATE_VLD_ORGANIZATION') THEN
16600          Update_Lookup('UPDATE_VLD_ORGANIZATION');
16601          log(date_time_stamp||'update_vld_organization');
16602          csi_diagnostics_pkg.Update_Vld_Organization;
16603          commit;
16604       ELSE
16605          log(date_time_stamp||'Update_Vld_Organization already executed...');
16606       END IF;
16607 
16608       IF Is_Routine_Enabled(l_lookup_tbl,'UPDATE_REVISION') THEN
16609          Update_Lookup('UPDATE_REVISION');
16610          log(date_time_stamp||'update_revision');
16611          csi_diagnostics_pkg.Update_Revision;
16612          commit;
16613       ELSE
16614          log(date_time_stamp||'Update_Revision already executed...');
16615       END IF;
16616 
16617       IF Is_Routine_Enabled(l_lookup_tbl,'MERGE_NON_SRL_INV_INSTANCE') THEN
16618          Update_Lookup('MERGE_NON_SRL_INV_INSTANCE');
16619          log(date_time_stamp||'merge_non_srl_inv_instance');
16620          csi_diagnostics_pkg.merge_non_srl_inv_instance;
16621          commit;
16622       ELSE
16623          log(date_time_stamp||'merge_non_srl_inv_instance already executed...');
16624       END IF;
16625 
16626       IF Is_Routine_Enabled(l_lookup_tbl,'DELETE_DUP_ACCOUNT') THEN
16627          Update_Lookup('DELETE_DUP_ACCOUNT');
16628          log(date_time_stamp||'delete_dup_account');
16629          csi_diagnostics_pkg.delete_dup_account;
16630          commit;
16631       ELSE
16632          log(date_time_stamp||'delete_dup_account already executed...');
16633       END IF;
16634 
16635       IF Is_Routine_Enabled(l_lookup_tbl,'UPDATE_INSTANCE_PARTY_SOURCE') THEN
16636          Update_Lookup('UPDATE_INSTANCE_PARTY_SOURCE');
16637          log(date_time_stamp||'update_instance_party_source');
16638          csi_diagnostics_pkg.update_instance_party_source;
16639          commit;
16640       ELSE
16641          log(date_time_stamp||'update_instance_party_source already executed...');
16642       END IF;
16643 
16644       IF Is_Routine_Enabled(l_lookup_tbl,'UPDATE_CONTACT_PARTY_RECORD') THEN
16645          Update_Lookup('UPDATE_CONTACT_PARTY_RECORD');
16646          log(date_time_stamp||'update_contact_party_record');
16647          csi_diagnostics_pkg.update_contact_party_record;
16648          commit;
16649       ELSE
16650          log(date_time_stamp||'update_contact_party_record already executed...');
16651       END IF;
16652 
16653       IF Is_Routine_Enabled(l_lookup_tbl,'REVERT_PARTY_REL_TYPE_UPDATE') THEN
16654          Update_Lookup('REVERT_PARTY_REL_TYPE_UPDATE');
16655          log(date_time_stamp||'revert_party_rel_type_update');
16656          csi_diagnostics_pkg.revert_party_rel_type_update;
16657          commit;
16658       ELSE
16659          log(date_time_stamp||'revert_party_rel_type_update already executed...');
16660       END IF;
16661 
16662       IF Is_Routine_Enabled(l_lookup_tbl,'UPDATE_MASTER_ORGANIZATION_ID') THEN
16663          Update_Lookup('UPDATE_MASTER_ORGANIZATION_ID');
16664          log(date_time_stamp||'update_master_organization_id');
16665          csi_diagnostics_pkg.update_master_organization_ID;
16666          commit;
16667       ELSE
16668          log(date_time_stamp||'update_master_organization_ID already executed...');
16669       END IF;
16670 
16671       IF Is_Routine_Enabled(l_lookup_tbl,'MISSING_MTL_TXN_ID_IN_CSI') THEN
16672          Update_Lookup('MISSING_MTL_TXN_ID_IN_CSI');
16673          log(date_time_stamp||'missing_mtl_txn_id_in_csi');
16674          csi_diagnostics_pkg.missing_mtl_txn_id_in_csi;
16675          commit;
16676       ELSE
16677          log(date_time_stamp||'missing_mtl_txn_id_in_csi already executed...');
16678       END IF;
16679 
16680       IF Is_Routine_Enabled(l_lookup_tbl,'FIX_WIP_USAGE') THEN
16681          Update_Lookup('FIX_WIP_USAGE');
16682          log(date_time_stamp||'fix_wip_usage');
16683          csi_diagnostics_pkg.fix_wip_usage;
16684          commit;
16685       ELSE
16686          log(date_time_stamp||'fix_wip_usage already executed...');
16687       END IF;
16688 
16689       IF Is_Routine_Enabled(l_lookup_tbl,'DELETE_DUP_NSRL_WIP_INSTANCES') THEN
16690          Update_Lookup('DELETE_DUP_NSRL_WIP_INSTANCES');
16691          log(date_time_stamp||'delete_dup_nsrl_wip_instances');
16692          csi_diagnostics_pkg.delete_dup_nsrl_wip_instances;
16693          commit;
16694       ELSE
16695          log(date_time_stamp||'delete_dup_nsrl_wip_instances already executed...');
16696       END IF;
16697 
16698       IF Is_Routine_Enabled(l_lookup_tbl,'DELETE_DUP_ORG_ASSIGNMENTS') THEN
16699          Update_Lookup('DELETE_DUP_ORG_ASSIGNMENTS');
16700          log(date_time_stamp||'Delete_Dup_Org_Assignments');
16701          csi_diagnostics_pkg.Delete_Dup_Org_Assignments;
16702          commit;
16703       ELSE
16704          log(date_time_stamp||'Delete_Dup_Org_Assignments already executed...');
16705       END IF;
16706       --
16707       commit;
16708 
16709     END IF;
16710 
16711     IF p_mode = 'S' THEN -- Synchronize IB
16712 
16713       create_mmt_trigger;
16714 
16715       -- read pending messages in SFM queue and pump them as errors
16716       log(date_time_stamp||'deque_messages_as_errors');
16717       dequeue_messages_as_errors;
16718 
16719       -- spawn the requbmit interface for the pumped errors
16720       log(date_time_stamp||'Resubmit interface for pending messages');
16721       csi_resubmit_pub.resubmit_interface(
16722         errbuf        => l_errbuf,
16723         retcode       => l_retcode,
16724         p_option      => 'SELECTED');
16725 
16726       log(date_time_stamp||'Resubmit interface completed successfully.');
16727 
16728     END IF;
16729 
16730     IF p_mode in ('C', 'S') THEN
16731 
16732       IF Is_Routine_Enabled(l_lookup_tbl,'EXPIRE_NON_TRACKABLE_INSTANCE') THEN
16733          Update_Lookup('EXPIRE_NON_TRACKABLE_INSTANCE');
16734          log(date_time_stamp||'expire_non_trackable_instance');
16735          csi_diagnostics_pkg.expire_non_trackable_instance;
16736          commit;
16737       ELSE
16738          log(date_time_stamp||'expire_non_trackable_instance already executed...');
16739       END IF;
16740 
16741       log(date_time_stamp||'fix_srlsoi_returned_serials');
16742       csi_diagnostics_pkg.fix_srlsoi_returned_serials;
16743 
16744       log(date_time_stamp||'mark_error_transactions');
16745       csi_diagnostics_pkg.mark_error_transactions;
16746 
16747       log(date_time_stamp||'create_or_update_shipping_inst');
16748       csi_diagnostics_pkg.create_or_update_shipping_inst;
16749 
16750       l_auto_populate_allowed := FND_PROFILE.VALUE('CSI_AUTO_POPULATE_INSTANCE'); --bug 5248037--
16751       log('Auto population allowed '||l_auto_populate_allowed);
16752 
16753       --Added IF condition for bug 5248037--
16754       IF UPPER(l_auto_populate_allowed) = 'Y'  THEN
16755 	log(date_time_stamp||'create_nsrl_rma_tld');
16756         csi_diagnostics_pkg.create_nsrl_rma_tld;
16757       END IF;
16758 
16759       -- serial data spool and preprocess
16760       log(date_time_stamp||'get_srldata');
16761       csi_diagnostics_pkg.get_srldata;
16762 
16763       log(date_time_stamp||'preprocess_srldata');
16764       csi_diagnostics_pkg.preprocess_srldata;
16765 
16766       -- fix srl errors
16767       log(date_time_stamp||'fix_srldata');
16768       csi_diagnostics_pkg.fix_srldata;
16769 
16770       -- serial correction reports
16771       log(date_time_stamp||'spool errors serial Info');
16772       csi_diagnostics_pkg.spool_srldata('ERRORS');
16773 
16774       -- serial correction reports
16775       log(date_time_stamp||'spool all serial info');
16776       csi_diagnostics_pkg.spool_srldata('ALL');
16777 
16778       -- spawn the resubmit interface for the marked errors
16779       log(date_time_stamp||'Resubmit errors for the corrected serial numbers');
16780       csi_resubmit_pub.Resubmit_Interface(
16781         errbuf        => l_errbuf,
16782         retcode       => l_retcode,
16783         p_option      => 'SELECTED');
16784       log(date_time_stamp||'Resubmit interface completed successfully.');
16785 
16786       -- for the newly created column populate the value reading mtl_txn_id
16787       populate_mtl_txn_creation_date;
16788 
16789       -- forward sync serial routine
16790       log(date_time_stamp||'forward_sync');
16791       csi_diagnostics_pkg.forward_sync;
16792       commit;
16793 
16794       -- Check whether all the instances are forward synched
16795       BEGIN
16796         SELECT count(*)
16797         INTO   l_recount
16798         FROM   csi_ii_forward_sync_temp
16799         WHERE  process_flag <> 'P';
16800       END;
16801       --
16802       IF nvl(l_recount,0) > 0 THEN
16803         log( 'Forward Synch did not complete successfully. unprocessed count : '||l_recount);
16804         errbuf  := 'Forward Sync did not complete successfully.';
16805         retcode := 1;
16806         log(date_time_stamp||'dump_unprocessed_fs_serials');
16807         dump_unprocessed_fs_serials;
16808       END IF;
16809       --
16810     END IF;
16811 
16812     IF p_mode = 'S' THEN -- synchronize IB
16813 
16814       IF l_global_sync_flag = 'Y' THEN
16815         log('Error condition detected for SRL uniqueness across org/Non serial RMA failures.');
16816         log('Please check the items reported in the concurrent request output.');
16817         log('Process terminating...');
16818         errbuf  := 'SRL uniqueness across org OR Non serial RMA failures';
16819         retcode := 1;
16820         Raise comp_error;
16821       END IF;
16822 
16823       IF is_sfm_active THEN
16824         log('Please shut down the SFM Event Manager queue and re-run the program.');
16825         log('Process terminating...');
16826         retcode := 1;
16827         errbuf  := 'Please shut down the SFM event manager queue and re-run the program.';
16828         Raise comp_error;
16829       END IF;
16830       --
16831 
16832       log(date_time_stamp||'sync_inv_serials');
16833       csi_diagnostics_pkg.sync_inv_serials;
16834 
16835       log(date_time_stamp||'ib_inv_synch_non_srl');
16836       csi_diagnostics_pkg.ib_inv_synch_non_srl;
16837 
16838       log(date_time_stamp||'reverse_ib_inv_synch');
16839       csi_diagnostics_pkg.reverse_ib_inv_synch;
16840 
16841     END IF;
16842 
16843     drop_mmt_trigger;
16844 
16845     commit;
16846 
16847     log(date_time_stamp||'end ib_sync');
16848     log('------------------------------------------------------------------------');
16849 
16850   EXCEPTION
16851     WHEN comp_error THEN
16852       drop_mmt_trigger;
16853     WHEN OTHERS THEN
16854       drop_mmt_trigger;
16855       log('others error in ib_sync : '||sqlerrm);
16856       retcode := 1;
16857       errbuf  := sqlerrm;
16858   END ib_sync;
16859 
16860 
16861 PROCEDURE create_oper_upd_manager
16862   (x_errbuf         OUT  NOCOPY VARCHAR2,
16863    x_retcode        OUT  NOCOPY VARCHAR2
16864   )
16865  IS
16866  BEGIN
16867    FND_FILE.PUT_LINE(FND_FILE.LOG, 'Start create_oper_upd_manager');
16868   -- Parent Processing
16869 
16870   AD_CONC_UTILS_PKG.Submit_Subrequests
16871     (x_errbuf                    => x_errbuf,
16872      x_retcode                   => x_retcode,
16873      x_workerconc_app_shortname  => 'CSI',
16874      x_workerconc_progname       => 'CSIUPOPS',
16875      x_batch_size                => 1000,
16876      x_num_workers               => 5
16877     );
16878 
16879   FND_FILE.PUT_LINE(FND_FILE.LOG, 'x_errbuf: ' || x_errbuf);
16880   FND_FILE.PUT_LINE(FND_FILE.LOG, 'x_retcode: ' || x_retcode);
16881   FND_FILE.PUT_LINE(FND_FILE.LOG, 'End create_oper_upd_manager');
16882 
16883  END create_oper_upd_manager;
16884 
16885 PROCEDURE create_oper_upd_worker
16886   (x_errbuf         OUT  NOCOPY VARCHAR2,
16887    x_retcode        OUT  NOCOPY VARCHAR2,
16888    x_batch_size     IN   NUMBER,
16889    x_worker_id      IN   NUMBER,
16890    x_num_workers    IN   NUMBER
16891   )
16892  IS
16893    l_table_owner           VARCHAR2(30);
16894    l_product               VARCHAR2(30) := 'CSI';
16895    l_status                VARCHAR2(30);
16896    l_industry              VARCHAR2(30);
16897    l_retstatus             BOOLEAN;
16898    l_batch_size            NUMBER := 1000;
16899    l_worker_id             NUMBER:=5;
16900    l_num_workers           NUMBER := 5;
16901    l_any_rows_to_process   BOOLEAN;
16902    l_table_name            VARCHAR2(30) := 'CSI_ITEM_INSTANCES';
16903    l_update_name           VARCHAR2(30) := 'csidiagb.pls';
16904    l_start_rowid           rowid;
16905    l_end_rowid             rowid;
16906    l_rows_processed        number;
16907  BEGIN
16908 
16909   l_retstatus := fnd_installation.get_app_info(
16910                      l_product, l_status, l_industry, l_table_owner);
16911 
16912   IF ((l_retstatus = FALSE) OR (l_table_owner IS NULL)) THEN
16913        RAISE_APPLICATION_ERROR(-20001,
16914           'Cannot get schema name for product : '||l_product);
16915   END IF;
16916   FND_FILE.PUT_LINE(FND_FILE.LOG, '  x_worker_id : '||x_worker_id);
16917   FND_FILE.PUT_LINE(FND_FILE.LOG, 'x_num_workers : '||x_num_workers);
16918   ad_parallel_updates_pkg.initialize_rowid_range(
16919       ad_parallel_updates_pkg.ROWID_RANGE
16920       ,l_table_owner
16921       ,l_table_name
16922       ,l_update_name
16923       ,x_worker_id
16924       ,x_num_workers
16925       ,x_batch_size,0);
16926 
16927    ad_parallel_updates_pkg.get_rowid_range(
16928        l_start_rowid
16929       ,l_end_rowid
16930       ,l_any_rows_to_process
16931       ,x_batch_size
16932       ,TRUE);
16933 
16934    WHILE(l_any_rows_to_process = TRUE)
16935    LOOP
16936 
16937       UPDATE /*+ rowid(cii) */ csi_item_instances cii
16938       SET    operational_status_code =
16939              DECODE(instance_usage_code,'IN_SERVICE','IN_SERVICE','OUT_OF_SERVICE',
16940                  'OUT_OF_SERVICE', 'INSTALLED','INSTALLED','NOT_USED')
16941              ,LAST_UPDATE_DATE = sysdate
16942              ,LAST_UPDATED_BY = -1
16943       where  operational_status_code IS NULL
16944       AND    rowid between l_start_rowid and l_end_rowid;
16945 
16946       l_rows_processed := SQL%ROWCOUNT;
16947 
16948       ad_parallel_updates_pkg.processed_rowid_range(
16949            l_rows_processed,
16950            l_end_rowid);
16951 
16952       commit;
16953 
16954       ad_parallel_updates_pkg.get_rowid_range(
16955            l_start_rowid,
16956            l_end_rowid,
16957            l_any_rows_to_process,
16958            x_batch_size,
16959            FALSE);
16960    END LOOP;
16961    x_retcode := AD_CONC_UTILS_PKG.CONC_SUCCESS;
16962 
16963     EXCEPTION
16964       WHEN OTHERS THEN
16965         x_retcode := AD_CONC_UTILS_PKG.CONC_FAIL;
16966         RAISE;
16967  END create_oper_upd_worker;
16968 
16969 END csi_diagnostics_pkg;