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