DBA Data[Home] [Help]

PACKAGE BODY: APPS.AHL_OSP_ORDERS_PVT

Source


1 PACKAGE BODY AHL_OSP_ORDERS_PVT AS
2 /* $Header: AHLVOSPB.pls 120.28.12020000.2 2012/12/07 01:32:16 sareepar ship $ */
3 
4   --G_DEBUG varchar2(1) := FND_PROFILE.VALUE('AHL_API_FILE_DEBUG_ON');
5   G_DEBUG       VARCHAR2(1)   := AHL_DEBUG_PUB.is_log_enabled;
6   G_PKG_NAME    CONSTANT VARCHAR2(30) := 'AHL_OSP_ORDERS_PVT';
7   G_APP_NAME    CONSTANT VARCHAR2(3) := 'AHL';
8   G_LOG_PREFIX  CONSTANT VARCHAR2(100) := 'ahl.plsql.'||G_PKG_NAME||'.';
9   G_OAF_MODULE  CONSTANT VARCHAR2(3) := 'OAF';
10 
11   g_module_type VARCHAR2(30)  := NULL;
12   --Mainly used for whether to default unchanged attributes.For OAF, we don't.
13   g_old_status_code VARCHAR2(30) := NULL;
14   /*
15   -- Commented out by jaramana on January 7, 2008 for the Requisition ER 6034236
16   g_order_status_for_update VARCHAR2(30) := NULL;
17   --For OSP status change
18   g_order_conversion_flag VARCHAR2(1) := 'N';
19   -- jaramana End
20   */
21 
22   --Indicates whether update order involves converting between 'SERVICE' and 'EXCHANGE'
23   g_old_type_code VARCHAR2(30) := NULL;
24   g_new_type_code VARCHAR2(30) := NULL;
25   --For OSP order type conversion
26 
27 --***** The following lines were added by Jerry for Inventory Service Orders *****--
28   g_dummy_char VARCHAR2(1);
29   g_dummy_num  NUMBER;
30 
31   TYPE item_service_rel_rec_type IS RECORD (
32     inv_org_id  NUMBER,
33     inv_item_id NUMBER,
34     service_item_id NUMBER);
35   TYPE item_service_rels_tbl_type IS TABLE OF item_service_rel_rec_type INDEX BY BINARY_INTEGER;
36 
37   TYPE Vendor_Id_Rec_Type IS RECORD (
38     vendor_id      NUMBER,
39     vendor_site_id NUMBER
40   );
41   TYPE Vendor_id_tbl_type IS TABLE OF Vendor_Id_Rec_Type INDEX BY BINARY_INTEGER;
42 
43 --Given a series of physical item, service item combinations to get the common
44 --vendor attributes.
45 PROCEDURE derive_default_vendor(
46   p_item_service_rels_tbl IN item_service_rels_tbl_type,
47   x_vendor_id             OUT NOCOPY NUMBER,
48   x_vendor_site_id        OUT NOCOPY NUMBER,
49   x_vendor_contact_id     OUT NOCOPY NUMBER,
50   x_valid_vendors_tbl     OUT NOCOPY vendor_id_tbl_type);
51 
52 --This procedure tries to derive the common vendors for an OSP Order based on its Lines.
53 --If the x_any_vendor_flag is 'Y', it means that any vendor can be used for this Order.
54 --Else, only the vendors whose ids are returned in the table x_valid_vendors_tbl
55 --are valid. It uses the derive_default_vendor method.
56 PROCEDURE Derive_Common_Vendors(p_osp_order_id      IN  NUMBER,
57                                 x_valid_vendors_tbl OUT NOCOPY Vendor_id_tbl_type,
58                                 x_any_vendor_flag   OUT NOCOPY VARCHAR2);
59 
60 --Newly created for validating vendor_id, vendor_site_id and
61 --vendor_contact_id together
62 PROCEDURE validate_vendor_site_contact(p_vendor_id IN NUMBER,
63                                        p_vendor_site_id IN NUMBER,
64                                        p_vendor_contact_id IN NUMBER);
65 
66 --yazhou 07-Aug-2006 starts
67 -- bug fix#5448191
68 
69 --Create shipment header/lines for osp line
70 PROCEDURE create_shipment(
71   p_osp_order_lines_tbl IN OUT NOCOPY osp_order_lines_tbl_type);
72 
73 --yazhou 07-Aug-2006 ends
74 
75 --Contains OSP order header record default, validation and creation
76 PROCEDURE create_osp_order_header(
77   p_x_osp_order_rec       IN OUT NOCOPY osp_order_rec_type,
78   p_x_osp_order_lines_tbl IN OUT NOCOPY osp_order_lines_tbl_type);
79 
80 --Contains OSP order header record default, validation and update, including
81 --Status change and type conversion
82 PROCEDURE update_osp_order_header(
83   p_x_osp_order_rec IN OUT NOCOPY osp_order_rec_type);
84 
85 --Contains OSP order line record default and creation
86 PROCEDURE create_osp_order_line(
87   p_x_osp_order_line_rec IN OUT NOCOPY osp_order_line_rec_type);
88 
89 --validate osp line for creation
90 PROCEDURE validate_order_line_creation(
91   p_x_osp_order_line_rec IN OUT NOCOPY osp_order_line_rec_type);
92 
93 --update osp order line record
94 PROCEDURE update_osp_order_line(
95   p_x_osp_order_line_rec IN OUT NOCOPY osp_order_line_rec_type);
96 
97 --validate osp line for update
98 PROCEDURE validate_order_line_update(
99   p_x_osp_order_line_rec IN OUT NOCOPY osp_order_line_rec_type);
100 
101 --do the G_MISS to NULL and NULL to old value conversion for order
102 --line record
103 PROCEDURE default_unchanged_order_line(
104   p_x_osp_order_line_rec IN OUT NOCOPY osp_order_line_rec_type);
105 
106 --Convert values to Ids for OSP order line record
107 PROCEDURE convert_order_line_val_to_id(
108   p_x_osp_order_line_rec IN OUT NOCOPY osp_order_line_rec_type);
109 
110 --Validate the physical/service item combination with the vendor
111 PROCEDURE validate_vendor_service_item(
112   p_osp_order_line_rec IN osp_order_line_rec_type);
113 
114 --Validate service_item_id (simplified the original one)
115 PROCEDURE validate_service_item_id(
116   p_service_item_id IN NUMBER,
117   p_organization_id IN NUMBER);
118 
119 --Handle the order type change (new procedure but copied the old logic)
120 PROCEDURE process_order_type_change(
121   p_osp_order_rec IN osp_order_rec_type);
122 
123 -----------------------------------------------
124 -- Declare Local TYPE, FUNCTION and PROCEDURE--
125 -----------------------------------------------
126 TYPE del_cancel_so_line_rec IS RECORD(
127    osp_order_id NUMBER,
128    oe_ship_line_id NUMBER,
129    oe_return_line_id NUMBER
130    );
131 TYPE del_cancel_so_lines_tbl_type IS TABLE OF del_cancel_so_line_rec INDEX BY BINARY_INTEGER;
132 PROCEDURE delete_cancel_so(
133    p_oe_header_id             IN        NUMBER,
134    p_del_cancel_so_lines_tbl  IN        del_cancel_so_lines_tbl_type,
135    p_cancel_flag              IN        VARCHAR2  := FND_API.G_FALSE
136    );
137 PROCEDURE process_order_status_change(
138    p_x_osp_order_rec IN OUT NOCOPY   osp_order_rec_type
139    );
140 FUNCTION can_convert_order(
141     p_osp_order_id  IN  NUMBER,
142     p_old_type_code IN  VARCHAR2,
143     p_new_type_code IN  VARCHAR2
144     --p_new_order_rec  IN AHL_OSP_ORDERS_PVT.osp_order_rec_type,
145     --p_old_order_rec  IN AHL_OSP_ORDERS_PVT.osp_order_rec_type
146   ) RETURN BOOLEAN;
147 FUNCTION vendor_id_exist_in_PO(
148      p_po_header_id   IN NUMBER,
149      p_vendor_id      IN NUMBER
150   ) RETURN BOOLEAN;
151 FUNCTION vendor_site_id_exist_in_PO(
152     p_po_header_id      IN NUMBER,
153     p_vendor_site_id    IN NUMBER
154  ) RETURN BOOLEAN;
155 
156 PROCEDURE validate_order_header(
157     p_x_osp_order_rec  IN OUT NOCOPY   osp_order_rec_type
158     );
159 PROCEDURE validate_vendor(
160     p_vendor_id  IN NUMBER
161     );
162 PROCEDURE validate_vendor_site(
163     p_vendor_id  IN NUMBER,
164     p_vendor_site_id  IN NUMBER
165     );
166 PROCEDURE validate_customer(
167     p_customer_id IN NUMBER
168     );
169 PROCEDURE validate_buyer(
170     p_po_agent_id IN NUMBER
171     );
172 PROCEDURE validate_contract(
173     p_order_type_code IN VARCHAR2,
174     p_contract_id IN VARCHAR2,
175     p_party_vendor_id IN VARCHAR2,
176     p_authoring_org_id IN VARCHAR2
177     );
178 PROCEDURE validate_po_header(
179     p_osp_order_id IN NUMBER,
180     p_po_header_id IN NUMBER
181     );
182 PROCEDURE default_unchanged_order_header(
183 	p_x_osp_order_rec  IN OUT NOCOPY osp_order_rec_type
184     );
185 PROCEDURE validate_workorder(
186     p_workorder_id IN NUMBER
187     );
188 
189 --Commented by mpothuku on 27-Feb-06 as the following procs. are not being used anymore and the Perf Bug #4919164 --has been logged for some of the cursors in the procedures below
190 /*
191 PROCEDURE validate_service_item(
192     p_workorder_id IN NUMBER,
193     p_service_item_id IN NUMBER,
194     p_order_type_code IN VARCHAR2
195     );
196 PROCEDURE validate_service_item_desc(
197     p_service_item_id IN NUMBER,
198     p_service_item_description IN VARCHAR2
199     );
200 */
201 
202 -- Added by jaramana on January 7, 2008 for the Requisition ER 6034236
203 PROCEDURE val_svc_item_vs_wo_svc_item(
204     p_workorder_id IN NUMBER,
205     p_service_item_id IN NUMBER
206     );
207 -- jaramana End
208 
209 --p_org_id added by mpothuku to fix the Perf Bug #4919164
210 PROCEDURE validate_service_item_uom(
211    p_service_item_id IN NUMBER,
212    p_service_item_uom_code IN VARCHAR2,
213    p_org_id IN NUMBER
214    );
215 PROCEDURE validate_po_line_type(
216    p_po_line_type_id IN NUMBER
217    );
218 PROCEDURE validate_po_line(
219    p_po_line_id IN NUMBER,
220    p_osp_order_id IN NUMBER
221    );
222 PROCEDURE validate_exchange_instance_id(
223    p_exchange_instance_id IN NUMBER
224    );
225 -- Commented out by jaramana on January 7, 2008 for the Requisition ER 6034236 (We are not using this procedure anymore)
226 /*
227 PROCEDURE nullify_exchange_instance(
228   p_osp_order_id          IN NUMBER,
229   p_x_osp_order_lines_tbl IN OUT NOCOPY osp_order_lines_tbl_type
230 );
231 */
232 -- jaramana End
233 
234 ------------------------------
235 -- Declare Local Procedures --
236 ------------------------------
237 /*
238 PROCEDURE process_order_header(
239     p_validation_level IN   NUMBER    := FND_API.G_VALID_LEVEL_FULL,
240     p_x_osp_order_rec  IN OUT NOCOPY  osp_order_rec_type
241     );
242 PROCEDURE process_order_lines(
243     p_validation_level      IN       NUMBER    := FND_API.G_VALID_LEVEL_FULL,
244     p_osp_order_rec         IN       osp_order_rec_type,
245     p_x_osp_order_lines_tbl IN OUT NOCOPY  osp_order_lines_tbl_type
246     );
247 PROCEDURE convert_order_lines_val_to_id(
248     p_x_osp_order_lines_tbl IN OUT NOCOPY  osp_order_lines_tbl_type
249     );
250 PROCEDURE convert_order_header_val_to_id(
251     p_x_osp_order_rec  IN OUT NOCOPY  osp_order_rec_type
252     );
253 PROCEDURE validate_order_lines(
254     p_osp_order_rec         IN       osp_order_rec_type,
255     p_x_osp_order_lines_tbl IN OUT NOCOPY    osp_order_lines_tbl_type,
256     x_del_cancel_so_lines_tbl OUT NOCOPY del_cancel_so_lines_tbl_type
257     );
258 PROCEDURE default_unchanged_order_lines(
259 	p_x_osp_order_lines_tbl IN OUT NOCOPY osp_order_lines_tbl_type
260     );
261 PROCEDURE validate_osp_updates(
262    p_osp_order_rec         IN       osp_order_rec_type
263    );
264 */
265 
266 -- *****Procedure definitions start here *****--
267 -- When DML operation is not INSERT and osp_order_id is not available, populates osp_order_id from othe unique keys.
268 -- other unique keys are -- osp_order_number, po_header_id, oe_header_id
269 --------------------------------------------------------------------------------------------------------------
270 --(?)Jerry on 04/17/2005, may need to add vendor_contact value to id conversion in this procedure
271 
272 --salogan added the following for supplier warranty starts
273 PROCEDURE Get_Warranty_Rec_for_Create(
274         p_x_osp_order_line_rec IN         osp_order_line_rec_type,
275         x_warranty_entl_tbl    OUT NOCOPY AHL_WARRANTY_ENTL_PVT.Warranty_Entl_Tbl_Type,
276         x_call_process_entl    OUT NOCOPY VARCHAR2
277 );
278 
279 PROCEDURE Get_Warranty_Rec_for_Update(
280         p_x_osp_order_line_rec IN         osp_order_line_rec_type,
281         x_warranty_entl_tbl    OUT NOCOPY AHL_WARRANTY_ENTL_PVT.Warranty_Entl_Tbl_Type,
282         x_call_process_entl    OUT NOCOPY VARCHAR2
283 );
284 
285 PROCEDURE Get_Entl_rec_for_Delete(
286         p_osp_order_id         IN         NUMBER := null,
287         p_osp_order_line_id    IN         NUMBER := null,
288         x_warranty_entl_tbl    OUT NOCOPY AHL_WARRANTY_ENTL_PVT.Warranty_Entl_Tbl_Type
289 );
290 --salogan added the following for supplier warranty ends
291 
292 PROCEDURE convert_order_header_val_to_id(
293     p_x_osp_order_rec  IN OUT NOCOPY  osp_order_rec_type
294     ) IS
295     CURSOR vendor_id_csr(p_vendor_name IN VARCHAR2) IS
296     SELECT vendor_id FROM po_vendors_view
297     WHERE vendor_name = p_vendor_name
298       AND enabled_flag = G_YES_FLAG
299       AND NVL(vendor_start_date_active, SYSDATE - 1) <= SYSDATE
300       AND NVL(vendor_end_date_active, SYSDATE + 1) > SYSDATE;
301     l_vendor_id NUMBER;
302     CURSOR vendor_site_id_csr(p_vendor_site_code IN VARCHAR2) IS
303     SELECT vendor_site_id FROM po_vendor_sites
304     WHERE vendor_site_code = p_vendor_site_code
305       AND   NVL(inactive_date, SYSDATE + 1) > SYSDATE
306       AND   purchasing_site_flag = G_YES_FLAG;
307     l_vendor_site_id NUMBER;
308 
309     CURSOR po_agent_id_csr(p_buyer_name IN VARCHAR2) IS
310     SELECT buyer_id FROM po_agents_name_v
311     WHERE full_name = p_buyer_name;
312     l_po_agent_id NUMBER;
313     CURSOR customer_id_csr(p_customer_name IN VARCHAR2) IS
314     SELECT party_id from hz_parties
315     where party_name = p_customer_name and party_type = 'ORGANIZATION';
316     l_customer_id NUMBER;
317     CURSOR contract_id_csr(p_contract_number IN VARCHAR2) IS
318     SELECT chr.id  FROM okc_k_headers_b chr, okc_statuses_b sts
319     WHERE chr.contract_number = p_contract_number
320     AND chr.sts_code = sts.code AND sts.ste_code in ('ACTIVE', 'SIGNED');
321     l_contract_id NUMBER;
322     CURSOR osp_order_on_csr(p_osp_order_number IN NUMBER) IS
323     SELECT osp_order_id, object_version_number from ahl_osp_orders_b
324     WHERE osp_order_number = p_osp_order_number;
325     -- Commented out by jaramana on January 8, 2008 for the Requisition ER 6034236
326     /*
327     CURSOR osp_order_po_csr(p_po_header_id IN NUMBER) IS
328     SELECT osp_order_id, object_version_number from ahl_osp_orders_b
329     WHERE po_header_id = p_po_header_id;
330     CURSOR osp_order_oe_csr(p_oe_header_id IN NUMBER) IS
331     SELECT osp_order_id, object_version_number from ahl_osp_orders_b
332     WHERE oe_header_id = p_oe_header_id;
333     */
334     -- jaramana End
335     l_osp_order_id NUMBER;
336     l_object_version_number NUMBER;
337     l_count NUMBER;
338     L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.convert_order_header_val_to_id';
339 BEGIN
340     --dbms_output.put_line('Entering : convert_order_header_val_to_id');
341     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
342       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Procedure');
343     END IF;
344     IF (p_x_osp_order_rec.operation_flag IS NOT NULL AND p_x_osp_order_rec.operation_flag NOT IN(G_OP_CREATE, G_OP_UPDATE, G_OP_DELETE)) THEN
345         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INVOP');
346         FND_MSG_PUB.ADD;
347     END IF;
348     IF FND_MSG_PUB.count_msg > 0 THEN
349        RAISE  FND_API.G_EXC_ERROR;
350        -- RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
351     END IF;
352     IF(g_module_type = 'JSP')THEN
353         -- conversion of value to id for Vendor name
354         IF(p_x_osp_order_rec.vendor_name IS NULL) THEN
355            p_x_osp_order_rec.vendor_id := NULL;
356         ELSIF (p_x_osp_order_rec.vendor_name = FND_API.G_MISS_CHAR) THEN
357            IF(p_x_osp_order_rec.operation_flag <> G_OP_CREATE) THEN
358               p_x_osp_order_rec.vendor_id := FND_API.G_MISS_NUM;
359            ELSE
360               p_x_osp_order_rec.vendor_id := NULL;
361            END IF;
362         END IF;
363         IF(p_x_osp_order_rec.vendor_site_code IS NULL) THEN
364            p_x_osp_order_rec.vendor_site_id := NULL;
365         ELSIF (p_x_osp_order_rec.vendor_site_code = FND_API.G_MISS_CHAR) THEN
366            IF(p_x_osp_order_rec.operation_flag <> G_OP_CREATE) THEN
367               p_x_osp_order_rec.vendor_site_id := FND_API.G_MISS_NUM;
368            ELSE
369               p_x_osp_order_rec.vendor_site_id := NULL;
370            END IF;
371         END IF;
372         IF(p_x_osp_order_rec.buyer_name IS NULL) THEN
373            p_x_osp_order_rec.po_agent_id := NULL;
374         ELSIF (p_x_osp_order_rec.buyer_name = FND_API.G_MISS_CHAR) THEN
375            IF(p_x_osp_order_rec.operation_flag <> G_OP_CREATE) THEN
376               p_x_osp_order_rec.po_agent_id := FND_API.G_MISS_NUM;
377            ELSE
378               p_x_osp_order_rec.po_agent_id := NULL;
379            END IF;
380         END IF;
381         IF(p_x_osp_order_rec.customer_name IS NULL) THEN
382            p_x_osp_order_rec.customer_id := NULL;
383         ELSIF (p_x_osp_order_rec.customer_name = FND_API.G_MISS_CHAR) THEN
384            IF(p_x_osp_order_rec.operation_flag <> G_OP_CREATE) THEN
385               p_x_osp_order_rec.customer_id := FND_API.G_MISS_NUM;
386            ELSE
387               p_x_osp_order_rec.customer_id := NULL;
388            END IF;
389         END IF;
390         IF(p_x_osp_order_rec.contract_number IS NULL) THEN
391            p_x_osp_order_rec.contract_id := NULL;
392         ELSIF (p_x_osp_order_rec.contract_number = FND_API.G_MISS_CHAR) THEN
393            IF(p_x_osp_order_rec.operation_flag <> G_OP_CREATE) THEN
394               p_x_osp_order_rec.contract_id := FND_API.G_MISS_NUM;
395            ELSE
396               p_x_osp_order_rec.contract_id := NULL;
397            END IF;
398         END IF;
399         --dbms_output.put_line('Done nulling out order header ids');
400     END IF;
401    -- conversion of value to id for Vendor name
402    IF (p_x_osp_order_rec.vendor_name IS NOT NULL AND  p_x_osp_order_rec.vendor_name <> FND_API.G_MISS_CHAR) THEN
403         l_count := 0;
404         --dbms_output.put_line('converting vendor_name to id');
405         OPEN vendor_id_csr(p_x_osp_order_rec.vendor_name);
406         LOOP
407            FETCH vendor_id_csr INTO l_vendor_id;
408            IF(vendor_id_csr%NOTFOUND) THEN
409              EXIT;
410            ELSIF (p_x_osp_order_rec.vendor_id IS NOT NULL AND p_x_osp_order_rec.vendor_id = l_vendor_id) THEN
411              l_count := 1;
412              EXIT;
413            ELSE
414              l_count := l_count + 1;
415            END IF;
416         END LOOP;
417         CLOSE vendor_id_csr;
418         IF(l_count = 0 ) THEN
419            --dbms_output.put_line('checkpoint 1');
420            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENDOR_NAME_INV');
421            FND_MESSAGE.Set_Token('VENDOR_NAME', p_x_osp_order_rec.vendor_name);
422            FND_MSG_PUB.ADD;
423         ELSIF (l_count > 1) THEN
424            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENDOR_INV_NON_UNQ');
425            FND_MESSAGE.Set_Token('VENDOR_NAME', p_x_osp_order_rec.vendor_name);
426            FND_MSG_PUB.ADD;
427         ELSE
428            p_x_osp_order_rec.vendor_id := l_vendor_id;
429         END IF;
430         --dbms_output.put_line('done converting vendor_name to id');
431     END IF;
432     -- conversion of value to id for vendor site code
433     IF (p_x_osp_order_rec.vendor_site_code IS NOT NULL AND  p_x_osp_order_rec.vendor_site_code <> FND_API.G_MISS_CHAR) THEN
434         l_count := 0;
435         OPEN vendor_site_id_csr(p_x_osp_order_rec.vendor_site_code);
436         LOOP
437            FETCH vendor_site_id_csr INTO l_vendor_site_id;
438            IF(vendor_site_id_csr%NOTFOUND) THEN
439              EXIT;
440            ELSIF (p_x_osp_order_rec.vendor_site_id IS NOT NULL AND p_x_osp_order_rec.vendor_site_id = l_vendor_site_id) THEN
441              l_count := 1;
442              EXIT;
443            ELSE
444              l_count := l_count + 1;
445            END IF;
446         END LOOP;
447         CLOSE vendor_site_id_csr;
448         IF(l_count = 0 ) THEN
449            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENSITE_INV');
450            FND_MESSAGE.Set_Token('VENDOR_SITE', p_x_osp_order_rec.vendor_site_code);
451            FND_MSG_PUB.ADD;
452         ELSIF (l_count > 1) THEN
453            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENSITE_INV_NON_UNQ');
454            FND_MESSAGE.Set_Token('VENDOR_SITE', p_x_osp_order_rec.vendor_site_code);
455            FND_MSG_PUB.ADD;
456         ELSE
457            p_x_osp_order_rec.vendor_site_id := l_vendor_site_id;
458         END IF;
459      END IF;
460      -- conversion of value to id for buyer name
461      IF (p_x_osp_order_rec.buyer_name IS NOT NULL AND  p_x_osp_order_rec.buyer_name <> FND_API.G_MISS_CHAR) THEN
462         l_count := 0;
463         OPEN po_agent_id_csr(p_x_osp_order_rec.buyer_name);
464         LOOP
465            FETCH po_agent_id_csr INTO l_po_agent_id;
466            IF(po_agent_id_csr%NOTFOUND) THEN
467              EXIT;
468            ELSIF (p_x_osp_order_rec.po_agent_id IS NOT NULL AND p_x_osp_order_rec.po_agent_id = l_po_agent_id) THEN
469              l_count := 1;
470              EXIT;
471            ELSE
472              l_count := l_count + 1;
473            END IF;
474         END LOOP;
475         CLOSE po_agent_id_csr;
476         IF(l_count = 0 ) THEN
477            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_BUYER_INV');
478            FND_MESSAGE.Set_Token('BUYER_NAME', p_x_osp_order_rec.buyer_name);
479            FND_MSG_PUB.ADD;
480         ELSIF (l_count > 1) THEN
481            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_BUYER_INV_NON_UNQ');
482            FND_MESSAGE.Set_Token('BUYER_NAME', p_x_osp_order_rec.buyer_name);
483            FND_MSG_PUB.ADD;
484         ELSE
485            p_x_osp_order_rec.po_agent_id := l_po_agent_id;
486         END IF;
487      END IF;
488      -- conversion of value to id for customer name
489      IF (p_x_osp_order_rec.customer_name IS NOT NULL AND  p_x_osp_order_rec.customer_name <> FND_API.G_MISS_CHAR) THEN
490         --dbms_output.put_line('converting customer_name to id');
491         l_count := 0;
492         OPEN customer_id_csr(p_x_osp_order_rec.customer_name);
493         LOOP
494            FETCH customer_id_csr INTO l_customer_id;
495            IF(customer_id_csr%NOTFOUND) THEN
496              EXIT;
497            ELSIF (p_x_osp_order_rec.customer_id IS NOT NULL AND p_x_osp_order_rec.customer_id = l_customer_id) THEN
498              l_count := 1;
499              EXIT;
500            ELSE
501              l_count := l_count + 1;
502            END IF;
503         END LOOP;
504         CLOSE customer_id_csr;
505         IF(l_count = 0 ) THEN
506            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_CUST_INV');
507            FND_MESSAGE.Set_Token('CUST_NAME', p_x_osp_order_rec.customer_name);
508            FND_MSG_PUB.ADD;
509         ELSIF (l_count > 1) THEN
510            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_CUST_INV_NON_UNQ');
511            FND_MESSAGE.Set_Token('CUST_NAME', p_x_osp_order_rec.customer_name);
512            FND_MSG_PUB.ADD;
513         ELSE
514            p_x_osp_order_rec.customer_id := l_customer_id;
515         END IF;
516      END IF;
517      -- conversion of value to id for contract number
518      IF (p_x_osp_order_rec.contract_number IS NOT NULL AND  p_x_osp_order_rec.contract_number <> FND_API.G_MISS_CHAR) THEN
519          --dbms_output.put_line('converting contract_number to id');
520          OPEN contract_id_csr(p_x_osp_order_rec.contract_number);
521          FETCH contract_id_csr INTO l_contract_id;
522          IF(contract_id_csr%NOTFOUND) THEN
523             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_CONTRACT_INV');
524             FND_MESSAGE.Set_Token('CONTRACT_NUM', p_x_osp_order_rec.contract_number);
525             FND_MSG_PUB.ADD;
526          ELSE
527             p_x_osp_order_rec.contract_id := l_contract_id;
528          END IF;
529          CLOSE contract_id_csr;
530      END IF;
531     IF FND_MSG_PUB.count_msg > 0 THEN
532        RAISE  FND_API.G_EXC_ERROR;
533        -- RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
534     END IF;
535     -- finding unique key osp_order_id when operation flag is not 'C'
536     IF (p_x_osp_order_rec.osp_order_id IS NULL) THEN
537         --dbms_output.put_line('finding osp_order_id');
538         IF p_x_osp_order_rec.operation_flag <> G_OP_CREATE THEN
539             IF (p_x_osp_order_rec.osp_order_number IS NOT NULL) THEN
540                 OPEN osp_order_on_csr(p_x_osp_order_rec.osp_order_number);
541                 FETCH osp_order_on_csr INTO l_osp_order_id,l_object_version_number;
542                 IF(osp_order_on_csr%NOTFOUND) THEN
543                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_NUM_INV');
544                     FND_MESSAGE.Set_Token('ORDER_NUMBER', p_x_osp_order_rec.osp_order_number);
545                     FND_MSG_PUB.ADD;
546                 ELSE
547                     p_x_osp_order_rec.osp_order_id := l_osp_order_id;
548                     p_x_osp_order_rec.object_version_number := l_object_version_number;
549                 END IF;
550                 CLOSE osp_order_on_csr;
551             -- Commented out by jaramana on January 8, 2008 for the Requisition ER 6034236
552             /*
553             ELSIF (p_x_osp_order_rec.po_header_id IS NOT NULL AND p_x_osp_order_rec.po_header_id <> FND_API.G_MISS_NUM) THEN
554                 OPEN osp_order_po_csr(p_x_osp_order_rec.po_header_id);
555                 FETCH osp_order_po_csr INTO l_osp_order_id,l_object_version_number;
556                 IF(osp_order_po_csr%NOTFOUND) THEN
557                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_PO_HEADER_INV');
558                     FND_MESSAGE.Set_Token('PO_HEADER_ID', p_x_osp_order_rec.po_header_id);
559                     FND_MSG_PUB.ADD;
560                 ELSE
561                     p_x_osp_order_rec.osp_order_id := l_osp_order_id;
562                     p_x_osp_order_rec.object_version_number := l_object_version_number;
563                 END IF;
564                 CLOSE osp_order_po_csr;
565             ELSIF (p_x_osp_order_rec.oe_header_id IS NOT NULL AND p_x_osp_order_rec.oe_header_id <> FND_API.G_MISS_NUM) THEN
566                 OPEN osp_order_oe_csr(p_x_osp_order_rec.oe_header_id);
567                 FETCH osp_order_oe_csr INTO l_osp_order_id,l_object_version_number;
568                 IF(osp_order_oe_csr%NOTFOUND) THEN
569                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_OE_HEADER_INV');
570                     FND_MESSAGE.Set_Token('OE_HEADER_ID', p_x_osp_order_rec.oe_header_id);
571                     FND_MSG_PUB.ADD;
572                 ELSE
573                     p_x_osp_order_rec.osp_order_id := l_osp_order_id;
574                     p_x_osp_order_rec.object_version_number := l_object_version_number;
575                 END IF;
576                 CLOSE osp_order_oe_csr;
577             */
578             -- jaramana End
579             ELSE
580                 FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_HEADER_INV');
581                 FND_MSG_PUB.ADD;
582             END IF;
583         END IF;
584     END IF;
585     IF FND_MSG_PUB.count_msg > 0 THEN
586        RAISE  FND_API.G_EXC_ERROR;
587        -- RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
588     END IF;
589     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
590       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'End Procedure');
591     END IF;
592     --dbms_output.put_line('Exiting : convert_order_header_val_to_id');
593 END convert_order_header_val_to_id;
594 
595 -------------------------------------------------------------------------------------------------------------
596 PROCEDURE validate_vendor(p_vendor_id  IN NUMBER) IS
597   CURSOR val_vendor_id_csr(p_vendor_id IN NUMBER) IS
598     SELECT 'X'
599       FROM po_vendors_view
600      WHERE vendor_id = p_vendor_id
601        AND enabled_flag = G_YES_FLAG
602        AND NVL(vendor_start_date_active, SYSDATE - 1) <= SYSDATE
603        AND NVL(vendor_end_date_active, SYSDATE + 1) > SYSDATE;
604   CURSOR get_vendor_cert IS
605     SELECT 'X'
606       FROM ahl_vendor_certifications_v
607      WHERE vendor_id = p_vendor_id
608        AND TRUNC(active_start_date) <= TRUNC(SYSDATE)
609        AND TRUNC(nvl(active_end_date, SYSDATE+1)) > TRUNC(SYSDATE);
610     l_exist VARCHAR2(1);
611     L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.validate_vendor';
612 BEGIN
613     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
614       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Procedure');
615     END IF;
616     IF(p_vendor_id IS NOT NULL AND p_vendor_id <> FND_API.G_MISS_NUM) THEN
617        OPEN val_vendor_id_csr(p_vendor_id);
618        FETCH val_vendor_id_csr INTO l_exist;
619        IF(val_vendor_id_csr%NOTFOUND) THEN
620           FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENDOR_ID_INV');
621           FND_MESSAGE.Set_Token('VENDOR_ID', p_vendor_id);
622           FND_MSG_PUB.ADD;
623        END IF;
624        CLOSE val_vendor_id_csr;
625        OPEN get_vendor_cert;
626        FETCH get_vendor_cert INTO l_exist;
627        IF(get_vendor_cert%NOTFOUND) THEN
628           FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENDOR_ID_INV');
629           FND_MESSAGE.Set_Token('VENDOR_ID', p_vendor_id);
630           FND_MSG_PUB.ADD;
631        END IF;
632        CLOSE get_vendor_cert;
633     END IF;
634     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
635       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'End Procedure');
636     END IF;
637 END validate_vendor;
638 
639 -------------------------------------------------------------------------------------------------------------
640 PROCEDURE validate_vendor_site(p_vendor_id IN NUMBER, p_vendor_site_id IN NUMBER) IS
641   CURSOR val_vendor_site_id_csr(p_vendor_id IN NUMBER, p_vendor_site_id IN NUMBER) IS
642     SELECT 'x' FROM po_vendor_sites
643      WHERE vendor_site_id = p_vendor_site_id
644        AND vendor_id = p_vendor_id
645        AND NVL(inactive_date, SYSDATE + 1) > SYSDATE
646        AND purchasing_site_flag = G_YES_FLAG
647        AND NVL(RFQ_ONLY_SITE_FLAG, G_NO_FLAG) = G_NO_FLAG;
648   CURSOR get_vendor_cert IS
649     SELECT 'X'
650       FROM ahl_vendor_certifications_v
651      WHERE vendor_id = p_vendor_id
652        AND vendor_site_id = p_vendor_site_id
653        AND TRUNC(active_start_date) <= TRUNC(SYSDATE)
654        AND TRUNC(nvl(active_end_date, SYSDATE+1)) > TRUNC(SYSDATE);
655   l_exist VARCHAR2(1);
656   L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.validate_vendor_site';
657 BEGIN
658     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
659       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Procedure');
660     END IF;
661     IF (p_vendor_id IS NULL OR p_vendor_id = FND_API.G_MISS_NUM) THEN
662         IF(p_vendor_site_id IS NOT NULL AND p_vendor_site_id <> FND_API.G_MISS_NUM) THEN
663            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VEND_ID_NLL');
664            FND_MESSAGE.Set_Token('VENDOR_SITE_ID', p_vendor_site_id);
665            FND_MSG_PUB.ADD;
666         END IF;
667     ELSE
668         IF(p_vendor_site_id IS NOT NULL AND p_vendor_site_id <> FND_API.G_MISS_NUM)THEN
669            OPEN val_vendor_site_id_csr(p_vendor_id, p_vendor_site_id);
670            FETCH val_vendor_site_id_csr INTO l_exist;
671            IF(val_vendor_site_id_csr%NOTFOUND) THEN
672               FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENSITE_ID_INV');
673               FND_MESSAGE.Set_Token('VENDOR_SITE_ID', p_vendor_site_id);
674               FND_MSG_PUB.ADD;
675            END IF;
676            CLOSE val_vendor_site_id_csr;
677            OPEN get_vendor_cert;
678            FETCH get_vendor_cert INTO l_exist;
679            IF(get_vendor_cert%NOTFOUND) THEN
680               FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENSITE_ID_INV');
681               FND_MESSAGE.Set_Token('VENDOR_SITE_ID', p_vendor_site_id);
682               FND_MSG_PUB.ADD;
683            END IF;
684            CLOSE get_vendor_cert;
685        END IF;
686     END IF;
687     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
688       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'End Procedure');
689     END IF;
690 END validate_vendor_site;
691 
692 -------------------------------------------------------------------------------------------------------------
693 PROCEDURE validate_customer(
694     p_customer_id IN NUMBER
695     )IS
696     CURSOR val_customer_id_csr(p_customer_id IN NUMBER) IS
697     SELECT 'x' from hz_parties
698     where party_id = p_customer_id and party_type = 'ORGANIZATION';
699     l_exist VARCHAR2(1);
700     L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.validate_customer';
701 BEGIN
702     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
703       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Procedure');
704     END IF;
705     IF(p_customer_id IS NOT NULL AND p_customer_id <> FND_API.G_MISS_NUM) THEN
706        OPEN val_customer_id_csr(p_customer_id);
707        FETCH val_customer_id_csr INTO l_exist;
708        IF(val_customer_id_csr%NOTFOUND) THEN
709           FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_CUST_ID_INV');
710           FND_MESSAGE.Set_Token('CUSTOMER_ID', p_customer_id);
711           FND_MSG_PUB.ADD;
712        END IF;
713        CLOSE val_customer_id_csr;
714     END IF;
715     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
716       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'End Procedure');
717     END IF;
718 END validate_customer;
719 
720 -------------------------------------------------------------------------------------------------------------
721 PROCEDURE validate_buyer(
722     p_po_agent_id IN NUMBER
723     )IS
724     CURSOR val_po_agent_id_csr(p_po_agent_id IN NUMBER) IS
725     SELECT 'x' FROM po_agents_name_v
726     WHERE buyer_id = p_po_agent_id;
727     l_exist VARCHAR2(1);
728     L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.validate_buyer';
729 BEGIN
730     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
731       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Procedure');
732     END IF;
733     IF(p_po_agent_id IS NOT NULL AND p_po_agent_id <> FND_API.G_MISS_NUM) THEN
734        OPEN val_po_agent_id_csr(p_po_agent_id);
735        FETCH val_po_agent_id_csr INTO l_exist;
736        IF(val_po_agent_id_csr%NOTFOUND) THEN
737           FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_BUYER_ID_INV');
738           FND_MESSAGE.Set_Token('BUYER_ID', p_po_agent_id);
739           FND_MSG_PUB.ADD;
740        END IF;
741        CLOSE val_po_agent_id_csr;
742     END IF;
743     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
744       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'End Procedure');
745     END IF;
746 END validate_buyer;
747 
748 -------------------------------------------------------------------------------------------------------------
749 PROCEDURE validate_contract(
750     p_order_type_code IN VARCHAR2,
751     p_contract_id IN VARCHAR2,
752     p_party_vendor_id IN VARCHAR2,
753     p_authoring_org_id IN VARCHAR2
754     )IS
755     CURSOR val_contract_id_csr(p_contract_id IN NUMBER,p_party_vendor_id in NUMBER,
756                            p_buy_or_sell IN VARCHAR2, p_authoring_org_id IN NUMBER, p_object_code IN VARCHAR2) IS
757     SELECT 'x'  FROM okc_k_headers_b chr, okc_k_party_roles_b cpl, okc_statuses_b sts
758     WHERE chr.id = p_contract_id
759       AND cpl.object1_id1 = p_party_vendor_id
760       AND chr.authoring_org_id = p_authoring_org_id
761       AND chr.id = cpl.chr_id AND chr.buy_or_sell = p_buy_or_sell AND chr.sts_code = sts.code AND sts.ste_code in ('ACTIVE', 'SIGNED')
762       AND cpl.jtot_object1_code = p_object_code;
763     l_exist VARCHAR2(1);
764     l_buy_or_sell VARCHAR2(1);
765     l_object_code VARCHAR2(30);
766     L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.validate_contract';
767 BEGIN
768      IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
769       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Procedure');
770     END IF;
771      IF(p_order_type_code = G_OSP_ORDER_TYPE_BORROW) THEN
772         l_buy_or_sell := 'B';
773         l_object_code := 'OKX_VENDOR';
774      ELSIF (p_order_type_code = G_OSP_ORDER_TYPE_LOAN) THEN
775         l_buy_or_sell := 'S';
776         l_object_code := 'OKX_PARTY';
777      ELSE
778         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_CTRCT');
779         FND_MSG_PUB.ADD;
780         RETURN;
781      END IF;
782      IF(p_party_vendor_id IS NULL OR p_party_vendor_id = FND_API.G_MISS_NUM) THEN
783         IF(p_contract_id IS NOT NULL AND p_contract_id <> FND_API.G_MISS_NUM) THEN
784            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VEND_PTY_ID_NLL');
785            FND_MESSAGE.Set_Token('CONTRACT_ID', p_contract_id);
786            FND_MSG_PUB.ADD;
787         END IF;
788      ELSE
789         IF(p_contract_id IS NOT NULL AND p_contract_id <> FND_API.G_MISS_NUM) THEN
790            OPEN val_contract_id_csr(p_contract_id, p_party_vendor_id, l_buy_or_sell, p_authoring_org_id, l_object_code);
791            FETCH val_contract_id_csr INTO l_exist;
792            IF(val_contract_id_csr%NOTFOUND) THEN
793               FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_CTRCT');
794               FND_MESSAGE.Set_Token('CONTRACT_ID', p_contract_id);
795               FND_MSG_PUB.ADD;
796            END IF;
797            CLOSE val_contract_id_csr;
798         END IF;
799      END IF;
800     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
801       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'End Procedure');
802     END IF;
803 END validate_contract;
804 
805 -------------------------------------------------------------------------------------------------------------
806 PROCEDURE validate_po_header(
807     p_osp_order_id IN NUMBER,
808     p_po_header_id IN NUMBER
809     )IS
810     CURSOR val_po_header_id_csr(p_po_header_id IN NUMBER,p_osp_order_id IN NUMBER) IS
811     SELECT 'x' FROM po_headers_all
812     WHERE po_header_id = p_po_header_id
813      AND reference_num = p_osp_order_id
814      AND interface_source_code = G_APP_NAME;
815     l_exist VARCHAR2(1);
816     L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.validate_po_header';
817 BEGIN
818     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
819       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Procedure');
820     END IF;
821     IF(p_osp_order_id IS NULL OR p_osp_order_id = FND_API.G_MISS_NUM) THEN
822        FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_HEADER_INV');
823        FND_MSG_PUB.ADD;
824        RETURN;
825     END IF;
826     IF(p_po_header_id IS NOT NULL AND p_po_header_id <> FND_API.G_MISS_NUM) THEN
827        OPEN val_po_header_id_csr(p_po_header_id, p_osp_order_id);
828        FETCH val_po_header_id_csr INTO l_exist;
829        IF(val_po_header_id_csr%NOTFOUND) THEN
830           FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_PO_HEADER_INV');
831           FND_MESSAGE.Set_Token('PO_HEADER_ID', p_po_header_id);
832           FND_MSG_PUB.ADD;
833        END IF;
834        CLOSE val_po_header_id_csr;
835     END IF;
836     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
837       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'End Procedure');
838     END IF;
839 END validate_po_header;
840 
841 --------------------------------------------------------------------------------------------------------------
842 -- validates osp header information for different order types and DML operations.
843 -- Steps :
844 -- Order Type Code can be Service, Loan , Borrow
845 -- Opreation Flag can be 'C', 'U', 'D' or null
846 -- IF not 'C', record should exist and osp_order_id and object version number should match in AHL_OSP_ORDERS_B table.
847 -- IF 'C', osp_order_id and object version number should be null.
848 -- IMPORTANT NOTE: This procedure is NOT being called for Order Header Creation any more.
849 -- Added by jaramana on January 8, 2008 for the Requisition ER 6034236
850 -- Also Note that operation_flag will never be NULL in the call to this API, as if it is null, then there is no updation
851 -- that needs to be performed on the Order Header, hence no validation is necessary.
852 --------------------------------------------------------------------------------------------------------------
853 PROCEDURE validate_order_header(
854     p_x_osp_order_rec  IN OUT NOCOPY    osp_order_rec_type)
855 IS
856     CURSOR osp_order_csr(p_osp_order_id IN NUMBER, p_object_version_number IN NUMBER) IS
857     -- po_req_header_id added by jaramana on January 8, 2008 for the Requisition ER 6034236
858     SELECT  osp_order_number, order_type_code, single_instance_flag, po_header_id, oe_header_id,vendor_id, vendor_site_id,
859         customer_id,order_date,contract_id,contract_terms,operating_unit_id, po_synch_flag, status_code,
860         po_batch_id, po_request_id,po_agent_id, po_interface_header_id, po_req_header_id , description, vendor_contact_id
861     -- jaramana End
862     FROM ahl_osp_orders_vl
863     WHERE osp_order_id = p_osp_order_id
864     AND object_version_number = p_object_version_number;
865 
866     -- Added by jaramana on January 8, 2008 for the Requisition ER 6034236
867     CURSOR chk_requisition_exists_csr(c_osp_order_id IN NUMBER) IS
868       SELECT POREQ.SEGMENT1
869         FROM PO_REQUISITION_HEADERS_ALL POREQ, AHL_OSP_ORDERS_B OSP
870         WHERE POREQ.INTERFACE_SOURCE_LINE_ID = c_osp_order_id
871           AND OSP.OSP_ORDER_ID = c_osp_order_id
872           AND OSP.OPERATING_UNIT_ID = POREQ.ORG_ID
873           AND POREQ.INTERFACE_SOURCE_CODE = AHL_GLOBAL.AHL_APP_SHORT_NAME
874 	  AND NVL(POREQ.CLOSED_CODE, 'X') NOT IN ('CANCELLED', 'CLOSED', 'CLOSED FOR INVOICE', 'CLOSED FOR RECEIVING',
875              'FINALLY CLOSED', 'REJECTED', 'RETURNED');
876 
877     l_req_num VARCHAR2(20);
878 
879     l_osp_order_rec osp_order_rec_type;
880     l_operating_unit_id NUMBER;
881     l_new_order_number NUMBER;
882     l_del_cancel_so_lines_tbl del_cancel_so_lines_tbl_type;
883     L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.validate_order_header';
884 BEGIN
885     --dbms_output.put_line('Entering : validate_order_header');
886     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
887       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Procedure');
888     END IF;
889     l_operating_unit_id := mo_global.get_current_org_id();
890     IF (l_operating_unit_id IS NULL) THEN
891         FND_MESSAGE.Set_Name('AHL', 'AHL_OSP_ORG_NOT_SET');
892         FND_MSG_PUB.ADD;
893     END IF;
894     IF FND_MSG_PUB.count_msg > 0 THEN
895        RAISE  FND_API.G_EXC_ERROR;
896        -- RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
897     END IF;
898     -- Note added by jaramana on January 8, 2008 for the Requisition ER 6034236
899     -- Note that p_x_osp_order_rec.operation_flag will never be NULL in the call to this API. As an operation_flag of NULL
900     -- means, there need to be no change in the osp_order_header, hence no validation is necessary.
901     IF(p_x_osp_order_rec.operation_flag IS NULL OR p_x_osp_order_rec.operation_flag <> G_OP_CREATE) THEN
902         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
903             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY || '.notCreate', 'operation is not create, it is:'||p_x_osp_order_rec.operation_flag);
904         END IF;
905         IF(p_x_osp_order_rec.osp_order_id IS NULL OR p_x_osp_order_rec.object_version_number IS NULL) THEN
906            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_ID_OBJ_MISS');
907            FND_MSG_PUB.ADD;
908         ELSE
909             -- fetch record and verify the latest record is being dealt with.
910             OPEN osp_order_csr(p_x_osp_order_rec.osp_order_id, p_x_osp_order_rec.object_version_number);
911             -- jaramana modified on January 8, 2008 for the Requisition ER 6034236 (Added po_req_header_id)
912             FETCH osp_order_csr INTO l_osp_order_rec.osp_order_number, l_osp_order_rec.order_type_code,l_osp_order_rec.single_instance_flag,
913                   l_osp_order_rec.po_header_id, l_osp_order_rec.oe_header_id,l_osp_order_rec.vendor_id,
914                   l_osp_order_rec.vendor_site_id, l_osp_order_rec.customer_id, l_osp_order_rec.order_date,
915                   l_osp_order_rec.contract_id, l_osp_order_rec.contract_terms, l_osp_order_rec.operating_unit_id,
916                   l_osp_order_rec.po_synch_flag, l_osp_order_rec.status_code, l_osp_order_rec.po_batch_id, l_osp_order_rec.po_request_id,
917                   l_osp_order_rec.po_agent_id, l_osp_order_rec.po_interface_header_id, l_osp_order_rec.po_req_header_id, l_osp_order_rec.description, l_osp_order_rec.vendor_contact_id;
918             -- jaramana End
919             IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
920               FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'osp_order_id='||p_x_osp_order_rec.osp_order_id|| 'ovn='||p_x_osp_order_rec.object_version_number);
921             END IF;
922             -- if record not found, raise error and declare that record has been modified.
923             IF (osp_order_csr%NOTFOUND) THEN
924                 FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INVOP_OSP_NFOUND');
925                 FND_MSG_PUB.ADD;
926             END IF;
927             CLOSE osp_order_csr;
928             -- check existing status. If closed, raise error.
929             IF(l_osp_order_rec.status_code = G_OSP_CLOSED_STATUS) THEN
930                FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_STATUS');
931                FND_MSG_PUB.ADD;
932             ELSE
933                 g_old_status_code := l_osp_order_rec.status_code;
934 	                --g_order_status_for_update := l_osp_order_rec.status_code;
935                 g_old_type_code := l_osp_order_rec.order_type_code;
936             END IF;
937 
938             -- Added by jaramana on January 8, 2008 for the Requisition ER 6034236
939             -- User should not pass G_OSP_REQ_SUBMITTED_STATUS directly. User need to pass G_OSP_SUBMITTED_STATUS and
940             -- depening on the 'Initialize Purchase Requisition' profile we deduce whether Requisition needs to be created
941             IF(p_x_osp_order_rec.status_code is not null AND l_osp_order_rec.status_code <> G_OSP_REQ_SUBMITTED_STATUS AND p_x_osp_order_rec.status_code = G_OSP_REQ_SUBMITTED_STATUS) THEN
942                FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_STATUS');
943                FND_MSG_PUB.ADD;
944             END IF;
945 
946             --If the Profile 'Initialize Purchase Requisition' is set, then set the status to G_OSP_REQ_SUBMITTED_STATUS
947             --if the passed status is G_OSP_SUBMITTED_STATUS and the old status is not G_OSP_SUBMITTED_STATUS
948             IF (NVL(FND_PROFILE.VALUE('AHL_OSP_INIT_PO_REQ'), 'N') = 'Y') THEN
949               IF(p_x_osp_order_rec.order_type_code IN (G_OSP_ORDER_TYPE_SERVICE, G_OSP_ORDER_TYPE_EXCHANGE) AND
950                             (p_x_osp_order_rec.status_code is not null and p_x_osp_order_rec.status_code = G_OSP_SUBMITTED_STATUS and
951                              l_osp_order_rec.status_code <> G_OSP_SUBMITTED_STATUS)) THEN
952                 p_x_osp_order_rec.status_code := G_OSP_REQ_SUBMITTED_STATUS;
953               END IF;
954             END IF;
955             -- jaramana End
956 
957             -- check whether order_type_code is same -- . IF null, default it.
958             IF(p_x_osp_order_rec.order_type_code IS NOT NULL AND p_x_osp_order_rec.order_type_code <> l_osp_order_rec.order_type_code) THEN
959                --item exchange enhancement
960                --only service/exchange ordertype conversion are allowed
961                IF(p_x_osp_order_rec.order_type_code IN ( G_OSP_ORDER_TYPE_SERVICE, G_OSP_ORDER_TYPE_EXCHANGE)
962                   AND l_osp_order_rec.order_type_code IN ( G_OSP_ORDER_TYPE_SERVICE, G_OSP_ORDER_TYPE_EXCHANGE)) THEN
963                      IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
964                         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY || '.notCreate', 'Set conversion Flag to Yes');
965                      END IF;
966                      -- Commented out by jaramana on January 8, 2008 for the Requisition ER 6034236
967                      -- g_order_conversion_flag := G_YES_FLAG;
968                      -- jaramana End
969                      g_old_type_code := l_osp_order_rec.order_type_code;
970                      g_new_type_code := p_x_osp_order_rec.order_type_code;
971                ELSE
972                   FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_TYPE');
973                   FND_MSG_PUB.ADD;
974                END IF;
975             ELSE
976                 p_x_osp_order_rec.order_type_code := l_osp_order_rec.order_type_code;
977             END IF;
978             -- osp order number can not be changed.
979             IF(p_x_osp_order_rec.osp_order_number IS NOT NULL AND p_x_osp_order_rec.osp_order_number <> l_osp_order_rec.osp_order_number) THEN
980                FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_NUM_INV');
981                FND_MESSAGE.Set_Token('ORDER_NUMBER', p_x_osp_order_rec.osp_order_number);
982                FND_MSG_PUB.ADD;
983             END IF;
984             -- order date can not be changed.
985             IF(p_x_osp_order_rec.order_date IS NOT NULL AND TRUNC(p_x_osp_order_rec.order_date) <> TRUNC(l_osp_order_rec.order_date)) THEN
986                FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_ORD_DT');
987                FND_MSG_PUB.ADD;
988             END IF;
989             -- operating_unit_id can not change
990             IF(p_x_osp_order_rec.operating_unit_id IS NOT NULL AND p_x_osp_order_rec.operating_unit_id <> l_osp_order_rec.operating_unit_id) THEN
991                FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_OPUNIT');
992                FND_MSG_PUB.ADD;
993             END IF;
994 
995 
996             -- Commented out by jaramana on January 8, 2008 for the Requisition ER 6034236
997             /*
998             Note that p_x_osp_order_rec.operation_flag will never be NULL in the call to this API. As an operation_flag of NULL
999             means, there need to be no change in the osp_order_header, hence no validation is necessary.
1000             Hence commenting out the following code
1001             */
1002             -- if operation_flag is NULL , default po_header_id, oe_header_id (needed  for lines)
1003             /*
1004             IF(p_x_osp_order_rec.operation_flag IS NULL) THEN
1005                p_x_osp_order_rec.po_header_id := l_osp_order_rec.po_header_id;
1006                p_x_osp_order_rec.oe_header_id := l_osp_order_rec.oe_header_id;
1007                p_x_osp_order_rec.operating_unit_id := l_osp_order_rec.operating_unit_id;
1008                p_x_osp_order_rec.single_instance_flag := l_osp_order_rec.single_instance_flag;
1009             END IF;
1010             */
1011             -- contract terms should be null.
1012             IF(p_x_osp_order_rec.contract_terms IS NOT NULL AND p_x_osp_order_rec.contract_terms <> FND_API.G_MISS_NUM) THEN
1013                FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_CO');
1014                FND_MSG_PUB.ADD;
1015            END IF;
1016            -- shipping info is not allowed to update in this API
1017            IF(p_x_osp_order_rec.oe_header_id IS NOT NULL ) THEN
1018                IF(l_osp_order_rec.oe_header_id IS NULL AND p_x_osp_order_rec.oe_header_id <> FND_API.G_MISS_NUM) THEN
1019                   FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_OE');                   FND_MSG_PUB.ADD;
1020                ELSIF(p_x_osp_order_rec.oe_header_id <> l_osp_order_rec.oe_header_id) THEN
1021                   FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_OE');
1022                   FND_MSG_PUB.ADD;
1023                END IF;
1024             END IF;
1025         END IF;
1026     -- Commented out by jaramana on January 8, 2008 for the Requisition ER 6034236
1027     --This API is not being called during the create mode any more. Hence commenting out the following
1028     --validations. Some of the fields like PO related fields are defaulted to Null ignoring the user passed inputs
1029     --and the rest of them are being validated in the create_osp_order_header API
1030     /*
1031     ELSE -- mode is create and following processing is same for all type of orders
1032 
1033         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1034             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY || '.create', 'operation is create');
1035          END IF;
1036         IF(p_x_osp_order_rec.osp_order_id IS NOT NULL AND p_x_osp_order_rec.osp_order_id <> FND_API.G_MISS_NUM) THEN
1037            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_ID_NNLL');
1038            FND_MSG_PUB.ADD;
1039         END IF;
1040         IF(p_x_osp_order_rec.object_version_number IS NOT NULL AND p_x_osp_order_rec.object_version_number <> FND_API.G_MISS_NUM) THEN
1041            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_OBJV_NNLL');
1042            FND_MSG_PUB.ADD;
1043         END IF;
1044         -- populate order number
1045         IF(p_x_osp_order_rec.osp_order_number IS NOT NULL AND p_x_osp_order_rec.osp_order_number <> FND_API.G_MISS_NUM) THEN
1046            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_NUM_INV');
1047            FND_MESSAGE.Set_Token('ORDER_NUMBER', p_x_osp_order_rec.osp_order_number);
1048            FND_MSG_PUB.ADD;
1049         END IF;
1050         -- populate order_date
1051         IF(p_x_osp_order_rec.order_date IS NOT NULL AND p_x_osp_order_rec.order_date <> FND_API.G_MISS_DATE) THEN
1052            IF(TRUNC(p_x_osp_order_rec.order_date) <> TRUNC(SYSDATE)) THEN
1053               FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_ORD_DAT');
1054               FND_MESSAGE.Set_Token('ORDER_DATE', p_x_osp_order_rec.order_date);
1055               FND_MSG_PUB.ADD;
1056            END IF;
1057         ELSE
1058            p_x_osp_order_rec.order_date := TRUNC(SYSDATE);
1059         END IF;
1060         -- populate operating_unit_id
1061         IF(p_x_osp_order_rec.operating_unit_id IS NOT NULL AND p_x_osp_order_rec.operating_unit_id <> FND_API.G_MISS_NUM) THEN
1062            IF(p_x_osp_order_rec.operating_unit_id <> l_operating_unit_id) THEN
1063               FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_OP_UNIT');
1064               FND_MSG_PUB.ADD;
1065            END IF;
1066         ELSE
1067               p_x_osp_order_rec.operating_unit_id := l_operating_unit_id;
1068         END IF;
1069         -- po_header_id, oe_header_id, po_batch_id, po_request_id, po_interface_header_id, contract_terms should be null for CREATE.
1070         IF(p_x_osp_order_rec.po_header_id IS NOT NULL AND p_x_osp_order_rec.po_header_id <> FND_API.G_MISS_NUM) THEN
1071            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_PO_NNLL');
1072            FND_MSG_PUB.ADD;
1073         END IF;
1074         IF(p_x_osp_order_rec.oe_header_id IS NOT NULL AND p_x_osp_order_rec.oe_header_id <> FND_API.G_MISS_NUM) THEN
1075            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_OE_NNLL');
1076            FND_MSG_PUB.ADD;
1077         END IF;
1078         IF(p_x_osp_order_rec.po_batch_id IS NOT NULL AND p_x_osp_order_rec.po_batch_id <> FND_API.G_MISS_NUM) THEN
1079            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_PO_NNLL');
1080            FND_MSG_PUB.ADD;
1081         END IF;
1082         IF(p_x_osp_order_rec.po_request_id IS NOT NULL AND p_x_osp_order_rec.po_request_id <> FND_API.G_MISS_NUM) THEN
1083            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_PO_NNLL');
1084            FND_MSG_PUB.ADD;
1085         END IF;
1086         IF(p_x_osp_order_rec.po_interface_header_id IS NOT NULL AND p_x_osp_order_rec.po_interface_header_id <> FND_API.G_MISS_NUM) THEN
1087            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_PO_NNLL');
1088            FND_MSG_PUB.ADD;
1089         END IF;
1090         -- status_code should be null or 'ENTERED'. IF null, default to 'ENTERED'
1091         IF(p_x_osp_order_rec.status_code IS NOT NULL AND p_x_osp_order_rec.status_code <> FND_API.G_MISS_CHAR
1092            AND p_x_osp_order_rec.status_code <> G_OSP_ENTERED_STATUS) THEN
1093            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_STATUS');
1094            FND_MSG_PUB.ADD;
1095         ELSE
1096            p_x_osp_order_rec.status_code := G_OSP_ENTERED_STATUS;
1097            g_old_status_code := G_OSP_ENTERED_STATUS;
1098            --g_order_status_for_update := G_OSP_ENTERED_STATUS;
1099         END IF;
1100         --dbms_output.put_line('Create mode defaulting done');
1101     */
1102     END IF;
1103     IF FND_MSG_PUB.count_msg > 0 THEN
1104        RAISE  FND_API.G_EXC_ERROR;
1105        -- RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1106     END IF;
1107     ----------------------------
1108     -- forcing null column rules
1109     ----------------------------
1110     -- add item exchange enhancement to if condition
1111     IF (p_x_osp_order_rec.order_type_code IN ( G_OSP_ORDER_TYPE_SERVICE, G_OSP_ORDER_TYPE_EXCHANGE))THEN
1112         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1113             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY || '.type', 'Update Order type is Service or Exchange');
1114          END IF;
1115         -- contract_id and customer_id must be null for SERVICE or EXCHANGE
1116         IF(p_x_osp_order_rec.contract_id IS NOT NULL AND p_x_osp_order_rec.contract_id <> FND_API.G_MISS_NUM) THEN
1117            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_CTRCT');
1118            FND_MESSAGE.Set_Token('CONTRACT_ID', p_x_osp_order_rec.contract_id);
1119            FND_MSG_PUB.ADD;
1120         END IF;
1121         IF(p_x_osp_order_rec.customer_id IS NOT NULL AND p_x_osp_order_rec.customer_id <> FND_API.G_MISS_NUM) THEN
1122            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_CUST_ID_INV');
1123            FND_MESSAGE.Set_Token('CUSTOMER_ID', p_x_osp_order_rec.customer_id);
1124            FND_MSG_PUB.ADD;
1125         END IF;
1126         IF(p_x_osp_order_rec.contract_terms IS NOT NULL AND p_x_osp_order_rec.contract_terms <> FND_API.G_MISS_NUM) THEN
1127            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_CO');
1128            FND_MSG_PUB.ADD;
1129         END IF;
1130     ELSIF (p_x_osp_order_rec.order_type_code IN( G_OSP_ORDER_TYPE_LOAN,G_OSP_ORDER_TYPE_BORROW) ) THEN
1131         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1132             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY || '.type', 'Update Order type is Loan or Borrow');
1133          END IF;
1134         -- po_agent_id,vendor_id(FOR LOAN only) and vendor_site_id must be null.
1135         IF(p_x_osp_order_rec.po_agent_id IS NOT NULL AND p_x_osp_order_rec.po_agent_id <> FND_API.G_MISS_NUM) THEN
1136            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_BUYER_ID_INV');
1137            FND_MESSAGE.Set_Token('BUYER_ID', p_x_osp_order_rec.po_agent_id);
1138            FND_MSG_PUB.ADD;
1139         END IF;
1140         IF(p_x_osp_order_rec.vendor_site_id IS NOT NULL AND p_x_osp_order_rec.vendor_site_id <> FND_API.G_MISS_NUM) THEN
1141            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENSITE_ID_INV');
1142            FND_MESSAGE.Set_Token('VENDOR_SITE_ID', p_x_osp_order_rec.vendor_site_id);
1143            FND_MSG_PUB.ADD;
1144         END IF;
1145         IF(p_x_osp_order_rec.order_type_code = G_OSP_ORDER_TYPE_LOAN) THEN
1146            IF(p_x_osp_order_rec.vendor_id IS NOT NULL AND p_x_osp_order_rec.vendor_id <> FND_API.G_MISS_NUM) THEN
1147               FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENDOR_ID_INV');
1148               FND_MESSAGE.Set_Token('VENDOR_ID', p_x_osp_order_rec.vendor_id);
1149               FND_MSG_PUB.ADD;
1150            END IF;
1151         END IF;
1152         -- po_header_id, po_batch_id, po_request_id, po_interface_header_id, contract_terms should be null
1153         IF(p_x_osp_order_rec.po_header_id IS NOT NULL AND p_x_osp_order_rec.po_header_id <> FND_API.G_MISS_NUM) THEN
1154            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_PO_NNLL');
1155            FND_MSG_PUB.ADD;
1156         END IF;
1157         IF(p_x_osp_order_rec.po_batch_id IS NOT NULL AND p_x_osp_order_rec.po_batch_id <> FND_API.G_MISS_NUM) THEN
1158            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_PO_NNLL');
1159            FND_MSG_PUB.ADD;
1160         END IF;
1161         IF(p_x_osp_order_rec.po_request_id IS NOT NULL AND p_x_osp_order_rec.po_request_id <> FND_API.G_MISS_NUM) THEN
1162            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_PO_NNLL');
1163            FND_MSG_PUB.ADD;
1164         END IF;
1165         IF(p_x_osp_order_rec.po_interface_header_id IS NOT NULL AND p_x_osp_order_rec.po_interface_header_id <> FND_API.G_MISS_NUM) THEN
1166            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_PO_NNLL');
1167            FND_MSG_PUB.ADD;
1168         END IF;
1169 
1170         -- Added by jaramana on January 8, 2008 for the Requisition ER 6034236
1171         IF(p_x_osp_order_rec.po_req_header_id IS NOT NULL AND p_x_osp_order_rec.po_req_header_id <> FND_API.G_MISS_NUM) THEN
1172            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_REQ_NNLL');
1173            FND_MSG_PUB.ADD;
1174         END IF;
1175         -- jaramana End
1176 
1177         IF(p_x_osp_order_rec.contract_terms IS NOT NULL AND p_x_osp_order_rec.contract_terms <> FND_API.G_MISS_NUM) THEN
1178            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_CO');
1179            FND_MSG_PUB.ADD;
1180         END IF;
1181 
1182     END IF;
1183     --dbms_output.put_line('ensured null columns');
1184     IF FND_MSG_PUB.count_msg > 0 THEN
1185        RAISE  FND_API.G_EXC_ERROR;
1186        -- RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1187     END IF;
1188     IF (p_x_osp_order_rec.order_type_code IN ( G_OSP_ORDER_TYPE_SERVICE, G_OSP_ORDER_TYPE_EXCHANGE)) THEN  --item exchange enhancement add G_OSP_ORDER_TYPE_EXCHANGE condition
1189         -- Commented out by jaramana on January 8, 2008 for the Requisition ER 6034236
1190         -- This API is not being called during the create mode any more.
1191         -- Hence commenting out the following validations.
1192 
1193         /*
1194         IF(p_x_osp_order_rec.operation_flag = G_OP_CREATE) THEN
1195            IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1196             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY || '.type.oper', 'Update Order type is Service or Exchange Operation is Create');
1197            END IF;
1198            --dbms_output.put_line('validate single_instance_flag');
1199            --set single_instance_flag
1200            IF(p_x_osp_order_rec.single_instance_flag IS NULL OR p_x_osp_order_rec.single_instance_flag = FND_API.G_MISS_CHAR)THEN
1201               p_x_osp_order_rec.single_instance_flag := G_NO_FLAG;
1202            ELSIF (p_x_osp_order_rec.single_instance_flag NOT IN(G_NO_FLAG,G_YES_FLAG))THEN
1203               FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_INST_FLG');
1204               FND_MESSAGE.Set_Token('INST_FLG', p_x_osp_order_rec.single_instance_flag);
1205               FND_MSG_PUB.ADD;
1206            END IF;
1207            --dbms_output.put_line('validate vendor');
1208 
1209            -- validate vendor_id.
1210            IF(p_x_osp_order_rec.vendor_id IS NULL OR p_x_osp_order_rec.vendor_id = FND_API.G_MISS_NUM) THEN
1211               FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENDOR_ID_NLL');
1212               FND_MSG_PUB.ADD;
1213            ELSE
1214               validate_vendor(p_x_osp_order_rec.vendor_id);
1215            END IF;
1216            --dbms_output.put_line('validate vendor site');
1217            -- validate vendor_site_id.
1218            IF(p_x_osp_order_rec.vendor_site_id IS NULL OR p_x_osp_order_rec.vendor_site_id = FND_API.G_MISS_NUM) THEN
1219               FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENSITE_ID_NLL');
1220               FND_MSG_PUB.ADD;
1221            ELSE
1222               validate_vendor_site(p_x_osp_order_rec.vendor_id, p_x_osp_order_rec.vendor_site_id);
1223            END IF;
1224 
1225            validate_vendor_site_contact(p_x_osp_order_rec.vendor_id,
1226                                         p_x_osp_order_rec.vendor_site_id,
1227                                         p_x_osp_order_rec.vendor_contact_id);
1228            -- validate po_agent_id.
1229            --dbms_output.put_line('validate buyer');
1230            IF(p_x_osp_order_rec.po_agent_id IS NULL OR p_x_osp_order_rec.po_agent_id = FND_API.G_MISS_NUM) THEN
1231               FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_BUYER_ID_NLL');
1232               FND_MSG_PUB.ADD;
1233            ELSE
1234               validate_buyer(p_x_osp_order_rec.po_agent_id);
1235            END IF;
1236         */
1237         -- jaramana End
1238         IF(p_x_osp_order_rec.operation_flag = G_OP_UPDATE) THEN
1239            IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1240              FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY || '.srvExch.update', 'Update Order type is Service or Exchange Operation is Update ');
1241            END IF;
1242            -- Modified by jaramana on January 9, 2008 for the Requisition ER 6034236 (Added G_OSP_REQ_SUB_FAILED_STATUS)
1243            IF(g_old_status_code IN (G_OSP_ENTERED_STATUS, G_OSP_SUB_FAILED_STATUS, G_OSP_REQ_SUB_FAILED_STATUS)) THEN
1244            -- jaramana End
1245               IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1246                 FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY || '.srvExch.update.', 'Update Order type is Service or Exchange Operation is Update'
1247                               || 'g_old_status_code' || g_old_status_code);
1248               END IF;
1249               -- po_header_id should be null.
1250               IF(p_x_osp_order_rec.po_header_id IS NOT NULL AND p_x_osp_order_rec.po_header_id <> FND_API.G_MISS_NUM) THEN
1251                 FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_PO_NNLL');
1252                 FND_MSG_PUB.ADD;
1253               END IF;
1254               -- Modified by jaramana on January 9, 2008 for the Requisition ER 6034236
1255               -- req_header_id should be null.
1256               IF(p_x_osp_order_rec.po_req_header_id IS NOT NULL AND p_x_osp_order_rec.po_req_header_id <> FND_API.G_MISS_NUM) THEN
1257                 FND_MESSAGE.Set_Name(G_APP_NAME, 'AHL_OSP_ORD_REQ_NNLL');
1258                 FND_MSG_PUB.ADD;
1259               END IF;
1260               -- jaramana End
1261 
1262               -- Modified by jaramana on January 9, 2008 for the Requisition ER 6034236
1263               -- status_code should be null,'ENTERED', 'SUBMISSION_FAILED' or 'SUBMITTED'
1264               /*
1265               IF(p_x_osp_order_rec.status_code IS NOT NULL AND
1266                  p_x_osp_order_rec.status_code NOT IN(G_OSP_ENTERED_STATUS, G_OSP_SUB_FAILED_STATUS, G_OSP_SUBMITTED_STATUS)) THEN
1267                  FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_STATUS');
1268                  FND_MSG_PUB.ADD;
1269               ELSIF (g_old_status_code  = G_OSP_ENTERED_STATUS AND p_x_osp_order_rec.status_code = G_OSP_SUB_FAILED_STATUS) THEN
1270                  FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_STATUS');
1271                  FND_MSG_PUB.ADD;
1272               ELSIF (g_old_status_code  = G_OSP_SUB_FAILED_STATUS AND p_x_osp_order_rec.status_code = G_OSP_ENTERED_STATUS) THEN
1273                  FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_STATUS');
1274                  FND_MSG_PUB.ADD;
1275               END IF;
1276               */
1277               /*
1278               1. If the current status is G_OSP_ENTERED_STATUS, the status cannot be changed to G_OSP_SUB_FAILED_STATUS or
1279               G_OSP_REQ_SUB_FAILED_STATUS. It can only be changed to G_OSP_SUBMITTED_STATUS or G_OSP_REQ_SUBMITTED_STATUS or
1280               it can be changed to G_OSP_ENTERED_STATUS (No update)
1281 
1282               2. If the current status is G_OSP_SUB_FAILED_STATUS, the status cannot be changed to G_OSP_ENTERED_STATUS or
1283               G_OSP_REQ_SUB_FAILED_STATUS. It can only be changed to G_OSP_SUBMITTED_STATUS or G_OSP_REQ_SUBMITTED_STATUS or
1284               it can be changed to G_OSP_SUB_FAILED_STATUS (No update)
1285 
1286               3. If the current status is G_OSP_REQ_SUB_FAILED_STATUS, the status cannot be changed to G_OSP_ENTERED_STATUS or
1287               G_OSP_SUB_FAILED_STATUS. It can only be changed to G_OSP_SUBMITTED_STATUS or G_OSP_REQ_SUBMITTED_STATUS or
1288               it can be changed to G_OSP_REQ_SUB_FAILED_STATUS (No update)
1289 
1290               In all the above the changes status is either itself or G_OSP_SUBMITTED_STATUS or G_OSP_REQ_SUBMITTED_STATUS
1291               */
1292 
1293               IF(p_x_osp_order_rec.status_code IS NOT NULL AND p_x_osp_order_rec.status_code <> g_old_status_code AND p_x_osp_order_rec.status_code NOT IN (G_OSP_SUBMITTED_STATUS,G_OSP_REQ_SUBMITTED_STATUS )) THEN
1294                  FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_STATUS');
1295                  FND_MSG_PUB.ADD;
1296               END IF;
1297 
1298               --
1299               --set single_instance_flag
1300               IF(p_x_osp_order_rec.single_instance_flag = FND_API.G_MISS_CHAR)THEN
1301                  p_x_osp_order_rec.single_instance_flag := G_NO_FLAG;
1302               ELSIF (p_x_osp_order_rec.single_instance_flag IS NOT NULL AND p_x_osp_order_rec.single_instance_flag NOT IN(G_NO_FLAG,G_YES_FLAG))THEN
1303                  FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_INST_FLG');
1304                  FND_MESSAGE.Set_Token('INST_FLG', p_x_osp_order_rec.single_instance_flag);
1305                  FND_MSG_PUB.ADD;
1306               END IF;
1307               /*
1308               -- validate vendor_id
1309               IF(p_x_osp_order_rec.vendor_id IS NOT NULL AND p_x_osp_order_rec.vendor_id = FND_API.G_MISS_NUM) THEN
1310                  FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENDOR_ID_NLL');
1311                  FND_MSG_PUB.ADD;
1312               ELSIF (p_x_osp_order_rec.vendor_id IS NOT NULL) THEN
1313                  validate_vendor(p_x_osp_order_rec.vendor_id);
1314               END IF;
1315               -- validate vendor_site_id.
1316               IF(p_x_osp_order_rec.vendor_site_id IS NOT NULL AND p_x_osp_order_rec.vendor_site_id = FND_API.G_MISS_NUM) THEN
1317                  FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENSITE_ID_NLL');
1318                  FND_MSG_PUB.ADD;
1319               ELSE
1320                  p_x_osp_order_rec.vendor_site_id := NVL(p_x_osp_order_rec.vendor_site_id,l_osp_order_rec.vendor_site_id);
1321                  p_x_osp_order_rec.vendor_id := NVL(p_x_osp_order_rec.vendor_id, l_osp_order_rec.vendor_id);
1322                  validate_vendor_site(p_x_osp_order_rec.vendor_id, p_x_osp_order_rec.vendor_site_id);
1323               END IF;
1324               */
1325               --When updating OSP order header, vendor_id is required
1326               --G_MISS/Null conversion has already been made in default_unchanged_order_header
1327               IF(p_x_osp_order_rec.vendor_id IS NULL) THEN
1328                 FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENSITE_ID_NLL');
1329                 FND_MSG_PUB.ADD;
1330               END IF;
1331 
1332               --When updating OSP order header, vendor_site_id is required
1333               --G_MISS/Null conversion has already been made in default_unchanged_order_header
1334               IF(p_x_osp_order_rec.vendor_site_id IS NULL) THEN
1335                 FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENDOR_ID_NLL');
1336                 FND_MSG_PUB.ADD;
1337               END IF;
1338 
1339               validate_vendor_site_contact(p_x_osp_order_rec.vendor_id,
1340                                            p_x_osp_order_rec.vendor_site_id,
1341                                            p_x_osp_order_rec.vendor_contact_id);
1342               -- validate po_agent_id.
1343               --dbms_output.put_line('validate buyer in update');
1344               --IF(p_x_osp_order_rec.po_agent_id IS NOT NULL AND p_x_osp_order_rec.po_agent_id = FND_API.G_MISS_NUM) THEN
1345               --G_MISS/Null conversion has already been made in default_unchanged_order_header
1346               IF(p_x_osp_order_rec.po_agent_id IS NULL) THEN
1347                  FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_BUYER_ID_NLL');
1348                  FND_MSG_PUB.ADD;
1349               ELSIF (p_x_osp_order_rec.po_agent_id IS NOT NULL) THEN
1350                  validate_buyer(p_x_osp_order_rec.po_agent_id);
1351               END IF;
1352               -- Added by jaramana on January 9, 2008 for the Requisition ER 6034236
1353               -- Prevent Resubmission if the old status is G_OSP_REQ_SUB_FAILED_STATUS
1354               -- and new status is (G_OSP_SUBMITTED_STATUS or G_OSP_REQ_SUBMITTED_STATUS)
1355               -- and if the requisition header is not yet deleted by the user.
1356               -- This is the case where the Requisition was created only for some OSP lines
1357               -- and failed for the remaining - user has to manually delete the Requisition before resubmitting.
1358               IF(g_old_status_code = G_OSP_REQ_SUB_FAILED_STATUS AND
1359                  p_x_osp_order_rec.status_code IS NOT NULL AND
1360                  p_x_osp_order_rec.status_code IN (G_OSP_SUBMITTED_STATUS,G_OSP_REQ_SUBMITTED_STATUS)) THEN
1361                  OPEN chk_requisition_exists_csr(p_x_osp_order_rec.osp_order_id);
1362                  FETCH chk_requisition_exists_csr INTO l_req_num;
1363                  IF (chk_requisition_exists_csr%FOUND) THEN
1364                    FND_MESSAGE.Set_Name(G_APP_NAME, 'AHL_OSP_REQ_NOT_DELETED');
1365                    FND_MESSAGE.Set_Token('REQ_NUM', l_req_num);
1366                    FND_MSG_PUB.ADD;
1367                  END IF;
1368                  CLOSE chk_requisition_exists_csr;
1369               END IF;
1370               -- End addition by jaramana on January 9, 2008
1371            ELSIF (g_old_status_code = G_OSP_SUBMITTED_STATUS) THEN
1372               -- status_code should be null,'SUBMITTED', 'SUBMISSION_FAILED' or 'PO_CREATED'
1373               IF(p_x_osp_order_rec.status_code IS NULL OR p_x_osp_order_rec.status_code = G_OSP_SUBMITTED_STATUS) THEN
1374                  IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1375                     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY || '.srvExch.update.', 'Update Order type is Service or Exchange Operation is Update'
1376                               || 'g_old_status_code: ' || g_old_status_code || 'new status: ' || p_x_osp_order_rec.status_code );
1377                   END IF;
1378                  -- po_header_id should be null.
1379                  IF(p_x_osp_order_rec.po_header_id IS NOT NULL AND p_x_osp_order_rec.po_header_id <> FND_API.G_MISS_NUM) THEN
1380                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_PO_NNLL');
1381                     FND_MSG_PUB.ADD;
1382                  END IF;
1383 
1384                 -- Added by jaramana on January 9, 2008 for the Requisition ER 6034236
1385                 -- req_header_id should be null.
1386                 IF(p_x_osp_order_rec.po_req_header_id IS NOT NULL AND p_x_osp_order_rec.po_req_header_id <> FND_API.G_MISS_NUM) THEN
1387                   FND_MESSAGE.Set_Name(G_APP_NAME, 'AHL_OSP_ORD_REQ_NNLL');
1388                   FND_MSG_PUB.ADD;
1389                 END IF;
1390                 -- jaramana End
1391 
1392                  -- single_instance_flag cant change
1393                  IF(p_x_osp_order_rec.single_instance_flag IS NOT NULL AND p_x_osp_order_rec.single_instance_flag <> l_osp_order_rec.single_instance_flag) THEN
1394                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_INST_FLG_CHG');
1395                     FND_MSG_PUB.ADD;
1396                  END IF;
1397                  -- vendor_id cant change
1398                  IF(p_x_osp_order_rec.vendor_id IS NOT NULL AND p_x_osp_order_rec.vendor_id <> l_osp_order_rec.vendor_id) THEN
1399                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_VEN_CHG');
1400                     FND_MSG_PUB.ADD;
1401                  END IF;
1402                  -- vendor_site_id cant change
1403                  IF(p_x_osp_order_rec.vendor_site_id IS NOT NULL AND p_x_osp_order_rec.vendor_site_id <> l_osp_order_rec.vendor_site_id) THEN
1404                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_VEN_STE_CHG');
1405                     FND_MSG_PUB.ADD;
1406                  END IF;
1407 
1408                  -- Added by jaramana on January 9, 2008 for the Requisition ER 6034236
1409                  -- This should have been present with ISO changes. Adding now.
1410                  -- vendor_contact_id cant change
1411                  IF(p_x_osp_order_rec.vendor_contact_id IS NOT NULL AND p_x_osp_order_rec.vendor_contact_id <> l_osp_order_rec.vendor_contact_id) THEN
1412                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_VEN_CTCT_CHG ');
1413                     FND_MSG_PUB.ADD;
1414                  END IF;
1415                  -- jaramana End
1416 
1417                  -- Buyer cant change
1418                  IF(p_x_osp_order_rec.po_agent_id IS NOT NULL AND p_x_osp_order_rec.po_agent_id <> l_osp_order_rec.po_agent_id) THEN
1419                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_BUYER_CHG');
1420                     FND_MSG_PUB.ADD;
1421                  END IF;
1422               -- jaramana modified on January 9, 2008 for the Requisition ER 6034236
1423               --The transitions from G_OSP_SUBMITTED_STATUS -> G_OSP_SUB_FAILED_STATUS and
1424               --G_OSP_SUBMITTED_STATUS -> G_OSP_PO_CREATED_STATUS are not done from this API and are handled by the PO Synch
1425               --API. Hence removing these tranisitions from here. If at all these transitions take place, we are throwing a
1426               --validation error.
1427               /*
1428               ELSIF (p_x_osp_order_rec.status_code = G_OSP_SUB_FAILED_STATUS) THEN
1429                  -- po_header_id should be null.
1430                  IF(p_x_osp_order_rec.po_header_id IS NOT NULL AND p_x_osp_order_rec.po_header_id <> FND_API.G_MISS_NUM) THEN
1431                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_PO_NNLL');
1432                     FND_MSG_PUB.ADD;
1433                  END IF;
1434                  --set single_instance_flag
1435                  IF(p_x_osp_order_rec.single_instance_flag = FND_API.G_MISS_CHAR)THEN
1436                     p_x_osp_order_rec.single_instance_flag := G_NO_FLAG;
1437                  ELSIF (p_x_osp_order_rec.single_instance_flag IS NOT NULL AND p_x_osp_order_rec.single_instance_flag NOT IN(G_NO_FLAG,G_YES_FLAG))THEN
1438                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_INST_FLG');
1439                     FND_MESSAGE.Set_Token('INST_FLG', p_x_osp_order_rec.single_instance_flag);
1440                     FND_MSG_PUB.ADD;
1441                  END IF;
1442                 */
1443                 /*
1444                  -- validate vendor_id.
1445                 IF(p_x_osp_order_rec.vendor_id IS NOT NULL AND p_x_osp_order_rec.vendor_id = FND_API.G_MISS_NUM) THEN
1446                    FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENDOR_ID_NLL');
1447                    FND_MSG_PUB.ADD;
1448                 ELSIF (p_x_osp_order_rec.vendor_id IS NOT NULL) THEN
1449                    validate_vendor(p_x_osp_order_rec.vendor_id);
1450                 END IF;
1451                 -- validate vendor_site_id.
1452                 IF(p_x_osp_order_rec.vendor_site_id IS NOT NULL AND p_x_osp_order_rec.vendor_site_id = FND_API.G_MISS_NUM) THEN
1453                    FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENSITE_ID_NLL');
1454                    FND_MSG_PUB.ADD;
1455                 ELSE
1456                    p_x_osp_order_rec.vendor_site_id := NVL(p_x_osp_order_rec.vendor_site_id,l_osp_order_rec.vendor_site_id);
1457                    p_x_osp_order_rec.vendor_id := NVL(p_x_osp_order_rec.vendor_id, l_osp_order_rec.vendor_id);
1458                    validate_vendor_site(p_x_osp_order_rec.vendor_id, p_x_osp_order_rec.vendor_site_id);
1459                 END IF;
1460                 */
1461                 /*
1462                 validate_vendor_site_contact(p_x_osp_order_rec.vendor_id,
1463                                              p_x_osp_order_rec.vendor_site_id,
1464                                              p_x_osp_order_rec.vendor_contact_id);
1465                 -- validate po_agent_id.
1466                 --dbms_output.put_line('validate buyer in update');
1467                 IF(p_x_osp_order_rec.po_agent_id IS NOT NULL AND p_x_osp_order_rec.po_agent_id = FND_API.G_MISS_NUM) THEN
1468                    FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_BUYER_ID_NLL');
1469                    FND_MSG_PUB.ADD;
1470                 ELSIF (p_x_osp_order_rec.po_agent_id IS NOT NULL) THEN
1471                    validate_buyer(p_x_osp_order_rec.po_agent_id);
1472                 END IF;
1473                  -- set status_code for lines
1474                  g_order_status_for_update := G_OSP_SUB_FAILED_STATUS;
1475               ELSIF (p_x_osp_order_rec.status_code = G_OSP_PO_CREATED_STATUS) THEN
1476                  IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1477                     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY || '.srvExch.update.', 'Update Order type is Service or Exchange Operation is Update'
1478                               || 'g_old_status_code: ' || g_old_status_code || 'new status: ' || p_x_osp_order_rec.status_code );
1479                   END IF;
1480                  -- po_header_id should be not be null.
1481                  IF(p_x_osp_order_rec.po_header_id IS NULL OR p_x_osp_order_rec.po_header_id = FND_API.G_MISS_NUM) THEN
1482                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_PO_NLL');
1483                     FND_MSG_PUB.ADD;
1484                  ELSE
1485                     validate_po_header(p_x_osp_order_rec.osp_order_id,p_x_osp_order_rec.po_header_id);
1486                  END IF;
1487                  -- single_instance_flag cant change
1488                  IF(p_x_osp_order_rec.single_instance_flag IS NOT NULL AND p_x_osp_order_rec.single_instance_flag <> l_osp_order_rec.single_instance_flag) THEN
1489                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_INST_FLG_CHG');
1490                     FND_MSG_PUB.ADD;
1491                  END IF;
1492                  --PO Synch enhancement  vendor id and vendor sitecode can change if the change come from PO
1493                  -- vendor_id cannot change unless it was change in PO
1494                  IF(p_x_osp_order_rec.vendor_id IS NOT NULL AND p_x_osp_order_rec.vendor_id <> l_osp_order_rec.vendor_id) THEN
1495                     IF( vendor_id_exist_in_PO(l_osp_order_rec.po_header_id, p_x_osp_order_rec.vendor_id) = false) THEN
1496                          FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_VEN_CHG');
1497                          FND_MSG_PUB.ADD;
1498                      END IF;
1499                  END IF;
1500                  -- vendor_site_id cannot change unless it was changed in PO
1501                  IF(p_x_osp_order_rec.vendor_site_id IS NOT NULL AND p_x_osp_order_rec.vendor_site_id <> l_osp_order_rec.vendor_site_id) THEN
1502                     IF( vendor_site_id_exist_in_PO(l_osp_order_rec.po_header_id, p_x_osp_order_rec.vendor_site_id) = false) THEN
1503                        FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_VEN_STE_CHG');
1504                        FND_MSG_PUB.ADD;
1505                      END IF;
1506                  END IF;
1507                  -- Buyer cant change
1508                  IF(p_x_osp_order_rec.po_agent_id IS NOT NULL AND p_x_osp_order_rec.po_agent_id <> l_osp_order_rec.po_agent_id) THEN
1509                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_BUYER_CHG');
1510                     FND_MSG_PUB.ADD;
1511                  END IF;
1512                  -- set status_code for lines
1513                  g_order_status_for_update := G_OSP_PO_CREATED_STATUS;
1514               */
1515               -- jaramana End
1516               ELSE
1517                  FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_STATUS');
1518                  FND_MSG_PUB.ADD;
1519               END IF;
1520 
1521            ELSIF (g_old_status_code = G_OSP_PO_CREATED_STATUS) THEN
1522               IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1523                     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY || '.srvExch.update.', 'Update Order type is Service or Exchange Operation is Update'
1524                               || 'g_old_status_code: ' || g_old_status_code);
1525               END IF;
1526               -- status_code should be null, 'PO_CREATED' or 'CLOSED'
1527               IF(p_x_osp_order_rec.status_code IS NULL OR p_x_osp_order_rec.status_code IN( G_OSP_PO_CREATED_STATUS,G_OSP_CLOSED_STATUS)) THEN
1528                  -- po_header_id cant change
1529                  IF(p_x_osp_order_rec.po_header_id IS NOT NULL AND p_x_osp_order_rec.po_header_id <> l_osp_order_rec.po_header_id) THEN
1530                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_PO_CHG');
1531                     FND_MSG_PUB.ADD;
1532                  END IF;
1533 
1534                  -- jaramana modified on January 9, 2008 for the Requisition ER 6034236
1535                  -- req_header_id should be null.
1536                  IF(p_x_osp_order_rec.po_req_header_id IS NOT NULL AND p_x_osp_order_rec.po_req_header_id <> FND_API.G_MISS_NUM) THEN
1537                    FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_REQ_NNLL');
1538                    FND_MSG_PUB.ADD;
1539                  END IF;
1540                  -- jaramana End
1541 
1542                  -- single_instance_flag cant change
1543                  IF(p_x_osp_order_rec.single_instance_flag IS NOT NULL AND p_x_osp_order_rec.single_instance_flag <> l_osp_order_rec.single_instance_flag) THEN
1544                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_INST_FLG_CHG');
1545                     FND_MSG_PUB.ADD;
1546                  END IF;
1547                  /*
1548                  --PO Synch enhancement  vendor id and vendor sitecode can change if the change come from PO
1549                  -- vendor_id cant change
1550                  IF(p_x_osp_order_rec.vendor_id IS NOT NULL AND p_x_osp_order_rec.vendor_id <> l_osp_order_rec.vendor_id) THEN
1551                     IF( vendor_id_exist_in_PO(l_osp_order_rec.po_header_id, p_x_osp_order_rec.vendor_id) = false) THEN
1552                       FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_VEN_CHG');
1553                       FND_MSG_PUB.ADD;
1554                     END IF;
1555                  END IF;
1556                  -- vendor_site_id cant change
1557                  IF(p_x_osp_order_rec.vendor_site_id IS NOT NULL AND p_x_osp_order_rec.vendor_site_id <> l_osp_order_rec.vendor_site_id) THEN
1558                    IF( vendor_site_id_exist_in_PO(l_osp_order_rec.po_header_id, p_x_osp_order_rec.vendor_site_id) = false) THEN
1559                       FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_VEN_STE_CHG');
1560                       FND_MSG_PUB.ADD;
1561                    END IF;
1562                  END IF;
1563                  */
1564                  -- Changes by jaramana on January 9, 2008 for the Requisition ER 6034236
1565                  -- We are not planning to support this PO attribute continuous synch anymore.
1566 
1567                  -- vendor_id cant change
1568                  IF(p_x_osp_order_rec.vendor_id IS NOT NULL AND p_x_osp_order_rec.vendor_id <> l_osp_order_rec.vendor_id) THEN
1569                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_VEN_CHG');
1570                     FND_MSG_PUB.ADD;
1571                  END IF;
1572                  -- vendor_site_id cant change
1573                  IF(p_x_osp_order_rec.vendor_site_id IS NOT NULL AND p_x_osp_order_rec.vendor_site_id <> l_osp_order_rec.vendor_site_id) THEN
1574                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_VEN_STE_CHG');
1575                     FND_MSG_PUB.ADD;
1576                  END IF;
1577                  -- vendor_contact_id cant change
1578                  IF(p_x_osp_order_rec.vendor_contact_id IS NOT NULL AND p_x_osp_order_rec.vendor_contact_id <> l_osp_order_rec.vendor_contact_id) THEN
1579                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_VEN_CTCT_CHG ');
1580                     FND_MSG_PUB.ADD;
1581                  END IF;
1582                  -- jaramana End
1583 
1584                  -- Buyer cant change
1585                  IF(p_x_osp_order_rec.po_agent_id IS NOT NULL AND p_x_osp_order_rec.po_agent_id <> l_osp_order_rec.po_agent_id) THEN
1586                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_BUYER_CHG');
1587                     FND_MSG_PUB.ADD;
1588                  END IF;
1589                  -- set status_code for lines
1590                  --g_order_status_for_update := G_OSP_PO_CREATED_STATUS;
1591               ELSE
1592                  FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_STATUS');
1593                  FND_MSG_PUB.ADD;
1594               END IF;
1595               -- Added by jaramana on January 9, 2008 for the Requisition ER 6034236
1596            ELSIF (g_old_status_code = G_OSP_REQ_SUBMITTED_STATUS) THEN
1597               IF(p_x_osp_order_rec.status_code IS NULL OR p_x_osp_order_rec.status_code = G_OSP_REQ_SUBMITTED_STATUS) THEN
1598                  IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1599                     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY || '.srvExch.update.', 'Update Order type is Service or Exchange Operation is Update'||
1600                                         'g_old_status_code: ' || g_old_status_code || 'new status: ' || p_x_osp_order_rec.status_code );
1601                   END IF;
1602                  -- po_header_id should be null.
1603                  IF(p_x_osp_order_rec.po_header_id IS NOT NULL AND p_x_osp_order_rec.po_header_id <> FND_API.G_MISS_NUM) THEN
1604                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_PO_NNLL');
1605                     FND_MSG_PUB.ADD;
1606                  END IF;
1607 
1608                 -- req_header_id should be null.
1609                 IF(p_x_osp_order_rec.po_req_header_id IS NOT NULL AND p_x_osp_order_rec.po_req_header_id <> FND_API.G_MISS_NUM) THEN
1610                   FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_REQ_NNLL');
1611                   FND_MSG_PUB.ADD;
1612                 END IF;
1613 
1614                  -- single_instance_flag cant change
1615                  IF(p_x_osp_order_rec.single_instance_flag IS NOT NULL AND p_x_osp_order_rec.single_instance_flag <> l_osp_order_rec.single_instance_flag) THEN
1616                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_INST_FLG_CHG');
1617                     FND_MSG_PUB.ADD;
1618                  END IF;
1619                  -- vendor_id cant change
1620                  IF(p_x_osp_order_rec.vendor_id IS NOT NULL AND p_x_osp_order_rec.vendor_id <> l_osp_order_rec.vendor_id) THEN
1621                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_VEN_CHG');
1622                     FND_MSG_PUB.ADD;
1623                  END IF;
1624                  -- vendor_site_id cant change
1625                  IF(p_x_osp_order_rec.vendor_site_id IS NOT NULL AND p_x_osp_order_rec.vendor_site_id <> l_osp_order_rec.vendor_site_id) THEN
1626                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_VEN_STE_CHG');
1627                     FND_MSG_PUB.ADD;
1628                  END IF;
1629                  -- vendor_contact_id cant change
1630                  IF(p_x_osp_order_rec.vendor_contact_id IS NOT NULL AND p_x_osp_order_rec.vendor_contact_id <> l_osp_order_rec.vendor_contact_id) THEN
1631                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_VEN_CTCT_CHG ');
1632                     FND_MSG_PUB.ADD;
1633                  END IF;
1634                  -- Buyer cant change
1635                  IF(p_x_osp_order_rec.po_agent_id IS NOT NULL AND p_x_osp_order_rec.po_agent_id <> l_osp_order_rec.po_agent_id) THEN
1636                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_BUYER_CHG');
1637                     FND_MSG_PUB.ADD;
1638                  END IF;
1639               ELSE
1640                  FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_STATUS');
1641                  FND_MSG_PUB.ADD;
1642               END IF;
1643 
1644            ELSIF (g_old_status_code = G_OSP_REQ_CREATED_STATUS) THEN
1645               IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1646                     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY || '.srvExch.update.', 'Update Order type is Service or Exchange Operation is Update'
1647                               || 'g_old_status_code: ' || g_old_status_code);
1648               END IF;
1649               -- status_code should be null, 'REQ_CREATED' or 'CLOSED'
1650               IF(p_x_osp_order_rec.status_code IS NULL OR p_x_osp_order_rec.status_code IN( G_OSP_REQ_CREATED_STATUS,G_OSP_CLOSED_STATUS)) THEN
1651                  -- req_header_id cant change
1652                  IF(p_x_osp_order_rec.po_req_header_id IS NOT NULL AND p_x_osp_order_rec.po_req_header_id <> l_osp_order_rec.po_req_header_id) THEN
1653                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_REQ_CHG');
1654                     FND_MSG_PUB.ADD;
1655                  END IF;
1656 
1657                  --po_header_id should be null
1658                  IF(p_x_osp_order_rec.po_header_id IS NOT NULL AND p_x_osp_order_rec.po_header_id <> FND_API.G_MISS_NUM) THEN
1659                    FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_PO_NNLL');
1660                    FND_MSG_PUB.ADD;
1661                  END IF;
1662 
1663                  -- single_instance_flag cant change
1664                  IF(p_x_osp_order_rec.single_instance_flag IS NOT NULL AND p_x_osp_order_rec.single_instance_flag <> l_osp_order_rec.single_instance_flag) THEN
1665                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_INST_FLG_CHG');
1666                     FND_MSG_PUB.ADD;
1667                  END IF;
1668 
1669                  -- vendor_id cant change
1670                  IF(p_x_osp_order_rec.vendor_id IS NOT NULL AND p_x_osp_order_rec.vendor_id <> l_osp_order_rec.vendor_id) THEN
1671                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_VEN_CHG');
1672                     FND_MSG_PUB.ADD;
1673                  END IF;
1674                  -- vendor_site_id cant change
1675                  IF(p_x_osp_order_rec.vendor_site_id IS NOT NULL AND p_x_osp_order_rec.vendor_site_id <> l_osp_order_rec.vendor_site_id) THEN
1676                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_VEN_STE_CHG');
1677                     FND_MSG_PUB.ADD;
1678                  END IF;
1679 
1680                  -- vendor_contact_id cant change
1681                  IF(p_x_osp_order_rec.vendor_contact_id IS NOT NULL AND p_x_osp_order_rec.vendor_contact_id <> l_osp_order_rec.vendor_contact_id) THEN
1682                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_VEN_CTCT_CHG ');
1683                     FND_MSG_PUB.ADD;
1684                  END IF;
1685 
1686                  -- Buyer cant change
1687                  IF(p_x_osp_order_rec.po_agent_id IS NOT NULL AND p_x_osp_order_rec.po_agent_id <> l_osp_order_rec.po_agent_id) THEN
1688                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_BUYER_CHG');
1689                     FND_MSG_PUB.ADD;
1690                  END IF;
1691                  -- set status_code for lines
1692                  --g_order_status_for_update := G_OSP_PO_CREATED_STATUS;
1693               ELSE
1694                  FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_STATUS');
1695                  FND_MSG_PUB.ADD;
1696               END IF;
1697               --mpothuku End
1698            END IF; --IF(p_x_osp_order_rec.operation_flag = G_OP_UPDATE) THEN
1699         END IF; --IF (p_x_osp_order_rec.order_type_code IN ( G_OSP_ORDER_TYPE_SERVICE, G_OSP_ORDER_TYPE_EXCHANGE)) THEN
1700     -- validate Loan order header
1701     ELSIF (p_x_osp_order_rec.order_type_code = G_OSP_ORDER_TYPE_LOAN) THEN
1702        IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1703             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY || '.loan', 'Update order type is Loan');
1704        END IF;
1705         -- single_instance_flag can not be other than null, GMISS or G_NO_FLAG.
1706         IF(p_x_osp_order_rec.single_instance_flag IS NULL OR p_x_osp_order_rec.single_instance_flag = FND_API.G_MISS_CHAR
1707            OR p_x_osp_order_rec.single_instance_flag = G_NO_FLAG )THEN
1708            p_x_osp_order_rec.single_instance_flag := G_NO_FLAG;
1709         ELSE
1710            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_INST_FLG');
1711            FND_MESSAGE.Set_Token('INST_FLG', p_x_osp_order_rec.single_instance_flag);
1712            FND_MSG_PUB.ADD;
1713         END IF;
1714         IF(p_x_osp_order_rec.operation_flag = G_OP_CREATE) THEN
1715            -- validate customer_id.
1716            IF(p_x_osp_order_rec.customer_id IS NULL OR p_x_osp_order_rec.customer_id = FND_API.G_MISS_NUM) THEN
1717               FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_CUSTOMER_ID_NLL');
1718               FND_MSG_PUB.ADD;
1719            ELSE
1720               validate_customer(p_x_osp_order_rec.customer_id);
1721            END IF;
1722            -- validate contract_id
1723           /* Change made by mpothuku to make the contract_id optional on 12/16/04
1724 	     This is a work around and has to be revoked after PM comes up with the Service Contract Integration
1725 	  */
1726 	  -- Changes begin
1727 	 /*
1728            IF (p_x_osp_order_rec.contract_id IS NULL OR p_x_osp_order_rec.contract_id = FND_API.G_MISS_NUM) THEN
1729               FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_CONTRACT_ID_NLL');
1730               FND_MSG_PUB.ADD;
1731            ELSE
1732            validate_contract(G_OSP_ORDER_TYPE_LOAN, p_x_osp_order_rec.contract_id , p_x_osp_order_rec.customer_id , l_operating_unit_id);
1733 	 */
1734 	   IF (p_x_osp_order_rec.contract_id IS NOT NULL AND p_x_osp_order_rec.contract_id <> FND_API.G_MISS_NUM) THEN
1735 	    validate_contract(G_OSP_ORDER_TYPE_LOAN, p_x_osp_order_rec.contract_id , p_x_osp_order_rec.customer_id , l_operating_unit_id);
1736            END IF;
1737 	  --Changes by mpothuku End
1738         ELSIF(p_x_osp_order_rec.operation_flag = G_OP_UPDATE) THEN
1739            IF(g_old_status_code  = G_OSP_ENTERED_STATUS) THEN
1740               -- Changed by jaramana on January 9, 2008 for the Requisition ER 6034236
1741               -- status_code should be null,'ENTERED', or 'SUBMITTED' or 'REQ_SUBMITTED'
1742               IF(p_x_osp_order_rec.status_code IS NOT NULL AND
1743                  p_x_osp_order_rec.status_code NOT IN(G_OSP_ENTERED_STATUS, G_OSP_SUBMITTED_STATUS, G_OSP_REQ_SUBMITTED_STATUS)) THEN
1744                  FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_STATUS');
1745                  FND_MSG_PUB.ADD;
1746               END IF;
1747               -- jaramana End
1748               -- validate customer_id.
1749               IF(p_x_osp_order_rec.customer_id IS NOT NULL AND p_x_osp_order_rec.customer_id = FND_API.G_MISS_NUM) THEN
1750                  FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_CUSTOMER_ID_NLL');
1751                  FND_MSG_PUB.ADD;
1752               ELSIF(p_x_osp_order_rec.customer_id IS NOT NULL) THEN
1753                  validate_customer(p_x_osp_order_rec.customer_id);
1754               END IF;
1755               -- validate contract_id
1756               /* Change made by mpothuku to make the contract_id optional on 12/16/04
1757 		This is a work around and has to be revoked after PM comes up with the Service Contract Integration
1758 	      */
1759 	      -- Changes begin
1760 	      /*
1761               IF(p_x_osp_order_rec.contract_id IS NOT NULL AND p_x_osp_order_rec.contract_id = FND_API.G_MISS_NUM) THEN
1762                  FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_CONTRACT_ID_NLL');
1763                  FND_MSG_PUB.ADD;
1764               ELSIF ( p_x_osp_order_rec.contract_id IS NOT NULL) THEN
1765                  validate_contract(G_OSP_ORDER_TYPE_LOAN, p_x_osp_order_rec.contract_id , p_x_osp_order_rec.customer_id , l_operating_unit_id);
1766 	      */
1767               IF ( p_x_osp_order_rec.contract_id IS NOT NULL AND p_x_osp_order_rec.contract_id <> FND_API.G_MISS_NUM) THEN
1768                  validate_contract(G_OSP_ORDER_TYPE_LOAN, p_x_osp_order_rec.contract_id , p_x_osp_order_rec.customer_id , l_operating_unit_id);
1769               END IF;
1770 	     --Changes by mpothuku End
1771               -- set status
1772               --g_order_status_for_update := G_OSP_ENTERED_STATUS;
1773            ELSIF(g_old_status_code  = G_OSP_SUBMITTED_STATUS) THEN
1774                -- status_code should be null,'ENTERED', or 'SUBMITTED'
1775               IF(p_x_osp_order_rec.status_code IS NOT NULL AND
1776                  p_x_osp_order_rec.status_code NOT IN(G_OSP_SUBMITTED_STATUS, G_OSP_CLOSED_STATUS)) THEN
1777                  FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_STATUS');
1778                  FND_MSG_PUB.ADD;
1779               END IF;
1780               -- CUSTOMER_ID cant change
1781               IF(p_x_osp_order_rec.customer_id IS NOT NULL AND p_x_osp_order_rec.customer_id <> l_osp_order_rec.customer_id) THEN
1782                  FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_CUST_CHG');
1783                  FND_MSG_PUB.ADD;
1784               END IF;
1785               -- contract_id cant change
1786               IF(p_x_osp_order_rec.contract_id IS NOT NULL AND p_x_osp_order_rec.contract_id <> l_osp_order_rec.contract_id) THEN
1787                  FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_CTRCT_CHG');
1788                  FND_MSG_PUB.ADD;
1789               END IF;
1790               --g_order_status_for_update := G_OSP_SUBMITTED_STATUS;
1791            ELSE
1792               FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_STATUS');
1793               FND_MSG_PUB.ADD;
1794            END IF;
1795         END IF;
1796     -- validate BORROW order header
1797     ELSIF (p_x_osp_order_rec.order_type_code = G_OSP_ORDER_TYPE_BORROW) THEN
1798         -- single_instance_flag can not be other than null, GMISS or G_NO_FLAG.
1799         IF(p_x_osp_order_rec.single_instance_flag IS NULL OR p_x_osp_order_rec.single_instance_flag = FND_API.G_MISS_CHAR
1800            OR p_x_osp_order_rec.single_instance_flag = G_NO_FLAG )THEN
1801            p_x_osp_order_rec.single_instance_flag := G_NO_FLAG;
1802         ELSE
1803            FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_INST_FLG');
1804            FND_MESSAGE.Set_Token('INST_FLG', p_x_osp_order_rec.single_instance_flag);
1805            FND_MSG_PUB.ADD;
1806         END IF;
1807         IF(p_x_osp_order_rec.operation_flag = G_OP_CREATE) THEN
1808            -- validate customer_id.
1809            IF(p_x_osp_order_rec.customer_id IS NOT NULL) THEN
1810               validate_customer(p_x_osp_order_rec.customer_id);
1811            END IF;
1812            /*
1813            -- validate vendor_id.
1814            IF(p_x_osp_order_rec.vendor_id IS NULL OR p_x_osp_order_rec.vendor_id = FND_API.G_MISS_NUM) THEN
1815               FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENDOR_ID_NLL');
1816               FND_MSG_PUB.ADD;
1817            ELSE
1818               validate_vendor(p_x_osp_order_rec.vendor_id);
1819            END IF;
1820            */
1821            validate_vendor_site_contact(p_x_osp_order_rec.vendor_id,
1822                                         p_x_osp_order_rec.vendor_site_id,
1823                                         p_x_osp_order_rec.vendor_contact_id);
1824            -- validate contract_id
1825 	   /* Change made by mpothuku to make the contract_id optional on 12/16/04
1826 	      This is a work around and has to be revoked after PM comes up with the Service Contract Integration
1827 	   */
1828 	   -- Changes begin
1829 	   /*
1830             IF (p_x_osp_order_rec.contract_id IS NULL OR p_x_osp_order_rec.contract_id = FND_API.G_MISS_NUM) THEN
1831               FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_CONTRACT_ID_NLL');
1832               FND_MSG_PUB.ADD;
1833            ELSE
1834               validate_contract(G_OSP_ORDER_TYPE_BORROW, p_x_osp_order_rec.contract_id , p_x_osp_order_rec.vendor_id , l_operating_unit_id);
1835            END IF;
1836 	   */
1837 	   IF (p_x_osp_order_rec.contract_id IS NOT NULL AND p_x_osp_order_rec.contract_id <> FND_API.G_MISS_NUM) THEN
1838 		validate_contract(G_OSP_ORDER_TYPE_BORROW, p_x_osp_order_rec.contract_id , p_x_osp_order_rec.vendor_id , l_operating_unit_id);
1839            END IF;
1840 	   --Changes by mpothuku End
1841         ELSIF(p_x_osp_order_rec.operation_flag = G_OP_UPDATE) THEN
1842            IF(g_old_status_code  = G_OSP_ENTERED_STATUS) THEN
1843               -- status_code should be null,'ENTERED', or 'SUBMITTED'
1844               IF(p_x_osp_order_rec.status_code IS NOT NULL AND
1845                  p_x_osp_order_rec.status_code NOT IN(G_OSP_ENTERED_STATUS, G_OSP_SUBMITTED_STATUS)) THEN
1846                  FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_STATUS');
1847                  FND_MSG_PUB.ADD;
1848               END IF;
1849               -- validate customer_id.
1850               IF(p_x_osp_order_rec.customer_id IS NOT NULL AND p_x_osp_order_rec.customer_id <> FND_API.G_MISS_NUM) THEN
1851                  validate_customer(p_x_osp_order_rec.customer_id);
1852               END IF;
1853 	      /* Change made by mpothuku to make the contract_id optional on 12/16/04
1854 	         This is a work around and has to be revoked after PM comes up with the Service Contract Integration
1855 	      */
1856 	      -- Changes begin
1857 	      /*
1858               IF (p_x_osp_order_rec.contract_id IS NOT NULL AND p_x_osp_order_rec.contract_id = FND_API.G_MISS_NUM) THEN
1859                   FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_CONTRACT_ID_NLL');
1860                   FND_MSG_PUB.ADD;
1861               ELSIF(p_x_osp_order_rec.contract_id IS NOT NULL AND (p_x_osp_order_rec.vendor_id IS NULL OR p_x_osp_order_rec.vendor_id = FND_API.G_MISS_NUM)) THEN
1862                   FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENDOR_ID_NLL');
1863                   FND_MSG_PUB.ADD;
1864               ELSIF(p_x_osp_order_rec.vendor_id IS NOT NULL OR p_x_osp_order_rec.contract_id IS NOT NULL) THEN
1865                   -- validate vendor_id.
1866                    validate_vendor(p_x_osp_order_rec.vendor_id);
1867                    p_x_osp_order_rec.contract_id := NVL(p_x_osp_order_rec.contract_id, l_osp_order_rec.contract_id);
1868                   -- validate contract_id
1869                   validate_contract(G_OSP_ORDER_TYPE_BORROW, p_x_osp_order_rec.contract_id , p_x_osp_order_rec.vendor_id , l_operating_unit_id);
1870               END IF;
1871 	     */
1872              /*
1873 	     -- validate vendor_id.
1874              IF(p_x_osp_order_rec.vendor_id IS NULL OR p_x_osp_order_rec.vendor_id = FND_API.G_MISS_NUM) THEN
1875 		 FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENDOR_ID_NLL');
1876 		  FND_MSG_PUB.ADD;
1877 	      ELSE
1878 		validate_vendor(p_x_osp_order_rec.vendor_id);
1879 	     END IF;
1880              */
1881              validate_vendor_site_contact(p_x_osp_order_rec.vendor_id,
1882                                           p_x_osp_order_rec.vendor_site_id,
1883                                           p_x_osp_order_rec.vendor_contact_id);
1884 
1885              p_x_osp_order_rec.contract_id := NVL(p_x_osp_order_rec.contract_id, l_osp_order_rec.contract_id);
1886 	     --validate contract_id only if it is not null.
1887 	     IF (p_x_osp_order_rec.contract_id IS NOT NULL AND p_x_osp_order_rec.contract_id <> FND_API.G_MISS_NUM) THEN
1888 		validate_contract(G_OSP_ORDER_TYPE_BORROW, p_x_osp_order_rec.contract_id , p_x_osp_order_rec.vendor_id , l_operating_unit_id);
1889 	     END IF;
1890 	  --Changes by mpothuku End
1891 	     -- set status
1892               --g_order_status_for_update := G_OSP_ENTERED_STATUS;
1893            ELSIF(g_old_status_code  = G_OSP_SUBMITTED_STATUS) THEN
1894                -- status_code should be null,'ENTERED', or 'SUBMITTED'
1895               IF(p_x_osp_order_rec.status_code IS NOT NULL AND
1896                  p_x_osp_order_rec.status_code NOT IN(G_OSP_SUBMITTED_STATUS, G_OSP_CLOSED_STATUS)) THEN
1897                  FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_STATUS');
1898                  FND_MSG_PUB.ADD;
1899               END IF;
1900               -- CUSTOMER_ID cant change
1901               IF(p_x_osp_order_rec.customer_id IS NOT NULL AND p_x_osp_order_rec.customer_id <> l_osp_order_rec.customer_id) THEN
1902                  FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_CUST_CHG');
1903                  FND_MSG_PUB.ADD;
1904               END IF;
1905               -- vendor_id cant change
1906               IF(p_x_osp_order_rec.vendor_id IS NOT NULL AND p_x_osp_order_rec.vendor_id <> l_osp_order_rec.vendor_id) THEN
1907                  FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_VEND_CHG');
1908                  FND_MSG_PUB.ADD;
1909               END IF;
1910               -- contract_id cant change
1911               IF(p_x_osp_order_rec.contract_id IS NOT NULL AND p_x_osp_order_rec.contract_id <> l_osp_order_rec.contract_id) THEN
1912                  FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_CTRCT_CHG');
1913                  FND_MSG_PUB.ADD;
1914               END IF;
1915               -- set status
1916               --g_order_status_for_update := G_OSP_SUBMITTED_STATUS;
1917            ELSE
1918               FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_STATUS');
1919               FND_MSG_PUB.ADD;
1920            END IF;
1921         END IF;
1922     ELSE
1923         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INV_TYPE');
1924         FND_MSG_PUB.ADD;
1925     END IF;
1926     -- delete header
1927     IF(p_x_osp_order_rec.operation_flag = G_OP_DELETE) THEN
1928         -- Changed by jaramana on January 9, 2008 for the Requisition ER 6034236 (Added G_OSP_REQ_SUB_FAILED_STATUS)
1929         IF(g_old_status_code NOT IN( G_OSP_ENTERED_STATUS, G_OSP_SUB_FAILED_STATUS, G_OSP_REQ_SUB_FAILED_STATUS ))THEN
1930             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INVOP');
1931             FND_MSG_PUB.ADD;
1932         ELSE
1933             --g_order_status_for_update := G_OSP_DELETED_STATUS;
1934             IF(l_osp_order_rec.oe_header_id IS NOT NULL) THEN
1935               -- calling to delete SO HEADER
1936               delete_cancel_so(
1937                  p_oe_header_id             => l_osp_order_rec.oe_header_id,
1938                  p_del_cancel_so_lines_tbl  =>l_del_cancel_so_lines_tbl
1939               );
1940             END IF;
1941         END IF;
1942         -- jaramana End
1943     END IF;
1944     IF FND_MSG_PUB.count_msg > 0 THEN
1945        RAISE  FND_API.G_EXC_ERROR;
1946        -- RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1947     END IF;
1948    IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1949       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'End Procedure');
1950     END IF;
1951     --dbms_output.put_line('Exiting : validate_order_header');
1952 END validate_order_header;
1953 
1954 --------------------------------------------------------------------------------------------------------------
1955 -- defaults values and populates unchanged fields in header record.
1956 --------------------------------------------------------------------------------------------------------------
1957 --Added the handling for vendor_contact_id, (?)Jerry 04/17/2005
1958 --This procedure should be called after value to id conversion
1959 PROCEDURE default_unchanged_order_header(
1960 	p_x_osp_order_rec  IN OUT NOCOPY osp_order_rec_type
1961     ) IS
1962 
1963     -- Changed by jaramana on January 9, 2008 for the Requisition ER 6034236 (Added the PO_REQ_HEADER_ID in the cursor below)
1964     CURSOR osp_order_csr(p_osp_order_id IN NUMBER, p_object_version_number IN NUMBER) IS
1965     SELECT  osp_order_number, order_type_code, single_instance_flag, po_header_id, oe_header_id,vendor_id, vendor_site_id,
1966         customer_id,order_date,contract_id,contract_terms,operating_unit_id, po_synch_flag, status_code,
1967         po_batch_id, po_request_id,po_agent_id, po_interface_header_id, po_req_header_id, description,attribute_category,
1968         attribute1,attribute2, attribute3, attribute4, attribute5, attribute6, attribute7, attribute8, attribute9,
1969         attribute10, attribute11, attribute12, attribute13, attribute14, attribute15, vendor_contact_id
1970     -- jaramana End
1971     FROM ahl_osp_orders_vl
1972     WHERE osp_order_id = p_osp_order_id
1973     AND object_version_number= p_object_version_number;
1974     l_osp_order_rec osp_order_rec_type;
1975     L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.default_unchanged_order_header';
1976 BEGIN
1977     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1978       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Procedure');
1979     END IF;
1980     IF(p_x_osp_order_rec.operation_flag = G_OP_UPDATE) THEN
1981         OPEN osp_order_csr(p_x_osp_order_rec.osp_order_id, p_x_osp_order_rec.object_version_number);
1982         -- Added by jaramana on January 9, 2008 for the Requisition ER 6034236 (Added the PO_REQ_HEADER_ID in the rec below)
1983         FETCH osp_order_csr INTO l_osp_order_rec.osp_order_number, l_osp_order_rec.order_type_code,l_osp_order_rec.single_instance_flag,
1984          l_osp_order_rec.po_header_id, l_osp_order_rec.oe_header_id,l_osp_order_rec.vendor_id,
1985          l_osp_order_rec.vendor_site_id, l_osp_order_rec.customer_id, l_osp_order_rec.order_date,
1986          l_osp_order_rec.contract_id, l_osp_order_rec.contract_terms, l_osp_order_rec.operating_unit_id,
1987          l_osp_order_rec.po_synch_flag, l_osp_order_rec.status_code, l_osp_order_rec.po_batch_id, l_osp_order_rec.po_request_id,
1988          l_osp_order_rec.po_agent_id, l_osp_order_rec.po_interface_header_id, l_osp_order_rec.po_req_header_id, l_osp_order_rec.description,
1989          l_osp_order_rec.attribute_category,l_osp_order_rec.attribute1,l_osp_order_rec.attribute2,
1990          l_osp_order_rec.attribute3, l_osp_order_rec.attribute4, l_osp_order_rec.attribute5,
1991          l_osp_order_rec.attribute6, l_osp_order_rec.attribute7, l_osp_order_rec.attribute8,
1992          l_osp_order_rec.attribute9, l_osp_order_rec.attribute10, l_osp_order_rec.attribute11,
1993          l_osp_order_rec.attribute12, l_osp_order_rec.attribute13, l_osp_order_rec.attribute14,
1994          l_osp_order_rec.attribute15, l_osp_order_rec.vendor_contact_id;
1995          -- jaramana End
1996         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
1997           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'osp_order_id='||p_x_osp_order_rec.osp_order_id|| 'ovn='||p_x_osp_order_rec.object_version_number);
1998         END IF;
1999         IF (osp_order_csr%NOTFOUND) THEN
2000             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INVOP_OSP_NFOUND');
2001             FND_MSG_PUB.ADD;
2002         ELSE
2003             --dbms_output.put_line('l_osp_order_rec.osp_order_number ' || l_osp_order_rec.osp_order_number);
2004             IF (p_x_osp_order_rec.osp_order_number IS NULL) THEN
2005                 p_x_osp_order_rec.osp_order_number := l_osp_order_rec.osp_order_number;
2006             ELSIF(p_x_osp_order_rec.osp_order_number = FND_API.G_MISS_NUM) THEN
2007                 p_x_osp_order_rec.osp_order_number := null;
2008             END IF;
2009              IF (p_x_osp_order_rec.order_type_code IS NULL) THEN
2010                 p_x_osp_order_rec.order_type_code := l_osp_order_rec.order_type_code;
2011             ELSIF(p_x_osp_order_rec.order_type_code = FND_API.G_MISS_CHAR) THEN
2012                 p_x_osp_order_rec.order_type_code := null;
2013             END IF;
2014              IF (p_x_osp_order_rec.single_instance_flag IS NULL) THEN
2015                 p_x_osp_order_rec.single_instance_flag := l_osp_order_rec.single_instance_flag;
2016             ELSIF(p_x_osp_order_rec.single_instance_flag = FND_API.G_MISS_CHAR) THEN
2017                 p_x_osp_order_rec.single_instance_flag := null;
2018             END IF;
2019             IF (p_x_osp_order_rec.po_header_id IS NULL) THEN
2020                 p_x_osp_order_rec.po_header_id := l_osp_order_rec.po_header_id;
2021             ELSIF(p_x_osp_order_rec.po_header_id = FND_API.G_MISS_NUM) THEN
2022                 p_x_osp_order_rec.po_header_id := null;
2023             END IF;
2024             IF (p_x_osp_order_rec.oe_header_id IS NULL) THEN
2025                 p_x_osp_order_rec.oe_header_id := l_osp_order_rec.oe_header_id;
2026             ELSIF(p_x_osp_order_rec.oe_header_id = FND_API.G_MISS_NUM) THEN
2027                 p_x_osp_order_rec.oe_header_id := null;
2028             END IF;
2029             IF (p_x_osp_order_rec.vendor_id IS NULL) THEN
2030                 p_x_osp_order_rec.vendor_id := l_osp_order_rec.vendor_id;
2031             ELSIF(p_x_osp_order_rec.vendor_id = FND_API.G_MISS_NUM) THEN
2032                 p_x_osp_order_rec.vendor_id := null;
2033             END IF;
2034             IF (p_x_osp_order_rec.vendor_site_id IS NULL) THEN
2035                 p_x_osp_order_rec.vendor_site_id := l_osp_order_rec.vendor_site_id;
2036             ELSIF(p_x_osp_order_rec.vendor_site_id = FND_API.G_MISS_NUM) THEN
2037               p_x_osp_order_rec.vendor_site_id := null;
2038             END IF;
2039             IF (p_x_osp_order_rec.customer_id IS NULL) THEN
2040                 p_x_osp_order_rec.customer_id := l_osp_order_rec.customer_id;
2041             ELSIF(p_x_osp_order_rec.customer_id = FND_API.G_MISS_NUM) THEN
2042                 p_x_osp_order_rec.customer_id := null;
2043             END IF;
2044             IF (p_x_osp_order_rec.order_date IS NULL) THEN
2045                 p_x_osp_order_rec.order_date := l_osp_order_rec.order_date;
2046             ELSIF(p_x_osp_order_rec.order_date = FND_API.G_MISS_DATE) THEN
2047                 p_x_osp_order_rec.order_date := null;
2048             END IF;
2049             IF (p_x_osp_order_rec.contract_id IS NULL) THEN
2050                 p_x_osp_order_rec.contract_id := l_osp_order_rec.contract_id;
2051             ELSIF(p_x_osp_order_rec.contract_id = FND_API.G_MISS_NUM) THEN
2052                 p_x_osp_order_rec.contract_id := null;
2053             END IF;
2054             IF (p_x_osp_order_rec.contract_terms IS NULL) THEN
2055                 p_x_osp_order_rec.contract_terms := l_osp_order_rec.contract_terms;
2056             ELSIF(p_x_osp_order_rec.contract_terms = FND_API.G_MISS_CHAR) THEN
2057                 p_x_osp_order_rec.contract_terms := null;
2058             END IF;
2059             IF (p_x_osp_order_rec.operating_unit_id IS NULL) THEN
2060                 p_x_osp_order_rec.operating_unit_id := l_osp_order_rec.operating_unit_id;
2061             ELSIF(p_x_osp_order_rec.operating_unit_id = FND_API.G_MISS_NUM) THEN
2062                 p_x_osp_order_rec.operating_unit_id := null;
2063             END IF;
2064             IF (p_x_osp_order_rec.po_synch_flag IS NULL) THEN
2065                 p_x_osp_order_rec.po_synch_flag := l_osp_order_rec.po_synch_flag;
2066             ELSIF(p_x_osp_order_rec.po_synch_flag = FND_API.G_MISS_CHAR) THEN
2067                 p_x_osp_order_rec.po_synch_flag := null;
2068             END IF;
2069             IF (p_x_osp_order_rec.status_code IS NULL) THEN
2070                 p_x_osp_order_rec.status_code := l_osp_order_rec.status_code;
2071             ELSIF(p_x_osp_order_rec.status_code = FND_API.G_MISS_CHAR) THEN
2072                 p_x_osp_order_rec.status_code := null;
2073             END IF;
2074               IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2075                 FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin',
2076                     'batch_id='||p_x_osp_order_rec.po_batch_id||
2077                     'request_id='||p_x_osp_order_rec.po_request_id||
2078                     'interface_id='||p_x_osp_order_rec.po_interface_header_id);
2079               END IF;
2080             IF (p_x_osp_order_rec.po_batch_id IS NULL) THEN
2081                 p_x_osp_order_rec.po_batch_id := l_osp_order_rec.po_batch_id;
2082             ELSIF(p_x_osp_order_rec.po_batch_id = FND_API.G_MISS_NUM) THEN
2083                 p_x_osp_order_rec.po_batch_id := null;
2084             END IF;
2085             IF (p_x_osp_order_rec.po_request_id IS NULL) THEN
2086                 p_x_osp_order_rec.po_request_id := l_osp_order_rec.po_request_id;
2087             ELSIF(p_x_osp_order_rec.po_request_id = FND_API.G_MISS_NUM) THEN
2088                 p_x_osp_order_rec.po_request_id := null;
2089             END IF;
2090             IF (p_x_osp_order_rec.po_agent_id IS NULL) THEN
2091                 p_x_osp_order_rec.po_agent_id := l_osp_order_rec.po_agent_id;
2092             ELSIF(p_x_osp_order_rec.po_agent_id = FND_API.G_MISS_NUM) THEN
2093                 p_x_osp_order_rec.po_agent_id := null;
2094             END IF;
2095             IF (p_x_osp_order_rec.po_interface_header_id IS NULL) THEN
2096                 p_x_osp_order_rec.po_interface_header_id := l_osp_order_rec.po_interface_header_id;
2097             ELSIF(p_x_osp_order_rec.po_interface_header_id = FND_API.G_MISS_NUM) THEN
2098                 p_x_osp_order_rec.po_interface_header_id := null;
2099             END IF;
2100             -- Added by jaramana on January 9, 2008 for the Requisition ER 6034236
2101             IF (p_x_osp_order_rec.po_req_header_id IS NULL) THEN
2102                 p_x_osp_order_rec.po_req_header_id := l_osp_order_rec.po_req_header_id;
2103             ELSIF(p_x_osp_order_rec.po_req_header_id = FND_API.G_MISS_NUM) THEN
2104                 p_x_osp_order_rec.po_req_header_id := null;
2105             END IF;
2106             -- jaramana End
2107             IF (p_x_osp_order_rec.description IS NULL) THEN
2108                 p_x_osp_order_rec.description := l_osp_order_rec.description;
2109             ELSIF(p_x_osp_order_rec.description = FND_API.G_MISS_CHAR) THEN
2110                 p_x_osp_order_rec.description := null;
2111             END IF;
2112             IF (p_x_osp_order_rec.attribute_category IS NULL) THEN
2113                 p_x_osp_order_rec.attribute_category := l_osp_order_rec.attribute_category;
2114             ELSIF(p_x_osp_order_rec.attribute_category = FND_API.G_MISS_CHAR) THEN
2115                 p_x_osp_order_rec.attribute_category := null;
2116             END IF;
2117             IF (p_x_osp_order_rec.attribute1 IS NULL) THEN
2118                 p_x_osp_order_rec.attribute1 := l_osp_order_rec.attribute1;
2119             ELSIF(p_x_osp_order_rec.attribute1 = FND_API.G_MISS_CHAR) THEN
2120                 p_x_osp_order_rec.attribute1 := null;
2121             END IF;
2122             IF (p_x_osp_order_rec.attribute2 IS NULL) THEN
2123                 p_x_osp_order_rec.attribute2 := l_osp_order_rec.attribute2;
2124             ELSIF(p_x_osp_order_rec.attribute2 = FND_API.G_MISS_CHAR) THEN
2125                 p_x_osp_order_rec.attribute2 := null;
2126             END IF;
2127             IF (p_x_osp_order_rec.attribute3 IS NULL) THEN
2128                 p_x_osp_order_rec.attribute3 := l_osp_order_rec.attribute3;
2129             ELSIF(p_x_osp_order_rec.attribute3 = FND_API.G_MISS_CHAR) THEN
2130                 p_x_osp_order_rec.attribute3 := null;
2131             END IF;
2132             IF (p_x_osp_order_rec.attribute4 IS NULL) THEN
2133                 p_x_osp_order_rec.attribute4 := l_osp_order_rec.attribute4;
2134             ELSIF(p_x_osp_order_rec.attribute4 = FND_API.G_MISS_CHAR) THEN
2135                 p_x_osp_order_rec.attribute4 := null;
2136             END IF;
2137             IF (p_x_osp_order_rec.attribute5 IS NULL) THEN
2138                 p_x_osp_order_rec.attribute5 := l_osp_order_rec.attribute5;
2139             ELSIF(p_x_osp_order_rec.attribute5 = FND_API.G_MISS_CHAR) THEN
2140                 p_x_osp_order_rec.attribute5 := null;
2141             END IF;
2142             IF (p_x_osp_order_rec.attribute6 IS NULL) THEN
2143                 p_x_osp_order_rec.attribute6 := l_osp_order_rec.attribute6;
2144             ELSIF(p_x_osp_order_rec.attribute6 = FND_API.G_MISS_CHAR) THEN
2145                 p_x_osp_order_rec.attribute6 := null;
2146             END IF;
2147             IF (p_x_osp_order_rec.attribute7 IS NULL) THEN
2148                 p_x_osp_order_rec.attribute7 := l_osp_order_rec.attribute7;
2149             ELSIF(p_x_osp_order_rec.attribute7 = FND_API.G_MISS_CHAR) THEN
2150                 p_x_osp_order_rec.attribute7 := null;
2151             END IF;
2152             IF (p_x_osp_order_rec.attribute8 IS NULL) THEN
2153                 p_x_osp_order_rec.attribute8 := l_osp_order_rec.attribute8;
2154             ELSIF(p_x_osp_order_rec.attribute8 = FND_API.G_MISS_CHAR) THEN
2155                 p_x_osp_order_rec.attribute8 := null;
2156             END IF;
2157             IF (p_x_osp_order_rec.attribute9 IS NULL) THEN
2158                 p_x_osp_order_rec.attribute9 := l_osp_order_rec.attribute9;
2159             ELSIF(p_x_osp_order_rec.attribute9 = FND_API.G_MISS_CHAR) THEN
2160                 p_x_osp_order_rec.attribute9 := null;
2161             END IF;
2162             IF (p_x_osp_order_rec.attribute10 IS NULL) THEN
2163                 p_x_osp_order_rec.attribute10 := l_osp_order_rec.attribute10;
2164             ELSIF(p_x_osp_order_rec.attribute10 = FND_API.G_MISS_CHAR) THEN
2165                 p_x_osp_order_rec.attribute10 := null;
2166             END IF;
2167             IF (p_x_osp_order_rec.attribute11 IS NULL) THEN
2168                 p_x_osp_order_rec.attribute11 := l_osp_order_rec.attribute11;
2169             ELSIF(p_x_osp_order_rec.attribute11 = FND_API.G_MISS_CHAR) THEN
2170                 p_x_osp_order_rec.attribute11 := null;
2171             END IF;
2172             IF (p_x_osp_order_rec.attribute12 IS NULL) THEN
2173                 p_x_osp_order_rec.attribute12 := l_osp_order_rec.attribute12;
2174             ELSIF(p_x_osp_order_rec.attribute12 = FND_API.G_MISS_CHAR) THEN
2175                 p_x_osp_order_rec.attribute12 := null;
2176             END IF;
2177             IF (p_x_osp_order_rec.attribute13 IS NULL) THEN
2178                 p_x_osp_order_rec.attribute13 := l_osp_order_rec.attribute13;
2179             ELSIF(p_x_osp_order_rec.attribute13 = FND_API.G_MISS_CHAR) THEN
2180                 p_x_osp_order_rec.attribute13 := null;
2181             END IF;
2182             IF (p_x_osp_order_rec.attribute14 IS NULL) THEN
2183                 p_x_osp_order_rec.attribute14 := l_osp_order_rec.attribute14;
2184             ELSIF(p_x_osp_order_rec.attribute14 = FND_API.G_MISS_CHAR) THEN
2185                 p_x_osp_order_rec.attribute14 := null;
2186             END IF;
2187             IF (p_x_osp_order_rec.attribute15 IS NULL) THEN
2188                 p_x_osp_order_rec.attribute15 := l_osp_order_rec.attribute15;
2189             ELSIF(p_x_osp_order_rec.attribute15 = FND_API.G_MISS_CHAR) THEN
2190                 p_x_osp_order_rec.attribute15 := null;
2191             END IF;
2192             IF (p_x_osp_order_rec.vendor_contact_id IS NULL) THEN
2193                 p_x_osp_order_rec.vendor_contact_id := l_osp_order_rec.vendor_contact_id;
2194             ELSIF(p_x_osp_order_rec.vendor_contact_id = FND_API.G_MISS_NUM) THEN
2195                 p_x_osp_order_rec.vendor_contact_id := null;
2196             END IF;
2197         END IF;
2198         CLOSE osp_order_csr;
2199     ELSIF (p_x_osp_order_rec.operation_flag = G_OP_CREATE) THEN
2200          IF(p_x_osp_order_rec.osp_order_number = FND_API.G_MISS_NUM) THEN
2201             p_x_osp_order_rec.osp_order_number := null;
2202          END IF;
2203          IF(p_x_osp_order_rec.order_type_code = FND_API.G_MISS_CHAR) THEN
2204             p_x_osp_order_rec.order_type_code := null;
2205          END IF;
2206          IF(p_x_osp_order_rec.single_instance_flag = FND_API.G_MISS_CHAR) THEN
2207             p_x_osp_order_rec.single_instance_flag := null;
2208          END IF;
2209          IF(p_x_osp_order_rec.po_header_id = FND_API.G_MISS_NUM) THEN
2210             p_x_osp_order_rec.po_header_id := null;
2211          END IF;
2212          IF(p_x_osp_order_rec.oe_header_id = FND_API.G_MISS_NUM) THEN
2213             p_x_osp_order_rec.oe_header_id := null;
2214          END IF;
2215          IF(p_x_osp_order_rec.vendor_id = FND_API.G_MISS_NUM) THEN
2216             p_x_osp_order_rec.vendor_id := null;
2217          END IF;
2218          IF(p_x_osp_order_rec.vendor_site_id = FND_API.G_MISS_NUM) THEN
2219             p_x_osp_order_rec.vendor_site_id := null;
2220          END IF;
2221          IF(p_x_osp_order_rec.customer_id = FND_API.G_MISS_NUM) THEN
2222             p_x_osp_order_rec.customer_id := null;
2223          END IF;
2224          IF(p_x_osp_order_rec.order_date = FND_API.G_MISS_DATE) THEN
2225             p_x_osp_order_rec.order_date := null;
2226          END IF;
2227          IF(p_x_osp_order_rec.contract_id = FND_API.G_MISS_NUM) THEN
2228             p_x_osp_order_rec.contract_id := null;
2229          END IF;
2230          IF(p_x_osp_order_rec.contract_terms = FND_API.G_MISS_CHAR) THEN
2231             p_x_osp_order_rec.contract_terms := null;
2232          END IF;
2233          IF(p_x_osp_order_rec.operating_unit_id = FND_API.G_MISS_NUM) THEN
2234             p_x_osp_order_rec.operating_unit_id := null;
2235          END IF;
2236          IF(p_x_osp_order_rec.po_synch_flag = FND_API.G_MISS_CHAR) THEN
2237             p_x_osp_order_rec.po_synch_flag := null;
2238          END IF;
2239          IF(p_x_osp_order_rec.status_code = FND_API.G_MISS_CHAR) THEN
2240             p_x_osp_order_rec.status_code := null;
2241          END IF;
2242          IF(p_x_osp_order_rec.po_batch_id = FND_API.G_MISS_NUM) THEN
2243             p_x_osp_order_rec.po_batch_id := null;
2244          END IF;
2245          IF(p_x_osp_order_rec.po_request_id = FND_API.G_MISS_NUM) THEN
2246             p_x_osp_order_rec.po_request_id := null;
2247          END IF;
2248          IF(p_x_osp_order_rec.po_agent_id = FND_API.G_MISS_NUM) THEN
2249             p_x_osp_order_rec.po_agent_id := null;
2250          END IF;
2251          IF(p_x_osp_order_rec.po_interface_header_id = FND_API.G_MISS_NUM) THEN
2252             p_x_osp_order_rec.po_interface_header_id := null;
2253          END IF;
2254          -- Added by jaramana on January 9, 2008 for the Requisition ER 6034236
2255          IF(p_x_osp_order_rec.po_req_header_id = FND_API.G_MISS_NUM) THEN
2256             p_x_osp_order_rec.po_req_header_id := null;
2257          END IF;
2258          -- jaramana End
2259          IF(p_x_osp_order_rec.description = FND_API.G_MISS_CHAR) THEN
2260             p_x_osp_order_rec.description := null;
2261          END IF;
2262          IF(p_x_osp_order_rec.attribute_category = FND_API.G_MISS_CHAR) THEN
2263             p_x_osp_order_rec.attribute_category := null;
2264          END IF;
2265          IF(p_x_osp_order_rec.attribute1 = FND_API.G_MISS_CHAR) THEN
2266             p_x_osp_order_rec.attribute1 := null;
2267          END IF;
2268          IF(p_x_osp_order_rec.attribute2 = FND_API.G_MISS_CHAR) THEN
2269             p_x_osp_order_rec.attribute2 := null;
2270          END IF;
2271          IF(p_x_osp_order_rec.attribute3 = FND_API.G_MISS_CHAR) THEN
2272             p_x_osp_order_rec.attribute3 := null;
2273          END IF;
2274          IF(p_x_osp_order_rec.attribute4 = FND_API.G_MISS_CHAR) THEN
2275             p_x_osp_order_rec.attribute4 := null;
2276          END IF;
2277          IF(p_x_osp_order_rec.attribute5 = FND_API.G_MISS_CHAR) THEN
2278             p_x_osp_order_rec.attribute5 := null;
2279          END IF;
2280          IF(p_x_osp_order_rec.attribute6 = FND_API.G_MISS_CHAR) THEN
2281             p_x_osp_order_rec.attribute6 := null;
2282          END IF;
2283          IF(p_x_osp_order_rec.attribute7 = FND_API.G_MISS_CHAR) THEN
2284             p_x_osp_order_rec.attribute7 := null;
2285          END IF;
2286          IF(p_x_osp_order_rec.attribute8 = FND_API.G_MISS_CHAR) THEN
2287             p_x_osp_order_rec.attribute8 := null;
2288          END IF;
2289          IF(p_x_osp_order_rec.attribute9 = FND_API.G_MISS_CHAR) THEN
2290             p_x_osp_order_rec.attribute9 := null;
2291          END IF;
2292          IF(p_x_osp_order_rec.attribute10 = FND_API.G_MISS_CHAR) THEN
2293             p_x_osp_order_rec.attribute10 := null;
2294          END IF;
2295          IF(p_x_osp_order_rec.attribute11 = FND_API.G_MISS_CHAR) THEN
2296             p_x_osp_order_rec.attribute11 := null;
2297          END IF;
2298          IF(p_x_osp_order_rec.attribute12 = FND_API.G_MISS_CHAR) THEN
2299             p_x_osp_order_rec.attribute12 := null;
2300          END IF;
2301          IF(p_x_osp_order_rec.attribute13 = FND_API.G_MISS_CHAR) THEN
2302             p_x_osp_order_rec.attribute13 := null;
2303          END IF;
2304          IF(p_x_osp_order_rec.attribute14 = FND_API.G_MISS_CHAR) THEN
2305             p_x_osp_order_rec.attribute14 := null;
2306          END IF;
2307          IF(p_x_osp_order_rec.attribute15 = FND_API.G_MISS_CHAR) THEN
2308             p_x_osp_order_rec.attribute15 := null;
2309          END IF;
2310          IF(p_x_osp_order_rec.vendor_contact_id = FND_API.G_MISS_NUM) THEN
2311             p_x_osp_order_rec.vendor_contact_id := null;
2312          END IF;
2313     END IF;
2314     IF FND_MSG_PUB.count_msg > 0 THEN
2315        RAISE  FND_API.G_EXC_ERROR;
2316        -- RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2317     END IF;
2318     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2319       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'End Procedure');
2320     END IF;
2321     --dbms_output.put_line('Exiting : default_unchanged_order_header');
2322 END default_unchanged_order_header;
2323 
2324 -------------------------------------------------------------------------
2325 PROCEDURE validate_workorder(
2326     p_workorder_id IN NUMBER
2327     )IS
2328     CURSOR val_workorder_id_csr(p_workorder_id IN NUMBER) IS
2329     --Modified by mpothuku on 27-Feb-06 to fix the Perf Bug #4919164
2330     /*
2331     SELECT 'x' FROM ahl_workorders_osp_v
2332     WHERE workorder_id = p_workorder_id
2333     AND upper(department_class_code) = 'VENDOR'
2334     AND job_status_code = G_OSP_WO_RELEASED;
2335     */
2336     SELECT 'x'
2337     FROM ahl_workorders wo,
2338          wip_discrete_jobs wdj,
2339          bom_departments bmd,
2340          ahl_visits_b vst,
2341          ahl_visit_tasks_b vts,
2342          inv_organization_info_v org
2343    WHERE wo.workorder_id = p_workorder_id
2344      AND wo.master_workorder_flag = 'N'
2345      AND wo.visit_task_id = vts.visit_task_id
2346      AND vst.visit_id = vts.visit_id
2347      AND wdj.organization_id = vst.organization_id
2348      AND wdj.wip_entity_id = wo.wip_entity_id
2349      AND wdj.owning_department = bmd.department_id
2350      AND upper(bmd.department_class_code) = 'VENDOR'
2351      AND org.organization_id = vst.organization_id
2352      AND NVL (org.operating_unit, mo_global.get_current_org_id ()) = mo_global.get_current_org_id()
2353      AND wo.status_code = G_OSP_WO_RELEASED;
2354 
2355     l_exist VARCHAR2(1);
2356     L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.validate_workorder';
2357 BEGIN
2358     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2359       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Procedure');
2360     END IF;
2361     IF(p_workorder_id IS NOT NULL) THEN
2362        OPEN val_workorder_id_csr(p_workorder_id);
2363        FETCH val_workorder_id_csr INTO l_exist;
2364        IF(val_workorder_id_csr%NOTFOUND) THEN
2365           FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_WO');
2366           FND_MESSAGE.Set_Token('WORKORDER_ID', p_workorder_id);
2367           FND_MSG_PUB.ADD;
2368        END IF;
2369        CLOSE val_workorder_id_csr;
2370     END IF;
2371     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2372       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'End Procedure');
2373     END IF;
2374 END validate_workorder;
2375 
2376 /*
2377 --Commented by mpothuku on 27-Feb-06 as the following procs. are not being used anymore and the Perf Bug #4919164 --has been logged for some of the cursors in the procedures below
2378 
2379 --------------------------------------------------------------------------------------------------------------
2380 PROCEDURE validate_service_item(
2381     p_workorder_id IN NUMBER,
2382     p_service_item_id IN NUMBER,
2383     p_order_type_code IN VARCHAR2
2384     )IS
2385     CURSOR val_service_item_id_inv_csr(p_service_item_id IN VARCHAR2, p_workorder_id IN NUMBER) IS
2386     SELECT 'x' FROM mtl_system_items_kfv MTL, ahl_workorders_osp_v WO
2387     WHERE MTL.inventory_item_id = p_service_item_id
2388      AND MTL.enabled_flag = G_YES_FLAG
2389      AND MTL.inventory_item_flag = G_NO_FLAG
2390      AND MTL.stock_enabled_flag = G_NO_FLAG
2391      AND NVL(MTL.start_date_active, SYSDATE - 1) <= SYSDATE
2392      AND NVL(MTL.end_date_active, SYSDATE + 1) > SYSDATE
2393      AND MTL.purchasing_enabled_flag = G_YES_FLAG
2394      AND NVL(outside_operation_flag, G_NO_FLAG) = G_NO_FLAG
2395      AND MTL.organization_id = WO.organization_id
2396      AND WO.workorder_id = p_workorder_id;
2397     CURSOR val_service_item_id_wo_csr(p_workorder_id IN NUMBER, p_service_item_id IN NUMBER) IS
2398     SELECT 'x' from ahl_workorders_osp_v
2399     WHERE workorder_id = p_workorder_id
2400      AND NVL(service_item_id,p_service_item_id) = p_service_item_id;
2401      CURSOR val_service_item_id_nll_csr(p_workorder_id IN NUMBER) IS
2402      SELECT 'x' from ahl_workorders_osp_v
2403      WHERE workorder_id = p_workorder_id
2404      AND service_item_id IS NULL;
2405 
2406      CURSOR item_exists_in_wo_org_csr(p_workorder_id IN NUMBER) IS
2407      SELECT 'x'
2408      from ahl_workorders_osp_v wo, ahl_mtl_items_ou_v mtl
2409      where wo.workorder_id = p_workorder_id and
2410      wo.service_item_id = mtl.inventory_item_id and
2411      mtl.inventory_org_id = wo.organization_id;
2412     l_exist VARCHAR2(1);
2413     L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.validate_service_item';
2414 BEGIN
2415     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2416       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Procedure');
2417     END IF;
2418     IF(p_service_item_id IS NOT NULL AND p_service_item_id <> FND_API.G_MISS_NUM) THEN
2419        OPEN val_service_item_id_inv_csr(p_service_item_id, p_workorder_id);
2420        FETCH val_service_item_id_inv_csr INTO l_exist;
2421        IF(val_service_item_id_inv_csr%NOTFOUND) THEN
2422           FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_SVC_ITEM');
2423           FND_MESSAGE.Set_Token('SERVICE_ITEM_ID', p_service_item_id);
2424           FND_MSG_PUB.ADD;
2425           --dbms_output.put_line('Invalid service item not in inventory');
2426        ELSE
2427           -- Valid service item, exists in WO Org
2428           IF ( p_order_type_code = G_OSP_ORDER_TYPE_SERVICE) THEN   -- item exchange enhancement
2429              OPEN val_service_item_id_wo_csr(p_workorder_id, p_service_item_id);
2430              FETCH val_service_item_id_wo_csr INTO l_exist;
2431              IF(val_service_item_id_wo_csr%NOTFOUND) THEN
2432                 -- Service Item does not match that in WO
2433                 OPEN item_exists_in_wo_org_csr(p_workorder_id);
2434                 FETCH item_exists_in_wo_org_csr INTO l_exist;
2435                 IF (item_exists_in_wo_org_csr%FOUND) THEN
2436                    -- WO Service Item does exist in WO Org. So, should have matched!
2437                    FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_SVC_ITEM_MISWO');
2438                    FND_MESSAGE.Set_Token('SERVICE_ITEM_ID', p_service_item_id);
2439                    FND_MSG_PUB.ADD;
2440                    --dbms_output.put_line('Invalid service item , does not match in wo');
2441                 ELSE
2442                    -- WO Service Item does not exist in WO Org.
2443                    -- So, a mismatch is OK
2444                    null;
2445                 END IF;
2446                 CLOSE item_exists_in_wo_org_csr;
2447               END IF;
2448              CLOSE val_service_item_id_wo_csr;
2449            END IF;
2450        END IF;
2451        CLOSE val_service_item_id_inv_csr;
2452     ELSE
2453        -- Service item is null
2454        IF ( p_order_type_code = G_OSP_ORDER_TYPE_SERVICE) THEN --item exchange enhancement
2455           OPEN val_service_item_id_nll_csr(p_workorder_id);
2456           FETCH val_service_item_id_nll_csr INTO l_exist;
2457           IF(val_service_item_id_nll_csr%NOTFOUND) THEN
2458              -- Work order has a not null service item
2459              OPEN item_exists_in_wo_org_csr(p_workorder_id);
2460              FETCH item_exists_in_wo_org_csr INTO l_exist;
2461              IF (item_exists_in_wo_org_csr%FOUND) THEN
2462                 -- WO Service Item does exist in WO Org. So, should not have been null
2463                 FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_SVC_ITEM_NLL');
2464                 FND_MSG_PUB.ADD;
2465                 --dbms_output.put_line('Passed service item is null, in wo its not null');
2466              ELSE
2467                 -- WO Service Item does not exist in WO Org.
2468                 -- So, user entered value can be null
2469                 null;
2470              END IF;
2471              CLOSE item_exists_in_wo_org_csr;
2472           END IF;
2473           CLOSE val_service_item_id_nll_csr;
2474         END IF;
2475     END IF;
2476     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2477       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'End Procedure');
2478     END IF;
2479 END validate_service_item;
2480 
2481 --------------------------------------------------------------------------------------------------------------
2482 PROCEDURE validate_service_item_desc(
2483     p_service_item_id IN NUMBER,
2484     p_service_item_description IN VARCHAR2
2485     ) IS
2486     CURSOR val_service_item_desc_csr(p_service_item_id IN VARCHAR2, p_service_item_description IN VARCHAR2) IS
2487     SELECT 'x' FROM mtl_system_items_kfv MTL
2488     WHERE MTL.inventory_item_id = p_service_item_id
2489     AND MTL.DESCRIPTION = p_service_item_description;
2490     l_exist VARCHAR2(1);
2491     L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.validate_service_item_desc';
2492 BEGIN
2493     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2494       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Procedure');
2495     END IF;
2496     IF(p_service_item_id IS NOT NULL AND p_service_item_id <> FND_API.G_MISS_NUM) THEN
2497        OPEN val_service_item_desc_csr(p_service_item_id , p_service_item_description);
2498        FETCH val_service_item_desc_csr INTO l_exist;
2499        IF(val_service_item_desc_csr%NOTFOUND) THEN
2500           FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_SVC_ITEM_DESC');
2501           FND_MESSAGE.Set_Token('SERVICE_ITEM_ID', p_service_item_id);
2502           FND_MESSAGE.Set_Token('SERVICE_ITEM_DESC', p_service_item_description);
2503           FND_MSG_PUB.ADD;
2504           --dbms_output.put_line('Invalid service item description');
2505        END IF;
2506        CLOSE val_service_item_desc_csr;
2507      END IF;
2508      IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2509       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'End Procedure');
2510     END IF;
2511 END validate_service_item_desc;
2512 
2513 --------------------------------------------------------------------------------------------------------------
2514 */
2515 -- Added by jaramana on January 9, 2008 for the Requisition ER 6034236
2516 PROCEDURE val_svc_item_vs_wo_svc_item(
2517     p_workorder_id IN NUMBER,
2518     p_service_item_id IN NUMBER
2519     )IS
2520     CURSOR val_service_item_id_inv_csr(p_service_item_id IN VARCHAR2, p_workorder_id IN NUMBER) IS
2521     SELECT 'x' FROM mtl_system_items_kfv MTL, ahl_workorders WO, AHL_VISITS_B VST
2522     WHERE MTL.inventory_item_id = p_service_item_id
2523      AND MTL.enabled_flag = G_YES_FLAG
2524      AND MTL.inventory_item_flag = G_NO_FLAG
2525      AND MTL.stock_enabled_flag = G_NO_FLAG
2526      AND NVL(MTL.start_date_active, SYSDATE - 1) <= SYSDATE
2527      AND NVL(MTL.end_date_active, SYSDATE + 1) > SYSDATE
2528      AND MTL.purchasing_enabled_flag = G_YES_FLAG
2529      AND NVL(outside_operation_flag, G_NO_FLAG) = G_NO_FLAG
2530      AND MTL.organization_id = VST.organization_id
2531      AND VST.visit_id = WO.visit_id
2532      AND WO.workorder_id = p_workorder_id;
2533 
2534     CURSOR val_service_item_id_wo_csr(p_workorder_id IN NUMBER, p_service_item_id IN NUMBER) IS
2535     SELECT 'x' from ahl_workorders WO, AHL_ROUTES_B ARB
2536     WHERE WO.workorder_id = p_workorder_id
2537      AND WO.ROUTE_ID = ARB.ROUTE_ID (+)
2538      AND NVL(ARB.service_item_id, p_service_item_id) = p_service_item_id;
2539 
2540     CURSOR val_service_item_id_nll_csr(p_workorder_id IN NUMBER) IS
2541     SELECT 'x' from ahl_workorders WO, AHL_ROUTES_B ARB
2542     WHERE WO.workorder_id = p_workorder_id
2543      AND WO.ROUTE_ID = ARB.ROUTE_ID (+)
2544      AND ARB.service_item_id IS NULL;
2545 
2546     CURSOR item_exists_in_wo_org_csr(p_workorder_id IN NUMBER) IS
2547     SELECT 'x'
2548     from ahl_workorders WO, AHL_ROUTES_B ARB, AHL_VISITS_B VST, ahl_mtl_items_ou_v mtl
2549     where wo.workorder_id = p_workorder_id and
2550      WO.ROUTE_ID = ARB.ROUTE_ID (+) and
2551      ARB.service_item_id = mtl.inventory_item_id and
2552      VST.visit_id = WO.visit_id and
2553      mtl.inventory_org_id = VST.organization_id;
2554 
2555     l_exist VARCHAR2(1);
2556     L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.val_svc_item_vs_wo_svc_item';
2557 
2558 BEGIN
2559     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2560       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Procedure');
2561     END IF;
2562     IF(p_service_item_id IS NOT NULL AND p_service_item_id <> FND_API.G_MISS_NUM) THEN
2563 
2564       OPEN val_service_item_id_inv_csr(p_service_item_id, p_workorder_id);
2565       FETCH val_service_item_id_inv_csr INTO l_exist;
2566       IF(val_service_item_id_inv_csr%NOTFOUND) THEN
2567         FND_MESSAGE.Set_Name(G_APP_NAME, 'AHL_OSP_LN_INV_SVC_ITEM');
2568         FND_MESSAGE.Set_Token('SERVICE_ITEM_ID', p_service_item_id);
2569         FND_MSG_PUB.ADD;
2570         --dbms_output.put_line('Invalid service item not in inventory');
2571       ELSE
2572         -- Valid service item, exists in WO Org
2573          OPEN val_service_item_id_wo_csr(p_workorder_id, p_service_item_id);
2574          FETCH val_service_item_id_wo_csr INTO l_exist;
2575          IF(val_service_item_id_wo_csr%NOTFOUND) THEN
2576             -- Service Item does not match that in WO
2577             OPEN item_exists_in_wo_org_csr(p_workorder_id);
2578             FETCH item_exists_in_wo_org_csr INTO l_exist;
2579             IF (item_exists_in_wo_org_csr%FOUND) THEN
2580                -- WO Service Item does exist in WO Org. So, should have matched!
2581                FND_MESSAGE.Set_Name(G_APP_NAME, 'AHL_OSP_LN_INV_SVC_ITEM_MISWO');
2582                FND_MESSAGE.Set_Token('SERVICE_ITEM_ID', p_service_item_id);
2583                FND_MSG_PUB.ADD;
2584                --dbms_output.put_line('Invalid service item , does not match in wo');
2585             ELSE
2586                -- WO Service Item does not exist in WO Org.
2587                -- So, a mismatch is OK
2588                null;
2589             END IF;
2590             CLOSE item_exists_in_wo_org_csr;
2591           END IF;
2592          CLOSE val_service_item_id_wo_csr;
2593        END IF;
2594       CLOSE val_service_item_id_inv_csr;
2595 
2596     ELSE
2597       -- Service item is null
2598       OPEN val_service_item_id_nll_csr(p_workorder_id);
2599       FETCH val_service_item_id_nll_csr INTO l_exist;
2600       IF(val_service_item_id_nll_csr%NOTFOUND) THEN
2601          -- Work order has a not null service item
2602          OPEN item_exists_in_wo_org_csr(p_workorder_id);
2603          FETCH item_exists_in_wo_org_csr INTO l_exist;
2604          IF (item_exists_in_wo_org_csr%FOUND) THEN
2605             -- WO Service Item does exist in WO Org. So, should not have been null
2606             FND_MESSAGE.Set_Name(G_APP_NAME, 'AHL_OSP_LN_INV_SVC_ITEM_NLL');
2607             FND_MSG_PUB.ADD;
2608             --dbms_output.put_line('Passed service item is null, in wo its not null');
2609          ELSE
2610             -- WO Service Item does not exist in WO Org.
2611             -- So, user entered value can be null
2612             null;
2613          END IF;
2614          CLOSE item_exists_in_wo_org_csr;
2615       END IF;
2616       CLOSE val_service_item_id_nll_csr;
2617     END IF;
2618 
2619     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2620       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'End Procedure');
2621     END IF;
2622 END val_svc_item_vs_wo_svc_item;
2623 
2624 --------------------------------------------------------------------------------------------------------------
2625 
2626 --p_org_id added by mpothuku to fix the Perf Bug #4919164
2627 PROCEDURE validate_service_item_uom(
2628    p_service_item_id IN NUMBER,
2629    p_service_item_uom_code IN VARCHAR2,
2630    p_org_id IN NUMBER
2631    )IS
2632    CURSOR val_service_item_uom_csr(p_service_item_id IN NUMBER,p_service_item_uom_code IN VARCHAR2, p_org_id IN NUMBER ) IS
2633    SELECT 'x' FROM ahl_item_class_uom_v
2634    WHERE inventory_item_id = p_service_item_id
2635    AND uom_code = p_service_item_uom_code
2636    AND inventory_org_id = p_org_id;
2637    CURSOR val_uom_code_csr(p_service_item_uom_code IN VARCHAR2) IS
2638    SELECT 'x' from mtl_units_of_measure_vl
2639    WHERE uom_code = p_service_item_uom_code;
2640    l_exist VARCHAR2(1);
2641    L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.validate_service_item_uom';
2642 BEGIN
2643     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2644       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Procedure'
2645                       || '- Service Item Id: ' || p_service_item_id || 'uom_code' || p_service_item_uom_code );
2646     END IF;
2647     IF(p_service_item_id IS NOT NULL AND p_service_item_id <> FND_API.G_MISS_NUM) THEN
2648        IF(p_service_item_uom_code IS NOT NULL AND p_service_item_uom_code <> FND_API.G_MISS_CHAR) THEN
2649           OPEN val_service_item_uom_csr(p_service_item_id,p_service_item_uom_code,p_org_id);
2650           FETCH val_service_item_uom_csr INTO l_exist;
2651           IF(val_service_item_uom_csr%NOTFOUND) THEN
2652             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_UOM');
2653             FND_MESSAGE.Set_Token('UOM_CODE', p_service_item_uom_code);
2654            -- FND_MESSAGE.Set_Token('SERVICE_ITEM_ID', p_service_item_id);
2655             FND_MSG_PUB.ADD;
2656           END IF;
2657           CLOSE val_service_item_uom_csr;
2658         END IF;
2659     ELSE
2660         IF(p_service_item_uom_code IS NOT NULL AND p_service_item_uom_code <> FND_API.G_MISS_CHAR) THEN
2661            OPEN val_uom_code_csr(p_service_item_uom_code);
2662            FETCH val_uom_code_csr INTO l_exist;
2663            IF(val_uom_code_csr%NOTFOUND) THEN
2664              FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_UOM');
2665              FND_MESSAGE.Set_Token('UOM_CODE', p_service_item_uom_code);
2666            --  FND_MESSAGE.Set_Token('SERVICE_ITEM_ID', p_service_item_id);
2667              FND_MSG_PUB.ADD;
2668            END IF;
2669            CLOSE val_uom_code_csr;
2670         END IF;
2671     END IF;
2672     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2673       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'End Procedure');
2674     END IF;
2675 END validate_service_item_uom;
2676 
2677 --------------------------------------------------------------------------------------------------------------
2678 PROCEDURE validate_po_line_type(
2679    p_po_line_type_id IN NUMBER
2680    )IS
2681    CURSOR val_po_line_type_id_csr(p_po_line_type_id IN NUMBER) IS
2682    SELECT 'x' FROM po_line_types
2683    WHERE line_type_id = p_po_line_type_id and
2684    ORDER_TYPE_LOOKUP_CODE = 'QUANTITY' and
2685    NVL(OUTSIDE_OPERATION_FLAG, G_NO_FLAG) = G_NO_FLAG;
2686    l_exist VARCHAR2(1);
2687    L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.validate_po_line_type';
2688 BEGIN
2689     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2690       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Procedure');
2691     END IF;
2692     IF(p_po_line_type_id IS NOT NULL AND p_po_line_type_id <> FND_API.G_MISS_NUM) THEN
2693        OPEN val_po_line_type_id_csr(p_po_line_type_id);
2694        FETCH val_po_line_type_id_csr INTO l_exist;
2695        IF(val_po_line_type_id_csr%NOTFOUND) THEN
2696           FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_LNTYP_ID');
2697           FND_MESSAGE.Set_Token('LINE_TYPE_ID', p_po_line_type_id);
2698           FND_MSG_PUB.ADD;
2699        END IF;
2700        CLOSE val_po_line_type_id_csr;
2701     END IF;
2702     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2703       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'End Procedure');
2704     END IF;
2705 END validate_po_line_type;
2706 
2707 --------------------------------------------------------------------------------------------------------------
2708 PROCEDURE validate_po_line(
2709    p_po_line_id IN NUMBER,
2710    p_osp_order_id IN NUMBER
2711    )IS
2712    CURSOR val_po_line_id_csr(p_po_line_id IN NUMBER, p_osp_order_id IN NUMBER) IS
2713    SELECT 'x' FROM po_lines_all POL, ahl_osp_orders_b OO
2714     WHERE POL.po_line_id = p_po_line_id
2715     AND OO.osp_order_id = p_osp_order_id
2716     AND POL.PO_HEADER_ID = OO.po_header_id
2717     -- Added by jaramana on January 9, 2008 to fix the Bug 5358438/5967633
2718     AND NVL(POL.CANCEL_FLAG, 'N') <> 'Y'
2719     --the line_id should not have already been associated to the same osp order
2720     AND NOT EXISTS (select 1 from ahl_osp_order_lines
2721                      where osp_order_id = p_osp_order_id
2722                        and po_line_id = p_po_line_id );
2723     -- jaramana End
2724     l_exist VARCHAR2(1);
2725     L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.validate_po_line';
2726 BEGIN
2727     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2728       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Procedure');
2729     END IF;
2730     IF(p_po_line_id IS NOT NULL AND p_po_line_id <> FND_API.G_MISS_NUM) THEN
2731        OPEN val_po_line_id_csr(p_po_line_id, p_osp_order_id);
2732        FETCH val_po_line_id_csr INTO l_exist;
2733        IF(val_po_line_id_csr%NOTFOUND) THEN
2734           FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_PO_LINE_INV');
2735           FND_MESSAGE.Set_Token('PO_LINE_ID', p_po_line_id);
2736           FND_MSG_PUB.ADD;
2737        END IF;
2738        CLOSE val_po_line_id_csr;
2739     END IF;
2740     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2741       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'End Procedure');
2742     END IF;
2743 END validate_po_line;
2744 
2745 -------------------------------------------------------------------------------------------------------------
2746 PROCEDURE validate_exchange_instance_id(p_exchange_instance_id IN NUMBER) IS
2747     CURSOR val_instance_id_csr(p_instance_id IN NUMBER) IS
2748       SELECT 'x' FROM csi_item_instances csi
2749         WHERE instance_id = p_instance_id
2750           AND trunc(sysdate) < trunc(nvl(active_end_date, sysdate+1))
2751           AND NOT EXISTS (select subject_id from csi_ii_relationships where
2752                            subject_id = p_instance_id and
2753                            relationship_type_code = 'COMPONENT-OF' and
2754                            trunc(sysdate) >= trunc(nvl(active_start_date,sysdate)) and
2755                            trunc(sysdate) < trunc(nvl(active_end_date, sysdate+1))
2756                           ) ;
2757     l_exist VARCHAR2(1);
2758     L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.validate_exchange_instance_id';
2759 BEGIN
2760      IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2761         FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Procedure');
2762      END IF;
2763      IF(p_exchange_instance_id IS NOT NULL AND p_exchange_instance_id <> FND_API.G_MISS_NUM) THEN
2764         OPEN val_instance_id_csr(p_exchange_instance_id);
2765         FETCH val_instance_id_csr INTO l_exist;
2766         IF (val_instance_id_csr %NOTFOUND) THEN
2767           FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INSTANCE_ID_INV');
2768           FND_MESSAGE.Set_Token('EXCHANGE_INSTANCE_ID', p_exchange_instance_id);
2769           FND_MSG_PUB.ADD;
2770         END IF;
2771       END IF;
2772       IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2773          FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'End Procedure');
2774       END IF;
2775 END validate_exchange_instance_id;
2776 
2777 
2778 /*
2779 - Changed by jaramana on January 9, 2008 for the Requisition ER 6034236 (We are not using this procedure anymore):
2780 Commented the nullify_exchange_instance procedure and placed it at the end of package. Please note that instead of using this
2781 heavy weight procedure we are directly updating the AHL_OSP_ORDER_LINES table
2782 */
2783 
2784 --------------------------------------------------------------------------------------------------------------
2785 -- Jerry made minor changes to this procedure for ISO in May 27, 2005
2786 -- Basically, just replace g_old_status_code with l_old_status_code which
2787 -- is queried on the fly
2788 --------------------------------------------------------------------------------------------------------------
2789 PROCEDURE process_order_status_change(
2790    p_x_osp_order_rec IN OUT NOCOPY osp_order_rec_type)
2791 IS
2792    CURSOR val_order_has_lines(p_osp_order_id IN NUMBER)IS
2793    SELECT 'x' FROM ahl_osp_order_lines
2794    WHERE osp_order_id = p_osp_order_id;
2795    l_exist VARCHAR2(1);
2796    CURSOR val_order_has_ship_lines(p_osp_order_id IN NUMBER)IS
2797    SELECT oe_ship_line_id, oe_return_line_id, osp_line_number FROM ahl_osp_order_lines
2798    WHERE osp_order_id = p_osp_order_id;
2799    l_oe_ship_line_id NUMBER;
2800    l_oe_return_line_id NUMBER;
2801    l_line_number NUMBER;
2802    l_count NUMBER;
2803    l_batch_id NUMBER;
2804    l_request_id NUMBER;
2805    l_interface_header_id NUMBER;
2806    l_return_status VARCHAR2(1);
2807    l_msg_count NUMBER;
2808    l_msg_data  VARCHAR2(1000);
2809    l_shipment_IDs_Tbl       AHL_OSP_SHIPMENT_PUB.Ship_ID_Tbl_Type;
2810    CURSOR val_all_wo_closed(p_osp_order_id IN NUMBER) IS
2811    --Modified by mpothuku on 27-Feb-06 to use ahl_workorders instead of ahl_workorders_osp_v
2812    --to fix the Perf Bug #4919164
2813    SELECT 'x' from ahl_osp_order_lines OL, ahl_workorders WO
2814    WHERE OL.osp_order_id = p_osp_order_id
2815    AND OL.workorder_id = WO.workorder_id
2816    AND OL.status_code IS NULL
2817    AND WO.status_code NOT IN(G_OSP_WO_CANCELLED,G_OSP_WO_CLOSED);
2818    CURSOR val_order_lines_csr(p_osp_order_id IN NUMBER) IS
2819    SELECT osp_line_number, service_item_id, service_item_description, service_item_uom_code, quantity, need_by_date, po_line_type_id
2820    FROM AHL_OSP_ORDER_LINES
2821    WHERE osp_order_id = p_osp_order_id
2822    ORDER BY osp_line_number;
2823    CURSOR get_old_status IS
2824      SELECT status_code, object_version_number
2825        FROM ahl_osp_orders_b
2826       WHERE osp_order_id = p_x_osp_order_rec.osp_order_id;
2827    --jrotich added for osp receiving
2828    CURSOR val_all_acc_closed(p_osp_order_id IN NUMBER) IS
2829    SELECT 'x' from ahl_osp_order_lines OL, ahl_osp_accomplishments AOA
2830    WHERE OL.osp_order_id = p_osp_order_id
2831    AND ol.osp_order_line_id = AOA.OSP_ORDER_LINE_ID
2832    AND AOA.STATUS_CODE = G_OSP_AOA_OPEN;
2833    --jrotich end
2834 
2835    l_osp_line_number  NUMBER;
2836    l_service_item_id  NUMBER;
2837    l_service_item_description VARCHAR(2000);
2838    l_service_item_uom_code VARCHAR(30);
2839    l_quantity   NUMBER;
2840    l_need_by_date DATE;
2841    l_po_line_type_id NUMBER;
2842    l_old_status_code VARCHAR2(30);
2843    l_temp_status_code VARCHAR2(30);
2844    l_new_ovn NUMBER;
2845    l_old_ovn NUMBER;
2846    L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || 'process_order_status_change';
2847 BEGIN
2848    IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2849      FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin',
2850                     'osp_order_id='||p_x_osp_order_rec.osp_order_id||
2851                     'status='||p_x_osp_order_rec.status_code||
2852                     'ovn='||p_x_osp_order_rec.object_version_number);
2853    END IF;
2854    OPEN get_old_status;
2855    FETCH get_old_status INTO l_old_status_code, l_old_ovn;
2856    CLOSE get_old_status;
2857    IF(p_x_osp_order_rec.order_type_code IN (G_OSP_ORDER_TYPE_SERVICE, G_OSP_ORDER_TYPE_EXCHANGE) AND p_x_osp_order_rec.operation_flag = G_OP_UPDATE) THEN
2858       --IF(g_old_status_code IN (G_OSP_ENTERED_STATUS, G_OSP_SUB_FAILED_STATUS) AND
2859       -- Added by jaramana on January 9, 2008 for the Requisition ER 6034236
2860       IF(l_old_status_code IN (G_OSP_ENTERED_STATUS, G_OSP_SUB_FAILED_STATUS, G_OSP_REQ_SUB_FAILED_STATUS) AND
2861          p_x_osp_order_rec.status_code IN (G_OSP_SUBMITTED_STATUS, G_OSP_REQ_SUBMITTED_STATUS)) THEN
2862       -- jaramana End
2863          -- validate fields and submit for PO creation
2864          IF(p_x_osp_order_rec.vendor_id IS NULL) THEN
2865             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_SUB_VEN_NLL');
2866             FND_MSG_PUB.ADD;
2867          END IF;
2868          IF (p_x_osp_order_rec.vendor_site_id IS NULL) THEN
2869             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_SUB_VENST_NLL');
2870             FND_MSG_PUB.ADD;
2871          END IF;
2872          IF (p_x_osp_order_rec.po_agent_id IS NULL) THEN
2873             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_SUB_BUYER_NLL');
2874             FND_MSG_PUB.ADD;
2875          END IF;
2876          IF (p_x_osp_order_rec.po_header_id IS NOT NULL) THEN
2877             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_SUB_PO_NNLL');
2878             FND_MSG_PUB.ADD;
2879          END IF;
2880 
2881          -- Added by jaramana on January 9, 2008 for the Requisition ER 6034236
2882          IF (p_x_osp_order_rec.po_req_header_id IS NOT NULL) THEN
2883             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_SUB_REQ_NNLL');
2884             FND_MSG_PUB.ADD;
2885          END IF;
2886          -- jaramana End
2887 
2888          -- check whether order has lines
2889          OPEN val_order_has_lines(p_x_osp_order_rec.osp_order_id);
2890          FETCH val_order_has_lines INTO l_exist;
2891          IF(val_order_has_lines%NOTFOUND) THEN
2892             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_SUB_NO_LNS');
2893             FND_MSG_PUB.ADD;
2894          END IF;
2895          CLOSE val_order_has_lines;
2896           -- check null values in order lines
2897           OPEN val_order_lines_csr(p_x_osp_order_rec.osp_order_id);
2898           LOOP
2899             FETCH val_order_lines_csr INTO l_osp_line_number, l_service_item_id, l_service_item_description,
2900                                            l_service_item_uom_code, l_quantity, l_need_by_date, l_po_line_type_id;
2901              EXIT WHEN  val_order_lines_csr%NOTFOUND;
2902               --service item id and service item description cannot be both null
2903              IF(l_service_item_description IS NULL  AND l_service_item_id IS NULL) THEN
2904                FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_SUB_ITMID_NL');
2905                FND_MESSAGE.Set_Token('LINE_NUM', l_osp_line_number);
2906                FND_MSG_PUB.ADD;
2907              END IF;
2908              --l_service_item_uom_code cannot be null
2909              IF(l_service_item_uom_code IS NULL) THEN
2910                FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_SUB_UOM_NL');
2911                FND_MESSAGE.Set_Token('LINE_NUM', l_osp_line_number);
2912                FND_MSG_PUB.ADD;
2913              END IF;
2914              --quantity cannot be null
2915              IF(l_quantity IS NULL) THEN
2916                FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_SUB_QTY_NL');
2917                FND_MESSAGE.Set_Token('LINE_NUM', l_osp_line_number);
2918                FND_MSG_PUB.ADD;
2919              END IF;
2920              --need_by_date cannot be null
2921              IF(l_need_by_date IS NULL) THEN
2922                FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_SUB_NBD_NL');
2923                FND_MESSAGE.Set_Token('LINE_NUM', l_osp_line_number);
2924                FND_MSG_PUB.ADD;
2925              END IF;
2926              --po_line_type_id cannot be null
2927              IF(l_po_line_type_id IS NULL) THEN
2928                FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_SUB_PO_LNTYP_NL');
2929                FND_MESSAGE.Set_Token('LINE_NUM', l_osp_line_number);
2930                FND_MSG_PUB.ADD;
2931              END IF;
2932          END LOOP;
2933          CLOSE val_order_lines_csr;
2934          IF FND_MSG_PUB.count_msg > 0 THEN
2935             RAISE  FND_API.G_EXC_ERROR;
2936          -- Changed by jaramana on January 9, 2008 for the Requisition ER 6034236
2937          ELSIF(p_x_osp_order_rec.status_code =  G_OSP_SUBMITTED_STATUS) THEN
2938          -- jaramana End
2939             -- submit for PO creation
2940             AHL_OSP_PO_PVT.Create_Purchase_Order
2941             (
2942              p_api_version   => 1.0 ,
2943              p_osp_order_id  => p_x_osp_order_rec.osp_order_id,
2944              x_batch_id      => l_batch_id,
2945              x_request_id    => l_request_id,
2946              x_interface_header_id  =>l_interface_header_id,
2947              x_return_status => l_return_status,
2948              x_msg_count  => l_msg_count,
2949              x_msg_data  => l_msg_data
2950             );
2951             OPEN get_old_status;
2952             FETCH get_old_status INTO l_temp_status_code, l_new_ovn;
2953             CLOSE get_old_status;
2954             IF (l_return_status <> FND_API.G_RET_STS_SUCCESS OR
2955                 l_new_ovn <> l_old_ovn+1) THEN
2956               FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_SUB_PO_FLD');
2957               FND_MSG_PUB.ADD;
2958             ELSE
2959               p_x_osp_order_rec.object_version_number := l_new_ovn;
2960               p_x_osp_order_rec.po_request_id := l_request_id;
2961               p_x_osp_order_rec.po_interface_header_id := l_interface_header_id;
2962               p_x_osp_order_rec.po_batch_id := l_batch_id;
2963               IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2964                 FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin',
2965                     'batch_id='||p_x_osp_order_rec.po_batch_id||
2966                     'request_id='||p_x_osp_order_rec.po_request_id||
2967                     'interface_id='||p_x_osp_order_rec.po_interface_header_id);
2968               END IF;
2969             END IF;
2970           -- Added by jaramana on January 9, 2008 for the Requisition ER 6034236
2971           ELSIF(p_x_osp_order_rec.status_code = G_OSP_REQ_SUBMITTED_STATUS) THEN
2972             -- submit for PO creation
2973             AHL_OSP_PO_REQ_PVT.Create_PO_Requisition
2974             (
2975              p_api_version   => 1.0 ,
2976              p_osp_order_id  => p_x_osp_order_rec.osp_order_id,
2977              x_batch_id      => l_batch_id,
2978              x_request_id    => l_request_id,
2979              x_return_status => l_return_status,
2980              x_msg_count  => l_msg_count,
2981              x_msg_data  => l_msg_data
2982             );
2983             OPEN get_old_status;
2984             FETCH get_old_status INTO l_temp_status_code, l_new_ovn;
2985             CLOSE get_old_status;
2986             IF (l_return_status <> FND_API.G_RET_STS_SUCCESS OR
2987                 l_new_ovn <> l_old_ovn+1) THEN
2988               FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_SUB_REQ_FLD');
2989               FND_MSG_PUB.ADD;
2990             ELSE
2991               p_x_osp_order_rec.object_version_number := l_new_ovn;
2992               p_x_osp_order_rec.po_request_id := l_request_id;
2993               p_x_osp_order_rec.po_batch_id := l_batch_id;
2994               IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
2995                 FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin',
2996                     'batch_id='||p_x_osp_order_rec.po_batch_id||
2997                     'request_id='||p_x_osp_order_rec.po_request_id);
2998               END IF;
2999             END IF;
3000           -- jaramana End
3001 
3002          END IF;
3003       --ELSIF(g_old_status_code = G_OSP_PO_CREATED_STATUS AND p_x_osp_order_rec.status_code = G_OSP_CLOSED_STATUS) THEN
3004       -- Changed by jaramana on January 9, 2008 for the Requisition ER 6034236
3005       --ELSIF(l_old_status_code = G_OSP_PO_CREATED_STATUS AND p_x_osp_order_rec.status_code = G_OSP_CLOSED_STATUS) THEN
3006       ELSIF(l_old_status_code IN (G_OSP_PO_CREATED_STATUS,G_OSP_REQ_CREATED_STATUS) AND p_x_osp_order_rec.status_code = G_OSP_CLOSED_STATUS) THEN
3007 
3008          -- validate fields and ask whether PO and SO is closed.
3009          IF(l_old_status_code = G_OSP_PO_CREATED_STATUS AND p_x_osp_order_rec.po_header_id IS NULL) THEN
3010             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_CL_PO_NLL');
3011             FND_MSG_PUB.ADD;
3012          ELSIF(l_old_status_code = G_OSP_REQ_CREATED_STATUS AND p_x_osp_order_rec.po_req_header_id IS NULL) THEN
3013             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_CL_REQ_NLL');
3014             FND_MSG_PUB.ADD;
3015          ELSE --This would mean either po_header_id is not null OR po_req_header_id is not NULL
3016             -- If the status is PO_CREATED, ask PO whether it is closed/cancelled.
3017             IF (l_old_status_code = G_OSP_PO_CREATED_STATUS AND
3018             AHL_OSP_PO_PVT.Is_PO_Closed(p_x_osp_order_rec.po_header_id) = G_NO_FLAG ) THEN
3019               FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_CL_PO_OPEN');
3020               FND_MSG_PUB.ADD;
3021             END IF;
3022             -- If the status is REQ_CREATED, ask Requisition whether the order can be closed.
3023             IF(l_old_status_code = G_OSP_REQ_CREATED_STATUS AND
3024             AHL_OSP_PO_REQ_PVT.Is_PO_Req_Closed(p_x_osp_order_rec.po_req_header_id) = G_NO_FLAG ) THEN
3025               FND_MESSAGE.Set_Name('AHL', 'AHL_OSP_INV_CL_REQ_OPEN');
3026               FND_MSG_PUB.ADD;
3027             END IF;
3028 
3029             -- ask SO whether can close
3030             IF(p_x_osp_order_rec.oe_header_id IS NOT NULL) THEN
3031               IF NOT(FND_API.TO_BOOLEAN(AHL_OSP_SHIPMENT_PUB.Is_Order_Header_Closed( p_x_osp_order_rec.oe_header_id))) THEN
3032                 FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_CL_SO_OPEN');
3033                 FND_MSG_PUB.ADD;
3034               END IF;
3035             END IF;
3036             -- check whether all the workorders are closed.
3037             OPEN val_all_wo_closed(p_x_osp_order_rec.osp_order_id);
3038             FETCH val_all_wo_closed INTO l_exist;
3039             IF(val_all_wo_closed%FOUND) THEN
3040                FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_CL_WO_OPEN');
3041                FND_MSG_PUB.ADD;
3042             END IF;
3043             CLOSE val_all_wo_closed;
3044 	    -- jrotich added for osp receiving project
3045   	    -- check whether all mr's are accomplished
3046 	    OPEN val_all_acc_closed(p_x_osp_order_rec.osp_order_id);
3047             FETCH val_all_acc_closed INTO l_exist;
3048             IF(val_all_acc_closed%FOUND) THEN
3049               FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_CL_ACC_OPEN');
3050               FND_MSG_PUB.ADD;
3051             END IF;
3052             CLOSE val_all_acc_closed;
3053 	    -- jrotich end
3054          END IF;
3055       END IF;
3056     ELSIF(p_x_osp_order_rec.order_type_code IN(G_OSP_ORDER_TYPE_LOAN, G_OSP_ORDER_TYPE_BORROW) AND p_x_osp_order_rec.operation_flag = G_OP_UPDATE)THEN
3057       --IF(g_old_status_code = G_OSP_ENTERED_STATUS  AND
3058       IF(l_old_status_code = G_OSP_ENTERED_STATUS  AND
3059          p_x_osp_order_rec.status_code = G_OSP_SUBMITTED_STATUS) THEN
3060          IF(p_x_osp_order_rec.customer_id IS NULL AND p_x_osp_order_rec.order_type_code = G_OSP_ORDER_TYPE_LOAN) THEN
3061 			--only loan type requires customer id for borrow it's optional (bug fix).
3062             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_SUB_CUST_NLL');
3063             FND_MSG_PUB.ADD;
3064          END IF;
3065 	/* Change made by mpothuku to make the contract_id optional on 12/16/04
3066 	   This is a work around and has to be revoked after PM comes up with the Service Contract Integration
3067 	*/
3068 	-- Changes start
3069 	/*
3070          IF(p_x_osp_order_rec.contract_id IS NULL) THEN
3071             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_SUB_CTRCT_NLL');
3072             FND_MSG_PUB.ADD;
3073          END IF;
3074 	*/
3075 	-- Changes by mpothuku End
3076          IF(p_x_osp_order_rec.oe_header_id IS NULL) THEN
3077             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_SUB_OE_NLL');
3078             FND_MSG_PUB.ADD;
3079          END IF;
3080          -- check whether order has lines and all lines have shipping information
3081          OPEN val_order_has_ship_lines(p_x_osp_order_rec.osp_order_id);
3082          l_count := 0;
3083          LOOP
3084            FETCH val_order_has_ship_lines INTO l_oe_ship_line_id, l_oe_return_line_id, l_line_number;
3085            IF(val_order_has_ship_lines%NOTFOUND) THEN
3086              EXIT;
3087            ELSIF (l_oe_ship_line_id IS NULL AND  l_oe_return_line_id IS NULL) THEN
3088               FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_SUB_NO_SHIP_LN');
3089               FND_MESSAGE.Set_Token('LINE_NUMBER', l_line_number);
3090               FND_MSG_PUB.ADD;
3091               l_count := l_count + 1;
3092            ELSE
3093               l_count := l_count + 1;
3094            END IF;
3095          END LOOP;
3096          IF(l_count < 1) THEN
3097             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_SUB_NO_LNS');
3098             FND_MSG_PUB.ADD;
3099          END IF;
3100          CLOSE val_order_has_ship_lines;
3101          IF FND_MSG_PUB.count_msg > 0 THEN
3102             RAISE  FND_API.G_EXC_ERROR;
3103          ELSE
3104             l_shipment_IDs_Tbl(1) := p_x_osp_order_rec.oe_header_id;
3105             AHL_OSP_SHIPMENT_PUB.Book_Order(
3106                        p_api_version      => 1.0,
3107                        p_init_msg_list    => FND_API.G_FALSE,
3108                        p_commit           => FND_API.G_FALSE,
3109                        p_validation_level => FND_API.G_VALID_LEVEL_FULL,
3110                        p_oe_header_tbl    => l_shipment_IDs_Tbl,
3111                        x_return_status    => l_return_status,
3112                        x_msg_count        => l_msg_count,
3113                        x_msg_data         => l_msg_data
3114             );
3115             IF (l_return_status <> FND_API.G_RET_STS_SUCCESS)THEN
3116                FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_SUB_SO_FLD');
3117                FND_MSG_PUB.ADD;
3118             END IF;
3119          END IF;
3120       --ELSIF(g_old_status_code = G_OSP_SUBMITTED_STATUS AND p_x_osp_order_rec.status_code = G_OSP_CLOSED_STATUS) THEN
3121       ELSIF(l_old_status_code = G_OSP_SUBMITTED_STATUS AND p_x_osp_order_rec.status_code = G_OSP_CLOSED_STATUS) THEN
3122          -- validate fields and ask SO whether order can be closed.
3123          --dbms_output.put_line('Sales order closed ');
3124          IF(p_x_osp_order_rec.oe_header_id IS NULL) THEN
3125             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_CL_OE_NLL');
3126             FND_MSG_PUB.ADD;
3127          ELSE
3128             --dbms_output.put_line('Sales order closed ' || AHL_OSP_SHIPMENT_PUB.Is_Order_Header_Closed( p_x_osp_order_rec.oe_header_id));
3129             IF NOT(FND_API.TO_BOOLEAN(AHL_OSP_SHIPMENT_PUB.Is_Order_Header_Closed( p_x_osp_order_rec.oe_header_id))) THEN
3130               --dbms_output.put_line('Sales order is not closed');
3131               FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_CL_SO_OPEN');
3132               FND_MSG_PUB.ADD;
3133             END IF;
3134             -- check whether all the workorders are closed.
3135             OPEN val_all_wo_closed(p_x_osp_order_rec.osp_order_id);
3136             FETCH val_all_wo_closed INTO l_exist;
3137             IF(val_all_wo_closed%FOUND) THEN
3138                FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_CL_WO_OPEN');
3139                FND_MSG_PUB.ADD;
3140             END IF;
3141             CLOSE val_all_wo_closed;
3142          END IF;
3143       END IF;
3144     END IF;
3145     IF FND_MSG_PUB.count_msg > 0 THEN
3146       RAISE  FND_API.G_EXC_ERROR;
3147     END IF;
3148     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3149       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'End Procedure');
3150     END IF;
3151 END process_order_status_change;
3152 
3153 --------------------------------------------------------------------------------------------------------------
3154 PROCEDURE delete_cancel_so(
3155    p_oe_header_id             IN        NUMBER,
3156    p_del_cancel_so_lines_tbl  IN        del_cancel_so_lines_tbl_type,
3157    p_cancel_flag              IN        VARCHAR2  := FND_API.G_FALSE
3158 ) IS
3159   l_Ship_ID_Tbl AHL_OSP_SHIPMENT_PUB.Ship_ID_Tbl_Type;
3160   l_return_status VARCHAR2(1);
3161   l_msg_count     NUMBER;
3162   l_msg_data      VARCHAR2(1000);
3163   CURSOR can_del_ship_line_csr(p_osp_order_id IN NUMBER, p_oe_ship_line_id IN NUMBER) IS
3164   SELECT 'x' FROM ahl_osp_order_lines
3165   WHERE osp_order_id = p_osp_order_id
3166   AND oe_ship_line_id = p_oe_ship_line_id
3167   AND status_code IS NULL;
3168   CURSOR can_del_return_line_csr(p_osp_order_id IN NUMBER, p_oe_return_line_id IN NUMBER) IS
3169   SELECT 'x' FROM ahl_osp_order_lines
3170   WHERE osp_order_id = p_osp_order_id
3171   AND oe_return_line_id = p_oe_return_line_id
3172   AND status_code IS NULL;
3173   CURSOR line_already_shipped_csr(p_oe_line_id IN NUMBER) IS
3174     SELECT 'x' from oe_order_lines_all
3175       where line_id = p_oe_line_id AND
3176             shipped_quantity > 0;
3177   l_exist VARCHAR2(1);
3178   l_index NUMBER := 1;
3179   l_unique_flag BOOLEAN := TRUE;
3180   L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.delete_cancel_so';
3181 BEGIN
3182    IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3183       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Procedure');
3184     END IF;
3185    IF(p_oe_header_id IS NOT NULL) THEN
3186       -- CALL SO to delete/cancel SO
3187       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3188         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Calling AHL_OSP_SHIPMENT_PUB.Delete_Cancel_Order with p_oe_header_id = ' || p_oe_header_id);
3189       END IF;
3190       AHL_OSP_SHIPMENT_PUB.Delete_Cancel_Order (
3191           p_api_version              => 1.0,
3192           p_oe_header_id             => p_oe_header_id,
3193           p_oe_lines_tbl             => l_Ship_ID_Tbl,
3194           p_cancel_flag              => p_cancel_flag,
3195           x_return_status            => l_return_status ,
3196           x_msg_count                => l_msg_count ,
3197           x_msg_data                 => l_msg_data
3198       );
3199       IF(l_return_status <> FND_API.G_RET_STS_SUCCESS)THEN
3200          FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_SO_CAN_FLD');
3201          FND_MSG_PUB.ADD;
3202       END IF;
3203    ELSIF ( p_del_cancel_so_lines_tbl IS NOT NULL) THEN
3204       IF NOT(p_del_cancel_so_lines_tbl.COUNT > 0) THEN
3205        RETURN;
3206       END IF;
3207       FOR i IN p_del_cancel_so_lines_tbl.FIRST..p_del_cancel_so_lines_tbl.LAST  LOOP
3208           IF(p_del_cancel_so_lines_tbl(i).oe_ship_line_id IS NOT NULL) THEN
3209              IF(l_Ship_ID_Tbl.COUNT > 0) THEN
3210                l_unique_flag := TRUE;
3211                -- Check if this shipment line has already been included for deletion
3212                -- If so, set the l_unique_flag to false to prevent duplicates
3213                FOR j IN l_Ship_ID_Tbl.FIRST..l_Ship_ID_Tbl.LAST  LOOP
3214                  IF(l_Ship_ID_Tbl(j) = p_del_cancel_so_lines_tbl(i).oe_ship_line_id) THEN
3215                    l_unique_flag := FALSE;
3216                    EXIT;
3217                  END IF;
3218                END LOOP;
3219              END IF;
3220              IF(l_unique_flag) THEN
3221                 -- Not a duplicate
3222                 -- Check if there are any active OSP Lines for this Shipment
3223                 OPEN can_del_ship_line_csr(p_del_cancel_so_lines_tbl(i).osp_order_id, p_del_cancel_so_lines_tbl(i).oe_ship_line_id);
3224                 FETCH can_del_ship_line_csr INTO l_exist;
3225                 IF(can_del_ship_line_csr%NOTFOUND) THEN
3226                    -- No other active OSP Line for this shipment
3227                    -- Check if the shipment has already occured
3228                    OPEN line_already_shipped_csr(p_del_cancel_so_lines_tbl(i).oe_ship_line_id);
3229                    FETCH line_already_shipped_csr INTO l_exist;
3230                    IF (line_already_shipped_csr%NOTFOUND) THEN
3231                      -- Line not yet shipped: Include for deletion
3232                      IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3233                        FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Shipment line with id ' || p_del_cancel_so_lines_tbl(i).oe_ship_line_id || ' included for deletion/cancellation.');
3234                      END IF;
3235                      l_Ship_ID_Tbl(l_index) := p_del_cancel_so_lines_tbl(i).oe_ship_line_id;
3236                      l_index := l_index + 1;
3237                    END IF;
3238                    CLOSE line_already_shipped_csr;
3239                 END IF;
3240                 CLOSE can_del_ship_line_csr;
3241              END IF;
3242           END IF;
3243           IF(p_del_cancel_so_lines_tbl(i).oe_return_line_id IS NOT NULL) THEN
3244              IF(l_Ship_ID_Tbl.COUNT > 0) THEN
3245                l_unique_flag := TRUE;
3246                -- Check if this shipment line has already been included for deletion
3247                -- If so, set the l_unique_flag to false to prevent duplicates
3248                FOR j IN l_Ship_ID_Tbl.FIRST..l_Ship_ID_Tbl.LAST  LOOP
3249                  IF(l_Ship_ID_Tbl(j) = p_del_cancel_so_lines_tbl(i).oe_return_line_id) THEN
3250                    l_unique_flag := FALSE;
3251                    EXIT;
3252                  END IF;
3253                END LOOP;
3254              END IF;
3255              IF(l_unique_flag) THEN
3256                 -- Not a duplicate
3257                 -- Check if there are any active OSP Lines for this Return Shipment
3258                 OPEN can_del_return_line_csr(p_del_cancel_so_lines_tbl(i).osp_order_id,p_del_cancel_so_lines_tbl(i).oe_return_line_id);
3259                 FETCH can_del_return_line_csr INTO l_exist;
3260                 IF(can_del_return_line_csr%NOTFOUND) THEN
3261                    -- No other active OSP Line for this Return shipment
3262                    -- Check if the shipment has already occured
3263                    OPEN line_already_shipped_csr(p_del_cancel_so_lines_tbl(i).oe_return_line_id);
3264                    FETCH line_already_shipped_csr INTO l_exist;
3265                    IF (line_already_shipped_csr%NOTFOUND) THEN
3266                      -- Line not yet shipped: Include for deletion
3267                      IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3268                        FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Return Shipment line with id ' || p_del_cancel_so_lines_tbl(i).oe_return_line_id || ' included for deletion/cancellation.');
3269                      END IF;
3270                      l_Ship_ID_Tbl(l_index) := p_del_cancel_so_lines_tbl(i).oe_return_line_id;
3271                      l_index := l_index + 1;
3272                    END IF;
3273                    CLOSE line_already_shipped_csr;
3274                 END IF;
3275                 CLOSE can_del_return_line_csr;
3276              END IF;
3277           END IF;
3278       END LOOP;
3279       -- CALL SO API with consolidated list of line IDs
3280       IF(l_Ship_ID_Tbl.COUNT > 0) THEN
3281          -- CALL SO to delete/cancel SO
3282          IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3283            FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Calling AHL_OSP_SHIPMENT_PUB.Delete_Cancel_Order with l_Ship_ID_Tbl.COUNT = ' || l_Ship_ID_Tbl.COUNT);
3284          END IF;
3285          AHL_OSP_SHIPMENT_PUB.Delete_Cancel_Order (
3286           p_api_version              => 1.0,
3287           p_oe_header_id             => NULL,
3288           p_oe_lines_tbl             => l_Ship_ID_Tbl,
3289           p_cancel_flag              => p_cancel_flag,
3290           x_return_status            => l_return_status ,
3291           x_msg_count                => l_msg_count ,
3292           x_msg_data                 => l_msg_data
3293          );
3294        IF(l_return_status <> FND_API.G_RET_STS_SUCCESS)THEN
3295           FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_SO_LN_CAN_FLD');
3296           FND_MSG_PUB.ADD;
3297        END IF;
3298       END IF;
3299    END IF;
3300    IF FND_MSG_PUB.count_msg > 0 THEN
3301       RAISE  FND_API.G_EXC_ERROR;
3302       --  RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3303     END IF;
3304     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3305       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'End Procedure');
3306     END IF;
3307 END delete_cancel_so;
3308 
3309 -----------------------------------------------------------------------------------------------------
3310 FUNCTION can_convert_order(p_osp_order_id IN NUMBER,
3311                            p_old_type_code IN VARCHAR2,
3312                            p_new_type_code IN VARCHAR2
3313                             --p_new_order_rec  IN AHL_OSP_ORDERS_PVT.osp_order_rec_type,
3314                            --p_old_order_rec  IN AHL_OSP_ORDERS_PVT.osp_order_rec_type
3315                           ) RETURN BOOLEAN  IS
3316   CURSOR order_has_ship_return_csr(p_osp_order_id IN NUMBER) IS
3317       SELECT 'x' FROM ahl_osp_order_lines ol       --, oe_order_lines_all oel
3318           WHERE ol.osp_order_id = p_osp_order_id
3319            AND ol.oe_return_line_id IS NOT NULL;   --= oel.line_id;
3320   CURSOR order_header_status_csr(p_osp_order_id IN NUMBER) IS
3321      SELECT status_code FROM ahl_osp_orders_b
3322         WHERE osp_order_id = p_osp_order_id;
3323   l_exist VARCHAR(1);
3324   l_status_code VARCHAR(30);
3325   L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.CAN_CONVERT_ORDER';
3326 BEGIN
3327 IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3328       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Function');
3329 END IF;
3330 IF(p_old_type_code  NOT IN (G_OSP_ORDER_TYPE_SERVICE, G_OSP_ORDER_TYPE_EXCHANGE)) THEN
3331    FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_CONV_FR_TYPE');
3332      FND_MSG_PUB.ADD;
3333    IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3334       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY ||'.error',  'Cannot Convert from Order Type is not Service or Exchange');
3335    END IF;
3336     return false;
3337 END IF;
3338 IF(p_new_type_code NOT IN (G_OSP_ORDER_TYPE_SERVICE, G_OSP_ORDER_TYPE_EXCHANGE)) THEN
3339    FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_CONV_TO_TYPE');
3340      FND_MSG_PUB.ADD;
3341    IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3342       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY ||'.error', 'Cannot Convert to an order that is not of  type Service or Exchange');
3343    END IF;
3344     return false;
3345 END IF;
3346 -- check if the order is closed
3347 OPEN order_header_status_csr(p_osp_order_id);
3348 FETCH order_header_status_csr INTO l_status_code;
3349 IF(l_status_code = G_OSP_CLOSED_STATUS) THEN
3350     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_CONV_CLOSED');
3351     FND_MSG_PUB.ADD;
3352     CLOSE order_header_status_csr;
3353     return false;
3354 END IF;
3355 CLOSE order_header_status_csr;
3356 --mpothuku commented on 10-Feb-2007 to implment the Osp Receiving ER. Conversion is enhanced to cancel the return lines in the back ground.
3357 --We do not need this check any more.
3358 /*
3359 OPEN order_has_ship_return_csr(p_osp_order_id);
3360 FETCH  order_has_ship_return_csr INTO l_exist;
3361 IF( order_has_ship_return_csr %FOUND) THEN
3362   FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INV_CONV_HAS_RET_LINE');
3363     FND_MSG_PUB.ADD;
3364   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3365       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY ||'.error', 'can_convert:  Cannot Convert. Order has shipment return line');
3366   END IF;
3367   CLOSE order_has_ship_return_csr;
3368    return false;
3369 END IF;
3370 CLOSE order_has_ship_return_csr;
3371 */
3372 --mpothuku End
3373   RETURN true;
3374 END can_convert_order;
3375 
3376 ------------------------------------------------------------------------------------------------------
3377 FUNCTION vendor_id_exist_in_PO( p_po_header_id   IN NUMBER,
3378                                 p_vendor_id      IN NUMBER
3379  ) RETURN BOOLEAN IS
3380  CURSOR vendor_id_csr(p_po_header_id IN NUMBER, p_vendor_id IN NUMBER) IS
3381     SELECT 'x' FROM po_headers_all
3382       WHERE po_header_id = p_po_header_id
3383         AND vendor_id = p_vendor_id;
3384  l_exist VARCHAR(1);
3385  L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.vendor_id_exist_in_PO';
3386 BEGIN
3387 IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3388       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Function - '
3389                      || 'p_po_header_id: ' || p_po_header_id || '  p_vendor_id: ' || p_vendor_id);
3390 END IF;
3391 IF(p_po_header_id IS NOT NULL AND p_po_header_id <> FND_API.G_MISS_NUM
3392    AND p_vendor_id IS NOT NULL AND p_vendor_id <> FND_API.G_MISS_NUM) THEN
3393    OPEN vendor_id_csr(p_po_header_id, p_vendor_id);
3394    FETCH vendor_id_csr INTO l_exist;
3395    IF(vendor_id_csr %FOUND) THEN
3396       CLOSE vendor_id_csr;
3397       RETURN true;
3398    END IF;
3399    CLOSE vendor_id_csr;
3400 END IF;
3401 RETURN false;
3402 END vendor_id_exist_in_PO;
3403 
3404 --------------------------------------------------------------------------------------------------------------
3405 FUNCTION vendor_site_id_exist_in_PO( p_po_header_id   IN NUMBER,
3406                                 p_vendor_site_id      IN NUMBER
3407  ) RETURN BOOLEAN IS
3408  CURSOR vendor_site_id_csr(p_po_header_id IN NUMBER, p_vendor_site_id IN NUMBER) IS
3409     SELECT 'x' FROM po_headers_all
3410       WHERE po_header_id = p_po_header_id
3411         AND vendor_site_id = p_vendor_site_id;
3412  l_exist VARCHAR(1);
3413  L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.vendor_site_id_exist_in_PO';
3414 BEGIN
3415 IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3416     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Function - '
3417                    || 'p_po_header_id: ' || p_po_header_id || '  p_vendor_site_id: ' || p_vendor_site_id);
3418 END IF;
3419 IF(p_po_header_id IS NOT NULL AND p_po_header_id <> FND_API.G_MISS_NUM
3420    AND p_vendor_site_id IS NOT NULL AND p_vendor_site_id <> FND_API.G_MISS_NUM) THEN
3421    OPEN vendor_site_id_csr(p_po_header_id, p_vendor_site_id);
3422    FETCH vendor_site_id_csr INTO l_exist;
3423    IF(vendor_site_id_csr %FOUND) THEN
3424       CLOSE vendor_site_id_csr;
3425       RETURN true;
3426    END IF;
3427    CLOSE vendor_site_id_csr;
3428 END IF;
3429 RETURN false;
3430 END vendor_site_id_exist_in_PO;
3431 
3432 ---------------------------------------------------------------------------
3433 --This is the main API for processing Inventory Service Orders including work order based.
3434 --It handles OSP order header Creation, Update, and Deletion, order lines Creation, Update
3435 --and Deletion.
3436 PROCEDURE process_osp_order(
3437   p_api_version           IN              NUMBER    := 1.0,
3438   p_init_msg_list         IN              VARCHAR2  := FND_API.G_TRUE,
3439   p_commit                IN              VARCHAR2  := FND_API.G_FALSE,
3440   p_validation_level      IN              NUMBER    := FND_API.G_VALID_LEVEL_FULL,
3441   p_module_type           IN              VARCHAR2  := NULL,
3442   p_x_osp_order_rec       IN OUT NOCOPY   osp_order_rec_type,
3443   p_x_osp_order_lines_tbl IN OUT NOCOPY   osp_order_lines_tbl_type,
3444   x_return_status         OUT NOCOPY      VARCHAR2,
3445   x_msg_count             OUT NOCOPY      NUMBER,
3446   x_msg_data              OUT NOCOPY      VARCHAR2)
3447 IS
3448   l_api_name       CONSTANT   VARCHAR2(30)   := 'process_osp_order';
3449   l_api_version    CONSTANT   NUMBER         := 1.0;
3450   l_return_status             VARCHAR2(1);
3451   l_msg_count                 NUMBER;
3452   l_msg_data                  VARCHAR2(2000);
3453   l_del_cancel_so_lines_tbl   del_cancel_so_lines_tbl_type;
3454   l_dummy_dc_so_lines_tbl     del_cancel_so_lines_tbl_type;
3455   l_osp_order_id              NUMBER;
3456   l_oe_ship_line_id           NUMBER;
3457   l_oe_return_line_id         NUMBER;
3458   l_oe_header_id              NUMBER;
3459   l_status_code               VARCHAR2(30);
3460   l_vendor_validate_flag      BOOLEAN := FALSE;
3461   l_validate_pass_flag        BOOLEAN := FALSE;
3462   l_first_index               NUMBER;
3463   l_valid_vendors_tbl         vendor_id_tbl_type;
3464   l_any_vendor_flag           VARCHAR2(1);
3465   l_header_vendor_id          NUMBER;
3466   l_header_site_id            NUMBER;
3467   i                           NUMBER;
3468   --salogan added for supplier warranty starts
3469   l_call_process_entl         VARCHAR2(1);
3470   l_entl_rec_tbl              AHL_WARRANTY_ENTL_PVT.Warranty_Entl_Tbl_Type;
3471   --salogan added for supplier warranty ends
3472 
3473   CURSOR get_order_lines(c_osp_order_id NUMBER) IS
3474     SELECT osp_order_line_id
3475       FROM ahl_osp_order_lines
3476      WHERE osp_order_id = c_osp_order_id;
3477 BEGIN
3478   --Initialize API return status to success
3479   x_return_status := FND_API.G_RET_STS_SUCCESS;
3480 
3481   --Standard Start of API savepoint
3482   SAVEPOINT process_osp_order;
3483 
3484   --Standard call to check for call compatibility.
3485   IF NOT FND_API.compatible_api_call(
3486     l_api_version,
3487     p_api_version,
3488     l_api_name,
3489     G_PKG_NAME)
3490   THEN
3491     RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3492   END IF;
3493 
3494   --Initialize message list if p_init_msg_list is set to TRUE.
3495   IF FND_API.to_boolean(p_init_msg_list) THEN
3496     FND_MSG_PUB.initialize;
3497   END IF;
3498 
3499   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3500     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
3501       G_LOG_PREFIX||l_api_name, --module
3502       'The main API Begin: and header operation_flag='||p_x_osp_order_rec.operation_flag); --message_text
3503   END IF;
3504 
3505   --Validate the operation_flag of the header record, and it could be NULL which
3506   --means no change to the header record
3507   IF (p_x_osp_order_rec.operation_flag IS NOT NULL AND
3508       p_x_osp_order_rec.operation_flag NOT IN (G_OP_CREATE, G_OP_UPDATE, G_OP_DELETE)) THEN
3509     FND_MESSAGE.set_name('AHL', 'AHL_OSP_ORD_INVOP');
3510     FND_MSG_PUB.add;
3511     RAISE FND_API.G_EXC_ERROR;
3512   END IF;
3513   --Validate the operation_flag of the line records and it couldn't be NULL
3514   --In case only header record is to be processed, then NULL is supposed to be passed
3515   --to the lines table
3516   IF (p_x_osp_order_lines_tbl.COUNT > 0) THEN
3517     FOR i IN p_x_osp_order_lines_tbl.FIRST..p_x_osp_order_lines_tbl.LAST LOOP
3518       IF (p_x_osp_order_lines_tbl(i).operation_flag NOT IN (G_OP_CREATE, G_OP_UPDATE, G_OP_DELETE)) THEN
3519         FND_MESSAGE.set_name('AHL', 'AHL_OSP_ORD_INVOP');
3520         FND_MSG_PUB.add;
3521         RAISE FND_API.G_EXC_ERROR;
3522       END IF;
3523     END LOOP;
3524   END IF;
3525 
3526   --Set the global variable g_module_type
3527   g_module_type := p_module_type;
3528 
3529   /* Customer pre-processing section, Mandatory */
3530 
3531   IF (JTF_USR_HKS.Ok_to_execute('AHL_OSP_ORDERS_PVT', 'PROCESS_OSP_ORDER', 'B', 'C' )) then
3532     ahl_osp_orders_CUHK.process_osp_order_pre(
3533       p_osp_order_rec => p_x_osp_order_rec,
3534       p_osp_order_lines_tbl => p_x_osp_order_lines_tbl,
3535       x_msg_count => l_msg_count,
3536       x_msg_data => l_msg_data,
3537       x_return_status => l_return_status);
3538     IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
3539       RAISE FND_API.G_EXC_ERROR;
3540     ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
3541       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3542     END IF;
3543   END IF;
3544 
3545   IF (p_x_osp_order_rec.operation_flag IS NULL) THEN
3546     IF (FND_LOG.LEVEL_EVENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3547       FND_LOG.STRING(FND_LOG.LEVEL_EVENT,
3548         G_LOG_PREFIX||l_api_name, --module
3549         'Within API: No change to OSP header, just for lines');
3550     END IF;
3551     --No change to the header record and only to the line records
3552     IF (p_x_osp_order_lines_tbl.COUNT > 0) THEN
3553       FOR i IN p_x_osp_order_lines_tbl.FIRST..p_x_osp_order_lines_tbl.LAST LOOP
3554         IF (p_x_osp_order_lines_tbl(i).operation_flag = G_OP_DELETE) THEN
3555         --Front end just passes osp_order_line_id and operation_flag
3556           BEGIN
3557             SELECT osp_order_id, oe_ship_line_id, oe_return_line_id
3558               INTO l_osp_order_id, l_oe_ship_line_id, l_oe_return_line_id
3559               FROM ahl_osp_order_lines
3560              WHERE osp_order_line_id = p_x_osp_order_lines_tbl(i).osp_order_line_id;
3561 
3562              SELECT status_code, oe_header_id INTO l_status_code, l_oe_header_id
3563               FROM ahl_osp_orders_b
3564              WHERE osp_order_id = l_osp_order_id;
3565           EXCEPTION
3566             WHEN NO_DATA_FOUND THEN
3567               FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_ID_LN_INV');
3568               FND_MESSAGE.Set_Token('OSP_LINE_ID',p_x_osp_order_lines_tbl(i).osp_order_line_id);
3569               FND_MSG_PUB.ADD;
3570               RAISE FND_API.G_EXC_ERROR;
3571           END;
3572           -- Added by jaramana on January 9, 2008 for the Requisition ER 6034236
3573           IF (l_status_code IN (G_OSP_ENTERED_STATUS, G_OSP_SUB_FAILED_STATUS, G_OSP_REQ_SUB_FAILED_STATUS)) THEN
3574           -- jaramana End
3575             IF (l_oe_ship_line_id IS NOT NULL OR l_oe_return_line_id IS NOT NULL) THEN
3576               l_del_cancel_so_lines_tbl(1).osp_order_id := l_osp_order_id;
3577               l_del_cancel_so_lines_tbl(1).oe_ship_line_id := l_oe_ship_line_id;
3578               l_del_cancel_so_lines_tbl(1).oe_return_line_id := l_oe_return_line_id;
3579             END IF;
3580 
3581             -- salogan added the following for supplier warranty starts
3582             -- Delete the entitlement record for the OSP order/line before deleting the OSP record
3583             Get_Entl_rec_for_Delete(
3584                 p_osp_order_line_id => p_x_osp_order_lines_tbl(i).osp_order_line_id,
3585                 x_warranty_entl_tbl => l_entl_rec_tbl);
3586             --Not passing p_commit the main API commits the data at the end
3587             AHL_WARRANTY_ENTL_PVT.Process_Warranty_Entitlements(
3588                 p_api_version                 => p_api_version,
3589                 p_validation_level            => p_validation_level,
3590                 p_module_type                 => p_module_type,
3591                 p_user_role                   => AHL_WARRANTY_ENTL_PVT.G_USER_PLANNER,
3592                 p_x_warranty_entl_tbl         => l_entl_rec_tbl,
3593                 x_return_status               => l_return_status,
3594                 x_msg_count                   => l_msg_count,
3595                 x_msg_data                    => l_msg_data);
3596 
3597             IF(FND_LOG.LEVEL_EVENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3598                FND_LOG.STRING(FND_LOG.LEVEL_EVENT,
3599                G_LOG_PREFIX||l_api_name,
3600                'After calling AHL_WARRANTY_ENTL_PVT.Process_Warranty_Entitlements. Return Status = ' ||
3601                l_return_status);
3602             END IF;
3603 
3604             l_msg_count := Fnd_Msg_Pub.count_msg;
3605             IF l_msg_count > 0 OR l_return_status <> Fnd_Api.g_ret_sts_success THEN
3606               x_msg_count := l_msg_count;
3607               x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
3608               RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
3609             END IF;
3610             -- salogan added the following for supplier warranty ends
3611 
3612             --Delete OSP line record first (it makes more sense to delete shipment lines first, but
3613             --here just keep it the same as the old logic).
3614             AHL_OSP_ORDER_LINES_PKG.delete_row(p_x_osp_order_lines_tbl(i).osp_order_line_id);
3615 
3616             --Delete or cancel shipment lines
3617             delete_cancel_so(
3618                 p_oe_header_id            => NULL,
3619                 p_del_cancel_so_lines_tbl => l_del_cancel_so_lines_tbl);
3620           ELSE
3621             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INVOP');
3622             FND_MSG_PUB.ADD;
3623             RAISE FND_API.G_EXC_ERROR;
3624           END IF;
3625         ELSIF (p_x_osp_order_lines_tbl(i).operation_flag = G_OP_CREATE) THEN
3626           --Create(Add) new line record
3627           create_osp_order_line(p_x_osp_order_lines_tbl(i));
3628           --Create shipment at the same time if the flag is checked
3629           --Removed to the out loop for performance gain
3630           --IF (p_x_osp_order_lines_tbl(i).shipment_creation_flag = 'Y') THEN
3631             --create_shipment(p_x_osp_order_lines_tbl(i));
3632           --END IF;
3633 
3634           -- salogan added for supplier warranty starts
3635           -- calling Get_Warranty_Rec_for_Create
3636           Get_Warranty_Rec_for_Create(
3637               p_x_osp_order_line_rec => p_x_osp_order_lines_tbl(i),
3638               x_warranty_entl_tbl    => l_entl_rec_tbl,
3639               x_call_process_entl    => l_call_process_entl);
3640 
3641           IF(FND_LOG.LEVEL_EVENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3642              FND_LOG.STRING(FND_LOG.LEVEL_EVENT,
3643              G_LOG_PREFIX||l_api_name,
3644              'Before calling AHL_WARRANTY_ENTL_PVT.Process_Warranty_Entitlements. Need to call ? ' || l_call_process_entl);
3645           END IF;
3646 
3647           IF(l_call_process_entl = 'Y') THEN
3648              --Not passing p_commit the main API commits the data at the end
3649              AHL_WARRANTY_ENTL_PVT.Process_Warranty_Entitlements(
3650                  p_api_version                 => p_api_version,
3651                  p_validation_level            => p_validation_level,
3652                  p_module_type                 => p_module_type,
3653                  p_user_role                   => AHL_WARRANTY_ENTL_PVT.G_USER_PLANNER,
3654                  p_x_warranty_entl_tbl         => l_entl_rec_tbl,
3655                  x_return_status               => l_return_status,
3656                  x_msg_count                   => l_msg_count,
3657                  x_msg_data                    => l_msg_data);
3658 
3659              IF(FND_LOG.LEVEL_EVENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3660                 FND_LOG.STRING(FND_LOG.LEVEL_EVENT,
3661                 G_LOG_PREFIX||l_api_name,
3662                 'After calling AHL_WARRANTY_ENTL_PVT.Process_Warranty_Entitlements. Return Status = ' ||
3663                 l_return_status);
3664              END IF;
3665 
3666               l_msg_count := Fnd_Msg_Pub.count_msg;
3667               IF l_msg_count > 0 OR l_return_status <> Fnd_Api.g_ret_sts_success THEN
3668                  x_msg_count := l_msg_count;
3669                  x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
3670                  RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
3671               END IF;
3672           END IF;
3673           -- salogan added for supplier warranty ends
3674 
3675         ELSIF (p_x_osp_order_lines_tbl(i).operation_flag = G_OP_UPDATE) THEN
3676           --Update line record
3677           update_osp_order_line(p_x_osp_order_lines_tbl(i));
3678           -- salogan added for supplier warranty starts
3679           -- calling Get_Warranty_Rec_for_Update
3680           Get_Warranty_Rec_for_Update(
3681               p_x_osp_order_line_rec => p_x_osp_order_lines_tbl(i),
3682               x_warranty_entl_tbl    => l_entl_rec_tbl,
3683               x_call_process_entl    => l_call_process_entl);
3684 
3685           IF(FND_LOG.LEVEL_EVENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3686              FND_LOG.STRING(FND_LOG.LEVEL_EVENT,
3687              G_LOG_PREFIX||l_api_name,
3688              'Before calling AHL_WARRANTY_ENTL_PVT.Process_Warranty_Entitlements. Need to call ? ' || l_call_process_entl);
3689           END IF;
3690 
3691           IF(l_call_process_entl = 'Y') THEN
3692              --Not passing p_commit the main API commits the data at the end
3693              AHL_WARRANTY_ENTL_PVT.Process_Warranty_Entitlements(
3694                  p_api_version                 => p_api_version,
3695                  p_validation_level            => p_validation_level,
3696                  p_module_type                 => p_module_type,
3697                  p_user_role                   => AHL_WARRANTY_ENTL_PVT.G_USER_NONPLANNER,
3698                  p_x_warranty_entl_tbl         => l_entl_rec_tbl,
3699                  x_return_status               => l_return_status,
3700                  x_msg_count                   => l_msg_count,
3701                  x_msg_data                    => l_msg_data);
3702 
3703              IF(FND_LOG.LEVEL_EVENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3704                 FND_LOG.STRING(FND_LOG.LEVEL_EVENT,
3705                 G_LOG_PREFIX||l_api_name,
3706                 'After calling AHL_WARRANTY_ENTL_PVT.Process_Warranty_Entitlements. Return Status = ' ||
3707                 l_return_status);
3708              END IF;
3709 
3710              l_msg_count := Fnd_Msg_Pub.count_msg;
3711              IF l_msg_count > 0 OR l_return_status <> Fnd_Api.g_ret_sts_success THEN
3712                 x_msg_count := l_msg_count;
3713                 x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
3714                 RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
3715              END IF;
3716           END IF;
3717           -- salogan added for supplier warranty ends
3718         END IF;
3719       END LOOP;
3720 
3721       --To check whether it is necessary to do a post vendor/items combination validation
3722       --after the records were inserted into or updated from the database
3723       FOR i IN p_x_osp_order_lines_tbl.FIRST..p_x_osp_order_lines_tbl.LAST LOOP
3724         -- Modified by jaramana on January 9, 2008 to not do this Vendor check if the line is PO Cancelled or PO Deleted
3725         IF p_x_osp_order_lines_tbl(i).operation_flag <> G_OP_DELETE AND
3726            (NVL(p_x_osp_order_lines_tbl(i).status_code, 'ENTERED') NOT IN (G_OL_PO_CANCELLED_STATUS, G_OL_PO_DELETED_STATUS)) THEN
3727           l_vendor_validate_flag := TRUE;
3728           EXIT;
3729         END IF;
3730       END LOOP;
3731       IF l_vendor_validate_flag THEN
3732       --Needs to do the post vendor/items combination validation
3733         l_first_index := p_x_osp_order_lines_tbl.FIRST;
3734         derive_common_vendors(p_x_osp_order_lines_tbl(l_first_index).osp_order_id,
3735                                l_valid_vendors_tbl,
3736                                l_any_vendor_flag);
3737         IF l_any_vendor_flag <> 'Y' THEN
3738           BEGIN
3739             SELECT vendor_id, vendor_site_id INTO l_header_vendor_id, l_header_site_id
3740               FROM ahl_osp_orders_b
3741              WHERE osp_order_id = p_x_osp_order_lines_tbl(l_first_index).osp_order_id;
3742           EXCEPTION
3743             WHEN NO_DATA_FOUND THEN
3744               FND_MESSAGE.set_name('AHL', 'AHL_OSP_ORD_INVALID');
3745               FND_MSG_PUB.add;
3746               RAISE FND_API.G_EXC_ERROR;
3747           END;
3748 
3749           IF l_valid_vendors_tbl.count > 0 THEN
3750             IF (l_header_vendor_id IS NULL OR l_header_site_id IS NULL) THEN
3751               l_validate_pass_flag := TRUE;
3752             ELSE
3753               FOR i IN l_valid_vendors_tbl.FIRST..l_valid_vendors_tbl.LAST LOOP
3754                 IF (l_header_vendor_id = l_valid_vendors_tbl(i).vendor_id AND
3755                     l_header_site_id = l_valid_vendors_tbl(i).vendor_site_id) THEN
3756                   l_validate_pass_flag := TRUE;
3757                   EXIT;
3758                 END IF;
3759               END LOOP;
3760             END IF;
3761             IF NOT l_validate_pass_flag THEN
3762               FND_MESSAGE.set_name('AHL', 'AHL_OSP_ITEM_VENDOR_MISMATCH');
3763               FND_MSG_PUB.add;
3764               RAISE FND_API.G_EXC_ERROR;
3765             END IF;
3766           ELSE
3767             FND_MESSAGE.set_name('AHL', 'AHL_OSP_ITEM_VENDOR_MISMATCH');
3768             FND_MSG_PUB.add;
3769             RAISE FND_API.G_EXC_ERROR;
3770           END IF;
3771         END IF;
3772       END IF;
3773       --Call create_shipment. Create_shipment will check whether it will create
3774       --shipment or not(if the operation is not Create and no shipment_creation_flag
3775       --is checked, it will do nothing)
3776       create_shipment(p_x_osp_order_lines_tbl);
3777     END IF;
3778   ELSIF (p_x_osp_order_rec.operation_flag = G_OP_DELETE) THEN
3779     IF (FND_LOG.LEVEL_EVENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3780       FND_LOG.STRING(FND_LOG.LEVEL_EVENT,
3781         G_LOG_PREFIX||l_api_name, --module
3782         'Within API: Delete OSP Header/Lines');
3783     END IF;
3784     --Front end just passes the osp_order_id
3785     BEGIN
3786       SELECT status_code, oe_header_id INTO l_status_code, l_oe_header_id
3787         FROM ahl_osp_orders_b
3788        WHERE osp_order_id = p_x_osp_order_rec.osp_order_id;
3789     EXCEPTION
3790       WHEN NO_DATA_FOUND THEN
3791         FND_MESSAGE.set_name('AHL', 'AHL_OSP_ORD_INVALID');
3792         FND_MSG_PUB.add;
3793         RAISE FND_API.G_EXC_ERROR;
3794     END;
3795     --check the osp order could be deleted, if Yes then
3796     --Delete OSP order Header/Line records
3797     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3798       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
3799         G_LOG_PREFIX||l_api_name,
3800         'Before deletion and status='||l_status_code||' oe_header='||l_oe_header_id);
3801     END IF;
3802     -- Added by jaramana on January 9, 2008 for the Requisition ER 6034236
3803     IF(l_status_code IN (G_OSP_ENTERED_STATUS, G_OSP_SUB_FAILED_STATUS, G_OSP_REQ_SUB_FAILED_STATUS)) THEN
3804     -- jaramana End
3805       IF (l_oe_header_id IS NOT NULL) THEN
3806         -- calling to delete SO HEADER
3807         -- Here needs to populate the l_del_cancel_so_lines_tbl
3808         i := 1;
3809         FOR l_get_order_lines IN get_order_lines(p_x_osp_order_rec.osp_order_id) LOOP
3810           BEGIN
3811           SELECT oe_ship_line_id, oe_return_line_id
3812             INTO l_oe_ship_line_id, l_oe_return_line_id
3813             FROM ahl_osp_order_lines
3814            WHERE osp_order_line_id = l_get_order_lines.osp_order_line_id;
3815           EXCEPTION
3816             WHEN NO_DATA_FOUND THEN
3817               FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_ID_LN_INV');
3818               FND_MESSAGE.Set_Token('OSP_LINE_ID',l_get_order_lines.osp_order_line_id);
3819               FND_MSG_PUB.ADD;
3820               RAISE FND_API.G_EXC_ERROR;
3821           END;
3822           IF (l_oe_ship_line_id IS NOT NULL OR l_oe_return_line_id IS NOT NULL) THEN
3823             l_del_cancel_so_lines_tbl(i).osp_order_id := p_x_osp_order_rec.osp_order_id;
3824             l_del_cancel_so_lines_tbl(i).oe_ship_line_id := l_oe_ship_line_id;
3825             l_del_cancel_so_lines_tbl(i).oe_return_line_id := l_oe_return_line_id;
3826             i := i+1;
3827           END IF;
3828         END LOOP;
3829         IF (FND_LOG.LEVEL_EVENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3830           FND_LOG.STRING(FND_LOG.LEVEL_EVENT,
3831             G_LOG_PREFIX||l_api_name,
3832             'Before calling: delete_cancel_so and table_count='||to_char(i-1));
3833         END IF;
3834         --Delete Shipment header/lines
3835         delete_cancel_so(
3836           p_oe_header_id            => l_oe_header_id,
3837           p_del_cancel_so_lines_tbl => l_dummy_dc_so_lines_tbl);
3838       END IF;
3839 
3840       -- salogan added the following for supplier warranty starts
3841       -- Delete the entitlement record for the OSP order/line before deleting the OSP record
3842       Get_Entl_rec_for_Delete(
3843         p_osp_order_id => p_x_osp_order_rec.osp_order_id,
3844         x_warranty_entl_tbl => l_entl_rec_tbl);
3845 
3846       --Not passing p_commit the main API commits the data at the end
3847       AHL_WARRANTY_ENTL_PVT.Process_Warranty_Entitlements(
3848         p_api_version                 => p_api_version,
3849         p_validation_level            => p_validation_level,
3850         p_module_type                 => p_module_type,
3851         p_user_role                   => AHL_WARRANTY_ENTL_PVT.G_USER_PLANNER,
3852         p_x_warranty_entl_tbl         => l_entl_rec_tbl,
3853         x_return_status               => l_return_status,
3854         x_msg_count                   => l_msg_count,
3855         x_msg_data                    => l_msg_data);
3856 
3857       IF(FND_LOG.LEVEL_EVENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3858          FND_LOG.STRING(FND_LOG.LEVEL_EVENT,
3859          G_LOG_PREFIX||l_api_name,
3860          'After calling AHL_WARRANTY_ENTL_PVT.Process_Warranty_Entitlements. Return Status = ' ||
3861          l_return_status);
3862       END IF;
3863 
3864       l_msg_count := Fnd_Msg_Pub.count_msg;
3865       IF l_msg_count > 0 OR l_return_status <> Fnd_Api.g_ret_sts_success THEN
3866          x_msg_count := l_msg_count;
3867          x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
3868          RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
3869       END IF;
3870       -- salogan added the following for supplier warranty ends
3871 
3872       --Delete OSP order lines
3873       FOR l_get_order_lines IN get_order_lines(p_x_osp_order_rec.osp_order_id) LOOP
3874         AHL_OSP_ORDER_LINES_PKG.delete_row(l_get_order_lines.osp_order_line_id);
3875       END LOOP;
3876 
3877       --Delete OSP order header record
3878       AHL_OSP_ORDERS_PKG.delete_row(x_osp_order_id => p_x_osp_order_rec.osp_order_id);
3879 
3880       IF (FND_LOG.LEVEL_EVENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3881         FND_LOG.STRING(FND_LOG.LEVEL_EVENT,
3882           G_LOG_PREFIX||l_api_name,
3883           'After deleting header.');
3884       END IF;
3885     ELSE
3886       FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INVOP');
3887       FND_MSG_PUB.ADD;
3888       RAISE FND_API.G_EXC_ERROR;
3889     END IF;
3890   ELSIF (p_x_osp_order_rec.operation_flag = G_OP_UPDATE) THEN
3891     IF (FND_LOG.LEVEL_EVENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3892       FND_LOG.STRING(FND_LOG.LEVEL_EVENT,
3893         G_LOG_PREFIX||l_api_name, --module
3894         'Within API: Update OSP Header/Lines');
3895     END IF;
3896     -- Validate and update header record
3897     -- Handling if order status is to be changed
3898     --process_order_status_change(p_x_osp_order_rec);
3899     -- Handling if order type is to be changed
3900     --process_order_type_change(p_x_osp_order_rec);
3901     --Just update the Header record first
3902     update_osp_order_header(p_x_osp_order_rec);
3903     IF (p_x_osp_order_lines_tbl IS NOT NULL AND p_x_osp_order_lines_tbl.COUNT > 0) THEN
3904       FOR i IN p_x_osp_order_lines_tbl.FIRST..p_x_osp_order_lines_tbl.LAST LOOP
3905         IF (p_x_osp_order_lines_tbl(i).operation_flag = G_OP_DELETE) THEN
3906         --Front end just passes osp_order_line_id and operation_flag
3907           BEGIN
3908             SELECT osp_order_id, oe_ship_line_id, oe_return_line_id
3909               INTO l_osp_order_id, l_oe_ship_line_id, l_oe_return_line_id
3910               FROM ahl_osp_order_lines
3911              WHERE osp_order_line_id = p_x_osp_order_lines_tbl(i).osp_order_line_id;
3912 
3913              SELECT status_code, oe_header_id INTO l_status_code, l_oe_header_id
3914               FROM ahl_osp_orders_b
3915              WHERE osp_order_id = l_osp_order_id;
3916           EXCEPTION
3917             WHEN NO_DATA_FOUND THEN
3918               FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_ID_LN_INV');
3919               FND_MESSAGE.Set_Token('OSP_LINE_ID',p_x_osp_order_lines_tbl(i).osp_order_line_id);
3920               FND_MSG_PUB.ADD;
3921               RAISE FND_API.G_EXC_ERROR;
3922           END;
3923           -- Added by jaramana on January 9, 2008 for the Requisition ER 6034236
3924           IF (l_status_code IN (G_OSP_ENTERED_STATUS, G_OSP_SUB_FAILED_STATUS, G_OSP_REQ_SUB_FAILED_STATUS)) THEN
3925           -- jaramana End
3926             IF (p_x_osp_order_lines_tbl(i).oe_ship_line_id IS NOT NULL OR
3927                 p_x_osp_order_lines_tbl(i).oe_return_line_id IS NOT NULL) THEN
3928               l_del_cancel_so_lines_tbl(1).osp_order_id := l_osp_order_id;
3929               l_del_cancel_so_lines_tbl(1).oe_ship_line_id := l_oe_ship_line_id;
3930               l_del_cancel_so_lines_tbl(1).oe_return_line_id := l_oe_return_line_id;
3931             END IF;
3932 
3933             -- salogan added the following for supplier warranty starts
3934             -- Delete the entitlement record for the OSP order line before deleting the OSP record
3935             Get_Entl_rec_for_Delete(
3936                 p_osp_order_line_id => p_x_osp_order_lines_tbl(i).osp_order_line_id,
3937                 x_warranty_entl_tbl => l_entl_rec_tbl);
3938 
3939             --Not passing p_commit the main API commits the data at the end
3940             AHL_WARRANTY_ENTL_PVT.Process_Warranty_Entitlements(
3941                 p_api_version                 => p_api_version,
3942                 p_validation_level            => p_validation_level,
3943                 p_module_type                 => p_module_type,
3944                 p_user_role                   => AHL_WARRANTY_ENTL_PVT.G_USER_PLANNER,
3945                 p_x_warranty_entl_tbl         => l_entl_rec_tbl,
3946                 x_return_status               => l_return_status,
3947                 x_msg_count                   => l_msg_count,
3948                 x_msg_data                    => l_msg_data);
3949 
3950             IF(FND_LOG.LEVEL_EVENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3951                FND_LOG.STRING(FND_LOG.LEVEL_EVENT,
3952                G_LOG_PREFIX||l_api_name,
3953                'After calling AHL_WARRANTY_ENTL_PVT.Process_Warranty_Entitlements. Return Status = ' ||
3954                l_return_status);
3955             END IF;
3956 
3957             l_msg_count := Fnd_Msg_Pub.count_msg;
3958             IF l_msg_count > 0 OR l_return_status <> Fnd_Api.g_ret_sts_success THEN
3959               x_msg_count := l_msg_count;
3960               x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
3961               RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
3962             END IF;
3963             -- salogan added the following for supplier warranty ends
3964 
3965             --Delete line record first (it makes more sense to delete shipment lines first, but
3966             --here just keep it the same as the old logic.
3967             AHL_OSP_ORDER_LINES_PKG.delete_row(p_x_osp_order_lines_tbl(i).osp_order_line_id);
3968 
3969             --Delete or cancel Shipment lines
3970             delete_cancel_so(
3971                 p_oe_header_id            => NULL,
3972                 p_del_cancel_so_lines_tbl => l_del_cancel_so_lines_tbl);
3973           ELSE
3974             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INVOP');
3975             FND_MSG_PUB.ADD;
3976             RAISE FND_API.G_EXC_ERROR;
3977           END IF;
3978           --AHL_OSP_ORDER_LINES_PKG.delete_row(p_x_osp_order_lines_tbl(i).osp_order_line_id);
3979         ELSIF (p_x_osp_order_lines_tbl(i).operation_flag = G_OP_UPDATE) THEN
3980           --update osp order line
3981           update_osp_order_line(p_x_osp_order_lines_tbl(i));
3982           -- salogan added for supplier warranty starts
3983           -- calling Get_Warranty_Rec_for_Update
3984           Get_Warranty_Rec_for_Update(
3985               p_x_osp_order_line_rec => p_x_osp_order_lines_tbl(i),
3986               x_warranty_entl_tbl    => l_entl_rec_tbl,
3987               x_call_process_entl    => l_call_process_entl);
3988 
3989           IF(FND_LOG.LEVEL_EVENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
3990              FND_LOG.STRING(FND_LOG.LEVEL_EVENT,
3991              G_LOG_PREFIX||l_api_name,
3992              'Before calling AHL_WARRANTY_ENTL_PVT.Process_Warranty_Entitlements. Need to call ? ' || l_call_process_entl);
3993           END IF;
3994 
3995           IF(l_call_process_entl = 'Y') THEN
3996              --Not passing p_commit the main API commits the data at the end
3997              AHL_WARRANTY_ENTL_PVT.Process_Warranty_Entitlements(
3998                  p_api_version                 => p_api_version,
3999                  p_validation_level            => p_validation_level,
4000                  p_module_type                 => p_module_type,
4001                  p_user_role                   => AHL_WARRANTY_ENTL_PVT.G_USER_NONPLANNER,
4002                  p_x_warranty_entl_tbl         => l_entl_rec_tbl,
4003                  x_return_status               => l_return_status,
4004                  x_msg_count                   => l_msg_count,
4005                  x_msg_data                    => l_msg_data);
4006 
4007              IF(FND_LOG.LEVEL_EVENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4008                 FND_LOG.STRING(FND_LOG.LEVEL_EVENT,
4009                 G_LOG_PREFIX||l_api_name,
4010                 'After calling AHL_WARRANTY_ENTL_PVT.Process_Warranty_Entitlements. Return Status = ' ||
4011                 l_return_status);
4012              END IF;
4013 
4014              l_msg_count := Fnd_Msg_Pub.count_msg;
4015              IF l_msg_count > 0 OR l_return_status <> Fnd_Api.g_ret_sts_success THEN
4016                 x_msg_count := l_msg_count;
4017                 x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
4018                 RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
4019              END IF;
4020           END IF;
4021           -- salogan added for supplier warranty ends
4022         ELSIF (p_x_osp_order_lines_tbl(i).operation_flag = G_OP_CREATE) THEN
4023           --Add new osp order line
4024           create_osp_order_line(p_x_osp_order_lines_tbl(i));
4025           -- Moved out of the loop for performance gain
4026           --IF (p_x_osp_order_lines_tbl(i).shipment_creation_flag = 'Y') THEN
4027             --create_shipment(p_x_osp_order_lines_tbl(i));
4028           --END IF;
4029           -- salogan added for supplier warranty starts
4030           -- calling Get_Warranty_Rec_for_Create
4031           Get_Warranty_Rec_for_Create(
4032               p_x_osp_order_line_rec => p_x_osp_order_lines_tbl(i),
4033               x_warranty_entl_tbl    => l_entl_rec_tbl,
4034               x_call_process_entl    => l_call_process_entl);
4035 
4036           IF(FND_LOG.LEVEL_EVENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4037              FND_LOG.STRING(FND_LOG.LEVEL_EVENT,
4038              G_LOG_PREFIX||l_api_name,
4039              'Before calling AHL_WARRANTY_ENTL_PVT.Process_Warranty_Entitlements. Need to call ? ' || l_call_process_entl);
4040           END IF;
4041 
4042           IF(l_call_process_entl = 'Y') THEN
4043              --Not passing p_commit the main API commits the data at the end
4044              AHL_WARRANTY_ENTL_PVT.Process_Warranty_Entitlements(
4045                  p_api_version                 => p_api_version,
4046                  p_validation_level            => p_validation_level,
4047                  p_module_type                 => p_module_type,
4048                  p_user_role                   => AHL_WARRANTY_ENTL_PVT.G_USER_PLANNER,
4049                  p_x_warranty_entl_tbl         => l_entl_rec_tbl,
4050                  x_return_status               => l_return_status,
4051                  x_msg_count                   => l_msg_count,
4052                  x_msg_data                    => l_msg_data);
4053 
4054              IF(FND_LOG.LEVEL_EVENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4055                 FND_LOG.STRING(FND_LOG.LEVEL_EVENT,
4056                 G_LOG_PREFIX||l_api_name,
4057                 'After calling AHL_WARRANTY_ENTL_PVT.Process_Warranty_Entitlements. Return Status = ' ||
4058                 l_return_status);
4059              END IF;
4060 
4061              l_msg_count := Fnd_Msg_Pub.count_msg;
4062              IF l_msg_count > 0 OR l_return_status <> Fnd_Api.g_ret_sts_success THEN
4063                 x_msg_count := l_msg_count;
4064                 x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
4065                 RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
4066               END IF;
4067           END IF;
4068           -- salogan added for supplier warranty ends
4069         END IF;
4070       END LOOP;
4071 
4072       --Call create_shipment. Create_shipment will check whether it will create
4073       --shipment or not(if the operation is not Create and no shipment_creation_flag
4074       --is checked, it will do nothing)
4075       create_shipment(p_x_osp_order_lines_tbl);
4076     END IF;
4077 
4078     --To check whether it is necessary to do a post vendor/items combination validation
4079     --after the records were inserted into or updated from the database
4080     derive_common_vendors(p_x_osp_order_rec.osp_order_id,
4081                           l_valid_vendors_tbl,
4082                           l_any_vendor_flag);
4083     BEGIN
4084       SELECT vendor_id, vendor_site_id INTO l_header_vendor_id, l_header_site_id
4085         FROM ahl_osp_orders_b
4086        WHERE osp_order_id = p_x_osp_order_rec.osp_order_id;
4087     EXCEPTION
4088       WHEN NO_DATA_FOUND THEN
4089         FND_MESSAGE.set_name('AHL', 'AHL_OSP_ORD_INVALID');
4090         FND_MSG_PUB.add;
4091         RAISE FND_API.G_EXC_ERROR;
4092     END;
4093     IF l_any_vendor_flag <> 'Y' THEN
4094       IF l_valid_vendors_tbl.count > 0 THEN
4095         IF (l_header_vendor_id IS NULL OR l_header_site_id IS NULL) THEN
4096           l_validate_pass_flag := TRUE;
4097         ELSE
4098           FOR i IN l_valid_vendors_tbl.FIRST..l_valid_vendors_tbl.LAST LOOP
4099             IF (l_header_vendor_id = l_valid_vendors_tbl(i).vendor_id AND
4100                 l_header_site_id = l_valid_vendors_tbl(i).vendor_site_id) THEN
4101               l_validate_pass_flag := TRUE;
4102               EXIT;
4103             END IF;
4104           END LOOP;
4105         END IF;
4106         IF NOT l_validate_pass_flag THEN
4107           FND_MESSAGE.set_name('AHL', 'AHL_OSP_ITEM_VENDOR_MISMATCH');
4108           FND_MSG_PUB.add;
4109           RAISE FND_API.G_EXC_ERROR;
4110         END IF;
4111       ELSE
4112         FND_MESSAGE.set_name('AHL', 'AHL_OSP_ITEM_VENDOR_MISMATCH');
4113         FND_MSG_PUB.add;
4114         RAISE FND_API.G_EXC_ERROR;
4115       END IF;
4116     END IF;
4117   ELSIF (p_x_osp_order_rec.operation_flag = G_OP_CREATE) THEN
4118     --Validate new line records first because in header record only one attribute needs validation
4119     --but it needs line records to derive vendor attributes.
4120     --
4121     --Default the attributes of header record and create OSP order header record
4122     IF (FND_LOG.LEVEL_EVENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4123       FND_LOG.STRING(FND_LOG.LEVEL_EVENT,
4124         G_LOG_PREFIX||l_api_name,
4125         'OSP order header/Lines creation and lines count='||p_x_osp_order_lines_tbl.Count||
4126         'osp_order_id='||p_x_osp_order_rec.osp_order_id);
4127     END IF;
4128     create_osp_order_header(p_x_osp_order_rec, p_x_osp_order_lines_tbl);
4129     IF (p_x_osp_order_lines_tbl IS NOT NULL AND p_x_osp_order_lines_tbl.COUNT > 0) THEN
4130       FOR i IN p_x_osp_order_lines_tbl.FIRST..p_x_osp_order_lines_tbl.LAST LOOP
4131         --Create OSP order line records
4132         --p_x_osp_order_lines_tbl(i).osp_order_id := p_x_osp_order_rec.osp_order_id;
4133         IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4134           FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
4135             G_LOG_PREFIX||l_api_name,
4136             'Within the API and i='||i||'workorder_id='||p_x_osp_order_lines_tbl(i).workorder_id||
4137             'First='||p_x_osp_order_lines_tbl.FIRST||'Last='||p_x_osp_order_lines_tbl.LAST||
4138             'Count='||p_x_osp_order_lines_tbl.Count);
4139         END IF;
4140         p_x_osp_order_lines_tbl(i).osp_order_id := p_x_osp_order_rec.osp_order_id;
4141         create_osp_order_line(p_x_osp_order_lines_tbl(i));
4142         -- Moved out of the loop for performance gain
4143         --IF (p_x_osp_order_lines_tbl(i).shipment_creation_flag = 'Y') THEN
4144           --create_shipment(p_x_osp_order_lines_tbl(i));
4145         --END IF;
4146         -- salogan added for supplier warranty starts
4147         -- calling Get_Warranty_Rec_for_Create
4148         Get_Warranty_Rec_for_Create(
4149             p_x_osp_order_line_rec => p_x_osp_order_lines_tbl(i),
4150             x_warranty_entl_tbl    => l_entl_rec_tbl,
4151             x_call_process_entl    => l_call_process_entl);
4152 
4153         IF(FND_LOG.LEVEL_EVENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4154            FND_LOG.STRING(FND_LOG.LEVEL_EVENT,
4155            G_LOG_PREFIX||l_api_name,
4156            'Before calling AHL_WARRANTY_ENTL_PVT.Process_Warranty_Entitlements. Need to call ? ' || l_call_process_entl);
4157         END IF;
4158 
4159         IF(l_call_process_entl = 'Y') THEN
4160            --Not passing p_commit the main API commits the data at the end
4161            AHL_WARRANTY_ENTL_PVT.Process_Warranty_Entitlements(
4162                p_api_version                 => p_api_version,
4163                p_validation_level            => p_validation_level,
4164                p_module_type                 => p_module_type,
4165                p_user_role                   => AHL_WARRANTY_ENTL_PVT.G_USER_PLANNER,
4166                p_x_warranty_entl_tbl         => l_entl_rec_tbl,
4167                x_return_status               => l_return_status,
4168                x_msg_count                   => l_msg_count,
4169                x_msg_data                    => l_msg_data);
4170 
4171            IF(FND_LOG.LEVEL_EVENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4172               FND_LOG.STRING(FND_LOG.LEVEL_EVENT,
4173               G_LOG_PREFIX||l_api_name,
4174               'After calling AHL_WARRANTY_ENTL_PVT.Process_Warranty_Entitlements. Return Status = ' ||
4175               l_return_status);
4176            END IF;
4177 
4178            l_msg_count := Fnd_Msg_Pub.count_msg;
4179            IF l_msg_count > 0 OR l_return_status <> Fnd_Api.g_ret_sts_success THEN
4180               x_msg_count := l_msg_count;
4181               x_return_status := Fnd_Api.G_RET_STS_UNEXP_ERROR;
4182               RAISE Fnd_Api.G_EXC_UNEXPECTED_ERROR;
4183            END IF;
4184         END IF;
4185         -- salogan added for supplier warranty ends
4186       END LOOP;
4187       --Call create_shipment. Create_shipment will check whether it will create
4188       --shipment or not(if the operation is not Create and no shipment_creation_flag
4189       --is checked, it will do nothing)
4190       create_shipment(p_x_osp_order_lines_tbl);
4191     END IF;
4192   END IF;
4193 
4194   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4195     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
4196       G_LOG_PREFIX||l_api_name,
4197       'API End: at the end of the procedure');
4198   END IF;
4199 
4200 /* Customer Post Processing section - mandatory */
4201   IF (JTF_USR_HKS.Ok_to_execute('AHL_OSP_ORDERS_PVT', 'PROCESS_OSP_ORDER', 'A', 'C' )) then
4202     ahl_osp_orders_CUHK. process_osp_order_Post(
4203       p_osp_order_rec => p_x_osp_order_rec,
4204       p_osp_order_lines_tbl => p_x_osp_order_lines_tbl,
4205       x_msg_count => l_msg_count,
4206       x_msg_data => l_msg_data,
4207       x_return_status => l_return_status);
4208     IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
4209       RAISE FND_API.G_EXC_ERROR;
4210     ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
4211       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4212     END IF;
4213   END IF;
4214 
4215   -- Get all the error messages from the previous steps (if any) and raise the appropriate Exception
4216   l_msg_count := FND_MSG_PUB.count_msg;
4217   IF l_msg_count > 0 THEN
4218     x_msg_count := l_msg_count;
4219     RAISE FND_API.G_EXC_ERROR;
4220   END IF;
4221 
4222   -- Perform the Commit (if requested)
4223   IF FND_API.to_boolean(p_commit) THEN
4224     COMMIT;
4225   END IF;
4226 
4227   -- Count and Get messages (optional)
4228   FND_MSG_PUB.count_and_get(
4229     p_encoded  => FND_API.G_FALSE,
4230     p_count    => x_msg_count,
4231     p_data     => x_msg_data);
4232 
4233 EXCEPTION
4234   WHEN FND_API.G_EXC_ERROR THEN
4235     ROLLBACK TO process_osp_order;
4236     x_return_status := FND_API.G_RET_STS_ERROR;
4237     FND_MSG_PUB.count_and_get(
4238       p_encoded  => FND_API.G_FALSE,
4239       p_count    => x_msg_count,
4240       p_data     => x_msg_data);
4241 
4242   WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4243     ROLLBACK TO process_osp_order;
4244     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4245     FND_MSG_PUB.count_and_get(
4246       p_encoded  => FND_API.G_FALSE,
4247       p_count    => x_msg_count,
4248       p_data     => x_msg_data);
4249 
4250   WHEN OTHERS THEN
4251     ROLLBACK TO process_osp_order;
4252     x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4253     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
4254     THEN
4255       FND_MSG_PUB.add_exc_msg(
4256         p_pkg_name         => G_PKG_NAME,
4257         p_procedure_name   => l_api_name,
4258         p_error_text       => SUBSTRB(SQLERRM,1,240));
4259     END IF;
4260     FND_MSG_PUB.count_and_get(
4261       p_encoded  => FND_API.G_FALSE,
4262       p_count    => x_msg_count,
4263       p_data     => x_msg_data);
4264 END process_osp_order;
4265 
4266 -- create_osp_order_header Procedure revamped by jaramana on May 9, 2006 for Bug 5215894
4267 -- to make the process_osp_order API conform to Oracle Apps Public API Standards
4268 -- in the create mode also.
4269 -- 1. Perform Value to Id conversions
4270 -- 2. Honor the input values/Ids passed
4271 -- 3. Validate the Ids passed or derived
4272 PROCEDURE create_osp_order_header(p_x_osp_order_rec IN OUT NOCOPY osp_order_rec_type,
4273                                   p_x_osp_order_lines_tbl IN OUT NOCOPY osp_order_lines_tbl_type)
4274 IS
4275   l_operating_unit_id        NUMBER;
4276   l_osp_order_id             NUMBER;
4277   l_osp_order_number         NUMBER;
4278   l_item_service_rels_tbl    item_service_rels_tbl_type;
4279   l_vendor_id                NUMBER;
4280   l_vendor_site_id           NUMBER;
4281   l_vendor_contact_id        NUMBER;
4282   l_return_status            VARCHAR2(1);
4283   l_organization_id          NUMBER;
4284   l_inventory_item_id        NUMBER;
4285   l_service_item_id          NUMBER;
4286   l_rowid_dummy              VARCHAR2(100);
4287   l_buyer_id                 NUMBER;
4288   l_valid_vendors_tbl        vendor_id_tbl_type;
4289   l_temp_num                 NUMBER;
4290   l_vendor_valid_flag        BOOLEAN := FALSE;
4291 
4292   CURSOR get_buyer_id IS
4293     SELECT PA.buyer_id
4294       FROM po_agents_name_v PA,
4295            fnd_user FU
4296      WHERE FU.user_id = fnd_global.user_id
4297        AND PA.buyer_id = FU.employee_id;
4298 BEGIN
4299   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4300     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
4301                    G_LOG_PREFIX || 'create_osp_order_header',
4302                    'Procedure begins...');
4303   END IF;
4304 
4305   --Generate the primary key
4306   SELECT ahl_osp_orders_b_s.NEXTVAL
4307     INTO l_osp_order_id
4308     FROM sys.dual;
4309   --If there is no records in the table, then max(osp_order_number) IS null
4310   --SELECT NVL(MAX(osp_order_number), l_osp_order_id-1)+1
4311   --  INTO l_osp_order_number
4312   --  FROM ahl_osp_orders_b;
4313   --Finally decided to change back because the above logic will probably violate the unique
4314   --index defined on osp_order_number if two concurrent users submit the OSP Order creation at
4315   --the same time
4316   l_osp_order_number := l_osp_order_id;
4317   --Derive operating_unit_id
4318   l_operating_unit_id :=mo_global.get_current_org_id();
4319   IF (l_operating_unit_id IS NULL) THEN
4320     FND_MESSAGE.Set_Name('AHL', 'AHL_OSP_ORG_NOT_SET');
4321     FND_MSG_PUB.ADD;
4322     RAISE FND_API.G_EXC_ERROR;
4323   END IF;
4324   --Validate the only user passed parameter order_type_code
4325   IF (p_x_osp_order_rec.order_type_code IS NULL OR
4326       p_x_osp_order_rec.order_type_code NOT IN (G_OSP_ORDER_TYPE_SERVICE,
4327                                                 G_OSP_ORDER_TYPE_EXCHANGE,
4328                                                 G_OSP_ORDER_TYPE_BORROW)) THEN
4329     FND_MESSAGE.set_name('AHL', 'AHL_OSP_ORDER_TYPE_INVALID');
4330     FND_MSG_PUB.add;
4331     RAISE FND_API.G_EXC_ERROR;
4332   END IF;
4333 
4334   -- Added by jaramana on January 10, 2008 for the Requisition ER 6034236
4335   --This API nulls out the G_MISS info, if passed by the user in the create mode.
4336   IF nvl(g_module_type, 'NULL') <> G_OAF_MODULE THEN
4337     default_unchanged_order_header(p_x_osp_order_rec);
4338   END IF;
4339   -- jaramana End
4340 
4341   -- Added by jaramana on May 9, 2006 for Bug 5215894
4342   convert_order_header_val_to_id(p_x_osp_order_rec);
4343 
4344   --Derive vendor related attributes
4345   IF (p_x_osp_order_lines_tbl IS NULL OR p_x_osp_order_lines_tbl.COUNT = 0) THEN
4346     l_vendor_id := NULL;
4347     l_vendor_site_id := NULL;
4348     l_vendor_contact_id := NULL;
4349     /** Added by jaramana on May 9, 2006 for Bug 5215894 **/
4350     IF (p_x_osp_order_rec.vendor_id IS NOT NULL) THEN
4351       IF (p_x_osp_order_rec.vendor_site_id IS NOT NULL) THEN
4352         IF (p_x_osp_order_rec.vendor_contact_id IS NOT NULL) THEN
4353           -- Validate the combination of vendor/site/contact
4354           validate_vendor_site_contact(p_x_osp_order_rec.vendor_id,
4355                                        p_x_osp_order_rec.vendor_site_id,
4356                                        p_x_osp_order_rec.vendor_contact_id);
4357           IF FND_MSG_PUB.count_msg > 0 THEN
4358             -- Vendor/Vendor Site/Vendor Contact is Invalid
4359             RAISE  FND_API.G_EXC_ERROR;
4360           END IF;
4361           -- If valid, use the input values
4362           l_vendor_id := p_x_osp_order_rec.vendor_id;
4363           l_vendor_site_id := p_x_osp_order_rec.vendor_site_id;
4364           l_vendor_contact_id := p_x_osp_order_rec.vendor_contact_id;
4365         ELSE
4366           -- Validate the vendor and vendor site
4367           validate_vendor_site(p_x_osp_order_rec.vendor_id,
4368                                p_x_osp_order_rec.vendor_site_id);
4369           IF FND_MSG_PUB.count_msg > 0 THEN
4370             -- Vendor/Vendor Site/Vendor Contact is Invalid
4371             RAISE  FND_API.G_EXC_ERROR;
4372           END IF;
4373           -- If valid, use the input values
4374           l_vendor_id := p_x_osp_order_rec.vendor_id;
4375           l_vendor_site_id := p_x_osp_order_rec.vendor_site_id;
4376         END IF;
4377       ELSE
4378         -- Validate the Vendor
4379         validate_vendor(p_x_osp_order_rec.vendor_id);
4380         IF FND_MSG_PUB.count_msg > 0 THEN
4381           -- Vendor/Vendor Site/Vendor Contact is Invalid
4382           RAISE  FND_API.G_EXC_ERROR;
4383         END IF;
4384         -- If valid, use the input values
4385         l_vendor_id := p_x_osp_order_rec.vendor_id;
4386       END IF;
4387     END IF;
4388     /** End addition by jaramana on May 9, 2006 for Bug 5215894 **/
4389   ELSIF (p_x_osp_order_lines_tbl.COUNT > 0) THEN
4390     IF (p_x_osp_order_lines_tbl(p_x_osp_order_lines_tbl.FIRST).workorder_id IS NULL) THEN
4391     --Only need to check the the first record, either all work order lines or inventory
4392     --service order lines. All service order lines
4393       FOR i IN p_x_osp_order_lines_tbl.FIRST..p_x_osp_order_lines_tbl.LAST LOOP
4394         l_item_service_rels_tbl(i).inv_org_id := p_x_osp_order_lines_tbl(i).inventory_org_id;
4395         l_item_service_rels_tbl(i).inv_item_id := p_x_osp_order_lines_tbl(i).inventory_item_id;
4396         l_item_service_rels_tbl(i).service_item_id := p_x_osp_order_lines_tbl(i).service_item_id;
4397       END LOOP;
4398     ELSE
4399       --All work order lines
4400       FOR i IN p_x_osp_order_lines_tbl.FIRST..p_x_osp_order_lines_tbl.LAST LOOP
4401         BEGIN
4402         --Modified by mpothuku on 27-Feb-06 to fix the Perf Bug #4919164
4403          /*
4404           SELECT organization_id, inventory_item_id, service_item_id
4405             INTO l_organization_id, l_inventory_item_id, l_service_item_id
4406             FROM ahl_workorders_osp_v
4407            WHERE workorder_id = p_x_osp_order_lines_tbl(i).workorder_id;
4408          */
4409         SELECT vst.organization_id, vts.inventory_item_id, arb.service_item_id
4410           INTO l_organization_id, l_inventory_item_id, l_service_item_id
4411           FROM ahl_workorders wo,
4412                ahl_visits_b vst,
4413                ahl_visit_tasks_b vts,
4414                ahl_routes_b arb,
4415                inv_organization_info_v org
4416          WHERE wo.workorder_id = p_x_osp_order_lines_tbl (i).workorder_id
4417            AND wo.route_id = arb.route_id(+)
4418            AND wo.master_workorder_flag = 'N'
4419            AND wo.visit_id = vst.visit_id
4420            AND wo.visit_task_id = vts.visit_task_id
4421            AND vst.visit_id = vts.visit_id
4422            AND vst.organization_id = org.organization_id
4423            AND NVL (org.operating_unit, mo_global.get_current_org_id ())= mo_global.get_current_org_id();
4424 
4425           l_item_service_rels_tbl(i).inv_org_id := l_organization_id;
4426           l_item_service_rels_tbl(i).inv_item_id := l_inventory_item_id;
4427           l_item_service_rels_tbl(i).service_item_id := l_service_item_id;
4428         EXCEPTION
4429           WHEN NO_DATA_FOUND THEN
4430             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_WO');
4431             FND_MESSAGE.Set_Token('WORKORDER_ID', p_x_osp_order_lines_tbl(i).workorder_id);
4432             FND_MSG_PUB.ADD;
4433             RAISE FND_API.G_EXC_ERROR;
4434         END;
4435       END LOOP;
4436     END IF;
4437     derive_default_vendor(l_item_service_rels_tbl,
4438                           l_vendor_id,
4439                           l_vendor_site_id,
4440                           l_vendor_contact_id,
4441                           l_valid_vendors_tbl);
4442     /** Added by jaramana on May 9, 2006 for Bug 5215894 **/
4443     -- If the user has passed a Vendor or Vendor + Vendor Site, validate and use these instead of the default
4444     -- If only Vendor Site is passed (without the Vendor), ignore and use default
4445     IF (p_x_osp_order_rec.vendor_id IS NOT NULL) THEN
4446       IF (l_valid_vendors_tbl.count > 0) THEN
4447         FOR l_temp_num in l_valid_vendors_tbl.FIRST..l_valid_vendors_tbl.LAST LOOP
4448           IF (l_valid_vendors_tbl(l_temp_num).vendor_id = p_x_osp_order_rec.vendor_id AND
4449               l_valid_vendors_tbl(l_temp_num).vendor_site_id = NVL(p_x_osp_order_rec.vendor_site_id, l_valid_vendors_tbl(l_temp_num).vendor_site_id)) THEN
4450             l_vendor_id := p_x_osp_order_rec.vendor_id;
4451             l_vendor_site_id := NVL(p_x_osp_order_rec.vendor_site_id, l_valid_vendors_tbl(l_temp_num).vendor_site_id);
4452             l_vendor_valid_flag := TRUE;
4453             EXIT;
4454           END IF;
4455         END LOOP;
4456       END IF;
4457       IF (l_vendor_valid_flag = FALSE) THEN
4458         -- The vendor or vendor + vendor site passed in is not a valid: Throw Error
4459         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENDOR_ID_INV');
4460         FND_MESSAGE.Set_Token('VENDOR_ID', p_x_osp_order_rec.vendor_id);
4461         FND_MSG_PUB.ADD;
4462         RAISE FND_API.G_EXC_ERROR;
4463       END IF;
4464       IF (p_x_osp_order_rec.vendor_site_id IS NOT NULL) THEN
4465         IF (p_x_osp_order_rec.vendor_contact_id IS NOT NULL) THEN
4466           -- Check if p_x_osp_order_rec.vendor_contact_id is valid
4467           validate_vendor_site_contact(p_x_osp_order_rec.vendor_id,
4468                                        p_x_osp_order_rec.vendor_site_id,
4469                                        p_x_osp_order_rec.vendor_contact_id);
4470           IF FND_MSG_PUB.count_msg > 0 THEN
4471             -- Vendor/Vendor Site/Vendor Contact is Invalid
4472             RAISE  FND_API.G_EXC_ERROR;
4473           END IF;
4474           -- Vendor Contact is valid: Use it
4475           l_vendor_contact_id := p_x_osp_order_rec.vendor_contact_id;
4476         ELSE
4477           -- User has passed Vendor and Vendor Site, but not Vendor Contact: Leave Contact as null
4478           l_vendor_contact_id := null;
4479         END IF;
4480       ELSE
4481         -- User has passed Vendor, but not Vendor Site: Set Contact as null even if passed
4482         l_vendor_contact_id := null;
4483       END IF;
4484     END IF;
4485   END IF;
4486 
4487   IF (p_x_osp_order_rec.po_agent_id IS NOT NULL) THEN
4488     -- Validate the buyer
4489     validate_buyer(p_x_osp_order_rec.po_agent_id);
4490     IF FND_MSG_PUB.count_msg > 0 THEN
4491       -- Buyer is Invalid
4492       RAISE  FND_API.G_EXC_ERROR;
4493     END IF;
4494     -- Buyer is valid: Use it
4495     l_buyer_id := p_x_osp_order_rec.po_agent_id;
4496   ELSE
4497     --Derive the default po_agent_id
4498     l_buyer_id := fnd_global.user_id;
4499     OPEN get_buyer_id;
4500     FETCH get_buyer_id INTO l_buyer_id;
4501     CLOSE get_buyer_id;
4502   END IF;
4503   /** End addition by jaramana on May 9, 2006 for Bug 5215894 **/
4504 
4505   --In Create mode, assume that UI will never pass G_MISS value(if it is null in UI, then just pass
4506   --Null to the API). Thus in this Create procedure, there are only three attributes probably need
4507   --validation: customer_id, contract_id and contract_terms
4508   AHL_OSP_ORDERS_PKG.insert_row(
4509     x_rowid => l_rowid_dummy,
4510     x_osp_order_id => l_osp_order_id,
4511     x_object_version_number => 1,
4512     x_created_by => FND_GLOBAL.user_id,
4513     x_creation_date => SYSDATE,
4514     x_last_updated_by => FND_GLOBAL.user_id,
4515     x_last_update_date => SYSDATE,
4516     x_last_update_login => FND_GLOBAL.login_id,
4517     x_osp_order_number => l_osp_order_number,
4518     x_order_type_code => p_x_osp_order_rec.order_type_code, --User entered
4519     x_single_instance_flag => G_NO_FLAG,
4520     x_po_header_id => NULL, --p_x_osp_order_rec.po_header_id,
4521     x_oe_header_id => NULL, --p_x_osp_order_rec.oe_header_id,
4522     x_vendor_id => l_vendor_id,
4523     x_vendor_site_id => l_vendor_site_id,
4524     x_vendor_contact_id => l_vendor_contact_id,
4525     x_customer_id => p_x_osp_order_rec.customer_id,
4526     x_order_date => TRUNC(SYSDATE),
4527     x_contract_id => p_x_osp_order_rec.contract_id,
4528     x_contract_terms => p_x_osp_order_rec.contract_terms,
4529     x_operating_unit_id => l_operating_unit_id,
4530     x_po_synch_flag => NULL, --p_x_osp_order_rec.po_synch_flag,
4531     x_status_code => G_OSP_ENTERED_STATUS,
4532     x_po_batch_id => NULL, --p_x_osp_order_rec.po_batch_id,
4533     x_po_request_id => NULL, --p_x_osp_order_rec.po_request_id,
4534     x_po_agent_id => l_buyer_id,
4535     x_po_interface_header_id => NULL, --p_x_osp_order_rec.po_interface_header_id,
4536     -- Added by jaramana on January 10, 2008 for the Requisition ER 6034236
4537     x_po_req_header_id => NULL,
4538     -- jaramana End
4539     x_description => p_x_osp_order_rec.description,
4540     x_attribute_category => p_x_osp_order_rec.attribute_category,
4541     x_attribute1 => p_x_osp_order_rec.attribute1,
4542     x_attribute2 => p_x_osp_order_rec.attribute2,
4543     x_attribute3 => p_x_osp_order_rec.attribute3,
4544     x_attribute4 => p_x_osp_order_rec.attribute4,
4545     x_attribute5 => p_x_osp_order_rec.attribute5,
4546     x_attribute6 => p_x_osp_order_rec.attribute6,
4547     x_attribute7 => p_x_osp_order_rec.attribute7,
4548     x_attribute8 => p_x_osp_order_rec.attribute8,
4549     x_attribute9 => p_x_osp_order_rec.attribute9,
4550     x_attribute10 => p_x_osp_order_rec.attribute10,
4551     x_attribute11 => p_x_osp_order_rec.attribute11,
4552     x_attribute12 => p_x_osp_order_rec.attribute12,
4553     x_attribute13 => p_x_osp_order_rec.attribute13,
4554     x_attribute14 => p_x_osp_order_rec.attribute14,
4555     x_attribute15 => p_x_osp_order_rec.attribute15);
4556   --Return the generated order header id to the output record structure
4557   p_x_osp_order_rec.osp_order_id := l_osp_order_id;
4558   IF (p_x_osp_order_lines_tbl IS NOT NULL AND p_x_osp_order_lines_tbl.COUNT > 0) THEN
4559     FOR i IN p_x_osp_order_lines_tbl.FIRST..p_x_osp_order_lines_tbl.LAST LOOP
4560       p_x_osp_order_lines_tbl(i).osp_order_id := l_osp_order_id;
4561     END LOOP;
4562   END IF;
4563   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4564     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
4565                    G_LOG_PREFIX || '.create_osp_order_header',
4566                    'Procedure exits normally');
4567   END IF;
4568 END create_osp_order_header;
4569 
4570 PROCEDURE update_osp_order_header(p_x_osp_order_rec IN OUT NOCOPY osp_order_rec_type)
4571 IS
4572   l_operating_unit_id        NUMBER;
4573   l_osp_order_id             NUMBER;
4574   l_osp_order_number         NUMBER;
4575   l_item_service_rels_tbl    item_service_rels_tbl_type;
4576   l_vendor_id                NUMBER;
4577   l_vendor_site_id           NUMBER;
4578   l_vendor_contact_id        NUMBER;
4579   l_return_status            VARCHAR2(1);
4580   l_msg_count                NUMBER;
4581   l_msg_data                 VARCHAR2(2000);
4582   l_organization_id          NUMBER;
4583   l_inventory_item_id        NUMBER;
4584   l_service_item_id          NUMBER;
4585   l_dummy_num                NUMBER;
4586   l_osp_order_line_rec       osp_order_line_rec_type;
4587   l_oe_header_id             NUMBER;
4588   CURSOR get_all_lines(c_osp_order_id NUMBER) IS
4589     SELECT osp_order_line_id, inventory_item_id,
4590            inventory_org_id, service_item_id, workorder_id
4591       FROM ahl_osp_order_lines
4592      WHERE osp_order_id = c_osp_order_id;
4593   CURSOR get_old_vendor_attrs(c_osp_order_id NUMBER) IS
4594     SELECT vendor_id, vendor_site_id, oe_header_id
4595       FROM ahl_osp_orders_b
4596      WHERE osp_order_id = c_osp_order_id;
4597 
4598 BEGIN
4599   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4600     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
4601                    G_LOG_PREFIX || '.update_osp_order_header',
4602                    'Procedure begins and osp_order_id='||p_x_osp_order_rec.osp_order_id);
4603   END IF;
4604   --This three procedures are borrowed from the original ones
4605   --For OAF, the default procedure may not be necessary.
4606   --we decided to pass null when you want to change it to null, the old value if there is no change.
4607   IF nvl(g_module_type, 'NULL') <> G_OAF_MODULE THEN
4608     default_unchanged_order_header(p_x_osp_order_rec);
4609   END IF;
4610   convert_order_header_val_to_id(p_x_osp_order_rec);
4611   validate_order_header(p_x_osp_order_rec);
4612 
4613   --Handling if order status is to be changed
4614   process_order_status_change(p_x_osp_order_rec);
4615   --Handling if order type is to be changed
4616   process_order_type_change(p_x_osp_order_rec);
4617 
4618   OPEN get_old_vendor_attrs(p_x_osp_order_rec.osp_order_id);
4619   FETCH get_old_vendor_attrs INTO l_vendor_id, l_vendor_site_id, l_oe_header_id;
4620   CLOSE get_old_vendor_attrs;
4621 
4622   --Call table handler to update
4623   AHL_OSP_ORDERS_PKG.update_row(
4624         x_osp_order_id => p_x_osp_order_rec.osp_order_id,
4625         x_object_version_number => p_x_osp_order_rec.object_version_number + 1,
4626         x_osp_order_number => p_x_osp_order_rec.osp_order_number,
4627         x_order_type_code => p_x_osp_order_rec.order_type_code,
4628         x_single_instance_flag => p_x_osp_order_rec.single_instance_flag,
4629         x_po_header_id => p_x_osp_order_rec.po_header_id,
4630         x_oe_header_id => p_x_osp_order_rec.oe_header_id,
4631         x_vendor_id => p_x_osp_order_rec.vendor_id,
4632         x_vendor_site_id => p_x_osp_order_rec.vendor_site_id,
4633         x_vendor_contact_id => p_x_osp_order_rec.vendor_contact_id,
4634         x_customer_id => p_x_osp_order_rec.customer_id,
4635         x_order_date => TRUNC(p_x_osp_order_rec.order_date),
4636         x_contract_id => p_x_osp_order_rec.contract_id,
4637         x_contract_terms => p_x_osp_order_rec.contract_terms,
4638         x_operating_unit_id => p_x_osp_order_rec.operating_unit_id,
4639         x_po_synch_flag => p_x_osp_order_rec.po_synch_flag,
4640         x_status_code => p_x_osp_order_rec.status_code,
4641         x_po_batch_id => p_x_osp_order_rec.po_batch_id,
4642         x_po_request_id => p_x_osp_order_rec.po_request_id,
4643         x_po_agent_id => p_x_osp_order_rec.po_agent_id,
4644         x_po_interface_header_id => p_x_osp_order_rec.po_interface_header_id,
4645 -- Added by jaramana on January 10, 2008 for the Requisition ER 6034236
4646         x_po_req_header_id => p_x_osp_order_rec.po_req_header_id,
4647 -- jaramana End
4648         x_description => p_x_osp_order_rec.description,
4649         x_attribute_category => p_x_osp_order_rec.attribute_category,
4650         x_attribute1 => p_x_osp_order_rec.attribute1,
4651         x_attribute2 => p_x_osp_order_rec.attribute2,
4652         x_attribute3 => p_x_osp_order_rec.attribute3,
4653         x_attribute4 => p_x_osp_order_rec.attribute4,
4654         x_attribute5 => p_x_osp_order_rec.attribute5,
4655         x_attribute6 => p_x_osp_order_rec.attribute6,
4656         x_attribute7 => p_x_osp_order_rec.attribute7,
4657         x_attribute8 => p_x_osp_order_rec.attribute8,
4658         x_attribute9 => p_x_osp_order_rec.attribute9,
4659         x_attribute10 => p_x_osp_order_rec.attribute10,
4660         x_attribute11 => p_x_osp_order_rec.attribute11,
4661         x_attribute12 => p_x_osp_order_rec.attribute12,
4662         x_attribute13 => p_x_osp_order_rec.attribute13,
4663         x_attribute14 => p_x_osp_order_rec.attribute14,
4664         x_attribute15 => p_x_osp_order_rec.attribute15,
4665         x_last_updated_by => fnd_global.user_id,
4666         x_last_update_date => SYSDATE,
4667         x_last_update_login => fnd_global.login_id
4668       );
4669   --After the header vendor gets changed then calling validate_vendor_service_item.
4670   --If it is not for calling validate_vendor_service_item, then it is better to put
4671   --this validation ahead of table_handler calling
4672   --Validate the header vendor with physical/Service item combinations in lines
4673   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4674     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
4675                    G_LOG_PREFIX || '.update_osp_order_header',
4676                    'new_vendor='||p_x_osp_order_rec.vendor_id||
4677                    'old_vendor='||l_vendor_id);
4678   END IF;
4679   --The following validation was moved to process_osp_order and acting like
4680   --a post database operation validation (Jerry) 06/15/2005
4681   --IF ((p_x_osp_order_rec.vendor_id <> l_vendor_id) OR
4682       --(p_x_osp_order_rec.vendor_id IS NOT NULL AND l_vendor_id IS NULL) OR
4683       --(p_x_osp_order_rec.vendor_site_id <> l_vendor_site_id) OR
4684       --(p_x_osp_order_rec.vendor_site_id IS NOT NULL AND l_vendor_site_id IS NULL)) THEN
4685     -- Both new vendor_id and vendor_site_id can't be null. Already validated in
4686     -- validate_order_header
4687     --FOR l_get_all_lines IN get_all_lines(p_x_osp_order_rec.osp_order_id) LOOP
4688       --l_osp_order_line_rec.osp_order_id := p_x_osp_order_rec.osp_order_id;
4689       --l_osp_order_line_rec.osp_order_line_id := l_get_all_lines.osp_order_line_id;
4690       --l_osp_order_line_rec.inventory_item_id := l_get_all_lines.inventory_item_id;
4691       --l_osp_order_line_rec.inventory_org_id := l_get_all_lines.inventory_org_id;
4692       --l_osp_order_line_rec.service_item_id := l_get_all_lines.service_item_id;
4693       --IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4694         --FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
4695                    --G_LOG_PREFIX || '.update_osp_order_header',
4696                    --'before calling validate_vendor_service_item'||
4697                    --'line_id='||l_get_all_lines.osp_order_line_id||
4698                    --'inventory_item_id ='||l_osp_order_line_rec.inventory_item_id||
4699                    --'org_id = '||l_osp_order_line_rec.inventory_org_id||
4700                    --'service_id = '||l_osp_order_line_rec.service_item_id);
4701       --END IF;
4702       -- validate_vendor_service_item(l_osp_order_line_rec);
4703     --END LOOP;
4704   --END IF;
4705   IF (FND_LOG.LEVEL_EVENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4706     FND_LOG.STRING(FND_LOG.LEVEL_EVENT,
4707                    G_LOG_PREFIX || '.update_osp_order_header',
4708                    'Before calling AHL_OSP_SHIPMENT_PUB.handle_vendor_change');
4709   END IF;
4710   IF ((l_oe_header_id IS NOT NULL) AND
4711       ((p_x_osp_order_rec.vendor_id IS NOT NULL AND l_vendor_id IS NULL) OR
4712        (p_x_osp_order_rec.vendor_id <> l_vendor_id) OR
4713        -- Added by jaramana on January 10, 2008 to fix the issue with the SO Customer change if the Vendor Site is Changed, Bug 6521712.
4714        (p_x_osp_order_rec.vendor_site_id IS NOT NULL AND l_vendor_site_id IS NULL) OR
4715        (p_x_osp_order_rec.vendor_site_id <> l_vendor_site_id ))) THEN
4716   --Only if vendor gets changed, then call the shipment API to handle the change
4717     AHL_OSP_SHIPMENT_PUB.handle_vendor_change(
4718       p_osp_order_id  => p_x_osp_order_rec.osp_order_id,
4719       p_vendor_id     => p_x_osp_order_rec.vendor_id,
4720       p_vendor_loc_id => p_x_osp_order_rec.vendor_site_id,
4721       x_return_status => l_return_status,
4722       x_msg_count     => l_msg_count,
4723       x_msg_data      => l_msg_data);
4724     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4725       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
4726                      G_LOG_PREFIX || '.update_osp_order_header',
4727                      'Normally exit after calling AHL_OSP_SHIPMENT_PUB.handle_vendor_change'||
4728                      'x_return_status='||l_return_status);
4729     END IF;
4730   END IF;
4731 END update_osp_order_header;
4732 
4733 PROCEDURE create_osp_order_line(p_x_osp_order_line_rec IN OUT NOCOPY osp_order_line_rec_type)
4734 IS
4735   l_osp_line_id              NUMBER;
4736   l_osp_line_number          NUMBER;
4737   l_return_status            VARCHAR2(1);
4738   l_organization_id          NUMBER;
4739   l_inventory_item_id        NUMBER;
4740   l_service_item_id          NUMBER;
4741   l_service_duration         NUMBER;
4742   l_quantity                 NUMBER;
4743   l_service_item_uom_code    VARCHAR2(3);
4744   l_temp_uom_code            VARCHAR2(3);
4745   l_item_description         VARCHAR2(240);
4746   l_vendor_id                NUMBER;
4747   l_vendor_site_id           NUMBER;
4748   --Added by mpothuku on 24-Mar-06 for ER: 4544654
4749   l_owrite_svc_desc_prf VARCHAR2(1);
4750   l_item_prefix         VARCHAR2(240);
4751   l_serial_prefix       VARCHAR2(240);
4752   l_inv_item_number     VARCHAR2(40);
4753   l_svc_item_number     VARCHAR2(40);
4754   l_desc_update_flag    VARCHAR2(1);
4755   --mpothuku end
4756   CURSOR get_wo_item_attrs(c_workorder_id NUMBER) IS
4757   --Modified by mpothuku on 27-Feb-06 to fix the Perf Bug #4919164
4758   /*
4759     SELECT inventory_item_id,
4760            organization_id,
4761            lot_number,
4762            serial_number,
4763            quantity, --This quantity is from csi, so it means instance quantity
4764            item_instance_uom,
4765            service_item_id,
4766            service_item_description,
4767            service_item_uom
4768       FROM ahl_workorders_osp_v
4769      WHERE workorder_id = c_workorder_id;
4770  */
4771      SELECT vts.inventory_item_id,
4772            vst.organization_id,
4773            csii.lot_number,
4774            csii.serial_number,
4775            csii.quantity, --This quantity is from csi, so it means instance quantity
4776            csii.unit_of_measure item_instance_uom,
4777            arb.service_item_id,
4778            mtls.description service_item_description,
4779            mtls.primary_uom_code service_item_uom
4780       FROM ahl_workorders wo,
4781            ahl_visits_b vst,
4782            ahl_visit_tasks_b vts,
4783            csi_item_instances csii,
4784            mtl_system_items_kfv mtls,
4785            ahl_routes_b arb
4786      WHERE wo.workorder_id = c_workorder_id
4787        AND wo.visit_task_id = vts.visit_task_id
4788        AND vst.visit_id = vts.visit_id
4789        AND wo.route_id = arb.route_id(+)
4790        AND arb.service_item_org_id = mtls.organization_id(+)
4791        AND arb.service_item_id = mtls.inventory_item_id(+)
4792        AND vts.instance_id = csii.instance_id;
4793 BEGIN
4794   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4795     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, G_LOG_PREFIX || '.create_osp_order_line',
4796                    'Before line validation:'||
4797                    'osp_order_id='||p_x_osp_order_line_rec.osp_order_id);
4798   END IF;
4799   --Validate the order line
4800   validate_order_line_creation(p_x_osp_order_line_rec);
4801 
4802 --yazhou 22-Aug-2006 starts
4803 -- Bug fix#5479266
4804 
4805   IF FND_MSG_PUB.count_msg > 0 THEN
4806     RAISE FND_API.G_EXC_ERROR;
4807   END IF;
4808 
4809 --yazhou 22-Aug-2006 ends
4810 
4811   --Generate the primary key
4812   SELECT ahl_osp_order_lines_s.NEXTVAL
4813     INTO l_osp_line_id
4814     FROM sys.dual;
4815 
4816   --Generate the line number
4817   --NOTE: This logic to generate osp_line number will probably violate the unique
4818   --index defined on osp_line_number if two concurrent users try to add osp order line
4819   --to the same OSP Order headet at the same time.
4820   SELECT NVL(MAX(osp_line_number), 0)+1
4821     INTO l_osp_line_number
4822     FROM ahl_osp_order_lines
4823    WHERE osp_order_id = p_x_osp_order_line_rec.osp_order_id;
4824 
4825   /*
4826     Changed by jaramana on January 10, 2008
4827     Fix for the Bug 5358438/5967633/5417460
4828     Its better not to change the existing flow if the po_line_id is passed. The attribute defaulting as well as validation
4829     happens in the validate_order_creation, we only need to call the insert_row method here.
4830   */
4831   IF(p_x_osp_order_line_rec.po_line_id is null) THEN
4832 
4833   --For work order based lines, it is better to populate the physical item/service item related attributes
4834   --And these attributes don't have to be validated.
4835   IF (p_x_osp_order_line_rec.workorder_id IS NOT NULL) THEN
4836     OPEN get_wo_item_attrs(p_x_osp_order_line_rec.workorder_id);
4837     FETCH get_wo_item_attrs INTO p_x_osp_order_line_rec.inventory_item_id,
4838                                  p_x_osp_order_line_rec.inventory_org_id,
4839                                  p_x_osp_order_line_rec.lot_number,
4840                                  p_x_osp_order_line_rec.serial_number,
4841                                  p_x_osp_order_line_rec.inventory_item_quantity,
4842                                  p_x_osp_order_line_rec.inventory_item_uom,
4843                                  p_x_osp_order_line_rec.service_item_id,
4844                                  p_x_osp_order_line_rec.service_item_description,
4845                                  p_x_osp_order_line_rec.service_item_uom_code;
4846                                  --leave p_x_osp_order_line_rec.sub_inventory blank
4847     CLOSE get_wo_item_attrs;
4848   END IF;
4849 
4850   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
4851     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, G_LOG_PREFIX || 'create_osp_order_line',
4852                    'After line validation and before insert operation:'||
4853                    'inv_item_id='||p_x_osp_order_line_rec.inventory_item_id||' service_item_id='||p_x_osp_order_line_rec.service_item_id);
4854   END IF;
4855 
4856   --Default service item quantity and uom if there are null and service item not null, derive
4857   --service item description always as long as service_item_id is not null regardless what being passed (means
4858   --it could be overwrite if a non null value passed to service_item_description if service_item_id is not null)
4859 
4860 -- yazhou 06-Jul-2006 starts
4861 -- bug fix#5376907
4862 --  l_quantity := p_x_osp_order_line_rec.quantity; --here it means service_item quantity
4863 -- yazhou 06-Jul-2006 ends
4864 
4865   l_service_item_uom_code := p_x_osp_order_line_rec.service_item_uom_code;
4866   l_item_description := p_x_osp_order_line_rec.service_item_description;
4867   IF (p_x_osp_order_line_rec.service_item_id IS NOT NULL) THEN
4868     BEGIN
4869       --Assuming the org of service_item_id is the same as that of physical item
4870       --Alwasy set service_item_description to be derived from service_item_id if service_item_id is not null
4871       SELECT primary_uom_code, description,concatenated_segments, allow_item_desc_update_flag INTO
4872 			l_temp_uom_code, l_item_description, l_svc_item_number, l_desc_update_flag
4873         FROM mtl_system_items_kfv
4874        WHERE inventory_item_id = p_x_osp_order_line_rec.service_item_id
4875          AND organization_id = p_x_osp_order_line_rec.inventory_org_id;
4876     EXCEPTION
4877       WHEN NO_DATA_FOUND THEN
4878         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_SVC_ITEM');
4879         FND_MESSAGE.Set_Token('SERVICE_ITEM_ID', p_x_osp_order_line_rec.service_item_id);
4880         FND_MSG_PUB.ADD;
4881     END;
4882 
4883 -- yazhou 06-Jul-2006 starts
4884 -- bug fix#5376907
4885 
4886 --    IF (p_x_osp_order_line_rec.quantity IS NULL) THEN
4887 --      l_quantity := 1;
4888 --    END IF;
4889 -- yazhou 06-Jul-2006 ends
4890 
4891     IF (p_x_osp_order_line_rec.service_item_uom_code IS NULL) THEN
4892       l_service_item_uom_code := l_temp_uom_code;
4893     END IF;
4894     --Added by mpothuku on 24-Mar-06 for ER: 4544654
4895     /*IF Overwrite Svc Description profile set to No l_item_description would have been
4896       defaulted as the service item desc from inventory above, if the profile is Yes, we proceed below
4897       to override the value set from the inventory */
4898 
4899     l_owrite_svc_desc_prf := NVL(FND_PROFILE.VALUE('AHL_OSP_OWRITE_SVC_DESC'), 'N');
4900     l_item_prefix := FND_PROFILE.VALUE('AHL_OSP_POL_ITEM_PREFIX');
4901     l_serial_prefix := FND_PROFILE.VALUE('AHL_OSP_POL_SER_PREFIX');
4902 
4903     IF(l_owrite_svc_desc_prf = 'Y') THEN --Overwrite Svc Description profile set to Yes
4904       IF(NVL(l_desc_update_flag, 'N') = 'N') THEN --Allow Description update set to No
4905         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_SVC_DESC_NOCHNG');
4906         FND_MESSAGE.Set_Token('SERVICE_ITEM_NUMBER', l_svc_item_number);
4907         FND_MSG_PUB.ADD;
4908       ELSE
4909         BEGIN
4910           SELECT concatenated_segments INTO l_inv_item_number
4911             FROM mtl_system_items_kfv
4912            WHERE inventory_item_id = p_x_osp_order_line_rec.inventory_item_id
4913              AND organization_id = p_x_osp_order_line_rec.inventory_org_id;
4914         EXCEPTION
4915           WHEN NO_DATA_FOUND THEN
4916             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_PHY_ITEM_INVALID');
4917             FND_MSG_PUB.ADD;
4918         END;
4919         -- SALOGAN added an if condition to avoid adding trailing spaces for Non Serial Item
4920         -- for bug #9826606 Starts
4921         IF(p_x_osp_order_line_rec.serial_number IS NULL) THEN
4922           l_item_description :=
4923           TRIM(SUBSTR(l_item_prefix||l_inv_item_number,1,240));
4924         ELSE
4925           l_item_description :=
4926           TRIM(SUBSTR(l_item_prefix||l_inv_item_number||' '||l_serial_prefix||p_x_osp_order_line_rec.serial_number,1,240));
4927         END IF;
4928         -- SALOGAN changes for bug #9826606 Ends
4929       END IF;
4930     END IF;
4931       --mpothuku End
4932   END IF;
4933 
4934   --This is for deriving need_by_date from the profile option value and service_duration defined
4935   --at association between vendor and items combination
4936   BEGIN
4937     SELECT vendor_id, vendor_site_id
4938       INTO l_vendor_id, l_vendor_site_id
4939       FROM ahl_osp_orders_b
4940      WHERE osp_order_id = p_x_osp_order_line_rec.osp_order_id;
4941     IF (l_vendor_id IS NOT NULL AND l_vendor_site_id IS NOT NULL) THEN
4942       SELECT IV.service_duration INTO l_service_duration
4943         FROM ahl_inv_service_item_rels SI,
4944              ahl_item_vendor_rels IV,
4945              ahl_vendor_certifications_v VC
4946        WHERE SI.inv_item_id = p_x_osp_order_line_rec.inventory_item_id
4947          AND SI.inv_org_id = p_x_osp_order_line_rec.inventory_org_id
4948          AND SI.service_item_id = p_x_osp_order_line_rec.service_item_id
4949          AND VC.vendor_id = l_vendor_id
4950          AND VC.vendor_site_id = l_vendor_site_id
4951          AND SI.inv_service_item_rel_id = IV.inv_service_item_rel_id
4952          AND IV.vendor_certification_id = VC.vendor_certification_id
4953          AND trunc(SI.active_start_date) <= trunc(SYSDATE)
4954          AND trunc(nvl(SI.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
4955          AND trunc(IV.active_start_date) <= trunc(SYSDATE)
4956          AND trunc(nvl(IV.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
4957          AND trunc(VC.active_start_date) <= trunc(SYSDATE)
4958          AND trunc(nvl(VC.active_end_date, SYSDATE+1)) > trunc(SYSDATE);
4959      ELSE
4960        l_service_duration := FND_PROFILE.value('AHL_VENDOR_SERVICE_DURATION');
4961      END IF;
4962   EXCEPTION
4963     WHEN OTHERS THEN
4964       l_service_duration := FND_PROFILE.value('AHL_VENDOR_SERVICE_DURATION');
4965   END;
4966   --In case service_duration returned from the above query is null
4967   IF l_service_duration IS NULL THEN
4968     l_service_duration := FND_PROFILE.value('AHL_VENDOR_SERVICE_DURATION');
4969   END IF;
4970 
4971 -- yazhou 06-Jul-2006 starts
4972 -- bug fix#5376907
4973 -- Service item qty should be the same as physical item qty at the time of creation
4974 
4975   --Call table handler to insert the line
4976   AHL_OSP_ORDER_LINES_PKG.insert_row(
4977                p_x_osp_order_line_id => l_osp_line_id,
4978                p_object_version_number => 1,
4979                p_created_by => fnd_global.user_id,
4980                p_creation_date => SYSDATE,
4981                p_last_updated_by => fnd_global.user_id,
4982                p_last_update_date => SYSDATE,
4983                p_last_update_login => fnd_global.login_id,
4984                p_osp_order_id => p_x_osp_order_line_rec.osp_order_id,
4985                p_osp_line_number => l_osp_line_number,
4986                p_status_code => NULL, --Derived from header status when displaying
4987                p_po_line_type_id => to_number(FND_PROFILE.VALUE('AHL_OSP_PO_LINE_TYPE_ID')),
4988                p_service_item_id => p_x_osp_order_line_rec.service_item_id,
4989                p_service_item_description => l_item_description,
4990                p_service_item_uom_code => l_service_item_uom_code,
4991                p_need_by_date => TRUNC(SYSDATE+l_service_duration),
4992                p_ship_by_date => TRUNC(nvl(p_x_osp_order_line_rec.ship_by_date, SYSDATE)),
4993                p_po_line_id => NULL,
4994                -- by jaramana on January 10, 2008 to fix the Bug 5358438/5967633/5417460
4995                --p_po_line_id => p_x_osp_order_line_rec.po_line_id, --yazhou 28-jul-2006 bug#5417460
4996                p_oe_ship_line_id => NULL,
4997                p_oe_return_line_id => NULL,
4998                p_workorder_id => p_x_osp_order_line_rec.workorder_id,
4999                p_operation_id => NULL,
5000 --               p_quantity => l_quantity,
5001                p_quantity => p_x_osp_order_line_rec.inventory_item_quantity,
5002                p_exchange_instance_id => NULL,
5003                p_inventory_item_id => p_x_osp_order_line_rec.inventory_item_id,
5004                p_inventory_org_id => p_x_osp_order_line_rec.inventory_org_id,
5005                p_sub_inventory => p_x_osp_order_line_rec.sub_inventory,
5006                p_lot_number => p_x_osp_order_line_rec.lot_number,
5007                p_serial_number => p_x_osp_order_line_rec.serial_number,
5008                p_inventory_item_uom => p_x_osp_order_line_rec.inventory_item_uom,
5009                p_inventory_item_quantity => p_x_osp_order_line_rec.inventory_item_quantity,
5010                -- Added by jaramana on January 10, 2008 for the Requisition ER 6034236
5011                p_po_req_line_id => NULL,
5012                -- jaramana End
5013                p_attribute_category => p_x_osp_order_line_rec.attribute_category,
5014                p_attribute1 => p_x_osp_order_line_rec.attribute1,
5015                p_attribute2 => p_x_osp_order_line_rec.attribute2,
5016                p_attribute3 => p_x_osp_order_line_rec.attribute3,
5017                p_attribute4 => p_x_osp_order_line_rec.attribute4,
5018                p_attribute5 => p_x_osp_order_line_rec.attribute5,
5019                p_attribute6 => p_x_osp_order_line_rec.attribute6,
5020                p_attribute7 => p_x_osp_order_line_rec.attribute7,
5021                p_attribute8 => p_x_osp_order_line_rec.attribute8,
5022                p_attribute9 => p_x_osp_order_line_rec.attribute9,
5023                p_attribute10 => p_x_osp_order_line_rec.attribute10,
5024                p_attribute11 => p_x_osp_order_line_rec.attribute11,
5025                p_attribute12 => p_x_osp_order_line_rec.attribute12,
5026                p_attribute13 => p_x_osp_order_line_rec.attribute13,
5027                p_attribute14 => p_x_osp_order_line_rec.attribute14,
5028                p_attribute15 => p_x_osp_order_line_rec.attribute15
5029              );
5030 -- yazhou 06-Jul-2006 ends
5031   ELSE --IF(p_x_osp_order_line_rec.po_line_id is null) THEN,  which would mean po_line_id is passed
5032 
5033     --Purchasing allows past need_by_date as well, if so defaulting ship_by_date to the need_by_date
5034     --else initializing it to the sysdate
5035     --this field does not have much significance
5036     IF(trunc(p_x_osp_order_line_rec.need_by_date) <= trunc(sysdate)) THEN
5037       p_x_osp_order_line_rec.ship_by_date := p_x_osp_order_line_rec.need_by_date;
5038     ELSE
5039       p_x_osp_order_line_rec.ship_by_date := SYSDATE;
5040     END IF;
5041 
5042     --Call table handler to insert the line
5043     AHL_OSP_ORDER_LINES_PKG.insert_row(
5044                  p_x_osp_order_line_id => l_osp_line_id,
5045                  p_object_version_number => 1,
5046                  p_created_by => fnd_global.user_id,
5047                  p_creation_date => SYSDATE,
5048                  p_last_updated_by => fnd_global.user_id,
5049                  p_last_update_date => SYSDATE,
5050                  p_last_update_login => fnd_global.login_id,
5051                  p_osp_order_id => p_x_osp_order_line_rec.osp_order_id,
5052                  p_osp_line_number => l_osp_line_number,
5053                  p_status_code => NULL, --Derived from header status when displaying
5054                  p_po_line_type_id => p_x_osp_order_line_rec.po_line_type_id, --derived from PO Line
5055                  p_service_item_id => p_x_osp_order_line_rec.service_item_id, --derived from PO Line
5056                  p_service_item_description => p_x_osp_order_line_rec.service_item_description, --derived from PO Line
5057                  p_service_item_uom_code => p_x_osp_order_line_rec.service_item_uom_code, --derived from PO Line
5058                  p_need_by_date => p_x_osp_order_line_rec.need_by_date, --derived from PO Line
5059                  p_ship_by_date => p_x_osp_order_line_rec.ship_by_date,
5060                  p_po_line_id => p_x_osp_order_line_rec.po_line_id,
5061                  p_oe_ship_line_id => NULL,
5062                  p_oe_return_line_id => NULL,
5063                  p_workorder_id => p_x_osp_order_line_rec.workorder_id,
5064                  p_operation_id => NULL,
5065                  p_quantity => p_x_osp_order_line_rec.quantity, --derived from PO Line
5066                  p_exchange_instance_id => NULL,
5067                  p_inventory_item_id => p_x_osp_order_line_rec.inventory_item_id,
5068                  p_inventory_org_id => p_x_osp_order_line_rec.inventory_org_id,
5069                  p_sub_inventory => p_x_osp_order_line_rec.sub_inventory,
5070                  p_lot_number => p_x_osp_order_line_rec.lot_number,
5071                  p_serial_number => p_x_osp_order_line_rec.serial_number,
5072                  p_inventory_item_uom => p_x_osp_order_line_rec.inventory_item_uom,
5073                  p_inventory_item_quantity => p_x_osp_order_line_rec.inventory_item_quantity,
5074                  p_po_req_line_id => NULL,
5075                  p_attribute_category => p_x_osp_order_line_rec.attribute_category,
5076                  p_attribute1 => p_x_osp_order_line_rec.attribute1,
5077                  p_attribute2 => p_x_osp_order_line_rec.attribute2,
5078                  p_attribute3 => p_x_osp_order_line_rec.attribute3,
5079                  p_attribute4 => p_x_osp_order_line_rec.attribute4,
5080                  p_attribute5 => p_x_osp_order_line_rec.attribute5,
5081                  p_attribute6 => p_x_osp_order_line_rec.attribute6,
5082                  p_attribute7 => p_x_osp_order_line_rec.attribute7,
5083                  p_attribute8 => p_x_osp_order_line_rec.attribute8,
5084                  p_attribute9 => p_x_osp_order_line_rec.attribute9,
5085                  p_attribute10 => p_x_osp_order_line_rec.attribute10,
5086                  p_attribute11 => p_x_osp_order_line_rec.attribute11,
5087                  p_attribute12 => p_x_osp_order_line_rec.attribute12,
5088                  p_attribute13 => p_x_osp_order_line_rec.attribute13,
5089                  p_attribute14 => p_x_osp_order_line_rec.attribute14,
5090                  p_attribute15 => p_x_osp_order_line_rec.attribute15
5091                );
5092 
5093   END IF; --IF(p_x_osp_order_line_rec.po_line_id is null) THEN
5094   -- jaramana January 10, 2008 Bug 5358438/5967633/5417460 ends
5095 
5096   --Remember to return the line id created
5097   p_x_osp_order_line_rec.osp_order_line_id := l_osp_line_id;
5098   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5099     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, G_LOG_PREFIX || '.create_osp_order_line', 'After insert operation:');
5100   END IF;
5101 
5102 END create_osp_order_line;
5103 
5104 --yazhou 07-Aug-2006 starts
5105 -- bug fix#5448191
5106 
5107 PROCEDURE create_shipment(p_osp_order_lines_tbl IN OUT NOCOPY osp_order_lines_tbl_type)
5108 --yazhou 07-Aug-2006 ends
5109 
5110 IS
5111   l_oe_header_id       NUMBER;
5112   l_oe_header_rec      AHL_OSP_SHIPMENT_PUB.ship_header_rec_type;
5113   l_oe_lines_tbl       AHL_OSP_SHIPMENT_PUB.ship_line_tbl_type;
5114   l_return_status      VARCHAR2(1);
5115   l_msg_count          NUMBER;
5116   l_msg_data           VARCHAR2(2000);
5117   l_create_shipment    BOOLEAN;
5118   l_osp_order_id       NUMBER;
5119   i                    NUMBER;
5120   j                    NUMBER;
5121   k                    NUMBER;
5122   l_create_ship_line   BOOLEAN;
5123   CURSOR check_ship_line_exists(c_inventory_item_id NUMBER,
5124                                 c_inventory_org_id NUMBER,
5125                                 c_serial_number VARCHAR2,
5126                                 c_osp_order_id NUMBER) IS
5127     SELECT 'X'
5128       FROM ahl_osp_order_lines
5129      WHERE inventory_item_id = c_inventory_item_id
5130        AND inventory_org_id = c_inventory_org_id
5131        AND serial_number = c_serial_number
5132        AND osp_order_id = c_osp_order_id
5133        AND (oe_ship_line_id IS NOT NULL OR oe_return_line_id IS NOT NULL);
5134 
5135 BEGIN
5136   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5137     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
5138                    G_LOG_PREFIX||'create_shipment','Begin');
5139   END IF;
5140   l_create_shipment := FALSE;
5141   IF p_osp_order_lines_tbl.count > 0 THEN
5142     FOR i IN p_osp_order_lines_tbl.FIRST..p_osp_order_lines_tbl.LAST LOOP
5143 
5144 -- yazhou 07-Aug-2006 starts
5145 -- bug fix#5448191
5146       IF p_osp_order_lines_tbl(i).operation_flag = G_OP_CREATE THEN
5147 
5148         IF p_osp_order_lines_tbl(i).shipment_creation_flag = G_YES_FLAG THEN
5149            IF NOT l_create_shipment THEN
5150              l_create_shipment := TRUE;
5151              l_osp_order_id := p_osp_order_lines_tbl(i).osp_order_id;
5152            END IF;
5153         ELSE
5154            -- turn on create shipment flag if shipments for the same physical item
5155            -- already exist, so that shipment can be associated to new OSP order line
5156            -- in AHL_OSP_SHIPMENT_PUB.process_order
5157            OPEN check_ship_line_exists(p_osp_order_lines_tbl(i).inventory_item_id,
5158                                         p_osp_order_lines_tbl(i).inventory_org_id,
5159                                         p_osp_order_lines_tbl(i).serial_number,
5160                                         p_osp_order_lines_tbl(i).osp_order_id);
5161             FETCH check_ship_line_exists INTO g_dummy_char;
5162             IF check_ship_line_exists%FOUND THEN
5163               p_osp_order_lines_tbl(i).shipment_creation_flag := G_YES_FLAG;
5164               IF NOT l_create_shipment THEN
5165                  l_create_shipment := TRUE;
5166                  l_osp_order_id := p_osp_order_lines_tbl(i).osp_order_id;
5167               END IF;
5168             END IF;
5169 
5170             CLOSE check_ship_line_exists;
5171 
5172         END IF;
5173 
5174         --EXIT;
5175       END IF;
5176 
5177 -- yazhou 07-Aug-2006 ends
5178 
5179     END LOOP;
5180     --For safety purpose, just check whether shipment header already exists
5181     --This is probably not necessary
5182     IF l_create_shipment THEN
5183       BEGIN
5184         SELECT oe_header_id INTO l_oe_header_id
5185           FROM ahl_osp_orders_b
5186          WHERE osp_order_id = l_osp_order_id;
5187       EXCEPTION
5188         WHEN NO_DATA_FOUND THEN
5189           FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_INVALID');
5190           FND_MSG_PUB.ADD;
5191           RAISE FND_API.G_EXC_ERROR;
5192       END;
5193       l_oe_header_rec.header_id := l_oe_header_id;
5194       l_oe_header_rec.osp_order_id := l_osp_order_id;
5195       IF l_oe_header_id IS NULL THEN
5196       --No shipment header/lines exist yet
5197         l_oe_header_rec.operation := G_OP_CREATE;
5198       ELSE
5199       --Already has shipment header, but no change to shipment header itself and
5200       --just adding new shipment lines to the existing shipment header
5201       --And try to make it the same as front end calling Shipment API for which
5202       --Shipment header as a whole is passed as null
5203         l_oe_header_rec.operation := NULL;
5204         l_oe_header_rec.header_id := NULL;
5205 
5206 --yazhou 03-Aug-2006 starts
5207 -- bug fix#5442904
5208 -- osp_order_id is used in AHL_OSP_SHIPMENT_PUB.Process_Line_Tbl to fetch vendor info
5209 
5210 --        l_oe_header_rec.osp_order_id := NULL;
5211 
5212 --yazhou 03-Aug-2006 ends
5213 
5214       END IF;
5215       k := 1;
5216       FOR i IN p_osp_order_lines_tbl.FIRST..p_osp_order_lines_tbl.LAST LOOP
5217         IF (p_osp_order_lines_tbl(i).operation_flag = G_OP_CREATE AND
5218             p_osp_order_lines_tbl(i).shipment_creation_flag = G_YES_FLAG) THEN
5219           --Jerry added on 10/19/2005
5220           --Adding the following logic to check whether really needs to create shipment
5221           --line because for the same tracked item instance with different service time
5222           --combination, we still need to create one set of shipment lines. This is
5223           --feature is added for fixing bug 4571305
5224 
5225           l_create_ship_line := TRUE;
5226           IF l_oe_header_id IS NOT NULL THEN
5227             OPEN check_ship_line_exists(p_osp_order_lines_tbl(i).inventory_item_id,
5228                                         p_osp_order_lines_tbl(i).inventory_org_id,
5229                                         p_osp_order_lines_tbl(i).serial_number,
5230                                         p_osp_order_lines_tbl(i).osp_order_id);
5231             FETCH check_ship_line_exists INTO g_dummy_char;
5232             IF check_ship_line_exists%FOUND THEN
5233               l_create_ship_line := FALSE;
5234             ELSIF i > p_osp_order_lines_tbl.FIRST THEN
5235               FOR j IN p_osp_order_lines_tbl.FIRST .. i-1 LOOP
5236                 IF (p_osp_order_lines_tbl(i).inventory_item_id = p_osp_order_lines_tbl(j).inventory_item_id AND
5237                     p_osp_order_lines_tbl(i).inventory_org_id = p_osp_order_lines_tbl(j).inventory_org_id AND
5238                     p_osp_order_lines_tbl(i).serial_number = p_osp_order_lines_tbl(j).serial_number AND
5239                     p_osp_order_lines_tbl(i).osp_order_id = p_osp_order_lines_tbl(j).osp_order_id) THEN
5240                   l_create_ship_line := FALSE;
5241                   EXIT;
5242                 END IF;
5243               END LOOP;
5244             END IF;
5245             CLOSE check_ship_line_exists;
5246           ELSIF (l_oe_header_id IS NULL AND i > p_osp_order_lines_tbl.FIRST) THEN
5247             FOR j IN p_osp_order_lines_tbl.FIRST .. i-1 LOOP
5248               IF (p_osp_order_lines_tbl(i).inventory_item_id = p_osp_order_lines_tbl(j).inventory_item_id AND
5249                   p_osp_order_lines_tbl(i).inventory_org_id = p_osp_order_lines_tbl(j).inventory_org_id AND
5250                   p_osp_order_lines_tbl(i).serial_number = p_osp_order_lines_tbl(j).serial_number AND
5251                   p_osp_order_lines_tbl(i).osp_order_id = p_osp_order_lines_tbl(j).osp_order_id) THEN
5252                   l_create_ship_line := FALSE;
5253                 EXIT;
5254               END IF;
5255             END LOOP;
5256           END IF;
5257 
5258           IF l_create_ship_line THEN
5259             l_oe_lines_tbl(k).header_id := l_oe_header_id;
5260             l_oe_lines_tbl(k).line_type_id := FND_PROFILE.VALUE('AHL_OSP_OE_SHIP_ONLY_ID');
5261             l_oe_lines_tbl(k).osp_order_id := p_osp_order_lines_tbl(i).osp_order_id;
5262             l_oe_lines_tbl(k).osp_line_id := p_osp_order_lines_tbl(i).osp_order_line_id;
5263             l_oe_lines_tbl(k).ship_from_org_id := p_osp_order_lines_tbl(i).inventory_org_id;
5264             /* The following will be derived from Shipment API
5265             l_oe_lines_tbl(k).inventory_item_id := p_osp_order_lines_tbl(i).inventory_item_id;
5266             l_oe_lines_tbl(k).inventory_org_id := p_osp_order_lines_tbl(i).inventory_org_id;
5267             l_oe_lines_tbl(k).subinventory := p_osp_order_lines_tbl(i).sub_inventory;
5268             l_oe_lines_tbl(k).serial_number := p_osp_order_lines_tbl(i).serial_number;
5269             l_oe_lines_tbl(k).lot_number := p_osp_order_lines_tbl(i).lot_number;
5270             */
5271             l_oe_lines_tbl(k).osp_line_flag := G_YES_FLAG;
5272             l_oe_lines_tbl(k).operation := G_OP_CREATE;
5273 
5274             l_oe_lines_tbl(k+1).header_id := l_oe_header_id;
5275             l_oe_lines_tbl(k+1).line_type_id := FND_PROFILE.VALUE('AHL_OSP_OE_RETURN_ID');
5276             l_oe_lines_tbl(k+1).osp_order_id := p_osp_order_lines_tbl(i).osp_order_id;
5277             l_oe_lines_tbl(k+1).osp_line_id := p_osp_order_lines_tbl(i).osp_order_line_id;
5278             l_oe_lines_tbl(k+1).ship_from_org_id := p_osp_order_lines_tbl(i).inventory_org_id;
5279             /* The following will be derived from Shipment API
5280             l_oe_lines_tbl(k+1).inventory_item_id := p_osp_order_lines_tbl(i).inventory_item_id;
5281             l_oe_lines_tbl(k+1).inventory_org_id := p_osp_order_lines_tbl(i).inventory_org_id;
5282             l_oe_lines_tbl(k+1).subinventory := p_osp_order_lines_tbl(i).sub_inventory;
5283             l_oe_lines_tbl(k+1).serial_number := p_osp_order_lines_tbl(i).serial_number;
5284             l_oe_lines_tbl(k+1).lot_number := p_osp_order_lines_tbl(i).lot_number;
5285             */
5286             l_oe_lines_tbl(k+1).osp_line_flag := G_YES_FLAG;
5287             l_oe_lines_tbl(k+1).operation := G_OP_CREATE;
5288             --other attributes to populate
5289             k := k+2;
5290           END IF;
5291         END IF;
5292       END LOOP;
5293       IF (l_oe_lines_tbl.COUNT > 0 AND FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5294         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
5295                    G_LOG_PREFIX||'create_shipment','Before calling Shipment API: process_order and the parameters are'||
5296                    'oe_header='||l_oe_header_rec.header_id||' header_oper_flag='||l_oe_header_rec.operation||
5297                    'oe_line1_osp_order_id='||l_oe_lines_tbl(1).osp_order_id||'oe_line2_osp_line_id='||l_oe_lines_tbl(1).osp_line_id||
5298                    'oe_line_operation='||l_oe_lines_tbl(1).operation);
5299       END IF;
5300       AHL_OSP_SHIPMENT_PUB.process_order(
5301               p_api_version      => 1.0,
5302               p_init_msg_list    => FND_API.G_FALSE,
5303               p_commit           => FND_API.G_FALSE,
5304               p_validation_level => FND_API.G_VALID_LEVEL_FULL,
5305               --Changed by mpothuku on 14-Dec-05 to differentiate the call from here and from
5306               --Shipment Line Details UI. Note that Shipment Line Details will be using OAF as the p_module_type
5307               p_module_type      => NULL,--G_OAF_MODULE,
5308               p_x_header_rec     => l_oe_header_rec,
5309               p_x_lines_tbl 	 => l_oe_lines_tbl,
5310               x_return_status    => l_return_status,
5311               x_msg_count        => l_msg_count,
5312               x_msg_data         => l_msg_data);
5313     END IF;
5314     IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
5315       RAISE FND_API.G_EXC_ERROR;
5316     ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
5317       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5318     END IF;
5319   END IF;
5320 
5321   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5322     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
5323                    G_LOG_PREFIX||'create_shipment',
5324                    'End: after calling shipment creation API and x_return_status='||l_return_status);
5325   END IF;
5326 END create_shipment;
5327 
5328 PROCEDURE validate_order_line_creation(p_x_osp_order_line_rec IN OUT NOCOPY osp_order_line_rec_type)
5329 IS
5330   CURSOR check_physical_item(c_inventory_item_id NUMBER, c_inventory_org_id NUMBER) IS
5331     SELECT organization_id,
5332            inventory_item_id,
5333            concatenated_segments,
5334            primary_uom_code,
5335            serial_number_control_code,
5336            lot_control_code,
5337            comms_nl_trackable_flag
5338       FROM mtl_system_items_kfv
5339      WHERE inventory_item_id = c_inventory_item_id
5340        AND organization_id = c_inventory_org_id;
5341   l_check_physical_item check_physical_item%ROWTYPE;
5342   CURSOR check_sub_inventory(c_organization_id NUMBER, c_sub_inventory VARCHAR2) IS
5343     SELECT 'X'
5344       FROM mtl_secondary_inventories
5345      WHERE organization_id = c_organization_id
5346        AND secondary_inventory_name = c_sub_inventory;
5347   CURSOR check_serial_number (c_inv_item_id NUMBER, c_serial_number VARCHAR2) IS
5348     SELECT 'X'
5349       FROM mtl_serial_numbers
5350      WHERE inventory_item_id = c_inv_item_id
5351        AND serial_number = c_serial_number;
5352   CURSOR check_lot_number (c_inv_item_id NUMBER, c_org_id NUMBER, c_lot_number VARCHAR2) IS
5353     SELECT 'X'
5354       FROM mtl_lot_numbers
5355      WHERE inventory_item_id = c_inv_item_id
5356        AND organization_id = c_org_id
5357        AND lot_number = c_lot_number;
5358 
5359   -- Modified by mpothuku on 27-Feb-06 to fix the Perf Bug #4919164 and also added subinventory to
5360   --calculate the onhand quantity correctly.
5361   CURSOR get_onhand_quantity(c_inv_item_id NUMBER, c_inv_org_id NUMBER, c_subinv VARCHAR2, c_lot_number VARCHAR2) IS
5362 	--Added by mpothuku on 23rd Aug, 06 to fix the Bug 5252627
5363     SELECT ahl_osp_queries_pvt.get_onhand_quantity(c_inv_org_id, c_subinv, c_inv_item_id, c_lot_number)  onhand_quantity,        primary_uom_code uom_code
5364       FROM mtl_system_items_b
5365      WHERE inventory_item_id = c_inv_item_id
5366        AND organization_id = c_inv_org_id;
5367   CURSOR check_phy_ser_item_unique(c_osp_order_id NUMBER,
5368                                    c_service_item_id NUMBER,
5369                                    c_inv_item_id NUMBER,
5370                                    c_inv_org_id NUMBER,
5371                                    c_lot_number VARCHAR2,
5372                                    c_serial_number VARCHAR2) IS
5373     SELECT 'X'
5374       FROM ahl_osp_order_lines
5375      WHERE osp_order_id = c_osp_order_id
5376        AND ((service_item_id = c_service_item_id) OR
5377             (service_item_id IS NULL AND c_service_item_id IS NULL))
5378        AND inventory_item_id = c_inv_item_id
5379        AND inventory_org_id = c_inv_org_id
5380        AND ((lot_number IS NULL AND c_lot_number IS NULL) OR (lot_number = c_lot_number))
5381        AND ((serial_number IS NULL AND c_serial_number IS NULL) OR (serial_number = c_serial_number));
5382   CURSOR check_phy_item_unique(c_osp_order_id NUMBER, c_inv_item_id NUMBER) IS
5383     SELECT 'X'
5384       FROM ahl_osp_order_lines
5385      WHERE inventory_item_id = c_inv_item_id
5386        AND osp_order_id = c_osp_order_id;
5387 
5388 --yazhou 22-Aug-2006 starts
5389 -- Bug fix#5479266
5390 
5391 /*
5392   CURSOR check_osp_order_unique(c_osp_order_id NUMBER, c_inventory_item_id NUMBER,
5393                                 c_inventory_org_id NUMBER, c_serial_number VARCHAR2) IS
5394    SELECT 'X'
5395      FROM ahl_osp_order_lines ospl, ahl_osp_orders_b osp
5396     WHERE ospl.osp_order_id = osp.osp_order_id
5397       AND osp.status_code <> 'CLOSED'
5398       AND ospl.status_code is null
5399       AND ospl.inventory_item_id = c_inventory_item_id
5400       AND ospl.inventory_org_id = c_inventory_org_id
5401       AND NVL (ospl.serial_number, 'XXX') = NVL (c_serial_number, 'XXX')
5402       AND osp.osp_order_id <> c_osp_order_id;
5403 */
5404 -- Added by jaramana on January 10, 2008 for the Bug 5547870/5673279
5405   CURSOR check_osp_order_unique(c_osp_order_id NUMBER, c_inventory_item_id NUMBER,
5406                                 c_inventory_org_id NUMBER, c_serial_number VARCHAR2) IS
5407    SELECT 'X'
5408      FROM ahl_osp_order_lines ospl, ahl_osp_orders_b osp, oe_order_lines_all oelship
5409     WHERE ospl.osp_order_id = osp.osp_order_id
5410       AND osp.status_code <> 'CLOSED'
5411       AND oelship.LINE_ID = ospl.OE_SHIP_LINE_ID
5412       --The order line should not be closed and should not be cancelled to be considered in the uniqueness check
5413       AND (nvl(oelship.cancelled_flag, 'N') <> 'Y' OR nvl(oelship.flow_status_code, 'XXX') <> 'CANCELLED')
5414       AND (oelship.open_flag <> 'N' OR nvl(oelship.flow_status_code, 'XXX') <> 'CLOSED')
5415       AND ospl.inventory_item_id = c_inventory_item_id
5416       AND ospl.inventory_org_id = c_inventory_org_id
5417       AND NVL (ospl.serial_number, 'XXX') = NVL (c_serial_number, 'XXX')
5418       AND osp.osp_order_id <> c_osp_order_id;
5419 
5420   -- Added by jaramana on January 10, 2008 for the Bug 5358438/5967633/5417460
5421   CURSOR get_wo_item_attrs(c_workorder_id NUMBER) IS
5422     SELECT vts.inventory_item_id,
5423            vst.organization_id,
5424            csii.lot_number,
5425            csii.serial_number,
5426            csii.quantity,
5427            csii.unit_of_measure item_instance_uom,
5428            arb.service_item_id,
5429            mtls.description service_item_description,
5430            mtls.primary_uom_code service_item_uom
5431       FROM ahl_workorders wo,
5432            ahl_visits_b vst,
5433            ahl_visit_tasks_b vts,
5434            csi_item_instances csii,
5435            mtl_system_items_kfv mtls,
5436            ahl_routes_b arb
5437      WHERE workorder_id = c_workorder_id
5438        AND wo.visit_task_id = vts.visit_task_id
5439        AND vst.visit_id = vts.visit_id
5440        AND wo.route_id = arb.route_id(+)
5441        AND arb.service_item_org_id = mtls.organization_id(+)
5442        AND arb.service_item_id = mtls.inventory_item_id(+)
5443        AND vts.instance_id = csii.instance_id;
5444 
5445   -- Added by jaramana on January 10, 2008 for the Bug 5358438/5967633/5417460
5446   --Get the PO Line attributes and the need_by_date from the first record of po_line_locations_all
5447   CURSOR get_po_line_attrs(c_po_line_id NUMBER) IS
5448     SELECT pol.line_num,
5449            pol.item_id,
5450            pol.item_description,
5451            pol.line_type_id,
5452            uom.uom_code,
5453            pol.quantity,
5454            (select min(need_by_date)
5455               from po_line_locations_all
5456              where po_line_id = pol.po_line_id
5457                and po_header_id = pol.po_header_id) need_by_date
5458       FROM po_lines_all pol,
5459            mtl_units_of_measure_vl uom
5460      WHERE pol.po_line_id = c_po_line_id
5461        AND uom.unit_of_measure = pol.unit_meas_lookup_code;
5462   -- jaramana January 10, 2008 Ends
5463 --yazhou 22-Aug-2006 ends
5464 
5465   l_vendor_id         NUMBER;
5466   l_vendor_site_id    NUMBER;
5467   l_vendor_contact_id NUMBER;
5468   l_quantity          NUMBER;
5469   l_onhand_quantity   NUMBER;
5470   l_uom_code          VARCHAR2(3);
5471   l_po_line_attrs     get_po_line_attrs%ROWTYPE;
5472   L_DEBUG_KEY         CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.validate_order_line_creation';
5473 
5474 BEGIN
5475   --Suppose all value id conversion has been done.
5476   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5477     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, G_LOG_PREFIX || '.validate_order_line_creation', 'Begin:');
5478   END IF;
5479   --For OAF, we decided to pass null when you want to change it to null, the old value if there is no
5480   --change.
5481   IF nvl(g_module_type, 'NULL') <> G_OAF_MODULE THEN
5482     default_unchanged_order_line(p_x_osp_order_line_rec);
5483   END IF;
5484 
5485   --And need to adde value to id conversion here
5486   convert_order_line_val_to_id(p_x_osp_order_line_rec);
5487   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5488     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, G_LOG_PREFIX || 'create_osp_order_line',
5489     'osp_order_id='||p_x_osp_order_line_rec.osp_order_id);
5490   END IF;
5491   --Validate osp_order_id
5492   BEGIN
5493 
5494 -- yazhou 28-jul-2006 starts
5495 -- bug#5417460
5496    IF p_x_osp_order_line_rec.po_line_id is not null and p_x_osp_order_line_rec.workorder_id is not null then
5497     SELECT osp_order_id INTO g_dummy_num
5498       FROM ahl_osp_orders_b
5499      WHERE osp_order_id = p_x_osp_order_line_rec.osp_order_id
5500        AND status_code = G_OSP_PO_CREATED_STATUS
5501        -- Added by jaramana on January 10, 2008 for the Bug 5358438/5967633/5417460
5502        --Pos are not applying to the other order types.
5503        AND order_type_code IN (G_OSP_ORDER_TYPE_SERVICE, G_OSP_ORDER_TYPE_EXCHANGE)
5504        -- Added by jaramana on January 10, 2008 for the Requisition ER 6034236
5505        --If we are creating an osp order line based on PO line that is created from the Purchasing forms, then
5506        --the work order selected for the order Line, should not be associated with any open order lines
5507        --Note that instead of status_code not in, we could have use status_code is null as well.
5508        AND not exists (select 1
5509                          from ahl_osp_order_lines
5510                          where workorder_id =p_x_osp_order_line_rec.workorder_id
5511                            and nvl(status_code, 'X') not in (G_OL_PO_CANCELLED_STATUS, G_OL_PO_DELETED_STATUS, G_OL_REQ_CANCELLED_STATUS, G_OL_REQ_DELETED_STATUS));
5512        -- jaramana End
5513    ELSE
5514     SELECT osp_order_id INTO g_dummy_num
5515       FROM ahl_osp_orders_b
5516      WHERE osp_order_id = p_x_osp_order_line_rec.osp_order_id
5517        -- jaramana modified on January 10, 2008 for the Requisition ER 6034236 (Added G_OSP_REQ_SUB_FAILED_STATUS)
5518        AND status_code IN (G_OSP_ENTERED_STATUS, G_OSP_SUB_FAILED_STATUS, G_OSP_REQ_SUB_FAILED_STATUS);
5519        -- jaramana End
5520    END IF;
5521 -- yazhou 28-jul-2006 ends
5522 
5523   EXCEPTION
5524     WHEN NO_DATA_FOUND THEN
5525       FND_MESSAGE.set_name('AHL', 'AHL_OSP_ORD_INVALID');
5526       FND_MSG_PUB.add;
5527   END;
5528 
5529   -- Added by jaramana on January 10, 2008 for the Requisition ER 6034236 (partial fix for the Bug 5358438/5967633/5417460)
5530   --po_line_id should be passed only along with the work_order_id
5531   --value to id conversion should already have taken place, so even if the user passes job_number,
5532   --we would have derived the workorder_id
5533   IF p_x_osp_order_line_rec.po_line_id is not null and p_x_osp_order_line_rec.workorder_id is not null THEN
5534     validate_po_line(p_x_osp_order_line_rec.po_line_id, p_x_osp_order_line_rec.osp_order_id);
5535   ELSIF (p_x_osp_order_line_rec.po_line_id is not null and p_x_osp_order_line_rec.workorder_id is null) THEN
5536     FND_MESSAGE.set_name('AHL', 'AHL_OSP_WO_NLL_CR_POL');
5537     FND_MSG_PUB.add;
5538   END IF;
5539   -- jaramna End
5540 
5541   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5542     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, G_LOG_PREFIX || '.validate_order_line_creation', 'item='||p_x_osp_order_line_rec.inventory_item_id||'org='||p_x_osp_order_line_rec.inventory_org_id);
5543   END IF;
5544   IF (p_x_osp_order_line_rec.workorder_id IS NULL AND p_x_osp_order_line_rec.inventory_item_id IS NULL) THEN
5545     FND_MESSAGE.set_name('AHL', 'AHL_OSP_WO_ITEM_ALL_NULL');
5546     FND_MSG_PUB.add;
5547   --Validate workorder_id (borrow the old code)
5548   ELSIF(p_x_osp_order_line_rec.workorder_id IS NOT NULL) THEN
5549     validate_workorder(p_x_osp_order_line_rec.workorder_id);
5550   --Validate physical item
5551   ELSIF (p_x_osp_order_line_rec.inventory_item_id IS NOT NULL) THEN
5552     IF (p_x_osp_order_line_rec.inventory_org_id IS NULL) THEN
5553       FND_MESSAGE.set_name('AHL', 'AHL_OSP_ITEM_ORG_NULL');
5554       FND_MSG_PUB.add;
5555     END IF;
5556     OPEN check_physical_item(p_x_osp_order_line_rec.inventory_item_id,
5557                              p_x_osp_order_line_rec.inventory_org_id);
5558     FETCH check_physical_item INTO l_check_physical_item;
5559     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5560       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, G_LOG_PREFIX || '.validate_order_line_creation', 'track_flag='||l_check_physical_item.comms_nl_trackable_flag);
5561     END IF;
5562     IF check_physical_item%NOTFOUND THEN
5563       FND_MESSAGE.set_name('AHL', 'AHL_OSP_PHY_ITEM_INVALID');
5564       FND_MSG_PUB.add;
5565     ELSE
5566       IF (p_x_osp_order_line_rec.sub_inventory IS NOT NULL) THEN
5567         OPEN check_sub_inventory(p_x_osp_order_line_rec.inventory_org_id,
5568                                  p_x_osp_order_line_rec.sub_inventory);
5569         FETCH check_sub_inventory INTO g_dummy_char;
5570         IF check_sub_inventory%NOTFOUND THEN
5571           FND_MESSAGE.set_name('AHL', 'AHL_OSP_SUB_INV_INVALID');
5572           FND_MSG_PUB.add;
5573         END IF;
5574         CLOSE check_sub_inventory;
5575       END IF;
5576 
5577       --Non tracked physical item's quantity (to be serviced) is manadatory
5578       IF (nvl(l_check_physical_item.comms_nl_trackable_flag, 'N')='N' AND
5579           (p_x_osp_order_line_rec.inventory_item_quantity IS NULL)) THEN
5580         FND_MESSAGE.set_name('AHL', 'AHL_OSP_PHY_ITEM_QTY_REQUIRED');
5581         FND_MSG_PUB.add;
5582       END IF;
5583 
5584 	  /*
5585 	  Comment Added by mpothuku on 27-Feb-06: May need to revise this logic
5586 	  as the checks below will not cater to serial and lot controlled items
5587 	  */
5588       IF l_check_physical_item.serial_number_control_code IN (2, 5, 6) THEN
5589         OPEN check_serial_number(p_x_osp_order_line_rec.inventory_item_id,
5590                                  p_x_osp_order_line_rec.serial_number);
5591         FETCH check_serial_number INTO g_dummy_char;
5592         IF check_serial_number%NOTFOUND THEN
5593           FND_MESSAGE.set_name('AHL', 'AHL_OSP_PHY_ITEM_SN_INVALID');
5594           FND_MSG_PUB.add;
5595         ELSIF p_x_osp_order_line_rec.inventory_item_quantity <> 1 THEN
5596           FND_MESSAGE.set_name('AHL', 'AHL_OSP_PHY_ITEM_QT_INVALID');
5597           FND_MSG_PUB.add;
5598         END IF;
5599         CLOSE check_serial_number;
5600       ELSIF l_check_physical_item.lot_control_code = 2 THEN
5601         OPEN check_lot_number(p_x_osp_order_line_rec.inventory_item_id,
5602                               p_x_osp_order_line_rec.inventory_org_id,
5603                               p_x_osp_order_line_rec.lot_number);
5604         FETCH check_lot_number INTO g_dummy_char;
5605         IF check_lot_number%NOTFOUND THEN
5606           FND_MESSAGE.set_name('AHL', 'AHL_OSP_PHY_ITEM_LOT_INVALID');
5607           FND_MSG_PUB.add;
5608         END IF;
5609         CLOSE check_lot_number;
5610       --Validate the onhand quantity with the quantity to be serviced
5611       --Subinventory_code added by mpothuku on 27-Feb-06 to fix the Perf bug #4919164
5612       ELSIF nvl(l_check_physical_item.comms_nl_trackable_flag, 'N')='N' THEN
5613         OPEN get_onhand_quantity(p_x_osp_order_line_rec.inventory_item_id,
5614                                  p_x_osp_order_line_rec.inventory_org_id,
5615                                  p_x_osp_order_line_rec.sub_inventory,
5616                                  --Added by mpothuku on 23rd Aug, 06 to fix the Bug 5252627
5617                                  p_x_osp_order_line_rec.lot_number);
5618         FETCH get_onhand_quantity INTO l_onhand_quantity, l_uom_code;
5619         IF get_onhand_quantity%NOTFOUND THEN
5620           FND_MESSAGE.set_name('AHL', 'AHL_OSP_PHY_ITEM_INVALID');
5621           FND_MSG_PUB.add;
5622         END IF;
5623         CLOSE get_onhand_quantity;
5624         IF (l_onhand_quantity IS NOT NULL AND l_uom_code IS NOT NULL) THEN
5625           --l_uom_code is primary_uom_code
5626           --convert the inventory_item_uom to l_uom_code
5627           IF (p_x_osp_order_line_rec.inventory_item_quantity IS NOT NULL AND
5628               p_x_osp_order_line_rec.inventory_item_uom IS NOT NULL) THEN
5629             l_quantity := inv_convert.inv_um_convert(
5630                                item_id        => p_x_osp_order_line_rec.inventory_item_id,
5631                                precision      => 6,
5632                                from_quantity  => p_x_osp_order_line_rec.inventory_item_quantity,
5633                                from_unit      => p_x_osp_order_line_rec.inventory_item_uom,
5634                                to_unit        => l_uom_code,
5635                                from_name      => NULL,
5636                                to_name        => NULL);
5637             IF (l_quantity > l_onhand_quantity) THEN
5638               FND_MESSAGE.set_name('AHL', 'AHL_OSP_ITEM_ONHAND_LESS_SERV');
5639               FND_MSG_PUB.add;
5640             END IF;
5641           END IF;
5642         END IF;
5643       END IF;
5644     END IF;
5645     CLOSE check_physical_item;
5646   END IF;
5647 
5648   -- Added by jaramana on January 10, 2008 for the Bug 5358438/5967633/5417460
5649   /*
5650   1. Retrieve the ITEM_ID, ITEM_DESCRIPTION, UNIT_MEAS_LOOKUP_CODE, QUANTITY, LINE_TYPE_ID
5651      from PO_LINES_All corresponding to the PO_LINE_ID passed
5652   2. ITEM_ID and ITEM_DESCRIPTION cannot both be null
5653   3. If ITEM_ID is not null, validate it agains the work order's service_item_id
5654   6. UNIT_MEAS_LOOKUP_CODE, QUANTITY, LINE_TYPE_ID, NEED_BY_DATE are mandatory
5655   */
5656 
5657   --We already added the check that if the po_line_id is not null, the workorder_id also should not be null
5658   IF(p_x_osp_order_line_rec.po_line_id is NOT NULL) THEN
5659     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5660       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'p_x_osp_order_line_rec.po_line_id='||p_x_osp_order_line_rec.po_line_id);
5661       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'p_x_osp_order_line_rec.workorder_id='||p_x_osp_order_line_rec.workorder_id);
5662     END IF;
5663 
5664     OPEN get_wo_item_attrs(p_x_osp_order_line_rec.workorder_id);
5665     FETCH get_wo_item_attrs INTO p_x_osp_order_line_rec.inventory_item_id,
5666                                  p_x_osp_order_line_rec.inventory_org_id,
5667                                  p_x_osp_order_line_rec.lot_number,
5668                                  p_x_osp_order_line_rec.serial_number,
5669                                  p_x_osp_order_line_rec.inventory_item_quantity,
5670                                  p_x_osp_order_line_rec.inventory_item_uom,
5671                                  p_x_osp_order_line_rec.service_item_id,
5672                                  p_x_osp_order_line_rec.service_item_description,
5673                                  p_x_osp_order_line_rec.service_item_uom_code;
5674 
5675     CLOSE get_wo_item_attrs;
5676 
5677 
5678     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5679       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'p_x_osp_order_line_rec.service_item_id ='||p_x_osp_order_line_rec.service_item_id);
5680       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'p_x_osp_order_line_rec.service_item_description ='||p_x_osp_order_line_rec.service_item_description);
5681       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'p_x_osp_order_line_rec.service_item_uom_code ='||p_x_osp_order_line_rec.service_item_uom_code);
5682     END IF;
5683 
5684     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5685       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'p_x_osp_order_line_rec.po_line_id='||p_x_osp_order_line_rec.po_line_id);
5686     END IF;
5687 
5688     OPEN get_po_line_attrs(p_x_osp_order_line_rec.po_line_id);
5689     FETCH get_po_line_attrs INTO l_po_line_attrs.line_num,
5690                                  l_po_line_attrs.item_id,
5691                                  l_po_line_attrs.item_description,
5692                                  l_po_line_attrs.line_type_id,
5693                                  l_po_line_attrs.uom_code,
5694                                  l_po_line_attrs.quantity,
5695                                  l_po_line_attrs.need_by_date;
5696     CLOSE get_po_line_attrs;
5697 
5698     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5699       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_po_line_attrs.line_num ='||l_po_line_attrs.line_num);
5700       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_po_line_attrs.item_id ='||l_po_line_attrs.item_id);
5701       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_po_line_attrs.item_description ='||l_po_line_attrs.item_description);
5702       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_po_line_attrs.line_type_id ='||l_po_line_attrs.line_type_id);
5703       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_po_line_attrs.uom_code ='||l_po_line_attrs.uom_code);
5704       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_po_line_attrs.quantity ='||l_po_line_attrs.quantity);
5705       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'l_po_line_attrs.need_by_date ='||l_po_line_attrs.need_by_date);
5706     END IF;
5707 
5708 
5709     IF (l_po_line_attrs.item_id is null and l_po_line_attrs.item_description is null) THEN
5710       FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_POL_ITEM_ID_DESC_NLL');
5711       FND_MESSAGE.Set_Token('LINE_NUM', l_po_line_attrs.line_num);
5712       FND_MSG_PUB.ADD;
5713     END IF;
5714 
5715     IF (l_po_line_attrs.line_type_id is null) THEN
5716       FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_POL_TYPE_NLL');
5717       FND_MESSAGE.Set_Token('LINE_NUM', l_po_line_attrs.line_num);
5718       FND_MSG_PUB.ADD;
5719     END IF;
5720 
5721     IF (l_po_line_attrs.uom_code is null) THEN
5722       FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_POL_UOM_NLL');
5723       FND_MESSAGE.Set_Token('LINE_NUM', l_po_line_attrs.line_num);
5724       FND_MSG_PUB.ADD;
5725     END IF;
5726 
5727     IF (l_po_line_attrs.quantity is null) THEN
5728       FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_POL_QUANT_NLL');
5729       FND_MESSAGE.Set_Token('LINE_NUM', l_po_line_attrs.line_num);
5730       FND_MSG_PUB.ADD;
5731     END IF;
5732 
5733     IF (l_po_line_attrs.need_by_date is null) THEN
5734       FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_POL_NBDATE_NLL');
5735       FND_MESSAGE.Set_Token('LINE_NUM', l_po_line_attrs.line_num);
5736       FND_MSG_PUB.ADD;
5737     END IF;
5738 
5739   END IF;
5740 
5741   --If the po_line_id is passed validate the attributes got from the PO Line
5742   IF(p_x_osp_order_line_rec.po_line_id is NOT NULL) THEN
5743     /*
5744     Note that the messages below will not have PO line numbers associated. They will not ideally occur from
5745     front end as the front LOV already will filter records according to the conditions below
5746     and moreover the data from po_lines_all should not have the below errors unless manually changed in the tables
5747     */
5748     val_svc_item_vs_wo_svc_item (p_x_osp_order_line_rec.workorder_id, l_po_line_attrs.item_id);
5749     validate_service_item_uom(l_po_line_attrs.item_id, p_x_osp_order_line_rec.service_item_uom_code, p_x_osp_order_line_rec.inventory_org_id);
5750     IF(l_po_line_attrs.quantity <=0)  THEN
5751       FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_QUANT_VAL');
5752       FND_MESSAGE.Set_Token('QUANTITY', p_x_osp_order_line_rec.quantity);
5753       FND_MSG_PUB.ADD;
5754     END IF;
5755     validate_po_line_type(l_po_line_attrs.line_type_id);
5756     --Assign the attributes from the po line to the osp line
5757     --over-write the ones defined at the workorder route level
5758     p_x_osp_order_line_rec.service_item_id := l_po_line_attrs.item_id;
5759     p_x_osp_order_line_rec.service_item_description := l_po_line_attrs.item_description;
5760     p_x_osp_order_line_rec.service_item_uom_code := l_po_line_attrs.uom_code;
5761 
5762     p_x_osp_order_line_rec.po_line_type_id := l_po_line_attrs.line_type_id;
5763     p_x_osp_order_line_rec.quantity := l_po_line_attrs.quantity;
5764     p_x_osp_order_line_rec.need_by_date := l_po_line_attrs.need_by_date;
5765 
5766   ELSE --retain the existing logic
5767 
5768   --validate service_item_id (simplfied the original one)
5769   --All the service item related attributes validations are applicable only
5770   --when the service item is provided
5771   /* Comment Added by mpothuku on 28-Feb-06: Note that for workorder based lines, the service item defined
5772      at the route level is not yet defaulted, so for such lines both the service_item_id and service_item_uom are null and also there is no need to validate these for the workorder based lines */
5773   IF (p_x_osp_order_line_rec.service_item_id IS NOT NULL) THEN
5774     validate_service_item_id(p_x_osp_order_line_rec.service_item_id, p_x_osp_order_line_rec.inventory_org_id);
5775     --validate service_item_description
5776     --it is not necessary to add the original one here.
5777     --validate sercice_item_uom (just borrowed the old one)
5778     validate_service_item_uom(p_x_osp_order_line_rec.service_item_id, p_x_osp_order_line_rec.service_item_uom_code, p_x_osp_order_line_rec.inventory_org_id);
5779     --validate sercice_item_quantity(quantity)
5780     IF (p_x_osp_order_line_rec.quantity IS NOT NULL AND p_x_osp_order_line_rec.quantity <> FND_API.G_MISS_NUM) THEN
5781       IF(p_x_osp_order_line_rec.quantity <=0)  THEN
5782         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_QUANT_VAL');
5783         FND_MESSAGE.Set_Token('QUANTITY', p_x_osp_order_line_rec.quantity);
5784         FND_MSG_PUB.ADD;
5785       END IF;
5786     --quantity cannot be null when UOM is not null
5787     ELSIF (p_x_osp_order_line_rec.service_item_uom_code IS NOT NULL AND p_x_osp_order_line_rec.service_item_uom_code <> FND_API.G_MISS_CHAR) THEN
5788       FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_QUANT_NLL');
5789       FND_MSG_PUB.ADD;
5790     END IF;
5791 
5792     --Validate header vendor with physical, service item combination in line
5793     --validate_vendor_service_item(p_x_osp_order_line_rec);
5794     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5795       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, G_LOG_PREFIX || '.validate_order_line_creation', 'Normal End.');
5796     END IF;
5797   END IF;
5798 
5799   --yazhou 22-Aug-2006 starts
5800   -- Bug fix#5479266
5801   -- For serial controlled items, check whether another OSP order
5802   -- has already been created for the same item.
5803 
5804    IF l_check_physical_item.serial_number_control_code IN (2, 5, 6) THEN
5805 
5806     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5807       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, G_LOG_PREFIX || '.validate_order_line_creation', 'osp_order_id='||p_x_osp_order_line_rec.osp_order_id||'lot_number='||p_x_osp_order_line_rec.lot_number);
5808       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, G_LOG_PREFIX || '.validate_order_line_creation', 'serial_number='||p_x_osp_order_line_rec.serial_number);
5809     END IF;
5810 
5811      OPEN check_osp_order_unique(p_x_osp_order_line_rec.osp_order_id,
5812                                  p_x_osp_order_line_rec.inventory_item_id,
5813                                  p_x_osp_order_line_rec.inventory_org_id,
5814                                  p_x_osp_order_line_rec.serial_number);
5815 
5816       FETCH check_osp_order_unique INTO g_dummy_char;
5817 
5818       IF check_osp_order_unique%FOUND THEN
5819          FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_PHY_ITEM_UNIUE');
5820          FND_MSG_PUB.ADD;
5821       END IF;
5822       CLOSE check_osp_order_unique;
5823    END IF;
5824   --yazhou 22-Aug-2006 ends
5825 
5826   --For tracked item, same physical item and service item combination can only appear once in a given OSP order header
5827   --While for non tracked item, same physical item can only appear once in a given OSP order header
5828   IF (p_x_osp_order_line_rec.workorder_id IS NULL) THEN
5829     IF (nvl(l_check_physical_item.comms_nl_trackable_flag, 'N')='N') THEN
5830       OPEN check_phy_item_unique(p_x_osp_order_line_rec.osp_order_id,
5831                                  p_x_osp_order_line_rec.inventory_item_id);
5832       FETCH check_phy_item_unique INTO g_dummy_char;
5833       IF check_phy_item_unique%FOUND THEN
5834         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_PHY_ITEM_UNIUE');
5835         FND_MSG_PUB.ADD;
5836       END IF;
5837       CLOSE check_phy_item_unique;
5838     --ELSIF (p_x_osp_order_line_rec.service_item_id IS NOT NULL) THEN
5839     --The cursor query contains the case which service_item_id is null
5840     ELSE
5841       OPEN check_phy_ser_item_unique(p_x_osp_order_line_rec.osp_order_id,
5842                                      p_x_osp_order_line_rec.service_item_id,
5843                                      p_x_osp_order_line_rec.inventory_item_id,
5844                                      p_x_osp_order_line_rec.inventory_org_id,
5845                                      p_x_osp_order_line_rec.lot_number,
5846                                      p_x_osp_order_line_rec.serial_number);
5847       FETCH check_phy_ser_item_unique INTO g_dummy_char;
5848       IF check_phy_ser_item_unique%FOUND THEN
5849         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_PHY_SER_ITEM_UNIUE');
5850         FND_MSG_PUB.ADD;
5851       END IF;
5852       CLOSE check_phy_ser_item_unique;
5853     END IF;
5854   END IF;
5855 
5856   END IF;--IF(p_x_osp_order_line_rec.po_line_id is NOT NULL)
5857   -- jaramana January 10, 2008 Bug 5358438/5967633/5417460 Ends
5858 
5859 END validate_order_line_creation;
5860 
5861 PROCEDURE update_osp_order_line(p_x_osp_order_line_rec IN OUT NOCOPY osp_order_line_rec_type)
5862 IS
5863   l_osp_line_id              NUMBER;
5864   l_osp_line_number          NUMBER;
5865   l_return_status            VARCHAR2(1);
5866   l_organization_id          NUMBER;
5867   l_inventory_item_id        NUMBER;
5868   l_service_item_id          NUMBER;
5869   l_service_duration         NUMBER;
5870 
5871    --Modification by JROTICH for bug#13818139 BEGIN
5872   CURSOR get_exch_instance_id(c_osp_order_line_id NUMBER) is
5873     SELECT
5874       EXCHANGE_INSTANCE_ID
5875     FROM AHL_OSP_ORDER_LINES
5876     WHERE OSP_ORDER_LINE_ID=c_osp_order_line_id;
5877   l_exchange_instance_id      NUMBER;
5878   l_msg_count                 NUMBER;
5879   l_msg_data                  VARCHAR2(2000);
5880   L_DEBUG_KEY                 VARCHAR2(150) := G_LOG_PREFIX || '.update_osp_order_line';
5881    --Modification by JROTICH for bug#13818139 END
5882 
5883 
5884 BEGIN
5885   validate_order_line_update(p_x_osp_order_line_rec);
5886 
5887   --Modification by JROTICH to handle update of csi_txn tables after update of exchange instance for bug #13818139: BEGIN
5888   OPEN get_exch_instance_id(c_osp_order_line_id => p_x_osp_order_line_rec.osp_order_line_id);
5889   FETCH get_exch_instance_id INTO l_exchange_instance_id;
5890   CLOSE get_exch_instance_id;
5891 
5892   --JROTICH added parenthesis in order to always satisfy a non null return line ID for fixing bug # 13917427
5893   IF (((l_exchange_instance_id is null AND p_x_osp_order_line_rec.exchange_instance_id is not null) OR --new exchange instance
5894       (l_exchange_instance_id is not null AND p_x_osp_order_line_rec.exchange_instance_id is null) OR --exchange instance deleted
5895       (l_exchange_instance_id  is not null AND l_exchange_instance_id <> p_x_osp_order_line_rec.exchange_instance_id)) AND--exchange instance modified
5896       (p_x_osp_order_line_rec.oe_return_line_id is not null)) THEN --we can't get to the transaction tables without the return_line_id
5897       --Delete IB Transactions for these cases
5898       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5899             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Before calling Delete_IB_Transaction ' );
5900       END IF;
5901 
5902       AHL_OSP_SHIPMENT_PUB.Delete_IB_Transaction(
5903         p_init_msg_list         => FND_API.G_FALSE, --p_init_msg_list,
5904         p_commit                => FND_API.G_FALSE,
5905         p_validation_level      => FND_API.G_VALID_LEVEL_FULL,
5906         x_return_status         => l_return_status,
5907         x_msg_count             => l_msg_count,
5908         x_msg_data              => l_msg_data,
5909         p_oe_line_id            => p_x_osp_order_line_rec.oe_return_line_id);
5910       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5911         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'After calling Delete_IB_Transaction: Return status' || l_return_status  );
5912       END IF;
5913       IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
5914         RAISE FND_API.G_EXC_ERROR;
5915       ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
5916         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5917       END IF;
5918 
5919 
5920       IF (p_x_osp_order_line_rec.exchange_instance_id is not null) THEN
5921         --Create IB Transactions for these cases, note that we are not recreating when the exchange instance id is null
5922         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5923             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Before calling Create_IB_Transaction ' );
5924         END IF;
5925 
5926         AHL_OSP_SHIPMENT_PUB.Create_IB_Transaction(
5927           p_init_msg_list         => FND_API.G_FALSE, --p_init_msg_list,
5928           p_commit                => FND_API.G_FALSE,
5929           p_validation_level      => FND_API.G_VALID_LEVEL_FULL,
5930           x_return_status         => l_return_status,
5931           x_msg_count             => l_msg_count,
5932           x_msg_data              => l_msg_data,
5933           p_osp_order_type        => AHL_OSP_ORDERS_PVT.G_OSP_ORDER_TYPE_EXCHANGE,
5934           p_oe_line_type          => 'RETURN',
5935           p_oe_line_id            => p_x_osp_order_line_rec.oe_return_line_id,
5936           p_csi_instance_id       => p_x_osp_order_line_rec.exchange_instance_id);
5937 
5938         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
5939             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'After calling Create_IB_Transaction: Return status' || l_return_status );
5940         END IF;
5941         IF (l_return_status = FND_API.G_RET_STS_ERROR) THEN
5942           RAISE FND_API.G_EXC_ERROR;
5943         ELSIF (l_return_status = FND_API.G_RET_STS_UNEXP_ERROR) THEN
5944           RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5945         END IF;
5946 
5947 
5948       END IF;
5949   END IF;
5950   --Modification by JROTICH: END
5951 
5952 
5953   --call table handler to update the record
5954   AHL_OSP_ORDER_LINES_PKG.update_row(
5955            p_osp_order_line_id => p_x_osp_order_line_rec.osp_order_line_id,
5956            p_object_version_number => p_x_osp_order_line_rec.object_version_number +1,
5957            p_osp_order_id => p_x_osp_order_line_rec.osp_order_id,
5958            p_osp_line_number => p_x_osp_order_line_rec.osp_line_number,
5959            p_status_code => p_x_osp_order_line_rec.status_code,
5960            p_po_line_type_id => p_x_osp_order_line_rec.po_line_type_id,
5961            p_service_item_id => p_x_osp_order_line_rec.service_item_id,
5962            p_service_item_description => p_x_osp_order_line_rec.service_item_description,
5963            p_service_item_uom_code => p_x_osp_order_line_rec.service_item_uom_code,
5964            p_need_by_date => TRUNC(p_x_osp_order_line_rec.need_by_date),
5965            p_ship_by_date => TRUNC(p_x_osp_order_line_rec.ship_by_date),
5966            p_po_line_id => p_x_osp_order_line_rec.po_line_id,
5967            p_oe_ship_line_id => p_x_osp_order_line_rec.oe_ship_line_id,
5968            p_oe_return_line_id => p_x_osp_order_line_rec.oe_return_line_id,
5969            p_workorder_id => p_x_osp_order_line_rec.workorder_id,
5970            p_operation_id => p_x_osp_order_line_rec.operation_id,
5971            p_quantity => p_x_osp_order_line_rec.quantity,
5972            p_exchange_instance_id    => p_x_osp_order_line_rec.exchange_instance_id,
5973            p_inventory_item_id       => p_x_osp_order_line_rec.inventory_item_id,
5974            p_inventory_org_id        => p_x_osp_order_line_rec.inventory_org_id,
5975            p_inventory_item_uom      => p_x_osp_order_line_rec.inventory_item_uom,
5976            p_inventory_item_quantity => p_x_osp_order_line_rec.inventory_item_quantity,
5977            p_sub_inventory           => p_x_osp_order_line_rec.sub_inventory,
5978            p_lot_number              => p_x_osp_order_line_rec.lot_number,
5979            p_serial_number           => p_x_osp_order_line_rec.serial_number,
5980 -- Added by jaramana on January 10, 2008 for the Requisition ER 6034236
5981            p_po_req_line_id          => p_x_osp_order_line_rec.po_req_line_id,
5982 -- jaramana End
5983            p_attribute_category => p_x_osp_order_line_rec.attribute_category,
5984            p_attribute1 => p_x_osp_order_line_rec.attribute1,
5985            p_attribute2 => p_x_osp_order_line_rec.attribute2,
5986            p_attribute3 => p_x_osp_order_line_rec.attribute3,
5987            p_attribute4 => p_x_osp_order_line_rec.attribute4,
5988            p_attribute5 => p_x_osp_order_line_rec.attribute5,
5989            p_attribute6 => p_x_osp_order_line_rec.attribute6,
5990            p_attribute7 => p_x_osp_order_line_rec.attribute7,
5991            p_attribute8 => p_x_osp_order_line_rec.attribute8,
5992            p_attribute9 => p_x_osp_order_line_rec.attribute9,
5993            p_attribute10 => p_x_osp_order_line_rec.attribute10,
5994            p_attribute11 => p_x_osp_order_line_rec.attribute11,
5995            p_attribute12 => p_x_osp_order_line_rec.attribute12,
5996            p_attribute13 => p_x_osp_order_line_rec.attribute13,
5997            p_attribute14 => p_x_osp_order_line_rec.attribute14,
5998            p_attribute15 => p_x_osp_order_line_rec.attribute15,
5999            p_last_updated_by => FND_GLOBAL.user_id,
6000            p_last_update_date => SYSDATE,
6001            p_last_update_login => FND_GLOBAL.login_id
6002          );
6003 END update_osp_order_line;
6004 
6005 PROCEDURE validate_order_line_update(p_x_osp_order_line_rec IN OUT NOCOPY osp_order_line_rec_type)
6006 IS
6007   l_osp_line_id              NUMBER;
6008   l_osp_line_number          NUMBER;
6009   l_return_status            VARCHAR2(1);
6010   l_organization_id          NUMBER;
6011   l_inventory_item_id        NUMBER;
6012   l_service_item_id          NUMBER;
6013   l_service_duration         NUMBER;
6014   l_trackable_flag           VARCHAR2(1);
6015   l_header_status_code       VARCHAR2(30);
6016   l_item_description         VARCHAR2(240);
6017   l_desc_update_flag         VARCHAR2(1);
6018   CURSOR osp_order_line_csr IS
6019     SELECT *
6020       FROM ahl_osp_order_lines
6021      WHERE osp_order_line_id = p_x_osp_order_line_rec.osp_order_line_id
6022        AND object_version_number= p_x_osp_order_line_rec.object_version_number;
6023   l_osp_order_line_rec osp_order_line_csr%ROWTYPE;
6024   CURSOR check_phy_ser_item_unique(c_osp_order_id NUMBER,
6025                                    c_service_item_id NUMBER,
6026                                    c_inv_item_id NUMBER,
6027                                    c_inv_org_id NUMBER,
6028                                    c_lot_number VARCHAR2,
6029                                    c_serial_number VARCHAR2) IS
6030     SELECT 'X'
6031       FROM ahl_osp_order_lines
6032      WHERE osp_order_id = c_osp_order_id
6033        AND ((service_item_id = c_service_item_id) OR
6034             (service_item_id IS NULL AND c_service_item_id IS NULL))
6035        AND inventory_item_id = c_inv_item_id
6036        AND inventory_org_id = c_inv_org_id
6037        AND ((lot_number IS NULL AND c_lot_number IS NULL) OR (lot_number = c_lot_number))
6038        AND ((serial_number IS NULL AND c_serial_number IS NULL) OR (serial_number = c_serial_number));
6039   CURSOR check_phy_item_unique(c_osp_order_id NUMBER, c_inv_item_id NUMBER) IS
6040     SELECT 'X'
6041       FROM ahl_osp_order_lines
6042      WHERE inventory_item_id = c_inv_item_id
6043        AND osp_order_id = c_osp_order_id;
6044   L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.validate_order_line_update';
6045   --Added by mpothuku on 24-Mar-06 for ER: 4544654
6046   l_owrite_svc_desc_prf VARCHAR2(1);
6047   l_item_prefix         VARCHAR2(240);
6048   l_serial_prefix       VARCHAR2(240);
6049   l_inv_item_number     VARCHAR2(40);
6050   l_svc_item_number     VARCHAR2(40);
6051   --mpothuku end
6052 BEGIN
6053   --For OAF, the default logic may not be necessary
6054   --we decided to pass null when you want to change it to null, the old value if there is no change.
6055   IF nvl(g_module_type, 'NULL') <> G_OAF_MODULE THEN
6056     default_unchanged_order_line(p_x_osp_order_line_rec);
6057   END IF;
6058   convert_order_line_val_to_id(p_x_osp_order_line_rec);
6059   --Add the other validations here
6060   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6061     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Procedure');
6062   END IF;
6063   IF(p_x_osp_order_line_rec.osp_order_line_id IS NULL OR p_x_osp_order_line_rec.object_version_number IS NULL) THEN
6064     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_ID_OBJ_MISS');
6065     FND_MSG_PUB.ADD;
6066   ELSE
6067     OPEN osp_order_line_csr;
6068     FETCH osp_order_line_csr INTO l_osp_order_line_rec;
6069     IF (osp_order_line_csr%NOTFOUND) THEN
6070       FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INVOP_OSP_LN_NFOUND');
6071       FND_MSG_PUB.ADD;
6072     ELSE
6073       --The following attributes can't be changed once created
6074       --osp_order_id can't be changed, and osp_order_id is required
6075       IF(p_x_osp_order_line_rec.osp_order_id IS NULL OR
6076          p_x_osp_order_line_rec.osp_order_id <> l_osp_order_line_rec.osp_order_id) THEN
6077         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_ORD_ID_CHG');
6078         FND_MESSAGE.Set_Token('LINE_NUMBER', l_osp_order_line_rec.osp_line_number);
6079         FND_MSG_PUB.ADD;
6080       END IF;
6081       --osp_order_number can't be changed and osp_order_number is required
6082       IF(p_x_osp_order_line_rec.osp_line_number IS NULL OR
6083          p_x_osp_order_line_rec.osp_line_number <> l_osp_order_line_rec.osp_line_number) THEN
6084         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_LN_NUM_CHG');
6085         FND_MESSAGE.Set_Token('LINE_NUMBER', l_osp_order_line_rec.osp_line_number);
6086         FND_MSG_PUB.ADD;
6087       END IF;
6088       --workorder_id can't be changed but workorder can be null
6089       IF((p_x_osp_order_line_rec.workorder_id <> l_osp_order_line_rec.workorder_id) OR
6090          (p_x_osp_order_line_rec.workorder_id IS NOT NULL AND l_osp_order_line_rec.workorder_id IS NULL) OR
6091          (p_x_osp_order_line_rec.workorder_id IS NULL AND l_osp_order_line_rec.workorder_id IS NOT NULL)) THEN
6092         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_WO_CHG');
6093         FND_MESSAGE.Set_Token('LINE_NUMBER', l_osp_order_line_rec.osp_line_number);
6094         FND_MSG_PUB.ADD;
6095       END IF;
6096       --inventory_item_id can't be changed and inventory_item_id is always populated
6097       IF(p_x_osp_order_line_rec.inventory_item_id IS NULL OR
6098          p_x_osp_order_line_rec.inventory_item_id <> l_osp_order_line_rec.inventory_item_id) THEN
6099         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_ITEM_CHG');
6100         FND_MESSAGE.Set_Token('LINE_NUMBER', l_osp_order_line_rec.osp_line_number);
6101         FND_MSG_PUB.ADD;
6102       END IF;
6103       --inventory_org_id can't be changed and inventory_org_id is always popluated
6104       IF(p_x_osp_order_line_rec.inventory_org_id IS NULL OR
6105          p_x_osp_order_line_rec.inventory_org_id <> l_osp_order_line_rec.inventory_org_id) THEN
6106         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_ORG_CHG');
6107         FND_MESSAGE.Set_Token('LINE_NUMBER', l_osp_order_line_rec.osp_line_number);
6108         FND_MSG_PUB.ADD;
6109       END IF;
6110       --inventory_item_uom can't be changed and inventory_item_uom is always populated
6111       IF(p_x_osp_order_line_rec.inventory_item_uom IS NULL OR
6112          p_x_osp_order_line_rec.inventory_item_uom <> l_osp_order_line_rec.inventory_item_uom) THEN
6113         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_UOM_CHG');
6114         FND_MESSAGE.Set_Token('LINE_NUMBER', l_osp_order_line_rec.osp_line_number);
6115         FND_MSG_PUB.ADD;
6116       END IF;
6117       --inventory_item_quantity can't be changed and inventory_item_quantity is always populated
6118       IF(p_x_osp_order_line_rec.inventory_item_quantity IS NULL OR
6119          p_x_osp_order_line_rec.inventory_item_quantity <> l_osp_order_line_rec.inventory_item_quantity) THEN
6120         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_QTY_CHG');
6121         FND_MESSAGE.Set_Token('LINE_NUMBER', l_osp_order_line_rec.osp_line_number);
6122         FND_MSG_PUB.ADD;
6123       END IF;
6124       --sub_inventory can't be changed but sub_inventory can be null
6125       IF((p_x_osp_order_line_rec.sub_inventory <> l_osp_order_line_rec.sub_inventory) OR
6126          (p_x_osp_order_line_rec.sub_inventory IS NOT NULL AND l_osp_order_line_rec.sub_inventory IS NULL) OR
6127          (p_x_osp_order_line_rec.sub_inventory IS NULL AND l_osp_order_line_rec.sub_inventory IS NOT NULL)) THEN
6128         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_SUB_CHG');
6129         FND_MESSAGE.Set_Token('LINE_NUMBER', l_osp_order_line_rec.osp_line_number);
6130         FND_MSG_PUB.ADD;
6131       END IF;
6132       --lot_number can't be changed but lot_number can be null
6133       IF((p_x_osp_order_line_rec.lot_number <> l_osp_order_line_rec.lot_number) OR
6134          (p_x_osp_order_line_rec.lot_number IS NOT NULL AND l_osp_order_line_rec.lot_number IS NULL) OR
6135          (p_x_osp_order_line_rec.lot_number IS NULL AND l_osp_order_line_rec.lot_number IS NOT NULL)) THEN
6136         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_LOT_CHG');
6137         FND_MESSAGE.Set_Token('LINE_NUMBER', l_osp_order_line_rec.osp_line_number);
6138         FND_MSG_PUB.ADD;
6139       END IF;
6140       --serial_number can't be changed but serial_number can be null
6141       IF((p_x_osp_order_line_rec.serial_number <> l_osp_order_line_rec.serial_number) OR
6142          (p_x_osp_order_line_rec.serial_number IS NOT NULL AND l_osp_order_line_rec.serial_number IS NULL) OR
6143          (p_x_osp_order_line_rec.serial_number IS NULL AND l_osp_order_line_rec.serial_number IS NOT NULL)) THEN
6144         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_SERIAL_CHG');
6145         FND_MESSAGE.Set_Token('LINE_NUMBER', l_osp_order_line_rec.osp_line_number);
6146         FND_MSG_PUB.ADD;
6147       END IF;
6148       -- this API does not allow to update SO line information, Shipment API needs to update
6149       -- OSP tables directly (is this the best approach?)
6150       --oe_ship_line_id can't be changed from this API, and it could be null.
6151       IF((p_x_osp_order_line_rec.oe_ship_line_id <> l_osp_order_line_rec.oe_ship_line_id) OR
6152          (p_x_osp_order_line_rec.oe_ship_line_id IS NOT NULL AND l_osp_order_line_rec.oe_ship_line_id IS NULL) OR
6153          (p_x_osp_order_line_rec.oe_ship_line_id IS NULL AND l_osp_order_line_rec.oe_ship_line_id IS NOT NULL)) THEN
6154         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_SHIP_ID_CHG');
6155         FND_MESSAGE.Set_Token('LINE_NUMBER', l_osp_order_line_rec.osp_line_number);
6156         FND_MSG_PUB.ADD;
6157       END IF;
6158       --oe_return_line_id can't be changed from this API, and it could be null.
6159       IF((p_x_osp_order_line_rec.oe_return_line_id <> l_osp_order_line_rec.oe_return_line_id) OR
6160          (p_x_osp_order_line_rec.oe_return_line_id IS NOT NULL AND l_osp_order_line_rec.oe_return_line_id IS NULL) OR
6161          (p_x_osp_order_line_rec.oe_return_line_id IS NULL AND l_osp_order_line_rec.oe_return_line_id IS NOT NULL)) THEN
6162         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_RET_ID_CHG');
6163         FND_MESSAGE.Set_Token('LINE_NUMBER', l_osp_order_line_rec.osp_line_number);
6164         FND_MSG_PUB.ADD;
6165       END IF;
6166       -- this API does not allow to update PO line information, PO API needs to update
6167       -- OSP tables directly (is this the best approach?)
6168       --po_line_id can't be changed from this API, and it could be null.
6169       IF((p_x_osp_order_line_rec.po_line_id <> l_osp_order_line_rec.po_line_id) OR
6170          (p_x_osp_order_line_rec.po_line_id IS NOT NULL AND l_osp_order_line_rec.po_line_id IS NULL) OR
6171          (p_x_osp_order_line_rec.po_line_id IS NULL AND l_osp_order_line_rec.po_line_id IS NOT NULL)) THEN
6172         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_POLINE_ID_CHG');
6173         FND_MESSAGE.Set_Token('LINE_NUMBER', l_osp_order_line_rec.osp_line_number);
6174         FND_MSG_PUB.ADD;
6175       END IF;
6176 
6177       -- Added by jaramana on January 10, 2008 for the Requisition ER 6034236
6178       -- this API does not allow to update PO line information, PO API needs to update OSP tables directly
6179       --po_req_line_id can't be changed from this API, and it could be null.
6180       IF((p_x_osp_order_line_rec.po_req_line_id <> l_osp_order_line_rec.po_req_line_id) OR
6181          (p_x_osp_order_line_rec.po_req_line_id IS NOT NULL AND l_osp_order_line_rec.po_req_line_id IS NULL) OR
6182          (p_x_osp_order_line_rec.po_req_line_id IS NULL AND l_osp_order_line_rec.po_req_line_id IS NOT NULL)) THEN
6183         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_REQLINE_ID_CHG');
6184         FND_MESSAGE.Set_Token('LINE_NUMBER', l_osp_order_line_rec.osp_line_number);
6185         FND_MSG_PUB.ADD;
6186       END IF;
6187 
6188       --po_line_type_id can't be changed from this API, and it could be null.
6189       IF((p_x_osp_order_line_rec.po_line_type_id <> l_osp_order_line_rec.po_line_type_id) OR
6190          (p_x_osp_order_line_rec.po_line_type_id IS NOT NULL AND l_osp_order_line_rec.po_line_type_id IS NULL) OR
6191          (p_x_osp_order_line_rec.po_line_type_id IS NULL AND l_osp_order_line_rec.po_line_type_id IS NOT NULL)) THEN
6192         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_POTYPE_ID_CHG');
6193         FND_MESSAGE.Set_Token('LINE_NUMBER', l_osp_order_line_rec.osp_line_number);
6194         FND_MSG_PUB.ADD;
6195       END IF;
6196 
6197       -- Added by jaramana on January 10, 2008 for the Requisition ER 6034236
6198       --If the status is other than 'ENTERED', 'SUBMISSION_FAILED', 'REQ_SUBMISSION_FAILED' we should not allow updates of
6199       --service_item_id, service_item_uom_code, quantity and need_by_date
6200       SELECT status_code INTO l_header_status_code
6201         FROM ahl_osp_orders_b
6202        WHERE osp_order_id = p_x_osp_order_line_rec.osp_order_id;
6203 
6204        IF(l_header_status_code NOT IN (G_OSP_ENTERED_STATUS, G_OSP_SUB_FAILED_STATUS, G_OSP_REQ_SUB_FAILED_STATUS)) THEN
6205           --service_item_id can't be changed from this API, and it could be null.
6206           IF((p_x_osp_order_line_rec.service_item_id <> l_osp_order_line_rec.service_item_id) OR
6207              (p_x_osp_order_line_rec.service_item_id IS NOT NULL AND l_osp_order_line_rec.service_item_id IS NULL) OR
6208              (p_x_osp_order_line_rec.service_item_id IS NULL AND l_osp_order_line_rec.service_item_id IS NOT NULL)) THEN
6209             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_SER_ID_CHG');
6210             FND_MESSAGE.Set_Token('LINE_NUMBER', l_osp_order_line_rec.osp_line_number);
6211             FND_MSG_PUB.ADD;
6212           END IF;
6213 
6214           --service_item_description can't be changed from this API, and it could be null.
6215           IF((p_x_osp_order_line_rec.service_item_description <> l_osp_order_line_rec.service_item_description) OR
6216              (p_x_osp_order_line_rec.service_item_description IS NOT NULL AND l_osp_order_line_rec.service_item_description IS NULL) OR
6217              (p_x_osp_order_line_rec.service_item_description IS NULL AND l_osp_order_line_rec.service_item_description IS NOT NULL)) THEN
6218             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_SER_DESC_CHG');
6219             FND_MESSAGE.Set_Token('LINE_NUMBER', l_osp_order_line_rec.osp_line_number);
6220             FND_MSG_PUB.ADD;
6221           END IF;
6222 
6223 	 /*
6224           --exchange_instance_id can't be changed from this API, and it could be null.
6225           IF((p_x_osp_order_line_rec.exchange_instance_id <> l_osp_order_line_rec.exchange_instance_id) OR
6226              (p_x_osp_order_line_rec.exchange_instance_id IS NOT NULL AND l_osp_order_line_rec.exchange_instance_id IS NULL) OR
6227              (p_x_osp_order_line_rec.exchange_instance_id IS NULL AND l_osp_order_line_rec.exchange_instance_id IS NOT NULL)) THEN
6228             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_EXCH_ID_CHG');
6229             FND_MESSAGE.Set_Token('LINE_NUMBER', l_osp_order_line_rec.osp_line_number);
6230             FND_MSG_PUB.ADD;
6231           END IF;
6232 	 */
6233 
6234           --service_item_uom cannot change and cannot be null
6235           IF(p_x_osp_order_line_rec.service_item_uom_code IS NULL OR
6236              (p_x_osp_order_line_rec.service_item_uom_code <> l_osp_order_line_rec.service_item_uom_code) ) THEN
6237             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_SER_UOM_CHG');
6238             FND_MESSAGE.Set_Token('LINE_NUMBER', l_osp_order_line_rec.osp_line_number);
6239             FND_MSG_PUB.ADD;
6240           END IF;
6241 
6242           --service_item_quantity cannot change and cannot be null
6243           IF(p_x_osp_order_line_rec.quantity IS NULL OR
6244              (p_x_osp_order_line_rec.quantity <> l_osp_order_line_rec.quantity) ) THEN
6245             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_SER_QTY_CHG');
6246             FND_MESSAGE.Set_Token('LINE_NUMBER', l_osp_order_line_rec.osp_line_number);
6247             FND_MSG_PUB.ADD;
6248           END IF;
6249 
6250           --Neeed By Date cannot change and cannot be null
6251           IF(p_x_osp_order_line_rec.need_by_date IS NULL OR
6252              (trunc(p_x_osp_order_line_rec.need_by_date) <> trunc(l_osp_order_line_rec.need_by_date) )) THEN
6253             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_NBD_CHG');
6254             FND_MESSAGE.Set_Token('LINE_NUMBER', l_osp_order_line_rec.osp_line_number);
6255             FND_MSG_PUB.ADD;
6256           END IF;
6257       END IF;
6258       -- jaramana End
6259 
6260       --Service_item_description is updatable only if service_item_id is NULL, and serive_item_description can be null
6261       --Here we use the item description derived from service_item_id instead of l_service_item_description which is from
6262       --database table and could be derived from service_item_id and could be just
6263       --the identifier of one-time service item
6264 
6265       --Jerry changed on 10/04/05 for AE enhancement 4544654 to allow the service item description to be changed in AHL
6266       --side.
6267 
6268       IF (p_x_osp_order_line_rec.service_item_id IS NOT NULL) THEN
6269         BEGIN
6270           --Assuming service_item_id is always in mtl_system_items_kfv and its organization equals that of physical item
6271           --Alwasy set service_item_description to be derived from service_item_id if service_item_id is not null
6272           --Changes made by mpothuku on 27-Mar-06 to fix the ER 4544654 and Bug 5013047
6273           SELECT description, allow_item_desc_update_flag, concatenated_segments INTO l_item_description, l_desc_update_flag, l_svc_item_number
6274             FROM mtl_system_items_kfv
6275            WHERE inventory_item_id = p_x_osp_order_line_rec.service_item_id
6276              AND organization_id = p_x_osp_order_line_rec.inventory_org_id;
6277         EXCEPTION
6278           WHEN NO_DATA_FOUND THEN
6279             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_SVC_ITEM');
6280             FND_MESSAGE.Set_Token('SERVICE_ITEM_ID', p_x_osp_order_line_rec.service_item_id);
6281             FND_MSG_PUB.ADD;
6282         END;
6283         l_owrite_svc_desc_prf := NVL(FND_PROFILE.VALUE('AHL_OSP_OWRITE_SVC_DESC'), 'N');
6284         l_item_prefix := FND_PROFILE.VALUE('AHL_OSP_POL_ITEM_PREFIX');
6285         l_serial_prefix := FND_PROFILE.VALUE('AHL_OSP_POL_SER_PREFIX');
6286 
6287         --Fix for the bug 5013047
6288         /*
6289         IF(l_desc_update_flag <> 'Y' AND
6290            ((p_x_osp_order_line_rec.service_item_description <> l_item_description) OR
6291             (p_x_osp_order_line_rec.service_item_description IS NOT NULL AND l_item_description IS NULL) OR
6292             (p_x_osp_order_line_rec.service_item_description IS NULL AND l_item_description IS NOT NULL))) THEN
6293           FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_SVCITMDSC_CHG');
6294           FND_MESSAGE.Set_Token('LINE_NUMBER', l_osp_order_line_rec.osp_line_number);
6295           FND_MSG_PUB.ADD;
6296         END IF;
6297         */
6298         /*
6299         IF Overwrite Svc Description profile set to No l_item_description would have been
6300           defaulted as the service item desc from inventory above, if the profile is Yes, we proceed below
6301           to override the value set from the inventory
6302         */
6303 
6304         IF(l_owrite_svc_desc_prf = 'Y') THEN --Overwrite Svc Description profile set to Yes
6305           IF(NVL(l_desc_update_flag, 'N') = 'N') THEN --Allow Description update set to No
6306             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_SVCITMDSC_CHG');
6307             FND_MESSAGE.Set_Token('SERVICE_ITEM_NUMBER', l_svc_item_number);
6308             FND_MESSAGE.Set_Token('LINE_NUMBER', l_osp_order_line_rec.osp_line_number);
6309             FND_MSG_PUB.ADD;
6310           ELSE
6311             BEGIN
6312               SELECT concatenated_segments INTO l_inv_item_number
6313                 FROM mtl_system_items_kfv
6314                WHERE inventory_item_id = p_x_osp_order_line_rec.inventory_item_id
6315                  AND organization_id = p_x_osp_order_line_rec.inventory_org_id;
6316             EXCEPTION
6317               WHEN NO_DATA_FOUND THEN
6318                 FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_PHY_ITEM_INVALID');
6319                 FND_MSG_PUB.ADD;
6320             END;
6321             -- SALOGAN added an if condition to avoid adding trailing spaces for Non Serial Item
6322             -- for bug #9826606 Starts
6323             IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6324                 FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, G_LOG_PREFIX || 'validate_order_line_update',
6325                 'Item description : BEFORE : '|| l_item_description);
6326             END IF;
6327             IF(p_x_osp_order_line_rec.serial_number IS NULL) THEN
6328                l_item_description :=
6329                TRIM(SUBSTR(l_item_prefix||l_inv_item_number,1,240));
6330             ELSE
6331               l_item_description :=
6332               TRIM(SUBSTR(l_item_prefix||l_inv_item_number||' '||l_serial_prefix||p_x_osp_order_line_rec.serial_number,1,240));
6333             END IF;
6334             IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6335                 FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, G_LOG_PREFIX || 'validate_order_line_update',
6336                 'Item description : AFTER : '|| l_item_description);
6337             END IF;
6338             -- SALOGAN changes for bug #9826606 Ends
6339             --l_item_description :=
6340             --SUBSTR(l_item_prefix||l_inv_item_number||' '||l_serial_prefix||p_x_osp_order_line_rec.serial_number,1,240);
6341           END IF;
6342         END IF;
6343         p_x_osp_order_line_rec.service_item_description := l_item_description;
6344       END IF;
6345       --mpothuku End
6346 
6347       --Enforcing that either service_item_id and description should be not null
6348       --This is only enforced during update. Maybe it is better if delaying this validation
6349       --until submitting the OSP order?
6350       IF (p_x_osp_order_line_rec.service_item_id IS NULL AND p_x_osp_order_line_rec.service_item_description IS NULL) THEN
6351         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_SVCITMDSC_BOTH_NLL');
6352         FND_MESSAGE.Set_Token('LINE_NUMBER', l_osp_order_line_rec.osp_line_number);
6353         FND_MSG_PUB.ADD;
6354       END IF;
6355       --validate status_code change
6356       --it looks like only changing to 'CLOSED' occurs in this API, other statuses will be
6357       --changed in PO API?
6358       --NULL;
6359       --validate need_by_date
6360       -- Modified by jaramana on August 11, 2006 to not do this Date check if the line is PO Cancelled or PO Deleted (Bug 5478764)
6361       -- Added by jaramana on January 10, 2008 for the Requisition ER 6034236 (Added the Requisition related statuses)
6362       IF(TRUNC(p_x_osp_order_line_rec.need_by_date) < TRUNC(SYSDATE) AND
6363          (NVL(p_x_osp_order_line_rec.status_code, 'ENTERED') NOT IN (G_OL_PO_CANCELLED_STATUS, G_OL_PO_DELETED_STATUS,G_OL_REQ_CANCELLED_STATUS, G_OL_REQ_DELETED_STATUS))) THEN
6364         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_NEED_DT');
6365         FND_MESSAGE.Set_Token('NEED_BY_DATE', p_x_osp_order_line_rec.need_by_date);
6366         FND_MSG_PUB.ADD;
6367       END IF;
6368       -- jaramana End
6369       --validate ship_by_date (is this necessary?)
6370       /* Commented out on 05/31/2005 by Jerry
6371       IF(TRUNC(p_x_osp_order_line_rec.ship_by_date) < TRUNC(SYSDATE))THEN
6372         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_SHIP_DT');
6373         FND_MESSAGE.Set_Token('SHIP_BY_DATE', p_x_osp_order_line_rec.ship_by_date);
6374         FND_MSG_PUB.ADD;
6375       END IF;
6376       */
6377       --validate service_item_id (simplfied the original one)
6378 
6379       /* p_org_id added to validate_service_item_uom to fix the Perf Bug #4919164 by mpothuku on 28-Feb-06
6380       default_unchanged_order_line would have defaulted the inventory_org_id by now, hence even for workorder based lines inventory_org_id will be populated with the visit's org */
6381 
6382       validate_service_item_id(p_x_osp_order_line_rec.service_item_id, p_x_osp_order_line_rec.inventory_org_id);
6383       --validate service_item_description
6384       --it is not necessary to add the original one here.
6385       --validate sercice_item_uom (just borrowed the old one)
6386       validate_service_item_uom(p_x_osp_order_line_rec.service_item_id, p_x_osp_order_line_rec.service_item_uom_code, p_x_osp_order_line_rec.inventory_org_id);
6387       --validate sercice_item_quantity(quantity)
6388       --Service item quantity could be from the quantity of PO lines and not necessary to be the same as
6389       --as the value in ahl_osp_order_lines when PO has ever been created
6390       --created
6391       -- Added by jaramana on January 10, 2008 for the Requisition ER 6034236
6392       --Moved the logic below, to above
6393       /*
6394       SELECT status_code INTO l_header_status_code
6395         FROM ahl_osp_orders_b
6396        WHERE osp_order_id = p_x_osp_order_line_rec.osp_order_id;
6397       */
6398       -- jaramana End
6399       IF (p_x_osp_order_line_rec.quantity IS NOT NULL AND p_x_osp_order_line_rec.quantity <> FND_API.G_MISS_NUM) THEN
6400         IF(p_x_osp_order_line_rec.quantity <= 0)  THEN
6401           FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_QUANT_VAL');
6402           FND_MESSAGE.Set_Token('QUANTITY', p_x_osp_order_line_rec.quantity);
6403           FND_MSG_PUB.ADD;
6404         END IF;
6405         -- Added by jaramana on January 10, 2008 for the Requisition ER 6034236
6406         --Included the equal to zero clause above. Since we are modifying the OSP Line Views and are refraining from
6407         --synching up the quantity from PO Lines, the equal to clause can be imposed on all the OSP line statuses
6408         /*
6409         IF (p_x_osp_order_line_rec.quantity = 0 AND
6410             l_header_status_code NOT IN (G_OSP_PO_CREATED_STATUS, G_OSP_CLOSED_STATUS)) THEN
6411           FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_QUANT_VAL');
6412           FND_MESSAGE.Set_Token('QUANTITY', p_x_osp_order_line_rec.quantity);
6413           FND_MSG_PUB.ADD;
6414         END IF;
6415         */
6416         -- jaramana End
6417       --quantity cannot be null when UOM is not null
6418       ELSIF (p_x_osp_order_line_rec.service_item_uom_code IS NOT NULL AND p_x_osp_order_line_rec.service_item_uom_code <> FND_API.G_MISS_CHAR) THEN
6419         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_QUANT_NLL');
6420         FND_MSG_PUB.ADD;
6421       END IF;
6422 
6423       -- validate service/physical item combination with vendor
6424       --validate_vendor_service_item(p_x_osp_order_line_rec);
6425       -- validate physical/service item combination unique for a given OSP order
6426       -- For tracked item, same physical item and service item combination can only appear once in a given OSP order header
6427       -- While for non tracked item, same physical item can only appear once in a given OSP order header
6428       IF (p_x_osp_order_line_rec.workorder_id IS NULL AND
6429           p_x_osp_order_line_rec.service_item_id <> l_osp_order_line_rec.service_item_id) THEN
6430         SELECT comms_nl_trackable_flag INTO l_trackable_flag
6431           FROM mtl_system_items_kfv
6432          WHERE inventory_item_id = p_x_osp_order_line_rec.inventory_item_id
6433            AND organization_id = p_x_osp_order_line_rec.inventory_org_id;
6434         /* Commented out by Jerry on 10/11/05 due to an issue found by Pavan
6435         IF (nvl(l_trackable_flag, 'N')='N') THEN
6436           OPEN check_phy_item_unique(p_x_osp_order_line_rec.osp_order_id,
6437                                      p_x_osp_order_line_rec.inventory_item_id);
6438           FETCH check_phy_item_unique INTO g_dummy_char;
6439           IF check_phy_item_unique%FOUND THEN
6440             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_PHY_ITEM_UNIUE');
6441             FND_MSG_PUB.ADD;
6442           END IF;
6443           CLOSE check_phy_item_unique;
6444         ELSE
6445         */
6446         IF (nvl(l_trackable_flag, 'N')='Y') THEN
6447           OPEN check_phy_ser_item_unique(p_x_osp_order_line_rec.osp_order_id,
6448                                          p_x_osp_order_line_rec.service_item_id,
6449                                          p_x_osp_order_line_rec.inventory_item_id,
6450                                          p_x_osp_order_line_rec.inventory_org_id,
6451                                          p_x_osp_order_line_rec.lot_number,
6452                                          p_x_osp_order_line_rec.serial_number);
6453           FETCH check_phy_ser_item_unique INTO g_dummy_char;
6454           IF check_phy_ser_item_unique%FOUND THEN
6455             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_PHY_SER_ITEM_UNIUE');
6456             FND_MSG_PUB.ADD;
6457           END IF;
6458           CLOSE check_phy_ser_item_unique;
6459         END IF;
6460       END IF;
6461     END IF;
6462     CLOSE osp_order_line_csr;
6463   END IF;
6464 END validate_order_line_update;
6465 
6466 PROCEDURE derive_default_vendor(
6467   p_item_service_rels_tbl IN item_service_rels_tbl_type,
6468   x_vendor_id             OUT NOCOPY NUMBER,
6469   x_vendor_site_id        OUT NOCOPY NUMBER,
6470   x_vendor_contact_id     OUT NOCOPY NUMBER,
6471   x_valid_vendors_tbl     OUT NOCOPY Vendor_id_tbl_type)
6472 IS
6473   CURSOR get_vendor_cert(c_inv_item_id NUMBER,
6474                          c_inv_org_id NUMBER,
6475                          c_service_item_id NUMBER) IS
6476 
6477     /* mpothuku modified ahl_vendor_certifications to _v to fix the OU related Bug 5350882 on 21-Jul-06
6478        Also added the inv_org_id related OU filter */
6479 
6480     SELECT IV.vendor_certification_id,
6481            IV.rank
6482       FROM ahl_inv_service_item_rels SI,
6483            ahl_item_vendor_rels IV,
6484            ahl_vendor_certifications_v VC
6485      WHERE SI.inv_service_item_rel_id = IV.inv_service_item_rel_id
6486        AND IV.vendor_certification_id = VC.vendor_certification_id
6487        AND SI.inv_item_id = c_inv_item_id
6488        AND SI.inv_org_id = c_inv_org_id
6489        AND SI.service_item_id = c_service_item_id
6490        AND trunc(SI.active_start_date) <= trunc(SYSDATE)
6491        AND trunc(nvl(SI.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
6492        AND trunc(IV.active_start_date) <= trunc(SYSDATE)
6493        AND trunc(nvl(IV.active_end_date, SYSDATE+1)) > trunc(SYSDATE)
6494        AND trunc(VC.active_start_date) <= trunc(SYSDATE)
6495        AND trunc(nvl(VC.active_end_date, SYSDATE+1)) > trunc(SYSDATE);
6496 
6497   CURSOR get_vendor_ids(c_vendor_cert_id NUMBER) IS
6498     SELECT vendor_id,
6499            vendor_site_id,
6500            vendor_contact_id
6501       FROM ahl_vendor_certifications_v
6502      WHERE vendor_certification_id = c_vendor_cert_id
6503        AND trunc(active_start_date) <= trunc(SYSDATE)
6504        AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
6505   TYPE vendor_rank_count_rec_type IS RECORD (
6506     rank_sum        NUMBER, --the summary of ranks for all the vendor_certification_ids
6507     count_num       NUMBER);--if count_num = p_item_service_rels_tbl.count then the vendor_cert_id is common
6508   --The index of the record type is vendor_certification_id
6509   TYPE vendor_rank_count_tbl_type IS TABLE OF vendor_rank_count_rec_type INDEX BY BINARY_INTEGER;
6510   l_vendor_rank_count_tbl vendor_rank_count_tbl_type;
6511   i                     NUMBER;
6512   l_first_index         NUMBER;
6513   l_tbl_index           NUMBER;
6514   l_vendor_index        NUMBER;
6515   l_temp_index          NUMBER;
6516   l_temp_vendor_cert_id NUMBER;
6517   l_temp_rank           NUMBER;
6518   l_temp_rank_sum       NUMBER;
6519   l_vendor_cert_id      NUMBER;
6520   l_common_exists_flag  BOOLEAN;
6521 
6522   l_temp_vendor_index   NUMBER := 0;
6523   l_dummy_contact_id    NUMBER;
6524 BEGIN
6525   --This whole logic depends on the assumption that rank is mandatory in table
6526   --ahl_item_vendor_rels
6527   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6528     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
6529                    G_LOG_PREFIX || 'derive_default_vendor',
6530                    'Procedure begins and count='||p_item_service_rels_tbl.count);
6531   END IF;
6532   x_vendor_id := NULL;
6533   x_vendor_site_id := NULL;
6534   x_vendor_contact_id := NULL;
6535   IF (p_item_service_rels_tbl.count > 0) THEN
6536     --get the vendor_cert_id set for the first physical/service item combination record
6537     --and take it as the base for comparing with other sets to seek the common vendor_cert_id
6538     l_tbl_index := p_item_service_rels_tbl.FIRST;
6539     FOR l_get_vendor_cert IN get_vendor_cert(p_item_service_rels_tbl(l_tbl_index).inv_item_id,
6540                                              p_item_service_rels_tbl(l_tbl_index).inv_org_id,
6541                                              p_item_service_rels_tbl(l_tbl_index).service_item_id) LOOP
6542       l_first_index := l_get_vendor_cert.vendor_certification_id;
6543       l_vendor_rank_count_tbl(l_first_index).rank_sum := l_get_vendor_cert.rank;
6544       l_vendor_rank_count_tbl(l_first_index).count_num := 1;
6545     END LOOP;
6546     --Loop through other vendor_cert_id sets of all the other physical/service item combination records
6547     --Counting the appearing times and suming their ranks in all the vendor_cert_id sets of each
6548     --vendor_cert_id in the base set. By this rule we could find the common vendor_cert_id
6549     --with the highest rank.
6550     IF (l_vendor_rank_count_tbl.count > 0) THEN
6551       FOR i IN (l_tbl_index+1)..p_item_service_rels_tbl.LAST LOOP
6552         l_common_exists_flag := FALSE;
6553         FOR l_get_vendor_cert IN get_vendor_cert(p_item_service_rels_tbl(i).inv_item_id,
6554                                                  p_item_service_rels_tbl(i).inv_org_id,
6555                                                  p_item_service_rels_tbl(i).service_item_id) LOOP
6556           l_temp_index := l_get_vendor_cert.vendor_certification_id;
6557           l_temp_rank := l_get_vendor_cert.rank;
6558           IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6559             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
6560                    G_LOG_PREFIX || 'derive_default_vendor',
6561                    'l_temp_index='||l_temp_index||
6562                    'l_temp_rank='||l_temp_rank);
6563           END IF;
6564           IF l_vendor_rank_count_tbl.EXISTS(l_temp_index) THEN
6565             l_common_exists_flag := TRUE;
6566             l_vendor_rank_count_tbl(l_temp_index).rank_sum := l_vendor_rank_count_tbl(l_temp_index).rank_sum + l_temp_rank;
6567             l_vendor_rank_count_tbl(l_temp_index).count_num := l_vendor_rank_count_tbl(l_temp_index).count_num + 1;
6568           END IF;
6569         END LOOP;
6570         IF NOT l_common_exists_flag THEN
6571           RETURN;
6572         END IF;
6573       END LOOP;
6574       --Pick up the common vendor_cert_id (exists in all the sets) with
6575       --the highest(least) summary rank
6576       l_vendor_cert_id := NULL;
6577       l_temp_rank_sum := FND_API.G_MISS_NUM; --Just want to use a big positive number
6578       IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6579         FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
6580                    G_LOG_PREFIX || 'derive_default_vendor',
6581                    'Procedure begins and count2='||l_vendor_rank_count_tbl.count||
6582                    'first='||l_vendor_rank_count_tbl.first||'last='||l_vendor_rank_count_tbl.last);
6583       END IF;
6584       l_vendor_index := l_vendor_rank_count_tbl.FIRST;  -- get subscript of first element
6585       --You can't use FOR l_vendor_index IN l_vendor_rank_count_tbl.FIRST..l_vendor_rank_count_tbl.LAST here
6586       WHILE l_vendor_index IS NOT NULL LOOP
6587         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6588           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
6589                    G_LOG_PREFIX || 'derive_default_vendor',
6590                    'l_vendor_index='||l_vendor_index);
6591         END IF;
6592         IF (l_vendor_rank_count_tbl(l_vendor_index).count_num = p_item_service_rels_tbl.count) THEN
6593           -- Make a note of this common vendor
6594           l_temp_vendor_index := l_temp_vendor_index + 1;
6595           x_valid_vendors_tbl(l_temp_vendor_index).vendor_id := l_vendor_index;
6596           IF (l_vendor_rank_count_tbl(l_vendor_index).rank_sum < l_temp_rank_sum) THEN
6597             -- Current Best Rank
6598             l_vendor_cert_id := l_vendor_index;
6599             l_temp_rank_sum := l_vendor_rank_count_tbl(l_vendor_index).rank_sum;
6600           END IF;
6601         END IF;
6602         l_vendor_index := l_vendor_rank_count_tbl.NEXT(l_vendor_index);  -- get subscript of next element
6603       END LOOP;
6604 
6605       OPEN get_vendor_ids(l_vendor_cert_id);
6606       FETCH get_vendor_ids INTO x_vendor_id, x_vendor_site_id, x_vendor_contact_id;
6607       CLOSE get_vendor_ids;
6608 
6609       -- Convert All Certification Ids to Vendor/Vendor Location Ids
6610       IF x_valid_vendors_tbl.count > 0 THEN
6611         FOR i IN x_valid_vendors_tbl.FIRST..x_valid_vendors_tbl.LAST LOOP
6612           OPEN get_vendor_ids(x_valid_vendors_tbl(i).vendor_id);
6613           FETCH get_vendor_ids INTO x_valid_vendors_tbl(i).vendor_id, x_valid_vendors_tbl(i).vendor_site_id, l_dummy_contact_id;
6614           CLOSE get_vendor_ids;
6615         END LOOP;
6616       END IF;
6617     END IF;
6618   END IF;
6619   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6620     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
6621                    G_LOG_PREFIX || 'derive_default_vendor',
6622                    'Procedure exits normallly');
6623   END IF;
6624 END derive_default_vendor;
6625 
6626 --This procedure tries to derive the common vendors for an OSP Order based on its Lines.
6627 --If the x_any_vendor_flag is 'Y', it means that any vendor can be used for this Order.
6628 --Else, only the vendors whose ids are returned in the table x_valid_vendors_tbl
6629 --are valid. It uses the derive_default_vendor method.
6630 PROCEDURE derive_common_vendors(p_osp_order_id      IN  NUMBER,
6631                                 x_valid_vendors_tbl OUT NOCOPY Vendor_id_tbl_type,
6632                                 x_any_vendor_flag   OUT NOCOPY VARCHAR2) IS
6633 
6634   CURSOR item_service_dtls_csr IS
6635     SELECT INVENTORY_ORG_ID, INVENTORY_ITEM_ID, SERVICE_ITEM_ID FROM ahl_osp_order_lines
6636     WHERE osp_order_id = p_osp_order_id AND
6637           service_item_id IS NOT NULL;
6638 
6639   CURSOR is_vendor_defined_csr (c_inventory_item_id IN NUMBER,
6640                                 c_service_item_id   IN NUMBER) IS
6641     SELECT 1
6642 	FROM ahl_inv_service_item_rels isr,
6643 	     ahl_item_vendor_rels ivr
6644     WHERE isr.inv_item_id = c_inventory_item_id AND
6645           isr.service_item_id = c_service_item_id AND
6646           isr.inv_service_item_rel_id = ivr.inv_service_item_rel_id;
6647 
6648    L_DEBUG_KEY   CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.Derive_Common_Vendors';
6649    l_temp_number NUMBER;
6650    l_item_dtls_tbl item_service_rels_tbl_type;
6651    l_item_dtls_rec item_service_rel_rec_type;
6652    l_temp_index NUMBER := 0;
6653    l_dummy_vendor_id     NUMBER;
6654    l_dummy_site_id       NUMBER;
6655    l_dummy_contact_id    NUMBER;
6656 
6657 BEGIN
6658 
6659   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6660     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Entering Procedure. p_osp_order_id = ' || p_osp_order_id);
6661   END IF;
6662 
6663   x_any_vendor_flag := 'N';
6664 
6665   OPEN item_service_dtls_csr;
6666   LOOP
6667     FETCH item_service_dtls_csr INTO l_item_dtls_rec;
6668     EXIT WHEN item_service_dtls_csr%NOTFOUND;
6669     -- Check if a Vendor exists for this item/service
6670     OPEN is_vendor_defined_csr(l_item_dtls_rec.inv_item_id, l_item_dtls_rec.service_item_id);
6671     FETCH is_vendor_defined_csr INTO l_temp_number;
6672     IF (is_vendor_defined_csr%FOUND) THEN
6673       -- There is a vendor defined for this item/svc association
6674       l_temp_index := l_temp_index + 1;  -- One based index
6675       -- Populate the table with this Item Association Details
6676       l_item_dtls_tbl(l_temp_index) := l_item_dtls_rec;
6677     END IF;
6678     CLOSE is_vendor_defined_csr;
6679   END LOOP;
6680   CLOSE item_service_dtls_csr;
6681 
6682   IF (l_temp_index = 0) THEN
6683     -- All OSP lines have only one-time items or no vendor is defined for any item/svc combination
6684 	-- In such a case, Can use any Vendor
6685     x_any_vendor_flag := 'Y';
6686     RETURN;
6687   END IF;
6688 
6689   -- Now try to get the common Vendor(s) for the associations in l_item_dtls_tbl
6690 
6691   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6692     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Trying to get the common vendors for ' || l_item_dtls_tbl.COUNT || ' associations.');
6693   END IF;
6694 
6695   derive_default_vendor(p_item_service_rels_tbl => l_item_dtls_tbl,
6696                         x_vendor_id             => l_dummy_vendor_id,
6697                         x_vendor_site_id        => l_dummy_site_id,
6698                         x_vendor_contact_id     => l_dummy_contact_id,
6699                         x_valid_vendors_tbl     => x_valid_vendors_tbl);
6700 
6701   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6702     FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'Total number of common vendors: ' || x_valid_vendors_tbl.COUNT);
6703   END IF;
6704 
6705 
6706   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6707     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'Exiting Procedure');
6708   END IF;
6709 
6710 END derive_common_vendors;
6711 
6712 --Newly created to validate vendor_id, vendor_site_id and
6713 --vendor_contact_id together(crosss attribute validation)
6714 PROCEDURE validate_vendor_site_contact(p_vendor_id IN NUMBER,
6715                                        p_vendor_site_id IN NUMBER,
6716                                        p_vendor_contact_id IN NUMBER)
6717 IS
6718   CURSOR val_vendor_id_csr IS
6719     SELECT 'X'
6720      FROM po_vendors_view
6721     WHERE vendor_id = p_vendor_id
6722       AND enabled_flag = G_YES_FLAG
6723       AND TRUNC(NVL(vendor_start_date_active, SYSDATE - 1)) <= TRUNC(SYSDATE)
6724       AND TRUNC(NVL(vendor_end_date_active, SYSDATE + 1)) > TRUNC(SYSDATE);
6725   CURSOR get_vendor_cert IS
6726     SELECT 'X'
6727       FROM ahl_vendor_certifications_v
6728      WHERE vendor_id = p_vendor_id
6729        AND TRUNC(active_start_date) <= TRUNC(SYSDATE)
6730        AND TRUNC(nvl(active_end_date, SYSDATE+1)) > TRUNC(SYSDATE);
6731   CURSOR val_vendor_site_id_csr IS
6732     SELECT 'X'
6733      FROM po_vendor_sites
6734     WHERE vendor_site_id = p_vendor_site_id
6735       AND vendor_id = p_vendor_id
6736       AND TRUNC(NVL(inactive_date, SYSDATE + 1)) > TRUNC(SYSDATE)
6737       AND purchasing_site_flag = G_YES_FLAG
6738       AND NVL(RFQ_ONLY_SITE_FLAG, G_NO_FLAG) =G_NO_FLAG ;
6739   CURSOR get_site_cert IS
6740     SELECT 'X'
6741       FROM ahl_vendor_certifications_v
6742      WHERE vendor_id = p_vendor_id
6743        AND vendor_site_id = p_vendor_site_id
6744        AND TRUNC(active_start_date) <= TRUNC(SYSDATE)
6745        AND TRUNC(nvl(active_end_date, SYSDATE+1)) > TRUNC(SYSDATE);
6746   CURSOR val_vendor_contact_id_csr IS
6747     SELECT 'X'
6748      FROM po_vendor_contacts
6749     WHERE vendor_site_id = p_vendor_site_id
6750       AND vendor_contact_id = p_vendor_contact_id
6751       AND TRUNC(NVL(inactive_date, SYSDATE+1)) > TRUNC(SYSDATE);
6752   CURSOR get_contact_cert IS
6753     SELECT 'X'
6754      FROM ahl_vendor_certifications
6755     WHERE vendor_contact_id = p_vendor_contact_id
6756       AND TRUNC(active_start_date) <= TRUNC(SYSDATE)
6757       AND TRUNC(NVL(active_end_date, SYSDATE+1)) > TRUNC(SYSDATE);
6758     l_exist VARCHAR2(1);
6759     L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.validate_vendor_site_contact';
6760 BEGIN
6761   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6762     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Procedure');
6763   END IF;
6764   IF (p_vendor_id IS NULL) THEN
6765     IF (p_vendor_site_id IS NOT NULL OR p_vendor_contact_id IS NOT NULL) THEN
6766       FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENDOR_ID_INV');
6767       FND_MESSAGE.Set_Token('VENDOR_ID', p_vendor_id);
6768       FND_MSG_PUB.ADD;
6769     END IF;
6770   ELSE
6771     OPEN val_vendor_id_csr;
6772     FETCH val_vendor_id_csr INTO l_exist;
6773     IF(val_vendor_id_csr%NOTFOUND) THEN
6774       FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENDOR_ID_INV');
6775       FND_MESSAGE.Set_Token('VENDOR_ID', p_vendor_id);
6776       FND_MSG_PUB.ADD;
6777     END IF;
6778     CLOSE val_vendor_id_csr;
6779     OPEN get_vendor_cert;
6780     FETCH get_vendor_cert INTO l_exist;
6781     IF(get_vendor_cert%NOTFOUND) THEN
6782       FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENDOR_ID_INV');
6783       FND_MESSAGE.Set_Token('VENDOR_ID', p_vendor_id);
6784       FND_MSG_PUB.ADD;
6785     END IF;
6786     CLOSE get_vendor_cert;
6787     IF (p_vendor_site_id IS NULL AND p_vendor_contact_id IS NOT NULL) THEN
6788       FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENSITE_ID_INV');
6789       FND_MESSAGE.Set_Token('VENDOR_SITE_ID', p_vendor_site_id);
6790       FND_MSG_PUB.ADD;
6791     ELSIF (p_vendor_site_id IS NOT NULL) THEN
6792       OPEN val_vendor_site_id_csr;
6793       FETCH val_vendor_site_id_csr INTO l_exist;
6794       IF(val_vendor_site_id_csr%NOTFOUND) THEN
6795         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENSITE_ID_INV');
6796         FND_MESSAGE.Set_Token('VENDOR_SITE_ID', p_vendor_site_id);
6797         FND_MSG_PUB.ADD;
6798       END IF;
6799       CLOSE val_vendor_site_id_csr;
6800       OPEN get_site_cert;
6801       FETCH get_site_cert INTO l_exist;
6802       IF(get_site_cert%NOTFOUND) THEN
6803         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENSITE_ID_INV');
6804         FND_MESSAGE.Set_Token('VENDOR_SITE_ID', p_vendor_site_id);
6805         FND_MSG_PUB.ADD;
6806       END IF;
6807       CLOSE get_site_cert;
6808       IF (p_vendor_contact_id IS NOT NULL) THEN
6809         OPEN val_vendor_contact_id_csr;
6810         FETCH val_vendor_contact_id_csr INTO l_exist;
6811         IF(val_vendor_contact_id_csr%NOTFOUND) THEN
6812           FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VEN_CONTACT_ID_INV');
6813           FND_MESSAGE.Set_Token('VENDOR_CONTACT_ID', p_vendor_contact_id);
6814           FND_MSG_PUB.ADD;
6815         END IF;
6816         CLOSE val_vendor_contact_id_csr;
6817         OPEN get_contact_cert;
6818         FETCH get_contact_cert INTO l_exist;
6819         IF(get_contact_cert%NOTFOUND) THEN
6820           FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VEN_CONTACT_ID_INV');
6821           FND_MESSAGE.Set_Token('VENDOR_CONTACT_ID', p_vendor_contact_id);
6822           FND_MSG_PUB.ADD;
6823         END IF;
6824         CLOSE get_contact_cert;
6825       END IF;
6826     END IF;
6827   END IF;
6828 
6829   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6830     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'End Procedure');
6831   END IF;
6832 END validate_vendor_site_contact;
6833 
6834 --Copied from original default_unchanged_order_lines, so some inherited logic
6835 --may not be necessary. It is applicable to both Update and Create
6836 PROCEDURE default_unchanged_order_line(p_x_osp_order_line_rec IN OUT NOCOPY osp_order_line_rec_type)
6837 IS
6838   CURSOR osp_order_lines_csr IS
6839   --Updated by mpothuku on 27-Feb-06 to fix the Perf Bug #4919164
6840   /*
6841     SELECT osp_order_id,
6842            osp_line_number,
6843            status_code, --status is not defined in the record because user can't directly change the status
6844            po_line_type_id, --no po_line_type here because it is not changeable once it is created(from a profile option)
6845            service_item_id,
6846            service_item_number,
6847            service_item_description,
6848            service_item_uom_code,
6849            quantity,
6850            need_by_date,
6851            ship_by_date,
6852            po_line_id,
6853            oe_ship_line_id,
6854            oe_return_line_id,
6855            workorder_id,
6856            job_number,
6857            operation_id, --this attribute should be deleted from the table, so ignore it
6858            exchange_instance_id,
6859            exchange_instance_number,
6860            inventory_item_id,
6861            inventory_org_id,
6862            item_number,
6863            inventory_item_uom,
6864            inventory_item_quantity,
6865            sub_inventory,
6866            lot_number,
6867            serial_number,
6868            attribute_category,
6869            attribute1,
6870            attribute2,
6871            attribute3,
6872            attribute4,
6873            attribute5,
6874            attribute6,
6875            attribute7,
6876            attribute8,
6877            attribute9,
6878            attribute10,
6879            attribute11,
6880            attribute12,
6881            attribute13,
6882            attribute14,
6883            attribute15
6884     FROM ahl_osp_order_lines_v
6885     WHERE osp_order_line_id = p_x_osp_order_line_rec.osp_order_line_id
6886     AND object_version_number= p_x_osp_order_line_rec.object_version_number;
6887  */
6888     SELECT ospl.osp_order_id,
6889             ospl.osp_line_number,
6890             ospl.status_code, --status is not defined in the record because user can't directly change the status
6891             ospl.po_line_type_id, --no po_line_type here because it is not changeable once it is created(from a profile option)
6892             ospl.service_item_id,
6893             (select mtlsvc.concatenated_segments
6894               from mtl_system_items_kfv mtlsvc
6895              where mtlsvc.inventory_item_id = ospl.service_item_id
6896                and mtlsvc.organization_id = decode(ospl.workorder_id, null, ospl.inventory_org_id, vst.organization_id)
6897             )service_item_number,
6898             ospl.service_item_description,
6899             --modified by mpothuku on 14-mar-2008 to fix the Bug 6885513
6900             /*
6901             decode(ospl.po_line_id, null, ospl.service_item_uom_code, mtluom.uom_code )service_item_uom_code,
6902             decode(ospl.po_line_id, null, ospl.quantity, pl.quantity )quantity,
6903             */
6904             ospl.service_item_uom_code,
6905             ospl.quantity,
6906             --mpothuku End
6907             ospl.need_by_date,
6908             ospl.ship_by_date,
6909             ospl.po_line_id,
6910             ospl.oe_ship_line_id,
6911             ospl.oe_return_line_id,
6912             ospl.workorder_id,
6913             wo.workorder_name job_number,
6914             ospl.operation_id, --this attribute should be deleted from the table, so ignore it
6915             ospl.exchange_instance_id,
6916             csiex.instance_number exchange_instance_number,
6917             decode(ospl.workorder_id, null, ospl.inventory_item_id, vts.inventory_item_id)inventory_item_id,
6918             --Fix for the regression issue by mpothuku on 28th August 2006, changed the item_id to org_id in decode below
6919             decode(ospl.workorder_id, null, ospl.inventory_org_id, vst.organization_id) inventory_org_id,
6920             (select mtli.concatenated_segments
6921               from mtl_system_items_kfv mtli
6922              where mtli.inventory_item_id = ospl.inventory_item_id
6923                and mtli.organization_id = decode(ospl.workorder_id, null, ospl.inventory_org_id, vst.organization_id)
6924             )item_number,
6925             decode(ospl.workorder_id, null, ospl.inventory_item_uom, csiwo.unit_of_measure)inventory_item_uom,
6926             decode(ospl.workorder_id, null, ospl.inventory_item_quantity, csiwo.quantity)inventory_item_quantity,
6927             decode(ospl.workorder_id, null, ospl.sub_inventory, null)sub_inventory,
6928             decode(ospl.workorder_id, null, ospl.lot_number, csiwo.lot_number)lot_number,
6929             decode(ospl.workorder_id, null, ospl.serial_number, csiwo.serial_number)serial_number,
6930             -- Added by jaramana on January 10, 2008 for the Requisition ER 6034236
6931             po_req_line_id,
6932             -- jaramana End
6933             ospl.attribute_category,
6934             ospl.attribute1,
6935             ospl.attribute2,
6936             ospl.attribute3,
6937             ospl.attribute4,
6938             ospl.attribute5,
6939             ospl.attribute6,
6940             ospl.attribute7,
6941             ospl.attribute8,
6942             ospl.attribute9,
6943             ospl.attribute10,
6944             ospl.attribute11,
6945             ospl.attribute12,
6946             ospl.attribute13,
6947             ospl.attribute14,
6948             ospl.attribute15
6949        FROM ahl_osp_order_lines ospl,
6950             ahl_workorders wo,
6951             ahl_visits_b vst,
6952             ahl_visit_tasks_b vts,
6953             csi_item_instances csiwo,
6954             csi_item_instances csiex,
6955             --po_lines_all pl,
6956             mtl_units_of_measure_tl mtluom
6957       WHERE ospl.osp_order_line_id = p_x_osp_order_line_rec.osp_order_line_id
6958         AND ospl.object_version_number= p_x_osp_order_line_rec.object_version_number
6959         AND ospl.workorder_id = wo.workorder_id(+)
6960         AND wo.visit_task_id = vts.visit_task_id(+)
6961         AND vts.visit_id = vst.visit_id(+)
6962         AND vts.instance_id = csiwo.instance_id(+)
6963         AND NVL (ospl.status_code, 'ENTERED') <> 'PO_DELETED'
6964         --modified by mpothuku on 14-mar-2008 to fix the Bug 6885513
6965         /*
6966         AND ospl.po_line_id = pl.po_line_id(+)
6967         AND pl.unit_meas_lookup_code = mtluom.unit_of_measure(+)
6968         AND mtluom.language(+) = USERENV('LANG')
6969         */
6970         --mpothuku End
6971         AND csiex.instance_id(+) = ospl.exchange_instance_id;
6972 
6973     l_osp_order_line_rec osp_order_lines_csr%ROWTYPE;
6974     L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.default_unchanged_order_lines';
6975 BEGIN
6976     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
6977       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Procedure');
6978     END IF;
6979     IF(p_x_osp_order_line_rec.operation_flag = G_OP_UPDATE) THEN
6980       OPEN osp_order_lines_csr;
6981       FETCH osp_order_lines_csr INTO l_osp_order_line_rec;
6982       IF (osp_order_lines_csr%NOTFOUND) THEN
6983         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_INVOP_OSP_LN_NFOUND');
6984         FND_MSG_PUB.ADD;
6985       ELSE
6986         IF (p_x_osp_order_line_rec.osp_order_id IS NULL) THEN
6987           p_x_osp_order_line_rec.osp_order_id := l_osp_order_line_rec.osp_order_id;
6988         ELSIF(p_x_osp_order_line_rec.osp_order_id = FND_API.G_MISS_NUM) THEN
6989           p_x_osp_order_line_rec.osp_order_id := null;
6990         END IF;
6991         IF (p_x_osp_order_line_rec.osp_line_number IS NULL) THEN
6992           p_x_osp_order_line_rec.osp_line_number := l_osp_order_line_rec.osp_line_number;
6993         ELSIF(p_x_osp_order_line_rec.osp_line_number = FND_API.G_MISS_NUM) THEN
6994           p_x_osp_order_line_rec.osp_line_number := null;
6995         END IF;
6996         IF (p_x_osp_order_line_rec.status_code IS NULL) THEN
6997           p_x_osp_order_line_rec.status_code := l_osp_order_line_rec.status_code;
6998         ELSIF(p_x_osp_order_line_rec.status_code = FND_API.G_MISS_CHAR) THEN
6999           p_x_osp_order_line_rec.status_code := null;
7000         END IF;
7001         IF (p_x_osp_order_line_rec.need_by_date IS NULL) THEN
7002           p_x_osp_order_line_rec.need_by_date := l_osp_order_line_rec.need_by_date;
7003         ELSIF(p_x_osp_order_line_rec.need_by_date = FND_API.G_MISS_DATE) THEN
7004           p_x_osp_order_line_rec.need_by_date := null;
7005         END IF;
7006         IF (p_x_osp_order_line_rec.ship_by_date IS NULL) THEN
7007           p_x_osp_order_line_rec.ship_by_date := l_osp_order_line_rec.ship_by_date;
7008         ELSIF(p_x_osp_order_line_rec.ship_by_date = FND_API.G_MISS_DATE) THEN
7009           p_x_osp_order_line_rec.ship_by_date := null;
7010         END IF;
7011         IF (p_x_osp_order_line_rec.service_item_id IS NULL) THEN
7012           p_x_osp_order_line_rec.service_item_id := l_osp_order_line_rec.service_item_id;
7013         ELSIF(p_x_osp_order_line_rec.service_item_id = FND_API.G_MISS_NUM) THEN
7014           p_x_osp_order_line_rec.service_item_id := null;
7015         END IF;
7016         IF (p_x_osp_order_line_rec.service_item_number IS NULL) THEN
7017           p_x_osp_order_line_rec.service_item_number := l_osp_order_line_rec.service_item_number;
7018         ELSIF(p_x_osp_order_line_rec.service_item_number = FND_API.G_MISS_CHAR) THEN
7019           p_x_osp_order_line_rec.service_item_number := null;
7020         END IF;
7021         IF (p_x_osp_order_line_rec.service_item_description IS NULL) THEN
7022           p_x_osp_order_line_rec.service_item_description := l_osp_order_line_rec.service_item_description;
7023         ELSIF(p_x_osp_order_line_rec.service_item_description = FND_API.G_MISS_CHAR) THEN
7024           p_x_osp_order_line_rec.service_item_description := null;
7025         END IF;
7026         IF (p_x_osp_order_line_rec.service_item_uom_code IS NULL) THEN
7027           p_x_osp_order_line_rec.service_item_uom_code := l_osp_order_line_rec.service_item_uom_code;
7028         ELSIF(p_x_osp_order_line_rec.service_item_uom_code = FND_API.G_MISS_CHAR) THEN
7029           p_x_osp_order_line_rec.service_item_uom_code := null;
7030         END IF;
7031         IF (p_x_osp_order_line_rec.quantity IS NULL) THEN
7032           p_x_osp_order_line_rec.quantity := l_osp_order_line_rec.quantity;
7033         ELSIF(p_x_osp_order_line_rec.quantity = FND_API.G_MISS_NUM) THEN
7034           p_x_osp_order_line_rec.quantity := null;
7035         END IF;
7036         IF (p_x_osp_order_line_rec.po_line_type_id IS NULL) THEN
7037           p_x_osp_order_line_rec.po_line_type_id := l_osp_order_line_rec.po_line_type_id;
7038         ELSIF(p_x_osp_order_line_rec.po_line_type_id = FND_API.G_MISS_NUM) THEN
7039           p_x_osp_order_line_rec.po_line_type_id := null;
7040         END IF;
7041         IF (p_x_osp_order_line_rec.po_line_id IS NULL) THEN
7042           p_x_osp_order_line_rec.po_line_id := l_osp_order_line_rec.po_line_id;
7043         ELSIF(p_x_osp_order_line_rec.po_line_id = FND_API.G_MISS_NUM) THEN
7044           p_x_osp_order_line_rec.po_line_id := null;
7045         END IF;
7046         IF (p_x_osp_order_line_rec.oe_ship_line_id IS NULL) THEN
7047           p_x_osp_order_line_rec.oe_ship_line_id := l_osp_order_line_rec.oe_ship_line_id;
7048         ELSIF(p_x_osp_order_line_rec.oe_ship_line_id = FND_API.G_MISS_NUM) THEN
7049           p_x_osp_order_line_rec.oe_ship_line_id := null;
7050         END IF;
7051         IF (p_x_osp_order_line_rec.oe_return_line_id IS NULL) THEN
7052           p_x_osp_order_line_rec.oe_return_line_id := l_osp_order_line_rec.oe_return_line_id;
7053         ELSIF(p_x_osp_order_line_rec.oe_return_line_id = FND_API.G_MISS_NUM) THEN
7054           p_x_osp_order_line_rec.oe_return_line_id := null;
7055         END IF;
7056         IF (p_x_osp_order_line_rec.workorder_id IS NULL) THEN
7057           p_x_osp_order_line_rec.workorder_id := l_osp_order_line_rec.workorder_id;
7058         ELSIF(p_x_osp_order_line_rec.workorder_id = FND_API.G_MISS_NUM) THEN
7059           p_x_osp_order_line_rec.workorder_id := null;
7060         END IF;
7061         IF (p_x_osp_order_line_rec.job_number IS NULL) THEN
7062           p_x_osp_order_line_rec.job_number := l_osp_order_line_rec.job_number;
7063         ELSIF(p_x_osp_order_line_rec.job_number = FND_API.G_MISS_CHAR) THEN
7064           p_x_osp_order_line_rec.job_number := null;
7065         END IF;
7066         IF (p_x_osp_order_line_rec.exchange_instance_id IS NULL) THEN
7067           p_x_osp_order_line_rec.exchange_instance_id := l_osp_order_line_rec.exchange_instance_id;
7068         ELSIF(p_x_osp_order_line_rec.exchange_instance_id = FND_API.G_MISS_NUM) THEN
7069           p_x_osp_order_line_rec.exchange_instance_id := null;
7070         END IF;
7071         IF (p_x_osp_order_line_rec.exchange_instance_number IS NULL) THEN
7072           p_x_osp_order_line_rec.exchange_instance_number := l_osp_order_line_rec.exchange_instance_number;
7073         ELSIF(p_x_osp_order_line_rec.exchange_instance_number = FND_API.G_MISS_CHAR) THEN
7074           p_x_osp_order_line_rec.exchange_instance_number := null;
7075         END IF;
7076         IF (p_x_osp_order_line_rec.inventory_item_id IS NULL) THEN
7077           p_x_osp_order_line_rec.inventory_item_id := l_osp_order_line_rec.inventory_item_id;
7078         ELSIF(p_x_osp_order_line_rec.inventory_item_id = FND_API.G_MISS_NUM) THEN
7079           p_x_osp_order_line_rec.inventory_item_id := null;
7080         END IF;
7081         IF (p_x_osp_order_line_rec.inventory_org_id IS NULL) THEN
7082           p_x_osp_order_line_rec.inventory_org_id := l_osp_order_line_rec.inventory_org_id;
7083         ELSIF(p_x_osp_order_line_rec.inventory_org_id = FND_API.G_MISS_NUM) THEN
7084           p_x_osp_order_line_rec.inventory_org_id := null;
7085         END IF;
7086         IF (p_x_osp_order_line_rec.item_number IS NULL) THEN
7087           p_x_osp_order_line_rec.item_number := l_osp_order_line_rec.item_number;
7088         ELSIF(p_x_osp_order_line_rec.item_number = FND_API.G_MISS_CHAR) THEN
7089           p_x_osp_order_line_rec.item_number := null;
7090         END IF;
7091         IF (p_x_osp_order_line_rec.inventory_item_uom IS NULL) THEN
7092           p_x_osp_order_line_rec.inventory_item_uom := l_osp_order_line_rec.inventory_item_uom;
7093         ELSIF(p_x_osp_order_line_rec.inventory_item_uom = FND_API.G_MISS_CHAR) THEN
7094           p_x_osp_order_line_rec.inventory_item_uom := null;
7095         END IF;
7096         IF (p_x_osp_order_line_rec.inventory_item_quantity IS NULL) THEN
7097           p_x_osp_order_line_rec.inventory_item_quantity := l_osp_order_line_rec.inventory_item_quantity;
7098         ELSIF(p_x_osp_order_line_rec.inventory_item_quantity = FND_API.G_MISS_NUM) THEN
7099           p_x_osp_order_line_rec.inventory_item_quantity := null;
7100         END IF;
7101         IF (p_x_osp_order_line_rec.sub_inventory IS NULL) THEN
7102           p_x_osp_order_line_rec.sub_inventory := l_osp_order_line_rec.sub_inventory;
7103         ELSIF(p_x_osp_order_line_rec.sub_inventory = FND_API.G_MISS_CHAR) THEN
7104           p_x_osp_order_line_rec.sub_inventory := null;
7105         END IF;
7106         IF (p_x_osp_order_line_rec.lot_number IS NULL) THEN
7107           p_x_osp_order_line_rec.lot_number := l_osp_order_line_rec.lot_number;
7108         ELSIF(p_x_osp_order_line_rec.lot_number = FND_API.G_MISS_CHAR) THEN
7109           p_x_osp_order_line_rec.lot_number := null;
7110         END IF;
7111         IF (p_x_osp_order_line_rec.serial_number IS NULL) THEN
7112           p_x_osp_order_line_rec.serial_number := l_osp_order_line_rec.serial_number;
7113         ELSIF(p_x_osp_order_line_rec.serial_number = FND_API.G_MISS_CHAR) THEN
7114           p_x_osp_order_line_rec.serial_number := null;
7115         END IF;
7116 
7117         -- Added by jaramana on January 10, 2008 for the Requisition ER 6034236
7118         IF (p_x_osp_order_line_rec.po_req_line_id IS NULL) THEN
7119           p_x_osp_order_line_rec.po_req_line_id := l_osp_order_line_rec.po_req_line_id;
7120         ELSIF(p_x_osp_order_line_rec.po_req_line_id = FND_API.G_MISS_NUM) THEN
7121           p_x_osp_order_line_rec.po_req_line_id := null;
7122         END IF;
7123         -- jaramana End
7124 
7125         IF (p_x_osp_order_line_rec.attribute_category IS NULL) THEN
7126           p_x_osp_order_line_rec.attribute_category := l_osp_order_line_rec.attribute_category;
7127         ELSIF(p_x_osp_order_line_rec.attribute_category = FND_API.G_MISS_CHAR) THEN
7128           p_x_osp_order_line_rec.attribute_category := null;
7129         END IF;
7130         IF (p_x_osp_order_line_rec.attribute1 IS NULL) THEN
7131           p_x_osp_order_line_rec.attribute1 := l_osp_order_line_rec.attribute1;
7132         ELSIF(p_x_osp_order_line_rec.attribute1 = FND_API.G_MISS_CHAR) THEN
7133           p_x_osp_order_line_rec.attribute1 := null;
7134         END IF;
7135         IF (p_x_osp_order_line_rec.attribute2 IS NULL) THEN
7136           p_x_osp_order_line_rec.attribute2 := l_osp_order_line_rec.attribute2;
7137         ELSIF(p_x_osp_order_line_rec.attribute2 = FND_API.G_MISS_CHAR) THEN
7138           p_x_osp_order_line_rec.attribute2 := null;
7139         END IF;
7140         IF (p_x_osp_order_line_rec.attribute3 IS NULL) THEN
7141           p_x_osp_order_line_rec.attribute3 := l_osp_order_line_rec.attribute3;
7142         ELSIF(p_x_osp_order_line_rec.attribute3 = FND_API.G_MISS_CHAR) THEN
7143           p_x_osp_order_line_rec.attribute3 := null;
7144         END IF;
7145         IF (p_x_osp_order_line_rec.attribute4 IS NULL) THEN
7146           p_x_osp_order_line_rec.attribute4 := l_osp_order_line_rec.attribute4;
7147         ELSIF(p_x_osp_order_line_rec.attribute4 = FND_API.G_MISS_CHAR) THEN
7148           p_x_osp_order_line_rec.attribute4 := null;
7149         END IF;
7150         IF (p_x_osp_order_line_rec.attribute5 IS NULL) THEN
7151           p_x_osp_order_line_rec.attribute5 := l_osp_order_line_rec.attribute5;
7152         ELSIF(p_x_osp_order_line_rec.attribute5 = FND_API.G_MISS_CHAR) THEN
7153           p_x_osp_order_line_rec.attribute5 := null;
7154         END IF;
7155         IF (p_x_osp_order_line_rec.attribute6 IS NULL) THEN
7156           p_x_osp_order_line_rec.attribute6 := l_osp_order_line_rec.attribute6;
7157         ELSIF(p_x_osp_order_line_rec.attribute6 = FND_API.G_MISS_CHAR) THEN
7158           p_x_osp_order_line_rec.attribute6 := null;
7159         END IF;
7160         IF (p_x_osp_order_line_rec.attribute7 IS NULL) THEN
7161           p_x_osp_order_line_rec.attribute7 := l_osp_order_line_rec.attribute7;
7162         ELSIF(p_x_osp_order_line_rec.attribute7 = FND_API.G_MISS_CHAR) THEN
7163           p_x_osp_order_line_rec.attribute7 := null;
7164         END IF;
7165         IF (p_x_osp_order_line_rec.attribute8 IS NULL) THEN
7166           p_x_osp_order_line_rec.attribute8 := l_osp_order_line_rec.attribute8;
7167         ELSIF(p_x_osp_order_line_rec.attribute8 = FND_API.G_MISS_CHAR) THEN
7168           p_x_osp_order_line_rec.attribute8 := null;
7169         END IF;
7170         IF (p_x_osp_order_line_rec.attribute9 IS NULL) THEN
7171           p_x_osp_order_line_rec.attribute9 := l_osp_order_line_rec.attribute9;
7172         ELSIF(p_x_osp_order_line_rec.attribute9 = FND_API.G_MISS_CHAR) THEN
7173           p_x_osp_order_line_rec.attribute9 := null;
7174         END IF;
7175         IF (p_x_osp_order_line_rec.attribute10 IS NULL) THEN
7176           p_x_osp_order_line_rec.attribute10 := l_osp_order_line_rec.attribute10;
7177         ELSIF(p_x_osp_order_line_rec.attribute10 = FND_API.G_MISS_CHAR) THEN
7178           p_x_osp_order_line_rec.attribute10 := null;
7179         END IF;
7180         IF (p_x_osp_order_line_rec.attribute11 IS NULL) THEN
7181           p_x_osp_order_line_rec.attribute11 := l_osp_order_line_rec.attribute11;
7182         ELSIF(p_x_osp_order_line_rec.attribute11 = FND_API.G_MISS_CHAR) THEN
7183           p_x_osp_order_line_rec.attribute11 := null;
7184         END IF;
7185         IF (p_x_osp_order_line_rec.attribute12 IS NULL) THEN
7186           p_x_osp_order_line_rec.attribute12 := l_osp_order_line_rec.attribute12;
7187         ELSIF(p_x_osp_order_line_rec.attribute12 = FND_API.G_MISS_CHAR) THEN
7188           p_x_osp_order_line_rec.attribute12 := null;
7189         END IF;
7190         IF (p_x_osp_order_line_rec.attribute13 IS NULL) THEN
7191           p_x_osp_order_line_rec.attribute13 := l_osp_order_line_rec.attribute13;
7192         ELSIF(p_x_osp_order_line_rec.attribute13 = FND_API.G_MISS_CHAR) THEN
7193           p_x_osp_order_line_rec.attribute13 := null;
7194         END IF;
7195         IF (p_x_osp_order_line_rec.attribute14 IS NULL) THEN
7196           p_x_osp_order_line_rec.attribute14 := l_osp_order_line_rec.attribute14;
7197         ELSIF(p_x_osp_order_line_rec.attribute14 = FND_API.G_MISS_CHAR) THEN
7198           p_x_osp_order_line_rec.attribute14 := null;
7199         END IF;
7200         IF (p_x_osp_order_line_rec.attribute15 IS NULL) THEN
7201           p_x_osp_order_line_rec.attribute15 := l_osp_order_line_rec.attribute15;
7202         ELSIF(p_x_osp_order_line_rec.attribute15 = FND_API.G_MISS_CHAR) THEN
7203           p_x_osp_order_line_rec.attribute15 := null;
7204         END IF;
7205       END IF;
7206       CLOSE osp_order_lines_csr;
7207     ELSIF (p_x_osp_order_line_rec.operation_flag = G_OP_CREATE) THEN
7208       IF(p_x_osp_order_line_rec.osp_order_id = FND_API.G_MISS_NUM) THEN
7209         p_x_osp_order_line_rec.osp_order_id := null;
7210       END IF;
7211       IF(p_x_osp_order_line_rec.osp_line_number = FND_API.G_MISS_NUM) THEN
7212         p_x_osp_order_line_rec.osp_line_number := null;
7213       END IF;
7214       IF(p_x_osp_order_line_rec.status_code = FND_API.G_MISS_CHAR) THEN
7215         p_x_osp_order_line_rec.status_code := null;
7216       END IF;
7217       IF(p_x_osp_order_line_rec.need_by_date = FND_API.G_MISS_DATE) THEN
7218         p_x_osp_order_line_rec.need_by_date := null;
7219       END IF;
7220       IF(p_x_osp_order_line_rec.ship_by_date = FND_API.G_MISS_DATE) THEN
7221         p_x_osp_order_line_rec.ship_by_date := null;
7222       END IF;
7223       IF(p_x_osp_order_line_rec.service_item_id = FND_API.G_MISS_NUM) THEN
7224         p_x_osp_order_line_rec.service_item_id := null;
7225       END IF;
7226       IF(p_x_osp_order_line_rec.service_item_number = FND_API.G_MISS_CHAR) THEN
7227         p_x_osp_order_line_rec.service_item_number := null;
7228       END IF;
7229       IF(p_x_osp_order_line_rec.service_item_description = FND_API.G_MISS_CHAR) THEN
7230         p_x_osp_order_line_rec.service_item_description := null;
7231       END IF;
7232       IF(p_x_osp_order_line_rec.service_item_uom_code = FND_API.G_MISS_CHAR) THEN
7233         p_x_osp_order_line_rec.service_item_uom_code := null;
7234       END IF;
7235       IF(p_x_osp_order_line_rec.quantity = FND_API.G_MISS_NUM) THEN
7236         p_x_osp_order_line_rec.quantity := null;
7237       END IF;
7238       IF(p_x_osp_order_line_rec.po_line_type_id = FND_API.G_MISS_NUM) THEN
7239         p_x_osp_order_line_rec.po_line_type_id := null;
7240       END IF;
7241       IF(p_x_osp_order_line_rec.po_line_id = FND_API.G_MISS_NUM) THEN
7242         p_x_osp_order_line_rec.po_line_id := null;
7243       END IF;
7244       IF(p_x_osp_order_line_rec.oe_ship_line_id = FND_API.G_MISS_NUM) THEN
7245         p_x_osp_order_line_rec.oe_ship_line_id := null;
7246       END IF;
7247       IF(p_x_osp_order_line_rec.oe_return_line_id = FND_API.G_MISS_NUM) THEN
7248         p_x_osp_order_line_rec.oe_return_line_id := null;
7249       END IF;
7250       IF(p_x_osp_order_line_rec.workorder_id = FND_API.G_MISS_NUM) THEN
7251         p_x_osp_order_line_rec.workorder_id := null;
7252       END IF;
7253       IF(p_x_osp_order_line_rec.job_number = FND_API.G_MISS_CHAR) THEN
7254         p_x_osp_order_line_rec.job_number := null;
7255       END IF;
7256       IF(p_x_osp_order_line_rec.exchange_instance_id = FND_API.G_MISS_NUM) THEN
7257         p_x_osp_order_line_rec.exchange_instance_id := null;
7258       END IF;
7259       IF(p_x_osp_order_line_rec.exchange_instance_number = FND_API.G_MISS_CHAR) THEN
7260         p_x_osp_order_line_rec.exchange_instance_number := null;
7261       END IF;
7262       IF (p_x_osp_order_line_rec.inventory_item_id = FND_API.G_MISS_NUM) THEN
7263         p_x_osp_order_line_rec.inventory_item_id := null;
7264       END IF;
7265       IF (p_x_osp_order_line_rec.inventory_org_id = FND_API.G_MISS_NUM) THEN
7266         p_x_osp_order_line_rec.inventory_org_id := null;
7267       END IF;
7268       IF (p_x_osp_order_line_rec.item_number = FND_API.G_MISS_CHAR) THEN
7269         p_x_osp_order_line_rec.item_number := null;
7270       END IF;
7271       IF (p_x_osp_order_line_rec.inventory_item_uom = FND_API.G_MISS_CHAR) THEN
7272         p_x_osp_order_line_rec.inventory_item_uom := null;
7273       END IF;
7274       IF (p_x_osp_order_line_rec.inventory_item_quantity = FND_API.G_MISS_NUM) THEN
7275         p_x_osp_order_line_rec.inventory_item_quantity := null;
7276       END IF;
7277       IF (p_x_osp_order_line_rec.sub_inventory = FND_API.G_MISS_CHAR) THEN
7278         p_x_osp_order_line_rec.sub_inventory := null;
7279       END IF;
7280       IF (p_x_osp_order_line_rec.lot_number = FND_API.G_MISS_CHAR) THEN
7281         p_x_osp_order_line_rec.lot_number := null;
7282       END IF;
7283       IF (p_x_osp_order_line_rec.serial_number = FND_API.G_MISS_CHAR) THEN
7284         p_x_osp_order_line_rec.serial_number := null;
7285       END IF;
7286       -- Added by jaramana on January 10, 2008 for the Requisition ER 6034236
7287       IF (p_x_osp_order_line_rec.po_req_line_id  = FND_API.G_MISS_NUM) THEN
7288         p_x_osp_order_line_rec.po_req_line_id := null;
7289       END IF;
7290       -- jaramana End
7291       IF (p_x_osp_order_line_rec.attribute_category = FND_API.G_MISS_CHAR) THEN
7292         p_x_osp_order_line_rec.attribute_category := null;
7293       END IF;
7294       IF (p_x_osp_order_line_rec.attribute1 = FND_API.G_MISS_CHAR) THEN
7295         p_x_osp_order_line_rec.attribute1 := null;
7296       END IF;
7297       IF (p_x_osp_order_line_rec.attribute2 = FND_API.G_MISS_CHAR) THEN
7298         p_x_osp_order_line_rec.attribute2 := null;
7299       END IF;
7300       IF (p_x_osp_order_line_rec.attribute3 = FND_API.G_MISS_CHAR) THEN
7301         p_x_osp_order_line_rec.attribute3 := null;
7302       END IF;
7303       IF (p_x_osp_order_line_rec.attribute4 = FND_API.G_MISS_CHAR) THEN
7304         p_x_osp_order_line_rec.attribute4 := null;
7305       END IF;
7306       IF (p_x_osp_order_line_rec.attribute5 = FND_API.G_MISS_CHAR) THEN
7307         p_x_osp_order_line_rec.attribute5 := null;
7308       END IF;
7309       IF (p_x_osp_order_line_rec.attribute6 = FND_API.G_MISS_CHAR) THEN
7310         p_x_osp_order_line_rec.attribute6 := null;
7311       END IF;
7312       IF (p_x_osp_order_line_rec.attribute7 = FND_API.G_MISS_CHAR) THEN
7313         p_x_osp_order_line_rec.attribute7 := null;
7314       END IF;
7315       IF (p_x_osp_order_line_rec.attribute8 = FND_API.G_MISS_CHAR) THEN
7316         p_x_osp_order_line_rec.attribute8 := null;
7317       END IF;
7318       IF (p_x_osp_order_line_rec.attribute9 = FND_API.G_MISS_CHAR) THEN
7319         p_x_osp_order_line_rec.attribute9 := null;
7320       END IF;
7321       IF (p_x_osp_order_line_rec.attribute10 = FND_API.G_MISS_CHAR) THEN
7322         p_x_osp_order_line_rec.attribute10 := null;
7323       END IF;
7324       IF (p_x_osp_order_line_rec.attribute11 = FND_API.G_MISS_CHAR) THEN
7325         p_x_osp_order_line_rec.attribute11 := null;
7326       END IF;
7327       IF (p_x_osp_order_line_rec.attribute12 = FND_API.G_MISS_CHAR) THEN
7328         p_x_osp_order_line_rec.attribute12 := null;
7329       END IF;
7330       IF (p_x_osp_order_line_rec.attribute13 = FND_API.G_MISS_CHAR) THEN
7331         p_x_osp_order_line_rec.attribute13 := null;
7332       END IF;
7333       IF (p_x_osp_order_line_rec.attribute14 = FND_API.G_MISS_CHAR) THEN
7334         p_x_osp_order_line_rec.attribute14 := null;
7335       END IF;
7336       IF (p_x_osp_order_line_rec.attribute15 = FND_API.G_MISS_CHAR) THEN
7337         p_x_osp_order_line_rec.attribute15 := null;
7338       END IF;
7339     END IF;
7340     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7341       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'End Procedure');
7342     END IF;
7343 END default_unchanged_order_line;
7344 
7345 --Copied from original convert_order_lines_val_to_id, so some inherited logic
7346 --may not be necessary
7347 PROCEDURE convert_order_line_val_to_id(p_x_osp_order_line_rec IN OUT NOCOPY  osp_order_line_rec_type)
7348 IS
7349     CURSOR workorder_id_csr(p_job_number IN VARCHAR2) IS
7350     --Modified by mpothuku on 27-Feb-06 to fix the Perf Bug #4919164
7351     /*
7352     SELECT workorder_id
7353       FROM ahl_workorders_osp_v
7354      WHERE job_number = p_job_number;
7355     */
7356     SELECT workorder_id from ahl_workorders
7357      WHERE workorder_name = p_job_number;
7358 
7359     l_workorder_id NUMBER;
7360     CURSOR service_item_id_csr(p_service_item_number IN VARCHAR2, p_inventory_org_id NUMBER) IS
7361     SELECT MTL.inventory_item_id
7362       FROM mtl_system_items_kfv MTL
7363      WHERE MTL.concatenated_segments = p_service_item_number
7364        AND MTL.organization_id = p_inventory_org_id;
7365     l_service_item_id NUMBER;
7366     l_organization_id NUMBER;
7367     CURSOR po_line_type_id_csr(p_po_line_type IN VARCHAR2) IS
7368     SELECT line_type_id
7369       FROM po_line_types
7370      WHERE line_type = p_po_line_type
7371        AND order_type_lookup_code = 'QUANTITY'
7372        AND NVL(outside_operation_flag, G_NO_FLAG) = G_NO_FLAG;
7373     l_po_line_type_id NUMBER;
7374     CURSOR osp_order_line_on_csr(p_osp_order_id IN NUMBER, p_osp_line_number IN NUMBER) IS
7375     SELECT osp_order_line_id
7376       FROM ahl_osp_order_lines
7377      WHERE osp_order_id = p_osp_order_id
7378     and osp_line_number = p_osp_line_number;
7379     -- Added by jaramana on January 10, 2008 for the Requisition ER 6034236
7380     -- We are not planning to derive the OSP_ORDER_LINE_ID from PO_LINE_ID
7381     /*
7382     CURSOR osp_order_line_po_csr(p_po_line_id IN NUMBER) IS
7383     SELECT osp_order_line_id
7384       FROM ahl_osp_order_lines
7385      WHERE po_line_id = p_po_line_id;
7386     */
7387     -- jaramana End
7388     l_osp_order_line_id NUMBER;
7389     CURSOR exchange_instance_id_csr(p_exchange_instance_number IN VARCHAR2) IS
7390     SELECT instance_id
7391       FROM csi_item_instances
7392      WHERE instance_number = p_exchange_instance_number;
7393     l_exchange_instance_id NUMBER;
7394     CURSOR physical_item_id_csr(p_item_number IN VARCHAR2, p_inventory_org_id NUMBER) IS
7395     SELECT MTL.inventory_item_id
7396       FROM mtl_system_items_kfv MTL
7397      WHERE MTL.concatenated_segments = p_item_number
7398        AND MTL.organization_id = p_inventory_org_id;
7399     l_physical_item_id NUMBER;
7400     L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.convert_order_lines_val_to_id';
7401 BEGIN
7402     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7403       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Procedure');
7404     END IF;
7405     -- nulling out ids if module type is JSP
7406     --Jerry 04/18/2005, this 'JSP' consideration may not be necessary for
7407     --newly added physical item, so it is not added here
7408         IF(g_module_type = 'JSP') THEN
7409             -- service item number
7410             IF(p_x_osp_order_line_rec.service_item_number IS NULL) THEN
7411                 p_x_osp_order_line_rec.service_item_id := NULL;
7412             ELSIF (p_x_osp_order_line_rec.service_item_number = FND_API.G_MISS_CHAR) THEN
7413                 IF(p_x_osp_order_line_rec.operation_flag <> G_OP_CREATE) THEN
7414                     p_x_osp_order_line_rec.service_item_id := FND_API.G_MISS_NUM;
7415                 ELSE
7416                     p_x_osp_order_line_rec.service_item_id := NULL;
7417                 END IF;
7418             END IF;
7419             -- job number and workorder_id
7420             IF(p_x_osp_order_line_rec.job_number IS NULL) THEN
7421                 p_x_osp_order_line_rec.workorder_id := NULL;
7422             ELSIF (p_x_osp_order_line_rec.job_number = FND_API.G_MISS_CHAR) THEN
7423                 IF(p_x_osp_order_line_rec.operation_flag <> G_OP_CREATE) THEN
7424                     p_x_osp_order_line_rec.workorder_id := FND_API.G_MISS_NUM;
7425                 ELSE
7426                     p_x_osp_order_line_rec.workorder_id := NULL;
7427                 END IF;
7428             END IF;
7429             -- po_line_type_id
7430             IF(p_x_osp_order_line_rec.po_line_type IS NULL) THEN
7431                 p_x_osp_order_line_rec.po_line_type_id := NULL;
7432             ELSIF (p_x_osp_order_line_rec.po_line_type = FND_API.G_MISS_CHAR) THEN
7433                 IF(p_x_osp_order_line_rec.operation_flag <> G_OP_CREATE) THEN
7434                     p_x_osp_order_line_rec.po_line_type_id := FND_API.G_MISS_NUM;
7435                 ELSE
7436                     p_x_osp_order_line_rec.po_line_type_id := NULL;
7437                 END IF;
7438             END IF;
7439         END IF;
7440        -- conversion of value to id for job number (to workorder_id)
7441        IF (p_x_osp_order_line_rec.job_number IS NOT NULL AND  p_x_osp_order_line_rec.job_number <> FND_API.G_MISS_CHAR) THEN
7442          OPEN workorder_id_csr(p_x_osp_order_line_rec.job_number);
7443          FETCH workorder_id_csr INTO l_workorder_id;
7444          IF(workorder_id_csr%NOTFOUND) THEN
7445             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_JOB_NUM_INV');
7446             FND_MESSAGE.Set_Token('JOB_NUMBER', p_x_osp_order_line_rec.job_number);
7447             FND_MSG_PUB.ADD;
7448          ELSE
7449             p_x_osp_order_line_rec.workorder_id := l_workorder_id;
7450          END IF;
7451          CLOSE workorder_id_csr;
7452        END IF;
7453        -- conversion of value to id for service item number
7454        IF (p_x_osp_order_line_rec.service_item_number IS NOT NULL AND  p_x_osp_order_line_rec.service_item_number <> FND_API.G_MISS_CHAR) THEN
7455          OPEN service_item_id_csr(p_x_osp_order_line_rec.service_item_number, p_x_osp_order_line_rec.inventory_org_id);
7456          FETCH service_item_id_csr INTO l_service_item_id;
7457          IF(service_item_id_csr%NOTFOUND) THEN
7458             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_SVC_ITEM_INV');
7459             FND_MESSAGE.Set_Token('SERVICE_ITEM', p_x_osp_order_line_rec.service_item_number);
7460             FND_MSG_PUB.ADD;
7461          ELSE
7462             p_x_osp_order_line_rec.service_item_id := l_service_item_id;
7463          END IF;
7464          CLOSE service_item_id_csr;
7465        END IF;
7466        -- conversion of value to id for po_line_type (to po_line_type_id)
7467        IF (p_x_osp_order_line_rec.po_line_type IS NOT NULL AND  p_x_osp_order_line_rec.po_line_type <> FND_API.G_MISS_CHAR) THEN
7468          OPEN po_line_type_id_csr(p_x_osp_order_line_rec.po_line_type);
7469          FETCH po_line_type_id_csr INTO l_po_line_type_id;
7470          IF(po_line_type_id_csr%NOTFOUND) THEN
7471             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_PO_LN_TYPE_INV');
7472             FND_MESSAGE.Set_Token('PO_LINE_TYPE', p_x_osp_order_line_rec.po_line_type);
7473             FND_MSG_PUB.ADD;
7474          ELSE
7475             p_x_osp_order_line_rec.po_line_type_id := l_po_line_type_id;
7476          END IF;
7477          CLOSE po_line_type_id_csr;
7478        END IF;
7479        -- conversion of value to id for exchange_instance_number (to exchange_instance_id)
7480        IF (p_x_osp_order_line_rec.exchange_instance_number IS NOT NULL AND  p_x_osp_order_line_rec.exchange_instance_number <> FND_API.G_MISS_CHAR) THEN
7481          OPEN exchange_instance_id_csr(p_x_osp_order_line_rec.exchange_instance_number);
7482          FETCH exchange_instance_id_csr INTO l_exchange_instance_id;
7483          IF(exchange_instance_id_csr%NOTFOUND) THEN
7484             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_X_INST_NUM_INV');
7485             FND_MESSAGE.Set_Token('INTANCE_NUMBER', p_x_osp_order_line_rec.exchange_instance_number);
7486             FND_MSG_PUB.ADD;
7487          ELSE
7488             p_x_osp_order_line_rec.exchange_instance_id := l_exchange_instance_id;
7489          END IF;
7490          CLOSE exchange_instance_id_csr;
7491        END IF;
7492        -- conversion of value to id for physical item number
7493        IF (p_x_osp_order_line_rec.item_number IS NOT NULL AND  p_x_osp_order_line_rec.inventory_item_id IS NULL) THEN
7494          OPEN physical_item_id_csr(p_x_osp_order_line_rec.item_number, p_x_osp_order_line_rec.inventory_org_id);
7495          FETCH physical_item_id_csr INTO l_physical_item_id;
7496          IF(physical_item_id_csr%NOTFOUND) THEN
7497             FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_PHY_ITEM_INV');
7498             FND_MESSAGE.Set_Token('PHSICAL_ITEM', p_x_osp_order_line_rec.item_number);
7499             FND_MSG_PUB.ADD;
7500          ELSE
7501             p_x_osp_order_line_rec.inventory_item_id := l_physical_item_id;
7502          END IF;
7503          CLOSE physical_item_id_csr;
7504        END IF;
7505        -- fetching the unique key osp_order_line_id based on other unique keys
7506        IF (p_x_osp_order_line_rec.osp_order_line_id IS NULL) THEN
7507             IF p_x_osp_order_line_rec.operation_flag <> G_OP_CREATE THEN
7508                 IF (p_x_osp_order_line_rec.osp_order_id IS NOT NULL AND p_x_osp_order_line_rec.osp_line_number IS NOT NULL) THEN
7509                     OPEN osp_order_line_on_csr(p_x_osp_order_line_rec.osp_order_id , p_x_osp_order_line_rec.osp_line_number);
7510                     FETCH osp_order_line_on_csr INTO l_osp_order_line_id;
7511                     IF(osp_order_line_on_csr%NOTFOUND) THEN
7512                         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_ORD_ID_LN_INV');
7513                         FND_MESSAGE.Set_Token('OSP_LINE_NUMBER', p_x_osp_order_line_rec.osp_line_number);
7514                         FND_MSG_PUB.ADD;
7515                     ELSE
7516                         p_x_osp_order_line_rec.osp_order_line_id := l_osp_order_line_id;
7517                     END IF;
7518                     CLOSE osp_order_line_on_csr;
7519                 -- Added by jaramana on January 10, 2008 for the Requisition ER 6034236
7520                 -- We are not planning to derive the OSP_ORDER_LINE_ID from PO_LINE_ID
7521                 /*
7522                 ELSIF (p_x_osp_order_line_rec.po_line_id IS NOT NULL AND p_x_osp_order_line_rec.po_line_id <>  FND_API.G_MISS_NUM) THEN
7523                     OPEN osp_order_line_po_csr(p_x_osp_order_line_rec.po_line_id );
7524                     FETCH osp_order_line_po_csr INTO l_osp_order_line_id;
7525                     IF(osp_order_line_po_csr%NOTFOUND) THEN
7526                         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_PO_LINE_INV');
7527                         FND_MESSAGE.Set_Token('PO_LINE_ID', p_x_osp_order_line_rec.po_line_id);
7528                         FND_MSG_PUB.ADD;
7529                     ELSE
7530                         p_x_osp_order_line_rec.osp_order_line_id := l_osp_order_line_id;
7531                     END IF;
7532                     CLOSE osp_order_line_po_csr;
7533                 */
7534                 -- jaramana End
7535                 ELSE
7536                     FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV');
7537                     FND_MESSAGE.Set_Token('OSP_LINE_NUMBER', p_x_osp_order_line_rec.osp_line_number);                     FND_MSG_PUB.ADD;
7538                 END IF;
7539             END IF;
7540         END IF;
7541     IF FND_MSG_PUB.count_msg > 0 THEN
7542        RAISE  FND_API.G_EXC_ERROR;
7543        -- RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7544     END IF;
7545      --dbms_output.put_line('Exiting : convert_order_lines_val_to_id');
7546     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7547       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'End Procedure');
7548     END IF;
7549 END convert_order_line_val_to_id;
7550 
7551 --validate service_item_id (simplified the original one)
7552 PROCEDURE validate_service_item_id(
7553   p_service_item_id IN NUMBER,
7554   p_organization_id IN NUMBER)
7555 IS
7556   CURSOR val_service_item_id_csr IS
7557     SELECT 'X'
7558       FROM mtl_system_items_kfv MTL
7559      WHERE MTL.inventory_item_id = p_service_item_id
7560        AND MTL.enabled_flag = G_YES_FLAG
7561        AND MTL.inventory_item_flag = G_NO_FLAG
7562        AND MTL.stock_enabled_flag = G_NO_FLAG
7563        AND NVL(MTL.start_date_active, SYSDATE - 1) <= SYSDATE
7564        AND NVL(MTL.end_date_active, SYSDATE + 1) > SYSDATE
7565        AND MTL.purchasing_enabled_flag = G_YES_FLAG
7566        AND NVL(MTL.outside_operation_flag, G_NO_FLAG) = G_NO_FLAG
7567        AND MTL.organization_id = p_organization_id;
7568   L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.validate_service_item_id';
7569 BEGIN
7570   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7571     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Procedure');
7572   END IF;
7573 
7574   IF(p_service_item_id IS NOT NULL AND p_service_item_id <> FND_API.G_MISS_NUM) THEN
7575     OPEN val_service_item_id_csr;
7576     FETCH val_service_item_id_csr INTO g_dummy_char;
7577     IF(val_service_item_id_csr%NOTFOUND) THEN
7578       FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_LN_INV_SVC_ITEM');
7579       FND_MESSAGE.Set_Token('SERVICE_ITEM_ID', p_service_item_id);
7580       FND_MSG_PUB.ADD;
7581     END IF;
7582     CLOSE val_service_item_id_csr;
7583   END IF;
7584 
7585   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7586     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'End Procedure');
7587   END IF;
7588 END validate_service_item_id;
7589 
7590 --Validate header vendor with physical, service item combination in line
7591 PROCEDURE validate_vendor_service_item(p_osp_order_line_rec IN osp_order_line_rec_type)
7592 IS
7593   l_vendor_id         NUMBER;
7594   l_vendor_site_id    NUMBER;
7595   l_vendor_contact_id NUMBER;
7596   l_msg_count        NUMBER;
7597   l_msg_data         VARCHAR2(2000);
7598   CURSOR verify_vendor(c_vendor_id NUMBER, c_vendor_site_id NUMBER) IS
7599     SELECT A.inv_service_item_rel_id
7600       FROM ahl_inv_service_item_rels A,
7601            ahl_item_vendor_rels_v B
7602      WHERE A.inv_service_item_rel_id = B.inv_service_item_rel_id
7603        AND A.inv_item_id = p_osp_order_line_rec.inventory_item_id
7604        AND A.inv_org_id = p_osp_order_line_rec.inventory_org_id
7605        AND A.service_item_id = p_osp_order_line_rec.service_item_id
7606        AND TRUNC(A.active_start_date) <= TRUNC(SYSDATE)
7607        AND TRUNC(NVL(A.active_end_date, SYSDATE+1)) > TRUNC(SYSDATE)
7608        AND TRUNC(B.active_start_date) <= TRUNC(SYSDATE)
7609        AND TRUNC(NVL(B.active_end_date, SYSDATE+1)) > TRUNC(SYSDATE)
7610        AND B.vendor_id = c_vendor_id
7611        AND B.vendor_site_id = c_vendor_site_id;
7612        --AND B.vendor_contact_id = l_vendor_contact_id;
7613 BEGIN
7614   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7615       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
7616                      G_LOG_PREFIX || '.validate_vendor_service_item',
7617                      'osp_order_id='||p_osp_order_line_rec.osp_order_id );
7618   END IF;
7619   --IF (p_osp_order_line_rec.workorder_id IS NULL) THEN --commented out on 06/10/2006
7620     /*(From Jay said on 06/07/2005) service_item_id is not mandatory even for update
7621     IF p_osp_order_line_rec.service_item_id IS NULL THEN
7622       FND_MESSAGE.set_name('AHL', 'AHL_OSP_SERVICE_ITEM_NULL');
7623       FND_MSG_PUB.add;
7624     END IF;
7625     */
7626     --(Note: 05/31/2005 Jerry)It is not good to use ahl_osp_orders_v here but
7627     --there exists a special case where vendor_contact_id is null in
7628     --ahl_osp_orders_b but populated in ahl_osp_orders_v. After discussion with
7629     --Jay and found we have to use _v here because the vendor_id and vendor_site_id
7630     --could be changed from PO side
7631   BEGIN
7632     --Modified by mpothuku on 27-Feb-06 to fix the Perf Bug #4919164
7633     /*
7634     SELECT decode(osp.po_header_id, null, osp.vendor_id, po.vendor_id) vendor_id,
7635            decode(osp.po_header_id, null, osp.vendor_site_id, po.vendor_site_id) vendor_site_id --, vendor_contact_id
7636       INTO l_vendor_id, l_vendor_site_id --, l_vendor_contact_id
7637       FROM ahl_osp_orders_b osp,
7638            po_headers_all po
7639      WHERE osp.osp_order_id = p_osp_order_line_rec.osp_order_id
7640        AND osp.po_header_id = po.po_header_id(+);
7641     */
7642     --modified by mpothuku on 14-mar-2008 to fix the Bug 6885513
7643     SELECT vendor_id,
7644            vendor_site_id
7645       INTO l_vendor_id, l_vendor_site_id --, l_vendor_contact_id
7646       FROM ahl_osp_orders_b osp
7647      WHERE osp.osp_order_id = p_osp_order_line_rec.osp_order_id;
7648     --mpothuku End
7649   EXCEPTION
7650     WHEN NO_DATA_FOUND THEN
7651       FND_MESSAGE.set_name('AHL', 'AHL_OSP_ORDER_INVALID');
7652       FND_MSG_PUB.add;
7653   END;
7654 
7655   IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7656       FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
7657                      G_LOG_PREFIX || '.validate_vendor_service_item',
7658                      'vendor_id='||l_vendor_id||
7659                      'item_id='||p_osp_order_line_rec.inventory_item_id||
7660                      'org_id='||p_osp_order_line_rec.inventory_org_id||
7661                      'service_id='||p_osp_order_line_rec.service_item_id);
7662   END IF;
7663 
7664   IF (l_vendor_id IS NOT NULL AND l_vendor_site_id IS NOT NULL AND
7665       p_osp_order_line_rec.service_item_id IS NOT NULL) THEN
7666     --If the service_item_id in OSP Order Line is null then we don't have to
7667     --validate it against OSP header vendor attributes
7668     --Check the vendor itself is valid
7669     BEGIN
7670       SELECT 'X' INTO g_dummy_char
7671         FROM ahl_vendor_certifications_v
7672        WHERE vendor_id = l_vendor_id
7673          AND vendor_site_id = l_vendor_site_id
7674          AND trunc(active_start_date) <= trunc(SYSDATE)
7675          AND trunc(nvl(active_end_date, SYSDATE+1)) > trunc(SYSDATE);
7676     EXCEPTION
7677       WHEN NO_DATA_FOUND THEN
7678         FND_MESSAGE.Set_Name(G_APP_NAME,'AHL_OSP_VENDOR_ID_INV');
7679         FND_MESSAGE.Set_Token('VENDOR_ID', l_vendor_id);
7680         FND_MSG_PUB.ADD;
7681     END;
7682 
7683     OPEN verify_vendor(l_vendor_id, l_vendor_site_id);
7684     FETCH verify_vendor INTO g_dummy_num;
7685     IF verify_vendor%NOTFOUND THEN
7686       FND_MESSAGE.set_name('AHL', 'AHL_OSP_ITEM_VENDOR_MISMATCH');
7687       FND_MESSAGE.set_token('ITEM_ID', p_osp_order_line_rec.inventory_item_id);
7688       FND_MSG_PUB.add;
7689       RAISE FND_API.G_EXC_ERROR;
7690       --FND_MSG_PUB.count_and_get(
7691         --p_encoded  => FND_API.G_FALSE,
7692         --p_count    => l_msg_count,
7693         --p_data     => l_msg_data);
7694     END IF;
7695     CLOSE verify_vendor;
7696   END IF;
7697   --END IF;
7698   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7699     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
7700                    G_LOG_PREFIX || '.validate_vendor_service_item',
7701                    'Normal end and msg_count='||l_msg_count||
7702                    'msg='||l_msg_data);
7703   END IF;
7704 END validate_vendor_service_item;
7705 
7706 PROCEDURE process_order_type_change(
7707   p_osp_order_rec IN osp_order_rec_type) IS
7708   l_old_type_code VARCHAR2(30);
7709   l_return_status VARCHAR2(30);
7710   l_msg_count     NUMBER;
7711   l_msg_data      VARCHAR2(2000);
7712 BEGIN
7713   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7714     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, G_LOG_PREFIX||'process_order_type_change', 'Begin');
7715   END IF;
7716   SELECT order_type_code INTO l_old_type_code
7717     FROM ahl_osp_orders_b
7718    WHERE osp_order_id = p_osp_order_rec.osp_order_id;
7719   IF l_old_type_code <> p_osp_order_rec.order_type_code THEN
7720     IF(can_convert_order(p_osp_order_rec.osp_order_id, l_old_type_code, p_osp_order_rec.order_type_code)) THEN
7721       IF(p_osp_order_rec.order_type_code = G_OSP_ORDER_TYPE_SERVICE) THEN
7722         UPDATE ahl_osp_order_lines
7723            SET exchange_instance_id = NULL,
7724                last_update_date = SYSDATE,
7725                last_updated_by = fnd_global.user_id,
7726                last_update_login = fnd_global.login_id,
7727                object_version_number = object_version_number+1
7728         WHERE osp_order_id = p_osp_order_rec.osp_order_id;
7729         --set exchange instance id to null and update object_version_number in p_x_osp_order_lines_tbl
7730         --nullify_exchange_instance(p_osp_order_rec.osp_order_id, p_x_osp_order_lines_tbl);
7731       END IF;
7732       AHL_OSP_SHIPMENT_PUB.convert_subtxn_type(
7733           p_api_version          =>    1.0,
7734           p_init_msg_list        =>    FND_API.G_FALSE,
7735           p_commit               =>    FND_API.G_FALSE,
7736           p_validation_level     =>    FND_API.G_VALID_LEVEL_FULL,
7737           p_default              =>    FND_API.G_TRUE,
7738           p_module_type          =>    NULL,
7739           p_osp_order_id         =>    p_osp_order_rec.osp_order_id ,
7740           p_old_order_type_code  =>    l_old_type_code,
7741           p_new_order_type_code  =>    p_osp_order_rec.order_type_code,
7742           x_return_status        =>    l_return_status,
7743           x_msg_count            =>    l_msg_count,
7744           x_msg_data             =>    l_msg_data);
7745     END IF;
7746   END IF;
7747   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7748     FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, G_LOG_PREFIX||'process_order_type_change', 'End');
7749   END IF;
7750 END process_order_type_change;
7751 
7752 --salogan added the following for supplier warranty starts
7753 ------------------------------------------------------------------------------------
7754 -- Start of Comments
7755 --  Function name     : Can_Show_Claim_Details
7756 --  Type              : Public
7757 --  Function          : The Warranty Claim and Status fields will only be displayed if:
7758 --                          There is a Warranty Contract approved for the OSP Line.
7759 --                          The OSP Claim checkbox is checked in the Warranty Contract.
7760 --                          The OSP Order has been submitted.
7761 --  Pre-reqs          :
7762 --  Parameters        : p_osp_order_line_id IN  NUMBER  : Required
7763 --                    : Returns VARCHAR2 ('Y' or 'N')
7764 --
7765 --  End of Comments
7766 
7767 FUNCTION Can_Show_Claim_Details(
7768         p_osp_order_line_id IN  NUMBER
7769 ) RETURN VARCHAR2
7770 IS
7771 -- Cursor to check the condition
7772     CURSOR c_can_show_claim (c_osp_order_line_id IN NUMBER) IS
7773      SELECT 'X'
7774      FROM AHL_WARRANTY_ENTITLEMENTS AWSE,
7775      AHL_WARRANTY_CONTRACTS_B AWCB,
7776      AHL_OSP_ORDERS_B AOOB,
7777      AHL_OSP_ORDER_LINES AOOL
7778      WHERE AOOB.OSP_ORDER_ID = AOOL.OSP_ORDER_ID
7779      AND AOOL.OSP_ORDER_LINE_ID = AWSE.OSP_ORDER_LINE_ID
7780      AND AWCB.WARRANTY_CONTRACT_ID = AWSE.WARRANTY_CONTRACT_ID
7781      AND AOOB.STATUS_CODE = 'SUBMITTED'
7782      AND AWSE.ENTITLEMENT_STATUS_CODE = 'APPROVED'
7783      AND AWCB.OSP_CLAIM_FLAG = 'Y'
7784      AND AOOL.OSP_ORDER_LINE_ID = c_osp_order_line_id;
7785 
7786 --
7787     l_dummy    VARCHAR2(1);
7788 --
7789 BEGIN
7790     OPEN c_can_show_claim(p_osp_order_line_id);
7791     FETCH c_can_show_claim into l_dummy;
7792     IF(c_can_show_claim%FOUND) THEN
7793        RETURN 'Y';
7794     END IF;
7795     CLOSE c_can_show_claim;
7796     RETURN 'N';
7797 END Can_Show_Claim_Details;
7798 
7799 ------------------------------------------------------------------------------------
7800 -- Start of Comments
7801 --  Procedure name    : Get_Warranty_Rec_for_Create
7802 --  Type              : Private
7803 --  Function          : To get the warranty entitlement record during OSP creation
7804 --  Pre-reqs          :
7805 --  Parameters        : p_x_osp_order_line_rec IN  osp_order_line_rec_type Required
7806 --                    : x_warranty_entl_tbl    OUT AHL_WARRANTY_ENTL_PVT.Warranty_Entl_Tbl_Type
7807 --                    : x_call_process_entl    OUT Detarmines the call to process entitlements
7808 --
7809 --  End of Comments
7810 
7811 PROCEDURE Get_Warranty_Rec_for_Create(
7812         p_x_osp_order_line_rec IN         osp_order_line_rec_type,
7813         x_warranty_entl_tbl    OUT NOCOPY AHL_WARRANTY_ENTL_PVT.Warranty_Entl_Tbl_Type,
7814         x_call_process_entl    OUT NOCOPY VARCHAR2
7815 ) IS
7816    -- Standard variables
7817    L_API_NAME  CONSTANT VARCHAR2(30)  := 'Get_Warranty_Rec_for_Create';
7818    L_DEBUG_KEY CONSTANT VARCHAR2(100) := G_LOG_PREFIX || '.' || L_API_NAME;
7819    -- Local variables defined for the procedure
7820    l_trackable            VARCHAR2(1);
7821    l_contract_id          NUMBER      := FND_API.G_MISS_NUM;
7822    l_warranty_entl_id     NUMBER;
7823    l_call_process_entl    VARCHAR2(1) := 'Y';
7824    l_ovn                  NUMBER;
7825    l_entitlement_rec      AHL_WARRANTY_ENTL_PVT.Warranty_Entl_Rec_Type;
7826 
7827    -- Cursors defined for the procedure
7828    --Cursor to fetch the active contracts for the given item instance id
7829    CURSOR c_match_contract (c_instance_id IN NUMBER) IS
7830     SELECT warranty_contract_id
7831     FROM   ahl_warranty_contracts_b
7832     WHERE  item_instance_id     = c_instance_id
7833     AND    contract_status_code = 'ACTIVE';
7834 
7835    --Cursor to check whether the item is trackable
7836    CURSOR  c_is_trackable_item(c_inventory_item_id NUMBER, c_inventory_org_id NUMBER) IS
7837     SELECT COMMS_NL_TRACKABLE_FLAG
7838     FROM   MTL_SYSTEM_ITEMS_KFV
7839     WHERE  INVENTORY_ITEM_ID = c_inventory_item_id
7840     AND    ORGANIZATION_ID = c_inventory_org_id;
7841 
7842    --Cursor to fetch entitlement id for a given workorder
7843    CURSOR  c_get_entl_for_wo(c_workorder_id NUMBER) IS
7844     SELECT ASWE.WARRANTY_ENTITLEMENT_ID, ASWE.OBJECT_VERSION_NUMBER
7845      FROM   AHL_WARRANTY_ENTITLEMENTS ASWE
7846      WHERE  ASWE.VISIT_TASK_ID = (SELECT wo.visit_task_id
7847                                   FROM   ahl_workorders wo
7848                                   WHERE  wo.workorder_id = c_workorder_id);
7849 
7850 BEGIN
7851    --------------------Start of API Body-----------------------------------
7852     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7853         FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
7854                      L_DEBUG_KEY ||'.begin',
7855                      'At the start of PL SQL procedure.');
7856    END IF;
7857 
7858    --------------------Get Entitlements Record For Create-------------------
7859    IF(p_x_osp_order_line_rec.osp_order_line_id = null OR p_x_osp_order_line_rec.instance_id = null) THEN
7860     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7861         FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
7862                        L_DEBUG_KEY,
7863                        'Passed OSP_ORDER_LINE_ID : '|| p_x_osp_order_line_rec.osp_order_line_id ||
7864                        ' or Instance id : '|| p_x_osp_order_line_rec.instance_id ||' is null');
7865     END IF;
7866     FND_MSG_PUB.add;
7867     l_call_process_entl := 'N';
7868    END IF;
7869 
7870    --Store the osp order line id and operation flag in Entitlements Record
7871    l_entitlement_rec.osp_order_line_id := p_x_osp_order_line_rec.osp_order_line_id;
7872    l_entitlement_rec.operation_flag := AHL_WARRANTY_ENTL_PVT.G_OP_CREATE;
7873    --Store the return value that is validated before process entl API call
7874 
7875    --Auto match contract
7876    IF(p_x_osp_order_line_rec.workorder_id IS NULL) THEN -- Inventory Item based
7877       OPEN c_is_trackable_item(p_x_osp_order_line_rec.inventory_item_id, p_x_osp_order_line_rec.inventory_org_id);
7878       FETCH c_is_trackable_item into l_trackable; -- check whether the inv item is trackable
7879       IF(c_is_trackable_item%FOUND AND l_trackable = 'Y') THEN -- If the warranty template id at the MR level is found
7880          OPEN c_match_contract(p_x_osp_order_line_rec.instance_id);
7881          LOOP
7882             FETCH c_match_contract into l_contract_id; -- Store the warranty contract id  for the instance match
7883             EXIT WHEN c_match_contract%NOTFOUND OR c_match_contract%ROWCOUNT > 1;
7884          END LOOP;
7885          IF(c_match_contract%ROWCOUNT > 1) THEN
7886             l_entitlement_rec.warranty_contract_id    := FND_API.G_MISS_NUM;
7887             l_entitlement_rec.entitlement_status_code := 'REVIEW';
7888          ELSIF(c_match_contract%ROWCOUNT = 1) THEN
7889             l_entitlement_rec.warranty_contract_id    := l_contract_id;
7890             l_entitlement_rec.entitlement_status_code := 'APPROVED';
7891          ELSIF(c_match_contract%NOTFOUND) THEN
7892             l_call_process_entl := 'N';
7893          END IF;
7894          CLOSE c_match_contract;
7895       ELSE
7896          l_call_process_entl := 'N';
7897       END IF;
7898       CLOSE c_is_trackable_item;
7899    ELSE -- Workorder Based
7900       -- Instance warranty enabled
7901       IF(ahl_warranty_contracts_pvt.is_instance_warranty_available( p_x_osp_order_line_rec.instance_id)  = 'Y') THEN
7902          OPEN c_get_entl_for_wo(p_x_osp_order_line_rec.workorder_id);
7903          FETCH c_get_entl_for_wo into l_warranty_entl_id, l_ovn;
7904          IF(c_get_entl_for_wo%FOUND) THEN --If entitlements already exists for the WO
7905             --set the entitlement id for update and the osp line id is already defauled to the record
7906             l_entitlement_rec.warranty_entitlement_id := l_warranty_entl_id;
7907             l_entitlement_rec.object_version_number   := l_ovn;
7908             --Overwrite the operation flag to update
7909             l_entitlement_rec.operation_flag          := AHL_WARRANTY_ENTL_PVT.G_OP_UPDATE;
7910          ELSE
7911             l_call_process_entl := 'N';
7912          END IF;
7913       CLOSE c_get_entl_for_wo;
7914     ELSE
7915       l_call_process_entl := 'N';
7916       END IF;
7917    END IF;
7918    -- Set OUT parameter with created entitlement record
7919    x_warranty_entl_tbl(0) := l_entitlement_rec;
7920    x_call_process_entl    := l_call_process_entl;
7921 
7922     --------------------End of API Body-----------------------------------
7923    IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7924        FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
7925                       L_DEBUG_KEY||'.end',
7926                      'At the end of PL SQL procedure.' ||
7927                      ' OSP Order Line Id : '|| l_entitlement_rec.osp_order_line_id ||
7928                      ' Warranty Contract Id : '|| l_entitlement_rec.warranty_contract_id ||
7929                      ' Entitlement Status Code : '|| l_entitlement_rec.entitlement_status_code ||
7930                      ' Warranty Entitlement Id : '|| l_entitlement_rec.warranty_entitlement_id ||
7931                      ' Operation Flag : '|| l_entitlement_rec.operation_flag);
7932    END IF;
7933 END Get_Warranty_Rec_for_Create;
7934 
7935 
7936 
7937 ------------------------------------------------------------------------------------
7938 -- Start of Comments
7939 --  Procedure name    : Get_Warranty_Rec_for_Update
7940 --  Type              : Private
7941 --  Function          : To get the set warranty entitlement record (table type) for OSP updation
7942 --  Pre-reqs          :
7943 --  Parameters        : p_x_osp_order_line_rec IN  osp_order_line_rec_type Required
7944 --                    : x_warranty_entl_tbl    OUT AHL_WARRANTY_ENTL_PVT.Warranty_Entl_Tbl_Type
7945 --                    : x_call_process_entl    OUT Detarmines the call to process entitlements
7946 --
7947 --  End of Comments
7948 
7949 PROCEDURE Get_Warranty_Rec_for_Update(
7950         p_x_osp_order_line_rec IN         osp_order_line_rec_type,
7951         x_warranty_entl_tbl    OUT NOCOPY AHL_WARRANTY_ENTL_PVT.Warranty_Entl_Tbl_Type,
7952         x_call_process_entl    OUT NOCOPY VARCHAR2
7953 ) IS
7954    -- Standard variables
7955    L_API_NAME  CONSTANT VARCHAR2(30)  := 'Get_Warranty_Rec_for_Update';
7956    L_DEBUG_KEY CONSTANT VARCHAR2(100) := 'ahl.plsql.' || G_PKG_NAME || '.' || L_API_NAME;
7957    -- Local variables defined for the procedure
7958    l_contract_id          NUMBER        := null;
7959    l_entl_status          VARCHAR2(20)  := null;
7960    l_old_contract_id      NUMBER        := null;
7961    l_old_entl_status      VARCHAR2(20)  := 'NOT_APPLICABLE';
7962    l_task_type            VARCHAR2(20)  := null;
7963    l_call_process_entl    VARCHAR2(1)   := 'Y';
7964    l_ovn                  NUMBER;
7965    l_entitlement_rec      AHL_WARRANTY_ENTL_PVT.Warranty_Entl_Rec_Type;
7966 
7967    -- Cursors defined for the procedure
7968    --Cursor to fetch the entitlement values from DB
7969    CURSOR c_old_entitlement_rec (c_osp_order_line_id IN NUMBER) IS
7970     SELECT AWCB.WARRANTY_CONTRACT_ID, AWSE.ENTITLEMENT_STATUS_CODE, AWSE.OBJECT_VERSION_NUMBER
7971     FROM AHL_WARRANTY_ENTITLEMENTS AWSE, AHL_WARRANTY_CONTRACTS_B AWCB
7972     WHERE  AWSE.WARRANTY_CONTRACT_ID = AWCB.WARRANTY_CONTRACT_ID(+)
7973     AND AWSE.OSP_ORDER_LINE_ID = c_osp_order_line_id;
7974 
7975    --Cursor to fetch contract id using the contract number
7976    CURSOR c_get_contract_id (c_contract_num IN NUMBER) IS
7977     SELECT WARRANTY_CONTRACT_ID
7978     FROM AHL_WARRANTY_CONTRACTS_B
7979     WHERE CONTRACT_NUMBER = c_contract_num
7980     AND CONTRACT_STATUS_CODE = 'ACTIVE';
7981 
7982 BEGIN
7983    --------------------Start of API Body-----------------------------------
7984    IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7985        FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
7986                      L_DEBUG_KEY ||'.begin',
7987                      'At the start of PL SQL procedure.');
7988    END IF;
7989 
7990    --------------------Get Entitlements Record For Update-------------------
7991    IF(p_x_osp_order_line_rec.osp_order_line_id is null ) THEN
7992     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
7993         FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
7994                        L_DEBUG_KEY,
7995                        'Passed osp order line id is null');
7996     END IF;
7997     FND_MSG_PUB.add;
7998     l_call_process_entl := 'N';
7999    END IF;
8000 
8001    IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
8002        FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
8003                       L_DEBUG_KEY,
8004                       ' OSP Order Line Id is : '|| p_x_osp_order_line_rec.osp_order_line_id||
8005                       ' Instance is : '|| p_x_osp_order_line_rec.instance_id||
8006                       ' Entitlement Id is : '|| p_x_osp_order_line_rec.warranty_entitlement_id||
8007                       ' Entitlement Status Code is : '|| p_x_osp_order_line_rec.entitlement_status_code||
8008                       ' Warranty Contract Num is : '|| p_x_osp_order_line_rec.contract_number||
8009                       ' Instance Warranty Flag is : '|| p_x_osp_order_line_rec.instance_warranty);
8010    END IF;
8011 
8012    OPEN c_old_entitlement_rec(p_x_osp_order_line_rec.osp_order_line_id);
8013    FETCH c_old_entitlement_rec into l_old_contract_id, l_old_entl_status, l_ovn;
8014    CLOSE c_old_entitlement_rec;
8015 
8016    -- If no change in contract number - donot call process entitlements
8017    OPEN c_get_contract_id(p_x_osp_order_line_rec.contract_number);
8018    -- The fetched contract id is used for futher computation below
8019    FETCH c_get_contract_id into l_contract_id;
8020    CLOSE c_get_contract_id;
8021    IF((l_contract_id IS NULL AND l_old_contract_id IS NULL)
8022       OR
8023       (l_contract_id IS NOT NULL AND l_old_contract_id IS NOT NULL
8024        AND (l_contract_id = l_old_contract_id))) THEN
8025       l_call_process_entl := 'N';
8026    END IF;
8027 
8028    IF(p_x_osp_order_line_rec.instance_warranty = 'Y' AND l_call_process_entl = 'Y') THEN
8029     IF(p_x_osp_order_line_rec.contract_number is not null) THEN -- If contact number is associated
8030        --OPEN c_get_contract_id(p_x_osp_order_line_rec.contract_number);
8031        --FETCH c_get_contract_id into l_contract_id;
8032        --CLOSE c_get_contract_id;
8033        IF(l_contract_id is null) THEN
8034           FND_MESSAGE.set_name('AHL','AHL_COM_INVALID_WARRANTY_CONTRACT');
8035           FND_MESSAGE.set_token('CONT_NUM', p_x_osp_order_line_rec.contract_number);
8036           FND_MSG_PUB.add;
8037        END IF;
8038        l_entl_status := 'APPROVAL_PENDING';
8039     ELSE -- If contact number is not associated/removed
8040        l_contract_id := FND_API.G_MISS_NUM;
8041        IF(l_old_contract_id IS NULL AND l_old_entl_status IN ('REVIEW','NOT_APPLICABLE')) THEN
8042           l_entl_status := l_old_entl_status;
8043        ELSE
8044           l_entl_status := 'APPROVAL_PENDING';
8045        END IF;
8046     END IF;
8047    ELSE
8048     l_call_process_entl := 'N';
8049    END IF;
8050 
8051    -- Store the local variables in the Entitlements Record
8052    l_entitlement_rec.osp_order_line_id := p_x_osp_order_line_rec.osp_order_line_id;
8053    l_entitlement_rec.warranty_contract_id    := l_contract_id;
8054    l_entitlement_rec.entitlement_status_code := l_entl_status;
8055 
8056    IF(p_x_osp_order_line_rec.warranty_entitlement_id is not null) THEN -- Entitlements Record Already Exists UPDATE
8057       l_entitlement_rec.warranty_entitlement_id := p_x_osp_order_line_rec.warranty_entitlement_id;
8058       l_entitlement_rec.object_version_number   := l_ovn;
8059       l_entitlement_rec.operation_flag          := AHL_WARRANTY_ENTL_PVT.G_OP_UPDATE;
8060    ELSIF(p_x_osp_order_line_rec.warranty_entitlement_id is null AND l_entl_status = 'APPROVAL_PENDING') THEN
8061       -- CREATE Entitlement record
8062       l_entitlement_rec.operation_flag    := AHL_WARRANTY_ENTL_PVT.G_OP_CREATE;
8063       l_entitlement_rec.visit_task_id     := FND_API.G_MISS_NUM;
8064    ELSE
8065       l_call_process_entl := 'N';
8066    END IF;
8067 
8068    --Adding the first record to the OUT param table type
8069    x_warranty_entl_tbl(0) := l_entitlement_rec;
8070    x_call_process_entl    := l_call_process_entl;
8071 
8072    IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
8073        FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
8074                       L_DEBUG_KEY,
8075                       ' After updating the Entitlements record.' ||
8076                       ' Task Id : '|| l_entitlement_rec.visit_task_id ||
8077                       ' Warranty Contract Id : '|| l_entitlement_rec.warranty_contract_id ||
8078                       ' Entitlement id : '|| l_entitlement_rec.warranty_entitlement_id ||
8079                       ' Entitlement Status Code : '|| l_entitlement_rec.entitlement_status_code ||
8080                       ' Operation Flag : '|| l_entitlement_rec.operation_flag ||
8081                       ' Obj Version Num ' || l_entitlement_rec.object_version_number);
8082    END IF;
8083 
8084    ---------------------------End of API Body---------------------------------------
8085    IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
8086        FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
8087                       L_DEBUG_KEY ||'.end',
8088                       'At the end of PL SQL procedure. ');
8089    END IF;
8090 END Get_Warranty_Rec_for_Update;
8091 
8092 ------------------------------------------------------------------------------------
8093 -- Start of Comments
8094 --  Procedure name    : Get_Entl_rec_for_Delete
8095 --  Type              : Private
8096 --  Function          : To delete/unnassociate the corresponding entitlement record from the OSP Order line.
8097 --  Pre-reqs          :
8098 --  Parameters        : p_osp_order_id      IN  NUMBER
8099 --                    : p_osp_order_line_id IN  NUMBER
8100 --                    : x_warranty_entl_tbl OUT AHL_WARRANTY_ENTL_PVT.Warranty_Entl_Tbl_Type
8101 --
8102 --  End of Comments
8103 
8104 PROCEDURE Get_Entl_rec_for_Delete(
8105         p_osp_order_id      IN         NUMBER := null,
8106         p_osp_order_line_id IN         NUMBER := null,
8107         x_warranty_entl_tbl OUT NOCOPY AHL_WARRANTY_ENTL_PVT.Warranty_Entl_Tbl_Type
8108 ) IS
8109    -- Standard variables
8110    L_API_NAME  CONSTANT VARCHAR2(30)  := 'Get_Entl_rec_for_Delete';
8111    L_DEBUG_KEY CONSTANT VARCHAR2(100) := 'ahl.plsql.' || G_PKG_NAME || '.' || L_API_NAME;
8112    -- Local variables defined for the procedure
8113    l_warranty_entl_id     NUMBER      := null;
8114    l_visit_task_id        NUMBER      := null;
8115    l_osp_line_id          NUMBER      := null;
8116    l_count                NUMBER;
8117    l_ovn                  NUMBER;
8118    l_entitlement_rec      AHL_WARRANTY_ENTL_PVT.Warranty_Entl_Rec_Type;
8119 
8120    -- Cursors defined for the procedure
8121    --Cursor to get entitlement details
8122    CURSOR c_get_entl_details (c_osp_order_line_id IN NUMBER) IS
8123     SELECT AWSE.WARRANTY_ENTITLEMENT_ID, AWSE.VISIT_TASK_ID, AWSE.OBJECT_VERSION_NUMBER
8124     FROM   AHL_WARRANTY_ENTITLEMENTS AWSE
8125     WHERE  AWSE.OSP_ORDER_LINE_ID =  c_osp_order_line_id;
8126 
8127    --Cursor to fetch osp lines for a given osp order
8128    CURSOR c_get_osp_lines (c_osp_order_id IN NUMBER) IS
8129     SELECT OSP_ORDER_LINE_ID
8130     FROM   AHL_OSP_ORDER_LINES
8131     WHERE  OSP_ORDER_ID = c_osp_order_id;
8132 
8133 BEGIN
8134    --------------------Start of API Body-----------------------------------
8135    IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
8136        FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
8137                      L_DEBUG_KEY ||'.begin',
8138                      'At the start of PL SQL procedure.'||
8139                      'OSP Order Id : '||p_osp_order_id|| ' OSP Line id : '||p_osp_order_line_id);
8140    END IF;
8141 
8142    --------------------Get Entitlements Record For Update-------------------
8143    IF(p_osp_order_id is null AND p_osp_order_line_id is null) THEN
8144     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
8145         FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
8146                        L_DEBUG_KEY,
8147                        'Passed osp order id and osp order line id is null');
8148     END IF;
8149     FND_MSG_PUB.add;
8150    END IF;
8151 
8152    IF(p_osp_order_id IS NULL) THEN -- if the order id passed is null then its line delete
8153     OPEN c_get_entl_details(p_osp_order_line_id);
8154     FETCH c_get_entl_details into l_warranty_entl_id, l_visit_task_id, l_ovn;
8155     CLOSE c_get_entl_details;
8156 
8157     IF(l_warranty_entl_id IS NOT NULL) THEN --warranty entitlement exists
8158         IF(l_visit_task_id IS NOT NULL) THEN --entitlement based on visit
8159            l_entitlement_rec.osp_order_line_id       := FND_API.G_MISS_NUM;
8160            l_entitlement_rec.warranty_entitlement_id := l_warranty_entl_id;
8161            l_entitlement_rec.object_version_number   := l_ovn;
8162            l_entitlement_rec.operation_flag          := AHL_WARRANTY_ENTL_PVT.G_OP_UPDATE;
8163         ELSE --entitlement based on OSP
8164            l_entitlement_rec.warranty_entitlement_id := l_warranty_entl_id;
8165            l_entitlement_rec.operation_flag          := AHL_WARRANTY_ENTL_PVT.G_OP_DELETE;
8166         END IF;
8167         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
8168             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
8169                            L_DEBUG_KEY,
8170                            ' Removed OSP Order Line Id ' || p_osp_order_line_id ||
8171                            ' Task Id : ' || l_entitlement_rec.visit_task_id ||
8172                            ' Entitlement id : '|| l_entitlement_rec.warranty_entitlement_id ||
8173                            ' Operation Flag : '|| l_entitlement_rec.operation_flag);
8174         END IF;
8175         x_warranty_entl_tbl(0) := l_entitlement_rec;
8176     END IF;
8177    ELSE -- Osp Order delete
8178     l_count := 0;
8179     OPEN c_get_osp_lines(p_osp_order_id); -- get all osp lines
8180     LOOP
8181        FETCH c_get_osp_lines into l_osp_line_id;
8182        EXIT WHEN c_get_osp_lines%NOTFOUND;
8183 
8184        OPEN c_get_entl_details(l_osp_line_id); --fetch line details
8185        FETCH c_get_entl_details into l_warranty_entl_id, l_visit_task_id, l_ovn;
8186        CLOSE c_get_entl_details;
8187 
8188        IF(l_warranty_entl_id IS NOT NULL) THEN --warranty entitlement exists
8189         IF(l_visit_task_id IS NOT NULL) THEN --entitlement based on visit
8190            l_entitlement_rec.osp_order_line_id       := FND_API.G_MISS_NUM;
8191            l_entitlement_rec.warranty_entitlement_id := l_warranty_entl_id;
8192            l_entitlement_rec.object_version_number   := l_ovn;
8193            l_entitlement_rec.operation_flag          := AHL_WARRANTY_ENTL_PVT.G_OP_UPDATE;
8194         ELSE --entitlement based on OSP
8195            l_entitlement_rec.warranty_entitlement_id := l_warranty_entl_id;
8196            l_entitlement_rec.operation_flag          := AHL_WARRANTY_ENTL_PVT.G_OP_DELETE;
8197         END IF;
8198         IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
8199             FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT,
8200                            L_DEBUG_KEY,
8201                            ' Removed OSP Order Line Id ' || l_osp_line_id ||
8202                            ' Task Id : ' || l_entitlement_rec.visit_task_id ||
8203                            ' Entitlement id : '|| l_entitlement_rec.warranty_entitlement_id ||
8204                            ' Operation Flag : '|| l_entitlement_rec.operation_flag);
8205         END IF;
8206         x_warranty_entl_tbl(l_count) := l_entitlement_rec;
8207        END IF;
8208        l_count := l_count+1;
8209     END LOOP;
8210     CLOSE c_get_osp_lines;
8211    END IF;
8212 
8213    IF(FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
8214       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE,
8215                      L_DEBUG_KEY,
8216                      ' End of API.');
8217    END IF;
8218 END Get_Entl_rec_for_Delete;
8219 --salogan added the following for supplier warranty ends
8220 
8221 END ahl_osp_orders_pvt;
8222 
8223 /*
8224 PROCEDURE nullify_exchange_instance(
8225              p_osp_order_id IN NUMBER,
8226              p_x_osp_order_lines_tbl IN OUT NOCOPY osp_order_lines_tbl_type
8227            ) IS
8228 CURSOR l_order_lines_csr(p_osp_order_id IN NUMBER) IS
8229     SELECT osp_order_line_id, object_version_number, last_update_date, last_updated_by , last_update_login,
8230            osp_order_id, osp_line_number, status_code , po_line_type_id, service_item_id, service_item_description , service_item_uom_code,
8231            need_by_date, ship_by_date, po_line_id, oe_ship_line_id , oe_return_line_id , workorder_id, operation_id,
8232            quantity, exchange_instance_id, attribute_category, attribute1, attribute2, attribute3, attribute4, attribute5, attribute6,
8233            attribute7, attribute8, attribute9, attribute10, attribute11, attribute12, attribute13, attribute14, attribute15
8234    FROM ahl_osp_order_lines
8235    WHERE osp_order_id = p_osp_order_id;
8236 l_osp_order_lines_rec osp_order_line_rec_type;
8237 CURSOR l_order_line_obj_ver_csr(p_osp_order_line_id IN NUMBER) IS
8238    SELECT object_version_number FROM ahl_osp_order_lines
8239    WHERE osp_order_line_id = p_osp_order_line_id;
8240 i  NUMBER;   --index for looping
8241 L_DEBUG_KEY      CONSTANT VARCHAR2(150) := G_LOG_PREFIX || '.nullify_exchange_instance';
8242 BEGIN
8243     IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
8244       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.begin', 'Begin Procedure');
8245     END IF;
8246   OPEN l_order_lines_csr(p_osp_order_id);
8247   LOOP
8248      FETCH l_order_lines_csr INTO l_osp_order_lines_rec.osp_order_line_id         ,
8249                                l_osp_order_lines_rec.object_version_number     ,
8250                                l_osp_order_lines_rec.last_update_date          ,
8251                                l_osp_order_lines_rec.last_updated_by           ,
8252                                l_osp_order_lines_rec.last_update_login         ,
8253                                l_osp_order_lines_rec.osp_order_id              ,
8254                                l_osp_order_lines_rec.osp_line_number           ,
8255                                l_osp_order_lines_rec.status_code               ,
8256                                l_osp_order_lines_rec.po_line_type_id           ,
8257                                l_osp_order_lines_rec.service_item_id           ,
8258                                l_osp_order_lines_rec.service_item_description  ,
8259                                l_osp_order_lines_rec.service_item_uom_code     ,
8260                                l_osp_order_lines_rec.need_by_date              ,
8261                                l_osp_order_lines_rec.ship_by_date              ,
8262                                l_osp_order_lines_rec.po_line_id                ,
8263                                l_osp_order_lines_rec.oe_ship_line_id           ,
8264                                l_osp_order_lines_rec.oe_return_line_id         ,
8265                                l_osp_order_lines_rec.workorder_id              ,
8266                                l_osp_order_lines_rec.operation_id              ,
8267                                l_osp_order_lines_rec.quantity                  ,
8268                                l_osp_order_lines_rec.exchange_instance_id      ,
8269                                l_osp_order_lines_rec.attribute_category        ,
8270                                l_osp_order_lines_rec.attribute1                ,
8271                                l_osp_order_lines_rec.attribute2                ,
8272                                l_osp_order_lines_rec.attribute3                ,
8273                                l_osp_order_lines_rec.attribute4                ,
8274                                l_osp_order_lines_rec.attribute5                ,
8275                                l_osp_order_lines_rec.attribute6                ,
8276                                l_osp_order_lines_rec.attribute7                ,
8277                                l_osp_order_lines_rec.attribute8                ,
8278                                l_osp_order_lines_rec.attribute9                ,
8279                                l_osp_order_lines_rec.attribute10               ,
8280                                l_osp_order_lines_rec.attribute11               ,
8281                                l_osp_order_lines_rec.attribute12               ,
8282                                l_osp_order_lines_rec.attribute13               ,
8283                                l_osp_order_lines_rec.attribute14               ,
8284                                l_osp_order_lines_rec.attribute15               ;
8285      IF(l_order_lines_csr %NOTFOUND) THEN
8286        EXIT;
8287      END IF;
8288      AHL_OSP_ORDER_LINES_PKG.update_row(
8289         p_osp_order_line_id        =>  l_osp_order_lines_rec.osp_order_line_id,
8290         p_object_version_number    =>  l_osp_order_lines_rec.object_version_number + 1,
8291         p_last_update_date         =>  l_osp_order_lines_rec.last_update_date,
8292         p_last_updated_by          =>  l_osp_order_lines_rec.last_updated_by,
8293         p_last_update_login        =>  l_osp_order_lines_rec.last_update_login,
8294         p_osp_order_id             =>  l_osp_order_lines_rec.osp_order_id,
8295         p_osp_line_number          =>  l_osp_order_lines_rec.osp_line_number,
8296         p_status_code              =>  l_osp_order_lines_rec.status_code,
8297         p_po_line_type_id          =>  l_osp_order_lines_rec.po_line_type_id,
8298         p_service_item_id          =>  l_osp_order_lines_rec.service_item_id,
8299         p_service_item_description =>  l_osp_order_lines_rec.service_item_description,
8300         p_service_item_uom_code    =>  l_osp_order_lines_rec.service_item_uom_code,
8301         p_need_by_date             =>  l_osp_order_lines_rec.need_by_date,
8302         p_ship_by_date             =>  l_osp_order_lines_rec.ship_by_date,
8303         p_po_line_id               =>  l_osp_order_lines_rec.po_line_id,
8304         p_oe_ship_line_id          =>  l_osp_order_lines_rec.oe_ship_line_id,
8305         p_oe_return_line_id        =>  l_osp_order_lines_rec.oe_return_line_id,
8306         p_workorder_id             =>  l_osp_order_lines_rec.workorder_id,
8307         p_operation_id             =>  l_osp_order_lines_rec.operation_id,
8308         p_quantity                 =>  l_osp_order_lines_rec.quantity,
8309         p_exchange_instance_id     =>  NULL,
8310         p_inventory_item_id       => l_osp_order_lines_rec.inventory_item_id,
8311         p_inventory_org_id        => l_osp_order_lines_rec.inventory_org_id,
8312         p_inventory_item_uom      => l_osp_order_lines_rec.inventory_item_uom,
8313         p_inventory_item_quantity => l_osp_order_lines_rec.inventory_item_quantity,
8314         p_sub_inventory           => l_osp_order_lines_rec.sub_inventory,
8315         p_lot_number              => l_osp_order_lines_rec.lot_number,
8316         p_serial_number           => l_osp_order_lines_rec.serial_number,
8317 
8318         p_attribute_category       =>  l_osp_order_lines_rec.attribute_category,
8319         p_attribute1               =>  l_osp_order_lines_rec.attribute1,
8320         p_attribute2               =>  l_osp_order_lines_rec.attribute2,
8321         p_attribute3               =>  l_osp_order_lines_rec.attribute3,
8322         p_attribute4               =>  l_osp_order_lines_rec.attribute4,
8323         p_attribute5               =>  l_osp_order_lines_rec.attribute5,
8324         p_attribute6               =>  l_osp_order_lines_rec.attribute6,
8325         p_attribute7               =>  l_osp_order_lines_rec.attribute7,
8326         p_attribute8               =>  l_osp_order_lines_rec.attribute8,
8327         p_attribute9               =>  l_osp_order_lines_rec.attribute9,
8328         p_attribute10              =>  l_osp_order_lines_rec.attribute10,
8329         p_attribute11              =>  l_osp_order_lines_rec.attribute11,
8330         p_attribute12              =>  l_osp_order_lines_rec.attribute12,
8331         p_attribute13              =>  l_osp_order_lines_rec.attribute13,
8332         p_attribute14              =>  l_osp_order_lines_rec.attribute14,
8333         p_attribute15              =>  l_osp_order_lines_rec.attribute15
8334      );
8335   END LOOP;
8336   --get the newest object_version_number for p_x_osp_order_lines_tbl
8337   IF(p_x_osp_order_lines_tbl IS NOT NULL AND p_x_osp_order_lines_tbl.COUNT > 0) THEN
8338     IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
8339           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, ' p_x_osp_order_lines_tbl not null' ||
8340                               'p_x_osp_order_lines_tbl.FIRST: ' || p_x_osp_order_lines_tbl.FIRST ||
8341                               'p_x_osp_order_lines_tbl.LAST:  ' || p_x_osp_order_lines_tbl.LAST);
8342      END IF;
8343     FOR i IN p_x_osp_order_lines_tbl.FIRST..p_x_osp_order_lines_tbl.LAST  LOOP
8344        IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
8345           FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'loop inside table p_x_osp_order_lines_tbl');
8346         END IF;
8347         IF(p_x_osp_order_lines_tbl(i).osp_order_line_id IS NOT NULL) THEN
8348           IF (FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
8349                FND_LOG.STRING(FND_LOG.LEVEL_STATEMENT, L_DEBUG_KEY, 'loop inside table p_x_osp_order_lines_tbl.osp_order_line_id');
8350           END IF;
8351            OPEN l_order_line_obj_ver_csr(p_x_osp_order_lines_tbl(i).osp_order_line_id);
8352            FETCH l_order_line_obj_ver_csr INTO p_x_osp_order_lines_tbl(i).object_version_number;
8353            CLOSE l_order_line_obj_ver_csr;
8354         END IF;
8355     END LOOP;
8356   END IF;
8357   IF (FND_LOG.LEVEL_PROCEDURE >= FND_LOG.G_CURRENT_RUNTIME_LEVEL) THEN
8358       FND_LOG.STRING(FND_LOG.LEVEL_PROCEDURE, L_DEBUG_KEY || '.end', 'End Procedure');
8359   END IF;
8360 END nullify_exchange_instance;
8361 */