DBA Data[Home] [Help]

PACKAGE BODY: APPS.CSI_ITEM_INSTANCE_PUB

Source


1 PACKAGE BODY CSI_ITEM_INSTANCE_PUB AS
2 /* $Header: csipiib.pls 120.24.12020000.4 2013/04/12 19:13:19 dsingire ship $ */
3 
4 -- --------------------------------------------------------
5 -- Define global variables
6 -- --------------------------------------------------------
7 
8 G_PKG_NAME CONSTANT VARCHAR2(30):= 'CSI_ITEM_INSTANCE_PUB';
9 G_FILE_NAME CONSTANT VARCHAR2(12) := 'csipiib.pls';
10 
11 /*----------------------------------------------------*/
12 /* procedure name: create_item_instance               */
13 /* description :   procedure used to                  */
14 /*                 create item instances              */
15 /*----------------------------------------------------*/
16 
17 PROCEDURE create_item_instance
18  (
19      p_api_version           IN     NUMBER
20     ,p_commit                IN     VARCHAR2
21     ,p_init_msg_list         IN     VARCHAR2
22     ,p_validation_level      IN     NUMBER
23     ,p_instance_rec          IN OUT NOCOPY csi_datastructures_pub.instance_rec
24     ,p_ext_attrib_values_tbl IN OUT NOCOPY csi_datastructures_pub.extend_attrib_values_tbl
25     ,p_party_tbl             IN OUT NOCOPY csi_datastructures_pub.party_tbl
26     ,p_account_tbl           IN OUT NOCOPY csi_datastructures_pub.party_account_tbl
27     ,p_pricing_attrib_tbl    IN OUT NOCOPY csi_datastructures_pub.pricing_attribs_tbl
28     ,p_org_assignments_tbl   IN OUT NOCOPY csi_datastructures_pub.organization_units_tbl
29     ,p_asset_assignment_tbl  IN OUT NOCOPY csi_datastructures_pub.instance_asset_tbl
30     ,p_txn_rec               IN OUT NOCOPY csi_datastructures_pub.transaction_rec
31     ,x_return_status         OUT    NOCOPY VARCHAR2
32     ,x_msg_count             OUT    NOCOPY NUMBER
33     ,x_msg_data              OUT    NOCOPY VARCHAR2
34  )
35 IS
36     l_api_name                      CONSTANT VARCHAR2(30)   := 'CREATE_ITEM_INSTANCE';
37     l_api_version                   CONSTANT NUMBER         := 1.0;
38     l_debug_level                   NUMBER;
39     l_flag                          VARCHAR2(1)             := 'N';
40     l_msg_data                      VARCHAR2(2000);
41     l_msg_index                     NUMBER;
42     l_msg_count                     NUMBER;
43     l_transaction_type              VARCHAR2(10);
44     l_old_oks_cp_rec                oks_ibint_pub.cp_rec_type;
45     l_new_oks_cp_rec                oks_ibint_pub.cp_rec_type;
46     l_contracts_status              VARCHAR2(3);
47     l_account_id                    NUMBER;
48     l_internal_party_id             NUMBER;
49     l_party_id                      NUMBER;
50     l_version_label                 VARCHAR2(30);
51     l_version_label_rec             csi_datastructures_pub.version_label_rec;
52     l_item_attribute_tbl            csi_item_instance_pvt.item_attribute_tbl;
53     l_location_tbl                  csi_item_instance_pvt.location_tbl;
54     l_generic_id_tbl                csi_item_instance_pvt.generic_id_tbl;
55     l_lookup_tbl                    csi_item_instance_pvt.lookup_tbl;
56     l_ins_count_rec                 csi_item_instance_pvt.ins_count_rec;
57     l_asset_lookup_tbl              csi_asset_pvt.lookup_tbl;
58     l_asset_count_rec               csi_asset_pvt.asset_count_rec;
59     l_asset_id_tbl                  csi_asset_pvt.asset_id_tbl;
60     l_asset_loc_tbl                 csi_asset_pvt.asset_loc_tbl;
61     l_party                         NUMBER;
62     l_contact                       NUMBER;
63     l_account                       NUMBER;
64     l_party_tbl                     csi_datastructures_pub.party_tbl;
65     lc_party_tbl                    csi_datastructures_pub.party_tbl;
66     l_temp_party_tbl                csi_datastructures_pub.party_tbl;
67     l_account_tbl                   csi_datastructures_pub.party_account_tbl;
68     l_temp_account_tbl              csi_datastructures_pub.party_account_tbl;
69     l_count                         NUMBER;
70     l_t_party_tbl                   csi_datastructures_pub.party_tbl;
71     -- following were added for att enhancements.
72     l_component_ins_type            VARCHAR2(1):=NULL ;
73     l_config_hdr_id                 NUMBER;
74     l_config_rev_nbr                NUMBER;
75     l_config_key                    csi_utility_grp.config_instance_key;
76     l_config_valid_status           VARCHAR2(10);
77     l_return_message                VARCHAR2(100);
78     -- Begin Add Code for Siebel Genesis Project
79     l_raise_bes_event               VARCHAR2(1) := nvl(fnd_profile.value('CSI_RAISE_BES_CUST_OWNED_INSTANCES'),'N');
80     l_link_to_line_id               NUMBER;
81     l_item_type_code                VARCHAR2(30);
82 	l_create_event_called           VARCHAR2(1) := 'N'; --Added for bug 7156553, base bug 6996605
83     -- End Add Code for Siebel Genesis Project
84     -- end addition
85     CURSOR instance_csr (p_ins_id IN NUMBER) IS
86       SELECT  *
87       FROM    csi_item_instances
88       WHERE   instance_id = p_ins_id;
89     l_instance_csr                  instance_csr%ROWTYPE;
90 
91 
92     CURSOR config_ins_key (p_config_hdr_id NUMBER, p_config_rev_nbr NUMBER) is
93       SELECT instance_hdr_id
94             ,config_item_id
95             ,has_failures
96       FROM   cz_config_details_v d
97             ,cz_config_hdrs_v h
98       WHERE d.instance_hdr_id = h.config_hdr_id
99       AND   d.instance_rev_nbr = h.config_rev_nbr
100       AND   d.component_instance_type = 'I'
101       AND   d.config_hdr_id = p_config_hdr_id
102       AND   d.config_rev_nbr = p_config_rev_nbr;
103 
104     CURSOR instance_key (p_config_inst_hdr_id NUMBER, p_config_inst_item_id NUMBER)is
105       SELECT instance_id
106             ,object_version_number
107             ,config_valid_status
108       FROM   csi_item_instances
109       WHERE  config_inst_hdr_id=p_config_inst_hdr_id
110       AND    config_inst_item_id=p_config_inst_item_id;
111 
112     CURSOR new_config_ins_key (p_config_ins_hdr_id NUMBER, p_config_ins_rev_nbr NUMBER) is
113       SELECT has_failures
114             ,config_status
115       FROM   cz_config_details_v d
116             ,cz_config_hdrs h
117       WHERE d.instance_hdr_id = p_config_ins_hdr_id
118       AND   d.instance_rev_nbr = p_config_ins_rev_nbr
119      -- AND   d.component_instance_type = 'I'
120       AND   d.config_hdr_id = h.config_hdr_id
121       AND   d.config_rev_nbr = h.config_rev_nbr;
122 
123     CURSOR new_instance_key (p_config_inst_hdr_id NUMBER, p_config_inst_item_id NUMBER)is
124       SELECT instance_id
125             ,object_version_number
126             ,config_valid_status
127       FROM   csi_item_instances
128       WHERE  config_inst_hdr_id=p_config_inst_hdr_id
129       AND    config_inst_item_id=p_config_inst_item_id;
130 
131       l_config_instance_rec           csi_datastructures_pub.instance_rec ;
132       l_config_temp_rec               csi_datastructures_pub.instance_rec ;
133       l_batch_hdr_id                  NUMBER;
134       l_batch_rev_nbr                 NUMBER;
135       l_instance_id_lst               csi_datastructures_pub.id_tbl;
136       l_config_ins_rec                csi_datastructures_pub.instance_rec;
137       l_config_tmp_rec                csi_datastructures_pub.instance_rec;
138       --
139       px_oks_txn_inst_tbl             OKS_IBINT_PUB.TXN_INSTANCE_TBL;
140       px_child_inst_tbl               csi_item_instance_grp.child_inst_tbl;
141       l_batch_id                      NUMBER;
142       l_batch_type                    VARCHAR2(50);
143 BEGIN
144 
145      -- Standard Start of API savepoint
146      SAVEPOINT  create_item_instance;
147 
148      -- Check for freeze_flag in csi_install_parameters is set to 'Y'
149 
150      csi_utility_grp.check_ib_active;
151 
152      -- Standard call to check for call compatibility.
153      IF NOT FND_API.Compatible_API_Call (l_api_version,
154                                          p_api_version,
155                                          l_api_name   ,
156                                          G_PKG_NAME   )
157          THEN
158                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
159          END IF;
160 
161      -- Initialize message list if p_init_msg_list is set to TRUE.
162      IF FND_API.to_Boolean( p_init_msg_list ) THEN
163                 FND_MSG_PUB.initialize;
164      END IF;
165 
166      --  Initialize API return status to success
167      x_return_status := FND_API.G_RET_STS_SUCCESS;
168 
169      -- Check the profile option debug_level for debug message reporting
170      l_debug_level:=fnd_profile.value('CSI_DEBUG_LEVEL');
171 
172      -- If debug_level = 1 then dump the procedure name
173      IF (l_debug_level > 0) THEN
174         csi_gen_utility_pvt.put_line( 'create_item_instance');
175      END IF;
176 
177      -- If the debug level = 2 then dump all the parameters values.
178      IF (l_debug_level > 1) THEN
179 
180             csi_gen_utility_pvt.put_line( 'create_item_instance'     ||
181                                           p_api_version         ||'-'||
182                                           p_commit              ||'-'||
183                                           p_init_msg_list       ||'-'||
184                                           p_validation_level );
185                csi_gen_utility_pvt.dump_instance_rec(p_instance_rec);
186                csi_gen_utility_pvt.dump_party_tbl(p_party_tbl);
187                csi_gen_utility_pvt.dump_txn_rec(p_txn_rec);
188                csi_gen_utility_pvt.dump_organization_unit_tbl(p_org_assignments_tbl);
189                csi_gen_utility_pvt.dump_pricing_attribs_tbl(p_pricing_attrib_tbl);
190                csi_gen_utility_pvt.dump_party_account_tbl(p_account_tbl);
191                csi_gen_utility_pvt.dump_ext_attrib_values_tbl(p_ext_attrib_values_tbl);
192      END IF;
193    /***** srramakr commented for bug # 3304439
194      -- Check for the profile option and enable trace
195              l_flag:=CSI_GEN_UTILITY_PVT.enable_trace(l_trace_flag => l_flag);
196      -- End enable trace
197    ****/
198     -- Start API body
199     --
200     -- Calling Pre Customer User Hook
201   BEGIN
202 
203     IF JTF_USR_HKS.Ok_to_execute(G_PKG_NAME, l_api_name, 'B', 'C' ) THEN
204        csi_gen_utility_pvt.put_line('Calling CSI_ITEM_INSTANCE_CUHK.Create_Item_Instance_Pre ..');
205 	  CSI_ITEM_INSTANCE_CUHK.Create_Item_Instance_Pre
206 		(
207 		p_api_version              => 1.0
208 	    ,p_commit                   => fnd_api.g_false
209 	    ,p_init_msg_list            => fnd_api.g_false
210 	    ,p_validation_level         => fnd_api.g_valid_level_full
211 	    ,p_instance_rec             => p_instance_rec
212 	    ,p_ext_attrib_values_tbl    => p_ext_attrib_values_tbl
213 	    ,p_party_tbl                => p_party_tbl
214 	    ,p_account_tbl              => p_account_tbl
215 	    ,p_pricing_attrib_tbl       => p_pricing_attrib_tbl
216 	    ,p_org_assignments_tbl      => p_org_assignments_tbl
217 	    ,p_asset_assignment_tbl     => p_asset_assignment_tbl
218 	    ,p_txn_rec                  => p_txn_rec
219 	    ,x_return_status            => x_return_status
220 	    ,x_msg_count                => x_msg_count
221 	    ,x_msg_data                 => x_msg_data
222 	 );
223 	  --
224 	  IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
225           l_msg_index := 1;
226           l_msg_count := x_msg_count;
227           WHILE l_msg_count > 0 LOOP
228                   x_msg_data := FND_MSG_PUB.GET
229                               (  l_msg_index,
230                                  FND_API.G_FALSE );
231               csi_gen_utility_pvt.put_line('ERROR FROM CSI_ITEM_INSTANCE_CUHK.Create_Item_Instance_Pre API ');
232               csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
233               l_msg_index := l_msg_index + 1;
234               l_msg_count := l_msg_count - 1;
235           END LOOP;
236          RAISE FND_API.G_EXC_ERROR;
237 	   END IF;
238 	  --
239     END IF;
240   EXCEPTION
241     WHEN OTHERS THEN
242        csi_gen_utility_pvt.put_line('ERROR FROM JTF_USR_HKS.Ok_to_execute API Create Pre Customer');
243        RAISE FND_API.G_EXC_ERROR;
244   END;
245     --
246     -- Calling Pre Vertical User Hook
247   BEGIN
248 
249     IF JTF_USR_HKS.Ok_to_execute( G_PKG_NAME, l_api_name, 'B', 'V' )  THEN
250        csi_gen_utility_pvt.put_line('Calling CSI_ITEM_INSTANCE_VUHK.Create_Item_Instance_Pre ..');
251 	  CSI_ITEM_INSTANCE_VUHK.Create_Item_Instance_Pre
252 		(
253 		p_api_version              => 1.0
254 	    ,p_commit                   => fnd_api.g_false
255 	    ,p_init_msg_list            => fnd_api.g_false
256 	    ,p_validation_level         => fnd_api.g_valid_level_full
257 	    ,p_instance_rec             => p_instance_rec
258 	    ,p_ext_attrib_values_tbl    => p_ext_attrib_values_tbl
259 	    ,p_party_tbl                => p_party_tbl
260 	    ,p_account_tbl              => p_account_tbl
261 	    ,p_pricing_attrib_tbl       => p_pricing_attrib_tbl
262 	    ,p_org_assignments_tbl      => p_org_assignments_tbl
263 	    ,p_asset_assignment_tbl     => p_asset_assignment_tbl
264 	    ,p_txn_rec                  => p_txn_rec
265 	    ,x_return_status            => x_return_status
266 	    ,x_msg_count                => x_msg_count
267 	    ,x_msg_data                 => x_msg_data
268 	 );
269 	  --
270 	  IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
271           l_msg_index := 1;
272           l_msg_count := x_msg_count;
273           WHILE l_msg_count > 0 LOOP
274                   x_msg_data := FND_MSG_PUB.GET
275                               (  l_msg_index,
276                                  FND_API.G_FALSE );
277               csi_gen_utility_pvt.put_line('ERROR FROM CSI_ITEM_INSTANCE_VUHK.Create_Item_Instance_Pre API ');
278               csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
279               l_msg_index := l_msg_index + 1;
280               l_msg_count := l_msg_count - 1;
281           END LOOP;
282          RAISE FND_API.G_EXC_ERROR;
283        END IF;
284 	  --
285     END IF;
286 
287   EXCEPTION
288     WHEN OTHERS THEN
289        csi_gen_utility_pvt.put_line('ERROR FROM JTF_USR_HKS.Ok_to_execute API Create Pre Vertical');
290        RAISE FND_API.G_EXC_ERROR;
291   END;
292 
293     -- End of PRE User Hooks
294     --
295     -- Create an item instance after validating all the instance attributes.
296     -- API also validates that exactly one owner is being created for the
297     -- item instance
298     -- The following code has been added to assign call_contracts
299     -- false so that contracts call can be made only once.
300     IF (p_account_tbl.count > 0) THEN
301       FOR tab_row IN p_account_tbl.FIRST .. p_account_tbl.LAST
302          LOOP
303             IF p_account_tbl.EXISTS(tab_row) THEN
304                p_account_tbl(tab_row).call_contracts := fnd_api.g_false;
305                p_account_tbl(tab_row).vld_organization_id := p_instance_rec.vld_organization_id;
306             END IF;
307          END LOOP;
308     END IF;
309 
310 
311      csi_item_instance_pvt.create_item_instance
312        (
313         p_api_version        => p_api_version
314        ,p_commit             => fnd_api.g_false
315        ,p_init_msg_list      => p_init_msg_list
316        ,p_validation_level   => p_validation_level
317        ,p_instance_rec       => p_instance_rec
318        ,p_txn_rec            => p_txn_rec
319        ,p_party_tbl          => p_party_tbl
320        ,p_asset_tbl          => p_asset_assignment_tbl
321        ,x_return_status      => x_return_status
322        ,x_msg_count          => x_msg_count
323        ,x_msg_data           => x_msg_data
324        ,p_item_attribute_tbl => l_item_attribute_tbl
325        ,p_location_tbl       => l_location_tbl
326        ,p_generic_id_tbl     => l_generic_id_tbl
327        ,p_lookup_tbl         => l_lookup_tbl
328        ,p_ins_count_rec      => l_ins_count_rec
329        );
330        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
331                   l_msg_index := 1;
332           l_msg_count := x_msg_count;
333          WHILE l_msg_count > 0 LOOP
334                   x_msg_data := FND_MSG_PUB.GET
335                           (  l_msg_index,
336                         FND_API.G_FALSE );
337           csi_gen_utility_pvt.put_line( ' Error from CSI_ITEM_INSTANCE_PVT.. ');
338           csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
339               l_msg_index := l_msg_index + 1;
340               l_msg_count := l_msg_count - 1;
341              END LOOP;
342          RAISE FND_API.G_EXC_ERROR;
343        END IF;
344      --
345      -- Calling Post Customer User Hook
346    BEGIN
347 
348      IF JTF_USR_HKS.Ok_to_execute(G_PKG_NAME, l_api_name, 'A', 'C' ) THEN
349        csi_gen_utility_pvt.put_line('Calling  CSI_ITEM_INSTANCE_CUHK.Create_Item_Instance_Post ..');
350         CSI_ITEM_INSTANCE_CUHK.Create_Item_Instance_Post
351            (
352 	      p_api_version              => 1.0
353 	     ,p_commit                   => fnd_api.g_false
354 	     ,p_init_msg_list            => fnd_api.g_false
355 	     ,p_validation_level         => fnd_api.g_valid_level_full
356 	     ,p_instance_rec             => p_instance_rec
357 	     ,p_ext_attrib_values_tbl    => p_ext_attrib_values_tbl
358 	     ,p_party_tbl                => p_party_tbl
359 	     ,p_account_tbl              => p_account_tbl
360 	     ,p_pricing_attrib_tbl       => p_pricing_attrib_tbl
361 	     ,p_org_assignments_tbl      => p_org_assignments_tbl
362 	     ,p_asset_assignment_tbl     => p_asset_assignment_tbl
363 	     ,p_txn_rec                  => p_txn_rec
364 	     ,x_return_status            => x_return_status
365 	     ,x_msg_count                => x_msg_count
366 	     ,x_msg_data                 => x_msg_data
367 	  );
368         --
369         IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
370           l_msg_index := 1;
371           l_msg_count := x_msg_count;
372           WHILE l_msg_count > 0 LOOP
373                   x_msg_data := FND_MSG_PUB.GET
374                               (  l_msg_index,
375                                  FND_API.G_FALSE );
376               csi_gen_utility_pvt.put_line('ERROR FROM CSI_ITEM_INSTANCE_CUHK.Create_Item_Instance_Post API ');
377               csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
378               l_msg_index := l_msg_index + 1;
379               l_msg_count := l_msg_count - 1;
380           END LOOP;
381          RAISE FND_API.G_EXC_ERROR;
382        END IF;
383         --
384     END IF;
385   EXCEPTION
386     WHEN OTHERS THEN
387        csi_gen_utility_pvt.put_line('ERROR FROM JTF_USR_HKS.Ok_to_execute API Create Post Customer');
388        RAISE FND_API.G_EXC_ERROR;
389   END;
390 
391 
392 -- Call the create_party_relationship API to create instance-to-party
393 -- relationships.
394 
395   IF (p_party_tbl.count > 0) THEN
396       FOR tab_row IN p_party_tbl.FIRST .. p_party_tbl.LAST
397          LOOP
398             IF p_party_tbl.EXISTS(tab_row) THEN
399                p_party_tbl(tab_row).instance_id := p_instance_rec.instance_id;
400                IF ((p_party_tbl(tab_row).active_start_date IS NULL) OR
401                    (p_party_tbl(tab_row).active_start_date = FND_API.G_MISS_DATE)) THEN
402                     p_party_tbl(tab_row).active_start_date := p_instance_rec.active_start_date;
403                END IF;
404             END IF;
405          END LOOP;
406          -- Added by sguthiva for att enhancements
407          -- The following code has been written to allow the flexibility
408          -- to pass a party and its contacts.
409          l_party:=1;
410          l_contact:=1;
411          l_account:=1;
412          -- The following code is written for parties
413         FOR tab_row IN p_party_tbl.FIRST .. p_party_tbl.LAST
414         LOOP
415            IF nvl(p_party_tbl(tab_row).contact_flag,'N') <> 'Y'
416            THEN
417               l_party_tbl(l_party):=p_party_tbl(tab_row);
418               IF p_account_tbl.count > 0
419               THEN
420                  FOR acct_row IN p_account_tbl.FIRST..p_account_tbl.last
421                  LOOP
422                     IF p_account_tbl(acct_row).parent_tbl_index =tab_row
423                     THEN
424                        l_account_tbl(l_account):=p_account_tbl(acct_row);
425                        l_account_tbl(l_account).parent_tbl_index:=l_party;
426                        l_account:=l_account+1;
427                     END IF;
428                  END LOOP;
429               END IF;
430 
431               l_t_party_tbl(l_party).parent_tbl_index:=tab_row;
432               l_t_party_tbl(l_party).contact_parent_tbl_index:=l_party;
433               l_party:=l_party+1;
434            END IF;
435         END LOOP;
436         -- The following code is written for contacts
437         FOR cont_row IN p_party_tbl.FIRST .. p_party_tbl.LAST
438         LOOP
439            IF p_party_tbl(cont_row).contact_flag='Y'
440            AND (p_party_tbl(cont_row).contact_ip_id IS NULL
441            OR   p_party_tbl(cont_row).contact_ip_id=fnd_api.g_miss_num
442                )
443            AND (p_party_tbl(cont_row).contact_parent_tbl_index IS NOT NULL
444            AND  p_party_tbl(cont_row).contact_parent_tbl_index <> fnd_api.g_miss_num
445                )
446            THEN
447               lc_party_tbl(l_contact):=p_party_tbl(cont_row);
448               FOR k in l_t_party_tbl.first..l_t_party_tbl.last
449               LOOP
450                  IF l_t_party_tbl(k).parent_tbl_index=lc_party_tbl(l_contact).contact_parent_tbl_index
451                  THEN
452                     lc_party_tbl(l_contact).contact_parent_tbl_index:=l_t_party_tbl(k).contact_parent_tbl_index;
453                  END IF;
454               END LOOP;
455               l_contact:=l_contact+1;
456            END IF;
457         END LOOP;
458 
459         p_account_tbl:=l_temp_account_tbl;
460         p_account_tbl:=l_account_tbl;
461         l_count:=l_party_tbl.count;
462         p_party_tbl:=l_temp_party_tbl;
463         p_party_tbl:=l_party_tbl;
464 
465         IF lc_party_tbl.COUNT > 0
466         THEN
467            FOR cont_row IN lc_party_tbl.FIRST..lc_party_tbl.LAST
468            LOOP
469               l_count:=l_count+1;
470               p_party_tbl(l_count):=lc_party_tbl(cont_row);
471            END LOOP;
472         END IF;
473 
474         csi_party_relationships_pub.create_inst_party_relationship
475         (
476           p_api_version         => p_api_version
477          ,p_commit              => fnd_api.g_false
478          ,p_init_msg_list       => p_init_msg_list
479          ,p_validation_level    => p_validation_level
480          ,p_party_tbl           => p_party_tbl
481          ,p_party_account_tbl   => p_account_tbl
482          ,p_txn_rec             => p_txn_rec
483 	 ,p_oks_txn_inst_tbl    => px_oks_txn_inst_tbl
484          ,x_return_status       => x_return_status
485          ,x_msg_count           => x_msg_count
486          ,x_msg_data            => x_msg_data
487         );
488         IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
489                   l_msg_index := 1;
490           l_msg_count := x_msg_count;
491          WHILE l_msg_count > 0 LOOP
492                   x_msg_data := FND_MSG_PUB.GET
493                           (  l_msg_index,
494                         FND_API.G_FALSE );
495           csi_gen_utility_pvt.put_line( ' Error from CSI_PARTY_RELATIONSHIPS_PUB.. ');
496           csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
497               l_msg_index := l_msg_index + 1;
498               l_msg_count := l_msg_count - 1;
499              END LOOP;
500          RAISE FND_API.G_EXC_ERROR;
501        END IF;
502   END IF;
503 
504   -- Call create_organization_unit API to create instance-
505   -- to-organization units associations
506 
507   IF (p_org_assignments_tbl.count > 0) THEN
508       FOR tab_row IN p_org_assignments_tbl.FIRST .. p_org_assignments_tbl.LAST
509          LOOP
510              IF p_org_assignments_tbl.EXISTS(tab_row) THEN
511                 p_org_assignments_tbl(tab_row).instance_id := p_instance_rec.instance_id;
512              END IF;
513           END LOOP;
514 
515         csi_organization_unit_pub.create_organization_unit
516         (
517           p_api_version      => p_api_version
518          ,p_commit           => fnd_api.g_false
519          ,p_init_msg_list    => p_init_msg_list
520          ,p_validation_level => p_validation_level
521          ,p_org_unit_tbl     => p_org_assignments_tbl
522          ,p_txn_rec          => p_txn_rec
523          ,x_return_status    => x_return_status
524          ,x_msg_count        => x_msg_count
525          ,x_msg_data         => x_msg_data
526         );
527 
528        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
529                   l_msg_index := 1;
530           l_msg_count := x_msg_count;
531          WHILE l_msg_count > 0 LOOP
532                   x_msg_data := FND_MSG_PUB.GET
533                           (  l_msg_index,
534                                          FND_API.G_FALSE        );
535           csi_gen_utility_pvt.put_line( ' Error from CSI_ORGANIZATION_UNIT_PUB..');
536           csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
537               l_msg_index := l_msg_index + 1;
538               l_msg_count := l_msg_count - 1;
539              END LOOP;
540          RAISE FND_API.G_EXC_ERROR;
541        END IF;
542 
543   END IF;
544 
545 -- Call create_pricing_attribs to associate any pricing attributes
546 -- to the item instance
547 
548   IF (p_pricing_attrib_tbl.count > 0) THEN
549       FOR tab_row IN p_pricing_attrib_tbl.FIRST .. p_pricing_attrib_tbl.LAST
550          LOOP
551              IF p_pricing_attrib_tbl.EXISTS(tab_row) THEN
552                 p_pricing_attrib_tbl(tab_row).instance_id := p_instance_rec.instance_id;
553              END IF;
554          END LOOP;
555 
556         csi_pricing_attribs_pub.create_pricing_attribs
557         (
558           p_api_version         => p_api_version
559          ,p_commit              => fnd_api.g_false
560          ,p_init_msg_list       => p_init_msg_list
561          ,p_validation_level    => p_validation_level
562          ,p_pricing_attribs_tbl => p_pricing_attrib_tbl
563          ,p_txn_rec             => p_txn_rec
564          ,x_return_status       => x_return_status
565          ,x_msg_count           => x_msg_count
566          ,x_msg_data            => x_msg_data
567         );
568 
569        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
570                   l_msg_index := 1;
571           l_msg_count := x_msg_count;
572          WHILE l_msg_count > 0 LOOP
573                   x_msg_data := FND_MSG_PUB.GET
574                           (  l_msg_index,
575                                          FND_API.G_FALSE        );
576 
577           csi_gen_utility_pvt.put_line( ' Error from CSI_PRICING_ATTRIBS_PUB..');
578           csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
579               l_msg_index := l_msg_index + 1;
580               l_msg_count := l_msg_count - 1;
581              END LOOP;
582          RAISE FND_API.G_EXC_ERROR;
583        END IF;
584   END IF;
585 
586 -- Call create_extended_attribs to associate any extended attributes
587 -- to the item instance
588 
589   IF (p_ext_attrib_values_tbl.count > 0) THEN
590       FOR tab_row IN p_ext_attrib_values_tbl.FIRST .. p_ext_attrib_values_tbl.LAST
591          LOOP
592             IF p_ext_attrib_values_tbl.EXISTS(tab_row) THEN
593                p_ext_attrib_values_tbl(tab_row).instance_id := p_instance_rec.instance_id;
594             END IF;
595          END LOOP;
596 
597        create_extended_attrib_values
598         (
599           p_api_version         => p_api_version
600          ,p_commit              => fnd_api.g_false
601          ,p_init_msg_list       => p_init_msg_list
602          ,p_validation_level    => p_validation_level
603          ,p_ext_attrib_tbl      => p_ext_attrib_values_tbl
604          ,p_txn_rec             => p_txn_rec
605          ,x_return_status       => x_return_status
606          ,x_msg_count           => x_msg_count
607          ,x_msg_data            => x_msg_data
608         );
609 
610        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
611                   l_msg_index := 1;
612           l_msg_count := x_msg_count;
613          WHILE l_msg_count > 0 LOOP
614                   x_msg_data := FND_MSG_PUB.GET
615                           (  l_msg_index,
616                                          FND_API.G_FALSE        );
617 
618           csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
619               l_msg_index := l_msg_index + 1;
620               l_msg_count := l_msg_count - 1;
621              END LOOP;
622          RAISE FND_API.G_EXC_ERROR;
623        END IF;
624   END IF;
625 
626 -- Call create_asset_assignments to associate any assets associated
627 -- to the item instance
628 
629   IF (p_asset_assignment_tbl.count > 0) THEN
630      FOR tab_row IN p_asset_assignment_tbl.FIRST .. p_asset_assignment_tbl.LAST
631      LOOP
632        IF p_asset_assignment_tbl.EXISTS(tab_row) THEN
633           p_asset_assignment_tbl(tab_row).instance_id := p_instance_rec.instance_id;
634          csi_asset_pvt.create_instance_asset
635          (
636            p_api_version         => p_api_version
637           ,p_commit              => fnd_api.g_false
638           ,p_init_msg_list       => p_init_msg_list
639           ,p_instance_asset_rec  => p_asset_assignment_tbl(tab_row)
640           ,p_txn_rec             => p_txn_rec
641           ,x_return_status       => x_return_status
642           ,x_msg_count           => x_msg_count
643           ,x_msg_data            => x_msg_data
644           ,p_lookup_tbl          => l_asset_lookup_tbl
645           ,p_asset_count_rec     => l_asset_count_rec
646           ,p_asset_id_tbl        => l_asset_id_tbl
647           ,p_asset_loc_tbl       => l_asset_loc_tbl
648           );
649 
650          IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
651                   l_msg_index := 1;
652                   l_msg_count := x_msg_count;
653             WHILE l_msg_count > 0 LOOP
654                   x_msg_data := FND_MSG_PUB.GET
655                           (  l_msg_index,
656                                          FND_API.G_FALSE        );
657                   csi_gen_utility_pvt.put_line( ' Error from CSI_ASSET_PVT..');
658                   csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
659                   l_msg_index := l_msg_index + 1;
660                   l_msg_count := l_msg_count - 1;
661              END LOOP;
662          RAISE FND_API.G_EXC_ERROR;
663        END IF;
664      END IF;
665    END LOOP;
666   END IF;
667 
668 -- Added by rtalluri for Bug 2256588 on 03/26/02
669 
670 -- If version label is null, then we need read the the default value from the profile option
671 
672    IF ((p_instance_rec.version_label IS NULL) OR
673        (p_instance_rec.version_label = FND_API.G_MISS_CHAR)) THEN
674         l_version_label := FND_PROFILE.VALUE('CSI_DEFAULT_VERSION_LABEL');
675            IF  l_version_label IS NULL THEN
676                FND_MESSAGE.SET_NAME('CSI','CSI_API_INVALID_VERSION_LABEL');
677                FND_MSG_PUB.ADD;
678                RAISE FND_API.G_EXC_ERROR;
679            ELSE
680                p_instance_rec.version_label := l_version_label;
681            END IF;
682    END IF;
683 
684 --Calling the Create Version Label API to associate a version label for an instance created
685 
686         l_version_label_rec.instance_id           := p_instance_rec.instance_id;
687         l_version_label_rec.version_label         := p_instance_rec.version_label;
688         l_version_label_rec.date_time_stamp       := SYSDATE;
689         l_version_label_rec.active_start_date     := p_instance_rec.active_start_date;
690 
691 
692         csi_item_instance_pvt.create_version_label
693           (
694            p_api_version         => p_api_version
695           ,p_commit              => fnd_api.g_false
696           ,p_init_msg_list       => p_init_msg_list
697           ,p_validation_level    => p_validation_level
698           ,p_version_label_rec   => l_version_label_rec
699           ,p_txn_rec             => p_txn_rec
700           ,x_return_status       => x_return_status
701           ,x_msg_count           => x_msg_count
702           ,x_msg_data            => x_msg_data
703            );
704 
705          IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
706               csi_gen_utility_pvt.put_line( ' Error from CSI_ITEM_INSTANCE_PVT.CREATE_VERSION_LABEL..');
707               csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
708               FND_MESSAGE.SET_NAME('CSI','CSI_FAILED_TO_CREATE_VERSION');
709               FND_MESSAGE.SET_TOKEN('API_ERROR', 'CREATE_VERSION_LABEL');
710               FND_MSG_PUB.Add;
711               RAISE FND_API.G_EXC_ERROR;
712          END IF;
713 
714    -- End of addition by rtalluri for Bug 2256588 on 03/26/02
715 
716    -- Here we call update_version_time to update date_time_stamp of
717    -- version labels created with this transaction_id to sysdate.
718    -- Commented the following code, which is causing the prformance
719    -- issue. -- bug 3558082 (reported in 11.5.8)
720    -- Calling Contracts
721    -- Added on 02-OCT-01
722    -- Added by sk for fixing bug 2245976
723    IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
724       csi_gen_utility_pvt.populate_install_param_rec;
725    END IF;
726    --
727    l_internal_party_id := csi_datastructures_pub.g_install_param_rec.internal_party_id;
728    --
729    BEGIN
730       SELECT cip.party_id
731       INTO   l_party_id
732       FROM   csi_i_parties cip
733       WHERE  cip.instance_id = p_instance_rec.instance_id
734       AND    cip.relationship_type_code = 'OWNER';
735    EXCEPTION
736      WHEN OTHERS THEN
737       l_party_id := NULL;
738    END;
739 
740    IF l_party_id IS NOT NULL AND
741       l_internal_party_id IS NOT NULL AND
742       l_party_id <> l_internal_party_id
743    THEN
744    -- End addition by sk for fixing bug 2245976
745       l_transaction_type:= 'NEW';
746 
747       -- Added for DEBUG purposes for bug 9028424
748       IF p_instance_rec.call_contracts = fnd_api.g_false THEN
749         csi_gen_utility_pvt.put_line('call_contracts(1): '||p_instance_rec.call_contracts);
750       ELSIF p_instance_rec.call_contracts = 'N' THEN
751         csi_gen_utility_pvt.put_line('call_contracts(2): '||p_instance_rec.call_contracts);
752       ELSE
753         csi_gen_utility_pvt.put_line('call_contracts(3): '||p_instance_rec.call_contracts);
754       END IF;
755       -- End DEBUG section
756 
757       IF (p_instance_rec.call_contracts <> fnd_api.g_false AND p_instance_rec.call_contracts <> 'N') --added by radha on 04/04/02 --added by HYONLEE on 10/30/09
758       THEN
759                    csi_item_instance_pvt.Call_to_Contracts(
760                               p_transaction_type   =>   l_transaction_type
761                              ,p_instance_id        =>   p_instance_rec.instance_id
762                              ,p_new_instance_id    =>   NULL
763                              ,p_vld_org_id         =>   p_instance_rec.vld_organization_id
764                              ,p_quantity           =>   NULL
765                              ,p_party_account_id1  =>   NULL
766                              ,p_party_account_id2  =>   NULL
767                              ,p_transaction_date   =>   p_txn_rec.transaction_date -- Refer 3483763
768                              ,p_source_transaction_date   =>   p_txn_rec.source_transaction_date -- Added jpwilson
769                              ,p_oks_txn_inst_tbl   =>   px_oks_txn_inst_tbl
770                              ,x_return_status      =>   x_return_status
771                              ,x_msg_count          =>   x_msg_count
772                              ,x_msg_data           =>   x_msg_data
773                               );
774 
775            IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS)
776            THEN
777                   l_msg_index := 1;
778                   l_msg_count := x_msg_count;
779               WHILE l_msg_count > 0 LOOP
780                 x_msg_data := FND_MSG_PUB.GET
781                               (  l_msg_index,
782                                  FND_API.G_FALSE
783                                );
784                 csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
785                     l_msg_index := l_msg_index + 1;
786                     l_msg_count := l_msg_count - 1;
787               END LOOP;
788                 RAISE FND_API.G_EXC_ERROR;
789            END IF;
790       END IF;
791    END IF;
792 
793 -- Start att enhancements
794   IF p_instance_rec.call_batch_validation<>fnd_api.g_false
795   THEN
796     IF   (p_instance_rec.config_inst_hdr_id IS NOT NULL AND p_instance_rec.config_inst_hdr_id <> fnd_api.g_miss_num)
797         AND (p_instance_rec.config_inst_item_id IS NOT NULL AND p_instance_rec.config_inst_item_id <> fnd_api.g_miss_num)
798         AND (p_instance_rec.config_inst_rev_num IS NOT NULL AND p_instance_rec.config_inst_rev_num <> fnd_api.g_miss_num)
799     THEN
800 
801           IF NOT csi_item_instance_vld_pvt.is_unique_config_key ( p_config_inst_hdr_id  => p_instance_rec.config_inst_hdr_id
802                                                                  ,p_config_inst_item_id => p_instance_rec.config_inst_item_id
803                                                                  ,p_instance_id         => p_instance_rec.instance_id
804                                                                  ,p_validation_mode     => 'CREATE'
805                                                                  )
806           THEN
807            FND_MESSAGE.SET_NAME('CSI','CSI_CONFIG_KEY_EXISTS');
808            FND_MSG_PUB.Add;
809            RAISE fnd_api.g_exc_error;
810           END IF;
811 
812           BEGIN
813               SELECT component_instance_type
814                     ,config_hdr_id
815                     ,config_rev_nbr
816               INTO   l_component_ins_type
817                     ,l_config_hdr_id
818                     ,l_config_rev_nbr
819               FROM   cz_config_items_v
820               WHERE  instance_hdr_id = p_instance_rec.config_inst_hdr_id
821               AND    instance_rev_nbr = p_instance_rec.config_inst_rev_num
822               AND    config_item_id = p_instance_rec.config_inst_item_id;
823           EXCEPTION
824               WHEN OTHERS THEN
825                FND_MESSAGE.SET_NAME('CSI','CSI_CONFIG_NOT_IN_CZ');
826                FND_MESSAGE.SET_TOKEN('INSTANCE_HDR_ID',p_instance_rec.config_inst_hdr_id);
827                FND_MESSAGE.SET_TOKEN('INSTANCE_REV_NBR',p_instance_rec.config_inst_rev_num);
828                FND_MESSAGE.SET_TOKEN('CONFIG_ITEM_ID',p_instance_rec.config_inst_item_id);
829                FND_MSG_PUB.Add;
830                RAISE FND_API.G_EXC_ERROR;
831           END;
832 
833           IF l_component_ins_type='I'
834           THEN
835             Csi_item_instance_vld_pvt.Call_batch_validate
836               ( p_instance_rec    => p_instance_rec
837                ,p_config_hdr_id   => l_config_hdr_id
838                ,p_config_rev_nbr  => l_config_rev_nbr
839                ,x_config_hdr_id   => l_batch_hdr_id
840                ,x_config_rev_nbr  => l_batch_rev_nbr
841                ,x_return_status   => x_return_status);
842 
843             IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
844                FND_MESSAGE.SET_NAME('CSI','CSI_CONFIG_ERROR');
845                FND_MSG_PUB.Add;
846                csi_gen_utility_pvt.put_line('Call to batch validation has errored ');
847                RAISE FND_API.G_EXC_ERROR;
848             END IF;
849          END IF;
850      ELSIF (  (p_instance_rec.config_inst_hdr_id IS NULL OR p_instance_rec.config_inst_hdr_id = fnd_api.g_miss_num)
851             AND (p_instance_rec.config_inst_item_id IS NOT NULL AND p_instance_rec.config_inst_item_id <> fnd_api.g_miss_num))
852           OR (  (p_instance_rec.config_inst_hdr_id IS NOT NULL AND p_instance_rec.config_inst_hdr_id <> fnd_api.g_miss_num)
853             AND (p_instance_rec.config_inst_item_id IS NULL OR p_instance_rec.config_inst_item_id = fnd_api.g_miss_num))
854           OR (  (p_instance_rec.config_inst_hdr_id IS NOT NULL AND p_instance_rec.config_inst_hdr_id <> fnd_api.g_miss_num)
855             AND (p_instance_rec.config_inst_item_id IS NOT NULL AND p_instance_rec.config_inst_item_id <> fnd_api.g_miss_num)
856             AND (p_instance_rec.config_inst_rev_num IS NULL OR p_instance_rec.config_inst_rev_num = fnd_api.g_miss_num))
857     THEN
858 		       FND_MESSAGE.SET_NAME('CSI','CSI_INVALID_CONFIG_COMB');
859 		       FND_MSG_PUB.Add;
860 		       RAISE fnd_api.g_exc_error;
861     END IF;
862   END IF;
863   -- End att enhancements
864   -- Adding new changes for bug 3799694
865     IF   (p_instance_rec.config_inst_hdr_id IS NOT NULL AND p_instance_rec.config_inst_hdr_id <> fnd_api.g_miss_num)
866         AND (p_instance_rec.config_inst_item_id IS NOT NULL AND p_instance_rec.config_inst_item_id <> fnd_api.g_miss_num)
867         AND (p_instance_rec.config_inst_rev_num IS NOT NULL AND p_instance_rec.config_inst_rev_num <> fnd_api.g_miss_num)
868     THEN
869              FOR l_config_ins_key IN new_config_ins_key(p_instance_rec.config_inst_hdr_id,p_instance_rec.config_inst_rev_num)
870               LOOP
871 
872                 IF l_config_ins_key.has_failures ='1'
873                  OR nvl(l_config_ins_key.config_status,'0') <> '2'
874                 THEN
875                    FOR l_instance_key IN new_instance_key(p_instance_rec.config_inst_hdr_id,p_instance_rec.config_inst_item_id)
876                    LOOP
877 
878                          l_config_instance_rec:=l_config_temp_rec;
879                       IF (l_instance_key.instance_id IS NOT NULL AND
880                           l_instance_key.instance_id <> fnd_api.g_miss_num) AND
881                          ( l_instance_key.config_valid_status IS NULL OR
882                           (l_instance_key.config_valid_status IS NOT NULL AND
883                            l_instance_key.config_valid_status <> '1'))
884                       THEN
885                          l_config_instance_rec.instance_id:=l_instance_key.instance_id;
886                          l_config_instance_rec.object_version_number:=l_instance_key.object_version_number;
887                          l_config_instance_rec.config_valid_status:='1'; --INVALID
888                          csi_item_instance_pvt.update_item_instance
889                           (
890                             p_api_version        => p_api_version
891                            ,p_commit             => fnd_api.g_false
892                            ,p_init_msg_list      => p_init_msg_list
893                            ,p_validation_level   => p_validation_level
894                            ,p_instance_rec       => l_config_instance_rec
895                            ,p_txn_rec            => p_txn_rec
896                            ,x_instance_id_lst    => l_instance_id_lst
897                            ,x_return_status      => x_return_status
898                            ,x_msg_count          => x_msg_count
899                            ,x_msg_data           => x_msg_data
900                            ,p_item_attribute_tbl => l_item_attribute_tbl
901                            ,p_location_tbl       => l_location_tbl
902                            ,p_generic_id_tbl     => l_generic_id_tbl
903                            ,p_lookup_tbl         => l_lookup_tbl
904                            ,p_ins_count_rec      => l_ins_count_rec
905                            ,p_oks_txn_inst_tbl   => px_oks_txn_inst_tbl
906                            ,p_child_inst_tbl     => px_child_inst_tbl
907                          );
908 
909                       IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
910                            l_msg_index := 1;
911                            l_msg_count := x_msg_count;
912                         WHILE l_msg_count > 0 LOOP
913                              x_msg_data := FND_MSG_PUB.GET
914                                     (  l_msg_index,
915                                        FND_API.G_FALSE        );
916                              csi_gen_utility_pvt.put_line( 'Error from UPDATE_ITEM_INSTANCE_PVT..');
917                              csi_gen_utility_pvt.put_line( 'while updating config status');
918                              csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
919                               l_msg_index := l_msg_index + 1;
920                               l_msg_count := l_msg_count - 1;
921                         END LOOP;
922                            RAISE FND_API.G_EXC_ERROR;
923                       END IF;
924                       END IF;
925                    END LOOP;
926                 ELSE
927                    FOR l_instance_key IN new_instance_key(p_instance_rec.config_inst_hdr_id,p_instance_rec.config_inst_item_id)
928                    LOOP
929 
930                          l_config_instance_rec:=l_config_temp_rec;
931                       IF (l_instance_key.instance_id IS NOT NULL AND
932                           l_instance_key.instance_id <> fnd_api.g_miss_num) AND
933                          ( l_instance_key.config_valid_status IS NULL OR
934                           (l_instance_key.config_valid_status IS NOT NULL AND
935                            l_instance_key.config_valid_status <> '0'))
936                       THEN
937                          l_config_instance_rec.instance_id:=l_instance_key.instance_id;
938                          l_config_instance_rec.object_version_number:=l_instance_key.object_version_number;
939                          l_config_instance_rec.config_valid_status:='0'; --VALID
940                          csi_item_instance_pvt.update_item_instance
941                           (
942                             p_api_version        => p_api_version
943                            ,p_commit             => fnd_api.g_false
944                            ,p_init_msg_list      => p_init_msg_list
945                            ,p_validation_level   => p_validation_level
946                            ,p_instance_rec       => l_config_instance_rec
947                            ,p_txn_rec            => p_txn_rec
948                            ,x_instance_id_lst    => l_instance_id_lst
949                            ,x_return_status      => x_return_status
950                            ,x_msg_count          => x_msg_count
951                            ,x_msg_data           => x_msg_data
952                            ,p_item_attribute_tbl => l_item_attribute_tbl
953                            ,p_location_tbl       => l_location_tbl
954                            ,p_generic_id_tbl     => l_generic_id_tbl
955                            ,p_lookup_tbl         => l_lookup_tbl
956                            ,p_ins_count_rec      => l_ins_count_rec
957                            ,p_oks_txn_inst_tbl   => px_oks_txn_inst_tbl
958                            ,p_child_inst_tbl     => px_child_inst_tbl
959                          );
960 
961                       IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
962                          l_msg_index := 1;
963                          l_msg_count := x_msg_count;
964                          WHILE l_msg_count > 0 LOOP
965                             x_msg_data := FND_MSG_PUB.GET
966                                     (  l_msg_index,
967                                        FND_API.G_FALSE        );
968                             csi_gen_utility_pvt.put_line( 'Error from UPDATE_ITEM_INSTANCE_PVT..');
969                             csi_gen_utility_pvt.put_line( 'while updating config status');
970                             csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
971                             l_msg_index := l_msg_index + 1;
972                             l_msg_count := l_msg_count - 1;
973                          END LOOP;
974                          RAISE FND_API.G_EXC_ERROR;
975                       END IF;
976                    END IF;
977                 END LOOP;
978              END IF;
979           END LOOP;
980     END IF;
981     --
982     IF px_oks_txn_inst_tbl.count > 0 THEN
983        csi_gen_utility_pvt.dump_oks_txn_inst_tbl(px_oks_txn_inst_tbl);
984        csi_gen_utility_pvt.put_line('Calling OKS Core API...');
985        --
986        IF p_txn_rec.transaction_type_id = 3 THEN
987           l_batch_id := p_txn_rec.source_header_ref_id;
988           l_batch_type := p_txn_rec.source_group_ref;
989        ELSE
990           l_batch_id := NULL;
991           l_batch_type := NULL;
992        END IF;
993        --
994        UPDATE CSI_TRANSACTIONS
995        set contracts_invoked = 'Y'
996        where transaction_id = p_txn_rec.transaction_id;
997        --
998        OKS_IBINT_PUB.IB_interface
999           (
1000             P_Api_Version           =>  1.0,
1001             P_init_msg_list         =>  p_init_msg_list,
1002             P_single_txn_date_flag  =>  'Y',
1003             P_Batch_type            =>  l_batch_type,
1004             P_Batch_ID              =>  l_batch_id,
1005             P_OKS_Txn_Inst_tbl      =>  px_oks_txn_inst_tbl,
1006             x_return_status         =>  x_return_status,
1007             x_msg_count             =>  x_msg_count,
1008             x_msg_data              =>  x_msg_data
1009          );
1010        --
1011        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
1012 	  l_msg_index := 1;
1013 	  l_msg_count := x_msg_count;
1014 	  WHILE l_msg_count > 0 LOOP
1015 	     x_msg_data := FND_MSG_PUB.GET
1016 		     (  l_msg_index,
1017 			FND_API.G_FALSE        );
1018 	     csi_gen_utility_pvt.put_line( 'Error from OKS_IBINT_PUB.IB_interface..');
1019 	     csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
1020 	     l_msg_index := l_msg_index + 1;
1021 	     l_msg_count := l_msg_count - 1;
1022 	  END LOOP;
1023 	  RAISE FND_API.G_EXC_ERROR;
1024        END IF;
1025     END IF;
1026 
1027 -- End addition of changes for bug 3799694
1028         -- End of API body
1029 
1030     -- Begin Add Code for Siebel Genesis Project - Call the Business Event
1031     IF l_raise_bes_event = 'Y' THEN
1032        csi_gen_utility_pvt.put_line('l_internal_party_Id = '||to_char(l_internal_party_id));
1033        csi_gen_utility_pvt.put_line('l_party_Id = '||to_char(l_party_id));
1034        csi_gen_utility_pvt.put_line('fnd_api.g_miss_num = '||to_char(fnd_api.g_miss_num));
1035        IF l_internal_party_id <> l_party_id THEN
1036           --Added for bug 7156553, base bug 6996605, when completing a sales order for a serial controlled item instance
1037           --which has been previously shipped out through a sales order then returned through a RMA,
1038           --the event to raise should be the update event, since a create event has already been raised
1039           --for the same instance_id
1040           BEGIN
1041             SELECT 'Y'
1042             INTO l_create_event_called
1043             FROM csi_item_instances_h
1044             WHERE instance_id = p_instance_rec.instance_id
1045             AND new_accounting_class_code = 'CUST_PROD'
1046             AND transaction_id <> p_txn_rec.transaction_id
1047             AND ROWNUM = 1;
1048           EXCEPTION
1049             WHEN OTHERS THEN
1050              l_create_event_called := 'N';
1051           END;
1052           IF (l_debug_level > 1) THEN
1053             csi_gen_utility_pvt.put_line('l_create_event_called  : '||l_create_event_called);
1054           END IF;
1055           IF l_create_event_called = 'N' THEN
1056              csi_gen_utility_pvt.put_line('Firing the Create Instance Evnet');
1057              CSI_BUSINESS_EVENT_PVT.CREATE_INSTANCE_EVENT
1058                 (p_api_version          => p_api_version
1059                  ,p_commit              => fnd_api.g_false
1060                  ,p_init_msg_list       => p_init_msg_list
1061                  ,p_validation_level    => p_validation_level
1062                  ,p_instance_id         => p_instance_rec.instance_id
1063                  ,p_subject_instance_id => null
1064                  ,x_return_status       => x_return_status
1065                  ,x_msg_count           => x_msg_count
1066                  ,x_msg_data            => x_msg_data
1067                 );
1068 
1069              IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
1070                 l_msg_index := 1;
1071                 l_msg_count := x_msg_count;
1072 
1073                 WHILE l_msg_count > 0 LOOP
1074                    x_msg_data := FND_MSG_PUB.GET(l_msg_index, FND_API.G_FALSE);
1075                    csi_gen_utility_pvt.put_line('Error from CSI_BUSINESS_EVENT.CREATE_INSTANCE_EVENT');
1076                    csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
1077                    l_msg_index := l_msg_index + 1;
1078                    l_msg_count := l_msg_count - 1;
1079                 END LOOP;
1080                 RAISE FND_API.G_EXC_ERROR;
1081              END IF;
1082           ELSE
1083            csi_gen_utility_pvt.put_line('Firing the Update Instance Event');
1084            CSI_BUSINESS_EVENT_PVT.UPDATE_INSTANCE_EVENT
1085               ( p_api_version         => p_api_version
1086                ,p_commit              => fnd_api.g_false
1087                ,p_init_msg_list       => p_init_msg_list
1088                ,p_validation_level    => p_validation_level
1089                ,p_instance_id         => p_instance_rec.instance_id
1090                ,p_subject_instance_id => null
1091                ,x_return_status       => x_return_status
1092                ,x_msg_count           => x_msg_count
1093                ,x_msg_data            => x_msg_data
1094               );
1095 
1096            IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
1097               l_msg_index := 1;
1098               l_msg_count := x_msg_count;
1099 
1100               WHILE l_msg_count > 0 LOOP
1101                  x_msg_data := FND_MSG_PUB.GET(l_msg_index, FND_API.G_FALSE);
1102                  csi_gen_utility_pvt.put_line(' Error from CSI_BUSINESS_EVENT.UPDATE_INSTANCE_EVENT');
1103                  csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
1104                  l_msg_index := l_msg_index + 1;
1105                  l_msg_count := l_msg_count - 1;
1106               END LOOP;
1107               RAISE FND_API.G_EXC_ERROR;
1108            END IF;
1109          END IF;
1110        END IF;
1111     END IF;
1112     -- End Add Code for Siebel Genesis Project - Call the Business Event
1113 
1114         -- Standard check of p_commit.
1115         IF FND_API.To_Boolean( p_commit ) THEN
1116            COMMIT WORK;
1117         END IF;
1118    /***** srramakr commented for bug # 3304439
1119     -- Check for the profile option and disable the trace
1120     IF (l_flag = 'Y') THEN
1121          dbms_session.set_sql_trace(FALSE);
1122     END IF;
1123     ****/
1124         -- End disable trace
1125 
1126         -- Standard call to get message count and if count is  get message info.
1127         FND_MSG_PUB.Count_And_Get
1128                (p_encoded => FND_API.G_FALSE,   --Added for bug 7657438
1129                 p_count => x_msg_count ,
1130                 p_data  => x_msg_data
1131                 );
1132 
1133 
1134 -- Calling Post Vertical User Hook
1135   BEGIN
1136 
1137      IF JTF_USR_HKS.Ok_to_execute( G_PKG_NAME, l_api_name, 'A', 'V' )  THEN
1138        csi_gen_utility_pvt.put_line('Calling  CSI_ITEM_INSTANCE_VUHK.Create_Item_Instance_Post ..');
1139         CSI_ITEM_INSTANCE_VUHK.Create_Item_Instance_Post
1140            (
1141 	      p_api_version              => 1.0
1142 	     ,p_commit                   => fnd_api.g_false
1143 	     ,p_init_msg_list            => fnd_api.g_false
1144 	     ,p_validation_level         => fnd_api.g_valid_level_full
1145 	     ,p_instance_rec             => p_instance_rec
1146 	     ,p_ext_attrib_values_tbl    => p_ext_attrib_values_tbl
1147 	     ,p_party_tbl                => p_party_tbl
1148 	     ,p_account_tbl              => p_account_tbl
1149 	     ,p_pricing_attrib_tbl       => p_pricing_attrib_tbl
1150 	     ,p_org_assignments_tbl      => p_org_assignments_tbl
1151 	     ,p_asset_assignment_tbl     => p_asset_assignment_tbl
1152 	     ,p_txn_rec                  => p_txn_rec
1153 	     ,x_return_status            => x_return_status
1154 	     ,x_msg_count                => x_msg_count
1155 	     ,x_msg_data                 => x_msg_data
1156 	  );
1157 
1158 	  IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
1159           l_msg_index := 1;
1160           l_msg_count := x_msg_count;
1161           WHILE l_msg_count > 0 LOOP
1162                   x_msg_data := FND_MSG_PUB.GET
1163                               (  l_msg_index,
1164                                  FND_API.G_FALSE );
1165               csi_gen_utility_pvt.put_line('ERROR FROM CSI_ITEM_INSTANCE_VUHK.Create_Item_Instance_Post API ');
1166               csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
1167               l_msg_index := l_msg_index + 1;
1168               l_msg_count := l_msg_count - 1;
1169           END LOOP;
1170         RAISE FND_API.G_EXC_ERROR;
1171 	  END IF;
1172         --
1173      END IF;
1174 EXCEPTION
1175     WHEN OTHERS THEN
1176        csi_gen_utility_pvt.put_line('ERROR FROM JTF_USR_HKS.Ok_to_execute API Create Post Vertical');
1177        RAISE FND_API.G_EXC_ERROR;
1178   END;
1179      -- End of POST User Hooks
1180 
1181 
1182 
1183 EXCEPTION
1184 
1185     WHEN FND_API.G_EXC_ERROR THEN
1186                 x_return_status := FND_API.G_RET_STS_ERROR ;
1187                 ROLLBACK TO create_item_instance;
1188                 FND_MSG_PUB.Count_And_Get
1189                 (       p_encoded => FND_API.G_FALSE,   --Added for bug 7657438
1190                         p_count   => x_msg_count,
1191                         p_data    => x_msg_data
1192                 );
1193 
1194         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1195                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1196                 ROLLBACK TO create_item_instance;
1197                 FND_MSG_PUB.Count_And_Get
1198                 (       p_encoded => FND_API.G_FALSE,
1199                  p_count => x_msg_count,
1200                         p_data  => x_msg_data
1201                 );
1202 
1203         WHEN OTHERS THEN
1204                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1205                 ROLLBACK TO create_item_instance;
1206                 IF      FND_MSG_PUB.Check_Msg_Level
1207                         (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
1208                 THEN
1209                 FND_MSG_PUB.Add_Exc_Msg
1210                 (G_PKG_NAME,
1211              l_api_name
1212                 );
1213                 END IF;
1214                 FND_MSG_PUB.Count_And_Get
1215                 (       p_encoded => FND_API.G_FALSE,
1216                  p_count                 =>      x_msg_count,
1217                         p_data                  =>      x_msg_data
1218                 );
1219 
1220 END create_item_instance;
1221 
1222 /*----------------------------------------------------*/
1223 /* Procedure name: update_item_instance               */
1224 /* Description :   procedure used to update an Item   */
1225 /*                 Instance                           */
1226 /*----------------------------------------------------*/
1227 
1228 PROCEDURE update_item_instance
1229  (
1230      p_api_version           IN     NUMBER
1231     ,p_commit                IN     VARCHAR2
1232     ,p_init_msg_list         IN     VARCHAR2
1233     ,p_validation_level      IN     NUMBER
1234     ,p_instance_rec          IN     csi_datastructures_pub.instance_rec
1235     ,p_ext_attrib_values_tbl IN OUT NOCOPY csi_datastructures_pub.extend_attrib_values_tbl
1236     ,p_party_tbl             IN OUT NOCOPY csi_datastructures_pub.party_tbl
1237     ,p_account_tbl           IN OUT NOCOPY csi_datastructures_pub.party_account_tbl
1238     ,p_pricing_attrib_tbl    IN OUT NOCOPY csi_datastructures_pub.pricing_attribs_tbl
1239     ,p_org_assignments_tbl   IN OUT NOCOPY csi_datastructures_pub.organization_units_tbl
1240     ,p_asset_assignment_tbl  IN OUT NOCOPY csi_datastructures_pub.instance_asset_tbl
1241     ,p_txn_rec               IN OUT NOCOPY csi_datastructures_pub.transaction_rec
1242     ,x_instance_id_lst       OUT    NOCOPY csi_datastructures_pub.id_tbl
1243     ,x_return_status         OUT    NOCOPY VARCHAR2
1244     ,x_msg_count             OUT    NOCOPY NUMBER
1245     ,x_msg_data              OUT    NOCOPY VARCHAR2
1246  )
1247 
1248 IS
1249     l_api_name               CONSTANT VARCHAR2(30)     := 'UPDATE_ITEM_INSTANCE';
1250     l_api_version            CONSTANT NUMBER           := 1.0;
1251     l_debug_level            NUMBER;
1252     l_instance_rec           csi_datastructures_pub.instance_rec;
1253     l_temp_instance_rec      csi_datastructures_pub.instance_rec;
1254     l_new_instance_rec       csi_datastructures_pub.instance_rec := p_instance_rec;
1255     l_config_instance_rec    csi_datastructures_pub.instance_rec ;
1256     l_config_temp_rec        csi_datastructures_pub.instance_rec ;
1257     l_party_tbl              csi_datastructures_pub.party_tbl;
1258     l_account_tbl            csi_datastructures_pub.party_account_tbl;
1259     l_party_row              NUMBER := 1;
1260     l_acct_row               NUMBER := 1;
1261     l_msg_count              NUMBER;
1262     l_msg_data               VARCHAR2(2000);
1263     l_msg_index              NUMBER;
1264     l_line_count             NUMBER;
1265     l_flag                   VARCHAR2(1):='N';
1266     l_transaction_type       VARCHAR2(10) := NULL;
1267     l_old_oks_cp_rec         oks_ibint_pub.cp_rec_type;
1268     l_new_oks_cp_rec         oks_ibint_pub.cp_rec_type;
1269     l_contracts_status       VARCHAR2(3);
1270     l_account_id             NUMBER;
1271     l_owner_party_id         NUMBER;
1272     l_transaction_date       DATE ;
1273     l_internal_party_id      NUMBER;
1274     l_party_id               NUMBER;
1275     l_active_end_date        DATE;
1276     l_dummy                  VARCHAR2(1);
1277     l_version_label_rec      csi_datastructures_pub.version_label_rec;
1278     l_item_attribute_tbl     csi_item_instance_pvt.item_attribute_tbl;
1279     l_location_tbl           csi_item_instance_pvt.location_tbl;
1280     l_generic_id_tbl         csi_item_instance_pvt.generic_id_tbl;
1281     l_lookup_tbl             csi_item_instance_pvt.lookup_tbl;
1282     l_ins_count_rec          csi_item_instance_pvt.ins_count_rec;
1283     l_ou_lookup_tbl          csi_organization_unit_pvt.lookup_tbl;
1284     l_ou_count_rec           csi_organization_unit_pvt.ou_count_rec;
1285     l_ou_id_tbl              csi_organization_unit_pvt.ou_id_tbl;
1286     l_ext_id_tbl             csi_item_instance_pvt.ext_id_tbl;
1287     l_ext_count_rec          csi_item_instance_pvt.ext_count_rec;
1288     l_ext_attr_tbl           csi_item_instance_pvt.ext_attr_tbl;
1289     l_ext_cat_tbl            csi_item_instance_pvt.ext_cat_tbl;
1290     l_asset_lookup_tbl       csi_asset_pvt.lookup_tbl;
1291     l_asset_count_rec        csi_asset_pvt.asset_count_rec;
1292     l_asset_id_tbl           csi_asset_pvt.asset_id_tbl;
1293     l_asset_loc_tbl          csi_asset_pvt.asset_loc_tbl;
1294     l_updated                BOOLEAN := FALSE;
1295     l_date                   BOOLEAN := FALSE;
1296     l_curr_party_id          NUMBER;                           -- Added for 2972082
1297     l_cascade_party_tbl      csi_datastructures_pub.party_tbl; -- Added for 2972082
1298     l_cascade_account_tbl    csi_datastructures_pub.party_account_tbl; -- Added for 2972082
1299     l_found                  BOOLEAN := FALSE;                 -- Added for 2972082
1300     -- The following were added for att
1301     l_component_ins_type     VARCHAR2(1):=NULL;
1302     l_config_hdr_id          NUMBER;
1303     l_config_rev_nbr         NUMBER;
1304     l_config_key             csi_utility_grp.config_instance_key;
1305     l_config_valid_status    VARCHAR2(10);
1306     l_no_config_keys         BOOLEAN := FALSE;
1307     l_return_message         VARCHAR2(100);
1308     l_order_line_id          NUMBER;
1309     l_party_slot             NUMBER;
1310     l_tmp_return_status      VARCHAR2(1) := FND_API.G_RET_STS_SUCCESS;
1311     l_bump_date              DATE;
1312     -- Begin Add Code for Siebel Genesis Project
1313     l_source_code            VARCHAR2(30);
1314     l_raise_bes_event        VARCHAR2(1) := nvl(fnd_profile.value('CSI_RAISE_BES_CUST_OWNED_INSTANCES'),'N');
1315     l_link_to_line_Id        NUMBER;
1316     l_item_type_code         VARCHAR2(30);
1317     l_root_asset_id          NUMBER;
1318     l_subject_id             NUMBER;
1319     l_object_id              NUMBER;
1320     l_relationship_type_code VARCHAR2(30);
1321     l_relationship_exists    VARCHAR2(1);
1322     l_instance_id	     NUMBER; -- Bug #5912182
1323 	l_create_event_called    VARCHAR2(1) := 'N'; --Added for bug 7156553, base bug 6990065
1324 	l_inst_id                NUMBER;  -- Bug #14346149 and 14584626
1325     -- End Add Code for Siebel Genesis Project
1326     -- end addition
1327     CURSOR instance_csr (p_ins_id IN NUMBER) IS
1328       SELECT  *
1329       FROM    csi_item_instances
1330       WHERE   instance_id = p_ins_id;
1331     l_instance_csr           instance_csr%ROWTYPE;
1332 
1333     CURSOR old_ins_csr (p_ins_id IN NUMBER) IS
1334       SELECT *
1335       FROM   csi_item_instances
1336       WHERE  instance_id=p_ins_id;
1337 
1338     l_old_ins_csr            old_ins_csr%ROWTYPE;
1339 --  Start of defining variables for bug 2172968
1340 
1341     l_create                 NUMBER;
1342     l_update                 NUMBER;
1343     l_p_id                   NUMBER;
1344     l_t_party_tbl            csi_datastructures_pub.party_tbl;
1345     l_t_account_tbl          csi_datastructures_pub.party_account_tbl;
1346     l_new_party_tbl          csi_datastructures_pub.party_tbl;
1347     l_new_account_tbl        csi_datastructures_pub.party_account_tbl;
1348     lb_party_tbl             csi_datastructures_pub.party_tbl;
1349     lc_party_tbl             csi_datastructures_pub.party_tbl;
1350     l_temp_party_tbl         csi_datastructures_pub.party_tbl;
1351     la_account_tbl           csi_datastructures_pub.party_account_tbl;
1352     l_temp_acct_tbl          csi_datastructures_pub.party_account_tbl;
1353 
1354 --  End of defining variables for bug 2172968
1355 
1356     CURSOR config_ins_key (p_config_hdr_id NUMBER, p_config_rev_nbr NUMBER) is
1357       SELECT instance_hdr_id
1358             ,config_item_id
1359             ,has_failures
1360       FROM   cz_config_details_v d
1361             ,cz_config_hdrs_v h
1362       WHERE d.instance_hdr_id = h.config_hdr_id
1363       AND   d.instance_rev_nbr = h.config_rev_nbr
1364       AND   d.component_instance_type = 'I'
1365       AND   d.config_hdr_id = p_config_hdr_id
1366       AND   d.config_rev_nbr = p_config_rev_nbr;
1367 
1368     CURSOR instance_key (p_config_inst_hdr_id NUMBER, p_config_inst_item_id NUMBER)is
1369       SELECT instance_id
1370             ,object_version_number
1371             ,config_valid_status
1372       FROM   csi_item_instances
1373       WHERE  config_inst_hdr_id=p_config_inst_hdr_id
1374       AND    config_inst_item_id=p_config_inst_item_id;
1375 
1376     CURSOR new_config_ins_key (p_config_ins_hdr_id NUMBER, p_config_ins_rev_nbr NUMBER) is
1377       SELECT has_failures
1378             ,config_status
1379       FROM   cz_config_details_v d
1380             ,cz_config_hdrs h
1381       WHERE d.instance_hdr_id = p_config_ins_hdr_id
1382       AND   d.instance_rev_nbr = p_config_ins_rev_nbr
1383      -- AND   d.component_instance_type = 'I'
1384       AND   d.config_hdr_id = h.config_hdr_id
1385       AND   d.config_rev_nbr = h.config_rev_nbr;
1386 
1387     CURSOR new_instance_key (p_config_inst_hdr_id NUMBER, p_config_inst_item_id NUMBER)is
1388       SELECT instance_id
1389             ,object_version_number
1390             ,config_valid_status
1391       FROM   csi_item_instances
1392       WHERE  config_inst_hdr_id=p_config_inst_hdr_id
1393       AND    config_inst_item_id=p_config_inst_item_id;
1394       ------------------------------------------added-----------
1395     CURSOR non_owner_csr (p_ins_pty_id NUMBER) IS
1396       SELECT ip_account_id
1397             ,active_end_date
1398             ,object_version_number
1399       from   csi_ip_accounts
1400       where  instance_party_id=p_ins_pty_id
1401       and    relationship_type_code<>'OWNER';
1402       l_tem_acct_tbl         csi_datastructures_pub.party_account_tbl;
1403       l_tem_party_id         NUMBER:=NULL;
1404       l_temp_var             NUMBER:=1;
1405       l_bacct_row            NUMBER;
1406 	   l_src_change_owner    VARCHAR2(1);
1407       ------------------------------------------end addition ---
1408       l_batch_hdr_id         NUMBER;
1409       l_batch_rev_nbr        NUMBER;
1410       l_instance_id_lst      csi_datastructures_pub.id_tbl;
1411       l_config_ins_rec       csi_datastructures_pub.instance_rec;
1412       l_config_tmp_rec       csi_datastructures_pub.instance_rec;
1413       l_lock_status          NUMBER;
1414       --
1415       px_oks_txn_inst_tbl             OKS_IBINT_PUB.TXN_INSTANCE_TBL;
1416       px_child_inst_tbl               csi_item_instance_grp.child_inst_tbl;
1417       l_batch_type                    VARCHAR2(50);
1418       l_batch_id                      NUMBER;
1419 
1420 BEGIN
1421         -- Standard Start of API savepoint
1422         SAVEPOINT       update_item_instance;
1423 
1424         -- srramakr fix for Bug # 2909878
1425         -- Bug 3804960 commented the following as we are not going to use l_date.
1426         -- We need to honor the end_date passed to instance_rec
1427 /****        IF p_ext_attrib_values_tbl.count > 0 OR
1428            p_party_tbl.count > 0 OR
1429            p_account_tbl.count > 0 OR
1430            p_pricing_attrib_tbl.count > 0 OR
1431            p_org_assignments_tbl.count > 0 OR
1432            p_asset_assignment_tbl.count > 0 THEN
1433            IF p_instance_rec.active_end_date <= sysdate -- srramakr changed to <=
1434            THEN
1435              l_date:=TRUE;
1436            END IF;
1437         END IF; ***/
1438         --
1439         IF p_instance_rec.active_end_date IS NOT NULL AND
1440            p_instance_rec.active_end_date <> FND_API.G_MISS_DATE THEN
1441            p_txn_rec.src_txn_creation_date := p_instance_rec.active_end_date;
1442         END IF;
1443         --
1444         -- End of 3804960
1445         -- Check for freeze_flag in csi_install_parameters is set to 'Y'
1446         csi_utility_grp.check_ib_active;
1447 
1448         -- Standard call to check for call compatibility.
1449         IF NOT FND_API.Compatible_API_Call (l_api_version,
1450                                                 p_api_version,
1451                                                 l_api_name       ,
1452                                                 G_PKG_NAME       )
1453         THEN
1454                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1455         END IF;
1456 
1457         -- Initialize message list if p_init_msg_list is set to TRUE.
1458         IF FND_API.to_Boolean( p_init_msg_list ) THEN
1459                 FND_MSG_PUB.initialize;
1460         END IF;
1461 
1462         --  Initialize API return status to success
1463         x_return_status := FND_API.G_RET_STS_SUCCESS;
1464 
1465         -- Check the profile option debug_level for debug message reporting
1466         l_debug_level:=fnd_profile.value('CSI_DEBUG_LEVEL');
1467 
1468         -- If debug_level = 1 then dump the procedure name
1469         IF (l_debug_level > 0) THEN
1470             csi_gen_utility_pvt.put_line( 'update_item_instance');
1471         END IF;
1472 
1473         -- If the debug level = 2 then dump all the parameters values.
1474         IF (l_debug_level > 1) THEN
1475                    csi_gen_utility_pvt.put_line( 'update_item_instance'     ||
1476                                                  p_api_version         ||'-'||
1477                                                  p_commit              ||'-'||
1478                                                  p_init_msg_list       ||'-'||
1479                                                  p_validation_level );
1480                csi_gen_utility_pvt.dump_instance_rec(p_instance_rec);
1481                csi_gen_utility_pvt.dump_party_tbl(p_party_tbl);
1482                csi_gen_utility_pvt.dump_txn_rec(p_txn_rec);
1483                csi_gen_utility_pvt.dump_organization_unit_tbl(p_org_assignments_tbl);
1484                csi_gen_utility_pvt.dump_pricing_attribs_tbl(p_pricing_attrib_tbl);
1485                csi_gen_utility_pvt.dump_party_account_tbl(p_account_tbl);
1486                csi_gen_utility_pvt.dump_ext_attrib_values_tbl(p_ext_attrib_values_tbl);
1487         END IF;
1488 
1489         /***** srramakr commented for bug # 3304439
1490         -- Check for the profile option and enable trace
1491              l_flag:=CSI_GEN_UTILITY_PVT.enable_trace(l_trace_flag => l_flag);
1492         -- End enable trace
1493         ****/
1494         -- Start API body
1495         IF csi_datastructures_pub.g_install_param_rec.fetch_flag IS NULL THEN
1496            csi_gen_utility_pvt.populate_install_param_rec;
1497         END IF;
1498         --
1499         l_internal_party_id := csi_datastructures_pub.g_install_param_rec.internal_party_id;
1500         --
1501         IF l_internal_party_id IS NULL THEN
1502            FND_MESSAGE.SET_NAME('CSI','CSI_API_UNINSTALLED_PARAMETER');
1503            FND_MSG_PUB.ADD;
1504            RAISE FND_API.G_EXC_ERROR;
1505         END IF;
1506 
1507    -- Begin Add Code for Siebel Genesis Project
1508    csi_gen_utility_pvt.put_line('Inside CSI_ITEM_INSTANCE_PUB.Update_Item_Instance');
1509    csi_gen_utility_pvt.put_line('p_instance_rec = '||to_char(p_instance_rec.instance_id));
1510    -- End Add Code for Siebel Genesis Project
1511 
1512    --Added for bug 7156553, base bug 6990065, when creating a new customer instance, sometime the procedure
1513    --create_instance is called to create the instance in inventory first, then
1514    --an update_instance is called to set the instance as a customer product. This causes only
1515    --update event to be raised for the instance. The code added is to ensure the create event
1516    --will get invoked for the instance.
1517    BEGIN
1518     --Modified for Bug 14346149, forword ported the changes from 11i bug 9436131
1519 	IF p_party_tbl.count > 0 THEN
1520 		csi_gen_utility_pvt.put_line('p_party_tbl.count = '||p_party_tbl.count);
1521         FOR l_party_rec IN p_party_tbl.FIRST .. p_party_tbl.LAST LOOP
1522 			IF p_party_tbl.EXISTS(l_party_rec) THEN
1523 				csi_gen_utility_pvt.put_line('p_party_tbl(l_party_rec).instance_party_id = '||p_party_tbl(l_party_rec).instance_party_id);
1524 				SELECT instance_id
1525 				INTO l_inst_id
1526 				FROM csi_i_parties
1527 				WHERE instance_party_id = p_party_tbl(l_party_rec).instance_party_id
1528 				AND ROWNUM = 1;
1529 			END IF;
1530 		END LOOP;
1531 	END IF;
1532 	csi_gen_utility_pvt.put_line('l_inst_id = '||l_inst_id);
1533 		IF l_inst_id is not null THEN
1534 			csi_gen_utility_pvt.put_line('l_inst_id is not null');
1535 			SELECT 'Y'
1536 			INTO l_create_event_called
1537 			FROM csi_item_instances_h
1538 			WHERE instance_id = l_inst_id
1539 			AND new_accounting_class_code = 'CUST_PROD'
1540 			AND ROWNUM = 1;
1541 		ELSE
1542 			SELECT 'Y'
1543 			INTO l_create_event_called
1544 			FROM csi_item_instances_h
1545 			WHERE instance_id = p_instance_rec.instance_id
1546 			AND new_accounting_class_code = 'CUST_PROD'
1547 			AND ROWNUM = 1;
1548 		END IF;
1549 	csi_gen_utility_pvt.put_line('l_create_event_called = '||l_create_event_called);
1550    EXCEPTION
1551     WHEN OTHERS THEN
1552       l_create_event_called := 'N';
1553    END;
1554    IF (l_debug_level > 1) THEN
1555     csi_gen_utility_pvt.put_line('l_create_event_called  : '||l_create_event_called);
1556    END IF;
1557 
1558     --Added for MACD lock functionality
1559    IF NOT (csi_Item_Instance_Pvt.Anything_To_Update(p_instance_rec   =>  p_instance_rec ))
1560    THEN
1561    -- If Anything is getting updated then lock check will be made in pvt.
1562     IF p_instance_rec.instance_id IS NOT NULL AND
1563        p_instance_rec.instance_id <> fnd_api.g_miss_num
1564     THEN
1565       csi_item_instance_pvt.get_instance_lock_status
1566       ( p_instance_id  => p_instance_rec.instance_id ,
1567         p_lock_status  => l_lock_status
1568       );
1569        IF (p_txn_rec.transaction_type_id = 401 AND
1570            l_lock_status = 1) OR
1571           (l_lock_status = 0)
1572        THEN
1573           NULL;
1574        ELSE
1575         FND_MESSAGE.SET_NAME('CSI','CSI_INSTANCE_LOCKED');
1576         FND_MESSAGE.SET_TOKEN('INSTANCE_ID',p_instance_rec.instance_id);
1577         FND_MSG_PUB.ADD;
1578         RAISE FND_API.G_EXC_ERROR;
1579        END IF;
1580     END IF;
1581    END IF;
1582     -- End addition for MACD lock functionality
1583         -- Call Pre Customer User Hook
1584    BEGIN
1585         IF JTF_USR_HKS.Ok_to_execute(G_PKG_NAME, l_api_name, 'B', 'C' ) THEN
1586            csi_gen_utility_pvt.put_line('Calling  CSI_ITEM_INSTANCE_CUHK.Update_Item_Instance_Pre ..');
1587            CSI_ITEM_INSTANCE_CUHK.Update_Item_Instance_Pre
1588             (
1589 	        p_api_version           =>  1.0
1590 	       ,p_commit                =>  fnd_api.g_false
1591 	       ,p_init_msg_list         =>  fnd_api.g_false
1592 	       ,p_validation_level      =>  fnd_api.g_valid_level_full
1593 	       ,p_instance_rec          =>  p_instance_rec
1594 	       ,p_ext_attrib_values_tbl =>  p_ext_attrib_values_tbl
1595 	       ,p_party_tbl             =>  p_party_tbl
1596 	       ,p_account_tbl           =>  p_account_tbl
1597 	       ,p_pricing_attrib_tbl    =>  p_pricing_attrib_tbl
1598 	       ,p_org_assignments_tbl   =>  p_org_assignments_tbl
1599 	       ,p_asset_assignment_tbl  =>  p_asset_assignment_tbl
1600 	       ,p_txn_rec               =>  p_txn_rec
1601 	       ,x_instance_id_lst       =>  x_instance_id_lst
1602 	       ,x_return_status         =>  x_return_status
1603 	       ,x_msg_count             =>  x_msg_count
1604 	       ,x_msg_data              =>  x_msg_data
1605             );
1606            --
1607            IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
1608                l_msg_index := 1;
1609                l_msg_count := x_msg_count;
1610                WHILE l_msg_count > 0 LOOP
1611                       x_msg_data := FND_MSG_PUB.GET
1612                               (  l_msg_index,
1613                                  FND_API.G_FALSE );
1614                     csi_gen_utility_pvt.put_line('ERROR FROM CSI_ITEM_INSTANCE_CUHK.Update_Item_Instance_Pre API ');
1615                     csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
1616                     l_msg_index := l_msg_index + 1;
1617                     l_msg_count := l_msg_count - 1;
1618                END LOOP;
1619              RAISE FND_API.G_EXC_ERROR;
1620 	       END IF;
1621         --
1622         END IF;
1623 
1624    EXCEPTION
1625     WHEN OTHERS THEN
1626        csi_gen_utility_pvt.put_line('ERROR FROM JTF_USR_HKS.Ok_to_execute API Update Pre Customer');
1627        RAISE FND_API.G_EXC_ERROR;
1628    END;
1629         --
1630         -- Call Pre Vertical user Hook
1631    BEGIN
1632         IF JTF_USR_HKS.Ok_to_execute(G_PKG_NAME, l_api_name, 'B', 'V' ) THEN
1633            csi_gen_utility_pvt.put_line('Calling  CSI_ITEM_INSTANCE_VUHK.Update_Item_Instance_Pre ..');
1634            CSI_ITEM_INSTANCE_VUHK.Update_Item_Instance_Pre
1635             (
1636 		   p_api_version           =>  1.0
1637 	       ,p_commit                =>  fnd_api.g_false
1638 	       ,p_init_msg_list         =>  fnd_api.g_false
1639 	       ,p_validation_level      =>  fnd_api.g_valid_level_full
1640 	       ,p_instance_rec          =>  p_instance_rec
1641 	       ,p_ext_attrib_values_tbl =>  p_ext_attrib_values_tbl
1642 	       ,p_party_tbl             =>  p_party_tbl
1643 	       ,p_account_tbl           =>  p_account_tbl
1644 	       ,p_pricing_attrib_tbl    =>  p_pricing_attrib_tbl
1645 	       ,p_org_assignments_tbl   =>  p_org_assignments_tbl
1646 	       ,p_asset_assignment_tbl  =>  p_asset_assignment_tbl
1647 	       ,p_txn_rec               =>  p_txn_rec
1648 	       ,x_instance_id_lst       =>  x_instance_id_lst
1649 	       ,x_return_status         =>  x_return_status
1650 	       ,x_msg_count             =>  x_msg_count
1651 	       ,x_msg_data              =>  x_msg_data
1652             );
1653            --
1654            IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
1655                l_msg_index := 1;
1656                l_msg_count := x_msg_count;
1657                WHILE l_msg_count > 0 LOOP
1658                       x_msg_data := FND_MSG_PUB.GET
1659                               (  l_msg_index,
1660                                  FND_API.G_FALSE );
1661                     csi_gen_utility_pvt.put_line('ERROR FROM CSI_ITEM_INSTANCE_VUHK.Update_Item_Instance_Pre API ');
1662                     csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
1663                     l_msg_index := l_msg_index + 1;
1664                     l_msg_count := l_msg_count - 1;
1665                END LOOP;
1666              RAISE FND_API.G_EXC_ERROR;
1667 	       END IF;
1668         END IF;
1669   EXCEPTION
1670     WHEN OTHERS THEN
1671        csi_gen_utility_pvt.put_line('ERROR FROM JTF_USR_HKS.Ok_to_execute API Update Pre Vertical');
1672        RAISE FND_API.G_EXC_ERROR;
1673   END;
1674         -- End of PRE User Hooks
1675 	--
1676         -- This will fetch old instance data for the purpose of contracts
1677        BEGIN
1678         OPEN   old_ins_csr (p_instance_rec.instance_id);
1679         FETCH  old_ins_csr INTO l_old_ins_csr;
1680         CLOSE  old_ins_csr;
1681        EXCEPTION
1682          WHEN OTHERS THEN
1683          NULL;
1684        END;
1685 
1686        BEGIN
1687           SELECT cip.party_id
1688           INTO   l_owner_party_id
1689           FROM   csi_i_parties cip
1690           WHERE  cip.instance_id = p_instance_rec.instance_id
1691           AND    cip.relationship_type_code = 'OWNER';
1692        EXCEPTION
1693           WHEN OTHERS THEN
1694           NULL;
1695        END;
1696 
1697 	    BEGIN
1698  	        SELECT src_change_owner
1699  	        INTO l_src_change_owner
1700  	        FROM csi_txn_sub_types
1701  	        WHERE  sub_type_id = P_txn_rec.txn_sub_type_id
1702  	        AND    transaction_type_id = P_txn_rec.transaction_type_id;
1703  	    EXCEPTION
1704  	        WHEN OTHERS THEN
1705  	        NULL;
1706  	    END;
1707 
1708 -- Following are the cascade ownership changes for
1709 -- bug 2972082
1710       IF p_instance_rec.cascade_ownership_flag IS NOT NULL AND
1711          p_instance_rec.cascade_ownership_flag='Y'
1712       THEN
1713       csi_gen_utility_pvt.put_line('Cascade_ownership_flag      :'||p_instance_rec.cascade_ownership_flag);
1714        IF p_party_tbl.count > 0
1715        THEN
1716          FOR l_party_rec IN p_party_tbl.FIRST .. p_party_tbl.LAST
1717          LOOP
1718            IF p_party_tbl.EXISTS(l_party_rec)
1719            THEN
1720               IF p_party_tbl(l_party_rec).instance_id = p_instance_rec.instance_id AND
1721                  p_party_tbl(l_party_rec).relationship_type_code = 'OWNER'
1722               THEN
1723               -- Assigning cascade_ownership_flag with a value true
1724                  p_party_tbl(l_party_rec).cascade_ownership_flag:='Y';
1725 
1726                  IF p_party_tbl(l_party_rec).party_id<>l_internal_party_id
1727                  THEN
1728                     IF p_account_tbl.count>0
1729                     THEN
1730                        FOR l_acct_rec IN p_account_tbl.FIRST .. p_account_tbl.LAST
1731                        LOOP
1732                          IF p_account_tbl.EXISTS(l_acct_rec)
1733                          THEN
1734                            IF p_account_tbl(l_acct_rec).instance_party_id=
1735                               p_party_tbl(l_party_rec).instance_party_id AND
1736                               p_account_tbl(l_acct_rec).relationship_type_code='OWNER'
1737                            THEN
1738                               l_found:=TRUE;
1739                            END IF;
1740                          END IF;
1741                        END LOOP;
1742                     END IF;
1743                  ELSE
1744                   l_found:= TRUE;
1745                  END IF;
1746               END IF;
1747            END IF;
1748          END LOOP;
1749          IF NOT(l_found)
1750          THEN
1751 		     IF l_src_change_owner = 'N' THEN
1752  	            NULL;
1753  	         ELSE
1754 			    csi_gen_utility_pvt.put_line('Owner party or account information is not passed for the instance');
1755                 csi_gen_utility_pvt.put_line(',which you are trying to cascade the ownership.');
1756                 RAISE FND_API.G_EXC_ERROR;
1757              END IF;
1758 	     END IF;
1759        ELSE
1760          -- Since party information is not passed so I need to build the party and account
1761          -- information.
1762          -- In the party record assign cascade_ownership_flag with a value fnd_api.g_true.
1763          BEGIN
1764            SELECT instance_party_id,
1765                   instance_id,
1766                   party_source_table,
1767                   party_id,
1768                   relationship_type_code,
1769                   contact_flag,
1770                   object_version_number,
1771                   'Y'
1772            INTO   l_cascade_party_tbl(1).instance_party_id,
1773                   l_cascade_party_tbl(1).instance_id,
1774                   l_cascade_party_tbl(1).party_source_table,
1775                   l_cascade_party_tbl(1).party_id,
1776                   l_cascade_party_tbl(1).relationship_type_code,
1777                   l_cascade_party_tbl(1).contact_flag,
1778                   l_cascade_party_tbl(1).object_version_number,
1779                   l_cascade_party_tbl(1).cascade_ownership_flag
1780            FROM   csi_i_parties
1781            WHERE  instance_id=p_instance_rec.instance_id
1782            AND    relationship_type_code='OWNER'
1783            AND   (active_end_date IS NULL OR active_end_date>sysdate);
1784 
1785            p_party_tbl:=l_cascade_party_tbl;
1786 
1787          EXCEPTION
1788            WHEN OTHERS
1789            THEN
1790              csi_gen_utility_pvt.put_line('Owner party information not found for the instance');
1791              csi_gen_utility_pvt.put_line(',which you are trying to cascade the ownership.');
1792              RAISE FND_API.G_EXC_ERROR;
1793          END;
1794 
1795 
1796          IF l_cascade_party_tbl(1).party_id <> l_internal_party_id
1797          THEN
1798          -- Here I need to build account record
1799           BEGIN
1800            SELECT ip_account_id,
1801                   instance_party_id,
1802                   party_account_id,
1803                   relationship_type_code,
1804                   1,
1805                   object_version_number
1806            INTO   l_cascade_account_tbl(1).ip_account_id,
1807                   l_cascade_account_tbl(1).instance_party_id,
1808                   l_cascade_account_tbl(1).party_account_id,
1809                   l_cascade_account_tbl(1).relationship_type_code,
1810                   l_cascade_account_tbl(1).parent_tbl_index,
1811                   l_cascade_account_tbl(1).object_version_number
1812            FROM   csi_ip_accounts
1813            WHERE  instance_party_id=l_cascade_party_tbl(1).instance_party_id
1814            AND    relationship_type_code='OWNER'
1815            AND   (active_end_date IS NULL OR active_end_date>sysdate);
1816                IF p_account_tbl.count>0
1817                THEN
1818                 p_account_tbl(p_account_tbl.count+1):=l_cascade_account_tbl(1);
1819                ELSE
1820                 p_account_tbl:=l_cascade_account_tbl;
1821                END IF;
1822           EXCEPTION
1823             WHEN OTHERS THEN
1824              csi_gen_utility_pvt.put_line('Owner account information not found for the instance');
1825              csi_gen_utility_pvt.put_line(',which you are trying to cascade the ownership');
1826              RAISE FND_API.G_EXC_ERROR;
1827           END;
1828          END IF; -- End of building account record.
1829        END IF;   -- End of party building party record.
1830       END IF;    -- End of cascade ownership check.
1831 
1832 -- End of cascade ownership changes for
1833 -- bug 2972082
1834 
1835        --
1836        --
1837        -- The following code has been added to assign call_contracts
1838        -- false so that contracts call can be made only once.
1839 /*     -- Commented by sguthiva for bug 2307804
1840        IF (p_account_tbl.count > 0) THEN
1841             FOR tab_row IN p_account_tbl.FIRST .. p_account_tbl.LAST
1842             LOOP
1843                IF p_account_tbl.EXISTS(tab_row)
1844                THEN
1845                    IF   p_account_tbl(tab_row).instance_party_id = l_owner_party_id
1846                     AND p_account_tbl(tab_row).instance_party_id IS NOT NULL
1847                     AND p_account_tbl(tab_row).instance_party_id <> fnd_api.g_miss_num
1848                     AND l_owner_party_id IS NOT NULL
1849                    THEN
1850                        p_account_tbl(tab_row).call_contracts := fnd_api.g_false;
1851                        p_account_tbl(tab_row).vld_organization_id := p_instance_rec.vld_organization_id;
1852                    END IF;
1853                END IF;
1854             END LOOP;
1855        END IF;
1856         -- End fetching old instance data for the purpose of contracts
1857 */      -- End commentation by sguthiva for bug 2307804
1858 -- IF any of the instance columns are changing then call the update_item_instance private api
1859    IF (csi_Item_Instance_Pvt.Anything_To_Update(p_instance_rec   =>  p_instance_rec ))
1860        THEN
1861 
1862        l_new_instance_rec := p_instance_rec;
1863 
1864        IF (p_party_tbl.count > 0) THEN
1865            FOR tab_row IN p_party_tbl.FIRST .. p_party_tbl.LAST
1866              LOOP
1867                 IF p_party_tbl.EXISTS(tab_row) THEN
1868                    IF ((p_party_tbl(tab_row).instance_party_id IS NOT NULL) AND
1869                        (p_party_tbl(tab_row).instance_party_id <> FND_API.G_MISS_NUM))THEN
1870                          l_new_instance_rec.accounting_class_code := NULL;
1871                    END IF;
1872                 END IF;
1873              END LOOP;
1874        END IF;
1875 
1876        -- If Ownership is changing from Internal to External then OKS call should be made only at Account update level.
1877        -- The call should be suppressed at instance level. This is true for child instances also.
1878        -- Hence instance_rec.call_contracts is set to FLASE which eventually gets passed to child instances where the
1879        -- check is made.
1880        --
1881        l_party_slot := NULL;
1882        IF l_owner_party_id = l_internal_party_id THEN
1883 	  IF p_party_tbl.count > 0 THEN
1884 	     FOR party_rec in p_party_tbl.FIRST .. p_party_tbl.LAST LOOP
1885 		IF p_party_tbl.EXISTS(party_rec) THEN
1886 		   IF p_party_tbl(party_rec).instance_id = p_instance_rec.instance_id AND
1887 		      p_party_tbl(party_rec).relationship_type_code = 'OWNER' AND
1888 		      p_party_tbl(party_rec).party_id IS NOT NULL AND
1889 		      p_party_tbl(party_rec).party_id <> FND_API.G_MISS_NUM AND
1890 		      p_party_tbl(party_rec).party_id <> l_internal_party_id THEN
1891 		      l_party_slot := party_rec;
1892               -- Added
1893               l_new_instance_rec.owner_party_id:=p_party_tbl(party_rec).party_id;
1894 		      exit;
1895 		   END IF;
1896 		END IF;
1897 	     END LOOP;
1898 	     IF l_party_slot IS NOT NULL AND
1899 		p_account_tbl.count > 0 THEN
1900 		FOR acct_rec in p_account_tbl.FIRST .. p_account_tbl.LAST LOOP
1901 		   IF p_account_tbl.EXISTS(acct_rec) THEN
1902 		      IF p_account_tbl(acct_rec).parent_tbl_index = l_party_slot AND
1903 			 p_account_tbl(acct_rec).relationship_type_code = 'OWNER' THEN
1904 			 l_new_instance_rec.call_contracts := FND_API.G_FALSE; -- since p_instance_rec is IN parameter
1905 			 csi_gen_utility_pvt.put_line('Instance Rec Call contracts set to FALSE');
1906 			 exit;
1907 		      END IF;
1908 		   END IF;
1909 		END LOOP;
1910 	     END IF;
1911 	  END IF;
1912        END IF;
1913        --
1914        -- Similarly if the ownership is transferred from external to internal then
1915        -- make the call to contracts during party update and supress it at instance level.
1916        -- If this instance has a configuration then for the child instance, contracts call will be supressed
1917        -- for TRM txn type in Update_Child_Instance_Location by looking at the instance_rec.call_contracts.
1918        IF l_owner_party_id <> l_internal_party_id AND
1919           p_party_tbl.count > 0 THEN
1920           FOR party_rec in p_party_tbl.FIRST .. p_party_tbl.LAST LOOP
1921              IF p_party_tbl.EXISTS(party_rec) THEN
1922                 IF p_party_tbl(party_rec).instance_id = p_instance_rec.instance_id AND
1923                    p_party_tbl(party_rec).relationship_type_code = 'OWNER' AND
1924                    p_party_tbl(party_rec).party_id = l_internal_party_id THEN
1925                    l_new_instance_rec.call_contracts := FND_API.G_FALSE; -- since p_instance_rec is IN parameter
1926               -- Added
1927                    l_new_instance_rec.owner_party_id:=p_party_tbl(party_rec).party_id;
1928                    csi_gen_utility_pvt.put_line('Instance Rec Call contracts set to FALSE');
1929                    exit;
1930                 END IF;
1931              END IF;
1932           END LOOP;
1933        END IF;
1934        --
1935        IF (p_asset_assignment_tbl.count > 0) THEN
1936            l_new_instance_rec.accounting_class_code := NULL;
1937        END IF;
1938        --
1939        --
1940     -- srramakr fix for Bug # 2909878
1941     IF p_ext_attrib_values_tbl.count > 0 OR
1942        p_party_tbl.count > 0 OR
1943        p_account_tbl.count > 0 OR
1944        p_pricing_attrib_tbl.count > 0 OR
1945        p_org_assignments_tbl.count > 0 OR
1946        p_asset_assignment_tbl.count > 0 OR
1947        -- Added the following code for bug 4350017
1948        ((p_instance_rec.config_inst_hdr_id IS NOT NULL AND
1949          p_instance_rec.config_inst_hdr_id <> fnd_api.g_miss_num) AND
1950         (p_instance_rec.config_inst_item_id IS NOT NULL AND
1951          p_instance_rec.config_inst_item_id <> fnd_api.g_miss_num) AND
1952         (p_instance_rec.config_inst_rev_num IS NOT NULL AND
1953          p_instance_rec.config_inst_rev_num <> fnd_api.g_miss_num)
1954        )
1955        -- End code addition for bug 4350017
1956     THEN
1957        IF    l_new_instance_rec.active_end_date IS NOT NULL
1958         AND  l_new_instance_rec.active_end_date <> fnd_api.g_miss_date
1959         AND  l_new_instance_rec.active_end_date <= SYSDATE
1960        THEN
1961           l_new_instance_rec.active_end_date:=fnd_api.g_miss_date;
1962           l_new_instance_rec.instance_status_id:=fnd_api.g_miss_num; -- srramakr Fix for Bug # 2766216
1963           l_updated:=TRUE;
1964        END IF;
1965     END IF;
1966     -- Call the update_item_instance private API to update the instances
1967     csi_item_instance_pvt.update_item_instance
1968        (
1969          p_api_version      => p_api_version
1970         ,p_commit           => fnd_api.g_false
1971         ,p_init_msg_list    => p_init_msg_list
1972         ,p_validation_level => p_validation_level
1973         ,p_instance_rec     => l_new_instance_rec
1974         ,p_txn_rec          => p_txn_rec
1975         ,x_instance_id_lst  => x_instance_id_lst
1976         ,x_return_status    => x_return_status
1977         ,x_msg_count        => x_msg_count
1978         ,x_msg_data         => x_msg_data
1979         ,p_item_attribute_tbl => l_item_attribute_tbl
1980         ,p_location_tbl     => l_location_tbl
1981         ,p_generic_id_tbl     => l_generic_id_tbl
1982         ,p_lookup_tbl         => l_lookup_tbl
1983         ,p_ins_count_rec      => l_ins_count_rec
1984         ,p_oks_txn_inst_tbl   => px_oks_txn_inst_tbl
1985         ,p_child_inst_tbl     => px_child_inst_tbl
1986        );
1987 
1988        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
1989                   l_msg_index := 1;
1990           l_msg_count := x_msg_count;
1991          WHILE l_msg_count > 0 LOOP
1992                   x_msg_data := FND_MSG_PUB.GET
1993                           (  l_msg_index,
1994                                          FND_API.G_FALSE        );
1995           csi_gen_utility_pvt.put_line( 'Error from UPDATE_ITEM_INSTANCE_PVT..');
1996           csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
1997               l_msg_index := l_msg_index + 1;
1998               l_msg_count := l_msg_count - 1;
1999              END LOOP;
2000          RAISE FND_API.G_EXC_ERROR;
2001        END IF;
2002        -- Calling Contracts for Quantity Change
2003        IF p_instance_rec.quantity IS NOT NULL AND
2004           p_instance_rec.quantity <> FND_API.G_MISS_NUM AND
2005           p_instance_rec.quantity <> 0 AND -- Supress 'UPD' call if Qty drops to 0
2006           l_old_ins_csr.quantity <> p_instance_rec.quantity THEN
2007 	  IF l_owner_party_id IS NOT NULL AND
2008 	     l_owner_party_id <> l_internal_party_id THEN
2009             IF p_txn_rec.transaction_type_id <> 7   -- Added for bug 3973706
2010             THEN
2011 	     CSI_Item_Instance_Pvt.Call_to_Contracts
2012 		   ( p_transaction_type    => 'UPD'
2013 		    ,p_instance_id         => p_instance_rec.instance_id
2014 		    ,p_new_instance_id     => NULL
2015 		    ,p_vld_org_id          => p_instance_rec.vld_organization_id
2016 		    ,p_quantity            => l_old_ins_csr.quantity -- Passing the Old qty
2017 		    ,p_party_account_id1   => NULL
2018 		    ,p_party_account_id2   => NULL
2019                     ,p_transaction_date    => p_txn_rec.transaction_date -- Refer 3483763
2020                     ,p_source_transaction_date   =>   p_txn_rec.source_transaction_date -- Added jpwilson
2021 		    ,p_txn_type_id         => p_txn_rec.transaction_type_id
2022                     ,p_oks_txn_inst_tbl    => px_oks_txn_inst_tbl
2023 		    ,x_return_status       => x_return_status
2024 		    ,x_msg_count           => x_msg_count
2025 		    ,x_msg_data            => x_msg_data
2026 		  );
2027 	       --
2028 	      IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
2029 			l_msg_index := 1;
2030 			l_msg_count := x_msg_count;
2031 		    WHILE l_msg_count > 0 LOOP
2032 		      x_msg_data := FND_MSG_PUB.GET
2033 				    (  l_msg_index,
2034 				       FND_API.G_FALSE
2035 				     );
2036 		      csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
2037 			  l_msg_index := l_msg_index + 1;
2038 			  l_msg_count := l_msg_count - 1;
2039 		    END LOOP;
2040 		      RAISE FND_API.G_EXC_ERROR;
2041 	      END IF;
2042             END IF;  -- Added for bug 3973706
2043 
2044 	  END IF;
2045        END IF;
2046        --
2047        -- Caliing Contracts for Install Date Change
2048        IF ( (p_instance_rec.install_date IS NULL AND
2049              l_old_ins_csr.install_date IS NOT NULL) OR
2050             (p_instance_rec.install_date IS NOT NULL AND
2051              p_instance_rec.install_date <> FND_API.G_MISS_DATE AND
2052              nvl(l_old_ins_csr.install_date,FND_API.G_MISS_DATE) <> p_instance_rec.install_date) ) THEN
2053 	  IF l_owner_party_id IS NOT NULL AND
2054 	     l_owner_party_id <> l_internal_party_id THEN
2055 	     CSI_Item_Instance_Pvt.Call_to_Contracts
2056 	        ( p_transaction_type    => 'IDC'
2057 	         ,p_instance_id         => p_instance_rec.instance_id
2058 		 ,p_new_instance_id     => NULL
2059 		 ,p_vld_org_id          => p_instance_rec.vld_organization_id
2060 		 ,p_quantity            => p_instance_rec.quantity
2061 		 ,p_party_account_id1   => NULL
2062 		 ,p_party_account_id2   => NULL
2063                  ,p_transaction_date    => p_txn_rec.transaction_date -- Refer 3483763
2064                  ,p_source_transaction_date   =>   p_txn_rec.source_transaction_date -- Added jpwilson
2065                  ,p_oks_txn_inst_tbl    => px_oks_txn_inst_tbl
2066 		 ,x_return_status       => x_return_status
2067 		 ,x_msg_count           => x_msg_count
2068 		 ,x_msg_data            => x_msg_data
2069 		 );
2070 	      --
2071               IF x_return_status = 'W' THEN -- Warning from OKS
2072                  l_tmp_return_status := 'W';
2073 		 l_msg_index := 1;
2074 		 l_msg_count := x_msg_count;
2075 	         WHILE l_msg_count > 0 LOOP
2076 	            x_msg_data := FND_MSG_PUB.GET
2077 			     (  l_msg_index,
2078 				FND_API.G_FALSE
2079 			      );
2080 	            csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
2081 		    l_msg_index := l_msg_index + 1;
2082 		    l_msg_count := l_msg_count - 1;
2083 	         END LOOP;
2084                  FND_MSG_PUB.Count_And_Get
2085                  ( p_encoded => FND_API.G_FALSE,
2086                  p_count                 =>      x_msg_count,
2087                    p_data                  =>      x_msg_data
2088                  );
2089 	      ELSIF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
2090 			l_msg_index := 1;
2091 			l_msg_count := x_msg_count;
2092 		    WHILE l_msg_count > 0 LOOP
2093 		      x_msg_data := FND_MSG_PUB.GET
2094 				    (  l_msg_index,
2095 				       FND_API.G_FALSE
2096 				     );
2097 		      csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
2098 			  l_msg_index := l_msg_index + 1;
2099 			  l_msg_count := l_msg_count - 1;
2100 		    END LOOP;
2101 	         RAISE FND_API.G_EXC_ERROR;
2102 	      END IF;
2103           END IF;
2104        END IF;
2105    END IF;
2106    --
2107    x_return_status := l_tmp_return_status;
2108    -- Call Post Customer User Hook
2109   BEGIN
2110 
2111        IF JTF_USR_HKS.Ok_to_execute(G_PKG_NAME, l_api_name, 'A', 'C' ) THEN
2112           csi_gen_utility_pvt.put_line('Calling  CSI_ITEM_INSTANCE_CUHK.Update_Item_Instance_Post ..');
2113           CSI_ITEM_INSTANCE_CUHK.Update_Item_Instance_Post
2114           (
2115            p_api_version           =>  1.0
2116           ,p_commit                =>  fnd_api.g_false
2117 	      ,p_init_msg_list         =>  fnd_api.g_false
2118 	      ,p_validation_level      =>  fnd_api.g_valid_level_full
2119 	      ,p_instance_rec          =>  p_instance_rec
2120 	      ,p_ext_attrib_values_tbl =>  p_ext_attrib_values_tbl
2121 	      ,p_party_tbl             =>  p_party_tbl
2122 	      ,p_account_tbl           =>  p_account_tbl
2123 	      ,p_pricing_attrib_tbl    =>  p_pricing_attrib_tbl
2124 	      ,p_org_assignments_tbl   =>  p_org_assignments_tbl
2125 	      ,p_asset_assignment_tbl  =>  p_asset_assignment_tbl
2126 	      ,p_txn_rec               =>  p_txn_rec
2127 	      ,x_instance_id_lst       =>  x_instance_id_lst
2128 	      ,x_return_status         =>  x_return_status
2129 	      ,x_msg_count             =>  x_msg_count
2130 	      ,x_msg_data              =>  x_msg_data
2131 	      );
2132       --
2133            IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
2134                l_msg_index := 1;
2135                l_msg_count := x_msg_count;
2136                WHILE l_msg_count > 0 LOOP
2137                       x_msg_data := FND_MSG_PUB.GET
2138                               (  l_msg_index,
2139                                  FND_API.G_FALSE );
2140                     csi_gen_utility_pvt.put_line('ERROR FROM CSI_ITEM_INSTANCE_CUHK.Update_Item_Instance_Post API ');
2141                     csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
2142                     l_msg_index := l_msg_index + 1;
2143                     l_msg_count := l_msg_count - 1;
2144                END LOOP;
2145               RAISE FND_API.G_EXC_ERROR;
2146 	   END IF;
2147    END IF;
2148 
2149   EXCEPTION
2150     WHEN OTHERS THEN
2151        csi_gen_utility_pvt.put_line('ERROR FROM JTF_USR_HKS.Ok_to_execute API Update Post Customer');
2152        RAISE FND_API.G_EXC_ERROR;
2153   END;
2154    --
2155    x_return_status := l_tmp_return_status;
2156 
2157   -- Call the update_party_relationship API to update instance-to-party
2158   -- relationships.
2159   -- START MODIFICATION BY SK for bug 2172968
2160    l_t_party_tbl := p_party_tbl;
2161    l_t_account_tbl := p_account_tbl;
2162 
2163    -- p_party_tbl has been replaced by l_t_party_tbl
2164    -- p_account_tbl has been replaced by l_t_account_tbl
2165    -- l_party_tbl has been replaced by l_new_party_tbl
2166    -- l_account_tbl has been replaced by l_new_account_tbl
2167    -- Above changes were made so as not to break the existing code.
2168    l_party_row := 1;
2169    l_acct_row  := 1;
2170    l_bacct_row  := 1;
2171    l_create:=1;
2172    l_update:=1;
2173    IF (l_t_party_tbl.count > 0) THEN
2174       FOR party_row IN l_t_party_tbl.FIRST .. l_t_party_tbl.LAST
2175       LOOP
2176          IF l_t_party_tbl.EXISTS(party_row) THEN
2177              IF ((l_t_party_tbl(party_row).instance_party_id IS NULL)
2178                OR
2179                 (l_t_party_tbl(party_row).instance_party_id = FND_API.G_MISS_NUM))
2180              THEN
2181              -- The following code has been written to take of creating the contacts
2182              -- no matter the order which they are passed. So I'm grabbing the contacts
2183              -- into another temp table lb_party_tbl which I will make use in the seperate
2184              -- call during create_inst_party_relationship.
2185                IF l_t_party_tbl(party_row).contact_ip_id IS NOT NULL AND
2186                   l_t_party_tbl(party_row).contact_ip_id <> FND_API.G_MISS_NUM
2187                THEN
2188                   lb_party_tbl(l_create):= l_t_party_tbl(party_row);
2189                   l_t_party_tbl.DELETE(party_row);
2190                   l_create:=l_create+1;
2191                ELSE
2192                   l_new_party_tbl(l_party_row) := l_t_party_tbl(party_row);
2193                   l_t_party_tbl.DELETE(party_row);
2194                END IF;
2195               -- Grab all its accounts in a temprorary account table
2196               -- The following code has been written to take care the accounts for
2197               -- other than OWNER party.
2198 
2199                  IF l_t_account_tbl.COUNT > 0 THEN
2200                    FOR acct_row IN l_t_account_tbl.FIRST .. l_t_account_tbl.LAST
2201                    LOOP
2202                      IF l_t_account_tbl.EXISTS(acct_row) THEN
2203                        IF (l_t_account_tbl(acct_row).parent_tbl_index = l_party_row ) THEN --changed
2204                            la_account_tbl(l_bacct_row) := l_t_account_tbl(acct_row);
2205                            la_account_tbl(l_bacct_row).parent_tbl_index := l_party_row;
2206                            l_t_account_tbl.DELETE(acct_row);
2207                            l_bacct_row := l_bacct_row + 1;
2208                        END IF;
2209                      END IF;
2210                    END LOOP;
2211                  END IF;
2212 
2213              ELSE
2214               -- The following code has been written to take care the accounts for the
2215               -- OWNER party.
2216                   lc_party_tbl(l_update) := l_t_party_tbl(party_row);
2217                   l_t_party_tbl.DELETE(party_row);
2218 
2219                  IF l_t_account_tbl.COUNT > 0 THEN
2220                    FOR acct_row IN l_t_account_tbl.FIRST .. l_t_account_tbl.LAST
2221                    LOOP
2222                      IF l_t_account_tbl.EXISTS(acct_row) THEN
2223                        IF (l_t_account_tbl(acct_row).parent_tbl_index = l_party_row ) THEN --change
2224                            l_new_account_tbl(l_acct_row) := l_t_account_tbl(acct_row);
2225                            l_new_account_tbl(l_acct_row).parent_tbl_index := l_update;
2226                            l_t_account_tbl.DELETE(acct_row);
2227                            l_acct_row := l_acct_row + 1;
2228                        END IF;
2229                      END IF;
2230                    END LOOP;
2231                  END IF;
2232                  l_update:=l_update+1;
2233              END IF;
2234              l_party_row := l_party_row + 1;
2235          END IF;
2236       END LOOP;
2237    END IF;
2238 
2239    -- Update the parties,contacts and its accounts
2240   IF (lc_party_tbl.count > 0) THEN
2241 
2242   -- The following changes were made to make sure to associate
2243   -- accounts belonging to owner party to a newly created
2244   -- non owner parties.
2245      IF lc_party_tbl.COUNT > 0
2246      THEN
2247          FOR i IN 1..lc_party_tbl.COUNT
2248          LOOP
2249           IF lc_party_tbl.EXISTS(i)
2250           THEN
2251              BEGIN
2252                l_tem_party_id:=NULL;
2253                SELECT party_id
2254                INTO   l_tem_party_id
2255                FROM   csi_i_parties
2256                WHERE  instance_party_id=lc_party_tbl(i).instance_party_id
2257                AND    relationship_type_code='OWNER';
2258              EXCEPTION
2259                WHEN OTHERS THEN
2260                  l_tem_party_id:=NULL;
2261              END;
2262              IF  l_tem_party_id IS NOT NULL AND
2263                  lc_party_tbl(i).party_id <> l_tem_party_id AND
2264                  lc_party_tbl(i).relationship_type_code='OWNER'
2265              THEN
2266                  FOR j IN non_owner_csr(lc_party_tbl(i).instance_party_id)
2267                  LOOP
2268                    l_tem_acct_tbl(l_temp_var).ip_account_id:=j.ip_account_id;
2269                    l_tem_acct_tbl(l_temp_var).object_version_number:=j.object_version_number;
2270                    l_tem_acct_tbl(l_temp_var).active_end_date:=j.active_end_date;
2271                    l_temp_var:=l_temp_var+1;
2272                  END LOOP;
2273 
2274                  -- This loop has been written to take care of contacts issue.
2275                  -- When ever there is a transfer ownership then all the contacts
2276                  -- will get expired.
2277                  -- Contact object_version_numbers need to be bumped up
2278                  -- bug 2933430
2279 
2280                  FOR k IN i..lc_party_tbl.COUNT
2281                  LOOP
2282                    IF   lc_party_tbl(k).contact_ip_id=lc_party_tbl(i).instance_party_id
2283                     AND lc_party_tbl(k).contact_flag='Y'
2284                    THEN
2285                        -- Commented for bug 3376233
2286                        -- lc_party_tbl(k).object_version_number:=lc_party_tbl(k).object_version_number+1;
2287                        l_bump_date:=null;
2288                        -- IF lc_party_tbl(k).active_end_date = fnd_api.g_miss_date
2289                        -- THEN
2290                           BEGIN
2291                            SELECT active_end_date
2292                            INTO   l_bump_date --lc_party_tbl(k).active_end_date
2293                            FROM   csi_i_parties
2294                            WHERE  instance_party_id=lc_party_tbl(k).instance_party_id;
2295 
2296                        -- Added the following if condition for bug 3376233.
2297                            IF l_bump_date IS NULL OR
2298                               l_bump_date > SYSDATE
2299                            THEN
2300                            -- We need to bump up this record as it is active and will
2301                            -- get expired during transfer of ownership.
2302                            -- For those records which were already expired, there is no
2303                            -- need to bump up the version number.
2304                              IF lc_party_tbl(k).active_end_date = fnd_api.g_miss_date
2305                              THEN
2306                               lc_party_tbl(k).active_end_date:=l_bump_date;
2307                              END IF;
2308                              lc_party_tbl(k).object_version_number:=lc_party_tbl(k).object_version_number+1;
2309                            END IF;
2310 
2311 
2312 
2313                           EXCEPTION
2314                             WHEN NO_DATA_FOUND THEN
2315                              NULL;
2316                           END;
2317                        -- END IF;
2318                    END IF;
2319                  END LOOP;
2320 
2321                  -- End addition for contacts bug 2933430
2322 
2323              END IF;
2324           END IF;
2325          END LOOP;
2326      END IF;
2327      --
2328      csi_party_relationships_pub.update_inst_party_relationship
2329 	 (p_api_version      => p_api_version
2330 	 ,p_commit           => fnd_api.g_false
2331 	 ,p_init_msg_list    => p_init_msg_list
2332 	 ,p_validation_level => p_validation_level
2333 	 ,p_party_tbl        => lc_party_tbl
2334 	 ,p_party_account_tbl=> l_new_account_tbl
2335 	 ,p_txn_rec          => p_txn_rec
2336 	 ,p_oks_txn_inst_tbl => px_oks_txn_inst_tbl
2337 	 ,x_return_status    => x_return_status
2338 	 ,x_msg_count        => x_msg_count
2339 	 ,x_msg_data         => x_msg_data
2340 	   );
2341 
2342       IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
2343 	 l_msg_index := 1;
2344 	 l_msg_count := x_msg_count;
2345 	WHILE l_msg_count > 0
2346 	   LOOP
2347 		      x_msg_data := FND_MSG_PUB.GET
2348 			     (  l_msg_index,
2349 				FND_API.G_FALSE       );
2350 		  csi_gen_utility_pvt.put_line('Error from CSI_PARTY_RELATIONSHIPS_PUB.. ');
2351 		  csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
2352 		  l_msg_index := l_msg_index + 1;
2353 		  l_msg_count := l_msg_count - 1;
2354 	    END LOOP;
2355 	    RAISE FND_API.G_EXC_ERROR;
2356       END IF;
2357    END IF;
2358    --
2359    x_return_status := l_tmp_return_status;
2360    -- Create the parties and its accounts
2361    IF l_new_party_tbl.COUNT > 0 THEN
2362       -- The following changes were made to make sure to associate
2363       -- accounts belonging to owner party to a newly created
2364       -- non owner parties.
2365       IF la_account_tbl.COUNT > 0
2366       THEN
2367          FOR i IN 1..la_account_tbl.COUNT
2368          LOOP
2369           IF la_account_tbl.EXISTS(i)
2370           THEN
2371              IF l_tem_acct_tbl.COUNT>0
2372              THEN
2373                FOR j IN 1..l_tem_acct_tbl.COUNT
2374                LOOP
2375                 IF la_account_tbl.EXISTS(i) AND
2376                    la_account_tbl(i).ip_account_id=l_tem_acct_tbl(j).ip_account_id
2377                 THEN
2378                 /*
2379                    la_account_tbl(i).object_version_number:=l_tem_acct_tbl(j).object_version_number+1;
2380                    IF la_account_tbl(i).active_end_date=fnd_api.g_miss_date
2381                    THEN
2382                      la_account_tbl(i).active_end_date:=l_tem_acct_tbl(j).active_end_date;
2383                    END IF;
2384                 */
2385                  -- The following code is added for bug 3594408 (Rel 11.5.9)
2386                  BEGIN
2387                    SELECT object_version_number
2388                    INTO   la_account_tbl(i).object_version_number
2389                    FROM   csi_ip_accounts
2390                    WHERE  ip_account_id=la_account_tbl(i).ip_account_id;
2391                  EXCEPTION
2392                   WHEN NO_DATA_FOUND THEN
2393                    NULL;
2394                  END;
2395                  -- End addition for bug 3594408 (Rel 11.5.9)
2396                 END IF;
2397                END LOOP;
2398              END IF;
2399           END IF;
2400          END LOOP;
2401       END IF;
2402       csi_party_relationships_pub.create_inst_party_relationship
2403        ( p_api_version         => p_api_version
2404 	,p_commit              => fnd_api.g_false
2405 	,p_init_msg_list       => p_init_msg_list
2406 	,p_validation_level    => p_validation_level
2407 	,p_party_tbl           => l_new_party_tbl
2408 	,p_party_account_tbl   => la_account_tbl
2409 	,p_txn_rec             => p_txn_rec
2410 	,p_oks_txn_inst_tbl    => px_oks_txn_inst_tbl
2411 	,x_return_status       => x_return_status
2412 	,x_msg_count           => x_msg_count
2413 	,x_msg_data            => x_msg_data
2414        );
2415       IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
2416 	 l_msg_index := 1;
2417 	 l_msg_count := x_msg_count;
2418 	 WHILE l_msg_count > 0 LOOP
2419 		      x_msg_data := FND_MSG_PUB.GET
2420 			     (  l_msg_index,
2421 				FND_API.G_FALSE       );
2422 	      csi_gen_utility_pvt.put_line('Error from CSI_PARTY_RELATIONSHIPS_PUB.. ');
2423 	      csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
2424 		  l_msg_index := l_msg_index + 1;
2425 		  l_msg_count := l_msg_count - 1;
2426 	 END LOOP;
2427          RAISE FND_API.G_EXC_ERROR;
2428       END IF;
2429    END IF;
2430    x_return_status := l_tmp_return_status;
2431 
2432    -- Create the contacts for oooo.
2433 
2434    IF lb_party_tbl.COUNT > 0 THEN
2435       csi_party_relationships_pub.create_inst_party_relationship
2436        ( p_api_version         => p_api_version
2437 	,p_commit              => fnd_api.g_false
2438 	,p_init_msg_list       => p_init_msg_list
2439 	,p_validation_level    => p_validation_level
2440 	,p_party_tbl           => lb_party_tbl
2441 	,p_party_account_tbl   => l_temp_acct_tbl
2442 	,p_txn_rec             => p_txn_rec
2443 	,p_oks_txn_inst_tbl    => px_oks_txn_inst_tbl
2444 	,x_return_status       => x_return_status
2445 	,x_msg_count           => x_msg_count
2446 	,x_msg_data            => x_msg_data
2447        );
2448       IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
2449 	 l_msg_index := 1;
2450 	 l_msg_count := x_msg_count;
2451 	 WHILE l_msg_count > 0 LOOP
2452 		      x_msg_data := FND_MSG_PUB.GET
2453 			     (  l_msg_index,
2454 				FND_API.G_FALSE       );
2455 	      csi_gen_utility_pvt.put_line('Error from CSI_PARTY_RELATIONSHIPS_PUB.. ');
2456 	      csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
2457 		  l_msg_index := l_msg_index + 1;
2458 		  l_msg_count := l_msg_count - 1;
2459 	 END LOOP;
2460 	 RAISE FND_API.G_EXC_ERROR;
2461       END IF;
2462    END IF;
2463    x_return_status := l_tmp_return_status;
2464 
2465    -- END MODIFICATION BY SK for bug 2172968
2466 
2467 
2468    -- Since some rows were deleted in accounts we need to add them up
2469    IF ((p_account_tbl.count > 0)
2470       OR (l_account_tbl.count > 0)) THEN
2471 
2472         IF p_party_tbl.count = 0 THEN
2473           l_acct_row := l_account_tbl.LAST + 1;
2474         ELSE
2475 
2476           l_acct_row := p_account_tbl.LAST + 1;
2477           IF l_acct_row IS NULL THEN
2478             l_acct_row := 1;
2479           END IF;
2480         END IF;
2481 
2482         IF l_account_tbl.count > 0 THEN
2483           FOR acct_row IN l_account_tbl.FIRST .. l_account_tbl.LAST
2484           LOOP
2485             IF l_account_tbl.EXISTS(acct_row) THEN
2486               p_account_tbl(l_acct_row) := l_account_tbl(acct_row);
2487               l_acct_row := l_acct_row + 1;
2488             END IF;
2489           END LOOP;
2490         END IF;
2491    END IF;
2492 
2493 
2494 -- Call update_organization_unit to associate any org. assignments
2495 -- to the item instance
2496  IF (p_org_assignments_tbl.count > 0) THEN
2497     FOR tab_row IN p_org_assignments_tbl.FIRST .. p_org_assignments_tbl.LAST
2498     LOOP
2499       IF p_org_assignments_tbl.EXISTS(tab_row) THEN
2500         IF ((p_org_assignments_tbl(tab_row).instance_ou_id IS NULL)
2501            OR
2502            (p_org_assignments_tbl(tab_row).instance_ou_id = FND_API.G_MISS_NUM))
2503         THEN
2504             csi_organization_unit_pvt.create_organization_unit
2505              (p_api_version       => p_api_version
2506              ,p_commit            => fnd_api.g_false
2507              ,p_init_msg_list     => p_init_msg_list
2508              ,p_validation_level  => p_validation_level
2509              ,p_org_unit_rec      => p_org_assignments_tbl(tab_row)
2510              ,p_txn_rec           => p_txn_rec
2511              ,x_return_status     => x_return_status
2512              ,x_msg_count         => x_msg_count
2513              ,x_msg_data          => x_msg_data
2514              ,p_lookup_tbl        => l_ou_lookup_tbl
2515              ,p_ou_count_rec      => l_ou_count_rec
2516              ,p_ou_id_tbl         => l_ou_id_tbl
2517             );
2518          ELSE
2519             csi_organization_unit_pvt.update_organization_unit
2520              (p_api_version       => p_api_version
2521              ,p_commit            => fnd_api.g_false
2522              ,p_init_msg_list     => p_init_msg_list
2523              ,p_validation_level  => p_validation_level
2524              ,p_org_unit_rec      => p_org_assignments_tbl(tab_row)
2525              ,p_txn_rec           => p_txn_rec
2526              ,x_return_status     => x_return_status
2527              ,x_msg_count         => x_msg_count
2528              ,x_msg_data          => x_msg_data
2529              ,p_lookup_tbl        => l_ou_lookup_tbl
2530              ,p_ou_count_rec      => l_ou_count_rec
2531              ,p_ou_id_tbl         => l_ou_id_tbl
2532             );
2533        END IF;
2534        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
2535                   l_msg_index := 1;
2536           l_msg_count := x_msg_count;
2537          WHILE l_msg_count > 0 LOOP
2538                   x_msg_data := FND_MSG_PUB.GET
2539                           (  l_msg_index,
2540                              FND_API.G_FALSE    );
2541           csi_gen_utility_pvt.put_line( ' Error from CSI_ORGANIZATION_UNIT_PUB..');
2542           csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
2543               l_msg_index := l_msg_index + 1;
2544               l_msg_count := l_msg_count - 1;
2545              END LOOP;
2546           RAISE FND_API.G_EXC_ERROR;
2547         END IF;
2548       END IF;
2549     END LOOP;
2550   END IF;
2551    x_return_status := l_tmp_return_status;
2552 
2553 -- Call update_pricing_attribs to associate any pricing attributes
2554 -- to the item instance
2555   IF (p_pricing_attrib_tbl.count > 0) THEN
2556     FOR tab_row IN p_pricing_attrib_tbl.FIRST .. p_pricing_attrib_tbl.LAST
2557     LOOP
2558       IF p_pricing_attrib_tbl.EXISTS(tab_row) THEN
2559         IF ((p_pricing_attrib_tbl(tab_row).pricing_attribute_id IS NULL)
2560           OR
2561            (p_pricing_attrib_tbl(tab_row).pricing_attribute_id = FND_API.G_MISS_NUM))
2562         THEN
2563                csi_pricing_attribs_pvt.create_pricing_attribs
2564                 ( p_api_version         => p_api_version
2565                  ,p_commit              => p_commit
2566                  ,p_init_msg_list       => p_init_msg_list
2567                  ,p_validation_level    => p_validation_level
2568                  ,p_pricing_attribs_rec => p_pricing_attrib_tbl(tab_row)
2569                  ,p_txn_rec             => p_txn_rec
2570                  ,x_return_status       => x_return_status
2571                  ,x_msg_count           => x_msg_count
2572                  ,x_msg_data            => x_msg_data
2573                  );
2574          ELSE
2575               csi_pricing_attribs_pvt.update_pricing_attribs
2576                ( p_api_version          => p_api_version
2577                 ,p_commit               => fnd_api.g_false
2578                 ,p_init_msg_list        => p_init_msg_list
2579                 ,p_validation_level     => p_validation_level
2580                 ,p_pricing_attribs_rec  => p_pricing_attrib_tbl(tab_row)
2581                 ,p_txn_rec              => p_txn_rec
2582                 ,x_return_status        => x_return_status
2583                 ,x_msg_count            => x_msg_count
2584                 ,x_msg_data             => x_msg_data
2585                );
2586          END IF;
2587 
2588          IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
2589            l_msg_index := 1;
2590            l_msg_count := x_msg_count;
2591            WHILE l_msg_count > 0 LOOP
2592                      x_msg_data := FND_MSG_PUB.GET
2593                               (  l_msg_index,
2594                                      FND_API.G_FALSE    );
2595                  csi_gen_utility_pvt.put_line( ' Error from CSI_PRICING_ATTRIBS_PUB..');
2596                  csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
2597                      l_msg_index := l_msg_index + 1;
2598                      l_msg_count := l_msg_count - 1;
2599                END LOOP;
2600                  RAISE FND_API.G_EXC_ERROR;
2601          END IF;
2602      END IF;
2603    END LOOP;
2604  END IF;
2605    x_return_status := l_tmp_return_status;
2606 
2607 -- Call create_extended_attribs to associate any extended attributes
2608 -- to the item instance
2609  IF (p_ext_attrib_values_tbl.count > 0) THEN
2610     FOR tab_row IN p_ext_attrib_values_tbl.FIRST .. p_ext_attrib_values_tbl.LAST
2611     LOOP
2612       IF p_ext_attrib_values_tbl.EXISTS (tab_row) THEN
2613         IF ((p_ext_attrib_values_tbl(tab_row).attribute_value_id IS NULL)
2614           OR
2615            (p_ext_attrib_values_tbl(tab_row).attribute_value_id = FND_API.G_MISS_NUM))
2616         THEN
2617             csi_item_instance_pvt.create_extended_attrib_values
2618                 ( p_api_version         => p_api_version
2619                  ,p_commit              => fnd_api.g_false
2620                  ,p_init_msg_list       => p_init_msg_list
2621                  ,p_validation_level    => p_validation_level
2622                  ,p_ext_attrib_rec      => p_ext_attrib_values_tbl(tab_row)
2623                  ,p_txn_rec             => p_txn_rec
2624                  ,x_return_status       => x_return_status
2625                  ,x_msg_count           => x_msg_count
2626                  ,x_msg_data            => x_msg_data
2627                  ,p_ext_id_tbl          => l_ext_id_tbl
2628                  ,p_ext_count_rec       => l_ext_count_rec
2629                  ,p_ext_attr_tbl        => l_ext_attr_tbl
2630                  ,p_ext_cat_tbl         => l_ext_cat_tbl
2631                 );
2632         ELSE
2633 -- call the update extended attributes api
2634             csi_item_instance_pvt.update_extended_attrib_values
2635                ( p_api_version          => p_api_version
2636                 ,p_commit               => fnd_api.g_false
2637                 ,p_init_msg_list        => p_init_msg_list
2638                 ,p_validation_level     => p_validation_level
2639                 ,p_ext_attrib_rec       => p_ext_attrib_values_tbl(tab_row)
2640                 ,p_txn_rec              => p_txn_rec
2641                 ,x_return_status        => x_return_status
2642                 ,x_msg_count            => x_msg_count
2643                 ,x_msg_data             => x_msg_data
2644 --                ,p_ext_id_tbl           => l_ext_id_tbl
2645 --                ,p_ext_count_rec        => l_ext_count_rec
2646 --                ,p_ext_attr_tbl         => l_ext_attr_tbl
2647 --                ,p_ext_cat_tbl          => l_ext_cat_tbl
2648                );
2649         END IF;
2650         IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
2651               l_msg_index := 1;
2652           l_msg_count := x_msg_count;
2653           WHILE l_msg_count > 0 LOOP
2654                     x_msg_data := FND_MSG_PUB.GET
2655                               (  l_msg_index,
2656                                              FND_API.G_FALSE    );
2657                 csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
2658                     l_msg_index := l_msg_index + 1;
2659                     l_msg_count := l_msg_count - 1;
2660               END LOOP;
2661                 RAISE FND_API.G_EXC_ERROR;
2662         END IF;
2663      END IF; -- exist if
2664    END LOOP; -- for loop
2665  END IF;
2666    x_return_status := l_tmp_return_status;
2667 
2668 -- Call create_asset_assignments to associate any assets associated
2669 -- to the item instance
2670   IF (p_asset_assignment_tbl.count > 0) THEN
2671     FOR tab_row IN p_asset_assignment_tbl.FIRST .. p_asset_assignment_tbl.LAST
2672     LOOP
2673       IF p_asset_assignment_tbl.EXISTS(tab_row) THEN
2674         IF ((p_asset_assignment_tbl(tab_row).instance_asset_id IS NULL)
2675           OR
2676            (p_asset_assignment_tbl(tab_row).instance_asset_id = FND_API.G_MISS_NUM)) THEN
2677                csi_asset_pvt.create_instance_asset
2678                 (p_api_version          => p_api_version
2679                 ,p_commit               => fnd_api.g_false
2680                 ,p_init_msg_list        => p_init_msg_list
2681                 ,p_validation_level     => p_validation_level
2682                 ,p_instance_asset_rec   => p_asset_assignment_tbl(tab_row)
2683                 ,p_txn_rec              => p_txn_rec
2684                 ,x_return_status        => x_return_status
2685                 ,x_msg_count            => x_msg_count
2686                 ,x_msg_data             => x_msg_data
2687                 ,p_lookup_tbl           => l_asset_lookup_tbl
2688                 ,p_asset_count_rec      => l_asset_count_rec
2689                 ,p_asset_id_tbl         => l_asset_id_tbl
2690                 ,p_asset_loc_tbl        => l_asset_loc_tbl
2691                 );
2692         ELSE
2693 --call the update assets api
2694                csi_asset_pvt.update_instance_asset
2695                 (p_api_version          => p_api_version
2696                 ,p_commit               => fnd_api.g_false
2697                 ,p_init_msg_list        => p_init_msg_list
2698                 ,p_validation_level     => p_validation_level
2699                 ,p_instance_asset_rec   => p_asset_assignment_tbl(tab_row)
2700                 ,p_txn_rec              => p_txn_rec
2701                 ,x_return_status        => x_return_status
2702                 ,x_msg_count            => x_msg_count
2703                 ,x_msg_data             => x_msg_data
2704                 ,p_lookup_tbl           => l_asset_lookup_tbl
2705                 ,p_asset_count_rec      => l_asset_count_rec
2706                 ,p_asset_id_tbl         => l_asset_id_tbl
2707                 ,p_asset_loc_tbl        => l_asset_loc_tbl
2708                 );
2709         END IF;
2710         IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
2711                   l_msg_index := 1;
2712           l_msg_count := x_msg_count;
2713           WHILE l_msg_count > 0 LOOP
2714                 x_msg_data := FND_MSG_PUB.GET
2715                               (  l_msg_index,
2716                                          FND_API.G_FALSE  );
2717                 csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
2718                     l_msg_index := l_msg_index + 1;
2719                     l_msg_count := l_msg_count - 1;
2720               END LOOP;
2721                 RAISE FND_API.G_EXC_ERROR;
2722         END IF;
2723       END IF;
2724     END LOOP;
2725   END IF;
2726    x_return_status := l_tmp_return_status;
2727 
2728 -- Start att enhancements
2729   IF p_instance_rec.call_batch_validation<>fnd_api.g_false
2730   THEN
2731     IF p_instance_rec.instance_id IS NOT NULL AND
2732        p_instance_rec.instance_id <> fnd_api.g_miss_num
2733     THEN
2734        IF  (   l_old_ins_csr.config_inst_hdr_id IS NOT NULL
2735           AND (p_instance_rec.config_inst_hdr_id IS NULL OR p_instance_rec.config_inst_hdr_id = fnd_api.g_miss_num) )
2736          OR(   l_old_ins_csr.config_inst_hdr_id IS NULL
2737           AND (p_instance_rec.config_inst_hdr_id IS NOT NULL AND p_instance_rec.config_inst_hdr_id <> fnd_api.g_miss_num) )
2738          OR(   l_old_ins_csr.config_inst_hdr_id IS NOT NULL
2739           AND (p_instance_rec.config_inst_hdr_id IS NOT NULL AND p_instance_rec.config_inst_hdr_id <> fnd_api.g_miss_num)
2740           AND (l_old_ins_csr.config_inst_hdr_id <> p_instance_rec.config_inst_hdr_id) )
2741          OR(   l_old_ins_csr.config_inst_item_id IS NOT NULL
2742           AND (p_instance_rec.config_inst_item_id IS NULL OR p_instance_rec.config_inst_item_id = fnd_api.g_miss_num) )
2743          OR(   l_old_ins_csr.config_inst_item_id IS NULL
2744           AND (p_instance_rec.config_inst_item_id IS NOT NULL AND p_instance_rec.config_inst_item_id <> fnd_api.g_miss_num) )
2745          OR(   l_old_ins_csr.config_inst_item_id IS NOT NULL
2746           AND (p_instance_rec.config_inst_item_id IS NOT NULL AND p_instance_rec.config_inst_item_id <> fnd_api.g_miss_num)
2747           AND (l_old_ins_csr.config_inst_item_id <> p_instance_rec.config_inst_item_id) )
2748        THEN
2749                l_no_config_keys:=TRUE;
2750 		       csi_gen_utility_pvt.put_line( 'Config keys were not provided. So no batch validation will be performed');
2751        END IF;
2752 
2753       IF NOT l_no_config_keys
2754       THEN
2755        IF   (p_instance_rec.config_inst_hdr_id IS NOT NULL AND p_instance_rec.config_inst_hdr_id <> fnd_api.g_miss_num)
2756         AND (p_instance_rec.config_inst_item_id IS NOT NULL AND p_instance_rec.config_inst_item_id <> fnd_api.g_miss_num)
2757         AND (p_instance_rec.config_inst_rev_num IS NOT NULL AND p_instance_rec.config_inst_rev_num <> fnd_api.g_miss_num)
2758        THEN
2759 
2760           IF NOT csi_item_instance_vld_pvt.is_unique_config_key ( p_config_inst_hdr_id  => p_instance_rec.config_inst_hdr_id
2761                                                                  ,p_config_inst_item_id => p_instance_rec.config_inst_item_id
2762                                                                  ,p_instance_id         => p_instance_rec.instance_id
2763                                                                  ,p_validation_mode     => 'UPDATE'
2764                                                                  )
2765           THEN
2766            FND_MESSAGE.SET_NAME('CSI','CSI_CONFIG_KEY_EXISTS');
2767            FND_MSG_PUB.Add;
2768            RAISE fnd_api.g_exc_error;
2769           END IF;
2770 
2771           BEGIN
2772               SELECT component_instance_type
2773                     ,config_hdr_id
2774                     ,config_rev_nbr
2775               INTO   l_component_ins_type
2776                     ,l_config_hdr_id
2777                     ,l_config_rev_nbr
2778               FROM   cz_config_items_v
2779               WHERE  instance_hdr_id = p_instance_rec.config_inst_hdr_id
2780               AND    instance_rev_nbr = p_instance_rec.config_inst_rev_num
2781               AND    config_item_id = p_instance_rec.config_inst_item_id;
2782           EXCEPTION
2783               WHEN OTHERS THEN
2784                FND_MESSAGE.SET_NAME('CSI','CSI_CONFIG_NOT_IN_CZ');
2785                FND_MESSAGE.SET_TOKEN('INSTANCE_HDR_ID',p_instance_rec.config_inst_hdr_id);
2786                FND_MESSAGE.SET_TOKEN('INSTANCE_REV_NBR',p_instance_rec.config_inst_rev_num);
2787                FND_MESSAGE.SET_TOKEN('CONFIG_ITEM_ID',p_instance_rec.config_inst_item_id);
2788 
2789                FND_MSG_PUB.Add;
2790                RAISE FND_API.G_EXC_ERROR;
2791           END;
2792 
2793           IF l_component_ins_type='I'
2794           THEN
2795 
2796             Csi_item_instance_vld_pvt.Call_batch_validate
2797               ( p_instance_rec    => p_instance_rec
2798                ,p_config_hdr_id   => l_config_hdr_id
2799                ,p_config_rev_nbr  => l_config_rev_nbr
2800                ,x_config_hdr_id   => l_batch_hdr_id
2801                ,x_config_rev_nbr  => l_batch_rev_nbr
2802                ,x_return_status   => x_return_status);
2803 
2804             IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
2805                FND_MESSAGE.SET_NAME('CSI','CSI_CONFIG_ERROR');
2806                FND_MSG_PUB.Add;
2807                csi_gen_utility_pvt.put_line('Call to batch validation has errored ');
2808                RAISE FND_API.G_EXC_ERROR;
2809             END IF;
2810           END IF;
2811 
2812        ELSIF (  (p_instance_rec.config_inst_hdr_id IS NULL OR p_instance_rec.config_inst_hdr_id = fnd_api.g_miss_num)
2813             AND (p_instance_rec.config_inst_item_id IS NOT NULL AND p_instance_rec.config_inst_item_id <> fnd_api.g_miss_num))
2814           OR (  (p_instance_rec.config_inst_hdr_id IS NOT NULL AND p_instance_rec.config_inst_hdr_id <> fnd_api.g_miss_num)
2815             AND (p_instance_rec.config_inst_item_id IS NULL OR p_instance_rec.config_inst_item_id = fnd_api.g_miss_num))
2816           OR (  (p_instance_rec.config_inst_hdr_id IS NOT NULL AND p_instance_rec.config_inst_hdr_id <> fnd_api.g_miss_num)
2817             AND (p_instance_rec.config_inst_item_id IS NOT NULL AND p_instance_rec.config_inst_item_id <> fnd_api.g_miss_num)
2818             AND (p_instance_rec.config_inst_rev_num IS NULL OR p_instance_rec.config_inst_rev_num = fnd_api.g_miss_num))
2819        THEN
2820 		       FND_MESSAGE.SET_NAME('CSI','CSI_INVALID_CONFIG_COMB');
2821 		       FND_MSG_PUB.Add;
2822 		       RAISE fnd_api.g_exc_error;
2823        END IF;
2824       END IF;
2825     END IF;
2826   END IF;
2827    -- Adding new changes for bug 3799694
2828     IF   (p_instance_rec.config_inst_hdr_id IS NOT NULL AND p_instance_rec.config_inst_hdr_id <> fnd_api.g_miss_num)
2829         AND (p_instance_rec.config_inst_item_id IS NOT NULL AND p_instance_rec.config_inst_item_id <> fnd_api.g_miss_num)
2830         AND (p_instance_rec.config_inst_rev_num IS NOT NULL AND p_instance_rec.config_inst_rev_num <> fnd_api.g_miss_num)
2831     THEN
2832              FOR l_config_ins_key IN new_config_ins_key(p_instance_rec.config_inst_hdr_id,p_instance_rec.config_inst_rev_num)
2833               LOOP
2834                 IF l_config_ins_key.has_failures ='1'
2835                  OR nvl(l_config_ins_key.config_status,'0') <> '2'
2836                 THEN
2837                    FOR l_instance_key IN new_instance_key(p_instance_rec.config_inst_hdr_id,p_instance_rec.config_inst_item_id)
2838                    LOOP
2839 
2840                          l_config_instance_rec:=l_config_temp_rec;
2841                       IF (l_instance_key.instance_id IS NOT NULL AND
2842                           l_instance_key.instance_id <> fnd_api.g_miss_num) AND
2843                          ( l_instance_key.config_valid_status IS NULL OR
2844                           (l_instance_key.config_valid_status IS NOT NULL AND
2845                            l_instance_key.config_valid_status <> '1'))
2846                       THEN
2847                          l_config_instance_rec.instance_id:=l_instance_key.instance_id;
2848                          l_config_instance_rec.object_version_number:=l_instance_key.object_version_number;
2849                          l_config_instance_rec.config_valid_status:='1'; --INVALID
2850 
2851                          csi_item_instance_pvt.update_item_instance
2852                           (
2853                             p_api_version        => p_api_version
2854                            ,p_commit             => fnd_api.g_false
2855                            ,p_init_msg_list      => p_init_msg_list
2856                            ,p_validation_level   => p_validation_level
2857                            ,p_instance_rec       => l_config_instance_rec
2858                            ,p_txn_rec            => p_txn_rec
2859                            ,x_instance_id_lst    => l_instance_id_lst
2860                            ,x_return_status      => x_return_status
2861                            ,x_msg_count          => x_msg_count
2862                            ,x_msg_data           => x_msg_data
2863                            ,p_item_attribute_tbl => l_item_attribute_tbl
2864                            ,p_location_tbl       => l_location_tbl
2865                            ,p_generic_id_tbl     => l_generic_id_tbl
2866                            ,p_lookup_tbl         => l_lookup_tbl
2867                            ,p_ins_count_rec      => l_ins_count_rec
2868 	                   ,p_oks_txn_inst_tbl   => px_oks_txn_inst_tbl
2869                            ,p_child_inst_tbl     => px_child_inst_tbl
2870                          );
2871 
2872                       IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
2873                            l_msg_index := 1;
2874                            l_msg_count := x_msg_count;
2875                         WHILE l_msg_count > 0 LOOP
2876                              x_msg_data := FND_MSG_PUB.GET
2877                                     (  l_msg_index,
2878                                        FND_API.G_FALSE        );
2879                              csi_gen_utility_pvt.put_line( 'Error from UPDATE_ITEM_INSTANCE_PVT..');
2880                              csi_gen_utility_pvt.put_line( 'while updating config status');
2881                              csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
2882                               l_msg_index := l_msg_index + 1;
2883                               l_msg_count := l_msg_count - 1;
2884                         END LOOP;
2885                            RAISE FND_API.G_EXC_ERROR;
2886                       END IF;
2887                       END IF;
2888                    END LOOP;
2889                 ELSE
2890                    FOR l_instance_key IN new_instance_key(p_instance_rec.config_inst_hdr_id,p_instance_rec.config_inst_item_id)
2891                    LOOP
2892                          l_config_instance_rec:=l_config_temp_rec;
2893                       IF (l_instance_key.instance_id IS NOT NULL AND
2894                           l_instance_key.instance_id <> fnd_api.g_miss_num) AND
2895                          ( l_instance_key.config_valid_status IS NULL OR
2896                           (l_instance_key.config_valid_status IS NOT NULL AND
2897                            l_instance_key.config_valid_status <> '0'))
2898                       THEN
2899                          l_config_instance_rec.instance_id:=l_instance_key.instance_id;
2900                          l_config_instance_rec.object_version_number:=l_instance_key.object_version_number;
2901                          l_config_instance_rec.config_valid_status:='0'; --VALID
2902                          csi_item_instance_pvt.update_item_instance
2903                           (
2904                             p_api_version        => p_api_version
2905                            ,p_commit             => fnd_api.g_false
2906                            ,p_init_msg_list      => p_init_msg_list
2907                            ,p_validation_level   => p_validation_level
2908                            ,p_instance_rec       => l_config_instance_rec
2909                            ,p_txn_rec            => p_txn_rec
2910                            ,x_instance_id_lst    => l_instance_id_lst
2911                            ,x_return_status      => x_return_status
2912                            ,x_msg_count          => x_msg_count
2913                            ,x_msg_data           => x_msg_data
2914                            ,p_item_attribute_tbl => l_item_attribute_tbl
2915                            ,p_location_tbl       => l_location_tbl
2916                            ,p_generic_id_tbl     => l_generic_id_tbl
2917                            ,p_lookup_tbl         => l_lookup_tbl
2918                            ,p_ins_count_rec      => l_ins_count_rec
2919 	                   ,p_oks_txn_inst_tbl   => px_oks_txn_inst_tbl
2920                            ,p_child_inst_tbl     => px_child_inst_tbl
2921                          );
2922 
2923                       IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
2924                            l_msg_index := 1;
2925                            l_msg_count := x_msg_count;
2926                         WHILE l_msg_count > 0 LOOP
2927                              x_msg_data := FND_MSG_PUB.GET
2928                                     (  l_msg_index,
2929                                        FND_API.G_FALSE        );
2930                              csi_gen_utility_pvt.put_line( 'Error from UPDATE_ITEM_INSTANCE_PVT..');
2931                              csi_gen_utility_pvt.put_line( 'while updating config status');
2932                              csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
2933                               l_msg_index := l_msg_index + 1;
2934                               l_msg_count := l_msg_count - 1;
2935                         END LOOP;
2936                            RAISE FND_API.G_EXC_ERROR;
2937                       END IF;
2938                       END IF;
2939                    END LOOP;
2940                 END IF;
2941              END LOOP;
2942 
2943     END IF;
2944 
2945 
2946 -- End addition of changes for bug 3799694
2947   x_return_status := l_tmp_return_status;
2948 -- End att enhancements
2949 
2950     -- sguthiva Added the following for bug 2632869
2951     IF l_updated
2952     THEN
2953     l_instance_rec:=l_temp_instance_rec;
2954     l_instance_rec:=p_instance_rec;
2955     -- Bug 3804960 We need to honor the end_date passed to instance_rec
2956    /***  IF l_date
2957      THEN
2958        l_instance_rec.active_end_date:=SYSDATE;
2959      END IF; ***/
2960      -- End of 3804960
2961      l_instance_rec.call_contracts := l_new_instance_rec.call_contracts; -- added for avoiding multiple OKS calls
2962      BEGIN
2963        SELECT object_version_number
2964        INTO   l_instance_rec.object_version_number
2965        FROM   csi_item_instances
2966        WHERE  instance_id=p_instance_rec.instance_id;
2967      EXCEPTION
2968        WHEN NO_DATA_FOUND THEN
2969         NULL;
2970      END;
2971 -- Call the update_item_instance private API to update the instances
2972        csi_item_instance_pvt.update_item_instance
2973        (
2974          p_api_version      => p_api_version
2975         ,p_commit           => fnd_api.g_false
2976         ,p_init_msg_list    => p_init_msg_list
2977         ,p_validation_level => p_validation_level
2978         ,p_instance_rec     => l_instance_rec
2979         ,p_txn_rec          => p_txn_rec
2980         ,x_instance_id_lst  => x_instance_id_lst
2981         ,x_return_status    => x_return_status
2982         ,x_msg_count        => x_msg_count
2983         ,x_msg_data         => x_msg_data
2984         ,p_item_attribute_tbl => l_item_attribute_tbl
2985         ,p_location_tbl     => l_location_tbl
2986         ,p_generic_id_tbl     => l_generic_id_tbl
2987         ,p_lookup_tbl         => l_lookup_tbl
2988         ,p_ins_count_rec      => l_ins_count_rec
2989 	,p_oks_txn_inst_tbl   => px_oks_txn_inst_tbl
2990         ,p_child_inst_tbl     => px_child_inst_tbl
2991        );
2992 
2993        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
2994                   l_msg_index := 1;
2995           l_msg_count := x_msg_count;
2996          WHILE l_msg_count > 0 LOOP
2997                   x_msg_data := FND_MSG_PUB.GET
2998                           (  l_msg_index,
2999                                          FND_API.G_FALSE        );
3000           csi_gen_utility_pvt.put_line( 'Error from UPDATE_ITEM_INSTANCE_PVT..');
3001           csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
3002               l_msg_index := l_msg_index + 1;
3003               l_msg_count := l_msg_count - 1;
3004              END LOOP;
3005          RAISE FND_API.G_EXC_ERROR;
3006        END IF;
3007     END IF;
3008    x_return_status := l_tmp_return_status;
3009     -- End addition for bug 2632869
3010 
3011 -- Added by rtalluri for Bug 2256588 on 03/26/02
3012 -- Call the Version label API to associate a version label for the updated record
3013 
3014    IF p_instance_rec.active_end_date = FND_API.G_MISS_DATE
3015    THEN
3016      BEGIN
3017        SELECT active_end_date
3018        INTO   l_active_end_date
3019        FROM   csi_item_instances
3020        WHERE  instance_id = p_instance_rec.instance_id;
3021      EXCEPTION
3022        WHEN OTHERS THEN
3023          NULL;
3024      END;
3025    ELSE
3026        l_active_end_date := p_instance_rec.active_end_date;
3027    END IF;
3028 
3029       IF  ((l_active_end_date > SYSDATE) OR
3030            (l_active_end_date IS NULL))
3031       THEN
3032           IF    ((p_instance_rec.version_label IS NOT NULL) AND
3033                  (p_instance_rec.version_label <> FND_API.G_MISS_CHAR))
3034           THEN
3035  -- Check if version label already exists in csi_i_version_labels
3036  -- If exists then raise an error message
3037                  BEGIN
3038 
3039                    SELECT 'x'
3040                    INTO   l_dummy
3041                    FROM   csi_i_version_labels
3042                    WHERE  instance_id = p_instance_rec.instance_id
3043                    AND    version_label = p_instance_rec.version_label
3044                    AND    ROWNUM=1;
3045 
3046                    fnd_message.set_name('CSI','CSI_VERSION_LABEL_EXIST');
3047                    fnd_msg_pub.add;
3048                    RAISE fnd_api.g_exc_error;
3049                  EXCEPTION
3050                     WHEN NO_DATA_FOUND THEN
3051                       NULL;
3052                     WHEN OTHERS THEN
3053                       RAISE fnd_api.g_exc_error;
3054                  END;
3055 
3056                   l_version_label_rec.instance_id           := p_instance_rec.instance_id;
3057                   l_version_label_rec.version_label         := p_instance_rec.version_label;
3058                   l_version_label_rec.description           := p_instance_rec.version_label_description;
3059                   l_version_label_rec.date_time_stamp       := SYSDATE;
3060                -- calling create version label api
3061                   csi_item_instance_pvt.create_version_label
3062                   ( p_api_version         => p_api_version
3063                    ,p_commit              => p_commit
3064                    ,p_init_msg_list       => p_init_msg_list
3065                    ,p_validation_level    => p_validation_level
3066                    ,p_version_label_rec   => l_version_label_rec
3067                    ,p_txn_rec             => p_txn_rec
3068                    ,x_return_status       => x_return_status
3069                    ,x_msg_count           => x_msg_count
3070                    ,x_msg_data            => x_msg_data         );
3071 
3072                 IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
3073                    FND_MESSAGE.SET_NAME('CSI','CSI_FAILED_TO_CREATE_VERSION');
3074                    FND_MESSAGE.SET_TOKEN('API_ERROR', 'CREATE_VERSION_LABEL');
3075                    FND_MSG_PUB.Add;
3076                    RAISE FND_API.G_EXC_ERROR;
3077                 END IF;
3078           END IF;
3079       END IF;
3080    x_return_status := l_tmp_return_status;
3081 
3082    -- End of addition by rtalluri for Bug 2256588 on 03/26/02
3083 
3084    -- Here we call update_version_time to update date_time_stamp of
3085    -- version labels created with this transaction_id to sysdate.
3086    -- Commented the following code, which is causing the prformance
3087    -- issue. -- bug 3558082 (reported in 11.5.8)
3088    --
3089    x_return_status := l_tmp_return_status;
3090 
3091    -- Calling Contracts
3092    -- Added on 02-OCT-01
3093    OPEN  instance_csr (p_instance_rec.instance_id);
3094    FETCH instance_csr INTO l_instance_csr;
3095    CLOSE instance_csr;
3096    --
3097    BEGIN
3098       SELECT cip.party_id
3099       INTO   l_party_id
3100       FROM   csi_i_parties cip
3101       WHERE  cip.instance_id = p_instance_rec.instance_id
3102       AND    cip.relationship_type_code = 'OWNER';
3103    EXCEPTION
3104       WHEN OTHERS THEN
3105 	 l_party_id := NULL;
3106    END;
3107 
3108    csi_gen_utility_pvt.put_line('l_party_id  - ' || l_party_id );
3109    csi_gen_utility_pvt.put_line('l_internal_party_id  - ' || l_internal_party_id );
3110    csi_gen_utility_pvt.put_line('l_old_ins_csr.instance_status_id  - ' ||  l_old_ins_csr.instance_status_id );
3111    csi_gen_utility_pvt.put_line('l_instance_csr.instance_status_id  - ' || l_instance_csr.instance_status_id );
3112    csi_gen_utility_pvt.put_line('p_txn_rec.source_line_ref_id  - ' || p_txn_rec.source_line_ref_id );
3113    csi_gen_utility_pvt.put_line('p_txn_rec.transaction_type_id  - ' || p_txn_rec.transaction_type_id );
3114    csi_gen_utility_pvt.put_line('l_instance_csr.instance_usage_code  - ' || l_instance_csr.instance_usage_code );
3115    csi_gen_utility_pvt.put_line('l_old_ins_csr.instance_usage_code  - ' || l_old_ins_csr.instance_usage_code );
3116 
3117    --
3118    -- srramakr. If the current party is Internal, then no need to call OKS
3119    -- with TRM or RET. The call would have been made when the ownership got changed.
3120     if ((l_party_id is not null and
3121       l_party_id <> l_internal_party_id) or
3122 	  (l_instance_csr.instance_usage_code = 'IN_RELATIONSHIP' OR l_old_ins_csr.instance_usage_code = 'IN_RELATIONSHIP')) THEN --modified for bug 13057871
3123       IF l_old_ins_csr.instance_status_id <> l_instance_csr.instance_status_id THEN
3124 	 IF csi_item_instance_vld_pvt.termination_status
3125 	   ( p_instance_status_id => l_instance_csr.instance_status_id )
3126 	 THEN
3127 	    IF p_txn_rec.transaction_type_id in (53,54) THEN -- RMA
3128 	       l_transaction_type := 'RET';
3129 	    ELSE
3130 	       l_transaction_type := 'TRM';
3131          l_new_instance_rec.call_contracts := 'Y'; -- Bug 14658869
3132 	    END IF;
3133 	     l_transaction_date := l_instance_csr.active_end_date;
3134 	 END IF;
3135       END IF;
3136    END IF;
3137    --
3138    csi_gen_utility_pvt.put_line('l_transaction_type  - ' || l_transaction_type ); -- Added for bug 13057871
3139    IF l_transaction_type IS NULL
3140    THEN
3141       IF l_old_ins_csr.active_end_date <= SYSDATE
3142 	 AND (l_instance_csr.active_end_date IS NULL
3143 	    OR l_instance_csr.active_end_date > SYSDATE )
3144       THEN
3145 	 -- Added by sk for fixing bug 2245976
3146 	 -- OWNER Party_id and Internal_party_id selection moved up.
3147 	 IF l_party_id IS NOT NULL AND
3148 	    l_internal_party_id IS NOT NULL AND
3149 	    l_party_id <> l_internal_party_id
3150 	 THEN
3151 	 -- End addition by sk for fixing bug 2245976
3152 	    l_transaction_type := 'NEW';
3153 	    l_transaction_date := l_instance_csr.active_end_date;
3154 	    -- 11.5.10 ER. While un-expiring the instance, order Line ID will passed only if it is changing
3155 	    IF l_instance_csr.location_type_code = 'IN_TRANSIT' THEN
3156 	       IF nvl(l_old_ins_csr.in_transit_order_line_id,-99999) <>
3157 				  nvl(l_instance_csr.in_transit_order_line_id,-99999) THEN
3158 		  l_order_line_id := l_instance_csr.in_transit_order_line_id;
3159 	       ELSE
3160 		  l_order_line_id := NULL;
3161 	       END IF;
3162 	    ELSE
3163 	       IF nvl(l_old_ins_csr.last_oe_order_line_id,-99999) <>
3164 				nvl(l_instance_csr.last_oe_order_line_id,-99999) THEN
3165 		  l_order_line_id := l_instance_csr.last_oe_order_line_id;
3166 	       ELSE
3167 		  l_order_line_id := NULL;
3168 	       END IF;
3169 	    END IF;
3170 	 ELSE
3171 	    l_transaction_type := NULL;
3172 	 END IF;
3173       END IF;
3174    END IF;
3175    --
3176    IF l_transaction_type IS NOT NULL THEN
3177 
3178       -- Added for DEBUG purposes for bug 9028424
3179       IF l_new_instance_rec.call_contracts = fnd_api.g_false THEN
3180         csi_gen_utility_pvt.put_line('call_contracts(1): '||l_new_instance_rec.call_contracts);
3181       ELSIF l_new_instance_rec.call_contracts = 'N' THEN
3182         csi_gen_utility_pvt.put_line('call_contracts(2): '||l_new_instance_rec.call_contracts);
3183       ELSE
3184         csi_gen_utility_pvt.put_line('call_contracts(3): '||l_new_instance_rec.call_contracts);
3185       END IF;
3186       -- End DEBUG section
3187 
3188       -- srramakr changed from p_instance_rec to l_new_instance_rec
3189       IF (l_new_instance_rec.call_contracts <> fnd_api.g_false AND l_new_instance_rec.call_contracts <> 'N') --added by radha on 04/04/02
3190       THEN
3191          IF p_txn_rec.transaction_type_id <> 7   -- Added for bug 3973706
3192          THEN
3193 	     csi_item_instance_pvt.Call_to_Contracts
3194 	       ( p_transaction_type   =>   l_transaction_type
3195 		,p_instance_id        =>   p_instance_rec.instance_id
3196 		,p_new_instance_id    =>   NULL
3197 		,p_vld_org_id         =>   p_instance_rec.vld_organization_id
3198 		,p_quantity           =>   NULL
3199 		,p_party_account_id1  =>   NULL
3200 		,p_party_account_id2  =>   NULL
3201 		,p_transaction_date   =>   p_txn_rec.transaction_date -- l_transaction_date
3202                 ,p_source_transaction_date   =>   p_txn_rec.source_transaction_date -- Added jpwilson
3203 		,p_txn_type_id        => p_txn_rec.transaction_type_id  --added for BUG# 5752271
3204 		,p_order_line_id      =>   l_order_line_id -- will have a value only when there is a change in order line_id
3205 		,p_oks_txn_inst_tbl   =>   px_oks_txn_inst_tbl
3206 		,x_return_status      =>   x_return_status
3207 		,x_msg_count          =>   x_msg_count
3208 		,x_msg_data           =>   x_msg_data
3209 	       );
3210              --
3211              IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
3212                   l_msg_index := 1;
3213                   l_msg_count := x_msg_count;
3214                 WHILE l_msg_count > 0 LOOP
3215                    x_msg_data := FND_MSG_PUB.GET
3216                               (  l_msg_index,
3217                                  FND_API.G_FALSE
3218                                );
3219                    csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
3220                     l_msg_index := l_msg_index + 1;
3221                     l_msg_count := l_msg_count - 1;
3222                 END LOOP;
3223                 RAISE FND_API.G_EXC_ERROR;
3224              END IF;
3225          END IF; -- Added for bug 3973706
3226       END IF;
3227    END IF;
3228    --
3229    x_return_status := l_tmp_return_status;
3230    --
3231    IF px_oks_txn_inst_tbl.count > 0 THEN
3232       csi_gen_utility_pvt.dump_oks_txn_inst_tbl(px_oks_txn_inst_tbl);
3233       csi_gen_utility_pvt.put_line('Calling OKS Core API...');
3234       --
3235       IF p_txn_rec.transaction_type_id = 3 THEN
3236 	 l_batch_id := p_txn_rec.source_header_ref_id;
3237 	 l_batch_type := p_txn_rec.source_group_ref;
3238       ELSE
3239 	 l_batch_id := NULL;
3240 	 l_batch_type := NULL;
3241       END IF;
3242       --
3243       UPDATE CSI_TRANSACTIONS
3244       set contracts_invoked = 'Y'
3245       where transaction_id = p_txn_rec.transaction_id;
3246       --
3247       OKS_IBINT_PUB.IB_interface
3248 	 (
3249 	   P_Api_Version           =>  1.0,
3250 	   P_init_msg_list         =>  p_init_msg_list,
3251 	   P_single_txn_date_flag  =>  'Y',
3252 	   P_Batch_type            =>  l_batch_type,
3253 	   P_Batch_ID              =>  l_batch_id,
3254 	   P_OKS_Txn_Inst_tbl      =>  px_oks_txn_inst_tbl,
3255 	   x_return_status         =>  x_return_status,
3256 	   x_msg_count             =>  x_msg_count,
3257 	   x_msg_data              =>  x_msg_data
3258 	);
3259       --
3260       IF x_return_status = 'W' THEN -- Warning from OKS
3261 	 l_msg_index := 1;
3262 	 l_msg_count := x_msg_count;
3263 	 WHILE l_msg_count > 0 LOOP
3264 	    x_msg_data := FND_MSG_PUB.GET
3265 		     (  l_msg_index,
3266 			FND_API.G_FALSE
3267 		      );
3268 	    csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
3269 	    l_msg_index := l_msg_index + 1;
3270 	    l_msg_count := l_msg_count - 1;
3271 	 END LOOP;
3272 	 FND_MSG_PUB.Count_And_Get
3273 	 ( p_encoded => FND_API.G_FALSE,
3274                  p_count                 =>      x_msg_count,
3275 	   p_data                  =>      x_msg_data
3276 	 );
3277       ELSIF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
3278 	 l_msg_index := 1;
3279 	 l_msg_count := x_msg_count;
3280 	 WHILE l_msg_count > 0 LOOP
3281 	    x_msg_data := FND_MSG_PUB.GET
3282 		    (  l_msg_index,
3283 		       FND_API.G_FALSE        );
3284 	    csi_gen_utility_pvt.put_line( 'Error from OKS_IBINT_PUB.IB_interface..');
3285 	    csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
3286 	    l_msg_index := l_msg_index + 1;
3287 	    l_msg_count := l_msg_count - 1;
3288 	 END LOOP;
3289 	 RAISE FND_API.G_EXC_ERROR;
3290       END IF;
3291    END IF;
3292    -- End of API body
3293    -- Standard check of p_commit.
3294    IF FND_API.To_Boolean( p_commit ) THEN
3295       COMMIT WORK;
3296    END IF;
3297    /***** srramakr commented for bug # 3304439
3298     -- Check for the profile option and disable the trace
3299     IF (l_flag = 'Y') THEN
3300          dbms_session.set_sql_trace(FALSE);
3301     END IF;
3302         -- End disable trace
3303     ****/
3304     -- Standard call to get message count and if count is  get message info.
3305     FND_MSG_PUB.Count_And_Get
3306        (p_encoded => FND_API.G_FALSE,
3307                  p_count        =>      x_msg_count ,
3308         p_data         =>      x_msg_data
3309        );
3310 
3311  -- Call Post Vertical user Hook
3312   BEGIN
3313        IF JTF_USR_HKS.Ok_to_execute(G_PKG_NAME, l_api_name, 'A', 'V' ) THEN
3314           csi_gen_utility_pvt.put_line('Calling  CSI_ITEM_INSTANCE_VUHK.Update_Item_Instance_Post ..');
3315           CSI_ITEM_INSTANCE_VUHK.Update_Item_Instance_Post
3316           (
3317            p_api_version           =>  1.0
3318           ,p_commit                =>  fnd_api.g_false
3319 	      ,p_init_msg_list         =>  fnd_api.g_false
3320 	      ,p_validation_level      =>  fnd_api.g_valid_level_full
3321 	      ,p_instance_rec          =>  p_instance_rec
3322 	      ,p_ext_attrib_values_tbl =>  p_ext_attrib_values_tbl
3323 	      ,p_party_tbl             =>  p_party_tbl
3324 	      ,p_account_tbl           =>  p_account_tbl
3325 	      ,p_pricing_attrib_tbl    =>  p_pricing_attrib_tbl
3326 	      ,p_org_assignments_tbl   =>  p_org_assignments_tbl
3327 	      ,p_asset_assignment_tbl  =>  p_asset_assignment_tbl
3328 	      ,p_txn_rec               =>  p_txn_rec
3329 	      ,x_instance_id_lst       =>  x_instance_id_lst
3330 	      ,x_return_status         =>  x_return_status
3331 	      ,x_msg_count             =>  x_msg_count
3332 	      ,x_msg_data              =>  x_msg_data
3333 	      );
3334       --
3335           IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
3336               l_msg_index := 1;
3337               l_msg_count := x_msg_count;
3338               WHILE l_msg_count > 0 LOOP
3339                     x_msg_data := FND_MSG_PUB.GET
3340                              (  l_msg_index,
3341                                FND_API.G_FALSE );
3342                      csi_gen_utility_pvt.put_line('ERROR FROM CSI_ITEM_INSTANCE_VUHK.Update_Item_Instance_Post API ');
3343                      csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
3344                     l_msg_index := l_msg_index + 1;
3345                     l_msg_count := l_msg_count - 1;
3346               END LOOP;
3347              RAISE FND_API.G_EXC_ERROR;
3348 	       END IF;
3349        END IF;
3350 
3351   EXCEPTION
3352     WHEN OTHERS THEN
3353        csi_gen_utility_pvt.put_line('ERROR FROM JTF_USR_HKS.Ok_to_execute API Update Post Vertical');
3354        RAISE FND_API.G_EXC_ERROR;
3355   END;
3356    x_return_status := l_tmp_return_status;
3357    -- End of POST User Hooks
3358    --
3359 
3360   -- Begin Add Code for Siebel Genesis Project
3361   csi_gen_utility_pvt.put_line('Source code = '||l_instance_csr.source_code);
3362   csi_gen_utility_pvt.put_line('p_instance_rec.instance_id = '||to_char(p_instance_rec.instance_id));
3363   csi_gen_utility_pvt.put_line('p_instance_rec.source_code = '||p_instance_rec.source_code);
3364 
3365   l_instance_id := p_instance_rec.instance_id;
3366   IF (l_instance_id = NULL OR l_instance_id = FND_API.G_MISS_NUM) THEN
3367     IF p_party_tbl.count > 0 THEN
3368       l_instance_id := p_party_tbl(1).instance_id;
3369     END IF;
3370   END IF;
3371 
3372   BEGIN
3373      SELECT source_code, owner_party_id
3374      INTO   l_source_code, l_owner_party_id
3375      FROM   csi_item_instances
3376      WHERE  instance_id = l_instance_id;
3377   EXCEPTION
3378      WHEN OTHERS THEN NULL;
3379   END;
3380 
3381   IF l_raise_bes_event = 'Y' THEN
3382      csi_gen_utility_pvt.put_line('l_internal_party_id = '||to_char(l_internal_party_id));
3383      csi_gen_utility_pvt.put_line('l_owner_party_id = '||to_char(l_owner_party_id));
3384      csi_gen_utility_pvt.put_line('Before calling update instance event');
3385      IF (l_internal_party_id <> l_owner_party_id OR
3386         (p_txn_rec.transaction_type_id IN (53,54) AND l_create_event_called = 'Y')) THEN -- Bug 7156553, base bug 7155591
3387         csi_gen_utility_pvt.put_line('Before last_oe_order_line_id');
3388         csi_gen_utility_pvt.put_line('last_oe_order_line_id = '||to_char(p_instance_rec.last_oe_order_line_id));
3389         IF l_create_event_called = 'N' THEN
3390            csi_gen_utility_pvt.put_line('Calling Create Instance Event');
3391            CSI_BUSINESS_EVENT_PVT.CREATE_INSTANCE_EVENT
3392               ( p_api_version         => p_api_version
3393                ,p_commit              => fnd_api.g_false
3394                ,p_init_msg_list       => p_init_msg_list
3395                ,p_validation_level    => p_validation_level
3396                ,p_instance_id         => l_instance_id
3397                ,p_subject_instance_id => null
3398                ,x_return_status       => x_return_status
3399                ,x_msg_count           => x_msg_count
3400                ,x_msg_data            => x_msg_data
3401               );
3402 
3403            IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
3404               l_msg_index := 1;
3405               l_msg_count := x_msg_count;
3406 
3407               WHILE l_msg_count > 0 LOOP
3408                  x_msg_data := FND_MSG_PUB.GET(l_msg_index, FND_API.G_FALSE);
3409                  csi_gen_utility_pvt.put_line(' Error from CSI_BUSINESS_EVENT.CREATE_INSTANCE_EVENT');
3410                  csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
3411                  l_msg_index := l_msg_index + 1;
3412                  l_msg_count := l_msg_count - 1;
3413               END LOOP;
3414               RAISE FND_API.G_EXC_ERROR;
3415            END IF;
3416         ELSE
3417            csi_gen_utility_pvt.put_line('Calling Update Instance Event');
3418            CSI_BUSINESS_EVENT_PVT.UPDATE_INSTANCE_EVENT
3419               ( p_api_version         => p_api_version
3420                ,p_commit              => fnd_api.g_false
3421                ,p_init_msg_list       => p_init_msg_list
3422                ,p_validation_level    => p_validation_level
3423                ,p_instance_id         => l_instance_id
3424                ,p_subject_instance_id => null
3425                ,x_return_status       => x_return_status
3426                ,x_msg_count           => x_msg_count
3427                ,x_msg_data            => x_msg_data
3428               );
3429 
3430            IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
3431               l_msg_index := 1;
3432               l_msg_count := x_msg_count;
3433 
3434               WHILE l_msg_count > 0 LOOP
3435                  x_msg_data := FND_MSG_PUB.GET(l_msg_index, FND_API.G_FALSE);
3436                  csi_gen_utility_pvt.put_line(' Error from CSI_BUSINESS_EVENT.UPDATE_INSTANCE_EVENT');
3437                  csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
3438                  l_msg_index := l_msg_index + 1;
3439                  l_msg_count := l_msg_count - 1;
3440               END LOOP;
3441               RAISE FND_API.G_EXC_ERROR;
3442            END IF;
3443         END IF;
3444      END IF;
3445   END IF;
3446   -- End Add Code for Siebel Genesis Project
3447 
3448 EXCEPTION
3449     WHEN FND_API.G_EXC_ERROR THEN
3450        ROLLBACK TO update_item_instance;
3451        x_return_status := FND_API.G_RET_STS_ERROR ;
3452        FND_MSG_PUB.Count_And_Get
3453                 (p_encoded               =>      FND_API.G_FALSE,
3454                  p_count                 =>      x_msg_count,
3455                  p_data                  =>      x_msg_data
3456           );
3457     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3458        ROLLBACK TO update_item_instance;
3459        x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3460        FND_MSG_PUB.Count_And_Get
3461          ( p_encoded => FND_API.G_FALSE,
3462                  p_count => x_msg_count,
3463            p_data  => x_msg_data
3464          );
3465     WHEN OTHERS THEN
3466        ROLLBACK TO update_item_instance;
3467        x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3468        IF FND_MSG_PUB.Check_Msg_Level
3469              (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
3470        THEN
3471           FND_MSG_PUB.Add_Exc_Msg
3472              ( G_PKG_NAME,
3473                l_api_name
3474              );
3475        END IF;
3476        FND_MSG_PUB.Count_And_Get
3477           ( p_encoded => FND_API.G_FALSE,
3478                  p_count => x_msg_count,
3479             p_data  => x_msg_data
3480           );
3481 END update_item_instance;
3482 
3483 /*----------------------------------------------------*/
3484 /* Procedure name: expire_item_instance               */
3485 /* Description :   procedure for                      */
3486 /*                 Expiring an Item Instance          */
3487 /*----------------------------------------------------*/
3488 
3489 PROCEDURE expire_item_instance
3490  (
3491       p_api_version         IN      NUMBER
3492      ,p_commit              IN      VARCHAR2
3493      ,p_init_msg_list       IN      VARCHAR2
3494      ,p_validation_level    IN      NUMBER
3495      ,p_instance_rec        IN      csi_datastructures_pub.instance_rec
3496      ,p_expire_children     IN      VARCHAR2
3497      ,p_txn_rec             IN OUT  NOCOPY csi_datastructures_pub.transaction_rec
3498      ,x_instance_id_lst     OUT     NOCOPY csi_datastructures_pub.id_tbl
3499      ,x_return_status       OUT     NOCOPY VARCHAR2
3500      ,x_msg_count           OUT     NOCOPY NUMBER
3501      ,x_msg_data            OUT     NOCOPY VARCHAR2
3502  )
3503 IS
3504      l_api_name              CONSTANT VARCHAR2(30)     := 'EXPIRE_ITEM_INSTANCE';
3505      l_api_version           CONSTANT NUMBER           := 1.0;
3506      l_debug_level                    NUMBER;
3507      l_flag                           VARCHAR2(1);
3508      l_msg_index                      NUMBER;
3509      l_msg_count                      NUMBER;
3510     -- The following were added for att
3511     l_component_ins_type     VARCHAR2(1):=NULL;
3512     l_config_hdr_id          NUMBER;
3513     l_config_rev_nbr         NUMBER;
3514     l_config_key             csi_utility_grp.config_instance_key;
3515     l_config_valid_status    VARCHAR2(10);
3516     l_no_config_keys         BOOLEAN := FALSE;
3517     l_return_message         VARCHAR2(100);
3518     -- end addition
3519     CURSOR config_ins_key (p_config_hdr_id NUMBER, p_config_rev_nbr NUMBER) is
3520       SELECT instance_hdr_id
3521             ,config_item_id
3522             ,has_failures
3523       FROM   cz_config_details_v d
3524             ,cz_config_hdrs_v h
3525       WHERE d.instance_hdr_id = h.config_hdr_id
3526       AND   d.instance_rev_nbr = h.config_rev_nbr
3527       AND   d.component_instance_type = 'I'
3528       AND   d.config_hdr_id = p_config_hdr_id
3529       AND   d.config_rev_nbr = p_config_rev_nbr;
3530 
3531     CURSOR instance_key (p_config_inst_hdr_id NUMBER, p_config_inst_item_id NUMBER)is
3532       SELECT instance_id
3533             ,object_version_number
3534             ,config_valid_status
3535       FROM   csi_item_instances
3536       WHERE  config_inst_hdr_id=p_config_inst_hdr_id
3537       AND    config_inst_item_id=p_config_inst_item_id;
3538 
3539     CURSOR new_config_ins_key (p_config_ins_hdr_id NUMBER, p_config_ins_rev_nbr NUMBER) is
3540       SELECT has_failures
3541             ,config_status
3542       FROM   cz_config_details_v d
3543             ,cz_config_hdrs h
3544       WHERE d.instance_hdr_id = p_config_ins_hdr_id
3545       AND   d.instance_rev_nbr = p_config_ins_rev_nbr
3546      -- AND   d.component_instance_type = 'I'
3547       AND   d.config_hdr_id = h.config_hdr_id
3548       AND   d.config_rev_nbr = h.config_rev_nbr;
3549 
3550     CURSOR new_instance_key (p_config_inst_hdr_id NUMBER, p_config_inst_item_id NUMBER)is
3551       SELECT instance_id
3552             ,object_version_number
3553             ,config_valid_status
3554       FROM   csi_item_instances
3555       WHERE  config_inst_hdr_id=p_config_inst_hdr_id
3556       AND    config_inst_item_id=p_config_inst_item_id;
3557 
3558       l_config_instance_rec           csi_datastructures_pub.instance_rec ;
3559       l_config_temp_rec               csi_datastructures_pub.instance_rec ;
3560       l_batch_hdr_id                  NUMBER;
3561       l_batch_rev_nbr                 NUMBER;
3562       l_instance_id_lst               csi_datastructures_pub.id_tbl;
3563       l_item_attribute_tbl            csi_item_instance_pvt.item_attribute_tbl;
3564       l_location_tbl                  csi_item_instance_pvt.location_tbl;
3565       l_generic_id_tbl                csi_item_instance_pvt.generic_id_tbl;
3566       l_lookup_tbl                    csi_item_instance_pvt.lookup_tbl;
3567       l_ins_count_rec                 csi_item_instance_pvt.ins_count_rec;
3568       l_config_ins_rec                csi_datastructures_pub.instance_rec;
3569       l_config_tmp_rec                csi_datastructures_pub.instance_rec;
3570       --
3571       px_oks_txn_inst_tbl             OKS_IBINT_PUB.TXN_INSTANCE_TBL;
3572       px_child_inst_tbl               csi_item_instance_grp.child_inst_tbl;
3573       l_batch_type                    VARCHAR2(50);
3574       l_batch_id                      NUMBER;
3575 BEGIN
3576    -- Standard Start of API savepoint
3577    SAVEPOINT  expire_item_instance;
3578 
3579    -- Check for freeze_flag in csi_install_parameters is set to 'Y'
3580 
3581    csi_utility_grp.check_ib_active;
3582 
3583    -- Standard call to check for call compatibility.
3584    IF NOT FND_API.Compatible_API_Call (l_api_version,
3585 				       p_api_version,
3586 				       l_api_name   ,
3587 				       G_PKG_NAME   )
3588    THEN
3589 	   RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3590    END IF;
3591 
3592    -- Initialize message list if p_init_msg_list is set to TRUE.
3593    IF FND_API.to_Boolean( p_init_msg_list ) THEN
3594 	   FND_MSG_PUB.initialize;
3595    END IF;
3596 
3597    --  Initialize API return status to success
3598    x_return_status := FND_API.G_RET_STS_SUCCESS;
3599 
3600    -- Check the profile option debug_level for debug message reporting
3601    l_debug_level:=fnd_profile.value('CSI_DEBUG_LEVEL');
3602 
3603    -- If debug_level = 1 then dump the procedure name
3604    IF (l_debug_level > 0) THEN
3605        csi_gen_utility_pvt.put_line( 'expire_item_instance');
3606    END IF;
3607 
3608    -- If the debug level = 2 then dump all the parameters values.
3609    IF (l_debug_level > 1) THEN
3610       csi_gen_utility_pvt.put_line( 'expire_item_instance:'  ||
3611 					     p_api_version      ||'-'||
3612 					     p_commit           ||'-'||
3613 					     p_init_msg_list    ||'-'||
3614 					     p_validation_level      );
3615       -- Dump the records in the log file
3616       csi_gen_utility_pvt.dump_txn_rec(p_txn_rec);
3617       csi_gen_utility_pvt.dump_instance_rec(p_instance_rec);
3618    END IF;
3619    /***** srramakr commented for bug # 3304439
3620    -- Check for the profile option and enable trace
3621    l_flag:=CSI_GEN_UTILITY_PVT.enable_trace(l_trace_flag => l_flag);
3622    -- End enable trace
3623    ****/
3624    -- Start API body
3625    --
3626         -- Call Pre Customer User Hook
3627    BEGIN
3628       IF JTF_USR_HKS.Ok_to_execute(G_PKG_NAME, l_api_name, 'B', 'C' ) THEN
3629 	 csi_gen_utility_pvt.put_line('Calling  CSI_ITEM_INSTANCE_CUHK.Expire_Item_Instance_Pre ..');
3630 	 CSI_ITEM_INSTANCE_CUHK.Expire_Item_Instance_Pre
3631 	  (
3632               p_api_version         => 1.0
3633 	     ,p_commit              => fnd_api.g_false
3634 	     ,p_init_msg_list       => fnd_api.g_false
3635 	     ,p_validation_level    => fnd_api.g_valid_level_full
3636 	     ,p_instance_rec        => p_instance_rec
3637 	     ,p_expire_children     => fnd_api.g_false
3638 	     ,p_txn_rec             => p_txn_rec
3639 	     ,x_instance_id_lst     => x_instance_id_lst
3640 	     ,x_return_status       => x_return_status
3641 	     ,x_msg_count           => x_msg_count
3642 	     ,x_msg_data            => x_msg_data
3643 	  );
3644 	 IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
3645 	    l_msg_index := 1;
3646 	    l_msg_count := x_msg_count;
3647 	    WHILE l_msg_count > 0 LOOP
3648 		    x_msg_data := FND_MSG_PUB.GET
3649 			    (  l_msg_index,
3650 			       FND_API.G_FALSE );
3651 		  csi_gen_utility_pvt.put_line('ERROR FROM CSI_ITEM_INSTANCE_CUHK.Expire_Item_Instance_Pre API ');
3652 		  csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
3653 		  l_msg_index := l_msg_index + 1;
3654 		  l_msg_count := l_msg_count - 1;
3655 	    END LOOP;
3656 	    RAISE FND_API.G_EXC_ERROR;
3657 	 END IF;
3658       END IF;
3659    EXCEPTION
3660       WHEN OTHERS THEN
3661          csi_gen_utility_pvt.put_line('ERROR FROM JTF_USR_HKS.Ok_to_execute API Expire Pre Customer');
3662          RAISE FND_API.G_EXC_ERROR;
3663    END;
3664    --
3665    -- Call Pre Vertical User Hook
3666    BEGIN
3667       IF JTF_USR_HKS.Ok_to_execute(G_PKG_NAME, l_api_name, 'B', 'V' ) THEN
3668 	 csi_gen_utility_pvt.put_line('Calling  CSI_ITEM_INSTANCE_VUHK.Expire_Item_Instance_Pre ..');
3669 	 CSI_ITEM_INSTANCE_VUHK.Expire_Item_Instance_Pre
3670 	  (
3671 	      p_api_version         => 1.0
3672 	     ,p_commit              => fnd_api.g_false
3673 	     ,p_init_msg_list       => fnd_api.g_false
3674 	     ,p_validation_level    => fnd_api.g_valid_level_full
3675 	     ,p_instance_rec        => p_instance_rec
3676 	     ,p_expire_children     => fnd_api.g_false
3677 	     ,p_txn_rec             => p_txn_rec
3678 	     ,x_instance_id_lst     => x_instance_id_lst
3679 	     ,x_return_status       => x_return_status
3680 	     ,x_msg_count           => x_msg_count
3681 	     ,x_msg_data            => x_msg_data
3682 	  );
3683 	 IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
3684 	    l_msg_index := 1;
3685 	    l_msg_count := x_msg_count;
3686 	    WHILE l_msg_count > 0 LOOP
3687 	       x_msg_data := FND_MSG_PUB.GET
3688 			    (  l_msg_index,
3689 			       FND_API.G_FALSE );
3690 	       csi_gen_utility_pvt.put_line('ERROR FROM CSI_ITEM_INSTANCE_VUHK.Expire_Item_Instance_Pre API ');
3691 	       csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
3692 	       l_msg_index := l_msg_index + 1;
3693 	       l_msg_count := l_msg_count - 1;
3694 	    END LOOP;
3695 	    RAISE FND_API.G_EXC_ERROR;
3696 	 END IF;
3697       END IF;
3698    EXCEPTION
3699       WHEN OTHERS THEN
3700          csi_gen_utility_pvt.put_line('ERROR FROM JTF_USR_HKS.Ok_to_execute API Expire Pre Vertical');
3701          RAISE FND_API.G_EXC_ERROR;
3702    END;
3703    -- End PRE User Hooks
3704    --
3705    csi_item_instance_pvt.expire_item_instance
3706       (
3707         p_api_version      => p_api_version
3708         ,p_commit           => fnd_api.g_false
3709         ,p_init_msg_list    => p_init_msg_list
3710         ,p_validation_level => p_validation_level
3711         ,p_instance_rec     => p_instance_rec
3712         ,p_expire_children  => p_expire_children
3713         ,p_txn_rec          => p_txn_rec
3714         ,x_instance_id_lst  => x_instance_id_lst
3715         ,p_oks_txn_inst_tbl => px_oks_txn_inst_tbl
3716         ,x_return_status    => x_return_status
3717         ,x_msg_count        => x_msg_count
3718         ,x_msg_data         => x_msg_data
3719       );
3720    IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
3721       l_msg_index := 1;
3722       l_msg_count := x_msg_count;
3723       WHILE l_msg_count > 0 LOOP
3724                        x_msg_data := FND_MSG_PUB.GET(
3725                                         l_msg_index,
3726                                         FND_API.G_FALSE
3727                                                 );
3728          csi_gen_utility_pvt.put_line('message data = '||x_msg_data);
3729          l_msg_index := l_msg_index + 1;
3730          l_msg_count := l_msg_count - 1;
3731       END LOOP;
3732                RAISE FND_API.G_EXC_ERROR;
3733    END IF;
3734    --
3735    -- Call Post Customer User Hook
3736    BEGIN
3737       IF JTF_USR_HKS.Ok_to_execute(G_PKG_NAME, l_api_name, 'A', 'C' ) THEN
3738 	 csi_gen_utility_pvt.put_line('Calling  CSI_ITEM_INSTANCE_CUHK.Expire_Item_Instance_Post ..');
3739 	 CSI_ITEM_INSTANCE_CUHK.Expire_Item_Instance_Post
3740 	  (
3741 	      p_api_version         => 1.0
3742 	     ,p_commit              => fnd_api.g_false
3743 	     ,p_init_msg_list       => fnd_api.g_false
3744 	     ,p_validation_level    => fnd_api.g_valid_level_full
3745 	     ,p_instance_rec        => p_instance_rec
3746 	     ,p_expire_children     => fnd_api.g_false
3747 	     ,p_txn_rec             => p_txn_rec
3748 	     ,x_instance_id_lst     => x_instance_id_lst
3749 	     ,x_return_status       => x_return_status
3750 	     ,x_msg_count           => x_msg_count
3751 	     ,x_msg_data            => x_msg_data
3752 	  );
3753 	 IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
3754 	    l_msg_index := 1;
3755 	    l_msg_count := x_msg_count;
3756 	    WHILE l_msg_count > 0 LOOP
3757 		    x_msg_data := FND_MSG_PUB.GET
3758 			    (  l_msg_index,
3759 			       FND_API.G_FALSE );
3760 		  csi_gen_utility_pvt.put_line('ERROR FROM CSI_ITEM_INSTANCE_CUHK.Expire_Item_Instance_Post API ');
3761 		  csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
3762 		  l_msg_index := l_msg_index + 1;
3763 		  l_msg_count := l_msg_count - 1;
3764 	    END LOOP;
3765 	    RAISE FND_API.G_EXC_ERROR;
3766 	 END IF;
3767       END IF;
3768    EXCEPTION
3769       WHEN OTHERS THEN
3770          csi_gen_utility_pvt.put_line('ERROR FROM JTF_USR_HKS.Ok_to_execute API Expire Post Customer');
3771          RAISE FND_API.G_EXC_ERROR;
3772    END;
3773    --
3774 
3775    --
3776    -- Start att enhancements
3777   IF p_instance_rec.call_batch_validation<>fnd_api.g_false
3778   THEN
3779     IF p_instance_rec.instance_id IS NOT NULL AND
3780        p_instance_rec.instance_id <> fnd_api.g_miss_num
3781     THEN
3782        IF    ((p_instance_rec.config_inst_hdr_id IS NULL OR p_instance_rec.config_inst_hdr_id = fnd_api.g_miss_num)
3783          AND  (p_instance_rec.config_inst_item_id IS NULL OR p_instance_rec.config_inst_item_id = fnd_api.g_miss_num)
3784              )
3785        THEN
3786                l_no_config_keys:=TRUE;
3787 		       csi_gen_utility_pvt.put_line( 'Config keys were not provided. So no batch validation will be performed');
3788        END IF;
3789 
3790       IF NOT l_no_config_keys
3791       THEN
3792        IF   (p_instance_rec.config_inst_hdr_id IS NOT NULL AND p_instance_rec.config_inst_hdr_id <> fnd_api.g_miss_num)
3793         AND (p_instance_rec.config_inst_item_id IS NOT NULL AND p_instance_rec.config_inst_item_id <> fnd_api.g_miss_num)
3794         AND (p_instance_rec.config_inst_rev_num IS NOT NULL AND p_instance_rec.config_inst_rev_num <> fnd_api.g_miss_num)
3795        THEN
3796 
3797           IF NOT csi_item_instance_vld_pvt.is_unique_config_key ( p_config_inst_hdr_id  => p_instance_rec.config_inst_hdr_id
3798                                                                  ,p_config_inst_item_id => p_instance_rec.config_inst_item_id
3799                                                                  ,p_instance_id         => p_instance_rec.instance_id
3800                                                                  ,p_validation_mode     => 'UPDATE'
3801                                                                  )
3802           THEN
3803            FND_MESSAGE.SET_NAME('CSI','CSI_CONFIG_KEY_EXISTS');
3804            FND_MSG_PUB.Add;
3805            RAISE fnd_api.g_exc_error;
3806           END IF;
3807 
3808           BEGIN
3809               SELECT component_instance_type
3810                     ,config_hdr_id
3811                     ,config_rev_nbr
3812               INTO   l_component_ins_type
3813                     ,l_config_hdr_id
3814                     ,l_config_rev_nbr
3815               FROM   cz_config_items_v
3816               WHERE  instance_hdr_id = p_instance_rec.config_inst_hdr_id
3817               AND    instance_rev_nbr = p_instance_rec.config_inst_rev_num
3818               AND    config_item_id = p_instance_rec.config_inst_item_id;
3819           EXCEPTION
3820               WHEN OTHERS THEN
3821                FND_MESSAGE.SET_NAME('CSI','CSI_CONFIG_NOT_IN_CZ');
3822                FND_MESSAGE.SET_TOKEN('INSTANCE_HDR_ID',p_instance_rec.config_inst_hdr_id);
3823                FND_MESSAGE.SET_TOKEN('INSTANCE_REV_NBR',p_instance_rec.config_inst_rev_num);
3824                FND_MESSAGE.SET_TOKEN('CONFIG_ITEM_ID',p_instance_rec.config_inst_item_id);
3825 
3826                FND_MSG_PUB.Add;
3827                RAISE FND_API.G_EXC_ERROR;
3828           END;
3829 
3830           IF l_component_ins_type='I'
3831           THEN
3832 
3833             Csi_item_instance_vld_pvt.Call_batch_validate
3834               ( p_instance_rec    => p_instance_rec
3835                ,p_config_hdr_id   => l_config_hdr_id
3836                ,p_config_rev_nbr  => l_config_rev_nbr
3837                ,x_config_hdr_id   => l_batch_hdr_id
3838                ,x_config_rev_nbr  => l_batch_rev_nbr
3839                ,x_return_status   => x_return_status);
3840 
3841             IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
3842                FND_MESSAGE.SET_NAME('CSI','CSI_CONFIG_ERROR');
3843                FND_MSG_PUB.Add;
3844                csi_gen_utility_pvt.put_line('Call to batch validation has errored ');
3845                RAISE FND_API.G_EXC_ERROR;
3846             END IF;
3847           END IF;
3848             -- Commenting the following code in order to maintain status history.
3849        ELSIF (  (p_instance_rec.config_inst_hdr_id IS NULL OR p_instance_rec.config_inst_hdr_id = fnd_api.g_miss_num)
3850             AND (p_instance_rec.config_inst_item_id IS NOT NULL AND p_instance_rec.config_inst_item_id <> fnd_api.g_miss_num))
3851           OR (  (p_instance_rec.config_inst_hdr_id IS NOT NULL AND p_instance_rec.config_inst_hdr_id <> fnd_api.g_miss_num)
3852             AND (p_instance_rec.config_inst_item_id IS NULL OR p_instance_rec.config_inst_item_id = fnd_api.g_miss_num))
3853           OR (  (p_instance_rec.config_inst_hdr_id IS NOT NULL AND p_instance_rec.config_inst_hdr_id <> fnd_api.g_miss_num)
3854             AND (p_instance_rec.config_inst_item_id IS NOT NULL AND p_instance_rec.config_inst_item_id <> fnd_api.g_miss_num)
3855             AND (p_instance_rec.config_inst_rev_num IS NULL OR p_instance_rec.config_inst_rev_num = fnd_api.g_miss_num))
3856        THEN
3857 		       FND_MESSAGE.SET_NAME('CSI','CSI_INVALID_CONFIG_COMB');
3858 		       FND_MSG_PUB.Add;
3859 		       RAISE fnd_api.g_exc_error;
3860        END IF;
3861       END IF;
3862     END IF;
3863   END IF;
3864 -- End att enhancements
3865 -- Adding new changes for bug 3799694
3866     IF   (p_instance_rec.config_inst_hdr_id IS NOT NULL AND p_instance_rec.config_inst_hdr_id <> fnd_api.g_miss_num)
3867         AND (p_instance_rec.config_inst_item_id IS NOT NULL AND p_instance_rec.config_inst_item_id <> fnd_api.g_miss_num)
3868         AND (p_instance_rec.config_inst_rev_num IS NOT NULL AND p_instance_rec.config_inst_rev_num <> fnd_api.g_miss_num)
3869     THEN
3870        FOR l_config_ins_key IN new_config_ins_key(p_instance_rec.config_inst_hdr_id,p_instance_rec.config_inst_rev_num)
3871        LOOP
3872           IF l_config_ins_key.has_failures ='1'
3873           OR nvl(l_config_ins_key.config_status,'0') <> '2'
3874           THEN
3875              FOR l_instance_key IN new_instance_key(p_instance_rec.config_inst_hdr_id,p_instance_rec.config_inst_item_id)
3876              LOOP
3877                 l_config_instance_rec:=l_config_temp_rec;
3878                 IF (l_instance_key.instance_id IS NOT NULL AND
3879                    l_instance_key.instance_id <> fnd_api.g_miss_num) AND
3880                    ( l_instance_key.config_valid_status IS NULL OR
3881                    (l_instance_key.config_valid_status IS NOT NULL AND
3882                    l_instance_key.config_valid_status <> '1'))
3883                 THEN
3884                    l_config_instance_rec.instance_id:=l_instance_key.instance_id;
3885                    l_config_instance_rec.object_version_number:=l_instance_key.object_version_number;
3886                    l_config_instance_rec.config_valid_status:='1'; --INVALID
3887                    --
3888                    csi_item_instance_pvt.update_item_instance
3889                           (
3890                             p_api_version        => p_api_version
3891                            ,p_commit             => fnd_api.g_false
3892                            ,p_init_msg_list      => p_init_msg_list
3893                            ,p_validation_level   => p_validation_level
3894                            ,p_instance_rec       => l_config_instance_rec
3895                            ,p_txn_rec            => p_txn_rec
3896                            ,x_instance_id_lst    => l_instance_id_lst
3897                            ,x_return_status      => x_return_status
3898                            ,x_msg_count          => x_msg_count
3899                            ,x_msg_data           => x_msg_data
3900                            ,p_item_attribute_tbl => l_item_attribute_tbl
3901                            ,p_location_tbl       => l_location_tbl
3902                            ,p_generic_id_tbl     => l_generic_id_tbl
3903                            ,p_lookup_tbl         => l_lookup_tbl
3904                            ,p_ins_count_rec      => l_ins_count_rec
3905                            ,p_oks_txn_inst_tbl   => px_oks_txn_inst_tbl
3906                            ,p_child_inst_tbl     => px_child_inst_tbl
3907                          );
3908 
3909                    IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
3910                       l_msg_index := 1;
3911                       l_msg_count := x_msg_count;
3912                       WHILE l_msg_count > 0 LOOP
3913                              x_msg_data := FND_MSG_PUB.GET
3914                                     (  l_msg_index,
3915                                        FND_API.G_FALSE        );
3916                              csi_gen_utility_pvt.put_line( 'Error from UPDATE_ITEM_INSTANCE_PVT..');
3917                              csi_gen_utility_pvt.put_line( 'while updating config status');
3918                              csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
3919                               l_msg_index := l_msg_index + 1;
3920                               l_msg_count := l_msg_count - 1;
3921                       END LOOP;
3922                       RAISE FND_API.G_EXC_ERROR;
3923                    END IF;
3924                 END IF;
3925              END LOOP;
3926           ELSE
3927              FOR l_instance_key IN new_instance_key(p_instance_rec.config_inst_hdr_id,p_instance_rec.config_inst_item_id)
3928              LOOP
3929                 l_config_instance_rec:=l_config_temp_rec;
3930                 IF (l_instance_key.instance_id IS NOT NULL AND
3931                     l_instance_key.instance_id <> fnd_api.g_miss_num) AND
3932                     ( l_instance_key.config_valid_status IS NULL OR
3933                     (l_instance_key.config_valid_status IS NOT NULL AND
3934                     l_instance_key.config_valid_status <> '0'))
3935                 THEN
3936                    l_config_instance_rec.instance_id:=l_instance_key.instance_id;
3937                    l_config_instance_rec.object_version_number:=l_instance_key.object_version_number;
3938                    l_config_instance_rec.config_valid_status:='0'; --VALID
3939                    csi_item_instance_pvt.update_item_instance
3940                           (
3941                             p_api_version        => p_api_version
3942                            ,p_commit             => fnd_api.g_false
3943                            ,p_init_msg_list      => p_init_msg_list
3944                            ,p_validation_level   => p_validation_level
3945                            ,p_instance_rec       => l_config_instance_rec
3946                            ,p_txn_rec            => p_txn_rec
3947                            ,x_instance_id_lst    => l_instance_id_lst
3948                            ,x_return_status      => x_return_status
3949                            ,x_msg_count          => x_msg_count
3950                            ,x_msg_data           => x_msg_data
3951                            ,p_item_attribute_tbl => l_item_attribute_tbl
3952                            ,p_location_tbl       => l_location_tbl
3953                            ,p_generic_id_tbl     => l_generic_id_tbl
3954                            ,p_lookup_tbl         => l_lookup_tbl
3955                            ,p_ins_count_rec      => l_ins_count_rec
3956                            ,p_oks_txn_inst_tbl   => px_oks_txn_inst_tbl
3957                            ,p_child_inst_tbl     => px_child_inst_tbl
3958                          );
3959 
3960                    IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
3961                       l_msg_index := 1;
3962                       l_msg_count := x_msg_count;
3963                       WHILE l_msg_count > 0 LOOP
3964                              x_msg_data := FND_MSG_PUB.GET
3965                                     (  l_msg_index,
3966                                        FND_API.G_FALSE        );
3967                              csi_gen_utility_pvt.put_line( 'Error from UPDATE_ITEM_INSTANCE_PVT..');
3968                              csi_gen_utility_pvt.put_line( 'while updating config status');
3969                              csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
3970                               l_msg_index := l_msg_index + 1;
3971                               l_msg_count := l_msg_count - 1;
3972                       END LOOP;
3973                       RAISE FND_API.G_EXC_ERROR;
3974                    END IF;
3975                 END IF;
3976              END LOOP;
3977           END IF;
3978        END LOOP;
3979     END IF;
3980     -- End addition of changes for bug 3799694
3981     IF px_oks_txn_inst_tbl.count > 0 THEN
3982        csi_gen_utility_pvt.dump_oks_txn_inst_tbl(px_oks_txn_inst_tbl);
3983        csi_gen_utility_pvt.put_line('Calling OKS Core API...');
3984        --
3985        IF p_txn_rec.transaction_type_id = 3 THEN
3986 	  l_batch_id := p_txn_rec.source_header_ref_id;
3987 	  l_batch_type := p_txn_rec.source_group_ref;
3988        ELSE
3989 	  l_batch_id := NULL;
3990 	  l_batch_type := NULL;
3991        END IF;
3992        --
3993        UPDATE CSI_TRANSACTIONS
3994        set contracts_invoked = 'Y'
3995        where transaction_id = p_txn_rec.transaction_id;
3996        --
3997        OKS_IBINT_PUB.IB_interface
3998 	  (
3999 	    P_Api_Version           =>  1.0,
4000 	    P_init_msg_list         =>  p_init_msg_list,
4001 	    P_single_txn_date_flag  =>  'Y',
4002 	    P_Batch_type            =>  l_batch_type,
4003 	    P_Batch_ID              =>  l_batch_id,
4004 	    P_OKS_Txn_Inst_tbl      =>  px_oks_txn_inst_tbl,
4005 	    x_return_status         =>  x_return_status,
4006 	    x_msg_count             =>  x_msg_count,
4007 	    x_msg_data              =>  x_msg_data
4008 	 );
4009        --
4010        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
4011 	  l_msg_index := 1;
4012 	  l_msg_count := x_msg_count;
4013 	  WHILE l_msg_count > 0 LOOP
4014 	     x_msg_data := FND_MSG_PUB.GET
4015 		     (  l_msg_index,
4016 			FND_API.G_FALSE        );
4017 	     csi_gen_utility_pvt.put_line( 'Error from OKS_IBINT_PUB.IB_interface..');
4018 	     csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
4019 	     l_msg_index := l_msg_index + 1;
4020 	     l_msg_count := l_msg_count - 1;
4021 	  END LOOP;
4022 	  RAISE FND_API.G_EXC_ERROR;
4023        END IF;
4024     END IF;
4025     -- End of API body
4026     -- Standard check of p_commit.
4027     IF FND_API.To_Boolean( p_commit ) THEN
4028        COMMIT WORK;
4029     END IF;
4030     /***** srramakr commented for bug # 3304439
4031     -- Check for the profile option and disable the trace
4032     IF (l_flag = 'Y') THEN
4033        dbms_session.set_sql_trace(FALSE);
4034     END IF;
4035     -- End disable trace
4036     ****/
4037     -- Standard call to get message count and if count is  get message info.
4038     FND_MSG_PUB.Count_And_Get
4039                 (p_encoded => FND_API.G_FALSE,
4040                  p_count        =>      x_msg_count ,
4041                  p_data         =>      x_msg_data   );
4042   -- Call Post Vertical User Hook
4043    BEGIN
4044       IF JTF_USR_HKS.Ok_to_execute(G_PKG_NAME, l_api_name, 'A', 'V' ) THEN
4045 	 csi_gen_utility_pvt.put_line('Calling  CSI_ITEM_INSTANCE_VUHK.Expire_Item_Instance_Post ..');
4046 	 CSI_ITEM_INSTANCE_VUHK.Expire_Item_Instance_Post
4047 	  (
4048 	      p_api_version         => 1.0
4049 	     ,p_commit              => fnd_api.g_false
4050 	     ,p_init_msg_list       => fnd_api.g_false
4051 	     ,p_validation_level    => fnd_api.g_valid_level_full
4052 	     ,p_instance_rec        => p_instance_rec
4053 	     ,p_expire_children     => fnd_api.g_false
4054 	     ,p_txn_rec             => p_txn_rec
4055 	     ,x_instance_id_lst     => x_instance_id_lst
4056 	     ,x_return_status       => x_return_status
4057 	     ,x_msg_count           => x_msg_count
4058 	     ,x_msg_data            => x_msg_data
4059 	  );
4060 	 IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
4061 	    l_msg_index := 1;
4062 	    l_msg_count := x_msg_count;
4063 	    WHILE l_msg_count > 0 LOOP
4064 		    x_msg_data := FND_MSG_PUB.GET
4065 			    (  l_msg_index,
4066 			       FND_API.G_FALSE );
4067 		  csi_gen_utility_pvt.put_line('ERROR FROM CSI_ITEM_INSTANCE_VUHK.Expire_Item_Instance_Post API ');
4068 		  csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
4069 		  l_msg_index := l_msg_index + 1;
4070 		  l_msg_count := l_msg_count - 1;
4071 	    END LOOP;
4072 	    RAISE FND_API.G_EXC_ERROR;
4073 	 END IF;
4074       END IF;
4075    EXCEPTION
4076       WHEN OTHERS THEN
4077          csi_gen_utility_pvt.put_line('ERROR FROM JTF_USR_HKS.Ok_to_execute API Expire Post Vertical');
4078          RAISE FND_API.G_EXC_ERROR;
4079    END;
4080    -- End of POST User Hooks
4081 
4082 EXCEPTION
4083         WHEN FND_API.G_EXC_ERROR THEN
4084                 ROLLBACK TO expire_item_instance;
4085                 x_return_status := FND_API.G_RET_STS_ERROR ;
4086                 FND_MSG_PUB.Count_And_Get
4087                 (       p_encoded => FND_API.G_FALSE,
4088                  p_count                 =>      x_msg_count,
4089                         p_data                  =>      x_msg_data
4090                 );
4091 
4092         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4093                 ROLLBACK TO expire_item_instance;
4094                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4095                 FND_MSG_PUB.Count_And_Get
4096                 (  p_encoded => FND_API.G_FALSE,
4097                  p_count     =>      x_msg_count,
4098                    p_data      =>      x_msg_data  );
4099 
4100         WHEN OTHERS THEN
4101                 ROLLBACK TO expire_item_instance;
4102                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4103                 IF FND_MSG_PUB.Check_Msg_Level
4104                      (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
4105                 THEN
4106                    FND_MSG_PUB.Add_Exc_Msg
4107                     ( G_PKG_NAME, l_api_name );
4108                 END IF;
4109                 FND_MSG_PUB.Count_And_Get
4110                 (  p_encoded => FND_API.G_FALSE,
4111                  p_count    =>      x_msg_count,
4112                    p_data     =>      x_msg_data  );
4113 
4114 END  expire_item_instance;
4115 
4116 /*----------------------------------------------------*/
4117 /* Procedure name: get_item_instances                 */
4118 /* Description :   procedure to                       */
4119 /*                 get an Item Instance               */
4120 /*----------------------------------------------------*/
4121 
4122 PROCEDURE get_item_instances
4123  (
4124       p_api_version          IN  NUMBER
4125      ,p_commit               IN  VARCHAR2
4126      ,p_init_msg_list        IN  VARCHAR2
4127      ,p_validation_level     IN  NUMBER
4128      ,p_instance_query_rec   IN  csi_datastructures_pub.instance_query_rec
4129      ,p_party_query_rec      IN  csi_datastructures_pub.party_query_rec
4130      ,p_account_query_rec    IN  csi_datastructures_pub.party_account_query_rec
4131      ,p_transaction_id       IN  NUMBER
4132      ,p_resolve_id_columns   IN  VARCHAR2
4133      ,p_active_instance_only IN  VARCHAR2
4134      ,x_instance_header_tbl  OUT NOCOPY csi_datastructures_pub.instance_header_tbl
4135      ,x_return_status        OUT NOCOPY VARCHAR2
4136      ,x_msg_count            OUT NOCOPY NUMBER
4137      ,x_msg_data             OUT NOCOPY VARCHAR2
4138     )IS
4139 
4140     l_api_name               CONSTANT VARCHAR2(30)   := 'GET_ITEM_INSTANCES';
4141     l_api_version            CONSTANT NUMBER                 := 1.0;
4142     l_debug_level            NUMBER;
4143     l_instance_rec           csi_datastructures_pub.instance_rec;
4144     l_descriptions_rec       csi_datastructures_pub.instance_rec;
4145     l_txn_rec                csi_datastructures_pub.transaction_rec;
4146     l_flag                   VARCHAR2(1)  :='N';
4147     l_error_code             VARCHAR2(10);
4148     l_return_status          NUMBER;
4149     l_msg_count              NUMBER;
4150     l_txn_id                 NUMBER;
4151     l_msg_index              NUMBER;
4152     l_line_count             NUMBER;
4153     l_cur_get_inst_rel       NUMBER;
4154     l_where_clause           VARCHAR2(20000) := '';
4155     l_instance_header_rec    csi_datastructures_pub.instance_header_rec;
4156     l_party_rec              csi_datastructures_pub.party_rec;
4157     l_party_account_rec      csi_datastructures_pub.party_account_rec;
4158     l_instance_id_list       VARCHAR2(2000):= '';
4159     l_instance_id            NUMBER;
4160     l_rows_processed         NUMBER;
4161     l_count                  NUMBER := 0;
4162     l_select_stmt            VARCHAR2(20000) := '';
4163 
4164 
4165  BEGIN
4166         -- Standard Start of API savepoint
4167         --SAVEPOINT       get_item_instances;
4168 
4169         -- Check for freeze_flag in csi_install_parameters is set to 'Y'
4170 
4171         csi_utility_grp.check_ib_active;
4172 
4173 
4174         -- Standard call to check for call compatibility.
4175         IF NOT FND_API.Compatible_API_Call (l_api_version,
4176                                                     p_api_version,
4177                                                 l_api_name       ,
4178                                                 G_PKG_NAME       )
4179         THEN
4180                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4181         END IF;
4182 
4183     -- Initialize message list if p_init_msg_list is set to TRUE.
4184         IF FND_API.to_Boolean( p_init_msg_list ) THEN
4185                 FND_MSG_PUB.initialize;
4186         END IF;
4187 
4188         --  Initialize API return status to success
4189         x_return_status := FND_API.G_RET_STS_SUCCESS;
4190 
4191     -- Check the profile option debug_level for debug message reporting
4192     l_debug_level:=fnd_profile.value('CSI_DEBUG_LEVEL');
4193 
4194     -- If debug_level = 1 then dump the procedure name
4195     IF (l_debug_level > 0) THEN
4196          csi_gen_utility_pvt.put_line( 'get_item_instances');
4197     END IF;
4198 
4199     -- If the debug level = 2 then dump all the parameters values.
4200     IF (l_debug_level > 1) THEN
4201                    csi_gen_utility_pvt.put_line( 'get_item_instances'          ||
4202                                                  p_api_version           ||'-'||
4203                                                  p_commit                ||'-'||
4204                                                  p_init_msg_list         ||'-'||
4205                                                  p_validation_level            );
4206     --dump the query records into a log file
4207     csi_gen_utility_pvt.dump_instance_query_rec(p_instance_query_rec);
4208     csi_gen_utility_pvt.dump_party_query_rec(p_party_query_rec);
4209     csi_gen_utility_pvt.dump_account_query_rec(p_account_query_rec);
4210     END IF;
4211 
4212     /***** srramakr commented for bug # 3304439
4213     -- Check for the profile option and enable trace
4214     l_flag:=CSI_GEN_UTILITY_PVT.enable_trace(l_trace_flag => l_flag);
4215     -- End enable trace
4216     ****/
4217     -- Start API body
4218 
4219     IF   (p_party_query_rec.instance_party_id                       =  FND_API.G_MISS_NUM)
4220           AND (p_party_query_rec.instance_id                        =  FND_API.G_MISS_NUM)
4221           AND (p_party_query_rec.party_id                           =  FND_API.G_MISS_NUM)
4222           AND (p_party_query_rec.relationship_type_code             =  FND_API.G_MISS_CHAR)
4223           AND (p_account_query_rec.ip_account_id                    =  FND_API.G_MISS_NUM)
4224           AND (p_account_query_rec.instance_party_id                =  FND_API.G_MISS_NUM)
4225           AND (p_account_query_rec.party_account_id                 =  FND_API.G_MISS_NUM)
4226           AND (p_account_query_rec.relationship_type_code           =  FND_API.G_MISS_CHAR)
4227           AND (p_transaction_id IS NULL)
4228           AND (p_instance_query_rec.INSTANCE_ID                     =  FND_API.G_MISS_NUM)
4229           AND (p_instance_query_rec.INVENTORY_ITEM_ID               =  FND_API.G_MISS_NUM)
4230           AND (p_instance_query_rec.INVENTORY_REVISION              =  FND_API.G_MISS_CHAR)
4231           AND (p_instance_query_rec.INV_MASTER_ORGANIZATION_ID      =  FND_API.G_MISS_NUM)
4232           AND (p_instance_query_rec.SERIAL_NUMBER                   =  FND_API.G_MISS_CHAR)
4233           AND (p_instance_query_rec.LOT_NUMBER                      =  FND_API.G_MISS_CHAR)
4234           AND (p_instance_query_rec.UNIT_OF_MEASURE                 =  FND_API.G_MISS_CHAR)
4235           AND (p_instance_query_rec.INSTANCE_CONDITION_ID           =  FND_API.G_MISS_NUM)
4236           AND (p_instance_query_rec.INSTANCE_STATUS_ID              =  FND_API.G_MISS_NUM)
4237           AND (p_instance_query_rec.SYSTEM_ID                       =  FND_API.G_MISS_NUM)
4238           AND (p_instance_query_rec.INSTANCE_TYPE_CODE              =  FND_API.G_MISS_CHAR)
4239           AND (p_instance_query_rec.LOCATION_TYPE_CODE              =  FND_API.G_MISS_CHAR)
4240           AND (p_instance_query_rec.LOCATION_ID                     =  FND_API.G_MISS_NUM)
4241           AND (p_instance_query_rec.INV_ORGANIZATION_ID             =  FND_API.G_MISS_NUM)
4242           AND (p_instance_query_rec.INV_SUBINVENTORY_NAME           =  FND_API.G_MISS_CHAR)
4243           AND (p_instance_query_rec.INV_LOCATOR_ID                  =  FND_API.G_MISS_NUM)
4244           AND (p_instance_query_rec.PA_PROJECT_ID                   =  FND_API.G_MISS_NUM)
4245           AND (p_instance_query_rec.PA_PROJECT_TASK_ID              =  FND_API.G_MISS_NUM)
4246           AND (p_instance_query_rec.IN_TRANSIT_ORDER_LINE_ID        =  FND_API.G_MISS_NUM)
4247           AND (p_instance_query_rec.WIP_JOB_ID                      =  FND_API.G_MISS_NUM)
4248           AND (p_instance_query_rec.PO_ORDER_LINE_ID                =  FND_API.G_MISS_NUM)
4249           AND (p_instance_query_rec.LAST_OE_ORDER_LINE_ID           =  FND_API.G_MISS_NUM)
4250           AND (p_instance_query_rec.LAST_OE_RMA_LINE_ID             =  FND_API.G_MISS_NUM)
4251           AND (p_instance_query_rec.LAST_PO_PO_LINE_ID              =  FND_API.G_MISS_NUM)
4252           AND (p_instance_query_rec.LAST_OE_PO_NUMBER               =  FND_API.G_MISS_CHAR)
4253           AND (p_instance_query_rec.LAST_WIP_JOB_ID                 =  FND_API.G_MISS_NUM)
4254           AND (p_instance_query_rec.LAST_PA_PROJECT_ID              =  FND_API.G_MISS_NUM)
4255           AND (p_instance_query_rec.LAST_PA_TASK_ID                 =  FND_API.G_MISS_NUM)
4256           AND (p_instance_query_rec.LAST_OE_AGREEMENT_ID            =  FND_API.G_MISS_NUM)
4257           AND (p_instance_query_rec.INSTALL_DATE                    =  FND_API.G_MISS_DATE)
4258           AND (p_instance_query_rec.MANUALLY_CREATED_FLAG           =  FND_API.G_MISS_CHAR)
4259           AND (p_instance_query_rec.RETURN_BY_DATE                  =  FND_API.G_MISS_DATE)
4260           AND (p_instance_query_rec.ACTUAL_RETURN_DATE              =  FND_API.G_MISS_DATE)
4261           AND (p_instance_query_rec.CONTRACT_NUMBER                 =  FND_API.G_MISS_CHAR)
4262           AND (p_instance_query_rec.INSTANCE_USAGE_CODE             =  FND_API.G_MISS_CHAR)
4263           AND (p_instance_query_rec.CONFIG_INST_HDR_ID              =  FND_API.G_MISS_NUM)  -- sguthiva added for att
4264           AND (p_instance_query_rec.CONFIG_INST_REV_NUM             =  FND_API.G_MISS_NUM)  -- sguthiva added for att
4265           AND (p_instance_query_rec.CONFIG_INST_ITEM_ID             =  FND_API.G_MISS_NUM)  -- sguthiva added for att
4266           AND (p_instance_query_rec.INSTANCE_DESCRIPTION            =  FND_API.G_MISS_CHAR) -- sguthiva added for att
4267           AND (p_instance_query_rec.OPERATIONAL_STATUS_CODE         =  FND_API.G_MISS_CHAR) -- Addition of columns for FA Integration
4268      THEN
4269 
4270            FND_MESSAGE.Set_Name('CSI', 'CSI_API_INVALID_PARAMETERS');
4271            FND_MSG_PUB.ADD;
4272          RAISE FND_API.G_EXC_ERROR;
4273 
4274      END IF;
4275 
4276      -- Generate the where clause
4277      csi_item_instance_pvt.Gen_Inst_Where_Clause
4278        (    p_instance_query_rec    =>  p_instance_query_rec,
4279             p_party_query_rec       =>  p_party_query_rec,
4280             p_pty_acct_query_rec    =>  p_account_query_rec,
4281             p_transaction_id        =>  p_transaction_id,
4282             x_select_stmt           =>  l_select_stmt ,
4283             p_active_instance_only  =>  p_active_instance_only);
4284 
4285      -- Open the cursor
4286      l_cur_get_inst_rel := dbms_sql.open_cursor;
4287 
4288      --Parse the select statement
4289      dbms_sql.parse(l_cur_get_inst_rel, l_select_stmt , dbms_sql.native);
4290 
4291      -- Bind the variables
4292      csi_item_instance_pvt.Bind_inst_variable(p_instance_query_rec,
4293                                                 p_party_query_rec,
4294                                                 p_account_query_rec,
4295                                                 p_transaction_id,
4296                                                 l_cur_get_inst_rel);
4297 
4298      -- Define output variables
4299      csi_item_instance_pvt.Define_Inst_Columns(l_cur_get_inst_rel,
4300                                                p_instance_query_rec );
4301 
4302      -- execute the select statement
4303      l_rows_processed := dbms_sql.execute(l_cur_get_inst_rel);
4304      -- srramakr Bug # 2636868. Construct_inst_header_rec is called within the loop and
4305      -- x_instance_header_tbl is constructed. This is basically to l_instance_id_list
4306      -- with the bind variable p_instance_id.
4307      LOOP
4308        EXIT WHEN DBMS_SQL.FETCH_ROWS(l_cur_get_inst_rel) = 0;
4309        csi_item_instance_pvt.Get_Inst_Column_Values(l_cur_get_inst_rel,
4310                                                     l_instance_id  );
4311        l_count := l_count + 1;
4312        csi_item_instance_pvt.Construct_inst_header_rec(l_instance_id,
4313                                                        x_instance_header_tbl);
4314       /***** COMMENTED FOR Bug # 2636868 IF l_instance_id_list IS NULL THEN
4315                l_instance_id_list := to_char(l_instance_id);
4316              ELSE
4317                l_instance_id_list := l_instance_id_list||' , '||to_char(l_instance_id);
4318              END IF; *****/
4319      END LOOP;
4320 
4321    -- Close the cursor
4322    DBMS_SQL.CLOSE_CURSOR(l_cur_get_inst_rel);
4323 
4324   /***** COMMENTED FOR Bug # 2636868 IF l_instance_id_list IS NOT NULL THEN
4325          csi_item_instance_pvt.Construct_inst_header_rec(l_instance_id_list,
4326                                                          x_instance_header_tbl);
4327    END IF; *****/
4328    --
4329    -- srramakr Get_Item_Instances is a frequently called API. Unless there is a requirement,
4330    -- no need to get the version label for each of the item instance retreived.
4331    -- If there is a need, call csi_ietm_instance_vld_pvt.Get_Version_label by passing the instance_id
4332    -- and p_time_stamp as null (because this routine gets the current image of item instance).
4333    -- Resolve_id routine has already been modified to get the version label meaning.
4334    --
4335    IF p_resolve_id_columns = fnd_api.g_true THEN
4336       IF x_instance_header_tbl.count > 0 THEN
4337          csi_item_instance_pvt.resolve_id_columns
4338                             (p_instance_header_tbl      =>   x_instance_header_tbl);
4339 
4340   -- Added by sguthiva for att enhancements
4341        csi_Item_Instance_Vld_pvt.get_link_locations
4342                             (p_instance_header_tbl => x_instance_header_tbl,
4343                              x_return_status       => x_return_status
4344                             );
4345        IF NOT x_return_status = FND_API.G_RET_STS_SUCCESS THEN
4346 	  RAISE FND_API.G_EXC_ERROR;
4347        END IF;
4348   -- End addition by sguthiva for att enhancements
4349 
4350       END IF;
4351    END IF;
4352 
4353    -- End of API body
4354    -- Standard check of p_commit.
4355    /*
4356    IF FND_API.To_Boolean( p_commit ) THEN
4357                 COMMIT WORK;
4358    END IF;
4359    */
4360 
4361    /***** srramakr commented for bug # 3304439
4362    -- Check for the profile option and disable the trace
4363    IF (l_flag = 'Y') THEN
4364           dbms_session.set_sql_trace(FALSE);
4365    END IF;
4366    -- End disable trace
4367    ****/
4368 
4369    -- Standard call to get message count and if count is  get message info.
4370    FND_MSG_PUB.Count_And_Get
4371                 (p_encoded => FND_API.G_FALSE,
4372                  p_count        =>      x_msg_count ,
4373          p_data         =>      x_msg_data
4374                 );
4375 
4376 EXCEPTION
4377 
4378         WHEN FND_API.G_EXC_ERROR THEN
4379               --  ROLLBACK TO get_item_instances;
4380                 x_return_status := FND_API.G_RET_STS_ERROR ;
4381                 FND_MSG_PUB.Count_And_Get
4382                 (       p_encoded => FND_API.G_FALSE,
4383                  p_count                 =>      x_msg_count,
4384                         p_data                  =>      x_msg_data
4385                 );
4386 
4387         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4388               --  ROLLBACK TO get_item_instances;
4389                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4390                 FND_MSG_PUB.Count_And_Get
4391                 (  p_encoded => FND_API.G_FALSE,
4392                  p_count     =>      x_msg_count,
4393                    p_data      =>      x_msg_data  );
4394 
4395         WHEN OTHERS THEN
4396              --   ROLLBACK TO get_item_instances;
4397                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4398                 IF FND_MSG_PUB.Check_Msg_Level
4399                      (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
4400                 THEN
4401                    FND_MSG_PUB.Add_Exc_Msg
4402                     ( G_PKG_NAME, l_api_name );
4403                 END IF;
4404                 FND_MSG_PUB.Count_And_Get
4405                 (  p_encoded => FND_API.G_FALSE,
4406                  p_count    =>      x_msg_count,
4407                    p_data     =>      x_msg_data  );
4408 
4409 END get_item_instances;
4410 
4411 /*----------------------------------------------------*/
4412 /* Procedure name: get_item_instance_details          */
4413 /* Description :   procedure to                       */
4414 /*                 get an Item Instance details       */
4415 /*----------------------------------------------------*/
4416 
4417  PROCEDURE get_item_instance_details
4418  (
4419       p_api_version              IN      NUMBER
4420      ,p_commit                   IN      VARCHAR2
4421      ,p_init_msg_list            IN      VARCHAR2
4422      ,p_validation_level         IN      NUMBER
4423      ,p_instance_rec             IN OUT  NOCOPY csi_datastructures_pub.instance_header_rec
4424      ,p_get_parties              IN      VARCHAR2
4425      ,p_party_header_tbl         OUT     NOCOPY csi_datastructures_pub.party_header_tbl
4426      ,p_get_accounts             IN      VARCHAR2
4427      ,p_account_header_tbl       OUT     NOCOPY csi_datastructures_pub.party_account_header_tbl
4428      ,p_get_org_assignments      IN      VARCHAR2
4429      ,p_org_header_tbl           OUT     NOCOPY csi_datastructures_pub.org_units_header_tbl
4430      ,p_get_pricing_attribs      IN      VARCHAR2
4431      ,p_pricing_attrib_tbl       OUT     NOCOPY csi_datastructures_pub.pricing_attribs_tbl
4432      ,p_get_ext_attribs          IN      VARCHAR2
4433      ,p_ext_attrib_tbl           OUT     NOCOPY csi_datastructures_pub.extend_attrib_values_tbl
4434      ,p_ext_attrib_def_tbl       OUT     NOCOPY csi_datastructures_pub.extend_attrib_tbl --added
4435      ,p_get_asset_assignments    IN      VARCHAR2
4436      ,p_asset_header_tbl         OUT     NOCOPY csi_datastructures_pub.instance_asset_header_tbl
4437      ,p_resolve_id_columns       IN      VARCHAR2
4438      ,p_time_stamp               IN      DATE
4439      ,x_return_status            OUT     NOCOPY VARCHAR2
4440      ,x_msg_count                OUT     NOCOPY NUMBER
4441      ,x_msg_data                 OUT     NOCOPY VARCHAR2
4442 )
4443  IS
4444      l_api_name                 CONSTANT VARCHAR2(30)   := 'GET_ITEM_INSTANCE_DETAILS';
4445      l_api_version              CONSTANT NUMBER         := 1.0;
4446      l_debug_level                       NUMBER;
4447      l_party_query_rec          csi_datastructures_pub.party_query_rec;
4448      l_account_query_rec        csi_datastructures_pub.party_account_query_rec;
4449      l_org_unit_query_rec       csi_datastructures_pub.organization_unit_query_rec;
4450      l_pricing_attrib_query_rec csi_datastructures_pub.pricing_attribs_query_rec;
4451      l_extend_attrib_query_rec  csi_datastructures_pub.extend_attrib_query_rec;
4452      l_instance_asset_query_rec csi_datastructures_pub.instance_asset_query_rec;
4453      l_version_label_query_rec  csi_datastructures_pub.version_label_query_rec;
4454      l_account_header_tbl       csi_datastructures_pub.party_account_header_tbl;
4455      l_acct_header_row          NUMBER;
4456      l_flag                     VARCHAR2(1)  :='N';
4457      l_msg_data                 VARCHAR2(100);
4458      l_msg_count                NUMBER;
4459      l_msg_index                NUMBER;
4460      l_cur_get_instance_rel     NUMBER;
4461      l_where_clause             VARCHAR2(20000)         := '';
4462      l_instance_rec             csi_datastructures_pub.instance_header_rec;
4463      l_instance_header_tbl      csi_datastructures_pub.instance_header_tbl;
4464      l_rows_processed           NUMBER;
4465      l_count                    NUMBER                  := 0;
4466      l_instance_query_rec       csi_datastructures_pub.instance_query_rec;
4467      --Modified the select for R12 build to resolve column sequencing issue in table--
4468      l_select_stmt              VARCHAR2(20000) := ' select instance_id,instance_number, external_reference, '||
4469                                 'inventory_item_id,inventory_revision,inv_master_organization_id,serial_number, '||
4470                                 'mfg_serial_number_flag,lot_number,quantity,unit_of_measure,accounting_class_code, '||
4471                                 'instance_condition_id,instance_status_id,customer_view_flag,merchant_view_flag, '||
4472                                 'sellable_flag,system_id,instance_type_code,active_start_date,active_end_date, '||
4473                                 'location_type_code,location_id,inv_organization_id,inv_subinventory_name,inv_locator_id, '||
4474                                 'pa_project_id,pa_project_task_id,in_transit_order_line_id,wip_job_id,po_order_line_id, '||
4475                                 'last_oe_order_line_id,last_oe_rma_line_id,last_po_po_line_id,last_oe_po_number, '||
4476                                 'last_wip_job_id,last_pa_project_id,last_pa_task_id,last_oe_agreement_id,install_date, '||
4477                                 'manually_created_flag,return_by_date,actual_return_date,creation_complete_flag, '||
4478                                 'completeness_flag,context,attribute1,attribute2,attribute3,attribute4,attribute5, '||
4479                                 'attribute6,attribute7,attribute8,attribute9,attribute10,attribute11,attribute12, '||
4480                                 'attribute13,attribute14,attribute15,object_version_number,last_txn_line_detail_id, '||
4481                                 'install_location_type_code,install_location_id,instance_usage_code,last_vld_organization_id, '||
4482                                 'config_inst_hdr_id,config_inst_rev_num,config_inst_item_id,config_valid_status, '||
4483                                 'instance_description,network_asset_flag,maintainable_flag,pn_location_id, '||
4484                                 'asset_criticality_code,category_id,equipment_gen_object_id,instantiation_flag, '||
4485                                 'linear_location_id,operational_log_flag,checkin_status,supplier_warranty_exp_date,attribute16, '||
4486                                 'attribute17,attribute18,attribute19,attribute20,attribute21,attribute22,attribute23, '||
4487                                 'attribute24,attribute25,attribute26,attribute27,attribute28,attribute29,attribute30, '||
4488                                  -- Addition of columns for FA Integration
4489                                 'purchase_unit_price, purchase_currency_code, payables_unit_price, payables_currency_code, '||
4490                                 'sales_unit_price, sales_currency_code, operational_status_code '||
4491                                  -- End addition of columns for FA Integration
4492                                 'from csi_item_instances where instance_id = :instance_id ';
4493      l_last_purge_date          DATE;
4494      l_time_stamp               DATE;
4495 
4496 BEGIN
4497 
4498         -- Standard Start of API savepoint
4499        -- SAVEPOINT       get_item_instance_details;
4500 
4501 
4502         -- Check for freeze_flag in csi_install_parameters is set to 'Y'
4503 
4504         csi_utility_grp.check_ib_active;
4505 
4506         -- Standard call to check for call compatibility.
4507         IF NOT FND_API.Compatible_API_Call (l_api_version,
4508                                                     p_api_version,
4509                                                 l_api_name       ,
4510                                                 G_PKG_NAME       )
4511         THEN
4512                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4513         END IF;
4514 
4515     -- Initialize message list if p_init_msg_list is set to TRUE.
4516         IF FND_API.to_Boolean( p_init_msg_list ) THEN
4517                 FND_MSG_PUB.initialize;
4518         END IF;
4519 
4520 
4521         --  Initialize API return status to success
4522         x_return_status := FND_API.G_RET_STS_SUCCESS;
4523 
4524         -- Check the profile option debug_level for debug message reporting
4525         l_debug_level:=fnd_profile.value('CSI_DEBUG_LEVEL');
4526 
4527         -- If debug_level = 1 then dump the procedure name
4528         IF (l_debug_level > 0) THEN
4529             csi_gen_utility_pvt.put_line( 'get_item_instance_details');
4530         END IF;
4531 
4532         -- If the debug level = 2 then dump all the parameters values.
4533         IF (l_debug_level > 1) THEN
4534             csi_gen_utility_pvt.put_line( 'get_item_instance_details:'  ||
4535                                           p_api_version           ||'-'||
4536                                                           p_commit                ||'-'||
4537                                                           p_init_msg_list         ||'-'||
4538                                           p_validation_level            );
4539         --dump the queried records into a log file
4540         csi_gen_utility_pvt.dump_instance_header_rec(p_instance_rec);
4541         END IF;
4542 
4543         /***** srramakr commented for bug # 3304439
4544         -- Check for the profile option and enable trace
4545         l_flag:=CSI_GEN_UTILITY_PVT.enable_trace(l_trace_flag => l_flag);
4546         -- End enable trace
4547         ****/
4548         -- Start API body
4549 
4550        -- Open the cursor
4551        l_cur_get_instance_rel := dbms_sql.open_cursor;
4552 
4553        --Parse the select statement
4554        dbms_sql.parse(l_cur_get_instance_rel, l_select_stmt , dbms_sql.native);
4555 
4556        -- Bind the variables
4557        csi_item_instance_pvt.Bind_instance_variable(p_instance_rec,
4558                                                     l_cur_get_instance_rel);
4559 
4560        -- Define output variables
4561        csi_item_instance_pvt.Define_Instance_Columns(l_cur_get_instance_rel);
4562 
4563        -- execute the select statement
4564        l_rows_processed := dbms_sql.execute(l_cur_get_instance_rel);
4565 
4566        LOOP
4567        EXIT WHEN DBMS_SQL.FETCH_ROWS(l_cur_get_instance_rel) = 0;
4568              csi_item_instance_pvt.Get_Instance_Col_Values(l_cur_get_instance_rel,
4569                                                            l_instance_rec
4570                                                            );
4571              p_instance_rec := l_instance_rec;
4572        END LOOP;
4573 
4574        -- Close the cursor
4575        DBMS_SQL.CLOSE_CURSOR(l_cur_get_instance_rel);
4576 
4577       --
4578       -- Get the last purge date from csi_item_instances table
4579       --
4580       BEGIN
4581         SELECT last_purge_date
4582         INTO   l_last_purge_date
4583         FROM   CSI_ITEM_INSTANCES
4584         WHERE  instance_id = p_instance_rec.instance_id;
4585       EXCEPTION
4586         WHEN no_data_found THEN
4587              null;
4588         WHEN others THEN
4589              null;
4590       END;
4591 
4592       l_time_stamp := p_time_stamp;
4593       --call to get the history data
4594       IF ((p_time_stamp IS NOT NULL) AND (p_time_stamp <> FND_API.G_MISS_DATE))
4595       THEN
4596           IF ((l_last_purge_date IS NOT NULL) AND (p_time_stamp <= l_last_purge_date))
4597           THEN
4598                -- If the user is requesting the instance history before the purge date
4599                -- then display a warning message
4600                   csi_gen_utility_pvt.put_line('Warning! History for this entity has already been purged for the datetime stamp passed. ' ||
4601                   'Please provide a valid datetime stamp.');
4602                   FND_MESSAGE.Set_Name('CSI', 'CSI_API_HIST_AFTER_PURGE_REQ');
4603                   FND_MESSAGE.Set_Token('LAST_PURGE_DATE', l_last_purge_date);
4604                   FND_MSG_PUB.ADD;
4605            END IF;
4606            IF     (p_time_stamp <= sysdate) THEN
4607                -- construct from the history if the p_time_stamp
4608                -- is < than sysdate
4609                   csi_item_instance_pvt.Construct_inst_from_hist(p_instance_rec, l_time_stamp);
4610            ELSE
4611                   FND_MESSAGE.Set_Name('CSI', 'CSI_API_INVALID_HIST_PARAMS');
4612                   FND_MSG_PUB.ADD;
4613                   RAISE FND_API.G_EXC_ERROR;
4614            END IF;
4615       END IF;
4616       --
4617       -- srramakr Bug 4558115
4618       p_instance_rec.version_label := csi_item_instance_vld_pvt.Get_Version_Label
4619                                              ( p_instance_id      =>  p_instance_rec.instance_id,
4620                                                p_time_stamp       =>  p_time_stamp
4621                                              );
4622       --Resolve the id columns, get the corresponding descriptions for the passed ids
4623 
4624       IF p_resolve_id_columns = fnd_api.g_true THEN
4625 
4626          l_instance_header_tbl(1) := p_instance_rec;
4627 
4628               csi_item_instance_pvt.resolve_id_columns
4629                             (p_instance_header_tbl      =>   l_instance_header_tbl);
4630 
4631                  p_instance_rec := l_instance_header_tbl(1);
4632       END IF;
4633 
4634       IF p_get_parties = fnd_api.g_true THEN
4635 
4636         l_party_query_rec.instance_id := p_instance_rec.instance_id;
4637         csi_party_relationships_pub.get_inst_party_relationships
4638          (
4639           p_api_version             => p_api_version
4640          ,p_commit                  => fnd_api.g_false
4641          ,p_init_msg_list           => fnd_api.g_false
4642          ,p_validation_level        => fnd_api.g_valid_level_full
4643          ,p_party_query_rec         => l_party_query_rec
4644          ,p_resolve_id_columns      => p_resolve_id_columns --fnd_api.g_true
4645          ,p_time_stamp              => l_time_stamp
4646          ,x_party_header_tbl        => p_party_header_tbl
4647          ,x_return_status           => x_return_status
4648          ,x_msg_count               => x_msg_count
4649          ,x_msg_data                => x_msg_data
4650          );
4651        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
4652                   l_msg_index := 1;
4653                   l_msg_count := x_msg_count;
4654          WHILE l_msg_count > 0 LOOP
4655                   x_msg_data := FND_MSG_PUB.GET
4656                           (  l_msg_index,
4657                         FND_API.G_FALSE );
4658           csi_gen_utility_pvt.put_line( ' Error from csi_party_relationships_pub.get_inst_party_relationships.. ');
4659           csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
4660               l_msg_index := l_msg_index + 1;
4661               l_msg_count := l_msg_count - 1;
4662              END LOOP;
4663          RAISE FND_API.G_EXC_ERROR;
4664        END IF;
4665       END IF;
4666 
4667       IF p_get_accounts = fnd_api.g_true AND
4668          p_party_header_tbl.count > 0 THEN -- without this condition if p_party_header_tbl is blindly
4669                                            -- accessed, we will get an unhandled exception.
4670 
4671        FOR tab_row IN p_party_header_tbl.FIRST..p_party_header_tbl.LAST LOOP
4672           IF p_party_header_tbl.EXISTS(tab_row) THEN
4673              l_account_query_rec.instance_party_id  := p_party_header_tbl(tab_row).instance_party_id;
4674           END IF;
4675 
4676          csi_party_relationships_pub.get_inst_party_accounts
4677          (
4678           p_api_version             => p_api_version
4679          ,p_commit                  => fnd_api.g_false
4680          ,p_init_msg_list           => fnd_api.g_false
4681          ,p_validation_level        => fnd_api.g_valid_level_full
4682          ,p_account_query_rec       => l_account_query_rec
4683          ,p_resolve_id_columns      => p_resolve_id_columns --fnd_api.g_true
4684          ,p_time_stamp              => l_time_stamp
4685          ,x_account_header_tbl      => l_account_header_tbl
4686          ,x_return_status           => x_return_status
4687          ,x_msg_count               => x_msg_count
4688          ,x_msg_data                => x_msg_data         );
4689        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
4690                   l_msg_index := 1;
4691                   l_msg_count := x_msg_count;
4692          WHILE l_msg_count > 0 LOOP
4693                   x_msg_data := FND_MSG_PUB.GET
4694                           (  l_msg_index,
4695                         FND_API.G_FALSE );
4696           csi_gen_utility_pvt.put_line( ' Error from csi_party_relationships_pub.get_inst_party_accounts.. ');
4697           csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
4698               l_msg_index := l_msg_index + 1;
4699               l_msg_count := l_msg_count - 1;
4700              END LOOP;
4701          RAISE FND_API.G_EXC_ERROR;
4702        END IF;
4703             IF (l_account_header_tbl.count > 0) THEN
4704                 IF (p_account_header_tbl.count = 0) THEN
4705                   l_acct_header_row := 1;
4706                 ELSE
4707                   l_acct_header_row := p_account_header_tbl.LAST + 1;
4708                 END IF;
4709 
4710                 FOR acct_header_row  IN l_account_header_tbl.FIRST .. l_account_header_tbl.LAST
4711                 LOOP
4712                       IF l_account_header_tbl.EXISTS(acct_header_row) THEN
4713                          p_account_header_tbl(l_acct_header_row) := l_account_header_tbl(acct_header_row);
4714                          l_acct_header_row := l_acct_header_row  + 1;
4715                       END IF;
4716                 END LOOP;
4717              END IF;
4718 
4719        END LOOP;
4720       END IF;
4721 
4722       IF p_get_org_assignments = fnd_api.g_true THEN
4723 
4724        l_org_unit_query_rec.instance_id := p_instance_rec.instance_id;
4725        csi_organization_unit_pub.get_organization_unit
4726          (
4727           p_api_version             => p_api_version
4728          ,p_commit                  => fnd_api.g_false
4729          ,p_init_msg_list           => fnd_api.g_false
4730          ,p_validation_level        => fnd_api.g_valid_level_full
4731          ,p_ou_query_rec            => l_org_unit_query_rec
4732          ,p_resolve_id_columns      => p_resolve_id_columns --fnd_api.g_true
4733          ,p_time_stamp              => l_time_stamp
4734          ,x_org_unit_tbl            => p_org_header_tbl
4735          ,x_return_status           => x_return_status
4736          ,x_msg_count               => x_msg_count
4737          ,x_msg_data                => x_msg_data
4738          );
4739        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
4740                   l_msg_index := 1;
4741                   l_msg_count := x_msg_count;
4742          WHILE l_msg_count > 0 LOOP
4743                   x_msg_data := FND_MSG_PUB.GET
4744                           (  l_msg_index,
4745                         FND_API.G_FALSE );
4746           csi_gen_utility_pvt.put_line( ' Error from csi_organization_unit_pub.get_organization_unit.. ');
4747           csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
4748               l_msg_index := l_msg_index + 1;
4749               l_msg_count := l_msg_count - 1;
4750              END LOOP;
4751          RAISE FND_API.G_EXC_ERROR;
4752        END IF;
4753       END IF;
4754 
4755       IF p_get_pricing_attribs = fnd_api.g_true THEN
4756 
4757        l_pricing_attrib_query_rec.instance_id := p_instance_rec.instance_id;
4758        csi_pricing_attribs_pub.get_pricing_attribs
4759          (
4760           p_api_version                 => p_api_version
4761          ,p_commit                      => fnd_api.g_false
4762          ,p_init_msg_list               => fnd_api.g_false
4763          ,p_validation_level            => fnd_api.g_valid_level_full
4764          ,p_pricing_attribs_query_rec   => l_pricing_attrib_query_rec
4765          ,p_time_stamp                  => l_time_stamp
4766          ,x_pricing_attribs_tbl         => p_pricing_attrib_tbl
4767          ,x_return_status               => x_return_status
4768          ,x_msg_count                   => x_msg_count
4769          ,x_msg_data                    => x_msg_data
4770          );
4771        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
4772                   l_msg_index := 1;
4773                   l_msg_count := x_msg_count;
4774          WHILE l_msg_count > 0 LOOP
4775                   x_msg_data := FND_MSG_PUB.GET
4776                           (  l_msg_index,
4777                         FND_API.G_FALSE );
4778           csi_gen_utility_pvt.put_line( ' Error from csi_pricing_attribs_pub.get_pricing_attribs.. ');
4779           csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
4780               l_msg_index := l_msg_index + 1;
4781               l_msg_count := l_msg_count - 1;
4782              END LOOP;
4783          RAISE FND_API.G_EXC_ERROR;
4784        END IF;
4785       END IF;
4786 
4787       IF p_get_ext_attribs = fnd_api.g_true THEN
4788 
4789        l_extend_attrib_query_rec.instance_id := p_instance_rec.instance_id;
4790         get_extended_attrib_values
4791         (
4792           p_api_version                 => p_api_version
4793          ,p_commit                      => fnd_api.g_false
4794          ,p_init_msg_list               => fnd_api.g_false
4795          ,p_validation_level            => fnd_api.g_valid_level_full
4796          ,p_ext_attribs_query_rec       => l_extend_attrib_query_rec
4797          ,p_time_stamp                  => l_time_stamp
4798          ,x_ext_attrib_tbl              => p_ext_attrib_tbl
4799          ,x_ext_attrib_def_tbl          => p_ext_attrib_def_tbl  -- added
4800          ,x_return_status               => x_return_status
4801          ,x_msg_count                   => x_msg_count
4802          ,x_msg_data                    => x_msg_data
4803         );
4804        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
4805                   l_msg_index := 1;
4806                   l_msg_count := x_msg_count;
4807          WHILE l_msg_count > 0 LOOP
4808                   x_msg_data := FND_MSG_PUB.GET
4809                           (  l_msg_index,
4810                         FND_API.G_FALSE );
4811           csi_gen_utility_pvt.put_line( ' Error from get_extended_attrib_values.. ');
4812           csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
4813               l_msg_index := l_msg_index + 1;
4814               l_msg_count := l_msg_count - 1;
4815              END LOOP;
4816          RAISE FND_API.G_EXC_ERROR;
4817        END IF;
4818       END IF;
4819 
4820       IF p_get_asset_assignments = fnd_api.g_true THEN
4821 
4822        l_instance_asset_query_rec.instance_id := p_instance_rec.instance_id;
4823         csi_asset_pvt.get_instance_assets
4824         (
4825           p_api_version                 => p_api_version
4826          ,p_commit                      => fnd_api.g_false
4827          ,p_init_msg_list               => fnd_api.g_false
4828          ,p_validation_level            => fnd_api.g_valid_level_full
4829          ,p_instance_asset_query_rec    => l_instance_asset_query_rec
4830          ,p_resolve_id_columns          => p_resolve_id_columns --fnd_api.g_true
4831          ,p_time_stamp                  => l_time_stamp
4832          ,x_instance_asset_tbl          => p_asset_header_tbl
4833          ,x_return_status               => x_return_status
4834          ,x_msg_count                   => x_msg_count
4835          ,x_msg_data                    => x_msg_data
4836         );
4837        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
4838                   l_msg_index := 1;
4839                   l_msg_count := x_msg_count;
4840          WHILE l_msg_count > 0 LOOP
4841                   x_msg_data := FND_MSG_PUB.GET
4842                           (  l_msg_index,
4843                         FND_API.G_FALSE );
4844           csi_gen_utility_pvt.put_line( ' Error from csi_asset_pvt.get_instance_assets.. ');
4845           csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
4846               l_msg_index := l_msg_index + 1;
4847               l_msg_count := l_msg_count - 1;
4848              END LOOP;
4849          RAISE FND_API.G_EXC_ERROR;
4850        END IF;
4851       END IF;
4852 
4853         -- End of API body
4854 
4855     -- Standard check of p_commit.
4856     /*
4857         IF FND_API.To_Boolean( p_commit ) THEN
4858                 COMMIT WORK;
4859         END IF;
4860         */
4861 
4862    /***** srramakr commented for bug # 3304439
4863    -- Check for the profile option and disable the trace
4864     IF (l_flag = 'Y') THEN
4865           dbms_session.set_sql_trace(FALSE);
4866     END IF;
4867     -- End disable trace
4868     ****/
4869 
4870         -- Standard call to get message count and if count is  get message info.
4871         FND_MSG_PUB.Count_And_Get
4872                 (p_encoded => FND_API.G_FALSE,
4873                  p_count        =>      x_msg_count ,
4874          p_data         =>      x_msg_data
4875                 );
4876 
4877 EXCEPTION
4878 
4879     WHEN FND_API.G_EXC_ERROR THEN
4880 
4881                -- ROLLBACK TO get_item_instance_details;
4882                 x_return_status := FND_API.G_RET_STS_ERROR ;
4883                 FND_MSG_PUB.Count_And_Get
4884                 (       p_encoded => FND_API.G_FALSE,
4885                  p_count                 =>      x_msg_count,
4886                         p_data                  =>      x_msg_data
4887                 );
4888 
4889         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4890 
4891                -- ROLLBACK TO get_item_instance_details;
4892                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4893                 FND_MSG_PUB.Count_And_Get
4894                 (       p_encoded => FND_API.G_FALSE,
4895                  p_count                 =>      x_msg_count,
4896                         p_data                  =>      x_msg_data
4897                 );
4898     WHEN OTHERS THEN
4899 
4900         IF DBMS_SQL.IS_OPEN(l_cur_get_instance_rel) THEN
4901            DBMS_SQL.CLOSE_CURSOR(l_cur_get_instance_rel);
4902         END IF;
4903        -- ROLLBACK TO get_item_instance_details;
4904         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4905           IF  FND_MSG_PUB.Check_Msg_Level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
4906               FND_MSG_PUB.Add_Exc_Msg
4907               ( G_PKG_NAME  ,
4908                 l_api_name  );
4909           END IF;
4910               FND_MSG_PUB.Count_And_Get
4911               ( p_encoded => FND_API.G_FALSE,
4912                  p_count     =>      x_msg_count,
4913                 p_data      =>      x_msg_data  );
4914 
4915 END get_item_instance_details;
4916 
4917 
4918 
4919 /*----------------------------------------------------*/
4920 /* Pocedure name:  get_version_label                  */
4921 /* Description :   procedure for creating             */
4922 /*                 version label for                  */
4923 /*                 an Item Instance                   */
4924 /*----------------------------------------------------*/
4925 
4926 PROCEDURE get_version_labels
4927  (    p_api_version             IN  NUMBER
4928      ,p_commit                  IN  VARCHAR2
4929      ,p_init_msg_list           IN  VARCHAR2
4930      ,p_validation_level        IN  NUMBER
4931      ,p_version_label_query_rec IN  csi_datastructures_pub.version_label_query_rec
4932      ,p_time_stamp              IN  DATE
4933      ,x_version_label_tbl       OUT NOCOPY csi_datastructures_pub.version_label_tbl
4934      ,x_return_status           OUT NOCOPY VARCHAR2
4935      ,x_msg_count               OUT NOCOPY NUMBER
4936      ,x_msg_data                OUT NOCOPY VARCHAR2
4937     )IS
4938 
4939       l_api_name                CONSTANT VARCHAR2(30)   := 'GET_VERSION_LABEL';
4940       l_api_version             CONSTANT NUMBER             := 1.0;
4941       l_debug_level             NUMBER;
4942       l_version_label_id    NUMBER;
4943       l_version_label_tbl   csi_datastructures_pub.version_label_tbl;
4944       l_line_count          NUMBER;
4945       l_msg_index           NUMBER;
4946       l_count               NUMBER := 0;
4947       l_where_clause        VARCHAR2(2000) ;
4948       l_get_ver_cursor_id   NUMBER ;
4949       l_flag                VARCHAR2(1)              :='N';
4950       l_rows_processed      NUMBER ;
4951       l_version_label_rec   csi_datastructures_pub.version_label_rec;
4952       l_select_stmt         VARCHAR2(20000) := ' SELECT * FROM CSI_I_VERSION_LABELS  ';
4953 
4954 BEGIN
4955         -- Standard Start of API savepoint
4956         -- SAVEPOINT  get_version_label_pub;
4957 
4958         -- Check for freeze_flag in csi_install_parameters is set to 'Y'
4959 
4960         csi_utility_grp.check_ib_active;
4961 
4962         -- Standard call to check for call compatibility.
4963         IF NOT FND_API.Compatible_API_Call (l_api_version       ,
4964                                                     p_api_version       ,
4965                                                 l_api_name              ,
4966                                                 G_PKG_NAME              )
4967         THEN
4968                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4969         END IF;
4970 
4971     -- Initialize message list if p_init_msg_list is set to TRUE.
4972         IF FND_API.to_Boolean( p_init_msg_list ) THEN
4973                 FND_MSG_PUB.initialize;
4974         END IF;
4975 
4976         --  Initialize API return status to success
4977         x_return_status := FND_API.G_RET_STS_SUCCESS;
4978 
4979     -- Check the profile option debug_level for debug message reporting
4980     l_debug_level:=fnd_profile.value('CSI_DEBUG_LEVEL');
4981 
4982     -- If debug_level = 1 then dump the procedure name
4983     IF (l_debug_level > 0) THEN
4984          csi_gen_utility_pvt.put_line( 'get_version_label');
4985     END IF;
4986 
4987     -- If the debug level = 2 then dump all the parameters values.
4988     IF (l_debug_level > 1) THEN
4989                 csi_gen_utility_pvt.put_line( 'get_version_label:'   ||
4990                                                                      p_api_version         ||'-'||
4991                                                                  p_commit              ||'-'||
4992                                                                      p_init_msg_list       ||'-'||
4993                                      p_validation_level          );
4994 
4995     END IF;
4996 
4997      /***** srramakr commented for bug # 3304439
4998      -- Check for the profile option and enable trace
4999      l_flag:=CSI_GEN_UTILITY_PVT.enable_trace(l_trace_flag => l_flag);
5000      -- End enable trace
5001      ****/
5002 
5003     -- Start API body
5004     IF    (p_version_label_query_rec.version_label_id  = FND_API.G_MISS_NUM)
5005      AND  (p_version_label_query_rec.instance_id       = FND_API.G_MISS_NUM)
5006      AND  (p_version_label_query_rec.version_label     = FND_API.G_MISS_CHAR)
5007      AND  (p_version_label_query_rec.date_time_stamp = FND_API.G_MISS_DATE) THEN
5008 
5009            FND_MESSAGE.Set_Name('CSI', 'CSI_API_INVALID_PARAMETERS');
5010            FND_MSG_PUB.ADD;
5011           RAISE FND_API.G_EXC_ERROR;
5012      END IF;
5013 
5014        -- Generate the where clause
5015        csi_item_instance_pvt.Gen_Ver_Where_Clause
5016        (   p_ver_label_query_rec  =>  p_version_label_query_rec,
5017            x_where_clause         =>  l_where_clause    );
5018 
5019        -- Build the select statement
5020        l_select_stmt := l_select_stmt || ' where '||l_where_clause;
5021 
5022        -- Open the cursor
5023        l_get_ver_cursor_id := dbms_sql.open_cursor;
5024 
5025        --Parse the select statement
5026        dbms_sql.parse(l_get_ver_cursor_id, l_select_stmt , dbms_sql.native);
5027 
5028        -- Bind the variables
5029        csi_item_instance_pvt.Bind_Ver_variable(p_version_label_query_rec, l_get_ver_cursor_id);
5030 
5031        -- Define output variables
5032        csi_item_instance_pvt.Define_Ver_Columns(l_get_ver_cursor_id);
5033 
5034         -- execute the select statement
5035        l_rows_processed := dbms_sql.execute(l_get_ver_cursor_id);
5036 
5037     LOOP
5038        EXIT WHEN DBMS_SQL.FETCH_ROWS(l_get_ver_cursor_id) = 0;
5039              csi_item_instance_pvt.Get_Ver_Column_Values(l_get_ver_cursor_id, l_version_label_rec);
5040              l_count := l_count + 1;
5041              x_version_label_tbl(l_count) := l_version_label_rec;
5042     END LOOP;
5043 
5044     -- Close the cursor
5045     DBMS_SQL.CLOSE_CURSOR(l_get_ver_cursor_id);
5046 
5047      IF p_time_stamp < sysdate THEN
5048           csi_item_instance_pvt.Construct_ver_from_hist(x_version_label_tbl, p_time_stamp);
5049      ELSE
5050       If (p_time_stamp <> fnd_api.g_miss_date and p_time_stamp > sysdate) THEN
5051             FND_MESSAGE.Set_Name('CSI', 'CSI_API_INVALID_HIST_PARAMS');
5052             FND_MSG_PUB.ADD;
5053             RAISE FND_API.G_EXC_ERROR;
5054       END IF;
5055      END IF;
5056 
5057         --
5058         -- End of API body
5059 
5060         -- Standard check of p_commit.
5061         /*
5062         IF FND_API.To_Boolean( p_commit ) THEN
5063                 COMMIT WORK;
5064         END IF;
5065         */
5066 
5067     /***** srramakr commented for bug # 3304439
5068     -- Check for the profile option and disable the trace
5069     IF (l_flag = 'Y') THEN
5070         dbms_session.set_sql_trace(false);
5071     END IF;
5072     -- End disable trace
5073     ****/
5074 
5075         -- Standard call to get message count and if count is  get message info.
5076         FND_MSG_PUB.Count_And_Get
5077                 (p_encoded => FND_API.G_FALSE,
5078                  p_count        =>      x_msg_count ,
5079                  p_data         =>      x_msg_data);
5080 
5081 EXCEPTION
5082         WHEN FND_API.G_EXC_ERROR THEN
5083                -- ROLLBACK TO get_version_label_pub;
5084                 x_return_status := FND_API.G_RET_STS_ERROR ;
5085                 FND_MSG_PUB.Count_And_Get
5086                 (       p_encoded => FND_API.G_FALSE,
5087                  p_count  =>      x_msg_count,
5088                         p_data   =>      x_msg_data);
5089         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5090                -- ROLLBACK TO get_version_label_pub;
5091                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5092                 FND_MSG_PUB.Count_And_Get
5093                 (       p_encoded => FND_API.G_FALSE,
5094                  p_count   =>      x_msg_count,
5095                         p_data    =>      x_msg_data );
5096         WHEN OTHERS THEN
5097                -- ROLLBACK TO get_version_label_pub;
5098                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5099                 IF FND_MSG_PUB.Check_Msg_Level
5100                      (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
5101                 THEN
5102                    FND_MSG_PUB.Add_Exc_Msg
5103                     ( G_PKG_NAME, l_api_name );
5104                 END IF;
5105                 FND_MSG_PUB.Count_And_Get
5106                 (       p_encoded => FND_API.G_FALSE,
5107                  p_count  =>      x_msg_count,
5108                         p_data   =>      x_msg_data);
5109 END get_version_labels;
5110 
5111 /*----------------------------------------------------*/
5112 /* Pocedure name: Create_version_label                */
5113 /* Description :   procedure for creating             */
5114 /*                 version label for                  */
5115 /*                 an Item Instance                   */
5116 /*----------------------------------------------------*/
5117 
5118 PROCEDURE create_version_label
5119  (    p_api_version         IN     NUMBER
5120      ,p_commit              IN     VARCHAR2
5121      ,p_init_msg_list       IN     VARCHAR2
5122      ,p_validation_level    IN     NUMBER
5123      ,p_version_label_tbl   IN OUT NOCOPY csi_datastructures_pub.version_label_tbl
5124      ,p_txn_rec             IN OUT NOCOPY csi_datastructures_pub.transaction_rec
5125      ,x_return_status       OUT    NOCOPY VARCHAR2
5126      ,x_msg_count           OUT    NOCOPY NUMBER
5127      ,x_msg_data            OUT    NOCOPY VARCHAR2
5128  ) IS
5129       l_api_name           CONSTANT VARCHAR2(30)   := 'CREATE_VERSION_LABEL';
5130       l_api_version        CONSTANT NUMBER                 := 1.0                   ;
5131       l_debug_level             NUMBER                             ;
5132       l_line_count              NUMBER                                  ;
5133       l_version_label_rec       csi_datastructures_pub.version_label_rec;
5134       l_msg_index               NUMBER                                  ;
5135       l_flag                    VARCHAR2(1)  :='N'                      ;
5136       l_msg_count               NUMBER;
5137 BEGIN
5138 
5139     -- Standard Start of API savepoint
5140         SAVEPOINT  create_version_label_pub;
5141 
5142         -- Check for freeze_flag in csi_install_parameters is set to 'Y'
5143 
5144         csi_utility_grp.check_ib_active;
5145 
5146         -- Standard call to check for call compatibility.
5147         IF NOT FND_API.Compatible_API_Call ( l_api_version  ,
5148                                                      p_api_version  ,
5149                                                  l_api_name     ,
5150                                                  G_PKG_NAME     )
5151         THEN
5152                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5153         END IF;
5154 
5155     -- Initialize message list if p_init_msg_list is set to TRUE.
5156         IF FND_API.to_Boolean( p_init_msg_list ) THEN
5157                 FND_MSG_PUB.initialize;
5158         END IF;
5159 
5160         --  Initialize API return status to success
5161         x_return_status := FND_API.G_RET_STS_SUCCESS;
5162 
5163     -- Check the profile option debug_level for debug message reporting
5164     l_debug_level:=fnd_profile.value('CSI_DEBUG_LEVEL');
5165 
5166     -- If debug_level = 1 then dump the procedure name
5167     IF (l_debug_level > 0) THEN
5168          csi_gen_utility_pvt.put_line( 'create_version_label');
5169     END IF;
5170 
5171 
5172     -- If the debug level = 2 then dump all the parameters values.
5173     IF (l_debug_level > 1) THEN
5174                 csi_gen_utility_pvt.put_line( 'create_version_label:'||
5175                                                                    p_api_version           ||'-'||
5176                                                                p_commit                ||'-'||
5177                                                                p_init_msg_list         ||'-'||
5178                                    p_validation_level           );
5179 
5180         -- Dump the records in the log file
5181         csi_gen_utility_pvt.dump_version_label_tbl(p_version_label_tbl);
5182         csi_gen_utility_pvt.dump_txn_rec(p_txn_rec);
5183     END IF;
5184 
5185     /***** srramakr commented for bug # 3304439
5186     -- Check for the profile option and enable trace
5187     l_flag:=CSI_GEN_UTILITY_PVT.enable_trace(l_trace_flag => l_flag);
5188     -- End enable trace
5189     ****/
5190 
5191     -- Start API body
5192     --
5193     IF p_version_label_tbl.count > 0 THEN
5194           FOR l_count IN p_version_label_tbl.FIRST..p_version_label_tbl.LAST
5195       LOOP
5196          IF p_version_label_tbl.EXISTS(l_count) THEN
5197 
5198             csi_item_instance_pvt.create_version_label
5199            (  p_api_version         => p_api_version
5200              ,p_commit              => p_commit
5201              ,p_init_msg_list       => p_init_msg_list
5202              ,p_validation_level    => p_validation_level
5203              ,p_version_label_rec   => p_version_label_tbl(l_count)
5204              ,p_txn_rec             => p_txn_rec
5205              ,x_return_status       => x_return_status
5206              ,x_msg_count           => x_msg_count
5207              ,x_msg_data            => x_msg_data       );
5208 
5209            IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
5210                       l_msg_index := 1;
5211               l_msg_count := x_msg_count;
5212                   WHILE l_msg_count > 0 LOOP
5213                             x_msg_data := FND_MSG_PUB.GET(
5214                                               l_msg_index,
5215                                               FND_API.G_FALSE);
5216                         csi_gen_utility_pvt.put_line('message data = '||x_msg_data);
5217                     l_msg_index := l_msg_index + 1;
5218                             l_msg_count := l_msg_count - 1;
5219                   END LOOP;
5220                 ROLLBACK TO create_version_label_pub;
5221               RETURN;
5222            END IF;
5223          END IF;
5224       END LOOP;
5225     END IF;
5226     --
5227         -- End of API body
5228 
5229         -- Standard check of p_commit.
5230         IF FND_API.To_Boolean( p_commit ) THEN
5231                 COMMIT WORK;
5232         END IF;
5233 
5234     /***** srramakr commented for bug # 3304439
5235     -- Check for the profile option and disable the trace
5236     IF (l_flag = 'Y') THEN
5237          dbms_session.set_sql_trace(false);
5238     END IF;
5239     -- End disable trace
5240     ****/
5241 
5242         -- Standard call to get message count and if count is  get message info.
5243         FND_MSG_PUB.Count_And_Get
5244                 (p_encoded => FND_API.G_FALSE,
5245                  p_count        =>      x_msg_count ,
5246          p_data         =>      x_msg_data      );
5247 
5248 EXCEPTION
5249     WHEN FND_API.G_EXC_ERROR THEN
5250         ROLLBACK TO create_version_label_pub;
5251                 x_return_status := FND_API.G_RET_STS_ERROR ;
5252                         FND_MSG_PUB.Count_And_Get
5253                 (       p_encoded => FND_API.G_FALSE,
5254                  p_count  =>      x_msg_count,
5255                         p_data   =>      x_msg_data );
5256 
5257         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5258                 ROLLBACK TO create_version_label_pub;
5259                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5260                 FND_MSG_PUB.Count_And_Get
5261                 (       p_encoded => FND_API.G_FALSE,
5262                  p_count  =>      x_msg_count,
5263                         p_data   =>      x_msg_data );
5264         WHEN OTHERS THEN
5265                 ROLLBACK TO create_version_label_pub;
5266                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5267                 IF FND_MSG_PUB.Check_Msg_Level
5268                      (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
5269                 THEN
5270         FND_MSG_PUB.Add_Exc_Msg
5271                    ( G_PKG_NAME, l_api_name );
5272                 END IF;
5273         FND_MSG_PUB.Count_And_Get
5274                 (       p_encoded => FND_API.G_FALSE,
5275                  p_count =>      x_msg_count,
5276                         p_data  =>      x_msg_data);
5277 END create_version_label;
5278 
5279 /*----------------------------------------------------*/
5280 /* Procedure name: Update_version_label               */
5281 /* Description :   procedure for Update               */
5282 /*                 version label for                  */
5283 /*                 an Item Instance                   */
5284 /*----------------------------------------------------*/
5285 
5286 PROCEDURE update_version_label
5287  (    p_api_version                 IN     NUMBER
5288      ,p_commit                      IN     VARCHAR2
5289      ,p_init_msg_list               IN     VARCHAR2
5290      ,p_validation_level            IN     NUMBER
5291      ,p_version_label_tbl           IN     csi_datastructures_pub.version_label_tbl
5292      ,p_txn_rec                     IN OUT NOCOPY csi_datastructures_pub.transaction_rec
5293      ,x_return_status               OUT    NOCOPY VARCHAR2
5294      ,x_msg_count                   OUT    NOCOPY NUMBER
5295      ,x_msg_data                    OUT    NOCOPY VARCHAR2
5296    ) IS
5297 
5298    l_api_name       CONSTANT VARCHAR2(30)   := 'UPDATE_VERSION_LABEL';
5299    l_api_version        CONSTANT NUMBER             := 1.0;
5300    l_debug_level             NUMBER;
5301    l_version_label_rec       csi_datastructures_pub.version_label_rec;
5302    l_msg_index               NUMBER;
5303    l_line_count              NUMBER ;
5304    l_flag                    VARCHAR2(1)    :='N';
5305    l_msg_count               NUMBER;
5306 
5307 BEGIN
5308 
5309         -- Standard Start of API savepoint
5310         SAVEPOINT  update_version_label_pub;
5311 
5312         -- Check for freeze_flag in csi_install_parameters is set to 'Y'
5313 
5314         csi_utility_grp.check_ib_active;
5315 
5316         -- Standard call to check for call compatibility.
5317         IF NOT FND_API.Compatible_API_Call (l_api_version       ,
5318                                                     p_api_version       ,
5319                                                 l_api_name              ,
5320                                                 G_PKG_NAME              )
5321         THEN
5322                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5323         END IF;
5324 
5325 
5326     -- Initialize message list if p_init_msg_list is set to TRUE.
5327         IF FND_API.to_Boolean( p_init_msg_list ) THEN
5328                 FND_MSG_PUB.initialize;
5329         END IF;
5330 
5331         --  Initialize API return status to success
5332         x_return_status := FND_API.G_RET_STS_SUCCESS;
5333 
5334     -- Check the profile option debug_level for debug message reporting
5335     l_debug_level:=fnd_profile.value('CSI_DEBUG_LEVEL');
5336 
5337     -- If debug_level = 1 then dump the procedure name
5338     IF (l_debug_level > 0) THEN
5339             csi_gen_utility_pvt.put_line( 'update_version_label');
5340     END IF;
5341 
5342 
5343     -- If the debug level = 2 then dump all the parameters values.
5344     IF (l_debug_level > 1) THEN
5345                 csi_gen_utility_pvt.put_line( 'update_version_label:'||
5346                                                                  p_api_version           ||'-'||
5347                                                              p_commit                ||'-'||
5348                                                                  p_init_msg_list               );
5349 
5350          -- Dump the records in the log file
5351          csi_gen_utility_pvt.dump_version_label_tbl(p_version_label_tbl);
5352          csi_gen_utility_pvt.dump_txn_rec(p_txn_rec);
5353 
5354         END IF;
5355 
5356     /***** srramakr commented for bug # 3304439
5357     -- Check for the profile option and enable trace
5358     l_flag:=CSI_GEN_UTILITY_PVT.enable_trace(l_trace_flag => l_flag);
5359     -- End enable trace
5360     ****/
5361     -- Start API body
5362         --
5363     IF p_version_label_tbl.count > 0 THEN
5364        FOR l_count IN p_version_label_tbl.FIRST..p_version_label_tbl.LAST
5365        LOOP
5366           IF p_version_label_tbl.EXISTS(l_count) THEN
5367 
5368              csi_item_instance_pvt.update_version_label
5369              ( p_api_version             => p_api_version
5370               ,p_commit                  => p_commit
5371               ,p_init_msg_list           => p_init_msg_list
5372               ,p_validation_level        => p_validation_level
5373               ,p_version_label_rec       => p_version_label_tbl(l_count)
5374               ,p_txn_rec                 => p_txn_rec
5375               ,x_return_status           => x_return_status
5376               ,x_msg_count               => x_msg_count
5377               ,x_msg_data                => x_msg_data     );
5378 
5379 
5380               IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
5381                      l_msg_index := 1;
5382                  l_msg_count := x_msg_count;
5383                      WHILE l_msg_count > 0 LOOP
5384                            x_msg_data := FND_MSG_PUB.GET(
5385                                                          l_msg_index,
5386                                                          FND_API.G_FALSE        );
5387                            csi_gen_utility_pvt.put_line('message data = '||x_msg_data);
5388                                l_msg_index := l_msg_index + 1;
5389                                l_msg_count := l_msg_count - 1;
5390                      END LOOP;
5391               END IF;
5392             END IF;
5393         END LOOP;
5394     END IF;
5395         --
5396         -- End of API body
5397 
5398         -- Standard check of p_commit.
5399         IF FND_API.To_Boolean( p_commit ) THEN
5400                 COMMIT WORK;
5401         END IF;
5402 
5403     /***** srramakr commented for bug # 3304439
5404     -- Check for the profile option and disable the trace
5405     IF (fnd_profile.value('CSI_ENABLE_SQL_TRACE') = 'Y') THEN
5406                    dbms_session.set_sql_trace(false);
5407     END IF;
5408     -- End disable trace
5409     ****/
5410 
5411     -- Standard call to get message count and if count is  get message info.
5412         FND_MSG_PUB.Count_And_Get
5413                 (p_encoded => FND_API.G_FALSE,
5414                  p_count        =>      x_msg_count ,
5415          p_data         =>      x_msg_data);
5416 EXCEPTION
5417     WHEN FND_API.G_EXC_ERROR THEN
5418                 ROLLBACK TO update_version_label_pub;
5419                 x_return_status := FND_API.G_RET_STS_ERROR ;
5420                 FND_MSG_PUB.Count_And_Get
5421                 (       p_encoded => FND_API.G_FALSE,
5422                  p_count    =>      x_msg_count,
5423                         p_data     =>      x_msg_data);
5424         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5425                 ROLLBACK TO update_version_label_pub;
5426                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5427                 FND_MSG_PUB.Count_And_Get
5428                 (       p_encoded => FND_API.G_FALSE,
5429                  p_count   =>      x_msg_count,
5430                         p_data    =>      x_msg_data);
5431         WHEN OTHERS THEN
5432                 ROLLBACK TO update_version_label_pub;
5433                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5434                 IF FND_MSG_PUB.Check_Msg_Level
5435                      (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
5436                 THEN
5437                    FND_MSG_PUB.Add_Exc_Msg
5438                     ( G_PKG_NAME, l_api_name );
5439                 END IF;
5440                 FND_MSG_PUB.Count_And_Get
5441                 (       p_encoded => FND_API.G_FALSE,
5442                  p_count   =>      x_msg_count,
5443                         p_data    =>      x_msg_data);
5444 
5445 END update_version_label;
5446 
5447 /*----------------------------------------------------*/
5448 /* Procedure name: expire_version_label               */
5449 /* Description :   procedure for expire               */
5450 /*                 version label for                  */
5451 /*                 an Item Instance                   */
5452 /*----------------------------------------------------*/
5453 
5454 PROCEDURE expire_version_label
5455  (    p_api_version                 IN     NUMBER
5456      ,p_commit                      IN     VARCHAR2
5457      ,p_init_msg_list               IN     VARCHAR2
5458      ,p_validation_level            IN     NUMBER
5459      ,p_version_label_tbl           IN     csi_datastructures_pub.version_label_tbl
5460      ,p_txn_rec                     IN OUT NOCOPY csi_datastructures_pub.transaction_rec
5461      ,x_return_status               OUT    NOCOPY VARCHAR2
5462      ,x_msg_count                   OUT    NOCOPY NUMBER
5463      ,x_msg_data                    OUT    NOCOPY VARCHAR2
5464    ) IS
5465      l_api_name             CONSTANT VARCHAR2(30)   := 'UPDATE_VERSION_LABEL';
5466      l_api_version              CONSTANT NUMBER         := 1.0;
5467      l_debug_level                  NUMBER;
5468      l_version_label_rec    csi_datastructures_pub.version_label_rec;
5469      l_msg_index            NUMBER;
5470      l_line_count           NUMBER;
5471      l_version_label_id     NUMBER;
5472      l_flag                 VARCHAR2(1)  :='N';
5473      l_msg_count               NUMBER;
5474 
5475 BEGIN
5476 
5477         -- Standard Start of API savepoint
5478         SAVEPOINT  expire_version_label_pub;
5479 
5480         -- Check for freeze_flag in csi_install_parameters is set to 'Y'
5481 
5482         csi_utility_grp.check_ib_active;
5483 
5484         -- Standard call to check for call compatibility.
5485         IF NOT FND_API.Compatible_API_Call (l_api_version       ,
5486                                                     p_api_version       ,
5487                                                 l_api_name              ,
5488                                                 G_PKG_NAME              )
5489         THEN
5490                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5491         END IF;
5492 
5493     -- Initialize message list if p_init_msg_list is set to TRUE.
5494         IF FND_API.to_Boolean( p_init_msg_list ) THEN
5495                 FND_MSG_PUB.initialize;
5496         END IF;
5497 
5498         --  Initialize API return status to success
5499         x_return_status := FND_API.G_RET_STS_SUCCESS;
5500 
5501     -- Check the profile option debug_level for debug message reporting
5502     l_debug_level:=fnd_profile.value('CSI_DEBUG_LEVEL');
5503 
5504     -- If debug_level = 1 then dump the procedure name
5505     IF (l_debug_level > 0) THEN
5506          csi_gen_utility_pvt.put_line( 'expire_version_label');
5507     END IF;
5508 
5509     -- If the debug level = 2 then dump all the parameters values.
5510     IF (l_debug_level > 1) THEN
5511                 csi_gen_utility_pvt.put_line( 'expire_version_label:'||
5512                                                                   p_api_version           ||'-'||
5513                                                               p_commit                ||'-'||
5514                                                                   p_init_msg_list            );
5515          -- Dump the records in the log file
5516          csi_gen_utility_pvt.dump_version_label_tbl(p_version_label_tbl);
5517          csi_gen_utility_pvt.dump_txn_rec(p_txn_rec);
5518      END IF;
5519      /***** srramakr commented for bug # 3304439
5520      -- Check for the profile option and enable trace
5521      l_flag:=CSI_GEN_UTILITY_PVT.enable_trace(l_trace_flag => l_flag);
5522      -- End enable trace
5523      ****/
5524      -- Start API body
5525          --
5526      IF p_version_label_tbl.count > 0 THEN
5527        FOR l_count IN p_version_label_tbl.FIRST..p_version_label_tbl.LAST
5528        LOOP
5529          IF p_version_label_tbl.EXISTS(l_count) THEN
5530 
5531             csi_item_instance_pvt.expire_version_label
5532             (  p_api_version             =>  p_api_version
5533               ,p_commit                  =>  p_commit
5534               ,p_init_msg_list           =>  p_init_msg_list
5535               ,p_validation_level        =>  p_validation_level
5536               ,p_version_label_rec       =>  p_version_label_tbl(l_count)
5537               ,p_txn_rec                 =>  p_txn_rec
5538               ,x_return_status           =>  x_return_status
5539               ,x_msg_count               =>  x_msg_count
5540               ,x_msg_data                =>  x_msg_data      );
5541 
5542               IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
5543                      l_msg_index := 1;
5544                  l_msg_count := x_msg_count;
5545                      WHILE l_msg_count > 0 LOOP
5546                            x_msg_data := FND_MSG_PUB.GET(
5547                                                  l_msg_index,
5548                                                          FND_API.G_FALSE );
5549                            csi_gen_utility_pvt.put_line('message data = '||x_msg_data);
5550                                l_msg_index := l_msg_index + 1;
5551                                l_msg_count := l_msg_count - 1;
5552                      END LOOP;
5553               END IF;
5554           END IF;
5555        END LOOP;
5556      END IF;
5557         --
5558         -- End of API body
5559 
5560         -- Standard check of p_commit.
5561         IF FND_API.To_Boolean( p_commit ) THEN
5562                 COMMIT WORK;
5563         END IF;
5564 
5565     /***** srramakr commented for bug # 3304439
5566     -- Check for the profile option and disable the trace
5567         IF (fnd_profile.value('CSI_ENABLE_SQL_TRACE') = 'Y') THEN
5568                    dbms_session.set_sql_trace(false);
5569     END IF;
5570         -- End disable trace
5571     ****/
5572 
5573         -- Standard call to get message count and if count is  get message info.
5574         FND_MSG_PUB.Count_And_Get
5575                 (p_encoded => FND_API.G_FALSE,
5576                  p_count        =>      x_msg_count ,
5577          p_data         =>      x_msg_data      );
5578 EXCEPTION
5579     WHEN FND_API.G_EXC_ERROR THEN
5580                 ROLLBACK TO expire_version_label_pub;
5581                 x_return_status := FND_API.G_RET_STS_ERROR ;
5582                 FND_MSG_PUB.Count_And_Get
5583                 (       p_encoded => FND_API.G_FALSE,
5584                  p_count =>      x_msg_count,
5585                         p_data  =>      x_msg_data);
5586         WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5587                 ROLLBACK TO expire_version_label_pub;
5588                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5589                 FND_MSG_PUB.Count_And_Get
5590                 (       p_encoded => FND_API.G_FALSE,
5591                  p_count =>      x_msg_count,
5592                         p_data  =>      x_msg_data);
5593         WHEN OTHERS THEN
5594                 ROLLBACK TO expire_version_label_pub;
5595                 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5596                 IF FND_MSG_PUB.Check_Msg_Level
5597                      (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
5598                 THEN
5599                    FND_MSG_PUB.Add_Exc_Msg
5600                     ( G_PKG_NAME, l_api_name );
5601                 END IF;
5602                 FND_MSG_PUB.Count_And_Get
5603                 (       p_encoded => FND_API.G_FALSE,
5604                  p_count  =>      x_msg_count,
5605                         p_data   =>      x_msg_data);
5606 END expire_version_label;
5607 
5608 /*----------------------------------------------------*/
5609 /* procedure name: get_extended_attrib_values         */
5610 /* description :   Gets the extended attribute        */
5611 /*                 values of an item instance         */
5612 /*----------------------------------------------------*/
5613 
5614 PROCEDURE get_extended_attrib_values
5615  (    p_api_version           IN     NUMBER
5616      ,p_commit                IN     VARCHAR2
5617      ,p_init_msg_list         IN     VARCHAR2
5618      ,p_validation_level      IN     NUMBER
5619      ,p_ext_attribs_query_rec IN     csi_datastructures_pub.extend_attrib_query_rec
5620      ,p_time_stamp            IN     DATE
5621      ,x_ext_attrib_tbl           OUT NOCOPY csi_datastructures_pub.extend_attrib_values_tbl
5622      ,x_ext_attrib_def_tbl       OUT NOCOPY csi_datastructures_pub.extend_attrib_tbl  -- added
5623      ,x_return_status            OUT NOCOPY VARCHAR2
5624      ,x_msg_count                OUT NOCOPY NUMBER
5625      ,x_msg_data                 OUT NOCOPY VARCHAR2
5626  )
5627 
5628 IS
5629     l_api_name          CONSTANT VARCHAR2(30)    := 'get_extended_attrib_values';
5630     l_api_version       CONSTANT NUMBER          := 1.0;
5631     l_debug_level                NUMBER;
5632     l_ext_rec                    csi_datastructures_pub.extend_attrib_values_rec;
5633     l_rows_processed             NUMBER;
5634     l_where_clause               VARCHAR2(2000)  := ''  ;
5635     l_select_stmt                VARCHAR2(20000);
5636     l_cur_get_ext                NUMBER;
5637     l_count                      NUMBER          := 0;
5638     l_trace_enable_flag          VARCHAR2(1)     :='N';
5639     l_row                        NUMBER          := 0;
5640 
5641     CURSOR ext_attrib_csr (p_instance_id NUMBER) IS
5642     SELECT  /*+ INDEX(iea CSI_I_EXTENDED_ATTRIBS_N04)*/    -- Added by sguthiva for bug 2367664
5643             iea.attribute_id            attribute_id
5644            ,iea.attribute_level         attribute_level
5645            ,iea.master_organization_id  master_organization_id
5646            ,iea.inventory_item_id       inventory_item_id
5647            ,iea.item_category_id        item_category_id
5648            ,iea.instance_id             instance_id
5649            ,iea.attribute_code          attribute_code
5650            ,iea.attribute_name          attribute_name
5651            ,iea.attribute_category      attribute_category
5652            ,iea.description             description
5653            ,iea.active_start_date       active_start_date
5654            ,iea.active_end_date         active_end_date
5655            ,iea.context                 context
5656            ,iea.attribute1              attribute1
5657            ,iea.attribute2              attribute2
5658            ,iea.attribute3              attribute3
5659            ,iea.attribute4              attribute4
5660            ,iea.attribute5              attribute5
5661            ,iea.attribute6              attribute6
5662            ,iea.attribute7              attribute7
5663            ,iea.attribute8              attribute8
5664            ,iea.attribute9              attribute9
5665            ,iea.attribute10             attribute10
5666            ,iea.attribute11             attribute11
5667            ,iea.attribute12             attribute12
5668            ,iea.attribute13             attribute13
5669            ,iea.attribute14             attribute14
5670            ,iea.attribute15             attribute15
5671            ,iea.object_version_number   object_version_number
5672     FROM   csi_i_extended_attribs iea
5673     WHERE  attribute_level = 'GLOBAL'
5674     UNION ALL
5675     SELECT  /*+ INDEX(iea CSI_I_EXTENDED_ATTRIBS_N01)*/    -- Added by sguthiva for bug 2367664
5676             iea.attribute_id            attribute_id
5677            ,iea.attribute_level         attribute_level
5678            ,iea.master_organization_id  master_organization_id
5679            ,iea.inventory_item_id       inventory_item_id
5680            ,iea.item_category_id        item_category_id
5681            ,iea.instance_id             instance_id
5682            ,iea.attribute_code          attribute_code
5683            ,iea.attribute_name          attribute_name
5684            ,iea.attribute_category      attribute_category
5685            ,iea.description             description
5686            ,iea.active_start_date       active_start_date
5687            ,iea.active_end_date         active_end_date
5688            ,iea.context                 context
5689            ,iea.attribute1              attribute1
5690            ,iea.attribute2              attribute2
5691            ,iea.attribute3              attribute3
5692            ,iea.attribute4              attribute4
5693            ,iea.attribute5              attribute5
5694            ,iea.attribute6              attribute6
5695            ,iea.attribute7              attribute7
5696            ,iea.attribute8              attribute8
5697            ,iea.attribute9              attribute9
5698            ,iea.attribute10             attribute10
5699            ,iea.attribute11             attribute11
5700            ,iea.attribute12             attribute12
5701            ,iea.attribute13             attribute13
5702            ,iea.attribute14             attribute14
5703            ,iea.attribute15             attribute15
5704            ,iea.object_version_number   object_version_number
5705     FROM   csi_i_extended_attribs iea
5706     WHERE  attribute_level = 'INSTANCE'
5707     AND    instance_id = p_instance_id
5708     UNION ALL
5709     SELECT  /*+ INDEX(ia CSI_ITEM_INSTANCES_U01)
5710                 INDEX(iea CSI_I_EXTENDED_ATTRIBS_N01)
5711             */                                             -- Added by sguthiva for bug 2367664
5712             iea.attribute_id            attribute_id
5713            ,iea.attribute_level         attribute_level
5714            ,iea.master_organization_id  master_organization_id
5715            ,iea.inventory_item_id       inventory_item_id
5716            ,iea.item_category_id        item_category_id
5717            ,iea.instance_id             instance_id
5718            ,iea.attribute_code          attribute_code
5719            ,iea.attribute_name          attribute_name
5720            ,iea.attribute_category      attribute_category
5721            ,iea.description             description
5722            ,iea.active_start_date       active_start_date
5723            ,iea.active_end_date         active_end_date
5724            ,iea.context                 context
5725            ,iea.attribute1              attribute1
5726            ,iea.attribute2              attribute2
5727            ,iea.attribute3              attribute3
5728            ,iea.attribute4              attribute4
5729            ,iea.attribute5              attribute5
5730            ,iea.attribute6              attribute6
5731            ,iea.attribute7              attribute7
5732            ,iea.attribute8              attribute8
5733            ,iea.attribute9              attribute9
5734            ,iea.attribute10             attribute10
5735            ,iea.attribute11             attribute11
5736            ,iea.attribute12             attribute12
5737            ,iea.attribute13             attribute13
5738            ,iea.attribute14             attribute14
5739            ,iea.attribute15             attribute15
5740            ,iea.object_version_number   object_version_number
5741     FROM   csi_i_extended_attribs iea, csi_item_instances ia
5742     WHERE  iea.attribute_level = 'ITEM'
5743     AND    iea.inventory_item_id = ia.inventory_item_id  --p_inv_item_id
5744     AND    iea.master_organization_id = ia.inv_master_organization_id --p_org_id;
5745     AND    ia.instance_id = p_instance_id
5746     UNION ALL
5747     SELECT  /*+ INDEX(ia CSI_ITEM_INSTANCES_U01)
5748                 INDEX(iea CSI_I_EXTENDED_ATTRIBS_N01)
5749                 INDEX(ic MTL_ITEM_CATEGORIES_U1)
5750             */                                             -- Added by sguthiva for bug 2367664
5751             iea.attribute_id            attribute_id
5752            ,iea.attribute_level         attribute_level
5753            ,iea.master_organization_id  master_organization_id
5754            ,iea.inventory_item_id       inventory_item_id
5755            ,iea.item_category_id        item_category_id
5756            ,iea.instance_id             instance_id
5757            ,iea.attribute_code          attribute_code
5758            ,iea.attribute_name          attribute_name
5759            ,iea.attribute_category      attribute_category
5760            ,iea.description             description
5761            ,iea.active_start_date       active_start_date
5762            ,iea.active_end_date         active_end_date
5763            ,iea.context                 context
5764            ,iea.attribute1              attribute1
5765            ,iea.attribute2              attribute2
5766            ,iea.attribute3              attribute3
5767            ,iea.attribute4              attribute4
5768            ,iea.attribute5              attribute5
5769            ,iea.attribute6              attribute6
5770            ,iea.attribute7              attribute7
5771            ,iea.attribute8              attribute8
5772            ,iea.attribute9              attribute9
5773            ,iea.attribute10             attribute10
5774            ,iea.attribute11             attribute11
5775            ,iea.attribute12             attribute12
5776            ,iea.attribute13             attribute13
5777            ,iea.attribute14             attribute14
5778            ,iea.attribute15             attribute15
5779            ,iea.object_version_number   object_version_number
5780     FROM   csi_i_extended_attribs iea
5781           ,csi_item_instances ia
5782           ,mtl_item_categories ic
5783     WHERE  iea.attribute_level = 'CATEGORY'
5784   --  AND    iea.inventory_item_id = ia.inventory_item_id  -- commented for Bug # 3189494
5785   --  AND    iea.master_organization_id = ia.inv_master_organization_id -- commented for Bug # 3189494
5786     AND    ic.organization_id = ia.inv_master_organization_id
5787     AND    ic.inventory_item_id = ia.inventory_item_id
5788     AND    ic.category_id = iea.item_category_id
5789     AND    ia.instance_id = p_instance_id;
5790 
5791 BEGIN
5792 
5793     -- Standard Start of API savepoint
5794    -- SAVEPOINT    get_extended_attrib_values;
5795 
5796     -- Check for freeze_flag in csi_install_parameters is set to 'Y'
5797 
5798         csi_utility_grp.check_ib_active;
5799 
5800     -- Standard call to check for call compatibility.
5801     IF NOT FND_API.Compatible_API_Call (l_api_version,
5802                                         p_api_version,
5803                                         l_api_name,
5804                                         g_pkg_name)
5805     THEN
5806         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5807     END IF;
5808 
5809 
5810     -- Initialize message list if p_init_msg_list is set to TRUE.
5811     IF FND_API.to_Boolean( p_init_msg_list ) THEN
5812         FND_MSG_PUB.initialize;
5813     END IF;
5814 
5815 
5816     --  Initialize API return status to success
5817     x_return_status := FND_API.G_RET_STS_SUCCESS;
5818 
5819 
5820     -- Check the profile option debug_level for debug message reporting
5821     l_debug_level:=fnd_profile.value('CSI_DEBUG_LEVEL');
5822 
5823     -- If debug_level = 1 then dump the procedure name
5824     IF (l_debug_level > 0) THEN
5825         csi_gen_utility_pvt.put_line( 'get_extended_attrib_values');
5826     END IF;
5827 
5828 
5829     -- If the debug level = 2 then dump all the parameters values.
5830     IF (l_debug_level > 1) THEN
5831       csi_gen_utility_pvt.put_line( p_api_version ||'-'
5832                      || p_commit                     ||'-'
5833                      || p_init_msg_list              ||'-'
5834                      || p_validation_level           ||'-'
5835                      || p_time_stamp);
5836 
5837       -- Dump extended attribs
5838       csi_gen_utility_pvt.dump_ext_attrib_query_rec(p_ext_attribs_query_rec);
5839     END IF;
5840 
5841     /***** srramakr commented for bug # 3304439
5842     -- Check for the profile option and enable trace
5843     l_trace_enable_flag:=CSI_GEN_UTILITY_PVT.enable_trace(l_trace_flag => l_trace_enable_flag);
5844     -- End enable trace
5845     ****/
5846 
5847     -- Start API body
5848     -- Check if atleast one query parameters are passed
5849     IF (p_ext_attribs_query_rec.attribute_value_id  = FND_API.G_MISS_NUM)
5850          AND ( p_ext_attribs_query_rec.instance_id = FND_API.G_MISS_NUM)
5851          AND ( p_ext_attribs_query_rec.attribute_id = FND_API.G_MISS_NUM)
5852     THEN
5853 
5854           FND_MESSAGE.Set_Name('CSI', 'CSI_API_INVALID_PARAMETERS');
5855           FND_MSG_PUB.ADD;
5856          RAISE FND_API.G_EXC_ERROR;
5857     END IF;
5858 
5859 
5860     -- Generate the where clause
5861     csi_item_instance_pvt.Gen_ext_Where_Clause
5862       (    p_ext_query_rec      =>   p_ext_attribs_query_rec,
5863            x_where_clause       =>  l_where_clause    );
5864 
5865     -- Build the select statement
5866     l_select_stmt := 'SELECT  cv.attribute_value_id attribute_value_id'        ||
5867                             ',cv.attribute_id attribute_id'                    ||
5868                             ',cv.instance_id instance_id'                      ||
5869                             ',ca.attribute_code attribute_code'                ||
5870                             ',cv.attribute_value attribute_value'              ||
5871                             ',cv.active_start_date active_start_date'          ||
5872                             ',cv.active_end_date active_end_date'              ||
5873                             ',cv.context context'                              ||
5874                             ',cv.attribute1 attribute1'                        ||
5875                             ',cv.attribute2 attribute2'                        ||
5876                             ',cv.attribute3 attribute3'                        ||
5877                             ',cv.attribute4 attribute4'                        ||
5878                             ',cv.attribute5 attribute5'                        ||
5879                             ',cv.attribute6 attribute6'                        ||
5880                             ',cv.attribute7 attribute7'                        ||
5881                             ',cv.attribute8 attribute8'                        ||
5882                             ',cv.attribute9 attribute9'                        ||
5883                             ',cv.attribute10 attribute10'                      ||
5884                             ',cv.attribute11 attribute11'                      ||
5885                             ',cv.attribute12 attribute12'                      ||
5886                             ',cv.attribute13 attribute13'                      ||
5887                             ',cv.attribute14 attribute14'                      ||
5888                             ',cv.attribute15 attribute15'                      ||
5889                             ',cv.object_version_number object_version_number  '||
5890                      'FROM csi_iea_values cv, csi_i_extended_attribs ca  '     ||
5891                      'WHERE cv.attribute_id = ca.attribute_id' ;
5892 
5893     l_select_stmt := l_select_stmt || ' AND '||l_where_clause;
5894 
5895     -- Open the cursor
5896     l_cur_get_ext := dbms_sql.open_cursor;
5897 
5898 
5899     --Parse the select statement
5900     dbms_sql.parse(l_cur_get_ext, l_select_stmt , dbms_sql.native);
5901 
5902     -- Bind the variables
5903     csi_item_instance_pvt.Bind_ext_variable
5904            ( p_ext_attribs_query_rec,
5905              l_cur_get_ext );
5906 
5907     -- Define output variables
5908     csi_item_instance_pvt.Define_ext_Columns(l_cur_get_ext);
5909 
5910     -- execute the select statement
5911     l_rows_processed := dbms_sql.execute(l_cur_get_ext);
5912 
5913 
5914     LOOP
5915     EXIT WHEN DBMS_SQL.FETCH_ROWS(l_cur_get_ext) = 0;
5916             csi_item_instance_pvt.Get_ext_Column_Values(l_cur_get_ext, l_ext_rec);
5917             l_count := l_count + 1;
5918             x_ext_attrib_tbl(l_count) := l_ext_rec;
5919     END LOOP;
5920 
5921     -- Close the cursor
5922     DBMS_SQL.CLOSE_CURSOR(l_cur_get_ext);
5923 
5924     IF (p_time_stamp IS NOT NULL) AND (p_time_stamp <> FND_API.G_MISS_DATE) THEN
5925       IF p_time_stamp <= sysdate THEN
5926            csi_item_instance_pvt.Construct_ext_From_Hist(x_ext_attrib_tbl, p_time_stamp);
5927       ELSE
5928             FND_MESSAGE.Set_Name('CSI', 'CSI_API_INVALID_HIST_PARAMS');
5929             FND_MSG_PUB.ADD;
5930             RAISE FND_API.G_EXC_ERROR;
5931       END IF;
5932     END IF;
5933 
5934     IF (p_ext_attribs_query_rec.instance_id IS NOT NULL AND p_ext_attribs_query_rec.instance_id <> fnd_api.g_miss_num)
5935     THEN
5936 
5937        FOR attrib_csr IN ext_attrib_csr(p_ext_attribs_query_rec.instance_id)
5938        LOOP
5939            l_row:=l_row+1;
5940            x_ext_attrib_def_tbl(l_row).attribute_id           := attrib_csr.attribute_id;
5941            x_ext_attrib_def_tbl(l_row).attribute_level        := attrib_csr.attribute_level;
5942            x_ext_attrib_def_tbl(l_row).master_organization_id := attrib_csr.master_organization_id;
5943            x_ext_attrib_def_tbl(l_row).inventory_item_id      := attrib_csr.inventory_item_id;
5944            x_ext_attrib_def_tbl(l_row).item_category_id       := attrib_csr.item_category_id;
5945            x_ext_attrib_def_tbl(l_row).instance_id            := attrib_csr.instance_id;
5946            x_ext_attrib_def_tbl(l_row).attribute_code         := attrib_csr.attribute_code;
5947            x_ext_attrib_def_tbl(l_row).attribute_name         := attrib_csr.attribute_name;
5948            x_ext_attrib_def_tbl(l_row).attribute_category     := attrib_csr.attribute_category;
5949            x_ext_attrib_def_tbl(l_row).description            := attrib_csr.description;
5950            x_ext_attrib_def_tbl(l_row).active_start_date      := attrib_csr.active_start_date;
5951            x_ext_attrib_def_tbl(l_row).active_end_date        := attrib_csr.active_end_date;
5952            x_ext_attrib_def_tbl(l_row).context                := attrib_csr.context;
5953            x_ext_attrib_def_tbl(l_row).attribute1             := attrib_csr.attribute1;
5954            x_ext_attrib_def_tbl(l_row).attribute2             := attrib_csr.attribute2;
5955            x_ext_attrib_def_tbl(l_row).attribute3             := attrib_csr.attribute3;
5956            x_ext_attrib_def_tbl(l_row).attribute4             := attrib_csr.attribute4;
5957            x_ext_attrib_def_tbl(l_row).attribute5             := attrib_csr.attribute5;
5958            x_ext_attrib_def_tbl(l_row).attribute6             := attrib_csr.attribute6;
5959            x_ext_attrib_def_tbl(l_row).attribute7             := attrib_csr.attribute7;
5960            x_ext_attrib_def_tbl(l_row).attribute8             := attrib_csr.attribute8;
5961            x_ext_attrib_def_tbl(l_row).attribute9             := attrib_csr.attribute9;
5962            x_ext_attrib_def_tbl(l_row).attribute10            := attrib_csr.attribute10;
5963            x_ext_attrib_def_tbl(l_row).attribute11            := attrib_csr.attribute11;
5964            x_ext_attrib_def_tbl(l_row).attribute12            := attrib_csr.attribute12;
5965            x_ext_attrib_def_tbl(l_row).attribute13            := attrib_csr.attribute13;
5966            x_ext_attrib_def_tbl(l_row).attribute14            := attrib_csr.attribute14;
5967            x_ext_attrib_def_tbl(l_row).attribute15            := attrib_csr.attribute15;
5968            x_ext_attrib_def_tbl(l_row).object_version_number  := attrib_csr.object_version_number;
5969 
5970        END LOOP;
5971     END IF;
5972 
5973     -- End of API body
5974 
5975     -- Standard check of p_commit.
5976     /*
5977     IF FND_API.To_Boolean( p_commit ) THEN
5978       COMMIT WORK;
5979     END IF;
5980     */
5981 
5982     /***** srramakr commented for bug # 3304439
5983     -- Check for the profile option and disable the trace
5984     IF (l_trace_enable_flag = 'Y') THEN
5985        dbms_session.set_sql_trace(false);
5986     END IF;
5987     -- End disable trace
5988     ****/
5989 
5990     -- Standard call to get message count and if count is  get message info.
5991     FND_MSG_PUB.Count_And_Get
5992         (p_encoded => FND_API.G_FALSE,
5993                  p_count     =>     x_msg_count ,
5994           p_data     =>     x_msg_data
5995         );
5996 
5997 
5998 EXCEPTION
5999     WHEN FND_API.G_EXC_ERROR THEN
6000       --  ROLLBACK TO get_extended_attrib_values;
6001         x_return_status := FND_API.G_RET_STS_ERROR ;
6002         FND_MSG_PUB.Count_And_Get
6003             (  p_encoded => FND_API.G_FALSE,
6004                  p_count     =>      x_msg_count,
6005                p_data      =>      x_msg_data
6006              );
6007 
6008     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6009       --  ROLLBACK TO get_extended_attrib_values;
6010         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6011         FND_MSG_PUB.Count_And_Get
6012             (   p_encoded => FND_API.G_FALSE,
6013                  p_count    =>      x_msg_count,
6014                 p_data     =>      x_msg_data
6015              );
6016 
6017     WHEN OTHERS THEN
6018        IF DBMS_SQL.IS_OPEN(l_cur_get_ext) THEN
6019               DBMS_SQL.CLOSE_CURSOR(l_cur_get_ext);
6020         END IF;
6021 
6022       --  ROLLBACK TO  get_extended_attrib_values;
6023         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6024 
6025         IF  FND_MSG_PUB.Check_Msg_Level
6026             (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
6027         THEN
6028                 FND_MSG_PUB.Add_Exc_Msg
6029                 (    g_pkg_name,
6030                     l_api_name
6031                  );
6032         END IF;
6033 
6034         FND_MSG_PUB.Count_And_Get
6035             (   p_encoded => FND_API.G_FALSE,
6036                  p_count     =>      x_msg_count,
6037                 p_data      =>      x_msg_data
6038              );
6039 
6040 END get_extended_attrib_values;
6041 
6042 /*----------------------------------------------------*/
6043 /* procedure name: create_extended_attrib_values      */
6044 /* description :  Associates extended attribute       */
6045 /*                values to an item instance          */
6046 /*----------------------------------------------------*/
6047 
6048 PROCEDURE create_extended_attrib_values
6049  (    p_api_version        IN     NUMBER
6050      ,p_commit             IN     VARCHAR2
6051      ,p_init_msg_list      IN     VARCHAR2
6052      ,p_validation_level   IN     NUMBER
6053      ,p_ext_attrib_tbl     IN OUT NOCOPY csi_datastructures_pub.extend_attrib_values_tbl
6054      ,p_txn_rec            IN OUT NOCOPY csi_datastructures_pub.transaction_rec
6055      ,x_return_status         OUT NOCOPY VARCHAR2
6056      ,x_msg_count             OUT NOCOPY NUMBER
6057      ,x_msg_data              OUT NOCOPY VARCHAR2
6058  )
6059 
6060 IS
6061      l_api_name       CONSTANT VARCHAR2(30)   := 'create_extended_attrib_values';
6062      l_api_version    CONSTANT NUMBER         := 1.0;
6063      l_debug_level             NUMBER;
6064      l_msg_index               NUMBER;
6065      l_msg_count               NUMBER;
6066      l_trace_enable_flag       VARCHAR2(1)    :='N';
6067      l_ext_id_tbl              csi_item_instance_pvt.ext_id_tbl;
6068      l_ext_count_rec           csi_item_instance_pvt.ext_count_rec;
6069      l_ext_attr_tbl            csi_item_instance_pvt.ext_attr_tbl;
6070      l_ext_cat_tbl             csi_item_instance_pvt.ext_cat_tbl;
6071 BEGIN
6072 
6073     -- Standard Start of API savepoint
6074     SAVEPOINT    create_extended_attrib_values;
6075 
6076     -- Check for freeze_flag in csi_install_parameters is set to 'Y'
6077 
6078         csi_utility_grp.check_ib_active;
6079 
6080     -- Standard call to check for call compatibility.
6081     IF NOT FND_API.Compatible_API_Call (l_api_version,
6082                                         p_api_version,
6083                                         l_api_name ,
6084                                         g_pkg_name)
6085     THEN
6086         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6087     END IF;
6088 
6089 
6090     -- Initialize message list if p_init_msg_list is set to TRUE.
6091     IF FND_API.to_Boolean( p_init_msg_list ) THEN
6092         FND_MSG_PUB.initialize;
6093     END IF;
6094 
6095 
6096     --  Initialize API return status to success
6097     x_return_status := FND_API.G_RET_STS_SUCCESS;
6098 
6099 
6100     -- Check the profile option debug_level for debug message reporting
6101 
6102     l_debug_level:=fnd_profile.value('CSI_DEBUG_LEVEL');
6103 
6104     -- If debug_level = 1 then dump the procedure name
6105     IF (l_debug_level > 0) THEN
6106         csi_gen_utility_pvt.put_line( 'create_extended_attrib_values');
6107     END IF;
6108 
6109 
6110     -- If the debug level = 2 then dump all the parameters values.
6111     IF (l_debug_level > 1) THEN
6112      csi_gen_utility_pvt.put_line( p_api_version ||'-'
6113                      || p_commit                    ||'-'
6114                      || p_init_msg_list             ||'-'
6115                      || p_validation_level );
6116 
6117      -- Dump extended attribs tbl
6118      csi_gen_utility_pvt.dump_ext_attrib_values_tbl(p_ext_attrib_tbl);
6119      -- Dump txn_rec
6120      csi_gen_utility_pvt.dump_txn_rec(p_txn_rec);
6121 
6122     END IF;
6123 
6124     /***** srramakr commented for bug # 3304439
6125     -- Check for the profile option and enable trace
6126         l_trace_enable_flag:=CSI_GEN_UTILITY_PVT.enable_trace(l_trace_flag => l_trace_enable_flag);
6127     -- End enable trace
6128     ****/
6129 
6130 
6131     -- Start API body
6132 
6133     IF p_ext_attrib_tbl.COUNT > 0 THEN
6134          FOR tab_row IN p_ext_attrib_tbl.FIRST .. p_ext_attrib_tbl.LAST
6135          LOOP
6136             IF p_ext_attrib_tbl.EXISTS(tab_row) THEN
6137                csi_item_instance_pvt.create_extended_attrib_values
6138                 ( p_api_version         => p_api_version
6139                  ,p_commit              => fnd_api.g_false
6140                  ,p_init_msg_list       => p_init_msg_list
6141                  ,p_validation_level    => p_validation_level
6142                  ,p_ext_attrib_rec      => p_ext_attrib_tbl(tab_row)
6143                  ,p_txn_rec             => p_txn_rec
6144                  ,x_return_status       => x_return_status
6145                  ,x_msg_count           => x_msg_count
6146                  ,x_msg_data            => x_msg_data
6147                  ,p_ext_id_tbl          => l_ext_id_tbl
6148                  ,p_ext_count_rec       => l_ext_count_rec
6149                  ,p_ext_attr_tbl        => l_ext_attr_tbl
6150                  ,p_ext_cat_tbl         => l_ext_cat_tbl
6151                 );
6152 
6153                IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
6154                   l_msg_index := 1;
6155                    l_msg_count := x_msg_count;
6156                   WHILE l_msg_count > 0 LOOP
6157                      x_msg_data := FND_MSG_PUB.GET
6158                                       (l_msg_index,
6159                                      FND_API.G_FALSE      );
6160 
6161                      csi_gen_utility_pvt.put_line( ' Failed Pub:create_extended_attrib_values..');
6162                      csi_gen_utility_pvt.put_line('message data = '||x_msg_data);
6163                      l_msg_index := l_msg_index + 1;
6164                      l_msg_count := l_msg_count - 1;
6165                   END LOOP;
6166                   RAISE FND_API.G_EXC_ERROR;
6167                END IF;
6168             END IF;
6169          END LOOP;
6170     END IF;
6171 
6172     -- End of API body
6173 
6174 
6175     -- Standard check of p_commit.
6176     IF FND_API.To_Boolean( p_commit ) THEN
6177         COMMIT WORK;
6178     END IF;
6179 
6180     /***** srramakr commented for bug # 3304439
6181     -- Check for the profile option and disable the trace
6182     IF (fnd_profile.value('CSI_ENABLE_SQL_TRACE') = 'Y') THEN
6183          dbms_session.set_sql_trace(false);
6184     END IF;
6185     -- End disable trace
6186     ****/
6187 
6188 
6189     -- Standard call to get message count and if count is  get message info.
6190     FND_MSG_PUB.Count_And_Get
6191         (p_encoded => FND_API.G_FALSE,
6192                  p_count     =>     x_msg_count ,
6193           p_data     =>     x_msg_data
6194         );
6195 
6196 
6197 EXCEPTION
6198 
6199     WHEN FND_API.G_EXC_ERROR THEN
6200         ROLLBACK TO create_extended_attrib_values;
6201         x_return_status := FND_API.G_RET_STS_ERROR ;
6202         FND_MSG_PUB.Count_And_Get
6203             (   p_encoded => FND_API.G_FALSE,
6204                  p_count    =>      x_msg_count,
6205                 p_data     =>      x_msg_data
6206              );
6207 
6208     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6209         ROLLBACK TO create_extended_attrib_values;
6210         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6211         FND_MSG_PUB.Count_And_Get
6212             (   p_encoded => FND_API.G_FALSE,
6213                  p_count    =>      x_msg_count,
6214                 p_data     =>      x_msg_data
6215             );
6216 
6217     WHEN OTHERS THEN
6218 
6219         ROLLBACK TO  create_extended_attrib_values;
6220         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6221 
6222         IF     FND_MSG_PUB.Check_Msg_Level
6223             (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
6224         THEN
6225                 FND_MSG_PUB.Add_Exc_Msg
6226                 (   g_pkg_name,
6227                     l_api_name
6228                 );
6229         END IF;
6230 
6231         FND_MSG_PUB.Count_And_Get
6232             (   p_encoded => FND_API.G_FALSE,
6233                  p_count    =>      x_msg_count,
6234                 p_data     =>      x_msg_data
6235             );
6236 
6237 END create_extended_attrib_values;
6238 
6239 /*----------------------------------------------------*/
6240 /* procedure name: update_extended_attrib_values      */
6241 /* description :  Updates extended attrib values for  */
6242 /*                for an item instance                */
6243 /*----------------------------------------------------*/
6244 
6245 PROCEDURE update_extended_attrib_values
6246  (    p_api_version        IN     NUMBER
6247      ,p_commit             IN     VARCHAR2
6248      ,p_init_msg_list      IN     VARCHAR2
6249      ,p_validation_level   IN     NUMBER
6250      ,p_ext_attrib_tbl     IN     csi_datastructures_pub.extend_attrib_values_tbl
6251      ,p_txn_rec            IN OUT NOCOPY csi_datastructures_pub.transaction_rec
6252      ,x_return_status         OUT NOCOPY VARCHAR2
6253      ,x_msg_count             OUT NOCOPY NUMBER
6254      ,x_msg_data              OUT NOCOPY VARCHAR2
6255  )
6256 
6257 IS
6258     l_api_name          CONSTANT VARCHAR2(30)   := 'update_extended_attrib_values';
6259     l_api_version       CONSTANT NUMBER         := 1.0;
6260     l_debug_level                NUMBER;
6261     l_msg_index                  NUMBER;
6262     l_msg_count                  NUMBER;
6263     l_trace_enable_flag          VARCHAR2(1)    :='N';
6264     l_ext_id_tbl                 csi_item_instance_pvt.ext_id_tbl;
6265     l_ext_count_rec              csi_item_instance_pvt.ext_count_rec;
6266     l_ext_attr_tbl               csi_item_instance_pvt.ext_attr_tbl;
6267     l_ext_cat_tbl                csi_item_instance_pvt.ext_cat_tbl;
6268 BEGIN
6269 
6270     -- Standard Start of API savepoint
6271     SAVEPOINT    update_extended_attrib_values;
6272 
6273         -- Check for freeze_flag in csi_install_parameters is set to 'Y'
6274 
6275         csi_utility_grp.check_ib_active;
6276 
6277         -- Standard call to check for call compatibility.
6278     IF NOT FND_API.Compatible_API_Call (l_api_version,
6279                                         p_api_version,
6280                                         l_api_name,
6281                                         g_pkg_name)
6282     THEN
6283         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6284     END IF;
6285 
6286 
6287     -- Initialize message list if p_init_msg_list is set to TRUE.
6288     IF FND_API.to_Boolean( p_init_msg_list ) THEN
6289         FND_MSG_PUB.initialize;
6290     END IF;
6291 
6292 
6293     --  Initialize API return status to success
6294     x_return_status := FND_API.G_RET_STS_SUCCESS;
6295 
6296 
6297     -- Check the profile option debug_level for debug message reporting
6298     l_debug_level:=fnd_profile.value('CSI_DEBUG_LEVEL');
6299 
6300     -- If debug_level = 1 then dump the procedure name
6301     IF (l_debug_level > 0) THEN
6302         csi_gen_utility_pvt.put_line( 'update_extended_attrib_values');
6303     END IF;
6304 
6305 
6306     -- If the debug level = 2 then dump all the parameters values.
6307     IF (l_debug_level > 1) THEN
6308        csi_gen_utility_pvt.put_line( p_api_version ||'-'
6309                      || p_commit                    ||'-'
6310                      || p_init_msg_list             ||'-'
6311                      || p_validation_level );
6312 
6313        -- Dump extended attribs tbl
6314        csi_gen_utility_pvt.dump_ext_attrib_values_tbl(p_ext_attrib_tbl);
6315        -- Dump txn_rec
6316        csi_gen_utility_pvt.dump_txn_rec(p_txn_rec);
6317     END IF;
6318 
6319     /***** srramakr commented for bug # 3304439
6320     -- Check for the profile option and enable trace
6321         l_trace_enable_flag:=CSI_GEN_UTILITY_PVT.enable_trace(l_trace_flag => l_trace_enable_flag);
6322     -- End enable trace
6323     ****/
6324 
6325 
6326     -- Start API body
6327      IF p_ext_attrib_tbl.COUNT > 0 THEN
6328          FOR tab_row IN p_ext_attrib_tbl.FIRST .. p_ext_attrib_tbl.LAST
6329          LOOP
6330           IF p_ext_attrib_tbl.EXISTS(tab_row) THEN
6331              csi_item_instance_pvt.update_extended_attrib_values
6332              (p_api_version       => p_api_version
6333              ,p_commit            => fnd_api.g_false
6334              ,p_init_msg_list     => p_init_msg_list
6335              ,p_validation_level  => p_validation_level
6336              ,p_ext_attrib_rec    => p_ext_attrib_tbl(tab_row)
6337              ,p_txn_rec           => p_txn_rec
6338              ,x_return_status     => x_return_status
6339              ,x_msg_count         => x_msg_count
6340              ,x_msg_data          => x_msg_data
6341 --             ,p_ext_id_tbl        => l_ext_id_tbl
6342 --             ,p_ext_count_rec     => l_ext_count_rec
6343 --             ,p_ext_attr_tbl      => l_ext_attr_tbl
6344 --             ,p_ext_cat_tbl       => l_ext_cat_tbl
6345              );
6346 
6347              IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
6348                 l_msg_index := 1;
6349                    l_msg_count := x_msg_count;
6350                 WHILE l_msg_count > 0 LOOP
6351                      x_msg_data := FND_MSG_PUB.GET
6352                           (l_msg_index,
6353                            FND_API.G_FALSE      );
6354 
6355                      csi_gen_utility_pvt.put_line( ' Failed Pub:update_extended_attrib_values..');
6356                      csi_gen_utility_pvt.put_line('message data = '||x_msg_data);
6357                      l_msg_index := l_msg_index + 1;
6358                      l_msg_count := l_msg_count - 1;
6359                 END LOOP;
6360                 RAISE FND_API.G_EXC_ERROR;
6361              END IF;
6362 
6363           END IF;
6364          END LOOP;
6365     END IF;
6366 
6367     -- End of API body
6368 
6369     -- Standard check of p_commit.
6370     IF FND_API.To_Boolean( p_commit ) THEN
6371         COMMIT WORK;
6372     END IF;
6373 
6374     /***** srramakr commented for bug # 3304439
6375     -- Check for the profile option and disable the trace
6376     IF (l_trace_enable_flag = 'Y') THEN
6377        dbms_session.set_sql_trace(false);
6378     END IF;
6379     -- End disable trace
6380     ****/
6381 
6382     -- Standard call to get message count and if count is  get message info.
6383     FND_MSG_PUB.Count_And_Get
6384         (p_encoded => FND_API.G_FALSE,
6385                  p_count     =>     x_msg_count ,
6386           p_data     =>     x_msg_data
6387         );
6388 
6389 
6390 EXCEPTION
6391 
6392     WHEN FND_API.G_EXC_ERROR THEN
6393         ROLLBACK TO update_extended_attrib_values;
6394         x_return_status := FND_API.G_RET_STS_ERROR ;
6395         FND_MSG_PUB.Count_And_Get
6396              (  p_encoded => FND_API.G_FALSE,
6397                  p_count      =>      x_msg_count,
6398                 p_data       =>      x_msg_data
6399              );
6400 
6401     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6402         ROLLBACK TO update_extended_attrib_values;
6403         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6404         FND_MSG_PUB.Count_And_Get
6405             (   p_encoded => FND_API.G_FALSE,
6406                  p_count      =>      x_msg_count,
6407                 p_data       =>      x_msg_data
6408             );
6409 
6410     WHEN OTHERS THEN
6411         ROLLBACK TO  update_extended_attrib_values;
6412         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6413 
6414         IF     FND_MSG_PUB.Check_Msg_Level
6415                (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
6416         THEN
6417                 FND_MSG_PUB.Add_Exc_Msg
6418                 (   g_pkg_name          ,
6419                     l_api_name
6420                  );
6421         END IF;
6422 
6423         FND_MSG_PUB.Count_And_Get
6424             (   p_encoded => FND_API.G_FALSE,
6425                  p_count      =>      x_msg_count,
6426                 p_data       =>      x_msg_data
6427              );
6428 
6429 END update_extended_attrib_values;
6430 
6431 /*----------------------------------------------------*/
6432 /* procedure name: Expire_extended_attrib_values      */
6433 /* description :  Expires extended attribute values   */
6434 /*                for an item instance                */
6435 /*----------------------------------------------------*/
6436 
6437 PROCEDURE expire_extended_attrib_values
6438  (    p_api_version         IN     NUMBER
6439      ,p_commit              IN     VARCHAR2
6440      ,p_init_msg_list       IN     VARCHAR2
6441      ,p_validation_level    IN     NUMBER
6442      ,p_ext_attrib_tbl      IN     csi_datastructures_pub.extend_attrib_values_tbl
6443      ,p_txn_rec             IN OUT NOCOPY csi_datastructures_pub.transaction_rec
6444      ,x_return_status          OUT NOCOPY VARCHAR2
6445      ,x_msg_count              OUT NOCOPY NUMBER
6446      ,x_msg_data               OUT NOCOPY VARCHAR2
6447  )
6448 
6449 IS
6450     l_api_name          CONSTANT VARCHAR2(30)  := 'delete_extended_attrib_values';
6451     l_api_version       CONSTANT NUMBER        := 1.0;
6452     l_debug_level                NUMBER;
6453     l_msg_index                  NUMBER;
6454     l_msg_count                  NUMBER;
6455     l_trace_enable_flag          VARCHAR2(1)   :='N';
6456 BEGIN
6457 
6458     -- Standard Start of API savepoint
6459     SAVEPOINT    delete_extended_attrib_values;
6460 
6461         -- Check for freeze_flag in csi_install_parameters is set to 'Y'
6462 
6463         csi_utility_grp.check_ib_active;
6464 
6465         -- Standard call to check for call compatibility.
6466     IF NOT FND_API.Compatible_API_Call (l_api_version,
6467                                         p_api_version,
6468                                         l_api_name ,
6469                                         g_pkg_name)
6470     THEN
6471         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6472     END IF;
6473 
6474 
6475         -- Initialize message list if p_init_msg_list is set to TRUE.
6476     IF FND_API.to_Boolean( p_init_msg_list ) THEN
6477         FND_MSG_PUB.initialize;
6478     END IF;
6479 
6480 
6481     --  Initialize API return status to success
6482     x_return_status := FND_API.G_RET_STS_SUCCESS;
6483 
6484 
6485     -- Check the profile option debug_level for debug message reporting
6486 
6487     l_debug_level:=fnd_profile.value('CSI_DEBUG_LEVEL');
6488 
6489     -- If debug_level = 1 then dump the procedure name
6490     IF (l_debug_level > 0) THEN
6491         csi_gen_utility_pvt.put_line( 'delete_extended_attrib_values');
6492     END IF;
6493 
6494 
6495     -- If the debug level = 2 then dump all the parameters values.
6496     IF (l_debug_level > 1) THEN
6497       csi_gen_utility_pvt.put_line( p_api_version ||'-'
6498                      || p_commit                    ||'-'
6499                      || p_init_msg_list             ||'-'
6500                      || p_validation_level     );
6501       -- Dump extended attribs tbl
6502       csi_gen_utility_pvt.dump_ext_attrib_values_tbl(p_ext_attrib_tbl);
6503       -- Dump txn_rec
6504       csi_gen_utility_pvt.dump_txn_rec(p_txn_rec);
6505     END IF;
6506 
6507     /***** srramakr commented for bug # 3304439
6508     -- Check for the profile option and enable trace
6509         l_trace_enable_flag:=CSI_GEN_UTILITY_PVT.enable_trace(l_trace_flag => l_trace_enable_flag);
6510     -- End enable trace
6511     ****/
6512 
6513 
6514      -- Start API body
6515 
6516      IF p_ext_attrib_tbl.COUNT > 0 THEN
6517          FOR tab_row IN p_ext_attrib_tbl.FIRST .. p_ext_attrib_tbl.LAST
6518          LOOP
6519             IF p_ext_attrib_tbl.EXISTS(tab_row) THEN
6520 
6521                csi_item_instance_pvt.expire_extended_attrib_values
6522                 ( p_api_version      => p_api_version
6523                  ,p_commit           => fnd_api.g_false
6524                  ,p_init_msg_list    => p_init_msg_list
6525                  ,p_validation_level => p_validation_level
6526                  ,p_ext_attrib_rec   => p_ext_attrib_tbl(tab_row)
6527                  ,p_txn_rec          => p_txn_rec
6528                  ,x_return_status    => x_return_status
6529                  ,x_msg_count        => x_msg_count
6530                  ,x_msg_data         => x_msg_data
6531                 );
6532 
6533                 IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
6534                    l_msg_index := 1;
6535                    l_msg_count := x_msg_count;
6536                    WHILE l_msg_count > 0 LOOP
6537                    x_msg_data := FND_MSG_PUB.GET
6538                                       (l_msg_index,
6539                                      FND_API.G_FALSE      );
6540 
6541                      csi_gen_utility_pvt.put_line( ' Failed Pub:expire_extended_attrib_values..');
6542                      csi_gen_utility_pvt.put_line('message data = '||x_msg_data);
6543                      l_msg_index := l_msg_index + 1;
6544                      l_msg_count := l_msg_count - 1;
6545                    END LOOP;
6546                    RAISE FND_API.G_EXC_ERROR;
6547                  END IF;
6548             END IF;
6549          END LOOP;
6550      END IF;
6551 
6552     -- End of API body
6553 
6554 
6555     -- Standard check of p_commit.
6556     IF FND_API.To_Boolean( p_commit ) THEN
6557         COMMIT WORK;
6558     END IF;
6559 
6560     /***** srramakr commented for bug # 3304439
6561     -- Check for the profile option and disable the trace
6562     IF (l_trace_enable_flag = 'Y') THEN
6563        dbms_session.set_sql_trace(false);
6564     END IF;
6565     -- End disable trace
6566     ****/
6567 
6568 
6569     -- Standard call to get message count and if count is  get message info.
6570     FND_MSG_PUB.Count_And_Get
6571         (p_encoded => FND_API.G_FALSE,
6572                  p_count     =>     x_msg_count ,
6573           p_data     =>     x_msg_data
6574         );
6575 
6576 
6577 EXCEPTION
6578 
6579     WHEN FND_API.G_EXC_ERROR THEN
6580         ROLLBACK TO delete_extended_attrib_values;
6581         x_return_status := FND_API.G_RET_STS_ERROR ;
6582         FND_MSG_PUB.Count_And_Get
6583             (   p_encoded => FND_API.G_FALSE,
6584                  p_count      =>      x_msg_count,
6585                 p_data       =>      x_msg_data
6586             );
6587 
6588     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6589         ROLLBACK TO delete_extended_attrib_values;
6590         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6591         FND_MSG_PUB.Count_And_Get
6592             (   p_encoded => FND_API.G_FALSE,
6593                  p_count      =>      x_msg_count,
6594                 p_data       =>      x_msg_data
6595             );
6596 
6597     WHEN OTHERS THEN
6598         ROLLBACK TO  delete_extended_attrib_values;
6599         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6600 
6601           IF FND_MSG_PUB.Check_Msg_Level
6602             (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
6603           THEN
6604                 FND_MSG_PUB.Add_Exc_Msg
6605                 (    g_pkg_name ,
6606                      l_api_name
6607                  );
6608           END IF;
6609 
6610         FND_MSG_PUB.Count_And_Get
6611             (   p_encoded => FND_API.G_FALSE,
6612                  p_count     =>      x_msg_count,
6613                 p_data      =>      x_msg_data
6614             );
6615 
6616 END expire_extended_attrib_values;
6617 
6618 
6619 
6620 
6621 /*------------------------------------------------------*/
6622 /* procedure name: copy_item_instance                   */
6623 /* description :  Copies an instace from an instance    */
6624 /*                                                      */
6625 /*------------------------------------------------------*/
6626 
6627 PROCEDURE copy_item_instance
6628  (
6629    p_api_version            IN         NUMBER
6630   ,p_commit                 IN         VARCHAR2
6631   ,p_init_msg_list          IN         VARCHAR2
6632   ,p_validation_level       IN         NUMBER
6633   ,p_source_instance_rec    IN         csi_datastructures_pub.instance_rec
6634   ,p_copy_ext_attribs       IN         VARCHAR2
6635   ,p_copy_org_assignments   IN         VARCHAR2
6636   ,p_copy_parties           IN         VARCHAR2
6637   ,p_copy_party_contacts    IN         VARCHAR2
6638   ,p_copy_accounts          IN         VARCHAR2
6639   ,p_copy_asset_assignments IN         VARCHAR2
6640   ,p_copy_pricing_attribs   IN         VARCHAR2
6641   ,p_copy_inst_children     IN         VARCHAR2
6642   ,p_txn_rec                IN  OUT    NOCOPY csi_datastructures_pub.transaction_rec
6643   ,x_new_instance_tbl           OUT    NOCOPY csi_datastructures_pub.instance_tbl
6644   ,x_return_status              OUT    NOCOPY VARCHAR2
6645   ,x_msg_count                  OUT    NOCOPY NUMBER
6646   ,x_msg_data                   OUT    NOCOPY VARCHAR2
6647  )
6648  IS
6649 
6650     l_api_name                         CONSTANT VARCHAR2(30) := 'copy_item_instance';
6651     l_api_version                     CONSTANT NUMBER      := 1.0;
6652     l_debug_level                                NUMBER;
6653 
6654     l_msg_index                             NUMBER;
6655     l_msg_count                             NUMBER;
6656     x_msg_index_out                         NUMBER;
6657 BEGIN
6658 
6659     -- Standard Start of API savepoint
6660           SAVEPOINT      copy_item_instance;
6661 
6662         -- Check for freeze_flag in csi_install_parameters is set to 'Y'
6663 
6664         csi_utility_grp.check_ib_active;
6665 
6666     -- Standard call to check for call compatibility.
6667       IF NOT FND_API.Compatible_API_Call (l_api_version,
6668                                           p_api_version,
6669                                           l_api_name ,
6670                                           G_PKG_NAME)
6671       THEN
6672             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6673       END IF;
6674 
6675    -- Initialize message list if p_init_msg_list is set to TRUE.
6676       IF FND_API.to_Boolean( p_init_msg_list ) THEN
6677             FND_MSG_PUB.initialize;
6678       END IF;
6679 
6680       --  Initialize API return status to success
6681           x_return_status := FND_API.G_RET_STS_SUCCESS;
6682 
6683     -- Check the profile option debug_level for debug message reporting
6684           l_debug_level:=fnd_profile.value('CSI_DEBUG_LEVEL');
6685 
6686     -- If debug_level = 1 then dump the procedure name
6687    IF (l_debug_level > 0) THEN
6688        csi_gen_utility_pvt.put_line('copy_item_instance ');
6689    END IF;
6690 
6691     -- If the debug level = 2 then dump all the parameters values.
6692     IF (l_debug_level > 1) THEN
6693          csi_gen_utility_pvt.put_line('copy_item_instance '||
6694                               p_api_version ||'-'
6695                            || p_commit ||'-'
6696                            || p_init_msg_list ||'-'
6697                            || p_validation_level ||'-'
6698                            || p_copy_ext_attribs ||'-'
6699                            || p_copy_org_assignments ||'-'
6700                            || p_copy_parties  ||'-'
6701                            || p_copy_party_contacts  ||'-'
6702                            || p_copy_accounts  ||'-'
6703                            || p_copy_asset_assignments ||'-'
6704                            || p_copy_pricing_attribs
6705                             );
6706 
6707      -- Dump item instanc red
6708          csi_gen_utility_pvt.dump_instance_rec(p_source_instance_rec);
6709      -- Dump txn_rec
6710          csi_gen_utility_pvt.dump_txn_rec(p_txn_rec);
6711 
6712     END IF;
6713 
6714     -- Start API body
6715 
6716      csi_item_instance_pvt.copy_item_instance
6717            ( p_api_version                 => p_api_version
6718             ,p_commit                      => fnd_api.g_false
6719             ,p_init_msg_list               => p_init_msg_list
6720             ,p_validation_level            => p_validation_level
6721             ,p_source_instance_rec         => p_source_instance_rec
6722             ,p_copy_ext_attribs            => p_copy_ext_attribs
6723             ,p_copy_org_assignments        => p_copy_org_assignments
6724             ,p_copy_parties                => p_copy_parties
6725             ,p_copy_contacts               => p_copy_party_contacts
6726             ,p_copy_accounts               => p_copy_accounts
6727             ,p_copy_asset_assignments      => p_copy_asset_assignments
6728             ,p_copy_pricing_attribs        => p_copy_pricing_attribs
6729             ,p_copy_inst_children          => p_copy_inst_children
6730             ,p_call_from_split             => fnd_api.g_false
6731             ,p_txn_rec                     => p_txn_rec
6732             ,x_new_instance_tbl            => x_new_instance_tbl
6733             ,x_return_status               => x_return_status
6734             ,x_msg_count                   => x_msg_count
6735             ,x_msg_data                    => x_msg_data
6736            );
6737 
6738        IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
6739           FOR i in 1..x_msg_Count LOOP
6740             FND_MSG_PUB.Get(p_msg_index     => i,
6741                             p_encoded       => 'F',
6742                             p_data          => x_msg_data,
6743                             p_msg_index_out => x_msg_index_out );
6744 
6745             csi_gen_utility_pvt.put_line('message data = '||x_msg_data);
6746          End LOOP;
6747          RAISE fnd_api.g_exc_error;
6748        END IF;
6749 
6750        -- End of API body
6751 
6752        -- Standard check of p_commit
6753        IF FND_API.To_Boolean( p_commit ) THEN
6754                COMMIT WORK;
6755        END IF;
6756 
6757 
6758        -- Standard call to get message count and if count is  get message info.
6759 
6760           FND_MSG_PUB.Count_And_Get
6761           (p_encoded => FND_API.G_FALSE,
6762                  p_count       =>       x_msg_count ,
6763          p_data       =>       x_msg_data
6764            );
6765 
6766 
6767 EXCEPTION
6768     WHEN FND_API.G_EXC_ERROR THEN
6769             ROLLBACK TO copy_item_instance;
6770             x_return_status := FND_API.G_RET_STS_ERROR ;
6771             FND_MSG_PUB.Count_And_Get
6772                 (        p_encoded => FND_API.G_FALSE,
6773                  p_count               =>      x_msg_count,
6774                     p_data                =>      x_msg_data
6775                  );
6776 
6777       WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6778             ROLLBACK TO copy_item_instance;
6779             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6780             FND_MSG_PUB.Count_And_Get
6781                 (        p_encoded => FND_API.G_FALSE,
6782                  p_count               =>      x_msg_count,
6783                     p_data                =>      x_msg_data
6784                  );
6785 
6786       WHEN OTHERS THEN
6787             ROLLBACK TO  copy_item_instance;
6788             x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6789               IF       FND_MSG_PUB.Check_Msg_Level
6790                   (FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
6791             THEN
6792                     FND_MSG_PUB.Add_Exc_Msg
6793                     (      G_PKG_NAME            ,
6794                           l_api_name
6795                      );
6796             END IF;
6797 
6798             FND_MSG_PUB.Count_And_Get
6799                 (        p_encoded => FND_API.G_FALSE,
6800                  p_count               =>      x_msg_count,
6801                     p_data                =>      x_msg_data
6802                 );
6803 
6804 
6805 END  copy_item_instance;
6806 
6807 PROCEDURE get_oks_txn_types (
6808          p_api_version            IN                    NUMBER
6809         ,p_commit                 IN                    VARCHAR2
6810         ,p_init_msg_list          IN                    VARCHAR2
6811         ,p_instance_rec           IN                    CSI_DATASTRUCTURES_PUB.INSTANCE_REC
6812         ,p_check_contracts_yn     IN                    VARCHAR2
6813         ,p_txn_type               IN                    VARCHAR2
6814         ,x_txn_type_tbl                OUT    NOCOPY    CSI_ITEM_INSTANCE_PUB.TXN_OKS_TYPE_TBL
6815         ,x_configflag                  OUT    NOCOPY    VARCHAR2
6816         ,px_txn_date              IN   OUT    NOCOPY    DATE
6817         ,x_imp_contracts_flag          OUT    NOCOPY    VARCHAR2
6818         ,x_return_status               OUT    NOCOPY    VARCHAR2
6819         ,x_msg_count                   OUT    NOCOPY    NUMBER
6820         ,x_msg_data                    OUT    NOCOPY    VARCHAR2
6821         ) IS
6822 
6823    CURSOR old_instance_rec ( cp_instance_id NUMBER) is
6824    SELECT quantity, install_date, active_end_date
6825      FROM csi_item_instances
6826     WHERE instance_id = cp_instance_id;
6827 
6828     l_curr_instance_rec     old_instance_rec%ROWTYPE;
6829     l_row_index             NUMBER := 0;
6830     l_exists                VARCHAR(1);
6831     l_msg_count             NUMBER;
6832     l_msg_index             NUMBER;
6833     l_oks_txn_tbl           oks_ibint_pub.txn_tbl_type;
6834 
6835   BEGIN
6836     SAVEPOINT  get_oks_txn_types;
6837 
6838     OPEN  old_instance_rec(p_instance_rec.INSTANCE_ID);
6839     FETCH old_instance_rec INTO l_curr_instance_rec;
6840     CLOSE old_instance_rec;
6841 
6842     IF p_txn_type IS NULL THEN
6843 
6844       IF l_curr_instance_rec.quantity <> p_instance_rec.quantity THEN
6845         x_txn_type_tbl(l_row_index) := 'UPD';
6846         l_row_index := l_row_index + 1;
6847       END IF;
6848 
6849       IF NVL(l_curr_instance_rec.install_date,FND_API.G_MISS_DATE) <>
6850           NVL(p_instance_rec.install_date,FND_API.G_MISS_DATE) THEN
6851         x_txn_type_tbl(l_row_index) := 'IDC';
6852         l_row_index := l_row_index + 1;
6853       END IF;
6854 
6855       IF p_instance_rec.active_end_date IS NOT NULL AND
6856          p_instance_rec.active_end_date <> FND_API.G_MISS_DATE AND
6857          p_instance_rec.active_end_date <= SYSDATE THEN
6858          x_txn_type_tbl(l_row_index) := 'TRM';
6859          l_row_index := l_row_index + 1;
6860       END IF;
6861 
6862 /*
6863       IF l_curr_instance_rec.active_end_date IS NOT NULL AND
6864          l_curr_instance_rec.active_end_date <= SYSDATE AND
6865          (p_instance_rec.active_end_date IS NULL OR
6866           (p_instance_rec.active_end_date IS NOT NULL AND
6867            p_instance_rec.active_end_date <> FND_API.G_MISS_DATE AND
6868            p_instance_rec.active_end_date > SYSDATE) ) THEN
6869 */
6870       IF (l_curr_instance_rec.active_end_date IS NOT NULL
6871          AND ( p_instance_rec.active_end_date <> FND_API.G_MISS_DATE or p_instance_rec.active_end_date is null))
6872          AND ((l_curr_instance_rec.active_end_date < p_instance_rec.active_end_date
6873          AND p_instance_rec.active_end_date > SYSDATE)
6874          OR (p_instance_rec.active_end_date IS NULL)) THEN
6875          x_txn_type_tbl(l_row_index) := 'RIN';
6876         l_row_index := l_row_index + 1;
6877       END IF;
6878 
6879     ELSE
6880       x_txn_type_tbl(l_row_index) := p_txn_type;
6881       l_row_index := l_row_index + 1;
6882     END IF;
6883 
6884     l_exists := NULL;
6885     Begin
6886       -- Commenting the following code for bug 4775959
6887       /*
6888        SELECT 'x' INTO l_exists
6889        FROM dual
6890        WHERE EXISTS (SELECT 'x'
6891                      FROM csi_ii_relationships
6892                      WHERE object_id = p_instance_rec.instance_id
6893                      AND relationship_type_code = 'COMPONENT-OF'
6894                      AND NVL (active_end_date,(sysdate+1)) > sysdate);
6895       */
6896         SELECT 'x'
6897           INTO l_exists
6898           FROM csi_ii_relationships
6899          WHERE object_id = p_instance_rec.instance_id
6900            AND relationship_type_code = 'COMPONENT-OF'
6901            AND NVL (active_end_date,(sysdate+1)) > sysdate
6902            AND ROWNUM=1;
6903     Exception
6904        when no_data_found then
6905           l_exists := null;
6906     End;
6907 
6908     IF l_exists IS NULL THEN
6909       x_configflag := 'N';
6910     ELSE
6911       x_configflag := 'Y';
6912     END IF;
6913 
6914     IF px_txn_date IS NULL THEN
6915       px_txn_date := sysdate;
6916     END IF;
6917 
6918     IF x_txn_type_tbl.count > 0 THEN
6919        FOR i in x_txn_type_tbl.FIRST..x_txn_type_tbl.LAST
6920        LOOP
6921           l_oks_txn_tbl(i) := x_txn_type_tbl(i);
6922        END LOOP;
6923     END IF;
6924 
6925     IF p_check_contracts_yn = 'Y' THEN
6926          OKS_IBINT_PUB.CHECK_CONTRACTS_IMPACTED(
6927            p_api_version        =>  1.0,
6928            P_instance_id        =>  p_instance_rec.INSTANCE_ID,
6929            p_parent_instance_yn =>  x_configflag,
6930            p_transaction_date   =>  px_txn_date,
6931            p_new_install_date   =>  p_instance_rec.install_date,
6932            P_txn_tbl            =>  l_oks_txn_tbl,
6933            x_contract_exists_yn =>  x_imp_contracts_flag,
6934            X_msg_Count          =>  x_msg_count,
6935            X_msg_Data           =>  x_msg_data,
6936            x_return_status      =>  x_return_status
6937            );
6938 
6939            IF l_oks_txn_tbl.count > 0 THEN
6940              FOR i in l_oks_txn_tbl.FIRST..l_oks_txn_tbl.LAST
6941              LOOP
6942                  x_txn_type_tbl(i) := l_oks_txn_tbl(i);
6943              END LOOP;
6944            END IF;
6945 
6946         IF NOT(x_return_status = FND_API.G_RET_STS_SUCCESS)
6947         THEN
6948     	 csi_gen_utility_pvt.put_line('Error from Call_to_contracts...');
6949     	 l_msg_index := 1;
6950 	     l_msg_count := x_msg_count;
6951     	 WHILE l_msg_count > 0 LOOP
6952 	      x_msg_data := FND_MSG_PUB.GET
6953 		       (  l_msg_index,
6954 			  FND_API.G_FALSE
6955 	       		);
6956           csi_gen_utility_pvt.put_line('MESSAGE DATA = '||x_msg_data);
6957 	      l_msg_index := l_msg_index + 1;
6958 	      l_msg_count := l_msg_count - 1;
6959     	 END LOOP;
6960     	 RAISE FND_API.G_EXC_ERROR;
6961       END IF;
6962 
6963      END IF;
6964 
6965 EXCEPTION
6966     WHEN FND_API.G_EXC_ERROR THEN
6967             ROLLBACK TO get_oks_txn_types;
6968             x_return_status := FND_API.G_RET_STS_ERROR ;
6969 
6970  END get_oks_txn_types;
6971 
6972 
6973 END CSI_ITEM_INSTANCE_PUB;