DBA Data[Home] [Help]

PACKAGE BODY: APPS.OZF_RESALE_COMMON_PVT

Source


1 PACKAGE BODY OZF_RESALE_COMMON_PVT AS
2 /* $Header: ozfvrscb.pls 120.36.12020000.3 2013/02/12 09:23:28 annsrini ship $ */
3 -------------------------------------------------------------------------------
4 -- PACKAGE:
5 -- OZF_RESALE_COMMON_PVT
6 --
7 -- PURPOSE:
8 -- Private API for common resale functionality across all IDSM batches.
9 --
10 -- HISTORY:
11 -- 02-Oct-2003  Jim Wu    Created
12 -- 28-Feb-2004  Sarvanan  Error Handling, Formating, Changes to error logging
13 --                        and Changes for Workflow.
14 -- 28-May-2007  ateotia   Bug# 5997978 fixed.
15 -- 22-Jun-2007  ateotia   Bug# 6134121 fixed.
16 -- 19-Feb-2009  nirprasa  Bug# 6790803 fixed.
17 -- 15-Apr-2009  ateotia   Bug# 8414563 fixed.
18 -- 06-May-2009  ateotia   Bug# 8489216 fixed.
19 --                        Added the logic for End Customer/Bill_To/Ship_To
20 --                        Party creation.
21 -- 2/17/2010    nepanda   Bug 9131648 : multi currency changes
22 --06-Nov-2011   BKUNJAN    ER 13333298  Added OUT parameter x_utilization_id in Procedure Create_Adj_And_Utilization
23 -------------------------------------------------------------------------------
24 
25 G_PKG_NAME  CONSTANT VARCHAR2(30) := 'OZF_RESALE_COMMON_PVT';
26 G_FILE_NAME CONSTANT VARCHAR2(30) := 'ozfvscb.pls';
27 
28 OZF_DEBUG_HIGH_ON CONSTANT BOOLEAN := FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.g_msg_lvl_debug_high);
29 OZF_DEBUG_LOW_ON  CONSTANT BOOLEAN := FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.g_msg_lvl_debug_low);
30 OZF_UNEXP_ERROR   CONSTANT BOOLEAN := FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.g_msg_lvl_unexp_error);
31 OZF_ERROR         CONSTANT BOOLEAN := FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.g_msg_lvl_error);
32 
33 G_CHBK_UTIL_TYPE       CONSTANT VARCHAR2(30) := 'CHARGEBACK';
34 G_SPP_UTIL_TYPE        CONSTANT VARCHAR2(30) :='UTILIZED';
35 G_TP_ACCRUAL_UTIL_TYPE CONSTANT VARCHAR2(30) :='ADJUSTMENT';
36 G_CHBK_ADJ_TYPE_ID     CONSTANT NUMBER := -10;
37 G_ACCEPT_ALLOWED       CONSTANT VARCHAR2(30) := 'ACCEPT_ALLOWED';
38 G_ACCEPT_CLAIMED       CONSTANT VARCHAR2(30) := 'ACCEPT_CLAIMED';
39 G_ITEM_ORG_ID          NUMBER := FND_PROFILE.value('AMS_ITEM_ORGANIZATION_ID');
40 g_universal_currency   CONSTANT VARCHAR2 (15) := fnd_profile.VALUE ('OZF_UNIV_CURR_CODE');
41 
42 -------------------------------------------------------------------------------
43 
44 
45 ---------------------------------------------------------------------
46 -- PROCEDURE
47 --    Insert_Resale_Log
48 --
49 -- PURPOSE
50 -- This procedure inserts a record in ozf_resale_logs_all table
51 --
52 -- PARAMETERS
53 --
54 --
55 -- NOTES
56 ---------------------------------------------------------------------
57 PROCEDURE Insert_Resale_Log (
58   p_id_value      IN VARCHAR2,
59   p_id_type       IN VARCHAR2,
60   p_error_code    IN VARCHAR2,
61   p_error_message IN VARCHAR2 := NULL,
62   p_column_name   IN VARCHAR2,
63   p_column_value  IN VARCHAR2,
64   x_return_status OUT NOCOPY VARCHAR2 )
65 IS
66   l_api_name                  CONSTANT VARCHAR2(30) := 'Insert_Resale_Log';
67   l_api_version_number        CONSTANT NUMBER   := 1.0;
68   l_log_id                    NUMBER;
69   l_org_id                    NUMBER;
70 BEGIN
71    x_return_status := FND_API.G_RET_STS_SUCCESS;
72    IF OZF_DEBUG_HIGH_ON THEN
73       OZF_UTILITY_PVT.debug_message(l_api_name||': Start');
74    END IF;
75    IF OZF_DEBUG_LOW_ON THEN
76       OZF_UTILITY_PVT.debug_message('id_value:'||p_id_value );
77       OZF_UTILITY_PVT.debug_message('id_type:'||p_id_type );
78       OZF_UTILITY_PVT.debug_message('error_code:'||p_error_code);
79       IF p_error_message is NOT NULL THEN
80          OZF_UTILITY_PVT.debug_message('error_message:'||p_error_message);
81       ELSE
82          OZF_UTILITY_PVT.debug_message('error_message:'||fnd_message.get_string('OZF',p_error_code));
83       END IF;
84       OZF_UTILITY_PVT.debug_message('column_name'||p_column_name);
85       OZF_UTILITY_PVT.debug_message('column_value:'||p_column_value);
86    END IF;
87    --
88 
89    IF p_error_code IS NOT NULL THEN
90       OPEN g_log_id_csr;
91       FETCH g_log_id_csr into l_log_id;
92       CLOSE g_log_id_csr;
93 
94       -- julou bug 6317120. get org_id from table
95       IF p_id_type = 'BATCH' THEN
96         OPEN  OZF_RESALE_COMMON_PVT.gc_batch_org_id(p_id_value);
97         FETCH OZF_RESALE_COMMON_PVT.gc_batch_org_id INTO l_org_id;
98         CLOSE OZF_RESALE_COMMON_PVT.gc_batch_org_id;
99       ELSIF p_id_type = 'LINE' THEN
100         OPEN  OZF_RESALE_COMMON_PVT.gc_line_org_id(p_id_value);
101         FETCH OZF_RESALE_COMMON_PVT.gc_line_org_id INTO l_org_id;
102         CLOSE OZF_RESALE_COMMON_PVT.gc_line_org_id;
103       ELSIF p_id_type = 'IFACE' THEN
104         OPEN  OZF_RESALE_COMMON_PVT.gc_iface_org_id(p_id_value);
105         FETCH OZF_RESALE_COMMON_PVT.gc_iface_org_id INTO l_org_id;
106         CLOSE OZF_RESALE_COMMON_PVT.gc_iface_org_id;
107       END IF;
108 
109       BEGIN
110       OZF_RESALE_LOGS_PKG.Insert_Row(
111          px_resale_log_id           => l_log_id,
112          p_resale_id                => p_id_value,
113          p_resale_id_type           => p_id_type,
114          p_error_code               => p_error_code,
115          p_error_message            => nvl(p_error_message, fnd_message.get_string('OZF',p_error_code)),
116          p_column_name              => p_column_name,
117          p_column_value             => p_column_value,
118          --px_org_id                  => OZF_RESALE_COMMON_PVT.g_org_id
119          px_org_id                  => l_org_id
120       );
121       EXCEPTION
122          WHEN OTHERS THEN
123             OZF_UTILITY_PVT.error_message('OZF_INS_RESALE_LOG_WRG');
124             RAISE FND_API.g_exc_unexpected_error;
125       END;
126    END IF;
127    --
128    IF OZF_DEBUG_HIGH_ON THEN
129       OZF_UTILITY_PVT.debug_message(l_api_name||': End');
130    END IF;
131 EXCEPTION
132    WHEN FND_API.G_EXC_ERROR THEN
133       x_return_status := FND_API.G_RET_STS_ERROR;
134    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
135       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
136    WHEN OTHERS THEN
137       IF OZF_UNEXP_ERROR THEN
138          FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
139       END IF;
140       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
141 END Insert_Resale_Log;
142 
143 ---------------------------------------------------------------------
144 -- PROCEDURE
145 --    Bulk_Insert_Resale_Log
146 --
147 -- PURPOSE
148 -- This procedure inserts a lot of records in ozf_resale_logs_all table
149 --
150 -- PARAMETERS
151 --
152 --
153 -- NOTES
154 ---------------------------------------------------------------------
155 PROCEDURE Bulk_Insert_Resale_Log (
156   p_id_value      IN number_tbl_type,
157   p_id_type       IN VARCHAR2,
158   p_error_code    IN varchar_tbl_type,
159   p_column_name   IN varchar_tbl_type,
160   p_column_value  IN long_varchar_tbl_type,
161   p_batch_id      IN NUMBER, -- bug # 5997978 fixed
162   x_return_status OUT NOCOPY VARCHAR2
163 )
164 IS
165 l_api_name varchar2(30) := 'Bulk_Insert_Resale_Log';
166 -- Start: bug # 5997978 fixed
167 l_batch_org_id NUMBER;
168 l_org_id NUMBER;
169 -- End: bug # 5997978 fixed
170 BEGIN
171    x_return_status := FND_API.G_RET_STS_SUCCESS;
172    IF OZF_DEBUG_HIGH_ON THEN
173       OZF_UTILITY_PVT.debug_message(l_api_name||': Start');
174    END IF;
175    -- Start: bug # 5997978 fixed
176    OPEN g_resale_batch_org_id_csr(p_batch_id);
177    FETCH g_resale_batch_org_id_csr INTO l_batch_org_id;
178    CLOSE g_resale_batch_org_id_csr;
179    l_org_id := MO_GLOBAL.get_valid_org(l_batch_org_id);
180    IF (l_batch_org_id IS NULL OR l_org_id IS NULL) THEN
181          OZF_UTILITY_PVT.error_message(p_message_name => 'OZF_ORG_ID_NOTFOUND');
182          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
183    END IF;
184    -- End: bug # 5997978 fixed
185 
186    -- bulk insert into resale logs table for above id's
187    FORALL i in 1..p_id_value.count
188       INSERT INTO ozf_resale_logs_all (
189          RESALE_LOG_ID,
190          RESALE_ID,
191          RESALE_ID_TYPE,
192          ERROR_CODE,
193          ERROR_MESSAGE,
194          COLUMN_NAME,
195          COLUMN_VALUE,
196          ORG_ID
197       ) VALUES (
198          ozf_resale_logs_all_s.nextval,
199          p_id_value(i),
200          p_id_type,
201          p_error_code(i),
202          FND_MESSAGE.get_string('OZF',p_error_code(i)),
203          p_column_name(i),
204          p_column_value(i),
205          -- Start: bug # 5997978 fixed
206          -- NVL(SUBSTRB(USERENV('CLIENT_INFO'),1,10),-99)
207          l_org_id
208          -- End: bug # 5997978 fixed
209       );
210 
211    IF OZF_DEBUG_HIGH_ON THEN
212       OZF_UTILITY_PVT.debug_message(l_api_name||': End');
213    END IF;
214 EXCEPTION
215    WHEN FND_API.G_EXC_ERROR THEN
216       x_return_status := FND_API.G_RET_STS_ERROR;
217    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
218       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
219    WHEN OTHERS THEN
220       IF OZF_UNEXP_ERROR THEN
221          FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
222       END IF;
223       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
224 END Bulk_Insert_Resale_Log;
225 
226 ---------------------------------------------------------------------
227 -- PROCEDURE
228 --    Log_Null_Values
229 --
230 -- PURPOSE
231 -- This procedure checks null values from ozf_resale_lines_int_all table
232 --
233 -- PARAMETERS
234 --
235 --
236 -- NOTES
237 -- JXWU this proceducre should be moved to preprocess
238 --
239 ---------------------------------------------------------------------
240 PROCEDURE Log_Null_Values (
241    p_batch_id      IN  VARCHAR2,
242    x_return_status OUT NOCOPY VARCHAR2
243 )
244 IS
245    l_api_name                  CONSTANT VARCHAR2(30) := 'Log_Null_Values';
246    l_id_tbl number_tbl_type;
247    l_err_tbl varchar_tbl_type;
248    l_col_tbl varchar_tbl_type;
249    l_val_tbl long_varchar_tbl_type;
250    l_return_status varchar2(1);
251    --
252    l_report_start_date date;
253    l_report_end_date   date;
254 
255 CURSOR batch_info_csr (p_id IN NUMBER) IS
256  SELECT report_start_date, report_end_date
257    FROM ozf_resale_batches
258   WHERE resale_batch_id = p_id;
259 -- bugfix
260 CURSOR null_columns_csr (p_start_date IN DATE, p_end_date IN DATE) IS
261    SELECT resale_line_int_id, 'OZF_RESALE_ORD_NUM_MISS', 'ORDER_NUMBER', NULL
262      FROM ozf_resale_lines_int_all
263     WHERE resale_batch_id = p_batch_id
264       AND status_code IN (G_BATCH_ADJ_OPEN, G_BATCH_ADJ_DISPUTED)
265       AND order_number IS NULL
266     UNION ALL
267    SELECT resale_line_int_id, 'OZF_RESALE_ORD_DATE_MISS', 'DATE_ORDERED', NULL
268      FROM ozf_resale_lines_int_all
269     WHERE resale_batch_id = p_batch_id
270       AND status_code IN (G_BATCH_ADJ_OPEN, G_BATCH_ADJ_DISPUTED)
271       AND date_ordered IS NULL
272     UNION ALL
273    SELECT resale_line_int_id, 'OZF_ORD_DATE_LT_START', 'DATE_ORDERED', TO_CHAR(date_ordered)
274      FROM ozf_resale_lines_int_all
275     WHERE resale_batch_id = p_batch_id
276       AND status_code IN (G_BATCH_ADJ_OPEN, G_BATCH_ADJ_DISPUTED)
277       AND date_ordered IS NOT NULL
278       AND date_ordered < p_start_date
279     UNION ALL
280    SELECT resale_line_int_id, 'OZF_ORD_DATE_GT_END', 'DATE_ORDERED', TO_CHAR(date_ordered)
281      FROM ozf_resale_lines_int_all
282     WHERE resale_batch_id = p_batch_id
283       AND status_code IN (G_BATCH_ADJ_OPEN, G_BATCH_ADJ_DISPUTED)
284       AND date_ordered IS NOT NULL
285       AND date_ordered > p_end_date
286     UNION ALL
287    SELECT resale_line_int_id, 'OZF_RESALE_PRODUCT_ID_MISS', 'INVENTORY_ITEM_ID', NULL
288      FROM ozf_resale_lines_int_all
289     WHERE resale_batch_id = p_batch_id
290       AND status_code IN (G_BATCH_ADJ_OPEN, G_BATCH_ADJ_DISPUTED)
291       AND inventory_item_id IS NULL
292     UNION ALL
293    SELECT resale_line_int_id, 'OZF_RESALE_UOM_MISS', 'UOM_CODE', NULL
294      FROM ozf_resale_lines_int_all
295     WHERE resale_batch_id = p_batch_id
296       AND status_code IN (G_BATCH_ADJ_OPEN, G_BATCH_ADJ_DISPUTED)
297       AND uom_code IS NULL
298     UNION ALL
299    SELECT resale_line_int_id, 'OZF_RESALE_SOLD_FROM_MISS', 'SOLD_FROM_CUST_ACCOUNT_ID', NULL
300      FROM ozf_resale_lines_int_all
301     WHERE resale_batch_id = p_batch_id
302       AND status_code IN (G_BATCH_ADJ_OPEN, G_BATCH_ADJ_DISPUTED)
303       AND sold_from_cust_account_id IS NULL
304     UNION ALL
305    SELECT resale_line_int_id, 'OZF_RESALE_SHIP_FROM_MISS', 'SHIP_FROM_CUST_ACCOUNT_ID', NULL
306      FROM ozf_resale_lines_int_all
307     WHERE resale_batch_id = p_batch_id
308       AND status_code IN (G_BATCH_ADJ_OPEN, G_BATCH_ADJ_DISPUTED)
309       AND ship_from_cust_account_id IS NULL
310     UNION ALL
311    SELECT resale_line_int_id, 'OZF_RESALE_PRICE_LIST_NULL', 'AGREEMENT_ID', NULL
312      FROM ozf_resale_lines_int_all
313     WHERE resale_batch_id = p_batch_id
314       AND status_code IN (G_BATCH_ADJ_OPEN, G_BATCH_ADJ_DISPUTED)
315       AND agreement_type = 'PL'
316       AND agreement_id IS NULL
317     UNION ALL
318    SELECT resale_line_int_id, 'OZF_RESALE_AGREE_NUM_NULL', 'AGREEMENT_ID', NULL
319      FROM ozf_resale_lines_int_all
320     WHERE resale_batch_id = p_batch_id
321       AND status_code IN (G_BATCH_ADJ_OPEN, G_BATCH_ADJ_DISPUTED)
322       AND agreement_type = 'SPO'
323       AND agreement_id IS NULL;
324 
325 BEGIN
326    x_return_status := FND_API.G_RET_STS_SUCCESS;
327    --
328    IF OZF_DEBUG_HIGH_ON THEN
329       OZF_UTILITY_PVT.debug_message(l_api_name||': Start');
330    END IF;
331 
332    -- get batch start and end date
333    OPEN batch_info_csr (p_batch_id);
334    FETCH batch_info_csr INTO l_report_start_date, l_report_end_date;
335    CLOSE batch_info_csr;
336 
337    -- bulk select all lines with missing order numbers
338    OPEN null_columns_csr (l_report_start_date, l_report_end_date);
339    FETCH null_columns_csr BULK COLLECT INTO l_id_tbl, l_err_tbl, l_col_tbl, l_val_tbl;
340    CLOSE null_columns_csr;
341    --
342 
343    IF l_id_tbl.exists(1) THEN
344       -- log disputed lines
345       IF OZF_DEBUG_LOW_ON THEN
346          OZF_UTILITY_PVT.debug_message('Number of errors: ' || l_id_tbl.LAST);
347       END IF;
348       Bulk_Insert_Resale_Log (
349          p_id_value      => l_id_tbl,
350          p_id_type       => G_ID_TYPE_IFACE,
351          p_error_code    => l_err_tbl,
352          p_column_name   => l_col_tbl,
353          p_column_value  => l_val_tbl,
354          p_batch_id      => p_batch_id, --bug # 5997978 fixed
355          x_return_status => l_return_status
356       );
357       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
358          RAISE FND_API.G_EXC_ERROR;
359       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
360          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
361       END IF;
362    END IF;
363    --
364    IF OZF_DEBUG_HIGH_ON THEN
365       OZF_UTILITY_PVT.debug_message(l_api_name||': End');
366    END IF;
367 EXCEPTION
368    WHEN FND_API.G_EXC_ERROR THEN
369       x_return_status := FND_API.G_RET_STS_ERROR;
370    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
371       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
372    WHEN OTHERS THEN
373       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
374       IF OZF_UNEXP_ERROR THEN
375          FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
376       END IF;
377 END Log_Null_Values;
378 
379 ---------------------------------------------------------------------
380 -- PROCEDURE
381 --    Log_Invalid_Values
382 --
383 -- PURPOSE
384 -- This procedure checks invalid values from ozf_resale_lines_int_all table
385 --
386 -- PARAMETERS
387 --
388 -- NOTES:
389 -- JXWU this proceducre should be moved to preprocess
390 --
391 ---------------------------------------------------------------------
392 PROCEDURE Log_Invalid_Values (
393    p_batch_id      IN  VARCHAR2,
394    x_return_status OUT NOCOPY VARCHAR2
395 )
396 IS
397    l_api_name CONSTANT VARCHAR2(30) := 'Log_Invalid_Values';
398    l_id_tbl number_tbl_type;
399    l_err_tbl varchar_tbl_type;
400    l_col_tbl varchar_tbl_type;
401    l_val_tbl long_varchar_tbl_type;
402    l_return_status varchar2(1);
403 -- bugfix 4901702 SQL Repository - sangara
404 CURSOR invalid_columns_csr (p_resale_batch_id IN NUMBER) IS
405 SELECT orsl.resale_line_int_id
406      , 'OZF_CLAIM_CUST_NOT_IN_DB'
407      , 'SOLD_FROM_CUST_ACCOUNT_ID'
408      , to_char(orsl.sold_from_cust_account_id)
409   FROM ozf_resale_lines_int_all orsl
410  WHERE orsl.status_code = 'OPEN'
411    AND orsl.direct_customer_flag = 'T'
412    AND orsl.resale_batch_id = p_resale_batch_id
413    AND orsl.sold_from_cust_account_id IS NOT NULL
414    AND NOT EXISTS ( SELECT 1
415                       FROM hz_cust_accounts hca
416                      WHERE hca.cust_account_id = orsl.sold_from_cust_account_id)
417 UNION ALL
418 SELECT orsl.resale_line_int_id
419      , 'OZF_CLAIM_CUST_NOT_IN_DB'
420      , 'SHIP_FROM_CUST_ACCOUNT_ID'
421      , to_char(orsl.ship_from_cust_account_id)
422   FROM ozf_resale_lines_int_all orsl
423  WHERE orsl.status_code = 'OPEN'
424    AND orsl.direct_customer_flag = 'T'
425    AND orsl.resale_batch_id = p_resale_batch_id
426    AND orsl.ship_from_cust_account_id IS NOT NULL
427    AND NOT EXISTS ( SELECT 1
428                       FROM hz_cust_accounts hca
429                      WHERE hca.cust_account_id = orsl.ship_from_cust_account_id)
430 UNION ALL
431 SELECT orsl.resale_line_int_id
432      , 'OZF_RESALE_UOM_NOT_IN_DB'
433      , 'UOM_CODE'
434      , orsl.uom_code
435   FROM ozf_resale_lines_int_all orsl
436  WHERE orsl.direct_customer_flag ='T'
437    AND orsl.status_code = 'OPEN'
438    AND orsl.resale_batch_id = p_resale_batch_id
439    AND orsl.uom_code IS NOT NULL
440    AND NOT EXISTS ( SELECT 1
441                       FROM mtl_units_of_measure mum
442                      WHERE mum.uom_code = orsl.uom_code )
443 UNION ALL
444 SELECT orsl.resale_line_int_id
445      , 'OZF_RESALE_ORDTYPE_NOT_IN_DB'
446      , 'ORDER_TYPE_ID'
447      , to_char(orsl.order_type_id)
448   FROM ozf_resale_lines_int_all orsl
449  WHERE orsl.status_code = 'OPEN'
450    AND orsl.direct_customer_flag = 'T'
451    AND orsl.order_type_id IS NOT NULL
452    AND orsl.resale_batch_id = p_resale_batch_id
453    AND NOT EXISTS ( SELECT 1
454                       FROM oe_transaction_types_all ottv
455                      WHERE ottv.transaction_type_id = orsl.order_type_id)
456 UNION ALL
457 /*
458 SELECT orsl.resale_line_int_id
459      , 'OZF_RESALE_PRICE_NOT_IN_DB'
460      , 'AGREEMENT_ID'
461      , to_char(orsl.agreement_id)
462   FROM ozf_resale_lines_int_all orsl
463  WHERE orsl.status_code = 'OPEN'
464    AND orsl.direct_customer_flag = 'T'
465    AND orsl.resale_batch_id = p_resale_batch_id
466    AND orsl.agreement_id IS NOT NULL
467    AND NOT EXISTS ( SELECT 1
468                       FROM qp_list_headers_b qlhv
469                      WHERE qlhv.list_header_id = orsl.agreement_id
470                        AND qlhv.list_type_code = 'PRL')
471 UNION ALL
472 */
473 SELECT orsl.resale_line_int_id
474      , 'OZF_RESALE_PRODUCT_NOT_IN_DB'
475      , 'INVENTORY_ITEM_ID'
476      , to_char(orsl.inventory_item_id)
477   FROM ozf_resale_lines_int_all orsl
478  WHERE orsl.status_code = 'OPEN'
479    AND orsl.direct_customer_flag = 'T'
480    AND orsl.resale_batch_id = p_resale_batch_id
481    AND orsl.inventory_item_id IS NOT NULL
482    AND NOT EXISTS ( SELECT 1
483                     FROM mtl_system_items_b msi
484                     WHERE msi.inventory_item_id = orsl.inventory_item_id
485                     AND msi.organization_id = G_ITEM_ORG_ID);
486 
487 BEGIN
488    x_return_status := FND_API.G_RET_STS_SUCCESS;
489    -- Debug Message
490    IF OZF_DEBUG_HIGH_ON THEN
491       OZF_UTILITY_PVT.debug_message(l_api_name||': Start');
492    END IF;
493    -- bulk select all lines with missing order numbers
494    OPEN invalid_columns_csr (p_batch_id);
495    FETCH invalid_columns_csr BULK COLLECT INTO l_id_tbl, l_err_tbl, l_col_tbl, l_val_tbl;
496    CLOSE invalid_columns_csr;
497    --
498 
499    IF l_id_tbl.exists(1) THEN
500       -- log disputed lines
501       IF OZF_DEBUG_LOW_ON THEN
502          OZF_UTILITY_PVT.debug_message('Number of errors: ' || l_id_tbl.LAST);
503       END IF;
504       -- log disputed lines
505       Bulk_Insert_Resale_Log (
506          p_id_value      => l_id_tbl,
507          p_id_type       => G_ID_TYPE_IFACE,
508          p_error_code    => l_err_tbl,
509          p_column_name   => l_col_tbl,
510          p_column_value  => l_val_tbl,
511          p_batch_id      => p_batch_id, --bug # 5997978 fixed
512          x_return_status => l_return_status
513       );
514       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
515          RAISE FND_API.G_EXC_ERROR;
516       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
517          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
518       END IF;
519       --
520    END IF;
521 
522    IF OZF_DEBUG_HIGH_ON THEN
523       OZF_UTILITY_PVT.debug_message(l_api_name||': End');
524    END IF;
525 EXCEPTION
526    WHEN FND_API.G_EXC_ERROR THEN
527       x_return_status := FND_API.G_RET_STS_ERROR;
528    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
529       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
530    WHEN OTHERS THEN
531       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
532       IF OZF_UNEXP_ERROR THEN
533          FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
534       END IF;
535 END Log_Invalid_Values;
536 
537 ---------------------------------------------------------------------
538 -- PROCEDURE
539 --    Bulk_Dispute_Line
540 --
541 -- PURPOSE
542 -- This procedure update disputed lines
543 --
544 -- PARAMETERS
545 --
546 -- NOTES:
547 -- JXWU this proceducre should be moved to preprocess
548 --
549 ---------------------------------------------------------------------
550 PROCEDURE Bulk_Dispute_Line (
551    p_batch_id      IN NUMBER,
552    p_line_status   IN VARCHAR2,
553    x_return_status OUT NOCOPY VARCHAR2
554 )
555 IS
556    l_api_name                  CONSTANT VARCHAR2(30) := 'Bulk_Dispute_Line';
557    l_api_version_number        CONSTANT NUMBER   := 1.0;
558    --
559 --
560 BEGIN
561    x_return_status := FND_API.G_RET_STS_SUCCESS;
562    IF OZF_DEBUG_HIGH_ON THEN
563       OZF_UTILITY_PVT.debug_message(l_api_name||': Start');
564    END IF;
565    --
566    BEGIN
567       UPDATE ozf_resale_lines_int_all orli
568          SET orli.dispute_code = (SELECT orl.error_code
569                                     FROM ozf_resale_logs_all orl
570                                    WHERE orl.resale_id = orli.resale_line_int_id
571                                      AND resale_id_type = 'IFACE'
572                                      AND rownum = 1)
573            , orli.status_code = G_BATCH_ADJ_DISPUTED
574            , followup_action_code = 'C'
575            , response_type = 'CA'
576            , response_code = 'N'
577        WHERE orli.resale_batch_id = p_batch_id
578          AND orli.status_code = p_line_status
579          AND EXISTS( SELECT 1
580                      FROM ozf_resale_logs_all c
581                      WHERE c.resale_id = orli.resale_line_int_id
582                       AND c.resale_id_type = 'IFACE');
583    EXCEPTION
584       WHEN OTHERS THEN
585          IF OZF_UNEXP_ERROR THEN
586             FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
587          END IF;
588          x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
589    END;
590    --
591    IF OZF_DEBUG_HIGH_ON THEN
592       OZF_UTILITY_PVT.debug_message(l_api_name||': End');
593    END IF;
594 EXCEPTION
595    WHEN FND_API.G_EXC_ERROR THEN
596       x_return_status := FND_API.G_RET_STS_ERROR;
597    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
598       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
599    WHEN OTHERS THEN
600       IF OZF_UNEXP_ERROR THEN
601          FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
602       END IF;
603       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
604 END Bulk_Dispute_Line;
605 
606 ---------------------------------------------------------------------
607 -- PROCEDURE
608 --    Update_Batch_Calculations
609 --
610 -- PURPOSE
611 -- ThIS procedure updates batch column based on data processing
612 --
613 -- PARAMETERS
614 --
615 --
616 -- NOTES
617 ---------------------------------------------------------------------
618 PROCEDURE Update_Batch_Calculations (
619     p_api_version            IN  NUMBER
620    ,p_init_msg_list          IN  VARCHAR2 := FND_API.G_FALSE
621    ,p_commit                 IN  VARCHAR2 := FND_API.G_FALSE
622    ,p_validation_level       IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL
623    ,p_resale_batch_id        IN  NUMBER
624    ,x_return_status          OUT NOCOPY   VARCHAR2
625    ,x_msg_data               OUT NOCOPY   VARCHAR2
626    ,x_msg_count              OUT NOCOPY   NUMBER
627 )
628 IS
629 l_api_name          CONSTANT VARCHAR2(30) := 'Update_Batch_Calculations';
630 l_api_version       CONSTANT NUMBER := 1.0;
631 l_full_name         CONSTANT VARCHAR2(60) := G_PKG_NAME ||'.'|| l_api_name;
632 --
633 l_return_status              VARCHAR2(1);
634 
635 l_lines_disputed             NUMBER;
636 l_lines_w_tolerance          NUMBER;
637 l_lines_invalid              NUMBER;
638 l_lines_duplicated           NUMBER;
639 --
640 l_tolerance                  NUMBER;
641 l_header_tolerance_calc_cd   VARCHAR2(30);
642 l_header_tolerance_operand   NUMBER;
643 --
644 l_calculated_amount          NUMBER;
645 l_total_accepted_amount      NUMBER;
646 l_total_allowed_amount       NUMBER;
647 l_total_disputed_amount      NUMBER;
648 l_total_claimed_amount       NUMBER;
649 l_total_duplicated_amount    NUMBER;
650 l_status_code                VARCHAR2(30);
651 l_need_tolerance             BOOLEAN;
652 --
653 CURSOR tolerance_line_count_csr (p_id NUMBER)IS
654 SELECT count(1)
655   FROM ozf_resale_lines_int
656  WHERE status_code = OZF_RESALE_COMMON_PVT.G_BATCH_ADJ_PROCESSED
657    AND tolerance_flag = 'T'
658    AND resale_batch_id = p_id;
659 
660 CURSOR invalid_line_count_csr (p_id NUMBER)IS
661 SELECT count(1)
662   FROM ozf_resale_lines_int
663  WHERE status_code = OZF_RESALE_COMMON_PVT.G_BATCH_ADJ_DISPUTED
664    AND dispute_code = OZF_RESALE_COMMON_PVT.G_INVALD_DISPUTE_CODE
665    AND resale_batch_id = p_id;
666 
667 CURSOR header_tolerance_csr(p_id in NUMBER) IS
668 SELECT header_tolerance_operand, header_tolerance_calc_code
669   FROM ozf_resale_batches
670  WHERE resale_batch_id = p_id;
671 
672 CURSOR csr_duplicated_dispute_amount(p_resale_batch_id IN NUMBER) IS
673   SELECT NVL(COUNT(resale_line_int_id), 0)
674        , NVL(SUM(total_claimed_amount), 0)
675   FROM ozf_resale_lines_int_all
676   WHERE resale_batch_id = p_resale_batch_id
677   AND status_code = 'DUPLICATED';
678 
679 BEGIN
680    -- Standard begin of API savepoint
681    SAVEPOINT  Update_Batch_Calculations;
682 
683    -- Standard call to check FOR call compatibility.
684    IF NOT FND_API.Compatible_API_Call (
685       l_api_version,
686       p_api_version,
687       l_api_name,
688       G_PKG_NAME)
689    THEN
690       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
691    END IF;
692 
693    --Initialize message if p_init_msg_list IS TRUE.
694    IF FND_API.To_BOOLEAN (p_init_msg_list) THEN
695       FND_MSG_PUB.initialize;
696    END IF;
697 
698    -- Debug Message
699    IF OZF_DEBUG_HIGH_ON THEN
700       OZF_UTILITY_PVT.debug_message(p_message_text => l_full_name||': Start');
701    END IF;
702    -- Initialize API return status to sucess
703    x_return_status := FND_API.G_RET_STS_SUCCESS;
704 
705    -- get data regard thIS process
706    OPEN  OZF_RESALE_COMMON_PVT.g_disputed_line_count_csr (p_resale_batch_id);
707    FETCH OZF_RESALE_COMMON_PVT.g_disputed_line_count_csr INTO l_lines_disputed;
708    CLOSE OZF_RESALE_COMMON_PVT.g_disputed_line_count_csr;
709 
710    OPEN  tolerance_line_count_csr (p_resale_batch_id);
711    FETCH tolerance_line_count_csr INTO l_lines_w_tolerance;
712    CLOSE tolerance_line_count_csr;
713 
714    -- get header level tolerance
715    OPEN header_tolerance_csr(p_resale_batch_id);
716    FETCH header_tolerance_csr INTO l_header_tolerance_operand,l_header_tolerance_calc_cd;
717    CLOSE header_tolerance_csr;
718 
719    OPEN invalid_line_count_csr(p_resale_batch_id);
720    FETCH invalid_line_count_csr INTO l_lines_invalid;
721    CLOSE invalid_line_count_csr;
722 
723    OPEN  OZF_RESALE_COMMON_PVT.g_total_amount_csr (p_resale_batch_id);
724    FETCH OZF_RESALE_COMMON_PVT.g_total_amount_csr INTO l_calculated_amount,
725                                                        l_total_claimed_amount,
726                                                        l_total_accepted_amount,
727                                                        l_total_allowed_amount,
728                                                        l_total_disputed_amount;
729    CLOSE OZF_RESALE_COMMON_PVT.g_total_amount_csr;
730 
731    --bug # 6134121 fixed by ateotia(+)
732    OPEN csr_duplicated_dispute_amount(p_resale_batch_id);
733    FETCH csr_duplicated_dispute_amount INTO l_lines_duplicated
734                                           , l_total_duplicated_amount;
735    CLOSE csr_duplicated_dispute_amount;
736    --bug # 6134121 fixed by ateotia(-)
737 
738    IF OZF_DEBUG_LOW_ON THEN
739       OZF_UTILITY_PVT.debug_message('diputed line:' || l_lines_disputed || ' tolerance line' || l_lines_w_tolerance);
740    END IF;
741    --bug # 6134121 fixed by ateotia(+)
742    --IF l_lines_disputed = 0 THEN
743    IF (l_lines_disputed = 0 AND l_lines_duplicated = 0)THEN
744    --bug # 6134121 fixed by ateotia(-)
745       -- Need to check header tolerance
746       IF l_header_tolerance_operand IS NULL or
747          l_header_tolerance_calc_cd IS NULL
748       THEN
749          -- No need for tolerance
750          l_need_tolerance := false;
751       ELSE
752          l_need_tolerance := true;
753          -- Check tolerance level
754          -- % will be based on the transaction value of thIS batch
755          IF l_header_tolerance_calc_cd = '%' THEN
756             --Bug# 8418811 fixed by muthsubr(+)
757             --l_tolerance := l_total_allowed_amount * (l_header_tolerance_operand /100);
758             l_tolerance := ABS(l_total_allowed_amount) * (l_header_tolerance_operand /100);
759             --Bug# 8418811 fixed by muthsubr(-)
760          ELSE
761             l_tolerance := l_header_tolerance_operand;
762          END IF;
763       END IF;
764 
765       IF l_need_tolerance THEN
766          --Bug# 8418811 fixed by muthsubr(+)
767          /*
768          -- BUG 4879544 (+)
769          -- IF l_total_accepted_amount <= l_total_claimed_amount + l_tolerance AND
770          --   l_total_accepted_amount >= l_total_claimed_amount - l_tolerance THEN
771          IF l_total_allowed_amount - l_tolerance <= l_total_accepted_amount  AND
772             l_total_allowed_amount + l_tolerance >= l_total_accepted_amount  THEN
773          -- BUG 4879544 (-)
774          */
775          IF ABS(l_total_allowed_amount) - l_tolerance <= ABS(l_total_accepted_amount) AND
776             ABS(l_total_allowed_amount) + l_tolerance >= ABS(l_total_accepted_amount) THEN
777          --Bug# 8418811 fixed by muthsubr(-)
778             l_status_code := 'PROCESSED';
779          ELSE
780             l_status_code := 'DISPUTED';
781             -- BUG 4879544 (+)
782             Insert_Resale_Log (
783                p_id_value      => p_resale_batch_id,
784                p_id_type       => 'BATCH',
785                p_error_code    => 'OZF_BATCH_AMT_OUT_TOLERANCE',
786                p_column_name   => 'ALLOWED_AMOUNT',
787                p_column_value  => l_total_allowed_amount,
788                x_return_status => l_return_status
789             );
790             IF l_return_status = FND_API.G_RET_STS_ERROR THEN
791                RAISE FND_API.G_EXC_ERROR;
792             ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
793                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
794             END IF;
795             -- BUG 4879544 (-)
796          END IF;
797       ELSE
798          -- No need to check tolerance
799          l_status_code := 'PROCESSED';
800       END IF;
801    ELSE
802       -- batch IS in dispute
803       l_status_code := 'DISPUTED';
804    END IF;
805 
806    -- invalid lines are the lines with status 'DISPUTED' AND dISpute code as 'INVLD'
807    -- lines_invalid = l_lines_invalid,
808    -- Lastly, I will UPDATE the batch
809 
810    --bug # 6134121 fixed by ateotia(+)
811    /*OPEN csr_duplicated_dispute_amount(p_resale_batch_id);
812    FETCH csr_duplicated_dispute_amount INTO l_lines_duplicated
813                                           , l_total_duplicated_amount;
814    CLOSE csr_duplicated_dispute_amount;*/
815    --bug # 6134121 fixed by ateotia(-)
816 
817    BEGIN
818       UPDATE ozf_resale_batches_all
819       SET status_code = l_status_code,
820           allowed_amount =l_total_allowed_amount,
821           accepted_amount = l_total_accepted_amount,
822           disputed_amount = ABS(l_total_disputed_amount) + ABS(l_total_duplicated_amount),
823           lines_w_tolerance = l_lines_w_tolerance,
824           lines_disputed = l_lines_disputed + l_lines_duplicated,
825           lines_invalid = l_lines_invalid
826       WHERE resale_batch_id = p_resale_batch_id;
827    EXCEPTION
828       WHEN OTHERS THEN
829          OZF_UTILITY_PVT.error_message('OZF_UPD_RESALE_BATCH_WRG');
830          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
831    END;
832 
833    -- Debug Message
834    IF OZF_DEBUG_HIGH_ON THEN
835       OZF_UTILITY_PVT.debug_message(l_full_name||': End');
836    END IF;
837 
838    --Standard call to get message count AND if count=1, get the message
839    FND_MSG_PUB.Count_and_Get (
840       p_encoded => FND_API.G_FALSE,
841       p_count => x_msg_count,
842       p_data  => x_msg_data
843    );
844 EXCEPTION
845    WHEN FND_API.G_EXC_ERROR THEN
846       ROLLBACK TO Update_Batch_Calculations;
847       x_return_status := FND_API.G_RET_STS_ERROR;
848       -- Standard call to get message count AND IF count=1, get the message
849       FND_MSG_PUB.Count_and_Get (
850           p_encoded => FND_API.G_FALSE,
851           p_count => x_msg_count,
852           p_data  => x_msg_data
853       );
854    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
855       ROLLBACK TO Update_Batch_Calculations;
856       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
857       -- Standard call to get message count AND IF count=1, get the message
858       FND_MSG_PUB.Count_and_Get (
859           p_encoded => FND_API.G_FALSE,
860           p_count => x_msg_count,
861           p_data  => x_msg_data
862       );
863    WHEN OTHERS THEN
864       ROLLBACK TO Update_Batch_Calculations;
865       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
866       IF OZF_UNEXP_ERROR THEN
867          FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
868       END IF;
869       -- Standard call to get message count AND IF count=1, get the message
870       FND_MSG_PUB.Count_and_Get (
871           p_encoded => FND_API.G_FALSE,
872           p_count => x_msg_count,
873           p_data  => x_msg_data
874       );
875 END Update_Batch_Calculations;
876 
877 ---------------------------------------------------------------------
878 -- PROCEDURE
879 --    Update_Line_Calculations
880 --
881 -- PURPOSE
882 --    This procedure update ozf_lines_int_all table based on the data processing
883 -- PARAMETERS
884 --    x_return_status  out VARCHAR2
885 --
886 -- NOTES
887 --
888 ---------------------------------------------------------------------
889 PROCEDURE Update_Line_Calculations(
890     p_resale_line_int_rec IN OZF_RESALE_COMMON_PVT.g_interface_rec_csr%ROWTYPE,
891     p_unit_price          IN NUMBER,
892     p_line_quantity       IN NUMBER,
893     p_allowed_amount      IN NUMBER,
894     x_return_status       OUT NOCOPY VARCHAR2
895 )
896 IS
897 l_api_name       CONSTANT VARCHAR2(30) := 'Update_Line_Calculations';
898 l_api_version    CONSTANT NUMBER       := 1.0;
899 l_full_name      CONSTANT VARCHAR2(60) := G_PKG_NAME ||'.'|| l_api_name;
900 l_return_status           VARCHAR2(1);
901 l_allowed_or_claimed      VARCHAR2(30);
902 l_accepted_amount         NUMBER;
903 l_tolerance               NUMBER;
904 l_status_code             VARCHAR2(30);
905 l_tolerance_flag          VARCHAR2(1);
906 l_line_tolerance_amount   NUMBER;
907 l_line_tolerance_calc_cd  VARCHAR2(30);
908 l_line_tolerance_operand  NUMBER;
909 l_followup_action_code    VARCHAR2(30) := NULL;
910 l_response_type           VARCHAR2(30) := NULL;
911 l_dispute_code            VARCHAR2(30) := NULL;
912 l_net_adjusted_amount     NUMBER;
913 l_total_accepted_amount   NUMBER;
914 l_total_allowed_amount    NUMBER;
915 
916 CURSOR line_tolerance_csr(p_id in NUMBER) IS
917 SELECT line_tolerance_operand,  line_tolerance_calc_code
918   FROM ozf_resale_batches
919  WHERE resale_batch_id = p_id;
920 
921 CURSOR allowed_or_claimed_csr IS
922 SELECT ship_debit_calc_type
923 from ozf_sys_parameters;
924 --Bug# 8418811 fixed by muthsubr(+)
925 /*
926 -- bug 5969118 Ship and Debit return order generates positive claim amount
927 CURSOR c_batch_type(p_batch_id NUMBER) IS
928 SELECT batch_type
929 FROM   ozf_resale_batches_all
930 WHERE  resale_batch_id = p_batch_id;
931 l_batch_type VARCHAR2(30);
932 -- bug 5969118 end
933 */
934 --Bug# 8418811 fixed by muthsubr(-)
935 
936 BEGIN
937    -- Standard begin of API savepoint
938    SAVEPOINT Update_Line_Calculations;
939 
940    -- Debug Message
941    IF OZF_DEBUG_HIGH_ON THEN
942       OZF_UTILITY_PVT.debug_message(l_full_name||': Start');
943    END IF;
944    IF OZF_DEBUG_LOW_ON THEN
945       OZF_UTILITY_PVT.debug_message('unit_price:'||p_unit_price);
946       OZF_UTILITY_PVT.debug_message('line_quantity:'||p_line_quantity);
947       OZF_UTILITY_PVT.debug_message('Allowed_amount:'|| p_allowed_amount);
948    END IF;
949 
950    -- Initialize API return status to sucess
951    x_return_status := FND_API.G_RET_STS_SUCCESS;
952 
953    IF p_resale_line_int_rec.claimed_amount IS NOT NULL THEN
954       --bug 6790803 for negative quantity
955       --IF p_allowed_amount = p_resale_line_int_rec.claimed_amount THEN
956       --Bug# 8418811 fixed by muthsubr(+)
957       --IF abs(p_allowed_amount) = p_resale_line_int_rec.claimed_amount THEN
958       IF ABS(p_allowed_amount) = ABS(p_resale_line_int_rec.claimed_amount) THEN
959       --Bug# 8418811 fixed by muthsubr(-)
960          -- No dispute in line as allowed and claimed are same
961          l_status_code := OZF_RESALE_COMMON_PVT.G_BATCH_ADJ_PROCESSED;
962          l_line_tolerance_amount := 0;
963          l_tolerance_flag := 'F';
964          l_accepted_amount := p_allowed_amount;
965       ELSE
966          -- Check tolerance level
967          OPEN line_tolerance_csr (p_resale_line_int_rec.resale_batch_id);
968          FETCH line_tolerance_csr INTO l_line_tolerance_operand,l_line_tolerance_calc_cd;
969          CLOSE line_tolerance_csr;
970 
971          -- tolerance % will be based on unit_price, or the total of the transaction
972          IF l_line_tolerance_calc_cd IS NULL THEN
973             l_tolerance := 0;
974          ELSE
975             IF l_line_tolerance_calc_cd = '%' THEN
976                l_tolerance := p_unit_price * l_line_tolerance_operand / 100;
977             ELSE
978                l_tolerance := l_line_tolerance_operand;
979             END IF;
980          END IF;
981 
982          -- Set lines that do not fall INTO tolerence as DISPUTED
983          --Bug# 8418811 fixed by muthsubr(+)
984          /*IF p_allowed_amount - l_tolerance <= p_resale_line_int_rec.claimed_amount AND
985             p_allowed_amount + l_tolerance >= p_resale_line_int_rec.claimed_amount THEN*/
986          IF ABS(p_allowed_amount) - l_tolerance <= ABS(p_resale_line_int_rec.claimed_amount) AND
987             ABS(p_allowed_amount) + l_tolerance >= ABS(p_resale_line_int_rec.claimed_amount) THEN
988          --Bug# 8418811 fixed by muthsubr(-)
989             l_status_code := OZF_RESALE_COMMON_PVT.G_BATCH_ADJ_PROCESSED;
990             l_line_tolerance_amount := l_tolerance;
991             l_tolerance_flag := 'T';
992 
993             -- use system parameter flag to determine with amount assign to accepted amount
994             OPEN allowed_or_claimed_csr;
995             FETCH allowed_or_claimed_csr into l_allowed_or_claimed;
996             CLOSE allowed_or_claimed_csr;
997 
998             -- default to allowed
999             IF l_allowed_or_claimed IS NULL THEN
1000                l_allowed_or_claimed := G_ACCEPT_ALLOWED;
1001             END IF;
1002 
1003             IF l_allowed_or_claimed = G_ACCEPT_ALLOWED THEN
1004                l_accepted_amount := p_allowed_amount;
1005             ELSE
1006                l_accepted_amount := p_resale_line_int_rec.claimed_amount;
1007             END IF;
1008          ELSE
1009             l_status_code := OZF_RESALE_COMMON_PVT.G_BATCH_ADJ_DISPUTED;
1010             l_line_tolerance_amount := l_tolerance;
1011             l_tolerance_flag := 'F';
1012             l_dispute_code := 'OZF_AMT_NOT_MATCH';
1013             l_followup_action_code := 'C';
1014             l_response_type := 'CA';
1015 
1016             -- BUG 4879544 (+)
1017             Insert_Resale_Log (
1018                p_id_value      => p_resale_line_int_rec.resale_line_int_id,
1019                p_id_type       => 'IFACE',
1020                p_error_code    => 'OZF_IFACE_AMT_OUT_TOLERANCE',
1021                p_column_name   => 'LINE_TOLERANCE_AMOUNT',
1022                p_column_value  => l_line_tolerance_amount,
1023                x_return_status => l_return_status
1024             );
1025             IF l_return_status = FND_API.G_RET_STS_ERROR THEN
1026                RAISE FND_API.G_EXC_ERROR;
1027             ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
1028                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1029             END IF;
1030             -- BUG 4879544 (-)
1031          END if;
1032       END IF;
1033       --Bug# 8418811 fixed by muthsubr(+)
1034       --l_net_adjusted_amount := p_resale_line_int_rec.claimed_amount - l_accepted_amount;
1035       l_net_adjusted_amount := ABS(p_resale_line_int_rec.claimed_amount) - ABS(l_accepted_amount);
1036       --Bug# 8418811 fixed by muthsubr(-)
1037    ELSE
1038       --Here user did not specific the claimed amount, I will calculate it based on the request
1039       l_status_code := OZF_RESALE_COMMON_PVT.G_BATCH_ADJ_PROCESSED;
1040       l_accepted_amount := p_allowed_amount;
1041       l_line_tolerance_amount := null;
1042       l_tolerance_flag := 'F';
1043       l_net_adjusted_amount := NULL;
1044    END IF;
1045 
1046    -- Update Line
1047    BEGIN
1048       --Bug# 8418811 fixed by muthsubr(+)
1049       /*
1050       -- bug 5969118 Ship and Debit return order generates positive claim amount
1051       OPEN  c_batch_type(p_resale_line_int_rec.resale_batch_id);
1052       FETCH c_batch_type INTO l_batch_type;
1053       CLOSE c_batch_type;
1054 
1055       IF l_batch_type = 'SHIP_DEBIT' AND p_resale_line_int_rec.resale_transfer_type = 'BN' THEN
1056       */
1057       IF p_resale_line_int_rec.resale_transfer_type = 'BN' THEN
1058       --Bug# 8418811 fixed by muthsubr(-)
1059         l_total_accepted_amount := ABS(l_accepted_amount * p_line_quantity) * -1;
1060         l_total_allowed_amount := ABS(p_allowed_amount * p_line_quantity) * -1;
1061       ELSE
1062         l_total_accepted_amount := l_accepted_amount * ABS(p_line_quantity);
1063         l_total_allowed_amount := p_allowed_amount * ABS(p_line_quantity);
1064       END IF;
1065       -- bug 5969118 end
1066 
1067       --//Fix for Bug: 16083006
1068       l_total_accepted_amount := OZF_UTILITY_PVT.CurrRound(l_total_accepted_amount, p_resale_line_int_rec.currency_code);
1069       l_total_allowed_amount  := OZF_UTILITY_PVT.CurrRound(l_total_allowed_amount, p_resale_line_int_rec.currency_code);
1070 
1071       UPDATE ozf_resale_lines_int_all
1072       SET accepted_amount = l_accepted_amount,
1073 --         total_accepted_amount = l_accepted_amount * ABS(p_line_quantity),
1074          -- bug 5969118 Ship and Debit return order generates positive claim amount
1075          total_accepted_amount = l_total_accepted_amount,
1076          -- bug 5969118 end
1077          allowed_amount = p_allowed_amount,
1078 --         total_allowed_amount = p_allowed_amount * ABS(p_line_quantity),
1079          -- bug 5969118 Ship and Debit return order generates positive claim amount
1080          total_allowed_amount = l_total_allowed_amount,
1081          -- bug 5969118 end
1082          net_adjusted_amount = l_net_adjusted_amount,
1083          calculated_price  = p_unit_price,
1084          acctd_calculated_price = p_resale_line_int_rec.acctd_calculated_price,
1085          calculated_amount  = p_unit_price *  p_line_quantity,
1086          acctd_selling_price = p_resale_line_int_rec.acctd_selling_price,
1087          exchange_rate = p_resale_line_int_rec.exchange_rate,
1088          exchange_rate_date = p_resale_line_int_rec.exchange_rate_date,
1089          exchange_rate_type = p_resale_line_int_rec.exchange_rate_type,
1090          status_code = l_status_code,
1091          dispute_code = l_dispute_code,
1092          line_tolerance_amount = l_line_tolerance_amount,
1093          tolerance_flag = l_tolerance_flag,
1094          followup_action_code = l_followup_action_code,
1095          response_type = l_response_type,
1096          response_code = decode(l_status_code, OZF_RESALE_COMMON_PVT.G_BATCH_ADJ_DISPUTED, 'N',
1097                                                OZF_RESALE_COMMON_PVT.G_BATCH_ADJ_PROCESSED, 'Y')
1098       WHERE resale_line_int_id = p_resale_line_int_rec.resale_line_int_id;
1099    EXCEPTION
1100       WHEN OTHERS THEN
1101          IF OZF_UNEXP_ERROR THEN
1102             FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
1103          END IF;
1104          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1105    END;
1106 
1107    -- Debug Message
1108    IF OZF_DEBUG_HIGH_ON THEN
1109       OZF_UTILITY_PVT.debug_message(l_full_name||': End');
1110    END IF;
1111 EXCEPTION
1112    WHEN FND_API.G_EXC_ERROR THEN
1113       ROLLBACK TO Update_Line_Calculations;
1114       x_return_status := FND_API.G_RET_STS_ERROR;
1115    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1116       ROLLBACK TO Update_Line_Calculations;
1117       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1118    WHEN OTHERS THEN
1119       ROLLBACK TO Update_Line_Calculations;
1120       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1121       IF OZF_UNEXP_ERROR THEN
1122          FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
1123       END IF;
1124 END Update_Line_Calculations;
1125 
1126 ---------------------------------------------------------------------
1127 -- PROCEDURE
1128 --    Check_Duplicate_Line
1129 --
1130 -- PURPOSE
1131 --    This procedure tries to see whether the current line AND adjustments have been sent before.
1132 --
1133 -- PARAMETERS
1134 --
1135 --
1136 -- NOTES
1137 ---------------------------------------------------------------------
1138 PROCEDURE Check_Duplicate_Line(
1139     p_api_version_number         IN  NUMBER
1140    ,p_init_msg_LIST              IN  VARCHAR2   := FND_API.G_FALSE
1141    ,p_commit                     IN  VARCHAR2   := FND_API.G_FALSE
1142    ,p_validation_level           IN  NUMBER     := FND_API.G_VALID_LEVEL_FULL
1143    ,p_resale_line_int_id         IN  NUMBER
1144    ,p_direct_customer_flag       IN  VARCHAR2
1145    ,p_claimed_amount             IN  NUMBER
1146    ,p_batch_type                 IN  VARCHAR2
1147    ,x_dup_line_id                OUT NOCOPY     NUMBER
1148    ,x_dup_adjustment_id          OUT NOCOPY     NUMBER
1149    ,x_reprocessing               OUT NOCOPY     BOOLEAN
1150    ,x_return_status              OUT NOCOPY     VARCHAR2
1151    ,x_msg_count                  OUT NOCOPY     NUMBER
1152    ,x_msg_data                   OUT NOCOPY     VARCHAR2
1153 )IS
1154 l_api_name          CONSTANT VARCHAR2(30) := 'Check_Duplicate_Line';
1155 l_api_version       CONSTANT NUMBER := 1.0;
1156 l_full_name         CONSTANT VARCHAR2(60) := G_PKG_NAME ||'.'|| l_api_name;
1157 --
1158 l_adjustment_id  NUMBER;
1159 l_claimed_amount NUMBER;
1160 l_line_id        NUMBER := NULL;
1161 l_tracing_flag   VARCHAR2(1); --Bug# 8414563 fixed by ateotia
1162 l_dup_chk_attrb_unique_prfl varchar2(250):= FND_PROFILE.value('OZF_DUP_CHECK_ATTRB_UNIQUE');
1163 l_addl_attrb_dup_check_prfl  varchar2(250):= FND_PROFILE.value('OZF_DUP_CHECK_ADDL_ATTRB');
1164 
1165 
1166 CURSOR dup_line_direct_resale_csr ( p_resale_line_int_id IN NUMBER )
1167 IS
1168 SELECT orl.resale_line_id
1169   -- Bug 4670154 (+)
1170   FROM ozf_resale_lines_all orl,
1171        ozf_resale_lines_int_all orli
1172   /*
1173   FROM ozf_resale_lines orl,
1174        ozf_resale_lines_int orli
1175   */
1176   -- Bug 4670154 (-)
1177  WHERE orl.order_number = orli.order_number
1178    AND orl.date_ordered =  orli.date_ordered
1179    -- 6704619 (+)
1180    AND (orl.date_shipped = orli.date_shipped
1181        OR (orl.date_shipped IS NULL AND orli.date_shipped IS NULL))
1182    -- 6704619 (-)
1183    --AND orl.invoice_number = orli.invoice_number
1184    --AND orl.date_invoiced = orli.date_invoiced
1185    AND orl.inventory_item_id = orli.inventory_item_id
1186    AND orl.quantity = orli.quantity
1187    AND orl.uom_code = orli.uom_code
1188    AND NVL(orl.sold_from_cust_account_id,FND_API.G_MISS_NUM) = NVL(orli.sold_from_cust_account_id,FND_API.G_MISS_NUM)
1189    --AND orl.ship_from_cust_account_id = orli.sold_from_cust_account_id
1190    AND NVL(orl.ship_from_cust_account_id,FND_API.G_MISS_NUM) = NVL(orli.ship_from_cust_account_id,FND_API.G_MISS_NUM)
1191    AND orl.direct_customer_flag = 'T'
1192    AND NVL(orl.bill_to_cust_account_id,FND_API.G_MISS_NUM) = NVL(orli.bill_to_cust_account_id,FND_API.G_MISS_NUM)
1193    AND orli.resale_line_int_id = p_resale_line_int_id
1194    AND rownum = 1;
1195 
1196 CURSOR dup_line_indirect_resale_csr ( p_resale_line_int_id IN NUMBER )
1197 IS
1198 SELECT orl.resale_line_id
1199   -- Bug 4670154 (+)
1200   FROM ozf_resale_lines_all orl,
1201        ozf_resale_lines_int_all orli
1202   /*
1203   FROM ozf_resale_lines orl,
1204        ozf_resale_lines_int orli
1205   */
1206   -- Bug 4670154 (-)
1207  WHERE orl.order_number = orli.order_number
1208    AND nvl (orl.order_line_number, 0) = nvl (orli.order_line_number, 0)   -- For Bug#9447673 SSD IDSM ER
1209    AND orl.date_ordered =  orli.date_ordered
1210    -- 6704619 (+)
1211    AND (orl.date_shipped = orli.date_shipped
1212        OR (orl.date_shipped IS NULL AND orli.date_shipped IS NULL))
1213    -- 6704619 (-)
1214    --AND orl.invoice_number = orli.invoice_number
1215    --AND orl.date_invoiced = orli.date_invoiced
1216    AND orl.inventory_item_id = orli.inventory_item_id
1217    AND orl.quantity = orli.quantity
1218    AND orl.uom_code = orli.uom_code
1219    AND NVL(orl.sold_from_cust_account_id,FND_API.G_MISS_NUM) = NVL(orli.sold_from_cust_account_id,FND_API.G_MISS_NUM)
1220    --AND orl.ship_from_cust_account_id = orli.sold_from_cust_account_id
1221    AND NVL(orl.ship_from_cust_account_id,FND_API.G_MISS_NUM) = NVL(orli.ship_from_cust_account_id,FND_API.G_MISS_NUM)
1222    AND orl.direct_customer_flag = 'F'
1223    AND NVL(orl.bill_to_party_name,FND_API.G_MISS_NUM) = NVL(orli.bill_to_party_name,FND_API.G_MISS_NUM)
1224    AND orli.resale_line_int_id = p_resale_line_int_id
1225    AND rownum = 1;
1226 
1227 CURSOR dup_adj_csr(
1228          p_line_id IN NUMBER,
1229          p_batch_type IN VARCHAR2)
1230 IS
1231 SELECT orsa.resale_adjustment_id
1232      , orsa.claimed_amount
1233   -- Bug 4670154 (+)
1234   FROM ozf_resale_adjustments_all orsa
1235      , ozf_resale_batches_all orsb
1236   /*
1237   FROM ozf_resale_adjustments orsa
1238      , ozf_resale_batches orsb
1239   */
1240   -- Bug 4670154 (-)
1241  WHERE orsa.resale_line_id = p_line_id
1242    AND orsa.resale_batch_id = orsb.resale_batch_id
1243    AND orsb.batch_type = p_batch_type
1244   -- Bug 4670154 (+)
1245    AND orsa.list_header_id IS NULL
1246    AND orsa.list_line_id IS NULL;
1247   -- Bug 4670154 (-)
1248 
1249 CURSOR dup_line_direct_iface_csr ( p_resale_line_int_id IN NUMBER )
1250 IS
1251 SELECT orlo.resale_line_int_id
1252   -- Bug 4670154 (+)
1253   FROM ozf_resale_lines_int_all orlo,
1254        ozf_resale_lines_int_all orli
1255   /*
1256   FROM ozf_resale_lines_int orlo,
1257        ozf_resale_lines_int orli
1258   */
1259   -- Bug 4670154 (-)
1260  WHERE orlo.order_number = orli.order_number
1261    AND nvl (orlo.order_line_number, 0) = nvl (orli.order_line_number, 0) -- For Bug#9447673 SSD IDSM ER
1262    AND orlo.date_ordered =  orli.date_ordered
1263    -- 6704619 (+)
1264    AND (orlo.date_shipped = orli.date_shipped
1265        OR (orlo.date_shipped IS NULL AND orli.date_shipped IS NULL))
1266    -- 6704619 (-)
1267    AND orlo.inventory_item_id = orli.inventory_item_id
1268    AND orlo.quantity = orli.quantity
1269    AND orlo.uom_code = orli.uom_code
1270    AND NVL(orlo.sold_from_cust_account_id,FND_API.G_MISS_NUM) = NVL(orli.sold_from_cust_account_id,FND_API.G_MISS_NUM)
1271    --AND orlo.ship_from_cust_account_id = orli.sold_from_cust_account_id
1272    AND NVL(orlo.ship_from_cust_account_id,FND_API.G_MISS_NUM) = NVL(orli.ship_from_cust_account_id,FND_API.G_MISS_NUM)
1273    AND orlo.claimed_amount = orli.claimed_amount
1274    AND NVL(orlo.bill_to_cust_account_id,FND_API.G_MISS_NUM) = NVL(orli.bill_to_cust_account_id,FND_API.G_MISS_NUM)
1275    AND orlo.status_code in (OZF_RESALE_COMMON_PVT.G_BATCH_ADJ_OPEN, OZF_RESALE_COMMON_PVT.G_BATCH_ADJ_PROCESSED)
1276    AND orlo.resale_line_int_id <> p_resale_line_int_id
1277    AND orlo.resale_batch_id <> orli.resale_batch_id
1278    AND orli.resale_line_int_id = p_resale_line_int_id
1279    AND orlo.duplicated_line_id IS NULL
1280    AND orlo.duplicated_adjustment_id IS NULL
1281    AND orlo.direct_customer_flag = 'T'
1282    AND orlo.creation_date <= orli.creation_date
1283    AND rownum = 1;
1284 
1285 CURSOR dup_line_indirect_iface_csr ( p_resale_line_int_id IN NUMBER )
1286 IS
1287 SELECT orlo.resale_line_int_id
1288   -- Bug 4670154 (+)
1289   FROM ozf_resale_lines_int_all orlo,
1290        ozf_resale_lines_int_all orli
1291   /*
1292   FROM ozf_resale_lines_int orlo,
1293        ozf_resale_lines_int orli
1294   */
1295   -- Bug 4670154 (-)
1296  WHERE orlo.order_number = orli.order_number
1297    AND orlo.date_ordered =  orli.date_ordered
1298    -- 6704619 (+)
1299    AND (orlo.date_shipped = orli.date_shipped
1300        OR (orlo.date_shipped IS NULL AND orli.date_shipped IS NULL))
1301    -- 6704619 (-)
1302    --AND orl.invoice_number = orli.invoice_number
1303    --AND orl.date_invoiced = orli.date_invoiced
1304    AND orlo.inventory_item_id = orli.inventory_item_id
1305    AND orlo.quantity = orli.quantity
1306    AND orlo.uom_code = orli.uom_code
1307    AND NVL(orlo.sold_from_cust_account_id,FND_API.G_MISS_NUM) = NVL(orli.sold_from_cust_account_id,FND_API.G_MISS_NUM)
1308    --AND orlo.ship_from_cust_account_id = orli.sold_from_cust_account_id
1309    AND NVL(orlo.ship_from_cust_account_id,FND_API.G_MISS_NUM) = NVL(orli.ship_from_cust_account_id,FND_API.G_MISS_NUM)
1310    AND orlo.claimed_amount = orli.claimed_amount
1311    AND NVL(orlo.bill_to_party_name,FND_API.G_MISS_NUM) = NVL(orli.bill_to_party_name,FND_API.G_MISS_NUM)
1312    AND orlo.status_code in (OZF_RESALE_COMMON_PVT.G_BATCH_ADJ_OPEN, OZF_RESALE_COMMON_PVT.G_BATCH_ADJ_PROCESSED)
1313    AND orlo.resale_line_int_id <> p_resale_line_int_id
1314    AND orlo.resale_batch_id <> orli.resale_batch_id
1315    AND orli.resale_line_int_id = p_resale_line_int_id
1316    AND orlo.duplicated_line_id IS NULL
1317    AND orlo.duplicated_adjustment_id IS NULL
1318    AND orlo.direct_customer_flag = 'F'
1319    AND orlo.creation_date <= orli.creation_date
1320    AND rownum = 1;
1321 
1322 CURSOR dup_line_nondirect_resale_csr ( p_resale_line_int_id IN NUMBER )
1323 IS
1324 SELECT orl.resale_line_id
1325   FROM ozf_resale_lines_all orl,
1326        ozf_resale_lines_int_all orli
1327   -- Bug 4670154 (+)
1328   /*
1329   FROM ozf_resale_lines orl,
1330        ozf_resale_lines_int orli
1331   */
1332   -- Bug 4670154 (-)
1333  WHERE orl.order_number = orli.order_number
1334    AND nvl (orl.order_line_number, 0) = nvl (orli.order_line_number, 0)   -- For Bug#9447673 SSD IDSM ER
1335    AND orl.date_ordered =  orli.date_ordered
1336    -- 6704619 (+)
1337    AND (orl.date_shipped = orli.date_shipped
1338        OR (orl.date_shipped IS NULL AND orli.date_shipped IS NULL))
1339    -- 6704619 (-)
1340    --AND orl.invoice_number = orli.invoice_number
1341    --AND orl.date_invoiced = orli.date_invoiced
1342    AND orl.inventory_item_id = orli.inventory_item_id
1343    AND orl.quantity = orli.quantity
1344    AND orl.uom_code = orli.uom_code
1345    AND NVL(orl.sold_from_cust_account_id,FND_API.G_MISS_NUM) = NVL(orli.sold_from_cust_account_id,FND_API.G_MISS_NUM)
1346    --AND orl.ship_from_cust_account_id = orli.sold_from_cust_account_id
1347    AND NVL(orl.ship_from_cust_account_id,FND_API.G_MISS_NUM) = NVL(orli.ship_from_cust_account_id,FND_API.G_MISS_NUM)
1348    AND orl.bill_to_cust_account_id is null
1349    AND orl.bill_to_party_name is null
1350    AND orl.direct_customer_flag is null
1351    AND orli.resale_line_int_id = p_resale_line_int_id
1352    AND rownum = 1;
1353 
1354 CURSOR dup_line_nondirect_iface_csr ( p_resale_line_int_id IN NUMBER )
1355 IS
1356 SELECT orlo.resale_line_int_id
1357   -- Bug 4670154 (+)
1358   FROM ozf_resale_lines_int_all orlo,
1359        ozf_resale_lines_int_all orli
1360   /*
1361   FROM ozf_resale_lines_int orlo,
1362        ozf_resale_lines_int orli
1363   */
1364   -- Bug 4670154 (-)
1365  WHERE orlo.order_number = orli.order_number
1366    AND nvl (orlo.order_line_number, 0) = nvl (orli.order_line_number, 0)  -- For Bug#9447673 SSD IDSM ER
1367    AND orlo.date_ordered =  orli.date_ordered
1368    -- 6704619 (+)
1369    AND (orlo.date_shipped = orli.date_shipped
1370        OR (orlo.date_shipped IS NULL AND orli.date_shipped IS NULL))
1371    -- 6704619 (-)
1372    AND orlo.inventory_item_id = orli.inventory_item_id
1373    AND orlo.quantity = orli.quantity
1374    AND orlo.uom_code = orli.uom_code
1375    AND NVL(orlo.sold_from_cust_account_id,FND_API.G_MISS_NUM) = NVL(orli.sold_from_cust_account_id,FND_API.G_MISS_NUM)
1376    --AND orlo.ship_from_cust_account_id = orli.sold_from_cust_account_id
1377    AND NVL(orlo.ship_from_cust_account_id,FND_API.G_MISS_NUM) = NVL(orli.ship_from_cust_account_id,FND_API.G_MISS_NUM)
1378    AND orlo.claimed_amount = orli.claimed_amount
1379    AND orlo.bill_to_cust_account_id is null
1380    AND orlo.bill_to_party_name is null
1381    AND orlo.direct_customer_flag is null
1382    AND orlo.status_code in (OZF_RESALE_COMMON_PVT.G_BATCH_ADJ_OPEN, OZF_RESALE_COMMON_PVT.G_BATCH_ADJ_PROCESSED)
1383    AND orlo.resale_line_int_id <> p_resale_line_int_id
1384    AND orlo.resale_batch_id <> orli.resale_batch_id
1385    AND orli.resale_line_int_id = p_resale_line_int_id
1386    AND orlo.duplicated_line_id IS NULL
1387    AND orlo.duplicated_adjustment_id IS NULL
1388    AND orlo.creation_date <= orli.creation_date
1389    AND rownum = 1;
1390 ---
1391 
1392 CURSOR dup_line_dir_iface_self_csr ( p_resale_line_int_id IN NUMBER )
1393 IS
1394 SELECT orlo.resale_line_int_id
1395   FROM ozf_resale_lines_int_all orlo,
1396        ozf_resale_lines_int_all orli
1397  WHERE orlo.order_number = orli.order_number
1398    AND nvl (orlo.order_line_number, 0) = nvl (orli.order_line_number, 0) -- For Bug#9447673 SSD IDSM ER
1399    AND orlo.date_ordered =  orli.date_ordered
1400    -- 6704619 (+)
1401    AND (orlo.date_shipped = orli.date_shipped
1402        OR (orlo.date_shipped IS NULL AND orli.date_shipped IS NULL))
1403    -- 6704619 (-)
1404    AND orlo.inventory_item_id = orli.inventory_item_id
1405    AND orlo.quantity = orli.quantity
1406    AND orlo.uom_code = orli.uom_code
1407    AND NVL(orlo.sold_from_cust_account_id,FND_API.G_MISS_NUM) = NVL(orli.sold_from_cust_account_id,FND_API.G_MISS_NUM)
1408    AND NVL(orlo.ship_from_cust_account_id,FND_API.G_MISS_NUM) = NVL(orli.ship_from_cust_account_id,FND_API.G_MISS_NUM)
1409    --AND orlo.claimed_amount = orli.claimed_amount
1410    AND NVL(orlo.bill_to_cust_account_id,FND_API.G_MISS_NUM) = NVL(orli.bill_to_cust_account_id,FND_API.G_MISS_NUM)
1411    AND orlo.status_code IN ('OPEN', 'PROCESSED')
1412    AND orlo.resale_line_int_id <> p_resale_line_int_id
1413    AND orlo.resale_batch_id = orli.resale_batch_id
1414    AND orli.resale_line_int_id = p_resale_line_int_id
1415    AND orlo.duplicated_line_id IS NULL
1416    AND orlo.duplicated_adjustment_id IS NULL
1417    AND orlo.direct_customer_flag = 'T'
1418    AND orlo.creation_date <= orli.creation_date
1419    AND rownum = 1;
1420 
1421 CURSOR dup_line_indir_iface_self_csr ( p_resale_line_int_id IN NUMBER )
1422 IS
1423 SELECT orlo.resale_line_int_id
1424   FROM ozf_resale_lines_int_all orlo,
1425        ozf_resale_lines_int_all orli
1426  WHERE orlo.order_number = orli.order_number
1427    AND orlo.date_ordered =  orli.date_ordered
1428    AND nvl (orlo.order_line_number, 0) = nvl (orli.order_line_number, 0)   -- For Bug#9447673 SSD IDSM ER
1429    -- 6704619 (+)
1430    AND (orlo.date_shipped = orli.date_shipped
1431        OR (orlo.date_shipped IS NULL AND orli.date_shipped IS NULL))
1432    -- 6704619 (-)
1433    AND orlo.inventory_item_id = orli.inventory_item_id
1434    AND orlo.quantity = orli.quantity
1435    AND orlo.uom_code = orli.uom_code
1436    AND NVL(orlo.sold_from_cust_account_id,FND_API.G_MISS_NUM) = NVL(orli.sold_from_cust_account_id,FND_API.G_MISS_NUM)
1437    AND NVL(orlo.ship_from_cust_account_id,FND_API.G_MISS_NUM) = NVL(orli.ship_from_cust_account_id,FND_API.G_MISS_NUM)
1438    --AND orlo.claimed_amount = orli.claimed_amount
1439    AND orlo.bill_to_party_name = orli.bill_to_party_name
1440 
1441    AND orlo.status_code IN ('OPEN', 'PROCESSED')
1442    AND orlo.resale_line_int_id <> p_resale_line_int_id
1443    AND orlo.resale_batch_id = orli.resale_batch_id
1444    AND orli.resale_line_int_id = p_resale_line_int_id
1445    AND orlo.duplicated_line_id IS NULL
1446    AND orlo.duplicated_adjustment_id IS NULL
1447    AND orlo.direct_customer_flag = 'F'
1448    AND orlo.creation_date <= orli.creation_date
1449    AND rownum = 1;
1450 
1451 CURSOR dup_line_nondir_iface_self_csr ( p_resale_line_int_id IN NUMBER )
1452 IS
1453 SELECT orlo.resale_line_int_id
1454   FROM ozf_resale_lines_int_all orlo,
1455        ozf_resale_lines_int_all orli
1456  WHERE orlo.order_number = orli.order_number
1457    AND nvl (orlo.order_line_number, 0) = nvl (orli.order_line_number, 0)  -- For Bug#9447673 SSD IDSM ER
1458    AND orlo.date_ordered =  orli.date_ordered
1459    -- 6704619 (+)
1460    AND (orlo.date_shipped = orli.date_shipped
1461        OR (orlo.date_shipped IS NULL AND orli.date_shipped IS NULL))
1462    -- 6704619 (-)
1463    AND orlo.inventory_item_id = orli.inventory_item_id
1464    AND orlo.quantity = orli.quantity
1465    AND orlo.uom_code = orli.uom_code
1466    AND NVL(orlo.sold_from_cust_account_id,FND_API.G_MISS_NUM) = NVL(orli.sold_from_cust_account_id,FND_API.G_MISS_NUM)
1467    AND NVL(orlo.ship_from_cust_account_id,FND_API.G_MISS_NUM) = NVL(orli.ship_from_cust_account_id,FND_API.G_MISS_NUM)
1468    --AND orlo.claimed_amount = orli.claimed_amount
1469    AND orlo.bill_to_cust_account_id is null
1470    AND orlo.bill_to_party_name is null
1471    AND orlo.direct_customer_flag is null
1472    AND orlo.status_code IN ('OPEN', 'PROCESSED')
1473    AND orlo.resale_line_int_id <> p_resale_line_int_id
1474    AND orlo.resale_batch_id = orli.resale_batch_id
1475    AND orli.resale_line_int_id = p_resale_line_int_id
1476    AND orlo.duplicated_line_id IS NULL
1477    AND orlo.duplicated_adjustment_id IS NULL
1478    AND orlo.creation_date <= orli.creation_date
1479    AND rownum = 1;
1480 
1481 -- Start RSATYAVA Bug#10085365   fix
1482 
1483 l_dup_resl_csr_stmt VARCHAR2(4000) := ' SELECT orl.resale_line_id
1484                                            FROM ozf_resale_lines_all orl,
1485                                            ozf_resale_lines_int_all orli ';
1486 
1487 l_dup_resl_exclus_whr_cls_stmt  VARCHAR2(4000) := ' WHERE orli.resale_line_int_id = :resale_line_int_id ';
1488 
1489 -- For Bug#9447673 SSD IDSM ER(+)
1490 l_dup_resl_cmn_whr_cls_stmt VARCHAR2(4000) := ' AND orl.order_number = orli.order_number
1491                                            AND nvl (orl.order_line_number, 0) = nvl (orli.order_line_number, 0)
1492                                            AND orl.date_ordered =  orli.date_ordered
1493                                            AND orl.inventory_item_id = orli.inventory_item_id
1494                                            AND orl.quantity = orli.quantity
1495                                            AND orl.uom_code = orli.uom_code
1496                                            AND NVL(orl.sold_from_cust_account_id, '||FND_API.G_MISS_NUM||' ) = NVL(orli.sold_from_cust_account_id, '||FND_API.G_MISS_NUM||' )
1497                                            AND NVL(orl.ship_from_cust_account_id, '||FND_API.G_MISS_NUM||' ) = NVL(orli.ship_from_cust_account_id, '||FND_API.G_MISS_NUM||' )  ';
1498 -- For Bug#9447673 SSD IDSM ER(-)
1499 
1500 l_dup_resl_dir_whr_cls_stmt VARCHAR2(4000) := ' AND orl.direct_customer_flag = ''T''   AND  NVL(orl.bill_to_cust_account_id,'||FND_API.G_MISS_NUM||' ) = NVL(orli.bill_to_cust_account_id, '||FND_API.G_MISS_NUM||' )   AND rownum = 1 ' ;
1501 
1502 l_dup_resl_ind_whr_cls_stmt VARCHAR2(4000) :=  ' AND orl.direct_customer_flag = ''F''
1503                                                  AND NVL(orl.bill_to_party_name, '||FND_API.G_MISS_NUM||' ) = NVL(orli.bill_to_party_name, '||FND_API.G_MISS_NUM||' )
1504                                                  AND rownum = 1 ' ;
1505 
1506 l_dup_resl_nondir_whr_cls_stmt VARCHAR2(4000) :=  ' AND orl.bill_to_cust_account_id is null
1507                                                  AND orl.bill_to_party_name is null
1508                                                  AND orl.direct_customer_flag is null
1509                                                   AND rownum = 1 ' ;
1510 
1511 
1512 l_dup_iface_csr_stmt VARCHAR2(4000) :=' SELECT orlo.resale_line_int_id
1513                                                 FROM ozf_resale_lines_int_all orlo,
1514                                                        ozf_resale_lines_int_all orli ';
1515 
1516 l_dup_iface_exc_wr_cls_stmt  VARCHAR2(4000) := ' WHERE orlo.resale_line_int_id <> :resale_line_int_id
1517                                                     AND orli.resale_line_int_id = :resale_line_int_id';
1518 
1519 -- For Bug#9447673 SSD IDSM ER(+)
1520 l_dup_iface_cmn_whr_cls_stmt VARCHAR2(4000) := ' AND orlo.order_number = orli.order_number
1521                                                 AND nvl (orlo.order_line_number, 0) = nvl (orli.order_line_number, 0)
1522                                                 AND orlo.date_ordered =  orli.date_ordered
1523                                                 AND orlo.inventory_item_id = orli.inventory_item_id
1524                                                 AND orlo.quantity = orli.quantity
1525                                                 AND orlo.uom_code = orli.uom_code
1526                                                 AND NVL(orlo.sold_from_cust_account_id, '||FND_API.G_MISS_NUM||' ) = NVL(orli.sold_from_cust_account_id, '||FND_API.G_MISS_NUM||' )
1527                                                 AND NVL(orlo.ship_from_cust_account_id, '||FND_API.G_MISS_NUM||' ) = NVL(orli.ship_from_cust_account_id, '||FND_API.G_MISS_NUM||' )
1528                                                 AND orlo.claimed_amount = orli.claimed_amount
1529                                                 AND orlo.status_code in ( '''||OZF_RESALE_COMMON_PVT.G_BATCH_ADJ_OPEN||''', '''||OZF_RESALE_COMMON_PVT.G_BATCH_ADJ_PROCESSED||'''  )
1530                                                 AND orlo.duplicated_line_id IS NULL
1531                                                 AND orlo.duplicated_adjustment_id IS NULL
1532                                                 AND orlo.creation_date <= orli.creation_date  ' ;
1533 -- For Bug#9447673 SSD IDSM ER(-)
1534 
1535 l_dup_iface_whr_cls_stmt VARCHAR2(4000) := ' AND orlo.resale_batch_id <> orli.resale_batch_id  ';
1536 
1537 
1538 l_dup_iface_dir_whr_cls_stmt VARCHAR2(4000) := ' AND orlo.direct_customer_flag = ''T'' AND NVL(orlo.bill_to_cust_account_id, '||FND_API.G_MISS_NUM||' )  = NVL(orli.bill_to_cust_account_id, '||FND_API.G_MISS_NUM||' )  AND rownum = 1 ' ;
1539 
1540 
1541 l_dup_iface_ind_whr_cls_stmt VARCHAR2(4000) := ' AND orlo.direct_customer_flag = ''F''   AND  NVL(orlo.bill_to_party_name, '||FND_API.G_MISS_NUM||' ) = NVL(orli.bill_to_party_name,'||FND_API.G_MISS_NUM||' ) AND rownum = 1  ' ;
1542 
1543 l_dup_iface_nondir_whr_cls VARCHAR2(4000) := ' AND orlo.bill_to_cust_account_id is null
1544                                                  AND orlo.bill_to_party_name is null
1545                                                  AND orlo.direct_customer_flag is null
1546                                                  AND rownum = 1  ' ;
1547 
1548 
1549 l_dup_iface_self_whr_cls_stmt VARCHAR2(4000) := '  AND orlo.resale_batch_id = orli.resale_batch_id ' ;
1550 
1551 l_resl_addl_whr_cls VARCHAR2(2000);
1552 l_resl_iface_addl_whr_cls VARCHAR2(2000)  ;
1553 l_dup_csr dup_cursor;
1554 
1555 
1556 -- Start RSATYAVA Bug#10085365   fix
1557 
1558 BEGIN
1559 
1560    -- Standard call to check for call compatibility.
1561    IF NOT FND_API.Compatible_API_Call (
1562       l_api_version,
1563       p_api_version_number,
1564       l_api_name,
1565       G_PKG_NAME)
1566    THEN
1567       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
1568    END IF;
1569 
1570    --Initialize message LIST IF p_init_msg_LIST IS TRUE.
1571    IF FND_API.To_Boolean (p_init_msg_LIST) THEN
1572       FND_MSG_PUB.initialize;
1573    END IF;
1574 
1575    -- Debug Message
1576    IF OZF_DEBUG_HIGH_ON THEN
1577       OZF_UTILITY_PVT.debug_message(l_full_name||': Start');
1578    END IF;
1579 
1580    -- Initialize API return status to sucess
1581    x_return_status := FND_API.G_RET_STS_SUCCESS;
1582    IF OZF_DEBUG_LOW_ON THEN
1583       OZF_UTILITY_PVT.debug_message('resale_line_int_id'||p_resale_line_int_id);
1584       OZF_UTILITY_PVT.debug_message('direct_customer_flag'||p_direct_customer_flag);
1585       OZF_UTILITY_PVT.debug_message('claimed_amount'||p_claimed_amount);
1586    END IF;
1587 
1588 IF (l_addl_attrb_dup_check_prfl IS NULL ) THEN
1589    -- Check with resale data first
1590    IF p_direct_customer_flag IS NULL THEN
1591       OPEN dup_line_nondirect_resale_csr(p_resale_line_int_id);
1592       FETCH dup_line_nondirect_resale_csr INTO l_line_id;
1593       CLOSE dup_line_nondirect_resale_csr;
1594    ELSE
1595       IF p_direct_customer_flag = 'T' THEN
1596          OPEN dup_line_direct_resale_csr(p_resale_line_int_id);
1597          FETCH dup_line_direct_resale_csr INTO l_line_id;
1598          CLOSE dup_line_direct_resale_csr;
1599       ELSE
1600          OPEN dup_line_indirect_resale_csr(p_resale_line_int_id);
1601          FETCH dup_line_indirect_resale_csr INTO l_line_id;
1602          CLOSE dup_line_indirect_resale_csr;
1603       END IF;
1604    END IF;
1605 
1606    x_reprocessing := false;
1607    IF l_line_id IS NOT NULL THEN
1608       --Bug# 8414563 fixed by ateotia(+)
1609      --AMITAMKU, Fix for bug 14685303  - Added Condition for TP_ACCRUAL Batch.
1610       IF (p_batch_type = G_TRACING OR p_batch_type = OZF_RESALE_COMMON_PVT.G_TP_ACCRUAL) THEN
1611          x_reprocessing := true;
1612          -- -2 to indicate that it's a tracing batch and current line is duplicate of a resale line
1613          l_adjustment_id := -2;
1614       ELSE
1615          --Check for tracing line
1616          OPEN g_tracing_flag_csr (p_resale_line_int_id);
1617          FETCH g_tracing_flag_csr INTO l_tracing_flag;
1618          CLOSE g_tracing_flag_csr;
1619          IF (NVL(l_tracing_flag, 'F') = 'T') THEN
1620             x_reprocessing := true;
1621             -- -3 to indicate that current line is a tracing line and is duplicate of a resale line
1622             l_adjustment_id := -3;
1623          END IF;
1624       END IF;
1625       IF (NOT x_reprocessing) THEN
1626          OPEN dup_adj_csr(l_line_id, p_batch_type);
1627          FETCH dup_adj_csr INTO l_adjustment_id, l_claimed_amount;
1628          CLOSE dup_adj_csr;
1629 
1630          x_dup_adjustment_id := l_adjustment_id;
1631          x_reprocessing := l_adjustment_id IS NOT NULL AND
1632                            p_claimed_amount = l_claimed_amount;
1633       END IF;
1634       --Bug# 8414563 fixed by ateotia(-)
1635       IF OZF_DEBUG_LOW_ON THEN
1636          OZF_UTILITY_PVT.debug_message('l_adjustment_id = '||l_adjustment_id);
1637          OZF_UTILITY_PVT.debug_message('x_dup_adjustment_id = '||x_dup_adjustment_id);
1638          IF x_reprocessing THEN
1639             OZF_UTILITY_PVT.debug_message('x_reprocessing >> Yes');
1640          ELSE
1641             OZF_UTILITY_PVT.debug_message('x_reprocessing >> No');
1642          END IF;
1643       END IF;
1644 
1645    ELSE
1646       -- check to see whether it can be a duplicate of an interface line.
1647       -- In this case, reprocess is always false.
1648       IF p_direct_customer_flag IS NULL THEN
1649          OPEN dup_line_nondirect_iface_csr(p_resale_line_int_id);
1650          FETCH dup_line_nondirect_iface_csr INTO l_line_id;
1651          CLOSE dup_line_nondirect_iface_csr;
1652       ELSE
1653          IF p_direct_customer_flag = 'T' THEN
1654             OPEN dup_line_direct_iface_csr(p_resale_line_int_id);
1655             FETCH dup_line_direct_iface_csr INTO l_line_id;
1656             CLOSE dup_line_direct_iface_csr;
1657          ELSE
1658             OPEN dup_line_indirect_iface_csr(p_resale_line_int_id);
1659             FETCH dup_line_indirect_iface_csr INTO l_line_id;
1660             CLOSE dup_line_indirect_iface_csr;
1661          END IF;
1662       END IF;
1663       -- -1 to indicate it's a duplication to an interface line.
1664       IF l_line_id is NOT NULL THEN
1665          l_adjustment_id := -1;
1666          --x_reprocessing := true;
1667          x_reprocessing := false;
1668       END IF;
1669    END IF;
1670 
1671    IF l_line_id IS NULL THEN
1672       IF p_direct_customer_flag IS NULL THEN
1673          OPEN dup_line_nondir_iface_self_csr(p_resale_line_int_id);
1674          FETCH dup_line_nondir_iface_self_csr INTO l_line_id;
1675          CLOSE dup_line_nondir_iface_self_csr;
1676       ELSE
1677          IF p_direct_customer_flag = 'T' THEN
1678             OPEN dup_line_dir_iface_self_csr(p_resale_line_int_id);
1679             FETCH dup_line_dir_iface_self_csr INTO l_line_id;
1680             CLOSE dup_line_dir_iface_self_csr;
1681          ELSE
1682             OPEN dup_line_indir_iface_self_csr(p_resale_line_int_id);
1683             FETCH dup_line_indir_iface_self_csr INTO l_line_id;
1684             CLOSE dup_line_indir_iface_self_csr;
1685          END IF;
1686       END IF;
1687       -- -1 to indicate it's a duplication to an interface line within the same batch
1688       IF l_line_id IS NOT NULL THEN
1689          l_adjustment_id := -1;
1690          x_reprocessing := true;
1691       END IF;
1692    END IF;
1693 
1694 
1695    IF OZF_DEBUG_LOW_ON THEN
1696       OZF_UTILITY_PVT.debug_message('dup adj id' || l_adjustment_id);
1697       OZF_UTILITY_PVT.debug_message('dup line id' || l_line_id);
1698    END IF;
1699 
1700    x_dup_adjustment_id := l_adjustment_id;
1701    x_dup_line_id := l_line_id;
1702 ELSE  --if l_addl_attrb_dup_check_prfl  IS NOT NULL
1703 l_resl_addl_whr_cls := ' AND  NVL(orl.' ||l_addl_attrb_dup_check_prfl||' , '||FND_API.G_MISS_NUM||' ) =  NVL(orli.'||l_addl_attrb_dup_check_prfl||' , '|| FND_API.G_MISS_NUM||' )';
1704 l_resl_iface_addl_whr_cls := ' AND  NVL(orlo.' ||l_addl_attrb_dup_check_prfl||' , '||FND_API.G_MISS_NUM||' ) =  NVL(orli.'||l_addl_attrb_dup_check_prfl||' ,'|| FND_API.G_MISS_NUM||' )';
1705 
1706 
1707         IF(l_dup_chk_attrb_unique_prfl = 'Y') THEN
1708             -- Check with resale data first
1709                    IF p_direct_customer_flag IS NULL THEN
1710                       OPEN l_dup_csr FOR l_dup_resl_csr_stmt||l_dup_resl_exclus_whr_cls_stmt||l_dup_resl_nondir_whr_cls_stmt||l_resl_addl_whr_cls  using p_resale_line_int_id;
1711                       FETCH l_dup_csr INTO l_line_id;
1712                       CLOSE l_dup_csr;
1713                    ELSE
1714                       IF p_direct_customer_flag = 'T' THEN
1715                          OPEN  l_dup_csr FOR l_dup_resl_csr_stmt||l_dup_resl_exclus_whr_cls_stmt||l_dup_resl_dir_whr_cls_stmt||l_resl_addl_whr_cls using p_resale_line_int_id;
1716                          FETCH l_dup_csr INTO l_line_id;
1717                          CLOSE l_dup_csr;
1718                       ELSE
1719                          OPEN l_dup_csr FOR l_dup_resl_csr_stmt||l_dup_resl_exclus_whr_cls_stmt||l_dup_resl_ind_whr_cls_stmt||l_resl_addl_whr_cls using p_resale_line_int_id;
1720                          FETCH l_dup_csr INTO l_line_id;
1721                          CLOSE l_dup_csr;
1722                       END IF;
1723                    END IF;
1724 
1725                    x_reprocessing := false;
1726                    IF l_line_id IS NOT NULL THEN
1727                    --Bug# 8414563 fixed by ateotia(+)
1728                       --AMITAMKU, Fix for bug 14685303  - Added Condition for TP_ACCRUAL Batch.
1729                       IF (p_batch_type = G_TRACING OR p_batch_type = OZF_RESALE_COMMON_PVT.G_TP_ACCRUAL) THEN
1730                          x_reprocessing := true;
1731                          -- -2 to indicate that it's a tracing batch and current line is duplicate of a resale line
1732                          l_adjustment_id := -2;
1733                       ELSE
1734                          --Check for tracing line
1735                          OPEN g_tracing_flag_csr (p_resale_line_int_id);
1736                          FETCH g_tracing_flag_csr INTO l_tracing_flag;
1737                          CLOSE g_tracing_flag_csr;
1738                          IF (NVL(l_tracing_flag, 'F') = 'T') THEN
1739                             x_reprocessing := true;
1740                             -- -3 to indicate that current line is a tracing line and is duplicate of a resale line
1741                             l_adjustment_id := -3;
1742                          END IF;
1743                       END IF;
1744                       IF (NOT x_reprocessing) THEN
1745                          OPEN dup_adj_csr(l_line_id, p_batch_type);
1746                          FETCH dup_adj_csr INTO l_adjustment_id, l_claimed_amount;
1747                          CLOSE dup_adj_csr;
1748 
1749                          x_dup_adjustment_id := l_adjustment_id;
1750                          x_reprocessing := l_adjustment_id IS NOT NULL AND
1751                                            p_claimed_amount = l_claimed_amount;
1752                       END IF;
1753                       --Bug# 8414563 fixed by ateotia(-)
1754 
1755                       IF OZF_DEBUG_LOW_ON THEN
1756                          OZF_UTILITY_PVT.debug_message('l_adjustment_id = '||l_adjustment_id);
1757                          OZF_UTILITY_PVT.debug_message('x_dup_adjustment_id = '||x_dup_adjustment_id);
1758                          IF x_reprocessing THEN
1759                             OZF_UTILITY_PVT.debug_message('x_reprocessing >> Yes');
1760                          ELSE
1761                             OZF_UTILITY_PVT.debug_message('x_reprocessing >> No');
1762                          END IF;
1763                       END IF;
1764                   ELSE
1765                       -- check to see whether it can be a duplicate of an interface line.
1766                       -- In this case, reprocess is always false.
1767 
1768 
1769                       IF p_direct_customer_flag IS NULL THEN
1770                          OPEN l_dup_csr for l_dup_iface_csr_stmt||l_dup_resl_exclus_whr_cls_stmt||l_dup_iface_whr_cls_stmt||l_dup_iface_nondir_whr_cls||l_resl_iface_addl_whr_cls using p_resale_line_int_id;
1771                          FETCH l_dup_csr INTO l_line_id;
1772                          CLOSE l_dup_csr;
1773                       ELSE
1774                          IF p_direct_customer_flag = 'T' THEN
1775                             OPEN l_dup_csr for l_dup_iface_csr_stmt||l_dup_resl_exclus_whr_cls_stmt||l_dup_iface_whr_cls_stmt||l_dup_iface_dir_whr_cls_stmt||l_resl_iface_addl_whr_cls using p_resale_line_int_id;
1776                             FETCH l_dup_csr INTO l_line_id;
1777                             CLOSE l_dup_csr;
1778                          ELSE
1779                             OPEN l_dup_csr for l_dup_iface_csr_stmt||l_dup_resl_exclus_whr_cls_stmt||l_dup_iface_whr_cls_stmt||l_dup_iface_ind_whr_cls_stmt||l_resl_iface_addl_whr_cls using p_resale_line_int_id;
1780                             FETCH l_dup_csr INTO l_line_id;
1781                             CLOSE l_dup_csr;
1782                          END IF;
1783                       END IF;
1784                       -- -1 to indicate it's a duplication to an interface line.
1785                       IF l_line_id is NOT NULL THEN
1786                          l_adjustment_id := -1;
1787                          x_reprocessing := true;
1788                          --x_reprocessing := false;
1789                       END IF;
1790                    END IF;
1791 
1792 
1793                     IF l_line_id IS NULL THEN
1794                       IF p_direct_customer_flag IS NULL THEN
1795                          OPEN l_dup_csr for l_dup_iface_csr_stmt||l_dup_iface_exc_wr_cls_stmt||l_dup_iface_self_whr_cls_stmt||l_dup_iface_nondir_whr_cls||l_resl_iface_addl_whr_cls using p_resale_line_int_id,p_resale_line_int_id;
1796                          FETCH l_dup_csr INTO l_line_id;
1797                          CLOSE l_dup_csr;
1798                       ELSE
1799                          IF p_direct_customer_flag = 'T' THEN
1800                             OPEN l_dup_csr for l_dup_iface_csr_stmt||l_dup_iface_exc_wr_cls_stmt||l_dup_iface_self_whr_cls_stmt||l_dup_iface_dir_whr_cls_stmt||l_resl_iface_addl_whr_cls using p_resale_line_int_id,p_resale_line_int_id;
1801                             FETCH l_dup_csr INTO l_line_id;
1802                             CLOSE l_dup_csr;
1803                          ELSE
1804                             OPEN l_dup_csr for l_dup_iface_csr_stmt||l_dup_iface_exc_wr_cls_stmt||l_dup_iface_self_whr_cls_stmt||l_dup_iface_ind_whr_cls_stmt||l_resl_iface_addl_whr_cls using p_resale_line_int_id,p_resale_line_int_id;
1805                             FETCH l_dup_csr INTO l_line_id;
1806                             CLOSE l_dup_csr;
1807                          END IF;
1808                       END IF;
1809 
1810                       -- -1 to indicate it's a duplication to an interface line within the same batch
1811                       IF l_line_id IS NOT NULL THEN
1812                          l_adjustment_id := -1;
1813                          x_reprocessing := true;
1814                       END IF;
1815                     END IF;
1816                     IF OZF_DEBUG_LOW_ON THEN
1817                               OZF_UTILITY_PVT.debug_message('dup adj id' || l_adjustment_id);
1818                               OZF_UTILITY_PVT.debug_message('dup line id' || l_line_id);
1819                     END IF;
1820 
1821                            x_dup_adjustment_id := l_adjustment_id;
1822                            x_dup_line_id := l_line_id;
1823 
1824 
1825                     ELSE
1826                      -- l_dup_chk_attrb_unique_prfl <> 'Y'
1827             -- Check with resale data first
1828                    IF p_direct_customer_flag IS NULL THEN
1829                       OPEN l_dup_csr FOR l_dup_resl_csr_stmt||l_dup_resl_exclus_whr_cls_stmt||l_dup_resl_cmn_whr_cls_stmt||l_dup_resl_nondir_whr_cls_stmt||l_resl_addl_whr_cls  using p_resale_line_int_id;
1830                       FETCH l_dup_csr INTO l_line_id;
1831                       CLOSE l_dup_csr;
1832                    ELSE
1833                       IF p_direct_customer_flag = 'T' THEN
1834                          OPEN  l_dup_csr FOR l_dup_resl_csr_stmt||l_dup_resl_exclus_whr_cls_stmt||l_dup_resl_cmn_whr_cls_stmt||l_dup_resl_dir_whr_cls_stmt||l_resl_addl_whr_cls using p_resale_line_int_id;
1835                          FETCH l_dup_csr INTO l_line_id;
1836                          CLOSE l_dup_csr;
1837                       ELSE
1838                          OPEN l_dup_csr FOR l_dup_resl_csr_stmt||l_dup_resl_exclus_whr_cls_stmt||l_dup_resl_cmn_whr_cls_stmt||l_dup_resl_ind_whr_cls_stmt||l_resl_addl_whr_cls using p_resale_line_int_id;
1839                          FETCH l_dup_csr INTO l_line_id;
1840                          CLOSE l_dup_csr;
1841                       END IF;
1842                    END IF;
1843 
1844                    x_reprocessing := false;
1845                    IF l_line_id IS NOT NULL THEN
1846                    --Bug# 8414563 fixed by ateotia(+)
1847                       --AMITAMKU, Fix for bug 14685303  - Added Condition for TP_ACCRUAL Batch.
1848                       IF (p_batch_type = G_TRACING OR p_batch_type = OZF_RESALE_COMMON_PVT.G_TP_ACCRUAL) THEN
1849                          x_reprocessing := true;
1850                          -- -2 to indicate that it's a tracing batch and current line is duplicate of a resale line
1851                          l_adjustment_id := -2;
1852                       ELSE
1853                          --Check for tracing line
1854                          OPEN g_tracing_flag_csr (p_resale_line_int_id);
1855                          FETCH g_tracing_flag_csr INTO l_tracing_flag;
1856                          CLOSE g_tracing_flag_csr;
1857                          IF (NVL(l_tracing_flag, 'F') = 'T') THEN
1858                             x_reprocessing := true;
1859                             -- -3 to indicate that current line is a tracing line and is duplicate of a resale line
1860                             l_adjustment_id := -3;
1861                          END IF;
1862                       END IF;
1863                       IF (NOT x_reprocessing) THEN
1864                          OPEN dup_adj_csr(l_line_id, p_batch_type);
1865                          FETCH dup_adj_csr INTO l_adjustment_id, l_claimed_amount;
1866                          CLOSE dup_adj_csr;
1867 
1868                          x_dup_adjustment_id := l_adjustment_id;
1869                          x_reprocessing := l_adjustment_id IS NOT NULL AND
1870                                            p_claimed_amount = l_claimed_amount;
1871                       END IF;
1872                       --Bug# 8414563 fixed by ateotia(-)
1873 
1874                       IF OZF_DEBUG_LOW_ON THEN
1875                          OZF_UTILITY_PVT.debug_message('l_adjustment_id = '||l_adjustment_id);
1876                          OZF_UTILITY_PVT.debug_message('x_dup_adjustment_id = '||x_dup_adjustment_id);
1877                          IF x_reprocessing THEN
1878                             OZF_UTILITY_PVT.debug_message('x_reprocessing >> Yes');
1879                          ELSE
1880                             OZF_UTILITY_PVT.debug_message('x_reprocessing >> No');
1881                          END IF;
1882                       END IF;
1883 
1884                    ELSE
1885                       -- check to see whether it can be a duplicate of an interface line.
1886                       -- In this case, reprocess is always false.
1887                       IF p_direct_customer_flag IS NULL THEN
1888                          OPEN l_dup_csr for l_dup_iface_csr_stmt||l_dup_resl_exclus_whr_cls_stmt||l_dup_iface_cmn_whr_cls_stmt||l_dup_iface_whr_cls_stmt||l_dup_iface_nondir_whr_cls||l_resl_iface_addl_whr_cls using p_resale_line_int_id;
1889                          FETCH l_dup_csr INTO l_line_id;
1890                          CLOSE l_dup_csr;
1891                       ELSE
1892                          IF p_direct_customer_flag = 'T' THEN
1893                             OPEN l_dup_csr for l_dup_iface_csr_stmt||l_dup_resl_exclus_whr_cls_stmt||l_dup_iface_cmn_whr_cls_stmt||l_dup_iface_whr_cls_stmt||l_dup_iface_dir_whr_cls_stmt||l_resl_iface_addl_whr_cls
1894 			    using p_resale_line_int_id;
1895                             FETCH l_dup_csr INTO l_line_id;
1896                             CLOSE l_dup_csr;
1897                          ELSE
1898                             OPEN l_dup_csr for l_dup_iface_csr_stmt||l_dup_resl_exclus_whr_cls_stmt||l_dup_iface_cmn_whr_cls_stmt||l_dup_iface_whr_cls_stmt||l_dup_iface_ind_whr_cls_stmt||l_resl_iface_addl_whr_cls
1899 			    using p_resale_line_int_id;
1900                             FETCH l_dup_csr INTO l_line_id;
1901                             CLOSE l_dup_csr;
1902                          END IF;
1903                       END IF;
1904                       -- -1 to indicate it's a duplication to an interface line.
1905                       IF l_line_id is NOT NULL THEN
1906                          l_adjustment_id := -1;
1907                          x_reprocessing := true;
1908                          --x_reprocessing := false;
1909                       END IF;
1910                    END IF;
1911                    IF l_line_id IS NULL THEN
1912                       IF p_direct_customer_flag IS NULL THEN
1913                          OPEN l_dup_csr for l_dup_iface_csr_stmt||l_dup_iface_exc_wr_cls_stmt||l_dup_iface_cmn_whr_cls_stmt||l_dup_iface_self_whr_cls_stmt||l_dup_iface_nondir_whr_cls||l_resl_iface_addl_whr_cls
1914 			 using p_resale_line_int_id,p_resale_line_int_id;
1915                          FETCH l_dup_csr INTO l_line_id;
1916                          CLOSE l_dup_csr;
1917                       ELSE
1918                          IF p_direct_customer_flag = 'T' THEN
1919                             OPEN l_dup_csr for l_dup_iface_csr_stmt||l_dup_iface_exc_wr_cls_stmt||l_dup_iface_cmn_whr_cls_stmt||l_dup_iface_self_whr_cls_stmt||l_dup_iface_dir_whr_cls_stmt||l_resl_iface_addl_whr_cls
1920 			    using p_resale_line_int_id,p_resale_line_int_id;
1921                             FETCH l_dup_csr INTO l_line_id;
1922                             CLOSE l_dup_csr;
1923                          ELSE
1924                             OPEN l_dup_csr for l_dup_iface_csr_stmt||l_dup_iface_exc_wr_cls_stmt||l_dup_iface_cmn_whr_cls_stmt||l_dup_iface_self_whr_cls_stmt||l_dup_iface_ind_whr_cls_stmt||l_resl_iface_addl_whr_cls
1925 			    using p_resale_line_int_id,p_resale_line_int_id;
1926                             FETCH l_dup_csr INTO l_line_id;
1927                             CLOSE l_dup_csr;
1928                          END IF;
1929                       END IF;
1930                       -- -1 to indicate it's a duplication to an interface line within the same batch
1931                       IF l_line_id IS NOT NULL THEN
1932                          l_adjustment_id := -1;
1933                          x_reprocessing := true;
1934                       END IF;
1935                     END IF;
1936                     IF OZF_DEBUG_LOW_ON THEN
1937                               OZF_UTILITY_PVT.debug_message('dup adj id' || l_adjustment_id);
1938                               OZF_UTILITY_PVT.debug_message('dup line id' || l_line_id);
1939                     END IF;
1940 
1941                            x_dup_adjustment_id := l_adjustment_id;
1942                            x_dup_line_id := l_line_id;
1943 
1944         END IF;
1945   END IF;
1946 
1947    -- Debug Message
1948    IF OZF_DEBUG_HIGH_ON THEN
1949       OZF_UTILITY_PVT.debug_message(l_full_name||': End');
1950    END IF;
1951 
1952    --Standard call to get message count AND IF count=1, get the message
1953    FND_MSG_PUB.Count_And_Get (
1954       p_encoded => FND_API.G_FALSE,
1955       p_count => x_msg_count,
1956       p_data  => x_msg_data
1957    );
1958 EXCEPTION
1959    WHEN FND_API.G_EXC_ERROR THEN
1960       x_return_status := FND_API.G_RET_STS_ERROR;
1961       -- Standard call to get message count AND IF count=1, get the message
1962       FND_MSG_PUB.Count_And_Get (
1963          p_encoded => FND_API.G_FALSE,
1964          p_count => x_msg_count,
1965          p_data  => x_msg_data
1966       );
1967    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
1968       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1969       -- Standard call to get message count AND IF count=1, get the message
1970       FND_MSG_PUB.Count_And_Get (
1971          p_encoded => FND_API.G_FALSE,
1972          p_count => x_msg_count,
1973          p_data  => x_msg_data
1974       );
1975    WHEN OTHERS THEN
1976       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1977       IF OZF_UNEXP_ERROR THEN
1978          FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
1979       END IF;
1980       -- Standard call to get message count AND IF count=1, get the message
1981       FND_MSG_PUB.Count_And_Get (
1982          p_encoded => FND_API.G_FALSE,
1983          p_count => x_msg_count,
1984          p_data  => x_msg_data
1985       );
1986 END Check_Duplicate_Line;
1987 
1988 ---------------------------------------------------------------------
1989 -- PROCEDURE
1990 --    Update_Duplicates
1991 --
1992 -- PURPOSE
1993 --    This procedure updates the duplicates
1994 --
1995 -- PARAMETERS
1996 --
1997 --
1998 -- NOTES
1999 ---------------------------------------------------------------------
2000 PROCEDURE Update_Duplicates (
2001     p_api_version            IN  NUMBER
2002    ,p_init_msg_LIST          IN  VARCHAR2 := FND_API.G_FALSE
2003    ,p_commit                 IN  VARCHAR2 := FND_API.G_FALSE
2004    ,p_validation_level       IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL
2005    ,p_resale_batch_id        IN  NUMBER
2006    ,p_resale_batch_type      IN  VARCHAR2
2007    ,p_batch_status           IN  VARCHAR2
2008    ,x_batch_status           OUT NOCOPY   VARCHAR2
2009    ,x_return_status          OUT NOCOPY   VARCHAR2
2010    ,x_msg_data               OUT NOCOPY   VARCHAR2
2011    ,x_msg_count              OUT NOCOPY   NUMBER
2012 )
2013 IS
2014 l_api_name          CONSTANT VARCHAR2(30) := 'Update_Duplicates';
2015 l_api_version       CONSTANT NUMBER := 1.0;
2016 l_full_name         CONSTANT VARCHAR2(60) := G_PKG_NAME ||'.'|| l_api_name;
2017 --
2018 l_return_status     VARCHAR2(30);
2019 l_msg_data          VARCHAR2(2000);
2020 l_msg_count         NUMBER;
2021 
2022 i                   NUMBER;
2023 
2024 l_dup_line_id       NUMBER;
2025 l_dup_adjustment_id NUMBER;
2026 l_reprocessing      BOOLEAN;
2027 l_duplicate_count   NUMBER  := 0;
2028 l_batch_count       NUMBER  := 0;
2029 --
2030 l_open_lines_tbl        number_tbl_type;
2031 l_direct_customer_tbl   varchar_tbl_type;
2032 l_claimed_amount_tbl    number_tbl_type;
2033 l_batch_number          ozf_resale_batches_all.batch_number%TYPE;
2034 --
2035 CURSOR open_lines_csr(p_id IN NUMBER) IS
2036 SELECT resale_line_int_id, direct_customer_flag, claimed_amount
2037   FROM ozf_resale_lines_int
2038  WHERE resale_batch_id = p_id
2039  --AMITAMKU, Fix for bug 14685303 - Added condition to avoid processing disputed line for duplicate check
2040  AND status_code <> OZF_RESALE_COMMON_PVT.G_BATCH_ADJ_DISPUTED;
2041 
2042 CURSOR batch_count_csr(pc_batch_id NUMBER) IS
2043 SELECT NVL(batch_count,0), batch_number
2044   FROM ozf_resale_batches
2045  WHERE resale_batch_id = pc_batch_id;
2046 
2047 CURSOR duplicate_count_csr(p_id NUMBER) IS
2048 SELECT count(1)
2049   FROM ozf_resale_lines_int
2050  WHERE status_code = OZF_RESALE_COMMON_PVT.G_BATCH_ADJ_DUPLICATED
2051    AND resale_batch_id = p_id;
2052 
2053 BEGIN
2054    SAVEPOINT  Update_Duplicates;
2055 
2056    -- Standard call to check for call compatibility.
2057    IF NOT FND_API.Compatible_API_Call (
2058       l_api_version,
2059       p_api_version,
2060       l_api_name,
2061       G_PKG_NAME)
2062    THEN
2063       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2064    END IF;
2065 
2066    --Initialize message LIST IF p_init_msg_LIST IS TRUE.
2067    IF FND_API.To_Boolean (p_init_msg_LIST) THEN
2068       FND_MSG_PUB.initialize;
2069    END IF;
2070 
2071    -- Debug Message
2072    IF OZF_DEBUG_HIGH_ON THEN
2073       OZF_UTILITY_PVT.debug_message(l_full_name||': Start');
2074    END IF;
2075 
2076    -- Initialize API return status to sucess
2077    x_return_status := FND_API.G_RET_STS_SUCCESS;
2078 
2079    OPEN open_lines_csr(p_resale_batch_id);
2080    FETCH open_lines_csr BULK COLLECT INTO l_open_lines_tbl, l_direct_customer_tbl, l_claimed_amount_tbl;
2081    CLOSE open_lines_csr;
2082 
2083    IF l_open_lines_tbl.EXISTS(1) THEN
2084       FOR i IN 1..l_open_lines_tbl.LAST
2085       LOOP
2086          -- BUG 4670154 (+)
2087          UPDATE ozf_resale_lines_int_all
2088             SET duplicated_line_id = NULL
2089             ,   duplicated_adjustment_id = NULL
2090          WHERE resale_line_int_id = l_open_lines_tbl(i);
2091          -- BUG 4670154 (-)
2092 
2093          OZF_RESALE_COMMON_PVT.Check_Duplicate_Line (
2094              p_api_version_number => 1.0
2095             ,p_init_msg_LIST      => FND_API.G_FALSE
2096             ,p_commit             => FND_API.G_FALSE
2097             ,p_validation_level   => FND_API.G_VALID_LEVEL_FULL
2098             ,x_return_status      => l_return_status
2099             ,x_msg_count          => l_msg_count
2100             ,x_msg_data           => l_msg_data
2101             ,p_resale_line_int_id => l_open_lines_tbl(i)
2102             ,p_direct_customer_flag => l_direct_customer_tbl(i)
2103             ,p_claimed_amount     => l_claimed_amount_tbl(i)
2104             ,p_batch_type         => p_resale_batch_type
2105             ,x_dup_line_id        => l_dup_line_id
2106             ,x_dup_adjustment_id  => l_dup_adjustment_id
2107             ,x_reprocessing       => l_reprocessing
2108          );
2109 
2110          IF l_return_status <> FND_API.G_RET_STS_SUCCESS THEN
2111             OZF_UTILITY_PVT.error_message('OZF_RESALE_CHK_DUP_ERR');
2112          ELSE
2113             IF l_dup_adjustment_id IS NOT NULL AND l_reprocessing THEN
2114                -- Set the line and adjustment as duplicates
2115                UPDATE ozf_resale_lines_int
2116                   SET status_code = OZF_RESALE_COMMON_PVT.G_BATCH_ADJ_DUPLICATED
2117                   ,   duplicated_line_id = l_dup_line_id
2118                   ,   duplicated_adjustment_id = l_dup_adjustment_id
2119                   ,   dispute_code = 'OZF_RESALE_DUP'
2120                WHERE resale_line_int_id = l_open_lines_tbl(i);
2121 
2122                Insert_Resale_Log (
2123                   p_id_value    => l_open_lines_tbl(i),
2124                   p_id_type     => 'IFACE',
2125                   p_error_code  => 'OZF_RESALE_DUP',
2126                   p_column_name => 'DUPLICATED_ADJUSTMENT_ID',
2127                   p_column_value => l_dup_adjustment_id,
2128                   x_return_status => l_return_status );
2129                IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2130                   RAISE FND_API.G_EXC_ERROR;
2131                ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2132                   RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2133                END IF;
2134             ELSE
2135                -- record dup line if necessary
2136                UPDATE ozf_resale_lines_int
2137                   SET duplicated_line_id = l_dup_line_id
2138                     , duplicated_adjustment_id = l_dup_adjustment_id
2139                 WHERE resale_line_int_id = l_open_lines_tbl(i);
2140             END IF;
2141          END IF;
2142       END LOOP;
2143    END IF;
2144 
2145    OPEN duplicate_count_csr (p_resale_batch_id);
2146    FETCH duplicate_count_csr INTO l_duplicate_count;
2147    CLOSE duplicate_count_csr;
2148 
2149    OPEN batch_count_csr(p_resale_batch_id);
2150    FETCH batch_count_csr INTO l_batch_count, l_batch_number;
2151    CLOSE batch_count_csr;
2152 
2153    IF l_duplicate_count = l_batch_count THEN
2154       -- Reject batch if all lines are duplicates
2155       x_batch_status := OZF_RESALE_COMMON_PVT.G_BATCH_REJECTED ;
2156       --
2157       UPDATE ozf_resale_batches_all
2158          SET status_code = OZF_RESALE_COMMON_PVT.G_BATCH_REJECTED
2159        WHERE resale_batch_id = p_resale_batch_id;
2160       --AMITAMKU, Fix for bug 14685303  - Added code to print the details of rejected batch in output file.
2161      FND_FILE.PUT_LINE(FND_FILE.OUTPUT, ' Batch# ' || l_batch_number || ' is rejected since all the lines are duplicates.');
2162 
2163    --bug # 6134121 fixed by ateotia(+)
2164    ELSIF (l_duplicate_count >=1) THEN
2165       --dispute the batch if lines are duplicated within in the same batch
2166       x_batch_status := OZF_RESALE_COMMON_PVT.G_BATCH_DISPUTED ;
2167       UPDATE ozf_resale_batches_all
2168          SET status_code = OZF_RESALE_COMMON_PVT.G_BATCH_DISPUTED
2169          WHERE resale_batch_id = p_resale_batch_id;
2170 	--AMITAMKU, Fix for bug 14685303  - Added code to print the details of disputed batch in output file.
2171       FND_FILE.PUT_LINE(FND_FILE.OUTPUT, ' Batch# ' || l_batch_number || ' is disputed since it has duplicate lines.');
2172    --bug # 6134121 fixed by ateotia(-)
2173    ELSE
2174       -- JXWU In this case we just keep the current status
2175       x_batch_status := p_batch_status;
2176 
2177    END IF;
2178 
2179    -- Debug Message
2180    IF OZF_DEBUG_HIGH_ON THEN
2181       OZF_UTILITY_PVT.debug_message(l_full_name||': End');
2182    END IF;
2183 
2184    --Standard call to get message count AND IF count=1, get the message
2185    FND_MSG_PUB.Count_And_Get (
2186       p_encoded => FND_API.G_FALSE,
2187       p_count => x_msg_count,
2188       p_data  => x_msg_data
2189    );
2190 
2191 EXCEPTION
2192    WHEN FND_API.G_EXC_ERROR THEN
2193       ROLLBACK TO Update_Duplicates;
2194       x_return_status := FND_API.G_RET_STS_ERROR;
2195       -- Standard call to get message count AND IF count=1, get the message
2196       FND_MSG_PUB.Count_And_Get (
2197          p_encoded => FND_API.G_FALSE,
2198          p_count => x_msg_count,
2199          p_data  => x_msg_data
2200       );
2201    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2202       ROLLBACK TO Update_Duplicates;
2203       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2204       -- Standard call to get message count AND IF count=1, get the message
2205       FND_MSG_PUB.Count_And_Get (
2206          p_encoded => FND_API.G_FALSE,
2207          p_count => x_msg_count,
2208          p_data  => x_msg_data
2209       );
2210    WHEN OTHERS THEN
2211       ROLLBACK TO Update_Duplicates;
2212       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2213       IF OZF_UNEXP_ERROR THEN
2214          FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
2215       END IF;
2216       -- Standard call to get message count AND IF count=1, get the message
2217       FND_MSG_PUB.Count_And_Get (
2218          p_encoded => FND_API.G_FALSE,
2219          p_count => x_msg_count,
2220          p_data  => x_msg_data
2221       );
2222 END Update_Duplicates;
2223 
2224 ---------------------------------------------------------------------
2225 -- PROCEDURE
2226 --   Validate_Batch
2227 --
2228 -- PURPOSE
2229 --    This procedure validates the batch information
2230 --
2231 -- PARAMETERS
2232 --
2233 --
2234 -- NOTES
2235 ---------------------------------------------------------------------
2236 PROCEDURE  Validate_Batch(
2237     p_api_version            IN  NUMBER
2238    ,p_init_msg_LIST          IN  VARCHAR2 := FND_API.G_FALSE
2239    ,p_commit                 IN  VARCHAR2 := FND_API.G_FALSE
2240    ,p_validation_level       IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL
2241    ,p_resale_batch_id        IN  NUMBER
2242    ,x_batch_status           OUT NOCOPY VARCHAR2
2243    ,x_return_status          OUT NOCOPY   VARCHAR2
2244    ,x_msg_data               OUT NOCOPY   VARCHAR2
2245    ,x_msg_count              OUT NOCOPY   NUMBER
2246 )IS
2247 l_api_name          CONSTANT VARCHAR2(30) := 'Validate_Batch';
2248 l_api_version       CONSTANT NUMBER := 1.0;
2249 l_full_name         CONSTANT VARCHAR2(60) := G_PKG_NAME ||'.'|| l_api_name;
2250 --
2251 l_return_status     VARCHAR2(30);
2252 l_batch_count NUMBER;
2253 l_status_code VARCHAR2(30);
2254 l_report_date date;
2255 l_report_start_date date;
2256 l_report_end_date date;
2257 l_partner_cust_account_id NUMBER;
2258 --
2259 l_int_line_count NUMBER;
2260 l_line_count NUMBER;
2261 l_total_line_count NUMBER := NULL;
2262 l_partner_id_count NUMBER;
2263 
2264 CURSOR batch_info_csr IS
2265 SELECT batch_count
2266      , status_code
2267      , report_date
2268      , report_start_date
2269      , report_end_date
2270      , partner_cust_account_id
2271   FROM ozf_resale_batches
2272  WHERE resale_batch_id = p_resale_batch_id;
2273 
2274 CURSOR int_line_count_csr IS
2275 SELECT count(1)
2276   FROM ozf_resale_lines_int
2277  WHERE resale_batch_id = p_resale_batch_id;
2278 
2279 CURSOR line_count_csr IS
2280 SELECT count(1)
2281   FROM ozf_resale_batch_line_maps
2282  WHERE resale_batch_id = p_resale_batch_id;
2283 
2284 CURSOR count_cust_acctid_csr(p_id NUMBER) IS
2285 SELECT 1
2286   FROM dual
2287  WHERE EXISTS (SELECT hca.cust_account_id
2288                  FROM hz_cust_accounts hca
2289                 WHERE hca.cust_account_id = p_id);
2290 --
2291 BEGIN
2292 
2293    -- Standard call to check for call compatibility.
2294    IF NOT FND_API.Compatible_API_Call (
2295      l_api_version,
2296      p_api_version,
2297      l_api_name,
2298      G_PKG_NAME)
2299    THEN
2300      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2301    END IF;
2302 
2303    --Initialize message LIST IF p_init_msg_LIST IS TRUE.
2304    IF FND_API.To_Boolean (p_init_msg_LIST) THEN
2305       FND_MSG_PUB.initialize;
2306    END IF;
2307 
2308    -- Debug Message
2309    IF OZF_DEBUG_HIGH_ON THEN
2310       OZF_UTILITY_PVT.debug_message(l_full_name||': Start');
2311    END IF;
2312 
2313    -- Initialize API return status to sucess
2314    x_return_status := FND_API.G_RET_STS_SUCCESS;
2315 
2316    -- First, do some basic check
2317    OPEN batch_info_csr;
2318    FETCH batch_info_csr INTO l_batch_count,
2319                              l_status_code,
2320                              l_report_date,
2321                              l_report_start_date,
2322                              l_report_end_date,
2323                              l_partner_cust_account_id;
2324    CLOSE batch_info_csr;
2325 
2326    IF OZF_DEBUG_LOW_ON THEN
2327       OZF_UTILITY_PVT.debug_message('Report date:' || l_report_date);
2328       OZF_UTILITY_PVT.debug_message('Report Start date:' || l_report_start_date);
2329       OZF_UTILITY_PVT.debug_message('Report End date:' || l_report_end_date);
2330    END IF;
2331 
2332    -- Check status
2333    IF l_status_code <> G_BATCH_PROCESSING THEN
2334       --
2335       x_return_status := FND_API.G_RET_STS_ERROR;
2336       --
2337       Insert_Resale_Log (
2338          p_id_value      => p_resale_batch_id,
2339          p_id_type       => G_ID_TYPE_BATCH,
2340          p_error_code    => 'OZF_BATCH_STATUS_WNG',
2341          p_column_name   => 'STATUS_CODE',
2342          p_column_value  => l_status_code,
2343          x_return_status => l_return_status );
2344       --
2345       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2346          RAISE FND_API.G_EXC_ERROR;
2347       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2348          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2349       END IF;
2350       --
2351    END IF;
2352 
2353    -- Check report_date
2354    IF l_report_date IS NULL THEN
2355       --
2356       x_return_status := FND_API.G_RET_STS_ERROR;
2357       --
2358       Insert_Resale_Log (
2359          p_id_value      => p_resale_batch_id,
2360          p_id_type       => G_ID_TYPE_BATCH,
2361          p_error_code    => 'OZF_BATCH_REPORT_DATE_NULL',
2362          p_column_name   => 'REPORT_date',
2363          p_column_value  => NULL,
2364          x_return_status => l_return_status );
2365       --
2366       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2367          RAISE FND_API.G_EXC_ERROR;
2368       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2369          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2370       END IF;
2371       --
2372    END IF;
2373 
2374    -- Check report_start_date
2375    IF l_report_start_date IS NULL THEN
2376       --
2377       x_return_status := FND_API.G_RET_STS_ERROR;
2378       --
2379       Insert_Resale_Log (
2380          p_id_value      => p_resale_batch_id,
2381          p_id_type       => G_ID_TYPE_BATCH,
2382          p_error_code    => 'OZF_BATCH_REPORT_ST_DATE_NULL',
2383          p_column_name   => 'REPORT_START_date',
2384          p_column_value  => NULL,
2385          x_return_status => l_return_status );
2386       --
2387       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2388          RAISE FND_API.G_EXC_ERROR;
2389       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2390          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2391       END IF;
2392       --
2393    END IF;
2394 
2395    -- Check report_end_date
2396    IF l_report_end_date IS NULL THEN
2397       --
2398       x_return_status := FND_API.G_RET_STS_ERROR;
2399       --
2400       Insert_Resale_Log (
2401          p_id_value      => p_resale_batch_id,
2402          p_id_type       => G_ID_TYPE_BATCH,
2403          p_error_code    => 'OZF_BATCH_REPORT_END_DATE_NULL',
2404          p_column_name   => 'REPORT_END_date',
2405          p_column_value  => NULL,
2406          x_return_status => l_return_status );
2407       --
2408       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2409          RAISE FND_API.G_EXC_ERROR;
2410       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2411          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2412       END IF;
2413       --
2414    END IF;
2415 
2416    --
2417    IF l_report_date IS NOT NULL        AND
2418       l_report_start_date IS NOT NULL  AND
2419       l_report_end_date IS NOT NULL
2420    THEN
2421       IF l_report_start_date > l_report_end_date THEN
2422 
2423          x_return_status := FND_API.G_RET_STS_ERROR;
2424          --
2425          Insert_Resale_Log (
2426             p_id_value      => p_resale_batch_id,
2427             p_id_type       => G_ID_TYPE_BATCH,
2428             p_error_code    => 'OZF_RESALE_WNG_DATE_RANGE',
2429             p_column_name   => 'REPORT_START_DATE',
2430             p_column_value  => NULL,
2431             x_return_status => l_return_status );
2432          --
2433          IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2434             RAISE FND_API.G_EXC_ERROR;
2435          ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2436             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2437          END IF;
2438          --
2439       END IF;
2440       --
2441    END IF;
2442 
2443    -- Check partner_cust_account_id
2444    IF l_partner_cust_account_id IS NULL THEN
2445       --
2446       x_return_status := FND_API.G_RET_STS_ERROR;
2447       --
2448       Insert_Resale_Log (
2449          p_id_value      => p_resale_batch_id,
2450          p_id_type       => G_ID_TYPE_BATCH,
2451          p_error_code    => 'OZF_BATCH_PARTNER_NULL',
2452          p_column_name   => 'PARTNER_CUST_ACCOUNT_ID',
2453          p_column_value  => NULL,
2454          x_return_status => l_return_status );
2455       --
2456       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2457          RAISE FND_API.G_EXC_ERROR;
2458       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2459          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2460       END IF;
2461       --
2462    ELSE
2463       -- make sure partner cust_account_id IS valid
2464       OPEN count_cust_acctid_csr(l_partner_cust_account_id);
2465       FETCH count_cust_acctid_csr INTO l_partner_id_count;
2466       CLOSE count_cust_acctid_csr;
2467       --
2468       IF l_partner_id_count IS NULL THEN
2469          x_return_status := FND_API.G_RET_STS_ERROR;
2470          --
2471          Insert_Resale_Log (
2472             p_id_value      => p_resale_batch_id,
2473             p_id_type       => G_ID_TYPE_BATCH,
2474             p_error_code    => 'OZF_BATCH_PARTNER_ERR',
2475             p_column_name   => 'PARTNER_CUST_ACCOUNT_ID',
2476             p_column_value  => l_partner_cust_account_id,
2477             x_return_status => l_return_status );
2478          --
2479          IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2480             RAISE FND_API.G_EXC_ERROR;
2481          ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2482             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2483          END IF;
2484          --
2485       END IF;
2486    END IF;
2487 
2488    -- check batch count. why IS this required ??
2489    IF l_batch_count IS NULL OR
2490       l_batch_count = 0
2491    THEN
2492       --
2493       x_return_status := FND_API.G_RET_STS_ERROR;
2494       --
2495       Insert_Resale_Log (
2496          p_id_value      => p_resale_batch_id,
2497          p_id_type       => G_ID_TYPE_BATCH,
2498          p_error_code    => 'OZF_BATCH_COUNT_NULL',
2499          p_column_name   => 'BATCH_COUNT',
2500          p_column_value  => NULL,
2501          x_return_status => l_return_status );
2502       --
2503       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2504          RAISE FND_API.G_EXC_ERROR;
2505       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2506          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2507       END IF;
2508       --
2509    END IF;
2510 
2511    -- THEN I will check whether the batch_count = line belongs to that batch
2512    OPEN int_line_count_csr;
2513    FETCH int_line_count_csr INTO l_int_line_count;
2514    CLOSE int_line_count_csr;
2515 
2516    OPEN line_count_csr;
2517    FETCH line_count_csr INTO l_line_count;
2518    CLOSE line_count_csr;
2519 
2520    IF l_int_line_count IS NOT NULL THEN
2521       -- records the total number of lines in interface table for the batch
2522       l_total_line_count := l_int_line_count;
2523       IF l_line_count IS NOT NULL THEN
2524          -- records the total number of lines in int table and map table for the batch
2525          l_total_line_count := l_total_line_count + l_line_count;
2526       END IF;
2527    ELSE
2528       IF l_line_count IS NOT NULL THEN
2529          -- records the total number of lines in map table for the batch
2530          l_total_line_count := l_line_count;
2531       END IF;
2532    END IF;
2533 
2534    -- batch without any lines cannot be processed
2535    IF l_total_line_count IS NULL THEN
2536       --
2537       x_return_status := FND_API.G_RET_STS_ERROR;
2538       --
2539       Insert_Resale_Log (
2540          p_id_value      => p_resale_batch_id,
2541          p_id_type       => G_ID_TYPE_BATCH,
2542          p_error_code    => 'OZF_BATCH_LINE_COUNT_ERR',
2543          p_column_name   => NULL,
2544          p_column_value  => NULL,
2545          x_return_status => l_return_status );
2546       --
2547       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2548          RAISE FND_API.G_EXC_ERROR;
2549       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2550          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2551       END IF;
2552       --
2553    ELSE
2554       -- checks if the batch count and actual number of lines in batch are same
2555       IF l_batch_count IS NOT NULL AND
2556          l_batch_count <> l_total_line_count
2557       THEN
2558          --
2559          x_return_status := FND_API.G_RET_STS_ERROR;
2560          --
2561          Insert_Resale_Log (
2562             p_id_value      => p_resale_batch_id,
2563             p_id_type       => G_ID_TYPE_BATCH,
2564             p_error_code    => 'OZF_BATCH_COUNT_ERR',
2565             p_column_name   => 'BATCH_COUNT',
2566             p_column_value  => l_batch_count,
2567             x_return_status => l_return_status );
2568          --
2569          IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2570             RAISE FND_API.G_EXC_ERROR;
2571          ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2572             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2573          END IF;
2574          --
2575       END IF;
2576    END IF;
2577 
2578    IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2579       -- SLKRISHN common update
2580       BEGIN
2581          UPDATE ozf_resale_batches
2582          SET    status_code= G_BATCH_DISPUTED
2583          WHERE  resale_batch_id = p_resale_batch_id;
2584       EXCEPTION
2585          WHEN OTHERS THEN
2586             IF OZF_UNEXP_ERROR THEN
2587                FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
2588             END IF;
2589             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2590       END;
2591    END IF;
2592 
2593    -- Debug Message
2594    IF OZF_DEBUG_HIGH_ON THEN
2595       OZF_UTILITY_PVT.debug_message(l_full_name||': END');
2596    END IF;
2597 
2598    --Standard call to get message count AND IF count=1, get the message
2599    FND_MSG_PUB.Count_And_Get (
2600       p_encoded => FND_API.G_FALSE,
2601       p_count => x_msg_count,
2602       p_data  => x_msg_data
2603    );
2604 --
2605 EXCEPTION
2606    WHEN FND_API.G_EXC_ERROR THEN
2607       x_return_status := FND_API.G_RET_STS_ERROR;
2608       -- Standard call to get message count AND IF count=1, get the message
2609       FND_MSG_PUB.Count_And_Get (
2610          p_encoded => FND_API.G_FALSE,
2611          p_count => x_msg_count,
2612          p_data  => x_msg_data
2613       );
2614    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2615       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2616       -- Standard call to get message count AND IF count=1, get the message
2617       FND_MSG_PUB.Count_And_Get (
2618          p_encoded => FND_API.G_FALSE,
2619          p_count => x_msg_count,
2620          p_data  => x_msg_data
2621       );
2622    WHEN OTHERS THEN
2623       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2624       IF OZF_UNEXP_ERROR THEN
2625          FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
2626       END IF;
2627       -- Standard call to get message count AND IF count=1, get the message
2628       FND_MSG_PUB.Count_And_Get (
2629          p_encoded => FND_API.G_FALSE,
2630          p_count => x_msg_count,
2631          p_data  => x_msg_data
2632       );
2633 END Validate_Batch;
2634 
2635 ---------------------------------------------------------------------
2636 -- PROCEDURE
2637 --   Validate_Order_Record
2638 --
2639 -- PURPOSE
2640 --    This procedure validates the order information
2641 --
2642 -- PARAMETERS
2643 --
2644 --
2645 -- NOTES
2646 ---------------------------------------------------------------------
2647 PROCEDURE  Validate_Order_Record(
2648     p_api_version            IN  NUMBER
2649    ,p_init_msg_LIST          IN  VARCHAR2 := FND_API.G_FALSE
2650    ,p_commit                 IN  VARCHAR2 := FND_API.G_FALSE
2651    ,p_validation_level       IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL
2652    ,p_resale_batch_id        IN  NUMBER
2653    ,x_return_status          OUT NOCOPY   VARCHAR2
2654    ,x_msg_data               OUT NOCOPY   VARCHAR2
2655    ,x_msg_count              OUT NOCOPY   NUMBER
2656 )IS
2657 l_api_name          CONSTANT VARCHAR2(30) := 'Validate_Order_Record';
2658 l_api_version       CONSTANT NUMBER := 1.0;
2659 l_full_name         CONSTANT VARCHAR2(60) := G_PKG_NAME ||'.'|| l_api_name;
2660 --
2661 l_return_status varchar2(1);
2662 BEGIN
2663 
2664    -- Standard call to check for call compatibility.
2665    IF NOT FND_API.Compatible_API_Call (
2666        l_api_version,
2667        p_api_version,
2668        l_api_name,
2669        G_PKG_NAME)
2670    THEN
2671       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2672    END IF;
2673 
2674    --Initialize message LIST IF p_init_msg_LIST IS TRUE.
2675    IF FND_API.To_Boolean (p_init_msg_LIST) THEN
2676       FND_MSG_PUB.initialize;
2677    END IF;
2678 
2679    -- Debug Message
2680    IF OZF_DEBUG_HIGH_ON THEN
2681       OZF_UTILITY_PVT.debug_message(l_full_name||': Start');
2682    END IF;
2683 
2684    -- Initialize API return status to sucess
2685    x_return_status := FND_API.G_RET_STS_SUCCESS;
2686 
2687    -- Log lines with null values when required
2688    Log_Null_Values (
2689       p_batch_id       =>p_resale_batch_id,
2690       x_return_status  => l_return_status);
2691    --
2692    IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2693       RAISE FND_API.G_EXC_ERROR;
2694    ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2695       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2696    END IF;
2697    --
2698 
2699    -- Log lines with invalid values
2700    Log_Invalid_Values (
2701       p_batch_id       =>p_resale_batch_id,
2702       x_return_status  => l_return_status);
2703    --
2704    IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2705       RAISE FND_API.G_EXC_ERROR;
2706    ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2707       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2708    END IF;
2709    --
2710 
2711    Bulk_Dispute_Line (
2712       p_batch_id      => p_resale_batch_id,
2713       p_line_status   => G_BATCH_ADJ_OPEN,
2714       x_return_status => l_return_status
2715    );
2716    --
2717    IF l_return_status = FND_API.G_RET_STS_ERROR THEN
2718       RAISE FND_API.G_EXC_ERROR;
2719    ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
2720       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
2721    END IF;
2722    --
2723 
2724    -- Debug Message
2725    IF OZF_DEBUG_HIGH_ON THEN
2726       OZF_UTILITY_PVT.debug_message(l_full_name||': End');
2727    END IF;
2728 
2729    --Standard call to get message count AND IF count=1, get the message
2730    FND_MSG_PUB.Count_And_Get (
2731       p_encoded => FND_API.G_FALSE,
2732       p_count => x_msg_count,
2733       p_data  => x_msg_data
2734    );
2735 EXCEPTION
2736    WHEN FND_API.G_EXC_ERROR THEN
2737       x_return_status := FND_API.G_RET_STS_ERROR;
2738       -- Standard call to get message count AND IF count=1, get the message
2739       FND_MSG_PUB.Count_And_Get (
2740           p_encoded => FND_API.G_FALSE,
2741           p_count => x_msg_count,
2742           p_data  => x_msg_data
2743       );
2744    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
2745       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2746       -- Standard call to get message count AND IF count=1, get the message
2747       FND_MSG_PUB.Count_And_Get (
2748           p_encoded => FND_API.G_FALSE,
2749           p_count => x_msg_count,
2750           p_data  => x_msg_data
2751       );
2752    WHEN OTHERS THEN
2753       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
2754       IF OZF_UNEXP_ERROR THEN
2755          FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
2756       END IF;
2757       -- Standard call to get message count AND IF count=1, get the message
2758       FND_MSG_PUB.Count_And_Get (
2759           p_encoded => FND_API.G_FALSE,
2760           p_count => x_msg_count,
2761           p_data  => x_msg_data
2762       );
2763 END Validate_Order_Record;
2764 
2765 ---------------------------------------------------------------------
2766 -- PROCEDURE
2767 --    Create_Utilization_record
2768 --
2769 -- PURPOSE
2770 --    ThIS procedure prepare the record FOR utilization
2771 --
2772 -- PARAMETERS
2773 --
2774 --
2775 -- NOTES
2776 -- 27/10/2009  muthsubr Added px_ozf_act_budgets_tbl, p_use_fund_staging_tables
2777 --                      px_ozf_funds_old_rectype, px_ozf_funds_new_rectype
2778 --                      for bug#8867381.
2779 -- 29/11/2010  muthsubr Altered px_ozf_funds_new_rectype to px_ozf_funds_new_tbl
2780 --                      for JMS ER Bug#9614703.
2781 ---------------------------------------------------------------------
2782 PROCEDURE  Create_Utilization_record(
2783    p_line_int_rec        IN  g_interface_rec_csr%ROWTYPE
2784   ,p_batch_type          IN  VARCHAR2
2785   ,p_fund_id             IN  NUMBER
2786   ,p_line_id             IN  NUMBER
2787   ,p_cust_account_id     IN  NUMBER
2788   ,p_approver_id         IN  NUMBER
2789   ,p_line_agreement_flag IN  VARCHAR2
2790   ,p_utilization_type    IN  VARCHAR2
2791   ,p_adjustment_type_id     IN  NUMBER
2792   ,p_budget_source_type  IN  VARCHAR2
2793   ,p_budget_source_id    IN  NUMBER
2794   ,p_justification       IN  VARCHAR2
2795   ,p_to_create_utilization  IN BOOLEAN
2796   ,x_return_status       OUT NOCOPY VARCHAR2
2797   ,p_use_fund_staging_tables IN  VARCHAR2
2798   ,px_ozf_act_budgets_tbl       IN OUT  NOCOPY  OZF_UTILITY_PVT.ozf_act_budgets_table
2799   ,px_ozf_funds_old_rectype     IN      OZF_UTILITY_PVT.ozf_funds_all_b_rectype
2800   ,px_ozf_funds_new_tbl     IN OUT  NOCOPY  OZF_UTILITY_PVT.ozf_funds_table
2801 )
2802 IS
2803 l_api_name          CONSTANT VARCHAR2(30) := 'Create_Utilization_Rec';
2804 l_api_version       CONSTANT NUMBER := 1.0;
2805 l_full_name         CONSTANT VARCHAR2(60) := G_PKG_NAME ||'.'|| l_api_name;
2806 --
2807 l_return_status     VARCHAR2(30);
2808 l_msg_data          VARCHAR2(2000);
2809 l_msg_count         NUMBER;
2810 
2811 l_pric_act_budgets_rec ozf_actbudgets_pvt.act_budgets_rec_type;
2812 l_pric_act_util_rec    ozf_actbudgets_pvt.act_util_rec_type;
2813 l_pric_price_adj_rec   ozf_resale_adjustments_all%ROWTYPE;
2814 
2815 l_adjustment_id NUMBER;
2816 l_rate NUMBER;
2817 l_exchange_type VARCHAR2(30);
2818 l_utilization_id NUMBER;
2819 BEGIN
2820    -- Standard begin of API savepoint
2821    SAVEPOINT IDSM_Create_Utiz_Rec;
2822    x_return_status := FND_API.G_RET_STS_SUCCESS;
2823 
2824    --
2825    IF OZF_DEBUG_HIGH_ON THEN
2826       OZF_UTILITY_PVT.debug_message(l_full_name||': Start');
2827    END IF;
2828 
2829    -- Create act Utilization Record.
2830    l_pric_act_util_rec.object_type        := 'TP_ORDER';
2831    l_pric_act_util_rec.object_id          :=  p_line_id;
2832    l_pric_act_util_rec.product_level_type := 'PRODUCT';
2833    l_pric_act_util_rec.product_id         :=  p_line_int_rec.inventory_item_Id;
2834 
2835    -- Pass partner account id for this
2836    l_pric_act_util_rec.billto_cust_account_id    := p_cust_account_id;
2837    l_pric_act_util_rec.utilization_type   := p_utilization_type;
2838    l_pric_act_util_rec.adjustment_type_id := p_adjustment_type_id;
2839 
2840    -- Reference for batch
2841    l_pric_act_util_rec.reference_type     := OZF_RESALE_COMMON_PVT.G_BATCH_REF_TYPE;
2842    l_pric_act_util_rec.reference_id       := p_line_int_rec.resale_batch_id;
2843 
2844    -- Add gl_date
2845    l_pric_act_util_rec.gl_date            := p_line_int_rec.date_shipped;
2846 
2847    -- Add org_id
2848    l_pric_act_util_rec.org_id             := p_line_int_rec.org_id;
2849 
2850    --nirprasa,12.2 ER 8399134
2851    l_pric_act_util_rec.plan_currency_code           := p_line_int_rec.currency_code;
2852    l_pric_act_util_rec.fund_request_currency_code   := p_line_int_rec.currency_code;
2853    --nirprasa,12.2
2854    -- Create Budget Record.
2855    l_pric_act_budgets_rec.parent_source_id  := p_fund_id;
2856    l_pric_act_budgets_rec.arc_act_budget_used_by := p_budget_source_type;
2857    l_pric_act_budgets_rec.act_budget_used_by_id  := p_budget_source_id;
2858    l_pric_act_budgets_rec.budget_source_type     := p_budget_source_type;
2859    l_pric_act_budgets_rec.budget_source_id       := p_budget_source_id;
2860    l_pric_act_budgets_rec.status_code            := 'APPROVED';--l_utilization_rec.status_code;
2861 
2862    -- get request amount in budget currency
2863    l_pric_act_budgets_rec.request_currency       := p_line_int_rec.currency_code;
2864    l_pric_act_budgets_rec.request_amount         := p_line_int_rec.total_accepted_amount;
2865 
2866    OZF_UTILITY_PVT.debug_message('l_pric_act_util_rec.plan_currency_code  : '||l_pric_act_util_rec.plan_currency_code  );
2867 
2868    IF OZF_DEBUG_LOW_ON THEN
2869       OZF_UTILITY_PVT.debug_message('request currency'|| l_pric_act_budgets_rec.request_currency);
2870       OZF_UTILITY_PVT.debug_message('request amount: '||l_pric_act_budgets_rec.request_amount);
2871    END IF;
2872 
2873    IF p_batch_type <> G_SPECIAL_PRICING THEN
2874       l_pric_act_budgets_rec.parent_src_curr :=
2875             OZF_ACTBUDGETS_PVT.Get_Object_Currency (
2876                'FUND'
2877                ,l_pric_act_budgets_rec.parent_source_id
2878                ,l_return_status
2879             );
2880       IF l_return_status = FND_API.g_ret_sts_error THEN
2881          RAISE FND_API.g_exc_error;
2882       ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
2883          RAISE FND_API.g_exc_unexpected_error;
2884       END IF;
2885 
2886       l_pric_act_budgets_rec.parent_src_apprvd_amt := p_line_int_rec.total_accepted_amount;
2887 
2888       IF p_line_int_rec.currency_code <> l_pric_act_budgets_rec.parent_src_curr THEN
2889          IF OZF_DEBUG_LOW_ON THEN
2890             OZF_UTILITY_PVT.debug_message('in exchange');
2891          END IF;
2892          -- get convertion type
2893          OPEN OZF_RESALE_COMMON_PVT.g_exchange_rate_type_csr;
2894          FETCH OZF_RESALE_COMMON_PVT.g_exchange_rate_type_csr INTO l_exchange_type;
2895          CLOSE OZF_RESALE_COMMON_PVT.g_exchange_rate_type_csr;
2896 
2897          OZF_UTILITY_PVT.Convert_Currency (
2898              p_from_currency   => p_line_int_rec.currency_code
2899             ,p_to_currency     => l_pric_act_budgets_rec.parent_src_curr
2900             ,p_conv_type       => l_exchange_type
2901             ,p_conv_rate       => FND_API.G_MISS_NUM
2902             ,p_conv_date       => sysdate
2903             ,p_from_amount     => p_line_int_rec.total_accepted_amount
2904             ,x_return_status   => l_return_status
2905             ,x_to_amount       => l_pric_act_budgets_rec.parent_src_apprvd_amt
2906             ,x_rate            => l_rate);
2907 
2908          IF l_return_status = FND_API.g_ret_sts_error THEN
2909             RAISE FND_API.g_exc_error;
2910          ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
2911             RAISE FND_API.g_exc_unexpected_error;
2912          END IF;
2913          --
2914       END IF;
2915 
2916    END IF;
2917 
2918    IF OZF_DEBUG_LOW_ON THEN
2919       OZF_UTILITY_PVT.debug_message('act currency: '||p_line_int_rec.currency_code);
2920       OZF_UTILITY_PVT.debug_message('par src: '||l_pric_act_budgets_rec.parent_src_curr);
2921       OZF_UTILITY_PVT.debug_message('approve amount: '||l_pric_act_budgets_rec.parent_src_apprvd_amt);
2922    END IF;
2923 
2924    l_pric_act_budgets_rec.transfer_type := 'UTILIZED';
2925    l_pric_act_budgets_rec.justification := p_justification;
2926 
2927    -- Add approver_id AND requester_id
2928    l_pric_act_budgets_rec.approver_id := OZF_UTILITY_PVT.get_resource_id (p_approver_id);
2929    l_pric_act_budgets_rec.requester_id := OZF_UTILITY_PVT.get_resource_id (p_approver_id);
2930 
2931    -- Insert INTO ozf_adjustment TABLE.
2932    OPEN OZF_RESALE_COMMON_PVT.g_adjustment_id_csr;
2933    FETCH OZF_RESALE_COMMON_PVT.g_adjustment_id_csr INTO l_adjustment_id;
2934    CLOSE OZF_RESALE_COMMON_PVT.g_adjustment_id_csr;
2935 
2936    l_pric_price_adj_rec.resale_adjustment_id := l_adjustment_id;
2937    l_pric_price_adj_rec.resale_batch_id := p_line_int_rec.resale_batch_id;
2938    l_pric_price_adj_rec.resale_line_id := p_line_id;
2939    l_pric_price_adj_rec.status_code := OZF_RESALE_COMMON_PVT.G_BATCH_ADJ_CLOSED;
2940    l_pric_price_adj_rec.orig_system_agreement_uom      := p_line_int_rec.orig_system_agreement_uom;
2941    l_pric_price_adj_rec.orig_system_agreement_name     := p_line_int_rec.orig_system_agreement_name;
2942    l_pric_price_adj_rec.orig_system_agreement_type     := p_line_int_rec.orig_system_agreement_type;
2943    l_pric_price_adj_rec.orig_system_agreement_status   := p_line_int_rec.orig_system_agreement_status;
2944    l_pric_price_adj_rec.orig_system_agreement_curr     := p_line_int_rec.orig_system_agreement_curr;
2945    l_pric_price_adj_rec.orig_system_agreement_price    := p_line_int_rec.orig_system_agreement_price;
2946    l_pric_price_adj_rec.orig_system_agreement_quantity := p_line_int_rec.orig_system_agreement_quantity;
2947    l_pric_price_adj_rec.agreement_id                   := p_line_int_rec.agreement_id;
2948    l_pric_price_adj_rec.agreement_type                 := p_line_int_rec.agreement_type;
2949    l_pric_price_adj_rec.agreement_name                 := p_line_int_rec.agreement_name;
2950    l_pric_price_adj_rec.agreement_price                := p_line_int_rec.agreement_price;
2951    l_pric_price_adj_rec.agreement_uom_code             := p_line_int_rec.agreement_uom_code;
2952    l_pric_price_adj_rec.corrected_agreement_id         := p_line_int_rec.corrected_agreement_id;
2953    l_pric_price_adj_rec.corrected_agreement_name       := p_line_int_rec.corrected_agreement_name;
2954    l_pric_price_adj_rec.credit_code                    := p_line_int_rec.credit_code;
2955    l_pric_price_adj_rec.credit_advice_date             := p_line_int_rec.credit_advice_date;
2956    l_pric_price_adj_rec.claimed_amount                 := p_line_int_rec.claimed_amount;
2957    l_pric_price_adj_rec.total_claimed_amount           := p_line_int_rec.total_claimed_amount;
2958    l_pric_price_adj_rec.allowed_amount                 := p_line_int_rec.allowed_amount;
2959    l_pric_price_adj_rec.total_allowed_amount           := p_line_int_rec.total_allowed_amount;
2960    l_pric_price_adj_rec.accepted_amount                := p_line_int_rec.accepted_amount;
2961    l_pric_price_adj_rec.total_accepted_amount          := p_line_int_rec.total_accepted_amount;
2962    l_pric_price_adj_rec.calculated_price               := p_line_int_rec.calculated_price;
2963    l_pric_price_adj_rec.acctd_calculated_price         := p_line_int_rec.acctd_calculated_price;
2964    l_pric_price_adj_rec.calculated_amount              := p_line_int_rec.calculated_amount;
2965    l_pric_price_adj_rec.line_agreement_flag            := p_line_agreement_flag;
2966    l_pric_price_adj_rec.tolerance_flag                 := p_line_int_rec.tolerance_flag;
2967    l_pric_price_adj_rec.line_tolerance_amount          := p_line_int_rec.line_tolerance_amount;
2968    l_pric_price_adj_rec.operand                        := NULL;
2969    l_pric_price_adj_rec.operand_calculation_code       := NULL;
2970    l_pric_price_adj_rec.priced_quantity                := p_line_int_rec.quantity;
2971    l_pric_price_adj_rec.priced_uom_code                := p_line_int_rec.uom_code;
2972    l_pric_price_adj_rec.priced_unit_price              := p_line_int_rec.calculated_price;
2973    l_pric_price_adj_rec.liSt_header_id                 := NULL;
2974    l_pric_price_adj_rec.liSt_line_id                   := NULL;
2975 
2976 
2977    -- For Bug#8867381, introduced p_use_fund_staging_tables for staging table impln of chargeback flow
2978    -- and px_ozf_act_budgets_tbl for PLSQL table impln of storing budgets value.
2979    -- and px_ozf_funds_old_rectype for taking the funds old values
2980    -- For JMS ER Bug#9614703
2981    -- and changed px_ozf_funds_new_rectype to px_ozf_funds_new_tbl.
2982 
2983    OZF_RESALE_COMMON_PVT.Create_Adj_And_Utilization(
2984        p_api_version			=> 1
2985       ,p_init_msg_list			=> FND_API.G_FALSE
2986       ,p_commit				=> FND_API.G_FALSE
2987       ,p_validation_level		=> FND_API.G_VALID_LEVEL_FULL
2988       ,p_price_adj_rec			=> l_pric_price_adj_rec
2989       ,p_act_budgets_rec		=> l_pric_act_budgets_rec
2990       ,p_act_util_rec			=> l_pric_act_util_rec
2991       ,p_to_create_utilization		=> p_to_create_utilization
2992       ,x_return_status			=> l_return_status
2993       ,x_msg_data			=> l_msg_data
2994       ,x_msg_count			=> l_msg_count
2995       ,x_utilization_id			=> l_utilization_id   ---ER 13333298
2996       ,p_batch_type			=> p_batch_type
2997       ,p_use_fund_staging_tables	=> p_use_fund_staging_tables
2998       ,px_ozf_act_budgets_tbl		=> px_ozf_act_budgets_tbl
2999       ,px_ozf_funds_old_rectype		=> px_ozf_funds_old_rectype
3000       ,px_ozf_funds_new_tbl		=> px_ozf_funds_new_tbl  -- For JMS ER Bug#9614703
3001    );
3002    IF l_return_status = FND_API.g_ret_sts_error THEN
3003       RAISE FND_API.g_exc_error;
3004    ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
3005       RAISE FND_API.g_exc_unexpected_error;
3006    END IF;
3007 
3008    -- update the interface duplicated line
3009    UPDATE ozf_resale_lines_int_all
3010    SET duplicated_line_id = p_line_id
3011      , duplicated_adjustment_id =l_adjustment_id
3012    WHERE duplicated_line_id = p_line_int_rec.resale_line_int_id
3013    AND duplicated_adjustment_id = -1;
3014 
3015    IF OZF_DEBUG_HIGH_ON THEN
3016       OZF_UTILITY_PVT.debug_message(l_full_name||': End');
3017    END IF;
3018 
3019 EXCEPTION
3020    WHEN FND_API.g_exc_error THEN
3021       ROLLBACK TO IDSM_Create_Utiz_Rec;
3022       x_return_status := FND_API.G_RET_STS_ERROR;
3023    WHEN FND_API.g_exc_unexpected_error THEN
3024       ROLLBACK TO IDSM_Create_Utiz_Rec;
3025       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3026    WHEN OTHERS THEN
3027       ROLLBACK TO IDSM_Create_Utiz_Rec;
3028       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3029       IF OZF_UNEXP_ERROR THEN
3030          FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
3031       END IF;
3032 END Create_Utilization_record;
3033 
3034 -- For TPA Parallel Execution ER - 9614703 (+)
3035 /*
3036 ---------------------------------------------------------------------
3037 -- PROCEDURE
3038 --    Create_Adj_And_Utilization
3039 --
3040 -- PURPOSE
3041 --    This function receives the price adjustment rec AND utilization record
3042 --    It them inserts the price adjustmetns AND utilization
3043 --
3044 -- PARAMETERS
3045 -- p_use_fund_staging_tables for staging table impln of chargeback flow
3046 -- and px_ozf_act_budgets_tbl for PLSQL table impln of storing budgets value.
3047 -- and px_ozf_funds_old_rectype for taking the funds old values
3048 -- and px_ozf_funds_new_rectype for taking the new values after updation.
3049 --
3050 -- NOTES
3051 -- Overloaded procedure Create_Adj_And_Utilization for bug#8867381.
3052 --
3053 ---------------------------------------------------------------------
3054 PROCEDURE Create_Adj_And_Utilization(
3055     p_api_version            IN    NUMBER
3056    ,p_init_msg_LIST          IN    VARCHAR2 := FND_API.G_FALSE
3057    ,p_commit                 IN    VARCHAR2 := FND_API.G_FALSE
3058    ,p_validation_level       IN    NUMBER   := FND_API.G_VALID_LEVEL_FULL
3059    ,p_price_adj_rec          IN    ozf_resale_adjustments_all%rowtype
3060    ,p_act_budgets_rec        IN    ozf_actbudgets_pvt.act_budgets_rec_type
3061    ,p_act_util_rec           IN    ozf_actbudgets_pvt.act_util_rec_type
3062    ,p_to_create_utilization  IN BOOLEAN
3063    ,x_return_status          OUT NOCOPY VARCHAR2
3064    ,x_msg_data               OUT NOCOPY   VARCHAR2
3065    ,x_msg_count              OUT NOCOPY   NUMBER
3066    --,p_use_fund_staging_tables IN VARCHAR2
3067 )IS
3068 l_use_fund_staging_tables VARCHAR2(1) := 'F';
3069 lx_ozf_act_budgets_tbl   ozf_chargeback_pvt.ozf_act_budgets_table;
3070 lx_ozf_funds_old_rectype OZF_CHARGEBACK_PVT.ozf_funds_all_b_rectype;
3071 lx_ozf_funds_new_rectype OZF_CHARGEBACK_PVT.ozf_funds_all_b_rectype;
3072 BEGIN
3073 Create_Adj_And_Utilization(
3074     p_api_version            =>    p_api_version
3075    ,p_init_msg_LIST          =>    p_init_msg_LIST
3076    ,p_commit                 =>    p_commit
3077    ,p_validation_level       =>    p_validation_level
3078    ,p_price_adj_rec          =>   p_price_adj_rec
3079    ,p_act_budgets_rec        =>    p_act_budgets_rec
3080    ,p_act_util_rec           =>    p_act_util_rec
3081    ,p_to_create_utilization  =>  p_to_create_utilization
3082    ,x_return_status          => x_return_status
3083    ,x_msg_data               =>   x_msg_data
3084    ,x_msg_count              =>   x_msg_count
3085    ,p_use_fund_staging_tables => l_use_fund_staging_tables
3086    ,px_ozf_act_budgets_tbl      =>  lx_ozf_act_budgets_tbl
3087    ,px_ozf_funds_old_rectype    =>      lx_ozf_funds_old_rectype
3088    ,px_ozf_funds_new_rectype    =>      lx_ozf_funds_new_rectype
3089 );
3090 END Create_Adj_And_Utilization;
3091 */
3092 
3093 
3094 ---------------------------------------------------------------------
3095 -- PROCEDURE
3096 --    Create_Adj_And_Utilization
3097 --
3098 -- PURPOSE
3099 --    This function receives the price adjustment rec AND utilization record
3100 --    It them inserts the price adjustmetns AND utilization
3101 --
3102 -- PARAMETERS
3103 -- p_use_fund_staging_tables for staging table impln of chargeback flow
3104 -- and px_ozf_act_budgets_tbl for PLSQL table impln of storing budgets value.
3105 -- and px_ozf_funds_old_rectype for taking the funds old values
3106 -- and px_ozf_funds_new_tbl for taking the new values after updation.
3107 --
3108 -- NOTES
3109 -- Overloaded procedure Create_Adj_And_Utilization for bug#8867381.
3110 -- Altered the procedure for JMS ER bug#9614703.
3111 --06/11/2011   BKUNJAN Added OUT parameter x_utilization_id for ER 13333298
3112 ---------------------------------------------------------------------
3113 PROCEDURE Create_Adj_And_Utilization(
3114     p_api_version            IN    NUMBER
3115    ,p_init_msg_LIST          IN    VARCHAR2 := FND_API.G_FALSE
3116    ,p_commit                 IN    VARCHAR2 := FND_API.G_FALSE
3117    ,p_validation_level       IN    NUMBER   := FND_API.G_VALID_LEVEL_FULL
3118    ,p_price_adj_rec          IN    ozf_resale_adjustments_all%rowtype
3119    ,p_act_budgets_rec        IN    ozf_actbudgets_pvt.act_budgets_rec_type
3120    ,p_act_util_rec           IN    ozf_actbudgets_pvt.act_util_rec_type
3121    ,p_to_create_utilization  IN BOOLEAN
3122    ,x_return_status          OUT NOCOPY VARCHAR2
3123    ,x_msg_data               OUT NOCOPY   VARCHAR2
3124    ,x_msg_count              OUT NOCOPY   NUMBER
3125    ,x_utilization_id         OUT NOCOPY   NUMBER
3126 )IS
3127 l_use_fund_staging_tables VARCHAR2(1) := 'F';
3128 lx_ozf_act_budgets_tbl   OZF_UTILITY_PVT.ozf_act_budgets_table;
3129 lx_ozf_funds_old_rectype OZF_UTILITY_PVT.ozf_funds_all_b_rectype;
3130 lx_ozf_funds_new_tbl OZF_UTILITY_PVT.ozf_funds_table;
3131 l_batch_type VARCHAR2(30);
3132 BEGIN
3133 Create_Adj_And_Utilization(
3134     p_api_version            =>    p_api_version
3135    ,p_init_msg_LIST          =>    p_init_msg_LIST
3136    ,p_commit                 =>    p_commit
3137    ,p_validation_level       =>    p_validation_level
3138    ,p_price_adj_rec          =>    p_price_adj_rec
3139    ,p_act_budgets_rec        =>    p_act_budgets_rec
3140    ,p_act_util_rec           =>    p_act_util_rec
3141    ,p_to_create_utilization  =>    p_to_create_utilization
3142    ,x_return_status          =>    x_return_status
3143    ,x_msg_data               =>    x_msg_data
3144    ,x_msg_count              =>    x_msg_count
3145    ,x_utilization_id         =>    x_utilization_id   ---ER 13333298
3146    ,p_batch_type             =>    l_batch_type
3147    ,p_use_fund_staging_tables =>   l_use_fund_staging_tables
3148    ,px_ozf_act_budgets_tbl    =>   lx_ozf_act_budgets_tbl
3149    ,px_ozf_funds_old_rectype  =>   lx_ozf_funds_old_rectype
3150    ,px_ozf_funds_new_tbl      =>   lx_ozf_funds_new_tbl
3151 );
3152 END Create_Adj_And_Utilization;
3153 
3154 -- For TPA Parallel Execution ER - 9614703 (-)
3155 
3156 ---------------------------------------------------------------------
3157 -- PROCEDURE
3158 --    Create_Adj_And_Utilization
3159 --
3160 -- PURPOSE
3161 --    This function receives the price adjustment rec AND utilization record
3162 --    It them inserts the price adjustmetns AND utilization
3163 --
3164 -- PARAMETERS
3165 --
3166 -- p_adj_rec IN ozf_chargeback_price_adj_all%rowtype
3167 -- p_act_budgets_rec ozf_actbudgets_pvt.act_budgets_rec_type
3168 -- p_act_util_rec    ozf_actbudgets_pvt.act_util_rec_type;
3169 -- p_use_fund_staging_tables for staging table impln of chargeback flow
3170 -- and px_ozf_act_budgets_tbl for PLSQL table impln of storing budgets value.
3171 -- and px_ozf_funds_old_rectype for taking the funds old values
3172 -- and px_ozf_funds_new_rectype for taking the new values after updation.
3173 --
3174 -- NOTES
3175 -- 27/10/2009  muthsubr Added px_ozf_act_budgets_tbl, p_use_fund_staging_tables
3176 --                      px_ozf_funds_old_rectype, px_ozf_funds_new_rectype
3177 --                      for bug#8867381.
3178 -- 29/11/2010  muthsubr Altered px_ozf_funds_new_rectype to px_ozf_funds_new_tbl
3179 --                      for bug#9614703 for JMS ER.
3180 ---------------------------------------------------------------------
3181 PROCEDURE Create_Adj_And_Utilization(
3182     p_api_version		IN    NUMBER
3183    ,p_init_msg_LIST		IN    VARCHAR2 := FND_API.G_FALSE
3184    ,p_commit			IN    VARCHAR2 := FND_API.G_FALSE
3185    ,p_validation_level		IN    NUMBER   := FND_API.G_VALID_LEVEL_FULL
3186    ,p_price_adj_rec		IN    ozf_resale_adjustments_all%rowtype
3187    ,p_act_budgets_rec		IN    ozf_actbudgets_pvt.act_budgets_rec_type
3188    ,p_act_util_rec		IN    ozf_actbudgets_pvt.act_util_rec_type
3189    ,p_to_create_utilization	IN BOOLEAN
3190    ,x_return_status		OUT NOCOPY VARCHAR2
3191    ,x_msg_data			OUT NOCOPY   VARCHAR2
3192    ,x_msg_count			OUT NOCOPY   NUMBER
3193    ,x_utilization_id		OUT NOCOPY   NUMBER
3194    ,p_batch_type		IN  VARCHAR2
3195    ,p_use_fund_staging_tables	IN VARCHAR2
3196    ,px_ozf_act_budgets_tbl	IN OUT  NOCOPY  OZF_UTILITY_PVT.ozf_act_budgets_table
3197    ,px_ozf_funds_old_rectype	IN      OZF_UTILITY_PVT.ozf_funds_all_b_rectype
3198    ,px_ozf_funds_new_tbl	IN OUT  NOCOPY  OZF_UTILITY_PVT.ozf_funds_table
3199 )IS
3200 l_api_name          CONSTANT VARCHAR2(30) := 'Create_Adj_and_Utilization';
3201 l_api_version       CONSTANT NUMBER := 1.0;
3202 l_full_name         CONSTANT VARCHAR2(60) := G_PKG_NAME ||'.'|| l_api_name;
3203 --
3204 l_act_budget_id NUMBER;
3205 l_msg_data VARCHAR2(2000);
3206 l_msg_count NUMBER;
3207 l_return_status VARCHAR2(30);
3208 
3209 l_adjustment_id  NUMBER:= p_price_adj_rec.resale_adjustment_id;
3210 
3211 l_obj_ver_num NUMBER := 1;
3212 l_org_id NUMBER;
3213 
3214 l_utilized_amount NUMBER;
3215 l_utilization_id  NUMBER;
3216 BEGIN
3217    -- Standard BEGIN of API savepoint
3218    SAVEPOINT  Create_Adj_And_Utilization;
3219    -- Standard call to check for call compatibility.
3220    IF NOT FND_API.Compatible_API_Call (
3221       l_api_version,
3222       p_api_version,
3223       l_api_name,
3224       G_PKG_NAME)
3225    THEN
3226       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3227    END IF;
3228 
3229    --Initialize message LIST IF p_init_msg_LIST IS TRUE.
3230    IF FND_API.To_Boolean (p_init_msg_LIST) THEN
3231      FND_MSG_PUB.initialize;
3232    END IF;
3233 
3234    -- Debug Message
3235    IF OZF_DEBUG_HIGH_ON THEN
3236       OZF_UTILITY_PVT.debug_message(l_full_name||': Start');
3237    END IF;
3238 
3239    -- Initialize API return status to sucess
3240    x_return_status := FND_API.G_RET_STS_SUCCESS;
3241 
3242    IF OZF_DEBUG_LOW_ON THEN
3243       OZF_UTILITY_PVT.debug_message('adj_id:' || p_price_adj_rec.resale_adjustment_id ||'line_id:' || p_price_adj_rec.resale_line_id);
3244       IF p_to_create_utilization THEN
3245          OZF_UTILITY_PVT.debug_message('create utilization:T');
3246       ELSE
3247          OZF_UTILITY_PVT.debug_message('create utilization:F');
3248       END IF;
3249    END IF;
3250 
3251    OZF_UTILITY_PVT.debug_message('Create_Adj_And_Utilization mg '||p_act_util_rec.plan_currency_code);
3252 
3253    -- get price_adj_id
3254    IF l_adjustment_id IS NULL THEN
3255       OPEN g_adjustment_id_csr;
3256       FETCH g_adjustment_id_csr INTO l_adjustment_id;
3257       CLOSE g_adjustment_id_csr;
3258    END IF;
3259 
3260    IF p_price_adj_rec.org_id IS NOT NULL THEN
3261       l_org_id := p_price_adj_rec.org_id;
3262    ELSE
3263       l_org_id := MO_GLOBAL.get_current_org_id();
3264    END IF;
3265 
3266    -- We only need to record for every price adjustment information
3267    BEGIN
3268       OZF_RESALE_ADJUSTMENTS_PKG.Insert_Row(
3269          px_resale_adjustment_id       => l_adjustment_id,
3270          px_object_version_number      => l_obj_ver_num,
3271          p_last_update_date            => sysdate,
3272          p_last_updated_by             => NVL(FND_GLOBAL.user_id,-1),
3273          p_creation_date               => sysdate,
3274          p_request_id                  => FND_GLOBAL.CONC_REQUEST_ID,
3275          p_created_by                  => NVL(FND_GLOBAL.user_id,-1),
3276          p_created_from                => p_price_adj_rec.created_from,
3277          p_last_update_login           => NVL(FND_GLOBAL.conc_login_id,-1),
3278          p_program_application_id      => FND_GLOBAL.PROG_APPL_ID,
3279          p_program_update_date         => sysdate,
3280          p_program_id                  => FND_GLOBAL.CONC_PROGRAM_ID,
3281          p_resale_line_id              => p_price_adj_rec.resale_line_id,
3282          p_resale_batch_id             => p_price_adj_rec.resale_batch_id,
3283          p_orig_system_agreement_uom   => p_price_adj_rec.orig_system_agreement_uom,
3284          p_orig_system_agreement_name  => p_price_adj_rec.orig_system_agreement_name,
3285          p_orig_system_agreement_type  => p_price_adj_rec.orig_system_agreement_type,
3286          p_orig_system_agreement_status=> p_price_adj_rec.orig_system_agreement_status,
3287          p_orig_system_agreement_curr  => p_price_adj_rec.orig_system_agreement_curr,
3288          p_orig_system_agreement_price => p_price_adj_rec.orig_system_agreement_price,
3289          p_orig_system_agreement_quant => p_price_adj_rec.orig_system_agreement_quantity,
3290          p_agreement_id                => p_price_adj_rec.agreement_id  ,
3291          p_agreement_type              => p_price_adj_rec.agreement_type ,
3292          p_agreement_name              => p_price_adj_rec.agreement_name ,
3293          p_agreement_price             => p_price_adj_rec.agreement_price ,
3294          p_agreement_uom_code          => p_price_adj_rec.agreement_uom_code,
3295          p_corrected_agreement_id      => p_price_adj_rec.corrected_agreement_id ,
3296          p_corrected_agreement_name    => p_price_adj_rec.corrected_agreement_name ,
3297          p_credit_code                 => p_price_adj_rec.credit_code,
3298          p_credit_advice_date          => p_price_adj_rec.credit_advice_date,
3299          p_total_allowed_amount        => p_price_adj_rec.total_allowed_amount,
3300          p_allowed_amount              => p_price_adj_rec.allowed_amount,
3301          p_total_accepted_amount       => p_price_adj_rec.total_accepted_amount,
3302          p_accepted_amount             => p_price_adj_rec.accepted_amount,
3303          p_total_claimed_amount        => p_price_adj_rec.total_claimed_amount,
3304          p_claimed_amount              => p_price_adj_rec.claimed_amount,
3305          p_calculated_price            => p_price_adj_rec.calculated_price,
3306          p_acctd_calculated_price      => p_price_adj_rec.acctd_calculated_price,
3307          p_calculated_amount           => p_price_adj_rec.calculated_amount,
3308          p_line_agreement_flag         => p_price_adj_rec.line_agreement_flag,
3309          p_tolerance_flag              => p_price_adj_rec.tolerance_flag,
3310          p_line_tolerance_amount       => p_price_adj_rec.line_tolerance_amount,
3311          p_operand                     => p_price_adj_rec.operand,
3312          p_operand_calculation_code    => p_price_adj_rec.operand_calculation_code,
3313          p_priced_quantity             => p_price_adj_rec.priced_quantity,
3314          p_priced_uom_code             => p_price_adj_rec.priced_uom_code,
3315          p_priced_unit_price           => p_price_adj_rec.priced_unit_price,
3316          p_list_header_id              => p_price_adj_rec.list_header_id,
3317          p_list_line_id                => p_price_adj_rec.list_line_id,
3318          p_status_code                 => 'CLOSED',
3319          px_org_id                     => l_org_id
3320       );
3321          --
3322    EXCEPTION
3323       WHEN OTHERS THEN
3324          IF OZF_UNEXP_ERROR THEN
3325             FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
3326          END IF;
3327          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3328    END;
3329 
3330    -- Create accrual only when its required
3331    IF p_to_create_utilization THEN
3332       IF OZF_DEBUG_LOW_ON THEN
3333          ozf_utility_pvt.debug_message('p_act_util_rec.object_type              = '|| p_act_util_rec.object_type);
3334          ozf_utility_pvt.debug_message('p_act_util_rec.object_id                = '|| p_act_util_rec.object_id);
3335          ozf_utility_pvt.debug_message('p_act_util_rec.product_level_type       = '|| p_act_util_rec.product_level_type);
3336          ozf_utility_pvt.debug_message('p_act_util_rec.product_id               = '|| p_act_util_rec.product_id);
3337          ozf_utility_pvt.debug_message('p_act_util_rec.billto_cust_account_id   = '|| p_act_util_rec.billto_cust_account_id);
3338          ozf_utility_pvt.debug_message('p_act_util_rec.gl_date                  = '|| p_act_util_rec.gl_date);
3339          ozf_utility_pvt.debug_message('p_act_util_rec.reference_type           = '|| p_act_util_rec.reference_type);
3340          ozf_utility_pvt.debug_message('p_act_util_rec.reference_id             = '|| p_act_util_rec.reference_id);
3341          ozf_utility_pvt.debug_message('p_act_util_rec.price_adjustment_id      = '|| p_act_util_rec.price_adjustment_id);
3342          ozf_utility_pvt.debug_message('p_act_util_rec.utilization_type         = '|| p_act_util_rec.utilization_type);
3343          ozf_utility_pvt.debug_message('p_act_util_rec.adjustment_type_id       = '|| p_act_util_rec.adjustment_type_id);
3344          ozf_utility_pvt.debug_message('p_act_util_rec.org_id                   = '|| p_act_util_rec.org_id);
3345 
3346          ozf_utility_pvt.debug_message('p_act_budgets_rec.act_budget_used_by_id = '|| p_act_budgets_rec.act_budget_used_by_id);
3347          ozf_utility_pvt.debug_message('p_act_budgets_rec.budget_source_id      = '|| p_act_budgets_rec.budget_source_id);
3348          ozf_utility_pvt.debug_message('p_act_budgets_rec.status_code           = '|| p_act_budgets_rec.status_code);
3349          ozf_utility_pvt.debug_message('p_act_budgets_rec.transfer_type         = '|| p_act_budgets_rec.transfer_type);
3350          ozf_utility_pvt.debug_message('p_act_budgets_rec.arc_act_budget_used_by= '|| p_act_budgets_rec.arc_act_budget_used_by);
3351          ozf_utility_pvt.debug_message('p_act_budgets_rec.budget_source_type    = '|| p_act_budgets_rec.budget_source_type);
3352          ozf_utility_pvt.debug_message('p_act_budgets_rec.approver_id           = '|| p_act_budgets_rec.approver_id);
3353          ozf_utility_pvt.debug_message('p_act_budgets_rec.requester_id          = '|| p_act_budgets_rec.requester_id);
3354          ozf_utility_pvt.debug_message('p_act_budgets_rec.request_currency      = '|| p_act_budgets_rec.request_currency);
3355          ozf_utility_pvt.debug_message('p_act_budgets_rec.parent_source_id      = '|| p_act_budgets_rec.parent_source_id);
3356          ozf_utility_pvt.debug_message('p_act_budgets_rec.parent_src_curr       = '|| p_act_budgets_rec.parent_src_curr);
3357          ozf_utility_pvt.debug_message('p_act_budgets_rec.justification         = '|| p_act_budgets_rec.justification);
3358          ozf_utility_pvt.debug_message('p_act_budgets_rec.arc_act_budget_used_by= '|| p_act_budgets_rec.arc_act_budget_used_by);
3359          ozf_utility_pvt.debug_message('p_act_budgets_rec.budget_source_type    = '|| p_act_budgets_rec.budget_source_type);
3360          ozf_utility_pvt.debug_message('p_act_budgets_rec.request_currency      = '|| p_act_budgets_rec.request_currency);
3361          ozf_utility_pvt.debug_message('p_act_budgets_rec.request_amount        = '|| p_act_budgets_rec.request_amount);
3362          ozf_utility_pvt.debug_message('p_act_budgets_rec.parent_src_apprvd_amt = '|| p_act_budgets_rec.parent_src_apprvd_amt);
3363       END IF;
3364 
3365       BEGIN
3366          --
3367          -- For Bug#8867381, introduced p_use_fund_staging_tables for staging table impln of chargeback flow
3368          -- and px_ozf_act_budgets_tbl for PLSQL table impln of storing budgets value.
3369          -- and px_ozf_funds_old_rectype for taking the funds old values
3370          -- For JMS ER Bug#9614703
3371          -- and changed px_ozf_funds_new_rectype to px_ozf_funds_new_tbl.
3372          OZF_FUND_ADJUSTMENT_PVT.Process_Act_Budgets (
3373             x_return_status		=> l_return_status,
3374             x_msg_count			=> l_msg_count,
3375             x_msg_data			=> l_msg_data,
3376             p_act_budgets_rec		=> p_act_budgets_rec,
3377             p_act_util_rec		=> p_act_util_rec,
3378             x_act_budget_id		=> l_act_budget_id,
3379             x_utilized_amount		=> l_utilized_amount,
3380             p_batch_type		=> p_batch_type,
3381             p_use_fund_staging_tables	=> p_use_fund_staging_tables,
3382             px_ozf_act_budgets_tbl	=> px_ozf_act_budgets_tbl,
3383             px_ozf_funds_old_rectype    => px_ozf_funds_old_rectype,
3384             px_ozf_funds_new_tbl	=> px_ozf_funds_new_tbl,  -- For JMS ER Bug#9614703
3385             x_utilization_id		=> l_utilization_id
3386          );
3387 
3388 	 --ER 13333298
3389 	 x_utilization_id := l_utilization_id;
3390 
3391 	 IF OZF_DEBUG_LOW_ON THEN
3392             OZF_UTILITY_PVT.debug_message('OZF_FUND_ADJUSTMENT_PVT.Process_Act_Budgets return result: '||l_return_status);
3393 	     OZF_UTILITY_PVT.debug_message('l_utilization_id '|| l_utilization_id);
3394             OZF_UTILITY_PVT.debug_message('post to budget: budget_source_id:' || p_act_budgets_rec.budget_source_id);
3395             OZF_UTILITY_PVT.debug_message('post to budget: amount:' || p_act_budgets_rec.request_amount);
3396             OZF_UTILITY_PVT.debug_message('x_utilized_amount: '||l_utilized_amount);
3397          END IF;
3398 
3399          IF l_return_status = FND_API.g_ret_sts_error THEN
3400             RAISE FND_API.g_exc_error;
3401          ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
3402             RAISE FND_API.g_exc_unexpected_error;
3403          END IF;
3404          -- bug 5391758,5216124
3405          IF l_utilized_amount = 0 AND p_act_budgets_rec.request_amount <> 0 THEN
3406             ozf_utility_pvt.error_message ( 'OZF_COMMAMT_LESS_REQAMT');
3407             RAISE fnd_api.g_exc_error;
3408          END IF;
3409 
3410       EXCEPTION
3411          WHEN OTHERS THEN
3412             IF OZF_UNEXP_ERROR THEN
3413                FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
3414             END IF;
3415             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3416       END;
3417    END IF;
3418 
3419    -- Debug Message
3420    IF OZF_DEBUG_HIGH_ON THEN
3421       OZF_UTILITY_PVT.debug_message(l_full_name||': End');
3422    END IF;
3423 
3424    --Standard call to get message count AND IF count=1, get the message
3425    FND_MSG_PUB.Count_And_Get (
3426        p_encoded => FND_API.G_FALSE,
3427        p_count => x_msg_count,
3428        p_data  => x_msg_data
3429    );
3430 EXCEPTION
3431    WHEN FND_API.G_EXC_ERROR THEN
3432       ROLLBACK TO Create_Adj_And_Utilization;
3433       x_return_status := FND_API.G_RET_STS_ERROR;
3434       -- Standard call to get message count AND IF count=1, get the message
3435       FND_MSG_PUB.Count_And_Get (
3436           p_encoded => FND_API.G_FALSE,
3437           p_count => x_msg_count,
3438           p_data  => x_msg_data
3439       );
3440    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
3441       ROLLBACK TO Create_Adj_And_Utilization;
3442       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3443       -- Standard call to get message count AND IF count=1, get the message
3444       FND_MSG_PUB.Count_And_Get (
3445           p_encoded => FND_API.G_FALSE,
3446           p_count => x_msg_count,
3447           p_data  => x_msg_data
3448       );
3449    WHEN OTHERS THEN
3450       ROLLBACK TO Create_Adj_And_Utilization;
3451       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
3452       IF OZF_UNEXP_ERROR THEN
3453          FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
3454       END IF;
3455       -- Standard call to get message count AND IF count=1, get the message
3456       FND_MSG_PUB.Count_And_Get (
3457           p_encoded => FND_API.G_FALSE,
3458           p_count => x_msg_count,
3459           p_data  => x_msg_data
3460       );
3461 END Create_Adj_And_Utilization;
3462 
3463 -- For TPA Parallel Execution ER - 9614703 (+)
3464 /*
3465 ---------------------------------------------------------------------
3466 -- PROCEDURE
3467 --    Create_Utilization
3468 --
3469 -- PURPOSE
3470 --    This procedure creates utilization
3471 --
3472 -- PARAMETERS
3473 --
3474 --
3475 -- NOTES
3476 -- Overloaded the procedure Create_Utilization for Bug#8867381
3477 ---------------------------------------------------------------------
3478 PROCEDURE  Create_Utilization(
3479     p_api_version         IN    NUMBER
3480    ,p_init_msg_LIST       IN    VARCHAR2 := FND_API.G_FALSE
3481    ,p_commit              IN    VARCHAR2 := FND_API.G_FALSE
3482    ,p_validation_level    IN    NUMBER   := FND_API.G_VALID_LEVEL_FULL
3483    ,p_line_int_rec        IN  g_interface_rec_csr%ROWTYPE
3484    ,p_fund_id             IN  NUMBER
3485    ,p_line_id             IN  NUMBER
3486    ,p_cust_account_id     IN  NUMBER
3487    ,p_approver_id         IN  NUMBER
3488    ,x_return_status       OUT NOCOPY VARCHAR2
3489    ,x_msg_data               OUT NOCOPY   VARCHAR2
3490    ,x_msg_count              OUT NOCOPY   NUMBER
3491 )
3492 IS
3493 l_fund_int_rec                  ozf_funds_pvt.fund_rec_int_type;
3494 l_use_fund_staging_tables       VARCHAR2(1) := 'F';
3495 lx_ozf_act_budgets_tbl ozf_chargeback_pvt.ozf_act_budgets_table;
3496 lx_ozf_funds_old_rectype OZF_CHARGEBACK_PVT.ozf_funds_all_b_rectype;
3497 lx_ozf_funds_new_rectype OZF_CHARGEBACK_PVT.ozf_funds_all_b_rectype;
3498 BEGIN
3499 Create_Utilization(
3500     p_api_version         =>    p_api_version
3501    ,p_init_msg_LIST       =>    p_init_msg_LIST
3502    ,p_commit              =>    p_commit
3503    ,p_validation_level    =>    p_validation_level
3504    ,p_line_int_rec        =>  p_line_int_rec
3505    ,p_fund_id             =>  p_fund_id
3506    ,p_fund_int_rec        =>  l_fund_int_rec
3507    ,p_use_fund_staging_tables => l_use_fund_staging_tables
3508    ,p_line_id             =>  p_line_id
3509    ,p_cust_account_id     =>  p_cust_account_id
3510    ,p_approver_id         =>  p_approver_id
3511    ,x_return_status       => x_return_status
3512    ,x_msg_data            =>   x_msg_data
3513    ,x_msg_count           =>   x_msg_count
3514    ,px_ozf_act_budgets_tbl =>  lx_ozf_act_budgets_tbl
3515    ,px_ozf_funds_old_rectype    =>  lx_ozf_funds_old_rectype
3516    ,px_ozf_funds_new_rectype    =>  lx_ozf_funds_new_rectype
3517    );
3518 END Create_Utilization;
3519 */
3520 ---------------------------------------------------------------------
3521 -- PROCEDURE
3522 --    Create_Utilization
3523 --
3524 -- PURPOSE
3525 --    This procedure creates utilization
3526 --
3527 -- PARAMETERS
3528 --
3529 --
3530 -- NOTES
3531 -- Overloaded the procedure Create_Utilization for Bug#8867381
3532 -- Altered px_ozf_funds_new_rectype to px_ozf_funds_new_tbl
3533 -- for JMS ER Bug#9614703.
3534 ---------------------------------------------------------------------
3535 PROCEDURE  Create_Utilization(
3536     p_api_version         IN    NUMBER
3537    ,p_init_msg_LIST       IN    VARCHAR2 := FND_API.G_FALSE
3538    ,p_commit              IN    VARCHAR2 := FND_API.G_FALSE
3539    ,p_validation_level    IN    NUMBER   := FND_API.G_VALID_LEVEL_FULL
3540    ,p_line_int_rec        IN  g_interface_rec_csr%ROWTYPE
3541    ,p_fund_id             IN  NUMBER
3542    ,p_line_id             IN  NUMBER
3543    ,p_cust_account_id     IN  NUMBER
3544    ,p_approver_id         IN  NUMBER
3545    ,x_return_status       OUT NOCOPY VARCHAR2
3546    ,x_msg_data            OUT NOCOPY   VARCHAR2
3547    ,x_msg_count           OUT NOCOPY   NUMBER
3548 )
3549 IS
3550 l_fund_int_rec                  ozf_funds_pvt.fund_rec_int_type;
3551 l_use_fund_staging_tables       VARCHAR2(1) := 'F';
3552 lx_ozf_act_budgets_tbl OZF_UTILITY_PVT.ozf_act_budgets_table;
3553 lx_ozf_funds_old_rectype OZF_UTILITY_PVT.ozf_funds_all_b_rectype;
3554 lx_ozf_funds_new_tbl OZF_UTILITY_PVT.ozf_funds_table;
3555 l_batch_type VARCHAR2(30);
3556 BEGIN
3557 Create_Utilization(
3558     p_api_version         =>    p_api_version
3559    ,p_init_msg_LIST       =>    p_init_msg_LIST
3560    ,p_commit              =>    p_commit
3561    ,p_validation_level    =>    p_validation_level
3562    ,p_line_int_rec        =>  p_line_int_rec
3563    ,p_fund_id             =>  p_fund_id
3564    ,p_fund_int_rec        =>  l_fund_int_rec
3565    ,p_use_fund_staging_tables => l_use_fund_staging_tables
3566    ,p_line_id             =>  p_line_id
3567    ,p_cust_account_id     =>  p_cust_account_id
3568    ,p_approver_id         =>  p_approver_id
3569    ,x_return_status       => x_return_status
3570    ,x_msg_data            =>   x_msg_data
3571    ,x_msg_count           =>   x_msg_count
3572    ,p_batch_type             =>    l_batch_type
3573    ,px_ozf_act_budgets_tbl =>  lx_ozf_act_budgets_tbl
3574    ,px_ozf_funds_old_rectype    =>  lx_ozf_funds_old_rectype
3575    ,px_ozf_funds_new_tbl    =>  lx_ozf_funds_new_tbl
3576    );
3577 END Create_Utilization;
3578 -- For TPA Parallel Execution ER - 9614703 (-)
3579 
3580 ---------------------------------------------------------------------
3581 -- PROCEDURE
3582 --    Create_Utilization
3583 --
3584 -- PURPOSE
3585 --    This procedure creates utilization
3586 --
3587 -- PARAMETERS
3588 -- p_use_fund_staging_tables for staging table impln of chargeback flow
3589 -- and px_ozf_act_budgets_tbl for PLSQL table impln of storing budgets value.
3590 -- and px_ozf_funds_old_rectype for taking the funds old values
3591 -- and px_ozf_funds_new_rectype for taking the new values after updation.
3592 
3593 --
3594 -- NOTES
3595 -- 27/10/2009  muthsubr Added px_ozf_act_budgets_tbl, p_use_fund_staging_tables
3596 --                      px_ozf_funds_old_rectype, px_ozf_funds_new_rectype
3597 --                      for bug#8867381.
3598 -- 29/11/2010  muthsubr Altered px_ozf_funds_new_rectype to px_ozf_funds_new_tbl
3599 --                      for JMS ER Bug#9614703.
3600 ---------------------------------------------------------------------
3601 PROCEDURE  Create_Utilization(
3602     p_api_version         IN    NUMBER
3603    ,p_init_msg_LIST       IN    VARCHAR2 := FND_API.G_FALSE
3604    ,p_commit              IN    VARCHAR2 := FND_API.G_FALSE
3605    ,p_validation_level    IN    NUMBER   := FND_API.G_VALID_LEVEL_FULL
3606    ,p_line_int_rec        IN  g_interface_rec_csr%ROWTYPE
3607    ,p_fund_id             IN  NUMBER
3608    ,p_fund_int_rec        IN  ozf_funds_pvt.fund_rec_int_type
3609    ,p_use_fund_staging_tables IN VARCHAR2
3610    ,p_line_id             IN  NUMBER
3611    ,p_cust_account_id     IN  NUMBER
3612    ,p_approver_id         IN  NUMBER
3613    ,x_return_status       OUT NOCOPY VARCHAR2
3614    ,x_msg_data            OUT NOCOPY   VARCHAR2
3615    ,x_msg_count           OUT NOCOPY   NUMBER
3616    ,p_batch_type             IN  VARCHAR2
3617    ,px_ozf_act_budgets_tbl      IN OUT NOCOPY OZF_UTILITY_PVT.ozf_act_budgets_table
3618    ,px_ozf_funds_old_rectype    IN      OZF_UTILITY_PVT.ozf_funds_all_b_rectype
3619    ,px_ozf_funds_new_tbl    IN OUT NOCOPY   OZF_UTILITY_PVT.ozf_funds_table
3620 )
3621 IS
3622 l_api_name          CONSTANT VARCHAR2(30) := 'Create_Utilization';
3623 l_api_version       CONSTANT NUMBER := 1.0;
3624 l_full_name         CONSTANT VARCHAR2(60) := G_PKG_NAME ||'.'|| l_api_name;
3625 --
3626 l_return_status     VARCHAR2(30);
3627 l_msg_data          VARCHAR2(2000);
3628 l_msg_count         NUMBER;
3629 --
3630 l_dup_adjustment_id NUMBER;
3631 l_dup_total_accepted_amount NUMBER;
3632 l_line_int_rec OZF_RESALE_COMMON_PVT.g_interface_rec_csr%ROWTYPE := p_line_int_rec;
3633 
3634 CURSOR dup_adj_csr(p_line_id in NUMBER, p_batch_type in VARCHAR2) IS
3635 SELECT a.resale_adjustment_id,
3636        a.total_accepted_amount
3637   FROM ozf_resale_adjustments a,
3638        ozf_resale_batches b,
3639        ozf_resale_lines c
3640  WHERE a.resale_line_id = p_line_id
3641    AND a.resale_batch_id = b.resale_batch_id
3642    AND b.batch_type = p_batch_type
3643    AND b.status_code = OZF_RESALE_COMMON_PVT.G_BATCH_CLOSED
3644    AND c.resale_line_id = a.resale_line_id
3645    AND c.status_code = OZF_RESALE_COMMON_PVT.G_BATCH_ADJ_PROCESSED
3646    -- BUG 4670154 (+)
3647    AND a.list_header_id IS NULL
3648    AND a.list_line_id IS NULL;
3649    -- BUG 4670154 (-)
3650 
3651 CURSOR dup_adj_rec_csr(p_adj_id NUMBER) IS
3652 SELECT *
3653   FROM ozf_resale_adjustments
3654  WHERE resale_adjustment_id = p_adj_id;
3655 
3656 l_dup_adj_rec dup_adj_rec_csr%ROWTYPE;
3657 
3658 l_batch_type  VARCHAR2(30);
3659 l_utilization_type VARCHAR2(30);
3660 l_adjustment_type_id  NUMBER := NULL;
3661 l_budget_source_type  VARCHAR2(30);
3662 l_justification       VARCHAR2(250);
3663 l_to_create_utilization  BOOLEAN;
3664 l_budget_source_id  NUMBER;
3665 -- POS Batch Processing by profiles by ateotia (+)
3666    l_spr_ship_from_stock_flag      VARCHAR2(1);
3667    l_spr_offer_type                VARCHAR2(30);
3668    l_spr_offer_id                  NUMBER;
3669    CURSOR request_header_info_csr(p_agreement_num VARCHAR2,
3670                                   p_resale_batch_id NUMBER) IS
3671    SELECT
3672      orha.ship_from_stock_flag,
3673      orha.offer_type,
3674      qlha.list_header_id
3675    FROM
3676      ozf_request_headers_all_vl orha,
3677      ozf_resale_batches_all orba,
3678      qp_list_headers_all qlha
3679    WHERE
3680      orha.agreement_number = p_agreement_num
3681      AND orha.status_code = 'APPROVED'
3682      AND orha.request_class = 'SPECIAL_PRICE'
3683      AND orha.partner_id = orba.partner_id
3684      AND orba.resale_batch_id = p_resale_batch_id
3685      AND orha.authorization_code = qlha.name;
3686    -- POS Batch Processing by profiles by ateotia (-)
3687 
3688 --
3689 BEGIN
3690    -- Standard BEGIN of API savepoint
3691    SAVEPOINT  IDSM_Create_Utilization;
3692    -- Standard call to check for call compatibility.
3693    IF NOT FND_API.Compatible_API_Call (
3694       l_api_version,
3695       p_api_version,
3696       l_api_name,
3697       G_PKG_NAME)
3698    THEN
3699       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3700    END IF;
3701 
3702    --Initialize message LIST IF p_init_msg_LIST IS TRUE.
3703    IF FND_API.To_Boolean (p_init_msg_LIST) THEN
3704      FND_MSG_PUB.initialize;
3705    END IF;
3706 
3707    -- Debug Message
3708    IF OZF_DEBUG_HIGH_ON THEN
3709       OZF_UTILITY_PVT.debug_message(l_full_name||': Start');
3710    END IF;
3711 
3712    -- Initialize API return status to sucess
3713    x_return_status := FND_API.G_RET_STS_SUCCESS;
3714 
3715    IF OZF_DEBUG_LOW_ON THEN
3716       OZF_UTILITY_PVT.debug_message('IN create_utilization');
3717    END IF;
3718 
3719    OPEN g_batch_type_csr(l_line_int_rec.resale_batch_id);
3720    FETCH g_batch_type_csr into l_batch_type;
3721    CLOSE g_batch_type_csr;
3722 
3723    IF OZF_DEBUG_LOW_ON THEN
3724       OZF_UTILITY_PVT.debug_message('batch type' ||l_batch_type);
3725    END IF;
3726 
3727    IF l_batch_type = G_CHARGEBACK THEN
3728       l_utilization_type:= G_CHBK_UTIL_TYPE;
3729       l_adjustment_type_id := G_CHBK_ADJ_TYPE_id;
3730       l_budget_source_type := 'PRIC';
3731       l_justification      := 'CHARGEBACK';
3732       l_to_create_utilization := true;
3733       IF l_line_int_rec.corrected_agreement_id IS NOT NULL THEN
3734          l_budget_source_id := l_line_int_rec.corrected_agreement_id;
3735       ELSE
3736          l_budget_source_id := l_line_int_rec.agreement_id;
3737       END IF;
3738 
3739    ELSIF l_batch_type = G_SPECIAL_PRICING THEN
3740       IF OZF_DEBUG_LOW_ON THEN
3741          OZF_UTILITY_PVT.debug_message('In batch type as spp.');
3742       END IF;
3743         --POS Batch Processing by profiles by ateotia (+)
3744          OPEN request_header_info_csr(l_line_int_rec.agreement_name, l_line_int_rec.resale_batch_id);
3745          FETCH request_header_info_csr INTO l_spr_ship_from_stock_flag,
3746                                             l_spr_offer_type,
3747                                             l_spr_offer_id;
3748          CLOSE request_header_info_csr;
3749          IF (l_spr_ship_from_stock_flag = 'Y' AND l_spr_offer_type = 'ACCRUAL') THEN
3750                     l_to_create_utilization  := true;
3751                     l_budget_source_id       := l_spr_offer_id;
3752                     l_budget_source_type     := 'OFFR';
3753                     l_utilization_type       := 'ACCRUAL';
3754                     -- l_justification for populating ozf_funds_utilized_all_tl.adjustment_desc
3755                     l_justification          := ' Special Pricing Ship From Stock Accrual';
3756          ELSE
3757 
3758               l_utilization_type:= G_SPP_UTIL_TYPE;
3759               l_budget_source_type := 'OFFR';
3760               l_justification      := 'SPECIAL PRICE';
3761               l_to_create_utilization  := false;  --???
3762               IF l_line_int_rec.corrected_agreement_id IS NOT NULL THEN
3763                  l_budget_source_id := l_line_int_rec.corrected_agreement_id;
3764               ELSE
3765                  l_budget_source_id := l_line_int_rec.agreement_id;
3766        END IF;
3767       END IF;
3768           --POS Batch Processing by profiles by ateotia (+)
3769 
3770 /*
3771    ELSIF l_batch_type = G_TP_ACCRUAL THEN
3772       -- Third party acrrual run from inter face.
3773       l_utilization_type:= G_TP_ACCRUAL_UTIL_TYPE;
3774       l_budget_source_id := l_line_int_rec.price_list_id;
3775       l_adjustment_type_id := G_CHBK_ADJ_TYPE_id;
3776       l_justification      := 'THIRD PARTY PRICE DIFF';
3777 */
3778    END IF;
3779 
3780    IF OZF_DEBUG_LOW_ON THEN
3781       OZF_UTILITY_PVT.debug_message('dup line id' ||l_line_int_rec.duplicated_line_id);
3782       OZF_UTILITY_PVT.debug_message('dup_accepted_amount:'||l_dup_total_accepted_amount);
3783       OZF_UTILITY_PVT.debug_message('total_accepted_amount:'||l_line_int_rec.total_accepted_amount);
3784    END IF;
3785 
3786    IF l_line_int_rec.duplicated_line_id IS NOT NULL THEN
3787       IF l_line_int_rec.duplicated_adjustment_id = -1 THEN
3788          -- Create utilization using int rec
3789 
3790          -- For Bug#8867381, introduced p_use_fund_staging_tables for staging table impln of chargeback flow
3791          -- and px_ozf_act_budgets_tbl for PLSQL table impln of storing budgets value.
3792          -- and px_ozf_funds_old_rectype for taking the funds old values
3793          -- For JMS ER Bug#9614703
3794          -- and changed px_ozf_funds_new_rectype to px_ozf_funds_new_tbl.
3795          Create_Utilization_record(
3796              p_line_int_rec  => l_line_int_rec
3797             ,p_batch_type    => p_batch_type
3798             ,p_fund_id       => p_fund_id
3799             ,p_line_id       => p_line_id
3800             ,p_cust_account_id => p_cust_account_id
3801             ,p_approver_id   => p_approver_id
3802             ,p_line_agreement_flag => 'T'
3803             ,p_utilization_type => l_utilization_type
3804             ,p_adjustment_type_id   => l_adjustment_type_id
3805             ,p_budget_source_type  => l_budget_source_type
3806             ,p_budget_source_id     => l_budget_source_id
3807             ,p_justification       => l_justification
3808             ,p_to_create_utilization =>l_to_create_utilization
3809             ,x_return_status   => l_return_status
3810             ,p_use_fund_staging_tables => p_use_fund_staging_tables
3811             ,px_ozf_act_budgets_tbl => px_ozf_act_budgets_tbl
3812             ,px_ozf_funds_old_rectype => px_ozf_funds_old_rectype
3813             ,px_ozf_funds_new_tbl => px_ozf_funds_new_tbl       -- JMS ER Bug#9614703
3814          );
3815          IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3816             RAISE FND_API.G_EXC_ERROR;
3817          ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3818             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3819          END IF;
3820       ELSE
3821          -- Go check whether there IS a need to create a reverse utilization
3822          OPEN dup_adj_csr (l_line_int_rec.duplicated_line_id, l_batch_type);
3823          FETCH dup_adj_csr INTO l_dup_adjustment_id, l_dup_total_accepted_amount;
3824          CLOSE dup_adj_csr;
3825 
3826          IF OZF_DEBUG_LOW_ON THEN
3827             OZF_UTILITY_PVT.debug_message('dup_adj_crs:: l_dup_adjustment_id = ' ||l_dup_adjustment_id);
3828             OZF_UTILITY_PVT.debug_message('dup_adj_crs:: l_dup_total_accepted_amount = '||l_dup_total_accepted_amount);
3829          END IF;
3830 
3831          -- Here the claimed_amount should NOT equal to the current claimed amount
3832          IF l_dup_total_accepted_amount IS NULL OR -- bug 5222273
3833             l_dup_total_accepted_amount <> l_line_int_rec.total_accepted_amount THEN
3834             -- AND the create one FOR the current int rec.
3835             -- creat utilization using int rec
3836 
3837             -- For Bug#8867381, introduced p_use_fund_staging_tables for staging table impln of chargeback flow
3838             -- and px_ozf_act_budgets_tbl for PLSQL table impln of storing budgets value.
3839             -- and px_ozf_funds_old_rectype for taking the funds old values
3840             -- For JMS ER Bug#9614703
3841             -- and changed px_ozf_funds_new_rectype to px_ozf_funds_new_tbl.
3842             Create_Utilization_record(
3843                 p_line_int_rec  => l_line_int_rec
3844                ,p_batch_type    => p_batch_type
3845                ,p_fund_id       => p_fund_id
3846                ,p_line_id       => p_line_id
3847                ,p_cust_account_id => p_cust_account_id
3848                ,p_approver_id         => p_approver_id
3849                ,p_line_agreement_flag => 'T'
3850                ,p_utilization_type    => l_utilization_type
3851                ,p_adjustment_type_id  => l_adjustment_type_id
3852                ,p_budget_source_type  => l_budget_source_type
3853                ,p_budget_source_id     => l_budget_source_id
3854                ,p_justification       => l_justification
3855                ,p_to_create_utilization =>l_to_create_utilization
3856                ,x_return_status   => l_return_status
3857                ,p_use_fund_staging_tables => p_use_fund_staging_tables
3858                ,px_ozf_act_budgets_tbl => px_ozf_act_budgets_tbl
3859                ,px_ozf_funds_old_rectype        =>      px_ozf_funds_old_rectype
3860                ,px_ozf_funds_new_tbl => px_ozf_funds_new_tbl    -- JMS ER Bug#9614703
3861             );
3862             IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3863                RAISE FND_API.G_EXC_ERROR;
3864             ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3865                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3866             END IF;
3867 
3868             -- We need to reverse the old utilization
3869             -- Here I need to repopulate the adj related columns.
3870             OPEN dup_adj_rec_csr(l_dup_adjustment_id);
3871             FETCH dup_adj_rec_csr INTO l_dup_adj_rec;
3872             CLOSE dup_adj_rec_csr;
3873             l_line_int_rec.orig_system_agreement_uom     := l_dup_adj_rec.orig_system_agreement_uom;
3874             l_line_int_rec.orig_system_agreement_name    := l_dup_adj_rec.orig_system_agreement_name;
3875             l_line_int_rec.orig_system_agreement_type    := l_dup_adj_rec.orig_system_agreement_type;
3876             l_line_int_rec.orig_system_agreement_status  := l_dup_adj_rec.orig_system_agreement_status;
3877             l_line_int_rec.orig_system_agreement_curr    := l_dup_adj_rec.orig_system_agreement_curr;
3878             l_line_int_rec.orig_system_agreement_price   := l_dup_adj_rec.orig_system_agreement_price;
3879             l_line_int_rec.orig_system_agreement_quantity:= l_dup_adj_rec.orig_system_agreement_quantity;
3880             l_line_int_rec.agreement_id                  := l_dup_adj_rec.agreement_id;
3881             l_line_int_rec.agreement_type                := l_dup_adj_rec.agreement_type;
3882             l_line_int_rec.agreement_name                := l_dup_adj_rec.agreement_name;
3883             l_line_int_rec.agreement_price               := l_dup_adj_rec.agreement_price;
3884             l_line_int_rec.agreement_uom_code            := l_dup_adj_rec.agreement_uom_code;
3885             l_line_int_rec.corrected_agreement_id        := l_dup_adj_rec.corrected_agreement_id;
3886             l_line_int_rec.corrected_agreement_name      := l_dup_adj_rec.corrected_agreement_name;
3887             l_line_int_rec.credit_code                   := l_dup_adj_rec.credit_code;
3888             l_line_int_rec.credit_advice_date            := l_dup_adj_rec.credit_advice_date;
3889             l_line_int_rec.claimed_amount                := l_dup_adj_rec.claimed_amount;
3890             l_line_int_rec.total_claimed_amount          := l_dup_adj_rec.total_claimed_amount;
3891             l_line_int_rec.allowed_amount                := l_dup_adj_rec.allowed_amount;
3892             l_line_int_rec.total_allowed_amount          := l_dup_adj_rec.total_allowed_amount;
3893             l_line_int_rec.accepted_amount               := -1 * l_dup_adj_rec.accepted_amount;
3894             l_line_int_rec.total_accepted_amount         := -1 * l_dup_adj_rec.total_accepted_amount;
3895             l_line_int_rec.calculated_price              := l_dup_adj_rec.calculated_price;
3896             l_line_int_rec.acctd_calculated_price        := l_dup_adj_rec.acctd_calculated_price;
3897             l_line_int_rec.calculated_amount             := l_line_int_rec.calculated_amount;
3898             l_line_int_rec.tolerance_flag                := l_dup_adj_rec.tolerance_flag;
3899             l_line_int_rec.line_tolerance_amount         := l_dup_adj_rec.line_tolerance_amount;
3900             l_line_int_rec.quantity                      := l_dup_adj_rec.priced_quantity;
3901             l_line_int_rec.uom_code                      := l_dup_adj_rec.priced_uom_code;
3902             l_line_int_rec.calculated_price              := l_dup_adj_rec.priced_unit_price;
3903 
3904 
3905             -- For Bug#8867381, introduced p_use_fund_staging_tables for staging table impln of chargeback flow
3906             -- and px_ozf_act_budgets_tbl for PLSQL table impln of storing budgets value.
3907             -- and px_ozf_funds_old_rectype for taking the funds old values
3908             -- For JMS ER Bug#9614703
3909             -- and changed px_ozf_funds_new_rectype to px_ozf_funds_new_tbl.
3910             Create_Utilization_record(
3911                 p_line_int_rec         => l_line_int_rec
3912                ,p_batch_type    => p_batch_type
3913                ,p_fund_id              => p_fund_id
3914                ,p_line_id              => p_line_id
3915                ,p_cust_account_id      => p_cust_account_id
3916                ,p_approver_id          => p_approver_id
3917                ,p_line_agreement_flag  => 'F'
3918                ,p_utilization_type     => l_utilization_type
3919                ,p_adjustment_type_id     => l_adjustment_type_id
3920                ,p_budget_source_type   => l_budget_source_type
3921                ,p_budget_source_id     => l_budget_source_id
3922                ,p_justification        => l_justification
3923                ,p_to_create_utilization =>l_to_create_utilization
3924                ,x_return_status        => l_return_status
3925                ,p_use_fund_staging_tables => p_use_fund_staging_tables
3926                ,px_ozf_act_budgets_tbl => px_ozf_act_budgets_tbl
3927                ,px_ozf_funds_old_rectype       =>      px_ozf_funds_old_rectype
3928                ,px_ozf_funds_new_tbl => px_ozf_funds_new_tbl    -- JMS ER Bug#9614703
3929             );
3930             IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3931                RAISE FND_API.G_EXC_ERROR;
3932             ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3933                RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3934             END IF;
3935          ELSE
3936             -- This IS a duplicate. No need to create utilization
3937             -- SLKRISHN move update to resale common pvt
3938             UPDATE ozf_resale_lines_int_all
3939                SET status_code = OZF_RESALE_COMMON_PVT.G_BATCH_ADJ_DUPLICATED,
3940                    duplicated_line_id = p_line_id,
3941                    duplicated_adjustment_id = l_dup_adjustment_id
3942              WHERE resale_line_int_id = l_line_int_rec.resale_line_int_id;
3943          END IF;
3944       END IF;
3945    ELSE
3946       IF OZF_DEBUG_LOW_ON THEN
3947          OZF_UTILITY_PVT.debug_message('In creat utilization with nothing');
3948       END IF;
3949       -- Create utilization using int rec
3950 
3951         -- For Bug#8867381, introduced p_use_fund_staging_tables for staging table impln of chargeback flow
3952         -- and px_ozf_act_budgets_tbl for PLSQL table impln of storing budgets value.
3953         -- and px_ozf_funds_old_rectype for taking the funds old values
3954         -- For JMS ER Bug#9614703
3955         -- and changed px_ozf_funds_new_rectype to px_ozf_funds_new_tbl.
3956       Create_Utilization_record(
3957           p_line_int_rec  => l_line_int_rec
3958          ,p_batch_type    => p_batch_type
3959          ,p_fund_id       => p_fund_id
3960          ,p_line_id       => p_line_id
3961          ,p_cust_account_id => p_cust_account_id
3962          ,p_approver_id   => p_approver_id
3963          ,p_line_agreement_flag => 'T'
3964          ,p_utilization_type => l_utilization_type
3965          ,p_adjustment_type_id   => l_adjustment_type_id
3966          ,p_budget_source_type  => l_budget_source_type
3967          ,p_budget_source_id     => l_budget_source_id
3968          ,p_justification       => l_justification
3969          ,p_to_create_utilization =>l_to_create_utilization
3970          ,x_return_status   => l_return_status
3971          ,p_use_fund_staging_tables => p_use_fund_staging_tables
3972          ,px_ozf_act_budgets_tbl => px_ozf_act_budgets_tbl
3973          ,px_ozf_funds_old_rectype       =>      px_ozf_funds_old_rectype
3974          ,px_ozf_funds_new_tbl => px_ozf_funds_new_tbl  -- JMS ER Bug#9614703
3975       );
3976       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
3977          RAISE FND_API.G_EXC_ERROR;
3978       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
3979          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
3980       END IF;
3981    END IF;
3982 
3983    -- Debug Message
3984    IF OZF_DEBUG_HIGH_ON THEN
3985       OZF_UTILITY_PVT.debug_message(l_full_name||': End');
3986    END IF;
3987 
3988    --Standard call to get message count AND IF count=1, get the message
3989    FND_MSG_PUB.Count_And_Get (
3990        p_encoded => FND_API.G_FALSE,
3991        p_count => x_msg_count,
3992        p_data  => x_msg_data
3993    );
3994 EXCEPTION
3995    WHEN FND_API.G_EXC_ERROR THEN
3996       ROLLBACK TO IDSM_Create_Utilization;
3997       x_return_status := FND_API.G_RET_STS_ERROR;
3998       -- Standard call to get message count AND IF count=1, get the message
3999       FND_MSG_PUB.Count_And_Get (
4000           p_encoded => FND_API.G_FALSE,
4001           p_count => x_msg_count,
4002           p_data  => x_msg_data
4003       );
4004    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4005       ROLLBACK TO IDSM_Create_Utilization;
4006       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4007       -- Standard call to get message count AND IF count=1, get the message
4008       FND_MSG_PUB.Count_And_Get (
4009           p_encoded => FND_API.G_FALSE,
4010           p_count => x_msg_count,
4011           p_data  => x_msg_data
4012       );
4013    WHEN OTHERS THEN
4014       ROLLBACK TO IDSM_Create_Utilization;
4015       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4016       IF OZF_UNEXP_ERROR THEN
4017          FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
4018       END IF;
4019       -- Standard call to get message count AND IF count=1, get the message
4020       FND_MSG_PUB.Count_And_Get (
4021           p_encoded => FND_API.G_FALSE,
4022           p_count => x_msg_count,
4023           p_data  => x_msg_data
4024       );
4025 END Create_Utilization;
4026 
4027 ---------------------------------------------------------------------
4028 -- PROCEDURE
4029 --    Create_Sales_Transaction
4030 --
4031 -- PURPOSE
4032 --    This procedure inserts a record in ozf sales transaction table
4033 --
4034 -- PARAMETERS
4035 --    p_line_int_rec  IN g_interface_rec_csr%rowtype,
4036 --    x_headerid       out NUMBER
4037 --    x_return_status  out VARCHAR2
4038 --
4039 -- NOTES
4040 --
4041 ---------------------------------------------------------------------
4042 PROCEDURE Create_Sales_Transaction(
4043     p_api_version            IN  NUMBER
4044    ,p_init_msg_list          IN  VARCHAR2 := FND_API.G_FALSE
4045    ,p_commit                 IN  VARCHAR2 := FND_API.G_FALSE
4046    ,p_validation_level       IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL
4047    ,p_line_int_rec           IN  g_interface_rec_csr%rowtype
4048    ,p_header_id              IN  NUMBER
4049    ,p_line_id                IN  NUMBER
4050    ,x_sales_transaction_id   OUT NOCOPY   NUMBER
4051    ,x_return_status          OUT NOCOPY   VARCHAR2
4052    ,x_msg_data               OUT NOCOPY   VARCHAR2
4053    ,x_msg_count              OUT NOCOPY   NUMBER
4054 )
4055 IS
4056 l_api_name          CONSTANT VARCHAR2(30) := 'Create_Sales_Transaction';
4057 l_api_version       CONSTANT NUMBER := 1.0;
4058 l_full_name         CONSTANT VARCHAR2(60) := G_PKG_NAME ||'.'|| l_api_name;
4059 --
4060 l_return_status     VARCHAR2(30);
4061 l_msg_data          VARCHAR2(2000);
4062 l_msg_count         NUMBER;
4063 
4064 l_sales_transaction_id NUMBER;
4065 l_object_version_number NUMBER := 1;
4066 l_org_id    NUMBER;
4067 l_direct_order_flag VARCHAR2(1) := 'N';
4068 
4069 l_sales_transaction_rec OZF_SALES_TRANSACTIONS_PVT.SALES_TRANSACTION_REC_TYPE;
4070 
4071 CURSOR party_id_csr(p_cust_account_id NUMBER) IS
4072 SELECT party_id
4073   FROM hz_cust_accounts
4074  WHERE cust_account_id = p_cust_account_id;
4075 
4076 CURSOR party_site_id_csr(p_account_site_id NUMBER) IS
4077 SELECT party_site_id
4078   FROM hz_cust_acct_sites
4079  WHERE cust_acct_site_id = p_account_site_id;
4080 
4081 -- For Bug#9447673 SSD IDSM ER(+)
4082 CURSOR batch_direct_flag_csr (p_id IN NUMBER) IS
4083 SELECT NVL(direct_order_flag,'N')
4084    FROM ozf_resale_batches_all
4085 WHERE resale_batch_id = p_id;
4086 -- For Bug#9447673 SSD IDSM ER(-)
4087 
4088 BEGIN
4089    -- Standard BEGIN of API savepoint
4090    SAVEPOINT  Create_Sales_Transaction;
4091    -- Standard call to check for call compatibility.
4092    IF NOT FND_API.Compatible_API_Call (
4093          l_api_version,
4094          p_api_version,
4095          l_api_name,
4096          G_PKG_NAME)
4097    THEN
4098       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4099    END IF;
4100 
4101    -- Debug Message
4102    IF OZF_DEBUG_HIGH_ON THEN
4103       OZF_UTILITY_PVT.debug_message(l_full_name||': Start');
4104    END IF;
4105 
4106    --Initialize message LIST IF p_init_msg_LIST IS TRUE.
4107    IF FND_API.To_Boolean (p_init_msg_LIST) THEN
4108       FND_MSG_PUB.initialize;
4109    END IF;
4110 
4111    -- Initialize API return status to sucess
4112    x_return_status := FND_API.G_RET_STS_SUCCESS;
4113 
4114  -- For Bug#9447673 SSD IDSM ER(+)
4115    OPEN batch_direct_flag_csr(p_line_int_rec.resale_batch_id);
4116    FETCH batch_direct_flag_csr INTO l_direct_order_flag;
4117    CLOSE batch_direct_flag_csr;
4118 
4119 
4120    /*
4121    For Bug#9447673 SSD IDSM ER
4122    For Direct orders sold_from_cust_account_id, sold_from_party_id,
4123    sold_from_party_site_id are not needed
4124    */
4125    IF (l_direct_order_flag = 'Y') THEN
4126         l_sales_transaction_rec.sold_from_cust_account_id := NULL;
4127         l_sales_transaction_rec.sold_from_party_id := null;
4128         l_sales_transaction_rec.sold_from_party_site_id := null;
4129    ELSE
4130         l_sales_transaction_rec.sold_from_cust_account_id :=p_line_int_rec.sold_from_cust_account_id;
4131 
4132         OPEN party_id_csr(l_sales_transaction_rec.sold_from_cust_account_id);
4133         FETCH party_id_csr INTO l_sales_transaction_rec.sold_from_party_id;
4134         CLOSE party_id_csr;
4135 
4136         OPEN party_site_id_csr(p_line_int_rec.sold_from_site_id);
4137         FETCH party_site_id_csr INTO l_sales_transaction_rec.sold_from_party_site_id;
4138         CLOSE party_site_id_csr;
4139    END IF;
4140    -- For Bug#9447673 SSD IDSM ER(-)
4141 
4142    l_sales_transaction_rec.sold_to_cust_account_id := p_line_int_rec.bill_to_cust_account_id;
4143    l_sales_transaction_rec.sold_to_party_id        := p_line_int_rec.bill_to_party_id;
4144    l_sales_transaction_rec.sold_to_party_site_id   := p_line_int_rec.bill_to_party_site_id;
4145    l_sales_transaction_rec.bill_to_site_use_id  := p_line_int_rec.bill_to_site_use_id;
4146    l_sales_transaction_rec.ship_to_site_use_id  := p_line_int_rec.ship_to_site_use_id;
4147    l_sales_transaction_rec.transaction_date := p_line_int_rec.date_ordered;
4148 
4149    -- For Bug#9447673 SSD IDSM ER(+)
4150    l_sales_transaction_rec.quantity     := p_line_int_rec.quantity;
4151 
4152    /*
4153    For Bug#9447673 SSD IDSM ER
4154    For Direct orders, source_code = 'OM';
4155    If quantity is +ve,  transfer_type = OUT.
4156    If quantity is -ve,  transfer_type = IN.
4157    */
4158    IF (l_direct_order_flag = 'Y') THEN
4159         l_sales_transaction_rec.source_code  := 'OM';
4160         IF p_line_int_rec.quantity > 0 THEN
4161               l_sales_transaction_rec.transfer_type  := 'IN';
4162         ELSE
4163               l_sales_transaction_rec.transfer_type  := 'OUT';
4164         END IF;
4165    ELSE
4166         l_sales_transaction_rec.source_code  := 'IS';
4167 
4168         IF p_line_int_rec.product_transfer_movement_type = 'TI' THEN
4169               l_sales_transaction_rec.transfer_type    := 'IN';
4170         ELSIF p_line_int_rec.product_transfer_movement_type = 'TO' THEN
4171               l_sales_transaction_rec.transfer_type    := 'OUT';
4172         ELSIF p_line_int_rec.product_transfer_movement_type = 'DC' THEN
4173               l_sales_transaction_rec.transfer_type    := 'OUT';
4174         ELSIF p_line_int_rec.product_transfer_movement_type = 'CD' THEN
4175               l_sales_transaction_rec.transfer_type    := 'IN';
4176         END IF;
4177    END IF;
4178    -- For Bug#9447673 SSD IDSM ER(-)
4179 
4180    l_sales_transaction_rec.uom_code        := p_line_int_rec.uom_code;
4181    l_sales_transaction_rec.amount          := p_line_int_rec.selling_price * p_line_int_rec.quantity;
4182    l_sales_transaction_rec.currency_code   := p_line_int_rec.currency_code;
4183    l_sales_transaction_rec.inventory_item_id := p_line_int_rec.inventory_item_id;
4184    l_sales_transaction_rec.header_id    := p_header_id;
4185    l_sales_transaction_rec.line_id      := p_line_id;
4186    l_sales_transaction_rec.reason_code  := NULL;
4187    -- l_sales_transaction_rec.source_code  := 'IS';     -- For Bug#9447673 SSD IDSM
4188    l_sales_transaction_rec.error_flag  := NULL;
4189 
4190    -- We need to create sales transactions based on these lines.
4191    OZF_SALES_TRANSACTIONS_PVT.Create_Transaction (
4192        p_api_version      => 1.0
4193       ,p_init_msg_list    => FND_API.G_FALSE
4194       ,p_commit           => FND_API.G_FALSE
4195       ,p_validation_level => FND_API.G_VALID_LEVEL_FULL
4196       ,p_transaction_rec  => l_sales_transaction_rec
4197       ,x_sales_transaction_id => l_sales_transaction_id
4198       ,x_return_status    => l_return_status
4199       ,x_msg_data         => l_msg_data
4200       ,x_msg_count        => l_msg_count
4201    );
4202    IF l_return_status = FND_API.G_RET_STS_ERROR THEN
4203       RAISE FND_API.G_EXC_ERROR;
4204    ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
4205       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4206    END IF;
4207    --
4208 
4209    -- Debug Message
4210    IF OZF_DEBUG_HIGH_ON THEN
4211       OZF_UTILITY_PVT.debug_message(l_full_name||': End');
4212    END IF;
4213 
4214    --Standard call to get message count AND IF count=1, get the message
4215    FND_MSG_PUB.Count_And_Get (
4216        p_encoded => FND_API.G_FALSE,
4217        p_count => x_msg_count,
4218        p_data  => x_msg_data
4219    );
4220 EXCEPTION
4221    WHEN FND_API.G_EXC_ERROR THEN
4222       ROLLBACK TO Create_Sales_Transaction;
4223       x_return_status := FND_API.G_RET_STS_ERROR;
4224       -- Standard call to get message count AND IF count=1, get the message
4225       FND_MSG_PUB.Count_And_Get (
4226           p_encoded => FND_API.G_FALSE,
4227           p_count => x_msg_count,
4228           p_data  => x_msg_data
4229       );
4230    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4231       ROLLBACK TO Create_Sales_Transaction;
4232       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4233       -- Standard call to get message count AND IF count=1, get the message
4234       FND_MSG_PUB.Count_And_Get (
4235           p_encoded => FND_API.G_FALSE,
4236           p_count => x_msg_count,
4237           p_data  => x_msg_data
4238       );
4239    WHEN OTHERS THEN
4240       ROLLBACK TO Create_Sales_Transaction;
4241       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4242       IF OZF_UNEXP_ERROR THEN
4243          FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
4244       END IF;
4245       -- Standard call to get message count AND IF count=1, get the message
4246       FND_MSG_PUB.Count_And_Get (
4247           p_encoded => FND_API.G_FALSE,
4248           p_count => x_msg_count,
4249           p_data  => x_msg_data
4250       );
4251 END Create_Sales_Transaction;
4252 
4253 ---------------------------------------------------------------------
4254 -- PROCEDURE
4255 --    Insert_Resale_Header
4256 --
4257 -- PURPOSE
4258 --    This procedure inserts a record in to resale header table
4259 --
4260 -- PARAMETERS
4261 --    p_line_int_rec  IN g_interface_rec_csr%rowtype,
4262 --    x_headerid       OUT NUMBER
4263 --    x_return_status  OUT VARCHAR2
4264 --
4265 -- NOTES
4266 --
4267 ---------------------------------------------------------------------
4268 PROCEDURE Insert_Resale_Header(
4269     p_api_version            IN  NUMBER
4270    ,p_init_msg_LIST          IN  VARCHAR2 := FND_API.G_FALSE
4271    ,p_commit                 IN  VARCHAR2 := FND_API.G_FALSE
4272    ,p_validation_level       IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL
4273    ,p_line_int_rec           IN g_interface_rec_csr%rowtype
4274    ,x_header_id              OUT NOCOPY NUMBER
4275    ,x_return_status          OUT NOCOPY VARCHAR2
4276    ,x_msg_data               OUT NOCOPY   VARCHAR2
4277    ,x_msg_count              OUT NOCOPY   NUMBER
4278 )
4279 IS
4280 l_api_name          CONSTANT VARCHAR2(30) := 'Insert_resale_header';
4281 l_api_version       CONSTANT NUMBER := 1.0;
4282 l_full_name         CONSTANT VARCHAR2(60) := G_PKG_NAME ||'.'|| l_api_name;
4283 --
4284 l_return_status     VARCHAR2(30);
4285 l_msg_data          VARCHAR2(2000);
4286 l_msg_count         NUMBER;
4287 
4288 l_header_id NUMBER;
4289 l_object_version_number NUMBER := 1;
4290 l_org_id    NUMBER;
4291 BEGIN
4292    -- Standard BEGIN of API savepoint
4293    SAVEPOINT  Insert_Resale_Header;
4294    -- Standard call to check for call compatibility.
4295    IF NOT FND_API.Compatible_API_Call (
4296          l_api_version,
4297          p_api_version,
4298          l_api_name,
4299          G_PKG_NAME)
4300    THEN
4301       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4302    END IF;
4303 
4304    -- Debug Message
4305    IF OZF_DEBUG_HIGH_ON THEN
4306       OZF_UTILITY_PVT.debug_message(l_full_name||': Start');
4307    END IF;
4308 
4309    --Initialize message LIST IF p_init_msg_LIST IS TRUE.
4310    IF FND_API.To_Boolean (p_init_msg_LIST) THEN
4311       FND_MSG_PUB.initialize;
4312    END IF;
4313 
4314    -- Initialize API return status to sucess
4315    x_return_status := FND_API.G_RET_STS_SUCCESS;
4316 
4317    -- INSERT the order information to ozf_resale_headers_all
4318    OPEN g_header_id_csr;
4319    FETCH g_header_id_csr INTO l_header_id;
4320    CLOSE g_header_id_csr;
4321 
4322    x_header_id := l_header_id;
4323    l_org_id := p_line_int_rec.org_id; -- bug # 5997978 fixed
4324    IF OZF_DEBUG_LOW_ON THEN
4325       OZF_UTILITY_PVT.debug_message('before INSERT: header_id' || l_header_id);
4326    END IF;
4327 
4328    OZF_RESALE_HEADERS_PKG.Insert_Row(
4329       px_resale_header_id        => l_header_id,
4330       px_object_version_number   => l_object_version_number,
4331       p_last_update_date         => SYSdate,
4332       p_last_updated_by          => NVL(FND_GLOBAL.user_id,-1),
4333       p_creation_date            => SYSdate,
4334       p_request_id               => FND_GLOBAL.CONC_REQUEST_ID,
4335       p_created_by               => NVL(FND_GLOBAL.user_id,-1),
4336       p_last_update_login        => NVL(FND_GLOBAL.conc_login_id,-1),
4337       p_program_application_id   => FND_GLOBAL.PROG_APPL_ID,
4338       p_program_update_date      => SYSdate,
4339       p_program_id               => FND_GLOBAL.CONC_PROGRAM_ID,
4340       p_created_from             => p_line_int_rec.created_from,
4341       p_date_shipped             => p_line_int_rec.date_shipped,
4342       p_date_ordered             => p_line_int_rec.date_ordered,
4343       p_order_type_id            => p_line_int_rec.order_type_id,
4344       p_order_type               => p_line_int_rec.order_type,
4345       p_order_category           => p_line_int_rec.order_category,
4346       p_status_code              => G_BATCH_PROCESSED,
4347       p_direct_customer_flag     => p_line_int_rec.direct_customer_flag,
4348       p_order_number             => p_line_int_rec.order_number,
4349       p_price_LIST_id            => p_line_int_rec.price_LIST_id,
4350       p_bill_to_cust_account_id  => p_line_int_rec.bill_to_cust_account_id,
4351       p_bill_to_site_use_id      => p_line_int_rec.bill_to_site_use_id,
4352       p_bill_to_party_name       => p_line_int_rec.bill_to_party_name,
4353       p_bill_to_party_id         =>p_line_int_rec.bill_to_party_id ,
4354       p_bill_to_party_site_id    =>p_line_int_rec.bill_to_party_site_id ,
4355       p_bill_to_location         => p_line_int_rec.bill_to_location ,
4356       p_bill_to_duns_number      => p_line_int_rec.bill_to_duns_number,
4357       p_bill_to_address          => p_line_int_rec.bill_to_address,
4358       p_bill_to_city             => p_line_int_rec.bill_to_city ,
4359       p_bill_to_state            => p_line_int_rec.bill_to_state,
4360       p_bill_to_postal_code      => p_line_int_rec.bill_to_postal_code,
4361       p_bill_to_country          => p_line_int_rec.bill_to_country,
4362       p_bill_to_contact_party_id => p_line_int_rec.bill_to_contact_party_id,
4363       p_bill_to_contact_name     => p_line_int_rec.bill_to_contact_name,
4364       p_bill_to_email            => p_line_int_rec.bill_to_email,
4365       p_bill_to_phone            => p_line_int_rec.bill_to_phone,
4366       p_bill_to_fax              => p_line_int_rec.bill_to_fax,
4367       p_ship_to_cust_account_id  => p_line_int_rec.ship_to_cust_account_id,
4368       p_ship_to_site_use_id      => p_line_int_rec.ship_to_site_use_id,
4369       p_ship_to_party_name       => p_line_int_rec.ship_to_party_name,
4370       p_ship_to_party_id         =>p_line_int_rec.ship_to_party_id ,
4371       p_ship_to_party_site_id    =>p_line_int_rec.ship_to_party_site_id ,
4372       p_ship_to_location         => p_line_int_rec.ship_to_location,
4373       p_ship_to_duns_number      => p_line_int_rec.ship_to_duns_number,
4374       p_ship_to_address          => p_line_int_rec.ship_to_address,
4375       p_ship_to_city             => p_line_int_rec.ship_to_city,
4376       p_ship_to_state            => p_line_int_rec.ship_to_state,
4377       p_ship_to_postal_code      => p_line_int_rec.ship_to_postal_code,
4378       p_ship_to_country          => p_line_int_rec.ship_to_country,
4379       p_ship_to_contact_party_id => p_line_int_rec.ship_to_contact_party_id,
4380       p_ship_to_contact_name     => p_line_int_rec.ship_to_contact_name,
4381       p_ship_to_email            => p_line_int_rec.ship_to_email,
4382       p_ship_to_phone            => p_line_int_rec.ship_to_phone,
4383       p_ship_to_fax              => p_line_int_rec.ship_to_fax,
4384       p_sold_from_cust_account_id=> p_line_int_rec.sold_from_cust_account_id,
4385       p_ship_from_cust_account_id=> p_line_int_rec.ship_from_cust_account_id,
4386       p_header_attribute_category=> p_line_int_rec.header_attribute_category,
4387       p_header_attribute1        => p_line_int_rec.header_attribute1,
4388       p_header_attribute2        => p_line_int_rec.header_attribute2,
4389       p_header_attribute3        => p_line_int_rec.header_attribute3,
4390       p_header_attribute4        => p_line_int_rec.header_attribute4,
4391       p_header_attribute5        => p_line_int_rec.header_attribute5,
4392       p_header_attribute6        => p_line_int_rec.header_attribute6,
4393       p_header_attribute7        => p_line_int_rec.header_attribute7,
4394       p_header_attribute8        => p_line_int_rec.header_attribute8,
4395       p_header_attribute9        => p_line_int_rec.header_attribute9,
4396       p_header_attribute10       => p_line_int_rec.header_attribute10,
4397       p_header_attribute11       => p_line_int_rec.header_attribute11,
4398       p_header_attribute12       => p_line_int_rec.header_attribute12,
4399       p_header_attribute13       => p_line_int_rec.header_attribute13,
4400       p_header_attribute14       => p_line_int_rec.header_attribute14,
4401       p_header_attribute15       => p_line_int_rec.header_attribute15,
4402       p_attribute_category       => NULL,
4403       p_attribute1               => NULL,
4404       p_attribute2               => NULL,
4405       p_attribute3               => NULL,
4406       p_attribute4               => NULL,
4407       p_attribute5               => NULL,
4408       p_attribute6               => NULL,
4409       p_attribute7               => NULL,
4410       p_attribute8               => NULL,
4411       p_attribute9               => NULL,
4412       p_attribute10              => NULL,
4413       p_attribute11              => NULL,
4414       p_attribute12              => NULL,
4415       p_attribute13              => NULL,
4416       p_attribute14              => NULL,
4417       p_attribute15              => NULL,
4418       px_org_id                  => l_org_id);
4419 
4420    -- Debug Message
4421    IF OZF_DEBUG_HIGH_ON THEN
4422       OZF_UTILITY_PVT.debug_message(l_full_name||': End');
4423    END IF;
4424 
4425    --Standard call to get message count AND IF count=1, get the message
4426    FND_MSG_PUB.Count_And_Get (
4427        p_encoded => FND_API.G_FALSE,
4428        p_count => x_msg_count,
4429        p_data  => x_msg_data
4430    );
4431 EXCEPTION
4432    WHEN FND_API.G_EXC_ERROR THEN
4433       ROLLBACK TO Insert_Resale_Header;
4434       x_return_status := FND_API.G_RET_STS_ERROR;
4435       -- Standard call to get message count AND IF count=1, get the message
4436       FND_MSG_PUB.Count_And_Get (
4437           p_encoded => FND_API.G_FALSE,
4438           p_count => x_msg_count,
4439           p_data  => x_msg_data
4440       );
4441    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4442       ROLLBACK TO Insert_Resale_Header;
4443       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4444       -- Standard call to get message count AND IF count=1, get the message
4445       FND_MSG_PUB.Count_And_Get (
4446           p_encoded => FND_API.G_FALSE,
4447           p_count => x_msg_count,
4448           p_data  => x_msg_data
4449       );
4450    WHEN OTHERS THEN
4451       ROLLBACK TO Insert_Resale_Header;
4452       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4453       IF OZF_UNEXP_ERROR THEN
4454          FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
4455       END IF;
4456       -- Standard call to get message count AND IF count=1, get the message
4457       FND_MSG_PUB.Count_And_Get (
4458           p_encoded => FND_API.G_FALSE,
4459           p_count => x_msg_count,
4460           p_data  => x_msg_data
4461       );
4462 END Insert_Resale_Header;
4463 
4464 ---------------------------------------------------------------------
4465 -- PROCEDURE
4466 --    Insert_Resale_Line
4467 --
4468 -- PURPOSE
4469 --    This procedure inserts a record IN resale line table
4470 --
4471 -- PARAMETERS
4472 --    p_line_int_rec  IN g_interface_rec_csr%rowtype,
4473 --    x_return_status  OUT VARCHAR2
4474 --
4475 -- NOTES
4476 --
4477 ---------------------------------------------------------------------
4478 PROCEDURE Insert_Resale_Line(
4479     p_api_version            IN  NUMBER
4480    ,p_init_msg_LIST          IN  VARCHAR2 := FND_API.G_FALSE
4481    ,p_commit                 IN  VARCHAR2 := FND_API.G_FALSE
4482    ,p_validation_level       IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL
4483    ,p_line_int_rec           IN g_interface_rec_csr%rowtype
4484    ,p_header_id              IN NUMBER
4485    ,x_line_id                OUT NOCOPY NUMBER
4486    ,x_return_status          OUT NOCOPY VARCHAR2
4487    ,x_msg_data               OUT NOCOPY   VARCHAR2
4488    ,x_msg_count              OUT NOCOPY   NUMBER
4489 )
4490 
4491 IS
4492 l_api_name          CONSTANT VARCHAR2(30) := 'Insert_resale_line';
4493 l_api_version       CONSTANT NUMBER := 1.0;
4494 l_full_name         CONSTANT VARCHAR2(60) := G_PKG_NAME ||'.'|| l_api_name;
4495 --
4496 l_return_status     VARCHAR2(30);
4497 l_msg_data          VARCHAR2(2000);
4498 l_msg_count         NUMBER;
4499 
4500 l_line_id NUMBER;
4501 l_obj_ver_num NUMBER := 1;
4502 l_org_id NUMBER;
4503 l_map_id NUMBER;
4504 BEGIN
4505    -- Standard BEGIN of API savepoint
4506    SAVEPOINT  Insert_Resale_Line;
4507    -- Standard call to check for call compatibility.
4508    IF NOT FND_API.Compatible_API_Call (
4509          l_api_version,
4510          p_api_version,
4511          l_api_name,
4512          G_PKG_NAME)
4513    THEN
4514        RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4515    END IF;
4516 
4517    -- Debug Message
4518    IF OZF_DEBUG_HIGH_ON THEN
4519       OZF_UTILITY_PVT.debug_message(l_full_name||': Start');
4520    END IF;
4521 
4522    --Initialize message LIST IF p_init_msg_LIST IS TRUE.
4523    IF FND_API.To_Boolean (p_init_msg_LIST) THEN
4524       FND_MSG_PUB.initialize;
4525    END IF;
4526 
4527    -- Initialize API return status to sucess
4528    x_return_status := FND_API.G_RET_STS_SUCCESS;
4529 
4530    OPEN g_line_id_csr;
4531    FETCH g_line_id_csr INTO l_line_id;
4532    CLOSE g_line_id_csr;
4533    x_line_id := l_line_id;
4534    l_org_id := p_line_int_rec.org_id; -- bug # 5997978 fixed
4535    IF OZF_DEBUG_LOW_ON THEN
4536       OZF_UTILITY_PVT.debug_message('before line INSERT: header id' || p_header_id);
4537       OZF_UTILITY_PVT.debug_message('before line INSERT:' || l_line_id);
4538    END IF;
4539 
4540    OZF_RESALE_LINES_PKG.Insert_Row(
4541       p_resale_line_id              => l_line_id ,
4542       p_resale_header_id            => p_header_id ,
4543       p_resale_transfer_type        => p_line_int_rec.resale_transfer_type ,
4544       px_object_version_number      => l_obj_ver_num,
4545       p_last_update_date            => SYSdate,
4546       p_last_updated_by             => NVL(FND_GLOBAL.user_id,-1),
4547       p_creation_date               => SYSdate,
4548       p_request_id                  => FND_GLOBAL.CONC_REQUEST_ID,
4549       p_created_by                  => NVL(FND_GLOBAL.user_id,-1),
4550       p_last_update_login           => NVL(FND_GLOBAL.conc_login_id,-1),
4551       p_program_application_id      => FND_GLOBAL.PROG_APPL_ID,
4552       p_program_update_date         => SYSdate,
4553       p_program_id                  => FND_GLOBAL.CONC_PROGRAM_ID,
4554       p_created_from                => p_line_int_rec.created_from,
4555       p_status_code                 => G_BATCH_ADJ_PROCESSED ,
4556       p_product_trans_movement_type => p_line_int_rec.product_transfer_movement_type ,
4557       p_product_transfer_date       => p_line_int_rec.product_transfer_date,
4558       p_end_cust_party_id           => p_line_int_rec.end_cust_party_id,
4559       p_end_cust_site_use_id        => p_line_int_rec.end_cust_site_use_id,
4560       p_end_cust_site_use_code      => p_line_int_rec.end_cust_site_use_code,
4561       p_end_cust_party_site_id      => p_line_int_rec.end_cust_party_site_id ,
4562       p_end_cust_party_name         => p_line_int_rec.end_cust_party_name ,
4563       p_end_cust_location           => p_line_int_rec.end_cust_location ,
4564       p_end_cust_address            => p_line_int_rec.end_cust_address ,
4565       p_end_cust_city               => p_line_int_rec.end_cust_city ,
4566       p_end_cust_state              => p_line_int_rec.end_cust_state ,
4567       p_end_cust_postal_code        => p_line_int_rec.end_cust_postal_code ,
4568       p_end_cust_country            => p_line_int_rec.end_cust_country ,
4569       p_end_cust_contact_party_id   => p_line_int_rec.end_cust_contact_party_id ,
4570       p_end_cust_contact_name       => p_line_int_rec.end_cust_contact_name ,
4571       p_end_cust_email              => p_line_int_rec.end_cust_email ,
4572       p_end_cust_phone              => p_line_int_rec.end_cust_phone ,
4573       p_end_cust_fax                => p_line_int_rec.end_cust_fax ,
4574       p_bill_to_cust_account_id     => p_line_int_rec.bill_to_cust_account_id,
4575       p_bill_to_site_use_id         => p_line_int_rec.bill_to_site_use_id  ,
4576       p_bill_to_party_name          => p_line_int_rec.bill_to_party_name ,
4577       p_bill_to_party_id            => p_line_int_rec.bill_to_party_id ,
4578       p_bill_to_party_site_id       => p_line_int_rec.bill_to_party_site_id ,
4579       p_bill_to_duns_number         => p_line_int_rec.bill_to_duns_number ,
4580       p_bill_to_location            => p_line_int_rec.bill_to_location ,
4581       p_bill_to_address             => p_line_int_rec.bill_to_address ,
4582       p_bill_to_city                => p_line_int_rec.bill_to_city ,
4583       p_bill_to_state               => p_line_int_rec.bill_to_state ,
4584       p_bill_to_postal_code         => p_line_int_rec.bill_to_postal_code  ,
4585       p_bill_to_country             => p_line_int_rec.bill_to_country ,
4586       p_bill_to_contact_party_id    => p_line_int_rec.bill_to_contact_party_id ,
4587       p_bill_to_contact_name        => p_line_int_rec.bill_to_contact_name ,
4588       p_bill_to_email               => p_line_int_rec.bill_to_email ,
4589       p_bill_to_phone               => p_line_int_rec.bill_to_phone ,
4590       p_bill_to_fax                 => p_line_int_rec.bill_to_fax ,
4591       p_ship_to_cust_account_id     => p_line_int_rec.ship_to_cust_account_id  ,
4592       p_ship_to_site_use_id         => p_line_int_rec.ship_to_site_use_id ,
4593       p_ship_to_party_name          => p_line_int_rec.ship_to_party_name ,
4594       p_ship_to_party_id            => p_line_int_rec.ship_to_party_id ,
4595       p_ship_to_party_site_id       => p_line_int_rec.ship_to_party_site_id ,
4596       p_ship_to_duns_number         => p_line_int_rec.ship_to_duns_number ,
4597       p_ship_to_location            => p_line_int_rec.ship_to_location ,
4598       p_ship_to_address             => p_line_int_rec.ship_to_address,
4599       p_ship_to_city                => p_line_int_rec.ship_to_city ,
4600       p_ship_to_state               => p_line_int_rec.ship_to_state ,
4601       p_ship_to_postal_code         => p_line_int_rec.ship_to_postal_code ,
4602       p_ship_to_country             => p_line_int_rec.ship_to_country ,
4603       p_ship_to_contact_party_id    => p_line_int_rec.ship_to_contact_party_id ,
4604       p_ship_to_contact_name        => p_line_int_rec.ship_to_contact_name ,
4605       p_ship_to_email               => p_line_int_rec.ship_to_email ,
4606       p_ship_to_phone               => p_line_int_rec.ship_to_phone ,
4607       p_ship_to_fax                 => p_line_int_rec.ship_to_fax ,
4608       p_ship_from_cust_account_id   => p_line_int_rec.ship_from_cust_account_id  ,
4609       p_ship_from_site_id           => p_line_int_rec.ship_from_site_id,
4610       p_ship_from_party_name        => p_line_int_rec.ship_from_party_name,
4611       p_ship_from_location          => p_line_int_rec.ship_from_location ,
4612       p_ship_from_address           => p_line_int_rec.ship_from_address ,
4613       p_ship_from_city              => p_line_int_rec.ship_from_city ,
4614       p_ship_from_state             => p_line_int_rec.ship_from_state ,
4615       p_ship_from_postal_code       => p_line_int_rec.ship_from_postal_code ,
4616       p_ship_from_country           => p_line_int_rec.ship_from_country,
4617       p_ship_from_contact_party_id  => p_line_int_rec.ship_from_contact_party_id ,
4618       p_ship_from_contact_name      => p_line_int_rec.ship_from_contact_name ,
4619       p_ship_from_email             => p_line_int_rec.ship_from_email ,
4620       p_ship_from_fax               => p_line_int_rec.ship_from_fax ,
4621       p_ship_from_phone             => p_line_int_rec.ship_from_phone ,
4622       p_sold_from_cust_account_id   => p_line_int_rec.sold_from_cust_account_id ,
4623       p_sold_from_site_id           => p_line_int_rec.sold_from_site_id ,
4624       p_sold_from_party_name        => p_line_int_rec.sold_from_party_name,
4625       p_sold_from_location          => p_line_int_rec.sold_from_location ,
4626       p_sold_from_address           => p_line_int_rec.sold_from_address ,
4627       p_sold_from_city              => p_line_int_rec.sold_from_city ,
4628       p_sold_from_state             => p_line_int_rec.sold_from_state ,
4629       p_sold_from_postal_code       => p_line_int_rec.sold_from_postal_code ,
4630       p_sold_from_country           => p_line_int_rec.sold_from_country,
4631       p_sold_from_contact_party_id  => p_line_int_rec.sold_from_contact_party_id ,
4632       p_sold_from_contact_name      => p_line_int_rec.sold_from_contact_name ,
4633       p_sold_from_email             => p_line_int_rec.sold_from_email,
4634       p_sold_from_phone             => p_line_int_rec.sold_from_phone,
4635       p_sold_from_fax               => p_line_int_rec.sold_from_fax,
4636       p_price_LIST_id               => p_line_int_rec.price_LIST_id ,
4637       p_price_LIST_name             => p_line_int_rec.price_LIST_name ,
4638       p_invoice_number              => p_line_int_rec.invoice_number ,
4639       p_date_invoiced               => p_line_int_rec.date_invoiced,
4640       p_po_number                   => p_line_int_rec.po_number ,
4641       p_po_release_number           => p_line_int_rec.po_release_number ,
4642       p_po_type                     => p_line_int_rec.po_type ,
4643       p_order_number                => p_line_int_rec.order_number ,
4644       p_date_ordered                => p_line_int_rec.date_ordered,
4645       p_date_shipped                => p_line_int_rec.date_shipped,
4646       p_purchase_uom_code           => p_line_int_rec.purchase_uom_code ,
4647       p_quantity                    => p_line_int_rec.quantity ,
4648       p_uom_code                    => p_line_int_rec.uom_code ,
4649       p_currency_code               => p_line_int_rec.currency_code ,
4650       p_exchange_rate               => p_line_int_rec.exchange_rate ,
4651       p_exchange_rate_type          => p_line_int_rec.exchange_rate_type,
4652       p_exchange_rate_date          => p_line_int_rec.exchange_rate_date,
4653       p_selling_price               => p_line_int_rec.selling_price ,
4654       p_acctd_selling_price         => p_line_int_rec.acctd_selling_price ,
4655       p_purchase_price              => p_line_int_rec.purchase_price ,
4656       p_acctd_purchase_price        => p_line_int_rec.acctd_purchase_price ,
4657       p_tracing_flag                => p_line_int_rec.tracing_flag ,
4658       p_orig_system_quantity        => p_line_int_rec. orig_system_quantity,
4659       p_orig_system_uom             => p_line_int_rec.orig_system_uom ,
4660       p_orig_system_currency_code   => p_line_int_rec.orig_system_currency_code,
4661       p_orig_system_selling_price   => p_line_int_rec.orig_system_selling_price ,
4662       p_orig_system_line_reference  => p_line_int_rec.orig_system_line_reference ,
4663       p_orig_system_reference       => p_line_int_rec.orig_system_reference ,
4664       p_orig_system_purchase_uom    => p_line_int_rec.orig_system_purchase_uom,
4665       p_orig_system_purchase_curr   => p_line_int_rec.orig_system_purchase_curr,
4666       p_orig_system_purchase_price  => p_line_int_rec.orig_system_purchase_price,
4667       p_orig_system_purchase_quant  => p_line_int_rec.orig_system_purchase_quantity,
4668       p_orig_system_item_number     => p_line_int_rec.orig_system_item_number,
4669       p_product_category_id         => p_line_int_rec.product_category_id ,
4670       p_category_name               => p_line_int_rec.category_name  ,
4671       p_inventory_item_segment1     => p_line_int_rec.inventory_item_segment1 ,
4672       p_inventory_item_segment2     => p_line_int_rec.inventory_item_segment2 ,
4673       p_inventory_item_segment3     => p_line_int_rec.inventory_item_segment3 ,
4674       p_inventory_item_segment4     => p_line_int_rec.inventory_item_segment4 ,
4675       p_inventory_item_segment5     => p_line_int_rec.inventory_item_segment5 ,
4676       p_inventory_item_segment6     => p_line_int_rec.inventory_item_segment6 ,
4677       p_inventory_item_segment7     => p_line_int_rec.inventory_item_segment7 ,
4678       p_inventory_item_segment8     => p_line_int_rec.inventory_item_segment8 ,
4679       p_inventory_item_segment9     => p_line_int_rec.inventory_item_segment9,
4680       p_inventory_item_segment10    => p_line_int_rec.inventory_item_segment10,
4681       p_inventory_item_segment11    => p_line_int_rec.inventory_item_segment11,
4682       p_inventory_item_segment12    => p_line_int_rec.inventory_item_segment12,
4683       p_inventory_item_segment13    => p_line_int_rec.inventory_item_segment13,
4684       p_inventory_item_segment14    => p_line_int_rec.inventory_item_segment14,
4685       p_inventory_item_segment15    => p_line_int_rec.inventory_item_segment15,
4686       p_inventory_item_segment16    => p_line_int_rec.inventory_item_segment16,
4687       p_inventory_item_segment17    => p_line_int_rec.inventory_item_segment17,
4688       p_inventory_item_segment18    => p_line_int_rec.inventory_item_segment18,
4689       p_inventory_item_segment19    => p_line_int_rec.inventory_item_segment19,
4690       p_inventory_item_segment20    => p_line_int_rec.inventory_item_segment20 ,
4691       p_inventory_item_id           => p_line_int_rec.inventory_item_id ,
4692       p_item_description            => p_line_int_rec.item_description ,
4693       p_upc_code                    => p_line_int_rec.upc_code ,
4694       p_item_number                 => p_line_int_rec.item_number ,
4695       p_direct_customer_flag        => p_line_int_rec.direct_customer_flag ,
4696       p_attribute_category          => NULL,
4697       p_attribute1                  => NULL,
4698       p_attribute2                  => NULL,
4699       p_attribute3                  => NULL,
4700       p_attribute4                  => NULL,
4701       p_attribute5                  => NULL,
4702       p_attribute6                  => NULL,
4703       p_attribute7                  => NULL,
4704       p_attribute8                  => NULL,
4705       p_attribute9                  => NULL,
4706       p_attribute10                 => NULL,
4707       p_attribute11                 => NULL,
4708       p_attribute12                 => NULL,
4709       p_attribute13                 => NULL,
4710       p_attribute14                 => NULL,
4711       p_attribute15                 => NULL,
4712       p_line_attribute_category     => p_line_int_rec.line_attribute_category,
4713       p_line_attribute1             => p_line_int_rec.line_attribute1 ,
4714       p_line_attribute2             => p_line_int_rec.line_attribute2 ,
4715       p_line_attribute3             => p_line_int_rec.line_attribute3,
4716       p_line_attribute4             => p_line_int_rec.line_attribute4 ,
4717       p_line_attribute5             => p_line_int_rec.line_attribute5 ,
4718       p_line_attribute6             => p_line_int_rec.line_attribute6 ,
4719       p_line_attribute7             => p_line_int_rec.line_attribute7,
4720       p_line_attribute8             => p_line_int_rec.line_attribute8,
4721       p_line_attribute9             => p_line_int_rec.line_attribute9,
4722       p_line_attribute10            => p_line_int_rec.line_attribute10,
4723       p_line_attribute11            => p_line_int_rec.line_attribute11,
4724       p_line_attribute12            => p_line_int_rec.line_attribute12,
4725       p_line_attribute13            => p_line_int_rec.line_attribute13,
4726       p_line_attribute14            => p_line_int_rec.line_attribute14,
4727       p_line_attribute15            => p_line_int_rec.line_attribute15 ,
4728       px_org_id                     => l_org_id,
4729 
4730       -- For Bug#9447673 SSD IDSM ER(+)
4731       p_invoice_line_number         => p_line_int_rec.invoice_line_number,
4732       p_order_line_number           => p_line_int_rec.order_line_number,
4733       p_supplier_item_cost          => p_line_int_rec.supplier_item_cost,
4734       p_supplier_item_uom           => p_line_int_rec.supplier_item_uom
4735       -- For Bug#9447673 SSD IDSM ER(-)
4736 
4737       );
4738 
4739    IF OZF_DEBUG_HIGH_ON THEN
4740       OZF_UTILITY_PVT.debug_message('line INSERT successful id:' || l_line_id);
4741       OZF_UTILITY_PVT.debug_message(l_full_name||': End');
4742    END IF;
4743 
4744    --Standard call to get message count AND IF count=1, get the message
4745    FND_MSG_PUB.Count_And_Get (
4746       p_encoded => FND_API.G_FALSE,
4747       p_count => x_msg_count,
4748       p_data  => x_msg_data
4749    );
4750    x_return_status := l_return_status;
4751 EXCEPTION
4752    WHEN FND_API.G_EXC_ERROR THEN
4753       ROLLBACK TO Insert_Resale_Line;
4754       x_return_status := FND_API.G_RET_STS_ERROR;
4755       -- Standard call to get message count AND IF count=1, get the message
4756       FND_MSG_PUB.Count_And_Get (
4757           p_encoded => FND_API.G_FALSE,
4758           p_count => x_msg_count,
4759           p_data  => x_msg_data
4760       );
4761    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4762       ROLLBACK TO Insert_Resale_Line;
4763       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4764       -- Standard call to get message count AND IF count=1, get the message
4765       FND_MSG_PUB.Count_And_Get (
4766           p_encoded => FND_API.G_FALSE,
4767           p_count => x_msg_count,
4768           p_data  => x_msg_data
4769       );
4770    WHEN OTHERS THEN
4771       ROLLBACK TO Insert_Resale_Line;
4772       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4773       IF OZF_UNEXP_ERROR THEN
4774          FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
4775       END IF;
4776       -- Standard call to get message count AND IF count=1, get the message
4777       FND_MSG_PUB.Count_And_Get (
4778           p_encoded => FND_API.G_FALSE,
4779           p_count => x_msg_count,
4780           p_data  => x_msg_data
4781       );
4782 END Insert_Resale_Line;
4783 
4784 ---------------------------------------------------------------------
4785 -- PROCEDURE
4786 --    Insert_Resale_Line_Mapping
4787 --
4788 -- PURPOSE
4789 --    This procedure inserts a record IN resale_batch_line_mapping  table
4790 --
4791 -- PARAMETERS
4792 --    p_line_int_rec  IN g_interface_rec_csr%rowtype,
4793 --    x_return_status  OUT VARCHAR2
4794 --
4795 -- NOTES
4796 --
4797 ---------------------------------------------------------------------
4798 PROCEDURE Insert_Resale_Line_Mapping(
4799     p_api_version            IN  NUMBER
4800    ,p_init_msg_LIST          IN  VARCHAR2 := FND_API.G_FALSE
4801    ,p_commit                 IN  VARCHAR2 := FND_API.G_FALSE
4802    ,p_validation_level       IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL
4803    ,p_resale_batch_id        IN  NUMBER
4804    ,p_line_id                IN  NUMBER
4805    ,x_return_status          OUT NOCOPY VARCHAR2
4806    ,x_msg_data               OUT NOCOPY VARCHAR2
4807    ,x_msg_count              OUT NOCOPY NUMBER
4808 )
4809 IS
4810 l_api_name          CONSTANT VARCHAR2(30) := 'Insert_Resale_Line_Mapping';
4811 l_api_version       CONSTANT NUMBER := 1.0;
4812 l_full_name         CONSTANT VARCHAR2(60) := G_PKG_NAME ||'.'|| l_api_name;
4813 --
4814 l_return_status     VARCHAR2(30);
4815 l_msg_data          VARCHAR2(2000);
4816 l_msg_count         NUMBER;
4817 
4818 l_obj_ver_num NUMBER := 1;
4819 l_org_id NUMBER;
4820 l_batch_org_id NUMBER; -- bug # 5997978 fixed
4821 l_map_id NUMBER;
4822 BEGIN
4823    -- Standard BEGIN of API savepoint
4824    SAVEPOINT  Insert_Resale_Line_Mapping;
4825    -- Standard call to check for call compatibility.
4826    IF NOT FND_API.Compatible_API_Call (
4827         l_api_version,
4828         p_api_version,
4829         l_api_name,
4830         G_PKG_NAME)
4831    THEN
4832       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4833    END IF;
4834 
4835    -- Debug Message
4836    IF OZF_DEBUG_HIGH_ON THEN
4837       OZF_UTILITY_PVT.debug_message(l_full_name||': Start');
4838    END IF;
4839 
4840    --Initialize message LIST IF p_init_msg_LIST IS TRUE.
4841    IF FND_API.To_Boolean (p_init_msg_LIST) THEN
4842       FND_MSG_PUB.initialize;
4843    END IF;
4844 
4845    -- Initialize API return status to sucess
4846    x_return_status := FND_API.G_RET_STS_SUCCESS;
4847    OPEN g_map_id_csr;
4848    FETCH g_map_id_csr INTO l_map_id;
4849    CLOSE g_map_id_csr;
4850 
4851    -- Start: bug # 5997978 fixed
4852    OPEN g_resale_batch_org_id_csr(p_resale_batch_id);
4853    FETCH g_resale_batch_org_id_csr INTO l_batch_org_id;
4854    CLOSE g_resale_batch_org_id_csr;
4855    l_org_id := MO_GLOBAL.get_valid_org(l_batch_org_id);
4856    IF (l_batch_org_id IS NULL OR l_org_id IS NULL) THEN
4857          OZF_UTILITY_PVT.error_message(p_message_name => 'OZF_ORG_ID_NOTFOUND');
4858          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4859    END IF;
4860    -- End: bug # 5997978 fixed
4861 
4862    -- INSERT INTO mapping table
4863    OZF_RESALE_BATCH_LINE_MAPS_PKG.Insert_Row(
4864       px_resale_batch_line_map_id   => l_map_id,
4865       p_resale_batch_id             => p_resale_batch_id,
4866       p_resale_line_id              => p_line_id,
4867       px_object_version_number      => l_obj_ver_num,
4868       p_last_update_date            => SYSdate,
4869       p_last_updated_by             => NVL(FND_GLOBAL.user_id,-1),
4870       p_creation_date               => SYSdate,
4871       p_request_id                  => FND_GLOBAL.CONC_REQUEST_ID,
4872       p_created_by                  => NVL(FND_GLOBAL.user_id,-1),
4873       p_last_update_login           => NVL(FND_GLOBAL.conc_login_id,-1),
4874       p_program_application_id      => FND_GLOBAL.PROG_APPL_ID,
4875       p_program_update_date         => SYSdate,
4876       p_program_id                  => FND_GLOBAL.CONC_PROGRAM_ID,
4877       p_created_from                => NULL,
4878       p_attribute_category          => NULL,
4879       p_attribute1                  => NULL,
4880       p_attribute2                  => NULL,
4881       p_attribute3                  => NULL,
4882       p_attribute4                  => NULL,
4883       p_attribute5                  => NULL,
4884       p_attribute6                  => NULL,
4885       p_attribute7                  => NULL,
4886       p_attribute8                  => NULL,
4887       p_attribute9                  => NULL,
4888       p_attribute10                 => NULL,
4889       p_attribute11                 => NULL,
4890       p_attribute12                 => NULL,
4891       p_attribute13                 => NULL,
4892       p_attribute14                 => NULL,
4893       p_attribute15                 => NULL,
4894       px_org_id                     => l_org_id);
4895 
4896     -- Debug Message
4897     IF OZF_DEBUG_HIGH_ON THEN
4898       OZF_UTILITY_PVT.debug_message(l_full_name||': End');
4899     END IF;
4900 
4901     --Standard call to get message count AND IF count=1, get the message
4902     FND_MSG_PUB.Count_And_Get (
4903         p_encoded => FND_API.G_FALSE,
4904         p_count => x_msg_count,
4905         p_data  => x_msg_data
4906     );
4907 EXCEPTION
4908    WHEN FND_API.G_EXC_ERROR THEN
4909       ROLLBACK TO Insert_Resale_Line_Mapping;
4910       x_return_status := FND_API.G_RET_STS_ERROR;
4911       -- Standard call to get message count AND IF count=1, get the message
4912       FND_MSG_PUB.Count_And_Get (
4913           p_encoded => FND_API.G_FALSE,
4914           p_count => x_msg_count,
4915           p_data  => x_msg_data
4916       );
4917    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
4918       ROLLBACK TO Insert_Resale_Line_Mapping;
4919       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4920       -- Standard call to get message count AND IF count=1, get the message
4921       FND_MSG_PUB.Count_And_Get (
4922           p_encoded => FND_API.G_FALSE,
4923           p_count => x_msg_count,
4924           p_data  => x_msg_data
4925       );
4926    WHEN OTHERS THEN
4927       ROLLBACK TO Insert_Resale_Line_Mapping;
4928       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
4929       IF OZF_UNEXP_ERROR THEN
4930          FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
4931       END IF;
4932       -- Standard call to get message count AND IF count=1, get the message
4933       FND_MSG_PUB.Count_And_Get (
4934           p_encoded => FND_API.G_FALSE,
4935           p_count => x_msg_count,
4936           p_data  => x_msg_data
4937       );
4938 END Insert_Resale_Line_Mapping;
4939 
4940 ---------------------------------------------------------------------
4941 -- PROCEDURE
4942 --    Delete_Log
4943 --
4944 -- PURPOSE
4945 --    This procedure delets the log for all open lines of batch
4946 --
4947 -- PARAMETERS
4948 --    p_resale_batch_id  IN number
4949 --    x_return_status  out VARCHAR2
4950 --
4951 -- NOTES
4952 -----------------------------------------------------------------------
4953 PROCEDURE Delete_Log(
4954     p_api_version            IN  NUMBER
4955    ,p_init_msg_list          IN  VARCHAR2 := FND_API.G_FALSE
4956    ,p_commit                 IN  VARCHAR2 := FND_API.G_FALSE
4957    ,p_validation_level       IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL
4958    ,p_resale_batch_id        IN  NUMBER
4959    ,x_return_status          OUT NOCOPY VARCHAR2
4960    ,x_msg_data               OUT NOCOPY VARCHAR2
4961    ,x_msg_count              OUT NOCOPY NUMBER
4962 )
4963 IS
4964 l_api_name          CONSTANT VARCHAR2(30) := 'Delete_Log';
4965 l_api_version       CONSTANT NUMBER := 1.0;
4966 l_full_name         CONSTANT VARCHAR2(60) := G_PKG_NAME ||'.'|| l_api_name;
4967 --
4968 l_return_status     VARCHAR2(30);
4969 l_msg_data          VARCHAR2(2000);
4970 l_msg_count         NUMBER;
4971 
4972 BEGIN
4973    -- Standard BEGIN of API savepoint
4974    SAVEPOINT  Delete_Log;
4975    -- Standard call to check for call compatibility.
4976    IF NOT FND_API.Compatible_API_Call (
4977         l_api_version,
4978         p_api_version,
4979         l_api_name,
4980         G_PKG_NAME)
4981    THEN
4982       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
4983    END IF;
4984 
4985    -- Debug Message
4986    IF OZF_DEBUG_HIGH_ON THEN
4987       OZF_UTILITY_PVT.debug_message(l_full_name||': Start');
4988    END IF;
4989 
4990    --Initialize message LIST IF p_init_msg_LIST IS TRUE.
4991    IF FND_API.To_Boolean (p_init_msg_LIST) THEN
4992       FND_MSG_PUB.initialize;
4993    END IF;
4994 
4995    -- Initialize API return status to sucess
4996    x_return_status := FND_API.G_RET_STS_SUCCESS;
4997 
4998    -- delete batch logs
4999    DELETE FROM OZF_RESALE_LOGS
5000    WHERE resale_id = p_resale_batch_id
5001    AND   resale_id_type = G_ID_TYPE_BATCH;
5002 
5003    -- delete interface logs
5004    DELETE FROM OZF_RESALE_LOGS a
5005    WHERE exists (
5006       SELECT 1
5007       FROM OZF_RESALE_LINES_INT b
5008       WHERE b.resale_batch_id = p_resale_batch_id
5009       AND   a.resale_id = b.resale_line_int_id
5010       AND   a.resale_id_type = G_ID_TYPE_IFACE
5011       );
5012 
5013     -- Debug Message
5014     IF OZF_DEBUG_HIGH_ON THEN
5015       OZF_UTILITY_PVT.debug_message(l_full_name||': End');
5016     END IF;
5017 
5018     --Standard call to get message count AND IF count=1, get the message
5019     FND_MSG_PUB.Count_And_Get (
5020         p_encoded => FND_API.G_FALSE,
5021         p_count => x_msg_count,
5022         p_data  => x_msg_data
5023     );
5024 EXCEPTION
5025    WHEN FND_API.G_EXC_ERROR THEN
5026       ROLLBACK TO  Delete_Log;
5027       x_return_status := FND_API.G_RET_STS_ERROR;
5028       -- Standard call to get message count AND IF count=1, get the message
5029       FND_MSG_PUB.Count_And_Get (
5030           p_encoded => FND_API.G_FALSE,
5031           p_count => x_msg_count,
5032           p_data  => x_msg_data
5033       );
5034    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5035       ROLLBACK TO  Delete_Log;
5036       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5037       -- Standard call to get message count AND IF count=1, get the message
5038       FND_MSG_PUB.Count_And_Get (
5039           p_encoded => FND_API.G_FALSE,
5040           p_count => x_msg_count,
5041           p_data  => x_msg_data
5042       );
5043    WHEN OTHERS THEN
5044       ROLLBACK TO  Delete_Log;
5045       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5046       IF OZF_UNEXP_ERROR THEN
5047          FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
5048       END IF;
5049       -- Standard call to get message count AND IF count=1, get the message
5050       FND_MSG_PUB.Count_And_Get (
5051           p_encoded => FND_API.G_FALSE,
5052           p_count => x_msg_count,
5053           p_data  => x_msg_data
5054       );
5055 END  Delete_Log;
5056 
5057 
5058 ---------------------------------------------------------------------
5059 -- PROCEDURE
5060 --    Create_Party
5061 --
5062 -- PURPOSE
5063 --    This procedure creates party, party site, party site use and relationship
5064 --
5065 -- PARAMETERS
5066 --    px_party_rec  IN OUT party_rec_type
5067 --    x_return_status  out VARCHAR2
5068 --
5069 -- NOTES
5070 --
5071 ---------------------------------------------------------------------
5072 PROCEDURE Create_Party
5073 (  p_api_version            IN  NUMBER
5074   ,p_init_msg_list          IN  VARCHAR2 := FND_API.G_FALSE
5075   ,p_commit                 IN  VARCHAR2 := FND_API.G_FALSE
5076   ,p_validation_level       IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL
5077   ,px_party_rec             IN OUT NOCOPY party_rec_type
5078   ,x_return_status          OUT NOCOPY VARCHAR2
5079   ,x_msg_count              OUT NOCOPY NUMBER
5080   ,x_msg_data               OUT NOCOPY VARCHAR2
5081 )
5082 IS
5083 
5084 l_api_name              CONSTANT VARCHAR2(30) := 'Create_Party';
5085 l_api_version           CONSTANT NUMBER := 1.0;
5086 l_full_name             CONSTANT VARCHAR2(60) := G_PKG_NAME ||'.'|| l_api_name;
5087 
5088 l_organization_rec      HZ_PARTY_V2PUB.ORGANIZATION_REC_TYPE;
5089 l_location_rec          HZ_LOCATION_V2PUB.LOCATION_REC_TYPE;
5090 l_party_site_rec        HZ_PARTY_SITE_V2PUB.PARTY_SITE_REC_TYPE;
5091 l_party_site_use_rec    HZ_PARTY_SITE_V2PUB.PARTY_SITE_USE_REC_TYPE;
5092 l_relationship_rec      HZ_RELATIONSHIP_V2PUB.RELATIONSHIP_REC_TYPE;
5093 
5094 l_party_id              NUMBER;
5095 l_party_number          VARCHAR2(2000);
5096 l_party_no              VARCHAR2(2000);
5097 l_profile_id            NUMBER;
5098 l_location_id           NUMBER;
5099 l_party_site_id         NUMBER;
5100 l_party_site_number     VARCHAR2(2000);
5101 l_party_site_use_id     NUMBER;
5102 l_relationship_id       NUMBER;
5103 
5104 BEGIN
5105    -- Standard BEGIN of API savepoint
5106    SAVEPOINT  Party_Create;
5107    -- Standard call to check for call compatibility.
5108    IF NOT FND_API.Compatible_API_Call (
5109         l_api_version,
5110         p_api_version,
5111         l_api_name,
5112         G_PKG_NAME)
5113    THEN
5114       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5115    END IF;
5116 
5117    --Initialize message LIST IF p_init_msg_LIST IS TRUE.
5118    IF FND_API.To_Boolean (p_init_msg_LIST) THEN
5119       FND_MSG_PUB.initialize;
5120    END IF;
5121 
5122    -- Debug Message
5123    IF OZF_DEBUG_HIGH_ON THEN
5124       OZF_UTILITY_PVT.debug_message(l_full_name||': Start');
5125    END IF;
5126 
5127    x_return_status := FND_API.g_ret_sts_success;
5128 
5129    --  Create  Organization
5130    IF  px_party_rec.name IS NOT NULL THEN
5131        IF OZF_DEBUG_LOW_ON THEN
5132          OZF_UTILITY_PVT.debug_message('NAME '|| px_party_rec.name);
5133        END IF;
5134       l_organization_rec.organization_name     := px_party_rec.name;
5135       -- Bug 4630628 (+)
5136       --l_organization_rec.created_by_module     := 'TCA_V2_API';
5137       l_organization_rec.created_by_module     := 'OZF_RESALE';
5138       -- Bug 4630628 (-)
5139       l_organization_rec.party_rec.status      := 'A';
5140       l_organization_rec.application_id        :=  682;
5141 
5142       HZ_PARTY_V2PUB.create_organization(
5143          p_init_msg_list     => FND_API.G_FALSE,
5144          p_organization_rec  => l_organization_rec,
5145          x_return_status     => x_return_status,
5146          x_msg_count        => x_msg_count,
5147          x_msg_data          => x_msg_data,
5148          x_party_id          => px_party_rec.party_id,
5149          x_party_number      => l_party_number,
5150          x_profile_id        => l_profile_id);
5151 
5152       IF x_return_status = FND_API.G_RET_STS_ERROR THEN
5153          RAISE FND_API.G_EXC_ERROR;
5154       ELSIF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5155          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5156       END IF;
5157       IF OZF_DEBUG_LOW_ON THEN
5158          OZF_UTILITY_PVT.debug_message('Party Id '||px_party_rec.party_id);
5159       END IF;
5160    END IF;
5161 
5162    --  Create Location
5163    IF px_party_rec.address IS NOT NULL THEN
5164 
5165       l_location_rec.country               := px_party_rec.country;
5166       l_location_rec.address1              := px_party_rec.address;
5167       l_location_rec.city                  := px_party_rec.city;
5168       l_location_rec.postal_code           := px_party_rec.postal_code;
5169       l_location_rec.state                 := px_party_rec.state;
5170       -- Bug 4630628 (+)
5171       --l_location_rec.created_by_module     := 'TCA_V2_API';
5172       l_location_rec.created_by_module     := 'OZF_RESALE';
5173       -- Bug 4630628 (-)
5174 
5175       HZ_LOCATION_V2PUB.create_location(
5176          p_init_msg_list    => FND_API.G_FALSE,
5177          p_location_rec     => l_location_rec,
5178          x_return_status    => x_return_status,
5179          x_msg_count        => x_msg_count,
5180          x_msg_data         => x_msg_data,
5181          x_location_id      => l_location_id);
5182 
5183       IF x_return_status = FND_API.G_RET_STS_ERROR THEN
5184          RAISE FND_API.G_EXC_ERROR;
5185       ELSIF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5186          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5187       END IF;
5188       IF OZF_DEBUG_LOW_ON THEN
5189          OZF_UTILITY_PVT.debug_message('Location ID '|| l_location_id);
5190       END IF;
5191    END IF;
5192 
5193    --  Create Party Site
5194    IF  px_party_rec.party_id IS NOT NULL AND
5195        l_location_id IS NOT NULL THEN
5196 
5197       l_party_site_rec.party_id                 := px_party_rec.party_id;
5198       l_party_site_rec.location_id              := l_location_id;
5199       l_party_site_rec.identifying_address_flag := 'Y';
5200       l_party_site_rec.status                   := 'A';
5201       -- Bug 4630628 (+)
5202       --l_party_site_rec.created_by_module        := 'TCA_V2_API';
5203       l_party_site_rec.created_by_module        := 'OZF_RESALE';
5204       -- Bug 4630628 (-)
5205 
5206       HZ_PARTY_SITE_V2PUB.create_party_site (
5207          p_init_msg_list    => FND_API.G_FALSE,
5208          p_party_site_rec   => l_party_site_rec,
5209          x_return_status    => x_return_status,
5210          x_msg_count        => x_msg_count,
5211          x_msg_data         => x_msg_data,
5212          x_party_site_id    => px_party_rec.party_site_id,
5213          x_party_site_number=> l_party_site_number);
5214 
5215       IF x_return_status = FND_API.G_RET_STS_ERROR THEN
5216          RAISE FND_API.G_EXC_ERROR;
5217       ELSIF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5218          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5219       END IF;
5220       IF OZF_DEBUG_LOW_ON THEN
5221          OZF_UTILITY_PVT.debug_message('Party Site ID '|| px_party_rec.party_site_id);
5222       END IF;
5223    END IF;
5224 
5225    --  Create Party Site Use
5226    IF px_party_rec.party_site_id IS NOT NULL THEN
5227 
5228       l_party_site_use_rec.party_site_id           := px_party_rec.party_site_id;
5229       -- Bug 4630628 (+)
5230       --l_party_site_use_rec.created_by_module       := 'TCA_V2_API';
5231       l_party_site_use_rec.created_by_module       := 'OZF_RESALE';
5232       -- Bug 4630628 (-)
5233       l_party_site_use_rec.application_id          := 682;
5234 
5235 
5236       IF (px_party_rec.site_use_code is null OR px_party_rec.site_use_code = FND_API.G_MISS_CHAR) THEN
5237          l_party_site_use_rec.site_use_type := 'BILL_TO';
5238       ELSE
5239          l_party_site_use_rec.site_use_type := px_party_rec.site_use_code;  -- 'BILL_TO';
5240       END IF;
5241 
5242       HZ_PARTY_SITE_V2PUB.create_party_site_use(
5243          p_init_msg_list      => FND_API.G_FALSE,
5244          p_party_site_use_rec => l_party_site_use_rec,
5245          x_return_status      => x_return_status,
5246          x_msg_count          => x_msg_count,
5247          x_msg_data           => x_msg_data,
5248          x_party_site_use_id  => px_party_rec.party_site_use_id
5249       );
5250       IF x_return_status = FND_API.G_RET_STS_ERROR THEN
5251          RAISE FND_API.G_EXC_ERROR;
5252       ELSIF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5253          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5254       END IF;
5255       IF OZF_DEBUG_LOW_ON THEN
5256          OZF_UTILITY_PVT.debug_message('Party Site Use ID '|| px_party_rec.party_site_use_id);
5257       END IF;
5258    END IF;
5259 
5260    -- Create Relationship
5261    IF px_party_rec.party_id IS NOT NULL AND
5262       px_party_rec.partner_party_id IS NOT NULL THEN
5263 
5264       l_relationship_rec.subject_id                :=  px_party_rec.party_id;
5265       l_relationship_rec.subject_type              := 'ORGANIZATION';
5266       l_relationship_rec.subject_table_name        := 'HZ_PARTIES';
5267       l_relationship_rec.object_id                 :=  px_party_rec.partner_party_id;
5268       l_relationship_rec.object_type               := 'ORGANIZATION';
5269       l_relationship_rec.object_table_name         := 'HZ_PARTIES';
5270       l_relationship_rec.relationship_type         := 'CUSTOMER/SELLER';
5271       l_relationship_rec.start_date                := sysdate;
5272       l_relationship_rec.relationship_code         := 'CUSTOMER_OF';
5273       -- Bug 4630628 (+)
5274       --l_relationship_rec.created_by_module         := 'TCA_V2_API';
5275       l_relationship_rec.created_by_module         := 'OZF_RESALE';
5276       -- Bug 4630628 (-)
5277       l_relationship_rec.application_id            := 682;
5278       l_relationship_rec.status                    := 'A';
5279 
5280 
5281       HZ_RELATIONSHIP_V2PUB.create_relationship(
5282          p_init_msg_list              => FND_API.G_FALSE,
5283          p_relationship_rec           => l_relationship_rec,
5284          x_relationship_id            => l_relationship_id,
5285          x_party_id                   => l_party_id,
5286          x_party_number               => l_party_no,
5287          x_return_status              => x_return_status,
5288          x_msg_count                  => x_msg_count,
5289          x_msg_data                   => x_msg_data,
5290          p_create_org_contact         => 'Y'
5291       );
5292       IF x_return_status = FND_API.G_RET_STS_ERROR THEN
5293          RAISE FND_API.G_EXC_ERROR;
5294       ELSIF x_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5295          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5296       END IF;
5297       IF OZF_DEBUG_LOW_ON THEN
5298          OZF_UTILITY_PVT.debug_message('Relationship ID '|| l_relationship_id);
5299       END IF;
5300    END IF;
5301 
5302 
5303 
5304    -- Debug Message
5305    IF OZF_DEBUG_HIGH_ON THEN
5306       OZF_UTILITY_PVT.debug_message(l_full_name||': End');
5307    END IF;
5308    --Standard call to get message count AND IF count=1, get the message
5309    FND_MSG_PUB.Count_And_Get (
5310         p_encoded => FND_API.G_FALSE,
5311         p_count => x_msg_count,
5312         p_data  => x_msg_data);
5313 
5314 EXCEPTION
5315    WHEN FND_API.G_EXC_ERROR THEN
5316       x_return_status := FND_API.G_RET_STS_ERROR;
5317       -- Standard call to get message count and if count=1, get the message
5318       FND_MSG_PUB.Count_And_Get (
5319             p_encoded => FND_API.G_FALSE,
5320             p_count   => x_msg_count,
5321             p_data    => x_msg_data
5322       );
5323    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5324       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5325       -- Standard call to get message count and if count=1, get the message
5326       FND_MSG_PUB.Count_And_Get (
5327             p_encoded => FND_API.G_FALSE,
5328             p_count   => x_msg_count,
5329             p_data    => x_msg_data
5330       );
5331    WHEN OTHERS THEN
5332       OZF_UTILITY_PVT.debug_message('SQLERRM '|| sqlerrm);
5333 
5334       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5335       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
5336       THEN
5337          FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
5338       END IF;
5339       -- Standard call to get message count and if count=1, get the message
5340       FND_MSG_PUB.Count_And_Get (
5341             p_encoded => FND_API.G_FALSE,
5342             p_count   => x_msg_count,
5343             p_data    => x_msg_data
5344       );
5345 END  Create_Party;
5346 
5347 
5348 ---------------------------------------------------------------------
5349 -- PROCEDURE
5350 --    Build_Global_Resale_Rec
5351 --
5352 -- PURPOSE
5353 --    Build Global Resale Record for Pricing Simulation
5354 --
5355 -- PARAMETERS
5356 --    p_caller_type          IN VARCHAR2
5357 --    p_resale_line_int_rec  IN OZF_RESALE_COMMON_PVT.g_interface_rec_csr%ROWTYPE
5358 --    p_resale_line_rec      IN OZF_RESALE_LINES%ROWTYPE
5359 --
5360 -- NOTES
5361 --
5362 ---------------------------------------------------------------------
5363 PROCEDURE Build_Global_Resale_Rec
5364 (  p_api_version         IN  NUMBER
5365   ,p_init_msg_list       IN  VARCHAR2
5366   ,p_commit              IN  VARCHAR2
5367   ,p_validation_level    IN  NUMBER
5368   ,p_caller_type         IN  VARCHAR2
5369   ,p_line_index          IN  NUMBER
5370   ,p_resale_line_int_rec IN  OZF_RESALE_COMMON_PVT.g_interface_rec_csr%ROWTYPE
5371   ,p_resale_header_rec   IN  OZF_RESALE_HEADERS%ROWTYPE
5372   ,p_resale_line_rec     IN  OZF_RESALE_LINES%ROWTYPE
5373   ,x_return_status       OUT NOCOPY VARCHAR2
5374   ,x_msg_count           OUT NOCOPY NUMBER
5375   ,x_msg_data            OUT NOCOPY VARCHAR2
5376 )
5377 IS
5378 l_api_name          CONSTANT VARCHAR2(30) := 'Build_Global_Resale_Rec';
5379 l_api_version       CONSTANT NUMBER := 1.0;
5380 l_full_name         CONSTANT VARCHAR2(60) := G_PKG_NAME ||'.'|| l_api_name;
5381 --
5382 l_qp_context_request_id      NUMBER;
5383 
5384 BEGIN
5385    -- Debug Message
5386    IF OZF_DEBUG_HIGH_ON THEN
5387       ozf_utility_pvt.debug_message(l_full_name||': start');
5388    END IF;
5389 
5390    IF NOT FND_API.Compatible_API_Call (
5391       l_api_version,
5392       p_api_version,
5393       l_api_name,
5394       G_PKG_NAME)
5395    THEN
5396       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5397    END IF;
5398 
5399    --Initialize message list if p_init_msg_list is TRUE.
5400    IF FND_API.To_Boolean (p_init_msg_list) THEN
5401       FND_MSG_PUB.initialize;
5402    END IF;
5403 
5404    x_return_status := FND_API.G_RET_STS_SUCCESS;
5405 
5406    IF p_caller_type = 'IFACE' AND
5407       p_resale_line_int_rec.resale_line_int_id IS NULL THEN
5408       IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
5409          FND_MESSAGE.set_name('OZF', 'OZF_RESALE_INT_RECD_NULL');
5410          FND_MSG_PUB.add;
5411       END IF;
5412       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5413    END IF;
5414 
5415    IF p_caller_type = 'RESALE' AND
5416       p_resale_line_rec.resale_line_id IS NULL THEN
5417       IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
5418          FND_MESSAGE.set_name('OZF', 'OZF_RESALE_RECD_NULL');
5419          FND_MSG_PUB.add;
5420       END IF;
5421       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5422    END IF;
5423 
5424    l_qp_context_request_id := QP_Price_Request_Context.Get_Request_Id;
5425 
5426    IF p_caller_type = 'IFACE' THEN
5427       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.qp_context_request_id          := l_qp_context_request_id;
5428       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_index                     := p_line_index;
5429       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.resale_table_type              := 'IFACE';
5430       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_id                        := p_resale_line_int_rec.resale_line_int_id;
5431       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.resale_transfer_type           := p_resale_line_int_rec.resale_transfer_type;
5432       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.product_transfer_movement_type := p_resale_line_int_rec.product_transfer_movement_type;
5433       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.product_transfer_date          := p_resale_line_int_rec.product_transfer_date;
5434       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.tracing_flag                   := p_resale_line_int_rec.tracing_flag;
5435       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.sold_from_cust_account_id      := p_resale_line_int_rec.sold_from_cust_account_id;
5436       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.sold_from_site_id              := p_resale_line_int_rec.sold_from_site_id;
5437       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.sold_from_contact_party_id     := p_resale_line_int_rec.sold_from_contact_party_id;
5438       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.ship_from_cust_account_id      := p_resale_line_int_rec.ship_from_cust_account_id;
5439       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.ship_from_site_id              := p_resale_line_int_rec.ship_from_site_id;
5440       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.ship_from_contact_party_id     := p_resale_line_int_rec.ship_from_contact_party_id;
5441       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.bill_to_party_id               := p_resale_line_int_rec.bill_to_party_id;
5442       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.bill_to_party_site_id          := p_resale_line_int_rec.bill_to_party_site_id;
5443       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.bill_to_contact_party_id       := p_resale_line_int_rec.bill_to_contact_party_id;
5444       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.ship_to_party_id               := p_resale_line_int_rec.ship_to_party_id;
5445       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.ship_to_party_site_id          := p_resale_line_int_rec.ship_to_party_site_id;
5446       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.ship_to_contact_party_id       := p_resale_line_int_rec.ship_to_contact_party_id;
5447       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.end_cust_party_id              := p_resale_line_int_rec.end_cust_party_id;
5448       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.end_cust_site_use_id           := p_resale_line_int_rec.end_cust_site_use_id;
5449       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.end_cust_site_use_code         := p_resale_line_int_rec.end_cust_site_use_code;
5450       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.end_cust_party_site_id         := p_resale_line_int_rec.end_cust_party_site_id;
5451       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.end_cust_contact_party_id      := p_resale_line_int_rec.end_cust_contact_party_id;
5452       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.data_source_code               := p_resale_line_int_rec.data_source_code;
5453       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute_category      := p_resale_line_int_rec.header_attribute_category;
5454       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute1              := p_resale_line_int_rec.header_attribute1;
5455       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute2              := p_resale_line_int_rec.header_attribute2;
5456       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute3              := p_resale_line_int_rec.header_attribute3;
5457       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute4              := p_resale_line_int_rec.header_attribute4;
5458       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute5              := p_resale_line_int_rec.header_attribute5;
5459       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute6              := p_resale_line_int_rec.header_attribute6;
5460       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute7              := p_resale_line_int_rec.header_attribute7;
5461       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute8              := p_resale_line_int_rec.header_attribute8;
5462       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute9              := p_resale_line_int_rec.header_attribute9;
5463       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute10             := p_resale_line_int_rec.header_attribute10;
5464       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute11             := p_resale_line_int_rec.header_attribute11;
5465       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute12             := p_resale_line_int_rec.header_attribute12;
5466       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute13             := p_resale_line_int_rec.header_attribute13;
5467       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute14             := p_resale_line_int_rec.header_attribute14;
5468       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute15             := p_resale_line_int_rec.header_attribute15;
5469       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute_category        := p_resale_line_int_rec.line_attribute_category;
5470       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute1                := p_resale_line_int_rec.line_attribute1;
5471       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute2                := p_resale_line_int_rec.line_attribute2;
5472       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute3                := p_resale_line_int_rec.line_attribute3;
5473       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute4                := p_resale_line_int_rec.line_attribute4;
5474       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute5                := p_resale_line_int_rec.line_attribute5;
5475       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute6                := p_resale_line_int_rec.line_attribute6;
5476       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute7                := p_resale_line_int_rec.line_attribute7;
5477       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute8                := p_resale_line_int_rec.line_attribute8;
5478       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute9                := p_resale_line_int_rec.line_attribute9;
5479       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute10               := p_resale_line_int_rec.line_attribute10;
5480       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute11               := p_resale_line_int_rec.line_attribute11;
5481       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute12               := p_resale_line_int_rec.line_attribute12;
5482       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute13               := p_resale_line_int_rec.line_attribute13;
5483       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute14               := p_resale_line_int_rec.line_attribute14;
5484       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute15               := p_resale_line_int_rec.line_attribute15;
5485 
5486    ELSIF p_caller_type = 'RESALE' THEN
5487       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.qp_context_request_id          := l_qp_context_request_id;
5488       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_index                     := p_line_index;
5489       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.resale_table_type              := 'RESALE';
5490       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_id                        := p_resale_line_rec.resale_line_id;
5491       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.resale_transfer_type           := p_resale_line_rec.resale_transfer_type;
5492       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.product_transfer_movement_type := p_resale_line_rec.product_transfer_movement_type;
5493       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.product_transfer_date          := p_resale_line_rec.product_transfer_date;
5494       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.tracing_flag                   := p_resale_line_rec.tracing_flag;
5495       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.sold_from_cust_account_id      := p_resale_line_rec.sold_from_cust_account_id;
5496       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.sold_from_site_id              := p_resale_line_rec.sold_from_site_id;
5497       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.sold_from_contact_party_id     := p_resale_line_rec.sold_from_contact_party_id;
5498       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.ship_from_cust_account_id      := p_resale_line_rec.ship_from_cust_account_id;
5499       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.ship_from_site_id              := p_resale_line_rec.ship_from_site_id;
5500       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.ship_from_contact_party_id     := p_resale_line_rec.ship_from_contact_party_id;
5501       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.bill_to_party_id               := p_resale_line_rec.bill_to_party_id;
5502       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.bill_to_party_site_id          := p_resale_line_rec.bill_to_party_site_id;
5503       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.bill_to_contact_party_id       := p_resale_line_rec.bill_to_contact_party_id;
5504       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.ship_to_party_id               := p_resale_line_rec.ship_to_party_id;
5505       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.ship_to_party_site_id          := p_resale_line_rec.ship_to_party_site_id;
5506       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.ship_to_contact_party_id       := p_resale_line_rec.ship_to_contact_party_id;
5507       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.end_cust_party_id              := p_resale_line_rec.end_cust_party_id;
5508       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.end_cust_site_use_id           := p_resale_line_rec.end_cust_site_use_id;
5509       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.end_cust_site_use_code         := p_resale_line_rec.end_cust_site_use_code;
5510       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.end_cust_party_site_id         := p_resale_line_rec.end_cust_party_site_id;
5511       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.end_cust_contact_party_id      := p_resale_line_rec.end_cust_contact_party_id;
5512       --OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.data_source_code               := ?
5513       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute_category      := p_resale_header_rec.header_attribute_category;
5514       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute1              := p_resale_header_rec.header_attribute1;
5515       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute2              := p_resale_header_rec.header_attribute2;
5516       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute3              := p_resale_header_rec.header_attribute3;
5517       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute4              := p_resale_header_rec.header_attribute4;
5518       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute5              := p_resale_header_rec.header_attribute5;
5519       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute6              := p_resale_header_rec.header_attribute6;
5520       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute7              := p_resale_header_rec.header_attribute7;
5521       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute8              := p_resale_header_rec.header_attribute8;
5522       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute9              := p_resale_header_rec.header_attribute9;
5523       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute10             := p_resale_header_rec.header_attribute10;
5524       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute11             := p_resale_header_rec.header_attribute11;
5525       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute12             := p_resale_header_rec.header_attribute12;
5526       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute13             := p_resale_header_rec.header_attribute13;
5527       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute14             := p_resale_header_rec.header_attribute14;
5528       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.header_attribute15             := p_resale_header_rec.header_attribute15;
5529       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute_category        := p_resale_line_rec.line_attribute_category;
5530       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute1                := p_resale_line_rec.line_attribute1;
5531       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute2                := p_resale_line_rec.line_attribute2;
5532       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute3                := p_resale_line_rec.line_attribute3;
5533       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute4                := p_resale_line_rec.line_attribute4;
5534       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute5                := p_resale_line_rec.line_attribute5;
5535       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute6                := p_resale_line_rec.line_attribute6;
5536       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute7                := p_resale_line_rec.line_attribute7;
5537       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute8                := p_resale_line_rec.line_attribute8;
5538       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute9                := p_resale_line_rec.line_attribute9;
5539       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute10               := p_resale_line_rec.line_attribute10;
5540       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute11               := p_resale_line_rec.line_attribute11;
5541       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute12               := p_resale_line_rec.line_attribute12;
5542       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute13               := p_resale_line_rec.line_attribute13;
5543       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute14               := p_resale_line_rec.line_attribute14;
5544       OZF_ORDER_PRICE_PVT.G_RESALE_LINE_REC.line_attribute15               := p_resale_line_rec.line_attribute15;
5545    END IF;
5546 
5547 
5548    FND_MSG_PUB.Count_And_Get (
5549      p_encoded => FND_API.G_FALSE,
5550      p_count   => x_msg_count,
5551      p_data    => x_msg_data
5552    );
5553 
5554    -- Debug Message
5555    IF OZF_DEBUG_HIGH_ON THEN
5556       ozf_utility_pvt.debug_message(l_full_name||': end');
5557    END IF;
5558 EXCEPTION
5559    WHEN FND_API.G_EXC_ERROR THEN
5560       x_return_status := FND_API.G_RET_STS_ERROR;
5561       FND_MSG_PUB.Count_And_Get (
5562          p_encoded => FND_API.G_FALSE,
5563          p_count   => x_msg_count,
5564          p_data    => x_msg_data
5565       );
5566    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5567       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5568       FND_MSG_PUB.Count_And_Get (
5569          p_encoded => FND_API.G_FALSE,
5570          p_count   => x_msg_count,
5571          p_data    => x_msg_data
5572       );
5573    WHEN OTHERS THEN
5574       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5575       FND_MSG_PUB.Count_And_Get (
5576          p_encoded => FND_API.G_FALSE,
5577          p_count   => x_msg_count,
5578          p_data    => x_msg_data
5579       );
5580 END Build_Global_Resale_Rec;
5581 
5582 -------------------------------------------------------------------------------
5583 -- PROCEDURE
5584 --    Derive_Orig_Parties
5585 --
5586 -- PURPOSE
5587 --    This procedure derives Bill_To, Ship_To and End_Cust Party information.
5588 --
5589 -- PARAMETERS
5590 --    p_resale_batch_id IN NUMBER
5591 --    p_partner_party_id IN NUMBER
5592 --    x_return_status OUT NOCOPY VARCHAR2
5593 --
5594 -- NOTES
5595 --
5596 -- HISTORY
5597 --    06-May-2009  ateotia   Created.
5598 --                           Bug# 8489216 fixed.
5599 -------------------------------------------------------------------------------
5600 PROCEDURE Derive_Orig_Parties (
5601     p_api_version       IN   NUMBER
5602    ,p_init_msg_list     IN   VARCHAR2 := FND_API.G_FALSE
5603    ,p_commit            IN   VARCHAR2 := FND_API.G_FALSE
5604    ,p_validation_level  IN   NUMBER   := FND_API.G_VALID_LEVEL_FULL
5605    ,p_resale_batch_id   IN   NUMBER
5606    ,p_partner_party_id  IN   NUMBER
5607    ,x_return_status     OUT  NOCOPY   VARCHAR2
5608    ,x_msg_data          OUT  NOCOPY   VARCHAR2
5609    ,x_msg_count         OUT  NOCOPY   NUMBER
5610 )
5611 IS
5612 l_api_name      CONSTANT VARCHAR2(30) := 'Derive_Orig_Parties';
5613 l_api_version   CONSTANT NUMBER       := 1.0;
5614 l_full_name     CONSTANT VARCHAR2(60) := G_PKG_NAME ||'.'|| l_api_name;
5615 l_return_status VARCHAR2(30);
5616 l_msg_data      VARCHAR2(2000);
5617 l_msg_count     NUMBER;
5618 
5619 BEGIN
5620    -- Standard BEGIN of API savepoint
5621    SAVEPOINT  Derive_Orig_Parties;
5622 
5623    -- Standard call to check for call compatibility.
5624    IF NOT FND_API.Compatible_API_Call (
5625         l_api_version,
5626         p_api_version,
5627         l_api_name,
5628         G_PKG_NAME)
5629    THEN
5630       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5631    END IF;
5632 
5633    --Initialize message LIST IF p_init_msg_LIST IS TRUE.
5634    IF FND_API.To_Boolean (p_init_msg_LIST) THEN
5635       FND_MSG_PUB.initialize;
5636    END IF;
5637 
5638    -- Debug Message
5639    IF OZF_DEBUG_HIGH_ON THEN
5640       OZF_UTILITY_PVT.debug_message(l_full_name||': Start');
5641    END IF;
5642 
5643    x_return_status := FND_API.g_ret_sts_success;
5644 
5645    OZF_RESALE_COMMON_PVT.Derive_Bill_To_Party
5646    (  p_api_version      => 1.0
5647      ,p_init_msg_list    => FND_API.G_FALSE
5648      ,p_commit           => FND_API.G_FALSE
5649      ,p_validation_level => FND_API.G_VALID_LEVEL_FULL
5650      ,p_resale_batch_id  => p_resale_batch_id
5651      ,p_partner_party_id => p_partner_party_id
5652      ,x_return_status    => l_return_status
5653      ,x_msg_data         => l_msg_data
5654      ,x_msg_count        => l_msg_count
5655    );
5656    IF l_return_status = FND_API.G_RET_STS_ERROR THEN
5657       RAISE FND_API.G_EXC_ERROR;
5658    ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5659       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5660    END IF;
5661 
5662    OZF_RESALE_COMMON_PVT.Derive_Ship_To_Party
5663    (  p_api_version      => 1.0
5664      ,p_init_msg_list    => FND_API.G_FALSE
5665      ,p_commit           => FND_API.G_FALSE
5666      ,p_validation_level => FND_API.G_VALID_LEVEL_FULL
5667      ,p_resale_batch_id  => p_resale_batch_id
5668      ,p_partner_party_id => p_partner_party_id
5669      ,x_return_status    => l_return_status
5670      ,x_msg_data         => l_msg_data
5671      ,x_msg_count        => l_msg_count
5672    );
5673    IF l_return_status = FND_API.G_RET_STS_ERROR THEN
5674       RAISE FND_API.G_EXC_ERROR;
5675    ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5676       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5677    END IF;
5678 
5679    OZF_RESALE_COMMON_PVT.Derive_End_Cust_Party
5680    (  p_api_version      => 1.0
5681      ,p_init_msg_list    => FND_API.G_FALSE
5682      ,p_commit           => FND_API.G_FALSE
5683      ,p_validation_level => FND_API.G_VALID_LEVEL_FULL
5684      ,p_resale_batch_id  => p_resale_batch_id
5685      ,p_partner_party_id => p_partner_party_id
5686      ,x_return_status    => l_return_status
5687      ,x_msg_data         => l_msg_data
5688      ,x_msg_count        => l_msg_count
5689    );
5690    IF l_return_status = FND_API.G_RET_STS_ERROR THEN
5691       RAISE FND_API.G_EXC_ERROR;
5692    ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5693       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5694    END IF;
5695 
5696    -- Debug Message
5697    IF OZF_DEBUG_HIGH_ON THEN
5698       OZF_UTILITY_PVT.debug_message(l_full_name||': End');
5699    END IF;
5700 
5701    --Standard call to get message count AND IF count=1, get the message
5702    FND_MSG_PUB.Count_And_Get (
5703         p_encoded => FND_API.G_FALSE,
5704         p_count => x_msg_count,
5705         p_data  => x_msg_data);
5706 
5707 EXCEPTION
5708    WHEN FND_API.G_EXC_ERROR THEN
5709       ROLLBACK TO Derive_Orig_Parties;
5710       x_return_status := FND_API.G_RET_STS_ERROR;
5711       -- Standard call to get message count and if count=1, get the message
5712       FND_MSG_PUB.Count_And_Get (
5713             p_encoded => FND_API.G_FALSE,
5714             p_count   => x_msg_count,
5715             p_data    => x_msg_data
5716       );
5717    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5718       ROLLBACK TO Derive_Orig_Parties;
5719       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5720       -- Standard call to get message count and if count=1, get the message
5721       FND_MSG_PUB.Count_And_Get (
5722             p_encoded => FND_API.G_FALSE,
5723             p_count   => x_msg_count,
5724             p_data    => x_msg_data
5725       );
5726    WHEN OTHERS THEN
5727       ROLLBACK TO Derive_Orig_Parties;
5728       IF OZF_DEBUG_LOW_ON THEN
5729          OZF_UTILITY_PVT.debug_message('SQLERRM '|| sqlerrm);
5730       END IF;
5731       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5732       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
5733       THEN
5734          FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
5735       END IF;
5736       -- Standard call to get message count and if count=1, get the message
5737       FND_MSG_PUB.Count_And_Get (
5738             p_encoded => FND_API.G_FALSE,
5739             p_count   => x_msg_count,
5740             p_data    => x_msg_data
5741       );
5742 END Derive_Orig_Parties;
5743 
5744 -------------------------------------------------------------------------------
5745 -- PROCEDURE
5746 --    Derive_Bill_To_Party
5747 --
5748 -- PURPOSE
5749 --    This procedure derives Bill_To Party, Party_Site, Party_Site_Use and
5750 --    Relationship.
5751 --
5752 -- PARAMETERS
5753 --    p_resale_batch_id IN NUMBER
5754 --    p_partner_party_id IN NUMBER
5755 --    x_return_status OUT NOCOPY VARCHAR2
5756 --
5757 -- NOTES
5758 --
5759 --
5760 -- HISTORY
5761 --    06-May-2009  ateotia   Created.
5762 --                           Bug# 8489216 fixed.
5763 -------------------------------------------------------------------------------
5764 PROCEDURE Derive_Bill_To_Party (
5765     p_api_version       IN   NUMBER
5766    ,p_init_msg_list     IN   VARCHAR2 := FND_API.G_FALSE
5767    ,p_commit            IN   VARCHAR2 := FND_API.G_FALSE
5768    ,p_validation_level  IN   NUMBER   := FND_API.G_VALID_LEVEL_FULL
5769    ,p_resale_batch_id   IN   NUMBER
5770    ,p_partner_party_id  IN   NUMBER
5771    ,x_return_status     OUT  NOCOPY   VARCHAR2
5772    ,x_msg_data          OUT  NOCOPY   VARCHAR2
5773    ,x_msg_count         OUT  NOCOPY   NUMBER
5774 )
5775 IS
5776 l_api_name              CONSTANT VARCHAR2(30) := 'Derive_Bill_To_Party';
5777 l_api_version           CONSTANT NUMBER       := 1.0;
5778 l_full_name             CONSTANT VARCHAR2(60) := G_PKG_NAME ||'.'|| l_api_name;
5779 l_return_status         VARCHAR2(30);
5780 l_msg_data              VARCHAR2(2000);
5781 l_msg_count             NUMBER;
5782 l_new_party_rec         OZF_RESALE_COMMON_PVT.party_rec_type;
5783 l_orig_billto_count     NUMBER;
5784 l_exist_billto_party_id NUMBER;
5785 
5786 CURSOR csr_orig_billto_count (cv_resale_batch_id IN NUMBER) IS
5787    SELECT COUNT(DISTINCT bill_to_party_name)
5788    FROM ozf_resale_lines_int_all
5789    WHERE resale_batch_id = cv_resale_batch_id
5790    AND status_code = 'PROCESSED'
5791    AND ( duplicated_line_id IS NULL
5792          OR ( duplicated_line_id IS NOT NULL AND duplicated_adjustment_id = -1)
5793        )
5794    AND bill_to_party_id IS NULL
5795    AND bill_to_cust_account_id IS NULL
5796    AND bill_to_party_name IS NOT NULL;
5797 
5798 CURSOR csr_exist_billto_party_id (cv_resale_batch_id IN NUMBER, cv_billto_party_name IN VARCHAR2) IS
5799    SELECT bill_to_party_id
5800    FROM ozf_resale_lines_int_all
5801    WHERE resale_batch_id = cv_resale_batch_id
5802    AND bill_to_party_name = cv_billto_party_name
5803    AND status_code = 'PROCESSED'
5804    AND ( duplicated_line_id IS NULL
5805          OR ( duplicated_line_id IS NOT NULL AND duplicated_adjustment_id = -1)
5806        )
5807    AND bill_to_party_id IS NOT NULL
5808    GROUP BY bill_to_party_id
5809    ORDER BY bill_to_party_id;
5810 
5811 CURSOR csr_orig_billto_cust (cv_resale_batch_id IN NUMBER) IS
5812    SELECT DISTINCT bill_to_party_name
5813         , bill_to_address
5814         , bill_to_city
5815         , bill_to_state
5816         , bill_to_postal_code
5817         , bill_to_country
5818    FROM ozf_resale_lines_int_all
5819    WHERE resale_batch_id = cv_resale_batch_id
5820    AND status_code = 'PROCESSED'
5821    AND ( duplicated_line_id IS NULL
5822          OR ( duplicated_line_id IS NOT NULL AND duplicated_adjustment_id = -1)
5823        )
5824    AND bill_to_party_id IS NULL
5825    AND bill_to_cust_account_id IS NULL
5826    AND bill_to_party_name IS NOT NULL;
5827 
5828 TYPE orig_billto_cust_tbl_type IS TABLE OF csr_orig_billto_cust%ROWTYPE INDEX BY BINARY_INTEGER;
5829 l_orig_billto_cust_tbl orig_billto_cust_tbl_type;
5830 
5831 BEGIN
5832    -- Standard BEGIN of API savepoint
5833    SAVEPOINT  Derive_Bill_To_Party;
5834 
5835    -- Standard call to check for call compatibility.
5836    IF NOT FND_API.Compatible_API_Call (
5837         l_api_version,
5838         p_api_version,
5839         l_api_name,
5840         G_PKG_NAME)
5841    THEN
5842       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5843    END IF;
5844 
5845    --Initialize message LIST IF p_init_msg_LIST IS TRUE.
5846    IF FND_API.To_Boolean (p_init_msg_LIST) THEN
5847       FND_MSG_PUB.initialize;
5848    END IF;
5849 
5850    -- Debug Message
5851    IF OZF_DEBUG_HIGH_ON THEN
5852       OZF_UTILITY_PVT.debug_message(l_full_name||': Start');
5853    END IF;
5854 
5855    x_return_status := FND_API.g_ret_sts_success;
5856 
5857    OPEN  csr_orig_billto_count (p_resale_batch_id);
5858    FETCH csr_orig_billto_count INTO l_orig_billto_count;
5859    CLOSE csr_orig_billto_count;
5860 
5861    OPEN  csr_orig_billto_cust(p_resale_batch_id);
5862    FETCH csr_orig_billto_cust BULK COLLECT INTO l_orig_billto_cust_tbl;
5863    CLOSE csr_orig_billto_cust;
5864 
5865    IF l_orig_billto_cust_tbl.COUNT > 0 THEN
5866       FOR i IN 1..l_orig_billto_cust_tbl.COUNT
5867       LOOP
5868          l_new_party_rec                  := NULL;
5869          l_exist_billto_party_id          := NULL;
5870          l_new_party_rec.partner_party_id := p_partner_party_id;
5871          l_new_party_rec.name             := l_orig_billto_cust_tbl(i).bill_to_party_name;
5872          l_new_party_rec.address          := l_orig_billto_cust_tbl(i).bill_to_address;
5873          l_new_party_rec.city             := l_orig_billto_cust_tbl(i).bill_to_city;
5874          l_new_party_rec.state            := l_orig_billto_cust_tbl(i).bill_to_state;
5875          l_new_party_rec.postal_Code      := l_orig_billto_cust_tbl(i).bill_to_postal_code;
5876          l_new_party_rec.country          := l_orig_billto_cust_tbl(i).bill_to_country;
5877          l_new_party_rec.site_Use_Code    := 'BILL_TO';
5878 
5879          IF (l_orig_billto_count <> l_orig_billto_cust_tbl.COUNT) THEN
5880             --Check whether a party with this name has already been already created.
5881             OPEN  csr_exist_billto_party_id (p_resale_batch_id, l_orig_billto_cust_tbl(i).bill_to_party_name);
5882             FETCH csr_exist_billto_party_id INTO l_exist_billto_party_id;
5883             CLOSE csr_exist_billto_party_id;
5884             IF (l_exist_billto_party_id IS NOT NULL) THEN
5885                --New Party and Relationship should not be created.
5886                --Derive the party_id from existing party.
5887                l_new_party_rec.party_id         := l_exist_billto_party_id;
5888                l_new_party_rec.name             := NULL;
5889                l_new_party_rec.partner_party_id := NULL;
5890             END IF;
5891          END IF;
5892 
5893          OZF_RESALE_COMMON_PVT.Create_Party(
5894             p_api_version     => 1.0
5895            ,p_init_msg_list   => FND_API.G_FALSE
5896            ,p_commit          => FND_API.G_FALSE
5897            ,p_validation_level=> FND_API.G_VALID_LEVEL_FULL
5898            ,px_party_rec      => l_new_party_rec
5899            ,x_return_status   => l_return_status
5900            ,x_msg_data        => l_msg_data
5901            ,x_msg_count       => l_msg_count
5902          );
5903          IF l_return_status = FND_API.G_RET_STS_ERROR THEN
5904             RAISE FND_API.G_EXC_ERROR;
5905          ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5906             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5907          END IF;
5908 
5909          --Update bill_to_party_id, bill_to_party_site_id and bill_to_site_use_id
5910          UPDATE ozf_resale_lines_int_all
5911          SET bill_to_party_id      = l_new_party_rec.party_id,
5912              bill_to_party_site_id = l_new_party_rec.party_site_id,
5913              bill_to_site_use_id   = l_new_party_rec.party_site_use_id
5914          WHERE bill_to_party_id IS NULL
5915          AND bill_to_cust_account_id IS NULL
5916          AND resale_batch_id            = p_resale_batch_id
5917          AND bill_to_party_name         = l_orig_billto_cust_tbl(i).bill_to_party_name
5918          AND NVL(bill_to_address,1)     = NVL(l_orig_billto_cust_tbl(i).bill_to_address,1)
5919          AND NVL(bill_to_city,1)        = NVL(l_orig_billto_cust_tbl(i).bill_to_city,1)
5920          AND NVL(bill_to_state,1)       = NVL(l_orig_billto_cust_tbl(i).bill_to_state,1)
5921          AND NVL(bill_to_postal_code,1) = NVL(l_orig_billto_cust_tbl(i).bill_to_postal_code,1)
5922          AND NVL(bill_to_country,1)     = NVL(l_orig_billto_cust_tbl(i).bill_to_country,1);
5923 
5924       END LOOP;
5925    END IF;
5926 
5927  -- Debug Message
5928  IF OZF_DEBUG_HIGH_ON THEN
5929     OZF_UTILITY_PVT.debug_message(l_full_name||': End');
5930  END IF;
5931 
5932  --Standard call to get message count AND IF count=1, get the message
5933  FND_MSG_PUB.Count_And_Get (
5934     p_encoded => FND_API.G_FALSE,
5935     p_count => x_msg_count,
5936     p_data  => x_msg_data);
5937 
5938 EXCEPTION
5939    WHEN FND_API.G_EXC_ERROR THEN
5940       ROLLBACK TO Derive_Bill_To_Party;
5941       x_return_status := FND_API.G_RET_STS_ERROR;
5942       -- Standard call to get message count and if count=1, get the message
5943       FND_MSG_PUB.Count_And_Get (
5944             p_encoded => FND_API.G_FALSE,
5945             p_count   => x_msg_count,
5946             p_data    => x_msg_data
5947       );
5948    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
5949       ROLLBACK TO Derive_Bill_To_Party;
5950       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5951       -- Standard call to get message count and if count=1, get the message
5952       FND_MSG_PUB.Count_And_Get (
5953             p_encoded => FND_API.G_FALSE,
5954             p_count   => x_msg_count,
5955             p_data    => x_msg_data
5956       );
5957    WHEN OTHERS THEN
5958       ROLLBACK TO Derive_Bill_To_Party;
5959       IF OZF_DEBUG_LOW_ON THEN
5960          OZF_UTILITY_PVT.debug_message('SQLERRM '|| sqlerrm);
5961       END IF;
5962       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
5963       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
5964       THEN
5965          FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
5966       END IF;
5967       -- Standard call to get message count and if count=1, get the message
5968       FND_MSG_PUB.Count_And_Get (
5969             p_encoded => FND_API.G_FALSE,
5970             p_count   => x_msg_count,
5971             p_data    => x_msg_data
5972       );
5973 END Derive_Bill_To_Party;
5974 
5975 -------------------------------------------------------------------------------
5976 -- PROCEDURE
5977 --    Derive_Ship_To_Party
5978 --
5979 -- PURPOSE
5980 --    This procedure derives Ship_To Party, Party_Site, Party_Site_Use and
5981 --    Relationship.
5982 --
5983 -- PARAMETERS
5984 --    p_resale_batch_id IN NUMBER
5985 --    p_partner_party_id IN NUMBER
5986 --    x_return_status OUT NOCOPY VARCHAR2
5987 --
5988 -- NOTES
5989 --
5990 --
5991 -- HISTORY
5992 --    06-May-2009  ateotia   Created.
5993 --                           Bug# 8489216 fixed.
5994 -------------------------------------------------------------------------------
5995 PROCEDURE Derive_Ship_To_Party (
5996     p_api_version       IN   NUMBER
5997    ,p_init_msg_list     IN   VARCHAR2 := FND_API.G_FALSE
5998    ,p_commit            IN   VARCHAR2 := FND_API.G_FALSE
5999    ,p_validation_level  IN   NUMBER   := FND_API.G_VALID_LEVEL_FULL
6000    ,p_resale_batch_id   IN   NUMBER
6001    ,p_partner_party_id  IN   NUMBER
6002    ,x_return_status     OUT  NOCOPY   VARCHAR2
6003    ,x_msg_data          OUT  NOCOPY   VARCHAR2
6004    ,x_msg_count         OUT  NOCOPY   NUMBER
6005 )
6006 IS
6007 l_api_name                   CONSTANT VARCHAR2(30) := 'Derive_Ship_To_Party';
6008 l_api_version                CONSTANT NUMBER       := 1.0;
6009 l_full_name                  CONSTANT VARCHAR2(60) := G_PKG_NAME ||'.'|| l_api_name;
6010 l_return_status              VARCHAR2(30);
6011 l_msg_data                   VARCHAR2(2000);
6012 l_msg_count                  NUMBER;
6013 l_new_party_rec              OZF_RESALE_COMMON_PVT.party_rec_type;
6014 l_orig_shipto_count          NUMBER;
6015 l_exist_shipto_party_id      NUMBER;
6016 l_exist_billto_party_id      NUMBER;
6017 l_exist_billto_party_site_id NUMBER;
6018 
6019 CURSOR csr_orig_shipto_count (cv_resale_batch_id IN NUMBER) IS
6020    SELECT COUNT(DISTINCT ship_to_party_name)
6021    FROM ozf_resale_lines_int_all
6022    WHERE resale_batch_id = cv_resale_batch_id
6023    AND status_code = 'PROCESSED'
6024    AND ( duplicated_line_id IS NULL
6025          OR ( duplicated_line_id IS NOT NULL AND duplicated_adjustment_id = -1)
6026        )
6027    AND ship_to_party_id IS NULL
6028    AND ship_to_cust_account_id IS NULL
6029    AND ship_to_party_name IS NOT NULL;
6030 
6031 CURSOR csr_exist_shipto_party_id (cv_resale_batch_id IN NUMBER, cv_shipto_party_name IN VARCHAR2) IS
6032    SELECT ship_to_party_id
6033    FROM ozf_resale_lines_int_all
6034    WHERE resale_batch_id = cv_resale_batch_id
6035    AND ship_to_party_name = cv_shipto_party_name
6036    AND status_code = 'PROCESSED'
6037    AND ( duplicated_line_id IS NULL
6038          OR ( duplicated_line_id IS NOT NULL AND duplicated_adjustment_id = -1)
6039        )
6040    AND ship_to_party_id IS NOT NULL
6041    GROUP BY ship_to_party_id
6042    ORDER BY ship_to_party_id;
6043 
6044 CURSOR csr_exist_billto_party_id (cv_resale_batch_id IN NUMBER, cv_billto_party_name IN VARCHAR2) IS
6045    SELECT bill_to_party_id
6046    FROM ozf_resale_lines_int_all
6047    WHERE resale_batch_id = cv_resale_batch_id
6048    AND bill_to_party_name = cv_billto_party_name
6049    AND status_code = 'PROCESSED'
6050    AND ( duplicated_line_id IS NULL
6051          OR ( duplicated_line_id IS NOT NULL AND duplicated_adjustment_id = -1)
6052        )
6053    AND bill_to_party_id IS NOT NULL
6054    GROUP BY bill_to_party_id
6055    ORDER BY bill_to_party_id;
6056 
6057 CURSOR csr_exist_billto_party_site_id
6058 (
6059    cv_resale_batch_id IN NUMBER,
6060    cv_billto_party_name IN VARCHAR2,
6061    cv_bill_to_address IN VARCHAR2,
6062    cv_bill_to_city IN VARCHAR2,
6063    cv_bill_to_state IN VARCHAR2,
6064    cv_bill_to_postal_code IN VARCHAR2,
6065    cv_bill_to_country IN VARCHAR2
6066 )  IS
6067    SELECT bill_to_party_site_id
6068    FROM ozf_resale_lines_int_all
6069    WHERE resale_batch_id = cv_resale_batch_id
6070    AND status_code = 'PROCESSED'
6071    AND ( duplicated_line_id IS NULL
6072          OR ( duplicated_line_id IS NOT NULL AND duplicated_adjustment_id = -1)
6073        )
6074    AND bill_to_party_id IS NOT NULL
6075    AND bill_to_party_site_id IS NOT NULL
6076    AND bill_to_party_name          = cv_billto_party_name
6077    AND NVL(bill_to_address,1)     = NVL(cv_bill_to_address,1)
6078    AND NVL(bill_to_city,1)        = NVL(cv_bill_to_city,1)
6079    AND NVL(bill_to_state,1)       = NVL(cv_bill_to_state,1)
6080    AND NVL(bill_to_postal_code,1) = NVL(cv_bill_to_postal_code,1)
6081    AND NVL(bill_to_country,1)     = NVL(cv_bill_to_country,1)
6082    GROUP BY bill_to_party_site_id
6083    ORDER BY bill_to_party_site_id;
6084 
6085 CURSOR csr_orig_shipto_cust (cv_resale_batch_id IN NUMBER) IS
6086    SELECT DISTINCT ship_to_party_name
6087         , ship_to_address
6088         , ship_to_city
6089         , ship_to_state
6090         , ship_to_postal_code
6091         , ship_to_country
6092    FROM ozf_resale_lines_int_all
6093    WHERE resale_batch_id = cv_resale_batch_id
6094    AND status_code = 'PROCESSED'
6095    AND ( duplicated_line_id IS NULL
6096          OR ( duplicated_line_id IS NOT NULL AND duplicated_adjustment_id = -1)
6097        )
6098    AND ship_to_party_id IS NULL
6099    AND ship_to_cust_account_id IS NULL
6100    AND ship_to_party_name IS NOT NULL;
6101 
6102 TYPE orig_shipto_cust_tbl_type IS TABLE OF csr_orig_shipto_cust%ROWTYPE INDEX BY BINARY_INTEGER;
6103 l_orig_shipto_cust_tbl orig_shipto_cust_tbl_type;
6104 
6105 BEGIN
6106    -- Standard BEGIN of API savepoint
6107    SAVEPOINT  Derive_ship_To_Party;
6108 
6109    -- Standard call to check for call compatibility.
6110    IF NOT FND_API.Compatible_API_Call (
6111         l_api_version,
6112         p_api_version,
6113         l_api_name,
6114         G_PKG_NAME)
6115    THEN
6116       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6117    END IF;
6118 
6119    --Initialize message LIST IF p_init_msg_LIST IS TRUE.
6120    IF FND_API.To_Boolean (p_init_msg_LIST) THEN
6121       FND_MSG_PUB.initialize;
6122 
6123    END IF;
6124    -- Debug Message
6125    IF OZF_DEBUG_HIGH_ON THEN
6126       OZF_UTILITY_PVT.debug_message(l_full_name||': Start');
6127    END IF;
6128 
6129    x_return_status := FND_API.g_ret_sts_success;
6130 
6131    OPEN  csr_orig_shipto_count (p_resale_batch_id);
6132    FETCH csr_orig_shipto_count INTO l_orig_shipto_count;
6133    CLOSE csr_orig_shipto_count;
6134 
6135    OPEN  csr_orig_shipto_cust(p_resale_batch_id);
6136    FETCH csr_orig_shipto_cust BULK COLLECT INTO l_orig_shipto_cust_tbl;
6137    CLOSE csr_orig_shipto_cust;
6138 
6139    IF l_orig_shipto_cust_tbl.COUNT > 0 THEN
6140       FOR i IN 1..l_orig_shipto_cust_tbl.COUNT
6141       LOOP
6142          l_new_party_rec              := NULL;
6143          l_exist_shipto_party_id      := NULL;
6144          l_exist_billto_party_id      := NULL;
6145          l_exist_billto_party_site_id := NULL;
6146 
6147          --Derive ship_to_party_id from bill_to_party_id
6148          OPEN  csr_exist_billto_party_id (p_resale_batch_id, l_orig_shipto_cust_tbl(i).ship_to_party_name);
6149          FETCH csr_exist_billto_party_id INTO l_exist_billto_party_id;
6150          CLOSE csr_exist_billto_party_id;
6151          IF (l_exist_billto_party_id IS NOT NULL) THEN
6152             --New Party and Relationship should not be created.
6153             l_new_party_rec.party_id := l_exist_billto_party_id;
6154             l_new_party_rec.name := NULL;
6155             l_new_party_rec.partner_party_id := NULL;
6156             --Derive ship_to_party_site_id from bill_party_to_site_id
6157             OPEN  csr_exist_billto_party_site_id (
6158                p_resale_batch_id,
6159                l_orig_shipto_cust_tbl(i).ship_to_party_name,
6160                l_orig_shipto_cust_tbl(i).ship_to_address,
6161                l_orig_shipto_cust_tbl(i).ship_to_city,
6162                l_orig_shipto_cust_tbl(i).ship_to_state,
6163                l_orig_shipto_cust_tbl(i).ship_to_postal_code,
6164                l_orig_shipto_cust_tbl(i).ship_to_country
6165             );
6166             FETCH csr_exist_billto_party_site_id INTO l_exist_billto_party_site_id;
6167             CLOSE csr_exist_billto_party_site_id;
6168             IF (l_exist_billto_party_site_id IS NOT NULL) THEN
6169                --New Location and Party Site should not be created.
6170                l_new_party_rec.party_site_id := l_exist_billto_party_site_id;
6171                l_new_party_rec.address := NULL;
6172             ELSE
6173                --Create New Party Site
6174                l_new_party_rec.address := l_orig_shipto_cust_tbl(i).ship_to_address;
6175             END IF;
6176          ELSE
6177             --Create New Ship To Party
6178             l_new_party_rec.name := l_orig_shipto_cust_tbl(i).ship_to_party_name;
6179             l_new_party_rec.partner_party_id := p_partner_party_id;
6180             l_new_party_rec.address := l_orig_shipto_cust_tbl(i).ship_to_address;
6181          END IF;
6182 
6183          IF (l_exist_billto_party_id IS NULL
6184          AND l_orig_shipto_count <> l_orig_shipto_cust_tbl.COUNT) THEN
6185             --Check whether a party with this name has already been already created.
6186             OPEN  csr_exist_shipto_party_id (p_resale_batch_id, l_orig_shipto_cust_tbl(i).ship_to_party_name);
6187             FETCH csr_exist_shipto_party_id INTO l_exist_shipto_party_id;
6188             CLOSE csr_exist_shipto_party_id;
6189             IF (l_exist_shipto_party_id IS NOT NULL) THEN
6190                --New Party and Relationship should not be created.
6191                --Derive the party_id from existing party.
6192                l_new_party_rec.party_id         := l_exist_shipto_party_id;
6193                l_new_party_rec.name             := NULL;
6194                l_new_party_rec.partner_party_id := NULL;
6195             END IF;
6196          END IF;
6197 
6198          --Pass the following information anyway to create ship_to_site_use_id
6199          l_new_party_rec.city             := l_orig_shipto_cust_tbl(i).ship_to_city;
6200          l_new_party_rec.state            := l_orig_shipto_cust_tbl(i).ship_to_state;
6201          l_new_party_rec.postal_Code      := l_orig_shipto_cust_tbl(i).ship_to_postal_code;
6202          l_new_party_rec.country          := l_orig_shipto_cust_tbl(i).ship_to_country;
6203          l_new_party_rec.site_Use_Code    := 'SHIP_TO';
6204 
6205          OZF_RESALE_COMMON_PVT.Create_Party(
6206             p_api_version     => 1.0
6207            ,p_init_msg_list   => FND_API.G_FALSE
6208            ,p_commit          => FND_API.G_FALSE
6209            ,p_validation_level=> FND_API.G_VALID_LEVEL_FULL
6210            ,px_party_rec      => l_new_party_rec
6211            ,x_return_status   => l_return_status
6212            ,x_msg_data        => l_msg_data
6213            ,x_msg_count       => l_msg_count
6214          );
6215          IF l_return_status = FND_API.G_RET_STS_ERROR THEN
6216             RAISE FND_API.G_EXC_ERROR;
6217          ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6218             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6219          END IF;
6220 
6221          --Update ship_to_party_id, ship_to_party_site_id and ship_to_site_use_id
6222          UPDATE ozf_resale_lines_int_all
6223          SET ship_to_party_id      = l_new_party_rec.party_id,
6224              ship_to_party_site_id = l_new_party_rec.party_site_id,
6225              ship_to_site_use_id   = l_new_party_rec.party_site_use_id
6226          WHERE ship_to_party_id IS NULL
6227          AND ship_to_cust_account_id IS NULL
6228          AND resale_batch_id            = p_resale_batch_id
6229          AND ship_to_party_name         = l_orig_shipto_cust_tbl(i).ship_to_party_name
6230          AND NVL(ship_to_address,1)     = NVL(l_orig_shipto_cust_tbl(i).ship_to_address,1)
6231          AND NVL(ship_to_city,1)        = NVL(l_orig_shipto_cust_tbl(i).ship_to_city,1)
6232          AND NVL(ship_to_state,1)       = NVL(l_orig_shipto_cust_tbl(i).ship_to_state,1)
6233          AND NVL(ship_to_postal_code,1) = NVL(l_orig_shipto_cust_tbl(i).ship_to_postal_code,1)
6234          AND NVL(ship_to_country,1)     = NVL(l_orig_shipto_cust_tbl(i).ship_to_country,1);
6235 
6236       END LOOP;
6237    END IF;
6238 
6239  -- Debug Message
6240  IF OZF_DEBUG_HIGH_ON THEN
6241     OZF_UTILITY_PVT.debug_message(l_full_name||': End');
6242  END IF;
6243 
6244  --Standard call to get message count AND IF count=1, get the message
6245  FND_MSG_PUB.Count_And_Get (
6246     p_encoded => FND_API.G_FALSE,
6247     p_count => x_msg_count,
6248     p_data  => x_msg_data);
6249 
6250 EXCEPTION
6251    WHEN FND_API.G_EXC_ERROR THEN
6252       ROLLBACK TO Derive_Ship_To_Party;
6253       x_return_status := FND_API.G_RET_STS_ERROR;
6254       -- Standard call to get message count and if count=1, get the message
6255       FND_MSG_PUB.Count_And_Get (
6256             p_encoded => FND_API.G_FALSE,
6257             p_count   => x_msg_count,
6258             p_data    => x_msg_data
6259       );
6260    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6261       ROLLBACK TO Derive_Ship_To_Party;
6262       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6263       -- Standard call to get message count and if count=1, get the message
6264       FND_MSG_PUB.Count_And_Get (
6265             p_encoded => FND_API.G_FALSE,
6266             p_count   => x_msg_count,
6267             p_data    => x_msg_data
6268       );
6269    WHEN OTHERS THEN
6270       ROLLBACK TO Derive_Ship_To_Party;
6271       IF OZF_DEBUG_LOW_ON THEN
6272          OZF_UTILITY_PVT.debug_message('SQLERRM '|| sqlerrm);
6273       END IF;
6274       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6275       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
6276       THEN
6277          FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
6278       END IF;
6279       -- Standard call to get message count and if count=1, get the message
6280       FND_MSG_PUB.Count_And_Get (
6281             p_encoded => FND_API.G_FALSE,
6282             p_count   => x_msg_count,
6283             p_data    => x_msg_data
6284       );
6285 END Derive_Ship_To_Party;
6286 
6287 -------------------------------------------------------------------------------
6288 -- PROCEDURE
6289 --    Derive_End_Cust_Party
6290 --
6291 -- PURPOSE
6292 --    This procedure derives End Customer Party, Party_Site, Party_Site_Use and
6293 --    Relationship.
6294 --
6295 -- PARAMETERS
6296 --    p_resale_batch_id IN NUMBER
6297 --    p_partner_party_id IN NUMBER
6298 --    x_return_status OUT NOCOPY VARCHAR2
6299 --
6300 -- NOTES
6301 --
6302 --
6303 -- HISTORY
6304 --    06-May-2009  ateotia   Created.
6305 --                           Bug# 8489216 fixed.
6306 -------------------------------------------------------------------------------
6307 PROCEDURE Derive_End_Cust_Party (
6308     p_api_version            IN  NUMBER
6309    ,p_init_msg_list          IN  VARCHAR2 := FND_API.G_FALSE
6310    ,p_commit                 IN  VARCHAR2 := FND_API.G_FALSE
6311    ,p_validation_level       IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL
6312    ,p_resale_batch_id        IN  NUMBER
6313    ,p_partner_party_id       IN  NUMBER
6314    ,x_return_status          OUT NOCOPY   VARCHAR2
6315    ,x_msg_data               OUT NOCOPY   VARCHAR2
6316    ,x_msg_count              OUT NOCOPY   NUMBER
6317 )
6318 IS
6319 l_api_name                CONSTANT VARCHAR2(30) := 'Derive_End_Cust_Party';
6320 l_api_version             CONSTANT NUMBER       := 1.0;
6321 l_full_name               CONSTANT VARCHAR2(60) := G_PKG_NAME ||'.'|| l_api_name;
6322 l_return_status           VARCHAR2(30);
6323 l_msg_data                VARCHAR2(2000);
6324 l_msg_count               NUMBER;
6325 l_new_party_rec           OZF_RESALE_COMMON_PVT.party_rec_type;
6326 l_orig_end_cust_count     NUMBER;
6327 l_exist_end_cust_party_id NUMBER;
6328 
6329 CURSOR csr_orig_end_cust_count (cv_resale_batch_id IN NUMBER) IS
6330    SELECT COUNT(DISTINCT end_cust_party_name)
6331    FROM ozf_resale_lines_int_all
6332    WHERE resale_batch_id = cv_resale_batch_id
6333    AND status_code = 'PROCESSED'
6334    AND ( duplicated_line_id IS NULL
6335          OR ( duplicated_line_id IS NOT NULL AND duplicated_adjustment_id = -1)
6336        )
6337    AND end_cust_party_id IS NULL
6338    AND end_cust_party_name IS NOT NULL;
6339 
6340 CURSOR csr_exist_end_cust_party_id (cv_resale_batch_id IN NUMBER, cv_end_cust_party_name IN VARCHAR2) IS
6341    SELECT end_cust_party_id
6342    FROM ozf_resale_lines_int_all
6343    WHERE resale_batch_id = cv_resale_batch_id
6344    AND end_cust_party_name = cv_end_cust_party_name
6345    AND status_code = 'PROCESSED'
6346    AND ( duplicated_line_id IS NULL
6347          OR ( duplicated_line_id IS NOT NULL AND duplicated_adjustment_id = -1)
6348        )
6349    AND end_cust_party_id IS NOT NULL
6350    GROUP BY end_cust_party_id
6351    ORDER BY end_cust_party_id;
6352 
6353 CURSOR csr_orig_end_cust(cv_resale_batch_id IN NUMBER) IS
6354    SELECT DISTINCT end_cust_party_name
6355         , end_cust_address
6356         , end_cust_city
6357         , end_cust_state
6358         , end_cust_postal_code
6359         , end_cust_country
6360         , end_cust_site_use_code
6361    FROM ozf_resale_lines_int_all
6362    WHERE resale_batch_id = cv_resale_batch_id
6363    AND status_code = 'PROCESSED'
6364    AND ( duplicated_line_id IS NULL
6365          OR ( duplicated_line_id IS NOT NULL AND duplicated_adjustment_id = -1)
6366        )
6367    AND end_cust_party_id IS NULL
6368    AND end_cust_party_name IS NOT NULL;
6369 
6370 TYPE orig_end_cust_tbl_type IS TABLE of csr_orig_end_cust%ROWTYPE INDEX BY BINARY_INTEGER;
6371 l_orig_end_cust_tbl orig_end_cust_tbl_type;
6372 
6373 BEGIN
6374    -- Standard BEGIN of API savepoint
6375    SAVEPOINT  Derive_Bill_To_Party;
6376 
6377    -- Standard call to check for call compatibility.
6378    IF NOT FND_API.Compatible_API_Call (
6379         l_api_version,
6380         p_api_version,
6381         l_api_name,
6382         G_PKG_NAME)
6383    THEN
6384       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6385    END IF;
6386 
6387    --Initialize message LIST IF p_init_msg_LIST IS TRUE.
6388    IF FND_API.To_Boolean (p_init_msg_LIST) THEN
6389       FND_MSG_PUB.initialize;
6390    END IF;
6391 
6392    -- Debug Message
6393    IF OZF_DEBUG_HIGH_ON THEN
6394       OZF_UTILITY_PVT.debug_message(l_full_name||': Start');
6395    END IF;
6396 
6397    x_return_status := FND_API.g_ret_sts_success;
6398 
6399    OPEN csr_orig_end_cust_count (p_resale_batch_id);
6400    FETCH csr_orig_end_cust_count INTO l_orig_end_cust_count;
6401    CLOSE csr_orig_end_cust_count;
6402 
6403    OPEN csr_orig_end_cust(p_resale_batch_id);
6404    FETCH csr_orig_end_cust BULK COLLECT INTO l_orig_end_cust_tbl;
6405    CLOSE csr_orig_end_cust;
6406 
6407    IF l_orig_end_cust_tbl.COUNT > 0 THEN
6408       FOR i IN 1..l_orig_end_cust_tbl.COUNT
6409       LOOP
6410          l_new_party_rec                  := NULL;
6411          l_exist_end_cust_party_id        := NULL;
6412          l_new_party_rec.partner_party_id := p_partner_party_id;
6413          l_new_party_rec.name             := l_orig_end_cust_tbl(i).end_cust_party_name;
6414          l_new_party_rec.address          := l_orig_end_cust_tbl(i).end_cust_address;
6415          l_new_party_rec.city             := l_orig_end_cust_tbl(i).end_cust_city;
6416          l_new_party_rec.state            := l_orig_end_cust_tbl(i).end_cust_state;
6417          l_new_party_rec.postal_code      := l_orig_end_cust_tbl(i).end_cust_postal_code;
6418          l_new_party_rec.country          := l_orig_end_cust_tbl(i).end_cust_country;
6419          l_new_party_rec.site_use_code    := l_orig_end_cust_tbl(i).end_cust_site_use_code;
6420 
6421          IF (l_orig_end_cust_count <> l_orig_end_cust_tbl.COUNT) THEN
6422             --Check whether a party with this name has already been already created.
6423             OPEN csr_exist_end_cust_party_id (p_resale_batch_id, l_orig_end_cust_tbl(i).end_cust_party_name);
6424             FETCH csr_exist_end_cust_party_id INTO l_exist_end_cust_party_id;
6425             CLOSE csr_exist_end_cust_party_id;
6426             IF(l_exist_end_cust_party_id IS NOT NULL) THEN
6427                --New Party and Party Relationship should not be created.
6428                --Derive the party_id from existing party.
6429                l_new_party_rec.party_id         := l_exist_end_cust_party_id;
6430                l_new_party_rec.name             := NULL;
6431                l_new_party_rec.partner_party_id := NULL;
6432             END IF;
6433          END IF;
6434 
6435          OZF_RESALE_COMMON_PVT.Create_Party(
6436             p_api_version     => 1.0
6437            ,p_init_msg_list   => FND_API.G_FALSE
6438            ,p_commit          => FND_API.G_FALSE
6439            ,p_validation_level=> FND_API.G_VALID_LEVEL_FULL
6440            ,px_party_rec      => l_new_party_rec
6441            ,x_return_status   => l_return_status
6442            ,x_msg_data        => l_msg_data
6443            ,x_msg_count       => l_msg_count
6444          );
6445          IF l_return_status = FND_API.G_RET_STS_ERROR THEN
6446             RAISE FND_API.G_EXC_ERROR;
6447          ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
6448             RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
6449          END IF;
6450 
6451          --Update end_cust_party_id, end_cust_party_site_id and end_cust_site_use_id
6452          UPDATE ozf_resale_lines_int_all
6453          SET end_cust_party_id      = l_new_party_rec.party_id,
6454              end_cust_party_site_id = l_new_party_rec.party_site_id,
6455              end_cust_site_use_id   = l_new_party_rec.party_site_use_id
6456          WHERE end_cust_party_id IS NULL
6457          AND resale_batch_id               = p_resale_batch_id
6458          AND end_cust_party_name           = l_orig_end_cust_tbl(i).end_cust_party_name
6459          AND NVL(end_cust_address,1)       = NVL(l_orig_end_cust_tbl(i).end_cust_address,1)
6460          AND NVL(end_cust_city,1)          = NVL(l_orig_end_cust_tbl(i).end_cust_city,1)
6461          AND NVL(end_cust_state,1)         = NVL(l_orig_end_cust_tbl(i).end_cust_state,1)
6462          AND NVL(end_cust_postal_code,1)   = NVL(l_orig_end_cust_tbl(i).end_cust_postal_code,1)
6463          AND NVL(end_cust_country,1)       = NVL(l_orig_end_cust_tbl(i).end_cust_country,1)
6464          AND NVL(end_cust_site_use_code,1) = NVL(l_orig_end_cust_tbl(i).end_cust_site_use_code,1);
6465 
6466       END LOOP;
6467    END IF;
6468 
6469  -- Debug Message
6470  IF OZF_DEBUG_HIGH_ON THEN
6471     OZF_UTILITY_PVT.debug_message(l_full_name||': End');
6472  END IF;
6473 
6474  --Standard call to get message count AND IF count=1, get the message
6475  FND_MSG_PUB.Count_And_Get (
6476     p_encoded => FND_API.G_FALSE,
6477     p_count => x_msg_count,
6478     p_data  => x_msg_data);
6479 
6480 EXCEPTION
6481    WHEN FND_API.G_EXC_ERROR THEN
6482       ROLLBACK TO Derive_End_Cust_Party;
6483       x_return_status := FND_API.G_RET_STS_ERROR;
6484       -- Standard call to get message count and if count=1, get the message
6485       FND_MSG_PUB.Count_And_Get (
6486             p_encoded => FND_API.G_FALSE,
6487             p_count   => x_msg_count,
6488             p_data    => x_msg_data
6489       );
6490    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
6491       ROLLBACK TO Derive_End_Cust_Party;
6492       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6493       -- Standard call to get message count and if count=1, get the message
6494       FND_MSG_PUB.Count_And_Get (
6495             p_encoded => FND_API.G_FALSE,
6496             p_count   => x_msg_count,
6497             p_data    => x_msg_data
6498       );
6499    WHEN OTHERS THEN
6500       ROLLBACK TO Derive_End_Cust_Party;
6501       IF OZF_DEBUG_LOW_ON THEN
6502          OZF_UTILITY_PVT.debug_message('SQLERRM '|| sqlerrm);
6503       END IF;
6504       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
6505       IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
6506       THEN
6507          FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
6508       END IF;
6509       -- Standard call to get message count and if count=1, get the message
6510       FND_MSG_PUB.Count_And_Get (
6511             p_encoded => FND_API.G_FALSE,
6512             p_count   => x_msg_count,
6513             p_data    => x_msg_data
6514       );
6515 END Derive_End_Cust_Party;
6516 
6517 
6518 --======================================================================
6519 -- PROCEDURE
6520 --    update_main_tables
6521 --
6522 -- PURPOSE
6523 --    This API is called via concurrent program 'OZF: Update Budgets
6524 --    from Interface Tables' to update the main tables ozf_funds_all_b,
6525 --    ozf_act_budgets with the records from staging tables
6526 --    ozf_funds_all_b_int, ozf_act_budgets_int.
6527 --
6528 -- HISTORY
6529 --    30-OCT-2009  muthsubr  Created for the bug#8867381.
6530 --    29-NOV-2010  muthsubr  For TPA Parallel Execution ER Bug#9614703.
6531 --    19-JAN-2011  nirprasa  ozf_funds_all_tl and ozf_mc_transactions_all table will also be updated
6532 --                                      along with rollup budget.
6533 --======================================================================
6534 PROCEDURE update_main_tables (x_errbuf OUT NOCOPY VARCHAR2,
6535                               x_retcode OUT NOCOPY NUMBER,
6536                               p_batch_type IN VARCHAR2,
6537                               p_batch_id IN NUMBER)
6538 IS
6539 
6540    l_activity_budget_id NUMBER;
6541    l_resale_batch_id NUMBER;
6542    l_return_status VARCHAR2(50);
6543    l_api_name VARCHAR2(30) := 'update_main_tables';
6544    error_no_rollback          EXCEPTION;
6545    l_batch_id NUMBER;
6546 
6547 
6548    CURSOR batch_id_int_csr(p_batch_type IN VARCHAR) IS
6549    SELECT DISTINCT RESALE_BATCH_ID
6550    FROM OZF_FUNDS_ALL_B_INT
6551    WHERE BATCH_TYPE = p_batch_type;
6552 
6553    CURSOR funds_int_csr (p_batch_type IN VARCHAR) IS
6554    SELECT
6555    FUND_ID,
6556    SUM(BUDGET_AMOUNT_TC) BUDGET_AMOUNT_TC,
6557    SUM(BUDGET_AMOUNT_FC) BUDGET_AMOUNT_FC,
6558    SUM(AVAILABLE_AMOUNT) AVAILABLE_AMOUNT,
6559    SUM(TRANSFERED_IN_AMT) TRANSFERED_IN_AMT,
6560    SUM(TRANSFERED_OUT_AMT) TRANSFERED_OUT_AMT,
6561    SUM(PLANNED_AMT) PLANNED_AMT,
6562    SUM(COMMITTED_AMT) COMMITTED_AMT,
6563    SUM(EARNED_AMT) EARNED_AMT,
6564    SUM(PAID_AMT) PAID_AMT,
6565    SUM(RECAL_COMMITTED) RECAL_COMMITTED,
6566    SUM(ROLLUP_ORIGINAL_BUDGET) ROLLUP_ORIGINAL_BUDGET,
6567    SUM(ROLLUP_TRANSFERED_IN_AMT) ROLLUP_TRANSFERED_IN_AMT,
6568    SUM(ROLLUP_TRANSFERED_OUT_AMT) ROLLUP_TRANSFERED_OUT_AMT,
6569    SUM(ROLLUP_HOLDBACK_AMT) ROLLUP_HOLDBACK_AMT,
6570    SUM(ROLLUP_PLANNED_AMT) ROLLUP_PLANNED_AMT,
6571    SUM(ROLLUP_COMMITTED_AMT) ROLLUP_COMMITTED_AMT,
6572    SUM(ROLLUP_RECAL_COMMITTED) ROLLUP_RECAL_COMMITTED,
6573    SUM(ROLLUP_EARNED_AMT) ROLLUP_EARNED_AMT,
6574    SUM(ROLLUP_PAID_AMT) ROLLUP_PAID_AMT,
6575    SUM(UTILIZED_AMT) UTILIZED_AMT,
6576    SUM(ROLLUP_UTILIZED_AMT) ROLLUP_UTILIZED_AMT
6577    FROM OZF_FUNDS_ALL_B_INT
6578    WHERE BATCH_TYPE = p_batch_type
6579    GROUP BY RESALE_BATCH_ID, FUND_ID;
6580 
6581    CURSOR funds_int_wp_csr(p_batch_id IN NUMBER, p_batch_type IN VARCHAR) IS
6582    SELECT
6583    FUND_ID,
6584    SUM(BUDGET_AMOUNT_TC) BUDGET_AMOUNT_TC,
6585    SUM(BUDGET_AMOUNT_FC) BUDGET_AMOUNT_FC,
6586    SUM(AVAILABLE_AMOUNT) AVAILABLE_AMOUNT,
6587    SUM(TRANSFERED_IN_AMT) TRANSFERED_IN_AMT,
6588    SUM(TRANSFERED_OUT_AMT) TRANSFERED_OUT_AMT,
6589    SUM(PLANNED_AMT) PLANNED_AMT,
6590    SUM(COMMITTED_AMT) COMMITTED_AMT,
6591    SUM(EARNED_AMT) EARNED_AMT,
6592    SUM(PAID_AMT) PAID_AMT,
6593    SUM(RECAL_COMMITTED) RECAL_COMMITTED,
6594    SUM(ROLLUP_ORIGINAL_BUDGET) ROLLUP_ORIGINAL_BUDGET,
6595    SUM(ROLLUP_TRANSFERED_IN_AMT) ROLLUP_TRANSFERED_IN_AMT,
6596    SUM(ROLLUP_TRANSFERED_OUT_AMT) ROLLUP_TRANSFERED_OUT_AMT,
6597    SUM(ROLLUP_HOLDBACK_AMT) ROLLUP_HOLDBACK_AMT,
6598    SUM(ROLLUP_PLANNED_AMT) ROLLUP_PLANNED_AMT,
6599    SUM(ROLLUP_COMMITTED_AMT) ROLLUP_COMMITTED_AMT,
6600    SUM(ROLLUP_RECAL_COMMITTED) ROLLUP_RECAL_COMMITTED,
6601    SUM(ROLLUP_EARNED_AMT) ROLLUP_EARNED_AMT,
6602    SUM(ROLLUP_PAID_AMT) ROLLUP_PAID_AMT,
6603    SUM(UTILIZED_AMT) UTILIZED_AMT,
6604    SUM(ROLLUP_UTILIZED_AMT) ROLLUP_UTILIZED_AMT
6605    FROM OZF_FUNDS_ALL_B_INT
6606    WHERE RESALE_BATCH_ID = p_batch_id
6607    AND BATCH_TYPE = p_batch_type
6608    GROUP BY RESALE_BATCH_ID, FUND_ID;
6609 
6610   CURSOR budgets_int_csr(p_batch_type IN VARCHAR) IS
6611   SELECT
6612   ACTIVITY_BUDGET_ID,
6613   SUM(REQUEST_AMOUNT) REQUEST_AMOUNT,
6614   SUM( APPROVED_AMOUNT) APPROVED_AMOUNT,
6615   SUM(APPROVED_ORIGINAL_AMOUNT) APPROVED_ORIGINAL_AMOUNT,
6616   SUM(APPROVED_AMOUNT_FC) APPROVED_AMOUNT_FC,
6617   SUM(PARENT_SRC_APPRVD_AMT) PARENT_SRC_APPRVD_AMT,
6618   SUM(SRC_CURR_REQUEST_AMT) SRC_CURR_REQUEST_AMT
6619   FROM OZF_ACT_BUDGETS_INT
6620   WHERE BATCH_TYPE = p_batch_type
6621   group by RESALE_BATCH_ID, ACTIVITY_BUDGET_ID;
6622 
6623 
6624   CURSOR batch_id_csr(p_batch_type IN VARCHAR) IS
6625   SELECT DISTINCT
6626   RESALE_BATCH_ID
6627   FROM OZF_FUNDS_ALL_B_INT -- nepanda : changed the table name, as OZF_ACT_BUDGETS_INT will not have any data when its the 1st time accrual is created for the offer.
6628   WHERE BATCH_TYPE = p_batch_type;
6629 
6630   CURSOR budgets_int_wp_csr(p_batch_id IN NUMBER, p_batch_type IN VARCHAR) IS
6631   SELECT
6632   ACTIVITY_BUDGET_ID,
6633   SUM(REQUEST_AMOUNT) REQUEST_AMOUNT,
6634   SUM( APPROVED_AMOUNT) APPROVED_AMOUNT,
6635   SUM(APPROVED_ORIGINAL_AMOUNT) APPROVED_ORIGINAL_AMOUNT,
6636   SUM(APPROVED_AMOUNT_FC) APPROVED_AMOUNT_FC,
6637   SUM(PARENT_SRC_APPRVD_AMT) PARENT_SRC_APPRVD_AMT,
6638   SUM(SRC_CURR_REQUEST_AMT) SRC_CURR_REQUEST_AMT
6639   FROM OZF_ACT_BUDGETS_INT
6640   WHERE RESALE_BATCH_ID = p_batch_id
6641   AND BATCH_TYPE = p_batch_type
6642   group by RESALE_BATCH_ID, ACTIVITY_BUDGET_ID;
6643 
6644   --nirprasa, added for Bug#9614703
6645   l_mc_record_id       NUMBER;
6646   l_mc_obj_number      NUMBER;
6647   l_original_budget    NUMBER;
6648   l_currency_code_tc   VARCHAR2(30);
6649   l_org_id             NUMBER;
6650 
6651   l_budget_amount_tc            NUMBER;
6652   l_budget_amount_fc            NUMBER;
6653   l_available_amount            NUMBER;
6654   l_transfered_in_amt           NUMBER;
6655   l_transfered_out_amt          NUMBER;
6656   l_planned_amt                 NUMBER;
6657   l_committed_amt               NUMBER;
6658   l_earned_amt                  NUMBER;
6659   l_paid_amt                    NUMBER;
6660   l_recal_committed             NUMBER;
6661   l_rollup_original_budget      NUMBER;
6662   l_rollup_transfered_in_amt    NUMBER;
6663   l_rollup_transfered_out_amt   NUMBER;
6664   l_rollup_holdback_amt         NUMBER;
6665   l_rollup_planned_amt          NUMBER;
6666   l_rollup_committed_amt        NUMBER;
6667   l_rollup_recal_committed      NUMBER;
6668   l_rollup_earned_amt           NUMBER;
6669   l_rollup_paid_amt             NUMBER;
6670   l_utilized_amt                NUMBER;
6671   l_rollup_utilized_amt         NUMBER;
6672   l_object_version_number       NUMBER;
6673   l_api_version                 CONSTANT NUMBER   := 1.0;
6674   x_msg_count                   NUMBER;
6675    x_msg_data                    VARCHAR2(50);
6676   l_fund_rec                    ozf_funds_pvt.fund_rec_type;
6677   --l_mc_transaction_rec  ozf_mc_transactions_pvt.mc_transactions_rec_type;
6678 
6679 /*
6680   CURSOR c_mc_record(source_id   IN   NUMBER) IS
6681   SELECT   mc_record_id, object_version_number
6682   FROM     ozf_mc_transactions_all
6683   WHERE  source_object_id = source_id
6684   AND source_object_name = 'FUND';
6685 */
6686 
6687 CURSOR funds_rollup_csr(p_fund_id IN NUMBER) IS
6688 SELECT
6689  ORIGINAL_BUDGET,
6690  OBJECT_VERSION_NUMBER,
6691  CURRENCY_CODE_TC,
6692  ORG_ID,
6693  BUDGET_AMOUNT_TC,
6694  BUDGET_AMOUNT_FC,
6695  AVAILABLE_AMOUNT,
6696  TRANSFERED_IN_AMT,
6697  TRANSFERED_OUT_AMT,
6698  PLANNED_AMT,
6699  COMMITTED_AMT,
6700  EARNED_AMT,
6701  PAID_AMT,
6702  RECAL_COMMITTED,
6703  ROLLUP_ORIGINAL_BUDGET,
6704  ROLLUP_TRANSFERED_IN_AMT,
6705  ROLLUP_TRANSFERED_OUT_AMT,
6706  ROLLUP_HOLDBACK_AMT,
6707  ROLLUP_PLANNED_AMT,
6708  ROLLUP_COMMITTED_AMT,
6709  ROLLUP_RECAL_COMMITTED,
6710  ROLLUP_EARNED_AMT,
6711  ROLLUP_PAID_AMT,
6712  UTILIZED_AMT,
6713  ROLLUP_UTILIZED_AMT
6714 FROM OZF_FUNDS_ALL_B
6715 WHERE FUND_ID = p_fund_id;
6716 
6717 /* Obj_Fundsum changes */
6718 
6719 l_objfundsum_rec                        ozf_objfundsum_pvt.objfundsum_rec_type := NULL;
6720 
6721 l_utiz_amt                              number;
6722 l_gl_posted_flag                        VARCHAR2(30);
6723 l_fund_req_amt                          number;
6724 l_fund_req_amt_rem                      number;
6725 l_utiz_type                             VARCHAR2(30);
6726 l_amt_rem                               number;
6727 l_currency_code                         VARCHAR2(30);
6728 l_comp_type                             VARCHAR2(30);
6729 l_comp_id                               number;
6730 l_exchange_rate_date                    date;
6731 l_plan_curr_amount                      number;
6732 l_univ_curr_amount                      number;
6733 l_offer_type                            VARCHAR2(30);
6734 l_offer_accrual_flag                    boolean;
6735 l_offinvoice_gl_post_flag               VARCHAR2(5);
6736 l_utiz_org_id                           number;
6737 l_plan_type                             VARCHAR2(30);
6738 l_fund_id                               number;
6739 l_fund_type                             VARCHAR2(30);
6740 
6741 CURSOR c_get_objfundsum_rec(p_object_type IN VARCHAR2, p_object_id IN NUMBER, p_fund_id IN NUMBER) IS
6742 SELECT objfundsum_id
6743         , object_version_number
6744         , planned_amt
6745         , committed_amt
6746         , recal_committed_amt
6747         , plan_curr_planned_amt
6748         , plan_curr_committed_amt
6749         , plan_curr_recal_committed_amt
6750         , univ_curr_planned_amt
6751         , univ_curr_committed_amt
6752         , univ_curr_recal_committed_amt
6753         , utilized_amt
6754         , utilized_amt
6755         , plan_curr_utilized_amt
6756         , univ_curr_utilized_amt
6757         , earned_amt
6758         , plan_curr_earned_amt
6759         , univ_curr_earned_amt
6760         , paid_amt
6761         , plan_curr_paid_amt
6762         , univ_curr_paid_amt
6763 FROM   ozf_object_fund_summary
6764 WHERE  object_type = p_object_type
6765 AND    object_id = p_object_id
6766 AND    fund_id = p_fund_id;
6767 
6768 CURSOR funds_rec_csr(p_fund_id IN NUMBER) IS
6769 SELECT FUND_TYPE
6770 FROM OZF_FUNDS_ALL_B
6771 WHERE FUND_ID = p_fund_id;
6772 
6773 -- fix for bug 16233786
6774 
6775 CURSOR funds_util_rec_csr(p_batch_id IN NUMBER) IS
6776 select SUM(amount) amount,
6777 SUM(fund_request_amount) fund_request_amount,
6778 SUM(amount_remaining) amount_remaining,
6779 SUM(univ_curr_amount) univ_curr_amount,
6780 SUM(plan_curr_amount) plan_curr_amount,
6781 gl_posted_flag,
6782 utilization_type,
6783 plan_type,
6784 component_type,
6785 component_id,
6786 fund_id
6787 from ozf_funds_utilized_all_b fut
6788 WHERE
6789 object_type = 'TP_ORDER' AND
6790 utilization_type = 'ACCRUAL'  -- nepanda : Fix for 12747322 and 12660466 adjustments were getting added to obj_fundsum table twice
6791 AND object_id IN
6792 (select orl.resale_line_id
6793 from ozf_resale_lines_all orl,
6794 OZF_RESALE_BATCH_LINE_MAPS_ALL orblm
6795 where orl.resale_line_id = orblm.resale_line_id
6796 and orblm.resale_batch_id = p_batch_id)
6797 AND EXISTS (SELECT 1 FROM ozf_act_budgets_int where request_id = fut.request_id)
6798 GROUP BY
6799 gl_posted_flag,
6800 utilization_type,
6801 plan_type,
6802 component_type,
6803 component_id,
6804 fund_id;
6805 
6806 CURSOR c_get_deal_accrual_flag(p_qp_list_header_id IN NUMBER, p_product_type IN VARCHAR2, p_product_id IN NUMBER) IS
6807 SELECT 1
6808 FROM   DUAL
6809 WHERE  EXISTS (SELECT 1
6810                        FROM   qp_list_lines line, qp_pricing_attributes attr
6811                        WHERE  attr.list_header_id = p_qp_list_header_id
6812                        AND    line.list_line_id = attr.list_line_id
6813                        AND    NVL(line.accrual_flag, 'N') = 'Y'
6814                        AND    attr.product_attribute = DECODE(NVL(p_product_type, 'OTHER')
6815                                                              , 'PRODUCT', 'ITEM', 'FAMILY', 'CATEGORY', attr.product_attribute)
6816                        AND    attr.product_attr_value = NVL(p_product_id, attr.product_attr_value)
6817                        AND    attr.product_attribute_context = 'ITEM'
6818 );
6819 
6820 
6821 CURSOR c_offer_info (p_offer_id IN NUMBER) IS
6822 SELECT offer_type
6823 FROM   ozf_offers
6824 WHERE  qp_list_header_id = p_offer_id;
6825 
6826 CURSOR c_offinvoice_gl_post_flag(p_org_id IN NUMBER) IS
6827 SELECT  NVL(sob.gl_acct_for_offinv_flag, 'F')
6828 FROM    ozf_sys_parameters_all sob
6829 WHERE   sob.org_id = p_org_id;
6830 
6831 
6832 CURSOR ozf_vol_int_exists_csr IS
6833 SELECT 'Y'
6834 FROM DUAL
6835 WHERE EXISTS (SELECT 'Y' FROM
6836 OZF_VOLUME_SUMMARY_INT
6837 WHERE BATCH_TYPE='TP_ACCRUAL');
6838 
6839 
6840 CURSOR ozf_vol_int_exists_wp_csr(p_batch_id IN NUMBER) IS
6841 SELECT 'Y'
6842 FROM  DUAL
6843 WHERE EXISTS (SELECT 'Y' FROM OZF_VOLUME_SUMMARY_INT
6844 WHERE BATCH_TYPE='TP_ACCRUAL'
6845 AND RESALE_BATCH_ID=p_batch_id );
6846 
6847 
6848 l_vol_exists VARCHAR2(1) :='N';
6849 
6850 
6851 
6852 
6853 /* Obj_Fundsum changes */
6854 
6855 BEGIN
6856 
6857     SAVEPOINT update_main_tables;
6858 
6859    ozf_utility_pvt.write_conc_log('Concurrent Program updating records from Staging to Main tables - Started');
6860 
6861         -- For TPA Parallel Execution ER - BUG#9614703 (+)
6862         -- Update the main table with the records from staging tables.
6863 
6864           IF p_batch_id IS NULL THEN
6865                 ozf_utility_pvt.write_conc_log('Updating all the records to table OZF_FUNDS_ALL_B, OZF_ACT_BUDGETS when batch_id is null');
6866                 ozf_utility_pvt.write_conc_log('p_batch_type: ' || p_batch_type);
6867                 ozf_utility_pvt.write_conc_log('p_batch_id: ' || p_batch_id);
6868 
6869                 FOR funds_int_rec IN funds_int_csr(p_batch_type)
6870                 LOOP
6871                     /*
6872                         OPEN c_mc_record(funds_int_rec.fund_id);
6873                         FETCH c_mc_record INTO l_mc_record_id, l_mc_obj_number;
6874                         CLOSE c_mc_record;
6875                     */
6876 
6877                         OPEN funds_rollup_csr(funds_int_rec.fund_id);
6878                         FETCH funds_rollup_csr INTO l_original_budget, l_object_version_number, l_currency_code_tc,
6879                         l_org_id,l_budget_amount_tc,
6880                         l_budget_amount_fc, l_available_amount, l_transfered_in_amt, l_transfered_out_amt, l_planned_amt,
6881                         l_committed_amt, l_earned_amt,
6882                         l_paid_amt, l_recal_committed, l_rollup_original_budget, l_rollup_transfered_in_amt,
6883                         l_rollup_transfered_out_amt,
6884                         l_rollup_holdback_amt, l_rollup_planned_amt, l_rollup_committed_amt, l_rollup_recal_committed,
6885                         l_rollup_earned_amt,
6886                         l_rollup_paid_amt, l_utilized_amt, l_rollup_utilized_amt;
6887                         CLOSE funds_rollup_csr;
6888 
6889                         ozf_utility_pvt.write_conc_log('Updating funds record '||funds_int_rec.fund_id);
6890 
6891                    UPDATE OZF_FUNDS_ALL_B
6892                    SET
6893                          BUDGET_AMOUNT_TC = NVL(BUDGET_AMOUNT_TC,0) + NVL(funds_int_rec.BUDGET_AMOUNT_TC,0),
6894                          BUDGET_AMOUNT_FC = NVL(BUDGET_AMOUNT_FC,0) + NVL(funds_int_rec.BUDGET_AMOUNT_FC,0),
6895                          AVAILABLE_AMOUNT = NVL(AVAILABLE_AMOUNT,0) + NVL(funds_int_rec.AVAILABLE_AMOUNT,0),
6896                          TRANSFERED_IN_AMT = NVL(TRANSFERED_IN_AMT,0) + NVL(funds_int_rec.TRANSFERED_IN_AMT,0),
6897                          TRANSFERED_OUT_AMT = NVL(TRANSFERED_OUT_AMT,0) + NVL(funds_int_rec.TRANSFERED_OUT_AMT,0),
6898                          PLANNED_AMT = NVL(PLANNED_AMT,0) + NVL(funds_int_rec.PLANNED_AMT,0),
6899                          COMMITTED_AMT = NVL(COMMITTED_AMT,0) + NVL(funds_int_rec.COMMITTED_AMT,0),
6900                          EARNED_AMT = NVL(EARNED_AMT,0) + NVL(funds_int_rec.EARNED_AMT,0),
6901                          PAID_AMT = NVL(PAID_AMT,0) + NVL(funds_int_rec.PAID_AMT,0),
6902                          RECAL_COMMITTED = NVL(RECAL_COMMITTED,0) + NVL(funds_int_rec.RECAL_COMMITTED,0),
6903                          ROLLUP_ORIGINAL_BUDGET = NVL(ROLLUP_ORIGINAL_BUDGET,0) + NVL(funds_int_rec.ROLLUP_ORIGINAL_BUDGET,0),
6904                          ROLLUP_TRANSFERED_IN_AMT = NVL(ROLLUP_TRANSFERED_IN_AMT,0) + NVL(funds_int_rec.ROLLUP_TRANSFERED_IN_AMT,0),
6905                          ROLLUP_TRANSFERED_OUT_AMT = NVL(ROLLUP_TRANSFERED_OUT_AMT,0) + NVL(funds_int_rec.ROLLUP_TRANSFERED_OUT_AMT,0),
6906                          ROLLUP_HOLDBACK_AMT = NVL(ROLLUP_HOLDBACK_AMT,0) + NVL(funds_int_rec.ROLLUP_HOLDBACK_AMT,0),
6907                          ROLLUP_PLANNED_AMT = NVL(ROLLUP_PLANNED_AMT,0) + NVL(funds_int_rec.ROLLUP_PLANNED_AMT,0),
6908                          ROLLUP_COMMITTED_AMT = NVL(ROLLUP_COMMITTED_AMT,0) + NVL(funds_int_rec.ROLLUP_COMMITTED_AMT,0),
6909                          ROLLUP_RECAL_COMMITTED = NVL(ROLLUP_RECAL_COMMITTED,0) + NVL(funds_int_rec.ROLLUP_RECAL_COMMITTED,0),
6910                          ROLLUP_EARNED_AMT = NVL(ROLLUP_EARNED_AMT,0) + NVL(funds_int_rec.ROLLUP_EARNED_AMT,0),
6911                          ROLLUP_PAID_AMT = NVL(ROLLUP_PAID_AMT,0) + NVL(funds_int_rec.ROLLUP_PAID_AMT,0),
6912                          UTILIZED_AMT = NVL(UTILIZED_AMT,0) + NVL(funds_int_rec.UTILIZED_AMT,0),
6913                          ROLLUP_UTILIZED_AMT = NVL(ROLLUP_UTILIZED_AMT,0) + NVL(funds_int_rec.ROLLUP_UTILIZED_AMT,0),
6914                          LAST_UPDATE_DATE = SYSDATE,
6915                          LAST_UPDATED_BY = NVL (fnd_global.user_id, -1),
6916                          OBJECT_VERSION_NUMBER = OBJECT_VERSION_NUMBER + 1
6917                    WHERE  FUND_ID = funds_int_rec.fund_id;
6918 
6919                     -- Updating the fund tl tables.
6920                         UPDATE ozf_funds_all_tl
6921                         SET last_update_date = SYSDATE
6922                         ,last_updated_by = NVL(fnd_global.user_id, -1)
6923                         ,last_update_login = NVL(fnd_global.conc_login_id, -1)
6924                         ,request_id = fnd_global.conc_request_id
6925                         ,program_application_id = fnd_global.prog_appl_id
6926                         ,program_id = fnd_global.conc_program_id
6927                         ,program_update_date = SYSDATE
6928                         WHERE fund_id =  funds_int_rec.fund_id
6929                         AND USERENV('LANG') IN(language, source_lang);
6930 
6931 
6932                 -- Updating the roll up amount of the parent funds.
6933                 l_fund_rec.fund_id                      :=      funds_int_rec.fund_id;
6934                 l_fund_rec.budget_amount_tc             :=      NVL(l_budget_amount_tc,0) + NVL(funds_int_rec.BUDGET_AMOUNT_TC,0);
6935                 l_fund_rec.budget_amount_fc             :=      NVL(l_budget_amount_fc,0) + NVL(funds_int_rec.BUDGET_AMOUNT_FC,0);
6936                 l_fund_rec.available_amount             :=      NVL(l_available_amount,0) + NVL(funds_int_rec.AVAILABLE_AMOUNT,0);
6937                 l_fund_rec.transfered_in_amt            :=      NVL(l_transfered_in_amt,0) + NVL(funds_int_rec.TRANSFERED_IN_AMT,0);
6938                 l_fund_rec.transfered_out_amt           :=      NVL(l_transfered_out_amt,0) + NVL(funds_int_rec.TRANSFERED_OUT_AMT,0);
6939                 l_fund_rec.planned_amt                  :=      NVL(l_planned_amt,0) + NVL(funds_int_rec.PLANNED_AMT,0);
6940                 l_fund_rec.committed_amt                :=      NVL(l_committed_amt,0) + NVL(funds_int_rec.COMMITTED_AMT,0);
6941                 l_fund_rec.earned_amt                   :=      NVL(l_earned_amt,0) + NVL(funds_int_rec.EARNED_AMT,0);
6942                 l_fund_rec.paid_amt                     :=      NVL(l_paid_amt,0) + NVL(funds_int_rec.PAID_AMT,0);
6943                 l_fund_rec.utilized_amt                 :=      NVL(l_utilized_amt,0) + NVL(funds_int_rec.UTILIZED_AMT,0);
6944                 l_fund_rec.recal_committed              :=      NVL(l_recal_committed,0) + NVL(funds_int_rec.RECAL_COMMITTED,0);
6945                 l_fund_rec.rollup_original_budget       :=      NVL(l_rollup_original_budget,0) + NVL(funds_int_rec.ROLLUP_ORIGINAL_BUDGET,0);
6946                 l_fund_rec.rollup_transfered_in_amt     :=      NVL(l_rollup_transfered_in_amt,0) + NVL(funds_int_rec.ROLLUP_TRANSFERED_IN_AMT,0);
6947                 l_fund_rec.rollup_transfered_out_amt    :=      NVL(l_rollup_transfered_out_amt,0) + NVL(funds_int_rec.ROLLUP_TRANSFERED_OUT_AMT,0);
6948                 l_fund_rec.rollup_holdback_amt          :=      NVL(l_rollup_holdback_amt,0) + NVL(funds_int_rec.ROLLUP_HOLDBACK_AMT,0);
6949                 l_fund_rec.rollup_planned_amt           :=      NVL(l_rollup_planned_amt,0) + NVL(funds_int_rec.ROLLUP_PLANNED_AMT,0);
6950                 l_fund_rec.rollup_committed_amt         :=      NVL(l_rollup_committed_amt,0) + NVL(funds_int_rec.ROLLUP_COMMITTED_AMT,0);
6951                 l_fund_rec.rollup_recal_committed       :=      NVL(l_rollup_recal_committed,0) + NVL(funds_int_rec.ROLLUP_RECAL_COMMITTED,0);
6952                 l_fund_rec.rollup_earned_amt            :=      NVL(l_rollup_earned_amt,0) + NVL(funds_int_rec.ROLLUP_EARNED_AMT,0);
6953                 l_fund_rec.rollup_paid_amt              :=      NVL(l_rollup_paid_amt,0) + NVL(funds_int_rec.ROLLUP_PAID_AMT,0);
6954                 l_fund_rec.rollup_utilized_amt          :=      NVL(l_rollup_utilized_amt,0) + NVL(funds_int_rec.ROLLUP_UTILIZED_AMT,0);
6955 
6956                 ozf_funds_pvt.update_rollup_amount(
6957                         p_api_version  => l_api_version
6958                         ,p_init_msg_list  => fnd_api.g_false
6959                         ,p_commit     => fnd_api.g_false
6960                         ,p_validation_level   => fnd_api.g_valid_level_full
6961                         ,x_return_status      => l_return_status
6962                         ,x_msg_count  => x_msg_count
6963                         ,x_msg_data   => x_msg_data
6964                         ,p_fund_rec => l_fund_rec
6965                         );
6966 
6967                 END LOOP;
6968 
6969                 FOR budgets_int_rec IN budgets_int_csr(p_batch_type)
6970                 LOOP
6971 
6972                    UPDATE ozf_act_budgets
6973                    SET    request_amount = NVL(request_amount, 0) +  NVL(budgets_int_rec.REQUEST_AMOUNT,0)
6974                          ,src_curr_request_amt = NVL(src_curr_request_amt, 0) + NVL(budgets_int_rec.SRC_CURR_REQUEST_AMT,0)
6975                          ,approved_amount = NVL(approved_amount, 0) + NVL(budgets_int_rec.APPROVED_AMOUNT,0)
6976                          ,approved_original_amount = NVL(approved_original_amount, 0) + NVL(budgets_int_rec.APPROVED_ORIGINAL_AMOUNT,0)
6977                          ,approved_amount_fc = NVL(approved_amount_fc, 0) + NVL(budgets_int_rec.APPROVED_AMOUNT_FC,0)
6978                          ,parent_src_apprvd_amt = NVL(parent_src_apprvd_amt, 0) + NVL(budgets_int_rec.parent_src_apprvd_amt,0)
6979                          ,last_update_date = sysdate
6980                          ,last_updated_by = NVL (fnd_global.user_id, -1)
6981                          ,last_update_login = NVL (fnd_global.conc_login_id, -1)
6982                          ,object_version_number = NVL (object_version_number,0) + 1
6983                     WHERE  activity_budget_id = budgets_int_rec.ACTIVITY_BUDGET_ID;
6984 
6985                     ozf_utility_pvt.write_conc_log('Updating ozf_act_budgets record ' || budgets_int_rec.ACTIVITY_BUDGET_ID);
6986 
6987 		END LOOP;
6988 
6989 
6990                 /* Obj_Fundsum Changes */
6991 
6992                 FOR batch_id_rec IN batch_id_csr(p_batch_type)
6993                 LOOP
6994 
6995                           FOR funds_util_rec IN funds_util_rec_csr(batch_id_rec.RESALE_BATCH_ID)
6996                           LOOP
6997 
6998 		                ozf_utility_pvt.write_conc_log('p_batch_type: ' || p_batch_type);
6999 				ozf_utility_pvt.write_conc_log('Processing batch_id: ' || batch_id_rec.RESALE_BATCH_ID);
7000 
7001                                 l_utiz_amt :=           funds_util_rec.amount;
7002 				l_fund_req_amt:=        funds_util_rec.fund_request_amount;
7003                                 l_amt_rem:=             funds_util_rec.amount_remaining;
7004 				l_univ_curr_amount :=   funds_util_rec.univ_curr_amount;
7005 				l_plan_curr_amount:=    funds_util_rec.plan_curr_amount;
7006 				l_gl_posted_flag :=     funds_util_rec.gl_posted_flag;
7007                                 l_utiz_type:=           funds_util_rec.utilization_type;
7008                                 l_plan_type:=           funds_util_rec.plan_type;
7009                                 l_comp_type:=           funds_util_rec.component_type;
7010 
7011                                 IF l_comp_type = 'OFFR' THEN
7012                                    OPEN c_offer_info(funds_util_rec.component_id);
7013                                    FETCH c_offer_info INTO l_offer_type;
7014                                    CLOSE c_offer_info;
7015                                 END IF;
7016 
7017                        l_objfundsum_rec := NULL;
7018                        OPEN c_get_objfundsum_rec(funds_util_rec.component_type
7019                                                 ,funds_util_rec.component_id
7020                                                 ,funds_util_rec.fund_id);
7021                         FETCH c_get_objfundsum_rec INTO l_objfundsum_rec.objfundsum_id
7022                                                   , l_objfundsum_rec.object_version_number
7023                                                   , l_objfundsum_rec.planned_amt
7024                                                   , l_objfundsum_rec.committed_amt
7025                                                   , l_objfundsum_rec.recal_committed_amt
7026                                                   , l_objfundsum_rec.plan_curr_planned_amt
7027                                                   , l_objfundsum_rec.plan_curr_committed_amt
7028                                                   , l_objfundsum_rec.plan_curr_recal_committed_amt
7029                                                   , l_objfundsum_rec.univ_curr_planned_amt
7030                                                   , l_objfundsum_rec.univ_curr_committed_amt
7031                                                   , l_objfundsum_rec.univ_curr_recal_committed_amt
7032                                                   , l_objfundsum_rec.utilized_amt
7033                                                   , l_objfundsum_rec.utilized_amt
7034                                                   , l_objfundsum_rec.plan_curr_utilized_amt
7035                                                   , l_objfundsum_rec.univ_curr_utilized_amt
7036                                                   , l_objfundsum_rec.earned_amt
7037                                                   , l_objfundsum_rec.plan_curr_earned_amt
7038                                                   , l_objfundsum_rec.univ_curr_earned_amt
7039                                                   , l_objfundsum_rec.paid_amt
7040                                                   , l_objfundsum_rec.plan_curr_paid_amt
7041                                                   , l_objfundsum_rec.univ_curr_paid_amt                                   ;
7042                         CLOSE c_get_objfundsum_rec;
7043 
7044 
7045                                --common initializations for FIXED and FULLY_ACCRUED fund
7046 			       l_objfundsum_rec.utilized_amt := NVL(l_objfundsum_rec.utilized_amt, 0) + NVL(l_utiz_amt, 0);
7047                                l_objfundsum_rec.plan_curr_utilized_amt := NVL(l_objfundsum_rec.plan_curr_utilized_amt, 0) + NVL(l_fund_req_amt, 0);
7048                                l_objfundsum_rec.univ_curr_utilized_amt := NVL(l_objfundsum_rec.univ_curr_utilized_amt, 0) + NVL(l_univ_curr_amount, 0);
7049 
7050 
7051 			    OPEN funds_rec_csr(funds_util_rec.fund_id);
7052                             FETCH funds_rec_csr INTO l_fund_type;
7053                             CLOSE funds_rec_csr;
7054 
7055 			    IF l_fund_type = 'FULLY_ACCRUED' THEN
7056 
7057                                l_objfundsum_rec.committed_amt := NVL(l_objfundsum_rec.committed_amt, 0) + NVL(l_utiz_amt, 0);
7058 			       l_objfundsum_rec.plan_curr_committed_amt := NVL(l_objfundsum_rec.plan_curr_committed_amt, 0) + NVL(l_plan_curr_amount, 0);
7059 			       l_objfundsum_rec.univ_curr_committed_amt := NVL(l_objfundsum_rec.univ_curr_committed_amt, 0) + NVL(l_univ_curr_amount, 0);
7060 			       l_objfundsum_rec.recal_committed_amt := NVL(l_objfundsum_rec.recal_committed_amt, 0) + NVL(l_utiz_amt, 0);
7061                                l_objfundsum_rec.plan_curr_recal_committed_amt := NVL(l_objfundsum_rec.plan_curr_recal_committed_amt, 0) + NVL(l_plan_curr_amount, 0);
7062                                l_objfundsum_rec.univ_curr_recal_committed_amt := NVL(l_objfundsum_rec.univ_curr_recal_committed_amt, 0) + NVL(l_univ_curr_amount, 0);
7063 
7064                             ELSE
7065 
7066                              IF l_utiz_type IN ('ACCRUAL', 'LEAD_ACCRUAL', 'CHARGEBACK') THEN
7067                                 NULL;
7068 
7069                              ELSE
7070                                 IF l_comp_type <> 'OFFR' THEN
7071 
7072                                     IF l_comp_type <> 'PRIC' THEN
7073 
7074                                          l_objfundsum_rec.earned_amt := NVL(l_objfundsum_rec.earned_amt, 0) + NVL(l_utiz_amt, 0);
7075                                          l_objfundsum_rec.plan_curr_earned_amt := NVL(l_objfundsum_rec.plan_curr_earned_amt, 0)
7076                                                                                   + NVL(l_fund_req_amt, 0);
7077                                          l_objfundsum_rec.univ_curr_earned_amt := NVL(l_objfundsum_rec.univ_curr_earned_amt, 0)
7078                                                                                   + NVL(l_univ_curr_amount, 0);
7079                                          l_objfundsum_rec.paid_amt := NVL(l_objfundsum_rec.paid_amt, 0) + NVL(l_utiz_amt, 0);
7080                                          l_objfundsum_rec.plan_curr_paid_amt := NVL(l_objfundsum_rec.plan_curr_paid_amt, 0)
7081                                                                                   + NVL(l_fund_req_amt, 0);
7082                                          l_objfundsum_rec.univ_curr_paid_amt := NVL(l_objfundsum_rec.univ_curr_paid_amt, 0)
7083                                                                                   + NVL(l_univ_curr_amount, 0);
7084                                      END IF;
7085 
7086                                 ELSE
7087                                      IF l_utiz_type NOT IN ('ADJUSTMENT', 'LEAD_ADJUSTMENT') THEN
7088 
7089                                          OPEN c_offinvoice_gl_post_flag(l_utiz_org_id);
7090                                          FETCH c_offinvoice_gl_post_flag INTO l_offinvoice_gl_post_flag;
7091                                          CLOSE c_offinvoice_gl_post_flag;
7092 
7093                                             IF (l_offinvoice_gl_post_flag = 'F') THEN
7094 
7095                                                 l_objfundsum_rec.earned_amt := NVL(l_objfundsum_rec.earned_amt, 0) + NVL(l_utiz_amt, 0);
7096                                                 l_objfundsum_rec.plan_curr_earned_amt := NVL(l_objfundsum_rec.plan_curr_earned_amt, 0)
7097                                                                                        + NVL(l_plan_curr_amount, 0);
7098                                                 l_objfundsum_rec.univ_curr_earned_amt := NVL(l_objfundsum_rec.univ_curr_earned_amt, 0)
7099                                                                                        + NVL(l_univ_curr_amount, 0);
7100                                                 IF l_utiz_type NOT IN ('ADJUSTMENT', 'LEAD_ADJUSTMENT') THEN
7101                                                    l_objfundsum_rec.paid_amt := NVL(l_objfundsum_rec.paid_amt, 0) + NVL(l_utiz_amt, 0);
7102                                                    l_objfundsum_rec.plan_curr_paid_amt := NVL(l_objfundsum_rec.plan_curr_paid_amt, 0)
7103                                                                                         + NVL(l_plan_curr_amount, 0);
7104                                                    l_objfundsum_rec.univ_curr_paid_amt := NVL(l_objfundsum_rec.univ_curr_paid_amt, 0)
7105                                                                                         + NVL(l_univ_curr_amount, 0);
7106                                                 END IF;
7107                                             ELSE
7108                                                l_gl_posted_flag := ozf_accrual_engine.G_GL_FLAG_NO;
7109 
7110                                             END IF;
7111 
7112                                END IF;  -- IF p_utilization_rec.utilization_type IN ('ADJUSTMENT', 'LEAD_ADJUSTMENT')
7113                               END IF;    -- IF p_utilization_rec.plan_type <> 'OFFR'
7114                              END IF; -- IF p_utilization_rec.utilization_type IN ('ACCRUAL', 'LEAD_ACCRUAL', 'CHARGEBACK')
7115                             END IF; --IF l_fund_type = 'FULLY_ACCRUED' THEN
7116 
7117                             IF l_gl_posted_flag = 'Y' AND  l_plan_type IN ( 'OFFR' , 'PRIC')  THEN
7118 
7119                                 IF l_utiz_type <> 'SALES_ACCRUAL' THEN
7120 
7121 
7122                                            l_objfundsum_rec.earned_amt := NVL(l_objfundsum_rec.earned_amt, 0) + NVL(l_utiz_amt, 0);
7123                                                     l_objfundsum_rec.plan_curr_earned_amt := NVL(l_objfundsum_rec.plan_curr_earned_amt, 0) + NVL(l_fund_req_amt, 0);
7124                                                     l_objfundsum_rec.univ_curr_earned_amt := NVL(l_objfundsum_rec.univ_curr_earned_amt, 0) + NVL(l_univ_curr_amount, 0);
7125 
7126                                                 /*
7127 						IF l_comp_type = 'OFFR' AND NOT l_offer_accrual_flag AND l_utiz_type NOT IN ('ADJUSTMENT', 'LEAD_ADJUSTMENT') THEN
7128                                                         l_objfundsum_rec.paid_amt := NVL(l_objfundsum_rec.paid_amt, 0) + NVL(l_utiz_amt, 0);
7129                                                         l_objfundsum_rec.plan_curr_paid_amt := NVL(l_objfundsum_rec.plan_curr_paid_amt, 0) + NVL(l_fund_req_amt_rem, 0);
7130                                                         l_objfundsum_rec.univ_curr_paid_amt := NVL(l_objfundsum_rec.univ_curr_paid_amt, 0) + NVL(l_univ_curr_amount, 0);
7131                                                 END IF;
7132 						*/
7133                                 END IF;
7134                              END IF;
7135 
7136                              ozf_utility_pvt.write_conc_log('Updating OZF_OBJFUNDSUM_PVT for objfundsum_id: ' || l_objfundsum_rec.objfundsum_id);
7137 
7138                              IF l_objfundsum_rec.objfundsum_id IS NOT NULL THEN
7139                                 -- l_objfundsum_rec.fund_id := l_fund_id;
7140                                 -- l_objfundsum_rec.fund_currency := l_currency_code;
7141                                 -- l_objfundsum_rec.object_type := l_comp_type;
7142                                 -- l_objfundsum_rec.object_id := l_comp_id;
7143 
7144                                 OZF_OBJFUNDSUM_PVT.update_objfundsum(
7145                                p_api_version                => 1.0,
7146                                p_init_msg_list              => Fnd_Api.G_FALSE,
7147                                p_validation_level           => Fnd_Api.G_VALID_LEVEL_NONE,
7148                                p_objfundsum_rec             => l_objfundsum_rec,
7149                                p_conv_date                  => l_exchange_rate_date,
7150                                x_return_status              => l_return_status,
7151                                x_msg_count                  => x_msg_count,
7152                                x_msg_data                   => x_msg_data
7153                           );
7154 
7155                                 IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
7156                                    RAISE fnd_api.g_exc_unexpected_error;
7157                                 ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
7158                                    RAISE fnd_api.g_exc_error;
7159                                 END IF;
7160 
7161                              END IF;
7162 
7163 		/* Obj_Fundsum Changes */
7164 
7165 	        END LOOP; -- funds_util_rec_csr
7166              END LOOP;   -- batch_id_csr
7167 
7168 
7169 	     IF p_batch_type='TP_ACCRUAL' THEN
7170 	       	MERGE INTO OZF_VOLUME_SUMMARY ovolsum
7171 		USING (SELECT   SUM (GROUP_VOLUME) GROUP_VOLUME,
7172 			OFFER_ID,
7173 			GROUP_NO,
7174 			PBH_LINE_ID
7175 			FROM OZF_VOLUME_SUMMARY_INT
7176 			WHERE GROUP_NO IS NOT NULL
7177 			GROUP BY OFFER_ID, GROUP_NO, PBH_LINE_ID) ovolgrpint
7178 		ON (ovolsum.OFFER_ID= ovolgrpint.OFFER_ID
7179 		 AND ovolsum.GROUP_NO = ovolgrpint.GROUP_NO
7180 		AND ovolsum.PBH_LINE_ID= ovolgrpint.PBH_LINE_ID)
7181 		 WHEN MATCHED THEN
7182 			 UPDATE
7183 			 SET ovolsum.GROUP_VOLUME = ovolsum.GROUP_VOLUME + nvl(ovolgrpint.GROUP_VOLUME,0),
7184 			     LAST_UPDATE_DATE = SYSDATE,
7185 				 LAST_UPDATED_BY = FND_GLOBAL.user_id,
7186 				 LAST_UPDATE_LOGIN = FND_GLOBAL.conc_login_id
7187 		 WHEN NOT MATCHED THEN
7188 			  INSERT
7189 			  (
7190 			       ovolsum.volume_summary_id
7191 			      ,ovolsum.creation_date
7192 			      ,ovolsum.created_by
7193 			      ,ovolsum.last_update_date
7194 			      ,ovolsum.last_updated_by
7195 			      ,ovolsum.last_update_login
7196 			      ,ovolsum.offer_id
7197 			      ,ovolsum.group_no
7198 			      ,ovolsum.group_volume
7199 			      ,ovolsum.pbh_line_id
7200 			    )
7201 			VALUES
7202 			(
7203 			       ozf_volume_summary_s.NEXTVAL
7204 			      ,SYSDATE
7205 			      ,FND_GLOBAL.user_id
7206 			      ,SYSDATE
7207 			      ,FND_GLOBAL.user_id
7208 			      ,FND_GLOBAL.conc_login_id
7209 			      ,ovolgrpint.OFFER_ID
7210 			      ,ovolgrpint.GROUP_NO
7211 			      ,ovolgrpint.GROUP_VOLUME
7212 			      ,ovolgrpint.PBH_LINE_ID
7213 			);
7214 
7215 
7216 	      MERGE INTO OZF_VOLUME_SUMMARY ovolsum
7217 		USING (SELECT   SUM (INDIVIDUAL_VOLUME) INDIVIDUAL_VOLUME ,
7218 			OFFER_ID,
7219 			INDIVIDUAL_TYPE,
7220 			PBH_LINE_ID
7221 			FROM OZF_VOLUME_SUMMARY_INT
7222 			WHERE INDIVIDUAL_TYPE IS NOT NULL AND INDIVIDUAL_ID IS NULL
7223 			GROUP BY  OFFER_ID, INDIVIDUAL_TYPE,  PBH_LINE_ID) ovolindint
7224 		ON (ovolsum.OFFER_ID= ovolindint.OFFER_ID
7225 		 AND ovolsum.INDIVIDUAL_TYPE = ovolindint.INDIVIDUAL_TYPE
7226 		 AND ovolsum.INDIVIDUAL_ID IS NULL
7227 		AND ovolsum.PBH_LINE_ID= ovolindint.PBH_LINE_ID)
7228 		 WHEN MATCHED THEN
7229 			 UPDATE
7230 			 SET ovolsum.INDIVIDUAL_VOLUME = ovolsum.INDIVIDUAL_VOLUME + nvl(ovolindint.INDIVIDUAL_VOLUME,0),
7231 			     LAST_UPDATE_DATE = SYSDATE,
7232 				 LAST_UPDATED_BY = FND_GLOBAL.user_id,
7233 				 LAST_UPDATE_LOGIN = FND_GLOBAL.conc_login_id
7234 		 WHEN NOT MATCHED THEN
7235 			    INSERT
7236 			  (
7237 			       ovolsum.volume_summary_id
7238 			      ,ovolsum.creation_date
7239 			      ,ovolsum.created_by
7240 			      ,ovolsum.last_update_date
7241 			      ,ovolsum.last_updated_by
7242 			      ,ovolsum.last_update_login
7243 			      ,ovolsum.offer_id
7244 			      ,ovolsum.individual_type
7245 			      ,ovolsum.individual_volume
7246 			      ,ovolsum.pbh_line_id
7247 			    )
7248 			VALUES
7249 			(
7250 			       ozf_volume_summary_s.NEXTVAL
7251 			      ,SYSDATE
7252 			      ,FND_GLOBAL.user_id
7253 			      ,SYSDATE
7254 			      ,FND_GLOBAL.user_id
7255 			      ,FND_GLOBAL.conc_login_id
7256 			      ,ovolindint.OFFER_ID
7257 			      ,ovolindint.INDIVIDUAL_TYPE
7258 			      ,ovolindint.INDIVIDUAL_VOLUME
7259 			      ,ovolindint.PBH_LINE_ID
7260 			);
7261 
7262                 MERGE INTO OZF_VOLUME_SUMMARY ovolsum
7263 		USING (SELECT   SUM (INDIVIDUAL_VOLUME) INDIVIDUAL_VOLUME ,
7264 			OFFER_ID,
7265 			INDIVIDUAL_TYPE,
7266 			INDIVIDUAL_ID,
7267 			PBH_LINE_ID
7268 			FROM OZF_VOLUME_SUMMARY_INT
7269 			WHERE INDIVIDUAL_TYPE IS NOT NULL AND INDIVIDUAL_ID IS NOT NULL
7270 			GROUP BY  OFFER_ID, INDIVIDUAL_TYPE,INDIVIDUAL_ID,  PBH_LINE_ID) ovolindint
7271 		ON (ovolsum.OFFER_ID= ovolindint.OFFER_ID
7272 		 AND ovolsum.INDIVIDUAL_TYPE = ovolindint.INDIVIDUAL_TYPE
7273 		 AND ovolsum.INDIVIDUAL_ID = ovolindint.INDIVIDUAL_ID
7274 		AND ovolsum.PBH_LINE_ID= ovolindint.PBH_LINE_ID)
7275 		 WHEN MATCHED THEN
7276 			 UPDATE
7277 			 SET ovolsum.INDIVIDUAL_VOLUME = ovolsum.INDIVIDUAL_VOLUME+ nvl(ovolindint.INDIVIDUAL_VOLUME,0),
7278 			     LAST_UPDATE_DATE = SYSDATE,
7279                              LAST_UPDATED_BY = FND_GLOBAL.user_id,
7280                              LAST_UPDATE_LOGIN = FND_GLOBAL.conc_login_id
7281 		 WHEN NOT MATCHED THEN
7282 			  INSERT
7283 			  (
7284 			       ovolsum.volume_summary_id
7285 			      ,ovolsum.creation_date
7286 			      ,ovolsum.created_by
7287 			      ,ovolsum.last_update_date
7288 			      ,ovolsum.last_updated_by
7289 			      ,ovolsum.last_update_login
7290 			      ,ovolsum.offer_id
7291 			      ,ovolsum.individual_type
7292 			      ,ovolsum.individual_id
7293 			      ,ovolsum.individual_volume
7294 			      ,ovolsum.pbh_line_id
7295 			    )
7296 			VALUES
7297 			(
7298 			       ozf_volume_summary_s.NEXTVAL
7299 			      ,SYSDATE
7300 			      ,FND_GLOBAL.user_id
7301 			      ,SYSDATE
7302 			      ,FND_GLOBAL.user_id
7303 			      ,FND_GLOBAL.conc_login_id
7304 			      ,ovolindint.OFFER_ID
7305 			      ,ovolindint.INDIVIDUAL_TYPE
7306 			      ,ovolindint.INDIVIDUAL_ID
7307 			      ,ovolindint.INDIVIDUAL_VOLUME
7308 			      ,ovolindint.PBH_LINE_ID
7309 			);
7310 
7311 
7312 
7313 
7314             END IF;
7315 
7316                 -- Updating the status of all the batches as CLOSED in ozf_resale_batches_all table.
7317                 FOR batch_id_int_rec IN batch_id_int_csr(p_batch_type)
7318                 LOOP
7319 
7320                 ozf_utility_pvt.write_conc_log('updating batch to Closed status - resale_batch_id: ' || batch_id_int_rec.RESALE_BATCH_ID);
7321 
7322                         UPDATE ozf_resale_batches_all
7323                         SET status_code = OZF_RESALE_COMMON_PVT.G_BATCH_CLOSED,
7324                         last_updated_by = NVL(fnd_global.user_id, -1),
7325                         last_update_date = SYSDATE
7326                         WHERE resale_batch_id = batch_id_int_rec.RESALE_BATCH_ID
7327                         AND status_code in ('PENDING_CLOSE','PENDING_ACCRUALS');
7328                 END LOOP;
7329 
7330                 -- Delete the staging tables once the records are updated to the main table.
7331                 DELETE FROM OZF_FUNDS_ALL_B_INT WHERE BATCH_TYPE = p_batch_type;
7332                 DELETE FROM OZF_ACT_BUDGETS_INT WHERE BATCH_TYPE = p_batch_type;
7333 
7334                 IF p_batch_type='TP_ACCRUAL' THEN
7335 		  l_vol_exists :='N';
7336 		  OPEN ozf_vol_int_exists_csr;
7337                   FETCH ozf_vol_int_exists_csr into l_vol_exists;
7338 		  IF (ozf_vol_int_exists_csr%NOTFOUND) THEN
7339 		    l_vol_exists  :='N';
7340                   END IF;
7341 		  CLOSE ozf_vol_int_exists_csr;
7342 		  IF l_vol_exists = 'Y' THEN
7343 		          ozf_utility_pvt.write_conc_log('deleting records from staging table OZF_VOLUME_SUMMARY_INT for Batch Type '||p_batch_type);
7344 			  DELETE FROM OZF_VOLUME_SUMMARY_INT WHERE BATCH_TYPE = 'TP_ACCRUAL';
7345 			  ozf_utility_pvt.write_conc_log('deleted records from staging table OZF_VOLUME_SUMMARY_INT for Batch Type '||p_batch_type);
7346                   END IF;
7347 
7348                 END IF;
7349 
7350                 ozf_utility_pvt.write_conc_log('deleted records from staging tables OZF_FUNDS_ALL_B_INT and OZF_ACT_BUDGETS_INT ');
7351 
7352           ELSIF p_batch_id IS NOT NULL THEN
7353                 ozf_utility_pvt.write_conc_log('Updating Records to table OZF_FUNDS_ALL_B, OZF_ACT_BUDGETS for Batch Id: ' || p_batch_id);
7354                 ozf_utility_pvt.write_conc_log('Updating Records to table OZF_FUNDS_ALL_B, OZF_ACT_BUDGETS for Batch Type: ' || p_batch_type);
7355 
7356                 FOR funds_int_rec2 IN funds_int_wp_csr(p_batch_id, p_batch_type)
7357                 LOOP
7358 
7359                    ozf_utility_pvt.write_conc_log('Updating Fund Records to table OZF_FUNDS_ALL_B for Batch Id: ' || p_batch_id);
7360                    ozf_utility_pvt.write_conc_log('Fund id : ' || funds_int_rec2.FUND_ID);
7361 
7362                     --added for deadlock issue
7363 
7364                         OPEN funds_rollup_csr(funds_int_rec2.FUND_ID);
7365                         FETCH funds_rollup_csr INTO l_original_budget, l_object_version_number, l_currency_code_tc,
7366                         l_org_id,l_budget_amount_tc,
7367                         l_budget_amount_fc, l_available_amount, l_transfered_in_amt, l_transfered_out_amt, l_planned_amt,
7368                         l_committed_amt, l_earned_amt,
7369                         l_paid_amt, l_recal_committed, l_rollup_original_budget, l_rollup_transfered_in_amt,
7370                         l_rollup_transfered_out_amt,
7371                         l_rollup_holdback_amt, l_rollup_planned_amt, l_rollup_committed_amt, l_rollup_recal_committed,
7372                         l_rollup_earned_amt,
7373                         l_rollup_paid_amt, l_utilized_amt, l_rollup_utilized_amt;
7374                         CLOSE funds_rollup_csr;
7375 
7376                     --added for deadlock issue
7377 
7378 
7379                    UPDATE OZF_FUNDS_ALL_B
7380                    SET
7381                          BUDGET_AMOUNT_TC = NVL(BUDGET_AMOUNT_TC,0) + NVL(funds_int_rec2.BUDGET_AMOUNT_TC,0),
7382                          BUDGET_AMOUNT_FC = NVL(BUDGET_AMOUNT_FC,0) + NVL(funds_int_rec2.BUDGET_AMOUNT_FC,0),
7383                          AVAILABLE_AMOUNT = NVL(AVAILABLE_AMOUNT,0) + NVL(funds_int_rec2.AVAILABLE_AMOUNT,0),
7384                          TRANSFERED_IN_AMT = NVL(TRANSFERED_IN_AMT,0) + NVL(funds_int_rec2.TRANSFERED_IN_AMT,0),
7385                          TRANSFERED_OUT_AMT = NVL(TRANSFERED_OUT_AMT,0) + NVL(funds_int_rec2.TRANSFERED_OUT_AMT,0),
7386                          PLANNED_AMT = NVL(PLANNED_AMT,0) + NVL(funds_int_rec2.PLANNED_AMT,0),
7387                          COMMITTED_AMT = NVL(COMMITTED_AMT,0) + NVL(funds_int_rec2.COMMITTED_AMT,0),
7388                          EARNED_AMT = NVL(EARNED_AMT,0) + NVL(funds_int_rec2.EARNED_AMT,0),
7389                          PAID_AMT = NVL(PAID_AMT,0) + NVL(funds_int_rec2.PAID_AMT,0),
7390                          RECAL_COMMITTED = NVL(RECAL_COMMITTED,0) + NVL(funds_int_rec2.RECAL_COMMITTED,0),
7391                          ROLLUP_ORIGINAL_BUDGET = NVL(ROLLUP_ORIGINAL_BUDGET,0) + NVL(funds_int_rec2.ROLLUP_ORIGINAL_BUDGET,0),
7392                          ROLLUP_TRANSFERED_IN_AMT = NVL(ROLLUP_TRANSFERED_IN_AMT,0) + NVL(funds_int_rec2.ROLLUP_TRANSFERED_IN_AMT,0),
7393                          ROLLUP_TRANSFERED_OUT_AMT = NVL(ROLLUP_TRANSFERED_OUT_AMT,0) + NVL(funds_int_rec2.ROLLUP_TRANSFERED_OUT_AMT,0),
7394                          ROLLUP_HOLDBACK_AMT = NVL(ROLLUP_HOLDBACK_AMT,0) + NVL(funds_int_rec2.ROLLUP_HOLDBACK_AMT,0),
7395                          ROLLUP_PLANNED_AMT = NVL(ROLLUP_PLANNED_AMT,0) + NVL(funds_int_rec2.ROLLUP_PLANNED_AMT,0),
7396                          ROLLUP_COMMITTED_AMT = NVL(ROLLUP_COMMITTED_AMT,0) + NVL(funds_int_rec2.ROLLUP_COMMITTED_AMT,0),
7397                          ROLLUP_RECAL_COMMITTED = NVL(ROLLUP_RECAL_COMMITTED,0) + NVL(funds_int_rec2.ROLLUP_RECAL_COMMITTED,0),
7398                          ROLLUP_EARNED_AMT = NVL(ROLLUP_EARNED_AMT,0) + NVL(funds_int_rec2.ROLLUP_EARNED_AMT,0),
7399                          ROLLUP_PAID_AMT = NVL(ROLLUP_PAID_AMT,0) + NVL(funds_int_rec2.ROLLUP_PAID_AMT,0),
7400                          UTILIZED_AMT = NVL(UTILIZED_AMT,0) + NVL(funds_int_rec2.UTILIZED_AMT,0),
7401                          ROLLUP_UTILIZED_AMT = NVL(ROLLUP_UTILIZED_AMT,0) + NVL(funds_int_rec2.ROLLUP_UTILIZED_AMT,0),
7402                          LAST_UPDATE_DATE = SYSDATE,
7403                          LAST_UPDATED_BY = NVL (fnd_global.user_id, -1),
7404                          OBJECT_VERSION_NUMBER = OBJECT_VERSION_NUMBER + 1
7405                    WHERE  FUND_ID = funds_int_rec2.FUND_ID;
7406 
7407                    -- Updating the fund tl tables.
7408                         UPDATE ozf_funds_all_tl
7409                         SET last_update_date = SYSDATE
7410                         ,last_updated_by = NVL(fnd_global.user_id, -1)
7411                         ,last_update_login = NVL(fnd_global.conc_login_id, -1)
7412                         ,request_id = fnd_global.conc_request_id
7413                         ,program_application_id = fnd_global.prog_appl_id
7414                         ,program_id = fnd_global.conc_program_id
7415                         ,program_update_date = SYSDATE
7416                         WHERE fund_id =  funds_int_rec2.FUND_ID
7417                         AND USERENV('LANG') IN(language, source_lang);
7418 
7419 
7420                 -- Updating the roll up amount of the parent funds.
7421                 l_fund_rec.fund_id                      :=      funds_int_rec2.fund_id;
7422                 l_fund_rec.budget_amount_tc             :=      NVL(l_budget_amount_tc,0) + NVL(funds_int_rec2.BUDGET_AMOUNT_TC,0);
7423                 l_fund_rec.budget_amount_fc             :=      NVL(l_budget_amount_fc,0) + NVL(funds_int_rec2.BUDGET_AMOUNT_FC,0);
7424                 l_fund_rec.available_amount             :=      NVL(l_available_amount,0) + NVL(funds_int_rec2.AVAILABLE_AMOUNT,0);
7425                 l_fund_rec.transfered_in_amt            :=      NVL(l_transfered_in_amt,0) + NVL(funds_int_rec2.TRANSFERED_IN_AMT,0);
7426                 l_fund_rec.transfered_out_amt           :=      NVL(l_transfered_out_amt,0) + NVL(funds_int_rec2.TRANSFERED_OUT_AMT,0);
7427                 l_fund_rec.planned_amt                  :=      NVL(l_planned_amt,0) + NVL(funds_int_rec2.PLANNED_AMT,0);
7428                 l_fund_rec.committed_amt                :=      NVL(l_committed_amt,0) + NVL(funds_int_rec2.COMMITTED_AMT,0);
7429                 l_fund_rec.earned_amt                   :=      NVL(l_earned_amt,0) + NVL(funds_int_rec2.EARNED_AMT,0);
7430                 l_fund_rec.paid_amt                     :=      NVL(l_paid_amt,0) + NVL(funds_int_rec2.PAID_AMT,0);
7431                 l_fund_rec.utilized_amt                 :=      NVL(l_utilized_amt,0) + NVL(funds_int_rec2.UTILIZED_AMT,0);
7432                 l_fund_rec.recal_committed              :=      NVL(l_recal_committed,0) + NVL(funds_int_rec2.RECAL_COMMITTED,0);
7433                 l_fund_rec.rollup_original_budget       :=      NVL(l_rollup_original_budget,0) + NVL(funds_int_rec2.ROLLUP_ORIGINAL_BUDGET,0);
7434                 l_fund_rec.rollup_transfered_in_amt     :=      NVL(l_rollup_transfered_in_amt,0) + NVL(funds_int_rec2.ROLLUP_TRANSFERED_IN_AMT,0);
7435                 l_fund_rec.rollup_transfered_out_amt    :=      NVL(l_rollup_transfered_out_amt,0) + NVL(funds_int_rec2.ROLLUP_TRANSFERED_OUT_AMT,0);
7436                 l_fund_rec.rollup_holdback_amt          :=      NVL(l_rollup_holdback_amt,0) + NVL(funds_int_rec2.ROLLUP_HOLDBACK_AMT,0);
7437                 l_fund_rec.rollup_planned_amt           :=      NVL(l_rollup_planned_amt,0) + NVL(funds_int_rec2.ROLLUP_PLANNED_AMT,0);
7438                 l_fund_rec.rollup_committed_amt         :=      NVL(l_rollup_committed_amt,0) + NVL(funds_int_rec2.ROLLUP_COMMITTED_AMT,0);
7439                 l_fund_rec.rollup_recal_committed       :=      NVL(l_rollup_recal_committed,0) + NVL(funds_int_rec2.ROLLUP_RECAL_COMMITTED,0);
7440                 l_fund_rec.rollup_earned_amt            :=      NVL(l_rollup_earned_amt,0) + NVL(funds_int_rec2.ROLLUP_EARNED_AMT,0);
7441                 l_fund_rec.rollup_paid_amt              :=      NVL(l_rollup_paid_amt,0) + NVL(funds_int_rec2.ROLLUP_PAID_AMT,0);
7442                 l_fund_rec.rollup_utilized_amt          :=      NVL(l_rollup_utilized_amt,0) + NVL(funds_int_rec2.ROLLUP_UTILIZED_AMT,0);
7443 
7444 
7445                 OZF_UTILITY_PVT.write_conc_log('Updating update_rollup_amount from UPDATE_OZF_FUNDS_ALL_B for batch_id='||p_batch_id);
7446 
7447 
7448                 ozf_funds_pvt.update_rollup_amount(
7449                         p_api_version  => l_api_version
7450                         ,p_init_msg_list  => fnd_api.g_false
7451                         ,p_commit     => fnd_api.g_false
7452                         ,p_validation_level   => fnd_api.g_valid_level_full
7453                         ,x_return_status      => l_return_status
7454                         ,x_msg_count  => x_msg_count
7455                         ,x_msg_data   => x_msg_data
7456                         ,p_fund_rec => l_fund_rec
7457                         );
7458 
7459                 END LOOP;
7460 
7461                 FOR budgets_int_rec2 IN budgets_int_wp_csr(p_batch_id, p_batch_type)
7462                 LOOP
7463 
7464                 l_activity_budget_id := budgets_int_rec2.ACTIVITY_BUDGET_ID;
7465 
7466                    ozf_utility_pvt.write_conc_log('Updating Budget Records to table OZF_ACT_BUDGETS for Batch Id: ' || p_batch_id);
7467                    ozf_utility_pvt.write_conc_log('Activity Budget id1 : ' || l_activity_budget_id);
7468                    UPDATE ozf_act_budgets
7469                    SET    request_amount = NVL(request_amount, 0) +  NVL(budgets_int_rec2.REQUEST_AMOUNT,0)
7470                          ,src_curr_request_amt = NVL(src_curr_request_amt, 0) + NVL(budgets_int_rec2.SRC_CURR_REQUEST_AMT,0)
7471                          ,approved_amount = NVL(approved_amount, 0) + NVL(budgets_int_rec2.APPROVED_AMOUNT,0)
7472                          ,approved_original_amount = NVL(approved_original_amount, 0) + NVL(budgets_int_rec2.APPROVED_ORIGINAL_AMOUNT,0)
7473                          ,approved_amount_fc = NVL(approved_amount_fc, 0) + NVL(budgets_int_rec2.APPROVED_AMOUNT_FC,0)
7474                          ,parent_src_apprvd_amt = NVL(parent_src_apprvd_amt, 0) + NVL(budgets_int_rec2.parent_src_apprvd_amt,0)
7475                          ,last_update_date = sysdate
7476                          ,last_updated_by = NVL (fnd_global.user_id, -1)
7477                          ,last_update_login = NVL (fnd_global.conc_login_id, -1)
7478                          ,object_version_number = NVL (object_version_number,0) + 1
7479                     WHERE  activity_budget_id = l_activity_budget_id;
7480 
7481                    ozf_utility_pvt.write_conc_log('Activity Budget id2 : ' || l_activity_budget_id);
7482 
7483 		 END LOOP;
7484 
7485                 /* Obj_Fundsum Changes */
7486 
7487                           FOR funds_util_rec IN funds_util_rec_csr(p_batch_id)
7488                           LOOP
7489                                 l_utiz_amt :=           funds_util_rec.amount;
7490 				l_fund_req_amt:=        funds_util_rec.fund_request_amount;
7491                                 l_amt_rem:=             funds_util_rec.amount_remaining;
7492 				l_univ_curr_amount :=   funds_util_rec.univ_curr_amount;
7493 				l_plan_curr_amount:=    funds_util_rec.plan_curr_amount;
7494 				l_gl_posted_flag :=     funds_util_rec.gl_posted_flag;
7495                                 l_utiz_type:=           funds_util_rec.utilization_type;
7496                                 l_plan_type:=           funds_util_rec.plan_type;
7497                                 l_comp_type:=           funds_util_rec.component_type;
7498 
7499                                 IF l_comp_type = 'OFFR' THEN
7500                                    OPEN c_offer_info(funds_util_rec.component_id);
7501                                    FETCH c_offer_info INTO l_offer_type;
7502                                    CLOSE c_offer_info;
7503                                 END IF;
7504 
7505 			ozf_utility_pvt.write_conc_log('l_offer_type : ' || l_offer_type);
7506 
7507 			ozf_utility_pvt.write_conc_log('component_type : ' || funds_util_rec.component_type);
7508 			ozf_utility_pvt.write_conc_log('component_id : ' || funds_util_rec.component_id);
7509 			ozf_utility_pvt.write_conc_log('fund_id : ' || funds_util_rec.fund_id);
7510 
7511                        l_objfundsum_rec := NULL;
7512                        OPEN c_get_objfundsum_rec(funds_util_rec.component_type
7513                                                 ,funds_util_rec.component_id
7514                                                 ,funds_util_rec.fund_id);
7515                         FETCH c_get_objfundsum_rec INTO l_objfundsum_rec.objfundsum_id
7516                                                   , l_objfundsum_rec.object_version_number
7517                                                   , l_objfundsum_rec.planned_amt
7518                                                   , l_objfundsum_rec.committed_amt
7519                                                   , l_objfundsum_rec.recal_committed_amt
7520                                                   , l_objfundsum_rec.plan_curr_planned_amt
7521                                                   , l_objfundsum_rec.plan_curr_committed_amt
7522                                                   , l_objfundsum_rec.plan_curr_recal_committed_amt
7523                                                   , l_objfundsum_rec.univ_curr_planned_amt
7524                                                   , l_objfundsum_rec.univ_curr_committed_amt
7525                                                   , l_objfundsum_rec.univ_curr_recal_committed_amt
7526                                                   , l_objfundsum_rec.utilized_amt
7527                                                   , l_objfundsum_rec.utilized_amt
7528                                                   , l_objfundsum_rec.plan_curr_utilized_amt
7529                                                   , l_objfundsum_rec.univ_curr_utilized_amt
7530                                                   , l_objfundsum_rec.earned_amt
7531                                                   , l_objfundsum_rec.plan_curr_earned_amt
7532                                                   , l_objfundsum_rec.univ_curr_earned_amt
7533                                                   , l_objfundsum_rec.paid_amt
7534                                                   , l_objfundsum_rec.plan_curr_paid_amt
7535                                                   , l_objfundsum_rec.univ_curr_paid_amt                                   ;
7536                         CLOSE c_get_objfundsum_rec;
7537 
7538 
7539                              --common initializations for FIXED and FULLY_ACCRUED fund
7540 			     l_objfundsum_rec.utilized_amt := NVL(l_objfundsum_rec.utilized_amt, 0) + NVL(l_utiz_amt, 0);
7541                              l_objfundsum_rec.plan_curr_utilized_amt := NVL(l_objfundsum_rec.plan_curr_utilized_amt, 0) + NVL(l_fund_req_amt, 0);
7542                              l_objfundsum_rec.univ_curr_utilized_amt := NVL(l_objfundsum_rec.univ_curr_utilized_amt, 0) + NVL(l_univ_curr_amount, 0);
7543 
7544                              OZF_UTILITY_PVT.write_conc_log('l_objfundsum_rec.utilized_amt='||l_objfundsum_rec.utilized_amt);
7545 
7546 			    OPEN funds_rec_csr(funds_util_rec.fund_id);
7547                             FETCH funds_rec_csr INTO l_fund_type;
7548                             CLOSE funds_rec_csr;
7549 
7550 			    IF l_fund_type = 'FULLY_ACCRUED' THEN
7551 
7552                                l_objfundsum_rec.committed_amt := NVL(l_objfundsum_rec.committed_amt, 0) + NVL(l_utiz_amt, 0);
7553 			       l_objfundsum_rec.plan_curr_committed_amt := NVL(l_objfundsum_rec.plan_curr_committed_amt, 0) + NVL(l_plan_curr_amount, 0);
7554 			       l_objfundsum_rec.univ_curr_committed_amt := NVL(l_objfundsum_rec.univ_curr_committed_amt, 0) + NVL(l_univ_curr_amount, 0);
7555 			       l_objfundsum_rec.recal_committed_amt := NVL(l_objfundsum_rec.recal_committed_amt, 0) + NVL(l_utiz_amt, 0);
7556                                l_objfundsum_rec.plan_curr_recal_committed_amt := NVL(l_objfundsum_rec.plan_curr_recal_committed_amt, 0) + NVL(l_plan_curr_amount, 0);
7557                                l_objfundsum_rec.univ_curr_recal_committed_amt := NVL(l_objfundsum_rec.univ_curr_recal_committed_amt, 0) + NVL(l_univ_curr_amount, 0);
7558 
7559                             OZF_UTILITY_PVT.write_conc_log('l_fund_type1='||l_fund_type);
7560 
7561                             ELSE
7562 
7563                              IF l_utiz_type IN ('ACCRUAL', 'LEAD_ACCRUAL', 'CHARGEBACK') THEN
7564    			        OZF_UTILITY_PVT.write_conc_log('ne when l_utiz_type = '|| l_utiz_type);
7565 				OZF_UTILITY_PVT.write_conc_log('ne when l_utiz_type = '|| l_utiz_type);
7566                                 --NULL;
7567 
7568                              ELSE
7569                                 IF l_comp_type <> 'OFFR' THEN
7570 
7571                                     IF l_comp_type <> 'PRIC' THEN
7572 
7573                                          l_objfundsum_rec.earned_amt := NVL(l_objfundsum_rec.earned_amt, 0) + NVL(l_utiz_amt, 0);
7574                                          l_objfundsum_rec.plan_curr_earned_amt := NVL(l_objfundsum_rec.plan_curr_earned_amt, 0)
7575                                                                                   + NVL(l_fund_req_amt, 0);
7576                                          l_objfundsum_rec.univ_curr_earned_amt := NVL(l_objfundsum_rec.univ_curr_earned_amt, 0)
7577                                                                                   + NVL(l_univ_curr_amount, 0);
7578                                          l_objfundsum_rec.paid_amt := NVL(l_objfundsum_rec.paid_amt, 0) + NVL(l_utiz_amt, 0);
7579                                          l_objfundsum_rec.plan_curr_paid_amt := NVL(l_objfundsum_rec.plan_curr_paid_amt, 0)
7580                                                                                   + NVL(l_fund_req_amt, 0);
7581                                          l_objfundsum_rec.univ_curr_paid_amt := NVL(l_objfundsum_rec.univ_curr_paid_amt, 0)
7582                                                                                   + NVL(l_univ_curr_amount, 0);
7583                                      END IF;
7584 
7585                                 ELSE
7586                                      IF l_utiz_type NOT IN ('ADJUSTMENT', 'LEAD_ADJUSTMENT') THEN
7587 
7588                                          OPEN c_offinvoice_gl_post_flag(l_utiz_org_id);
7589                                          FETCH c_offinvoice_gl_post_flag INTO l_offinvoice_gl_post_flag;
7590                                          CLOSE c_offinvoice_gl_post_flag;
7591 
7592                                             IF (l_offinvoice_gl_post_flag = 'F') THEN
7593 
7594                                                 l_objfundsum_rec.earned_amt := NVL(l_objfundsum_rec.earned_amt, 0) + NVL(l_utiz_amt, 0);
7595                                                 l_objfundsum_rec.plan_curr_earned_amt := NVL(l_objfundsum_rec.plan_curr_earned_amt, 0)
7596                                                                                        + NVL(l_plan_curr_amount, 0);
7597                                                 l_objfundsum_rec.univ_curr_earned_amt := NVL(l_objfundsum_rec.univ_curr_earned_amt, 0)
7598                                                                                        + NVL(l_univ_curr_amount, 0);
7599                                                 IF l_utiz_type NOT IN ('ADJUSTMENT', 'LEAD_ADJUSTMENT') THEN
7600                                                    l_objfundsum_rec.paid_amt := NVL(l_objfundsum_rec.paid_amt, 0) + NVL(l_utiz_amt, 0);
7601                                                    l_objfundsum_rec.plan_curr_paid_amt := NVL(l_objfundsum_rec.plan_curr_paid_amt, 0)
7602                                                                                         + NVL(l_plan_curr_amount, 0);
7603                                                    l_objfundsum_rec.univ_curr_paid_amt := NVL(l_objfundsum_rec.univ_curr_paid_amt, 0)
7604                                                                                         + NVL(l_univ_curr_amount, 0);
7605                                                 END IF;
7606                                             ELSE
7607                                                l_gl_posted_flag := ozf_accrual_engine.G_GL_FLAG_NO;
7608 
7609                                             END IF;
7610 
7611                                OZF_UTILITY_PVT.write_conc_log('l_fund_type2='||l_fund_type);
7612 
7613                                END IF;  -- IF p_utilization_rec.utilization_type IN ('ADJUSTMENT', 'LEAD_ADJUSTMENT')
7614                               END IF;    -- IF p_utilization_rec.plan_type <> 'OFFR'
7615                              END IF; -- IF p_utilization_rec.utilization_type IN ('ACCRUAL', 'LEAD_ACCRUAL', 'CHARGEBACK')
7616                             END IF; --IF l_fund_type = 'FULLY_ACCRUED' THEN
7617 
7618                             IF l_gl_posted_flag = 'Y' AND  l_plan_type IN ( 'OFFR' , 'PRIC')  THEN
7619 
7620                                 IF l_utiz_type <> 'SALES_ACCRUAL' THEN
7621 
7622                                            l_objfundsum_rec.earned_amt := NVL(l_objfundsum_rec.earned_amt, 0) + NVL(l_utiz_amt, 0);
7623                                                     l_objfundsum_rec.plan_curr_earned_amt := NVL(l_objfundsum_rec.plan_curr_earned_amt, 0) + NVL(l_fund_req_amt, 0);
7624                                                     l_objfundsum_rec.univ_curr_earned_amt := NVL(l_objfundsum_rec.univ_curr_earned_amt, 0) + NVL(l_univ_curr_amount, 0);
7625 
7626                                                 /*
7627 						IF l_comp_type = 'OFFR' AND NOT l_offer_accrual_flag AND l_utiz_type NOT IN ('ADJUSTMENT', 'LEAD_ADJUSTMENT') THEN
7628                                                         l_objfundsum_rec.paid_amt := NVL(l_objfundsum_rec.paid_amt, 0) + NVL(l_utiz_amt, 0);
7629                                                         l_objfundsum_rec.plan_curr_paid_amt := NVL(l_objfundsum_rec.plan_curr_paid_amt, 0) + NVL(l_fund_req_amt_rem, 0);
7630                                                         l_objfundsum_rec.univ_curr_paid_amt := NVL(l_objfundsum_rec.univ_curr_paid_amt, 0) + NVL(l_univ_curr_amount, 0);
7631                                                 END IF;
7632 						*/
7633                                 END IF;
7634                              END IF;
7635 
7636                              ozf_utility_pvt.write_conc_log('Updating OZF_OBJFUNDSUM_PVT for objfundsum_id: ' || l_objfundsum_rec.objfundsum_id);
7637 
7638                              IF l_objfundsum_rec.objfundsum_id IS NOT NULL THEN
7639                                 -- l_objfundsum_rec.fund_id := l_fund_id;
7640                                 -- l_objfundsum_rec.fund_currency := l_currency_code;
7641                                 -- l_objfundsum_rec.object_type := l_comp_type;
7642                                 -- l_objfundsum_rec.object_id := l_comp_id;
7643 
7644                                 OZF_OBJFUNDSUM_PVT.update_objfundsum(
7645                                p_api_version                => 1.0,
7646                                p_init_msg_list              => Fnd_Api.G_FALSE,
7647                                p_validation_level           => Fnd_Api.G_VALID_LEVEL_NONE,
7648                                p_objfundsum_rec             => l_objfundsum_rec,
7649                                p_conv_date                  => l_exchange_rate_date,
7650                                x_return_status              => l_return_status,
7651                                x_msg_count                  => x_msg_count,
7652                                x_msg_data                   => x_msg_data
7653                           );
7654 
7655                                 IF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
7656                                    RAISE fnd_api.g_exc_unexpected_error;
7657                                 ELSIF l_return_status = fnd_api.g_ret_sts_error THEN
7658                                    RAISE fnd_api.g_exc_error;
7659                                 END IF;
7660 
7661                              END IF;
7662 
7663 		/* Obj_Fundsum Changes */
7664 
7665 	        END LOOP; -- funds_util_rec_csr
7666 
7667 
7668 
7669 		IF p_batch_type='TP_ACCRUAL' THEN
7670 			MERGE INTO OZF_VOLUME_SUMMARY ovolsum
7671 			USING (SELECT   SUM (GROUP_VOLUME) GROUP_VOLUME,
7672 				OFFER_ID,
7673 				GROUP_NO,
7674 				PBH_LINE_ID  ,
7675 				RESALE_BATCH_ID
7676 				FROM OZF_VOLUME_SUMMARY_INT
7677 				WHERE RESALE_BATCH_ID=p_batch_id
7678 				AND GROUP_NO IS NOT NULL
7679 				GROUP BY RESALE_BATCH_ID, OFFER_ID, GROUP_NO, PBH_LINE_ID) ovolgrpint
7680 			ON (ovolsum.OFFER_ID= ovolgrpint.OFFER_ID
7681 			 AND ovolsum.GROUP_NO = ovolgrpint.GROUP_NO
7682 			AND ovolsum.PBH_LINE_ID= ovolgrpint.PBH_LINE_ID)
7683 			 WHEN MATCHED THEN
7684 				 UPDATE
7685 				 SET ovolsum.GROUP_VOLUME = ovolsum.GROUP_VOLUME + nvl(ovolgrpint.GROUP_VOLUME,0),
7686 				     LAST_UPDATE_DATE = SYSDATE,
7687 				     LAST_UPDATED_BY = FND_GLOBAL.user_id,
7688 				     LAST_UPDATE_LOGIN = FND_GLOBAL.conc_login_id
7689 			 WHEN NOT MATCHED THEN
7690 				  INSERT
7691 				  (
7692 				       ovolsum.volume_summary_id
7693 				      ,ovolsum.creation_date
7694 				      ,ovolsum.created_by
7695 				      ,ovolsum.last_update_date
7696 				      ,ovolsum.last_updated_by
7697 				      ,ovolsum.last_update_login
7698 				      ,ovolsum.offer_id
7699 				      ,ovolsum.group_no
7700 				      ,ovolsum.group_volume
7701 				      ,ovolsum.pbh_line_id
7702 				    )
7703 				VALUES
7704 				(
7705 				       ozf_volume_summary_s.NEXTVAL
7706 				      ,SYSDATE
7707 				      ,FND_GLOBAL.user_id
7708 				      ,SYSDATE
7709 				      ,FND_GLOBAL.user_id
7710 				      ,FND_GLOBAL.conc_login_id
7711 				      ,ovolgrpint.OFFER_ID
7712 				      ,ovolgrpint.GROUP_NO
7713 				      ,ovolgrpint.GROUP_VOLUME
7714 				      ,ovolgrpint.PBH_LINE_ID
7715 				);
7716 
7717 
7718 
7719 				MERGE INTO OZF_VOLUME_SUMMARY ovolsum
7720 				USING (SELECT   SUM (INDIVIDUAL_VOLUME) INDIVIDUAL_VOLUME ,
7721 					OFFER_ID,
7722 					INDIVIDUAL_TYPE,
7723 					PBH_LINE_ID   ,
7724 					RESALE_BATCH_ID
7725 					FROM OZF_VOLUME_SUMMARY_INT
7726 					WHERE RESALE_BATCH_ID=p_batch_id
7727 					AND INDIVIDUAL_TYPE IS NOT NULL AND INDIVIDUAL_ID IS NULL
7728 					GROUP BY RESALE_BATCH_ID, OFFER_ID, INDIVIDUAL_TYPE,  PBH_LINE_ID) ovolindint
7729 				ON (ovolsum.OFFER_ID= ovolindint.OFFER_ID
7730 				 AND ovolsum.INDIVIDUAL_TYPE = ovolindint.INDIVIDUAL_TYPE
7731 				 AND ovolsum.INDIVIDUAL_ID IS NULL
7732 				AND ovolsum.PBH_LINE_ID= ovolindint.PBH_LINE_ID)
7733 				 WHEN MATCHED THEN
7734 					 UPDATE
7735 					 SET ovolsum.INDIVIDUAL_VOLUME = ovolsum.INDIVIDUAL_VOLUME + nvl(ovolindint.INDIVIDUAL_VOLUME,0),
7736 					     LAST_UPDATE_DATE = SYSDATE,
7737 					     LAST_UPDATED_BY = FND_GLOBAL.user_id,
7738 					     LAST_UPDATE_LOGIN = FND_GLOBAL.conc_login_id
7739 				 WHEN NOT MATCHED THEN
7740 					    INSERT
7741 					  (
7742 					       ovolsum.volume_summary_id
7743 					      ,ovolsum.creation_date
7744 					      ,ovolsum.created_by
7745 					      ,ovolsum.last_update_date
7746 					      ,ovolsum.last_updated_by
7747 					      ,ovolsum.last_update_login
7748 					      ,ovolsum.offer_id
7749 					      ,ovolsum.individual_type
7750 					      ,ovolsum.individual_volume
7751 					      ,ovolsum.pbh_line_id
7752 					    )
7753 					VALUES
7754 					(
7755 					       ozf_volume_summary_s.NEXTVAL
7756 					      ,SYSDATE
7757 					      ,FND_GLOBAL.user_id
7758 					      ,SYSDATE
7759 					      ,FND_GLOBAL.user_id
7760 					      ,FND_GLOBAL.conc_login_id
7761 					      ,ovolindint.OFFER_ID
7762 					      ,ovolindint.INDIVIDUAL_TYPE
7763 					      ,ovolindint.INDIVIDUAL_VOLUME
7764 					      ,ovolindint.PBH_LINE_ID
7765 					);
7766 
7767 
7768 				MERGE INTO OZF_VOLUME_SUMMARY ovolsum
7769 				USING (SELECT   SUM (INDIVIDUAL_VOLUME) INDIVIDUAL_VOLUME ,
7770 					OFFER_ID,
7771 					INDIVIDUAL_TYPE,
7772 					INDIVIDUAL_ID,
7773 					PBH_LINE_ID   ,
7774 					RESALE_BATCH_ID
7775 					FROM OZF_VOLUME_SUMMARY_INT
7776 					WHERE RESALE_BATCH_ID=p_batch_id
7777 					AND INDIVIDUAL_TYPE IS NOT NULL AND INDIVIDUAL_ID IS NOT NULL
7778 					GROUP BY RESALE_BATCH_ID, OFFER_ID, INDIVIDUAL_TYPE,INDIVIDUAL_ID,  PBH_LINE_ID) ovolindint
7779 				ON (ovolsum.OFFER_ID= ovolindint.OFFER_ID
7780 				 AND ovolsum.INDIVIDUAL_TYPE = ovolindint.INDIVIDUAL_TYPE
7781 				 AND ovolsum.INDIVIDUAL_ID = ovolindint.INDIVIDUAL_ID
7782 				AND ovolsum.PBH_LINE_ID= ovolindint.PBH_LINE_ID)
7783 				 WHEN MATCHED THEN
7784 					 UPDATE
7785 					 SET ovolsum.INDIVIDUAL_VOLUME = ovolsum.INDIVIDUAL_VOLUME+ nvl(ovolindint.INDIVIDUAL_VOLUME,0),
7786 					     LAST_UPDATE_DATE = SYSDATE,
7787 					     LAST_UPDATED_BY = FND_GLOBAL.user_id,
7788 					     LAST_UPDATE_LOGIN = FND_GLOBAL.conc_login_id
7789 				 WHEN NOT MATCHED THEN
7790 					  INSERT
7791 					  (
7792 					       ovolsum.volume_summary_id
7793 					      ,ovolsum.creation_date
7794 					      ,ovolsum.created_by
7795 					      ,ovolsum.last_update_date
7796 					      ,ovolsum.last_updated_by
7797 					      ,ovolsum.last_update_login
7798 					      ,ovolsum.offer_id
7799 					      ,ovolsum.individual_type
7800 					      ,ovolsum.individual_id
7801 					      ,ovolsum.individual_volume
7802 					      ,ovolsum.pbh_line_id
7803 					    )
7804 					VALUES
7805 					(
7806 					       ozf_volume_summary_s.NEXTVAL
7807 					      ,SYSDATE
7808 					      ,FND_GLOBAL.user_id
7809 					      ,SYSDATE
7810 					      ,FND_GLOBAL.user_id
7811 					      ,FND_GLOBAL.conc_login_id
7812 					      ,ovolindint.OFFER_ID
7813 					      ,ovolindint.INDIVIDUAL_TYPE
7814 					      ,ovolindint.INDIVIDUAL_ID
7815 					      ,ovolindint.INDIVIDUAL_VOLUME
7816 					      ,ovolindint.PBH_LINE_ID
7817 					);
7818 
7819 
7820                 END IF;
7821 
7822                 -- Updating the status of the batch as CLOSED in ozf_resale_batches_all table.
7823                 ozf_utility_pvt.write_conc_log('Updating the status of the batch as CLOSED for Batch Id='||p_batch_id);
7824 
7825                 UPDATE ozf_resale_batches_all
7826                 SET status_code = OZF_RESALE_COMMON_PVT.G_BATCH_CLOSED,
7827                 last_updated_by = NVL(fnd_global.user_id, -1),
7828                 last_update_date = SYSDATE
7829                 WHERE resale_batch_id = p_batch_id;
7830 
7831                 -- Delete the staging tables once the records are updated in the main table.
7832                 ozf_utility_pvt.write_conc_log('deleting records from staging tables OZF_FUNDS_ALL_B_INT and OZF_ACT_BUDGETS_INT for p_batch_type='||p_batch_type);
7833                 DELETE FROM ozf_act_budgets_int WHERE RESALE_BATCH_ID = p_batch_id AND BATCH_TYPE = p_batch_type;
7834                 DELETE FROM ozf_funds_all_b_int WHERE RESALE_BATCH_ID = p_batch_id AND BATCH_TYPE = p_batch_type;
7835 
7836 		ozf_utility_pvt.write_conc_log('deleted records from staging tables OZF_FUNDS_ALL_B_INT and OZF_ACT_BUDGETS_INT for the Batch Id='||p_batch_id);
7837 
7838 		IF p_batch_type='TP_ACCRUAL' THEN
7839                   l_vol_exists :='N';
7840 		  OPEN ozf_vol_int_exists_wp_csr(p_batch_id);
7841                   FETCH ozf_vol_int_exists_wp_csr into l_vol_exists;
7842 		  IF (ozf_vol_int_exists_wp_csr%NOTFOUND) THEN
7843 		    l_vol_exists  :='N';
7844                   END IF;
7845 		  CLOSE ozf_vol_int_exists_wp_csr;
7846 		  IF l_vol_exists = 'Y' THEN
7847 			 ozf_utility_pvt.write_conc_log('deleting records from staging table OZF_VOLUME_SUMMARY_INT  for p_batch_type='||p_batch_type ||' and for batch id '|| p_batch_id );
7848 		         DELETE FROM OZF_VOLUME_SUMMARY_INT WHERE RESALE_BATCH_ID = p_batch_id AND BATCH_TYPE = 'TP_ACCRUAL' ;
7849 		         ozf_utility_pvt.write_conc_log('deleted records from staging table OZF_VOLUME_SUMMARY_INT  for p_batch_type='||p_batch_type ||' and for batch id '|| p_batch_id );
7850                   END IF;
7851                 END IF;
7852 
7853 
7854           END IF;
7855           -- For TPA Parallel Execution ER - BUG#9614703 (-)
7856 
7857         ozf_utility_pvt.write_conc_log('Concurrent Program updating records from Staging to Main tables - End');
7858 
7859 EXCEPTION
7860    WHEN error_no_rollback THEN
7861       l_return_status := FND_API.G_RET_STS_ERROR;
7862       ozf_utility_pvt.write_conc_log('Concurrent Program updating records to main tables failed');
7863       ozf_utility_pvt.write_conc_log('l_return_status := '||l_return_status);
7864       OZF_UTILITY_PVT.Write_Conc_Log;
7865       x_errbuf  := 'Error while running staging table Concurrent Program';
7866       x_retcode := 2;
7867    WHEN FND_API.g_exc_error THEN
7868       ROLLBACK TO update_main_tables;
7869       l_return_status := FND_API.G_RET_STS_ERROR;
7870       ozf_utility_pvt.write_conc_log('Concurrent Program updating records to main tables failed');
7871       ozf_utility_pvt.write_conc_log('l_return_status := '||l_return_status);
7872       OZF_UTILITY_PVT.Write_Conc_Log;
7873       x_errbuf  := 'Error while running staging table Concurrent Program';
7874       x_retcode := 2;
7875    WHEN FND_API.g_exc_unexpected_error THEN
7876       ROLLBACK TO update_main_tables;
7877       l_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7878       ozf_utility_pvt.write_conc_log('Concurrent Program updating records to main tables failed');
7879       ozf_utility_pvt.write_conc_log('l_return_status := '||l_return_status);
7880       OZF_UTILITY_PVT.Write_Conc_Log;
7881       x_errbuf  := 'Error while running staging table Concurrent Program';
7882       x_retcode := 2;
7883    WHEN OTHERS THEN
7884       ROLLBACK TO update_main_tables;
7885       l_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
7886       IF OZF_UNEXP_ERROR THEN
7887          FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
7888       END IF;
7889       ozf_utility_pvt.write_conc_log('Concurrent Program updating records to main tables failed');
7890       ozf_utility_pvt.write_conc_log('l_return_status := '||l_return_status);
7891       OZF_UTILITY_PVT.Write_Conc_Log;
7892       x_errbuf  := 'Error while running staging table Concurrent Program';
7893       x_retcode := 2;
7894 
7895 END update_main_tables;
7896 
7897 --======================================================================
7898 -- PROCEDURE
7899 --    Create_Draft_Utilization
7900 --
7901 -- PURPOSE
7902 --    This procedure will be called when TPA is run in Draft Mode
7903 --    to insert draft utilization records into ozf_funds_utilized_draft_all table.
7904 --
7905 -- HISTORY
7906 --   7/31/2012     nepanda   Bug 14194884 - TPA In Draft Mode ER
7907 --======================================================================
7908 
7909 PROCEDURE Create_Draft_Utilization(
7910     p_api_version            IN  NUMBER
7911    ,p_init_msg_list          IN  VARCHAR2 := FND_API.G_FALSE
7912    ,p_commit                 IN  VARCHAR2 := FND_API.G_FALSE
7913    ,p_validation_level       IN  NUMBER   := FND_API.G_VALID_LEVEL_FULL
7914    ,p_price_adj_rec          IN  ozf_resale_adjustments_all%rowtype
7915    ,p_act_budgets_rec        IN  ozf_actbudgets_pvt.act_budgets_rec_type
7916    ,p_act_util_rec           IN  ozf_actbudgets_pvt.act_util_rec_type
7917    ,p_resale_line_int_rec    IN  OUT NOCOPY g_interface_rec_csr%rowtype
7918    ,p_draft_run_id	     IN  NUMBER
7919    ,p_caller_type	     IN  VARCHAR2
7920    ,px_draft_accrual_tbl     IN  OUT NOCOPY OZF_TP_ACCRUAL_PVT.ozf_draft_accrual_tbl_type
7921    ,x_return_status          OUT NOCOPY   VARCHAR2
7922    ,x_msg_data               OUT NOCOPY   VARCHAR2
7923    ,x_msg_count              OUT NOCOPY   NUMBER
7924    ,x_draft_utilization_id   OUT NOCOPY   NUMBER
7925 ) IS
7926 
7927 l_api_name              CONSTANT VARCHAR2(30) := 'Create_Draft_Utilization';
7928 l_api_version           CONSTANT NUMBER := 1.0;
7929 l_full_name             CONSTANT VARCHAR2(60) := G_PKG_NAME ||'.'|| l_api_name;
7930 l_return_status         VARCHAR2(30) := FND_API.G_RET_STS_SUCCESS ; -- Fix for bug # 14554578
7931 
7932 CURSOR csr_get_sys_params( p_org_id IN NUMBER) IS
7933   SELECT gs.currency_code
7934         ,org.exchange_rate_type
7935   FROM   gl_sets_of_books gs
7936        , ozf_sys_parameters org
7937   WHERE  org.set_of_books_id = gs.set_of_books_id
7938    AND  org.org_id = p_org_id;
7939 
7940 CURSOR draft_utilization_id_csr IS
7941    SELECT OZF_FUNDS_UTILIZED_DRAFT_ALL_S.NEXTVAL FROM DUAL;
7942 
7943 l_acctd_amount           NUMBER ;
7944 l_mrc_sob_type_code      VARCHAR2(50); -- not populating in draft table
7945 l_fc_currency_code       VARCHAR2(50); -- functional currency code -- not populating in draft table
7946 l_org_id		 NUMBER;
7947 l_ledger_id              NUMBER;
7948 l_exchange_rate_type     VARCHAR2(50) ;
7949 l_exchange_rate          NUMBER ;
7950 l_object_type            VARCHAR2(10);
7951 l_object_id              NUMBER ;
7952 line_count               NUMBER;
7953 l_draft_utilization_id   NUMBER;
7954 
7955 BEGIN
7956     -- Standard begin of API savepoint
7957     SAVEPOINT Create_Draft_Utilization;
7958 
7959     -- Standard call to check for call compatibility.
7960     IF NOT FND_API.Compatible_API_Call (
7961             l_api_version,
7962             p_api_version,
7963             l_api_name,
7964             G_PKG_NAME)
7965     THEN
7966          RAISE  FND_API.G_EXC_UNEXPECTED_ERROR;
7967     END IF;
7968     -- Debug Message
7969     IF OZF_DEBUG_LOW_ON THEN
7970             FND_MESSAGE.Set_Name('OZF','OZF_API_DEBUG_MESSAGE');
7971             FND_MESSAGE.Set_Token('TEXT',l_full_name||': Start');
7972             FND_MSG_PUB.Add;
7973     END IF;
7974 
7975     -- Initialize API return status to sucess
7976     x_return_status := FND_API.G_RET_STS_SUCCESS;
7977 
7978     IF OZF_DEBUG_LOW_ON THEN
7979       OZF_Utility_PVT.write_conc_log('--------- Create_Draft_Utilization ----------');
7980       OZF_Utility_PVT.write_conc_log('Create_Draft_Utilization: resale_line_int_id    : '|| p_resale_line_int_rec.resale_line_int_id);
7981       OZF_Utility_PVT.write_conc_log('Create_Draft_Utilization: org_id    : '|| p_act_util_rec.org_id);
7982     END IF;
7983 
7984 
7985     IF p_act_util_rec.org_id IS NOT NULL THEN
7986        OPEN csr_get_sys_params(p_act_util_rec.org_id);
7987        FETCH csr_get_sys_params INTO l_fc_currency_code,l_exchange_rate_type;
7988        CLOSE csr_get_sys_params;
7989 
7990        IF l_fc_currency_code IS NULL THEN
7991 	  IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
7992 	     FND_MESSAGE.set_name('OZF', 'OZF_METR_INVALID_FUNC_CUR');
7993 	     --//Functional currency does not exist. Please enter another currency.
7994 	     FND_MSG_PUB.add;
7995           END IF;
7996           p_resale_line_int_rec.dispute_code := 'OZF_METR_INVALID_FUNC_CUR';
7997           RAISE FND_API.G_EXC_ERROR;
7998        END IF;
7999        IF l_exchange_rate_type IS NULL THEN
8000           l_exchange_rate_type := p_resale_line_int_rec.exchange_rate_type ;
8001 	ELSIF  p_resale_line_int_rec.exchange_rate_type IS NOT NULL AND l_exchange_rate_type <> p_resale_line_int_rec.exchange_rate_type THEN
8002 	   l_exchange_rate_type := p_resale_line_int_rec.exchange_rate_type ;
8003        END IF;
8004        IF l_exchange_rate_type IS NULL THEN
8005 	  IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
8006 	     FND_MESSAGE.set_name('OZF', 'OZF_NO_EXCHANGE_TYPE');
8007 	     --//System Error: Exchange rate not setup.
8008 	     FND_MSG_PUB.add;
8009           END IF;
8010           p_resale_line_int_rec.dispute_code := 'OZF_NO_EXCHANGE_TYPE';
8011           RAISE FND_API.G_EXC_ERROR;
8012        END IF;
8013     END IF;
8014 
8015   IF OZF_DEBUG_LOW_ON THEN
8016       OZF_Utility_PVT.write_conc_log('Create_Draft_Utilization: p_act_budgets_rec.request_currency    : '|| p_act_budgets_rec.request_currency);
8017       OZF_Utility_PVT.write_conc_log('Create_Draft_Utilization: l_fc_currency_code    : '|| l_fc_currency_code);
8018     END IF;
8019 
8020 
8021     IF p_act_budgets_rec.request_currency <> l_fc_currency_code THEN
8022        OZF_UTILITY_PVT.Convert_Currency
8023 	    ( p_from_currency => p_act_budgets_rec.request_currency
8024 	    , p_to_currency   => l_fc_currency_code
8025 	    , p_conv_type     => l_exchange_rate_type
8026 	    , p_conv_date     => SYSDATE
8027 	    , p_from_amount   => p_act_budgets_rec.request_amount
8028 	    , x_return_status => l_return_status
8029 	    , x_to_amount     => l_acctd_amount
8030 	    , x_rate          => l_exchange_rate
8031 	    );
8032        IF l_return_status = FND_API.g_ret_sts_error THEN
8033           RAISE FND_API.g_exc_error;
8034        ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
8035           RAISE FND_API.g_exc_unexpected_error;
8036        END IF;
8037     ELSE
8038       l_acctd_amount  :=  p_act_budgets_rec.request_amount;
8039       l_exchange_rate :=  1;
8040     END IF;
8041 
8042   IF p_caller_type = 'IFACE' THEN
8043       l_object_type := 'TP_IFACE' ;
8044       l_object_id   := p_resale_line_int_rec.resale_line_int_id;
8045   ELSIF p_caller_type = 'RESALE' THEN
8046       l_object_type := 'TP_RESALE' ;
8047       l_object_id   := p_act_util_rec.object_id;
8048   END IF;
8049 
8050     IF OZF_DEBUG_LOW_ON THEN
8051       OZF_Utility_PVT.write_conc_log('Create_Draft_Utilization: l_acctd_amount    : '|| l_acctd_amount);
8052       OZF_Utility_PVT.write_conc_log('Create_Draft_Utilization: product_id    : '|| p_act_util_rec.product_id);
8053       OZF_Utility_PVT.write_conc_log('Create_Draft_Utilization: cust_account_id    : '|| p_act_util_rec.cust_account_id);
8054       OZF_Utility_PVT.write_conc_log('Create_Draft_Utilization: billto_cust_account_id    : '|| p_act_util_rec.billto_cust_account_id);
8055     END IF;
8056 
8057    --//Fix for Bug 16170069
8058    OPEN draft_utilization_id_csr;
8059    FETCH draft_utilization_id_csr into l_draft_utilization_id;
8060    CLOSE draft_utilization_id_csr;
8061 
8062    IF OZF_DEBUG_LOW_ON THEN
8063       OZF_Utility_PVT.write_conc_log('Create_Draft_Utilization: l_draft_utilization_id    : '|| l_draft_utilization_id);
8064    END IF;
8065 -- Fix for Bug # 14584673 : Adding accrual details to a table sturcture to be uploaded to ozf_funds_utilized_draft_all table at one shot later
8066 	line_count := px_draft_accrual_tbl.count + 1;
8067 
8068 	px_draft_accrual_tbl(line_count).draft_utilization_id := l_draft_utilization_id;
8069 	px_draft_accrual_tbl(line_count).draft_run_id := p_draft_run_id;
8070 	px_draft_accrual_tbl(line_count).object_type := l_object_type;
8071 	px_draft_accrual_tbl(line_count).object_id := l_object_id ;
8072 	px_draft_accrual_tbl(line_count).last_update_date := SYSDATE ;
8073 	px_draft_accrual_tbl(line_count).last_updated_by := NVL (fnd_global.user_id, -1) ;
8074 	px_draft_accrual_tbl(line_count).last_update_login := NVL (fnd_global.conc_login_id, -1) ;
8075 	px_draft_accrual_tbl(line_count).creation_date := SYSDATE ;
8076 	px_draft_accrual_tbl(line_count).created_by := NVL (fnd_global.user_id, -1) ;
8077 	px_draft_accrual_tbl(line_count).request_id := OZF_TP_ACCRUAL_PVT.G_CONC_REQUEST_ID ;
8078 	px_draft_accrual_tbl(line_count).program_application_id := fnd_global.prog_appl_id ;
8079 	px_draft_accrual_tbl(line_count).program_id := fnd_global.conc_program_id ;
8080 	px_draft_accrual_tbl(line_count).program_update_date := SYSDATE ;
8081 	px_draft_accrual_tbl(line_count).utilization_type := p_act_util_rec.utilization_type;
8082 	px_draft_accrual_tbl(line_count).plan_type :=  p_act_budgets_rec.arc_act_budget_used_by;
8083 	px_draft_accrual_tbl(line_count).plan_id := p_act_budgets_rec.act_budget_used_by_id;
8084 	px_draft_accrual_tbl(line_count).acctd_amount := l_acctd_amount;
8085 	px_draft_accrual_tbl(line_count).exchange_rate_type := l_exchange_rate_type;
8086 	px_draft_accrual_tbl(line_count).exchange_rate_date := SYSDATE;
8087 	px_draft_accrual_tbl(line_count).exchange_rate := l_exchange_rate;
8088 	px_draft_accrual_tbl(line_count).org_id := p_act_util_rec.org_id;
8089 	px_draft_accrual_tbl(line_count).product_id := p_act_util_rec.product_id;
8090 	px_draft_accrual_tbl(line_count).cust_account_id := p_act_util_rec.cust_account_id;
8091 	px_draft_accrual_tbl(line_count).product_level_type := p_act_util_rec.product_level_type;
8092 	px_draft_accrual_tbl(line_count).plan_curr_amount := p_act_budgets_rec.request_amount;
8093 	px_draft_accrual_tbl(line_count).activity_product_id := p_act_util_rec.activity_product_id;
8094 	px_draft_accrual_tbl(line_count).billto_cust_account_id := p_act_util_rec.billto_cust_account_id;
8095 	px_draft_accrual_tbl(line_count).reference_type := null;
8096 	px_draft_accrual_tbl(line_count).reference_id := null;
8097 	px_draft_accrual_tbl(line_count).ship_to_site_use_id := p_act_util_rec.ship_to_site_use_id;
8098 	px_draft_accrual_tbl(line_count).bill_to_site_use_id := p_act_util_rec.bill_to_site_use_id;
8099 	px_draft_accrual_tbl(line_count).plan_currency_code := p_act_budgets_rec.request_currency;
8100 	px_draft_accrual_tbl(line_count).discount_type := p_price_adj_rec.operand_calculation_code;
8101 	px_draft_accrual_tbl(line_count).discount_amount := p_price_adj_rec.operand;
8102 	px_draft_accrual_tbl(line_count).discount_amount_currency_code := p_act_util_rec.discount_amount_currency_code;
8103 	px_draft_accrual_tbl(line_count).list_line_id := p_price_adj_rec.LIST_LINE_ID;
8104 
8105 	x_return_status := l_return_status;
8106 
8107    --Standard check of commit
8108    IF FND_API.To_Boolean ( p_commit ) THEN
8109       COMMIT WORK;
8110    END IF;
8111    -- Debug Message
8112    IF OZF_DEBUG_LOW_ON THEN
8113       FND_MESSAGE.Set_Name('OZF','OZF_API_DEBUG_MESSAGE');
8114       FND_MESSAGE.Set_Token('TEXT',l_full_name||': End');
8115       FND_MSG_PUB.Add;
8116    END IF;
8117    --Standard call to get message count and if count=1, get the message
8118    FND_MSG_PUB.Count_And_Get (
8119       p_encoded => FND_API.G_FALSE,
8120       p_count => x_msg_count,
8121       p_data  => x_msg_data
8122    );
8123 EXCEPTION
8124    WHEN FND_API.G_EXC_ERROR THEN
8125         ozf_utility_pvt.write_conc_log('ERROR in Create Draft Utilization: '|| SQLERRM);
8126         ROLLBACK TO Create_Draft_Utilization;
8127         x_return_status := FND_API.G_RET_STS_ERROR;
8128         -- Standard call to get message count and if count=1, get the message
8129         FND_MSG_PUB.Count_And_Get (
8130                 p_encoded => FND_API.G_FALSE,
8131                 p_count => x_msg_count,
8132                 p_data  => x_msg_data
8133         );
8134    WHEN FND_API.G_EXC_UNEXPECTED_ERROR THEN
8135         ozf_utility_pvt.write_conc_log('UNEXPECTED ERROR in Create Draft Utilization: '|| SQLERRM);
8136         ROLLBACK TO Create_Draft_Utilization;
8137         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8138         -- Standard call to get message count and if count=1, get the message
8139         FND_MSG_PUB.Count_And_Get (
8140                 p_encoded => FND_API.G_FALSE,
8141                 p_count => x_msg_count,
8142                 p_data  => x_msg_data
8143         );
8144    WHEN OTHERS THEN
8145         ozf_utility_pvt.write_conc_log('OTHER ERROR in Create Draft Utilization: '|| SQLERRM);
8146         ROLLBACK TO Create_Draft_Utilization;
8147         x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
8148         IF FND_MSG_PUB.Check_Msg_Level ( FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR)
8149         THEN
8150                 FND_MSG_PUB.Add_Exc_Msg( G_PKG_NAME,l_api_name);
8151         END IF;
8152         -- Standard call to get message count and if count=1, get the message
8153         FND_MSG_PUB.Count_And_Get (
8154                 p_encoded => FND_API.G_FALSE,
8155                 p_count => x_msg_count,
8156                 p_data  => x_msg_data
8157         );
8158 
8159 END Create_Draft_Utilization;
8160 
8161 
8162 END OZF_RESALE_COMMON_PVT;