DBA Data[Home] [Help]

PACKAGE BODY: APPS.AHL_OSP_SHIPMENT_PUB

Source


1 PACKAGE BODY AHL_OSP_SHIPMENT_PUB AS
2   /* $Header: AHLPOSHB.pls 120.36.12020000.2 2012/12/07 00:47:13 sareepar ship $ */
3 
4   G_PKG_NAME            CONSTANT  VARCHAR2(30) := 'Ahl_OSP_Shipment_Pub';
5   G_APP_NAME            CONSTANT  VARCHAR2(3)  := 'AHL';
6   G_LOG_PREFIX          CONSTANT  VARCHAR2(100) := 'ahl.plsql.AHL_OSP_SHIPMENT_PUB';
7 
8   /** IB Transaction Related constants **/
9   -- Ship-only transaction type
10   G_OM_ORDER            CONSTANT  VARCHAR2(30) := 'OM_SHIPMENT';
11   -- Return transaction type
12   G_OM_RETURN           CONSTANT  VARCHAR2(30) := 'RMA_RECEIPT';
13   -- Transaction sub type for Ship-only lines (Service, Loan and Borrow)
14   --G_SUBTXN_ORDER        CONSTANT  VARCHAR2(30) := 'Ship Loaner';
15   -- Transaction sub type for Return lines (Service, Loan and Borrow)
16   --G_SUBTXN_RETURN       CONSTANT  VARCHAR2(30) := 'Return for Repair';
17   -- Transaction sub type for Exchange order's Ship-only lines
18   --G_SUBTXN_EXC_ORDER    CONSTANT  VARCHAR2(30) := 'Ship Replacement';
19   -- Transaction sub type for Exchange order's Return lines
20   --G_SUBTXN_EXC_RETURN   CONSTANT  VARCHAR2(30) := 'Return for Replacement';
21   -- Source transaction table
22   G_TRANSACTION_TABLE   CONSTANT  VARCHAR2(30) := 'OE_ORDER_LINES_ALL';
23   G_CSI_T_SOURCE_LINE_REF CONSTANT  VARCHAR2(50) := 'AHL_OSP_ORDER_LINES';
24 
25 
26    x_header_rec                    OE_ORDER_PUB.Header_Rec_Type;
27    x_header_val_rec                OE_ORDER_PUB.Header_Val_Rec_Type;
28    x_Header_Adj_tbl                OE_ORDER_PUB.Header_Adj_Tbl_Type;
29    x_Header_Adj_val_tbl            OE_ORDER_PUB.Header_Adj_Val_Tbl_Type;
30    x_Header_price_Att_tbl          OE_ORDER_PUB.Header_Price_Att_Tbl_Type;
31    x_Header_Adj_Att_tbl            OE_ORDER_PUB.Header_Adj_Att_Tbl_Type ;
32    x_Header_Adj_Assoc_tbl          OE_ORDER_PUB.Header_Adj_Assoc_Tbl_Type;
33    x_Header_Scredit_tbl            OE_ORDER_PUB.Header_Scredit_Tbl_Type;
34    x_Header_Scredit_val_tbl        OE_ORDER_PUB.Header_Scredit_Val_Tbl_Type;
35    x_line_tbl                      OE_ORDER_PUB.Line_Tbl_Type;
36    x_line_val_tbl                  OE_ORDER_PUB.Line_Val_Tbl_Type;
37    x_Line_Adj_tbl                  OE_ORDER_PUB.Line_Adj_Tbl_Type;
38    x_Line_Adj_val_tbl              OE_ORDER_PUB.Line_Adj_Val_Tbl_Type;
39    x_Line_price_Att_tbl            OE_ORDER_PUB.Line_Price_Att_Tbl_Type;
40    x_Line_Adj_Att_tbl              OE_ORDER_PUB.Line_Adj_Att_Tbl_Type ;
41    x_Line_Adj_Assoc_tbl            OE_ORDER_PUB.Line_Adj_Assoc_Tbl_Type;
42    x_Line_Scredit_tbl              OE_ORDER_PUB.Line_Scredit_Tbl_Type;
43    x_Line_Scredit_val_tbl          OE_ORDER_PUB.Line_Scredit_Val_Tbl_Type;
44    x_Lot_Serial_tbl                OE_ORDER_PUB.Lot_Serial_Tbl_Type;
45    x_Lot_Serial_val_tbl            OE_ORDER_PUB.Lot_Serial_Val_Tbl_Type;
46    x_action_request_tbl	           OE_ORDER_PUB.Request_Tbl_Type;
47 
48 ------------------------------
49 -- Declare Local Procedures --
50 ------------------------------
51 
52 PROCEDURE Convert_Header_Rec(
53 	p_header_rec     IN  AHL_OSP_SHIPMENT_PUB.SHIP_HEADER_REC_TYPE,
54 	p_module_type    IN            VARCHAR2 ,
55         x_header_rec     OUT NOCOPY OE_ORDER_PUB.HEADER_REC_TYPE,
56 	x_header_val_rec OUT NOCOPY OE_ORDER_PUB.HEADER_VAL_REC_TYPE);
57 
58 PROCEDURE Convert_Line_Tbl(
59 	p_line_tbl       IN AHL_OSP_SHIPMENT_PUB.SHIP_LINE_TBL_TYPE,
60 	p_module_type    IN            VARCHAR2 ,
61 	x_line_tbl       OUT NOCOPY OE_ORDER_PUB.LINE_TBL_TYPE,
62 	x_line_val_tbl   OUT NOCOPY OE_ORDER_PUB.LINE_VAL_TBL_TYPE,
63 	x_lot_serial_tbl OUT NOCOPY OE_ORDER_PUB.LOT_SERIAL_TBL_TYPE,
64     x_del_oe_lines_tbl OUT NOCOPY SHIP_ID_TBL_TYPE);
65 
66 PROCEDURE Process_Line_Tbl(
67         p_osp_order_id     IN NUMBER,
68         p_operation_flag   IN VARCHAR,
69         p_module_type    IN            VARCHAR2,
70 	p_x_line_tbl       IN OUT NOCOPY  AHL_OSP_SHIPMENT_PUB.SHIP_LINE_TBL_TYPE );
71 
72 PROCEDURE Update_OSP_Order(
73 	p_osp_order_id   IN NUMBER,
74         p_oe_header_id   IN NUMBER
75        );
76 
77 PROCEDURE Delete_OSP_Order(
78         p_oe_header_id   IN NUMBER
79        );
80 
81 PROCEDURE Update_OSP_Order_Lines(
82 	p_osp_order_id  IN NUMBER,
83       	p_item_instance_id   IN NUMBER,
84         p_oe_ship_line_id       IN NUMBER,
85         p_oe_return_line_id     IN NUMBER
86        );
87 
88 
89 PROCEDURE Update_OSP_Order_Lines(
90 	p_osp_order_id  IN NUMBER,
91 	p_osp_line_id   IN NUMBER,
92         p_oe_ship_line_id       IN NUMBER,
93         p_oe_return_line_id     IN NUMBER
94        );
95 
96 -- yazhou 10-Apr-2006 starts
97 -- Bug fix #4998349
98 
99 PROCEDURE Update_OSP_Line_Exch_Instance(
100 	p_osp_order_id   IN NUMBER,
101     p_osp_line_id    IN NUMBER,
102     p_exchange_instance_id   IN NUMBER
103       );
104 -- yazhou 10-Apr-2006 ends
105 
106 PROCEDURE Delete_OE_Lines(p_oe_line_id       IN NUMBER);
107 
108 --Commented by mpothuku on 05-Feb-2007 to implement the Osp Receiving ER
109 /*
110 -- Create IB Sub transaction for a order line based on osp_order_type and
111 -- line type.
112 PROCEDURE Create_IB_Transaction(
113     p_init_msg_list          IN            VARCHAR2  := FND_API.G_FALSE,
114     p_commit                 IN            VARCHAR2  := FND_API.G_FALSE,
115     p_validation_level       IN            NUMBER    := FND_API.G_VALID_LEVEL_FULL,
116     x_return_status          OUT NOCOPY            VARCHAR2,
117     x_msg_count              OUT NOCOPY            NUMBER,
118     x_msg_data               OUT NOCOPY            VARCHAR2,
119     p_OSP_order_type         IN            VARCHAR2,
120     p_oe_line_type           IN            VARCHAR2,
121     p_oe_line_id             IN            NUMBER,
122     p_csi_instance_id        IN            NUMBER);
123 
124 
125 -- Delete IB sub transaction for a shipment line.
126 PROCEDURE Delete_IB_Transaction(
127     p_init_msg_list          IN            VARCHAR2  := FND_API.G_FALSE,
128     p_commit                 IN            VARCHAR2  := FND_API.G_FALSE,
129     p_validation_level       IN            NUMBER    := FND_API.G_VALID_LEVEL_FULL,
130     x_return_status          OUT NOCOPY            VARCHAR2,
131     x_msg_count              OUT NOCOPY            NUMBER,
132     x_msg_data               OUT NOCOPY            VARCHAR2,
133     p_oe_line_id             IN            NUMBER);
134 */
135 --mpothuku End
136 
137 PROCEDURE Convert_Ship_Only_Lines(
138     p_osp_order_id        IN         NUMBER,
139     p_old_order_type_code IN         VARCHAR2,
140     p_new_order_type_code IN         VARCHAR2);
141 
142 FUNCTION GET_INSTANCE_NUM_FROM_ID(p_instance_id IN NUMBER )
143    RETURN VARCHAR2;
144 FUNCTION get_user_profile_option_name(p_profile_option_name IN VARCHAR2 )
145    RETURN VARCHAR2;
146 
147 /***********************************************************************
148 Local Procedures for usage of the Process_Osp_SerialNum_Change API
149 ************************************************************************/
150 --------------------------------------------------------
151 -- Procedure to return lookup code  given the meaning --
152 --------------------------------------------------------
153 PROCEDURE Convert_To_LookupCode (p_lookup_type     IN   VARCHAR2,
154                                  p_lookup_meaning  IN   VARCHAR2,
155                                  x_lookup_code     OUT  NOCOPY VARCHAR2,
156                                  x_return_val      OUT  NOCOPY BOOLEAN)  IS
157 
158 	CURSOR fnd_lookup_csr (p_lookup_type     IN  VARCHAR2, p_lookup_meaning  IN  VARCHAR2)  IS
159 	SELECT lookup_code
160 	  FROM fnd_lookup_values_vl
161 	 WHERE lookup_type = p_lookup_type
162 	   AND meaning = p_lookup_meaning
163 	   AND TRUNC(SYSDATE) >= TRUNC(NVL(start_date_active, SYSDATE))
164 	   AND TRUNC(SYSDATE) < TRUNC(NVL(end_date_active, SYSDATE+1));
165 
166 	l_lookup_code   fnd_lookups.lookup_code%TYPE DEFAULT NULL;
167 	l_return_val    BOOLEAN  DEFAULT  TRUE;
168 
169   BEGIN
170 
171 	OPEN fnd_lookup_csr(p_lookup_type, p_lookup_meaning);
172 	FETCH  fnd_lookup_csr INTO l_lookup_code;
173 	IF (fnd_lookup_csr%NOTFOUND) THEN
174 		l_return_val := FALSE;
175 		l_lookup_code := NULL;
176 	END IF;
177 	CLOSE fnd_lookup_csr;
178 
179 	 x_lookup_code := l_lookup_code;
180 	 x_return_val  := l_return_val;
181 
182 END  Convert_To_LookupCode;
183 --
184 PROCEDURE Validate_SerialNumber(p_Inventory_id           IN  NUMBER,
185                                 p_Serial_Number          IN  VARCHAR2,
186                                 p_serial_number_control  IN  NUMBER,
187                                 p_serialnum_tag_code     IN  VARCHAR2,
188                                 p_concatenated_segments  IN  VARCHAR2) IS
189 
190   CURSOR mtl_serial_numbers_csr(c_Inventory_id  IN  NUMBER,
191                                 c_Serial_Number IN  VARCHAR2) IS
192   SELECT 1
193     FROM mtl_serial_numbers
194    WHERE inventory_item_id = c_Inventory_id
195      AND Serial_Number = c_Serial_Number;
196 
197   l_junk       VARCHAR2(1);
198 
199 BEGIN
200 
201   -- Validate serial number.(1 = No serial number control; 2 = Pre-defined;
202   --                         3 = Dynamic Entry at inventory receipt.)
203   IF (nvl(p_serial_number_control,0) IN (2,5,6)) THEN
204     -- serial number is mandatory.
205     IF (p_Serial_Number IS NULL) OR (p_Serial_Number = FND_API.G_MISS_CHAR) THEN
206         FND_MESSAGE.Set_Name('AHL','AHL_PRD_SERIAL_NULL');
207         FND_MESSAGE.Set_Token('INV_ITEM',p_concatenated_segments);
208         FND_MSG_PUB.ADD;
209         --dbms_output.put_line('Serial Number is null');
210     /*
211     ELSE
212         -- If serial tag code = INVENTORY  then validate serial number against inventory.
213         IF (p_serialnum_tag_code = 'INVENTORY') THEN
214           OPEN  mtl_serial_numbers_csr(p_Inventory_id,p_Serial_Number);
215           FETCH mtl_serial_numbers_csr INTO l_junk;
216           IF (mtl_serial_numbers_csr%NOTFOUND) THEN
217              FND_MESSAGE.Set_Name('AHL','AHL_PRD_SERIAL_INVALID');
218              FND_MESSAGE.Set_Token('SERIAL',p_Serial_Number);
219              FND_MESSAGE.Set_Token('INV_ITEM',p_concatenated_segments);
220              FND_MSG_PUB.ADD;
221              --dbms_output.put_line('Serial Number does not exist in master ');
222           END IF;
223           CLOSE mtl_serial_numbers_csr;
224         END IF;
225     */
226     END IF;
227   ELSE
228      -- if not serialized item, then serial number must be null.
229      IF (p_Serial_Number <> FND_API.G_MISS_CHAR) AND (p_Serial_Number IS NOT NULL) THEN
230         FND_MESSAGE.Set_Name('AHL','AHL_PRD_SERIAL_NOTNULL');
231         FND_MESSAGE.Set_Token('SERIAL',p_Serial_Number);
232         FND_MESSAGE.Set_Token('INV_ITEM',p_concatenated_segments);
233         FND_MSG_PUB.ADD;
234         --dbms_output.put_line('Serial Number is not null');
235      END IF;
236 
237   END IF; /* for serial number control */
238 END Validate_SerialNumber;
239 
240 /***********************************************************************
241 End Local Procedures for usage of the Process_Osp_SerialNum_Change API
242 ************************************************************************/
243 
244 ------------------------
245 -- Define Procedures --
246 ------------------------
247 -- Start of Comments --
248 --  Procedure name    : Process_Order
249 --  Type              : Public
250 --  Function          : For one Shipment Header and a set of Shipment
251 -- Lines, call 1) SO API 2) Update IB with IB trxns.
252 --  Pre-reqs    :
253 --  Parameters  :
254 --
255 --  Standard IN  Parameters :
256 --      p_api_version                   IN      NUMBER       Default  1.0
257 --      p_init_msg_list                 IN      VARCHAR2     Default  FND_API.G_TRUE
258 --      p_commit                        IN      VARCHAR2     Default  FND_API.G_FALSE
259 --      p_validation_level              IN      NUMBER       Default  FND_API.G_VALID_LEVEL_FULL
260 --  Standard OUT NOCOPY Parameters :
261 --      x_return_status                 OUT NOCOPY      VARCHAR2             Required
262 --      x_msg_count                     OUT NOCOPY      NUMBER               Required
263 --      x_msg_data                      OUT NOCOPY      VARCHAR2             Required
264 --
265 --  Process Order Parameters:
266 --       p_x_Header_rec          IN OUT NOCOPY  Ship_Header_rec_type    Required
267 --         All parameters for SO Shipment Header
268 --       p_x_Lines_tbl        IN OUT NOCOPY  ship_line_tbl_type   Required
269 --         List of all parameters for shipment lines
270 --
271 --
272 --  Version :
273 --               Initial Version   1.0
274 --
275 --  End of Comments.
276 
277 PROCEDURE Process_Order (
278     p_api_version           IN        NUMBER    := 1.0,
279     p_init_msg_list         IN        VARCHAR2  := FND_API.G_TRUE,
280     p_commit                IN        VARCHAR2  := FND_API.G_FALSE,
281     p_validation_level      IN        NUMBER    := FND_API.G_VALID_LEVEL_FULL,
282     p_module_type           IN        VARCHAR2  := NULL,
283     p_x_header_rec         IN OUT NOCOPY    AHL_OSP_SHIPMENT_PUB.Ship_Header_Rec_Type,
284     p_x_lines_tbl 	   IN OUT NOCOPY    AHL_OSP_SHIPMENT_PUB.Ship_Line_Tbl_Type,
285     x_return_status         OUT NOCOPY            VARCHAR2,
286     x_msg_count             OUT NOCOPY            NUMBER,
287     x_msg_data              OUT NOCOPY            VARCHAR2)
288 IS
289 
290 -- yazhou 07-Aug-2006 starts
291 -- bug fix#5448191
292 -- Find all the OSP lines with no shipments associated, while for the same physical item
293 -- other OSP lines do have shipments associated
294 
295 CURSOR ahl_osp_line_no_ship_csr(p_osp_order_id IN NUMBER) IS
296 
297 Select osp_order_line_id,
298        OSP_LINE_NUMBER,
299        inventory_item_id,
300        inventory_org_id,
301        serial_number
302 from AHL_OSP_ORDER_LINES a
303 where OSP_ORDER_ID = p_osp_order_id
304 /* Modified by jaramana on January 11, 2008 to fix the Bug 5688387/5842229
305 changed the "and" operation in the line below to "Or" */
306 and (oe_ship_line_id is null or oe_return_line_id is null)
307 and serial_number is not null
308 and exists (select 1
309             from ahl_osp_order_lines
310             where osp_order_id = p_osp_order_id
311             and inventory_item_id = a.inventory_item_id
312             and inventory_org_id = a.inventory_org_id
313             and serial_number = a.serial_number
314             and (oe_ship_line_id is not null
315              or  oe_return_line_id is not null)
316              /* Modified by jaramana on January 11, 2008 to fix the Bug 5688387/5842229. Apart from a.osp_order_line_id we need
317              another line that exists with the same item and serial */
318             and osp_order_line_id <> a.osp_order_line_id);
319 
320 l_osp_line_no_ship_type ahl_osp_line_no_ship_csr%rowtype;
321 
322 CURSOR ahl_osp_line_ship_id_csr(p_osp_order_id IN NUMBER, p_inventory_item_id IN NUMBER, p_inventory_org_id IN NUMBER,
323 p_serial_number IN VARCHAR2) IS
324 
325 --Modified by jaramana on January 11, 2008 to fix the new issue raised by AE in the Bug 5688387/5842229
326 Select distinct nvl(oe_ship_line_id,-1) oe_ship_line_id, nvl(oe_return_line_id,-1) oe_return_line_id
327 from AHL_OSP_ORDER_LINES a
328 where OSP_ORDER_ID = p_osp_order_id
329 and (oe_ship_line_id is not null
330      or oe_return_line_id is not null)
331 and inventory_item_id = p_inventory_item_id
332 and inventory_org_id = p_inventory_org_id
333 and serial_number = p_serial_number
334 order by 1, 2 desc;
335 
336 l_osp_line_ship_id_type ahl_osp_line_ship_id_csr%rowtype;
337 
338 --mpothuku modified on 14-Sep-2007 to fix the Bug 6398921
339 CURSOR is_oe_item_IB_tracked(c_oe_line_id IN NUMBER, c_oe_org_id IN NUMBER) IS
340 SELECT 1
341   FROM mtl_system_items_b mtl,
342        oe_order_lines_all oel
343  WHERE oel.line_id = c_oe_line_id
344    AND mtl.inventory_item_id = oel.inventory_item_id
345    AND mtl.organization_id =  c_oe_org_id
346    AND nvl(mtl.comms_nl_trackable_flag,'N') = 'Y' ;
347 --mpothuku End
348 
349 
350 -- yazhou 07-Aug-2006 ends
351 
352 -- salogan added for bug 9722617 Begin
353 CURSOR is_osp_line_id_valid_csr(p_osp_line_id IN NUMBER, p_osp_order_id IN NUMBER) IS
354 SELECT 1
355   FROM ahl_osp_order_lines
356   WHERE osp_order_line_id = p_osp_line_id
357   AND osp_order_id = p_osp_order_id;
358 -- salogan added for bug 9722617 End
359 
360 --
361   l_api_name       CONSTANT VARCHAR2(30)  := 'Process_Order';
362   l_api_version    CONSTANT NUMBER        := 1.0;
363   L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.Process_Order';
364 
365 
366    l_header_rec 		   OE_ORDER_PUB.header_rec_type;
367    l_line_tbl 			   OE_ORDER_PUB.line_tbl_type;
368    l_header_val_rec        OE_ORDER_PUB.header_val_rec_type;
369    l_line_val_tbl          OE_ORDER_PUB.line_val_tbl_type;
370    l_lot_serial_tbl        OE_ORDER_PUB.lot_serial_tbl_type;
371    l_del_oe_lines_tbl      SHIP_ID_TBL_TYPE;
372    l_osp_order_id          NUMBER;
373    l_line_type             VARCHAR2(80);
374    l_msg_data              VARCHAR2(2000);
375    l_msg_index_out         NUMBER;
376    l_msg_count             NUMBER;
377    l_dummy                 NUMBER;
378 --
379 BEGIN
380 
381   -- Standard start of API savepoint
382   SAVEPOINT Process_Order_Pub;
383 
384   -- Initialize API return status to success
385   x_return_status := FND_API.G_RET_STS_SUCCESS;
386 
387   -- Standard call to check for call compatibility
388   IF NOT FND_API.Compatible_API_Call(l_api_version, p_api_version, l_api_name,
389                                      G_PKG_NAME) THEN
390     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
391   END IF;
392 
393   -- Initialize message list if p_init_msg_list is set to TRUE
394   IF FND_API.To_Boolean(p_init_msg_list) THEN
395     FND_MSG_PUB.Initialize;
396   END IF;
397 
398   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
399     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Entering Procedure');
400   END IF;
401 
402   -- Start processing
403 
404   IF (FND_PROFILE.VALUE('AHL_OSP_OE_MIXED_ID') IS NULL OR FND_PROFILE.VALUE('AHL_OSP_OE_RETURN_ID') = '') THEN
405     FND_MESSAGE.set_name('AHL', 'AHL_OSP_PROFILE_NULL');
406     --FND_MESSAGE.SET_TOKEN('PROFILE', 'AHL: OE Mixed Order Type ID');
407     FND_MESSAGE.SET_TOKEN('PROFILE', get_user_profile_option_name('AHL_OSP_OE_MIXED_ID'));
408     FND_MSG_PUB.ADD;
409     IF (FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
410       FND_LOG.MESSAGE(FND_LOG.LEVEL_UNEXPECTED, L_DEBUG_KEY, FALSE);
411     END IF;
412     RAISE Fnd_Api.g_exc_error;
413   END IF;
414 
415   IF (FND_PROFILE.VALUE('AHL_OSP_OE_SHIP_ONLY_ID') IS NULL OR FND_PROFILE.VALUE('AHL_OSP_OE_SHIP_ONLY_ID') = '' ) THEN
416     FND_MESSAGE.set_name('AHL', 'AHL_OSP_PROFILE_NULL');
417     --FND_MESSAGE.SET_TOKEN('PROFILE', 'AHL: OE Ship Only Line Type ID');
418     FND_MESSAGE.SET_TOKEN('PROFILE', get_user_profile_option_name('AHL_OSP_OE_SHIP_ONLY_ID'));
419     FND_MSG_PUB.ADD;
420     IF (FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
421       FND_LOG.MESSAGE(FND_LOG.LEVEL_UNEXPECTED, L_DEBUG_KEY, FALSE);
422     END IF;
423     RAISE Fnd_Api.g_exc_error;
424   END IF;
425 
426   IF (FND_PROFILE.VALUE('AHL_OSP_OE_RETURN_ID') IS NULL OR FND_PROFILE.VALUE('AHL_OSP_OE_RETURN_ID') = '') THEN
427     FND_MESSAGE.set_name('AHL', 'AHL_OSP_PROFILE_NULL');
428     --FND_MESSAGE.SET_TOKEN('PROFILE', 'AHL: OE Return Line Type ID');
429     FND_MESSAGE.SET_TOKEN('PROFILE', get_user_profile_option_name('AHL_OSP_OE_RETURN_ID'));
430     FND_MSG_PUB.ADD;
431     IF (FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
432       FND_LOG.MESSAGE(FND_LOG.LEVEL_UNEXPECTED, L_DEBUG_KEY, FALSE);
433     END IF;
434     RAISE Fnd_Api.g_exc_error;
435   END IF;
436 
437   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
438     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Got profile values, About to Convert_Header_rec');
439   END IF;
440 
441   Convert_Header_rec(p_header_rec  => p_x_header_rec,
442                      p_module_type => p_module_type,
443                      x_header_rec  => l_header_rec,
444                      x_header_val_rec => l_header_val_rec );
445 
446   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
447     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Completed Convert_Header_rec, About to Process_Line_Tbl');
448   END IF;
449 
450   Process_Line_Tbl(p_osp_order_id => p_x_header_rec.osp_order_id,
451                    p_operation_flag => p_x_header_rec.operation,
452   	           p_module_type => p_module_type,
453                    p_x_line_tbl  => p_x_lines_tbl);
454 
455   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
456     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Completed Process_Line_Tbl, About to Convert_Line_Tbl');
457   END IF;
458 
459   --salogan Added the following code to validate the OSP line number. Bug #9722617 Begin
460   IF(p_x_lines_tbl.count > 0) THEN
461     FOR i IN p_x_lines_tbl.FIRST..p_x_lines_tbl.LAST  LOOP
462       IF (p_x_lines_tbl(i).osp_line_flag <> 'N') THEN
463         OPEN is_osp_line_id_valid_csr(p_x_lines_tbl(i).osp_line_id, p_x_lines_tbl(i).osp_order_id);
464         FETCH is_osp_line_id_valid_csr into l_dummy;
465         IF (is_osp_line_id_valid_csr%NOTFOUND) THEN
466             FND_MESSAGE.SET_NAME(G_APP_NAME,'AHL_OSP_LN_INV_LOV');
467             FND_MSG_PUB.ADD;
468             IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
469                 FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY,
470                 'Invalid OSP Line id : '||p_x_lines_tbl(i).osp_line_id||' for OSP Order id : '||p_x_lines_tbl(i).osp_order_id);
471             END IF;
472             CLOSE is_osp_line_id_valid_csr;
473             RAISE FND_API.G_EXC_ERROR;
474         END IF;
475         CLOSE is_osp_line_id_valid_csr;
476       ELSE
477         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
478             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY,
479             'OSP based line flag is '||p_x_lines_tbl(i).osp_line_flag||'. Hence skipping the check for OSP Order id : '||p_x_lines_tbl(i).osp_order_id);
480         END IF;
481       END IF;
482     END LOOP;
483   END IF;
484   --salogan Changes for Bug #9722617 End
485 
486   Convert_Line_Tbl(p_line_tbl  => p_x_lines_tbl,
487                    p_module_type => p_module_type,
488                    x_line_tbl  => l_line_tbl,
489                    x_line_val_tbl => l_line_val_tbl,
490                    x_lot_serial_tbl => l_lot_serial_tbl,
491                    x_del_oe_lines_tbl => l_del_oe_lines_tbl  -- Additional Parameter to get the lines to be deleted
492                    );
493 
494   /*
495   Added by jaramana on January 11, 2008 to raise any validation errors that may have been accumulated by the AHL_OSP_ORDERS_PVT procedure
496   We do not have any warning messages hence if the message count is > 0 then it means there are validation errors and since
497   we call a Public API in this procedure, its better we throw the error here itself.
498   */
499   l_msg_count := FND_MSG_PUB.count_msg;
500   IF l_msg_count > 0 THEN
501     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
502       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Error Count Before calling OE_ORDER_GRP.PROCESS_ORDER: '||l_msg_count);
503     END IF;
504     RAISE  FND_API.G_EXC_ERROR;
505   END IF;
506 
507   -- jaramana End
508 
509   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
510     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Completed Convert_Line_Tbl, About to OE_ORDER_GRP.PROCESS_ORDER');
511   END IF;
512 
513   --OE_ORDER_GRP uses its own message stack OE_MSG_PUB, so we should pass p_init_msg_list as true to this API
514   --Note that this also does an FND_MSG_PUB.initialize along with clearing its own error stack
515   OE_ORDER_GRP.PROCESS_ORDER(
516     p_api_version_number  => 1.0,
517     p_init_msg_list       => FND_API.G_TRUE,
518     x_return_status       => x_return_status,
519     x_msg_count           => x_msg_count,
520     x_msg_data            => x_msg_data,
521     p_header_rec	      => l_header_rec,
522     p_header_val_rec      => l_header_val_rec,
523     p_line_tbl            => l_line_tbl,
524     p_line_val_tbl        => l_line_val_tbl,
525     p_lot_serial_tbl      => l_lot_serial_tbl,
526     x_header_rec          => x_header_rec,
527     x_header_val_rec      => x_header_val_rec,
528     x_Header_Adj_tbl       =>  x_Header_Adj_tbl,
529     x_Header_Adj_val_tbl   =>  x_Header_Adj_val_tbl,
530     x_Header_price_Att_tbl =>  x_Header_price_Att_tbl,
531     x_Header_Adj_Att_tbl   => x_Header_Adj_Att_tbl,
532     x_Header_Adj_Assoc_tbl =>  x_Header_Adj_Assoc_tbl,
533     x_Header_Scredit_tbl    =>   x_Header_Scredit_tbl,
534     x_Header_Scredit_val_tbl =>    x_Header_Scredit_val_tbl,
535     x_line_tbl               =>     x_line_tbl      ,
536     x_line_val_tbl           =>    x_line_val_tbl ,
537     x_Line_Adj_tbl           =>   x_Line_Adj_tbl    ,
538     x_Line_Adj_val_tbl       =>  x_Line_Adj_val_tbl,
539     x_Line_price_Att_tbl     =>   x_Line_price_Att_tbl,
540     x_Line_Adj_Att_tbl       =>  x_Line_Adj_Att_tbl ,
541     x_Line_Adj_Assoc_tbl     =>  x_Line_Adj_Assoc_tbl,
542     x_Line_Scredit_tbl       => x_Line_Scredit_tbl ,
543     x_Line_Scredit_val_tbl   =>  x_Line_Scredit_val_tbl,
544     x_Lot_Serial_tbl         => x_Lot_Serial_tbl  ,
545     x_Lot_Serial_val_tbl     => x_Lot_Serial_val_tbl   ,
546     x_action_request_tbl     => x_action_request_tbl  );
547 
548 
549   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
550     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Completed OE_ORDER_GRP.PROCESS_ORDER, x_return_status = ' || x_return_status);
551   END IF;
552 
553   IF (x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
554     FOR i IN 1..x_msg_count LOOP
555       OE_MSG_PUB.Get(p_msg_index => i,
556                      p_encoded => FND_API.G_FALSE,
557                      p_data    => l_msg_data,
558                      p_msg_index_out => l_msg_index_out);
559       fnd_msg_pub.add_exc_msg(p_pkg_name       => 'OE_ORDER_PUB',
560                               p_procedure_name => 'processOrder',
561                               p_error_text     => substr(l_msg_data,1,240));
562       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
563            FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'OE:Err Msg '||i||'.' || l_msg_data);
564       END IF;
565 
566     END LOOP;
567   END IF;
568 
569   IF (x_return_status = FND_API.G_RET_STS_ERROR) THEN
570     RAISE FND_API.G_EXC_ERROR;
571   ELSIF (x_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
572     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
573   /*
574   Modified by jaramana on January 11, 2008 to fix the Bug 5935388/6504122
575   If success is returned by the Public API, clear the message stack to eat up any warning messages from OM API.
576   Otherwise, OAExceptionUtils.checkErrors will consider these as errors.
577   Also note that had there been any validation errors that had been accumulated we would have thrown before the call to
578   OE_ORDER_GRP.PROCESS_ORDER
579   */
580 
581   ELSIF(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
582     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
583       FOR i IN 1..FND_MSG_PUB.count_msg LOOP
584         FND_MSG_PUB.get (
585             p_msg_index      => i,
586             p_encoded        => FND_API.G_FALSE,
587             p_data           => l_msg_data,
588             p_msg_index_out  => l_msg_index_out );
589         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY,'OE: Warning ' || i || ': ' || l_msg_data);
590       END LOOP;
591     END IF;
592     FND_MSG_PUB.Initialize;
593   -- jaramana End
594   END IF;
595 
596   IF (p_x_header_rec.operation = 'C') THEN
597     p_x_header_rec.header_id := x_header_rec.header_id;
598 
599     --Update AHL_OSP_ORDERS tables with ids/new id
600     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
601       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'About to call Update_OSP_Order');
602     END IF;
603     Update_OSP_Order(p_osp_order_id => p_x_header_rec.osp_order_id,
604                      p_oe_header_id => p_x_header_rec.header_id );
605   ELSIF (p_x_header_rec.operation = 'D') THEN
606      --Update AHL_OSP_ORDERS/OSP_ORDER_LINES tables with ids/new id
607     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
608       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'About to call Delete_OSP_Order');
609     END IF;
610     Delete_OSP_Order(p_oe_header_id => p_x_header_rec.header_id);
611   END IF;
612 
613   -- Handle Shipment Line Deletions
614   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
615     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_del_oe_lines_tbl.COUNT = ' || l_del_oe_lines_tbl.COUNT);
616   END IF;
617   IF (l_del_oe_lines_tbl.COUNT > 0) THEN
618     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
619       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'About to call Delete_Cancel_Order');
620     END IF;
621     Delete_Cancel_Order (
622           p_api_version              => 1.0,
623           p_init_msg_list            => FND_API.G_FALSE, -- Don't initialize the Message List
624           p_commit                   => FND_API.G_FALSE, -- Don't commit independently
625           p_oe_header_id             => null,  -- Not deleting the shipment header: Only the lines
626           p_oe_lines_tbl             => l_del_oe_lines_tbl,  -- Lines to be deleted/Cancelled
627           p_cancel_flag              => FND_API.G_FALSE,  -- Do Deletes if possible, Cancels if not
628           x_return_status            => x_return_status ,
629           x_msg_count                => x_msg_count ,
630           x_msg_data                 => x_msg_data
631       );
632     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
633       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Returned from Delete_Cancel_Order, x_return_status = ' || x_return_status);
634     END IF;
635     IF (x_return_status = FND_API.G_RET_STS_ERROR) THEN
636       RAISE FND_API.G_EXC_ERROR;
637     ELSIF (x_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
638       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
639     END IF;
640   END IF;   -- Delete Line Count > 0
641 
642   -- Handle New Shipment Lines
643   IF (p_x_lines_tbl.COUNT > 0) THEN
644     FOR i IN p_x_lines_tbl.FIRST..p_x_lines_tbl.LAST  LOOP
645       IF (p_x_lines_tbl(i).operation = 'C') THEN
646         -- Update the line_id
647         p_x_lines_tbl(i).line_id := x_line_tbl(i).line_id;
648         IF (p_x_lines_tbl(i).line_type_id = FND_PROFILE.VALUE('AHL_OSP_OE_SHIP_ONLY_ID')) THEN
649           l_line_type := 'ORDER';
650         ELSIF (p_x_lines_tbl(i).line_type_id = FND_PROFILE.VALUE('AHL_OSP_OE_RETURN_ID')) THEN
651           l_line_type := 'RETURN';
652         END IF;
653 
654         IF p_x_lines_tbl(i).csi_item_instance_id IS NOT NULL THEN
655 
656           IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
657             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'About to call Create_IB_Transaction');
658           END IF;
659         /*
660            Modified by jaramana on January 11, 2008 for fixing the Bug 5935388/6504122
661            The "Create Shipment" action passes p_init_msg_list as TRUE as it is the starting point.
662            p_init_msg_list is passed as FALSE otherwise. So the "Create Shipment" action is flushing the
663            warning messages generated by OE_ORDER_GRP.PROCESS_ORDER. So the shipment is created without
664            any errors (Warning converted to errors by OAExceptionUtils.checkErrors in the absence of
665            certain tokens).
666         */
667           Create_IB_Transaction(
668               p_init_msg_list         => FND_API.G_FALSE, --p_init_msg_list,
669               p_commit                => p_commit,
670               p_validation_level      => p_validation_level,
671               x_return_status         => x_return_status,
672               x_msg_count             => x_msg_count,
673               x_msg_data              => x_msg_data,
674               p_osp_order_type        => p_x_lines_tbl(i).order_type,
675               p_oe_line_type          => l_line_type,
676               p_oe_line_id            => p_x_lines_tbl(i).line_id,
677               p_csi_instance_id       => p_x_lines_tbl(i).csi_item_instance_id);
678           IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
679             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Return status from Create_IB_Transaction: ' || x_return_status);
680           END IF;
681           IF (x_return_status = FND_API.G_RET_STS_ERROR) THEN
682             RAISE FND_API.G_EXC_ERROR;
683           ELSIF (x_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
684             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
685           END IF;
686 
687           --Update OSP Order Lines with the new line_id
688           --FIX LINE TYPE ID validation
689 
690           IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
691             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'About to call Update_OSP_Order_Lines');
692           END IF;
693           IF (p_x_lines_tbl(i).line_type_id = FND_PROFILE.VALUE('AHL_OSP_OE_SHIP_ONLY_ID')) THEN
694             Update_OSP_Order_Lines(
695                 p_osp_order_id      => p_x_lines_tbl(i).osp_order_id,
696                 p_item_instance_id  => p_x_lines_tbl(i).csi_item_instance_id,
697                 p_oe_ship_line_id   => p_x_lines_tbl(i).line_id,
698                 p_oe_return_line_id  =>  FND_API.G_MISS_NUM);
699           ELSIF  (p_x_lines_tbl(i).line_type_id = FND_PROFILE.VALUE('AHL_OSP_OE_RETURN_ID')) THEN
700             IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
701               FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Calling Update_OSP_Order_Lines with p_oe_return_line_id = ' || p_x_lines_tbl(i).line_id );
702             END IF;
703             Update_OSP_Order_Lines(
704                 p_osp_order_id      => p_x_lines_tbl(i).osp_order_id,
705                 p_item_instance_id  => p_x_lines_tbl(i).csi_item_instance_id,
706                 p_oe_ship_line_id   => FND_API.G_MISS_NUM ,
707                 p_oe_return_line_id => p_x_lines_tbl(i).line_id);
708           END IF;
709 
710         ELSE -- instance id is null
711 
712           IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
713             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'About to call Update_OSP_Order_Lines');
714           END IF;
715           IF p_x_lines_tbl(i).osp_line_id IS NOT NULL THEN
716             IF (p_x_lines_tbl(i).line_type_id = FND_PROFILE.VALUE('AHL_OSP_OE_SHIP_ONLY_ID')) THEN
717               Update_OSP_Order_Lines(
718                   p_osp_order_id      => p_x_lines_tbl(i).osp_order_id,
719                   p_osp_line_id  => p_x_lines_tbl(i).osp_line_id,
720                   p_oe_ship_line_id   => p_x_lines_tbl(i).line_id,
721                   p_oe_return_line_id  =>  FND_API.G_MISS_NUM);
722             ELSIF  (p_x_lines_tbl(i).line_type_id = FND_PROFILE.VALUE('AHL_OSP_OE_RETURN_ID')) THEN
723               --mpothuku modified on 14-Sep-2007 to fix the Bug 6398921
724               IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
725                 FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'p_x_lines_tbl(i).order_type :' ||p_x_lines_tbl(i).order_type);
726                 FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'p_x_lines_tbl(i).line_id :' ||p_x_lines_tbl(i).line_id);
727                 FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'p_x_lines_tbl(i).ship_from_org_id :' ||p_x_lines_tbl(i).ship_from_org_id);
728               END IF;
729               IF(p_x_lines_tbl(i).order_type = AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_EXCHANGE) THEN
730                 /*
731                 For exchange orders, the return line's sub-transaction record is to be created by ourselves even though
732                 the instance is not updated as part of the order line's exchange instance. Otherwise OM creates it with
733                 the default sub-transction and not use the AHL IB profile.
734                 We check whether the OE Line item is IB tracked on the receiving org and, if so create the
735                 Installation details with the instance as null.
736                 */
737                 OPEN is_oe_item_IB_tracked(p_x_lines_tbl(i).line_id, p_x_lines_tbl(i).ship_from_org_id);
738                 FETCH is_oe_item_IB_tracked into l_dummy;
739                 IF(is_oe_item_IB_tracked%FOUND) THEN
740                   CLOSE is_oe_item_IB_tracked;
741                   Create_IB_Transaction(
742                       p_init_msg_list         => p_init_msg_list,
743                       p_commit                => p_commit,
744                       p_validation_level      => p_validation_level,
745                       x_return_status         => x_return_status,
746                       x_msg_count             => x_msg_count,
747                       x_msg_data              => x_msg_data,
748                       p_osp_order_type        => p_x_lines_tbl(i).order_type,
749                       p_oe_line_type          => l_line_type,
750                       p_oe_line_id            => p_x_lines_tbl(i).line_id,
751                       p_csi_instance_id       => NULL);
752                   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
753                     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Return status from Create_IB_Transaction: ' || x_return_status);
754                   END IF;
755                   IF (x_return_status = FND_API.G_RET_STS_ERROR) THEN
756                     RAISE FND_API.G_EXC_ERROR;
757                   ELSIF (x_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
758                     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
759                   END IF;
760                 ELSE
761                   CLOSE is_oe_item_IB_tracked;
762                 END IF;
763                 --mpothuku End
764               END IF;
765               IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
766                 FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Calling Update_OSP_Order_Lines with p_oe_return_line_id = ' || p_x_lines_tbl(i).line_id );
767               END IF;
768               Update_OSP_Order_Lines(
769                   p_osp_order_id      => p_x_lines_tbl(i).osp_order_id,
770                   p_osp_line_id  => p_x_lines_tbl(i).osp_line_id,
771                   p_oe_ship_line_id   => FND_API.G_MISS_NUM ,
772                   p_oe_return_line_id => p_x_lines_tbl(i).line_id);
773             END IF;
774           END IF; -- OSP Line Id not null
775         END IF; -- if instance id is not null
776 
777 /*
778       -- July 23, 2003: Deletion of lines should go through the Delete_Cancel_Order API
779       -- Not through the regular process.
780       -- This is to support the Post-Shipment Conversion process where deletion of shipment (return) lines
781       -- from the UI (after the order has been booked) is necessary.
782       -- If the shipment is booked, Delete_Cancel_Order just zeroes out the ordered quantity.
783       -- If not, it actually deletes the shipment line.
784       ELSIF (p_x_lines_tbl(i).operation = 'D') THEN
785         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
786           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'About to call Delete_IB_Transaction');
787         END IF;
788         Delete_IB_Transaction(
789            p_init_msg_list         => p_init_msg_list,
790            p_commit                => p_commit,
791            p_validation_level      => p_validation_level,
792            x_return_status         => x_return_status,
793            x_msg_count             => x_msg_count,
794            x_msg_data              => x_msg_data,
795            p_oe_line_id            => p_x_lines_tbl(i).line_id );
796 
797         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
798           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Return status from Delete_IB_Transaction: ' || x_return_status);
799         END IF;
800         IF (x_return_status = FND_API.G_RET_STS_ERROR) THEN
801           RAISE FND_API.G_EXC_ERROR;
802         ELSIF (x_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
803           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
804         END IF;
805 
806         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
807           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'About to call Delete_OE_Lines');
808         END IF;
809         --Remove all instances of Shipment Line from OSP_ORDER_LINES table
810         Delete_OE_Lines(p_oe_line_id => p_x_lines_tbl(i).line_id);
811 */
812       END IF;  -- If Operation = 'C'
813     END LOOP;  -- For all Create and Update Lines lines
814   END IF;  -- If Create/Update Line Count > 0
815 
816 
817 -- yazhou 07-Aug-2006 starts
818 -- bug fix#5448191
819 
820 -- When OSP lines already exist for a physical item and another OSP line is created
821 -- for this physical itme for a different service, no shipment will be created.
822 -- However, the corresponding oe_ship_line_id and oe_return_line_id should be associated to
823 -- the new OSP line
824 
825 
826   /* Modified by jaramana on January 11, 2008 to fix the Bug 5688387/5842229
827   p_x_header_rec will have the osp_order_id when
828   1. "Create Shipment" action from Edit Osp Order is used
829   2. If user is creating multiple services for the same physical item from the "Create Item Order Line" on the Edit Osp Order UI.
830   Note that in this case, AHL_OSP_ORDERS_PVT will not populate the p_x_lines_tbl if a shipment already exists for the first line
831 
832   p_x_lines_tbl will have the osp_order_id when "Create Shipment Line" UI is used for creating the ship lines.
833   In this case, the p_x_header_rec will not have been populated.
834   Instead of the l_osp_order_id we were directly using p_x_header_rec.osp_order_id before.
835   */
836 
837   l_osp_order_id := null;
838   l_osp_order_id := p_x_header_rec.osp_order_id;
839 
840   IF(l_osp_order_id is null AND p_x_lines_tbl.COUNT > 0) THEN
841     l_osp_order_id := p_x_lines_tbl(p_x_lines_tbl.FIRST).osp_order_id;
842   END IF;
843 
844   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
845     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Going to loop for multiple services for the Order = ' || l_osp_order_id );
846   END IF;
847 
848   IF(l_osp_order_id is not null) THEN
849 
850     FOR l_osp_line_no_ship_type IN ahl_osp_line_no_ship_csr(l_osp_order_id) LOOP
851 
852       OPEN ahl_osp_line_ship_id_csr(l_osp_order_id,l_osp_line_no_ship_type.inventory_item_id,
853                 l_osp_line_no_ship_type.inventory_org_id, l_osp_line_no_ship_type.serial_number);
854       FETCH ahl_osp_line_ship_id_csr into l_osp_line_ship_id_type;
855       CLOSE ahl_osp_line_ship_id_csr;
856 
857       -- Modified by jaramana on January 11, 2008 to fix the new issue raised by AE in the Bug 5688387/5842229
858       IF (l_osp_line_ship_id_type.oe_ship_line_id is not null and l_osp_line_ship_id_type.oe_ship_line_id = -1) THEN
859         l_osp_line_ship_id_type.oe_ship_line_id := NULL;
860       END IF;
861 
862       IF (l_osp_line_ship_id_type.oe_return_line_id is not null and l_osp_line_ship_id_type.oe_return_line_id = -1) THEN
863         l_osp_line_ship_id_type.oe_return_line_id := NULL;
864       END IF;
865 
866       IF (l_osp_line_ship_id_type.oe_ship_line_id is not null
867          OR l_osp_line_ship_id_type.oe_return_line_id is not null) THEN
868 
869         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
870           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Calling Update_OSP_Order_Lines with p_oe_ship_line_id = ' || l_osp_line_ship_id_type.oe_ship_line_id );
871           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Calling Update_OSP_Order_Lines with p_oe_return_line_id = ' || l_osp_line_ship_id_type.oe_return_line_id );
872         END IF;
873 
874         Update_OSP_Order_Lines(
875                       p_osp_order_id       => l_osp_order_id,
876                       p_osp_line_id        => l_osp_line_no_ship_type.osp_order_line_id,
877                       p_oe_ship_line_id    => l_osp_line_ship_id_type.oe_ship_line_id,
878                       p_oe_return_line_id  => l_osp_line_ship_id_type.oe_return_line_id);
879       END IF;
880 
881     END LOOP;
882   END IF;
883 
884 -- yazhou 07-Aug-2006 ends
885 
886   -- Standard check of p_commit
887   IF FND_API.To_Boolean(p_commit) THEN
888     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
889       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'About to commit work');
890     END IF;
891     COMMIT WORK;
892   END IF;
893 
894   -- Standard call to get message count and if count is 1, get message
895   FND_MSG_PUB.Count_And_Get
896     ( p_count => x_msg_count,
897       p_data  => x_msg_data,
898       p_encoded => fnd_api.g_false);
899   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
900     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'Exiting Procedure');
901   END IF;
902 
903 EXCEPTION
904  WHEN FND_API.G_EXC_ERROR THEN
905    x_return_status := FND_API.G_RET_STS_ERROR;
906    Rollback to Process_Order_Pub;
907    FND_MSG_PUB.count_and_get( p_count => x_msg_count,
908                               p_data  => x_msg_data,
909                               p_encoded => fnd_api.g_false);
910 
911  WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
912    x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
913    Rollback to Process_Order_Pub;
914    FND_MSG_PUB.count_and_get( p_count => x_msg_count,
915                               p_data  => x_msg_data,
916                                p_encoded => fnd_api.g_false);
917  WHEN OTHERS THEN
918     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
919     Rollback to Process_Order_Pub;
920        fnd_msg_pub.add_exc_msg(p_pkg_name       => G_PKG_NAME,
921                                p_procedure_name => 'Process_Order',
922                                p_error_text     => SQLERRM);
923 
924     FND_MSG_PUB.count_and_get( p_count => x_msg_count,
925                                p_data  => x_msg_data,
926                                 p_encoded => fnd_api.g_false);
927 END Process_Order;
928 
929 
930 -------------------------------------------------------------
931 -- Start of Comments --
932 --  Procedure name    : Book_Order
933 --  Type              : Public
934 --  Function          : For multiple Shipment Headers, book the orders
935 --  Pre-reqs    :
936 --  Parameters  :
937 --
938 --  Standard IN  Parameters :
939 --      p_api_version                   IN      NUMBER       Default  1.0
940 --      p_init_msg_list                 IN      VARCHAR2     Default  FND_API.G_TRUE
941 --      p_commit                        IN      VARCHAR2     Default  FND_API.G_FALSE
942 --      p_validation_level              IN      NUMBER       Default  FND_API.G_VALID_LEVEL_FULL
943 --  Standard OUT Parameters :
944 --      x_return_status                 OUT NOCOPY     VARCHAR2             Required
945 --      x_msg_count                     OUT NOCOPY     NUMBER               Required
946 --      x_msg_data                      OUT NOCOPY     VARCHAR2             Required
947 --
948 --  Book_Order Parameters:
949 --       p_oe_header_tbl          IN NUMBER TABLE
950 --         The array of header_id for the Shipment Headers
951 --
952 --  Version :
953 --               Initial Version   1.0
954 --
955 --  End of Comments.
956 
957 PROCEDURE Book_Order (
958     p_api_version           IN        NUMBER    := 1.0,
959     p_init_msg_list         IN        VARCHAR2  := FND_API.G_TRUE,
960     p_commit                IN        VARCHAR2  := FND_API.G_FALSE,
961     p_validation_level      IN        NUMBER    := FND_API.G_VALID_LEVEL_FULL,
962     p_oe_header_tbl         IN        SHIP_ID_TBL_TYPE,
963     x_return_status         OUT NOCOPY        VARCHAR2,
964     x_msg_count             OUT NOCOPY        NUMBER,
965     x_msg_data              OUT NOCOPY        VARCHAR2 ) IS
966 --
967    L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.Book_Order';
968    l_action_request_tbl    OE_ORDER_PUB.request_tbl_type;
969    l_action_request_rec    OE_ORDER_PUB.request_rec_type;
970    l_msg_data              VARCHAR2(2000);
971    l_msg_index_out         NUMBER;
972    l_msg_count             NUMBER;
973 --
974 BEGIN
975   SAVEPOINT Book_Order_Pub;
976 
977    -- Initialize message list if p_init_msg_list is set to TRUE
978   IF FND_API.To_Boolean(p_init_msg_list) THEN
979     FND_MSG_PUB.Initialize;
980   END IF;
981 
982   -- Initialize API return status to success
983   x_return_status := FND_API.G_RET_STS_SUCCESS;
984 
985   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
986     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Entering Procedure');
987   END IF;
988 
989   --LOOP throught all header ids
990  IF (p_oe_header_tbl.COUNT > 0) THEN
991   FOR i IN p_oe_header_tbl.FIRST..p_oe_header_tbl.LAST  LOOP
992     l_action_request_rec := OE_ORDER_PUB.G_MISS_REQUEST_REC;
993     l_action_request_rec.entity_code := OE_GLOBALS.G_ENTITY_HEADER;
994     l_action_request_rec.entity_id := p_oe_header_tbl(i);
995     l_action_request_rec.request_type := OE_GLOBALS.G_BOOK_ORDER;
996     l_action_request_tbl(i) := l_action_request_rec;
997   END LOOP;
998  END IF;
999 
1000   /*
1001   Added by jaramana on January 11, 2008 to raise any validation errors that may have been accumulated by the AHL_OSP_ORDERS_PVT procedure
1002   We do not have any warning messages hence if the message count is > 0 then it means there are validation errors and since
1003   we call a Public API in this procedure, its better we throw the error here itself.
1004   */
1005   l_msg_count := FND_MSG_PUB.count_msg;
1006   IF l_msg_count > 0 THEN
1007     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1008       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Error Count Before calling OE_ORDER_GRP.PROCESS_ORDER: '||l_msg_count);
1009     END IF;
1010     RAISE  FND_API.G_EXC_ERROR;
1011   END IF;
1012 
1013   -- jaramana End
1014 
1015   --OE_ORDER_GRP uses its own message stack OE_MSG_PUB, so we should pass p_init_msg_list as true to this API
1016   --Note that this also does an FND_MSG_PUB.initialize along with clearing its own error stack
1017   OE_ORDER_GRP.PROCESS_ORDER(
1018     p_api_version_number  => 1.0,
1019     p_init_msg_list       => FND_API.G_TRUE,
1020     x_return_status       => x_return_status,
1021     x_msg_count           => x_msg_count,
1022     x_msg_data            => x_msg_data,
1023     p_action_request_tbl  => l_action_request_tbl,
1024     x_header_rec          => x_header_rec,
1025     x_header_val_rec      => x_header_val_rec,
1026     x_Header_Adj_tbl       =>  x_Header_Adj_tbl,
1027     x_Header_Adj_val_tbl   =>  x_Header_Adj_val_tbl,
1028     x_Header_price_Att_tbl =>  x_Header_price_Att_tbl,
1029     x_Header_Adj_Att_tbl   => x_Header_Adj_Att_tbl,
1030     x_Header_Adj_Assoc_tbl =>  x_Header_Adj_Assoc_tbl,
1031     x_Header_Scredit_tbl    =>   x_Header_Scredit_tbl,
1032     x_Header_Scredit_val_tbl =>    x_Header_Scredit_val_tbl,
1033     x_line_tbl               =>     x_line_tbl      ,
1034     x_line_val_tbl           =>    x_line_val_tbl ,
1035     x_Line_Adj_tbl           =>   x_Line_Adj_tbl    ,
1036     x_Line_Adj_val_tbl       =>  x_Line_Adj_val_tbl,
1037     x_Line_price_Att_tbl     =>   x_Line_price_Att_tbl,
1038     x_Line_Adj_Att_tbl       =>  x_Line_Adj_Att_tbl ,
1039     x_Line_Adj_Assoc_tbl     =>  x_Line_Adj_Assoc_tbl,
1040     x_Line_Scredit_tbl       => x_Line_Scredit_tbl ,
1041     x_Line_Scredit_val_tbl   =>  x_Line_Scredit_val_tbl,
1042     x_Lot_Serial_tbl         => x_Lot_Serial_tbl  ,
1043     x_Lot_Serial_val_tbl     => x_Lot_Serial_val_tbl   ,
1044     x_action_request_tbl     => x_action_request_tbl  );
1045 
1046   IF (x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
1047    FOR i IN 1..x_msg_count LOOP
1048      OE_MSG_PUB.Get( 	p_msg_index => i,
1049 			p_encoded => FND_API.G_FALSE,
1050 			p_data    => l_msg_data,
1051 			p_msg_index_out => l_msg_index_out);
1052      fnd_msg_pub.add_exc_msg(p_pkg_name       => 'OE_ORDER_PUB',
1053                              p_procedure_name => 'bookOrder',
1054                              p_error_text     => substr(l_msg_data,1,240));
1055       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1056            FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'OE:Err Msg '||i||'.' || l_msg_data);
1057       END IF;
1058 
1059     END LOOP;
1060   END IF;
1061 
1062   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1063     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Return status at the end of all processing: ' || x_return_status);
1064   END IF;
1065 
1066   IF (x_return_status = FND_API.G_RET_STS_ERROR) THEN
1067     RAISE FND_API.G_EXC_ERROR;
1068   ELSIF (x_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
1069     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1070   /*
1071   Modified by jaramana on January 11, 2008 to fix the Bug 5935388/6504122
1072   If success is returned by the Public API, clear the message stack to eat up any warning messages from OM API.
1073   Otherwise, OAExceptionUtils.checkErrors will consider these as errors
1074   Also note that had there been any validation errors that had been accumulated we would have thrown before the call to
1075   OE_ORDER_GRP.PROCESS_ORDER
1076   */
1077   ELSIF(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
1078     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1079       FOR i IN 1..FND_MSG_PUB.count_msg LOOP
1080         FND_MSG_PUB.get (
1081             p_msg_index      => i,
1082             p_encoded        => FND_API.G_FALSE,
1083             p_data           => l_msg_data,
1084             p_msg_index_out  => l_msg_index_out );
1085         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY,'OE: Warning ' || i || ': ' || l_msg_data);
1086       END LOOP;
1087     END IF;
1088     FND_MSG_PUB.Initialize;
1089   -- jaramana End
1090   END IF;
1091 
1092   -- Standard check of p_commit
1093   IF FND_API.To_Boolean(p_commit) THEN
1094     COMMIT WORK;
1095   END IF;
1096 
1097   -- Standard call to get message count and if count is 1, get message
1098   FND_MSG_PUB.Count_And_Get
1099     ( p_count => x_msg_count,
1100       p_data  => x_msg_data,
1101       p_encoded => fnd_api.g_false);
1102 
1103   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1104     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'Exiting Procedure');
1105   END IF;
1106 
1107 EXCEPTION
1108  WHEN FND_API.G_EXC_ERROR THEN
1109    x_return_status := FND_API.G_RET_STS_ERROR;
1110    Rollback to Book_Order_Pub;
1111    FND_MSG_PUB.count_and_get( p_count => x_msg_count,
1112                               p_data  => x_msg_data,
1113                               p_encoded => fnd_api.g_false);
1114 
1115  WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1116    x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1117    Rollback to Book_Order_Pub;
1118    FND_MSG_PUB.count_and_get( p_count => x_msg_count,
1119                               p_data  => x_msg_data,
1120                                p_encoded => fnd_api.g_false);
1121  WHEN OTHERS THEN
1122     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1123     Rollback to Book_Order_Pub;
1124        fnd_msg_pub.add_exc_msg(p_pkg_name       => G_PKG_NAME,
1125                                p_procedure_name => 'Book_Order',
1126                                p_error_text     => SQLERRM);
1127 
1128     FND_MSG_PUB.count_and_get( p_count => x_msg_count,
1129                                p_data  => x_msg_data,
1130                                 p_encoded => fnd_api.g_false);
1131 END Book_Order;
1132 
1133 -------------------------------------------------------------
1134 -- Start of Comments --
1135 --  Procedure name    : Delete_Cancel_Order
1136 --  Type              : Public
1137 --  Function          : For one Shipment Header and a set of Shipment
1138 -- Lines, Cancel if booked. Process either the header or the lines
1139 --  Pre-reqs    :
1140 --  Parameters  :
1141 --
1142 --  Standard IN  Parameters :
1143 --      p_api_version                   IN      NUMBER       Default  1.0
1144 --      p_init_msg_list                 IN      VARCHAR2     Default  FND_API.G_TRUE
1145 --      p_commit                        IN      VARCHAR2     Default  FND_API.G_FALSE
1146 --      p_validation_level              IN      NUMBER       Default  FND_API.G_VALID_LEVEL_FULL
1147 --  Standard OUT Parameters :
1148 --      x_return_status                 OUT NOCOPY      VARCHAR2             Required
1149 --      x_msg_count                     OUT NOCOPY      NUMBER               Required
1150 --      x_msg_data                      OUT NOCOPY      VARCHAR2             Required
1151 --
1152 --  Delete_Cancel_Order Parameters:
1153 --       p_oe_header_id          IN NUMBER
1154 --         The header_id for the Shipment Header
1155 --       p_oe_lines_tbl        IN   ship_id_tbl_type
1156 --         All shipment line ids for delete or cancel
1157 --       p_cancel_flag         IN VARCHAR2
1158 --         If true, only do cancels, no deletes.
1159 --
1160 --
1161 --  Version :
1162 --               Initial Version   1.0
1163 --
1164 --  End of Comments.
1165 
1166 PROCEDURE Delete_Cancel_Order (
1167     p_api_version           IN        NUMBER    := 1.0,
1168     p_init_msg_list         IN        VARCHAR2  := FND_API.G_TRUE,
1169     p_commit                IN        VARCHAR2  := FND_API.G_FALSE,
1170     p_validation_level      IN        NUMBER    := FND_API.G_VALID_LEVEL_FULL,
1171     p_oe_header_id          IN        NUMBER,
1172     p_oe_lines_tbl 	    IN        SHIP_ID_TBL_TYPE,
1173     p_cancel_flag           IN        VARCHAR2  := FND_API.G_FALSE,
1174     x_return_status         OUT NOCOPY            VARCHAR2,
1175     x_msg_count             OUT NOCOPY            NUMBER,
1176     x_msg_data              OUT NOCOPY            VARCHAR2)
1177 IS
1178 --
1179  CURSOR ahl_is_header_deleteable_csr(p_header_id IN NUMBER) IS
1180     SELECT order_type_id
1181     FROM oe_order_headers_all
1182     WHERE header_id = p_header_id
1183      AND booked_flag = 'N';
1184 
1185  CURSOR ahl_is_line_deleteable_csr(p_line_id IN NUMBER) IS
1186     SELECT 1
1187     FROM oe_order_lines_all
1188     WHERE line_id = p_line_id
1189      AND shipped_quantity IS NULL
1190      AND booked_flag = 'N';
1191 
1192 --
1193    L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.Delete_Cancel_Order';
1194    l_header_rec     OE_ORDER_PUB.header_rec_type;
1195    l_line_rec       OE_ORDER_PUB.line_rec_type;
1196    l_line_tbl       OE_ORDER_PUB.line_tbl_type;
1197    l_osp_order_id   NUMBER;
1198    l_dummy          NUMBER;
1199    l_type           VARCHAR2(1);
1200    l_order_type_id  NUMBER;
1201    l_msg_data       VARCHAR2(2000);
1202    l_msg_index_out  NUMBER;
1203    l_msg_count      NUMBER;
1204 --
1205 BEGIN
1206 
1207   SAVEPOINT Delete_Cancel_Order_Pub;
1208 
1209    -- Initialize message list if p_init_msg_list is set to TRUE
1210   IF FND_API.To_Boolean(p_init_msg_list) THEN
1211     FND_MSG_PUB.Initialize;
1212   END IF;
1213 
1214   -- Initialize API return status to success
1215   x_return_status := FND_API.G_RET_STS_SUCCESS;
1216 
1217   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1218     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Entering Procedure');
1219   END IF;
1220 
1221   l_header_rec := OE_ORDER_PUB.G_MISS_HEADER_REC;
1222   l_line_tbl   := OE_ORDER_PUB.G_MISS_LINE_TBL;
1223 
1224   -- Try delete/cancel header first.
1225   IF (p_oe_header_id IS NOT NULL AND
1226       p_oe_header_id <> FND_API.G_MISS_NUM) THEN
1227 
1228     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1229       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'About to call Delete_OSP_Order with p_oe_header_id = ' || p_oe_header_id);
1230     END IF;
1231 
1232     -- Remove references in OSP tables
1233     Delete_OSP_Order(p_oe_header_id => p_oe_header_id );
1234 
1235     -- Convert header into cancel or delete
1236     OPEN ahl_is_header_deleteable_csr(p_oe_header_id);
1237     FETCH ahl_is_header_deleteable_csr INTO l_order_type_id;
1238     l_type := OE_Header_Util.Get_ord_seq_type(l_order_type_id);
1239 
1240     -- If deleteable and not gapless type
1241     IF (ahl_is_header_deleteable_csr%FOUND
1242         AND l_type <> 'G') THEN
1243        l_header_rec := OE_HEADER_UTIL.QUERY_ROW(p_header_id => p_oe_header_id);
1244        l_header_rec.operation := OE_GLOBALS.G_OPR_DELETE;
1245     ELSE
1246        l_header_rec := OE_HEADER_UTIL.QUERY_ROW(p_header_id => p_oe_header_id);
1247        l_header_rec.cancelled_flag := 'Y';
1248        l_header_rec.operation := OE_GLOBALS.G_OPR_UPDATE;
1249     END IF;
1250 
1251     CLOSE ahl_is_header_deleteable_csr;
1252 
1253   ELSIF (p_oe_lines_tbl.COUNT > 0) THEN
1254 
1255     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1256       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'p_oe_lines_tbl.COUNT = ' || p_oe_lines_tbl.COUNT);
1257     END IF;
1258 
1259     -- Process lines into cancel or delete
1260     FOR i IN p_oe_lines_tbl.FIRST..p_oe_lines_tbl.LAST  LOOP
1261 
1262       l_line_rec := OE_ORDER_PUB.G_MISS_LINE_REC;
1263 
1264       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1265         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'About to call Delete_OE_Lines with p_oe_line_id = ' || p_oe_lines_tbl(i));
1266       END IF;
1267       --Remove line_id references in OSP tables
1268       Delete_OE_Lines(p_oe_line_id => p_oe_lines_tbl(i));
1269 
1270       l_line_rec := OE_LINE_UTIL.QUERY_ROW(p_line_id => p_oe_lines_tbl(i));
1271 
1272       OPEN ahl_is_line_deleteable_csr(p_oe_lines_tbl(i));
1273       FETCH ahl_is_line_deleteable_csr INTO l_dummy;
1274       -- If deleteable
1275       IF (ahl_is_line_deleteable_csr%FOUND) THEN
1276         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1277           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Line is deletable: Deleting line');
1278         END IF;
1279         l_line_rec.operation := OE_GLOBALS.G_OPR_DELETE;
1280       ELSE
1281         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1282           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Line is not deletable: Setting Quantity to 0 and change_reason to Not provided');
1283         END IF;
1284         l_line_rec.ordered_quantity := 0;
1285         l_line_rec.change_reason := 'Not provided';  --No reason provided
1286         l_line_rec.operation := OE_GLOBALS.G_OPR_UPDATE;
1287       END IF;
1288       CLOSE ahl_is_line_deleteable_csr;
1289 
1290       l_line_tbl(i) := l_line_rec;
1291 
1292     END LOOP;  -- For all lines
1293   END IF;  -- Elsif line table count > 0
1294 
1295 --
1296   /*
1297   Added by jaramana on January 11, 2008 to raise any validation errors that may have been accumulated by the AHL_OSP_ORDERS_PVT procedure
1298   We do not have any warning messages hence if the message count is > 0 then it means there are validation errors and since
1299   we call a Public API in this procedure, its better we throw the error here itself.
1300   */
1301   l_msg_count := FND_MSG_PUB.count_msg;
1302   IF l_msg_count > 0 THEN
1303     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1304       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Error Count Before calling OE_ORDER_GRP.PROCESS_ORDER: '||l_msg_count);
1305     END IF;
1306     RAISE  FND_API.G_EXC_ERROR;
1307   END IF;
1308 
1309   --OE_ORDER_GRP uses its own message stack OE_MSG_PUB, so we should pass p_init_msg_list as true to this API
1310   --Note that this also does an FND_MSG_PUB.initialize along with clearing its own error stack
1311   OE_ORDER_GRP.PROCESS_ORDER(
1312     p_api_version_number  => 1.0,
1313     p_init_msg_list       => FND_API.G_TRUE,
1314     x_return_status       => x_return_status,
1315     x_msg_count           => x_msg_count,
1316     x_msg_data            => x_msg_data,
1317     p_header_rec	  => l_header_rec,
1318     p_line_tbl            => l_line_tbl,
1319     x_header_rec          => x_header_rec,
1320     x_header_val_rec      => x_header_val_rec,
1321     x_Header_Adj_tbl       =>  x_Header_Adj_tbl,
1322     x_Header_Adj_val_tbl   =>  x_Header_Adj_val_tbl,
1323     x_Header_price_Att_tbl =>  x_Header_price_Att_tbl,
1324     x_Header_Adj_Att_tbl   => x_Header_Adj_Att_tbl,
1325     x_Header_Adj_Assoc_tbl =>  x_Header_Adj_Assoc_tbl,
1326     x_Header_Scredit_tbl    =>   x_Header_Scredit_tbl,
1327     x_Header_Scredit_val_tbl =>    x_Header_Scredit_val_tbl,
1328     x_line_tbl               =>     x_line_tbl      ,
1329     x_line_val_tbl           =>    x_line_val_tbl ,
1330     x_Line_Adj_tbl           =>   x_Line_Adj_tbl    ,
1331     x_Line_Adj_val_tbl       =>  x_Line_Adj_val_tbl,
1332     x_Line_price_Att_tbl     =>   x_Line_price_Att_tbl,
1333     x_Line_Adj_Att_tbl       =>  x_Line_Adj_Att_tbl ,
1334     x_Line_Adj_Assoc_tbl     =>  x_Line_Adj_Assoc_tbl,
1335     x_Line_Scredit_tbl       => x_Line_Scredit_tbl ,
1336     x_Line_Scredit_val_tbl   =>  x_Line_Scredit_val_tbl,
1337     x_Lot_Serial_tbl         => x_Lot_Serial_tbl  ,
1338     x_Lot_Serial_val_tbl     => x_Lot_Serial_val_tbl   ,
1339     x_action_request_tbl     => x_action_request_tbl  );
1340 
1341   IF (x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
1342    FOR i IN 1..x_msg_count LOOP
1343      OE_MSG_PUB.Get( 	p_msg_index => i,
1344 			p_encoded => FND_API.G_FALSE,
1345 			p_data    => l_msg_data,
1346 			p_msg_index_out => l_msg_index_out);
1347      fnd_msg_pub.add_exc_msg(p_pkg_name       => 'OE_ORDER_PUB',
1348                              p_procedure_name => 'deleteCancelOrder',
1349                              p_error_text     => substr(l_msg_data,1,240));
1350       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1351            FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'OE:Err Msg '||i||'.' || l_msg_data);
1352       END IF;
1353     END LOOP;
1354   END IF;
1355 
1356   IF (x_return_status = FND_API.G_RET_STS_ERROR) THEN
1357     RAISE FND_API.G_EXC_ERROR;
1358   ELSIF (x_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
1359     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1360   /*
1361   Modified by jaramana on January 11, 2008 to fix the Bug 5935388/6504122
1362   If success is returned by the Public API, clear the message stack to eat up any warning messages from OM API.
1363   Otherwise, OAExceptionUtils.checkErrors will consider these as errors
1364   Also note that had there been any validation errors that had been accumulated we would have thrown before the call to
1365   OE_ORDER_GRP.PROCESS_ORDER
1366   */
1367   ELSIF(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
1368     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1369       FOR i IN 1..FND_MSG_PUB.count_msg LOOP
1370         FND_MSG_PUB.get (
1371             p_msg_index      => i,
1372             p_encoded        => FND_API.G_FALSE,
1373             p_data           => l_msg_data,
1374             p_msg_index_out  => l_msg_index_out );
1375         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY,'OE: Warning ' || i || ': ' || l_msg_data);
1376       END LOOP;
1377     END IF;
1378     FND_MSG_PUB.Initialize;
1379   END IF;
1380 
1381   IF (l_line_tbl.COUNT >0) THEN
1382    FOR i IN l_line_tbl.FIRST..l_line_tbl.LAST  LOOP
1383     Delete_IB_Transaction(
1384       -- Changed by jaramana on January 11, 2008 for the Requisition ER 6034236.
1385       p_init_msg_list         => FND_API.G_FALSE, --p_init_msg_list,
1386       p_commit                => p_commit,
1387       p_validation_level      => p_validation_level,
1388       x_return_status         => x_return_status,
1389       x_msg_count             => x_msg_count,
1390       x_msg_data              => x_msg_data,
1391       p_oe_line_id            => l_line_tbl(i).line_id );
1392 
1393     IF (x_return_status = FND_API.G_RET_STS_ERROR) THEN
1394       RAISE FND_API.G_EXC_ERROR;
1395     ELSIF (x_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
1396       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1397     END IF;
1398 
1399    END LOOP;
1400   END IF;
1401 
1402   -- Standard check of p_commit
1403   IF FND_API.To_Boolean(p_commit) THEN
1404     COMMIT WORK;
1405   END IF;
1406 
1407   -- Standard call to get message count and if count is 1, get message
1408   FND_MSG_PUB.Count_And_Get
1409     ( p_count => x_msg_count,
1410       p_data  => x_msg_data,
1411       p_encoded => fnd_api.g_false);
1412 
1413   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1414     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'Exiting Procedure');
1415   END IF;
1416 
1417 EXCEPTION
1418  WHEN FND_API.G_EXC_ERROR THEN
1419    x_return_status := FND_API.G_RET_STS_ERROR;
1420    Rollback to Delete_Cancel_Order_Pub;
1421    FND_MSG_PUB.count_and_get( p_count => x_msg_count,
1422                               p_data  => x_msg_data,
1423                               p_encoded => fnd_api.g_false);
1424 
1425  WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1426    x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1427    Rollback to Delete_Cancel_Order_Pub;
1428    FND_MSG_PUB.count_and_get( p_count => x_msg_count,
1429                               p_data  => x_msg_data,
1430                                p_encoded => fnd_api.g_false);
1431  WHEN OTHERS THEN
1432     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1433     Rollback to Delete_Cancel_Order_Pub;
1434        fnd_msg_pub.add_exc_msg(p_pkg_name       => G_PKG_NAME,
1435                                p_procedure_name => 'Delete_Cancel_Order',
1436                                p_error_text     => SQLERRM);
1437 
1438     FND_MSG_PUB.count_and_get( p_count => x_msg_count,
1439                                p_data  => x_msg_data,
1440                                 p_encoded => fnd_api.g_false);
1441 END Delete_Cancel_Order;
1442 
1443 ---------------------------------------------------------------------
1444 -- FUNCTION
1445 --    is_order_header_closed
1446 --
1447 -- PURPOSE
1448 --    This function checks if the shipment header is closed.
1449 --
1450 -- NOTES
1451 --    1. It will return FND_API.g_true/g_false.
1452 --    2. Exception encountered will be raised to the caller.
1453 ---------------------------------------------------------------------
1454 FUNCTION is_order_header_closed(
1455    p_oe_header_id IN NUMBER )
1456 RETURN VARCHAR2
1457 IS
1458 --Modified by mpothuku on 18-Sep-06 for fixing the Bug 5673483
1459 /*
1460  CURSOR ahl_osp_header_id_csr(p_header_id IN NUMBER) IS
1461     SELECT 1
1462     FROM oe_order_headers_all
1463     WHERE header_id = p_header_id;
1464 */
1465 --
1466  CURSOR ahl_osp_oe_closed_csr(p_header_id IN NUMBER) IS
1467     SELECT open_flag, nvl(flow_status_code,'XXX') flow_status_code, nvl(cancelled_flag,'N') cancelled_flag
1468     FROM oe_order_headers_all
1469     WHERE header_id = p_header_id;
1470     --Modified by mpothuku on 18-Sep-06 for fixing the Bug 5673483
1471     /*
1472     AND open_flag = 'N'
1473     AND flow_status_code = 'CLOSED';
1474     */
1475 --
1476  L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.is_order_header_closed';
1477  l_closed VARCHAR2(30);
1478  --l_dummy NUMBER;
1479  l_ahl_osp_oe_closed_csr ahl_osp_oe_closed_csr%rowtype;
1480 BEGIN
1481 
1482   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1483     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Entering Procedure');
1484   END IF;
1485 
1486   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1487     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY , 'p_oe_header_id is: '||p_oe_header_id);
1488   END IF;
1489   --Modified by mpothuku on 18-Sep-06 for fixing the Bug 5673483
1490   /*
1491    OPEN ahl_osp_header_id_csr(p_oe_header_id);
1492    FETCH ahl_osp_header_id_csr INTO l_dummy;
1493    IF (ahl_osp_header_id_csr%NOTFOUND) THEN
1494       CLOSE ahl_osp_header_id_csr;
1495       IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
1496          Fnd_Message.set_name('AHL', 'AHL_OSP_HEADER_ID_INV');
1497          Fnd_Msg_Pub.ADD;
1498       END IF;
1499       RAISE Fnd_Api.g_exc_error;
1500    END IF;
1501    CLOSE ahl_osp_header_id_csr;
1502   */
1503 
1504   --Modified by mpothuku on 18-Sep-06 for fixing the Bug 5673483
1505   l_closed := FND_API.G_FALSE;
1506   IF(p_oe_header_id is not null) THEN
1507       OPEN ahl_osp_oe_closed_csr (p_oe_header_id);
1508       FETCH ahl_osp_oe_closed_csr INTO l_ahl_osp_oe_closed_csr;
1509       IF (ahl_osp_oe_closed_csr%FOUND) THEN
1510          IF((l_ahl_osp_oe_closed_csr.open_flag = 'N' AND l_ahl_osp_oe_closed_csr.flow_status_code = 'CLOSED')
1511             OR (l_ahl_osp_oe_closed_csr.cancelled_flag = 'Y' AND l_ahl_osp_oe_closed_csr.flow_status_code = 'CANCELLED'))
1512          THEN
1513               l_closed := FND_API.G_TRUE;
1514          END IF;
1515       ELSE --This may mean that the Sales Order has been deleted from the OM Forms and Synch has not been done
1516         CLOSE ahl_osp_oe_closed_csr;
1517         l_closed := FND_API.G_FALSE;
1518         FND_MESSAGE.Set_Name('AHL', 'AHL_OSP_HEADER_ID_INV');
1519         FND_MSG_PUB.ADD;
1520         RAISE Fnd_Api.g_exc_error;
1521       END IF;
1522     CLOSE ahl_osp_oe_closed_csr;
1523   END IF;
1524   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1525     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'Exiting Procedure');
1526   END IF;
1527 
1528   RETURN l_closed;
1529 
1530 END is_order_header_closed;
1531 
1532 --Added by mpothuku on 14-Dec-05 to derive the user profile option name
1533 ------------------------------------------------------------------------------------
1534 -- FUNCTION
1535 --    get_user_profile_option_name
1536 --
1537 -- PURPOSE
1538 --    This function returns the user profile option name given a profile option name.
1539 --
1540 -- NOTES
1541 --    1. It will return user profile option name.
1542 -------------------------------------------------------------------------------------
1543 FUNCTION get_user_profile_option_name(p_profile_option_name IN VARCHAR2 )
1544 RETURN VARCHAR2
1545 IS
1546  CURSOR get_user_prf_opt_name_csr(c_prf_opt_name IN VARCHAR2) IS
1547     SELECT USER_PROFILE_OPTION_NAME
1548     FROM fnd_profile_options_vl
1549     WHERE profile_option_name = c_prf_opt_name;
1550 
1551  L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.get_user_profile_option_name';
1552  l_usr_prf_opt_name VARCHAR2(240);
1553 
1554 BEGIN
1555 
1556   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1557     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Entering Procedure');
1558   END IF;
1559 
1560    OPEN get_user_prf_opt_name_csr(p_profile_option_name);
1561    FETCH get_user_prf_opt_name_csr INTO l_usr_prf_opt_name;
1562    IF (get_user_prf_opt_name_csr%NOTFOUND) THEN
1563       l_usr_prf_opt_name := null;
1564       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1565         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Error: '||p_profile_option_name|| ' needs to be defined');
1566       END IF;
1567    END IF;
1568    CLOSE get_user_prf_opt_name_csr;
1569 
1570 
1571   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1572     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'Exiting Procedure. l_usr_prf_opt_name is: ' || l_usr_prf_opt_name);
1573   END IF;
1574 
1575   RETURN l_usr_prf_opt_name;
1576 
1577 END get_user_profile_option_name;
1578 
1579 ------------------------------
1580 -- Define Local Procedures --
1581 ------------------------------
1582 
1583 PROCEDURE Convert_Header_rec(
1584 	p_header_rec  IN AHL_OSP_SHIPMENT_PUB.SHIP_HEADER_REC_TYPE,
1585 	p_module_type         IN            VARCHAR2 ,
1586 	x_header_rec  OUT NOCOPY OE_ORDER_PUB.HEADER_REC_TYPE,
1587 	x_header_val_rec OUT NOCOPY OE_ORDER_PUB.HEADER_VAL_REC_TYPE)
1588 IS
1589 ---
1590   --Commented by mpothuku to fix the Perf Bug #4919255
1591   /*
1592   CURSOR ahl_salesrep_id_csr IS
1593     SELECT salesrep_id
1594     FROM ra_salesreps
1595     WHERE commissionable_flag = 'N';
1596   */
1597 
1598   CURSOR ahl_trxn_curr_code_csr IS
1599     SELECT GSB.currency_code
1600     FROM FINANCIALS_SYSTEM_PARAMETERS FSP,
1601       GL_SETS_OF_BOOKS GSB
1602     WHERE FSP.set_of_books_id = GSB.set_of_books_id;
1603 
1604    --Used inv_organization_info_v instead of org_organization_definitions to fix the Perf Bug #4919255
1605    CURSOR ahl_ship_from_orgs_csr(p_name IN VARCHAR2) IS
1606     SELECT org.organization_id
1607     FROM OE_SHIP_FROM_ORGS_V org, inv_organization_info_v def
1608     WHERE org.organization_id = def.organization_id
1609     -- Changed by jaramana on Sep 9, 2005 for MOAC Uptake
1610     -- AND def.operating_unit = FND_PROFILE.VALUE('DEFAULT_ORG_ID')
1611     AND def.operating_unit = MO_GLOBAL.get_current_org_id()
1612     AND org.name = p_name;
1613 
1614    CURSOR ahl_sold_to_orgs_csr(p_cust_number IN VARCHAR2) IS
1615     SELECT organization_id
1616     FROM OE_SOLD_TO_ORGS_V
1617     WHERE customer_number = p_cust_number;
1618 
1619  CURSOR ahl_ship_to_orgs_csr(p_name IN VARCHAR2, p_sold_to_org_id IN NUMBER) IS
1620     SELECT organization_id
1621     FROM OE_SHIP_TO_ORGS_V
1622     WHERE customer_id = p_sold_to_org_id
1623     AND name = p_name;
1624 
1625   CURSOR ahl_sold_to_contact_count_csr(p_sold_to_contact IN VARCHAR2, p_sold_to_org_id IN NUMBER) IS
1626     SELECT COUNT(CONTACT_ID)
1627     FROM OE_CONTACTS_V
1628     WHERE NAME = p_sold_to_contact
1629       AND CUSTOMER_ID = p_sold_to_org_id;
1630 
1631   CURSOR ahl_order_vendor_detls_csr(p_osp_order_id IN NUMBER) IS
1632   SELECT osp.vendor_id,
1633 	 osp.vendor_site_id,
1634 	 osp.vendor_contact_id,
1635 	 osp.osp_order_number,
1636 	 cust.customer_site_id,
1637 	 cust.customer_id
1638     FROM ahl_osp_orders_b osp,
1639          ahl_vendor_customer_rels_v cust
1640    WHERE osp.osp_order_id = p_osp_order_id
1641      AND osp.vendor_site_id = cust.vendor_site_id;
1642 
1643      l_vendor_cust_dtls ahl_order_vendor_detls_csr%ROWTYPE;
1644 
1645 
1646   CURSOR ahl_customer_info_csr(p_customer_site_id IN NUMBER) IS
1647   /*
1648   SELECT cust_account_id
1649    FROM  hz_cust_acct_sites_all acc,
1650          HZ_CUST_SITE_USES_ALL site
1651   WHERE  site.cust_acct_site_id = acc.cust_acct_site_id
1652   and SITE_USE_ID = p_customer_site_id;*/
1653 
1654   select customer_id from OE_SHIP_TO_ORGS_V
1655   where organization_id = p_customer_site_id;
1656 
1657   CURSOR ahl_get_ship_from_org_csr(p_osp_order_id IN NUMBER) IS
1658   SELECT inventory_org_id
1659     FROM ahl_osp_order_lines
1660    WHERE osp_order_id = p_osp_order_id
1661    ORDER BY osp_order_line_id;
1662 
1663   l_cust_id   NUMBER;
1664 
1665 ---
1666  L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.Convert_Header_Rec';
1667 
1668  l_trxn_curr_code    VARCHAR2(30);
1669  l_org_id            NUMBER;
1670  l_contact_count     NUMBER;
1671 ---
1672 BEGIN
1673 
1674   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1675     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Entering Procedure');
1676   END IF;
1677 
1678   IF (p_header_rec.operation = 'D') THEN
1679     x_header_rec := OE_HEADER_UTIL.QUERY_ROW(p_header_id => p_header_rec.header_id);
1680     x_header_val_rec :=  OE_ORDER_PUB.G_MISS_HEADER_VAL_REC;
1681     x_header_rec.operation := OE_GLOBALS.G_OPR_DELETE;
1682 
1683   ELSE
1684 
1685     -- Changed by jaramana on Sep 9, 2005 for MOAC Uptake
1686     l_org_id := MO_GLOBAL.get_current_org_id();
1687 
1688     --Convert our rec type into OE_ORDER_PUB entities
1689 
1690     x_header_rec := OE_ORDER_PUB.G_MISS_HEADER_REC;
1691     x_header_val_rec := OE_ORDER_PUB.G_MISS_HEADER_VAL_REC;
1692 
1693     --Added by mpothuku on 08-may-06 for fixing the Bug 5212130
1694     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1695        FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'p_header_rec.operation = ' || p_header_rec.operation);
1696        FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'p_header_rec.osp_order_id = ' || p_header_rec.osp_order_id);
1697     END IF;
1698 
1699     --If no operation, then invalid order
1700     IF (p_header_rec.operation IS NULL OR
1701       p_header_rec.operation = FND_API.G_MISS_CHAR) THEN
1702       RETURN;
1703     END IF;
1704 
1705     IF (p_header_rec.header_id IS NOT NULL) THEN
1706         x_header_rec.header_id := p_header_rec.header_id;
1707     END IF;
1708 
1709     IF (p_header_rec.order_number IS NOT NULL) THEN
1710         x_header_rec.order_number := p_header_rec.order_number;
1711     END IF;
1712 
1713     IF (p_header_rec.booked_flag IS NOT NULL)  THEN
1714         x_header_rec.booked_flag := p_header_rec.booked_flag;
1715     END IF;
1716 
1717     IF (p_header_rec.cancelled_flag IS NOT NULL)  THEN
1718         x_header_rec.cancelled_flag := p_header_rec.cancelled_flag;
1719     END IF;
1720 
1721     IF (p_header_rec.open_flag IS NOT NULL)  THEN
1722         x_header_rec.open_flag := p_header_rec.open_flag;
1723     END IF;
1724 
1725 /* OE to default
1726     IF (p_header_rec.price_list_id IS NOT NULL AND
1727         p_header_rec.price_list IS NULL) THEN
1728         x_header_rec.price_list_id := p_header_rec.price_list_id;
1729     END IF;
1730     IF (p_module_type = 'JSP' AND p_header_rec.price_list IS NOT NULL)  THEN
1731         x_header_val_rec.price_list := p_header_rec.price_list;
1732     END IF;
1733 */
1734 
1735     --Org section
1736     x_header_rec.org_id := l_org_id;
1737     x_header_rec.sold_from_org_id   := l_org_id;
1738 
1739     IF p_header_rec.osp_order_id IS NOT NULL THEN
1740 
1741        OPEN ahl_order_vendor_detls_csr(p_header_rec.osp_order_id);
1742        FETCH ahl_order_vendor_detls_csr INTO l_vendor_cust_dtls;
1743        CLOSE ahl_order_vendor_detls_csr;
1744 
1745        --Added by mpothuku on 08-may-06 for fixing the Bug 5212130
1746        IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1747          FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_vendor_cust_dtls.vendor_id = ' || l_vendor_cust_dtls.vendor_id);
1748          FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_vendor_cust_dtls.vendor_site_id = ' || l_vendor_cust_dtls.vendor_site_id);
1749          FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_vendor_cust_dtls.customer_id = ' || l_vendor_cust_dtls.customer_id);
1750          FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_vendor_cust_dtls.customer_site_id = ' || l_vendor_cust_dtls.customer_site_id);
1751        END IF;
1752 
1753        IF l_vendor_cust_dtls.customer_site_id IS NULL THEN
1754             Fnd_Message.set_name('AHL', 'AHL_OSP_VENDOR_INFO_NULL');
1755             Fnd_Msg_Pub.ADD;
1756             RAISE Fnd_Api.g_exc_error;
1757        END IF;
1758 
1759        OPEN ahl_customer_info_csr(l_vendor_cust_dtls.customer_site_id);
1760        FETCH ahl_customer_info_csr INTO l_cust_id;
1761        CLOSE ahl_customer_info_csr;
1762 
1763     END IF;
1764 
1765     --Sold to org id from sold_to_custom_number
1766     /*
1767     IF (p_module_type = 'JSP') THEN
1768       IF (p_header_rec.sold_to_custom_number IS NOT NULL) THEN
1769        OPEN ahl_sold_to_orgs_csr(p_header_rec.sold_to_custom_number);
1770        FETCH ahl_sold_to_orgs_csr INTO x_header_rec.sold_to_org_id;
1771        IF (ahl_sold_to_orgs_csr%NOTFOUND) THEN
1772           CLOSE ahl_sold_to_orgs_csr;
1773           IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
1774              Fnd_Message.set_name('AHL', 'AHL_OSP_SOLD_TO_ORG_INV');
1775              FND_MESSAGE.SET_TOKEN('SOLD_TO_ORG',p_header_rec.sold_to_custom_number);
1776              Fnd_Msg_Pub.ADD;
1777           END IF;
1778           RAISE Fnd_Api.g_exc_error;
1779        END IF;
1780        CLOSE ahl_sold_to_orgs_csr;
1781      END IF;
1782      x_header_val_rec.customer_number   := p_header_rec.sold_to_custom_number;
1783    ELSE
1784      x_header_rec.sold_to_org_id    := p_header_rec.sold_to_org_id;
1785    END IF;
1786    */
1787     --x_header_rec.sold_to_org_id := l_cust_id;
1788     x_header_rec.sold_to_org_id := l_vendor_cust_dtls.customer_id;
1789     x_header_rec.ship_to_org_id := l_vendor_cust_dtls.customer_site_id;
1790 
1791     /*
1792 
1793     IF (p_module_type = 'JSP') THEN
1794      IF (p_header_rec.ship_to_org IS NOT NULL AND
1795          x_header_rec.sold_to_org_id IS NOT NULL) THEN
1796        OPEN ahl_ship_to_orgs_csr(p_header_rec.ship_to_org,
1797 				 x_header_rec.sold_to_org_id);
1798        FETCH ahl_ship_to_orgs_csr INTO x_header_rec.ship_to_org_id;
1799        IF (ahl_ship_to_orgs_csr%NOTFOUND) THEN
1800           CLOSE ahl_ship_to_orgs_csr;
1801           IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
1802              Fnd_Message.set_name('AHL', 'AHL_OSP_SHIP_TO_ORG_INV');
1803              FND_MESSAGE.SET_TOKEN('SHIP_TO_ORG',p_header_rec.ship_to_org);
1804              Fnd_Msg_Pub.ADD;
1805           END IF;
1806           RAISE Fnd_Api.g_exc_error;
1807        END IF;
1808        CLOSE ahl_ship_to_orgs_csr;
1809      END IF;
1810      x_header_val_rec.ship_to_org   := p_header_rec.ship_to_org;
1811     ELSE
1812      x_header_rec.ship_to_org_id    := p_header_rec.ship_to_org_id;
1813     END IF;
1814 
1815     */
1816 
1817     -- Hack to find ship_from_org_id because the OE Value_to_ID converter
1818     -- does not exist.
1819     IF (p_module_type = 'JSP') THEN
1820      IF (p_header_rec.ship_from_org IS NOT NULL) THEN
1821        OPEN ahl_ship_from_orgs_csr(p_header_rec.ship_from_org);
1822        FETCH ahl_ship_from_orgs_csr INTO x_header_rec.ship_from_org_id;
1823        IF (ahl_ship_from_orgs_csr%NOTFOUND) THEN
1824           CLOSE ahl_ship_from_orgs_csr;
1825           IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
1826              Fnd_Message.set_name('AHL', 'AHL_OSP_SHIP_FROM_ORG_INV');
1827              FND_MESSAGE.SET_TOKEN('SHIP_FROM_ORG',p_header_rec.ship_from_org);
1828              Fnd_Msg_Pub.ADD;
1829           END IF;
1830           RAISE Fnd_Api.g_exc_error;
1831        END IF;
1832        CLOSE ahl_ship_from_orgs_csr;
1833      END IF;
1834      x_header_val_rec.ship_from_org  := p_header_rec.ship_from_org;
1835     ELSE
1836      x_header_rec.ship_from_org_id    := p_header_rec.ship_from_org_id;
1837     END IF;
1838 
1839     IF x_header_rec.ship_from_org_id IS NULL THEN
1840        OPEN ahl_get_ship_from_org_csr(p_header_rec.osp_order_id);
1841        FETCH ahl_get_ship_from_org_csr INTO x_header_rec.ship_from_org_id;
1842        CLOSE ahl_get_ship_from_org_csr;
1843     END IF;
1844 
1845    IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1846      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'x_header_rec.ship_from_org_id = ' || x_header_rec.ship_from_org_id);
1847    END IF;
1848 
1849     --Sold to contact (Uses ship_to_contact param name)
1850     OPEN ahl_sold_to_contact_count_csr(p_header_rec.ship_to_contact,
1851                                        x_header_rec.sold_to_org_id);
1852     FETCH ahl_sold_to_contact_count_csr INTO l_contact_count;
1853     IF (l_contact_count < 2) THEN
1854        x_header_rec.sold_to_contact_id   := FND_API.G_MISS_NUM;
1855        x_header_val_rec.sold_to_contact     := p_header_rec.ship_to_contact;
1856     ELSE
1857        x_header_rec.sold_to_contact_id   := p_header_rec.ship_to_contact_id;
1858     END IF;
1859     CLOSE ahl_sold_to_contact_count_csr;
1860 
1861     --Nullable LOV parameters
1862    IF (p_module_type = 'JSP') THEN
1863       x_header_rec.fob_point_code    := FND_API.G_MISS_CHAR;
1864     x_header_val_rec.fob_point   := p_header_rec.fob_point;
1865     ELSE
1866       x_header_rec.fob_point_code    := p_header_rec.fob_point_code;
1867     END IF;
1868 -- No used as of now . COmmented in JSP
1869     IF (p_module_type = 'JSP') THEN
1870       x_header_rec.freight_carrier_code   :=  FND_API.G_MISS_CHAR;
1871     x_header_val_rec.freight_carrier   := p_header_rec.freight_carrier;
1872     ELSE
1873       x_header_rec.freight_carrier_code   := p_header_rec.freight_carrier_code;
1874     END IF;
1875 
1876 
1877     IF (p_module_type = 'JSP') THEN
1878 	x_header_rec.freight_terms_code  := FND_API.G_MISS_CHAR ;
1879     x_header_val_rec.freight_terms  := p_header_rec.freight_terms;
1880     ELSE
1881 	x_header_rec.freight_terms_code  := p_header_rec.freight_terms_code;
1882     END IF;
1883 
1884 
1885 /* To be fetched from Profile */
1886     IF (p_module_type = 'JSP') THEN
1887 	  x_header_rec.shipment_priority_code  := FND_API.G_MISS_CHAR;
1888       x_header_val_rec.shipment_priority  := p_header_rec.shipment_priority;
1889     ELSE
1890     	x_header_rec.shipment_priority_code  := p_header_rec.shipment_priority_code;
1891     END IF;
1892 
1893     IF (p_module_type = 'JSP') THEN
1894 	x_header_rec.shipping_method_code  := FND_API.G_MISS_CHAR;
1895     x_header_val_rec.shipping_method    := p_header_rec.shipping_method;
1896     ELSE
1897 	x_header_rec.shipping_method_code  := p_header_rec.shipping_method_code;
1898     END IF;
1899 
1900     -- Added by jaramana on June 3, 2005 to default Header Values from setup
1901     IF (p_header_rec.operation = 'C') THEN
1902       -- Allow these values to be defaulted when creating a Shipment
1903       x_header_rec.shipping_method_code := FND_API.G_MISS_CHAR;
1904       x_header_rec.freight_terms_code := FND_API.G_MISS_CHAR;
1905       x_header_rec.freight_carrier_code := FND_API.G_MISS_CHAR;
1906       x_header_rec.fob_point_code := FND_API.G_MISS_CHAR;
1907       -- Get this from the profile
1908       x_header_rec.shipment_priority_code := FND_PROFILE.VALUE('AHL_OSP_OE_SHIPMENT_PRIORITY');
1909     END IF;
1910 
1911 /*
1912     -- JR: Coded on 8/19/2003 as fix for Bug 3095543
1913     -- Payment Term is mandatory
1914     IF ((p_module_type = 'JSP') AND (p_header_rec.payment_term = FND_API.G_MISS_CHAR)) THEN
1915       IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
1916           Fnd_Message.set_name('AHL', 'AHL_OSP_PAYMENT_TERM_NULL');
1917           Fnd_Msg_Pub.ADD;
1918       END IF;
1919       RAISE Fnd_Api.g_exc_error;
1920     END IF;
1921     IF (p_module_type = 'JSP') THEN
1922   	   x_header_rec.payment_term_id  := FND_API.G_MISS_NUM;
1923        x_header_val_rec.payment_term := p_header_rec.payment_term;
1924     ELSE
1925         x_header_rec.payment_term_id := p_header_rec.payment_term_id;
1926     END IF;
1927 */
1928 
1929 
1930 /* To be fetched from Profile */
1931 
1932     IF (p_header_rec.tax_exempt_flag IS NOT NULL) THEN
1933 	x_header_rec.tax_exempt_flag := p_header_rec.tax_exempt_flag;
1934     END IF;
1935     IF (p_header_rec.tax_exempt_number IS NOT NULL) THEN
1936 	x_header_rec.tax_exempt_number := p_header_rec.tax_exempt_number;
1937     END IF;
1938     IF (p_module_type = 'JSP') THEN
1939       x_header_rec.tax_exempt_reason_code := FND_API.G_MISS_CHAR;
1940       x_header_val_rec.tax_exempt_reason  := p_header_rec.tax_exempt_reason;
1941     ELSE
1942       x_header_rec.tax_exempt_reason_code := p_header_rec.tax_exempt_reason_code;
1943     END IF;
1944     IF x_header_rec.tax_exempt_flag IS NULL THEN
1945       IF (p_header_rec.operation = 'C') THEN
1946         x_header_rec.tax_exempt_flag := 'E'; -- Default is Exempted.
1947         x_header_rec.tax_exempt_reason_code := FND_PROFILE.VALUE('AHL_OSP_TAX_EXEMPT_REASON');
1948       END IF;
1949     END IF;
1950     x_header_rec.shipping_instructions := p_header_rec.shipping_instructions;
1951     x_header_rec.packing_instructions := p_header_rec.packing_instructions;
1952 
1953     --------------------Hard coded Values------------------------
1954 
1955     --Transaction currency code
1956     OPEN ahl_trxn_curr_code_csr;
1957     FETCH ahl_trxn_curr_code_csr INTO l_trxn_curr_code;
1958     IF (ahl_trxn_curr_code_csr%FOUND) THEN
1959         x_header_rec.transactional_curr_code := l_trxn_curr_code;
1960     END IF;
1961     CLOSE ahl_trxn_curr_code_csr;
1962 
1963     --Set the ORDER_TYPE to 'MIXED'
1964     IF (p_header_rec.operation = 'C') THEN
1965        x_header_rec.order_type_id := FND_PROFILE.VALUE('AHL_OSP_OE_MIXED_ID');
1966 
1967        x_header_rec.ordered_date := sysdate;
1968        x_header_rec.pricing_date := sysdate;
1969     END IF;
1970 
1971     --Fetch AHL_OSP_ORDER source document type id
1972     --AHL_ORDER_TYPE is defined to be 21 now.
1973     x_header_rec.source_document_type_id := 21;
1974 
1975     --Commented by mpothuku to fix the Perf Bug #4919255
1976     --Fetch salesrep id
1977     /*
1978     OPEN ahl_salesrep_id_csr;
1979     FETCH ahl_salesrep_id_csr INTO x_header_rec.salesrep_id;
1980     CLOSE ahl_salesrep_id_csr;
1981     */
1982     --'-3' corresponds to a non-commisionable salesrep in any org and its seeded according to the OM contact.
1983     x_header_rec.salesrep_id := -3;
1984     x_header_rec.source_document_id := p_header_rec.osp_order_id;
1985     x_header_rec.orig_sys_document_ref := p_header_rec.osp_order_number;
1986 
1987     ------------- Operations ----------------
1988     IF (p_header_rec.operation = 'C') THEN
1989       x_header_rec.operation := OE_GLOBALS.G_OPR_CREATE;
1990     ELSIF (p_header_rec.operation = 'U') THEN
1991       x_header_rec.operation := OE_GLOBALS.G_OPR_UPDATE;
1992     END IF;
1993   END IF; -- Not 'D'
1994 
1995   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1996     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'Exiting Procedure');
1997   END IF;
1998 
1999 END Convert_Header_rec;
2000 
2001 
2002 
2003 PROCEDURE Process_Line_Tbl(
2004         p_osp_order_id     IN            NUMBER,
2005         p_operation_flag   IN            VARCHAR,
2006         p_module_type      IN            VARCHAR2,
2007         p_x_line_tbl       IN OUT NOCOPY AHL_OSP_SHIPMENT_PUB.SHIP_LINE_TBL_TYPE
2008     ) IS
2009 --
2010 --Commented by mpothuku on 06-Mar-06 to fix the Perf Bug 4919255 as the cursor below is not being used.
2011 /*
2012   CURSOR ahl_osp_wo_csr(p_wo_name IN VARCHAR2, p_osp_id IN NUMBER) IS
2013    SELECT wo.inventory_item_id,
2014           wo.serial_number,
2015           wo.item_instance_uom,
2016           wo.quantity,
2017           wo.item_instance_id,
2018           wo.project_id,
2019           wo.project_task_id,
2020           wo.ORGANIZATION_ID,
2021           ospl.osp_order_id,
2022           ospl.osp_order_line_id,
2023           ospl.osp_line_number,
2024           ospl.exchange_instance_id
2025    FROM AHL_WORKORDERS_OSP_V wo, AHL_OSP_ORDER_LINES ospl
2026    WHERE ospl.workorder_id = wo.workorder_id
2027      AND wo.job_number = p_wo_name
2028      AND ospl.osp_order_id = p_osp_id
2029      AND ospl.status_code IS NULL;
2030 --
2031 */
2032 
2033 --yazhou 31-Jul-2006 starts
2034 -- bug fix#5442904
2035 
2036 l_service_duration  NUMBER;
2037 
2038 -- if multiple service lines are created for the same physical item, use the max service duration
2039 
2040 CURSOR ahl_max_service_duration_csr(p_vendor_id IN NUMBER, p_vendor_site_id IN NUMBER,
2041 p_osp_order_id IN NUMBER, p_inventory_item_id IN NUMBER, p_inventory_org_id IN NUMBER,
2042 p_serial_number IN VARCHAR2) IS
2043    select max(service_duration) from ahl_item_vendor_rels
2044    where vendor_certification_id = (select vendor_certification_id from ahl_vendor_certifications_v
2045    where vendor_id =p_vendor_id and vendor_site_id =p_vendor_site_id)
2046    and active_start_date <= sysdate
2047    and (active_end_date is null or active_end_date > sysdate)
2048    and inv_service_item_rel_id in (select inv_service_item_rel_id
2049                                     from ahl_inv_service_item_rels
2050                                    where service_item_id in (select service_item_id
2051                                                                from ahl_osp_order_lines
2052                                                               where osp_order_id = p_osp_order_id
2053 				                                               and inventory_item_id = p_inventory_item_id
2054 			                                                   and inventory_org_id = p_inventory_org_id
2055                                                                and serial_number = p_serial_number
2056                                                                and status_code is null)
2057                                       AND inv_item_id = p_inventory_item_id
2058                                       AND inv_org_id  = p_inventory_org_id);
2059 
2060 --yazhou 31-Jul-2006 ends
2061 
2062 
2063 --Commented by mpothuku on 21-Feb-06 to fix the Perf Bug 4919255 as the cursor below is not being used.
2064 /*
2065 CURSOR ahl_osp_instance_csr(p_instance_id IN NUMBER, p_osp_id IN NUMBER) IS
2066    SELECT wo.inventory_item_id,
2067           wo.serial_number,
2068           wo.item_instance_uom,
2069           wo.quantity,
2070           wo.item_instance_id,
2071           wo.project_id,
2072           wo.project_task_id,
2073           ospl.osp_order_id,
2074           ospl.osp_order_line_id,
2075           ospl.osp_line_number,
2076           ospl.exchange_instance_id
2077    FROM AHL_WORKORDERS_OSP_V wo, AHL_OSP_ORDER_LINES ospl
2078    WHERE ospl.workorder_id = wo.workorder_id
2079      AND wo.item_instance_id = p_instance_id
2080      AND ospl.osp_order_id = p_osp_id
2081      AND ospl.status_code IS NULL;
2082   */
2083   CURSOR ahl_exc_inst_details(p_instance_id IN NUMBER) IS
2084    SELECT inventory_item_id,
2085           serial_number,
2086           quantity,
2087           unit_of_measure,
2088           lot_number --jeli 12/01/05
2089    FROM csi_item_instances
2090    WHERE instance_id = p_instance_id;
2091 
2092 --yazhou 06-Jun-2006 starts
2093 --bug fix#5304095
2094 
2095 --For a given osp line id, check if shipment line exists
2096 CURSOR ahl_oe_ship_id_csr(p_osp_id    IN NUMBER,
2097                           p_osp_line_id    IN NUMBER)IS
2098     SELECT 1
2099      FROM AHL_OSP_ORDER_LINES
2100     WHERE osp_order_id = p_osp_id
2101       AND osp_order_line_id = p_osp_line_id
2102       AND oe_ship_line_id IS NOT NULL;
2103 
2104 --For a given osp line id, check if return line exists
2105 CURSOR ahl_oe_return_id_csr(p_osp_id    IN NUMBER,
2106                             p_osp_line_id IN NUMBER) IS
2107    SELECT 1
2108      FROM AHL_OSP_ORDER_LINES
2109     WHERE osp_order_id = p_osp_id
2110       AND osp_order_line_id = p_osp_line_id
2111       AND oe_return_line_id IS NOT NULL;
2112 
2113 /*
2114   CURSOR ahl_oe_ship_id_csr(p_osp_id    IN NUMBER,
2115                             p_csi_ii_id IN NUMBER) IS
2116     --Modified by mpothuku on 21-Feb-06 to fix the Perf Bug #4919255
2117 
2118    SELECT 1
2119      FROM AHL_OSP_ORDER_LINES ospl,
2120           AHL_WORKORDERS wo,
2121           ahl_visit_tasks_b vts
2122     WHERE ospl.workorder_id = wo.workorder_id
2123       AND ospl.osp_order_id = p_osp_id
2124       AND wo.visit_task_id = vts.visit_task_id
2125       AND vts.instance_id = p_csi_ii_id
2126       AND ospl.oe_ship_line_id IS NOT NULL;
2127 
2128 
2129   CURSOR ahl_oe_ship_id_Inv_csr(p_osp_id         IN NUMBER,
2130                                 p_inv_item_id    IN NUMBER,
2131                                 p_inv_org_id     IN NUMBER,
2132                                 p_sub_inventory  IN VARCHAR2,
2133                                 p_serial_number  IN VARCHAR2,
2134                                 p_lot_number     IN VARCHAR2,
2135                                 p_inv_item_uom   IN VARCHAR2) IS
2136     SELECT 1
2137      FROM AHL_OSP_ORDER_LINES a
2138     WHERE a.osp_order_id = p_osp_id
2139       AND a.inventory_item_id = p_inv_item_id
2140       AND a.inventory_org_id= p_inv_org_id
2141       AND NVL(a.sub_inventory, 'X') = NVL(p_sub_inventory, 'X')
2142       AND NVL(a.serial_number, 'X') = NVL(p_serial_number, 'X')
2143       AND NVL(a.lot_number, 'X') = NVL(p_lot_number, 'X')
2144       AND a.oe_ship_line_id IS NOT NULL;
2145 
2146   CURSOR ahl_oe_return_id_csr(p_osp_id    IN NUMBER,
2147                               p_csi_ii_id IN NUMBER) IS
2148 
2149    --Modified by mpothuku on 21-Feb-06 to fix the Perf Bug #4919255
2150    SELECT 1
2151      FROM AHL_OSP_ORDER_LINES ospl,
2152           AHL_WORKORDERS wo,
2153           ahl_visit_tasks_b vts
2154     WHERE ospl.workorder_id = wo.workorder_id
2155       AND ospl.osp_order_id = p_osp_id
2156       AND wo.visit_task_id = vts.visit_task_id
2157       AND vts.instance_id = p_csi_ii_id
2158       AND ospl.oe_return_line_id IS NOT NULL;
2159 
2160   CURSOR ahl_oe_inv_return_id_csr(p_osp_id         IN NUMBER,
2161                                   p_inv_item_id    IN NUMBER,
2162                                   p_inv_org_id     IN NUMBER,
2163                                   p_sub_inventory  IN VARCHAR2,
2164                                   p_serial_number  IN VARCHAR2,
2165                                   p_lot_number     IN VARCHAR2,
2166                                   p_inv_item_uom   IN VARCHAR2) IS
2167     SELECT 1
2168      FROM AHL_OSP_ORDER_LINES a
2169     WHERE a.osp_order_id = p_osp_id
2170       AND a.inventory_item_id = p_inv_item_id
2171       AND a.inventory_org_id= p_inv_org_id
2172       AND NVL(a.sub_inventory, 'X') = NVL(p_sub_inventory, 'X')
2173       AND NVL(a.serial_number, 'X') = NVL(p_serial_number, 'X')
2174       AND NVL(a.lot_number, 'X') = NVL(p_lot_number, 'X')
2175       AND a.oe_return_line_id IS NOT NULL;
2176 
2177 */
2178 --yazhou 06-Jun-2006 ends
2179 
2180    -- Cursor to check if an instance is independent (not an installed component)
2181    CURSOR ahl_csi_unit_test_csr (p_csi_ii_id IN NUMBER) IS
2182     SELECT 1
2183     FROM CSI_ITEM_INSTANCES CII
2184     WHERE CII.INSTANCE_ID = p_csi_ii_id AND
2185           NOT EXISTS
2186            (SELECT 'X' FROM CSI_II_RELATIONSHIPS CIR
2187              WHERE CIR.SUBJECT_ID = CII.INSTANCE_ID AND
2188                    CIR.RELATIONSHIP_TYPE_CODE = 'COMPONENT-OF' AND
2189                    NVL(CIR.ACTIVE_START_DATE, SYSDATE - 1) < SYSDATE AND
2190                    NVL(CIR.ACTIVE_END_DATE, SYSDATE + 1) > SYSDATE);
2191 
2192    CURSOR ahl_oe_schedule_date_csr(p_osp_id    IN NUMBER,
2193                                    p_csi_ii_id IN NUMBER) IS
2194     --Modified by mpothuku on 21-Feb-06 to fix the Perf Bug #4919255
2195      /*
2196      SELECT distinct oe1.line_id, oe1.schedule_ship_date,
2197                      oe2.line_id, oe2.schedule_ship_date
2198      FROM AHL_OSP_ORDER_LINES a, OE_ORDER_LINES_ALL oe1,
2199           OE_ORDER_LINES_ALL oe2, AHL_WORKORDERS_OSP_V b
2200     WHERE a.oe_ship_line_id = oe1.line_id (+)
2201       AND a.oe_return_line_id = oe2.line_id (+)
2202       AND a.workorder_id = b.workorder_id
2203       AND a.osp_order_id = p_osp_id
2204       AND b.item_instance_id = p_csi_ii_id;
2205     */
2206     SELECT distinct oe1.line_id, oe1.schedule_ship_date,
2207                      oe2.line_id, oe2.schedule_ship_date
2208      FROM AHL_OSP_ORDER_LINES ospl,
2209           OE_ORDER_LINES_ALL oe1,
2210           OE_ORDER_LINES_ALL oe2,
2211           AHL_WORKORDERS wo,
2212           ahl_visit_tasks_b vts
2213     WHERE ospl.oe_ship_line_id = oe1.line_id (+)
2214       AND ospl.oe_return_line_id = oe2.line_id (+)
2215       AND ospl.workorder_id = wo.workorder_id
2216       AND ospl.osp_order_id = p_osp_id
2217       AND wo.visit_task_id = vts.visit_task_id
2218       AND vts.instance_id = p_csi_ii_id;
2219 
2220    CURSOR ahl_oe_Inv_schedule_date_csr(p_osp_id         IN NUMBER,
2221                                        p_inv_item_id    IN NUMBER,
2222                                        p_inv_org_id     IN NUMBER,
2223                                        p_sub_inventory  IN VARCHAR2,
2224                                        p_serial_number  IN VARCHAR2,
2225                                        p_lot_number     IN VARCHAR2,
2226                                        p_inv_item_uom   IN VARCHAR2) IS
2227      SELECT oe1.line_id, oe1.schedule_ship_date,
2228             oe2.line_id, oe2.schedule_ship_date
2229      FROM AHL_OSP_ORDER_LINES a, OE_ORDER_LINES_ALL oe1,
2230           OE_ORDER_LINES_ALL oe2
2231     WHERE a.oe_ship_line_id = oe1.line_id (+)
2232       AND a.oe_return_line_id = oe2.line_id (+)
2233       AND a.osp_order_id = p_osp_id
2234       AND a.inventory_item_id = p_inv_item_id
2235       AND a.inventory_org_id = p_inv_org_id
2236       AND NVL(a.sub_inventory, 'X') = NVL(p_sub_inventory, 'X')
2237       AND NVL(a.serial_number, 'X') = NVL(p_serial_number, 'X')
2238       AND NVL(a.lot_number, 'X') = NVL(p_lot_number, 'X');
2239 
2240    CURSOR get_osp_order_type_csr(c_osp_id IN NUMBER) IS
2241    SELECT order_type_code from ahl_osp_orders_b
2242    where osp_order_id = c_osp_id;
2243 
2244    CURSOR osp_order_header_csr(p_osp_order_id IN NUMBER) IS
2245    SELECT *
2246    from ahl_osp_orders_b
2247    where osp_order_id = p_osp_order_id;
2248 
2249    l_osp_header_det osp_order_header_csr%ROWTYPE;
2250 
2251    CURSOR osp_line_details_csr(p_osp_order_id IN NUMBER,
2252                                p_osp_line_id  IN NUMBER) IS
2253    select osp_order_line_id,
2254           object_version_number,
2255           osp_order_id,
2256           osp_line_number,
2257           workorder_id,
2258           status_code,
2259           service_item_id,
2260           service_item_description,
2261           service_item_uom_code,
2262           need_by_date,
2263           ship_by_date,
2264           po_line_id,
2265           po_line_type_id,
2266           oe_ship_line_id,
2267           oe_return_line_id,
2268           operation_id,
2269           quantity,
2270           exchange_instance_id,
2271           inventory_item_id,
2272           inventory_org_id,
2273           sub_inventory,
2274           serial_number,
2275           lot_number,
2276           inventory_item_uom,
2277           inventory_item_quantity
2278    from ahl_osp_order_lines
2279    where osp_order_id = p_osp_order_id
2280      and osp_order_line_id = p_osp_line_id;
2281 
2282 --yazhou 26-Jul-2006 starts
2283 -- bug fix #5412158
2284 
2285    CURSOR osp_line_details_csr2(p_osp_order_id IN NUMBER)
2286    IS
2287    select osp_order_line_id       ,
2288           object_version_number   ,
2289           osp_order_id            ,
2290           osp_line_number         ,
2291           workorder_id            ,
2292           status_code             ,
2293           service_item_id         ,
2294           service_item_description,
2295           service_item_uom_code   ,
2296           need_by_date            ,
2297           ship_by_date            ,
2298           po_line_id              ,
2299           po_line_type_id         ,
2300           oe_ship_line_id         ,
2301           oe_return_line_id       ,
2302           operation_id            ,
2303           quantity                ,
2304           exchange_instance_id    ,
2305           inventory_item_id       ,
2306           inventory_org_id        ,
2307           sub_inventory           ,
2308           serial_number           ,
2309           lot_number              ,
2310           inventory_item_uom      ,
2311           inventory_item_quantity
2312    from ahl_osp_order_lines a
2313    where osp_order_id = p_osp_order_id
2314    AND ((osp_line_number = (select min(osp_line_number)
2315                           from ahl_osp_order_lines
2316                           where osp_order_id = p_osp_order_id
2317                             and inventory_item_id = a.inventory_item_id
2318                             and inventory_org_id = a.inventory_org_id
2319                             and serial_number = a.serial_number))
2320         -- Added by jaramana on January 11, 2008 as without this the non-serialized items are not getting picked for shipments
2321         --for the Bug 5688387/5842229
2322         OR serial_number is null)
2323      AND oe_ship_line_id IS NULL
2324      AND oe_return_line_id IS NULL;
2325 
2326 --yazhou 26-Jul-2006 ends
2327 
2328    /*
2329    CURSOR osp_line_details_csr3(p_osp_order_id IN NUMBER,
2330                                 p_line_id IN NUMBER) IS
2331    select osp_order_line_id       ,
2332           object_version_number   ,
2333           osp_order_id            ,
2334           osp_line_number         ,
2335           workorder_id            ,
2336           status_code             ,
2337           service_item_id         ,
2338           service_item_description,
2339           service_item_uom_code   ,
2340           need_by_date            ,
2341           ship_by_date            ,
2342           po_line_id              ,
2343           po_line_type_id         ,
2344           oe_ship_line_id         ,
2345           oe_return_line_id       ,
2346           operation_id            ,
2347           quantity                ,
2348           exchange_instance_id    ,
2349           inventory_item_id       ,
2350           inventory_org_id        ,
2351           sub_inventory           ,
2352           serial_number           ,
2353           lot_number              ,
2354           inventory_item_uom      ,
2355           inventory_item_quantity
2356    from ahl_osp_order_lines
2357    where osp_order_id = p_osp_order_id
2358      AND (NVL(oe_ship_line_id, -9) = NVL(p_line_id, -8)
2359      OR   NVL(oe_return_line_id, -9) = NVL(p_line_id, -8));
2360    */
2361 
2362    /*
2363    mpothuku changed the cursor definition on 18-Jul-2007 for the Bug 5676360
2364    Some of the details collected from this cursor ex: the inventory_item_id etc over-write the ones retrieved from the
2365    shipment line. Because of the part number change ER, if the part number change is done,
2366    these details will not match the ones on the shipment line and lead to inconsistencies.
2367    So if the installation details are present we pick the information from IB or else we pick it from the osp order
2368    line itself. Please note that because of this, if someone chages the part from the OM UIs and then try to update
2369    other details from Shipment Line Details UI, the information on the osp order lines will over-write this.
2370    But this seem to be the existing behavior.
2371    */
2372    CURSOR osp_line_details_csr3(p_osp_order_id IN NUMBER,
2373                                 p_line_id IN NUMBER) IS
2374    select ospl.osp_order_line_id       ,
2375           ospl.object_version_number   ,
2376           ospl.osp_order_id            ,
2377           ospl.osp_line_number         ,
2378           ospl.workorder_id            ,
2379           ospl.status_code             ,
2380           ospl.service_item_id         ,
2381           ospl.service_item_description,
2382           ospl.service_item_uom_code   ,
2383           ospl.need_by_date            ,
2384           ospl.ship_by_date            ,
2385           ospl.po_line_id              ,
2386           ospl.po_line_type_id         ,
2387           ospl.oe_ship_line_id         ,
2388           ospl.oe_return_line_id       ,
2389           ospl.operation_id            ,
2390           ospl.quantity                ,
2391           ospl.exchange_instance_id    ,
2392           decode(csi.instance_id, null, ospl.inventory_item_id, csi.inventory_item_id) inventory_item_id,
2393           ospl.inventory_org_id        ,
2394           ospl.sub_inventory           ,
2395           decode(csi.instance_id, null, ospl.serial_number, csi.serial_number) serial_number,
2396           decode(csi.instance_id, null, ospl.lot_number, csi.lot_number) lot_number,
2397           ospl.inventory_item_uom      ,
2398           ospl.inventory_item_quantity
2399      from ahl_osp_order_lines ospl,
2400           oe_order_lines_all oel,
2401           csi_t_transaction_lines tl,
2402           csi_t_txn_line_details tld,
2403           csi_item_instances csi
2404     where ospl.osp_order_id = p_osp_order_id
2405       AND (NVL(oe_ship_line_id, -9) = NVL(p_line_id, -8)
2406        OR NVL(oe_return_line_id, -9) = NVL(p_line_id, -8))
2407       AND oel.line_id = p_line_id
2408       AND oel.source_document_line_id = ospl.osp_order_line_id
2409       AND tl.source_transaction_id (+)= oel.line_id
2410       AND tl.source_transaction_table (+) = G_TRANSACTION_TABLE
2411       AND tl.transaction_line_id = tld.transaction_line_id(+)
2412       AND tld.instance_id = csi.instance_id(+);
2413   --mpothuku End
2414 
2415    CURSOR ahl_osp_work_csr(p_workorder_id IN NUMBER) IS
2416    --Modified by mpothuku on 22-Feb-06 to fix the Perf Bug #4919255
2417    /*
2418     SELECT wo.inventory_item_id,
2419            wo.serial_number,
2420            wo.item_instance_uom,
2421            wo.quantity,
2422            wo.item_instance_id,
2423            wo.project_id,
2424            wo.project_task_id,
2425            wo.ORGANIZATION_ID,
2426            wo.lot_number --jeli 12/01/05
2427     FROM AHL_WORKORDERS_OSP_V wo
2428     WHERE wo.workorder_id = p_workorder_id ;
2429    */
2430     SELECT vts.inventory_item_id,
2431         csii.serial_number,
2432         csii.unit_of_measure item_instance_uom,
2433         csii.quantity,
2434         vts.instance_id item_instance_id,
2435         vst.project_id,
2436         vts.project_task_id,
2437         vst.ORGANIZATION_ID,
2438         csii.lot_number
2439     FROM AHL_WORKORDERS wo,
2440         ahl_visits_b vst,
2441         ahl_visit_tasks_b vts,
2442         csi_item_instances csii
2443     WHERE wo.workorder_id = p_workorder_id
2444     AND wo.visit_task_id = vts.visit_task_id(+)
2445     AND vts.visit_id = vst.visit_id(+)
2446     AND vts.instance_id = csii.instance_id(+)
2447     AND wo.master_workorder_flag = 'N';
2448 
2449     l_work_det ahl_osp_work_csr%rowtype;
2450 
2451 -- Changed by jaramana on June 3, 2005
2452 /*
2453    CURSOR get_inv_inst_csr(p_inv_item_id   IN NUMBER,
2454                            p_inv_org_id    IN NUMBER,
2455                            p_serial_number IN VARCHAR2) IS
2456    SELECT INSTANCE_ID
2457      FROM csi_item_instances csi
2458     WHERE p_inv_item_id = csi.inventory_item_id
2459       and p_inv_org_id = csi.last_vld_organization_id
2460       and p_serial_number = csi.serial_number;
2461 */
2462    CURSOR get_inv_inst_csr(p_inv_item_id   IN NUMBER,
2463                            p_serial_number IN VARCHAR2) IS
2464    SELECT INSTANCE_ID
2465      FROM csi_item_instances csi
2466     WHERE p_inv_item_id = csi.inventory_item_id
2467       and p_serial_number = csi.serial_number;
2468 
2469    CURSOR get_inv_inst_from_lot_csr(p_inv_item_id IN NUMBER,
2470                                     p_lot_number  IN VARCHAR2) IS
2471    SELECT INSTANCE_ID
2472      FROM csi_item_instances csi
2473     WHERE csi.inventory_item_id = p_inv_item_id
2474       and csi.lot_number = p_lot_number;
2475 
2476 --Modified by mpothuku on 06-Mar-06 to fix the Perf Bug #4919255
2477 /*
2478    CURSOR validate_item(p_inventory_item_id IN NUMBER,
2479                         p_inventory_org_id  IN NUMBER,
2480                         p_serial_number     IN VARCHAR2,
2481                         p_lot_number        IN VARCHAR2) IS
2482    SELECT 1
2483    FROM  AHL_OSP_INV_ITEMS_V
2484    WHERE ORGANIZATION_ID = p_inventory_org_id
2485      AND INV_ITEM_ID = p_inventory_item_id
2486      AND NVL(SERIAL_NUMBER, 'X') = NVL(p_serial_number, 'X')
2487      AND NVL(LOT_NUMBER, 'X') = NVL(p_lot_number, 'X');
2488 */
2489    CURSOR validate_item(p_inventory_item_id IN NUMBER,
2490                         p_inventory_org_id  IN NUMBER
2491                        ) IS
2492    SELECT 1
2493    FROM  MTL_SYSTEM_ITEMS_B
2494    WHERE ORGANIZATION_ID = p_inventory_org_id
2495      AND INVENTORY_ITEM_ID = p_inventory_item_id;
2496 
2497    -- Cursor to get the exchange instance and all its details from a OSP Line Id.
2498    CURSOR exchange_instance_dtls_csr(p_osp_order_line_id IN NUMBER) IS
2499    select ospl.exchange_instance_id,
2500           csi.inventory_item_id,
2501           csi.serial_number,
2502           csi.lot_number,
2503           csi.quantity,
2504           csi.unit_of_measure,
2505           csi.last_vld_organization_id
2506    FROM ahl_osp_order_lines ospl, csi_item_instances csi
2507    WHERE ospl.osp_order_line_id = p_osp_order_line_id and
2508          csi.instance_id = ospl.exchange_instance_id;
2509 
2510    l_exchange_inst_dtls exchange_instance_dtls_csr%rowtype;
2511 
2512    --Added by mpothuku on 16-Jul-2007 to fix the Bug 6185894
2513    --Cursor to get the so line details
2514    CURSOR get_so_line_details(c_line_id IN NUMBER) IS
2515    SELECT open_flag,
2516           flow_status_code,
2517           ordered_quantity,
2518           cancelled_quantity
2519      FROM oe_order_lines_all
2520     WHERE line_id = c_line_id;
2521 
2522     l_get_so_line_details get_so_line_details%rowtype;
2523     --mpothuku End
2524 --
2525   l_dummy NUMBER;
2526   I PLS_INTEGER;
2527   l_ship_date DATE;
2528   l_return_date DATE;
2529   l_ship_line_id NUMBER;
2530   l_return_line_id NUMBER;
2531   /*
2532   mpothuku updated on 06-Mar-06 to remove the ref to l_wo_rec as its not being used
2533   l_wo_rec ahl_osp_wo_csr%ROWTYPE;
2534   */
2535   l_curr_osp_type AHL_OSP_ORDERS_B.order_type_code%type;
2536   l_osp_line_det_type osp_line_details_csr%rowtype;
2537   l_index NUMBER;
2538   L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.Process_Line_Tbl';
2539   --Jerry added on 11/01/05 in order to fix bug 4580226
2540   l_old_inv_item_id NUMBER;
2541   l_old_serial_number VARCHAR2(100);
2542   l_old_quantity NUMBER;
2543   l_old_uom VARCHAR2(20);
2544   l_old_lot_number VARCHAR2(80); --jeli 12/01/05
2545   l_ship_index NUMBER;
2546   --Bug fix 4580226 ends
2547 --
2548 BEGIN
2549 
2550   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2551     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Entering Procedure');
2552   END IF;
2553 
2554 -- yazhou 31-Jul-2006 starts
2555 -- bug fix#5442904
2556 -- use user defined vendor service duration if available
2557 
2558 
2559     OPEN osp_order_header_csr(p_osp_order_id);
2560     FETCH osp_order_header_csr INTO l_osp_header_det;
2561     CLOSE osp_order_header_csr;
2562 
2563 -- yazhou 31-Jul-2006 ends
2564 
2565   /*
2566   Added by mpothuku on 04-May-06
2567   The following Logic is used when creating shipment directly from Inventory item Search/Workorder Search UI
2568   Also its being used when creating shipments from the Create/Edit Shipment Line UI
2569   */
2570 
2571   IF (p_x_line_tbl.COUNT > 0) THEN
2572     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2573       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'p_x_line_tbl.COUNT = ' || p_x_line_tbl.COUNT);
2574     END IF;
2575     FOR i IN p_x_line_tbl.FIRST..p_x_line_tbl.LAST LOOP
2576       -- Get the OSP Order Type if not known
2577       IF(p_x_line_tbl(i).osp_order_id IS NOT NULL AND
2578          p_x_line_tbl(i).osp_order_id <> FND_API.G_MISS_NUM AND
2579          l_curr_osp_type IS NULL) THEN
2580         OPEN get_osp_order_type_csr(p_x_line_tbl(i).osp_order_id);
2581         FETCH get_osp_order_type_csr into l_curr_osp_type;
2582         CLOSE get_osp_order_type_csr;
2583       END IF;
2584 
2585       -- Set the Order Type field in the Line Record
2586       IF (p_x_line_tbl(i).order_type IS NULL OR p_x_line_tbl(i).order_type = FND_API.G_MISS_CHAR) THEN
2587         p_x_line_tbl(i).order_type := l_curr_osp_type;
2588       END IF;
2589 
2590       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2591         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'p_x_line_tbl(i).order_type = ' || p_x_line_tbl(i).order_type ||
2592                                                            ', p_x_line_tbl(i).osp_order_id = ' || p_x_line_tbl(i).osp_order_id ||
2593                                                            ', p_x_line_tbl(i).osp_line_id = ' || p_x_line_tbl(i).osp_line_id ||
2594                                                            ', p_x_line_tbl(i).osp_line_flag = ' || p_x_line_tbl(i).osp_line_flag ||
2595                                                            ', p_x_line_tbl(i).line_id = ' || p_x_line_tbl(i).line_id);
2596       END IF;
2597       IF ((p_x_line_tbl(i).osp_order_id IS NOT NULL AND p_x_line_tbl(i).osp_order_id <> FND_API.G_MISS_NUM) AND
2598           p_x_line_tbl(i).osp_line_id IS NOT NULL AND p_x_line_tbl(i).osp_line_flag = 'Y') THEN
2599         -- Shipment Line is for a OSP Line
2600         IF p_x_line_tbl(i).line_id IS NULL THEN
2601           -- Ship Line Id is not known: Get the OSP Line Details from the OSP Line Id
2602           OPEN osp_line_details_csr(p_x_line_tbl(i).osp_order_id,
2603                                     p_x_line_tbl(i).osp_line_id);
2604           FETCH osp_line_details_csr INTO l_osp_line_det_type;
2605           IF osp_line_details_csr%NOTFOUND THEN
2606             Fnd_Message.set_name('AHL', 'AHL_OSP_PRIMARY_KEY_NULL');
2607             Fnd_Msg_Pub.ADD;
2608             CLOSE osp_line_details_csr;
2609             RAISE Fnd_Api.g_exc_error;
2610           END IF;
2611           CLOSE osp_line_details_csr;
2612         ELSE
2613           /*
2614           One an SO is booked, it will not be possible to delete any of its lines. But the quantity can be set to 0.
2615           Once this is done, the status of the SO line becomes cancelled. We still show such lines on the
2616           Shipment Lines UI (The VO uses the source_document_id on the cancelled lines to get the osp_line_id.
2617           This association we do not delete when cancelling a booked line) and when user tries to update/delete
2618           such lines, the error reported in the Bug 6185894 is being ensued. It is because, the following cursor
2619           does not retrieve any records for such lines.
2620           OM does not happen to anyway allow deletes on booked lines and updates on cancelled lines.
2621           So we can check the status and throw an error up-front instead of changing the logic to make such records
2622           hit OM.
2623           */
2624           --Modified on 16-Jul-2007
2625           --Get the Shipment Line status.
2626           OPEN get_so_line_details(p_x_line_tbl(i).line_id);
2627           FETCH get_so_line_details INTO l_get_so_line_details;
2628           IF get_so_line_details%NOTFOUND THEN
2629             IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2630               FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Details not found for Shipment Line ID: '||p_x_line_tbl(i).line_id);
2631             END IF;
2632             FND_MESSAGE.SET_NAME('AHL', 'AHL_COM_NO_DATA_FOUND');
2633             FND_MSG_PUB.ADD;
2634             CLOSE get_so_line_details;
2635             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2636           END IF;
2637           CLOSE get_so_line_details;
2638 
2639           IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2640             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_get_so_line_details.open_flag: '||l_get_so_line_details.open_flag);
2641             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_get_so_line_details.flow_status_code: '||l_get_so_line_details.flow_status_code);
2642             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_get_so_line_details.ordered_quantity: '||l_get_so_line_details.ordered_quantity);
2643             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_get_so_line_details.cancelled_quantity: '||l_get_so_line_details.cancelled_quantity);
2644           END IF;
2645 
2646           IF(nvl(l_get_so_line_details.open_flag,'Y') = 'N') THEN
2647             FND_MESSAGE.SET_NAME('AHL', 'AHL_OSP_OM_LINE_CLOSED');
2648             FND_MSG_PUB.ADD;
2649             RAISE FND_API.G_EXC_ERROR;
2650           END IF;
2651           --mpothuku End
2652           -- Get the OSP Line Details from the Ship Line Id
2653           OPEN osp_line_details_csr3(p_x_line_tbl(i).osp_order_id,
2654                                      p_x_line_tbl(i).line_id);
2655           FETCH osp_line_details_csr3 INTO l_osp_line_det_type;
2656           IF osp_line_details_csr3%NOTFOUND THEN
2657             Fnd_Message.set_name('AHL', 'AHL_OSP_PRIMARY_KEY_NULL');
2658             Fnd_Msg_Pub.ADD;
2659             CLOSE osp_line_details_csr3;
2660             RAISE Fnd_Api.g_exc_error;
2661           END IF;
2662           CLOSE osp_line_details_csr3;
2663         END IF;
2664 
2665 -- yazhou 31-Jul-2006 starts
2666 -- bug fix#5442904
2667 -- use user defined vendor service duration if available
2668 
2669         l_service_duration := null;
2670 
2671         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2672           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_osp_header_det.vendor_id is '||l_osp_header_det.vendor_id);
2673           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_osp_header_det.vendor_site_id is' ||l_osp_header_det.vendor_site_id);
2674           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_osp_line_det_type.service_item_id is ' ||l_osp_line_det_type.service_item_id);
2675           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_osp_line_det_type.inventory_item_id is ' ||l_osp_line_det_type.inventory_item_id);
2676           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_osp_line_det_type.inventory_org_id is ' ||l_osp_line_det_type.inventory_org_id);
2677         END IF;
2678 
2679         IF l_osp_header_det.vendor_id is not null
2680         AND l_osp_header_det.vendor_site_id is not NULL
2681         AND l_osp_line_det_type.service_item_id is not null THEN
2682 
2683             OPEN ahl_max_service_duration_csr(l_osp_header_det.vendor_id, l_osp_header_det.vendor_site_id, p_osp_order_id,l_osp_line_det_type.inventory_item_id, l_osp_line_det_type.inventory_org_id, l_osp_line_det_type.serial_number);
2684             FETCH ahl_max_service_duration_csr INTO l_service_duration;
2685             CLOSE ahl_max_service_duration_csr;
2686 
2687         END IF;
2688 
2689         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2690           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_service_duration is '||l_service_duration);
2691         END IF;
2692 
2693         IF l_service_duration is null THEN
2694             -- Check if the Default Duration Profile has been Set
2695             IF (FND_PROFILE.VALUE('AHL_VENDOR_SERVICE_DURATION') IS NULL OR FND_PROFILE.VALUE('AHL_VENDOR_SERVICE_DURATION') = '') THEN
2696                 FND_MESSAGE.set_name('AHL', 'AHL_OSP_PROFILE_NULL');
2697                 --FND_MESSAGE.SET_TOKEN('PROFILE', 'AHL: Vendor Service Duration');
2698                 FND_MESSAGE.SET_TOKEN('PROFILE', get_user_profile_option_name('AHL_VENDOR_SERVICE_DURATION'));
2699                 FND_MSG_PUB.ADD;
2700                 IF (FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2701                     FND_LOG.MESSAGE(FND_LOG.LEVEL_UNEXPECTED, L_DEBUG_KEY, FALSE);
2702                 END IF;
2703                 RAISE Fnd_Api.g_exc_error;
2704             END IF;
2705 
2706             l_service_duration := FND_PROFILE.VALUE('AHL_VENDOR_SERVICE_DURATION');
2707 
2708         END IF;
2709 -- yazhou 31-Jul-2006 ends
2710 
2711 
2712       END IF; -- Shipment Line is for a OSP Line
2713 
2714       IF p_x_line_tbl(i).osp_line_flag = 'Y' THEN
2715         -- Shipment Line is for an OSP Line
2716         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2717           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'osp_line_flag is Y: Shipment Line is for an OSP Line');
2718           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_osp_line_det_type.workorder_id = ' || l_osp_line_det_type.workorder_id ||
2719                                                                ', l_osp_line_det_type.osp_order_line_id = ' || l_osp_line_det_type.osp_order_line_id);
2720         END IF;
2721 
2722         --Jerry added on 11/01/05 in order to fix bug 4580226
2723         l_old_inv_item_id := p_x_line_tbl(i).inventory_item_id;
2724         l_old_serial_number := p_x_line_tbl(i).serial_number;
2725         l_old_quantity := p_x_line_tbl(i).ordered_quantity;
2726         l_old_uom :=p_x_line_tbl(i).order_quantity_uom;
2727         l_old_lot_number :=p_x_line_tbl(i).lot_number; --jeli 12/01/05
2728         --Debug statements added by mpothuku on 14-Dec-05
2729         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2730             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_old_inv_item_id: ' || l_old_inv_item_id);
2731             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_old_serial_number: ' || l_old_serial_number);
2732             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_old_quantity: ' || l_old_quantity);
2733             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_old_uom: ' || l_old_uom);
2734             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_old_lot_number: ' || l_old_lot_number);
2735         END IF;
2736         --Bug fix 4580226 ends
2737 
2738         IF l_osp_line_det_type.workorder_id IS NOT NULL THEN
2739           -- Shipment Line Corresponds to an Work order
2740           IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2741             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_osp_line_det_type.workorder_id is not null');
2742           END IF;
2743 
2744           -- Get the Workorder Details
2745           OPEN ahl_osp_work_csr(l_osp_line_det_type.workorder_id);
2746           FETCH ahl_osp_work_csr INTO l_work_det;
2747           CLOSE ahl_osp_work_csr;
2748 
2749           -- Get Details for Shipment Line from the Work order
2750           p_x_line_tbl(i).inventory_item_id    := l_work_det.inventory_item_id;
2751           p_x_line_tbl(i).serial_number        := l_work_det.serial_number;
2752           p_x_line_tbl(i).order_quantity_uom   := l_work_det.item_instance_uom;
2753           -- Changed by jaramana on 02-MAR-2010 for Bug 9307889
2754           -- Get the Quantity from the OSP Line instead of from the instance
2755           -- p_x_line_tbl(i).ordered_quantity     := l_work_det.quantity;
2756           p_x_line_tbl(i).ordered_quantity     := l_osp_line_det_type.inventory_item_quantity;
2757           p_x_line_tbl(i).project_id           := l_work_det.project_id;
2758           p_x_line_tbl(i).task_id              := l_work_det.project_task_id;
2759           p_x_line_tbl(i).osp_line_id          := l_osp_line_det_type.osp_order_line_id;
2760           p_x_line_tbl(i).osp_line_number      := l_osp_line_det_type.osp_line_number;
2761           p_x_line_tbl(i).csi_item_instance_id := l_work_det.item_instance_id;
2762           p_x_line_tbl(i).lot_number           := l_work_det.lot_number; --jeli 12/01/05
2763 
2764 -- yazhou 07-Apr-2006 starts
2765 -- Bug fix #4998349
2766 
2767           -- yazhou 26-Apr-2006 starts
2768           -- Bug fix#5189285
2769 
2770           -- For Exchange Order Return lines, use the exchange instance id instead
2771           IF  (p_x_line_tbl(i).operation <> 'D') AND
2772               ((p_x_line_tbl(i).line_type_id = FND_PROFILE.VALUE('AHL_OSP_OE_RETURN_ID')) AND
2773               (p_x_line_tbl(i).order_type = AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_EXCHANGE)) THEN
2774 
2775           -- yazhou 26-Apr-2006 ends
2776            /*
2777             IF (l_wo_rec.exchange_instance_id IS NULL OR l_wo_rec.exchange_instance_id = FND_API.G_MISS_NUM) THEN
2778             */
2779             --mpothuku updated on 06-Mar-06 to remove the ref to l_wo_rec as its not being used
2780 
2781 
2782             /* Commented out since exchange instance can be null
2783 			IF (p_x_line_tbl(i).operation <> 'D') THEN
2784                 IF (l_osp_line_det_type.exchange_instance_id IS NULL OR l_osp_line_det_type.exchange_instance_id = FND_API.G_MISS_NUM) THEN
2785                     -- Need to set exchange instance before creating return shipment line for exchange orders
2786                     IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
2787                         Fnd_Message.set_name('AHL', 'AHL_OSP_EXC_INST_ID_NULL');
2788                         Fnd_Msg_Pub.ADD;
2789                     END IF;
2790                     RAISE Fnd_Api.g_exc_error;
2791                 END IF;
2792             END IF;
2793             */
2794 
2795             IF (l_osp_line_det_type.exchange_instance_id IS NOT NULL AND
2796 			   l_osp_line_det_type.exchange_instance_id <> FND_API.G_MISS_NUM) THEN
2797 
2798                 p_x_line_tbl(i).csi_item_instance_id:= l_osp_line_det_type.exchange_instance_id;
2799                 -- Get Details about the Exchange Instance
2800                 OPEN ahl_exc_inst_details(p_x_line_tbl(i).csi_item_instance_id);
2801                 FETCH ahl_exc_inst_details INTO p_x_line_tbl(i).inventory_item_id,
2802                                             p_x_line_tbl(i).serial_number,
2803                                             p_x_line_tbl(i).ordered_quantity,
2804                                             p_x_line_tbl(i).order_quantity_uom,
2805                                             p_x_line_tbl(i).lot_number; --jeli 12/01/05
2806                 CLOSE ahl_exc_inst_details;
2807                 IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2808                     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Fetched Exchange Instance details: ' ||
2809                                    'item_instance_id = ' || p_x_line_tbl(i).csi_item_instance_id ||
2810                                    ', inventory_item_id = ' || p_x_line_tbl(i).inventory_item_id ||
2811                                    ', serial_number = ' || p_x_line_tbl(i).serial_number ||
2812                                    ', ordered_quantity = ' || p_x_line_tbl(i).ordered_quantity ||
2813                                    ', order_quantity_uom = ' || p_x_line_tbl(i).order_quantity_uom ||
2814                                    ', lot_number = ' || p_x_line_tbl(i).lot_number); --jeli 12/01/05
2815                 END IF;
2816                 --jeli 12/01/05
2817 
2818                 --(jeli 12/01/05) Jerry added on 11/01/05 in order to fix bug 4580226
2819                --Added by mpothuku on 14-Dec-05 to fix the following validation that was raised when creating shipment
2820                --from Inventory Item Search UI.
2821                -- Validation only required if calling from front-end
2822                IF  (p_module_type is not null) AND
2823                     (p_x_line_tbl(i).inventory_item_id <> l_old_inv_item_id OR
2824                       NVL(p_x_line_tbl(i).serial_number, 'X') <> NVL(l_old_serial_number, 'X') OR
2825                       p_x_line_tbl(i).ordered_quantity <> l_old_quantity OR
2826                       p_x_line_tbl(i).order_quantity_uom <> l_old_uom OR
2827                       NVL(p_x_line_tbl(i).lot_number, 'X') <> NVL(l_old_lot_number, 'X')) THEN
2828                          Fnd_Message.set_name('AHL', 'AHL_OSP_ITEM_ATTR_NO_CHANGE');
2829                          Fnd_Msg_Pub.ADD;
2830                          RAISE Fnd_Api.g_exc_error;
2831                END IF;
2832                --Bug fix 4580226 ends
2833 
2834             ELSE    -- Exchange instance is null
2835 
2836              -- yazhou 26-Apr-2006 starts
2837              -- Bug fix#5189285
2838 
2839              -- restoring to the item entered by user if calling from front-end
2840              -- also try to derive and set exchange instance
2841              IF (p_module_type is not null) THEN
2842 
2843                  p_x_line_tbl(i).inventory_item_id :=l_old_inv_item_id;
2844                  p_x_line_tbl(i).serial_number := l_old_serial_number;
2845                  p_x_line_tbl(i).lot_number    := l_old_lot_number;
2846                  p_x_line_tbl(i).csi_item_instance_id := null;
2847 
2848                  -- If the serial number user entered corresponds to a tracked instance, derive the
2849                  --  instance number and update the Osp Order Line's Exchange Instance column transparently
2850                  IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2851                     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Exchange Instance id is null ' ||
2852                                    'l_osp_line_det_type.exchange_instance_id = ' || l_osp_line_det_type.exchange_instance_id);
2853                  END IF;
2854 
2855 
2856                  IF p_x_line_tbl(i).inventory_item_id IS NOT NULL  THEN
2857 
2858      			    IF p_x_line_tbl(i).serial_number IS NOT NULL  THEN
2859 
2860     			      OPEN get_inv_inst_csr(p_x_line_tbl(i).inventory_item_id, p_x_line_tbl(i).serial_number);
2861                       FETCH get_inv_inst_csr INTO p_x_line_tbl(i).csi_item_instance_id;
2862                       CLOSE get_inv_inst_csr;
2863 
2864     			    ELSIF p_x_line_tbl(i).lot_number IS NOT NULL THEN
2865 
2866     			      OPEN get_inv_inst_from_lot_csr(p_x_line_tbl(i).inventory_item_id, p_x_line_tbl(i).lot_number);
2867 	                  FETCH get_inv_inst_from_lot_csr INTO p_x_line_tbl(i).csi_item_instance_id;
2868                       CLOSE get_inv_inst_from_lot_csr;
2869 
2870     			    END IF;  --  user entered SN AND/OR Lot number
2871 
2872                     --If the SN/LOT user entered corresponds to a non-tracked item
2873 	    			-- simply ignore the serial number entered by the user without throwing an error.
2874 		    	    IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2875                        FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'p_x_line_tbl.csi_item_instance_id = ' || p_x_line_tbl(i).csi_item_instance_id);
2876                     END IF;
2877 
2878     			    IF p_x_line_tbl(i).csi_item_instance_id IS NOT NULL THEN
2879 
2880     				   -- update the Osp Order Line's Exchange Instance column
2881         	           IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2882                            FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'About to call Update_OSP_Line_Exch_Instance');
2883                        END IF;
2884 
2885     		          Update_OSP_Line_Exch_Instance(
2886                            p_osp_order_id      => p_x_line_tbl(i).osp_order_id,
2887                  	       p_osp_line_id       => p_x_line_tbl(i).osp_line_id,
2888                            p_exchange_instance_id  =>  p_x_line_tbl(i).csi_item_instance_id);
2889 
2890     			    ELSE
2891 
2892     				   -- ignore the SN/LOT number user entered
2893 	     		       p_x_line_tbl(i).serial_number := NULL;
2894 		    	       p_x_line_tbl(i).lot_number := NULL;
2895 			           p_x_line_tbl(i).csi_item_instance_id:= null;
2896 
2897     				END IF; -- exchange instance is derived
2898 
2899     			  END IF;  -- user entered item
2900 
2901               ELSE  -- called from back-end
2902 
2903 	     		       p_x_line_tbl(i).serial_number := NULL;
2904 		    	       p_x_line_tbl(i).lot_number := NULL;
2905 			           p_x_line_tbl(i).csi_item_instance_id:= null;
2906 
2907 
2908               END IF;  -- called from front-end
2909 
2910              -- yazhou 26-Apr-2006 ends
2911 
2912             END IF; --Exchange instance null check
2913 
2914           END IF;  -- Exchange Order Return Lines
2915 
2916           --Check that ship to or return instance is top node in CSI.
2917           -- If not unit, then throw error cause can't ship part of a unit.
2918           IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2919             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'About to perform Component Check.');
2920           END IF;
2921 
2922           IF p_x_line_tbl(i).csi_item_instance_id IS NOT NULL THEN
2923             OPEN ahl_csi_unit_test_csr(p_x_line_tbl(i).csi_item_instance_id);
2924             FETCH ahl_csi_unit_test_csr INTO l_dummy;
2925             IF (ahl_csi_unit_test_csr%NOTFOUND) THEN
2926               -- Instance is installed as a component
2927               CLOSE ahl_csi_unit_test_csr;
2928               IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
2929                 Fnd_Message.set_name('AHL', 'AHL_OSP_SHIP_COMPONENT');
2930                 Fnd_Msg_Pub.ADD;
2931               END IF;
2932               RAISE Fnd_Api.g_exc_error;
2933             END IF;
2934             CLOSE ahl_csi_unit_test_csr;
2935           END IF;
2936 
2937 -- yazhou 07-Apr-2006 ends
2938 
2939 
2940           -- Do additional Validation, Defaulting if not deleting Ship Line
2941           IF (p_x_line_tbl(i).operation <> 'D') THEN
2942             IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2943               FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Not a Shipment Delete Operation');
2944             END IF;
2945 
2946             IF (p_x_line_tbl(i).line_type_id = FND_PROFILE.VALUE('AHL_OSP_OE_SHIP_ONLY_ID')) THEN
2947               -- Ship-Only Line
2948               IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2949                 FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Processing a Ship-Only Line');
2950               END IF;
2951               -- Default the Ship Date if not already Set
2952               IF (p_x_line_tbl(i).schedule_ship_date IS NULL OR p_x_line_tbl(i).schedule_ship_date = FND_API.G_MISS_DATE) THEN
2953 
2954 -- yazhou 31-Jul-2006 starts
2955 -- bug fix#5442904
2956                 IF p_x_line_tbl(i).order_type = 'BORROW' THEN
2957                   -- For Borrow Orders, Shipping Out happens after receiving
2958                   p_x_line_tbl(i).schedule_ship_date := SYSDATE + l_service_duration;
2959                 ELSE
2960                   -- For all other Orders, Shipping Date is defaulted to current date
2961                   p_x_line_tbl(i).schedule_ship_date := SYSDATE;
2962                 END IF;
2963 -- yazhou 31-Jul-2006 ends
2964 
2965               END IF;  -- If Ship Date is not Set
2966 
2967               -- For Create only, check that no previous oe ship line exists for this job
2968 --yazhou 06-Jun-2006 starts
2969 --bug fix#5304095
2970               -- For Create only, check that no previous oe ship line exists for this job
2971               OPEN ahl_oe_ship_id_csr(p_x_line_tbl(i).osp_order_id,
2972                                       p_x_line_tbl(i).osp_line_id);
2973 --yazhou 06-Jun-2006 ends
2974 
2975 
2976               FETCH ahl_oe_ship_id_csr INTO l_return_line_id;
2977               IF (ahl_oe_ship_id_csr%FOUND AND p_x_line_tbl(i).operation = 'C') THEN
2978                 CLOSE ahl_oe_ship_id_csr;
2979                 IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
2980                   Fnd_Message.set_name('AHL', 'AHL_OSP_SHIP_LINE_DEFINED');
2981                   Fnd_Msg_Pub.ADD;
2982                 END IF;
2983                 RAISE Fnd_Api.g_exc_error;
2984               END IF;
2985               CLOSE ahl_oe_ship_id_csr;
2986               IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2987                 FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'No Duplicate Shipment Lines Found.');
2988               END IF;
2989 
2990               OPEN ahl_oe_schedule_date_csr(p_x_line_tbl(i).osp_order_id,
2991                                             p_x_line_tbl(i).csi_item_instance_id);
2992               FETCH ahl_oe_schedule_date_csr INTO l_ship_line_id, l_ship_date,
2993                                                   l_return_line_id, l_return_date;
2994 
2995               --If borrow, return line must be defined first
2996               -- Commented out by jaramana on June 3, 2005
2997               -- Since we support auto creation from the Inventory Service Order Project
2998               -- Ship and Return Lines may be created simultaneously. So cannot do this check
2999 /*
3000               IF (p_x_line_tbl(i).order_type = 'BORROW' AND l_return_line_id IS NULL) THEN
3001                 CLOSE ahl_oe_schedule_date_csr;
3002                 IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
3003                   Fnd_Message.set_name('AHL', 'AHL_OSP_SHIP_BORROW_ORDER');
3004                   Fnd_Msg_Pub.ADD;
3005                 END IF;
3006                 RAISE Fnd_Api.g_exc_error;
3007               END IF;
3008 */
3009               -- Check that the schedule ship dates are correct.
3010               IF (l_return_date IS NOT NULL) THEN
3011                 --For Borrow, return date must not be > ship date
3012                 IF(p_x_line_tbl(i).order_type = 'BORROW') THEN
3013                   IF (l_return_date > p_x_line_tbl(i).schedule_ship_date) THEN  -- Note that we are in the context of a Ship-only line
3014                     CLOSE ahl_oe_schedule_date_csr;
3015                     IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
3016                       Fnd_Message.set_name('AHL', 'AHL_OSP_SCHEDULE_DATE_ERROR');
3017                       Fnd_Msg_Pub.ADD;
3018                     END IF;
3019                     RAISE Fnd_Api.g_exc_error;
3020                   END IF;
3021                 -- For exchange orders, the ship date can be after the return date (Advance Exchange)
3022                 ELSIF (p_x_line_tbl(i).order_type <> AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_EXCHANGE) THEN
3023                   --For others, ship date must be before return date
3024                   IF (l_return_date < p_x_line_tbl(i).schedule_ship_date) THEN
3025                     CLOSE ahl_oe_schedule_date_csr;
3026                     IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
3027                       Fnd_Message.set_name('AHL', 'AHL_OSP_SCHEDULE_DATE_ERROR');
3028                       Fnd_Msg_Pub.ADD;
3029                     END IF;
3030                     RAISE Fnd_Api.g_exc_error;
3031                   END IF;  -- Return Date < Ship Date
3032                 END IF; -- Borrow or Exchange or Other
3033               END IF; -- Return Date is not null
3034               CLOSE ahl_oe_schedule_date_csr;
3035             ELSIF  (p_x_line_tbl(i).line_type_id = FND_PROFILE.VALUE('AHL_OSP_OE_RETURN_ID')) THEN
3036               -- Return Line
3037               IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3038                 FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Processing a Return Line');
3039               END IF;
3040               IF (p_x_line_tbl(i).schedule_ship_date IS NULL OR p_x_line_tbl(i).schedule_ship_date = FND_API.G_MISS_DATE) THEN
3041 
3042 -- yazhou 31-Jul-2006 starts
3043 -- bug fix#5442904
3044                 IF p_x_line_tbl(i).order_type = 'BORROW' THEN
3045                   -- For Borrow Orders, Receiving Date is defaulted to current date
3046                   p_x_line_tbl(i).schedule_ship_date := SYSDATE;
3047                 ELSE
3048                   -- For all other Orders, Receiving happens after Shipping
3049                   p_x_line_tbl(i).schedule_ship_date := SYSDATE + l_service_duration;
3050                 END IF;
3051 -- yazhou 31-Jul-2006 ends
3052 
3053               END IF;  -- If Return Date is not Set
3054 
3055               IF p_x_line_tbl(i).return_reason_code IS NULL THEN
3056                  p_x_line_tbl(i).return_reason_code := FND_PROFILE.VALUE('AHL_OSP_OE_LINE_RET_REASON_CODE');
3057               END IF;
3058 
3059               -- For Create only, check that no previous oe return line exists for this job
3060 --yazhou 06-Jun-2006 starts
3061 --bug fix#5304095
3062 
3063               -- For Create only, check that no previous oe return line exists for this job
3064               OPEN ahl_oe_return_id_csr(p_x_line_tbl(i).osp_order_id,
3065                                         p_x_line_tbl(i).osp_line_id);
3066 --yazhou 06-Jun-2006 ends
3067               FETCH ahl_oe_return_id_csr INTO l_dummy;
3068               IF (ahl_oe_return_id_csr%FOUND AND p_x_line_tbl(i).operation = 'C') THEN
3069                 CLOSE ahl_oe_return_id_csr;
3070                 IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
3071                   Fnd_Message.set_name('AHL', 'AHL_OSP_RETURN_LINE_DEFINED');
3072                   Fnd_Msg_Pub.ADD;
3073                 END IF;
3074                 RAISE Fnd_Api.g_exc_error;
3075               END IF;
3076               CLOSE ahl_oe_return_id_csr;
3077 
3078               OPEN ahl_oe_schedule_date_csr(p_x_line_tbl(i).osp_order_id,
3079                                             p_x_line_tbl(i).csi_item_instance_id);
3080               FETCH ahl_oe_schedule_date_csr INTO l_ship_line_id, l_ship_date,
3081                                                   l_return_line_id, l_return_date;
3082 
3083               -- Loan Order ship line check removed by jaramana on June 3, 2005
3084 
3085               -- Check that the schedule ship dates are correct.
3086               IF (l_ship_date IS NOT NULL) THEN
3087                 --For Borrow, ship date must not be < return date
3088                 IF(p_x_line_tbl(i).order_type = 'BORROW') THEN
3089                   IF (l_ship_date < p_x_line_tbl(i).schedule_ship_date) THEN  -- Note that we are in the context of a Return line
3090                     CLOSE ahl_oe_schedule_date_csr;
3091                     IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
3092                       Fnd_Message.set_name('AHL', 'AHL_OSP_SCHEDULE_DATE_ERROR');
3093                       Fnd_Msg_Pub.ADD;
3094                     END IF;
3095                     RAISE Fnd_Api.g_exc_error;
3096                   END IF;
3097                 -- For exchange orders, the ship date can be after the return date (Advance Exchange)
3098                 ELSIF (p_x_line_tbl(i).order_type <> AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_EXCHANGE) THEN
3099                   --For others, return date must be after the ship date
3100                   IF (l_ship_date > p_x_line_tbl(i).schedule_ship_date) THEN
3101                     CLOSE ahl_oe_schedule_date_csr;
3102                     IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
3103                       Fnd_Message.set_name('AHL', 'AHL_OSP_SCHEDULE_DATE_ERROR');
3104                       Fnd_Msg_Pub.ADD;
3105                     END IF;
3106                     RAISE Fnd_Api.g_exc_error;
3107                   END IF; -- Ship Date > Return Date
3108                 END IF; -- Borrow or Exchange or Other
3109               END IF; -- Ship Date is not null
3110               CLOSE ahl_oe_schedule_date_csr;
3111             END IF; -- Line Type Check: Ship or Return
3112           END IF; -- Line Operation is not 'D'
3113         ELSIF l_osp_line_det_type.osp_order_line_id IS NOT NULL THEN   -- workorder_id is null
3114           -- Shipment Line does not correspond to a work order, But still corresponds to a OSP Line
3115           IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3116             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Workorder Id is null and osp_order_line_id is not null');
3117           END IF;
3118           -- Begin changes by jaramana on August 27, 2005 to fix the bug for exchange orders
3119           -- (without work orders), where the return line incorrectly picks up the item of the ship line
3120           -- If the user had picked a new item, save it in a temporary variable so that
3121           -- it can be used later if found applicable.
3122           l_dummy := p_x_line_tbl(i).inventory_item_id;
3123           -- End changes by jaramana on August 27, 2005
3124           -- Get Details for Shipment Line from the OSP Order Line
3125           p_x_line_tbl(i).inventory_item_id := l_osp_line_det_type.inventory_item_id;
3126           p_x_line_tbl(i).serial_number := l_osp_line_det_type.serial_number;
3127           p_x_line_tbl(i).order_quantity_uom:= l_osp_line_det_type.INVENTORY_ITEM_UOM;
3128           p_x_line_tbl(i).ordered_quantity:= l_osp_line_det_type.inventory_item_quantity;
3129           p_x_line_tbl(i).project_id:= null;
3130           p_x_line_tbl(i).task_id:= null;
3131           p_x_line_tbl(i).osp_line_id := l_osp_line_det_type.osp_order_line_id;
3132           p_x_line_tbl(i).osp_line_number := l_osp_line_det_type.osp_line_number;
3133 	      --Added by mpothuku on 9-Nov-05 to fix the Sub-Inventory not getting defaulted issue.
3134           -- Additional condition for defaulting added by yazhou on July 26, 2006 bug fix#5109272
3135           -- Default the subinventory from the OSP Line only if
3136           -- creating Shipment for the Entire OSP Order.
3137           IF (p_x_line_tbl.COUNT > 1) THEN
3138 		  p_x_line_tbl(i).subinventory := l_osp_line_det_type.sub_inventory;
3139           END IF;
3140 		  p_x_line_tbl(i).lot_number := l_osp_line_det_type.lot_number; --jeli 12/01/05
3141 
3142           IF l_osp_line_det_type.serial_number IS NOT NULL THEN
3143             OPEN get_inv_inst_csr(l_osp_line_det_type.inventory_item_id,
3144                                   -- l_osp_line_det_type.inventory_org_id,  -- Changed by jaramana on June 3, 2005
3145                                   l_osp_line_det_type.serial_number);
3146             FETCH get_inv_inst_csr INTO p_x_line_tbl(i).csi_item_instance_id;
3147             CLOSE get_inv_inst_csr;
3148           --MOdification by jrotich bug # 14223880 begin
3149           ELSIF p_x_line_tbl(i).lot_number IS NOT NULL THEN
3150 
3151     			      OPEN get_inv_inst_from_lot_csr(p_x_line_tbl(i).inventory_item_id, p_x_line_tbl(i).lot_number);
3152 	                  FETCH get_inv_inst_from_lot_csr INTO p_x_line_tbl(i).csi_item_instance_id;
3153                       CLOSE get_inv_inst_from_lot_csr;
3154           --modification by jrotich bug # 14223880 end
3155           ELSE
3156             p_x_line_tbl(i).csi_item_instance_id:= null;
3157           END IF;
3158           -- Begin changes by jaramana on August 27, 2005 to fix the bug for exchange orders
3159           -- (without work orders), where the return line incorrectly picks up the values
3160           -- corresponding to the ship line: Serial Number even if the Exch. Instance
3161           -- is not set and Item even if the Exch Instance is set and is of a different
3162           -- item than the ship item.
3163           -- For Exchange Order Return lines, use the exchange instance id instead
3164 
3165 -- yazhou 07-Apr-2006 starts
3166 -- Bug fix #4998349
3167 
3168           -- yazhou 26-Apr-2006 starts
3169           -- Bug fix#5189285
3170 
3171           -- For Exchange Order Return lines, use the exchange instance id instead
3172           IF  (p_x_line_tbl(i).operation <> 'D') AND
3173               ((p_x_line_tbl(i).line_type_id = FND_PROFILE.VALUE('AHL_OSP_OE_RETURN_ID')) AND
3174               (p_x_line_tbl(i).order_type = AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_EXCHANGE)) THEN
3175 
3176           -- yazhou 26-Apr-2006 ends
3177 
3178             /*
3179             IF (l_wo_rec.exchange_instance_id IS NULL OR l_wo_rec.exchange_instance_id = FND_API.G_MISS_NUM) THEN
3180             */
3181             --mpothuku updated on 06-Mar-06 to remove the ref to l_wo_rec as its not being used
3182 
3183 
3184             /* Commented out since exchange instance can be null
3185 			IF (p_x_line_tbl(i).operation <> 'D') THEN
3186                 IF (l_osp_line_det_type.exchange_instance_id IS NULL OR l_osp_line_det_type.exchange_instance_id = FND_API.G_MISS_NUM) THEN
3187                     -- Need to set exchange instance before creating return shipment line for exchange orders
3188                     IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
3189                         Fnd_Message.set_name('AHL', 'AHL_OSP_EXC_INST_ID_NULL');
3190                         Fnd_Msg_Pub.ADD;
3191                     END IF;
3192                     RAISE Fnd_Api.g_exc_error;
3193                 END IF;
3194             END IF;
3195             */
3196 
3197             IF (l_osp_line_det_type.exchange_instance_id IS NOT NULL AND
3198 			   l_osp_line_det_type.exchange_instance_id <> FND_API.G_MISS_NUM) THEN
3199 
3200                 p_x_line_tbl(i).csi_item_instance_id:= l_osp_line_det_type.exchange_instance_id;
3201                 -- Get Details about the Exchange Instance
3202                 OPEN ahl_exc_inst_details(p_x_line_tbl(i).csi_item_instance_id);
3203                 FETCH ahl_exc_inst_details INTO p_x_line_tbl(i).inventory_item_id,
3204                                             p_x_line_tbl(i).serial_number,
3205                                             p_x_line_tbl(i).ordered_quantity,
3206                                             p_x_line_tbl(i).order_quantity_uom,
3207                                             p_x_line_tbl(i).lot_number; --jeli 12/01/05
3208                 CLOSE ahl_exc_inst_details;
3209                 IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3210                     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Fetched Exchange Instance details: ' ||
3211                                    'item_instance_id = ' || p_x_line_tbl(i).csi_item_instance_id ||
3212                                    ', inventory_item_id = ' || p_x_line_tbl(i).inventory_item_id ||
3213                                    ', serial_number = ' || p_x_line_tbl(i).serial_number ||
3214                                    ', ordered_quantity = ' || p_x_line_tbl(i).ordered_quantity ||
3215                                    ', order_quantity_uom = ' || p_x_line_tbl(i).order_quantity_uom ||
3216                                    ', lot_number = ' || p_x_line_tbl(i).lot_number); --jeli 12/01/05
3217                 END IF;
3218                 --jeli 12/01/05
3219 
3220                 --(jeli 12/01/05) Jerry added on 11/01/05 in order to fix bug 4580226
3221                --Added by mpothuku on 14-Dec-05 to fix the following validation that was raised when creating shipment
3222                --from Inventory Item Search UI.
3223                -- Validation only required if calling from front-end
3224                IF  (p_module_type is not null) AND
3225                     (p_x_line_tbl(i).inventory_item_id <> l_old_inv_item_id OR
3226                       NVL(p_x_line_tbl(i).serial_number, 'X') <> NVL(l_old_serial_number, 'X') OR
3227                       p_x_line_tbl(i).ordered_quantity <> l_old_quantity OR
3228                       p_x_line_tbl(i).order_quantity_uom <> l_old_uom OR
3229                       NVL(p_x_line_tbl(i).lot_number, 'X') <> NVL(l_old_lot_number, 'X')) THEN
3230                          Fnd_Message.set_name('AHL', 'AHL_OSP_ITEM_ATTR_NO_CHANGE');
3231                          Fnd_Msg_Pub.ADD;
3232                          RAISE Fnd_Api.g_exc_error;
3233                END IF;
3234                --Bug fix 4580226 ends
3235 
3236             ELSE    -- Exchange instance is null
3237 
3238              -- yazhou 26-Apr-2006 starts
3239              -- Bug fix#5189285
3240 
3241              -- restoring to the item entered by user if calling from front-end
3242              -- also try to derive and set exchange instance
3243              IF (p_module_type is not null) THEN
3244 
3245                  p_x_line_tbl(i).inventory_item_id :=l_old_inv_item_id;
3246                  p_x_line_tbl(i).serial_number := l_old_serial_number;
3247                  p_x_line_tbl(i).lot_number    := l_old_lot_number;
3248                  p_x_line_tbl(i).csi_item_instance_id := null;
3249 
3250                  -- If the serial number user entered corresponds to a tracked instance, derive the
3251                  --  instance number and update the Osp Order Line's Exchange Instance column transparently
3252                  IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3253                     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Exchange Instance id is null ' ||
3254                                    'l_osp_line_det_type.exchange_instance_id = ' || l_osp_line_det_type.exchange_instance_id);
3255                  END IF;
3256 
3257 
3258                  IF p_x_line_tbl(i).inventory_item_id IS NOT NULL  THEN
3259 
3260      			    IF p_x_line_tbl(i).serial_number IS NOT NULL  THEN
3261 
3262     			      OPEN get_inv_inst_csr(p_x_line_tbl(i).inventory_item_id, p_x_line_tbl(i).serial_number);
3263                       FETCH get_inv_inst_csr INTO p_x_line_tbl(i).csi_item_instance_id;
3264                       CLOSE get_inv_inst_csr;
3265 
3266     			    ELSIF p_x_line_tbl(i).lot_number IS NOT NULL THEN
3267 
3268     			      OPEN get_inv_inst_from_lot_csr(p_x_line_tbl(i).inventory_item_id, p_x_line_tbl(i).lot_number);
3269 	                  FETCH get_inv_inst_from_lot_csr INTO p_x_line_tbl(i).csi_item_instance_id;
3270                       CLOSE get_inv_inst_from_lot_csr;
3271 
3272     			    END IF;  --  user entered SN AND/OR Lot number
3273 
3274                     --If the SN/LOT user entered corresponds to a non-tracked item
3275 	    			-- simply ignore the serial number entered by the user without throwing an error.
3276 		    	    IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3277                        FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'p_x_line_tbl.csi_item_instance_id = ' || p_x_line_tbl(i).csi_item_instance_id);
3278                     END IF;
3279 
3280     			    IF p_x_line_tbl(i).csi_item_instance_id IS NOT NULL THEN
3281 
3282     				   -- update the Osp Order Line's Exchange Instance column
3283         	           IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3284                            FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'About to call Update_OSP_Line_Exch_Instance');
3285                        END IF;
3286 
3287     		          Update_OSP_Line_Exch_Instance(
3288                            p_osp_order_id      => p_x_line_tbl(i).osp_order_id,
3289                  	       p_osp_line_id       => p_x_line_tbl(i).osp_line_id,
3290                            p_exchange_instance_id  =>  p_x_line_tbl(i).csi_item_instance_id);
3291 
3292     			    ELSE
3293 
3294     				   -- ignore the SN/LOT number user entered
3295 	     		       p_x_line_tbl(i).serial_number := NULL;
3296 		    	       p_x_line_tbl(i).lot_number := NULL;
3297 			           p_x_line_tbl(i).csi_item_instance_id:= null;
3298 
3299     				END IF; -- exchange instance is derived
3300 
3301     			  END IF;  -- user entered item
3302 
3303               ELSE  -- called from back-end
3304 
3305 	     		       p_x_line_tbl(i).serial_number := NULL;
3306 		    	       p_x_line_tbl(i).lot_number := NULL;
3307 			           p_x_line_tbl(i).csi_item_instance_id:= null;
3308 
3309 
3310               END IF;  -- called from front-end
3311 
3312              -- yazhou 26-Apr-2006 ends
3313 
3314             END IF; --Exchange instance null check
3315 
3316           END IF;  -- Exchange Order Return Lines
3317 
3318           --Check that ship to or return instance is top node in CSI.
3319           -- If not unit, then throw error cause can't ship part of a unit.
3320           IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3321             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'About to perform Component Check.');
3322           END IF;
3323 
3324         IF p_x_line_tbl(i).csi_item_instance_id IS NOT NULL THEN
3325           OPEN ahl_csi_unit_test_csr(p_x_line_tbl(i).csi_item_instance_id);
3326           FETCH ahl_csi_unit_test_csr INTO l_dummy;
3327           IF (ahl_csi_unit_test_csr%NOTFOUND) THEN
3328             -- Instance is installed as a component
3329             CLOSE ahl_csi_unit_test_csr;
3330             IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
3331               Fnd_Message.set_name('AHL', 'AHL_OSP_SHIP_COMPONENT');
3332               Fnd_Msg_Pub.ADD;
3333             END IF;
3334             RAISE Fnd_Api.g_exc_error;
3335           END IF;
3336           CLOSE ahl_csi_unit_test_csr;
3337         END IF;
3338 
3339 -- yazhou 07-Apr-2006 ends
3340 
3341 
3342 
3343           -- Do additional Validation, Defaulting if not deleting Ship Line
3344           IF (p_x_line_tbl(i).operation <> 'D') THEN
3345             IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3346               FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Not a Shipment Delete Operation');
3347             END IF;
3348             IF (p_x_line_tbl(i).line_type_id = FND_PROFILE.VALUE('AHL_OSP_OE_SHIP_ONLY_ID')) THEN
3349               -- Ship-Only Line
3350               IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3351                 FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Processing a Ship-Only Line');
3352               END IF;
3353               -- Default the Ship Date if not already Set
3354               IF (p_x_line_tbl(i).schedule_ship_date IS NULL OR p_x_line_tbl(i).schedule_ship_date = FND_API.G_MISS_DATE) THEN
3355 
3356 -- yazhou 31-Jul-2006 starts
3357 -- bug fix#5442904
3358                 IF p_x_line_tbl(i).order_type = 'BORROW' THEN
3359                   -- For Borrow Orders, Shipping Out happens after receiving
3360                   p_x_line_tbl(i).schedule_ship_date := SYSDATE + l_service_duration;
3361                 ELSE
3362                   -- For all other Orders, Shipping Date is defaulted to current date
3363                   p_x_line_tbl(i).schedule_ship_date := SYSDATE;
3364                 END IF;
3365 -- yazhou 31-Jul-2006 ends
3366 
3367               END IF;  -- If Ship Date is not Set
3368 
3369               --For Create only, check that no previous oe ship line for the Item
3370 --yazhou 06-Jun-2006 starts
3371 --bug fix#5304095
3372 
3373               -- For Create only, check that no previous oe return line exists for this job
3374               OPEN ahl_oe_ship_id_csr(p_x_line_tbl(i).osp_order_id,
3375                                         p_x_line_tbl(i).osp_line_id);
3376 /*
3377               OPEN ahl_oe_ship_id_Inv_csr(p_x_line_tbl(i).osp_order_id,
3378                                           p_x_line_tbl(i).inventory_item_id,
3379                                           p_x_line_tbl(i).inventory_org_id,
3380                                           p_x_line_tbl(i).subinventory,
3381                                           p_x_line_tbl(i).serial_number,
3382                                           p_x_line_tbl(i).lot_number,
3383                                           p_x_line_tbl(i).inventory_item_uom);
3384 */
3385               FETCH ahl_oe_ship_id_csr INTO l_return_line_id;
3386               IF (ahl_oe_ship_id_csr%FOUND AND p_x_line_tbl(i).operation = 'C') THEN
3387                 CLOSE ahl_oe_ship_id_csr;
3388                 IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
3389                   Fnd_Message.set_name('AHL', 'AHL_OSP_SHIP_LINE_DEFINED');
3390                   Fnd_Msg_Pub.ADD;
3391                 END IF;
3392                 RAISE Fnd_Api.g_exc_error;
3393               END IF;
3394               CLOSE ahl_oe_ship_id_csr;
3395 
3396 --yazhou 06-Jun-2006 ends
3397 
3398               IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3399                 FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'No Duplicate Shipment Lines Found.');
3400               END IF;
3401 
3402               OPEN ahl_oe_Inv_schedule_date_csr(p_x_line_tbl(i).osp_order_id,
3403                                                 p_x_line_tbl(i).inventory_item_id,
3404                                                 p_x_line_tbl(i).inventory_org_id,
3405                                                 p_x_line_tbl(i).subinventory,
3406                                                 p_x_line_tbl(i).serial_number,
3407                                                 p_x_line_tbl(i).lot_number,
3408                                                 p_x_line_tbl(i).inventory_item_uom);
3409               FETCH ahl_oe_Inv_schedule_date_csr INTO l_ship_line_id, l_ship_date,
3410                                                       l_return_line_id, l_return_date;
3411 
3412               --If borrow, return line must be defined first
3413               -- Commented out by jaramana on June 3, 2005
3414               -- Since we support auto creation from the Inventory Service Order Project
3415               -- Ship and Return Lines may be created simultaneously. So cannot do this check
3416 /*
3417               IF (p_x_line_tbl(i).order_type = 'BORROW' AND l_return_line_id IS NULL) THEN
3418                 CLOSE ahl_oe_Inv_schedule_date_csr;
3419                 IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
3420                   Fnd_Message.set_name('AHL', 'AHL_OSP_SHIP_BORROW_ORDER');
3421                   Fnd_Msg_Pub.ADD;
3422                 END IF;
3423                 RAISE Fnd_Api.g_exc_error;
3424               END IF;
3425 */
3426               --Check that the schedule ship dates are correct.
3427               IF (l_return_date IS NOT NULL) THEN
3428                 --For Borrow, return date must not be > ship date
3429                 IF(p_x_line_tbl(i).order_type = 'BORROW') THEN
3430                   IF (l_return_date > p_x_line_tbl(i).schedule_ship_date) THEN -- Note that we are in the context of a Ship-only line
3431                     CLOSE ahl_oe_Inv_schedule_date_csr;
3432                     IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
3433                       Fnd_Message.set_name('AHL', 'AHL_OSP_SCHEDULE_DATE_ERROR');
3434                       Fnd_Msg_Pub.ADD;
3435                     END IF;
3436                     RAISE Fnd_Api.g_exc_error;
3437                   END IF;
3438                   -- For exchange orders, the ship date can be after the return date (Advance Exchange)
3439                 ELSIF (p_x_line_tbl(i).order_type <> AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_EXCHANGE) THEN
3440                   --For others, ship date must be before return date
3441                   IF (l_return_date < p_x_line_tbl(i).schedule_ship_date) THEN
3442                     CLOSE ahl_oe_Inv_schedule_date_csr;
3443                     IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
3444                       Fnd_Message.set_name('AHL', 'AHL_OSP_SCHEDULE_DATE_ERROR');
3445                       Fnd_Msg_Pub.ADD;
3446                     END IF;
3447                     RAISE Fnd_Api.g_exc_error;
3448                   END IF;  -- Return Date < Ship Date
3449                 END IF;  -- Borrow or Exchange or Other
3450               END IF; -- Return Date is not null
3451               CLOSE ahl_oe_Inv_schedule_date_csr;
3452             ELSIF  (p_x_line_tbl(i).line_type_id = FND_PROFILE.VALUE('AHL_OSP_OE_RETURN_ID')) THEN
3453               IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3454                 FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Processing a Return Line');
3455               END IF;
3456               IF (p_x_line_tbl(i).schedule_ship_date IS NULL OR p_x_line_tbl(i).schedule_ship_date = FND_API.G_MISS_DATE) THEN
3457 
3458 -- yazhou 31-Jul-2006 starts
3459 -- bug fix#5442904
3460                 IF p_x_line_tbl(i).order_type = 'BORROW' THEN
3461                   -- For Borrow Orders, Receiving Date is defaulted to current date
3462                   p_x_line_tbl(i).schedule_ship_date := SYSDATE;
3463                 ELSE
3464                   -- For all other Orders, Receiving happens after Shipping
3465                   p_x_line_tbl(i).schedule_ship_date := SYSDATE + l_service_duration;
3466                 END IF;
3467 -- yazhou 31-Jul-2006 ends
3468 
3469               END IF;  -- If Return Date is not Set
3470 
3471               IF p_x_line_tbl(i).return_reason_code IS NULL THEN
3472                  p_x_line_tbl(i).return_reason_code := FND_PROFILE.VALUE('AHL_OSP_OE_LINE_RET_REASON_CODE');
3473               END IF;
3474 
3475               --For Create only, check that no previous oe return line exists for the Item
3476 --yazhou 06-Jun-2006 starts
3477 --bug fix#5304095
3478 
3479               -- For Create only, check that no previous oe return line exists for this job
3480               OPEN ahl_oe_return_id_csr(p_x_line_tbl(i).osp_order_id,
3481                                         p_x_line_tbl(i).osp_line_id);
3482 /*              OPEN ahl_oe_inv_return_id_csr(p_x_line_tbl(i).osp_order_id,
3483                                             p_x_line_tbl(i).inventory_item_id,
3484                                             p_x_line_tbl(i).inventory_org_id,
3485                                             p_x_line_tbl(i).subinventory,
3486                                             p_x_line_tbl(i).serial_number,
3487                                             p_x_line_tbl(i).lot_number,
3488                                             p_x_line_tbl(i).inventory_item_uom);
3489   */
3490 
3491 
3492               FETCH ahl_oe_return_id_csr INTO l_dummy;
3493 
3494               IF (ahl_oe_return_id_csr%FOUND AND p_x_line_tbl(i).operation = 'C') THEN
3495 
3496                 CLOSE ahl_oe_return_id_csr;
3497                 IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
3498                   Fnd_Message.set_name('AHL', 'AHL_OSP_RETURN_LINE_DEFINED');
3499                   Fnd_Msg_Pub.ADD;
3500                 END IF;
3501                 RAISE Fnd_Api.g_exc_error;
3502               END IF;
3503               CLOSE ahl_oe_return_id_csr;
3504 
3505 --yazhou 06-Jun-2006 ends
3506 
3507               OPEN ahl_oe_Inv_schedule_date_csr(p_x_line_tbl(i).osp_order_id,
3508                                                 p_x_line_tbl(i).inventory_item_id,
3509                                                 p_x_line_tbl(i).inventory_org_id,
3510                                                 p_x_line_tbl(i).subinventory,
3511                                                 p_x_line_tbl(i).serial_number,
3512                                                 p_x_line_tbl(i).lot_number,
3513                                                 p_x_line_tbl(i).inventory_item_uom);
3514               FETCH ahl_oe_Inv_schedule_date_csr INTO l_ship_line_id, l_ship_date,
3515                                                       l_return_line_id, l_return_date;
3516 
3517               -- Loan Order ship line check removed by jaramana on June 3, 2005
3518 
3519               -- Check that the schedule ship dates are correct.
3520               IF (l_ship_date IS NOT NULL) THEN
3521                 --For Borrow, ship date must not be < return date
3522                 IF(p_x_line_tbl(i).order_type = 'BORROW') THEN
3523                   IF (l_ship_date < p_x_line_tbl(i).schedule_ship_date) THEN  -- Note that we are in the context of a Return line
3524                     CLOSE ahl_oe_Inv_schedule_date_csr;
3525                     IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
3526                       Fnd_Message.set_name('AHL', 'AHL_OSP_SCHEDULE_DATE_ERROR');
3527                       Fnd_Msg_Pub.ADD;
3528                     END IF;
3529                     RAISE Fnd_Api.g_exc_error;
3530                   END IF;
3531                 -- For exchange orders, the ship date can be after the return date (Advance Exchange)
3532                 ELSIF (p_x_line_tbl(i).order_type <> AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_EXCHANGE) THEN
3533                   --For others, return date must be after the ship date
3534                   IF (l_ship_date > p_x_line_tbl(i).schedule_ship_date) THEN
3535                     CLOSE ahl_oe_Inv_schedule_date_csr;
3536                     IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
3537                       Fnd_Message.set_name('AHL', 'AHL_OSP_SCHEDULE_DATE_ERROR');
3538                       Fnd_Msg_Pub.ADD;
3539                     END IF;
3540                     RAISE Fnd_Api.g_exc_error;
3541                   END IF;  -- Ship Date > Return Date
3542                 END IF;  -- Borrow or Exchange or Other
3543               END IF;  -- Ship Date is not null
3544               CLOSE ahl_oe_Inv_schedule_date_csr;
3545             END IF;  -- Line Type Check: Ship or Return
3546           END IF;  -- Line Operation is not 'D'
3547         END IF;  -- Work Order or OSP Order Line Id is not Null
3548       ELSE
3549         -- OSP Line Flag is Not Y: Shipment Line is for a spare part or tool.
3550         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3551           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'osp_line_flag is NOT Y: Shipment Line is for a spare part or tool.');
3552         END IF;
3553         p_x_line_tbl(i).osp_line_id := null;
3554         p_x_line_tbl(i).osp_line_number := null;
3555 
3556         IF p_x_line_tbl(i).schedule_ship_date < TRUNC(sysdate) THEN
3557           Fnd_Message.set_name('AHL', 'AHL_OSP_SHIP_DATE_LT_SYS');
3558           Fnd_Msg_Pub.ADD;
3559           RAISE Fnd_Api.g_exc_error;
3560         END IF;
3561 
3562         IF (p_x_line_tbl(i).line_type_id = FND_PROFILE.VALUE('AHL_OSP_OE_SHIP_ONLY_ID')) THEN
3563           p_x_line_tbl(i).line_type := 'SHIP';
3564         ELSIF (p_x_line_tbl(i).line_type_id = FND_PROFILE.VALUE('AHL_OSP_OE_RETURN_ID')) THEN
3565           p_x_line_tbl(i).line_type := 'RETURN';
3566         ELSE
3567           Fnd_Message.set_name('AHL', 'AHL_OSP_LINE_TYPE_NULL');
3568           Fnd_Msg_Pub.ADD;
3569         END IF;
3570         IF p_x_line_tbl(i).line_type IS NULL THEN
3571           Fnd_Message.set_name('AHL', 'AHL_OSP_LINE_TYPE_NULL');
3572           Fnd_Msg_Pub.ADD;
3573         ELSIF p_x_line_tbl(i).line_type = 'RETURN' AND p_x_line_tbl(i).return_reason_code IS NULL THEN
3574            Fnd_Message.set_name('AHL', 'AHL_OSP_RETURN_REASON_NULL');
3575            Fnd_Msg_Pub.ADD;
3576         END IF;
3577 
3578         IF p_x_line_tbl(i).osp_order_id IS NULL THEN
3579             Fnd_Message.set_name('AHL', 'AHL_OSP_PRIMARY_KEY_NULL');
3580             Fnd_Msg_Pub.ADD;
3581         END IF;
3582 
3583 
3584         -- Validate the Spare part to be shipped
3585         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3586           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'About to validate spare item to be shipped with ' ||
3587                             'p_x_line_tbl(i).inventory_item_id = ' || p_x_line_tbl(i).inventory_item_id ||
3588                             ', p_x_line_tbl(i).inventory_org_id = ' || p_x_line_tbl(i).inventory_org_id ||
3589                             ', p_x_line_tbl(i).serial_number = ' || p_x_line_tbl(i).serial_number ||
3590                             ', p_x_line_tbl(i).LOT_NUMBER = ' || p_x_line_tbl(i).LOT_NUMBER);
3591         END IF;
3592         IF p_x_line_tbl(i).line_id IS NULL THEN
3593 
3594         OPEN validate_item(p_x_line_tbl(i).inventory_item_id,
3595                            p_x_line_tbl(i).inventory_org_id);
3596         --Following arguments are removed by mpothuku on 06-Mar-06 to fix the Perf Bug #4919255
3597         --We are deliberately escaping the Validations of serial number and lot number for now.
3598                            --p_x_line_tbl(i).serial_number,
3599                            --p_x_line_tbl(i).LOT_NUMBER
3600         FETCH validate_item INTO l_dummy;
3601         IF validate_item%NOTFOUND THEN
3602           Fnd_Message.set_name('AHL', 'AHL_OSP_INVALID_INV_ITEM');
3603           Fnd_Msg_Pub.ADD;
3604           CLOSE validate_item;
3605           RAISE Fnd_Api.g_exc_error;
3606         END IF;
3607         CLOSE validate_item;
3608         END IF; -- IF p_x_line_tbl(i).line_id IS NULL
3609       END IF; -- OSP Line Flag is Y or Not Y
3610 
3611       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3612         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Completed Processing Ship Line with index i = ' || i);
3613       END IF;
3614     END LOOP;
3615 
3616   /*
3617   Added by mpothuku on 04-May-06
3618   The following Logic is used when creating shipment from Edit Osp Order UI's "Create Shipment" Action
3619   */
3620 
3621   ELSIF p_operation_flag = 'C' THEN  -- Line Table Count is Zero
3622     -- Creating Shipment using OSP Header
3623     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3624       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Line Table Count is Zero: Creating Shipment using OSP Header');
3625     END IF;
3626 
3627 -- yazhou 31-Jul-2006 starts
3628 -- bug fix#5442904
3629 /*
3630     OPEN osp_order_header_csr(p_osp_order_id => p_osp_order_id);
3631     FETCH osp_order_header_csr INTO l_osp_header_det;
3632     CLOSE osp_order_header_csr;
3633 */
3634 -- yazhou 31-Jul-2006 ends
3635 
3636     l_index := 0;
3637     FOR I IN osp_line_details_csr2(p_osp_order_id) LOOP
3638       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3639         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Getting details for OSP Line with id ' || I.osp_order_line_id);
3640       END IF;
3641       -- Modified by jaramana on January 11, 2008 to fix the Bug 5688387/5842229
3642       l_index := l_index + 2;
3643       -- First Prepare data for Return Lines
3644       p_x_line_tbl(l_index).line_id := NULL;
3645       p_x_line_tbl(l_index).line_number := NULL;
3646       p_x_line_tbl(l_index).header_id := l_osp_header_det.OE_HEADER_ID;
3647       p_x_line_tbl(l_index).order_type := l_osp_header_det.ORDER_TYPE_CODE;
3648       p_x_line_tbl(l_index).line_type_id := FND_PROFILE.VALUE('AHL_OSP_OE_RETURN_ID');
3649       p_x_line_tbl(l_index).line_type := 'RETURN';
3650       p_x_line_tbl(l_index).operation := 'C';
3651       /* NOTE: Exchange Order is handled in a special way below
3652          after the ship-only lines are created*/
3653       p_x_line_tbl(l_index).inventory_item_id := I.inventory_item_id;
3654       p_x_line_tbl(l_index).inventory_org_id := I.INVENTORY_ORG_ID;
3655       p_x_line_tbl(l_index).inventory_item := NULL;
3656       p_x_line_tbl(l_index).LOT_NUMBER := I.LOT_NUMBER;
3657       p_x_line_tbl(l_index).INVENTORY_ITEM_UOM := NULL;
3658       p_x_line_tbl(l_index).INVENTORY_ITEM_QUANTITY := NULL;
3659       p_x_line_tbl(l_index).serial_number := I.SERIAL_NUMBER;
3660       p_x_line_tbl(l_index).csi_item_instance_id := NULL;
3661       p_x_line_tbl(l_index).ordered_quantity := I.INVENTORY_ITEM_QUANTITY;
3662       p_x_line_tbl(l_index).order_quantity_uom := I.INVENTORY_ITEM_UOM;
3663 
3664       -- Default the Return Reason from the Profile
3665       p_x_line_tbl(l_index).return_reason_code := FND_PROFILE.VALUE('AHL_OSP_OE_LINE_RET_REASON_CODE');
3666       IF p_x_line_tbl(l_index).return_reason_code IS NULL THEN
3667         FND_MESSAGE.set_name('AHL', 'AHL_OSP_PROFILE_NULL');
3668         -- @@@@@ jaramana June 4, 2004: May become a translation issue
3669         --FND_MESSAGE.SET_TOKEN('PROFILE', 'AHL: OM Line Return Reason');
3670         FND_MESSAGE.SET_TOKEN('PROFILE', get_user_profile_option_name('AHL_OSP_OE_LINE_RET_REASON_CODE'));
3671         FND_MSG_PUB.ADD;
3672         RAISE Fnd_Api.g_exc_error;
3673       END IF;
3674       p_x_line_tbl(l_index).return_reason := NULL;
3675 
3676 -- yazhou 31-Jul-2006 starts
3677 -- bug fix#5442904
3678 -- use user defined vendor service duration if available
3679 
3680         l_service_duration := null;
3681 
3682         IF l_osp_header_det.vendor_id is not null
3683         AND l_osp_header_det.vendor_site_id is not NULL THEN
3684 
3685             OPEN ahl_max_service_duration_csr(l_osp_header_det.vendor_id, l_osp_header_det.vendor_site_id, p_osp_order_id,I.inventory_item_id,I.INVENTORY_ORG_ID,I.SERIAL_NUMBER );
3686             FETCH ahl_max_service_duration_csr INTO l_service_duration;
3687             CLOSE ahl_max_service_duration_csr;
3688 
3689         END IF;
3690 
3691         IF l_service_duration is null THEN
3692             -- Check if the Default Duration Profile has been Set
3693             IF (FND_PROFILE.VALUE('AHL_VENDOR_SERVICE_DURATION') IS NULL OR FND_PROFILE.VALUE('AHL_VENDOR_SERVICE_DURATION') = '') THEN
3694                 FND_MESSAGE.set_name('AHL', 'AHL_OSP_PROFILE_NULL');
3695                 --FND_MESSAGE.SET_TOKEN('PROFILE', 'AHL: Vendor Service Duration');
3696                 FND_MESSAGE.SET_TOKEN('PROFILE', get_user_profile_option_name('AHL_VENDOR_SERVICE_DURATION'));
3697                 FND_MSG_PUB.ADD;
3698                 IF (FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3699                     FND_LOG.MESSAGE(FND_LOG.LEVEL_UNEXPECTED, L_DEBUG_KEY, FALSE);
3700                 END IF;
3701                 RAISE Fnd_Api.g_exc_error;
3702             END IF;
3703 
3704             l_service_duration := FND_PROFILE.VALUE('AHL_VENDOR_SERVICE_DURATION');
3705 
3706         END IF;
3707 
3708 
3709       IF p_x_line_tbl(l_index).order_type = 'BORROW' THEN
3710         -- For Borrow Orders, the Return Shipment is defaulted to current date
3711         p_x_line_tbl(l_index).schedule_ship_date := SYSDATE;
3712       ELSE
3713         -- For all other types of orders, the Return Shipment happens x days after current date
3714         p_x_line_tbl(l_index).schedule_ship_date := SYSDATE + l_service_duration;
3715       END IF;
3716 -- yazhou 31-Jul-2006 ends
3717 
3718       p_x_line_tbl(l_index).packing_instructions := NULL;
3719       p_x_line_tbl(l_index).ship_from_org := NULL;
3720       p_x_line_tbl(l_index).ship_from_org_id := I.INVENTORY_ORG_ID;
3721       p_x_line_tbl(l_index).fob_point := NULL;
3722       p_x_line_tbl(l_index).fob_point_code := NULL;
3723       p_x_line_tbl(l_index).freight_carrier := NULL;
3724       p_x_line_tbl(l_index).freight_carrier_code := NULL;
3725       p_x_line_tbl(l_index).freight_terms := NULL;
3726       p_x_line_tbl(l_index).freight_terms_code := NULL;
3727       p_x_line_tbl(l_index).shipment_priority_code := NULL;
3728       p_x_line_tbl(l_index).shipment_priority := NULL;
3729       p_x_line_tbl(l_index).shipping_method_code := NULL;
3730       p_x_line_tbl(l_index).shipping_method := NULL;
3731       --Jeli updated the following line for fixing the sub-inventory default issue
3732       --found by AE on 11-08-05
3733       --p_x_line_tbl(l_index).subinventory := NULL;
3734       p_x_line_tbl(l_index).subinventory := I.sub_inventory;
3735       p_x_line_tbl(l_index).osp_order_id := I.OSP_ORDER_ID;
3736       p_x_line_tbl(l_index).osp_order_number := NULL;
3737       p_x_line_tbl(l_index).osp_line_id := I.OSP_ORDER_LINE_ID;
3738       p_x_line_tbl(l_index).osp_line_number := I.OSP_LINE_NUMBER;
3739       p_x_line_tbl(l_index).instance_id := NULL;
3740 
3741       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3742         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'OSP Line based and sub_inv='||p_x_line_tbl(l_index).subinventory);
3743       END IF;
3744 
3745       -- Get additional details from the Work order, if the OSP Line is work order based
3746       IF I.WORKORDER_ID IS NOT NULL THEN
3747         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3748           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'OSP Line is Work order based. Getting details from Workorder with id ' || I.WORKORDER_ID);
3749         END IF;
3750         OPEN ahl_osp_work_csr(I.WORKORDER_ID);
3751         FETCH ahl_osp_work_csr INTO l_work_det;
3752         -- jaramana June 4, 2005: Added check for Workorder
3753         IF(ahl_osp_work_csr%NOTFOUND) THEN
3754           FND_MESSAGE.Set_Name(G_APP_NAME, 'AHL_OSP_LN_INV_WO');
3755           FND_MESSAGE.Set_Token('WORKORDER_ID', I.WORKORDER_ID);
3756           CLOSE ahl_osp_work_csr;
3757           RAISE Fnd_Api.g_exc_error;
3758         END IF;
3759         CLOSE ahl_osp_work_csr;
3760         p_x_line_tbl(l_index).inventory_item_id :=  l_work_det.inventory_item_id;
3761         p_x_line_tbl(l_index).serial_number :=  l_work_det.serial_number;
3762         p_x_line_tbl(l_index).order_quantity_uom:=  l_work_det.item_instance_uom;
3763         -- Changed by jaramana on 02-MAR-2010 for Bug 9307889
3764         -- Get the Quantity from the OSP Line instead of from the instance
3765         -- p_x_line_tbl(l_index).ordered_quantity:=  l_work_det.quantity;
3766         p_x_line_tbl(l_index).ordered_quantity := I.inventory_item_quantity;
3767         p_x_line_tbl(l_index).project_id := l_work_det.project_id;
3768         p_x_line_tbl(l_index).task_id := l_work_det.project_task_id;
3769         p_x_line_tbl(l_index).csi_item_instance_id:=  l_work_det.item_instance_id;
3770         p_x_line_tbl(l_index).ship_from_org_id:= l_work_det.ORGANIZATION_ID;
3771 
3772         --Added by mpothuku on 04-May-06 to fix the Bug 5113542
3773         --Check that ship to or return instance is top node in CSI.
3774         -- If not unit, then throw error cause can't ship part of a unit.
3775         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3776           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'About to perform Component Check.');
3777         END IF;
3778 
3779         IF p_x_line_tbl(l_index).csi_item_instance_id IS NOT NULL THEN
3780           OPEN ahl_csi_unit_test_csr(p_x_line_tbl(l_index).csi_item_instance_id);
3781           FETCH ahl_csi_unit_test_csr INTO l_dummy;
3782           IF (ahl_csi_unit_test_csr%NOTFOUND) THEN
3783             -- Instance is installed as a component
3784             CLOSE ahl_csi_unit_test_csr;
3785             IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
3786               Fnd_Message.set_name('AHL', 'AHL_OSP_SHIP_COMPONENT');
3787               Fnd_Msg_Pub.ADD;
3788             END IF;
3789             RAISE Fnd_Api.g_exc_error;
3790           END IF;
3791           CLOSE ahl_csi_unit_test_csr;
3792         END IF;
3793         --mpothuku on 04-May-06 Ends
3794 
3795       ELSIF p_x_line_tbl(l_index).serial_number IS NOT NULL THEN
3796         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3797           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'OSP Line is not Work order based. Getting details for Serial Number ' || p_x_line_tbl(l_index).serial_number);
3798         END IF;
3799         OPEN get_inv_inst_csr(p_x_line_tbl(l_index).inventory_item_id ,
3800                               -- p_x_line_tbl(l_index).inventory_org_id,
3801                               p_x_line_tbl(l_index).serial_number);
3802         FETCH get_inv_inst_csr INTO p_x_line_tbl(l_index).csi_item_instance_id;
3803         CLOSE get_inv_inst_csr;
3804       -- Added by jaramana on June 4, 2005 to get details about lot controlled items
3805       ELSIF p_x_line_tbl(l_index).lot_number IS NOT NULL THEN
3806         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3807           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'OSP Line is not Work order based. Getting details for Lot Number ' || p_x_line_tbl(l_index).lot_number);
3808         END IF;
3809         OPEN get_inv_inst_from_lot_csr(p_x_line_tbl(l_index).inventory_item_id ,
3810                               p_x_line_tbl(l_index).lot_number);
3811         FETCH get_inv_inst_from_lot_csr INTO p_x_line_tbl(l_index).csi_item_instance_id;
3812         CLOSE get_inv_inst_from_lot_csr;
3813       END IF;
3814 
3815       -- Added by jaramana on January 11, 2008 for the Bug 5688387/5842229.
3816       -- All return lines were being created upfront and then the ship lines
3817       -- which was creating confusion with the sequencing of the ship/return lines
3818       -- Changing the logic henceforth.
3819       l_ship_index := l_index-1;
3820       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3821         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Defaulting the shipline: index ' || l_ship_index );
3822       END IF;
3823 
3824       -- Copy all the details about from the Return Line
3825       p_x_line_tbl(l_ship_index) := p_x_line_tbl(l_index);
3826       p_x_line_tbl(l_ship_index).line_type_id:=FND_PROFILE.VALUE('AHL_OSP_OE_SHIP_ONLY_ID');
3827       p_x_line_tbl(l_ship_index).line_type:='SHIP';
3828       p_x_line_tbl(l_ship_index).return_reason_code:=NULL;
3829 
3830       IF p_x_line_tbl(l_ship_index).order_type = 'BORROW' THEN
3831         p_x_line_tbl(l_ship_index).schedule_ship_date := SYSDATE + l_service_duration;
3832       ELSE
3833         p_x_line_tbl(l_ship_index).schedule_ship_date := SYSDATE;
3834       END IF;
3835 
3836       /* For Exchange Orders, the return lines must reflect the exchange instance.
3837          l_index corresponds to the index of the return line
3838       */
3839       IF (p_x_line_tbl(l_index).order_type = AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_EXCHANGE) THEN
3840         -- Get the exchange instance and the instance details
3841         OPEN exchange_instance_dtls_csr(p_x_line_tbl(l_index).osp_line_id);
3842         FETCH exchange_instance_dtls_csr INTO l_exchange_inst_dtls;
3843 
3844         -- exchange instance can be null
3845         IF (exchange_instance_dtls_csr%FOUND) THEN
3846           -- exchange instance is not null
3847           p_x_line_tbl(l_index).inventory_item_id :=    l_exchange_inst_dtls.inventory_item_id;
3848           p_x_line_tbl(l_index).inventory_org_id :=     l_exchange_inst_dtls.last_vld_organization_id;
3849           p_x_line_tbl(l_index).lot_number :=           l_exchange_inst_dtls.lot_number;
3850           p_x_line_tbl(l_index).serial_number :=        l_exchange_inst_dtls.serial_number;
3851           p_x_line_tbl(l_index).ordered_quantity :=     l_exchange_inst_dtls.quantity;
3852           p_x_line_tbl(l_index).order_quantity_uom :=   l_exchange_inst_dtls.unit_of_measure;
3853           p_x_line_tbl(l_index).csi_item_instance_id := l_exchange_inst_dtls.exchange_instance_id;
3854           --@@@@@ Need to check if p_x_line_tbl(l_index).ship_from_org_id is valid
3855         ELSE
3856           -- exchange instance is null
3857           p_x_line_tbl(l_index).serial_number := NULL;
3858           p_x_line_tbl(l_index).lot_number    := NULL;
3859           p_x_line_tbl(l_index).csi_item_instance_id := null;
3860         END IF;
3861         CLOSE exchange_instance_dtls_csr;
3862       END IF;
3863       -- jaramana End
3864 
3865       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3866         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Completed details for OSP Line with id ' || I.osp_order_line_id);
3867       END IF;
3868     END LOOP;
3869 
3870   END IF;  -- Line Table Count is Zero or c_operation_flag = 'C'
3871   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3872     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'Exiting Procedure');
3873   END IF;
3874 END Process_Line_Tbl;
3875 
3876 
3877 PROCEDURE Convert_Line_Tbl(p_line_tbl  IN AHL_OSP_SHIPMENT_PUB.SHIP_LINE_TBL_TYPE,
3878                            p_module_type         IN            VARCHAR2 ,
3879                            x_line_tbl  OUT NOCOPY OE_ORDER_PUB.LINE_TBL_TYPE,
3880                            x_line_val_tbl OUT NOCOPY OE_ORDER_PUB.LINE_VAL_TBL_TYPE,
3881                            x_lot_serial_tbl OUT NOCOPY OE_ORDER_PUB.LOT_SERIAL_TBL_TYPE,
3882                            x_del_oe_lines_tbl OUT NOCOPY SHIP_ID_TBL_TYPE) IS
3883 --
3884   l_count          NUMBER;
3885   l_line_rec       OE_ORDER_PUB.LINE_REC_TYPE;
3886   l_line_val_rec   OE_ORDER_PUB.LINE_VAL_REC_TYPE;
3887   l_lot_serial_rec OE_ORDER_PUB.LOT_SERIAL_REC_TYPE;
3888   l_lot_serial_id  NUMBER;
3889   l_del_count      NUMBER := 0;
3890   l_line_table_index NUMBER := 0;
3891 --
3892   CURSOR ahl_oe_header_csr(p_oe_header_id IN NUMBER) IS
3893    SELECT ship_to_org_id,
3894           sold_to_org_id,
3895           sold_from_org_id,
3896           price_list_id,
3897           payment_term_id
3898     FROM  oe_order_headers_all
3899    WHERE  header_id = p_oe_header_id;
3900 
3901  --Used inv_organization_info_v instead of org_organization_definitions to fix the Perf Bug #4919255
3902   CURSOR ahl_ship_from_orgs_csr(p_name IN VARCHAR2) IS
3903    SELECT org.organization_id
3904    FROM OE_SHIP_FROM_ORGS_V org, inv_organization_info_v def
3905    WHERE org.organization_id = def.organization_id
3906    -- Changed by jaramana on Sep 9, 2005 for MOAC Uptake
3907    -- AND def.operating_unit = FND_PROFILE.VALUE('DEFAULT_ORG_ID')
3908     AND def.operating_unit = MO_GLOBAL.get_current_org_id()
3909    AND org.name = p_name;
3910 
3911   CURSOR ahl_oe_lot_serial_id (p_oe_line_id IN NUMBER) IS
3912     SELECT lot_serial_id
3913      FROM oe_lot_serial_numbers
3914     WHERE line_id = p_oe_line_id;
3915 
3916   L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.Convert_Line_Tbl';
3917 --
3918 BEGIN
3919   l_count := 1;
3920   --Convert our rec type into OE_ORDER_PUB entities
3921 
3922   x_line_tbl := OE_ORDER_PUB.G_MISS_LINE_TBL;
3923   x_line_val_tbl := OE_ORDER_PUB.G_MISS_LINE_VAL_TBL;
3924   x_lot_serial_tbl := OE_ORDER_PUB.G_MISS_LOT_SERIAL_TBL;
3925 
3926   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3927     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Entering Procedure');
3928   END IF;
3929 
3930   IF (p_line_tbl.COUNT > 0) THEN
3931     FOR i IN p_line_tbl.FIRST..p_line_tbl.LAST  LOOP
3932       --Handle Delete operations first, differently than the rest/
3933       IF (p_line_tbl(i).operation = 'D') THEN
3934         /*
3935         -- July 23, 2003: Deletion of lines should go through the Delete_Cancel_Order API
3936         -- Not through the regular process.
3937         -- This is to support the Post-Shipment Conversion process where deletion of shipment (return) lines
3938         -- from the UI (after the order has been booked) is necessary.
3939         -- If the shipment is booked, Delete_Cancel_Order just zeroes out the ordered quantity.
3940         -- If not, it actually deletes the shipment line.
3941         l_line_rec := OE_LINE_UTIL.QUERY_ROW(p_line_id => p_line_tbl(i).line_id);
3942         l_line_val_rec :=  OE_ORDER_PUB.G_MISS_LINE_VAL_REC;
3943         l_line_rec.operation := OE_GLOBALS.G_OPR_DELETE;
3944         */
3945         l_del_count := l_del_count + 1;
3946         x_del_oe_lines_tbl(l_del_count) := p_line_tbl(i).line_id;
3947       ELSE
3948         l_line_rec :=  OE_ORDER_PUB.G_MISS_LINE_REC;
3949         l_line_val_rec :=  OE_ORDER_PUB.G_MISS_LINE_VAL_REC;
3950 
3951         IF (p_line_tbl(i).line_id IS NOT NULL) THEN
3952           l_line_rec.line_id := p_line_tbl(i).line_id;
3953         END IF;
3954 
3955         IF (p_line_tbl(i).line_number IS NOT NULL) THEN
3956           l_line_rec.line_number := p_line_tbl(i).line_number;
3957         END IF;
3958 
3959         IF (p_line_tbl(i).header_id IS NOT NULL)  THEN
3960           l_line_rec.header_id := p_line_tbl(i).header_id;
3961         END IF;
3962 
3963         IF (p_line_tbl(i).line_type_id IS NOT NULL)  THEN
3964           l_line_rec.line_type_id := p_line_tbl(i).line_type_id;
3965         END IF;
3966         IF (p_line_tbl(i).line_type IS NOT NULL)  THEN
3967           l_line_val_rec.line_type := p_line_tbl(i).line_type;
3968         END IF;
3969 
3970         IF (p_line_tbl(i).inventory_item_id IS NOT NULL)  THEN
3971           l_line_rec.inventory_item_id := p_line_tbl(i).inventory_item_id;
3972         END IF;
3973         IF (p_line_tbl(i).inventory_item IS NOT NULL)  THEN
3974           l_line_val_rec.inventory_item := p_line_tbl(i).inventory_item;
3975         END IF;
3976 
3977         IF (p_line_tbl(i).ordered_quantity IS NOT NULL)  THEN
3978           l_line_rec.ordered_quantity := p_line_tbl(i).ordered_quantity;
3979         END IF;
3980         IF (p_line_tbl(i).order_quantity_uom IS NOT NULL)  THEN
3981           l_line_rec.order_quantity_uom := p_line_tbl(i).order_quantity_uom;
3982         END IF;
3983 
3984         IF (p_line_tbl(i).return_reason_code IS NOT NULL)  THEN
3985           l_line_rec.return_reason_code := p_line_tbl(i).return_reason_code;
3986         END IF;
3987         IF (p_line_tbl(i).return_reason IS NOT NULL)  THEN
3988           l_line_val_rec.return_reason := p_line_tbl(i).return_reason;
3989         END IF;
3990 
3991         l_line_rec.schedule_ship_date := p_line_tbl(i).schedule_ship_date;
3992 
3993         --NEED TO FIX: CHANGE TO ADD SERIAL NUMBER MESSAGE.
3994         l_line_rec.shipping_instructions := p_line_tbl(i).serial_number;
3995         l_line_rec.packing_instructions := p_line_tbl(i).packing_instructions;
3996 
3997         l_line_val_rec.project   := p_line_tbl(i).project;
3998         l_line_rec.project_id    := p_line_tbl(i).project_id;
3999         l_line_val_rec.task      := p_line_tbl(i).task;
4000         l_line_rec.task_id       := p_line_tbl(i).task_id ;
4001 
4002         -- Hack to find ship_from_org_id because the OE Value_to_ID converter
4003         -- does not exist.
4004         IF (p_module_type = 'JSP') THEN
4005           IF (p_line_tbl(i).ship_from_org IS NOT NULL) THEN
4006             OPEN ahl_ship_from_orgs_csr(p_line_tbl(i).ship_from_org);
4007             FETCH ahl_ship_from_orgs_csr INTO l_line_rec.ship_from_org_id;
4008             IF (ahl_ship_from_orgs_csr%NOTFOUND) THEN
4009               CLOSE ahl_ship_from_orgs_csr;
4010               IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
4011                 FND_MESSAGE.set_name('AHL', 'AHL_OSP_SHIP_FROM_ORG_INV');
4012                 FND_MESSAGE.SET_TOKEN('SHIP_FROM_ORG', p_line_tbl(i).ship_from_org);
4013                 FND_MSG_PUB.ADD;
4014               END IF;
4015               RAISE Fnd_Api.g_exc_error;
4016             END IF;
4017             CLOSE ahl_ship_from_orgs_csr;
4018           END IF;
4019         ELSE
4020           l_line_rec.ship_from_org_id    := p_line_tbl(i).ship_from_org_id;
4021         END IF;
4022         l_line_val_rec.ship_from_org  := p_line_tbl(i).ship_from_org;
4023 
4024         l_line_rec.subinventory := p_line_tbl(i).subinventory;
4025         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4026           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_line_rec.ship_from_org_id = ' || l_line_rec.ship_from_org_id);
4027           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_line_val_rec.ship_from_org = ' || l_line_val_rec.ship_from_org);
4028           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_line_rec.subinventory = ' || l_line_rec.subinventory);
4029         END IF;
4030 
4031         IF (p_module_type = 'JSP') THEN
4032           -- Set all Ids to G_MISS Values
4033           l_line_rec.fob_point_code   := FND_API.G_MISS_CHAR ;
4034           l_line_rec.freight_carrier_code:= FND_API.G_MISS_CHAR ;
4035           l_line_rec.freight_terms_code  :=  FND_API.G_MISS_CHAR ;
4036           l_line_rec.shipment_priority_code  := FND_API.G_MISS_CHAR;
4037           l_line_rec.shipping_method_code  := FND_API.G_MISS_CHAR ;
4038         ELSE
4039           l_line_rec.fob_point_code   := p_line_tbl(i).fob_point_code;
4040           l_line_rec.freight_carrier_code := p_line_tbl(i).freight_carrier_code;
4041           l_line_rec.freight_terms_code  := p_line_tbl(i).freight_terms_code;
4042           l_line_rec.shipment_priority_code  := p_line_tbl(i).shipment_priority_code;
4043           l_line_rec.shipping_method_code  := p_line_tbl(i).shipping_method_code;
4044         END IF;
4045         l_line_val_rec.fob_point    := p_line_tbl(i).fob_point;
4046         l_line_val_rec.freight_carrier   := p_line_tbl(i).freight_carrier;
4047         l_line_val_rec.freight_terms  := p_line_tbl(i).freight_terms;
4048         l_line_val_rec.shipment_priority  := p_line_tbl(i).shipment_priority;
4049         l_line_val_rec.shipping_method    := p_line_tbl(i).shipping_method;
4050 
4051         --Set values
4052         l_line_rec.item_type_code := 'STANDARD';
4053 
4054         --Fetch AHL_OSP_ORDER source document type id
4055         --AHL Document_type is defined to be 21
4056         l_line_rec.source_document_type_id := 21;
4057 
4058         l_line_rec.unit_selling_price := 0;
4059         l_line_rec.unit_list_price := 0;
4060         l_line_rec.calculate_price_flag := 'N';
4061 
4062         l_line_rec.source_document_id := p_line_tbl(i).osp_order_id;
4063         l_line_rec.source_document_line_id := p_line_tbl(i).osp_line_id;
4064         l_line_rec.orig_sys_document_ref := p_line_tbl(i).osp_order_number;
4065         l_line_rec.orig_sys_line_ref := p_line_tbl(i).osp_line_number;
4066 
4067         IF (p_line_tbl(i).operation = 'C') THEN
4068           OPEN ahl_oe_header_csr(p_line_tbl(i).header_id);
4069           FETCH ahl_oe_header_csr INTO l_line_rec.ship_to_org_id,
4070                                        l_line_rec.sold_to_org_id,
4071                                        l_line_rec.sold_from_org_id,
4072                                        l_line_rec.price_list_id,
4073                                        l_line_rec.payment_term_id;
4074           CLOSE ahl_oe_header_csr;
4075         END IF;
4076 
4077         IF (p_line_tbl(i).operation = 'C') THEN
4078           l_line_rec.operation := OE_GLOBALS.G_OPR_CREATE;
4079         ELSIF (p_line_tbl(i).operation = 'U') THEN
4080           l_line_rec.operation := OE_GLOBALS.G_OPR_UPDATE;
4081         END IF;
4082 
4083         -- NEED TO FIX: 'Return'
4084         IF (p_line_tbl(i).line_type_id = FND_PROFILE.VALUE('AHL_OSP_OE_RETURN_ID') AND
4085             p_line_tbl(i).serial_number IS NOT NULL AND
4086             p_line_tbl(i).serial_number <> FND_API.G_MISS_CHAR) THEN
4087           IF (p_line_tbl(i).operation = 'C') THEN
4088             l_lot_serial_rec := OE_ORDER_PUB.G_MISS_LOT_SERIAL_REC;
4089             l_lot_serial_rec.from_serial_number := p_line_tbl(i).serial_number;
4090             l_lot_serial_rec.lot_number := p_line_tbl(i).lot_number;
4091             l_lot_serial_rec.quantity := p_line_tbl(i).ordered_quantity;
4092             l_lot_serial_rec.line_index := i;
4093             l_lot_serial_rec.operation := OE_GLOBALS.G_OPR_CREATE;
4094             x_lot_serial_tbl(l_count) := l_lot_serial_rec;
4095             l_count := l_count + 1;
4096           ELSIF (p_line_tbl(i).operation = 'U') THEN
4097             OPEN ahl_oe_lot_serial_id (p_line_tbl(i).line_id);
4098             FETCH ahl_oe_lot_serial_id INTO l_lot_serial_id;
4099             IF (ahl_oe_lot_serial_id%FOUND) THEN
4100               OE_LOT_SERIAL_UTIL.Query_Row(p_lot_serial_id => l_lot_serial_id,
4101                                            x_lot_serial_rec =>l_lot_serial_rec);
4102               l_lot_serial_rec.from_serial_number := p_line_tbl(i).serial_number;
4103               l_lot_serial_rec.lot_number := p_line_tbl(i).lot_number;
4104               l_lot_serial_rec.quantity := p_line_tbl(i).ordered_quantity;
4105               l_lot_serial_rec.operation := OE_GLOBALS.G_OPR_UPDATE;
4106               x_lot_serial_tbl(l_count) := l_lot_serial_rec;
4107               l_count := l_count + 1;
4108             END IF;
4109             CLOSE ahl_oe_lot_serial_id;
4110           END IF;
4111         END IF;  --return line type
4112         l_line_table_index := l_line_table_index + 1;
4113         x_line_tbl(l_line_table_index) := l_line_rec;
4114         x_line_val_tbl(l_line_table_index) := l_line_val_rec;
4115         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4116           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_line_rec.ship_from_org_id = ' || l_line_rec.ship_from_org_id);
4117         END IF;
4118       END IF;  -- Operation is not 'D'
4119     END LOOP;  -- For all lines in Line Table
4120   END IF;  -- Line Table Count > 0
4121   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4122     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'Exiting Procedure');
4123   END IF;
4124 
4125 END Convert_Line_Tbl;
4126 
4127 ---
4128 PROCEDURE Delete_OSP_Order(
4129         p_oe_header_id   IN NUMBER
4130        ) IS
4131 --
4132   CURSOR ahl_osp_order_id_csr(p_header_id IN NUMBER) IS
4133     SELECT osp_order_id
4134     FROM ahl_osp_orders_b
4135     WHERE oe_header_id = p_header_id;
4136 
4137 --
4138   CURSOR ahl_osp_order_line_csr(p_order_id IN NUMBER) IS
4139     SELECT *
4140     FROM AHL_OSP_ORDER_LINES
4141     WHERE osp_order_id = p_order_id;
4142 --
4143   l_osp_order_id NUMBER;
4144   l_osp_line_rec ahl_osp_order_line_csr%ROWTYPE;
4145 --
4146 BEGIN
4147     --Update AHL_OSP_ORDERS/OSP_ORDER_LINES tables with ids/new id
4148     OPEN ahl_osp_order_id_csr(p_oe_header_id);
4149     FETCH ahl_osp_order_id_csr INTO l_osp_order_id;
4150     IF (ahl_osp_order_id_csr%FOUND) THEN
4151       Update_OSP_Order(p_osp_order_id => l_osp_order_id,
4152                        p_oe_header_id => NULL );
4153 
4154     --Update the lines
4155     FOR l_osp_line_rec IN ahl_osp_order_line_csr(l_osp_order_id) LOOP
4156      AHL_OSP_ORDER_LINES_PKG.UPDATE_ROW (
4157             P_OSP_ORDER_LINE_ID        => l_osp_line_rec.OSP_ORDER_LINE_ID,
4158             P_OBJECT_VERSION_NUMBER    => l_osp_line_rec.OBJECT_VERSION_NUMBER+1,
4159             P_LAST_UPDATE_DATE         => l_osp_line_rec.LAST_UPDATE_DATE,
4160             P_LAST_UPDATED_BY          => l_osp_line_rec.LAST_UPDATED_BY,
4161             P_LAST_UPDATE_LOGIN        => l_osp_line_rec.LAST_UPDATE_LOGIN,
4162             P_OSP_ORDER_ID             => l_osp_line_rec.OSP_ORDER_ID,
4163             P_OSP_LINE_NUMBER          => l_osp_line_rec.OSP_LINE_NUMBER,
4164             P_STATUS_CODE              => l_osp_line_rec.STATUS_CODE,
4165             P_PO_LINE_TYPE_ID          => l_osp_line_rec.PO_LINE_TYPE_ID,
4166             P_SERVICE_ITEM_ID          => l_osp_line_rec.SERVICE_ITEM_ID,
4167             P_SERVICE_ITEM_DESCRIPTION => l_osp_line_rec.SERVICE_ITEM_DESCRIPTION,
4168             P_SERVICE_ITEM_UOM_CODE    => l_osp_line_rec.SERVICE_ITEM_UOM_CODE,
4169             P_NEED_BY_DATE             => l_osp_line_rec.NEED_BY_DATE,
4170             P_SHIP_BY_DATE             => l_osp_line_rec.SHIP_BY_DATE,
4171             P_PO_LINE_ID               => l_osp_line_rec.PO_LINE_ID,
4172             P_OE_SHIP_LINE_ID          => NULL,
4173             P_OE_RETURN_LINE_ID        => NULL,
4174             P_WORKORDER_ID             => l_osp_line_rec.WORKORDER_ID,
4175             P_OPERATION_ID             => l_osp_line_rec.OPERATION_ID,
4176             P_QUANTITY                 => l_osp_line_rec.QUANTITY,
4177 	P_INVENTORY_ITEM_ID            	       => l_osp_line_rec.INVENTORY_ITEM_ID,
4178 	P_INVENTORY_ORG_ID             	       => l_osp_line_rec.INVENTORY_ORG_ID,
4179 	P_INVENTORY_ITEM_UOM           	       => l_osp_line_rec.INVENTORY_ITEM_UOM,
4180 	P_INVENTORY_ITEM_QUANTITY      	       => l_osp_line_rec.INVENTORY_ITEM_QUANTITY,
4181 	P_SUB_INVENTORY                	       => l_osp_line_rec.SUB_INVENTORY,
4182 	P_LOT_NUMBER                   	       => l_osp_line_rec.LOT_NUMBER,
4183 	P_SERIAL_NUMBER          	       => l_osp_line_rec.SERIAL_NUMBER,
4184 -- Begin Changes by jaramana on January 11, 2008 for the Requisition ER 6034236
4185             P_PO_REQ_LINE_ID           => l_osp_line_rec.PO_REQ_LINE_ID,
4186 -- End Changes by jaramana on January 11, 2008 for the Requisition ER 6034236
4187             P_EXCHANGE_INSTANCE_ID     => l_osp_line_rec.EXCHANGE_INSTANCE_ID,
4188             P_ATTRIBUTE_CATEGORY       => l_osp_line_rec.ATTRIBUTE_CATEGORY,
4189             P_ATTRIBUTE1               => l_osp_line_rec.ATTRIBUTE1,
4190             P_ATTRIBUTE2               => l_osp_line_rec.ATTRIBUTE2,
4191             P_ATTRIBUTE3               => l_osp_line_rec.ATTRIBUTE3,
4192             P_ATTRIBUTE4               => l_osp_line_rec.ATTRIBUTE4,
4193             P_ATTRIBUTE5               => l_osp_line_rec.ATTRIBUTE5,
4194             P_ATTRIBUTE6               => l_osp_line_rec.ATTRIBUTE6,
4195             P_ATTRIBUTE7               => l_osp_line_rec.ATTRIBUTE7,
4196             P_ATTRIBUTE8               => l_osp_line_rec.ATTRIBUTE8,
4197             P_ATTRIBUTE9               => l_osp_line_rec.ATTRIBUTE9,
4198             P_ATTRIBUTE10              => l_osp_line_rec.ATTRIBUTE10,
4199             P_ATTRIBUTE11              => l_osp_line_rec.ATTRIBUTE11,
4200             P_ATTRIBUTE12              => l_osp_line_rec.ATTRIBUTE12,
4201             P_ATTRIBUTE13              => l_osp_line_rec.ATTRIBUTE13,
4202             P_ATTRIBUTE14              => l_osp_line_rec.ATTRIBUTE14,
4203             P_ATTRIBUTE15              => l_osp_line_rec.ATTRIBUTE15 );
4204      END LOOP;
4205 
4206     END IF;
4207     CLOSE ahl_osp_order_id_csr;
4208 
4209 END Delete_OSP_Order;
4210 
4211 --
4212 
4213 PROCEDURE Update_OSP_Order(
4214 	p_osp_order_id   IN NUMBER,
4215         p_oe_header_id   IN NUMBER
4216        ) IS
4217 --
4218   CURSOR ahl_osp_order_csr(p_osp_id IN NUMBER) IS
4219     SELECT *
4220     FROM AHL_OSP_ORDERS_VL
4221     WHERE osp_order_id = p_osp_id;
4222 
4223 --
4224   l_osp_order ahl_osp_order_csr%ROWTYPE;
4225 --
4226 BEGIN
4227 
4228   --Now fetch information corresponding to osp_order_id
4229    OPEN ahl_osp_order_csr(p_osp_order_id);
4230    FETCH ahl_osp_order_csr INTO l_osp_order;
4231    IF (ahl_osp_order_csr%NOTFOUND) THEN
4232       CLOSE ahl_osp_order_csr;
4233       IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
4234          Fnd_Message.set_name('AHL', 'AHL_OSP_OSP_ID_NULL');
4235          Fnd_Msg_Pub.ADD;
4236       END IF;
4237       RAISE Fnd_Api.g_exc_error;
4238    END IF;
4239    CLOSE ahl_osp_order_csr;
4240 
4241   AHL_OSP_ORDERS_PKG.UPDATE_ROW(
4242    X_OSP_ORDER_ID   =>    l_osp_order.OSP_ORDER_ID,
4243    X_OBJECT_VERSION_NUMBER   =>    l_osp_order.OBJECT_VERSION_NUMBER+1,
4244    X_OSP_ORDER_NUMBER   =>    l_osp_order.OSP_ORDER_NUMBER,
4245    X_ORDER_TYPE_CODE   =>     l_osp_order.ORDER_TYPE_CODE,
4246    X_SINGLE_INSTANCE_FLAG   =>     l_osp_order.SINGLE_INSTANCE_FLAG,
4247    X_PO_HEADER_ID   =>         l_osp_order.PO_HEADER_ID,
4248    X_OE_HEADER_ID   =>         p_oe_header_id,
4249    X_VENDOR_ID   =>    l_osp_order.VENDOR_ID,
4250    X_VENDOR_SITE_ID   =>    l_osp_order.VENDOR_SITE_ID,
4251    X_VENDOR_CONTACT_ID  => l_osp_order.VENDOR_CONTACT_ID,
4252    X_CUSTOMER_ID   =>    l_osp_order.CUSTOMER_ID,
4253    X_ORDER_DATE   =>     l_osp_order.ORDER_DATE,
4254    X_CONTRACT_ID   =>    l_osp_order.CONTRACT_ID,
4255    X_CONTRACT_TERMS   =>     l_osp_order.CONTRACT_TERMS,
4256    X_OPERATING_UNIT_ID   =>    l_osp_order.OPERATING_UNIT_ID,
4257    X_PO_SYNCH_FLAG   =>     l_osp_order.PO_SYNCH_FLAG,
4258    X_STATUS_CODE   =>     l_osp_order.STATUS_CODE,
4259    X_PO_BATCH_ID   =>    l_osp_order.PO_BATCH_ID,
4260    X_PO_REQUEST_ID   =>    l_osp_order.PO_REQUEST_ID,
4261    X_PO_AGENT_ID   =>    l_osp_order.PO_AGENT_ID,
4262    X_PO_INTERFACE_HEADER_ID   =>    l_osp_order.PO_INTERFACE_HEADER_ID,
4263    X_ATTRIBUTE_CATEGORY   =>     l_osp_order.ATTRIBUTE_CATEGORY,
4264    X_ATTRIBUTE1   =>     l_osp_order.ATTRIBUTE1,
4265    X_ATTRIBUTE2   =>     l_osp_order.ATTRIBUTE2,
4266    X_ATTRIBUTE3   =>     l_osp_order.ATTRIBUTE3,
4267    X_ATTRIBUTE4   =>     l_osp_order.ATTRIBUTE4,
4268    X_ATTRIBUTE5   =>     l_osp_order.ATTRIBUTE5,
4269    X_ATTRIBUTE6   =>     l_osp_order.ATTRIBUTE6,
4270    X_ATTRIBUTE7   =>     l_osp_order.ATTRIBUTE7,
4271    X_ATTRIBUTE8   =>     l_osp_order.ATTRIBUTE8,
4272    X_ATTRIBUTE9   =>     l_osp_order.ATTRIBUTE9,
4273    X_ATTRIBUTE10   =>     l_osp_order.ATTRIBUTE10,
4274    X_ATTRIBUTE11   =>     l_osp_order.ATTRIBUTE11,
4275    X_ATTRIBUTE12   =>     l_osp_order.ATTRIBUTE12,
4276    X_ATTRIBUTE13   =>     l_osp_order.ATTRIBUTE13,
4277    X_ATTRIBUTE14   =>     l_osp_order.ATTRIBUTE14,
4278    X_ATTRIBUTE15   =>     l_osp_order.ATTRIBUTE15,
4279    X_DESCRIPTION   =>     l_osp_order.DESCRIPTION,
4280    X_PO_REQ_HEADER_ID  => l_osp_order.PO_REQ_HEADER_ID, -- Added by jaramana on January 11, 2008 for the Requisition ER 6034236
4281    X_LAST_UPDATE_DATE   =>     l_osp_order.LAST_UPDATE_DATE,
4282    X_LAST_UPDATED_BY   =>    l_osp_order.LAST_UPDATED_BY,
4283    X_LAST_UPDATE_LOGIN   =>    l_osp_order.LAST_UPDATE_LOGIN
4284    );
4285 
4286 END Update_OSP_Order;
4287 ---
4288 
4289 
4290 PROCEDURE Update_OSP_Order_Lines(
4291 	p_osp_order_id  IN NUMBER,
4292 	p_osp_line_id   IN NUMBER,
4293         p_oe_ship_line_id       IN NUMBER,
4294         p_oe_return_line_id     IN NUMBER
4295        ) IS
4296 --
4297 --Since one item instance can not be in multiple ship lines for given
4298 --osp order, fetch all the ship/return lines for item instance
4299 --
4300  CURSOR ahl_osp_lines_csr(p_osp_id IN NUMBER, p_osp_line_id IN NUMBER) IS
4301  --mpothuku removed the usage of AHL_OSP_ORDER_LINES_V usage for fixing the perf Bug# 4919255 on 21-Feb-06
4302     SELECT  a.OSP_ORDER_LINE_ID,
4303             a.OBJECT_VERSION_NUMBER,
4304             a.LAST_UPDATE_DATE,
4305             a.LAST_UPDATED_BY,
4306             a.LAST_UPDATE_LOGIN,
4307             a.OSP_ORDER_ID,
4308             a.OSP_LINE_NUMBER,
4309             a.STATUS_CODE,
4310             a.PO_LINE_TYPE_ID,
4311             a.SERVICE_ITEM_ID,
4312             a.SERVICE_ITEM_DESCRIPTION,
4313             a.SERVICE_ITEM_UOM_CODE,
4314             a.NEED_BY_DATE,
4315             a.SHIP_BY_DATE,
4316             a.PO_LINE_ID,
4317             a.OE_SHIP_LINE_ID,
4318             a.OE_RETURN_LINE_ID,
4319             a.WORKORDER_ID,
4320             a.OPERATION_ID,
4321             a.EXCHANGE_INSTANCE_ID,
4322             a.INVENTORY_ITEM_ID,
4323             a.INVENTORY_ORG_ID,
4324             --a.ITEM_NUMBER,
4325             --a.ITEM_DESCRIPTION,
4326             a.SERIAL_NUMBER,
4327             a.LOT_NUMBER,
4328             a.INVENTORY_ITEM_UOM,
4329             a.INVENTORY_ITEM_QUANTITY,
4330             a.SUB_INVENTORY,
4331             a.QUANTITY,
4332             a.ATTRIBUTE_CATEGORY,
4333             a.ATTRIBUTE1,
4334             a.ATTRIBUTE2,
4335             a.ATTRIBUTE3,
4336             a.ATTRIBUTE4,
4337             a.ATTRIBUTE5,
4338             a.ATTRIBUTE6,
4339             a.ATTRIBUTE7,
4340             a.ATTRIBUTE8,
4341             a.ATTRIBUTE9,
4342             a.ATTRIBUTE10,
4343             a.ATTRIBUTE11,
4344             a.ATTRIBUTE12,
4345             a.ATTRIBUTE13,
4346             a.ATTRIBUTE14,
4347             a.ATTRIBUTE15,
4348 -- Begin Changes by jaramana on January 14, 2008 for the Requisition ER 6034236
4349            a.PO_REQ_LINE_ID
4350 -- End Changes by jaramana on January 14, 2008 for the Requisition ER 6034236
4351     FROM AHL_OSP_ORDER_LINES a
4352     WHERE a.osp_order_id = p_osp_id
4353       AND a.osp_order_line_id = p_osp_line_id;
4354 --
4355   l_oe_ship_line_id    	 NUMBER;
4356   l_oe_return_line_id 	 NUMBER;
4357   l_row_check      VARCHAR2(1):='N';
4358 --
4359 BEGIN
4360 
4361    FOR l_osp_line_rec IN ahl_osp_lines_csr(p_osp_order_id, p_osp_line_id)
4362   LOOP
4363      l_row_check := 'Y';
4364      IF ( p_oe_ship_line_id IS NOT NULL
4365  	AND p_oe_ship_line_id <> FND_API.G_MISS_NUM) THEN
4366           l_oe_ship_line_id := p_oe_ship_line_id;
4367      ELSE
4368           l_oe_ship_line_id := l_osp_line_rec.oe_ship_line_id;
4369      END IF;
4370 
4371      IF (p_oe_return_line_id IS NOT NULL
4372  	AND p_oe_return_line_id <> FND_API.G_MISS_NUM) THEN
4373           l_oe_return_line_id := p_oe_return_line_id;
4374      ELSE
4375           l_oe_return_line_id := l_osp_line_rec.oe_return_line_id;
4376      END IF;
4377 
4378      AHL_OSP_ORDER_LINES_PKG.UPDATE_ROW (
4379             P_OSP_ORDER_LINE_ID        => l_osp_line_rec.OSP_ORDER_LINE_ID,
4380             P_OBJECT_VERSION_NUMBER    => l_osp_line_rec.OBJECT_VERSION_NUMBER+1,
4381             P_LAST_UPDATE_DATE         => l_osp_line_rec.LAST_UPDATE_DATE,
4382             P_LAST_UPDATED_BY          => l_osp_line_rec.LAST_UPDATED_BY,
4383             P_LAST_UPDATE_LOGIN        => l_osp_line_rec.LAST_UPDATE_LOGIN,
4384             P_OSP_ORDER_ID             => l_osp_line_rec.OSP_ORDER_ID,
4385             P_OSP_LINE_NUMBER          => l_osp_line_rec.OSP_LINE_NUMBER,
4386             P_STATUS_CODE              => l_osp_line_rec.STATUS_CODE,
4387             P_PO_LINE_TYPE_ID          => l_osp_line_rec.PO_LINE_TYPE_ID,
4388             P_SERVICE_ITEM_ID          => l_osp_line_rec.SERVICE_ITEM_ID,
4389             P_SERVICE_ITEM_DESCRIPTION => l_osp_line_rec.SERVICE_ITEM_DESCRIPTION,
4390             P_SERVICE_ITEM_UOM_CODE    => l_osp_line_rec.SERVICE_ITEM_UOM_CODE,
4391             P_NEED_BY_DATE             => l_osp_line_rec.NEED_BY_DATE,
4392             P_SHIP_BY_DATE             => l_osp_line_rec.SHIP_BY_DATE,
4393             P_PO_LINE_ID               => l_osp_line_rec.PO_LINE_ID,
4394             P_OE_SHIP_LINE_ID          => l_oe_ship_line_id,
4395             P_OE_RETURN_LINE_ID        => l_oe_return_line_id,
4396             P_WORKORDER_ID             => l_osp_line_rec.WORKORDER_ID,
4397             P_OPERATION_ID             => l_osp_line_rec.OPERATION_ID,
4398             P_QUANTITY                 => l_osp_line_rec.QUANTITY,
4399             P_EXCHANGE_INSTANCE_ID     => l_osp_line_rec.EXCHANGE_INSTANCE_ID,
4400             P_INVENTORY_ITEM_ID        => l_osp_line_rec.INVENTORY_ITEM_ID,
4401             P_INVENTORY_ORG_ID         => l_osp_line_rec.INVENTORY_ORG_ID,
4402             P_INVENTORY_ITEM_UOM       => l_osp_line_rec.INVENTORY_ITEM_UOM,
4403             P_INVENTORY_ITEM_QUANTITY  => l_osp_line_rec.INVENTORY_ITEM_QUANTITY,
4404             P_SUB_INVENTORY            => l_osp_line_rec.SUB_INVENTORY,
4405             P_LOT_NUMBER               => l_osp_line_rec.LOT_NUMBER,
4406             P_SERIAL_NUMBER            => l_osp_line_rec.SERIAL_NUMBER,
4407 -- Begin Changes by jaramana on January 14, 2008 for the Requisition ER 6034236
4408             P_PO_REQ_LINE_ID           => l_osp_line_rec.PO_REQ_LINE_ID,
4409 -- End Changes by jaramana on January 14, 2008 for the Requisition ER 6034236
4410             P_ATTRIBUTE_CATEGORY       => l_osp_line_rec.ATTRIBUTE_CATEGORY,
4411             P_ATTRIBUTE1               => l_osp_line_rec.ATTRIBUTE1,
4412             P_ATTRIBUTE2               => l_osp_line_rec.ATTRIBUTE2,
4413             P_ATTRIBUTE3               => l_osp_line_rec.ATTRIBUTE3,
4414             P_ATTRIBUTE4               => l_osp_line_rec.ATTRIBUTE4,
4415             P_ATTRIBUTE5               => l_osp_line_rec.ATTRIBUTE5,
4416             P_ATTRIBUTE6               => l_osp_line_rec.ATTRIBUTE6,
4417             P_ATTRIBUTE7               => l_osp_line_rec.ATTRIBUTE7,
4418             P_ATTRIBUTE8               => l_osp_line_rec.ATTRIBUTE8,
4419             P_ATTRIBUTE9               => l_osp_line_rec.ATTRIBUTE9,
4420             P_ATTRIBUTE10              => l_osp_line_rec.ATTRIBUTE10,
4421             P_ATTRIBUTE11              => l_osp_line_rec.ATTRIBUTE11,
4422             P_ATTRIBUTE12              => l_osp_line_rec.ATTRIBUTE12,
4423             P_ATTRIBUTE13              => l_osp_line_rec.ATTRIBUTE13,
4424             P_ATTRIBUTE14              => l_osp_line_rec.ATTRIBUTE14,
4425             P_ATTRIBUTE15              => l_osp_line_rec.ATTRIBUTE15 );
4426     END LOOP;
4427 
4428     IF l_row_check = 'N' THEN
4429             Fnd_Message.set_name('AHL', 'AHL_OSP_INVALID_LINE_ITEM');
4430             Fnd_Msg_Pub.ADD;
4431             RAISE Fnd_Api.g_exc_error;
4432     END IF;
4433 
4434 END Update_OSP_Order_Lines;
4435 
4436 
4437 PROCEDURE Update_OSP_Order_Lines(
4438 	p_osp_order_id  IN NUMBER,
4439 	p_item_instance_id   IN NUMBER,
4440         p_oe_ship_line_id       IN NUMBER,
4441         p_oe_return_line_id     IN NUMBER
4442        ) IS
4443 --
4444 --Since one item instance can not be in multiple ship lines for given
4445 --osp order, fetch all the ship/return lines for item instance
4446 --
4447  CURSOR ahl_osp_lines_csr(p_osp_id IN NUMBER, p_csi_ii_id IN NUMBER) IS
4448  --mpothuku removed the usage of AHL_OSP_ORDER_LINES_V usage for fixing the perf Bug# 4919255 on 21-Feb-06
4449  /*
4450     SELECT  a.OSP_ORDER_LINE_ID,
4451             a.OBJECT_VERSION_NUMBER,
4452             a.LAST_UPDATE_DATE,
4453             a.LAST_UPDATED_BY,
4454             a.LAST_UPDATE_LOGIN,
4455             a.OSP_ORDER_ID,
4456             a.OSP_LINE_NUMBER,
4457             a.STATUS_CODE,
4458             a.PO_LINE_TYPE_ID,
4459             a.SERVICE_ITEM_ID,
4460             a.SERVICE_ITEM_DESCRIPTION,
4461             a.SERVICE_ITEM_UOM_CODE,
4462             a.NEED_BY_DATE,
4463             a.SHIP_BY_DATE,
4464             a.PO_LINE_ID,
4465             a.OE_SHIP_LINE_ID,
4466             a.OE_RETURN_LINE_ID,
4467             a.WORKORDER_ID,
4468             a.OPERATION_ID,
4469             a.EXCHANGE_INSTANCE_ID,
4470             a.INVENTORY_ITEM_ID,
4471             a.INVENTORY_ORG_ID,
4472             a.ITEM_NUMBER,
4473             a.ITEM_DESCRIPTION,
4474             a.SERIAL_NUMBER,
4475             a.LOT_NUMBER,
4476             a.INVENTORY_ITEM_UOM,
4477             a.INVENTORY_ITEM_QUANTITY,
4478             a.SUB_INVENTORY,
4479             a.QUANTITY,
4480             a.ATTRIBUTE_CATEGORY,
4481             a.ATTRIBUTE1,
4482             a.ATTRIBUTE2,
4483             a.ATTRIBUTE3,
4484             a.ATTRIBUTE4,
4485             a.ATTRIBUTE5,
4486             a.ATTRIBUTE6,
4487             a.ATTRIBUTE7,
4488             a.ATTRIBUTE8,
4489             a.ATTRIBUTE9,
4490             a.ATTRIBUTE10,
4491             a.ATTRIBUTE11,
4492             a.ATTRIBUTE12,
4493             a.ATTRIBUTE13,
4494             a.ATTRIBUTE14,
4495             a.ATTRIBUTE15
4496 
4497 --    FROM AHL_OSP_ORDER_LINES a, AHL_WORKORDERS_V b
4498 --    WHERE a.workorder_id = b.workorder_id
4499 --      AND a.osp_order_id = p_osp_id
4500 --     AND b.item_instance_id = p_csi_ii_id;
4501 
4502     FROM AHL_OSP_ORDER_LINES_V a
4503     WHERE a.osp_order_id = p_osp_id
4504       AND (a.item_instance_id = p_csi_ii_id OR a.exchange_instance_id = p_csi_ii_id);
4505 */
4506 
4507     SELECT  a.OSP_ORDER_LINE_ID,
4508             a.OBJECT_VERSION_NUMBER,
4509             a.LAST_UPDATE_DATE,
4510             a.LAST_UPDATED_BY,
4511             a.LAST_UPDATE_LOGIN,
4512             a.OSP_ORDER_ID,
4513             a.OSP_LINE_NUMBER,
4514             a.STATUS_CODE,
4515             a.PO_LINE_TYPE_ID,
4516             a.SERVICE_ITEM_ID,
4517             a.SERVICE_ITEM_DESCRIPTION,
4518             a.SERVICE_ITEM_UOM_CODE,
4519             a.NEED_BY_DATE,
4520             a.SHIP_BY_DATE,
4521             a.PO_LINE_ID,
4522             a.OE_SHIP_LINE_ID,
4523             a.OE_RETURN_LINE_ID,
4524             a.WORKORDER_ID,
4525             a.OPERATION_ID,
4526             a.EXCHANGE_INSTANCE_ID,
4527             a.INVENTORY_ITEM_ID,
4528             a.INVENTORY_ORG_ID,
4529             a.SERIAL_NUMBER,
4530             a.LOT_NUMBER,
4531             a.INVENTORY_ITEM_UOM,
4532             a.INVENTORY_ITEM_QUANTITY,
4533             a.SUB_INVENTORY,
4534             a.QUANTITY,
4535             a.ATTRIBUTE_CATEGORY,
4536             a.ATTRIBUTE1,
4537             a.ATTRIBUTE2,
4538             a.ATTRIBUTE3,
4539             a.ATTRIBUTE4,
4540             a.ATTRIBUTE5,
4541             a.ATTRIBUTE6,
4542             a.ATTRIBUTE7,
4543             a.ATTRIBUTE8,
4544             a.ATTRIBUTE9,
4545             a.ATTRIBUTE10,
4546             a.ATTRIBUTE11,
4547             a.ATTRIBUTE12,
4548             a.ATTRIBUTE13,
4549             a.ATTRIBUTE14,
4550             a.ATTRIBUTE15,
4551 -- Begin Changes by jaramana on January 14, 2008 for the Requisition ER 6034236
4552             a.PO_REQ_LINE_ID
4553 -- End Changes by jaramana on January 14, 2008 for the Requisition ER 6034236
4554     FROM AHL_OSP_ORDER_LINES a,
4555          ahl_workorders wo,
4556          csi_item_instances csii,
4557          ahl_visit_tasks_b vts
4558     WHERE a.osp_order_id = p_osp_id
4559       AND wo.workorder_id(+) = a.workorder_id
4560       AND wo.visit_task_id = vts.visit_task_id(+)
4561       AND csii.last_vld_organization_id(+) = a.inventory_org_id
4562       AND csii.inventory_item_id(+) = a.inventory_item_id
4563       AND csii.serial_number(+) = a.serial_number
4564       AND (decode(a.workorder_id, NULL, csii.instance_id, vts.instance_id) = p_csi_ii_id OR a.exchange_instance_id = p_csi_ii_id)
4565 --BEGIN changes by jrotich for bug # 14223880
4566   UNION
4567     SELECT  a.OSP_ORDER_LINE_ID,
4568             a.OBJECT_VERSION_NUMBER,
4569             a.LAST_UPDATE_DATE,
4570             a.LAST_UPDATED_BY,
4571             a.LAST_UPDATE_LOGIN,
4572             a.OSP_ORDER_ID,
4573             a.OSP_LINE_NUMBER,
4574             a.STATUS_CODE,
4575             a.PO_LINE_TYPE_ID,
4576             a.SERVICE_ITEM_ID,
4577             a.SERVICE_ITEM_DESCRIPTION,
4578             a.SERVICE_ITEM_UOM_CODE,
4579             a.NEED_BY_DATE,
4580             a.SHIP_BY_DATE,
4581             a.PO_LINE_ID,
4582             a.OE_SHIP_LINE_ID,
4583             a.OE_RETURN_LINE_ID,
4584             a.WORKORDER_ID,
4585             a.OPERATION_ID,
4586             a.EXCHANGE_INSTANCE_ID,
4587             a.INVENTORY_ITEM_ID,
4588             a.INVENTORY_ORG_ID,
4589             a.SERIAL_NUMBER,
4590             a.LOT_NUMBER,
4591             a.INVENTORY_ITEM_UOM,
4592             a.INVENTORY_ITEM_QUANTITY,
4593             a.SUB_INVENTORY,
4594             a.QUANTITY,
4595             a.ATTRIBUTE_CATEGORY,
4596             a.ATTRIBUTE1,
4597             a.ATTRIBUTE2,
4598             a.ATTRIBUTE3,
4599             a.ATTRIBUTE4,
4600             a.ATTRIBUTE5,
4601             a.ATTRIBUTE6,
4602             a.ATTRIBUTE7,
4603             a.ATTRIBUTE8,
4604             a.ATTRIBUTE9,
4605             a.ATTRIBUTE10,
4606             a.ATTRIBUTE11,
4607             a.ATTRIBUTE12,
4608             a.ATTRIBUTE13,
4609             a.ATTRIBUTE14,
4610             a.ATTRIBUTE15,
4611 -- Begin Changes by jaramana on January 14, 2008 for the Requisition ER 6034236
4612             a.PO_REQ_LINE_ID
4613 -- End Changes by jaramana on January 14, 2008 for the Requisition ER 6034236
4614     FROM AHL_OSP_ORDER_LINES a,
4615          ahl_workorders wo,
4616          csi_item_instances csii,
4617          ahl_visit_tasks_b vts
4618     WHERE a.osp_order_id = p_osp_id
4619       AND wo.workorder_id(+) = a.workorder_id
4620       AND wo.visit_task_id = vts.visit_task_id(+)
4621       AND csii.last_vld_organization_id(+) = a.inventory_org_id
4622       AND csii.inventory_item_id(+) = a.inventory_item_id
4623       AND csii.lot_number(+) = a.lot_number
4624       AND (decode(a.workorder_id, NULL, csii.instance_id, vts.instance_id) = p_csi_ii_id OR a.exchange_instance_id = p_csi_ii_id);
4625 --END Changes by jrotich for bug# 14223880
4626 
4627 --
4628   l_oe_ship_line_id    	 NUMBER;
4629   l_oe_return_line_id 	 NUMBER;
4630   l_row_check      VARCHAR2(1):='N';
4631 --
4632 BEGIN
4633 
4634    FOR l_osp_line_rec IN ahl_osp_lines_csr(p_osp_order_id, p_item_instance_id)
4635   LOOP
4636      l_row_check := 'Y';
4637      IF ( p_oe_ship_line_id IS NOT NULL
4638  	AND p_oe_ship_line_id <> FND_API.G_MISS_NUM) THEN
4639           l_oe_ship_line_id := p_oe_ship_line_id;
4640      ELSE
4641           l_oe_ship_line_id := l_osp_line_rec.oe_ship_line_id;
4642      END IF;
4643 
4644      IF (p_oe_return_line_id IS NOT NULL
4645  	AND p_oe_return_line_id <> FND_API.G_MISS_NUM) THEN
4646           l_oe_return_line_id := p_oe_return_line_id;
4647      ELSE
4648           l_oe_return_line_id := l_osp_line_rec.oe_return_line_id;
4649      END IF;
4650 
4651      AHL_OSP_ORDER_LINES_PKG.UPDATE_ROW (
4652             P_OSP_ORDER_LINE_ID        => l_osp_line_rec.OSP_ORDER_LINE_ID,
4653             P_OBJECT_VERSION_NUMBER    => l_osp_line_rec.OBJECT_VERSION_NUMBER+1,
4654             P_LAST_UPDATE_DATE         => l_osp_line_rec.LAST_UPDATE_DATE,
4655             P_LAST_UPDATED_BY          => l_osp_line_rec.LAST_UPDATED_BY,
4656             P_LAST_UPDATE_LOGIN        => l_osp_line_rec.LAST_UPDATE_LOGIN,
4657             P_OSP_ORDER_ID             => l_osp_line_rec.OSP_ORDER_ID,
4658             P_OSP_LINE_NUMBER          => l_osp_line_rec.OSP_LINE_NUMBER,
4659             P_STATUS_CODE              => l_osp_line_rec.STATUS_CODE,
4660             P_PO_LINE_TYPE_ID          => l_osp_line_rec.PO_LINE_TYPE_ID,
4661             P_SERVICE_ITEM_ID          => l_osp_line_rec.SERVICE_ITEM_ID,
4662             P_SERVICE_ITEM_DESCRIPTION => l_osp_line_rec.SERVICE_ITEM_DESCRIPTION,
4663             P_SERVICE_ITEM_UOM_CODE    => l_osp_line_rec.SERVICE_ITEM_UOM_CODE,
4664             P_NEED_BY_DATE             => l_osp_line_rec.NEED_BY_DATE,
4665             P_SHIP_BY_DATE             => l_osp_line_rec.SHIP_BY_DATE,
4666             P_PO_LINE_ID               => l_osp_line_rec.PO_LINE_ID,
4667             P_OE_SHIP_LINE_ID          => l_oe_ship_line_id,
4668             P_OE_RETURN_LINE_ID        => l_oe_return_line_id,
4669             P_WORKORDER_ID             => l_osp_line_rec.WORKORDER_ID,
4670             P_OPERATION_ID             => l_osp_line_rec.OPERATION_ID,
4671             P_QUANTITY                 => l_osp_line_rec.QUANTITY,
4672             P_EXCHANGE_INSTANCE_ID     => l_osp_line_rec.EXCHANGE_INSTANCE_ID,
4673             P_INVENTORY_ITEM_ID        => l_osp_line_rec.INVENTORY_ITEM_ID,
4674             P_INVENTORY_ORG_ID         => l_osp_line_rec.INVENTORY_ORG_ID,
4675             P_INVENTORY_ITEM_UOM       => l_osp_line_rec.INVENTORY_ITEM_UOM,
4676             P_INVENTORY_ITEM_QUANTITY  => l_osp_line_rec.INVENTORY_ITEM_QUANTITY,
4677             P_SUB_INVENTORY            => l_osp_line_rec.SUB_INVENTORY,
4678             P_LOT_NUMBER               => l_osp_line_rec.LOT_NUMBER,
4679             P_SERIAL_NUMBER            => l_osp_line_rec.SERIAL_NUMBER,
4680 -- Begin Changes by jaramana on January 14, 2008 for the Requisition ER 6034236
4681             P_PO_REQ_LINE_ID           => l_osp_line_rec.PO_REQ_LINE_ID,
4682 -- End Changes by jaramana on January 14, 2008 for the Requisition ER 6034236
4683             P_ATTRIBUTE_CATEGORY       => l_osp_line_rec.ATTRIBUTE_CATEGORY,
4684             P_ATTRIBUTE1               => l_osp_line_rec.ATTRIBUTE1,
4685             P_ATTRIBUTE2               => l_osp_line_rec.ATTRIBUTE2,
4686             P_ATTRIBUTE3               => l_osp_line_rec.ATTRIBUTE3,
4687             P_ATTRIBUTE4               => l_osp_line_rec.ATTRIBUTE4,
4688             P_ATTRIBUTE5               => l_osp_line_rec.ATTRIBUTE5,
4689             P_ATTRIBUTE6               => l_osp_line_rec.ATTRIBUTE6,
4690             P_ATTRIBUTE7               => l_osp_line_rec.ATTRIBUTE7,
4691             P_ATTRIBUTE8               => l_osp_line_rec.ATTRIBUTE8,
4692             P_ATTRIBUTE9               => l_osp_line_rec.ATTRIBUTE9,
4693             P_ATTRIBUTE10              => l_osp_line_rec.ATTRIBUTE10,
4694             P_ATTRIBUTE11              => l_osp_line_rec.ATTRIBUTE11,
4695             P_ATTRIBUTE12              => l_osp_line_rec.ATTRIBUTE12,
4696             P_ATTRIBUTE13              => l_osp_line_rec.ATTRIBUTE13,
4697             P_ATTRIBUTE14              => l_osp_line_rec.ATTRIBUTE14,
4698             P_ATTRIBUTE15              => l_osp_line_rec.ATTRIBUTE15 );
4699     END LOOP;
4700 
4701     IF l_row_check = 'N' THEN
4702             Fnd_Message.set_name('AHL', 'AHL_OSP_INVALID_LINE_ITEM');
4703             Fnd_Msg_Pub.ADD;
4704             RAISE Fnd_Api.g_exc_error;
4705     END IF;
4706 
4707 END Update_OSP_Order_Lines;
4708 
4709 -- yazhou 10-Apr-2006 starts
4710 -- Bug fix #4998349
4711 
4712 PROCEDURE Update_OSP_Line_Exch_Instance(
4713 	p_osp_order_id   IN NUMBER,
4714     p_osp_line_id    IN NUMBER,
4715     p_exchange_instance_id   IN NUMBER
4716 )IS
4717 
4718 
4719 -- Check if the instance is a valid IB instance
4720 -- Also not part of relationship
4721 
4722     CURSOR val_exg_instance_id_csr(p_instance_id IN NUMBER) IS
4723       SELECT 'x' FROM csi_item_instances csi
4724         WHERE instance_id = p_instance_id
4725           AND nvl(csi.active_end_date, sysdate + 1) > sysdate
4726           AND NOT EXISTS (select subject_id from csi_ii_relationships where
4727                            subject_id = p_instance_id and
4728                            relationship_type_code = 'COMPONENT-OF' and
4729                            NVL(ACTIVE_START_DATE, SYSDATE - 1) < SYSDATE AND
4730 						   NVL(ACTIVE_END_DATE, SYSDATE + 1) > SYSDATE
4731                           ) ;
4732 
4733     l_exist VARCHAR2(1);
4734 
4735 -- retrieve order line details
4736 
4737  CURSOR ahl_osp_lines_csr(p_osp_id IN NUMBER, p_osp_line_id IN NUMBER) IS
4738     SELECT a.OSP_ORDER_LINE_ID,
4739            a.OBJECT_VERSION_NUMBER,
4740            a.LAST_UPDATE_DATE,
4741            a.LAST_UPDATED_BY,
4742            a.LAST_UPDATE_LOGIN,
4743            a.OSP_ORDER_ID,
4744            a.OSP_LINE_NUMBER,
4745            a.STATUS_CODE,
4746            a.PO_LINE_TYPE_ID,
4747            a.SERVICE_ITEM_ID,
4748            a.SERVICE_ITEM_DESCRIPTION,
4749            a.SERVICE_ITEM_UOM_CODE,
4750            a.NEED_BY_DATE,
4751            a.SHIP_BY_DATE,
4752            a.PO_LINE_ID,
4753            a.OE_SHIP_LINE_ID,
4754            a.OE_RETURN_LINE_ID,
4755            a.WORKORDER_ID,
4756            a.OPERATION_ID,
4757            a.EXCHANGE_INSTANCE_ID,
4758            a.INVENTORY_ITEM_ID,
4759            a.INVENTORY_ORG_ID,
4760            a.SERIAL_NUMBER,
4761            a.LOT_NUMBER,
4762            a.INVENTORY_ITEM_UOM,
4763            a.INVENTORY_ITEM_QUANTITY,
4764            a.SUB_INVENTORY,
4765            a.QUANTITY,
4766            a.ATTRIBUTE_CATEGORY,
4767            a.ATTRIBUTE1,
4768            a.ATTRIBUTE2,
4769            a.ATTRIBUTE3,
4770            a.ATTRIBUTE4,
4771            a.ATTRIBUTE5,
4772            a.ATTRIBUTE6,
4773            a.ATTRIBUTE7,
4774            a.ATTRIBUTE8,
4775            a.ATTRIBUTE9,
4776            a.ATTRIBUTE10,
4777            a.ATTRIBUTE11,
4778            a.ATTRIBUTE12,
4779            a.ATTRIBUTE13,
4780            a.ATTRIBUTE14,
4781            a.ATTRIBUTE15,
4782 -- Begin Changes by jaramana on January 14, 2008 for the Requisition ER 6034236
4783            a.PO_REQ_LINE_ID
4784 -- End Changes by jaramana on January 14, 2008 for the Requisition ER 6034236
4785 
4786     FROM AHL_OSP_ORDER_LINES a
4787     WHERE a.osp_order_id = p_osp_id
4788       AND a.osp_order_line_id = p_osp_line_id;
4789 
4790 --
4791   l_row_check      VARCHAR2(1):='N';
4792 --
4793 BEGIN
4794 
4795   -- Validate exchange instance
4796   OPEN val_exg_instance_id_csr(p_exchange_instance_id);
4797   FETCH val_exg_instance_id_csr INTO l_exist;
4798   IF (val_exg_instance_id_csr %NOTFOUND) THEN
4799         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_SHIP_COMPONENT');
4800         FND_MSG_PUB.ADD;
4801         CLOSE val_exg_instance_id_csr;
4802         RAISE Fnd_Api.g_exc_error;
4803   END IF;
4804   CLOSE val_exg_instance_id_csr;
4805 
4806   FOR l_osp_line_rec IN ahl_osp_lines_csr(p_osp_order_id, p_osp_line_id)
4807    LOOP
4808      l_row_check := 'Y';
4809 
4810      AHL_OSP_ORDER_LINES_PKG.UPDATE_ROW (
4811             P_OSP_ORDER_LINE_ID        => l_osp_line_rec.OSP_ORDER_LINE_ID,
4812             P_OBJECT_VERSION_NUMBER    => l_osp_line_rec.OBJECT_VERSION_NUMBER+1,
4813             P_LAST_UPDATE_DATE         => l_osp_line_rec.LAST_UPDATE_DATE,
4814             P_LAST_UPDATED_BY          => l_osp_line_rec.LAST_UPDATED_BY,
4815             P_LAST_UPDATE_LOGIN        => l_osp_line_rec.LAST_UPDATE_LOGIN,
4816             P_OSP_ORDER_ID             => l_osp_line_rec.OSP_ORDER_ID,
4817             P_OSP_LINE_NUMBER          => l_osp_line_rec.OSP_LINE_NUMBER,
4818             P_STATUS_CODE              => l_osp_line_rec.STATUS_CODE,
4819             P_PO_LINE_TYPE_ID          => l_osp_line_rec.PO_LINE_TYPE_ID,
4820             P_SERVICE_ITEM_ID          => l_osp_line_rec.SERVICE_ITEM_ID,
4821             P_SERVICE_ITEM_DESCRIPTION => l_osp_line_rec.SERVICE_ITEM_DESCRIPTION,
4822             P_SERVICE_ITEM_UOM_CODE    => l_osp_line_rec.SERVICE_ITEM_UOM_CODE,
4823             P_NEED_BY_DATE             => l_osp_line_rec.NEED_BY_DATE,
4824             P_SHIP_BY_DATE             => l_osp_line_rec.SHIP_BY_DATE,
4825             P_PO_LINE_ID               => l_osp_line_rec.PO_LINE_ID,
4826             P_OE_SHIP_LINE_ID          => l_osp_line_rec.OE_SHIP_LINE_ID,
4827             P_OE_RETURN_LINE_ID        => l_osp_line_rec.OE_RETURN_LINE_ID,
4828             P_WORKORDER_ID             => l_osp_line_rec.WORKORDER_ID,
4829             P_OPERATION_ID             => l_osp_line_rec.OPERATION_ID,
4830             P_QUANTITY                 => l_osp_line_rec.QUANTITY,
4831             P_EXCHANGE_INSTANCE_ID     => p_exchange_instance_id,
4832             P_INVENTORY_ITEM_ID        => l_osp_line_rec.INVENTORY_ITEM_ID,
4833             P_INVENTORY_ORG_ID         => l_osp_line_rec.INVENTORY_ORG_ID,
4834             P_INVENTORY_ITEM_UOM       => l_osp_line_rec.INVENTORY_ITEM_UOM,
4835             P_INVENTORY_ITEM_QUANTITY  => l_osp_line_rec.INVENTORY_ITEM_QUANTITY,
4836             P_SUB_INVENTORY            => l_osp_line_rec.SUB_INVENTORY,
4837             P_LOT_NUMBER               => l_osp_line_rec.LOT_NUMBER,
4838             P_SERIAL_NUMBER            => l_osp_line_rec.SERIAL_NUMBER,
4839 -- Begin Changes by jaramana on January 14, 2008 for the Requisition ER 6034236
4840             P_PO_REQ_LINE_ID           => l_osp_line_rec.PO_REQ_LINE_ID,
4841 -- End Changes by jaramana on January 14, 2008 for the Requisition ER 6034236
4842             P_ATTRIBUTE_CATEGORY       => l_osp_line_rec.ATTRIBUTE_CATEGORY,
4843             P_ATTRIBUTE1               => l_osp_line_rec.ATTRIBUTE1,
4844             P_ATTRIBUTE2               => l_osp_line_rec.ATTRIBUTE2,
4845             P_ATTRIBUTE3               => l_osp_line_rec.ATTRIBUTE3,
4846             P_ATTRIBUTE4               => l_osp_line_rec.ATTRIBUTE4,
4847             P_ATTRIBUTE5               => l_osp_line_rec.ATTRIBUTE5,
4848             P_ATTRIBUTE6               => l_osp_line_rec.ATTRIBUTE6,
4849             P_ATTRIBUTE7               => l_osp_line_rec.ATTRIBUTE7,
4850             P_ATTRIBUTE8               => l_osp_line_rec.ATTRIBUTE8,
4851             P_ATTRIBUTE9               => l_osp_line_rec.ATTRIBUTE9,
4852             P_ATTRIBUTE10              => l_osp_line_rec.ATTRIBUTE10,
4853             P_ATTRIBUTE11              => l_osp_line_rec.ATTRIBUTE11,
4854             P_ATTRIBUTE12              => l_osp_line_rec.ATTRIBUTE12,
4855             P_ATTRIBUTE13              => l_osp_line_rec.ATTRIBUTE13,
4856             P_ATTRIBUTE14              => l_osp_line_rec.ATTRIBUTE14,
4857             P_ATTRIBUTE15              => l_osp_line_rec.ATTRIBUTE15 );
4858     END LOOP;
4859 
4860     IF l_row_check = 'N' THEN
4861             Fnd_Message.set_name('AHL', 'AHL_OSP_INVALID_LINE_ITEM');
4862             Fnd_Msg_Pub.ADD;
4863             RAISE Fnd_Api.g_exc_error;
4864     END IF;
4865 
4866 END Update_OSP_Line_Exch_Instance;
4867 
4868 -- yazhou 10-Apr-2006 ends
4869 
4870 --
4871 --Clears all osp_order_lines with either ship_line_id or return_line_id
4872 --
4873 PROCEDURE Delete_OE_Lines(p_oe_line_id       IN NUMBER) IS
4874 --
4875 --Fetch all osp lines with reference to p_oe_line_id
4876   CURSOR ahl_osp_lines_csr(p_line_id   IN NUMBER) IS
4877     SELECT  *
4878      FROM AHL_OSP_ORDER_LINES
4879     WHERE oe_ship_line_id = p_line_id
4880       OR  oe_return_line_id = p_line_id;
4881 
4882 --
4883   l_oe_ship_line_id    	 NUMBER;
4884   l_oe_return_line_id 	 NUMBER;
4885 --
4886 BEGIN
4887 
4888    FOR l_osp_line_rec IN ahl_osp_lines_csr(p_oe_line_id) LOOP
4889 
4890      --Remove all references to p_oe_line_id
4891      IF (p_oe_line_id = l_osp_line_rec.oe_ship_line_id) THEN
4892          l_oe_ship_line_id := NULL;
4893      ELSE
4894          l_oe_ship_line_id := l_osp_line_rec.oe_ship_line_id;
4895      END IF;
4896 
4897      IF (p_oe_line_id = l_osp_line_rec.oe_return_line_id) THEN
4898          l_oe_return_line_id := NULL;
4899      ELSE
4900          l_oe_return_line_id := l_osp_line_rec.oe_return_line_id;
4901      END IF;
4902      AHL_OSP_ORDER_LINES_PKG.UPDATE_ROW (
4903             P_OSP_ORDER_LINE_ID        => l_osp_line_rec.OSP_ORDER_LINE_ID,
4904             P_OBJECT_VERSION_NUMBER    => l_osp_line_rec.OBJECT_VERSION_NUMBER+1,
4905             P_LAST_UPDATE_DATE         => l_osp_line_rec.LAST_UPDATE_DATE,
4906             P_LAST_UPDATED_BY          => l_osp_line_rec.LAST_UPDATED_BY,
4907             P_LAST_UPDATE_LOGIN        => l_osp_line_rec.LAST_UPDATE_LOGIN,
4908             P_OSP_ORDER_ID             => l_osp_line_rec.OSP_ORDER_ID,
4909             P_OSP_LINE_NUMBER          => l_osp_line_rec.OSP_LINE_NUMBER,
4910             P_STATUS_CODE              => l_osp_line_rec.STATUS_CODE,
4911             P_PO_LINE_TYPE_ID          => l_osp_line_rec.PO_LINE_TYPE_ID,
4912             P_SERVICE_ITEM_ID          => l_osp_line_rec.SERVICE_ITEM_ID,
4913             P_SERVICE_ITEM_DESCRIPTION => l_osp_line_rec.SERVICE_ITEM_DESCRIPTION,
4914             P_SERVICE_ITEM_UOM_CODE    => l_osp_line_rec.SERVICE_ITEM_UOM_CODE,
4915             P_NEED_BY_DATE             => l_osp_line_rec.NEED_BY_DATE,
4916             P_SHIP_BY_DATE             => l_osp_line_rec.SHIP_BY_DATE,
4917             P_PO_LINE_ID               => l_osp_line_rec.PO_LINE_ID,
4918             P_OE_SHIP_LINE_ID          => l_oe_ship_line_id,
4919             P_OE_RETURN_LINE_ID        => l_oe_return_line_id,
4920             P_WORKORDER_ID             => l_osp_line_rec.WORKORDER_ID,
4921             P_OPERATION_ID             => l_osp_line_rec.OPERATION_ID,
4922             P_QUANTITY                 => l_osp_line_rec.QUANTITY,
4923             P_INVENTORY_ITEM_ID        => l_osp_line_rec.INVENTORY_ITEM_ID,
4924             P_INVENTORY_ORG_ID         => l_osp_line_rec.INVENTORY_ORG_ID,
4925             P_INVENTORY_ITEM_UOM       => l_osp_line_rec.INVENTORY_ITEM_UOM,
4926             P_INVENTORY_ITEM_QUANTITY  => l_osp_line_rec.INVENTORY_ITEM_QUANTITY,
4927             P_SUB_INVENTORY            => l_osp_line_rec.SUB_INVENTORY,
4928             P_LOT_NUMBER               => l_osp_line_rec.LOT_NUMBER,
4929             P_SERIAL_NUMBER            => l_osp_line_rec.SERIAL_NUMBER,
4930 -- Begin Changes by jaramana on January 14, 2008 for the Requisition ER 6034236
4931             P_PO_REQ_LINE_ID           => l_osp_line_rec.PO_REQ_LINE_ID,
4932 -- End Changes by jaramana on January 14, 2008 for the Requisition ER 6034236
4933             P_EXCHANGE_INSTANCE_ID     => l_osp_line_rec.EXCHANGE_INSTANCE_ID,
4934             P_ATTRIBUTE_CATEGORY       => l_osp_line_rec.ATTRIBUTE_CATEGORY,
4935             P_ATTRIBUTE1               => l_osp_line_rec.ATTRIBUTE1,
4936             P_ATTRIBUTE2               => l_osp_line_rec.ATTRIBUTE2,
4937             P_ATTRIBUTE3               => l_osp_line_rec.ATTRIBUTE3,
4938             P_ATTRIBUTE4               => l_osp_line_rec.ATTRIBUTE4,
4939             P_ATTRIBUTE5               => l_osp_line_rec.ATTRIBUTE5,
4940             P_ATTRIBUTE6               => l_osp_line_rec.ATTRIBUTE6,
4941             P_ATTRIBUTE7               => l_osp_line_rec.ATTRIBUTE7,
4942             P_ATTRIBUTE8               => l_osp_line_rec.ATTRIBUTE8,
4943             P_ATTRIBUTE9               => l_osp_line_rec.ATTRIBUTE9,
4944             P_ATTRIBUTE10              => l_osp_line_rec.ATTRIBUTE10,
4945             P_ATTRIBUTE11              => l_osp_line_rec.ATTRIBUTE11,
4946             P_ATTRIBUTE12              => l_osp_line_rec.ATTRIBUTE12,
4947             P_ATTRIBUTE13              => l_osp_line_rec.ATTRIBUTE13,
4948             P_ATTRIBUTE14              => l_osp_line_rec.ATTRIBUTE14,
4949             P_ATTRIBUTE15              => l_osp_line_rec.ATTRIBUTE15 );
4950 
4951     END LOOP;
4952 
4953 END Delete_OE_Lines;
4954 
4955 -- Create IB sub-txn for OSP order.
4956 PROCEDURE Create_IB_Transaction(
4957     p_init_msg_list          IN            VARCHAR2  := FND_API.G_FALSE,
4958     p_commit                 IN            VARCHAR2  := FND_API.G_FALSE,
4959     p_validation_level       IN            NUMBER    := FND_API.G_VALID_LEVEL_FULL,
4960     x_return_status          OUT NOCOPY            VARCHAR2,
4961     x_msg_count              OUT NOCOPY            NUMBER,
4962     x_msg_data               OUT NOCOPY            VARCHAR2,
4963     p_OSP_order_type         IN            VARCHAR2,
4964     p_oe_line_type           IN            VARCHAR2,
4965     p_oe_line_id             IN            NUMBER,
4966     p_csi_instance_id        IN            NUMBER)
4967 IS
4968 
4969     -- get csi transaction type.
4970     CURSOR csi_txn_types_csr(p_source_txn_type IN VARCHAR2) IS
4971       SELECT transaction_type_id
4972       FROM csi_txn_types
4973       WHERE source_transaction_type = p_source_txn_type;
4974 
4975     /* Commented out by jeli on 11/30/05 for ER 4746426
4976     -- get sub-type id based on transaction.
4977     CURSOR csi_txn_sub_types_csr(p_transaction_type_id IN NUMBER,
4978                                  p_sub_type_name       IN VARCHAR2) IS
4979       SELECT sub_type_id
4980       FROM csi_txn_sub_types
4981       WHERE transaction_type_id = p_transaction_type_id
4982         AND name = p_sub_type_name;
4983     */
4984 
4985     -- get oe line details.
4986     CURSOR oe_order_lines_csr (p_oe_line_id IN NUMBER) IS
4987       SELECT inventory_item_id, ordered_quantity, ship_from_org_id, order_quantity_uom
4988       FROM oe_order_lines_all
4989       WHERE line_id = p_oe_line_id;
4990 
4991     -- get instance details.
4992     CURSOR csi_item_instances_csr (p_instance_id IN NUMBER) IS
4993       SELECT serial_number, inventory_revision, lot_number, mfg_serial_number_flag
4994       FROM csi_item_instances
4995       WHERE instance_id = p_instance_id;
4996 
4997     -- get value for return by date.
4998     CURSOR ahl_osp_order_lines_csr (p_oe_line_id   IN NUMBER) IS
4999       SELECT nvl(need_by_date, ship_by_date)
5000       FROM ahl_osp_order_lines
5001       WHERE oe_ship_line_id = p_oe_line_id;
5002 
5003     CURSOR get_internal_party_csr IS
5004       SELECT internal_party_id from csi_install_parameters;
5005 
5006     CURSOR get_sold_to_org(p_oe_line_id IN NUMBER) IS
5007       SELECT HZ.PARTY_ID, HZ.CUST_ACCOUNT_ID from HZ_CUST_ACCOUNTS HZ,
5008                               OE_ORDER_HEADERS_ALL OE,
5009                               oe_order_lines_all OEL
5010       WHERE OEL.line_id = p_oe_line_id AND
5011             OE.HEADER_ID = OEL.HEADER_ID AND
5012             HZ.CUST_ACCOUNT_ID = OE.SOLD_TO_ORG_ID;
5013 
5014     CURSOR get_owner_ip_id(p_instance_id IN NUMBER) IS
5015       SELECT INSTANCE_PARTY_ID, PARTY_ID from csi_i_parties
5016       WHERE INSTANCE_ID = p_instance_id AND
5017             RELATIONSHIP_TYPE_CODE = 'OWNER' AND
5018             NVL(ACTIVE_START_DATE, SYSDATE - 1) <= SYSDATE AND
5019             NVL(ACTIVE_END_DATE, SYSDATE + 1) >= SYSDATE;
5020 
5021 
5022 
5023     l_txn_line_rec             csi_t_datastructures_grp.txn_line_rec;
5024     l_txn_line_dtl_tbl         csi_t_datastructures_grp.txn_line_detail_tbl;
5025     l_txn_party_tbl            csi_t_datastructures_grp.txn_party_detail_tbl;
5026     l_txn_pty_acct_tbl         csi_t_datastructures_grp.txn_pty_acct_detail_tbl;
5027     l_txn_org_assgn_tbl        csi_t_datastructures_grp.txn_org_assgn_tbl;
5028 
5029     l_txn_ii_reln_tbl          csi_t_datastructures_grp.txn_ii_rltns_tbl;
5030     l_txn_ext_attrib_vals_tbl  csi_t_datastructures_grp.txn_ext_attrib_vals_tbl;
5031     l_txn_systems_tbl          csi_t_datastructures_grp.txn_systems_tbl;
5032 
5033     --l_subtxn_name              VARCHAR2(30); --for ER 4746426
5034     l_om_order_id              NUMBER;
5035     l_subtxn_id                NUMBER;
5036     l_return_by_date           DATE;
5037     l_party_dtl_rec            csi_t_datastructures_grp.txn_party_detail_rec;
5038     l_new_party_id             NUMBER;
5039     l_new_party_account_id     NUMBER;
5040     l_internal_party_id        NUMBER;
5041     l_curr_inst_pty_id         NUMBER;
5042     l_curr_pty_id              NUMBER;
5043     l_party_account_rec        csi_t_datastructures_grp.txn_pty_acct_detail_rec;
5044 
5045     L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.Create_IB_Transaction';
5046 
5047 BEGIN
5048 
5049    -- Initialize return status.
5050    x_return_status := FND_API.G_RET_STS_SUCCESS;
5051 
5052    IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5053      FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Entering Procedure');
5054    END IF;
5055 
5056    IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5057      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'p_OSP_order_type = ' || p_OSP_order_type || ', p_oe_line_type = ' || p_oe_line_type ||
5058                                                           ', p_oe_line_id = ' || p_oe_line_id || ', p_csi_instance_id = ' || p_csi_instance_id);
5059    END IF;
5060 
5061    -- Check input order and line types.
5062    -- If types not valid , return.
5063    IF (p_oe_line_type NOT IN ('ORDER','RETURN')) OR
5064       (p_OSP_order_type NOT IN (AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_SERVICE,
5065                                 AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_LOAN,
5066                                 AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_BORROW,
5067                                 AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_EXCHANGE)) THEN
5068       RETURN;
5069    END IF;
5070 
5071    IF (p_oe_line_type = 'ORDER') THEN
5072 
5073       -- get transaction type ID for OM-Shipment.
5074       OPEN csi_txn_types_csr(G_OM_ORDER);
5075       FETCH csi_txn_types_csr INTO l_om_order_id;
5076       IF (csi_txn_types_csr%NOTFOUND) THEN
5077          CLOSE csi_txn_types_csr;
5078          FND_MESSAGE.Set_Name('AHL','AHL_OSP_IB_TXN_NOTFOUND');
5079          FND_MESSAGE.Set_Token('TYPE',G_OM_ORDER);
5080          FND_MSG_PUB.ADD;
5081          x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5082          RETURN;
5083       END IF;
5084       CLOSE csi_txn_types_csr;
5085 
5086       -- get csi transaction ID for sub type based on OSP Order type
5087       IF p_OSP_order_type = AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_EXCHANGE THEN
5088         --l_subtxn_name := G_SUBTXN_EXC_ORDER; --for ER 4746426
5089         l_subtxn_id := FND_PROFILE.VALUE('AHL_OSP_IB_SUBTXN_EXC_SHIP');
5090         IF l_subtxn_id IS NULL THEN
5091           --FND_MESSAGE.Set_Name('AHL','AHL_OSP_IB_SUBTXN_NOTFOUND');
5092           --FND_MESSAGE.Set_Token('TYPE','Exchange Order Ship');
5093           FND_MESSAGE.set_name('AHL', 'AHL_OSP_PROFILE_NULL');
5094           FND_MESSAGE.SET_TOKEN('PROFILE', get_user_profile_option_name('AHL_OSP_IB_SUBTXN_EXC_SHIP'));
5095           FND_MSG_PUB.ADD;
5096           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5097           RETURN;
5098         END IF;
5099         OPEN get_sold_to_org(p_oe_line_id);
5100         FETCH get_sold_to_org INTO l_new_party_id, l_new_party_account_id;
5101         IF (get_sold_to_org%NOTFOUND) THEN
5102           IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
5103             Fnd_Message.set_name('AHL', 'AHL_OSP_CANT_GET_NEW_PARTY');
5104             FND_MESSAGE.SET_TOKEN('INSTANCE_NUMBER', GET_INSTANCE_NUM_FROM_ID(p_csi_instance_id));
5105             Fnd_Msg_Pub.ADD;
5106           END IF;
5107           CLOSE get_sold_to_org;
5108           RAISE Fnd_Api.g_exc_error;
5109         END IF;
5110         CLOSE get_sold_to_org;
5111         -- Populate Party Details
5112         l_party_dtl_rec.PARTY_SOURCE_TABLE := 'HZ_PARTIES';
5113         l_party_dtl_rec.PARTY_SOURCE_ID := l_new_party_id;
5114         l_party_dtl_rec.RELATIONSHIP_TYPE_CODE := 'OWNER';
5115         l_party_dtl_rec.CONTACT_FLAG := 'N';
5116         l_party_dtl_rec.TXN_LINE_DETAILS_INDEX := 1;
5117         -- Populate Party Account Details
5118         l_party_account_rec.ACCOUNT_ID := l_new_party_account_id;
5119         l_party_account_rec.RELATIONSHIP_TYPE_CODE := 'OWNER';
5120         l_party_account_rec.TXN_PARTY_DETAILS_INDEX := 1;
5121         OPEN get_owner_ip_id(p_csi_instance_id);
5122         FETCH get_owner_ip_id INTO l_curr_inst_pty_id,
5123                                         l_curr_pty_id;
5124         IF (get_owner_ip_id%NOTFOUND) THEN
5125           IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
5126             Fnd_Message.set_name('AHL', 'AHL_OSP_CANT_GET_CURR_PARTY');
5127             FND_MESSAGE.SET_TOKEN('INSTANCE_NUMBER', GET_INSTANCE_NUM_FROM_ID(p_csi_instance_id));
5128             Fnd_Msg_Pub.ADD;
5129           END IF;
5130           CLOSE get_owner_ip_id;
5131           RAISE Fnd_Api.g_exc_error;
5132         END IF;
5133         CLOSE get_owner_ip_id;
5134         l_party_dtl_rec.INSTANCE_PARTY_ID := l_curr_inst_pty_id;
5135 
5136         l_txn_party_tbl(1) := l_party_dtl_rec;
5137         l_txn_pty_acct_tbl(1) := l_party_account_rec;
5138         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5139           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_party_dtl_rec:');
5140           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  PARTY_SOURCE_TABLE = ' || l_party_dtl_rec.PARTY_SOURCE_TABLE);
5141           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  PARTY_SOURCE_ID = ' || l_party_dtl_rec.PARTY_SOURCE_ID);
5142           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  RELATIONSHIP_TYPE_CODE = ' || l_party_dtl_rec.RELATIONSHIP_TYPE_CODE);
5143           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  CONTACT_FLAG = ' || l_party_dtl_rec.CONTACT_FLAG);
5144           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  TXN_LINE_DETAILS_INDEX = ' || l_party_dtl_rec.TXN_LINE_DETAILS_INDEX);
5145           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  INSTANCE_PARTY_ID = ' || l_party_dtl_rec.INSTANCE_PARTY_ID);
5146 
5147           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_party_account_rec:');
5148           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  ACCOUNT_ID = ' || l_party_account_rec.ACCOUNT_ID);
5149           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  RELATIONSHIP_TYPE_CODE = ' || l_party_account_rec.RELATIONSHIP_TYPE_CODE);
5150           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  TXN_PARTY_DETAILS_INDEX = ' || l_party_account_rec.TXN_PARTY_DETAILS_INDEX);
5151         END IF;
5152       ELSIF p_OSP_order_type = AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_SERVICE THEN
5153         l_subtxn_id := FND_PROFILE.VALUE('AHL_OSP_IB_SUBTXN_SER_SHIP');
5154         IF l_subtxn_id IS NULL THEN
5155           --FND_MESSAGE.Set_Name('AHL','AHL_OSP_IB_SUBTXN_NOTFOUND');
5156           --FND_MESSAGE.Set_Token('TYPE','Service Order Ship');
5157           FND_MESSAGE.set_name('AHL', 'AHL_OSP_PROFILE_NULL');
5158           FND_MESSAGE.SET_TOKEN('PROFILE', get_user_profile_option_name('AHL_OSP_IB_SUBTXN_SER_SHIP'));
5159           FND_MSG_PUB.ADD;
5160           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5161           RETURN;
5162         END IF;
5163       ELSIF p_OSP_order_type = AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_BORROW THEN
5164         --l_subtxn_name := G_SUBTXN_ORDER;
5165         l_subtxn_id := FND_PROFILE.VALUE('AHL_OSP_IB_SUBTXN_BOR_SHIP');
5166         IF l_subtxn_id IS NULL THEN
5167           --FND_MESSAGE.Set_Name('AHL','AHL_OSP_IB_SUBTXN_NOTFOUND');
5168           --FND_MESSAGE.Set_Token('TYPE','Borrow Order Ship');
5169           FND_MESSAGE.set_name('AHL', 'AHL_OSP_PROFILE_NULL');
5170           FND_MESSAGE.SET_TOKEN('PROFILE', get_user_profile_option_name('AHL_OSP_IB_SUBTXN_BOR_SHIP'));
5171           FND_MSG_PUB.ADD;
5172           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5173           RETURN;
5174         END IF;
5175       END IF;
5176 
5177       /* Commented out by jeli on 11/30/05 for ER 4746426
5178       OPEN csi_txn_sub_types_csr(l_om_order_id, l_subtxn_name);
5179       FETCH csi_txn_sub_types_csr INTO l_subtxn_id;
5180       IF (csi_txn_sub_types_csr%NOTFOUND) THEN
5181          CLOSE csi_txn_sub_types_csr;
5182          FND_MESSAGE.Set_Name('AHL','AHL_OSP_IB_SUBTXN_NOTFOUND');
5183          FND_MESSAGE.Set_Token('TYPE',l_subtxn_name);
5184          FND_MSG_PUB.ADD;
5185          x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5186          RETURN;
5187       END IF;
5188       CLOSE csi_txn_sub_types_csr;
5189       */
5190    ELSIF (p_oe_line_type = 'RETURN') THEN
5191 
5192       -- get transaction type ID for RMA-Receipt.
5193       OPEN csi_txn_types_csr(G_OM_RETURN);
5194       FETCH csi_txn_types_csr INTO l_om_order_id;
5195       IF (csi_txn_types_csr%NOTFOUND) THEN
5196          CLOSE csi_txn_types_csr;
5197          FND_MESSAGE.Set_Name('AHL','AHL_OSP_IB_TXN_NOTFOUND');
5198          FND_MESSAGE.Set_Token('TYPE',G_OM_RETURN);
5199          FND_MSG_PUB.ADD;
5200          x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5201          RETURN;
5202       END IF;
5203       CLOSE csi_txn_types_csr;
5204 
5205       -- get csi transaction ID for sub type based on OSP Order type
5206       IF p_OSP_order_type = AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_EXCHANGE THEN
5207         --l_subtxn_name := G_SUBTXN_EXC_RETURN; --for ER 4746426
5208         l_subtxn_id := fnd_profile.VALUE('AHL_OSP_IB_SUBTXN_EXC_RETURN');
5209         IF l_subtxn_id IS NULL THEN
5210           --FND_MESSAGE.Set_Name('AHL','AHL_OSP_IB_SUBTXN_NOTFOUND');
5211           --FND_MESSAGE.Set_Token('TYPE','Exchange Order Return');
5212           FND_MESSAGE.set_name('AHL', 'AHL_OSP_PROFILE_NULL');
5213           FND_MESSAGE.SET_TOKEN('PROFILE', get_user_profile_option_name('AHL_OSP_IB_SUBTXN_EXC_RETURN'));
5214           FND_MSG_PUB.ADD;
5215           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5216           RETURN;
5217         END IF;
5218         -- Get internal Party Id
5219         OPEN get_internal_party_csr;
5220         FETCH get_internal_party_csr INTO l_internal_party_id;
5221         IF (get_internal_party_csr%NOTFOUND) THEN
5222           IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
5223             Fnd_Message.set_name('AHL', 'AHL_OSP_CANT_GET_INT_PARTY');
5224             Fnd_Msg_Pub.ADD;
5225           END IF;
5226           CLOSE get_internal_party_csr;
5227           RAISE Fnd_Api.g_exc_error;
5228         END IF;
5229         CLOSE get_internal_party_csr;
5230         l_party_dtl_rec.PARTY_SOURCE_TABLE := 'HZ_PARTIES';
5231         l_party_dtl_rec.PARTY_SOURCE_ID := l_internal_party_id;
5232         l_party_dtl_rec.RELATIONSHIP_TYPE_CODE := 'OWNER';
5233         l_party_dtl_rec.CONTACT_FLAG := 'N';
5234         l_party_dtl_rec.TXN_LINE_DETAILS_INDEX := 1;
5235         --mpothuku modified on 14-Sep-2007 to fix the Bug 6398921
5236         --we are creating the IB transactions even if the instance is not present on the order line
5237         IF(p_csi_instance_id is not null) THEN
5238           OPEN get_owner_ip_id(p_csi_instance_id);
5239           FETCH get_owner_ip_id INTO l_curr_inst_pty_id,
5240                                           l_curr_pty_id;
5241           IF (get_owner_ip_id%NOTFOUND) THEN
5242             IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
5243               Fnd_Message.set_name('AHL', 'AHL_OSP_CANT_GET_CURR_PARTY');
5244               FND_MESSAGE.SET_TOKEN('INSTANCE_NUMBER', GET_INSTANCE_NUM_FROM_ID(p_csi_instance_id));
5245               Fnd_Msg_Pub.ADD;
5246             END IF;
5247             CLOSE get_owner_ip_id;
5248             RAISE Fnd_Api.g_exc_error;
5249           END IF;
5250           CLOSE get_owner_ip_id;
5251         END IF;
5252         ---mpothuku End
5253         l_party_dtl_rec.INSTANCE_PARTY_ID := l_curr_inst_pty_id;
5254         l_txn_party_tbl(1) := l_party_dtl_rec;
5255         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5256           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_party_dtl_rec:');
5257           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  PARTY_SOURCE_TABLE = ' || l_party_dtl_rec.PARTY_SOURCE_TABLE);
5258           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  PARTY_SOURCE_ID = ' || l_party_dtl_rec.PARTY_SOURCE_ID);
5259           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  RELATIONSHIP_TYPE_CODE = ' || l_party_dtl_rec.RELATIONSHIP_TYPE_CODE);
5260           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  CONTACT_FLAG = ' || l_party_dtl_rec.CONTACT_FLAG);
5261           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  TXN_LINE_DETAILS_INDEX = ' || l_party_dtl_rec.TXN_LINE_DETAILS_INDEX);
5262           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  INSTANCE_PARTY_ID = ' || l_party_dtl_rec.INSTANCE_PARTY_ID);
5263         END IF;
5264       ELSIF p_OSP_order_type = AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_SERVICE THEN
5265         l_subtxn_id := FND_PROFILE.VALUE('AHL_OSP_IB_SUBTXN_SER_RETURN');
5266         IF l_subtxn_id IS NULL THEN
5267           --FND_MESSAGE.Set_Name('AHL','AHL_OSP_IB_SUBTXN_NOTFOUND');
5268           --FND_MESSAGE.Set_Token('TYPE','Service Order Return');
5269           FND_MESSAGE.set_name('AHL', 'AHL_OSP_PROFILE_NULL');
5270           FND_MESSAGE.SET_TOKEN('PROFILE', get_user_profile_option_name('AHL_OSP_IB_SUBTXN_SER_RETURN'));
5271           FND_MSG_PUB.ADD;
5272           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5273           RETURN;
5274         END IF;
5275       ELSIF p_OSP_order_type = AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_BORROW THEN
5276         l_subtxn_id := FND_PROFILE.VALUE('AHL_OSP_IB_SUBTXN_BOR_RETURN');
5277         IF l_subtxn_id IS NULL THEN
5278           --FND_MESSAGE.Set_Name('AHL','AHL_OSP_IB_SUBTXN_NOTFOUND');
5279           --FND_MESSAGE.Set_Token('TYPE','Borrow Order Return');
5280           FND_MESSAGE.set_name('AHL', 'AHL_OSP_PROFILE_NULL');
5281           FND_MESSAGE.SET_TOKEN('PROFILE', get_user_profile_option_name('AHL_OSP_IB_SUBTXN_BOR_RETURN'));
5282           FND_MSG_PUB.ADD;
5283           x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5284           RETURN;
5285         END IF;
5286         --l_subtxn_name := G_SUBTXN_RETURN; for ER 4746426
5287       END IF;
5288       /* Commented out by jeli on 11/30/05 for ER 4746426
5289       OPEN csi_txn_sub_types_csr(l_om_order_id, l_subtxn_name);
5290       FETCH csi_txn_sub_types_csr INTO l_subtxn_id;
5291       IF (csi_txn_sub_types_csr%NOTFOUND) THEN
5292          CLOSE csi_txn_sub_types_csr;
5293          FND_MESSAGE.Set_Name('AHL','AHL_OSP_IB_SUBTXN_NOTFOUND');
5294          FND_MESSAGE.Set_Token('TYPE',l_subtxn_name);
5295          FND_MSG_PUB.ADD;
5296          x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5297          RETURN;
5298       END IF;
5299       CLOSE csi_txn_sub_types_csr;
5300       */
5301    END IF;
5302 
5303    l_txn_line_rec.TRANSACTION_LINE_ID := fnd_api.g_miss_num;
5304    l_txn_line_rec.SOURCE_TRANSACTION_TYPE_ID := l_om_order_id;
5305    l_txn_line_rec.SOURCE_TRANSACTION_ID := p_oe_line_id;
5306    l_txn_line_rec.SOURCE_TRANSACTION_TABLE :=  G_TRANSACTION_TABLE;
5307 
5308 
5309    -- get order line details.
5310    OPEN oe_order_lines_csr (p_oe_line_id);
5311    FETCH oe_order_lines_csr INTO l_txn_line_dtl_tbl(1).INVENTORY_ITEM_ID,
5312                                  l_txn_line_dtl_tbl(1).QUANTITY,
5313                                  l_txn_line_dtl_tbl(1).INV_ORGANIZATION_ID,
5314                                  l_txn_line_dtl_tbl(1).UNIT_OF_MEASURE;
5315    CLOSE oe_order_lines_csr;
5316 
5317    -- get instance details.
5318    /*
5319     mpothuku modified on 14-Sep-2007 to fix the Bug 6398921
5320     we are creating the IB transactions even if the instance is not present on the order line for Exchange Return Lines
5321     we do not need to any clauses for exchange orders below.
5322    */
5323    IF(p_csi_instance_id is not null) THEN
5324      OPEN csi_item_instances_csr (p_csi_instance_id);
5325      FETCH csi_item_instances_csr INTO l_txn_line_dtl_tbl(1).SERIAL_NUMBER,
5326                                        l_txn_line_dtl_tbl(1).INVENTORY_REVISION,
5327                                        l_txn_line_dtl_tbl(1).LOT_NUMBER,
5328                                        l_txn_line_dtl_tbl(1).MFG_SERIAL_NUMBER_FLAG;
5329      CLOSE csi_item_instances_csr;
5330    ELSE
5331      l_txn_line_dtl_tbl(1).SERIAL_NUMBER := NULL;
5332      l_txn_line_dtl_tbl(1).INVENTORY_REVISION := NULL;
5333      l_txn_line_dtl_tbl(1).LOT_NUMBER := NULL;
5334      l_txn_line_dtl_tbl(1).MFG_SERIAL_NUMBER_FLAG := NULL;
5335    END IF;
5336    --mpothuku End
5337 
5338    l_txn_line_dtl_tbl(1).SOURCE_TRANSACTION_FLAG := 'Y';
5339 --   l_txn_line_dtl_tbl(1).TRANSACTION_LINE_ID := p_oe_line_id;
5340    l_txn_line_dtl_tbl(1).SUB_TYPE_ID := l_subtxn_id;
5341    /*
5342     mpothuku modified on 14-Sep-2007 to fix the Bug 6398921
5343     we are creating the IB transactions even if the instance is not present on the order line for Exchange Return Lines
5344     we do not need to any clauses for exchange orders below.
5345    */
5346    IF(p_csi_instance_id is not null) THEN
5347     l_txn_line_dtl_tbl(1).INSTANCE_EXISTS_FLAG := 'Y';
5348     l_txn_line_dtl_tbl(1).INSTANCE_ID := p_csi_instance_id;
5349    END IF;
5350    --mpothuku End
5351 
5352    l_txn_line_dtl_tbl(1).PRESERVE_DETAIL_FLAG := 'Y';
5353 
5354    -- get return by date.
5355    IF (p_oe_line_type = 'ORDER') THEN
5356       OPEN ahl_osp_order_lines_csr(p_oe_line_id);
5357       FETCH ahl_osp_order_lines_csr INTO l_return_by_date;
5358       CLOSE ahl_osp_order_lines_csr;
5359 
5360       -- if null then initialise to system date.
5361       IF (l_return_by_date IS NULL) THEN
5362          l_return_by_date := SYSDATE;
5363       END IF;
5364 
5365       l_txn_line_dtl_tbl(1).RETURN_BY_DATE := l_return_by_date;
5366 
5367    END IF;
5368 
5369    IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5370      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_txn_line_rec:');
5371      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  SOURCE_TRANSACTION_TYPE_ID = ' || l_txn_line_rec.SOURCE_TRANSACTION_TYPE_ID);
5372      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  SOURCE_TRANSACTION_ID = ' || l_txn_line_rec.SOURCE_TRANSACTION_ID);
5373      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  SOURCE_TRANSACTION_TABLE = ' || l_txn_line_rec.SOURCE_TRANSACTION_TABLE);
5374 
5375      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_txn_line_dtl_tbl(1):');
5376      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  INVENTORY_ITEM_ID = ' || l_txn_line_dtl_tbl(1).INVENTORY_ITEM_ID);
5377      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  QUANTITY = ' || l_txn_line_dtl_tbl(1).QUANTITY);
5378      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  INV_ORGANIZATION_ID = ' || l_txn_line_dtl_tbl(1).INV_ORGANIZATION_ID);
5379      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  UNIT_OF_MEASURE = ' || l_txn_line_dtl_tbl(1).UNIT_OF_MEASURE);
5380      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  SERIAL_NUMBER = ' || l_txn_line_dtl_tbl(1).SERIAL_NUMBER);
5381      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  INVENTORY_REVISION = ' || l_txn_line_dtl_tbl(1).INVENTORY_REVISION);
5382      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  LOT_NUMBER = ' || l_txn_line_dtl_tbl(1).LOT_NUMBER);
5383      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  MFG_SERIAL_NUMBER_FLAG = ' || l_txn_line_dtl_tbl(1).MFG_SERIAL_NUMBER_FLAG);
5384      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  SOURCE_TRANSACTION_FLAG = ' || l_txn_line_dtl_tbl(1).SOURCE_TRANSACTION_FLAG);
5385      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  TRANSACTION_LINE_ID = ' || l_txn_line_dtl_tbl(1).TRANSACTION_LINE_ID);
5386      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  SUB_TYPE_ID = ' || l_txn_line_dtl_tbl(1).SUB_TYPE_ID);
5387      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  INSTANCE_EXISTS_FLAG = ' || l_txn_line_dtl_tbl(1).INSTANCE_EXISTS_FLAG);
5388      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  INSTANCE_ID = ' || l_txn_line_dtl_tbl(1).INSTANCE_ID);
5389      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  PRESERVE_DETAIL_FLAG = ' || l_txn_line_dtl_tbl(1).PRESERVE_DETAIL_FLAG);
5390      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  RETURN_BY_DATE = ' || l_txn_line_dtl_tbl(1).RETURN_BY_DATE);
5391    END IF;
5392 
5393 
5394    IF (FND_LOG.LEVEL_EVENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5395      FND_LOG.STRING(FND_LOG.LEVEL_EVENT, L_DEBUG_KEY, 'About to call csi_t_txn_details_grp.create_transaction_dtls.');
5396    END IF;
5397    csi_t_txn_details_grp.create_transaction_dtls(
5398       p_api_version           => 1.0,
5399       p_commit                => p_commit,
5400       -- Changed by jaramana on January 14, 2008 for the Requisition ER 6034236
5401       p_init_msg_list         => FND_API.G_FALSE, --p_init_msg_list,
5402       px_txn_line_rec         => l_txn_line_rec,
5403       px_txn_line_detail_tbl  => l_txn_line_dtl_tbl,
5404       px_txn_party_detail_tbl => l_txn_party_tbl,
5405       px_txn_pty_acct_detail_tbl  => l_txn_pty_acct_tbl,
5406       px_txn_ii_rltns_tbl     => l_txn_ii_reln_tbl,
5407       px_txn_org_assgn_tbl    => l_txn_org_assgn_tbl,
5408       px_txn_ext_attrib_vals_tbl  => l_txn_ext_attrib_vals_tbl,
5409       px_txn_systems_tbl      => l_txn_systems_tbl,
5410       x_return_status         => x_return_status,
5411       x_msg_count             => x_msg_count,
5412       x_msg_data              => x_msg_data);
5413 
5414 
5415    IF (FND_LOG.LEVEL_EVENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5416      FND_LOG.STRING(FND_LOG.LEVEL_EVENT, L_DEBUG_KEY, 'Returned from call to csi_t_txn_details_grp.create_transaction_dtls. x_return_status = ' || x_return_status);
5417    END IF;
5418   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5419     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'Exiting Procedure');
5420   END IF;
5421 
5422 END Create_IB_Transaction;
5423 
5424 
5425 -- Delete IB sub-txn for the oe line.
5426 PROCEDURE Delete_IB_Transaction(
5427     p_init_msg_list          IN            VARCHAR2  := FND_API.G_FALSE,
5428     p_commit                 IN            VARCHAR2  := FND_API.G_FALSE,
5429     p_validation_level       IN            NUMBER    := FND_API.G_VALID_LEVEL_FULL,
5430     x_return_status          OUT NOCOPY            VARCHAR2,
5431     x_msg_count              OUT NOCOPY            NUMBER,
5432     x_msg_data               OUT NOCOPY            VARCHAR2,
5433     p_oe_line_id             IN            NUMBER)
5434 
5435 IS
5436 
5437   CURSOR csi_txn_lines_csr (p_oe_line_id IN  NUMBER) IS
5438     SELECT transaction_line_id
5439     FROM csi_t_transaction_lines
5440     WHERE SOURCE_TRANSACTION_ID = p_oe_line_id
5441     -- 3/3/03: Corrected to include txn table
5442     AND SOURCE_TRANSACTION_TABLE = G_TRANSACTION_TABLE;
5443 
5444   l_transaction_line_id  NUMBER;
5445 
5446   L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.Delete_IB_Transaction';
5447 
5448 BEGIN
5449 
5450    IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5451      FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Entering Procedure');
5452    END IF;
5453 
5454    -- Initialize return status.
5455    x_return_status := FND_API.G_RET_STS_SUCCESS;
5456 
5457   OPEN csi_txn_lines_csr (p_oe_line_id);
5458   FETCH csi_txn_lines_csr INTO l_transaction_line_id;
5459   CLOSE csi_txn_lines_csr;
5460 
5461   IF (l_transaction_line_id IS NOT NULL) THEN
5462 
5463     IF (FND_LOG.LEVEL_EVENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5464       FND_LOG.STRING(FND_LOG.LEVEL_EVENT, L_DEBUG_KEY, 'About to call csi_t_txn_details_grp.delete_transaction_dtls.');
5465     END IF;
5466 
5467     csi_t_txn_details_grp.delete_transaction_dtls
5468        ( p_api_version           => 1.0,
5469          p_commit                => p_commit,
5470          -- Changed by jaramana on January 14, 2008 for the Requisition ER 6034236
5471          p_init_msg_list         => FND_API.G_FALSE,--p_init_msg_list,
5472          p_validation_level      => p_validation_level,
5473          p_transaction_line_id   => l_transaction_line_id,
5474          x_return_status         => x_return_status,
5475          x_msg_count             => x_msg_count,
5476          x_msg_data              => x_msg_data);
5477 
5478     IF (FND_LOG.LEVEL_EVENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5479       FND_LOG.STRING(FND_LOG.LEVEL_EVENT, L_DEBUG_KEY, 'Returned from call to csi_t_txn_details_grp.delete_transaction_dtls. x_return_status = ' || x_return_status);
5480     END IF;
5481 
5482   END IF;
5483 
5484   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5485     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'Exiting Procedure');
5486   END IF;
5487 
5488 END Delete_IB_Transaction;
5489 
5490 -------------------------------------------------------------
5491 -- Start of Comments --
5492 --  Procedure name    : Convert_SubTxn_Type
5493 --  Type              : Public
5494 --  Function          : API to delete OSP shipment return lines and change IB transaction
5495 --                      sub types for ship-only lines while converting an OSP Order from
5496 --                      Exchange to Service type or vice versa.
5497 --  Pre-reqs    :
5498 --  Parameters  :
5499 --
5500 --  Standard IN  Parameters :
5501 --      p_api_version                   IN      NUMBER       Default  1.0
5502 --      p_init_msg_list                 IN      VARCHAR2     Default  FND_API.G_FALSE
5503 --      p_commit                        IN      VARCHAR2     Default  FND_API.G_FALSE
5504 --      p_validation_level              IN      NUMBER       Default  FND_API.G_VALID_LEVEL_FULL
5505 --      p_default                       IN      VARCHAR2     Default  FND_API.G_TRUE
5506 --      p_module_type                   IN      VARCHAR2     Default  NULL.
5507 --  Standard OUT Parameters :
5508 --      x_return_status                 OUT NOCOPY     VARCHAR2             Required
5509 --      x_msg_count                     OUT NOCOPY     NUMBER               Required
5510 --      x_msg_data                      OUT NOCOPY     VARCHAR2             Required
5511 --
5512 --  Convert_SubTxn_Type Parameters:
5513 --       p_osp_order_id          IN NUMBER
5514 --         The header_id for the OSP Order that is going through a type change
5515 --       p_old_order_type_code   IN VARCHAR2(30)
5516 --         The old type of the OSP Order. Can be SERVICE or EXCHANGE only
5517 --       p_new_order_type_code   IN VARCHAR2(30)
5518 --         The new type of the OSP Order. Can be EXCHANGE or SERVICE only.
5519 --
5520 --
5521 --  Version :
5522 --               Initial Version   1.0
5523 --
5524 --  End of Comments.
5525 
5526 PROCEDURE Convert_SubTxn_Type (
5527     p_api_version           IN        NUMBER    := 1.0,
5528     p_init_msg_list         IN        VARCHAR2  := FND_API.G_FALSE,
5529     p_commit                IN        VARCHAR2  := FND_API.G_FALSE,
5530     p_validation_level      IN        NUMBER    := FND_API.G_VALID_LEVEL_FULL,
5531     p_default               IN        VARCHAR2  := FND_API.G_TRUE,
5532     p_module_type           IN        VARCHAR2  := NULL,
5533     p_osp_order_id          IN        NUMBER,
5534     p_old_order_type_code   IN        VARCHAR2,
5535     p_new_order_type_code   IN        VARCHAR2,
5536     x_return_status         OUT NOCOPY           VARCHAR2,
5537     x_msg_count             OUT NOCOPY           NUMBER,
5538     x_msg_data              OUT NOCOPY           VARCHAR2) IS
5539 
5540   CURSOR validate_osp_id_csr (p_osp_order_id IN  NUMBER) IS
5541     SELECT 'X' from ahl_osp_orders_b where
5542     OSP_ORDER_ID = p_osp_order_id and
5543     status_code <> AHL_OSP_ORDERS_PVT.G_OSP_CLOSED_STATUS and
5544     ORDER_TYPE_CODE IN (AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_SERVICE,
5545                         AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_EXCHANGE);
5546 
5547   CURSOR get_return_line_ids_csr (p_osp_order_id IN  NUMBER) IS
5548     --mpothuku added distinct on 13-Feb-2007 for implementing the Osp Receiving feature.
5549     --If there are multiple services for the same physical item, the following query will return the same return line id multiple
5550     --times, and this leads to calls to Delete_Cancel_Order multiple times leading to a run-time error.
5551     SELECT DISTINCT OSPL.OE_RETURN_LINE_ID, OE.shipped_quantity, OE.booked_flag
5552     from ahl_osp_order_lines OSPL, oe_order_lines_all OE
5553     where OSPL.osp_order_id = p_osp_order_id AND
5554     OSPL.OE_RETURN_LINE_ID IS NOT NULL AND
5555     OE.line_id = OSPL.OE_RETURN_LINE_ID;
5556 
5557   l_api_name       CONSTANT VARCHAR2(30) := 'Convert_SubTxn_Type';
5558   l_api_version    CONSTANT NUMBER       := 1.0;
5559   L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.Convert_SubTxn_Type';
5560   l_dummy          VARCHAR2(1);
5561   l_oe_return_line_id NUMBER;
5562   l_shipped_quantity  NUMBER;
5563   l_booked_flag       VARCHAR2(1);
5564   l_oe_line_ids_tbl   SHIP_ID_TBL_TYPE;
5565   l_temp_count        NUMBER := 0;
5566 
5567 BEGIN
5568 
5569   -- Standard start of API savepoint
5570   SAVEPOINT Convert_SubTxn_Type_Pub;
5571 
5572   -- Initialize API return status to success
5573   x_return_status := FND_API.G_RET_STS_SUCCESS;
5574 
5575   -- Standard call to check for call compatibility
5576   IF NOT FND_API.Compatible_API_Call(l_api_version, p_api_version, l_api_name,
5577                                      G_PKG_NAME) THEN
5578     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5579   END IF;
5580 
5581     -- Initialize message list if p_init_msg_list is set to TRUE
5582   IF FND_API.To_Boolean(p_init_msg_list) THEN
5583     FND_MSG_PUB.Initialize;
5584   END IF;
5585 
5586   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5587     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Entering Procedure');
5588   END IF;
5589 
5590   -- Validate the OSP Order Id
5591   OPEN validate_osp_id_csr(p_osp_order_id);
5592   FETCH validate_osp_id_csr INTO l_dummy;
5593   IF (validate_osp_id_csr%NOTFOUND) THEN
5594     CLOSE validate_osp_id_csr;
5595     IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
5596        Fnd_Message.set_name('AHL', 'AHL_OSP_HEADER_ID_INV');
5597        Fnd_Msg_Pub.ADD;
5598     END IF;
5599     RAISE Fnd_Api.g_exc_error;
5600   END IF;
5601   CLOSE validate_osp_id_csr;
5602 
5603   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5604     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'OSP Id validated.');
5605   END IF;
5606 
5607   -- Validate the type codes
5608   IF ((p_old_order_type_code IS NULL) OR
5609      (p_new_order_type_code IS NULL) OR
5610      (p_old_order_type_code = p_new_order_type_code) OR
5611      (p_old_order_type_code NOT IN (AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_SERVICE,
5612                                     AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_EXCHANGE)) OR
5613      (p_new_order_type_code NOT IN (AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_SERVICE,
5614                                     AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_EXCHANGE))) THEN
5615     IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
5616       Fnd_Message.set_name('AHL', 'AHL_OSP_INV_TYPE_CHANGE');
5617       Fnd_Msg_Pub.ADD;
5618     IF (FND_LOG.LEVEL_ERROR >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5619       FND_LOG.MESSAGE(FND_LOG.LEVEL_ERROR, L_DEBUG_KEY, FALSE);
5620     END IF;
5621     END IF;
5622     RAISE Fnd_Api.g_exc_error;
5623   END IF;
5624 
5625   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5626     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Validated Order type codes.');
5627   END IF;
5628 
5629   -- Validate that the return quantity is zero
5630   OPEN get_return_line_ids_csr(p_osp_order_id);
5631   LOOP
5632     FETCH get_return_line_ids_csr INTO l_oe_return_line_id,
5633                                        l_shipped_quantity,
5634                                        l_booked_flag;
5635     EXIT WHEN get_return_line_ids_csr%NOTFOUND;
5636     IF (l_shipped_quantity IS NOT NULL AND
5637         l_shipped_quantity > 0) THEN
5638       -- Return line is already shipped: Too late to convert!
5639       IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
5640         Fnd_Message.set_name('AHL', 'AHL_OSP_ALREADY_SHIPPED');
5641         Fnd_Msg_Pub.ADD;
5642         IF (FND_LOG.LEVEL_ERROR >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5643           FND_LOG.MESSAGE(FND_LOG.LEVEL_ERROR, L_DEBUG_KEY, FALSE);
5644         END IF;
5645       END IF;
5646       CLOSE get_return_line_ids_csr;
5647       RAISE Fnd_Api.g_exc_error;
5648     END IF;
5649     -- Return line not yet shipped: Prepare to delete it
5650     l_temp_count := l_temp_count + 1;
5651     l_oe_line_ids_tbl(l_temp_count) := l_oe_return_line_id;
5652   END LOOP;
5653   CLOSE get_return_line_ids_csr;
5654 
5655   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5656     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Validated return quantities.');
5657   END IF;
5658 
5659   IF (l_temp_count > 0) THEN
5660     -- Delete all return shipment lines
5661     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5662       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'About to call Delete_Cancel_Order.');
5663     END IF;
5664 
5665     Delete_Cancel_Order (p_api_version      => 1.0,
5666                          p_init_msg_list    => FND_API.G_FALSE,
5667                          p_commit           => FND_API.G_FALSE,
5668                          p_validation_level => FND_API.G_VALID_LEVEL_FULL,
5669                          p_oe_header_id     => NULL, -- Don't delete the shipment header!
5670                          p_oe_lines_tbl     => l_oe_line_ids_tbl,  -- Delete only the return lines
5671                          p_cancel_flag      => FND_API.G_FALSE,
5672                          x_return_status    => x_return_status,
5673                          x_msg_count        => x_msg_count,
5674                          x_msg_data         => x_msg_data);
5675     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5676       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Delete_Cancel_Order: x_return_status = ' || x_return_status);
5677     END IF;
5678   ELSE
5679     null;
5680     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5681       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Not calling Delete_Cancel_Order since there are no return shipment lines');
5682     END IF;
5683   END IF;
5684 
5685   -- If the deletion has succeeded, proceed with converting the
5686   -- Ship only lines
5687   IF (x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
5688     Convert_Ship_Only_Lines(p_osp_order_id        => p_osp_order_id,
5689                             p_old_order_type_code => p_old_order_type_code,
5690                             p_new_order_type_code => p_new_order_type_code);
5691     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5692       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Convert_Ship_Only_Lines returned with no exception');
5693     END IF;
5694   END IF;
5695 
5696   -- Standard check of p_commit
5697   IF FND_API.To_Boolean(p_commit) THEN
5698     COMMIT WORK;
5699   END IF;
5700 
5701   -- Standard call to get message count and if count is 1, get message
5702   FND_MSG_PUB.Count_And_Get
5703     ( p_count => x_msg_count,
5704       p_data  => x_msg_data,
5705       p_encoded => fnd_api.g_false);
5706 
5707   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5708     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'Exiting Procedure');
5709   END IF;
5710 
5711 EXCEPTION
5712  WHEN FND_API.G_EXC_ERROR THEN
5713    x_return_status := FND_API.G_RET_STS_ERROR;
5714    Rollback to Convert_SubTxn_Type_Pub;
5715    FND_MSG_PUB.count_and_get( p_count => x_msg_count,
5716                               p_data  => x_msg_data,
5717                               p_encoded => fnd_api.g_false);
5718 
5719  WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5720    x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5721    Rollback to Convert_SubTxn_Type_Pub;
5722    FND_MSG_PUB.count_and_get( p_count => x_msg_count,
5723                               p_data  => x_msg_data,
5724                               p_encoded => fnd_api.g_false);
5725  WHEN OTHERS THEN
5726     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5727     Rollback to Convert_SubTxn_Type_Pub;
5728     fnd_msg_pub.add_exc_msg(p_pkg_name       => G_PKG_NAME,
5729                             p_procedure_name => 'Convert_SubTxn_Type',
5730                             p_error_text     => SQLERRM);
5731 
5732     FND_MSG_PUB.count_and_get( p_count => x_msg_count,
5733                                p_data  => x_msg_data,
5734                                p_encoded => fnd_api.g_false);
5735 END Convert_SubTxn_Type;
5736 
5737 
5738 -- Local helper method that deletes IB sub transactions for ship only lines
5739 -- and creates new ones based on the new OSP Order type.
5740 -- This method also changes the ownership if the shipment has already occurred.
5741 PROCEDURE Convert_Ship_Only_Lines(
5742     p_osp_order_id        IN         NUMBER,
5743     p_old_order_type_code IN         VARCHAR2,
5744     p_new_order_type_code IN         VARCHAR2) IS
5745 
5746   CURSOR get_ship_only_lines_csr (p_osp_order_id IN  NUMBER) IS
5747   --Modified by mpothuku on 21-Feb-06 to fix the Perf Bug #4919255
5748   /*
5749     SELECT OSPL.OE_SHIP_LINE_ID,
5750            ospl.item_instance_id,
5751            --wo.item_instance_id, --Jeli on 01/24/2006 for ER 4746426
5752            NVL(OE.shipped_quantity, 0)
5753     from ahl_osp_order_lines_v OSPL, oe_order_lines_all OE --, AHL_WORKORDERS_OSP_V wo
5754     where OSPL.osp_order_id = p_osp_order_id AND
5755     --ospl.workorder_id = wo.workorder_id AND
5756     OSPL.OE_SHIP_LINE_ID IS NOT NULL AND
5757     OE.line_id = OSPL.OE_SHIP_LINE_ID;
5758   */
5759     SELECT OSPL.OE_SHIP_LINE_ID,
5760            decode(wo.workorder_id, null, csii.instance_id, vts.instance_id) item_instance_id,
5761            NVL(OE.shipped_quantity, 0)
5762       from ahl_osp_order_lines OSPL,
5763            oe_order_lines_all OE,
5764            ahl_workorders wo,
5765            ahl_visit_tasks_b vts,
5766            csi_item_instances csii
5767      where OSPL.osp_order_id = p_osp_order_id
5768        AND OSPL.OE_SHIP_LINE_ID IS NOT NULL
5769        AND OE.line_id = OSPL.OE_SHIP_LINE_ID
5770        AND wo.workorder_id(+) = ospl.workorder_id
5771        AND wo.visit_task_id = vts.visit_task_id(+)
5772        AND csii.last_vld_organization_id(+) = ospl.inventory_org_id
5773        AND csii.inventory_item_id(+) = ospl.inventory_item_id
5774        AND csii.serial_number(+) = ospl.serial_number;
5775 
5776   CURSOR get_internal_party_csr IS
5777     SELECT internal_party_id from csi_install_parameters;
5778 
5779   CURSOR get_owner_ip_id(p_instance_id IN NUMBER) IS
5780     SELECT INSTANCE_PARTY_ID, PARTY_ID, OBJECT_VERSION_NUMBER from csi_i_parties where
5781     INSTANCE_ID = p_instance_id AND
5782     relationship_type_code = 'OWNER' AND
5783     NVL(ACTIVE_START_DATE, SYSDATE - 1) <= SYSDATE AND
5784     NVL(ACTIVE_END_DATE, SYSDATE + 1) >= SYSDATE;
5785 
5786   CURSOR get_sold_to_org(p_oe_line_id IN NUMBER) IS
5787     SELECT HZ.PARTY_ID, HZ.CUST_ACCOUNT_ID from HZ_CUST_ACCOUNTS HZ,
5788                             OE_ORDER_HEADERS_ALL OE,
5789                             oe_order_lines_all OEL
5790     WHERE OEL.line_id = p_oe_line_id AND
5791           OE.HEADER_ID = OEL.HEADER_ID AND
5792           HZ.CUST_ACCOUNT_ID = OE.SOLD_TO_ORG_ID;
5793 
5794 
5795   l_oe_ship_line_id   NUMBER;
5796   l_shipped_quantity  NUMBER;
5797   l_item_instance_id  NUMBER;
5798   l_temp_count        NUMBER := 0;
5799   l_internal_party_id NUMBER;
5800   l_curr_inst_pty_id  NUMBER;
5801   l_curr_pty_id       NUMBER;
5802   l_new_party_id      NUMBER;
5803   l_new_party_acc_id  NUMBER;
5804   l_party_ovn         NUMBER;
5805   l_party_tbl         CSI_DATASTRUCTURES_PUB.PARTY_TBL;
5806   l_party_rec         CSI_DATASTRUCTURES_PUB.PARTY_REC;
5807   l_party_account_tbl CSI_DATASTRUCTURES_PUB.PARTY_ACCOUNT_TBL;
5808   l_party_account_rec CSI_DATASTRUCTURES_PUB.PARTY_ACCOUNT_REC;
5809   l_transaction_rec   CSI_DATASTRUCTURES_PUB.TRANSACTION_REC;
5810   l_instance_rec          csi_datastructures_pub.instance_rec;
5811   l_ext_attrib_values_tbl csi_datastructures_pub.extend_attrib_values_tbl;
5812   l_pricing_attrib_tbl    csi_datastructures_pub.pricing_attribs_tbl;
5813   l_org_assignments_tbl   csi_datastructures_pub.organization_units_tbl;
5814   l_asset_assignment_tbl  csi_datastructures_pub.instance_asset_tbl;
5815   l_instance_id_lst       csi_datastructures_pub.id_tbl;
5816 
5817   L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.Convert_Ship_Only_Lines';
5818 
5819   l_return_status     VARCHAR2(1);
5820   l_msg_count         NUMBER;
5821   l_msg_data          VARCHAR2(2000);
5822 
5823 BEGIN
5824   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5825     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Entering Procedure');
5826   END IF;
5827   -- Get internal Party Id
5828   OPEN get_internal_party_csr;
5829   FETCH get_internal_party_csr INTO l_internal_party_id;
5830   IF (get_internal_party_csr%NOTFOUND) THEN
5831     IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
5832       Fnd_Message.set_name('AHL', 'AHL_OSP_CANT_GET_INT_PARTY');
5833       Fnd_Msg_Pub.ADD;
5834       IF (FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5835         FND_LOG.MESSAGE(FND_LOG.LEVEL_UNEXPECTED, L_DEBUG_KEY, FALSE);
5836       END IF;
5837     END IF;
5838     CLOSE get_internal_party_csr;
5839     RAISE Fnd_Api.g_exc_error;
5840   END IF;
5841   CLOSE get_internal_party_csr;
5842 
5843   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5844     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Got Internal Party Id: ' || l_internal_party_id);
5845   END IF;
5846 
5847   -- Process ship only lines
5848   OPEN get_ship_only_lines_csr(p_osp_order_id);
5849   LOOP
5850     FETCH get_ship_only_lines_csr INTO l_oe_ship_line_id,
5851                                        l_item_instance_id,
5852                                        l_shipped_quantity;
5853     EXIT WHEN get_ship_only_lines_csr%NOTFOUND;
5854     l_temp_count := l_temp_count + 1;
5855     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5856       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'index = ' || l_temp_count || ', l_oe_ship_line_id = ' || l_oe_ship_line_id || ', l_shipped_quantity = ' || l_shipped_quantity || ', l_item_instance_id = ' || l_item_instance_id);
5857     END IF;
5858     --IF (l_shipped_quantity > 0) THEN --Jeli on 01/24/2006 for ER47
5859     IF (l_shipped_quantity > 0 AND l_item_instance_id IS NOT NULL) THEN
5860       -- Already shipped: Need to change ownership
5861       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5862         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Since l_shipped_quantity > 0, need to change owner');
5863       END IF;
5864       OPEN get_owner_ip_id(l_item_instance_id);
5865       FETCH get_owner_ip_id INTO l_curr_inst_pty_id,
5866                                  l_curr_pty_id,
5867                                  l_party_ovn;
5868       IF (get_owner_ip_id%NOTFOUND) THEN
5869         IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
5870           Fnd_Message.set_name('AHL', 'AHL_OSP_CANT_GET_CURR_PARTY');
5871           FND_MESSAGE.SET_TOKEN('INSTANCE_NUMBER', GET_INSTANCE_NUM_FROM_ID(l_item_instance_id));
5872           Fnd_Msg_Pub.ADD;
5873           IF (FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5874             FND_LOG.MESSAGE(FND_LOG.LEVEL_UNEXPECTED, L_DEBUG_KEY, FALSE);
5875           END IF;
5876         END IF;
5877         CLOSE get_owner_ip_id;
5878         CLOSE get_ship_only_lines_csr;
5879         RAISE Fnd_Api.g_exc_error;
5880       END IF;
5881       CLOSE get_owner_ip_id;
5882       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5883         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Got current owner, l_curr_pty_id = ' || l_curr_pty_id || ', l_curr_inst_pty_id = ' || l_curr_inst_pty_id);
5884       END IF;
5885 
5886       IF (p_new_order_type_code = AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_SERVICE) THEN
5887         -- Exchange to Service conversion: New owner will be internal
5888         l_new_party_id := l_internal_party_id;
5889         -- l_new_party_acc_id will be null
5890         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5891           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Exchange to Service conversion: New internal owner = ' || l_new_party_id);
5892         END IF;
5893       ELSIF (p_new_order_type_code = AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_EXCHANGE) THEN
5894         -- Service to Exchange conversion: New owner will be the ship to org
5895         -- Determine the id of this party
5896         OPEN get_sold_to_org(l_oe_ship_line_id);
5897         FETCH get_sold_to_org INTO l_new_party_id, l_new_party_acc_id;
5898         IF (get_sold_to_org%NOTFOUND) THEN
5899           IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
5900             Fnd_Message.set_name('AHL', 'AHL_OSP_CANT_GET_NEW_PARTY');
5901             FND_MESSAGE.SET_TOKEN('INSTANCE_NUMBER', GET_INSTANCE_NUM_FROM_ID(l_item_instance_id));
5902             Fnd_Msg_Pub.ADD;
5903             IF (FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5904               FND_LOG.MESSAGE(FND_LOG.LEVEL_UNEXPECTED, L_DEBUG_KEY, FALSE);
5905             END IF;
5906           END IF;
5907           CLOSE get_sold_to_org;
5908           CLOSE get_ship_only_lines_csr;
5909           RAISE Fnd_Api.g_exc_error;
5910         END IF;
5911         CLOSE get_sold_to_org;
5912         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5913           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Service to Exchange conversion: New external owner (party) = ' || l_new_party_id || ', New Party Account = ' || l_new_party_acc_id);
5914         END IF;
5915       ELSE
5916         IF Fnd_Msg_Pub.check_msg_level(Fnd_Msg_Pub.g_msg_lvl_error) THEN
5917           Fnd_Message.set_name('AHL', 'AHL_OSP_INV_TYPE_CHANGE');
5918           Fnd_Msg_Pub.ADD;
5919           IF (FND_LOG.LEVEL_ERROR >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5920             FND_LOG.MESSAGE(FND_LOG.LEVEL_ERROR, L_DEBUG_KEY, FALSE);
5921           END IF;
5922         END IF;
5923         CLOSE get_ship_only_lines_csr;
5924         RAISE Fnd_Api.g_exc_error;
5925       END IF;
5926       -- Call CSI API CSI_ITEM_INSTANCE_PUB.UPDATE_ITEM_INSTANCE
5927       -- Populate the Party Rec
5928       l_party_rec.instance_id := l_item_instance_id;
5929       l_party_rec.party_source_table := 'HZ_PARTIES';
5930       l_party_rec.party_id := l_new_party_id;
5931       l_party_rec.relationship_type_code := 'OWNER';
5932       l_party_rec.instance_party_id := l_curr_inst_pty_id;
5933       l_party_rec.object_version_number := l_party_ovn;
5934       l_party_tbl(1) := l_party_rec;
5935       -- Populate the Party Account Rec only if new Party is external
5936       IF (l_new_party_id <> l_internal_party_id) THEN
5937         l_party_account_rec.instance_party_id := l_curr_inst_pty_id;
5938         l_party_account_rec.party_account_id := l_new_party_acc_id;
5939         l_party_account_rec.relationship_type_code := 'OWNER';
5940         l_party_account_rec.call_contracts := FND_API.G_FALSE;
5941         l_party_account_rec.parent_tbl_index := 1;
5942         l_party_account_tbl(1) := l_party_account_rec;
5943       END IF;
5944       -- Populate the Transaction Rec
5945       l_transaction_rec.transaction_date := sysdate;
5946       l_transaction_rec.source_transaction_date := sysdate;
5947       l_transaction_rec.transaction_type_id := 1;
5948       -- Call the API
5949       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5950         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'About to call CSI_ITEM_INSTANCE_PUB.UPDATE_ITEM_INSTANCE');
5951 
5952         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_party_rec:');
5953         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  instance_id = ' || l_party_rec.instance_id);
5954         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  party_source_table = ' || l_party_rec.party_source_table);
5955         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  party_id = ' || l_party_rec.party_id);
5956         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  relationship_type_code = ' || l_party_rec.relationship_type_code);
5957         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  instance_party_id = ' || l_party_rec.instance_party_id);
5958         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  object_version_number = ' || l_party_rec.object_version_number);
5959 
5960         IF (l_new_party_id <> l_internal_party_id) THEN
5961           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_party_account_rec:');
5962           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  instance_party_id = ' || l_party_account_rec.instance_party_id);
5963           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  party_account_id = ' || l_party_account_rec.party_account_id);
5964           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  relationship_type_code = ' || l_party_account_rec.relationship_type_code);
5965           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, '  call_contracts = ' || l_party_account_rec.call_contracts);
5966         END IF;
5967       END IF;
5968 
5969 --Updated by Jerry on 07/26/05 after the discussion with CSI Srini. From his suggestion,
5970 --replacing the call to CSI_PARTY_RELATIONSHIPS_PUB.UPDATE_INST_PARTY_RELATIONSHIP with
5971 -- CSI_ITEM_INSTANCE_PUB.UPDATE_ITEM_INSTANCE, and keep the original existing parameter i
5972 --(no change) and just adding some new parameters with blank values.
5973       CSI_ITEM_INSTANCE_PUB.UPDATE_ITEM_INSTANCE(
5974           p_api_version           => 1.0,
5975           p_commit                => FND_API.G_FALSE,
5976           p_init_msg_list         => FND_API.G_FALSE,
5977           p_validation_level      => FND_API.G_VALID_LEVEL_FULL,
5978           p_instance_rec          => l_instance_rec,
5979           p_ext_attrib_values_tbl => l_ext_attrib_values_tbl,
5980           p_party_tbl             => l_party_tbl,
5981           p_account_tbl           => l_party_account_tbl,
5982           p_pricing_attrib_tbl    => l_pricing_attrib_tbl,
5983           p_org_assignments_tbl   => l_org_assignments_tbl,
5984           p_asset_assignment_tbl  => l_asset_assignment_tbl,
5985           p_txn_rec               => l_transaction_rec,
5986           x_instance_id_lst       => l_instance_id_lst,
5987           x_return_status         => l_return_status,
5988           x_msg_count             => l_msg_count,
5989           x_msg_data              => l_msg_data);
5990       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5991         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Returned from CSI_ITEM_INSTANCE_PUB.UPDATE_ITEM_INSTANCE, x_return_status = ' || l_return_status);
5992       END IF;
5993       IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
5994         CLOSE get_ship_only_lines_csr;
5995         RAISE Fnd_Api.g_exc_error;
5996       END IF;
5997     ELSE
5998       -- Not yet shipped: No need to change ownership
5999       -- Delete existing IB subtransaction (with old type)
6000       -- Jeli added the instance_id nullable check on 01/24/2006 for ER 4746426
6001       IF (l_item_instance_id IS NULL) THEN
6002         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6003           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Non-tracked item, so without IB transaction deletion or creation');
6004         END IF;
6005       ELSE
6006         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6007           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Since l_shipped_quantity = 0, No need to change owner. Calling Delete_IB_Transaction.');
6008         END IF;
6009         Delete_IB_Transaction(
6010           p_init_msg_list    => FND_API.G_FALSE,
6011           p_commit           => FND_API.G_FALSE,
6012           p_validation_level => FND_API.G_VALID_LEVEL_FULL,
6013           x_return_status    => l_return_status,
6014           x_msg_count        => l_msg_count,
6015           x_msg_data         => l_msg_data,
6016           p_oe_line_id       => l_oe_ship_line_id);
6017         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6018           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Returned from Delete_IB_Transaction, x_return_status = ' || l_return_status);
6019         END IF;
6020         IF (l_return_status = FND_API.G_RET_STS_SUCCESS) THEN
6021           -- Create new IB subtransaction (with new type)
6022           Create_IB_Transaction(
6023             p_init_msg_list    => FND_API.G_FALSE,
6024             p_commit           => FND_API.G_FALSE,
6025             p_validation_level => FND_API.G_VALID_LEVEL_FULL,
6026             x_return_status    => l_return_status,
6027             x_msg_count        => l_msg_count,
6028             x_msg_data         => l_msg_data,
6029             p_OSP_order_type   => p_new_order_type_code,
6030             p_oe_line_type     => 'ORDER',
6031             p_oe_line_id       => l_oe_ship_line_id,
6032             p_csi_instance_id  => l_item_instance_id);
6033           IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6034             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Returned from Create_IB_Transaction, x_return_status = ' || l_return_status);
6035           END IF;
6036           IF (l_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
6037             -- Error creating IB Subtransaction: throw exception
6038             CLOSE get_ship_only_lines_csr;
6039             RAISE Fnd_Api.g_exc_error;
6040           END IF;
6041         ELSE
6042           -- Error deleting IB Subtransaction: throw exception
6043           CLOSE get_ship_only_lines_csr;
6044           RAISE Fnd_Api.g_exc_error;
6045         END IF;
6046       END IF; --Jeli added this END IF; on 01/24/2006 for ER 4746426
6047     END IF;
6048   END LOOP;
6049   CLOSE get_ship_only_lines_csr;
6050 
6051   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6052     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'Exiting Procedure');
6053   END IF;
6054 
6055 END Convert_Ship_Only_Lines;
6056 
6057 FUNCTION GET_INSTANCE_NUM_FROM_ID(p_instance_id IN NUMBER )
6058 RETURN VARCHAR2
6059 IS
6060  CURSOR csi_instance_num_csr(p_instance_id IN NUMBER) IS
6061     SELECT INSTANCE_NUMBER
6062     FROM CSI_ITEM_INSTANCES
6063     WHERE INSTANCE_ID = p_instance_id;
6064 
6065   p_instance_num CSI_ITEM_INSTANCES.INSTANCE_NUMBER%TYPE;
6066   L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.GET_INSTANCE_NUM_FROM_ID';
6067 
6068 BEGIN
6069   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6070     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'GET_INSTANCE_NUM_FROM_ID: p_instance_id = ' || p_instance_id);
6071   END IF;
6072   IF p_instance_id IS NULL THEN
6073     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6074       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'GET_INSTANCE_NUM_FROM_ID: returning NULL');
6075     END IF;
6076     RETURN NULL;
6077   ELSE
6078     OPEN csi_instance_num_csr(p_instance_id);
6079     FETCH csi_instance_num_csr into p_instance_num;
6080     CLOSE csi_instance_num_csr;
6081     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6082       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'GET_INSTANCE_NUM_FROM_ID: returning ' || p_instance_num);
6083     END IF;
6084     RETURN p_instance_num;
6085   END IF;
6086 END;
6087 
6088 --FP Bug fix: 5380842 for the Customer Change when Vendor Changes: By mpothuku on 22nd August, 2006
6089 
6090 PROCEDURE Handle_Vendor_Change (
6091     p_api_version           IN        NUMBER    := 1.0,
6092     p_init_msg_list         IN        VARCHAR2  := FND_API.G_FALSE,
6093     p_commit                IN        VARCHAR2  := FND_API.G_FALSE,
6094     p_validation_level      IN        NUMBER    := FND_API.G_VALID_LEVEL_FULL,
6095     p_default               IN        VARCHAR2  := FND_API.G_TRUE,
6096     p_module_type           IN        VARCHAR2  := NULL,
6097     p_osp_order_id          IN        NUMBER,
6098     p_vendor_id             IN        NUMBER,
6099     p_vendor_loc_id         IN        NUMBER,
6100     x_return_status         OUT NOCOPY           VARCHAR2,
6101     x_msg_count             OUT NOCOPY           NUMBER,
6102     x_msg_data              OUT NOCOPY           VARCHAR2) AS
6103 
6104 
6105   l_api_name       CONSTANT VARCHAR2(30)  := 'Handle_Vendor_Change';
6106   l_api_version    CONSTANT NUMBER        := 1.0;
6107   L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.Handle_Vendor_Change';
6108 
6109     CURSOR get_shipment_det_csr
6110     IS
6111     SELECT OE_HEADER_ID
6112     FROM   AHL_OSP_ORDERS_B
6113     WHERE  OSP_ORDER_ID = p_osp_order_id;
6114 
6115     CURSOR check_vendor_site_valid_csr
6116     IS
6117     SELECT 1
6118     FROM   po_vendor_sites_all
6119     WHERE  VENDOR_SITE_ID =   p_vendor_loc_id
6120       AND  VENDOR_ID =  p_vendor_id;
6121 
6122     CURSOR check_cust_vendor_rel
6123     IS
6124     SELECT CUSTOMER_SITE_ID
6125     FROM   AHL_VENDOR_CUSTOMER_RELS
6126     WHERE VENDOR_SITE_ID = p_vendor_loc_id;
6127 
6128     CURSOR ahl_order_vendor_detls_csr(p_osp_order_id IN NUMBER) IS
6129     SELECT osp.vendor_id,
6130      osp.vendor_site_id,
6131      osp.vendor_contact_id,
6132      osp.osp_order_number,
6133      cust.customer_site_id,
6134      cust.customer_id
6135       FROM ahl_osp_orders_b osp,
6136            ahl_vendor_customer_rels_v cust
6137      WHERE osp.osp_order_id = p_osp_order_id
6138        AND osp.vendor_site_id = cust.vendor_site_id;
6139 
6140     CURSOR get_oe_order_lines(c_oe_header_id IN NUMBER) IS
6141     select line_id from oe_order_lines_all
6142      where header_id = c_oe_header_id;
6143 
6144     l_vendor_cust_dtls ahl_order_vendor_detls_csr%ROWTYPE;
6145     l_oe_header_id NUMBER;
6146     l_oe_line_id NUMBER;
6147 
6148    l_header_rec            OE_ORDER_PUB.header_rec_type;
6149    l_line_tbl              OE_ORDER_PUB.line_tbl_type;
6150    l_msg_data              VARCHAR2(2000);
6151    l_msg_index_out         NUMBER;
6152    l_index                 NUMBER;
6153    l_oe_line_rec           OE_ORDER_PUB.line_rec_type;
6154    l_count                 NUMBER := 0;
6155 
6156 BEGIN
6157 
6158   -- Standard start of API savepoint
6159   SAVEPOINT Handle_Vendor_Change;
6160 
6161   -- Initialize API return status to success
6162   x_return_status := FND_API.G_RET_STS_SUCCESS;
6163 
6164   -- Standard call to check for call compatibility
6165   IF NOT FND_API.Compatible_API_Call(l_api_version, p_api_version, l_api_name,
6166                                      G_PKG_NAME) THEN
6167     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6168   END IF;
6169 
6170   --Added by mpothuku on 11-Jul-06 to raise any validation errors that may have been accumulated by the AHL_OSP_ORDERS_PVT procedure
6171   --We do not have any warning messages hence if the message count is > 0 then it means there are validation errors and since
6172   --we call a Public API in this procedure, its better we throw the errore here itself.
6173 
6174   IF FND_MSG_PUB.count_msg > 0 THEN
6175      RAISE  FND_API.G_EXC_ERROR;
6176   END IF;
6177 
6178   --mpothuku End
6179 
6180   -- Initialize message list if p_init_msg_list is set to TRUE
6181   IF FND_API.To_Boolean(p_init_msg_list) THEN
6182     FND_MSG_PUB.Initialize;
6183   END IF;
6184 
6185   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6186     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Entering Procedure');
6187   END IF;
6188 
6189   IF p_osp_order_id IS NOT NULL THEN
6190     OPEN get_shipment_det_csr;
6191     FETCH get_shipment_det_csr INTO l_oe_header_id;
6192     IF get_shipment_det_csr%NOTFOUND THEN
6193     CLOSE get_shipment_det_csr;
6194     RETURN;
6195     END IF;
6196     CLOSE get_shipment_det_csr;
6197   END IF;
6198 
6199   --Added by mpothuku on 08-may-06 for fixing the Bug 5212130
6200   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6201      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'p_osp_order_id = ' || p_osp_order_id);
6202      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'p_vendor_id = ' || p_vendor_id);
6203      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'p_vendor_loc_id = ' || p_vendor_loc_id);
6204      FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_oe_header_id = ' || l_oe_header_id);
6205   END IF;
6206 
6207   OPEN ahl_order_vendor_detls_csr(p_osp_order_id);
6208   FETCH ahl_order_vendor_detls_csr INTO l_vendor_cust_dtls;
6209   CLOSE ahl_order_vendor_detls_csr;
6210 
6211   --Added by mpothuku on 08-may-06 for fixing the Bug 5212130
6212   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6213    FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_vendor_cust_dtls.vendor_id = ' || l_vendor_cust_dtls.vendor_id);
6214    FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_vendor_cust_dtls.vendor_site_id = ' || l_vendor_cust_dtls.vendor_site_id);
6215    FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_vendor_cust_dtls.customer_id = ' || l_vendor_cust_dtls.customer_id);
6216    FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_vendor_cust_dtls.customer_site_id = ' || l_vendor_cust_dtls.customer_site_id);
6217   END IF;
6218 
6219   IF l_vendor_cust_dtls.customer_site_id IS NULL THEN
6220       Fnd_Message.set_name('AHL', 'AHL_OSP_CUST_SETUP_NOTFOUND');
6221       Fnd_Msg_Pub.ADD;
6222       RAISE Fnd_Api.g_exc_error;
6223   END IF;
6224 
6225   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6226     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'About to OE_ORDER_GRP.PROCESS_ORDER');
6227   END IF;
6228 
6229   l_header_rec := OE_ORDER_PUB.G_MISS_HEADER_REC;
6230   l_line_tbl   := OE_ORDER_PUB.G_MISS_LINE_TBL;
6231 
6232   l_header_rec.header_id := l_oe_header_id;
6233   l_header_rec.operation := OE_GLOBALS.G_OPR_UPDATE;
6234   l_header_rec.sold_to_org_id := l_vendor_cust_dtls.customer_id;
6235   l_header_rec.ship_to_org_id := l_vendor_cust_dtls.customer_site_id;
6236 
6237   --Retrieve all the order lines
6238   --Fix for the customer site change related Bug 6521712 fix, by jaramana on January 14, 2008
6239   OPEN get_oe_order_lines(l_oe_header_id);
6240   LOOP
6241     FETCH get_oe_order_lines INTO l_oe_line_id;
6242     EXIT WHEN get_oe_order_lines%NOTFOUND;
6243     l_oe_line_rec := OE_ORDER_PUB.G_MISS_LINE_REC;
6244     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6245       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_oe_line_id = ' || l_oe_line_id);
6246     END IF;
6247     l_oe_line_rec.line_id := l_oe_line_id;
6248     l_oe_line_rec.sold_to_org_id := l_vendor_cust_dtls.customer_id;
6249     l_oe_line_rec.ship_to_org_id := l_vendor_cust_dtls.customer_site_id;
6250     l_oe_line_rec.operation := OE_GLOBALS.G_OPR_UPDATE;
6251     l_line_tbl(l_count) := l_oe_line_rec;
6252     l_count := l_count + 1;
6253   END LOOP;
6254   CLOSE get_oe_order_lines;
6255 
6256   --OE_ORDER_GRP uses its own message stack OE_MSG_PUB, so we should pass p_init_msg_list as true to this API
6257   --Note that this also does an FND_MSG_PUB.initialize along with clearing its own error stack
6258   OE_ORDER_GRP.PROCESS_ORDER(
6259     p_api_version_number  => 1.0,
6260     p_init_msg_list       => FND_API.G_TRUE,
6261     x_return_status       => x_return_status,
6262     x_msg_count           => x_msg_count,
6263     x_msg_data            => x_msg_data,
6264     p_header_rec          => l_header_rec,
6265     --p_header_val_rec      => x_header_val_rec,
6266     --Following line uncommented for the customer site change related Bug 6521712 fix, by jaramana on January 14, 2008
6267     p_line_tbl            => l_line_tbl,
6268     --p_line_val_tbl        => x_line_val_tbl,
6269     --p_lot_serial_tbl      => l_lot_serial_tbl,
6270     x_header_rec          => x_header_rec,
6271     x_header_val_rec      => x_header_val_rec,
6272     x_Header_Adj_tbl       =>  x_Header_Adj_tbl,
6273     x_Header_Adj_val_tbl   =>  x_Header_Adj_val_tbl,
6274     x_Header_price_Att_tbl =>  x_Header_price_Att_tbl,
6275     x_Header_Adj_Att_tbl   => x_Header_Adj_Att_tbl,
6276     x_Header_Adj_Assoc_tbl =>  x_Header_Adj_Assoc_tbl,
6277     x_Header_Scredit_tbl    =>   x_Header_Scredit_tbl,
6278     x_Header_Scredit_val_tbl =>    x_Header_Scredit_val_tbl,
6279     x_line_tbl               =>     x_line_tbl      ,
6280     x_line_val_tbl           =>    x_line_val_tbl ,
6281     x_Line_Adj_tbl           =>   x_Line_Adj_tbl    ,
6282     x_Line_Adj_val_tbl       =>  x_Line_Adj_val_tbl,
6283     x_Line_price_Att_tbl     =>   x_Line_price_Att_tbl,
6284     x_Line_Adj_Att_tbl       =>  x_Line_Adj_Att_tbl ,
6285     x_Line_Adj_Assoc_tbl     =>  x_Line_Adj_Assoc_tbl,
6286     x_Line_Scredit_tbl       => x_Line_Scredit_tbl ,
6287     x_Line_Scredit_val_tbl   =>  x_Line_Scredit_val_tbl,
6288     x_Lot_Serial_tbl         => x_Lot_Serial_tbl  ,
6289     x_Lot_Serial_val_tbl     => x_Lot_Serial_val_tbl   ,
6290     x_action_request_tbl     => x_action_request_tbl  );
6291 
6292 
6293   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6294     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Completed OE_ORDER_GRP.PROCESS_ORDER, x_return_status = ' || x_return_status);
6295   END IF;
6296 
6297   IF (x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
6298     FOR i IN 1..x_msg_count LOOP
6299       OE_MSG_PUB.Get(p_msg_index => i,
6300                      p_encoded => FND_API.G_FALSE,
6301                      p_data    => l_msg_data,
6302                      p_msg_index_out => l_msg_index_out);
6303       fnd_msg_pub.add_exc_msg(p_pkg_name       => 'OE_ORDER_PUB',
6304                               p_procedure_name => 'processOrder',
6305                               p_error_text     => substr(l_msg_data,1,240));
6306       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6307            FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'OE:Err Msg '||i||'.' || l_msg_data);
6308       END IF;
6309 
6310     END LOOP;
6311   END IF;
6312 
6313   /*
6314   x_msg_count := FND_MSG_PUB.count_msg;
6315 
6316   IF x_msg_count > 0 THEN
6317      RAISE  FND_API.G_EXC_ERROR;
6318   END IF;
6319   */
6320 
6321   IF (x_return_status = FND_API.G_RET_STS_ERROR) THEN
6322     RAISE FND_API.G_EXC_ERROR;
6323   ELSIF (x_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
6324     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6325   /*
6326   If success is returned by the Public API, clear the message stack to eat up any warning messages from OM API.
6327   Note that we may need to revise this approach at a later point to show the warning messages too
6328   If this is not done, AHL_OSP_ORDERS_PVT assumes that there is an error if the message stack count is > 0
6329   Also note that had there been any validation errors that had been accumulated we would have thrown at the beginning
6330   of this procedure
6331   */
6332 
6333   ELSIF(x_return_status = FND_API.G_RET_STS_SUCCESS) THEN
6334     --Added the message logging on January 14, 2008 to fix the Bug 5935388/6504122
6335     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6336       FOR i IN 1..FND_MSG_PUB.count_msg LOOP
6337         FND_MSG_PUB.get (
6338             p_msg_index      => i,
6339             p_encoded        => FND_API.G_FALSE,
6340             p_data           => l_msg_data,
6341             p_msg_index_out  => l_msg_index_out );
6342         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY,'OE: Warning ' || i || ': ' || l_msg_data);
6343       END LOOP;
6344     END IF;
6345     FND_MSG_PUB.Initialize;
6346   END IF;
6347 
6348   FND_MSG_PUB.Count_And_Get
6349     ( p_count => x_msg_count,
6350       p_data  => x_msg_data,
6351       p_encoded => fnd_api.g_false);
6352   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6353     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'Exiting Procedure');
6354   END IF;
6355 
6356 EXCEPTION
6357  WHEN FND_API.G_EXC_ERROR THEN
6358    x_return_status := FND_API.G_RET_STS_ERROR;
6359    Rollback to Handle_Vendor_Change;
6360    FND_MSG_PUB.count_and_get( p_count => x_msg_count,
6361                               p_data  => x_msg_data,
6362                               p_encoded => fnd_api.g_false);
6363 
6364  WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6365    x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6366    Rollback to Handle_Vendor_Change;
6367    FND_MSG_PUB.count_and_get( p_count => x_msg_count,
6368                               p_data  => x_msg_data,
6369                                p_encoded => fnd_api.g_false);
6370  WHEN OTHERS THEN
6371     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6372     Rollback to Handle_Vendor_Change;
6373        fnd_msg_pub.add_exc_msg(p_pkg_name       => G_PKG_NAME,
6374                                p_procedure_name => 'Handle_Vendor_Change',
6375                                p_error_text     => SQLERRM);
6376 
6377     FND_MSG_PUB.count_and_get( p_count => x_msg_count,
6378                                p_data  => x_msg_data,
6379                                 p_encoded => fnd_api.g_false);
6380 
6381 END;
6382 
6383 
6384 --Added by mpothuku on 01-Jun-2007 to support the part number and serial number change for osp lines
6385 ----------------------------------------------------------------------------------------------------
6386 -- FUNCTION
6387 --    Is_part_chg_valid_for_ospline
6388 --
6389 -- PURPOSE
6390 --    This function checks that the osp line is valid for part number/serial number change.
6391 --
6392 -- NOTES
6393 --    It returns 'N' if
6394 --       1. There is no ship line for the osp line
6395 --       2. Item is not IB tracked
6396 --       3. Line status is PO Deleted/PO Cancelled/Req Deleted/Req Cancelled
6397 --       4. Item is not shipped
6398 --       5. Item has already been received
6399 --    Otherwise it will return 'Y'
6400 -----------------------------------------------------------------------------------------------------
6401 
6402 FUNCTION Is_part_chg_valid_for_ospline(p_osp_order_line_id IN NUMBER)
6403 RETURN VARCHAR2
6404 IS
6405 
6406 CURSOR get_inventory_flags(c_inv_item_id IN NUMBER, c_inv_org_id IN NUMBER) IS
6407 SELECT serial_number_control_code,
6408        comms_nl_trackable_flag
6409   FROM mtl_system_items_b
6410  WHERE inventory_item_id = c_inv_item_id
6411    AND organization_id =  c_inv_org_id;
6412 
6413 CURSOR get_ship_line_instance(c_osp_order_line_id IN NUMBER) IS
6414 SELECT csi.instance_id,
6415        csi.inventory_item_id,
6416        csi.serial_number,
6417        csi.last_vld_organization_id
6418   FROM csi_t_transaction_lines tl,
6419        csi_t_txn_line_details tld,
6420        ahl_osp_order_lines ospl,
6421        csi_item_instances csi
6422  WHERE tl.source_transaction_id = ospl.oe_ship_line_id
6423    AND tl.source_transaction_table = G_TRANSACTION_TABLE
6424    AND tl.transaction_line_id = tld.transaction_line_id
6425    AND ospl.osp_order_line_id = c_osp_order_line_id
6426    AND tld.instance_id = csi.instance_id;
6427 
6428 CURSOR get_order_details(c_osp_order_line_id IN NUMBER) IS
6429 SELECT ospl.inventory_item_id,
6430        ospl.inventory_org_id,
6431        ospl.serial_number,
6432        ospl.oe_ship_line_id,
6433        nvl(oesh.shipped_quantity,0) shipped_quantity,
6434        ospl.oe_return_line_id,
6435        nvl(oert.shipped_quantity,0) returned_quantity,
6436        ospl.status_code osp_line_status_code,
6437        osph.status_code osp_header_status_code,
6438        osph.order_type_code
6439   FROM ahl_osp_order_lines ospl,
6440        ahl_osp_orders_b osph,
6441        oe_order_lines_all oesh,
6442        oe_order_lines_all oert
6443  WHERE ospl.osp_order_line_id = c_osp_order_line_id
6444    AND ospl.osp_order_id = osph.osp_order_id
6445    AND ospl.oe_ship_line_id = oesh.line_id(+)
6446    AND ospl.oe_return_line_id = oert.line_id(+);
6447 
6448  L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.Is_part_chg_valid_for_ospline';
6449  l_get_order_details get_order_details%ROWTYPE;
6450  l_serial_number_control_code mtl_system_items_b.serial_number_control_code%TYPE;
6451  l_comms_nl_trackable_flag mtl_system_items_b.comms_nl_trackable_flag%TYPE;
6452  l_serial_status NUMBER;
6453  l_instance_id NUMBER;
6454  l_dummy NUMBER;
6455  l_ship_line_instance get_ship_line_instance%ROWTYPE;
6456 
6457 BEGIN
6458 
6459   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6460     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Entering Function');
6461   END IF;
6462 
6463   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6464     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'p_osp_order_line_id: '||p_osp_order_line_id);
6465   END IF;
6466 
6467   --Retrieve the shipment line information. If data is not retrieved, then the osp_order_line_id is invalid.
6468   OPEN get_order_details(p_osp_order_line_id);
6469   FETCH get_order_details INTO l_get_order_details;
6470   IF (get_order_details%NOTFOUND) THEN
6471     CLOSE get_order_details;
6472     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6473       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Invalid Order Line, Failed to retrieve order line/shipment information');
6474     END IF;
6475     return 'N';
6476   END IF;
6477   CLOSE get_order_details;
6478 
6479   /*
6480   Check that the Order Type is Service and the status of the order is not 'CLOSED' and the status of the order line is not populated, which would mean that the related purchase line details have been cancelled or deleted.
6481   */
6482   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6483     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'order_type_code -> ' ||l_get_order_details.order_type_code ||
6484     'osp_header_status_code -> ' ||l_get_order_details.osp_header_status_code ||
6485     'osp_line_status_code -> ' ||l_get_order_details.osp_line_status_code);
6486   END IF;
6487 
6488   IF(l_get_order_details.order_type_code <> AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_SERVICE OR
6489      l_get_order_details.osp_header_status_code = AHL_OSP_ORDERS_PVT.G_OSP_CLOSED_STATUS OR
6490      l_get_order_details.osp_line_status_code is not NULL) THEN
6491     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6492       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Invalid Osp Order Type or Status');
6493     END IF;
6494     return 'N';
6495   END IF;
6496 
6497   /*
6498   Check that the onward shipment line is 'Shipped'. Note that if the line is cancelled, we do not want to allow the part number
6499   change for this order line.
6500   Return 'N' if, the quantity is not shipped yet.
6501   */
6502   IF(l_get_order_details.oe_ship_line_id is NULL OR l_get_order_details.shipped_quantity = 0) THEN
6503       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6504         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Ship Line not present or is not valid to allow Part Number Change: '||
6505         ' oe_ship_line_id ->' ||l_get_order_details.oe_ship_line_id ||
6506         ' shipped_quantity ->' ||l_get_order_details.shipped_quantity) ;
6507       END IF;
6508       return 'N';
6509   END IF;
6510 
6511   /*
6512   Check that the return shipment line is 'Open'. Note that if the line is cancelled, we will allow the Part Number change.
6513   Return 'N' if the return line is already shipped.
6514   */
6515   IF(l_get_order_details.oe_return_line_id is NOT NULL AND l_get_order_details.returned_quantity > 0) THEN
6516       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6517         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Return Line already shipped'||
6518         ' oe_return_line_id ->' ||l_get_order_details.oe_return_line_id ||
6519         ' returned_quantity ->' ||l_get_order_details.returned_quantity ) ;
6520       END IF;
6521       return 'N';
6522   END IF;
6523 
6524   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6525     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Item Details as present on the osp order line: '||
6526     ' inventory_item_id ->' ||l_get_order_details.inventory_item_id ||
6527     ' inventory_org_id ->' ||l_get_order_details.inventory_org_id ||
6528     ' serial_number ->' ||l_get_order_details.serial_number) ;
6529   END IF;
6530 
6531   /*
6532   Get the instance_id corresponding to the osp_order_line_id.
6533   We are assuming that this instance_id details are not manually updated/deleted by the user from
6534 	OM forms or elsewhere. If the item is IB tracked, at the time of ship line creation, we create the IB installation
6535 	details as well. Since the item and serial on the osp order line can undergo multiple part number/serial number changes,
6536 	and we are not storing the instance information we are retrieving it from IB transactions.
6537 	We need this instance to be present, otherwise we will not be able to pass the same to the PartNumber/Serial Number
6538 	change UI. If there is an issue with this approach, we may need to retrieve the instance details from
6539 	the history/consider storing the instance_id in the ahl_osp_order_lines table.
6540   */
6541   OPEN get_ship_line_instance(p_osp_order_line_id);
6542   FETCH get_ship_line_instance INTO l_ship_line_instance;
6543   IF (get_ship_line_instance%NOTFOUND) THEN
6544     CLOSE get_ship_line_instance;
6545     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6546       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Failed to find instance details for the ship line');
6547     END IF;
6548     return 'N';
6549   END IF;
6550   CLOSE get_ship_line_instance;
6551 
6552   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6553     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Shipped instance attributes : '||
6554     ' inventory_item_id ->' ||l_ship_line_instance.inventory_item_id ||
6555     ' last_vld_organization_id ->' || l_ship_line_instance.last_vld_organization_id) ;
6556   END IF;
6557 
6558   --Retrieve the item information corresponding to item. The IB tracked checks may be redundant as, if get_ship_line_instance
6559   --has an instance, it will surely be IB tracked
6560   OPEN get_inventory_flags(l_ship_line_instance.inventory_item_id,l_ship_line_instance.last_vld_organization_id );
6561   FETCH get_inventory_flags INTO l_serial_number_control_code, l_comms_nl_trackable_flag;
6562   IF (get_inventory_flags%NOTFOUND) THEN
6563     CLOSE get_inventory_flags;
6564     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6565       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Invalid Item, Failed to retrieve item information');
6566     END IF;
6567     return 'N';
6568   END IF;
6569   CLOSE get_inventory_flags;
6570 
6571   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6572     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'osp order line: Item Number flags : '||
6573     ' l_serial_number_control_code ->' || l_serial_number_control_code ||
6574     ' l_comms_nl_trackable_flag ->' || l_comms_nl_trackable_flag) ;
6575   END IF;
6576 
6577   --Item has to serial controlled and IB trackable
6578   IF (l_serial_number_control_code NOT IN (2,5,6) OR nvl(l_comms_nl_trackable_flag,'N') <> 'Y') THEN
6579     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6580       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Invalid Item: Has to be serial controlled and IB trackable');
6581     END IF;
6582     return 'N';
6583   END IF;
6584 
6585   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6586     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Returning Y');
6587   END IF;
6588 
6589   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6590     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'Exiting Function');
6591   END IF;
6592 
6593   return 'Y';
6594 
6595 END;
6596 
6597 -------------------------------------------------------------
6598 -- Start of Comments --
6599 --  Procedure name    :
6600 --  Type              : Public
6601 --  Function          : API to delete or cancel OSP shipment return lines and/or the IB installation
6602 --                      details before the part number/serial number change is performed from production
6603 --
6604 --  Pre-reqs    :
6605 --  Parameters  :
6606 --
6607 --  Standard IN  Parameters :
6608 --      p_api_version                   IN      NUMBER       Default  1.0
6609 --      p_init_msg_list                 IN      VARCHAR2     Default  FND_API.G_FALSE
6610 --      p_commit                        IN      VARCHAR2     Default  FND_API.G_FALSE
6611 --      p_validation_level              IN      NUMBER       Default  FND_API.G_VALID_LEVEL_FULL
6612 --      p_module_type                   IN      VARCHAR2     Default  NULL.
6613 --  Standard OUT Parameters :
6614 --      x_return_status                 OUT NOCOPY     VARCHAR2             Required
6615 --      x_msg_count                     OUT NOCOPY     NUMBER               Required
6616 --      x_msg_data                      OUT NOCOPY     VARCHAR2             Required
6617 --
6618 --  Cancel_retline_For_PartNum_Chg Parameters:
6619 --       p_osp_order_line_id          IN NUMBER
6620 --         The osp_line_id for of OSP Order for which the part number/serial number change
6621 --       p_inv_item_id                IN NUMBER
6622 --         The inv_item_id chosen by the user to replace the existint item.
6623 --       p_serial_number              IN VARCHAR2
6624 --         The serial_number chosen by the user to replace the existint serial.
6625 --
6626 --
6627 --  Version :
6628 --               Initial Version   1.0
6629 --
6630 --  End of Comments.
6631 
6632 PROCEDURE Process_Osp_SerialNum_Change(
6633     p_api_version           IN        NUMBER    := 1.0,
6634     p_init_msg_list         IN        VARCHAR2  := FND_API.G_FALSE,
6635     p_commit                IN        VARCHAR2  := FND_API.G_FALSE,
6636     p_validation_level      IN        NUMBER    := FND_API.G_VALID_LEVEL_FULL,
6637     p_module_type           IN        VARCHAR2  := NULL,
6638     p_serialnum_change_rec  IN        Sernum_Change_Rec_Type,
6639     x_return_status         OUT NOCOPY           VARCHAR2,
6640     x_msg_count             OUT NOCOPY           NUMBER,
6641     x_msg_data              OUT NOCOPY           VARCHAR2
6642     ) IS
6643 
6644     CURSOR get_osp_order_line_dtls(c_osp_order_line_id IN NUMBER) IS
6645     SELECT ospl.inventory_item_id,
6646            ospl.serial_number,
6647            ospl.inventory_org_id,
6648            ospl.oe_ship_line_id,
6649            ospl.oe_return_line_id,
6650            retl.ship_from_org_id receiving_org_id,
6651            ospl.osp_order_id
6652       FROM ahl_osp_order_lines ospl,
6653            oe_order_lines_all retl
6654      WHERE osp_order_line_id = c_osp_order_line_id
6655        AND ospl.oe_return_line_id = retl.line_id(+);
6656 
6657     CURSOR get_ship_line_instance(c_osp_order_line_id IN NUMBER) IS
6658     SELECT tld.instance_id
6659       FROM csi_t_transaction_lines tl,
6660            csi_t_txn_line_details tld,
6661            ahl_osp_order_lines ospl
6662      WHERE tl.source_transaction_id = ospl.oe_ship_line_id
6663        AND tl.source_transaction_table = G_TRANSACTION_TABLE
6664        AND tl.transaction_line_id = tld.transaction_line_id
6665        AND ospl.osp_order_line_id = c_osp_order_line_id;
6666 
6667    -- Get the current record from csi item instances
6668    CURSOR get_instance_details (c_instance_id IN NUMBER) IS
6669    SELECT instance_number,
6670           instance_id,
6671           object_version_number,
6672           inventory_item_id,
6673           serial_number,
6674           lot_number,
6675           inventory_revision
6676      FROM csi_item_instances
6677     WHERE instance_id = c_instance_id;
6678 
6679     CURSOR get_item_information(c_inv_item_id IN NUMBER, c_inv_org_id IN NUMBER) IS
6680     SELECT serial_number_control_code,
6681            comms_nl_trackable_flag
6682       FROM mtl_system_items_b
6683      WHERE inventory_item_id = c_inv_item_id
6684        AND organization_id = c_inv_org_id;
6685 
6686     CURSOR ahl_oe_lot_serial_id (p_oe_line_id IN NUMBER) IS
6687       SELECT lot_serial_id
6688        FROM oe_lot_serial_numbers
6689       WHERE line_id = p_oe_line_id;
6690 
6691     CURSOR get_same_phyitem_order_lines(c_osp_order_line_id IN NUMBER) IS
6692     SELECT matched_ol.osp_order_line_id
6693       FROM ahl_osp_order_lines matched_ol,
6694            ahl_osp_order_lines passed_ol
6695      WHERE passed_ol.osp_order_line_id = c_osp_order_line_id
6696        AND passed_ol.inventory_item_id = matched_ol.inventory_item_id
6697        AND passed_ol.serial_number = matched_ol.serial_number
6698        -- Added by jaramana on 12-APR-2010 for bug 9229301
6699        -- Check only within the current OSP Order
6700        AND passed_ol.osp_order_id = matched_ol.osp_order_id;
6701 
6702     CURSOR mtl_system_items_csr(c_inventory_id IN NUMBER, c_organization_id IN  NUMBER) IS
6703     SELECT serial_number_control_code,
6704            lot_control_code,
6705            comms_nl_trackable_flag,
6706            concatenated_segments
6707       FROM mtl_system_items_vl
6708      WHERE inventory_item_id   = c_inventory_id
6709        AND organization_id = c_organization_id;
6710 
6711     CURSOR c_get_inv_item_id(c_item_number VARCHAR2)IS
6712     SELECT inventory_item_id
6713       FROM MTL_SYSTEM_ITEMS_KFV
6714      WHERE CONCATENATED_SEGMENTS = c_item_number;
6715 
6716     l_api_name       CONSTANT VARCHAR2(30)  := 'Process_Osp_SerialNum_Change';
6717     l_api_version    CONSTANT NUMBER        := 1.0;
6718     L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.Process_Osp_SerialNum_Change';
6719 
6720     l_msg_data              VARCHAR2(2000);
6721     l_msg_index_out         NUMBER;
6722     l_index                 NUMBER;
6723     l_osp_order_line_dtls   get_osp_order_line_dtls%ROWTYPE;
6724     l_instance_dtls_rec     get_instance_details%ROWTYPE;
6725     l_mtl_system_items_rec  mtl_system_items_csr%ROWTYPE;
6726     l_lookup_code            fnd_lookups.lookup_code%TYPE;
6727     l_organization_id        NUMBER;
6728     l_ship_line_instance_id  NUMBER;
6729     l_del_oe_lines_tbl      SHIP_ID_TBL_TYPE;
6730     l_lot_serial_id         NUMBER;
6731 
6732     l_oe_line_rec           OE_ORDER_PUB.line_rec_type;
6733     l_oe_lot_serial_rec     OE_ORDER_PUB.lot_serial_rec_type;
6734     l_oe_line_tbl           OE_ORDER_PUB.LINE_TBL_TYPE;
6735     l_oe_lot_serial_tbl     OE_ORDER_PUB.LOT_SERIAL_TBL_TYPE;
6736     l_part_num_changed      VARCHAR2(1) := 'N';
6737     l_serial_num_changed    VARCHAR2(1) := 'N';
6738     l_order_line_id         NUMBER;
6739     l_serialnum_change_rec  Sernum_Change_Rec_Type := p_serialnum_change_rec;
6740     l_inventory_item_id     NUMBER;
6741 
6742     --Variables for Updating the CSI Instance Start
6743     l_return_val                BOOLEAN;
6744     l_attribute_value_id        NUMBER;
6745     l_object_version_number     NUMBER;
6746     l_attribute_value           csi_iea_values.attribute_value%TYPE;
6747     l_attribute_id              NUMBER;
6748     l_idx                       NUMBER := 0;
6749     l_serial_tag_code           csi_iea_values.attribute_value%TYPE;
6750     l_serial_tag_rec_found      VARCHAR2(1) DEFAULT 'Y';
6751     l_transaction_type_id       NUMBER;
6752 
6753     l_csi_instance_id_lst       CSI_DATASTRUCTURES_PUB.Id_Tbl;
6754 
6755     l_csi_instance_rec          csi_datastructures_pub.instance_rec;
6756     l_csi_party_rec             csi_datastructures_pub.party_rec;
6757     l_csi_transaction_rec       csi_datastructures_pub.transaction_rec;
6758     l_csi_extend_attrib_rec     csi_datastructures_pub.extend_attrib_values_rec;
6759     l_csi_relationship_rec      csi_datastructures_pub.ii_relationship_rec;
6760 
6761     l_csi_ext_attrib_values_tbl csi_datastructures_pub.extend_attrib_values_tbl;
6762     l_csi_party_tbl             csi_datastructures_pub.party_tbl;
6763     l_csi_account_tbl           csi_datastructures_pub.party_account_tbl;
6764     l_csi_pricing_attrib_tbl    csi_datastructures_pub.pricing_attribs_tbl;
6765     l_csi_org_assignments_tbl   csi_datastructures_pub.organization_units_tbl;
6766     l_csi_asset_assignment_tbl  csi_datastructures_pub.instance_asset_tbl;
6767     l_csi_relationship_tbl      csi_datastructures_pub.ii_relationship_tbl;
6768     l_csi_extend_attrib_rec1     csi_datastructures_pub.extend_attrib_values_rec;
6769     l_csi_ext_attrib_values_tbl1 csi_datastructures_pub.extend_attrib_values_tbl;
6770     l_idx1                       NUMBER := 0;
6771     --Variables for Updating the CSI Instance End
6772 
6773 BEGIN
6774 
6775   -- Standard start of API savepoint
6776   SAVEPOINT Process_Osp_SerialNum_Change;
6777 
6778   -- Initialize API return status to success
6779   x_return_status := FND_API.G_RET_STS_SUCCESS;
6780 
6781   l_oe_line_tbl := OE_ORDER_PUB.G_MISS_LINE_TBL;
6782   l_oe_lot_serial_tbl := OE_ORDER_PUB.G_MISS_LOT_SERIAL_TBL;
6783 
6784 
6785   -- Standard call to check for call compatibility
6786   IF NOT FND_API.Compatible_API_Call(l_api_version, p_api_version, l_api_name,
6787                                      G_PKG_NAME) THEN
6788     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6789   END IF;
6790 
6791   -- Initialize message list if p_init_msg_list is set to TRUE
6792   IF FND_API.To_Boolean(p_init_msg_list) THEN
6793     FND_MSG_PUB.Initialize;
6794   END IF;
6795 
6796   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6797     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Entering Procedure');
6798   END IF;
6799 
6800   --Dump the input parameters
6801   IF (fnd_log.level_statement >= fnd_log.g_current_runtime_level)THEN
6802     fnd_log.string(fnd_log.level_statement, L_DEBUG_KEY,'p_serialnum_change_rec.osp_line_id -> '||p_serialnum_change_rec.osp_line_id);
6803     fnd_log.string(fnd_log.level_statement, L_DEBUG_KEY,'p_serialnum_change_rec.instance_id -> '||p_serialnum_change_rec.instance_id);
6804     fnd_log.string(fnd_log.level_statement, L_DEBUG_KEY,'p_serialnum_change_rec.new_item_number -> '||p_serialnum_change_rec.new_item_number);
6805     fnd_log.string(fnd_log.level_statement, L_DEBUG_KEY,'p_serialnum_change_rec.new_serial_number -> '||p_serialnum_change_rec.new_serial_number);
6806   END IF;
6807 
6808   --osp_order_line_id should not be null
6809   IF(p_serialnum_change_rec.osp_line_id is NULL) THEN
6810     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_CHG_OSPLID_NLL');
6811     FND_MSG_PUB.ADD;
6812     RAISE  FND_API.G_EXC_ERROR;
6813   END IF;
6814 
6815   --validate the osp_order_line_id
6816   OPEN get_osp_order_line_dtls(l_serialnum_change_rec.osp_line_id);
6817   FETCH get_osp_order_line_dtls INTO l_osp_order_line_dtls;
6818   IF (get_osp_order_line_dtls%NOTFOUND) THEN
6819     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INVOP_OSP_LN_NFOUND');
6820     FND_MSG_PUB.ADD;
6821     CLOSE get_osp_order_line_dtls;
6822     RAISE  FND_API.G_EXC_ERROR;
6823   END IF;
6824   CLOSE get_osp_order_line_dtls;
6825 
6826   --check that the osp line is valid for the part number change
6827   IF(Is_part_chg_valid_for_ospline(p_serialnum_change_rec.osp_line_id) = 'N') THEN
6828     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_CHG_OSPL_INV');
6829     FND_MSG_PUB.ADD;
6830     RAISE  FND_API.G_EXC_ERROR;
6831   END IF;
6832 
6833   --Validate the passed new_item_number
6834   IF (l_serialnum_change_rec.new_item_number IS NOT NULL and l_serialnum_change_rec.new_item_number <> FND_API.G_MISS_CHAR) THEN
6835     -- Retrieve inventory_item_id from item_number
6836     OPEN c_get_inv_item_id(l_serialnum_change_rec.new_item_number);
6837     FETCH c_get_inv_item_id INTO l_inventory_item_id;
6838     CLOSE c_get_inv_item_id;
6839     IF l_inventory_item_id IS NULL THEN
6840       FND_MESSAGE.Set_Name('AHL','AHL_OSP_ITEM_INVALID');
6841       FND_MESSAGE.Set_token('ITEM',l_serialnum_change_rec.new_item_number);
6842       FND_MSG_PUB.ADD;
6843       RAISE FND_API.G_EXC_ERROR;
6844     END IF;
6845   ELSE
6846     FND_MESSAGE.Set_Name('AHL','AHL_OSP_INV_ITEM_NULL');
6847     FND_MSG_PUB.ADD;
6848     RAISE FND_API.G_EXC_ERROR;
6849   END IF;
6850 
6851   /*
6852   Get the instance present at the ship line
6853   We are assuming that this instance_id details are not manually updated/deleted by the user from
6854   OM forms or elsewhere. If the item is IB tracked, at the time of ship line creation, we create the IB installation
6855   details as well. Since the item and serial on the osp order line can undergo multiple part number/serial number changes,
6856   and we are not storing the instance information we are retrieving it from IB transactions.
6857   We need this instance to be present, otherwise we will not be able to pass the same to the PartNumber/Serial Number
6858   change UI. If there is an issue with this approach, we may need to retrieve the instance details from
6859   the history/consider storing the instance_id in the ahl_osp_order_lines table.
6860   */
6861 
6862   --If passed instance_id is null, derive it from the osp line, else check that its the one present on the osp line.
6863   OPEN get_ship_line_instance(l_serialnum_change_rec.osp_line_id);
6864   FETCH get_ship_line_instance INTO l_ship_line_instance_id;
6865   IF (get_ship_line_instance%NOTFOUND) THEN
6866     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_SHIPLINE_NO_INST');
6867     FND_MSG_PUB.ADD;
6868     CLOSE get_ship_line_instance;
6869     RAISE  FND_API.G_EXC_ERROR;
6870   END IF;
6871   CLOSE get_ship_line_instance;
6872 
6873   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6874     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_ship_line_instance_id =>' ||l_ship_line_instance_id);
6875   END IF;
6876   IF (l_serialnum_change_rec.instance_id is NULL) THEN
6877     l_serialnum_change_rec.instance_id := l_ship_line_instance_id;
6878   ELSE
6879     IF(l_serialnum_change_rec.instance_id <> l_ship_line_instance_id) THEN
6880       FND_MESSAGE.Set_Name('AHL','AHL_INVALID_INSTANCE');
6881       FND_MSG_PUB.ADD;
6882       RAISE FND_API.G_EXC_ERROR;
6883     END IF;
6884   END IF;
6885 
6886   -------------------------------------------------------------------------------------
6887   -- retrieve all instance related details and performe related validations
6888   -------------------------------------------------------------------------------------
6889   -- retrieve old instance details
6890   OPEN get_instance_details(l_serialnum_change_rec.instance_id);
6891   FETCH get_instance_details INTO l_instance_dtls_rec;
6892   CLOSE get_instance_details;
6893   IF l_instance_dtls_rec.instance_id IS NULL THEN
6894     FND_MESSAGE.Set_Name('AHL','AHL_INVALID_INSTANCE');
6895     FND_MSG_PUB.ADD;
6896     RAISE FND_API.G_EXC_ERROR;
6897   END IF;
6898 
6899   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6900     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_instance_dtls_rec.inventory_item_id =>' ||l_instance_dtls_rec.inventory_item_id);
6901     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_instance_dtls_rec.serial_number =>' ||l_instance_dtls_rec.serial_number);
6902   END IF;
6903 
6904   /*
6905   --If the l_new_inventory_item_id is null, it means that we are using the older part number itself and new serial number.
6906   l_inventory_item_id := NVL(l_new_inventory_item_id,l_instance_dtls_rec.inventory_item_id);
6907   */
6908 
6909   /*
6910   validate the passed item against the receiving organization, if the return line is present, otherwise
6911   validate it against the inventory org of the osp order line.
6912   */
6913   IF(l_osp_order_line_dtls.oe_return_line_id is not null AND l_osp_order_line_dtls.receiving_org_id is not NULL) THEN
6914     l_organization_id := l_osp_order_line_dtls.receiving_org_id;
6915   ELSE
6916     l_organization_id := l_osp_order_line_dtls.inventory_org_id;
6917   END IF;
6918 
6919   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6920     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_organization_id =>' ||l_organization_id);
6921     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'oe_return_line_id =>' ||l_osp_order_line_dtls.oe_return_line_id);
6922   END IF;
6923 
6924   --------------------------------------------
6925   -- Convert meaning to lookup code
6926   -- For Serialnum_tag_code.
6927   --------------------------------------------
6928   IF (l_serialnum_change_rec.New_Serial_Tag_Code IS NULL) OR (l_serialnum_change_rec.New_Serial_Tag_Code = FND_API.G_MISS_CHAR)
6929   THEN
6930     -- Check if meaning exists.
6931     IF (l_serialnum_change_rec.New_Serial_Tag_Mean IS NOT NULL) AND (l_serialnum_change_rec.New_Serial_Tag_Mean <> FND_API.G_MISS_CHAR)
6932     THEN
6933     Convert_To_LookupCode('AHL_SERIALNUMBER_TAG',
6934           l_serialnum_change_rec.New_Serial_Tag_Mean,
6935           l_lookup_code,
6936           l_return_val);
6937       IF NOT(l_return_val) THEN
6938         FND_MESSAGE.Set_Name('AHL','AHL_PRD_TAGMEANING_INVALID');
6939         FND_MESSAGE.Set_Token('TAG',l_serialnum_change_rec.New_Serial_Tag_Mean);
6940         FND_MSG_PUB.ADD;
6941         RAISE FND_API.G_EXC_ERROR;
6942       ELSE
6943         l_serialnum_change_rec.New_Serial_Tag_Code := l_lookup_code;
6944       END IF;
6945     END IF;
6946   END IF;
6947 
6948   --get the item information
6949   OPEN mtl_system_items_csr(l_inventory_item_id, l_organization_id);
6950   FETCH mtl_system_items_csr INTO l_mtl_system_items_rec;
6951   IF (mtl_system_items_csr%NOTFOUND) THEN
6952     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ITEM_INVALID');
6953     FND_MESSAGE.Set_token('ITEM',l_inventory_item_id);
6954     FND_MSG_PUB.ADD;
6955     CLOSE mtl_system_items_csr;
6956     RAISE  FND_API.G_EXC_ERROR;
6957   END IF;
6958   CLOSE mtl_system_items_csr;
6959 
6960   --Item has to serial controlled and IB trackable
6961   IF (l_mtl_system_items_rec.serial_number_control_code NOT IN (2,5,6) OR nvl(l_mtl_system_items_rec.comms_nl_trackable_flag,'N') <> 'Y') THEN
6962     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6963       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Invalid Item: Has to be serial controlled and IB trackable '||
6964       ' l_serial_number_control_code =>' || l_mtl_system_items_rec.serial_number_control_code || 'l_comms_nl_trackable_flag =>'||l_mtl_system_items_rec.comms_nl_trackable_flag );
6965     END IF;
6966     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_CHG_ITEM_SER_TR');
6967     FND_MESSAGE.Set_token('ITEM',l_mtl_system_items_rec.concatenated_segments);
6968     FND_MSG_PUB.ADD;
6969     RAISE  FND_API.G_EXC_ERROR;
6970   END IF;
6971 
6972   ------------------------------------------------------
6973   -- Call local procedure to validate the serial number
6974   ------------------------------------------------------
6975   Validate_SerialNumber(l_inventory_item_id,l_serialnum_change_rec.new_serial_number,
6976                         l_mtl_system_items_rec.serial_number_control_code,
6977                         l_serialnum_change_rec.New_Serial_Tag_Code,
6978                         l_mtl_system_items_rec.concatenated_segments);
6979 
6980   IF FND_MSG_PUB.count_msg > 0 THEN
6981     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6982       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Error Count after calling Validate_SerialNumber: '||FND_MSG_PUB.count_msg);
6983     END IF;
6984     RAISE  FND_API.G_EXC_ERROR;
6985   END IF;
6986 
6987   -- Retrieve existing value of serialNum_Tag_Code if present.
6988   AHL_UTIL_UC_PKG.GetCSI_Attribute_Value (l_serialnum_change_rec.instance_id,
6989                           'AHL_TEMP_SERIAL_NUM',
6990                           l_attribute_value,
6991                           l_attribute_value_id,
6992                           l_object_version_number,
6993                           l_return_val);
6994 
6995   IF NOT(l_return_val) THEN
6996     l_serial_tag_code := null;
6997     l_serial_tag_rec_found := 'N';
6998   ELSE
6999     l_serial_tag_code := l_attribute_value;
7000   END IF;
7001 
7002   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7003     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_serial_tag_code =>'||l_serial_tag_code);
7004     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_serial_tag_rec_found =>'||l_serial_tag_rec_found);
7005   END IF;
7006 
7007   -----------------------------------------------------------------------------------------
7008   -- Delete/Cancel the Return Shipment Line and/or the IB installation details
7009   -----------------------------------------------------------------------------------------
7010   --If the return line is present, then only delete the IB installation details, otherwise just update the instance details
7011   IF (l_osp_order_line_dtls.oe_return_line_id is not null) THEN
7012 
7013     --Whether its part number or serial number change, collect the lot serial record.
7014     OPEN ahl_oe_lot_serial_id (l_osp_order_line_dtls.oe_return_line_id);
7015     FETCH ahl_oe_lot_serial_id INTO l_lot_serial_id;
7016     IF (ahl_oe_lot_serial_id%FOUND) THEN
7017       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7018         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_lot_serial_id=>'||l_lot_serial_id);
7019       END IF;
7020       OE_LOT_SERIAL_UTIL.Query_Row(p_lot_serial_id => l_lot_serial_id, x_lot_serial_rec =>l_oe_lot_serial_rec);
7021     END IF;
7022     -- Missed CLOSE statement added by jaramana on 12-APR-2010
7023     CLOSE ahl_oe_lot_serial_id;
7024 
7025     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7026       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_instance_dtls_rec.inventory_item_id =>'||l_instance_dtls_rec.inventory_item_id);
7027       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_instance_dtls_rec.inventory_item_id =>'||l_instance_dtls_rec.inventory_item_id);
7028       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_instance_dtls_rec.serial_number =>'||l_instance_dtls_rec.serial_number);
7029       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_serialnum_change_rec.new_serial_number =>'||l_serialnum_change_rec.new_serial_number);
7030       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_instance_dtls_rec.lot_number =>'||l_instance_dtls_rec.lot_number);
7031       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_serialnum_change_rec.new_lot_number =>'||l_serialnum_change_rec.new_lot_number);
7032       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_instance_dtls_rec.inventory_revision =>'||l_instance_dtls_rec.inventory_revision);
7033       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_serialnum_change_rec.new_item_rev_number =>'||l_serialnum_change_rec.new_item_rev_number);
7034     END IF;
7035 
7036     --If it is only a serial number change, just delete the IB transation.
7037     IF(l_instance_dtls_rec.inventory_item_id = l_inventory_item_id) THEN
7038       IF((l_instance_dtls_rec.serial_number <> l_serialnum_change_rec.new_serial_number) OR
7039          (nvl(l_instance_dtls_rec.inventory_revision,FND_API.G_MISS_CHAR) <>
7040           nvl(l_serialnum_change_rec.new_item_rev_number,FND_API.G_MISS_CHAR)) OR
7041          (nvl(l_instance_dtls_rec.lot_number,FND_API.G_MISS_CHAR) <>
7042           nvl(l_serialnum_change_rec.new_lot_number,FND_API.G_MISS_CHAR)))
7043 
7044          THEN --Serial Number/Item Revision/Lot Number Changed
7045         l_serial_num_changed := 'Y';
7046         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7047           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Serial Number has been Changed');
7048           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Before calling Delete_IB_Transaction' );
7049         END IF;
7050         Delete_IB_Transaction(
7051           p_init_msg_list         => FND_API.G_FALSE, --p_init_msg_list,
7052           p_commit                => FND_API.G_FALSE,
7053           p_validation_level      => p_validation_level,
7054           x_return_status         => x_return_status,
7055           x_msg_count             => x_msg_count,
7056           x_msg_data              => x_msg_data,
7057           p_oe_line_id            => l_osp_order_line_dtls.oe_return_line_id);
7058 
7059         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7060           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'After calling Delete_IB_Transaction: x_return_status =>'||x_return_status );
7061         END IF;
7062         IF (x_return_status = FND_API.G_RET_STS_ERROR) THEN
7063           RAISE FND_API.G_EXC_ERROR;
7064         ELSIF (x_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
7065           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7066         END IF;
7067 
7068       ELSE --Item Number is same and Serial Number are same, rest of the item attributes are same.
7069         --If the serial tag code is also the same, there is no need to proceed further. Ideally we may also
7070         --consider making this change in the factory
7071         IF((l_serial_tag_rec_found = 'Y') AND
7072           ((l_serialnum_change_rec.New_Serial_Tag_Code IS NULL AND l_serial_tag_code IS NULL) OR
7073           (l_serial_tag_code IS NOT NULL AND l_serialnum_change_rec.New_Serial_Tag_Code IS NOT NULL AND
7074            l_serial_tag_code = l_serialnum_change_rec.New_Serial_Tag_Code))) THEN
7075           IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7076             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'None of the attributes are changed. Hence Returning');
7077             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_serialnum_change_rec.New_Serial_Tag_Code =>'||l_serialnum_change_rec.New_Serial_Tag_Code);
7078             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_serial_tag_code =>' || l_serial_tag_code);
7079           END IF;
7080           RETURN;
7081         END IF;
7082       END IF;--IF(l_instance_dtls_rec.serial_number <> l_serialnum_change_rec.new_serial_number)
7083     ELSE --Items are different, need to cancel the current line.
7084 
7085       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7086         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Item Number has been Changed');
7087         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'About to call Delete_Cancel_Order');
7088       END IF;
7089       l_part_num_changed := 'Y';
7090 
7091       --Save the OE Return Line information, before deletion
7092       l_oe_line_rec := OE_LINE_UTIL.QUERY_ROW(p_line_id => l_osp_order_line_dtls.oe_return_line_id);
7093       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7094         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Queried the OM Line Record');
7095       END IF;
7096 
7097       l_del_oe_lines_tbl(1) := l_osp_order_line_dtls.oe_return_line_id;
7098       Delete_Cancel_Order (
7099             p_api_version              => 1.0,
7100             p_init_msg_list            => FND_API.G_FALSE, -- Don't initialize the Message List
7101             p_commit                   => FND_API.G_FALSE, -- Don't commit independently
7102             p_oe_header_id             => null,  -- Not deleting the shipment header: Only the lines
7103             p_oe_lines_tbl             => l_del_oe_lines_tbl,  -- Lines to be deleted/Cancelled
7104             p_cancel_flag              => FND_API.G_FALSE,  -- Do Deletes if possible, Cancels if not
7105             x_return_status            => x_return_status ,
7106             x_msg_count                => x_msg_count ,
7107             x_msg_data                 => x_msg_data
7108         );
7109       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7110         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Returned from Delete_Cancel_Order, x_return_status = ' || x_return_status);
7111       END IF;
7112       IF (x_return_status = FND_API.G_RET_STS_ERROR) THEN
7113         RAISE FND_API.G_EXC_ERROR;
7114       ELSIF (x_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
7115         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7116       END IF;
7117 
7118     END IF; --IF(l_instance_dtls_rec.inventory_item_id = l_inventory_item_id)
7119   END IF; --IF (l_osp_order_line_dtls.oe_return_line_id is not null) THEN
7120 
7121 
7122   -----------------------------------------------------------------------------------------
7123   -- Update the Part Number/Serial Number using IB apis -- Start
7124   -----------------------------------------------------------------------------------------
7125 
7126 -- Build extended attribute record for serialnum_tag_code.
7127 
7128   IF (l_serial_tag_rec_found = 'Y' ) THEN
7129     IF(l_serialnum_change_rec.New_Serial_Tag_Code IS NULL AND l_serial_tag_code IS NOT NULL) OR
7130       (l_serial_tag_code IS NULL AND l_serialnum_change_rec.New_Serial_Tag_Code IS NOT NULL) OR
7131       (l_serialnum_change_rec.New_Serial_Tag_Code IS NOT NULL AND l_Serial_tag_code IS NOT NULL AND
7132        l_serialnum_change_rec.New_Serial_Tag_Code <> FND_API.G_MISS_CHAR AND
7133        l_serialnum_change_rec.New_Serial_Tag_Code <> l_Serial_tag_code) THEN
7134 
7135       -- changed value. update attribute record.
7136       l_csi_extend_attrib_rec.attribute_value_id := l_attribute_value_id;
7137       l_csi_extend_attrib_rec.attribute_value    := l_serialnum_change_rec.New_Serial_Tag_Code;
7138       l_csi_extend_attrib_rec.object_version_number := l_object_version_number;
7139       l_idx := l_idx + 1;
7140       l_csi_ext_attrib_values_tbl(l_idx) := l_csi_extend_attrib_rec;
7141     END IF;
7142   ELSIF (l_serial_tag_rec_found = 'N' ) THEN
7143      IF (l_serialnum_change_rec.New_Serial_Tag_Code IS NOT NULL) THEN
7144          -- create extended attributes.
7145          AHL_UTIL_UC_PKG.GetCSI_Attribute_ID('AHL_TEMP_SERIAL_NUM',l_attribute_id, l_return_val);
7146          IF NOT(l_return_val) THEN
7147             FND_MESSAGE.Set_Name('AHL','AHL_ATTRIB_CODE_MISSING');
7148             FND_MESSAGE.Set_Token('CODE', 'AHL_TEMP_SERIAL_NUM');
7149             FND_MSG_PUB.ADD;
7150          ELSE
7151             l_csi_extend_attrib_rec1.attribute_id := l_attribute_id;
7152             l_csi_extend_attrib_rec1.attribute_value := l_serialnum_change_rec.New_Serial_Tag_Code;
7153             l_csi_extend_attrib_rec1.instance_id := l_serialnum_change_rec.instance_id;
7154             l_idx1 := l_idx1 + 1;
7155             l_csi_ext_attrib_values_tbl1(l_idx1) := l_csi_extend_attrib_rec1;
7156          END IF;
7157      END IF;
7158   END IF;
7159 
7160   -- Populate rest of the attributes needed.
7161   l_csi_instance_rec.instance_id := l_serialnum_change_rec.instance_id;
7162   l_csi_instance_rec.object_version_number := l_instance_dtls_rec.object_version_number;
7163   l_csi_instance_rec.serial_number := l_serialnum_change_rec.new_serial_number;
7164   l_csi_instance_rec.inventory_item_id := l_inventory_item_id;
7165   l_csi_instance_rec.inventory_revision := l_serialnum_change_rec.new_item_rev_number;
7166   l_csi_instance_rec.lot_number := l_serialnum_change_rec.new_lot_number;
7167 
7168   -- Per IB team, this flag should always to 'N'.
7169   l_csi_instance_rec.mfg_serial_number_flag := 'N';
7170   -- csi transaction record.
7171   l_csi_transaction_rec.source_transaction_date := sysdate;
7172 
7173   -- get transaction_type_id .
7174   -- Balaji modified the transaction id type to 205--ITEM_SERIAL_CHANGE
7175   AHL_UTIL_UC_PKG.GetCSI_Transaction_ID('ITEM_SERIAL_CHANGE',l_transaction_type_id, l_return_val);
7176   IF NOT(l_return_val) THEN
7177     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7178   END IF;
7179 
7180   l_csi_transaction_rec.transaction_type_id := l_transaction_type_id;
7181   l_csi_transaction_rec.source_line_ref := G_CSI_T_SOURCE_LINE_REF;
7182   l_csi_transaction_rec.source_line_ref_id := l_serialnum_change_rec.osp_line_id;
7183 
7184 
7185   -------------------------------------------------------------
7186   -- Call IB API for making item/serial change for the instance.
7187   -------------------------------------------------------------
7188   CSI_Item_Instance_PUB.Update_Item_Instance(
7189            p_api_version            => 1.0,
7190            p_instance_rec           => l_csi_instance_rec,
7191            p_txn_rec                => l_csi_transaction_rec,
7192            p_ext_attrib_values_tbl  => l_csi_ext_attrib_values_tbl,
7193            p_party_tbl              => l_csi_party_tbl,
7194            p_account_tbl            => l_csi_account_tbl,
7195            p_pricing_attrib_tbl     => l_csi_pricing_attrib_tbl,
7196            p_org_assignments_tbl    => l_csi_org_assignments_tbl,
7197            p_asset_assignment_tbl   => l_csi_asset_assignment_tbl,
7198            x_instance_id_lst        => l_csi_instance_id_lst,
7199            x_return_status          => x_return_status,
7200            x_msg_count              => x_msg_count,
7201            x_msg_data               => x_msg_data );
7202 
7203   IF (x_return_status = FND_API.G_RET_STS_ERROR) THEN
7204      RAISE FND_API.G_EXC_ERROR;
7205   ELSIF (x_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
7206      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7207   END IF;
7208 
7209   -------------------------------------------------------------
7210   -- for extended attributes.
7211   -------------------------------------------------------------
7212   IF (l_idx1 > 0) THEN
7213      -- Call API to create extended attributes.
7214      CSI_Item_Instance_PUB.Create_Extended_attrib_values(
7215              p_api_version            => 1.0,
7216              p_txn_rec                => l_csi_transaction_rec,
7217              p_ext_attrib_tbl         => l_csi_ext_attrib_values_tbl1,
7218              x_return_status          => x_return_status,
7219              x_msg_count              => x_msg_count,
7220              x_msg_data               => x_msg_data );
7221 
7222      IF (x_return_status = FND_API.G_RET_STS_ERROR) THEN
7223        RAISE FND_API.G_EXC_ERROR;
7224      ELSIF (x_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
7225        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7226      END IF;
7227   END IF;
7228 
7229   -----------------------------------------------------------------------------------------
7230   -- Update the Part Number/Serial Number using IB apis -- End
7231   -----------------------------------------------------------------------------------------
7232 
7233   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7234     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_part_num_changed =>' ||l_part_num_changed);
7235     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_serial_num_changed =>' ||l_serial_num_changed);
7236   END IF;
7237 
7238   ----------------------------------------------------------------------------------------------
7239   -- Create the Return Line/IB Transaction and associate the return line with the Osp Order Line
7240   ----------------------------------------------------------------------------------------------
7241   --Only when the return line was present and was deleted/cancelled by us, we need to create the details again.
7242   IF (l_osp_order_line_dtls.oe_return_line_id is not null) THEN
7243     IF(l_part_num_changed = 'Y' OR l_serial_num_changed = 'Y') THEN
7244       IF(l_part_num_changed = 'Y') THEN
7245         --Create the Return Ship Line by calling the OM API
7246         /*
7247         We are not validating the l_oe_line_tbl and the l_oe_lot_serial_tbl as they have been extracted before deletion by us.
7248         */
7249         l_oe_line_tbl(1) := l_oe_line_rec;
7250         l_oe_line_tbl(1).inventory_item_id := l_inventory_item_id;
7251         l_oe_line_tbl(1).line_id := FND_API.G_MISS_NUM;
7252         l_oe_line_tbl(1).line_number := FND_API.G_MISS_NUM;
7253         l_oe_line_tbl(1).operation := OE_GLOBALS.G_OPR_CREATE;
7254         --l_oe_lot_serial_tbl(1) := l_oe_lot_serial_rec;
7255         --Do not use the lot serial record as is.
7256         l_oe_lot_serial_tbl(1).lot_serial_id := FND_API.G_MISS_NUM;
7257         l_oe_lot_serial_tbl(1).lot_number := l_serialnum_change_rec.new_lot_number;
7258         l_oe_lot_serial_tbl(1).from_serial_number := l_serialnum_change_rec.new_serial_number;
7259         l_oe_lot_serial_tbl(1).quantity := l_oe_lot_serial_rec.quantity;
7260         l_oe_lot_serial_tbl(1).line_index := 1;
7261         l_oe_lot_serial_tbl(1).operation := OE_GLOBALS.G_OPR_CREATE;
7262 
7263       ELSIF (l_serial_num_changed = 'Y') THEN
7264         --Part Number change has not happened. Do not create a new shipment line. But only update the serial num record.
7265         --Just populate the lot serial record to be updated.
7266         /*
7267         Note that even though only the revision number is changed and rest of the attributes are un changed,
7268         we are currently going through the update of the oe_lot_serial table, though this table does not store
7269         the revision number, so that the logic is kept simple. The serial change page ideally is not meant for
7270         isolated revision number changes. We are still cateting to it, just that we will be updating the record below
7271         though it will effectively not have any changes.
7272         */
7273         l_oe_lot_serial_rec.from_serial_number := l_serialnum_change_rec.new_serial_number;
7274         l_oe_lot_serial_rec.lot_number := l_serialnum_change_rec.new_lot_number;
7275         l_oe_lot_serial_rec.operation := OE_GLOBALS.G_OPR_UPDATE;
7276         l_oe_lot_serial_tbl(1) := l_oe_lot_serial_rec;
7277       END IF;
7278 
7279       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7280         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'About to call OE_ORDER_GRP.PROCESS_ORDER');
7281       END IF;
7282 
7283       OE_ORDER_GRP.PROCESS_ORDER(
7284         p_api_version_number  => 1.0,
7285         p_init_msg_list       => FND_API.G_TRUE,
7286         x_return_status       => x_return_status,
7287         x_msg_count           => x_msg_count,
7288         x_msg_data            => x_msg_data,
7289         p_header_rec          => x_header_rec,
7290         p_header_val_rec      => x_header_val_rec,
7291         p_line_tbl            => l_oe_line_tbl,
7292         p_line_val_tbl        => x_line_val_tbl,
7293         p_lot_serial_tbl      => l_oe_lot_serial_tbl,
7294         x_header_rec          => x_header_rec,
7295         x_header_val_rec      => x_header_val_rec,
7296         x_Header_Adj_tbl       =>  x_Header_Adj_tbl,
7297         x_Header_Adj_val_tbl   =>  x_Header_Adj_val_tbl,
7298         x_Header_price_Att_tbl =>  x_Header_price_Att_tbl,
7299         x_Header_Adj_Att_tbl   => x_Header_Adj_Att_tbl,
7300         x_Header_Adj_Assoc_tbl =>  x_Header_Adj_Assoc_tbl,
7301         x_Header_Scredit_tbl    =>   x_Header_Scredit_tbl,
7302         x_Header_Scredit_val_tbl =>    x_Header_Scredit_val_tbl,
7303         x_line_tbl               =>     x_line_tbl      ,
7304         x_line_val_tbl           =>    x_line_val_tbl ,
7305         x_Line_Adj_tbl           =>   x_Line_Adj_tbl    ,
7306         x_Line_Adj_val_tbl       =>  x_Line_Adj_val_tbl,
7307         x_Line_price_Att_tbl     =>   x_Line_price_Att_tbl,
7308         x_Line_Adj_Att_tbl       =>  x_Line_Adj_Att_tbl ,
7309         x_Line_Adj_Assoc_tbl     =>  x_Line_Adj_Assoc_tbl,
7310         x_Line_Scredit_tbl       => x_Line_Scredit_tbl ,
7311         x_Line_Scredit_val_tbl   =>  x_Line_Scredit_val_tbl,
7312         x_Lot_Serial_tbl         => x_Lot_Serial_tbl  ,
7313         x_Lot_Serial_val_tbl     => x_Lot_Serial_val_tbl   ,
7314         x_action_request_tbl     => x_action_request_tbl  );
7315 
7316       --populate the return_line_id with the one that was created in the OM API call.
7317       --A new return line is created only if part number changed happened.
7318       IF(l_part_num_changed = 'Y') THEN
7319         l_osp_order_line_dtls.oe_return_line_id := x_line_tbl(1).line_id;
7320       END IF;
7321 
7322       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7323         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Completed OE_ORDER_GRP.PROCESS_ORDER, x_return_status = ' || x_return_status);
7324         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_osp_order_line_dtls.oe_return_line_id = ' || l_osp_order_line_dtls.oe_return_line_id);
7325       END IF;
7326 
7327       IF (x_return_status <> FND_API.G_RET_STS_SUCCESS) THEN
7328         FOR i IN 1..x_msg_count LOOP
7329           OE_MSG_PUB.Get(p_msg_index => i,
7330                          p_encoded => FND_API.G_FALSE,
7331                          p_data    => l_msg_data,
7332                          p_msg_index_out => l_msg_index_out);
7333           fnd_msg_pub.add_exc_msg(p_pkg_name       => 'OE_ORDER_PUB',
7334                                   p_procedure_name => 'processOrder',
7335                                   p_error_text     => substr(l_msg_data,1,240));
7336           IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7337                FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'OE:Err Msg '||i||'.' || l_msg_data);
7338           END IF;
7339 
7340         END LOOP;
7341       END IF;
7342 
7343       IF (x_return_status = FND_API.G_RET_STS_ERROR) THEN
7344         RAISE FND_API.G_EXC_ERROR;
7345       ELSIF (x_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
7346         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7347       END IF;
7348 
7349 
7350       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7351         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Before calling Create_IB_Transaction');
7352       END IF;
7353 
7354       --Whether its the part number and/or the serial number that was changed, we need to call Create_IB_Transaction
7355       Create_IB_Transaction(
7356           p_init_msg_list         => FND_API.G_FALSE, --p_init_msg_list,
7357           p_commit                => FND_API.G_FALSE,
7358           p_validation_level      => p_validation_level,
7359           x_return_status         => x_return_status,
7360           x_msg_count             => x_msg_count,
7361           x_msg_data              => x_msg_data,
7362           p_osp_order_type        => AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_SERVICE,
7363           p_oe_line_type          => 'RETURN',
7364           p_oe_line_id            => l_osp_order_line_dtls.oe_return_line_id,
7365           p_csi_instance_id       => l_serialnum_change_rec.instance_id);
7366 
7367       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7368         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Return status from Create_IB_Transaction: ' || x_return_status);
7369       END IF;
7370 
7371       IF (x_return_status = FND_API.G_RET_STS_ERROR) THEN
7372         RAISE FND_API.G_EXC_ERROR;
7373       ELSIF (x_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
7374         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7375       END IF;
7376 
7377       /*
7378       We need to update the osp_order_line with the new return_line_id, if part number change has happened.
7379       If multiple services are being performed on the same item, we create only one shipment/return line.
7380       So we need to identify all such lines and update the oe_return_line_id of all such lines.
7381 
7382       How do we identify such lines? They should have the same Item and Serial Number on all the order lines.
7383       Even if a part number change has been done before, it would have been anyway applicable to all the
7384       order lines. So this logic of looking at the old item/serial numbers should be sufficient to retrieve all
7385       the order lines having the same physical item.
7386       */
7387       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7388         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Getting order lines with same physical item for Osp Order Id: ' ||l_osp_order_line_dtls.osp_order_id);
7389       END IF;
7390 
7391       --Only if a new return line has been created, update the osp order lines with the oe_return_line_id.
7392       IF(l_part_num_changed = 'Y') THEN
7393         OPEN get_same_phyitem_order_lines(l_serialnum_change_rec.osp_line_id);
7394         LOOP
7395           FETCH get_same_phyitem_order_lines INTO l_order_line_id;
7396           EXIT WHEN get_same_phyitem_order_lines%NOTFOUND;
7397           IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7398             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_order_line_id: ' || l_order_line_id);
7399           END IF;
7400           Update_OSP_Order_Lines(
7401               p_osp_order_id      => l_osp_order_line_dtls.osp_order_id,
7402               p_osp_line_id  => l_order_line_id,
7403               p_oe_ship_line_id   => FND_API.G_MISS_NUM ,
7404               p_oe_return_line_id => l_osp_order_line_dtls.oe_return_line_id);
7405 
7406         END LOOP;
7407         CLOSE get_same_phyitem_order_lines;
7408       END IF;
7409     END IF; --IF(l_part_num_changed = 'Y' OR l_serial_num_changed := 'Y') THEN
7410   END IF;--IF (l_osp_order_line_dtls.oe_return_line_id is not null) THEN
7411 
7412   IF Fnd_Api.TO_BOOLEAN(p_commit) THEN
7413     COMMIT;
7414   END IF;
7415 
7416   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7417     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Exiting Procedure');
7418   END IF;
7419 
7420   FND_MSG_PUB.Count_And_Get
7421     ( p_count => x_msg_count,
7422       p_data  => x_msg_data,
7423       p_encoded => fnd_api.g_false);
7424 
7425   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7426     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'Exiting Procedure');
7427   END IF;
7428 
7429 EXCEPTION
7430  WHEN FND_API.G_EXC_ERROR THEN
7431    x_return_status := FND_API.G_RET_STS_ERROR;
7432    Rollback to Process_Osp_SerialNum_Change;
7433    FND_MSG_PUB.count_and_get( p_count => x_msg_count,
7434                               p_data  => x_msg_data,
7435                               p_encoded => fnd_api.g_false);
7436 
7437  WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
7438    x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7439    Rollback to Process_Osp_SerialNum_Change;
7440    FND_MSG_PUB.count_and_get( p_count => x_msg_count,
7441                               p_data  => x_msg_data,
7442                                p_encoded => fnd_api.g_false);
7443  WHEN OTHERS THEN
7444     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7445     Rollback to Process_Osp_SerialNum_Change;
7446        fnd_msg_pub.add_exc_msg(p_pkg_name       => G_PKG_NAME,
7447                                p_procedure_name => 'Process_Osp_SerialNum_Change',
7448                                p_error_text     => SQLERRM);
7449 
7450     FND_MSG_PUB.count_and_get( p_count => x_msg_count,
7451                                p_data  => x_msg_data,
7452                                 p_encoded => fnd_api.g_false);
7453 
7454 
7455 END Process_Osp_SerialNum_Change;
7456 END AHL_OSP_SHIPMENT_PUB;