DBA Data[Home] [Help]

PACKAGE BODY: APPS.CSI_ORDER_SHIP_PUB

Source


1 PACKAGE BODY  csi_order_ship_pub  AS
2 /* $Header: csipiosb.pls 120.47.12020000.3 2012/11/09 10:04:00 mvaradam ship $ */
3 
4 
5   PROCEDURE debug(
6     p_message IN varchar2)
7   IS
8   BEGIN
9     IF csi_t_gen_utility_pvt.g_debug_level > 0 THEN
10       csi_t_gen_utility_pvt.add(p_message);
11     END IF;
12   END debug;
13 
14   PROCEDURE api_log(
15     p_api_name  IN varchar2)
16   IS
17   BEGIN
18 
19     g_api_name := 'csi_order_ship_pub.'||p_api_name;
20 
21     csi_t_gen_utility_pvt.dump_api_info(
22       p_api_name => p_api_name,
23       p_pkg_name => 'csi_order_ship_pub');
24 
25   END api_log;
26 
27   PROCEDURE process_ato_options(
28     p_order_line_rec   IN order_line_rec,
29     x_return_status    OUT NOCOPY varchar2)
30   IS
31     CURSOR ato_option_cur(p_ato_line_id in number) IS
32       SELECT oel.line_id
33       FROM   mtl_system_items   msi,
34              oe_order_lines_all oel
35       WHERE  oel.ato_line_id                       = p_ato_line_id
36       AND    oel.item_type_code                    = 'OPTION'
37       AND    nvl(oel.cancelled_flag,'N')          <> 'Y'
38       AND    msi.inventory_item_id                 = oel.inventory_item_id
39       AND    msi.organization_id                   = p_order_line_rec.om_vld_org_id
40       AND    nvl(msi.comms_nl_trackable_flag, 'N') = 'Y'
41       AND    nvl(msi.shippable_item_flag, 'N')     = 'Y';
42 
43     l_return_status       varchar2(1) := fnd_api.g_ret_sts_success;
44     l_error_message       varchar2(2000);
45     l_already_interfaced  varchar2(1);
46     l_bypass              varchar2(1);
47 
48     l_error_code          number;
49     l_message_id          number;
50     l_error_rec           csi_datastructures_pub.transaction_error_rec;
51 
52   BEGIN
53 
54     api_log('process_ato_options');
55 
56     x_return_status := fnd_api.g_ret_sts_success;
57 
58     IF nvl(p_order_line_rec.ato_line_id,fnd_api.g_miss_num) <> fnd_api.g_miss_num
59     THEN
60 
61       l_bypass := nvl(fnd_profile.value('CSE_BYPASS_EVENT_QUEUE'), 'N');
62 
63       FOR ato_option_rec IN ato_option_cur(p_order_line_rec.ato_line_id)
64       LOOP
65 
66         IF l_bypass = 'N' THEN
67 
68           debug('  Publishing CSISOFUL for option line :'||ato_option_rec.line_id);
69 
70           XNP_CSISOFUL_U.publish(
71             xnp$order_line_id => ato_option_rec.line_id,
72             x_message_id      => l_message_id,
73             x_error_code      => l_error_code,
74             x_error_message   => l_error_message);
75         ELSE
76 
77           debug('  Invoking CSISOFUL for option line :'||ato_option_rec.line_id);
78 
79           l_error_rec.source_id           := ato_option_rec.line_id;
80           l_error_rec.source_type         := 'CSISOFUL';
81           l_error_rec.transaction_type_id := 51;
82 
83           csi_inv_txnstub_pkg.execute_trx_dpl(
84             p_transaction_type  => 'CSISOFUL',
85             p_transaction_id    => ato_option_rec.line_id,
86             x_trx_return_status => l_return_status,
87             x_trx_error_rec     => l_error_rec);
88 
89           IF l_return_status <> fnd_api.g_ret_sts_success THEN
90             csi_inv_trxs_pkg.log_csi_error(l_error_rec);
91             l_return_status := fnd_api.g_ret_sts_success;
92           END IF;
93 
94         END IF;
95 
96         csi_t_gen_utility_pvt.build_file_name(
97           p_file_segment1 => 'csisoshp',
98           p_file_segment2 => p_order_line_rec.inv_mtl_transaction_id);
99 
100       END LOOP;
101     END IF;
102 
103   EXCEPTION
104     WHEN fnd_api.g_exc_error THEN
105       x_return_status := fnd_api.g_ret_sts_error;
106   END process_ato_options;
107 
108   PROCEDURE create_dflt_td_from_ship(
109     p_serial_code         IN            number,
110     p_order_line_rec      IN            order_line_rec,
111     p_trackable_parent    IN            boolean,
112     px_order_shipment_tbl IN OUT NOCOPY order_shipment_tbl,
113     x_transaction_line_id    OUT NOCOPY number,
114     x_return_status          OUT NOCOPY varchar2)
115   IS
116 
117     l_ship_tbl            order_shipment_tbl;
118 
119     l_c_ind               binary_integer := 0;
120     l_c_tl_rec            csi_t_datastructures_grp.txn_line_rec ;
121     l_c_tld_tbl           csi_t_datastructures_grp.txn_line_detail_tbl;
122     l_c_t_pty_tbl         csi_t_datastructures_grp.txn_party_detail_tbl ;
123     l_c_t_pty_acct_tbl    csi_t_datastructures_grp.txn_pty_acct_detail_tbl;
124     l_c_t_oa_tbl          csi_t_datastructures_grp.txn_org_assgn_tbl;
125     l_c_t_ea_tbl          csi_t_datastructures_grp.txn_ext_attrib_vals_tbl;
126     l_c_t_sys_tbl         csi_t_datastructures_grp.txn_systems_tbl;
127     l_c_t_iir_tbl         csi_t_datastructures_grp.txn_ii_rltns_tbl;
128 
129     l_return_status       varchar2(1) := fnd_api.g_ret_sts_success;
130 
131     l_msg_count           number;
132     l_msg_data            varchar2(2000);
133     l_instance_party_id   number;
134     l_ip_account_id       number;
135     l_end_loop            number;
136     l_quantity            number;
137     l_parent_ord_qty      number;
138     l_total_qty           number := 0;
139 
140     l_satisfied           boolean := FALSE;
141 
142   BEGIN
143 
144     x_return_status := fnd_api.g_ret_sts_success;
145 
146     api_log('create_dflt_td_from_ship');
147 
148     l_ship_tbl := px_order_shipment_tbl;
149 
150     /* Initialize the pl/sql tables */
151     l_c_tld_tbl.delete;
152     l_c_t_pty_tbl.delete;
153     l_c_t_pty_acct_tbl.delete;
154     l_c_t_oa_tbl.delete;
155     l_c_t_ea_tbl.delete;
156     l_c_t_sys_tbl.delete;
157     l_c_t_iir_tbl.delete;
158 
159     -- assign values for the columns in Txn_line_rec
160     l_c_tl_rec.transaction_line_id        := fnd_api.g_miss_num;
161     l_c_tl_rec.source_transaction_id      := p_order_line_rec.order_line_id;
162     l_c_tl_rec.source_transaction_type_id := g_txn_type_id;
163     l_c_tl_rec.source_transaction_table   := 'WSH_DELIVERY_DETAILS';
164     l_c_tl_rec.processing_status          := 'IN_PROCESS';
165     l_c_tl_rec.object_version_number      := 1;
166 
167     BEGIN
168       SELECT transaction_line_id
169       INTO   l_c_tl_rec.transaction_line_id
170       FROM   csi_t_transaction_lines
171       WHERE  source_transaction_table   = l_c_tl_rec.source_transaction_table
172       AND    source_transaction_id      = l_c_tl_rec.source_transaction_id
173       AND    source_transaction_type_id = l_c_tl_rec.source_transaction_type_id;
174     EXCEPTION
175       WHEN no_data_found THEN
176         null;
177     END;
178 
179     IF l_ship_tbl.count > 0 THEN
180 
181       FOR s_ind in l_ship_tbl.FIRST .. l_ship_tbl.LAST
182       LOOP
183 
184         IF p_serial_code <> 1 OR p_trackable_parent THEN
185           l_end_loop := l_ship_tbl(s_ind).shipped_quantity;
186           l_quantity := 1;
187         ELSE
188 
189           IF nvl(p_order_line_rec.link_to_line_id, fnd_api.g_miss_num) <> fnd_api.g_miss_num THEN
190 
191             -- create tld records with respect to the parent/child ratio
192             SELECT ordered_quantity
193             INTO   l_parent_ord_qty
194             FROM   oe_order_lines_all
195             WHERE  line_id = p_order_line_rec.link_to_line_id;
196 
197             l_end_loop := l_parent_ord_qty;
198             l_quantity := p_order_line_rec.ordered_quantity/l_parent_ord_qty;
199 
200           ELSE
201             l_end_loop := 1;
202             l_quantity := l_ship_tbl(s_ind).shipped_quantity;
203           END IF;
204 
205         END IF;
206 
207         SELECT instance_party_id
208         INTO   l_instance_party_id
209         FROM   csi_i_parties
210         WHERE  instance_id = l_ship_tbl(s_ind).instance_id
211         AND    relationship_type_code = 'OWNER';
212 
213         BEGIN
214           SELECT ip_account_id
215           INTO   l_ip_account_id
216           FROM   csi_ip_accounts
217           WHERE  instance_party_id      = l_instance_party_id
218           AND    relationship_type_code = 'OWNER';
219         EXCEPTION
220           WHEN no_data_found THEN
221             l_ip_account_id := fnd_api.g_miss_num;
222         END;
223 
224         l_satisfied := FALSE;
225 
226         FOR q_ind IN 1..l_end_loop
227         LOOP
228 
229           IF p_serial_code = 1 AND NOT(p_trackable_parent)
230              AND
231              nvl(p_order_line_rec.link_to_line_id, fnd_api.g_miss_num) <> fnd_api.g_miss_num
232           THEN
233             l_total_qty := l_total_qty + l_quantity;
234 
235             IF l_total_qty = l_ship_tbl(s_ind).shipped_quantity THEN
236               l_satisfied := TRUE;
237             ELSIF l_total_qty > l_ship_tbl(s_ind).shipped_quantity THEN
238               l_quantity := l_ship_tbl(s_ind).shipped_quantity - (l_quantity*(q_ind-1));
239               l_satisfied := TRUE;
240             END IF;
241           END IF;
242 
243           l_c_ind := l_c_ind + 1;
244 
245           -- assign values for the columns in txn_line_details_tbl
246           l_c_tld_tbl(l_c_ind).instance_id             := l_ship_tbl(s_ind).instance_id;
247           l_c_tld_tbl(l_c_ind).instance_exists_flag    := 'Y';
248           l_c_tld_tbl(l_c_ind).source_transaction_flag := 'Y';
249           l_c_tld_tbl(l_c_ind).sub_type_id             := g_dflt_sub_type_id ;
250           l_c_tld_tbl(l_c_ind).inventory_item_id       := l_ship_tbl(s_ind).inventory_item_id;
251           l_c_tld_tbl(l_c_ind).inv_organization_id     := l_ship_tbl(s_ind).organization_id;
252           l_c_tld_tbl(l_c_ind).inventory_revision      := l_ship_tbl(s_ind).revision;
253           l_c_tld_tbl(l_c_ind).item_condition_id       := fnd_api.g_miss_num;
254           l_c_tld_tbl(l_c_ind).instance_type_code      := fnd_api.g_miss_char;
255           l_c_tld_tbl(l_c_ind).unit_of_measure         := l_ship_tbl(s_ind).transaction_uom;
256           l_c_tld_tbl(l_c_ind).serial_number           := l_ship_tbl(s_ind).serial_number;
257           l_c_tld_tbl(l_c_ind).quantity                := l_quantity;
258           l_c_tld_tbl(l_c_ind).lot_number              := l_ship_tbl(s_ind).lot_number;
259           l_c_tld_tbl(l_c_ind).location_type_code      := 'HZ_PARTY_SITES';
260           l_c_tld_tbl(l_c_ind).location_id             := p_order_line_rec.ship_to_party_site_id;
261           l_c_tld_tbl(l_c_ind).sellable_flag           := 'Y';
262           l_c_tld_tbl(l_c_ind).active_start_date       := sysdate;
263           l_c_tld_tbl(l_c_ind).object_version_number   := 1;
264           l_c_tld_tbl(l_c_ind).preserve_detail_flag    := 'Y';
265           l_c_tld_tbl(l_c_ind).processing_status       := 'IN_PROCESS';
266 
267           IF p_serial_code <> 1 THEN
268             l_c_tld_tbl(l_c_ind).mfg_serial_number_flag  := 'Y';
269           ELSE
270             l_c_tld_tbl(l_c_ind).mfg_serial_number_flag  := 'N';
271           END IF;
272 
273           -- assign party record values
274           l_c_t_pty_tbl(l_c_ind).instance_party_id      := l_instance_party_id;
275           l_c_t_pty_tbl(l_c_ind).party_source_id        := l_ship_tbl(s_ind).party_id;
276           l_c_t_pty_tbl(l_c_ind).party_source_table     := 'HZ_PARTIES';
277           l_c_t_pty_tbl(l_c_ind).relationship_type_code := 'OWNER';
278           l_c_t_pty_tbl(l_c_ind).contact_flag           := 'N';
279           l_c_t_pty_tbl(l_c_ind).active_start_date      := sysdate;
280           l_c_t_pty_tbl(l_c_ind).preserve_detail_flag   := 'Y';
281           l_c_t_pty_tbl(l_c_ind).object_version_number  := 1;
282           l_c_t_pty_tbl(l_c_ind).txn_line_details_index := l_c_ind;
283 
284           -- assign party account values
285           l_c_t_pty_acct_tbl(l_c_ind).ip_account_id           := l_ip_account_id;
286           l_c_t_pty_acct_tbl(l_c_ind).account_id              := l_ship_tbl(s_ind).party_account_id;
287           l_c_t_pty_acct_tbl(l_c_ind).bill_to_address_id      := l_ship_tbl(s_ind).invoice_to_org_id;
288           l_c_t_pty_acct_tbl(l_c_ind).ship_to_address_id      := l_ship_tbl(s_ind).ship_to_org_id;
289           l_c_t_pty_acct_tbl(l_c_ind).relationship_type_code  := 'OWNER';
290           l_c_t_pty_acct_tbl(l_c_ind).active_start_date       := sysdate;
291           l_c_t_pty_acct_tbl(l_c_ind).preserve_detail_flag    := 'Y';
292           l_c_t_pty_acct_tbl(l_c_ind).object_version_number   := 1;
293           l_c_t_pty_acct_tbl(l_c_ind).txn_party_details_index := l_c_ind;
294 
295           -- assign org assignment values
296           IF nvl(l_ship_tbl(s_ind).sold_from_org_id, fnd_api.g_miss_num) <> fnd_api.g_miss_num
297           THEN
298 
299             l_c_t_oa_tbl(l_c_ind).txn_operating_unit_id  := fnd_api.g_miss_num;
300             l_c_t_oa_tbl(l_c_ind).txn_line_detail_id     := fnd_api.g_miss_num;
301             l_c_t_oa_tbl(l_c_ind).instance_ou_id         := fnd_api.g_miss_num;
302             l_c_t_oa_tbl(l_c_ind).operating_unit_id      := l_ship_tbl(s_ind).sold_from_org_id;
303             l_c_t_oa_tbl(l_c_ind).relationship_type_code := 'SOLD_FROM';
304             l_c_t_oa_tbl(l_c_ind).active_start_date      := sysdate;
305             l_c_t_oa_tbl(l_c_ind).preserve_detail_flag   := 'Y';
306             l_c_t_oa_tbl(l_c_ind).txn_line_details_index := l_c_ind;
307             l_c_t_oa_tbl(l_c_ind).object_version_number  := 1;
308           END IF;
309 
310           -- assign the txn details in the shipping pl/sql table
311           l_ship_tbl(s_ind).txn_dtls_qty   := l_c_tld_tbl(l_c_ind).quantity;
312           l_ship_tbl(s_ind).instance_match := 'Y';
313           l_ship_tbl(s_ind).quantity_match := 'Y';
314 
315           IF l_satisfied THEN
316             exit;
317           END IF;
318 
319         END LOOP;
320       END LOOP;
321 
322       debug('  l_c_tld_tbl.count        :'||l_c_tld_tbl.count);
323       debug('  l_c_t_pty_tbl.count      :'||l_c_t_pty_tbl.count);
324       debug('  l_c_t_pty_acct_tbl.count :'||l_c_t_pty_acct_tbl.count);
325       debug('  l_c_t_iir_tbl.count      :'||l_c_t_iir_tbl.count);
326       debug('  l_c_t_oa_tbl.count       :'||l_c_t_oa_tbl.count);
327       debug('  l_c_t_ea_tbl.count       :'||l_c_t_ea_tbl.count);
328       debug('  l_c_t_sys_tbl.count      :'||l_c_t_sys_tbl.count);
329 
330       csi_t_txn_details_grp.create_transaction_dtls(
331         p_api_version              => 1.0,
332         p_commit                   => fnd_api.g_false,
333         p_init_msg_list            => fnd_api.g_true,
334         p_validation_level         => fnd_api.g_valid_level_none,
335         px_txn_line_rec            => l_c_tl_rec,
336         px_txn_line_detail_tbl     => l_c_tld_tbl,
337         px_txn_party_detail_tbl    => l_c_t_pty_tbl,
338         px_txn_pty_acct_detail_tbl => l_c_t_pty_acct_tbl,
339         px_txn_ii_rltns_tbl        => l_c_t_iir_tbl,
340         px_txn_org_assgn_tbl       => l_c_t_oa_tbl,
341         px_txn_ext_attrib_vals_tbl => l_c_t_ea_tbl,
342         px_txn_systems_tbl         => l_c_t_sys_tbl,
343         x_return_status            => l_return_status,
344         x_msg_count                => l_msg_count,
345         x_msg_data                 => l_msg_data);
346 
347       IF l_return_status <> fnd_api.g_ret_sts_success THEN
348         RAISE fnd_api.g_exc_error;
349       END IF;
350 
351       x_transaction_line_id := l_c_tl_rec.transaction_line_id;
352 
353     END IF;
354 
355   EXCEPTION
356     WHEN fnd_api.g_exc_error THEN
357       x_return_status := fnd_api.g_ret_sts_error;
358   END create_dflt_td_from_ship;
359 
360 
361   /* sync tld quantities with sales order quantity */
362   PROCEDURE sync_tld_and_order(
363     p_order_line_rec          IN order_line_rec,
364     x_return_status           OUT NOCOPY varchar2)
365   IS
366     l_transaction_line_id     number;
367     l_total_tld_quantity      number;
368     l_diff_quantity           number;
369 
370     l_src_change_owner_to_code varchar2(1);
371     l_src_change_owner         varchar2(1);
372 
373     l_c_tld_rec               csi_t_datastructures_grp.txn_line_detail_rec ;
374     l_c_tpd_tbl               csi_t_datastructures_grp.txn_party_detail_tbl ;
375     l_c_tpad_tbl              csi_t_datastructures_grp.txn_pty_acct_detail_tbl;
376     l_c_toa_tbl               csi_t_datastructures_grp.txn_org_assgn_tbl;
377     l_c_teav_tbl              csi_t_datastructures_grp.txn_ext_attrib_vals_tbl;
378     l_c_ts_tbl                csi_t_datastructures_grp.txn_systems_tbl;
379     l_c_tiir_tbl              csi_t_datastructures_grp.txn_ii_rltns_tbl;
380 
381     l_return_status           varchar2(1) := fnd_api.g_ret_sts_success;
382     l_msg_count               number;
383     l_msg_data                varchar2(2000);
384 
385   BEGIN
386 
387     x_return_status := fnd_api.g_ret_sts_success;
388 
389     BEGIN
390 
391       SELECT transaction_line_id
392       INTO   l_transaction_line_id
393       FROM   csi_t_transaction_lines
394       WHERE  source_transaction_table = 'OE_ORDER_LINES_ALL'
395       AND    source_transaction_id    = p_order_line_rec.order_line_id;
396 
397       SELECT sum(abs(quantity))
398       INTO   l_total_tld_quantity
399       FROM   csi_t_txn_line_details
400       WHERE  transaction_line_id     = l_transaction_line_id
401       AND    source_transaction_flag = 'Y';
402 
403       IF l_total_tld_quantity < p_order_line_rec.ordered_quantity THEN
404 
405         -- now sync the dif
406         l_diff_quantity :=  p_order_line_rec.ordered_quantity - l_total_tld_quantity;
407         debug('diff quantity :'|| l_diff_quantity);
408 
409         l_c_tld_rec.source_transaction_flag := 'Y';
410         l_c_tld_rec.instance_exists_flag    := 'N';
411         l_c_tld_rec.sub_type_id             := g_dflt_sub_type_id;
412         l_c_tld_rec.transaction_line_id     := l_transaction_line_id;
413         l_c_tld_rec.txn_line_detail_id      := fnd_api.g_miss_num;
414         l_c_tld_rec.inventory_item_id       := p_order_line_rec.inv_item_id;
415         l_c_tld_rec.inv_organization_id     := p_order_line_rec.inv_org_id;
416         l_c_tld_rec.quantity                := l_diff_quantity;
417         l_c_tld_rec.unit_of_measure         := p_order_line_rec.order_quantity_uom;
418         l_c_tld_rec.object_version_number   := 1;
419         l_c_tld_rec.processing_status       := 'SUBMIT';
420 
421         SELECT src_change_owner,
422                src_change_owner_to_code
423         INTO   l_src_change_owner,
424                l_src_change_owner_to_code
425         FROM   csi_ib_txn_types
426         WHERE  sub_type_id = l_c_tld_rec.sub_type_id;
427 
428         IF l_src_change_owner = 'Y' AND l_src_change_owner_to_code = 'E' THEN
429 
430           l_c_tpd_tbl(1).txn_party_detail_id      := fnd_api.g_miss_num;
431 
432           SELECT party_id
433           INTO   l_c_tpd_tbl(1).party_source_id
434           FROM   hz_cust_accounts
435           WHERE  cust_account_id = p_order_line_rec.customer_id;
436 
437           l_c_tpd_tbl(1).party_source_table       := 'HZ_PARTIES';
438           l_c_tpd_tbl(1).relationship_type_code   := 'OWNER';
439           l_c_tpd_tbl(1).contact_flag             := 'N';
440           l_c_tpd_tbl(1).active_start_date        := sysdate;
441           l_c_tpd_tbl(1).object_version_number    := 1;
442           l_c_tpd_tbl(1).txn_line_details_index   := 1;
443 
444 
445           l_c_tpad_tbl(1).txn_account_detail_id   := fnd_api.g_miss_num;
446           l_c_tpad_tbl(1).account_id              := p_order_line_rec.customer_id;
447           l_c_tpad_tbl(1).relationship_type_code  := 'OWNER';
448           l_c_tpad_tbl(1).active_start_date       := sysdate;
449           l_c_tpad_tbl(1).object_version_number   := 1;
450           l_c_tpad_tbl(1).txn_party_details_index := 1;
451 
452         END IF;
453 
454         -- call api to create the transaction line details
455         csi_t_txn_line_dtls_pvt.create_txn_line_dtls(
456           p_api_version              => 1.0 ,
457           p_commit                   => fnd_api.g_false,
458           p_init_msg_list            => fnd_api.g_true,
459           p_validation_level         => fnd_api.g_valid_level_none,
460           p_txn_line_dtl_index       => 1,
461           p_txn_line_dtl_rec         => l_c_tld_rec,
462           px_txn_party_dtl_tbl       => l_c_tpd_tbl,
463           px_txn_pty_acct_detail_tbl => l_c_tpad_tbl,
464           px_txn_ii_rltns_tbl        => l_c_tiir_tbl,
465           px_txn_org_assgn_tbl       => l_c_toa_tbl,
466           px_txn_ext_attrib_vals_tbl => l_c_teav_tbl,
467           x_return_status            => l_return_status,
468           x_msg_count                => l_msg_count,
469           x_msg_data                 => l_msg_data);
470 
471         IF l_return_status <> fnd_api.g_ret_sts_success THEN
472           RAISE fnd_api.g_exc_error;
473         END IF;
474 
475       END IF;
476 
477     EXCEPTION
478       WHEN no_data_found THEN
479         null;
480     END;
481 
482   EXCEPTION
483     WHEN fnd_api.g_exc_error THEN
484       x_return_status := fnd_api.g_ret_sts_error;
485   END sync_tld_and_order;
486 
487   /*--------------------------------------------------------------------*/
488   /* This is the main procedure to interface the order and installation */
489   /* details info in to oracle Install Base                             */
490   /*--------------------------------------------------------------------*/
491   PROCEDURE order_shipment(
492     p_mtl_transaction_id   IN            number,
493     p_message_id           IN            number,
494     x_return_status           OUT NOCOPY varchar2,
495     px_trx_error_rec       IN OUT NOCOPY csi_datastructures_pub.transaction_error_rec)
496   IS
497 
498     l_trx_detail_exist        boolean := FALSE;
499     l_copy_trx_detail_exist   boolean := FALSE;
500 
501     l_partial_ship            boolean := FALSE;
502     l_qty_ratio               number;
503     l_split_ord_line_id       number;
504     l_trx_type_id             number;
505     l_internal_party_id       number;
506     l_trx_line_id             number;
507     x_msg_count               number;
508     x_msg_data                varchar2(2000);
509 
510     x_order_shipment_tbl      order_shipment_tbl;
511     x_txn_line_query_rec      csi_t_datastructures_grp.txn_line_query_rec  ;
512     x_pricing_attb_tbl        csi_datastructures_pub.pricing_attribs_tbl;
513 
514     l_txn_line_query_rec      csi_t_datastructures_grp.txn_line_query_rec;
515     l_txn_line_dtl_query_rec  csi_t_datastructures_grp.txn_line_detail_query_rec;
516     l_txn_line_rec            csi_t_datastructures_grp.txn_line_rec;
517     l_transaction_line_rec    csi_t_datastructures_grp.txn_line_rec;
518     l_tmp_txn_line_rec        csi_t_datastructures_grp.txn_line_rec;
519     l_order_line_rec          order_line_rec;
520     l_trk_oe_line_tbl         oe_order_pub.line_tbl_type;
521     l_trackable_parent        boolean := FALSE;
522 
523     l_txn_line_detail_tbl     csi_t_datastructures_grp.txn_line_detail_tbl;
524     l_txn_party_detail_tbl    csi_t_datastructures_grp.txn_party_detail_tbl;
525     l_txn_pty_acct_dtl_tbl    csi_t_datastructures_grp.txn_pty_acct_detail_tbl;
526     l_txn_ii_rltns_tbl        csi_t_datastructures_grp.txn_ii_rltns_tbl;
527     l_txn_ext_attrib_vals_tbl csi_t_datastructures_grp.txn_ext_attrib_vals_tbl;
528     l_csi_ext_attribs_tbl     csi_t_datastructures_grp.csi_ext_attribs_tbl;
529     l_csi_iea_values_tbl      csi_t_datastructures_grp.csi_ext_attrib_vals_tbl;
530     l_txn_org_assgn_tbl       csi_t_datastructures_grp.txn_org_assgn_tbl  ;
531     l_txn_systems_tbl         csi_t_datastructures_grp.txn_systems_tbl;
532 
533     l_src_txn_line_rec        csi_t_datastructures_grp.txn_line_rec;
534     l_new_txn_line_rec        csi_t_datastructures_grp.txn_line_rec;
535     l_trx_rec                 csi_datastructures_pub.transaction_rec;
536 
537     l_copy_txn_line_rec       csi_t_datastructures_grp.txn_line_rec;
538 
539     x_trx_sub_type_rec        txn_sub_type_rec;
540     x_txn_ii_rltns_tbl        csi_t_datastructures_grp.txn_ii_rltns_tbl;
541     x_model_inst_tbl          model_inst_tbl;
542 
543     l_item_control_rec        csi_order_ship_pub.item_control_rec;
544     l_return_status           varchar2(1) := fnd_api.g_ret_sts_success;
545     l_msg_count               number;
546     l_msg_data                varchar2(2000);
547     l_temp_txn_ii_rltns_tbl   csi_t_datastructures_grp.txn_ii_rltns_tbl;
548     l_t_ii_indx               NUMBER := 0;
549 
550     l_error_rec               csi_datastructures_pub.transaction_error_rec;
551     l_parent_line_qty         NUMBER := fnd_api.g_miss_num;
552     l_literal1   	      VARCHAR2(30) ;
553     l_literal2   	      VARCHAR2(30) ;
554     l_source_header_rec       csi_interface_pkg.source_header_rec;
555     l_source_line_rec         csi_interface_pkg.source_line_rec;
556     l_conv_to_prim_uom_req    VARCHAR2(1) := 'Y';
557     --Added for bug 5194812--
558     l_om_session_key            csi_utility_grp.config_session_key;
559     l_macd_processing           BOOLEAN     := FALSE;
560     l_party_site_id          NUMBER;
561     l_party_install_site_id NUMBER;
562 
563 
564   BEGIN
565 
566     /* Standard Start of API savepoint */
567     savepoint  order_shipment;
568 
569     /* Initialize API return status to success */
570     x_return_status := fnd_api.g_ret_sts_success;
571     l_error_rec     := px_trx_error_rec;
572 
573     csi_t_gen_utility_pvt.build_file_name (
574       p_file_segment1 => 'csisoshp',
575       p_file_segment2 => p_mtl_transaction_id);
576 
577     api_log('order_shipment');
578 
579     debug('  Transaction Time   : '||to_char(sysdate, 'MM/DD/YY HH24:MI:SS'));
580     debug('  Transaction Type   : Sales Order Shipment');
581     debug('  Transaction ID     : '||p_mtl_transaction_id);
582 
583     l_error_rec.source_id     := p_mtl_transaction_id;
584 
585     /* this routine checks if ib is active */
586     csi_utility_grp.check_ib_active;
587 
588     /* get internal party id */
589     csi_utl_pkg.get_int_party(
590       x_int_party_id  => l_internal_party_id,
591       x_return_status => l_return_status);
592 
593     IF l_return_status <> fnd_api.g_ret_sts_success THEN
594       raise fnd_api.g_exc_error;
595     END IF;
596 
597     -- Get the order line details
598     csi_utl_pkg.get_order_line_dtls(
599       p_mtl_transaction_id => p_mtl_transaction_id,
600       x_order_line_rec     => l_order_line_rec,
601       x_return_status      => l_return_status);
602 
603     IF l_return_status <> fnd_api.g_ret_sts_success THEN
604       raise fnd_api.g_exc_error;
605     END IF;
606 
607     -- Bug 10100285/Bug 10113627
608     -- Convert ib_current_loc_id and ib_install_loc_id
609     -- which contains party site use id to party site id
610     -- The party site id will be later assigned to
611     -- instance locaation id and install location id resp
612 
613     IF NVL(l_order_line_rec.ib_current_loc_id, fnd_api.g_miss_num) <> fnd_api.g_miss_num  THEN
614     BEGIN
615 
616       SELECT party_site_id
617       INTO   l_party_site_id
618       FROM   hz_cust_acct_sites_all c,
619            hz_cust_site_uses_all u
620       WHERE  c.cust_acct_site_id = u.cust_acct_site_id
621       AND    u.site_use_id = l_order_line_rec.ib_current_loc_id;
622 
623       -- Re-assigning party site id fetched from the above block
624       l_order_line_rec.ib_current_loc_id := l_party_site_id;
625 
626       EXCEPTION
627         WHEN no_data_found then
628           fnd_message.set_name('CSI','CSI_INT_PTY_SITE_MISSING');
629           fnd_message.set_token('LOCATION_ID', l_order_line_rec.ib_current_loc_id);
630           fnd_msg_pub.add;
631           raise fnd_api.g_exc_error;
632         WHEN too_many_rows then
633           debug('Many Party sites found');
634           raise fnd_api.g_exc_error;
635       END;
636     END IF;
637 
638     IF NVL(l_order_line_rec.ib_install_loc_id, fnd_api.g_miss_num) <> fnd_api.g_miss_num THEN
639       BEGIN
640        SELECT party_site_id
641       INTO   l_party_install_site_id
642       FROM   hz_cust_acct_sites_all c,
643              hz_cust_site_uses_all u
644       WHERE  c.cust_acct_site_id = u.cust_acct_site_id
645       AND    u.site_use_id = l_order_line_rec.ib_install_loc_id;
646 
647       l_order_line_rec.ib_install_loc_id := l_party_install_site_id;
648 
649       EXCEPTION
650         WHEN no_data_found then
651             fnd_message.set_name('CSI','CSI_INT_PTY_SITE_MISSING');
652             fnd_message.set_token('LOCATION_ID',l_order_line_rec.ib_install_loc_id);
653             fnd_msg_pub.add;
654             raise fnd_api.g_exc_error;
655         WHEN too_many_rows then
656             debug('Many Party sites found');
657             raise fnd_api.g_exc_error;
658       END;
659      END IF;
660     -- End Bug 10100285/Bug 10113627
661 
662 --Code start for bug 5194812--
663       l_om_session_key.session_hdr_id  := l_order_line_rec.config_header_id;
664       l_om_session_key.session_rev_num := l_order_line_rec.config_rev_nbr;
665       l_om_session_key.session_item_id := l_order_line_rec.configuration_id;
666       --
667       l_macd_processing := csi_interface_pkg.check_macd_processing
668                                ( p_config_session_key => l_om_session_key,
669                                  x_return_status      => l_return_status
670                                );
671 --Code end for bug5194812--
672 
673     IF l_order_line_rec.mtl_action_id = 1 and l_order_line_rec.mtl_src_type_id = 8 THEN
674       g_txn_type_id := 126;
675     ELSE
676       --- Added for bug 5194812
677       IF l_macd_processing THEN
678         g_txn_type_id := 401;
679       ELSE
680         g_txn_type_id := 51;
681       END IF;
682       --- Code end for bug 5194812
683     END IF;
684 
685 
686     dbms_application_info.set_client_info(l_order_line_rec.org_id);
687 
688     csi_utl_pkg.get_item_control_rec(
689       p_mtl_txn_id        => p_mtl_transaction_id,
690       x_item_control_rec  => l_item_control_rec,
691       x_return_status     => l_return_status);
692 
693     IF l_return_status <> fnd_api.g_ret_sts_success THEN
694       RAISE fnd_api.g_exc_error;
695     END IF;
696 
697     -- assign the values to l_order_line_rec
698     l_order_line_rec.inv_mtl_transaction_id := p_mtl_transaction_id;
699     l_order_line_rec.internal_party_id      := l_internal_party_id;
700     l_order_line_rec.serial_code            := l_item_control_rec.serial_control_code;
701     l_order_line_rec.reservable_type        := l_item_control_rec.reservable_type;
702     l_order_line_rec.negative_balances_code := l_item_control_rec.negative_balances_code;
703     l_order_line_rec.bom_item_type          := l_item_control_rec.bom_item_type;
704     l_order_line_rec.primary_uom            := l_item_control_rec.primary_uom_code;
705 
706     l_error_rec.source_header_ref        := l_order_line_rec.order_number;
707     l_error_rec.source_header_ref_id     := l_order_line_rec.header_id;
708     l_error_rec.source_line_ref          := l_order_line_rec.line_number;
709     l_error_rec.source_line_ref_id       := l_order_line_rec.order_line_id;
710     l_error_rec.inventory_item_id        := l_item_control_rec.inventory_item_id;
711     l_error_rec.src_serial_num_ctrl_code := l_item_control_rec.serial_control_code;
712     l_error_rec.src_lot_ctrl_code        := l_item_control_rec.lot_control_code;
713     l_error_rec.src_location_ctrl_code   := l_item_control_rec.locator_control_code;
714     l_error_rec.src_rev_qty_ctrl_code    := l_item_control_rec.revision_control_code;
715     l_error_rec.comms_nl_trackable_flag  := l_item_control_rec.ib_trackable_flag;
716 
717     IF NVL(l_order_line_rec.link_to_line_id,fnd_api.g_miss_num) <> fnd_api.g_miss_num THEN
718 
719       csi_utl_pkg.get_link_to_line_id (
720         x_link_to_line_id  => l_order_line_rec.link_to_line_id,
721         x_return_status    => l_return_status );
722 
723       IF l_return_status <> fnd_api.g_ret_sts_success THEN
724         raise fnd_api.g_exc_error;
725       END IF;
726 
727     END IF;
728 
729     -- check if there is any ib trackable children for the current order line
730     csi_utl_pkg.get_ib_trackable_children(
731       p_order_line_rec     => l_order_line_rec,
732       x_trackable_line_tbl => l_trk_oe_line_tbl,
733       x_return_status      => l_return_status);
734 
735     IF l_return_status <> fnd_api.g_ret_sts_success THEN
736       RAISE fnd_api.g_exc_error;
737     END IF;
738 
739     IF l_trk_oe_line_tbl.COUNT > 0 THEN
740       l_trackable_parent := TRUE;
741     END IF;
742 
743     /* getting the default sub type id */
744     csi_utl_pkg.get_dflt_sub_type_id(
745       p_transaction_type_id  => g_txn_type_id,
746       x_sub_type_id          => g_dflt_sub_type_id,
747       x_return_status        => l_return_status);
748 
749     IF l_return_status <> fnd_api.g_ret_sts_success THEN
750       raise fnd_api.g_exc_error;
751     END IF;
752 
753     csi_utl_pkg.get_sub_type_rec(
754       p_sub_type_id        => g_dflt_sub_type_id,
755       p_trx_type_id        => g_txn_type_id,
756       x_trx_sub_type_rec   => x_trx_sub_type_rec,
757       x_return_status      => l_return_status) ;
758 
759     IF l_return_status <> fnd_api.g_ret_sts_success THEN
760       raise fnd_api.g_exc_error;
761     END IF;
762 
763     debug('  transaction_type_id: '||x_trx_sub_type_rec.trx_type_id );
764     debug('  default sub_type_id: '||x_trx_sub_type_rec.sub_type_id );
765 
766     /* assign the parameter for txn_line_rec */
767     l_txn_line_rec.source_transaction_id       :=  l_order_line_rec.order_line_id;
768     l_txn_line_rec.source_transaction_table    :=  'OE_ORDER_LINES_ALL';
769 
770     -- Check if txn details exist
771     l_trx_detail_exist := csi_t_txn_details_grp.check_txn_details_exist(
772                             p_txn_line_rec  => l_txn_line_rec );
773 
774     IF l_trx_detail_exist THEN
775 
776       debug('user entered installation details found.' );
777 
778       --sync source tld quantites with order line quantity
779       sync_tld_and_order(
780         p_order_line_rec => l_order_line_rec,
781         x_return_status  => l_return_status);
782 
783       IF l_return_status <> fnd_api.g_ret_sts_success THEN
784         RAISE fnd_api.g_exc_error;
785       END IF;
786 
787       /*------------------------------------------------*/
788       /* If the order line has a parent then create txn */
789       /* line dtls in the parent/child order qty ratio  */
790       /* -----------------------------------------------*/
791 
792       IF NVL(l_order_line_rec.link_to_line_id,fnd_api.g_miss_num) <> fnd_api.g_miss_num THEN
793 
794         debug('This is a child order line. Check model and cascade installation details.');
795 
796         csi_utl_pkg.get_qty_ratio(
797           p_order_line_qty   => l_order_line_rec.ordered_quantity,
798 	  p_order_item_id    => l_order_line_rec.inv_item_id,
799 	  p_model_remnant_flag => l_order_line_rec.model_remnant_flag, --added for bug5096435
800           p_link_to_line_id  => l_order_line_rec.link_to_line_id,
801           x_qty_ratio        => l_qty_ratio ,
802           x_return_status    => l_return_status );
803 
804         IF x_return_status <> fnd_api.g_ret_sts_success THEN
805           raise fnd_api.g_exc_error;
806         END IF;
807 
808         csi_utl_pkg.cascade_txn_dtls(
809           p_source_trx_id    => l_order_line_rec.order_line_id,
810           p_source_trx_table => 'OE_ORDER_LINES_ALL',
811           p_ratio            => l_qty_ratio,
812           x_return_status    => x_return_status);
813 
814         IF x_return_status <> fnd_api.g_ret_sts_success THEN
815           raise fnd_api.g_exc_error;
816         END IF;
817 
818       END IF;
819 
820 
821       l_copy_txn_line_rec.source_transaction_id    :=  l_order_line_rec.order_line_id;
822       l_copy_txn_line_rec.source_transaction_table :=  'WSH_DELIVERY_DETAILS';
823 
824       debug('Concurrency check - Is installation details copied already?' );
825 
826       -- Check if txn details exist
827       l_copy_trx_detail_exist := csi_t_txn_details_grp.check_txn_details_exist(
828                                    p_txn_line_rec  => l_copy_txn_line_rec );
829 
830       IF l_copy_trx_detail_exist THEN
831 
832         debug('  Yes it is.' );
833 
834         BEGIN
835           SELECT transaction_line_id
836           INTO   l_trx_line_id
837           FROM   csi_t_transaction_lines
838           WHERE  source_transaction_id    = l_copy_txn_line_rec.source_transaction_id
839           AND    source_transaction_table = l_copy_txn_line_rec.source_transaction_table;
840 
841         EXCEPTION
842           WHEN no_data_found THEN
843             fnd_message.set_name('CSI','CSI_INT_TRX_LINE_MISSING');
844             fnd_message.set_token('SOURCE_TRANSACTION_ID', l_copy_txn_line_rec.SOURCE_TRANSACTION_ID);
845             fnd_message.set_token('SOURCE_TRANSACTION_TABLE',l_copy_txn_line_rec.SOURCE_TRANSACTION_TABLE);
846             fnd_msg_pub.add;
847             raise fnd_api.g_exc_error;
848         END;
849 
850       ELSE
851 
852         debug('  No it is not. Copying installation details as WSH_DELIVERY_DETAILS.');
853 
854         --Parameters for the source order line
855         l_src_txn_line_rec.source_transaction_id      := l_order_line_rec.order_line_id;
856         l_src_txn_line_rec.source_transaction_table   := 'OE_ORDER_LINES_ALL';
857 
858         --Parameters for the new source trx rec
859         l_new_txn_line_rec.source_transaction_id      := l_order_line_rec.order_line_id;
860         l_new_txn_line_rec.source_transaction_table   := 'WSH_DELIVERY_DETAILS';
861         l_new_txn_line_rec.source_transaction_type_id := g_txn_type_id;
862 
863 
864         ---Added (Start) for m-to-m enhancements
865         l_new_txn_line_rec.source_txn_header_id := l_order_line_rec.header_id ;
866         ---Added (End) for m-to-m enhancements
867 
868         csi_t_txn_details_pvt.copy_transaction_dtls(
869           p_api_version           => 1.0,
870           p_commit                => fnd_api.g_false,
871           p_init_msg_list         => fnd_api.g_true,
872           p_validation_level      => fnd_api.g_valid_level_none,
873           p_src_txn_line_rec      => l_src_txn_line_rec,
874           px_new_txn_line_rec     => l_new_txn_line_rec,
875           p_copy_parties_flag     => fnd_api.g_true,
876           p_copy_pty_accts_flag   => fnd_api.g_true,
877           p_copy_ii_rltns_flag    => fnd_api.g_true,
878           p_copy_org_assgn_flag   => fnd_api.g_true,
879           p_copy_ext_attribs_flag => fnd_api.g_true,
880           p_copy_txn_systems_flag => fnd_api.g_true,
881           x_return_status         => x_return_status,
882           x_msg_count             => x_msg_count,
883           x_msg_data              => x_msg_data);
884 
885         IF x_return_status <> fnd_api.g_ret_sts_success THEN
886           g_api_name := 'csi_t_txn_details_pvt.copy_transaction_dtls';
887           raise fnd_api.g_exc_error;
888         END IF;
889 
890         l_trx_line_id :=  l_new_txn_line_rec.transaction_line_id;
891 
892         ---Added (Start) for m-to-m enhancements
893         ---Now split the copied TLD's to one each.
894         IF l_item_control_rec.serial_control_code <> 1  OR l_trackable_parent THEN
895 
896           debug('splitting the WSH txn details in to one each.' );
897 
898           -- shegde 2769321 oe_orderlines_all changed to wsh_delivery_details
899           csi_utl_pkg.create_txn_dtls(
900             p_source_trx_id    =>  l_order_line_rec.order_line_id,
901             p_source_trx_table =>  'WSH_DELIVERY_DETAILS',
902             x_return_status    =>  x_return_status );
903 
904           IF x_return_status <> fnd_api.g_ret_sts_success THEN
905             raise fnd_api.g_exc_error;
906           END IF;
907 
908         END IF;
909         ---Added (End) for m-to-m enhancements
910 
911       END IF;
912 
913     ELSE -- installation details not entered
914 
915       debug('user entered installation details not found');
916 
917       /*----------------------------------------------------------------*/
918       /* In case of option and config items, call cascade_model that    */
919       /* will cascade the txn_line_details to option/ config lines      */
920       /* from the txn line details entered for the top model            */
921       /*----------------------------------------------------------------*/
922 
923       IF NVL(l_order_line_rec.top_model_line_id,fnd_api.g_miss_num) <> fnd_api.g_miss_num THEN
924 
925         debug('component of a pto/ato model. cascade installation details from top model.');
926 
927         csi_t_utilities_pvt.cascade_model(
928           p_model_line_id    => l_order_line_rec.top_model_line_id,
929           x_return_status    => l_return_status );
930 
931         IF l_return_status <> fnd_api.g_ret_sts_success THEN
932           raise fnd_api.g_exc_error;
933         END IF;
934 
935         l_txn_line_rec.source_transaction_id       :=  l_order_line_rec.order_line_id;
936         l_txn_line_rec.source_transaction_table    :=  'OE_ORDER_LINES_ALL';
937 
938         debug('check if installation details are cascaded from the model ?');
939 
940         -- Check if txn details exist
941         l_trx_detail_exist := csi_t_txn_details_grp.check_txn_details_exist(
942                                 p_txn_line_rec  => l_txn_line_rec );
943 
944         IF l_trx_detail_exist THEN
945 
946           debug('  yes. cascaded from top model.');
947 
948           /*------------------------------------------------*/
949           /* If the order line has a parent then create txn */
950           /* line dtls in the parent/child order qty ratio  */
951           /* -----------------------------------------------*/
952 
953           IF NVL(l_order_line_rec.link_to_line_id,fnd_api.g_miss_num) <> fnd_api.g_miss_num THEN
954 
955             csi_utl_pkg.get_qty_ratio(
956               p_order_line_qty   => l_order_line_rec.ordered_quantity,
957 	      p_order_item_id    => l_order_line_rec.inv_item_id,
958 	      p_model_remnant_flag => l_order_line_rec.model_remnant_flag, --added for bug5096435
959               p_link_to_line_id  => l_order_line_rec.link_to_line_id,
960               x_qty_ratio        => l_qty_ratio ,
961               x_return_status    => l_return_status );
962 
963             IF l_return_status <> fnd_api.g_ret_sts_success THEN
964               raise fnd_api.g_exc_error;
965             END IF;
966 
967             csi_utl_pkg.cascade_txn_dtls(
968               p_source_trx_id    => l_order_line_rec.order_line_id,
969               p_source_trx_table => 'OE_ORDER_LINES_ALL',
970               p_ratio            => l_qty_ratio ,
971               x_return_status    => l_return_status );
972 
973             IF l_return_status <> fnd_api.g_ret_sts_success THEN
974               raise fnd_api.g_exc_error;
975             END IF;
976 
977           END IF;--link_to_line_id <>fnd_api.g_miss_num
978 
979           /* assign the parameter for txn_line_rec */
980           l_copy_txn_line_rec.SOURCE_TRANSACTION_ID       :=  l_order_line_rec.order_line_id;
981           l_copy_txn_line_rec.SOURCE_TRANSACTION_TABLE    :=  'WSH_DELIVERY_DETAILS';
982 
983           debug('concurrency check - is installation details copied already?' );
984 
985           -- Check if txn details exist
986           l_copy_trx_detail_exist := csi_t_txn_details_grp.check_txn_details_exist(
987                                        p_txn_line_rec  => l_copy_txn_line_rec );
988 
989           IF l_copy_trx_detail_exist THEN
990             debug('  Yes it is.');
991             BEGIN
992               SELECT transaction_line_id
993               INTO   l_trx_line_id
994               FROM   csi_t_transaction_lines
995               WHERE  source_transaction_id    = l_copy_txn_line_rec.source_transaction_id
996               AND    source_transaction_table = l_copy_txn_line_rec.source_transaction_table;
997             EXCEPTION
998               WHEN no_data_found  THEN
999                 fnd_message.set_name('CSI','CSI_INT_TRX_LINE_MISSING');
1000                 fnd_message.set_token('SOURCE_TRANSACTION_ID',
1001                                        l_copy_txn_line_rec.SOURCE_TRANSACTION_ID);
1002                 fnd_message.set_token('SOURCE_TRANSACTION_TABLE',
1003                                        l_copy_txn_line_rec.SOURCE_TRANSACTION_TABLE);
1004                 fnd_msg_pub.add;
1005                 raise fnd_api.g_exc_error;
1006             END;
1007           ELSE
1008 
1009             debug('  No it is not. Copying installation details as WSH_DELIVERY_DETAILS.');
1010 
1011             --Parameters for the source order line
1012             l_src_txn_line_rec.source_transaction_id      := l_order_line_rec.order_line_id;
1013             l_src_txn_line_rec.source_transaction_table   := 'OE_ORDER_LINES_ALL';
1014 
1015             --Parameters for the new source trx rec
1016             l_new_txn_line_rec.source_transaction_id      := l_order_line_rec.order_line_id;
1017             l_new_txn_line_rec.source_transaction_table   := 'WSH_DELIVERY_DETAILS';
1018             l_new_txn_line_rec.source_transaction_type_id := g_txn_type_id;
1019 
1020             csi_t_txn_details_pvt.copy_transaction_dtls(
1021               p_api_version           => 1.0,
1022               p_commit                => fnd_api.g_false,
1023               p_init_msg_list         => fnd_api.g_true,
1024               p_validation_level      => fnd_api.g_valid_level_none,
1025               p_src_txn_line_rec      => l_src_txn_line_rec,
1026               px_new_txn_line_rec     => l_new_txn_line_rec,
1027               p_copy_parties_flag     => fnd_api.g_true,
1028               p_copy_pty_accts_flag   => fnd_api.g_true,
1029               p_copy_ii_rltns_flag    => fnd_api.g_true,
1030               p_copy_org_assgn_flag   => fnd_api.g_true,
1031               p_copy_ext_attribs_flag => fnd_api.g_true,
1032               p_copy_txn_systems_flag => fnd_api.g_true,
1033               x_return_status         => l_return_status,
1034               x_msg_count             => l_msg_count,
1035               x_msg_data              => l_msg_data);
1036 
1037             IF l_return_status <> fnd_api.g_ret_sts_success THEN
1038               g_api_name := 'csi_t_txn_details_pvt.copy_transaction_dtls';
1039               raise fnd_api.g_exc_error;
1040             END IF;
1041 
1042             l_trx_line_id :=  l_new_txn_line_rec.transaction_line_id;
1043 
1044             /* Copied the following code from IF condition above as part of fix for bug 3555078 */
1045             ---Added (Start) for m-to-m enhancements
1046             ---Now split the copied TLD's to one each.
1047             IF l_item_control_rec.serial_control_code <> 1  OR l_trackable_parent THEN
1048 
1049                debug('splitting the WSH txn details in to one each' );
1050                -- shegde 2769321 oe_orderlines_all changed to wsh_delivery_details
1051                csi_utl_pkg.create_txn_dtls(
1052                  p_source_trx_id    =>  l_order_line_rec.order_line_id,
1053                  p_source_trx_table =>  'WSH_DELIVERY_DETAILS',
1054                  x_return_status    =>  l_return_status );
1055 
1056                IF l_return_status <> fnd_api.g_ret_sts_success THEN
1057                  raise fnd_api.g_exc_error;
1058                END IF;
1059              END IF;
1060              ---Added (End) for m-to-m enhancements
1061             /* Copied the above code from IF condition above as part of fix for bug 3555078 */
1062 
1063           END IF ; -- if NOT(l_copy_trx_detail_exist)
1064         END IF; -- if l_trx_detail_exist for option and model
1065       END IF; -- if item_type_code in (config,option)
1066     END IF; -- installation details entered check
1067 
1068     /* get the splitted order line for partial shipment */
1069     IF l_trx_detail_exist then
1070 
1071       debug('check if this is a partial shipment case.');
1072 
1073       csi_utl_pkg.get_split_order_line(
1074         l_order_line_rec.order_line_id,
1075         l_order_line_rec.header_id,
1076         l_split_ord_line_id,
1077         l_return_status);
1078 
1079       IF l_return_status <> fnd_api.g_ret_sts_success THEN
1080         raise fnd_api.g_exc_error;
1081       END IF;
1082 
1083       IF l_split_ord_line_id is not null THEN
1084 
1085         l_partial_ship := TRUE;
1086 
1087         debug('  Yes. split_order_line_id :'||l_split_ord_line_id);
1088 
1089         l_order_line_rec.split_ord_line_id := l_split_ord_line_id;
1090 
1091         /* assign the parameter for txn_line_rec */
1092         l_copy_txn_line_rec.source_transaction_id    :=  l_split_ord_line_id;
1093         l_copy_txn_line_rec.source_transaction_table :=  'OE_ORDER_LINES_ALL';
1094 
1095         debug('Check if copied installation details exist for the split order line?' );
1096 
1097         l_copy_trx_detail_exist := csi_t_txn_details_grp.check_txn_details_exist(
1098                                      p_txn_line_rec  => l_copy_txn_line_rec );
1099 
1100         IF NOT(l_copy_trx_detail_exist) THEN
1101 
1102           /*------------------------------------------------------------------------*/
1103           /* Copy the txn details with the same source for the splitted order line  */
1104           /*------------------------------------------------------------------------*/
1105 
1106           --Parameters for the source transaction
1107           l_src_txn_line_rec.source_transaction_id     := l_order_line_rec.order_line_id;
1108           l_src_txn_line_rec.source_transaction_table  := 'OE_ORDER_LINES_ALL';
1109 
1110           --Parameters for the destination trx rec
1111           l_new_txn_line_rec.source_transaction_id      := l_split_ord_line_id;
1112           l_new_txn_line_rec.source_transaction_table   := 'OE_ORDER_LINES_ALL';
1113           l_new_txn_line_rec.source_transaction_type_id := g_txn_type_id;
1114           l_new_txn_line_rec.transaction_line_id        := fnd_api.g_miss_num;
1115 
1116           debug('  No. So copying installation details for the split order line.' );
1117 
1118           csi_t_txn_details_pvt.copy_transaction_dtls(
1119             p_api_version           => 1.0,
1120             p_commit                => fnd_api.g_false,
1121             p_init_msg_list         => fnd_api.g_true,
1122             p_validation_level      => fnd_api.g_valid_level_none,
1123             p_src_txn_line_rec      => l_src_txn_line_rec,
1124             px_new_txn_line_rec     => l_new_txn_line_rec,
1125             p_copy_parties_flag     => fnd_api.g_true,
1126             p_copy_pty_accts_flag   => fnd_api.g_true,
1127             p_copy_ii_rltns_flag    => fnd_api.g_true,
1128             p_copy_org_assgn_flag   => fnd_api.g_true,
1129             p_copy_ext_attribs_flag => fnd_api.g_true,
1130             p_copy_txn_systems_flag => fnd_api.g_true,
1131             x_return_status         => l_return_status,
1132             x_msg_count             => l_msg_count,
1133             x_msg_data              => l_msg_data);
1134 
1135           IF l_return_status <> fnd_api.g_ret_sts_success THEN
1136             g_api_name := 'csi_t_txn_details_pvt.copy_transaction_dtls';
1137             raise fnd_api.g_exc_error;
1138           END IF;
1139 
1140         END IF; -- installation details check
1141       ELSE
1142         l_partial_ship := FALSE;
1143         debug('  No.');
1144       END IF; -- split line id check
1145     END IF; -- end if l_trx_line_detail_exists
1146 
1147     l_order_line_rec.trx_line_id            := l_trx_line_id;
1148 
1149     --assign the values for the trx_rec
1150     l_trx_rec.source_header_ref_id        := l_order_line_rec.header_id;
1151     l_trx_rec.source_header_ref           := l_order_line_rec.order_number;
1152     l_trx_rec.source_line_ref_id          := l_order_line_rec.order_line_id;
1153     l_trx_rec.source_line_ref             := l_order_line_rec.line_number;
1154     l_trx_rec.inv_material_transaction_id := p_mtl_transaction_id;
1155     l_trx_rec.transaction_type_id         := g_txn_type_id  ;
1156     l_trx_rec.transaction_date            := l_order_line_rec.transaction_date ;
1157     l_trx_rec.source_transaction_date     := l_order_line_rec.transaction_date ;
1158     l_trx_rec.transaction_status_code     := 'PENDING';
1159 
1160     /* Create csi transaction and use this trx_rec for all  ib transactions */
1161     csi_transactions_pvt.create_transaction(
1162       p_api_version            => 1.0,
1163       p_commit                 => fnd_api.g_false,
1164       p_init_msg_list          => fnd_api.g_true,
1165       p_validation_level       => fnd_api.g_valid_level_full,
1166       p_success_if_exists_flag => 'Y',
1167       p_transaction_rec        => l_trx_rec,
1168       x_return_status          => l_return_status,
1169       x_msg_count              => l_msg_count,
1170       x_msg_data               => l_msg_data);
1171 
1172     IF l_return_status <> fnd_api.g_ret_sts_success THEN
1173       g_api_name := 'csi_transactions_pvt.create_transaction';
1174       raise fnd_api.g_exc_error;
1175     END IF;
1176 
1177     debug('csi transaction_id :'||l_trx_rec.transaction_id);
1178 
1179     /* -------------------------------------------------*/
1180     /* Match the txn details with shipment and          */
1181     /* build the relationship for Component OF          */
1182     /*--------------------------------------------------*/
1183 
1184     -- Included Transaction rec as part of fix for Bug 2767338
1185     build_shtd_table(
1186       p_mtl_transaction_id  =>  p_mtl_transaction_id,
1187       p_order_line_rec      =>  l_order_line_rec,
1188       p_txn_sub_type_rec    =>  x_trx_sub_type_rec,
1189       p_trx_detail_exist    =>  l_trx_detail_exist,
1190       p_transaction_rec     =>  l_trx_rec,
1191       p_trackable_parent    =>  l_trackable_parent,
1192       x_order_shipment_tbl  =>  x_order_shipment_tbl,
1193       px_error_rec          =>  l_error_rec,
1194       x_return_status       =>  l_return_status);
1195 
1196     IF l_return_status <> fnd_api.g_ret_sts_success THEN
1197       raise fnd_api.g_exc_error;
1198     END IF;
1199 
1200     /* switch the non source transaction details to the IN_PROCESS status */
1201     --Assign the literals.. bug 4311676
1202     l_literal1 := 'PROCESSED';
1203     l_literal2 := 'WSH_DELIVERY_DETAILS';
1204 
1205     UPDATE csi_t_txn_line_details
1206     SET    processing_status = 'IN_PROCESS'
1207     WHERE  transaction_line_id IN (
1208       SELECT transaction_line_id
1209       FROM   csi_t_transaction_lines
1210       WHERE  source_transaction_table = l_literal2
1211       AND    source_transaction_id    = l_order_line_rec.order_line_id)
1212       AND    source_transaction_flag = 'N'
1213       AND    processing_status <> l_literal1;
1214     --
1215     l_txn_line_query_rec.SOURCE_TRANSACTION_ID    := l_order_line_rec.order_line_id;
1216     l_txn_line_query_rec.SOURCE_TRANSACTION_TABLE := 'WSH_DELIVERY_DETAILS';
1217     l_txn_line_dtl_query_rec.PROCESSING_STATUS    := 'IN_PROCESS';
1218 
1219     /* read all the matched txn line dtls and all the child tables for final processing */
1220 
1221     debug('Installation details for the final process - Update Install Base.' );
1222 
1223     csi_t_txn_details_grp.get_transaction_details (
1224       p_api_version               => 1.0,
1225       p_commit                    => fnd_api.g_false,
1226       p_init_msg_list             => fnd_api.g_true,
1227       p_validation_level          => fnd_api.g_valid_level_full,
1228       p_txn_line_query_rec        => l_txn_line_query_rec,
1229       p_txn_line_detail_query_rec => l_txn_line_dtl_query_rec,
1230       x_txn_line_detail_tbl       => l_txn_line_detail_tbl,
1231       p_get_parties_flag          => fnd_api.g_true,
1232       x_txn_party_detail_tbl      => l_txn_party_detail_tbl,
1233       p_get_pty_accts_flag        => fnd_api.g_true,
1234       x_txn_pty_acct_detail_tbl   => l_txn_pty_acct_dtl_tbl,
1235       ---Added (Start) for m-to-m enhancements
1236       -- Let us not get relations here.
1237       -- get_ii_relations_tbl will get the relations at later stage
1238       p_get_ii_rltns_flag         => fnd_api.g_false,
1239       ---Added (End) for m-to-m enhancements
1240       x_txn_ii_rltns_tbl          => l_txn_ii_rltns_tbl,
1241       p_get_org_assgns_flag       => fnd_api.g_true,
1242       x_txn_org_assgn_tbl         => l_txn_org_assgn_tbl,
1243       p_get_ext_attrib_vals_flag  => fnd_api.g_true,
1244       x_txn_ext_attrib_vals_tbl   => l_txn_ext_attrib_vals_tbl,
1245       p_get_csi_attribs_flag      => fnd_api.g_false,
1246       x_csi_ext_attribs_tbl       => l_csi_ext_attribs_tbl,
1247       p_get_csi_iea_values_flag   => fnd_api.g_false,
1248       x_csi_iea_values_tbl        => l_csi_iea_values_tbl,
1249       p_get_txn_systems_flag      => fnd_api.g_true,
1250       x_txn_systems_tbl           => l_txn_systems_tbl,
1251       x_return_status             => l_return_status,
1252       x_msg_count                 => l_msg_count,
1253       x_msg_data                  => l_msg_data);
1254 
1255     IF l_return_status <> fnd_api.g_ret_sts_success THEN
1256       g_api_name := 'csi_t_txn_details_grp.get_transaction_details';
1257       raise fnd_api.g_exc_error;
1258     END IF;
1259 
1260     debug('  txn_line_detail_tbl.count  :'|| l_txn_line_detail_tbl.count  );
1261     debug('  txn_party_detail_tbl.count :'|| l_txn_party_detail_tbl.count  );
1262     debug('  txn_pty_acct_dtl_tbl.count :'|| l_txn_pty_acct_dtl_tbl.count  );
1263     debug('  txn_org_assgn_tbl.count    :'|| l_txn_org_assgn_tbl.count  );
1264     debug('  txn_systems_tbl.count      :'|| l_txn_systems_tbl.count  );
1265     debug('  txn_ii_rltns_tbl.count     :'|| l_txn_ii_rltns_tbl.count  );
1266     debug('  txn_eav_tbl.count          :'|| l_txn_ext_attrib_vals_tbl.count );
1267 
1268     /*---------------------------------------------------*/
1269     /* Get the pricing attributes for the order line and */
1270     /* pass it to update_install_base                    */
1271     /*---------------------------------------------------*/
1272 
1273     csi_utl_pkg.Get_Pricing_Attribs(
1274       p_line_id           =>  l_order_line_rec.order_line_id,
1275       x_pricing_attb_tbl  =>  x_pricing_attb_tbl,
1276       x_return_status     =>  x_return_status );
1277 
1278     IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
1279       raise fnd_api.g_exc_error;
1280     END IF;
1281 
1282     debug('  pricing_attb_tbl.count :'||x_pricing_attb_tbl.count ) ;
1283 
1284     -- Added for bug 5101635
1285     l_conv_to_prim_uom_req := 'Y';
1286     IF l_txn_line_detail_tbl.count > 0 THEN
1287       FOR u in l_txn_line_detail_tbl.FIRST..l_txn_line_detail_tbl.LAST
1288       LOOP
1289         IF NVL(l_txn_line_detail_tbl(u).serial_number,fnd_api.g_miss_char) <> fnd_api.g_miss_char AND
1290            NVL(l_txn_line_detail_tbl(u).quantity,fnd_api.g_miss_num) = 1 THEN
1291            l_conv_to_prim_uom_req := 'N';   -- No UOM Conversion Required
1292 	   Exit;
1293         END IF;
1294       END LOOP;
1295     END IF;
1296     -- End bug 5101635
1297 
1298     IF l_conv_to_prim_uom_req = 'Y' THEN      -- Added for bug 5101635
1299       csi_utl_pkg.conv_to_prim_uom(
1300          p_inv_organization_id => l_order_line_rec.inv_org_id,
1301          p_inventory_item_id   => l_order_line_rec.inv_item_id,
1302          p_uom                 => l_order_line_rec.order_quantity_uom,
1303          x_txn_line_dtl_tbl    => l_txn_line_detail_tbl,
1304          x_return_status       => x_return_status);
1305 
1306         IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
1307           raise fnd_api.g_exc_error;
1308         END IF;
1309     END IF;
1310 
1311     --
1312     -- srramakr TSO with Equipment.
1313     -- If this is a MACD order line then we need to merge the Transaction details written by CZ and WSH
1314     -- For this, we call CSI_INTERFACE_PKG.Get_CZ_Relations.
1315     -- macd_order_line flag is set by Build_Shtd_Table routine.
1316     --
1317     IF l_order_line_rec.macd_order_line = FND_API.G_TRUE THEN
1318        l_source_line_rec.source_line_id := l_order_line_rec.order_line_id;
1319        --
1320        Csi_Interface_Pkg.Get_CZ_Relations
1321           ( p_source_header_rec     =>  l_source_header_rec,
1322             p_source_line_rec       =>  l_source_line_rec,
1323             px_txn_line_rec         =>  l_transaction_line_rec,
1324             px_txn_line_dtl_tbl     =>  l_txn_line_detail_tbl,
1325             x_txn_ii_rltns_tbl      =>  l_txn_ii_rltns_tbl,
1326             x_txn_eav_tbl           =>  l_txn_ext_attrib_vals_tbl,
1327             x_return_status         =>  x_return_status
1328           );
1329        IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
1330           raise fnd_api.g_exc_error;
1331        END IF;
1332     END IF;
1333     --
1334     --
1335     l_transaction_line_rec := l_tmp_txn_line_rec;
1336     --
1337     l_transaction_line_rec.transaction_line_id        := l_order_line_rec.trx_line_id;
1338     l_transaction_line_rec.source_transaction_id      := l_order_line_rec.order_line_id;
1339     l_transaction_line_rec.source_transaction_table   := 'WSH_DELIVERY_DETAILS';
1340     l_transaction_line_rec.source_transaction_type_id := g_txn_type_id ;
1341 
1342     /*--------------------------------------------------------*/
1343     /* Once the txn details are matched and COMPONENT-OF      */
1344     /* relationships are build  then Update instance in IB    */
1345     /*--------------------------------------------------------*/
1346 
1347     update_install_base(
1348       p_api_version             => 1.0,
1349       p_commit                  => fnd_api.g_false,
1350       p_init_msg_list           => fnd_api.g_true,
1351       p_validation_level        => fnd_api.g_valid_level_full,
1352       p_txn_line_rec            => l_transaction_line_rec,
1353       p_txn_line_detail_tbl     => l_txn_line_detail_tbl,
1354       p_txn_party_detail_tbl    => l_txn_party_detail_tbl,
1355       p_txn_pty_acct_dtl_tbl    => l_txn_pty_acct_dtl_tbl,
1356       p_txn_org_assgn_tbl       => l_txn_org_assgn_tbl,
1357       p_txn_ext_attrib_vals_tbl => l_txn_ext_attrib_vals_tbl,
1358       p_txn_ii_rltns_tbl        => l_txn_ii_rltns_tbl,
1359       p_txn_systems_tbl         => l_txn_systems_tbl,
1360       p_pricing_attribs_tbl     => x_pricing_attb_tbl,
1361       p_order_line_rec          => l_order_line_rec,
1362       p_trx_rec                 => l_trx_rec,
1363       p_source                  => 'SHIPMENT',
1364       p_validate_only           => 'N',
1365       px_error_rec              => l_error_rec,
1366       x_return_status           => x_return_status,
1367       x_msg_count               => x_msg_count,
1368       x_msg_data                => x_msg_data);
1369 
1370     IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
1371       raise fnd_api.g_exc_error;
1372     END IF;
1373 
1374     debug('end: csi_order_ship_pub.order_shipment. status: successful');
1375     debug('  timestamp : '||to_char(sysdate, 'MM/DD/YY HH24:MI:SS'));
1376 
1377   EXCEPTION
1378     WHEN fnd_api.g_exc_error THEN
1379       rollback to order_shipment;
1380       x_return_status        := fnd_api.g_ret_sts_error;
1381       l_error_rec.error_text := csi_t_gen_utility_pvt.dump_error_stack;
1382       px_trx_error_rec := l_error_rec;
1383       debug('Error(E) :'||l_error_rec.error_text);
1384     WHEN fnd_api.g_exc_unexpected_error THEN
1385       rollback to order_shipment;
1386       x_return_status        := fnd_api.g_ret_sts_unexp_error ;
1387       l_error_rec.error_text := csi_t_gen_utility_pvt.dump_error_stack;
1388       px_trx_error_rec := l_error_rec;
1389       debug('Error(U) :'||l_error_rec.error_text);
1390     WHEN others THEN
1391       rollback to order_shipment;
1392       x_return_status        := fnd_api.g_ret_sts_unexp_error;
1393       l_error_rec.error_text := substr(sqlerrm, 1, 540);
1394       px_trx_error_rec        := l_error_rec;
1395       debug('Error(O) :'||l_error_rec.error_text);
1396   END order_shipment;
1397 
1398   PROCEDURE process_non_source(
1399     p_txn_line_detail_rec  IN  csi_t_datastructures_grp.txn_line_detail_rec,
1400     p_call_contracts       IN  varchar2 := fnd_api.g_true,
1401     p_trx_rec              IN  csi_datastructures_pub.transaction_rec,
1402     x_return_status        OUT NOCOPY varchar2)
1403   IS
1404 
1405     l_trx_rec                    csi_datastructures_pub.transaction_rec;
1406 
1407     l_ns_instance_rec            csi_datastructures_pub.instance_rec;
1408     l_ns_party_tbl               csi_datastructures_pub.party_tbl;
1409     l_ns_party_acct_tbl          csi_datastructures_pub.party_account_tbl;
1410     l_ns_pricing_attribs_tbl     csi_datastructures_pub.pricing_attribs_tbl;
1411     l_ns_ext_attrib_val_tbl      csi_datastructures_pub.extend_attrib_values_tbl;
1412     l_ns_org_units_tbl           csi_datastructures_pub.organization_units_tbl;
1413     l_ns_inst_asset_tbl          csi_datastructures_pub.instance_asset_tbl;
1414     l_ns_inst_id_lst             csi_datastructures_pub.id_tbl;
1415 
1416     l_ii_relation_code           varchar2(30);
1417     l_location_code              varchar2(30);
1418     l_serial_number              varchar2(80);
1419     l_active_end_date            date;
1420     l_exp_instance_id_lst        csi_datastructures_pub.id_tbl;
1421     l_exp_instance_rec           csi_datastructures_pub.instance_rec;
1422 
1423     l_return_status              varchar2(1) := fnd_api.g_ret_sts_success;
1424     l_msg_count                  number;
1425     l_msg_data                   varchar2(2000);
1426 
1427     -- Added this parameter as part of fix for Bug 3145503.
1428     l_upd_ns_inst                varchar2(1) := 'Y';
1429 
1430     CURSOR comp_of_cur(p_tl_id IN number, p_tld_id IN number) IS
1431       SELECT txn_relationship_id
1432       FROM   csi_t_ii_relationships
1433       WHERE  transaction_line_id = p_tl_id
1434       AND   (subject_id = p_tld_id
1435               OR
1436               object_id = p_tld_id)
1437       AND    relationship_type_code = 'COMPONENT-OF';
1438 
1439     l_comp_of_tld boolean := FALSE;
1440 
1441     CURSOR other_nsrc_cur(p_tl_id IN number, p_tld_id IN number) IS
1442       SELECT sub_tld.instance_id sub_instance_id,
1443              obj_tld.instance_id obj_instance_id
1444       FROM   csi_t_ii_relationships iir,
1445              csi_t_txn_line_details sub_tld,
1446              csi_t_txn_line_details obj_tld
1447       WHERE  iir.transaction_line_id = p_tl_id
1448       AND    (iir.subject_id = p_tld_id
1449               OR
1450               iir.object_id = p_tld_id)
1451       AND    iir.relationship_type_code <> 'COMPONENT-OF'
1452       AND    sub_tld.transaction_line_id = iir.transaction_line_id
1453       AND    sub_tld.txn_line_detail_id  = iir.subject_id
1454       AND    obj_tld.transaction_line_id = iir.transaction_line_id
1455       AND    obj_tld.txn_line_detail_id  = iir.object_id;
1456 
1457     l_skip_reason   varchar2(80);
1458     skip_non_source exception ;
1459 
1460   BEGIN
1461 
1462     api_log('process_non_source');
1463 
1464     x_return_status  := fnd_api.g_ret_sts_success;
1465     l_trx_rec        := p_trx_rec;
1466 
1467     l_comp_of_tld := FALSE;
1468 
1469     FOR comp_of_rec IN comp_of_cur(
1470           p_txn_line_detail_rec.transaction_line_id,
1471           p_txn_line_detail_rec.txn_line_detail_id)
1472     LOOP
1473       l_comp_of_tld := TRUE;
1474     END LOOP;
1475 
1476     IF NOT(l_comp_of_tld) THEN
1477 
1478       -- check if the source and the non source points to the same instance
1479       FOR other_nsrc_rec IN other_nsrc_cur(
1480             p_txn_line_detail_rec.transaction_line_id,
1481             p_txn_line_detail_rec.txn_line_detail_id)
1482       LOOP
1483         IF other_nsrc_rec.sub_instance_id = other_nsrc_rec.obj_instance_id THEN
1484           l_skip_reason := 'source and non source instances are the same.';
1485           RAISE skip_non_source;
1486         END IF;
1487       END LOOP;
1488 
1489       l_ns_instance_rec.instance_id := p_txn_line_detail_rec.instance_id;
1490 
1491       BEGIN
1492         SELECT  non_src_status_id
1493         INTO    l_ns_instance_rec.instance_status_id
1494         FROM    csi_txn_sub_types
1495         WHERE   transaction_type_id = p_trx_rec.transaction_type_id
1496         AND     sub_type_id = p_txn_line_detail_rec.sub_type_id;
1497       EXCEPTION
1498         WHEN no_data_found THEN
1499           null;
1500       END;
1501 
1502       l_trx_rec.txn_sub_type_id := p_txn_line_detail_rec.sub_type_id;
1503 
1504       -- added this IF as part of fix for bug 3145503.
1505       IF l_location_code <> 'INVENTORY'
1506       THEN
1507         l_ns_instance_rec.active_end_date :=
1508           nvl(p_txn_line_detail_rec.active_end_date,fnd_api.g_miss_date);
1509         l_ns_instance_rec.return_by_date :=  p_txn_line_detail_rec.return_by_date;
1510       END IF;
1511 
1512       l_ns_instance_rec.call_contracts := p_call_contracts;
1513 
1514       BEGIN
1515 
1516         SELECT object_version_number,
1517                location_type_code,
1518                serial_number,
1519 	       active_end_date
1520         INTO   l_ns_instance_rec.object_version_number,
1521                l_location_code,
1522                l_serial_number,
1523 	       l_active_end_date
1524         FROM   csi_item_instances
1525         WHERE  instance_id = p_txn_line_detail_rec.instance_id;
1526 
1527         debug('  TXN Line Dtl ID :'||p_txn_line_detail_rec.txn_line_detail_id);
1528         debug('  Instance ID     :'||l_ns_instance_rec.instance_id );
1529         debug('  Status ID       :'||l_ns_instance_rec.instance_status_id);
1530         debug('  location Code   :'||l_location_code);
1531         debug('  Active end date :'||l_active_end_date);
1532 
1533         -- IF l_location_code <> 'INVENTORY'
1534         --   AND
1535         -- Commented the above condition as part of fix for bug 3145503.
1536         IF  l_ns_instance_rec.instance_status_id IS NOT NULL
1537         THEN
1538 
1539           -- check for terminable status bug 2272771
1540           IF csi_item_instance_vld_pvt.val_inst_ter_flag(
1541                p_status_id => l_ns_instance_rec.instance_status_id)
1542           THEN
1543 
1544             -- Added the fillowing If condition as part of fix for Bug 3145503.
1545             IF l_location_code = 'INVENTORY' Then
1546               debug('Instance Status is TERMINABLE so cannot update the instance status');
1547               l_upd_ns_inst  := 'N';
1548             ELSE
1549             -- End code for Bug 3145503.
1550               debug('  Instance Status is defined as TERMINABLE.');
1551               IF nvl(l_ns_instance_rec.active_end_date, fnd_api.g_miss_date) = fnd_api.g_miss_date
1552               THEN
1553                 --bug 4026148
1554                 --l_ns_instance_rec.active_end_date := sysdate;
1555                 l_ns_instance_rec.active_end_date := l_trx_rec.source_transaction_date;
1556                 --bug 4026148
1557               END IF;
1558             END IF;  -- End if for Bug 3145503.
1559           END IF;
1560 
1561           IF l_upd_ns_inst  = 'Y' -- Added as fix for Bug 3145503.
1562           THEN
1563 	    l_ns_instance_rec.last_oe_order_line_id := l_trx_rec.source_line_ref_id;
1564            -- we should be stamping the replaced instance also with the
1565            --order information. not sure why it wasn't done ... shegde. bug 3692473
1566            --code modification start for 3681856--
1567             IF (l_active_end_date is NOT NULL) AND (l_active_end_date <= sysdate)
1568             THEN -- we will expire the instance back. Contracts shouldn't be called while unexpiring
1569 	       l_ns_instance_rec.call_contracts := fnd_api.g_false;
1570             END IF;
1571            --code modification end for 3681856--
1572 
1573             csi_t_gen_utility_pvt.dump_api_info(
1574               p_pkg_name => 'csi_item_instance_pub',
1575               p_api_name => 'update_item_instance');
1576 
1577             csi_t_gen_utility_pvt.dump_csi_instance_rec(
1578               p_csi_instance_rec => l_ns_instance_rec);
1579 
1580             /* non source status update call. */
1581             csi_item_instance_pub.update_item_instance(
1582               p_api_version           => 1.0,
1583               p_commit                => fnd_api.g_false,
1584               p_init_msg_list         => fnd_api.g_true,
1585               p_validation_level      => fnd_api.g_valid_level_full,
1586               p_instance_rec          => l_ns_instance_rec,
1587               p_ext_attrib_values_tbl => l_ns_ext_attrib_val_tbl,
1588               p_party_tbl             => l_ns_party_tbl,
1589               p_account_tbl           => l_ns_party_acct_tbl,
1590               p_pricing_attrib_tbl    => l_ns_pricing_attribs_tbl,
1591               p_org_assignments_tbl   => l_ns_org_units_tbl,
1592               p_txn_rec               => l_trx_rec,
1593               p_asset_assignment_tbl  => l_ns_inst_asset_tbl,
1594               x_instance_id_lst       => l_ns_inst_id_lst,
1595               x_return_status         => l_return_status,
1596               x_msg_count             => l_msg_count,
1597               x_msg_data              => l_msg_data );
1598 
1599             -- IF l_return_status <> fnd_api.g_ret_sts_success THEN
1600             IF l_return_status not in (fnd_api.g_ret_sts_success,'W') THEN
1601               RAISE fnd_api.g_exc_error;
1602             END IF;
1603 
1604             IF (l_active_end_date is NOT NULL) AND (l_active_end_date <= sysdate)
1605             THEN
1606               -- expiring the instance back. Added this IF for Bug 3039821
1607 
1608 	      SELECT object_version_number
1609               INTO   l_exp_instance_rec.object_version_number
1610               FROM   csi_item_instances
1611               WHERE  instance_id = p_txn_line_detail_rec.instance_id;
1612 
1613               l_exp_instance_rec.instance_id 	:= p_txn_line_detail_rec.instance_id;
1614 
1615             --code modification start for 3681856--
1616       	      --l_exp_instance_rec.call_contracts   := p_call_contracts;
1617 	      l_exp_instance_rec.call_contracts := fnd_api.g_false;
1618             --code modification end for 3681856--
1619 
1620               l_trx_rec.transaction_id       	:= fnd_api.g_miss_num;
1621 
1622               csi_t_gen_utility_pvt.dump_api_info(
1623                 p_api_name => 'expire_item_instance',
1624                 p_pkg_name => 'csi_item_instance_pub');
1625 
1626               csi_item_instance_pub.expire_item_instance(
1627                 p_api_version      => 1.0,
1628                 p_commit           => fnd_api.g_false,
1629                 p_init_msg_list    => fnd_api.g_true,
1630                 p_validation_level => fnd_api.g_valid_level_full,
1631                 p_instance_rec     => l_exp_instance_rec,
1632                 p_expire_children  => fnd_api.g_true,
1633                 p_txn_rec          => l_trx_rec,
1634                 x_instance_id_lst  => l_exp_instance_id_lst,
1635                 x_return_status    => l_return_status,
1636                 x_msg_count        => l_msg_count,
1637                 x_msg_data         => l_msg_data);
1638 
1639               IF NOT(l_return_status = fnd_api.g_ret_sts_success) THEN
1640                 g_api_name := 'csi_item_instance_pub.expire_item_instance';
1641                 raise fnd_api.g_exc_error;
1642               END IF;
1643 	    END IF;
1644 
1645             l_trx_rec.source_group_ref_id := l_ns_instance_rec.instance_id;
1646             l_trx_rec.source_group_ref    := l_serial_number;
1647 
1648             SELECT object_version_number
1649             INTO   l_trx_rec.object_version_number
1650             FROM   csi_transactions
1651             WHERE  transaction_id = l_trx_rec.transaction_id;
1652 
1653             csi_transactions_pvt.update_transactions(
1654               p_api_version       => 1.0,
1655               p_init_msg_list     => fnd_api.g_false,
1656               p_commit            => fnd_api.g_false,
1657               p_validation_level  => fnd_api.g_valid_level_full,
1658               p_transaction_rec   => l_trx_rec,
1659               x_return_status     => l_return_status,
1660               x_msg_count         => l_msg_count,
1661               x_msg_data          => l_msg_data);
1662           END IF; -- Added as fix for Bug 3145503.
1663         END IF;
1664       EXCEPTION
1665         WHEN no_data_found THEN
1666           debug('Could not find the non source instance.');
1667       END;
1668     ELSE
1669       debug('Non Source record built by interface. Do not process.');
1670     END IF;
1671 
1672   EXCEPTION
1673     WHEN skip_non_source THEN
1674       debug('  skip non source processing :'||l_skip_reason);
1675       x_return_status := fnd_api.g_ret_sts_success;
1676     WHEN fnd_api.g_exc_error THEN
1677       x_return_status := fnd_api.g_ret_sts_error;
1678   END process_non_source;
1679 
1680   /* ********************************************************************** */
1681   /* Procedure name: get_order_shipment_rec                                 */
1682   /* Description   : Procedure that identifies the the instance shipped and */
1683   /*                 build the Shipment record for all the delivery lines   */
1684   /*                 for that material transaction id                       */
1685   /* ********************************************************************** */
1686 
1687   -- Included Transaction rec as part of fix for Bug 2767338
1688   PROCEDURE get_order_shipment_rec(
1689     p_mtl_transaction_id      IN  NUMBER,
1690     p_order_line_rec          IN  order_line_rec,
1691     p_txn_sub_type_rec        IN  txn_sub_type_rec,
1692     p_transaction_rec         IN OUT NOCOPY csi_datastructures_pub.transaction_rec,
1693     x_order_shipment_tbl      OUT NOCOPY order_shipment_tbl,
1694     px_error_rec              IN OUT NOCOPY csi_datastructures_pub.transaction_error_rec,
1695     x_return_status           OUT NOCOPY varchar2)
1696   IS
1697 
1698     l_party_tbl             csi_datastructures_pub.party_tbl;
1699     l_party_account_tbl     csi_datastructures_pub.party_account_tbl;
1700     l_party_query_rec       csi_datastructures_pub.party_query_rec;
1701     l_pty_acct_query_rec    csi_datastructures_pub.party_account_query_rec;
1702     l_instance_header_tbl   csi_datastructures_pub.instance_header_tbl;
1703     l_ext_attrib_val_tabl   csi_datastructures_pub.extend_attrib_values_tbl;
1704     l_pricing_attribs_tbl   csi_datastructures_pub.pricing_attribs_tbl;
1705     l_org_units_tbl         csi_datastructures_pub.organization_units_tbl;
1706     l_inst_query_rec        csi_datastructures_pub.instance_query_rec;
1707 
1708     -- Added a column reservable_type from mtl_systems_items for negative quantity check.
1709     l_cre_instance_rec        csi_datastructures_pub.instance_rec;
1710     l_cre_party_tbl           csi_datastructures_pub.party_tbl;
1711     l_cre_ext_attrib_val_tbl  csi_datastructures_pub.extend_attrib_values_tbl;
1712     l_cre_party_acct_tbl      csi_datastructures_pub.party_account_tbl;
1713     l_cre_pricing_attribs_tbl csi_datastructures_pub.pricing_attribs_tbl;
1714     l_cre_org_units_tbl       csi_datastructures_pub.organization_units_tbl;
1715     l_cre_inst_asset_tbl      csi_datastructures_pub.instance_asset_tbl;
1716     l_trx_rec                 csi_datastructures_pub.transaction_rec;
1717 
1718     -- Added for updating instance to ZERO
1719     l_zero_instance_rec       csi_datastructures_pub.instance_rec;
1720     l_zero_parties_tbl        csi_datastructures_pub.party_tbl;
1721     l_zero_pty_accts_tbl      csi_datastructures_pub.party_account_tbl;
1722     l_zero_org_units_tbl      csi_datastructures_pub.organization_units_tbl;
1723     l_zero_ea_values_tbl      csi_datastructures_pub.extend_attrib_values_tbl;
1724     l_zero_pricing_tbl        csi_datastructures_pub.pricing_attribs_tbl;
1725     l_zero_assets_tbl         csi_datastructures_pub.instance_asset_tbl;
1726     l_inst_id_lst             csi_datastructures_pub.id_tbl;
1727 
1728     x_msg_count             NUMBER;
1729     x_msg_data              varchar2(2000);
1730     l_party_id              NUMBER;
1731     l_uom_rate              NUMBER;
1732     l_shipped_qty           NUMBER;
1733     l_count                 NUMBER := 0;
1734     l_return_status         varchar2(1) := fnd_api.g_ret_sts_success;
1735     l_active_instances_only varchar2(1);
1736 
1737     -- Included Transaction rec as part of fix for Bug 2767338
1738     CURSOR mmt_cur(p_trx_id IN NUMBER) IS
1739       SELECT ool.line_id,
1740              ool.header_id,
1741              ool.item_type_code,
1742              ool.cust_po_number,
1743              ool.ato_line_id,
1744              ool.top_model_line_id,
1745              ool.link_to_line_id,
1746              NVL(ool.invoice_to_contact_id ,ooh.invoice_to_contact_id ) invoice_to_contact_id ,
1747              ool.line_type_id,
1748              ool.ordered_quantity,
1749              ool.shipped_quantity ord_line_shipped_qty,
1750              NVL(ool.ship_to_contact_id, ooh.ship_to_contact_id) ship_to_contact_id,
1751              NVL(ool.ship_from_org_id, ooh.ship_from_org_id)     ship_from_org_id  ,
1752              NVL(ool.sold_to_org_id, ooh.sold_to_org_id)         sold_to_org_id    ,
1753              NVL(ool.sold_from_org_id, ooh.sold_from_org_id)     sold_from_org_id  ,
1754              NVL(ool.ship_to_org_id, ooh.ship_to_org_id)         ship_to_org_id    ,
1755              NVL(ool.invoice_to_org_id, ooh.invoice_to_org_id)   invoice_to_org_id ,
1756              NVL(ool.deliver_to_org_id, ooh.deliver_to_org_id)   deliver_to_org_id ,
1757              ool.order_quantity_uom,
1758              mmt.inventory_item_id   inventory_item_id,
1759              mmt.organization_id     inv_organization_id,
1760              mmt.revision            revision,
1761              mmt.subinventory_code   subinventory,
1762              mmt.locator_id          locator_id,
1763              null                    lot_number,
1764              mut.serial_number       serial_number,
1765              abs(mmt.transaction_quantity)  shipped_quantity,
1766              mmt.transaction_uom,
1767              mmt.transaction_date,
1768             -- msi.lot_control_code,
1769 		  nvl(csi_utl_pkg.get_lot_ctrl_code(p_trx_id),lot_control_code) lot_control_code,-- Added for bug#14835893
1770              msi.serial_number_control_code,
1771              msi.reservable_type,
1772              haou.location_id        hr_location_id,
1773              msei.location_id        subinv_location_id,
1774              to_char(null)           ib_owner,
1775              to_number(null)         end_customer_id,
1776              to_char(null)           ib_install_loc,
1777              to_number(null)         ib_install_loc_id,
1778              to_char(null)           ib_current_loc,
1779              to_number(null)         ib_current_loc_id,
1780              ooh.order_source_id     order_source_id -- Added for Siebel Genesis Project
1781       FROM   oe_order_headers_all         ooh,
1782              oe_order_lines_all           ool,
1783              mtl_system_items             msi,
1784              mtl_unit_transactions        mut,
1785              mtl_material_transactions    mmt,
1786              mtl_secondary_inventories    msei,
1787              hr_all_organization_units    haou
1788       WHERE  mmt.transaction_id       = p_trx_id
1789       AND    mmt.transaction_id       = mut.transaction_id(+)
1790       AND    msi.organization_id      = mmt.organization_id
1791       AND    msi.inventory_item_id    = mmt.inventory_item_id
1792       AND    msi.lot_control_code     = 1   -- no lot case
1793       AND    mmt.organization_id      = haou.organization_id(+)
1794       AND    mmt.subinventory_code    = msei.secondary_inventory_name(+)
1795       AND    mmt.organization_id      = msei.organization_id(+)
1796       AND    ool.line_id              = mmt.trx_source_line_id
1797       AND    ooh.header_id            = ool.header_id
1798       UNION
1799       SELECT ool.line_id,
1800              ool.header_id,
1801              ool.item_type_code,
1802              ool.cust_po_number,
1803              ool.ato_line_id,
1804              ool.top_model_line_id,
1805              ool.link_to_line_id,
1806              NVL(ool.invoice_to_contact_id, ooh.invoice_to_contact_id ) invoice_to_contact_id ,
1807              ool.line_type_id,
1808              ool.ordered_quantity,
1809              ool.shipped_quantity ord_line_shipped_qty,
1810              NVL(ool.ship_to_contact_id, ooh.ship_to_contact_id)  ship_to_contact_id,
1811              NVL(ool.ship_from_org_id , ooh.ship_from_org_id)     ship_from_org_id  ,
1812              NVL(ool.sold_to_org_id , ooh.sold_to_org_id)         sold_to_org_id    ,
1813              NVL(ool.sold_from_org_id, ooh.sold_from_org_id)      sold_from_org_id  ,
1814              NVL(ool.ship_to_org_id , ooh.ship_to_org_id)         ship_to_org_id    ,
1815              NVL(ool.invoice_to_org_id, ooh.invoice_to_org_id)    invoice_to_org_id ,
1816              NVL(ool.deliver_to_org_id, ooh.deliver_to_org_id)    deliver_to_org_id ,
1817              ool.order_quantity_uom     ,
1818              mmt.inventory_item_id   inventory_item_id,
1819              mmt.organization_id     inv_organization_id,
1820              mmt.revision            revision,
1821              mmt.subinventory_code   subinventory,
1822              mmt.locator_id          locator_id,
1823              mtln.lot_number         lot_number,
1824              mut.serial_number         serial_number,
1825              abs(mtln.transaction_quantity)  shipped_quantity,
1826              mmt.transaction_uom,
1827              mmt.transaction_date,
1828             -- msi.lot_control_code,
1829 		   nvl(csi_utl_pkg.get_lot_ctrl_code(p_trx_id),lot_control_code) lot_control_code,-- Added for bug#14835893
1830              msi.serial_number_control_code,
1831              msi.reservable_type,
1832              haou.location_id        hr_location_id,
1833              msei.location_id        subinv_location_id,
1834              to_char(null)           ib_owner,
1835              to_number(null)         end_customer_id,
1836              to_char(null)           ib_install_loc,
1837              to_number(null)         ib_install_loc_id,
1838              to_char(null)           ib_current_loc,
1839              to_number(null)         ib_current_loc_id,
1840              ooh.order_source_id     order_source_id -- Added for Siebel Genesis Project
1841       FROM   oe_order_headers_all         ooh,
1842              oe_order_lines_all           ool,
1843              mtl_system_items             msi,
1844              mtl_unit_transactions        mut,
1845              mtl_transaction_lot_numbers  mtln,
1846              mtl_material_transactions    mmt,
1847              mtl_secondary_inventories    msei,
1848              hr_all_organization_units    haou
1849       WHERE  mmt.transaction_id         = p_trx_id
1850       AND    mmt.transaction_id         = mtln.transaction_id(+)
1851       AND    mtln.serial_transaction_id = mut.transaction_id(+)
1852       AND    msi.organization_id        = mmt.organization_id
1853       AND    msi.inventory_item_id      = mmt.inventory_item_id
1854       AND    msi.lot_control_code       = 2   -- lot control case
1855       AND    mmt.organization_id        = haou.organization_id(+)
1856       AND    mmt.subinventory_code      = msei.secondary_inventory_name(+)
1857       AND    mmt.organization_id        = msei.organization_id(+)
1858       AND    mmt.trx_source_line_id     = ool.line_id
1859       AND    ool.header_id              = ooh.header_id;
1860 
1861       -- For partner prdering
1862       l_partner_rec             oe_install_base_util.partner_order_rec;
1863       l_partner_owner_id        NUMBER;
1864       l_partner_owner_acct_id   NUMBER;
1865 
1866   BEGIN
1867 
1868     x_return_status := fnd_api.g_ret_sts_success;
1869 
1870     -- Included Transaction rec as part of fix for Bug 2767338
1871     l_trx_rec := p_transaction_rec;
1872 
1873     api_log('get_order_shipment_rec');
1874 
1875     /* Get the shipment record and assign to PL/SQL table */
1876     FOR mmt_rec IN mmt_cur(p_mtl_transaction_id) LOOP
1877 
1878       px_error_rec.serial_number := mmt_rec.serial_number;
1879       px_error_rec.lot_number    := mmt_rec.lot_number;
1880 
1881       l_count := l_count + 1;
1882 
1883       -- for partner ordering
1884       OE_INSTALL_BASE_UTIL.get_partner_ord_rec(p_order_line_id      => mmt_rec.line_id,
1885                                                x_partner_order_rec  => l_partner_rec);
1886 
1887       IF l_partner_rec.IB_OWNER = 'END_CUSTOMER'
1888       THEN
1889         IF l_partner_rec.END_CUSTOMER_ID is null Then
1890            fnd_message.set_name('CSI','CSI_PARTNER_VAL_MISSING');
1891            fnd_msg_pub.add;
1892            raise fnd_api.g_exc_error;
1893         ELSE
1894            mmt_rec.ib_owner        := l_partner_rec.ib_owner;
1895            mmt_rec.end_customer_id := l_partner_rec.end_customer_id;
1896         END IF;
1897       ELSIF l_partner_rec.IB_OWNER = 'INSTALL_BASE'
1898       THEN
1899            mmt_rec.ib_owner        := l_partner_rec.ib_owner;
1900            mmt_rec.end_customer_id := fnd_api.g_miss_num;
1901       ELSE
1902         mmt_rec.end_customer_id   := mmt_rec.sold_to_org_id;
1903       END IF;
1904 
1905       IF l_partner_rec.IB_INSTALLED_AT_LOCATION is not null
1906       THEN
1907        mmt_rec.ib_install_loc   := l_partner_rec.IB_INSTALLED_AT_LOCATION;
1908        IF mmt_rec.ib_install_loc = 'END_CUSTOMER'
1909        THEN
1910          IF l_partner_rec.end_customer_site_use_id is null
1911          THEN
1912            fnd_message.set_name('CSI','CSI_PARTNER_VAL_MISSING');
1913            fnd_msg_pub.add;
1914            raise fnd_api.g_exc_error;
1915          ELSE
1916            mmt_rec.ib_install_loc_id :=  l_partner_rec.end_customer_site_use_id;
1917          END IF;
1918        ELSIF mmt_rec.ib_install_loc = 'SHIP_TO'
1919        THEN
1920          IF mmt_rec.ship_to_org_id is null
1921          THEN
1922            fnd_message.set_name('CSI','CSI_PARTNER_VAL_MISSING');
1923            fnd_msg_pub.add;
1924            raise fnd_api.g_exc_error;
1925          ELSE
1926            mmt_rec.ib_install_loc_id := mmt_rec.ship_to_org_id;
1927          END IF;
1928        ELSIF  mmt_rec.ib_install_loc = 'SOLD_TO'
1929        THEN
1930          IF l_partner_rec.SOLD_TO_SITE_USE_ID is null -- 3412544 mmt_rec.sold_to_org_id is null
1931          THEN
1932            fnd_message.set_name('CSI','CSI_PARTNER_VAL_MISSING');
1933            fnd_msg_pub.add;
1934            raise fnd_api.g_exc_error;
1935          ELSE
1936            mmt_rec.ib_install_loc_id := l_partner_rec.SOLD_TO_SITE_USE_ID; -- 3412544 mmt_rec.sold_to_org_id;
1937          END IF;
1938        ELSIF mmt_rec.ib_install_loc = 'DELIVER_TO'
1939        THEN
1940          IF mmt_rec.deliver_to_org_id is null
1941          THEN
1942            fnd_message.set_name('CSI','CSI_PARTNER_VAL_MISSING');
1943            fnd_msg_pub.add;
1944            raise fnd_api.g_exc_error;
1945          ELSE
1946            mmt_rec.ib_install_loc_id := mmt_rec.deliver_to_org_id;
1947          END IF;
1948        ELSIF mmt_rec.ib_install_loc = 'BILL_TO'
1949        THEN
1950          IF mmt_rec.invoice_to_org_id is null
1951          THEN
1952            fnd_message.set_name('CSI','CSI_PARTNER_VAL_MISSING');
1953            fnd_msg_pub.add;
1954            raise fnd_api.g_exc_error;
1955          ELSE
1956            mmt_rec.ib_install_loc_id := mmt_rec.invoice_to_org_id;
1957          END IF;
1958        ELSIF mmt_rec.ib_install_loc = 'INSTALL_BASE'
1959        THEN
1960           mmt_rec.ib_install_loc_id := fnd_api.g_miss_num;
1961        END IF;
1962      ELSE
1963        mmt_rec.ib_install_loc_id := mmt_rec.ship_to_org_id;
1964      END IF;
1965 
1966     IF l_partner_rec.IB_CURRENT_LOCATION is not null
1967     THEN
1968        mmt_rec.ib_current_loc   := l_partner_rec.IB_CURRENT_LOCATION;
1969        IF mmt_rec.ib_current_loc = 'END_CUSTOMER'
1970        THEN
1971          IF l_partner_rec.end_customer_site_use_id is null
1972          THEN
1973            fnd_message.set_name('CSI','CSI_PARTNER_VAL_MISSING');
1974            fnd_msg_pub.add;
1975            raise fnd_api.g_exc_error;
1976          ELSE
1977            mmt_rec.ib_current_loc_id :=  l_partner_rec.end_customer_site_use_id;
1978          END IF;
1979        ELSIF mmt_rec.ib_current_loc = 'SHIP_TO'
1980        THEN
1981          IF mmt_rec.ship_to_org_id is null
1982          THEN
1983            fnd_message.set_name('CSI','CSI_PARTNER_VAL_MISSING');
1984            fnd_msg_pub.add;
1985            raise fnd_api.g_exc_error;
1986          ELSE
1987            mmt_rec.ib_current_loc_id := mmt_rec.ship_to_org_id;
1988          END IF;
1989        ELSIF mmt_rec.ib_current_loc = 'SOLD_TO'
1990        THEN
1991          IF l_partner_rec.SOLD_TO_SITE_USE_ID is null -- 3412544 mmt_rec.sold_to_org_id is null
1992          THEN
1993            fnd_message.set_name('CSI','CSI_PARTNER_VAL_MISSING');
1994            fnd_msg_pub.add;
1995            raise fnd_api.g_exc_error;
1996          ELSE
1997            mmt_rec.ib_current_loc_id := l_partner_rec.SOLD_TO_SITE_USE_ID; -- 3412544 mmt_rec.sold_to_org_id;
1998          END IF;
1999        ELSIF mmt_rec.ib_current_loc = 'DELIVER_TO'
2000        THEN
2001          IF mmt_rec.deliver_to_org_id is null
2002          THEN
2003            fnd_message.set_name('CSI','CSI_PARTNER_VAL_MISSING');
2004            fnd_msg_pub.add;
2005            raise fnd_api.g_exc_error;
2006          ELSE
2007            mmt_rec.ib_current_loc_id := mmt_rec.deliver_to_org_id;
2008          END IF;
2009        ELSIF mmt_rec.ib_current_loc = 'BILL_TO'
2010        THEN
2011          IF  mmt_rec.invoice_to_org_id is null
2012          THEN
2013            fnd_message.set_name('CSI','CSI_PARTNER_VAL_MISSING');
2014            fnd_msg_pub.add;
2015            raise fnd_api.g_exc_error;
2016          ELSE
2017           mmt_rec.ib_current_loc_id := mmt_rec.invoice_to_org_id;
2018          END IF;
2019        ELSIF mmt_rec.ib_current_loc = 'INSTALL_BASE'
2020          THEN
2021            mmt_rec.ib_current_loc_id := fnd_api.g_miss_num;
2022        END IF;
2023     ELSE
2024       mmt_rec.ib_current_loc_id := mmt_rec.ship_to_org_id;
2025     END IF;
2026 
2027 
2028       /* Derive the party_id from hz_cust_accounts table */
2029 
2030       csi_utl_pkg.get_party_id(
2031         p_cust_acct_id  => mmt_rec.end_customer_id, --mmt_rec.sold_to_org_id,
2032         x_party_id      => l_party_id,
2033         x_return_status => l_return_status);
2034 
2035       IF l_return_status <> fnd_api.g_ret_sts_success THEN
2036         raise fnd_api.g_exc_error;
2037       END IF;
2038 
2039       IF l_party_id = -1 THEN
2040         raise fnd_api.g_exc_error;
2041       END IF;
2042 
2043       /* Convert the shipped qty to UOM in OM */
2044       inv_convert.inv_um_conversion (
2045         from_unit  => mmt_rec.transaction_uom,
2046         to_unit    => mmt_rec.order_quantity_uom,
2047         item_id    => mmt_rec.inventory_item_id,
2048         uom_rate   => l_uom_rate );
2049 
2050       debug('UOM Conv Factor     :'||l_uom_rate);
2051 
2052       IF l_uom_rate = -99999 THEN
2053         debug('inv_convert.inv_um_conversion failed ');
2054         raise fnd_api.g_exc_error;
2055       END IF;
2056 
2057       l_shipped_qty := (l_uom_rate * mmt_rec.shipped_quantity);
2058 
2059       l_inst_query_rec.location_type_code  := 'INVENTORY';
2060       l_inst_query_rec.instance_usage_code := 'IN_INVENTORY';
2061 
2062       IF mmt_rec.serial_number_control_code in (1,6) THEN
2063         l_inst_query_rec.inventory_item_id     := mmt_rec.inventory_item_id;
2064         l_inst_query_rec.inv_organization_id   := mmt_rec.inv_organization_id;
2065         l_inst_query_rec.inventory_revision    := mmt_rec.revision;
2066         l_inst_query_rec.inv_subinventory_name := mmt_rec.subinventory;
2067         l_inst_query_rec.inv_locator_id        := mmt_rec.locator_id;
2068         l_inst_query_rec.serial_number         := NULL; -- Bug 3056511
2069       ELSE
2070         l_inst_query_rec.inventory_item_id     := mmt_rec.inventory_item_id;
2071         l_inst_query_rec.inv_organization_id   := mmt_rec.inv_organization_id;
2072         l_inst_query_rec.inv_subinventory_name := mmt_rec.subinventory;
2073         l_inst_query_rec.serial_number         := mmt_rec.serial_number;
2074       END IF;
2075 
2076       IF mmt_rec.lot_control_code = 2 THEN
2077         l_inst_query_rec.lot_number            :=  mmt_rec.lot_number;
2078       END IF;
2079 
2080       debug('query criteria to identify inventory source instance' );
2081       debug('----------------------------------------------------' );
2082       debug('inventory_item_id    :'||l_inst_query_rec.inventory_item_id);
2083       debug('inv_organization_id  :'||l_inst_query_rec.inv_organization_id);
2084       debug('location_type_code   :'||l_inst_query_rec.location_type_code);
2085       debug('instance_usage_code  :'||l_inst_query_rec.instance_usage_code);
2086       debug('inventory_revision   :'||l_inst_query_rec.inventory_revision);
2087       debug('inv_subinv_name      :'||l_inst_query_rec.inv_subinventory_name);
2088       debug('inv_locator_id       :'||l_inst_query_rec.inv_locator_id);
2089       debug('lot_number           :'||l_inst_query_rec.lot_number);
2090       debug('serial_number        :'||l_inst_query_rec.serial_number);
2091       debug('----------------------------------------------------' );
2092 
2093       csi_t_gen_utility_pvt.dump_api_info(
2094         p_pkg_name => 'csi_item_instance_pub',
2095         p_api_name => 'get_item_instances');
2096 
2097       -- As part of fix for Bug 2985193 this call even brings the expired Inv. Instances.
2098       -- To achieve this p_active_instances_only should have a value of false.
2099 
2100       IF mmt_rec.reservable_type = 2
2101       THEN
2102          l_active_instances_only := fnd_api.g_false;
2103       ELSE
2104          l_active_instances_only := fnd_api.g_true;
2105       END IF;
2106 
2107       csi_item_instance_pub.get_item_instances(
2108         p_api_version          => 1.0,
2109         p_commit               => fnd_api.g_false,
2110         p_init_msg_list        => fnd_api.g_true,
2111         p_validation_level     => fnd_api.g_valid_level_full,
2112         p_instance_query_rec   => l_inst_query_rec,
2113         p_party_query_rec      => l_party_query_rec,
2114         p_account_query_rec    => l_pty_acct_query_rec,
2115         p_transaction_id       => null,
2116         p_resolve_id_columns   => fnd_api.g_false,
2117         p_active_instance_only => l_active_instances_only,-- fnd_api.g_false,  --fnd_api.g_true,
2118         x_instance_header_tbl  => l_instance_header_tbl,
2119         x_return_status        => x_return_status,
2120         x_msg_count            => x_msg_count,
2121         x_msg_data             => x_msg_data);
2122 
2123       IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
2124         raise fnd_api.g_exc_error;
2125       END IF;
2126 
2127       debug('Instance(s) Count   :'||l_instance_header_tbl.count);
2128 
2129       IF l_instance_header_tbl.count < 1 THEN
2130 
2131         -- Bug 2767338
2132         -- Begin Code for negative quantity option set at Org Level
2133 
2134         IF p_order_line_rec.negative_balances_code= 1
2135           AND
2136            mmt_rec.reservable_type = 2
2137           AND
2138             mmt_rec.serial_number_control_code in (1,6)
2139         THEN
2140 
2141           debug(' Negative Quantity allowed for this Org so Building Instance Rec');
2142 
2143           -- Create a Instance with missing Quantity
2144           -- Building Instance Rec
2145           l_cre_instance_rec.inventory_item_id       := l_inst_query_rec.inventory_item_id;
2146           l_cre_instance_rec.inv_organization_id     := l_inst_query_rec.inv_organization_id;
2147           l_cre_instance_rec.vld_organization_id     := l_inst_query_rec.inv_organization_id;
2148           l_cre_instance_rec.location_type_code      := l_inst_query_rec.location_type_code;
2149           l_cre_instance_rec.instance_usage_code     := l_inst_query_rec.instance_usage_code;
2150           l_cre_instance_rec.inventory_revision      := l_inst_query_rec.inventory_revision;
2151           l_cre_instance_rec.inv_subinventory_name   := l_inst_query_rec.inv_subinventory_name;
2152           l_cre_instance_rec.inv_locator_id          := l_inst_query_rec.inv_locator_id;
2153           l_cre_instance_rec.lot_number              := l_inst_query_rec.lot_number;
2154           l_cre_instance_rec.serial_number           := l_inst_query_rec.serial_number;
2155           l_cre_instance_rec.quantity                := 1; --mmt_rec.shipped_quantity;
2156           l_cre_instance_rec.unit_of_measure         := mmt_rec.transaction_uom;
2157           l_cre_instance_rec.location_id             := nvl(mmt_rec.subinv_location_id,mmt_rec.hr_location_id);
2158           l_cre_instance_rec.operational_status_code := 'NOT_USED';
2159           -- Begin Add Code for Siebel Genesis Project
2160           IF (mmt_rec.order_source_id = '28' OR mmt_rec.order_source_id = '29')
2161           THEN
2162              l_cre_instance_rec.source_code := 'SIEBEL';
2163           END IF;
2164           -- End Add Code for Siebel Genesis Project
2165 
2166           IF nvl(l_inst_query_rec.serial_number,fnd_api.g_miss_char) = fnd_api.g_miss_char  THEN
2167             l_cre_instance_rec.mfg_serial_number_flag := 'N';
2168           ELSE
2169             l_cre_instance_rec.mfg_serial_number_flag := 'Y';
2170           END IF;
2171 
2172           -- Building Party Rec for creating Instance with missing Quantity
2173           l_cre_party_tbl(1).party_id := csi_datastructures_pub.g_install_param_rec.internal_party_id;
2174 
2175           l_cre_party_tbl(1).party_source_table     :=  'HZ_PARTIES';
2176           l_cre_party_tbl(1).relationship_type_code :=  'OWNER';
2177           l_cre_party_tbl(1).contact_flag           :=  'N';
2178 
2179           debug('Creating an inventory instance in the staging area for non reservable item.');
2180           csi_t_gen_utility_pvt.dump_api_info(
2181             p_api_name => 'create_item_instance',
2182             p_pkg_name => 'csi_item_instance_pub');
2183 
2184           /* Creation of instance with the Shippable Quantity for Negative Quantities */
2185 
2186           debug('Creating an instance with quantity 1 for Non-Reservable Item');
2187           csi_item_instance_pub.create_item_instance(
2188             p_api_version           => 1.0,
2189             p_commit                => fnd_api.g_false,
2190             p_init_msg_list         => fnd_api.g_true,
2191             p_validation_level      => fnd_api.g_valid_level_full,
2192             p_instance_rec          => l_cre_instance_rec,
2193             p_ext_attrib_values_tbl => l_cre_ext_attrib_val_tbl,
2194             p_party_tbl             => l_cre_party_tbl,
2195             p_account_tbl           => l_cre_party_acct_tbl,
2196             p_pricing_attrib_tbl    => l_cre_pricing_attribs_tbl,
2197             p_org_assignments_tbl   => l_cre_org_units_tbl,
2198             p_asset_assignment_tbl  => l_cre_inst_asset_tbl,
2199             p_txn_rec               => l_trx_rec,
2200             x_return_status         => x_return_status,
2201             x_msg_count             => x_msg_count,
2202             x_msg_data              => x_msg_data  );
2203 
2204           -- For Bug 4057183
2205           -- IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
2206           IF NOT(x_return_status in (fnd_api.g_ret_sts_success,'W')) THEN
2207             g_api_name := 'csi_item_instance_pub.create_item_instance';
2208             raise fnd_api.g_exc_error;
2209           END IF;
2210 
2211           l_zero_instance_rec.instance_id           := l_cre_instance_rec.instance_id;
2212           l_zero_instance_rec.quantity              := 0;
2213 
2214           -- debug('After Create OBJECT_VERSION_NUMBER '||l_cre_instance_rec.object_version_number);
2215           Select object_version_number
2216           Into   l_zero_instance_rec.object_version_number
2217           From   csi_item_instances
2218           Where  instance_id = l_cre_instance_rec.instance_id;
2219 
2220           -- debug('Before calling update OBJECT_VERSION_NUMBER '||l_zero_instance_rec.object_version_number);
2221 
2222           debug('Updating Created Instance to zero Quantity');
2223 
2224           csi_t_gen_utility_pvt.dump_api_info(
2225             p_pkg_name => 'csi_item_instance_pub',
2226             p_api_name => 'update_item_instance');
2227 
2228           csi_item_instance_pub.update_item_instance(
2229             p_api_version           => 1.0,
2230             p_commit                => fnd_api.g_false,
2231             p_init_msg_list         => fnd_api.g_true,
2232             p_validation_level      => fnd_api.g_valid_level_full,
2233             p_instance_rec          => l_zero_instance_rec,
2234             p_party_tbl             => l_zero_parties_tbl,
2235             p_account_tbl           => l_zero_pty_accts_tbl,
2236             p_org_assignments_tbl   => l_zero_org_units_tbl,
2237             p_ext_attrib_values_tbl => l_zero_ea_values_tbl,
2238             p_pricing_attrib_tbl    => l_zero_pricing_tbl,
2239             p_asset_assignment_tbl  => l_zero_assets_tbl,
2240             p_txn_rec               => l_trx_rec,
2241             x_instance_id_lst       => l_inst_id_lst,
2242             x_return_status         => l_return_status,
2243             x_msg_count             => x_msg_count,
2244             x_msg_data              => x_msg_data);
2245 
2246           -- For Bug 4057183
2247           -- IF l_return_status <> fnd_api.g_ret_sts_success THEN
2248           IF l_return_status not in (fnd_api.g_ret_sts_success,'W') THEN
2249             RAISE fnd_api.g_exc_error;
2250           END IF;
2251 
2252           debug('Instance ('||l_zero_instance_rec.instance_id||') created and updated with zero quantity successfully.');
2253           l_instance_header_tbl(1).instance_id           := l_zero_instance_rec.instance_id;
2254           l_instance_header_tbl(1).quantity              := l_zero_instance_rec.quantity;
2255           -- l_instance_header_tbl(1).object_version_number := l_cre_instance_rec.object_version_number;
2256 
2257           Select object_version_number
2258           Into   l_instance_header_tbl(1).object_version_number
2259           From   csi_item_instances
2260           Where  instance_id = l_zero_instance_rec.instance_id;
2261 
2262           px_error_rec.instance_id := l_zero_instance_rec.instance_id;
2263 
2264         ELSE
2265           fnd_message.SET_NAME('CSI','CSI_INT_NO_INST_FOUND');
2266           fnd_msg_pub.add;
2267           raise fnd_api.g_exc_error;
2268         END IF;
2269       END IF;
2270       -- Bug 2767338
2271       -- End Fix for negative quantity option set at Org Level
2272 
2273       IF l_instance_header_tbl.count > 1 THEN
2274         fnd_message.SET_NAME('CSI','CSI_INT_MANY_INST_FOUND');
2275         fnd_msg_pub.add;
2276         raise fnd_api.g_exc_error;
2277       END IF;
2278 
2279       debug('Unique inventory instance found.....!');
2280       px_error_rec.instance_id := l_instance_header_tbl(1).instance_id;
2281 
2282       debug('  Inventory Instance_ID :'||l_instance_header_tbl(1).instance_id);
2283       debug('  Instance Quantity     :'||l_instance_header_tbl(1).quantity );
2284 
2285       x_order_shipment_tbl(l_count).line_id            := mmt_rec.line_id ;
2286       x_order_shipment_tbl(l_count).header_id          := mmt_rec.header_id;
2287       x_order_shipment_tbl(l_count).instance_id        := l_instance_header_tbl(1).instance_id;
2288       x_order_shipment_tbl(l_count).instance_qty       := l_instance_header_tbl(1).quantity;
2289       x_order_shipment_tbl(l_count).party_id           := l_party_id;
2290       x_order_shipment_tbl(l_count).party_source_table := 'HZ_PARTIES';
2291       x_order_shipment_tbl(l_count).party_account_id   := mmt_rec.end_customer_id; -- mmt_rec.sold_to_org_id;
2292       x_order_shipment_tbl(l_count).inventory_item_id  := mmt_rec.inventory_item_id ;
2293       x_order_shipment_tbl(l_count).organization_id    := mmt_rec.inv_organization_id ;
2294       x_order_shipment_tbl(l_count).revision           := mmt_rec.revision;
2295       x_order_shipment_tbl(l_count).subinventory       := mmt_rec.subinventory ;
2296       x_order_shipment_tbl(l_count).locator_id         := mmt_rec.locator_id ;
2297       x_order_shipment_tbl(l_count).lot_number         := mmt_rec.lot_number ;
2298       x_order_shipment_tbl(l_count).serial_number      := mmt_rec.serial_number  ;
2299       x_order_shipment_tbl(l_count).transaction_uom    := mmt_rec.transaction_uom  ;
2300       x_order_shipment_tbl(l_count).order_quantity_uom := mmt_rec.order_quantity_uom  ;
2301       x_order_shipment_tbl(l_count).invoice_to_org_id  := mmt_rec.invoice_to_org_id;
2302       x_order_shipment_tbl(l_count).line_type_id       := mmt_rec.line_type_id  ;
2303       x_order_shipment_tbl(l_count).ordered_quantity   := mmt_rec.ordered_quantity;
2304       x_order_shipment_tbl(l_count).ship_to_contact_id := mmt_rec.ship_to_contact_id;
2305       x_order_shipment_tbl(l_count).ship_to_org_id     := mmt_rec.ship_to_org_id  ;
2306       x_order_shipment_tbl(l_count).ship_from_org_id   := mmt_rec.ship_from_org_id ;
2307       x_order_shipment_tbl(l_count).sold_to_org_id     := mmt_rec.sold_to_org_id  ;
2308       x_order_shipment_tbl(l_count).sold_from_org_id   := mmt_rec.sold_from_org_id ;
2309 
2310       /* added this if condition for the bug 2356340 */
2311       /* Added 2 and 5 as part of fix for Bug 3089110 */
2312 
2313       IF mmt_rec.serial_number_control_code in (2,5,6) THEN
2314         x_order_shipment_tbl(l_count).shipped_quantity := 1;
2315       ELSE
2316         x_order_shipment_tbl(l_count).shipped_quantity := l_shipped_qty;
2317       END IF;
2318 
2319       x_order_shipment_tbl(l_count).customer_id        := mmt_rec.end_customer_id; -- mmt_rec.sold_to_org_id ;
2320       x_order_shipment_tbl(l_count).transaction_date   := mmt_rec.transaction_date ;
2321       x_order_shipment_tbl(l_count).item_type_code     := mmt_rec.item_type_code ;
2322       x_order_shipment_tbl(l_count).cust_po_number     := mmt_rec.cust_po_number ;
2323       x_order_shipment_tbl(l_count).ato_line_id        := mmt_rec.ato_line_id ;
2324       x_order_shipment_tbl(l_count).top_model_line_id  := mmt_rec.top_model_line_id ;
2325       x_order_shipment_tbl(l_count).link_to_line_id    := mmt_rec.link_to_line_id ;
2326       x_order_shipment_tbl(l_count).instance_match     := 'N' ;
2327       x_order_shipment_tbl(l_count).quantity_match     := 'N' ;
2328       -- Added lot_match for bug 3384668
2329       x_order_shipment_tbl(l_count).lot_match          := 'N' ;
2330       x_order_shipment_tbl(l_count).invoice_to_contact_id   := mmt_rec.invoice_to_contact_id ;
2331       x_order_shipment_tbl(l_count).ord_line_shipped_qty    :=  mmt_rec.ord_line_shipped_qty;
2332       x_order_shipment_tbl(l_count).inst_obj_version_number :=  l_instance_header_tbl(1).object_version_number;
2333       -- Added for Partner Ordering.
2334       x_order_shipment_tbl(l_count).ib_install_loc_id  := mmt_rec.ib_install_loc_id;
2335       x_order_shipment_tbl(l_count).ib_current_loc_id  := mmt_rec.ib_current_loc_id;
2336       x_order_shipment_tbl(l_count).ib_install_loc     := mmt_rec.ib_install_loc;
2337       x_order_shipment_tbl(l_count).ib_current_loc     := mmt_rec.ib_current_loc;
2338       -- Begin Fix for Bug 3361434
2339       x_order_shipment_tbl(l_count).end_customer_id    := mmt_rec.end_customer_id;
2340       -- End Fix for Bug 3361434
2341       -- Begin Add Code for Siebel Genesis Project
2342       IF (mmt_rec.order_source_id = '28' OR mmt_rec.order_source_id = '29')
2343       THEN
2344          x_order_shipment_tbl(l_count).source_code := 'SIEBEL';
2345       END IF;
2346       -- End Add Code for Siebel Genesis Project
2347     END LOOP;
2348 
2349   EXCEPTION
2350     WHEN fnd_api.g_exc_error THEN
2351       x_return_status := fnd_api.g_ret_sts_error;
2352     WHEN fnd_api.g_exc_unexpected_error THEN
2353       x_return_status := fnd_api.g_ret_sts_unexp_error;
2354   END get_order_shipment_rec;
2355 
2356   /* To build  the matched txn line details if entered for order line .  */
2357   /* If txn line details does not exist then it creates one from the     */
2358   /* shipment details. Also it builds the COMPONENT-OF relationship if   */
2359   /* there is a top model for that order line                            */
2360 
2361   PROCEDURE build_shtd_table(
2362     p_mtl_transaction_id  IN     number,
2363     p_order_line_rec      IN OUT NOCOPY order_line_rec,
2364     p_txn_sub_type_rec    IN     txn_sub_type_rec,
2365     p_trx_detail_exist    IN     boolean,
2366     p_trackable_parent    IN     boolean,
2367     p_transaction_rec     IN OUT NOCOPY csi_datastructures_pub.transaction_rec,
2368     x_order_shipment_tbl     OUT NOCOPY order_shipment_tbl,
2369     px_error_rec          IN OUT NOCOPY csi_datastructures_pub.transaction_error_rec,
2370     x_return_status          OUT NOCOPY varchar2)
2371   IS
2372     x_msg_count                number;
2373     x_msg_data                 varchar2(2000);
2374     x_trx_line_id              number;
2375 
2376     l_txn_line_rec              csi_t_datastructures_grp.txn_line_rec;
2377     l_txn_line_query_rec        csi_t_datastructures_grp.txn_line_query_rec;
2378     l_txn_line_dtl_query_rec    csi_t_datastructures_grp.txn_line_detail_query_rec;
2379 
2380     l_txn_line_detail_tbl       csi_t_datastructures_grp.txn_line_detail_tbl;
2381     l_txn_party_detail_tbl      csi_t_datastructures_grp.txn_party_detail_tbl;
2382     l_txn_pty_acct_detail_tbl   csi_t_datastructures_grp.txn_pty_acct_detail_tbl;
2383     l_txn_ii_rltns_tbl          csi_t_datastructures_grp.txn_ii_rltns_tbl;
2384     l_txn_ext_attrib_vals_tbl   csi_t_datastructures_grp.txn_ext_attrib_vals_tbl;
2385     l_csi_ext_attribs_tbl       csi_t_datastructures_grp.csi_ext_attribs_tbl;
2386     l_extend_attrib_values_tbl  csi_t_datastructures_grp.csi_ext_attrib_vals_tbl;
2387     l_txn_org_assgn_tbl         csi_t_datastructures_grp.txn_org_assgn_tbl  ;
2388     l_txn_systems_tbl           csi_t_datastructures_grp.txn_systems_tbl;
2389 
2390     l_upd_txn_line_rec          csi_t_datastructures_grp.txn_line_rec;
2391     l_upd_txn_line_dtl_tbl      csi_t_datastructures_grp.txn_line_detail_tbl;
2392     l_upd_txn_ii_rltns_tbl      csi_t_datastructures_grp.txn_ii_rltns_tbl;
2393     l_upd_txn_party_detail_tbl  csi_t_datastructures_grp.txn_party_detail_tbl;
2394     l_upd_txn_pty_acct_dtl_tbl  csi_t_datastructures_grp.txn_pty_acct_detail_tbl;
2395     l_upd_txn_org_assgn_tbl     csi_t_datastructures_grp.txn_org_assgn_tbl;
2396     l_upd_txn_ext_attr_vals_tbl csi_t_datastructures_grp.txn_ext_attrib_vals_tbl;
2397 
2398     x_model_inst_tbl            model_inst_tbl;
2399     l_qty_ratio                 number;
2400     l_model_exist               boolean := FALSE;
2401 
2402     l_return_status             varchar2(1) := fnd_api.g_ret_sts_success;
2403     l_msg_count                 number;
2404     l_msg_data                  varchar2(2000);
2405 
2406     -- Included Transaction rec as part of fix for Bug 2767338
2407     l_trx_rec                   csi_datastructures_pub.transaction_rec;
2408 
2409     ---Added (Start) for m-to-m enhancements
2410     l_temp_txn_ii_rltns_tbl   csi_t_datastructures_grp.txn_ii_rltns_tbl;
2411     ---Added (End) for m-to-m enhancements
2412 
2413     l_parent_line_qty           NUMBER := fnd_api.g_miss_num;
2414     --
2415     -- srramakr Added for TSO With Equipment
2416     l_om_session_key            csi_utility_grp.config_session_key;
2417     l_macd_processing           BOOLEAN     := FALSE;
2418 
2419   BEGIN
2420 
2421     x_return_status := fnd_api.g_ret_sts_success;
2422     -- Included Transaction rec as part of fix for Bug 2767338
2423     l_trx_rec := p_transaction_rec;
2424 
2425     api_log('build_shtd_table');
2426 
2427     /* Build the Shipment table for the mtl_trx_id */
2428     -- Included Transaction rec as part of fix for Bug 2767338
2429     get_order_shipment_rec(
2430       p_mtl_transaction_id  => p_mtl_transaction_id,
2431       p_order_line_rec      => p_order_line_rec,
2432       p_txn_sub_type_rec    => p_txn_sub_type_rec,
2433       p_transaction_rec     => l_trx_rec,
2434       x_order_shipment_tbl  => x_order_shipment_tbl,
2435       px_error_rec          => px_error_rec,
2436       x_return_status       => l_return_status );
2437 
2438     IF l_return_status <> fnd_api.g_ret_sts_success THEN
2439       raise fnd_api.g_exc_error;
2440     END IF;
2441 
2442     debug('  order_shipment_tbl.count :'||x_order_shipment_tbl.count);
2443 
2444     /* Get the instances for the option class/model line_id (ATO/PTO) */
2445     IF NVL(p_order_line_rec.link_to_line_id ,fnd_api.g_miss_num) <> fnd_api.g_miss_num THEN
2446 
2447       x_model_inst_tbl.delete;
2448       --
2449       -- srramakr Added for TSO with Equipment
2450       l_om_session_key.session_hdr_id := p_order_line_rec.config_header_id;
2451       l_om_session_key.session_rev_num := p_order_line_rec.config_rev_nbr;
2452       l_om_session_key.session_item_id := p_order_line_rec.configuration_id;
2453       --
2454       l_macd_processing := csi_interface_pkg.check_macd_processing
2455                                ( p_config_session_key => l_om_session_key,
2456                                  x_return_status      => l_return_status
2457                                );
2458       --
2459       IF l_return_status <> fnd_api.g_ret_sts_success THEN
2460          RAISE fnd_api.g_exc_error;
2461       END IF;
2462       --
2463       IF NOT l_macd_processing THEN
2464          csi_utl_pkg.get_model_inst_lst(
2465            p_parent_line_id => p_order_line_rec.link_to_line_id,
2466            x_model_inst_tbl => x_model_inst_tbl,
2467            x_return_status  => l_return_status);
2468 
2469          IF l_return_status <> fnd_api.g_ret_sts_success THEN
2470            raise fnd_api.g_exc_error;
2471          END IF;
2472       ELSE -- MACD Order Line
2473          p_order_line_rec.macd_order_line := FND_API.G_TRUE;
2474       END IF; -- MACD Processing check
2475       -- srramakr End of TSO with Equipment
2476       --
2477       debug('  model_inst_tbl.count :'||x_model_inst_tbl.count);
2478 
2479       IF x_model_inst_tbl.COUNT > 0 THEN
2480         l_model_exist := TRUE;
2481       ELSE
2482         l_model_exist := FALSE;
2483       END IF;
2484 
2485     END IF;
2486 
2487     /* ---------------------------------------------------*/
2488     /*   The following scenarios are handled              */
2489     /*        1.Transaction line Details does not exist   */
2490     /*           1.1 Partial                              */
2491     /*           1.2 Full                                 */
2492     /*           1.3 Config (ATO)                         */
2493     /*           1.4 Option items (PTO)                   */
2494     /*        2.Transaction line details exist            */
2495     /*            1.1 Partial                             */
2496     /*            1.2 Full                                */
2497     /*  --------------------------------------------------*/
2498     IF NOT(p_trx_detail_exist )  THEN
2499       /* Txn details does not exist for the order line*/
2500       IF p_order_line_rec.item_type_code = 'CONFIG' THEN
2501 
2502         create_dflt_td_from_ship(
2503           p_serial_code         => p_order_line_rec.serial_code,
2504           p_order_line_rec      => p_order_line_rec,
2505           p_trackable_parent    => TRUE,
2506           px_order_shipment_tbl => x_order_shipment_tbl,
2507           x_transaction_line_id => x_trx_line_id,
2508           x_return_status       => l_return_status);
2509 
2510         IF l_return_status <> fnd_api.g_ret_sts_success THEN
2511           raise fnd_api.g_exc_error;
2512         END IF;
2513 
2514       ELSIF p_order_line_rec.item_type_code = 'OPTION' THEN
2515 
2516         process_option_item(
2517           p_serial_code        => p_order_line_rec.serial_code,
2518           p_txn_sub_type_rec   => p_txn_sub_type_rec,
2519           p_order_line_rec     => p_order_line_rec,
2520 	  p_trackable_parent   => p_trackable_parent,
2521           x_order_shipment_tbl => x_order_shipment_tbl,
2522           x_model_inst_tbl     => x_model_inst_tbl,
2523           x_trx_line_id        => x_trx_line_id,
2524           x_return_status      => l_return_status);
2525 
2526         IF l_return_status <> fnd_api.g_ret_sts_success THEN
2527           raise fnd_api.g_exc_error;
2528         END IF;
2529 
2530       ELSE
2531 
2532         IF l_model_exist THEN
2533 
2534           process_option_item(
2535             p_serial_code        => p_order_line_rec.serial_code,
2536             p_txn_sub_type_rec   => p_txn_sub_type_rec,
2537             p_order_line_rec     => p_order_line_rec,
2538 	    p_trackable_parent   => p_trackable_parent,
2539             x_order_shipment_tbl => x_order_shipment_tbl,
2540             x_model_inst_tbl     => x_model_inst_tbl,
2541             x_trx_line_id        => x_trx_line_id,
2542             x_return_status      => l_return_status );
2543 
2544           IF l_return_status <> fnd_api.g_ret_sts_success THEN
2545             raise fnd_api.g_exc_error;
2546           END IF;
2547 
2548         ELSE -- create transaction details
2549 
2550           create_dflt_td_from_ship(
2551             p_serial_code         => p_order_line_rec.serial_code,
2552             p_order_line_rec      => p_order_line_rec,
2553             p_trackable_parent    => p_trackable_parent,
2554             px_order_shipment_tbl => x_order_shipment_tbl,
2555             x_transaction_line_id => x_trx_line_id,
2556             x_return_status       => l_return_status);
2557 
2558           IF l_return_status <> fnd_api.g_ret_sts_success THEN
2559             raise fnd_api.g_exc_error;
2560           END IF;
2561 
2562         END IF;
2563 
2564       END IF;
2565 
2566       p_order_line_rec.trx_line_id :=  x_trx_line_id ;
2567 
2568     ELSE
2569 
2570       /*--------------------------------------------*/
2571       /* If txn details exist then Get txn details  */
2572       /* and match it with shipment table           */
2573       /*--------------------------------------------*/
2574 
2575       x_trx_line_id       := p_order_line_rec.trx_line_id;
2576       l_txn_line_query_rec.source_transaction_id    := p_order_line_rec.order_line_id;
2577       l_txn_line_query_rec.source_transaction_table := 'WSH_DELIVERY_DETAILS';
2578       l_txn_line_dtl_query_rec.processing_status    := 'UNPROCESSED';
2579 
2580       debug('Getting installation details for matching with material transaction info.' );
2581 
2582       csi_t_txn_details_grp.get_transaction_details(
2583         p_api_version               => 1.0,
2584         p_commit                    => fnd_api.g_false,
2585         p_init_msg_list             => fnd_api.g_true,
2586         p_validation_level          => fnd_api.g_valid_level_full,
2587         p_txn_line_query_rec        => l_txn_line_query_rec,
2588         p_txn_line_detail_query_rec => l_txn_line_dtl_query_rec,
2589         x_txn_line_detail_tbl       => l_txn_line_detail_tbl,
2590         p_get_parties_flag          => fnd_api.g_false,
2591         x_txn_party_detail_tbl      => l_txn_party_detail_tbl,
2592         p_get_pty_accts_flag        => fnd_api.g_false,
2593         x_txn_pty_acct_detail_tbl   => l_txn_pty_acct_detail_tbl,
2594         p_get_ii_rltns_flag         => fnd_api.g_true,
2595         x_txn_ii_rltns_tbl          => l_txn_ii_rltns_tbl,
2596         p_get_org_assgns_flag       => fnd_api.g_false,
2597         x_txn_org_assgn_tbl         => l_txn_org_assgn_tbl,
2598         p_get_ext_attrib_vals_flag  => fnd_api.g_false,
2599         x_txn_ext_attrib_vals_tbl   => l_txn_ext_attrib_vals_tbl,
2600         p_get_csi_attribs_flag      => fnd_api.g_false,
2601         x_csi_ext_attribs_tbl       => l_csi_ext_attribs_tbl,
2602         p_get_csi_iea_values_flag   => fnd_api.g_false,
2603         x_csi_iea_values_tbl        => l_extend_attrib_values_tbl,
2604         p_get_txn_systems_flag      => fnd_api.g_false,
2605         x_txn_systems_tbl           => l_txn_systems_tbl,
2606         x_return_status             => l_return_status,
2607         x_msg_count                 => l_msg_count,
2608         x_msg_data                  => l_msg_data);
2609 
2610       IF l_return_status <> fnd_api.g_ret_sts_success THEN
2611         raise fnd_api.g_exc_error;
2612       END IF;
2613 
2614       debug('  txn_line_detail_tbl.count :'||l_txn_line_detail_tbl.count );
2615       debug('  txn_ii_rltns_tbl.count    :'||l_txn_ii_rltns_tbl.count );
2616 
2617       IF l_txn_line_detail_tbl.COUNT > 0 THEN
2618 
2619         construct_for_txn_exists(
2620           p_txn_sub_type_rec     => p_txn_sub_type_rec,
2621           p_order_line_rec       => p_order_line_rec,
2622           x_txn_line_detail_tbl  => l_txn_line_detail_tbl,
2623           x_txn_ii_rltns_tbl     => l_txn_ii_rltns_tbl,
2624           x_order_shipment_tbl   => x_order_shipment_tbl,
2625           x_return_status        => l_return_status );
2626 
2627        IF l_return_status <> fnd_api.g_ret_sts_success THEN
2628          raise fnd_api.g_exc_error;
2629        END IF;
2630 
2631        --Added (Start) for m-to-m enhancements
2632        --05/20 For each TLD in ,l_txn_line_detail_tbl
2633        --get the relations and append these relations to l_txn_ii_rltns_tbl.
2634 
2635        csi_utl_pkg.build_txn_relations(
2636          l_txn_line_detail_tbl,
2637          l_temp_txn_ii_rltns_tbl,
2638          l_return_status);
2639 
2640        IF l_return_status <> fnd_api.g_ret_sts_success THEN
2641          raise fnd_api.g_exc_error;
2642        END IF;
2643 
2644        --Create these relations in csi_t_ii_relationships table
2645 
2646        IF l_temp_txn_ii_rltns_tbl.count > 0 THEN
2647          csi_t_txn_rltnshps_grp.create_txn_ii_rltns_dtls(
2648            p_api_version       => 1.0,
2649            p_commit            => fnd_api.g_false,
2650            p_init_msg_list     => fnd_api.g_true,
2651            p_validation_level  => fnd_api.g_valid_level_full,
2652            px_txn_ii_rltns_tbl => l_temp_txn_ii_rltns_tbl,
2653            x_return_status     => l_return_status,
2654            x_msg_count         => x_msg_count,
2655            x_msg_data          => x_msg_data);
2656 
2657          IF l_return_status <> fnd_api.g_ret_sts_success THEN
2658            raise fnd_api.g_exc_error;
2659          END IF;
2660        END IF ; ---l_temp_txn_ii_rltns_tbl.count>0
2661        --added (end) for m-to-m enhancements
2662       ELSE
2663 
2664         IF p_order_line_rec.item_type_code = 'CONFIG' THEN
2665 
2666           create_dflt_td_from_ship(
2667             p_serial_code         => p_order_line_rec.serial_code,
2668             p_order_line_rec      => p_order_line_rec,
2669             p_trackable_parent    => TRUE ,
2670             px_order_shipment_tbl => x_order_shipment_tbl,
2671             x_transaction_line_id => x_trx_line_id,
2672             x_return_status       => l_return_status);
2673 
2674           IF l_return_status <> fnd_api.g_ret_sts_success THEN
2675             raise fnd_api.g_exc_error;
2676           END IF;
2677 
2678         ELSIF p_order_line_rec.item_type_code = 'OPTION' THEN
2679 
2680           process_option_item(
2681             p_serial_code        => p_order_line_rec.serial_code,
2682             p_txn_sub_type_rec   => p_txn_sub_type_rec,
2683             p_order_line_rec     => p_order_line_rec,
2684 	    p_trackable_parent   => p_trackable_parent,
2685             x_order_shipment_tbl => x_order_shipment_tbl,
2686             x_model_inst_tbl     => x_model_inst_tbl,
2687             x_trx_line_id        => x_trx_line_id,
2688             x_return_status      => l_return_status);
2689 
2690           IF l_return_status <> fnd_api.g_ret_sts_success THEN
2691             raise fnd_api.g_exc_error;
2692           END IF;
2693 
2694         ELSE
2695 
2696           create_dflt_td_from_ship(
2697             p_serial_code         => p_order_line_rec.serial_code,
2698             p_order_line_rec      => p_order_line_rec,
2699             p_trackable_parent    => p_trackable_parent,
2700             px_order_shipment_tbl => x_order_shipment_tbl,
2701             x_transaction_line_id => x_trx_line_id,
2702             x_return_status       => l_return_status);
2703 
2704           IF l_return_status <> fnd_api.g_ret_sts_success THEN
2705             raise fnd_api.g_exc_error;
2706           END IF;
2707 
2708        END IF;
2709      END IF;
2710    END IF;
2711 
2712    /*-----------------------------------------------*/
2713    /* If top model exists for that order line       */
2714    /* then build the component-of relationship table*/
2715    /*-----------------------------------------------*/
2716 
2717    IF x_model_inst_tbl.count > 0 THEN
2718 
2719      debug('Instance exists for option class/Model, so building relation' );
2720 
2721      l_txn_line_query_rec.source_transaction_id       := p_order_line_rec.order_line_id;
2722      l_txn_line_query_rec.source_transaction_table    := 'WSH_DELIVERY_DETAILS';
2723      l_txn_line_dtl_query_rec.processing_status       := 'IN_PROCESS';
2724      l_txn_line_dtl_query_rec.source_transaction_flag := 'Y';
2725 
2726      debug('Getting only matched txn line dtls ' );
2727 
2728      csi_t_txn_details_grp.get_transaction_details(
2729         p_api_version           =>   1.0
2730        ,p_commit                =>   fnd_api.g_false
2731        ,p_init_msg_list         =>   fnd_api.g_true
2732        ,p_validation_level      =>   fnd_api.g_valid_level_full
2733        ,p_txn_line_query_rec    =>   l_txn_line_query_rec
2734        ,p_txn_line_detail_query_rec => l_txn_line_dtl_query_rec
2735        ,x_txn_line_detail_tbl   =>   l_txn_line_detail_tbl
2736        ,p_get_parties_flag      =>   fnd_api.g_false
2737        ,x_txn_party_detail_tbl  =>   l_txn_party_detail_tbl
2738        ,p_get_pty_accts_flag    =>   fnd_api.g_false
2739        ,x_txn_pty_acct_detail_tbl => l_txn_pty_acct_detail_tbl
2740         ---Added (Start) for m-to-m enhancements
2741         -- Now get the relations
2742        ,p_get_ii_rltns_flag     =>   fnd_api.g_true
2743         ---Added (End) for m-to-m enhancements
2744        ,x_txn_ii_rltns_tbl      =>   l_txn_ii_rltns_tbl
2745        ,p_get_org_assgns_flag   =>   fnd_api.g_false
2746        ,x_txn_org_assgn_tbl     =>   l_txn_org_assgn_tbl
2747        ,p_get_ext_attrib_vals_flag => fnd_api.g_false
2748        ,x_txn_ext_attrib_vals_tbl => l_txn_ext_attrib_vals_tbl
2749        ,p_get_csi_attribs_flag  =>   fnd_api.g_false
2750        ,x_csi_ext_attribs_tbl   =>   l_csi_ext_attribs_tbl
2751        ,p_get_csi_iea_values_flag => fnd_api.g_false
2752        ,x_csi_iea_values_tbl    =>   l_extend_attrib_values_tbl
2753        ,p_get_txn_systems_flag  =>   fnd_api.g_false
2754        ,x_txn_systems_tbl       =>   l_txn_systems_tbl
2755        ,x_return_status         =>   x_return_status
2756        ,x_msg_count             =>   x_msg_count
2757        ,x_msg_data              =>   x_msg_data      );
2758 
2759      IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
2760        raise fnd_api.g_exc_error;
2761      END IF;
2762 
2763      debug('l_txn_line_detail_tbl.count ='||l_txn_line_detail_tbl.count );
2764 
2765      csi_utl_pkg.get_qty_ratio(
2766        p_order_line_qty   => p_order_line_rec.ordered_quantity,
2767        p_link_to_line_id  => p_order_line_rec.link_to_line_id,
2768        p_order_item_id    => p_order_line_rec.inv_item_id,
2769        p_model_remnant_flag => p_order_line_rec.model_remnant_flag, --added for bug5096435
2770        x_qty_ratio        => l_qty_ratio ,
2771        x_return_status    => l_return_status );
2772 
2773      IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
2774        raise fnd_api.g_exc_error;
2775      END IF;
2776 
2777      debug('l_qty_ratio = '||l_qty_ratio );
2778 
2779      IF x_model_inst_tbl.count > 0 THEN
2780        FOR i in x_model_inst_tbl.first..x_model_inst_tbl.last
2781        LOOP
2782          x_model_inst_tbl(i).rem_qty      := l_qty_ratio;
2783          x_model_inst_tbl(i).process_flag := 'N';
2784        END LOOP;
2785      END IF;
2786 
2787      csi_utl_pkg.build_parent_relation(
2788        p_order_line_rec    => p_order_line_rec,
2789        x_model_inst_tbl    => x_model_inst_tbl,
2790        x_txn_line_dtl_tbl  => l_txn_line_detail_tbl,
2791        x_txn_ii_rltns_tbl  => l_txn_ii_rltns_tbl,
2792        x_return_status     => x_return_status  );
2793 
2794      IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
2795        raise fnd_api.g_exc_error;
2796      END IF;
2797 
2798      csi_t_txn_rltnshps_grp.create_txn_ii_rltns_dtls(
2799        p_api_version       => 1.0,
2800        p_commit            => fnd_api.g_false,
2801        p_init_msg_list     => fnd_api.g_true,
2802        p_validation_level  => fnd_api.g_valid_level_full,
2803        px_txn_ii_rltns_tbl => l_txn_ii_rltns_tbl,
2804        x_return_status     => x_return_status,
2805        x_msg_count         => x_msg_count,
2806        x_msg_data          => x_msg_data);
2807 
2808      IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
2809        g_api_name := 'csi_t_txn_rltnshps_grp.create_txn_ii_rltns_dtls';
2810        raise fnd_api.g_exc_error;
2811      END IF;
2812 
2813    END IF;
2814 
2815    l_txn_line_rec.source_transaction_table := 'WSH_DELIVERY_DETAILS';
2816    l_txn_line_rec.source_transaction_id    := p_order_line_rec.order_line_id;
2817    BEGIN
2818      SELECT transaction_line_id
2819      INTO   l_txn_line_rec.transaction_line_id
2820      FROM   csi_t_transaction_lines
2821      WHERE  source_transaction_id    = l_txn_line_rec.source_transaction_id
2822      AND    source_transaction_table = l_txn_line_rec.source_transaction_table;
2823    EXCEPTION
2824      WHEN no_data_found THEN
2825        null;
2826    END;
2827 
2828   IF NVL(p_order_line_rec.model_remnant_flag,'N') <> 'Y' THEN
2829     csi_utl_pkg.build_child_relation(
2830       p_order_line_rec     => p_order_line_rec,
2831       p_model_txn_line_rec => l_txn_line_rec,
2832       px_csi_txn_rec       => l_trx_rec,
2833       x_return_status      => l_return_status);
2834 
2835     IF l_return_status <> fnd_api.g_ret_sts_success THEN
2836       RAISE fnd_api.g_exc_error;
2837     END IF;
2838 
2839  END IF;
2840   EXCEPTION
2841     WHEN FND_API.G_EXC_ERROR THEN
2842       x_return_status := FND_API.G_RET_STS_ERROR ;
2843     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2844       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2845   END build_shtd_table;
2846 
2847 
2848   PROCEDURE check_return_processing(
2849     px_txn_line_detail_tbl  IN OUT NOCOPY csi_t_datastructures_grp.txn_line_detail_tbl,
2850     x_return_status         OUT NOCOPY    varchar2)
2851   IS
2852     l_return_status         varchar2(1) := fnd_api.g_ret_sts_success;
2853     l_instance_usage_code   varchar2(30) := 'NULL';
2854   BEGIN
2855 
2856     x_return_status := fnd_api.g_ret_sts_success;
2857 
2858     api_log('check_return_processing');
2859 
2860     IF px_txn_line_detail_tbl.COUNT > 0 THEN
2861       FOR l_ind IN px_txn_line_detail_tbl.FIRST .. px_txn_line_detail_tbl.LAST
2862       LOOP
2863         IF px_txn_line_detail_tbl(l_ind).source_transaction_flag = 'Y'
2864            AND
2865            nvl(px_txn_line_detail_tbl(l_ind).instance_id,fnd_api.g_miss_num) <> fnd_api.g_miss_num
2866         THEN
2867 
2868           debug('  referred instance id :'||px_txn_line_detail_tbl(l_ind).instance_id);
2869 
2870           BEGIN
2871             SELECT instance_usage_code
2872             INTO   l_instance_usage_code
2873             FROM   csi_item_instances
2874             WHERE  instance_id = px_txn_line_detail_tbl(l_ind).instance_id;
2875           EXCEPTION
2876             WHEN no_data_found THEN
2877               l_instance_usage_code := 'NULL';
2878           END;
2879 
2880           debug('  instance usage code  :'||l_instance_usage_code);
2881 
2882           IF l_instance_usage_code = 'RETURNED' THEN
2883             px_txn_line_detail_tbl(l_ind).changed_instance_id :=
2884                                          px_txn_line_detail_tbl(l_ind).instance_id ;
2885             px_txn_line_detail_tbl(l_ind).instance_id         := null;
2886           END IF;
2887         END IF;
2888       END LOOP;
2889     END IF;
2890   EXCEPTION
2891     WHEN fnd_api.g_exc_error THEN
2892       x_return_status := fnd_api.g_ret_sts_error;
2893     WHEN fnd_api.g_exc_unexpected_error THEN
2894       x_return_status := fnd_api.g_ret_sts_unexp_error;
2895   END check_return_processing;
2896 
2897   /*---------------------------------------------------------------*/
2898   /* Procedure does matching of txn line details with the shipment */
2899   /*---------------------------------------------------------------*/
2900   PROCEDURE construct_for_txn_exists(
2901     p_txn_sub_type_rec        IN  txn_sub_type_rec,
2902     p_order_line_rec          IN order_line_rec,
2903     x_order_shipment_tbl      IN OUT NOCOPY order_shipment_tbl,
2904     x_txn_ii_rltns_tbl        IN OUT NOCOPY csi_t_datastructures_grp.txn_ii_rltns_tbl,
2905     x_txn_line_detail_tbl     IN OUT NOCOPY csi_t_datastructures_grp.txn_line_detail_tbl,
2906     x_return_status           OUT NOCOPY varchar2)
2907   IS
2908     l_return_status           varchar2(1) := fnd_api.g_ret_sts_success;
2909   BEGIN
2910 
2911     /* Initialize API return status to success */
2912     x_return_status := fnd_api.g_ret_sts_success;
2913 
2914     api_log('construct_for_txn_exists');
2915 
2916     /* brmanesh - hooking this procedure to check if the instance referenced is a
2917        returned configured non serial instance . If yes then save this instance id in
2918        the changed_instance_id column in installation details and mark the record for
2919        return processing
2920     */
2921     check_return_processing(
2922       px_txn_line_detail_tbl  => x_txn_line_detail_tbl,
2923       x_return_status         => l_return_status);
2924 
2925     IF l_return_status <> fnd_api.g_ret_sts_success THEN
2926       RAISE fnd_api.g_exc_error;
2927     END IF;
2928 
2929     match_txn_with_ship(
2930       p_serial_code             => p_order_line_rec.serial_code,
2931       x_txn_line_detail_tbl     => x_txn_line_detail_tbl,
2932       x_order_shipment_tbl      => x_order_shipment_tbl,
2933       x_return_status           => l_return_status );
2934 
2935     IF l_return_status <> fnd_api.g_ret_sts_success THEN
2936       raise fnd_api.g_exc_error;
2937     END IF;
2938 
2939     process_txn_dtl(
2940       p_serial_code             => p_order_line_rec.serial_code,
2941       p_txn_sub_type_rec        => p_txn_sub_type_rec,
2942       p_order_line_rec          => p_order_line_rec,
2943       x_txn_line_detail_tbl     => x_txn_line_detail_tbl,
2944       x_txn_ii_rltns_tbl        => x_txn_ii_rltns_tbl,
2945       x_order_shipment_tbl      => x_order_shipment_tbl,
2946       x_return_status           => l_return_status );
2947 
2948     IF l_return_status <> fnd_api.g_ret_sts_success THEN
2949       raise fnd_api.g_exc_error;
2950     END IF;
2951 
2952   EXCEPTION
2953     WHEN FND_API.G_EXC_ERROR THEN
2954       x_return_status := FND_API.G_RET_STS_ERROR ;
2955     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2956       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2957   END construct_for_txn_exists;
2958 
2959   /* ------------------------------------------------------------------------*/
2960   /* procedure compare if the inventory instance and the qty on the shipment */
2961   /* material transaction matches with the installation details and marks    */
2962   /* ------------------------------------------------------------------------*/
2963 
2964   PROCEDURE match_txn_with_ship(
2965     p_serial_code             IN number,
2966     x_txn_line_detail_tbl     IN OUT NOCOPY csi_t_datastructures_grp.txn_line_detail_tbl,
2967     x_order_shipment_tbl      IN OUT NOCOPY order_shipment_tbl,
2968     x_return_status           OUT NOCOPY varchar2)
2969   IS
2970     l_return_status           varchar2(1) := fnd_api.g_ret_sts_success;
2971   BEGIN
2972 
2973     x_return_status := fnd_api.g_ret_sts_success;
2974 
2975     api_log('match_txn_with_ship');
2976     debug('  order_shipment_tbl.count  :'||x_order_shipment_tbl.count );
2977     debug('  txn_line_detail_tbl.count :'||x_txn_line_detail_tbl.count);
2978 
2979     debug('Trying to match with the instance id.' );
2980 
2981     IF x_order_shipment_tbl.count > 0 THEN
2982       FOR i IN x_order_shipment_tbl.FIRST..x_order_shipment_tbl.LAST LOOP
2983 
2984         IF x_txn_line_detail_tbl.count > 0 THEN
2985           FOR j  IN x_txn_line_detail_tbl.FIRST..x_txn_line_detail_tbl.LAST LOOP
2986 
2987             IF x_txn_line_detail_tbl(j).source_transaction_flag = 'Y'
2988                AND
2989                x_txn_line_detail_tbl(j).processing_status in ('SUBMIT','ERROR')
2990             THEN
2991 
2992               debug('  td line_detail_id    :'||x_txn_line_detail_tbl(j).txn_line_detail_id);
2993               debug('  td instance_id       :'||x_txn_line_detail_tbl(j).instance_id);
2994               debug('  td quantity          :'||x_txn_line_detail_tbl(j).quantity);
2995 
2996               IF x_order_shipment_tbl(i).instance_id =
2997                                          NVL(x_txn_line_detail_tbl(j).instance_id,-1)
2998               THEN
2999 
3000                 IF x_order_shipment_tbl(i).shipped_quantity =
3001                                            x_txn_line_detail_tbl(j).quantity
3002                 THEN
3003                   x_txn_line_detail_tbl(j).processing_status := 'MATCH' ;
3004                   x_order_shipment_tbl(i).instance_match     := 'Y';
3005                   x_order_shipment_tbl(i).quantity_match     := 'Y';
3006                   -- Added lot_match for Bug 3384668
3007                   x_order_shipment_tbl(i).lot_match          := 'N';
3008                   x_order_shipment_tbl(i).txn_line_detail_id := x_txn_line_detail_tbl(j).txn_line_detail_id;
3009                   debug('INSTANCE AND QTY MATCH');
3010                   exit;
3011                 ELSE
3012                   x_order_shipment_tbl(i).instance_match     := 'Y';
3013                   x_order_shipment_tbl(i).quantity_match     := 'N';
3014                   -- Added lot_match for Bug 3384668
3015                   x_order_shipment_tbl(i).lot_match          := 'N';
3016                   x_txn_line_detail_tbl(j).processing_status := 'INST_MATCH' ;
3017                   x_order_shipment_tbl(i).txn_line_detail_id :=  x_txn_line_detail_tbl(j).txn_line_detail_id;
3018                   debug('INSTANCE MATCH');
3019                 END IF;
3020               END IF;
3021             END IF; -- processing status <> 'in_process'
3022           END LOOP; -- end of processing txn table
3023         END IF;
3024       END LOOP; -- end loop processing  shipment table
3025     END IF;  -- end if shipment table
3026 
3027     -- Begin fix for BUg 3384668
3028     debug('Trying to match with Lot Number ');
3029     IF x_order_shipment_tbl.count > 0
3030     THEN
3031       FOR i IN x_order_shipment_tbl.FIRST..x_order_shipment_tbl.LAST
3032       LOOP
3033         IF x_order_shipment_tbl(i).instance_match = 'N'
3034           AND
3035            x_order_shipment_tbl(i).quantity_match = 'N'
3036           AND
3037            x_order_shipment_tbl(i).lot_match      = 'N'
3038         THEN
3039           IF x_txn_line_detail_tbl.count > 0
3040           THEN
3041             FOR j  IN x_txn_line_detail_tbl.FIRST..x_txn_line_detail_tbl.LAST
3042             LOOP
3043               IF (x_txn_line_detail_tbl(j).source_transaction_flag = 'Y')
3044                 AND
3045                  (x_txn_line_detail_tbl(j).processing_status in ('ERROR','NO_MATCH','SUBMIT'))
3046               THEN
3047                 IF x_order_shipment_tbl(i).lot_number = NVL(x_txn_line_detail_tbl(j).lot_number,-1)
3048                 THEN
3049                   debug('LOT MATCH for :'||x_txn_line_detail_tbl(j).lot_number);
3050                   debug('td line_detail_id    :'||x_txn_line_detail_tbl(j).txn_line_detail_id);
3051                   debug('td instance_id       :'||x_txn_line_detail_tbl(j).instance_id);
3052                   debug('td quantity          :'||x_txn_line_detail_tbl(j).quantity);
3053 
3054                   IF x_order_shipment_tbl(i).shipped_quantity = x_txn_line_detail_tbl(j).quantity
3055                   THEN
3056                     x_order_shipment_tbl(i).instance_match     := 'N';
3057                     x_order_shipment_tbl(i).lot_match          := 'Y';
3058                     x_order_shipment_tbl(i).quantity_match     := 'Y';
3059                     x_txn_line_detail_tbl(j).processing_status := 'MATCH' ;
3060                     x_order_shipment_tbl(i).txn_line_detail_id := x_txn_line_detail_tbl(j).txn_line_detail_id;
3061                     debug('LOT and QUANTITY Match');
3062                     exit;
3063                   ELSE
3064                     x_order_shipment_tbl(i).instance_match     := 'N';
3065                     x_order_shipment_tbl(i).lot_match          := 'Y';
3066                     x_order_shipment_tbl(i).quantity_match     := 'N';
3067                     x_txn_line_detail_tbl(j).processing_status := 'LOT_MATCH' ;
3068                     x_order_shipment_tbl(i).txn_line_detail_id := x_txn_line_detail_tbl(j).txn_line_detail_id;
3069                     debug('LOT Match');
3070                   END IF;
3071                 END IF;
3072               END IF;
3073             END LOOP;
3074           END IF;
3075         END IF;
3076       END LOOP;
3077     END IF;
3078     -- End fix for BUg 3384668
3079 
3080     debug('Trying to match with the quantity.' );
3081 
3082     IF x_order_shipment_tbl.count > 0 THEN
3083       FOR i IN x_order_shipment_tbl.FIRST..x_order_shipment_tbl.LAST LOOP
3084 
3085         IF x_order_shipment_tbl(i).instance_match = 'N'
3086            and
3087            x_order_shipment_tbl(i).quantity_match = 'N'
3088            -- Added the followinh and condition for Bug 3384668
3089            and
3090            x_order_shipment_tbl(i).lot_match      = 'N'
3091         THEN
3092 
3093           IF x_txn_line_detail_tbl.count > 0 THEN
3094             FOR j  IN x_txn_line_detail_tbl.FIRST..x_txn_line_detail_tbl.LAST LOOP
3095 
3096               IF (x_txn_line_detail_tbl(j).source_transaction_flag = 'Y')
3097                   AND
3098                  (x_txn_line_detail_tbl(j).processing_status in ('ERROR','NO_MATCH','SUBMIT'))
3099               THEN
3100 
3101                 debug('  td line_detail_id    :'||x_txn_line_detail_tbl(j).txn_line_detail_id);
3102                 debug('  td instance_id       :'||x_txn_line_detail_tbl(j).instance_id);
3103                 debug('  td quantity          :'||x_txn_line_detail_tbl(j).quantity);
3104 
3105                 IF x_order_shipment_tbl(i).instance_id <> NVL(x_txn_line_detail_tbl(j).instance_id,-1)
3106                   -- added the following and condition for bug 3384668
3107                   and
3108                   -- added nvl for Bug 3523567
3109                    nvl(x_order_shipment_tbl(i).lot_number,'@#$') <> nvl(x_txn_line_detail_tbl(j).lot_number,'$#@')
3110                 THEN
3111                   IF x_order_shipment_tbl(i).shipped_quantity = x_txn_line_detail_tbl(j).quantity
3112                   THEN
3113 
3114                     x_order_shipment_tbl(i).instance_match     :=  'N';
3115                     x_order_shipment_tbl(i).quantity_match     :=  'Y';
3116                     -- Added lot_match for Bug 3384668
3117                     x_order_shipment_tbl(i).lot_match          :=  'N';
3118                     x_txn_line_detail_tbl(j).processing_status :=  'QTY_MATCH' ;
3119                     x_order_shipment_tbl(i).txn_line_detail_id :=   x_txn_line_detail_tbl(j).txn_line_detail_id;
3120                     debug('QTY MATCH');
3121                     exit;
3122                   ELSE
3123                     x_order_shipment_tbl(i).instance_match     :=  'N';
3124                     x_order_shipment_tbl(i).quantity_match     :=  'N';
3125                     -- Added lot_match for Bug 3384668
3126                     x_order_shipment_tbl(i).lot_match          :=  'N';
3127                     x_txn_line_detail_tbl(j).processing_status :=  'NO_MATCH' ;
3128                     debug('NEITHER INSTANCE, LOT NUMBER OR QTY MATCH  ');
3129                   END IF;
3130                 END IF;
3131               END IF; -- processing status <> 'in_process'
3132             END LOOP; -- end of processing txn table
3133           END IF;
3134         END IF; -- end if inst_match = N and qty_match = N
3135       END LOOP; -- end of processing shipment table
3136     END IF;
3137   EXCEPTION
3138     WHEN FND_API.G_EXC_ERROR THEN
3139       x_return_status := FND_API.G_RET_STS_ERROR ;
3140     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3141       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3142   END match_txn_with_ship;
3143 
3144   /*----------------------------------------------------------*/
3145   /* Description   :  Procedure that matches the unresolved   */
3146   /*   the txn line details and updates the txn line dtls     */
3147   /*   with the processing_status as "IN_PROCESS"             */
3148   /*----------------------------------------------------------*/
3149 
3150   PROCEDURE process_txn_dtl(
3151     p_serial_code             IN NUMBER,
3152     p_txn_sub_type_rec        IN txn_sub_type_rec,
3153     p_order_line_rec          IN order_line_rec,
3154     x_txn_line_detail_tbl     IN OUT NOCOPY csi_t_datastructures_grp.txn_line_detail_tbl,
3155     x_txn_ii_rltns_tbl        IN csi_t_datastructures_grp.txn_ii_rltns_tbl,
3156     x_order_shipment_tbl      IN OUT NOCOPY order_shipment_tbl,
3157     x_return_status           OUT NOCOPY varchar2)
3158   IS
3159 
3160     x_msg_count                 number ;
3161     x_msg_data                  varchar2(2000);
3162     l_upd                       number  := 1;
3163     l_exit_flag                 boolean := FALSE;
3164     l_config_exists             boolean := FALSE;
3165     l_tmp                       number;
3166     l_proc_qty                  number;
3167     l_total_proc_qty            number;
3168     l_rem_qty			number; --fix for bug 4354267
3169 
3170     l_upd_txn_line_rec          csi_t_datastructures_grp.txn_line_rec;
3171     l_upd_txn_line_dtl_tbl      csi_t_datastructures_grp.txn_line_detail_tbl;
3172     l_upd_txn_ii_rltns_tbl      csi_t_datastructures_grp.txn_ii_rltns_tbl;
3173     l_upd_txn_party_detail_tbl  csi_t_datastructures_grp.txn_party_detail_tbl;
3174     l_upd_txn_pty_acct_dtl_tbl  csi_t_datastructures_grp.txn_pty_acct_detail_tbl;
3175     l_upd_txn_org_assgn_tbl     csi_t_datastructures_grp.txn_org_assgn_tbl;
3176     l_upd_txn_ext_attr_vals_tbl csi_t_datastructures_grp.txn_ext_attrib_vals_tbl;
3177 
3178     l_return_status             varchar2(1) := fnd_api.g_ret_sts_success;
3179     l_msg_count                 number;
3180     l_msg_data                  varchar2(2000);
3181 
3182   BEGIN
3183 
3184     /* Initialize API return status to success */
3185     x_return_status := fnd_api.g_ret_sts_success;
3186 
3187     api_log('process_txn_dtl');
3188 
3189     /* assign values for the columns in l_upd_txn_line_rec */
3190     l_upd_txn_line_rec.transaction_line_id        := p_order_line_rec.trx_line_id;
3191     l_upd_txn_line_rec.source_transaction_id      := p_order_line_rec.order_line_id;
3192     l_upd_txn_line_rec.source_transaction_type_id := g_txn_type_id;
3193     l_upd_txn_line_rec.source_transaction_table   := 'WSH_DELIVERY_DETAILS';
3194 
3195     /* Initialize the pl/sql tables used for processing */
3196     l_upd_txn_line_dtl_tbl.delete;
3197     l_upd_txn_ii_rltns_tbl.delete;
3198     l_upd_txn_party_detail_tbl.delete;
3199     l_upd_txn_pty_acct_dtl_tbl.delete ;
3200     l_upd_txn_org_assgn_tbl.delete;
3201     l_upd_txn_ext_attr_vals_tbl.delete;
3202 
3203     debug('  order_shipment_tbl.count  :'||x_order_shipment_tbl.count);
3204     debug('  txn_line_detail_tbl.count :'||x_txn_line_detail_tbl.count);
3205 
3206     IF x_order_shipment_tbl.count > 0 THEN
3207       FOR i IN x_order_shipment_tbl.FIRST..x_order_shipment_tbl.LAST
3208       LOOP
3209 
3210         IF ( x_order_shipment_tbl(i).instance_match  = 'Y'
3211              -- Added the or condition for bug 3384668
3212              OR
3213              x_order_shipment_tbl(i).lot_match = 'Y'
3214            )
3215            AND
3216            ( x_order_shipment_tbl(i).quantity_match  = 'N' )
3217         THEN
3218 
3219           debug('INSTANCE/LOT MATCH BUT QTY DOES NOT MATCH');
3220 
3221           IF x_txn_line_detail_tbl.count > 0 THEN
3222             FOR j in x_txn_line_detail_tbl.first..x_txn_line_detail_tbl.last
3223             LOOP
3224 
3225               IF x_txn_line_detail_tbl(j).source_transaction_flag = 'Y' AND
3226                  x_txn_line_detail_tbl(j).processing_status <> 'IN_PROCESS' THEN
3227 
3228                 IF (x_txn_line_detail_tbl(j).txn_line_detail_id = x_order_shipment_tbl(i).txn_line_detail_id)
3229                   AND
3230                    (x_txn_line_detail_tbl(j).processing_status = 'INST_MATCH'
3231                     -- Added the or condition for bug 3384668
3232                     OR
3233                     x_txn_line_detail_tbl(j).processing_status = 'LOT_MATCH'
3234                    )
3235                 THEN
3236 
3237                   debug('Checking if Config exists..  ');
3238 
3239                   l_config_exists := csi_utl_pkg.Check_config_exists(
3240                                        x_txn_ii_rltns_tbl,
3241                                        x_txn_line_detail_tbl(j).txn_line_detail_id);
3242 
3243                   IF l_config_exists AND
3244                      x_txn_line_detail_tbl(j).preserve_detail_flag = 'N' THEN
3245 
3246                     debug('Config exists so ignoring txn_dtls and creating one  ');
3247 
3248                     /* update the txn_line_detail as ERROR and create new txn_line_dtls*/
3249 
3250                     l_upd_txn_line_dtl_tbl(l_upd).txn_line_detail_id := x_txn_line_detail_tbl(j).txn_line_detail_id;
3251                     l_upd_txn_line_dtl_tbl(l_upd).processing_status  := 'ERROR';
3252 
3253                     csi_utl_pkg.create_txn_details(
3254                       x_txn_line_dtl_rec   => x_txn_line_detail_tbl(j),
3255                       p_txn_sub_type_rec   => p_txn_sub_type_rec,
3256                       p_order_shipment_rec => x_order_shipment_tbl(i),
3257                       p_order_line_rec     => p_order_line_rec,
3258                       x_return_status      => x_return_status);
3259 
3260                     IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
3261                       raise fnd_api.g_exc_error;
3262                     END IF;
3263 
3264                     l_upd := l_upd +1;
3265 
3266                     x_order_shipment_tbl(i).quantity_match := 'Y';
3267                     -- Added the IF/ELSE for 3384668
3268                     -- x_order_shipment_tbl(i).instance_match := 'Y';
3269                     IF x_order_shipment_tbl(i).instance_match  = 'Y'
3270                     THEN
3271                       x_order_shipment_tbl(i).instance_match := 'Y';
3272                     ELSE
3273                       x_order_shipment_tbl(i).lot_match := 'Y';
3274                     END IF;
3275 
3276                     x_txn_line_detail_tbl(j).processing_status := 'IN_PROCESS';
3277                     exit;
3278                   ELSE
3279 
3280                     IF x_order_shipment_tbl(i).shipped_quantity > x_txn_line_detail_tbl(j).quantity THEN
3281 
3282                       debug('shipped_qty > txn_dtl_qty  ');
3283 
3284                       /*---------------------------------------------------------*/
3285                       /* Process the txn_line_dtl and check if there are other   */
3286                       /* txn_line_dtls with the same instance. Else create a     */
3287                       /* txn_line_dtl with the shipment details for remaining qty*/
3288                       /*---------------------------------------------------------*/
3289 
3290                       l_upd_txn_line_dtl_tbl(l_upd).txn_line_detail_id := x_txn_line_detail_tbl(j).txn_line_detail_id;
3291                       l_upd_txn_line_dtl_tbl(l_upd).preserve_detail_flag := 'Y';
3292                       l_upd_txn_line_dtl_tbl(l_upd).serial_number     := x_order_shipment_tbl(i).serial_number;
3293                       l_upd_txn_line_dtl_tbl(l_upd).lot_number        := x_order_shipment_tbl(i).lot_number;
3294                       l_upd_txn_line_dtl_tbl(l_upd).inv_organization_id := x_order_shipment_tbl(i).organization_id;
3295                       l_upd_txn_line_dtl_tbl(l_upd).processing_status := 'IN_PROCESS';
3296                       l_upd_txn_line_dtl_tbl(l_upd).changed_instance_id := x_txn_line_detail_tbl(j).changed_instance_id;
3297 
3298                       -- Begin fix for Bug 3384668
3299                       l_upd_txn_line_dtl_tbl(l_upd).inventory_item_id   := x_order_shipment_tbl(i).inventory_item_id;
3300                       l_upd_txn_line_dtl_tbl(l_upd).instance_id         := x_order_shipment_tbl(i).instance_id;
3301                       -- End fix for Bug 3384668
3302 
3303                       /* Begin fix for Bug 2972082 cascade owner flag */
3304                       l_upd_txn_line_dtl_tbl(l_upd).cascade_owner_flag := x_txn_line_detail_tbl(j).cascade_owner_flag;
3305                       /* end fix for Bug 2972082 cascade owner flag */
3306 
3307                       l_proc_qty := x_order_shipment_tbl(i).shipped_quantity - x_txn_line_detail_tbl(j).quantity;
3308                       x_txn_line_detail_tbl(j).processing_status := 'IN_PROCESS';
3309 
3310                       debug('Remaining qty to process ='||l_proc_qty);
3311 
3312                       csi_utl_pkg.split_ship_rec(
3313                         x_upd_txn_line_dtl_tbl    => l_upd_txn_line_dtl_tbl,
3314                         x_txn_line_detail_tbl     => x_txn_line_detail_tbl,
3315                         x_txn_line_detail_rec     => x_txn_line_detail_tbl(j),
3316                         p_txn_sub_type_rec        => p_txn_sub_type_rec,
3317                         p_order_shipment_rec      => x_order_shipment_tbl(i),
3318                         p_order_line_rec          => p_order_line_rec,
3319                         p_proc_qty                => l_proc_qty,
3320                         x_return_status           => x_return_status );
3321 
3322                       IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
3323                         raise fnd_api.g_exc_error;
3324                       END IF;
3325 
3326                       debug('After split_ship_rec ');
3327                       debug('  upd_txn_line_dtl_tbl.count  : '||l_upd_txn_line_dtl_tbl.count);
3328                       debug('  txn_line_detail_tbl.count   : '||x_txn_line_detail_tbl.count);
3329 
3330                       x_order_shipment_tbl(i).quantity_match := 'Y';
3331                       -- Added the IF/ELSE for 3384668
3332                       -- x_order_shipment_tbl(i).instance_match := 'Y';
3333                       IF x_order_shipment_tbl(i).instance_match  = 'Y'
3334                       THEN
3335                         x_order_shipment_tbl(i).instance_match := 'Y';
3336                       ELSE
3337                         x_order_shipment_tbl(i).lot_match := 'Y';
3338                       END IF;
3339 
3340                       l_upd := l_upd +1;
3341 
3342                       exit;
3343 
3344                     ELSIF x_txn_line_detail_tbl(j).quantity > x_order_shipment_tbl(i).shipped_quantity THEN
3345 
3346                       debug('shipped_qty < txn_dtl_qty  ');
3347 
3348                       /* update the txn_detail with the qty of difference between txn_line_dtls and shipment*/
3349 
3350                       l_upd_txn_line_dtl_tbl(l_upd).txn_line_detail_id   := x_txn_line_detail_tbl(j).txn_line_detail_id;
3351                       -- Begin change for Bug 3384668
3352                       -- l_upd_txn_line_dtl_tbl(l_upd).quantity
3353                                                --  := x_txn_line_detail_tbl(j).quantity - x_order_shipment_tbl(i).shipped_quantity;
3354                       l_upd_txn_line_dtl_tbl(l_upd).quantity             := x_order_shipment_tbl(i).shipped_quantity;
3355                       -- End change for Bug 3384668
3356                       l_upd_txn_line_dtl_tbl(l_upd).inv_organization_id  := x_order_shipment_tbl(i).organization_id;
3357                       l_upd_txn_line_dtl_tbl(l_upd).serial_number        := x_order_shipment_tbl(i).serial_number;
3358                       l_upd_txn_line_dtl_tbl(l_upd).lot_number           := x_order_shipment_tbl(i).lot_number;
3359                       l_upd_txn_line_dtl_tbl(l_upd).processing_status    := 'IN_PROCESS';
3360                       l_upd_txn_line_dtl_tbl(l_upd).preserve_detail_flag := 'Y';
3361                       l_upd_txn_line_dtl_tbl(l_upd).changed_instance_id  :=
3362                              x_txn_line_detail_tbl(j).changed_instance_id;
3363 
3364                       -- Begin fix for Bug 3384668
3365                       l_upd_txn_line_dtl_tbl(l_upd).inventory_item_id   := x_order_shipment_tbl(i).inventory_item_id;
3366                       l_upd_txn_line_dtl_tbl(l_upd).instance_id         := x_order_shipment_tbl(i).instance_id;
3367                       -- End fix for Bug 3384668
3368 
3369                       /* Begin fix for Bug 2972082 cascade owner flag */
3370                       l_upd_txn_line_dtl_tbl(l_upd).cascade_owner_flag := x_txn_line_detail_tbl(j).cascade_owner_flag;
3371                       /* end fix for Bug 2972082 cascade owner flag */
3372 
3373                       debug('shipped_qty ='||x_order_shipment_tbl(i).shipped_quantity);
3374                       debug('txn_qty     ='||x_txn_line_detail_tbl(j).quantity);
3375 
3376                       /* create_txn_line_dtls with the shipped qty */
3377                       csi_utl_pkg.create_txn_details(
3378                         x_txn_line_dtl_rec        => x_txn_line_detail_tbl(j),
3379                         p_txn_sub_type_rec        => p_txn_sub_type_rec,
3380                         p_order_shipment_rec      => x_order_shipment_tbl(i),
3381                         p_order_line_rec          => p_order_line_rec,
3382                         x_return_status           => x_return_status );
3383 
3384                       IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
3385                         raise fnd_api.g_exc_error;
3386                       END IF;
3387 
3388                       UPDATE csi_t_txn_line_details
3389                       SET processing_status = 'SUBMIT'
3390                       WHERE txn_line_detail_id = x_txn_line_detail_tbl(j).txn_line_detail_id;
3391 
3392                       l_upd := l_upd +1;
3393 
3394                       x_order_shipment_tbl(i).quantity_match := 'Y';
3395                       -- Added the IF/ELSE for 3384668
3396                       -- x_order_shipment_tbl(i).instance_match := 'Y';
3397                       IF x_order_shipment_tbl(i).instance_match  = 'Y'
3398                       THEN
3399                         x_order_shipment_tbl(i).instance_match := 'Y';
3400                       ELSE
3401                         x_order_shipment_tbl(i).lot_match := 'Y';
3402                       END IF;
3403                       x_txn_line_detail_tbl(j).processing_status := 'IN_PROCESS';
3404 
3405                       exit;
3406                     END IF;
3407                   END IF;
3408                 END IF;
3409               END IF;
3410             END LOOP;
3411           END IF;
3412         ELSIF ( x_order_shipment_tbl(i).instance_match  = 'Y'
3413                -- added the or condition for bug 3384668
3414                 OR
3415                 x_order_shipment_tbl(i).lot_match = 'Y'
3416               )
3417               AND
3418               ( x_order_shipment_tbl(i).quantity_match  = 'Y' )
3419         THEN
3420 
3421           debug('BOTH INSTANCE/LOT AND QTY MATCH ');
3422 
3423           IF x_txn_line_detail_tbl.count > 0 THEN
3424             FOR j in x_txn_line_detail_tbl.first..x_txn_line_detail_tbl.last LOOP
3425 
3426               -- debug('txn_line_detail_id ='||x_txn_line_detail_tbl(j).txn_line_detail_id);
3427 
3428               IF x_txn_line_detail_tbl(j).source_transaction_flag = 'Y' AND
3429                  x_txn_line_detail_tbl(j).processing_status <> 'IN_PROCESS' THEN
3430 
3431                 IF (x_txn_line_detail_tbl(j).txn_line_detail_id = x_order_shipment_tbl(i).txn_line_detail_id ) AND
3432                    (x_txn_line_detail_tbl(j).processing_status  = 'MATCH'  ) THEN
3433 
3434                   /* Process the txn_line_dtls as both instance and qty matches */
3435 
3436                   debug('Processing the txn_dtl  ');
3437 
3438                   l_upd_txn_line_dtl_tbl(l_upd).txn_line_detail_id := x_txn_line_detail_tbl(j).txn_line_detail_id;
3439                   l_upd_txn_line_dtl_tbl(l_upd).processing_status := 'IN_PROCESS' ;
3440                   l_upd_txn_line_dtl_tbl(l_upd).preserve_detail_flag := 'Y';
3441                   l_upd_txn_line_dtl_tbl(l_upd).serial_number     := x_order_shipment_tbl(i).serial_number;
3442                   l_upd_txn_line_dtl_tbl(l_upd).lot_number        := x_order_shipment_tbl(i).lot_number;
3443                   l_upd_txn_line_dtl_tbl(l_upd).inv_organization_id := x_order_shipment_tbl(i).organization_id;
3444 
3445                   -- Begin fix for Bug 3384668
3446                   l_upd_txn_line_dtl_tbl(l_upd).inventory_item_id   := x_order_shipment_tbl(i).inventory_item_id;
3447                   l_upd_txn_line_dtl_tbl(l_upd).instance_id         := x_order_shipment_tbl(i).instance_id;
3448                   l_upd_txn_line_dtl_tbl(l_upd).changed_instance_id := x_txn_line_detail_tbl(j).changed_instance_id;
3449                   -- End fix for Bug 3384668
3450 
3451                   /* Begin fix for Bug 2972082 cascade owner flag */
3452                   l_upd_txn_line_dtl_tbl(l_upd).cascade_owner_flag := x_txn_line_detail_tbl(j).cascade_owner_flag;
3453                   /* end fix for Bug 2972082 cascade owner flag */
3454 
3455                   l_upd := l_upd +1;
3456 
3457                   x_txn_line_detail_tbl(j).processing_status := 'IN_PROCESS';
3458                   x_order_shipment_tbl(i).quantity_match := 'Y';
3459 
3460                   -- Begin fix for Bug 3384668
3461                   IF x_order_shipment_tbl(i).lot_match = 'Y'
3462                   THEN
3463                      x_order_shipment_tbl(i).lot_match := 'Y';
3464                   ELSE
3465                   -- End fix for Bug 3384668
3466                      x_order_shipment_tbl(i).instance_match := 'Y';
3467                   END IF;
3468                   exit;
3469                 END IF;
3470               END IF;
3471 
3472             END LOOP;
3473           END IF;
3474 
3475         ELSIF (x_order_shipment_tbl(i).instance_match   = 'N' )
3476                -- added the lot_match and condition for Bug 3384668
3477                AND
3478               (x_order_shipment_tbl(i).lot_match   = 'N' )
3479                AND
3480               (x_order_shipment_tbl(i).quantity_match   = 'Y' )
3481         THEN
3482 
3483           debug('QTY MATCH BUT INSTANCE/LOT DOES NOT MATCH ');
3484 
3485           IF x_txn_line_detail_tbl.count > 0 THEN
3486             FOR j in x_txn_line_detail_tbl.first..x_txn_line_detail_tbl.last LOOP
3487 
3488             IF x_txn_line_detail_tbl(j).source_transaction_flag = 'Y'
3489                AND
3490                x_txn_line_detail_tbl(j).processing_status <> 'IN_PROCESS'
3491             THEN
3492 
3493               IF x_txn_line_detail_tbl(j).txn_line_detail_id = x_order_shipment_tbl(i).txn_line_detail_id
3494                  AND
3495                  x_txn_line_detail_tbl(j).processing_status  = 'QTY_MATCH'
3496               THEN
3497 
3498                 IF (x_txn_line_detail_tbl(j).instance_exists_flag = 'N' ) THEN
3499 
3500                   debug('No instance reference exists,so processing the txn line ');
3501 
3502                   /* update the txn_detail with the instance in shipping record */
3503                   l_upd_txn_line_dtl_tbl(l_upd).txn_line_detail_id  :=
3504                                             x_txn_line_detail_tbl(j).txn_line_detail_id;
3505                   l_upd_txn_line_dtl_tbl(l_upd).instance_id         :=
3506                                             x_order_shipment_tbl(i).instance_id;
3507                   l_upd_txn_line_dtl_tbl(l_upd).instance_exists_flag := 'Y';
3508                   l_upd_txn_line_dtl_tbl(l_upd).quantity            :=
3509                                             x_order_shipment_tbl(i).shipped_quantity;
3510                   l_upd_txn_line_dtl_tbl(l_upd).preserve_detail_flag := 'Y';
3511                   l_upd_txn_line_dtl_tbl(l_upd).serial_number       :=
3512                                             x_order_shipment_tbl(i).serial_number;
3513                   l_upd_txn_line_dtl_tbl(l_upd).lot_number          :=
3514                                             x_order_shipment_tbl(i).lot_number;
3515                   l_upd_txn_line_dtl_tbl(l_upd).processing_status   := 'IN_PROCESS';
3516                   l_upd_txn_line_dtl_tbl(l_upd).inv_organization_id :=
3517                                             x_order_shipment_tbl(i).organization_id;
3518                   l_upd_txn_line_dtl_tbl(l_upd).inventory_item_id   :=
3519                                             x_order_shipment_tbl(i).inventory_item_id;
3520                   l_upd_txn_line_dtl_tbl(l_upd).changed_instance_id :=
3521                                             x_txn_line_detail_tbl(j).changed_instance_id;
3522                   /* Begin fix for Bug 2972082 cascade owner flag */
3523                   l_upd_txn_line_dtl_tbl(l_upd).cascade_owner_flag  :=
3524                                             x_txn_line_detail_tbl(j).cascade_owner_flag;
3525                   /* end fix for Bug 2972082 cascade owner flag */
3526                   l_upd := l_upd +1;
3527                   exit;
3528                 ELSE
3529 
3530                   debug('Checking if config exists ');
3531 
3532                   l_config_exists := csi_utl_pkg.Check_config_exists(
3533                                        x_txn_ii_rltns_tbl,
3534                                        x_txn_line_detail_tbl(j).txn_line_detail_id);
3535                   IF l_config_exists
3536                      AND
3537                      x_txn_line_detail_tbl(j).preserve_detail_flag = 'N'
3538                   THEN
3539 
3540                     /* update the txn_dtls as errored and create txn_dtls from shipping rec */
3541 
3542                     l_upd_txn_line_dtl_tbl(l_upd).txn_line_detail_id := x_txn_line_detail_tbl(j).txn_line_detail_id;
3543                     l_upd_txn_line_dtl_tbl(l_upd).processing_status := 'ERROR' ;
3544                     l_upd := l_upd +1;
3545 
3546                     debug('Config exists so ignoring the txn_line_dtl and creating one');
3547 
3548                     csi_utl_pkg.create_txn_details(
3549                       x_txn_line_dtl_rec        => x_txn_line_detail_tbl(j),
3550                       p_txn_sub_type_rec        => p_txn_sub_type_rec,
3551                       p_order_shipment_rec      => x_order_shipment_tbl(i),
3552                       p_order_line_rec          => p_order_line_rec,
3553                       x_return_status           => x_return_status );
3554 
3555                     IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
3556                       raise fnd_api.g_exc_error;
3557                     END IF;
3558 
3559                     x_order_shipment_tbl(i).quantity_match := 'Y';
3560                     x_order_shipment_tbl(i).instance_match := 'Y';
3561                     x_txn_line_detail_tbl(j).processing_status := 'MATCH';
3562                     exit;
3563                   ELSE
3564 
3565                     /*update the txn_detail with the instance in shipping record */
3566 
3567                     debug('Config does not exists, so processing the txn line dtls ');
3568 
3569                     l_upd_txn_line_dtl_tbl(l_upd).txn_line_detail_id   :=
3570                                            x_txn_line_detail_tbl(j).txn_line_detail_id;
3571                     l_upd_txn_line_dtl_tbl(l_upd).instance_id          :=
3572                                            x_order_shipment_tbl(i).instance_id;
3573                     l_upd_txn_line_dtl_tbl(l_upd).instance_exists_flag := 'Y';
3574                     l_upd_txn_line_dtl_tbl(l_upd).quantity             :=
3575                                            x_order_shipment_tbl(i).shipped_quantity;
3576                     l_upd_txn_line_dtl_tbl(l_upd).preserve_detail_flag := 'Y';
3577                     l_upd_txn_line_dtl_tbl(l_upd).serial_number        :=
3578                                            x_order_shipment_tbl(i).serial_number;
3579                     l_upd_txn_line_dtl_tbl(l_upd).lot_number           :=
3580                                            x_order_shipment_tbl(i).lot_number;
3581                     l_upd_txn_line_dtl_tbl(l_upd).processing_status    := 'IN_PROCESS';
3582                     l_upd_txn_line_dtl_tbl(l_upd).inv_organization_id  :=
3583                                            x_order_shipment_tbl(i).organization_id;
3584                     l_upd_txn_line_dtl_tbl(l_upd).changed_instance_id  :=
3585                                            x_txn_line_detail_tbl(j).changed_instance_id;
3586                 /* Added  the item id assignment for bug 3734427 */
3587                     l_upd_txn_line_dtl_tbl(l_upd).inventory_item_id   :=
3588                                             x_order_shipment_tbl(i).inventory_item_id;
3589                     /* Begin fix for Bug 2972082 cascade owner flag */
3590                     l_upd_txn_line_dtl_tbl(l_upd).cascade_owner_flag   :=
3591                                            x_txn_line_detail_tbl(j).cascade_owner_flag;
3592                     /* end fix for Bug 2972082 cascade owner flag */
3593 
3594                     l_upd := l_upd +1;
3595 
3596                     x_order_shipment_tbl(i).quantity_match := 'Y';
3597                     x_order_shipment_tbl(i).instance_match := 'Y';
3598                     x_txn_line_detail_tbl(j).processing_status := 'MATCH';
3599                     exit;
3600                   END IF;
3601                 END IF;
3602               END IF;
3603             END IF;
3604           END LOOP;
3605         END IF;
3606       ELSIF ( x_order_shipment_tbl(i).instance_match    = 'N' )
3607               AND
3608             ( x_order_shipment_tbl(i).quantity_match    = 'N' )
3609              -- added the and condition for bug 3384668
3610               AND
3611             ( x_order_shipment_tbl(i).lot_match = 'N' )
3612       THEN
3613 
3614     debug('NEITHER INSTANCE, LOT OR QTY MATCH ');
3615     l_total_proc_qty := x_order_shipment_tbl(i).shipped_quantity;
3616 
3617     debug('total_proc_qty :' ||l_total_proc_qty);
3618 
3619     IF x_txn_line_detail_tbl.count > 0 THEN
3620       FOR j in x_txn_line_detail_tbl.first..x_txn_line_detail_tbl.last LOOP
3621 
3622        IF x_txn_line_detail_tbl(j).source_transaction_flag = 'Y' AND
3623           x_txn_line_detail_tbl(j).processing_status <> 'IN_PROCESS' THEN
3624 
3625          IF x_txn_line_detail_tbl(j).instance_exists_flag = 'N' THEN
3626 
3627            debug('Checking if config exists ');
3628 
3629            l_config_exists := csi_utl_pkg.Check_config_exists
3630                                                (x_txn_ii_rltns_tbl,
3631                                                 x_txn_line_detail_tbl(j).txn_line_detail_id);
3632            IF l_config_exists AND
3633              x_txn_line_detail_tbl(j).preserve_detail_flag = 'N'  THEN
3634 
3635              debug('Config exists so ignoring the txn_line_dtl and creating one');
3636 
3637              l_upd_txn_line_dtl_tbl(l_upd).txn_line_detail_id := x_txn_line_detail_tbl(j).txn_line_detail_id;
3638              l_upd_txn_line_dtl_tbl(l_upd).processing_status := 'ERROR' ;
3639 
3640              x_order_shipment_tbl(i).quantity_match := 'Y';
3641              x_order_shipment_tbl(i).instance_match := 'Y';
3642              x_txn_line_detail_tbl(j).processing_status := 'ERROR';
3643 
3644              csi_utl_pkg.create_txn_details
3645              (x_txn_line_dtl_rec        => x_txn_line_detail_tbl(j)
3646              ,p_txn_sub_type_rec        => p_txn_sub_type_rec
3647              ,p_order_shipment_rec      => x_order_shipment_tbl(i)
3648              ,p_order_line_rec          => p_order_line_rec
3649              ,x_return_status           => x_return_status );
3650 
3651             IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
3652                raise fnd_api.g_exc_error;
3653             END IF;
3654 
3655             l_upd := l_upd +1;
3656 
3657             exit;
3658          ELSE
3659 
3660             debug('Config does not exist so processing the txn_line_dtl ');
3661 
3662             /* update txn_detail with the instance from shipping record */
3663             l_upd_txn_line_dtl_tbl(l_upd).txn_line_detail_id := x_txn_line_detail_tbl(j).txn_line_detail_id;
3664             l_upd_txn_line_dtl_tbl(l_upd).instance_id       := x_order_shipment_tbl(i).instance_id;
3665             l_upd_txn_line_dtl_tbl(l_upd).serial_number := x_order_shipment_tbl(i).serial_number;
3666             IF nvl(x_order_shipment_tbl(i).serial_number, fnd_api.g_miss_char) <> fnd_api.g_miss_char THEN
3667               l_upd_txn_line_dtl_tbl(l_upd).mfg_serial_number_flag := 'Y';
3668             END IF;
3669             debug('Serial Number :'||l_upd_txn_line_dtl_tbl(l_upd).serial_number);
3670             l_upd_txn_line_dtl_tbl(l_upd).lot_number        := x_order_shipment_tbl(i).lot_number;
3671             l_upd_txn_line_dtl_tbl(l_upd).preserve_detail_flag := 'Y';
3672             l_upd_txn_line_dtl_tbl(l_upd).instance_exists_flag := 'Y';
3673             l_upd_txn_line_dtl_tbl(l_upd).processing_status := 'IN_PROCESS';
3674             l_upd_txn_line_dtl_tbl(l_upd).inv_organization_id := x_order_shipment_tbl(i).organization_id;
3675          /* Added  the item id assignment for bug 3734427 */
3676             l_upd_txn_line_dtl_tbl(l_upd).inventory_item_id := x_order_shipment_tbl(i).inventory_item_id;
3677 
3678             /* Begin fix for Bug 2972082 cascade owner flag */
3679             l_upd_txn_line_dtl_tbl(l_upd).cascade_owner_flag   := x_txn_line_detail_tbl(j).cascade_owner_flag;
3680             /* end fix for Bug 2972082 cascade owner flag */
3681 
3682            IF l_total_proc_qty > x_txn_line_detail_tbl(j).quantity THEN
3683             l_total_proc_qty := l_total_proc_qty - x_txn_line_detail_tbl(j).quantity;
3684 	   --Start of fix for bug 4354267
3685 	   ELSIF l_total_proc_qty < x_txn_line_detail_tbl(j).quantity THEN
3686             l_upd_txn_line_dtl_tbl(l_upd).quantity := l_total_proc_qty;
3687             l_rem_qty   := x_txn_line_detail_tbl(j).quantity - l_total_proc_qty;
3688 	    debug('Installation line detail quantity > mtl transaction quantity,so split line dtls');
3689             csi_utl_pkg.split_txn_dtls_with_qty(
3690             split_txn_dtl_id => x_txn_line_detail_tbl(j).txn_line_detail_id,
3691             p_split_qty      => l_rem_qty,
3692             x_return_status => l_return_status );
3693             IF l_return_status <> fnd_api.g_ret_sts_success THEN
3694                 raise fnd_api.g_exc_error;
3695             END IF;
3696             exit;
3697 	    --End of fix for bug 4354267
3698            ELSE
3699             l_upd_txn_line_dtl_tbl(l_upd).quantity := l_total_proc_qty;
3700             exit;
3701            END IF;
3702 
3703            x_order_shipment_tbl(i).quantity_match := 'Y';
3704            x_order_shipment_tbl(i).instance_match := 'Y';
3705            x_txn_line_detail_tbl(j).processing_status := 'IN_PROCESS';
3706            l_upd := l_upd +1;
3707 
3708            IF x_order_shipment_tbl.count > 1 THEN
3709              exit;
3710            END IF;
3711 
3712           END IF;
3713         ELSE
3714 
3715            debug('Instance reference exists so ignoring the txn_line_dtls and creating one ');
3716 
3717            l_upd_txn_line_dtl_tbl(l_upd).txn_line_detail_id := x_txn_line_detail_tbl(j).txn_line_detail_id;
3718            l_upd_txn_line_dtl_tbl(l_upd).processing_status := 'ERROR' ;
3719 
3720            csi_utl_pkg.create_txn_details
3721            (x_txn_line_dtl_rec        => x_txn_line_detail_tbl(j)
3722            ,p_txn_sub_type_rec        => p_txn_sub_type_rec
3723            ,p_order_shipment_rec      => x_order_shipment_tbl(i)
3724            ,p_order_line_rec          => p_order_line_rec
3725            ,x_return_status           => x_return_status );
3726 
3727           IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
3728             raise fnd_api.g_exc_error;
3729           END IF;
3730 
3731           l_upd := l_upd +1;
3732 
3733           x_txn_line_detail_tbl(j).processing_status := 'IN_PROCESS';
3734           x_order_shipment_tbl(i).quantity_match := 'Y';
3735           x_order_shipment_tbl(i).instance_match := 'Y';
3736           exit;
3737 
3738        END IF;
3739       END IF;
3740 
3741      END LOOP;
3742     END IF;
3743    END IF;
3744 
3745       END LOOP;
3746     END IF;
3747 
3748     debug('Stamping the transaction details with the material transaction info.');
3749     debug('  upd_txn_line_dtl_tbl.count :'||l_upd_txn_line_dtl_tbl.count);
3750 
3751     IF l_upd_txn_line_dtl_tbl.count > 0 then
3752 
3753       /* update the txn dtls with the processing status */
3754 
3755       csi_t_txn_details_grp.update_txn_line_dtls(
3756         p_api_version              => 1.0,
3757         p_commit                   => fnd_api.g_false,
3758         p_init_msg_list            => fnd_api.g_true,
3759         p_validation_level         => fnd_api.g_valid_level_none,
3760         p_txn_line_rec             => l_upd_txn_line_rec,
3761         p_txn_line_detail_tbl      => l_upd_txn_line_dtl_tbl,
3762         px_txn_ii_rltns_tbl        => l_upd_txn_ii_rltns_tbl,
3763         px_txn_party_detail_tbl    => l_upd_txn_party_detail_tbl,
3764         px_txn_pty_acct_detail_tbl => l_upd_txn_pty_acct_dtl_tbl,
3765         px_txn_org_assgn_tbl       => l_upd_txn_org_assgn_tbl,
3766         px_txn_ext_attrib_vals_tbl => l_upd_txn_ext_attr_vals_tbl,
3767         x_return_status            => x_return_status,
3768         x_msg_count                => x_msg_count,
3769         x_msg_data                 => x_msg_data);
3770 
3771       IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
3772         g_api_name := 'csi_t_txn_details_grp.update_txn_line_dtls';
3773         raise fnd_api.g_exc_error;
3774       END IF;
3775 
3776       debug('update_txn_line_dtls completed successfully');
3777 
3778     END IF;
3779 
3780   EXCEPTION
3781     WHEN FND_API.G_EXC_ERROR THEN
3782       x_return_status := FND_API.G_RET_STS_ERROR ;
3783     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3784       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3785   END process_txn_dtl;
3786 
3787   PROCEDURE decrement_source_instance(
3788     p_instance_id    in number,
3789     p_quantity       in number,
3790     p_trx_rec        in OUT NOCOPY csi_datastructures_pub.transaction_rec,
3791     x_return_status     OUT NOCOPY varchar2)
3792   IS
3793 
3794     l_instance_rec        csi_datastructures_pub.instance_rec;
3795     l_party_tbl           csi_datastructures_pub.party_tbl;
3796     l_party_acct_tbl      csi_datastructures_pub.party_account_tbl;
3797     l_inst_asset_tbl      csi_datastructures_pub.instance_asset_tbl;
3798     l_ext_attrib_val_tbl  csi_datastructures_pub.extend_attrib_values_tbl;
3799     l_pricing_attribs_tbl csi_datastructures_pub.pricing_attribs_tbl;
3800     l_org_units_tbl       csi_datastructures_pub.organization_units_tbl;
3801     l_inst_id_lst         csi_datastructures_pub.id_tbl;
3802 
3803     l_return_status       varchar2(1) := fnd_api.g_ret_sts_success;
3804     l_msg_count           number;
3805     l_msg_data            varchar2(2000);
3806 
3807 	l_last_vld_organization_id NUMBER; -- Bug 12420630 ; FP for Bug 9396215
3808 	l_neg_code	NUMBER := 0; -- Bug 12420630 ; FP for Bug 9396215
3809   BEGIN
3810 
3811     x_return_status       := fnd_api.g_ret_sts_success;
3812 
3813     api_log('decrement_source_instance');
3814 
3815 
3816     l_instance_rec.instance_id := p_instance_id;
3817 
3818 	--START: Bug 12420630 ; FP for Bug 9396215
3819 	BEGIN
3820 		SELECT last_vld_organization_id
3821 		INTO l_last_vld_organization_id
3822 		FROM csi_item_instances
3823 		WHERE instance_id = p_instance_id;
3824 
3825 		l_neg_code := csi_inv_trxs_pkg.get_neg_inv_code(l_last_vld_organization_id);
3826 		IF l_neg_code = 1 THEN
3827             debug('The Org accepts negative quantity. Setting the check_for_instance_expiry to False');
3828             l_instance_rec.check_for_instance_expiry := fnd_api.g_false;
3829         END IF; -- l_neg_code = 1
3830 	EXCEPTION
3831 		WHEN OTHERS THEN
3832 			NULL;
3833 	END;
3834 	--END: Bug 12420630 ; FP for Bug 9396215
3835 
3836     SELECT object_version_number,
3837            quantity - p_quantity
3838     INTO   l_instance_rec.object_version_number,
3839            l_instance_rec.quantity
3840     FROM   csi_item_instances
3841     WHERE  instance_id = l_instance_rec.instance_id;
3842 
3843     csi_t_gen_utility_pvt.dump_api_info(
3844       p_api_name => 'update_item_instance',
3845       p_pkg_name => 'csi_item_instance_pub');
3846 
3847     csi_t_gen_utility_pvt.dump_csi_instance_rec(
3848       p_csi_instance_rec => l_instance_rec);
3849 
3850     /* decrement the inventory source instance */
3851 
3852     csi_item_instance_pub.update_item_instance(
3853       p_api_version           => 1.0,
3854       p_commit                => fnd_api.g_false,
3855       p_init_msg_list         => fnd_api.g_true,
3856       p_validation_level      => fnd_api.g_valid_level_full,
3857       p_instance_rec          => l_instance_rec,
3858       p_ext_attrib_values_tbl => l_ext_attrib_val_tbl,
3859       p_party_tbl             => l_party_tbl,
3860       p_account_tbl           => l_party_acct_tbl,
3861       p_pricing_attrib_tbl    => l_pricing_attribs_tbl,
3862       p_org_assignments_tbl   => l_org_units_tbl,
3863       p_txn_rec               => p_trx_rec,
3864       p_asset_assignment_tbl  => l_inst_asset_tbl,
3865       x_instance_id_lst       => l_inst_id_lst,
3866       x_return_status         => l_return_status,
3867       x_msg_count             => l_msg_count,
3868       x_msg_data              => l_msg_data);
3869 
3870     -- For Bug 4057183
3871     -- IF l_return_status <> fnd_api.g_ret_sts_success THEN
3872     IF l_return_status not in (fnd_api.g_ret_sts_success,'W') THEN
3873       g_api_name := 'csi_item_instance_pub.update_item_instance';
3874       raise fnd_api.g_exc_error;
3875     END IF;
3876 
3877   EXCEPTION
3878 
3879     WHEN fnd_api.g_exc_error THEN
3880       x_return_status := fnd_api.g_ret_sts_error;
3881 
3882   END decrement_source_instance;
3883 
3884   PROCEDURE dump_customer_products(
3885     p_cps_tbl                 IN customer_products_tbl)
3886   IS
3887     l_rec                     customer_product_rec;
3888   BEGIN
3889     IF p_cps_tbl.COUNT > 0 THEN
3890 
3891       debug('InstID     Quantity   LineID     TLDID      CSITxnID   Serial          Lot            ');
3892       debug('---------- ---------- ---------- ---------- ---------- --------------- ---------------');
3893 
3894       FOR l_ind IN p_cps_tbl.FIRST .. p_cps_tbl.LAST
3895       LOOP
3896 
3897         l_rec := p_cps_tbl(l_ind);
3898 
3899         debug(rpad(to_char(l_rec.instance_id), 11, ' ')||
3900               rpad(to_char(l_rec.quantity), 11, ' ')||
3901               rpad(to_char(l_rec.line_id), 11, ' ')||
3902               rpad(to_char(l_rec.txn_line_detail_id), 11, ' ')||
3903               rpad(to_char(l_rec.transaction_id), 11, ' ')||
3904               rpad(l_rec.serial_number, 16, ' ')||
3905               rpad(l_rec.lot_number, 16, ' '));
3906 
3907       END LOOP;
3908 
3909      END IF;
3910 
3911   END dump_customer_products;
3912 
3913   PROCEDURE make_non_hdr_rec(
3914     p_instance_hdr_rec  IN         csi_datastructures_pub.instance_header_rec,
3915     x_instance_rec      OUT NOCOPY csi_datastructures_pub.instance_rec,
3916     x_return_status     OUT NOCOPY varchar2)
3917   IS
3918 
3919     l_instance_hdr_tbl  csi_datastructures_pub.instance_header_tbl;
3920     l_instance_tbl      csi_datastructures_pub.instance_tbl;
3921 
3922     l_return_status     varchar2(1) := fnd_api.g_ret_sts_success;
3923 
3924   BEGIN
3925 
3926     x_return_status := l_return_status;
3927 
3928     api_log('make_non_hdr_rec');
3929 
3930 
3931     l_instance_hdr_tbl(1) := p_instance_hdr_rec;
3932 
3933     csi_utl_pkg.make_non_header_tbl(
3934       p_instance_header_tbl => l_instance_hdr_tbl,
3935       x_instance_tbl        => l_instance_tbl,
3936       x_return_status       => l_return_status);
3937 
3938     IF l_return_status <> fnd_api.g_ret_sts_success THEN
3939       RAISE fnd_api.g_exc_error;
3940     END IF;
3941 
3942     x_instance_rec := l_instance_tbl(1);
3943 
3944   EXCEPTION
3945     WHEN fnd_api.g_exc_error THEN
3946       x_return_status := fnd_api.g_ret_sts_error;
3947   END make_non_hdr_rec;
3948 
3949   PROCEDURE split_instance_using_ratio(
3950     p_instance_id         IN     number,
3951     p_qty_ratio           IN     number,
3952     p_qty_completed       IN     number,
3953     p_organization_id     IN     number,
3954     px_csi_txn_rec        IN OUT NOCOPY csi_datastructures_pub.transaction_rec,
3955     x_splitted_instances     OUT NOCOPY csi_datastructures_pub.instance_tbl,
3956     x_return_status          OUT NOCOPY varchar2)
3957   IS
3958 
3959     l_qty_remaining         number;
3960 
3961     l_init_instance_rec     csi_datastructures_pub.instance_rec;
3962 
3963     l_return_status         varchar2(1) := fnd_api.g_ret_sts_success;
3964     l_msg_data              varchar2(2000);
3965     l_msg_count             number;
3966 
3967     l_split_flag            boolean := FALSE;
3968 
3969     l_splitted_instances    csi_datastructures_pub.instance_tbl;
3970     l_s_ind                 binary_integer;
3971 
3972     -- get_item_instance_details variables
3973     l_g_instance_rec        csi_datastructures_pub.instance_header_rec;
3974     l_g_ph_tbl              csi_datastructures_pub.party_header_tbl;
3975     l_g_pah_tbl             csi_datastructures_pub.party_account_header_tbl;
3976     l_g_ouh_tbl             csi_datastructures_pub.org_units_header_tbl;
3977     l_g_pa_tbl              csi_datastructures_pub.pricing_attribs_tbl;
3978     l_g_eav_tbl             csi_datastructures_pub.extend_attrib_values_tbl;
3979     l_g_ea_tbl              csi_datastructures_pub.extend_attrib_tbl;
3980     l_g_iah_tbl             csi_datastructures_pub.instance_asset_header_tbl;
3981     l_g_time_stamp          date;
3982 
3983     -- make_non_hdr variables
3984     l_instance_rec          csi_datastructures_pub.instance_rec;
3985 
3986     -- update_item_instance variables
3987     l_u_instance_rec        csi_datastructures_pub.instance_rec;
3988     l_u_parties_tbl         csi_datastructures_pub.party_tbl;
3989     l_u_pty_accts_tbl       csi_datastructures_pub.party_account_tbl;
3990     l_u_org_units_tbl       csi_datastructures_pub.organization_units_tbl;
3991     l_u_ea_values_tbl       csi_datastructures_pub.extend_attrib_values_tbl;
3992     l_u_pricing_tbl         csi_datastructures_pub.pricing_attribs_tbl;
3993     l_u_assets_tbl          csi_datastructures_pub.instance_asset_tbl;
3994     l_u_instance_ids_list   csi_datastructures_pub.id_tbl;
3995 
3996     -- create_item_instance varaibles
3997     l_c_instance_rec        csi_datastructures_pub.instance_rec;
3998     l_c_parties_tbl         csi_datastructures_pub.party_tbl;
3999     l_c_pty_accts_tbl       csi_datastructures_pub.party_account_tbl;
4000     l_c_org_units_tbl       csi_datastructures_pub.organization_units_tbl;
4001     l_c_ea_values_tbl       csi_datastructures_pub.extend_attrib_values_tbl;
4002     l_c_pricing_tbl         csi_datastructures_pub.pricing_attribs_tbl;
4003     l_c_assets_tbl          csi_datastructures_pub.instance_asset_tbl;
4004     c_pa_ind                binary_integer;
4005 
4006   BEGIN
4007 
4008     x_return_status := fnd_api.g_ret_sts_success;
4009 
4010     api_log('split_instance_using_ratio');
4011 
4012     l_s_ind := 0;
4013 
4014     l_g_instance_rec.instance_id := p_instance_id;
4015 
4016     -- get the instance party and party account info
4017     csi_item_instance_pub.get_item_instance_details(
4018       p_api_version           => 1.0,
4019       p_commit                => fnd_api.g_false,
4020       p_init_msg_list         => fnd_api.g_true,
4021       p_validation_level      => fnd_api.g_valid_level_full,
4022       p_instance_rec          => l_g_instance_rec,
4023       p_get_parties           => fnd_api.g_true,
4024       p_party_header_tbl      => l_g_ph_tbl,
4025       p_get_accounts          => fnd_api.g_true,
4026       p_account_header_tbl    => l_g_pah_tbl,
4027       p_get_org_assignments   => fnd_api.g_false,
4028       p_org_header_tbl        => l_g_ouh_tbl,
4029       p_get_pricing_attribs   => fnd_api.g_false,
4030       p_pricing_attrib_tbl    => l_g_pa_tbl,
4031       p_get_ext_attribs       => fnd_api.g_false,
4032       p_ext_attrib_tbl        => l_g_eav_tbl,
4033       p_ext_attrib_def_tbl    => l_g_ea_tbl,
4034       p_get_asset_assignments => fnd_api.g_false,
4035       p_asset_header_tbl      => l_g_iah_tbl,
4036       p_time_stamp            => l_g_time_stamp,
4037       x_return_status         => l_return_status,
4038       x_msg_count             => l_msg_count,
4039       x_msg_data              => l_msg_data);
4040 
4041     IF l_return_status <> fnd_api.g_ret_sts_success THEN
4042       RAISE fnd_api.g_exc_error;
4043     END IF;
4044 
4045     make_non_hdr_rec(
4046       p_instance_hdr_rec => l_g_instance_rec,
4047       x_instance_rec     => l_instance_rec,
4048       x_return_status    => l_return_status);
4049 
4050     debug('  Component Instance ID :'||l_instance_rec.instance_id);
4051     debug('  Component Quantity    :'||l_instance_rec.quantity);
4052 
4053     l_qty_remaining := l_g_instance_rec.quantity;
4054 
4055     FOR ind IN 1 .. p_qty_completed
4056     LOOP
4057 
4058       IF l_qty_remaining > p_qty_ratio THEN
4059 
4060         l_split_flag := TRUE;
4061 
4062         -- initialize the record structure
4063         l_c_instance_rec := l_init_instance_rec;
4064         l_u_instance_rec := l_init_instance_rec;
4065 
4066         l_qty_remaining := l_qty_remaining - p_qty_ratio;
4067 
4068         debug('  Allocated Qty(NEW) :'||p_qty_ratio);
4069         debug('  Remaining Qty(UPD) :'||l_qty_remaining );
4070 
4071         l_c_instance_rec := l_instance_rec;
4072 
4073         -- substitute create specific attributes
4074         l_c_instance_rec.instance_id           := fnd_api.g_miss_num;
4075         l_c_instance_rec.instance_number       := fnd_api.g_miss_char;
4076         l_c_instance_rec.object_version_number := 1.0;
4077         l_c_instance_rec.vld_organization_id   := p_organization_id;
4078         l_c_instance_rec.quantity              := p_qty_ratio;
4079         l_c_instance_rec.operational_status_code:= 'NOT_USED';
4080 
4081         -- build party
4082         l_c_parties_tbl.DELETE;
4083         l_c_pty_accts_tbl.DELETE;
4084         c_pa_ind := 0;
4085 
4086         IF l_g_ph_tbl.COUNT > 0 THEN
4087 
4088           FOR l_pt_ind IN l_g_ph_tbl.FIRST ..l_g_ph_tbl.LAST
4089           LOOP
4090             l_c_parties_tbl(l_pt_ind).instance_party_id  := fnd_api.g_miss_num;
4091             l_c_parties_tbl(l_pt_ind).instance_id        := fnd_api.g_miss_num;
4092             l_c_parties_tbl(l_pt_ind).party_id           :=
4093                             l_g_ph_tbl(l_pt_ind).party_id;
4094             l_c_parties_tbl(l_pt_ind).party_source_table :=
4095                              l_g_ph_tbl(l_pt_ind).party_source_table;
4096             l_c_parties_tbl(l_pt_ind).relationship_type_code :=
4097                              l_g_ph_tbl(l_pt_ind).relationship_type_code;
4098             l_c_parties_tbl(l_pt_ind).contact_flag       := 'N';
4099 
4100             -- build party account
4101             IF l_g_pah_tbl.COUNT > 0 THEN
4102               FOR l_pa_ind IN l_g_pah_tbl.FIRST..l_g_pah_tbl.LAST
4103               LOOP
4104                 IF l_g_pah_tbl(l_pa_ind).instance_party_id = l_g_ph_tbl(l_pt_ind).instance_party_id
4105                 THEN
4106                   c_pa_ind := c_pa_ind + 1;
4107                   l_c_pty_accts_tbl(c_pa_ind).parent_tbl_index   := l_pt_ind;
4108                   l_c_pty_accts_tbl(c_pa_ind).ip_account_id      := fnd_api.g_miss_num;
4109                   l_c_pty_accts_tbl(c_pa_ind).instance_party_id  := fnd_api.g_miss_num;
4110                   l_c_pty_accts_tbl(c_pa_ind).party_account_id       :=
4111                                               l_g_pah_tbl(l_pa_ind).party_account_id;
4112                   l_c_pty_accts_tbl(c_pa_ind).relationship_type_code :=
4113                             l_g_pah_tbl(l_pa_ind).relationship_type_code;
4114                 END IF;
4115               END LOOP;
4116             END IF;
4117 
4118           END LOOP;
4119         END IF;
4120 
4121         -- create a new instance for the decremented qty
4122 
4123         csi_item_instance_pub.create_item_instance(
4124           p_api_version           => 1.0,
4125           p_commit                => fnd_api.g_false,
4126           p_init_msg_list         => fnd_api.g_true,
4127           p_validation_level      => fnd_api.g_valid_level_full,
4128           p_instance_rec          => l_c_instance_rec,
4129           p_party_tbl             => l_c_parties_tbl,
4130           p_account_tbl           => l_c_pty_accts_tbl,
4131           p_org_assignments_tbl   => l_c_org_units_tbl,
4132           p_ext_attrib_values_tbl => l_c_ea_values_tbl,
4133           p_pricing_attrib_tbl    => l_c_pricing_tbl,
4134           p_asset_assignment_tbl  => l_c_assets_tbl,
4135           p_txn_rec               => px_csi_txn_rec,
4136           x_return_status         => l_return_status,
4137           x_msg_count             => l_msg_count,
4138           x_msg_data              => l_msg_data );
4139 
4140         -- For Bug 4057183
4141         -- IF l_return_status <> fnd_api.g_ret_sts_success THEN
4142         IF l_return_status not in (fnd_api.g_ret_sts_success,'W') THEN
4143           RAISE fnd_api.g_exc_error;
4144         END IF;
4145 
4146         debug('  New Instance ID :'||l_c_instance_rec.instance_id);
4147 
4148         l_s_ind := l_s_ind + 1;
4149         l_splitted_instances(l_s_ind) := l_c_instance_rec;
4150 
4151         -- decrementing the existing wip instance with the remaining quantity
4152         l_u_instance_rec.instance_id         := p_instance_id;
4153         l_u_instance_rec.quantity            := l_qty_remaining;
4154         l_u_instance_rec.vld_organization_id := p_organization_id;
4155 
4156         SELECT object_version_number
4157         INTO   l_u_instance_rec.object_version_number
4158         FROM   csi_item_instances
4159         WHERE  instance_id = l_u_instance_rec.instance_id;
4160 
4161         csi_item_instance_pub.update_item_instance(
4162           p_api_version           => 1.0,
4163           p_commit                => fnd_api.g_false,
4164           p_init_msg_list         => fnd_api.g_true,
4165           p_validation_level      => fnd_api.g_valid_level_full,
4166           p_instance_rec          => l_u_instance_rec,
4167           p_party_tbl             => l_u_parties_tbl,
4168           p_account_tbl           => l_u_pty_accts_tbl,
4169           p_org_assignments_tbl   => l_u_org_units_tbl,
4170           p_ext_attrib_values_tbl => l_u_ea_values_tbl,
4171           p_pricing_attrib_tbl    => l_u_pricing_tbl,
4172           p_asset_assignment_tbl  => l_u_assets_tbl,
4173           p_txn_rec               => px_csi_txn_rec,
4174           x_instance_id_lst       => l_u_instance_ids_list,
4175           x_return_status         => l_return_status,
4176           x_msg_count             => l_msg_count,
4177           x_msg_data              => l_msg_data);
4178 
4179         -- For Bug 4057183
4180         -- IF l_return_status <> fnd_api.g_ret_sts_success THEN
4181         IF l_return_status not in (fnd_api.g_ret_sts_success,'W') THEN
4182           RAISE fnd_api.g_exc_error;
4183         END IF;
4184 
4185       ELSE
4186 
4187         -- we get two cases here < and =
4188         -- when the remaining qty is < ratio do not allocate it to an assy instance
4189         -- making sure that assy instances are always getting the full ratio. this
4190         -- simplifies the process of elliminating assy instances when further partial
4191         -- issues are done. otherwise it is difficult to get the partially allocated
4192         -- component instance and update it with the remaining ratio qty blah blah blah
4193         --(just simplifying my coding)
4194 
4195         IF l_qty_remaining < p_qty_ratio THEN
4196           NULL;
4197         ELSE
4198 
4199           l_s_ind := l_s_ind + 1;
4200 
4201           IF l_split_flag THEN
4202             l_splitted_instances(l_s_ind) := l_u_instance_rec;
4203           ELSE
4204             l_splitted_instances(l_s_ind) := l_instance_rec;
4205           END IF;
4206 
4207         END IF;
4208 
4209         EXIT;
4210 
4211       END IF;
4212 
4213     END LOOP;
4214 
4215     x_splitted_instances := l_splitted_instances;
4216 
4217   EXCEPTION
4218     WHEN fnd_api.g_exc_error THEN
4219       x_return_status := fnd_api.g_ret_sts_error;
4220   END split_instance_using_ratio;
4221 
4222   PROCEDURE convert_wip_instance_to_cp(
4223     p_instance_id       IN  number,
4224     p_line_id           IN  number,
4225     p_csi_txn_rec       IN  OUT NOCOPY csi_datastructures_pub.transaction_rec,
4226     x_return_status     OUT NOCOPY varchar2)
4227   IS
4228 
4229     l_party_site_id            number;
4230     l_owner_party_id           number;
4231     l_owner_account_id         number;
4232 
4233     l_order_line_rec           oe_order_lines_all%rowtype;
4234     l_order_header_rec         oe_order_headers_all%rowtype;
4235 
4236     l_location_type_code       varchar2(80);
4237     l_inst_object_ver_num      number;
4238 
4239     l_u_instance_rec           csi_datastructures_pub.instance_rec;
4240     l_u_party_tbl              csi_datastructures_pub.party_tbl;
4241     l_u_party_acct_tbl         csi_datastructures_pub.party_account_tbl;
4242     l_u_inst_asset_tbl         csi_datastructures_pub.instance_asset_tbl;
4243     l_u_ext_attrib_val_tbl     csi_datastructures_pub.extend_attrib_values_tbl;
4244     l_u_pricing_attribs_tbl    csi_datastructures_pub.pricing_attribs_tbl;
4245     l_u_org_units_tbl          csi_datastructures_pub.organization_units_tbl;
4246     l_u_inst_id_lst            csi_datastructures_pub.id_tbl;
4247 
4248     l_instance_party_id        number;
4249     l_pty_object_ver_num       number;
4250     l_pa_object_ver_num        number;
4251 
4252     l_return_status            varchar2(1) := fnd_api.g_ret_sts_success;
4253     l_msg_count                number;
4254     l_msg_data                 varchar2(2000);
4255 
4256   BEGIN
4257 
4258     x_return_status := fnd_api.g_ret_sts_success;
4259     api_log('convert_wip_instance_to_cp');
4260 
4261     -- check if the instance is in WIP
4262     SELECT object_version_number,
4263            location_type_code
4264     INTO   l_inst_object_ver_num,
4265            l_location_type_code
4266     FROM   csi_item_instances
4267     WHERE  instance_id = p_instance_id;
4268 
4269     SELECT * INTO l_order_line_rec
4270     FROM   oe_order_lines_all
4271     WHERE  line_id = p_line_id;
4272 
4273     SELECT * INTO l_order_header_rec
4274     FROM   oe_order_headers_all
4275     WHERE  header_id = l_order_line_rec.header_id;
4276 
4277     -- derive location
4278 
4279     IF l_order_line_rec.ship_to_org_id is null THEN
4280       l_order_line_rec.ship_to_org_id := l_order_header_rec.ship_to_org_id;
4281     END IF;
4282 
4283     IF l_order_line_rec.sold_from_org_id is null THEN
4284       l_order_line_rec.sold_from_org_id := l_order_header_rec.sold_from_org_id;
4285     END IF;
4286 
4287     IF l_order_line_rec.sold_to_org_id is null THEN
4288       l_order_line_rec.sold_to_org_id := l_order_header_rec.sold_to_org_id;
4289     END IF;
4290 
4291     IF l_order_line_rec.agreement_id is null THEN
4292       l_order_line_rec.agreement_id := l_order_header_rec.agreement_id;
4293     END IF;
4294 
4295     IF l_order_line_rec.ship_to_org_id is not null THEN
4296 
4297       BEGIN
4298 
4299         SELECT HCAS.party_site_id
4300         INTO   l_party_site_id
4301         FROM   hz_cust_site_uses_all  HCSU,
4302                hz_cust_acct_sites_all HCAS
4303         WHERE  HCSU.site_use_id       = l_order_line_rec.ship_to_org_id
4304         AND    HCAS.cust_acct_site_id = HCSU.cust_acct_site_id;
4305 
4306       EXCEPTION
4307         WHEN no_data_found THEN
4308 
4309           fnd_message.set_name('CSI','CSI_TXN_SITE_USE_INVALID');
4310           fnd_message.set_token('SITE_USE_ID',l_order_line_rec.ship_to_org_id);
4311           fnd_message.set_token('SITE_USE_CODE','SHIP_TO');
4312           fnd_msg_pub.add;
4313           raise fnd_api.g_exc_error;
4314       END;
4315 
4316     END IF;
4317 
4318     -- update the instance to make it a cp
4319     l_u_instance_rec.instance_id              := p_instance_id ;
4320     l_u_instance_rec.vld_organization_id      := l_order_line_rec.ship_from_org_id;
4321     l_u_instance_rec.location_type_code       := 'HZ_PARTY_SITES';
4322     l_u_instance_rec.location_id              := l_party_site_id;
4323     l_u_instance_rec.install_location_type_code := 'HZ_PARTY_SITES';
4324     l_u_instance_rec.install_location_id      := l_party_site_id;
4325     l_u_instance_rec.accounting_class_code    := 'CUST_PROD';
4326     l_u_instance_rec.active_end_date          := null;
4327     l_u_instance_rec.instance_usage_code      := 'OUT_OF_ENTERPRISE';
4328     l_u_instance_rec.object_version_number    := l_inst_object_ver_num;
4329 
4330     -- build owner party
4331 
4332     SELECT party_id
4333     INTO   l_owner_party_id
4334     FROM   hz_cust_accounts
4335     WHERE  cust_account_id = l_order_line_rec.sold_to_org_id;
4336 
4337     SELECT instance_party_id,
4338            object_version_number
4339     INTO   l_instance_party_id,
4340            l_pty_object_ver_num
4341     FROM   csi_i_parties
4342     WHERE  instance_id = p_instance_id;
4343 
4344     l_u_party_tbl(1).instance_party_id      := l_instance_party_id;
4345     l_u_party_tbl(1).instance_id            := p_instance_id;
4346     l_u_party_tbl(1).party_id               := l_owner_party_id;
4347     l_u_party_tbl(1).party_source_table     := 'HZ_PARTIES';
4348     l_u_party_tbl(1).relationship_type_code := 'OWNER';
4349     l_u_party_tbl(1).contact_flag           := 'N';
4350     l_u_party_tbl(1).object_version_number  :=  l_pty_object_ver_num;
4351 
4352     -- build owner account
4353     l_owner_account_id := l_order_line_rec.sold_to_org_id;
4354 
4355     l_u_party_acct_tbl(1).ip_account_id          := fnd_api.g_miss_num;
4356     l_u_party_acct_tbl(1).party_account_id       := l_owner_account_id;
4357     l_u_party_acct_tbl(1).relationship_type_code := 'OWNER';
4358     l_u_party_acct_tbl(1).bill_to_address        := fnd_api.g_miss_num;
4359     l_u_party_acct_tbl(1).ship_to_address        := fnd_api.g_miss_num;
4360     l_u_party_acct_tbl(1).instance_party_id      := l_instance_party_id;
4361     l_u_party_acct_tbl(1).parent_tbl_index       := 1;
4362 
4363     csi_t_gen_utility_pvt.dump_api_info(
4364       p_api_name => 'update_item_instance',
4365       p_pkg_name => 'csi_item_instance_pub');
4366 
4367     csi_item_instance_pub.update_item_instance(
4368       p_api_version           => 1.0,
4369       p_commit                => fnd_api.g_false,
4370       p_init_msg_list         => fnd_api.g_true,
4371       p_validation_level      => fnd_api.g_valid_level_full,
4372       p_instance_rec          => l_u_instance_rec,
4373       p_ext_attrib_values_tbl => l_u_ext_attrib_val_tbl,
4374       p_party_tbl             => l_u_party_tbl,
4375       p_account_tbl           => l_u_party_acct_tbl,
4376       p_pricing_attrib_tbl    => l_u_pricing_attribs_tbl,
4377       p_org_assignments_tbl   => l_u_org_units_tbl,
4378       p_txn_rec               => p_csi_txn_rec,
4379       p_asset_assignment_tbl  => l_u_inst_asset_tbl,
4380       x_instance_id_lst       => l_u_inst_id_lst,
4381       x_return_status         => l_return_status,
4382       x_msg_count             => l_msg_count,
4383       x_msg_data              => l_msg_data);
4384 
4385     -- For Bug 4057183
4386     -- IF NOT(l_return_status = fnd_api.g_ret_sts_success) THEN
4387     IF NOT(l_return_status in (fnd_api.g_ret_sts_success,'W')) THEN
4388       raise fnd_api.g_exc_error;
4389     END IF;
4390 
4391     debug('WIP instance is successfully converted to a CP.');
4392 
4393   EXCEPTION
4394     WHEN fnd_api.g_exc_error THEN
4395       x_return_status := fnd_api.g_ret_sts_error;
4396   END convert_wip_instance_to_cp;
4397 
4398   PROCEDURE get_single_qty_instances(
4399     p_all_cps_tbl        IN     customer_products_tbl,
4400     x_single_cps_tbl        OUT nocopy customer_products_tbl)
4401   IS
4402     l_cps_tbl        customer_products_tbl;
4403     l_ind            binary_integer := 0;
4404   BEGIN
4405     IF p_all_cps_tbl.COUNT > 0 THEN
4406       FOR p_ind IN p_all_cps_tbl.FIRST .. p_all_cps_tbl.LAST
4407       LOOP
4408         IF p_all_cps_tbl(p_ind).quantity = 1 THEN
4409           l_ind := l_ind + 1;
4410           l_cps_tbl(l_ind) := p_all_cps_tbl(p_ind);
4411         END IF;
4412       END LOOP;
4413     END IF;
4414     x_single_cps_tbl := l_cps_tbl;
4415   END get_single_qty_instances;
4416 
4417   PROCEDURE get_comp_instances_from_wip(
4418     p_wip_entity_id   IN     number,
4419     p_organization_id IN     number,
4420     p_cps_tbl         IN     customer_products_tbl,
4421     px_csi_txn_rec    IN OUT nocopy csi_datastructures_pub.transaction_rec,
4422     x_iir_tbl            OUT nocopy csi_datastructures_pub.ii_relationship_tbl,
4423     x_return_status      OUT nocopy varchar2)
4424   IS
4425 
4426     l_cps_tbl                customer_products_tbl;
4427     l_qty_per_assy           number := 0;
4428     l_iir_ind                binary_integer := 0;
4429     l_iir_tbl                csi_datastructures_pub.ii_relationship_tbl;
4430     l_serial_code            number;
4431 
4432     l_splitted_instances     csi_datastructures_pub.instance_tbl;
4433     l_return_status          varchar2(1) := fnd_api.g_ret_sts_success;
4434 
4435     CURSOR req_cur IS
4436       SELECT wip.inventory_item_id,
4437              sum(required_quantity)     qty_required,
4438              sum(quantity_issued)       qty_issued,
4439              sum(quantity_per_assembly) qty_per_assy
4440       FROM   wip_requirement_operations wip,
4441              mtl_system_items           msi
4442       WHERE  wip_entity_id         = p_wip_entity_id
4443       AND    wip.organization_id   = p_organization_id
4444       AND    wip.inventory_item_id = msi.inventory_item_id
4445       AND    wip.organization_id   = msi.organization_id
4446       AND    nvl(msi.comms_nl_trackable_flag, 'N') = 'Y'
4447       AND    nvl(quantity_issued,0) > 0
4448       GROUP BY wip.inventory_item_id;
4449 
4450     CURSOR wip_nsrl_cur(p_item_id IN number) IS
4451       SELECT instance_id,
4452              quantity,
4453              serial_number
4454       FROM   csi_item_instances
4455       WHERE  inventory_item_id  = p_item_id
4456       AND    location_type_code = 'WIP'
4457       AND    wip_job_id         = p_wip_entity_id;
4458 
4459     CURSOR wip_inst_cur(p_item_id IN number, p_qty_per_assy IN number) IS
4460       SELECT instance_id,
4461              quantity,
4462              serial_number
4463       FROM   csi_item_instances
4464       WHERE  inventory_item_id  = p_item_id
4465       AND    location_type_code = 'WIP'
4466       AND    wip_job_id         = p_wip_entity_id
4467       AND    quantity          <= p_qty_per_assy;
4468 
4469     FUNCTION already_allocated(
4470       p_subject_id       IN number,
4471       p_iir_tbl          IN csi_datastructures_pub.ii_relationship_tbl)
4472     RETURN BOOLEAN
4473     IS
4474        l_allocated boolean := FALSE;
4475     BEGIN
4476       IF p_iir_tbl.COUNT > 0 THEN
4477         FOR l_ind IN p_iir_tbl.FIRST .. p_iir_tbl.LAST
4478         LOOP
4479           IF p_iir_tbl(l_ind).subject_id = p_subject_id THEN
4480             l_allocated := TRUE;
4481             exit;
4482           END IF;
4483         END LOOP;
4484       END IF;
4485       RETURN l_allocated;
4486     END already_allocated;
4487 
4488   BEGIN
4489 
4490     x_return_status := fnd_api.g_ret_sts_success;
4491     api_log('get_comp_instances_from_wip');
4492 
4493     l_cps_tbl := p_cps_tbl;
4494 
4495     FOR req_rec IN req_cur
4496     LOOP
4497 
4498       IF req_rec.qty_per_assy > 0 THEN
4499 
4500         SELECT serial_number_control_code
4501         INTO   l_serial_code
4502         FROM   mtl_system_items
4503         WHERE  inventory_item_id = req_rec.inventory_item_id
4504         AND    organization_id   = p_organization_id;
4505 
4506         IF l_serial_code in (2, 5) THEN
4507           IF l_cps_tbl.count > 0 THEN
4508             FOR l_cp_ind IN l_cps_tbl.FIRST .. l_cps_tbl.LAST
4509             LOOP
4510               l_qty_per_assy := req_rec.qty_per_assy;
4511               FOR wip_inst_rec IN wip_inst_cur(req_rec.inventory_item_id, req_rec.qty_per_assy)
4512               LOOP
4513                 IF l_qty_per_assy > 0 THEN
4514                   -- check in iir as subject and skip
4515                   IF NOT already_allocated(wip_inst_rec.instance_id, l_iir_tbl) THEN
4516                     l_iir_ind := l_iir_ind + 1;
4517                     l_iir_tbl(l_iir_ind).subject_id := wip_inst_rec.instance_id;
4518                     l_iir_tbl(l_iir_ind).object_id  := l_cps_tbl(l_cp_ind).instance_id;
4519                     l_iir_tbl(l_iir_ind).relationship_type_code := 'COMPONENT-OF';
4520                     l_qty_per_assy := l_qty_per_assy - 1;
4521                   END IF;
4522                 END IF;
4523               END LOOP;
4524             END LOOP;
4525           END IF;
4526 
4527         ELSIF l_serial_code in (1, 6) THEN
4528 
4529           -- split_instance_by_ratio
4530           FOR wip_nsrl_rec IN wip_nsrl_cur(req_rec.inventory_item_id)
4531           LOOP
4532 
4533             split_instance_using_ratio(
4534               p_instance_id         => wip_nsrl_rec.instance_id,
4535               p_qty_ratio           => req_rec.qty_per_assy,
4536               p_qty_completed       => p_cps_tbl.count,
4537               p_organization_id     => p_organization_id,
4538               px_csi_txn_rec        => px_csi_txn_rec,
4539               x_splitted_instances  => l_splitted_instances,
4540               x_return_status       => l_return_status);
4541 
4542             IF l_return_status <> fnd_api.g_ret_sts_success THEN
4543               RAISE fnd_api.g_exc_error;
4544             END IF;
4545 
4546           END LOOP;
4547 
4548           IF l_cps_tbl.count > 0 THEN
4549             FOR l_cp_ind IN l_cps_tbl.FIRST .. l_cps_tbl.LAST
4550             LOOP
4551               FOR wip_inst_rec IN wip_inst_cur(req_rec.inventory_item_id, req_rec.qty_per_assy)
4552               LOOP
4553                 -- check in iir as subject and skip
4554                 IF NOT already_allocated(wip_inst_rec.instance_id, l_iir_tbl) THEN
4555                   l_iir_ind := l_iir_ind + 1;
4556                   l_iir_tbl(l_iir_ind).subject_id := wip_inst_rec.instance_id;
4557                   l_iir_tbl(l_iir_ind).object_id  := l_cps_tbl(l_cp_ind).instance_id;
4558                   l_iir_tbl(l_iir_ind).relationship_type_code := 'COMPONENT-OF';
4559                   exit;
4560                 END IF;
4561               END LOOP;
4562             END LOOP;
4563           END IF;
4564         END IF;
4565 
4566       END IF;
4567 
4568     END LOOP;
4569 
4570     x_iir_tbl := l_iir_tbl;
4571 
4572   EXCEPTION
4573     WHEN fnd_api.g_exc_error THEN
4574       x_return_status := fnd_api.g_ret_sts_error;
4575   END get_comp_instances_from_wip;
4576 
4577   PROCEDURE get_tld_set(
4578     px_tld_rec          IN OUT NOCOPY csi_t_datastructures_grp.txn_line_detail_rec,
4579     px_tld_party_tbl    IN OUT NOCOPY csi_t_datastructures_grp.txn_party_detail_tbl,
4580     px_tld_account_tbl  IN OUT NOCOPY csi_t_datastructures_grp.txn_pty_acct_detail_tbl,
4581     px_tld_oa_tbl       IN OUT NOCOPY csi_t_datastructures_grp.txn_org_assgn_tbl,
4582     px_tld_ea_tbl       IN OUT NOCOPY csi_t_datastructures_grp.txn_ext_attrib_vals_tbl,
4583     -- out set
4584     x_tld_party_tbl        OUT NOCOPY csi_t_datastructures_grp.txn_party_detail_tbl,
4585     x_tld_account_tbl      OUT NOCOPY csi_t_datastructures_grp.txn_pty_acct_detail_tbl,
4586     x_tld_oa_tbl           OUT NOCOPY csi_t_datastructures_grp.txn_org_assgn_tbl,
4587     x_tld_ea_tbl           OUT NOCOPY csi_t_datastructures_grp.txn_ext_attrib_vals_tbl,
4588     x_return_status        OUT NOCOPY varchar2)
4589   IS
4590 
4591     l_create_flag      varchar2(1) := 'N';
4592 
4593     l_tld_party_tbl    csi_t_datastructures_grp.txn_party_detail_tbl;
4594     l_tld_account_tbl  csi_t_datastructures_grp.txn_pty_acct_detail_tbl;
4595     l_tld_oa_tbl       csi_t_datastructures_grp.txn_org_assgn_tbl;
4596     l_tld_ea_tbl       csi_t_datastructures_grp.txn_ext_attrib_vals_tbl;
4597 
4598     l_p_ind            binary_integer := 0;
4599     l_a_ind            binary_integer := 0;
4600     l_oa_ind           binary_integer := 0;
4601     l_ea_ind           binary_integer := 0;
4602     l_iir_ind          binary_integer := 0;
4603     l_pa_ind           binary_integer := 0;
4604 
4605     p_ind            binary_integer := 0;
4606     a_ind            binary_integer := 0;
4607     oa_ind           binary_integer := 0;
4608     ea_ind           binary_integer := 0;
4609     iir_ind          binary_integer := 0;
4610     pa_ind           binary_integer := 0;
4611 
4612   BEGIN
4613 
4614     api_Log('get_tld_set');
4615 
4616     x_return_status := fnd_api.g_ret_sts_success;
4617 
4618     IF nvl(px_tld_rec.instance_id, fnd_api.g_miss_num) = fnd_api.g_miss_num THEN
4619       l_create_flag := 'Y';
4620     ELSE
4621       l_create_flag := 'N';
4622     END IF;
4623 
4624     IF px_tld_party_tbl.COUNT > 0 THEN
4625       p_ind := 0;
4626       LOOP
4627         p_ind := px_tld_party_tbl.NEXT(p_ind);
4628         EXIT when p_ind is null;
4629         IF px_tld_party_tbl(p_ind).txn_line_detail_id = px_tld_rec.txn_line_detail_id THEN
4630           l_p_ind := l_p_ind + 1;
4631           l_tld_party_tbl(l_p_ind) := px_tld_party_tbl(p_ind);
4632 
4633           IF px_tld_account_tbl.COUNT > 0 THEN
4634             a_ind := 0;
4635             LOOP
4636               a_ind := px_tld_account_tbl.NEXT(a_ind);
4637               exit when a_ind is null;
4638               IF px_tld_account_tbl(a_ind).txn_party_detail_id =
4639                  px_tld_party_tbl(p_ind).txn_party_detail_id
4640               THEN
4641                 l_a_ind := l_a_ind + 1;
4642                 l_tld_account_tbl(l_a_ind) := px_tld_account_tbl(a_ind);
4643                 px_tld_account_tbl.delete(a_ind);
4644               END IF;
4645             END LOOP;
4646           END IF;
4647           px_tld_party_tbl.DELETE(p_ind);
4648         END IF;
4649       END LOOP;
4650     END IF;
4651 
4652     IF px_tld_oa_tbl.COUNT > 0 THEN
4653       oa_ind := 0;
4654       LOOP
4655         oa_ind := px_tld_oa_tbl.NEXT(oa_ind);
4656         exit when oa_ind is null;
4657         IF px_tld_oa_tbl(oa_ind).txn_line_detail_id = px_tld_rec.txn_line_detail_id THEN
4658           l_oa_ind := l_oa_ind + 1;
4659           l_tld_oa_tbl(l_oa_ind) := px_tld_oa_tbl(oa_ind);
4660           px_tld_oa_tbl.delete(oa_ind);
4661         END IF;
4662       END LOOP;
4663     END IF;
4664 
4665     IF px_tld_ea_tbl.COUNT > 0 THEN
4666       ea_ind := 0;
4667       LOOP
4668         ea_ind := px_tld_ea_tbl.NEXT(ea_ind);
4669         exit when ea_ind is null;
4670         IF px_tld_ea_tbl(ea_ind).txn_line_detail_id = px_tld_rec.txn_line_detail_id THEN
4671           l_ea_ind := l_ea_ind + 1;
4672           l_tld_ea_tbl(l_ea_ind) := px_tld_ea_tbl(ea_ind);
4673           px_tld_ea_tbl.delete(ea_ind);
4674         END IF;
4675       END LOOP;
4676     END IF;
4677 
4678     debug('TLD set counts:');
4679     debug('  l_tld_party_tbl   :'||l_tld_party_tbl.COUNT);
4680     debug('  l_tld_account_tbl :'||l_tld_account_tbl.COUNT);
4681     debug('  l_tld_oa_tbl      :'||l_tld_oa_tbl.COUNT);
4682     debug('  l_tld_ea_tbl      :'||l_tld_ea_tbl.COUNT);
4683 
4684     x_tld_party_tbl    := l_tld_party_tbl;
4685     x_tld_account_tbl  := l_tld_account_tbl;
4686     x_tld_oa_tbl       := l_tld_oa_tbl;
4687     x_tld_ea_tbl       := l_tld_ea_tbl;
4688 
4689   EXCEPTION
4690     WHEN fnd_api.g_exc_error THEN
4691       x_return_status := fnd_api.g_ret_sts_error;
4692   END get_tld_set;
4693 
4694 
4695   -- convert txn_systems in to csi_systems
4696   PROCEDURE create_csi_systems(
4697     px_csi_txn_rec     IN OUT NOCOPY csi_datastructures_pub.transaction_rec,
4698     px_txn_systems_tbl IN OUT NOCOPY csi_t_datastructures_grp.txn_systems_tbl,
4699     px_tld_tbl         IN OUT NOCOPY csi_t_datastructures_grp.txn_line_detail_tbl,
4700     x_return_status       OUT NOCOPY varchar2)
4701   IS
4702     l_system_exist     boolean := FALSE;
4703     l_system_rec       csi_datastructures_pub.system_rec;
4704     l_system_id        number;
4705 
4706     l_return_status    varchar2(1) := fnd_api.g_ret_sts_success;
4707     l_msg_count        number;
4708     l_msg_data         varchar2(2000);
4709   BEGIN
4710 
4711     x_return_status    :=  fnd_api.g_ret_sts_success;
4712     api_log('create_csi_systems');
4713 
4714     IF px_txn_systems_tbl.count > 0 THEN
4715 
4716       FOR ind in px_txn_systems_tbl.FIRST..px_txn_systems_tbl.LAST
4717       LOOP
4718 
4719         BEGIN
4720           SELECT system_id
4721           INTO   l_system_id
4722           FROM   csi_systems_vl
4723           WHERE  system_type_code = px_txn_systems_tbl(ind).system_type_code
4724           AND    name             = px_txn_systems_tbl(ind).system_name
4725           AND    customer_id      = px_txn_systems_tbl(ind).customer_id;
4726 
4727           debug('  CSI System ID :'||l_system_id);
4728 
4729           l_system_exist := TRUE;
4730 
4731         EXCEPTION
4732           WHEN no_data_found THEN
4733             l_system_exist := FALSE;
4734           WHEN too_many_rows THEN
4735             fnd_message.set_name('CSI','CSI_INT_MUL_SYS_FOUND');
4736             fnd_message.set_token('SYSTEM_NUMBER',px_txn_systems_tbl(ind).system_number);
4737             fnd_msg_pub.add;
4738             RAISE fnd_api.g_exc_error;
4739         END;
4740 
4741         IF NOT(l_system_exist) THEN
4742           csi_utl_pkg.get_system_tbl(
4743             p_txn_systems_rec  => px_txn_systems_tbl(ind),
4744             x_cre_systems_rec  => l_system_rec );
4745 
4746           csi_t_gen_utility_pvt.dump_api_info(
4747             p_pkg_name => 'csi_systems_pub',
4748             p_api_name => 'create_system');
4749 
4750           csi_systems_pub.create_system(
4751             p_api_version       => 1.0,
4752             p_commit            => fnd_api.g_false,
4753             p_init_msg_list     => fnd_api.g_true,
4754             p_validation_level  => fnd_api.g_valid_level_full,
4755             p_system_rec        => l_system_rec,
4756             p_txn_rec           => px_csi_txn_rec,
4757             x_system_id         => l_system_id,
4758             x_return_status     => l_return_status,
4759             x_msg_count         => l_msg_count,
4760             x_msg_data          => l_msg_data);
4761 
4762           IF l_return_status <> fnd_api.g_ret_sts_success THEN
4763             RAISE fnd_api.g_exc_error;
4764           END IF;
4765 
4766           debug('  CSI System Created Successfully. System ID :'||l_system_id);
4767         END IF;
4768 
4769         IF px_tld_tbl.count > 0 THEN
4770           FOR tld_ind IN px_tld_tbl.FIRST .. px_tld_tbl.LAST
4771           LOOP
4772             IF px_tld_tbl(tld_ind).transaction_system_id =
4773                px_txn_systems_tbl(ind).transaction_system_id
4774             THEN
4775                px_tld_tbl(tld_ind).csi_system_id  := l_system_id;
4776             END IF;
4777           END LOOP;
4778         END IF;
4779 
4780       END LOOP; -- txn systems loop
4781     END IF; -- end if for system table count > 0
4782   EXCEPTION
4783     WHEN fnd_api.g_exc_error THEN
4784       x_return_status := fnd_api.g_ret_sts_error;
4785   END create_csi_systems;
4786 
4787 
4788   PROCEDURE proc_for_last_mtl_trx(
4789     p_source              IN         varchar2,
4790     p_transaction_line_id IN         number,
4791     p_order_line_rec      IN         order_line_rec,
4792     x_return_status       OUT NOCOPY VARCHAR2)
4793   IS
4794     l_order_quantity    number;
4795     l_total_qty         number := 0;
4796     l_return_status     varchar2(1) := fnd_api.g_ret_sts_success;
4797     l_literal1   	VARCHAR2(30) := 'PROCESSED' ;
4798     l_literal2   	VARCHAR2(30) := 'OE_ORDER_LINES_ALL' ;
4799   BEGIN
4800 
4801     x_return_status := fnd_api.g_ret_sts_success;
4802     api_log('proc_for_last_mtl_trx');
4803 
4804     IF p_source = 'FULFILLMENT' THEN
4805       l_order_quantity := p_order_line_rec.fulfilled_quantity;
4806     ELSIF p_source = 'SHIPMENT' THEN
4807       l_order_quantity := p_order_line_rec.shipped_quantity;
4808     END IF;
4809 
4810     BEGIN
4811       SELECT sum(quantity)
4812       INTO   l_total_qty
4813       FROM   csi_item_instances
4814       WHERE  last_oe_order_line_id = p_order_line_rec.order_line_id
4815       AND    inventory_item_id     = p_order_line_rec.inv_item_id;
4816     EXCEPTION
4817       WHEN others then
4818         l_total_qty := 0;
4819     END;
4820 
4821     debug('  order_quantity          : '||l_order_quantity);
4822     debug('  total_inst_qty_for_line : '||l_total_qty);
4823 
4824     IF l_total_qty >= l_order_quantity  THEN
4825 
4826       debug('  last transaction for this order line...!');
4827 
4828       UPDATE csi_t_txn_line_details a
4829       SET    error_code        = NULL,
4830              error_explanation = NULL ,
4831              processing_status = 'PROCESSED'
4832       WHERE  a.processing_status <> l_literal1
4833       AND    a.source_transaction_flag = 'Y'
4834       AND    a.inventory_item_id       = p_order_line_rec.inv_item_id
4835       AND    a.transaction_line_id in (SELECT transaction_line_id
4836 				 FROM csi_t_transaction_lines b
4837                     WHERE -- a.transaction_line_id = b.transaction_line_id AND -- Commented for Perf Bug bug 4311676
4838                      b.source_transaction_id    = p_order_line_rec.order_line_id
4839                      AND  b.source_transaction_table = l_literal2 );
4840 
4841       UPDATE csi_t_transaction_lines
4842       SET    processing_status = 'PROCESSED'
4843       WHERE  source_transaction_id = p_order_line_rec.order_line_id;
4844 
4845       DELETE FROM csi_t_txn_line_details
4846       WHERE  transaction_line_id     = p_transaction_line_id
4847       AND    source_transaction_flag = 'Y'
4848       AND    processing_status      <> l_literal1;
4849 
4850       -- R12 changes because ATO Option funcillment will happen from the OM Code.
4851       /*
4852       IF p_source = 'SHIPMENT' AND p_order_line_rec.item_type_code = 'CONFIG' THEN
4853         debug('  ato config shipment. spawn shippable option lines for fulfillment.');
4854         process_ato_options(
4855           p_order_line_rec   => p_order_line_rec,
4856           x_return_status    => l_return_status);
4857         IF l_return_status <> fnd_api.g_ret_sts_success THEN
4858           RAISE fnd_api.g_exc_error;
4859         END IF;
4860       END IF;
4861       */
4862 
4863     END IF;
4864 
4865   EXCEPTION
4866     WHEN fnd_api.g_exc_error THEN
4867       x_return_status := fnd_api.g_ret_sts_error ;
4868      WHEN fnd_api.g_exc_unexpected_error THEN
4869       x_return_status := fnd_api.g_ret_sts_unexp_error ;
4870   END proc_for_last_mtl_trx ;
4871 
4872   PROCEDURE auto_split_instances(
4873     p_instance_rec            IN  csi_datastructures_pub.instance_rec,
4874     px_txn_rec                IN OUT nocopy csi_datastructures_pub.transaction_rec,
4875     x_instance_tbl               OUT nocopy csi_datastructures_pub.instance_tbl,
4876     x_return_status              OUT nocopy varchar2)
4877   IS
4878     l_src_instance_rec        csi_datastructures_pub.instance_rec;
4879     l_instance_tbl            csi_datastructures_pub.instance_tbl;
4880     l_return_status           varchar2(1) := fnd_api.g_ret_sts_success;
4881     l_msg_count               number;
4882     l_msg_data                varchar2(2000);
4883   BEGIN
4884 
4885     x_return_status := fnd_api.g_ret_sts_success;
4886 
4887     api_log('auto_split_instances');
4888 
4889     l_src_instance_rec.instance_id                := p_instance_rec.instance_id ;
4890     l_src_instance_rec.vld_organization_id        := p_instance_rec.vld_organization_id;
4891     l_src_instance_rec.location_type_code         := p_instance_rec.location_type_code;
4892     l_src_instance_rec.location_id                := p_instance_rec.location_id;
4893     l_src_instance_rec.install_location_type_code := p_instance_rec.install_location_type_code;
4894     l_src_instance_rec.install_location_id        := p_instance_rec.install_location_id;
4895     l_src_instance_rec.instance_usage_code        := p_instance_rec.instance_usage_code;
4896     -- START changes for bug 4050897
4897     l_src_instance_rec.version_label              := p_instance_rec.version_label;
4898     l_src_instance_rec.instance_type_code         := p_instance_rec.instance_type_code;
4899     l_src_instance_rec.instance_condition_id      := p_instance_rec.instance_condition_id;
4900     l_src_instance_rec.return_by_date             := p_instance_rec.return_by_date;
4901     -- END changes for bug 4050897
4902     l_src_instance_rec.inv_organization_id        := null;
4903     l_src_instance_rec.inv_subinventory_name      := null;
4904     l_src_instance_rec.inv_locator_id             := null;
4905     l_src_instance_rec.pa_project_id              := null;
4906     l_src_instance_rec.pa_project_task_id         := null;
4907     l_src_instance_rec.wip_job_id                 := null;
4908     l_src_instance_rec.po_order_line_id           := null;
4909 
4910     csi_t_gen_utility_pvt.dump_api_info(
4911       p_pkg_name => 'csi_item_instance_pvt',
4912       p_api_name => 'split_item_instance_lines');
4913 
4914     csi_item_instance_pvt.split_item_instance_lines(
4915       p_api_version            => 1.0,
4916       p_commit                 => fnd_api.g_false,
4917       p_init_msg_list          => fnd_api.g_true,
4918       p_validation_level       => fnd_api.g_valid_level_full,
4919       p_source_instance_rec    => l_src_instance_rec,
4920       p_copy_ext_attribs       => fnd_api.g_true,
4921       p_copy_org_assignments   => fnd_api.g_true,
4922       p_copy_parties           => fnd_api.g_true,
4923       p_copy_accounts          => fnd_api.g_true,
4924       p_copy_asset_assignments => fnd_api.g_true,
4925       p_copy_pricing_attribs   => fnd_api.g_true,
4926       p_txn_rec                => px_txn_rec,
4927       x_new_instance_tbl       => l_instance_tbl,
4928       x_return_status          => l_return_status,
4929       x_msg_count              => l_msg_count,
4930       x_msg_data               => l_msg_data);
4931 
4932     IF l_return_status not in (fnd_api.g_ret_sts_success, 'W') THEN
4933       RAISE fnd_api.g_exc_error;
4934     END IF;
4935 
4936     x_instance_tbl := l_instance_tbl;
4937 
4938   EXCEPTION
4939     WHEN fnd_api.g_exc_error THEN
4940       x_return_status := fnd_api.g_ret_sts_error;
4941   END auto_split_instances;
4942 
4943   PROCEDURE get_config_wip_id (
4944 	p_order_line_rec       IN order_line_rec,
4945 	p_config_wip_id        OUT NOCOPY NUMBER
4946   )
4947   IS
4948 	l_config_rec          csi_order_fulfill_pub.config_rec;
4949   BEGIN
4950     api_log('get_config_wip_id');
4951     BEGIN
4952       SELECT oel.line_id,
4953              oel.inventory_item_id,
4954              oel.ship_from_org_id,
4955              oel.ordered_quantity,
4956              oel.split_from_line_id
4957       INTO   l_config_rec.line_id,
4958              l_config_rec.item_id,
4959              l_config_rec.ship_organization_id,
4960              l_config_rec.order_quantity,
4961              l_config_rec.split_from_line_id
4962       FROM   oe_order_lines_all  oel
4963       WHERE  oel.header_id         = p_order_line_rec.header_id
4964       AND    oel.link_to_line_id   = p_order_line_rec.ato_line_id
4965       AND    oel.item_type_code    = 'CONFIG';
4966     EXCEPTION
4967       WHEN no_data_found THEN
4968         null;
4969       WHEN too_many_rows THEN
4970         SELECT oel.line_id,
4971                oel.inventory_item_id,
4972                oel.ship_from_org_id,
4973                oel.ordered_quantity,
4974                oel.split_from_line_id
4975         INTO   l_config_rec.line_id,
4976                l_config_rec.item_id,
4977                l_config_rec.ship_organization_id,
4978                l_config_rec.order_quantity,
4979                l_config_rec.split_from_line_id
4980         FROM   oe_order_lines_all  oel
4981         WHERE  oel.header_id         = p_order_line_rec.header_id
4982         AND    oel.link_to_line_id   = p_order_line_rec.ato_line_id
4983         AND    oel.item_type_code    = 'CONFIG'
4984         AND    oel.split_from_line_id is null;
4985 
4986     END;
4987 
4988     IF l_config_rec.line_id is not null THEN
4989 
4990       SELECT serial_number_control_code
4991       INTO   l_config_rec.serial_code
4992       FROM   mtl_system_items
4993       WHERE  inventory_item_id = l_config_rec.item_id
4994       AND    organization_id   = l_config_rec.ship_organization_id;
4995 
4996       csi_order_fulfill_pub.get_job_for_config_line(
4997         p_config_rec           => l_config_rec,
4998         x_wip_entity_id        => l_config_rec.config_wip_job_id,
4999         x_wip_organization_id  => l_config_rec.config_wip_org_id,
5000         x_request_id           => l_config_rec.request_id);
5001 
5002       IF l_config_rec.config_wip_job_id is null THEN
5003         IF l_config_rec.serial_code in (2, 5) THEN
5004           csi_order_fulfill_pub.get_wip_info_from_txn(
5005             px_config_rec  => l_config_rec);
5006         END IF;
5007       END IF;
5008 
5009     END IF;
5010 
5011 	p_config_wip_id := l_config_rec.config_wip_job_id;
5012 
5013   EXCEPTION
5014 	WHEN OTHERS THEN
5015 		debug('Others Exception in get_config_wip_id');
5016   END get_config_wip_id;
5017 
5018   FUNCTION check_option_relationship (
5019     p_option_object_id     IN NUMBER,
5020     p_source               IN VARCHAR2,
5021     p_order_line_rec       IN order_line_rec
5022   )
5023    RETURN BOOLEAN
5024    IS
5025     l_explode_bom_flag boolean := TRUE;
5026     l_option_flag     VARCHAR2(1) := 'Y';
5027     l_config_wip_job_id NUMBER;
5028 
5029    BEGIN
5030       api_log('check_option_relationship');
5031 
5032       -- Bug 10329599
5033       -- Get flag for Phantom Option Item behaviour
5034       -- If l_option_flag - Y then Phantom Option Item BOM explosion wont happen for internally manufactured
5035       --          Config Item. Eligible WIP issuesed instance part of config WIP job
5036       --          will come under phantom option item (as per BOM structure)
5037       --          The behaviour will be similar to Option class
5038       -- If l_option_flag - N  then Phantom Option Item BOM explosion for internally manufactured item will happen
5039       csi_order_fulfill_pub.get_phantom_option_expl_flag
5040       (p_option_expl_flag    =>  l_option_flag);
5041 
5042       -- Get Config WIP Job Id
5043       get_config_wip_id (
5044       p_order_line_rec     => p_order_line_rec,
5045       p_config_wip_id      => l_config_wip_job_id
5046       );
5047 
5048       debug('l_config_wip_job_id - ' || l_config_wip_job_id);
5049       -- Verify if BOM explosion is necessary for Phantom Option Item
5050       IF p_source = 'FULFILLMENT'
5051        AND p_order_line_rec.item_type_code = 'OPTION'
5052        AND NVL(l_config_wip_job_id, FND_API.G_MISS_NUM) <> FND_API.G_MISS_NUM
5053        AND l_option_flag = 'Y' THEN
5054 		 l_explode_bom_flag := FALSE;
5055          debug('check_option_relationship - Relationship for Option Item exist. Dont explode BOM');
5056 		 RETURN l_explode_bom_flag;
5057 		ELSE
5058 		 debug('check_option_relationship - Relationship for Option Item doesnt exist. Explode BOM');
5059 		 RETURN l_explode_bom_flag;
5060 		END IF;
5061 
5062    END check_option_relationship;
5063 
5064   PROCEDURE update_install_base(
5065     p_api_version             IN  number,
5066     p_commit                  IN  varchar2 := fnd_api.g_false,
5067     p_init_msg_list           IN  varchar2 := fnd_api.g_false,
5068     p_validation_level        IN  number   := fnd_api.g_valid_level_full,
5069     p_txn_line_rec            IN  OUT NOCOPY csi_t_datastructures_grp.txn_line_rec,
5070     p_txn_line_detail_tbl     IN  OUT NOCOPY csi_t_datastructures_grp.txn_line_detail_tbl,
5071     p_txn_party_detail_tbl    IN  OUT NOCOPY csi_t_datastructures_grp.txn_party_detail_tbl,
5072     p_txn_pty_acct_dtl_tbl    IN  OUT NOCOPY csi_t_datastructures_grp.txn_pty_acct_detail_tbl,
5073     p_txn_org_assgn_tbl       IN  OUT NOCOPY csi_t_datastructures_grp.txn_org_assgn_tbl,
5074     p_txn_ext_attrib_vals_tbl IN  OUT NOCOPY csi_t_datastructures_grp.txn_ext_attrib_vals_tbl,
5075     p_txn_ii_rltns_tbl        IN  OUT NOCOPY csi_t_datastructures_grp.txn_ii_rltns_tbl,
5076     p_txn_systems_tbl         IN  OUT NOCOPY csi_t_datastructures_grp.txn_systems_tbl,
5077     p_pricing_attribs_tbl     IN  OUT NOCOPY csi_datastructures_pub.pricing_attribs_tbl,
5078     p_order_line_rec          IN  order_line_rec,
5079     p_trx_rec                 IN  csi_datastructures_pub.transaction_rec,
5080     p_source                  IN  varchar2,
5081     p_validate_only           IN  varchar2,
5082     px_error_rec              IN OUT NOCOPY csi_datastructures_pub.transaction_error_rec,
5083     x_return_status           OUT NOCOPY varchar2,
5084     x_msg_count               OUT NOCOPY number,
5085     x_msg_data                OUT NOCOPY varchar2)
5086   IS
5087 
5088     l_api_name          CONSTANT varchar2(30)   := 'update_install_base';
5089     l_api_version       CONSTANT NUMBER         := 1.0;
5090     l_csi_debug_level            varchar2(1);
5091 
5092     l_return_status              varchar2(1) := fnd_api.g_ret_sts_success;
5093     l_msg_count                  number;
5094     l_msg_data                   varchar2(2000);
5095 
5096     l_order_line_rec             order_line_rec;
5097     l_item_control_rec           item_control_rec;
5098 
5099     x_trx_sub_type_rec           txn_sub_type_rec;
5100     l_tmp_instance_rec           csi_datastructures_pub.instance_rec;
5101     l_upd_instance_rec           csi_datastructures_pub.instance_rec;
5102     l_upd_txn_rec                csi_datastructures_pub.transaction_rec;
5103     l_upd_party_tbl              csi_datastructures_pub.party_tbl;
5104     l_upd_party_acct_tbl         csi_datastructures_pub.party_account_tbl;
5105     l_upd_inst_asset_tbl         csi_datastructures_pub.instance_asset_tbl;
5106     l_upd_ext_attrib_val_tbl     csi_datastructures_pub.extend_attrib_values_tbl;
5107     l_upd_pricing_attribs_tbl    csi_datastructures_pub.pricing_attribs_tbl;
5108     l_upd_org_units_tbl          csi_datastructures_pub.organization_units_tbl;
5109     l_inst_id_lst                csi_datastructures_pub.id_tbl;
5110     l_upd_ii_rltns_tbl           csi_datastructures_pub.ii_relationship_tbl;
5111 
5112     l_upd_so_instance_rec        csi_datastructures_pub.instance_rec;
5113     l_upd_so_party_tbl           csi_datastructures_pub.party_tbl;
5114     l_upd_so_party_acct_tbl      csi_datastructures_pub.party_account_tbl;
5115     l_upd_so_inst_asset_tbl      csi_datastructures_pub.instance_asset_tbl;
5116     l_upd_so_ext_attrib_val_tbl  csi_datastructures_pub.extend_attrib_values_tbl;
5117     l_upd_so_pricing_attribs_tbl csi_datastructures_pub.pricing_attribs_tbl;
5118     l_upd_so_org_units_tbl       csi_datastructures_pub.organization_units_tbl;
5119 
5120     l_cre_instance_rec           csi_datastructures_pub.instance_rec;
5121     l_cre_txn_rec                csi_datastructures_pub.transaction_rec;
5122     l_cre_party_tbl              csi_datastructures_pub.party_tbl;
5123     l_cre_party_acct_tbl         csi_datastructures_pub.party_account_tbl;
5124     l_cre_inst_asset_tbl         csi_datastructures_pub.instance_asset_tbl;
5125     l_cre_ext_attrib_val_tbl     csi_datastructures_pub.extend_attrib_values_tbl;
5126     l_cre_pricing_attribs_tbl    csi_datastructures_pub.pricing_attribs_tbl;
5127     l_cre_org_units_tbl          csi_datastructures_pub.organization_units_tbl;
5128     l_cre_ii_rltns_tbl           csi_datastructures_pub.ii_relationship_tbl;
5129     l_cre_systems_rec            csi_datastructures_pub.system_rec;
5130 
5131     l_txn_line_rec               csi_t_datastructures_grp.txn_line_rec;
5132     l_txn_line_query_rec         csi_t_datastructures_grp.txn_line_query_rec;
5133     l_txn_line_detail_tbl        csi_t_datastructures_grp.txn_line_detail_tbl;
5134     l_txn_party_detail_tbl       csi_t_datastructures_grp.txn_party_detail_tbl;
5135     l_txn_pty_acct_detail_tbl    csi_t_datastructures_grp.txn_pty_acct_detail_tbl;
5136     l_txn_ii_rltns_tbl           csi_t_datastructures_grp.txn_ii_rltns_tbl;
5137     l_txn_ext_attrib_vals_tbl    csi_t_datastructures_grp.txn_ext_attrib_vals_tbl;
5138     l_csi_ext_attribs_tbl        csi_t_datastructures_grp.csi_ext_attribs_tbl;
5139     l_csi_iea_values_tbl         csi_t_datastructures_grp.csi_ext_attrib_vals_tbl;
5140     l_txn_org_assgn_tbl          csi_t_datastructures_grp.txn_org_assgn_tbl  ;
5141     l_txn_systems_tbl            csi_t_datastructures_grp.txn_systems_tbl;
5142 
5143     l_upd_txn_ii_rltns_tbl       csi_t_datastructures_grp.txn_ii_rltns_tbl;
5144     l_upd_txn_party_detail_tbl   csi_t_datastructures_grp.txn_party_detail_tbl;
5145     l_upd_txn_pty_acct_dtl_tbl   csi_t_datastructures_grp.txn_pty_acct_detail_tbl;
5146     l_upd_txn_org_assgn_tbl      csi_t_datastructures_grp.txn_org_assgn_tbl;
5147     l_upd_txn_ext_attr_vals_tbl  csi_t_datastructures_grp.txn_ext_attrib_vals_tbl;
5148 
5149     /*  Pricing Attributes variables */
5150     l_old_index  NUMBER := 0;
5151     l_new_index  NUMBER := 0;
5152     l_old_pricing_attribs_tbl    csi_datastructures_pub.pricing_attribs_tbl;
5153     l_new_pricing_attribs_tbl    csi_datastructures_pub.pricing_attribs_tbl;
5154 
5155     /*  Extended Attributes variables */
5156     l_ext_old_index  NUMBER := 0;
5157     l_ext_new_index  NUMBER := 0;
5158     l_old_extended_attribs_tbl    csi_datastructures_pub.extend_attrib_values_tbl;
5159     l_new_extended_attribs_tbl    csi_datastructures_pub.extend_attrib_values_tbl;
5160 
5161     /* Decrement and Increment Logic variables */
5162     l_un_exp_instance_rec         csi_datastructures_pub.instance_rec;
5163     l_un_exp_party_tbl            csi_datastructures_pub.party_tbl;
5164     l_un_exp_party_acct_tbl       csi_datastructures_pub.party_account_tbl;
5165     l_un_exp_inst_asset_tbl       csi_datastructures_pub.instance_asset_tbl;
5166     l_un_exp_ext_attrib_val_tbl   csi_datastructures_pub.extend_attrib_values_tbl;
5167     l_un_exp_pricing_attribs_tbl  csi_datastructures_pub.pricing_attribs_tbl;
5168     l_un_exp_org_units_tbl        csi_datastructures_pub.organization_units_tbl;
5169     l_un_exp_ii_rltns_tbl         csi_datastructures_pub.ii_relationship_tbl;
5170     l_un_exp_systems_rec          csi_datastructures_pub.system_rec;
5171 
5172     l_trx_rec                    csi_datastructures_pub.transaction_rec;
5173     l_auto_split_instances       csi_datastructures_pub.instance_tbl;
5174 
5175     l_split_src_inst_rec         csi_datastructures_pub.instance_rec;
5176     l_split_src_trx_rec          csi_datastructures_pub.transaction_rec;
5177     l_split_new_inst_rec         csi_datastructures_pub.instance_rec;
5178     l_sys_query_rec              csi_datastructures_pub.system_query_rec;
5179     x_systems_tbl                csi_datastructures_pub.systems_tbl;
5180 
5181     /* expire item instance variables */
5182     l_exp_instance_rec           csi_datastructures_pub.instance_rec;
5183     l_exp_instance_id_lst        csi_datastructures_pub.id_tbl;
5184     l_expire_flag                BOOLEAN := FALSE;
5185 
5186     /* non source processing variables */
5187     l_ns_instance_rec            csi_datastructures_pub.instance_rec;
5188     l_ns_party_tbl               csi_datastructures_pub.party_tbl;
5189     l_ns_party_acct_tbl          csi_datastructures_pub.party_account_tbl;
5190     l_ns_pricing_attribs_tbl     csi_datastructures_pub.pricing_attribs_tbl;
5191     l_ns_ext_attrib_val_tbl      csi_datastructures_pub.extend_attrib_values_tbl;
5192     l_ns_org_units_tbl           csi_datastructures_pub.organization_units_tbl;
5193     l_ns_inst_asset_tbl          csi_datastructures_pub.instance_asset_tbl;
5194     l_ns_inst_id_lst             csi_datastructures_pub.id_tbl;
5195 
5196     l_chg_instance_rec           csi_datastructures_pub.instance_rec;
5197     l_chg_party_tbl              csi_datastructures_pub.party_tbl;
5198     l_chg_party_acct_tbl         csi_datastructures_pub.party_account_tbl;
5199     l_chg_pricing_attribs_tbl    csi_datastructures_pub.pricing_attribs_tbl;
5200     l_chg_ext_attrib_val_tbl     csi_datastructures_pub.extend_attrib_values_tbl;
5201     l_chg_org_units_tbl          csi_datastructures_pub.organization_units_tbl;
5202     l_chg_inst_asset_tbl         csi_datastructures_pub.instance_asset_tbl;
5203     l_chg_inst_id_lst            csi_datastructures_pub.id_tbl;
5204     l_chg_txn_rec                csi_datastructures_pub.transaction_rec;
5205 
5206     l_all_tld_party_tbl          csi_t_datastructures_grp.txn_party_detail_tbl;
5207     l_all_tld_account_tbl        csi_t_datastructures_grp.txn_pty_acct_detail_tbl;
5208     l_all_tld_oa_tbl             csi_t_datastructures_grp.txn_org_assgn_tbl;
5209     l_all_tld_ea_tbl             csi_t_datastructures_grp.txn_ext_attrib_vals_tbl;
5210 
5211     l_tld_rec                    csi_t_datastructures_grp.txn_line_detail_rec;
5212     l_tld_party_tbl              csi_t_datastructures_grp.txn_party_detail_tbl;
5213     l_tld_account_tbl            csi_t_datastructures_grp.txn_pty_acct_detail_tbl;
5214     l_tld_oa_tbl                 csi_t_datastructures_grp.txn_org_assgn_tbl;
5215     l_tld_ea_tbl                 csi_t_datastructures_grp.txn_ext_attrib_vals_tbl;
5216 
5217     l_quantity1                NUMBER;
5218     l_quantity2                NUMBER;
5219     x_system_id                NUMBER;
5220     l_model_inst_id            NUMBER := -1;
5221     l_option_inst_id           NUMBER := -1;
5222     x_master_org_id            NUMBER ;
5223     l_object_inst_id           NUMBER;
5224     l_trx_type_id              NUMBER;
5225     l_subject_inst_id          NUMBER;
5226     l_rel_exist                BOOLEAN := TRUE;
5227     l_end                      NUMBER := 1;
5228     l_split_flag               varchar2(10);
5229     l_found                    BOOLEAN := FALSE;
5230     l_ii_rel_id                NUMBER;
5231     l_curr_object_id           NUMBER;
5232     l_curr_subject_id          NUMBER;
5233     l_date                     DATE := fnd_api.g_miss_date;
5234     l_debug_level              NUMBER;
5235     l_inst_status_id           NUMBER;
5236     l_inst_obj_ver_num         NUMBER;
5237     l_pty_changed              BOOLEAN := FALSE;
5238     l_curr_pty_id              NUMBER;
5239     l_instance_id              NUMBER;
5240     l_curr_instance_qty        NUMBER;
5241     l_instance_party_id        NUMBER;
5242     l_int_pty_obj_ver_num      NUMBER;
5243     l_ip_account_id            NUMBER;
5244     l_pty_acct_obj_ver_num     NUMBER;
5245     l_instance_ou_id           NUMBER;
5246     l_ou_obj_ver_num           NUMBER;
5247     l_attrib_value_id          NUMBER;
5248     l_av_obj_ver_num           NUMBER;
5249     l_party_id                 NUMBER;
5250     l_party_acct_id            NUMBER;
5251     l_start                    NUMBER;
5252     l_bom_explode_flag         BOOLEAN := FALSE;
5253     l_party_site_id            NUMBER;
5254     l_total_qty_processed      NUMBER := 0;
5255     l_system_id                NUMBER;
5256     l_system_exist             BOOLEAN := FALSE;
5257     l_parent_tbl_index         NUMBER;
5258     l_process_acct_flag        BOOLEAN := TRUE;
5259     l_explosion_level          NUMBER := 0;
5260     l_so_qty                   NUMBER;
5261     l_so_instance_id           NUMBER;
5262     l_so_obj_ver_num           NUMBER;
5263     is_instance_rma            BOOLEAN := FALSE;
5264     l_relationship_id          NUMBER;
5265     l_ii_rel_obj_ver_num       NUMBER;
5266     l_instance_pty_id          NUMBER;
5267     l_exp_ii_relationship_rec  csi_datastructures_pub.ii_relationship_rec;
5268     l_exp_instance_id_tbl      csi_datastructures_pub.id_tbl;
5269 
5270     l_dflt_inst_status_id      NUMBER;
5271     l_owner_party_id           NUMBER;
5272     l_internal_party_id        NUMBER;
5273     l_ownership_flag           VARCHAR2(1);
5274 
5275     -- explode_bom variables
5276     l_bom_ind                  binary_integer := 0;
5277     l_bom_std_item_rec         csi_datastructures_pub.instance_rec;
5278     l_bom_std_item_tbl         csi_datastructures_pub.instance_tbl;
5279 
5280     l_comp_instance_tbl        csi_datastructures_pub.instance_tbl;
5281     l_comp_relation_tbl        csi_datastructures_pub.ii_relationship_tbl;
5282 
5283     l_active_end_date          date := fnd_api.g_miss_date;
5284     l_call_contracts           varchar2(1) := fnd_api.g_true;
5285 
5286     -- profile value checking
5287     l_default_install_date    VARCHAR2(1);
5288 
5289     l_install_party_site_id   NUMBER;
5290 
5291     -- Partner Order changes
5292     l_upd_party_site_id        number;
5293     l_cre_party_site_id        number;
5294 
5295     l_owner_pty_rec            csi_datastructures_pub.party_rec;
5296     l_owner_acct_rec           csi_datastructures_pub.party_account_rec;
5297 
5298     --brmanesh 01-DEC-2003
5299     l_wip_job_id               number;
5300     l_wip_iir_tbl              csi_datastructures_pub.ii_relationship_tbl;
5301     l_cps_tbl                  customer_products_tbl;
5302     l_cp_ind                   binary_integer := 0;
5303     l_all_cps_tbl              customer_products_tbl;
5304     l_single_cps_tbl           customer_products_tbl;
5305     l_acp_ind                  binary_integer := 0;
5306     -- Added this for unlock_item_instances
5307     l_config_tbl               csi_cz_int.config_tbl;
5308     l_cia_found                varchar2(1);
5309 
5310 	--Added variables for bug 10329599
5311 	l_shippable_item_flag  varchar2(1) := 'N';
5312 	l_inv_transactable_flag varchar2(1) := 'N';
5313 	l_option_flag            VARCHAR2(1) := 'Y';
5314   l_relationship_exist     BOOLEAN := FALSE;
5315 
5316   BEGIN
5317 
5318     /* Standard Start of API savepoint */
5319     SAVEPOINT  update_install_base;
5320 
5321     api_log('update_install_base');
5322 
5323     /* Initialize message list if p_init_msg_list is set to TRUE. */
5324     IF FND_API.to_Boolean( p_init_msg_list ) THEN
5325       fnd_msg_pub.initialize;
5326     END IF;
5327 
5328     /* Initialize API return status to success */
5329     x_return_status := fnd_api.g_ret_sts_success;
5330 
5331     -- Bug 10329599
5332     -- Get flag for Phantom Option Item behaviour
5333     -- If l_option_flag - Y then Phantom Option Item BOM explosion wont happen for internally manufactured
5334     --          Config Item. Eligible WIP issuesed instance part of config WIP job
5335     --          will come under phantom option item (as per BOM structure)
5336     --          The behaviour will be similar to Option class
5337     -- If l_option_flag - N  then Phantom Option Item BOM explosion for internally manufactured item will happen
5338     csi_order_fulfill_pub.get_phantom_option_expl_flag
5339     (p_option_expl_flag    =>  l_option_flag);
5340 
5341     debug('  txn_line_detail_tbl.count     :'||p_txn_line_detail_tbl.count );
5342     debug('  txn_party_detail_tbl.count    :'||p_txn_party_detail_tbl.count );
5343     debug('  txn_pty_acct_dtl_tbl.count    :'||p_txn_pty_acct_dtl_tbl.count);
5344     debug('  txn_org_assgn_tbl.count       :'||p_txn_org_assgn_tbl.count);
5345     debug('  txn_ii_rltns_tbl.count        :'||p_txn_ii_rltns_tbl.count);
5346     debug('  txn_ext_attrib_vals_tbl.count :'||p_txn_ext_attrib_vals_tbl.count);
5347     debug('  pricing_attribs_tbl.count     :'||p_pricing_attribs_tbl.count);
5348     debug('  txn_systems_tbl.count         :'||p_txn_systems_tbl.count);
5349 
5350    IF p_txn_line_detail_tbl.count <= 0
5351    THEN
5352        fnd_message.set_name('CSI', 'CSI_CANNOT_UPDATE');
5353        fnd_message.set_token('OBJECT_ID','');
5354        fnd_message.set_token('RELATIONSHIP_TYPE_CODE','');
5355        fnd_msg_pub.add;
5356        Raise fnd_api.g_exc_error;
5357    END IF;
5358 
5359     /* get the profile value for CSI default install date */
5360     BEGIN
5361        SELECT nvl(fnd_profile.value('CSI_DEF_INST_DATE'),'N')
5362        INTO   l_default_install_date
5363        FROM   dual;
5364     EXCEPTION
5365        WHEN OTHERS THEN
5366           NULL;
5367     END;
5368 
5369     /* assign the trx_rec that will be used for all trx's in IB*/
5370     l_trx_rec := p_trx_rec;
5371 
5372     /* get the debug level from the profile */
5373     l_debug_level := csi_t_gen_utility_pvt.g_debug_level;
5374 
5375     l_order_line_rec := p_order_line_rec;
5376 
5377     /* validate the txn table and its child tables */
5378     validate_txn_tbl(
5379       p_txn_line_rec         => p_txn_line_rec,
5380       p_txn_line_detail_tbl  => p_txn_line_detail_tbl,
5381       p_txn_party_detail_tbl => p_txn_party_detail_tbl,
5382       p_txn_pty_acct_dtl_tbl => p_txn_pty_acct_dtl_tbl,
5383       p_txn_ii_rltns_tbl     => p_txn_ii_rltns_tbl,
5384       p_txn_org_assgn_tbl    => p_txn_org_assgn_tbl,
5385       p_order_line_rec       => l_order_line_rec,
5386       p_source               => p_source,
5387       x_return_status        => x_return_status);
5388 
5389     IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
5390       raise fnd_api.g_exc_error;
5391     END IF;
5392 
5393     l_all_tld_party_tbl   := p_txn_party_detail_tbl;
5394     l_all_tld_account_tbl := p_txn_pty_acct_dtl_tbl;
5395     l_all_tld_oa_tbl      := p_txn_org_assgn_tbl;
5396     l_all_tld_ea_tbl      := p_txn_ext_attrib_vals_tbl;
5397 
5398     /* Process the txn details only if  p_validate_only is 'N' */
5399     IF p_validate_only = 'N' THEN
5400 
5401       /*-----------------------------------------------*/
5402       /* Check if the systems exists,if found          */
5403       /* then use the system else create a new system  */
5404       /*-----------------------------------------------*/
5405       IF p_txn_systems_tbl.count > 0 THEN
5406         create_csi_systems(
5407           px_csi_txn_rec     => l_trx_rec,
5408           px_txn_systems_tbl => p_txn_systems_tbl,
5409           px_tld_tbl         => p_txn_line_detail_tbl,
5410           x_return_status    => l_return_status);
5411 
5412         IF l_return_status <> fnd_api.g_ret_sts_success THEN
5413           RAISE fnd_api.g_exc_error;
5414         END IF;
5415       END IF; -- end if for system table count > 0
5416 
5417       csi_utl_pkg.get_dflt_inst_status_id(
5418         x_instance_status_id  => l_dflt_inst_status_id,
5419         x_return_status       => l_return_status);
5420 
5421       IF l_return_status <> fnd_api.g_ret_sts_success THEN
5422         RAISE fnd_api.g_exc_error;
5423       END IF;
5424 
5425       l_split_flag := nvl(fnd_profile.value('CSI_AUTO_SPLIT_INSTANCE' ),'N');
5426 
5427       debug('  profile_auto_split : '||l_split_flag);
5428 
5429 
5430       /* Process the txn details and the child tables     */
5431 
5432       IF p_txn_line_detail_tbl.count > 0 THEN
5433 
5434         debug ('Processing the txn line dtl and child tables.' );
5435 
5436         FOR i IN p_txn_line_detail_tbl.FIRST..p_txn_line_detail_tbl.LAST
5437         LOOP
5438 
5439           l_tld_rec := p_txn_line_detail_tbl(i);
5440 
5441           debug('processing tld record # '||i||' source_flag : '||l_tld_rec.source_transaction_flag);
5442 
5443           l_cp_ind := 0;
5444           l_cps_tbl.delete;
5445 
5446           /* Process only txn details that have the source_trx_flag = 'Y' */
5447           IF l_debug_level > 1 THEN
5448             csi_t_gen_utility_pvt.dump_line_detail_rec(l_tld_rec);
5449           END IF;
5450 
5451           get_tld_set(
5452             px_tld_rec          => l_tld_rec,
5453             px_tld_party_tbl    => l_all_tld_party_tbl,
5454             px_tld_account_tbl  => l_all_tld_account_tbl,
5455             px_tld_oa_tbl       => l_all_tld_oa_tbl,
5456             px_tld_ea_tbl       => l_all_tld_ea_tbl,
5457             -- out set
5458             x_tld_party_tbl     => l_tld_party_tbl,
5459             x_tld_account_tbl   => l_tld_account_tbl,
5460             x_tld_oa_tbl        => l_tld_oa_tbl,
5461             x_tld_ea_tbl        => l_tld_ea_tbl,
5462             x_return_status     => l_return_status);
5463 
5464         IF (l_tld_rec.source_transaction_flag = 'Y') AND
5465            (l_tld_rec.processing_status = 'IN_PROCESS') THEN
5466 
5467           px_error_rec.serial_number := l_tld_rec.serial_number;
5468           px_error_rec.lot_number    := l_tld_rec.lot_number;
5469           px_error_rec.instance_id   := l_tld_rec.instance_id;
5470 
5471           /* Initialize the pl/sql table for each txn_line_dtls*/
5472           l_cre_party_tbl.delete;
5473           l_upd_party_tbl.delete;
5474           l_cre_party_acct_tbl.delete;
5475           l_upd_party_acct_tbl.delete;
5476           l_cre_org_units_tbl.delete;
5477           l_upd_org_units_tbl.delete;
5478           l_cre_ext_attrib_val_tbl.delete;
5479           l_upd_ext_attrib_val_tbl.delete;
5480           l_cre_pricing_attribs_tbl.delete;
5481 
5482           /* Keep the txn_line_dtl qty processed */
5483           l_total_qty_processed := l_total_qty_processed + l_tld_rec.quantity;
5484 
5485           /* Get the sub_type_rec from the txn line_dtls */
5486 
5487           csi_utl_pkg.get_sub_type_rec(
5488             p_sub_type_id            => l_tld_rec.sub_type_id,
5489             p_trx_type_id            => p_txn_line_rec.source_transaction_type_id,
5490             x_trx_sub_type_rec       => x_trx_sub_type_rec,
5491             x_return_status          => x_return_status) ;
5492 
5493           IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
5494             raise fnd_api.g_exc_error;
5495           END IF;
5496 
5497           csi_utl_pkg.get_org_assignment_tbl(
5498             p_txn_line_detail_rec     => l_tld_rec,
5499             p_txn_org_assgn_tbl       => l_tld_oa_tbl,
5500             x_cre_org_units_tbl       => l_cre_org_units_tbl,
5501             x_upd_org_units_tbl       => l_upd_org_units_tbl,
5502             x_return_status           => x_return_status);
5503 
5504           IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
5505             raise fnd_api.g_exc_error;
5506           END IF;
5507 
5508           csi_utl_pkg.get_ext_attribs_tbl(
5509             p_txn_line_detail_rec     => l_tld_rec,
5510             p_txn_ext_attrib_vals_tbl => l_tld_ea_tbl,
5511             x_cre_ext_attrib_val_tbl  => l_cre_ext_attrib_val_tbl,
5512             x_upd_ext_attrib_val_tbl  => l_upd_ext_attrib_val_tbl,
5513             x_return_status           => x_return_status );
5514 
5515           IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
5516             raise fnd_api.g_exc_error;
5517           END IF;
5518 
5519           csi_utl_pkg.get_party_owner(
5520             p_txn_line_detail_rec     => l_tld_rec,
5521             p_txn_party_detail_tbl    => l_tld_party_tbl,
5522             p_txn_pty_acct_dtl_tbl    => l_tld_account_tbl,
5523             x_trx_sub_type_rec        => x_trx_sub_type_rec,
5524             p_order_line_rec          => p_order_line_rec,
5525             x_upd_party_tbl           => l_upd_party_tbl,
5526             x_upd_party_acct_tbl      => l_upd_party_acct_tbl,
5527             x_cre_party_tbl           => l_cre_party_tbl,
5528             x_cre_party_acct_tbl      => l_cre_party_acct_tbl,
5529             x_return_status           => x_return_status );
5530 
5531           IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
5532             raise fnd_api.g_exc_error;
5533           END IF;
5534 
5535 
5536           IF p_source = 'SHIPMENT' THEN
5537             IF l_upd_party_acct_tbl.COUNT = 0 THEN
5538               l_upd_party_acct_tbl :=   l_cre_party_acct_tbl;
5539               l_cre_party_acct_tbl.delete;
5540             END IF;
5541           ELSIF p_source = 'FULFILLMENT' THEN
5542             -- Bug 4996316, these are demo conversion shipments and have a need to convert
5543             -- an externally located but internally owned item instance to CP
5544             -- current check for it is simple and additional checks may be added in here...
5545             IF nvl(l_tld_rec.instance_id, fnd_api.g_miss_num) <> fnd_api.g_miss_num
5546             THEN
5547              IF l_upd_party_acct_tbl.COUNT = 0 THEN
5548                 l_upd_party_acct_tbl :=   l_cre_party_acct_tbl;
5549                 l_cre_party_acct_tbl.delete;
5550              END IF;
5551             END IF;
5552           END IF;
5553 
5554           debug('After building all the pl/sql tables');
5555           debug('  upd_party_tbl.count           :'||l_upd_party_tbl.count);
5556           debug('  upd_party_acct_tbl.count      :'||l_upd_party_acct_tbl.count);
5557           debug('  upd_org_units_tbl.count       :'||l_upd_org_units_tbl.count);
5558           debug('  upd_inst_asset_tbl.count      :'||l_upd_inst_asset_tbl.count);
5559           debug('  upd_ext_attrib_val_tbl.count  :'||l_upd_ext_attrib_val_tbl.count);
5560           debug('  upd_pricing_attribs_tbl.count :'||l_upd_pricing_attribs_tbl.count);
5561           debug('  cre_party_tbl.count           :'||l_cre_party_tbl.count);
5562           debug('  cre_party_acct_tbl.count      :'||l_cre_party_acct_tbl.count);
5563           debug('  cre_org_units_tbl.count       :'||l_cre_org_units_tbl.count);
5564           debug('  cre_inst_asset_tbl.count      :'||l_cre_inst_asset_tbl.count);
5565           debug('  cre_ext_attrib_val_tbl.count  :'||l_cre_ext_attrib_val_tbl.count);
5566           debug('  cre_pricing_attribs_tbl.count :'||l_cre_pricing_attribs_tbl.count);
5567 
5568           -- Forward Port bug 7420858 for base bug 7312328
5569           -- Also Bug 9682428
5570           -- When current location is provided in OM lines
5571           -- Current location id should override ship to org location
5572           -- If both location_id and ib_current_loc_id is not null
5573           -- ib_current_loc_id should be used for
5574           -- current location of the instance
5575 
5576           -- IF ((NVL(l_tld_rec.location_id, fnd_api.g_miss_num) = fnd_api.g_miss_num AND
5577           --   p_order_line_rec.ib_current_loc_id IS NOT NULL)  -- added for bug 5069906
5578          /* IF NVL(p_order_line_rec.ib_current_loc_id, fnd_api.g_miss_num) <> fnd_api.g_miss_num
5579 	        THEN
5580 
5581             BEGIN
5582 
5583               SELECT party_site_id
5584               INTO   l_party_site_id
5585               FROM   hz_cust_acct_sites_all c,
5586                      hz_cust_site_uses_all u
5587               WHERE  c.cust_acct_site_id = u.cust_acct_site_id
5588               AND    u.site_use_id = p_order_line_rec.ib_current_loc_id; -- ship_to_org_id;
5589 
5590             EXCEPTION
5591               WHEN no_data_found then
5592                 fnd_message.set_name('CSI','CSI_INT_PTY_SITE_MISSING');
5593                 fnd_message.set_token('LOCATION_ID', l_tld_rec.location_id);
5594                 fnd_msg_pub.add;
5595                 raise fnd_api.g_exc_error;
5596               WHEN too_many_rows then
5597                 debug('Many Party sites found');
5598                 raise fnd_api.g_exc_error;
5599             END;
5600 
5601             l_tld_rec.location_id        := l_party_site_id;
5602             l_tld_rec.location_type_code := 'HZ_PARTY_SITES';
5603 
5604           END IF;
5605 
5606           -- Forward Port bug 7420858 for base bug 7312328
5607           --IF NVL(l_tld_rec.install_location_id, fnd_api.g_miss_num) = fnd_api.g_miss_num
5608 	        --AND  NVL(p_order_line_rec.ib_install_loc_id, fnd_api.g_miss_num) <> fnd_api.g_miss_num THEN --5147603 -- Modified for the bug 9924233
5609           IF
5610 
5611             BEGIN
5612 
5613               SELECT party_site_id
5614               INTO   l_install_party_site_id
5615               FROM   hz_cust_acct_sites_all c,
5616                      hz_cust_site_uses_all u
5617               WHERE  c.cust_acct_site_id = u.cust_acct_site_id
5618               AND    u.site_use_id = p_order_line_rec.ib_install_loc_id; -- ship_to_org_id;
5619 
5620             EXCEPTION
5621               WHEN no_data_found then
5622                 fnd_message.set_name('CSI','CSI_INT_PTY_SITE_MISSING');
5623                 fnd_message.set_token('LOCATION_ID',p_order_line_rec.ib_install_loc_id);--5147603
5624                 fnd_msg_pub.add;
5625                 raise fnd_api.g_exc_error;
5626               WHEN too_many_rows then
5627                 debug('Many Party sites found');
5628                 raise fnd_api.g_exc_error;
5629             END;
5630 
5631             l_tld_rec.install_location_id        := l_install_party_site_id;
5632             l_tld_rec.install_location_type_code := 'HZ_PARTY_SITES';
5633 
5634           END IF;*/
5635 
5636           -- Bug 9924233
5637           -- p_order_line_rec.ib_install_loc_id and p_order_line_rec.ib_install_loc_id
5638           -- contains the party site id which is the location id
5639           -- Above query was commented out because site use id was
5640           -- converted in to site id in in the order fulfillment API
5641           -- after getting get_partner_order_info. Also the fix in 9682428 is
5642           -- retained
5643 
5644           IF NVL(p_order_line_rec.ib_current_loc_id, fnd_api.g_miss_num) <> fnd_api.g_miss_num THEN
5645 
5646             l_tld_rec.location_id        := p_order_line_rec.ib_current_loc_id;
5647             l_tld_rec.location_type_code := 'HZ_PARTY_SITES';
5648 
5649           END IF;
5650 
5651           IF NVL(p_order_line_rec.ib_install_loc_id, fnd_api.g_miss_num) <> fnd_api.g_miss_num THEN
5652 
5653             l_tld_rec.install_location_id        := p_order_line_rec.ib_install_loc_id;
5654             l_tld_rec.install_location_type_code := 'HZ_PARTY_SITES';
5655 
5656           END IF;
5657           -- End 9924233
5658 
5659 
5660           /* If the instance reference exists then call update instance api else */
5661           /* call create instance api .Also check if the split instance profile  */
5662           /* is on then split it into so many number of instances                */
5663 
5664           IF NVL(l_tld_rec.instance_id,fnd_api.g_miss_num ) <> fnd_api.g_miss_num THEN
5665 
5666            l_upd_instance_rec := l_tmp_instance_rec;
5667 
5668             csi_utl_pkg.get_instance(
5669               p_instance_id        => l_tld_rec.instance_id,
5670               x_obj_version_number => l_inst_obj_ver_num,
5671               x_inst_qty           => l_curr_instance_qty,
5672               x_return_status      => x_return_status);
5673 
5674             IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
5675               raise fnd_api.g_exc_error;
5676             END IF;
5677 
5678             debug('Referenced Instance ID :'||l_tld_rec.instance_id);
5679             debug('  object_ver_num       :'||l_inst_obj_ver_num);
5680             debug('  quantitity           :'||l_curr_instance_qty);
5681             debug('  returned instance id :'||l_tld_rec.changed_instance_id);
5682 
5683             l_instance_id := l_tld_rec.instance_id;
5684 
5685             IF p_order_line_rec.serial_code = 6 THEN
5686 
5687               /*--------------------------------------------------------------------*/
5688               /* Check if the instance exists with shipped serial number then it is */
5689               /* a shipping of returned instance So decrement the source instance   */
5690               /* and update the destination instance to make it a cp                */
5691               /*--------------------------------------------------------------------*/
5692 
5693               BEGIN
5694 
5695                 SELECT instance_id,
5696                        quantity,
5697                        object_version_number
5698                 INTO   l_so_instance_id,
5699                        l_so_qty,
5700                        l_so_obj_ver_num
5701                 FROM   csi_item_instances
5702                 WHERE  serial_number       = l_tld_rec.serial_number
5703                 AND    inventory_item_id   = l_tld_rec.inventory_item_id
5704                 AND    (instance_usage_code = 'RETURNED' --added the outer braces for bug6310708
5705 		--Start of code for 6188180
5706                 OR (instance_usage_code = 'IN_TRANSIT'
5707                     AND
5708                     active_end_date IS NOT NULL)
5709 		--End of code for 6188180
5710                 -- Bug 10092644
5711                 OR (instance_usage_code = 'IN_WIP'
5712                     AND
5713                     active_end_date IS NOT NULL)
5714                   );
5715 
5716 
5717                 debug('Returned or In Transit or Expired In WIP instance found. Instance ID :'||l_so_instance_id);
5718                 debug('Re-using Returned or In Transit or Expired In WIP instance serial instance');
5719 
5720                 decrement_source_instance(
5721                   p_instance_id    => l_tld_rec.instance_id,
5722                   p_quantity       => 1,
5723                   p_trx_rec        => l_trx_rec,
5724                   x_return_status  => l_return_status);
5725 
5726                 IF l_return_status <> fnd_api.g_ret_sts_success THEN
5727                   RAISE fnd_api.g_exc_error;
5728                 END IF;
5729 
5730                 debug('Rebuilding the TD tables for the returned instance.');
5731 
5732                 l_curr_instance_qty := l_so_qty ;
5733                 l_inst_obj_ver_num  := l_so_obj_ver_num;
5734                 l_instance_id       := l_so_instance_id;
5735 
5736                 csi_utl_pkg.rebuild_tbls(
5737                   p_new_instance_id        => l_so_instance_id,
5738                   x_upd_party_tbl          => l_upd_party_tbl,
5739                   x_upd_party_acct_tbl     => l_upd_party_acct_tbl,
5740                   x_upd_org_units_tbl      => l_upd_org_units_tbl,
5741                   x_upd_ext_attrib_val_tbl => l_upd_ext_attrib_val_tbl,
5742                   x_cre_org_units_tbl      => l_cre_org_units_tbl,
5743                   x_cre_ext_attrib_val_tbl => l_cre_ext_attrib_val_tbl,
5744                   x_txn_ii_rltns_tbl       => p_txn_ii_rltns_tbl,
5745                   x_txn_line_detail_rec    => l_tld_rec,
5746                   x_return_status          => x_return_status);
5747 
5748                 IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
5749                   raise fnd_api.g_exc_error;
5750                 END IF;
5751 
5752                 -- Mark the instance as RMA so that it is not eligible for split
5753                 is_instance_rma := TRUE;
5754 
5755               EXCEPTION
5756                WHEN no_data_found THEN
5757                  is_instance_rma := FALSE;
5758                  debug('Returned Instance not found for the serial number.');
5759               END;
5760             END IF;
5761 
5762             /* this code is exclusively for re-shipping a returned non serial config */
5763             /* instance. ATO non serial config assembly for xerox bug 2304221        */
5764             IF nvl(l_tld_rec.changed_instance_id, fnd_api.g_miss_num) <>
5765                fnd_api.g_miss_num
5766 		    AND p_order_line_rec.serial_code = 1 -- Added for Bug 3008953
5767             THEN
5768               debug('re-shipping of a returned configuration.');
5769               -- a returned instance reference specified.
5770 
5771               is_instance_rma := TRUE;
5772 
5773               decrement_source_instance(
5774                 p_instance_id    => l_tld_rec.instance_id,
5775                 p_quantity       => 1,
5776                 p_trx_rec        => l_trx_rec,
5777                 x_return_status  => l_return_status);
5778 
5779               IF l_return_status <> fnd_api.g_ret_sts_success THEN
5780                 RAISE fnd_api.g_exc_error;
5781               END IF;
5782 
5783               debug('Rebuilding the TD tables for the returned config instance.');
5784 
5785               l_curr_instance_qty := 1 ;
5786               l_instance_id       := l_tld_rec.changed_instance_id;
5787 
5788               SELECT object_version_number
5789               INTO   l_inst_obj_ver_num
5790               FROM   csi_item_instances
5791               WHERE  instance_id = l_tld_rec.changed_instance_id;
5792 
5793               csi_utl_pkg.rebuild_tbls(
5794                 p_new_instance_id        => l_instance_id,
5795                 x_upd_party_tbl          => l_upd_party_tbl,
5796                 x_upd_party_acct_tbl     => l_upd_party_acct_tbl,
5797                 x_upd_org_units_tbl      => l_upd_org_units_tbl,
5798                 x_upd_ext_attrib_val_tbl => l_upd_ext_attrib_val_tbl,
5799                 x_cre_org_units_tbl      => l_cre_org_units_tbl,
5800                 x_cre_ext_attrib_val_tbl => l_cre_ext_attrib_val_tbl,
5801                 x_txn_ii_rltns_tbl       => p_txn_ii_rltns_tbl,
5802                 x_txn_line_detail_rec    => l_tld_rec,
5803                 x_return_status          => x_return_status);
5804 
5805               IF l_return_status <> fnd_api.g_ret_sts_success THEN
5806                 raise fnd_api.g_exc_error;
5807               END IF;
5808 
5809             END IF;
5810 
5811             IF (p_order_line_rec.serial_code in (1,6))
5812                 AND
5813                (NOT(is_instance_rma))
5814                 -- Commented as part of fix for Bug 2985193
5815                 -- AND
5816                 -- (l_curr_instance_qty > l_tld_rec.quantity )
5817             -- Begin code fix for Bug 2985193
5818             THEN
5819               IF (l_curr_instance_qty > l_tld_rec.quantity )
5820               THEN
5821                 /* Split the instance if the inst_qty >  shipped_qty                           */
5822                 /* update the existing instance with the qty equal to (inst_qty - shipped_qty) */
5823                 /* and create a new instance with qty equal to txn qty                         */
5824                 l_quantity1 := (l_curr_instance_qty - l_tld_rec.quantity );
5825                 l_quantity2 :=  l_tld_rec.quantity ;
5826               ELSIF ( l_curr_instance_qty - l_tld_rec.quantity ) < 0
5827               THEN
5828                 IF p_order_line_rec.negative_balances_code = 1
5829                 THEN
5830                   IF  l_curr_instance_qty = 0
5831                     AND p_order_line_rec.reservable_type = 2
5832                   THEN
5833 
5834                     /*
5835                     debug('Non reservable Item and is expired so unexpiring first');
5836 
5837                     -- Calling update_item_instance to unexpire the source instance
5838                     l_un_exp_instance_rec.instance_id           := l_tld_rec.instance_id;
5839                     l_un_exp_instance_rec.active_end_date       := null;
5840                     l_un_exp_instance_rec.object_version_number := l_inst_obj_ver_num;
5841 
5842                     csi_t_gen_utility_pvt.dump_api_info(
5843                       p_pkg_name => 'csi_item_instance_pub',
5844                       p_api_name => 'update_item_instance');
5845 
5846                     csi_item_instance_pub.update_item_instance(
5847                       p_api_version           => 1.0,
5848                       p_commit                => fnd_api.g_false,
5849                       p_init_msg_list         => fnd_api.g_true,
5850                       p_validation_level      => fnd_api.g_valid_level_full,
5851                       p_instance_rec          => l_un_exp_instance_rec,
5852                       p_ext_attrib_values_tbl => l_un_exp_ext_attrib_val_tbl,
5853                       p_party_tbl             => l_un_exp_party_tbl,
5854                       p_account_tbl           => l_un_exp_party_acct_tbl,
5855                       p_pricing_attrib_tbl    => l_un_exp_pricing_attribs_tbl,
5856                       p_org_assignments_tbl   => l_un_exp_org_units_tbl,
5857                       p_txn_rec               => l_trx_rec,
5858                       p_asset_assignment_tbl  => l_un_exp_inst_asset_tbl,
5859                       x_instance_id_lst       => l_chg_inst_id_lst,
5860                       x_return_status         => l_return_status,
5861                       x_msg_count             => l_msg_count,
5862                       x_msg_data              => l_msg_data );
5863 
5864                     -- For Bug 4057183
5865                     -- IF l_return_status <> fnd_api.g_ret_sts_success THEN
5866                     IF l_return_status not in (fnd_api.g_ret_sts_success,'W') THEN
5867                        RAISE fnd_api.g_exc_error;
5868                     END IF;
5869                     debug('After Non reservable Item instance is unexpired');
5870                     */
5871                     -- instead of an unexpiry I am passing the end date of the source instance
5872                     -- for the split as null and passing created as the instance status.
5873 
5874                     l_split_src_inst_rec.active_end_date       := null;
5875                     l_split_src_inst_rec.instance_status_id    := 510;
5876                   END IF;
5877                   l_quantity1 := (l_curr_instance_qty - l_tld_rec.quantity );
5878                   l_quantity2 :=  l_tld_rec.quantity ;
5879                 ELSE
5880                   fnd_message.set_name('CSI', 'CSI_ORG_NO_NEG_QTY');
5881                   fnd_message.set_token('ORG_ID',l_tld_rec.inv_organization_id);
5882                   fnd_msg_pub.add;
5883                   Raise fnd_api.g_exc_error;
5884                 END IF;
5885               ELSE
5886                 l_quantity1 := (l_curr_instance_qty - l_tld_rec.quantity );
5887                 l_quantity2 :=  l_tld_rec.quantity ;
5888               END IF;
5889             -- END IF;  -- Commented as part of Bug 3033092
5890             -- End code fix for Bug 2985193
5891 
5892               l_split_src_inst_rec.instance_id           := l_tld_rec.instance_id;
5893               l_split_src_inst_rec.vld_organization_id   := l_tld_rec.inv_organization_id;
5894               l_split_src_inst_rec.location_type_code    := l_tld_rec.location_type_code;
5895               l_split_src_inst_rec.location_id           := l_tld_rec.location_id;
5896               -- Added for partner ordering
5897               l_split_src_inst_rec.install_location_type_code    := l_tld_rec.install_location_type_code;
5898               l_split_src_inst_rec.install_location_id           := l_tld_rec.install_location_id;
5899               -- Added for partner ordering
5900               l_split_src_inst_rec.inv_organization_id   := null;
5901               l_split_src_inst_rec.inv_subinventory_name := null;
5902               l_split_src_inst_rec.inv_locator_id        := null;
5903               l_split_src_inst_rec.pa_project_id         := null;
5904               l_split_src_inst_rec.pa_project_task_id    := null;
5905               l_split_src_inst_rec.wip_job_id            := null;
5906               l_split_src_inst_rec.po_order_line_id      := null;
5907               l_split_src_inst_rec.version_label         := nvl(l_tld_rec.version_label,fnd_api.g_miss_char);--bug 5112946
5908               l_split_src_trx_rec := p_trx_rec;
5909 
5910               csi_t_gen_utility_pvt.dump_api_info(
5911                 p_pkg_name => 'csi_item_instance_pvt',
5912                 p_api_name => 'split_item_instance');
5913 
5914               /* split the inventory staging instance when the instance qty > ship qty */
5915               /* the owner party is still internal */
5916               csi_item_instance_pvt.split_item_instance (
5917                 p_api_version            => 1.0,
5918                 p_commit                 => fnd_api.g_false,
5919                 p_init_msg_list          => fnd_api.g_true,
5920                 p_validation_level       => fnd_api.g_valid_level_full,
5921                 p_source_instance_rec    => l_split_src_inst_rec,
5922                 p_quantity1              => l_quantity1,
5923                 p_quantity2              => l_quantity2,
5924                 p_copy_ext_attribs       => fnd_api.g_true,
5925                 p_copy_org_assignments   => fnd_api.g_true,
5926                 p_copy_parties           => fnd_api.g_true,
5927                 p_copy_accounts          => fnd_api.g_true,
5928                 p_copy_asset_assignments => fnd_api.g_true,
5929                 p_copy_pricing_attribs   => fnd_api.g_true,
5930                 p_txn_rec                => l_split_src_trx_rec,
5931                 x_new_instance_rec       => l_split_new_inst_rec,
5932                 x_return_status          => x_return_status,
5933                 x_msg_count              => x_msg_count,
5934                 x_msg_data               => x_msg_data);
5935 
5936               IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
5937                 g_api_name := 'csi_item_instance_pvt.split_item_instance';
5938                 raise fnd_api.g_exc_error;
5939               END IF;
5940 
5941               l_instance_id      := l_split_new_inst_rec.instance_id ;
5942               l_inst_obj_ver_num := l_split_new_inst_rec.object_version_number;
5943 
5944               debug('  Old Instance_ID :' ||l_tld_rec.instance_id );
5945               debug('  New Instance_ID :' ||l_split_new_inst_rec.instance_id );
5946 
5947               debug('Rebuilding the TD tables for the newly created instance.');
5948 
5949               csi_utl_pkg.rebuild_tbls(
5950                 p_new_instance_id         => l_instance_id,
5951                 x_upd_party_tbl           => l_upd_party_tbl,
5952                 x_upd_party_acct_tbl      => l_upd_party_acct_tbl,
5953                 x_upd_org_units_tbl       => l_upd_org_units_tbl,
5954                 x_upd_ext_attrib_val_tbl  => l_upd_ext_attrib_val_tbl,
5955                 x_cre_org_units_tbl       => l_cre_org_units_tbl,
5956                 x_cre_ext_attrib_val_tbl  => l_cre_ext_attrib_val_tbl,
5957                 x_txn_ii_rltns_tbl        => p_txn_ii_rltns_tbl,
5958                 x_txn_line_detail_rec     => l_tld_rec,
5959                 x_return_status           => x_return_status  );
5960 
5961               IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
5962                 raise fnd_api.g_exc_error;
5963               END IF;
5964 
5965             END IF; -- end of split_item_instances
5966              -- Uncommented the end if as part of fix for Bug 3033092
5967             -- Commented the END IF and moved it to before even building the split record
5968             -- as part of fix for Bug 2985193
5969 
5970             IF NVL(x_trx_sub_type_rec.src_status_id,fnd_api.g_miss_num) =  fnd_api.g_miss_num THEN
5971 
5972               l_inst_status_id := l_dflt_inst_status_id;
5973 
5974               l_upd_instance_rec.instance_status_id := l_inst_status_id;
5975             ELSE
5976               l_upd_instance_rec.instance_status_id := x_trx_sub_type_rec.src_status_id;
5977             END IF;
5978 
5979             l_trx_rec.txn_sub_type_id := l_tld_rec.sub_type_id;
5980 
5981             /* Assign the values for update_instance_rec */
5982 
5983             l_upd_instance_rec.instance_id              := l_instance_id ;
5984             l_upd_instance_rec.vld_organization_id      := l_tld_rec.inv_organization_id;
5985 
5986             -- Partner Ordering Code change
5987             -- l_upd_instance_rec.location_type_code       := l_tld_rec.location_type_code;
5988             -- l_upd_instance_rec.location_id              := l_tld_rec.location_id;
5989             -- Added the nvl to l_ltd_rec optional columns for Bug 3818547
5990             l_upd_instance_rec.install_location_type_code  := nvl(l_tld_rec.install_location_type_code,fnd_api.g_miss_char);
5991             IF p_order_line_rec.ib_install_loc is not null
5992               AND
5993                p_order_line_rec.ib_install_loc <> fnd_api.g_miss_char
5994             THEN
5995               BEGIN
5996                 SELECT HCAS.party_site_id
5997                 INTO   l_upd_party_site_id
5998                 FROM   hz_cust_site_uses_all  HCSU,
5999                        hz_cust_acct_sites_all HCAS
6000                 WHERE  HCSU.site_use_id       = p_order_line_rec.ib_install_loc_id
6001                 AND    HCAS.cust_acct_site_id = HCSU.cust_acct_site_id;
6002 
6003               EXCEPTION
6004                 WHEN others THEN
6005                   NULL;
6006               END;
6007 
6008               IF  l_upd_party_site_id <> l_tld_rec.install_location_id
6009               THEN
6010                 l_upd_instance_rec.install_location_id  := l_upd_party_site_id;
6011               -- Added else as part of fix for Bug 3419098
6012               ELSE
6013                 l_upd_instance_rec.install_location_id  := nvl(l_tld_rec.install_location_id,fnd_api.g_miss_num);
6014               END IF;
6015             ELSE
6016               l_upd_instance_rec.install_location_id    := nvl(l_tld_rec.install_location_id,fnd_api.g_miss_num);
6017             END IF;
6018             l_upd_instance_rec.location_type_code       := nvl(l_tld_rec.location_type_code,l_tld_rec.install_location_type_code);
6019             l_upd_instance_rec.location_id              := nvl(l_tld_rec.location_id,l_upd_instance_rec.install_location_id);
6020             -- Partner Ordering code change
6021 
6022             l_upd_instance_rec.external_reference       := nvl(l_tld_rec.external_reference,fnd_api.g_miss_char);
6023             l_upd_instance_rec.system_id                := nvl(l_tld_rec.csi_system_id,fnd_api.g_miss_num);
6024             -- l_upd_instance_rec.accounting_class_code    := 'CUST_PROD';
6025 
6026             IF nvl(l_tld_rec.installation_date,fnd_api.g_miss_date) = fnd_api.g_miss_date THEN
6027                IF l_default_install_date = 'Y' THEN
6028                   IF p_source =  'SHIPMENT' THEN
6029                      l_upd_instance_rec.install_date := p_order_line_rec.actual_shipment_date;
6030                   ELSIF p_source = 'FULFILLMENT' THEN
6031                      l_upd_instance_rec.install_date := nvl(p_order_line_rec.fulfillment_date,sysdate);
6032                   END IF;
6033                END IF;
6034             ELSE
6035                /* Begin code change for 3254347 Install_Date and Start_Date changes */
6036               IF p_source =  'SHIPMENT' THEN
6037                  l_upd_instance_rec.install_date := GREATEST(l_tld_rec.installation_date,p_order_line_rec.actual_shipment_date);
6038               ELSIF  p_source = 'FULFILLMENT' THEN
6039 							  l_upd_instance_rec.install_date       := l_tld_rec.installation_date; --Added for bug 7668298
6040  	              /* --Commented  for bug 7668298
6041 					      IF p_order_line_rec.fulfillment_date is NOT NULL
6042 					        AND
6043 					          p_order_line_rec.fulfillment_date <> fnd_api.g_miss_date
6044 					      THEN
6045 					        l_upd_instance_rec.install_date       := GREATEST(l_tld_rec.installation_date,p_order_line_rec.fulfillment_date);
6046 					      ELSE
6047 					          l_upd_instance_rec.install_date      := GREATEST(l_tld_rec.installation_date,sysdate);
6048 					      END IF;
6049       					*/ --Commented  for bug 7668298
6050               END IF;
6051                --l_upd_instance_rec.install_date             := l_tld_rec.installation_date;
6052                /* End code change for 3254347 Install_Date and Start_Date changes */
6053             END IF;
6054 
6055             l_upd_instance_rec.instance_type_code       := nvl(l_tld_rec.instance_type_code,fnd_api.g_miss_char);
6056             l_upd_instance_rec.instance_condition_id    := nvl(l_tld_rec.item_condition_id,fnd_api.g_miss_num);
6057             l_upd_instance_rec.quantity                 := l_tld_rec.quantity;
6058             l_upd_instance_rec.unit_of_measure          := l_tld_rec.unit_of_measure;
6059             l_upd_instance_rec.sellable_flag            := nvl(l_tld_rec.sellable_flag,fnd_api.g_miss_char);
6060             l_upd_instance_rec.last_oe_order_line_id    := p_order_line_rec.order_line_id ;
6061             l_upd_instance_rec.last_txn_line_detail_id  := l_tld_rec.txn_line_detail_id ;
6062             l_upd_instance_rec.return_by_date           := nvl(l_tld_rec.return_by_date,fnd_api.g_miss_date);
6063             --l_upd_instance_rec.active_end_date          := nvl(l_tld_rec.active_end_date,fnd_api.g_miss_date);--Bug3964060
6064             l_upd_instance_rec.active_end_date          := l_tld_rec.active_end_date;
6065 	     --Added IF condition for bug 5112946--
6066             IF (p_order_line_rec.serial_code in (2,5)) OR
6067                  (p_order_line_rec.serial_code =6 AND is_instance_rma) THEN
6068               l_upd_instance_rec.version_label            := nvl(l_tld_rec.version_label,fnd_api.g_miss_char);
6069             END IF;
6070             l_upd_instance_rec.lot_number               := nvl(l_tld_rec.lot_number,fnd_api.g_miss_char);
6071             l_upd_instance_rec.instance_usage_code      := 'OUT_OF_ENTERPRISE';
6072             l_upd_instance_rec.last_oe_agreement_id     := p_order_line_rec.agreement_id;
6073             l_upd_instance_rec.actual_return_date       := null;
6074 
6075             l_upd_instance_rec.object_version_number    := l_inst_obj_ver_num;
6076             l_upd_instance_rec.source_code              := p_order_line_rec.source_code; -- Added for Siebel Genesis Project
6077 
6078             -- Added this as part of fix for Bug 2972082
6079             l_upd_instance_rec.cascade_ownership_flag   := l_tld_rec.cascade_owner_flag;
6080             -- End fix for Bug 2972082
6081 
6082             IF p_order_line_rec.serial_code = 6 THEN
6083               l_upd_instance_rec.serial_number          := l_tld_rec.serial_number ;
6084               l_upd_instance_rec.mfg_serial_number_flag := 'Y';
6085             END IF;
6086 
6087             /* Null the inventory location attributes  */
6088             l_upd_instance_rec.inv_organization_id      := NULL;
6089             l_upd_instance_rec.inv_subinventory_name    := NULL;
6090             l_upd_instance_rec.inv_locator_id           := NULL;
6091             l_upd_instance_rec.pa_project_id            := NULL;
6092             l_upd_instance_rec.pa_project_task_id       := NULL;
6093             l_upd_instance_rec.wip_job_id               := NULL;
6094             l_upd_instance_rec.po_order_line_id         := NULL;
6095 
6096             l_expire_flag := FALSE;
6097 
6098             IF nvl(l_upd_instance_rec.active_end_date,fnd_api.g_miss_date) <> fnd_api.g_miss_date
6099 	     AND (l_upd_instance_rec.active_end_date) <= SYSDATE
6100             THEN
6101 
6102               l_upd_ext_attrib_val_tbl.DELETE;
6103               l_upd_party_tbl.DELETE;
6104               l_upd_party_acct_tbl.DELETE;
6105               l_upd_pricing_attribs_tbl.DELETE;
6106               l_upd_org_units_tbl.DELETE;
6107               l_upd_inst_asset_tbl.DELETE;
6108 
6109               l_upd_instance_rec.active_end_date := fnd_api.g_miss_date;
6110 
6111               l_expire_flag := TRUE;
6112 
6113             END IF;
6114 
6115 
6116             /* check if this is a replacement instnace. If this is replacement
6117                then pass the call_contracts flag as false . bug 2298453*/
6118             csi_utl_pkg.call_contracts_chk(
6119               p_txn_line_detail_id => l_tld_rec.txn_line_detail_id,
6120               p_txn_ii_rltns_tbl   => p_txn_ii_rltns_tbl,
6121               x_call_contracts     => l_call_contracts,
6122               x_return_status      => l_return_status);
6123 
6124             -- set the call contracts
6125             l_upd_instance_rec.call_contracts := l_call_contracts;
6126 
6127             -- set the flag in party record also for the owner
6128             IF l_upd_party_tbl.count > 0 THEN
6129               FOR l_upa_ind IN l_upd_party_tbl.FIRST .. l_upd_party_tbl.LAST
6130               LOOP
6131                 IF l_upd_party_tbl(l_upa_ind).relationship_type_code = 'OWNER' THEN
6132                   l_upd_party_tbl(l_upa_ind).call_contracts := l_call_contracts;
6133                 END IF;
6134               END LOOP;
6135             END IF;
6136 
6137             -- set the flag in party account record also for the owner
6138             IF l_upd_party_acct_tbl.count > 0 THEN
6139               FOR l_upa_ind IN l_upd_party_acct_tbl.FIRST .. l_upd_party_acct_tbl.LAST
6140               LOOP
6141                 IF l_upd_party_acct_tbl(l_upa_ind).relationship_type_code = 'OWNER' THEN
6142                   l_upd_party_acct_tbl(l_upa_ind).call_contracts := l_call_contracts;
6143                 END IF;
6144               END LOOP;
6145             END IF;
6146 
6147             /* Code fix as part of ER 2581101 */
6148 
6149             IF x_trx_sub_type_rec.src_change_owner = 'Y' THEN
6150 
6151               IF l_upd_party_acct_tbl.COUNT > 0 THEN
6152 
6153                 l_chg_instance_rec.instance_id     := l_upd_instance_rec.instance_id;
6154                 l_chg_instance_rec.active_end_date := null;
6155 
6156                 SELECT owner_party_id,
6157                        object_version_number
6158                 INTO   l_owner_party_id,
6159                        l_chg_instance_rec.object_version_number
6160                 FROM   CSI_ITEM_INSTANCES
6161                 WHERE  instance_id = l_upd_party_tbl(1).instance_id;
6162 
6163                 IF l_owner_party_id <> p_order_line_rec.internal_party_id
6164                    AND
6165                    l_owner_party_id <> l_upd_party_tbl(1).party_id
6166                 THEN
6167 
6168                   l_ownership_flag:=
6169                     NVL(csi_datastructures_pub.g_install_param_rec.ownership_override_at_txn,'N');
6170 
6171                   IF l_ownership_flag = 'N' Then
6172                     fnd_message.set_name('CSI','CSI_SHIP_OWNER_MISMATCH');
6173                     fnd_message.set_token('OLD_PARTY_ID',l_owner_party_id);
6174                     fnd_message.set_token('NEW_PARTY_ID',l_upd_party_tbl(1).party_id);
6175                     fnd_message.set_token('INSTANCE_ID',l_upd_party_tbl(1).instance_id);
6176                     fnd_msg_pub.add;
6177                     raise fnd_api.g_exc_error;
6178                   Else
6179 
6180                     /* Changing the Owner to Internal Before shipping to New Customer */
6181                     l_chg_party_tbl(1).instance_id          := l_upd_party_tbl(1).instance_id;
6182                     l_chg_party_tbl(1).instance_party_id    := l_upd_party_tbl(1).instance_party_id;
6183                     l_chg_party_tbl(1).object_version_number:= l_upd_party_tbl(1).object_version_number;
6184                     l_chg_party_tbl(1).party_source_table   := 'HZ_PARTIES';
6185                     l_chg_party_tbl(1).relationship_type_code := 'OWNER';
6186                     l_chg_party_tbl(1).contact_flag           := 'N';
6187                     l_chg_party_tbl(1).party_id               := p_order_line_rec.internal_party_id;
6188 
6189                     debug('change owner to internal to terminate contracts.');
6190 
6191                     csi_t_gen_utility_pvt.dump_api_info(
6192                       p_pkg_name => 'csi_item_instance_pub',
6193                       p_api_name => 'update_item_instance');
6194 
6195                     -- owner change to internal
6196                     csi_item_instance_pub.update_item_instance(
6197                       p_api_version           => 1.0,
6198                       p_commit                => fnd_api.g_false,
6199                       p_init_msg_list         => fnd_api.g_true,
6200                       p_validation_level      => fnd_api.g_valid_level_full,
6201                       p_instance_rec          => l_chg_instance_rec,
6202                       p_ext_attrib_values_tbl => l_chg_ext_attrib_val_tbl,
6203                       p_party_tbl             => l_chg_party_tbl,
6204                       p_account_tbl           => l_chg_party_acct_tbl,
6205                       p_pricing_attrib_tbl    => l_chg_pricing_attribs_tbl,
6206                       p_org_assignments_tbl   => l_chg_org_units_tbl,
6207                       p_txn_rec               => l_trx_rec,
6208                       p_asset_assignment_tbl  => l_chg_inst_asset_tbl,
6209                       x_instance_id_lst       => l_chg_inst_id_lst,
6210                       x_return_status         => l_return_status,
6211                       x_msg_count             => l_msg_count,
6212                       x_msg_data              => l_msg_data );
6213 
6214                     -- For Bug 4057183
6215                     -- IF l_return_status <> fnd_api.g_ret_sts_success THEN
6216                     IF l_return_status not in (fnd_api.g_ret_sts_success,'W') THEN
6217                       RAISE fnd_api.g_exc_error;
6218                     END IF;
6219 
6220                     l_upd_party_tbl(1).instance_party_id     := l_chg_party_tbl(1).instance_party_id;
6221                     /* Fixed for Bug 2728984 */
6222                     BEGIN
6223                       SELECT object_version_number
6224                       INTO   l_upd_instance_rec.object_version_number
6225                       FROM   CSI_ITEM_INSTANCES
6226                       WHERE  instance_id = l_upd_instance_rec.instance_id;
6227                     END;
6228 
6229                     BEGIN
6230                       SELECT object_version_number
6231                       INTO   l_upd_party_tbl(1).object_version_number
6232                       FROM   CSI_I_PARTIES
6233                       WHERE  instance_party_id = l_upd_party_tbl(1).instance_party_id;
6234                     END;
6235                     /* End Of fix for Bug  2728984 */
6236                   END IF;
6237                 END IF;
6238               END IF;
6239               /* Fixed for Bug 2714715 */
6240 
6241               l_owner_pty_rec  := l_upd_party_tbl(1);
6242               l_owner_acct_rec := l_upd_party_acct_tbl(1);
6243 
6244             ELSIF NVL(x_trx_sub_type_rec.src_change_owner,'N') = 'N' THEN
6245               l_upd_party_tbl.delete;
6246               l_upd_party_acct_tbl.delete;
6247               l_owner_pty_rec  := null;
6248               l_owner_acct_rec := null;
6249 
6250               BEGIN
6251                 SELECT 'Y' INTO l_cia_found
6252                 FROM   sys.dual
6253                 WHERE  exists (
6254                   SELECT '1' FROM csi_i_assets
6255                   WHERE  instance_id = l_upd_instance_rec.instance_id
6256                   AND    sysdate between nvl(active_start_date, sysdate-1) and nvl(active_end_date, sysdate+1));
6257                 l_upd_instance_rec.operational_status_code := 'IN_SERVICE';
6258               EXCEPTION
6259                 WHEN no_data_found THEN
6260                   null;
6261               END;
6262 
6263             END IF;
6264             /* End Of Code fix as part of ER 2581101 */
6265 
6266             csi_utl_pkg.get_parties_and_accounts(
6267               p_instance_id      => l_upd_instance_rec.instance_id,
6268               p_tld_rec          => l_tld_rec,
6269               p_t_pty_tbl        => l_tld_party_tbl,
6270               p_t_pty_acct_tbl   => l_tld_account_tbl,
6271               p_owner_pty_rec    => l_owner_pty_rec,
6272               p_owner_acct_rec   => l_owner_acct_rec,
6273               p_order_line_rec   => p_order_line_rec,
6274               x_i_pty_tbl        => l_upd_party_tbl,
6275               x_i_pty_acct_tbl   => l_upd_party_acct_tbl,
6276               x_return_status    => l_return_status);
6277 
6278             IF l_return_status <> fnd_api.g_ret_sts_success THEN
6279               RAISE fnd_api.g_exc_error;
6280             END IF;
6281 
6282             -- srramakr TSO with Equipment
6283             -- Need to pass the config keys to the Update_Item_Instance API.
6284             l_upd_instance_rec.config_inst_hdr_id  := l_tld_rec.config_inst_hdr_id;
6285             l_upd_instance_rec.config_inst_rev_num := l_tld_rec.config_inst_rev_num;
6286             l_upd_instance_rec.config_inst_item_id := l_tld_rec.config_inst_item_id;
6287             --
6288 
6289             l_upd_instance_rec.sales_currency_code := l_order_line_rec.currency_code;
6290 
6291             IF l_order_line_rec.primary_uom <> l_order_line_rec.order_quantity_uom THEN
6292 
6293               l_item_control_rec.inventory_item_id := l_order_line_rec.inv_item_id;
6294               l_item_control_rec.organization_id   := l_order_line_rec.inv_org_id;
6295               l_item_control_rec.primary_uom_code  := l_order_line_rec.primary_uom;
6296 
6297               csi_utl_pkg.get_unit_price_in_primary_uom(
6298                 p_unit_price                => l_order_line_rec.unit_price,
6299                 p_unit_price_uom            => l_order_line_rec.order_quantity_uom,
6300                 px_item_control_rec         => l_item_control_rec,
6301                 x_unit_price_in_primary_uom => l_upd_instance_rec.sales_unit_price,
6302                 x_return_status             => l_return_status);
6303 
6304               IF l_return_status <> fnd_api.g_ret_sts_success THEN
6305                 RAISE fnd_api.g_exc_error;
6306               END IF;
6307 
6308             ELSE
6309               l_upd_instance_rec.sales_unit_price := l_order_line_rec.unit_price;
6310             END IF;
6311 
6312             debug('  upd_party_tbl.count           :'||l_upd_party_tbl.count);
6313             debug('  upd_party_acct_tbl.count      :'||l_upd_party_acct_tbl.count);
6314             debug('  upd_org_units_tbl.count       :'||l_upd_org_units_tbl.count);
6315             debug('  upd_inst_asset_tbl.count      :'||l_upd_inst_asset_tbl.count);
6316             debug('  upd_ext_attrib_val_tbl.count  :'||l_upd_ext_attrib_val_tbl.count);
6317             debug('  upd_pricing_attribs_tbl.count :'||l_upd_pricing_attribs_tbl.count);
6318 
6319             IF l_debug_level > 1 THEN
6320               csi_t_gen_utility_pvt.dump_csi_instance_rec(l_upd_instance_rec);
6321             END IF;
6322 
6323             csi_t_gen_utility_pvt.dump_api_info(
6324               p_api_name => 'update_item_instance',
6325               p_pkg_name => 'csi_item_instance_pub');
6326 
6327             debug('Converting the instance into a customer product....!');
6328 
6329             /* this is the call that makes the instance as customer product */
6330             csi_item_instance_pub.update_item_instance(
6331               p_api_version           => 1.0,
6332               p_commit                => fnd_api.g_false,
6333               p_init_msg_list         => fnd_api.g_true,
6334               p_validation_level      => fnd_api.g_valid_level_full,
6335               p_instance_rec          => l_upd_instance_rec,
6336               p_ext_attrib_values_tbl => l_upd_ext_attrib_val_tbl,
6337               p_party_tbl             => l_upd_party_tbl,
6338               p_account_tbl           => l_upd_party_acct_tbl,
6339               p_pricing_attrib_tbl    => l_upd_pricing_attribs_tbl,
6340               p_org_assignments_tbl   => l_upd_org_units_tbl,
6341               p_txn_rec               => l_trx_rec,
6342               p_asset_assignment_tbl  => l_upd_inst_asset_tbl,
6343               x_instance_id_lst       => l_inst_id_lst,
6344               x_return_status         => l_return_status,
6345               x_msg_count             => x_msg_count,
6346               x_msg_data              => x_msg_data);
6347 
6348             -- For Bug 4057183
6349             -- IF l_return_status <> fnd_api.g_ret_sts_success THEN
6350             IF l_return_status not in (fnd_api.g_ret_sts_success,'W') THEN
6351               g_api_name := 'csi_item_instance_pub.update_item_instance';
6352               raise fnd_api.g_exc_error;
6353             END IF;
6354 
6355             IF l_expire_flag THEN
6356 
6357               l_exp_instance_rec.instance_id           := l_upd_instance_rec.instance_id;
6358               l_exp_instance_rec.object_version_number := l_inst_obj_ver_num + 1;
6359 
6360               l_trx_rec.transaction_id                 := fnd_api.g_miss_num;
6361 
6362               csi_t_gen_utility_pvt.dump_api_info(
6363                 p_api_name => 'expire_item_instance',
6364                 p_pkg_name => 'csi_item_instance_pub');
6365 
6366               csi_item_instance_pub.expire_item_instance(
6367                 p_api_version      => 1.0,
6368                 p_commit           => fnd_api.g_false,
6369                 p_init_msg_list    => fnd_api.g_true,
6370                 p_validation_level => fnd_api.g_valid_level_full,
6371                 p_instance_rec     => l_exp_instance_rec,
6372                 p_expire_children  => fnd_api.g_true,
6373                 p_txn_rec          => l_trx_rec,
6374                 x_instance_id_lst  => l_exp_instance_id_lst,
6375                 x_return_status    => l_return_status,
6376                 x_msg_count        => x_msg_count,
6377                 x_msg_data         => x_msg_data);
6378 
6379               IF NOT(l_return_status = fnd_api.g_ret_sts_success) THEN
6380                 g_api_name := 'csi_item_instance_pub.expire_item_instance';
6381                 raise fnd_api.g_exc_error;
6382               END IF;
6383 
6384             END IF;
6385 
6386             l_instance_id := l_upd_instance_rec.instance_id ;
6387 
6388             l_tld_rec.changed_instance_id    := l_upd_instance_rec.instance_id;
6389             l_tld_rec.inv_mtl_transaction_id := p_order_line_rec.inv_mtl_transaction_id;
6390 
6391             debug('Customer Product ID :'||l_upd_instance_rec.instance_id);
6392 
6393             l_cp_ind := l_cp_ind + 1;
6394             l_cps_tbl(l_cp_ind).instance_id        := l_upd_instance_rec.instance_id;
6395             l_cps_tbl(l_cp_ind).quantity           := l_upd_instance_rec.quantity;
6396             l_cps_tbl(l_cp_ind).txn_line_detail_id := l_upd_instance_rec.last_txn_line_detail_id;
6397             l_cps_tbl(l_cp_ind).line_id            := l_upd_instance_rec.last_oe_order_line_id;
6398             l_cps_tbl(l_cp_ind).transaction_id     := l_trx_rec.transaction_id;
6399             l_cps_tbl(l_cp_ind).serial_number      := l_tld_rec.serial_number;
6400             l_cps_tbl(l_cp_ind).lot_number         := l_upd_instance_rec.lot_number;
6401 
6402             IF NOT (l_expire_flag) THEN
6403 
6404               /* create pricing attributes */
6405 
6406               IF p_pricing_attribs_tbl.count > 0 THEN
6407                 --Changes for bug 3901064 Start
6408                 l_new_pricing_attribs_tbl.delete;
6409                 l_old_pricing_attribs_tbl.delete;
6410                 l_old_index := 0;
6411                 l_new_index := 0;
6412                 --Changes for bug 3901064 End
6413                 FOR l_index in p_pricing_attribs_tbl.first..p_pricing_attribs_tbl.last LOOP
6414                   p_pricing_attribs_tbl(l_index).instance_id := l_upd_instance_rec.instance_id ;
6415                 END LOOP;
6416               END IF;
6417 
6418               -- BEGIN MRK CODE FOR UPDATE PRICING ATTRIBUTE
6419 
6420               IF p_pricing_attribs_tbl.count > 0 THEN
6421                 For i in p_pricing_attribs_tbl.first..p_pricing_attribs_tbl.last LOOP
6422                   BEGIN
6423                     SELECT pricing_attribute_id,
6424                            object_version_number
6425                     INTO   p_pricing_attribs_tbl(i).pricing_attribute_id,
6426                            p_pricing_attribs_tbl(i).object_version_number
6427                     FROM   csi_i_pricing_attribs
6428                     WHERE  instance_id = p_pricing_attribs_tbl(i).instance_id
6429                     AND    pricing_context = p_pricing_attribs_tbl(i).pricing_context;
6430 
6431                   EXCEPTION
6432                     WHEN NO_DATA_FOUND THEN
6433                          p_pricing_attribs_tbl(i).pricing_attribute_id := fnd_api.g_miss_num;  -- Modified for bug 13072884
6434                          p_pricing_attribs_tbl(i).object_version_number := fnd_api.g_miss_num; -- Modified for bug 13072884
6435                   END;
6436                   debug('p_pricing_attribs_tbl(i).pricing_attribute_id : '||p_pricing_attribs_tbl(i).pricing_attribute_id); --Added for bug 13072884
6437                   IF (  p_pricing_attribs_tbl(i).pricing_attribute_id is not null
6438                       AND
6439                         p_pricing_attribs_tbl(i).pricing_attribute_id <> fnd_api.g_miss_num
6440                      )
6441                   THEN
6442                     l_old_index                            := l_old_index + 1;
6443                     l_old_pricing_attribs_tbl(l_old_index) := p_pricing_attribs_tbl(i);
6444                     l_old_pricing_attribs_tbl(l_old_index).active_end_date := null;
6445                   ELSE
6446                     l_new_index                            := l_new_index + 1;
6447                     l_new_pricing_attribs_tbl(l_new_index) := p_pricing_attribs_tbl(i);
6448                   END IF;
6449                 END LOOP;
6450               END IF;
6451 
6452               IF l_old_pricing_attribs_tbl.count > 0 THEN
6453                 debug('Update Pricing_Attributes Count '||l_old_pricing_attribs_tbl.count);
6454 
6455                 csi_t_gen_utility_pvt.dump_api_info(
6456                   p_api_name => 'update_pricing_attribs',
6457                   p_pkg_name => 'csi_pricing_attribs_pub');
6458 
6459                 csi_pricing_attribs_pub.update_pricing_attribs(
6460                   p_api_version         => 1.0,
6461                   p_commit              => fnd_api.g_false,
6462                   p_init_msg_list       => fnd_api.g_true,
6463                   p_validation_level    => fnd_api.g_valid_level_full,
6464                   p_pricing_attribs_tbl => l_old_pricing_attribs_tbl,
6465                   p_txn_rec             => l_trx_rec,
6466                   x_return_status       => x_return_status,
6467                   x_msg_count           => x_msg_count,
6468                   x_msg_data            => x_msg_data);
6469 
6470                 IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
6471                    g_api_name := 'csi_pricing_attribs_pub.update_pricing_attribs';
6472                    raise fnd_api.g_exc_error;
6473                 END IF;
6474                    debug('Pricing Attributes updated successfully.');
6475               END IF;
6476 
6477               -- IF p_pricing_attribs_tbl.count > 0 THEN
6478               -- END MRK CODE FOR UPDATE PRICING ATTRIBUTE
6479 
6480              IF l_new_pricing_attribs_tbl.count > 0 THEN
6481                 debug('Creating Pricing_Attributes Count '||l_new_pricing_attribs_tbl.count);
6482 
6483                 csi_t_gen_utility_pvt.dump_api_info(
6484                   p_api_name => 'create_pricing_attribs',
6485                   p_pkg_name => 'csi_pricing_attribs_pub');
6486 
6487                 csi_pricing_attribs_pub.create_pricing_attribs(
6488                   p_api_version         => 1.0,
6489                   p_commit              => fnd_api.g_false,
6490                   p_init_msg_list       => fnd_api.g_true,
6491                   p_validation_level    => fnd_api.g_valid_level_full,
6492                   p_pricing_attribs_tbl => l_new_pricing_attribs_tbl, --p_pricing_attribs_tbl,
6493                   p_txn_rec             => l_trx_rec,
6494                   x_return_status       => x_return_status,
6495                   x_msg_count           => x_msg_count,
6496                   x_msg_data            => x_msg_data);
6497 
6498                 IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
6499                   g_api_name := 'csi_pricing_attribs_pub.create_pricing_attribs';
6500                   raise fnd_api.g_exc_error;
6501                 END IF;
6502 
6503                 debug('Pricing Attributes created successfully.');
6504 
6505               END IF;
6506 
6507 /*
6508               csi_utl_pkg.create_party_and_acct(
6509                 p_instance_id            => l_instance_id,
6510                 p_txn_line_detail_rec    => l_tld_rec,
6511                 p_txn_party_detail_tbl   => l_tld_party_tbl,
6512                 p_txn_pty_acct_dtl_tbl   => l_tld_account_tbl,
6513                 p_order_line_rec         => l_order_line_rec,
6514                 p_trx_rec                => l_trx_rec,
6515                 x_return_status          => x_return_status);
6516 
6517               IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
6518                 raise fnd_api.g_exc_error;
6519               END IF;
6520 */
6521 
6522               debug('Party and Account created successfully.');
6523 
6524               IF l_cre_org_units_tbl.count > 0 THEN
6525 
6526                 csi_t_gen_utility_pvt.dump_api_info(
6527                   p_api_name => 'create_organization_unit',
6528                   p_pkg_name => 'csi_organization_unit_pub');
6529 
6530                 csi_organization_unit_pub.create_organization_unit(
6531                   p_api_version         => 1.0,
6532                   p_commit              => fnd_api.g_false,
6533                   p_init_msg_list       => fnd_api.g_true,
6534                   p_validation_level    => fnd_api.g_valid_level_full,
6535                   p_org_unit_tbl        => l_cre_org_units_tbl,
6536                   p_txn_rec             => l_trx_rec,
6537                   x_return_status       => x_return_status,
6538                   x_msg_count           => x_msg_count,
6539                   x_msg_data            => x_msg_data);
6540 
6541                 IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
6542                   g_api_name := 'csi_organization_unit_pub.create_organization_unit';
6543                   raise fnd_api.g_exc_error;
6544                 END IF;
6545 
6546                 debug('Org Assignment created successfully.');
6547               END IF;
6548 
6549               -- BEGIN MRK CODE FOR UPDATE EXTENDED ATTRIBUTE
6550 
6551               IF l_cre_ext_attrib_val_tbl.count > 0 THEN
6552 
6553                 --Changes for bug 3901064 Start
6554                   l_old_extended_attribs_tbl.delete;
6555                   l_new_extended_attribs_tbl.delete;
6556                   l_ext_old_index := 0;
6557                   l_ext_new_index := 0;
6558                 --Changes for bug 3901064 End
6559 
6560                 FOR i in l_cre_ext_attrib_val_tbl.first..l_cre_ext_attrib_val_tbl.last LOOP
6561                   BEGIN
6562                     SELECT attribute_value_id,
6563                            object_version_number
6564                     INTO   l_cre_ext_attrib_val_tbl(i).attribute_value_id,
6565                            l_cre_ext_attrib_val_tbl(i).object_version_number
6566                     FROM   csi_iea_values
6567                     WHERE  instance_id = l_cre_ext_attrib_val_tbl(i).instance_id
6568                     AND    attribute_id = l_cre_ext_attrib_val_tbl(i).attribute_id;
6569 
6570                   EXCEPTION
6571                      WHEN NO_DATA_FOUND THEN
6572                           Null;
6573                   END;
6574                   IF (  l_cre_ext_attrib_val_tbl(i).attribute_value_id is not null
6575                        AND
6576                         l_cre_ext_attrib_val_tbl(i).attribute_value_id <> fnd_api.g_miss_num
6577                       )
6578                   THEN
6579                     l_old_extended_attribs_tbl(l_ext_old_index) := l_cre_ext_attrib_val_tbl(i);
6580                     l_ext_old_index                             := l_ext_old_index + 1;
6581                   ELSE
6582                     l_new_extended_attribs_tbl(l_ext_new_index) := l_cre_ext_attrib_val_tbl(i);
6583                     l_ext_new_index                             := l_ext_new_index + 1;
6584                   END IF;
6585                 END LOOP;
6586               END IF;
6587 
6588               IF l_old_extended_attribs_tbl.count > 0 THEN
6589                 debug('Update Extended_Attributes Count '||l_old_extended_attribs_tbl.count);
6590 
6591                 csi_t_gen_utility_pvt.dump_api_info(
6592                   p_api_name => 'update_extended_attrib_values',
6593                   p_pkg_name => 'csi_item_instance_pub');
6594 
6595                 csi_item_instance_pub.update_extended_attrib_values(
6596                   p_api_version         => 1.0,
6597                   p_commit              => fnd_api.g_false,
6598                   p_init_msg_list       => fnd_api.g_true,
6599                   p_validation_level    => fnd_api.g_valid_level_full,
6600                   p_ext_attrib_tbl      => l_old_extended_attribs_tbl,
6601                   p_txn_rec             => l_trx_rec,
6602                   x_return_status       => x_return_status,
6603                   x_msg_count           => x_msg_count,
6604                   x_msg_data            => x_msg_data);
6605 
6606                 IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
6607                    g_api_name := 'csi_item_instance_pub.update_extended_attrib_values';
6608                    raise fnd_api.g_exc_error;
6609                 END IF;
6610 
6611                 debug('Extended Attributed updated successfully.');
6612               END IF;
6613 
6614               --IF l_cre_ext_attrib_val_tbl.count > 0 THEN
6615               IF l_new_extended_attribs_tbl.count > 0 THEN
6616                 debug('Create Extended_Attributes Count '||l_new_extended_attribs_tbl.count);
6617 
6618                 -- END MRK CODE FOR UPDATE EXTENDED ATTRIBUTE
6619 
6620                 csi_t_gen_utility_pvt.dump_api_info(
6621                   p_api_name => 'create_extended_attrib_values',
6622                   p_pkg_name => 'csi_item_instance_pub');
6623 
6624                 csi_item_instance_pub.create_extended_attrib_values(
6625                   p_api_version         => 1.0,
6626                   p_commit              => fnd_api.g_false,
6627                   p_init_msg_list       => fnd_api.g_true,
6628                   p_validation_level    => fnd_api.g_valid_level_full,
6629                   p_ext_attrib_tbl      =>  l_new_extended_attribs_tbl, --l_cre_ext_attrib_val_tbl
6630                   p_txn_rec             => l_trx_rec,
6631                   x_return_status       => x_return_status,
6632                   x_msg_count           => x_msg_count,
6633                   x_msg_data            => x_msg_data);
6634 
6635                 IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
6636                   g_api_name := 'csi_item_instance_pub.create_extended_attrib_values';
6637                   raise fnd_api.g_exc_error;
6638                 END IF;
6639 
6640                 debug('Extended Attributed created successfully.');
6641 
6642               END IF;
6643             END IF;
6644 
6645             IF p_source = 'FULFILLMENT'
6646                AND
6647                (NVL(l_tld_rec.active_end_date,l_date)  <> l_date)
6648             THEN
6649 
6650               /*-----------------------------------------------------*/
6651               /* If instance is expired then check if there is any   */
6652               /* relationships in csi_ii_relationship. If exist then */
6653               /* expire the instance-to-instance relationships also  */
6654               /*-----------------------------------------------------*/
6655 
6656               BEGIN
6657 
6658                 SELECT relationship_id,
6659                        object_version_number
6660                 INTO   l_relationship_id,
6661                        l_ii_rel_obj_ver_num
6662                 FROM   csi_ii_relationships
6663                 WHERE  subject_id = l_tld_rec.instance_id
6664                 AND    (active_end_date is null OR active_end_date >= sysdate);
6665 
6666                 l_exp_ii_relationship_rec.relationship_id := l_relationship_id;
6667                 l_exp_ii_relationship_rec.object_version_number := l_ii_rel_obj_ver_num;
6668 
6669                 csi_t_gen_utility_pvt.dump_api_info(
6670                   p_api_name => 'expire_relationship',
6671                   p_pkg_name => 'csi_ii_relationships_pub');
6672 
6673                 csi_ii_relationships_pub.expire_relationship(
6674                   p_api_version           => 1.0,
6675                   p_commit                => fnd_api.g_false,
6676                   p_init_msg_list         => fnd_api.g_true,
6677                   p_validation_level      => fnd_api.g_valid_level_full,
6678                   p_relationship_rec      => l_exp_ii_relationship_rec,
6679                   p_txn_rec               => l_trx_rec,
6680                   x_instance_id_lst       => l_exp_instance_id_tbl,
6681                   x_return_status         => x_return_status,
6682                   x_msg_count             => x_msg_count,
6683                   x_msg_data              => x_msg_data);
6684 
6685                 IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
6686                   g_api_name := 'csi_ii_relationship_pub.expire_relationship';
6687                   raise fnd_api.g_exc_error;
6688                 END IF;
6689 
6690               EXCEPTION
6691                 WHEN OTHERS THEN
6692                   NULL;
6693               END;
6694 
6695             END IF; --l_tld_rec.active_end_date,l_date
6696 
6697             IF (p_order_line_rec.serial_code = 1)
6698                 AND
6699                (l_split_flag  = 'Y' )
6700                 AND
6701                (l_upd_instance_rec.quantity > 1)
6702             THEN
6703 
6704               l_auto_split_instances.delete;
6705 	      --Added for bug 5112946--
6706               l_upd_instance_rec.version_label            := nvl(l_tld_rec.version_label,fnd_api.g_miss_char);
6707 
6708               auto_split_instances(
6709                 p_instance_rec  => l_upd_instance_rec,
6710                 px_txn_rec      => l_trx_rec,
6711                 x_instance_tbl  => l_auto_split_instances,
6712                 x_return_status => l_return_status);
6713 
6714               IF l_return_status <> fnd_api.g_ret_sts_success THEN
6715                 RAISE fnd_api.g_exc_error;
6716               END IF;
6717 
6718               IF l_auto_split_instances.count > 0 THEN
6719                 l_cp_ind := 0;
6720                 l_cps_tbl.delete;
6721                 FOR nis_ind in l_auto_split_instances.FIRST .. l_auto_split_instances.LAST
6722                 LOOP
6723                   debug(' Instance ID :'||l_auto_split_instances(nis_ind).instance_id);
6724 
6725                   l_cp_ind := l_cp_ind + 1;
6726                   l_cps_tbl(l_cp_ind).instance_id        := l_auto_split_instances(nis_ind).instance_id;
6727                   l_cps_tbl(l_cp_ind).quantity           := 1;
6728                   l_cps_tbl(l_cp_ind).txn_line_detail_id := l_upd_instance_rec.last_txn_line_detail_id;
6729                   l_cps_tbl(l_cp_ind).line_id            := l_upd_instance_rec.last_oe_order_line_id;
6730                   l_cps_tbl(l_cp_ind).transaction_id     := l_trx_rec.transaction_id;
6731 
6732                 END LOOP;
6733               END IF;
6734 
6735               IF p_txn_ii_rltns_tbl.count > 0 THEN
6736 
6737                 csi_utl_pkg.build_inst_ii_tbl(
6738                   p_orig_inst_id     => l_upd_instance_rec.instance_id,
6739                   p_txn_ii_rltns_tbl => p_txn_ii_rltns_tbl ,
6740                   p_new_instance_tbl => l_auto_split_instances,
6741                   x_return_status    => x_return_status  );
6742 
6743                 IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
6744                   raise fnd_api.g_exc_error;
6745                 END IF;
6746 
6747               END IF; -- p_txn_ii_rltns_tbl.count > 0
6748             END IF; -- l_split_flag  = 'Y'
6749 
6750       ELSE /* Instance reference does not exist so create instance */
6751 
6752         l_cre_instance_rec := l_tmp_instance_rec;
6753 
6754         /* get the master organization */
6755         csi_utl_pkg.get_master_organization(
6756           p_organization_id        => l_tld_rec.inv_organization_id,
6757           p_master_organization_id => x_master_org_id,
6758           x_return_status          => x_return_status);
6759 
6760         IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
6761           raise fnd_api.g_exc_error;
6762         END IF;
6763 
6764         debug('Instance reference does not exist, So calling create API');
6765 
6766         IF NVL(x_trx_sub_type_rec.src_status_id ,fnd_api.g_miss_num) = fnd_api.g_miss_num THEN
6767 
6768           l_inst_status_id := l_dflt_inst_status_id;
6769 
6770           l_cre_instance_rec.instance_status_id      := l_inst_status_id;
6771         ELSE
6772           l_cre_instance_rec.instance_status_id      := x_trx_sub_type_rec.src_status_id;
6773         END IF;
6774 
6775         l_trx_rec.txn_sub_type_id := l_tld_rec.sub_type_id;
6776 
6777         /* If the instance reference does not exist then create an instance */
6778 
6779         l_cre_instance_rec.instance_id             := fnd_api.g_miss_num;
6780         l_cre_instance_rec.instance_number         := fnd_api.g_miss_char;
6781         l_cre_instance_rec.external_reference      := l_tld_rec.external_reference ;
6782         l_cre_instance_rec.inventory_item_id       := l_tld_rec.inventory_item_id;
6783         l_cre_instance_rec.inventory_revision      := l_tld_rec.inventory_revision;
6784         l_cre_instance_rec.inv_master_organization_id := x_master_org_id;
6785         l_cre_instance_rec.serial_number           := l_tld_rec.serial_number;
6786         l_cre_instance_rec.mfg_serial_number_flag  := l_tld_rec.mfg_serial_number_flag;
6787         l_cre_instance_rec.lot_number              := l_tld_rec.lot_number;
6788         l_cre_instance_rec.quantity                := l_tld_rec.quantity;
6789         l_cre_instance_rec.unit_of_measure         := l_tld_rec.unit_of_measure;
6790         -- l_cre_instance_rec.accounting_class_code   := 'CUST_PROD'   ;
6791         l_cre_instance_rec.instance_condition_id   := l_tld_rec.item_condition_id;
6792         l_cre_instance_rec.customer_view_flag      := 'Y';
6793         l_cre_instance_rec.merchant_view_flag      := 'Y';
6794         l_cre_instance_rec.sellable_flag           := l_tld_rec.sellable_flag;
6795         l_cre_instance_rec.system_id               := l_tld_rec.csi_system_id;
6796         l_cre_instance_rec.instance_type_code      := l_tld_rec.instance_type_code;
6797         -- l_cre_instance_rec.active_start_date       := l_tld_rec.active_start_date;
6798         l_cre_instance_rec.active_end_date         := l_tld_rec.active_end_date;
6799         l_cre_instance_rec.location_type_code      := l_tld_rec.location_type_code;
6800         l_cre_instance_rec.location_id             := l_tld_rec.location_id;
6801         -- Changed for Partner ordering
6802         l_cre_instance_rec.install_location_type_code      := l_tld_rec.install_location_type_code;
6803         l_cre_instance_rec.source_code             := p_order_line_rec.source_code; -- Added for Siebel Genesis Project
6804         IF p_order_line_rec.ib_install_loc is not null
6805           AND
6806            p_order_line_rec.ib_install_loc <> fnd_api.g_miss_char
6807         THEN
6808           BEGIN
6809             SELECT HCAS.party_site_id
6810             INTO   l_cre_party_site_id
6811             FROM   hz_cust_site_uses_all  HCSU,
6812                    hz_cust_acct_sites_all HCAS
6813             WHERE  HCSU.site_use_id       = p_order_line_rec.ib_install_loc_id
6814             AND    HCAS.cust_acct_site_id = HCSU.cust_acct_site_id;
6815 
6816           EXCEPTION
6817             WHEN others THEN
6818                  NULL;
6819           END;
6820 
6821           IF l_cre_party_site_id <> l_tld_rec.install_location_id
6822           THEN
6823             l_cre_instance_rec.install_location_id := l_cre_party_site_id;
6824           -- Added the else for Bug 3419098
6825           ELSE
6826             l_cre_instance_rec.install_location_id := l_tld_rec.install_location_id;
6827           END IF;
6828         ELSE
6829           l_cre_instance_rec.install_location_id             := l_tld_rec.install_location_id;
6830         END IF;
6831         -- Changed for Partner ordering
6832         l_cre_instance_rec.vld_organization_id     := l_tld_rec.inv_organization_id;
6833         l_cre_instance_rec.last_oe_order_line_id   := p_order_line_rec.order_line_id ;
6834         l_cre_instance_rec.last_txn_line_detail_id := l_tld_rec.txn_line_detail_id ;
6835 
6836         /* Begin fix for Bug 3254347 Install_Date and Start_Date changes */
6837         IF l_tld_rec.active_start_date IS NOT NULL
6838           AND
6839            l_tld_rec.active_start_date <> fnd_api.g_miss_date
6840         THEN
6841           IF p_source = 'FULFILLMENT'
6842           THEN
6843             --l_cre_instance_rec.active_start_date   := p_order_line_rec.fulfillment_date; Bug 3807619
6844             l_cre_instance_rec.active_start_date   := nvl(Nvl(l_tld_rec.active_start_date,p_order_line_rec.fulfillment_date),sysdate); -- Bug 7668298
6845           ELSIF  p_source = 'SHIPMENT'
6846           THEN
6847             l_cre_instance_rec.active_start_date   := p_order_line_rec.transaction_date;
6848           END IF;
6849         END IF;
6850         /* End fix for Bug 3254347 Install_Date and Start_Date changes */
6851 
6852         /* Fix for Bug 3578671 */
6853         debug(' Default Install Date Profile Value '||l_default_install_date);
6854 
6855         IF nvl(l_tld_rec.installation_date,fnd_api.g_miss_date) = fnd_api.g_miss_date THEN
6856            IF l_default_install_date = 'Y' THEN
6857               IF p_source =  'SHIPMENT' THEN
6858                  -- For Bug 3578671
6859                  IF nvl(p_order_line_rec.actual_shipment_date,fnd_api.g_miss_date) = fnd_api.g_miss_date THEN
6860                     l_cre_instance_rec.install_date := l_trx_rec.source_transaction_date;
6861                  ELSE
6862                     l_cre_instance_rec.install_date := p_order_line_rec.actual_shipment_date;
6863                  END IF;
6864               ELSIF p_source = 'FULFILLMENT' THEN
6865                  -- For Bug 3578671
6866                  IF nvl(p_order_line_rec.fulfillment_date,fnd_api.g_miss_date) = fnd_api.g_miss_date THEN
6867                    l_cre_instance_rec.install_date := sysdate;
6868                  ELSE
6869                    l_cre_instance_rec.install_date := p_order_line_rec.fulfillment_date;
6870                  END IF;
6871               END IF;
6872            END IF;
6873         ELSE
6874           /* Begin fix for Bug 3254347 Install_Date and Start_Date changes */
6875           IF p_source =  'SHIPMENT' THEN
6876            l_cre_instance_rec.install_date       :=
6877                GREATEST(l_tld_rec.installation_date,p_order_line_rec.actual_shipment_date);
6878           ELSIF p_source = 'FULFILLMENT' THEN
6879 					  l_cre_instance_rec.install_date       := l_tld_rec.installation_date; --Added for bug 7668298
6880   				 /* --Commented  for bug 7668298
6881 						IF p_order_line_rec.fulfillment_date is NOT NULL
6882 							AND
6883 							 p_order_line_rec.fulfillment_date <> fnd_api.g_miss_date
6884 						THEN
6885 							l_cre_instance_rec.install_date      := GREATEST(l_tld_rec.installation_date,p_order_line_rec.fulfillment_date);
6886 						ELSE
6887 							l_cre_instance_rec.install_date      := GREATEST(l_tld_rec.installation_date,sysdate);
6888 						END IF;
6889 						*/ --Commented  for bug 7668298
6890 					END IF;
6891 					/* End fix for Bug 3254347 Install_Date and Start_Date changes */
6892         END IF;
6893 
6894         l_cre_instance_rec.manually_created_flag   := 'N';
6895         l_cre_instance_rec.return_by_date          := l_tld_rec.return_by_date;
6896         l_cre_instance_rec.creation_complete_flag  := fnd_api.g_miss_char;
6897         l_cre_instance_rec.completeness_flag       := fnd_api.g_miss_char;
6898         l_cre_instance_rec.instance_usage_code     := 'OUT_OF_ENTERPRISE';
6899         l_cre_instance_rec.last_oe_agreement_id    := p_order_line_rec.agreement_id;
6900         -- Included for Bug 2962072.
6901         l_cre_instance_rec.version_label           := l_tld_rec.version_label;
6902         l_cre_instance_rec.operational_status_code := 'NOT_USED';
6903 
6904         /* Fix for Bug 2668504   */
6905         l_cre_pricing_attribs_tbl := p_pricing_attribs_tbl;
6906 
6907 	-- Bug 13572608
6908 
6909 	IF (l_cre_party_tbl.count = 0 AND l_upd_party_tbl.count = 0 AND NVL(x_trx_sub_type_rec.src_change_owner,'N') = 'N') THEN
6910 		fnd_message.set_name('CSI','CSI_INT_PTY_OWNER_REC_MISSING');
6911 		fnd_message.set_token('TXN_LINE_DETAIL_ID',l_tld_rec.txn_line_detail_id);
6912                 fnd_msg_pub.add;
6913                 raise fnd_api.g_exc_error;
6914 	END IF;
6915 
6916    	-- End Bug 13572608
6917 
6918         l_owner_pty_rec  := l_cre_party_tbl(1);
6919 
6920         -- Bug 9588170
6921         -- When account party table has no records the account record should be
6922         -- assigned to NULL. For example when there is no default account assigned
6923         -- for internal party
6924         IF l_cre_party_acct_tbl.COUNT > 0 THEN
6925           l_owner_acct_rec := l_cre_party_acct_tbl(1);
6926         ELSE
6927           l_owner_acct_rec := NULL;
6928         END IF;
6929         -- End Bug 9588170
6930 
6931         csi_utl_pkg.get_parties_and_accounts(
6932           p_instance_id      => l_cre_instance_rec.instance_id,
6933           p_tld_rec          => l_tld_rec,
6934           p_t_pty_tbl        => l_tld_party_tbl,
6935           p_t_pty_acct_tbl   => l_tld_account_tbl,
6936           p_owner_pty_rec    => l_owner_pty_rec,
6937           p_owner_acct_rec   => l_owner_acct_rec,
6938           p_order_line_rec   => p_order_line_rec,
6939           x_i_pty_tbl        => l_cre_party_tbl,
6940           x_i_pty_acct_tbl   => l_cre_party_acct_tbl,
6941           x_return_status    => l_return_status);
6942 
6943         IF l_return_status <> fnd_api.g_ret_sts_success THEN
6944           RAISE fnd_api.g_exc_error;
6945         END IF;
6946         --
6947         -- srramakr TSO with Equipment
6948         -- Serialized instance gets created as a part of shipment for Items with srl ctl 6 (AT SO Issue)
6949         -- Hence assigning the config keys during create.
6950         --
6951         IF p_order_line_rec.serial_code = 6 THEN
6952           l_cre_instance_rec.CONFIG_INST_HDR_ID := l_tld_rec.CONFIG_INST_HDR_ID;
6953           l_cre_instance_rec.CONFIG_INST_REV_NUM := l_tld_rec.CONFIG_INST_REV_NUM;
6954           l_cre_instance_rec.CONFIG_INST_ITEM_ID := l_tld_rec.CONFIG_INST_ITEM_ID;
6955         END IF;
6956         --
6957 
6958         l_cre_instance_rec.sales_currency_code := l_order_line_rec.currency_code;
6959 
6960         IF l_order_line_rec.primary_uom <> l_order_line_rec.order_quantity_uom THEN
6961 
6962           l_item_control_rec.inventory_item_id := l_order_line_rec.inv_item_id;
6963           l_item_control_rec.organization_id   := l_order_line_rec.inv_org_id;
6964           l_item_control_rec.primary_uom_code  := l_order_line_rec.primary_uom;
6965 
6966           csi_utl_pkg.get_unit_price_in_primary_uom(
6967             p_unit_price                => l_order_line_rec.unit_price,
6968             p_unit_price_uom            => l_order_line_rec.order_quantity_uom,
6969             px_item_control_rec         => l_item_control_rec,
6970             x_unit_price_in_primary_uom => l_cre_instance_rec.sales_unit_price,
6971             x_return_status             => l_return_status);
6972 
6973           IF l_return_status <> fnd_api.g_ret_sts_success THEN
6974             RAISE fnd_api.g_exc_error;
6975           END IF;
6976 
6977         ELSE
6978           l_cre_instance_rec.sales_unit_price := l_order_line_rec.unit_price;
6979         END IF;
6980 
6981         debug('  party_tbl.count           :'||l_cre_party_tbl.count);
6982         debug('  party_acct_tbl.count      :'||l_cre_party_acct_tbl.count);
6983         debug('  org_units_tbl.count       :'||l_cre_org_units_tbl.count);
6984         debug('  inst_asset_tbl.count      :'||l_cre_inst_asset_tbl.count);
6985         debug('  pricing_attribs_tbl.count :'||l_cre_pricing_attribs_tbl.count);
6986         debug('  ext_attrib_val_tbl.count  :'||l_cre_ext_attrib_val_tbl.count);
6987 
6988         csi_t_gen_utility_pvt.dump_api_info(
6989           p_api_name => 'create_item_instance',
6990           p_pkg_name => 'csi_item_instance_pub');
6991 
6992         IF l_debug_level > 1 THEN
6993           csi_t_gen_utility_pvt.dump_csi_instance_rec(l_cre_instance_rec);
6994         END IF;
6995 
6996         /* for order fulfillment instance ref is not there so create an instance */
6997         csi_item_instance_pub.create_item_instance(
6998           p_api_version           => 1.0,
6999           p_commit                => fnd_api.g_false,
7000           p_init_msg_list         => fnd_api.g_true,
7001           p_validation_level      => fnd_api.g_valid_level_full,
7002           p_instance_rec          => l_cre_instance_rec,
7003           p_ext_attrib_values_tbl => l_cre_ext_attrib_val_tbl,
7004           p_party_tbl             => l_cre_party_tbl,
7005           p_account_tbl           => l_cre_party_acct_tbl,
7006           p_pricing_attrib_tbl    => l_cre_pricing_attribs_tbl,
7007           p_org_assignments_tbl   => l_cre_org_units_tbl,
7008           p_asset_assignment_tbl  => l_cre_inst_asset_tbl,
7009           p_txn_rec               => l_trx_rec,
7010           x_return_status         => x_return_status,
7011           x_msg_count             => x_msg_count,
7012           x_msg_data              => x_msg_data  );
7013 
7014         -- For Bug 4057183
7015         -- IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
7016         IF NOT(x_return_status in(fnd_api.g_ret_sts_success,'W')) THEN
7017           g_api_name := 'csi_item_instance_pub.create_item_instance';
7018           raise fnd_api.g_exc_error;
7019         END IF;
7020 
7021         l_cp_ind := l_cp_ind + 1;
7022 
7023         l_cps_tbl(l_cp_ind).instance_id        := l_cre_instance_rec.instance_id;
7024         l_cps_tbl(l_cp_ind).quantity           := l_cre_instance_rec.quantity;
7025         l_cps_tbl(l_cp_ind).txn_line_detail_id := l_cre_instance_rec.last_txn_line_detail_id;
7026         l_cps_tbl(l_cp_ind).line_id            := l_cre_instance_rec.last_oe_order_line_id;
7027         l_cps_tbl(l_cp_ind).transaction_id     := l_trx_rec.transaction_id;
7028         l_cps_tbl(l_cp_ind).serial_number      := l_cre_instance_rec.serial_number;
7029         l_cps_tbl(l_cp_ind).lot_number         := l_cre_instance_rec.lot_number;
7030 
7031         debug('Instance created successfully. Instance ID :'||l_cre_instance_rec.instance_id);
7032 
7033         l_tld_rec.changed_instance_id    := l_cre_instance_rec.instance_id;
7034 
7035         /* After instance is created assign the instance_id for the subject and object id */
7036 
7037         l_tld_rec.instance_id := l_cre_instance_rec.instance_id;
7038         l_tld_rec.instance_exists_flag := 'Y';
7039 
7040         debug('  txn_party_detail_tbl.count :'||p_txn_party_detail_tbl.count);
7041         debug('  txn_pty_acct_dtl_tbl.count :'||p_txn_pty_acct_dtl_tbl.count);
7042 
7043 /*
7044         csi_utl_pkg.create_party_and_acct(
7045           p_instance_id            => l_cre_instance_rec.instance_id,
7046           p_txn_line_detail_rec    => l_tld_rec ,
7047           p_txn_party_detail_tbl   => p_txn_party_detail_tbl ,
7048           p_txn_pty_acct_dtl_tbl   => p_txn_pty_acct_dtl_tbl,
7049           p_order_line_rec         => p_order_line_rec,
7050           p_trx_rec                => l_trx_rec,
7051           x_return_status          => x_return_status );
7052 
7053         IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
7054           RAISE fnd_api.g_exc_error;
7055         END IF;
7056 */
7057 
7058         IF l_cre_instance_rec.quantity > 1
7059            AND
7060            l_split_flag  = 'Y'
7061            AND
7062            p_order_line_rec.serial_code = 1
7063         THEN
7064 
7065           l_auto_split_instances.delete;
7066 
7067           auto_split_instances(
7068             p_instance_rec  => l_cre_instance_rec,
7069             px_txn_rec      => l_trx_rec,
7070             x_instance_tbl  => l_auto_split_instances,
7071             x_return_status => l_return_status);
7072 
7073           IF l_return_status <> fnd_api.g_ret_sts_success THEN
7074             RAISE fnd_api.g_exc_error;
7075           END IF;
7076 
7077 
7078           IF l_auto_split_instances.count > 0 THEN
7079             l_cp_ind := 0;
7080             l_cps_tbl.delete;
7081             FOR nis_ind in l_auto_split_instances.FIRST .. l_auto_split_instances.LAST
7082             LOOP
7083 
7084               l_cp_ind := l_cp_ind + 1;
7085               l_cps_tbl(l_cp_ind).instance_id        := l_auto_split_instances(nis_ind).instance_id;
7086               l_cps_tbl(l_cp_ind).quantity           := 1;
7087               l_cps_tbl(l_cp_ind).txn_line_detail_id :=
7088                                         l_cre_instance_rec.last_txn_line_detail_id;
7089               l_cps_tbl(l_cp_ind).line_id            :=
7090                                         l_cre_instance_rec.last_oe_order_line_id;
7091               l_cps_tbl(l_cp_ind).transaction_id     := l_trx_rec.transaction_id;
7092 
7093              END LOOP;
7094            END IF;
7095 
7096                 IF p_txn_ii_rltns_tbl.count > 0 THEN
7097 
7098                   csi_utl_pkg.build_inst_ii_tbl(
7099                     p_orig_inst_id     => l_cre_instance_rec.instance_id,
7100                     p_txn_ii_rltns_tbl => p_txn_ii_rltns_tbl,
7101                     p_new_instance_tbl => l_auto_split_instances,
7102                     x_return_status    => x_return_status);
7103 
7104                   IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
7105                     raise fnd_api.g_exc_error;
7106                   END IF;
7107 
7108                   debug('build_inst_ii_tbl completed successfully ');
7109                 END IF; -- p_txn_ii_rltns_tbl.count > 0
7110               END IF; -- l_split_flag  = 'Y'
7111             END IF; -- end if for instance_exists_flag = 'Y'
7112 
7113             /* Update the txn_line_detail to processed after updating IB successfully */
7114 
7115             l_tld_rec.processing_status  := 'PROCESSED';
7116             l_tld_rec.error_code         := NULL;
7117             l_tld_rec.error_explanation  := NULL;
7118             l_tld_rec.csi_transaction_id := p_trx_rec.transaction_id;
7119 
7120           END IF;  -- end if for source_trx_flag = 'Y' -- bug 3692473.The child item instances for a Non-sourced item instance gets updated in a replacement scenario. This need to happen after the get_ii_realtion_tbl routine.
7121 
7122           IF l_cps_tbl.COUNT > 0 THEN
7123             FOR r_cp_ind IN l_cps_tbl.FIRST .. l_cps_tbl.LAST
7124             LOOP
7125               l_acp_ind := l_acp_ind + 1;
7126               l_all_cps_tbl(l_acp_ind) := l_cps_tbl(r_cp_ind);
7127             END LOOP;
7128           END IF;
7129 
7130           p_txn_line_detail_tbl(i) := l_tld_rec;
7131 
7132         END LOOP; -- end of for  loop for p_txn_detail_tbl
7133 
7134         debug('customer products :'||l_all_cps_tbl.COUNT);
7135 
7136     -- added for bug
7137         dump_customer_products(l_all_cps_tbl);
7138 
7139         get_single_qty_instances(
7140           p_all_cps_tbl    => l_all_cps_tbl,
7141           x_single_cps_tbl => l_single_cps_tbl);
7142 
7143         IF l_single_cps_tbl.count > 0 THEN
7144 
7145           l_bom_explode_flag := csi_utl_pkg.check_standard_bom(
7146                                   p_order_line_rec  => l_order_line_rec);
7147 
7148           IF l_bom_explode_flag THEN
7149 
7150             FOR l_scp_ind IN l_single_cps_tbl.FIRST .. l_single_cps_tbl.LAST
7151             LOOP
7152 
7153               IF NOT(csi_utl_pkg.wip_config_exists(l_single_cps_tbl(l_scp_ind).instance_id)) THEN
7154 
7155                 l_relationship_exist := check_option_relationship (
7156                   p_option_object_id    => l_single_cps_tbl(l_scp_ind).instance_id,
7157                   p_source              => p_source,
7158                   p_order_line_rec      => l_order_line_rec
7159                 );
7160 				debug('l_single_cps_tbl(l_scp_ind).instance_id - ' ||  l_single_cps_tbl(l_scp_ind).instance_id) ;
7161 				debug('p_source - ' ||  p_source) ;
7162 			    debug('l_order_line_rec.item_type_code - ' ||  l_order_line_rec.item_type_code) ;
7163 				--debug('l_config_rec.config_wip_job_id - ' ||  l_config_rec.config_wip_job_id) ;
7164                 --debug('l_relationship_exist - ' ||  l_relationship_exist) ;
7165 
7166                 IF (l_relationship_exist) THEN
7167 
7168                 l_bom_std_item_rec.instance_id         := l_single_cps_tbl(l_scp_ind).instance_id ;
7169                 l_bom_std_item_rec.inventory_item_id   := l_order_line_rec.inv_item_id ;
7170                 l_bom_std_item_rec.vld_organization_id := l_order_line_rec.inv_org_id ;
7171                 l_bom_std_item_rec.quantity            := 1;
7172 
7173                 l_bom_ind := l_bom_ind + 1;
7174                 l_bom_std_item_tbl(l_bom_ind) := l_bom_std_item_rec;
7175 
7176                 END IF; -- l_relationship_exist = 'N'
7177 
7178               END IF;
7179             END LOOP;
7180 
7181             IF l_bom_std_item_tbl.COUNT > 0 THEN
7182               debug('explode bom start time :'||to_char(sysdate, 'hh24:mi:ss'));
7183 
7184               csi_t_gen_utility_pvt.dump_api_info(
7185                 p_pkg_name => 'csi_item_instance_grp',
7186                 p_api_name => 'explode_bom');
7187 
7188               csi_item_instance_grp.explode_bom( -- changes done to call the Group API for performance issues, bug3722382
7189                 p_api_version         => 1.0,
7190                 p_commit              => fnd_api.g_false,
7191                 p_init_msg_list       => fnd_api.g_true,
7192                 p_validation_level    => fnd_api.g_valid_level_full,
7193                 p_source_instance_tbl => l_bom_std_item_tbl,
7194                 p_explosion_level     => fnd_api.g_miss_num,
7195                 p_txn_rec             => l_trx_rec,
7196                 x_return_status       => l_return_status,
7197                 x_msg_count           => l_msg_count,
7198                 x_msg_data            => l_msg_data);
7199 
7200               IF l_return_status <> fnd_api.g_ret_sts_success THEN
7201                 RAISE fnd_api.g_exc_error;
7202               END IF;
7203 
7204               debug('explode bom end time   :'||to_char(sysdate, 'hh24:mi:ss'));
7205             END IF;
7206           ELSE
7207 
7208             IF l_order_line_rec.item_type_code = 'STANDARD'
7209                AND
7210                l_order_line_rec.order_line_id = l_order_line_rec.ato_line_id
7211                AND
7212                nvl(l_order_line_rec.top_model_line_id, fnd_api.g_miss_num) = fnd_api.g_miss_num
7213                AND
7214                l_order_line_rec.serial_code in (1, 6)
7215             THEN
7216 
7217               BEGIN
7218 
7219                 SELECT wip_entity_id
7220                 INTO   l_wip_job_id
7221                 FROM   wip_discrete_jobs
7222                 WHERE  primary_item_id = l_order_line_rec.inv_item_id
7223                 AND    organization_id = l_order_line_rec.inv_org_id
7224                 AND    source_line_id  = l_order_line_rec.order_line_id
7225                 AND    status_type    <> 7;  -- excluding the cancelled wip jobs
7226 
7227                 get_comp_instances_from_wip(
7228                   p_wip_entity_id    => l_wip_job_id,
7229                   p_organization_id  => l_order_line_rec.inv_org_id,
7230                   p_cps_tbl          => l_single_cps_tbl,
7231                   px_csi_txn_rec     => l_trx_rec,
7232                   x_iir_tbl          => l_wip_iir_tbl,
7233                   x_return_status    => l_return_status);
7234 
7235                 IF l_return_status <> fnd_api.g_ret_sts_success THEN
7236                   RAISE fnd_api.g_exc_error;
7237                 END IF;
7238 
7239                 IF l_wip_iir_tbl.COUNT > 0 THEN
7240                   FOR l_w_ind IN l_wip_iir_tbl.FIRST .. l_wip_iir_tbl.LAST
7241                   LOOP
7242 
7243                     debug('object_id :'||l_wip_iir_tbl(l_w_ind).object_id||
7244                           'subject_id :'||l_wip_iir_tbl(l_w_ind).subject_id);
7245 
7246                     convert_wip_instance_to_cp(
7247                       p_instance_id       => l_wip_iir_tbl(l_w_ind).subject_id,
7248                       p_line_id           => l_order_line_rec.order_line_id,
7249                       p_csi_txn_rec       => l_trx_rec,
7250                       x_return_status     => l_return_status);
7251 
7252                     IF l_return_status <> fnd_api.g_ret_sts_success THEN
7253                       RAISE fnd_api.g_exc_error;
7254                     END IF;
7255 
7256                   END LOOP;
7257 
7258                   csi_t_gen_utility_pvt.dump_api_info(
7259                     p_api_name => 'create_relationship',
7260                     p_pkg_name => 'csi_ii_relationships_pub');
7261 
7262                   csi_ii_relationships_pub.create_relationship(
7263                     p_api_version      => 1.0,
7264                     p_commit           => fnd_api.g_false,
7265                     p_init_msg_list    => fnd_api.g_true,
7266                     p_validation_level => fnd_api.g_valid_level_full,
7267                     p_relationship_tbl => l_wip_iir_tbl,
7268                     p_txn_rec          => l_trx_rec,
7269                     x_return_status    => l_return_status,
7270                     x_msg_count        => l_msg_count,
7271                     x_msg_data         => l_msg_data);
7272 
7273                   IF l_return_status <> fnd_api.g_ret_sts_success THEN
7274                     RAISE fnd_api.g_exc_error;
7275                   END IF;
7276                 END IF;
7277 
7278               EXCEPTION
7279                 WHEN no_data_found THEN
7280                   null;
7281                 WHEN too_many_rows THEN
7282                   null;
7283               END;
7284 
7285             END IF;
7286 
7287           END IF; -- bom explode flag
7288         END IF;
7289 
7290 	-- changes for 3692473. moved the entire code unconditionally to  process relationships
7291         -- (get_ii_relation_tbl) before the non-source txn details updates.
7292 
7293         -- initialize the pl/sql table
7294         l_upd_ii_rltns_tbl.delete;
7295         l_cre_ii_rltns_tbl.delete;
7296 
7297         -- srramakr TSO with Equipment changes.
7298         -- For a Tangible line under a MACD order, we need to build the relationships using the
7299         -- relationship records written by Configurator. p_txn_ii_rltns_tbl contains this info.
7300         -- We will fork the code based on MACD order line flag in l_order_line_rec
7301         --
7302         IF l_order_line_rec.macd_order_line = FND_API.G_TRUE THEN
7303 	   csi_interface_pkg.build_relationship_tbl(
7304 	     p_txn_ii_rltns_tbl  =>  p_txn_ii_rltns_tbl,
7305 	     p_txn_line_dtl_tbl  =>  p_txn_line_detail_tbl,
7306 	     x_c_ii_rltns_tbl    =>  l_cre_ii_rltns_tbl,
7307 	     x_u_ii_rltns_tbl    =>  l_upd_ii_rltns_tbl,
7308 	     x_return_status     =>  x_return_status );
7309 
7310            IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
7311              raise fnd_api.g_exc_error;
7312            END IF;
7313         ELSE
7314            /*  Added p_trx_rec for ER 2581101 */
7315 
7316            csi_utl_pkg.get_ii_relation_tbl(
7317              p_txn_line_detail_tbl  => p_txn_line_detail_tbl,
7318              p_txn_ii_rltns_tbl     => p_txn_ii_rltns_tbl ,
7319              p_trx_rec              => l_trx_rec,
7320              p_order_line_rec       => l_order_line_rec,
7321              x_cre_ii_rltns_tbl     => l_cre_ii_rltns_tbl ,
7322              x_upd_ii_rltns_tbl     => l_upd_ii_rltns_tbl ,
7323              x_return_status        => x_return_status   );
7324 
7325            IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
7326              raise fnd_api.g_exc_error;
7327            END IF;
7328         END IF; -- MACD Order Check
7329 
7330         debug('upd_txn_ii_rltns_tbl.count :'||l_upd_ii_rltns_tbl.count );
7331         debug('cre_txn_ii_rltns_tbl.count :'||l_cre_ii_rltns_tbl.count );
7332 
7333         /* update instance relationship in IB */
7334         IF l_upd_ii_rltns_tbl.count > 0  THEN
7335 
7336           csi_t_gen_utility_pvt.dump_api_info(
7337             p_api_name => 'update_relationship',
7338             p_pkg_name => 'csi_ii_relationships_pub');
7339 
7340           csi_ii_relationships_pub.update_relationship(
7341             p_api_version         => 1.0,
7342             p_commit              => fnd_api.g_false,
7343             p_init_msg_list       => fnd_api.g_true,
7344             p_validation_level    => fnd_api.g_valid_level_full,
7345             p_relationship_tbl    => l_upd_ii_rltns_tbl,
7346             p_txn_rec             => l_trx_rec,
7347             x_return_status       => x_return_status,
7348             x_msg_count           => x_msg_count,
7349             x_msg_data            => x_msg_data  );
7350 
7351           IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
7352             raise fnd_api.g_exc_error;
7353           END IF;
7354           debug('update_relationship completed successfully');
7355         END IF;
7356 
7357           /* Create instance relationships in IB */
7358         IF l_cre_ii_rltns_tbl.count > 0 THEN
7359 
7360 			-- Begin fix for FP of bug 5655955 (Bug 13503707)
7361 	        -- If there is a 'COMPONENT-OF' relationship for the component item instance.
7362 	        -- It is an unexpired old relationship, need to break it.
7363 	        FOR l_ind IN l_cre_ii_rltns_tbl.FIRST .. l_cre_ii_rltns_tbl.LAST
7364 	        LOOP
7365 	          csi_process_txn_pvt.check_and_break_relation(
7366 	            p_instance_id   => l_cre_ii_rltns_tbl(l_ind).subject_id,
7367 	            p_csi_txn_rec   => l_trx_rec,
7368 	            x_return_status => l_return_status);
7369 
7370 	          IF l_return_status <> fnd_api.g_ret_sts_success THEN
7371 	            DEBUG('Error in csi_order_ship_pub.update_install_base, call to csi_process_txn_pvt.check_and_break_relation');
7372 	            RAISE fnd_api.g_exc_error;
7373 	          END IF;
7374 	        END LOOP;
7375 	        -- End fix for FP of bug 5655955 (Bug 13503707)
7376 
7377 
7378           csi_t_gen_utility_pvt.dump_api_info(
7379             p_api_name => 'create_relationship',
7380             p_pkg_name => 'csi_ii_relationships_pub');
7381 
7382           csi_ii_relationships_pub.create_relationship(
7383             p_api_version         => 1.0,
7384             p_commit              => fnd_api.g_false,
7385             p_init_msg_list       => fnd_api.g_true,
7386             p_validation_level    => fnd_api.g_valid_level_full,
7387             p_relationship_tbl    => l_cre_ii_rltns_tbl,
7388             p_txn_rec             => l_trx_rec,
7389             x_return_status       => x_return_status,
7390             x_msg_count           => x_msg_count,
7391             x_msg_data            => x_msg_data  );
7392 
7393           IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
7394             raise fnd_api.g_exc_error;
7395           END IF;
7396 
7397           debug('create_relationship completed sucessfully');
7398         END IF; -- end if for l_cre_txn_ii_rltns_tbl.count > 0
7399 
7400         -- relooping for non-source. bug 3692473
7401 
7402         FOR i IN p_txn_line_detail_tbl.FIRST..p_txn_line_detail_tbl.LAST
7403         LOOP
7404 
7405           l_tld_rec := p_txn_line_detail_tbl(i);
7406 
7407           IF l_tld_rec.source_transaction_flag = 'N' THEN
7408 
7409             debug('processing tld record # '||i||' source_transaction_flag : '||
7410                   l_tld_rec.source_transaction_flag);
7411             IF nvl(l_tld_rec.instance_id, fnd_api.g_miss_num) <> fnd_api.g_miss_num then
7412 
7413               /* check if this is a replacement instnace. If this is replacement
7414                  then pass the call_contracts flag as false . bug 2298453*/
7415               l_call_contracts := fnd_api.g_true;
7416 
7417               csi_utl_pkg.call_contracts_chk(
7418                 p_txn_line_detail_id => l_tld_rec.txn_line_detail_id,
7419                 p_txn_ii_rltns_tbl   => p_txn_ii_rltns_tbl,
7420                 x_call_contracts     => l_call_contracts,
7421                 x_return_status      => l_return_status);
7422 
7423               process_non_source(
7424                 p_txn_line_detail_rec => l_tld_rec,
7425                 p_call_contracts      => l_call_contracts,
7426                 p_trx_rec             => l_trx_rec,
7427                 x_return_status       => l_return_status);
7428 
7429               IF l_return_status <> fnd_api.g_ret_sts_success THEN
7430                 raise fnd_api.g_exc_error;
7431               END IF;
7432 
7433               /* bug 2351217 non source not stamping the status */
7434               l_tld_rec.processing_status           := 'PROCESSED';
7435               l_tld_rec.error_code                  := NULL;
7436               l_tld_rec.error_explanation           := NULL;
7437               l_tld_rec.csi_transaction_id          := p_trx_rec.transaction_id;
7438 
7439               p_txn_line_detail_tbl(i) := l_tld_rec;
7440 
7441             END IF;
7442 
7443           END IF;  -- end if for source_trx_flag = 'N'
7444         END LOOP; -- end of for  loop for p_txn_detail_tbl
7445       END IF; -- end if for p_txn_detail_tbl.count > 0
7446 
7447       IF p_source in ('SHIPMENT','FULFILLMENT') THEN
7448 
7449         IF p_txn_line_detail_tbl.count > 0 THEN
7450 
7451           debug('Updating the Transaction Details to reflect the processing status.' );
7452 
7453           csi_t_txn_details_grp.update_txn_line_dtls(
7454             p_api_version              => 1.0,
7455             p_commit                   => fnd_api.g_false,
7456             p_init_msg_list            => fnd_api.g_true,
7457             p_validation_level         => fnd_api.g_valid_level_full,
7458             p_txn_line_rec             => p_txn_line_rec,
7459             p_txn_line_detail_tbl      => p_txn_line_detail_tbl,
7460             px_txn_ii_rltns_tbl        => l_upd_txn_ii_rltns_tbl,
7461             px_txn_party_detail_tbl    => l_upd_txn_party_detail_tbl,
7462             px_txn_pty_acct_detail_tbl => l_upd_txn_pty_acct_dtl_tbl,
7463             px_txn_org_assgn_tbl       => l_upd_txn_org_assgn_tbl,
7464             px_txn_ext_attrib_vals_tbl => l_upd_txn_ext_attr_vals_tbl,
7465             x_return_status            => x_return_status,
7466             x_msg_count                => x_msg_count,
7467             x_msg_data                 => x_msg_data);
7468 
7469           IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
7470             g_api_name := 'csi_t_txn_details_grp.update_txn_line_dtls';
7471             raise fnd_api.g_exc_error;
7472           END IF;
7473 
7474         END IF;
7475       END IF; -- end of p_source
7476       -- check if the shipment/fulfillment is the last txn for final update
7477       -- and spawning ato fulfillments.
7478 
7479       proc_for_last_mtl_trx(
7480         p_source              => p_source,
7481         p_transaction_line_id => p_txn_line_rec.transaction_line_id,
7482         p_order_line_rec      => l_order_line_rec,
7483         x_return_status       => l_return_status );
7484 
7485       IF l_return_status <> fnd_api.g_ret_sts_success THEN
7486         RAISE fnd_api.g_exc_error;
7487       END IF;
7488       --
7489       -- srramakr TSO With Equipment
7490       -- Need to update the Processing status of Configurator written Txn Line and Details.
7491       -- This status will be used by Filter_Relations while processing the fulfillable line.
7492       --
7493       IF l_order_line_rec.macd_order_line = FND_API.G_TRUE THEN
7494          IF p_txn_line_detail_tbl.count > 0 THEN
7495             FOR J IN p_txn_line_detail_tbl.FIRST .. p_txn_line_detail_tbl.LAST LOOP
7496                IF p_txn_line_detail_tbl.EXISTS(J) THEN
7497                   IF p_txn_line_detail_tbl(J).source_transaction_flag = 'Y' AND
7498                      p_txn_line_detail_tbl(J).config_inst_hdr_id IS NOT NULL AND
7499                      p_txn_line_detail_tbl(J).config_inst_rev_num IS NOT NULL AND
7500                      p_txn_line_detail_tbl(J).config_inst_item_id IS NOT NULL THEN
7501                      --
7502                      -- Unlocking the Tangible Item
7503 		     l_config_tbl(1).source_application_id := 542;
7504 		     l_config_tbl(1).config_inst_hdr_id  := p_txn_line_detail_tbl(J).config_inst_hdr_id;
7505 		     l_config_tbl(1).config_inst_item_id := p_txn_line_detail_tbl(J).config_inst_item_id;
7506 		     l_config_tbl(1).config_inst_rev_num := p_txn_line_detail_tbl(J).config_inst_rev_num;
7507 		     l_config_tbl(1).source_txn_header_ref := l_order_line_rec.header_id;
7508                      l_config_tbl(1).source_txn_line_ref1 := l_order_line_rec.order_line_id;
7509                      --
7510                      debug('Calling csi_cz_int.unlock_item_instances...');
7511 		     csi_cz_int.unlock_item_instances(
7512 			      p_api_version               => 1.0,
7513 			      p_init_msg_list             => fnd_api.g_true,
7514 			      p_commit                    => fnd_api.g_false,
7515 			      p_validation_level          => fnd_api.g_valid_level_full,
7516 			      p_config_tbl                => l_config_tbl,
7517 			      x_return_status             => x_return_status,
7518 			      x_msg_count                 => x_msg_count,
7519 			      x_msg_data                  => x_msg_data);
7520 
7521 		     IF x_return_status <> fnd_api.g_ret_sts_success THEN
7522 			RAISE fnd_api.g_exc_error;
7523 		     END IF;
7524 		     --
7525                      debug('Updating CONFIGURATOR Created Transaction Details...');
7526                      --
7527                      UPDATE CSI_T_TRANSACTION_LINES
7528                      set PROCESSING_STATUS = 'PROCESSED'
7529                      where transaction_line_id in
7530                                 ( select transaction_line_id
7531                                   from CSI_T_TXN_LINE_DETAILS
7532                                   where config_inst_hdr_id = p_txn_line_detail_tbl(J).config_inst_hdr_id
7533                                   and   config_inst_rev_num = p_txn_line_detail_tbl(J).config_inst_rev_num
7534                                   and   config_inst_item_id = p_txn_line_detail_tbl(J).config_inst_item_id
7535                                  )
7536                      and  processing_status = 'SUBMIT';
7537                      --
7538                      UPDATE CSI_T_TXN_LINE_DETAILS
7539                      set PROCESSING_STATUS = 'PROCESSED'
7540                      where config_inst_hdr_id = p_txn_line_detail_tbl(J).config_inst_hdr_id
7541                      and   config_inst_rev_num = p_txn_line_detail_tbl(J).config_inst_rev_num
7542                      and   config_inst_item_id = p_txn_line_detail_tbl(J).config_inst_item_id
7543                      and   source_transaction_flag = 'Y'
7544                      and  processing_status = 'SUBMIT';
7545                   END IF;
7546                END IF;
7547             END LOOP;
7548          END IF;
7549       END IF;
7550     END IF; -- end if for p_validate_flag = 'N'
7551 
7552     -- Standard check of p_commit.
7553     IF FND_API.To_Boolean(p_commit) THEN
7554       COMMIT WORK;
7555     END IF;
7556 
7557     fnd_msg_pub.count_and_get(
7558       p_count => x_msg_count ,
7559       p_data  => x_msg_data );
7560 
7561   EXCEPTION
7562     WHEN FND_API.G_EXC_ERROR THEN
7563       ROLLBACK TO update_install_base;
7564       x_return_status := FND_API.G_RET_STS_ERROR ;
7565       fnd_msg_pub.count_and_get(
7566         p_count => x_msg_count,
7567         p_data  => x_msg_data );
7568     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7569       ROLLBACK TO update_install_base;
7570       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7571       fnd_msg_pub.count_and_get(
7572         p_count => x_msg_count,
7573         p_data  => x_msg_data );
7574     WHEN others THEN
7575 
7576       fnd_message.set_name('FND', 'FND_GENERIC_MESSAGE');
7577       fnd_message.set_token('MESSAGE', substr(sqlerrm, 1, 240));
7578       fnd_msg_pub.add;
7579 
7580       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7581 
7582       fnd_msg_pub.count_and_get(
7583         p_count => x_msg_count,
7584         p_data  => x_msg_data );
7585 
7586   END update_install_base;
7587 
7588   /*----------------------------------------------------------*/
7589   /* Procedure name:  validate_txn_tbl                        */
7590   /* Description : Procedure that validates the               */
7591   /*               txn line details and the child tables      */
7592   /*----------------------------------------------------------*/
7593 
7594   PROCEDURE validate_txn_tbl(
7595     p_txn_line_rec            IN OUT NOCOPY csi_t_datastructures_grp.txn_line_rec,
7596     p_txn_line_detail_tbl     IN OUT NOCOPY csi_t_datastructures_grp.txn_line_detail_tbl,
7597     p_txn_party_detail_tbl    IN OUT NOCOPY csi_t_datastructures_grp.txn_party_detail_tbl,
7598     p_txn_pty_acct_dtl_tbl    IN OUT NOCOPY csi_t_datastructures_grp.txn_pty_acct_detail_tbl,
7599     p_txn_ii_rltns_tbl        IN OUT NOCOPY csi_t_datastructures_grp.txn_ii_rltns_tbl,
7600     p_txn_org_assgn_tbl       IN OUT NOCOPY csi_t_datastructures_grp.txn_org_assgn_tbl,
7601     p_order_line_rec          IN OUT NOCOPY order_line_rec,
7602     p_source                  IN varchar2,
7603     x_return_status           OUT NOCOPY varchar2)
7604   IS
7605 
7606     x_txn_sub_type_rec    txn_sub_type_rec;
7607     l_found               BOOLEAN := FALSE;
7608     l_owner_count         NUMBER;
7609     l_ii_rel_id           NUMBER;
7610     l_curr_object_id      NUMBER;
7611     l_curr_subject_id     NUMBER;
7612     l_object_inst_id      NUMBER;
7613     l_trx_type_id         NUMBER;
7614     l_org_assign          BOOLEAN := FALSE;
7615     l_ind_org             NUMBER;
7616     l_party_id            NUMBER;
7617     l_pty                 NUMBER := 0;
7618     l_acct                NUMBER := 0;
7619     l_txn_party_detail_id NUMBER;
7620     l_sold_to_pty         BOOLEAN := TRUE;
7621     l_sold_to_acct        BOOLEAN := TRUE;
7622     l_acct_owner_count    NUMBER;
7623     l_sold_from_org_found varchar2(1);
7624     l_return_status       varchar2(1) := fnd_api.g_ret_sts_success;
7625 
7626     -- Partner ordering
7627     l_owner_party         NUMBER;
7628 
7629     -- Porting 11.5.9 changes for Bug 3625218
7630     l_owner_txn_pty_dtl_id NUMBER := 0;
7631     l_owner_pty_accounts  varchar2(1) := 'N';
7632     l_owner_pty_contacts  varchar2(1) := 'N';
7633 
7634   BEGIN
7635 
7636     --  Initialize API return status to success
7637     x_return_status := fnd_api.g_ret_sts_success;
7638 
7639     api_log('validate_txn_tbl');
7640 
7641     IF p_txn_line_detail_tbl.count > 0 THEN
7642       FOR i IN p_txn_line_detail_tbl.FIRST..p_txn_line_detail_tbl.LAST LOOP
7643 
7644         debug('  txn_line_detail_id      :'||p_txn_line_detail_tbl(i).txn_line_detail_id);
7645         debug('  source_transaction_flag :'||p_txn_line_detail_tbl(i).source_transaction_flag);
7646         debug('  instance_exists_flag    :'||p_txn_line_detail_tbl(i).instance_exists_flag);
7647         debug('  instance_id             :'||p_txn_line_detail_tbl(i).instance_id);
7648         debug('  quantity                :'||p_txn_line_detail_tbl(i).quantity);
7649 
7650         /* Derive the trx_sub_type from the txn details */
7651         csi_utl_pkg.get_sub_type_rec(
7652           p_sub_type_id        => p_txn_line_detail_tbl(i).sub_type_id,
7653           p_trx_type_id        => p_txn_line_rec.source_transaction_type_id,
7654           x_trx_sub_type_rec   => x_txn_sub_type_rec,
7655           x_return_status      => x_return_status) ;
7656 
7657         IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
7658           raise fnd_api.g_exc_error;
7659         END IF;
7660 
7661         /*-----------------------------------------------------------------*/
7662         /* If Org assignment with 'SOLD_FROM' relationship_type_code       */
7663         /* does not exist then 'SOLD_FROM' create an org assignments       */
7664         /*-----------------------------------------------------------------*/
7665 
7666         IF nvl(p_order_line_rec.sold_from_org_id, fnd_api.g_miss_num) <> fnd_api.g_miss_num
7667         THEN
7668           IF p_txn_org_assgn_tbl.count > 0 THEN
7669             FOR l_org in p_txn_org_assgn_tbl.first..p_txn_org_assgn_tbl.last
7670             LOOP
7671               IF p_txn_org_assgn_tbl(l_org).txn_line_detail_id = p_txn_line_detail_tbl(i).txn_line_detail_id
7672                  AND
7673                  p_txn_org_assgn_tbl(l_org).relationship_type_code='SOLD_FROM'
7674               THEN
7675                 l_org_assign := TRUE;
7676               END IF;
7677             END LOOP;
7678           END IF;
7679 
7680           IF NOT(l_org_assign) THEN
7681             /* also check if the instance reference already has a SOLD_FROM relation */
7682             IF nvl(p_txn_line_detail_tbl(i).instance_id, fnd_api.g_miss_num) <> fnd_api.g_miss_num THEN
7683               BEGIN
7684                 SELECT 'X'
7685                 INTO   l_sold_from_org_found
7686                 FROM   csi_i_org_assignments
7687                 WHERE  instance_id = p_txn_line_detail_tbl(i).instance_id
7688 		AND    operating_unit_id  = p_order_line_rec.sold_from_org_id
7689                 AND    relationship_type_code = 'SOLD_FROM'
7690 		AND    nvl (active_end_date, sysdate+1) > sysdate;
7691 
7692 
7693 
7694                 l_org_assign := TRUE;
7695 
7696               EXCEPTION
7697                 WHEN no_data_found THEN
7698                   l_org_assign := FALSE;
7699                 WHEN too_many_rows THEN
7700                   l_org_assign := TRUE;
7701               END;
7702             END IF;
7703           END IF;
7704 
7705           IF NOT(l_org_assign) THEN
7706             l_ind_org := p_txn_org_assgn_tbl.count + 1;
7707 
7708             debug('Building the SOLD_FROM org assignment.. ' );
7709 
7710             p_txn_org_assgn_tbl(l_ind_org).txn_operating_unit_id  := fnd_api.g_miss_num;
7711             p_txn_org_assgn_tbl(l_ind_org).txn_line_detail_id     := p_txn_line_detail_tbl(i).txn_line_detail_id;
7712             p_txn_org_assgn_tbl(l_ind_org).instance_ou_id         := fnd_api.g_miss_num;
7713             p_txn_org_assgn_tbl(l_ind_org).operating_unit_id      := p_order_line_rec.sold_from_org_id;
7714             p_txn_org_assgn_tbl(l_ind_org).relationship_type_code := 'SOLD_FROM';
7715             p_txn_org_assgn_tbl(l_ind_org).active_start_date      := sysdate;
7716             p_txn_org_assgn_tbl(l_ind_org).active_end_date        := p_txn_line_detail_tbl(i).active_end_date; -- fix for 4293723
7717             p_txn_org_assgn_tbl(l_ind_org).preserve_detail_flag   := 'Y';
7718             p_txn_org_assgn_tbl(l_ind_org).txn_line_details_index := i;
7719             p_txn_org_assgn_tbl(l_ind_org).object_version_number  := 1;
7720 
7721           END IF;
7722         END IF;
7723         -- Begin porting of fix for Bug 3625218 -- To get party detail for OWNER party
7724         IF p_txn_party_detail_tbl.count > 0 THEN
7725           FOR j in p_txn_party_detail_tbl.FIRST..p_txn_party_detail_tbl.LAST LOOP
7726                 IF p_txn_party_detail_tbl(j).relationship_type_code = 'OWNER'
7727                    AND
7728                    p_txn_party_detail_tbl(j).contact_flag = 'N'
7729                    AND
7730                    p_txn_party_detail_tbl(j).txn_line_detail_id = p_txn_line_detail_tbl(i).txn_line_detail_id
7731                 THEN
7732                   l_owner_txn_pty_dtl_id  := p_txn_party_detail_tbl(j).txn_party_detail_id;
7733                   exit;
7734                 END IF;
7735           END LOOP;
7736         END IF;
7737         -- End porting of fix for Bug 3625218
7738         /* sub type validation for source transactions */
7739 
7740         IF p_txn_line_detail_tbl(i).source_transaction_flag = 'Y' THEN
7741 
7742           l_owner_count := 0;
7743           l_acct_owner_count := 0;
7744 
7745           /*---------------------------------------*/
7746           /* Validate the following things if      */
7747           /*    1. there is owner party            */
7748           /*    2. there is an owner party account */
7749           /*    2. the sold_to party is passed     */
7750           /*    3. the sold_to account is passed   */
7751           /*---------------------------------------*/
7752 
7753           IF p_txn_party_detail_tbl.COUNT > 0 THEN
7754             FOR l_index IN p_txn_party_detail_tbl.FIRST .. p_txn_party_detail_tbl.LAST
7755             LOOP
7756 
7757               IF p_txn_party_detail_tbl(l_index).txn_line_detail_id =
7758                  p_txn_line_detail_tbl(i).txn_line_detail_id
7759               THEN
7760 
7761                 IF p_txn_party_detail_tbl(l_index).relationship_type_code = 'OWNER' THEN
7762                   l_owner_count := l_owner_count + 1;
7763                 END IF;
7764 
7765                 IF p_txn_party_detail_tbl(l_index).relationship_type_code = 'SOLD_TO' THEN
7766                   l_sold_to_pty := FALSE;
7767                 END IF;
7768                 -- Begin porting for 11.5.9 3625218: Check for additional contacts that exists for OWNER party
7769                 IF p_txn_party_detail_tbl(l_index).contact_party_id = l_owner_txn_pty_dtl_id
7770                    AND
7771                    p_txn_party_detail_tbl(l_index).contact_flag = 'Y'
7772                 THEN
7773                    l_owner_pty_contacts := 'Y';
7774                 END IF;
7775                 -- End porting for 11.5.9 3625218
7776                 IF p_txn_pty_acct_dtl_tbl.count > 0 THEN
7777                   FOR l_ind_acct in p_txn_pty_acct_dtl_tbl.first..p_txn_pty_acct_dtl_tbl.last
7778                   LOOP
7779                     IF p_txn_pty_acct_dtl_tbl(l_ind_acct).txn_party_detail_id =
7780                        p_txn_party_detail_tbl(l_index).txn_party_detail_id
7781                     THEN
7782                       -- Begin porting for 3625218: Check for additional accounts that exists for OWNER party
7783                       IF p_txn_party_detail_tbl(l_index).relationship_type_code = 'OWNER'
7784                                AND
7785                          p_txn_pty_acct_dtl_tbl(l_ind_acct).relationship_type_code <> 'OWNER' THEN
7786                                  l_owner_pty_accounts := 'Y';
7787                       END IF;
7788                       -- End porting for 3625218
7789                       IF p_txn_pty_acct_dtl_tbl(l_ind_acct).relationship_type_code = 'OWNER' THEN
7790                         l_acct_owner_count := l_acct_owner_count + 1;
7791                       END IF;
7792 
7793                       IF p_txn_pty_acct_dtl_tbl(l_ind_acct).relationship_type_code = 'SOLD_TO' THEN
7794                         l_sold_to_acct := FALSE;
7795                       END IF;
7796                     END IF;
7797                   END LOOP;
7798                 END IF ;  --p_txn_pty_acct_dtl_tbl.count > 0
7799               END IF;
7800             END LOOP;
7801           END IF;--p_txn_party_detail_tbl.COUNT > 0
7802 
7803           /* If multiple owner exists then raise error */
7804           IF (l_owner_count > 1) THEN
7805             fnd_message.set_name('CSI','CSI_INT_MULTIPLE_OWNER');
7806             fnd_message.set_token('TXN_LINE_DETAIL_ID', p_txn_line_detail_tbl(i).txn_line_detail_id);
7807             fnd_msg_pub.add;
7808             raise fnd_api.g_exc_error;
7809           END IF;
7810 
7811           /* If src_change_owner = Y and owner party does not exist then raise error */
7812           IF x_txn_sub_type_rec.src_change_owner = 'Y' THEN
7813             IF (l_owner_count = 0) THEN
7814               fnd_message.set_name('CSI','CSI_INT_PTY_OWNER_MISSING');
7815               fnd_message.set_token('TXN_LINE_DETAIL_ID', p_txn_line_detail_tbl(i).txn_line_detail_id);
7816               fnd_msg_pub.add;
7817               raise fnd_api.g_exc_error;
7818             END IF;
7819           END IF;
7820 
7821           IF nvl(x_txn_sub_type_rec.src_change_owner, 'N') = 'N' THEN
7822             p_order_line_rec.ship_to_contact_id := fnd_api.g_miss_num;
7823             p_order_line_rec.invoice_to_contact_id := fnd_api.g_miss_num;
7824           END IF;
7825 
7826           /* If multiple owner exists then raise error */
7827           IF (l_acct_owner_count > 1) THEN
7828             fnd_message.set_name('CSI','CSI_INT_MULTI_ACCT_OWNER');
7829             fnd_message.set_token('TXN_LINE_DETAIL_ID', p_txn_line_detail_tbl(i).txn_line_detail_id);
7830             fnd_msg_pub.add;
7831             raise fnd_api.g_exc_error;
7832           END IF;
7833 
7834           /* If src_change_owner = Y and owner party does not exist then raise error */
7835           IF x_txn_sub_type_rec.src_change_owner = 'Y' THEN
7836             IF (l_acct_owner_count = 0) THEN
7837               fnd_message.set_name('CSI','CSI_INT_ACCT_OWNER_MISSING');
7838               fnd_message.set_token('TXN_LINE_DETAIL_ID', p_txn_line_detail_tbl(i).txn_line_detail_id);
7839               fnd_msg_pub.add;
7840               raise fnd_api.g_exc_error;
7841             END IF;
7842           END IF;
7843 
7844           /*---------------------------------------------------------*/
7845           /*   Check if the OWNER party passed matches with the      */
7846           /*   Shipped OWNER party . If it does not match then update*/
7847           /*   update the party with the Shipped OWNER Party .       */
7848           /*   Also create another party rec with the 'SOLD_TO'      */
7849           /*   relationship                                          */
7850           /*---------------------------------------------------------*/
7851 
7852           l_pty  := p_txn_party_detail_tbl.count + 1;
7853           l_acct := p_txn_pty_acct_dtl_tbl.count + 1;
7854 
7855           IF p_txn_party_detail_tbl.COUNT > 0 THEN
7856             FOR l_txn_pty in p_txn_party_detail_tbl.first..p_txn_party_detail_tbl.last
7857             LOOP
7858               IF p_txn_party_detail_tbl(l_txn_pty).txn_line_detail_id =
7859                  p_txn_line_detail_tbl(i).txn_line_detail_id
7860                  AND
7861                  p_txn_party_detail_tbl(l_txn_pty).relationship_type_code = 'OWNER'
7862                  AND
7863                  l_sold_to_pty
7864               THEN
7865 
7866                 debug('Building the SOLD_TO Party record.');
7867 
7868                 BEGIN
7869                   SELECT csi_t_party_details_s.nextval
7870                   INTO   l_txn_party_detail_id
7871                   FROM   sys.dual;
7872                 EXCEPTION
7873                   WHEN others THEN
7874                     debug('Sequence csi_t_party_details_s is missing');
7875                     raise fnd_api.g_exc_error;
7876                 END;
7877 
7878                 p_txn_party_detail_tbl(l_pty) := p_txn_party_detail_tbl(l_txn_pty);
7879                 p_txn_party_detail_tbl(l_pty).instance_party_id := fnd_api.g_miss_num;
7880                 p_txn_party_detail_tbl(l_pty).txn_party_detail_id := l_txn_party_detail_id;
7881                 p_txn_party_detail_tbl(l_pty).relationship_type_code := 'SOLD_TO';
7882 
7883                 csi_utl_pkg.get_party_id(
7884                   p_cust_acct_id  => p_order_line_rec.sold_to_org_id,
7885                   x_party_id      => l_party_id,
7886                   x_return_status => l_return_status);
7887 
7888                 IF l_return_status <> fnd_api.g_ret_sts_success THEN
7889                   RAISE fnd_api.g_exc_error;
7890                 END IF;
7891 
7892                 p_txn_party_detail_tbl(l_pty).party_source_id        := l_party_id;
7893 
7894                 csi_utl_pkg.get_party_id(
7895                   p_cust_acct_id  => p_order_line_rec.end_customer_id,
7896                   x_party_id      => l_owner_party,
7897                   x_return_status => l_return_status);
7898 
7899                 IF l_return_status <> fnd_api.g_ret_sts_success THEN
7900                   RAISE fnd_api.g_exc_error;
7901                 END IF;
7902                 --Added clause to if condition for bug 5897123 (FP 5764676)
7903                 IF p_txn_party_detail_tbl(l_txn_pty).party_source_id <> l_owner_party AND x_txn_sub_type_rec.src_change_owner = 'Y' THEN
7904                   -- Begin porting for 3625218: If owner party mismatch occurs then Erroring out if
7905                   -- additional contacts/accounts exists for OWNER party.:w!
7906                   IF l_owner_pty_accounts = 'Y'
7907                     OR
7908                      l_owner_pty_contacts = 'Y'
7909                   THEN
7910                     fnd_message.set_name('CSI','CSI_OWNER_PARTY_MISMATCH');
7911                     fnd_msg_pub.add;
7912                     RAISE fnd_api.g_exc_error;
7913                   END IF;
7914                   -- End porting for 3625218
7915                   debug('Party_id on txn_dtls is not same as Shipment Party_id ');
7916                   p_txn_party_detail_tbl(l_txn_pty).party_source_id := l_owner_party ;
7917                   -- Added this for Bug 3757156
7918                   p_txn_party_detail_tbl(l_pty).party_source_id := l_party_id;
7919                   -- End fix for Bug 3757156
7920                 END IF;
7921 
7922                 IF p_txn_pty_acct_dtl_tbl.count > 0 THEN
7923                   FOR l_txn_acct in p_txn_pty_acct_dtl_tbl.first..p_txn_pty_acct_dtl_tbl.last
7924                   LOOP
7925                     IF p_txn_pty_acct_dtl_tbl(l_txn_acct).txn_party_detail_id =
7926                        p_txn_party_detail_tbl(l_txn_pty).txn_party_detail_id
7927                        AND
7928                        p_txn_pty_acct_dtl_tbl(l_txn_acct).relationship_type_code = 'OWNER'
7929                        AND
7930                        l_sold_to_acct
7931                      THEN
7932 
7933                        debug('Building the SOLD_TO Account record. ');
7934                        p_txn_pty_acct_dtl_tbl(l_acct) := p_txn_pty_acct_dtl_tbl(l_txn_acct);
7935                        p_txn_pty_acct_dtl_tbl(l_acct).ip_account_id  := fnd_api.g_miss_num;
7936                        p_txn_pty_acct_dtl_tbl(l_acct).txn_party_detail_id := l_txn_party_detail_id;
7937                        p_txn_pty_acct_dtl_tbl(l_acct).relationship_type_code := 'SOLD_TO';
7938                        p_txn_pty_acct_dtl_tbl(l_acct).account_id :=  p_order_line_rec.sold_to_org_id;
7939 
7940                        -- Begin fix for Bug 3757156
7941                        p_txn_pty_acct_dtl_tbl(l_acct).bill_to_address_id     :=
7942                          p_order_line_rec.invoice_to_org_id;
7943                        p_txn_pty_acct_dtl_tbl(l_acct).ship_to_address_id     :=
7944                          p_order_line_rec.ship_to_org_id;
7945                        -- End Fix for Bug 3757156
7946 
7947                        -- Fix for Bug 2666489
7948                        IF p_txn_pty_acct_dtl_tbl(l_txn_acct).account_id <> p_order_line_rec.end_customer_id
7949                         OR p_txn_pty_acct_dtl_tbl(l_txn_acct).bill_to_address_id <> -- added for bug 5075764
7950                            p_order_line_rec.invoice_to_org_id
7951                         OR p_txn_pty_acct_dtl_tbl(l_txn_acct).ship_to_address_id <> -- added for bug 5075764
7952                            p_order_line_rec.ship_to_org_id
7953 		       THEN
7954                            debug('Party_Account_id on txn_dtls is not same as Shipment Party_Account_id ');
7955                            p_txn_pty_acct_dtl_tbl(l_txn_acct).account_id         := p_order_line_rec.end_customer_id; -- p_order_line_rec.sold_to_org_id;
7956                            p_txn_pty_acct_dtl_tbl(l_txn_acct).bill_to_address_id := p_order_line_rec.invoice_to_org_id;
7957                            p_txn_pty_acct_dtl_tbl(l_txn_acct).ship_to_address_id := p_order_line_rec.ship_to_org_id;
7958                         END IF;
7959                         -- End of fix for Bug 2666489.
7960                     END IF;
7961                   END LOOP;
7962                 END IF; -- p_txn_pty_acct_dtl_tbl.count > 0
7963               END IF; -- end if for the match txn_party_detail_id
7964             END LOOP;
7965           END IF; -- p_txn_party_detail_tbl.COUNT > 0
7966 
7967           /* If the src_reference_reqd = "Y" then check if the instance is referenced */
7968           IF x_txn_sub_type_rec.src_reference_reqd = 'Y'
7969              AND
7970              NVL(p_txn_line_detail_tbl(i).instance_id, fnd_api.g_miss_num) = fnd_api.g_miss_num
7971           THEN
7972             fnd_message.set_name('CSI','CSI_INT_INST_REF_MISSING');
7973             fnd_message.set_token('TXN_LINE_DETAIL_ID', p_txn_line_detail_tbl(i).txn_line_detail_id);
7974             fnd_msg_pub.add;
7975             raise fnd_api.g_exc_error;
7976           END IF;
7977 
7978           /* If the src_return_reqd = "Y" then check if the return_by_date is not null */
7979           IF x_txn_sub_type_rec.src_return_reqd = 'Y'
7980              AND
7981              NVL(p_txn_line_detail_tbl(i).return_by_date, FND_API.G_MISS_DATE) = FND_API.G_MISS_DATE
7982           THEN
7983             fnd_message.set_name('CSI','CSI_INT_RET_DATE_MISSING');
7984             fnd_message.set_token('TXN_LINE_DETAIL_ID', p_txn_line_detail_tbl(i).txn_line_detail_id);
7985             fnd_msg_pub.add;
7986             raise fnd_api.g_exc_error;
7987           END IF;
7988 
7989           IF p_txn_party_detail_tbl.count > 0 THEN
7990             FOR j in p_txn_party_detail_tbl.first..p_txn_party_detail_tbl.last LOOP
7991               IF p_txn_party_detail_tbl(j).txn_line_detail_id = p_txn_line_detail_tbl(i).txn_line_detail_id THEN
7992 
7993                 IF NVL(p_txn_line_detail_tbl(i).instance_id,fnd_api.g_miss_num) <> fnd_api.g_miss_num AND
7994                    NVL(p_txn_party_detail_tbl(j).instance_party_id,fnd_api.g_miss_num) <> fnd_api.g_miss_num  THEN
7995 
7996                    /* validate if the instance_party_id is for that instance_id */
7997                    l_found := csi_utl_pkg.validate_inst_party
7998                                              (p_txn_line_detail_tbl(i).instance_id,
7999                                               p_txn_party_detail_tbl(j).instance_party_id,
8000                                               p_txn_party_detail_tbl(j).relationship_type_code);
8001 
8002                    IF NOT(l_found) THEN
8003                      fnd_message.set_name('CSI','CSI_INT_INV_INST_PTY_ID');
8004                      fnd_message.set_token('INSTANCE_ID',p_txn_line_detail_tbl(i).instance_id);
8005                      fnd_message.set_token('INSTANCE_PARTY_ID',p_txn_party_detail_tbl(j).instance_party_id);
8006                      fnd_message.set_token('RELATIONSHIP_TYPE_CODE',p_txn_party_detail_tbl(j).relationship_type_code);
8007                      fnd_msg_pub.add;
8008                      raise fnd_api.g_exc_error;
8009                    END IF;
8010                  END IF;
8011 
8012                  IF (p_txn_party_detail_tbl(j).relationship_type_code = 'OWNER') THEN
8013                   IF (x_txn_sub_type_rec.src_change_owner = 'Y') THEN
8014 
8015                     IF NVL(p_txn_line_detail_tbl(i).instance_id,fnd_api.g_miss_num) <> fnd_api.g_miss_num THEN
8016                       IF x_txn_sub_type_rec.src_chg_owner_code = 'E' THEN
8017 
8018                         /* if change_owner = Y and chg_owner_code = E then party id should
8019                         be external party id */
8020                         IF p_txn_party_detail_tbl(j).party_source_id = p_order_line_rec.internal_party_id THEN
8021                           fnd_message.set_name('CSI','CSI_INT_INV_PTY_ID');
8022                           fnd_message.set_token('PARTY_ID',p_txn_party_detail_tbl(j).party_source_id);
8023                           fnd_message.set_token('INTERNAL_PARTY_ID',p_order_line_rec.internal_party_id);
8024                           fnd_msg_pub.add;
8025                           raise fnd_api.g_exc_error;
8026                         END IF;
8027                       ELSE
8028                         /* if change_owner = N and chg_owner_code <> E then party id should
8029                         be external party id */
8030                         IF p_txn_party_detail_tbl(j).party_source_id <> p_order_line_rec.internal_party_id THEN
8031                           fnd_message.set_name('CSI','CSI_INT_INV_PTY_ID');
8032                           fnd_message.set_token('PARTY_ID',p_txn_party_detail_tbl(j).party_source_id);
8033                           fnd_message.set_token('INTERNAL_PARTY_ID',p_order_line_rec.internal_party_id);
8034                           fnd_msg_pub.add;
8035                           raise fnd_api.g_exc_error;
8036                         END IF;
8037                       END IF;
8038                     END IF;
8039                   END IF;
8040                 END IF;
8041               END IF;
8042             END LOOP;
8043           END IF;
8044         ELSIF p_txn_line_detail_tbl(i).source_transaction_flag = 'N' THEN
8045           /* sub type validation for non-source transactions */
8046 
8047           /* If the src_reference_reqd = "Y" then check if the instance is referenced */
8048           IF x_txn_sub_type_rec.nsrc_reference_reqd = 'Y'
8049              AND
8050              NVL(p_txn_line_detail_tbl(i).instance_id, fnd_api.g_miss_num) = fnd_api.g_miss_num
8051           THEN
8052             fnd_message.set_name('CSI','CSI_INT_INST_REF_MISSING');
8053             fnd_msg_pub.add;
8054             raise fnd_api.g_exc_error;
8055           END IF;
8056 
8057           /* If the src_return_reqd = "Y" then check if the return_by_date is not null */
8058 
8059           IF x_txn_sub_type_rec.nsrc_return_reqd = 'Y'
8060              AND
8061               NVL(p_txn_line_detail_tbl(i).return_by_date, FND_API.G_MISS_DATE) = FND_API.G_MISS_DATE
8062           THEN
8063             fnd_message.set_name('CSI','CSI_INT_RET_DATE_MISSING');
8064             fnd_msg_pub.add;
8065             raise fnd_api.g_exc_error;
8066           END IF;
8067 
8068           /* If  src_reference_reqd = 'Y' then the relationship should be defined in the txn_ii_rltns_tbl */
8069 
8070          IF x_txn_sub_type_rec.src_reference_reqd = 'Y'
8071             AND
8072             NOT(csi_utl_pkg.check_relation_exists(
8073               p_txn_ii_rltns_tbl,
8074               p_txn_line_detail_tbl(i).txn_line_detail_id))
8075          THEN
8076            fnd_message.set_name('CSI','CSI_INT_NSRC_REL_MISSING');
8077            fnd_message.set_token('TXN_LINE_DETAIL_ID',p_txn_line_detail_tbl(i).txn_line_detail_id );
8078            fnd_msg_pub.add;
8079            raise fnd_api.g_exc_error;
8080          END IF;
8081 
8082          IF p_txn_party_detail_tbl.count > 0 THEN
8083            FOR j in p_txn_party_detail_tbl.first..p_txn_party_detail_tbl.last LOOP
8084              IF p_txn_party_detail_tbl(j).txn_line_detail_id = p_txn_line_detail_tbl(i).txn_line_detail_id THEN
8085 
8086                IF NVL(p_txn_line_detail_tbl(i).instance_id,fnd_api.g_miss_num) <> fnd_api.g_miss_num AND
8087                   NVL(p_txn_party_detail_tbl(j).instance_party_id,fnd_api.g_miss_num) <> fnd_api.g_miss_num THEN
8088 
8089                   /* validate if the instance_party_id is for that instance_id */
8090 
8091                  l_found := csi_utl_pkg.validate_inst_party
8092                                              (p_txn_line_detail_tbl(i).instance_id,
8093                                               p_txn_party_detail_tbl(j).instance_party_id,
8094                                               p_txn_party_detail_tbl(j).relationship_type_code);
8095                  IF NOT(l_found) THEN
8096                    fnd_message.set_name('CSI','CSI_INT_INV_INST_PTY_ID');
8097                    fnd_message.set_token('INSTANCE_ID',p_txn_line_detail_tbl(i).instance_id);
8098                    fnd_message.set_token('INSTANCE_PARTY_ID',p_txn_party_detail_tbl(j).instance_party_id);
8099                    fnd_message.set_token('RELATIONSHIP_TYPE_CODE',p_txn_party_detail_tbl(j).relationship_type_code);
8100                    fnd_msg_pub.add;
8101                    raise fnd_api.g_exc_error;
8102                  END IF;
8103                END IF;
8104 
8105                IF (p_txn_party_detail_tbl(j).relationship_type_code = 'OWNER') THEN
8106                  IF (x_txn_sub_type_rec.nsrc_change_owner = 'Y') THEN
8107 
8108                    IF x_txn_sub_type_rec.nsrc_chg_owner_code = 'E' THEN
8109 
8110                      /* if change_owner = Y and chg_owner_code = E then party id should
8111                      be external party id */
8112                      IF p_txn_party_detail_tbl(j).party_source_id = p_order_line_rec.internal_party_id THEN
8113                        fnd_message.set_name('CSI','CSI_INT_INV_PTY_ID');
8114                        fnd_message.set_token('PARTY_ID',p_txn_party_detail_tbl(j).party_source_id);
8115                        fnd_message.set_token('INSTANCE_PARTY_ID',p_order_line_rec.internal_party_id);
8116                        fnd_msg_pub.add;
8117                        raise fnd_api.g_exc_error;
8118                      END IF;
8119                    ELSE
8120 
8121                      /* if change_owner = N and chg_owner_code <> E then party id should
8122                      be external party id */
8123 
8124                      IF p_txn_party_detail_tbl(j).party_source_id <> p_order_line_rec.internal_party_id THEN
8125                        fnd_message.set_name('CSI','CSI_INT_INV_PTY_ID');
8126                        fnd_message.set_token('PARTY_ID',p_txn_party_detail_tbl(j).party_source_id);
8127                        fnd_message.set_token('INSTANCE_PARTY_ID',p_order_line_rec.internal_party_id);
8128                        fnd_msg_pub.add;
8129                        raise fnd_api.g_exc_error;
8130                      END IF;
8131                    END IF;
8132                  END IF;
8133                END IF;
8134              END IF;
8135            END LOOP;
8136          END IF;
8137        END IF;
8138      END LOOP;
8139    END IF;
8140 
8141    /* validating the relationships */
8142 
8143     IF p_txn_ii_rltns_tbl.count > 0 THEN
8144      FOR j in p_txn_ii_rltns_tbl.first..p_txn_ii_rltns_tbl.last LOOP
8145 
8146        debug('Validating ii_relationships .. ' );
8147 
8148        IF NVL(p_txn_ii_rltns_tbl(j).csi_inst_relationship_id, fnd_api.g_miss_num) <> fnd_api.g_miss_num THEN
8149 
8150          csi_utl_pkg.get_inst_relation(
8151            p_ii_relationship_id => p_txn_ii_rltns_tbl(j).csi_inst_relationship_id,
8152            x_object_id          => l_curr_object_id,
8153            x_subject_id         => l_curr_subject_id,
8154            x_return_status      => x_return_status);
8155 
8156          IF NOT(x_return_status = fnd_api.g_ret_sts_success)  THEN
8157            fnd_message.set_name('CSI','CSI_INT_II_REL_MISSING');
8158            fnd_message.set_token('II_RELATIONSHIP_ID',p_txn_ii_rltns_tbl(j).csi_inst_relationship_id);
8159            fnd_msg_pub.add;
8160            raise fnd_api.g_exc_error;
8161          END IF;
8162 
8163          IF p_txn_line_detail_tbl.count > 0 THEN
8164            FOR i IN p_txn_line_detail_tbl.FIRST..p_txn_line_detail_tbl.LAST LOOP
8165              IF p_txn_line_detail_tbl(i).txn_line_detail_id = p_txn_ii_rltns_tbl(j).object_id then
8166                l_object_inst_id := p_txn_line_detail_tbl(i).instance_id;
8167                exit;
8168              END IF;
8169            END LOOP;
8170          END IF;
8171 
8172          /* Check if the object id is being updated, if so raise error */
8173 
8174          IF l_curr_object_id <> l_object_inst_id THEN
8175            fnd_message.set_name('CSI','CSI_INT_OBJ_ID_NOT_ALLOW_UPD');
8176            fnd_message.set_token('OBJECT_ID',l_object_inst_id);
8177            fnd_msg_pub.add;
8178            raise fnd_api.g_exc_error;
8179          END IF;
8180 
8181        END IF;
8182 
8183       ---Added (Start) for m-to-m enhancements
8184       -- IF p_txn_ii_rltns_tbl(j).relationship_type_code <> 'COMPONENT-OF' THEN
8185       --   debug('Only Component of relationship is allowed');
8186       -- END IF;
8187       ---Added (End) for m-to-m enhancements
8188 
8189      END LOOP;
8190      END IF; -- end of relationship validation
8191 
8192   EXCEPTION
8193     WHEN FND_API.G_EXC_ERROR THEN
8194       x_return_status := FND_API.G_RET_STS_ERROR ;
8195     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8196       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8197   END validate_txn_tbl;
8198 
8199   /*----------------------------------------------------------*/
8200   /* Procedure name:  process_option_item                     */
8201   /* Description : Procedure used to create the txn line      */
8202   /* details if the txn line dtls have to be splitted in qty  */
8203   /* ratio and txn details does not exist                     */
8204   /*----------------------------------------------------------*/
8205 
8206   PROCEDURE process_option_item(
8207     p_serial_code        IN     NUMBER,
8208     p_order_line_rec     IN     order_line_rec,
8209     p_txn_sub_type_rec   IN     txn_sub_type_rec,
8210     p_trackable_parent   IN      BOOLEAN,
8211     x_order_shipment_tbl IN OUT NOCOPY order_shipment_tbl,
8212     x_model_inst_tbl     IN OUT NOCOPY model_inst_tbl,
8213     x_trx_line_id           OUT NOCOPY NUMBER,
8214     x_return_status         OUT NOCOPY varchar2)
8215   IS
8216 
8217     l_qty_ratio     NUMBER;
8218     l_instance_id   NUMBER;
8219     l_inst_party_id NUMBER;
8220     l_ip_account_id NUMBER;
8221     l_txn_ii        NUMBER := 1;
8222     l_index         NUMBER;
8223     x_msg_count     NUMBER;
8224     x_msg_data      varchar2(2000);
8225     l_party_site_id NUMBER;
8226     l_trx_line_id   NUMBER;
8227 
8228     x_cre_txn_line_rec            csi_t_datastructures_grp.txn_line_rec ;
8229     x_txn_line_dtl_rec            csi_t_datastructures_grp.txn_line_detail_rec;
8230     x_cre_txn_line_dtls_tbl       csi_t_datastructures_grp.txn_line_detail_tbl;
8231     x_cre_txn_party_dtls_tbl      csi_t_datastructures_grp.txn_party_detail_tbl ;
8232     x_cre_txn_pty_acct_dtls_tbl   csi_t_datastructures_grp.txn_pty_acct_detail_tbl;
8233     x_cre_txn_org_assgn_tbl       csi_t_datastructures_grp.txn_org_assgn_tbl;
8234     x_cre_txn_ext_attb_vals_tbl   csi_t_datastructures_grp.txn_ext_attrib_vals_tbl;
8235     x_cre_txn_systems_tbl         csi_t_datastructures_grp.txn_systems_tbl;
8236     x_cre_txn_ii_rltns_tbl        csi_t_datastructures_grp.txn_ii_rltns_tbl;
8237 
8238     l_install_party_site_id  NUMBER;
8239     l_parent_line_qty        NUMBER := fnd_api.g_miss_num;
8240     l_return_status       varchar2(1)   := fnd_api.g_ret_sts_success;
8241 
8242   BEGIN
8243 
8244     -- Initialize API return status to success
8245     x_return_status := fnd_api.g_ret_sts_success;
8246 
8247     api_log('process_option_item');
8248 
8249     /* Initialize the pl/sql tables */
8250     x_cre_txn_line_dtls_tbl.delete;
8251     x_cre_txn_party_dtls_tbl.delete;
8252     x_cre_txn_pty_acct_dtls_tbl.delete;
8253     x_cre_txn_org_assgn_tbl.delete;
8254     x_cre_txn_ext_attb_vals_tbl.delete;
8255     x_cre_txn_systems_tbl.delete;
8256     x_cre_txn_ii_rltns_tbl.delete;
8257 
8258     -- assign values for the columns in Txn_line_rec
8259     x_cre_txn_line_rec.transaction_line_id        := fnd_api.g_miss_num;
8260     x_cre_txn_line_rec.source_transaction_id      := p_order_line_rec.order_line_id;
8261     x_cre_txn_line_rec.source_transaction_type_id := g_txn_type_id;
8262     x_cre_txn_line_rec.source_transaction_table   := 'WSH_DELIVERY_DETAILS';
8263     x_cre_txn_line_rec.processing_status          := 'IN_PROCESS';
8264     x_cre_txn_line_rec.object_version_number      := 1;
8265 
8266 --Code added for bug5194812--
8267  IF x_cre_txn_line_rec.source_transaction_type_id = 401 THEN
8268     x_cre_txn_line_rec.config_session_hdr_id  := p_order_line_rec.config_header_id;
8269     x_cre_txn_line_rec.config_session_rev_num := p_order_line_rec.config_rev_nbr;
8270     x_cre_txn_line_rec.config_session_item_id := p_order_line_rec.configuration_id;
8271     x_cre_txn_line_rec.api_caller_identity       := 'CONFIG';
8272  END IF;
8273 --Code end for bug5194812--
8274 
8275     BEGIN
8276 
8277       SELECT transaction_line_id
8278       INTO   l_trx_line_id
8279       FROM   csi_t_transaction_lines
8280       WHERE  source_transaction_id      = x_cre_txn_line_rec.source_transaction_id
8281       and    source_transaction_table   = x_cre_txn_line_rec.source_transaction_table
8282       and    source_transaction_type_id = x_cre_txn_line_rec.source_transaction_type_id;
8283 
8284       debug('  transaction_line_id : '||l_trx_line_id);
8285     EXCEPTION
8286       WHEN no_data_found THEN
8287         csi_t_txn_details_grp.create_transaction_dtls(
8288           p_api_version              => 1.0,
8289           p_commit                   => fnd_api.g_false,
8290           p_init_msg_list            => fnd_api.g_true,
8291           p_validation_level         => fnd_api.g_valid_level_none,
8292           px_txn_line_rec            => x_cre_txn_line_rec,
8293           px_txn_line_detail_tbl     => x_cre_txn_line_dtls_tbl,
8294           px_txn_party_detail_tbl    => x_cre_txn_party_dtls_tbl,
8295           px_txn_pty_acct_detail_tbl => x_cre_txn_pty_acct_dtls_tbl,
8296           px_txn_ii_rltns_tbl        => x_cre_txn_ii_rltns_tbl,
8297           px_txn_org_assgn_tbl       => x_cre_txn_org_assgn_tbl,
8298           px_txn_ext_attrib_vals_tbl => x_cre_txn_ext_attb_vals_tbl,
8299           px_txn_systems_tbl         => x_cre_txn_systems_tbl,
8300           x_return_status            => x_return_status,
8301           x_msg_count                => x_msg_count,
8302           x_msg_data                 => x_msg_data);
8303 
8304         l_trx_line_id := x_cre_txn_line_rec.transaction_line_id;
8305 
8306         IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
8307           raise fnd_api.g_exc_error;
8308         END IF;
8309 
8310     END ;
8311 
8312     x_trx_line_id := l_trx_line_id;
8313 
8314     csi_utl_pkg.get_qty_ratio(
8315       p_order_line_qty   => p_order_line_rec.ordered_quantity,
8316       p_order_item_id    => p_order_line_rec.inv_item_id,
8317       p_model_remnant_flag => p_order_line_rec.model_remnant_flag, --added for bug5096435
8318       p_link_to_line_id  => p_order_line_rec.link_to_line_id,
8319       x_qty_ratio        => l_qty_ratio ,
8320       x_return_status    => l_return_status );
8321 
8322     IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
8323       raise fnd_api.g_exc_error;
8324     END IF;
8325 
8326     rebuild_shipping_tbl(
8327       p_qty_ratio               => l_qty_ratio
8328      ,x_order_shipment_tbl      => x_order_shipment_tbl
8329      ,x_return_status           => x_return_status );
8330 
8331     IF NOT(x_return_status = fnd_api.g_ret_sts_success) THEN
8332       debug('rebuild_shipping_tbl failed ');
8333       raise fnd_api.g_exc_error;
8334     END IF;
8335 
8336 
8337 
8338     IF x_order_shipment_tbl.count > 0 THEN
8339       FOR i in x_order_shipment_tbl.first..x_order_shipment_tbl.last LOOP
8340 
8341         l_instance_id := x_order_shipment_tbl(i).instance_id ;
8342 
8343         BEGIN
8344           SELECT party_site_id
8345           INTO   l_party_site_id
8346           FROM   hz_cust_acct_sites_all c,
8347                  hz_cust_site_uses_all u
8348           WHERE  c.cust_acct_site_id = u.cust_acct_site_id
8349           AND    u.site_use_id = x_order_shipment_tbl(i).ib_current_loc_id; -- ship_to_org_id;
8350         Exception
8351           When no_data_found then
8352             fnd_message.set_name('CSI','CSI_INT_PTY_SITE_MISSING');
8353             fnd_message.set_token('LOCATION_ID', x_order_shipment_tbl(i).ib_current_loc_id); -- ship_to_org_id);
8354             fnd_msg_pub.add;
8355             debug('Party_site_id not found');
8356             raise fnd_api.g_exc_error;
8357           when too_many_rows then
8358             fnd_message.set_name('CSI','CSI_INT_MANY_PTY_SITE_FOUND');
8359             fnd_message.set_token('LOCATION_ID', x_order_shipment_tbl(i).ib_current_loc_id); -- ship_to_org_id);
8360             fnd_msg_pub.add;
8361             debug('Party_site_id not found');
8362             raise fnd_api.g_exc_error;
8363         END ;
8364 
8365         IF x_order_shipment_tbl(i).ib_install_loc is not null
8366           AND
8367            x_order_shipment_tbl(i).ib_install_loc_id is not null
8368           AND
8369            x_order_shipment_tbl(i).ib_install_loc_id <> fnd_api.g_miss_num
8370         Then
8371           BEGIN
8372             SELECT party_site_id
8373             INTO   l_install_party_site_id
8374             FROM   hz_cust_acct_sites_all c,
8375                    hz_cust_site_uses_all u
8376             WHERE  c.cust_acct_site_id = u.cust_acct_site_id
8377             AND    u.site_use_id = x_order_shipment_tbl(i).ib_install_loc_id; -- ship_to_org_id;
8378           Exception
8379            When no_data_found then
8380               fnd_message.set_name('CSI','CSI_INT_PTY_SITE_MISSING');
8381               fnd_message.set_token('LOCATION_ID', x_order_shipment_tbl(i).ib_install_loc_id); -- ship_to_org_id);
8382               fnd_msg_pub.add;
8383               debug('Party_site_id not found');
8384               raise fnd_api.g_exc_error;
8385            when too_many_rows then
8386               fnd_message.set_name('CSI','CSI_INT_MANY_PTY_SITE_FOUND');
8387               fnd_message.set_token('LOCATION_ID', x_order_shipment_tbl(i).ib_install_loc_id); -- ship_to_org_id);
8388               fnd_msg_pub.add;
8389               debug('Party_site_id not found');
8390               raise fnd_api.g_exc_error;
8391           end ;
8392         END IF;
8393 
8394 
8395         -- assign values for the columns in Txn_line_details_tbl
8396         x_cre_txn_line_dtls_tbl(i).instance_id             := l_instance_id;
8397         x_cre_txn_line_dtls_tbl(i).sub_type_id             := g_dflt_sub_type_id;
8398         x_cre_txn_line_dtls_tbl(i).instance_exists_flag    := 'Y';
8399         x_cre_txn_line_dtls_tbl(i).source_transaction_flag := 'Y';
8400         x_cre_txn_line_dtls_tbl(i).inventory_item_id       := x_order_shipment_tbl(i).inventory_item_id  ;
8401         x_cre_txn_line_dtls_tbl(i).inv_organization_id     := x_order_shipment_tbl(i).organization_id  ;
8402         x_cre_txn_line_dtls_tbl(i).inventory_revision      := x_order_shipment_tbl(i).revision  ;
8403         x_cre_txn_line_dtls_tbl(i).item_condition_id       := fnd_api.g_miss_num;
8404         x_cre_txn_line_dtls_tbl(i).instance_type_code      := fnd_api.g_miss_char;
8405         x_cre_txn_line_dtls_tbl(i).quantity                := x_order_shipment_tbl(i).shipped_quantity  ;
8406         x_cre_txn_line_dtls_tbl(i).unit_of_measure         := x_order_shipment_tbl(i).transaction_uom ;
8407         x_cre_txn_line_dtls_tbl(i).serial_number           := x_order_shipment_tbl(i).serial_number;
8408         x_cre_txn_line_dtls_tbl(i).lot_number              := x_order_shipment_tbl(i).lot_number;
8409         x_cre_txn_line_dtls_tbl(i).location_type_code      := 'HZ_PARTY_SITES';
8410         x_cre_txn_line_dtls_tbl(i).location_id             := l_party_site_id;
8411         -- Added for partner ordering
8412         x_cre_txn_line_dtls_tbl(i).install_location_type_code := x_cre_txn_line_dtls_tbl(i).location_type_code;
8413         x_cre_txn_line_dtls_tbl(i).install_location_id     := l_install_party_site_id;
8414         -- End for Partner Ordering
8415         x_cre_txn_line_dtls_tbl(i).sellable_flag           := 'Y';
8416         x_cre_txn_line_dtls_tbl(i).active_start_date       := sysdate;
8417         x_cre_txn_line_dtls_tbl(i).object_version_number   := 1  ;
8418         x_cre_txn_line_dtls_tbl(i).preserve_detail_flag    := 'Y';
8419         x_cre_txn_line_dtls_tbl(i).processing_status       := 'IN_PROCESS';
8420 
8421         IF p_order_line_rec.serial_code <> 1   Then
8422           x_cre_txn_line_dtls_tbl(i).mfg_serial_number_flag  := 'Y';
8423         ELSE
8424           x_cre_txn_line_dtls_tbl(i).mfg_serial_number_flag  := 'N';
8425         END IF;
8426 
8427         l_inst_party_id := csi_utl_pkg.get_instance_party_id(l_instance_id);
8428 
8429         IF l_inst_party_id = -1 THEN
8430           raise fnd_api.g_exc_error;
8431         END IF;
8432 
8433         -- assign values for the columns in txn_party_detail_tbl
8434         x_cre_txn_party_dtls_tbl(i).instance_party_id      := l_inst_party_id;
8435         x_cre_txn_party_dtls_tbl(i).party_source_id        := x_order_shipment_tbl(i).party_id;
8436         x_cre_txn_party_dtls_tbl(i).party_source_table     := 'HZ_PARTIES';
8437         x_cre_txn_party_dtls_tbl(i).relationship_type_code := 'OWNER';
8438         x_cre_txn_party_dtls_tbl(i).contact_flag           := 'N';
8439         x_cre_txn_party_dtls_tbl(i).active_start_date      := sysdate;
8440         x_cre_txn_party_dtls_tbl(i).preserve_detail_flag   := 'Y';
8441         x_cre_txn_party_dtls_tbl(i).object_version_number  := 1;
8442         x_cre_txn_party_dtls_tbl(i).txn_line_details_index := i;
8443 
8444         /* get ip_account_id only if instance_party_id does not exist */
8445 
8446         IF l_inst_party_id is not null THEN
8447           l_ip_account_id := csi_utl_pkg.get_ip_account_id(l_inst_party_id);
8448         END IF;
8449 
8450         /* If ip_account_id is -1 then account does not exist in IB */
8451 
8452         IF l_ip_account_id = -1 THEN
8453           l_ip_account_id := NULL;
8454         END IF;
8455 
8456         /* assign values for the columns in txn_pty_acct_dtl_tbl */
8457         x_cre_txn_pty_acct_dtls_tbl(i).ip_account_id          := l_ip_account_id;
8458         x_cre_txn_pty_acct_dtls_tbl(i).account_id             := x_order_shipment_tbl(i).end_customer_id; -- x_order_shipment_tbl(i).sold_to_org_id;
8459         x_cre_txn_pty_acct_dtls_tbl(i).bill_to_address_id     := x_order_shipment_tbl(i).invoice_to_org_id;
8460         x_cre_txn_pty_acct_dtls_tbl(i).ship_to_address_id     := x_order_shipment_tbl(i).ship_to_org_id;
8461         x_cre_txn_pty_acct_dtls_tbl(i).relationship_type_code := 'OWNER';
8462         x_cre_txn_pty_acct_dtls_tbl(i).active_start_date      := sysdate;
8463         x_cre_txn_pty_acct_dtls_tbl(i).preserve_detail_flag   := 'Y';
8464         x_cre_txn_pty_acct_dtls_tbl(i).object_version_number  := 1;
8465         x_cre_txn_pty_acct_dtls_tbl(i).txn_party_details_index := i;
8466 
8467         /*assign values for the columns in x_txn_org_assgn_tbl */
8468         IF nvl(x_order_shipment_tbl(i).sold_from_org_id,fnd_api.g_miss_num) <> fnd_api.g_miss_num
8469         THEN
8470           x_cre_txn_org_assgn_tbl(i).txn_operating_unit_id  := fnd_api.g_miss_num;
8471           x_cre_txn_org_assgn_tbl(i).txn_line_detail_id     := fnd_api.g_miss_num;
8472           x_cre_txn_org_assgn_tbl(i).instance_ou_id         := fnd_api.g_miss_num;
8473           x_cre_txn_org_assgn_tbl(i).operating_unit_id      := x_order_shipment_tbl(i).sold_from_org_id;
8474           x_cre_txn_org_assgn_tbl(i).relationship_type_code := 'SOLD_FROM';
8475           x_cre_txn_org_assgn_tbl(i).active_start_date      := sysdate;
8476           x_cre_txn_org_assgn_tbl(i).preserve_detail_flag   := 'Y';
8477           x_cre_txn_org_assgn_tbl(i).txn_line_details_index := i;
8478           x_cre_txn_org_assgn_tbl(i).object_version_number  := 1;
8479         END IF;
8480 
8481         l_index := i;
8482         x_txn_line_dtl_rec                     := x_cre_txn_line_dtls_tbl(i);
8483         x_txn_line_dtl_rec.transaction_line_id := l_trx_line_id;
8484 
8485         debug('  cre_txn_line_dtls_tbl.count     : '||x_cre_txn_line_dtls_tbl.count );
8486         debug('  cre_txn_party_dtls_tbl.count    : '||x_cre_txn_party_dtls_tbl.count );
8487         debug('  cre_txn_pty_acct_dtls_tbl.count : '||x_cre_txn_pty_acct_dtls_tbl.count);
8488         debug('  cre_txn_ii_rltns_tbl.count      : '||x_cre_txn_ii_rltns_tbl.count);
8489         debug('  cre_txn_org_assgn_tbl.count     : '||x_cre_txn_org_assgn_tbl.count);
8490         debug('  cre_txn_ext_attb_vals_tbl.count : '||x_cre_txn_ext_attb_vals_tbl.count);
8491         debug('  cre_txn_systems_tbl.count       : '||x_cre_txn_systems_tbl.count);
8492 
8493         -- call api to create the transaction line details
8494         csi_t_txn_line_dtls_pvt.create_txn_line_dtls(
8495           p_api_version               => 1.0 ,
8496           p_commit                    => fnd_api.g_false,
8497           p_init_msg_list             => fnd_api.g_true,
8498           p_validation_level          => fnd_api.g_valid_level_none,
8499           p_txn_line_dtl_index        => l_index,
8500           p_txn_line_dtl_rec          => x_txn_line_dtl_rec,
8501           px_txn_party_dtl_tbl        => x_cre_txn_party_dtls_tbl,
8502           px_txn_pty_acct_detail_tbl  => x_cre_txn_pty_acct_dtls_tbl,
8503           px_txn_ii_rltns_tbl         => x_cre_txn_ii_rltns_tbl,
8504           px_txn_org_assgn_tbl        => x_cre_txn_org_assgn_tbl,
8505           px_txn_ext_attrib_vals_tbl  => x_cre_txn_ext_attb_vals_tbl,
8506           x_return_status             => x_return_status,
8507           x_msg_count                 => x_msg_count,
8508           x_msg_data                  => x_msg_data);
8509 
8510         IF x_return_status <> fnd_api.g_ret_sts_success THEN
8511           raise fnd_api.g_exc_error;
8512         END IF;
8513 
8514         -- assign the txn details in the shipping pl/sql table
8515         x_order_shipment_tbl(i).txn_dtls_qty   := x_order_shipment_tbl(i).shipped_quantity ;
8516         x_order_shipment_tbl(i).instance_match := 'Y';
8517         x_order_shipment_tbl(i).quantity_match := 'Y';
8518 
8519       END LOOP;
8520 
8521       --4483052
8522       IF p_trackable_parent
8523       THEN
8524        debug('Splitting the created installation details in to one each if it is a parent' );
8525         csi_utl_pkg.create_txn_dtls(
8526           p_source_trx_id    =>  p_order_line_rec.order_line_id,
8527           p_source_trx_table =>  'WSH_DELIVERY_DETAILS',
8528           x_return_status    =>  l_return_status );
8529          IF l_return_status <> fnd_api.g_ret_sts_success THEN
8530            raise fnd_api.g_exc_error;
8531          END IF;
8532       END IF;
8533 
8534 
8535     END IF;
8536 
8537   EXCEPTION
8538     WHEN FND_API.G_EXC_ERROR THEN
8539       x_return_status := FND_API.G_RET_STS_ERROR ;
8540     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8541       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8542   END process_option_item;
8543 
8544   /*----------------------------------------------------------*/
8545   /* Procedure name:  rebuild_shipping_tbl                    */
8546   /* Description : Procedure that splits the shipment into    */
8547   /* quantity of one if the item type code is config          */
8548   /*----------------------------------------------------------*/
8549 
8550   PROCEDURE rebuild_shipping_tbl(
8551     p_qty_ratio               IN NUMBER
8552    ,x_order_shipment_tbl      IN OUT NOCOPY order_shipment_tbl
8553    ,x_return_status           OUT NOCOPY varchar2)
8554   IS
8555 
8556     l_split_flag     BOOLEAN := FALSE;
8557     l_instance_id    NUMBER;
8558     l_ship_quantity  NUMBER;
8559     x_msg_count      NUMBER;
8560     x_msg_data       varchar2(2000);
8561     l_count          NUMBER := 1;
8562     l_rem_qty        NUMBER ;
8563 
8564     l_order_shipment_tbl  order_shipment_tbl;
8565 
8566   BEGIN
8567 
8568     --  Initialize API return status to success
8569     x_return_status := fnd_api.g_ret_sts_success;
8570 
8571     api_log('rebuild_shipping_tbl');
8572 
8573     l_order_shipment_tbl.delete;
8574 
8575     IF x_order_shipment_tbl.count > 0 THEN
8576       FOR i in x_order_shipment_tbl.first..x_order_shipment_tbl.last LOOP
8577 
8578         IF x_order_shipment_tbl(i).shipped_quantity > p_qty_ratio THEN
8579 
8580           l_rem_qty     := x_order_shipment_tbl(i).shipped_quantity;
8581           x_order_shipment_tbl(i).shipped_quantity := p_qty_ratio;
8582           l_instance_id := x_order_shipment_tbl(i).instance_id;
8583           l_split_flag  := TRUE;
8584 
8585           WHILE l_split_flag LOOP
8586 
8587             l_rem_qty       := l_rem_qty - p_qty_ratio;
8588 
8589             IF l_rem_qty > p_qty_ratio THEN
8590               l_split_flag := TRUE;
8591               l_ship_quantity   :=  p_qty_ratio;
8592             ELSE
8593               l_split_flag := FALSE;
8594               l_ship_quantity   :=  l_rem_qty  ;
8595             END IF;
8596 
8597             l_order_shipment_tbl(l_count).line_id            :=  x_order_shipment_tbl(i).line_id ;
8598             l_order_shipment_tbl(l_count).header_id          :=  x_order_shipment_tbl(i).header_id;
8599             l_order_shipment_tbl(l_count).instance_id        :=  l_instance_id;
8600             l_order_shipment_tbl(l_count).party_id           :=  x_order_shipment_tbl(i).party_id;
8601             l_order_shipment_tbl(l_count).party_source_table :=  'HZ_PARTIES';
8602             l_order_shipment_tbl(l_count).party_account_id   :=  x_order_shipment_tbl(i).end_customer_id; -- x_order_shipment_tbl(i).sold_to_org_id;
8603             l_order_shipment_tbl(l_count).inst_obj_version_number :=  x_order_shipment_tbl(i).inst_obj_version_number;
8604             l_order_shipment_tbl(l_count).inventory_item_id  :=  x_order_shipment_tbl(i).inventory_item_id ;
8605             l_order_shipment_tbl(l_count).organization_id    :=  x_order_shipment_tbl(i).organization_id ;
8606             l_order_shipment_tbl(l_count).revision           :=  x_order_shipment_tbl(i).revision;
8607             l_order_shipment_tbl(l_count).subinventory       :=  x_order_shipment_tbl(i).subinventory ;
8608             l_order_shipment_tbl(l_count).locator_id         :=  x_order_shipment_tbl(i).locator_id ;
8609             l_order_shipment_tbl(l_count).lot_number         :=  x_order_shipment_tbl(i).lot_number ;
8610             l_order_shipment_tbl(l_count).serial_number      :=  x_order_shipment_tbl(i).serial_number  ;
8611             l_order_shipment_tbl(l_count).transaction_uom    :=  x_order_shipment_tbl(i).transaction_uom  ;
8612             l_order_shipment_tbl(l_count).order_quantity_uom :=  x_order_shipment_tbl(i).order_quantity_uom  ;
8613             l_order_shipment_tbl(l_count).invoice_to_contact_id  := x_order_shipment_tbl(i).invoice_to_contact_id ;
8614             l_order_shipment_tbl(l_count).invoice_to_org_id  :=  x_order_shipment_tbl(i).invoice_to_org_id;
8615             l_order_shipment_tbl(l_count).line_type_id       :=  x_order_shipment_tbl(i).line_type_id  ;
8616             l_order_shipment_tbl(l_count).ordered_quantity   :=  x_order_shipment_tbl(i).ordered_quantity;
8617             l_order_shipment_tbl(l_count).ord_line_shipped_qty  :=  x_order_shipment_tbl(i).ord_line_shipped_qty;
8618             l_order_shipment_tbl(l_count).ship_to_contact_id :=  x_order_shipment_tbl(i).ship_to_contact_id;
8619             l_order_shipment_tbl(l_count).ship_to_org_id     :=  x_order_shipment_tbl(i).ship_to_org_id  ;
8620             l_order_shipment_tbl(l_count).ship_from_org_id   :=  x_order_shipment_tbl(i).ship_from_org_id ;
8621             l_order_shipment_tbl(l_count).sold_to_org_id     :=  x_order_shipment_tbl(i).sold_to_org_id  ;
8622             l_order_shipment_tbl(l_count).sold_from_org_id   :=  x_order_shipment_tbl(i).sold_from_org_id ;
8623             l_order_shipment_tbl(l_count).source_line_id     :=  x_order_shipment_tbl(i).source_line_id ;
8624             l_order_shipment_tbl(l_count).transaction_type_id :=  x_order_shipment_tbl(i).transaction_type_id ;
8625             l_order_shipment_tbl(l_count).customer_id        :=  x_order_shipment_tbl(i).end_customer_id; -- x_order_shipment_tbl(i).customer_id ;
8626             l_order_shipment_tbl(l_count).transaction_date   :=  x_order_shipment_tbl(i).transaction_date ;
8627             l_order_shipment_tbl(l_count).item_type_code     :=  x_order_shipment_tbl(i).item_type_code ;
8628             l_order_shipment_tbl(l_count).cust_po_number     :=  x_order_shipment_tbl(i).cust_po_number ;
8629             l_order_shipment_tbl(l_count).ato_line_id        :=  x_order_shipment_tbl(i).ato_line_id ;
8630             l_order_shipment_tbl(l_count).top_model_line_id  :=  x_order_shipment_tbl(i).top_model_line_id ;
8631             l_order_shipment_tbl(l_count).link_to_line_id    :=  x_order_shipment_tbl(i).link_to_line_id ;
8632             l_order_shipment_tbl(l_count).shipped_quantity   :=  l_ship_quantity ;
8633             -- Added for partner ordering
8634             l_order_shipment_tbl(l_count).ib_install_loc     :=  x_order_shipment_tbl(i).ib_install_loc;
8635             l_order_shipment_tbl(l_count).ib_install_loc_id  :=  x_order_shipment_tbl(i).ib_install_loc_id;
8636             l_order_shipment_tbl(l_count).ib_current_loc     :=  x_order_shipment_tbl(i).ib_current_loc;
8637             l_order_shipment_tbl(l_count).ib_current_loc_id  :=  x_order_shipment_tbl(i).ib_current_loc_id;
8638             -- Begin fix for Bug 3435269
8639             l_order_shipment_tbl(l_count).end_customer_id    :=  x_order_shipment_tbl(i).end_customer_id;
8640             -- End fix for 3435269
8641             -- End for partner ordering
8642             l_count := l_count +1 ;
8643 
8644           END LOOP; -- end of while loop
8645 
8646         END IF;
8647       END LOOP; -- end of for loop
8648     END IF; -- end if for x_order_shipment_tbl > 0
8649 
8650     debug('l_order_shipment_tbl.count ='||l_order_shipment_tbl.count );
8651 
8652     IF l_order_shipment_tbl.count > 0 THEN
8653       FOR i in l_order_shipment_tbl.first..l_order_shipment_tbl.last LOOP
8654         x_order_shipment_tbl(x_order_shipment_tbl.count + 1) := l_order_shipment_tbl(i);
8655       END LOOP;
8656     END IF;
8657 
8658     debug('x_order_shipment_tbl.count ='||x_order_shipment_tbl.count );
8659 
8660   EXCEPTION
8661     WHEN FND_API.G_EXC_ERROR THEN
8662       x_return_status := FND_API.G_RET_STS_ERROR ;
8663     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8664       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
8665   END rebuild_shipping_tbl;
8666 
8667   /*----------------------------------------------------------*/
8668   /* Procedure name :DECODE_MESSAGE                           */
8669   /* Description    :Procedure used to decode the messages    */
8670   /*----------------------------------------------------------*/
8671 
8672   PROCEDURE decode_message(
8673     p_msg_header      IN  XNP_MESSAGE.MSG_HEADER_REC_TYPE,
8674     p_msg_text        IN  varchar2,
8675     x_return_status   OUT NOCOPY varchar2,
8676     x_error_message   OUT NOCOPY varchar2,
8677     x_mtl_trx_rec     OUT NOCOPY MTL_TXN_REC)
8678   IS
8679 
8680     l_api_name            varchar2(100) := 'csi_order_ship_pub.decode_message';
8681     l_fnd_unexpected      varchar2(1)   := fnd_api.g_ret_sts_unexp_error;
8682     l_mtl_txn_id          number;
8683     l_return_status       varchar2(1)   := fnd_api.g_ret_sts_success;
8684 
8685   BEGIN
8686 
8687      --  Initialize API return status to success
8688      x_return_status := fnd_api.g_ret_sts_success;
8689 
8690      api_log('decode_message');
8691 
8692      xnp_xml_utils.decode(P_Msg_Text, 'MTL_TRANSACTION_ID', l_mtl_txn_id);
8693 
8694      IF (l_mtl_txn_id is NULL) or
8695         (l_mtl_txn_id = fnd_api.g_miss_num) THEN
8696        raise fnd_api.g_exc_error;
8697      END IF;
8698 
8699      csi_utl_pkg.get_source_trx_dtls(
8700        p_mtl_transaction_id => l_mtl_txn_id,
8701        x_mtl_txn_rec        => X_MTL_TRX_REC,
8702        x_error_message      => x_error_message,
8703        x_return_status      => l_return_status);
8704 
8705       IF l_return_status <> fnd_api.g_ret_sts_success THEN
8706         raise fnd_api.g_exc_error;
8707       END IF;
8708 
8709   EXCEPTION
8710     WHEN fnd_api.g_exc_error THEN
8711       fnd_message.set_name('CSI','CSI_DECODE_MGS_ERROR');
8712       fnd_message.set_token('message_id',p_msg_header.message_id);
8713       fnd_message.set_token('MESSAGE_CODE',p_msg_header.message_code);
8714       fnd_msg_pub.add;
8715       x_error_message := fnd_msg_pub.get;
8716       x_return_status := l_fnd_unexpected;
8717 
8718     WHEN others THEN
8719       fnd_message.set_name('CSI','CSI_UNEXP_SQL_ERROR');
8720       fnd_message.set_token('API_NAME',l_api_name);
8721       fnd_message.set_token('SQL_ERROR',SQLERRM);
8722       fnd_msg_pub.add;
8723       x_error_message := fnd_msg_pub.get;
8724       x_return_status := l_fnd_unexpected;
8725   END decode_message;
8726 
8727   PROCEDURE oke_shipment(
8728     p_mtl_txn_id       IN  number,
8729     x_return_status    OUT NOCOPY varchar2,
8730     px_trx_error_rec   IN OUT NOCOPY csi_datastructures_pub.transaction_error_rec)
8731   IS
8732 
8733     l_oke_source_table      varchar2(30) := csi_interface_pkg.g_oke_source_table;
8734 
8735     l_inventory_item_id     number;
8736     l_organization_id       number;
8737     l_source_line_id        number;
8738     l_source_quantity       number;
8739     l_mtl_txn_tbl           csi_interface_pkg.mtl_txn_tbl;
8740     l_item_attrib_rec       csi_interface_pkg.item_attributes_rec;
8741 
8742     l_source_header_rec     csi_interface_pkg.source_header_rec;
8743     l_source_line_rec       csi_interface_pkg.source_line_rec;
8744 
8745     l_csi_txn_rec           csi_datastructures_pub.transaction_rec;
8746 
8747     l_txn_line_rec          csi_t_datastructures_grp.txn_line_rec;
8748     l_txn_line_detail_tbl   csi_t_datastructures_grp.txn_line_detail_tbl;
8749     l_txn_party_tbl         csi_t_datastructures_grp.txn_party_detail_tbl;
8750     l_txn_party_acct_tbl    csi_t_datastructures_grp.txn_pty_acct_detail_tbl;
8751     l_txn_org_assgn_tbl     csi_t_datastructures_grp.txn_org_assgn_tbl;
8752     l_txn_ii_rltns_tbl      csi_t_datastructures_grp.txn_ii_rltns_tbl;
8753     l_txn_eav_tbl           csi_t_datastructures_grp.txn_ext_attrib_vals_tbl;
8754     l_txn_systems_tbl       csi_t_datastructures_grp.txn_systems_tbl;
8755 
8756     l_pricing_attribs_tbl   csi_datastructures_pub.pricing_attribs_tbl;
8757 
8758     l_return_status         varchar2(1) := fnd_api.g_ret_sts_success;
8759     l_return_message        varchar2(2000);
8760     l_msg_count             number;
8761     l_msg_data              varchar2(2000);
8762 
8763     l_error_rec             csi_datastructures_pub.transaction_error_rec;
8764 
8765   BEGIN
8766 
8767     savepoint oke_shipment;
8768 
8769     x_return_status := fnd_api.g_ret_sts_success;
8770     l_error_rec     := px_trx_error_rec;
8771 
8772     /* builds the debug file name */
8773     csi_t_gen_utility_pvt.build_file_name (
8774       p_file_segment1 => 'csiokshp',
8775       p_file_segment2 => p_mtl_txn_id);
8776 
8777     api_log('oke_shipment');
8778 
8779     debug('  Transaction Time :'||to_char(sysdate, 'MM/DD/YY HH24:MI:SS'));
8780     debug('  Transaction Type :Sales Order Shipment');
8781     debug('  Transaction ID   :'||p_mtl_txn_id);
8782 
8783     l_error_rec.source_id     := p_mtl_txn_id;
8784 
8785     /* this routine checks if ib is active */
8786     csi_utility_grp.check_ib_active;
8787 
8788     SELECT inventory_item_id,
8789            organization_id,
8790            picking_line_id,
8791            abs(primary_quantity)
8792     INTO   l_inventory_item_id,
8793            l_organization_id,
8794            l_source_line_id,
8795            l_source_quantity
8796     FROM   mtl_material_transactions
8797     WHERE  transaction_id = p_mtl_txn_id;
8798 
8799     csi_interface_pkg.get_source_info(
8800       p_source_table         => l_oke_source_table,
8801       p_source_id            => l_source_line_id,
8802       x_source_header_rec    => l_source_header_rec,
8803       x_source_line_rec      => l_source_line_rec,
8804       x_return_status        => l_return_status);
8805 
8806     IF l_return_status <> fnd_api.g_ret_sts_success THEN
8807       RAISE fnd_api.g_exc_error;
8808     END IF;
8809 
8810     l_error_rec.source_header_ref    := l_source_header_rec.source_header_ref;
8811     l_error_rec.source_header_ref_id := l_source_header_rec.source_header_id;
8812     l_error_rec.source_line_ref      := l_source_line_rec.source_line_ref;
8813     l_error_rec.source_line_ref_id   := l_source_line_rec.source_line_id;
8814 
8815     l_source_line_rec.source_quantity  := l_source_quantity;
8816     l_source_line_rec.shipped_quantity := l_source_quantity;
8817 
8818     csi_interface_pkg.get_item_attributes(
8819        p_transaction_id     => p_mtl_txn_id, --Added for bug# 14835893
8820 	 p_inventory_item_id  => l_inventory_item_id,
8821       p_organization_id    => l_organization_id,
8822       x_item_attrib_rec    => l_item_attrib_rec,
8823       x_return_status      => l_return_status);
8824 
8825     IF l_return_status <> fnd_api.g_ret_sts_success THEN
8826       RAISE fnd_api.g_exc_error;
8827     END IF;
8828 
8829     l_error_rec.inventory_item_id        := l_inventory_item_id;
8830     l_error_rec.src_serial_num_ctrl_code := l_item_attrib_rec.serial_control_code;
8831     l_error_rec.src_lot_ctrl_code        := l_item_attrib_rec.lot_control_code;
8832     l_error_rec.src_location_ctrl_code   := l_item_attrib_rec.locator_control_code;
8833     l_error_rec.src_rev_qty_ctrl_code    := l_item_attrib_rec.revision_control_code;
8834     l_error_rec.comms_nl_trackable_flag  := l_item_attrib_rec.ib_trackable_flag;
8835 
8836     l_source_line_rec.uom_code := l_item_attrib_rec.primary_uom_code;
8837 
8838     csi_interface_pkg.get_mtl_txn_tbl(
8839       p_mtl_txn_id    => p_mtl_txn_id,
8840       x_mtl_txn_tbl   => l_mtl_txn_tbl,
8841       x_return_status => l_return_status);
8842 
8843     IF l_return_status <> fnd_api.g_ret_sts_success THEN
8844       RAISE fnd_api.g_exc_error;
8845     END IF;
8846 
8847     IF l_mtl_txn_tbl.count > 0 THEN
8848 
8849       -- create csi_txn_rec
8850 
8851       l_csi_txn_rec.transaction_id := fnd_api.g_miss_num;
8852       l_csi_txn_rec.source_header_ref_id        := l_source_header_rec.source_header_id;
8853       l_csi_txn_rec.source_header_ref           := l_source_header_rec.source_header_ref;
8854       l_csi_txn_rec.source_line_ref_id          := l_source_line_rec.source_line_id;
8855       l_csi_txn_rec.source_line_ref             := l_source_line_rec.source_line_ref;
8856       l_csi_txn_rec.inv_material_transaction_id := p_mtl_txn_id;
8857       l_csi_txn_rec.transaction_type_id         := 326;
8858       l_csi_txn_rec.transaction_date            := sysdate;
8859       l_csi_txn_rec.source_transaction_date     := l_mtl_txn_tbl(1).transaction_date;
8860       l_csi_txn_rec.transaction_quantity        := l_source_line_rec.shipped_quantity;
8861       l_csi_txn_rec.transaction_status_code     := 'PENDING';
8862 
8863       csi_transactions_pvt.create_transaction(
8864         p_api_version            => 1.0,
8865         p_commit                 => fnd_api.g_false,
8866         p_init_msg_list          => fnd_api.g_true,
8867         p_validation_level       => fnd_api.g_valid_level_full,
8868         p_success_if_exists_flag => 'Y',
8869         p_transaction_rec        => l_csi_txn_rec,
8870         x_return_status          => l_return_status,
8871         x_msg_count              => l_msg_count,
8872         x_msg_data               => l_msg_data);
8873 
8874       csi_interface_pkg.pre_process_mtl_txn_tbl(
8875         p_item_attrib_rec => l_item_attrib_rec,
8876         px_mtl_txn_tbl    => l_mtl_txn_tbl,
8877         x_return_status   => l_return_status);
8878 
8879       IF l_return_status <> fnd_api.g_ret_sts_success THEN
8880         RAISE fnd_api.g_exc_error;
8881       END IF;
8882 
8883       csi_interface_pkg.get_inventory_instances(
8884         p_item_attrib_rec => l_item_attrib_rec,
8885         px_mtl_txn_tbl    => l_mtl_txn_tbl,
8886         x_return_status   => l_return_status);
8887 
8888       IF l_return_status <> fnd_api.g_ret_sts_success THEN
8889         RAISE fnd_api.g_exc_error;
8890       END IF;
8891 
8892       -- decrement source instances
8893       csi_interface_pkg.decrement_inventory_instances(
8894         p_item_attrib_rec => l_item_attrib_rec,
8895         p_mtl_txn_tbl     => l_mtl_txn_tbl,
8896         px_txn_rec        => l_csi_txn_rec,
8897         x_return_status   => l_return_status);
8898 
8899       IF l_return_status <> fnd_api.g_ret_sts_success THEN
8900         RAISE fnd_api.g_exc_error;
8901       END IF;
8902 
8903       IF l_item_attrib_rec.serial_control_code = 6 THEN
8904         IF l_mtl_txn_tbl.count > 0 THEN
8905           FOR ret_ind IN l_mtl_txn_tbl.FIRST .. l_mtl_txn_tbl.LAST
8906           LOOP
8907             BEGIN
8908               SELECT instance_id
8909               INTO   l_mtl_txn_tbl(ret_ind).instance_id
8910               FROM   csi_item_instances
8911               WHERE  inventory_item_id = l_mtl_txn_tbl(ret_ind).inventory_item_id
8912               AND    serial_number     = l_mtl_txn_tbl(ret_ind).serial_number
8913               AND    instance_usage_code = 'RETURNED';
8914             EXCEPTION
8915               WHEN no_data_found THEN
8916                 l_mtl_txn_tbl(ret_ind).instance_id := fnd_api.g_miss_num;
8917             END;
8918           END LOOP;
8919         END IF;
8920       END IF;
8921 
8922       csi_interface_pkg.build_default_txn_detail(
8923         p_source_table         => l_oke_source_table,
8924         p_source_id            => l_source_line_id,
8925         p_source_header_rec    => l_source_header_rec,
8926         p_source_line_rec      => l_source_line_rec,
8927         p_csi_txn_rec          => l_csi_txn_rec,
8928         px_txn_line_rec        => l_txn_line_rec,--Modified to IN OUT param for bug 5194812
8929         x_txn_line_detail_tbl  => l_txn_line_detail_tbl,
8930         x_txn_party_tbl        => l_txn_party_tbl,
8931         x_txn_party_acct_tbl   => l_txn_party_acct_tbl,
8932         x_txn_org_assgn_tbl    => l_txn_org_assgn_tbl,
8933         x_pricing_attribs_tbl  => l_pricing_attribs_tbl,
8934         x_return_status        => l_return_status);
8935 
8936       IF l_return_status <> fnd_api.g_ret_sts_success THEN
8937         RAISE fnd_api.g_exc_error;
8938       END IF;
8939 
8940       -- match transaction detail with material transaction
8941       csi_interface_pkg.sync_txn_dtl_and_mtl_txn(
8942         p_mtl_txn_tbl         => l_mtl_txn_tbl,
8943         p_item_attrib_rec     => l_item_attrib_rec,
8944         px_txn_line_dtl_tbl   => l_txn_line_detail_tbl,
8945         px_txn_party_dtl_tbl  => l_txn_party_tbl,
8946         px_txn_party_acct_tbl => l_txn_party_acct_tbl,
8947         px_txn_org_assgn_tbl  => l_txn_org_assgn_tbl,
8948         px_txn_eav_tbl        => l_txn_eav_tbl,
8949         px_txn_ii_rltns_tbl   => l_txn_ii_rltns_tbl,
8950         x_return_status       => l_return_status);
8951 
8952       IF l_return_status <> fnd_api.g_ret_sts_success THEN
8953         RAISE fnd_api.g_exc_error;
8954       END IF;
8955 
8956       -- ##**##-- comment this line later.
8957       l_source_line_rec.fulfilled_date := sysdate;
8958 
8959       csi_interface_pkg.interface_ib(
8960         p_source_header_rec    => l_source_header_rec,
8961         p_source_line_rec      => l_source_line_rec,
8962         px_csi_txn_rec         => l_csi_txn_rec,
8963         px_txn_line_rec        => l_txn_line_rec,
8964         px_txn_line_dtl_tbl    => l_txn_line_detail_tbl,
8965         px_txn_party_tbl       => l_txn_party_tbl,
8966         px_txn_party_acct_tbl  => l_txn_party_acct_tbl,
8967         px_txn_org_assgn_tbl   => l_txn_org_assgn_tbl,
8968         px_txn_eav_tbl         => l_txn_eav_tbl,
8969         px_txn_ii_rltns_tbl    => l_txn_ii_rltns_tbl,
8970         px_pricing_attribs_tbl => l_pricing_attribs_tbl,
8971         x_return_status        => l_return_status,
8972         x_return_message       => l_return_message);
8973 
8974       IF l_return_status <> fnd_api.g_ret_sts_success THEN
8975         RAISE fnd_api.g_exc_error;
8976       END IF;
8977 
8978     ELSE
8979       null;
8980       -- no material transaction records. error to be set
8981     END IF;
8982 
8983   EXCEPTION
8984     WHEN fnd_api.g_exc_error THEN
8985       rollback to oke_shipment;
8986       x_return_status        := fnd_api.g_ret_sts_error;
8987       l_error_rec.error_text := csi_t_gen_utility_pvt.dump_error_stack;
8988       px_trx_error_rec       := l_error_rec;
8989       debug('Error(E) :'||l_error_rec.error_text);
8990     WHEN others THEN
8991       rollback to oke_shipment;
8992       fnd_message.set_name ('FND', 'FND_GENERIC_MESSAGE');
8993       fnd_message.set_token('MESSAGE', 'OTHERS Error :'||substr(sqlerrm, 1, 540));
8994       fnd_msg_pub.add;
8995       x_return_status        := fnd_api.g_ret_sts_error;
8996       l_error_rec.error_text := csi_t_gen_utility_pvt.dump_error_stack;
8997       px_trx_error_rec       := l_error_rec;
8998       debug('Error(O) :'||l_error_rec.error_text);
8999   END oke_shipment;
9000 
9001 END csi_order_ship_pub ;