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.22.12020000.3 2012/12/07 15:40:51 sareepar 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   l_interchange_reason        ahl_item_associations_tl.interchange_reason%TYPE;
2284 
2285   --Variables needed for CSI API call
2286   l_csi_instance_rec          csi_datastructures_pub.instance_rec;
2287   l_csi_party_rec             csi_datastructures_pub.party_rec;
2288   l_csi_transaction_rec       csi_datastructures_pub.transaction_rec;
2289   l_csi_relationship_rec      csi_datastructures_pub.ii_relationship_rec;
2290   l_csi_relationship_tbl      csi_datastructures_pub.ii_relationship_tbl;
2291   l_csi_party_tbl             csi_datastructures_pub.party_tbl;
2292   l_csi_account_tbl           csi_datastructures_pub.party_account_tbl;
2293   l_csi_pricing_attrib_tbl    csi_datastructures_pub.pricing_attribs_tbl;
2294   l_csi_org_assignments_tbl   csi_datastructures_pub.organization_units_tbl;
2295   l_csi_asset_assignment_tbl  csi_datastructures_pub.instance_asset_tbl;
2296   l_csi_instance_id_lst       csi_datastructures_pub.id_tbl;
2297   l_party_account_rec         csi_datastructures_pub.party_account_rec;
2298   l_csi_extend_attrib_rec     csi_datastructures_pub.extend_attrib_values_rec;
2299   l_csi_ext_attrib_values_tbl csi_datastructures_pub.extend_attrib_values_tbl;
2300 
2301   CURSOR check_uc_header IS
2302     SELECT A.unit_config_header_id,
2303            A.object_version_number,
2304            A.unit_config_status_code,
2305            A.active_uc_status_code,
2306            A.csi_item_instance_id,
2307            B.relationship_id
2308       FROM ahl_unit_config_headers A,
2309            ahl_mc_relationships B
2310      WHERE A.unit_config_header_id = p_uc_header_id
2311        AND trunc(nvl(A.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
2312        AND A.master_config_id = B.mc_header_id
2313        AND B.parent_relationship_id IS NULL;
2314   l_check_uc_header check_uc_header%ROWTYPE;
2315   CURSOR get_uc_descendants(c_instance_id NUMBER) IS
2316     SELECT relationship_id,
2317            object_version_number,
2318            object_id,
2319            subject_id,
2320            to_number(position_reference) position_id
2321       FROM csi_ii_relationships
2322 START WITH object_id = c_instance_id
2323        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
2324        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
2325 CONNECT BY object_id = PRIOR subject_id
2326        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
2327        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
2328   --Cursor to check whether the c_parent_relationship_id is the parent of
2329   --c_child_relationshp_id or c_child_relationship_id's own parent as the top node of the sub-config
2330   --can be installed in c_parent_relationship_id
2331   CURSOR check_parent_relationship(c_child_relationship_id NUMBER, c_parent_relationship_id NUMBER) IS
2332     SELECT 'X'
2333       FROM ahl_mc_relationships
2334      WHERE relationship_id = c_child_relationship_id
2335        AND (parent_relationship_id = c_parent_relationship_id OR
2336             mc_header_id IN (SELECT mc_header_id
2337                                FROM ahl_mc_config_relations
2338                               WHERE relationship_id = c_parent_relationship_id
2339                                 AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
2340                                 AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)));
2341 
2342   --Cursor to check whether c_parent_instance_id's child position c_relationship_id is empty
2343   CURSOR check_position_empty(c_parent_instance_id NUMBER, c_relationship_id NUMBER) IS
2344     SELECT subject_id
2345       FROM csi_ii_relationships
2346      WHERE object_id = c_parent_instance_id
2347        AND position_reference = to_char(c_relationship_id)
2348        AND subject_id IS NOT NULL
2349        AND relationship_type_code = 'COMPONENT-OF'
2350        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
2351        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
2352 
2353   CURSOR csi_item_instance_csr(c_csi_instance_id IN NUMBER) IS
2354     SELECT location_id,
2355            location_type_code,
2356            party_id,
2357            party_source_table,
2358            instance_party_id,
2359            csi.wip_job_id
2360       FROM csi_item_instances csi, csi_i_parties p
2361      WHERE csi.instance_id = p.instance_id
2362        AND p.relationship_type_code = 'OWNER'
2363        AND csi.instance_id = c_csi_instance_id
2364        AND trunc(SYSDATE) < trunc(nvl(csi.active_end_date, SYSDATE+1));
2365   l_uc_owner_loc_rec          csi_item_instance_csr%ROWTYPE;
2366 
2367   CURSOR csi_ip_accounts_csr(c_instance_party_id IN NUMBER) IS
2368     SELECT party_account_id
2369       FROM csi_ip_accounts
2370      WHERE relationship_type_code = 'OWNER'
2371        AND instance_party_id = c_instance_party_id
2372        AND trunc(SYSDATE) >= trunc(nvl(active_start_date, SYSDATE))
2373        AND trunc(SYSDATE) < trunc(nvl(active_end_date, SYSDATE+1));
2374 
2375   --Cursor to check the uniqueness of the sub unit
2376   CURSOR check_uc_name_unique(c_uc_name VARCHAR2) IS
2377     SELECT 'X'
2378       FROM ahl_unit_config_headers
2379      WHERE name = c_uc_name
2380        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
2381 
2382   --Cursor to check the validatiy of the mc_header_id (for sub config) according to the mc_name, mc_revision and
2383   --relationship_id (in parent MC)
2384   CURSOR get_sub_mc_header(c_mc_name VARCHAR2, c_mc_revision VARCHAR2, c_relationship_id NUMBER) IS
2385     SELECT H.mc_header_id,
2386            R.relationship_id
2387       FROM ahl_mc_headers_b H,
2388            ahl_mc_relationships R
2389      WHERE H.mc_header_id = R.mc_header_id
2390        AND R.parent_relationship_id IS NULL
2391        AND trunc(nvl(R.active_start_date, SYSDATE)) <= trunc(SYSDATE)
2392        AND trunc(nvl(R.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
2393        AND H.name = c_mc_name
2394        AND H.revision = c_mc_revision
2395        AND H.mc_header_id IN (SELECT mc_header_id
2396                               FROM ahl_mc_config_relations
2397                              WHERE relationship_id = c_relationship_id
2398                                AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
2399                                AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE));
2400 
2401   -- SATHAPLI::ER 7419780, 24-Aug-2009, fetch the INTERCHANGE_REASON from the table ahl_item_associations_tl too
2402   CURSOR get_interchange_type (c_instance_id NUMBER, c_relationship_id NUMBER) IS
2403     SELECT i.interchange_type_code,
2404            itl.interchange_reason
2405       FROM csi_item_instances c,
2406            ahl_item_associations_b i,
2407            ahl_item_associations_tl itl,
2408            ahl_mc_relationships m
2409      WHERE m.relationship_id = c_relationship_id
2410        AND c.instance_id = c_instance_id
2411        AND m.item_group_id = i.item_group_id
2412        AND c.inventory_item_id = i.inventory_item_id
2413        AND c.inv_master_organization_id = i.inventory_org_id
2414        AND itl.item_association_id = i.item_association_id
2415        AND itl.language = USERENV('LANG')
2416        AND (c.inventory_revision IS NULL OR
2417             i.revision is NULL OR
2418             (c.inventory_revision IS NOT NULL AND
2419              i.revision IS NOT NULL AND
2420              c.inventory_revision = i.revision));
2421    --Added this last condition due to the impact of bug fixing 4102152, added by Jerry on 01/05/2005
2422    --Need to confirm which one is more accurate here to use c.inv_master_organization_id or
2423    --c.last_vld_organization_id
2424 
2425 BEGIN
2426   --Initialize API return status to success
2427   x_return_status := FND_API.G_RET_STS_SUCCESS;
2428 
2429   -- Standard Start of API savepoint
2430   SAVEPOINT install_new_instance;
2431 
2432   --Standard call to check for call compatibility.
2433   IF NOT FND_API.compatible_api_call(
2434     l_api_version,
2435     p_api_version,
2436     l_api_name,
2437     G_PKG_NAME)
2438   THEN
2439     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2440   END IF;
2441 
2442   --Initialize message list if p_init_msg_list is set to TRUE.
2443   IF FND_API.to_boolean( p_init_msg_list ) THEN
2444     FND_MSG_PUB.initialize;
2445   END IF;
2446 
2447   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2448     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
2449                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Begin API',
2450                    'At the start of the procedure');
2451   END IF;
2452 
2453   --Validate input parameters p_prod_user_flag
2454   IF upper(p_prod_user_flag) <> 'Y' AND upper(p_prod_user_flag) <> 'N' THEN
2455     FND_MESSAGE.set_name('AHL','AHL_UC_API_PARAMETER_INVALID');
2456     FND_MESSAGE.set_token('NAME', 'prod_user_flag');
2457     FND_MESSAGE.set_token('VALUE', p_prod_user_flag);
2458     FND_MSG_PUB.add;
2459     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2460   END IF;
2461   --Validate input parameter p_uc_header_id, its two statuses
2462   OPEN check_uc_header;
2463   FETCH check_uc_header INTO l_check_uc_header;
2464   IF check_uc_header%NOTFOUND THEN
2465     FND_MESSAGE.set_name('AHL','AHL_UC_API_PARAMETER_INVALID');
2466     FND_MESSAGE.set_token('NAME', 'uc_header_id');
2467     FND_MESSAGE.set_token('VALUE', p_uc_header_id);
2468     FND_MSG_PUB.add;
2469     CLOSE check_uc_header;
2470     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2471   ELSE
2472 
2473     -- ACL :: Changes for R12
2474     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
2475       FND_MESSAGE.set_name( 'AHL','AHL_UC_INVALID_Q_ACTION' );
2476       FND_MSG_PUB.add;
2477       CLOSE check_uc_header;
2478       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2479     END IF;
2480 
2481     ahl_util_uc_pkg.get_root_uc_attr(p_uc_header_id,
2482                                      l_root_uc_header_id,
2483                                      l_root_instance_id,
2484                                      l_root_uc_status_code,
2485                                      l_root_active_uc_status_code,
2486                                      l_root_uc_ovn);
2487     IF (p_prod_user_flag = 'Y' AND --For production user, no need to confirm either one of the statuses is not APPROVAL_PENDING
2488         l_root_uc_status_code NOT IN ('COMPLETE', 'INCOMPLETE')) THEN
2489       FND_MESSAGE.set_name( 'AHL','AHL_UC_STATUS_NOT_ACTIVE' );
2490       FND_MSG_PUB.add;
2491       CLOSE check_uc_header;
2492       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2493     ELSIF (p_prod_user_flag = 'N' AND
2494            (l_root_uc_status_code = 'APPROVAL_PENDING' OR
2495             l_root_active_uc_status_code = 'APPROVAL_PENDING')) THEN
2496       FND_MESSAGE.set_name( 'AHL','AHL_UC_STATUS_PENDING' );
2497       FND_MESSAGE.set_token( 'UC_HEADER_ID', l_root_uc_header_id);
2498       FND_MSG_PUB.add;
2499       CLOSE check_uc_header;
2500       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2501     ELSE
2502       CLOSE check_uc_header;
2503     END IF;
2504   END IF;
2505 
2506   --Get the operating unit of the root instance.
2507   l_root_instance_ou := get_operating_unit(l_root_instance_id);
2508 
2509   --Make sure p_parent_instance_id is installed in the UC
2510   IF p_parent_instance_id = l_check_uc_header.csi_item_instance_id THEN
2511     --The parent instance is the root node
2512     l_parent_relationship_id := l_check_uc_header.relationship_id;
2513   ELSE
2514     FOR l_get_uc_descendant IN get_uc_descendants(l_check_uc_header.csi_item_instance_id) LOOP
2515       l_csi_relationship_id := l_get_uc_descendant.relationship_id;
2516       l_object_version_number := l_get_uc_descendant.object_version_number;
2517       l_object_id := l_get_uc_descendant.object_id;
2518       l_subject_id := l_get_uc_descendant.subject_id;
2519       l_parent_relationship_id := l_get_uc_descendant.position_id;
2520       EXIT WHEN l_subject_id = p_parent_instance_id;
2521     END LOOP;
2522     --Ensure the instance is installed in this UC and not an extra node
2523     IF (l_subject_id <> p_parent_instance_id OR
2524         p_parent_instance_id IS NULL OR
2525         l_subject_id IS NULL OR
2526         l_parent_relationship_id IS NULL) THEN
2527       --Do we allow an extra node's attributes to be changed?
2528       FND_MESSAGE.set_name( 'AHL','AHL_UC_INSTANCE_NOT_IN_UC' );
2529       FND_MESSAGE.set_token('INSTANCE', p_parent_instance_id);
2530       FND_MSG_PUB.add;
2531       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2532     END IF;
2533   END IF;
2534 
2535   --Then validate p_x_uc_instance_rec.relationship_id can be child of l_parent_relationship_id
2536   OPEN check_parent_relationship(p_x_uc_instance_rec.relationship_id, l_parent_relationship_id);
2537   FETCH check_parent_relationship INTO l_dummy_char;
2538   IF check_parent_relationship%NOTFOUND THEN
2539     FND_MESSAGE.set_name( 'AHL','AHL_UC_POSITION_MISMATCH' );
2540     FND_MESSAGE.set_token('CHILD', p_x_uc_instance_rec.relationship_id);
2541     FND_MESSAGE.set_token('PARENT', l_parent_relationship_id);
2542     FND_MSG_PUB.add;
2543     CLOSE check_parent_relationship;
2544     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2545   ELSE
2546     CLOSE check_parent_relationship;
2547   END IF;
2548 
2549   --Make sure position p_x_uc_instance_rec.relationship_id is empty
2550   OPEN check_position_empty(p_parent_instance_id, p_x_uc_instance_rec.relationship_id);
2551   FETCH check_position_empty INTO l_dummy;
2552   IF check_position_empty%FOUND THEN
2553     FND_MESSAGE.set_name( 'AHL','AHL_UC_POSITION_INSTALLED' );
2554     FND_MESSAGE.set_token('POSITION', p_x_uc_instance_rec.relationship_id);
2555     FND_MSG_PUB.add;
2556     CLOSE check_position_empty;
2557     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2558   ELSE
2559     CLOSE check_position_empty;
2560   END IF;
2561   --When creating the new instances, the "From Inventory" Serial Tag should not be used anymore.
2562   --mpothuku added on 13-Jul-2007 to fix the Bug 4337259
2563   IF(p_x_uc_instance_rec.sn_tag_code is not null AND p_x_uc_instance_rec.sn_tag_code = 'INVENTORY') THEN
2564     FND_MESSAGE.set_name( 'AHL','AHL_UC_SER_TG_CR_INVEN' );
2565     FND_MSG_PUB.add;
2566   END IF;
2567   --mpothuku End
2568 
2569   --Check Error Message stack.
2570   l_msg_count := FND_MSG_PUB.count_msg;
2571   IF l_msg_count > 0 THEN
2572     RAISE  FND_API.G_EXC_ERROR;
2573   END IF;
2574 
2575   --Validate Inventory details.
2576   validate_uc_invdetails (p_x_uc_instance_rec.inventory_item_id,
2577                           p_x_uc_instance_rec.inventory_org_id,
2578                           p_x_uc_instance_rec.serial_number,
2579                           p_x_uc_instance_rec.sn_tag_code,
2580                           p_x_uc_instance_rec.quantity,
2581                           p_x_uc_instance_rec.uom_code,
2582                           p_x_uc_instance_rec.revision,
2583                           p_x_uc_instance_rec.lot_number,
2584                           NULL,
2585                           l_concatenated_segments);
2586 
2587   --Check Error Message stack.
2588   l_msg_count := FND_MSG_PUB.count_msg;
2589   IF l_msg_count > 0 THEN
2590     RAISE  FND_API.G_EXC_ERROR;
2591   END IF;
2592 
2593   --Check all sub mc_name, mc_revision and uc_name are NULL or NOT NULL
2594   IF (p_x_sub_uc_rec.mc_name IS NOT NULL AND (p_x_sub_uc_rec.mc_revision IS NULL OR
2595                                                 p_x_sub_uc_rec.uc_name IS NULL) OR
2596       p_x_sub_uc_rec.mc_revision IS NOT NULL AND (p_x_sub_uc_rec.mc_name IS NULL OR
2597                                                 p_x_sub_uc_rec.uc_name IS NULL) OR
2598       p_x_sub_uc_rec.uc_name IS NOT NULL AND (p_x_sub_uc_rec.mc_revision IS NULL OR
2599                                                 p_x_sub_uc_rec.mc_name IS NULL))
2600   THEN
2601     FND_MESSAGE.set_name('AHL','AHL_UC_SUB_UNIT_INFO_MISSING');
2602     FND_MSG_PUB.add;
2603     RAISE FND_API.G_EXC_ERROR;
2604   END IF;
2605 
2606   --Check the sub unit name is unique
2607   IF p_x_sub_uc_rec.uc_name IS NOT NULL THEN
2608     OPEN check_uc_name_unique(p_x_sub_uc_rec.uc_name);
2609     FETCH check_uc_name_unique INTO l_dummy_char;
2610     IF check_uc_name_unique%FOUND THEN
2611       FND_MESSAGE.set_name('AHL','AHL_UC_NAME_DUPLICATE');
2612       FND_MESSAGE.set_token('NAME', p_x_sub_uc_rec.uc_name);
2613       FND_MSG_PUB.add;
2614       CLOSE check_uc_name_unique;
2615       RAISE FND_API.G_EXC_ERROR;
2616     ELSE
2617       CLOSE check_uc_name_unique;
2618     END IF;
2619   END IF;
2620 
2621   --Derive mc_header_id from mc_name and mc_revision
2622   IF p_x_sub_uc_rec.mc_name IS NOT NULL THEN
2623     OPEN get_sub_mc_header(p_x_sub_uc_rec.mc_name,
2624                            p_x_sub_uc_rec.mc_revision,
2625                            p_x_uc_instance_rec.relationship_id);
2626     FETCH get_sub_mc_header INTO l_sub_mc_header_id, l_top_relationship_id;
2627     IF get_sub_mc_header%NOTFOUND THEN
2628       FND_MESSAGE.set_name('AHL','AHL_UC_SUB_MC_INVALID');
2629       FND_MESSAGE.set_token('NAME', p_x_sub_uc_rec.mc_name);
2630       FND_MESSAGE.set_token('REVISION', p_x_sub_uc_rec.mc_revision);
2631       FND_MSG_PUB.add;
2632       CLOSE get_sub_mc_header;
2633       RAISE FND_API.G_EXC_ERROR;
2634     ELSE
2635       CLOSE get_sub_mc_header;
2636     END IF;
2637   END IF;
2638 
2639   IF (l_top_relationship_id IS NOT NULL) THEN
2640     l_position_id := l_top_relationship_id;
2641   ELSE
2642     l_position_id := p_x_uc_instance_rec.relationship_id;
2643   END IF;
2644 
2645   --Validate whether an item can be installed into a position, the position refers to the
2646   --top node position in the sub UC if sub UC information is provided otherwise it refers
2647   --to the position from Parent UC.
2648   AHL_UTIL_UC_PKG.validate_for_position(l_position_id,
2649                                         p_x_uc_instance_rec.inventory_Item_id,
2650                                         p_x_uc_instance_rec.inventory_Org_id,
2651                                         p_x_uc_instance_rec.quantity,
2652                                         p_x_uc_instance_rec.revision,
2653                                         p_x_uc_instance_rec.uom_code,
2654                                         NULL,
2655                                         -- SATHAPLI::FP OGMA Issue# 105 - Non-Serialized Item Maintenance, 05-Dec-2007
2656                                         -- Pass 'N' for p_ignore_quant_vald.
2657                                         'N',
2658                                         l_item_assoc_id);
2659 
2660   --Check Error Message stack.
2661   l_msg_count := FND_MSG_PUB.count_msg;
2662   IF l_msg_count > 0 THEN
2663     RAISE  FND_API.G_EXC_ERROR;
2664   END IF;
2665 
2666   --Validate manufacturing date.
2667   IF (p_x_uc_instance_rec.mfg_date IS NOT NULL AND
2668       p_x_uc_instance_rec.mfg_date <> FND_API.G_MISS_DATE) THEN
2669     IF (p_x_uc_instance_rec.mfg_date > SYSDATE) THEN
2670       FND_MESSAGE.set_name('AHL','AHL_UC_MFGDATE_INVALID');
2671       FND_MESSAGE.set_token('DATE',p_x_uc_instance_rec.mfg_date);
2672       FND_MESSAGE.set_token('INV_ITEM',l_concatenated_segments);
2673       FND_MSG_PUB.add;
2674       --dbms_output.put_line('Mfg date invalid.');
2675     END IF;
2676   END IF;
2677 
2678   --Validate installation date.
2679   --Keep the installation date validation only for production user (04/21/2004)
2680   IF (p_x_uc_instance_rec.install_date IS NOT NULL AND
2681       p_x_uc_instance_rec.install_date <> FND_API.G_MISS_DATE) THEN
2682     IF (p_prod_user_flag = 'Y' AND trunc(p_x_uc_instance_rec.install_date) > trunc(SYSDATE)) THEN
2683       FND_MESSAGE.set_name('AHL','AHL_UC_INSTDATE_INVALID');
2684       FND_MESSAGE.set_token('DATE',p_x_uc_instance_rec.install_date);
2685       FND_MESSAGE.set_token('POSN_REF',p_x_uc_instance_rec.relationship_id);
2686       FND_MSG_PUB.add;
2687       --dbms_output.put_line('Installation date invalid.');
2688     END IF;
2689   END IF;
2690 
2691   -- Build CSI records and call API.
2692   -- First get unit config location and owner details.
2693   OPEN csi_item_instance_csr(p_parent_instance_id);
2694   FETCH csi_item_instance_csr INTO l_uc_owner_loc_rec;
2695   IF (csi_item_instance_csr%NOTFOUND) THEN
2696     CLOSE csi_item_instance_csr;
2697     FND_MESSAGE.set_name('AHL','AHL_UC_CSII_INVALID');
2698     FND_MESSAGE.set_token('CSII',p_parent_instance_id);
2699     FND_MESSAGE.Set_Token('POSN_REF',p_x_uc_instance_rec.relationship_id);
2700     FND_MSG_PUB.add;
2701     --dbms_output.put_line('Top node item instance does not exist.');
2702     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2703   END IF;
2704   CLOSE csi_item_instance_csr;
2705 
2706   --Set csi instance record
2707   l_csi_instance_rec.inventory_item_id := p_x_uc_instance_rec.inventory_item_id;
2708   l_csi_instance_rec.vld_organization_id := p_x_uc_instance_rec.inventory_org_id;
2709   l_csi_instance_rec.quantity := p_x_uc_instance_rec.quantity;
2710   l_csi_instance_rec.unit_of_measure := p_x_uc_instance_rec.uom_code;
2711   l_csi_instance_rec.install_date := p_x_uc_instance_rec.install_date;
2712   l_csi_instance_rec.location_id := l_uc_owner_loc_rec.location_id;
2713   l_csi_instance_rec.location_type_code := l_uc_owner_loc_rec.location_type_code;
2714 
2715   --In case item is in WIP; copy the parent WIP job ID to the component.
2716   l_csi_instance_rec.wip_job_id := l_uc_owner_loc_rec.wip_job_id;
2717 --  IF (p_x_uc_instance_rec.sn_tag_code = 'INVENTORY') THEN
2718 --    l_csi_instance_rec.mfg_serial_number_flag := 'Y';
2719 --  ELSE
2720 --  Changed by jaramana on April 26, 2005,
2721 --  As per request from IB team (Briestly Manesh), always setting to N
2722     l_csi_instance_rec.mfg_serial_number_flag := 'N';
2723 --  END IF;
2724 
2725   IF (p_x_uc_instance_rec.serial_number IS NOT NULL AND
2726       p_x_uc_instance_rec.serial_number <> FND_API.G_MISS_CHAR)  THEN
2727     l_csi_instance_rec.serial_number := p_x_uc_instance_rec.serial_number;
2728   END IF;
2729 
2730   IF (p_x_uc_instance_rec.lot_number IS NOT NULL AND
2731       p_x_uc_instance_rec.lot_number <> FND_API.G_MISS_CHAR) THEN
2732     l_csi_instance_rec.lot_number := p_x_uc_instance_rec.lot_number;
2733   END IF;
2734 
2735   IF (p_x_uc_instance_rec.revision IS NOT NULL AND
2736       p_x_uc_instance_rec.revision <> FND_API.G_MISS_CHAR) THEN
2737     l_csi_instance_rec.inventory_revision := p_x_uc_instance_rec.revision;
2738   END IF;
2739 
2740   --l_csi_instance_rec.instance_usage_code := 'IN_SERVICE';
2741   l_csi_instance_rec.instance_usage_code := NULL;
2742 
2743   -- SATHAPLI::FP ER 6453212, 10-Nov-2008
2744   -- populate the flexfield data in the CSI record
2745   IF (p_x_uc_instance_rec.context IS NOT NULL AND
2746       p_x_uc_instance_rec.context <> FND_API.G_MISS_CHAR) THEN
2747     l_csi_instance_rec.context := p_x_uc_instance_rec.context;
2748   END IF;
2749 
2750   IF (p_x_uc_instance_rec.attribute1 IS NOT NULL AND
2751       p_x_uc_instance_rec.attribute1 <> FND_API.G_MISS_CHAR) THEN
2752     l_csi_instance_rec.attribute1 := p_x_uc_instance_rec.attribute1;
2753   END IF;
2754 
2755   IF (p_x_uc_instance_rec.attribute2 IS NOT NULL AND
2756       p_x_uc_instance_rec.attribute2 <> FND_API.G_MISS_CHAR) THEN
2757     l_csi_instance_rec.attribute2 := p_x_uc_instance_rec.attribute2;
2758   END IF;
2759 
2760   IF (p_x_uc_instance_rec.attribute3 IS NOT NULL AND
2761       p_x_uc_instance_rec.attribute3 <> FND_API.G_MISS_CHAR) THEN
2762     l_csi_instance_rec.attribute3 := p_x_uc_instance_rec.attribute3;
2763   END IF;
2764 
2765   IF (p_x_uc_instance_rec.attribute4 IS NOT NULL AND
2766       p_x_uc_instance_rec.attribute4 <> FND_API.G_MISS_CHAR) THEN
2767     l_csi_instance_rec.attribute4 := p_x_uc_instance_rec.attribute4;
2768   END IF;
2769 
2770   IF (p_x_uc_instance_rec.attribute5 IS NOT NULL AND
2771       p_x_uc_instance_rec.attribute5 <> FND_API.G_MISS_CHAR) THEN
2772     l_csi_instance_rec.attribute5 := p_x_uc_instance_rec.attribute5;
2773   END IF;
2774 
2775   IF (p_x_uc_instance_rec.attribute6 IS NOT NULL AND
2776       p_x_uc_instance_rec.attribute6 <> FND_API.G_MISS_CHAR) THEN
2777     l_csi_instance_rec.attribute6 := p_x_uc_instance_rec.attribute6;
2778   END IF;
2779 
2780   IF (p_x_uc_instance_rec.attribute7 IS NOT NULL AND
2781       p_x_uc_instance_rec.attribute7 <> FND_API.G_MISS_CHAR) THEN
2782     l_csi_instance_rec.attribute7 := p_x_uc_instance_rec.attribute7;
2783   END IF;
2784 
2785   IF (p_x_uc_instance_rec.attribute8 IS NOT NULL AND
2786       p_x_uc_instance_rec.attribute8 <> FND_API.G_MISS_CHAR) THEN
2787     l_csi_instance_rec.attribute8 := p_x_uc_instance_rec.attribute8;
2788   END IF;
2789 
2790   IF (p_x_uc_instance_rec.attribute9 IS NOT NULL AND
2791       p_x_uc_instance_rec.attribute9 <> FND_API.G_MISS_CHAR) THEN
2792     l_csi_instance_rec.attribute9 := p_x_uc_instance_rec.attribute9;
2793   END IF;
2794 
2795   IF (p_x_uc_instance_rec.attribute10 IS NOT NULL AND
2796       p_x_uc_instance_rec.attribute10 <> FND_API.G_MISS_CHAR) THEN
2797     l_csi_instance_rec.attribute10 := p_x_uc_instance_rec.attribute10;
2798   END IF;
2799 
2800   IF (p_x_uc_instance_rec.attribute11 IS NOT NULL AND
2801       p_x_uc_instance_rec.attribute11 <> FND_API.G_MISS_CHAR) THEN
2802     l_csi_instance_rec.attribute11 := p_x_uc_instance_rec.attribute11;
2803   END IF;
2804 
2805   IF (p_x_uc_instance_rec.attribute12 IS NOT NULL AND
2806       p_x_uc_instance_rec.attribute12 <> FND_API.G_MISS_CHAR) THEN
2807     l_csi_instance_rec.attribute12 := p_x_uc_instance_rec.attribute12;
2808   END IF;
2809 
2810   IF (p_x_uc_instance_rec.attribute13 IS NOT NULL AND
2811       p_x_uc_instance_rec.attribute13 <> FND_API.G_MISS_CHAR) THEN
2812     l_csi_instance_rec.attribute13 := p_x_uc_instance_rec.attribute13;
2813   END IF;
2814 
2815   IF (p_x_uc_instance_rec.attribute14 IS NOT NULL AND
2816       p_x_uc_instance_rec.attribute14 <> FND_API.G_MISS_CHAR) THEN
2817     l_csi_instance_rec.attribute14 := p_x_uc_instance_rec.attribute14;
2818   END IF;
2819 
2820   IF (p_x_uc_instance_rec.attribute15 IS NOT NULL AND
2821       p_x_uc_instance_rec.attribute15 <> FND_API.G_MISS_CHAR) THEN
2822     l_csi_instance_rec.attribute15 := p_x_uc_instance_rec.attribute15;
2823   END IF;
2824 
2825   IF (p_x_uc_instance_rec.attribute16 IS NOT NULL AND
2826       p_x_uc_instance_rec.attribute16 <> FND_API.G_MISS_CHAR) THEN
2827     l_csi_instance_rec.attribute16 := p_x_uc_instance_rec.attribute16;
2828   END IF;
2829 
2830   IF (p_x_uc_instance_rec.attribute17 IS NOT NULL AND
2831       p_x_uc_instance_rec.attribute17 <> FND_API.G_MISS_CHAR) THEN
2832     l_csi_instance_rec.attribute17 := p_x_uc_instance_rec.attribute17;
2833   END IF;
2834 
2835   IF (p_x_uc_instance_rec.attribute18 IS NOT NULL AND
2836       p_x_uc_instance_rec.attribute18 <> FND_API.G_MISS_CHAR) THEN
2837     l_csi_instance_rec.attribute18 := p_x_uc_instance_rec.attribute18;
2838   END IF;
2839 
2840   IF (p_x_uc_instance_rec.attribute19 IS NOT NULL AND
2841       p_x_uc_instance_rec.attribute19 <> FND_API.G_MISS_CHAR) THEN
2842     l_csi_instance_rec.attribute19 := p_x_uc_instance_rec.attribute19;
2843   END IF;
2844 
2845   IF (p_x_uc_instance_rec.attribute20 IS NOT NULL AND
2846       p_x_uc_instance_rec.attribute20 <> FND_API.G_MISS_CHAR) THEN
2847     l_csi_instance_rec.attribute20 := p_x_uc_instance_rec.attribute20;
2848   END IF;
2849 
2850   IF (p_x_uc_instance_rec.attribute21 IS NOT NULL AND
2851       p_x_uc_instance_rec.attribute21 <> FND_API.G_MISS_CHAR) THEN
2852     l_csi_instance_rec.attribute21 := p_x_uc_instance_rec.attribute21;
2853   END IF;
2854 
2855   IF (p_x_uc_instance_rec.attribute22 IS NOT NULL AND
2856       p_x_uc_instance_rec.attribute22 <> FND_API.G_MISS_CHAR) THEN
2857     l_csi_instance_rec.attribute22 := p_x_uc_instance_rec.attribute22;
2858   END IF;
2859 
2860   IF (p_x_uc_instance_rec.attribute23 IS NOT NULL AND
2861       p_x_uc_instance_rec.attribute23 <> FND_API.G_MISS_CHAR) THEN
2862     l_csi_instance_rec.attribute23 := p_x_uc_instance_rec.attribute23;
2863   END IF;
2864 
2865   IF (p_x_uc_instance_rec.attribute24 IS NOT NULL AND
2866       p_x_uc_instance_rec.attribute24 <> FND_API.G_MISS_CHAR) THEN
2867     l_csi_instance_rec.attribute24 := p_x_uc_instance_rec.attribute24;
2868   END IF;
2869 
2870   IF (p_x_uc_instance_rec.attribute25 IS NOT NULL AND
2871       p_x_uc_instance_rec.attribute25 <> FND_API.G_MISS_CHAR) THEN
2872     l_csi_instance_rec.attribute25 := p_x_uc_instance_rec.attribute25;
2873   END IF;
2874 
2875   IF (p_x_uc_instance_rec.attribute26 IS NOT NULL AND
2876       p_x_uc_instance_rec.attribute26 <> FND_API.G_MISS_CHAR) THEN
2877     l_csi_instance_rec.attribute26 := p_x_uc_instance_rec.attribute26;
2878   END IF;
2879 
2880   IF (p_x_uc_instance_rec.attribute27 IS NOT NULL AND
2881       p_x_uc_instance_rec.attribute27 <> FND_API.G_MISS_CHAR) THEN
2882     l_csi_instance_rec.attribute27 := p_x_uc_instance_rec.attribute27;
2883   END IF;
2884 
2885   IF (p_x_uc_instance_rec.attribute28 IS NOT NULL AND
2886       p_x_uc_instance_rec.attribute28 <> FND_API.G_MISS_CHAR) THEN
2887     l_csi_instance_rec.attribute28 := p_x_uc_instance_rec.attribute28;
2888   END IF;
2889 
2890   IF (p_x_uc_instance_rec.attribute29 IS NOT NULL AND
2891       p_x_uc_instance_rec.attribute29 <> FND_API.G_MISS_CHAR) THEN
2892     l_csi_instance_rec.attribute29 := p_x_uc_instance_rec.attribute29;
2893   END IF;
2894 
2895   IF (p_x_uc_instance_rec.attribute30 IS NOT NULL AND
2896       p_x_uc_instance_rec.attribute30 <> FND_API.G_MISS_CHAR) THEN
2897     l_csi_instance_rec.attribute30 := p_x_uc_instance_rec.attribute30;
2898   END IF;
2899 
2900   --Build csi extended attribs.
2901   IF (p_x_uc_instance_rec.mfg_date IS NOT NULL AND
2902       p_x_uc_instance_rec.mfg_date <> FND_API.G_MISS_DATE) THEN
2903     AHL_UTIL_UC_PKG.getcsi_attribute_id('AHL_MFG_DATE',l_attribute_id, l_return_val);
2904     IF NOT(l_return_val) THEN
2905       FND_MESSAGE.set_name('AHL','AHL_UC_ATTRIB_CODE_MISSING');
2906       FND_MESSAGE.set_token('CODE', 'AHL_MFG_DATE');
2907       FND_MSG_PUB.add;
2908       --dbms_output.put_line('Attribute code for AHL_MFG_DATE not found');
2909     ELSE
2910       l_csi_extend_attrib_rec.attribute_id := l_attribute_id;
2911       l_csi_extend_attrib_rec.attribute_value := to_char(p_x_uc_instance_rec.mfg_date, 'DD/MM/YYYY');
2912       l_subscript := l_subscript + 1;
2913       l_csi_ext_attrib_values_tbl(l_subscript) := l_csi_extend_attrib_rec;
2914     END IF;
2915   END IF;
2916 
2917   IF (p_x_uc_instance_rec.serial_number IS NOT NULL AND
2918       p_x_uc_instance_rec.serial_number <> FND_API.G_MISS_CHAR) THEN
2919     AHL_UTIL_UC_PKG.getcsi_attribute_id('AHL_TEMP_SERIAL_NUM',l_attribute_id, l_return_val);
2920 
2921     IF NOT(l_return_val) THEN
2922       FND_MESSAGE.set_name('AHL','AHL_UC_ATTRIB_CODE_MISSING');
2923       FND_MESSAGE.set_token('CODE', 'AHL_TEMP_SERIAL_NUM');
2924       FND_MSG_PUB.add;
2925       --dbms_output.put_line('Attribute code for TEMP_SERIAL_NUM not found');
2926     ELSE
2927       l_csi_extend_attrib_rec.attribute_id := l_attribute_id;
2928       l_csi_extend_attrib_rec.attribute_value := p_x_uc_instance_rec.sn_tag_code;
2929       l_csi_ext_attrib_values_tbl(l_subscript+1) := l_csi_extend_attrib_rec;
2930     END IF;
2931   END IF;
2932 
2933   --Build CSI party record.
2934   l_csi_party_rec.party_id := l_uc_owner_loc_rec.party_id;
2935   l_csi_party_rec.relationship_type_code := 'OWNER';
2936   l_csi_party_rec.party_source_table := l_uc_owner_loc_rec.party_source_table;
2937   l_csi_party_rec.contact_flag := 'N';
2938   l_csi_party_tbl(1) := l_csi_party_rec;
2939 
2940   --dbms_output.put_line('before build accounts:...');
2941   --Build CSI accounts table.
2942   FOR party_ip_acct IN csi_ip_accounts_csr(l_uc_owner_loc_rec.instance_party_id)
2943   LOOP
2944     l_party_account_rec.party_account_id := party_ip_acct.party_account_id;
2945     l_party_account_rec.relationship_type_code := 'OWNER';
2946     l_party_account_rec.parent_tbl_index := 1;
2947     i := i + 1;
2948     l_csi_account_tbl(i) := l_party_account_rec;
2949   END LOOP;
2950 
2951   --Build CSI transaction record, first get transaction_type_id
2952   AHL_Util_UC_Pkg.getcsi_transaction_id('UC_CREATE',l_transaction_type_id, l_return_val);
2953 
2954   IF NOT(l_return_val) THEN
2955     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2956   END IF;
2957 
2958   l_csi_transaction_rec.source_transaction_date := SYSDATE;
2959   l_csi_transaction_rec.transaction_type_id := l_transaction_type_id;
2960 
2961   --Check Error Message stack.
2962   l_msg_count := FND_MSG_PUB.count_msg;
2963   IF l_msg_count > 0 THEN
2964     RAISE  FND_API.G_EXC_ERROR;
2965   END IF;
2966 
2967    --Call CSI API to create instance
2968   CSI_ITEM_INSTANCE_PUB.create_item_instance(
2969                        p_api_version            => 1.0,
2970                        p_instance_rec           => l_csi_instance_rec,
2971                        p_txn_rec                => l_csi_transaction_rec,
2972                        p_ext_attrib_values_tbl  => l_csi_ext_attrib_values_tbl,
2973                        p_party_tbl              => l_csi_party_tbl,
2974                        p_account_tbl            => l_csi_account_tbl,
2975                        p_pricing_attrib_tbl     => l_csi_pricing_attrib_tbl,
2976                        p_org_assignments_tbl    => l_csi_org_assignments_tbl,
2977                        p_asset_assignment_tbl   => l_csi_asset_assignment_tbl,
2978                        x_return_status          => l_return_status,
2979                        x_msg_count              => l_msg_count,
2980                        x_msg_data               => l_msg_data);
2981 
2982   --Assign out parameters.
2983 
2984   l_new_instance_id := l_csi_instance_rec.instance_id;
2985   l_new_csi_instance_ovn := l_csi_instance_rec.object_version_number;
2986   p_x_uc_instance_rec.instance_id := l_new_instance_id;
2987   p_x_uc_instance_rec.object_version_number := l_new_csi_instance_ovn;
2988 
2989   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2990     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
2991                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': within API',
2992                    ' After calling create_item_instance and instance_id = '||l_csi_instance_rec.instance_id||
2993                    ' l_return_status ='||l_return_status||
2994                    ' p_x_uc_instance_rec.instance_id='||p_x_uc_instance_rec.instance_id);
2995   END IF;
2996 
2997   IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
2998     RAISE FND_API.G_EXC_ERROR;
2999   ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
3000     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3001   END IF;
3002 
3003   --Before installing the new instance, make sure its operating unit is exactly the same as that
3004   --of the root instance.
3005   l_new_instance_ou := get_operating_unit(l_new_instance_id);
3006   IF l_root_instance_ou IS NULL THEN
3007     FND_MESSAGE.set_name('AHL', 'AHL_UC_INSTANCE_OU_NULL');
3008     FND_MESSAGE.set_token('INSTANCE', l_root_instance_id);
3009     FND_MSG_PUB.add;
3010     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3011   ELSIF l_new_instance_ou IS NULL THEN
3012     FND_MESSAGE.set_name('AHL', 'AHL_UC_INSTANCE_OU_NULL');
3013     FND_MESSAGE.set_token('INSTANCE', l_new_instance_id);
3014     FND_MSG_PUB.add;
3015     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3016   ELSIF l_root_instance_ou <> l_new_instance_ou THEN
3017     FND_MESSAGE.set_name('AHL', 'AHL_UC_INSTANCE_OU_UNMATCH');
3018     FND_MESSAGE.set_token('INSTANCE', l_new_instance_id);
3019     FND_MESSAGE.set_token('ROOT_INSTANCE', l_root_instance_id);
3020     FND_MSG_PUB.add;
3021     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3022   END IF;
3023 
3024   --Building csi_ii_relationship record should be after create_uc_header because create_uc_header
3025   --will validate the newly created instance, and this validation ensures that the instance is available
3026   --that is not in table csi_ii_realtionships and ahl_unit_config_headers
3027   --Build CSI relationships table
3028   l_csi_relationship_rec.relationship_type_code := 'COMPONENT-OF';
3029   l_csi_relationship_rec.object_id := p_parent_instance_id;
3030   l_csi_relationship_rec.position_reference := to_number(p_x_uc_instance_rec.relationship_id);
3031   l_csi_relationship_rec.subject_id := l_new_instance_id;
3032   l_csi_relationship_tbl(1) := l_csi_relationship_rec;
3033 
3034   CSI_II_RELATIONSHIPS_PUB.create_relationship(
3035                            p_api_version            => 1.0,
3036                            p_relationship_tbl       => l_csi_relationship_tbl,
3037                            p_txn_rec                => l_csi_transaction_rec,
3038                            x_return_status          => l_return_status,
3039                            x_msg_count              => l_msg_count,
3040                            x_msg_data               => l_msg_data);
3041   IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
3042     RAISE FND_API.G_EXC_ERROR;
3043   ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
3044     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3045   END IF;
3046 
3047   --Create the sub unit header record in ahl_unit_config_headers
3048   IF l_sub_mc_header_id IS NOT NULL THEN
3049     --Insert the newly added sub unit into UC headers table.
3050     p_x_sub_uc_rec.mc_header_id := l_sub_mc_header_id;
3051     p_x_sub_uc_rec.instance_id := l_new_instance_id;
3052     ahl_util_uc_pkg.get_parent_uc_header(l_new_instance_id,
3053                                          l_parent_uc_header_id,
3054                                          l_parent_instance_id);
3055     p_x_sub_uc_rec.parent_uc_header_id := l_parent_uc_header_id;
3056     --p_x_sub_uc_rec.parent_uc_header_id := p_uc_header_id;
3057     --The parameter p_uc_header_id is not necessarily the parent uc_header_id of the newly
3058     --installed instance.
3059 
3060     --dbms_output.put_line('Before calling create uc_header API:...');
3061     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3062       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
3063                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': within API',
3064                    ' p_x_sub_uc_rec.uc_header_id='||p_x_sub_uc_rec.uc_header_id||
3065                    ' p_x_sub_uc_rec.mc_header_id='||p_x_sub_uc_rec.mc_header_id||
3066                    ' p_x_sub_uc_rec.mc_name='||p_x_sub_uc_rec.mc_name||
3067                    ' p_x_sub_uc_rec.mc_revision='||p_x_sub_uc_rec.mc_revision||
3068                    ' p_x_sub_uc_rec.instance_id='||p_x_sub_uc_rec.instance_id||
3069                    ' p_x_sub_uc_rec.parent_uc_header_id='||p_x_sub_uc_rec.parent_uc_header_id);
3070     END IF;
3071     AHL_UC_UNITCONFIG_PVT.create_uc_header(
3072                             p_api_version        => 1.0,
3073                             p_init_msg_list      => FND_API.G_FALSE,
3074                             p_commit             => FND_API.G_FALSE,
3075                             p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
3076                             p_module_type        => NULL,
3077                             x_return_status      => l_return_status,
3078                             x_msg_count          => l_msg_count,
3079                             x_msg_data           => l_msg_data,
3080                             p_x_uc_header_rec    => p_x_sub_uc_rec);
3081 
3082     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3083       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
3084                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': within API',
3085                    ' p_x_sub_uc_rec.uc_header_id='||p_x_sub_uc_rec.uc_header_id||
3086                    ' p_x_sub_uc_rec.mc_header_id='||p_x_sub_uc_rec.mc_header_id||
3087                    ' p_x_sub_uc_rec.mc_name='||p_x_sub_uc_rec.mc_name||
3088                    ' p_x_sub_uc_rec.mc_revision='||p_x_sub_uc_rec.mc_revision||
3089                    ' p_x_sub_uc_rec.instance_id='||p_x_sub_uc_rec.instance_id||
3090                    ' p_x_sub_uc_rec.parent_uc_header_id='||p_x_sub_uc_rec.parent_uc_header_id);
3091     END IF;
3092     IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
3093       RAISE FND_API.G_EXC_ERROR;
3094     ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
3095       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3096     END IF;
3097     --dbms_output.put_line('After calling create uc_header API:...');
3098 
3099     --Copy the newly created UC header to history table
3100     ahl_util_uc_pkg.copy_uc_header_to_history(p_x_sub_uc_rec.uc_header_id, l_return_status);
3101 
3102     --IF history copy failed, then don't raise exception, just add the message to the message stack
3103     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
3104       FND_MESSAGE.set_name('AHL', 'AHL_UC_HISTORY_COPY_FAILED');
3105       FND_MSG_PUB.add;
3106     END IF;
3107 
3108   END IF;
3109 
3110   --Call completeness check API for the newly assigned instance
3111   ahl_uc_validation_pub.validate_complete_for_pos(
3112       p_api_version         => 1.0,
3113       p_init_msg_list       => FND_API.G_FALSE,
3114       p_commit              => FND_API.G_FALSE,
3115       p_validation_level    => FND_API.G_VALID_LEVEL_FULL,
3116       x_return_status       => l_return_status,
3117       x_msg_count           => l_msg_count,
3118       x_msg_data            => l_msg_data,
3119       p_csi_instance_id     => p_x_uc_instance_rec.instance_id,
3120       x_error_tbl           => x_warning_msg_tbl);
3121   IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
3122     RAISE FND_API.G_EXC_ERROR;
3123   ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
3124     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3125   END IF;
3126 
3127   --IF 1-WAY INTERCHANGEABLE item is installed, we will display the warning message to the user
3128   --This warning message is not added to the global message stack.
3129   -- SATHAPLI::ER 7419780, 24-Aug-2009, add the fetched INTERCHANGE_REASON as well, to the warning message AHL_UC_1WAY_ITEM_INSTALLED.
3130 
3131   IF p_x_sub_uc_rec.mc_header_id IS NOT NULL THEN
3132     OPEN get_interchange_type(p_x_uc_instance_rec.instance_id, l_top_relationship_id);
3133   ELSE
3134     OPEN get_interchange_type(p_x_uc_instance_rec.instance_id, p_x_uc_instance_rec.relationship_id);
3135   END IF;
3136   FETCH get_interchange_type INTO l_interchange_type_code, l_interchange_reason;
3137   IF get_interchange_type%NOTFOUND THEN
3138     FND_MESSAGE.set_name('AHL', 'AHL_UC_ITEM_INTERCHANGE_MISS');
3139     FND_MESSAGE.set_token('INSTANCE', p_x_uc_instance_rec.instance_id);
3140     FND_MSG_PUB.add;
3141     CLOSE get_interchange_type;
3142     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3143   ELSIF l_interchange_type_code = '1-WAY INTERCHANGEABLE' THEN
3144     FND_MESSAGE.set_name('AHL', 'AHL_UC_1WAY_ITEM_INSTALLED');
3145     SELECT f.meaning INTO l_position_ref_meaning
3146       FROM ahl_mc_relationships a,
3147            fnd_lookups f
3148      WHERE a.relationship_id = p_x_uc_instance_rec.relationship_id
3149        AND f.lookup_code (+) = A.position_ref_code
3150        AND f.lookup_type (+) = 'AHL_POSITION_REFERENCE' ;
3151     --Here always use p_x_uc_instance_rec.relationship_id instead of l_top_relationship_id because
3152     --UC tree UI always displays the parent leaf relationship_id instead of sub-uc top relationship_id
3153     --when it comes to the sub-uc.
3154     FND_MESSAGE.set_token('POSITION', l_position_ref_meaning);
3155     FND_MESSAGE.set_token('REASON', l_interchange_reason);
3156     --Here the message is not added to the global message stack;
3157     IF x_warning_msg_tbl.count > 0 THEN
3158       x_warning_msg_tbl(x_warning_msg_tbl.last + 1) := FND_MESSAGE.get;
3159     ELSE
3160       x_warning_msg_tbl(0) := FND_MESSAGE.get;
3161     END IF;
3162   END IF;
3163   CLOSE get_interchange_type;
3164 
3165   --For UC user, UC header status change needs to be made after the operation
3166   --Not confirmed whether need to copy the record into UC header history table
3167   --after status change. Not include the copy right now. (No history copy)
3168   IF p_prod_user_flag = 'N' THEN
3169     IF (l_root_uc_status_code = 'COMPLETE' AND
3170         x_warning_msg_tbl.count > 0 ) THEN
3171     --IF unit_config_status_code='INCOMPLETE' and active_uc_status_code='UNAPPROVED', this
3172     --update is only object_version_number change and not necessary.
3173       UPDATE ahl_unit_config_headers
3174          SET unit_config_status_code = 'INCOMPLETE',
3175              active_uc_status_code = 'UNAPPROVED',
3176              object_version_number = object_version_number + 1,
3177              last_update_date = SYSDATE,
3178              last_updated_by = FND_GLOBAL.user_id,
3179              last_update_login = FND_GLOBAL.login_id
3180        WHERE unit_config_header_id = l_root_uc_header_id
3181          AND object_version_number = l_root_uc_ovn;
3182       IF SQL%ROWCOUNT = 0 THEN
3183         FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
3184         FND_MSG_PUB.add;
3185         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3186       END IF;
3187     ELSIF (l_root_uc_status_code IN ('COMPLETE', 'INCOMPLETE') AND
3188            (l_root_active_uc_status_code IS NULL OR
3189             l_root_active_uc_status_code <> 'UNAPPROVED')) THEN
3190     --IF unit_config_status_code='INCOMPLETE' and active_uc_status_code='UNAPPROVED', this
3191     --update is only object_version_number change and not necessary.
3192       UPDATE ahl_unit_config_headers
3193          SET active_uc_status_code = 'UNAPPROVED',
3194              object_version_number = object_version_number + 1,
3195              last_update_date = SYSDATE,
3196              last_updated_by = FND_GLOBAL.user_id,
3197              last_update_login = FND_GLOBAL.login_id
3198        WHERE unit_config_header_id = l_root_uc_header_id
3199          AND object_version_number = l_root_uc_ovn;
3200       IF SQL%ROWCOUNT = 0 THEN
3201         FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
3202         FND_MSG_PUB.add;
3203         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3204       END IF;
3205     ELSIF (l_root_uc_status_code NOT IN ('COMPLETE', 'INCOMPLETE', 'DRAFT')) THEN
3206       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3207         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
3208                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Before calling completeness check',
3209                    'p_uc_header_id='||p_uc_header_id||'l_root_uc_header_id='||l_root_uc_header_id||
3210                    'l_root_uc_ovn='||l_root_uc_ovn);
3211       END IF;
3212     --IF unit_config_status_code='DRAFT', this update is only object_version_number change and
3213     --not necessary.
3214       UPDATE ahl_unit_config_headers
3215          SET unit_config_status_code = 'DRAFT',
3216              object_version_number = object_version_number + 1,
3217              last_update_date = SYSDATE,
3218              last_updated_by = FND_GLOBAL.user_id,
3219              last_update_login = FND_GLOBAL.login_id
3220        WHERE unit_config_header_id = l_root_uc_header_id
3221          AND object_version_number = l_root_uc_ovn;
3222       IF SQL%ROWCOUNT = 0 THEN
3223         FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
3224         FND_MSG_PUB.add;
3225         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3226       END IF;
3227     END IF;
3228   ELSIF (p_prod_user_flag = 'Y' AND
3229          x_warning_msg_tbl.count > 0 AND
3230          l_root_uc_status_code = 'COMPLETE') THEN
3231     UPDATE ahl_unit_config_headers
3232        SET unit_config_status_code = 'INCOMPLETE',
3233            object_version_number = object_version_number + 1,
3234            last_update_date = SYSDATE,
3235            last_updated_by = FND_GLOBAL.user_id,
3236            last_update_login = FND_GLOBAL.login_id
3237      WHERE unit_config_header_id = l_root_uc_header_id
3238        AND object_version_number = l_root_uc_ovn;
3239     IF SQL%ROWCOUNT = 0 THEN
3240       FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
3241       FND_MSG_PUB.add;
3242       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3243     END IF;
3244   END IF;
3245 
3246   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3247     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
3248                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': After normal execution',
3249                    'At the end of the procedure');
3250   END IF;
3251 
3252   --Get all the error messages from the previous steps (if any) and raise the appropriate Exception
3253   l_msg_count := FND_MSG_PUB.count_msg;
3254   IF l_msg_count > 0 THEN
3255     x_msg_count := l_msg_count;
3256     RAISE FND_API.G_EXC_ERROR;
3257   END IF;
3258   -- Perform the Commit (if requested)
3259   IF FND_API.to_boolean(p_commit) THEN
3260     COMMIT;
3261   END IF;
3262   --Count and Get messages(optional)
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 EXCEPTION
3268   WHEN FND_API.G_EXC_ERROR THEN
3269     ROLLBACK TO install_new_instance;
3270     x_return_status := FND_API.G_RET_STS_ERROR ;
3271     FND_MSG_PUB.count_and_get(
3272       p_encoded  => FND_API.G_FALSE,
3273       p_count    => x_msg_count,
3274       p_data     => x_msg_data);
3275   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3276     ROLLBACK TO install_new_instance;
3277     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3278     FND_MSG_PUB.count_and_get(
3279       p_encoded  => FND_API.G_FALSE,
3280       p_count    => x_msg_count,
3281       p_data     => x_msg_data);
3282   WHEN OTHERS THEN
3283     ROLLBACK TO install_new_instance;
3284     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
3285     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
3286     THEN
3287       FND_MSG_PUB.add_exc_msg(
3288         p_pkg_name         => G_PKG_NAME,
3289         p_procedure_name   => l_api_name,
3290         p_error_text       => SUBSTRB(SQLERRM,1,240));
3291     END IF;
3292     FND_MSG_PUB.count_and_get(
3293       p_encoded  => FND_API.G_FALSE,
3294       p_count    => x_msg_count,
3295       p_data     => x_msg_data);
3296 END;
3297 
3298 -- Define procedure install_existing_instance
3299 -- This API is used to assign an existing instance to a UC node.
3300 PROCEDURE install_existing_instance(
3301   p_api_version           IN  NUMBER := 1.0,
3302   p_init_msg_list         IN  VARCHAR2 := FND_API.G_FALSE,
3303   p_commit                IN  VARCHAR2 := FND_API.G_FALSE,
3304   p_validation_level      IN  NUMBER := FND_API.G_VALID_LEVEL_FULL,
3305   x_return_status         OUT NOCOPY VARCHAR2,
3306   x_msg_count             OUT NOCOPY NUMBER,
3307   x_msg_data              OUT NOCOPY VARCHAR2,
3308   p_uc_header_id          IN  NUMBER,
3309   p_parent_instance_id    IN  NUMBER,
3310   p_instance_id           IN  NUMBER,
3311   p_instance_number       IN  csi_item_instances.instance_number%TYPE := NULL,
3312   p_relationship_id       IN  NUMBER,
3313   p_csi_ii_ovn            IN  NUMBER,
3314   p_prod_user_flag        IN  VARCHAR2,
3315   -- SURRKUMA :: 13694898 :: Flag to bypass the validation of position rules, 29-FEB-2012
3316   p_validate_rule_flag    IN  VARCHAR2  := FND_API.G_TRUE,
3317   x_warning_msg_tbl       OUT NOCOPY ahl_uc_validation_pub.error_tbl_type)
3318 IS
3319   l_api_name       CONSTANT   VARCHAR2(30) := 'install_existing_instance';
3320   l_api_version    CONSTANT   NUMBER       := 1.0;
3321   l_return_status             VARCHAR2(1);
3322   l_msg_count                 NUMBER;
3323   l_msg_data                  VARCHAR2(2000);
3324   l_subject_id                NUMBER;
3325   l_object_id                 NUMBER;
3326   l_csi_relationship_id       NUMBER;
3327   l_object_version_number     NUMBER;
3328   l_position_reference        csi_ii_relationships.position_reference%TYPE;
3329   l_mc_header_id              NUMBER;
3330   l_sub_uc_header_id          NUMBER;
3331   l_parent_relationship_id    NUMBER;
3332   l_instance_type             VARCHAR2(1);
3333   l_dummy                     NUMBER;
3334   l_dummy_char                VARCHAR2(1);
3335   l_subunit                   BOOLEAN;
3336   i                           NUMBER := 0;
3337   l_subscript                 NUMBER DEFAULT 0;
3338   l_concatenated_segments     mtl_system_items_kfv.concatenated_segments%TYPE;
3339   l_item_assoc_id             NUMBER;
3340   l_meaning                   fnd_lookups.meaning%TYPE;
3341   l_parent_uc_header_id       NUMBER;
3342   l_parent_instance_id        NUMBER;
3343   l_root_uc_header_id         NUMBER;
3344   l_root_instance_id          NUMBER;
3345   l_root_uc_status_code       FND_LOOKUP_VALUES.lookup_code%TYPE;
3346   l_root_active_uc_status_code FND_LOOKUP_VALUES.lookup_code%TYPE;
3347   l_root_uc_ovn               NUMBER;
3348   l_root_instance_ou          NUMBER;
3349   l_instance_ou               NUMBER;
3350   l_uc_status_code            FND_LOOKUP_VALUES.lookup_code%TYPE;
3351   l_active_uc_status_code     FND_LOOKUP_VALUES.lookup_code%TYPE;
3352   l_position_ref_meaning      fnd_lookups.meaning%TYPE;
3353   l_interchange_type_code     ahl_item_associations_b.interchange_type_code%TYPE;
3354   l_interchange_reason        ahl_item_associations_tl.interchange_reason%TYPE;
3355 
3356   --Variables needed for CSI API call
3357   l_csi_party_rec             csi_datastructures_pub.party_rec;
3358   l_csi_relationship_rec      csi_datastructures_pub.ii_relationship_rec;
3359   l_csi_relationship_new_rec  csi_datastructures_pub.ii_relationship_rec; -- SATHAPLI::FP ER 6504147, 18-Nov-2008
3360   l_csi_relationship_tbl      csi_datastructures_pub.ii_relationship_tbl;
3361   l_csi_party_tbl             csi_datastructures_pub.party_tbl;
3362   l_csi_account_tbl           csi_datastructures_pub.party_account_tbl;
3363   l_csi_pricing_attrib_tbl    csi_datastructures_pub.pricing_attribs_tbl;
3364   l_csi_org_assignments_tbl   csi_datastructures_pub.organization_units_tbl;
3365   l_csi_asset_assignment_tbl  csi_datastructures_pub.instance_asset_tbl;
3366   l_csi_instance_id_lst       csi_datastructures_pub.id_tbl;
3367   l_party_account_rec         csi_datastructures_pub.party_account_rec;
3368   l_serial_number             csi_item_instances.serial_number%TYPE;
3369   l_mfg_serial_number_flag    csi_item_instances.mfg_serial_number_flag%TYPE;
3370   l_serial_number_tag         csi_iea_values.attribute_value%TYPE;
3371 
3372   l_return_val                BOOLEAN;
3373   l_transaction_type_id       NUMBER;
3374   l_attribute_id              NUMBER;
3375   l_attribute_value_id        NUMBER;
3376   l_attribute_value           csi_iea_values.attribute_value%TYPE;
3377   l_csi_instance_rec          csi_datastructures_pub.instance_rec;
3378   l_csi_transaction_rec       csi_datastructures_pub.transaction_rec;
3379   l_csi_extend_attrib_rec     csi_datastructures_pub.extend_attrib_values_rec;
3380   l_csi_ext_attrib_values_tbl csi_datastructures_pub.extend_attrib_values_tbl;
3381 
3382   CURSOR check_uc_header IS
3383     SELECT A.unit_config_header_id,
3384            A.object_version_number,
3385            A.unit_config_status_code,
3386            A.active_uc_status_code,
3387            A.csi_item_instance_id,
3388            B.relationship_id
3389       FROM ahl_unit_config_headers A,
3390            ahl_mc_relationships B
3391      WHERE A.unit_config_header_id = p_uc_header_id
3392        AND trunc(nvl(A.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
3393        AND A.master_config_id = B.mc_header_id
3394        AND B.parent_relationship_id IS NULL;
3395   l_check_uc_header check_uc_header%ROWTYPE;
3396   CURSOR get_uc_descendants(c_instance_id NUMBER) IS
3397     SELECT relationship_id,
3398            object_version_number,
3399            object_id,
3400            subject_id,
3401            to_number(position_reference) position_id
3402       FROM csi_ii_relationships
3403 START WITH object_id = c_instance_id
3404        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3405        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
3406 CONNECT BY object_id = PRIOR subject_id
3407        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3408        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
3409   --Cursor to check whether the c_parent_relationship_id is the parent of
3410   --c_child_relationshp_id or c_child_relationship_id's own parent as the top node of the sub-config
3411   --can be installed in c_parent_relationship_id
3412   CURSOR check_parent_relationship(c_child_relationship_id NUMBER, c_parent_relationship_id NUMBER) IS
3413     SELECT 1
3414       FROM ahl_mc_relationships
3415      WHERE relationship_id = c_child_relationship_id
3416        AND (parent_relationship_id = c_parent_relationship_id OR
3417             mc_header_id IN (SELECT mc_header_id
3418                                FROM ahl_mc_config_relations
3419                               WHERE relationship_id = c_parent_relationship_id
3420                                 AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3421                                 AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)));
3422 
3423   --Cursor to check whether c_parent_instance_id's child position c_relationship_id is empty
3424   CURSOR check_position_empty(c_parent_instance_id NUMBER, c_relationship_id NUMBER) IS
3425     SELECT subject_id
3426       FROM csi_ii_relationships
3427      WHERE object_id = c_parent_instance_id
3428        AND position_reference = to_char(c_relationship_id)
3429        AND subject_id IS NOT NULL
3430        AND relationship_type_code = 'COMPONENT-OF'
3431        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3432        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
3433 
3434   CURSOR check_instance_leaf(c_instance_id NUMBER) IS
3435     SELECT subject_id
3436       FROM csi_ii_relationships
3437      WHERE object_id = c_instance_id
3438        AND relationship_type_code = 'COMPONENT-OF'
3439        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3440        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
3441 
3442   CURSOR get_uc_header(c_instance_id NUMBER) IS
3443     SELECT unit_config_header_id, master_config_id
3444       FROM ahl_unit_config_headers
3445      WHERE csi_item_instance_id = c_instance_id
3446        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
3447   --To get all the first level sub-units for a given branch node. First get all of the
3448   --branch node's sub-units and then remove those sub-units which are not first level
3449   --(from the branch node's perspective)
3450 
3451   CURSOR get_1st_level_subunits(c_instance_id NUMBER) IS
3452   /*This query is replaced by the query below it for performance gain.
3453     SELECT subject_id
3454       FROM csi_ii_relationships
3455      WHERE subject_id IN (SELECT csi_item_instance_id
3456                             FROM ahl_unit_config_headers
3457                            WHERE trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE))
3458 START WITH object_id = c_instance_id
3459        AND relationship_type_code = 'COMPONENT-OF'
3460        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3461        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
3462 CONNECT BY object_id = PRIOR subject_id
3463        AND relationship_type_code = 'COMPONENT-OF'
3464        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3465        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
3466      MINUS
3467     SELECT subject_id
3468       FROM csi_ii_relationships
3469      WHERE subject_id IN (SELECT csi_item_instance_id
3470                             FROM ahl_unit_config_headers
3471                            WHERE trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE))
3472 START WITH object_id IN (SELECT subject_id
3473                            FROM csi_ii_relationships
3474                           WHERE subject_id IN (SELECT csi_item_instance_id
3475                                                  FROM ahl_unit_config_headers
3476                                                 WHERE trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE))
3477                      START WITH object_id = c_instance_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                      CONNECT BY object_id = PRIOR subject_id
3482                             AND relationship_type_code = 'COMPONENT-OF'
3483                             AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3484                             AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE))
3485        AND relationship_type_code = 'COMPONENT-OF'
3486        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3487        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
3488 CONNECT BY object_id = PRIOR subject_id
3489        AND relationship_type_code = 'COMPONENT-OF'
3490        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3491        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
3492   */
3493    SELECT i.subject_id
3494     FROM csi_ii_relationships i
3495    WHERE EXISTS (SELECT 'x'
3496                   FROM ahl_unit_config_headers u
3497                  WHERE u.csi_item_instance_id = i.subject_id
3498                    AND trunc(nvl(u.active_end_date, SYSDATE+1)) > trunc(SYSDATE))
3499      AND NOT EXISTS (SELECT ci.object_id
3500                        FROM csi_ii_relationships ci
3501                       WHERE (EXISTS (SELECT 'x'
3502                                        FROM ahl_unit_config_headers ui
3503                                       WHERE ui.csi_item_instance_id = ci.object_id)
3504                                 AND ci.object_id <> c_instance_id)
3505                  START WITH ci.subject_id = i.subject_id
3506                         AND ci.relationship_type_code = 'COMPONENT-OF'
3507                         AND trunc(nvl(ci.active_start_date, SYSDATE)) <= trunc(SYSDATE)
3508                         AND trunc(nvl(ci.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
3509                  CONNECT BY ci.subject_id = prior ci.object_id
3510                         AND ci.relationship_type_code = 'COMPONENT-OF'
3511                         AND trunc(nvl(ci.active_start_date, SYSDATE)) <= trunc(SYSDATE)
3512                         AND trunc(nvl(ci.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
3513                         AND ci.subject_id <> c_instance_id)
3514 START WITH i.object_id = c_instance_id
3515        AND i.relationship_type_code = 'COMPONENT-OF'
3516        AND trunc(nvl(i.active_start_date, SYSDATE)) <= trunc(SYSDATE)
3517        AND trunc(nvl(i.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
3518 CONNECT BY i.object_id = PRIOR i.subject_id
3519        AND i.relationship_type_code = 'COMPONENT-OF'
3520        AND trunc(nvl(i.active_start_date, SYSDATE)) <= trunc(SYSDATE)
3521        AND trunc(nvl(i.active_end_date, SYSDATE+1)) > trunc(SYSDATE);
3522 
3523   CURSOR csi_item_instance_csr(c_instance_id  IN  NUMBER) IS
3524     SELECT C.inventory_item_id,
3525            C.inv_master_organization_id inventory_org_id,
3526            C.quantity,
3527            C.unit_of_measure uom_code,
3528            C.inventory_revision revision,
3529            C.install_date,
3530            C.instance_usage_code,
3531            C.location_type_code,
3532            C.object_version_number,
3533            U.unit_config_header_id uc_header_id
3534       FROM csi_item_instances C,
3535            ahl_unit_config_headers U
3536      WHERE C.instance_id = c_instance_id
3537        AND C.instance_id = U.csi_item_instance_id (+)
3538        --AND U.parent_uc_header_id (+) IS NULL
3539        --Comment out in order to include the extra sibling subunits whose parent_uc_header_id
3540        --is not null
3541        AND trunc(SYSDATE) < trunc(nvl(C.active_end_date,SYSDATE+1))
3542        AND trunc(SYSDATE) < trunc(nvl(U.active_end_date (+),SYSDATE+1));
3543   l_instance_rec        csi_item_instance_csr%ROWTYPE;
3544 
3545   CURSOR check_sub_mc(c_relationship_id NUMBER) IS
3546     SELECT mc_header_id
3547       FROM ahl_mc_config_relations
3548      WHERE relationship_id = c_relationship_id
3549        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3550        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
3551 
3552   CURSOR check_extra_node(c_object_id NUMBER, c_subject_id NUMBER) IS
3553     SELECT relationship_id, object_version_number
3554       FROM csi_ii_relationships
3555      WHERE object_id = c_object_id
3556        AND subject_id = c_subject_id
3557        AND position_reference IS NULL
3558        AND relationship_type_code = 'COMPONENT-OF'
3559        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3560        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
3561 
3562   -- SATHAPLI::FP ER 6504147, 18-Nov-2008
3563   CURSOR check_unasgnd_extra_node_csr(p_parent_instance_id NUMBER, p_instance_id NUMBER) IS
3564     SELECT relationship_id, object_version_number
3565       FROM csi_ii_relationships
3566      WHERE object_id IN (
3567                          SELECT ii.object_id
3568                          FROM   csi_ii_relationships ii
3569                          START WITH ii.subject_id = p_parent_instance_id
3570                          AND    ii.relationship_type_code = 'COMPONENT-OF'
3571                          AND    trunc(nvl(ii.active_start_date, SYSDATE)) <= trunc(SYSDATE)
3572                          AND    trunc(nvl(ii.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
3573                          CONNECT BY ii.subject_id = PRIOR ii.object_id
3574                          AND    ii.relationship_type_code = 'COMPONENT-OF'
3575                          AND    trunc(nvl(ii.active_start_date, SYSDATE)) <= trunc(SYSDATE)
3576                          AND    trunc(nvl(ii.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
3577                         )
3578        AND subject_id = p_instance_id
3579        AND position_reference IS NULL
3580        AND relationship_type_code = 'COMPONENT-OF'
3581        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3582        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
3583 
3584   CURSOR get_serial_number(c_instance_id NUMBER) IS
3585     SELECT serial_number, mfg_serial_number_flag
3586       FROM csi_item_instances
3587      WHERE instance_id = c_instance_id
3588        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3589        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
3590 
3591   CURSOR check_instance_installed(c_instance_id NUMBER) IS
3592     SELECT 'X'
3593       FROM csi_ii_relationships
3594      WHERE subject_id = c_instance_id
3595        AND position_reference IS NOT NULL
3596        --for extra node, it is still available for its sibling nodes even
3597        --if it is installed and not removed
3598        AND relationship_type_code = 'COMPONENT-OF'
3599        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
3600        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
3601 
3602   -- SATHAPLI::ER 7419780, 24-Aug-2009, fetch the INTERCHANGE_REASON from the table ahl_item_associations_tl too
3603   CURSOR get_interchange_type (c_instance_id NUMBER, c_relationship_id NUMBER) IS
3604     SELECT i.interchange_type_code,
3605            itl.interchange_reason
3606       FROM csi_item_instances c,
3607            ahl_item_associations_b i,
3608            ahl_item_associations_tl itl,
3609            ahl_mc_relationships m
3610      WHERE m.relationship_id = c_relationship_id
3611        AND c.instance_id = c_instance_id
3612        AND m.item_group_id = i.item_group_id
3613        AND c.inventory_item_id = i.inventory_item_id
3614        AND c.inv_master_organization_id = i.inventory_org_id
3615        AND itl.item_association_id = i.item_association_id
3616        AND itl.language = USERENV('LANG')
3617        AND (c.inventory_revision IS NULL OR
3618             i.revision is NULL OR
3619             (c.inventory_revision IS NOT NULL AND
3620              i.revision IS NOT NULL AND
3621              c.inventory_revision = i.revision));
3622    --Added this last condition due to the impact of bug fixing 4102152, added by Jerry on 01/05/2005
3623    --Need to confirm which one is more accurate here to use c.inv_master_organization_id or
3624    --c.last_vld_organization_id
3625 
3626 BEGIN
3627   --Initialize API return status to success
3628   x_return_status := FND_API.G_RET_STS_SUCCESS;
3629 
3630   -- Standard Start of API savepoint
3631   SAVEPOINT install_existing_instance;
3632 
3633   --Standard call to check for call compatibility.
3634   IF NOT FND_API.compatible_api_call(
3635     l_api_version,
3636     p_api_version,
3637     l_api_name,  G_PKG_NAME)
3638   THEN
3639     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3640   END IF;
3641 
3642   --Initialize message list if p_init_msg_list is set to TRUE.
3643   IF FND_API.to_boolean( p_init_msg_list ) THEN
3644     FND_MSG_PUB.initialize;
3645   END IF;
3646 
3647   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3648     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
3649                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Begin API',
3650                    'At the start of the procedure');
3651   END IF;
3652 
3653   --Validate input parameters p_prod_user_flag
3654   IF upper(p_prod_user_flag) <> 'Y' AND upper(p_prod_user_flag) <> 'N' THEN
3655     FND_MESSAGE.set_name('AHL','AHL_UC_API_PARAMETER_INVALID');
3656     FND_MESSAGE.set_token('NAME', 'prod_user_flag');
3657     FND_MESSAGE.set_token('VALUE', p_prod_user_flag);
3658     FND_MSG_PUB.add;
3659     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3660   END IF;
3661 
3662   --Validate input parameter p_uc_header_id, its two statuses
3663   OPEN check_uc_header;
3664   FETCH check_uc_header INTO l_check_uc_header;
3665   IF check_uc_header%NOTFOUND THEN
3666     FND_MESSAGE.set_name('AHL','AHL_UC_API_PARAMETER_INVALID');
3667     FND_MESSAGE.set_token('NAME', 'uc_header_id');
3668     FND_MESSAGE.set_token('VALUE', p_uc_header_id);
3669     FND_MSG_PUB.add;
3670     CLOSE check_uc_header;
3671     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3672   ELSE
3673 
3674     -- ACL :: Changes for R12
3675     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
3676       FND_MESSAGE.set_name( 'AHL','AHL_UC_INVALID_Q_ACTION' );
3677       FND_MSG_PUB.add;
3678       CLOSE check_uc_header;
3679       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3680     END IF;
3681 
3682     ahl_util_uc_pkg.get_root_uc_attr(p_uc_header_id,
3683                                      l_root_uc_header_id,
3684                                      l_root_instance_id,
3685                                      l_root_uc_status_code,
3686                                      l_root_active_uc_status_code,
3687                                      l_root_uc_ovn);
3688     IF (p_prod_user_flag = 'Y' AND --For production user, no need to confirm either one of the statuses is not APPROVAL_PENDING
3689         l_root_uc_status_code NOT IN ('COMPLETE', 'INCOMPLETE')) THEN
3690       FND_MESSAGE.set_name( 'AHL','AHL_UC_STATUS_NOT_ACTIVE' );
3691       FND_MSG_PUB.add;
3692       CLOSE check_uc_header;
3693       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3694     ELSIF (p_prod_user_flag = 'N' AND
3695            (l_root_uc_status_code = 'APPROVAL_PENDING' OR
3696             l_root_active_uc_status_code = 'APPROVAL_PENDING')) THEN
3697       FND_MESSAGE.set_name( 'AHL','AHL_UC_STATUS_PENDING' );
3698       FND_MESSAGE.set_token('UC_HEADER_ID', l_root_uc_header_id);
3699       FND_MSG_PUB.add;
3700       CLOSE check_uc_header;
3701       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3702     ELSE
3703       CLOSE check_uc_header;
3704     END IF;
3705   END IF;
3706 
3707   --Get the operating unit of the root instance.
3708   l_root_instance_ou := get_operating_unit(l_root_instance_id);
3709 
3710   --Make sure p_parent_instance_id is installed in the UC
3711   IF p_parent_instance_id = l_check_uc_header.csi_item_instance_id THEN
3712     --The parent instance is the root node
3713     l_parent_relationship_id := l_check_uc_header.relationship_id;
3714   ELSE
3715     FOR l_get_uc_descendant IN get_uc_descendants(l_check_uc_header.csi_item_instance_id) LOOP
3716       l_csi_relationship_id := l_get_uc_descendant.relationship_id;
3717       l_object_version_number := l_get_uc_descendant.object_version_number;
3718       l_object_id := l_get_uc_descendant.object_id;
3719       l_subject_id := l_get_uc_descendant.subject_id;
3720       l_parent_relationship_id := l_get_uc_descendant.position_id;
3721       EXIT WHEN l_subject_id = p_parent_instance_id;
3722     END LOOP;
3723     --Ensure the instance is installed in this UC and not an extra node
3724     IF (l_subject_id <> p_parent_instance_id OR
3725         p_parent_instance_id IS NULL OR
3726         l_subject_id IS NULL OR
3727         l_parent_relationship_id IS NULL) THEN
3728       --We don't allow installing child instance to an extra node.
3729       FND_MESSAGE.set_name( 'AHL','AHL_UC_INSTANCE_NOT_IN_UC' );
3730       FND_MESSAGE.set_token('INSTANCE', p_parent_instance_id);
3731       FND_MSG_PUB.add;
3732       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3733     END IF;
3734   END IF;
3735   --Then validate p_relationship_id can be child of l_parent_relationship_id
3736   OPEN check_parent_relationship(p_relationship_id, l_parent_relationship_id);
3737   FETCH check_parent_relationship INTO l_dummy;
3738   IF check_parent_relationship%NOTFOUND THEN
3739     FND_MESSAGE.set_name( 'AHL','AHL_UC_POSITION_MISMATCH' );
3740     FND_MESSAGE.set_token('CHILD', p_relationship_id);
3741     FND_MESSAGE.set_token('PARENT', l_parent_relationship_id);
3742     FND_MSG_PUB.add;
3743     CLOSE check_parent_relationship;
3744     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3745   ELSE
3746     CLOSE check_parent_relationship;
3747   END IF;
3748   --Make sure position p_relationship_id is empty
3749   OPEN check_position_empty(p_parent_instance_id, p_relationship_id);
3750   FETCH check_position_empty INTO l_dummy;
3751   IF check_position_empty%FOUND THEN
3752     FND_MESSAGE.set_name( 'AHL','AHL_UC_POSITION_INSTALLED' );
3753     FND_MESSAGE.set_token('POSITION', p_relationship_id);
3754     FND_MSG_PUB.add;
3755     CLOSE check_position_empty;
3756     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3757   ELSE
3758     CLOSE check_position_empty;
3759   END IF;
3760 
3761 
3762   --Validate the instance to be installed is existing
3763   OPEN csi_item_instance_csr(p_instance_id);
3764   FETCH csi_item_instance_csr INTO l_instance_rec;
3765   IF (csi_item_instance_csr%NOTFOUND) THEN
3766     CLOSE csi_item_instance_csr;
3767     FND_MESSAGE.set_name('AHL','AHL_UC_CSII_INVALID');
3768     FND_MESSAGE.set_token('CSII',p_instance_id);
3769     FND_MESSAGE.set_token('POSN_REF',p_relationship_id);
3770     FND_MSG_PUB.add;
3771     --dbms_output.put_line('CSI item instance ID does not exist.');
3772     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3773   END IF;
3774   CLOSE csi_item_instance_csr;
3775 
3776   --Ensure the instance is available, not installed. For extra node, even if it
3777   --is installed but it is still available for its sibling nodes.
3778   OPEN check_instance_installed(p_instance_id);
3779   FETCH check_instance_installed INTO l_dummy_char;
3780   IF (check_instance_installed%FOUND) THEN
3781     CLOSE check_instance_installed;
3782     FND_MESSAGE.set_name('AHL','AHL_UC_INSTANCE_INSTALLED');
3783     FND_MESSAGE.set_token('INSTANCE',p_instance_id);
3784     FND_MSG_PUB.add;
3785     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3786   END IF;
3787   CLOSE check_instance_installed;
3788 
3789 
3790   --Check object_version_number of the instance
3791 /*  IF (p_uc_instance_rec.object_version_number <> l_csi_inst_rec.object_version
3792 _number) THEN
3793     CLOSE csi_item_instance_csr;
3794     FND_MESSAGE.set_name('AHL','AHL_COM_RECORD_CHANGED');
3795     FND_MSG_PUB.add;
3796     --dbms_output.put_line('Item Instance id object version changed');
3797     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3798   END IF;
3799 */
3800   --Validate the status of the instance
3801   IF (l_instance_rec.location_type_code IN ('PO','IN-TRANSIT','PROJECT','INVENTORY')) THEN
3802     FND_MESSAGE.set_name('AHL','AHL_UC_INST_STATUS_INVALID');
3803     AHL_UTIL_UC_PKG.convert_to_csimeaning('CSI_INST_LOCATION_SOURCE_CODE',
3804                                           l_instance_rec.location_type_code,
3805                                           l_meaning,l_return_val);
3806     IF NOT(l_return_val) THEN
3807       l_meaning := l_instance_rec.location_type_code;
3808     END IF;
3809     FND_MESSAGE.set_token('LOCATION',l_meaning);
3810     FND_MSG_PUB.add;
3811     --dbms_output.put_line('Item Instance location is not valid');
3812   END IF;
3813 
3814   --If the instance is not a unit, then validate positional attributes. For unit, it is not
3815   --necessary to validate.
3816   IF (l_instance_rec.uc_header_id IS NULL) THEN
3817     AHL_UTIL_UC_PKG.validate_for_position(p_relationship_id,
3818                                           l_instance_rec.inventory_item_id,
3819                                           l_instance_rec.inventory_org_id,
3820                                           l_instance_rec.quantity,
3821                                           l_instance_rec.revision,
3822                                           l_instance_rec.uom_code,
3823                                           NULL,
3824                                           -- SATHAPLI::FP OGMA Issue# 105 - Non-Serialized Item Maintenance, 05-Dec-2007
3825                                           -- Pass 'N' for p_ignore_quant_vald.
3826                                           'N',
3827                                           l_item_assoc_id);
3828   END IF;
3829 
3830   --Validate installation date.
3831   --Keep the installation date validation only for production user(04/21/2004).
3832   IF (l_instance_rec.install_date IS NOT NULL AND
3833       l_instance_rec.install_date <> FND_API.G_MISS_DATE) THEN
3834     IF (p_prod_user_flag = 'Y' AND trunc(l_instance_rec.install_date) > trunc(SYSDATE)) THEN
3835       FND_MESSAGE.set_name('AHL','AHL_UC_INSTDATE_INVALID');
3836       FND_MESSAGE.set_token('DATE',l_instance_rec.install_date);
3837       FND_MESSAGE.set_token('POSN_REF',p_relationship_id);
3838       FND_MSG_PUB.add;
3839       --dbms_output.put_line('Installation date invalid.');
3840     END IF;
3841   END IF;
3842 
3843   --Check Error Message stack.
3844   l_msg_count := FND_MSG_PUB.count_msg;
3845   IF l_msg_count > 0 THEN
3846     RAISE  FND_API.G_EXC_ERROR;
3847   END IF;
3848 
3849   --Before installing the existing instance, make sure its operating unit is exactly the same as that
3850   --of the root instance.
3851   l_instance_ou := get_operating_unit(p_instance_id);
3852   IF l_root_instance_ou IS NULL THEN
3853     FND_MESSAGE.set_name('AHL', 'AHL_UC_INSTANCE_OU_NULL');
3854     FND_MESSAGE.set_token('INSTANCE', l_root_instance_id);
3855     FND_MSG_PUB.add;
3856     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3857   ELSIF l_instance_ou IS NULL THEN
3858     FND_MESSAGE.set_name('AHL', 'AHL_UC_INSTANCE_OU_NULL');
3859     FND_MESSAGE.set_token('INSTANCE', p_instance_id);
3860     FND_MSG_PUB.add;
3861     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3862   ELSIF l_root_instance_ou <> l_instance_ou THEN
3863     FND_MESSAGE.set_name('AHL', 'AHL_UC_INSTANCE_OU_UNMATCH');
3864     FND_MESSAGE.set_token('INSTANCE', p_instance_id);
3865     FND_MESSAGE.set_token('ROOT_INSTANCE', l_root_instance_id);
3866     FND_MSG_PUB.add;
3867     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3868   END IF;
3869 
3870   --Check the instance to be installed is a leaf node, branch node or sub-unit top node(in this
3871   --case, it might also be a leaf node in csi_ii_relationships if all of its descendants are empty)
3872   OPEN get_uc_header(p_instance_id);
3873   FETCH get_uc_header INTO l_sub_uc_header_id, l_mc_header_id;
3874   IF get_uc_header%FOUND THEN
3875     -- ACL :: R12 Changes
3876     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
3877       FND_MESSAGE.set_name( 'AHL','AHL_UC_INVALID_Q_ACTION' );
3878       FND_MSG_PUB.add;
3879       CLOSE check_uc_header;
3880       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3881     END IF;
3882     --The instance is a unit top node, needs to see whether it can be a candidate sub-unit
3883     --in that position
3884     l_instance_type := 'S';
3885     l_subunit := FALSE;
3886     FOR l_check_sub_mc IN check_sub_mc(p_relationship_id) LOOP
3887       IF l_mc_header_id = l_check_sub_mc.mc_header_id THEN
3888         l_subunit := TRUE;
3889         EXIT;
3890       END IF;
3891     END LOOP;
3892     IF NOT l_subunit THEN
3893       FND_MESSAGE.set_name('AHL','AHL_UC_SUBUNIT_MISMATCH');
3894       FND_MESSAGE.set_token('INSTANCE', p_instance_id);
3895       FND_MESSAGE.set_token('POSITION', p_relationship_id);
3896       FND_MSG_PUB.add;
3897       CLOSE get_uc_header;
3898       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3899     END IF;
3900   ELSE
3901     OPEN check_instance_leaf(p_instance_id);
3902     FETCH check_instance_leaf INTO l_dummy;
3903     IF check_instance_leaf%FOUND THEN --Non leaf instance
3904       --The instance is a branch node, needs to call remap_uc_subtree to see whether the branch
3905       --can be installed in that position. If match, the corresponding position reference will be
3906       --updated as well.
3907       l_instance_type := 'B';
3908       ahl_uc_tree_pvt.remap_uc_subtree(
3909                           p_api_version      => 1.0,
3910                           p_init_msg_list    => FND_API.G_FALSE,
3911                           p_commit           => FND_API.G_FALSE,
3912                           p_validation_level => FND_API.G_VALID_LEVEL_FULL,
3913                           x_return_status    => l_return_status,
3914                           x_msg_count        => l_msg_count,
3915                           x_msg_data         => l_msg_data,
3916                           p_instance_id      => p_instance_id,
3917                           p_relationship_id  => p_relationship_id);
3918       IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
3919         CLOSE check_instance_leaf;
3920         RAISE FND_API.G_EXC_ERROR;
3921       ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
3922         CLOSE check_instance_leaf;
3923         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3924       END IF;
3925     ELSE
3926       l_instance_type := 'L';
3927     END IF;
3928     CLOSE check_instance_leaf;
3929   END IF;
3930   CLOSE get_uc_header;
3931 
3932   --Build CSI transaction record, first get transaction_type_id
3933   AHL_Util_UC_Pkg.getcsi_transaction_id('UC_UPDATE',l_transaction_type_id, l_return_val);
3934   IF NOT(l_return_val) THEN
3935     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3936   END IF;
3937 
3938   l_csi_transaction_rec.source_transaction_date := SYSDATE;
3939   l_csi_transaction_rec.transaction_type_id := l_transaction_type_id;
3940 
3941   --Update installation date if provided.
3942   IF (l_instance_rec.install_date IS NOT NULL AND
3943       l_instance_rec.install_date <> FND_API.G_MISS_DATE) THEN
3944     -- Build CSI instance rec.
3945     l_csi_instance_rec.instance_id           := p_instance_id;
3946     l_csi_instance_rec.object_version_number := l_instance_rec.object_version_number;
3947     l_csi_instance_rec.install_date          := l_instance_rec.install_date;
3948 
3949     -- Call API to update installation date.
3950     CSI_ITEM_INSTANCE_PUB.update_item_instance(
3951                           p_api_version            => 1.0,
3952                           p_instance_rec           => l_csi_instance_rec,
3953                           p_txn_rec                => l_csi_transaction_rec,
3954                           p_ext_attrib_values_tbl  => l_csi_ext_attrib_values_tbl,
3955                           p_party_tbl              => l_csi_party_tbl,
3956                           p_account_tbl            => l_csi_account_tbl,
3957                           p_pricing_attrib_tbl     => l_csi_pricing_attrib_tbl,
3958                           p_org_assignments_tbl    => l_csi_org_assignments_tbl,
3959                           p_asset_assignment_tbl   => l_csi_asset_assignment_tbl,
3960                           x_instance_id_lst        => l_csi_instance_id_lst,
3961                           x_return_status          => l_return_status,
3962                           x_msg_count              => l_msg_count,
3963                           x_msg_data               => l_msg_data);
3964     IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
3965       RAISE FND_API.G_EXC_ERROR;
3966     ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
3967       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3968     END IF;
3969   END IF;
3970 
3971   --Need to check if the instance picked from CSI with serial_number has a serial_no_tag, and
3972   --if not, we have to derive its value according to mfg_serail_number_flag ('Y'->'INVENTORY',
3973   --assuming CSI has the validation to ensure the serial_number exisiting in table
3974   --mfg_searil_numbers, otherwise it is 'TEMPORARY'
3975   OPEN get_serial_number(p_instance_id);
3976   FETCH get_serial_number INTO l_serial_number, l_mfg_serial_number_flag;
3977   IF get_serial_number%NOTFOUND THEN
3978     FND_MESSAGE.set_name('AHL', 'AHL_UC_CSII_INVALID');
3979     FND_MESSAGE.set_token('CSII', p_instance_id);
3980     FND_MSG_PUB.add;
3981     RAISE FND_API.G_EXC_ERROR;
3982     CLOSE get_serial_number;
3983   ELSE
3984     CLOSE get_serial_number;
3985   END IF;
3986 
3987   IF l_serial_number IS NOT NULL THEN
3988     --Retrieve existing value of serial_number_tag if present.
3989     AHL_UTIL_UC_PKG.getcsi_attribute_value(p_instance_id,
3990                                            'AHL_TEMP_SERIAL_NUM',
3991                                            l_attribute_value,
3992                                            l_attribute_value_id,
3993                                            l_object_version_number,
3994                                            l_return_val);
3995     IF NOT l_return_val THEN --serial_number_tag doesn't exist
3996       --Modified by mpothuku on 13-Jul-2007 for fixing the Bug 4337259
3997       /*
3998       IF l_mfg_serial_number_flag = 'Y' THEN
3999         l_serial_number_tag := 'INVENTORY';
4000       ELSE
4001         l_serial_number_tag := 'TEMPORARY';
4002       END IF;
4003       */
4004       l_serial_number_tag := 'ACTUAL';
4005       --mpothuku End
4006       AHL_Util_UC_Pkg.getcsi_attribute_id('AHL_TEMP_SERIAL_NUM', l_attribute_id, l_return_val);
4007 
4008       IF NOT(l_return_val) THEN
4009         FND_MESSAGE.set_name('AHL','AHL_UC_ATTRIB_CODE_MISSING');
4010         FND_MESSAGE.set_token('CODE', 'AHL_TEMP_SERIAL_NUM');
4011         FND_MSG_PUB.add;
4012       ELSE
4013         l_csi_extend_attrib_rec.attribute_id := l_attribute_id;
4014         l_csi_extend_attrib_rec.attribute_value := l_serial_number_tag;
4015         l_csi_extend_attrib_rec.instance_id := p_instance_id;
4016         l_csi_ext_attrib_values_tbl(1) := l_csi_extend_attrib_rec;
4017       END IF;
4018 
4019       CSI_ITEM_INSTANCE_PUB.create_extended_attrib_values(
4020                           p_api_version            => 1.0,
4021                           p_txn_rec                => l_csi_transaction_rec,
4022                           p_ext_attrib_tbl         => l_csi_ext_attrib_values_tbl,
4023                           x_return_status          => l_return_status,
4024                           x_msg_count              => l_msg_count,
4025                           x_msg_data               => l_msg_data);
4026 
4027       IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
4028         RAISE FND_API.G_EXC_ERROR;
4029       ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
4030         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4031       END IF;
4032     END IF;
4033   END IF;
4034 
4035   --Check to see whether an extra node relationship record has already existed.
4036   --then just update the position_reference from null to p_relationship_id, otherwise
4037   --need to create a new csi_ii_relationship record
4038   OPEN check_extra_node(p_parent_instance_id, p_instance_id);
4039   FETCH check_extra_node INTO l_csi_relationship_id, l_object_version_number;
4040   IF check_extra_node%FOUND THEN
4041     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4042         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name,
4043                        ' sibling extra node found'||
4044                        ' p_csi_ii_ovn => '||p_csi_ii_ovn);
4045     END IF;
4046 
4047     --Validate input parameters p_csi_ii_ovn
4048     IF (p_csi_ii_ovn IS NULL OR p_csi_ii_ovn <= 0 ) THEN
4049       FND_MESSAGE.set_name('AHL','AHL_UC_API_PARAMETER_INVALID');
4050       FND_MESSAGE.set_token('NAME', 'csi_ii_ovn');
4051       FND_MESSAGE.set_token('VALUE', p_csi_ii_ovn);
4052       FND_MSG_PUB.add;
4053       CLOSE check_extra_node;
4054       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4055     ELSIF l_object_version_number <> p_csi_ii_ovn THEN
4056       FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
4057       FND_MSG_PUB.add;
4058       CLOSE check_extra_node;
4059       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4060     END IF;
4061     l_csi_relationship_rec.relationship_id := l_csi_relationship_id;
4062     l_csi_relationship_rec.object_version_number := l_object_version_number;
4063     l_csi_relationship_rec.relationship_type_code := 'COMPONENT-OF';
4064     l_csi_relationship_rec.object_id := p_parent_instance_id;
4065     l_csi_relationship_rec.subject_id := p_instance_id;
4066     l_csi_relationship_rec.position_reference := to_char(p_relationship_id);
4067     l_csi_relationship_tbl(1) := l_csi_relationship_rec;
4068     CSI_II_RELATIONSHIPS_PUB.update_relationship(
4069                              p_api_version      => 1.0,
4070                              p_relationship_tbl => l_csi_relationship_tbl,
4071                              p_txn_rec          => l_csi_transaction_rec,
4072                              x_return_status    => l_return_status,
4073                              x_msg_count        => l_msg_count,
4074                              x_msg_data         => l_msg_data);
4075   ELSE
4076     -- SATHAPLI::FP ER 6504147, 18-Nov-2008
4077     -- check if it is unassigned extra instance attached to any of the parents uptill the root node
4078     OPEN check_unasgnd_extra_node_csr(p_parent_instance_id, p_instance_id);
4079     FETCH check_unasgnd_extra_node_csr INTO l_csi_relationship_id, l_object_version_number;
4080 
4081     IF check_unasgnd_extra_node_csr%FOUND THEN
4082       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4083           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name,
4084                          ' extra node attached to any of the parents uptill root found'||
4085                          ' p_parent_instance_id => '||p_parent_instance_id||
4086                          ' p_instance_id => '||p_instance_id||
4087                          ' p_csi_ii_ovn => '||p_csi_ii_ovn);
4088       END IF;
4089 
4090       -- Validate input parameters p_csi_ii_ovn
4091       IF (p_csi_ii_ovn IS NULL OR p_csi_ii_ovn <= 0 ) THEN
4092         FND_MESSAGE.set_name('AHL','AHL_UC_API_PARAMETER_INVALID');
4093         FND_MESSAGE.set_token('NAME', 'csi_ii_ovn');
4094         FND_MESSAGE.set_token('VALUE', p_csi_ii_ovn);
4095         FND_MSG_PUB.add;
4096         CLOSE check_unasgnd_extra_node_csr;
4097         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4098       ELSIF l_object_version_number <> p_csi_ii_ovn THEN
4099         FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
4100         FND_MSG_PUB.add;
4101         CLOSE check_unasgnd_extra_node_csr;
4102         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4103       END IF;
4104 
4105       -- expire the existing relationship
4106       -- Set CSI relationship record
4107       l_csi_relationship_rec.relationship_id := l_csi_relationship_id;
4108       l_csi_relationship_rec.object_version_number := l_object_version_number;
4109 
4110       CSI_II_RELATIONSHIPS_PUB.expire_relationship(
4111                                p_api_version      => 1.0,
4112                                p_relationship_rec => l_csi_relationship_rec,
4113                                p_txn_rec          => l_csi_transaction_rec,
4114                                x_instance_id_lst  => l_csi_instance_id_lst,
4115                                x_return_status    => l_return_status,
4116                                x_msg_count        => l_msg_count,
4117                                x_msg_data         => l_msg_data);
4118 
4119       IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
4120         RAISE FND_API.G_EXC_ERROR;
4121       ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
4122         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4123       END IF;
4124 
4125       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4126           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name,
4127                          ' unassigned extra node relationship expired');
4128       END IF;
4129 
4130       -- create new relationship
4131       -- create new CSI record
4132       l_csi_relationship_new_rec.relationship_type_code := 'COMPONENT-OF';
4133       l_csi_relationship_new_rec.object_id := p_parent_instance_id;
4134       l_csi_relationship_new_rec.subject_id := p_instance_id;
4135       l_csi_relationship_new_rec.position_reference := to_char(p_relationship_id);
4136       l_csi_relationship_tbl(1) := l_csi_relationship_new_rec;
4137       CSI_II_RELATIONSHIPS_PUB.create_relationship(
4138                                p_api_version      => 1.0,
4139                                p_relationship_tbl => l_csi_relationship_tbl,
4140                                p_txn_rec          => l_csi_transaction_rec,
4141                                x_return_status    => l_return_status,
4142                                x_msg_count        => l_msg_count,
4143                                x_msg_data         => l_msg_data);
4144 
4145       IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
4146         RAISE FND_API.G_EXC_ERROR;
4147       ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
4148         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4149       END IF;
4150 
4151       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4152           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name,
4153                          ' unassigned extra node new relationship created');
4154       END IF;
4155     ELSE
4156       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4157           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, 'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name,
4158                          ' free standing instance found'||
4159                          ' p_csi_ii_ovn => '||p_csi_ii_ovn);
4160       END IF;
4161 
4162       l_csi_relationship_rec.relationship_type_code := 'COMPONENT-OF';
4163       l_csi_relationship_rec.object_id := p_parent_instance_id;
4164       l_csi_relationship_rec.subject_id := p_instance_id;
4165       l_csi_relationship_rec.position_reference := to_char(p_relationship_id);
4166       l_csi_relationship_tbl(1) := l_csi_relationship_rec;
4167       CSI_II_RELATIONSHIPS_PUB.create_relationship(
4168                                p_api_version      => 1.0,
4169                                p_relationship_tbl => l_csi_relationship_tbl,
4170                                p_txn_rec          => l_csi_transaction_rec,
4171                                x_return_status    => l_return_status,
4172                                x_msg_count        => l_msg_count,
4173                                x_msg_data         => l_msg_data);
4174     END IF; -- end IF check_unasgnd_extra_node_csr%FOUND
4175 
4176     CLOSE check_unasgnd_extra_node_csr;
4177   END IF;
4178   CLOSE check_extra_node;
4179   IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
4180     RAISE FND_API.G_EXC_ERROR;
4181   ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
4182     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4183   END IF;
4184 
4185   --If the node is the top node of a sub-unit then just itself, otherwise if it is a
4186   --branch node, then get all of its first level sub-units. For all of these sub-units,
4187   --we have to update their parent_uc_header_id to p_uc_header_id. Once a unit is installed
4188   --to another unit and automatically becomes a sub-unit, then it loses all its statuses. So
4189   --we don't have to update the two statuses here.
4190 
4191   IF l_subunit THEN
4192     ahl_util_uc_pkg.get_parent_uc_header(p_instance_id,
4193                                          l_parent_uc_header_id,
4194                                          l_parent_instance_id);
4195     UPDATE ahl_unit_config_headers
4196        --SET parent_uc_header_id = p_uc_header_id
4197        --The parameter p_uc_header_id is not necessarily the parent uc_header_id of the newly
4198        --installed instance.
4199        SET parent_uc_header_id = l_parent_uc_header_id,
4200            object_version_number = object_version_number + 1,
4201            last_update_date = SYSDATE,
4202            last_updated_by = FND_GLOBAL.user_id,
4203            last_update_login = FND_GLOBAL.login_id
4204      WHERE csi_item_instance_id = p_instance_id
4205        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
4206            --Not necessary to check the object_version_number here
4207     --Copy the change to history table
4208     ahl_util_uc_pkg.copy_uc_header_to_history(l_sub_uc_header_id, l_return_status);
4209     --IF history copy failed, then don't raise exception, just add the message to the message stack
4210     IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4211       FND_MESSAGE.set_name('AHL', 'AHL_UC_HISTORY_COPY_FAILED');
4212       FND_MSG_PUB.add;
4213     END IF;
4214   ELSIF l_instance_type = 'B' THEN --this instance is a branch node
4215     ahl_util_uc_pkg.get_parent_uc_header(p_instance_id,
4216                                          l_parent_uc_header_id,
4217                                          l_parent_instance_id);
4218     FOR l_get_1st_level_subunit IN get_1st_level_subunits(p_instance_id) LOOP
4219       UPDATE ahl_unit_config_headers
4220          SET parent_uc_header_id = l_parent_uc_header_id,
4221              object_version_number = object_version_number + 1,
4222              last_update_date = SYSDATE,
4223              last_updated_by = FND_GLOBAL.user_id,
4224              last_update_login = FND_GLOBAL.login_id
4225        WHERE csi_item_instance_id = l_get_1st_level_subunit.subject_id
4226          AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
4227              --Not necessary to check the object_version_number here
4228 
4229       OPEN get_uc_header(l_get_1st_level_subunit.subject_id);
4230       FETCH get_uc_header INTO l_sub_uc_header_id, l_mc_header_id;
4231       IF get_uc_header%NOTFOUND THEN
4232         FND_MESSAGE.set_name('AHL', 'AHL_UC_INSTANCE_NOT_IN_UC');
4233         FND_MESSAGE.set_token('INSTANCE', l_get_1st_level_subunit.subject_id);
4234         FND_MSG_PUB.add;
4235         CLOSE get_uc_header;
4236         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4237       ELSE
4238         CLOSE get_uc_header;
4239       END IF;
4240 
4241       --Copy the change to history table
4242       ahl_util_uc_pkg.copy_uc_header_to_history(l_sub_uc_header_id, l_return_status);
4243       --IF history copy failed, then don't raise exception, just add the messageto the message stack
4244 
4245       IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
4246         FND_MESSAGE.set_name('AHL', 'AHL_UC_HISTORY_COPY_FAILED');
4247         FND_MSG_PUB.add;
4248       END IF;
4249     END LOOP;
4250   END IF;
4251 
4252   -- SURRKUMA :: 13694898 :: Flag to bypass the validation of position rules, 29-FEB-2012
4253   IF p_validate_rule_flag = FND_API.G_TRUE THEN
4254     --Call completeness check API for the newly assigned instance
4255     ahl_uc_validation_pub.validate_complete_for_pos(
4256         p_api_version         => 1.0,
4257         p_init_msg_list       => FND_API.G_FALSE,
4258         p_commit              => FND_API.G_FALSE,
4259         p_validation_level    => FND_API.G_VALID_LEVEL_FULL,
4260         x_return_status       => l_return_status,
4261         x_msg_count           => l_msg_count,
4262         x_msg_data            => l_msg_data,
4263         p_csi_instance_id     => p_instance_id,
4264         x_error_tbl           => x_warning_msg_tbl);
4265     IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
4266       RAISE FND_API.G_EXC_ERROR;
4267     ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
4268       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4269     END IF;
4270   END IF;
4271 
4272   --For sub unit top node, it is not necessary to have this item interchange type
4273   --code validation check
4274   -- SATHAPLI::ER 7419780, 24-Aug-2009, add the fetched INTERCHANGE_REASON as well, to the warning message AHL_UC_1WAY_ITEM_INSTALLED.
4275 
4276   IF l_instance_type <> 'S' THEN
4277     OPEN get_interchange_type(p_instance_id, p_relationship_id);
4278     FETCH get_interchange_type INTO l_interchange_type_code, l_interchange_reason;
4279     IF get_interchange_type%NOTFOUND THEN
4280       FND_MESSAGE.set_name('AHL', 'AHL_UC_ITEM_INTERCHANGE_MISS');
4281       FND_MESSAGE.set_token('INSTANCE', p_instance_id);
4282       FND_MSG_PUB.add;
4283       CLOSE get_interchange_type;
4284       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4285     ELSIF l_interchange_type_code = '1-WAY INTERCHANGEABLE' THEN
4286       FND_MESSAGE.set_name('AHL', 'AHL_UC_1WAY_ITEM_INSTALLED');
4287       SELECT f.meaning INTO l_position_ref_meaning
4288         FROM ahl_mc_relationships a,
4289              fnd_lookups f
4290        WHERE a.relationship_id = p_relationship_id
4291          AND f.lookup_code (+) = A.position_ref_code
4292          AND f.lookup_type (+) = 'AHL_POSITION_REFERENCE' ;
4293       FND_MESSAGE.set_token('POSITION', l_position_ref_meaning);
4294       FND_MESSAGE.set_token('REASON', l_interchange_reason);
4295       --Here the message is not added to the global message stack;
4296       IF x_warning_msg_tbl.count > 0 THEN
4297         x_warning_msg_tbl(x_warning_msg_tbl.last + 1) := FND_MESSAGE.get;
4298       ELSE
4299         x_warning_msg_tbl(0) := FND_MESSAGE.get;
4300       END IF;
4301     END IF;
4302     CLOSE get_interchange_type;
4303   END IF;
4304 
4305   --For UC user, UC header status change needs to be made after the operation
4306   --Not confirmed whether need to copy the record into UC header history table
4307   --after status change. Not include the copy right now. (No history copy)
4308   IF p_prod_user_flag = 'N' THEN
4309     IF (l_root_uc_status_code = 'COMPLETE' AND x_warning_msg_tbl.count > 0) THEN
4310     --IF unit_config_status_code='INCOMPLETE' and active_uc_status_code='UNAPPROVED', this
4311     --update is only object_version_number change and not necessary.
4312       UPDATE ahl_unit_config_headers
4313          SET unit_config_status_code = 'INCOMPLETE',
4314              active_uc_status_code = 'UNAPPROVED',
4315              object_version_number = object_version_number + 1,
4316              last_update_date = SYSDATE,
4317              last_updated_by = FND_GLOBAL.user_id,
4318              last_update_login = FND_GLOBAL.login_id
4319        WHERE unit_config_header_id = l_root_uc_header_id
4320          AND object_version_number = l_root_uc_ovn;
4321       IF SQL%ROWCOUNT = 0 THEN
4322         FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
4323         FND_MSG_PUB.add;
4324         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4325       END IF;
4326     ELSIF (l_root_uc_status_code IN ('COMPLETE', 'INCOMPLETE') AND
4327            (l_root_active_uc_status_code IS NULL OR
4328             l_root_active_uc_status_code <> 'UNAPPROVED')) THEN
4329     --IF unit_config_status_code='INCOMPLETE' and active_uc_status_code='UNAPPROVED', this
4330     --update is only object_version_number change and not necessary.
4331       UPDATE ahl_unit_config_headers
4332          SET active_uc_status_code = 'UNAPPROVED',
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     ELSIF l_root_uc_status_code NOT IN ('COMPLETE', 'INCOMPLETE', 'DRAFT') THEN
4345       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4346         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
4347                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Before calling completeness check',
4348                    'p_uc_header_id='||p_uc_header_id||'l_root_uc_header_id='||l_root_uc_header_id||
4349                    'l_root_uc_ovn='||l_root_uc_ovn);
4350       END IF;
4351     --IF unit_config_status_code='DRAFT', this update is only object_version_number change and
4352     --not necessary.
4353       UPDATE ahl_unit_config_headers
4354          SET unit_config_status_code = 'DRAFT',
4355              object_version_number = object_version_number + 1,
4356              last_update_date = SYSDATE,
4357              last_updated_by = FND_GLOBAL.user_id,
4358              last_update_login = FND_GLOBAL.login_id
4359        WHERE unit_config_header_id = l_root_uc_header_id
4360          AND object_version_number = l_root_uc_ovn;
4361       IF SQL%ROWCOUNT = 0 THEN
4362         FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
4363         FND_MSG_PUB.add;
4364         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4365       END IF;
4366     END IF;
4367   ELSIF (p_prod_user_flag = 'Y' AND
4368          x_warning_msg_tbl.count > 0 AND
4369          l_root_uc_status_code = 'COMPLETE') THEN
4370     UPDATE ahl_unit_config_headers
4371        SET unit_config_status_code = 'INCOMPLETE',
4372            object_version_number = object_version_number + 1,
4373            last_update_date = SYSDATE,
4374            last_updated_by = FND_GLOBAL.user_id,
4375            last_update_login = FND_GLOBAL.login_id
4376      WHERE unit_config_header_id = l_root_uc_header_id
4377        AND object_version_number = l_root_uc_ovn;
4378     IF SQL%ROWCOUNT = 0 THEN
4379       FND_MESSAGE.set_name( 'AHL','AHL_COM_RECORD_CHANGED' );
4380       FND_MSG_PUB.add;
4381       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4382     END IF;
4383   END IF;
4384 
4385   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4386     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
4387                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': After normal execution',
4388                    'At the end of the procedure');
4389   END IF;
4390 
4391   --Get all the error messages from the previous steps (if any) and raise the appropriate Exception
4392   l_msg_count := FND_MSG_PUB.count_msg;
4393   IF l_msg_count > 0 THEN
4394     x_msg_count := l_msg_count;
4395     RAISE FND_API.G_EXC_ERROR;
4396   END IF;
4397   -- Perform the Commit (if requested)
4398   IF FND_API.to_boolean(p_commit) THEN
4399     COMMIT;
4400   END IF;
4401   --Count and Get messages(optional)
4402   FND_MSG_PUB.count_and_get(
4403     p_encoded  => FND_API.G_FALSE,
4404     p_count    => x_msg_count,
4405     p_data     => x_msg_data);
4406 EXCEPTION
4407   WHEN FND_API.G_EXC_ERROR THEN
4408     ROLLBACK TO install_existing_instance;
4409     x_return_status := FND_API.G_RET_STS_ERROR;
4410     FND_MSG_PUB.count_and_get(
4411       p_encoded  => FND_API.G_FALSE,
4412       p_count    => x_msg_count,
4413       p_data     => x_msg_data);
4414   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4415     ROLLBACK TO install_existing_instance;
4416     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4417     FND_MSG_PUB.count_and_get(
4418       p_encoded  => FND_API.G_FALSE,
4419       p_count    => x_msg_count,
4420       p_data     => x_msg_data);
4421   WHEN OTHERS THEN
4422     ROLLBACK TO install_existing_instance;
4423     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4424     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
4425     THEN
4426       FND_MSG_PUB.add_exc_msg(
4427         p_pkg_name         => G_PKG_NAME,
4428         p_procedure_name   => l_api_name,
4429         p_error_text       => SUBSTRB(SQLERRM,1,240));
4430     END IF;
4431     FND_MSG_PUB.count_and_get(
4432       p_encoded  => FND_API.G_FALSE,
4433       p_count    => x_msg_count,
4434       p_data     => x_msg_data);
4435 END;
4436 
4437 -- Define procedure swap_instances
4438 -- This API is used by Production user to make parts change: replace an old instance
4439 -- a new one in a UC tree.
4440 PROCEDURE swap_instance(
4441   p_api_version           IN  NUMBER := 1.0,
4442   p_init_msg_list         IN  VARCHAR2 := FND_API.G_FALSE,
4443   p_commit                IN  VARCHAR2 := FND_API.G_FALSE,
4444   p_validation_level      IN  NUMBER := FND_API.G_VALID_LEVEL_FULL,
4445   x_return_status         OUT NOCOPY VARCHAR2,
4446   x_msg_count             OUT NOCOPY NUMBER,
4447   x_msg_data              OUT NOCOPY VARCHAR2,
4448   p_uc_header_id          IN  NUMBER,
4449   p_parent_instance_id    IN  NUMBER,
4450   p_old_instance_id       IN  NUMBER,
4451   p_new_instance_id       IN  NUMBER,
4452   p_new_instance_number   IN  csi_item_instances.instance_number%TYPE := NULL,
4453   p_relationship_id       IN  NUMBER,
4454   p_csi_ii_ovn            IN  NUMBER,
4455   p_prod_user_flag        IN  VARCHAR2,
4456   x_warning_msg_tbl       OUT NOCOPY ahl_uc_validation_pub.error_tbl_type)
4457 IS
4458   l_api_name       CONSTANT   VARCHAR2(30) := 'swap_instance';
4459   l_api_version    CONSTANT   NUMBER       := 1.0;
4460   l_return_status             VARCHAR2(1);
4461   l_msg_count                 NUMBER;
4462   l_msg_data                  VARCHAR2(2000);
4463   l_relationship_id           NUMBER;
4464   CURSOR check_relationship_id(c_subject_id NUMBER, c_relationship_id NUMBER) IS
4465     SELECT 'X'
4466       FROM csi_ii_relationships
4467      WHERE subject_id = c_subject_id
4468        AND position_reference = to_char(c_relationship_id)
4469        AND relationship_type_code = 'COMPONENT-OF'
4470        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
4471        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
4472 BEGIN
4473   -- Initialize API return status to success
4474   x_return_status := FND_API.G_RET_STS_SUCCESS;
4475 
4476   -- Standard Start of API savepoint
4477   SAVEPOINT swap_instance;
4478 
4479   -- Standard call to check for call compatibility.
4480   IF NOT FND_API.compatible_api_call(
4481     l_api_version,
4482     p_api_version,
4483     l_api_name,
4484     G_PKG_NAME)
4485   THEN
4486     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4487   END IF;
4488 
4489   -- Initialize message list if p_init_msg_list is set to TRUE.
4490   IF FND_API.to_boolean(p_init_msg_list) THEN
4491     FND_MSG_PUB.initialize;
4492   END IF;
4493 
4494   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4495     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
4496                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Begin API',
4497                    'At the start of the procedure');
4498   END IF;
4499 
4500   --Call remove_instance to remove the old instance
4501   remove_instance(
4502                   p_api_version      => 1.0,
4503                   p_init_msg_list    => FND_API.G_FALSE,
4504                   p_commit           => FND_API.G_FALSE,
4505                   p_validation_level => FND_API.G_VALID_LEVEL_FULL,
4506                   x_return_status    => l_return_status,
4507                   x_msg_count        => l_msg_count,
4508                   x_msg_data         => l_msg_data,
4509                   p_uc_header_id     => p_uc_header_id,
4510                   p_instance_id      => p_old_instance_id,
4511                   p_csi_ii_ovn       => p_csi_ii_ovn,
4512                   p_prod_user_flag   => p_prod_user_flag);
4513   IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
4514     RAISE FND_API.G_EXC_ERROR;
4515   ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
4516     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4517   END IF;
4518 
4519   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4520     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
4521                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': After calling remove_instance API',
4522                    'At the middle of the procedure');
4523   END IF;
4524 
4525   --Only need to ensure, the position_reference of the old_instance_id is just
4526   --the one of the new_instance_id. All the other validations will be made in the
4527   --other two called APIs.
4528   OPEN check_relationship_id(p_old_instance_id, p_relationship_id);
4529   IF check_relationship_id%NOTFOUND THEN
4530     FND_MESSAGE.set_name( 'AHL','AHL_UC_POSITION_NOT_SAME' );
4531     FND_MESSAGE.set_token('POSITION', p_relationship_id);
4532     FND_MSG_PUB.add;
4533     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4534   END IF;
4535 
4536   --Call install_existing_instance to install the new instance
4537   install_existing_instance(p_api_version        => 1.0,
4538                             p_init_msg_list      => FND_API.G_FALSE,
4539                             p_commit             => FND_API.G_FALSE,
4540                             p_validation_level   => FND_API.G_VALID_LEVEL_FULL,
4541                             x_return_status      => l_return_status,
4542                             x_msg_count          => l_msg_count,
4543                             x_msg_data           => l_msg_data,
4544                             p_uc_header_id       => p_uc_header_id,
4545                             p_parent_instance_id => p_parent_instance_id,
4546                             p_instance_id        => p_new_instance_id,
4547                             p_instance_number    => p_new_instance_number,
4548                             p_relationship_id    => p_relationship_id,
4549                             p_csi_ii_ovn         => p_csi_ii_ovn,
4550                             p_prod_user_flag     => p_prod_user_flag,
4551                             x_warning_msg_tbl    => x_warning_msg_tbl);
4552   IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
4553     RAISE FND_API.G_EXC_ERROR;
4554   ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
4555     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4556   END IF;
4557 
4558   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4559     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
4560                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': After calling install_existing_instance API and comes to normal execution',
4561                    'At the end of the procedure');
4562   END IF;
4563 
4564   -- Get all the error messages from the previous steps (if any) and raise the appropriate Exception
4565   l_msg_count := FND_MSG_PUB.count_msg;
4566   IF l_msg_count > 0 THEN
4567     x_msg_count := l_msg_count;
4568     RAISE FND_API.G_EXC_ERROR;
4569   END IF;
4570   -- Count and Get messages (optional)
4571   FND_MSG_PUB.count_and_get(
4572     p_encoded  => FND_API.G_FALSE,
4573     p_count    => x_msg_count,
4574     p_data     => x_msg_data);
4575 EXCEPTION
4576   WHEN FND_API.G_EXC_ERROR THEN
4577     ROLLBACK TO swap_instance;
4578     x_return_status := FND_API.G_RET_STS_ERROR ;
4579     FND_MSG_PUB.count_and_get(
4580       p_encoded  => FND_API.G_FALSE,
4581       p_count    => x_msg_count,
4582       p_data     => x_msg_data);
4583   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4584     ROLLBACK TO swap_instance;
4585     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4586     FND_MSG_PUB.count_and_get(
4587       p_encoded  => FND_API.G_FALSE,
4588       p_count    => x_msg_count,
4589       p_data     => x_msg_data);
4590   WHEN OTHERS THEN
4591     ROLLBACK TO swap_instance;
4592     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
4593     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
4594     THEN
4595       FND_MSG_PUB.add_exc_msg(
4596         p_pkg_name         => G_PKG_NAME,
4597         p_procedure_name   => l_api_name,
4598         p_error_text       => SUBSTRB(SQLERRM,1,240));
4599     END IF;
4600     FND_MSG_PUB.count_and_get(
4601       p_encoded  => FND_API.G_FALSE,
4602       p_count    => x_msg_count,
4603       p_data     => x_msg_data);
4604 END;
4605 
4606 --****************************************************************************
4607 -- Procedure for getting all instances that are available in sub inventory and
4608 -- available for installation at a particular UC position.
4609 -- Adithya added for OGMA issue # 86 FP
4610 --****************************************************************************
4611 PROCEDURE Get_Avail_Subinv_Instances(
4612   p_api_version            IN  NUMBER := 1.0,
4613   p_init_msg_list          IN  VARCHAR2 := FND_API.G_FALSE,
4614   p_validation_level       IN  NUMBER := FND_API.G_VALID_LEVEL_FULL,
4615   x_return_status          OUT NOCOPY VARCHAR2,
4616   x_msg_count              OUT NOCOPY NUMBER,
4617   x_msg_data               OUT NOCOPY VARCHAR2,
4618   p_relationship_id        IN  NUMBER,
4619   p_item_number            IN  VARCHAR2 :='%',
4620   p_serial_number          IN  VARCHAR2 :='%',
4621   p_instance_number        IN  VARCHAR2 :='%',
4622   p_workorder_id           IN  NUMBER := NULL, --required by Part Changes
4623   p_start_row_index        IN  NUMBER,
4624   p_max_rows               IN  NUMBER,
4625   x_avail_subinv_instance_tbl OUT NOCOPY available_instance_tbl_type
4626 )
4627 IS
4628 
4629 
4630 --Cursor for getting visit details
4631 
4632 CURSOR c_get_visit_details(c_workorder_id NUMBER)
4633 IS
4634 SELECT
4635   VST.visit_id,
4636   VST.project_id,
4637   VST.inv_locator_id,
4638   AWO.wip_entity_id
4639 FROM
4640   AHL_VISITS_B VST,
4641   AHL_WORKORDERS AWO
4642 WHERE
4643       VST.status_code NOT IN ('DELETED', 'CANCELLED')
4644       AND AWO.visit_id = VST.visit_id
4645       AND AWO.workorder_id = c_workorder_id;
4646 
4647 l_visit_details_rec c_get_visit_details%ROWTYPE;
4648 
4649 
4650 CURSOR c_get_subinv_inst(c_relationship_id NUMBER,
4651                        c_item_number VARCHAR2,
4652                        c_instance_number VARCHAR2,
4653                        c_serial_number VARCHAR2,
4654                        c_wip_job_id NUMBER,
4655                        c_project_id NUMBER,
4656                        c_inv_locator_id NUMBER
4657                        )
4658 IS
4659     SELECT C.instance_id,
4660            C.instance_number,
4661            C.inventory_item_id,
4662            C.inv_master_organization_id,
4663            C.quantity,
4664            C.inventory_revision,
4665            C.unit_of_measure uom_code,
4666            C.inv_subinventory_name,
4667            C.inv_locator_id,
4668            to_number(NULL) uc_header_id
4669       FROM csi_item_instances C,
4670            mtl_system_items_kfv M,
4671            ahl_mc_relationships R,
4672            ahl_item_associations_b A
4673      WHERE C.inventory_item_id = M.inventory_item_id
4674        AND C.inv_master_organization_id = M.organization_id
4675        AND R.item_group_id = A.item_group_id
4676        AND C.inventory_item_id = A.inventory_item_id
4677        AND R.relationship_id = c_relationship_id
4678        AND trunc(nvl(R.active_start_date, SYSDATE)) <= trunc(SYSDATE)
4679        AND trunc(nvl(R.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
4680        AND trunc(nvl(C.active_start_date, SYSDATE)) <= trunc(SYSDATE)
4681        AND trunc(nvl(C.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
4682        AND C.location_type_code IN ('INVENTORY')
4683        --AND C.location_type_code NOT IN ('PO','IN-TRANSIT','PROJECT','INVENTORY')
4684        AND A.interchange_type_code IN ('1-WAY INTERCHANGEABLE', '2-WAY INTERCHANGEABLE')
4685        AND (A.revision IS NULL OR A.revision = C.inventory_revision) --Added by Jerry on 03/31/2005
4686        --
4687        -- not installed in any position so far.
4688        --
4689        AND NOT EXISTS (
4690 			 SELECT 1
4691 			  FROM csi_ii_relationships i1
4692 			 WHERE i1.subject_id = C.instance_id
4693 			   AND i1.relationship_type_code = 'COMPONENT-OF'
4694 			   AND trunc(nvl(i1.active_start_date, SYSDATE)) <= trunc(SYSDATE)
4695 			   AND trunc(nvl(i1.active_end_date, SYSDATE+1)) >trunc(SYSDATE)
4696                        )
4697        --
4698        -- its not issued to any workorder already.
4699        --
4700        AND C.wip_job_id IS NULL
4701        --
4702        -- Its not in the top node of any UC.
4703        --
4704        AND NOT EXISTS (
4705                         SELECT 1
4706                          FROM ahl_unit_config_headers H
4707                         WHERE H.csi_item_instance_id = C.instance_id
4708                           AND trunc(nvl(H.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
4709                       )
4710        --
4711        -- and it satisfies other checks like serial number, instance and item number that are passed
4712        --
4713        AND upper(M.concatenated_segments) LIKE nvl(c_item_number,'%')
4714        AND upper(C.instance_number) LIKE nvl(c_instance_number,'%')
4715        AND upper(nvl(C.serial_number, '%')) LIKE nvl(c_serial_number,'%')
4716        -- SATHAPLI::Bug 9817209, 02-Jul-2010, changed the view ORG_ORGANIZATION_DEFINITIONS usage to
4717        -- view INV_ORGANIZATION_INFO_V instead
4718        /*
4719        AND M.organization_id IN (SELECT mp.master_organization_id
4720                                    FROM mtl_parameters mp, org_organization_definitions ood
4721                                   WHERE mp.organization_Id = ood.organization_id
4722                                   -- jaramana on Feb 14, 2008
4723                                   -- Removed reference to CLIENT_INFO
4724                                   AND NVL(ood.operating_unit, mo_global.get_current_org_id()) = mo_global.get_current_org_id())
4725        */
4726        AND EXISTS (
4727                    SELECT 'X'
4728                    FROM   mtl_parameters mp, inv_organization_info_v io
4729                    WHERE  mp.master_organization_id = M.organization_id AND
4730                           mp.organization_Id = io.organization_id AND
4731                           NVL(io.operating_unit, mo_global.get_current_org_id()) =
4732                               mo_global.get_current_org_id()
4733                   )
4734        AND C.inv_locator_id IN (
4735 	                            SELECT
4736 	                              ILOC.inventory_location_id
4737 	                            FROM
4738 	                              -- jaramana on Feb 14, 2008 for bug 6819370
4739 	                              -- Changed MTL_ITEM_LOCATIONS_KFV to MTL_ITEM_LOCATIONS
4740 	                              MTL_ITEM_LOCATIONS ILOC,
4741 	                              AHL_VISITS_B VST
4742 	                            WHERE
4743 	                              ILOC.subinventory_code = C.inv_subinventory_name
4744 	                              AND ILOC.organization_id = C.inv_organization_id
4745 	                              AND (ILOC.end_date_active IS NULL OR ILOC.end_date_active >= SYSDATE)
4746 	                              AND ILOC.segment19 = c_project_id
4747 	                              AND ILOC.physical_location_id = c_inv_locator_id
4748 	                       )
4749 
4750       AND EXISTS(--If serial number is present then check the status is "in stores"
4751                  (SELECT
4752 		   'X'
4753 		  FROM
4754 		   MTL_SERIAL_NUMBERS MSLN,
4755 		   MFG_LOOKUPS SL
4756 		  WHERE
4757 		   C.serial_number is not null
4758 		   AND MSLN.serial_number = C.serial_number
4759 		   AND MSLN.inventory_item_id = C.inventory_item_id
4760 		   AND MSLN.CURRENT_ORGANIZATION_ID = C.INV_ORGANIZATION_ID
4761 		   AND MSLN.CURRENT_STATUS  = SL.lookup_code
4762 		   AND SL.lookup_type = 'SERIAL_NUM_STATUS'
4763 		   AND MSLN.current_status = '3' -- "in stores"
4764 		  )
4765 		  UNION
4766 		  --If serial number not present then check on hand quantity > 0
4767 		  (
4768 		   SELECT
4769 		    'X'
4770 		   FROM
4771 		    MTL_ONHAND_QUANTITIES MOQ
4772 		   WHERE
4773 		    C.serial_number is null
4774 		    AND MOQ.inventory_item_id = C.inventory_item_id
4775 		    AND MOQ.ORGANIZATION_ID = C.INV_ORGANIZATION_ID
4776   		    AND MOQ.TRANSACTION_QUANTITY > 0
4777 		  )
4778 		)
4779 
4780 UNION ALL
4781 --
4782 -- A position can include alternate subconfigurations.
4783 -- This part of select clause is for picking top node instances of all alternate subconfigs.
4784 -- SATHAPLI::Bug 9817209, 02-Jul-2010, changed the view AHL_UNIT_CONFIG_HEADERS_V usage to base table
4785 --
4786         SELECT C.instance_id,
4787                C.instance_number,
4788                C.inventory_item_id,
4789                C.inv_master_organization_id,
4790                C.quantity,
4791                C.inventory_revision,
4792                C.unit_of_measure uom_code,
4793                C.inv_subinventory_name,
4794                C.inv_locator_id,
4795                U.uc_header_id uc_header_id
4796           FROM (
4797                 SELECT unit_config_header_id uc_header_id,
4798                        csi_item_instance_id csi_instance_id,
4799                        master_config_id mc_header_id,
4800                        unit_config_status_code uc_status_code,
4801                        active_end_date
4802                 FROM   ahl_unit_config_headers
4803                 -- this sub-unit should not be installed and should have valid status
4804                 WHERE  parent_uc_header_id IS NULL
4805                 AND    unit_config_status_code IN ('COMPLETE', 'INCOMPLETE')
4806                )U,
4807                csi_item_instances C,
4808                mtl_system_items_kfv M
4809          WHERE U.csi_instance_id = C.instance_id
4810            AND C.inventory_item_id = M.inventory_item_id
4811            AND C.inv_master_organization_id = M.organization_id
4812            AND trunc(nvl(U.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
4813            AND trunc(nvl(C.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
4814            AND C.location_type_code IN ('INVENTORY')
4815            --
4816            -- Check to see this UC is a subconfig
4817            --
4818            AND EXISTS (
4819                         SELECT 1
4820                          FROM ahl_mc_config_relations R
4821                         WHERE R.mc_header_id = U.mc_header_id
4822                           AND R.relationship_id = c_relationship_id
4823                           AND trunc(nvl(R.active_start_date, SYSDATE)) <= trunc(SYSDATE)
4824                           AND trunc(nvl(R.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
4825                       )
4826 	   -- SATHAPLI::Bug 9817209, 02-Jul-2010, this check is included above
4827 	   -- sub config is in valid status
4828 	   --
4829            /*
4830            AND (
4831                  U.parent_instance_id IS NULL
4832                  AND U.uc_status_code in ('COMPLETE', 'INCOMPLETE')
4833 	       )
4834            */
4835 	   --
4836 	   -- its not issued to any workorder already.
4837 	   --
4838 	   AND C.wip_job_id IS NULL
4839 	   --
4840 	   -- its not a parent for any other mc position.
4841 	   --
4842            AND NOT EXISTS (
4843                             SELECT 1
4844                              FROM ahl_mc_relationships MR
4845                             WHERE MR.parent_relationship_id = c_relationship_id
4846                               AND trunc(nvl(MR.active_start_date, SYSDATE)) <= trunc(SYSDATE)
4847                               AND trunc(nvl(MR.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
4848                            )
4849 	   --
4850 	   -- and it satisfies other checks like serial number, instance and item number that are passed
4851 	   --
4852            AND upper(M.concatenated_segments) LIKE nvl(c_item_number,'%')
4853            AND upper(C.instance_number) LIKE nvl(c_instance_number,'%')
4854            AND upper(nvl(C.serial_number, '%')) LIKE nvl(c_serial_number,'%')
4855            -- SATHAPLI::Bug 9817209, 02-Jul-2010, changed the view ORG_ORGANIZATION_DEFINITIONS usage to
4856            -- view INV_ORGANIZATION_INFO_V instead
4857            /*
4858            AND M.organization_id IN (SELECT mp.master_organization_id
4859                                        FROM mtl_parameters mp, org_organization_definitions ood
4860                                       WHERE mp.organization_Id = ood.organization_id
4861                                       -- jaramana on Feb 14, 2008
4862                                       -- Removed reference to CLIENT_INFO
4863                                       AND NVL(ood.operating_unit, mo_global.get_current_org_id()) = mo_global.get_current_org_id())
4864            */
4865            AND EXISTS (
4866                        SELECT 'X'
4867                        FROM   mtl_parameters mp, inv_organization_info_v io
4868                        WHERE  mp.master_organization_id = M.organization_id AND
4869                               mp.organization_Id = io.organization_id AND
4870                               NVL(io.operating_unit, mo_global.get_current_org_id()) =
4871                                   mo_global.get_current_org_id()
4872                       )
4873 	   AND C.inv_locator_id IN (
4874 	                            SELECT
4875 	                              ILOC.inventory_location_id
4876 	                            FROM
4877 	                              -- jaramana on Feb 14, 2008 for bug 6819370
4878 	                              -- Changed MTL_ITEM_LOCATIONS_KFV to MTL_ITEM_LOCATIONS
4879 	                              MTL_ITEM_LOCATIONS ILOC,
4880 	                              AHL_VISITS_B VST
4881 	                            WHERE
4882 	                              ILOC.subinventory_code = C.inv_subinventory_name
4883 	                              AND ILOC.organization_id = C.inv_organization_id
4884 	                              AND (ILOC.end_date_active IS NULL OR ILOC.end_date_active >= SYSDATE)
4885 	                              AND ILOC.segment19 = c_project_id
4886 	                              AND ILOC.physical_location_id = c_inv_locator_id
4887 	                           )
4888       AND EXISTS(--If serial number is present then check the status is "in stores"
4889                  (SELECT
4890 		   'X'
4891 		  FROM
4892 		   MTL_SERIAL_NUMBERS MSLN,
4893 		   MFG_LOOKUPS SL
4894 		  WHERE
4895 		   C.serial_number is not null
4896 		   AND MSLN.serial_number = C.serial_number
4897 		   AND MSLN.inventory_item_id = C.inventory_item_id
4898 		   AND MSLN.CURRENT_ORGANIZATION_ID = C.INV_ORGANIZATION_ID
4899 		   AND MSLN.CURRENT_STATUS  = SL.lookup_code
4900 		   AND SL.lookup_type = 'SERIAL_NUM_STATUS'
4901 		   AND MSLN.current_status = '3' -- "in stores"
4902 		  )
4903 		  UNION
4904 		  --If serial number not present then check on hand quantity > 0
4905 		  (
4906 		   SELECT
4907 		    'X'
4908 		   FROM
4909 		    MTL_ONHAND_QUANTITIES MOQ
4910 		   WHERE
4911 		    C.serial_number is null
4912 		    AND MOQ.inventory_item_id = C.inventory_item_id
4913 		    AND MOQ.ORGANIZATION_ID = C.INV_ORGANIZATION_ID
4914   		    AND MOQ.TRANSACTION_QUANTITY > 0
4915 		  )
4916 		);
4917 
4918 -- Cursor for validating relationship
4919 CURSOR check_relationship_id
4920 IS
4921 SELECT
4922    relationship_id
4923 FROM
4924    ahl_mc_relationships
4925 WHERE
4926     relationship_id = p_relationship_id
4927     AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
4928     AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
4929     AND mc_header_id IN (
4930                          SELECT
4931                             mc_header_id
4932                          FROM
4933                             ahl_mc_headers_b
4934                          WHERE
4935                              config_status_code = 'COMPLETE'
4936                         );
4937 -- Cursor for checking parent instance.
4938 CURSOR check_parent_instance(c_instance_id NUMBER)
4939 IS
4940   --Parent instance could be either in ahl_unit_config_headers(top node) or in csi_ii_relationships
4941   --(as the subject_id)
4942 SELECT
4943   'x'
4944 FROM
4945    ahl_unit_config_headers
4946 WHERE
4947    csi_item_instance_id = c_instance_id
4948    AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
4949 
4950 UNION ALL
4951 
4952 SELECT
4953     'x'
4954 FROM
4955     csi_ii_relationships
4956 WHERE
4957     subject_id = c_instance_id
4958     AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
4959     AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE);
4960 
4961 -- Cursor for getting top node instance
4962 CURSOR get_top_unit_instance(c_instance_id NUMBER)
4963 IS
4964 SELECT
4965     object_id
4966 FROM
4967     csi_ii_relationships
4968 WHERE
4969     object_id NOT IN (SELECT subject_id
4970                                FROM csi_ii_relationships
4971                               WHERE relationship_type_code = 'COMPONENT-OF'
4972                                 AND trunc(nvl(active_start_date,SYSDATE)) <= trunc(SYSDATE)
4973                                 AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE))
4974 START WITH subject_id = c_instance_id
4975        AND relationship_type_code = 'COMPONENT-OF'
4976        AND trunc(nvl(active_start_date,SYSDATE)) <= trunc(SYSDATE)
4977        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
4978 CONNECT BY subject_id = PRIOR object_id
4979        AND relationship_type_code = 'COMPONENT-OF'
4980        AND trunc(nvl(active_start_date,SYSDATE)) <= trunc(SYSDATE)
4981        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
4982 
4983 -- Cursor for getting UC status.
4984 CURSOR get_uc_status(c_instance_id NUMBER) IS
4985     SELECT unit_config_status_code
4986       FROM ahl_unit_config_headers
4987      WHERE csi_item_instance_id = c_instance_id
4988        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
4989 
4990   -- SATHAPLI::Bug 9817209, 02-Jul-2010, changed the view AHL_UNIT_CONFIG_HEADERS_V usage to base tables
4991   CURSOR ahl_instance_details (c_csi_item_instance_id IN NUMBER) IS
4992     SELECT A.csi_item_instance_id,
4993            A.csi_object_version csi_object_version_number,
4994            A.item_number,
4995            A.item_description,
4996            A.csi_instance_number,
4997            A.inventory_item_id,
4998            A.inventory_org_id,
4999            A.organization_code,
5000            A.serial_number,
5001            A.revision,
5002            A.lot_number,
5003            A.uom_code,
5004            A.quantity,
5005            A.install_date,
5006            A.mfg_date,
5007            A.location_description,
5008            A.party_type,
5009            A.owner_id,
5010            A.owner_number,
5011            A.owner_name,
5012            A.csi_location_id owner_site_id,
5013            A.owner_site_number,
5014            A.csi_party_object_version_num,
5015            A.status,
5016            A.condition,
5017            A.wip_entity_name,
5018            B.uc_header_id,
5019            B.uc_name,
5020            B.uc_status,
5021            B.mc_header_id,
5022            B.mc_name,
5023            B.mc_revision,
5024            B.mc_status,
5025            B.position_ref,
5026            B.root_uc_header_id
5027       FROM ahl_unit_installed_details_v A,
5028            (
5029             SELECT U.unit_config_header_id uc_header_id,
5030                    U.name uc_name,
5031                    UCSC.meaning uc_status,
5032                    U.master_config_id mc_header_id,
5033                    M.name mc_name,
5034                    M.revision mc_revision,
5035                    MCSC.meaning mc_status,
5036                    MRSC.meaning position_ref,
5037                    (
5038                     SELECT unit_config_header_id
5039                     FROM   ahl_unit_config_headers
5040                     WHERE  parent_uc_header_id IS NULL
5041                     START WITH
5042                            unit_config_header_id = U.unit_config_header_id
5043                     CONNECT BY
5044                            unit_config_header_id = PRIOR parent_uc_header_id
5045                    ) root_uc_header_id,
5046                    U.csi_item_instance_id csi_instance_id,
5047                    U.active_end_date active_end_date
5048             FROM   AHL_UNIT_CONFIG_HEADERS U, AHL_MC_HEADERS_B M,
5049                    AHL_MC_RELATIONSHIPS R, FND_LOOKUP_VALUES UCSC,
5050                    FND_LOOKUP_VALUES MRSC, FND_LOOKUP_VALUES MCSC
5051             WHERE  U.master_config_id = M.mc_header_id AND
5052                    M.mc_header_id = R.mc_header_id AND
5053                    R.parent_relationship_id IS NULL AND
5054                    U.unit_config_status_code = UCSC.lookup_code AND
5055                    'AHL_CONFIG_STATUS' = UCSC.lookup_type AND
5056     	           UCSC.language = USERENV('LANG') AND
5057                    M.config_status_code = MCSC.lookup_code AND
5058                    'AHL_CONFIG_STATUS' = MCSC.lookup_type AND
5059                    MCSC.language = USERENV('LANG') AND
5060                    R.position_ref_code = MRSC.lookup_code AND
5061                    'AHL_POSITION_REFERENCE' = MRSC.lookup_type AND
5062                    MRSC.language = USERENV('LANG')
5063            ) B
5064      WHERE A.csi_item_instance_id = c_csi_item_instance_id
5065        AND A.csi_item_instance_id = B.csi_instance_id (+)
5066        AND trunc(nvl(B.active_end_date (+), SYSDATE+1)) > trunc(SYSDATE);
5067   l_instance_details_rec ahl_instance_details%ROWTYPE;
5068 
5069 CURSOR get_priority (c_item_association_id IN NUMBER) IS
5070     SELECT priority
5071     FROM ahl_item_associations_b
5072     WHERE item_association_id = c_item_association_id;
5073 
5074 CURSOR get_csi_ii_relationship_ovn (c_instance_id NUMBER) IS
5075     SELECT object_version_number
5076       FROM csi_ii_relationships
5077      WHERE subject_id = c_instance_id
5078        AND position_reference IS NULL
5079        AND relationship_type_code = 'COMPONENT-OF'
5080        AND trunc(nvl(active_start_date,SYSDATE)) <= trunc(SYSDATE)
5081        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
5082   l_csi_ii_relationship_ovn   NUMBER;
5083 
5084 CURSOR c_get_locator_segments(c_inv_location_id NUMBER)
5085 IS
5086 SELECT
5087   concatenated_segments
5088 FROM
5089   MTL_ITEM_LOCATIONS_KFV
5090 WHERE
5091   inventory_location_id = c_inv_location_id;
5092 
5093 -- declare all local variables here
5094   l_api_name       CONSTANT   VARCHAR2(30) := 'Get_Avail_Subinv_Instances';
5095   l_api_version    CONSTANT   NUMBER       := 1.0;
5096   l_relationship_id           NUMBER;
5097   l_item_assoc_id             NUMBER;
5098   l_priority                  NUMBER;
5099   i                           NUMBER;
5100   j                           NUMBER;
5101   l_dummy_char                VARCHAR2(1);
5102   l_top_uc_status             ahl_unit_config_headers.unit_config_status_code%TYPE;
5103   l_top_instance_id           NUMBER;
5104   l_status                    fnd_lookup_values_vl.meaning%TYPE;
5105   l_msg_count                 NUMBER;
5106 
5107 BEGIN
5108 
5109      -- 0. Intial logic for the API.
5110      -------------------------------
5111      -- Initialize API return status to success
5112      x_return_status := FND_API.G_RET_STS_SUCCESS;
5113 
5114      -- Standard call to check for call compatibility.
5115      IF NOT FND_API.compatible_api_call(
5116        l_api_version,
5117        p_api_version,
5118        l_api_name,
5119        G_PKG_NAME)
5120      THEN
5121        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5122      END IF;
5123 
5124      -- Initialize message list if p_init_msg_list is set to TRUE.
5125      IF FND_API.to_boolean( p_init_msg_list ) THEN
5126        FND_MSG_PUB.initialize;
5127      END IF;
5128 
5129      IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5130    	FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
5131                       'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Begin API',
5132    			       'At the start of the procedure');
5133      END IF;
5134 
5135 
5136      IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5137 	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5138 		     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5139 			     'Logging API inputs');
5140 	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5141 		     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5142 			     '******************');
5143 	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5144 		     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5145 			     'p_relationship_id->'||p_relationship_id);
5146 	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5147 		     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5148 			     'p_item_number->'||p_item_number);
5149 	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5150 		     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5151 			     'p_serial_number->'||p_serial_number);
5152 	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5153 		     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5154 			     'p_instance_number->'||p_instance_number);
5155 	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5156 		     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5157 			     'p_workorder_id->'||p_workorder_id);
5158 	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5159 		     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5160 			     'p_start_row_index->'||p_start_row_index);
5161      	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5162      		     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5163      			     'p_max_rows->'||p_max_rows);
5164      END IF;
5165 
5166      --1. Do all mandatory validation here.
5167      --------------------------------------
5168      -- Work Order id is mandatory parmater. Throw error if its not passed.
5169      IF p_workorder_id IS NULL THEN
5170         -- Workorder is mandatory. Throw an error.
5171          FND_MESSAGE.set_name( 'AHL','AHL_COM_PARAM_MISSING' );-- check the message name here.
5172          FND_MSG_PUB.add;
5173          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5174      END IF;
5175 
5176      -- 1.b get all visit details and its validations
5177      OPEN c_get_visit_details(p_workorder_id);
5178      FETCH c_get_visit_details INTO l_visit_details_rec;
5179      CLOSE c_get_visit_details;
5180 
5181      -- 1.a validation corresponding to Work Order
5182      IF l_visit_details_rec.wip_entity_id IS NULL THEN
5183          FND_MESSAGE.set_name( 'AHL','AHL_UC_WORKORDER_INVALID' );
5184          FND_MESSAGE.set_token('WORKORDER', p_workorder_id);
5185          FND_MSG_PUB.add;
5186          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5187      END IF;
5188 
5189      IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5190 	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5191 		     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5192 			     'Visit Id derived->'||l_visit_details_rec.visit_id);
5193 	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5194 		     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5195 			     'project_id derived ->'||l_visit_details_rec.project_id);
5196 	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5197 		     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5198 			     'inv_locator_id setup at visit level->'||l_visit_details_rec.inv_locator_id);
5199      END IF;
5200 
5201      -- Rest of the logic is not applicable if visit doesnt have subinventory or inv_locator_id defined
5202      IF l_visit_details_rec.inv_locator_id IS NULL THEN
5203        RETURN;
5204      END IF;
5205 
5206      -- 1.c validation corresponding to relationship
5207      OPEN check_relationship_id;
5208      FETCH check_relationship_id INTO l_relationship_id;
5209      CLOSE check_relationship_id;
5210 
5211      IF l_relationship_id IS NULL THEN
5212        FND_MESSAGE.set_name( 'AHL','AHL_UC_POSITION_INVALID' );
5213        FND_MESSAGE.set_token('POSITION', p_relationship_id);
5214        FND_MSG_PUB.add;
5215        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5216      END IF;
5217 
5218      i := 0;
5219      j := 0;
5220      -- 2. Fetch all applicable instances and populate it in the out variable
5221      FOR  l_subinv_inst_rec IN c_get_subinv_inst(p_relationship_id,
5222                         			 p_item_number,
5223                         			 p_instance_number,
5224                         			 p_serial_number,
5225                         			 l_visit_details_rec.wip_entity_id,
5226 						 l_visit_details_rec.project_id,
5227 						 l_visit_details_rec.inv_locator_id)
5228      LOOP
5229 
5230 	    --If the instance is not a unit then, call procedure to validate whether thecorresponding inventory
5231 	    --can be assigned to that position
5232 
5233 	    IF l_subinv_inst_rec.uc_header_id IS NULL THEN
5234         AHL_UTIL_UC_PKG.validate_for_position(p_mc_relationship_id   => p_relationship_id,
5235                 p_inventory_id         => l_subinv_inst_rec.inventory_item_id,
5236                 p_organization_id      => l_subinv_inst_rec.inv_master_organization_id,
5237                 p_quantity             => l_subinv_inst_rec.quantity,
5238                 p_revision             => l_subinv_inst_rec.inventory_revision,
5239                 p_uom_code             => l_subinv_inst_rec.uom_code,
5240                 p_position_ref_meaning => NULL,
5241                 x_item_assoc_id        => l_item_assoc_id,
5242                 --Added by mpothuku on 17-May-2007 to fix the OGMA Issue 105.
5243                 p_ignore_quant_vald    => 'Y');
5244 	    END IF;
5245 
5246 	    IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5247 		  FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5248 			     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5249 				     'After position validation for l_subinv_inst_rec.instance_id->'||l_subinv_inst_rec.instance_id);
5250 		  FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5251 			     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5252 				     'fnd_msg_pub.count_msg->'||fnd_msg_pub.count_msg);
5253 	    END IF;
5254 
5255 	    IF (fnd_msg_pub.count_msg = 0) THEN
5256 	      i := i + 1;
5257 	      IF (i >= p_start_row_index AND i < p_start_row_index + p_max_rows) THEN
5258 		OPEN ahl_instance_details(l_subinv_inst_rec.instance_id);
5259 		FETCH ahl_instance_details INTO l_instance_details_rec;
5260 
5261                 IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5262 		  FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5263 			     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5264 				     'l_instance_details_rec.csi_item_instance_id->'||l_instance_details_rec.csi_item_instance_id);
5265 		END IF;
5266 
5267 		IF (ahl_instance_details%FOUND) THEN
5268 		  j := j + 1;
5269 		  x_avail_subinv_instance_tbl(j).csi_item_instance_id := l_instance_details_rec.csi_item_instance_id;
5270 		  x_avail_subinv_instance_tbl(j).csi_object_version_number := l_instance_details_rec.csi_object_version_number;
5271 		  x_avail_subinv_instance_tbl(j).item_number := l_instance_details_rec.item_number;
5272 		  x_avail_subinv_instance_tbl(j).item_description := l_instance_details_rec.item_description;
5273 		  x_avail_subinv_instance_tbl(j).csi_instance_number := l_instance_details_rec.csi_instance_number;
5274 		  x_avail_subinv_instance_tbl(j).organization_code := l_instance_details_rec.organization_code;
5275 		  x_avail_subinv_instance_tbl(j).inventory_item_id := l_instance_details_rec.inventory_item_id;
5276 		  x_avail_subinv_instance_tbl(j).inventory_org_id := l_instance_details_rec.inventory_org_id;
5277 		  x_avail_subinv_instance_tbl(j).serial_number := l_instance_details_rec.serial_number;
5278 		  x_avail_subinv_instance_tbl(j).revision := l_instance_details_rec.revision;
5279 		  x_avail_subinv_instance_tbl(j).lot_number := l_instance_details_rec.lot_number;
5280 		  x_avail_subinv_instance_tbl(j).uom_code := l_instance_details_rec.uom_code;
5281 		  x_avail_subinv_instance_tbl(j).quantity := l_instance_details_rec.quantity;
5282 		  x_avail_subinv_instance_tbl(j).install_date := l_instance_details_rec.install_date;
5283 		  x_avail_subinv_instance_tbl(j).mfg_date := l_instance_details_rec.mfg_date;
5284 		  -- Verify if location description return correct details else
5285 		  -- the logic in ahl_util_uc_pkg.getcsi_locationDesc need to be modified or
5286 		  -- a new cursor need to be opened to get correct description.
5287 		  x_avail_subinv_instance_tbl(j).location_description := l_instance_details_rec.location_description;
5288 		  x_avail_subinv_instance_tbl(j).party_type := l_instance_details_rec.party_type;
5289 		  x_avail_subinv_instance_tbl(j).owner_site_number := l_instance_details_rec.owner_site_number;
5290 		  x_avail_subinv_instance_tbl(j).owner_site_ID := l_instance_details_rec.owner_site_ID;
5291 		  x_avail_subinv_instance_tbl(j).owner_number := l_instance_details_rec.owner_number;
5292 		  x_avail_subinv_instance_tbl(j).owner_name := l_instance_details_rec.owner_name;
5293 		  x_avail_subinv_instance_tbl(j).owner_ID := l_instance_details_rec.owner_ID;
5294 		  x_avail_subinv_instance_tbl(j).csi_party_object_version_num := l_instance_details_rec.csi_party_object_version_num;
5295 		  x_avail_subinv_instance_tbl(j).status := l_instance_details_rec.status;
5296 		  x_avail_subinv_instance_tbl(j).condition := l_instance_details_rec.condition;
5297 		  x_avail_subinv_instance_tbl(j).wip_entity_name := l_instance_details_rec.wip_entity_name;
5298 		  x_avail_subinv_instance_tbl(j).uc_header_id := l_instance_details_rec.uc_header_id;
5299 		  x_avail_subinv_instance_tbl(j).uc_name := l_instance_details_rec.uc_name;
5300 		  --Modified on 02/26/2004 in case the status inconsistency occurred between an extra sub-unit and
5301 		  --its root unit
5302 
5303 		  IF l_instance_details_rec.root_uc_header_id IS NOT NULL THEN
5304                     -- SATHAPLI::Bug 9817209, 02-Jul-2010, changed the view AHL_UNIT_CONFIG_HEADERS_V usage to base tables
5305                     /*
5306 		    SELECT uc_status INTO l_status
5307 		      FROM ahl_unit_config_headers_v
5308 		     WHERE uc_header_id = l_instance_details_rec.root_uc_header_id;
5309                     */
5310                     SELECT FLV.meaning INTO l_status
5311                     FROM   AHL_UNIT_CONFIG_HEADERS AUCH, FND_LOOKUP_VALUES FLV
5312                     WHERE  AUCH.unit_config_header_id = l_instance_details_rec.root_uc_header_id AND
5313                            AUCH.unit_config_status_code = FLV.lookup_code AND
5314                            FLV.lookup_type  = 'AHL_CONFIG_STATUS' AND
5315                            FLV.language = USERENV('LANG');
5316 
5317 		    x_avail_subinv_instance_tbl(j).uc_status := l_status;
5318 		  ELSE
5319 		    x_avail_subinv_instance_tbl(j).uc_status := l_instance_details_rec.uc_status;
5320 		  END IF;
5321 		  x_avail_subinv_instance_tbl(j).mc_header_id := l_instance_details_rec.mc_header_id;
5322 		  x_avail_subinv_instance_tbl(j).mc_name := l_instance_details_rec.mc_name;
5323 		  x_avail_subinv_instance_tbl(j).mc_revision := l_instance_details_rec.mc_revision;
5324 		  x_avail_subinv_instance_tbl(j).mc_status := l_instance_details_rec.mc_status;
5325 		  x_avail_subinv_instance_tbl(j).position_ref := l_instance_details_rec.position_ref;
5326 		  --Get priority
5327 
5328 		  OPEN get_priority(l_item_assoc_id);
5329 		  FETCH get_priority INTO l_priority;
5330 		  CLOSE get_priority;
5331 		  x_avail_subinv_instance_tbl(j).priority := l_priority;
5332 		  --If the instance is an extra sibling node, then get its object version number in
5333 		  --table csi_ii_relationship
5334 		  OPEN get_csi_ii_relationship_ovn(x_avail_subinv_instance_tbl(j).csi_item_instance_id);
5335 		  FETCH get_csi_ii_relationship_ovn INTO l_csi_ii_relationship_ovn;
5336 		  IF get_csi_ii_relationship_ovn%FOUND THEN
5337 		    x_avail_subinv_instance_tbl(j).csi_ii_relationship_ovn := l_csi_ii_relationship_ovn;
5338 		  ELSE
5339 		    x_avail_subinv_instance_tbl(j).csi_ii_relationship_ovn := NULL;
5340 		  END IF;
5341 		  CLOSE get_csi_ii_relationship_ovn;
5342 
5343 		  x_avail_subinv_instance_tbl(j).subinventory_code :=  l_subinv_inst_rec.inv_subinventory_name;
5344 		  x_avail_subinv_instance_tbl(j).inventory_locator_id :=  l_subinv_inst_rec.inv_locator_id;
5345 
5346 		  OPEN c_get_locator_segments(l_subinv_inst_rec.inv_locator_id);
5347 		  FETCH c_get_locator_segments INTO x_avail_subinv_instance_tbl(j).locator_segments;
5348 		  CLOSE c_get_locator_segments;
5349 
5350 		END IF;
5351 		CLOSE ahl_instance_details;
5352 	      END IF;
5353 	    END IF;
5354 	    fnd_msg_pub.initialize;
5355    END LOOP;
5356 
5357   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5358 	FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5359 		     'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5360 			     'x_avail_subinv_instance_tbl count->'||x_avail_subinv_instance_tbl.COUNT);
5361   END IF;
5362 
5363   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5364 	FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
5365                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': End API',
5366 			       'After normal execution');
5367   END IF;
5368 
5369   -- Get all the error messages from the previous steps (if any) and raise the appropriate Exception
5370   l_msg_count := FND_MSG_PUB.count_msg;
5371   IF l_msg_count > 0 THEN
5372     x_msg_count := l_msg_count;
5373     RAISE FND_API.G_EXC_ERROR;
5374   END IF;
5375 
5376   -- Count and Get messages (optional)
5377   FND_MSG_PUB.count_and_get(
5378     p_encoded  => FND_API.G_FALSE,
5379     p_count    => x_msg_count,
5380     p_data     => x_msg_data);
5381 
5382 EXCEPTION
5383 
5384   WHEN FND_API.G_EXC_ERROR THEN
5385     x_return_status := FND_API.G_RET_STS_ERROR ;
5386     FND_MSG_PUB.count_and_get(
5387       p_encoded  => FND_API.G_FALSE,
5388       p_count    => x_msg_count,
5389       p_data     => x_msg_data);
5390 
5391   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5392     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5393     FND_MSG_PUB.count_and_get(
5394       p_encoded  => FND_API.G_FALSE,
5395       p_count    => x_msg_count,
5396       p_data     => x_msg_data);
5397 
5398   WHEN OTHERS THEN
5399     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
5400     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
5401     THEN
5402       FND_MSG_PUB.add_exc_msg(
5403         p_pkg_name         => G_PKG_NAME,
5404         p_procedure_name   => l_api_name,
5405         p_error_text       => SUBSTRB(SQLERRM,1,240));
5406     END IF;
5407     FND_MSG_PUB.count_and_get(
5408       p_encoded  => FND_API.G_FALSE,
5409       p_count    => x_msg_count,
5410       p_data     => x_msg_data);
5411 
5412 END Get_Avail_Subinv_Instances;
5413 
5414 -- Define procedure get_available_instances
5415 -- This API is used to get all the available instances for a given node in a UC tree.
5416 
5417 PROCEDURE get_available_instances(
5418   p_api_version            IN  NUMBER := 1.0,
5419   p_init_msg_list          IN  VARCHAR2 := FND_API.G_FALSE,
5420   --p_commit                 IN  VARCHAR2 := FND_API.G_FALSE,
5421   p_validation_level       IN  NUMBER := FND_API.G_VALID_LEVEL_FULL,
5422   x_return_status          OUT NOCOPY VARCHAR2,
5423   x_msg_count              OUT NOCOPY NUMBER,
5424   x_msg_data               OUT NOCOPY VARCHAR2,
5425   p_parent_instance_id     IN  NUMBER, --in order to include the extra siblings
5426   p_relationship_id        IN  NUMBER,
5427   p_item_number            IN  VARCHAR2 :='%',
5428   p_serial_number          IN  VARCHAR2 :='%',
5429   p_instance_number        IN  VARCHAR2 :='%',
5430   p_workorder_id           IN  NUMBER := NULL, --required by Part Changes
5431   p_start_row_index        IN  NUMBER,
5432   p_max_rows               IN  NUMBER,
5433   x_available_instance_tbl OUT NOCOPY available_instance_tbl_type,
5434   x_tbl_count              OUT NOCOPY NUMBER)
5435 IS
5436   l_api_name       CONSTANT   VARCHAR2(30) := 'get_available_instances';
5437   l_api_version    CONSTANT   NUMBER       := 1.0;
5438   l_return_status             VARCHAR2(1);
5439   l_msg_count                 NUMBER;
5440   l_relationship_id           NUMBER;
5441 
5442   -- SATHAPLI Bug# 4912576 fix
5443   -- Code pertaining to Org check has been removed from here
5444   -- Please refer earlier version for the details
5445 
5446   l_instance_id               NUMBER;
5447   l_inventory_item_id         NUMBER;
5448   l_instance_number           csi_item_instances.instance_number%TYPE := upper(nvl(p_instance_number, '%'));
5449   l_inventory_org_id          NUMBER;
5450   l_quantity                  NUMBER;
5451   l_inventory_revision        VARCHAR2(3);
5452   l_uom_code                  VARCHAR2(3);
5453   l_item_assoc_id             NUMBER;
5454   l_priority                  NUMBER;
5455   l_item_number               mtl_system_items_kfv.concatenated_segments%TYPE :=upper(nvl(p_item_number, '%'));
5456   l_serial_number             csi_item_instances.serial_number%TYPE := upper(nvl(p_serial_number, '%'));
5457   l_uc_header_id              NUMBER;
5458   i                           NUMBER;
5459   j                           NUMBER;
5460   l_dummy_char                VARCHAR2(1);
5461   l_top_uc_status             ahl_unit_config_headers.unit_config_status_code%TYPE;
5462   l_top_instance_id           NUMBER;
5463   l_status                    fnd_lookup_values_vl.meaning%TYPE;
5464   l_ignore_quant_vald         VARCHAR2(1);
5465 
5466   -- SATHAPLI Bug# 4912576 fix
5467   -- Code pertaining to Org check has been removed from here
5468   -- cursor get_instance1 is not used anymore
5469   -- Please refer earlier version for the details
5470 
5471   --Cursor to pick up all instances that match the item association setup. But units can only
5472   --be installed in MC leaf node which has associated sub-MC, in other words, any units
5473   --can't be installed in a branch node. Also need to include those sibling extra nodes which
5474   --are item-matched to the position
5475 
5476   CURSOR get_instance2(c_relationship_id NUMBER,
5477                        c_item_number VARCHAR2,
5478                        c_instance_number VARCHAR2,
5479                        c_serial_number VARCHAR2,
5480                        c_wip_job_id NUMBER,
5481                        c_parent_instance_id NUMBER,
5482                        c_uc_status VARCHAR2) IS
5483 
5484   -- SATHAPLI Bug# 4912576 fix::SQLid 14402108
5485   -- The query was changed to the following one for performance improvement.
5486   -- Please refer earlier version for previous query.
5487 SELECT
5488     C.instance_id,
5489     C.instance_number,
5490     C.inventory_item_id,
5491     C.inv_master_organization_id,
5492     C.quantity,
5493     C.inventory_revision,
5494     C.unit_of_measure uom_code,
5495     to_number(NULL) uc_header_id
5496 FROM csi_item_instances C,
5497     mtl_system_items_kfv M,
5498     ahl_mc_relationships R,
5499     ahl_item_associations_b A
5500 WHERE C.inventory_item_id = M.inventory_item_id
5501     AND C.inv_master_organization_id = M.organization_id
5502     AND R.item_group_id = A.item_group_id
5503     AND C.inventory_item_id = A.inventory_item_id
5504     AND R.relationship_id = c_relationship_id
5505     AND trunc(nvl(R.active_start_date, SYSDATE)) <= trunc(SYSDATE)
5506     AND trunc(nvl(R.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5507     AND trunc(nvl(C.active_start_date, SYSDATE)) <= trunc(SYSDATE)
5508     AND trunc(nvl(C.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5509     -- SATHAPLI::FP Bug 7498459, 27-Nov-2008 - For the root position, i.e. for UC header creation, make the inventory
5510     -- instances available, which are not issued to any job. This should be done only for the edit UC flow, i.e.
5511     -- when c_wip_job_id is NULL.
5512     -- AND C.location_type_code NOT IN ('PO','IN-TRANSIT','PROJECT','INVENTORY')
5513     AND C.location_type_code NOT IN ('PO','IN-TRANSIT','PROJECT', (DECODE(c_parent_instance_id, NULL, 'X', 'INVENTORY')))
5514     AND A.interchange_type_code IN ('1-WAY INTERCHANGEABLE', '2-WAY INTERCHANGEABLE')
5515     AND nvl(A.revision, nvl(C.inventory_revision,-1)) = nvl(C.inventory_revision,-1)
5516     AND (
5517         (c_wip_job_id IS NULL -- SATHAPLI, 30-Jan-2008 :: extra nodes should not come up during parts change
5518          AND
5519          EXISTS
5520             (
5521             SELECT 1
5522             FROM csi_ii_relationships i2
5523             WHERE i2.subject_id = C.instance_id
5524                 AND i2.position_reference IS NULL --because parent is not extra
5525                 -- SATHAPLI::FP ER 6504147, 18-Nov-2008
5526                 -- include extra nodes of all the parents uptill root
5527                 -- AND i2.object_id = NVL(c_parent_instance_id, -1)
5528                 AND i2.object_id IN (
5529                                      SELECT i3.object_id
5530                                      FROM   csi_ii_relationships i3
5531                                      START WITH i3.subject_id = nvl(c_parent_instance_id, -1)
5532                                      AND    i3.relationship_type_code = 'COMPONENT-OF'
5533                                      AND    trunc(nvl(i3.active_start_date, SYSDATE)) <= trunc(SYSDATE)
5534                                      AND    trunc(nvl(i3.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5535                                      CONNECT BY i3.subject_id = PRIOR i3.object_id
5536                                      AND    i3.relationship_type_code = 'COMPONENT-OF'
5537                                      AND    trunc(nvl(i3.active_start_date, SYSDATE)) <= trunc(SYSDATE)
5538                                      AND    trunc(nvl(i3.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5539                                      UNION ALL
5540                                      SELECT nvl(c_parent_instance_id, -1)
5541                                      FROM   DUAL
5542                                     )
5543                 AND i2.relationship_type_code = 'COMPONENT-OF'
5544                 AND trunc(nvl(i2.active_start_date, SYSDATE)) <= trunc(SYSDATE)
5545                 AND trunc(nvl(i2.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5546             )
5547         )
5548         OR
5549         (NOT EXISTS
5550             (
5551             SELECT 1
5552             FROM csi_ii_relationships i1
5553             WHERE i1.subject_id = C.instance_id
5554                 AND i1.relationship_type_code = 'COMPONENT-OF'
5555                 AND trunc(nvl(i1.active_start_date, SYSDATE)) <= trunc(SYSDATE)
5556                 AND trunc(nvl(i1.active_end_date, SYSDATE+1)) >trunc(SYSDATE)
5557             )
5558             -- SATHAPLI, 30-Jan-2008 :: If c_wip_job_id is not passed, instances issued to any job should not be fetched.
5559             -- If c_wip_job_id is passed, then fetch only those instances which are issued to this job.
5560             -- AND nvl(C.wip_job_id, -1) = nvl(c_wip_job_id, nvl(C.wip_job_id, -1))
5561             AND ((c_wip_job_id IS NULL AND C.wip_job_id IS NULL)
5562                  OR
5563                  (c_wip_job_id IS NOT NULL AND c_wip_job_id = NVL(C.wip_job_id, -1))
5564                 )
5565         )
5566     )
5567     --This wip_entity check is not necessary for an extra sibling nodes even for
5568     --so just include it here.
5569     AND NOT EXISTS
5570     (
5571     SELECT 1
5572     FROM ahl_unit_config_headers H
5573     WHERE H.csi_item_instance_id = C.instance_id
5574         AND trunc(nvl(H.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5575     )
5576     AND upper(M.concatenated_segments) LIKE c_item_number
5577     AND upper(C.instance_number) LIKE c_instance_number
5578     AND upper(nvl(C.serial_number, '%')) LIKE c_serial_number
5579     AND EXISTS
5580     (
5581      SELECT 'X'
5582      FROM   mtl_parameters mp, inv_organization_info_v io
5583      WHERE  mp.master_organization_id = M.organization_id AND
5584             mp.organization_Id = io.organization_id AND
5585             NVL(io.operating_unit, mo_global.get_current_org_id()) =
5586             mo_global.get_current_org_id()
5587     )
5588     --Plus those units could be installed
5589 UNION ALL
5590 SELECT
5591     C.instance_id,
5592     C.instance_number,
5593     C.inventory_item_id,
5594     C.inv_master_organization_id,
5595     C.quantity,
5596     C.inventory_revision,
5597     C.unit_of_measure uom_code,
5598     U.uc_header_id uc_header_id
5599 FROM (
5600       SELECT UH.unit_config_header_id uc_header_id,
5601              UH.csi_item_instance_id csi_instance_id,
5602              UH.master_config_id mc_header_id,
5603              UH.unit_config_status_code uc_status_code,
5604              UH.active_end_date,
5605              CR.object_id parent_instance_id
5606       FROM   ahl_unit_config_headers UH, csi_ii_relationships CR
5607       WHERE  UH.csi_item_instance_id = CR.subject_id (+) AND
5608              CR.relationship_type_code (+) = 'COMPONENT-OF' AND
5609              trunc(nvl(CR.active_start_date (+), SYSDATE)) <= trunc(SYSDATE) AND
5610              trunc(nvl(CR.active_end_date (+), SYSDATE+1)) > trunc(SYSDATE)
5611      ) U,
5612     csi_item_instances C,
5613     mtl_system_items_kfv M
5614 WHERE U.csi_instance_id = C.instance_id
5615     AND C.inventory_item_id = M.inventory_item_id
5616     AND C.inv_master_organization_id = M.organization_id
5617     -- SATHAPLI::Bug 9022080, 05-Nov-2009, filter out sub UCs in INVENTORY, i.e. with root instance in INVENTORY
5618     AND C.location_type_code NOT IN ('INVENTORY')
5619     AND trunc(nvl(U.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5620     AND trunc(nvl(C.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5621     --Per Barry, if the top instance is expired then this UC is also taken as
5622     --Either JOIN or IN, performance cost is bigger than EXISTS
5623     AND EXISTS
5624     (
5625     SELECT 1
5626     FROM ahl_mc_config_relations R
5627     WHERE R.mc_header_id = U.mc_header_id
5628         AND R.relationship_id = c_relationship_id
5629         AND trunc(nvl(R.active_start_date, SYSDATE)) <= trunc(SYSDATE)
5630         AND trunc(nvl(R.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5631     )
5632     --Either a separate unit or extra sibling subunit
5633     AND (
5634         (
5635             U.parent_instance_id IS NULL
5636             AND decode(U.uc_status_code, 'DRAFT', 'DRAFT',
5637                        'APPROVAL_REJECTED', 'DRAFT',
5638                        'COMPLETE','COMPLETE',
5639                        'INCOMPLETE','COMPLETE', NULL) = c_uc_status
5640             -- SATHAPLI, 30-Jan-2008 :: If c_wip_job_id is not passed, instances issued to any job should not be fetched.
5641             -- If c_wip_job_id is passed, then fetch only those instances which are issued to this job.
5642             -- AND nvl(C.wip_job_id, -1) = nvl(c_wip_job_id, nvl(C.wip_job_id, -1))
5643             AND ((c_wip_job_id IS NULL AND C.wip_job_id IS NULL)
5644                  OR
5645                  (c_wip_job_id IS NOT NULL AND c_wip_job_id = NVL(C.wip_job_id, -1))
5646                 )
5647         )
5648         --This wip_entity check is not necessary for an extra sibling nodes even
5649         --so just include it here.
5650         OR
5651         (
5652             c_wip_job_id IS NULL -- SATHAPLI::ER# 6504147 :: extra nodes should not come up during parts change
5653             AND
5654             -- SATHAPLI::FP ER 6504147, 18-Nov-2008
5655             -- include extra nodes of all the parents uptill root
5656             -- U.parent_instance_id = nvl(c_parent_instance_id, -1)
5657             U.parent_instance_id IN (
5658                                      SELECT i3.object_id
5659                                      FROM   csi_ii_relationships i3
5660                                      START WITH i3.subject_id = nvl(c_parent_instance_id, -1)
5661                                      AND    i3.relationship_type_code = 'COMPONENT-OF'
5662                                      AND    trunc(nvl(i3.active_start_date, SYSDATE)) <= trunc(SYSDATE)
5663                                      AND    trunc(nvl(i3.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5664                                      CONNECT BY i3.subject_id = PRIOR i3.object_id
5665                                      AND    i3.relationship_type_code = 'COMPONENT-OF'
5666                                      AND    trunc(nvl(i3.active_start_date, SYSDATE)) <= trunc(SYSDATE)
5667                                      AND    trunc(nvl(i3.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5668                                      UNION ALL
5669                                      SELECT nvl(c_parent_instance_id, -1)
5670                                      FROM   DUAL
5671                                     )
5672             AND EXISTS
5673             (SELECT 1
5674             FROM csi_ii_relationships CI
5675             WHERE CI.object_id = U.parent_instance_id
5676                 AND CI.subject_id = U.csi_instance_id
5677                 AND CI.position_reference IS NULL
5678                 AND CI.relationship_type_code = 'COMPONENT-OF'
5679                 AND trunc(nvl(CI.active_start_date,SYSDATE)) <= trunc(SYSDATE)
5680                 AND trunc(nvl(CI.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5681             )
5682         )
5683     )
5684     AND NOT EXISTS
5685     (SELECT 1
5686     FROM ahl_mc_relationships MR
5687     WHERE MR.parent_relationship_id = c_relationship_id
5688         AND trunc(nvl(MR.active_start_date, SYSDATE)) <= trunc(SYSDATE)
5689         AND trunc(nvl(MR.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5690     )
5691     AND upper(M.concatenated_segments) LIKE c_item_number
5692     AND upper(C.instance_number) LIKE c_instance_number
5693     AND upper(nvl(C.serial_number, '%')) LIKE c_serial_number
5694     AND EXISTS
5695     (
5696      SELECT 'X'
5697      FROM   mtl_parameters mp, inv_organization_info_v io
5698      WHERE  mp.master_organization_id = M.organization_id AND
5699             mp.organization_Id = io.organization_id AND
5700             NVL(io.operating_unit, mo_global.get_current_org_id()) =
5701             mo_global.get_current_org_id()
5702     )
5703     AND ahl_util_uc_pkg.IS_UNIT_QUARANTINED(U.uc_header_id , null) =
5704         FND_API.G_FALSE
5705 ORDER BY 2;
5706 
5707   CURSOR check_relationship_id IS
5708     SELECT relationship_id
5709       FROM ahl_mc_relationships
5710      WHERE relationship_id = p_relationship_id
5711        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
5712        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5713        AND mc_header_id IN (SELECT mc_header_id
5714                               FROM ahl_mc_headers_b
5715                              WHERE config_status_code = 'COMPLETE');
5716 
5717   CURSOR get_priority (c_item_association_id IN NUMBER) IS
5718     SELECT priority
5719     FROM ahl_item_associations_b
5720     WHERE item_association_id = c_item_association_id;
5721 
5722   -- SATHAPLI Bug# 4912576 fix::SQLid 14402138
5723   -- The cursor definition below is changed so that reference
5724   -- to view ahl_unit_config_headers_v is changed to base tables.
5725   -- Please refer earlier version for previous definition.
5726   CURSOR ahl_instance_details (c_csi_item_instance_id IN NUMBER) IS
5727 select a.csi_item_instance_id,
5728        a.csi_object_version csi_object_version_number,
5729        a.item_number,
5730        a.item_description,
5731        a.csi_instance_number,
5732        a.inventory_item_id,
5733        a.inventory_org_id,
5734        a.organization_code,
5735        a.serial_number,
5736        a.revision,
5737        a.lot_number,
5738        a.uom_code,
5739        a.quantity,
5740        a.install_date,
5741        a.mfg_date,
5742        a.location_description,
5743        a.party_type,
5744        a.owner_id,
5745        a.owner_number,
5746        a.owner_name,
5747        a.csi_location_id owner_site_id,
5748        a.owner_site_number,
5749        a.csi_party_object_version_num,
5750        a.status,
5751        a.condition,
5752        a.wip_entity_name,
5753        b.uc_header_id,
5754        b.uc_name,
5755        b.uc_status,
5756        b.mc_header_id,
5757        b.mc_name,
5758        b.mc_revision,
5759        b.mc_status,
5760        b.position_ref,
5761        b.root_uc_header_id
5762  from  ahl_unit_installed_details_v a,
5763        (
5764         SELECT U.unit_config_header_id uc_header_id,
5765                U.name uc_name,
5766                UCSC.meaning uc_status,
5767                U.master_config_id mc_header_id,
5768                M.name mc_name,
5769                M.revision mc_revision,
5770                MCSC.meaning mc_status,
5771                MRSC.meaning position_ref,
5772                (
5773                 SELECT unit_config_header_id
5774                 FROM   ahl_unit_config_headers
5775                 WHERE  parent_uc_header_id IS NULL
5776                 START WITH
5777                        unit_config_header_id = U.unit_config_header_id
5778                 CONNECT BY
5779                        unit_config_header_id = PRIOR parent_uc_header_id
5780                ) root_uc_header_id,
5781                U.csi_item_instance_id csi_instance_id,
5782                U.active_end_date active_end_date
5783         FROM   AHL_UNIT_CONFIG_HEADERS U, AHL_MC_HEADERS_B M,
5784                AHL_MC_RELATIONSHIPS R, FND_LOOKUP_VALUES UCSC,
5785                FND_LOOKUP_VALUES MRSC, FND_LOOKUP_VALUES MCSC
5786         WHERE  U.master_config_id = M.mc_header_id AND
5787                M.mc_header_id = R.mc_header_id AND
5788                R.parent_relationship_id IS NULL AND
5789                U.unit_config_status_code = UCSC.lookup_code AND
5790                'AHL_CONFIG_STATUS' = UCSC.lookup_type AND
5791 	       UCSC.language = USERENV('LANG') AND
5792                M.config_status_code = MCSC.lookup_code AND
5793                'AHL_CONFIG_STATUS' = MCSC.lookup_type AND
5794 	       MCSC.language = USERENV('LANG') AND
5795                R.position_ref_code = MRSC.lookup_code AND
5796                'AHL_POSITION_REFERENCE' = MRSC.lookup_type AND
5797 	       MRSC.language = USERENV('LANG')
5798        ) b
5799 where  a.csi_item_instance_id = c_csi_item_instance_id
5800   and  a.csi_item_instance_id = b.csi_instance_id (+)
5801   and  trunc(nvl(b.active_end_date (+), sysdate+1)) > trunc(sysdate);
5802 
5803   l_instance_details_rec ahl_instance_details%ROWTYPE;
5804 
5805   CURSOR get_wip_entity_id (c_workorder_id NUMBER) IS
5806     SELECT wip_entity_id
5807       FROM ahl_workorders
5808      WHERE workorder_id = c_workorder_id;
5809   l_wip_entity_id   NUMBER;
5810 
5811   CURSOR check_parent_instance(c_instance_id NUMBER) IS
5812   --Parent instance could be either in ahl_unit_config_headers(top node) or in csi_ii_relationships
5813   --(as the subject_id)
5814     SELECT 'x'
5815       FROM ahl_unit_config_headers
5816      WHERE csi_item_instance_id = c_instance_id
5817        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5818      UNION ALL
5819     SELECT 'x'
5820       FROM csi_ii_relationships
5821      WHERE subject_id = c_instance_id
5822        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5823        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE);
5824 
5825   CURSOR get_top_unit_instance(c_instance_id NUMBER) IS
5826 
5827   -- SATHAPLI::Bug#4912576 fix::SQL ID 14402149 --
5828   /*
5829     SELECT object_id
5830       FROM csi_ii_relationships
5831      WHERE object_id NOT IN (SELECT subject_id
5832                                FROM csi_ii_relationships
5833                               WHERE relationship_type_code = 'COMPONENT-OF'
5834                                 AND trunc(nvl(active_start_date,SYSDATE)) <= trunc(SYSDATE)
5835                                 AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE))
5836 START WITH subject_id = c_instance_id
5837        AND relationship_type_code = 'COMPONENT-OF'
5838        AND trunc(nvl(active_start_date,SYSDATE)) <= trunc(SYSDATE)
5839        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5840 CONNECT BY subject_id = PRIOR object_id
5841        AND relationship_type_code = 'COMPONENT-OF'
5842        AND trunc(nvl(active_start_date,SYSDATE)) <= trunc(SYSDATE)
5843        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
5844   */
5845     SELECT object_id
5846     FROM   csi_ii_relationships co
5847     WHERE  NOT EXISTS
5848            (
5849             SELECT 'X'
5850             FROM   csi_ii_relationships ci
5851             WHERE  ci.relationship_type_code = 'COMPONENT-OF' AND
5852                    ci.subject_id = co.object_id AND
5853                    trunc(nvl(ci.active_start_date,SYSDATE)) <= trunc(SYSDATE) AND
5854                    trunc(nvl(ci.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5855            )
5856            START WITH co.subject_id = c_instance_id AND
5857            co.relationship_type_code = 'COMPONENT-OF' AND
5858            trunc(nvl(co.active_start_date,SYSDATE)) <= trunc(SYSDATE) AND
5859            trunc(nvl(co.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
5860            CONNECT BY co.subject_id = PRIOR co.object_id AND
5861            co.relationship_type_code = 'COMPONENT-OF' AND
5862            trunc(nvl(co.active_start_date,SYSDATE)) <= trunc(SYSDATE) AND
5863            trunc(nvl(co.active_end_date, SYSDATE+1)) > trunc(SYSDATE);
5864 
5865   CURSOR get_uc_status(c_instance_id NUMBER) IS
5866     SELECT unit_config_status_code
5867       FROM ahl_unit_config_headers
5868      WHERE csi_item_instance_id = c_instance_id
5869        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
5870 
5871   CURSOR get_csi_ii_relationship_ovn (c_instance_id NUMBER) IS
5872     SELECT object_version_number
5873       FROM csi_ii_relationships
5874      WHERE subject_id = c_instance_id
5875        AND position_reference IS NULL
5876        AND relationship_type_code = 'COMPONENT-OF'
5877        AND trunc(nvl(active_start_date,SYSDATE)) <= trunc(SYSDATE)
5878        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
5879   l_csi_ii_relationship_ovn   NUMBER;
5880 
5881 BEGIN
5882   -- Initialize API return status to success
5883   x_return_status := FND_API.G_RET_STS_SUCCESS;
5884   -- Standard call to check for call compatibility.
5885   IF NOT FND_API.compatible_api_call(
5886     l_api_version,
5887     p_api_version,
5888     l_api_name,
5889     G_PKG_NAME)
5890   THEN
5891     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5892   END IF;
5893 
5894   -- Initialize message list if p_init_msg_list is set to TRUE.
5895   IF FND_API.to_boolean( p_init_msg_list ) THEN
5896     FND_MSG_PUB.initialize;
5897   END IF;
5898 
5899   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5900     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
5901                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Begin API',
5902                    'At the start of the procedure');
5903   END IF;
5904 
5905   OPEN check_relationship_id;
5906   FETCH check_relationship_id INTO l_relationship_id;
5907   IF check_relationship_id%NOTFOUND THEN
5908     FND_MESSAGE.set_name( 'AHL','AHL_UC_POSITION_INVALID' );
5909     FND_MESSAGE.set_token('POSITION', p_relationship_id);
5910     FND_MSG_PUB.add;
5911     CLOSE check_relationship_id;
5912     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5913   ELSE
5914     CLOSE check_relationship_id;
5915   END IF;
5916 
5917   --Get wip_entity_id from p_workorder_id
5918   IF p_workorder_id IS NOT NULL THEN
5919     OPEN get_wip_entity_id(p_workorder_id);
5920     FETCH get_wip_entity_id INTO l_wip_entity_id;
5921     IF get_wip_entity_id%NOTFOUND THEN
5922       FND_MESSAGE.set_name( 'AHL','AHL_UC_WORKORDER_INVALID' );
5923       FND_MESSAGE.set_token('WORKORDER', p_workorder_id);
5924       FND_MSG_PUB.add;
5925       CLOSE get_wip_entity_id;
5926       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5927     ELSE
5928       CLOSE get_wip_entity_id;
5929     END IF;
5930   ELSE
5931     l_wip_entity_id := NULL;
5932   END IF;
5933 
5934   --Validate p_parent_instance_id is Null(for top node) or existing in
5935   --ahl_unit_config_headers or csi_ii_relationships(for non-top node)
5936   IF p_parent_instance_id IS NOT NULL THEN
5937     OPEN check_parent_instance(p_parent_instance_id);
5938     FETCH check_parent_instance INTO l_dummy_char;
5939     IF check_parent_instance%NOTFOUND THEN
5940       FND_MESSAGE.set_name( 'AHL','AHL_UC_PARENT_INST_INVALID' );
5941       FND_MESSAGE.set_token('INSTANCE', p_parent_instance_id);
5942       FND_MSG_PUB.add;
5943       CLOSE check_parent_instance;
5944       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5945     ELSE
5946       CLOSE check_parent_instance;
5947     END IF;
5948   END IF;
5949 
5950   --Get the instance's ancestor unit's status, just in order to meet the regulation that
5951   --Draft unit can only be installed into Draft unit and Complete unit can only be installed
5952   --into Complete unit.
5953   IF p_parent_instance_id IS NOT NULL THEN --Not top position
5954     OPEN get_top_unit_instance(p_parent_instance_id);
5955     FETCH get_top_unit_instance INTO l_top_instance_id;
5956     IF get_top_unit_instance%NOTFOUND THEN --Parent_instance_id happens to be top node
5957       l_top_instance_id := p_parent_instance_id;
5958     END IF;
5959     CLOSE get_top_unit_instance;
5960 
5961     OPEN get_uc_status(l_top_instance_id);
5962     FETCH get_uc_status INTO l_top_uc_status;
5963     IF get_uc_status%NOTFOUND THEN
5964       FND_MESSAGE.set_name( 'AHL','AHL_UC_INSTANCE_NOT_IN_UC' );
5965       FND_MESSAGE.set_token('INSTANCE', l_top_instance_id);
5966       FND_MSG_PUB.add;
5967       CLOSE get_uc_status;
5968       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5969     ELSE
5970       CLOSE get_uc_status;
5971     END IF;
5972   ELSE
5973     l_top_uc_status := NULL; --Doesn't matter wether what it is, because no existing units will
5974   END IF;                    --be available for this top position
5975 
5976   SELECT decode(l_top_uc_status, 'DRAFT', 'DRAFT',
5977                      'APPROVAL_REJECTED', 'DRAFT',
5978                             'COMPLETE','COMPLETE',
5979                     'INCOMPLETE','COMPLETE', NULL) INTO l_top_uc_status
5980   FROM dual;
5981 
5982   --Based on profile value open the cursor.
5983   -- SATHAPLI Bug# 4912576 fix
5984   -- Code pertaining to Org check has been removed from here
5985   -- Please refer earlier version for the details
5986 
5987     OPEN get_instance2 (p_relationship_id,
5988                         l_item_number,
5989                         l_instance_number,
5990                         l_serial_number,
5991                         l_wip_entity_id,
5992                         p_parent_instance_id,
5993                         l_top_uc_status);
5994 
5995     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5996         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5997                        'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
5998                        ' ;p_relationship_id = '||p_relationship_id||' ;l_item_number = '||l_item_number||
5999                        ' ;l_instance_number = '||l_instance_number||' ;l_serial_number = '||l_serial_number||
6000                        ' ;l_wip_entity_id = '||l_wip_entity_id||' ;p_parent_instance_id = '||p_parent_instance_id||
6001                        ' ;l_top_uc_status = '||l_top_uc_status);
6002     END IF;
6003 
6004   i := 0;
6005   j := 0;
6006   -- row count.
6007   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6008     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
6009                      'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
6010                      'part_number='||l_item_number||'instance_number='||l_instance_number||
6011                      'serial_number='||l_serial_number||'wip_entity_id='||l_wip_entity_id||
6012                      'p_parent_instance_id='||p_parent_instance_id);
6013   END IF;
6014 
6015   LOOP
6016 
6017   -- SATHAPLI Bug# 4912576 fix
6018   -- Code pertaining to Org check has been removed from here
6019   -- Please refer earlier version for the details
6020 
6021       FETCH get_instance2 INTO l_instance_id, l_instance_number, l_inventory_item_id, l_inventory_org_id,
6022                                l_quantity, l_inventory_revision, l_uom_code, l_uc_header_id;
6023       EXIT WHEN get_instance2%NOTFOUND;
6024 
6025     --If the instance is not a unit then, call procedure to validate whether thecorresponding inventory
6026     --can be assigned to that position
6027     IF l_uc_header_id IS NULL THEN
6028       -- SATHAPLI::FP OGMA Issue# 105 - Non-Serialized Item Maintenance, 05-Dec-2007
6029       -- If p_workorder_id is not NULL, then the call is from Production. So pass 'Y' for p_ignore_quant_vald.
6030       -- Else, pass 'N' for p_ignore_quant_vald.
6031       IF(p_workorder_id IS NOT NULL) THEN
6032         l_ignore_quant_vald := 'Y';
6033       ELSE
6034         l_ignore_quant_vald := 'N';
6035       END IF;
6036 
6037       AHL_UTIL_UC_PKG.validate_for_position(p_mc_relationship_id   => p_relationship_id,
6038                                             p_inventory_id         => l_inventory_item_id,
6039                                             p_organization_id      => l_inventory_Org_id,
6040                                             p_quantity             => l_quantity,
6041                                             p_revision             => l_inventory_revision,
6042                                             p_uom_code             => l_uom_code,
6043                                             p_position_ref_meaning => NULL,
6044                                             p_ignore_quant_vald    => l_ignore_quant_vald,
6045                                             x_item_assoc_id        => l_item_assoc_id);
6046     END IF;
6047 
6048     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6049       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
6050                      'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': Within API',
6051                      'After position validation');
6052     END IF;
6053 
6054     IF (fnd_msg_pub.count_msg = 0) THEN
6055       i := i + 1;
6056       IF (i >= p_start_row_index AND i < p_start_row_index + p_max_rows) THEN
6057         OPEN ahl_instance_details(l_instance_id);
6058         FETCH ahl_instance_details INTO l_instance_details_rec;
6059         IF (ahl_instance_details%FOUND) THEN
6060           j := j + 1;
6061           x_available_instance_tbl(j).csi_item_instance_id := l_instance_details_rec.csi_item_instance_id;
6062           x_available_instance_tbl(j).csi_object_version_number := l_instance_details_rec.csi_object_version_number;
6063           x_available_instance_tbl(j).item_number := l_instance_details_rec.item_number;
6064           x_available_instance_tbl(j).item_description := l_instance_details_rec.item_description;
6065           x_available_instance_tbl(j).csi_instance_number := l_instance_details_rec.csi_instance_number;
6066           x_available_instance_tbl(j).organization_code := l_instance_details_rec.organization_code;
6067           x_available_instance_tbl(j).inventory_item_id := l_instance_details_rec.inventory_item_id;
6068           x_available_instance_tbl(j).inventory_org_id := l_instance_details_rec.inventory_org_id;
6069           x_available_instance_tbl(j).serial_number := l_instance_details_rec.serial_number;
6070           x_available_instance_tbl(j).revision := l_instance_details_rec.revision;
6071           x_available_instance_tbl(j).lot_number := l_instance_details_rec.lot_number;
6072           x_available_instance_tbl(j).uom_code := l_instance_details_rec.uom_code;
6073           x_available_instance_tbl(j).quantity := l_instance_details_rec.quantity;
6074           x_available_instance_tbl(j).install_date := l_instance_details_rec.install_date;
6075           x_available_instance_tbl(j).mfg_date := l_instance_details_rec.mfg_date;
6076           x_available_instance_tbl(j).location_description := l_instance_details_rec.location_description;
6077           x_available_instance_tbl(j).party_type := l_instance_details_rec.party_type;
6078           x_available_instance_tbl(j).owner_site_number := l_instance_details_rec.owner_site_number;
6079           x_available_instance_tbl(j).owner_site_ID := l_instance_details_rec.owner_site_ID;
6080           x_available_instance_tbl(j).owner_number := l_instance_details_rec.owner_number;
6081           x_available_instance_tbl(j).owner_name := l_instance_details_rec.owner_name;
6082           x_available_instance_tbl(j).owner_ID := l_instance_details_rec.owner_ID;
6083           x_available_instance_tbl(j).csi_party_object_version_num := l_instance_details_rec.csi_party_object_version_num;
6084           x_available_instance_tbl(j).status := l_instance_details_rec.status;
6085           x_available_instance_tbl(j).condition := l_instance_details_rec.condition;
6086           x_available_instance_tbl(j).wip_entity_name := l_instance_details_rec.wip_entity_name;
6087           x_available_instance_tbl(j).uc_header_id := l_instance_details_rec.uc_header_id;
6088           x_available_instance_tbl(j).uc_name := l_instance_details_rec.uc_name;
6089           --Modified on 02/26/2004 in case the status inconsistency occurred between an extra sub-unit and
6090           --its root unit
6091           IF l_instance_details_rec.root_uc_header_id IS NOT NULL THEN
6092 
6093             -- SATHAPLI::Bug#4912576 fix::SQL ID 14402160 --
6094 	    /*
6095 	    SELECT uc_status INTO l_status
6096               FROM ahl_unit_config_headers_v
6097              WHERE uc_header_id = l_instance_details_rec.root_uc_header_id;
6098             */
6099             SELECT FLV.meaning INTO l_status
6100             FROM   AHL_UNIT_CONFIG_HEADERS AUCH, FND_LOOKUP_VALUES FLV
6101             WHERE  AUCH.unit_config_header_id = l_instance_details_rec.root_uc_header_id AND
6102                    AUCH.unit_config_status_code = FLV.lookup_code AND
6103                    FLV.lookup_type  = 'AHL_CONFIG_STATUS' AND
6104                    FLV.language = USERENV('LANG');
6105 
6106             x_available_instance_tbl(j).uc_status := l_status;
6107           ELSE
6108             x_available_instance_tbl(j).uc_status := l_instance_details_rec.uc_status;
6109           END IF;
6110           x_available_instance_tbl(j).mc_header_id := l_instance_details_rec.mc_header_id;
6111           x_available_instance_tbl(j).mc_name := l_instance_details_rec.mc_name;
6112           x_available_instance_tbl(j).mc_revision := l_instance_details_rec.mc_revision;
6113           x_available_instance_tbl(j).mc_status := l_instance_details_rec.mc_status;
6114           x_available_instance_tbl(j).position_ref := l_instance_details_rec.position_ref;
6115           --Get priority
6116           OPEN get_priority(l_item_assoc_id);
6117           FETCH get_priority INTO l_priority;
6118           CLOSE get_priority;
6119           x_available_instance_tbl(j).priority := l_priority;
6120           --If the instance is an extra sibling node, then get its object version number in
6121           --table csi_ii_relationship
6122           OPEN get_csi_ii_relationship_ovn(x_available_instance_tbl(j).csi_item_instance_id);
6123           FETCH get_csi_ii_relationship_ovn INTO l_csi_ii_relationship_ovn;
6124           IF get_csi_ii_relationship_ovn%FOUND THEN
6125             x_available_instance_tbl(j).csi_ii_relationship_ovn := l_csi_ii_relationship_ovn;
6126           ELSE
6127             x_available_instance_tbl(j).csi_ii_relationship_ovn := NULL;
6128           END IF;
6129           CLOSE get_csi_ii_relationship_ovn;
6130         END IF;
6131         CLOSE ahl_instance_details;
6132       END IF;
6133     END IF;
6134     fnd_msg_pub.initialize;
6135   END LOOP;
6136   x_tbl_count := i;
6137 
6138   -- SATHAPLI Bug# 4912576 fix
6139   -- Code pertaining to Org check has been removed from here
6140   -- Please refer earlier version for the details
6141 
6142   IF (get_instance2%ISOPEN) THEN
6143     CLOSE get_instance2;
6144   END IF;
6145 
6146   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6147     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
6148                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||': End API',
6149                    'After normal execution');
6150   END IF;
6151 
6152   -- Get all the error messages from the previous steps (if any) and raise the appropriate Exception
6153   l_msg_count := FND_MSG_PUB.count_msg;
6154   IF l_msg_count > 0 THEN
6155     x_msg_count := l_msg_count;
6156     RAISE FND_API.G_EXC_ERROR;
6157   END IF;
6158   -- Count and Get messages (optional)
6159   FND_MSG_PUB.count_and_get(
6160     p_encoded  => FND_API.G_FALSE,
6161     p_count    => x_msg_count,
6162     p_data     => x_msg_data);
6163 EXCEPTION
6164   WHEN FND_API.G_EXC_ERROR THEN
6165     x_return_status := FND_API.G_RET_STS_ERROR ;
6166     FND_MSG_PUB.count_and_get(
6167       p_encoded  => FND_API.G_FALSE,
6168       p_count    => x_msg_count,
6169       p_data     => x_msg_data);
6170   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6171     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6172     FND_MSG_PUB.count_and_get(
6173       p_encoded  => FND_API.G_FALSE,
6174       p_count    => x_msg_count,
6175       p_data     => x_msg_data);
6176   WHEN OTHERS THEN
6177     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
6178     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
6179     THEN
6180       FND_MSG_PUB.add_exc_msg(
6181         p_pkg_name         => G_PKG_NAME,
6182         p_procedure_name   => l_api_name,
6183         p_error_text       => SUBSTRB(SQLERRM,1,240));
6184     END IF;
6185     FND_MSG_PUB.count_and_get(
6186       p_encoded  => FND_API.G_FALSE,
6187       p_count    => x_msg_count,
6188       p_data     => x_msg_data);
6189 END get_available_instances;
6190 
6191 -- SATHAPLI::FP ER 6504147, 18-Nov-2008
6192 -- Define procedure create_unassigned_instance.
6193 -- This API is used to create a new instance in csi_item_instances and assign it
6194 -- to the UC root node as extra node.
6195 
6196 PROCEDURE create_unassigned_instance(
6197     p_api_version           IN            NUMBER   := 1.0,
6198     p_init_msg_list         IN            VARCHAR2 := FND_API.G_FALSE,
6199     p_commit                IN            VARCHAR2 := FND_API.G_FALSE,
6200     p_validation_level      IN            NUMBER   := FND_API.G_VALID_LEVEL_FULL,
6201     x_return_status         OUT    NOCOPY VARCHAR2,
6202     x_msg_count             OUT    NOCOPY NUMBER,
6203     x_msg_data              OUT    NOCOPY VARCHAR2,
6204     p_uc_header_id          IN            NUMBER,
6205     p_x_uc_instance_rec     IN OUT NOCOPY uc_instance_rec_type
6206 )
6207 IS
6208 
6209 CURSOR check_uc_header_csr (p_uc_header_id NUMBER) IS
6210     SELECT unit_config_header_id,
6211            unit_config_status_code,
6212            active_uc_status_code,
6213            csi_item_instance_id,
6214            parent_uc_header_id
6215     FROM   ahl_unit_config_headers
6216     WHERE  unit_config_header_id  = p_uc_header_id
6217     AND    trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
6218 
6219 CURSOR csi_item_instance_csr(p_csi_instance_id NUMBER) IS
6220     SELECT location_id,
6221            location_type_code,
6222            party_id,
6223            party_source_table,
6224            instance_party_id,
6225            csi.wip_job_id
6226     FROM   csi_item_instances csi, csi_i_parties p
6227     WHERE  csi.instance_id          = p.instance_id
6228     AND    p.relationship_type_code = 'OWNER'
6229     AND    csi.instance_id          = p_csi_instance_id
6230     AND    trunc(nvl(csi.active_end_date, SYSDATE+1)) > trunc(SYSDATE);
6231 
6232 CURSOR csi_ip_accounts_csr(p_instance_party_id NUMBER) IS
6233     SELECT party_account_id
6234     FROM   csi_ip_accounts
6235     WHERE  relationship_type_code = 'OWNER'
6236     AND    instance_party_id      = p_instance_party_id
6237     AND    trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
6238     AND    trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
6239 --
6240     l_api_version    CONSTANT   NUMBER       := 1.0;
6241     l_api_name       CONSTANT   VARCHAR2(30) := 'create_unassigned_instance';
6242     l_full_name      CONSTANT   VARCHAR2(70) := 'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name;
6243 
6244     l_check_uc_header_rec       check_uc_header_csr%ROWTYPE;
6245     l_uc_owner_loc_rec          csi_item_instance_csr%ROWTYPE;
6246     l_msg_count_bef             NUMBER;
6247     l_msg_count                 NUMBER;
6248     l_root_instance_ou          NUMBER;
6249     l_new_instance_ou           NUMBER;
6250     l_parent_instance_id        NUMBER;
6251     l_return_status             VARCHAR2(1);
6252     l_msg_data                  VARCHAR2(2000);
6253     l_subscript                 NUMBER       DEFAULT 0;
6254     i                           NUMBER       := 0;
6255     l_transaction_type_id       NUMBER;
6256     l_return_val                BOOLEAN;
6257     l_attribute_id              NUMBER;
6258     l_concatenated_segments     mtl_system_items_kfv.concatenated_segments%TYPE;
6259     l_new_instance_id           NUMBER;
6260     l_new_csi_instance_ovn      NUMBER;
6261     l_end_date                  DATE;
6262 
6263     -- Variables needed for CSI API call
6264     l_csi_instance_rec          csi_datastructures_pub.instance_rec;
6265     l_csi_party_rec             csi_datastructures_pub.party_rec;
6266     l_csi_transaction_rec       csi_datastructures_pub.transaction_rec;
6267     l_csi_relationship_rec      csi_datastructures_pub.ii_relationship_rec;
6268     l_csi_relationship_tbl      csi_datastructures_pub.ii_relationship_tbl;
6269     l_csi_party_tbl             csi_datastructures_pub.party_tbl;
6270     l_csi_account_tbl           csi_datastructures_pub.party_account_tbl;
6271     l_csi_pricing_attrib_tbl    csi_datastructures_pub.pricing_attribs_tbl;
6272     l_csi_org_assignments_tbl   csi_datastructures_pub.organization_units_tbl;
6273     l_csi_asset_assignment_tbl  csi_datastructures_pub.instance_asset_tbl;
6274     l_csi_instance_id_lst       csi_datastructures_pub.id_tbl;
6275     l_party_account_rec         csi_datastructures_pub.party_account_rec;
6276     l_csi_extend_attrib_rec     csi_datastructures_pub.extend_attrib_values_rec;
6277     l_csi_ext_attrib_values_tbl csi_datastructures_pub.extend_attrib_values_tbl;
6278 --
6279 
6280 BEGIN
6281     IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
6282         FND_LOG.string(FND_LOG.level_procedure,l_full_name,'Start of the API');
6283     END IF;
6284 
6285     -- Standard start of API savepoint
6286     SAVEPOINT create_unassigned_instance;
6287 
6288     -- Initialize Procedure return status to success
6289     x_return_status := FND_API.G_RET_STS_SUCCESS;
6290 
6291     -- Standard call to check for call compatibility
6292     IF NOT FND_API.Compatible_API_Call(l_api_version, p_api_version,
6293                                        l_api_name, G_PKG_NAME) THEN
6294         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6295     END IF;
6296 
6297     -- Initialize message list if p_init_msg_list is set to TRUE
6298     IF FND_API.To_Boolean(p_init_msg_list) THEN
6299         FND_MSG_PUB.Initialize;
6300     END IF;
6301 
6302     -- Validate input parameter p_uc_header_id
6303     OPEN check_uc_header_csr(p_uc_header_id);
6304     FETCH check_uc_header_csr INTO l_check_uc_header_rec;
6305 
6306     IF check_uc_header_csr%NOTFOUND THEN
6307         CLOSE check_uc_header_csr;
6308         -- p_uc_header_id in invalid
6309         FND_MESSAGE.set_name('AHL','AHL_UC_API_PARAMETER_INVALID');
6310         FND_MESSAGE.set_token('NAME', 'uc_header_id');
6311         FND_MESSAGE.set_token('VALUE', p_uc_header_id);
6312         FND_MSG_PUB.add;
6313         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6314     ELSIF (l_check_uc_header_rec.unit_config_status_code = 'APPROVAL_PENDING' OR
6315            l_check_uc_header_rec.active_uc_status_code = 'APPROVAL_PENDING') THEN
6316         CLOSE check_uc_header_csr;
6317         -- UC status is not editable
6318         FND_MESSAGE.set_name('AHL','AHL_UC_STATUS_PENDING');
6319         FND_MESSAGE.set_token('UC_HEADER_ID', l_check_uc_header_rec.unit_config_header_id);
6320         FND_MSG_PUB.add;
6321         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6322     ELSIF (l_check_uc_header_rec.parent_uc_header_id IS NOT NULL) THEN
6323         CLOSE check_uc_header_csr;
6324         -- UC is installed sub config
6325         FND_MESSAGE.set_name('AHL','AHL_UC_INST_SUB_CONFIG');
6326         FND_MESSAGE.set_token('UC_HEADER_ID', l_check_uc_header_rec.unit_config_header_id);
6327         FND_MSG_PUB.add;
6328         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6329     ELSE
6330         CLOSE check_uc_header_csr;
6331     END IF;
6332 
6333     -- Get the operating unit of the root instance
6334     l_root_instance_ou := get_operating_unit(l_check_uc_header_rec.csi_item_instance_id);
6335 
6336     -- The parent instance is the root node
6337     l_parent_instance_id := l_check_uc_header_rec.csi_item_instance_id;
6338 
6339     -- Check whether the UC is expired or not by checking for the root instance
6340     OPEN get_instance_date(l_parent_instance_id);
6341     FETCH get_instance_date INTO l_end_date;
6342     CLOSE get_instance_date;
6343     IF TRUNC(NVL(l_end_date, SYSDATE+1)) <= TRUNC(SYSDATE) THEN
6344         FND_MESSAGE.set_name('AHL','AHL_UC_STATUS_EXPIRED');
6345         FND_MESSAGE.set_token('UC_NAME', l_check_uc_header_rec.unit_config_header_id);
6346         FND_MSG_PUB.add;
6347         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6348     END IF;
6349 
6350     -- When creating the new instances, the "From Inventory" Serial Tag should not be used anymore.
6351     IF(p_x_uc_instance_rec.sn_tag_code IS NOT NULL AND p_x_uc_instance_rec.sn_tag_code = 'INVENTORY') THEN
6352         FND_MESSAGE.set_name( 'AHL','AHL_UC_SER_TG_CR_INVEN' );
6353         FND_MSG_PUB.add;
6354         RAISE FND_API.G_EXC_ERROR;
6355     END IF;
6356 
6357     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6358         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, l_full_name,
6359                        ' l_root_instance_ou => '||l_root_instance_ou||
6360      	               ' l_parent_instance_id => '||l_parent_instance_id);
6361     END IF;
6362 
6363     -- Get the msg count befrore API call
6364     l_msg_count_bef := FND_MSG_PUB.count_msg;
6365 
6366     -- Validate Inventory details
6367     validate_uc_invdetails (
6368         p_inventory_id          => p_x_uc_instance_rec.inventory_item_id,
6369         p_organization_id       => p_x_uc_instance_rec.inventory_org_id,
6370         p_serial_number         => p_x_uc_instance_rec.serial_number,
6371         p_serialnum_tag_code    => p_x_uc_instance_rec.sn_tag_code,
6372         p_quantity              => p_x_uc_instance_rec.quantity,
6373         p_uom_code              => p_x_uc_instance_rec.uom_code,
6374         p_revision              => p_x_uc_instance_rec.revision,
6375         p_lot_number            => p_x_uc_instance_rec.lot_number,
6376         p_position_ref_meaning  => NULL,
6377         x_concatenated_segments => l_concatenated_segments);
6378 
6379     -- Check Error Message stack
6380     l_msg_count := FND_MSG_PUB.count_msg;
6381     IF (l_msg_count > l_msg_count_bef) THEN
6382         RAISE FND_API.G_EXC_ERROR;
6383     END IF;
6384 
6385     -- Validate manufacturing date
6386     IF (p_x_uc_instance_rec.mfg_date IS NOT NULL AND
6387         p_x_uc_instance_rec.mfg_date <> FND_API.G_MISS_DATE AND
6388         p_x_uc_instance_rec.mfg_date > SYSDATE) THEN
6389         -- mfg_date is invalid
6390         FND_MESSAGE.set_name('AHL','AHL_UC_MFGDATE_INVALID');
6391         FND_MESSAGE.set_token('DATE',p_x_uc_instance_rec.mfg_date);
6392         FND_MESSAGE.set_token('INV_ITEM',l_concatenated_segments);
6393         FND_MSG_PUB.add;
6394     END IF;
6395 
6396     -- Build CSI records and call API
6397     -- First get unit config location and owner details
6398     OPEN csi_item_instance_csr(l_parent_instance_id);
6399     FETCH csi_item_instance_csr INTO l_uc_owner_loc_rec;
6400 
6401     IF (csi_item_instance_csr%NOTFOUND) THEN
6402         CLOSE csi_item_instance_csr;
6403         -- parent instance is invalid
6404         FND_MESSAGE.set_name('AHL','AHL_UC_CSII_INVALID');
6405         FND_MESSAGE.set_token('CSII',l_parent_instance_id);
6406         FND_MSG_PUB.add;
6407         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6408     END IF;
6409 
6410     CLOSE csi_item_instance_csr;
6411 
6412     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6413         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, l_full_name,
6414                        ' l_uc_owner_loc_rec.location_id => '||l_uc_owner_loc_rec.location_id||
6415                        ' l_uc_owner_loc_rec.location_type_code => '||l_uc_owner_loc_rec.location_type_code||
6416                        ' l_uc_owner_loc_rec.party_id => '||l_uc_owner_loc_rec.party_id||
6417                        ' l_uc_owner_loc_rec.party_source_table => '||l_uc_owner_loc_rec.party_source_table||
6418                        ' l_uc_owner_loc_rec.instance_party_id => '||l_uc_owner_loc_rec.instance_party_id||
6419                        ' l_uc_owner_loc_rec.wip_job_id => '||l_uc_owner_loc_rec.wip_job_id);
6420     END IF;
6421 
6422     -- Set csi instance record
6423     l_csi_instance_rec.inventory_item_id      := p_x_uc_instance_rec.inventory_item_id;
6424     l_csi_instance_rec.vld_organization_id    := p_x_uc_instance_rec.inventory_org_id;
6425     l_csi_instance_rec.quantity               := p_x_uc_instance_rec.quantity;
6426     l_csi_instance_rec.unit_of_measure        := p_x_uc_instance_rec.uom_code;
6427     l_csi_instance_rec.install_date           := p_x_uc_instance_rec.install_date;
6428     l_csi_instance_rec.location_id            := l_uc_owner_loc_rec.location_id;
6429     l_csi_instance_rec.location_type_code     := l_uc_owner_loc_rec.location_type_code;
6430     l_csi_instance_rec.wip_job_id             := l_uc_owner_loc_rec.wip_job_id;
6431     l_csi_instance_rec.mfg_serial_number_flag := 'N';
6432     l_csi_instance_rec.instance_usage_code    := NULL;
6433 
6434     IF (p_x_uc_instance_rec.serial_number IS NOT NULL AND
6435         p_x_uc_instance_rec.serial_number <> FND_API.G_MISS_CHAR) THEN
6436         l_csi_instance_rec.serial_number := p_x_uc_instance_rec.serial_number;
6437     END IF;
6438 
6439     IF (p_x_uc_instance_rec.lot_number IS NOT NULL AND
6440         p_x_uc_instance_rec.lot_number <> FND_API.G_MISS_CHAR) THEN
6441         l_csi_instance_rec.lot_number := p_x_uc_instance_rec.lot_number;
6442     END IF;
6443 
6444     IF (p_x_uc_instance_rec.revision IS NOT NULL AND
6445         p_x_uc_instance_rec.revision <> FND_API.G_MISS_CHAR) THEN
6446         l_csi_instance_rec.inventory_revision := p_x_uc_instance_rec.revision;
6447     END IF;
6448 
6449     -- Build CSI extended attribs
6450     IF (p_x_uc_instance_rec.mfg_date IS NOT NULL AND
6451         p_x_uc_instance_rec.mfg_date <> FND_API.G_MISS_DATE) THEN
6452         AHL_UTIL_UC_PKG.getcsi_attribute_id('AHL_MFG_DATE',l_attribute_id, l_return_val);
6453 
6454         IF NOT(l_return_val) THEN
6455             FND_MESSAGE.set_name('AHL','AHL_UC_ATTRIB_CODE_MISSING');
6456             FND_MESSAGE.set_token('CODE', 'AHL_MFG_DATE');
6457             FND_MSG_PUB.add;
6458         ELSE
6459             l_csi_extend_attrib_rec.attribute_id := l_attribute_id;
6460             l_csi_extend_attrib_rec.attribute_value := to_char(p_x_uc_instance_rec.mfg_date, 'DD/MM/YYYY');
6461             l_subscript := l_subscript + 1;
6462             l_csi_ext_attrib_values_tbl(l_subscript) := l_csi_extend_attrib_rec;
6463         END IF;
6464     END IF;
6465 
6466     IF (p_x_uc_instance_rec.serial_number IS NOT NULL AND
6467         p_x_uc_instance_rec.serial_number <> FND_API.G_MISS_CHAR) THEN
6468         AHL_UTIL_UC_PKG.getcsi_attribute_id('AHL_TEMP_SERIAL_NUM',l_attribute_id, l_return_val);
6469 
6470         IF NOT(l_return_val) THEN
6471             FND_MESSAGE.set_name('AHL','AHL_UC_ATTRIB_CODE_MISSING');
6472             FND_MESSAGE.set_token('CODE', 'AHL_TEMP_SERIAL_NUM');
6473             FND_MSG_PUB.add;
6474         ELSE
6475             l_csi_extend_attrib_rec.attribute_id         := l_attribute_id;
6476             l_csi_extend_attrib_rec.attribute_value      := p_x_uc_instance_rec.sn_tag_code;
6477             l_csi_ext_attrib_values_tbl(l_subscript + 1) := l_csi_extend_attrib_rec;
6478         END IF;
6479     END IF;
6480 
6481     -- Build CSI party record
6482     l_csi_party_rec.party_id               := l_uc_owner_loc_rec.party_id;
6483     l_csi_party_rec.relationship_type_code := 'OWNER';
6484     l_csi_party_rec.party_source_table     := l_uc_owner_loc_rec.party_source_table;
6485     l_csi_party_rec.contact_flag           := 'N';
6486     l_csi_party_tbl(1)                     := l_csi_party_rec;
6487 
6488     -- Build CSI accounts table
6489     FOR party_ip_acct IN csi_ip_accounts_csr(l_uc_owner_loc_rec.instance_party_id)
6490     LOOP
6491         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6492             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, l_full_name,
6493                            ' i => '||i||
6494                            ' party_ip_acct.party_account_id => '||party_ip_acct.party_account_id);
6495         END IF;
6496 
6497         l_party_account_rec.party_account_id       := party_ip_acct.party_account_id;
6498         l_party_account_rec.relationship_type_code := 'OWNER';
6499         l_party_account_rec.parent_tbl_index       := 1;
6500         i := i + 1;
6501         l_csi_account_tbl(i)                       := l_party_account_rec;
6502     END LOOP;
6503 
6504     -- Build CSI transaction record, first get transaction_type_id
6505     AHL_UTIL_UC_PKG.getcsi_transaction_id('UC_CREATE',l_transaction_type_id, l_return_val);
6506 
6507     IF NOT(l_return_val) THEN
6508         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6509     END IF;
6510 
6511     l_csi_transaction_rec.source_transaction_date := SYSDATE;
6512     l_csi_transaction_rec.transaction_type_id     := l_transaction_type_id;
6513 
6514     -- Check Error Message stack
6515     l_msg_count := FND_MSG_PUB.count_msg;
6516     IF (l_msg_count > l_msg_count_bef) THEN
6517         RAISE  FND_API.G_EXC_ERROR;
6518     END IF;
6519 
6520     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6521         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, l_full_name,
6522                        ' About to call CSI_ITEM_INSTANCE_PUB.create_item_instance');
6523     END IF;
6524 
6525     --Call CSI API to create instance
6526     CSI_ITEM_INSTANCE_PUB.create_item_instance(
6527         p_api_version           => 1.0,
6528         p_instance_rec          => l_csi_instance_rec,
6529         p_txn_rec               => l_csi_transaction_rec,
6530         p_ext_attrib_values_tbl => l_csi_ext_attrib_values_tbl,
6531         p_party_tbl             => l_csi_party_tbl,
6532         p_account_tbl           => l_csi_account_tbl,
6533         p_pricing_attrib_tbl    => l_csi_pricing_attrib_tbl,
6534         p_org_assignments_tbl   => l_csi_org_assignments_tbl,
6535         p_asset_assignment_tbl  => l_csi_asset_assignment_tbl,
6536         x_return_status         => l_return_status,
6537         x_msg_count             => l_msg_count,
6538         x_msg_data              => l_msg_data);
6539 
6540     l_new_instance_id                         := l_csi_instance_rec.instance_id;
6541     l_new_csi_instance_ovn                    := l_csi_instance_rec.object_version_number;
6542     p_x_uc_instance_rec.instance_id           := l_new_instance_id;
6543     p_x_uc_instance_rec.object_version_number := l_new_csi_instance_ovn;
6544 
6545     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6546         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, l_full_name,
6547                        ' After call to CSI_ITEM_INSTANCE_PUB.create_item_instance'||
6548                        ' instance_id => '||l_csi_instance_rec.instance_id||
6549      	               ' l_return_status => '||l_return_status||
6550                        ' p_x_uc_instance_rec.instance_id='||p_x_uc_instance_rec.instance_id);
6551     END IF;
6552 
6553     IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
6554         RAISE FND_API.G_EXC_ERROR;
6555     ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
6556         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6557     END IF;
6558 
6559     -- Before assigning the new instance as extra node, make sure its operating unit is exactly the same as that
6560     -- of the root instance
6561     l_new_instance_ou := get_operating_unit(l_new_instance_id);
6562 
6563     IF l_root_instance_ou IS NULL THEN
6564         FND_MESSAGE.set_name('AHL', 'AHL_UC_INSTANCE_OU_NULL');
6565         FND_MESSAGE.set_token('INSTANCE', l_parent_instance_id);
6566         FND_MSG_PUB.add;
6567         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6568     ELSIF l_new_instance_ou IS NULL THEN
6569         FND_MESSAGE.set_name('AHL', 'AHL_UC_INSTANCE_OU_NULL');
6570         FND_MESSAGE.set_token('INSTANCE', l_new_instance_id);
6571         FND_MSG_PUB.add;
6572         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6573     ELSIF l_root_instance_ou <> l_new_instance_ou THEN
6574         FND_MESSAGE.set_name('AHL', 'AHL_UC_INSTANCE_OU_UNMATCH');
6575         FND_MESSAGE.set_token('INSTANCE', l_new_instance_id);
6576         FND_MESSAGE.set_token('ROOT_INSTANCE', l_parent_instance_id);
6577         FND_MSG_PUB.add;
6578         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6579     END IF;
6580 
6581     --  Build CSI relationships table
6582     l_csi_relationship_rec.relationship_type_code := 'COMPONENT-OF';
6583     l_csi_relationship_rec.object_id              := l_parent_instance_id;
6584     l_csi_relationship_rec.position_reference     := NULL;
6585     l_csi_relationship_rec.subject_id             := l_new_instance_id;
6586     l_csi_relationship_tbl(1)                     := l_csi_relationship_rec;
6587 
6588     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6589         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, l_full_name,
6590                        ' About to call CSI_II_RELATIONSHIPS_PUB.create_relationship');
6591     END IF;
6592 
6593     CSI_II_RELATIONSHIPS_PUB.create_relationship(
6594         p_api_version      => 1.0,
6595         p_relationship_tbl => l_csi_relationship_tbl,
6596         p_txn_rec          => l_csi_transaction_rec,
6597         x_return_status    => l_return_status,
6598         x_msg_count        => l_msg_count,
6599         x_msg_data         => l_msg_data);
6600 
6601     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6602         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, l_full_name,
6603                        ' After call to CSI_II_RELATIONSHIPS_PUB.create_relationship'||
6604      	               ' l_return_status => '||l_return_status);
6605     END IF;
6606 
6607     IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
6608         RAISE FND_API.G_EXC_ERROR;
6609     ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
6610         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6611     END IF;
6612 
6613     -- Standard check of p_commit
6614     IF FND_API.TO_BOOLEAN(p_commit) THEN
6615         COMMIT WORK;
6616     END IF;
6617 
6618     -- Standard call to get message count and if count is 1, get message info
6619     FND_MSG_PUB.Count_And_Get
6620     ( p_count   => x_msg_count,
6621       p_data    => x_msg_data,
6622       p_encoded => FND_API.G_FALSE
6623     );
6624 
6625     IF (FND_LOG.level_procedure >= FND_LOG.g_current_runtime_level) THEN
6626         FND_LOG.string(FND_LOG.level_procedure,l_full_name,'End of the API');
6627     END IF;
6628 
6629 EXCEPTION
6630     WHEN FND_API.G_EXC_ERROR THEN
6631         Rollback to create_unassigned_instance;
6632         x_return_status := FND_API.G_RET_STS_ERROR;
6633         FND_MSG_PUB.Count_And_Get( p_count   => x_msg_count,
6634                                    p_data    => x_msg_data,
6635                                    p_encoded => fnd_api.g_false);
6636 
6637     WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6638         Rollback to create_unassigned_instance;
6639         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6640         FND_MSG_PUB.Count_And_Get( p_count   => x_msg_count,
6641                                    p_data    => x_msg_data,
6642                                    p_encoded => fnd_api.g_false);
6643 
6644     WHEN OTHERS THEN
6645         Rollback to create_unassigned_instance;
6646         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6647         FND_MSG_PUB.Add_Exc_Msg( p_pkg_name       => G_PKG_NAME,
6648                                  p_procedure_name => l_api_name,
6649                                  p_error_text     => SQLERRM);
6650         FND_MSG_PUB.Count_And_Get( p_count   => x_msg_count,
6651                                    p_data    => x_msg_data,
6652                                    p_encoded => FND_API.G_FALSE);
6653 
6654 END create_unassigned_instance;
6655 
6656 -- Start of Comments  --
6657 -- Procedure get_instances_for_req
6658 -- This API is used to get all the instances for a given Position Based Material Requirement.
6659 -- It is similar to get_available_instances, but differs in the following ways:
6660 -- 1. It gets instances in Inventory also
6661 -- 2. It populates the global temp table AHL_APPLICABLE_INSTANCES instead of returing a table of instances.
6662 --    (POSITION_ID is set to p_relationship_id and not to the path position id)
6663 -- 3. It validates the item against the SB rules to return only instances that conform to SB rules if applicable.
6664 --
6665 -- Procedure name  : get_instances_for_req
6666 -- Type        	   : Private
6667 -- Function    	   : Get all applicable instances for a given Position Based Material Requirement.
6668 -- Pre-reqs    	:
6669 --
6670 -- get_instances_for_req parameters :
6671 --   p_schedule_material_id  IN NUMBER  Required: Specifies the Position Based Material Requirement
6672 --   p_inventory_item_id     IN NUMBER DEFAULT NULL: If given, gets instances of this item only
6673 --   p_supply_location_type  IN  VARCHAR2 DEFAULT NULL: Can be 'INVENTORY' to get only instances in inventory
6674 --   p_organization_name     IN  VARCHAR2 DEFAULT '%': Used only if p_supply_location_type is INVENTORY.
6675 --                                                     Name of inv org to look for instances; Supports wildcards
6676 --   p_serial_number         IN NUMBER DEFAULT '%': If given, gets instances with this serial only; Supports wildcards
6677 --
6678 -- Version : Initial Version   1.0 (written for the USAF Marshaling project by jaramana on 07-FEB-2012
6679 --
6680 --  End of Comments  --
6681 PROCEDURE get_instances_for_req(
6682   p_api_version            IN  NUMBER := 1.0,
6683   p_init_msg_list          IN  VARCHAR2 := FND_API.G_FALSE,
6684   x_return_status          OUT NOCOPY VARCHAR2,
6685   x_msg_count              OUT NOCOPY NUMBER,
6686   x_msg_data               OUT NOCOPY VARCHAR2,
6687   p_schedule_material_id   IN  NUMBER,
6688   p_inventory_item_id      IN  NUMBER DEFAULT NULL,
6689   p_supply_location_type   IN  VARCHAR2 DEFAULT NULL,
6690   p_organization_name      IN  VARCHAR2 DEFAULT '%',
6691   p_serial_number          IN  VARCHAR2 DEFAULT '%')
6692 IS
6693   l_api_name       CONSTANT   VARCHAR2(30) := 'get_instances_for_req';
6694   l_api_version    CONSTANT   NUMBER       := 1.0;
6695   l_return_status             VARCHAR2(1);
6696   l_msg_count                 NUMBER;
6697   l_msg_data                  VARCHAR2(2000);
6698   l_relationship_id           NUMBER;
6699   l_mc_header_id              NUMBER;
6700 
6701   l_instance_id               NUMBER;
6702   l_instance_number           csi_item_instances.instance_number%TYPE;
6703   l_inventory_item_id         NUMBER;
6704   l_inv_master_org_id         NUMBER;
6705   l_quantity                  NUMBER;
6706   l_inventory_revision        VARCHAR2(3);
6707   l_uom_code                  VARCHAR2(3);
6708   l_item_assoc_id             NUMBER;
6709   l_serial_number             csi_item_instances.serial_number%TYPE := upper(nvl(p_serial_number, '%'));
6710   l_uc_header_id              NUMBER;
6711   iIndex                      NUMBER;
6712   l_dummy_char                VARCHAR2(1);
6713   l_ignore_quant_vald         VARCHAR2(1) := 'Y';
6714   l_wip_job_id                NUMBER;
6715 
6716   l_alt_items_tbl AHL_LTP_MTL_REQ_PVT.Alt_Items_Tbl_Type;
6717   TYPE Ref_Tbl_Type IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
6718   l_id_ref_map_Tbl Ref_Tbl_Type;
6719 
6720   -- Validate the p_inventory_item_id parameter
6721   CURSOR check_inv_item_id IS
6722   SELECT 'x' from mtl_system_items_kfv
6723    WHERE inventory_item_id = p_inventory_item_id;
6724 
6725   -- Cursor to pick up all instances that match the item association setup. But units can only
6726   -- be installed in MC leaf node which has associated sub-MC, in other words, any units
6727   -- can't be installed in a branch node.
6728   -- Extra nodes and other installed instances will not be retrieved
6729   CURSOR get_instance(c_relationship_id NUMBER,
6730                       c_inventory_item_id NUMBER,
6731                       c_serial_number VARCHAR2,
6732                       c_supply_location_type VARCHAR2,
6733                       c_organization_name VARCHAR2) IS
6734     -- All stand alone instances that could be installed
6735     SELECT CII.instance_id,
6736            CII.instance_number,
6737            CII.inventory_item_id,
6738            CII.inv_master_organization_id,
6739            CII.quantity,
6740            CII.inventory_revision,
6741            CII.unit_of_measure uom_code,
6742            to_number(NULL) uc_header_id,
6743            CII.wip_job_id
6744       FROM csi_item_instances CII,
6745            mtl_system_items_kfv MSIK,
6746            ahl_mc_relationships AMR,
6747            ahl_item_associations_b AIA,
6748            hr_all_organization_units_tl HAOUT
6749      WHERE CII.inventory_item_id = NVL(c_inventory_item_id, CII.inventory_item_id)
6750        AND MSIK.inventory_item_id = CII.inventory_item_id
6751        AND MSIK.organization_id = CII.inv_master_organization_id
6752        AND AMR.relationship_id = c_relationship_id
6753        AND AIA.item_group_id = AMR.item_group_id
6754        AND AIA.inventory_item_id = CII.inventory_item_id
6755        AND trunc(nvl(AMR.active_start_date, SYSDATE)) <= trunc(SYSDATE)
6756        AND trunc(nvl(AMR.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
6757        AND trunc(nvl(CII.active_start_date, SYSDATE)) <= trunc(SYSDATE)
6758        AND trunc(nvl(CII.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
6759        AND CII.location_type_code NOT IN ('PO', 'PROJECT')  -- Do not exclude 'INVENTORY' and 'IN-TRANSIT'
6760        AND CII.location_type_code = NVL(p_supply_location_type, CII.location_type_code)
6761        AND AIA.interchange_type_code IN ('1-WAY INTERCHANGEABLE', '2-WAY INTERCHANGEABLE')
6762        AND nvl(AIA.revision, nvl(CII.inventory_revision, -1)) = nvl(CII.inventory_revision, -1)
6763        -- Not an installed child instance
6764        AND (NOT EXISTS (SELECT 1
6765                          FROM csi_ii_relationships CIR
6766                         WHERE CIR.subject_id = CII.instance_id
6767                           AND CIR.relationship_type_code = 'COMPONENT-OF'
6768                           AND trunc(nvl(CIR.active_start_date, SYSDATE)) <= trunc(SYSDATE)
6769                           AND trunc(nvl(CIR.active_end_date, SYSDATE+1)) >trunc(SYSDATE))
6770            )
6771        -- Not a unit configuration
6772        AND (NOT EXISTS (SELECT 1
6773                         FROM ahl_unit_config_headers AUCH
6774                        WHERE AUCH.csi_item_instance_id = CII.instance_id
6775                          AND trunc(nvl(AUCH.active_end_date, SYSDATE+1)) > trunc(SYSDATE))
6776            )
6777        -- Filter by serial number if provided
6778        AND upper(nvl(CII.serial_number, '%')) LIKE c_serial_number
6779        -- Instance's Item is defined in at least one Inv Org in User's OU
6780        AND (EXISTS (SELECT 'X'
6781                      FROM mtl_parameters mp, inv_organization_info_v io
6782                     WHERE mp.master_organization_id = MSIK.organization_id
6783                       AND mp.organization_Id = io.organization_id
6784                       AND NVL(io.operating_unit, mo_global.get_current_org_id()) = mo_global.get_current_org_id())
6785            )
6786        -- Org Filtering: If p_supply_location_type is INVENTORY, filter by the org by the passed in p_organization_name
6787        AND CII.inv_organization_id = HAOUT.organization_id(+)
6788        AND HAOUT.language = USERENV('LANG')
6789        AND HAOUT.NAME LIKE DECODE(p_supply_location_type, 'INVENTORY', NVL(c_organization_name, '%'), HAOUT.NAME)
6790     --Plus those units could be installed
6791     UNION ALL
6792     SELECT CII.instance_id,
6793            CII.instance_number,
6794            CII.inventory_item_id,
6795            CII.inv_master_organization_id,
6796            CII.quantity,
6797            CII.inventory_revision,
6798            CII.unit_of_measure uom_code,
6799            U.uc_header_id uc_header_id,
6800            CII.wip_job_id
6801       FROM (SELECT UH.unit_config_header_id uc_header_id,
6802                    UH.csi_item_instance_id csi_instance_id,
6803                    UH.master_config_id mc_header_id,
6804                    UH.unit_config_status_code uc_status_code,
6805                    UH.active_end_date,
6806                    CR.object_id parent_instance_id
6807               FROM ahl_unit_config_headers UH, csi_ii_relationships CR
6808              WHERE UH.csi_item_instance_id = CR.subject_id (+) AND
6809                    CR.relationship_type_code (+) = 'COMPONENT-OF' AND
6810                    trunc(nvl(CR.active_start_date (+), SYSDATE)) <= trunc(SYSDATE) AND
6811                    trunc(nvl(CR.active_end_date (+), SYSDATE+1)) > trunc(SYSDATE)
6812            ) U,
6813            csi_item_instances CII,
6814            mtl_system_items_kfv MSIK,
6815            hr_all_organization_units_tl HAOUT
6816      WHERE CII.inventory_item_id = NVL(c_inventory_item_id, CII.inventory_item_id)
6817        AND U.csi_instance_id = CII.instance_id
6818        AND CII.inventory_item_id = MSIK.inventory_item_id
6819        AND CII.inv_master_organization_id = MSIK.organization_id
6820        -- No need to exclude instances in WIP Jobs
6821        AND CII.location_type_code NOT IN ('PO', 'PROJECT')  -- Do not exclude 'INVENTORY' and 'IN-TRANSIT'
6822        AND CII.location_type_code = NVL(p_supply_location_type, CII.location_type_code)
6823        AND trunc(nvl(U.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
6824        -- If the top instance is expired then this UC is also taken as Expired
6825        AND trunc(nvl(CII.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
6826        -- Check if this is a applicable alternate subconfig for the position
6827        AND (EXISTS (SELECT 1
6828                       FROM ahl_mc_config_relations AMCR
6829                      WHERE AMCR.mc_header_id = U.mc_header_id
6830                        AND AMCR.relationship_id = c_relationship_id
6831                        AND trunc(nvl(AMCR.active_start_date, SYSDATE)) <= trunc(SYSDATE)
6832                        AND trunc(nvl(AMCR.active_end_date, SYSDATE+1)) > trunc(SYSDATE))
6833            )
6834        AND U.parent_instance_id IS NULL  -- Exclude installed units
6835        AND U.uc_status_code IN ('COMPLETE', 'INCOMPLETE')  -- Exclude DRAFT, APPROVAL_REJECTED
6836        -- Get units only if passed position is a leaf position
6837        AND (NOT EXISTS  (SELECT 1
6838                           FROM ahl_mc_relationships MR
6839                          WHERE MR.parent_relationship_id = c_relationship_id
6840                            AND trunc(nvl(MR.active_start_date, SYSDATE)) <= trunc(SYSDATE)
6841                            AND trunc(nvl(MR.active_end_date, SYSDATE+1)) > trunc(SYSDATE))
6842            )
6843        -- Filter by serial number if provided
6844        AND upper(nvl(CII.serial_number, '%')) LIKE c_serial_number
6845        -- Instance's Item is defined in at least one Inv Org in User's OU
6846        AND (EXISTS (SELECT 'X'
6847                      FROM mtl_parameters mp,
6848                           inv_organization_info_v io
6849                     WHERE mp.master_organization_id = MSIK.organization_id
6850                       AND mp.organization_Id = io.organization_id
6851                       AND NVL(io.operating_unit, mo_global.get_current_org_id()) = mo_global.get_current_org_id())
6852            )
6853        -- Do not get quarantined units
6854        AND ahl_util_uc_pkg.IS_UNIT_QUARANTINED(U.uc_header_id , null) = FND_API.G_FALSE
6855        -- Org Filtering: If p_supply_location_type is INVENTORY, filter by the org by the passed in p_organization_name
6856        AND CII.inv_organization_id = HAOUT.organization_id(+)
6857        AND HAOUT.language = USERENV('LANG')
6858        AND HAOUT.NAME LIKE DECODE(p_supply_location_type, 'INVENTORY', NVL(c_organization_name, '%'), HAOUT.NAME)
6859     ORDER BY 2;  -- Sorted by Instance Number
6860 
6861   CURSOR check_relationship_id(c_relationship_id IN NUMBER) IS
6862     SELECT relationship_id, mc_header_id
6863       FROM ahl_mc_relationships
6864      WHERE relationship_id = c_relationship_id
6865        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
6866        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
6867        AND mc_header_id IN (SELECT mc_header_id
6868                               FROM ahl_mc_headers_b
6869                              WHERE config_status_code = 'COMPLETE');
6870 
6871   CURSOR get_asm_req_details_csr IS
6872     SELECT asm.mc_header_id,
6873            asm.position_key,
6874            asm.inventory_item_id,
6875            asm.visit_id,
6876            asm.visit_task_id,
6877            asm.item_group_id,
6878            asm.organization_id,
6879            asm.relationship_id,
6880            vt.instance_id
6881       FROM ahl_schedule_materials asm,
6882            ahl_visit_tasks_b vt
6883      WHERE asm.scheduled_material_id = p_schedule_material_id
6884        AND vt.visit_task_id = asm.visit_task_id;
6885   l_asm_details_rec get_asm_req_details_csr%ROWTYPE;
6886 
6887 BEGIN
6888   -- Initialize API return status to success
6889   x_return_status := FND_API.G_RET_STS_SUCCESS;
6890   -- Standard call to check for call compatibility.
6891   IF NOT FND_API.compatible_api_call(
6892     l_api_version,
6893     p_api_version,
6894     l_api_name,
6895     G_PKG_NAME)
6896   THEN
6897     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6898   END IF;
6899 
6900   -- Initialize message list if p_init_msg_list is set to TRUE.
6901   IF FND_API.to_boolean(p_init_msg_list) THEN
6902     FND_MSG_PUB.initialize;
6903   END IF;
6904 
6905   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6906     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
6907                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||'.begin',
6908                    'At the start of the procedure. p_schedule_material_id = ' || p_schedule_material_id || ', p_inventory_item_id = ' || p_inventory_item_id);
6909   END IF;
6910 
6911   -- Get the relationship_id from the Schedule Mtl Requirement
6912   OPEN get_asm_req_details_csr;
6913   FETCH get_asm_req_details_csr INTO l_asm_details_rec;
6914   IF get_asm_req_details_csr%NOTFOUND THEN
6915     FND_MESSAGE.SET_NAME('AHL', 'AHL_LTP_MAT_ID_INVALID');
6916     FND_MSG_PUB.add;
6917     CLOSE get_asm_req_details_csr;
6918     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6919   ELSE
6920     CLOSE get_asm_req_details_csr;
6921   END IF;
6922 
6923   -- Validate the relationship_id
6924   OPEN check_relationship_id(l_asm_details_rec.relationship_id);
6925   FETCH check_relationship_id INTO l_relationship_id, l_mc_header_id;
6926   IF check_relationship_id%NOTFOUND THEN
6927     FND_MESSAGE.set_name('AHL', 'AHL_UC_POSITION_INVALID');
6928     FND_MESSAGE.set_token('POSITION', l_asm_details_rec.relationship_id);
6929     FND_MSG_PUB.add;
6930     CLOSE check_relationship_id;
6931     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6932   ELSE
6933     CLOSE check_relationship_id;
6934   END IF;
6935 
6936   -- Validate p_inventory_item_id
6937   IF p_inventory_item_id IS NOT NULL THEN
6938     OPEN check_inv_item_id;
6939     FETCH check_inv_item_id INTO l_dummy_char;
6940     IF check_inv_item_id%NOTFOUND THEN
6941       FND_MESSAGE.set_name('AHL','AHL_OSP_INVALID_INV_ITEM');
6942       FND_MSG_PUB.add;
6943       CLOSE check_inv_item_id;
6944       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6945     ELSE
6946       CLOSE check_inv_item_id;
6947     END IF;
6948   END IF;
6949 
6950   -- Get the valid alternate items for this requirement after evaluating the SB rules
6951   AHL_LTP_MTL_REQ_PVT.Get_Alternate_Items(p_api_version          => 1.0,
6952                                           p_init_msg_list        => FND_API.G_FALSE,
6953                                           p_validation_level     => FND_API.G_VALID_LEVEL_FULL,
6954                                           p_schedule_material_id => p_schedule_material_id,
6955                                           p_curr_item_flag       => FND_API.G_TRUE,
6956                                           x_alt_items            => l_alt_items_tbl,
6957                                           x_return_status        => l_return_status,
6958                                           x_msg_count            => l_msg_count,
6959                                           x_msg_data             => l_msg_data);
6960 
6961   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6962     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
6963                      'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name,
6964                      'AHL_LTP_MTL_REQ_PVT.Get_Alternate_Items returned ' || l_return_status);
6965   END IF;
6966   IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
6967     RAISE FND_API.G_EXC_ERROR;
6968   ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
6969     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6970   END IF;
6971 
6972   IF (l_alt_items_tbl IS NOT NULL AND l_alt_items_tbl.COUNT > 0) THEN
6973     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6974       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
6975                        'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name,
6976                        'Number of items returned by AHL_LTP_MTL_REQ_PVT.Get_Alternate_Items ' || l_alt_items_tbl.COUNT);
6977     END IF;
6978     -- Copy into an associative array for quick lookup
6979     iIndex := l_alt_items_tbl.FIRST;
6980     LOOP
6981       l_id_ref_map_Tbl(l_alt_items_tbl(iIndex)) := l_alt_items_tbl(iIndex);
6982       EXIT WHEN iIndex = l_alt_items_tbl.LAST ;
6983       iIndex := l_alt_items_tbl.NEXT(iIndex);
6984     END LOOP;
6985   END IF;
6986 
6987   OPEN get_instance(c_relationship_id      => l_asm_details_rec.relationship_id,
6988                     c_inventory_item_id    => p_inventory_item_id,
6989                     c_serial_number        => l_serial_number,
6990                     c_supply_location_type => p_supply_location_type,
6991                     c_organization_name    => p_organization_name);
6992   LOOP
6993     FETCH get_instance INTO l_instance_id, l_instance_number, l_inventory_item_id, l_inv_master_org_id,
6994                             l_quantity, l_inventory_revision, l_uom_code, l_uc_header_id, l_wip_job_id;
6995     EXIT WHEN get_instance%NOTFOUND;
6996 
6997     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6998       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
6999                      'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name,
7000                      'Record retrieved by get_instance: l_instance_id = ' || l_instance_id);
7001     END IF;
7002     -- Check if this item is valid as per SB rules
7003     IF(l_id_ref_map_Tbl.EXISTS(l_inventory_item_id)) THEN
7004 
7005       -- If the instance is not a unit then, call procedure to validate whether the corresponding inventory
7006       -- can be assigned to that position
7007       IF l_uc_header_id IS NULL THEN
7008 
7009         AHL_UTIL_UC_PKG.validate_for_position(p_mc_relationship_id   => l_asm_details_rec.relationship_id,
7010                                               p_inventory_id         => l_inventory_item_id,
7011                                               p_organization_id      => l_inv_master_org_id,
7012                                               p_quantity             => l_quantity,
7013                                               p_revision             => l_inventory_revision,
7014                                               p_uom_code             => l_uom_code,
7015                                               p_position_ref_meaning => NULL,
7016                                               p_ignore_quant_vald    => l_ignore_quant_vald,
7017                                               x_item_assoc_id        => l_item_assoc_id);
7018       END IF;
7019 
7020       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7021         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
7022                        'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name,
7023                        'After position validation. x_item_assoc_id = ' || l_item_assoc_id);
7024       END IF;
7025 
7026       IF (fnd_msg_pub.count_msg = 0) THEN
7027         INSERT INTO AHL_APPLICABLE_INSTANCES(CSI_ITEM_INSTANCE_ID, POSITION_ID)
7028                                      VALUES (l_instance_id, l_asm_details_rec.relationship_id);
7029       END IF;  -- Message Count is zero
7030       fnd_msg_pub.initialize;
7031     ELSE
7032       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7033         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
7034                        'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name,
7035                        'Item ' || l_inventory_item_id || ' is not a valid item.');
7036       END IF;
7037     END IF;
7038   END LOOP;
7039 
7040   IF (get_instance%ISOPEN) THEN
7041     CLOSE get_instance;
7042   END IF;
7043 
7044   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7045     select count(*) into iIndex from AHL_APPLICABLE_INSTANCES;
7046     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
7047                      'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name,
7048                      'Total number of records in AHL_APPLICABLE_INSTANCES: ' || iIndex);
7049   END IF;
7050 
7051   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7052     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
7053                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||'.end',
7054                    'After normal execution.');
7055   END IF;
7056 
7057   -- Get all the error messages from the previous steps (if any) and raise the appropriate Exception
7058   l_msg_count := FND_MSG_PUB.count_msg;
7059   IF l_msg_count > 0 THEN
7060     x_msg_count := l_msg_count;
7061     RAISE FND_API.G_EXC_ERROR;
7062   END IF;
7063   -- Count and Get messages (optional)
7064   FND_MSG_PUB.count_and_get(
7065     p_encoded  => FND_API.G_FALSE,
7066     p_count    => x_msg_count,
7067     p_data     => x_msg_data);
7068 EXCEPTION
7069   WHEN FND_API.G_EXC_ERROR THEN
7070     x_return_status := FND_API.G_RET_STS_ERROR ;
7071     FND_MSG_PUB.count_and_get(
7072       p_encoded  => FND_API.G_FALSE,
7073       p_count    => x_msg_count,
7074       p_data     => x_msg_data);
7075   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7076     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7077     FND_MSG_PUB.count_and_get(
7078       p_encoded  => FND_API.G_FALSE,
7079       p_count    => x_msg_count,
7080       p_data     => x_msg_data);
7081   WHEN OTHERS THEN
7082     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR ;
7083     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
7084     THEN
7085       FND_MSG_PUB.add_exc_msg(
7086         p_pkg_name         => G_PKG_NAME,
7087         p_procedure_name   => l_api_name,
7088         p_error_text       => SUBSTRB(SQLERRM,1,240));
7089     END IF;
7090     FND_MSG_PUB.count_and_get(
7091       p_encoded  => FND_API.G_FALSE,
7092       p_count    => x_msg_count,
7093       p_data     => x_msg_data);
7094 END get_instances_for_req;
7095 
7096 -- New Function to determine if the given instance can be installed in the given position
7097 -- If p_schedule_material_id is passed, the item will be validated against SB Rules
7098 -- Returns FND_API.G_TRUE or FND_API.G_FALSE
7099 -- Written by jaramana for the USAF Marshaling project on 22-FEB-2012
7100 FUNCTION is_instance_valid_for_pos(
7101    p_instance_id          IN NUMBER,
7102    p_relationship_id      IN NUMBER,
7103    p_schedule_material_id IN NUMBER DEFAULT NULL)
7104 RETURN VARCHAR2 IS
7105 
7106   CURSOR check_relationship_id(c_relationship_id IN NUMBER) IS
7107     SELECT relationship_id, mc_header_id
7108       FROM ahl_mc_relationships
7109      WHERE relationship_id = c_relationship_id
7110        AND trunc(nvl(active_start_date, SYSDATE)) <= trunc(SYSDATE)
7111        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE)
7112        AND mc_header_id IN (SELECT mc_header_id
7113                               FROM ahl_mc_headers_b
7114                              WHERE config_status_code = 'COMPLETE');
7115 
7116   CURSOR is_instance_valid(c_instance_id IN NUMBER, c_relationship_id IN NUMBER) IS
7117     SELECT CII.inventory_item_id
7118       FROM csi_item_instances CII,
7119            mtl_system_items_kfv MSIK,
7120            ahl_mc_relationships AMR,
7121            ahl_item_associations_b AIA
7122      WHERE CII.instance_id = c_instance_id
7123        AND MSIK.inventory_item_id = CII.inventory_item_id
7124        AND MSIK.organization_id = CII.inv_master_organization_id
7125        AND AMR.relationship_id = c_relationship_id
7126        AND AIA.item_group_id = AMR.item_group_id
7127        AND AIA.inventory_item_id = CII.inventory_item_id
7128        AND trunc(nvl(AMR.active_start_date, SYSDATE)) <= trunc(SYSDATE)
7129        AND trunc(nvl(AMR.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
7130        AND trunc(nvl(CII.active_start_date, SYSDATE)) <= trunc(SYSDATE)
7131        AND trunc(nvl(CII.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
7132        AND CII.location_type_code NOT IN ('PO', 'PROJECT')
7133        AND AIA.interchange_type_code IN ('1-WAY INTERCHANGEABLE', '2-WAY INTERCHANGEABLE')
7134        AND nvl(AIA.revision, nvl(CII.inventory_revision, -1)) = nvl(CII.inventory_revision, -1)
7135        -- No need to filter out installed instances
7136        -- Not a unit configuration
7137        AND (NOT EXISTS (SELECT 1
7138                         FROM ahl_unit_config_headers AUCH
7139                        WHERE AUCH.csi_item_instance_id = CII.instance_id
7140                          AND trunc(nvl(AUCH.active_end_date, SYSDATE+1)) > trunc(SYSDATE))
7141            )
7142        -- Instance's Item is defined in at least one Inv Org in User's OU
7143        AND (EXISTS (SELECT 'X'
7144                      FROM mtl_parameters mp, inv_organization_info_v io
7145                     WHERE mp.master_organization_id = MSIK.organization_id
7146                       AND mp.organization_Id = io.organization_id
7147                       AND NVL(io.operating_unit, mo_global.get_current_org_id()) = mo_global.get_current_org_id())
7148            )
7149     --Plus those units could be installed
7150     UNION ALL
7151     SELECT CII.inventory_item_id
7152       FROM (SELECT UH.unit_config_header_id uc_header_id,
7153                    UH.csi_item_instance_id csi_instance_id,
7154                    UH.master_config_id mc_header_id,
7155                    UH.unit_config_status_code uc_status_code,
7156                    UH.active_end_date,
7157                    CR.object_id parent_instance_id
7158               FROM ahl_unit_config_headers UH, csi_ii_relationships CR
7159              WHERE UH.csi_item_instance_id = CR.subject_id (+) AND
7160                    CR.relationship_type_code (+) = 'COMPONENT-OF' AND
7161                    trunc(nvl(CR.active_start_date (+), SYSDATE)) <= trunc(SYSDATE) AND
7162                    trunc(nvl(CR.active_end_date (+), SYSDATE+1)) > trunc(SYSDATE)
7163            ) U,
7164            csi_item_instances CII,
7165            mtl_system_items_kfv MSIK
7166      WHERE CII.instance_id = c_instance_id
7167        AND U.csi_instance_id = CII.instance_id
7168        AND CII.inventory_item_id = MSIK.inventory_item_id
7169        AND CII.inv_master_organization_id = MSIK.organization_id
7170        AND CII.location_type_code NOT IN ('PO', 'PROJECT')
7171        AND trunc(nvl(U.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
7172        -- If the top instance is expired then this UC is also taken as Expired
7173        AND trunc(nvl(CII.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
7174        -- Check if this is a applicable alternate subconfig for the position
7175        AND (EXISTS (SELECT 1
7176                       FROM ahl_mc_config_relations AMCR
7177                      WHERE AMCR.mc_header_id = U.mc_header_id
7178                        AND AMCR.relationship_id = c_relationship_id
7179                        AND trunc(nvl(AMCR.active_start_date, SYSDATE)) <= trunc(SYSDATE)
7180                        AND trunc(nvl(AMCR.active_end_date, SYSDATE+1)) > trunc(SYSDATE))
7181            )
7182        -- DO NOT Exclude installed units
7183        -- Exclude DRAFT, APPROVAL_REJECTED statuses
7184        AND U.uc_status_code IN ('COMPLETE', 'INCOMPLETE')
7185        -- Get units only if passed position is a leaf position
7186        AND (NOT EXISTS  (SELECT 1
7187                           FROM ahl_mc_relationships MR
7188                          WHERE MR.parent_relationship_id = c_relationship_id
7189                            AND trunc(nvl(MR.active_start_date, SYSDATE)) <= trunc(SYSDATE)
7190                            AND trunc(nvl(MR.active_end_date, SYSDATE+1)) > trunc(SYSDATE))
7191            )
7192        -- Instance's Item is defined in at least one Inv Org in User's OU
7193        AND (EXISTS (SELECT 'X'
7194                      FROM mtl_parameters mp,
7195                           inv_organization_info_v io
7196                     WHERE mp.master_organization_id = MSIK.organization_id
7197                       AND mp.organization_Id = io.organization_id
7198                       AND NVL(io.operating_unit, mo_global.get_current_org_id()) = mo_global.get_current_org_id())
7199            )
7200        -- Do not get quarantined units
7201        AND ahl_util_uc_pkg.IS_UNIT_QUARANTINED(U.uc_header_id , null) = FND_API.G_FALSE;
7202 
7203   l_api_name        CONSTANT  VARCHAR2(30) := 'is_instance_valid_for_pos';
7204   l_ret_val                   VARCHAR2(1)  := FND_API.G_FALSE;
7205   l_relationship_id           NUMBER;
7206   l_mc_header_id              NUMBER;
7207   l_inventory_item_id         NUMBER;
7208   l_alt_items_tbl             AHL_LTP_MTL_REQ_PVT.Alt_Items_Tbl_Type;
7209   l_api_version    CONSTANT   NUMBER       := 1.0;
7210   l_return_status             VARCHAR2(1);
7211   l_msg_count                 NUMBER;
7212   l_msg_data                  VARCHAR2(2000);
7213   iIndex                      NUMBER;
7214 
7215 BEGIN
7216 
7217   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7218     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
7219                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||'.begin',
7220                    'At the start of the function. p_instance_id = ' || p_instance_id ||
7221                    ', p_relationship_id = ' || p_relationship_id ||
7222                    ', p_schedule_material_id = ' || p_schedule_material_id);
7223   END IF;
7224 
7225   IF(p_instance_id IS NULL OR p_relationship_id IS NULL) THEN
7226     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7227       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
7228                      'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name,
7229                      'Mandatory parameters are missing. Returning False.');
7230     END IF;
7231     RETURN l_ret_val;
7232   END IF;
7233 
7234   -- Validate the relationship_id
7235   OPEN check_relationship_id(p_relationship_id);
7236   FETCH check_relationship_id INTO l_relationship_id, l_mc_header_id;
7237   IF check_relationship_id%NOTFOUND THEN
7238     CLOSE check_relationship_id;
7239     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7240       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
7241                      'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name,
7242                      'p_relationship_id is invalid. Returning False.');
7243     END IF;
7244     RETURN l_ret_val;
7245   ELSE
7246     CLOSE check_relationship_id;
7247   END IF;
7248 
7249   OPEN is_instance_valid(c_instance_id     => p_instance_id,
7250                          c_relationship_id => p_relationship_id);
7251   FETCH is_instance_valid INTO l_inventory_item_id;
7252   IF (is_instance_valid%FOUND) THEN
7253     CLOSE is_instance_valid;
7254     -- Validate against SB Rules if p_schedule_material_id is passed
7255     IF (p_schedule_material_id IS NOT NULL) THEN
7256       -- Get the valid alternate items for this requirement after evaluating the SB rules
7257       AHL_LTP_MTL_REQ_PVT.Get_Alternate_Items(p_api_version          => 1.0,
7258                                               p_init_msg_list        => FND_API.G_FALSE,
7259                                               p_validation_level     => FND_API.G_VALID_LEVEL_FULL,
7260                                               p_schedule_material_id => p_schedule_material_id,
7261                                               p_curr_item_flag       => FND_API.G_TRUE,
7262                                               x_alt_items            => l_alt_items_tbl,
7263                                               x_return_status        => l_return_status,
7264                                               x_msg_count            => l_msg_count,
7265                                               x_msg_data             => l_msg_data);
7266 
7267       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7268         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
7269                        'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name,
7270                        'AHL_LTP_MTL_REQ_PVT.Get_Alternate_Items returned ' || l_return_status);
7271       END IF;
7272       IF (l_return_status = FND_API.G_RET_STS_SUCCESS) THEN
7273         -- Process the returned rows since AHL_LTP_MTL_REQ_PVT.Get_Alternate_Items returned success
7274         IF (l_alt_items_tbl IS NOT NULL AND l_alt_items_tbl.COUNT > 0) THEN
7275           IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7276             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
7277                              'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name,
7278                              'Number of items returned by AHL_LTP_MTL_REQ_PVT.Get_Alternate_Items ' || l_alt_items_tbl.COUNT);
7279           END IF;
7280           -- Check in the result table if the instance's item is present
7281           iIndex := l_alt_items_tbl.FIRST;
7282           LOOP
7283             IF (l_alt_items_tbl(iIndex) = l_inventory_item_id) THEN
7284               -- Item is valid based on SB Rules
7285               l_ret_val := FND_API.G_TRUE;
7286               IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7287                 FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
7288                                  'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name,
7289                                  'Item matched against SB Rules. Returning True.');
7290               END IF;
7291             END IF;  -- Item Matches
7292             EXIT WHEN ((iIndex = l_alt_items_tbl.LAST) OR (l_ret_val = FND_API.G_TRUE));
7293             iIndex := l_alt_items_tbl.NEXT(iIndex);
7294           END LOOP;
7295         ELSE
7296           IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7297             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
7298                              'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name,
7299                              'Number of items returned by AHL_LTP_MTL_REQ_PVT.Get_Alternate_Items is null or zero. Returning False.');
7300           END IF;
7301         END IF;  -- l_alt_items_tbl.COUNT > 0
7302       ELSE
7303         -- AHL_LTP_MTL_REQ_PVT.Get_Alternate_Items returned error status
7304         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7305           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
7306                            'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name,
7307                            'Since AHL_LTP_MTL_REQ_PVT.Get_Alternate_Items returned error, returning False.');
7308         END IF;
7309       END IF;  -- l_return_status = FND_API.G_RET_STS_SUCCESS or not
7310     ELSE
7311       -- p_schedule_material_id is not passed: No need to validate against SB Rules: Return True
7312       l_ret_val := FND_API.G_TRUE;
7313       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7314         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
7315                          'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name,
7316                          'Instance Item validated. Returning True.');
7317       END IF;
7318     END IF; -- p_schedule_material_id is not null
7319   ELSE
7320     -- Instance did not match
7321     CLOSE is_instance_valid;
7322     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7323       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
7324                      'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name,
7325                      'Instance did not match. Returning False.');
7326     END IF;
7327   END IF;
7328 
7329   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7330     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
7331                    'ahl.plsql.'||G_PKG_NAME||'.'||l_api_name||'.begin',
7332                    'At the end of the function. About to return ' || l_ret_val);
7333   END IF;
7334   RETURN l_ret_val;
7335 END is_instance_valid_for_pos;
7336 
7337 END AHL_UC_INSTANCE_PVT; -- Package body,