DBA Data[Home] [Help]

PACKAGE BODY: APPS.AHL_UC_INSTANCE_PVT

Source


1 PACKAGE BODY AHL_UC_INSTANCE_PVT AS
2 /* $Header: AHLVUCIB.pls 120.16.12010000.5 2008/11/28 07:03:23 sathapli ship $ */
3 
4 -- Define global internal variables
5 G_PKG_NAME VARCHAR2(30) := 'AHL_UC_INSTANCE_PVT';
6 
7 -- Define global cursors
8 CURSOR get_instance_date(c_instance_id NUMBER) IS
9   SELECT active_end_date
10     FROM csi_item_instances
11    WHERE instance_id = c_instance_id;
12 
13 -- Local validation procedures
14 -- Procedure to validate serial numbers
15 PROCEDURE validate_serialnumber(p_inventory_id           IN  NUMBER,
16                                 p_serial_number          IN  VARCHAR2,
17                                 p_serial_number_control  IN  NUMBER,
18                                 p_serialnum_tag_code     IN  VARCHAR2,
19                                 p_quantity               IN  NUMBER,
20                                 p_concatenated_segments  IN  VARCHAR2) IS
21   CURSOR mtl_serial_numbers_csr(c_inventory_id  IN NUMBER,
22                                 c_serial_number IN VARCHAR2) IS
23     SELECT 'X'
24       FROM mtl_serial_numbers
25      WHERE inventory_item_id = c_inventory_id
26        AND serial_number = c_serial_number;
27   l_junk       VARCHAR2(1);
28 BEGIN
29   --Validate serial number(1 = No serial number control; 2 = Pre-defined;
30   --3 = Dynamic Entry at inventory receipt.)
31   IF (nvl(p_serial_number_control,0) IN (2,5,6)) THEN
32     -- serial number is mandatory.
33     IF (p_serial_number IS NULL) OR (p_serial_number = FND_API.G_MISS_CHAR) THEN
34       FND_MESSAGE.set_name('AHL','AHL_UC_SERIAL_NULL');
35       FND_MESSAGE.set_token('INV_ITEM',p_concatenated_segments);
36       FND_MSG_PUB.add;
37       --dbms_output.put_line('Serial Number is null');
38     ELSE
39 /**
40       Commented out by jaramana on April 26, 2005 since IB does this validation.
41       -- If serial tag code = INVENTORY  then validate serial number against inventory.
42       IF (p_serialnum_tag_code = 'INVENTORY') THEN
43         OPEN  mtl_serial_numbers_csr(p_inventory_id,p_Serial_Number);
44         FETCH mtl_serial_numbers_csr INTO l_junk;
45         IF (mtl_serial_numbers_csr%NOTFOUND) THEN
46           FND_MESSAGE.set_name('AHL','AHL_UC_SERIAL_INVALID');
47           FND_MESSAGE.set_token('SERIAL',p_Serial_Number);
48           FND_MESSAGE.set_token('INV_ITEM',p_concatenated_segments);
49           FND_MSG_PUB.add;
50           --dbms_output.put_line('Serial Number does not exist in master ');
51         END IF;
52         CLOSE mtl_serial_numbers_csr;
53       END IF;
54 **/
55       -- Check quantity.
56       IF (nvl(p_quantity,0) <> 1)  THEN
57         FND_MESSAGE.set_name('AHL','AHL_UC_SRLQTY_MISMATCH');
58         FND_MESSAGE.set_token('QTY',p_quantity);
59         FND_MESSAGE.set_token('INV_ITEM',p_concatenated_segments);
60         FND_MSG_PUB.add;
61         --dbms_output.put_line('For serialized items Quantity must be 1');
62       END IF;
63     END IF;
64   ELSE
65     -- if not serialized item, then serial number must be null.
66     IF (p_serial_number <> FND_API.G_MISS_CHAR) AND (p_serial_number IS NOT NULL) THEN
67       FND_MESSAGE.set_name('AHL','AHL_UC_SERIAL_NOTNULL');
68       FND_MESSAGE.set_token('SERIAL',p_Serial_Number);
69       FND_MESSAGE.set_token('INV_ITEM',p_concatenated_segments);
70       FND_MSG_PUB.add;
71       --dbms_output.put_line('Serial Number is not null');
72     END IF;
73   END IF; /* for serial number control */
74 END validate_serialnumber;
75 
76 --Procedure to validate quantity
77 PROCEDURE validate_quantity(p_inventory_id          IN  NUMBER,
78                             p_organization_id       IN  NUMBER,
79                             p_quantity              IN  NUMBER,
80                             p_uom_code              IN  VARCHAR2,
81                             p_concatenated_segments IN  VARCHAR2) IS
82 BEGIN
83   --Validate quantity and UOM code.
84   IF (p_quantity = FND_API.G_MISS_NUM OR p_quantity IS NULL) THEN
85     FND_MESSAGE.set_name('AHL','AHL_UC_QTY_NULL');
86     FND_MESSAGE.set_token('INV_ITEM',p_concatenated_segments);
87     FND_MSG_PUB.add;
88     --dbms_output.put_line('Quantity is null.');
89   ELSIF (p_quantity <= 0) THEN
90     FND_MESSAGE.set_name('AHL','AHL_UC_QTY_INVALID');
91     FND_MESSAGE.set_token('QTY',p_quantity);
92     FND_MESSAGE.set_token('INV_ITEM',p_concatenated_segments);
93     FND_MSG_PUB.add;
94     --dbms_output.put_line('Quantity is less than or equal to zero.');
95   ELSE
96     --Call inv function to validate uom.
97     IF NOT(inv_convert.validate_Item_Uom(p_item_id          => p_inventory_id,
98                                          p_organization_id  => p_organization_id,
99                                          p_uom_code         => p_uom_code))
100     THEN
101       FND_MESSAGE.set_name('AHL','AHL_UC_UOM_INVALID');
102       FND_MESSAGE.set_token('UOM',p_uom_code);
103       FND_MESSAGE.set_token('INV_ITEM',p_concatenated_segments);
104       FND_MSG_PUB.add;
105       --dbms_output.put_line('Invalid UOM code for the item');
106     END IF;
107   END IF; /* for p_quantity */
108 END validate_quantity;
109 
110 --Procedure to validate Lot Number
111 PROCEDURE validate_lotnumber(p_inventory_id          IN  NUMBER,
112                              p_organization_id       IN  NUMBER,
113                              p_lot_control_code      IN  NUMBER,
114                              p_lot_number            IN  VARCHAR2,
115                              p_concatenated_segments IN  VARCHAR2) IS
116   CURSOR mtl_lot_numbers_csr(c_inventory_id      IN  NUMBER,
117                              c_organization_id   IN  NUMBER,
118                              c_lot_number        IN  VARCHAR2)  IS
119     SELECT 'X'
120       FROM mtl_lot_numbers
121      WHERE inventory_item_id = c_inventory_id
122        AND organization_id =  c_organization_id
123        AND lot_number =  c_lot_number
124        AND nvl(disable_flag,2) = 2;
125   l_junk  VARCHAR(1);
126 BEGIN
127   -- Validate Lot number.(1 = No lot control; 2 = Full lot control)
128   IF (nvl(p_lot_control_code,0) = 2) THEN
129     IF (p_lot_number IS NULL) OR (p_lot_number = FND_API.G_MISS_CHAR) THEN
130       FND_MESSAGE.set_name('AHL','AHL_UC_LOT_NULL');
131       FND_MESSAGE.set_token('INV_ITEM',p_concatenated_segments);
132       FND_MSG_PUB.add;
133       --dbms_output.put_line('Lot Number is null');
134     ELSE
135       OPEN mtl_lot_numbers_csr(p_inventory_id,p_organization_id, p_lot_number);
136       FETCH mtl_lot_numbers_csr INTO l_junk;
137       IF (mtl_lot_numbers_csr%NOTFOUND) THEN
138         FND_MESSAGE.set_name('AHL','AHL_UC_LOT_INVALID');
139         FND_MESSAGE.set_token('LOT',p_Lot_number);
140         FND_MESSAGE.set_token('INV_ITEM',p_concatenated_segments);
141         FND_MSG_PUB.add;
142         --dbms_output.put_line('Lot number does not exist in master');
143       END IF;
144       CLOSE mtl_lot_numbers_csr;
145     END IF;
146   ELSIF (p_Lot_number <> FND_API.G_MISS_CHAR) AND (p_lot_Number IS NOT NULL) THEN
147 
148     -- If lot number not controlled; then lot num must be null.
149     FND_MESSAGE.set_name('AHL','AHL_UC_LOT_NOTNULL');
150     --FND_MESSAGE.set_token('LOT',p_Lot_Number);
151     FND_MESSAGE.set_token('INV_ITEM',p_concatenated_segments);
152     FND_MSG_PUB.add;
153     --dbms_output.put_line('Lot Number is not null');
154   END IF; /* for lot_control_code */
155 END validate_lotnumber;
156 
157 --Procedure to validate Revision
158 PROCEDURE validate_revision(p_inventory_id              IN  NUMBER,
159                             p_organization_id           IN  NUMBER,
160                             p_revision_qty_control_code IN  NUMBER,
161                             p_Revision                  IN  VARCHAR2,
162                             p_concatenated_segments     IN  VARCHAR2) IS
163   CURSOR mtl_item_revisions_csr(c_inventory_id          IN  NUMBER,
164                                 c_organization_id       IN  NUMBER,
165                                 c_revision              IN  VARCHAR2) IS
166     SELECT 'X'
167       FROM mtl_item_revisions
168      WHERE inventory_item_id = c_inventory_id
169        AND organization_id = c_organization_id
170        AND revision = c_revision;
171     l_junk   VARCHAR2(1);
172 BEGIN
173   --Validate Revision.
174   IF (nvl(p_revision_qty_control_code,0) = 2) THEN
175     IF (p_revision IS NULL) OR (p_revision = FND_API.G_MISS_CHAR) THEN
176       FND_MESSAGE.set_name('AHL','AHL_UC_REV_NULL');
177       FND_MESSAGE.set_token('INV_ITEM',p_concatenated_segments);
178       FND_MSG_PUB.add;
179       --dbms_output.put_line('Revision is null');
180     ELSE
181       OPEN mtl_item_revisions_csr(p_inventory_id,p_organization_id, p_revision);
182       FETCH mtl_item_revisions_csr INTO l_junk;
183       IF (mtl_item_revisions_csr%NOTFOUND) THEN
184         FND_MESSAGE.set_name('AHL','AHL_UC_REV_INVALID');
185         FND_MESSAGE.set_token('REV',p_revision);
186         FND_MESSAGE.set_token('INV_ITEM',p_concatenated_segments);
187         FND_MSG_PUB.add;
188         --dbms_output.put_line('Revision does not exist in master');
189       END IF;
190       CLOSE mtl_item_revisions_csr;
191     END IF;
192   ELSIF (p_revision IS NOT NULL) AND (p_revision <> FND_API.G_MISS_CHAR) THEN
193     FND_MESSAGE.set_name('AHL','AHL_UC_REV_NOTNULL');
194     --FND_MESSAGE.set_token('REV',p_revision);
195     FND_MESSAGE.set_token('INV_ITEM',p_concatenated_segments);
196     FND_MSG_PUB.add;
197     --dbms_output.put_line('Revision is not null. Revision not required.');
198   END IF; /* for revision_qty_control_code */
199 END validate_revision;
200 
201 --Procedure to validate Serial Number Tag
202 PROCEDURE validate_serialnum_tag(p_serialnum_tag_code    IN  VARCHAR2,
203                                  p_serial_number_control IN  NUMBER,
204                                  p_concatenated_segments IN  VARCHAR2) IS
205 
206 BEGIN
207   IF (p_serial_number_control IN (2,5,6)) THEN
208     IF (p_serialnum_tag_code IS NULL OR p_serialnum_tag_code = FND_API.G_MISS_CHAR) THEN
209       FND_MESSAGE.set_name('AHL','AHL_UC_SERIALTAG_NULL');
210       FND_MESSAGE.set_token('INV_ITEM',p_concatenated_segments);
211       FND_MSG_PUB.add;
212     ELSE
213       IF NOT(AHL_UTIL_MC_PKG.validate_Lookup_Code('AHL_SERIALNUMBER_TAG',p_serialnum_tag_code)) THEN
214 
215         FND_MESSAGE.set_name('AHL','AHL_UC_SERIALTAG_INVALID');
216         FND_MESSAGE.set_token('TAG',p_serialnum_tag_code);
217         FND_MESSAGE.set_token('INV_ITEM',p_concatenated_segments);
218         FND_MSG_PUB.add;
219         --dbms_output.put_line('Serial Tag code is invalid.');
220       END IF;
221     END IF;
222   ELSE
223     IF (p_serialnum_tag_code IS NOT NULL AND p_serialnum_tag_code <> FND_API.G_MISS_CHAR) THEN
224       FND_MESSAGE.set_name('AHL','AHL_UC_SERIALTAG_NOTNULL');
225       FND_MESSAGE.set_token('TAG',p_serialnum_tag_code);
226       FND_MESSAGE.set_token('INV_ITEM',p_concatenated_segments);
227       FND_MSG_PUB.add;
228       --dbms_output.put_line('Serial Tag code is invalid.');
229     END IF;
230   END IF; /* p_serial_number_control */
231 END validate_serialnum_tag;
232 
233 --Procedure to call the above procedures to validate the attributes
234 PROCEDURE validate_uc_invdetails(p_inventory_id          IN         NUMBER,
235                                  p_organization_id       IN         NUMBER,
236                                  p_Serial_Number         IN         VARCHAR2,
237                                  p_serialnum_tag_code    IN         VARCHAR2,
238                                  p_quantity              IN         NUMBER,
239                                  p_uom_code              IN         VARCHAR2,
240                                  p_revision              IN         VARCHAR2,
241                                  p_lot_number            IN         VARCHAR2,
242                                  p_position_ref_meaning  IN         VARCHAR2,
243                                  x_concatenated_segments OUT NOCOPY VARCHAR2 ) IS
244 
245   CURSOR mtl_system_items_csr(c_inventory_id IN NUMBER, c_organization_id IN NUMBER) IS
246     SELECT serial_number_control_code,
247            lot_control_code,
248            concatenated_segments,
249            revision_qty_control_code,
250            comms_nl_trackable_flag
251       FROM mtl_system_items_kfv
252      WHERE inventory_item_id = c_inventory_id
253        AND organization_id = c_organization_id;
254     l_serial_number_control     NUMBER;
255     l_lot_control_code          NUMBER;
256     -- Changed by jaramana on 16-APR-2008 for bug 6977832
257     -- Changed from 40 to mtl_system_items_kfv.concatenated_segments%TYPE in order
258     -- to accommodate longer item names.
259     l_concatenated_segments     mtl_system_items_kfv.concatenated_segments%TYPE;
260     l_revision_qty_control_code NUMBER;
261     l_comms_nl_trackable_flag   VARCHAR2(1);
262 BEGIN
263   IF (p_inventory_id IS NULL) OR (p_inventory_id = FND_API.G_MISS_NUM)
264       OR (p_organization_id IS NULL) OR (p_organization_id = FND_API.G_MISS_NUM)
265   THEN
266     FND_MESSAGE.set_name('AHL','AHL_UC_INVITEM_NULL');
267     FND_MESSAGE.set_token('POSN_REF',p_position_ref_meaning);
268     FND_MSG_PUB.add;
269     --dbms_output.put_line('Inventory Item is null');
270     RETURN;
271   END IF;
272   -- Check for existence of inventory item .
273   OPEN mtl_system_items_csr (p_inventory_id, p_organization_id);
274   FETCH mtl_system_items_csr INTO l_serial_number_control,
275                                   l_lot_control_code,
276                                   l_concatenated_segments,
277                                   l_revision_qty_control_code,
278                                   l_comms_nl_trackable_flag;
279   IF (mtl_system_items_csr%NOTFOUND) THEN
280     CLOSE mtl_system_items_csr;
281     FND_MESSAGE.set_name('AHL','AHL_UC_INVITEM_INVALID');
282     FND_MESSAGE.set_token('POSN_REF',p_position_ref_meaning);
283     FND_MSG_PUB.add;
284     x_concatenated_segments := null;
285     --dbms_output.put_line('Inventory item does not exist in Master');
286     RETURN;
287   END IF;
288   CLOSE mtl_system_items_csr;
289   IF upper(nvl(l_comms_nl_trackable_flag,'N')) = 'N' THEN
290     FND_MESSAGE.set_name('AHL','AHL_MC_INV_TRACK');
291     FND_MESSAGE.set_token('INV_ITEM',l_concatenated_segments);
292     FND_MSG_PUB.add;
293     --dbms_output.put_line('Inventory item does not exist in Master');
294   END IF;
295   -- Validate quantity .
296   validate_quantity(p_inventory_id,
297                     p_organization_id,
298                     p_quantity,
299                     p_uom_code,
300                     l_concatenated_segments);
301   -- Validate serialnumber.
302   validate_serialnumber(p_inventory_id,
303                         p_Serial_Number,
304                         l_serial_number_control,
305                         p_serialnum_tag_code,
306                         p_quantity,
307                         l_concatenated_segments);
308   -- Validate serialnum_tag_code.
309   validate_serialnum_tag(p_serialnum_tag_code,
310                          l_serial_number_control,
311                          l_concatenated_segments);
312   -- Validate lot.
313   validate_lotnumber(p_inventory_id,
314                      p_organization_id,
315                      l_lot_control_code,
316                      p_lot_number,
317                      l_concatenated_segments);
318   -- Validate Revision.
319   validate_revision(p_inventory_id,
320                     p_organization_id,
321                     l_revision_qty_control_code,
322                     p_revision,
323                     l_concatenated_segments);
324   x_concatenated_segments := l_concatenated_segments;
325 END validate_uc_invdetails;
326 
327 --Function to get the operating unit of a given instance_id
328 FUNCTION get_operating_unit(p_instance_id NUMBER) RETURN NUMBER IS
329   l_operating_unit NUMBER;
330   CURSOR get_instance_ou IS
331 
332     -- SATHAPLI::Bug#4912576 fix::SQL ID 14401150 --
333     /*
334     SELECT o.operating_unit
335       FROM org_organization_definitions o,
336            mtl_system_items_kfv m,
337            csi_item_instances c
338      WHERE c.instance_id = p_instance_id
339        AND c.inventory_item_id = m.inventory_item_id
340        AND c.inv_master_organization_id = m.organization_id
341        AND m.organization_id = o.organization_id;
342     */
343     SELECT i.operating_unit
344     FROM   inv_organization_info_v i, mtl_system_items_kfv m,
345            csi_item_instances c
346     WHERE  c.instance_id = p_instance_id AND
347            c.inventory_item_id = m.inventory_item_id AND
348            c.inv_master_organization_id = m.organization_id AND
349            m.organization_id = i.organization_id;
350 
351 BEGIN
352   OPEN get_instance_ou;
353   FETCH get_instance_ou INTO l_operating_unit;
354   CLOSE get_instance_ou;
355   return l_operating_unit;
356 END;
357 
358 -- Define Procedure unassociate_instance_pos --
359 -- This API is used to to remove a child instance's position reference but keep
360 -- the parent-child relationship in a UC tree structure (in other word, to make
361 -- the child instance as an extra node in the UC).
362 PROCEDURE unassociate_instance_pos (
363   p_api_version           IN  NUMBER := 1.0,
364   p_init_msg_list         IN  VARCHAR2 := FND_API.G_FALSE,
365   p_commit                IN  VARCHAR2 := FND_API.G_FALSE,
366   p_validation_level      IN  NUMBER := FND_API.G_VALID_LEVEL_FULL,
367   x_return_status         OUT NOCOPY VARCHAR2,
368   x_msg_count             OUT NOCOPY NUMBER,
369   x_msg_data              OUT NOCOPY VARCHAR2,
370   p_uc_header_id          IN  NUMBER,
371   p_instance_id           IN  NUMBER,
372   p_csi_ii_ovn            IN  NUMBER,
373   p_prod_user_flag        IN  VARCHAR2)
374 IS
375   l_api_name       CONSTANT   VARCHAR2(30) := 'unassociate_instance_pos';
376   l_api_version    CONSTANT   NUMBER       := 1.0;
377   l_return_status             VARCHAR2(1);
378   l_msg_count                 NUMBER;
379   l_msg_data                  VARCHAR2(2000);
380   l_csi_relationship_rec      csi_datastructures_pub.ii_relationship_rec;
381   l_csi_relationship_tbl      csi_datastructures_pub.ii_relationship_tbl;
382   l_csi_transaction_rec       csi_datastructures_pub.transaction_rec;
383   l_subject_id                NUMBER;
384   l_object_id                 NUMBER;
385   l_csi_relationship_id       NUMBER;
386   l_object_version_number     NUMBER;
387   l_transaction_type_id       NUMBER;
388   l_return_value              BOOLEAN;
389   l_root_uc_header_id         NUMBER;
390   l_root_instance_id          NUMBER;
391   l_root_uc_status_code       FND_LOOKUP_VALUES.lookup_code%TYPE;
392   l_root_active_uc_status_code FND_LOOKUP_VALUES.lookup_code%TYPE;
393   l_root_uc_ovn               NUMBER;
394   l_end_date                  DATE;
395   CURSOR check_uc_header IS
396     SELECT unit_config_header_id,
397            object_version_number,
398            unit_config_status_code,
399            active_uc_status_code,
400            csi_item_instance_id
401       FROM ahl_unit_config_headers
402      WHERE unit_config_header_id = p_uc_header_id
403        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
404   l_check_uc_header check_uc_header%ROWTYPE;
405   CURSOR get_uc_descendants(c_instance_id NUMBER) IS
406     SELECT relationship_id,
407            object_version_number,
408            object_id,
409            subject_id
410       FROM csi_ii_relationships
411 START WITH object_id = c_instance_id
412        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
413        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
414 CONNECT BY object_id = PRIOR subject_id
415        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
416        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
417 
418 BEGIN
419   --Initialize API return status to success
420   x_return_status := FND_API.G_RET_STS_SUCCESS;
421 
422   -- Standard Start of API savepoint
423   SAVEPOINT unassociate_instance_pos;
424 
425   --Standard call to check for call compatibility.
426   IF NOT FND_API.compatible_api_call(
427     l_api_version,
428     p_api_version,
429     l_api_name,
430     G_PKG_NAME)
431   THEN
432     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
433   END IF;
434 
435   --Initialize message list if p_init_msg_list is set to TRUE.
436   IF FND_API.to_boolean( p_init_msg_list ) THEN
437     FND_MSG_PUB.initialize;
438   END IF;
439 
440   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
441     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
442                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Begin API',
443                    'At the start of the procedure');
444   END IF;
445 
446   --Validate input parameters p_prod_user_flag
447   IF (upper(p_prod_user_flag) <> 'Y' AND upper(p_prod_user_flag) <> 'N') THEN
448     FND_MESSAGE.set_name('AHL','AHL_UC_API_PARAMETER_INVALID');
449     FND_MESSAGE.set_token('NAME', 'prod_user_flag');
450     FND_MESSAGE.set_token('VALUE', p_prod_user_flag);
451     FND_MSG_PUB.add;
452     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
453   END IF;
454   --Validate input parameters p_csi_ii_ovn
455   IF (p_csi_ii_ovn IS NULL OR p_csi_ii_ovn <= 0 ) THEN
456     FND_MESSAGE.set_name('AHL','AHL_UC_API_PARAMETER_INVALID');
457     FND_MESSAGE.set_token('NAME', 'csi_ii_ovn');
458     FND_MESSAGE.set_token('VALUE', p_csi_ii_ovn);
459     FND_MSG_PUB.add;
460     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
461   END IF;
462   --Validate input parameter p_uc_header_id, its two status
463   OPEN check_uc_header;
464   FETCH check_uc_header INTO l_check_uc_header;
465   IF check_uc_header%NOTFOUND THEN
466     FND_MESSAGE.set_name( 'AHL','AHL_UC_API_PARAMETER_INVALID');
467     FND_MESSAGE.set_token('NAME', 'uc_header_id');
468     FND_MESSAGE.set_token('VALUE', p_uc_header_id);
469     FND_MSG_PUB.add;
470     CLOSE check_uc_header;
471     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
472   ELSE
473 
474     -- ACL :: Changes for R12
475     IF (ahl_util_uc_pkg.IS_UNIT_QUARANTINED(p_unit_header_id => p_uc_header_id , p_instance_id => null) = FND_API.G_TRUE) THEN
476       FND_MESSAGE.set_name( 'AHL','AHL_UC_INVALID_Q_ACTION' );
477       FND_MSG_PUB.add;
478       CLOSE check_uc_header;
479       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
480     END IF;
481 
482     ahl_util_uc_pkg.get_root_uc_attr(p_uc_header_id,
483                                      l_root_uc_header_id,
484                                      l_root_instance_id,
485                                      l_root_uc_status_code,
486                                      l_root_active_uc_status_code,
487                                      l_root_uc_ovn);
488     IF (p_prod_user_flag = 'Y' AND --For production user, no need to confirm either one of the statuses is not APPROVAL_PENDING
489         l_root_uc_status_code NOT IN ('COMPLETE', 'INCOMPLETE')) THEN
490       FND_MESSAGE.set_name( 'AHL','AHL_UC_STATUS_NOT_ACTIVE' );
491       FND_MSG_PUB.add;
492       CLOSE check_uc_header;
493       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
494     ELSIF (p_prod_user_flag = 'N' AND
495            (l_root_uc_status_code = 'APPROVAL_PENDING' OR
496             l_root_active_uc_status_code = 'APPROVAL_PENDING')) THEN
497       FND_MESSAGE.set_name( 'AHL','AHL_UC_STATUS_PENDING' );
498       FND_MESSAGE.set_token('UC_HEADER_ID', l_root_uc_header_id);
499       FND_MSG_PUB.add;
500       CLOSE check_uc_header;
501       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
502     ELSE
503       CLOSE check_uc_header;
504     END IF;
505   END IF;
506   --Make sure p_instance_id is valid and installed in the UC
507   FOR l_get_uc_descendant IN get_uc_descendants(l_check_uc_header.csi_item_instance_id) LOOP
508     l_csi_relationship_id := l_get_uc_descendant.relationship_id;
509     l_object_version_number := l_get_uc_descendant.object_version_number;
510     l_object_id := l_get_uc_descendant.object_id;
511     l_subject_id := l_get_uc_descendant.subject_id;
512     EXIT WHEN l_subject_id = p_instance_id;
513   END LOOP;
514   IF (p_instance_id IS NULL OR l_subject_id IS NULL OR l_subject_id <> p_instance_id)THEN
515     FND_MESSAGE.set_name( 'AHL','AHL_UC_API_PARAMETER_INVALID');
516     FND_MESSAGE.set_token('NAME', 'instance_id');
517     FND_MESSAGE.set_token('VALUE', p_instance_id);
518     FND_MSG_PUB.add;
519     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
520   --Ensure no current user makes change to the same csi_ii_relationships record
521   ELSIF l_object_version_number <> p_csi_ii_ovn THEN
522     FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
523     FND_MSG_PUB.add;
524     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
525   END IF;
526 
527   --Make sure p_instance_id is not expired otherwise unassociation is not allowed
528   --Added on 02/26/2004
529   OPEN get_instance_date(p_instance_id);
530   FETCH get_instance_date INTO l_end_date;
531   CLOSE get_instance_date;
532   IF TRUNC(NVL(l_end_date, SYSDATE+1)) <= TRUNC(SYSDATE) THEN
533     FND_MESSAGE.set_name( 'AHL','AHL_UC_INSTANCE_EXPIRED');
534     FND_MESSAGE.set_token('INSTANCE', p_instance_id);
535     FND_MSG_PUB.add;
536     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
537   END IF;
538 
539   --The following lines are used to update the position_reference column in csi_ii_relationships
540   --First, get transaction_type_id .
541   AHL_UTIL_UC_PKG.getcsi_transaction_id('UC_UPDATE',l_transaction_type_id, l_return_value);
542   IF NOT l_return_value THEN
543     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
544   END IF;
545   --Set the CSI transaction record
546   l_csi_transaction_rec.source_transaction_date := SYSDATE;
547   l_csi_transaction_rec.transaction_type_id := l_transaction_type_id;
548   --Set CSI relationship record
549   l_csi_relationship_rec.relationship_id := l_csi_relationship_id;
550   l_csi_relationship_rec.object_version_number := l_object_version_number;
551   l_csi_relationship_rec.position_reference := NULL;
552   l_csi_relationship_rec.relationship_type_code := 'COMPONENT-OF';
553   l_csi_relationship_rec.object_id := l_object_id;
554   l_csi_relationship_rec.subject_id := l_subject_id;
555   l_csi_relationship_tbl(1) := l_csi_relationship_rec;
556   CSI_II_RELATIONSHIPS_PUB.update_relationship(
557                            p_api_version      => 1.0,
558                            p_relationship_tbl => l_csi_relationship_tbl,
559                            p_txn_rec          => l_csi_transaction_rec,
560                            x_return_status    => l_return_status,
561                            x_msg_count        => l_msg_count,
562                            x_msg_data         => l_msg_data);
563   IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
564     RAISE FND_API.G_EXC_ERROR;
565   ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
566     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
567   END IF;
568 
569   --For UC user, UC header status change needs to be made after the operation
570   --Not confirmed whether need to copy the record into UC header history table
571   --after status change. Not include the copy right now. (Confirmed and not necessary here)
572   IF p_prod_user_flag = 'N' THEN
573     IF l_root_uc_status_code = 'COMPLETE' THEN
574       UPDATE ahl_unit_config_headers
575          SET unit_config_status_code = 'INCOMPLETE',
576              active_uc_status_code = 'UNAPPROVED',
577              object_version_number = object_version_number + 1,
578              last_update_date = SYSDATE,
579              last_updated_by = FND_GLOBAL.user_id,
580              last_update_login = FND_GLOBAL.login_id
581        WHERE unit_config_header_id = l_root_uc_header_id
582          AND object_version_number = l_root_uc_ovn;
583       IF SQL%ROWCOUNT = 0 THEN
584         FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
585         FND_MSG_PUB.add;
586         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
587       END IF;
588     ELSIF (l_root_uc_status_code = 'INCOMPLETE' AND
589            (l_root_active_uc_status_code IS NULL OR
590             l_root_active_uc_status_code <> 'UNAPPROVED')) THEN
591       UPDATE ahl_unit_config_headers
592          SET active_uc_status_code = 'UNAPPROVED',
593              object_version_number = object_version_number + 1,
594              last_update_date = SYSDATE,
595              last_updated_by = FND_GLOBAL.user_id,
596              last_update_login = FND_GLOBAL.login_id
597        WHERE unit_config_header_id = l_root_uc_header_id
598          AND object_version_number = l_root_uc_ovn;
599       IF SQL%ROWCOUNT = 0 THEN
600         FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
601         FND_MSG_PUB.add;
602         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
603       END IF;
604     ELSIF l_root_uc_status_code NOT IN ('COMPLETE', 'INCOMPLETE', 'DRAFT') THEN
605       UPDATE ahl_unit_config_headers
606          SET unit_config_status_code = 'DRAFT',
607              object_version_number = object_version_number + 1,
608              last_update_date = SYSDATE,
609              last_updated_by = FND_GLOBAL.user_id,
610              last_update_login = FND_GLOBAL.login_id
611        WHERE unit_config_header_id = l_root_uc_header_id
612          AND object_version_number = l_root_uc_ovn;
613       IF SQL%ROWCOUNT = 0 THEN
614         FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
615         FND_MSG_PUB.add;
616         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
617       END IF;
618     END IF;
619   ELSIF p_prod_user_flag = 'Y' THEN
620     IF l_root_uc_status_code = 'COMPLETE' THEN
621       UPDATE ahl_unit_config_headers
622          SET unit_config_status_code = 'INCOMPLETE',
623              object_version_number = object_version_number + 1,
624              last_update_date = SYSDATE,
625              last_updated_by = FND_GLOBAL.user_id,
626              last_update_login = FND_GLOBAL.login_id
627        WHERE unit_config_header_id = l_root_uc_header_id
628          AND object_version_number = l_root_uc_ovn;
629       IF SQL%ROWCOUNT = 0 THEN
630         FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
631         FND_MSG_PUB.add;
632         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
633       END IF;
634     END IF;
635   END IF; --For production user, no need to change any one of the status.
636 
637   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
638     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
639                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': After normal execution',
640                    'At the end of the procedure');
641   END IF;
642   --Get all the error messages from the previous steps (if any) and raise the appropriate Exception
643 
644   l_msg_count := FND_MSG_PUB.count_msg;
645   IF l_msg_count > 0 THEN
646     x_msg_count := l_msg_count;
647     RAISE FND_API.G_EXC_ERROR;
648   END IF;
649   -- Perform the Commit (if requested)
650   IF FND_API.to_boolean(p_commit) THEN
651     COMMIT;
652   END IF;
653   --Count and Get messages(optional)
654   FND_MSG_PUB.count_and_get(
655     p_encoded  => FND_API.G_FALSE,
656     p_count    => x_msg_count,
657     p_data     => x_msg_data);
658 
659 EXCEPTION
660   WHEN FND_API.G_EXC_ERROR THEN
661     ROLLBACK TO unassociate_instance_pos;
662     x_return_status := FND_API.G_RET_STS_ERROR ;
663     FND_MSG_PUB.count_and_get(
664       p_encoded  => FND_API.G_FALSE,
665       p_count    => x_msg_count,
666       p_data     => x_msg_data);
667   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
668     ROLLBACK TO unassociate_instance_pos;
669     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
670     FND_MSG_PUB.count_and_get(
671       p_encoded  => FND_API.G_FALSE,
672       p_count    => x_msg_count,
673       p_data     => x_msg_data);
674   WHEN OTHERS THEN
675     ROLLBACK TO unassociate_instance_pos;
676     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
677     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
678     THEN
679       FND_MSG_PUB.add_exc_msg(
680         p_pkg_name         => G_PKG_NAME,
681         p_procedure_name   => l_api_name,
682         p_error_text       => SUBSTRB(SQLERRM,1,240));
683     END IF;
684     FND_MSG_PUB.count_and_get(
685       p_encoded  => FND_API.G_FALSE,
686       p_count    => x_msg_count,
687       p_data     => x_msg_data);
688 END unassociate_instance_pos;
689 
690 -- Define Procedure remove_instance
691 -- This API is used to remove an instance (leaf, branch node or sub-unit) from aUC node.
692 
693 PROCEDURE remove_instance (
694   p_api_version           IN  NUMBER := 1.0,
695   p_init_msg_list         IN  VARCHAR2 := FND_API.G_FALSE,
696   p_commit                IN  VARCHAR2 := FND_API.G_FALSE,
697   p_validation_level      IN  NUMBER := FND_API.G_VALID_LEVEL_FULL,
698   x_return_status         OUT NOCOPY VARCHAR2,
699   x_msg_count             OUT NOCOPY NUMBER,
700   x_msg_data              OUT NOCOPY VARCHAR2,
701   p_uc_header_id          IN  NUMBER,
702   p_instance_id           IN  NUMBER,
703   p_csi_ii_ovn            IN  NUMBER,
704   p_prod_user_flag        IN  VARCHAR2)
705 IS
706   l_api_name       CONSTANT   VARCHAR2(30) := 'remove_instance';
707   l_api_version    CONSTANT   NUMBER       := 1.0;
708   l_return_status             VARCHAR2(1);
709   l_msg_count                 NUMBER;
710   l_msg_data                  VARCHAR2(2000);
711   l_csi_relationship_rec      csi_datastructures_pub.ii_relationship_rec;
712   l_csi_transaction_rec       csi_datastructures_pub.transaction_rec;
713   l_csi_upd_transaction_rec   csi_datastructures_pub.transaction_rec;
714   l_csi_instance_rec          csi_datastructures_pub.instance_rec;
715   l_csi_instance_id_lst       csi_datastructures_pub.id_tbl;
716   l_subject_id                NUMBER;
717   l_object_id                 NUMBER;
718   l_csi_relationship_id       NUMBER;
719   l_object_version_number     NUMBER;
720   l_position_reference        csi_ii_relationships.position_reference%TYPE;
721   l_position_necessity        FND_LOOKUP_VALUES.lookup_code%TYPE;
722   l_transaction_type_id       NUMBER;
723   l_return_value              BOOLEAN;
724   l_dummy_num                 NUMBER;
725   l_sub_uc_header_id          NUMBER;
726   l_root_uc_header_id         NUMBER;
727   l_root_instance_id          NUMBER;
728   l_root_uc_status_code       FND_LOOKUP_VALUES.lookup_code%TYPE;
729   l_root_active_uc_status_code FND_LOOKUP_VALUES.lookup_code%TYPE;
730   l_root_uc_ovn               NUMBER;
731   l_uc_status_code            FND_LOOKUP_VALUES.lookup_code%TYPE;
732   l_active_uc_status_code FND_LOOKUP_VALUES.lookup_code%TYPE;
733   CURSOR check_uc_header IS
734     SELECT unit_config_header_id,
735            object_version_number,
736            unit_config_status_code,
737            active_uc_status_code,
738            csi_item_instance_id
739       FROM ahl_unit_config_headers
740      WHERE unit_config_header_id = p_uc_header_id
741        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
742   l_check_uc_header check_uc_header%ROWTYPE;
743 
744   CURSOR get_uc_descendants(c_instance_id NUMBER) IS
745     SELECT relationship_id,
746            object_version_number,
747            object_id,
748            subject_id,
749            to_number(position_reference) position_reference
750       FROM csi_ii_relationships
751 START WITH object_id = c_instance_id
752        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
753        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
754 CONNECT BY object_id = PRIOR subject_id
755        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
756        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
757   CURSOR get_csi_obj_ver_num(c_instance_id NUMBER) IS
758     SELECT object_version_number
759       FROM csi_item_instances
760      WHERE instance_id = c_instance_id
761        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
762        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
763 
764   CURSOR check_instance_non_leaf(c_instance_id NUMBER) IS
765     SELECT subject_id
766       FROM csi_ii_relationships
767      WHERE object_id = c_instance_id
768        AND relationship_type_code = 'COMPONENT-OF'
769        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
770        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
771 
772   CURSOR check_instance_subunit(c_instance_id NUMBER) IS
773     SELECT unit_config_header_id
774       FROM ahl_unit_config_headers
775      WHERE csi_item_instance_id = c_instance_id
776        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
777 
778   CURSOR get_position_necessity(c_relationship_id NUMBER) IS
779     SELECT position_necessity_code
780       FROM ahl_mc_relationships
781      WHERE relationship_id = c_relationship_id
782        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
783        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
784   --To get all the first level sub-units for a given branch node. First get all of the
785   --branch node's sub-units and then remove those sub-units which are not first level
786   --(from the branch node's perspective)
787   CURSOR get_1st_level_subunits(c_instance_id NUMBER) IS
788   /*This query is replaced by the one below it for performance gain
789     SELECT subject_id
790       FROM csi_ii_relationships
791      WHERE subject_id IN (SELECT csi_item_instance_id
792                             FROM ahl_unit_config_headers
793                            WHERE trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE))
794 START WITH object_id = c_instance_id
795        AND relationship_type_code = 'COMPONENT-OF'
796        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
797        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
798 CONNECT BY object_id = PRIOR subject_id
799        AND relationship_type_code = 'COMPONENT-OF'
800        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
801        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
802      MINUS
803     SELECT subject_id
804       FROM csi_ii_relationships
805      WHERE subject_id IN (SELECT csi_item_instance_id
806                             FROM ahl_unit_config_headers
807                            WHERE trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE))
808 START WITH object_id IN (SELECT subject_id
809                            FROM csi_ii_relationships
810                           WHERE subject_id IN (SELECT csi_item_instance_id
811                                                  FROM ahl_unit_config_headers
812                                                 WHERE trunc(nvl(active_end_date,SYSDATE+1)) > trunc(SYSDATE))
813                      START WITH object_id = c_instance_id
814                             AND relationship_type_code = 'COMPONENT-OF'
815                             AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
816                             AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
817                      CONNECT BY object_id = PRIOR subject_id
818                             AND relationship_type_code = 'COMPONENT-OF'
819                             AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
820                             AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE))
821        AND relationship_type_code = 'COMPONENT-OF'
822        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
823        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
824 CONNECT BY object_id = PRIOR subject_id
825        AND relationship_type_code = 'COMPONENT-OF'
826        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
827        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
828   */
829   SELECT i.subject_id
830     FROM csi_ii_relationships i
831    WHERE EXISTS (SELECT 'x'
832                   FROM ahl_unit_config_headers u
833                  WHERE u.csi_item_instance_id = i.subject_id
834                    AND trunc(nvl(u.active_end_date, SYSDATE+1)) > trunc(SYSDATE))
835      AND NOT EXISTS (SELECT ci.object_id
836                        FROM csi_ii_relationships ci
837                       WHERE (EXISTS (SELECT 'x'
838                                        FROM ahl_unit_config_headers ui
839                                       WHERE ui.csi_item_instance_id = ci.object_id)
840                                 AND ci.object_id <> c_instance_id)
841                  START WITH ci.subject_id = i.subject_id
842                         AND ci.relationship_type_code = 'COMPONENT-OF'
843                         AND trunc(nvl(ci.active_start_date, SYSDATE)) <= trunc(SYSDATE)
844                         AND trunc(nvl(ci.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
845                  CONNECT BY ci.subject_id = prior ci.object_id
846                         AND ci.relationship_type_code = 'COMPONENT-OF'
847                         AND trunc(nvl(ci.active_start_date, SYSDATE)) <= trunc(SYSDATE)
848                         AND trunc(nvl(ci.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
849                         AND ci.subject_id <> c_instance_id)
850 START WITH i.object_id = c_instance_id
851        AND i.relationship_type_code = 'COMPONENT-OF'
852        AND trunc(nvl(i.active_start_date, SYSDATE)) <= trunc(SYSDATE)
853        AND trunc(nvl(i.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
854 CONNECT BY i.object_id = PRIOR i.subject_id
855        AND i.relationship_type_code = 'COMPONENT-OF'
856        AND trunc(nvl(i.active_start_date, SYSDATE)) <= trunc(SYSDATE)
857        AND trunc(nvl(i.active_end_date, SYSDATE+1)) > trunc(SYSDATE);
858 
859   CURSOR get_uc_header_id(c_instance_id NUMBER) IS
860     SELECT unit_config_header_id
861       FROM ahl_unit_config_headers
862      WHERE csi_item_instance_id = c_instance_id
863        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
864 
865 BEGIN
866   --Initialize API return status to success
867   x_return_status := FND_API.G_RET_STS_SUCCESS;
868 
869   -- Standard Start of API savepoint
870   SAVEPOINT remove_instance;
871 
872   --Standard call to check for call compatibility.
873   IF NOT FND_API.compatible_api_call(
874     l_api_version,
875     p_api_version,
876     l_api_name,
877     G_PKG_NAME)
878   THEN
879     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
880   END IF;
881 
882   --Initialize message list if p_init_msg_list is set to TRUE.
883   IF FND_API.to_boolean( p_init_msg_list ) THEN
884     FND_MSG_PUB.initialize;
885   END IF;
886 
887   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
888     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
889                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Begin API',
890                    'At the start of the procedure');
891   END IF;
892 
893   --Validate input parameters p_prod_user_flag
894   IF upper(p_prod_user_flag) <> 'Y' AND upper(p_prod_user_flag) <> 'N' THEN
895     FND_MESSAGE.set_name('AHL','AHL_UC_API_PARAMETER_INVALID');
896     FND_MESSAGE.set_token('NAME', 'prod_user_flag');
897     FND_MESSAGE.set_token('VALUE', p_prod_user_flag);
898     FND_MSG_PUB.add;
899     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
900   END IF;
901 
902   --Validate input parameters p_csi_ii_ovn
903   IF (p_csi_ii_ovn IS NULL OR p_csi_ii_ovn <= 0 ) THEN
904     FND_MESSAGE.set_name('AHL','AHL_UC_API_PARAMETER_INVALID');
905     FND_MESSAGE.set_token('NAME', 'csi_ii_ovn');
906     FND_MESSAGE.set_token('VALUE', p_csi_ii_ovn);
907     FND_MSG_PUB.add;
908     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
909   END IF;
910   --Validate input parameter p_uc_header_id, its two statuses
911   OPEN check_uc_header;
912   FETCH check_uc_header INTO l_check_uc_header;
913   IF check_uc_header%NOTFOUND THEN
914     FND_MESSAGE.set_name('AHL','AHL_UC_API_PARAMETER_INVALID');
915     FND_MESSAGE.set_token('NAME', 'uc_header_id');
916     FND_MESSAGE.set_token('NAME', p_uc_header_id);
917     FND_MSG_PUB.add;
918     CLOSE check_uc_header;
919     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
920   ELSE
921 
922     -- ACL :: Changes for R12
923     IF (ahl_util_uc_pkg.IS_UNIT_QUARANTINED(p_unit_header_id => p_uc_header_id , p_instance_id => null) = FND_API.G_TRUE) THEN
924       FND_MESSAGE.set_name( 'AHL','AHL_UC_INVALID_Q_ACTION' );
925       FND_MSG_PUB.add;
926       CLOSE check_uc_header;
927       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
928     END IF;
929 
930     ahl_util_uc_pkg.get_root_uc_attr(p_uc_header_id,
931                                      l_root_uc_header_id,
932                                      l_root_instance_id,
933                                      l_root_uc_status_code,
934                                      l_root_active_uc_status_code,
935                                      l_root_uc_ovn);
936     IF (p_prod_user_flag = 'Y' AND --For production user, no need to confirm either one of the statuses is not APPROVAL_PENDING
937         l_root_uc_status_code NOT IN ('COMPLETE', 'INCOMPLETE')) THEN
938       FND_MESSAGE.set_name( 'AHL','AHL_UC_STATUS_NOT_ACTIVE' );
939       FND_MSG_PUB.add;
940       CLOSE check_uc_header;
941       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
942     ELSIF (p_prod_user_flag = 'N' AND
943            (l_root_uc_status_code = 'APPROVAL_PENDING' OR
944             l_root_active_uc_status_code = 'APPROVAL_PENDING')) THEN
945       FND_MESSAGE.set_name( 'AHL','AHL_UC_STATUS_PENDING' );
946       FND_MESSAGE.set_token('UC_HEADER_ID', l_root_uc_header_id);
947       FND_MSG_PUB.add;
948       CLOSE check_uc_header;
949       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
950     ELSE
951       CLOSE check_uc_header;
952     END IF;
953   END IF;
954   --Make sure p_instance_id is in the UC
955   FOR l_get_uc_descendant IN get_uc_descendants(l_check_uc_header.csi_item_instance_id) LOOP
956     l_csi_relationship_id := l_get_uc_descendant.relationship_id;
957     l_object_version_number := l_get_uc_descendant.object_version_number;
958     l_object_id := l_get_uc_descendant.object_id;
959     l_subject_id := l_get_uc_descendant.subject_id;
960     l_position_reference := l_get_uc_descendant.position_reference;
961     EXIT WHEN l_subject_id = p_instance_id;
962   END LOOP;
963   --Ensure the instance is installed in this UC but it could be an extra node(l_position_referece=null)
964 
965   IF (p_instance_id IS NULL OR l_subject_id IS NULL OR l_subject_id <> p_instance_id) THEN
966     FND_MESSAGE.set_name( 'AHL','AHL_UC_API_PARAMETER_INVALID' );
967     FND_MESSAGE.set_token('NAME', 'instance_id');
968     FND_MESSAGE.set_token('VALUE', p_instance_id);
969     FND_MSG_PUB.add;
970     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
971   --Ensure no current user makes change to the same csi_ii_relationships record
972   ELSIF l_object_version_number <> p_csi_ii_ovn THEN
973     FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
974     FND_MSG_PUB.add;
975     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
976   END IF;
977 
978   --The following lines are used to update the position_reference column in csi_ii_relationships
979   --First, get transaction_type_id .
980   AHL_UTIL_UC_PKG.getcsi_transaction_id('UC_UPDATE',l_transaction_type_id, l_return_value);
981   IF NOT l_return_value THEN
982     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
983   END IF;
984   --Set the CSI transaction record
985   l_csi_transaction_rec.source_transaction_date := SYSDATE;
986   l_csi_transaction_rec.transaction_type_id := l_transaction_type_id;
987   l_csi_upd_transaction_rec := l_csi_transaction_rec;
988 
989   --Set CSI relationship record
990   l_csi_relationship_rec.relationship_id := l_csi_relationship_id;
991   l_csi_relationship_rec.object_version_number := l_object_version_number;
992 
993   CSI_II_RELATIONSHIPS_PUB.expire_relationship(
994                            p_api_version      => 1.0,
995                            p_relationship_rec => l_csi_relationship_rec,
996                            p_txn_rec          => l_csi_transaction_rec,
997                            x_instance_id_lst  => l_csi_instance_id_lst,
998                            x_return_status    => l_return_status,
999                            x_msg_count        => l_msg_count,
1000                            x_msg_data         => l_msg_data);
1001 
1002   IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
1003     RAISE FND_API.G_EXC_ERROR;
1004   ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
1005     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1006   END IF;
1007 
1008   --Expire the instance and its descendant instances if it is a branch node or sub-unit when the
1009   --UC is not in active status. Even the sub-unit's descendants get also expired. The sub-unit
1010   --itself is inactive because inactive units can only contain inactive sub-units.
1011 
1012   /* According to the new requirement, this is no longer necessary
1013   IF (l_check_uc_header.unit_config_status_code NOT IN ('COMPLETE', 'INCOMPLETE'
1014 )) THEN
1015      --get the object_version_number of the instance
1016      OPEN get_csi_obj_ver_num(l_subject_id);
1017      FETCH get_csi_obj_ver_num INTO l_dummy;
1018      IF (get_csi_obj_ver_num%NOTFOUND) THEN
1019         CLOSE get_csi_obj_ver_num;
1020         FND_MESSAGE.set_name('AHL','AHL_COM_RECORD_DELETED');
1021         FND_MSG_PUB.add;
1022         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1023      ELSE
1024        CLOSE get_csi_obj_ver_num;
1025      END IF;
1026      --Call CSI API to expire the instance and all its descendants if exist
1027      l_csi_instance_rec.instance_id := l_subject_id;
1028      l_csi_instance_rec.object_version_number := l_dummy;
1029 
1030      CSI_ITEM_INSTANCE_PUB.expire_item_instance(
1031                            p_api_version         => 1.0,
1032                            p_instance_rec        => l_csi_instance_rec,
1033                            p_expire_children     => FND_API.G_TRUE,
1034                            p_txn_rec             => l_csi_upd_transaction_rec,
1035                            x_instance_id_lst     => l_csi_instance_id_lst,
1036                            x_return_status       => l_return_status,
1037                            x_msg_count           => l_msg_count,
1038                            x_msg_data            => l_msg_data);
1039     IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
1040       RAISE FND_API.G_EXC_ERROR;
1041     ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
1042       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1043     END IF;
1044   END IF; --unit_config_status_code check
1045   */
1046 
1047   --Derive unit_config_status_code based on its root UC
1048   IF l_root_uc_status_code IN ('DRAFT', 'APPROVAL_REJECTED') THEN
1049     l_uc_status_code := 'DRAFT';
1050   ELSIF l_root_uc_status_code = 'COMPLETE' THEN
1051     l_uc_status_code := 'COMPLETE';
1052   ELSIF l_root_uc_status_code = 'INCOMPLETE' THEN
1053     l_uc_status_code := 'INCOMPLETE';
1054   ELSE --'APPROVAL_PENDING' might be possible for production user
1055     l_uc_status_code := 'DRAFT'; --not sure whether 'DRAFT' is appropriate here?
1056   END IF;
1057   --Derive active_uc_status_code based on its original parent unit
1058   IF l_root_active_uc_status_code = 'UNAPPROVED' THEN
1059     l_active_uc_status_code := 'UNAPPROVED';
1060   ELSIF l_root_active_uc_status_code = 'APPROVED' THEN
1061     l_active_uc_status_code := 'APPROVED';
1062   ELSE --'APPROVAL_PENDING' might be possible for production user
1063     l_active_uc_status_code := 'UNAPPROVED'; --not sure whether 'UNAPPROVED' is appropriate here?
1064   END IF;
1065 
1066   --If the node is the top node of a sub-unit then just itself, otherwise if it is a
1067   --branch node, then get all of its first level sub-units. For all of these sub-units,
1068   --we have to remove their parent_uc_header_id and derive their own two statuses.
1069   --Here we asume removing the top node instance within its own UC context is not allowed
1070   OPEN check_instance_subunit(p_instance_id);
1071   FETCH check_instance_subunit INTO l_sub_uc_header_id;
1072   IF check_instance_subunit%FOUND THEN --this instance is a sub-unit top node
1073     UPDATE ahl_unit_config_headers
1074        SET parent_uc_header_id = NULL,
1075            unit_config_status_code = l_uc_status_code,
1076            active_uc_status_code = l_active_uc_status_code,
1077            object_version_number = object_version_number + 1,
1078            last_update_date = SYSDATE,
1079            last_updated_by = FND_GLOBAL.user_id,
1080            last_update_login = FND_GLOBAL.login_id
1081      WHERE unit_config_header_id = l_sub_uc_header_id;
1082        --Not necessary to check the object_version_number here
1083 
1084     --Copy the change to UC history table
1085     ahl_util_uc_pkg.copy_uc_header_to_history(l_sub_uc_header_id, l_return_status);
1086     --IF history copy failed, then don't raise exception, just add the messageto the message stack
1087     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1088       FND_MESSAGE.set_name('AHL', 'AHL_UC_HISTORY_COPY_FAILED');
1089       FND_MSG_PUB.add;
1090     END IF;
1091   ELSE --Non subunit top node
1092     OPEN check_instance_non_leaf(p_instance_id);
1093     FETCH check_instance_non_leaf INTO l_dummy_num;
1094     IF check_instance_non_leaf%FOUND THEN
1095     --This instance is a branch node
1096       FOR l_get_1st_level_subunit IN get_1st_level_subunits(p_instance_id) LOOP
1097         OPEN get_uc_header_id(l_get_1st_level_subunit.subject_id);
1098         FETCH get_uc_header_id INTO l_sub_uc_header_id;
1099         IF get_uc_header_id%NOTFOUND THEN
1100           FND_MESSAGE.set_name('AHL', 'AHL_UC_INSTANCE_INVALID');
1101           FND_MESSAGE.set_token('INSTANCE', l_get_1st_level_subunit.subject_id);
1102           FND_MSG_PUB.add;
1103         END IF;
1104         CLOSE get_uc_header_id;
1105 
1106         UPDATE ahl_unit_config_headers
1107            SET parent_uc_header_id = NULL,
1108                unit_config_status_code = l_uc_status_code,
1109                active_uc_status_code = l_active_uc_status_code,
1110                object_version_number = object_version_number + 1,
1111                last_update_date = SYSDATE,
1112                last_updated_by = FND_GLOBAL.user_id,
1113                last_update_login = FND_GLOBAL.login_id
1114          WHERE unit_config_header_id = l_sub_uc_header_id;
1115          --csi_item_instance_id = l_get_1st_level_subunit.subject_id
1116          --AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
1117          --Not necessary to check the object_version_number here
1118 
1119         --Copy the change to UC history table
1120         ahl_util_uc_pkg.copy_uc_header_to_history(l_sub_uc_header_id, l_return_status);
1121         --IF history copy failed, then don't raise exception, just add the messae to the message stack
1122         IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
1123           FND_MESSAGE.set_name('AHL', 'AHL_UC_HISTORY_COPY_FAILED');
1124           FND_MSG_PUB.add;
1125         END IF;
1126       END LOOP;
1127     END IF;
1128     CLOSE check_instance_non_leaf;
1129   END IF;
1130   CLOSE check_instance_subunit;
1131 
1132   IF (l_position_reference IS NOT NULL AND
1133       NOT ahl_util_uc_pkg.extra_node(p_instance_id, l_root_instance_id)) THEN
1134     OPEN get_position_necessity(l_position_reference);
1135     FETCH get_position_necessity INTO l_position_necessity;
1136     IF get_position_necessity%NOTFOUND THEN
1137       FND_MESSAGE.set_name( 'AHL','AHL_UC_POSTION_INVALID' );
1138       FND_MESSAGE.set_token('POSITION', l_position_reference);
1139       FND_MSG_PUB.add;
1140       CLOSE get_position_necessity;
1141       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1142     ELSE
1143       CLOSE get_position_necessity;
1144     END IF;
1145   END IF;
1146   --For UC user, UC header status change needs to be made after the operation
1147   --Not confirmed whether need to copy the record into UC header history table
1148   --after status change. Not include the copy right now.
1149   IF p_prod_user_flag = 'N' THEN
1150     IF (l_root_uc_status_code = 'COMPLETE' AND l_position_necessity = 'MANDATORY') THEN
1151     --IF unit_config_status_code='INCOMPLETE' and active_uc_status_code='UNAPPROVED', this
1152     --update is only object_version_number change and not necessary.
1153       UPDATE ahl_unit_config_headers
1154          SET unit_config_status_code = 'INCOMPLETE',
1155              active_uc_status_code = 'UNAPPROVED',
1156              object_version_number = object_version_number + 1,
1157              last_update_date = SYSDATE,
1158              last_updated_by = FND_GLOBAL.user_id,
1159              last_update_login = FND_GLOBAL.login_id
1160        WHERE unit_config_header_id = l_root_uc_header_id
1161          AND object_version_number = l_root_uc_ovn;
1162       IF SQL%ROWCOUNT = 0 THEN
1163         FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
1164         FND_MSG_PUB.add;
1165         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1166       END IF;
1167     ELSIF (l_root_uc_status_code IN ('COMPLETE', 'INCOMPLETE') AND
1168            (l_root_active_uc_status_code IS NULL OR
1169             l_root_active_uc_status_code <> 'UNAPPROVED')) THEN
1170     --IF unit_config_status_code='INCOMPLETE' and active_uc_status_code='UNAPPROVED', this
1171     --update is only object_version_number change and not necessary.
1172       UPDATE ahl_unit_config_headers
1173          SET active_uc_status_code = 'UNAPPROVED',
1174              object_version_number = object_version_number + 1,
1175              last_update_date = SYSDATE,
1176              last_updated_by = FND_GLOBAL.user_id,
1177              last_update_login = FND_GLOBAL.login_id
1178        WHERE unit_config_header_id = l_root_uc_header_id
1179          AND object_version_number = l_root_uc_ovn;
1180       IF SQL%ROWCOUNT = 0 THEN
1181         FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
1182         FND_MSG_PUB.add;
1183         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1184       END IF;
1185     ELSIF l_root_uc_status_code NOT IN ('COMPLETE', 'INCOMPLETE', 'DRAFT') THEN
1186     --IF unit_config_status_code='DRAFT', this update is only object_version_number change and
1187     --not necessary.
1188       UPDATE ahl_unit_config_headers
1189          SET unit_config_status_code = 'DRAFT',
1190              object_version_number = object_version_number + 1,
1191              last_update_date = SYSDATE,
1192              last_updated_by = FND_GLOBAL.user_id,
1193              last_update_login = FND_GLOBAL.login_id
1194        WHERE unit_config_header_id = l_root_uc_header_id
1195          AND object_version_number = l_root_uc_ovn;
1196       IF SQL%ROWCOUNT = 0 THEN
1197         FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
1198         FND_MSG_PUB.add;
1199         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1200       END IF;
1201     END IF;
1202   ELSIF p_prod_user_flag = 'Y' THEN
1203     IF (l_root_uc_status_code = 'COMPLETE' AND l_position_necessity = 'MANDATORY') THEN
1204       UPDATE ahl_unit_config_headers
1205          SET unit_config_status_code = 'INCOMPLETE',
1206              object_version_number = object_version_number + 1,
1207              last_update_date = SYSDATE,
1208              last_updated_by = FND_GLOBAL.user_id,
1209              last_update_login = FND_GLOBAL.login_id
1210        WHERE unit_config_header_id = l_root_uc_header_id
1211          AND object_version_number = l_root_uc_ovn;
1212       IF SQL%ROWCOUNT = 0 THEN
1213         FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
1214         FND_MSG_PUB.add;
1215         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1216       END IF;
1217     END IF;
1218   END IF;
1219 
1220   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1221     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
1222                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': After normal execution',
1223                    'At the end of the procedure');
1224   END IF;
1225 
1226   --Get all the error messages from the previous steps (if any) and raise the appropriate Exception
1227   l_msg_count := FND_MSG_PUB.count_msg;
1228   IF l_msg_count > 0 THEN
1229     x_msg_count := l_msg_count;
1230     RAISE FND_API.G_EXC_ERROR;
1231   END IF;
1232   -- Perform the Commit (if requested)
1233   IF FND_API.to_boolean(p_commit) THEN
1234     COMMIT;
1235   END IF;
1236   --Count and Get messages(optional)
1237   FND_MSG_PUB.count_and_get(
1238     p_encoded  => FND_API.G_FALSE,
1239     p_count    => x_msg_count,
1240     p_data     => x_msg_data);
1241 
1242 EXCEPTION
1243   WHEN FND_API.G_EXC_ERROR THEN
1244     ROLLBACK TO remove_instance;
1245     x_return_status := FND_API.G_RET_STS_ERROR ;
1246     FND_MSG_PUB.count_and_get(
1247       p_encoded  => FND_API.G_FALSE,
1248       p_count    => x_msg_count,
1249       p_data     => x_msg_data);
1250   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1251     ROLLBACK TO remove_instance;
1252     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1253     FND_MSG_PUB.count_and_get(
1254       p_encoded  => FND_API.G_FALSE,
1255       p_count    => x_msg_count,
1256       p_data     => x_msg_data);
1257   WHEN OTHERS THEN
1258     ROLLBACK TO remove_instance;
1259     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
1260     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
1261     THEN
1262       FND_MSG_PUB.add_exc_msg(
1263         p_pkg_name         => G_PKG_NAME,
1264         p_procedure_name   => l_api_name,
1265         p_error_text       => SUBSTRB(SQLERRM,1,240));
1266     END IF;
1267     FND_MSG_PUB.count_and_get(
1268       p_encoded  => FND_API.G_FALSE,
1269       p_count    => x_msg_count,
1270       p_data     => x_msg_data);
1271 END;
1272 
1273 -- Define procedure update_instance_attr
1274 -- This API is used to update an instance's (top node or non top node) attribute
1275 -- (serial number, serial_number_tag, lot_number, revision, mfg_date and etc.)
1276 PROCEDURE update_instance_attr(
1277   p_api_version           IN  NUMBER := 1.0,
1278   p_init_msg_list         IN  VARCHAR2 := FND_API.G_FALSE,
1279   p_commit                IN  VARCHAR2 := FND_API.G_FALSE,
1280   p_validation_level      IN  NUMBER := FND_API.G_VALID_LEVEL_FULL,
1281   x_return_status         OUT NOCOPY VARCHAR2,
1282   x_msg_count             OUT NOCOPY NUMBER,
1283   x_msg_data              OUT NOCOPY VARCHAR2,
1284   p_uc_header_id          IN  NUMBER,
1285   p_uc_instance_rec       IN  uc_instance_rec_type,
1286   p_prod_user_flag        IN  VARCHAR2)
1287 IS
1288   l_api_name       CONSTANT   VARCHAR2(30) := 'update_instance_attr';
1289   l_api_version    CONSTANT   NUMBER       := 1.0;
1290   l_return_status             VARCHAR2(1);
1291   l_msg_count                 NUMBER;
1292   l_msg_data                  VARCHAR2(2000);
1293   l_subject_id                NUMBER;
1294   l_object_id                 NUMBER;
1295   l_csi_relationship_id       NUMBER;
1296   l_object_version_number     NUMBER;
1297   l_position_reference        csi_ii_relationships.position_reference%TYPE;
1298   l_transaction_type_id       NUMBER;
1299   l_dummy                     NUMBER;
1300   l_uc_status_code            VARCHAR2(30);
1301   l_active_uc_status_code     VARCHAR2(30);
1302   l_root_uc_header_id         NUMBER;
1303   l_root_instance_id          NUMBER;
1304   l_root_uc_status_code       FND_LOOKUP_VALUES.lookup_code%TYPE;
1305   l_root_active_uc_status_code FND_LOOKUP_VALUES.lookup_code%TYPE;
1306   l_root_uc_ovn               NUMBER;
1307   l_return_val                BOOLEAN;
1308   l_attribute_value_id        NUMBER;
1309   l_attribute_value           csi_iea_values.attribute_value%TYPE;
1310   l_subscript                 NUMBER DEFAULT 0;
1311   l_subscript1                NUMBER DEFAULT 0;
1312   l_uc_instance_rec           uc_instance_rec_type;
1313   l_old_uc_instance_rec       uc_instance_rec_type;
1314   l_sn_tag_code               csi_iea_values.attribute_value%TYPE;
1315   l_sn_tag_rec_found          VARCHAR2(1) DEFAULT 'Y';
1316   l_serial_number_control     NUMBER;
1317   l_mfg_date                  csi_iea_values.attribute_value%TYPE;
1318   l_mfg_date_rec_found        VARCHAR2(1) DEFAULT 'Y';
1319   l_concatenated_segments     mtl_system_items_kfv.concatenated_segments%TYPE;
1320   l_lookup_code               fnd_lookups.lookup_code%TYPE;
1321   l_item_assoc_id             NUMBER;
1322   l_end_date                  DATE;
1323 
1324   --Variables needed for CSI API call
1325   l_csi_instance_rec          csi_datastructures_pub.instance_rec;
1326   l_csi_party_rec             csi_datastructures_pub.party_rec;
1327   l_csi_transaction_rec       csi_datastructures_pub.transaction_rec;
1328   l_csi_upd_transaction_rec   csi_datastructures_pub.transaction_rec;
1329   l_csi_relationship_rec      csi_datastructures_pub.ii_relationship_rec;
1330   l_csi_relationship_tbl      csi_datastructures_pub.ii_relationship_tbl;
1331   l_csi_party_tbl             csi_datastructures_pub.party_tbl;
1332   l_csi_account_tbl           csi_datastructures_pub.party_account_tbl;
1333   l_csi_pricing_attrib_tbl    csi_datastructures_pub.pricing_attribs_tbl;
1334   l_csi_org_assignments_tbl   csi_datastructures_pub.organization_units_tbl;
1335   l_csi_asset_assignment_tbl  csi_datastructures_pub.instance_asset_tbl;
1336   l_csi_instance_id_lst       csi_datastructures_pub.id_tbl;
1337 
1338   --Variables used for creating extended attributes.
1339   l_attribute_id               NUMBER;
1340   l_csi_extend_attrib_rec      csi_datastructures_pub.extend_attrib_values_rec;
1341   l_csi_extend_attrib_rec1     csi_datastructures_pub.extend_attrib_values_rec;
1342   l_csi_ext_attrib_values_tbl  csi_datastructures_pub.extend_attrib_values_tbl;
1343   l_csi_ext_attrib_values_tbl1 csi_datastructures_pub.extend_attrib_values_tbl;
1344 
1345   CURSOR check_uc_header IS
1346     SELECT unit_config_header_id,
1347            object_version_number,
1348            unit_config_status_code,
1349            active_uc_status_code,
1350            csi_item_instance_id
1351       FROM ahl_unit_config_headers
1352      WHERE unit_config_header_id = p_uc_header_id
1353        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
1354   l_check_uc_header check_uc_header%ROWTYPE;
1355   CURSOR get_uc_descendants(c_instance_id NUMBER) IS
1356     SELECT relationship_id,
1357            object_version_number,
1358            object_id,
1359            subject_id,
1360            position_reference
1361       FROM csi_ii_relationships
1362 START WITH object_id = c_instance_id
1363        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
1364        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
1365 CONNECT BY object_id = PRIOR subject_id
1366        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
1367        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
1368   CURSOR get_instance_attributes(c_instance_id NUMBER) IS
1369     SELECT instance_id,
1370            instance_number,
1371            inventory_item_id,
1372            last_vld_organization_id,
1373            serial_number,
1374            lot_number,
1375            quantity,
1376            unit_of_measure,
1377            install_date,
1378            inventory_revision,
1379            object_version_number
1380       FROM csi_item_instances
1381      WHERE instance_id = c_instance_id;
1382        --Removed on 02/26/2004, otherwise for expired instance, the error message displayed makes no sense.
1383        --AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
1384        --AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
1385 
1386 BEGIN
1387   --Initialize API return status to success
1388   x_return_status := FND_API.G_RET_STS_SUCCESS;
1389 
1390   -- Standard Start of API savepoint
1391   SAVEPOINT update_instance_attr;
1392 
1393   --Standard call to check for call compatibility.
1394   IF NOT FND_API.compatible_api_call(
1395     l_api_version,
1396     p_api_version,
1397     l_api_name,
1398     G_PKG_NAME)
1399   THEN
1400     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1401   END IF;
1402 
1403   --Initialize message list if p_init_msg_list is set to TRUE.
1404   IF FND_API.to_boolean( p_init_msg_list ) THEN
1405     FND_MSG_PUB.initialize;
1406   END IF;
1407 
1408   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1409     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
1410                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Begin API',
1411                    'At the start of the procedure');
1412   END IF;
1413 
1414   --Validate input parameters p_prod_user_flag
1415   IF upper(p_prod_user_flag) <> 'Y' AND upper(p_prod_user_flag) <> 'N' THEN
1416     FND_MESSAGE.set_name('AHL','AHL_UC_API_PARAMETER_INVALID');
1417     FND_MESSAGE.set_token('NAME', 'prod_user_flag');
1418     FND_MESSAGE.set_token('VALUE', p_prod_user_flag);
1419     FND_MSG_PUB.add;
1420     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1421   END IF;
1422   --Validate input parameter p_uc_header_id, its two statuses
1423   OPEN check_uc_header;
1424   FETCH check_uc_header INTO l_check_uc_header;
1425   IF check_uc_header%NOTFOUND THEN
1426     FND_MESSAGE.set_name('AHL','AHL_UC_API_PARAMETER_INVALID');
1427     FND_MESSAGE.set_token('NAME', 'uc_header_id');
1428     FND_MESSAGE.set_token('VALUE', p_uc_header_id);
1429     FND_MSG_PUB.add;
1430     CLOSE check_uc_header;
1431     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1432   ELSE
1433 
1434   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1435     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
1436                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': within API',
1437                    ' p_uc_header_id = '||p_uc_header_id||
1438                    ' IS_UNIT_QUARANTINED ='||ahl_util_uc_pkg.IS_UNIT_QUARANTINED(p_unit_header_id => p_uc_header_id , p_instance_id => null));
1439   END IF;
1440 
1441     -- ACL :: Changes for R12
1442     IF (ahl_util_uc_pkg.IS_UNIT_QUARANTINED(p_unit_header_id => p_uc_header_id , p_instance_id => null) = FND_API.G_TRUE) THEN
1443       FND_MESSAGE.set_name( 'AHL','AHL_UC_INVALID_Q_ACTION' );
1444       FND_MSG_PUB.add;
1445       CLOSE check_uc_header;
1446       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1447     END IF;
1448 
1449     ahl_util_uc_pkg.get_root_uc_attr(p_uc_header_id,
1450                                      l_root_uc_header_id,
1451                                      l_root_instance_id,
1452                                      l_root_uc_status_code,
1453                                      l_root_active_uc_status_code,
1454                                      l_root_uc_ovn);
1455     IF (p_prod_user_flag = 'Y' AND --For production user, no need to confirm either one of the statuses is not APPROVAL_PENDING
1456         l_root_uc_status_code NOT IN ('COMPLETE', 'INCOMPLETE')) THEN
1457       FND_MESSAGE.set_name( 'AHL','AHL_UC_STATUS_NOT_ACTIVE' );
1458       FND_MSG_PUB.add;
1459       CLOSE check_uc_header;
1460       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1461     ELSIF (p_prod_user_flag = 'N' AND
1462            (l_root_uc_status_code = 'APPROVAL_PENDING' OR
1463             l_root_active_uc_status_code = 'APPROVAL_PENDING')) THEN
1464       FND_MESSAGE.set_name( 'AHL','AHL_UC_STATUS_PENDING' );
1465       FND_MESSAGE.set_token( 'UC_HEADER_ID', l_root_uc_header_id);
1466       FND_MSG_PUB.add;
1467       CLOSE check_uc_header;
1468       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1469     ELSE
1470       CLOSE check_uc_header;
1471     END IF;
1472   END IF;
1473   --Make sure p_uc_instance_rec is installed in the UC
1474   FOR l_get_uc_descendant IN get_uc_descendants(l_check_uc_header.csi_item_instance_id) LOOP
1475     l_csi_relationship_id := l_get_uc_descendant.relationship_id;
1476     l_object_version_number := l_get_uc_descendant.object_version_number;
1477     l_object_id := l_get_uc_descendant.object_id;
1478     l_subject_id := l_get_uc_descendant.subject_id;
1479     l_position_reference := l_get_uc_descendant.position_reference;
1480     EXIT WHEN l_subject_id = p_uc_instance_rec.instance_id OR
1481               l_object_id = p_uc_instance_rec.instance_id;
1482   END LOOP;
1483   --Ensure the instance is installed in this UC
1484   IF (l_object_id <> p_uc_instance_rec.instance_id AND
1485       (l_subject_id <> p_uc_instance_rec.instance_id OR
1486        l_subject_id IS NULL)) THEN
1487     --Do we allow an extra node's attributes to be changed? Yes
1488     FND_MESSAGE.set_name( 'AHL','AHL_UC_INSTANCE_NOT_IN_UC' );
1489     FND_MESSAGE.set_token('INSTANCE', p_uc_instance_rec.instance_id);
1490     FND_MSG_PUB.add;
1491     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1492   END IF;
1493 
1494   --Make sure p_uc_instance_rec.instance_id is not expired otherwise update is not allowed
1495   --Added on 02/26/2004
1496   OPEN get_instance_date(p_uc_instance_rec.instance_id);
1497   FETCH get_instance_date INTO l_end_date;
1498   CLOSE get_instance_date;
1499   IF TRUNC(NVL(l_end_date, SYSDATE+1)) <= TRUNC(SYSDATE) THEN
1500     FND_MESSAGE.set_name( 'AHL','AHL_UC_INSTANCE_EXPIRED');
1501     FND_MESSAGE.set_token('INSTANCE', p_uc_instance_rec.instance_id);
1502     FND_MSG_PUB.add;
1503     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1504   END IF;
1505 
1506   --Get the original instance attributes from database. Instance_id can't be changed
1507   --And p_uc_instance_rec contains the new attributes
1508   OPEN get_instance_attributes(p_uc_instance_rec.instance_id);
1509   FETCH get_instance_attributes INTO l_old_uc_instance_rec.instance_id,
1510                                      l_old_uc_instance_rec.instance_number,
1511                                      l_old_uc_instance_rec.inventory_item_id,
1512                                      l_old_uc_instance_rec.inventory_org_id,
1513                                      l_old_uc_instance_rec.serial_number,
1514                                      l_old_uc_instance_rec.lot_number,
1515                                      l_old_uc_instance_rec.quantity,
1516                                      l_old_uc_instance_rec.uom_code,
1517                                      l_old_uc_instance_rec.install_date,
1518                                      l_old_uc_instance_rec.revision,
1519                                      l_old_uc_instance_rec.object_version_number;
1520 
1521   CLOSE get_instance_attributes;
1522 
1523   --Added by mpothuku on 16-Jul-2007 to fix the Bug 4337259
1524   --Retrieve the old serial tag code so that change validations can be performed
1525   AHL_UTIL_UC_PKG.getcsi_attribute_value(l_old_uc_instance_rec.instance_id,
1526                                          'AHL_TEMP_SERIAL_NUM',
1527                                          l_attribute_value,
1528                                          l_attribute_value_id,
1529                                          l_object_version_number,
1530                                          l_return_val);
1531   IF l_return_val THEN
1532     l_old_uc_instance_rec.sn_tag_code := l_attribute_value;
1533   ELSE
1534     l_old_uc_instance_rec.sn_tag_code := null;
1535   END IF;
1536   --mpothuku End
1537   l_uc_instance_rec := p_uc_instance_rec;
1538 
1539   /*
1540   FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
1541                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Begin API','The input rec is as following:');
1542   FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
1543                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Begin API','instance_id = '||l_uc_instance_rec.instance_id);
1544   FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
1545                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Begin API','obj_ver_num = '||l_uc_instance_rec.object_version_number);
1546   FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
1547                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Begin API','instance_num = '||l_uc_instance_rec.instance_number);
1548   FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
1549                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Begin API','inv_item_id = '||l_uc_instance_rec.inventory_item_id);
1550   FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
1551                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Begin API','org_id = '||l_uc_instance_rec.inventory_org_id);
1552   FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
1553                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Begin API','serial_no = '||l_uc_instance_rec.serial_number);
1554   FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
1555                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Begin API','lot_no = '||l_uc_instance_rec.lot_number);
1556   FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
1557                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Begin API','quantity = '||l_uc_instance_rec.quantity);
1558   FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
1559                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Begin API','uom_code = '||l_uc_instance_rec.uom_code);
1560   FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
1561                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Begin API','install_date = '||l_uc_instance_rec.install_date);
1562   FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
1563                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Begin API','revision = '||l_uc_instance_rec.revision);
1564   */
1565 
1566   --Convert serial_number_tag_meaning to its code
1567   IF (l_uc_instance_rec.sn_tag_code IS NULL OR
1568       l_uc_instance_rec.sn_tag_code = FND_API.G_MISS_CHAR) THEN
1569     IF (l_uc_instance_rec.sn_tag_meaning IS NOT NULL AND
1570         l_uc_instance_rec.sn_tag_meaning <> FND_API.G_MISS_CHAR) THEN
1571       AHL_UTIL_MC_PKG.convert_to_lookupcode('AHL_SERIALNUMBER_TAG',
1572                                             l_uc_instance_rec.sn_tag_meaning,
1573                                             l_lookup_code,
1574                                             l_return_val);
1575       IF NOT(l_return_val) THEN
1576         FND_MESSAGE.set_name('AHL','AHL_UC_TAGMEANING_INVALID');
1577         FND_MESSAGE.set_token('TAG',l_uc_instance_rec.sn_tag_meaning);
1578         FND_MSG_PUB.add;
1579       ELSE
1580         l_uc_instance_rec.sn_tag_code := l_lookup_code;
1581       END IF;
1582     END IF;
1583   END IF;
1584   --dbms_output.put_line('After convert serial tag');
1585 
1586   --Like instance_id, inventory_item_id and inventory_org_id can't be changed
1587   IF ((l_uc_instance_rec.inventory_item_id <> FND_API.G_MISS_NUM AND
1588        l_uc_instance_rec.inventory_item_id <> l_old_uc_instance_rec.inventory_item_id) OR
1589       (l_uc_instance_rec.inventory_org_id <> FND_API.G_MISS_NUM AND
1590        l_uc_instance_rec.inventory_org_id <> l_old_uc_instance_rec.inventory_org_id)) THEN
1591     FND_MESSAGE.Set_Name('AHL','AHL_COM_KEY_NOUPDATE');
1592     FND_MSG_PUB.ADD;
1593     --dbms_output.put_line('Cannot update key values');
1594     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1595   END IF;
1596 
1597   --Validate against inventory for changed items.
1598   --Following the old G_MISS standard, passing G_MISS value means no change for that column.
1599   IF (l_uc_instance_rec.relationship_id = FND_API.G_MISS_NUM OR
1600       l_uc_instance_rec.relationship_id IS NULL ) THEN
1601     l_uc_instance_rec.relationship_id := TO_NUMBER(l_position_reference);
1602   END IF;
1603 
1604   IF (l_uc_instance_rec.inventory_item_id = FND_API.G_MISS_NUM OR
1605       l_uc_instance_rec.inventory_item_id IS NULL ) THEN
1606     l_uc_instance_rec.inventory_item_id := l_old_uc_instance_rec.inventory_item_id;
1607   END IF;
1608 
1609   IF (l_uc_instance_rec.inventory_org_id = FND_API.G_MISS_NUM OR
1610       l_uc_instance_rec.inventory_org_id IS NULL) THEN
1611     l_uc_instance_rec.inventory_org_id := l_old_uc_instance_rec.inventory_org_id;
1612   END IF;
1613 
1614   IF (l_uc_instance_rec.quantity = FND_API.G_MISS_NUM OR
1615       l_uc_instance_rec.quantity IS NULL) THEN
1616     l_uc_instance_rec.quantity := l_old_uc_instance_rec.quantity;
1617   END IF;
1618 
1619   IF (l_uc_instance_rec.uom_code = FND_API.G_MISS_CHAR OR
1620       l_uc_instance_rec.uom_code IS NULL) THEN
1621     l_uc_instance_rec.uom_code := l_old_uc_instance_rec.uom_code;
1622   END IF;
1623 
1624   IF (l_uc_instance_rec.install_date = FND_API.G_MISS_DATE OR
1625       l_uc_instance_rec.install_date IS NULL) THEN
1626     l_uc_instance_rec.install_date := l_old_uc_instance_rec.install_date;
1627   END IF;
1628 
1629   --For the following updatable columns, front end code following the new
1630   --API G_MISS standard. If the value is blank, then pass G_MISS, othewise
1631   --pass the old value(unchanged) or new value (changed)
1632   IF (l_uc_instance_rec.serial_number = FND_API.G_MISS_CHAR) THEN
1633     l_uc_instance_rec.serial_number := NULL;
1634   END IF;
1635 
1636   IF (l_uc_instance_rec.revision = FND_API.G_MISS_CHAR) THEN
1637     l_uc_instance_rec.revision := NULL;
1638   END IF;
1639 
1640   IF (l_uc_instance_rec.lot_number = FND_API.G_MISS_CHAR) THEN
1641     l_uc_instance_rec.lot_number := NULL;
1642   END IF;
1643 
1644   IF (l_uc_instance_rec.sn_tag_code = FND_API.G_MISS_CHAR) THEN
1645     l_uc_instance_rec.sn_tag_code := NULL;
1646   END IF;
1647 
1648   IF (l_uc_instance_rec.mfg_date = FND_API.G_MISS_DATE) THEN
1649     l_uc_instance_rec.mfg_date := NULL;
1650   END IF;
1651 
1652   -- SATHAPLI::FP ER 6453212, 10-Nov-2008
1653   -- nullify the flexfield data in the UC instance record if G_MISS_CHAR is there
1654   IF (l_uc_instance_rec.context = FND_API.G_MISS_CHAR) THEN
1655     l_uc_instance_rec.context := NULL;
1656   END IF;
1657 
1658   IF (l_uc_instance_rec.attribute1 = FND_API.G_MISS_CHAR) THEN
1659     l_uc_instance_rec.attribute1 := NULL;
1660   END IF;
1661 
1662   IF (l_uc_instance_rec.attribute2 = FND_API.G_MISS_CHAR) THEN
1663     l_uc_instance_rec.attribute2 := NULL;
1664   END IF;
1665 
1666   IF (l_uc_instance_rec.attribute3 = FND_API.G_MISS_CHAR) THEN
1667     l_uc_instance_rec.attribute3 := NULL;
1668   END IF;
1669 
1670   IF (l_uc_instance_rec.attribute4 = FND_API.G_MISS_CHAR) THEN
1671     l_uc_instance_rec.attribute4 := NULL;
1672   END IF;
1673 
1674   IF (l_uc_instance_rec.attribute5 = FND_API.G_MISS_CHAR) THEN
1675     l_uc_instance_rec.attribute5 := NULL;
1676   END IF;
1677 
1678   IF (l_uc_instance_rec.attribute6 = FND_API.G_MISS_CHAR) THEN
1679     l_uc_instance_rec.attribute6 := NULL;
1680   END IF;
1681 
1682   IF (l_uc_instance_rec.attribute7 = FND_API.G_MISS_CHAR) THEN
1683     l_uc_instance_rec.attribute7 := NULL;
1684   END IF;
1685 
1686   IF (l_uc_instance_rec.attribute8 = FND_API.G_MISS_CHAR) THEN
1687     l_uc_instance_rec.attribute8 := NULL;
1688   END IF;
1689 
1690   IF (l_uc_instance_rec.attribute9 = FND_API.G_MISS_CHAR) THEN
1691     l_uc_instance_rec.attribute9 := NULL;
1692   END IF;
1693 
1694   IF (l_uc_instance_rec.attribute10 = FND_API.G_MISS_CHAR) THEN
1695     l_uc_instance_rec.attribute10 := NULL;
1696   END IF;
1697 
1698   IF (l_uc_instance_rec.attribute11 = FND_API.G_MISS_CHAR) THEN
1699     l_uc_instance_rec.attribute11 := NULL;
1700   END IF;
1701 
1702   IF (l_uc_instance_rec.attribute12 = FND_API.G_MISS_CHAR) THEN
1703     l_uc_instance_rec.attribute12 := NULL;
1704   END IF;
1705 
1706   IF (l_uc_instance_rec.attribute13 = FND_API.G_MISS_CHAR) THEN
1707     l_uc_instance_rec.attribute13 := NULL;
1708   END IF;
1709 
1710   IF (l_uc_instance_rec.attribute14 = FND_API.G_MISS_CHAR) THEN
1711     l_uc_instance_rec.attribute14 := NULL;
1712   END IF;
1713 
1714   IF (l_uc_instance_rec.attribute15 = FND_API.G_MISS_CHAR) THEN
1715     l_uc_instance_rec.attribute15 := NULL;
1716   END IF;
1717 
1718   IF (l_uc_instance_rec.attribute16 = FND_API.G_MISS_CHAR) THEN
1719     l_uc_instance_rec.attribute16 := NULL;
1720   END IF;
1721 
1722   IF (l_uc_instance_rec.attribute17 = FND_API.G_MISS_CHAR) THEN
1723     l_uc_instance_rec.attribute17 := NULL;
1724   END IF;
1725 
1726   IF (l_uc_instance_rec.attribute18 = FND_API.G_MISS_CHAR) THEN
1727     l_uc_instance_rec.attribute18 := NULL;
1728   END IF;
1729 
1730   IF (l_uc_instance_rec.attribute19 = FND_API.G_MISS_CHAR) THEN
1731     l_uc_instance_rec.attribute19 := NULL;
1732   END IF;
1733 
1734   IF (l_uc_instance_rec.attribute20 = FND_API.G_MISS_CHAR) THEN
1735     l_uc_instance_rec.attribute20 := NULL;
1736   END IF;
1737 
1738   IF (l_uc_instance_rec.attribute21 = FND_API.G_MISS_CHAR) THEN
1739     l_uc_instance_rec.attribute21 := NULL;
1740   END IF;
1741 
1742   IF (l_uc_instance_rec.attribute22 = FND_API.G_MISS_CHAR) THEN
1743     l_uc_instance_rec.attribute22 := NULL;
1744   END IF;
1745 
1746   IF (l_uc_instance_rec.attribute23 = FND_API.G_MISS_CHAR) THEN
1747     l_uc_instance_rec.attribute23 := NULL;
1748   END IF;
1749 
1750   IF (l_uc_instance_rec.attribute24 = FND_API.G_MISS_CHAR) THEN
1751     l_uc_instance_rec.attribute24 := NULL;
1752   END IF;
1753 
1754   IF (l_uc_instance_rec.attribute25 = FND_API.G_MISS_CHAR) THEN
1755     l_uc_instance_rec.attribute25 := NULL;
1756   END IF;
1757 
1758   IF (l_uc_instance_rec.attribute26 = FND_API.G_MISS_CHAR) THEN
1759     l_uc_instance_rec.attribute26 := NULL;
1760   END IF;
1761 
1762   IF (l_uc_instance_rec.attribute27 = FND_API.G_MISS_CHAR) THEN
1763     l_uc_instance_rec.attribute27 := NULL;
1764   END IF;
1765 
1766   IF (l_uc_instance_rec.attribute28 = FND_API.G_MISS_CHAR) THEN
1767     l_uc_instance_rec.attribute28 := NULL;
1768   END IF;
1769 
1770   IF (l_uc_instance_rec.attribute29 = FND_API.G_MISS_CHAR) THEN
1771     l_uc_instance_rec.attribute29 := NULL;
1772   END IF;
1773 
1774   IF (l_uc_instance_rec.attribute30 = FND_API.G_MISS_CHAR) THEN
1775     l_uc_instance_rec.attribute30 := NULL;
1776   END IF;
1777 
1778   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1779       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name,
1780                      ' l_old_uc_instance_rec.instance_id => '||l_old_uc_instance_rec.instance_id||
1781                      ' l_old_uc_instance_rec.sn_tag_code => '||l_old_uc_instance_rec.sn_tag_code ||
1782                      ' l_uc_instance_rec.sn_tag_code =>' || l_uc_instance_rec.sn_tag_code);
1783   END IF;
1784 
1785   --mpothuku added on 13-Jul-2007 to fix the Bug 4337259
1786   IF(l_old_uc_instance_rec.sn_tag_code is not NULL AND l_old_uc_instance_rec.sn_tag_code IN ('ACTUAL','TEMPORARY')) THEN
1787     IF(l_uc_instance_rec.sn_tag_code is not NULL AND l_uc_instance_rec.sn_tag_code = 'INVENTORY') THEN
1788       FND_MESSAGE.Set_Name('AHL','AHL_UC_SER_TG_EDIT_INVEN');
1789       FND_MSG_PUB.ADD;
1790     END IF;
1791   END IF;
1792   --mpothuku End
1793 
1794   --Validate Inventory Item details.
1795   validate_uc_invdetails(l_uc_instance_rec.inventory_item_id,
1796                          l_uc_instance_rec.inventory_org_id,
1797                          l_uc_instance_rec.serial_number,
1798                          l_uc_instance_rec.sn_tag_code,
1799                          l_uc_instance_rec.quantity,
1800                          l_uc_instance_rec.uom_code,
1801                          l_uc_instance_rec.revision,
1802                          l_uc_instance_rec.lot_number,
1803                          NULL,
1804                          l_concatenated_segments);
1805 
1806   --dbms_output.put_line('after validating invdetails');
1807   --Validate positional attributes.
1808   /*
1809   AHL_UTIL_UC_PKG.validate_for_position(l_uc_instance_rec.relationship_id,
1810                                         l_uc_instance_rec.inventory_item_id,
1811                                         l_uc_instance_rec.inventory_org_id,
1812                                         l_uc_instance_rec.quantity,
1813                                         l_uc_instance_rec.revision,
1814                                         l_uc_instance_rec.uom_code,
1815                                         NULL,
1816                                         l_item_assoc_id);
1817   --Check Error Message stack.
1818   l_msg_count := FND_MSG_PUB.count_msg;
1819   IF l_msg_count > 0 THEN
1820     RAISE  FND_API.G_EXC_ERROR;
1821   END IF;
1822   */
1823   --Validate Installation Date.
1824   --Removed this validation after Alex talking to Barry. Because in UC we don't provide any
1825   --way for the user to update the installation date. (04/21/2004)
1826   /*
1827   IF (l_uc_instance_rec.install_date IS NOT NULL AND
1828       l_uc_instance_rec.install_date <> FND_API.G_MISS_DATE) THEN
1829     IF (l_uc_instance_rec.install_date > SYSDATE) THEN
1830       FND_MESSAGE.Set_Name('AHL','AHL_UC_INSTDATE_INVALID');
1831       FND_MESSAGE.Set_Token('DATE',l_uc_instance_rec.install_date);
1832       FND_MESSAGE.Set_Token('INV_ITEM',l_concatenated_segments);
1833       FND_MSG_PUB.ADD;
1834       --dbms_output.put_line('Installation date invalid.');
1835     END IF;
1836   END IF;
1837   */
1838 
1839   --Validate mfg_date if not null.
1840   IF (l_uc_instance_rec.mfg_date IS NOT NULL AND l_uc_instance_rec.mfg_date <> FND_API.G_MISS_DATE) THEN
1841     IF (l_uc_instance_rec.mfg_date > SYSDATE) THEN
1842       FND_MESSAGE.Set_Name('AHL','AHL_UC_MFGDATE_INVALID');
1843       FND_MESSAGE.Set_Token('DATE',l_uc_instance_rec.mfg_date);
1844       FND_MESSAGE.Set_Token('INV_ITEM',l_concatenated_segments);
1845       FND_MSG_PUB.ADD;
1846       --dbms_output.put_line('Mfg date invalid.');
1847     END IF;
1848   END IF;
1849 
1850   --Check Error Message stack.
1851   l_msg_count := FND_MSG_PUB.count_msg;
1852   IF l_msg_count > 0 THEN
1853     RAISE  FND_API.G_EXC_ERROR;
1854   END IF;
1855 
1856   --Retrieve existing value of sn_tag_code if present.
1857   AHL_UTIL_UC_PKG.getcsi_attribute_value(l_uc_instance_rec.instance_id,
1858                                          'AHL_TEMP_SERIAL_NUM',
1859                                          l_attribute_value,
1860                                          l_attribute_value_id,
1861                                          l_object_version_number,
1862                                          l_return_val);
1863   IF l_return_val THEN
1864     l_sn_tag_code := l_attribute_value;
1865     l_sn_tag_rec_found := 'Y';
1866   ELSE
1867     l_sn_tag_code := null;
1868     l_sn_tag_rec_found := 'N';
1869   END IF;
1870   --dbms_output.put_line('After get serial tag code');
1871 
1872   --Build extended attribute record for sn_tag_code.
1873   IF (l_sn_tag_rec_found = 'Y' ) THEN
1874     /* This IF condition is not necessary
1875     IF (l_uc_instance_rec.sn_tag_code IS NULL AND l_sn_tag_code IS NOT NULL) OR
1876        (l_sn_tag_code IS NULL AND l_uc_instance_rec.sn_tag_code IS NOT NULL) OR
1877        (l_uc_instance_rec.sn_tag_code IS NOT NULL AND l_sn_tag_code IS NOT NULL AND
1878         l_uc_instance_rec.sn_tag_code <> FND_API.G_MISS_CHAR AND
1879         l_uc_instance_rec.sn_tag_code <> l_sn_tag_code) THEN
1880     */
1881       --Changed value so update attribute record.
1882       l_csi_extend_attrib_rec.attribute_value_id := l_attribute_value_id;
1883       l_csi_extend_attrib_rec.attribute_value := l_uc_instance_rec.sn_tag_code;
1884       l_csi_extend_attrib_rec.object_version_number := l_object_version_number;
1885       l_subscript := l_subscript + 1;
1886       l_csi_ext_attrib_values_tbl(l_subscript) := l_csi_extend_attrib_rec;
1887     --END IF;
1888   ELSIF (l_sn_tag_rec_found = 'N') THEN
1889     IF (l_uc_instance_rec.sn_tag_code IS NOT NULL) THEN
1890       -- create extended attributes.
1891       AHL_Util_UC_Pkg.getcsi_attribute_id('AHL_TEMP_SERIAL_NUM', l_attribute_id,l_return_val);
1892       IF NOT(l_return_val) THEN
1893         FND_MESSAGE.Set_Name('AHL','AHL_UC_ATTRIB_CODE_MISSING');
1894         FND_MESSAGE.Set_Token('CODE', 'AHL_TEMP_SERIAL_NUM');
1895         FND_MSG_PUB.ADD;
1896         --dbms_output.put_line('Attribute code for TEMP_SERIAL_NUM not found');
1897       ELSE
1898         l_csi_extend_attrib_rec1.attribute_id := l_attribute_id;
1899         l_csi_extend_attrib_rec1.attribute_value := l_uc_instance_rec.sn_tag_code;
1900         l_csi_extend_attrib_rec1.instance_id := l_uc_instance_rec.instance_id;
1901         l_subscript1 := l_subscript1 + 1;
1902         l_csi_ext_attrib_values_tbl1(l_subscript1) := l_csi_extend_attrib_rec1;
1903       END IF;
1904     END IF;
1905   END IF;
1906 
1907   --Retrieve existing value of manufacturing date if present.
1908   AHL_UTIL_UC_PKG.getcsi_attribute_value(l_uc_instance_rec.instance_id,
1909                                          'AHL_MFG_DATE',
1910                                          l_attribute_value,
1911                                          l_attribute_value_id,
1912                                          l_object_version_number,
1913                                          l_return_val);
1914   --Adding exception part in case l_attribute_value without an valid DATE fromat
1915   BEGIN
1916     IF NOT(l_return_val) THEN
1917       l_mfg_date := null;
1918       l_mfg_date_rec_found := 'N';
1919     ELSE
1920       l_mfg_date := to_date(l_attribute_value, 'DD/MM/YYYY');
1921       l_mfg_date_rec_found := 'Y';
1922     END IF;
1923   EXCEPTION
1924     WHEN OTHERS THEN
1925       FND_MESSAGE.Set_Name('AHL','AHL_UC_MFGDATE_INVALID');
1926       FND_MSG_PUB.ADD;
1927       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1928   END;
1929   --dbms_output.put_line('after get mfg_date');
1930 
1931   --Build extended attribs record for mfg_date.
1932   IF (l_mfg_date_rec_found = 'Y' ) THEN
1933     /* This IF condition is not necessary
1934     IF (l_uc_instance_rec.mfg_date IS NULL AND l_mfg_date IS NOT NULL) OR
1935        (l_mfg_date IS NULL AND l_uc_instance_rec.mfg_date IS NOT NULL) OR
1936        (l_uc_instance_rec.mfg_date IS NOT NULL AND l_mfg_date IS NOT NULL AND
1937         l_uc_instance_rec.mfg_date <> FND_API.G_MISS_DATE AND
1938         l_uc_instance_rec.mfg_date <> l_mfg_date) THEN
1939     */
1940       --Changed value so update attribute record.
1941       l_csi_extend_attrib_rec.attribute_value_id := l_attribute_value_id;
1942       l_csi_extend_attrib_rec.attribute_value := to_char(l_uc_instance_rec.mfg_date, 'DD/MM/YYYY');
1943       l_csi_extend_attrib_rec.object_version_number := l_object_version_number;
1944       l_subscript := l_subscript + 1;
1945       l_csi_ext_attrib_values_tbl(l_subscript) := l_csi_extend_attrib_rec;
1946     --END IF;
1947   ELSIF (l_mfg_date_rec_found = 'N' ) THEN
1948     IF (l_uc_instance_rec.mfg_date IS NOT NULL) THEN
1949       AHL_Util_UC_Pkg.getcsi_attribute_id('AHL_MFG_DATE', l_attribute_id, l_return_val);
1950       IF NOT(l_return_val) THEN
1951         FND_MESSAGE.Set_Name('AHL','AHL_UC_ATTRIB_CODE_MISSING');
1952         FND_MESSAGE.Set_Token('CODE', 'AHL_MFG_DATE');
1953         FND_MSG_PUB.ADD;
1954         --dbms_output.put_line('Attribute code for AHL_MFG_DATE not found');
1955       ELSE
1956         l_csi_extend_attrib_rec1.attribute_id := l_attribute_id;
1957         l_csi_extend_attrib_rec1.attribute_value := to_char(l_uc_instance_rec.mfg_date, 'DD/MM/YYYY');
1958         l_subscript1 := l_subscript1 + 1;
1959         l_csi_extend_attrib_rec1.instance_id := l_uc_instance_rec.instance_id;
1960         l_csi_ext_attrib_values_tbl1(l_subscript1) := l_csi_extend_attrib_rec1;
1961       END IF;
1962     END IF;
1963   END IF;
1964 
1965   --Check Error Message stack.
1966   l_msg_count := FND_MSG_PUB.count_msg;
1967   IF l_msg_count > 0 THEN
1968     RAISE  FND_API.G_EXC_ERROR;
1969   END IF;
1970 
1971   --Update item.
1972   l_csi_instance_rec.instance_id := l_uc_instance_rec.instance_id;
1973   l_csi_instance_rec.object_version_number := l_uc_instance_rec.object_version_number;
1974   l_csi_instance_rec.quantity := l_uc_instance_rec.quantity;
1975   l_csi_instance_rec.lot_number := l_uc_instance_rec.lot_number;
1976   l_csi_instance_rec.serial_number := l_uc_instance_rec.serial_number;
1977   l_csi_instance_rec.unit_of_measure := l_uc_instance_rec.uom_code;
1978   l_csi_instance_rec.inventory_revision := l_uc_instance_rec.revision;
1979   l_csi_instance_rec.install_date := l_uc_instance_rec.install_date;
1980 
1981   -- SATHAPLI::FP ER 6453212, 10-Nov-2008
1982   -- populate the flexfield data in the CSI record
1983   l_csi_instance_rec.context     := l_uc_instance_rec.context;
1984   l_csi_instance_rec.attribute1  := l_uc_instance_rec.attribute1;
1985   l_csi_instance_rec.attribute2  := l_uc_instance_rec.attribute2;
1986   l_csi_instance_rec.attribute3  := l_uc_instance_rec.attribute3;
1987   l_csi_instance_rec.attribute4  := l_uc_instance_rec.attribute4;
1988   l_csi_instance_rec.attribute5  := l_uc_instance_rec.attribute5;
1989   l_csi_instance_rec.attribute6  := l_uc_instance_rec.attribute6;
1990   l_csi_instance_rec.attribute7  := l_uc_instance_rec.attribute7;
1991   l_csi_instance_rec.attribute8  := l_uc_instance_rec.attribute8;
1992   l_csi_instance_rec.attribute9  := l_uc_instance_rec.attribute9;
1993   l_csi_instance_rec.attribute10 := l_uc_instance_rec.attribute10;
1994   l_csi_instance_rec.attribute11 := l_uc_instance_rec.attribute11;
1995   l_csi_instance_rec.attribute12 := l_uc_instance_rec.attribute12;
1996   l_csi_instance_rec.attribute13 := l_uc_instance_rec.attribute13;
1997   l_csi_instance_rec.attribute14 := l_uc_instance_rec.attribute14;
1998   l_csi_instance_rec.attribute15 := l_uc_instance_rec.attribute15;
1999   l_csi_instance_rec.attribute16 := l_uc_instance_rec.attribute16;
2000   l_csi_instance_rec.attribute17 := l_uc_instance_rec.attribute17;
2001   l_csi_instance_rec.attribute18 := l_uc_instance_rec.attribute18;
2002   l_csi_instance_rec.attribute19 := l_uc_instance_rec.attribute19;
2003   l_csi_instance_rec.attribute20 := l_uc_instance_rec.attribute20;
2004   l_csi_instance_rec.attribute21 := l_uc_instance_rec.attribute21;
2005   l_csi_instance_rec.attribute22 := l_uc_instance_rec.attribute22;
2006   l_csi_instance_rec.attribute23 := l_uc_instance_rec.attribute23;
2007   l_csi_instance_rec.attribute24 := l_uc_instance_rec.attribute24;
2008   l_csi_instance_rec.attribute25 := l_uc_instance_rec.attribute25;
2009   l_csi_instance_rec.attribute26 := l_uc_instance_rec.attribute26;
2010   l_csi_instance_rec.attribute27 := l_uc_instance_rec.attribute27;
2011   l_csi_instance_rec.attribute28 := l_uc_instance_rec.attribute28;
2012   l_csi_instance_rec.attribute29 := l_uc_instance_rec.attribute29;
2013   l_csi_instance_rec.attribute30 := l_uc_instance_rec.attribute30;
2014 
2015   --dbms_output.put_line('The new csi rec is as following:');
2016   --dbms_output.put_line('instance_id = '||l_csi_instance_rec.instance_id);
2017   --dbms_output.put_line('obj_ver_num = '||l_csi_instance_rec.object_version_number);
2018   --dbms_output.put_line('serial_no = '||l_csi_instance_rec.serial_number);
2019   --dbms_output.put_line('lot_no = '||l_csi_instance_rec.lot_number);
2020   --dbms_output.put_line('quantity = '||l_csi_instance_rec.quantity);
2021   --dbms_output.put_line('uom_code = '||l_csi_instance_rec.unit_of_measure);
2022   --dbms_output.put_line('install_date = '||l_csi_instance_rec.install_date);
2023   --dbms_output.put_line('revision = '||l_csi_instance_rec.inventory_revision);
2024 
2025 --  IF (l_uc_instance_rec.sn_tag_code = 'INVENTORY') THEN
2026 --    l_csi_instance_rec.mfg_serial_number_flag := 'Y';
2027 --  ELSE
2028 --  Changed by jaramana on April 26, 2005,
2029 --  As per request from IB team (Briestly Manesh), always setting to N
2030     l_csi_instance_rec.mfg_serial_number_flag := 'N';
2031 --  END IF;
2032 
2033   --Build CSI transaction record
2034   --First get transaction_type_id
2035   AHL_Util_UC_Pkg.getcsi_transaction_id('UC_UPDATE',l_transaction_type_id, l_return_val);
2036 
2037   IF NOT(l_return_val) THEN
2038     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2039   END IF;
2040   l_csi_transaction_rec.source_transaction_date := SYSDATE;
2041   l_csi_transaction_rec.transaction_type_id := l_transaction_type_id;
2042 
2043   /**
2044     Added by jaramana on April 26, 2005.
2045     Throw an exception if there are error messages even before calling
2046     the CSI APIs since the CSI API returns a Confirmation/Warning message
2047     even when the return status is S when the serial number is changed.
2048   **/
2049   --Get all the error messages from the previous steps (if any) and raise the appropriate Exception
2050   l_msg_count := FND_MSG_PUB.count_msg;
2051   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2052     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
2053                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': After normal execution',
2054                    'l_msg_count='||l_msg_count||' x_return_status='||x_return_status);
2055   END IF;
2056   IF l_msg_count > 0 THEN
2057     x_msg_count := l_msg_count;
2058     RAISE FND_API.G_EXC_ERROR;
2059   END IF;
2060   /** End addition by jaramana **/
2061 
2062   --Call CSI API to update instance attributes.
2063   CSI_ITEM_INSTANCE_PUB.update_item_instance(
2064                        p_api_version            => 1.0,
2065                        p_instance_rec           => l_csi_instance_rec,
2066                        p_txn_rec                => l_csi_transaction_rec,
2067                        p_ext_attrib_values_tbl  => l_csi_ext_attrib_values_tbl,
2068                        p_party_tbl              => l_csi_party_tbl,
2069                        p_account_tbl            => l_csi_account_tbl,
2070                        p_pricing_attrib_tbl     => l_csi_pricing_attrib_tbl,
2071                        p_org_assignments_tbl    => l_csi_org_assignments_tbl,
2072                        p_asset_assignment_tbl   => l_csi_asset_assignment_tbl,
2073                        x_instance_id_lst        => l_csi_instance_id_lst,
2074                        x_return_status          => l_return_status,
2075                        x_msg_count              => l_msg_count,
2076                        x_msg_data               => l_msg_data);
2077   IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
2078     RAISE FND_API.G_EXC_ERROR;
2079   ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
2080     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2081   END IF;
2082 
2083   --Create extended attributes if applicable.
2084   IF (l_subscript1 > 0) THEN
2085     --Call CSI API to create extended attributes.
2086     CSI_ITEM_INSTANCE_PUB.create_extended_attrib_values(
2087                           p_api_version            => 1.0,
2088                           p_txn_rec                => l_csi_transaction_rec,
2089                           p_ext_attrib_tbl         => l_csi_ext_attrib_values_tbl1,
2090                           x_return_status          => l_return_status,
2091                           x_msg_count              => l_msg_count,
2092                           x_msg_data               => l_msg_data);
2093 
2094     IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
2095       RAISE FND_API.G_EXC_ERROR;
2096     ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
2097       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2098     END IF;
2099   END IF;
2100 
2101   --For UC user, UC header status change needs to be made after the operation
2102   --Not confirmed whether need to copy the record into UC header history table
2103   --after status change. Not include the copy right now. (No)
2104   IF p_prod_user_flag = 'N' THEN
2105     IF l_root_uc_status_code = 'COMPLETE' THEN
2106     --IF unit_config_status_code='INCOMPLETE' and active_uc_status_code='UNAPPROVED', this
2107     --update is only object_version_number change and not necessary.
2108       UPDATE ahl_unit_config_headers
2109          SET unit_config_status_code = 'INCOMPLETE',
2110              active_uc_status_code = 'UNAPPROVED',
2111              object_version_number = object_version_number + 1,
2112              last_update_date = SYSDATE,
2113              last_updated_by = FND_GLOBAL.user_id,
2114              last_update_login = FND_GLOBAL.login_id
2115        WHERE unit_config_header_id = l_root_uc_header_id
2116          AND object_version_number = l_root_uc_ovn;
2117       IF SQL%ROWCOUNT = 0 THEN
2118         FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
2119         FND_MSG_PUB.add;
2120         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2121       END IF;
2122     ELSIF (l_root_uc_status_code = 'INCOMPLETE' AND
2123            (l_root_active_uc_status_code IS NULL OR
2124             l_root_active_uc_status_code <> 'UNAPPROVED')) THEN
2125       UPDATE ahl_unit_config_headers
2126          SET unit_config_status_code = 'INCOMPLETE',
2127              active_uc_status_code = 'UNAPPROVED',
2128              object_version_number = object_version_number + 1,
2129              last_update_date = SYSDATE,
2130              last_updated_by = FND_GLOBAL.user_id,
2131              last_update_login = FND_GLOBAL.login_id
2132        WHERE unit_config_header_id = l_root_uc_header_id
2133          AND object_version_number = l_root_uc_ovn;
2134       IF SQL%ROWCOUNT = 0 THEN
2135         FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
2136         FND_MSG_PUB.add;
2137         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2138       END IF;
2139     ELSIF (l_root_uc_status_code NOT IN ('COMPLETE', 'INCOMPLETE', 'DRAFT')) THEN
2140     --IF unit_config_status_code='DRAFT', this update is only object_version_number change and
2141     --not necessary.
2142       UPDATE ahl_unit_config_headers
2143          SET unit_config_status_code = 'DRAFT',
2144              object_version_number = object_version_number + 1,
2145              last_update_date = SYSDATE,
2146              last_updated_by = FND_GLOBAL.user_id,
2147              last_update_login = FND_GLOBAL.login_id
2148        WHERE unit_config_header_id = l_root_uc_header_id
2149          AND object_version_number = l_root_uc_ovn;
2150       IF SQL%ROWCOUNT = 0 THEN
2151         FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
2152         FND_MSG_PUB.add;
2153         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2154       END IF;
2155     END IF;
2156   END IF; --For production user, no need to change any one of the status.
2157   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2158     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
2159                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': After normal execution',
2160                    'At the end of the procedure');
2161   END IF;
2162 
2163 
2164   /* Commented out by jaramana on April 26, 2005.
2165      Moved this to the location before calling the CSI API sinc the CSI API
2166      returns a Confirmation/Warning Message even when the status is S
2167      while changing the serial number.
2168   --Get all the error messages from the previous steps (if any) and raise the appropriate Exception
2169   l_msg_count := FND_MSG_PUB.count_msg;
2170   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2171     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
2172                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': After normal execution',
2173                    'l_msg_count='||l_msg_count||' x_return_status='||x_return_status);
2174   END IF;
2175 
2176   IF l_msg_count > 0 THEN
2177     x_msg_count := l_msg_count;
2178     RAISE FND_API.G_EXC_ERROR;
2179   END IF;
2180   */
2181 
2182   -- Perform the Commit (if requested)
2183   IF FND_API.to_boolean(p_commit) THEN
2184     COMMIT;
2185   END IF;
2186 
2187   --Count and Get messages(optional)
2188   FND_MSG_PUB.count_and_get(
2189     p_encoded  => FND_API.G_FALSE,
2190     p_count    => x_msg_count,
2191     p_data     => x_msg_data);
2192 
2193 EXCEPTION
2194   WHEN FND_API.G_EXC_ERROR THEN
2195     ROLLBACK to update_instance_attr;
2196     x_return_status := FND_API.G_RET_STS_ERROR ;
2197     FND_MSG_PUB.count_and_get(
2198       p_encoded  => FND_API.G_FALSE,
2199       p_count    => x_msg_count,
2200       p_data     => x_msg_data);
2201   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2202     ROLLBACK to update_instance_attr;
2203     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2204     FND_MSG_PUB.count_and_get(
2205       p_encoded  => FND_API.G_FALSE,
2206       p_count    => x_msg_count,
2207       p_data     => x_msg_data);
2208   WHEN OTHERS THEN
2209     ROLLBACK to update_instance_attr;
2210     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
2211     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
2212     THEN
2213       FND_MSG_PUB.add_exc_msg(
2214         p_pkg_name         => G_PKG_NAME,
2215         p_procedure_name   => l_api_name,
2216         p_error_text       => SUBSTRB(SQLERRM,1,240));
2217     END IF;
2218     FND_MSG_PUB.count_and_get(
2219       p_encoded  => FND_API.G_FALSE,
2220       p_count    => x_msg_count,
2221       p_data     => x_msg_data);
2222 END;
2223 
2224 -- Define procedure install_new_instance
2225 -- This API is used to create a new instance in csi_item_instances and assign it
2226 -- to a UC node.
2227 PROCEDURE install_new_instance(
2228   p_api_version           IN NUMBER := 1.0,
2229   p_init_msg_list         IN VARCHAR2 := FND_API.G_FALSE,
2230   p_commit                IN VARCHAR2 := FND_API.G_FALSE,
2231   p_validation_level      IN NUMBER := FND_API.G_VALID_LEVEL_FULL,
2232   x_return_status         OUT NOCOPY VARCHAR2,
2233   x_msg_count             OUT NOCOPY NUMBER,
2234   x_msg_data              OUT NOCOPY VARCHAR2,
2235   p_uc_header_id          IN NUMBER,
2236   p_parent_instance_id    IN NUMBER,
2237   p_prod_user_flag        IN VARCHAR2,
2238   p_x_uc_instance_rec     IN OUT NOCOPY uc_instance_rec_type,
2239   p_x_sub_uc_rec          IN OUT NOCOPY uc_header_rec_type,
2240   x_warning_msg_tbl       OUT NOCOPY ahl_uc_validation_pub.error_tbl_type
2241 )
2242 IS
2243   l_api_name       CONSTANT   VARCHAR2(30) := 'install_new_instance';
2244   l_api_version    CONSTANT   NUMBER       := 1.0;
2245   l_return_status             VARCHAR2(1);
2246   l_msg_count                 NUMBER;
2247   l_msg_data                  VARCHAR2(2000);
2248   l_subject_id                NUMBER;
2249   l_object_id                 NUMBER;
2250   l_csi_relationship_id       NUMBER;
2251   l_object_version_number     NUMBER;
2252   l_position_reference        csi_ii_relationships.position_reference%TYPE;
2253   l_sub_mc_header_id          NUMBER := NULL;
2254   l_top_relationship_id       NUMBER := NULL;
2255   l_position_id               NUMBER := NULL;
2256   l_parent_relationship_id    NUMBER;
2257   l_root_uc_header_id         NUMBER;
2258   l_root_instance_id          NUMBER;
2259   l_root_uc_status_code       FND_LOOKUP_VALUES.lookup_code%TYPE;
2260   l_root_active_uc_status_code FND_LOOKUP_VALUES.lookup_code%TYPE;
2261   l_root_uc_ovn               NUMBER;
2262   l_root_instance_ou          NUMBER;
2263   l_new_instance_ou           NUMBER;
2264   l_uc_status_code            FND_LOOKUP_VALUES.lookup_code%TYPE;
2265   l_active_uc_status_code     FND_LOOKUP_VALUES.lookup_code%TYPE;
2266   l_dummy                     NUMBER;
2267   l_dummy_char                VARCHAR2(1);
2268   i                           NUMBER := 0;
2269   l_transaction_type_id       NUMBER;
2270   l_return_val                BOOLEAN;
2271   l_attribute_value_id        NUMBER;
2272   l_attribute_value           csi_iea_values.attribute_value%TYPE;
2273   l_attribute_id              NUMBER;
2274   l_subscript                 NUMBER DEFAULT 0;
2275   l_concatenated_segments     mtl_system_items_kfv.concatenated_segments%TYPE;
2276   l_item_assoc_id             NUMBER;
2277   l_new_instance_id           NUMBER;
2278   l_new_csi_instance_ovn      NUMBER;
2279   l_parent_uc_header_id       NUMBER;
2280   l_parent_instance_id        NUMBER;
2281   l_position_ref_meaning      fnd_lookups.meaning%TYPE;
2282   l_interchange_type_code     ahl_item_associations_b.interchange_type_code%TYPE;
2283 
2284   --Variables needed for CSI API call
2285   l_csi_instance_rec          csi_datastructures_pub.instance_rec;
2286   l_csi_party_rec             csi_datastructures_pub.party_rec;
2287   l_csi_transaction_rec       csi_datastructures_pub.transaction_rec;
2288   l_csi_relationship_rec      csi_datastructures_pub.ii_relationship_rec;
2289   l_csi_relationship_tbl      csi_datastructures_pub.ii_relationship_tbl;
2290   l_csi_party_tbl             csi_datastructures_pub.party_tbl;
2291   l_csi_account_tbl           csi_datastructures_pub.party_account_tbl;
2292   l_csi_pricing_attrib_tbl    csi_datastructures_pub.pricing_attribs_tbl;
2293   l_csi_org_assignments_tbl   csi_datastructures_pub.organization_units_tbl;
2294   l_csi_asset_assignment_tbl  csi_datastructures_pub.instance_asset_tbl;
2295   l_csi_instance_id_lst       csi_datastructures_pub.id_tbl;
2296   l_party_account_rec         csi_datastructures_pub.party_account_rec;
2297   l_csi_extend_attrib_rec     csi_datastructures_pub.extend_attrib_values_rec;
2298   l_csi_ext_attrib_values_tbl csi_datastructures_pub.extend_attrib_values_tbl;
2299 
2300   CURSOR check_uc_header IS
2301     SELECT A.unit_config_header_id,
2302            A.object_version_number,
2303            A.unit_config_status_code,
2304            A.active_uc_status_code,
2305            A.csi_item_instance_id,
2306            B.relationship_id
2307       FROM ahl_unit_config_headers A,
2308            ahl_mc_relationships B
2309      WHERE A.unit_config_header_id = p_uc_header_id
2310        AND trunc(nvl(A.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
2311        AND A.master_config_id = B.mc_header_id
2312        AND B.parent_relationship_id IS NULL;
2313   l_check_uc_header check_uc_header%ROWTYPE;
2314   CURSOR get_uc_descendants(c_instance_id NUMBER) IS
2315     SELECT relationship_id,
2316            object_version_number,
2317            object_id,
2318            subject_id,
2319            to_number(position_reference) position_id
2320       FROM csi_ii_relationships
2321 START WITH object_id = c_instance_id
2322        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
2323        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
2324 CONNECT BY object_id = PRIOR subject_id
2325        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
2326        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
2327   --Cursor to check whether the c_parent_relationship_id is the parent of
2328   --c_child_relationshp_id or c_child_relationship_id's own parent as the top node of the sub-config
2329   --can be installed in c_parent_relationship_id
2330   CURSOR check_parent_relationship(c_child_relationship_id NUMBER, c_parent_relationship_id NUMBER) IS
2331     SELECT 'X'
2332       FROM ahl_mc_relationships
2333      WHERE relationship_id = c_child_relationship_id
2334        AND (parent_relationship_id = c_parent_relationship_id OR
2335             mc_header_id IN (SELECT mc_header_id
2336                                FROM ahl_mc_config_relations
2337                               WHERE relationship_id = c_parent_relationship_id
2338                                 AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
2339                                 AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)));
2340 
2341   --Cursor to check whether c_parent_instance_id's child position c_relationship_id is empty
2342   CURSOR check_position_empty(c_parent_instance_id NUMBER, c_relationship_id NUMBER) IS
2343     SELECT subject_id
2344       FROM csi_ii_relationships
2345      WHERE object_id = c_parent_instance_id
2346        AND position_reference = to_char(c_relationship_id)
2347        AND subject_id IS NOT NULL
2348        AND relationship_type_code = 'COMPONENT-OF'
2349        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
2350        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
2351 
2352   CURSOR csi_item_instance_csr(c_csi_instance_id IN NUMBER) IS
2353     SELECT location_id,
2354            location_type_code,
2355            party_id,
2356            party_source_table,
2357            instance_party_id,
2358            csi.wip_job_id
2359       FROM csi_item_instances csi, csi_i_parties p
2360      WHERE csi.instance_id = p.instance_id
2361        AND p.relationship_type_code = 'OWNER'
2362        AND csi.instance_id = c_csi_instance_id
2363        AND trunc(SYSDATE) < trunc(nvl(csi.active_end_date, SYSDATE+1));
2364   l_uc_owner_loc_rec          csi_item_instance_csr%ROWTYPE;
2365 
2366   CURSOR csi_ip_accounts_csr(c_instance_party_id IN NUMBER) IS
2367     SELECT party_account_id
2368       FROM csi_ip_accounts
2369      WHERE relationship_type_code = 'OWNER'
2370        AND instance_party_id = c_instance_party_id
2371        AND trunc(SYSDATE) >= trunc(nvl(active_start_date, SYSDATE))
2372        AND trunc(SYSDATE) < trunc(nvl(active_end_date, SYSDATE+1));
2373 
2374   --Cursor to check the uniqueness of the sub unit
2375   CURSOR check_uc_name_unique(c_uc_name VARCHAR2) IS
2376     SELECT 'X'
2377       FROM ahl_unit_config_headers
2378      WHERE name = c_uc_name
2379        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
2380 
2381   --Cursor to check the validatiy of the mc_header_id (for sub config) according to the mc_name, mc_revision and
2382   --relationship_id (in parent MC)
2383   CURSOR get_sub_mc_header(c_mc_name VARCHAR2, c_mc_revision VARCHAR2, c_relationship_id NUMBER) IS
2384     SELECT H.mc_header_id,
2385            R.relationship_id
2386       FROM ahl_mc_headers_b H,
2387            ahl_mc_relationships R
2388      WHERE H.mc_header_id = R.mc_header_id
2389        AND R.parent_relationship_id IS NULL
2390        AND trunc(nvl(R.active_start_date, SYSDATE)) <= trunc(SYSDATE)
2391        AND trunc(nvl(R.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
2392        AND H.name = c_mc_name
2393        AND H.revision = c_mc_revision
2394        AND H.mc_header_id IN (SELECT mc_header_id
2395                               FROM ahl_mc_config_relations
2396                              WHERE relationship_id = c_relationship_id
2397                                AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
2398                                AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE));
2399 
2400   CURSOR get_interchange_type (c_instance_id NUMBER, c_relationship_id NUMBER) IS
2401     SELECT i.interchange_type_code
2402       FROM csi_item_instances c,
2403            ahl_item_associations_b i,
2404            ahl_mc_relationships m
2405      WHERE m.relationship_id = c_relationship_id
2406        AND c.instance_id = c_instance_id
2407        AND m.item_group_id = i.item_group_id
2408        AND c.inventory_item_id = i.inventory_item_id
2409        AND c.inv_master_organization_id = i.inventory_org_id
2410        AND (c.inventory_revision IS NULL OR
2411             i.revision is NULL OR
2412             (c.inventory_revision IS NOT NULL AND
2413              i.revision IS NOT NULL AND
2414              c.inventory_revision = i.revision));
2415    --Added this last condition due to the impact of bug fixing 4102152, added by Jerry on 01/05/2005
2416    --Need to confirm which one is more accurate here to use c.inv_master_organization_id or
2417    --c.last_vld_organization_id
2418 
2419 BEGIN
2420   --Initialize API return status to success
2421   x_return_status := FND_API.G_RET_STS_SUCCESS;
2422 
2423   -- Standard Start of API savepoint
2424   SAVEPOINT install_new_instance;
2425 
2426   --Standard call to check for call compatibility.
2427   IF NOT FND_API.compatible_api_call(
2428     l_api_version,
2429     p_api_version,
2430     l_api_name,
2431     G_PKG_NAME)
2432   THEN
2433     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2434   END IF;
2435 
2436   --Initialize message list if p_init_msg_list is set to TRUE.
2437   IF FND_API.to_boolean( p_init_msg_list ) THEN
2438     FND_MSG_PUB.initialize;
2439   END IF;
2440 
2441   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2442     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
2443                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Begin API',
2444                    'At the start of the procedure');
2445   END IF;
2446 
2447   --Validate input parameters p_prod_user_flag
2448   IF upper(p_prod_user_flag) <> 'Y' AND upper(p_prod_user_flag) <> 'N' THEN
2449     FND_MESSAGE.set_name('AHL','AHL_UC_API_PARAMETER_INVALID');
2450     FND_MESSAGE.set_token('NAME', 'prod_user_flag');
2451     FND_MESSAGE.set_token('VALUE', p_prod_user_flag);
2452     FND_MSG_PUB.add;
2453     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2454   END IF;
2455   --Validate input parameter p_uc_header_id, its two statuses
2456   OPEN check_uc_header;
2457   FETCH check_uc_header INTO l_check_uc_header;
2458   IF check_uc_header%NOTFOUND THEN
2459     FND_MESSAGE.set_name('AHL','AHL_UC_API_PARAMETER_INVALID');
2460     FND_MESSAGE.set_token('NAME', 'uc_header_id');
2461     FND_MESSAGE.set_token('VALUE', p_uc_header_id);
2462     FND_MSG_PUB.add;
2463     CLOSE check_uc_header;
2464     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2465   ELSE
2466 
2467     -- ACL :: Changes for R12
2468     IF (ahl_util_uc_pkg.IS_UNIT_QUARANTINED(p_unit_header_id => p_uc_header_id , p_instance_id => null) = FND_API.G_TRUE) THEN
2469       FND_MESSAGE.set_name( 'AHL','AHL_UC_INVALID_Q_ACTION' );
2470       FND_MSG_PUB.add;
2471       CLOSE check_uc_header;
2472       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2473     END IF;
2474 
2475     ahl_util_uc_pkg.get_root_uc_attr(p_uc_header_id,
2476                                      l_root_uc_header_id,
2477                                      l_root_instance_id,
2478                                      l_root_uc_status_code,
2479                                      l_root_active_uc_status_code,
2480                                      l_root_uc_ovn);
2481     IF (p_prod_user_flag = 'Y' AND --For production user, no need to confirm either one of the statuses is not APPROVAL_PENDING
2482         l_root_uc_status_code NOT IN ('COMPLETE', 'INCOMPLETE')) THEN
2483       FND_MESSAGE.set_name( 'AHL','AHL_UC_STATUS_NOT_ACTIVE' );
2484       FND_MSG_PUB.add;
2485       CLOSE check_uc_header;
2486       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2487     ELSIF (p_prod_user_flag = 'N' AND
2488            (l_root_uc_status_code = 'APPROVAL_PENDING' OR
2489             l_root_active_uc_status_code = 'APPROVAL_PENDING')) THEN
2490       FND_MESSAGE.set_name( 'AHL','AHL_UC_STATUS_PENDING' );
2491       FND_MESSAGE.set_token( 'UC_HEADER_ID', l_root_uc_header_id);
2492       FND_MSG_PUB.add;
2493       CLOSE check_uc_header;
2494       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2495     ELSE
2496       CLOSE check_uc_header;
2497     END IF;
2498   END IF;
2499 
2500   --Get the operating unit of the root instance.
2501   l_root_instance_ou := get_operating_unit(l_root_instance_id);
2502 
2503   --Make sure p_parent_instance_id is installed in the UC
2504   IF p_parent_instance_id = l_check_uc_header.csi_item_instance_id THEN
2505     --The parent instance is the root node
2506     l_parent_relationship_id := l_check_uc_header.relationship_id;
2507   ELSE
2508     FOR l_get_uc_descendant IN get_uc_descendants(l_check_uc_header.csi_item_instance_id) LOOP
2509       l_csi_relationship_id := l_get_uc_descendant.relationship_id;
2510       l_object_version_number := l_get_uc_descendant.object_version_number;
2511       l_object_id := l_get_uc_descendant.object_id;
2512       l_subject_id := l_get_uc_descendant.subject_id;
2513       l_parent_relationship_id := l_get_uc_descendant.position_id;
2514       EXIT WHEN l_subject_id = p_parent_instance_id;
2515     END LOOP;
2516     --Ensure the instance is installed in this UC and not an extra node
2517     IF (l_subject_id <> p_parent_instance_id OR
2518         p_parent_instance_id IS NULL OR
2519         l_subject_id IS NULL OR
2520         l_parent_relationship_id IS NULL) THEN
2521       --Do we allow an extra node's attributes to be changed?
2522       FND_MESSAGE.set_name( 'AHL','AHL_UC_INSTANCE_NOT_IN_UC' );
2523       FND_MESSAGE.set_token('INSTANCE', p_parent_instance_id);
2524       FND_MSG_PUB.add;
2525       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2526     END IF;
2527   END IF;
2528 
2529   --Then validate p_x_uc_instance_rec.relationship_id can be child of l_parent_relationship_id
2530   OPEN check_parent_relationship(p_x_uc_instance_rec.relationship_id, l_parent_relationship_id);
2531   FETCH check_parent_relationship INTO l_dummy_char;
2532   IF check_parent_relationship%NOTFOUND THEN
2533     FND_MESSAGE.set_name( 'AHL','AHL_UC_POSITION_MISMATCH' );
2534     FND_MESSAGE.set_token('CHILD', p_x_uc_instance_rec.relationship_id);
2535     FND_MESSAGE.set_token('PARENT', l_parent_relationship_id);
2536     FND_MSG_PUB.add;
2537     CLOSE check_parent_relationship;
2538     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2539   ELSE
2540     CLOSE check_parent_relationship;
2541   END IF;
2542 
2543   --Make sure position p_x_uc_instance_rec.relationship_id is empty
2544   OPEN check_position_empty(p_parent_instance_id, p_x_uc_instance_rec.relationship_id);
2545   FETCH check_position_empty INTO l_dummy;
2546   IF check_position_empty%FOUND THEN
2547     FND_MESSAGE.set_name( 'AHL','AHL_UC_POSITION_INSTALLED' );
2548     FND_MESSAGE.set_token('POSITION', p_x_uc_instance_rec.relationship_id);
2549     FND_MSG_PUB.add;
2550     CLOSE check_position_empty;
2551     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2552   ELSE
2553     CLOSE check_position_empty;
2554   END IF;
2555   --When creating the new instances, the "From Inventory" Serial Tag should not be used anymore.
2556   --mpothuku added on 13-Jul-2007 to fix the Bug 4337259
2557   IF(p_x_uc_instance_rec.sn_tag_code is not null AND p_x_uc_instance_rec.sn_tag_code = 'INVENTORY') THEN
2558     FND_MESSAGE.set_name( 'AHL','AHL_UC_SER_TG_CR_INVEN' );
2559     FND_MSG_PUB.add;
2560   END IF;
2561   --mpothuku End
2562 
2563   --Check Error Message stack.
2564   l_msg_count := FND_MSG_PUB.count_msg;
2565   IF l_msg_count > 0 THEN
2566     RAISE  FND_API.G_EXC_ERROR;
2567   END IF;
2568 
2569   --Validate Inventory details.
2570   validate_uc_invdetails (p_x_uc_instance_rec.inventory_item_id,
2571                           p_x_uc_instance_rec.inventory_org_id,
2572                           p_x_uc_instance_rec.serial_number,
2573                           p_x_uc_instance_rec.sn_tag_code,
2574                           p_x_uc_instance_rec.quantity,
2575                           p_x_uc_instance_rec.uom_code,
2576                           p_x_uc_instance_rec.revision,
2577                           p_x_uc_instance_rec.lot_number,
2578                           NULL,
2579                           l_concatenated_segments);
2580 
2581   --Check Error Message stack.
2582   l_msg_count := FND_MSG_PUB.count_msg;
2583   IF l_msg_count > 0 THEN
2584     RAISE  FND_API.G_EXC_ERROR;
2585   END IF;
2586 
2587   --Check all sub mc_name, mc_revision and uc_name are NULL or NOT NULL
2588   IF (p_x_sub_uc_rec.mc_name IS NOT NULL AND (p_x_sub_uc_rec.mc_revision IS NULL OR
2589                                                 p_x_sub_uc_rec.uc_name IS NULL) OR
2590       p_x_sub_uc_rec.mc_revision IS NOT NULL AND (p_x_sub_uc_rec.mc_name IS NULL OR
2591                                                 p_x_sub_uc_rec.uc_name IS NULL) OR
2592       p_x_sub_uc_rec.uc_name IS NOT NULL AND (p_x_sub_uc_rec.mc_revision IS NULL OR
2593                                                 p_x_sub_uc_rec.mc_name IS NULL))
2594   THEN
2595     FND_MESSAGE.set_name('AHL','AHL_UC_SUB_UNIT_INFO_MISSING');
2596     FND_MSG_PUB.add;
2597     RAISE FND_API.G_EXC_ERROR;
2598   END IF;
2599 
2600   --Check the sub unit name is unique
2601   IF p_x_sub_uc_rec.uc_name IS NOT NULL THEN
2602     OPEN check_uc_name_unique(p_x_sub_uc_rec.uc_name);
2603     FETCH check_uc_name_unique INTO l_dummy_char;
2604     IF check_uc_name_unique%FOUND THEN
2605       FND_MESSAGE.set_name('AHL','AHL_UC_NAME_DUPLICATE');
2606       FND_MESSAGE.set_token('NAME', p_x_sub_uc_rec.uc_name);
2607       FND_MSG_PUB.add;
2608       CLOSE check_uc_name_unique;
2609       RAISE FND_API.G_EXC_ERROR;
2610     ELSE
2611       CLOSE check_uc_name_unique;
2612     END IF;
2613   END IF;
2614 
2615   --Derive mc_header_id from mc_name and mc_revision
2616   IF p_x_sub_uc_rec.mc_name IS NOT NULL THEN
2617     OPEN get_sub_mc_header(p_x_sub_uc_rec.mc_name,
2618                            p_x_sub_uc_rec.mc_revision,
2619                            p_x_uc_instance_rec.relationship_id);
2620     FETCH get_sub_mc_header INTO l_sub_mc_header_id, l_top_relationship_id;
2621     IF get_sub_mc_header%NOTFOUND THEN
2622       FND_MESSAGE.set_name('AHL','AHL_UC_SUB_MC_INVALID');
2623       FND_MESSAGE.set_token('NAME', p_x_sub_uc_rec.mc_name);
2624       FND_MESSAGE.set_token('REVISION', p_x_sub_uc_rec.mc_revision);
2625       FND_MSG_PUB.add;
2626       CLOSE get_sub_mc_header;
2627       RAISE FND_API.G_EXC_ERROR;
2628     ELSE
2629       CLOSE get_sub_mc_header;
2630     END IF;
2631   END IF;
2632 
2633   IF (l_top_relationship_id IS NOT NULL) THEN
2634     l_position_id := l_top_relationship_id;
2635   ELSE
2636     l_position_id := p_x_uc_instance_rec.relationship_id;
2637   END IF;
2638 
2639   --Validate whether an item can be installed into a position, the position refers to the
2640   --top node position in the sub UC if sub UC information is provided otherwise it refers
2641   --to the position from Parent UC.
2642   AHL_UTIL_UC_PKG.validate_for_position(l_position_id,
2643                                         p_x_uc_instance_rec.inventory_Item_id,
2644                                         p_x_uc_instance_rec.inventory_Org_id,
2645                                         p_x_uc_instance_rec.quantity,
2646                                         p_x_uc_instance_rec.revision,
2647                                         p_x_uc_instance_rec.uom_code,
2648                                         NULL,
2649                                         -- SATHAPLI::FP OGMA Issue# 105 - Non-Serialized Item Maintenance, 05-Dec-2007
2650                                         -- Pass 'N' for p_ignore_quant_vald.
2651                                         'N',
2652                                         l_item_assoc_id);
2653 
2654   --Check Error Message stack.
2655   l_msg_count := FND_MSG_PUB.count_msg;
2656   IF l_msg_count > 0 THEN
2657     RAISE  FND_API.G_EXC_ERROR;
2658   END IF;
2659 
2660   --Validate manufacturing date.
2661   IF (p_x_uc_instance_rec.mfg_date IS NOT NULL AND
2662       p_x_uc_instance_rec.mfg_date <> FND_API.G_MISS_DATE) THEN
2663     IF (p_x_uc_instance_rec.mfg_date > SYSDATE) THEN
2664       FND_MESSAGE.set_name('AHL','AHL_UC_MFGDATE_INVALID');
2665       FND_MESSAGE.set_token('DATE',p_x_uc_instance_rec.mfg_date);
2666       FND_MESSAGE.set_token('INV_ITEM',l_concatenated_segments);
2667       FND_MSG_PUB.add;
2668       --dbms_output.put_line('Mfg date invalid.');
2669     END IF;
2670   END IF;
2671 
2672   --Validate installation date.
2673   --Keep the installation date validation only for production user (04/21/2004)
2674   IF (p_x_uc_instance_rec.install_date IS NOT NULL AND
2675       p_x_uc_instance_rec.install_date <> FND_API.G_MISS_DATE) THEN
2676     IF (p_prod_user_flag = 'Y' AND trunc(p_x_uc_instance_rec.install_date) > trunc(SYSDATE)) THEN
2677       FND_MESSAGE.set_name('AHL','AHL_UC_INSTDATE_INVALID');
2678       FND_MESSAGE.set_token('DATE',p_x_uc_instance_rec.install_date);
2679       FND_MESSAGE.set_token('POSN_REF',p_x_uc_instance_rec.relationship_id);
2680       FND_MSG_PUB.add;
2681       --dbms_output.put_line('Installation date invalid.');
2682     END IF;
2683   END IF;
2684 
2685   -- Build CSI records and call API.
2686   -- First get unit config location and owner details.
2687   OPEN csi_item_instance_csr(p_parent_instance_id);
2688   FETCH csi_item_instance_csr INTO l_uc_owner_loc_rec;
2689   IF (csi_item_instance_csr%NOTFOUND) THEN
2690     CLOSE csi_item_instance_csr;
2691     FND_MESSAGE.set_name('AHL','AHL_UC_CSII_INVALID');
2692     FND_MESSAGE.set_token('CSII',p_parent_instance_id);
2693     FND_MESSAGE.Set_Token('POSN_REF',p_x_uc_instance_rec.relationship_id);
2694     FND_MSG_PUB.add;
2695     --dbms_output.put_line('Top node item instance does not exist.');
2696     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2697   END IF;
2698   CLOSE csi_item_instance_csr;
2699 
2700   --Set csi instance record
2701   l_csi_instance_rec.inventory_item_id := p_x_uc_instance_rec.inventory_item_id;
2702   l_csi_instance_rec.vld_organization_id := p_x_uc_instance_rec.inventory_org_id;
2703   l_csi_instance_rec.quantity := p_x_uc_instance_rec.quantity;
2704   l_csi_instance_rec.unit_of_measure := p_x_uc_instance_rec.uom_code;
2705   l_csi_instance_rec.install_date := p_x_uc_instance_rec.install_date;
2706   l_csi_instance_rec.location_id := l_uc_owner_loc_rec.location_id;
2707   l_csi_instance_rec.location_type_code := l_uc_owner_loc_rec.location_type_code;
2708 
2709   --In case item is in WIP; copy the parent WIP job ID to the component.
2710   l_csi_instance_rec.wip_job_id := l_uc_owner_loc_rec.wip_job_id;
2711 --  IF (p_x_uc_instance_rec.sn_tag_code = 'INVENTORY') THEN
2712 --    l_csi_instance_rec.mfg_serial_number_flag := 'Y';
2713 --  ELSE
2714 --  Changed by jaramana on April 26, 2005,
2715 --  As per request from IB team (Briestly Manesh), always setting to N
2716     l_csi_instance_rec.mfg_serial_number_flag := 'N';
2717 --  END IF;
2718 
2719   IF (p_x_uc_instance_rec.serial_number IS NOT NULL AND
2720       p_x_uc_instance_rec.serial_number <> FND_API.G_MISS_CHAR)  THEN
2721     l_csi_instance_rec.serial_number := p_x_uc_instance_rec.serial_number;
2722   END IF;
2723 
2724   IF (p_x_uc_instance_rec.lot_number IS NOT NULL AND
2725       p_x_uc_instance_rec.lot_number <> FND_API.G_MISS_CHAR) THEN
2726     l_csi_instance_rec.lot_number := p_x_uc_instance_rec.lot_number;
2727   END IF;
2728 
2729   IF (p_x_uc_instance_rec.revision IS NOT NULL AND
2730       p_x_uc_instance_rec.revision <> FND_API.G_MISS_CHAR) THEN
2731     l_csi_instance_rec.inventory_revision := p_x_uc_instance_rec.revision;
2732   END IF;
2733 
2734   --l_csi_instance_rec.instance_usage_code := 'IN_SERVICE';
2735   l_csi_instance_rec.instance_usage_code := NULL;
2736 
2737   -- SATHAPLI::FP ER 6453212, 10-Nov-2008
2738   -- populate the flexfield data in the CSI record
2739   IF (p_x_uc_instance_rec.context IS NOT NULL AND
2740       p_x_uc_instance_rec.context <> FND_API.G_MISS_CHAR) THEN
2741     l_csi_instance_rec.context := p_x_uc_instance_rec.context;
2742   END IF;
2743 
2744   IF (p_x_uc_instance_rec.attribute1 IS NOT NULL AND
2745       p_x_uc_instance_rec.attribute1 <> FND_API.G_MISS_CHAR) THEN
2746     l_csi_instance_rec.attribute1 := p_x_uc_instance_rec.attribute1;
2747   END IF;
2748 
2749   IF (p_x_uc_instance_rec.attribute2 IS NOT NULL AND
2750       p_x_uc_instance_rec.attribute2 <> FND_API.G_MISS_CHAR) THEN
2751     l_csi_instance_rec.attribute2 := p_x_uc_instance_rec.attribute2;
2752   END IF;
2753 
2754   IF (p_x_uc_instance_rec.attribute3 IS NOT NULL AND
2755       p_x_uc_instance_rec.attribute3 <> FND_API.G_MISS_CHAR) THEN
2756     l_csi_instance_rec.attribute3 := p_x_uc_instance_rec.attribute3;
2757   END IF;
2758 
2759   IF (p_x_uc_instance_rec.attribute4 IS NOT NULL AND
2760       p_x_uc_instance_rec.attribute4 <> FND_API.G_MISS_CHAR) THEN
2761     l_csi_instance_rec.attribute4 := p_x_uc_instance_rec.attribute4;
2762   END IF;
2763 
2764   IF (p_x_uc_instance_rec.attribute5 IS NOT NULL AND
2765       p_x_uc_instance_rec.attribute5 <> FND_API.G_MISS_CHAR) THEN
2766     l_csi_instance_rec.attribute5 := p_x_uc_instance_rec.attribute5;
2767   END IF;
2768 
2769   IF (p_x_uc_instance_rec.attribute6 IS NOT NULL AND
2770       p_x_uc_instance_rec.attribute6 <> FND_API.G_MISS_CHAR) THEN
2771     l_csi_instance_rec.attribute6 := p_x_uc_instance_rec.attribute6;
2772   END IF;
2773 
2774   IF (p_x_uc_instance_rec.attribute7 IS NOT NULL AND
2775       p_x_uc_instance_rec.attribute7 <> FND_API.G_MISS_CHAR) THEN
2776     l_csi_instance_rec.attribute7 := p_x_uc_instance_rec.attribute7;
2777   END IF;
2778 
2779   IF (p_x_uc_instance_rec.attribute8 IS NOT NULL AND
2780       p_x_uc_instance_rec.attribute8 <> FND_API.G_MISS_CHAR) THEN
2781     l_csi_instance_rec.attribute8 := p_x_uc_instance_rec.attribute8;
2782   END IF;
2783 
2784   IF (p_x_uc_instance_rec.attribute9 IS NOT NULL AND
2785       p_x_uc_instance_rec.attribute9 <> FND_API.G_MISS_CHAR) THEN
2786     l_csi_instance_rec.attribute9 := p_x_uc_instance_rec.attribute9;
2787   END IF;
2788 
2789   IF (p_x_uc_instance_rec.attribute10 IS NOT NULL AND
2790       p_x_uc_instance_rec.attribute10 <> FND_API.G_MISS_CHAR) THEN
2791     l_csi_instance_rec.attribute10 := p_x_uc_instance_rec.attribute10;
2792   END IF;
2793 
2794   IF (p_x_uc_instance_rec.attribute11 IS NOT NULL AND
2795       p_x_uc_instance_rec.attribute11 <> FND_API.G_MISS_CHAR) THEN
2796     l_csi_instance_rec.attribute11 := p_x_uc_instance_rec.attribute11;
2797   END IF;
2798 
2799   IF (p_x_uc_instance_rec.attribute12 IS NOT NULL AND
2800       p_x_uc_instance_rec.attribute12 <> FND_API.G_MISS_CHAR) THEN
2801     l_csi_instance_rec.attribute12 := p_x_uc_instance_rec.attribute12;
2802   END IF;
2803 
2804   IF (p_x_uc_instance_rec.attribute13 IS NOT NULL AND
2805       p_x_uc_instance_rec.attribute13 <> FND_API.G_MISS_CHAR) THEN
2806     l_csi_instance_rec.attribute13 := p_x_uc_instance_rec.attribute13;
2807   END IF;
2808 
2809   IF (p_x_uc_instance_rec.attribute14 IS NOT NULL AND
2810       p_x_uc_instance_rec.attribute14 <> FND_API.G_MISS_CHAR) THEN
2811     l_csi_instance_rec.attribute14 := p_x_uc_instance_rec.attribute14;
2812   END IF;
2813 
2814   IF (p_x_uc_instance_rec.attribute15 IS NOT NULL AND
2815       p_x_uc_instance_rec.attribute15 <> FND_API.G_MISS_CHAR) THEN
2816     l_csi_instance_rec.attribute15 := p_x_uc_instance_rec.attribute15;
2817   END IF;
2818 
2819   IF (p_x_uc_instance_rec.attribute16 IS NOT NULL AND
2820       p_x_uc_instance_rec.attribute16 <> FND_API.G_MISS_CHAR) THEN
2821     l_csi_instance_rec.attribute16 := p_x_uc_instance_rec.attribute16;
2822   END IF;
2823 
2824   IF (p_x_uc_instance_rec.attribute17 IS NOT NULL AND
2825       p_x_uc_instance_rec.attribute17 <> FND_API.G_MISS_CHAR) THEN
2826     l_csi_instance_rec.attribute17 := p_x_uc_instance_rec.attribute17;
2827   END IF;
2828 
2829   IF (p_x_uc_instance_rec.attribute18 IS NOT NULL AND
2830       p_x_uc_instance_rec.attribute18 <> FND_API.G_MISS_CHAR) THEN
2831     l_csi_instance_rec.attribute18 := p_x_uc_instance_rec.attribute18;
2832   END IF;
2833 
2834   IF (p_x_uc_instance_rec.attribute19 IS NOT NULL AND
2835       p_x_uc_instance_rec.attribute19 <> FND_API.G_MISS_CHAR) THEN
2836     l_csi_instance_rec.attribute19 := p_x_uc_instance_rec.attribute19;
2837   END IF;
2838 
2839   IF (p_x_uc_instance_rec.attribute20 IS NOT NULL AND
2840       p_x_uc_instance_rec.attribute20 <> FND_API.G_MISS_CHAR) THEN
2841     l_csi_instance_rec.attribute20 := p_x_uc_instance_rec.attribute20;
2842   END IF;
2843 
2844   IF (p_x_uc_instance_rec.attribute21 IS NOT NULL AND
2845       p_x_uc_instance_rec.attribute21 <> FND_API.G_MISS_CHAR) THEN
2846     l_csi_instance_rec.attribute21 := p_x_uc_instance_rec.attribute21;
2847   END IF;
2848 
2849   IF (p_x_uc_instance_rec.attribute22 IS NOT NULL AND
2850       p_x_uc_instance_rec.attribute22 <> FND_API.G_MISS_CHAR) THEN
2851     l_csi_instance_rec.attribute22 := p_x_uc_instance_rec.attribute22;
2852   END IF;
2853 
2854   IF (p_x_uc_instance_rec.attribute23 IS NOT NULL AND
2855       p_x_uc_instance_rec.attribute23 <> FND_API.G_MISS_CHAR) THEN
2856     l_csi_instance_rec.attribute23 := p_x_uc_instance_rec.attribute23;
2857   END IF;
2858 
2859   IF (p_x_uc_instance_rec.attribute24 IS NOT NULL AND
2860       p_x_uc_instance_rec.attribute24 <> FND_API.G_MISS_CHAR) THEN
2861     l_csi_instance_rec.attribute24 := p_x_uc_instance_rec.attribute24;
2862   END IF;
2863 
2864   IF (p_x_uc_instance_rec.attribute25 IS NOT NULL AND
2865       p_x_uc_instance_rec.attribute25 <> FND_API.G_MISS_CHAR) THEN
2866     l_csi_instance_rec.attribute25 := p_x_uc_instance_rec.attribute25;
2867   END IF;
2868 
2869   IF (p_x_uc_instance_rec.attribute26 IS NOT NULL AND
2870       p_x_uc_instance_rec.attribute26 <> FND_API.G_MISS_CHAR) THEN
2871     l_csi_instance_rec.attribute26 := p_x_uc_instance_rec.attribute26;
2872   END IF;
2873 
2874   IF (p_x_uc_instance_rec.attribute27 IS NOT NULL AND
2875       p_x_uc_instance_rec.attribute27 <> FND_API.G_MISS_CHAR) THEN
2876     l_csi_instance_rec.attribute27 := p_x_uc_instance_rec.attribute27;
2877   END IF;
2878 
2879   IF (p_x_uc_instance_rec.attribute28 IS NOT NULL AND
2880       p_x_uc_instance_rec.attribute28 <> FND_API.G_MISS_CHAR) THEN
2881     l_csi_instance_rec.attribute28 := p_x_uc_instance_rec.attribute28;
2882   END IF;
2883 
2884   IF (p_x_uc_instance_rec.attribute29 IS NOT NULL AND
2885       p_x_uc_instance_rec.attribute29 <> FND_API.G_MISS_CHAR) THEN
2886     l_csi_instance_rec.attribute29 := p_x_uc_instance_rec.attribute29;
2887   END IF;
2888 
2889   IF (p_x_uc_instance_rec.attribute30 IS NOT NULL AND
2890       p_x_uc_instance_rec.attribute30 <> FND_API.G_MISS_CHAR) THEN
2891     l_csi_instance_rec.attribute30 := p_x_uc_instance_rec.attribute30;
2892   END IF;
2893 
2894   --Build csi extended attribs.
2895   IF (p_x_uc_instance_rec.mfg_date IS NOT NULL AND
2896       p_x_uc_instance_rec.mfg_date <> FND_API.G_MISS_DATE) THEN
2897     AHL_UTIL_UC_PKG.getcsi_attribute_id('AHL_MFG_DATE',l_attribute_id, l_return_val);
2898     IF NOT(l_return_val) THEN
2899       FND_MESSAGE.set_name('AHL','AHL_UC_ATTRIB_CODE_MISSING');
2900       FND_MESSAGE.set_token('CODE', 'AHL_MFG_DATE');
2901       FND_MSG_PUB.add;
2902       --dbms_output.put_line('Attribute code for AHL_MFG_DATE not found');
2903     ELSE
2904       l_csi_extend_attrib_rec.attribute_id := l_attribute_id;
2905       l_csi_extend_attrib_rec.attribute_value := to_char(p_x_uc_instance_rec.mfg_date, 'DD/MM/YYYY');
2906       l_subscript := l_subscript + 1;
2907       l_csi_ext_attrib_values_tbl(l_subscript) := l_csi_extend_attrib_rec;
2908     END IF;
2909   END IF;
2910 
2911   IF (p_x_uc_instance_rec.serial_number IS NOT NULL AND
2912       p_x_uc_instance_rec.serial_number <> FND_API.G_MISS_CHAR) THEN
2913     AHL_UTIL_UC_PKG.getcsi_attribute_id('AHL_TEMP_SERIAL_NUM',l_attribute_id, l_return_val);
2914 
2915     IF NOT(l_return_val) THEN
2916       FND_MESSAGE.set_name('AHL','AHL_UC_ATTRIB_CODE_MISSING');
2917       FND_MESSAGE.set_token('CODE', 'AHL_TEMP_SERIAL_NUM');
2918       FND_MSG_PUB.add;
2919       --dbms_output.put_line('Attribute code for TEMP_SERIAL_NUM not found');
2920     ELSE
2921       l_csi_extend_attrib_rec.attribute_id := l_attribute_id;
2922       l_csi_extend_attrib_rec.attribute_value := p_x_uc_instance_rec.sn_tag_code;
2923       l_csi_ext_attrib_values_tbl(l_subscript+1) := l_csi_extend_attrib_rec;
2924     END IF;
2925   END IF;
2926 
2927   --Build CSI party record.
2928   l_csi_party_rec.party_id := l_uc_owner_loc_rec.party_id;
2929   l_csi_party_rec.relationship_type_code := 'OWNER';
2930   l_csi_party_rec.party_source_table := l_uc_owner_loc_rec.party_source_table;
2931   l_csi_party_rec.contact_flag := 'N';
2932   l_csi_party_tbl(1) := l_csi_party_rec;
2933 
2934   --dbms_output.put_line('before build accounts:...');
2935   --Build CSI accounts table.
2936   FOR party_ip_acct IN csi_ip_accounts_csr(l_uc_owner_loc_rec.instance_party_id)
2937   LOOP
2938     l_party_account_rec.party_account_id := party_ip_acct.party_account_id;
2939     l_party_account_rec.relationship_type_code := 'OWNER';
2940     l_party_account_rec.parent_tbl_index := 1;
2941     i := i + 1;
2942     l_csi_account_tbl(i) := l_party_account_rec;
2943   END LOOP;
2944 
2945   --Build CSI transaction record, first get transaction_type_id
2946   AHL_Util_UC_Pkg.getcsi_transaction_id('UC_CREATE',l_transaction_type_id, l_return_val);
2947 
2948   IF NOT(l_return_val) THEN
2949     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2950   END IF;
2951 
2952   l_csi_transaction_rec.source_transaction_date := SYSDATE;
2953   l_csi_transaction_rec.transaction_type_id := l_transaction_type_id;
2954 
2955   --Check Error Message stack.
2956   l_msg_count := FND_MSG_PUB.count_msg;
2957   IF l_msg_count > 0 THEN
2958     RAISE  FND_API.G_EXC_ERROR;
2959   END IF;
2960 
2961    --Call CSI API to create instance
2962   CSI_ITEM_INSTANCE_PUB.create_item_instance(
2963                        p_api_version            => 1.0,
2964                        p_instance_rec           => l_csi_instance_rec,
2965                        p_txn_rec                => l_csi_transaction_rec,
2966                        p_ext_attrib_values_tbl  => l_csi_ext_attrib_values_tbl,
2967                        p_party_tbl              => l_csi_party_tbl,
2968                        p_account_tbl            => l_csi_account_tbl,
2969                        p_pricing_attrib_tbl     => l_csi_pricing_attrib_tbl,
2970                        p_org_assignments_tbl    => l_csi_org_assignments_tbl,
2971                        p_asset_assignment_tbl   => l_csi_asset_assignment_tbl,
2972                        x_return_status          => l_return_status,
2973                        x_msg_count              => l_msg_count,
2974                        x_msg_data               => l_msg_data);
2975 
2976   --Assign out parameters.
2977 
2978   l_new_instance_id := l_csi_instance_rec.instance_id;
2979   l_new_csi_instance_ovn := l_csi_instance_rec.object_version_number;
2980   p_x_uc_instance_rec.instance_id := l_new_instance_id;
2981   p_x_uc_instance_rec.object_version_number := l_new_csi_instance_ovn;
2982 
2983   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2984     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
2985                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': within API',
2986                    ' After calling create_item_instance and instance_id = '||l_csi_instance_rec.instance_id||
2987                    ' l_return_status ='||l_return_status||
2988                    ' p_x_uc_instance_rec.instance_id='||p_x_uc_instance_rec.instance_id);
2989   END IF;
2990 
2991   IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
2992     RAISE FND_API.G_EXC_ERROR;
2993   ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
2994     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2995   END IF;
2996 
2997   --Before installing the new instance, make sure its operating unit is exactly the same as that
2998   --of the root instance.
2999   l_new_instance_ou := get_operating_unit(l_new_instance_id);
3000   IF l_root_instance_ou IS NULL THEN
3001     FND_MESSAGE.set_name('AHL', 'AHL_UC_INSTANCE_OU_NULL');
3002     FND_MESSAGE.set_token('INSTANCE', l_root_instance_id);
3003     FND_MSG_PUB.add;
3004     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3005   ELSIF l_new_instance_ou IS NULL THEN
3006     FND_MESSAGE.set_name('AHL', 'AHL_UC_INSTANCE_OU_NULL');
3007     FND_MESSAGE.set_token('INSTANCE', l_new_instance_id);
3008     FND_MSG_PUB.add;
3009     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3010   ELSIF l_root_instance_ou <> l_new_instance_ou THEN
3011     FND_MESSAGE.set_name('AHL', 'AHL_UC_INSTANCE_OU_UNMATCH');
3012     FND_MESSAGE.set_token('INSTANCE', l_new_instance_id);
3013     FND_MESSAGE.set_token('ROOT_INSTANCE', l_root_instance_id);
3014     FND_MSG_PUB.add;
3015     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3016   END IF;
3017 
3018   --Building csi_ii_relationship record should be after create_uc_header because create_uc_header
3019   --will validate the newly created instance, and this validation ensures that the instance is available
3020   --that is not in table csi_ii_realtionships and ahl_unit_config_headers
3021   --Build CSI relationships table
3022   l_csi_relationship_rec.relationship_type_code := 'COMPONENT-OF';
3023   l_csi_relationship_rec.object_id := p_parent_instance_id;
3024   l_csi_relationship_rec.position_reference := to_number(p_x_uc_instance_rec.relationship_id);
3025   l_csi_relationship_rec.subject_id := l_new_instance_id;
3026   l_csi_relationship_tbl(1) := l_csi_relationship_rec;
3027 
3028   CSI_II_RELATIONSHIPS_PUB.create_relationship(
3029                            p_api_version            => 1.0,
3030                            p_relationship_tbl       => l_csi_relationship_tbl,
3031                            p_txn_rec                => l_csi_transaction_rec,
3032                            x_return_status          => l_return_status,
3033                            x_msg_count              => l_msg_count,
3034                            x_msg_data               => l_msg_data);
3035   IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
3036     RAISE FND_API.G_EXC_ERROR;
3037   ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
3038     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3039   END IF;
3040 
3041   --Create the sub unit header record in ahl_unit_config_headers
3042   IF l_sub_mc_header_id IS NOT NULL THEN
3043     --Insert the newly added sub unit into UC headers table.
3044     p_x_sub_uc_rec.mc_header_id := l_sub_mc_header_id;
3045     p_x_sub_uc_rec.instance_id := l_new_instance_id;
3046     ahl_util_uc_pkg.get_parent_uc_header(l_new_instance_id,
3047                                          l_parent_uc_header_id,
3048                                          l_parent_instance_id);
3049     p_x_sub_uc_rec.parent_uc_header_id := l_parent_uc_header_id;
3050     --p_x_sub_uc_rec.parent_uc_header_id := p_uc_header_id;
3051     --The parameter p_uc_header_id is not necessarily the parent uc_header_id of the newly
3052     --installed instance.
3053 
3054     --dbms_output.put_line('Before calling create uc_header API:...');
3055     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3056       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
3057                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': within API',
3058                    ' p_x_sub_uc_rec.uc_header_id='||p_x_sub_uc_rec.uc_header_id||
3059                    ' p_x_sub_uc_rec.mc_header_id='||p_x_sub_uc_rec.mc_header_id||
3060                    ' p_x_sub_uc_rec.mc_name='||p_x_sub_uc_rec.mc_name||
3061                    ' p_x_sub_uc_rec.mc_revision='||p_x_sub_uc_rec.mc_revision||
3062                    ' p_x_sub_uc_rec.instance_id='||p_x_sub_uc_rec.instance_id||
3063                    ' p_x_sub_uc_rec.parent_uc_header_id='||p_x_sub_uc_rec.parent_uc_header_id);
3064     END IF;
3065     AHL_UC_UNITCONFIG_PVT.create_uc_header(
3066                             p_api_version        => 1.0,
3067                             p_init_msg_list      => FND_API.G_FALSE,
3068                             p_commit             => FND_API.G_FALSE,
3069                             p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
3070                             p_module_type        => NULL,
3071                             x_return_status      => l_return_status,
3072                             x_msg_count          => l_msg_count,
3073                             x_msg_data           => l_msg_data,
3074                             p_x_uc_header_rec    => p_x_sub_uc_rec);
3075 
3076     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3077       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
3078                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': within API',
3079                    ' p_x_sub_uc_rec.uc_header_id='||p_x_sub_uc_rec.uc_header_id||
3080                    ' p_x_sub_uc_rec.mc_header_id='||p_x_sub_uc_rec.mc_header_id||
3081                    ' p_x_sub_uc_rec.mc_name='||p_x_sub_uc_rec.mc_name||
3082                    ' p_x_sub_uc_rec.mc_revision='||p_x_sub_uc_rec.mc_revision||
3083                    ' p_x_sub_uc_rec.instance_id='||p_x_sub_uc_rec.instance_id||
3084                    ' p_x_sub_uc_rec.parent_uc_header_id='||p_x_sub_uc_rec.parent_uc_header_id);
3085     END IF;
3086     IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
3087       RAISE FND_API.G_EXC_ERROR;
3088     ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
3089       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3090     END IF;
3091     --dbms_output.put_line('After calling create uc_header API:...');
3092 
3093     --Copy the newly created UC header to history table
3094     ahl_util_uc_pkg.copy_uc_header_to_history(p_x_sub_uc_rec.uc_header_id, l_return_status);
3095 
3096     --IF history copy failed, then don't raise exception, just add the message to the message stack
3097     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3098       FND_MESSAGE.set_name('AHL', 'AHL_UC_HISTORY_COPY_FAILED');
3099       FND_MSG_PUB.add;
3100     END IF;
3101 
3102   END IF;
3103 
3104   --Call completeness check API for the newly assigned instance
3105   ahl_uc_validation_pub.validate_complete_for_pos(
3106       p_api_version         => 1.0,
3107       p_init_msg_list       => FND_API.G_FALSE,
3108       p_commit              => FND_API.G_FALSE,
3109       p_validation_level    => FND_API.G_VALID_LEVEL_FULL,
3110       x_return_status       => l_return_status,
3111       x_msg_count           => l_msg_count,
3112       x_msg_data            => l_msg_data,
3113       p_csi_instance_id     => p_x_uc_instance_rec.instance_id,
3114       x_error_tbl           => x_warning_msg_tbl);
3115   IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
3116     RAISE FND_API.G_EXC_ERROR;
3117   ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
3118     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3119   END IF;
3120 
3121   --IF 1-WAY INTERCHANGEABLE item is installed, we will display the warning message to the user
3122   --This warning message is not added to the global message stack.
3123 
3124   IF p_x_sub_uc_rec.mc_header_id IS NOT NULL THEN
3125     OPEN get_interchange_type(p_x_uc_instance_rec.instance_id, l_top_relationship_id);
3126   ELSE
3127     OPEN get_interchange_type(p_x_uc_instance_rec.instance_id, p_x_uc_instance_rec.relationship_id);
3128   END IF;
3129   FETCH get_interchange_type INTO l_interchange_type_code;
3130   IF get_interchange_type%NOTFOUND THEN
3131     FND_MESSAGE.set_name('AHL', 'AHL_UC_ITEM_INTERCHANGE_MISS');
3132     FND_MESSAGE.set_token('INSTANCE', p_x_uc_instance_rec.instance_id);
3133     FND_MSG_PUB.add;
3134     CLOSE get_interchange_type;
3135     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3136   ELSIF l_interchange_type_code = '1-WAY INTERCHANGEABLE' THEN
3137     FND_MESSAGE.set_name('AHL', 'AHL_UC_1WAY_ITEM_INSTALLED');
3138     SELECT f.meaning INTO l_position_ref_meaning
3139       FROM ahl_mc_relationships a,
3140            fnd_lookups f
3141      WHERE a.relationship_id = p_x_uc_instance_rec.relationship_id
3142        AND f.lookup_code (+) = A.position_ref_code
3143        AND f.lookup_type (+) = 'AHL_POSITION_REFERENCE' ;
3144     --Here always use p_x_uc_instance_rec.relationship_id instead of l_top_relationship_id because
3145     --UC tree UI always displays the parent leaf relationship_id instead of sub-uc top relationship_id
3146     --when it comes to the sub-uc.
3147     FND_MESSAGE.set_token('POSITION', l_position_ref_meaning);
3148     --Here the message is not added to the global message stack;
3149     IF x_warning_msg_tbl.count > 0 THEN
3150       x_warning_msg_tbl(x_warning_msg_tbl.last + 1) := FND_MESSAGE.get;
3151     ELSE
3152       x_warning_msg_tbl(0) := FND_MESSAGE.get;
3153     END IF;
3154   END IF;
3155   CLOSE get_interchange_type;
3156 
3157   --For UC user, UC header status change needs to be made after the operation
3158   --Not confirmed whether need to copy the record into UC header history table
3159   --after status change. Not include the copy right now. (No history copy)
3160   IF p_prod_user_flag = 'N' THEN
3161     IF (l_root_uc_status_code = 'COMPLETE' AND
3162         x_warning_msg_tbl.count > 0 ) THEN
3163     --IF unit_config_status_code='INCOMPLETE' and active_uc_status_code='UNAPPROVED', this
3164     --update is only object_version_number change and not necessary.
3165       UPDATE ahl_unit_config_headers
3166          SET unit_config_status_code = 'INCOMPLETE',
3167              active_uc_status_code = 'UNAPPROVED',
3168              object_version_number = object_version_number + 1,
3169              last_update_date = SYSDATE,
3170              last_updated_by = FND_GLOBAL.user_id,
3171              last_update_login = FND_GLOBAL.login_id
3172        WHERE unit_config_header_id = l_root_uc_header_id
3173          AND object_version_number = l_root_uc_ovn;
3174       IF SQL%ROWCOUNT = 0 THEN
3175         FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
3176         FND_MSG_PUB.add;
3177         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3178       END IF;
3179     ELSIF (l_root_uc_status_code IN ('COMPLETE', 'INCOMPLETE') AND
3180            (l_root_active_uc_status_code IS NULL OR
3181             l_root_active_uc_status_code <> 'UNAPPROVED')) THEN
3182     --IF unit_config_status_code='INCOMPLETE' and active_uc_status_code='UNAPPROVED', this
3183     --update is only object_version_number change and not necessary.
3184       UPDATE ahl_unit_config_headers
3185          SET active_uc_status_code = 'UNAPPROVED',
3186              object_version_number = object_version_number + 1,
3187              last_update_date = SYSDATE,
3188              last_updated_by = FND_GLOBAL.user_id,
3189              last_update_login = FND_GLOBAL.login_id
3190        WHERE unit_config_header_id = l_root_uc_header_id
3191          AND object_version_number = l_root_uc_ovn;
3192       IF SQL%ROWCOUNT = 0 THEN
3193         FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
3194         FND_MSG_PUB.add;
3195         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3196       END IF;
3197     ELSIF (l_root_uc_status_code NOT IN ('COMPLETE', 'INCOMPLETE', 'DRAFT')) THEN
3198       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3199         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
3200                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Before calling completeness check',
3201                    'p_uc_header_id='||p_uc_header_id||'l_root_uc_header_id='||l_root_uc_header_id||
3202                    'l_root_uc_ovn='||l_root_uc_ovn);
3203       END IF;
3204     --IF unit_config_status_code='DRAFT', this update is only object_version_number change and
3205     --not necessary.
3206       UPDATE ahl_unit_config_headers
3207          SET unit_config_status_code = 'DRAFT',
3208              object_version_number = object_version_number + 1,
3209              last_update_date = SYSDATE,
3210              last_updated_by = FND_GLOBAL.user_id,
3211              last_update_login = FND_GLOBAL.login_id
3212        WHERE unit_config_header_id = l_root_uc_header_id
3213          AND object_version_number = l_root_uc_ovn;
3214       IF SQL%ROWCOUNT = 0 THEN
3215         FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
3216         FND_MSG_PUB.add;
3217         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3218       END IF;
3219     END IF;
3220   ELSIF (p_prod_user_flag = 'Y' AND
3221          x_warning_msg_tbl.count > 0 AND
3222          l_root_uc_status_code = 'COMPLETE') THEN
3223     UPDATE ahl_unit_config_headers
3224        SET unit_config_status_code = 'INCOMPLETE',
3225            object_version_number = object_version_number + 1,
3226            last_update_date = SYSDATE,
3227            last_updated_by = FND_GLOBAL.user_id,
3228            last_update_login = FND_GLOBAL.login_id
3229      WHERE unit_config_header_id = l_root_uc_header_id
3230        AND object_version_number = l_root_uc_ovn;
3231     IF SQL%ROWCOUNT = 0 THEN
3232       FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
3233       FND_MSG_PUB.add;
3234       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3235     END IF;
3236   END IF;
3237 
3238   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3239     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
3240                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': After normal execution',
3241                    'At the end of the procedure');
3242   END IF;
3243 
3244   --Get all the error messages from the previous steps (if any) and raise the appropriate Exception
3245   l_msg_count := FND_MSG_PUB.count_msg;
3246   IF l_msg_count > 0 THEN
3247     x_msg_count := l_msg_count;
3248     RAISE FND_API.G_EXC_ERROR;
3249   END IF;
3250   -- Perform the Commit (if requested)
3251   IF FND_API.to_boolean(p_commit) THEN
3252     COMMIT;
3253   END IF;
3254   --Count and Get messages(optional)
3255   FND_MSG_PUB.count_and_get(
3256     p_encoded  => FND_API.G_FALSE,
3257     p_count    => x_msg_count,
3258     p_data     => x_msg_data);
3259 EXCEPTION
3260   WHEN FND_API.G_EXC_ERROR THEN
3261     ROLLBACK TO install_new_instance;
3262     x_return_status := FND_API.G_RET_STS_ERROR ;
3263     FND_MSG_PUB.count_and_get(
3264       p_encoded  => FND_API.G_FALSE,
3265       p_count    => x_msg_count,
3266       p_data     => x_msg_data);
3267   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3268     ROLLBACK TO install_new_instance;
3269     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3270     FND_MSG_PUB.count_and_get(
3271       p_encoded  => FND_API.G_FALSE,
3272       p_count    => x_msg_count,
3273       p_data     => x_msg_data);
3274   WHEN OTHERS THEN
3275     ROLLBACK TO install_new_instance;
3276     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3277     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
3278     THEN
3279       FND_MSG_PUB.add_exc_msg(
3280         p_pkg_name         => G_PKG_NAME,
3281         p_procedure_name   => l_api_name,
3282         p_error_text       => SUBSTRB(SQLERRM,1,240));
3283     END IF;
3284     FND_MSG_PUB.count_and_get(
3285       p_encoded  => FND_API.G_FALSE,
3286       p_count    => x_msg_count,
3287       p_data     => x_msg_data);
3288 END;
3289 
3290 -- Define procedure install_existing_instance
3291 -- This API is used to assign an existing instance to a UC node.
3292 PROCEDURE install_existing_instance(
3293   p_api_version           IN  NUMBER := 1.0,
3294   p_init_msg_list         IN  VARCHAR2 := FND_API.G_FALSE,
3295   p_commit                IN  VARCHAR2 := FND_API.G_FALSE,
3296   p_validation_level      IN  NUMBER := FND_API.G_VALID_LEVEL_FULL,
3297   x_return_status         OUT NOCOPY VARCHAR2,
3298   x_msg_count             OUT NOCOPY NUMBER,
3299   x_msg_data              OUT NOCOPY VARCHAR2,
3300   p_uc_header_id          IN  NUMBER,
3301   p_parent_instance_id    IN  NUMBER,
3302   p_instance_id           IN  NUMBER,
3303   p_instance_number       IN  csi_item_instances.instance_number%TYPE := NULL,
3304   p_relationship_id       IN  NUMBER,
3305   p_csi_ii_ovn            IN  NUMBER,
3306   p_prod_user_flag        IN  VARCHAR2,
3307   x_warning_msg_tbl       OUT NOCOPY ahl_uc_validation_pub.error_tbl_type)
3308 IS
3309   l_api_name       CONSTANT   VARCHAR2(30) := 'install_existing_instance';
3310   l_api_version    CONSTANT   NUMBER       := 1.0;
3311   l_return_status             VARCHAR2(1);
3312   l_msg_count                 NUMBER;
3313   l_msg_data                  VARCHAR2(2000);
3314   l_subject_id                NUMBER;
3315   l_object_id                 NUMBER;
3316   l_csi_relationship_id       NUMBER;
3317   l_object_version_number     NUMBER;
3318   l_position_reference        csi_ii_relationships.position_reference%TYPE;
3319   l_mc_header_id              NUMBER;
3320   l_sub_uc_header_id          NUMBER;
3321   l_parent_relationship_id    NUMBER;
3322   l_instance_type             VARCHAR2(1);
3323   l_dummy                     NUMBER;
3324   l_dummy_char                VARCHAR2(1);
3325   l_subunit                   BOOLEAN;
3326   i                           NUMBER := 0;
3327   l_subscript                 NUMBER DEFAULT 0;
3328   l_concatenated_segments     mtl_system_items_kfv.concatenated_segments%TYPE;
3329   l_item_assoc_id             NUMBER;
3330   l_meaning                   fnd_lookups.meaning%TYPE;
3331   l_parent_uc_header_id       NUMBER;
3332   l_parent_instance_id        NUMBER;
3333   l_root_uc_header_id         NUMBER;
3334   l_root_instance_id          NUMBER;
3335   l_root_uc_status_code       FND_LOOKUP_VALUES.lookup_code%TYPE;
3336   l_root_active_uc_status_code FND_LOOKUP_VALUES.lookup_code%TYPE;
3337   l_root_uc_ovn               NUMBER;
3338   l_root_instance_ou          NUMBER;
3339   l_instance_ou               NUMBER;
3340   l_uc_status_code            FND_LOOKUP_VALUES.lookup_code%TYPE;
3341   l_active_uc_status_code     FND_LOOKUP_VALUES.lookup_code%TYPE;
3342   l_position_ref_meaning      fnd_lookups.meaning%TYPE;
3343   l_interchange_type_code     ahl_item_associations_b.interchange_type_code%TYPE;
3344 
3345   --Variables needed for CSI API call
3346   l_csi_party_rec             csi_datastructures_pub.party_rec;
3347   l_csi_relationship_rec      csi_datastructures_pub.ii_relationship_rec;
3348   l_csi_relationship_new_rec  csi_datastructures_pub.ii_relationship_rec; -- SATHAPLI::FP ER 6504147, 18-Nov-2008
3349   l_csi_relationship_tbl      csi_datastructures_pub.ii_relationship_tbl;
3350   l_csi_party_tbl             csi_datastructures_pub.party_tbl;
3351   l_csi_account_tbl           csi_datastructures_pub.party_account_tbl;
3352   l_csi_pricing_attrib_tbl    csi_datastructures_pub.pricing_attribs_tbl;
3353   l_csi_org_assignments_tbl   csi_datastructures_pub.organization_units_tbl;
3354   l_csi_asset_assignment_tbl  csi_datastructures_pub.instance_asset_tbl;
3355   l_csi_instance_id_lst       csi_datastructures_pub.id_tbl;
3356   l_party_account_rec         csi_datastructures_pub.party_account_rec;
3357   l_serial_number             csi_item_instances.serial_number%TYPE;
3358   l_mfg_serial_number_flag    csi_item_instances.mfg_serial_number_flag%TYPE;
3359   l_serial_number_tag         csi_iea_values.attribute_value%TYPE;
3360 
3361   l_return_val                BOOLEAN;
3362   l_transaction_type_id       NUMBER;
3363   l_attribute_id              NUMBER;
3364   l_attribute_value_id        NUMBER;
3365   l_attribute_value           csi_iea_values.attribute_value%TYPE;
3366   l_csi_instance_rec          csi_datastructures_pub.instance_rec;
3367   l_csi_transaction_rec       csi_datastructures_pub.transaction_rec;
3368   l_csi_extend_attrib_rec     csi_datastructures_pub.extend_attrib_values_rec;
3369   l_csi_ext_attrib_values_tbl csi_datastructures_pub.extend_attrib_values_tbl;
3370 
3371   CURSOR check_uc_header IS
3372     SELECT A.unit_config_header_id,
3373            A.object_version_number,
3374            A.unit_config_status_code,
3375            A.active_uc_status_code,
3376            A.csi_item_instance_id,
3377            B.relationship_id
3378       FROM ahl_unit_config_headers A,
3379            ahl_mc_relationships B
3380      WHERE A.unit_config_header_id = p_uc_header_id
3381        AND trunc(nvl(A.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
3382        AND A.master_config_id = B.mc_header_id
3383        AND B.parent_relationship_id IS NULL;
3384   l_check_uc_header check_uc_header%ROWTYPE;
3385   CURSOR get_uc_descendants(c_instance_id NUMBER) IS
3386     SELECT relationship_id,
3387            object_version_number,
3388            object_id,
3389            subject_id,
3390            to_number(position_reference) position_id
3391       FROM csi_ii_relationships
3392 START WITH object_id = c_instance_id
3393        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3394        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
3395 CONNECT BY object_id = PRIOR subject_id
3396        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3397        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
3398   --Cursor to check whether the c_parent_relationship_id is the parent of
3399   --c_child_relationshp_id or c_child_relationship_id's own parent as the top node of the sub-config
3400   --can be installed in c_parent_relationship_id
3401   CURSOR check_parent_relationship(c_child_relationship_id NUMBER, c_parent_relationship_id NUMBER) IS
3402     SELECT 1
3403       FROM ahl_mc_relationships
3404      WHERE relationship_id = c_child_relationship_id
3405        AND (parent_relationship_id = c_parent_relationship_id OR
3406             mc_header_id IN (SELECT mc_header_id
3407                                FROM ahl_mc_config_relations
3408                               WHERE relationship_id = c_parent_relationship_id
3409                                 AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3410                                 AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)));
3411 
3412   --Cursor to check whether c_parent_instance_id's child position c_relationship_id is empty
3413   CURSOR check_position_empty(c_parent_instance_id NUMBER, c_relationship_id NUMBER) IS
3414     SELECT subject_id
3415       FROM csi_ii_relationships
3416      WHERE object_id = c_parent_instance_id
3417        AND position_reference = to_char(c_relationship_id)
3418        AND subject_id IS NOT NULL
3419        AND relationship_type_code = 'COMPONENT-OF'
3420        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3421        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
3422 
3423   CURSOR check_instance_leaf(c_instance_id NUMBER) IS
3424     SELECT subject_id
3425       FROM csi_ii_relationships
3426      WHERE object_id = c_instance_id
3427        AND relationship_type_code = 'COMPONENT-OF'
3428        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3429        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
3430 
3431   CURSOR get_uc_header(c_instance_id NUMBER) IS
3432     SELECT unit_config_header_id, master_config_id
3433       FROM ahl_unit_config_headers
3434      WHERE csi_item_instance_id = c_instance_id
3435        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
3436   --To get all the first level sub-units for a given branch node. First get all of the
3437   --branch node's sub-units and then remove those sub-units which are not first level
3438   --(from the branch node's perspective)
3439 
3440   CURSOR get_1st_level_subunits(c_instance_id NUMBER) IS
3441   /*This query is replaced by the query below it for performance gain.
3442     SELECT subject_id
3443       FROM csi_ii_relationships
3444      WHERE subject_id IN (SELECT csi_item_instance_id
3445                             FROM ahl_unit_config_headers
3446                            WHERE trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE))
3447 START WITH object_id = c_instance_id
3448        AND relationship_type_code = 'COMPONENT-OF'
3449        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3450        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
3451 CONNECT BY object_id = PRIOR subject_id
3452        AND relationship_type_code = 'COMPONENT-OF'
3453        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3454        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
3455      MINUS
3456     SELECT subject_id
3457       FROM csi_ii_relationships
3458      WHERE subject_id IN (SELECT csi_item_instance_id
3459                             FROM ahl_unit_config_headers
3460                            WHERE trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE))
3461 START WITH object_id IN (SELECT subject_id
3462                            FROM csi_ii_relationships
3463                           WHERE subject_id IN (SELECT csi_item_instance_id
3464                                                  FROM ahl_unit_config_headers
3465                                                 WHERE trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE))
3466                      START WITH object_id = c_instance_id
3467                             AND relationship_type_code = 'COMPONENT-OF'
3468                             AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3469                             AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
3470                      CONNECT BY object_id = PRIOR subject_id
3471                             AND relationship_type_code = 'COMPONENT-OF'
3472                             AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3473                             AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE))
3474        AND relationship_type_code = 'COMPONENT-OF'
3475        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3476        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
3477 CONNECT BY object_id = PRIOR subject_id
3478        AND relationship_type_code = 'COMPONENT-OF'
3479        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3480        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
3481   */
3482    SELECT i.subject_id
3483     FROM csi_ii_relationships i
3484    WHERE EXISTS (SELECT 'x'
3485                   FROM ahl_unit_config_headers u
3486                  WHERE u.csi_item_instance_id = i.subject_id
3487                    AND trunc(nvl(u.active_end_date, SYSDATE+1)) > trunc(SYSDATE))
3488      AND NOT EXISTS (SELECT ci.object_id
3489                        FROM csi_ii_relationships ci
3490                       WHERE (EXISTS (SELECT 'x'
3491                                        FROM ahl_unit_config_headers ui
3492                                       WHERE ui.csi_item_instance_id = ci.object_id)
3493                                 AND ci.object_id <> c_instance_id)
3494                  START WITH ci.subject_id = i.subject_id
3495                         AND ci.relationship_type_code = 'COMPONENT-OF'
3496                         AND trunc(nvl(ci.active_start_date, SYSDATE)) <= trunc(SYSDATE)
3497                         AND trunc(nvl(ci.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
3498                  CONNECT BY ci.subject_id = prior ci.object_id
3499                         AND ci.relationship_type_code = 'COMPONENT-OF'
3500                         AND trunc(nvl(ci.active_start_date, SYSDATE)) <= trunc(SYSDATE)
3501                         AND trunc(nvl(ci.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
3502                         AND ci.subject_id <> c_instance_id)
3503 START WITH i.object_id = c_instance_id
3504        AND i.relationship_type_code = 'COMPONENT-OF'
3505        AND trunc(nvl(i.active_start_date, SYSDATE)) <= trunc(SYSDATE)
3506        AND trunc(nvl(i.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
3507 CONNECT BY i.object_id = PRIOR i.subject_id
3508        AND i.relationship_type_code = 'COMPONENT-OF'
3509        AND trunc(nvl(i.active_start_date, SYSDATE)) <= trunc(SYSDATE)
3510        AND trunc(nvl(i.active_end_date, SYSDATE+1)) > trunc(SYSDATE);
3511 
3512   CURSOR csi_item_instance_csr(c_instance_id  IN  NUMBER) IS
3513     SELECT C.inventory_item_id,
3514            C.inv_master_organization_id inventory_org_id,
3515            C.quantity,
3516            C.unit_of_measure uom_code,
3517            C.inventory_revision revision,
3518            C.install_date,
3519            C.instance_usage_code,
3520            C.location_type_code,
3521            C.object_version_number,
3522            U.unit_config_header_id uc_header_id
3523       FROM csi_item_instances C,
3524            ahl_unit_config_headers U
3525      WHERE C.instance_id = c_instance_id
3526        AND C.instance_id = U.csi_item_instance_id (+)
3527        --AND U.parent_uc_header_id (+) IS NULL
3528        --Comment out in order to include the extra sibling subunits whose parent_uc_header_id
3529        --is not null
3530        AND trunc(SYSDATE) < trunc(nvl(C.active_end_date,SYSDATE+1))
3531        AND trunc(SYSDATE) < trunc(nvl(U.active_end_date (+),SYSDATE+1));
3532   l_instance_rec        csi_item_instance_csr%ROWTYPE;
3533 
3534   CURSOR check_sub_mc(c_relationship_id NUMBER) IS
3535     SELECT mc_header_id
3536       FROM ahl_mc_config_relations
3537      WHERE relationship_id = c_relationship_id
3538        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3539        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
3540 
3541   CURSOR check_extra_node(c_object_id NUMBER, c_subject_id NUMBER) IS
3542     SELECT relationship_id, object_version_number
3543       FROM csi_ii_relationships
3544      WHERE object_id = c_object_id
3545        AND subject_id = c_subject_id
3546        AND position_reference IS NULL
3547        AND relationship_type_code = 'COMPONENT-OF'
3548        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3549        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
3550 
3551   -- SATHAPLI::FP ER 6504147, 18-Nov-2008
3552   CURSOR check_unasgnd_extra_node_csr(p_parent_instance_id NUMBER, p_instance_id NUMBER) IS
3553     SELECT relationship_id, object_version_number
3554       FROM csi_ii_relationships
3555      WHERE object_id IN (
3556                          SELECT ii.object_id
3557                          FROM   csi_ii_relationships ii
3558                          START WITH ii.subject_id = p_parent_instance_id
3559                          AND    ii.relationship_type_code = 'COMPONENT-OF'
3560                          AND    trunc(nvl(ii.active_start_date, SYSDATE)) <= trunc(SYSDATE)
3561                          AND    trunc(nvl(ii.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
3562                          CONNECT BY ii.subject_id = PRIOR ii.object_id
3563                          AND    ii.relationship_type_code = 'COMPONENT-OF'
3564                          AND    trunc(nvl(ii.active_start_date, SYSDATE)) <= trunc(SYSDATE)
3565                          AND    trunc(nvl(ii.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
3566                         )
3567        AND subject_id = p_instance_id
3568        AND position_reference IS NULL
3569        AND relationship_type_code = 'COMPONENT-OF'
3570        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3571        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
3572 
3573   CURSOR get_serial_number(c_instance_id NUMBER) IS
3574     SELECT serial_number, mfg_serial_number_flag
3575       FROM csi_item_instances
3576      WHERE instance_id = c_instance_id
3577        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3578        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
3579 
3580   CURSOR check_instance_installed(c_instance_id NUMBER) IS
3581     SELECT 'X'
3582       FROM csi_ii_relationships
3583      WHERE subject_id = c_instance_id
3584        AND position_reference IS NOT NULL
3585        --for extra node, it is still available for its sibling nodes even
3586        --if it is installed and not removed
3587        AND relationship_type_code = 'COMPONENT-OF'
3588        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3589        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
3590 
3591   CURSOR get_interchange_type (c_instance_id NUMBER, c_relationship_id NUMBER) IS
3592     SELECT i.interchange_type_code
3593       FROM csi_item_instances c,
3594            ahl_item_associations_b i,
3595            ahl_mc_relationships m
3596      WHERE m.relationship_id = c_relationship_id
3597        AND c.instance_id = c_instance_id
3598        AND m.item_group_id = i.item_group_id
3599        AND c.inventory_item_id = i.inventory_item_id
3600        AND c.inv_master_organization_id = i.inventory_org_id
3601        AND (c.inventory_revision IS NULL OR
3602             i.revision is NULL OR
3603             (c.inventory_revision IS NOT NULL AND
3604              i.revision IS NOT NULL AND
3605              c.inventory_revision = i.revision));
3606    --Added this last condition due to the impact of bug fixing 4102152, added by Jerry on 01/05/2005
3607    --Need to confirm which one is more accurate here to use c.inv_master_organization_id or
3608    --c.last_vld_organization_id
3609 
3610 BEGIN
3611   --Initialize API return status to success
3612   x_return_status := FND_API.G_RET_STS_SUCCESS;
3613 
3614   -- Standard Start of API savepoint
3615   SAVEPOINT install_existing_instance;
3616 
3617   --Standard call to check for call compatibility.
3618   IF NOT FND_API.compatible_api_call(
3619     l_api_version,
3620     p_api_version,
3621     l_api_name,  G_PKG_NAME)
3622   THEN
3623     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3624   END IF;
3625 
3626   --Initialize message list if p_init_msg_list is set to TRUE.
3627   IF FND_API.to_boolean( p_init_msg_list ) THEN
3628     FND_MSG_PUB.initialize;
3629   END IF;
3630 
3631   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3632     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
3633                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Begin API',
3634                    'At the start of the procedure');
3635   END IF;
3636 
3637   --Validate input parameters p_prod_user_flag
3638   IF upper(p_prod_user_flag) <> 'Y' AND upper(p_prod_user_flag) <> 'N' THEN
3639     FND_MESSAGE.set_name('AHL','AHL_UC_API_PARAMETER_INVALID');
3640     FND_MESSAGE.set_token('NAME', 'prod_user_flag');
3641     FND_MESSAGE.set_token('VALUE', p_prod_user_flag);
3642     FND_MSG_PUB.add;
3643     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3644   END IF;
3645 
3646   --Validate input parameter p_uc_header_id, its two statuses
3647   OPEN check_uc_header;
3648   FETCH check_uc_header INTO l_check_uc_header;
3649   IF check_uc_header%NOTFOUND THEN
3650     FND_MESSAGE.set_name('AHL','AHL_UC_API_PARAMETER_INVALID');
3651     FND_MESSAGE.set_token('NAME', 'uc_header_id');
3652     FND_MESSAGE.set_token('VALUE', p_uc_header_id);
3653     FND_MSG_PUB.add;
3654     CLOSE check_uc_header;
3655     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3656   ELSE
3657 
3658     -- ACL :: Changes for R12
3659     IF (ahl_util_uc_pkg.IS_UNIT_QUARANTINED(p_unit_header_id => p_uc_header_id , p_instance_id => null) = FND_API.G_TRUE) THEN
3660       FND_MESSAGE.set_name( 'AHL','AHL_UC_INVALID_Q_ACTION' );
3661       FND_MSG_PUB.add;
3662       CLOSE check_uc_header;
3663       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3664     END IF;
3665 
3666     ahl_util_uc_pkg.get_root_uc_attr(p_uc_header_id,
3667                                      l_root_uc_header_id,
3668                                      l_root_instance_id,
3669                                      l_root_uc_status_code,
3670                                      l_root_active_uc_status_code,
3671                                      l_root_uc_ovn);
3672     IF (p_prod_user_flag = 'Y' AND --For production user, no need to confirm either one of the statuses is not APPROVAL_PENDING
3673         l_root_uc_status_code NOT IN ('COMPLETE', 'INCOMPLETE')) THEN
3674       FND_MESSAGE.set_name( 'AHL','AHL_UC_STATUS_NOT_ACTIVE' );
3675       FND_MSG_PUB.add;
3676       CLOSE check_uc_header;
3677       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3678     ELSIF (p_prod_user_flag = 'N' AND
3679            (l_root_uc_status_code = 'APPROVAL_PENDING' OR
3680             l_root_active_uc_status_code = 'APPROVAL_PENDING')) THEN
3681       FND_MESSAGE.set_name( 'AHL','AHL_UC_STATUS_PENDING' );
3682       FND_MESSAGE.set_token('UC_HEADER_ID', l_root_uc_header_id);
3683       FND_MSG_PUB.add;
3684       CLOSE check_uc_header;
3685       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3686     ELSE
3687       CLOSE check_uc_header;
3688     END IF;
3689   END IF;
3690 
3691   --Get the operating unit of the root instance.
3692   l_root_instance_ou := get_operating_unit(l_root_instance_id);
3693 
3694   --Make sure p_parent_instance_id is installed in the UC
3695   IF p_parent_instance_id = l_check_uc_header.csi_item_instance_id THEN
3696     --The parent instance is the root node
3697     l_parent_relationship_id := l_check_uc_header.relationship_id;
3698   ELSE
3699     FOR l_get_uc_descendant IN get_uc_descendants(l_check_uc_header.csi_item_instance_id) LOOP
3700       l_csi_relationship_id := l_get_uc_descendant.relationship_id;
3701       l_object_version_number := l_get_uc_descendant.object_version_number;
3702       l_object_id := l_get_uc_descendant.object_id;
3703       l_subject_id := l_get_uc_descendant.subject_id;
3704       l_parent_relationship_id := l_get_uc_descendant.position_id;
3705       EXIT WHEN l_subject_id = p_parent_instance_id;
3706     END LOOP;
3707     --Ensure the instance is installed in this UC and not an extra node
3708     IF (l_subject_id <> p_parent_instance_id OR
3709         p_parent_instance_id IS NULL OR
3710         l_subject_id IS NULL OR
3711         l_parent_relationship_id IS NULL) THEN
3712       --We don't allow installing child instance to an extra node.
3713       FND_MESSAGE.set_name( 'AHL','AHL_UC_INSTANCE_NOT_IN_UC' );
3714       FND_MESSAGE.set_token('INSTANCE', p_parent_instance_id);
3715       FND_MSG_PUB.add;
3716       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3717     END IF;
3718   END IF;
3719   --Then validate p_relationship_id can be child of l_parent_relationship_id
3720   OPEN check_parent_relationship(p_relationship_id, l_parent_relationship_id);
3721   FETCH check_parent_relationship INTO l_dummy;
3722   IF check_parent_relationship%NOTFOUND THEN
3723     FND_MESSAGE.set_name( 'AHL','AHL_UC_POSITION_MISMATCH' );
3724     FND_MESSAGE.set_token('CHILD', p_relationship_id);
3725     FND_MESSAGE.set_token('PARENT', l_parent_relationship_id);
3726     FND_MSG_PUB.add;
3727     CLOSE check_parent_relationship;
3728     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3729   ELSE
3730     CLOSE check_parent_relationship;
3731   END IF;
3732   --Make sure position p_relationship_id is empty
3733   OPEN check_position_empty(p_parent_instance_id, p_relationship_id);
3734   FETCH check_position_empty INTO l_dummy;
3735   IF check_position_empty%FOUND THEN
3736     FND_MESSAGE.set_name( 'AHL','AHL_UC_POSITION_INSTALLED' );
3737     FND_MESSAGE.set_token('POSITION', p_relationship_id);
3738     FND_MSG_PUB.add;
3739     CLOSE check_position_empty;
3740     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3741   ELSE
3742     CLOSE check_position_empty;
3743   END IF;
3744 
3745 
3746   --Validate the instance to be installed is existing
3747   OPEN csi_item_instance_csr(p_instance_id);
3748   FETCH csi_item_instance_csr INTO l_instance_rec;
3749   IF (csi_item_instance_csr%NOTFOUND) THEN
3750     CLOSE csi_item_instance_csr;
3751     FND_MESSAGE.set_name('AHL','AHL_UC_CSII_INVALID');
3752     FND_MESSAGE.set_token('CSII',p_instance_id);
3753     FND_MESSAGE.set_token('POSN_REF',p_relationship_id);
3754     FND_MSG_PUB.add;
3755     --dbms_output.put_line('CSI item instance ID does not exist.');
3756     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3757   END IF;
3758   CLOSE csi_item_instance_csr;
3759 
3760   --Ensure the instance is available, not installed. For extra node, even if it
3761   --is installed but it is still available for its sibling nodes.
3762   OPEN check_instance_installed(p_instance_id);
3763   FETCH check_instance_installed INTO l_dummy_char;
3764   IF (check_instance_installed%FOUND) THEN
3765     CLOSE check_instance_installed;
3766     FND_MESSAGE.set_name('AHL','AHL_UC_INSTANCE_INSTALLED');
3767     FND_MESSAGE.set_token('INSTANCE',p_instance_id);
3768     FND_MSG_PUB.add;
3769     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3770   END IF;
3771   CLOSE check_instance_installed;
3772 
3773 
3774   --Check object_version_number of the instance
3775 /*  IF (p_uc_instance_rec.object_version_number <> l_csi_inst_rec.object_version
3776 _number) THEN
3777     CLOSE csi_item_instance_csr;
3778     FND_MESSAGE.set_name('AHL','AHL_COM_RECORD_CHANGED');
3779     FND_MSG_PUB.add;
3780     --dbms_output.put_line('Item Instance id object version changed');
3781     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3782   END IF;
3783 */
3784   --Validate the status of the instance
3785   IF (l_instance_rec.location_type_code IN ('PO','IN-TRANSIT','PROJECT','INVENTORY')) THEN
3786     FND_MESSAGE.set_name('AHL','AHL_UC_INST_STATUS_INVALID');
3787     AHL_UTIL_UC_PKG.convert_to_csimeaning('CSI_INST_LOCATION_SOURCE_CODE',
3788                                           l_instance_rec.location_type_code,
3789                                           l_meaning,l_return_val);
3790     IF NOT(l_return_val) THEN
3791       l_meaning := l_instance_rec.location_type_code;
3792     END IF;
3793     FND_MESSAGE.set_token('LOCATION',l_meaning);
3794     FND_MSG_PUB.add;
3795     --dbms_output.put_line('Item Instance location is not valid');
3796   END IF;
3797 
3798   --If the instance is not a unit, then validate positional attributes. For unit, it is not
3799   --necessary to validate.
3800   IF (l_instance_rec.uc_header_id IS NULL) THEN
3801     AHL_UTIL_UC_PKG.validate_for_position(p_relationship_id,
3802                                           l_instance_rec.inventory_item_id,
3803                                           l_instance_rec.inventory_org_id,
3804                                           l_instance_rec.quantity,
3805                                           l_instance_rec.revision,
3806                                           l_instance_rec.uom_code,
3807                                           NULL,
3808                                           -- SATHAPLI::FP OGMA Issue# 105 - Non-Serialized Item Maintenance, 05-Dec-2007
3809                                           -- Pass 'N' for p_ignore_quant_vald.
3810                                           'N',
3811                                           l_item_assoc_id);
3812   END IF;
3813 
3814   --Validate installation date.
3815   --Keep the installation date validation only for production user(04/21/2004).
3816   IF (l_instance_rec.install_date IS NOT NULL AND
3817       l_instance_rec.install_date <> FND_API.G_MISS_DATE) THEN
3818     IF (p_prod_user_flag = 'Y' AND trunc(l_instance_rec.install_date) > trunc(SYSDATE)) THEN
3819       FND_MESSAGE.set_name('AHL','AHL_UC_INSTDATE_INVALID');
3820       FND_MESSAGE.set_token('DATE',l_instance_rec.install_date);
3821       FND_MESSAGE.set_token('POSN_REF',p_relationship_id);
3822       FND_MSG_PUB.add;
3823       --dbms_output.put_line('Installation date invalid.');
3824     END IF;
3825   END IF;
3826 
3827   --Check Error Message stack.
3828   l_msg_count := FND_MSG_PUB.count_msg;
3829   IF l_msg_count > 0 THEN
3830     RAISE  FND_API.G_EXC_ERROR;
3831   END IF;
3832 
3833   --Before installing the existing instance, make sure its operating unit is exactly the same as that
3834   --of the root instance.
3835   l_instance_ou := get_operating_unit(p_instance_id);
3836   IF l_root_instance_ou IS NULL THEN
3837     FND_MESSAGE.set_name('AHL', 'AHL_UC_INSTANCE_OU_NULL');
3838     FND_MESSAGE.set_token('INSTANCE', l_root_instance_id);
3839     FND_MSG_PUB.add;
3840     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3841   ELSIF l_instance_ou IS NULL THEN
3842     FND_MESSAGE.set_name('AHL', 'AHL_UC_INSTANCE_OU_NULL');
3843     FND_MESSAGE.set_token('INSTANCE', p_instance_id);
3844     FND_MSG_PUB.add;
3845     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3846   ELSIF l_root_instance_ou <> l_instance_ou THEN
3847     FND_MESSAGE.set_name('AHL', 'AHL_UC_INSTANCE_OU_UNMATCH');
3848     FND_MESSAGE.set_token('INSTANCE', p_instance_id);
3849     FND_MESSAGE.set_token('ROOT_INSTANCE', l_root_instance_id);
3850     FND_MSG_PUB.add;
3851     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3852   END IF;
3853 
3854   --Check the instance to be installed is a leaf node, branch node or sub-unit top node(in this
3855   --case, it might also be a leaf node in csi_ii_relationships if all of its descendants are empty)
3856   OPEN get_uc_header(p_instance_id);
3857   FETCH get_uc_header INTO l_sub_uc_header_id, l_mc_header_id;
3858   IF get_uc_header%FOUND THEN
3859     -- ACL :: R12 Changes
3860     IF (ahl_util_uc_pkg.IS_UNIT_QUARANTINED(p_unit_header_id => l_sub_uc_header_id , p_instance_id => null) = FND_API.G_TRUE) THEN
3861       FND_MESSAGE.set_name( 'AHL','AHL_UC_INVALID_Q_ACTION' );
3862       FND_MSG_PUB.add;
3863       CLOSE check_uc_header;
3864       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3865     END IF;
3866     --The instance is a unit top node, needs to see whether it can be a candidate sub-unit
3867     --in that position
3868     l_instance_type := 'S';
3869     l_subunit := FALSE;
3870     FOR l_check_sub_mc IN check_sub_mc(p_relationship_id) LOOP
3871       IF l_mc_header_id = l_check_sub_mc.mc_header_id THEN
3872         l_subunit := TRUE;
3873         EXIT;
3874       END IF;
3875     END LOOP;
3876     IF NOT l_subunit THEN
3877       FND_MESSAGE.set_name('AHL','AHL_UC_SUBUNIT_MISMATCH');
3878       FND_MESSAGE.set_token('INSTANCE', p_instance_id);
3879       FND_MESSAGE.set_token('POSITION', p_relationship_id);
3880       FND_MSG_PUB.add;
3881       CLOSE get_uc_header;
3882       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3883     END IF;
3884   ELSE
3885     OPEN check_instance_leaf(p_instance_id);
3886     FETCH check_instance_leaf INTO l_dummy;
3887     IF check_instance_leaf%FOUND THEN --Non leaf instance
3888       --The instance is a branch node, needs to call remap_uc_subtree to see whether the branch
3889       --can be installed in that position. If match, the corresponding position reference will be
3890       --updated as well.
3891       l_instance_type := 'B';
3892       ahl_uc_tree_pvt.remap_uc_subtree(
3893                           p_api_version      => 1.0,
3894                           p_init_msg_list    => FND_API.G_FALSE,
3895                           p_commit           => FND_API.G_FALSE,
3896                           p_validation_level => FND_API.G_VALID_LEVEL_FULL,
3897                           x_return_status    => l_return_status,
3898                           x_msg_count        => l_msg_count,
3899                           x_msg_data         => l_msg_data,
3900                           p_instance_id      => p_instance_id,
3901                           p_relationship_id  => p_relationship_id);
3902       IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
3903         CLOSE check_instance_leaf;
3904         RAISE FND_API.G_EXC_ERROR;
3905       ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
3906         CLOSE check_instance_leaf;
3907         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3908       END IF;
3909     ELSE
3910       l_instance_type := 'L';
3911     END IF;
3912     CLOSE check_instance_leaf;
3913   END IF;
3914   CLOSE get_uc_header;
3915 
3916   --Build CSI transaction record, first get transaction_type_id
3917   AHL_Util_UC_Pkg.getcsi_transaction_id('UC_UPDATE',l_transaction_type_id, l_return_val);
3918   IF NOT(l_return_val) THEN
3919     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3920   END IF;
3921 
3922   l_csi_transaction_rec.source_transaction_date := SYSDATE;
3923   l_csi_transaction_rec.transaction_type_id := l_transaction_type_id;
3924 
3925   --Update installation date if provided.
3926   IF (l_instance_rec.install_date IS NOT NULL AND
3927       l_instance_rec.install_date <> FND_API.G_MISS_DATE) THEN
3928     -- Build CSI instance rec.
3929     l_csi_instance_rec.instance_id           := p_instance_id;
3930     l_csi_instance_rec.object_version_number := l_instance_rec.object_version_number;
3931     l_csi_instance_rec.install_date          := l_instance_rec.install_date;
3932 
3933     -- Call API to update installation date.
3934     CSI_ITEM_INSTANCE_PUB.update_item_instance(
3935                           p_api_version            => 1.0,
3936                           p_instance_rec           => l_csi_instance_rec,
3937                           p_txn_rec                => l_csi_transaction_rec,
3938                           p_ext_attrib_values_tbl  => l_csi_ext_attrib_values_tbl,
3939                           p_party_tbl              => l_csi_party_tbl,
3940                           p_account_tbl            => l_csi_account_tbl,
3941                           p_pricing_attrib_tbl     => l_csi_pricing_attrib_tbl,
3942                           p_org_assignments_tbl    => l_csi_org_assignments_tbl,
3943                           p_asset_assignment_tbl   => l_csi_asset_assignment_tbl,
3944                           x_instance_id_lst        => l_csi_instance_id_lst,
3945                           x_return_status          => l_return_status,
3946                           x_msg_count              => l_msg_count,
3947                           x_msg_data               => l_msg_data);
3948     IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
3949       RAISE FND_API.G_EXC_ERROR;
3950     ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
3951       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3952     END IF;
3953   END IF;
3954 
3955   --Need to check if the instance picked from CSI with serial_number has a serial_no_tag, and
3956   --if not, we have to derive its value according to mfg_serail_number_flag ('Y'->'INVENTORY',
3957   --assuming CSI has the validation to ensure the serial_number exisiting in table
3958   --mfg_searil_numbers, otherwise it is 'TEMPORARY'
3959   OPEN get_serial_number(p_instance_id);
3960   FETCH get_serial_number INTO l_serial_number, l_mfg_serial_number_flag;
3961   IF get_serial_number%NOTFOUND THEN
3962     FND_MESSAGE.set_name('AHL', 'AHL_UC_CSII_INVALID');
3963     FND_MESSAGE.set_token('CSII', p_instance_id);
3964     FND_MSG_PUB.add;
3965     RAISE FND_API.G_EXC_ERROR;
3966     CLOSE get_serial_number;
3967   ELSE
3968     CLOSE get_serial_number;
3969   END IF;
3970 
3971   IF l_serial_number IS NOT NULL THEN
3972     --Retrieve existing value of serial_number_tag if present.
3973     AHL_UTIL_UC_PKG.getcsi_attribute_value(p_instance_id,
3974                                            'AHL_TEMP_SERIAL_NUM',
3975                                            l_attribute_value,
3976                                            l_attribute_value_id,
3977                                            l_object_version_number,
3978                                            l_return_val);
3979     IF NOT l_return_val THEN --serial_number_tag doesn't exist
3980       --Modified by mpothuku on 13-Jul-2007 for fixing the Bug 4337259
3981       /*
3982       IF l_mfg_serial_number_flag = 'Y' THEN
3983         l_serial_number_tag := 'INVENTORY';
3984       ELSE
3985         l_serial_number_tag := 'TEMPORARY';
3986       END IF;
3987       */
3988       l_serial_number_tag := 'ACTUAL';
3989       --mpothuku End
3990       AHL_Util_UC_Pkg.getcsi_attribute_id('AHL_TEMP_SERIAL_NUM', l_attribute_id, l_return_val);
3991 
3992       IF NOT(l_return_val) THEN
3993         FND_MESSAGE.set_name('AHL','AHL_UC_ATTRIB_CODE_MISSING');
3994         FND_MESSAGE.set_token('CODE', 'AHL_TEMP_SERIAL_NUM');
3995         FND_MSG_PUB.add;
3996       ELSE
3997         l_csi_extend_attrib_rec.attribute_id := l_attribute_id;
3998         l_csi_extend_attrib_rec.attribute_value := l_serial_number_tag;
3999         l_csi_extend_attrib_rec.instance_id := p_instance_id;
4000         l_csi_ext_attrib_values_tbl(1) := l_csi_extend_attrib_rec;
4001       END IF;
4002 
4003       CSI_ITEM_INSTANCE_PUB.create_extended_attrib_values(
4004                           p_api_version            => 1.0,
4005                           p_txn_rec                => l_csi_transaction_rec,
4006                           p_ext_attrib_tbl         => l_csi_ext_attrib_values_tbl,
4007                           x_return_status          => l_return_status,
4008                           x_msg_count              => l_msg_count,
4009                           x_msg_data               => l_msg_data);
4010 
4011       IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
4012         RAISE FND_API.G_EXC_ERROR;
4013       ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
4014         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4015       END IF;
4016     END IF;
4017   END IF;
4018 
4019   --Check to see whether an extra node relationship record has already existed.
4020   --then just update the position_reference from null to p_relationship_id, otherwise
4021   --need to create a new csi_ii_relationship record
4022   OPEN check_extra_node(p_parent_instance_id, p_instance_id);
4023   FETCH check_extra_node INTO l_csi_relationship_id, l_object_version_number;
4024   IF check_extra_node%FOUND THEN
4025     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4026         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name,
4027                        ' sibling extra node found'||
4028                        ' p_csi_ii_ovn => '||p_csi_ii_ovn);
4029     END IF;
4030 
4031     --Validate input parameters p_csi_ii_ovn
4032     IF (p_csi_ii_ovn IS NULL OR p_csi_ii_ovn <= 0 ) THEN
4033       FND_MESSAGE.set_name('AHL','AHL_UC_API_PARAMETER_INVALID');
4034       FND_MESSAGE.set_token('NAME', 'csi_ii_ovn');
4035       FND_MESSAGE.set_token('VALUE', p_csi_ii_ovn);
4036       FND_MSG_PUB.add;
4037       CLOSE check_extra_node;
4038       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4039     ELSIF l_object_version_number <> p_csi_ii_ovn THEN
4040       FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
4041       FND_MSG_PUB.add;
4042       CLOSE check_extra_node;
4043       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4044     END IF;
4045     l_csi_relationship_rec.relationship_id := l_csi_relationship_id;
4046     l_csi_relationship_rec.object_version_number := l_object_version_number;
4047     l_csi_relationship_rec.relationship_type_code := 'COMPONENT-OF';
4048     l_csi_relationship_rec.object_id := p_parent_instance_id;
4049     l_csi_relationship_rec.subject_id := p_instance_id;
4050     l_csi_relationship_rec.position_reference := to_char(p_relationship_id);
4051     l_csi_relationship_tbl(1) := l_csi_relationship_rec;
4052     CSI_II_RELATIONSHIPS_PUB.update_relationship(
4053                              p_api_version      => 1.0,
4054                              p_relationship_tbl => l_csi_relationship_tbl,
4055                              p_txn_rec          => l_csi_transaction_rec,
4056                              x_return_status    => l_return_status,
4057                              x_msg_count        => l_msg_count,
4058                              x_msg_data         => l_msg_data);
4059   ELSE
4060     -- SATHAPLI::FP ER 6504147, 18-Nov-2008
4061     -- check if it is unassigned extra instance attached to any of the parents uptill the root node
4062     OPEN check_unasgnd_extra_node_csr(p_parent_instance_id, p_instance_id);
4063     FETCH check_unasgnd_extra_node_csr INTO l_csi_relationship_id, l_object_version_number;
4064 
4065     IF check_unasgnd_extra_node_csr%FOUND THEN
4066       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4067           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name,
4068                          ' extra node attached to any of the parents uptill root found'||
4069                          ' p_parent_instance_id => '||p_parent_instance_id||
4070                          ' p_instance_id => '||p_instance_id||
4071                          ' p_csi_ii_ovn => '||p_csi_ii_ovn);
4072       END IF;
4073 
4074       -- Validate input parameters p_csi_ii_ovn
4075       IF (p_csi_ii_ovn IS NULL OR p_csi_ii_ovn <= 0 ) THEN
4076         FND_MESSAGE.set_name('AHL','AHL_UC_API_PARAMETER_INVALID');
4077         FND_MESSAGE.set_token('NAME', 'csi_ii_ovn');
4078         FND_MESSAGE.set_token('VALUE', p_csi_ii_ovn);
4079         FND_MSG_PUB.add;
4080         CLOSE check_unasgnd_extra_node_csr;
4081         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4082       ELSIF l_object_version_number <> p_csi_ii_ovn THEN
4083         FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
4084         FND_MSG_PUB.add;
4085         CLOSE check_unasgnd_extra_node_csr;
4086         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4087       END IF;
4088 
4089       -- expire the existing relationship
4090       -- Set CSI relationship record
4091       l_csi_relationship_rec.relationship_id := l_csi_relationship_id;
4092       l_csi_relationship_rec.object_version_number := l_object_version_number;
4093 
4094       CSI_II_RELATIONSHIPS_PUB.expire_relationship(
4095                                p_api_version      => 1.0,
4096                                p_relationship_rec => l_csi_relationship_rec,
4097                                p_txn_rec          => l_csi_transaction_rec,
4098                                x_instance_id_lst  => l_csi_instance_id_lst,
4099                                x_return_status    => l_return_status,
4100                                x_msg_count        => l_msg_count,
4101                                x_msg_data         => l_msg_data);
4102 
4103       IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
4104         RAISE FND_API.G_EXC_ERROR;
4105       ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
4106         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4107       END IF;
4108 
4109       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4110           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name,
4111                          ' unassigned extra node relationship expired');
4112       END IF;
4113 
4114       -- create new relationship
4115       -- create new CSI record
4116       l_csi_relationship_new_rec.relationship_type_code := 'COMPONENT-OF';
4117       l_csi_relationship_new_rec.object_id := p_parent_instance_id;
4118       l_csi_relationship_new_rec.subject_id := p_instance_id;
4119       l_csi_relationship_new_rec.position_reference := to_char(p_relationship_id);
4120       l_csi_relationship_tbl(1) := l_csi_relationship_new_rec;
4121       CSI_II_RELATIONSHIPS_PUB.create_relationship(
4122                                p_api_version      => 1.0,
4123                                p_relationship_tbl => l_csi_relationship_tbl,
4124                                p_txn_rec          => l_csi_transaction_rec,
4125                                x_return_status    => l_return_status,
4126                                x_msg_count        => l_msg_count,
4127                                x_msg_data         => l_msg_data);
4128 
4129       IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
4130         RAISE FND_API.G_EXC_ERROR;
4131       ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
4132         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4133       END IF;
4134 
4135       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4136           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name,
4137                          ' unassigned extra node new relationship created');
4138       END IF;
4139     ELSE
4140       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4141           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name,
4142                          ' free standing instance found'||
4143                          ' p_csi_ii_ovn => '||p_csi_ii_ovn);
4144       END IF;
4145 
4146       l_csi_relationship_rec.relationship_type_code := 'COMPONENT-OF';
4147       l_csi_relationship_rec.object_id := p_parent_instance_id;
4148       l_csi_relationship_rec.subject_id := p_instance_id;
4149       l_csi_relationship_rec.position_reference := to_char(p_relationship_id);
4150       l_csi_relationship_tbl(1) := l_csi_relationship_rec;
4151       CSI_II_RELATIONSHIPS_PUB.create_relationship(
4152                                p_api_version      => 1.0,
4153                                p_relationship_tbl => l_csi_relationship_tbl,
4154                                p_txn_rec          => l_csi_transaction_rec,
4155                                x_return_status    => l_return_status,
4156                                x_msg_count        => l_msg_count,
4157                                x_msg_data         => l_msg_data);
4158     END IF; -- end IF check_unasgnd_extra_node_csr%FOUND
4159 
4160     CLOSE check_unasgnd_extra_node_csr;
4161   END IF;
4162   CLOSE check_extra_node;
4163   IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
4164     RAISE FND_API.G_EXC_ERROR;
4165   ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
4166     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4167   END IF;
4168 
4169   --If the node is the top node of a sub-unit then just itself, otherwise if it is a
4170   --branch node, then get all of its first level sub-units. For all of these sub-units,
4171   --we have to update their parent_uc_header_id to p_uc_header_id. Once a unit is installed
4172   --to another unit and automatically becomes a sub-unit, then it loses all its statuses. So
4173   --we don't have to update the two statuses here.
4174 
4175   IF l_subunit THEN
4176     ahl_util_uc_pkg.get_parent_uc_header(p_instance_id,
4177                                          l_parent_uc_header_id,
4178                                          l_parent_instance_id);
4179     UPDATE ahl_unit_config_headers
4180        --SET parent_uc_header_id = p_uc_header_id
4181        --The parameter p_uc_header_id is not necessarily the parent uc_header_id of the newly
4182        --installed instance.
4183        SET parent_uc_header_id = l_parent_uc_header_id,
4184            object_version_number = object_version_number + 1,
4185            last_update_date = SYSDATE,
4186            last_updated_by = FND_GLOBAL.user_id,
4187            last_update_login = FND_GLOBAL.login_id
4188      WHERE csi_item_instance_id = p_instance_id
4189        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
4190            --Not necessary to check the object_version_number here
4191     --Copy the change to history table
4192     ahl_util_uc_pkg.copy_uc_header_to_history(l_sub_uc_header_id, l_return_status);
4193     --IF history copy failed, then don't raise exception, just add the message to the message stack
4194     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4195       FND_MESSAGE.set_name('AHL', 'AHL_UC_HISTORY_COPY_FAILED');
4196       FND_MSG_PUB.add;
4197     END IF;
4198   ELSIF l_instance_type = 'B' THEN --this instance is a branch node
4199     ahl_util_uc_pkg.get_parent_uc_header(p_instance_id,
4200                                          l_parent_uc_header_id,
4201                                          l_parent_instance_id);
4202     FOR l_get_1st_level_subunit IN get_1st_level_subunits(p_instance_id) LOOP
4203       UPDATE ahl_unit_config_headers
4204          SET parent_uc_header_id = l_parent_uc_header_id,
4205              object_version_number = object_version_number + 1,
4206              last_update_date = SYSDATE,
4207              last_updated_by = FND_GLOBAL.user_id,
4208              last_update_login = FND_GLOBAL.login_id
4209        WHERE csi_item_instance_id = l_get_1st_level_subunit.subject_id
4210          AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
4211              --Not necessary to check the object_version_number here
4212 
4213       OPEN get_uc_header(l_get_1st_level_subunit.subject_id);
4214       FETCH get_uc_header INTO l_sub_uc_header_id, l_mc_header_id;
4215       IF get_uc_header%NOTFOUND THEN
4216         FND_MESSAGE.set_name('AHL', 'AHL_UC_INSTANCE_NOT_IN_UC');
4217         FND_MESSAGE.set_token('INSTANCE', l_get_1st_level_subunit.subject_id);
4218         FND_MSG_PUB.add;
4219         CLOSE get_uc_header;
4220         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4221       ELSE
4222         CLOSE get_uc_header;
4223       END IF;
4224 
4225       --Copy the change to history table
4226       ahl_util_uc_pkg.copy_uc_header_to_history(l_sub_uc_header_id, l_return_status);
4227       --IF history copy failed, then don't raise exception, just add the messageto the message stack
4228 
4229       IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4230         FND_MESSAGE.set_name('AHL', 'AHL_UC_HISTORY_COPY_FAILED');
4231         FND_MSG_PUB.add;
4232       END IF;
4233     END LOOP;
4234   END IF;
4235 
4236   --Call completeness check API for the newly assigned instance
4237   ahl_uc_validation_pub.validate_complete_for_pos(
4238       p_api_version         => 1.0,
4239       p_init_msg_list       => FND_API.G_FALSE,
4240       p_commit              => FND_API.G_FALSE,
4241       p_validation_level    => FND_API.G_VALID_LEVEL_FULL,
4242       x_return_status       => l_return_status,
4243       x_msg_count           => l_msg_count,
4244       x_msg_data            => l_msg_data,
4245       p_csi_instance_id     => p_instance_id,
4246       x_error_tbl           => x_warning_msg_tbl);
4247   IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
4248     RAISE FND_API.G_EXC_ERROR;
4249   ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
4250     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4251   END IF;
4252 
4253   --For sub unit top node, it is not necessary to have this item interchange type
4254   --code validation check
4255   IF l_instance_type <> 'S' THEN
4256     OPEN get_interchange_type(p_instance_id, p_relationship_id);
4257     FETCH get_interchange_type INTO l_interchange_type_code;
4258     IF get_interchange_type%NOTFOUND THEN
4259       FND_MESSAGE.set_name('AHL', 'AHL_UC_ITEM_INTERCHANGE_MISS');
4260       FND_MESSAGE.set_token('INSTANCE', p_instance_id);
4261       FND_MSG_PUB.add;
4262       CLOSE get_interchange_type;
4263       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4264     ELSIF l_interchange_type_code = '1-WAY INTERCHANGEABLE' THEN
4265       FND_MESSAGE.set_name('AHL', 'AHL_UC_1WAY_ITEM_INSTALLED');
4266       SELECT f.meaning INTO l_position_ref_meaning
4267         FROM ahl_mc_relationships a,
4268              fnd_lookups f
4269        WHERE a.relationship_id = p_relationship_id
4270          AND f.lookup_code (+) = A.position_ref_code
4271          AND f.lookup_type (+) = 'AHL_POSITION_REFERENCE' ;
4272       FND_MESSAGE.set_token('POSITION', l_position_ref_meaning);
4273       --Here the message is not added to the global message stack;
4274       IF x_warning_msg_tbl.count > 0 THEN
4275         x_warning_msg_tbl(x_warning_msg_tbl.last + 1) := FND_MESSAGE.get;
4276       ELSE
4277         x_warning_msg_tbl(0) := FND_MESSAGE.get;
4278       END IF;
4279     END IF;
4280     CLOSE get_interchange_type;
4281   END IF;
4282 
4283   --For UC user, UC header status change needs to be made after the operation
4284   --Not confirmed whether need to copy the record into UC header history table
4285   --after status change. Not include the copy right now. (No history copy)
4286   IF p_prod_user_flag = 'N' THEN
4287     IF (l_root_uc_status_code = 'COMPLETE' AND x_warning_msg_tbl.count > 0) THEN
4288     --IF unit_config_status_code='INCOMPLETE' and active_uc_status_code='UNAPPROVED', this
4289     --update is only object_version_number change and not necessary.
4290       UPDATE ahl_unit_config_headers
4291          SET unit_config_status_code = 'INCOMPLETE',
4292              active_uc_status_code = 'UNAPPROVED',
4293              object_version_number = object_version_number + 1,
4294              last_update_date = SYSDATE,
4295              last_updated_by = FND_GLOBAL.user_id,
4296              last_update_login = FND_GLOBAL.login_id
4297        WHERE unit_config_header_id = l_root_uc_header_id
4298          AND object_version_number = l_root_uc_ovn;
4299       IF SQL%ROWCOUNT = 0 THEN
4300         FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
4301         FND_MSG_PUB.add;
4302         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4303       END IF;
4304     ELSIF (l_root_uc_status_code IN ('COMPLETE', 'INCOMPLETE') AND
4305            (l_root_active_uc_status_code IS NULL OR
4306             l_root_active_uc_status_code <> 'UNAPPROVED')) THEN
4307     --IF unit_config_status_code='INCOMPLETE' and active_uc_status_code='UNAPPROVED', this
4308     --update is only object_version_number change and not necessary.
4309       UPDATE ahl_unit_config_headers
4310          SET active_uc_status_code = 'UNAPPROVED',
4311              object_version_number = object_version_number + 1,
4312              last_update_date = SYSDATE,
4313              last_updated_by = FND_GLOBAL.user_id,
4314              last_update_login = FND_GLOBAL.login_id
4315        WHERE unit_config_header_id = l_root_uc_header_id
4316          AND object_version_number = l_root_uc_ovn;
4317       IF SQL%ROWCOUNT = 0 THEN
4318         FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
4319         FND_MSG_PUB.add;
4320         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4321       END IF;
4322     ELSIF l_root_uc_status_code NOT IN ('COMPLETE', 'INCOMPLETE', 'DRAFT') THEN
4323       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4324         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
4325                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Before calling completeness check',
4326                    'p_uc_header_id='||p_uc_header_id||'l_root_uc_header_id='||l_root_uc_header_id||
4327                    'l_root_uc_ovn='||l_root_uc_ovn);
4328       END IF;
4329     --IF unit_config_status_code='DRAFT', this update is only object_version_number change and
4330     --not necessary.
4331       UPDATE ahl_unit_config_headers
4332          SET unit_config_status_code = 'DRAFT',
4333              object_version_number = object_version_number + 1,
4334              last_update_date = SYSDATE,
4335              last_updated_by = FND_GLOBAL.user_id,
4336              last_update_login = FND_GLOBAL.login_id
4337        WHERE unit_config_header_id = l_root_uc_header_id
4338          AND object_version_number = l_root_uc_ovn;
4339       IF SQL%ROWCOUNT = 0 THEN
4340         FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
4341         FND_MSG_PUB.add;
4342         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4343       END IF;
4344     END IF;
4345   ELSIF (p_prod_user_flag = 'Y' AND
4346          x_warning_msg_tbl.count > 0 AND
4347          l_root_uc_status_code = 'COMPLETE') THEN
4348     UPDATE ahl_unit_config_headers
4349        SET unit_config_status_code = 'INCOMPLETE',
4350            object_version_number = object_version_number + 1,
4351            last_update_date = SYSDATE,
4352            last_updated_by = FND_GLOBAL.user_id,
4353            last_update_login = FND_GLOBAL.login_id
4354      WHERE unit_config_header_id = l_root_uc_header_id
4355        AND object_version_number = l_root_uc_ovn;
4356     IF SQL%ROWCOUNT = 0 THEN
4357       FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
4358       FND_MSG_PUB.add;
4359       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4360     END IF;
4361   END IF;
4362 
4363   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4364     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
4365                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': After normal execution',
4366                    'At the end of the procedure');
4367   END IF;
4368 
4369   --Get all the error messages from the previous steps (if any) and raise the appropriate Exception
4370   l_msg_count := FND_MSG_PUB.count_msg;
4371   IF l_msg_count > 0 THEN
4372     x_msg_count := l_msg_count;
4373     RAISE FND_API.G_EXC_ERROR;
4374   END IF;
4375   -- Perform the Commit (if requested)
4376   IF FND_API.to_boolean(p_commit) THEN
4377     COMMIT;
4378   END IF;
4379   --Count and Get messages(optional)
4380   FND_MSG_PUB.count_and_get(
4381     p_encoded  => FND_API.G_FALSE,
4382     p_count    => x_msg_count,
4383     p_data     => x_msg_data);
4384 EXCEPTION
4385   WHEN FND_API.G_EXC_ERROR THEN
4386     ROLLBACK TO install_existing_instance;
4387     x_return_status := FND_API.G_RET_STS_ERROR;
4388     FND_MSG_PUB.count_and_get(
4389       p_encoded  => FND_API.G_FALSE,
4390       p_count    => x_msg_count,
4391       p_data     => x_msg_data);
4392   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4393     ROLLBACK TO install_existing_instance;
4394     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4395     FND_MSG_PUB.count_and_get(
4396       p_encoded  => FND_API.G_FALSE,
4397       p_count    => x_msg_count,
4398       p_data     => x_msg_data);
4399   WHEN OTHERS THEN
4400     ROLLBACK TO install_existing_instance;
4401     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4402     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
4403     THEN
4404       FND_MSG_PUB.add_exc_msg(
4405         p_pkg_name         => G_PKG_NAME,
4406         p_procedure_name   => l_api_name,
4407         p_error_text       => SUBSTRB(SQLERRM,1,240));
4408     END IF;
4409     FND_MSG_PUB.count_and_get(
4410       p_encoded  => FND_API.G_FALSE,
4411       p_count    => x_msg_count,
4412       p_data     => x_msg_data);
4413 END;
4414 
4415 -- Define procedure swap_instances
4416 -- This API is used by Production user to make parts change: replace an old instance
4417 -- a new one in a UC tree.
4418 PROCEDURE swap_instance(
4419   p_api_version           IN  NUMBER := 1.0,
4420   p_init_msg_list         IN  VARCHAR2 := FND_API.G_FALSE,
4421   p_commit                IN  VARCHAR2 := FND_API.G_FALSE,
4422   p_validation_level      IN  NUMBER := FND_API.G_VALID_LEVEL_FULL,
4423   x_return_status         OUT NOCOPY VARCHAR2,
4424   x_msg_count             OUT NOCOPY NUMBER,
4425   x_msg_data              OUT NOCOPY VARCHAR2,
4426   p_uc_header_id          IN  NUMBER,
4427   p_parent_instance_id    IN  NUMBER,
4428   p_old_instance_id       IN  NUMBER,
4429   p_new_instance_id       IN  NUMBER,
4430   p_new_instance_number   IN  csi_item_instances.instance_number%TYPE := NULL,
4431   p_relationship_id       IN  NUMBER,
4432   p_csi_ii_ovn            IN  NUMBER,
4433   p_prod_user_flag        IN  VARCHAR2,
4434   x_warning_msg_tbl       OUT NOCOPY ahl_uc_validation_pub.error_tbl_type)
4435 IS
4436   l_api_name       CONSTANT   VARCHAR2(30) := 'swap_instance';
4437   l_api_version    CONSTANT   NUMBER       := 1.0;
4438   l_return_status             VARCHAR2(1);
4439   l_msg_count                 NUMBER;
4440   l_msg_data                  VARCHAR2(2000);
4441   l_relationship_id           NUMBER;
4442   CURSOR check_relationship_id(c_subject_id NUMBER, c_relationship_id NUMBER) IS
4443     SELECT 'X'
4444       FROM csi_ii_relationships
4445      WHERE subject_id = c_subject_id
4446        AND position_reference = to_char(c_relationship_id)
4447        AND relationship_type_code = 'COMPONENT-OF'
4448        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
4449        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
4450 BEGIN
4451   -- Initialize API return status to success
4452   x_return_status := FND_API.G_RET_STS_SUCCESS;
4453 
4454   -- Standard Start of API savepoint
4455   SAVEPOINT swap_instance;
4456 
4457   -- Standard call to check for call compatibility.
4458   IF NOT FND_API.compatible_api_call(
4459     l_api_version,
4460     p_api_version,
4461     l_api_name,
4462     G_PKG_NAME)
4463   THEN
4464     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4465   END IF;
4466 
4467   -- Initialize message list if p_init_msg_list is set to TRUE.
4468   IF FND_API.to_boolean(p_init_msg_list) THEN
4469     FND_MSG_PUB.initialize;
4470   END IF;
4471 
4472   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4473     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
4474                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Begin API',
4475                    'At the start of the procedure');
4476   END IF;
4477 
4478   --Call remove_instance to remove the old instance
4479   remove_instance(
4480                   p_api_version      => 1.0,
4481                   p_init_msg_list    => FND_API.G_FALSE,
4482                   p_commit           => FND_API.G_FALSE,
4483                   p_validation_level => FND_API.G_VALID_LEVEL_FULL,
4484                   x_return_status    => l_return_status,
4485                   x_msg_count        => l_msg_count,
4486                   x_msg_data         => l_msg_data,
4487                   p_uc_header_id     => p_uc_header_id,
4488                   p_instance_id      => p_old_instance_id,
4489                   p_csi_ii_ovn       => p_csi_ii_ovn,
4490                   p_prod_user_flag   => p_prod_user_flag);
4491   IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
4492     RAISE FND_API.G_EXC_ERROR;
4493   ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
4494     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4495   END IF;
4496 
4497   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4498     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
4499                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': After calling remove_instance API',
4500                    'At the middle of the procedure');
4501   END IF;
4502 
4503   --Only need to ensure, the position_reference of the old_instance_id is just
4504   --the one of the new_instance_id. All the other validations will be made in the
4505   --other two called APIs.
4506   OPEN check_relationship_id(p_old_instance_id, p_relationship_id);
4507   IF check_relationship_id%NOTFOUND THEN
4508     FND_MESSAGE.set_name( 'AHL','AHL_UC_POSITION_NOT_SAME' );
4509     FND_MESSAGE.set_token('POSITION', p_relationship_id);
4510     FND_MSG_PUB.add;
4511     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4512   END IF;
4513 
4514   --Call install_existing_instance to install the new instance
4515   install_existing_instance(p_api_version        => 1.0,
4516                             p_init_msg_list      => FND_API.G_FALSE,
4517                             p_commit             => FND_API.G_FALSE,
4518                             p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
4519                             x_return_status      => l_return_status,
4520                             x_msg_count          => l_msg_count,
4521                             x_msg_data           => l_msg_data,
4522                             p_uc_header_id       => p_uc_header_id,
4523                             p_parent_instance_id => p_parent_instance_id,
4524                             p_instance_id        => p_new_instance_id,
4525                             p_instance_number    => p_new_instance_number,
4526                             p_relationship_id    => p_relationship_id,
4527                             p_csi_ii_ovn         => p_csi_ii_ovn,
4528                             p_prod_user_flag     => p_prod_user_flag,
4529                             x_warning_msg_tbl    => x_warning_msg_tbl);
4530   IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
4531     RAISE FND_API.G_EXC_ERROR;
4532   ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
4533     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4534   END IF;
4535 
4536   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4537     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
4538                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': After calling install_existing_instance API and comes to normal execution',
4539                    'At the end of the procedure');
4540   END IF;
4541 
4542   -- Get all the error messages from the previous steps (if any) and raise the appropriate Exception
4543   l_msg_count := FND_MSG_PUB.count_msg;
4544   IF l_msg_count > 0 THEN
4545     x_msg_count := l_msg_count;
4546     RAISE FND_API.G_EXC_ERROR;
4547   END IF;
4548   -- Count and Get messages (optional)
4549   FND_MSG_PUB.count_and_get(
4550     p_encoded  => FND_API.G_FALSE,
4551     p_count    => x_msg_count,
4552     p_data     => x_msg_data);
4553 EXCEPTION
4554   WHEN FND_API.G_EXC_ERROR THEN
4555     ROLLBACK TO swap_instance;
4556     x_return_status := FND_API.G_RET_STS_ERROR ;
4557     FND_MSG_PUB.count_and_get(
4558       p_encoded  => FND_API.G_FALSE,
4559       p_count    => x_msg_count,
4560       p_data     => x_msg_data);
4561   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4562     ROLLBACK TO swap_instance;
4563     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4564     FND_MSG_PUB.count_and_get(
4565       p_encoded  => FND_API.G_FALSE,
4566       p_count    => x_msg_count,
4567       p_data     => x_msg_data);
4568   WHEN OTHERS THEN
4569     ROLLBACK TO swap_instance;
4570     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4571     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
4572     THEN
4573       FND_MSG_PUB.add_exc_msg(
4574         p_pkg_name         => G_PKG_NAME,
4575         p_procedure_name   => l_api_name,
4576         p_error_text       => SUBSTRB(SQLERRM,1,240));
4577     END IF;
4578     FND_MSG_PUB.count_and_get(
4579       p_encoded  => FND_API.G_FALSE,
4580       p_count    => x_msg_count,
4581       p_data     => x_msg_data);
4582 END;
4583 
4584 --****************************************************************************
4585 -- Procedure for getting all instances that are available in sub inventory and
4586 -- available for installation at a particular UC position.
4587 -- Adithya added for OGMA issue # 86 FP
4588 --****************************************************************************
4589 PROCEDURE Get_Avail_Subinv_Instances(
4590   p_api_version            IN  NUMBER := 1.0,
4591   p_init_msg_list          IN  VARCHAR2 := FND_API.G_FALSE,
4592   p_validation_level       IN  NUMBER := FND_API.G_VALID_LEVEL_FULL,
4593   x_return_status          OUT NOCOPY VARCHAR2,
4594   x_msg_count              OUT NOCOPY NUMBER,
4595   x_msg_data               OUT NOCOPY VARCHAR2,
4596   p_relationship_id        IN  NUMBER,
4597   p_item_number            IN  VARCHAR2 :='%',
4598   p_serial_number          IN  VARCHAR2 :='%',
4599   p_instance_number        IN  VARCHAR2 :='%',
4600   p_workorder_id           IN  NUMBER := NULL, --required by Part Changes
4601   p_start_row_index        IN  NUMBER,
4602   p_max_rows               IN  NUMBER,
4603   x_avail_subinv_instance_tbl OUT NOCOPY available_instance_tbl_type
4604 )
4605 IS
4606 
4607 
4608 --Cursor for getting visit details
4609 
4610 CURSOR c_get_visit_details(c_workorder_id NUMBER)
4611 IS
4612 SELECT
4613   VST.visit_id,
4614   VST.project_id,
4615   VST.inv_locator_id,
4616   AWO.wip_entity_id
4617 FROM
4618   AHL_VISITS_B VST,
4619   AHL_WORKORDERS AWO
4620 WHERE
4621       VST.status_code NOT IN ('DELETED', 'CANCELLED')
4622       AND AWO.visit_id = VST.visit_id
4623       AND AWO.workorder_id = c_workorder_id;
4624 
4625 l_visit_details_rec c_get_visit_details%ROWTYPE;
4626 
4627 
4628 CURSOR c_get_subinv_inst(c_relationship_id NUMBER,
4629                        c_item_number VARCHAR2,
4630                        c_instance_number VARCHAR2,
4631                        c_serial_number VARCHAR2,
4632                        c_wip_job_id NUMBER,
4633                        c_project_id NUMBER,
4634                        c_inv_locator_id NUMBER
4635                        )
4636 IS
4637     SELECT C.instance_id,
4638            C.instance_number,
4639            C.inventory_item_id,
4640            C.inv_master_organization_id,
4641            C.quantity,
4642            C.inventory_revision,
4643            C.unit_of_measure uom_code,
4644            C.inv_subinventory_name,
4645            C.inv_locator_id,
4646            to_number(NULL) uc_header_id
4647       FROM csi_item_instances C,
4648            mtl_system_items_kfv M,
4649            ahl_mc_relationships R,
4650            ahl_item_associations_b A
4651      WHERE C.inventory_item_id = M.inventory_item_id
4652        AND C.inv_master_organization_id = M.organization_id
4653        AND R.item_group_id = A.item_group_id
4654        AND C.inventory_item_id = A.inventory_item_id
4655        AND R.relationship_id = c_relationship_id
4656        AND trunc(nvl(R.active_start_date, SYSDATE)) <= trunc(SYSDATE)
4657        AND trunc(nvl(R.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
4658        AND trunc(nvl(C.active_start_date, SYSDATE)) <= trunc(SYSDATE)
4659        AND trunc(nvl(C.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
4660        AND C.location_type_code IN ('INVENTORY')
4661        --AND C.location_type_code NOT IN ('PO','IN-TRANSIT','PROJECT','INVENTORY')
4662        AND A.interchange_type_code IN ('1-WAY INTERCHANGEABLE', '2-WAY INTERCHANGEABLE')
4663        AND (A.revision IS NULL OR A.revision = C.inventory_revision) --Added by Jerry on 03/31/2005
4664        --
4665        -- not installed in any position so far.
4666        --
4667        AND NOT EXISTS (
4668 			 SELECT 1
4669 			  FROM csi_ii_relationships i1
4670 			 WHERE i1.subject_id = C.instance_id
4671 			   AND i1.relationship_type_code = 'COMPONENT-OF'
4672 			   AND trunc(nvl(i1.active_start_date, SYSDATE)) <= trunc(SYSDATE)
4673 			   AND trunc(nvl(i1.active_end_date, SYSDATE+1)) >trunc(SYSDATE)
4674                        )
4675        --
4676        -- its not issued to any workorder already.
4677        --
4678        AND C.wip_job_id IS NULL
4679        --
4680        -- Its not in the top node of any UC.
4681        --
4682        AND NOT EXISTS (
4683                         SELECT 1
4684                          FROM ahl_unit_config_headers H
4685                         WHERE H.csi_item_instance_id = C.instance_id
4686                           AND trunc(nvl(H.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
4687                       )
4688        --
4689        -- and it satisfies other checks like serial number, instance and item number that are passed
4690        --
4691        AND upper(M.concatenated_segments) LIKE nvl(c_item_number,'%')
4692        AND upper(C.instance_number) LIKE nvl(c_instance_number,'%')
4693        AND upper(nvl(C.serial_number, '%')) LIKE nvl(c_serial_number,'%')
4694        AND M.organization_id IN (SELECT mp.master_organization_id
4695                                    FROM mtl_parameters mp, org_organization_definitions ood
4696                                   WHERE mp.organization_Id = ood.organization_id
4697                                   -- jaramana on Feb 14, 2008
4698                                   -- Removed reference to CLIENT_INFO
4699                                   AND NVL(ood.operating_unit, mo_global.get_current_org_id()) = mo_global.get_current_org_id())
4700        AND C.inv_locator_id IN (
4701 	                            SELECT
4702 	                              ILOC.inventory_location_id
4703 	                            FROM
4704 	                              -- jaramana on Feb 14, 2008 for bug 6819370
4705 	                              -- Changed MTL_ITEM_LOCATIONS_KFV to MTL_ITEM_LOCATIONS
4706 	                              MTL_ITEM_LOCATIONS ILOC,
4707 	                              AHL_VISITS_B VST
4708 	                            WHERE
4709 	                              ILOC.subinventory_code = C.inv_subinventory_name
4710 	                              AND ILOC.organization_id = C.inv_organization_id
4711 	                              AND (ILOC.end_date_active IS NULL OR ILOC.end_date_active >= SYSDATE)
4712 	                              AND ILOC.segment19 = c_project_id
4713 	                              AND ILOC.physical_location_id = c_inv_locator_id
4714 	                       )
4715 
4716       AND EXISTS(--If serial number is present then check the status is "in stores"
4717                  (SELECT
4718 		   'X'
4719 		  FROM
4720 		   MTL_SERIAL_NUMBERS MSLN,
4721 		   MFG_LOOKUPS SL
4722 		  WHERE
4723 		   C.serial_number is not null
4724 		   AND MSLN.serial_number = C.serial_number
4725 		   AND MSLN.inventory_item_id = C.inventory_item_id
4726 		   AND MSLN.CURRENT_ORGANIZATION_ID = C.INV_ORGANIZATION_ID
4727 		   AND MSLN.CURRENT_STATUS  = SL.lookup_code
4728 		   AND SL.lookup_type = 'SERIAL_NUM_STATUS'
4729 		   AND MSLN.current_status = '3' -- "in stores"
4730 		  )
4731 		  UNION
4732 		  --If serial number not present then check on hand quantity > 0
4733 		  (
4734 		   SELECT
4735 		    'X'
4736 		   FROM
4737 		    MTL_ONHAND_QUANTITIES MOQ
4738 		   WHERE
4739 		    C.serial_number is null
4740 		    AND MOQ.inventory_item_id = C.inventory_item_id
4741 		    AND MOQ.ORGANIZATION_ID = C.INV_ORGANIZATION_ID
4742   		    AND MOQ.TRANSACTION_QUANTITY > 0
4743 		  )
4744 		)
4745 
4746 UNION ALL
4747 --
4748 -- A position can include alternate subconfigurations.
4749 -- This part of select clause is for picking top node instances of all alternate subconfigs.
4750 --
4751         SELECT C.instance_id,
4752                C.instance_number,
4753                C.inventory_item_id,
4754                C.inv_master_organization_id,
4755                C.quantity,
4756                C.inventory_revision,
4757                C.unit_of_measure uom_code,
4758                C.inv_subinventory_name,
4759                C.inv_locator_id,
4760                U.uc_header_id uc_header_id
4761           FROM ahl_unit_config_headers_v U,
4762                csi_item_instances C,
4763                mtl_system_items_kfv M
4764          WHERE U.csi_instance_id = C.instance_id
4765            AND C.inventory_item_id = M.inventory_item_id
4766            AND C.inv_master_organization_id = M.organization_id
4767            AND trunc(nvl(U.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
4768            AND trunc(nvl(C.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
4769            AND C.location_type_code IN ('INVENTORY')
4770            --
4771            -- Check to see this UC is a subconfig
4772            --
4773            AND EXISTS (
4774                         SELECT 1
4775                          FROM ahl_mc_config_relations R
4776                         WHERE R.mc_header_id = U.mc_header_id
4777                           AND R.relationship_id = c_relationship_id
4778                           AND trunc(nvl(R.active_start_date, SYSDATE)) <= trunc(SYSDATE)
4779                           AND trunc(nvl(R.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
4780                       )
4781 	   --
4782 	   -- sub config is in valid status
4783 	   --
4784            AND (
4785                  U.parent_instance_id IS NULL
4786                  AND U.uc_status_code in ('COMPLETE', 'INCOMPLETE')
4787 	       )
4788 	   --
4789 	   -- its not issued to any workorder already.
4790 	   --
4791 	   AND C.wip_job_id IS NULL
4792 	   --
4793 	   -- its not a parent for any other mc position.
4794 	   --
4795            AND NOT EXISTS (
4796                             SELECT 1
4797                              FROM ahl_mc_relationships MR
4798                             WHERE MR.parent_relationship_id = c_relationship_id
4799                               AND trunc(nvl(MR.active_start_date, SYSDATE)) <= trunc(SYSDATE)
4800                               AND trunc(nvl(MR.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
4801                            )
4802 	   --
4803 	   -- and it satisfies other checks like serial number, instance and item number that are passed
4804 	   --
4805            AND upper(M.concatenated_segments) LIKE nvl(c_item_number,'%')
4806            AND upper(C.instance_number) LIKE nvl(c_instance_number,'%')
4807            AND upper(nvl(C.serial_number, '%')) LIKE nvl(c_serial_number,'%')
4808            AND M.organization_id IN (SELECT mp.master_organization_id
4809                                        FROM mtl_parameters mp, org_organization_definitions ood
4810                                       WHERE mp.organization_Id = ood.organization_id
4811                                       -- jaramana on Feb 14, 2008
4812                                       -- Removed reference to CLIENT_INFO
4813                                       AND NVL(ood.operating_unit, mo_global.get_current_org_id()) = mo_global.get_current_org_id())
4814 	   AND C.inv_locator_id IN (
4815 	                            SELECT
4816 	                              ILOC.inventory_location_id
4817 	                            FROM
4818 	                              -- jaramana on Feb 14, 2008 for bug 6819370
4819 	                              -- Changed MTL_ITEM_LOCATIONS_KFV to MTL_ITEM_LOCATIONS
4820 	                              MTL_ITEM_LOCATIONS ILOC,
4821 	                              AHL_VISITS_B VST
4822 	                            WHERE
4823 	                              ILOC.subinventory_code = C.inv_subinventory_name
4824 	                              AND ILOC.organization_id = C.inv_organization_id
4825 	                              AND (ILOC.end_date_active IS NULL OR ILOC.end_date_active >= SYSDATE)
4826 	                              AND ILOC.segment19 = c_project_id
4827 	                              AND ILOC.physical_location_id = c_inv_locator_id
4828 	                           )
4829       AND EXISTS(--If serial number is present then check the status is "in stores"
4830                  (SELECT
4831 		   'X'
4832 		  FROM
4833 		   MTL_SERIAL_NUMBERS MSLN,
4834 		   MFG_LOOKUPS SL
4835 		  WHERE
4836 		   C.serial_number is not null
4837 		   AND MSLN.serial_number = C.serial_number
4838 		   AND MSLN.inventory_item_id = C.inventory_item_id
4839 		   AND MSLN.CURRENT_ORGANIZATION_ID = C.INV_ORGANIZATION_ID
4840 		   AND MSLN.CURRENT_STATUS  = SL.lookup_code
4841 		   AND SL.lookup_type = 'SERIAL_NUM_STATUS'
4842 		   AND MSLN.current_status = '3' -- "in stores"
4843 		  )
4844 		  UNION
4845 		  --If serial number not present then check on hand quantity > 0
4846 		  (
4847 		   SELECT
4848 		    'X'
4849 		   FROM
4850 		    MTL_ONHAND_QUANTITIES MOQ
4851 		   WHERE
4852 		    C.serial_number is null
4853 		    AND MOQ.inventory_item_id = C.inventory_item_id
4854 		    AND MOQ.ORGANIZATION_ID = C.INV_ORGANIZATION_ID
4855   		    AND MOQ.TRANSACTION_QUANTITY > 0
4856 		  )
4857 		);
4858 
4859 -- Cursor for validating relationship
4860 CURSOR check_relationship_id
4861 IS
4862 SELECT
4863    relationship_id
4864 FROM
4865    ahl_mc_relationships
4866 WHERE
4867     relationship_id = p_relationship_id
4868     AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
4869     AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
4870     AND mc_header_id IN (
4871                          SELECT
4872                             mc_header_id
4873                          FROM
4874                             ahl_mc_headers_b
4875                          WHERE
4876                              config_status_code = 'COMPLETE'
4877                         );
4878 -- Cursor for checking parent instance.
4879 CURSOR check_parent_instance(c_instance_id NUMBER)
4880 IS
4881   --Parent instance could be either in ahl_unit_config_headers(top node) or in csi_ii_relationships
4882   --(as the subject_id)
4883 SELECT
4884   'x'
4885 FROM
4886    ahl_unit_config_headers
4887 WHERE
4888    csi_item_instance_id = c_instance_id
4889    AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
4890 
4891 UNION ALL
4892 
4893 SELECT
4894     'x'
4895 FROM
4896     csi_ii_relationships
4897 WHERE
4898     subject_id = c_instance_id
4899     AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
4900     AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE);
4901 
4902 -- Cursor for getting top node instance
4903 CURSOR get_top_unit_instance(c_instance_id NUMBER)
4904 IS
4905 SELECT
4906     object_id
4907 FROM
4908     csi_ii_relationships
4909 WHERE
4910     object_id NOT IN (SELECT subject_id
4911                                FROM csi_ii_relationships
4912                               WHERE relationship_type_code = 'COMPONENT-OF'
4913                                 AND trunc(nvl(active_start_date,SYSDATE)) <= trunc(SYSDATE)
4914                                 AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE))
4915 START WITH subject_id = c_instance_id
4916        AND relationship_type_code = 'COMPONENT-OF'
4917        AND trunc(nvl(active_start_date,SYSDATE)) <= trunc(SYSDATE)
4918        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
4919 CONNECT BY subject_id = PRIOR object_id
4920        AND relationship_type_code = 'COMPONENT-OF'
4921        AND trunc(nvl(active_start_date,SYSDATE)) <= trunc(SYSDATE)
4922        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
4923 
4924 -- Cursor for getting UC status.
4925 CURSOR get_uc_status(c_instance_id NUMBER) IS
4926     SELECT unit_config_status_code
4927       FROM ahl_unit_config_headers
4928      WHERE csi_item_instance_id = c_instance_id
4929        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
4930 
4931   CURSOR ahl_instance_details (c_csi_item_instance_id IN NUMBER) IS
4932     SELECT A.csi_item_instance_id,
4933            A.csi_object_version csi_object_version_number,
4934            A.item_number,
4935            A.item_description,
4936            A.csi_instance_number,
4937            A.inventory_item_id,
4938            A.inventory_org_id,
4939            A.organization_code,
4940            A.serial_number,
4941            A.revision,
4942            A.lot_number,
4943            A.uom_code,
4944            A.quantity,
4945            A.install_date,
4946            A.mfg_date,
4947            A.location_description,
4948            A.party_type,
4949            A.owner_id,
4950            A.owner_number,
4951            A.owner_name,
4952            A.csi_location_id owner_site_id,
4953            A.owner_site_number,
4954            A.csi_party_object_version_num,
4955            A.status,
4956            A.condition,
4957            A.wip_entity_name,
4958            B.uc_header_id,
4959            B.uc_name,
4960            B.uc_status,
4961            B.mc_header_id,
4962            B.mc_name,
4963            B.mc_revision,
4964            B.mc_status,
4965            B.position_ref,
4966            B.root_uc_header_id
4967       FROM ahl_unit_installed_details_v A,
4968            ahl_unit_config_headers_v B
4969      WHERE csi_item_instance_id = c_csi_item_instance_id
4970        AND A.csi_item_instance_id = B.csi_instance_id (+)
4971        AND trunc(nvl(B.active_end_date (+), SYSDATE+1)) > trunc(SYSDATE);
4972   l_instance_details_rec ahl_instance_details%ROWTYPE;
4973 
4974 CURSOR get_priority (c_item_association_id IN NUMBER) IS
4975     SELECT priority
4976     FROM ahl_item_associations_b
4977     WHERE item_association_id = c_item_association_id;
4978 
4979 CURSOR get_csi_ii_relationship_ovn (c_instance_id NUMBER) IS
4980     SELECT object_version_number
4981       FROM csi_ii_relationships
4982      WHERE subject_id = c_instance_id
4983        AND position_reference IS NULL
4984        AND relationship_type_code = 'COMPONENT-OF'
4985        AND trunc(nvl(active_start_date,SYSDATE)) <= trunc(SYSDATE)
4986        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
4987   l_csi_ii_relationship_ovn   NUMBER;
4988 
4989 CURSOR c_get_locator_segments(c_inv_location_id NUMBER)
4990 IS
4991 SELECT
4992   concatenated_segments
4993 FROM
4994   MTL_ITEM_LOCATIONS_KFV
4995 WHERE
4996   inventory_location_id = c_inv_location_id;
4997 
4998 -- declare all local variables here
4999   l_api_name       CONSTANT   VARCHAR2(30) := 'Get_Avail_Subinv_Instances';
5000   l_api_version    CONSTANT   NUMBER       := 1.0;
5001   l_relationship_id           NUMBER;
5002   l_item_assoc_id             NUMBER;
5003   l_priority                  NUMBER;
5004   i                           NUMBER;
5005   j                           NUMBER;
5006   l_dummy_char                VARCHAR2(1);
5007   l_top_uc_status             ahl_unit_config_headers.unit_config_status_code%TYPE;
5008   l_top_instance_id           NUMBER;
5009   l_status                    fnd_lookup_values_vl.meaning%TYPE;
5010   l_msg_count                 NUMBER;
5011 
5012 BEGIN
5013 
5014      -- 0. Intial logic for the API.
5015      -------------------------------
5016      -- Initialize API return status to success
5017      x_return_status := FND_API.G_RET_STS_SUCCESS;
5018 
5019      -- Standard call to check for call compatibility.
5020      IF NOT FND_API.compatible_api_call(
5021        l_api_version,
5022        p_api_version,
5023        l_api_name,
5024        G_PKG_NAME)
5025      THEN
5026        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5027      END IF;
5028 
5029      -- Initialize message list if p_init_msg_list is set to TRUE.
5030      IF FND_API.to_boolean( p_init_msg_list ) THEN
5031        FND_MSG_PUB.initialize;
5032      END IF;
5033 
5034      IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5035    	FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
5036                       'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Begin API',
5037    			       'At the start of the procedure');
5038      END IF;
5039 
5040 
5041      IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5042 	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5043 		     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5044 			     'Logging API inputs');
5045 	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5046 		     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5047 			     '******************');
5048 	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5049 		     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5050 			     'p_relationship_id->'||p_relationship_id);
5051 	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5052 		     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5053 			     'p_item_number->'||p_item_number);
5054 	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5055 		     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5056 			     'p_serial_number->'||p_serial_number);
5057 	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5058 		     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5059 			     'p_instance_number->'||p_instance_number);
5060 	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5061 		     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5062 			     'p_workorder_id->'||p_workorder_id);
5063 	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5064 		     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5065 			     'p_start_row_index->'||p_start_row_index);
5066      	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5067      		     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5068      			     'p_max_rows->'||p_max_rows);
5069      END IF;
5070 
5071      --1. Do all mandatory validation here.
5072      --------------------------------------
5073      -- Work Order id is mandatory parmater. Throw error if its not passed.
5074      IF p_workorder_id IS NULL THEN
5075         -- Workorder is mandatory. Throw an error.
5076          FND_MESSAGE.set_name( 'AHL','AHL_COM_PARAM_MISSING' );-- check the message name here.
5077          FND_MSG_PUB.add;
5078          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5079      END IF;
5080 
5081      -- 1.b get all visit details and its validations
5082      OPEN c_get_visit_details(p_workorder_id);
5083      FETCH c_get_visit_details INTO l_visit_details_rec;
5084      CLOSE c_get_visit_details;
5085 
5086      -- 1.a validation corresponding to Work Order
5087      IF l_visit_details_rec.wip_entity_id IS NULL THEN
5088          FND_MESSAGE.set_name( 'AHL','AHL_UC_WORKORDER_INVALID' );
5089          FND_MESSAGE.set_token('WORKORDER', p_workorder_id);
5090          FND_MSG_PUB.add;
5091          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5092      END IF;
5093 
5094      IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5095 	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5096 		     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5097 			     'Visit Id derived->'||l_visit_details_rec.visit_id);
5098 	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5099 		     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5100 			     'project_id derived ->'||l_visit_details_rec.project_id);
5101 	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5102 		     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5103 			     'inv_locator_id setup at visit level->'||l_visit_details_rec.inv_locator_id);
5104      END IF;
5105 
5106      -- Rest of the logic is not applicable if visit doesnt have subinventory or inv_locator_id defined
5107      IF l_visit_details_rec.inv_locator_id IS NULL THEN
5108        RETURN;
5109      END IF;
5110 
5111      -- 1.c validation corresponding to relationship
5112      OPEN check_relationship_id;
5113      FETCH check_relationship_id INTO l_relationship_id;
5114      CLOSE check_relationship_id;
5115 
5116      IF l_relationship_id IS NULL THEN
5117        FND_MESSAGE.set_name( 'AHL','AHL_UC_POSITION_INVALID' );
5118        FND_MESSAGE.set_token('POSITION', p_relationship_id);
5119        FND_MSG_PUB.add;
5120        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5121      END IF;
5122 
5123      i := 0;
5124      j := 0;
5125      -- 2. Fetch all applicable instances and populate it in the out variable
5126      FOR  l_subinv_inst_rec IN c_get_subinv_inst(p_relationship_id,
5127                         			 p_item_number,
5128                         			 p_instance_number,
5129                         			 p_serial_number,
5130                         			 l_visit_details_rec.wip_entity_id,
5131 						 l_visit_details_rec.project_id,
5132 						 l_visit_details_rec.inv_locator_id)
5133      LOOP
5134 
5135 	    --If the instance is not a unit then, call procedure to validate whether thecorresponding inventory
5136 	    --can be assigned to that position
5137 
5138 	    IF l_subinv_inst_rec.uc_header_id IS NULL THEN
5139         AHL_UTIL_UC_PKG.validate_for_position(p_mc_relationship_id   => p_relationship_id,
5140                 p_inventory_id         => l_subinv_inst_rec.inventory_item_id,
5141                 p_organization_id      => l_subinv_inst_rec.inv_master_organization_id,
5142                 p_quantity             => l_subinv_inst_rec.quantity,
5143                 p_revision             => l_subinv_inst_rec.inventory_revision,
5144                 p_uom_code             => l_subinv_inst_rec.uom_code,
5145                 p_position_ref_meaning => NULL,
5146                 x_item_assoc_id        => l_item_assoc_id,
5147                 --Added by mpothuku on 17-May-2007 to fix the OGMA Issue 105.
5148                 p_ignore_quant_vald    => 'Y');
5149 	    END IF;
5150 
5151 	    IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5152 		  FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5153 			     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5154 				     'After position validation for l_subinv_inst_rec.instance_id->'||l_subinv_inst_rec.instance_id);
5155 		  FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5156 			     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5157 				     'fnd_msg_pub.count_msg->'||fnd_msg_pub.count_msg);
5158 	    END IF;
5159 
5160 	    IF (fnd_msg_pub.count_msg = 0) THEN
5161 	      i := i + 1;
5162 	      IF (i >= p_start_row_index AND i < p_start_row_index + p_max_rows) THEN
5163 		OPEN ahl_instance_details(l_subinv_inst_rec.instance_id);
5164 		FETCH ahl_instance_details INTO l_instance_details_rec;
5165 
5166                 IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5167 		  FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5168 			     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5169 				     'l_instance_details_rec.csi_item_instance_id->'||l_instance_details_rec.csi_item_instance_id);
5170 		END IF;
5171 
5172 		IF (ahl_instance_details%FOUND) THEN
5173 		  j := j + 1;
5174 		  x_avail_subinv_instance_tbl(j).csi_item_instance_id := l_instance_details_rec.csi_item_instance_id;
5175 		  x_avail_subinv_instance_tbl(j).csi_object_version_number := l_instance_details_rec.csi_object_version_number;
5176 		  x_avail_subinv_instance_tbl(j).item_number := l_instance_details_rec.item_number;
5177 		  x_avail_subinv_instance_tbl(j).item_description := l_instance_details_rec.item_description;
5178 		  x_avail_subinv_instance_tbl(j).csi_instance_number := l_instance_details_rec.csi_instance_number;
5179 		  x_avail_subinv_instance_tbl(j).organization_code := l_instance_details_rec.organization_code;
5180 		  x_avail_subinv_instance_tbl(j).inventory_item_id := l_instance_details_rec.inventory_item_id;
5181 		  x_avail_subinv_instance_tbl(j).inventory_org_id := l_instance_details_rec.inventory_org_id;
5182 		  x_avail_subinv_instance_tbl(j).serial_number := l_instance_details_rec.serial_number;
5183 		  x_avail_subinv_instance_tbl(j).revision := l_instance_details_rec.revision;
5184 		  x_avail_subinv_instance_tbl(j).lot_number := l_instance_details_rec.lot_number;
5185 		  x_avail_subinv_instance_tbl(j).uom_code := l_instance_details_rec.uom_code;
5186 		  x_avail_subinv_instance_tbl(j).quantity := l_instance_details_rec.quantity;
5187 		  x_avail_subinv_instance_tbl(j).install_date := l_instance_details_rec.install_date;
5188 		  x_avail_subinv_instance_tbl(j).mfg_date := l_instance_details_rec.mfg_date;
5189 		  -- Verify if location description return correct details else
5190 		  -- the logic in ahl_util_uc_pkg.getcsi_locationDesc need to be modified or
5191 		  -- a new cursor need to be opened to get correct description.
5192 		  x_avail_subinv_instance_tbl(j).location_description := l_instance_details_rec.location_description;
5193 		  x_avail_subinv_instance_tbl(j).party_type := l_instance_details_rec.party_type;
5194 		  x_avail_subinv_instance_tbl(j).owner_site_number := l_instance_details_rec.owner_site_number;
5195 		  x_avail_subinv_instance_tbl(j).owner_site_ID := l_instance_details_rec.owner_site_ID;
5196 		  x_avail_subinv_instance_tbl(j).owner_number := l_instance_details_rec.owner_number;
5197 		  x_avail_subinv_instance_tbl(j).owner_name := l_instance_details_rec.owner_name;
5198 		  x_avail_subinv_instance_tbl(j).owner_ID := l_instance_details_rec.owner_ID;
5199 		  x_avail_subinv_instance_tbl(j).csi_party_object_version_num := l_instance_details_rec.csi_party_object_version_num;
5200 		  x_avail_subinv_instance_tbl(j).status := l_instance_details_rec.status;
5201 		  x_avail_subinv_instance_tbl(j).condition := l_instance_details_rec.condition;
5202 		  x_avail_subinv_instance_tbl(j).wip_entity_name := l_instance_details_rec.wip_entity_name;
5203 		  x_avail_subinv_instance_tbl(j).uc_header_id := l_instance_details_rec.uc_header_id;
5204 		  x_avail_subinv_instance_tbl(j).uc_name := l_instance_details_rec.uc_name;
5205 		  --Modified on 02/26/2004 in case the status inconsistency occurred between an extra sub-unit and
5206 		  --its root unit
5207 
5208 		  IF l_instance_details_rec.root_uc_header_id IS NOT NULL THEN
5209 		    SELECT uc_status INTO l_status
5210 		      FROM ahl_unit_config_headers_v
5211 		     WHERE uc_header_id = l_instance_details_rec.root_uc_header_id;
5212 		    x_avail_subinv_instance_tbl(j).uc_status := l_status;
5213 		  ELSE
5214 		    x_avail_subinv_instance_tbl(j).uc_status := l_instance_details_rec.uc_status;
5215 		  END IF;
5216 		  x_avail_subinv_instance_tbl(j).mc_header_id := l_instance_details_rec.mc_header_id;
5217 		  x_avail_subinv_instance_tbl(j).mc_name := l_instance_details_rec.mc_name;
5218 		  x_avail_subinv_instance_tbl(j).mc_revision := l_instance_details_rec.mc_revision;
5219 		  x_avail_subinv_instance_tbl(j).mc_status := l_instance_details_rec.mc_status;
5220 		  x_avail_subinv_instance_tbl(j).position_ref := l_instance_details_rec.position_ref;
5221 		  --Get priority
5222 
5223 		  OPEN get_priority(l_item_assoc_id);
5224 		  FETCH get_priority INTO l_priority;
5225 		  CLOSE get_priority;
5226 		  x_avail_subinv_instance_tbl(j).priority := l_priority;
5227 		  --If the instance is an extra sibling node, then get its object version number in
5228 		  --table csi_ii_relationship
5229 		  OPEN get_csi_ii_relationship_ovn(x_avail_subinv_instance_tbl(j).csi_item_instance_id);
5230 		  FETCH get_csi_ii_relationship_ovn INTO l_csi_ii_relationship_ovn;
5231 		  IF get_csi_ii_relationship_ovn%FOUND THEN
5232 		    x_avail_subinv_instance_tbl(j).csi_ii_relationship_ovn := l_csi_ii_relationship_ovn;
5233 		  ELSE
5234 		    x_avail_subinv_instance_tbl(j).csi_ii_relationship_ovn := NULL;
5235 		  END IF;
5236 		  CLOSE get_csi_ii_relationship_ovn;
5237 
5238 		  x_avail_subinv_instance_tbl(j).subinventory_code :=  l_subinv_inst_rec.inv_subinventory_name;
5239 		  x_avail_subinv_instance_tbl(j).inventory_locator_id :=  l_subinv_inst_rec.inv_locator_id;
5240 
5241 		  OPEN c_get_locator_segments(l_subinv_inst_rec.inv_locator_id);
5242 		  FETCH c_get_locator_segments INTO x_avail_subinv_instance_tbl(j).locator_segments;
5243 		  CLOSE c_get_locator_segments;
5244 
5245 		END IF;
5246 		CLOSE ahl_instance_details;
5247 	      END IF;
5248 	    END IF;
5249 	    fnd_msg_pub.initialize;
5250    END LOOP;
5251 
5252   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5253 	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5254 		     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5255 			     'x_avail_subinv_instance_tbl count->'||x_avail_subinv_instance_tbl.COUNT);
5256   END IF;
5257 
5258   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5259 	FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
5260                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': End API',
5261 			       'After normal execution');
5262   END IF;
5263 
5264   -- Get all the error messages from the previous steps (if any) and raise the appropriate Exception
5265   l_msg_count := FND_MSG_PUB.count_msg;
5266   IF l_msg_count > 0 THEN
5267     x_msg_count := l_msg_count;
5268     RAISE FND_API.G_EXC_ERROR;
5269   END IF;
5270 
5271   -- Count and Get messages (optional)
5272   FND_MSG_PUB.count_and_get(
5273     p_encoded  => FND_API.G_FALSE,
5274     p_count    => x_msg_count,
5275     p_data     => x_msg_data);
5276 
5277 EXCEPTION
5278 
5279   WHEN FND_API.G_EXC_ERROR THEN
5280     x_return_status := FND_API.G_RET_STS_ERROR ;
5281     FND_MSG_PUB.count_and_get(
5282       p_encoded  => FND_API.G_FALSE,
5283       p_count    => x_msg_count,
5284       p_data     => x_msg_data);
5285 
5286   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5287     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5288     FND_MSG_PUB.count_and_get(
5289       p_encoded  => FND_API.G_FALSE,
5290       p_count    => x_msg_count,
5291       p_data     => x_msg_data);
5292 
5293   WHEN OTHERS THEN
5294     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5295     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
5296     THEN
5297       FND_MSG_PUB.add_exc_msg(
5298         p_pkg_name         => G_PKG_NAME,
5299         p_procedure_name   => l_api_name,
5300         p_error_text       => SUBSTRB(SQLERRM,1,240));
5301     END IF;
5302     FND_MSG_PUB.count_and_get(
5303       p_encoded  => FND_API.G_FALSE,
5304       p_count    => x_msg_count,
5305       p_data     => x_msg_data);
5306 
5307 END Get_Avail_Subinv_Instances;
5308 
5309 -- Define procedure get_available_instances
5310 -- This API is used to get all the available instances for a given node in a UC tree.
5311 
5312 PROCEDURE get_available_instances(
5313   p_api_version            IN  NUMBER := 1.0,
5314   p_init_msg_list          IN  VARCHAR2 := FND_API.G_FALSE,
5315   --p_commit                 IN  VARCHAR2 := FND_API.G_FALSE,
5316   p_validation_level       IN  NUMBER := FND_API.G_VALID_LEVEL_FULL,
5317   x_return_status          OUT NOCOPY VARCHAR2,
5318   x_msg_count              OUT NOCOPY NUMBER,
5319   x_msg_data               OUT NOCOPY VARCHAR2,
5320   p_parent_instance_id     IN  NUMBER, --in order to include the extra siblings
5321   p_relationship_id        IN  NUMBER,
5322   p_item_number            IN  VARCHAR2 :='%',
5323   p_serial_number          IN  VARCHAR2 :='%',
5324   p_instance_number        IN  VARCHAR2 :='%',
5325   p_workorder_id           IN  NUMBER := NULL, --required by Part Changes
5326   p_start_row_index        IN  NUMBER,
5327   p_max_rows               IN  NUMBER,
5328   x_available_instance_tbl OUT NOCOPY available_instance_tbl_type,
5329   x_tbl_count              OUT NOCOPY NUMBER)
5330 IS
5331   l_api_name       CONSTANT   VARCHAR2(30) := 'get_available_instances';
5332   l_api_version    CONSTANT   NUMBER       := 1.0;
5333   l_return_status             VARCHAR2(1);
5334   l_msg_count                 NUMBER;
5335   l_relationship_id           NUMBER;
5336 
5337   -- SATHAPLI Bug# 4912576 fix
5338   -- Code pertaining to Org check has been removed from here
5339   -- Please refer earlier version for the details
5340 
5341   l_instance_id               NUMBER;
5342   l_inventory_item_id         NUMBER;
5343   l_instance_number           csi_item_instances.instance_number%TYPE := upper(nvl(p_instance_number, '%'));
5344   l_inventory_org_id          NUMBER;
5345   l_quantity                  NUMBER;
5346   l_inventory_revision        VARCHAR2(3);
5347   l_uom_code                  VARCHAR2(3);
5348   l_item_assoc_id             NUMBER;
5349   l_priority                  NUMBER;
5350   l_item_number               mtl_system_items_kfv.concatenated_segments%TYPE :=upper(nvl(p_item_number, '%'));
5351   l_serial_number             csi_item_instances.serial_number%TYPE := upper(nvl(p_serial_number, '%'));
5352   l_uc_header_id              NUMBER;
5353   i                           NUMBER;
5354   j                           NUMBER;
5355   l_dummy_char                VARCHAR2(1);
5356   l_top_uc_status             ahl_unit_config_headers.unit_config_status_code%TYPE;
5357   l_top_instance_id           NUMBER;
5358   l_status                    fnd_lookup_values_vl.meaning%TYPE;
5359   l_ignore_quant_vald         VARCHAR2(1);
5360 
5361   -- SATHAPLI Bug# 4912576 fix
5362   -- Code pertaining to Org check has been removed from here
5363   -- cursor get_instance1 is not used anymore
5364   -- Please refer earlier version for the details
5365 
5366   --Cursor to pick up all instances that match the item association setup. But units can only
5367   --be installed in MC leaf node which has associated sub-MC, in other words, any units
5368   --can't be installed in a branch node. Also need to include those sibling extra nodes which
5369   --are item-matched to the position
5370 
5371   CURSOR get_instance2(c_relationship_id NUMBER,
5372                        c_item_number VARCHAR2,
5373                        c_instance_number VARCHAR2,
5374                        c_serial_number VARCHAR2,
5375                        c_wip_job_id NUMBER,
5376                        c_parent_instance_id NUMBER,
5377                        c_uc_status VARCHAR2) IS
5378 
5379   -- SATHAPLI Bug# 4912576 fix::SQLid 14402108
5380   -- The query was changed to the following one for performance improvement.
5381   -- Please refer earlier version for previous query.
5382 SELECT
5383     C.instance_id,
5384     C.instance_number,
5385     C.inventory_item_id,
5386     C.inv_master_organization_id,
5387     C.quantity,
5388     C.inventory_revision,
5389     C.unit_of_measure uom_code,
5390     to_number(NULL) uc_header_id
5391 FROM csi_item_instances C,
5392     mtl_system_items_kfv M,
5393     ahl_mc_relationships R,
5394     ahl_item_associations_b A
5395 WHERE C.inventory_item_id = M.inventory_item_id
5396     AND C.inv_master_organization_id = M.organization_id
5397     AND R.item_group_id = A.item_group_id
5398     AND C.inventory_item_id = A.inventory_item_id
5399     AND R.relationship_id = c_relationship_id
5400     AND trunc(nvl(R.active_start_date, SYSDATE)) <= trunc(SYSDATE)
5401     AND trunc(nvl(R.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5402     AND trunc(nvl(C.active_start_date, SYSDATE)) <= trunc(SYSDATE)
5403     AND trunc(nvl(C.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5404     -- SATHAPLI::FP Bug 7498459, 27-Nov-2008 - For the root position, i.e. for UC header creation, make the inventory
5405     -- instances available, which are not issued to any job. This should be done only for the edit UC flow, i.e.
5406     -- when c_wip_job_id is NULL.
5407     -- AND C.location_type_code NOT IN ('PO','IN-TRANSIT','PROJECT','INVENTORY')
5408     AND C.location_type_code NOT IN ('PO','IN-TRANSIT','PROJECT', (DECODE(c_parent_instance_id, NULL, 'X', 'INVENTORY')))
5409     AND A.interchange_type_code IN ('1-WAY INTERCHANGEABLE', '2-WAY INTERCHANGEABLE')
5410     AND nvl(A.revision, nvl(C.inventory_revision,-1)) = nvl(C.inventory_revision,-1)
5411     AND (
5412         (c_wip_job_id IS NULL -- SATHAPLI, 30-Jan-2008 :: extra nodes should not come up during parts change
5413          AND
5414          EXISTS
5415             (
5416             SELECT 1
5417             FROM csi_ii_relationships i2
5418             WHERE i2.subject_id = C.instance_id
5419                 AND i2.position_reference IS NULL --because parent is not extra
5420                 -- SATHAPLI::FP ER 6504147, 18-Nov-2008
5421                 -- include extra nodes of all the parents uptill root
5422                 -- AND i2.object_id = NVL(c_parent_instance_id, -1)
5423                 AND i2.object_id IN (
5424                                      SELECT i3.object_id
5425                                      FROM   csi_ii_relationships i3
5426                                      START WITH i3.subject_id = nvl(c_parent_instance_id, -1)
5427                                      AND    i3.relationship_type_code = 'COMPONENT-OF'
5428                                      AND    trunc(nvl(i3.active_start_date, SYSDATE)) <= trunc(SYSDATE)
5429                                      AND    trunc(nvl(i3.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5430                                      CONNECT BY i3.subject_id = PRIOR i3.object_id
5431                                      AND    i3.relationship_type_code = 'COMPONENT-OF'
5432                                      AND    trunc(nvl(i3.active_start_date, SYSDATE)) <= trunc(SYSDATE)
5433                                      AND    trunc(nvl(i3.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5434                                      UNION ALL
5435                                      SELECT nvl(c_parent_instance_id, -1)
5436                                      FROM   DUAL
5437                                     )
5438                 AND i2.relationship_type_code = 'COMPONENT-OF'
5439                 AND trunc(nvl(i2.active_start_date, SYSDATE)) <= trunc(SYSDATE)
5440                 AND trunc(nvl(i2.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5441             )
5442         )
5443         OR
5444         (NOT EXISTS
5445             (
5446             SELECT 1
5447             FROM csi_ii_relationships i1
5448             WHERE i1.subject_id = C.instance_id
5449                 AND i1.relationship_type_code = 'COMPONENT-OF'
5450                 AND trunc(nvl(i1.active_start_date, SYSDATE)) <= trunc(SYSDATE)
5451                 AND trunc(nvl(i1.active_end_date, SYSDATE+1)) >trunc(SYSDATE)
5452             )
5453             -- SATHAPLI, 30-Jan-2008 :: If c_wip_job_id is not passed, instances issued to any job should not be fetched.
5454             -- If c_wip_job_id is passed, then fetch only those instances which are issued to this job.
5455             -- AND nvl(C.wip_job_id, -1) = nvl(c_wip_job_id, nvl(C.wip_job_id, -1))
5456             AND ((c_wip_job_id IS NULL AND C.wip_job_id IS NULL)
5457                  OR
5458                  (c_wip_job_id IS NOT NULL AND c_wip_job_id = NVL(C.wip_job_id, -1))
5459                 )
5460         )
5461     )
5462     --This wip_entity check is not necessary for an extra sibling nodes even for
5463     --so just include it here.
5464     AND NOT EXISTS
5465     (
5466     SELECT 1
5467     FROM ahl_unit_config_headers H
5468     WHERE H.csi_item_instance_id = C.instance_id
5469         AND trunc(nvl(H.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5470     )
5471     AND upper(M.concatenated_segments) LIKE c_item_number
5472     AND upper(C.instance_number) LIKE c_instance_number
5473     AND upper(nvl(C.serial_number, '%')) LIKE c_serial_number
5474     AND EXISTS
5475     (
5476      SELECT 'X'
5477      FROM   mtl_parameters mp, inv_organization_info_v io
5478      WHERE  mp.master_organization_id = M.organization_id AND
5479             mp.organization_Id = io.organization_id AND
5480             NVL(io.operating_unit, mo_global.get_current_org_id()) =
5481             mo_global.get_current_org_id()
5482     )
5483     --Plus those units could be installed
5484 UNION ALL
5485 SELECT
5486     C.instance_id,
5487     C.instance_number,
5488     C.inventory_item_id,
5489     C.inv_master_organization_id,
5490     C.quantity,
5491     C.inventory_revision,
5492     C.unit_of_measure uom_code,
5493     U.uc_header_id uc_header_id
5494 FROM (
5495       SELECT UH.unit_config_header_id uc_header_id,
5496              UH.csi_item_instance_id csi_instance_id,
5497              UH.master_config_id mc_header_id,
5498              UH.unit_config_status_code uc_status_code,
5499              UH.active_end_date,
5500              CR.object_id parent_instance_id
5501       FROM   ahl_unit_config_headers UH, csi_ii_relationships CR
5502       WHERE  UH.csi_item_instance_id = CR.subject_id (+) AND
5503              CR.relationship_type_code (+) = 'COMPONENT-OF' AND
5504              trunc(nvl(CR.active_start_date (+), SYSDATE)) <= trunc(SYSDATE) AND
5505              trunc(nvl(CR.active_end_date (+), SYSDATE+1)) > trunc(SYSDATE)
5506      ) U,
5507     csi_item_instances C,
5508     mtl_system_items_kfv M
5509 WHERE U.csi_instance_id = C.instance_id
5510     AND C.inventory_item_id = M.inventory_item_id
5511     AND C.inv_master_organization_id = M.organization_id
5512     AND trunc(nvl(U.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5513     AND trunc(nvl(C.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5514     --Per Barry, if the top instance is expired then this UC is also taken as
5515     --Either JOIN or IN, performance cost is bigger than EXISTS
5516     AND EXISTS
5517     (
5518     SELECT 1
5519     FROM ahl_mc_config_relations R
5520     WHERE R.mc_header_id = U.mc_header_id
5521         AND R.relationship_id = c_relationship_id
5522         AND trunc(nvl(R.active_start_date, SYSDATE)) <= trunc(SYSDATE)
5523         AND trunc(nvl(R.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5524     )
5525     --Either a separate unit or extra sibling subunit
5526     AND (
5527         (
5528             U.parent_instance_id IS NULL
5529             AND decode(U.uc_status_code, 'DRAFT', 'DRAFT',
5530                        'APPROVAL_REJECTED', 'DRAFT',
5531                        'COMPLETE','COMPLETE',
5532                        'INCOMPLETE','COMPLETE', NULL) = c_uc_status
5533             -- SATHAPLI, 30-Jan-2008 :: If c_wip_job_id is not passed, instances issued to any job should not be fetched.
5534             -- If c_wip_job_id is passed, then fetch only those instances which are issued to this job.
5535             -- AND nvl(C.wip_job_id, -1) = nvl(c_wip_job_id, nvl(C.wip_job_id, -1))
5536             AND ((c_wip_job_id IS NULL AND C.wip_job_id IS NULL)
5537                  OR
5538                  (c_wip_job_id IS NOT NULL AND c_wip_job_id = NVL(C.wip_job_id, -1))
5539                 )
5540         )
5541         --This wip_entity check is not necessary for an extra sibling nodes even
5542         --so just include it here.
5543         OR
5544         (
5545             c_wip_job_id IS NULL -- SATHAPLI::ER# 6504147 :: extra nodes should not come up during parts change
5546             AND
5547             -- SATHAPLI::FP ER 6504147, 18-Nov-2008
5548             -- include extra nodes of all the parents uptill root
5549             -- U.parent_instance_id = nvl(c_parent_instance_id, -1)
5550             U.parent_instance_id IN (
5551                                      SELECT i3.object_id
5552                                      FROM   csi_ii_relationships i3
5553                                      START WITH i3.subject_id = nvl(c_parent_instance_id, -1)
5554                                      AND    i3.relationship_type_code = 'COMPONENT-OF'
5555                                      AND    trunc(nvl(i3.active_start_date, SYSDATE)) <= trunc(SYSDATE)
5556                                      AND    trunc(nvl(i3.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5557                                      CONNECT BY i3.subject_id = PRIOR i3.object_id
5558                                      AND    i3.relationship_type_code = 'COMPONENT-OF'
5559                                      AND    trunc(nvl(i3.active_start_date, SYSDATE)) <= trunc(SYSDATE)
5560                                      AND    trunc(nvl(i3.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5561                                      UNION ALL
5562                                      SELECT nvl(c_parent_instance_id, -1)
5563                                      FROM   DUAL
5564                                     )
5565             AND EXISTS
5566             (SELECT 1
5567             FROM csi_ii_relationships CI
5568             WHERE CI.object_id = U.parent_instance_id
5569                 AND CI.subject_id = U.csi_instance_id
5570                 AND CI.position_reference IS NULL
5571                 AND CI.relationship_type_code = 'COMPONENT-OF'
5572                 AND trunc(nvl(CI.active_start_date,SYSDATE)) <= trunc(SYSDATE)
5573                 AND trunc(nvl(CI.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5574             )
5575         )
5576     )
5577     AND NOT EXISTS
5578     (SELECT 1
5579     FROM ahl_mc_relationships MR
5580     WHERE MR.parent_relationship_id = c_relationship_id
5581         AND trunc(nvl(MR.active_start_date, SYSDATE)) <= trunc(SYSDATE)
5582         AND trunc(nvl(MR.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5583     )
5584     AND upper(M.concatenated_segments) LIKE c_item_number
5585     AND upper(C.instance_number) LIKE c_instance_number
5586     AND upper(nvl(C.serial_number, '%')) LIKE c_serial_number
5587     AND EXISTS
5588     (
5589      SELECT 'X'
5590      FROM   mtl_parameters mp, inv_organization_info_v io
5591      WHERE  mp.master_organization_id = M.organization_id AND
5592             mp.organization_Id = io.organization_id AND
5593             NVL(io.operating_unit, mo_global.get_current_org_id()) =
5594             mo_global.get_current_org_id()
5595     )
5596     AND ahl_util_uc_pkg.IS_UNIT_QUARANTINED(U.uc_header_id , null) =
5597         FND_API.G_FALSE
5598 ORDER BY 2;
5599 
5600   CURSOR check_relationship_id IS
5601     SELECT relationship_id
5602       FROM ahl_mc_relationships
5603      WHERE relationship_id = p_relationship_id
5604        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
5605        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5606        AND mc_header_id IN (SELECT mc_header_id
5607                               FROM ahl_mc_headers_b
5608                              WHERE config_status_code = 'COMPLETE');
5609 
5610   CURSOR get_priority (c_item_association_id IN NUMBER) IS
5611     SELECT priority
5612     FROM ahl_item_associations_b
5613     WHERE item_association_id = c_item_association_id;
5614 
5615   -- SATHAPLI Bug# 4912576 fix::SQLid 14402138
5616   -- The cursor definition below is changed so that reference
5617   -- to view ahl_unit_config_headers_v is changed to base tables.
5618   -- Please refer earlier version for previous definition.
5619   CURSOR ahl_instance_details (c_csi_item_instance_id IN NUMBER) IS
5620 select a.csi_item_instance_id,
5621        a.csi_object_version csi_object_version_number,
5622        a.item_number,
5623        a.item_description,
5624        a.csi_instance_number,
5625        a.inventory_item_id,
5626        a.inventory_org_id,
5627        a.organization_code,
5628        a.serial_number,
5629        a.revision,
5630        a.lot_number,
5631        a.uom_code,
5632        a.quantity,
5633        a.install_date,
5634        a.mfg_date,
5635        a.location_description,
5636        a.party_type,
5637        a.owner_id,
5638        a.owner_number,
5639        a.owner_name,
5640        a.csi_location_id owner_site_id,
5641        a.owner_site_number,
5642        a.csi_party_object_version_num,
5643        a.status,
5644        a.condition,
5645        a.wip_entity_name,
5646        b.uc_header_id,
5647        b.uc_name,
5648        b.uc_status,
5649        b.mc_header_id,
5650        b.mc_name,
5651        b.mc_revision,
5652        b.mc_status,
5653        b.position_ref,
5654        b.root_uc_header_id
5655  from  ahl_unit_installed_details_v a,
5656        (
5657         SELECT U.unit_config_header_id uc_header_id,
5658                U.name uc_name,
5659                UCSC.meaning uc_status,
5660                U.master_config_id mc_header_id,
5661                M.name mc_name,
5662                M.revision mc_revision,
5663                MCSC.meaning mc_status,
5664                MRSC.meaning position_ref,
5665                (
5666                 SELECT unit_config_header_id
5667                 FROM   ahl_unit_config_headers
5668                 WHERE  parent_uc_header_id IS NULL
5669                 START WITH
5670                        unit_config_header_id = U.unit_config_header_id
5671                 CONNECT BY
5672                        unit_config_header_id = PRIOR parent_uc_header_id
5673                ) root_uc_header_id,
5674                U.csi_item_instance_id csi_instance_id,
5675                U.active_end_date active_end_date
5676         FROM   AHL_UNIT_CONFIG_HEADERS U, AHL_MC_HEADERS_B M,
5677                AHL_MC_RELATIONSHIPS R, FND_LOOKUP_VALUES UCSC,
5678                FND_LOOKUP_VALUES MRSC, FND_LOOKUP_VALUES MCSC
5679         WHERE  U.master_config_id = M.mc_header_id AND
5680                M.mc_header_id = R.mc_header_id AND
5681                R.parent_relationship_id IS NULL AND
5682                U.unit_config_status_code = UCSC.lookup_code AND
5683                'AHL_CONFIG_STATUS' = UCSC.lookup_type AND
5684 	       UCSC.language = USERENV('LANG') AND
5685                M.config_status_code = MCSC.lookup_code AND
5686                'AHL_CONFIG_STATUS' = MCSC.lookup_type AND
5687 	       MCSC.language = USERENV('LANG') AND
5688                R.position_ref_code = MRSC.lookup_code AND
5689                'AHL_POSITION_REFERENCE' = MRSC.lookup_type AND
5690 	       MRSC.language = USERENV('LANG')
5691        ) b
5692 where  a.csi_item_instance_id = c_csi_item_instance_id
5693   and  a.csi_item_instance_id = b.csi_instance_id (+)
5694   and  trunc(nvl(b.active_end_date (+), sysdate+1)) > trunc(sysdate);
5695 
5696   l_instance_details_rec ahl_instance_details%ROWTYPE;
5697 
5698   CURSOR get_wip_entity_id (c_workorder_id NUMBER) IS
5699     SELECT wip_entity_id
5700       FROM ahl_workorders
5701      WHERE workorder_id = c_workorder_id;
5702   l_wip_entity_id   NUMBER;
5703 
5704   CURSOR check_parent_instance(c_instance_id NUMBER) IS
5705   --Parent instance could be either in ahl_unit_config_headers(top node) or in csi_ii_relationships
5706   --(as the subject_id)
5707     SELECT 'x'
5708       FROM ahl_unit_config_headers
5709      WHERE csi_item_instance_id = c_instance_id
5710        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5711      UNION ALL
5712     SELECT 'x'
5713       FROM csi_ii_relationships
5714      WHERE subject_id = c_instance_id
5715        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5716        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE);
5717 
5718   CURSOR get_top_unit_instance(c_instance_id NUMBER) IS
5719 
5720   -- SATHAPLI::Bug#4912576 fix::SQL ID 14402149 --
5721   /*
5722     SELECT object_id
5723       FROM csi_ii_relationships
5724      WHERE object_id NOT IN (SELECT subject_id
5725                                FROM csi_ii_relationships
5726                               WHERE relationship_type_code = 'COMPONENT-OF'
5727                                 AND trunc(nvl(active_start_date,SYSDATE)) <= trunc(SYSDATE)
5728                                 AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE))
5729 START WITH subject_id = c_instance_id
5730        AND relationship_type_code = 'COMPONENT-OF'
5731        AND trunc(nvl(active_start_date,SYSDATE)) <= trunc(SYSDATE)
5732        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5733 CONNECT BY subject_id = PRIOR object_id
5734        AND relationship_type_code = 'COMPONENT-OF'
5735        AND trunc(nvl(active_start_date,SYSDATE)) <= trunc(SYSDATE)
5736        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
5737   */
5738     SELECT object_id
5739     FROM   csi_ii_relationships co
5740     WHERE  NOT EXISTS
5741            (
5742             SELECT 'X'
5743             FROM   csi_ii_relationships ci
5744             WHERE  ci.relationship_type_code = 'COMPONENT-OF' AND
5745                    ci.subject_id = co.object_id AND
5746                    trunc(nvl(ci.active_start_date,SYSDATE)) <= trunc(SYSDATE) AND
5747                    trunc(nvl(ci.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5748            )
5749            START WITH co.subject_id = c_instance_id AND
5750            co.relationship_type_code = 'COMPONENT-OF' AND
5751            trunc(nvl(co.active_start_date,SYSDATE)) <= trunc(SYSDATE) AND
5752            trunc(nvl(co.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5753            CONNECT BY co.subject_id = PRIOR co.object_id AND
5754            co.relationship_type_code = 'COMPONENT-OF' AND
5755            trunc(nvl(co.active_start_date,SYSDATE)) <= trunc(SYSDATE) AND
5756            trunc(nvl(co.active_end_date, SYSDATE+1)) > trunc(SYSDATE);
5757 
5758   CURSOR get_uc_status(c_instance_id NUMBER) IS
5759     SELECT unit_config_status_code
5760       FROM ahl_unit_config_headers
5761      WHERE csi_item_instance_id = c_instance_id
5762        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
5763 
5764   CURSOR get_csi_ii_relationship_ovn (c_instance_id NUMBER) IS
5765     SELECT object_version_number
5766       FROM csi_ii_relationships
5767      WHERE subject_id = c_instance_id
5768        AND position_reference IS NULL
5769        AND relationship_type_code = 'COMPONENT-OF'
5770        AND trunc(nvl(active_start_date,SYSDATE)) <= trunc(SYSDATE)
5771        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
5772   l_csi_ii_relationship_ovn   NUMBER;
5773 
5774 BEGIN
5775   -- Initialize API return status to success
5776   x_return_status := FND_API.G_RET_STS_SUCCESS;
5777   -- Standard call to check for call compatibility.
5778   IF NOT FND_API.compatible_api_call(
5779     l_api_version,
5780     p_api_version,
5781     l_api_name,
5782     G_PKG_NAME)
5783   THEN
5784     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5785   END IF;
5786 
5787   -- Initialize message list if p_init_msg_list is set to TRUE.
5788   IF FND_API.to_boolean( p_init_msg_list ) THEN
5789     FND_MSG_PUB.initialize;
5790   END IF;
5791 
5792   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5793     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
5794                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Begin API',
5795                    'At the start of the procedure');
5796   END IF;
5797 
5798   OPEN check_relationship_id;
5799   FETCH check_relationship_id INTO l_relationship_id;
5800   IF check_relationship_id%NOTFOUND THEN
5801     FND_MESSAGE.set_name( 'AHL','AHL_UC_POSITION_INVALID' );
5802     FND_MESSAGE.set_token('POSITION', p_relationship_id);
5803     FND_MSG_PUB.add;
5804     CLOSE check_relationship_id;
5805     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5806   ELSE
5807     CLOSE check_relationship_id;
5808   END IF;
5809 
5810   --Get wip_entity_id from p_workorder_id
5811   IF p_workorder_id IS NOT NULL THEN
5812     OPEN get_wip_entity_id(p_workorder_id);
5813     FETCH get_wip_entity_id INTO l_wip_entity_id;
5814     IF get_wip_entity_id%NOTFOUND THEN
5815       FND_MESSAGE.set_name( 'AHL','AHL_UC_WORKORDER_INVALID' );
5816       FND_MESSAGE.set_token('WORKORDER', p_workorder_id);
5817       FND_MSG_PUB.add;
5818       CLOSE get_wip_entity_id;
5819       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5820     ELSE
5821       CLOSE get_wip_entity_id;
5822     END IF;
5823   ELSE
5824     l_wip_entity_id := NULL;
5825   END IF;
5826 
5827   --Validate p_parent_instance_id is Null(for top node) or existing in
5828   --ahl_unit_config_headers or csi_ii_relationships(for non-top node)
5829   IF p_parent_instance_id IS NOT NULL THEN
5830     OPEN check_parent_instance(p_parent_instance_id);
5831     FETCH check_parent_instance INTO l_dummy_char;
5832     IF check_parent_instance%NOTFOUND THEN
5833       FND_MESSAGE.set_name( 'AHL','AHL_UC_PARENT_INST_INVALID' );
5834       FND_MESSAGE.set_token('INSTANCE', p_parent_instance_id);
5835       FND_MSG_PUB.add;
5836       CLOSE check_parent_instance;
5837       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5838     ELSE
5839       CLOSE check_parent_instance;
5840     END IF;
5841   END IF;
5842 
5843   --Get the instance's ancestor unit's status, just in order to meet the regulation that
5844   --Draft unit can only be installed into Draft unit and Complete unit can only be installed
5845   --into Complete unit.
5846   IF p_parent_instance_id IS NOT NULL THEN --Not top position
5847     OPEN get_top_unit_instance(p_parent_instance_id);
5848     FETCH get_top_unit_instance INTO l_top_instance_id;
5849     IF get_top_unit_instance%NOTFOUND THEN --Parent_instance_id happens to be top node
5850       l_top_instance_id := p_parent_instance_id;
5851     END IF;
5852     CLOSE get_top_unit_instance;
5853 
5854     OPEN get_uc_status(l_top_instance_id);
5855     FETCH get_uc_status INTO l_top_uc_status;
5856     IF get_uc_status%NOTFOUND THEN
5857       FND_MESSAGE.set_name( 'AHL','AHL_UC_INSTANCE_NOT_IN_UC' );
5858       FND_MESSAGE.set_token('INSTANCE', l_top_instance_id);
5859       FND_MSG_PUB.add;
5860       CLOSE get_uc_status;
5861       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5862     ELSE
5863       CLOSE get_uc_status;
5864     END IF;
5865   ELSE
5866     l_top_uc_status := NULL; --Doesn't matter wether what it is, because no existing units will
5867   END IF;                    --be available for this top position
5868 
5869   SELECT decode(l_top_uc_status, 'DRAFT', 'DRAFT',
5870                      'APPROVAL_REJECTED', 'DRAFT',
5871                             'COMPLETE','COMPLETE',
5872                     'INCOMPLETE','COMPLETE', NULL) INTO l_top_uc_status
5873   FROM dual;
5874 
5875   --Based on profile value open the cursor.
5876   -- SATHAPLI Bug# 4912576 fix
5877   -- Code pertaining to Org check has been removed from here
5878   -- Please refer earlier version for the details
5879 
5880     OPEN get_instance2 (p_relationship_id,
5881                         l_item_number,
5882                         l_instance_number,
5883                         l_serial_number,
5884                         l_wip_entity_id,
5885                         p_parent_instance_id,
5886                         l_top_uc_status);
5887 
5888     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5889         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5890                        'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5891                        ' ;p_relationship_id = '||p_relationship_id||' ;l_item_number = '||l_item_number||
5892                        ' ;l_instance_number = '||l_instance_number||' ;l_serial_number = '||l_serial_number||
5893                        ' ;l_wip_entity_id = '||l_wip_entity_id||' ;p_parent_instance_id = '||p_parent_instance_id||
5894                        ' ;l_top_uc_status = '||l_top_uc_status);
5895     END IF;
5896 
5897   i := 0;
5898   j := 0;
5899   -- row count.
5900   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5901     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5902                      'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5903                      'part_number='||l_item_number||'instance_number='||l_instance_number||
5904                      'serial_number='||l_serial_number||'wip_entity_id='||l_wip_entity_id||
5905                      'p_parent_instance_id='||p_parent_instance_id);
5906   END IF;
5907 
5908   LOOP
5909 
5910   -- SATHAPLI Bug# 4912576 fix
5911   -- Code pertaining to Org check has been removed from here
5912   -- Please refer earlier version for the details
5913 
5914       FETCH get_instance2 INTO l_instance_id, l_instance_number, l_inventory_item_id, l_inventory_org_id,
5915                                l_quantity, l_inventory_revision, l_uom_code, l_uc_header_id;
5916       EXIT WHEN get_instance2%NOTFOUND;
5917 
5918     --If the instance is not a unit then, call procedure to validate whether thecorresponding inventory
5919     --can be assigned to that position
5920     IF l_uc_header_id IS NULL THEN
5921       -- SATHAPLI::FP OGMA Issue# 105 - Non-Serialized Item Maintenance, 05-Dec-2007
5922       -- If p_workorder_id is not NULL, then the call is from Production. So pass 'Y' for p_ignore_quant_vald.
5923       -- Else, pass 'N' for p_ignore_quant_vald.
5924       IF(p_workorder_id IS NOT NULL) THEN
5925         l_ignore_quant_vald := 'Y';
5926       ELSE
5927         l_ignore_quant_vald := 'N';
5928       END IF;
5929 
5930       AHL_UTIL_UC_PKG.validate_for_position(p_mc_relationship_id   => p_relationship_id,
5931                                             p_inventory_id         => l_inventory_item_id,
5932                                             p_organization_id      => l_inventory_Org_id,
5933                                             p_quantity             => l_quantity,
5934                                             p_revision             => l_inventory_revision,
5935                                             p_uom_code             => l_uom_code,
5936                                             p_position_ref_meaning => NULL,
5937                                             p_ignore_quant_vald    => l_ignore_quant_vald,
5938                                             x_item_assoc_id        => l_item_assoc_id);
5939     END IF;
5940 
5941     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5942       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5943                      'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5944                      'After position validation');
5945     END IF;
5946 
5947     IF (fnd_msg_pub.count_msg = 0) THEN
5948       i := i + 1;
5949       IF (i >= p_start_row_index AND i < p_start_row_index + p_max_rows) THEN
5950         OPEN ahl_instance_details(l_instance_id);
5951         FETCH ahl_instance_details INTO l_instance_details_rec;
5952         IF (ahl_instance_details%FOUND) THEN
5953           j := j + 1;
5954           x_available_instance_tbl(j).csi_item_instance_id := l_instance_details_rec.csi_item_instance_id;
5955           x_available_instance_tbl(j).csi_object_version_number := l_instance_details_rec.csi_object_version_number;
5956           x_available_instance_tbl(j).item_number := l_instance_details_rec.item_number;
5957           x_available_instance_tbl(j).item_description := l_instance_details_rec.item_description;
5958           x_available_instance_tbl(j).csi_instance_number := l_instance_details_rec.csi_instance_number;
5959           x_available_instance_tbl(j).organization_code := l_instance_details_rec.organization_code;
5960           x_available_instance_tbl(j).inventory_item_id := l_instance_details_rec.inventory_item_id;
5961           x_available_instance_tbl(j).inventory_org_id := l_instance_details_rec.inventory_org_id;
5962           x_available_instance_tbl(j).serial_number := l_instance_details_rec.serial_number;
5963           x_available_instance_tbl(j).revision := l_instance_details_rec.revision;
5964           x_available_instance_tbl(j).lot_number := l_instance_details_rec.lot_number;
5965           x_available_instance_tbl(j).uom_code := l_instance_details_rec.uom_code;
5966           x_available_instance_tbl(j).quantity := l_instance_details_rec.quantity;
5967           x_available_instance_tbl(j).install_date := l_instance_details_rec.install_date;
5968           x_available_instance_tbl(j).mfg_date := l_instance_details_rec.mfg_date;
5969           x_available_instance_tbl(j).location_description := l_instance_details_rec.location_description;
5970           x_available_instance_tbl(j).party_type := l_instance_details_rec.party_type;
5971           x_available_instance_tbl(j).owner_site_number := l_instance_details_rec.owner_site_number;
5972           x_available_instance_tbl(j).owner_site_ID := l_instance_details_rec.owner_site_ID;
5973           x_available_instance_tbl(j).owner_number := l_instance_details_rec.owner_number;
5974           x_available_instance_tbl(j).owner_name := l_instance_details_rec.owner_name;
5975           x_available_instance_tbl(j).owner_ID := l_instance_details_rec.owner_ID;
5976           x_available_instance_tbl(j).csi_party_object_version_num := l_instance_details_rec.csi_party_object_version_num;
5977           x_available_instance_tbl(j).status := l_instance_details_rec.status;
5978           x_available_instance_tbl(j).condition := l_instance_details_rec.condition;
5979           x_available_instance_tbl(j).wip_entity_name := l_instance_details_rec.wip_entity_name;
5980           x_available_instance_tbl(j).uc_header_id := l_instance_details_rec.uc_header_id;
5981           x_available_instance_tbl(j).uc_name := l_instance_details_rec.uc_name;
5982           --Modified on 02/26/2004 in case the status inconsistency occurred between an extra sub-unit and
5983           --its root unit
5984           IF l_instance_details_rec.root_uc_header_id IS NOT NULL THEN
5985 
5986             -- SATHAPLI::Bug#4912576 fix::SQL ID 14402160 --
5987 	    /*
5988 	    SELECT uc_status INTO l_status
5989               FROM ahl_unit_config_headers_v
5990              WHERE uc_header_id = l_instance_details_rec.root_uc_header_id;
5991             */
5992             SELECT FLV.meaning INTO l_status
5993             FROM   AHL_UNIT_CONFIG_HEADERS AUCH, FND_LOOKUP_VALUES FLV
5994             WHERE  AUCH.unit_config_header_id = l_instance_details_rec.root_uc_header_id AND
5995                    AUCH.unit_config_status_code = FLV.lookup_code AND
5996                    FLV.lookup_type  = 'AHL_CONFIG_STATUS' AND
5997                    FLV.language = USERENV('LANG');
5998 
5999             x_available_instance_tbl(j).uc_status := l_status;
6000           ELSE
6001             x_available_instance_tbl(j).uc_status := l_instance_details_rec.uc_status;
6002           END IF;
6003           x_available_instance_tbl(j).mc_header_id := l_instance_details_rec.mc_header_id;
6004           x_available_instance_tbl(j).mc_name := l_instance_details_rec.mc_name;
6005           x_available_instance_tbl(j).mc_revision := l_instance_details_rec.mc_revision;
6006           x_available_instance_tbl(j).mc_status := l_instance_details_rec.mc_status;
6007           x_available_instance_tbl(j).position_ref := l_instance_details_rec.position_ref;
6008           --Get priority
6009           OPEN get_priority(l_item_assoc_id);
6010           FETCH get_priority INTO l_priority;
6011           CLOSE get_priority;
6012           x_available_instance_tbl(j).priority := l_priority;
6013           --If the instance is an extra sibling node, then get its object version number in
6014           --table csi_ii_relationship
6015           OPEN get_csi_ii_relationship_ovn(x_available_instance_tbl(j).csi_item_instance_id);
6016           FETCH get_csi_ii_relationship_ovn INTO l_csi_ii_relationship_ovn;
6017           IF get_csi_ii_relationship_ovn%FOUND THEN
6018             x_available_instance_tbl(j).csi_ii_relationship_ovn := l_csi_ii_relationship_ovn;
6019           ELSE
6020             x_available_instance_tbl(j).csi_ii_relationship_ovn := NULL;
6021           END IF;
6022           CLOSE get_csi_ii_relationship_ovn;
6023         END IF;
6024         CLOSE ahl_instance_details;
6025       END IF;
6026     END IF;
6027     fnd_msg_pub.initialize;
6028   END LOOP;
6029   x_tbl_count := i;
6030 
6031   -- SATHAPLI Bug# 4912576 fix
6032   -- Code pertaining to Org check has been removed from here
6033   -- Please refer earlier version for the details
6034 
6035   IF (get_instance2%ISOPEN) THEN
6036     CLOSE get_instance2;
6037   END IF;
6038 
6039   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6040     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
6041                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': End API',
6042                    'After normal execution');
6043   END IF;
6044 
6045   -- Get all the error messages from the previous steps (if any) and raise the appropriate Exception
6046   l_msg_count := FND_MSG_PUB.count_msg;
6047   IF l_msg_count > 0 THEN
6048     x_msg_count := l_msg_count;
6049     RAISE FND_API.G_EXC_ERROR;
6050   END IF;
6051   -- Count and Get messages (optional)
6052   FND_MSG_PUB.count_and_get(
6053     p_encoded  => FND_API.G_FALSE,
6054     p_count    => x_msg_count,
6055     p_data     => x_msg_data);
6056 EXCEPTION
6057   WHEN FND_API.G_EXC_ERROR THEN
6058     x_return_status := FND_API.G_RET_STS_ERROR ;
6059     FND_MSG_PUB.count_and_get(
6060       p_encoded  => FND_API.G_FALSE,
6061       p_count    => x_msg_count,
6062       p_data     => x_msg_data);
6063   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6064     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6065     FND_MSG_PUB.count_and_get(
6066       p_encoded  => FND_API.G_FALSE,
6067       p_count    => x_msg_count,
6068       p_data     => x_msg_data);
6069   WHEN OTHERS THEN
6070     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6071     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
6072     THEN
6073       FND_MSG_PUB.add_exc_msg(
6074         p_pkg_name         => G_PKG_NAME,
6075         p_procedure_name   => l_api_name,
6076         p_error_text       => SUBSTRB(SQLERRM,1,240));
6077     END IF;
6078     FND_MSG_PUB.count_and_get(
6079       p_encoded  => FND_API.G_FALSE,
6080       p_count    => x_msg_count,
6081       p_data     => x_msg_data);
6082 END get_available_instances;
6083 
6084 -- SATHAPLI::FP ER 6504147, 18-Nov-2008
6085 -- Define procedure create_unassigned_instance.
6086 -- This API is used to create a new instance in csi_item_instances and assign it
6087 -- to the UC root node as extra node.
6088 
6089 PROCEDURE create_unassigned_instance(
6090     p_api_version           IN            NUMBER   := 1.0,
6091     p_init_msg_list         IN            VARCHAR2 := FND_API.G_FALSE,
6092     p_commit                IN            VARCHAR2 := FND_API.G_FALSE,
6093     p_validation_level      IN            NUMBER   := FND_API.G_VALID_LEVEL_FULL,
6094     x_return_status         OUT    NOCOPY VARCHAR2,
6095     x_msg_count             OUT    NOCOPY NUMBER,
6096     x_msg_data              OUT    NOCOPY VARCHAR2,
6097     p_uc_header_id          IN            NUMBER,
6098     p_x_uc_instance_rec     IN OUT NOCOPY uc_instance_rec_type
6099 )
6100 IS
6101 
6102 CURSOR check_uc_header_csr (p_uc_header_id NUMBER) IS
6103     SELECT unit_config_header_id,
6104            unit_config_status_code,
6105            active_uc_status_code,
6106            csi_item_instance_id,
6107            parent_uc_header_id
6108     FROM   ahl_unit_config_headers
6109     WHERE  unit_config_header_id  = p_uc_header_id
6110     AND    trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
6111 
6112 CURSOR csi_item_instance_csr(p_csi_instance_id NUMBER) IS
6113     SELECT location_id,
6114            location_type_code,
6115            party_id,
6116            party_source_table,
6117            instance_party_id,
6118            csi.wip_job_id
6119     FROM   csi_item_instances csi, csi_i_parties p
6120     WHERE  csi.instance_id          = p.instance_id
6121     AND    p.relationship_type_code = 'OWNER'
6122     AND    csi.instance_id          = p_csi_instance_id
6123     AND    trunc(nvl(csi.active_end_date, SYSDATE+1)) > trunc(SYSDATE);
6124 
6125 CURSOR csi_ip_accounts_csr(p_instance_party_id NUMBER) IS
6126     SELECT party_account_id
6127     FROM   csi_ip_accounts
6128     WHERE  relationship_type_code = 'OWNER'
6129     AND    instance_party_id      = p_instance_party_id
6130     AND    trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
6131     AND    trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
6132 --
6133     l_api_version    CONSTANT   NUMBER       := 1.0;
6134     l_api_name       CONSTANT   VARCHAR2(30) := 'create_unassigned_instance';
6135     l_full_name      CONSTANT   VARCHAR2(70) := 'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name;
6136 
6137     l_check_uc_header_rec       check_uc_header_csr%ROWTYPE;
6138     l_uc_owner_loc_rec          csi_item_instance_csr%ROWTYPE;
6139     l_msg_count_bef             NUMBER;
6140     l_msg_count                 NUMBER;
6141     l_root_instance_ou          NUMBER;
6142     l_new_instance_ou           NUMBER;
6143     l_parent_instance_id        NUMBER;
6144     l_return_status             VARCHAR2(1);
6145     l_msg_data                  VARCHAR2(2000);
6146     l_subscript                 NUMBER       DEFAULT 0;
6147     i                           NUMBER       := 0;
6148     l_transaction_type_id       NUMBER;
6149     l_return_val                BOOLEAN;
6150     l_attribute_id              NUMBER;
6151     l_concatenated_segments     mtl_system_items_kfv.concatenated_segments%TYPE;
6152     l_new_instance_id           NUMBER;
6153     l_new_csi_instance_ovn      NUMBER;
6154     l_end_date                  DATE;
6155 
6156     -- Variables needed for CSI API call
6157     l_csi_instance_rec          csi_datastructures_pub.instance_rec;
6158     l_csi_party_rec             csi_datastructures_pub.party_rec;
6159     l_csi_transaction_rec       csi_datastructures_pub.transaction_rec;
6160     l_csi_relationship_rec      csi_datastructures_pub.ii_relationship_rec;
6161     l_csi_relationship_tbl      csi_datastructures_pub.ii_relationship_tbl;
6162     l_csi_party_tbl             csi_datastructures_pub.party_tbl;
6163     l_csi_account_tbl           csi_datastructures_pub.party_account_tbl;
6164     l_csi_pricing_attrib_tbl    csi_datastructures_pub.pricing_attribs_tbl;
6165     l_csi_org_assignments_tbl   csi_datastructures_pub.organization_units_tbl;
6166     l_csi_asset_assignment_tbl  csi_datastructures_pub.instance_asset_tbl;
6167     l_csi_instance_id_lst       csi_datastructures_pub.id_tbl;
6168     l_party_account_rec         csi_datastructures_pub.party_account_rec;
6169     l_csi_extend_attrib_rec     csi_datastructures_pub.extend_attrib_values_rec;
6170     l_csi_ext_attrib_values_tbl csi_datastructures_pub.extend_attrib_values_tbl;
6171 --
6172 
6173 BEGIN
6174     IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
6175         FND_LOG.string(FND_LOG.level_procedure,l_full_name,'Start of the API');
6176     END IF;
6177 
6178     -- Standard start of API savepoint
6179     SAVEPOINT create_unassigned_instance;
6180 
6181     -- Initialize Procedure return status to success
6182     x_return_status := FND_API.G_RET_STS_SUCCESS;
6183 
6184     -- Standard call to check for call compatibility
6185     IF NOT FND_API.Compatible_API_Call(l_api_version, p_api_version,
6186                                        l_api_name, G_PKG_NAME) THEN
6187         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6188     END IF;
6189 
6190     -- Initialize message list if p_init_msg_list is set to TRUE
6191     IF FND_API.To_Boolean(p_init_msg_list) THEN
6192         FND_MSG_PUB.Initialize;
6193     END IF;
6194 
6195     -- Validate input parameter p_uc_header_id
6196     OPEN check_uc_header_csr(p_uc_header_id);
6197     FETCH check_uc_header_csr INTO l_check_uc_header_rec;
6198 
6199     IF check_uc_header_csr%NOTFOUND THEN
6200         CLOSE check_uc_header_csr;
6201         -- p_uc_header_id in invalid
6202         FND_MESSAGE.set_name('AHL','AHL_UC_API_PARAMETER_INVALID');
6203         FND_MESSAGE.set_token('NAME', 'uc_header_id');
6204         FND_MESSAGE.set_token('VALUE', p_uc_header_id);
6205         FND_MSG_PUB.add;
6206         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6207     ELSIF (l_check_uc_header_rec.unit_config_status_code = 'APPROVAL_PENDING' OR
6208            l_check_uc_header_rec.active_uc_status_code = 'APPROVAL_PENDING') THEN
6209         CLOSE check_uc_header_csr;
6210         -- UC status is not editable
6211         FND_MESSAGE.set_name('AHL','AHL_UC_STATUS_PENDING');
6212         FND_MESSAGE.set_token('UC_HEADER_ID', l_check_uc_header_rec.unit_config_header_id);
6213         FND_MSG_PUB.add;
6214         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6215     ELSIF (l_check_uc_header_rec.parent_uc_header_id IS NOT NULL) THEN
6216         CLOSE check_uc_header_csr;
6217         -- UC is installed sub config
6218         FND_MESSAGE.set_name('AHL','AHL_UC_INST_SUB_CONFIG');
6219         FND_MESSAGE.set_token('UC_HEADER_ID', l_check_uc_header_rec.unit_config_header_id);
6220         FND_MSG_PUB.add;
6221         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6222     ELSE
6223         CLOSE check_uc_header_csr;
6224     END IF;
6225 
6226     -- Get the operating unit of the root instance
6227     l_root_instance_ou := get_operating_unit(l_check_uc_header_rec.csi_item_instance_id);
6228 
6229     -- The parent instance is the root node
6230     l_parent_instance_id := l_check_uc_header_rec.csi_item_instance_id;
6231 
6232     -- Check whether the UC is expired or not by checking for the root instance
6233     OPEN get_instance_date(l_parent_instance_id);
6234     FETCH get_instance_date INTO l_end_date;
6235     CLOSE get_instance_date;
6236     IF TRUNC(NVL(l_end_date, SYSDATE+1)) <= TRUNC(SYSDATE) THEN
6237         FND_MESSAGE.set_name('AHL','AHL_UC_STATUS_EXPIRED');
6238         FND_MESSAGE.set_token('UC_NAME', l_check_uc_header_rec.unit_config_header_id);
6239         FND_MSG_PUB.add;
6240         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6241     END IF;
6242 
6243     -- When creating the new instances, the "From Inventory" Serial Tag should not be used anymore.
6244     IF(p_x_uc_instance_rec.sn_tag_code IS NOT NULL AND p_x_uc_instance_rec.sn_tag_code = 'INVENTORY') THEN
6245         FND_MESSAGE.set_name( 'AHL','AHL_UC_SER_TG_CR_INVEN' );
6246         FND_MSG_PUB.add;
6247         RAISE FND_API.G_EXC_ERROR;
6248     END IF;
6249 
6250     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6251         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, l_full_name,
6252                        ' l_root_instance_ou => '||l_root_instance_ou||
6253      	               ' l_parent_instance_id => '||l_parent_instance_id);
6254     END IF;
6255 
6256     -- Get the msg count befrore API call
6257     l_msg_count_bef := FND_MSG_PUB.count_msg;
6258 
6259     -- Validate Inventory details
6260     validate_uc_invdetails (
6261         p_inventory_id          => p_x_uc_instance_rec.inventory_item_id,
6262         p_organization_id       => p_x_uc_instance_rec.inventory_org_id,
6263         p_serial_number         => p_x_uc_instance_rec.serial_number,
6264         p_serialnum_tag_code    => p_x_uc_instance_rec.sn_tag_code,
6265         p_quantity              => p_x_uc_instance_rec.quantity,
6266         p_uom_code              => p_x_uc_instance_rec.uom_code,
6267         p_revision              => p_x_uc_instance_rec.revision,
6268         p_lot_number            => p_x_uc_instance_rec.lot_number,
6269         p_position_ref_meaning  => NULL,
6270         x_concatenated_segments => l_concatenated_segments);
6271 
6272     -- Check Error Message stack
6273     l_msg_count := FND_MSG_PUB.count_msg;
6274     IF (l_msg_count > l_msg_count_bef) THEN
6275         RAISE FND_API.G_EXC_ERROR;
6276     END IF;
6277 
6278     -- Validate manufacturing date
6279     IF (p_x_uc_instance_rec.mfg_date IS NOT NULL AND
6280         p_x_uc_instance_rec.mfg_date <> FND_API.G_MISS_DATE AND
6281         p_x_uc_instance_rec.mfg_date > SYSDATE) THEN
6282         -- mfg_date is invalid
6283         FND_MESSAGE.set_name('AHL','AHL_UC_MFGDATE_INVALID');
6284         FND_MESSAGE.set_token('DATE',p_x_uc_instance_rec.mfg_date);
6285         FND_MESSAGE.set_token('INV_ITEM',l_concatenated_segments);
6286         FND_MSG_PUB.add;
6287     END IF;
6288 
6289     -- Build CSI records and call API
6290     -- First get unit config location and owner details
6291     OPEN csi_item_instance_csr(l_parent_instance_id);
6292     FETCH csi_item_instance_csr INTO l_uc_owner_loc_rec;
6293 
6294     IF (csi_item_instance_csr%NOTFOUND) THEN
6295         CLOSE csi_item_instance_csr;
6296         -- parent instance is invalid
6297         FND_MESSAGE.set_name('AHL','AHL_UC_CSII_INVALID');
6298         FND_MESSAGE.set_token('CSII',l_parent_instance_id);
6299         FND_MSG_PUB.add;
6300         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6301     END IF;
6302 
6303     CLOSE csi_item_instance_csr;
6304 
6305     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6306         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, l_full_name,
6307                        ' l_uc_owner_loc_rec.location_id => '||l_uc_owner_loc_rec.location_id||
6308                        ' l_uc_owner_loc_rec.location_type_code => '||l_uc_owner_loc_rec.location_type_code||
6309                        ' l_uc_owner_loc_rec.party_id => '||l_uc_owner_loc_rec.party_id||
6310                        ' l_uc_owner_loc_rec.party_source_table => '||l_uc_owner_loc_rec.party_source_table||
6311                        ' l_uc_owner_loc_rec.instance_party_id => '||l_uc_owner_loc_rec.instance_party_id||
6312                        ' l_uc_owner_loc_rec.wip_job_id => '||l_uc_owner_loc_rec.wip_job_id);
6313     END IF;
6314 
6315     -- Set csi instance record
6316     l_csi_instance_rec.inventory_item_id      := p_x_uc_instance_rec.inventory_item_id;
6317     l_csi_instance_rec.vld_organization_id    := p_x_uc_instance_rec.inventory_org_id;
6318     l_csi_instance_rec.quantity               := p_x_uc_instance_rec.quantity;
6319     l_csi_instance_rec.unit_of_measure        := p_x_uc_instance_rec.uom_code;
6320     l_csi_instance_rec.install_date           := p_x_uc_instance_rec.install_date;
6321     l_csi_instance_rec.location_id            := l_uc_owner_loc_rec.location_id;
6322     l_csi_instance_rec.location_type_code     := l_uc_owner_loc_rec.location_type_code;
6323     l_csi_instance_rec.wip_job_id             := l_uc_owner_loc_rec.wip_job_id;
6324     l_csi_instance_rec.mfg_serial_number_flag := 'N';
6325     l_csi_instance_rec.instance_usage_code    := NULL;
6326 
6327     IF (p_x_uc_instance_rec.serial_number IS NOT NULL AND
6328         p_x_uc_instance_rec.serial_number <> FND_API.G_MISS_CHAR) THEN
6329         l_csi_instance_rec.serial_number := p_x_uc_instance_rec.serial_number;
6330     END IF;
6331 
6332     IF (p_x_uc_instance_rec.lot_number IS NOT NULL AND
6333         p_x_uc_instance_rec.lot_number <> FND_API.G_MISS_CHAR) THEN
6334         l_csi_instance_rec.lot_number := p_x_uc_instance_rec.lot_number;
6335     END IF;
6336 
6337     IF (p_x_uc_instance_rec.revision IS NOT NULL AND
6338         p_x_uc_instance_rec.revision <> FND_API.G_MISS_CHAR) THEN
6339         l_csi_instance_rec.inventory_revision := p_x_uc_instance_rec.revision;
6340     END IF;
6341 
6342     -- Build CSI extended attribs
6343     IF (p_x_uc_instance_rec.mfg_date IS NOT NULL AND
6344         p_x_uc_instance_rec.mfg_date <> FND_API.G_MISS_DATE) THEN
6345         AHL_UTIL_UC_PKG.getcsi_attribute_id('AHL_MFG_DATE',l_attribute_id, l_return_val);
6346 
6347         IF NOT(l_return_val) THEN
6348             FND_MESSAGE.set_name('AHL','AHL_UC_ATTRIB_CODE_MISSING');
6349             FND_MESSAGE.set_token('CODE', 'AHL_MFG_DATE');
6350             FND_MSG_PUB.add;
6351         ELSE
6352             l_csi_extend_attrib_rec.attribute_id := l_attribute_id;
6353             l_csi_extend_attrib_rec.attribute_value := to_char(p_x_uc_instance_rec.mfg_date, 'DD/MM/YYYY');
6354             l_subscript := l_subscript + 1;
6355             l_csi_ext_attrib_values_tbl(l_subscript) := l_csi_extend_attrib_rec;
6356         END IF;
6357     END IF;
6358 
6359     IF (p_x_uc_instance_rec.serial_number IS NOT NULL AND
6360         p_x_uc_instance_rec.serial_number <> FND_API.G_MISS_CHAR) THEN
6361         AHL_UTIL_UC_PKG.getcsi_attribute_id('AHL_TEMP_SERIAL_NUM',l_attribute_id, l_return_val);
6362 
6363         IF NOT(l_return_val) THEN
6364             FND_MESSAGE.set_name('AHL','AHL_UC_ATTRIB_CODE_MISSING');
6365             FND_MESSAGE.set_token('CODE', 'AHL_TEMP_SERIAL_NUM');
6366             FND_MSG_PUB.add;
6367         ELSE
6368             l_csi_extend_attrib_rec.attribute_id         := l_attribute_id;
6369             l_csi_extend_attrib_rec.attribute_value      := p_x_uc_instance_rec.sn_tag_code;
6370             l_csi_ext_attrib_values_tbl(l_subscript + 1) := l_csi_extend_attrib_rec;
6371         END IF;
6372     END IF;
6373 
6374     -- Build CSI party record
6375     l_csi_party_rec.party_id               := l_uc_owner_loc_rec.party_id;
6376     l_csi_party_rec.relationship_type_code := 'OWNER';
6377     l_csi_party_rec.party_source_table     := l_uc_owner_loc_rec.party_source_table;
6378     l_csi_party_rec.contact_flag           := 'N';
6379     l_csi_party_tbl(1)                     := l_csi_party_rec;
6380 
6381     -- Build CSI accounts table
6382     FOR party_ip_acct IN csi_ip_accounts_csr(l_uc_owner_loc_rec.instance_party_id)
6383     LOOP
6384         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6385             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, l_full_name,
6386                            ' i => '||i||
6387                            ' party_ip_acct.party_account_id => '||party_ip_acct.party_account_id);
6388         END IF;
6389 
6390         l_party_account_rec.party_account_id       := party_ip_acct.party_account_id;
6391         l_party_account_rec.relationship_type_code := 'OWNER';
6392         l_party_account_rec.parent_tbl_index       := 1;
6393         i := i + 1;
6394         l_csi_account_tbl(i)                       := l_party_account_rec;
6395     END LOOP;
6396 
6397     -- Build CSI transaction record, first get transaction_type_id
6398     AHL_UTIL_UC_PKG.getcsi_transaction_id('UC_CREATE',l_transaction_type_id, l_return_val);
6399 
6400     IF NOT(l_return_val) THEN
6401         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6402     END IF;
6403 
6404     l_csi_transaction_rec.source_transaction_date := SYSDATE;
6405     l_csi_transaction_rec.transaction_type_id     := l_transaction_type_id;
6406 
6407     -- Check Error Message stack
6408     l_msg_count := FND_MSG_PUB.count_msg;
6409     IF (l_msg_count > l_msg_count_bef) THEN
6410         RAISE  FND_API.G_EXC_ERROR;
6411     END IF;
6412 
6413     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6414         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, l_full_name,
6415                        ' About to call CSI_ITEM_INSTANCE_PUB.create_item_instance');
6416     END IF;
6417 
6418     --Call CSI API to create instance
6419     CSI_ITEM_INSTANCE_PUB.create_item_instance(
6420         p_api_version           => 1.0,
6421         p_instance_rec          => l_csi_instance_rec,
6422         p_txn_rec               => l_csi_transaction_rec,
6423         p_ext_attrib_values_tbl => l_csi_ext_attrib_values_tbl,
6424         p_party_tbl             => l_csi_party_tbl,
6425         p_account_tbl           => l_csi_account_tbl,
6426         p_pricing_attrib_tbl    => l_csi_pricing_attrib_tbl,
6427         p_org_assignments_tbl   => l_csi_org_assignments_tbl,
6428         p_asset_assignment_tbl  => l_csi_asset_assignment_tbl,
6429         x_return_status         => l_return_status,
6430         x_msg_count             => l_msg_count,
6431         x_msg_data              => l_msg_data);
6432 
6433     l_new_instance_id                         := l_csi_instance_rec.instance_id;
6434     l_new_csi_instance_ovn                    := l_csi_instance_rec.object_version_number;
6435     p_x_uc_instance_rec.instance_id           := l_new_instance_id;
6436     p_x_uc_instance_rec.object_version_number := l_new_csi_instance_ovn;
6437 
6438     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6439         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, l_full_name,
6440                        ' After call to CSI_ITEM_INSTANCE_PUB.create_item_instance'||
6441                        ' instance_id => '||l_csi_instance_rec.instance_id||
6442      	               ' l_return_status => '||l_return_status||
6443                        ' p_x_uc_instance_rec.instance_id='||p_x_uc_instance_rec.instance_id);
6444     END IF;
6445 
6446     IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
6447         RAISE FND_API.G_EXC_ERROR;
6448     ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
6449         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6450     END IF;
6451 
6452     -- Before assigning the new instance as extra node, make sure its operating unit is exactly the same as that
6453     -- of the root instance
6454     l_new_instance_ou := get_operating_unit(l_new_instance_id);
6455 
6456     IF l_root_instance_ou IS NULL THEN
6457         FND_MESSAGE.set_name('AHL', 'AHL_UC_INSTANCE_OU_NULL');
6458         FND_MESSAGE.set_token('INSTANCE', l_parent_instance_id);
6459         FND_MSG_PUB.add;
6460         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6461     ELSIF l_new_instance_ou IS NULL THEN
6462         FND_MESSAGE.set_name('AHL', 'AHL_UC_INSTANCE_OU_NULL');
6463         FND_MESSAGE.set_token('INSTANCE', l_new_instance_id);
6464         FND_MSG_PUB.add;
6465         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6466     ELSIF l_root_instance_ou <> l_new_instance_ou THEN
6467         FND_MESSAGE.set_name('AHL', 'AHL_UC_INSTANCE_OU_UNMATCH');
6468         FND_MESSAGE.set_token('INSTANCE', l_new_instance_id);
6469         FND_MESSAGE.set_token('ROOT_INSTANCE', l_parent_instance_id);
6470         FND_MSG_PUB.add;
6471         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6472     END IF;
6473 
6474     --  Build CSI relationships table
6475     l_csi_relationship_rec.relationship_type_code := 'COMPONENT-OF';
6476     l_csi_relationship_rec.object_id              := l_parent_instance_id;
6477     l_csi_relationship_rec.position_reference     := NULL;
6478     l_csi_relationship_rec.subject_id             := l_new_instance_id;
6479     l_csi_relationship_tbl(1)                     := l_csi_relationship_rec;
6480 
6481     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6482         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, l_full_name,
6483                        ' About to call CSI_II_RELATIONSHIPS_PUB.create_relationship');
6484     END IF;
6485 
6486     CSI_II_RELATIONSHIPS_PUB.create_relationship(
6487         p_api_version      => 1.0,
6488         p_relationship_tbl => l_csi_relationship_tbl,
6489         p_txn_rec          => l_csi_transaction_rec,
6490         x_return_status    => l_return_status,
6491         x_msg_count        => l_msg_count,
6492         x_msg_data         => l_msg_data);
6493 
6494     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6495         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, l_full_name,
6496                        ' After call to CSI_II_RELATIONSHIPS_PUB.create_relationship'||
6497      	               ' l_return_status => '||l_return_status);
6498     END IF;
6499 
6500     IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
6501         RAISE FND_API.G_EXC_ERROR;
6502     ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
6503         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6504     END IF;
6505 
6506     -- Standard check of p_commit
6507     IF FND_API.TO_BOOLEAN(p_commit) THEN
6508         COMMIT WORK;
6509     END IF;
6510 
6511     -- Standard call to get message count and if count is 1, get message info
6512     FND_MSG_PUB.Count_And_Get
6513     ( p_count   => x_msg_count,
6514       p_data    => x_msg_data,
6515       p_encoded => FND_API.G_FALSE
6516     );
6517 
6518     IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
6519         FND_LOG.string(FND_LOG.level_procedure,l_full_name,'End of the API');
6520     END IF;
6521 
6522 EXCEPTION
6523     WHEN FND_API.G_EXC_ERROR THEN
6524         Rollback to create_unassigned_instance;
6525         x_return_status := FND_API.G_RET_STS_ERROR;
6526         FND_MSG_PUB.Count_And_Get( p_count   => x_msg_count,
6527                                    p_data    => x_msg_data,
6528                                    p_encoded => fnd_api.g_false);
6529 
6530     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6531         Rollback to create_unassigned_instance;
6532         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6533         FND_MSG_PUB.Count_And_Get( p_count   => x_msg_count,
6534                                    p_data    => x_msg_data,
6535                                    p_encoded => fnd_api.g_false);
6536 
6537     WHEN OTHERS THEN
6538         Rollback to create_unassigned_instance;
6539         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6540         FND_MSG_PUB.Add_Exc_Msg( p_pkg_name       => G_PKG_NAME,
6541                                  p_procedure_name => l_api_name,
6542                                  p_error_text     => SQLERRM);
6543         FND_MSG_PUB.Count_And_Get( p_count   => x_msg_count,
6544                                    p_data    => x_msg_data,
6545                                    p_encoded => FND_API.G_FALSE);
6546 
6547 END create_unassigned_instance;
6548 
6549 END AHL_UC_INSTANCE_PVT; -- Package body,