DBA Data[Home] [Help]

PACKAGE BODY: APPS.OZF_CLAIM_ACCRUAL_PVT

Source


1 PACKAGE BODY OZF_CLAIM_ACCRUAL_PVT AS
2 /* $Header: ozfvcacb.pls 120.130.12020000.6 2013/02/28 18:23:27 arpchakr ship $ */
3 
4 g_pkg_name   CONSTANT VARCHAR2(30):='OZF_Claim_Accrual_PVT';
5 
6 G_SCAN_VALUE                    NUMBER       := 0;
7 G_ENTERED_AMOUNT                NUMBER :=0;
8 G_BULK_LIMIT                 NUMBER := NVL(FND_PROFILE.value('OZF_BULK_LIMIT_SIZE') , 20000);
9 
10 -- object_type
11 G_CLAIM_OBJECT_TYPE    CONSTANT VARCHAR2(30) := 'CLAM';
12 
13 OZF_DEBUG_HIGH_ON      CONSTANT BOOLEAN := FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_debug_high);
14 OZF_DEBUG_LOW_ON       CONSTANT BOOLEAN := FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_debug_low);
15 
16 CURSOR g_site_trade_profile_csr(p_id in number) IS
17   SELECT trade_profile_id,
18          cust_account_id,
19          site_use_id,
20          payment_method,
21          vendor_id,
22          vendor_site_id,
23          last_paid_date,
24          autopay_periodicity,
25          autopay_periodicity_type,
26          autopay_flag,
27          claim_threshold,
28          claim_currency,
29          org_id
30   FROM ozf_cust_trd_prfls
31   WHERE site_use_id = p_id;
32 
33 CURSOR g_cust_trade_profile_csr(p_id in number) IS
34   SELECT trade_profile_id,
35          cust_account_id,
36          site_use_id,
37          payment_method,
38          vendor_id,
39          vendor_site_id,
40          last_paid_date,
41          autopay_periodicity,
42          autopay_periodicity_type,
43          autopay_flag,
44          claim_threshold,
45          claim_currency,
46          org_id
47   FROM ozf_cust_trd_prfls
48   WHERE cust_account_id = p_id;
49 
50 CURSOR g_party_trade_profile_csr(p_id in number) IS
51   SELECT trade_profile_id,
52          cust_account_id,
53          site_use_id,
54          payment_method,
55          vendor_id,
56          vendor_site_id,
57          last_paid_date,
58          autopay_periodicity,
59          autopay_periodicity_type,
60          autopay_flag,
61          claim_threshold,
62          claim_currency,
63          org_id
64   FROM ozf_cust_trd_prfls
65   WHERE party_id = p_id
66   AND cust_account_id IS NULL;
67 
68  l_original_total_amount NUMBER :=0;
69 
70 TYPE funds_rem_rec_type IS RECORD (
71   utilization_id         NUMBER,
72   amount_remaining       NUMBER,
73   scan_unit_remaining    NUMBER,
74   plan_currency_code     VARCHAR2(15),
75   plan_curr_amount       NUMBER,
76   plan_curr_amount_remaining NUMBER,
77   acctd_amount_remaining NUMBER
78 );
79 TYPE funds_rem_tbl_type is TABLE OF funds_rem_rec_type
80 INDEX BY BINARY_INTEGER;
81 
82 TYPE upd_fund_util_rec_type IS RECORD
83 (
84   amount_remaining              NUMBER,
85   acctd_amount_remaining        NUMBER,
86   plan_curr_amount_remaining    NUMBER,
87   univ_curr_amount_remaining    NUMBER,
88   fund_request_amount_remaining NUMBER,
89   utilization_id                NUMBER
90 );
91 
92 TYPE upd_fund_util_tbl_type is TABLE OF upd_fund_util_rec_type
93 INDEX BY BINARY_INTEGER;
94 
95 ---------------------------------------------------------------------
96 -- PROCEDURE
97 --    Create_Line_Util_Tbl
98 --
99 -- PURPOSE
100 --    Create multiple records of claim line utils.
101 --
102 -- PARAMETERS
103 --    p_line_util_tbl: the new records to be inserted
104 --    x_error_index: return the index number in table where error happened.
105 --
106 -- NOTES
107 ---------------------------------------------------------------------
108 PROCEDURE Create_Line_Util_Tbl(
109     p_api_version            IN    NUMBER
110    ,p_init_msg_list          IN    VARCHAR2 := FND_API.g_false
111    ,p_commit                 IN    VARCHAR2 := FND_API.g_false
112    ,p_validation_level       IN    NUMBER   := FND_API.g_valid_level_full
113 
114    ,x_return_status          OUT NOCOPY   VARCHAR2
115    ,x_msg_data               OUT NOCOPY   VARCHAR2
116    ,x_msg_count              OUT NOCOPY   NUMBER
117 
118    ,p_line_util_tbl          IN    line_util_tbl_type
119    ,p_currency_rec           IN    currency_rec_type
120    ,p_mode                   IN    VARCHAR2 := OZF_CLAIM_UTILITY_PVT.g_auto_mode
121 
122    ,x_error_index            OUT NOCOPY   NUMBER
123 );
124 ---------------------------------------------------------------------
125 -- PROCEDURE
126 --    Create_Line_Util
127 --
128 -- PURPOSE
129 --    Create a new record of claim line util.
130 --
131 -- PARAMETERS
132 --    p_line_util_rec: the new record to be inserted
133 --    x_line_util_id: return the claim_line_util_id of the new record.
134 --
135 -- NOTES
136 --    1. object_version_number will be set to 1.
137 --    2. If claim_line_util_id is passed in, the uniqueness will be checked.
138 --       Raise exception in case of duplicates.
139 --    3. If claim_line_util_id is not passed in, generate a unique one from
140 --       the sequence.
141 --    4. Please don't pass in any FND_API.g_miss_char/num/date for claim_line_util record.
142 ---------------------------------------------------------------------
143 
144 PROCEDURE Create_Line_Util(
145    p_api_version         IN  NUMBER
146   ,p_init_msg_list       IN  VARCHAR2  := FND_API.g_false
147   ,p_commit              IN  VARCHAR2  := FND_API.g_false
148   ,p_validation_level    IN  NUMBER    := FND_API.g_valid_level_full
149 
150   ,x_return_status       OUT NOCOPY VARCHAR2
151   ,x_msg_count           OUT NOCOPY NUMBER
152   ,x_msg_data            OUT NOCOPY VARCHAR2
153 
154   ,p_line_util_rec       IN  line_util_rec_type
155   ,p_currency_rec        IN  currency_rec_type
156   ,p_mode                IN  VARCHAR2  := OZF_CLAIM_UTILITY_PVT.g_auto_mode
157 
158   ,x_line_util_id        OUT NOCOPY NUMBER
159   ,px_line_util_tbl       IN OUT NOCOPY line_util_tbl_type --ninarasi fix for bug 13550004
160   ,px_upd_line_util_tbl   IN OUT NOCOPY upd_fund_util_tbl_type --ninarasi fix for bug 13530939
161 );
162 
163 --------------------------------------------------------------------
164 -- PROCEDURE
165 --    Delete_Line_Util
166 --
167 -- PURPOSE
168 --    Delete a record of claim line util.
169 --
170 -- PARAMETERS
171 --    p_line_util_id: the claim_line_id
172 --    p_object_version: the object_version_number
173 --
174 -- NOTES
175 --    1. Raise exception if the object_version_number doesn't match.
176 --------------------------------------------------------------------
177 PROCEDURE Delete_Line_Util(
178    p_api_version       IN  NUMBER
179   ,p_init_msg_list     IN  VARCHAR2 := FND_API.g_false
180   ,p_commit            IN  VARCHAR2 := FND_API.g_false
181 
182   ,x_return_status     OUT NOCOPY VARCHAR2
183   ,x_msg_count         OUT NOCOPY NUMBER
184   ,x_msg_data          OUT NOCOPY VARCHAR2
185 
186   ,p_line_util_id      IN  NUMBER
187   ,p_object_version    IN  NUMBER
188   ,p_mode              IN  VARCHAR2 := OZF_CLAIM_UTILITY_PVT.g_auto_mode
189   ,px_upd_line_util_tbl   IN OUT NOCOPY upd_fund_util_tbl_type --ninarasi fix for bug 13530939
190 );
191 
192 ---------------------------------------------------------------------
193 -- PROCEDURE
194 --    Update_Line_Util
195 --
196 -- PURPOSE
197 --    Update a claim line util record.
198 --
199 -- PARAMETERS
200 --    p_line_util_rec: the record with new items.
201 --
202 -- NOTES
203 --    1. Raise exception if the object_version_number doesn't match.
204 --    2. If an attribute is passed in as FND_API.g_miss_char/num/date,
205 --       that column won't be updated.
206 ----------------------------------------------------------------------
207 PROCEDURE Update_Line_Util(
208    p_api_version         IN  NUMBER
209   ,p_init_msg_list       IN  VARCHAR2  := FND_API.g_false
210   ,p_commit              IN  VARCHAR2  := FND_API.g_false
211   ,p_validation_level    IN  NUMBER    := FND_API.g_valid_level_full
212 
213   ,x_return_status       OUT NOCOPY VARCHAR2
214   ,x_msg_count           OUT NOCOPY NUMBER
215   ,x_msg_data            OUT NOCOPY VARCHAR2
216 
217   ,p_line_util_rec       IN  line_util_rec_type
218   ,p_mode               IN  VARCHAR2  := OZF_CLAIM_UTILITY_PVT.g_auto_mode
219 
220   ,x_object_version      OUT NOCOPY NUMBER
221   ,px_upd_line_util_tbl   IN OUT NOCOPY upd_fund_util_tbl_type --ninarasi fix for bug 13530939
222 );
223 
224 ---------------------------------------------------------------------
225 -- PROCEDURE
226 --   Update_Fund_utils
227 --   22-Oct-2005    Created     Sahana
228 --   08-Aug-06      azahmed     Modified for FXGL Er
229 --   21-Jan-08      psomyaju    Modified for Ship - Debit Claims
230 --   19-Apr-09      psomyaju    Re-organized code for R12 multicurrency ER.
231 --   24-Jun-09      BKUNJAN     Added parameter px_currency_rec.
232 ---------------------------------------------------------------------
233 PROCEDURE  Update_Fund_Utils(
234                 p_line_util_rec   IN  OUT NOCOPY  line_util_rec_type
235               , p_asso_amount     IN  NUMBER
236               , p_mode            IN  VARCHAR2 := 'CALCULATE'
237               , px_currency_rec   IN  OUT NOCOPY currency_rec_type
238               , x_return_status   OUT NOCOPY VARCHAR2
239               , x_msg_count       OUT NOCOPY NUMBER
240               , x_msg_data        OUT NOCOPY VARCHAR2
241               ,px_upd_line_util_tbl   IN OUT NOCOPY upd_fund_util_tbl_type --ninarasi fix for bug 13530939
242  )
243 IS
244 l_api_version   CONSTANT NUMBER       := 1.0;
245 l_api_name      CONSTANT VARCHAR2(30) := ' Update_Fund_Utils';
246 l_full_name     CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
247 l_return_status          VARCHAR2(1);
248 l_line_util_rec          line_util_rec_type := p_line_util_rec;
249 l_fu_req_currency        VARCHAR2(15);
250 l_fu_fund_currency       VARCHAR2(15);
251 l_claim_currency         VARCHAR2(15);
252 l_fu_exc_date            DATE;
253 l_fu_exc_type            VARCHAR2(30);
254 l_fu_amt_rem             NUMBER         := 0;
255 l_fu_acctd_amt_rem       NUMBER         := 0;
256 l_fu_plan_amt_rem        NUMBER         := 0;
257 l_fu_univ_amt_rem        NUMBER         := 0;
258 l_fu_req_amt_rem         NUMBER         := 0;
259 l_rate                   NUMBER         := 0;
260 l_reference_id           NUMBER         := 0;
261 l_source_object_class    VARCHAR2(15);
262 l_fu_plan_id             NUMBER;
263 l_fu_plan_type           VARCHAR2(15);
264 
265 l_currency_rec          currency_rec_type := px_currency_rec;
266 l_counter               NUMBER;
267 
268 CURSOR csr_fu_amt_rem(cv_utilization_id IN NUMBER) IS
269   SELECT currency_code
270        , fund_request_currency_code
271        , exchange_rate_date
272        , exchange_rate_type
273        , NVL(acctd_amount_remaining,0)
274        , NVL(plan_curr_amount_remaining,0)
275        , reference_id
276        , plan_id
277        , plan_type
278        , plan_currency_code
279   FROM   ozf_funds_utilized_all_b
280   WHERE  utilization_id = cv_utilization_id;
281 
282 --csr_object_class added for Ship - Debit claims / Pranay
283 --Bug# 8513457 fixed by ateotia (+)
284   /*
285   CURSOR  csr_object_class (cv_request_id IN NUMBER) IS
286   SELECT  cla.source_object_class
287   FROM    ozf_claims cla
288         , ozf_claim_lines line
289   WHERE   cla.claim_id = line.claim_id
290     AND   line.activity_id = cv_request_id;
291   */
292   CURSOR  csr_object_class (cv_claim_line_id IN NUMBER) IS
293   SELECT  cla.source_object_class
294   FROM    ozf_claims_all cla
295         , ozf_claim_lines_all line
296   WHERE   cla.claim_id = line.claim_id
297     AND   line.claim_line_id = cv_claim_line_id;
298 --Bug# 8513457 fixed by ateotia (-)
299 
300   CURSOR csr_claim_currency(cv_claim_line_id NUMBER) IS
301   SELECT currency_code
302   FROM   ozf_claim_lines
303   WHERE  claim_line_id = cv_claim_line_id;
304 BEGIN
305   --------------------- initialize -----------------------
306   SAVEPOINT  Update_Fund_Utils;
307 
308   IF OZF_DEBUG_HIGH_ON THEN
309      OZF_Utility_PVT.debug_message(l_full_name||': start');
310   END IF;
311 
312   x_return_status := FND_API.g_ret_sts_success;
313 
314   OPEN csr_fu_amt_rem(l_line_util_rec.utilization_id);
315   FETCH csr_fu_amt_rem INTO l_fu_fund_currency
316                           , l_fu_req_currency
317                           , l_fu_exc_date
318                           , l_fu_exc_type
319                           , l_fu_acctd_amt_rem
320                           , l_fu_plan_amt_rem
321                           , l_reference_id
322                           , l_fu_plan_id
323                           , l_fu_plan_type
324                           , l_currency_rec.transaction_currency_code;
325   CLOSE csr_fu_amt_rem;
326 
327   --Bug# 8513457 fixed by ateotia (+)
328   --OPEN csr_object_class(l_reference_id);
329   OPEN csr_object_class(l_line_util_rec.claim_line_id);
330   FETCH csr_object_class INTO l_source_object_class;
331   CLOSE csr_object_class;
332 
333   IF OZF_DEBUG_HIGH_ON THEN
334      OZF_Utility_PVT.debug_message('l_line_util_rec.claim_line_id: ' || l_line_util_rec.claim_line_id);
335      OZF_Utility_PVT.debug_message('l_source_object_class: ' || l_source_object_class);
336   END IF;
337   --Bug# 8513457 fixed by ateotia (-)
338 
339   --association_currency_code set in Update_Group_Line_Util program unit. In some processes, like
340   --public API which use other route for association this global variable will not set.
341   --Below logic will handle those scenarios.
342 
343   IF l_currency_rec.association_currency_code IS NULL THEN
344      OPEN  csr_claim_currency(l_line_util_rec.claim_line_id);
345      FETCH csr_claim_currency INTO l_currency_rec.claim_currency_code;
346      CLOSE csr_claim_currency;
347 
348      IF l_currency_rec.claim_currency_code = l_currency_rec.transaction_currency_code THEN
349         l_currency_rec.association_currency_code := l_currency_rec.transaction_currency_code;
350      ELSE
351         l_currency_rec.association_currency_code := l_currency_rec.functional_currency_code;
352      END IF;
353   END IF;
354 
355   --Set UNIVERSAL currency from profile.
356   l_currency_rec.universal_currency_code := FND_PROFILE.VALUE('OZF_TP_COMMON_CURRENCY');
357 
358   IF OZF_DEBUG_HIGH_ON THEN
359     OZF_Utility_PVT.debug_message('Currencies at Update_Fund_Utils API...');
360     OZF_Utility_PVT.debug_message('p_asso_amount = '||p_asso_amount);
361     OZF_Utility_PVT.debug_message('l_line_util_rec.amount = '||l_line_util_rec.amount);
362     OZF_Utility_PVT.debug_message('l_line_util_rec.claim_line_id = '||l_line_util_rec.claim_line_id);
363     OZF_Utility_PVT.debug_message('l_currency_rec.association_currency_code = '||l_currency_rec.association_currency_code);
364     OZF_Utility_PVT.debug_message('l_currency_rec.claim_currency_code = '||l_currency_rec.claim_currency_code);
365     OZF_Utility_PVT.debug_message('l_currency_rec.transaction_currency_code = '||l_currency_rec.transaction_currency_code);
366     OZF_Utility_PVT.debug_message('l_currency_rec.functional_currency_code = '||l_currency_rec.functional_currency_code);
367     OZF_Utility_PVT.debug_message('l_currency_rec.universal_currency_code  = '||l_currency_rec.universal_currency_code );
368     OZF_Utility_PVT.debug_message('l_fu_fund_currency  = '||l_fu_fund_currency);
369     OZF_Utility_PVT.debug_message('l_fu_req_currency  = '||l_fu_req_currency );
370     OZF_Utility_PVT.debug_message('l_line_util_rec.utilization_id  = '||l_line_util_rec.utilization_id );
371 
372   END IF;
373   --p_asso_amount (earning/adjustment amount) passed in TRANSACTIONAL or FUNCTIONAL
374   --currency. Need to calculate amount remaining to be reduced accordingly.
375     IF l_currency_rec.association_currency_code  = l_currency_rec.transaction_currency_code THEN
376       --l_fu_plan_amt_rem :=  l_fu_plan_amt_rem + NVL(p_asso_amount,0);
377         l_fu_plan_amt_rem :=  - NVL(p_asso_amount,0);
378       IF l_fu_plan_amt_rem IS NOT NULL AND l_fu_plan_amt_rem <> 0 THEN
379          OZF_UTILITY_PVT.Convert_Currency
380                      ( p_from_currency   => l_currency_rec.transaction_currency_code
381                      , p_to_currency     => l_currency_rec.functional_currency_code
382                      , p_conv_date       => l_fu_exc_date
383                      , p_conv_type       => l_fu_exc_type
384                      , p_from_amount     => l_fu_plan_amt_rem
385                      , x_return_status   => l_return_status
386                      , x_to_amount       => l_fu_acctd_amt_rem
387                      , x_rate            => l_rate
388                      );
389         IF l_return_status = FND_API.g_ret_sts_error THEN
390           RAISE FND_API.g_exc_error;
391         ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
392           RAISE FND_API.g_exc_unexpected_error;
393         END IF;
394         IF l_fu_acctd_amt_rem IS NOT NULL THEN
395           l_fu_acctd_amt_rem  := OZF_UTILITY_PVT.CurrRound(l_fu_acctd_amt_rem, l_currency_rec.functional_currency_code);
396         END IF;
397       ELSE
398         l_fu_acctd_amt_rem := 0;
399       END IF;
400          ELSE
401       --l_fu_acctd_amt_rem := l_fu_acctd_amt_rem + NVL(p_asso_amount,0);
402       l_fu_acctd_amt_rem := - NVL(p_asso_amount,0);
403       IF l_fu_acctd_amt_rem IS NOT NULL AND l_fu_acctd_amt_rem <> 0 THEN
404          OZF_UTILITY_PVT.Convert_Currency
405                      ( p_from_currency   => l_currency_rec.functional_currency_code
406                      , p_to_currency     => l_currency_rec.transaction_currency_code
407                      , p_conv_date       => l_fu_exc_date
408                      , p_conv_type       => l_fu_exc_type
409                      , p_from_amount     => l_fu_acctd_amt_rem
410                      , x_return_status   => l_return_status
411                      , x_to_amount       => l_fu_plan_amt_rem
412                      , x_rate            => l_rate
413                      );
414             IF l_return_status = FND_API.g_ret_sts_error THEN
415                   RAISE FND_API.g_exc_error;
416             ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
417                   RAISE FND_API.g_exc_unexpected_error;
418             END IF;
419         IF l_fu_plan_amt_rem IS NOT NULL THEN
420           l_fu_plan_amt_rem := OZF_UTILITY_PVT.CurrRound(l_fu_plan_amt_rem, l_currency_rec.transaction_currency_code);
421         END IF;
422       ELSE
423         l_fu_plan_amt_rem := 0;
424       END IF;
425     END IF;
426 
427     IF l_fu_plan_amt_rem  <> 0 OR
428        l_fu_acctd_amt_rem <> 0
429     THEN
430       IF l_fu_fund_currency = l_currency_rec.transaction_currency_code THEN
431          l_fu_amt_rem := l_fu_plan_amt_rem;
432       ELSIF l_fu_fund_currency = l_currency_rec.functional_currency_code THEN
433         l_fu_amt_rem := l_fu_acctd_amt_rem;
434       ELSE
435         IF l_fu_plan_amt_rem IS NOT NULL AND l_fu_plan_amt_rem <> 0 THEN
436            OZF_UTILITY_PVT.Convert_Currency
437                        ( p_from_currency   => l_currency_rec.transaction_currency_code
438                        , p_to_currency     => l_fu_fund_currency
439                        , p_conv_date       => l_fu_exc_date
440                        , p_conv_type       => l_fu_exc_type
441                        , p_from_amount     => l_fu_plan_amt_rem
442                        , x_return_status   => l_return_status
443                        , x_to_amount       => l_fu_amt_rem
444                        , x_rate            => l_rate
445                        );
446           IF l_return_status = FND_API.g_ret_sts_error THEN
447             RAISE FND_API.g_exc_error;
448           ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
449             RAISE FND_API.g_exc_unexpected_error;
450           END IF;
451           IF l_fu_amt_rem IS NOT NULL THEN
452             l_fu_amt_rem := OZF_UTILITY_PVT.CurrRound(l_fu_amt_rem, l_fu_fund_currency);
453           END IF;
454         ELSE
455           l_fu_amt_rem := 0;
456         END IF;
457       END IF;
458 
459       IF l_currency_rec.universal_currency_code = l_currency_rec.transaction_currency_code THEN
460         l_fu_univ_amt_rem := l_fu_plan_amt_rem;
461       ELSIF l_currency_rec.universal_currency_code = l_currency_rec.functional_currency_code THEN
462         l_fu_univ_amt_rem := l_fu_acctd_amt_rem;
463       ELSIF l_currency_rec.universal_currency_code = l_fu_fund_currency THEN
464         l_fu_univ_amt_rem := l_fu_amt_rem;
465       ELSE
466         IF l_fu_plan_amt_rem IS NOT NULL AND l_fu_plan_amt_rem <> 0 THEN
467            OZF_UTILITY_PVT.Convert_Currency
468                          ( p_from_currency   => l_currency_rec.transaction_currency_code
469                          , p_to_currency     => l_currency_rec.universal_currency_code
470                          , p_conv_date       => l_fu_exc_date
471                          , p_conv_type       => l_fu_exc_type
472                          , p_from_amount     => l_fu_plan_amt_rem
473                          , x_return_status   => l_return_status
474                          , x_to_amount       => l_fu_univ_amt_rem
475                          , x_rate            => l_rate
476                          );
477           IF l_return_status = FND_API.g_ret_sts_error THEN
478             RAISE FND_API.g_exc_error;
479           ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
480             RAISE FND_API.g_exc_unexpected_error;
481           END IF;
482           IF l_fu_univ_amt_rem IS NOT NULL THEN
483             l_fu_univ_amt_rem := OZF_UTILITY_PVT.CurrRound(l_fu_univ_amt_rem, l_currency_rec.universal_currency_code);
484           END IF;
485         ELSE
486           l_fu_univ_amt_rem := 0;
487         END IF;
488       END IF;
489 
490       IF l_fu_req_currency = l_currency_rec.transaction_currency_code THEN
491         l_fu_req_amt_rem := l_fu_plan_amt_rem;
492       ELSIF l_fu_req_currency = l_currency_rec.functional_currency_code THEN
493         l_fu_req_amt_rem := l_fu_acctd_amt_rem;
494       ELSIF l_fu_req_currency = l_fu_fund_currency THEN
495         l_fu_req_amt_rem := l_fu_amt_rem;
496       ELSIF l_fu_req_currency = l_currency_rec.universal_currency_code THEN
497         l_fu_req_amt_rem := l_fu_univ_amt_rem;
498       ELSE
499         IF l_fu_plan_amt_rem IS NOT NULL AND l_fu_plan_amt_rem <> 0 THEN
500            OZF_UTILITY_PVT.Convert_Currency
501                          ( p_from_currency   => l_currency_rec.transaction_currency_code
502                          , p_to_currency     => l_fu_req_currency
503                          , p_conv_date       => l_fu_exc_date
504                          , p_conv_type       => l_fu_exc_type
505                          , p_from_amount     => l_fu_plan_amt_rem
506                          , x_return_status   => l_return_status
507                          , x_to_amount       => l_fu_req_amt_rem
508                          , x_rate            => l_rate
509                          );
510             IF l_return_status = FND_API.g_ret_sts_error THEN
511               RAISE FND_API.g_exc_error;
512             ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
513               RAISE FND_API.g_exc_unexpected_error;
514             END IF;
515             IF l_fu_req_amt_rem IS NOT NULL THEN
516               l_fu_req_amt_rem := OZF_UTILITY_PVT.CurrRound(l_fu_req_amt_rem, l_fu_req_currency);
517             END IF;
518         ELSE
519           l_fu_req_amt_rem := 0;
520         END IF;
521       END IF;
522 
523     ELSE
524       l_fu_amt_rem := 0;
525       l_fu_univ_amt_rem := 0;
526     END IF;
527 
528     IF OZF_DEBUG_HIGH_ON THEN
529       OZF_Utility_PVT.debug_message('l_fu_amt_rem  : '||l_fu_amt_rem);
530       OZF_Utility_PVT.debug_message('l_fu_acctd_amt_rem  : '||l_fu_acctd_amt_rem);
531       OZF_Utility_PVT.debug_message('l_fu_plan_amt_rem  : '||l_fu_plan_amt_rem);
532       OZF_Utility_PVT.debug_message('l_fu_univ_amt_rem  : '||l_fu_univ_amt_rem);
533       OZF_Utility_PVT.debug_message('l_fu_req_amt_rem  : '||l_fu_req_amt_rem);
534       OZF_Utility_PVT.debug_message('l_line_util_rec.utilization_id  : '||l_line_util_rec.utilization_id);
535     END IF;
536     --Reduce utilization amount remaining columns of respective currencies.
537    -- Fix for Bug 9776744
538   IF NVL(l_source_object_class,'X') <> 'SD_SUPPLIER' THEN
539           l_counter := px_upd_line_util_tbl.COUNT +1;
540           px_upd_line_util_tbl(l_counter).amount_remaining := l_fu_amt_rem; --ninarasi fix for bug 13530939
541           px_upd_line_util_tbl(l_counter).acctd_amount_remaining := l_fu_acctd_amt_rem;
542           px_upd_line_util_tbl(l_counter).plan_curr_amount_remaining := l_fu_plan_amt_rem;
543           px_upd_line_util_tbl(l_counter).univ_curr_amount_remaining := l_fu_univ_amt_rem;
544           px_upd_line_util_tbl(l_counter).fund_request_amount_remaining := l_fu_req_amt_rem;
545           px_upd_line_util_tbl(l_counter).utilization_id := l_line_util_rec.utilization_id;
546 
547 
548 
549       /*UPDATE ozf_funds_utilized_all_b
550       SET    amount_remaining                  = amount_remaining - l_fu_amt_rem
551            , acctd_amount_remaining          = acctd_amount_remaining - l_fu_acctd_amt_rem
552            , plan_curr_amount_remaining      = plan_curr_amount_remaining - l_fu_plan_amt_rem
553            , univ_curr_amount_remaining      = univ_curr_amount_remaining - l_fu_univ_amt_rem
554            , fund_request_amount_remaining   = fund_request_amount_remaining - l_fu_req_amt_rem
555      WHERE utilization_id = l_line_util_rec.utilization_id;*/
556 
557 
558 
559   END IF; --SD_SUPPLIER check
560 
561   -- Calculate FXGL for association amount.
562   IF  p_mode = 'CALCULATE' THEN
563 
564     l_line_util_rec.fxgl_acctd_amount := Calculate_FXGL_Amount(l_line_util_rec,l_currency_rec);
565     l_line_util_rec.utilized_acctd_amount := l_line_util_rec.acctd_amount - l_line_util_rec.fxgl_acctd_amount;
566 
567     IF OZF_DEBUG_HIGH_ON THEN
568       OZF_Utility_PVT.debug_message('l_line_util_rec.fxgl_acctd_amount  : '||l_line_util_rec.fxgl_acctd_amount );
569       OZF_Utility_PVT.debug_message('l_line_util_rec.utilized_acctd_amount : '||l_line_util_rec.utilized_acctd_amount );
570     END IF;
571 --nepanda : fix for bug # 9508390  - issue # 1
572      l_line_util_rec.util_curr_amount := NVL(l_line_util_rec.util_curr_amount, 0) + l_fu_amt_rem;
573      l_line_util_rec.plan_curr_amount := NVL(l_line_util_rec.plan_curr_amount, 0) + l_fu_req_amt_rem;
574      l_line_util_rec.univ_curr_amount := NVL(l_line_util_rec.univ_curr_amount, 0) + l_fu_univ_amt_rem;
575 
576      IF OZF_DEBUG_HIGH_ON THEN
577         OZF_Utility_PVT.debug_message('l_line_util_rec.util_curr_amount  : '||l_line_util_rec.util_curr_amount );
578         OZF_Utility_PVT.debug_message('l_line_util_rec.plan_curr_amount : '||l_line_util_rec.plan_curr_amount );
579         OZF_Utility_PVT.debug_message('l_line_util_rec.univ_curr_amount : '||l_line_util_rec.univ_curr_amount );
580      END IF;
581 
582   END IF;  --'CALCULATE' check
583 
584   p_line_util_rec :=  l_line_util_rec;
585 
586 
587 
588   FND_MSG_PUB.count_and_get(
589          p_encoded => FND_API.g_false,
590          p_count   => x_msg_count,
591          p_data    => x_msg_data
592   );
593 
594   IF OZF_DEBUG_HIGH_ON THEN
595      OZF_Utility_PVT.debug_message(l_full_name ||': end');
596   END IF;
597 
598 EXCEPTION
599   WHEN FND_API.g_exc_error THEN
600     ROLLBACK TO Update_Fund_Utils;
601     x_return_status := FND_API.g_ret_sts_error;
602     FND_MSG_PUB.count_and_get (
603            p_encoded => FND_API.g_false
604           ,p_count   => x_msg_count
605           ,p_data    => x_msg_data
606     );
607 
608   WHEN FND_API.g_exc_unexpected_error THEN
609     ROLLBACK TO Update_Fund_Utils;
610     x_return_status := FND_API.g_ret_sts_unexp_error ;
611     FND_MSG_PUB.count_and_get (
612            p_encoded => FND_API.g_false
613           ,p_count   => x_msg_count
614           ,p_data    => x_msg_data
615     );
616 
617   WHEN OTHERS THEN
618     ROLLBACK TO Update_Fund_Utils;
619     x_return_status := FND_API.g_ret_sts_unexp_error ;
620     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
621       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
622     END IF;
623     FND_MSG_PUB.count_and_get(
624            p_encoded => FND_API.g_false
625           ,p_count   => x_msg_count
626           ,p_data    => x_msg_data
627     );
628 
629 END Update_Fund_Utils;
630 
631 
632 ---------------------------------------------------------------------
633 -- PROCEDURE
634 --   Get_Customer_For_Earnings
635 --    Helper procedure called by Get_Utiz_Sql_Stmt only.
636 --
637 -- PARAMETERS
638 --    p_cust_account_id   : Claiming customer account id
639 --    p_relationship_type : Relationship_type
640 --    p_related_cust_account_id : Related customer account id
641 --    p_buy_group_party_id : Buying group/member party id
642 --    p_select_cust_children_flag : Include all member earnings
643 --
644 -- HISTORY
645 --   14-FEB-2003  yizhang  Create.
646 --   05-MAY-2003  yizhang  Use FND_DSQL for dynamic sql and bind vars
647 --   28-feb-06   azahmed   modified for bugfix 4958714
648 ---------------------------------------------------------------------
649 PROCEDURE Get_Customer_For_Earnings(
650    p_cust_account_id           IN  NUMBER
651   ,p_relationship_type         IN  VARCHAR2
652   ,p_related_cust_account_id   IN  NUMBER
653   ,p_buy_group_party_id        IN  NUMBER
654   ,p_select_cust_children_flag IN  VARCHAR2
655 )
656 IS
657 
658 l_bg_is_parent_of_cust      NUMBER        := 0;
659 l_bg_is_parent_of_relcust   NUMBER        := 0;
660 
661 CURSOR csr_buy_group (cv_bg_party_id IN NUMBER, cv_cust_account_id IN NUMBER) IS
662   SELECT COUNT(seg.party_id)
663   FROM ams_party_market_segments seg
664      , hz_cust_accounts hca2
665   WHERE seg.market_qualifier_reference = cv_bg_party_id
666   AND hca2.party_id = seg.party_id
667   AND seg.market_qualifier_type = 'BG'
668   AND hca2.cust_account_id = cv_cust_account_id
669   AND seg.market_qualifier_reference <> seg.party_id;
670 
671 
672 BEGIN
673   IF p_select_cust_children_flag IS NULL OR
674      p_select_cust_children_flag = 'F' OR
675      p_select_cust_children_flag = 'N' THEN
676     -- not to include member earnings
677     IF p_buy_group_party_id IS NOT NULL THEN
678       FND_DSQL.add_text(' IN (SELECT c.cust_account_id FROM hz_cust_accounts c');
679       FND_DSQL.add_text(' WHERE c.party_id = ');
680       FND_DSQL.add_bind(p_buy_group_party_id);
681       FND_DSQL.add_text(')');
682     ELSIF p_relationship_type IS NOT NULL AND
683        p_related_cust_account_id IS NULL
684     THEN
685       FND_DSQL.add_text(' IN (SELECT related_cust_account_id FROM hz_cust_acct_relate');
686       FND_DSQL.add_text(' WHERE cust_account_id = ');
687       FND_DSQL.add_bind(p_cust_account_id);
688       FND_DSQL.add_text(' AND NVL(relationship_type,''ALL'') = ');--For Bug 13489587 to search as ALL for having relationship_type of related customer as null.
689       FND_DSQL.add_bind(p_relationship_type);
690       FND_DSQL.add_text(')');
691     ELSE
692       FND_DSQL.add_text(' = ');
693       IF p_related_cust_account_id IS NOT NULL THEN
694         FND_DSQL.add_bind(p_related_cust_account_id);
695       ELSE
696         FND_DSQL.add_bind(p_cust_account_id);
697       END IF;
698     END IF;
699 
700   ELSIF p_select_cust_children_flag = 'T' OR p_select_cust_children_flag = 'Y' THEN
701     -- to include member earnings
702     IF p_buy_group_party_id IS NOT NULL THEN
703       -- if buying group is parent of customer, do not include members
704       OPEN csr_buy_group(p_buy_group_party_id, p_cust_account_id);
705       FETCH csr_buy_group INTO l_bg_is_parent_of_cust;
706       CLOSE csr_buy_group;
707 
708       IF l_bg_is_parent_of_cust <> 1 THEN
709         -- if buying group is parent of related customer, do not include members
710         IF p_related_cust_account_id IS NOT NULL THEN
711           OPEN csr_buy_group(p_buy_group_party_id, p_related_cust_account_id);
712           FETCH csr_buy_group INTO l_bg_is_parent_of_relcust;
713           CLOSE csr_buy_group;
714         END IF;
715       END IF;
716     END IF;
717 
718     IF l_bg_is_parent_of_cust = 1 OR l_bg_is_parent_of_relcust = 1 THEN
719       FND_DSQL.add_text(' IN (SELECT c.cust_account_id ');
720       FND_DSQL.add_text(' FROM hz_cust_accounts c ');
721       FND_DSQL.add_text(' WHERE c.party_id = ');
722       FND_DSQL.add_bind(p_buy_group_party_id);
723       FND_DSQL.add_text('  OR  c.cust_account_id = ');
724         IF  p_related_cust_account_id is not NULL THEN
725                 FND_DSQL.add_bind(p_related_cust_account_id);
726         ELSE
727                 FND_DSQL.add_bind(p_cust_account_id);
728         END IF;
729      FND_DSQL.add_text(')');
730 
731     ELSE
732       IF p_buy_group_party_id IS NOT NULL THEN
733         FND_DSQL.add_text(' IN (SELECT c2.cust_account_id ');
734         FND_DSQL.add_text(' FROM ams_party_market_segments sg, hz_cust_accounts c2 ');
735         FND_DSQL.add_text(' WHERE sg.market_qualifier_type = ''BG'' ');
736         FND_DSQL.add_text(' AND sg.party_id = c2.party_id ');
737         FND_DSQL.add_text(' AND sg.market_qualifier_reference = ');
738         FND_DSQL.add_bind(p_buy_group_party_id);
739         FND_DSQL.add_text(')');
740       ELSE
741       -- Modified for Bugfix 5346249
742         FND_DSQL.add_text(' IN (SELECT cust2.cust_account_id FROM  hz_cust_accounts cust2  ');
743         FND_DSQL.add_text(' WHERE cust2.party_id IN (SELECT seg.party_id from ');
744         FND_DSQL.add_text(' ams_party_market_segments seg ,hz_cust_accounts cust1 ');
745         FND_DSQL.add_text(' where seg.market_qualifier_type = ''BG'' ');
746         FND_DSQL.add_text(' and seg.market_qualifier_reference =  cust1.party_id ');
747         FND_DSQL.add_text(' and cust1.cust_account_id = ');
748         IF p_related_cust_account_id IS NOT NULL THEN
749           FND_DSQL.add_bind(p_related_cust_account_id);
750         ELSE
751           FND_DSQL.add_bind(p_cust_account_id);
752         END IF;
753         FND_DSQL.add_text(')');
754         FND_DSQL.add_text(')');
755       END IF;
756     END IF;
757   END IF;
758 
759 END Get_Customer_For_Earnings;
760 
761 ---------------------------------------------------------------------
762 -- PROCEDURE
763 --   Copy_Util_Flt
764 --    Helper procedure called by Get_Utiz_Sql_Stmt only.
765 --
766 -- PARAMETERS
767 --
768 -- HISTORY
769 --   16-FEB-2004  yizhang  Create.
770 ---------------------------------------------------------------------
771 PROCEDURE Copy_Util_Flt(
772    px_funds_util_flt           IN OUT NOCOPY funds_util_flt_type
773 )
774 IS
775 
776 l_line_util_flt     funds_util_flt_type;
777 l_offer_id          NUMBER;
778 l_offer_type        VARCHAR2(30);
779 l_reference_type    VARCHAR2(30);
780 l_reference_id      NUMBER;
781 
782 CURSOR csr_line_flt(cv_claim_line_id IN NUMBER) IS
783   SELECT ln.activity_type
784   ,      ln.activity_id
785   ,      ln.offer_type
786   ,      ln.source_object_class
787   ,      ln.source_object_id
788   ,      ln.item_type
789   ,      ln.item_id
790   ,      ln.relationship_type
791   ,      ln.related_cust_account_id
792   ,      ln.buy_group_party_id
793   ,      ln.select_cust_children_flag
794   ,      cla.cust_account_id
795   ,      ln.earnings_end_date
796   ,      ln.claim_currency_amount
797   ,      cla.source_object_class
798   ,      cla.source_object_id
799   FROM ozf_claim_lines ln
800   ,    ozf_claims cla
801   WHERE ln.claim_id = cla.claim_id
802   AND ln.claim_line_id = cv_claim_line_id;
803 
804 CURSOR csr_offer_id(cv_request_id IN NUMBER) IS
805   SELECT offer_id
806   FROM ozf_request_headers_all_b
807   WHERE request_header_id = cv_request_id;
808 
809 CURSOR csr_offer_type(cv_offer_id IN NUMBER) IS
810   SELECT offer_type
811   FROM ozf_offers
812   WHERE qp_list_header_id = cv_offer_id;
813 
814 --Ship - Debit Enhancements / Added by Pranay
815 CURSOR csr_sd_offer_id(cv_request_id IN NUMBER) IS
816   SELECT offer_id
817   FROM ozf_sd_request_headers_all_b
818   WHERE request_header_id = cv_request_id;
819 --Bug# 8632964 fixed by anuj & muthsubr (+)
820 CURSOR sysparam_accrual_flag_csr (p_resale_batch_id IN NUMBER)
821 IS
822 SELECT NVL(ospa.ship_debit_accrual_flag, 'F')
823 FROM ozf_sys_parameters_all ospa,
824      ozf_resale_batches_all orba
825 WHERE ospa.org_id = orba.org_id
826 AND orba.resale_batch_id = p_resale_batch_id;
827 
828 l_accrual_flag VARCHAR2(1);
829 --Bug# 8632964 fixed by anuj & muthsubr (-)
830 
831 BEGIN
832    OPEN csr_line_flt(px_funds_util_flt.claim_line_id);
833    FETCH csr_line_flt INTO l_line_util_flt.activity_type
834                          , l_line_util_flt.activity_id
835                          , l_line_util_flt.offer_type
836                          , l_line_util_flt.document_class
837                          , l_line_util_flt.document_id
838                          , l_line_util_flt.product_level_type
839                          , l_line_util_flt.product_id
840                          , l_line_util_flt.relationship_type
841                          , l_line_util_flt.related_cust_account_id
842                          , l_line_util_flt.buy_group_party_id
843                          , l_line_util_flt.select_cust_children_flag
844                          , l_line_util_flt.cust_account_id
845                          , l_line_util_flt.end_date
846                          , l_line_util_flt.total_amount
847                          , l_reference_type
848                          , l_reference_id;
849    CLOSE csr_line_flt;
850 
851    IF px_funds_util_flt.activity_type IS NULL THEN
852       px_funds_util_flt.activity_type := l_line_util_flt.activity_type;
853    END IF;
854    IF px_funds_util_flt.activity_id IS NULL THEN
855       px_funds_util_flt.activity_id := l_line_util_flt.activity_id;
856    END IF;
857    IF px_funds_util_flt.offer_type IS NULL THEN
858       px_funds_util_flt.offer_type := l_line_util_flt.offer_type;
859    END IF;
860    IF px_funds_util_flt.document_class IS NULL THEN
861       px_funds_util_flt.document_class := l_line_util_flt.document_class;
862    END IF;
863    IF px_funds_util_flt.document_id IS NULL THEN
864       px_funds_util_flt.document_id := l_line_util_flt.document_id;
865    END IF;
866    IF px_funds_util_flt.product_level_type IS NULL THEN
867       px_funds_util_flt.product_level_type := l_line_util_flt.product_level_type;
868    END IF;
869    IF px_funds_util_flt.product_id IS NULL THEN
870       px_funds_util_flt.product_id := l_line_util_flt.product_id;
871    END IF;
872    IF px_funds_util_flt.relationship_type IS NULL THEN
873       px_funds_util_flt.relationship_type := l_line_util_flt.relationship_type;
874    END IF;
875    IF px_funds_util_flt.related_cust_account_id IS NULL THEN
876       px_funds_util_flt.related_cust_account_id := l_line_util_flt.related_cust_account_id;
877    END IF;
878    IF px_funds_util_flt.buy_group_party_id IS NULL THEN
879       px_funds_util_flt.buy_group_party_id := l_line_util_flt.buy_group_party_id;
880    END IF;
881    IF px_funds_util_flt.select_cust_children_flag IS NULL THEN
882       px_funds_util_flt.select_cust_children_flag := l_line_util_flt.select_cust_children_flag;
883    END IF;
884    IF px_funds_util_flt.cust_account_id IS NULL THEN
885       px_funds_util_flt.cust_account_id := l_line_util_flt.cust_account_id;
886    END IF;
887    IF px_funds_util_flt.end_date IS NULL THEN
888       px_funds_util_flt.end_date := l_line_util_flt.end_date;
889    END IF;
890    IF px_funds_util_flt.total_amount IS NULL THEN
891 --      px_funds_util_flt.total_amount := l_line_util_flt.total_amount;
892         NULL; -- If null, then leave as null implies line is to be deleted. Bugfix 5101106
893 
894    END IF;
895 
896    IF px_funds_util_flt.reference_type IS NULL THEN
897       IF l_reference_type = 'REFERRAL' THEN
898          px_funds_util_flt.reference_type := 'LEAD_REFERRAL';
899          px_funds_util_flt.reference_id := l_reference_id;
900          ELSIF l_reference_type = 'BATCH' THEN
901          --Bug# 8632964 fixed by anuj & muthsubr (+)
902          OPEN sysparam_accrual_flag_csr (l_reference_id);
903          FETCH sysparam_accrual_flag_csr INTO l_accrual_flag;
904          CLOSE sysparam_accrual_flag_csr;
905          IF l_accrual_flag = 'T' THEN
906          -- Added for Bug 4997509
907          px_funds_util_flt.reference_type := l_reference_type;
908          px_funds_util_flt.reference_id := l_reference_id;
909          END IF;
910          --Bug# 8632964 fixed by anuj & muthsubr (-)
911       END IF;
912    END IF;
913 
914    -- for special price request and soft fund, search by offer
915    IF px_funds_util_flt.activity_type IN ('SPECIAL_PRICE', 'SOFT_FUND') THEN
916       px_funds_util_flt.activity_type := 'OFFR';
917 
918       IF px_funds_util_flt.activity_id IS NOT NULL THEN
919          OPEN csr_offer_id(px_funds_util_flt.activity_id);
920          FETCH csr_offer_id INTO l_offer_id;
921          CLOSE csr_offer_id;
922 
923          px_funds_util_flt.activity_id := l_offer_id;
924       END IF;
925    END IF;
926 
927 --Ship - Debit Enhancements / Added by Pranay
928    IF px_funds_util_flt.activity_type = 'SD_REQUEST' THEN
929      px_funds_util_flt.activity_type := 'OFFR';
930 
931      IF px_funds_util_flt.activity_id IS NOT NULL THEN
932        OPEN csr_sd_offer_id(px_funds_util_flt.activity_id);
933        FETCH csr_sd_offer_id INTO l_offer_id;
934        CLOSE csr_sd_offer_id;
935        px_funds_util_flt.activity_id := l_offer_id;
936      END IF;
937    END IF;
938 
939    -- set offer_type
940    IF px_funds_util_flt.offer_type IS NULL AND
941       px_funds_util_flt.activity_type = 'OFFR' AND
942       px_funds_util_flt.activity_id IS NOT NULL
943    THEN
944       OPEN csr_offer_type(px_funds_util_flt.activity_id);
945       FETCH csr_offer_type INTO l_offer_type;
946       CLOSE csr_offer_type;
947 
948       px_funds_util_flt.offer_type := l_offer_type;
949    END IF;
950 
951 END Copy_Util_Flt;
952 
953 ---------------------------------------------------------------------
954 -- PROCEDURE
955 --   Get_Utiz_Sql_Stmt_Where_Clause
956 --
957 -- PARAMETERS
958 --
959 -- NOTE
960 --
961 -- HISTORY
962 --   17-FEB-2004  yizhang  Create.
963 --   08-AUg-2006  azahmed Modified for FXGL ER: Added condition fu.currency_code = claim_curr
964 --   26-Jun-2009  kpatro  Corrected the GSCC Error.
965 ---------------------------------------------------------------------
966 PROCEDURE Get_Utiz_Sql_Stmt_Where_Clause(
967    p_summary_view        IN  VARCHAR2  := NULL
968   ,p_funds_util_flt      IN  funds_util_flt_type
969   ,px_currency_rec       IN OUT  NOCOPY currency_rec_type
970 )
971 IS
972 l_api_name     CONSTANT VARCHAR2(30)   := 'Get_Utiz_Sql_Stmt_Where_Clause';
973 l_full_name    CONSTANT VARCHAR2(60)   := g_pkg_name ||'.'|| l_api_name;
974 l_return_status         VARCHAR2(1);
975 
976 l_funds_util_flt        funds_util_flt_type  := NULL;
977 l_cust_account_id       NUMBER         := NULL;
978 l_scan_data_flag        VARCHAR2(1)    := 'N';
979 l_org_id                NUMBER;
980 l_currency_rec          currency_rec_type := px_currency_rec;
981 
982 CURSOR csr_claim_currency(cv_claim_line_id IN NUMBER) IS
983 SELECT currency_code
984 FROM ozf_claim_lines
985 where claim_line_id = cv_claim_line_id;
986 
987 
988 BEGIN
989    --------------------- start -----------------------
990    l_org_id := FND_PROFILE.VALUE('AMS_ITEM_ORGANIZATION_ID');
991 
992    l_funds_util_flt := p_funds_util_flt;
993 
994     -- get claim currency
995     OPEN csr_claim_currency(l_funds_util_flt.claim_line_id);
996    FETCH csr_claim_currency INTO l_currency_rec.claim_currency_code;
997     CLOSE csr_claim_currency;
998     -- bug fix 4338584
999    -- when a fund line utilization is updated from the UI the account id should be picked up from the record
1000    l_cust_account_id := l_funds_util_flt.cust_account_id;
1001 
1002    IF l_funds_util_flt.offer_type = 'SCAN_DATA' THEN
1003       l_scan_data_flag := 'Y';
1004    END IF;
1005 
1006    -- Added Debug For Multi Currency - kpatro
1007     IF OZF_DEBUG_HIGH_ON THEN
1008       OZF_Utility_PVT.debug_message('----- Get_Utiz_Sql_Stmt_Where_Clause:Start -----');
1009       OZF_Utility_PVT.debug_message('l_currency_rec.claim_currency_code        : ' || l_currency_rec.claim_currency_code);
1010       OZF_Utility_PVT.debug_message('l_currency_rec.functional_currency_code   : ' || l_currency_rec.functional_currency_code);
1011       OZF_Utility_PVT.debug_message('l_currency_rec.transaction_currency_code  : ' || l_currency_rec.transaction_currency_code);
1012       OZF_Utility_PVT.debug_message('p_summary_view         : ' || p_summary_view);
1013       OZF_Utility_PVT.debug_message('l_funds_util_flt.utiz_currency_code         : ' || l_funds_util_flt.utiz_currency_code);
1014       OZF_Utility_PVT.debug_message('Process_By         : ' || l_funds_util_flt.autopay_check);
1015       OZF_Utility_PVT.debug_message('----------------------------Get_Utiz_Sql_Stmt_Where_Clause:End ------------');
1016    END IF;
1017     -- Bug5059770: Allow pay over for offers with zero available amount
1018   --Bug 5154157 : Reverted change made for bug 4927201 as this will not be called if
1019    --  total amount is to be overpaid
1020    -- Modified for FXGL ER: only accruals in claim currency must be returned
1021    -- Modified conditions for R12 Multicurrency / Pranay
1022    IF p_summary_view IS NULL OR p_summary_view <> 'DEL_GRP_LINE_UTIL' THEN
1023 
1024       -- Added For Multi Currency - kpatro
1025       IF (p_summary_view IN ('AUTOPAY','AUTOPAY_LINE') AND l_funds_util_flt.autopay_check = 'AUTOPAY') THEN
1026          FND_DSQL.add_text(' AND fu.plan_curr_amount_remaining <> 0 ');
1027       ELSE
1028          FND_DSQL.add_text(' AND (DECODE(NVL('''||l_currency_rec.claim_currency_code||''',fu.plan_currency_code), fu.plan_currency_code, fu.plan_curr_amount_remaining, fu.acctd_amount_remaining))<> 0 ');
1029       END IF;
1030 
1031    END IF;
1032 
1033      IF l_currency_rec.transaction_currency_code IS NOT NULL THEN --restrict for public API
1034          FND_DSQL.add_text(' AND fu.plan_currency_code = '''||l_currency_rec.transaction_currency_code||''''); --kdass
1035      END IF;
1036 
1037 
1038    -- for lead referral accruals, set utilization_type as LEAD_ACCRUAL
1039    -- Fixed for Bug4576309
1040    -- Modified for Bug4997509 to match ClaimAssoVO.getCommonWhereClause
1041    IF l_funds_util_flt.utilization_type IS NULL OR l_funds_util_flt.utilization_type = 'ACCRUAL' THEN
1042        IF l_funds_util_flt.reference_type = 'LEAD_REFERRAL' THEN
1043            FND_DSQL.add_text(' AND fu.utilization_type IN (''LEAD_ACCRUAL'', ''LEAD_ADJUSTMENT'') ' );
1044        ELSIF l_funds_util_flt.reference_type = 'BATCH' THEN
1045            FND_DSQL.add_text(' AND fu.utilization_type = ''CHARGEBACK'' ');
1046        ELSE
1047            FND_DSQL.add_text('  AND fu.utilization_type IN (''ACCRUAL'', ''ADJUSTMENT'') ' );
1048        END IF;
1049    ELSE
1050        FND_DSQL.add_text(' AND fu.utilization_type = ');
1051        FND_DSQL.add_bind(l_funds_util_flt.utilization_type);
1052    END IF;
1053 
1054    IF l_funds_util_flt.utilization_type = 'ADJUSTMENT' THEN
1055       FND_DSQL.add_text(' AND fu.cust_account_id IS NULL ');
1056    ELSE
1057       -- bug fix 4338584
1058       IF l_funds_util_flt.cust_account_id IS NOT NULL AND l_scan_data_flag = 'N' AND l_funds_util_flt.run_mode is not null THEN
1059          FND_DSQL.add_text(' AND fu.cust_account_id');
1060 
1061          Get_Customer_For_Earnings(
1062             p_cust_account_id           => l_funds_util_flt.cust_account_id
1063            ,p_relationship_type         => l_funds_util_flt.relationship_type
1064            ,p_related_cust_account_id   => l_funds_util_flt.related_cust_account_id
1065            ,p_buy_group_party_id        => l_funds_util_flt.buy_group_party_id
1066            ,p_select_cust_children_flag => l_funds_util_flt.select_cust_children_flag
1067          );
1068 
1069       ELSIF l_cust_account_id IS NOT NULL AND l_scan_data_flag = 'N' THEN
1070          FND_DSQL.add_text(' AND fu.cust_account_id = ');
1071          FND_DSQL.add_bind(l_cust_account_id);
1072       END IF;
1073    END IF;
1074 
1075    -- Add fund_id as search filter for claim autopay program.
1076    IF l_funds_util_flt.fund_id IS NOT NULL THEN
1077       FND_DSQL.add_text(' AND fu.fund_id = ');
1078       FND_DSQL.add_bind(l_funds_util_flt.fund_id);
1079    END IF;
1080 
1081    IF l_funds_util_flt.activity_id IS NOT NULL THEN
1082       FND_DSQL.add_text(' AND fu.plan_id = ');
1083       FND_DSQL.add_bind(l_funds_util_flt.activity_id);
1084    END IF;
1085 
1086    IF l_funds_util_flt.reference_type IS NOT NULL THEN
1087       FND_DSQL.add_text(' AND fu.reference_type = ');
1088       FND_DSQL.add_bind(l_funds_util_flt.reference_type);
1089    END IF;
1090 
1091    IF l_funds_util_flt.reference_id IS NOT NULL THEN
1092       FND_DSQL.add_text(' AND fu.reference_id = ');
1093       FND_DSQL.add_bind(l_funds_util_flt.reference_id);
1094    END IF;
1095 
1096    IF l_funds_util_flt.activity_product_id IS NOT NULL THEN
1097       FND_DSQL.add_text(' AND fu.activity_product_id = ');
1098       FND_DSQL.add_bind(l_funds_util_flt.activity_product_id);
1099    END IF;
1100 
1101    IF l_funds_util_flt.schedule_id IS NOT NULL AND l_scan_data_flag = 'N' THEN
1102       FND_DSQL.add_text(' AND fu.component_type = ''CSCH'' ');
1103       FND_DSQL.add_text(' AND fu.component_id = ');
1104       FND_DSQL.add_bind(l_funds_util_flt.schedule_id);
1105    END IF;
1106 
1107    -- fix for 4308165
1108         -- modified for bugfix 4990767
1109    IF l_funds_util_flt.document_class IS NOT NULL AND l_scan_data_flag = 'N' THEN
1110        IF l_funds_util_flt.document_class IN ('ORDER','TP_ORDER') THEN
1111          FND_DSQL.add_text(' AND fu.object_type = ');
1112          FND_DSQL.add_bind(l_funds_util_flt.document_class);
1113       END IF;
1114    END IF;
1115 
1116    IF l_funds_util_flt.document_id IS NOT NULL AND l_scan_data_flag = 'N' THEN
1117       IF l_funds_util_flt.document_class = 'TP_ORDER' THEN
1118         --//Fix for Bug 14021102
1119          --FND_DSQL.add_text(' AND fu.object_id IN (SELECT chargeback_line_id FROM ozf_chargeback_lines WHERE chargeback_header_id = ');
1120 	 FND_DSQL.add_text(' AND fu.object_id IN (SELECT resale_line_id FROM ozf_resale_lines WHERE resale_header_id = ');
1121          FND_DSQL.add_bind(l_funds_util_flt.document_id);
1122          FND_DSQL.add_text(') ');
1123       ELSE
1124        IF l_funds_util_flt.document_class = 'ORDER' THEN
1125          FND_DSQL.add_text(' AND fu.object_id = ');
1126          FND_DSQL.add_bind(l_funds_util_flt.document_id);
1127       END IF;
1128       END IF;
1129    END IF;
1130 
1131    IF (l_funds_util_flt.product_level_type = 'PRODUCT' OR
1132       l_funds_util_flt.product_level_type IS NULL) AND
1133       l_funds_util_flt.product_id IS NOT NULL THEN
1134       FND_DSQL.add_text(' AND ((fu.product_level_type = ''PRODUCT'' ');
1135       FND_DSQL.add_text(' AND fu.product_id = ');
1136       FND_DSQL.add_bind(l_funds_util_flt.product_id);
1137       FND_DSQL.add_text(' ) OR (fu.product_level_type = ''FAMILY'' ');
1138       FND_DSQL.add_text(' AND fu.product_id IN (select category_id from mtl_item_categories ');
1139       FND_DSQL.add_text(' where inventory_item_id = ');
1140       FND_DSQL.add_bind(l_funds_util_flt.product_id);
1141       FND_DSQL.add_text(' and organization_id = ');
1142       FND_DSQL.add_bind(l_org_id);
1143       FND_DSQL.add_text(' ))) ');
1144    ELSIF l_funds_util_flt.product_level_type = 'FAMILY' AND
1145       l_funds_util_flt.product_id IS NOT NULL THEN
1146       FND_DSQL.add_text(' AND ((fu.product_level_type = ''FAMILY'' ');
1147       FND_DSQL.add_text(' AND fu.product_id = ');
1148       FND_DSQL.add_bind(l_funds_util_flt.product_id);
1149       FND_DSQL.add_text(' ) OR (fu.product_level_type = ''PRODUCT'' ');
1150       FND_DSQL.add_text(' AND fu.product_id IN (select b.inventory_item_id from eni_denorm_hierarchies a, mtl_item_categories b  ');
1151       FND_DSQL.add_text(' where a.parent_id = ');
1152       FND_DSQL.add_text(l_funds_util_flt.product_id);
1153       FND_DSQL.add_text(' and b.organization_id = ');
1154       FND_DSQL.add_bind(l_org_id);
1155       FND_DSQL.add_text(' and a.object_type = ''CATEGORY_SET'' and b.category_id = a.child_id ');
1156       FND_DSQL.add_text(' ))) ');
1157    ELSIF l_funds_util_flt.product_level_type = 'MEDIA' THEN
1158       FND_DSQL.add_text(' AND fu.product_level_type = ''MEDIA'' ');
1159       IF l_funds_util_flt.product_id IS NOT NULL THEN
1160          FND_DSQL.add_text(' AND fu.product_id = ');
1161          FND_DSQL.add_bind(l_funds_util_flt.product_id);
1162       END IF;
1163    END IF;
1164 
1165     -- Fix for Bug 11793070
1166    IF l_funds_util_flt.end_date IS NOT NULL THEN
1167       FND_DSQL.add_text(' AND trunc(fu.gl_date) <= ');
1168       FND_DSQL.add_bind(l_funds_util_flt.end_date);
1169    END IF;
1170 
1171     -- Fix for Bug 11793070
1172    IF(l_funds_util_flt.period_name IS NOT NULL)THEN
1173 	FND_DSQL.add_text(' AND (select start_date, end_date from ozf_time_ent_qtr where period_name =  ');
1174 	FND_DSQL.add_text(' AND fu.gl_date between start_date and end_date)  ');
1175 	FND_DSQL.add_bind(l_funds_util_flt.period_name);
1176    END IF;
1177 
1178    IF(l_funds_util_flt.year_name IS NOT NULL) THEN
1179       if (l_funds_util_flt.year_name = 'CURRENT') then
1180           FND_DSQL.add_text(' AND fu.year_id = SELECT ent_year_id FROM OZF_TIME_ENT_YEAR WHERE fu.gl_date between start_date and end_date ');
1181       elsif(l_funds_util_flt.year_name = 'PRIOR') then
1182           FND_DSQL.add_text(' AND fu.year_id = ((SELECT ent_year_id FROM OZF_TIME_ENT_YEAR WHERE fu.gl_date between start_date and end_date) - 1) ');
1183       end if;
1184    END IF;
1185   --end Fix for Bug 11793070
1186 
1187    -- Fix for Bug 8402328
1188    IF (l_funds_util_flt.utilization_id IS NOT NULL) THEN
1189       FND_DSQL.add_text(' AND fu.utilization_id = ');
1190       FND_DSQL.add_bind(l_funds_util_flt.utilization_id );
1191    END IF;
1192 
1193    FND_DSQL.add_text(' AND fu.gl_posted_flag = ''Y'' ');
1194 
1195 END Get_Utiz_Sql_Stmt_Where_Clause;
1196 
1197 ---------------------------------------------------------------------
1198 -- PROCEDURE
1199 --   Get_Utiz_Sql_Stmt_From_Clause
1200 --
1201 -- PARAMETERS
1202 --
1203 -- NOTE
1204 --
1205 -- HISTORY
1206 --   17-FEB-2004  yizhang  Create.
1207 ---------------------------------------------------------------------
1208 PROCEDURE Get_Utiz_Sql_Stmt_From_Clause(
1209    p_summary_view        IN  VARCHAR2
1210   ,p_funds_util_flt      IN  funds_util_flt_type
1211   ,p_currency_rec        IN  currency_rec_type
1212 )
1213 IS
1214 l_api_name     CONSTANT VARCHAR2(30)   := 'Get_Utiz_Sql_Stmt_From_Clause';
1215 l_full_name    CONSTANT VARCHAR2(60)   := g_pkg_name ||'.'|| l_api_name;
1216 
1217 l_funds_util_flt        funds_util_flt_type  := NULL;
1218 l_cust_account_id       NUMBER         := NULL;
1219 l_scan_data_flag        VARCHAR2(1)    := 'N';
1220 l_offer_flag            VARCHAR2(1)    := 'Y';
1221 l_price_list_flag       VARCHAR2(1)    := 'Y';
1222 l_resource_id           NUMBER;
1223 l_sales_rep             VARCHAR2(1)    := FND_API.g_false;
1224 l_is_admin              BOOLEAN        :=  FALSE;
1225 l_orgId                 NUMBER;
1226 l_claim_line_id         NUMBER;
1227 l_currency_rec          currency_rec_type := p_currency_rec;
1228 BEGIN
1229    --------------------- initialize -----------------------
1230    IF OZF_DEBUG_HIGH_ON THEN
1231       OZF_Utility_PVT.debug_message(l_full_name||': start');
1232    END IF;
1233 
1234    --------------------- start -----------------------
1235    l_funds_util_flt := p_funds_util_flt;
1236 
1237    IF l_funds_util_flt.offer_type = 'SCAN_DATA' THEN
1238       l_scan_data_flag := 'Y';
1239    END IF;
1240 
1241    IF l_funds_util_flt.activity_type IS NOT NULL THEN
1242       IF l_funds_util_flt.activity_type = 'OFFR' THEN
1243          l_price_list_flag := 'N';
1244       ELSE
1245          l_offer_flag := 'N';
1246       END IF;
1247    END IF;
1248 
1249    IF l_funds_util_flt.run_mode IN ('OFFER_AUTOPAY', 'OFFER_NO_AUTOPAY') THEN
1250       l_price_list_flag := 'N';
1251    END IF;
1252 
1253    IF l_funds_util_flt.offer_type IS NOT NULL THEN
1254       l_price_list_flag := 'N';
1255    END IF;
1256 
1257    l_resource_id := ozf_utility_pvt.get_resource_id(fnd_global.user_id);
1258    l_is_admin := ams_access_PVT.Check_Admin_Access(l_resource_id);
1259    l_orgId := MO_GLOBAL.GET_CURRENT_ORG_ID();
1260 
1261    -- Added Debug For Multi Currency - kpatro
1262    IF OZF_DEBUG_HIGH_ON THEN
1263       OZF_Utility_PVT.debug_message('----- Get_Utiz_Sql_Stmt_From_Clause:Start -----');
1264       OZF_Utility_PVT.debug_message('l_currency_rec.claim_currency_code        : ' || l_currency_rec.claim_currency_code);
1265       OZF_Utility_PVT.debug_message('l_currency_rec.functional_currency_code   : ' || l_currency_rec.functional_currency_code);
1266       OZF_Utility_PVT.debug_message('l_currency_rec.transaction_currency_code  : ' || l_currency_rec.transaction_currency_code);
1267       OZF_Utility_PVT.debug_message('p_summary_view         : ' || p_summary_view);
1268       OZF_Utility_PVT.debug_message('Process_By         : ' || l_funds_util_flt.autopay_check);
1269       OZF_Utility_PVT.debug_message('----------------------------Get_Utiz_Sql_Stmt_From_Clause:End ------------');
1270    END IF;
1271    IF l_offer_flag = 'Y' THEN
1272       -- restrict offer access if user is sales rep
1273       IF l_funds_util_flt.reference_type = 'LEAD_REFERRAL' THEN
1274          l_sales_rep := FND_API.g_false;
1275       ELSE
1276          l_resource_id := ozf_utility_pvt.get_resource_id(fnd_global.user_id);
1277          l_sales_rep := ozf_utility_pvt.has_sales_rep_role(l_resource_id);
1278       END IF;
1279 
1280 
1281       FND_DSQL.add_text('SELECT fu.utilization_id, fu.cust_account_id '||
1282                         ', fu.plan_type, fu.plan_id, o.offer_type, o.autopay_method '||
1283                         ', fu.product_level_type, fu.product_id, fu.acctd_amount_remaining, ');
1284 
1285       -- Added For Multi Currency - kpatro
1286       IF (p_summary_view IN ('AUTOPAY','AUTOPAY_LINE') AND l_funds_util_flt.autopay_check = 'AUTOPAY') THEN
1287           FND_DSQL.add_text( 'fu.plan_curr_amount_remaining amount_remaining, ');
1288       ELSE
1289          FND_DSQL.add_text('DECODE(NVL('''||l_currency_rec.claim_currency_code||''',fu.plan_currency_code), fu.plan_currency_code, fu.plan_curr_amount_remaining, fu.acctd_amount_remaining) amount_remaining, ');
1290       END IF;
1291 
1292           FND_DSQL.add_text('fu.scan_unit_remaining , fu.creation_date, ');
1293 
1294        -- Added For Multi Currency - kpatro
1295       IF (p_summary_view IN ('AUTOPAY','AUTOPAY_LINE') AND l_funds_util_flt.autopay_check = 'AUTOPAY') THEN
1296           FND_DSQL.add_text( 'fu.PLAN_CURRENCY_CODE currency_code, ');
1297       ELSE
1298           FND_DSQL.add_text('DECODE(NVL('''||l_currency_rec.claim_currency_code||''',fu.PLAN_CURRENCY_CODE), fu.PLAN_CURRENCY_CODE, fu.PLAN_CURRENCY_CODE, '''||l_currency_rec.functional_currency_code||''') currency_code, ');
1299       END IF;
1300 
1301           FND_DSQL.add_text( 'fu.bill_to_site_use_id, fu.plan_curr_amount, fu.plan_curr_amount_remaining, fu.plan_currency_code ' || --ninarasi fix for bug 13550004
1302                         'FROM ozf_funds_utilized_all_b fu, ozf_offers o ');
1303       --Fix for Bug 11793070
1304       IF(l_funds_util_flt.period_name IS NOT NULL)THEN
1305 	FND_DSQL.add_text( ', OZF_TIME_ENT_QTR teq ');
1306       END IF;
1307 
1308       --Modified for Bugfix 5346249
1309       FND_DSQL.add_text('WHERE fu.plan_type = ''OFFR'' '||
1310                         'AND fu.plan_id = o.qp_list_header_id ' ||
1311                         'AND fu.org_id = ');
1312       FND_DSQL.add_bind(l_orgId);
1313 
1314      IF l_funds_util_flt.offer_type IS NOT NULL THEN
1315          FND_DSQL.add_text(' AND o.offer_type = ');
1316          FND_DSQL.add_bind(l_funds_util_flt.offer_type);
1317       ELSE
1318          FND_DSQL.add_text(' AND o.offer_type <> ''SCAN_DATA'' ');
1319       END IF;
1320 
1321       IF l_funds_util_flt.run_mode = 'OFFER_AUTOPAY' THEN
1322          FND_DSQL.add_text(' AND o.autopay_flag = ''Y'' ');
1323       ELSIF l_funds_util_flt.run_mode = 'OFFER_NO_AUTOPAY' THEN
1324          FND_DSQL.add_text(' AND (o.autopay_flag IS NULL OR o.autopay_flag = ''N'') ');
1325       END IF;
1326 
1327       IF l_funds_util_flt.offer_payment_method IS NOT NULL THEN
1328          IF l_funds_util_flt.offer_payment_method = 'NULL' THEN
1329             FND_DSQL.add_text(' AND o.autopay_method IS NULL ');
1330          ELSE
1331             FND_DSQL.add_text(' AND o.autopay_method = ');
1332             FND_DSQL.add_bind(l_funds_util_flt.offer_payment_method);
1333          END IF;
1334       END IF;
1335 
1336       IF (l_sales_rep = FND_API.g_true  AND  NOT l_is_admin ) THEN
1337          FND_DSQL.add_text(' AND (o.confidential_flag =''N'' OR ');
1338          FND_DSQL.add_text(' o.confidential_flag IS NULL OR ');
1339          FND_DSQL.add_text(' ( NVL(o.budget_offer_yn, ''N'') = ''N'' AND ');
1340          FND_DSQL.add_text(' EXISTS ( SELECT 1 FROM    ams_act_access_denorm act ');
1341          FND_DSQL.add_text(' WHERE act.object_id = o.qp_list_header_id ');
1342          FND_DSQL.add_text(' AND  act.object_type = ''OFFR'' ');
1343          FND_DSQL.add_text(' AND   act.resource_id= ');
1344          FND_DSQL.add_bind(l_resource_id);
1345          FND_DSQL.add_text('))');
1346          FND_DSQL.add_text(' OR ( NVL(o.budget_offer_yn, ''N'') = ''Y'' ');
1347          FND_DSQL.add_text(' AND EXISTS ( SELECT 1 FROM ams_act_access_denorm act ');
1348          FND_DSQL.add_text(' WHERE act.object_id = fu.fund_id ');
1349          FND_DSQL.add_text(' AND   act.object_type = ''FUND'' ');
1350          FND_DSQL.add_text(' AND   act.resource_id= ' );
1351          FND_DSQL.add_bind(l_resource_id);
1352          FND_DSQL.add_text(')))');
1353       END IF;
1354 
1355       Get_Utiz_Sql_Stmt_Where_Clause (
1356          p_summary_view        => p_summary_view
1357         ,p_funds_util_flt      => l_funds_util_flt
1358         ,px_currency_rec       => l_currency_rec
1359       );
1360 
1361    END IF;
1362 
1363    IF l_offer_flag = 'Y' AND l_price_list_flag = 'Y' THEN
1364       FND_DSQL.add_text('UNION ALL ');
1365    END IF;
1366 
1367    IF l_price_list_flag = 'Y' THEN
1368       FND_DSQL.add_text('SELECT fu.utilization_id, fu.cust_account_id '||
1369                         ', fu.plan_type, fu.plan_id, null, null '||
1370                         ', fu.product_level_type, fu.product_id '||
1371                         ', fu.acctd_amount_remaining,' );
1372       -- Added For Multii Currency - kpatro
1373     IF (p_summary_view IN ('AUTOPAY','AUTOPAY_LINE') AND l_funds_util_flt.autopay_check = 'AUTOPAY' ) THEN
1374        FND_DSQL.add_text( 'fu.plan_curr_amount_remaining amount_remaining, ');
1375     ELSE
1376         FND_DSQL.add_text('DECODE(NVL('''||l_currency_rec.claim_currency_code||''',fu.plan_currency_code), fu.plan_currency_code, fu.plan_curr_amount_remaining, fu.acctd_amount_remaining) amount_remaining, ');
1377     END IF;
1378 
1379        FND_DSQL.add_text('fu.scan_unit_remaining , fu.creation_date, ');
1380 
1381     -- Added For Multii Currency - kpatro
1382     IF (p_summary_view IN ('AUTOPAY','AUTOPAY_LINE') AND l_funds_util_flt.autopay_check = 'AUTOPAY') THEN
1383        FND_DSQL.add_text( 'fu.PLAN_CURRENCY_CODE currency_code, ');
1384     ELSE
1385        FND_DSQL.add_text('DECODE(NVL('''||l_currency_rec.claim_currency_code||''',fu.PLAN_CURRENCY_CODE), fu.PLAN_CURRENCY_CODE, fu.PLAN_CURRENCY_CODE, '''||l_currency_rec.functional_currency_code||''') currency_code, ');
1386     END IF;
1387 
1388           FND_DSQL.add_text('fu.bill_to_site_use_id , fu.plan_curr_amount, fu.plan_curr_amount_remaining, fu.plan_currency_code' ||
1389                         ' FROM ozf_funds_utilized_all_b fu ');
1390 
1391       --Fix for Bug 11793070
1392       IF(l_funds_util_flt.period_name IS NOT NULL)THEN
1393 	FND_DSQL.add_text( ', OZF_TIME_ENT_QTR teq ');
1394       END IF;
1395       FND_DSQL.add_text('WHERE fu.plan_type = ''PRIC'' ' ||
1396                         'AND fu.org_id =');
1397       FND_DSQL.add_bind(l_orgId);
1398       FND_DSQL.add_text(' AND fu.cust_account_id = ');
1399       FND_DSQL.add_bind(l_funds_util_flt.cust_account_id);
1400 
1401       Get_Utiz_Sql_Stmt_Where_Clause(
1402          p_summary_view        => p_summary_view
1403         ,p_funds_util_flt      => l_funds_util_flt
1404         ,px_currency_rec        => l_currency_rec
1405       );
1406    END IF;
1407 
1408 END Get_Utiz_Sql_Stmt_From_Clause;
1409 
1410 ---------------------------------------------------------------------
1411 -- PROCEDURE
1412 --   Get_Utiz_Sql_Stmt
1413 --
1414 -- PARAMETERS
1415 --    p_summary_view     : Available values
1416 --                          1. OZF_AUTOPAY_PVT -- 'AUTOPAY'
1417 --                          2. OZF_CLAIM_LINE_PVT --'ACTIVITY', 'PRODUCT', 'SCHEDULE'
1418 --    p_funds_util_flt   :
1419 --    p_cust_account_id  : Only be used for OZF_AUTOPAY_PVT
1420 --    x_utiz_sql_stmt    : Return datatype is VARCHAR2(500)
1421 --
1422 -- NOTE
1423 --   1. This statement will be used for both OZF_AUTOPAY_PVT and OZF_CLAIM_LINE_PVT
1424 --      to get funds_utilized SQL statement by giving in search criteria.
1425 --
1426 -- HISTORY
1427 --   25-JUN-2002  mchang  Create.
1428 ---------------------------------------------------------------------
1429 PROCEDURE Get_Utiz_Sql_Stmt(
1430    p_api_version         IN  NUMBER
1431   ,p_init_msg_list       IN  VARCHAR2  := FND_API.g_false
1432   ,p_commit              IN  VARCHAR2  := FND_API.g_false
1433   ,p_validation_level    IN  NUMBER    := FND_API.g_valid_level_full
1434 
1435   ,x_return_status       OUT NOCOPY VARCHAR2
1436   ,x_msg_count           OUT NOCOPY NUMBER
1437   ,x_msg_data            OUT NOCOPY VARCHAR2
1438 
1439   ,p_summary_view        IN  VARCHAR2  := NULL
1440   ,p_funds_util_flt      IN  funds_util_flt_type
1441   ,px_currency_rec       IN  OUT NOCOPY currency_rec_type
1442   ,p_cust_account_id     IN  NUMBER    := NULL
1443 
1444   ,x_utiz_sql_stmt       OUT NOCOPY VARCHAR2
1445 )
1446 IS
1447 l_api_version  CONSTANT NUMBER         := 1.0;
1448 l_api_name     CONSTANT VARCHAR2(30)   := 'Get_Utiz_Sql_Stmt';
1449 l_full_name    CONSTANT VARCHAR2(60)   := g_pkg_name ||'.'|| l_api_name;
1450 l_return_status         VARCHAR2(1);
1451 
1452 l_utiz_sql              VARCHAR2(4000) := NULL;
1453 l_utiz_sql_from_clause  VARCHAR2(2000) := NULL;
1454 l_funds_util_flt        funds_util_flt_type  := NULL;
1455 l_line_util_flt         funds_util_flt_type  := NULL;
1456 l_cust_account_id       NUMBER         := NULL;
1457 l_scan_data_flag        VARCHAR2(1)    := 'N';
1458 l_org_id                NUMBER;
1459 l_currency_rec          currency_rec_type := px_currency_rec;
1460 
1461 CURSOR csr_request_offer(cv_request_id IN NUMBER) IS
1462   SELECT o.qp_list_header_id
1463   ,      o.offer_type
1464   FROM   ozf_offers o
1465   ,      ozf_request_headers_all_b r
1466   WHERE  o.qp_list_header_id = r.offer_id
1467   AND    r.request_header_id = cv_request_id;
1468 -- Added For Multi Currency - kpatro (As For Public API this is the starting point)
1469 CURSOR csr_function_currency IS
1470   SELECT gs.currency_code
1471   FROM   gl_sets_of_books gs
1472        , ozf_sys_parameters org
1473   WHERE  org.set_of_books_id = gs.set_of_books_id
1474    AND  org.org_id = MO_GLOBAL.GET_CURRENT_ORG_ID();
1475 
1476 BEGIN
1477    --------------------- initialize -----------------------
1478    IF OZF_DEBUG_HIGH_ON THEN
1479       OZF_Utility_PVT.debug_message(l_full_name||': start');
1480    END IF;
1481 
1482    IF FND_API.to_boolean(p_init_msg_list) THEN
1483       FND_MSG_PUB.initialize;
1484    END IF;
1485 
1486    IF NOT FND_API.compatible_api_call(
1487          l_api_version,
1488          p_api_version,
1489          l_api_name,
1490          g_pkg_name
1491    ) THEN
1492       RAISE FND_API.g_exc_unexpected_error;
1493    END IF;
1494    x_return_status := FND_API.g_ret_sts_success;
1495     -- Added For Multi Currency - kpatro
1496      OPEN  csr_function_currency;
1497     FETCH csr_function_currency INTO l_currency_rec.functional_currency_code;
1498     CLOSE csr_function_currency;
1499 
1500    l_org_id := FND_PROFILE.VALUE('AMS_ITEM_ORGANIZATION_ID');
1501 
1502    --------------------- start -----------------------
1503    l_funds_util_flt := p_funds_util_flt;
1504 
1505    -- default filter parameters based on claim line properties
1506    IF l_funds_util_flt.claim_line_id IS NOT NULL THEN
1507       IF p_summary_view IS NULL OR p_summary_view <> 'DEL_GRP_LINE_UTIL' THEN
1508          copy_util_flt(px_funds_util_flt => l_funds_util_flt);
1509       END IF;
1510    END IF;
1511 
1512    -- for special pricing requests, set offer id
1513    IF l_funds_util_flt.reference_type = 'SPECIAL_PRICE' AND
1514       l_funds_util_flt.reference_id IS NOT NULL AND
1515       l_funds_util_flt.activity_id IS NULL
1516    THEN
1517       l_funds_util_flt.activity_type := 'OFFR';
1518       OPEN csr_request_offer(l_funds_util_flt.reference_id);
1519       FETCH csr_request_offer INTO l_funds_util_flt.activity_id
1520                                  , l_funds_util_flt.offer_type;
1521       CLOSE csr_request_offer;
1522    END IF;
1523 
1524    IF OZF_DEBUG_HIGH_ON THEN
1525       OZF_Utility_PVT.debug_message('----- p_funds_util_flt -----');
1526       OZF_Utility_PVT.debug_message('cust_account_id    : ' || l_funds_util_flt.cust_account_id);
1527       OZF_Utility_PVT.debug_message('activity_type      : ' || l_funds_util_flt.activity_type);
1528       OZF_Utility_PVT.debug_message('activity_id        : ' || l_funds_util_flt.activity_id);
1529       OZF_Utility_PVT.debug_message('offer_type         : ' || l_funds_util_flt.offer_type);
1530       OZF_Utility_PVT.debug_message('product_level_type : ' || l_funds_util_flt.product_level_type);
1531       OZF_Utility_PVT.debug_message('product_id         : ' || l_funds_util_flt.product_id);
1532       OZF_Utility_PVT.debug_message('Process_By         : ' || l_funds_util_flt.autopay_check); -- Added For Multi Currency - kpatro
1533       OZF_Utility_PVT.debug_message('l_currency_rec.universal_currency_code :' || l_currency_rec.universal_currency_code);
1534       OZF_Utility_PVT.debug_message('l_currency_rec.claim_currency_code :' || l_currency_rec.claim_currency_code);
1535       OZF_Utility_PVT.debug_message('l_currency_rec.functional_currency_code :' || l_currency_rec.functional_currency_code);
1536       OZF_Utility_PVT.debug_message('l_currency_rec.transaction_currency_code :' || l_currency_rec.transaction_currency_code);
1537       OZF_Utility_PVT.debug_message('l_currency_rec.association_currency_code :' || l_currency_rec.association_currency_code);
1538       OZF_Utility_PVT.debug_message('----------------------------');
1539    END IF;
1540 
1541    -- use FND_DSQL package to handle dynamic sql and bind variables
1542    FND_DSQL.init;
1543 
1544    IF p_funds_util_flt.offer_type = 'SCAN_DATA' THEN
1545       l_scan_data_flag := 'Y';
1546    END IF;
1547 
1548    IF p_summary_view = 'AUTOPAY' THEN
1549       -- if cust account is specified in autopay parameter, do
1550       -- not group by cust_account_id in query
1551       -- R12 Group By Offer Enhancement
1552       -- Modified for FXGL Enhancement, Need to derive amount_remaining instead of acctd_amount_remaining
1553       -- Also need to group by currency as amount of different currencies cannot be added
1554       -- Modified for R12.1 enhancements, Need to select the value of bill_to_site_use_id.
1555 
1556       IF p_cust_account_id IS NOT NULL AND l_funds_util_flt.group_by_offer = 'N' THEN
1557          FND_DSQL.add_text('SELECT autopay_method, sum(amount_remaining), currency_code, bill_to_site_use_id  ');
1558       ELSIF p_cust_account_id IS NULL AND l_funds_util_flt.group_by_offer = 'N' THEN
1559          FND_DSQL.add_text('SELECT autopay_method, sum(amount_remaining), currency_code , bill_to_site_use_id , cust_account_id ');
1560       ELSIF p_cust_account_id IS NOT NULL AND l_funds_util_flt.group_by_offer = 'Y' THEN
1561          FND_DSQL.add_text('SELECT autopay_method, sum(amount_remaining), currency_code , bill_to_site_use_id , plan_id ');
1562       ELSIF p_cust_account_id IS NULL AND l_funds_util_flt.group_by_offer = 'Y' THEN
1563          FND_DSQL.add_text('SELECT autopay_method, sum(amount_remaining), currency_code , bill_to_site_use_id , cust_account_id, plan_id ');
1564       END IF;
1565       FND_DSQL.add_text( 'FROM (');
1566    ELSIF p_summary_view = 'AUTOPAY_LINE' THEN
1567       FND_DSQL.add_text( 'SELECT cust_account_id, plan_type, plan_id, bill_to_site_use_id '||
1568                          ', product_level_type, product_id '||
1569                          ', sum(amount_remaining), currency_code '||
1570                          'FROM ('||
1571                          'SELECT cust_account_id, plan_type, plan_id '||
1572                          ', decode(product_id, null, null, product_level_type) product_level_type '||
1573                          ', product_id product_id '||
1574                          ', acctd_amount_remaining , amount_remaining, currency_code, bill_to_site_use_id  '||
1575                          'FROM (');
1576    ELSIF p_summary_view = 'DEL_GRP_LINE_UTIL' THEN
1577      -- Modified for FXGL ER(amount selected)
1578       FND_DSQL.add_text( 'SELECT lu.claim_line_util_id, lu.utilization_id, lu.amount, lu.scan_unit, lu.currency_code  '||
1579                          'FROM (');
1580    ELSE
1581    -- Modified for FXGL ER
1582    -- R12 Multicurrency Enhancements: Amount Remaining changed from BUDGET to TRANSACTIONAL currency
1583       FND_DSQL.add_text( 'SELECT utilization_id, amount_remaining, scan_unit_remaining, currency_code, plan_currency_code , plan_curr_amount_remaining , plan_curr_amount , acctd_amount_remaining '|| --ninarasi fix for bug 13550004
1584                          'FROM (');
1585    END IF;
1586 
1587    Get_Utiz_Sql_Stmt_From_Clause(
1588       p_summary_view        => p_summary_view
1589      ,p_funds_util_flt      => l_funds_util_flt
1590      ,p_currency_rec        => l_currency_rec
1591    );
1592 
1593         -- R12.1 autopay enhancement,  Need to select and group by bill_to_site_use_id.
1594         -- for p_summary_view = AUTOPAY and AUTOPAY_LINE.
1595 
1596    IF p_summary_view = 'AUTOPAY' THEN
1597       FND_DSQL.add_text( ') utiz ');
1598       -- R12 Enhancements: Group By Offer for Autopay.
1599       IF p_cust_account_id IS NOT NULL AND l_funds_util_flt.group_by_offer = 'N' THEN
1600          FND_DSQL.add_text('GROUP BY utiz.autopay_method, utiz.currency_code, utiz.bill_to_site_use_id ');
1601       ELSIF p_cust_account_id IS NULL AND l_funds_util_flt.group_by_offer = 'N' THEN
1602          FND_DSQL.add_text('GROUP BY utiz.cust_account_id, utiz.autopay_method, utiz.currency_code, utiz.bill_to_site_use_id ');
1603       ELSIF p_cust_account_id IS NOT NULL AND l_funds_util_flt.group_by_offer = 'Y' THEN
1604          FND_DSQL.add_text('GROUP BY utiz.plan_id, utiz.autopay_method , utiz.currency_code, utiz.bill_to_site_use_id ');
1605       ELSIF p_cust_account_id IS NULL AND l_funds_util_flt.group_by_offer = 'Y' THEN
1606          FND_DSQL.add_text('GROUP BY utiz.cust_account_id,utiz.plan_id, utiz.autopay_method , utiz.currency_code, utiz.bill_to_site_use_id ');
1607       END IF;
1608    ELSIF p_summary_view = 'AUTOPAY_LINE' THEN
1609       FND_DSQL.add_text( ') utiz ) '||
1610                          'GROUP BY cust_account_id, plan_type, plan_id, bill_to_site_use_id, product_level_type, product_id, currency_code '||
1611                          'ORDER BY cust_account_id, plan_type, plan_id, bill_to_site_use_id, product_level_type, product_id ');
1612    ELSIF p_summary_view = 'DEL_GRP_LINE_UTIL' THEN
1613       FND_DSQL.add_text( ') utiz, ozf_claim_lines_util lu '||
1614                          'WHERE lu.utilization_id = utiz.utilization_id '||
1615                          'AND lu.claim_line_id = ');
1616       FND_DSQL.add_bind( l_funds_util_flt.claim_line_id );
1617       FND_DSQL.add_text( ' ORDER BY utiz.creation_date desc ');
1618    ELSE
1619       FND_DSQL.add_text( ') utiz ');
1620       IF l_funds_util_flt.total_amount IS NOT NULL THEN
1621          IF l_funds_util_flt.total_amount >= 0 THEN
1622             FND_DSQL.add_text(' ORDER BY sign(utiz.amount_remaining) asc, utiz.creation_date asc');
1623          ELSE
1624             FND_DSQL.add_text(' ORDER BY sign(utiz.amount_remaining) desc, utiz.creation_date asc');
1625          END IF;
1626       ELSE
1627          FND_DSQL.add_text(' ORDER BY utiz.creation_date asc');
1628       END IF;
1629    END IF;
1630 
1631    x_utiz_sql_stmt := FND_DSQL.get_text(FALSE);
1632 
1633    IF OZF_DEBUG_HIGH_ON THEN
1634       --l_utiz_sql := FND_DSQL.get_text(TRUE);
1635       l_utiz_sql := SUBSTR(FND_DSQL.get_text(TRUE),1,4000);
1636       OZF_Utility_PVT.debug_message('----- UTIZ SQL -----');
1637       OZF_Utility_PVT.debug_message(SUBSTR(l_utiz_sql, 1, 250));
1638       OZF_Utility_PVT.debug_message(SUBSTR(l_utiz_sql, 251, 250));
1639       OZF_Utility_PVT.debug_message(SUBSTR(l_utiz_sql, 501, 250));
1640       OZF_Utility_PVT.debug_message(SUBSTR(l_utiz_sql, 751, 250));
1641       OZF_Utility_PVT.debug_message(SUBSTR(l_utiz_sql, 1001, 250));
1642       OZF_Utility_PVT.debug_message(SUBSTR(l_utiz_sql, 1251, 250));
1643       OZF_Utility_PVT.debug_message(SUBSTR(l_utiz_sql, 1501, 250));
1644       OZF_Utility_PVT.debug_message(SUBSTR(l_utiz_sql, 1751, 250));
1645       OZF_Utility_PVT.debug_message(SUBSTR(l_utiz_sql, 2001, 250));
1646       OZF_Utility_PVT.debug_message(SUBSTR(l_utiz_sql, 2251, 250));
1647       OZF_Utility_PVT.debug_message(SUBSTR(l_utiz_sql, 2751, 250));
1648       OZF_Utility_PVT.debug_message(SUBSTR(l_utiz_sql, 3001, 250));
1649       OZF_Utility_PVT.debug_message(SUBSTR(l_utiz_sql, 3251, 250));
1650       OZF_Utility_PVT.debug_message(SUBSTR(l_utiz_sql, 3501, 250));
1651       OZF_Utility_PVT.debug_message(SUBSTR(l_utiz_sql, 3751, 250));
1652       OZF_Utility_PVT.debug_message('--------------------');
1653       OZF_Utility_PVT.debug_message(l_full_name||': end');
1654    END IF;
1655 
1656 
1657 
1658 EXCEPTION
1659    WHEN OTHERS THEN
1660       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1661       IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
1662          FND_MESSAGE.set_name('OZF', 'OZF_CLAIM_ATPY_UTIZ_STMT_ERR');
1663          FND_MSG_PUB.add;
1664       END IF;
1665 END Get_Utiz_Sql_Stmt;
1666 
1667 
1668 ---------------------------------------------------------------------
1669 -- PROCEDURE
1670 --    Get_Cust_Trade_Profile
1671 --
1672 -- HISTORY
1673 --    15/12/2003  yizhang  Create.
1674 --    05-Sep-08   ateotia  Bug # 7379700 fixed.
1675 --                Claim does not get closed if customer trade profile is set at account level only.
1676 ---------------------------------------------------------------------
1677 PROCEDURE Get_Cust_Trade_Profile(
1678     p_cust_account_id        IN NUMBER
1679    ,x_cust_trade_profile     OUT NOCOPY g_cust_trade_profile_csr%rowtype
1680    ,p_site_use_id            IN NUMBER := NULL
1681 ) IS
1682 l_api_name     CONSTANT VARCHAR2(30)   := 'Get_Cust_Trade_Profile';
1683 l_full_name    CONSTANT VARCHAR2(60)   := g_pkg_name ||'.'|| l_api_name;
1684 
1685 l_cust_trade_profile   g_cust_trade_profile_csr%rowtype;
1686 l_party_id             NUMBER;
1687 
1688 CURSOR csr_get_party_id(cv_cust_account_id IN NUMBER) IS
1689   SELECT party_id
1690   FROM hz_cust_accounts
1691   WHERE cust_account_id = cv_cust_account_id;
1692 
1693 BEGIN
1694    IF OZF_DEBUG_HIGH_ON THEN
1695       OZF_Utility_PVT.debug_message(l_full_name||': start');
1696    END IF;
1697 
1698    -- bug # 7379700 fixed by ateotia (+)
1699    IF p_site_use_id is not null THEN
1700       -- 1. get trade profile by site_use_id
1701       OPEN g_site_trade_profile_csr(p_site_use_id);
1702       FETCH g_site_trade_profile_csr into l_cust_trade_profile;
1703       CLOSE g_site_trade_profile_csr;
1704    END IF;
1705 
1706    IF p_cust_account_id is not null
1707    AND l_cust_trade_profile.trade_profile_id IS NULL THEN
1708       -- 2. if trade profile is not there for site,
1709       --    then get trade profile at customer account level
1710       OPEN g_cust_trade_profile_csr(p_cust_account_id);
1711       FETCH g_cust_trade_profile_csr into l_cust_trade_profile;
1712       CLOSE g_cust_trade_profile_csr;
1713 
1714       -- 3. if trade profile is not there for customer,
1715       --    then get trade profile by party_id level
1716       IF l_cust_trade_profile.trade_profile_id IS NULL THEN
1717          OPEN csr_get_party_id(p_cust_account_id);
1718          FETCH csr_get_party_id INTO l_party_id;
1719          CLOSE csr_get_party_id;
1720 
1721          IF l_party_id IS NOT NULL THEN
1722             OPEN g_party_trade_profile_csr(l_party_id);
1723             FETCH g_party_trade_profile_csr INTO l_cust_trade_profile;
1724             CLOSE g_party_trade_profile_csr;
1725          END IF;
1726       END IF;
1727    END IF;
1728    -- bug # 7379700 fixed by ateotia (-)
1729 
1730    x_cust_trade_profile := l_cust_trade_profile;
1731 
1732    IF OZF_DEBUG_HIGH_ON THEN
1733       OZF_Utility_PVT.debug_message(l_full_name||': end');
1734    END IF;
1735 END Get_Cust_Trade_Profile;
1736 ---------------------------------------------------------------------
1737 -- PROCEDURE
1738 --    Get_Payment_Detail
1739 --
1740 -- PURPOSE
1741 --    This procedure will return the payment method, vendor ID,vendor
1742 --    site ID from customer trade profile
1743 --
1744 -- PARAMETERS
1745 -- p_cust_account       - Cust Account ID
1746 -- x_payment_method     - Payment Method
1747 -- x_vendor_id          - Vendor ID
1748 -- x_vendor_site_id     - Vendor Site ID
1749 --
1750 -- NOTES
1751 -- HISTORY
1752 --   30-APR-2010  KPATRO  Created for ER#9453443.
1753 ---------------------------------------------------------------------
1754 PROCEDURE Get_Payment_Detail(
1755          p_cust_account        IN  NUMBER,
1756          p_billto_site_use_id  IN NUMBER,
1757          x_payment_method      OUT NOCOPY VARCHAR2,
1758          x_vendor_id           OUT NOCOPY NUMBER,
1759          x_vendor_site_id      OUT NOCOPY NUMBER,
1760          x_return_status       OUT NOCOPY VARCHAR2
1761 ) IS
1762 l_api_name     CONSTANT VARCHAR2(30)   := 'Get_Payment_Detail';
1763 l_full_name    CONSTANT VARCHAR2(60)   := g_pkg_name ||'.'|| l_api_name;
1764 
1765 l_cust_trade_profile   g_cust_trade_profile_csr%rowtype;
1766 
1767 BEGIN
1768    -- Initialize API return status to sucess
1769    x_return_status := FND_API.G_RET_STS_SUCCESS;
1770 
1771    IF OZF_DEBUG_HIGH_ON THEN
1772       OZF_Utility_PVT.debug_message(l_full_name||': start');
1773    END IF;
1774 
1775    -- get payment method information from trade profile
1776     Get_Cust_Trade_Profile(
1777       p_cust_account_id     => p_cust_account
1778      ,x_cust_trade_profile  => l_cust_trade_profile
1779      ,p_site_use_id         => p_billto_site_use_id
1780     );
1781 
1782     IF l_cust_trade_profile.trade_profile_id IS NOT NULL THEN
1783        x_payment_method := l_cust_trade_profile.payment_method;
1784       IF l_cust_trade_profile.payment_method <> FND_API.G_MISS_CHAR THEN
1785 
1786          IF (l_cust_trade_profile.payment_method IN ('CHECK', 'EFT','WIRE','AP_DEBIT','AP_DEFAULT')) THEN
1787            x_vendor_id := l_cust_trade_profile.vendor_id;
1788            x_vendor_site_id := l_cust_trade_profile.vendor_site_id;
1789          END IF;
1790       END IF;
1791 
1792      END IF;
1793 
1794 EXCEPTION
1795    WHEN OTHERS THEN
1796      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1797 END Get_Payment_Detail;
1798 
1799 -- Start - Fix for ER#9453443
1800 
1801 ---------------------------------------------------------------------
1802 -- PROCEDURE
1803 --    Validate_Cust_Trade_Profile
1804 --
1805 -- HISTORY
1806 --    15/12/2003  yizhang  Create.
1807 ---------------------------------------------------------------------
1808 PROCEDURE Validate_Cust_Trade_Profile(
1809     p_cust_trade_profile     IN OUT NOCOPY g_cust_trade_profile_csr%rowtype
1810    ,x_return_status          OUT NOCOPY VARCHAR2
1811 ) IS
1812 l_api_name     CONSTANT VARCHAR2(30)   := 'Validate_Cust_Trade_Profile';
1813 l_full_name    CONSTANT VARCHAR2(60)   := g_pkg_name ||'.'|| l_api_name;
1814 
1815 CURSOR csr_cust_name(cv_cust_account_id IN NUMBER) IS
1816   SELECT CONCAT(CONCAT(party.party_name, ' ('), CONCAT(ca.account_number, ') '))
1817   FROM hz_cust_accounts ca
1818   ,    hz_parties party
1819   WHERE ca.party_id = party.party_id
1820   AND ca.cust_account_id = cv_cust_account_id;
1821 
1822 --Added cursor for Bug 16301629 , to check if both vendor and vendor site is active
1823  CURSOR csr_active_vendor(cv_vendor_id IN NUMBER, cv_vendor_site_id IN NUMBER) IS
1824  SELECT 1 FROM po_vendors pv, po_vendor_sites pvs
1825  WHERE pv.vendor_id = pvs.vendor_id
1826  AND (sysdate between nvl(pv.start_date_active, sysdate-1) and nvl(pv.end_date_active, sysdate+1))
1827  AND (pvs.inactive_date is null or pvs.inactive_date > trunc(sysdate))
1828  AND pv.vendor_id = cv_vendor_id
1829  AND pvs.vendor_site_id = cv_vendor_site_id;
1830 
1831 l_cust_account_id  number := p_cust_trade_profile.cust_account_id;
1832 l_cust_name_num    VARCHAR2(70);
1833 
1834 l_active_vendor_num   NUMBER ; --Bug 16301629
1835 
1836 BEGIN
1837    IF OZF_DEBUG_HIGH_ON THEN
1838       OZF_Utility_PVT.debug_message(l_full_name||': start');
1839    END IF;
1840 
1841    -- Initialize API return status to sucess
1842    x_return_status := FND_API.G_RET_STS_SUCCESS;
1843 
1844    IF p_cust_trade_profile.claim_currency is null OR
1845       p_cust_trade_profile.claim_currency = FND_API.G_MISS_CHAR THEN -- [BUG 4217781 FIXING]
1846       IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
1847          OPEN csr_cust_name(l_cust_account_id);
1848          FETCH csr_cust_name INTO l_cust_name_num;
1849          CLOSE csr_cust_name;
1850 
1851          FND_MESSAGE.Set_Name('OZF', 'OZF_CLAIM_ATPY_CURRENCY_MISS');
1852          FND_MESSAGE.Set_Token('ID',l_cust_name_num);
1853          FND_MSG_PUB.ADD;
1854       END IF;
1855       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1856    END IF;
1857 
1858    IF p_cust_trade_profile.payment_method is null OR
1859       p_cust_trade_profile.payment_method = FND_API.G_MISS_CHAR THEN -- [BUG 4217781 FIXING]
1860       IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
1861          OPEN csr_cust_name(l_cust_account_id);
1862          FETCH csr_cust_name INTO l_cust_name_num;
1863          CLOSE csr_cust_name;
1864 
1865          FND_MESSAGE.Set_Name('OZF', 'OZF_CLAIM_ATPY_PYMTHD_MISS');
1866          FND_MESSAGE.Set_Token('ID',l_cust_name_num);
1867          FND_MSG_PUB.ADD;
1868       END IF;
1869       x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1870    END IF;
1871 
1872   IF p_cust_trade_profile.payment_method = 'CHECK' THEN
1873       IF p_cust_trade_profile.vendor_id is NULL OR
1874          p_cust_trade_profile.vendor_site_id is NULL  THEN
1875          IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
1876             OPEN csr_cust_name(l_cust_account_id);
1877             FETCH csr_cust_name INTO l_cust_name_num;
1878             CLOSE csr_cust_name;
1879 
1880             FND_MESSAGE.Set_Name('OZF', 'OZF_CLAIM_ATPY_VENDOR_MISS');
1881             FND_MESSAGE.Set_Token('ID',l_cust_name_num);
1882             FND_MSG_PUB.ADD;
1883          END IF;
1884          x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1885       ELSE
1886       --Added for Bug 16301629
1887 	OPEN csr_active_vendor(p_cust_trade_profile.vendor_id, p_cust_trade_profile.vendor_site_id);
1888 	FETCH csr_active_vendor INTO l_active_vendor_num;
1889         CLOSE csr_active_vendor;
1890 
1891 	IF l_active_vendor_num is null THEN
1892 	  IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
1893 	    OPEN csr_cust_name(l_cust_account_id);
1894             FETCH csr_cust_name INTO l_cust_name_num;
1895             CLOSE csr_cust_name;
1896 	    FND_MESSAGE.Set_Name('OZF', 'OZF_TRADE_VENDOR_INACTIVE');
1897 	    FND_MESSAGE.Set_Token('ID',l_cust_name_num);
1898             FND_MSG_PUB.ADD;
1899 	  END IF;
1900 	  --if vendor or vendor site is inactive, don't default from Customer Trade Profile. Setting it null.
1901 	 p_cust_trade_profile.vendor_id := null;
1902 	 p_cust_trade_profile.vendor_site_id := null;
1903 	 x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1904 	END IF;
1905      --end bug 16301629
1906       END IF;
1907    ELSIF p_cust_trade_profile.payment_method = 'CREDIT_MEMO' THEN
1908       IF p_cust_trade_profile.site_use_id is NULL THEN
1909          IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
1910             OPEN csr_cust_name(l_cust_account_id);
1911             FETCH csr_cust_name INTO l_cust_name_num;
1912             CLOSE csr_cust_name;
1913 
1914             FND_MESSAGE.Set_Name('OZF', 'OZF_CLAIM_ATPY_SITEID_MISS');
1915             FND_MESSAGE.Set_Token('ID',l_cust_name_num);
1916             FND_MSG_PUB.ADD;
1917          END IF;
1918          x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1919       END IF;
1920    END IF;
1921 
1922    IF OZF_DEBUG_HIGH_ON THEN
1923       OZF_Utility_PVT.debug_message(l_full_name||': end');
1924    END IF;
1925 EXCEPTION
1926    WHEN OTHERS THEN
1927      x_return_status := FND_API.G_RET_STS_UNEXP_ERROR;
1928      IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.G_MSG_LVL_UNEXP_ERROR) THEN
1929         FND_MESSAGE.set_name('OZF', 'OZF_CLAIM_ATPY_CUSTOMER_ERR');
1930         FND_MSG_PUB.add;
1931      END IF;
1932 END Validate_Cust_Trade_Profile;
1933 
1934 ---------------------------------------------------------------------
1935 -- PROCEDURE
1936 --    Get_Prorate_Earnings_Flag
1937 --
1938 -- HISTORY
1939 --    01/21/2003  yizhang  Create.
1940 ---------------------------------------------------------------------
1941 PROCEDURE Get_Prorate_Earnings_Flag(
1942     p_funds_util_flt         IN funds_util_flt_type
1943    ,x_prorate_earnings_flag  OUT NOCOPY VARCHAR2
1944 ) IS
1945 l_api_name     CONSTANT VARCHAR2(30)   := 'Get_Prorate_Earnings_Flag';
1946 l_full_name    CONSTANT VARCHAR2(60)   := g_pkg_name ||'.'|| l_api_name;
1947 
1948 l_prorate_earnings_flag     VARCHAR2(1);
1949 
1950 CURSOR csr_line_flag(cv_claim_line_id IN NUMBER) IS
1951   SELECT prorate_earnings_flag
1952   FROM ozf_claim_lines
1953   WHERE claim_line_id = cv_claim_line_id;
1954 
1955  -- fix for bug 5042046
1956 CURSOR csr_system_flag IS
1957   SELECT NVL(prorate_earnings_flag, 'F')
1958   FROM ozf_sys_parameters
1959   WHERE org_id = MO_GLOBAL.GET_CURRENT_ORG_ID();
1960 
1961 BEGIN
1962    IF OZF_DEBUG_HIGH_ON THEN
1963       OZF_Utility_PVT.debug_message(l_full_name||': start');
1964    END IF;
1965 
1966    IF p_funds_util_flt.prorate_earnings_flag IS NOT NULL THEN
1967       l_prorate_earnings_flag := p_funds_util_flt.prorate_earnings_flag;
1968    ELSE
1969       OPEN csr_line_flag(p_funds_util_flt.claim_line_id);
1970       FETCH csr_line_flag INTO l_prorate_earnings_flag;
1971       CLOSE csr_line_flag;
1972 
1973       IF l_prorate_earnings_flag IS NULL THEN
1974          OPEN csr_system_flag;
1975          FETCH csr_system_flag INTO l_prorate_earnings_flag;
1976          CLOSE csr_system_flag;
1977       END IF;
1978    END IF;
1979    x_prorate_earnings_flag := l_prorate_earnings_flag;
1980 
1981    IF OZF_DEBUG_HIGH_ON THEN
1982       OZF_Utility_PVT.debug_message(l_full_name||': end');
1983    END IF;
1984 END Get_Prorate_Earnings_Flag;
1985 
1986 ---------------------------------------------------------------------
1987 -- PROCEDURE
1988 --    Create_Fund_Adjustment
1989 --
1990 -- PURPOSE
1991 --    This procedure creates a fund adjustment with amount zero
1992 --    for the given offer.
1993 --    Unlike Adjust_Fund_Utilization, which is called after claim
1994 --    is approved, this procedure is called when earnings are
1995 --    associated to the claim.
1996 --
1997 -- HISTORY
1998 --    07/22/2004  yizhang  Create.
1999 --    07/29/2005  sshivali Cleared GSCC warning
2000 --    14/06/06    azahmed  Bugfix 5333804
2001 --     06/08/06   azahmed  Modified for FXGL ER: Adjustments must be created in
2002 --                          fund currency and not in plan currrency
2003 ---------------------------------------------------------------------
2004 PROCEDURE Create_Fund_Adjustment(
2005    p_offer_id           IN  NUMBER
2006   ,p_cust_account_id    IN  NUMBER
2007   ,p_product_id         IN  NUMBER
2008   ,p_product_level_type IN  VARCHAR2
2009   ,p_fund_id            IN  NUMBER
2010   ,p_reference_type     IN  VARCHAR2
2011   ,p_reference_id       IN  NUMBER
2012   ,x_return_status      OUT NOCOPY VARCHAR2
2013   ,x_msg_count           OUT NOCOPY NUMBER
2014   ,x_msg_data            OUT NOCOPY VARCHAR2
2015   ,x_adj_util_id         OUT  NOCOPY NUMBER
2016 )
2017 IS
2018 l_api_name    CONSTANT VARCHAR2(30) := 'Create_Fund_Adjustment';
2019 l_full_name   CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
2020 l_return_status VARCHAR2(1);
2021 
2022 l_currOrgId            NUMBER     := MO_GLOBAL.GET_CURRENT_ORG_ID();
2023 l_cust_account_id      NUMBER;
2024 l_fund_id              NUMBER;
2025 l_act_budget_id        NUMBER;
2026 l_fund_curr_code       VARCHAR2(15);
2027 l_plan_curr_code       VARCHAR2(15);
2028 l_accrual_curr_code    VARCHAR2(15);
2029 
2030 l_act_budgets_rec      ozf_actbudgets_pvt.act_budgets_rec_type;
2031 l_act_util_rec         ozf_actbudgets_pvt.act_util_rec_type ;
2032 
2033 --Bug5333804
2034 -- Reverted back for Bugfix 5154157
2035 CURSOR csr_source_fund(cv_offer_id IN NUMBER, cv_fund_id IN NUMBER) IS
2036 SELECT  budget_source_id,
2037          request_currency,
2038          ARC_ACT_BUDGET_USED_BY,
2039          ACT_BUDGET_USED_BY_ID,
2040          APPROVED_IN_CURRENCY
2041  FROM  ozf_act_budgets
2042  WHERE transfer_type = 'REQUEST'
2043  AND   act_budget_used_by_id = cv_offer_id
2044  AND   budget_source_id  =  cv_fund_id;
2045 
2046 
2047 
2048 l_fu_plan_type  VARCHAR2(30);
2049 l_fu_plan_id      NUMBER;
2050 
2051 CURSOR csr_acc_adjustment(cv_offer_id IN NUMBER, cv_cust_account_id IN NUMBER, cv_fund_id IN NUMBER,
2052                           cv_product_id IN NUMBER, cv_product_level_type IN VARCHAR2) IS
2053   SELECT utilization_id
2054   FROM   ozf_funds_utilized_all_b
2055   WHERE  plan_type = 'OFFR'
2056   AND  org_id =   l_currOrgId
2057   AND    plan_id   = cv_offer_id
2058   AND    fund_id  = cv_fund_id
2059   AND    adjustment_type_id IN ( -11, -1)
2060   AND    ( ( cv_product_id IS NULL AND product_id IS NULL )
2061             OR product_id = cv_product_id )
2062   AND    (  (cv_product_level_type IS NULL AND product_level_type IS NULL )
2063            OR product_level_type = cv_product_level_type )
2064   AND    utilization_type IN ( 'ADJUSTMENT', 'LEAD_ADJUSTMENT')
2065   AND    cust_account_id = cv_cust_account_id;
2066 
2067 BEGIN
2068   ----------------------- initialize --------------------
2069   IF OZF_DEBUG_HIGH_ON THEN
2070      OZF_Utility_PVT.debug_message(l_full_name||': start');
2071   END IF;
2072 
2073   x_return_status := FND_API.g_ret_sts_success;
2074 
2075   -- This package is called only for non scan data offer adjustments
2076 
2077 
2078   -- Check if adjustment preexists
2079   OPEN csr_acc_adjustment(p_offer_id, p_cust_account_id, p_fund_id, p_product_id, p_product_level_type);
2080   FETCH csr_acc_adjustment INTO x_adj_util_id;
2081   CLOSE csr_acc_adjustment;
2082 
2083    IF OZF_DEBUG_HIGH_ON THEN
2084      OZF_Utility_PVT.debug_message('x_adj_util_id:' || x_adj_util_id);
2085    END IF;
2086   IF x_adj_util_id IS NOT NULL THEN
2087      RETURN;
2088   END IF;
2089 
2090   -- create adjustment
2091   OPEN csr_source_fund(p_offer_id, p_fund_id);
2092   FETCH csr_source_fund INTO l_fund_id, l_plan_curr_code, l_fu_plan_type, l_fu_plan_id, l_fund_curr_code;
2093   CLOSE csr_source_fund;
2094 
2095   l_act_util_rec.fund_request_currency_code := OZF_ACTBUDGETS_PVT.Get_Object_Currency
2096                                                   ( p_object          => l_fu_plan_type
2097                                                   , p_object_id       => l_fu_plan_id
2098                                                   , x_return_status   => l_return_status
2099                                                   );
2100 
2101    IF OZF_DEBUG_HIGH_ON THEN
2102            OZF_Utility_PVT.debug_message('Offer sourcing budget: '||l_fund_id);
2103    END IF;
2104 
2105 
2106    l_act_budgets_rec.parent_src_apprvd_amt := 0;
2107    l_act_budgets_rec.request_amount := 0;
2108    l_act_budgets_rec.act_budget_used_by_id := p_offer_id;
2109    l_act_budgets_rec.arc_act_budget_used_by := 'OFFR';
2110    l_act_budgets_rec.budget_source_type := 'OFFR';
2111    l_act_budgets_rec.budget_source_id := p_offer_id;
2112    l_act_budgets_rec.request_currency := l_plan_curr_code;
2113    l_act_budgets_rec.request_date := SYSDATE;
2114    l_act_budgets_rec.status_code := 'APPROVED';
2115    l_act_budgets_rec.user_status_id := ozf_utility_pvt.get_default_user_status (
2116                                       'OZF_BUDGETSOURCE_STATUS'
2117                                       ,l_act_budgets_rec.status_code
2118                                       );
2119    l_act_budgets_rec.transfer_type := 'UTILIZED';
2120    l_act_budgets_rec.approval_date := SYSDATE;
2121    l_act_budgets_rec.approver_id := ozf_utility_pvt.get_resource_id (fnd_global.user_id);
2122    l_act_budgets_rec.parent_source_id := l_fund_id;
2123 
2124    l_act_util_rec.gl_date := SYSDATE;
2125    IF p_reference_type = 'LEAD_REFERRAL' THEN
2126        l_act_util_rec.utilization_type := 'LEAD_ADJUSTMENT';
2127        l_act_util_rec.adjustment_type_id := -1;
2128    ELSE
2129       l_act_util_rec.utilization_type := 'ADJUSTMENT';
2130       l_act_util_rec.adjustment_type_id := -11;
2131    END IF;
2132 
2133    l_act_util_rec.adjustment_type := 'STANDARD';
2134 
2135    l_act_util_rec.cust_account_id := p_cust_account_id;
2136    l_act_util_rec.product_id := p_product_id;
2137    l_act_util_rec.product_level_type := p_product_level_type;
2138 
2139     -- Bug 4729839
2140    l_act_util_rec.org_id := l_currOrgId;
2141 
2142    l_act_util_rec.reference_type := p_reference_type;
2143    l_act_util_rec.reference_id := p_reference_id;
2144    l_act_util_rec.plan_currency_code := l_plan_curr_code; --Added for multicurrency ER
2145 
2146     -- Added for Bug#16301530
2147    l_act_budgets_rec.parent_src_curr := l_fund_curr_code;
2148 
2149    OZF_Fund_Adjustment_PVT.process_act_budgets (
2150                        x_return_status  => l_return_status,
2151                        x_msg_count => x_msg_count,
2152                        x_msg_data   => x_msg_data,
2153                        p_act_budgets_rec => l_act_budgets_rec,
2154                        p_act_util_rec   =>l_act_util_rec,
2155                        x_act_budget_id  => l_act_budget_id
2156                       );
2157    IF l_return_status = fnd_api.g_ret_sts_error THEN
2158            RAISE fnd_api.g_exc_error;
2159    ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2160           RAISE fnd_api.g_exc_unexpected_error;
2161    END IF;
2162 
2163 
2164   -- Query the adjustment created and return the util id
2165   OPEN csr_acc_adjustment(p_offer_id, p_cust_account_id, p_fund_id, p_product_id, p_product_level_type);
2166   FETCH csr_acc_adjustment INTO x_adj_util_id;
2167   CLOSE csr_acc_adjustment;
2168   IF OZF_DEBUG_HIGH_ON THEN
2169      OZF_Utility_PVT.debug_message(' New - 1:x_adj_util_id:' || x_adj_util_id);
2170   END IF;
2171 
2172 
2173   ------------------------- finish -------------------------------
2174   IF OZF_DEBUG_HIGH_ON THEN
2175      OZF_Utility_PVT.debug_message(l_full_name ||': end');
2176   END IF;
2177 
2178 
2179 
2180 EXCEPTION
2181   WHEN FND_API.g_exc_error THEN
2182     x_return_status := FND_API.g_ret_sts_error;
2183 
2184   WHEN FND_API.g_exc_unexpected_error THEN
2185     x_return_status := FND_API.g_ret_sts_unexp_error ;
2186 
2187   WHEN OTHERS THEN
2188     x_return_status := FND_API.g_ret_sts_unexp_error ;
2189     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
2190       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
2191     END IF;
2192 
2193 END Create_Fund_Adjustment;
2194 
2195 ---------------------------------------------------------------------
2196 -- PROCEDURE
2197 --    Create_Line_Util_Tbl
2198 --
2199 -- HISTORY
2200 --    05/11/2001  mchang  Create.
2201 --    05/08/2006  azahmed  Modified for FXGL ER
2202 --    01/09/2009  kpatro   Modified for Bug 7658894
2203 ---------------------------------------------------------------------
2204 PROCEDURE Create_Line_Util_Tbl(
2205     p_api_version            IN    NUMBER
2206    ,p_init_msg_list          IN    VARCHAR2 := FND_API.g_false
2207    ,p_commit                 IN    VARCHAR2 := FND_API.g_false
2208    ,p_validation_level       IN    NUMBER   := FND_API.g_valid_level_full
2209 
2210    ,x_return_status          OUT NOCOPY   VARCHAR2
2211    ,x_msg_data               OUT NOCOPY   VARCHAR2
2212    ,x_msg_count              OUT NOCOPY   NUMBER
2213 
2214    ,p_line_util_tbl          IN    line_util_tbl_type
2215    ,p_currency_rec           IN    currency_rec_type
2216    ,p_mode                   IN    VARCHAR2 := OZF_CLAIM_UTILITY_PVT.g_auto_mode
2217    ,x_error_index            OUT NOCOPY   NUMBER
2218 ) IS
2219 l_api_version       CONSTANT NUMBER       := 1.0;
2220 l_api_name          CONSTANT VARCHAR2(30) := 'Create_Line_Util_Tbl';
2221 l_full_name         CONSTANT VARCHAR2(60) := g_pkg_name || '.' || l_api_name;
2222 l_return_status              VARCHAR2(1);
2223 
2224 l_claim_id                   NUMBER;
2225 l_access                     VARCHAR2(1) := 'N';
2226 l_line_util_amount           NUMBER;
2227 l_claim_currency_code        VARCHAR2(15);
2228 l_claim_exc_rate             NUMBER;
2229 l_claim_exc_date             DATE;
2230 l_claim_exc_type             VARCHAR2(30);
2231 l_org_id                     NUMBER;
2232 l_line_util_rec              line_util_rec_type;
2233 l_line_util_id               NUMBER;
2234 l_currency_rec               currency_rec_type := p_currency_rec;
2235 l_line_util_tbl              line_util_tbl_type;
2236 l_upd_fund_util_tbl          upd_fund_util_tbl_type;
2237 
2238 
2239 CURSOR csr_claim_exc(cv_claim_line_id IN NUMBER) IS
2240   SELECT cla.currency_code
2241   ,      cla.exchange_rate_type
2242   ,      cla.exchange_rate_date
2243   ,      cla.exchange_rate
2244   FROM ozf_claims cla
2245   ,    ozf_claim_lines ln
2246   WHERE ln.claim_id = cla.claim_id
2247   AND ln.claim_line_id = cv_claim_line_id;
2248 
2249 CURSOR csr_claim_id(cv_claim_line_id IN NUMBER) IS
2250   SELECT claim_id
2251   FROM ozf_claim_lines
2252   WHERE claim_line_id = cv_claim_line_id;
2253 
2254  -- Bug fix 7463302
2255  CURSOR csr_claim_line_util_amount(cv_claim_line_util_id IN NUMBER) IS
2256   SELECT nvl(amount,0)
2257   FROM ozf_claim_lines_util
2258   WHERE claim_line_util_id = cv_claim_line_util_id;
2259 
2260 CURSOR csr_function_currency(cv_org_id NUMBER) IS
2261   SELECT gs.currency_code
2262   FROM   gl_sets_of_books gs
2263   ,      ozf_sys_parameters org
2264   WHERE  org.set_of_books_id = gs.set_of_books_id
2265   AND   org.org_id = cv_org_id;
2266 
2267 --nepanda Fix for Bug 9075792 - this code is moved to java layer to ClaimAssoVO
2268 CURSOR csr_line_util_sum_amount(cv_claim_line_id      IN NUMBER) IS
2269   SELECT SUM(amount)
2270   FROM  ozf_claim_lines_util
2271   WHERE claim_line_id = cv_claim_line_id;
2272 
2273 /*CURSOR csr_total_util_lines (cv_claim_line_id      IN NUMBER)IS
2274    SELECT COUNT(1),
2275           MAX(claim_line_util_id)
2276    FROM  ozf_claim_lines_util_all
2277    WHERE claim_line_id = cv_claim_line_id; */
2278 
2279   l_sum_amount           NUMBER :=0;
2280   l_last_record_num      NUMBER := 0;
2281   l_entered_diff_amount  NUMBER := 0;
2282   l_line_util_tot_amt    NUMBER := 0;
2283   l_last_line_util_id    NUMBER;
2284   l_line_util_count      NUMBER := 0;
2285 
2286 BEGIN
2287   --------------------- initialize -----------------------
2288   SAVEPOINT Create_Line_Util_Tbl;
2289 
2290   IF OZF_DEBUG_HIGH_ON THEN
2291      OZF_Utility_PVT.debug_message(l_full_name||': start');
2292   END IF;
2293 
2294   IF FND_API.to_boolean(p_init_msg_list) THEN
2295     FND_MSG_PUB.initialize;
2296   END IF;
2297 
2298   IF NOT FND_API.compatible_api_call(
2299               l_api_version,
2300               p_api_version,
2301               l_api_name,
2302               g_pkg_name
2303          )
2304   THEN
2305     RAISE FND_API.g_exc_unexpected_error;
2306   END IF;
2307 
2308   x_return_status := FND_API.g_ret_sts_success;
2309 
2310   --Claim Access Check: Abort process, if current user doesnt have access on claim.
2311   IF p_mode = OZF_CLAIM_UTILITY_PVT.g_manu_mode AND p_line_util_tbl.count > 0 THEN
2312     FOR j IN p_line_util_tbl.FIRST..p_line_util_tbl.LAST LOOP
2313       IF p_line_util_tbl.EXISTS(j) THEN
2314 
2315         OPEN csr_claim_id(p_line_util_tbl(j).claim_line_id);
2316         FETCH csr_claim_id INTO l_claim_id;
2317         CLOSE csr_claim_id;
2318 
2319         OZF_CLAIM_UTILITY_PVT.Check_Claim_access(
2320                P_Api_Version_Number => 1.0
2321              , P_Init_Msg_List      => FND_API.G_FALSE
2322              , p_validation_level   => FND_API.G_VALID_LEVEL_FULL
2323              , P_Commit             => FND_API.G_FALSE
2324              , P_object_id          => l_claim_id
2325              , P_object_type        => G_CLAIM_OBJECT_TYPE
2326              , P_user_id            => OZF_UTILITY_PVT.get_resource_id(NVL(FND_GLOBAL.user_id,-1))
2327              , X_Return_Status      => l_return_status
2328              , X_Msg_Count          => x_msg_count
2329              , X_Msg_Data           => x_msg_data
2330              , X_access             => l_access);
2331 
2332         IF l_access = 'N' THEN
2333           IF FND_MSG_PUB.check_msg_level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
2334             FND_MESSAGE.set_name('OZF','OZF_CLAIM_NO_ACCESS');
2335             FND_MSG_PUB.add;
2336           END IF;
2337           RAISE FND_API.G_EXC_ERROR;
2338         END IF;
2339         EXIT;
2340       END IF;
2341     END LOOP;
2342   END IF;
2343 
2344   --------------------- Create Line Util Table -----------------------
2345   IF p_line_util_tbl.FIRST IS NOT NULL THEN
2346 
2347     --Get currency and exchange rate details of claim line.
2348     OPEN csr_claim_exc(p_line_util_tbl(p_line_util_tbl.FIRST).claim_line_id);
2349     FETCH csr_claim_exc INTO  l_claim_currency_code
2350                             , l_claim_exc_type
2351                             , l_claim_exc_date
2352                             , l_claim_exc_rate;
2353     CLOSE csr_claim_exc;
2354 
2355     IF l_currency_rec.claim_currency_code IS NULL THEN
2356        l_currency_rec.claim_currency_code := l_claim_currency_code;
2357     END IF;
2358 
2359   --Get FUNCTIONAL currency from system parameters
2360   IF l_currency_rec.functional_currency_code IS NULL THEN
2361      OPEN  csr_function_currency(MO_GLOBAL.GET_CURRENT_ORG_ID());
2362      FETCH csr_function_currency INTO l_currency_rec.functional_currency_code;
2363      CLOSE csr_function_currency;
2364   END IF;
2365 
2366   --Set UNIVERSAL currency from profile.
2367   IF l_currency_rec.universal_currency_code IS NULL THEN
2368      l_currency_rec.universal_currency_code := FND_PROFILE.VALUE('OZF_TP_COMMON_CURRENCY');
2369    END IF;
2370   IF l_currency_rec.transaction_currency_code IS NULL THEN
2371      l_currency_rec.transaction_currency_code :=  l_currency_rec.claim_currency_code;
2372   END IF;
2373 
2374   --ninarasi fix for bug 13792836
2375   IF l_currency_rec.association_currency_code IS NULL THEN
2376      IF l_currency_rec.claim_currency_code = l_currency_rec.transaction_currency_code THEN
2377        l_currency_rec.association_currency_code  := l_currency_rec.transaction_currency_code;
2378      ELSE
2379        l_currency_rec.association_currency_code  := l_currency_rec.functional_currency_code;
2380      END IF;
2381   END IF;
2382     --Association process started for all earnings/adjustments of current
2383     --claim line.
2384     FOR i IN p_line_util_tbl.FIRST..p_line_util_tbl.LAST LOOP
2385       IF p_line_util_tbl.exists(i) THEN
2386         l_line_util_rec := p_line_util_tbl(i);
2387         l_line_util_rec.currency_code := l_claim_currency_code;
2388         l_line_util_rec.exchange_rate_type := l_claim_exc_type;
2389         l_line_util_rec.exchange_rate_date := l_claim_exc_date;
2390         l_line_util_rec.exchange_rate := l_claim_exc_rate;
2391         l_line_util_rec.org_id := MO_GLOBAL.GET_CURRENT_ORG_ID();
2392 
2393         l_line_util_rec.object_version_number := 1;
2394         l_line_util_rec.last_updated_by := NVL(FND_GLOBAL.user_id,-1);
2395         l_line_util_rec.created_by := NVL(FND_GLOBAL.user_id,-1);
2396         l_line_util_rec.last_update_login := NVL(FND_GLOBAL.conc_login_id,-1);
2397 
2398 
2399         Create_Line_Util(
2400                p_api_version       => 1.0
2401              , p_init_msg_list     => FND_API.g_false
2402              , p_commit            => FND_API.g_false
2403              , p_validation_level  => p_validation_level
2404              , x_return_status     => l_return_status
2405              , x_msg_count         => x_msg_count
2406              , x_msg_data          => x_msg_data
2407              , p_line_util_rec     => l_line_util_rec
2408              , p_currency_rec      => l_currency_rec
2409              , p_mode              => OZF_CLAIM_UTILITY_PVT.g_auto_mode
2410              , x_line_util_id      => l_line_util_id
2411              , px_line_util_tbl    => l_line_util_tbl --ninarasi fix for bug 13530939
2412              , px_upd_line_util_tbl => l_upd_fund_util_tbl
2413         );
2414 
2415 	IF l_upd_fund_util_tbl.COUNT = G_BULK_LIMIT OR i = p_line_util_tbl.LAST THEN --ninarasi fix for bug 13530939
2416            FORALL i IN NVL(l_upd_fund_util_tbl.FIRST,1) .. NVL(l_upd_fund_util_tbl.LAST,0)
2417               UPDATE ozf_funds_utilized_all_b
2418               SET    amount_remaining                  = amount_remaining - l_upd_fund_util_tbl(i).amount_remaining
2419                    , acctd_amount_remaining          = acctd_amount_remaining - l_upd_fund_util_tbl(i).acctd_amount_remaining
2420                    , plan_curr_amount_remaining      = plan_curr_amount_remaining - l_upd_fund_util_tbl(i).plan_curr_amount_remaining
2421                    , univ_curr_amount_remaining      = univ_curr_amount_remaining - l_upd_fund_util_tbl(i).univ_curr_amount_remaining
2422                    , fund_request_amount_remaining   = fund_request_amount_remaining - l_upd_fund_util_tbl(i).fund_request_amount_remaining
2423              WHERE utilization_id = l_upd_fund_util_tbl(i).utilization_id;
2424 
2425              l_upd_fund_util_tbl.delete;
2426         END IF;
2427 
2428 	 IF l_line_util_tbl.COUNT = G_BULK_LIMIT OR i = p_line_util_tbl.LAST THEN
2429            FORALL i IN NVL(l_line_util_tbl.FIRST, 1) .. NVL(l_line_util_tbl.LAST, 0)
2430               INSERT INTO ozf_claim_lines_util_all (
2431               claim_line_util_id,
2432               object_version_number,
2433               last_update_date,
2434               last_updated_by,
2435               creation_date,
2436               created_by,
2437               last_update_login,
2438               claim_line_id,
2439               utilization_id,
2440               amount,
2441               currency_code,
2442               exchange_rate_type,
2443               exchange_rate_date,
2444               exchange_rate,
2445               acctd_amount,
2446               util_curr_amount,
2447               plan_curr_amount,
2448               univ_curr_amount,
2449               scan_unit,
2450               activity_product_id,
2451               uom_code,
2452               quantity,
2453               org_id,
2454               fxgl_acctd_amount,
2455               utilized_acctd_amount
2456           )
2457           VALUES (
2458               l_line_util_tbl(i).claim_line_util_id,
2459               l_line_util_tbl(i).object_version_number,
2460               SYSDATE,
2461               l_line_util_tbl(i).last_updated_by,
2462               SYSDATE,
2463               l_line_util_tbl(i).created_by,
2464               l_line_util_tbl(i).last_update_login,
2465               l_line_util_tbl(i).claim_line_id,
2466               l_line_util_tbl(i).utilization_id,
2467               l_line_util_tbl(i).amount,
2468               l_line_util_tbl(i).currency_code,
2469               l_line_util_tbl(i).exchange_rate_type,
2470               l_line_util_tbl(i).exchange_rate_date,
2471               l_line_util_tbl(i).exchange_rate,
2472               l_line_util_tbl(i).acctd_amount,
2473               l_line_util_tbl(i).util_curr_amount,
2474               l_line_util_tbl(i).plan_curr_amount,
2475               l_line_util_tbl(i).univ_curr_amount,
2476               l_line_util_tbl(i).scan_unit,
2477               l_line_util_tbl(i).activity_product_id,
2478               l_line_util_tbl(i).uom_code,
2479               l_line_util_tbl(i).quantity,
2480               l_line_util_tbl(i).org_id,
2481               l_line_util_tbl(i).fxgl_acctd_amount,
2482               l_line_util_tbl(i).utilized_acctd_amount);
2483 
2484              l_line_util_tbl.delete;
2485 
2486 
2487          END IF;
2488 
2489 	--Fix for Bug 12369487
2490 	l_last_line_util_id := l_line_util_id;
2491 
2492 	IF OZF_DEBUG_LOW_ON THEN
2493         OZF_Utility_PVT.debug_message('l_last_line_util_id :'|| l_last_line_util_id );
2494 	END IF;
2495 
2496         /*OPEN csr_claim_line_util_amount(l_line_util_id);
2497         FETCH csr_claim_line_util_amount INTO l_line_util_amount;
2498         CLOSE csr_claim_line_util_amount;*/
2499 
2500 
2501 
2502         IF OZF_DEBUG_LOW_ON THEN
2503            OZF_Utility_PVT.debug_message(' l_line_util_amount'||l_line_util_amount);
2504 	END IF;
2505 
2506 
2507 	--l_sum_amount := NVL(l_sum_amount,0) + l_line_util_amount;
2508 
2509 
2510 
2511 
2512         IF l_return_status =  fnd_api.g_ret_sts_error THEN
2513           x_error_index := i;
2514           RAISE FND_API.g_exc_error;
2515         ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2516           x_error_index := i;
2517           RAISE FND_API.g_exc_unexpected_error;
2518         END IF;
2519 
2520       END IF;
2521     END LOOP;
2522      --Fix for Bug 12369487
2523    IF p_line_util_tbl.FIRST IS NOT NULL THEN
2524      OPEN csr_line_util_sum_amount(p_line_util_tbl(p_line_util_tbl.FIRST).claim_line_id);
2525         FETCH csr_line_util_sum_amount INTO l_sum_amount;
2526         CLOSE csr_line_util_sum_amount;
2527    END IF;
2528 
2529       IF OZF_DEBUG_LOW_ON THEN
2530           OZF_Utility_PVT.debug_message('l_original_total_amount'||l_original_total_amount);
2531 	  OZF_Utility_PVT.debug_message('l_sum_amount'||l_sum_amount);
2532       END IF;
2533 
2534       l_entered_diff_amount := l_original_total_amount;
2535 
2536       IF OZF_DEBUG_LOW_ON THEN
2537          OZF_Utility_PVT.debug_message('l_entered_diff_amount'||l_entered_diff_amount);
2538       END IF;
2539 
2540     IF (l_entered_diff_amount <> l_sum_amount) THEN
2541 
2542         OPEN csr_claim_line_util_amount(l_last_line_util_id);
2543         FETCH csr_claim_line_util_amount INTO l_line_util_amount;
2544         CLOSE csr_claim_line_util_amount;
2545 
2546         l_line_util_amount := l_line_util_amount + (l_entered_diff_amount - l_sum_amount);
2547 
2548         -- Round the Last Record
2549 	IF l_original_total_amount <> 0 THEN --ninarasi fix for 12.2 bug
2550 		UPDATE ozf_claim_lines_util_all
2551 		SET amount = l_line_util_amount
2552 		WHERE claim_line_util_id = l_last_line_util_id;
2553 	END IF;
2554 
2555     END IF;
2556     --End of fix for Bug 12369487
2557 
2558 
2559     -- Update Claim Line: set earnings_associated_flag to TRUE
2560     UPDATE ozf_claim_lines_all
2561       SET earnings_associated_flag = 'T'
2562       WHERE claim_line_id = l_line_util_rec.claim_line_id;
2563   END IF;
2564 
2565   ------------------------- finish -------------------------------
2566   IF FND_API.to_boolean(p_commit) THEN
2567     COMMIT;
2568   END IF;
2569 
2570   FND_MSG_PUB.count_and_get(
2571          p_encoded => FND_API.g_false,
2572          p_count   => x_msg_count,
2573          p_data    => x_msg_data
2574   );
2575 
2576   IF OZF_DEBUG_HIGH_ON THEN
2577      OZF_Utility_PVT.debug_message(l_full_name ||': end');
2578   END IF;
2579 
2580 EXCEPTION
2581   WHEN FND_API.g_exc_error THEN
2582     ROLLBACK TO Create_Line_Util_Tbl;
2583     x_return_status := FND_API.g_ret_sts_error;
2584     FND_MSG_PUB.count_and_get (
2585            p_encoded => FND_API.g_false
2586           ,p_count   => x_msg_count
2587           ,p_data    => x_msg_data
2588     );
2589 
2590   WHEN FND_API.g_exc_unexpected_error THEN
2591     ROLLBACK TO Create_Line_Util_Tbl;
2592     x_return_status := FND_API.g_ret_sts_unexp_error ;
2593     FND_MSG_PUB.count_and_get (
2594            p_encoded => FND_API.g_false
2595           ,p_count   => x_msg_count
2596           ,p_data    => x_msg_data
2597     );
2598 
2599   WHEN OTHERS THEN
2600     ROLLBACK TO Create_Line_Util_Tbl;
2601     x_return_status := FND_API.g_ret_sts_unexp_error ;
2602     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
2603       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
2604     END IF;
2605     FND_MSG_PUB.count_and_get(
2606            p_encoded => FND_API.g_false
2607           ,p_count   => x_msg_count
2608           ,p_data    => x_msg_data
2609     );
2610 
2611 END Create_Line_Util_Tbl;
2612 
2613 ---------------------------------------------------------------------
2614 -- PROCEDURE
2615 --    Create_Line_Util
2616 --
2617 -- HISTORY
2618 --    05/10/2001  mchang  Create.
2619 --    07/22/2002  yizhang add p_mode for security check
2620 --    05/08/2006  azahmed Modified for FXGL ER
2621 ---------------------------------------------------------------------
2622 PROCEDURE Create_Line_Util(
2623    p_api_version         IN  NUMBER
2624   ,p_init_msg_list       IN  VARCHAR2  := FND_API.g_false
2625   ,p_commit              IN  VARCHAR2  := FND_API.g_false
2626   ,p_validation_level    IN  NUMBER    := FND_API.g_valid_level_full
2627 
2628   ,x_return_status       OUT NOCOPY VARCHAR2
2629   ,x_msg_count           OUT NOCOPY NUMBER
2630   ,x_msg_data            OUT NOCOPY VARCHAR2
2631 
2632   ,p_line_util_rec       IN  line_util_rec_type
2633   ,p_currency_rec        IN  currency_rec_type
2634   ,p_mode                IN  VARCHAR2  := OZF_CLAIM_UTILITY_PVT.g_auto_mode
2635   ,x_line_util_id        OUT NOCOPY NUMBER
2636   ,px_line_util_tbl       IN OUT NOCOPY line_util_tbl_type --ninarasi fix for bug 13530939
2637   ,px_upd_line_util_tbl   IN OUT NOCOPY upd_fund_util_tbl_type --ninarasi fix for bug 13530939
2638 )
2639 IS
2640 l_api_version   CONSTANT NUMBER       := 1.0;
2641 l_api_name      CONSTANT VARCHAR2(30) := 'Create_Line_Util';
2642 l_full_name     CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
2643 l_return_status          VARCHAR2(1);
2644 
2645 l_line_util_rec          line_util_rec_type := p_line_util_rec;
2646 l_currency_rec           currency_rec_type := p_currency_rec;
2647 l_line_util_count        NUMBER;
2648 l_line_acctd_amount      NUMBER;
2649 
2650 l_line_util_amount       NUMBER;
2651 l_exchange_rate_type     VARCHAR2(30);
2652 l_exchange_rate_date     DATE;
2653 l_exchange_rate          NUMBER;
2654 l_convert_exchange_rate  NUMBER;
2655 l_counter                NUMBER;
2656 
2657 CURSOR csr_line_util_seq IS
2658  SELECT ozf_claim_lines_util_all_s.NEXTVAL
2659  FROM DUAL;
2660 
2661 CURSOR csr_line_util_count(cv_line_util_id IN NUMBER) IS
2662  SELECT COUNT(claim_line_util_id)
2663  FROM  ozf_claim_lines_util
2664  WHERE claim_line_util_id = cv_line_util_id;
2665 
2666 BEGIN
2667   --------------------- initialize -----------------------
2668   SAVEPOINT Create_Line_Util;
2669 
2670   IF OZF_DEBUG_HIGH_ON THEN
2671      OZF_Utility_PVT.debug_message(l_full_name||': start');
2672   END IF;
2673 
2674   IF FND_API.to_boolean(p_init_msg_list) THEN
2675     FND_MSG_PUB.initialize;
2676   END IF;
2677 
2678   IF NOT FND_API.compatible_api_call(
2679          l_api_version,
2680          p_api_version,
2681          l_api_name,
2682          g_pkg_name
2683   ) THEN
2684     RAISE FND_API.g_exc_unexpected_error;
2685   END IF;
2686 
2687   x_return_status := FND_API.g_ret_sts_success;
2688 
2689   IF p_line_util_rec.amount IS NOT NULL THEN
2690 
2691       IF l_line_util_rec.utilization_id > -1 THEN
2692         IF l_currency_rec.association_currency_code = l_currency_rec.transaction_currency_code THEN
2693           IF l_line_util_rec.amount IS NOT NULL AND l_line_util_rec.amount <> 0 THEN
2694             -- Bugfix 5528210
2695             OZF_UTILITY_PVT.Convert_Currency
2696                     ( p_from_currency => l_currency_rec.association_currency_code
2697                     , p_to_currency   => l_currency_rec.functional_currency_code
2698                     , p_conv_type     => l_line_util_rec.exchange_rate_type
2699                     , p_conv_date     => l_line_util_rec.exchange_rate_date
2700                     , p_from_amount   => l_line_util_rec.amount
2701                     , x_return_status => l_return_status
2702                     , x_to_amount     => l_line_util_rec.acctd_amount
2703                     , x_rate          => l_convert_exchange_rate
2704                     );
2705             IF l_return_status = FND_API.g_ret_sts_error THEN
2706               RAISE FND_API.g_exc_error;
2707             ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
2708               RAISE FND_API.g_exc_unexpected_error;
2709             END IF;
2710           ELSE
2711             l_line_util_rec.acctd_amount := 0;
2712           END IF;
2713         ELSE
2714           l_line_util_rec.acctd_amount := l_line_util_rec.amount;
2715         END IF;
2716 
2717 
2718       l_line_util_rec.acctd_amount := OZF_UTILITY_PVT.CurrRound(l_line_util_rec.acctd_amount, l_currency_rec.functional_currency_code);
2719       l_line_util_rec.amount       := OZF_UTILITY_PVT.CurrRound(l_line_util_rec.amount, l_currency_rec.association_currency_code);
2720 
2721       IF OZF_DEBUG_HIGH_ON THEN
2722         OZF_Utility_PVT.debug_message('l_line_util_rec.acctd_amount = '||l_line_util_rec.acctd_amount);
2723         OZF_Utility_PVT.debug_message('l_line_util_rec.utilization_id = '||l_line_util_rec.utilization_id);
2724         OZF_Utility_PVT.debug_message('l_line_util_rec.amount - Before Update_Fund_Utils() : '||l_line_util_rec.amount);
2725       END IF;
2726 
2727       Update_Fund_Utils(
2728               p_line_util_rec  => l_line_util_rec
2729             , p_asso_amount    => -NVL(l_line_util_rec.amount,0)
2730             , p_mode           => 'CALCULATE'
2731             , px_currency_rec  => l_currency_rec
2732             , x_return_status  => l_return_status
2733             , x_msg_count      => x_msg_count
2734             , x_msg_data       => x_msg_data
2735             , px_upd_line_util_tbl =>  px_upd_line_util_tbl --ninarasi fix for bug 13530939
2736             );
2737 
2738           IF l_return_status =  fnd_api.g_ret_sts_error THEN
2739                     RAISE FND_API.g_exc_error;
2740           ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
2741                  RAISE FND_API.g_exc_unexpected_error;
2742           END IF;
2743 
2744      IF OZF_DEBUG_HIGH_ON THEN
2745        OZF_Utility_PVT.debug_message('l_line_util_rec.amount - After Update_Fund_Utils() : '||l_line_util_rec.amount);
2746      END IF;
2747 
2748  ELSE
2749     IF l_line_util_rec.currency_code <> l_currency_rec.functional_currency_code THEN
2750 	OZF_UTILITY_PVT.Convert_Currency(
2751                  p_from_currency   => l_line_util_rec.currency_code --claim currency
2752                 ,p_to_currency     => l_currency_rec.functional_currency_code
2753                 ,p_conv_type       => l_line_util_rec.exchange_rate_type
2754                 ,p_conv_rate       => l_line_util_rec.exchange_rate
2755                 ,p_conv_date       => l_line_util_rec.exchange_rate_date
2756                 ,p_from_amount     => l_line_util_rec.amount
2757                 ,x_return_status   => l_return_status
2758                 ,x_to_amount       => l_line_util_rec.acctd_amount
2759                 ,x_rate            => l_convert_exchange_rate
2760                       );
2761     ELSE
2762         l_line_util_rec.acctd_amount := l_line_util_rec.amount;
2763     END IF;
2764   END IF;  --l_utilization_id > -1 check
2765 END IF; --ninarasi fix for bug currency error.
2766  --Since, amount column of OZF_CLAIM_LINES_UTIL table expect amount in CLAIM currency. Hence, need to convert
2767  --l_line_util_rec.amount into CLAIM currency, before creation of association record.
2768  IF l_line_util_rec.amount IS NOT NULL AND l_line_util_rec.amount <> 0 THEN
2769    IF l_currency_rec.association_currency_code <> l_currency_rec.claim_currency_code THEN
2770      OZF_UTILITY_PVT.Convert_Currency
2771                     ( p_from_currency => l_currency_rec.association_currency_code
2772                     , p_to_currency   => l_currency_rec.claim_currency_code
2773                     , p_conv_type     => l_line_util_rec.exchange_rate_type
2774                     --, p_conv_rate     => l_line_util_rec.exchange_rate
2775                     , p_conv_date     => l_line_util_rec.exchange_rate_date
2776                     , p_from_amount   => l_line_util_rec.amount
2777                     , x_return_status => l_return_status
2778                     , x_to_amount     => l_line_util_amount
2779                     , x_rate          => l_convert_exchange_rate
2780                     );
2781 
2782      IF l_return_status = FND_API.g_ret_sts_error THEN
2783        RAISE FND_API.g_exc_error;
2784      ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
2785        RAISE FND_API.g_exc_unexpected_error;
2786      END IF;
2787 
2788       IF OZF_DEBUG_HIGH_ON THEN
2789          OZF_Utility_PVT.debug_message('p_from_currency : '||l_currency_rec.association_currency_code);
2790          OZF_Utility_PVT.debug_message('p_to_currency   : '||l_currency_rec.claim_currency_code);
2791          OZF_Utility_PVT.debug_message('p_conv_type     : '||l_line_util_rec.exchange_rate_type);
2792          OZF_Utility_PVT.debug_message('p_conv_rate     : '||l_line_util_rec.exchange_rate);
2793          OZF_Utility_PVT.debug_message('p_conv_date     : '||l_line_util_rec.exchange_rate_date);
2794          OZF_Utility_PVT.debug_message('p_from_amount   : '||l_line_util_rec.amount);
2795          OZF_Utility_PVT.debug_message('x_to_amount     : '||l_line_util_amount);
2796      END IF;
2797 
2798      IF l_line_util_amount IS NOT NULL AND l_line_util_amount <> 0 THEN
2799        l_line_util_rec.amount := OZF_UTILITY_PVT.CurrRound(l_line_util_amount, l_currency_rec.claim_currency_code);
2800      END IF;
2801    END IF;
2802  ELSE
2803    l_line_util_rec.amount := 0;
2804  END IF;
2805  IF OZF_DEBUG_HIGH_ON THEN
2806      OZF_Utility_PVT.debug_message('Inserting association record...');
2807      OZF_Utility_PVT.debug_message('claim_line_id : '||l_line_util_rec.claim_line_id);
2808      OZF_Utility_PVT.debug_message('utilization_id : '||l_line_util_rec.utilization_id);
2809      OZF_Utility_PVT.debug_message('amount         : '||l_line_util_rec.amount);
2810      OZF_Utility_PVT.debug_message('acctd amount         : '||l_line_util_rec.acctd_amount);
2811  END IF;
2812 
2813 
2814   IF l_line_util_rec.claim_line_util_id IS NULL THEN
2815     LOOP
2816       OPEN  csr_line_util_seq;
2817       FETCH csr_line_util_seq INTO l_line_util_rec.claim_line_util_id;
2818       CLOSE csr_line_util_seq;
2819       -- Check the uniqueness of the identifier
2820       OPEN  csr_line_util_count(l_line_util_rec.claim_line_util_id);
2821       FETCH csr_line_util_count INTO l_line_util_count;
2822       CLOSE csr_line_util_count;
2823       -- Exit when the identifier uniqueness is established
2824       EXIT WHEN l_line_util_count = 0;
2825    END LOOP;
2826   END IF;
2827 
2828  l_counter := px_line_util_tbl.COUNT + 1;
2829 
2830 px_line_util_tbl(l_counter) := l_line_util_rec;
2831 
2832 --ninarasi fix for bug 13530939
2833   /*INSERT INTO ozf_claim_lines_util_all (
2834       claim_line_util_id,
2835       object_version_number,
2836       last_update_date,
2837       last_updated_by,
2838       creation_date,
2839       created_by,
2840       last_update_login,
2841       claim_line_id,
2842       utilization_id,
2843       amount,
2844       currency_code,
2845       exchange_rate_type,
2846       exchange_rate_date,
2847       exchange_rate,
2848       acctd_amount,
2849       util_curr_amount,
2850       plan_curr_amount,
2851       univ_curr_amount,
2852       scan_unit,
2853       activity_product_id,
2854       uom_code,
2855       quantity,
2856       org_id,
2857       fxgl_acctd_amount,
2858       utilized_acctd_amount
2859   )
2860   VALUES (
2861       l_line_util_rec.claim_line_util_id,
2862       l_line_util_rec.object_version_number,
2863       SYSDATE,
2864       l_line_util_rec.last_updated_by,
2865       SYSDATE,
2866       l_line_util_rec.created_by,
2867       l_line_util_rec.last_update_login,
2868       l_line_util_rec.claim_line_id,
2869       l_line_util_rec.utilization_id,
2870       l_line_util_rec.amount,
2871       l_line_util_rec.currency_code,
2872       l_line_util_rec.exchange_rate_type,
2873       l_line_util_rec.exchange_rate_date,
2874       l_line_util_rec.exchange_rate,
2875       l_line_util_rec.acctd_amount,
2876       l_line_util_rec.util_curr_amount,
2877       l_line_util_rec.plan_curr_amount,
2878       l_line_util_rec.univ_curr_amount,
2879       l_line_util_rec.scan_unit,
2880       l_line_util_rec.activity_product_id,
2881       l_line_util_rec.uom_code,
2882       l_line_util_rec.quantity,
2883       l_line_util_rec.org_id,
2884       l_line_util_rec.fxgl_acctd_amount,
2885       l_line_util_rec.utilized_acctd_amount
2886 
2887   );*/
2888 
2889   ------------------------- finish -------------------------------
2890   x_line_util_id := l_line_util_rec.claim_line_util_id;
2891 
2892   -- Check for commit
2893   IF FND_API.to_boolean(p_commit) THEN
2894     COMMIT;
2895   END IF;
2896 
2897   FND_MSG_PUB.count_and_get(
2898          p_encoded => FND_API.g_false,
2899          p_count   => x_msg_count,
2900          p_data    => x_msg_data
2901   );
2902 
2903   IF OZF_DEBUG_HIGH_ON THEN
2904      OZF_Utility_PVT.debug_message(l_full_name ||': end');
2905   END IF;
2906 
2907 EXCEPTION
2908   WHEN FND_API.g_exc_error THEN
2909     ROLLBACK TO Create_Line_Util;
2910     x_return_status := FND_API.g_ret_sts_error;
2911     FND_MSG_PUB.count_and_get (
2912            p_encoded => FND_API.g_false
2913           ,p_count   => x_msg_count
2914           ,p_data    => x_msg_data
2915     );
2916 
2917   WHEN FND_API.g_exc_unexpected_error THEN
2918     ROLLBACK TO Create_Line_Util;
2919     x_return_status := FND_API.g_ret_sts_unexp_error ;
2920     FND_MSG_PUB.count_and_get (
2921            p_encoded => FND_API.g_false
2922           ,p_count   => x_msg_count
2923           ,p_data    => x_msg_data
2924     );
2925 
2926   WHEN OTHERS THEN
2927     ROLLBACK TO Create_Line_Util;
2928     x_return_status := FND_API.g_ret_sts_unexp_error ;
2929     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
2930       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
2931     END IF;
2932     FND_MSG_PUB.count_and_get(
2933            p_encoded => FND_API.g_false
2934           ,p_count   => x_msg_count
2935           ,p_data    => x_msg_data
2936     );
2937 
2938 END Create_Line_Util;
2939 
2940 
2941 ---------------------------------------------------------------------
2942 -- PROCEDURE
2943 --    Update_Line_Util_Tbl
2944 --
2945 -- HISTORY
2946 --    05/12/2001  mchang  Create.
2947 --    07/22/2002  yizhang add p_mode for security check
2948 --   7-Aug-06     azahmed Modified for FXGL ER
2949 ---------------------------------------------------------------------
2950 PROCEDURE Update_Line_Util_Tbl(
2951     p_api_version            IN    NUMBER
2952    ,p_init_msg_list          IN    VARCHAR2 := FND_API.g_false
2953    ,p_commit                 IN    VARCHAR2 := FND_API.g_false
2954    ,p_validation_level       IN    NUMBER   := FND_API.g_valid_level_full
2955 
2956    ,x_return_status          OUT NOCOPY   VARCHAR2
2957    ,x_msg_data               OUT NOCOPY   VARCHAR2
2958    ,x_msg_count              OUT NOCOPY   NUMBER
2959 
2960    ,p_line_util_tbl          IN    line_util_tbl_type
2961    ,p_mode                   IN    VARCHAr2 := OZF_CLAIM_UTILITY_PVT.g_auto_mode
2962 
2963    ,x_error_index            OUT NOCOPY   NUMBER
2964 ) IS
2965 l_api_version       CONSTANT NUMBER       := 1.0;
2966 l_api_name          CONSTANT VARCHAR2(30) := 'Update_Line_Util_Tbl';
2967 l_full_name         CONSTANT VARCHAR2(60) := g_pkg_name || '.' || l_api_name;
2968 l_return_status              VARCHAR2(1);
2969 
2970 l_line_util_rec              line_util_rec_type;
2971 l_claim_id                   NUMBER;
2972 l_object_version             NUMBER;
2973 l_access                     VARCHAR2(1) := 'N';
2974 i                            PLS_INTEGER;
2975 l_upd_fund_util_tbl          upd_fund_util_tbl_type;
2976 
2977 
2978 -- Cursor to get claim_id
2979 CURSOR csr_claim_id(cv_claim_line_id IN NUMBER) IS
2980   SELECT claim_id
2981   FROM ozf_claim_lines
2982   WHERE claim_line_id = cv_claim_line_id;
2983 
2984 BEGIN
2985   --------------------- initialize -----------------------
2986   SAVEPOINT Update_Line_Util_Tbl;
2987 
2988   IF OZF_DEBUG_HIGH_ON THEN
2989      OZF_Utility_PVT.debug_message(l_full_name||': start');
2990   END IF;
2991 
2992   IF FND_API.to_boolean(p_init_msg_list) THEN
2993     FND_MSG_PUB.initialize;
2994   END IF;
2995 
2996   IF NOT FND_API.compatible_api_call(
2997             l_api_version,
2998             p_api_version,
2999             l_api_name,
3000             g_pkg_name
3001         ) THEN
3002     RAISE FND_API.g_exc_unexpected_error;
3003   END IF;
3004 
3005   x_return_status := FND_API.g_ret_sts_success;
3006 
3007   ---------------------- check claim access ------------------------
3008   IF p_mode = OZF_CLAIM_UTILITY_PVT.g_manu_mode AND p_line_util_tbl.count > 0 THEN
3009     FOR j IN p_line_util_tbl.FIRST..p_line_util_tbl.LAST LOOP
3010       IF p_line_util_tbl.EXISTS(j) THEN
3011 
3012         OPEN csr_claim_id(p_line_util_tbl(j).claim_line_id);
3013         FETCH csr_claim_id INTO l_claim_id;
3014         CLOSE csr_claim_id;
3015 
3016         OZF_CLAIM_UTILITY_PVT.Check_Claim_access(
3017                P_Api_Version_Number => 1.0
3018              , P_Init_Msg_List      => FND_API.G_FALSE
3019              , p_validation_level   => FND_API.G_VALID_LEVEL_FULL
3020              , P_Commit             => FND_API.G_FALSE
3021              , P_object_id          => l_claim_id
3022              , P_object_type        => G_CLAIM_OBJECT_TYPE
3023              , P_user_id            => OZF_UTILITY_PVT.get_resource_id(NVL(FND_GLOBAL.user_id,-1))
3024              , X_Return_Status      => l_return_status
3025              , X_Msg_Count          => x_msg_count
3026              , X_Msg_Data           => x_msg_data
3027              , X_access             => l_access);
3028 
3029         IF l_access = 'N' THEN
3030           IF FND_MSG_PUB.check_msg_level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
3031             FND_MESSAGE.set_name('OZF','OZF_CLAIM_NO_ACCESS');
3032             FND_MSG_PUB.add;
3033           END IF;
3034           RAISE FND_API.G_EXC_ERROR;
3035         END IF;
3036         EXIT;
3037       END IF;
3038     END LOOP;
3039   END IF;
3040 
3041   --------------------- Update Claim Line Table -----------------------
3042 
3043   i := p_line_util_tbl.FIRST;
3044   IF i IS NOT NULL THEN
3045     LOOP
3046       l_line_util_rec := p_line_util_tbl(i);
3047       IF l_line_util_rec.claim_line_util_id IS NOT NULL THEN
3048         l_line_util_rec.amount := OZF_UTILITY_PVT.CurrRound(l_line_util_rec.amount, l_line_util_rec.currency_code);
3049         l_line_util_rec.update_from_tbl_flag := FND_API.g_true;
3050         Update_Line_Util(
3051                  p_api_version       => l_api_version
3052                , p_init_msg_list     => FND_API.g_false
3053                , p_commit            => FND_API.g_false
3054                , p_validation_level  => p_validation_level
3055                , x_return_status     => l_return_status
3056                , x_msg_count         => x_msg_count
3057                , x_msg_data          => x_msg_data
3058                , p_line_util_rec     => l_line_util_rec
3059                , p_mode              => OZF_CLAIM_UTILITY_PVT.g_auto_mode
3060                , x_object_version    => l_object_version
3061                , px_upd_line_util_tbl => l_upd_fund_util_tbl
3062         );
3063         IF l_return_status =  fnd_api.g_ret_sts_error THEN
3064           x_error_index := i;
3065           RAISE FND_API.g_exc_error;
3066         ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3067           x_error_index := i;
3068           RAISE FND_API.g_exc_unexpected_error;
3069         END IF;
3070         IF l_upd_fund_util_tbl.COUNT = G_BULK_LIMIT OR i = p_line_util_tbl.LAST THEN --ninarasi fix for bug 13530939
3071            FORALL i IN NVL(l_upd_fund_util_tbl.FIRST,1) .. NVL(l_upd_fund_util_tbl.LAST,0)
3072               UPDATE ozf_funds_utilized_all_b
3073               SET    amount_remaining                  = amount_remaining - l_upd_fund_util_tbl(i).amount_remaining
3074                    , acctd_amount_remaining          = acctd_amount_remaining - l_upd_fund_util_tbl(i).acctd_amount_remaining
3075                    , plan_curr_amount_remaining      = plan_curr_amount_remaining - l_upd_fund_util_tbl(i).plan_curr_amount_remaining
3076                    , univ_curr_amount_remaining      = univ_curr_amount_remaining - l_upd_fund_util_tbl(i).univ_curr_amount_remaining
3077                    , fund_request_amount_remaining   = fund_request_amount_remaining - l_upd_fund_util_tbl(i).fund_request_amount_remaining
3078              WHERE utilization_id = l_upd_fund_util_tbl(i).utilization_id;
3079 
3080              l_upd_fund_util_tbl.delete;
3081         END IF;
3082       END IF;
3083       EXIT WHEN i = p_line_util_tbl.LAST;
3084       i := p_line_util_tbl.NEXT(i);
3085     END LOOP;
3086   END IF;
3087 
3088   ------------------------- finish -------------------------------
3089   IF FND_API.to_boolean(p_commit) THEN
3090     COMMIT;
3091   END IF;
3092 
3093   FND_MSG_PUB.count_and_get(
3094          p_encoded => FND_API.g_false,
3095          p_count   => x_msg_count,
3096          p_data    => x_msg_data
3097   );
3098 
3099   IF OZF_DEBUG_HIGH_ON THEN
3100      OZF_Utility_PVT.debug_message(l_full_name ||': end');
3101   END IF;
3102 
3103 EXCEPTION
3104   WHEN FND_API.g_exc_error THEN
3105     ROLLBACK TO Update_Line_Util_Tbl;
3106     x_return_status := FND_API.g_ret_sts_error;
3107     FND_MSG_PUB.count_and_get (
3108          p_encoded => FND_API.g_false
3109         ,p_count   => x_msg_count
3110         ,p_data    => x_msg_data
3111     );
3112 
3113   WHEN FND_API.g_exc_unexpected_error THEN
3114     ROLLBACK TO Update_Line_Util_Tbl;
3115     x_return_status := FND_API.g_ret_sts_unexp_error ;
3116     FND_MSG_PUB.count_and_get (
3117          p_encoded => FND_API.g_false
3118         ,p_count   => x_msg_count
3119         ,p_data    => x_msg_data
3120     );
3121 
3122   WHEN OTHERS THEN
3123     ROLLBACK TO Update_Line_Util_Tbl;
3124     x_return_status := FND_API.g_ret_sts_unexp_error ;
3125     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
3126       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
3127     END IF;
3128     FND_MSG_PUB.count_and_get(
3129          p_encoded => FND_API.g_false
3130         ,p_count   => x_msg_count
3131         ,p_data    => x_msg_data
3132     );
3133 
3134 END Update_Line_Util_Tbl;
3135 
3136 
3137 ---------------------------------------------------------------------
3138 -- PROCEDURE
3139 --    Update_Line_Util
3140 --
3141 -- HISTORY
3142 --    05/10/2001  mchang  Create.
3143 --    07/22/2002  yizhang add p_mode for security check
3144 --    15-Mar-06   azahmed  Bugfix 5101106 added condition to update fu only when util_id > -1
3145 --    08-Aug-06   azahmed  Modifed for FXGL ER
3146 ----------------------------------------------------------------------
3147 PROCEDURE Update_Line_Util(
3148    p_api_version         IN  NUMBER
3149   ,p_init_msg_list       IN  VARCHAR2  := FND_API.g_false
3150   ,p_commit              IN  VARCHAR2  := FND_API.g_false
3151   ,p_validation_level    IN  NUMBER    := FND_API.g_valid_level_full
3152 
3153   ,x_return_status       OUT NOCOPY VARCHAR2
3154   ,x_msg_count           OUT NOCOPY NUMBER
3155   ,x_msg_data            OUT NOCOPY VARCHAR2
3156 
3157   ,p_line_util_rec       IN  line_util_rec_type
3158   ,p_mode                IN  VARCHAR2  := OZF_CLAIM_UTILITY_PVT.g_auto_mode
3159   ,x_object_version      OUT NOCOPY NUMBER
3160   ,px_upd_line_util_tbl   IN OUT NOCOPY upd_fund_util_tbl_type --ninarasi fix for bug 13530939
3161 )
3162 IS
3163 l_api_version       CONSTANT NUMBER       := 1.0;
3164 l_api_name          CONSTANT VARCHAR2(30) := 'Update_Line_Util';
3165 l_full_name         CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
3166 l_return_status              VARCHAR2(1);
3167 
3168 l_line_util_rec              line_util_rec_type;
3169 l_claim_id                   NUMBER;
3170 l_lu_old_amt                 NUMBER;
3171 l_lu_old_scan_unit           NUMBER;
3172 l_utilization_rec            OZF_Fund_Utilized_PVT.utilization_rec_type;
3173 l_currency_rec               currency_rec_type ;
3174 
3175 l_access                     VARCHAR2(1) := 'N';
3176 l_total_exist_util_amt       NUMBER;
3177 l_line_acctd_amount          NUMBER;
3178 l_exchange_rate              NUMBER;
3179 l_claim_date                 DATE;
3180 l_utiz_currency              VARCHAR2(15);
3181 l_old_utiz_amount            NUMBER;
3182 l_update_fund_amount         NUMBER;
3183 l_update_acctd_fund_amount   NUMBER;
3184 l_rate                       NUMBER;
3185 
3186 -- fix for bug 5042046
3187 CURSOR csr_function_currency IS
3188   SELECT gs.currency_code
3189   FROM   gl_sets_of_books gs
3190   ,      ozf_sys_parameters org
3191   WHERE  org.set_of_books_id = gs.set_of_books_id
3192   AND    org.org_id = MO_GLOBAL.GET_CURRENT_ORG_ID();
3193 
3194 CURSOR csr_claim_id(cv_claim_line_id IN NUMBER) IS
3195   SELECT claim_id, acctd_amount
3196   FROM ozf_claim_lines
3197   WHERE claim_line_id = cv_claim_line_id;
3198 
3199 --Cursor changed for Claims-Multicurrency ER
3200 CURSOR csr_lu_old_amt(cv_line_util_id IN NUMBER) IS
3201   SELECT lu.amount
3202        , lu.scan_unit
3203        , lu.util_curr_amount
3204        , lu.currency_code
3205        , fu.plan_currency_code
3206   FROM ozf_claim_lines_util lu
3207      , ozf_funds_utilized_all_b fu
3208   WHERE lu.claim_line_util_id = cv_line_util_id
3209     AND fu.utilization_id = lu.utilization_id;
3210 
3211 CURSOR csr_total_exist_util_amt(cv_claim_line_id IN NUMBER) IS
3212   SELECT nvl(SUM(acctd_amount),0)
3213   FROM ozf_claim_lines_util
3214   WHERE claim_line_id = cv_claim_line_id;
3215 
3216   CURSOR csr_claim_date(cv_claim_line_id IN NUMBER) IS
3217   SELECT cla.creation_date
3218   FROM ozf_claims cla, ozf_claim_lines cl
3219   WHERE cla.claim_id = cl.claim_id
3220   AND cl.claim_line_id  = cv_claim_line_id;
3221 
3222 BEGIN
3223   -------------------- initialize -------------------------
3224   SAVEPOINT Update_Line_Util;
3225 
3226   IF OZF_DEBUG_HIGH_ON THEN
3227      OZF_Utility_PVT.debug_message(l_full_name||': start');
3228   END IF;
3229 
3230   IF FND_API.to_boolean(p_init_msg_list) THEN
3231     FND_MSG_PUB.initialize;
3232   END IF;
3233 
3234   IF NOT FND_API.compatible_api_call(
3235          l_api_version,
3236          p_api_version,
3237          l_api_name,
3238          g_pkg_name
3239   ) THEN
3240     RAISE FND_API.g_exc_unexpected_error;
3241   END IF;
3242 
3243   x_return_status := FND_API.g_ret_sts_success;
3244 
3245   ----------------------- validate ----------------------
3246   IF OZF_DEBUG_HIGH_ON THEN
3247      OZF_Utility_PVT.debug_message(l_full_name ||': validate');
3248   END IF;
3249 
3250   -- replace g_miss_char/num/date with current column values
3251   Complete_Line_Util_Rec(
3252          p_line_util_rec      => p_line_util_rec
3253         ,x_complete_rec       => l_line_util_rec
3254   );
3255 
3256   l_line_util_rec.object_version_number := l_line_util_rec.object_version_number + 1;
3257   l_line_util_rec.last_updated_by := NVL(FND_GLOBAL.user_id,-1);
3258   l_line_util_rec.last_update_login := NVL(FND_GLOBAL.conc_login_id,-1);
3259 
3260   -- get claim_id
3261   OPEN csr_claim_id(p_line_util_rec.claim_line_id);
3262   FETCH csr_claim_id INTO l_claim_id, l_line_acctd_amount;
3263   CLOSE csr_claim_id;
3264 
3265   OPEN csr_claim_date(p_line_util_rec.claim_line_id);
3266   FETCH csr_claim_date INTO l_claim_date;
3267   CLOSE csr_claim_date;
3268 
3269 
3270   ---------------------- check claim access ------------------------
3271   IF p_mode = OZF_CLAIM_UTILITY_PVT.g_manu_mode THEN
3272     OZF_CLAIM_UTILITY_PVT.Check_Claim_access(
3273            P_Api_Version_Number => 1.0
3274          , P_Init_Msg_List      => FND_API.G_FALSE
3275          , p_validation_level   => FND_API.G_VALID_LEVEL_FULL
3276          , P_Commit             => FND_API.G_FALSE
3277          , P_object_id          => l_claim_id
3278          , P_object_type        => G_CLAIM_OBJECT_TYPE
3279          , P_user_id            => OZF_UTILITY_PVT.get_resource_id(NVL(FND_GLOBAL.user_id,-1))
3280          , X_Return_Status      => l_return_status
3281          , X_Msg_Count          => x_msg_count
3282          , X_Msg_Data           => x_msg_data
3283          , X_access             => l_access);
3284 
3285     IF l_access = 'N' THEN
3286       IF FND_MSG_PUB.check_msg_level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
3287         FND_MESSAGE.set_name('OZF','OZF_CLAIM_NO_ACCESS');
3288           FND_MSG_PUB.add;
3289       END IF;
3290       RAISE FND_API.G_EXC_ERROR;
3291     END IF;
3292   END IF;
3293 
3294   --IF p_line_util_rec.update_from_tbl_flag = FND_API.g_false THEN
3295     -- get functional_currency
3296     OPEN csr_function_currency;
3297     FETCH csr_function_currency INTO l_currency_rec.functional_currency_code;
3298     CLOSE csr_function_currency;
3299   --END IF;
3300 
3301   IF (p_line_util_rec.amount is not null
3302     AND p_line_util_rec.amount <> FND_API.g_miss_num) THEN
3303     OPEN  csr_lu_old_amt(p_line_util_rec.claim_line_util_id);
3304     FETCH csr_lu_old_amt INTO l_lu_old_amt, l_lu_old_scan_unit, l_old_utiz_amount ,l_currency_rec.claim_currency_code,l_currency_rec.transaction_currency_code;
3305     CLOSE csr_lu_old_amt;
3306 
3307     IF l_line_util_rec.amount <> l_lu_old_amt THEN
3308       ------------------ convert currency ----------------------
3309       -- mOdified for FXGL Enhancement
3310       -- Convert amount --> acctd_amount(functional currency)
3311       IF l_currency_rec.functional_currency_code = l_line_util_rec.currency_code THEN
3312         l_line_util_rec.acctd_amount := l_line_util_rec.amount  ;
3313       ELSE
3314         OZF_UTILITY_PVT.Convert_Currency(
3315              p_from_currency   => l_line_util_rec.currency_code
3316             ,p_to_currency     => l_currency_rec.functional_currency_code
3317             ,p_conv_type       => l_line_util_rec.exchange_rate_type
3318             ,p_conv_rate       => l_line_util_rec.exchange_rate
3319             ,p_conv_date       => l_line_util_rec.exchange_rate_date
3320             ,p_from_amount     => l_line_util_rec.amount
3321             ,x_return_status   => l_return_status
3322             ,x_to_amount       => l_line_util_rec.acctd_amount
3323             ,x_rate            => l_exchange_rate
3324         );
3325         IF l_return_status = FND_API.g_ret_sts_error THEN
3326           RAISE FND_API.g_exc_error;
3327         ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
3328           RAISE FND_API.g_exc_unexpected_error;
3329         END IF;
3330         IF l_line_util_rec.acctd_amount IS NOT NULL THEN
3331           l_line_util_rec.acctd_amount := OZF_UTILITY_PVT.CurrRound(l_line_util_rec.acctd_amount, l_currency_rec.functional_currency_code);
3332         END IF;
3333 
3334       END IF;
3335 
3336 
3337       --------------------- update utilization ----------------------
3338       IF OZF_DEBUG_HIGH_ON THEN
3339         OZF_Utility_PVT.debug_message(l_full_name ||': update funds utilized');
3340       END IF;
3341 
3342       l_update_fund_amount := l_lu_old_amt - l_line_util_rec.amount;
3343 
3344       IF l_currency_rec.claim_currency_code = l_currency_rec.transaction_currency_code THEN
3345         l_currency_rec.association_currency_code := l_currency_rec.transaction_currency_code;
3346       ELSE
3347         l_currency_rec.association_currency_code := l_currency_rec.functional_currency_code;
3348         IF l_update_fund_amount IS NOT NULL and l_update_fund_amount <> 0 THEN
3349           OZF_UTILITY_PVT.Convert_Currency
3350                      ( p_from_currency   => l_currency_rec.claim_currency_code
3351                      , p_to_currency     => l_currency_rec.functional_currency_code
3352                      , p_conv_date       => l_line_util_rec.exchange_rate_date
3353                      , p_conv_type       => l_line_util_rec.exchange_rate_type
3354                      , p_from_amount     => l_update_fund_amount
3355                      , x_return_status   => l_return_status
3356                      , x_to_amount       => l_update_acctd_fund_amount
3357                      , x_rate            => l_rate
3358                      );
3359 
3360           IF l_return_status = FND_API.g_ret_sts_error THEN
3361             RAISE FND_API.g_exc_error;
3362           ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
3363             RAISE FND_API.g_exc_unexpected_error;
3364           END IF;
3365           l_update_fund_amount := l_update_acctd_fund_amount;
3366         END IF;
3367       END IF;
3368 
3369       IF OZF_DEBUG_HIGH_ON THEN
3370         OZF_Utility_PVT.debug_message('l_line_util_rec.acctd_amount1 = '||l_line_util_rec.acctd_amount);
3371         OZF_Utility_PVT.debug_message('l_line_util_rec.amount1 - Before Update_Fund_Utils() : '||l_line_util_rec.amount);
3372         OZF_Utility_PVT.debug_message('l_update_fund_amount : '|| l_update_fund_amount);
3373       END IF;
3374       Update_Fund_Utils(
3375                 p_line_util_rec  => l_line_util_rec
3376               , p_asso_amount    => NVL(l_update_fund_amount,0)
3377               , p_mode           => 'CALCULATE'
3378               , px_currency_rec  => l_currency_rec
3379               , x_return_status  => l_return_status
3380               , x_msg_count      => x_msg_count
3381               , x_msg_data       => x_msg_data
3382               , px_upd_line_util_tbl =>  px_upd_line_util_tbl --ninarasi fix for bug 13530939
3383               );
3384            IF l_return_status =  fnd_api.g_ret_sts_error THEN
3385                     RAISE FND_API.g_exc_error;
3386            ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3387                     RAISE FND_API.g_exc_unexpected_error;
3388            END IF;
3389 
3390     END IF;
3391   END IF;
3392 
3393 
3394   -------------------------- update -------------------------
3395   IF OZF_DEBUG_HIGH_ON THEN
3396      OZF_Utility_PVT.debug_message(l_full_name ||': update lines utilized');
3397   END IF;
3398 
3399   UPDATE ozf_claim_lines_util_all SET
3400       object_version_number  = l_line_util_rec.object_version_number,
3401       last_update_date       = SYSDATE,
3402       last_updated_by        = l_line_util_rec.last_updated_by,
3403       last_update_login      = l_line_util_rec.last_update_login,
3404       claim_line_id          = l_line_util_rec.claim_line_id,
3405       utilization_id         = l_line_util_rec.utilization_id,
3406       amount                 = l_line_util_rec.amount,
3407       currency_code          = l_line_util_rec.currency_code,
3408       exchange_rate_type     = l_line_util_rec.exchange_rate_type,
3409       exchange_rate_date     = l_line_util_rec.exchange_rate_date,
3410       exchange_rate          = l_line_util_rec.exchange_rate,
3411       acctd_amount           = l_line_util_rec.acctd_amount,
3412       util_curr_amount       = l_line_util_rec.util_curr_amount,
3413       plan_curr_amount       = l_line_util_rec.plan_curr_amount,
3414       univ_curr_amount       = l_line_util_rec.univ_curr_amount,
3415       uom_code               = l_line_util_rec.uom_code,
3416       quantity               = l_line_util_rec.quantity,
3417       scan_unit              = l_line_util_rec.scan_unit,
3418       fxgl_acctd_amount      = l_line_util_rec.fxgl_acctd_amount,
3419       utilized_acctd_amount  = l_line_util_rec.utilized_acctd_amount
3420   WHERE claim_line_util_id = p_line_util_rec.claim_line_util_id;
3421   --AND   object_version_number = p_line_util_rec.object_version_number;
3422 
3423   IF (SQL%NOTFOUND) THEN
3424     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
3425       FND_MESSAGE.set_name('OZF', 'OZF_API_RECORD_NOT_FOUND');
3426       FND_MSG_PUB.add;
3427     END IF;
3428     RAISE FND_API.g_exc_error;
3429   END IF;
3430 
3431   -------------------- finish --------------------------
3432   x_object_version := l_line_util_rec.object_version_number;
3433 
3434   -- Check for commit
3435   IF FND_API.to_boolean(p_commit) THEN
3436     COMMIT;
3437   END IF;
3438 
3439   FND_MSG_PUB.count_and_get(
3440          p_encoded => FND_API.g_false,
3441          p_count   => x_msg_count,
3442          p_data    => x_msg_data
3443   );
3444 
3445   IF OZF_DEBUG_HIGH_ON THEN
3446      OZF_Utility_PVT.debug_message(l_full_name ||': end');
3447   END IF;
3448 
3449 EXCEPTION
3450   WHEN FND_API.g_exc_error THEN
3451     ROLLBACK TO Update_Line_Util;
3452     x_return_status := FND_API.g_ret_sts_error;
3453     FND_MSG_PUB.count_and_get(
3454             p_encoded => FND_API.g_false,
3455             p_count   => x_msg_count,
3456             p_data    => x_msg_data
3457     );
3458 
3459   WHEN FND_API.g_exc_unexpected_error THEN
3460     ROLLBACK TO Update_Line_Util;
3461     x_return_status := FND_API.g_ret_sts_unexp_error ;
3462     FND_MSG_PUB.count_and_get(
3463             p_encoded => FND_API.g_false,
3464             p_count   => x_msg_count,
3465             p_data    => x_msg_data
3466     );
3467 
3468   WHEN OTHERS THEN
3469     ROLLBACK TO Update_Line_Util;
3470     x_return_status := FND_API.g_ret_sts_unexp_error ;
3471     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
3472       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
3473     END IF;
3474     FND_MSG_PUB.count_and_get(
3475             p_encoded => FND_API.g_false,
3476             p_count   => x_msg_count,
3477             p_data    => x_msg_data
3478     );
3479 
3480 END Update_Line_Util;
3481 
3482 
3483 ---------------------------------------------------------------------
3484 -- PROCEDURE
3485 --    Delete_All_Line_Utils
3486 --
3487 -- HISTORY
3488 --    04-Jul-2005  Sahana  Created for Bug4348163
3489 --    08-Aug-06    azahmed Modified for FXGL ER
3490 ---------------------------------------------------------------------
3491 PROCEDURE Delete_All_Line_Util(
3492    p_api_version         IN  NUMBER
3493   ,p_init_msg_list       IN  VARCHAR2  := FND_API.g_false
3494   ,p_commit              IN  VARCHAR2  := FND_API.g_false
3495   ,p_validation_level    IN  NUMBER    := FND_API.g_valid_level_full
3496 
3497   ,x_return_status       OUT NOCOPY VARCHAR2
3498   ,x_msg_count           OUT NOCOPY NUMBER
3499   ,x_msg_data            OUT NOCOPY VARCHAR2
3500 
3501   ,p_funds_util_flt      IN  funds_util_flt_type
3502   ,p_currency_rec        IN  currency_rec_type
3503   ,p_mode                IN  VARCHAR2  := OZF_CLAIM_UTILITY_PVT.g_auto_mode
3504 )
3505 IS
3506 l_api_version   CONSTANT NUMBER       := 1.0;
3507 l_api_name      CONSTANT VARCHAR2(30) := 'Delete_All_Line_Util';
3508 l_full_name     CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
3509 l_return_status          VARCHAR2(1);
3510 l_upd_fund_util_tbl          upd_fund_util_tbl_type;
3511 
3512 TYPE FundsUtilCsrTyp IS REF CURSOR;
3513 l_funds_util_csr         NUMBER; --FundsUtilCsrTyp;
3514 l_funds_util_sql         VARCHAR2(3000);
3515 l_line_util_tbl          line_util_tbl_type;
3516 l_lu_line_util_id        NUMBER;
3517 l_lu_utilization_id      NUMBER;
3518 l_lu_amt           NUMBER;
3519 l_lu_scan_unit           NUMBER;
3520 l_counter                PLS_INTEGER := 1;
3521 l_object_version_number  NUMBER;
3522 l_ignore              NUMBER;
3523 l_utiz_amount       NUMBER;
3524 l_lu_currency_code       VARCHAR2(15);
3525 l_currency_rec       currency_rec_type := p_currency_rec;
3526 
3527 CURSOR csr_util_obj_ver(cv_line_util_id IN NUMBER) IS
3528   SELECT object_version_number
3529   FROM ozf_claim_lines_util_all
3530   WHERE claim_line_util_id = cv_line_util_id;
3531 
3532 BEGIN
3533   --------------------- initialize -----------------------
3534   SAVEPOINT Delete_All_Line_Util;
3535 
3536   IF OZF_DEBUG_HIGH_ON THEN
3537      OZF_Utility_PVT.debug_message(l_full_name||': start');
3538   END IF;
3539 
3540   IF FND_API.to_boolean(p_init_msg_list) THEN
3541     FND_MSG_PUB.initialize;
3542   END IF;
3543 
3544   IF NOT FND_API.compatible_api_call(
3545          l_api_version,
3546          p_api_version,
3547          l_api_name,
3548          g_pkg_name
3549   ) THEN
3550     RAISE FND_API.g_exc_unexpected_error;
3551   END IF;
3552 
3553   x_return_status := FND_API.g_ret_sts_success;
3554 
3555 
3556   --------------------- start -----------------------
3557   Get_Utiz_Sql_Stmt(
3558       p_api_version         => l_api_version
3559      ,p_init_msg_list       => FND_API.g_false
3560      ,p_commit              => FND_API.g_false
3561      ,p_validation_level    => FND_API.g_valid_level_full
3562      ,x_return_status       => l_return_status
3563      ,x_msg_count           => x_msg_count
3564      ,x_msg_data            => x_msg_data
3565      ,p_summary_view        => 'DEL_GRP_LINE_UTIL'
3566      ,p_funds_util_flt      => p_funds_util_flt
3567      ,px_currency_rec        => l_currency_rec
3568      ,p_cust_account_id     => p_funds_util_flt.cust_account_id
3569      ,x_utiz_sql_stmt       => l_funds_util_sql
3570   );
3571   IF l_return_status = FND_API.g_ret_sts_error THEN
3572      RAISE FND_API.g_exc_error;
3573   ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
3574      RAISE FND_API.g_exc_unexpected_error;
3575   END IF;
3576 
3577     -- use FND_DSQL package for dynamic sql and bind variables
3578   l_funds_util_csr := DBMS_SQL.open_cursor;
3579   FND_DSQL.set_cursor(l_funds_util_csr);
3580   DBMS_SQL.parse(l_funds_util_csr, l_funds_util_sql, DBMS_SQL.native);
3581   DBMS_SQL.define_column(l_funds_util_csr, 1, l_lu_line_util_id);
3582   DBMS_SQL.define_column(l_funds_util_csr, 2, l_lu_utilization_id);
3583   DBMS_SQL.define_column(l_funds_util_csr, 3, l_lu_amt);
3584   DBMS_SQL.define_column(l_funds_util_csr, 4, l_lu_scan_unit);
3585   DBMS_SQL.define_column(l_funds_util_csr, 5, l_lu_currency_code, 15);
3586 --  DBMS_SQL.define_column(l_funds_util_csr, 5, l_utiz_amount);
3587   FND_DSQL.do_binds;
3588 
3589   l_ignore := DBMS_SQL.execute(l_funds_util_csr);
3590   LOOP
3591    IF DBMS_SQL.fetch_rows(l_funds_util_csr) > 0 THEN
3592     DBMS_SQL.column_value(l_funds_util_csr, 1, l_lu_line_util_id);
3593     DBMS_SQL.column_value(l_funds_util_csr, 2, l_lu_utilization_id);
3594     DBMS_SQL.column_value(l_funds_util_csr, 3, l_lu_amt);
3595     DBMS_SQL.column_value(l_funds_util_csr, 4, l_lu_scan_unit);
3596     DBMS_SQL.column_value(l_funds_util_csr, 5, l_lu_currency_code);
3597 --    DBMS_SQL.define_column(l_funds_util_csr, 5, l_utiz_amount);
3598 
3599     OPEN  csr_util_obj_ver(l_lu_line_util_id);
3600     FETCH csr_util_obj_ver INTO l_object_version_number;
3601     CLOSE csr_util_obj_ver;
3602 
3603     Delete_Line_Util(
3604          p_api_version            => l_api_version
3605         ,p_init_msg_list          => FND_API.g_false
3606         ,p_commit                 => FND_API.g_false
3607         ,x_return_status          => l_return_status
3608         ,x_msg_data               => x_msg_data
3609         ,x_msg_count              => x_msg_count
3610         ,p_line_util_id           => l_lu_line_util_id
3611         ,p_object_version         => l_object_version_number
3612         ,p_mode                   => OZF_CLAIM_UTILITY_PVT.g_auto_mode
3613         ,px_upd_line_util_tbl     => l_upd_fund_util_tbl --ninarasi fix for bug 13530939
3614     );
3615 
3616     IF l_upd_fund_util_tbl.COUNT = G_BULK_LIMIT THEN --ninarasi fix for bug 13530939
3617            FORALL i IN NVL(l_upd_fund_util_tbl.FIRST,1) .. NVL(l_upd_fund_util_tbl.LAST,0)
3618               UPDATE ozf_funds_utilized_all_b
3619               SET    amount_remaining                  = amount_remaining - l_upd_fund_util_tbl(i).amount_remaining
3620                    , acctd_amount_remaining          = acctd_amount_remaining - l_upd_fund_util_tbl(i).acctd_amount_remaining
3621                    , plan_curr_amount_remaining      = plan_curr_amount_remaining - l_upd_fund_util_tbl(i).plan_curr_amount_remaining
3622                    , univ_curr_amount_remaining      = univ_curr_amount_remaining - l_upd_fund_util_tbl(i).univ_curr_amount_remaining
3623                    , fund_request_amount_remaining   = fund_request_amount_remaining - l_upd_fund_util_tbl(i).fund_request_amount_remaining
3624              WHERE utilization_id = l_upd_fund_util_tbl(i).utilization_id;
3625 
3626         l_upd_fund_util_tbl.delete;
3627     END IF;
3628 
3629 
3630     IF l_return_status =  fnd_api.g_ret_sts_error THEN
3631         RAISE FND_API.g_exc_error;
3632     ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3633         RAISE FND_API.g_exc_unexpected_error;
3634     END IF;
3635    ELSE
3636      IF l_upd_fund_util_tbl.COUNT > 0 THEN
3637         FORALL i IN NVL(l_upd_fund_util_tbl.FIRST,1) .. NVL(l_upd_fund_util_tbl.LAST,0) --ninarasi fix for bug 13530939
3638               UPDATE ozf_funds_utilized_all_b
3639               SET    amount_remaining                  = amount_remaining - l_upd_fund_util_tbl(i).amount_remaining
3640                    , acctd_amount_remaining          = acctd_amount_remaining - l_upd_fund_util_tbl(i).acctd_amount_remaining
3641                    , plan_curr_amount_remaining      = plan_curr_amount_remaining - l_upd_fund_util_tbl(i).plan_curr_amount_remaining
3642                    , univ_curr_amount_remaining      = univ_curr_amount_remaining - l_upd_fund_util_tbl(i).univ_curr_amount_remaining
3643                    , fund_request_amount_remaining   = fund_request_amount_remaining - l_upd_fund_util_tbl(i).fund_request_amount_remaining
3644              WHERE utilization_id = l_upd_fund_util_tbl(i).utilization_id;
3645         l_upd_fund_util_tbl.delete;
3646      END IF;
3647 
3648 
3649     EXIT;
3650    END IF;
3651   END LOOP;
3652   DBMS_SQL.close_cursor(l_funds_util_csr);
3653 
3654   -- Check for commit
3655   IF FND_API.to_boolean(p_commit) THEN
3656     COMMIT;
3657   END IF;
3658 
3659   FND_MSG_PUB.count_and_get(
3660          p_encoded => FND_API.g_false,
3661          p_count   => x_msg_count,
3662          p_data    => x_msg_data
3663   );
3664 
3665   IF OZF_DEBUG_HIGH_ON THEN
3666      OZF_Utility_PVT.debug_message(l_full_name ||': end');
3667   END IF;
3668 
3669 EXCEPTION
3670   WHEN FND_API.g_exc_error THEN
3671     ROLLBACK TO Delete_All_Line_Util;
3672     x_return_status := FND_API.g_ret_sts_error;
3673     FND_MSG_PUB.count_and_get (
3674            p_encoded => FND_API.g_false
3675           ,p_count   => x_msg_count
3676           ,p_data    => x_msg_data
3677     );
3678 
3679   WHEN FND_API.g_exc_unexpected_error THEN
3680     ROLLBACK TO Delete_All_Line_Util;
3681     x_return_status := FND_API.g_ret_sts_unexp_error ;
3682     FND_MSG_PUB.count_and_get (
3683            p_encoded => FND_API.g_false
3684           ,p_count   => x_msg_count
3685           ,p_data    => x_msg_data
3686     );
3687 
3688   WHEN OTHERS THEN
3689     ROLLBACK TO Delete_All_Line_Util;
3690     x_return_status := FND_API.g_ret_sts_unexp_error ;
3691     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
3692       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
3693     END IF;
3694     FND_MSG_PUB.count_and_get(
3695            p_encoded => FND_API.g_false
3696           ,p_count   => x_msg_count
3697           ,p_data    => x_msg_data
3698     );
3699 
3700 END Delete_All_Line_Util;
3701 
3702 ---------------------------------------------------------------------
3703 -- PROCEDURE
3704 --    Delete_Line_Util_Tbl
3705 --
3706 -- HISTORY
3707 --    05/12/2001  mchang  Create.
3708 ---------------------------------------------------------------------
3709 PROCEDURE Delete_Line_Util_Tbl(
3710     p_api_version            IN    NUMBER
3711    ,p_init_msg_list          IN    VARCHAR2 := FND_API.g_false
3712    ,p_commit                 IN    VARCHAR2 := FND_API.g_false
3713    ,p_validation_level       IN    NUMBER   := FND_API.g_valid_level_full
3714 
3715    ,x_return_status          OUT NOCOPY   VARCHAR2
3716    ,x_msg_data               OUT NOCOPY   VARCHAR2
3717    ,x_msg_count              OUT NOCOPY   NUMBER
3718 
3719    ,p_line_util_tbl          IN    line_util_tbl_type
3720    ,p_mode                   IN    VARCHAR2 := OZF_CLAIM_UTILITY_PVT.g_auto_mode
3721 
3722    ,x_error_index            OUT NOCOPY   NUMBER
3723 ) IS
3724 l_api_version  CONSTANT NUMBER       := 1.0;
3725 l_api_name     CONSTANT VARCHAR2(30) := 'Delete_Line_Util_Tbl';
3726 l_full_name    CONSTANT VARCHAR2(60) := g_pkg_name || '.' || l_api_name;
3727 l_return_status         VARCHAR2(1);
3728 
3729 i                       PLS_INTEGER;
3730 
3731 l_line_util_id          NUMBER;
3732 l_claim_id              NUMBER;
3733 l_object_version        NUMBER;
3734 l_access                VARCHAR2(1) := 'N';
3735 l_final_lu_acctd_amt    NUMBER;
3736 l_upd_fund_util_tbl          upd_fund_util_tbl_type; --ninarasi fix for bug 13530939
3737 
3738 -- Cursor to get claim_id
3739 CURSOR csr_claim_id(cv_claim_line_id IN NUMBER) IS
3740   SELECT claim_id
3741   FROM ozf_claim_lines
3742   WHERE claim_line_id = cv_claim_line_id;
3743 
3744 CURSOR csr_final_lu_acctd_amt(cv_claim_line_id IN NUMBER) IS
3745   SELECT SUM(acctd_amount)
3746   FROM ozf_claim_lines_util
3747   WHERE claim_line_id = cv_claim_line_id;
3748 
3749 BEGIN
3750   --------------------- initialize -----------------------
3751   SAVEPOINT Delete_Line_Util_Tbl;
3752 
3753   IF OZF_DEBUG_HIGH_ON THEN
3754      OZF_Utility_PVT.debug_message(l_full_name||': start');
3755   END IF;
3756 
3757   IF FND_API.to_boolean(p_init_msg_list) THEN
3758      FND_MSG_PUB.initialize;
3759   END IF;
3760 
3761   IF NOT FND_API.compatible_api_call(
3762              l_api_version,
3763              p_api_version,
3764              l_api_name,
3765              g_pkg_name
3766          ) THEN
3767     RAISE FND_API.g_exc_unexpected_error;
3768   END IF;
3769 
3770   x_return_status := FND_API.g_ret_sts_success;
3771 
3772   ---------------------- check claim access ------------------------
3773   IF p_mode = OZF_CLAIM_UTILITY_PVT.g_manu_mode AND p_line_util_tbl.count > 0 THEN
3774     FOR j IN p_line_util_tbl.FIRST..p_line_util_tbl.LAST LOOP
3775       IF p_line_util_tbl.EXISTS(j) THEN
3776 
3777         OPEN csr_claim_id(p_line_util_tbl(j).claim_line_id);
3778         FETCH csr_claim_id INTO l_claim_id;
3779         CLOSE csr_claim_id;
3780 
3781         OZF_CLAIM_UTILITY_PVT.Check_Claim_access(
3782                P_Api_Version_Number => 1.0
3783              , P_Init_Msg_List      => FND_API.G_FALSE
3784              , p_validation_level   => FND_API.G_VALID_LEVEL_FULL
3785              , P_Commit             => FND_API.G_FALSE
3786              , P_object_id          => l_claim_id
3787              , P_object_type        => G_CLAIM_OBJECT_TYPE
3788              , P_user_id            => OZF_UTILITY_PVT.get_resource_id(NVL(FND_GLOBAL.user_id,-1))
3789              , X_Return_Status      => l_return_status
3790              , X_Msg_Count          => x_msg_count
3791              , X_Msg_Data           => x_msg_data
3792              , X_access             => l_access);
3793 
3794         IF l_access = 'N' THEN
3795           IF FND_MSG_PUB.check_msg_level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
3796             FND_MESSAGE.set_name('OZF','OZF_CLAIM_NO_ACCESS');
3797             FND_MSG_PUB.add;
3798           END IF;
3799           RAISE FND_API.G_EXC_ERROR;
3800         END IF;
3801         EXIT;
3802       END IF;
3803     END LOOP;
3804   END IF;
3805 
3806   --------------------- Delete Claim Line Table -----------------------
3807   i := p_line_util_tbl.FIRST;
3808   IF i IS NOT NULL THEN
3809 
3810     LOOP
3811       l_line_util_id := p_line_util_tbl(i).claim_line_util_id;
3812       l_object_version := p_line_util_tbl(i).object_version_number;
3813       IF l_line_util_id IS NOT NULL THEN
3814         Delete_Line_Util(
3815                   p_api_version       => 1.0
3816                 , p_init_msg_list     => FND_API.g_false
3817                 , p_commit            => FND_API.g_false
3818                 , x_return_status     => l_return_status
3819                 , x_msg_count         => x_msg_count
3820                 , x_msg_data          => x_msg_data
3821                 , p_line_util_id      => l_line_util_id
3822                 , p_object_version    => l_object_version
3823                 , p_mode              => OZF_CLAIM_UTILITY_PVT.g_auto_mode
3824                 , px_upd_line_util_tbl => l_upd_fund_util_tbl --ninarasi fix for bug 13530939
3825         );
3826 
3827         IF l_upd_fund_util_tbl.COUNT = G_BULK_LIMIT OR i = p_line_util_tbl.LAST THEN --ninarasi fix for bug 13530939
3828            FORALL i IN NVL(l_upd_fund_util_tbl.FIRST,1) .. NVL(l_upd_fund_util_tbl.LAST,0)
3829               UPDATE ozf_funds_utilized_all_b
3830               SET    amount_remaining                  = amount_remaining - l_upd_fund_util_tbl(i).amount_remaining
3831                    , acctd_amount_remaining          = acctd_amount_remaining - l_upd_fund_util_tbl(i).acctd_amount_remaining
3832                    , plan_curr_amount_remaining      = plan_curr_amount_remaining - l_upd_fund_util_tbl(i).plan_curr_amount_remaining
3833                    , univ_curr_amount_remaining      = univ_curr_amount_remaining - l_upd_fund_util_tbl(i).univ_curr_amount_remaining
3834                    , fund_request_amount_remaining   = fund_request_amount_remaining - l_upd_fund_util_tbl(i).fund_request_amount_remaining
3835              WHERE utilization_id = l_upd_fund_util_tbl(i).utilization_id;
3836             l_upd_fund_util_tbl.delete;
3837 
3838         END IF;
3839 
3840         IF l_return_status =  fnd_api.g_ret_sts_error THEN
3841           x_error_index := i;
3842           RAISE FND_API.g_exc_error;
3843         ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
3844           x_error_index := i;
3845           RAISE FND_API.g_exc_unexpected_error;
3846         END IF;
3847       END IF;
3848     EXIT WHEN i = p_line_util_tbl.LAST;
3849     i := p_line_util_tbl.NEXT(i);
3850     END LOOP;
3851 
3852     -- update claim line earnings_associated_flag
3853     -- if there is no more earnings associated.
3854     OPEN csr_final_lu_acctd_amt(p_line_util_tbl(1).claim_line_id);
3855     FETCH csr_final_lu_acctd_amt INTO l_final_lu_acctd_amt;
3856     CLOSE csr_final_lu_acctd_amt;
3857 
3858     IF l_final_lu_acctd_amt = 0 OR l_final_lu_acctd_amt IS NULL THEN
3859       UPDATE ozf_claim_lines_all
3860         SET earnings_associated_flag = 'F'
3861         WHERE claim_line_id = p_line_util_tbl(1).claim_line_id;
3862     END IF;
3863 
3864   END IF;
3865 
3866   ------------------------- finish -------------------------------
3867   IF FND_API.to_boolean(p_commit) THEN
3868     COMMIT;
3869   END IF;
3870 
3871   FND_MSG_PUB.count_and_get(
3872          p_encoded => FND_API.g_false,
3873          p_count   => x_msg_count,
3874          p_data    => x_msg_data
3875   );
3876 
3877   IF OZF_DEBUG_HIGH_ON THEN
3878      OZF_Utility_PVT.debug_message(l_full_name ||': end');
3879   END IF;
3880 
3881 EXCEPTION
3882   WHEN FND_API.g_exc_error THEN
3883     ROLLBACK TO Delete_Line_Util_Tbl;
3884     x_return_status := FND_API.g_ret_sts_error;
3885     FND_MSG_PUB.count_and_get (
3886          p_encoded => FND_API.g_false
3887         ,p_count   => x_msg_count
3888         ,p_data    => x_msg_data
3889     );
3890 
3891   WHEN FND_API.g_exc_unexpected_error THEN
3892     ROLLBACK TO Delete_Line_Util_Tbl;
3893     x_return_status := FND_API.g_ret_sts_unexp_error ;
3894     FND_MSG_PUB.count_and_get (
3895          p_encoded => FND_API.g_false
3896         ,p_count   => x_msg_count
3897         ,p_data    => x_msg_data
3898     );
3899 
3900   WHEN OTHERS THEN
3901     ROLLBACK TO Delete_Line_Util_Tbl;
3902     x_return_status := FND_API.g_ret_sts_unexp_error ;
3903     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
3904       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
3905     END IF;
3906     FND_MSG_PUB.count_and_get(
3907          p_encoded => FND_API.g_false
3908         ,p_count   => x_msg_count
3909         ,p_data    => x_msg_data
3910     );
3911 
3912 END Delete_Line_Util_Tbl;
3913 
3914 
3915 ---------------------------------------------------------------
3916 -- PROCEDURE
3917 --    Delete_Line_Util
3918 --
3919 -- HISTORY
3920 --    05/11/2001  mchang  Create.
3921 --    07/22/2002  yizhang add p_mode for security check
3922 --    08-Aug-06   azahmed Modified for FXGL ER (Amount passed to Update_Funds_Util)
3923 ---------------------------------------------------------------
3924 PROCEDURE Delete_Line_Util(
3925    p_api_version       IN  NUMBER
3926   ,p_init_msg_list     IN  VARCHAR2 := FND_API.g_false
3927   ,p_commit            IN  VARCHAR2 := FND_API.g_false
3928 
3929   ,x_return_status     OUT NOCOPY VARCHAR2
3930   ,x_msg_count         OUT NOCOPY NUMBER
3931   ,x_msg_data          OUT NOCOPY VARCHAR2
3932 
3933   ,p_line_util_id      IN  NUMBER
3934   ,p_object_version    IN  NUMBER
3935   ,p_mode              IN  VARCHAR2 := OZF_CLAIM_UTILITY_PVT.g_auto_mode
3936   ,px_upd_line_util_tbl   IN OUT NOCOPY upd_fund_util_tbl_type --ninarasi fix for bug 13530939
3937 )
3938 IS
3939 l_api_version     CONSTANT NUMBER       := 1.0;
3940 l_api_name        CONSTANT VARCHAR2(30) := 'Delete_Line_Util';
3941 l_full_name       CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
3942 l_return_status            VARCHAR2(1);
3943 
3944 l_claim_line_id            NUMBER;
3945 l_claim_id                 NUMBER;
3946 l_utilization_id           NUMBER;
3947 l_del_line_util_amt  NUMBER;
3948 l_del_line_util_scan_unit  NUMBER;
3949 l_claim_currency           VARCHAR2(15);
3950 l_utiz_currency             VARCHAR2(15);
3951 l_plan_curr_amount         NUMBER;
3952 
3953 l_access                   VARCHAR2(1) := 'N';
3954 
3955 CURSOR csr_claim_line_details(cv_line_util_id IN NUMBER) IS
3956   SELECT clu.claim_line_id , clu.currency_code
3957   FROM  ozf_claim_lines_util clu
3958   WHERE claim_line_util_id = cv_line_util_id;
3959 
3960 CURSOR csr_claim_id(cv_claim_line_id IN NUMBER) IS
3961   SELECT claim_id
3962   FROM ozf_claim_lines
3963   WHERE claim_line_id = cv_claim_line_id;
3964 
3965 --Cursor changed for Claims-Multicurrency ER / Pranay
3966 CURSOR csr_old_line_util_amt(cv_line_util_id IN NUMBER) IS
3967   SELECT lu.claim_line_id
3968   ,      lu.utilization_id
3969   ,      Decode(lu.currency_code,fu.plan_currency_code, lu.plan_curr_amount, lu.acctd_amount)
3970   ,      lu.scan_unit
3971   FROM  ozf_claim_lines_util lu
3972       , ozf_funds_utilized_all_b fu
3973   WHERE claim_line_util_id = cv_line_util_id
3974     AND lu.utilization_id = fu.utilization_id;
3975 
3976 
3977 l_line_util_rec          line_util_rec_type;
3978 l_currency_rec           currency_rec_type;
3979 
3980 CURSOR csr_function_currency IS
3981   SELECT gs.currency_code
3982   FROM gl_sets_of_books gs
3983   ,    ozf_sys_parameters org
3984   WHERE org.set_of_books_id = gs.set_of_books_id
3985   AND   org.org_id = MO_GLOBAL.GET_CURRENT_ORG_ID();
3986 
3987 BEGIN
3988   --------------------- initialize -----------------------
3989   SAVEPOINT Delete_Line_Util;
3990 
3991   IF OZF_DEBUG_HIGH_ON THEN
3992      OZF_Utility_PVT.debug_message(l_full_name||': start');
3993   END IF;
3994 
3995   IF FND_API.to_boolean(p_init_msg_list) THEN
3996      FND_MSG_PUB.initialize;
3997   END IF;
3998 
3999   IF NOT FND_API.compatible_api_call(
4000          l_api_version,
4001          p_api_version,
4002          l_api_name,
4003          g_pkg_name
4004   ) THEN
4005     RAISE FND_API.g_exc_unexpected_error;
4006   END IF;
4007 
4008   x_return_status := FND_API.G_RET_STS_SUCCESS;
4009 
4010   OPEN csr_function_currency;
4011   FETCH csr_function_currency INTO l_currency_rec.functional_currency_code;
4012   CLOSE csr_function_currency;
4013 
4014   OPEN csr_claim_line_details(p_line_util_id);
4015   FETCH csr_claim_line_details INTO l_claim_line_id , l_claim_currency;
4016   CLOSE csr_claim_line_details;
4017 
4018   OPEN csr_claim_id(l_claim_line_id);
4019   FETCH csr_claim_id INTO l_claim_id;
4020   CLOSE csr_claim_id;
4021 
4022   ---------------------- check claim access ------------------------
4023   IF p_mode = OZF_CLAIM_UTILITY_PVT.g_manu_mode THEN
4024     OZF_CLAIM_UTILITY_PVT.Check_Claim_access(
4025            P_Api_Version_Number => 1.0
4026          , P_Init_Msg_List      => FND_API.G_FALSE
4027          , p_validation_level   => FND_API.G_VALID_LEVEL_FULL
4028          , P_Commit             => FND_API.G_FALSE
4029          , P_object_id          => l_claim_id
4030          , P_object_type        => G_CLAIM_OBJECT_TYPE
4031          , P_user_id            => OZF_UTILITY_PVT.get_resource_id(NVL(FND_GLOBAL.user_id,-1))
4032          , X_Return_Status      => l_return_status
4033          , X_Msg_Count          => x_msg_count
4034          , X_Msg_Data           => x_msg_data
4035          , X_access             => l_access);
4036 
4037     IF l_access = 'N' THEN
4038       IF FND_MSG_PUB.check_msg_level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
4039         FND_MESSAGE.set_name('OZF','OZF_CLAIM_NO_ACCESS');
4040           FND_MSG_PUB.add;
4041       END IF;
4042       RAISE FND_API.G_EXC_ERROR;
4043     END IF;
4044   END IF;
4045 
4046   --------------------- Start  -----------------------
4047   OPEN csr_old_line_util_amt(p_line_util_id);
4048   FETCH csr_old_line_util_amt INTO l_claim_line_id
4049                                  , l_utilization_id
4050                                  , l_del_line_util_amt
4051                                  , l_del_line_util_scan_unit;
4052   CLOSE csr_old_line_util_amt;
4053 
4054   ----------------- Update Utilization -----------------
4055   -- skip dummy utils
4056  IF l_utilization_id <> -1 THEN
4057         IF OZF_DEBUG_HIGH_ON THEN
4058            OZF_Utility_PVT.debug_message(l_full_name ||': update funds_utilized');
4059         END IF;
4060 
4061         l_line_util_rec.utilization_id := l_utilization_id;
4062         l_line_util_rec.claim_line_id := l_claim_line_id;
4063 
4064         IF OZF_DEBUG_LOW_ON THEN
4065           OZF_Utility_PVT.debug_message('l_utilization_id' || l_utilization_id);
4066           OZF_Utility_PVT.debug_message('l_claim_line_id' || l_claim_line_id);
4067           OZF_Utility_PVT.debug_message('l_del_line_util_amt' || l_del_line_util_amt);
4068         END IF;
4069 
4070         Update_Fund_Utils(
4071                 p_line_util_rec  => l_line_util_rec
4072               , p_asso_amount    => NVL(l_del_line_util_amt,0)
4073               , p_mode           => 'NONE'
4074               , px_currency_rec  => l_currency_rec
4075               , x_return_status  => l_return_status
4076               , x_msg_count      => x_msg_count
4077               , x_msg_data       => x_msg_data
4078               , px_upd_line_util_tbl =>  px_upd_line_util_tbl --ninarasi fix for bug 13530939
4079               );
4080 
4081              IF l_return_status =  fnd_api.g_ret_sts_error THEN
4082                     RAISE FND_API.g_exc_error;
4083              ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
4084                  RAISE FND_API.g_exc_unexpected_error;
4085          END IF;
4086 
4087  END IF;  -- IF l_utilization_rec.utilization_id <> -1
4088 
4089   ------------------------ delete ------------------------
4090   IF OZF_DEBUG_HIGH_ON THEN
4091      OZF_Utility_PVT.debug_message(l_full_name ||': delete');
4092   END IF;
4093 
4094   DELETE FROM ozf_claim_lines_util_all
4095     WHERE claim_line_util_id = p_line_util_id
4096     AND   object_version_number = p_object_version;
4097 
4098   IF (SQL%NOTFOUND) THEN
4099     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
4100       FND_MESSAGE.set_name('OZF', 'OZF_API_RECORD_NOT_FOUND');
4101       FND_MSG_PUB.add;
4102     END IF;
4103     RAISE FND_API.g_exc_error;
4104   END IF;
4105 
4106   -------------------- finish --------------------------
4107   IF FND_API.to_boolean(p_commit) THEN
4108     COMMIT;
4109   END IF;
4110 
4111   FND_MSG_PUB.count_and_get(
4112         p_encoded => FND_API.g_false,
4113         p_count   => x_msg_count,
4114         p_data    => x_msg_data
4115   );
4116 
4117   IF OZF_DEBUG_HIGH_ON THEN
4118      OZF_Utility_PVT.debug_message(l_full_name ||': end');
4119   END IF;
4120 
4121 EXCEPTION
4122   WHEN FND_API.g_exc_error THEN
4123     ROLLBACK TO Delete_Line_Util;
4124     x_return_status := FND_API.g_ret_sts_error;
4125     FND_MSG_PUB.count_and_get(
4126             p_encoded => FND_API.g_false,
4127             p_count   => x_msg_count,
4128             p_data    => x_msg_data
4129     );
4130 
4131   WHEN FND_API.g_exc_unexpected_error THEN
4132     ROLLBACK TO Delete_Line_Util;
4133     x_return_status := FND_API.g_ret_sts_unexp_error ;
4134     FND_MSG_PUB.count_and_get(
4135             p_encoded => FND_API.g_false,
4136             p_count   => x_msg_count,
4137             p_data    => x_msg_data
4138     );
4139 
4140   WHEN OTHERS THEN
4141     ROLLBACK TO Delete_Line_Util;
4142     x_return_status := FND_API.g_ret_sts_unexp_error ;
4143     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
4144       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
4145     END IF;
4146     FND_MSG_PUB.count_and_get(
4147             p_encoded => FND_API.g_false,
4148             p_count   => x_msg_count,
4149             p_data    => x_msg_data
4150     );
4151 
4152 END Delete_Line_Util;
4153 
4154 
4155 ---------------------------------------------------------------------
4156 -- PROCEDURE
4157 --    Init_Line_Util_Rec
4158 --
4159 -- HISTORY
4160 --    05/10/2001  mchang  Create.
4161 ---------------------------------------------------------------------
4162 PROCEDURE Init_Line_Util_Rec(
4163    x_line_util_rec   OUT NOCOPY  line_util_rec_type
4164 )
4165 IS
4166 BEGIN
4167 
4168    RETURN;
4169 END Init_Line_Util_Rec;
4170 
4171 
4172 ---------------------------------------------------------------------
4173 -- PROCEDURE
4174 --    Complete_Line_Util_Rec
4175 --
4176 -- HISTORY
4177 --    05/10/2001  mchang  Create.
4178 ---------------------------------------------------------------------
4179 PROCEDURE Complete_Line_Util_Rec(
4180    p_line_util_rec      IN  line_util_rec_type
4181   ,x_complete_rec       OUT NOCOPY line_util_rec_type
4182 )
4183 IS
4184 CURSOR csr_line_util(cv_line_util_id  IN NUMBER) IS
4185 SELECT object_version_number,
4186        claim_line_id,
4187        utilization_id,
4188        amount,
4189        currency_code,
4190        exchange_rate_type,
4191        exchange_rate_date,
4192        exchange_rate,
4193        acctd_amount,
4194        scan_unit,
4195        activity_product_id,
4196        uom_code,
4197        quantity,
4198        org_id,
4199        fxgl_acctd_amount,
4200        utilized_acctd_amount,
4201        util_curr_amount, --nepanda : fix for bug # 9508390  - issue # 1
4202        univ_curr_amount,
4203        plan_curr_amount
4204 FROM  ozf_claim_lines_util
4205 WHERE  claim_line_util_id = cv_line_util_id;
4206 
4207 l_line_util_rec  csr_line_util%ROWTYPE;
4208 
4209 BEGIN
4210   x_complete_rec := p_line_util_rec;
4211 
4212   OPEN csr_line_util(p_line_util_rec.claim_line_util_id);
4213   FETCH csr_line_util INTO l_line_util_rec;
4214   IF csr_line_util%NOTFOUND THEN
4215     CLOSE csr_line_util;
4216     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
4217       FND_MESSAGE.set_name('OZF', 'OZF_API_RECORD_NOT_FOUND');
4218       FND_MSG_PUB.add;
4219     END IF;
4220     RAISE FND_API.g_exc_error;
4221   END IF;
4222   CLOSE csr_line_util;
4223 
4224   IF p_line_util_rec.object_version_number = FND_API.G_MISS_NUM THEN
4225      x_complete_rec.object_version_number := NULL;
4226   END IF;
4227   IF p_line_util_rec.object_version_number IS NULL THEN
4228      x_complete_rec.object_version_number := l_line_util_rec.object_version_number;
4229   END IF;
4230 
4231   IF p_line_util_rec.claim_line_id = FND_API.G_MISS_NUM THEN
4232      x_complete_rec.claim_line_id := NULL;
4233   END IF;
4234   IF p_line_util_rec.claim_line_id IS NULL THEN
4235      x_complete_rec.claim_line_id := l_line_util_rec.claim_line_id;
4236   END IF;
4237 
4238   IF p_line_util_rec.utilization_id = FND_API.G_MISS_NUM THEN
4239      x_complete_rec.utilization_id := NULL;
4240   END IF;
4241   IF p_line_util_rec.utilization_id IS NULL THEN
4242      x_complete_rec.utilization_id := l_line_util_rec.utilization_id;
4243   END IF;
4244 
4245   IF p_line_util_rec.amount = FND_API.G_MISS_NUM THEN
4246      x_complete_rec.amount := NULL;
4247   END IF;
4248   IF p_line_util_rec.amount IS NULL THEN
4249      x_complete_rec.amount := l_line_util_rec.amount;
4250   END IF;
4251 
4252   IF p_line_util_rec.currency_code = FND_API.G_MISS_CHAR THEN
4253      x_complete_rec.currency_code := NULL;
4254   END IF;
4255   IF p_line_util_rec.currency_code IS NULL THEN
4256      x_complete_rec.currency_code := l_line_util_rec.currency_code;
4257   END IF;
4258 
4259   IF p_line_util_rec.exchange_rate_type = FND_API.G_MISS_CHAR THEN
4260      x_complete_rec.exchange_rate_type := NULL;
4261   END IF;
4262   IF p_line_util_rec.exchange_rate_type IS NULL THEN
4263      x_complete_rec.exchange_rate_type := l_line_util_rec.exchange_rate_type;
4264   END IF;
4265 
4266   IF p_line_util_rec.exchange_rate_date = FND_API.G_MISS_DATE THEN
4267      x_complete_rec.exchange_rate_date := NULL;
4268   END IF;
4269   IF p_line_util_rec.exchange_rate_date IS NULL THEN
4270      x_complete_rec.exchange_rate_date := l_line_util_rec.exchange_rate_date;
4271   END IF;
4272 
4273   IF p_line_util_rec.exchange_rate = FND_API.G_MISS_NUM THEN
4274      x_complete_rec.exchange_rate := NULL;
4275   END IF;
4276   IF p_line_util_rec.exchange_rate IS NULL THEN
4277      x_complete_rec.exchange_rate := l_line_util_rec.exchange_rate;
4278   END IF;
4279 
4280   IF p_line_util_rec.acctd_amount = FND_API.G_MISS_NUM THEN
4281      x_complete_rec.acctd_amount := NULL;
4282   END IF;
4283   IF p_line_util_rec.acctd_amount IS NULL THEN
4284      x_complete_rec.acctd_amount := l_line_util_rec.acctd_amount;
4285   END IF;
4286 --nepanda : fix for bug # 9508390  - issue # 1
4287   IF p_line_util_rec.util_curr_amount = FND_API.G_MISS_NUM THEN
4288      x_complete_rec.util_curr_amount := NULL;
4289   END IF;
4290   IF p_line_util_rec.util_curr_amount IS NULL THEN
4291      x_complete_rec.util_curr_amount := l_line_util_rec.util_curr_amount;
4292   END IF;
4293   IF p_line_util_rec.univ_curr_amount = FND_API.G_MISS_NUM THEN
4294      x_complete_rec.univ_curr_amount := NULL;
4295   END IF;
4296   IF p_line_util_rec.univ_curr_amount IS NULL THEN
4297      x_complete_rec.univ_curr_amount := l_line_util_rec.univ_curr_amount;
4298   END IF;
4299   IF p_line_util_rec.plan_curr_amount = FND_API.G_MISS_NUM THEN
4300      x_complete_rec.plan_curr_amount := NULL;
4301   END IF;
4302   IF p_line_util_rec.plan_curr_amount IS NULL THEN
4303      x_complete_rec.plan_curr_amount := l_line_util_rec.plan_curr_amount;
4304   END IF;
4305   IF p_line_util_rec.scan_unit = FND_API.G_MISS_NUM THEN
4306      x_complete_rec.scan_unit := NULL;
4307   END IF;
4308   IF p_line_util_rec.scan_unit IS NULL THEN
4309      x_complete_rec.scan_unit := l_line_util_rec.scan_unit;
4310   END IF;
4311 
4312   IF p_line_util_rec.activity_product_id = FND_API.G_MISS_NUM THEN
4313      x_complete_rec.activity_product_id := NULL;
4314   END IF;
4315   IF p_line_util_rec.activity_product_id IS NULL THEN
4316      x_complete_rec.activity_product_id := l_line_util_rec.activity_product_id;
4317   END IF;
4318 
4319   IF p_line_util_rec.uom_code = FND_API.G_MISS_CHAR THEN
4320      x_complete_rec.uom_code := NULL;
4321   END IF;
4322   IF p_line_util_rec.uom_code IS NULL THEN
4323      x_complete_rec.uom_code := l_line_util_rec.uom_code;
4324   END IF;
4325 
4326   IF p_line_util_rec.quantity = FND_API.G_MISS_NUM THEN
4327      x_complete_rec.quantity := NULL;
4328   END IF;
4329   IF p_line_util_rec.quantity IS NULL THEN
4330      x_complete_rec.quantity := l_line_util_rec.quantity;
4331   END IF;
4332 
4333   IF p_line_util_rec.org_id = FND_API.G_MISS_NUM THEN
4334      x_complete_rec.org_id := NULL;
4335   END IF;
4336   IF p_line_util_rec.org_id IS NULL THEN
4337      x_complete_rec.org_id := l_line_util_rec.org_id;
4338   END IF;
4339 
4340   IF p_line_util_rec.fxgl_acctd_amount = FND_API.G_MISS_NUM THEN
4341      x_complete_rec.fxgl_acctd_amount := NULL;
4342   END IF;
4343   IF p_line_util_rec.fxgl_acctd_amount IS NULL THEN
4344      x_complete_rec.fxgl_acctd_amount := l_line_util_rec.fxgl_acctd_amount;
4345   END IF;
4346 
4347 
4348   IF p_line_util_rec.utilized_acctd_amount = FND_API.G_MISS_NUM THEN
4349      x_complete_rec.utilized_acctd_amount := NULL;
4350   END IF;
4351   IF p_line_util_rec.utilized_acctd_amount IS NULL THEN
4352      x_complete_rec.utilized_acctd_amount := l_line_util_rec.utilized_acctd_amount;
4353   END IF;
4354 
4355 END Complete_Line_Util_Rec;
4356 
4357 ---------------------------------------------------------------------
4358 -- PROCEDURE
4359 --   Check_Offer_Performance
4360 --
4361 -- PARAMETERS
4362 --    p_cust_account_id   : customer account id
4363 --    p_offer_id          : offer id
4364 --
4365 -- HISTORY
4366 ---------------------------------------------------------------------
4367 PROCEDURE Check_Offer_Performance(
4368    p_cust_account_id           IN  NUMBER
4369   ,p_offer_id                  IN  NUMBER
4370   ,p_resale_flag               IN  VARCHAR2
4371   ,p_check_all_flag            IN  VARCHAR2
4372 
4373   ,x_performance_flag          OUT NOCOPY VARCHAR2
4374   ,x_offer_perf_tbl            OUT NOCOPY offer_performance_tbl_type
4375 )
4376 IS
4377 l_offer_performance_id NUMBER;
4378 l_product_attr_context VARCHAR2(30);
4379 l_product_attribute   VARCHAR2(30);
4380 l_product_attr_value  VARCHAR2(240);
4381 l_start_date          DATE;
4382 l_end_date            DATE;
4383 l_requirement_type    VARCHAR2(30);
4384 l_requirement_value   NUMBER;
4385 l_uom_code            VARCHAR2(30);
4386 l_common_quantity     NUMBER;
4387 l_common_amount       NUMBER;
4388 l_common_uom_code     VARCHAR2(3);
4389 l_common_curr_code    VARCHAR2(15);
4390 l_comm_curr_req_amt   NUMBER;
4391 l_offer_currency      VARCHAR2(15);
4392 l_return_status       VARCHAR2(1);
4393 l_temp_sql            VARCHAR2(2000);
4394 l_emp_csr             NUMBER;
4395 l_ignore              NUMBER;
4396 l_counter             PLS_INTEGER := 0;
4397 l_performance_flag    VARCHAR2(1);
4398 l_offer_perf_tbl      offer_performance_tbl_type;
4399 
4400 CURSOR csr_offer_perfs(cv_offer_id IN NUMBER) IS
4401   SELECT offer_performance_id
4402        , product_attribute_context
4403        , product_attribute
4404        , product_attr_value
4405        , start_date
4406        , end_date
4407        , requirement_type
4408        , estimated_value
4409        , uom_code
4410   FROM ozf_offer_performances
4411   WHERE required_flag = 'Y'
4412   AND product_attribute_context = 'ITEM'
4413   AND requirement_type IN ('AMOUNT', 'VOLUME')
4414   AND list_header_id = cv_offer_id;
4415 
4416 -- fix for bug 5042046
4417 CURSOR csr_function_currency IS
4418   SELECT gs.currency_code
4419   FROM gl_sets_of_books gs
4420   ,    ozf_sys_parameters org
4421   WHERE org.set_of_books_id = gs.set_of_books_id
4422   AND   org.org_id = MO_GLOBAL.GET_CURRENT_ORG_ID();
4423 
4424 BEGIN
4425    x_performance_flag := FND_API.g_true;
4426 
4427    OPEN csr_offer_perfs(p_offer_id);
4428    LOOP
4429       FETCH csr_offer_perfs INTO l_offer_performance_id
4430                                , l_product_attr_context
4431                                , l_product_attribute
4432                                , l_product_attr_value
4433                                , l_start_date
4434                                , l_end_date
4435                                , l_requirement_type
4436                                , l_requirement_value
4437                                , l_uom_code;
4438       EXIT WHEN csr_offer_perfs%NOTFOUND;
4439 
4440       l_performance_flag := 'T';
4441 
4442       IF p_resale_flag IS NULL OR p_resale_flag = 'F' THEN
4443          FND_DSQL.init;
4444          FND_DSQL.add_text('SELECT NVL(sum(common_quantity), 0), NVL(sum(common_amount), 0), ');
4445          FND_DSQL.add_text(' common_uom_code, common_currency_code ');
4446          FND_DSQL.add_text(' FROM ozf_sales_transactions ');
4447          FND_DSQL.add_text(' WHERE sold_to_cust_account_id = ');
4448          FND_DSQL.add_bind(p_cust_account_id);
4449          FND_DSQL.add_text(' AND transaction_date between ');
4450          FND_DSQL.add_bind(l_start_date);
4451          FND_DSQL.add_text(' and ');
4452          FND_DSQL.add_bind(l_end_date);
4453          FND_DSQL.add_text(' AND inventory_item_id IN (SELECT s.product_id FROM ( ');
4454          l_temp_sql := OZF_OFFR_ELIG_PROD_DENORM_PVT.get_sql(
4455             p_context         => l_product_attr_context,
4456             p_attribute       => l_product_attribute,
4457             p_attr_value_from => l_product_attr_value,
4458             p_attr_value_to   => NULL,
4459             p_comparison      => NULL,
4460             p_type            => 'PROD'
4461          );
4462          FND_DSQL.add_text(') s) ');
4463          FND_DSQL.add_text(' GROUP BY common_uom_code, common_currency_code ');
4464 
4465          IF OZF_DEBUG_HIGH_ON THEN
4466             l_temp_sql := FND_DSQL.get_text(TRUE);
4467             OZF_Utility_PVT.debug_message('----- Check_Offer_Performance SQL -----');
4468             OZF_Utility_PVT.debug_message(SUBSTR(l_temp_sql, 1, 254));
4469             OZF_Utility_PVT.debug_message(SUBSTR(l_temp_sql, 255, 254));
4470             OZF_Utility_PVT.debug_message(SUBSTR(l_temp_sql, 509, 254));
4471             OZF_Utility_PVT.debug_message('---------------------------------------');
4472          END IF;
4473 
4474          l_emp_csr := DBMS_SQL.open_cursor;
4475          FND_DSQL.set_cursor(l_emp_csr);
4476          DBMS_SQL.parse(l_emp_csr, FND_DSQL.get_text(FALSE), DBMS_SQL.native);
4477          DBMS_SQL.define_column(l_emp_csr, 1, l_common_quantity);
4478          DBMS_SQL.define_column(l_emp_csr, 2, l_common_amount);
4479          DBMS_SQL.define_column(l_emp_csr, 3, l_common_uom_code, 3);
4480          DBMS_SQL.define_column(l_emp_csr, 4, l_common_curr_code, 15);
4481          FND_DSQL.do_binds;
4482 
4483          l_ignore := DBMS_SQL.execute(l_emp_csr);
4484          IF DBMS_SQL.fetch_rows(l_emp_csr) > 0 THEN
4485             DBMS_SQL.column_value(l_emp_csr, 1, l_common_quantity);
4486             DBMS_SQL.column_value(l_emp_csr, 2, l_common_amount);
4487             DBMS_SQL.column_value(l_emp_csr, 3, l_common_uom_code);
4488             DBMS_SQL.column_value(l_emp_csr, 4, l_common_curr_code);
4489 
4490             IF l_requirement_type = 'VOLUME' THEN
4491                IF l_uom_code = l_common_uom_code THEN
4492                   IF l_common_quantity < l_requirement_value THEN
4493                      l_performance_flag := FND_API.g_false;
4494                   END IF;
4495                ELSE
4496                   l_requirement_value := inv_convert.inv_um_convert(
4497                         item_id         => NULL
4498                        ,precision       => 2
4499                        ,from_quantity   => l_requirement_value
4500                        ,from_unit       => l_uom_code
4501                        ,to_unit         => l_common_uom_code
4502                        ,from_name       => NULL
4503                        ,to_name         => NULL
4504                   );
4505                   IF l_requirement_value = -99999 THEN
4506                       IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
4507                          FND_MESSAGE.set_name('OZF', 'OZF_CLAIM_CANT_CONVERT_UOM');
4508                          FND_MSG_PUB.add;
4509                       END IF;
4510                       RAISE FND_API.g_exc_error;
4511                   END IF;
4512 
4513                   IF l_common_quantity < l_requirement_value THEN
4514                      l_performance_flag := FND_API.g_false;
4515                   END IF;
4516                END IF;
4517             ELSIF l_requirement_type = 'AMOUNT' THEN
4518                l_offer_currency := OZF_ACTBUDGETS_PVT.Get_Object_Currency(
4519                      p_object          => 'OFFR'
4520                     ,p_object_id       => p_offer_id
4521                     ,x_return_status   => l_return_status
4522                );
4523                IF l_return_status = FND_API.g_ret_sts_error THEN
4524                   RAISE FND_API.g_exc_error;
4525                ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
4526                   RAISE FND_API.g_exc_unexpected_error;
4527                END IF;
4528 
4529                IF l_offer_currency = l_common_curr_code THEN
4530                   IF l_common_amount < l_requirement_value THEN
4531                      l_performance_flag := FND_API.g_false;
4532                   END IF;
4533                ELSE
4534                   OZF_UTILITY_PVT.Convert_Currency(
4535                       p_from_currency   => l_offer_currency
4536                      ,p_to_currency     => l_common_curr_code
4537                      ,p_conv_date       => SYSDATE
4538                      ,p_from_amount     => l_requirement_value
4539                      ,x_return_status   => l_return_status
4540                      ,x_to_amount       => l_comm_curr_req_amt
4541                   );
4542                   IF l_return_status = FND_API.g_ret_sts_error THEN
4543                      RAISE FND_API.g_exc_error;
4544                   ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
4545                      RAISE FND_API.g_exc_unexpected_error;
4546                   END IF;
4547 
4548                   IF l_common_amount < l_comm_curr_req_amt THEN
4549                      l_performance_flag := FND_API.g_false;
4550                   END IF;
4551                END IF;
4552             END IF;
4553          ELSE
4554             -- no rows returned
4555             IF OZF_DEBUG_HIGH_ON THEN
4556                OZF_Utility_PVT.debug_message('No sales transactions found');
4557             END IF;
4558             l_performance_flag := FND_API.g_false;
4559          END IF;
4560          DBMS_SQL.close_cursor(l_emp_csr);
4561 
4562       ELSE
4563          /********** Check resale data ***********/
4564          FND_DSQL.init;
4565          FND_DSQL.add_text('SELECT NVL(sum(quantity), 0), NVL(sum(quantity*acctd_selling_price), 0), ');
4566          FND_DSQL.add_text(' uom_code ');
4567          FND_DSQL.add_text(' FROM ozf_resale_lines ');
4568          FND_DSQL.add_text(' WHERE sold_from_cust_account_id = ');
4569          FND_DSQL.add_bind(p_cust_account_id);
4570          FND_DSQL.add_text(' AND date_ordered between ');
4571          FND_DSQL.add_bind(l_start_date);
4572          FND_DSQL.add_text(' and ');
4573          FND_DSQL.add_bind(l_end_date);
4574          FND_DSQL.add_text(' AND inventory_item_id IN (SELECT s.product_id FROM ( ');
4575          l_temp_sql := OZF_OFFR_ELIG_PROD_DENORM_PVT.get_sql(
4576             p_context         => l_product_attr_context,
4577             p_attribute       => l_product_attribute,
4578             p_attr_value_from => l_product_attr_value,
4579             p_attr_value_to   => NULL,
4580             p_comparison      => NULL,
4581             p_type            => 'PROD'
4582          );
4583          FND_DSQL.add_text(') s) ');
4584          FND_DSQL.add_text(' GROUP BY uom_code ');
4585 
4586          IF OZF_DEBUG_HIGH_ON THEN
4587             l_temp_sql := FND_DSQL.get_text(TRUE);
4588             OZF_Utility_PVT.debug_message('----- Check_Offer_Performance SQL -----');
4589             OZF_Utility_PVT.debug_message(SUBSTR(l_temp_sql, 1, 254));
4590             OZF_Utility_PVT.debug_message(SUBSTR(l_temp_sql, 255, 254));
4591             OZF_Utility_PVT.debug_message(SUBSTR(l_temp_sql, 509, 254));
4592             OZF_Utility_PVT.debug_message('---------------------------------------');
4593          END IF;
4594 
4595          l_emp_csr := DBMS_SQL.open_cursor;
4596          FND_DSQL.set_cursor(l_emp_csr);
4597          DBMS_SQL.parse(l_emp_csr, FND_DSQL.get_text(FALSE), DBMS_SQL.native);
4598          DBMS_SQL.define_column(l_emp_csr, 1, l_common_quantity);
4599          DBMS_SQL.define_column(l_emp_csr, 2, l_common_amount);
4600          DBMS_SQL.define_column(l_emp_csr, 3, l_common_uom_code, 3);
4601          FND_DSQL.do_binds;
4602 
4603          l_ignore := DBMS_SQL.execute(l_emp_csr);
4604          IF DBMS_SQL.fetch_rows(l_emp_csr) > 0 THEN
4605             DBMS_SQL.column_value(l_emp_csr, 1, l_common_quantity);
4606             DBMS_SQL.column_value(l_emp_csr, 2, l_common_amount);
4607             DBMS_SQL.column_value(l_emp_csr, 3, l_common_uom_code);
4608 
4609             IF l_requirement_type = 'VOLUME' THEN
4610                IF l_uom_code = l_common_uom_code THEN
4611                   IF l_common_quantity < l_requirement_value THEN
4612                      l_performance_flag := FND_API.g_false;
4613                   END IF;
4614                ELSE
4615                   l_requirement_value := inv_convert.inv_um_convert(
4616                         item_id         => NULL
4617                        ,precision       => 2
4618                        ,from_quantity   => l_requirement_value
4619                        ,from_unit       => l_uom_code
4620                        ,to_unit         => l_common_uom_code
4621                        ,from_name       => NULL
4622                        ,to_name         => NULL
4623                   );
4624                   IF l_requirement_value = -99999 THEN
4625                       IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
4626                          FND_MESSAGE.set_name('OZF', 'OZF_CLAIM_CANT_CONVERT_UOM');
4627                          FND_MSG_PUB.add;
4628                       END IF;
4629                       RAISE FND_API.g_exc_error;
4630                   END IF;
4631 
4632                   IF l_common_quantity < l_requirement_value THEN
4633                      l_performance_flag := FND_API.g_false;
4634                   END IF;
4635                END IF;
4636             ELSIF l_requirement_type = 'AMOUNT' THEN
4637                l_offer_currency := OZF_ACTBUDGETS_PVT.Get_Object_Currency(
4638                      p_object          => 'OFFR'
4639                     ,p_object_id       => p_offer_id
4640                     ,x_return_status   => l_return_status
4641                );
4642                IF l_return_status = FND_API.g_ret_sts_error THEN
4643                   RAISE FND_API.g_exc_error;
4644                ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
4645                   RAISE FND_API.g_exc_unexpected_error;
4646                END IF;
4647 
4648                OPEN csr_function_currency;
4649                FETCH csr_function_currency INTO l_common_curr_code;
4650                CLOSE csr_function_currency;
4651 
4652                IF l_offer_currency = l_common_curr_code THEN
4653                   IF l_common_amount < l_requirement_value THEN
4654                      l_performance_flag := FND_API.g_false;
4655                   END IF;
4656                ELSE
4657                   OZF_UTILITY_PVT.Convert_Currency(
4658                       p_from_currency   => l_offer_currency
4659                      ,p_to_currency     => l_common_curr_code
4660                      ,p_conv_date       => SYSDATE
4661                      ,p_from_amount     => l_requirement_value
4662                      ,x_return_status   => l_return_status
4663                      ,x_to_amount       => l_comm_curr_req_amt
4664                   );
4665                   IF l_return_status = FND_API.g_ret_sts_error THEN
4666                      RAISE FND_API.g_exc_error;
4667                   ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
4668                      RAISE FND_API.g_exc_unexpected_error;
4669                   END IF;
4670 
4671                   IF l_common_amount < l_comm_curr_req_amt THEN
4672                      l_performance_flag := FND_API.g_false;
4673                   END IF;
4674                END IF;
4675             END IF;
4676          ELSE
4677             -- no rows returned
4678             IF OZF_DEBUG_HIGH_ON THEN
4679                OZF_Utility_PVT.debug_message('No sales transactions found');
4680             END IF;
4681             l_performance_flag := FND_API.g_false;
4682          END IF;
4683          DBMS_SQL.close_cursor(l_emp_csr);
4684 
4685       END IF;
4686 
4687       IF l_performance_flag = 'F' THEN
4688          x_performance_flag := FND_API.g_false;
4689 
4690          l_counter := l_counter + 1;
4691          l_offer_perf_tbl(l_counter).offer_id := p_offer_id;
4692          l_offer_perf_tbl(l_counter).offer_performance_id := l_offer_performance_id;
4693          l_offer_perf_tbl(l_counter).product_attribute := l_product_attribute;
4694          l_offer_perf_tbl(l_counter).product_attr_value := l_product_attr_value;
4695          l_offer_perf_tbl(l_counter).start_date := l_start_date;
4696          l_offer_perf_tbl(l_counter).end_date := l_end_date;
4697          l_offer_perf_tbl(l_counter).requirement_type := l_requirement_type;
4698          l_offer_perf_tbl(l_counter).estimated_value := l_requirement_value;
4699          l_offer_perf_tbl(l_counter).uom_code := l_uom_code;
4700 
4701          IF p_check_all_flag = 'F' THEN
4702             EXIT;
4703          END IF;
4704       END IF;
4705 
4706    END LOOP;
4707    CLOSE csr_offer_perfs;
4708    x_offer_perf_tbl := l_offer_perf_tbl;
4709 
4710 END Check_Offer_Performance;
4711 
4712 ---------------------------------------------------------------------
4713 -- PROCEDURE
4714 --    Settle_Claim
4715 --
4716 -- PURPOSE
4717 --    Close a claim
4718 --
4719 -- PARAMETERS
4720 --    p_claim_id: claim id
4721 --
4722 -- NOTES
4723 ---------------------------------------------------------------------
4724 PROCEDURE Settle_Claim(
4725    p_claim_id            IN  NUMBER
4726   ,x_return_status       OUT NOCOPY VARCHAR2
4727   ,x_msg_count           OUT NOCOPY NUMBER
4728   ,x_msg_data            OUT NOCOPY VARCHAR2
4729 )
4730 IS
4731 l_api_version CONSTANT NUMBER       := 1.0;
4732 l_api_name    CONSTANT VARCHAR2(30) := 'Settle_Claim';
4733 l_return_status VARCHAR2(1);
4734 
4735 l_claim_id           NUMBER         := p_claim_id;
4736 l_claim_rec          OZF_CLAIM_PVT.claim_rec_type;
4737 l_object_version_number NUMBER;
4738 l_sales_rep_id       NUMBER;
4739 l_salesrep_req_flag  VARCHAR2(1);
4740 
4741 CURSOR csr_claim_info(cv_claim_id in number) IS
4742   select object_version_number, sales_rep_id
4743   from ozf_claims_all
4744   where claim_id = cv_claim_id;
4745 
4746 CURSOR csr_ar_system_options IS
4747   SELECT salesrep_required_flag
4748   FROM ar_system_parameters;
4749 
4750 BEGIN
4751   --------------------- initialize -----------------------
4752   SAVEPOINT Settle_Claim;
4753 
4754   x_return_status := FND_API.g_ret_sts_success;
4755 
4756   ----------------- start ----------------
4757   OPEN csr_claim_info(l_claim_id);
4758   FETCH csr_claim_info into l_object_version_number, l_sales_rep_id;
4759   CLOSE csr_claim_info;
4760 
4761   l_claim_rec.claim_id              := l_claim_id;
4762   l_claim_rec.object_version_number := l_object_version_number;
4763   l_claim_rec.USER_STATUS_ID        := to_number( ozf_utility_pvt.GET_DEFAULT_USER_STATUS(
4764                                                     P_STATUS_TYPE=> 'OZF_CLAIM_STATUS',
4765                                                     P_STATUS_CODE=> 'CLOSED'
4766                                                 ));
4767 
4768   ------------------------------------------------------
4769   -- Sales Credit
4770   --   Bug 2950241 fixing: default Sales Rep in Claims
4771   --   if "Requires Salesperson" in AR system options.
4772   ------------------------------------------------------
4773   IF l_sales_rep_id IS NULL THEN
4774     OPEN csr_ar_system_options;
4775     FETCH csr_ar_system_options INTO l_salesrep_req_flag;
4776     CLOSE csr_ar_system_options;
4777 
4778     IF l_salesrep_req_flag = 'Y' THEN
4779       l_claim_rec.sales_rep_id := -3;  -- No Sales Credit
4780     END IF;
4781   END IF;
4782 
4783   OZF_claim_PVT.Update_claim(
4784      P_Api_Version                => l_api_version,
4785      P_Init_Msg_List              => FND_API.g_false,
4786      P_Commit                     => FND_API.g_false,
4787      P_Validation_Level           => FND_API.g_valid_level_full,
4788      X_Return_Status              => l_return_status,
4789      X_Msg_Count                  => x_msg_count,
4790      X_Msg_Data                   => x_msg_data,
4791      P_claim                      => l_claim_Rec,
4792      p_event                      => 'UPDATE',
4793      p_mode                       => OZF_claim_Utility_pvt.G_AUTO_MODE,
4794      X_Object_Version_Number      => l_object_version_number
4795   );
4796   IF l_return_status = FND_API.g_ret_sts_error THEN
4797     RAISE FND_API.g_exc_error;
4798   ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
4799     RAISE FND_API.g_exc_unexpected_error;
4800   END IF;
4801 
4802 EXCEPTION
4803   WHEN FND_API.g_exc_error THEN
4804     ROLLBACK TO Settle_Claim;
4805     x_return_status := FND_API.g_ret_sts_error;
4806   WHEN FND_API.g_exc_unexpected_error THEN
4807     ROLLBACK TO Settle_Claim;
4808     x_return_status := FND_API.g_ret_sts_unexp_error ;
4809   WHEN OTHERS THEN
4810     ROLLBACK TO Settle_Claim;
4811     x_return_status := FND_API.g_ret_sts_unexp_error ;
4812     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
4813       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
4814     END IF;
4815 END Settle_Claim;
4816 
4817 
4818 ---------------------------------------------------------------------
4819 -- PROCEDURE
4820 --    Create_Claim_For_Accruals
4821 --
4822 -- PURPOSE
4823 --    Create a claim and associate earnings based on search filters.
4824 --
4825 -- PARAMETERS
4826 --    p_claim_rec: claim record
4827 --    p_funds_util_flt: search filter for earnings
4828 --
4829 -- NOTES
4830 --
4831 -- HISTORY
4832 --
4833 -----------------------------------------------------------------------------------------
4834 PROCEDURE Create_Claim_For_Accruals(
4835    p_api_version         IN  NUMBER
4836   ,p_init_msg_list       IN  VARCHAR2  := FND_API.g_false
4837   ,p_commit              IN  VARCHAR2  := FND_API.g_false
4838   ,p_validation_level    IN  NUMBER    := FND_API.g_valid_level_full
4839 
4840   ,x_return_status       OUT NOCOPY VARCHAR2
4841   ,x_msg_count           OUT NOCOPY NUMBER
4842   ,x_msg_data            OUT NOCOPY VARCHAR2
4843 
4844   ,p_claim_rec           IN  ozf_claim_pvt.claim_rec_type
4845   ,p_funds_util_flt      IN  ozf_claim_accrual_pvt.funds_util_flt_type
4846 
4847   ,x_claim_id            OUT NOCOPY NUMBER
4848 ) IS
4849 l_api_version CONSTANT NUMBER       := 1.0;
4850 l_api_name    CONSTANT VARCHAR2(30) := 'Create_Claim_For_Accruals';
4851 l_full_name   CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
4852 l_return_status VARCHAR2(1);
4853 
4854 l_funds_util_flt      ozf_claim_accrual_pvt.funds_util_flt_type;
4855 l_line_tbl            OZF_CLAIM_LINE_PVT.claim_line_tbl_type;
4856 l_claim_rec           OZF_CLAIM_PVT.claim_rec_type := p_claim_rec;
4857 l_claim_id            NUMBER;
4858 l_cust_account_id     NUMBER;
4859 l_plan_type           VARCHAR2(30);
4860 l_plan_id             NUMBER;
4861 l_product_level_type  VARCHAR2(30);
4862 l_product_id          NUMBER;
4863 l_amount              NUMBER;
4864 l_total_acctd_amount_rem NUMBER;
4865 l_performance_flag    VARCHAR2(1) := FND_API.g_true;
4866 
4867 l_emp_csr             NUMBER;
4868 l_stmt                VARCHAR2(3000);
4869 l_ignore              NUMBER;
4870 l_counter             PLS_INTEGER := 1;
4871 l_error_index         NUMBER;
4872 l_ignore_text         VARCHAR2(240);
4873 l_dummy               NUMBER;
4874 l_offer_perf_tbl      offer_performance_tbl_type;
4875 l_currency_code       VARCHAR2(15);
4876 l_amount_ut_curr      NUMBER; -- amount in utilization currency (source budget currency)
4877 l_bill_to_site_id     NUMBER;
4878 
4879 l_currency_rec       currency_rec_type;
4880 l_currOrgId            NUMBER     := MO_GLOBAL.GET_CURRENT_ORG_ID();
4881 
4882 --Change the amount for multi currency - kpatro - 5/22/2009
4883 CURSOR csr_claim_line(cv_claim_id IN NUMBER) IS
4884   SELECT claim_line_id
4885        , activity_type
4886        , activity_id
4887        , item_type
4888        , item_id
4889       -- , acctd_amount
4890      , claim_currency_amount
4891   FROM ozf_claim_lines
4892   WHERE claim_id = cv_claim_id;
4893 
4894 CURSOR csr_uom_code(cv_item_id IN NUMBER) IS
4895   SELECT primary_uom_code
4896   FROM mtl_system_items
4897   WHERE inventory_item_id = cv_item_id
4898   AND organization_id = FND_PROFILE.VALUE('AMS_ITEM_ORGANIZATION_ID');
4899 
4900 CURSOR csr_offer_perf(cv_list_header_id IN NUMBER) IS
4901   SELECT 1
4902   FROM ozf_offer_performances
4903   WHERE list_header_id = cv_list_header_id;
4904 
4905 /*
4906 --Bug# 7319828 fixed by ateotia(+)
4907 l_batch_accepted_amount NUMBER;
4908 l_pass_acctd_amount     VARCHAR2(1) := 'F';
4909 
4910 
4911 CURSOR csr_batch_accepted_amount(
4912        p_resale_batch_id IN NUMBER,
4913        p_agreement_id IN NUMBER,
4914        p_inventory_item_id IN NUMBER
4915        ) IS
4916 SELECT
4917   sum(orl.total_accepted_amount)
4918 FROM
4919   ozf_resale_batches_all orb,
4920   ozf_resale_lines_int_all orl
4921 WHERE
4922   orb.batch_type = 'CHARGEBACK'
4923   AND orb.resale_batch_id = orl.resale_batch_id
4924   AND orl.resale_batch_id = p_resale_batch_id
4925   AND NVL(orl.corrected_agreement_id, orl.agreement_id) = p_agreement_id
4926   AND orl.inventory_item_id = p_inventory_item_id
4927   AND orl.agreement_type = 'PL'
4928   AND NVL(orl.tracing_flag, 'F') <> 'T'
4929   AND orl.status_code = 'PROCESSED';
4930 --Bug# 7319828 fixed by ateotia(-)
4931 */
4932 
4933 --Added for bug 7030415
4934 CURSOR c_get_conversion_type IS
4935   SELECT exchange_rate_type
4936   FROM   ozf_sys_parameters_all
4937   WHERE  org_id = l_currOrgId;
4938 
4939 CURSOR csr_function_currency IS
4940   SELECT gs.currency_code
4941   FROM gl_sets_of_books gs
4942   ,    ozf_sys_parameters org
4943   WHERE org.set_of_books_id = gs.set_of_books_id
4944   AND   org.org_id = l_currOrgId;
4945 l_exchange_rate_type      VARCHAR2(30) := FND_API.G_MISS_CHAR;
4946 l_rate                    NUMBER;
4947 
4948 BEGIN
4949   --------------------- initialize -----------------------
4950   SAVEPOINT Create_Claim_For_Accruals;
4951 
4952   IF OZF_DEBUG_HIGH_ON THEN
4953      OZF_Utility_PVT.debug_message(l_full_name||': start');
4954      OZF_Utility_PVT.debug_message('l_claim_rec.claim_number :  ' || l_claim_rec.claim_number);
4955   END IF;
4956 
4957   IF FND_API.to_boolean(p_init_msg_list) THEN
4958     FND_MSG_PUB.initialize;
4959   END IF;
4960 
4961   IF NOT FND_API.compatible_api_call(
4962               l_api_version,
4963               p_api_version,
4964               l_api_name,
4965               g_pkg_name
4966          )
4967   THEN
4968     RAISE FND_API.g_exc_unexpected_error;
4969   END IF;
4970 
4971   x_return_status := FND_API.g_ret_sts_success;
4972 
4973   -------------------- start -------------------
4974   l_funds_util_flt := p_funds_util_flt;
4975   l_total_acctd_amount_rem := 0;
4976 
4977   IF OZF_DEBUG_HIGH_ON THEN
4978      OZF_Utility_PVT.debug_message('Claim Currency Code in Create Claim For Accruals :' || l_claim_rec.currency_code);
4979   END IF;
4980 
4981   -- Added For Multi Currency - kpatro
4982   IF(l_claim_rec.currency_code IS NOT NULL) THEN
4983         l_currency_rec.claim_currency_code := l_claim_rec.currency_code;
4984   END IF;
4985 
4986   l_currency_rec.transaction_currency_code := l_funds_util_flt.utiz_currency_code;
4987 
4988   OPEN csr_function_currency;
4989   FETCH csr_function_currency INTO l_currency_rec.functional_currency_code;
4990   CLOSE csr_function_currency;
4991 
4992 
4993    IF l_currency_rec.claim_currency_code = l_currency_rec.transaction_currency_code THEN
4994       l_currency_rec.association_currency_code := l_currency_rec.transaction_currency_code;
4995    ELSE
4996      l_currency_rec.association_currency_code := l_currency_rec.functional_currency_code;
4997    END IF;
4998 
4999    --Set UNIVERSAL currency from profile.
5000   l_currency_rec.universal_currency_code := FND_PROFILE.VALUE('OZF_TP_COMMON_CURRENCY');
5001 
5002    IF OZF_DEBUG_HIGH_ON THEN
5003      OZF_Utility_PVT.debug_message('l_currency_rec.universal_currency_code :' || l_currency_rec.universal_currency_code);
5004      OZF_Utility_PVT.debug_message('l_currency_rec.claim_currency_code :' || l_currency_rec.claim_currency_code);
5005      OZF_Utility_PVT.debug_message('l_currency_rec.functional_currency_code :' || l_currency_rec.functional_currency_code);
5006      OZF_Utility_PVT.debug_message('l_currency_rec.transaction_currency_code :' || l_currency_rec.transaction_currency_code);
5007      OZF_Utility_PVT.debug_message('l_currency_rec.association_currency_code :' || l_currency_rec.association_currency_code);
5008   END IF;
5009 
5010   Get_Utiz_Sql_Stmt(
5011      p_api_version         => 1.0
5012     ,p_init_msg_list       => FND_API.g_false
5013     ,p_commit              => FND_API.g_false
5014     ,p_validation_level    => FND_API.g_valid_level_full
5015     ,x_return_status       => l_return_status
5016     ,x_msg_count           => x_msg_count
5017     ,x_msg_data            => x_msg_data
5018     ,p_summary_view        => 'AUTOPAY_LINE'
5019     ,p_funds_util_flt      => l_funds_util_flt
5020     ,px_currency_rec       => l_currency_rec
5021     ,p_cust_account_id     => l_funds_util_flt.cust_account_id
5022     ,x_utiz_sql_stmt       => l_stmt
5023   );
5024 
5025   IF l_return_status = FND_API.g_ret_sts_error THEN
5026     RAISE FND_API.g_exc_error;
5027   ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
5028     RAISE FND_API.g_exc_error;
5029   END IF;
5030 
5031   l_emp_csr := DBMS_SQL.open_cursor;
5032   FND_DSQL.set_cursor(l_emp_csr);
5033   DBMS_SQL.parse(l_emp_csr, l_stmt, DBMS_SQL.native);
5034   DBMS_SQL.define_column(l_emp_csr, 1, l_cust_account_id);
5035   DBMS_SQL.define_column(l_emp_csr, 2, l_plan_type, 30);
5036   DBMS_SQL.define_column(l_emp_csr, 3, l_plan_id);
5037   DBMS_SQL.define_column(l_emp_csr, 4, l_bill_to_site_id); --R12.1 enhancements
5038   DBMS_SQL.define_column(l_emp_csr, 5, l_product_level_type, 30);
5039   DBMS_SQL.define_column(l_emp_csr, 6, l_product_id);
5040   DBMS_SQL.define_column(l_emp_csr, 7, l_amount);
5041   DBMS_SQL.define_column(l_emp_csr, 8, l_currency_code, 15);
5042   FND_DSQL.do_binds;
5043 
5044   l_ignore := DBMS_SQL.execute(l_emp_csr);
5045   LOOP
5046     IF DBMS_SQL.fetch_rows(l_emp_csr) > 0 THEN
5047       DBMS_SQL.column_value(l_emp_csr, 1, l_cust_account_id);
5048       DBMS_SQL.column_value(l_emp_csr, 2, l_plan_type);
5049       DBMS_SQL.column_value(l_emp_csr, 3, l_plan_id);
5050       DBMS_SQL.column_value(l_emp_csr, 4, l_bill_to_site_id); --R12.1 enhancements
5051       DBMS_SQL.column_value(l_emp_csr, 5, l_product_level_type);
5052       DBMS_SQL.column_value(l_emp_csr, 6, l_product_id);
5053       DBMS_SQL.column_value(l_emp_csr, 7, l_amount);
5054       DBMS_SQL.column_value(l_emp_csr, 8, l_currency_code);
5055 
5056       IF l_currency_code <> l_claim_rec.currency_code THEN
5057         l_amount_ut_curr := l_amount;
5058 
5059         --Added for bug 7030415, get exchange_rate type
5060         OPEN c_get_conversion_type;
5061         FETCH c_get_conversion_type INTO l_exchange_rate_type;
5062         CLOSE c_get_conversion_type;
5063         --end
5064 /*
5065         --Bug# 7319828 fixed by ateotia(+)
5066         IF (p_funds_util_flt.reference_type = OZF_RESALE_COMMON_PVT.G_BATCH_REF_TYPE
5067         AND p_funds_util_flt.utilization_type = 'CHARGEBACK') THEN
5068            OPEN csr_batch_accepted_amount(p_funds_util_flt.reference_id, l_plan_id, l_product_id);
5069            FETCH csr_batch_accepted_amount INTO l_amount;
5070            CLOSE csr_batch_accepted_amount;
5071            l_pass_acctd_amount := 'T';
5072         ELSE
5073 */
5074         OZF_UTILITY_PVT.Convert_Currency(
5075              p_from_currency   => l_currency_code
5076             ,p_to_currency     => l_claim_rec.currency_code
5077             ,p_conv_type       => l_exchange_rate_type
5078             ,p_conv_date       => SYSDATE
5079             ,p_from_amount     => l_amount_ut_curr -- amount in utilization currency (func currency)
5080             ,x_return_status   => l_return_status
5081             ,x_to_amount       => l_amount -- amount in claim currency
5082             ,x_rate            => l_rate
5083         );
5084         IF l_return_status = FND_API.g_ret_sts_error THEN
5085           RAISE FND_API.g_exc_error;
5086         ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
5087           RAISE FND_API.g_exc_unexpected_error;
5088         END IF;
5089 /*
5090         --END IF;
5091         --Bug# 7319828 fixed by ateotia(-)
5092 */
5093       END IF;
5094 
5095       IF l_amount IS NOT NULL AND l_amount <> 0 THEN
5096         IF OZF_DEBUG_HIGH_ON THEN
5097           OZF_Utility_PVT.debug_message('---------------------------------');
5098           OZF_Utility_PVT.debug_message('Line ' || l_counter || ': Amount='||l_amount);
5099           OZF_Utility_PVT.debug_message('Plan Type         :  ' || l_plan_type);
5100           OZF_Utility_PVT.debug_message('Plan Id           :  ' || l_plan_id);
5101           OZF_Utility_PVT.debug_message('Product Level Type:  ' || l_product_level_type);
5102           OZF_Utility_PVT.debug_message('Product Id        :  ' || l_product_id);
5103           -- Added For Bug 8402328
5104           OZF_Utility_PVT.debug_message('l_funds_util_flt.utilization_id        :  ' || l_funds_util_flt.utilization_id);
5105         END IF;
5106         IF p_claim_rec.created_from = 'AUTOPAY' THEN
5107           FND_FILE.PUT_LINE(FND_FILE.LOG, '---------------------------------');
5108           FND_FILE.PUT_LINE(FND_FILE.LOG, 'Line ' || l_counter || ': Amount='||l_amount);
5109           FND_FILE.PUT_LINE(FND_FILE.LOG, 'Plan Type         :  ' || l_plan_type);
5110           FND_FILE.PUT_LINE(FND_FILE.LOG, 'Plan Id           :  ' || l_plan_id);
5111           FND_FILE.PUT_LINE(FND_FILE.LOG, 'Product Level Type:  ' || l_product_level_type);
5112           FND_FILE.PUT_LINE(FND_FILE.LOG, 'Product Id        :  ' || l_product_id);
5113         END IF;
5114 
5115         IF l_plan_type = 'OFFR' THEN
5116           Check_Offer_Performance(
5117              p_cust_account_id      => l_cust_account_id
5118             ,p_offer_id             => l_plan_id
5119             ,p_resale_flag          => 'F'
5120             ,p_check_all_flag       => 'F'
5121             ,x_performance_flag     => l_performance_flag
5122             ,x_offer_perf_tbl       => l_offer_perf_tbl
5123           );
5124         END IF;
5125 
5126          --Added Debug For Multi Currency - kpatro
5127          IF OZF_DEBUG_HIGH_ON THEN
5128             OZF_Utility_PVT.debug_message('l_bill_to_site_id         :  ' || l_bill_to_site_id);
5129             OZF_Utility_PVT.debug_message('l_funds_util_flt.bill_to_site_use_id           :  ' || l_funds_util_flt.bill_to_site_use_id);
5130             OZF_Utility_PVT.debug_message('l_funds_util_flt.utiz_currency_code           :  ' || l_funds_util_flt.utiz_currency_code);
5131             OZF_Utility_PVT.debug_message('l_currency_code           :  ' || l_currency_code);
5132          END IF;
5133         --R12.1 enhancements. Added condition so that earnings accrued only against
5134         --the respective bill_to_site_id should be added.
5135 
5136          IF OZF_DEBUG_HIGH_ON THEN
5137             OZF_Utility_PVT.debug_message('l_bill_to_site_id         :  ' || l_bill_to_site_id);
5138             OZF_Utility_PVT.debug_message('l_funds_util_flt.bill_to_site_use_id           :  ' || l_funds_util_flt.bill_to_site_use_id);
5139             OZF_Utility_PVT.debug_message('l_funds_util_flt.utiz_currency_code           :  ' || l_funds_util_flt.utiz_currency_code);
5140          END IF;
5141         -- The Claim Created From Autopay should be grouped based on Currency Code Also - kpatro
5142         IF l_performance_flag = FND_API.g_true THEN
5143            IF l_bill_to_site_id IS NOT NULL
5144            AND l_bill_to_site_id = l_funds_util_flt.bill_to_site_use_id
5145            AND l_currency_code = l_funds_util_flt.utiz_currency_code -- Added the check for Multi Currency - kpatro
5146            THEN --nirma
5147 
5148                   -- assume single currency for now; add multi-curr later
5149                   l_line_tbl(l_counter).claim_currency_amount     := l_amount;
5150                   l_line_tbl(l_counter).activity_type             := l_plan_type;
5151                   l_line_tbl(l_counter).activity_id               := l_plan_id;
5152                   l_line_tbl(l_counter).item_type                 := l_product_level_type;
5153                   l_line_tbl(l_counter).item_id                   := l_product_id;
5154                   l_line_tbl(l_counter).relationship_type         := l_funds_util_flt.relationship_type;
5155                   l_line_tbl(l_counter).related_cust_account_id   := l_funds_util_flt.related_cust_account_id;
5156                   l_line_tbl(l_counter).buy_group_party_id        := l_funds_util_flt.buy_group_party_id;
5157                   l_line_tbl(l_counter).select_cust_children_flag := l_funds_util_flt.select_cust_children_flag;
5158                   -- Added For Bug 8402328
5159                   l_line_tbl(l_counter).utilization_id := l_funds_util_flt.utilization_id;
5160 
5161                   IF l_product_level_type = 'PRODUCT' AND l_product_id IS NOT NULL THEN
5162                     OPEN csr_uom_code(l_line_tbl(l_counter).item_id);
5163                     FETCH csr_uom_code INTO l_line_tbl(l_counter).quantity_uom;
5164                     IF csr_uom_code%NOTFOUND THEN
5165                       CLOSE csr_uom_code;
5166                       IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
5167                         FND_MESSAGE.Set_Name('OZF', 'OZF_PRODUCT_UOM_MISSING');
5168                         FND_MESSAGE.Set_Token('ITEM_ID', l_line_tbl(l_counter).item_id);
5169                         FND_MSG_PUB.ADD;
5170                       END IF;
5171                       RAISE FND_API.g_exc_unexpected_error;
5172                     END IF;
5173                     CLOSE csr_uom_code;
5174                   END IF;
5175 /*
5176           --Bug# 7319828 fixed by ateotia(+)
5177           IF l_pass_acctd_amount = 'T' THEN
5178              l_line_tbl(l_counter).acctd_amount  := l_amount_ut_curr;
5179              l_claim_rec.acctd_amount            := NVL(l_claim_rec.acctd_amount,0) + l_amount_ut_curr;
5180           END IF;
5181           --Bug# 7319828 fixed by ateotia(-)
5182 */
5183 
5184                   l_total_acctd_amount_rem := l_total_acctd_amount_rem + l_amount;
5185 
5186                   IF l_plan_type = 'OFFR' AND l_plan_id IS NOT NULL THEN
5187                     OPEN csr_offer_perf(l_plan_id);
5188                     FETCH csr_offer_perf INTO l_dummy;
5189                     CLOSE csr_offer_perf;
5190 
5191                     IF l_dummy = 1 THEN
5192                       l_line_tbl(l_counter).performance_attached_flag := FND_API.G_TRUE;
5193                       l_line_tbl(l_counter).performance_complete_flag := FND_API.G_TRUE;
5194                     END IF;
5195                   END IF;
5196 
5197                   l_counter := l_counter + 1;
5198 
5199             -- Fix for Bug 8501176
5200             ELSIF l_funds_util_flt.bill_to_site_use_id IS NULL THEN
5201 
5202                   l_line_tbl(l_counter).claim_currency_amount     := l_amount;
5203                   l_line_tbl(l_counter).activity_type             := l_plan_type;
5204                   l_line_tbl(l_counter).activity_id               := l_plan_id;
5205                   l_line_tbl(l_counter).item_type                 := l_product_level_type;
5206                   l_line_tbl(l_counter).item_id                   := l_product_id;
5207                   l_line_tbl(l_counter).relationship_type         := l_funds_util_flt.relationship_type;
5208                   l_line_tbl(l_counter).related_cust_account_id   := l_funds_util_flt.related_cust_account_id;
5209                   l_line_tbl(l_counter).buy_group_party_id        := l_funds_util_flt.buy_group_party_id;
5210                   l_line_tbl(l_counter).select_cust_children_flag := l_funds_util_flt.select_cust_children_flag;
5211                   -- Added For Bug 8402328
5212                   l_line_tbl(l_counter).utilization_id := l_funds_util_flt.utilization_id;
5213 
5214                   IF l_product_level_type = 'PRODUCT' AND l_product_id IS NOT NULL THEN
5215                     OPEN csr_uom_code(l_line_tbl(l_counter).item_id);
5216                     FETCH csr_uom_code INTO l_line_tbl(l_counter).quantity_uom;
5217                     IF csr_uom_code%NOTFOUND THEN
5218                       CLOSE csr_uom_code;
5219                       IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
5220                         FND_MESSAGE.Set_Name('OZF', 'OZF_PRODUCT_UOM_MISSING');
5221                         FND_MESSAGE.Set_Token('ITEM_ID', l_line_tbl(l_counter).item_id);
5222                         FND_MSG_PUB.ADD;
5223                       END IF;
5224                       RAISE FND_API.g_exc_unexpected_error;
5225                     END IF;
5226                     CLOSE csr_uom_code;
5227                   END IF;
5228 
5229                   FND_FILE.PUT_LINE(FND_FILE.LOG, 'NP l_total_acctd_amount_rem        :  ' || l_total_acctd_amount_rem);
5230                   FND_FILE.PUT_LINE(FND_FILE.LOG, 'NP l_amount        :  ' || l_amount);
5231 
5232 
5233                   l_total_acctd_amount_rem := l_total_acctd_amount_rem + l_amount;
5234 
5235                   IF l_plan_type = 'OFFR' AND l_plan_id IS NOT NULL THEN
5236                     OPEN csr_offer_perf(l_plan_id);
5237                     FETCH csr_offer_perf INTO l_dummy;
5238                     CLOSE csr_offer_perf;
5239 
5240                     IF l_dummy = 1 THEN
5241                       l_line_tbl(l_counter).performance_attached_flag := FND_API.G_TRUE;
5242                       l_line_tbl(l_counter).performance_complete_flag := FND_API.G_TRUE;
5243                     END IF;
5244                   END IF;
5245 
5246                   l_counter := l_counter + 1;
5247 
5248           END IF;--nirma
5249         ELSE
5250           IF OZF_DEBUG_HIGH_ON THEN
5251             OZF_Utility_PVT.debug_message('Performance requirements not met.');
5252           END IF;
5253           IF p_claim_rec.created_from = 'AUTOPAY' THEN
5254             FND_FILE.PUT_LINE(FND_FILE.LOG, 'Performance requirements not met.');
5255           END IF;
5256         END IF;
5257       END IF;
5258     ELSE
5259       EXIT;
5260     END IF;
5261   END LOOP;
5262   DBMS_SQL.close_cursor(l_emp_csr);
5263 
5264   IF OZF_DEBUG_HIGH_ON THEN
5265      OZF_Utility_PVT.debug_message('l_total_acctd_amount_rem:  ' || l_total_acctd_amount_rem);
5266   END IF;
5267     -- Flow initiated from promotional payment
5268     IF (l_claim_rec.created_from = 'PROMO_CLAIM') THEN
5269         l_claim_rec.amount := l_total_acctd_amount_rem;
5270     END IF;
5271 
5272      --ninarasi fix for bug 12942827 12-Sep-2011
5273   IF l_total_acctd_amount_rem = 0 THEN
5274      x_claim_id := -1;
5275      RETURN;
5276   END IF;
5277 
5278    -- Fix for Bug 8501176
5279   IF l_total_acctd_amount_rem <> 0 THEN
5280     l_claim_rec.amount := l_total_acctd_amount_rem;
5281 
5282     -- create claim in OPEN status, ignoring the status passed in
5283     l_claim_rec.status_code    := 'OPEN';
5284     l_claim_rec.user_status_id := to_number(ozf_utility_pvt.get_default_user_status(
5285               p_status_type   => 'OZF_CLAIM_STATUS',
5286               p_status_code   => l_claim_rec.status_code));
5287 
5288     OZF_CLAIM_PVT.Create_Claim(
5289        p_api_version            => l_api_version
5290       ,x_return_status          => l_return_status
5291       ,x_msg_data               => x_msg_data
5292       ,x_msg_count              => x_msg_count
5293       ,p_claim                  => l_claim_rec
5294       ,x_claim_id               => l_claim_id
5295     );
5296     IF l_return_status = FND_API.G_RET_STS_ERROR THEN
5297       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5298     ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5299       RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5300     END IF;
5301 
5302     IF OZF_DEBUG_HIGH_ON THEN
5303       OZF_Utility_PVT.debug_message('Claim created id: ' || l_claim_id);
5304     END IF;
5305 
5306  IF l_line_tbl.count > 0 THEN --ninarasi
5307     FOR i IN l_line_tbl.FIRST..l_line_tbl.LAST LOOP
5308       IF l_line_tbl.exists(i) IS NOT NULL THEN
5309         l_line_tbl(i).claim_id := l_claim_id;
5310       END IF;
5311     END LOOP;
5312  END IF;
5313 
5314     OZF_CLAIM_LINE_PVT.Create_Claim_Line_Tbl(
5315        p_api_version       => 1.0
5316       ,p_init_msg_list     => FND_API.g_false
5317       ,p_commit            => FND_API.g_false
5318       ,p_validation_level  => FND_API.g_valid_level_full
5319       ,x_return_status     => l_return_status
5320       ,x_msg_count         => x_msg_count
5321       ,x_msg_data          => x_msg_data
5322       ,p_claim_line_tbl    => l_line_tbl
5323       ,x_error_index       => l_error_index
5324     );
5325     IF l_return_status = FND_API.g_ret_sts_error THEN
5326       RAISE FND_API.g_exc_error;
5327     ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
5328       RAISE FND_API.g_exc_error;
5329     END IF;
5330 
5331     IF OZF_DEBUG_HIGH_ON THEN
5332       OZF_Utility_PVT.debug_message('Claim lines created for claim_id=' || l_claim_id);
5333     END IF;
5334 
5335     OPEN csr_claim_line(l_claim_id);
5336     LOOP
5337       FETCH csr_claim_line INTO l_funds_util_flt.claim_line_id
5338                               , l_funds_util_flt.activity_type
5339                               , l_funds_util_flt.activity_id
5340                               , l_funds_util_flt.product_level_type
5341                               , l_funds_util_flt.product_id
5342                               , l_funds_util_flt.total_amount;
5343       EXIT WHEN csr_claim_line%NOTFOUND;
5344 
5345       Update_Group_Line_Util(
5346          p_api_version         => 1.0
5347         ,p_init_msg_list       => FND_API.g_false
5348         ,p_commit              => FND_API.g_false
5349         ,p_validation_level    => FND_API.G_VALID_LEVEL_FULL
5350         ,x_return_status       => l_return_status
5351         ,x_msg_count           => x_msg_count
5352         ,x_msg_data            => x_msg_data
5353         ,p_summary_view        => 'ACTIVITY'
5354         ,p_funds_util_flt      => l_funds_util_flt
5355       );
5356       IF l_return_status = FND_API.g_ret_sts_error THEN
5357         RAISE FND_API.g_exc_error;
5358       ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
5359         RAISE FND_API.g_exc_error;
5360       END IF;
5361     END LOOP;
5362     CLOSE csr_claim_line;
5363   ELSIF l_claim_rec.created_from = 'PROMO_CLAIM' and l_claim_rec.amount <> 0 THEN
5364     IF OZF_DEBUG_HIGH_ON THEN
5365       OZF_Utility_PVT.debug_message('Created Form: ' || l_claim_rec.created_from);
5366       OZF_Utility_PVT.debug_message('Amount: ' || l_claim_rec.amount);
5367    END IF;
5368 
5369    IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
5370       FND_MESSAGE.Set_Name('OZF', 'OZF_ACCRUAL_SCAN_DATA_ERROR');
5371       FND_MSG_PUB.ADD;
5372     END IF;
5373 
5374     RAISE FND_API.g_exc_unexpected_error;
5375   ELSIF l_claim_rec.created_from <> 'PROMO_CLAIM' THEN
5376     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
5377       FND_MESSAGE.Set_Name('OZF', 'OZF_ACCRUAL_REM_AMOUNT_LT_ZERO');
5378       FND_MSG_PUB.ADD;
5379     END IF;
5380     RAISE FND_API.g_exc_unexpected_error;
5381 
5382   END IF;
5383 
5384   x_claim_id := l_claim_id;
5385 
5386   ------------------------- finish -------------------------------
5387   -- Check for commit
5388   IF FND_API.to_boolean(p_commit) THEN
5389     COMMIT;
5390   END IF;
5391 
5392   FND_MSG_PUB.count_and_get(
5393          p_encoded => FND_API.g_false,
5394          p_count   => x_msg_count,
5395          p_data    => x_msg_data
5396   );
5397 
5398   IF OZF_DEBUG_HIGH_ON THEN
5399      OZF_Utility_PVT.debug_message(l_full_name ||': end');
5400   END IF;
5401 
5402 EXCEPTION
5403   WHEN FND_API.g_exc_error THEN
5404     ROLLBACK TO Create_Claim_For_Accruals;
5405     x_return_status := FND_API.g_ret_sts_error;
5406     FND_MSG_PUB.count_and_get (
5407            p_encoded => FND_API.g_false
5408           ,p_count   => x_msg_count
5409           ,p_data    => x_msg_data
5410     );
5411 
5412   WHEN FND_API.g_exc_unexpected_error THEN
5413     ROLLBACK TO Create_Claim_For_Accruals;
5414     x_return_status := FND_API.g_ret_sts_unexp_error ;
5415     FND_MSG_PUB.count_and_get (
5416            p_encoded => FND_API.g_false
5417           ,p_count   => x_msg_count
5418           ,p_data    => x_msg_data
5419     );
5420 
5421   WHEN OTHERS THEN
5422     ROLLBACK TO Create_Claim_For_Accruals;
5423     x_return_status := FND_API.g_ret_sts_unexp_error ;
5424     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
5425       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
5426     END IF;
5427     FND_MSG_PUB.count_and_get(
5428            p_encoded => FND_API.g_false
5429           ,p_count   => x_msg_count
5430           ,p_data    => x_msg_data
5431     );
5432 
5433 END Create_Claim_For_Accruals;
5434 
5435 -------------------------------------------------------------------------------
5436 -- PROCEDURE
5437 --    Create_Claim_Existing_Accruals
5438 --
5439 -- PURPOSE
5440 --    This procedure creates a claim, associates the existing earnings with
5441 --    claim lines.
5442 --
5443 -- PARAMETERS
5444 --    p_claim_rec: claim record
5445 --
5446 -- NOTES
5447 --
5448 -- HISTORY
5449 --    09-Jul-2009  anuj & muthsubr   Created.
5450 --    Bug# 8632964 fixed.
5451 -------------------------------------------------------------------------------
5452 PROCEDURE Create_Claim_Existing_Accruals(
5453    p_api_version      IN  NUMBER
5454   ,p_init_msg_list    IN  VARCHAR2 := FND_API.g_false
5455   ,p_commit           IN  VARCHAR2 := FND_API.g_false
5456   ,p_validation_level IN  NUMBER   := FND_API.g_valid_level_full
5457   ,x_return_status    OUT NOCOPY VARCHAR2
5458   ,x_msg_count        OUT NOCOPY NUMBER
5459   ,x_msg_data         OUT NOCOPY VARCHAR2
5460   ,p_claim_rec        IN  ozf_claim_pvt.claim_rec_type
5461   ,p_funds_util_flt   IN  ozf_claim_accrual_pvt.funds_util_flt_type
5462   ,x_claim_id         OUT NOCOPY NUMBER
5463 )
5464 IS
5465 l_api_version CONSTANT   NUMBER       := 1.0;
5466 l_api_name    CONSTANT   VARCHAR2(30) := 'Create_Claim_Existing_Accruals';
5467 l_full_name   CONSTANT   VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
5468 l_return_status          VARCHAR2(1);
5469 l_funds_util_flt         OZF_CLAIM_ACCRUAL_PVT.funds_util_flt_type;
5470 l_line_tbl               OZF_CLAIM_LINE_PVT.claim_line_tbl_type;
5471 l_claim_rec              OZF_CLAIM_PVT.claim_rec_type := p_claim_rec;
5472 l_claim_id               NUMBER;
5473 l_cust_account_id        NUMBER;
5474 l_plan_type              VARCHAR2(30);
5475 l_plan_id                NUMBER;
5476 l_product_level_type     VARCHAR2(30);
5477 l_product_id             NUMBER;
5478 l_amount                 NUMBER;
5479 l_total_acctd_amount_rem NUMBER;
5480 l_performance_flag       VARCHAR2(1) := FND_API.g_true;
5481 l_emp_csr                NUMBER;
5482 l_counter                PLS_INTEGER := 1;
5483 l_error_index            NUMBER;
5484 l_dummy                  NUMBER;
5485 l_offer_perf_tbl         offer_performance_tbl_type;
5486 l_currency_code          VARCHAR2(15);
5487 
5488 l_batch_settlement_flag  VARCHAR2(1) := 'F';
5489 l_batch_product_id       NUMBER;
5490 l_batch_product_amount   NUMBER;
5491 l_batch_fund_id          NUMBER;
5492 l_batch_currency_code    VARCHAR2(15);
5493 l_accrual_amount         NUMBER;
5494 l_amount_utilized        NUMBER := 0;
5495 l_total_amount_utilized  NUMBER := 0;
5496 l_resale_line_int_id     NUMBER;
5497 error_no_rollback        EXCEPTION;
5498 
5499 
5500 CURSOR csr_claim_line(cv_claim_id IN NUMBER) IS
5501   SELECT claim_line_id
5502        , activity_type
5503        , activity_id
5504        , item_type
5505        , item_id
5506        , acctd_amount
5507   FROM ozf_claim_lines
5508   WHERE claim_id = cv_claim_id;
5509 
5510 CURSOR csr_uom_code(cv_item_id IN NUMBER) IS
5511   SELECT primary_uom_code
5512   FROM mtl_system_items
5513   WHERE inventory_item_id = cv_item_id
5514   AND organization_id = FND_PROFILE.VALUE('AMS_ITEM_ORGANIZATION_ID');
5515 
5516 CURSOR csr_offer_perf(cv_list_header_id IN NUMBER) IS
5517   SELECT 1
5518   FROM ozf_offer_performances
5519   WHERE list_header_id = cv_list_header_id;
5520 
5521 l_pass_acctd_amount     VARCHAR2(1) := 'F';
5522 
5523 CURSOR csr_batch_lines(p_resale_batch_id IN NUMBER)
5524 IS
5525 SELECT
5526   SUM(orl.total_accepted_amount) total_amount,
5527   orl.inventory_item_id product_id,
5528   orb.currency_code currency_code
5529 FROM
5530   ozf_resale_batches_all orb,
5531   ozf_resale_lines_int_all orl
5532 WHERE
5533   orb.batch_type = 'CHARGEBACK'
5534   AND orb.resale_batch_id = orl.resale_batch_id
5535   AND orb.resale_batch_id = p_resale_batch_id
5536   AND NVL(orl.tracing_flag, 'F') <> 'T'
5537   AND orl.status_code = 'PROCESSED'
5538 GROUP BY orl.inventory_item_id, orb.currency_code;
5539 
5540 CURSOR csr_resale_line(p_resale_batch_id IN NUMBER)
5541 IS
5542 SELECT
5543   orl.resale_line_int_id
5544 FROM
5545   ozf_resale_batches_all orb,
5546   ozf_resale_lines_int_all orl
5547 WHERE
5548   orb.batch_type = 'CHARGEBACK'
5549   AND orb.resale_batch_id = orl.resale_batch_id
5550   AND orb.resale_batch_id = p_resale_batch_id
5551   AND NVL(orl.tracing_flag, 'F') <> 'T'
5552   AND orl.status_code = 'PROCESSED';
5553 
5554 CURSOR csr_get_utils(p_cust_account_id IN NUMBER, p_fund_id IN VARCHAR2, p_product_id IN NUMBER)
5555 IS
5556 SELECT fu.cust_account_id cust_account_id, fu.plan_type plan_type, fu.plan_id plan_id,
5557 fu.product_id product_id, sum(fu.acctd_amount_remaining) amount, fu.currency_code currency_code
5558 FROM ozf_funds_utilized_all_b fu
5559 WHERE fu.org_id = MO_GLOBAL.GET_CURRENT_ORG_ID()
5560 AND fu.acctd_amount_remaining <> 0
5561 AND fu.utilization_type IN ('ACCRUAL', 'ADJUSTMENT')
5562 AND fu.cust_account_id = p_cust_account_id
5563 AND fu.fund_id = p_fund_id
5564 AND fu.product_level_type = 'PRODUCT'
5565 AND fu.product_id = p_product_id
5566 AND fu.gl_posted_flag = 'Y'
5567 AND fu.plan_type = 'OFFR'
5568 GROUP BY cust_account_id, plan_type, plan_id, product_id, currency_code;
5569 
5570 
5571 BEGIN
5572   --------------------- initialize -----------------------
5573   SAVEPOINT Create_Claim_Existing_Accruals;
5574 
5575   IF OZF_DEBUG_HIGH_ON THEN
5576      OZF_Utility_PVT.debug_message(l_full_name||': start');
5577   END IF;
5578 
5579   IF FND_API.to_boolean(p_init_msg_list) THEN
5580     FND_MSG_PUB.initialize;
5581   END IF;
5582 
5583   IF NOT FND_API.compatible_api_call(
5584               l_api_version,
5585               p_api_version,
5586               l_api_name,
5587               g_pkg_name
5588          )
5589   THEN
5590     RAISE FND_API.g_exc_unexpected_error;
5591   END IF;
5592 
5593   x_return_status := FND_API.g_ret_sts_success;
5594 
5595   -------------------- start -------------------
5596   l_funds_util_flt := p_funds_util_flt;
5597   l_total_acctd_amount_rem := 0;
5598   l_product_level_type := 'PRODUCT';
5599 
5600   -- Fetching Product (product_id) and its correponding amount (l_product_amount) for the batch.
5601   FOR batch_lines_rec IN csr_batch_lines(l_claim_rec.source_object_id)
5602   LOOP
5603 
5604      l_batch_product_id := batch_lines_rec.product_id;
5605      l_batch_product_amount := batch_lines_rec.total_amount;
5606      l_batch_fund_id := l_funds_util_flt.fund_id;
5607      l_batch_currency_code := batch_lines_rec.currency_code;
5608 
5609      IF OZF_DEBUG_HIGH_ON THEN
5610         OZF_Utility_PVT.debug_message('Customer Account Id      : ' || l_funds_util_flt.cust_account_id);
5611         OZF_UTILITY_PVT.debug_message('Fund_id                  : ' || l_batch_fund_id);
5612         OZF_UTILITY_PVT.debug_message('Product Amount           : ' || l_batch_product_amount);
5613         OZF_UTILITY_PVT.debug_message('Product id               : ' || l_batch_product_id);
5614      END IF;
5615 
5616          -- Fetching cust_account_id, plan_type, plan_id, product_id, amount for the utilization based on the batch's cust_account_id, Fund_id, Product_id.
5617          FOR get_utils_rec IN csr_get_utils(l_funds_util_flt.cust_account_id, l_batch_fund_id, l_batch_product_id)
5618          LOOP
5619 
5620            l_cust_account_id := get_utils_rec.cust_account_id;
5621            l_plan_type := get_utils_rec.plan_type;
5622            l_plan_id := get_utils_rec.plan_id;
5623            l_product_id := get_utils_rec.product_id;
5624            l_accrual_amount := get_utils_rec.amount;
5625            l_currency_code := get_utils_rec.currency_code;
5626 
5627            IF OZF_DEBUG_HIGH_ON THEN
5628                 OZF_Utility_PVT.debug_message('get_utils_rec.cust_account_id    : ' || l_cust_account_id);
5629                 OZF_UTILITY_PVT.debug_message('get_utils_rec.plan_type          : ' || l_plan_type);
5630                 OZF_UTILITY_PVT.debug_message('get_utils_rec.plan_id            : ' || l_plan_id);
5631                 OZF_UTILITY_PVT.debug_message('get_utils_rec.product_id         : ' || l_product_id);
5632                 OZF_UTILITY_PVT.debug_message('get_utils_rec.amount             : ' || l_accrual_amount);
5633                 OZF_UTILITY_PVT.debug_message('get_utils_rec.currency_code      : ' || l_currency_code);
5634            END IF;
5635 
5636            -- Calling Currency_conversion for converting accrual to batch currency
5637 
5638            IF l_currency_code <> l_batch_currency_code THEN
5639               OZF_UTILITY_PVT.Convert_Currency(
5640                  p_from_currency   => l_currency_code
5641                 ,p_to_currency     => l_batch_currency_code
5642                 ,p_conv_date       => SYSDATE
5643                 ,p_from_amount     => l_accrual_amount                  -- accrual amount
5644                 ,x_return_status   => l_return_status
5645                 ,x_to_amount       => l_amount                          -- accrual amount in batch currency
5646               );
5647               IF l_return_status = FND_API.g_ret_sts_error THEN
5648                  RAISE FND_API.g_exc_error;
5649               ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
5650                  RAISE FND_API.g_exc_unexpected_error;
5651               END IF;
5652            ELSE
5653              l_amount := l_accrual_amount;                              -- accrual amount in batch currency
5654            END IF;
5655 
5656            IF l_amount IS NOT NULL AND l_amount <> 0 THEN
5657 
5658               IF OZF_DEBUG_HIGH_ON THEN
5659                  OZF_Utility_PVT.debug_message('-------------Before using Accruals Logic------------');
5660                  OZF_Utility_PVT.debug_message('Line No ' || l_counter);
5661                  OZF_Utility_PVT.debug_message('l_cust_account_id = ' || l_cust_account_id);
5662                  OZF_Utility_PVT.debug_message('l_plan_type = ' || l_plan_type);
5663                  OZF_Utility_PVT.debug_message('l_plan_id = ' || l_plan_id);
5664                  OZF_Utility_PVT.debug_message('l_product_id = ' || l_product_id);
5665                  OZF_Utility_PVT.debug_message('l_amount = ' || l_amount);
5666                  OZF_Utility_PVT.debug_message('l_currency_code = ' || l_currency_code);
5667                  OZF_Utility_PVT.debug_message('----------------------------------------------------');
5668               END IF;
5669 
5670               -- USE l_amount from Accruals and continue with the ITERATION
5671 
5672               IF l_batch_product_amount >= l_amount THEN
5673                  l_batch_product_amount := l_batch_product_amount - l_amount;
5674                  l_amount_utilized := l_amount;
5675 
5676               ELSE
5677                  l_amount_utilized := l_batch_product_amount;
5678                  l_batch_product_amount := 0;
5679               END IF;
5680 
5681               IF OZF_DEBUG_HIGH_ON THEN
5682                  OZF_Utility_PVT.debug_message('-------------After using Accruals ------------');
5683                  OZF_Utility_PVT.debug_message('l_batch_product_amount = ' || l_batch_product_amount);
5684                  OZF_Utility_PVT.debug_message('l_amount_utilized = ' || l_amount_utilized);
5685                  OZF_Utility_PVT.debug_message('l_amount = ' || l_amount);
5686                  OZF_Utility_PVT.debug_message('----------------------------------------------');
5687               END IF;
5688 
5689               -- Processing utilization amount logic goes here
5690               -- We are taking l_amount_utilized for Processing of l_line_tbl(l_counter).XXXX.
5691 
5692                  l_line_tbl(l_counter).claim_currency_amount     := l_amount_utilized;
5693                  l_line_tbl(l_counter).activity_type             := l_plan_type;
5694                  l_line_tbl(l_counter).activity_id               := l_plan_id;
5695                  l_line_tbl(l_counter).item_type                 := l_product_level_type;
5696                  l_line_tbl(l_counter).item_id                   := l_product_id;
5697 
5698                  IF l_product_level_type = 'PRODUCT' AND l_product_id IS NOT NULL THEN
5699                     OPEN csr_uom_code(l_line_tbl(l_counter).item_id);
5700                     FETCH csr_uom_code INTO l_line_tbl(l_counter).quantity_uom;
5701                     IF csr_uom_code%NOTFOUND THEN
5702                        CLOSE csr_uom_code;
5703                        IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
5704                           FND_MESSAGE.Set_Name('OZF', 'OZF_PRODUCT_UOM_MISSING');
5705                           FND_MESSAGE.Set_Token('ITEM_ID', l_line_tbl(l_counter).item_id);
5706                           FND_MSG_PUB.ADD;
5707                        END IF;
5708                        RAISE FND_API.g_exc_unexpected_error;
5709                     END IF;
5710                     CLOSE csr_uom_code;
5711                  END IF;
5712 
5713                  IF l_plan_type = 'OFFR' AND l_plan_id IS NOT NULL THEN
5714                     OPEN csr_offer_perf(l_plan_id);
5715                     FETCH csr_offer_perf INTO l_dummy;
5716                     CLOSE csr_offer_perf;
5717 
5718                     IF l_dummy = 1 THEN
5719                        l_line_tbl(l_counter).performance_attached_flag := FND_API.G_TRUE;
5720                        l_line_tbl(l_counter).performance_complete_flag := FND_API.G_TRUE;
5721                     END IF;
5722                  END IF;
5723 
5724                  l_counter := l_counter + 1;
5725                  l_total_amount_utilized := l_total_amount_utilized + l_amount_utilized;
5726 
5727               ELSE
5728                  IF OZF_DEBUG_HIGH_ON THEN
5729                     OZF_Utility_PVT.debug_message('Performance requirements not met.');
5730                  END IF;
5731                  IF p_claim_rec.created_from = 'AUTOPAY' THEN
5732                     FND_FILE.PUT_LINE(FND_FILE.LOG, 'Performance requirements not met.');
5733                  END IF;
5734 
5735            END IF;      -- End of l_amount <> 0
5736 
5737 
5738            -- Breaking the loop, since l_batch_product_amount of product is successfully utilized.
5739            -- else continue with the utilization loop until l_batch_product_amount becomes 0.
5740 
5741            EXIT WHEN l_batch_product_amount = 0;
5742 
5743         END LOOP;               -- Ending loop of utilization
5744 
5745 
5746      -- suppose after utilizing all the accruals and still there is some batch_amount to be settled
5747      -- i.e., l_batch_product_amount > 0, set the flag as 'f' and dispute the batch by exiting the loops.
5748 
5749      IF l_batch_product_amount > 0 THEN
5750         l_batch_settlement_flag := 'F';
5751         EXIT;                   -- If l_batch_settlement_flag = 'f' break the batch loop
5752      ELSE
5753         l_batch_settlement_flag := 'T';
5754      END IF;
5755 
5756   END LOOP;             -- Ending loop of batch
5757 
5758      IF OZF_DEBUG_HIGH_ON THEN
5759         OZF_Utility_PVT.debug_message('-------------After Ending batch loop ------------');
5760         OZF_Utility_PVT.debug_message('l_batch_product_amount = ' || l_batch_product_amount);
5761         OZF_Utility_PVT.debug_message('l_batch_settlement_flag = ' || l_batch_settlement_flag);
5762         OZF_Utility_PVT.debug_message('l_total_amount_utilized = ' || l_total_amount_utilized);
5763         OZF_Utility_PVT.debug_message('-------------------------------------------------');
5764      END IF;
5765 
5766     -- If flag = f then dispute the batch and batch lines
5767     IF l_batch_settlement_flag = 'F' THEN
5768         IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
5769         FND_MESSAGE.Set_Name('OZF', 'OZF_CHBK_NO_FUNDS');
5770         FND_MSG_PUB.ADD;
5771         END IF;
5772         RAISE FND_API.g_exc_unexpected_error;
5773     END IF;
5774 
5775    --ninarasi fix for bug 12942827 12-Sep-2011
5776   IF l_total_amount_utilized = 0 THEN
5777      x_claim_id := -1;
5778      RETURN;
5779   END IF;
5780 
5781 
5782   IF l_total_amount_utilized <> 0 THEN
5783      l_claim_rec.amount := l_total_amount_utilized;
5784      IF l_total_amount_utilized < 0 THEN
5785         l_claim_rec.payment_method := 'DEBIT_MEMO';
5786      END IF;
5787 
5788      -- create claim in OPEN status, ignoring the status passed in
5789      l_claim_rec.status_code    := 'OPEN';
5790      l_claim_rec.user_status_id := to_number(ozf_utility_pvt.get_default_user_status(
5791                                                 p_status_type   => 'OZF_CLAIM_STATUS',
5792                                                 p_status_code   => l_claim_rec.status_code));
5793 
5794      l_claim_rec.source_object_class := NULL;
5795      l_claim_rec.source_object_id := NULL;
5796      l_claim_rec.source_object_number := NULL;
5797 
5798      OZF_CLAIM_PVT.Create_Claim(
5799         p_api_version            => l_api_version
5800        ,x_return_status          => l_return_status
5801        ,x_msg_data               => x_msg_data
5802        ,x_msg_count              => x_msg_count
5803        ,p_claim                  => l_claim_rec
5804        ,x_claim_id               => l_claim_id
5805      );
5806      IF l_return_status = FND_API.G_RET_STS_ERROR THEN
5807         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5808      ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
5809         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
5810      END IF;
5811 
5812      IF OZF_DEBUG_HIGH_ON THEN
5813         OZF_Utility_PVT.debug_message('Claim created id: ' || l_claim_id);
5814      END IF;
5815 
5816      FOR i IN l_line_tbl.FIRST..l_line_tbl.LAST LOOP
5817         IF l_line_tbl.exists(i) IS NOT NULL THEN
5818            l_line_tbl(i).claim_id := l_claim_id;
5819         END IF;
5820      END LOOP;
5821 
5822      OZF_CLAIM_LINE_PVT.Create_Claim_Line_Tbl(
5823         p_api_version       => 1.0
5824        ,p_init_msg_list     => FND_API.g_false
5825        ,p_commit            => FND_API.g_false
5826        ,p_validation_level  => FND_API.g_valid_level_full
5827        ,x_return_status     => l_return_status
5828        ,x_msg_count         => x_msg_count
5829        ,x_msg_data          => x_msg_data
5830        ,p_claim_line_tbl    => l_line_tbl
5831        ,x_error_index       => l_error_index
5832      );
5833 
5834      IF l_return_status = FND_API.g_ret_sts_error THEN
5835         RAISE FND_API.g_exc_error;
5836      ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
5837         RAISE FND_API.g_exc_error;
5838      END IF;
5839 
5840      IF OZF_DEBUG_HIGH_ON THEN
5841         OZF_Utility_PVT.debug_message('Claim lines created for claim_id=' || l_claim_id);
5842      END IF;
5843 
5844      OPEN csr_claim_line(l_claim_id);
5845      LOOP
5846         FETCH csr_claim_line INTO l_funds_util_flt.claim_line_id
5847                                 , l_funds_util_flt.activity_type
5848                                 , l_funds_util_flt.activity_id
5849                                 , l_funds_util_flt.product_level_type
5850                                 , l_funds_util_flt.product_id
5851                                 , l_funds_util_flt.total_amount;
5852         EXIT WHEN csr_claim_line%NOTFOUND;
5853 
5854         Update_Group_Line_Util(
5855            p_api_version         => 1.0
5856           ,p_init_msg_list       => FND_API.g_false
5857           ,p_commit              => FND_API.g_false
5858           ,p_validation_level    => FND_API.G_VALID_LEVEL_FULL
5859           ,x_return_status       => l_return_status
5860           ,x_msg_count           => x_msg_count
5861           ,x_msg_data            => x_msg_data
5862           ,p_summary_view        => 'ACTIVITY'
5863           ,p_funds_util_flt      => l_funds_util_flt
5864         );
5865         IF l_return_status = FND_API.g_ret_sts_error THEN
5866            RAISE FND_API.g_exc_error;
5867         ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
5868            RAISE FND_API.g_exc_error;
5869         END IF;
5870      END LOOP;
5871      CLOSE csr_claim_line;
5872 
5873   END IF;  -- end of l_amount_utilized <> 0
5874 
5875   x_claim_id := l_claim_id;
5876 
5877   ------------------------- finish -------------------------------
5878   -- Check for commit
5879   IF FND_API.to_boolean(p_commit) THEN
5880     COMMIT;
5881   END IF;
5882 
5883   FND_MSG_PUB.count_and_get(
5884          p_encoded => FND_API.g_false,
5885          p_count   => x_msg_count,
5886          p_data    => x_msg_data
5887   );
5888 
5889   IF OZF_DEBUG_HIGH_ON THEN
5890      OZF_Utility_PVT.debug_message(l_full_name ||': end');
5891   END IF;
5892 
5893 EXCEPTION
5894   WHEN FND_API.g_exc_error THEN
5895     ROLLBACK TO Create_Claim_Existing_Accruals;
5896     x_return_status := FND_API.g_ret_sts_error;
5897     FND_MSG_PUB.count_and_get (
5898            p_encoded => FND_API.g_false
5899           ,p_count   => x_msg_count
5900           ,p_data    => x_msg_data
5901     );
5902 
5903   WHEN FND_API.g_exc_unexpected_error THEN
5904     ROLLBACK TO Create_Claim_Existing_Accruals;
5905     x_return_status := FND_API.g_ret_sts_unexp_error ;
5906     FND_MSG_PUB.count_and_get (
5907            p_encoded => FND_API.g_false
5908           ,p_count   => x_msg_count
5909           ,p_data    => x_msg_data
5910     );
5911 
5912   WHEN OTHERS THEN
5913     ROLLBACK TO Create_Claim_Existing_Accruals;
5914     x_return_status := FND_API.g_ret_sts_unexp_error ;
5915     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
5916       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
5917     END IF;
5918     FND_MSG_PUB.count_and_get(
5919            p_encoded => FND_API.g_false
5920           ,p_count   => x_msg_count
5921           ,p_data    => x_msg_data
5922     );
5923 
5924 END Create_Claim_Existing_Accruals;
5925 
5926 ---------------------------------------------------------------------
5927 -- PROCEDURE
5928 --    Pay_Claim_For_Accruals
5929 --
5930 -- PURPOSE
5931 --    Create a claim, associate earnings based on search filters, and
5932 --    close the claim
5933 --
5934 -- PARAMETERS
5935 --    p_claim_rec: claim record
5936 --    p_funds_util_flt: search filter for earnings
5937 --
5938 -- NOTES
5939 ---------------------------------------------------------------------
5940 PROCEDURE Pay_Claim_For_Accruals(
5941    p_api_version         IN  NUMBER
5942   ,p_init_msg_list       IN  VARCHAR2  := FND_API.g_false
5943   ,p_commit              IN  VARCHAR2  := FND_API.g_false
5944   ,p_validation_level    IN  NUMBER    := FND_API.g_valid_level_full
5945   ,p_accrual_flag        IN  VARCHAR2
5946 
5947   ,x_return_status       OUT NOCOPY VARCHAR2
5948   ,x_msg_count           OUT NOCOPY NUMBER
5949   ,x_msg_data            OUT NOCOPY VARCHAR2
5950 
5951   ,p_claim_rec           IN  ozf_claim_pvt.claim_rec_type
5952   ,p_funds_util_flt      IN  ozf_claim_accrual_pvt.funds_util_flt_type
5953 
5954   ,x_claim_id            OUT NOCOPY NUMBER
5955 )
5956 IS
5957 l_api_version CONSTANT NUMBER       := 1.0;
5958 l_api_name    CONSTANT VARCHAR2(30) := 'Pay_Claim_For_Accruals';
5959 l_full_name   CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
5960 l_return_status VARCHAR2(1);
5961 
5962 l_funds_util_flt     funds_util_flt_type;
5963 l_claim_rec          OZF_CLAIM_PVT.claim_rec_type;
5964 l_claim_id           NUMBER;
5965 l_cust_trade_profile g_cust_trade_profile_csr%rowtype;
5966 l_party_name         VARCHAR2(360);
5967 l_close_claim_flag   VARCHAR2(1);
5968 l_accrual_flag       VARCHAR2(1):= p_accrual_flag;
5969 
5970 CURSOR csr_party_name(cv_cust_account_id IN NUMBER) IS
5971   SELECT p.party_name
5972   FROM hz_parties p, hz_cust_accounts c
5973   WHERE p.party_id = c.party_id
5974   AND c.cust_account_id = cv_cust_account_id;
5975 
5976 BEGIN
5977   --------------------- initialize -----------------------
5978   SAVEPOINT Pay_Claim_For_Accruals;
5979 
5980   IF OZF_DEBUG_HIGH_ON THEN
5981      OZF_Utility_PVT.debug_message(l_full_name||': start');
5982   END IF;
5983 
5984   IF FND_API.to_boolean(p_init_msg_list) THEN
5985     FND_MSG_PUB.initialize;
5986   END IF;
5987 
5988   IF NOT FND_API.compatible_api_call(
5989          l_api_version,
5990          p_api_version,
5991          l_api_name,
5992          g_pkg_name
5993   ) THEN
5994     RAISE FND_API.g_exc_unexpected_error;
5995   END IF;
5996 
5997   x_return_status := FND_API.g_ret_sts_success;
5998 
5999   ----------------- start ----------------
6000   l_claim_rec := p_claim_rec;
6001 
6002   l_close_claim_flag := 'T';
6003 
6004   IF l_claim_rec.payment_method IS NULL THEN
6005     -- get payment method information from trade profile
6006     Get_Cust_Trade_Profile(
6007       p_cust_account_id     => p_claim_rec.cust_account_id
6008      ,x_cust_trade_profile  => l_cust_trade_profile
6009      ,p_site_use_id         => p_claim_rec.cust_billto_acct_site_id
6010     );
6011 
6012     IF l_cust_trade_profile.trade_profile_id IS NOT NULL THEN
6013       Validate_Cust_Trade_Profile(
6014         p_cust_trade_profile  => l_cust_trade_profile
6015        ,x_return_status       => l_return_status
6016       );
6017       IF l_return_status = FND_API.g_ret_sts_error or
6018         l_return_status = FND_API.g_ret_sts_unexp_error THEN
6019         -- trade profile has errors. do not close claim in batch process
6020         -- and raise error in other cases
6021         IF l_claim_rec.source_object_class = OZF_RESALE_COMMON_PVT.G_BATCH_OBJECT_CLASS THEN
6022           l_close_claim_flag := 'F';
6023         ELSE
6024           RAISE FND_API.g_exc_unexpected_error;
6025         END IF;
6026       END IF;
6027 
6028       -- [BEGIN OF BUG 4217781 FIXING]
6029       IF l_claim_rec.source_object_class = OZF_RESALE_COMMON_PVT.G_BATCH_OBJECT_CLASS AND
6030          l_cust_trade_profile.autopay_flag <> 'T' THEN
6031          l_close_claim_flag := 'F';
6032       END IF;
6033       -- [END OF BUG 4217781 FIXING]
6034 
6035       IF l_cust_trade_profile.payment_method <> FND_API.G_MISS_CHAR THEN -- [BUG 4217781 FIXING]
6036          l_claim_rec.payment_method := l_cust_trade_profile.payment_method;
6037       END IF;
6038       l_claim_rec.cust_billto_acct_site_id := l_cust_trade_profile.site_use_id;
6039       l_claim_rec.vendor_id := l_cust_trade_profile.vendor_id;
6040       l_claim_rec.vendor_site_id := l_cust_trade_profile.vendor_site_id;
6041     ELSE
6042       -- trade profile does not exists. do not close claim in batch process
6043       -- and raise error in other cases
6044       IF l_claim_rec.source_object_class = OZF_RESALE_COMMON_PVT.G_BATCH_OBJECT_CLASS THEN
6045         l_close_claim_flag := 'F';
6046       ELSE
6047         IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
6048           OPEN csr_party_name(p_claim_rec.cust_account_id);
6049           FETCH csr_party_name INTO l_party_name;
6050           CLOSE csr_party_name;
6051 
6052           FND_MESSAGE.Set_Name('OZF', 'OZF_CLAIM_NO_TRADE_PROFILE');
6053           FND_MESSAGE.Set_Token('CUST_NAME', l_party_name);
6054           FND_MSG_PUB.ADD;
6055         END IF;
6056         RAISE FND_API.g_exc_unexpected_error;
6057       END IF;
6058     END IF;
6059   END IF;
6060 
6061    IF OZF_DEBUG_HIGH_ON THEN
6062      OZF_Utility_PVT.debug_message('Payment method is '||l_claim_rec.payment_method);
6063      OZF_Utility_PVT.debug_message('Autopay Flag is '||l_cust_trade_profile.autopay_flag);
6064      OZF_Utility_PVT.debug_message('Close Claim Flag is '||l_close_claim_flag);
6065      OZF_Utility_PVT.debug_message('l_claim_rec.source_object_class: '||l_claim_rec.source_object_class);
6066      OZF_Utility_PVT.debug_message('OZF_RESALE_COMMON_PVT.G_BATCH_OBJECT_CLASS: '||OZF_RESALE_COMMON_PVT.G_BATCH_OBJECT_CLASS);
6067      OZF_Utility_PVT.debug_message('l_claim_rec.batch_type: '||l_claim_rec.batch_type);
6068      OZF_Utility_PVT.debug_message('OZF_RESALE_COMMON_PVT.G_BATCH_REF_TYPE: '||OZF_RESALE_COMMON_PVT.G_BATCH_REF_TYPE);
6069      OZF_Utility_PVT.debug_message('l_accrual_flag: '||l_accrual_flag);
6070      OZF_Utility_PVT.debug_message('Invokes Create_Claim_For_Accruals ');
6071   END IF;
6072 
6073   --Bug# 8632964 fixed by anuj & muthsubr (+)
6074   IF (l_claim_rec.source_object_class = OZF_RESALE_COMMON_PVT.G_BATCH_OBJECT_CLASS AND
6075      l_claim_rec.batch_type = OZF_RESALE_COMMON_PVT.G_BATCH_REF_TYPE AND
6076      l_accrual_flag = 'T')
6077      OR (l_accrual_flag IS NULL) --kdass - bug 10149100 - for promotional payments, use existing API Create_Claim_For_Accruals()
6078   THEN
6079   Create_Claim_For_Accruals(
6080      p_api_version         => l_api_version
6081     ,p_init_msg_list       => FND_API.g_false
6082     ,p_commit              => FND_API.g_false
6083     ,p_validation_level    => p_validation_level
6084     ,x_return_status       => l_return_status
6085     ,x_msg_count           => x_msg_count
6086     ,x_msg_data            => x_msg_data
6087     ,p_claim_rec           => l_claim_rec
6088     ,p_funds_util_flt      => p_funds_util_flt
6089     ,x_claim_id            => l_claim_id
6090   );
6091           IF l_return_status =  fnd_api.g_ret_sts_error THEN
6092             RAISE FND_API.g_exc_error;
6093           ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6094             RAISE FND_API.g_exc_unexpected_error;
6095           END IF;
6096    ELSE
6097            Create_Claim_Existing_Accruals(
6098                 p_api_version         => l_api_version
6099                ,p_init_msg_list       => FND_API.g_false
6100                ,p_commit              => FND_API.g_false
6101                ,p_validation_level    => p_validation_level
6102                ,x_return_status       => l_return_status
6103                ,x_msg_count           => x_msg_count
6104                ,x_msg_data            => x_msg_data
6105                ,p_claim_rec           => l_claim_rec
6106                ,p_funds_util_flt      => p_funds_util_flt
6107                ,x_claim_id            => l_claim_id
6108              );
6109   IF l_return_status =  fnd_api.g_ret_sts_error THEN
6110     RAISE FND_API.g_exc_error;
6111   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6112     RAISE FND_API.g_exc_unexpected_error;
6113   END IF;
6114   END IF;
6115   --Bug# 8632964 fixed by anuj & muthsubr (-)
6116 
6117   IF OZF_DEBUG_HIGH_ON THEN
6118      OZF_Utility_PVT.debug_message('l_claim_id: '||l_claim_id);
6119   END IF;
6120 
6121   IF l_claim_id IS NOT NULL AND l_claim_id <> -1 AND l_close_claim_flag = 'T' THEN  --ninarasi fix for bug 12942827 12-Sep-2011
6122     Settle_Claim(
6123        p_claim_id            => l_claim_id
6124       ,x_return_status       => l_return_status
6125       ,x_msg_count           => x_msg_count
6126       ,x_msg_data            => x_msg_data
6127     );
6128     IF l_return_status =  fnd_api.g_ret_sts_error THEN
6129       RAISE FND_API.g_exc_error;
6130     ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6131       RAISE FND_API.g_exc_unexpected_error;
6132     END IF;
6133   END IF;
6134 
6135   x_claim_id := l_claim_id;
6136 
6137   ------------------------- finish -------------------------------
6138   -- Check for commit
6139   IF FND_API.to_boolean(p_commit) THEN
6140     COMMIT;
6141   END IF;
6142 
6143   FND_MSG_PUB.count_and_get(
6144          p_encoded => FND_API.g_false,
6145          p_count   => x_msg_count,
6146          p_data    => x_msg_data
6147   );
6148 
6149   IF OZF_DEBUG_HIGH_ON THEN
6150      OZF_Utility_PVT.debug_message(l_full_name ||': end');
6151   END IF;
6152 
6153 EXCEPTION
6154   WHEN FND_API.g_exc_error THEN
6155     ROLLBACK TO Pay_Claim_For_Accruals;
6156     x_return_status := FND_API.g_ret_sts_error;
6157     FND_MSG_PUB.count_and_get (
6158            p_encoded => FND_API.g_false
6159           ,p_count   => x_msg_count
6160           ,p_data    => x_msg_data
6161     );
6162 
6163   WHEN FND_API.g_exc_unexpected_error THEN
6164     ROLLBACK TO Pay_Claim_For_Accruals;
6165     x_return_status := FND_API.g_ret_sts_unexp_error ;
6166     FND_MSG_PUB.count_and_get (
6167            p_encoded => FND_API.g_false
6168           ,p_count   => x_msg_count
6169           ,p_data    => x_msg_data
6170     );
6171 
6172   WHEN OTHERS THEN
6173     ROLLBACK TO Pay_Claim_For_Accruals;
6174     x_return_status := FND_API.g_ret_sts_unexp_error ;
6175     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
6176       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
6177     END IF;
6178     FND_MSG_PUB.count_and_get(
6179            p_encoded => FND_API.g_false
6180           ,p_count   => x_msg_count
6181           ,p_data    => x_msg_data
6182     );
6183 
6184 END Pay_Claim_For_Accruals;
6185 
6186 
6187 ---------------------------------------------------------------------
6188 -- PROCEDURE
6189 --    Asso_Accruals_To_Claim
6190 --
6191 -- PURPOSE
6192 --    Associate earnings to the given claim based on given filters.
6193 --
6194 -- PARAMETERS
6195 --    p_claim_id:
6196 --    p_funds_util_flt:
6197 --
6198 -- NOTES
6199 ---------------------------------------------------------------------
6200 PROCEDURE Asso_Accruals_To_Claim(
6201    p_api_version         IN  NUMBER
6202   ,p_init_msg_list       IN  VARCHAR2  := FND_API.g_false
6203   ,p_commit              IN  VARCHAR2  := FND_API.g_false
6204   ,p_validation_level    IN  NUMBER    := FND_API.g_valid_level_full
6205 
6206   ,x_return_status       OUT NOCOPY VARCHAR2
6207   ,x_msg_count           OUT NOCOPY NUMBER
6208   ,x_msg_data            OUT NOCOPY VARCHAR2
6209 
6210   ,p_claim_id            IN  NUMBER
6211   ,p_funds_util_flt      IN  funds_util_flt_type
6212 )
6213 IS
6214 l_api_version CONSTANT NUMBER       := 1.0;
6215 l_api_name    CONSTANT VARCHAR2(30) := 'Asso_Accruals_To_Claim';
6216 l_full_name   CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
6217 l_return_status VARCHAR2(1);
6218 
6219 -- Added For Bug 7509079
6220 l_funds_util_flt     funds_util_flt_type := p_funds_util_flt;
6221 
6222 l_line_tbl           OZF_CLAIM_LINE_PVT.claim_line_tbl_type;
6223 l_cust_account_id    NUMBER;
6224 l_plan_type          VARCHAR2(30);
6225 l_plan_id            NUMBER;
6226 l_product_level_type VARCHAR2(30);
6227 l_product_id         NUMBER;
6228 l_total_amount       NUMBER;
6229 l_amount             NUMBER;
6230 
6231 l_emp_csr            NUMBER;
6232 l_error_index        NUMBER;
6233 l_counter            PLS_INTEGER := 1;
6234 l_ignore             NUMBER;
6235 l_dummy              VARCHAR2(1);
6236 l_stmt               VARCHAR2(3000);
6237 l_currency_code      VARCHAR2(15);
6238 --Added For Bug 7605745
6239 l_bill_to_site_id      NUMBER;
6240 
6241 -- Fix for Bug 7632911
6242 l_claim_class         VARCHAR2(15);
6243 
6244 -- Added For Multi Currency - kpatro
6245 l_claim_currency_code VARCHAR2(15);
6246 
6247 l_currency_rec       currency_rec_type;
6248 -- Added For Bug 7611966
6249 CURSOR csr_claim_line(cv_claim_id IN NUMBER) IS
6250   SELECT claim_line_id
6251   FROM ozf_claim_lines
6252   WHERE claim_id = cv_claim_id;
6253 
6254 CURSOR csr_uom_code(cv_item_id IN NUMBER) IS
6255   SELECT primary_uom_code
6256   FROM mtl_system_items
6257   WHERE inventory_item_id = cv_item_id
6258   AND organization_id = FND_PROFILE.VALUE('AMS_ITEM_ORGANIZATION_ID');
6259 
6260 -- Fix for Bug 7632911
6261 -- Added the currency_code for Multi Currency - kpatro
6262 CURSOR csr_claim_info(cv_claim_id IN NUMBER) IS
6263   SELECT cust_account_id, amount,claim_class,currency_code
6264   FROM ozf_claims
6265   WHERE claim_id = cv_claim_id;
6266 
6267  --Fix For Bug 7611966
6268 CURSOR csr_claim_line_util(cv_claim_line_id IN NUMBER, cv_claim_id NUMBER) IS
6269   SELECT claim_line_id
6270        , activity_type
6271        , activity_id
6272        , item_id
6273        , claim_currency_amount
6274   FROM ozf_claim_lines
6275   WHERE claim_line_id = cv_claim_line_id
6276   AND claim_id = cv_claim_id;
6277 CURSOR csr_function_currency IS
6278   SELECT gs.currency_code
6279   FROM gl_sets_of_books gs
6280   ,    ozf_sys_parameters org
6281   WHERE org.set_of_books_id = gs.set_of_books_id
6282   AND   org.org_id = MO_GLOBAL.GET_CURRENT_ORG_ID();
6283 
6284 BEGIN
6285   --------------------- initialize -----------------------
6286   SAVEPOINT Asso_Accruals_To_Claim;
6287 
6288   IF OZF_DEBUG_HIGH_ON THEN
6289      OZF_Utility_PVT.debug_message(l_full_name||': start');
6290   END IF;
6291 
6292   IF FND_API.to_boolean(p_init_msg_list) THEN
6293     FND_MSG_PUB.initialize;
6294   END IF;
6295 
6296   IF NOT FND_API.compatible_api_call(
6297          l_api_version,
6298          p_api_version,
6299          l_api_name,
6300          g_pkg_name
6301   ) THEN
6302     RAISE FND_API.g_exc_unexpected_error;
6303   END IF;
6304 
6305   x_return_status := FND_API.g_ret_sts_success;
6306 
6307   ----------------- start ----------------
6308 
6309  -- Added the currency_code for Multi Currency - kpatro
6310   OPEN csr_claim_info(p_claim_id);
6311   FETCH csr_claim_info INTO l_cust_account_id, l_total_amount,l_claim_class,l_claim_currency_code;
6312   IF csr_claim_info%NOTFOUND THEN
6313     CLOSE csr_claim_info;
6314     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
6315       FND_MESSAGE.Set_Name('OZF', 'OZF_CLAIM_ID_NOT_EXIST');
6316       FND_MESSAGE.Set_Token('CLAIM_ID', p_claim_id);
6317       FND_MSG_PUB.ADD;
6318     END IF;
6319     RAISE FND_API.g_exc_unexpected_error;
6320   END IF;
6321   CLOSE csr_claim_info;
6322 
6323   -- Assigning the Claim Currency for Multi Currency - kpatro
6324   l_currency_rec.claim_currency_code       := l_claim_currency_code;
6325   l_currency_rec.transaction_currency_code := l_funds_util_flt.utiz_currency_code;
6326 
6327   OPEN csr_function_currency;
6328   FETCH csr_function_currency INTO l_currency_rec.functional_currency_code;
6329   CLOSE csr_function_currency;
6330 
6331 
6332    IF l_currency_rec.claim_currency_code = l_currency_rec.transaction_currency_code THEN
6333       l_currency_rec.association_currency_code := l_currency_rec.transaction_currency_code;
6334    ELSE
6335      l_currency_rec.association_currency_code := l_currency_rec.functional_currency_code;
6336    END IF;
6337 
6338    --Set UNIVERSAL currency from profile.
6339   l_currency_rec.universal_currency_code := FND_PROFILE.VALUE('OZF_TP_COMMON_CURRENCY');
6340 
6341    IF OZF_DEBUG_HIGH_ON THEN
6342      OZF_Utility_PVT.debug_message('l_currency_rec.universal_currency_code :' || l_currency_rec.universal_currency_code);
6343      OZF_Utility_PVT.debug_message('l_currency_rec.claim_currency_code :' || l_currency_rec.claim_currency_code);
6344      OZF_Utility_PVT.debug_message('l_currency_rec.functional_currency_code :' || l_currency_rec.functional_currency_code);
6345      OZF_Utility_PVT.debug_message('l_currency_rec.transaction_currency_code :' || l_currency_rec.transaction_currency_code);
6346      OZF_Utility_PVT.debug_message('l_currency_rec.association_currency_code :' || l_currency_rec.association_currency_code);
6347   END IF;
6348 
6349 
6350   -- default cust_account_id if not given in parameters
6351   IF l_funds_util_flt.cust_account_id IS NULL THEN
6352     l_funds_util_flt.cust_account_id := l_cust_account_id;
6353   END IF;
6354 
6355    -- Fix for Bug 7632911
6356   IF l_funds_util_flt.total_amount IS NULL OR
6357      SIGN(l_funds_util_flt.total_amount) > SIGN(l_total_amount)
6358   THEN
6359     l_funds_util_flt.total_amount := l_total_amount;
6360   ELSE
6361     l_total_amount := l_funds_util_flt.total_amount;
6362   END IF;
6363 
6364   IF OZF_DEBUG_HIGH_ON THEN
6365      OZF_Utility_PVT.debug_message(l_funds_util_flt.utilization_id||': Before dynamic');
6366   END IF;
6367 
6368   Get_Utiz_Sql_Stmt(
6369      p_api_version         => 1.0
6370     ,p_init_msg_list       => FND_API.g_false
6371     ,p_commit              => FND_API.g_false
6372     ,p_validation_level    => FND_API.g_valid_level_full
6373     ,x_return_status       => l_return_status
6374     ,x_msg_count           => x_msg_count
6375     ,x_msg_data            => x_msg_data
6376     ,p_summary_view        => 'AUTOPAY_LINE'
6377     ,p_funds_util_flt      => l_funds_util_flt
6378     ,px_currency_rec       => l_currency_rec
6379     ,p_cust_account_id     => l_funds_util_flt.cust_account_id
6380     ,x_utiz_sql_stmt       => l_stmt
6381   );
6382 
6383   IF l_return_status = FND_API.g_ret_sts_error THEN
6384     RAISE FND_API.g_exc_error;
6385   ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
6386     RAISE FND_API.g_exc_error;
6387   END IF;
6388 
6389   l_emp_csr := DBMS_SQL.open_cursor;
6390   FND_DSQL.set_cursor(l_emp_csr);
6391    DBMS_SQL.parse(l_emp_csr, l_stmt, DBMS_SQL.native);
6392   DBMS_SQL.define_column(l_emp_csr, 1, l_cust_account_id);
6393   DBMS_SQL.define_column(l_emp_csr, 2, l_plan_type, 30);
6394   DBMS_SQL.define_column(l_emp_csr, 3, l_plan_id);
6395   DBMS_SQL.define_column(l_emp_csr, 4, l_bill_to_site_id); --Fix for Bug 7605745
6396   DBMS_SQL.define_column(l_emp_csr, 5, l_product_level_type, 30);
6397   DBMS_SQL.define_column(l_emp_csr, 6, l_product_id);
6398   DBMS_SQL.define_column(l_emp_csr, 7, l_amount);
6399   DBMS_SQL.define_column(l_emp_csr, 8, l_currency_code, 15);
6400   FND_DSQL.do_binds;
6401 
6402   l_ignore := DBMS_SQL.execute(l_emp_csr);
6403   LOOP
6404     IF DBMS_SQL.fetch_rows(l_emp_csr) > 0 AND l_total_amount <> 0 THEN
6405        DBMS_SQL.column_value(l_emp_csr, 1, l_cust_account_id);
6406       DBMS_SQL.column_value(l_emp_csr, 2, l_plan_type);
6407       DBMS_SQL.column_value(l_emp_csr, 3, l_plan_id);
6408       DBMS_SQL.column_value(l_emp_csr, 4, l_bill_to_site_id); --Fix for Bug 7605745
6409       DBMS_SQL.column_value(l_emp_csr, 5, l_product_level_type);
6410       DBMS_SQL.column_value(l_emp_csr, 6, l_product_id);
6411       DBMS_SQL.column_value(l_emp_csr, 7, l_amount);
6412       DBMS_SQL.column_value(l_emp_csr, 8, l_currency_code);
6413 
6414       IF l_amount IS NOT NULL AND l_amount <> 0 THEN
6415         FND_FILE.PUT_LINE(FND_FILE.LOG, 'Line ' || l_counter || ': Amount='||l_amount);
6416         FND_FILE.PUT_LINE(FND_FILE.LOG, 'Plan Type         :  ' || l_plan_type);
6417         FND_FILE.PUT_LINE(FND_FILE.LOG, 'Plan Id           :  ' || l_plan_id);
6418         FND_FILE.PUT_LINE(FND_FILE.LOG, 'Product Level Type:  ' || l_product_level_type);
6419         FND_FILE.PUT_LINE(FND_FILE.LOG, 'Product Id        :  ' || l_product_id);
6420         FND_FILE.PUT_LINE(FND_FILE.LOG, '---------------------------------');
6421 
6422         -- Fix for Bug 7632911
6423         IF (l_total_amount >= l_amount AND l_claim_class <>'CHARGE') THEN
6424           l_line_tbl(l_counter).claim_currency_amount   := l_amount;
6425           l_total_amount := l_total_amount - l_amount;
6426         ELSE
6427           l_line_tbl(l_counter).claim_currency_amount   := l_total_amount;
6428           -- Fix for Bug 7632911
6429           IF (l_claim_class = 'CHARGE') THEN
6430            l_total_amount := l_total_amount - l_amount;
6431           ELSE
6432           l_total_amount := 0;
6433           END IF;
6434         END IF;
6435 
6436         l_line_tbl(l_counter).claim_id                  := p_claim_id;
6437         l_line_tbl(l_counter).activity_type             := l_plan_type;
6438         l_line_tbl(l_counter).activity_id               := l_plan_id;
6439         l_line_tbl(l_counter).relationship_type         := l_funds_util_flt.relationship_type;
6440         l_line_tbl(l_counter).related_cust_account_id   := l_funds_util_flt.related_cust_account_id;
6441         l_line_tbl(l_counter).buy_group_party_id        := l_funds_util_flt.buy_group_party_id;
6442         l_line_tbl(l_counter).select_cust_children_flag := l_funds_util_flt.select_cust_children_flag;
6443         -- Added For Bug 8402328
6444         l_line_tbl(l_counter).utilization_id := l_funds_util_flt.utilization_id;
6445         IF l_product_level_type = 'PRODUCT' AND l_product_id IS NOT NULL THEN
6446           l_line_tbl(l_counter).item_id                   := l_product_id;
6447           OPEN csr_uom_code(l_line_tbl(l_counter).item_id);
6448           FETCH csr_uom_code INTO l_line_tbl(l_counter).quantity_uom;
6449           IF csr_uom_code%NOTFOUND THEN
6450             CLOSE csr_uom_code;
6451             IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
6452               FND_MESSAGE.Set_Name('OZF', 'OZF_PRODUCT_UOM_MISSING');
6453               FND_MESSAGE.Set_Token('ITEM_ID', l_product_id);
6454               FND_MSG_PUB.ADD;
6455             END IF;
6456             RAISE FND_API.g_exc_unexpected_error;
6457           END IF;
6458           CLOSE csr_uom_code;
6459         END IF;
6460 
6461         /*
6462         IF l_funds_util_flt.activity_type = 'OFFR' THEN
6463           l_dummy := Check_for_Offer_Performance ( p_cust_account_id   => l_funds_util_flt.cust_account_id
6464                                                  , p_funds_util_flt    => l_funds_util_flt
6465                                                  , x_return_status     => l_return_status
6466                                                  );
6467 
6468           IF l_return_status = FND_API.g_ret_sts_error THEN
6469             RAISE FND_API.g_exc_error;
6470           ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
6471             RAISE FND_API.g_exc_error;
6472           END IF;
6473 
6474           IF l_dummy = FND_API.G_TRUE THEN
6475             l_line_tbl(l_counter).performance_attached_flag := FND_API.G_TRUE;
6476             l_line_tbl(l_counter).performance_complete_flag := FND_API.G_TRUE;
6477           END IF;
6478         END IF;
6479         */
6480 
6481         l_counter := l_counter + 1;
6482       END IF;
6483     ELSE
6484       EXIT;
6485     END IF;
6486   END LOOP;
6487   DBMS_SQL.close_cursor(l_emp_csr);
6488 
6489   -- if earnings are found
6490   IF l_counter > 1 THEN
6491     OZF_CLAIM_LINE_PVT.Create_Claim_Line_Tbl(
6492        p_api_version       => 1.0
6493       ,p_init_msg_list     => FND_API.g_false
6494       ,p_commit            => FND_API.g_false
6495       ,p_validation_level  => FND_API.g_valid_level_full
6496       ,x_return_status     => l_return_status
6497       ,x_msg_count         => x_msg_count
6498       ,x_msg_data          => x_msg_data
6499       ,p_claim_line_tbl    => l_line_tbl
6500       ,x_error_index       => l_error_index
6501     );
6502 
6503     IF l_return_status = FND_API.g_ret_sts_error THEN
6504       RAISE FND_API.g_exc_error;
6505     ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
6506       RAISE FND_API.g_exc_error;
6507     END IF;
6508 
6509     -- Added For Bug 7611966
6510     OPEN csr_claim_line(p_claim_id);
6511     LOOP
6512       FETCH csr_claim_line INTO l_funds_util_flt.claim_line_id;
6513       EXIT WHEN csr_claim_line%NOTFOUND;
6514     END  LOOP;
6515     CLOSE csr_claim_line;
6516 
6517       -- Fix For Bug 7611966
6518      OPEN csr_claim_line_util(l_funds_util_flt.claim_line_id,p_claim_id);
6519      LOOP
6520       FETCH csr_claim_line_util INTO l_funds_util_flt.claim_line_id
6521                               , l_funds_util_flt.activity_type
6522                               , l_funds_util_flt.activity_id
6523                               , l_funds_util_flt.product_id
6524                               , l_funds_util_flt.total_amount;
6525       EXIT WHEN csr_claim_line_util%NOTFOUND;
6526 
6527       Update_Group_Line_Util(
6528          p_api_version         => 1.0
6529         ,p_init_msg_list       => FND_API.g_false
6530         ,p_commit              => FND_API.g_false
6531         ,p_validation_level    => FND_API.G_VALID_LEVEL_FULL
6532         ,x_return_status       => l_return_status
6533         ,x_msg_count           => x_msg_count
6534         ,x_msg_data            => x_msg_data
6535         ,p_summary_view        => 'ACTIVITY'
6536         ,p_funds_util_flt      => l_funds_util_flt
6537       );
6538 
6539       IF l_return_status = FND_API.g_ret_sts_error THEN
6540         RAISE FND_API.g_exc_error;
6541       ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
6542         RAISE FND_API.g_exc_error;
6543       END IF;
6544     END LOOP;
6545     CLOSE csr_claim_line_util;
6546   END IF;
6547 
6548   ------------------------- finish -------------------------------
6549   -- Check for commit
6550   IF FND_API.to_boolean(p_commit) THEN
6551     COMMIT;
6552   END IF;
6553 
6554   FND_MSG_PUB.count_and_get(
6555          p_encoded => FND_API.g_false,
6556          p_count   => x_msg_count,
6557          p_data    => x_msg_data
6558   );
6559 
6560   IF OZF_DEBUG_HIGH_ON THEN
6561      OZF_Utility_PVT.debug_message(l_full_name ||': end');
6562   END IF;
6563 
6564 EXCEPTION
6565   WHEN FND_API.g_exc_error THEN
6566     ROLLBACK TO Asso_Accruals_To_Claim;
6567     x_return_status := FND_API.g_ret_sts_error;
6568     FND_MSG_PUB.count_and_get (
6569            p_encoded => FND_API.g_false
6570           ,p_count   => x_msg_count
6571           ,p_data    => x_msg_data
6572     );
6573 
6574   WHEN FND_API.g_exc_unexpected_error THEN
6575     ROLLBACK TO Asso_Accruals_To_Claim;
6576     x_return_status := FND_API.g_ret_sts_unexp_error ;
6577     FND_MSG_PUB.count_and_get (
6578            p_encoded => FND_API.g_false
6579           ,p_count   => x_msg_count
6580           ,p_data    => x_msg_data
6581     );
6582 
6583   WHEN OTHERS THEN
6584     ROLLBACK TO Asso_Accruals_To_Claim;
6585     x_return_status := FND_API.g_ret_sts_unexp_error ;
6586     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
6587       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
6588     END IF;
6589     FND_MSG_PUB.count_and_get(
6590            p_encoded => FND_API.g_false
6591           ,p_count   => x_msg_count
6592           ,p_data    => x_msg_data
6593     );
6594 
6595 END Asso_Accruals_To_Claim;
6596 
6597 
6598 ---------------------------------------------------------------------
6599 -- PROCEDURE
6600 --    Asso_Accruals_To_Claim_Line
6601 --
6602 -- PURPOSE
6603 --    Associate earnings to the given claim line based on line
6604 --    properties
6605 --
6606 -- PARAMETERS
6607 --    p_claim_line_id:
6608 --
6609 -- NOTES
6610 -- modified for Bugfix 5182452 l_funds_util populated with claim line info.
6611 ---------------------------------------------------------------------
6612 PROCEDURE Asso_Accruals_To_Claim_Line(
6613    p_api_version         IN  NUMBER
6614   ,p_init_msg_list       IN  VARCHAR2  := FND_API.g_false
6615   ,p_commit              IN  VARCHAR2  := FND_API.g_false
6616   ,p_validation_level    IN  NUMBER    := FND_API.g_valid_level_full
6617 
6618   ,x_return_status       OUT NOCOPY VARCHAR2
6619   ,x_msg_count           OUT NOCOPY NUMBER
6620   ,x_msg_data            OUT NOCOPY VARCHAR2
6621 
6622   ,p_claim_line_id       IN  NUMBER
6623   ,p_run_mode		 IN  VARCHAR2 DEFAULT NULL --added parameter run mode, default null Bug 13349711
6624 )
6625 IS
6626 l_api_version CONSTANT NUMBER       := 1.0;
6627 l_api_name    CONSTANT VARCHAR2(30) := 'Asso_Accruals_To_Claim_Line';
6628 l_full_name   CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
6629 l_return_status VARCHAR2(1);
6630 
6631 l_funds_util_flt     funds_util_flt_type;
6632 l_line_id            NUMBER;
6633 
6634 --start of bugfix 5182452
6635 CURSOR csr_line_info(cv_claim_line_id IN NUMBER) IS
6636  select  claim_line_id
6637 , plan_id
6638 , activity_type
6639 , activity_id
6640 , offer_type
6641 , source_object_class --> document_class
6642 , source_object_id  -->    document_id
6643 , item_id -->product_id
6644 , amount -->total_amount
6645 , quantity
6646 , quantity_uom -->uom_code
6647 , relationship_type
6648 , related_cust_account_id
6649 , buy_group_cust_account_id
6650 , buy_group_party_id
6651 , select_cust_children_flag
6652 , prorate_earnings_flag
6653 , utilization_id -- Added For Bug 8402328
6654 from ozf_claim_lines_all
6655 WHERE claim_line_id = cv_claim_line_id;
6656 
6657 --used to derive old_total_amount
6658 CURSOR csr_sum_util_amounts(cv_claim_line_id IN NUMBER) IS
6659 select sum(amount)
6660 from ozf_claim_lines_util
6661 where claim_line_id = cv_claim_line_id;
6662 
6663 --used to derive old_total_units
6664 CURSOR csr_sum_scan_units(cv_claim_line_id IN NUMBER) IS
6665 select sum(scan_unit)
6666 from ozf_claim_lines_util
6667 where claim_line_id = cv_claim_line_id;
6668 --end of bugfix 5182452
6669 
6670 BEGIN
6671   --------------------- initialize -----------------------
6672   SAVEPOINT Asso_Accruals_To_Claim_Line;
6673 
6674   IF OZF_DEBUG_HIGH_ON THEN
6675      OZF_Utility_PVT.debug_message(l_full_name||': start');
6676   END IF;
6677 
6678   IF FND_API.to_boolean(p_init_msg_list) THEN
6679     FND_MSG_PUB.initialize;
6680   END IF;
6681 
6682   IF NOT FND_API.compatible_api_call(
6683          l_api_version,
6684          p_api_version,
6685          l_api_name,
6686          g_pkg_name
6687   ) THEN
6688     RAISE FND_API.g_exc_unexpected_error;
6689   END IF;
6690 
6691   x_return_status := FND_API.g_ret_sts_success;
6692 
6693   ----------------- Associate earnings ----------------
6694   -- verify the claim exists
6695   --start of bugfix 5182452
6696 
6697  OPEN csr_line_info(p_claim_line_id);
6698   FETCH csr_line_info INTO l_funds_util_flt.claim_line_id,
6699       l_funds_util_flt.fund_id
6700      ,l_funds_util_flt.activity_type
6701      ,l_funds_util_flt.activity_id
6702      , l_funds_util_flt.offer_type
6703      , l_funds_util_flt.document_class
6704      , l_funds_util_flt.document_id
6705      ,l_funds_util_flt.product_id
6706      , l_funds_util_flt.total_amount
6707      , l_funds_util_flt.quantity
6708      , l_funds_util_flt.uom_code
6709      , l_funds_util_flt.relationship_type
6710      , l_funds_util_flt.related_cust_account_id
6711      , l_funds_util_flt.buy_group_cust_account_id
6712      , l_funds_util_flt.buy_group_party_id
6713      , l_funds_util_flt.select_cust_children_flag
6714      ,l_funds_util_flt.prorate_earnings_flag
6715      ,l_funds_util_flt.utilization_id; -- Added For Bug 8402328
6716 
6717   IF csr_line_info%NOTFOUND THEN
6718     CLOSE csr_line_info;
6719     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
6720       FND_MESSAGE.Set_Name('OZF', 'OZF_CLAIM_LINEID_NOT_EXIST');
6721       FND_MESSAGE.Set_Token('LINE_ID', p_claim_line_id);
6722       FND_MSG_PUB.ADD;
6723     END IF;
6724     RAISE FND_API.g_exc_unexpected_error;
6725   END IF;
6726   CLOSE csr_line_info;
6727 
6728    OPEN csr_sum_util_amounts(p_claim_line_id);
6729    FETCH csr_sum_util_amounts INTO l_funds_util_flt.old_total_amount;
6730 
6731     IF csr_sum_util_amounts%NOTFOUND THEN
6732     CLOSE csr_line_info;
6733     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
6734       FND_MESSAGE.Set_Name('OZF', 'OZF_CLAIM_LINEID_NOT_EXIST');
6735       FND_MESSAGE.Set_Token('LINE_ID', p_claim_line_id);
6736       FND_MSG_PUB.ADD;
6737     END IF;
6738     RAISE FND_API.g_exc_unexpected_error;
6739   END IF;
6740   CLOSE csr_sum_util_amounts;
6741 
6742    OPEN csr_sum_scan_units(p_claim_line_id);
6743    FETCH csr_sum_scan_units INTO l_funds_util_flt.old_total_units;
6744 
6745     IF csr_sum_scan_units%NOTFOUND THEN
6746     CLOSE csr_line_info;
6747     IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
6748       FND_MESSAGE.Set_Name('OZF', 'OZF_CLAIM_LINEID_NOT_EXIST');
6749       FND_MESSAGE.Set_Token('LINE_ID', p_claim_line_id);
6750       FND_MSG_PUB.ADD;
6751     END IF;
6752     RAISE FND_API.g_exc_unexpected_error;
6753   END IF;
6754   CLOSE csr_sum_scan_units;
6755   --end of bugfix 5182452
6756 l_funds_util_flt.run_mode := p_run_mode; --added to fix bug 13349711
6757   Update_Group_Line_Util(
6758      p_api_version            => l_api_version
6759     ,p_init_msg_list          => FND_API.g_false
6760     ,p_commit                 => FND_API.g_false
6761     ,p_validation_level       => p_validation_level
6762     ,x_return_status          => l_return_status
6763     ,x_msg_count              => x_msg_count
6764     ,x_msg_data               => x_msg_data
6765     ,p_summary_view           => null
6766     ,p_funds_util_flt         => l_funds_util_flt
6767     ,p_mode                   => OZF_CLAIM_UTILITY_PVT.g_auto_mode
6768   );
6769   IF l_return_status =  fnd_api.g_ret_sts_error THEN
6770     RAISE FND_API.g_exc_error;
6771   ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
6772     RAISE FND_API.g_exc_unexpected_error;
6773   END IF;
6774 
6775   ------------------------- finish -------------------------------
6776   -- Check for commit
6777   IF FND_API.to_boolean(p_commit) THEN
6778     COMMIT;
6779   END IF;
6780 
6781   FND_MSG_PUB.count_and_get(
6782          p_encoded => FND_API.g_false,
6783          p_count   => x_msg_count,
6784          p_data    => x_msg_data
6785   );
6786 
6787   IF OZF_DEBUG_HIGH_ON THEN
6788      OZF_Utility_PVT.debug_message(l_full_name ||': end');
6789   END IF;
6790 
6791 EXCEPTION
6792   WHEN FND_API.g_exc_error THEN
6793     ROLLBACK TO Asso_Accruals_To_Claim_Line;
6794     x_return_status := FND_API.g_ret_sts_error;
6795     FND_MSG_PUB.count_and_get (
6796            p_encoded => FND_API.g_false
6797           ,p_count   => x_msg_count
6798           ,p_data    => x_msg_data
6799     );
6800 
6801   WHEN FND_API.g_exc_unexpected_error THEN
6802     ROLLBACK TO Asso_Accruals_To_Claim_Line;
6803     x_return_status := FND_API.g_ret_sts_unexp_error ;
6804     FND_MSG_PUB.count_and_get (
6805            p_encoded => FND_API.g_false
6806           ,p_count   => x_msg_count
6807           ,p_data    => x_msg_data
6808     );
6809 
6810   WHEN OTHERS THEN
6811     ROLLBACK TO Asso_Accruals_To_Claim_Line;
6812     x_return_status := FND_API.g_ret_sts_unexp_error ;
6813     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
6814       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
6815     END IF;
6816     FND_MSG_PUB.count_and_get(
6817            p_encoded => FND_API.g_false
6818           ,p_count   => x_msg_count
6819           ,p_data    => x_msg_data
6820     );
6821 
6822 END Asso_Accruals_To_Claim_Line;
6823 
6824 ---------------------------------------------------------------------
6825 -- PROCEDURE
6826 --    Create_Claim_Per_SPR
6827 --
6828 -- PURPOSE
6829 --    Create claim for each SPR in Ship and Debit batch.
6830 --
6831 -- PARAMETERS
6832 --    p_resale_batch_id: resale batch id
6833 --
6834 -- NOTES
6835 --
6836 -- HISTORY
6837 -- 03-Jun-09  ateotia  Created.
6838 --                     Bug# 8571085 fixed.
6839 ---------------------------------------------------------------------
6840 PROCEDURE Create_Claim_Per_SPR(
6841    p_api_version       IN  NUMBER
6842   ,p_init_msg_list     IN  VARCHAR2 := FND_API.g_false
6843   ,p_commit            IN  VARCHAR2 := FND_API.g_false
6844   ,p_validation_level  IN  NUMBER   := FND_API.g_valid_level_full
6845   ,x_return_status     OUT NOCOPY VARCHAR2
6846   ,x_msg_count         OUT NOCOPY NUMBER
6847   ,x_msg_data          OUT NOCOPY VARCHAR2
6848   ,p_resale_batch_id   IN  NUMBER
6849 )
6850 IS
6851 l_api_version    CONSTANT  NUMBER       := 1.0;
6852 l_api_name       CONSTANT  VARCHAR2(30) := 'Create_Claim_Per_SPR';
6853 l_full_name      CONSTANT  VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
6854 l_return_status            VARCHAR2(1);
6855 l_cust_account_id          NUMBER;
6856 l_partner_id               NUMBER;
6857 l_claim_curr_code          VARCHAR2(15);
6858 l_line_curr_code           VARCHAR2(15);
6859 l_request_header_id        NUMBER;
6860 l_request_number           VARCHAR2(30);
6861 l_agreement_number         VARCHAR2(240);
6862 l_product_id               NUMBER;
6863 l_uom_code                 VARCHAR2(3);
6864 l_quantity                 NUMBER;
6865 l_line_amount              NUMBER;
6866 l_new_line_amount          NUMBER;
6867 l_claim_amount             NUMBER;
6868 l_claim_id                 NUMBER;
6869 l_claim_rec                OZF_CLAIM_PVT.claim_rec_type;
6870 l_line_tbl                 OZF_CLAIM_LINE_PVT.claim_line_tbl_type;
6871 l_cust_trade_profile       g_cust_trade_profile_csr%rowtype;
6872 l_payment_method           VARCHAR2(30);
6873 l_cust_billto_acct_site_id NUMBER;
6874 l_vendor_id                NUMBER;
6875 l_vendor_site_id           NUMBER;
6876 l_claim_line_id            NUMBER;
6877 l_party_name               VARCHAR2(360);
6878 l_close_claim_flag         VARCHAR2(1);
6879 l_partner_claim_num        VARCHAR2(30);
6880 l_counter                  PLS_INTEGER := 1;
6881 l_error_index              NUMBER;
6882 l_scan_value               NUMBER;
6883 l_offer_uom_code           VARCHAR2(3);
6884 l_offer_quantity           NUMBER;
6885 l_trans_curr_code          VARCHAR2(15);
6886 l_new_claim_quantity       NUMBER;
6887 l_exchange_rate_type       VARCHAR2(30) := FND_API.G_MISS_CHAR;
6888 l_rate                     NUMBER;
6889 
6890 --POS Batch Processing by profiles by ateotia (+)
6891 l_ship_from_stock_flag     VARCHAR2(1);
6892 l_offer_type               VARCHAR2(30);
6893 --POS Batch Processing by profiles by ateotia (-)
6894 
6895 CURSOR csr_resale_batch(cv_batch_id IN NUMBER) IS
6896   SELECT partner_cust_account_id
6897        , partner_id
6898        , currency_code
6899        , partner_claim_number
6900   FROM ozf_resale_batches
6901   WHERE resale_batch_id = cv_batch_id;
6902 
6903 CURSOR csr_batch_request(cv_batch_id IN NUMBER, cv_partner_id IN NUMBER) IS
6904   SELECT r.request_header_id
6905        , r.request_number
6906        --POS Batch Processing by profiles by ateotia (+)
6907        , r.ship_from_stock_flag
6908        , r.offer_type
6909        --POS Batch Processing by profiles by ateotia (-)
6910        , s.agreement_name
6911   FROM   ozf_resale_lines_int s
6912        , ozf_request_headers_all_b r
6913   WHERE  s.resale_batch_id = cv_batch_id
6914   AND    s.agreement_name = r.agreement_number
6915   AND    r.partner_id = cv_partner_id
6916   AND    r.status_code = 'APPROVED'
6917   AND    r.request_class = 'SPECIAL_PRICE'
6918   GROUP BY r.request_header_id
6919          , r.request_number
6920          , s.agreement_name
6921          , r.ship_from_stock_flag
6922          , r.offer_type;
6923 
6924 CURSOR csr_batch_line(cv_batch_id IN NUMBER, cv_agreement_number IN VARCHAR2) IS
6925   SELECT inventory_item_id
6926        , uom_code
6927        , sum(quantity)
6928        , currency_code
6929        , sum(total_accepted_amount)
6930   FROM ozf_resale_lines_int
6931   WHERE resale_batch_id = cv_batch_id
6932   AND agreement_name = cv_agreement_number
6933   AND status_code = 'PROCESSED'
6934   GROUP BY inventory_item_id
6935          , uom_code
6936          , currency_code;
6937 
6938 -- added for Bugfix 5404951
6939 CURSOR csr_ams_act_products(cv_agreement_number IN VARCHAR2 ,cv_product_id IN NUMBER) IS
6940   SELECT act.SCAN_VALUE
6941        , act.UOM_CODE
6942        , act.Quantity
6943        , off.TRANSACTION_CURRENCY_CODE
6944   FROM ozf_offers off
6945      , ams_act_products act
6946   WHERE offer_code  = cv_agreement_number
6947   AND   ARC_ACT_PRODUCT_USED_BY = 'OFFR'
6948   AND   ACT_PRODUCT_USED_BY_ID = off.qp_list_header_id
6949   AND   INVENTORY_ITEM_ID = cv_product_id;
6950 
6951 CURSOR csr_claim_line(cv_claim_id IN NUMBER) IS
6952   SELECT claim_line_id
6953   FROM ozf_claim_lines
6954   WHERE claim_id = cv_claim_id;
6955 
6956 CURSOR csr_party_name(cv_cust_account_id IN NUMBER) IS
6957   SELECT p.party_name
6958   FROM hz_parties p
6959      , hz_cust_accounts c
6960   WHERE p.party_id = c.party_id
6961   AND   c.cust_account_id = cv_cust_account_id;
6962 
6963 CURSOR c_get_conversion_type IS
6964   SELECT exchange_rate_type
6965   FROM   ozf_sys_parameters_all
6966   WHERE  org_id = MO_GLOBAL.GET_CURRENT_ORG_ID;
6967 
6968    --POS Batch Processing by profiles by rsatyava (+)
6969   l_auto_claim_profile varchar2(1):= FND_PROFILE.value('OZF_AUTO_CLAIM_POS');
6970   --POS Batch Processing by profiles by rsatyava (+)
6971 
6972 BEGIN
6973   --------------------- initialize -----------------------
6974   SAVEPOINT Create_Claim_Per_SPR;
6975 
6976   IF OZF_DEBUG_HIGH_ON THEN
6977      OZF_Utility_PVT.debug_message(l_full_name||': start');
6978   END IF;
6979 
6980   IF FND_API.to_boolean(p_init_msg_list) THEN
6981     FND_MSG_PUB.initialize;
6982   END IF;
6983 
6984   IF NOT FND_API.compatible_api_call(
6985          l_api_version,
6986          p_api_version,
6987          l_api_name,
6988          g_pkg_name
6989   ) THEN
6990     RAISE FND_API.g_exc_unexpected_error;
6991   END IF;
6992 
6993   x_return_status := FND_API.g_ret_sts_success;
6994 
6995   ----------------- Process batch ----------------
6996   OPEN csr_resale_batch(p_resale_batch_id);
6997   FETCH csr_resale_batch INTO l_cust_account_id
6998                             , l_partner_id
6999                             , l_claim_curr_code
7000                             , l_partner_claim_num;
7001   CLOSE csr_resale_batch;
7002 
7003   IF OZF_DEBUG_HIGH_ON THEN
7004     OZF_Utility_PVT.debug_message('Process batch for customer id '||l_cust_account_id);
7005   END IF;
7006 
7007   l_close_claim_flag := 'T';
7008 
7009   -- get payment method information from trade profile
7010   Get_Cust_Trade_Profile(
7011       p_cust_account_id     => l_cust_account_id
7012      ,x_cust_trade_profile  => l_cust_trade_profile
7013   );
7014 
7015   IF l_cust_trade_profile.trade_profile_id IS NOT NULL THEN
7016      Validate_Cust_Trade_Profile(
7017         p_cust_trade_profile  => l_cust_trade_profile
7018        ,x_return_status       => l_return_status
7019      );
7020 
7021      -- do not settle claim if trade profile has errors
7022      IF l_return_status = FND_API.g_ret_sts_error or
7023         l_return_status = FND_API.g_ret_sts_unexp_error THEN
7024         l_close_claim_flag := 'F';
7025      END IF;
7026 
7027      l_payment_method := l_cust_trade_profile.payment_method;
7028      l_cust_billto_acct_site_id := l_cust_trade_profile.site_use_id;
7029      l_vendor_id := l_cust_trade_profile.vendor_id;
7030      l_vendor_site_id := l_cust_trade_profile.vendor_site_id;
7031   ELSE
7032      -- do not settle claim if trade profile does not exists
7033      l_close_claim_flag := 'F';
7034   END IF;
7035 
7036   OPEN csr_batch_request(p_resale_batch_id, l_partner_id);
7037   LOOP
7038      FETCH csr_batch_request INTO l_request_header_id
7039                                 , l_request_number
7040                                 --POS Batch Processing by profiles by ateotia (+)
7041                                 , l_ship_from_stock_flag
7042                                 , l_offer_type
7043                                 --POS Batch Processing by profiles by ateotia (-)
7044                                 , l_agreement_number;
7045      EXIT WHEN csr_batch_request%NOTFOUND;
7046 
7047      IF OZF_DEBUG_HIGH_ON THEN
7048         OZF_Utility_PVT.debug_message('request id:'||l_request_header_id);
7049      END IF;
7050 
7051 
7052  --POS batch Profile change.added logic to create claim line only if the auto claim profile is set /offer type is scan data
7053 
7054     IF (l_auto_claim_profile='Y' ) OR (l_offer_type='SCAN_DATA') THEN
7055      -- create a claim per request(agreement)
7056      l_claim_amount := 0;
7057      l_counter := 1;
7058 
7059      --Added for 7360703
7060      IF l_line_tbl.EXISTS(1) THEN
7061         l_line_tbl.DELETE;
7062      END IF;
7063 
7064      OPEN csr_batch_line(p_resale_batch_id, l_agreement_number);
7065      LOOP
7066         FETCH csr_batch_line INTO l_product_id
7067                                 , l_uom_code
7068                                 , l_quantity
7069                                 , l_line_curr_code
7070                                 , l_line_amount;
7071         EXIT WHEN csr_batch_line%NOTFOUND;
7072 
7073         OPEN csr_ams_act_products(l_agreement_number, l_product_id );
7074         FETCH csr_ams_act_products INTO l_scan_value
7075                                       , l_offer_uom_code
7076                                       , l_offer_quantity
7077                                       , l_trans_curr_code;
7078         CLOSE csr_ams_act_products;
7079 
7080         -- calculate claim amount
7081         IF l_line_curr_code = l_claim_curr_code THEN
7082            l_new_line_amount := l_line_amount;
7083         ELSE
7084            --Added for bug 7030415, get exchange_rate type
7085            OPEN c_get_conversion_type;
7086            FETCH c_get_conversion_type INTO l_exchange_rate_type;
7087            CLOSE c_get_conversion_type;
7088            OZF_UTILITY_PVT.Convert_Currency(
7089               p_from_currency   => l_line_curr_code
7090              ,p_to_currency     => l_claim_curr_code
7091              ,p_conv_type       => l_exchange_rate_type
7092              ,p_conv_date       => SYSDATE
7093              ,p_from_amount     => l_line_amount
7094              ,x_return_status   => l_return_status
7095              ,x_to_amount       => l_new_line_amount
7096              ,x_rate            => l_rate);
7097            IF l_return_status = FND_API.g_ret_sts_error THEN
7098               RAISE FND_API.g_exc_error;
7099            ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
7100               RAISE FND_API.g_exc_unexpected_error;
7101            END IF;
7102         END IF;
7103         l_claim_amount := l_claim_amount + l_new_line_amount;
7104 
7105         -- Bugfix 5404951
7106         IF l_offer_uom_code <> l_uom_code THEN
7107            --POS Batch Processing by profiles by ateotia (+)
7108            IF (l_ship_from_stock_flag = 'Y' AND l_offer_type = 'ACCRUAL') THEN
7109               l_new_claim_quantity := l_quantity;
7110            ELSE
7111            l_new_claim_quantity := (l_line_amount * l_offer_quantity) / l_scan_value ; -- correct qty in offer_uom
7112            END IF;
7113            --POS Batch Processing by profiles by ateotia (-)
7114            -- convert this to claim line uom
7115            l_quantity := inv_convert.inv_um_convert(
7116                             item_id         => l_product_id
7117                            ,precision       => 2
7118                            ,from_quantity   => l_new_claim_quantity
7119                            ,from_unit       => l_offer_uom_code
7120                            ,to_unit         => l_uom_code
7121                            ,from_name       => NULL
7122                            ,to_name         => NULL);
7123            IF l_quantity = -99999 THEN
7124               IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
7125                  FND_MESSAGE.set_name('OZF', 'OZF_CLAIM_CANT_CONVERT_UOM');
7126                  FND_MSG_PUB.add;
7127               END IF;
7128               RAISE FND_API.g_exc_error;
7129            END IF;
7130 
7131         ELSE
7132            -- the uom is same, only need to recalculate the correct quantity
7133              --POS Batch Processing by profiles by ateotia (+)
7134            IF (l_ship_from_stock_flag = 'Y' AND l_offer_type = 'SCAN_DATA') THEN
7135            l_quantity := (l_line_amount * l_offer_quantity) / l_scan_value;
7136            l_quantity := ROUND(l_quantity,2);
7137             END IF;
7138            --POS Batch Processing by profiles by ateotia (-)
7139         END IF;
7140 
7141         l_line_tbl(l_counter).activity_type             := 'SPECIAL_PRICE';
7142         l_line_tbl(l_counter).activity_id               := l_request_header_id;
7143         l_line_tbl(l_counter).item_type                 := 'PRODUCT';
7144         l_line_tbl(l_counter).item_id                   := l_product_id;
7145         l_line_tbl(l_counter).quantity_uom              := l_uom_code;
7146         l_line_tbl(l_counter).quantity                  := l_quantity;
7147         l_line_tbl(l_counter).currency_code             := l_line_curr_code;
7148         l_line_tbl(l_counter).amount                    := l_line_amount;
7149         l_line_tbl(l_counter).claim_currency_amount     := l_new_line_amount;
7150 
7151         l_counter := l_counter + 1;
7152      END LOOP;
7153      CLOSE csr_batch_line;
7154      END IF;
7155 
7156      -- create claim
7157      l_claim_rec.cust_account_id       := l_cust_account_id;
7158      l_claim_rec.source_object_class   := 'SPECIAL_PRICE';
7159      l_claim_rec.source_object_id      := l_request_header_id;
7160      l_claim_rec.source_object_number  := l_request_number;
7161      l_claim_rec.batch_id              := p_resale_batch_id;
7162      l_claim_rec.batch_type            := OZF_Resale_Common_PVT.G_BATCH_REF_TYPE;
7163      l_claim_rec.currency_code         := l_claim_curr_code;
7164      l_claim_rec.amount                := l_claim_amount;
7165      l_claim_rec.payment_method        := l_payment_method;
7166      --bug # 6690147 julou FP 6276634(+)
7167      --For -ve claim the settlement method defined in trade profile is not valid.
7168      --So, hard coded to DEBIT_MEMO.
7169      IF l_claim_rec.amount < 0 AND l_payment_method IS NOT NULL AND l_payment_method <> FND_API.G_MISS_CHAR THEN
7170         l_claim_rec.payment_method := 'DEBIT_MEMO';
7171      END IF;
7172      --bug # 6690147 (-)
7173      l_claim_rec.cust_billto_acct_site_id := l_cust_billto_acct_site_id;
7174      l_claim_rec.vendor_id             := l_vendor_id;
7175      l_claim_rec.vendor_site_id        := l_vendor_site_id;
7176      l_claim_rec.status_code           := 'OPEN';
7177      l_claim_rec.user_status_id        := to_number(
7178                                              ozf_utility_pvt.get_default_user_status(
7179                                              p_status_type   => 'OZF_CLAIM_STATUS',
7180                                              p_status_code   => l_claim_rec.status_code));
7181      -- save batch's partner claim number as customer reference
7182      l_claim_rec.customer_ref_number   := l_partner_claim_num;
7183 
7184      OZF_CLAIM_PVT.Create_Claim(
7185         p_api_version            => l_api_version
7186        ,x_return_status          => l_return_status
7187        ,x_msg_data               => x_msg_data
7188        ,x_msg_count              => x_msg_count
7189        ,p_claim                  => l_claim_rec
7190        ,x_claim_id               => l_claim_id);
7191      IF l_return_status = FND_API.G_RET_STS_ERROR THEN
7192         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7193      ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7194         RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7195      END IF;
7196 
7197      IF OZF_DEBUG_HIGH_ON THEN
7198         OZF_Utility_PVT.debug_message('Created claim id:'||l_claim_id);
7199      END IF;
7200 
7201      FOR i IN l_line_tbl.FIRST..l_line_tbl.LAST LOOP
7202         IF l_line_tbl.exists(i) IS NOT NULL THEN
7203            l_line_tbl(i).claim_id := l_claim_id;
7204         END IF;
7205      END LOOP;
7206 
7207      OZF_CLAIM_LINE_PVT.Create_Claim_Line_Tbl(
7208         p_api_version       => 1.0
7209        ,p_init_msg_list     => FND_API.g_false
7210        ,p_commit            => FND_API.g_false
7211        ,p_validation_level  => FND_API.g_valid_level_full
7212        ,x_return_status     => l_return_status
7213        ,x_msg_count         => x_msg_count
7214        ,x_msg_data          => x_msg_data
7215        ,p_claim_line_tbl    => l_line_tbl
7216        ,x_error_index       => l_error_index);
7217      IF l_return_status = FND_API.g_ret_sts_error THEN
7218         RAISE FND_API.g_exc_error;
7219      ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
7220         RAISE FND_API.g_exc_error;
7221      END IF;
7222 
7223      IF OZF_DEBUG_HIGH_ON THEN
7224         OZF_Utility_PVT.debug_message('Claim lines created for claim_id=' || l_claim_id);
7225      END IF;
7226 
7227      OPEN csr_claim_line(l_claim_id);
7228      LOOP
7229         FETCH csr_claim_line INTO l_claim_line_id;
7230         EXIT WHEN csr_claim_line%NOTFOUND;
7231 
7232         Asso_Accruals_To_Claim_Line(
7233            p_api_version         => 1.0
7234           ,p_init_msg_list       => FND_API.g_false
7235           ,p_commit              => FND_API.g_false
7236           ,p_validation_level    => FND_API.G_VALID_LEVEL_FULL
7237           ,x_return_status       => l_return_status
7238           ,x_msg_count           => x_msg_count
7239           ,x_msg_data            => x_msg_data
7240           ,p_claim_line_id       => l_claim_line_id
7241         );
7242 
7243         IF l_return_status = FND_API.g_ret_sts_error THEN
7244            RAISE FND_API.g_exc_error;
7245         ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
7246            RAISE FND_API.g_exc_error;
7247         END IF;
7248      END LOOP;
7249      CLOSE csr_claim_line;
7250 
7251      IF l_close_claim_flag = 'T' THEN
7252         Settle_Claim(
7253            p_claim_id            => l_claim_id
7254           ,x_return_status       => l_return_status
7255           ,x_msg_count           => x_msg_count
7256           ,x_msg_data            => x_msg_data);
7257         IF l_return_status =  fnd_api.g_ret_sts_error THEN
7258            RAISE FND_API.g_exc_error;
7259         ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
7260            RAISE FND_API.g_exc_unexpected_error;
7261         END IF;
7262      END IF;
7263 
7264   END LOOP;
7265   CLOSE csr_batch_request;
7266 
7267   ------------------------- finish -------------------------------
7268   IF FND_API.to_boolean(p_commit) THEN
7269     COMMIT;
7270   END IF;
7271 
7272   FND_MSG_PUB.count_and_get(
7273      p_encoded => FND_API.g_false,
7274      p_count   => x_msg_count,
7275      p_data    => x_msg_data);
7276 
7277   IF OZF_DEBUG_HIGH_ON THEN
7278      OZF_Utility_PVT.debug_message(l_full_name ||': end');
7279   END IF;
7280 
7281 EXCEPTION
7282   WHEN FND_API.g_exc_error THEN
7283     ROLLBACK TO Create_Claim_Per_SPR;
7284     x_return_status := FND_API.g_ret_sts_error;
7285     FND_MSG_PUB.count_and_get (
7286            p_encoded => FND_API.g_false
7287           ,p_count   => x_msg_count
7288           ,p_data    => x_msg_data
7289     );
7290 
7291   WHEN FND_API.g_exc_unexpected_error THEN
7292     ROLLBACK TO Create_Claim_Per_SPR;
7293     x_return_status := FND_API.g_ret_sts_unexp_error ;
7294     FND_MSG_PUB.count_and_get (
7295            p_encoded => FND_API.g_false
7296           ,p_count   => x_msg_count
7297           ,p_data    => x_msg_data
7298     );
7299 
7300   WHEN OTHERS THEN
7301     ROLLBACK TO Create_Claim_Per_SPR;
7302     x_return_status := FND_API.g_ret_sts_unexp_error ;
7303     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
7304       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
7305     END IF;
7306     FND_MSG_PUB.count_and_get(
7307            p_encoded => FND_API.g_false
7308           ,p_count   => x_msg_count
7309           ,p_data    => x_msg_data
7310     );
7311 
7312 END Create_Claim_Per_SPR;
7313 
7314 ---------------------------------------------------------------------
7315 -- PROCEDURE
7316 --    Create_Claim_Per_Batch
7317 --
7318 -- PURPOSE
7319 --    Create consolidated claim for all SPR in Ship and Debit batch.
7320 --
7321 -- PARAMETERS
7322 --    p_resale_batch_id: resale batch id
7323 --
7324 -- NOTES
7325 --
7326 -- HISTORY
7327 -- 03-Jun-09  ateotia  Created.
7328 --                     Bug# 8571085 fixed.
7329 ---------------------------------------------------------------------
7330 PROCEDURE Create_Claim_Per_Batch(
7331    p_api_version       IN  NUMBER
7332   ,p_init_msg_list     IN  VARCHAR2 := FND_API.g_false
7333   ,p_commit            IN  VARCHAR2 := FND_API.g_false
7334   ,p_validation_level  IN  NUMBER   := FND_API.g_valid_level_full
7335   ,x_return_status     OUT NOCOPY VARCHAR2
7336   ,x_msg_count         OUT NOCOPY NUMBER
7337   ,x_msg_data          OUT NOCOPY VARCHAR2
7338   ,p_resale_batch_id   IN  NUMBER
7339 )
7340 IS
7341 l_api_version    CONSTANT  NUMBER       := 1.0;
7342 l_api_name       CONSTANT  VARCHAR2(30) := 'Create_Claim_Per_Batch';
7343 l_full_name      CONSTANT  VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
7344 l_return_status            VARCHAR2(1);
7345 l_cust_account_id          NUMBER;
7346 l_partner_id               NUMBER;
7347 l_claim_curr_code          VARCHAR2(15);
7348 l_line_curr_code           VARCHAR2(15);
7349 l_request_header_id        NUMBER;
7350 l_request_number           VARCHAR2(30);
7351 l_agreement_number         VARCHAR2(240);
7352 l_product_id               NUMBER;
7353 l_uom_code                 VARCHAR2(3);
7354 l_quantity                 NUMBER;
7355 l_line_amount              NUMBER;
7356 l_new_line_amount          NUMBER;
7357 l_claim_amount             NUMBER;
7358 l_claim_id                 NUMBER;
7359 l_claim_rec                OZF_CLAIM_PVT.claim_rec_type;
7360 l_line_tbl                 OZF_CLAIM_LINE_PVT.claim_line_tbl_type;
7361 l_cust_trade_profile       g_cust_trade_profile_csr%rowtype;
7362 l_payment_method           VARCHAR2(30);
7363 l_cust_billto_acct_site_id NUMBER;
7364 l_vendor_id                NUMBER;
7365 l_vendor_site_id           NUMBER;
7366 l_claim_line_id            NUMBER;
7367 l_party_name               VARCHAR2(360);
7368 l_close_claim_flag         VARCHAR2(1);
7369 l_partner_claim_num        VARCHAR2(30);
7370 l_counter                  PLS_INTEGER := 1;
7371 l_error_index              NUMBER;
7372 l_scan_value               NUMBER;
7373 l_offer_uom_code           VARCHAR2(3);
7374 l_offer_quantity           NUMBER;
7375 l_trans_curr_code          VARCHAR2(15);
7376 l_new_claim_quantity       NUMBER;
7377 l_exchange_rate_type       VARCHAR2(30) := FND_API.G_MISS_CHAR;
7378 l_rate                     NUMBER;
7379 
7380 --POS Batch Processing by profiles by ateotia (+)
7381 l_ship_from_stock_flag     VARCHAR2(1);
7382 l_offer_type               VARCHAR2(30);
7383 --POS Batch Processing by profiles by ateotia (-)
7384 
7385 
7386 CURSOR csr_resale_batch(cv_batch_id IN NUMBER) IS
7387   SELECT partner_cust_account_id
7388        , partner_id
7389        , currency_code
7390        , partner_claim_number
7391   FROM ozf_resale_batches
7392   WHERE resale_batch_id = cv_batch_id;
7393 
7394 CURSOR csr_batch_line(cv_batch_id IN NUMBER, cv_partner_id IN NUMBER) IS
7395   SELECT r.request_header_id
7396        , r.request_number
7397       --POS Batch Processing by profiles by ateotia (+)
7398        , r.ship_from_stock_flag
7399        , r.offer_type
7400       --POS Batch Processing by profiles by ateotia (-)
7401        , s.agreement_name
7402        , s.inventory_item_id
7403        , s.uom_code
7404        , s.currency_code
7405        , sum(s.quantity)
7406        , sum(s.total_accepted_amount)
7407 
7408   FROM   ozf_resale_lines_int_all s,
7409          ozf_request_headers_all_b r
7410   WHERE  s.resale_batch_id = cv_batch_id
7411   AND    s.status_code = 'PROCESSED'
7412   AND    s.agreement_name = r.agreement_number
7413   AND    r.partner_id = cv_partner_id
7414   AND    r.status_code = 'APPROVED'
7415   AND    r.request_class = 'SPECIAL_PRICE'
7416   GROUP BY
7417          r.request_header_id
7418        , r.request_number
7419        , s.agreement_name
7420        , s.inventory_item_id
7421        , s.uom_code
7422        , s.currency_code
7423        , r.ship_from_stock_flag
7424        , r.offer_type  ;
7425 
7426 
7427 -- added for Bugfix 5404951
7428 CURSOR csr_ams_act_products(cv_agreement_number IN VARCHAR2 ,cv_product_id IN NUMBER) IS
7429   SELECT act.SCAN_VALUE
7430        , act.UOM_CODE
7431        , act.Quantity
7432        , off.TRANSACTION_CURRENCY_CODE
7433   FROM ozf_offers off
7434      , ams_act_products act
7435   WHERE offer_code  = cv_agreement_number
7436   AND   ARC_ACT_PRODUCT_USED_BY = 'OFFR'
7437   AND   ACT_PRODUCT_USED_BY_ID = off.qp_list_header_id
7438   AND   INVENTORY_ITEM_ID = cv_product_id;
7439 
7440 CURSOR csr_claim_line(cv_claim_id IN NUMBER) IS
7441   SELECT claim_line_id
7442   FROM ozf_claim_lines
7443   WHERE claim_id = cv_claim_id;
7444 
7445 CURSOR csr_party_name(cv_cust_account_id IN NUMBER) IS
7446   SELECT p.party_name
7447   FROM hz_parties p
7448      , hz_cust_accounts c
7449   WHERE p.party_id = c.party_id
7450   AND   c.cust_account_id = cv_cust_account_id;
7451 
7452 CURSOR c_get_conversion_type IS
7453   SELECT exchange_rate_type
7454   FROM   ozf_sys_parameters_all
7455   WHERE  org_id = MO_GLOBAL.GET_CURRENT_ORG_ID;
7456 
7457    --POS Batch Processing by profiles by rsatyava (+)
7458   l_auto_claim_profile varchar2(1):= FND_PROFILE.value('OZF_AUTO_CLAIM_POS');
7459   --POS Batch Processing by profiles by rsatyava (+)
7460 
7461 
7462 BEGIN
7463   --------------------- initialize -----------------------
7464   SAVEPOINT Create_Claim_Per_Batch;
7465 
7466   IF OZF_DEBUG_HIGH_ON THEN
7467      OZF_Utility_PVT.debug_message(l_full_name||': start');
7468   END IF;
7469 
7470   IF FND_API.to_boolean(p_init_msg_list) THEN
7471     FND_MSG_PUB.initialize;
7472   END IF;
7473 
7474   IF NOT FND_API.compatible_api_call(
7475          l_api_version,
7476          p_api_version,
7477          l_api_name,
7478          g_pkg_name
7479   ) THEN
7480     RAISE FND_API.g_exc_unexpected_error;
7481   END IF;
7482 
7483   x_return_status := FND_API.g_ret_sts_success;
7484 
7485   ----------------- Process batch ----------------
7486   OPEN csr_resale_batch(p_resale_batch_id);
7487   FETCH csr_resale_batch INTO l_cust_account_id
7488                             , l_partner_id
7489                             , l_claim_curr_code
7490                             , l_partner_claim_num;
7491   CLOSE csr_resale_batch;
7492 
7493   IF OZF_DEBUG_HIGH_ON THEN
7494     OZF_Utility_PVT.debug_message('Process batch for customer id '||l_cust_account_id);
7495   END IF;
7496 
7497   l_close_claim_flag := 'T';
7498 
7499   -- get payment method information from trade profile
7500   Get_Cust_Trade_Profile(
7501       p_cust_account_id     => l_cust_account_id
7502      ,x_cust_trade_profile  => l_cust_trade_profile
7503   );
7504 
7505   IF l_cust_trade_profile.trade_profile_id IS NOT NULL THEN
7506      Validate_Cust_Trade_Profile(
7507         p_cust_trade_profile  => l_cust_trade_profile
7508        ,x_return_status       => l_return_status
7509      );
7510 
7511      -- do not settle claim if trade profile has errors
7512      IF l_return_status = FND_API.g_ret_sts_error or
7513         l_return_status = FND_API.g_ret_sts_unexp_error THEN
7514         l_close_claim_flag := 'F';
7515      END IF;
7516 
7517      l_payment_method := l_cust_trade_profile.payment_method;
7518      l_cust_billto_acct_site_id := l_cust_trade_profile.site_use_id;
7519      l_vendor_id := l_cust_trade_profile.vendor_id;
7520      l_vendor_site_id := l_cust_trade_profile.vendor_site_id;
7521   ELSE
7522      -- do not settle claim if trade profile does not exists
7523      l_close_claim_flag := 'F';
7524   END IF;
7525 
7526   l_claim_amount := 0;
7527   l_counter := 1;
7528 
7529   --Added for 7360703
7530   IF l_line_tbl.EXISTS(1) THEN
7531      l_line_tbl.DELETE;
7532   END IF;
7533 
7534   OPEN csr_batch_line(p_resale_batch_id, l_partner_id);
7535   LOOP
7536      FETCH csr_batch_line INTO l_request_header_id
7537                              , l_request_number
7538                              --POS Batch Processing by profiles by ateotia (+)
7539                              , l_ship_from_stock_flag
7540                              , l_offer_type
7541                              --POS Batch Processing by profiles by ateotia (-)
7542                              , l_agreement_number
7543                              , l_product_id
7544                              , l_uom_code
7545                              , l_line_curr_code
7546                              , l_quantity
7547                              , l_line_amount;
7548 
7549 
7550      EXIT WHEN csr_batch_line%NOTFOUND;
7551 
7552 -- Added logic to create claim line only when the auto claim profile is set/offer type is scan data
7553   IF(l_auto_claim_profile = 'Y') OR (l_offer_type='SCAN_DATA') THEN
7554      OPEN csr_ams_act_products(l_agreement_number ,l_product_id );
7555      FETCH csr_ams_act_products INTO l_scan_value
7556                                    , l_offer_uom_code
7557                                    , l_offer_quantity
7558                                    , l_trans_curr_code;
7559      CLOSE csr_ams_act_products;
7560 
7561      -- calculate claim amount
7562      IF l_line_curr_code = l_claim_curr_code THEN
7563         l_new_line_amount := l_line_amount;
7564      ELSE
7565         --Added for bug 7030415, get exchange_rate type
7566         OPEN c_get_conversion_type;
7567         FETCH c_get_conversion_type INTO l_exchange_rate_type;
7568         CLOSE c_get_conversion_type;
7569         OZF_UTILITY_PVT.Convert_Currency(
7570              p_from_currency   => l_line_curr_code
7571             ,p_to_currency     => l_claim_curr_code
7572             ,p_conv_type       => l_exchange_rate_type
7573             ,p_conv_date       => SYSDATE
7574             ,p_from_amount     => l_line_amount
7575             ,x_return_status   => l_return_status
7576             ,x_to_amount       => l_new_line_amount
7577             ,x_rate            => l_rate);
7578         IF l_return_status = FND_API.g_ret_sts_error THEN
7579            RAISE FND_API.g_exc_error;
7580         ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
7581            RAISE FND_API.g_exc_unexpected_error;
7582         END IF;
7583      END IF;
7584      l_claim_amount := l_claim_amount + l_new_line_amount;
7585 
7586      -- Bugfix 5404951
7587      IF l_offer_uom_code <> l_uom_code THEN
7588              --POS Batch Processing by profiles by ateotia (+)
7589         IF (l_ship_from_stock_flag = 'Y' AND l_offer_type = 'ACCRUAL') THEN
7590            l_new_claim_quantity := l_quantity;
7591         ELSE
7592         l_new_claim_quantity := (l_line_amount * l_offer_quantity) / l_scan_value ; -- correct qty in offer_uom
7593         END IF;
7594         --POS Batch Processing by profiles by ateotia (-)
7595 
7596         -- convert this to claim line uom
7597         l_quantity := inv_convert.inv_um_convert(
7598                                 item_id         => l_product_id
7599                                ,precision       => 2
7600                                ,from_quantity   => l_new_claim_quantity
7601                                ,from_unit       => l_offer_uom_code
7602                                ,to_unit         => l_uom_code
7603                                ,from_name       => NULL
7604                                ,to_name         => NULL
7605                                );
7606          IF l_quantity = -99999 THEN
7607             IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
7608                FND_MESSAGE.set_name('OZF', 'OZF_CLAIM_CANT_CONVERT_UOM');
7609                FND_MSG_PUB.add;
7610             END IF;
7611             RAISE FND_API.g_exc_error;
7612          END IF;
7613 
7614      ELSE
7615         -- the uom is same, only need to recalculate the correct quantity
7616         --POS Batch Processing by profiles by ateotia (+)
7617         IF (l_ship_from_stock_flag = 'Y' AND l_offer_type = 'SCAN_DATA') THEN
7618            l_quantity := (l_line_amount * l_offer_quantity) / l_scan_value;
7619            l_quantity := ROUND(l_quantity,2);
7620         END IF;
7621         --POS Batch Processing by profiles by ateotia (-)
7622 
7623      END IF;
7624 
7625      l_line_tbl(l_counter).activity_type             := 'SPECIAL_PRICE';
7626      l_line_tbl(l_counter).activity_id               := l_request_header_id;
7627      l_line_tbl(l_counter).item_type                 := 'PRODUCT';
7628      l_line_tbl(l_counter).item_id                   := l_product_id;
7629      l_line_tbl(l_counter).quantity_uom              := l_uom_code;
7630      l_line_tbl(l_counter).quantity                  := l_quantity;
7631      l_line_tbl(l_counter).currency_code             := l_line_curr_code;
7632      l_line_tbl(l_counter).amount                    := l_line_amount;
7633      l_line_tbl(l_counter).claim_currency_amount     := l_new_line_amount;
7634 
7635      l_counter := l_counter + 1;
7636        END IF;
7637   END LOOP;
7638   CLOSE csr_batch_line;
7639 
7640   -- create claim
7641   l_claim_rec.cust_account_id       := l_cust_account_id;
7642   l_claim_rec.source_object_class   := 'SPECIAL_PRICE';
7643   l_claim_rec.source_object_id      := NULL;
7644   l_claim_rec.source_object_number  := NULL;
7645   l_claim_rec.batch_id              := p_resale_batch_id;
7646   l_claim_rec.batch_type            := OZF_Resale_Common_PVT.G_BATCH_REF_TYPE;
7647   l_claim_rec.currency_code         := l_claim_curr_code;
7648   l_claim_rec.amount                := l_claim_amount;
7649   l_claim_rec.payment_method        := l_payment_method;
7650   --bug # 6690147 julou FP 6276634(+)
7651   --For -ve claim the settlement method defined in trade profile is not valid.
7652   --So, hard coded to DEBIT_MEMO.
7653   IF l_claim_rec.amount < 0 AND l_payment_method IS NOT NULL AND l_payment_method <> FND_API.G_MISS_CHAR THEN
7654      l_claim_rec.payment_method := 'DEBIT_MEMO';
7655   END IF;
7656   --bug # 6690147 (-)
7657   l_claim_rec.cust_billto_acct_site_id := l_cust_billto_acct_site_id;
7658   l_claim_rec.vendor_id             := l_vendor_id;
7659   l_claim_rec.vendor_site_id        := l_vendor_site_id;
7660   l_claim_rec.status_code           := 'OPEN';
7661   l_claim_rec.user_status_id        := to_number(
7662                                           ozf_utility_pvt.get_default_user_status(
7663                                              p_status_type   => 'OZF_CLAIM_STATUS',
7664                                              p_status_code   => l_claim_rec.status_code));
7665   -- save batch's partner claim number as customer reference
7666   l_claim_rec.customer_ref_number   := l_partner_claim_num;
7667 
7668   OZF_CLAIM_PVT.Create_Claim(
7669        p_api_version            => l_api_version
7670       ,x_return_status          => l_return_status
7671       ,x_msg_data               => x_msg_data
7672       ,x_msg_count              => x_msg_count
7673       ,p_claim                  => l_claim_rec
7674       ,x_claim_id               => l_claim_id);
7675   IF l_return_status = FND_API.G_RET_STS_ERROR THEN
7676      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7677   ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7678      RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7679   END IF;
7680 
7681   IF OZF_DEBUG_HIGH_ON THEN
7682      OZF_Utility_PVT.debug_message('Created claim id:'||l_claim_id);
7683   END IF;
7684 
7685   FOR i IN l_line_tbl.FIRST..l_line_tbl.LAST LOOP
7686      IF l_line_tbl.exists(i) IS NOT NULL THEN
7687         l_line_tbl(i).claim_id := l_claim_id;
7688      END IF;
7689   END LOOP;
7690 
7691   OZF_CLAIM_LINE_PVT.Create_Claim_Line_Tbl(
7692        p_api_version       => 1.0
7693       ,p_init_msg_list     => FND_API.g_false
7694       ,p_commit            => FND_API.g_false
7695       ,p_validation_level  => FND_API.g_valid_level_full
7696       ,x_return_status     => l_return_status
7697       ,x_msg_count         => x_msg_count
7698       ,x_msg_data          => x_msg_data
7699       ,p_claim_line_tbl    => l_line_tbl
7700       ,x_error_index       => l_error_index);
7701   IF l_return_status = FND_API.g_ret_sts_error THEN
7702      RAISE FND_API.g_exc_error;
7703   ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
7704      RAISE FND_API.g_exc_error;
7705   END IF;
7706 
7707   IF OZF_DEBUG_HIGH_ON THEN
7708      OZF_Utility_PVT.debug_message('Claim lines created for claim_id=' || l_claim_id);
7709   END IF;
7710 
7711   OPEN csr_claim_line(l_claim_id);
7712   LOOP
7713      FETCH csr_claim_line INTO l_claim_line_id;
7714      EXIT WHEN csr_claim_line%NOTFOUND;
7715 
7716      Asso_Accruals_To_Claim_Line(
7717          p_api_version         => 1.0
7718         ,p_init_msg_list       => FND_API.g_false
7719         ,p_commit              => FND_API.g_false
7720         ,p_validation_level    => FND_API.G_VALID_LEVEL_FULL
7721         ,x_return_status       => l_return_status
7722         ,x_msg_count           => x_msg_count
7723         ,x_msg_data            => x_msg_data
7724         ,p_claim_line_id       => l_claim_line_id);
7725      IF l_return_status = FND_API.g_ret_sts_error THEN
7726         RAISE FND_API.g_exc_error;
7727      ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
7728         RAISE FND_API.g_exc_error;
7729      END IF;
7730   END LOOP;
7731   CLOSE csr_claim_line;
7732 
7733   IF l_close_claim_flag = 'T' THEN
7734      Settle_Claim(
7735          p_claim_id            => l_claim_id
7736         ,x_return_status       => l_return_status
7737         ,x_msg_count           => x_msg_count
7738         ,x_msg_data            => x_msg_data);
7739      IF l_return_status =  fnd_api.g_ret_sts_error THEN
7740         RAISE FND_API.g_exc_error;
7741      ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
7742         RAISE FND_API.g_exc_unexpected_error;
7743      END IF;
7744   END IF;
7745 
7746   ------------------------- finish -------------------------------
7747   IF FND_API.to_boolean(p_commit) THEN
7748      COMMIT;
7749   END IF;
7750 
7751   FND_MSG_PUB.count_and_get(
7752          p_encoded => FND_API.g_false,
7753          p_count   => x_msg_count,
7754          p_data    => x_msg_data);
7755 
7756   IF OZF_DEBUG_HIGH_ON THEN
7757      OZF_Utility_PVT.debug_message(l_full_name ||': end');
7758   END IF;
7759 
7760 EXCEPTION
7761   WHEN FND_API.g_exc_error THEN
7762     ROLLBACK TO Create_Claim_Per_Batch;
7763     x_return_status := FND_API.g_ret_sts_error;
7764     FND_MSG_PUB.count_and_get (
7765            p_encoded => FND_API.g_false
7766           ,p_count   => x_msg_count
7767           ,p_data    => x_msg_data
7768     );
7769 
7770   WHEN FND_API.g_exc_unexpected_error THEN
7771     ROLLBACK TO Create_Claim_Per_Batch;
7772     x_return_status := FND_API.g_ret_sts_unexp_error ;
7773     FND_MSG_PUB.count_and_get (
7774            p_encoded => FND_API.g_false
7775           ,p_count   => x_msg_count
7776           ,p_data    => x_msg_data
7777     );
7778 
7779   WHEN OTHERS THEN
7780     ROLLBACK TO Create_Claim_Per_Batch;
7781     x_return_status := FND_API.g_ret_sts_unexp_error ;
7782     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
7783       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
7784     END IF;
7785     FND_MSG_PUB.count_and_get(
7786            p_encoded => FND_API.g_false
7787           ,p_count   => x_msg_count
7788           ,p_data    => x_msg_data
7789     );
7790 
7791 END Create_Claim_Per_Batch;
7792 
7793 ---------------------------------------------------------------------
7794 -- PROCEDURE
7795 --    Initiate_Batch_Payment
7796 --
7797 -- PURPOSE
7798 --    Create claim for ship and debit batch based on
7799 --    'OZF: Consolidate Ship and Debit Claim' profile.
7800 --
7801 -- PARAMETERS
7802 --    p_resale_batch_id: resale batch id
7803 --
7804 -- NOTES
7805 --
7806 -- HISTORY
7807 -- 03-Jun-09  ateotia  Bug# 8571085 fixed.
7808 ---------------------------------------------------------------------
7809 PROCEDURE Initiate_Batch_Payment(
7810    p_api_version       IN  NUMBER
7811   ,p_init_msg_list     IN  VARCHAR2 := FND_API.g_false
7812   ,p_commit            IN  VARCHAR2 := FND_API.g_false
7813   ,p_validation_level  IN  NUMBER   := FND_API.g_valid_level_full
7814   ,x_return_status     OUT NOCOPY VARCHAR2
7815   ,x_msg_count         OUT NOCOPY NUMBER
7816   ,x_msg_data          OUT NOCOPY VARCHAR2
7817   ,p_resale_batch_id   IN  NUMBER
7818 )
7819 IS
7820 l_api_version    CONSTANT  NUMBER       := 1.0;
7821 l_api_name       CONSTANT  VARCHAR2(30) := 'Initiate_Batch_Payment';
7822 l_full_name      CONSTANT  VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
7823 l_return_status            VARCHAR2(1);
7824 l_consolidate_flag         VARCHAR2(1);
7825 
7826 BEGIN
7827   --------------------- initialize -----------------------
7828   SAVEPOINT Initiate_Batch_Payment;
7829 
7830   IF OZF_DEBUG_HIGH_ON THEN
7831      OZF_Utility_PVT.debug_message(l_full_name||': start');
7832   END IF;
7833 
7834   IF FND_API.to_boolean(p_init_msg_list) THEN
7835     FND_MSG_PUB.initialize;
7836   END IF;
7837 
7838   IF NOT FND_API.compatible_api_call(
7839          l_api_version,
7840          p_api_version,
7841          l_api_name,
7842          g_pkg_name
7843   ) THEN
7844     RAISE FND_API.g_exc_unexpected_error;
7845   END IF;
7846 
7847   x_return_status := FND_API.g_ret_sts_success;
7848 
7849   ----------------- Process batch ----------------
7850   l_consolidate_flag  := FND_PROFILE.VALUE('OZF_CONSOLIDATE_SHIP_DEBIT_CLAIM');
7851   IF NVL(l_consolidate_flag,'N') = 'N' THEN
7852      Create_Claim_Per_SPR(
7853          p_api_version      => 1.0
7854         ,p_init_msg_list    => FND_API.g_false
7855         ,p_commit           => FND_API.g_false
7856         ,p_validation_level => FND_API.g_valid_level_full
7857         ,x_return_status    => l_return_status
7858         ,x_msg_count        => x_msg_count
7859         ,x_msg_data         => x_msg_data
7860         ,p_resale_batch_id  => p_resale_batch_id
7861       );
7862 
7863       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
7864          RAISE FND_API.G_EXC_ERROR;
7865       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7866          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7867       END IF;
7868 
7869   ELSIF NVL(l_consolidate_flag,'N') = 'Y' THEN
7870      Create_Claim_Per_Batch(
7871          p_api_version      => 1.0
7872         ,p_init_msg_list    => FND_API.g_false
7873         ,p_commit           => FND_API.g_false
7874         ,p_validation_level => FND_API.g_valid_level_full
7875         ,x_return_status    => l_return_status
7876         ,x_msg_count        => x_msg_count
7877         ,x_msg_data         => x_msg_data
7878         ,p_resale_batch_id  => p_resale_batch_id
7879       );
7880 
7881       IF l_return_status = FND_API.G_RET_STS_ERROR THEN
7882          RAISE FND_API.G_EXC_ERROR;
7883       ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
7884          RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
7885       END IF;
7886 
7887   END IF;
7888   --------------------- finish -------------------
7889   IF FND_API.to_boolean(p_commit) THEN
7890     COMMIT;
7891   END IF;
7892 
7893   FND_MSG_PUB.count_and_get(
7894          p_encoded => FND_API.g_false,
7895          p_count   => x_msg_count,
7896          p_data    => x_msg_data
7897   );
7898 
7899   IF OZF_DEBUG_HIGH_ON THEN
7900      OZF_Utility_PVT.debug_message(l_full_name ||': end');
7901   END IF;
7902 
7903 EXCEPTION
7904   WHEN FND_API.g_exc_error THEN
7905     ROLLBACK TO Initiate_Batch_Payment;
7906     x_return_status := FND_API.g_ret_sts_error;
7907     FND_MSG_PUB.count_and_get (
7908            p_encoded => FND_API.g_false
7909           ,p_count   => x_msg_count
7910           ,p_data    => x_msg_data
7911     );
7912 
7913   WHEN FND_API.g_exc_unexpected_error THEN
7914     ROLLBACK TO Initiate_Batch_Payment;
7915     x_return_status := FND_API.g_ret_sts_unexp_error ;
7916     FND_MSG_PUB.count_and_get (
7917            p_encoded => FND_API.g_false
7918           ,p_count   => x_msg_count
7919           ,p_data    => x_msg_data
7920     );
7921 
7922   WHEN OTHERS THEN
7923     ROLLBACK TO Initiate_Batch_Payment;
7924     x_return_status := FND_API.g_ret_sts_unexp_error ;
7925     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
7926       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
7927     END IF;
7928     FND_MSG_PUB.count_and_get(
7929            p_encoded => FND_API.g_false
7930           ,p_count   => x_msg_count
7931           ,p_data    => x_msg_data
7932     );
7933 
7934 END Initiate_Batch_Payment;
7935 
7936 ---------------------------------------------------------------------
7937 -- PROCEDURE
7938 --    Validate_Over_Utilization
7939 --
7940 -- HISTORY
7941 --    10/15/2002  yizhang  Create.
7942 ---------------------------------------------------------------------
7943 PROCEDURE Validate_Over_Utilization(
7944    p_api_version        IN  NUMBER
7945   ,p_init_msg_list      IN  VARCHAR2  := FND_API.g_false
7946   ,p_validation_level   IN  NUMBER    := FND_API.g_valid_level_full
7947 
7948   ,x_return_status      OUT NOCOPY VARCHAR2
7949   ,x_msg_count          OUT NOCOPY NUMBER
7950   ,x_msg_data           OUT NOCOPY VARCHAR2
7951   ,p_currency_rec       IN  currency_rec_type
7952   ,p_funds_util_flt     IN  funds_util_flt_type
7953   ,p_over_paid_amount   IN  NUMBER --nepanda : fix for bug # 9508390  - issue # 3
7954 )
7955 IS
7956 l_api_version CONSTANT NUMBER       := 1.0;
7957 l_api_name    CONSTANT VARCHAR2(30) := 'Validate_Over_Utilization';
7958 l_full_name   CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
7959 l_return_status VARCHAR2(1);
7960 
7961 l_adjustment_flag    VARCHAR2(1);
7962 l_count_workflow     NUMBER;
7963 
7964 l_un_earned_pay_allow_to     VARCHAR2(30);
7965 l_un_earned_pay_thold_type   VARCHAR2(30);
7966 l_un_earned_pay_thold_amount NUMBER;
7967 l_un_earned_pay_thold_flag   VARCHAR2(1);
7968 l_threshold_amount           NUMBER;
7969 l_over_paid_amount           NUMBER;
7970 l_trd_prf_exist              BOOLEAN;
7971 l_total_amt_earned           NUMBER;
7972 l_total_amt_remaining        NUMBER;
7973 l_currOrgId                  NUMBER     := MO_GLOBAL.GET_CURRENT_ORG_ID();
7974 
7975 CURSOR csr_auto_adjust(cv_activity_product_id IN NUMBER) IS
7976   SELECT adjustment_flag
7977   FROM ams_act_products
7978   WHERE activity_product_id = cv_activity_product_id;
7979 
7980 CURSOR csr_count_workflow(cv_claim_line_id IN NUMBER) IS
7981   SELECT count(*)
7982   FROM ozf_claim_lines_util lu, ams_act_products ap
7983       ,ozf_claim_lines cln1, ozf_claim_lines cln2
7984   WHERE cln1.claim_line_id = cv_claim_line_id
7985   AND   cln1.claim_id = cln2.claim_id
7986   AND   lu.claim_line_id = cln2.claim_line_id
7987   AND   lu.activity_product_id = ap.activity_product_id
7988   AND   lu.utilization_id = -1
7989   AND   ap.adjustment_flag = 'N';
7990 
7991 CURSOR csr_cust_pay_unearned(cv_cust_account_id IN NUMBER) IS
7992   SELECT tp.un_earned_pay_allow_to, tp.un_earned_pay_thold_type
7993        , tp.un_earned_pay_thold_amount, tp.un_earned_pay_thold_flag
7994   FROM ozf_cust_trd_prfls tp
7995   WHERE tp.cust_account_id = cv_cust_account_id;
7996 
7997 CURSOR csr_party_pay_unearned(cv_cust_account_id IN NUMBER) IS
7998   SELECT tp.un_earned_pay_allow_to, tp.un_earned_pay_thold_type
7999        , tp.un_earned_pay_thold_amount, tp.un_earned_pay_thold_flag
8000   FROM ozf_cust_trd_prfls tp, hz_cust_accounts hca
8001   WHERE tp.party_id = hca.party_id
8002   AND   tp.cust_account_id IS NULL
8003   AND   hca.cust_account_id = cv_cust_account_id;
8004 
8005  -- fix for bug 5042046
8006 CURSOR csr_sys_pay_unearned IS
8007   SELECT un_earned_pay_allow_to, un_earned_pay_thold_type
8008        , un_earned_pay_thold_amount, un_earned_pay_thold_flag
8009   FROM ozf_sys_parameters
8010   WHERE  org_id = l_currOrgId;
8011 
8012 CURSOR csr_earnings(cv_cust_account_id IN NUMBER, cv_plan_id IN NUMBER) IS
8013   SELECT SUM(DECODE(p_currency_rec.association_currency_code,p_currency_rec.transaction_currency_code, plan_curr_amount,acctd_amount))
8014        , SUM(DECODE(p_currency_rec.association_currency_code,p_currency_rec.transaction_currency_code, plan_curr_amount_remaining,acctd_amount_remaining))
8015   FROM ozf_funds_utilized_all_b
8016   WHERE utilization_type IN ('ACCRUAL', 'ADJUSTMENT')
8017   AND org_id =l_currOrgId
8018   AND plan_type = 'OFFR'
8019   AND plan_id = cv_plan_id
8020   AND cust_account_id = cv_cust_account_id;
8021 
8022 
8023 BEGIN
8024   ----------------------- initialize --------------------
8025   IF OZF_DEBUG_HIGH_ON THEN
8026      OZF_Utility_PVT.debug_message(l_full_name||': start');
8027   END IF;
8028 
8029   IF FND_API.to_boolean(p_init_msg_list) THEN
8030     FND_MSG_PUB.initialize;
8031   END IF;
8032 
8033   IF NOT FND_API.compatible_api_call(
8034        l_api_version,
8035        p_api_version,
8036        l_api_name,
8037        g_pkg_name
8038   ) THEN
8039     RAISE FND_API.g_exc_unexpected_error;
8040   END IF;
8041 
8042   x_return_status := FND_API.g_ret_sts_success;
8043 
8044   ----------------------- validate ----------------------
8045   -- Scan Data over-utilization is configured in offer line
8046   IF p_funds_util_flt.offer_type = 'SCAN_DATA' THEN
8047     -- Check if auto-adjustment is set
8048     OPEN csr_auto_adjust(p_funds_util_flt.activity_product_id);
8049     FETCH csr_auto_adjust INTO l_adjustment_flag;
8050     CLOSE csr_auto_adjust;
8051 
8052     IF l_adjustment_flag = 'N' THEN
8053       -- As of 15-JAN-2003, adjustment workflow is not supported. An error is raised
8054       -- if the offer is not auto-adjustable.
8055       IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
8056         FND_MESSAGE.set_name('OZF', 'OZF_CLAIM_CANT_ADJUST_SCANUNIT');
8057         FND_MSG_PUB.add;
8058       END IF;
8059       x_return_status := FND_API.g_ret_sts_error;
8060       RETURN;
8061 
8062       /*
8063       -- Adjustment workflow can be invoked only when profile is set
8064       IF NVL(fnd_profile.value('OZF_CLAIM_SCAN_ADJUST_WORKFLOW'),'N') <> 'Y' THEN
8065         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
8066           FND_MESSAGE.set_name('OZF', 'OZF_CLAIM_NO_ADJUST_WORKFLOW');
8067           FND_MSG_PUB.add;
8068         END IF;
8069         x_return_status := FND_API.g_ret_sts_error;
8070         RETURN;
8071       END IF;
8072 
8073       -- At most one workflow can be invoked per claim
8074       OPEN csr_count_workflow(p_funds_util_flt.claim_line_id);
8075       FETCH csr_count_workflow INTO l_count_workflow;
8076       CLOSE csr_count_workflow;
8077 
8078       IF l_count_workflow > 0 THEN
8079         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
8080           FND_MESSAGE.set_name('OZF', 'OZF_CLAIM_MULTI_ADJ_WORKFLOW');
8081           FND_MSG_PUB.add;
8082         END IF;
8083         x_return_status := FND_API.g_ret_sts_error;
8084         RETURN;
8085       END IF;
8086       */
8087     END IF;
8088   ELSE
8089     -- non-scandata offers:
8090     -- offer must be specified for pay over earnings
8091     IF p_funds_util_flt.activity_id IS NULL OR
8092        p_funds_util_flt.activity_type IS NULL OR
8093        p_funds_util_flt.activity_type <> 'OFFR'
8094     THEN
8095       IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
8096         FND_MESSAGE.set_name('OZF', 'OZF_EARN_OVERPAY_NO_OFFER');
8097         FND_MSG_PUB.add;
8098       END IF;
8099       x_return_status := FND_API.g_ret_sts_error;
8100       RETURN;
8101     END IF;
8102 
8103     --eligibility is in trade profile or system parameter
8104     l_trd_prf_exist := FALSE;
8105 
8106     IF p_funds_util_flt.cust_account_id IS NOT NULL THEN
8107       -- first check cust account's trade profile
8108       OPEN csr_cust_pay_unearned(p_funds_util_flt.cust_account_id);
8109       FETCH csr_cust_pay_unearned INTO l_un_earned_pay_allow_to
8110                                      , l_un_earned_pay_thold_type
8111                                      , l_un_earned_pay_thold_amount
8112                                      , l_un_earned_pay_thold_flag;
8113       IF NOT csr_cust_pay_unearned%NOTFOUND THEN
8114         l_trd_prf_exist := TRUE;
8115       END IF;
8116       CLOSE csr_cust_pay_unearned;
8117 
8118       -- if account has no trade profile, check party's trade profile
8119       IF NOT l_trd_prf_exist THEN
8120         OPEN csr_party_pay_unearned(p_funds_util_flt.cust_account_id);
8121         FETCH csr_party_pay_unearned INTO l_un_earned_pay_allow_to
8122                                         , l_un_earned_pay_thold_type
8123                                         , l_un_earned_pay_thold_amount
8124                                         , l_un_earned_pay_thold_flag;
8125         IF NOT csr_party_pay_unearned%NOTFOUND THEN
8126           l_trd_prf_exist := TRUE;
8127         END IF;
8128         CLOSE csr_party_pay_unearned;
8129       END IF;
8130 
8131       IF l_un_earned_pay_allow_to = 'DISALLOW' THEN
8132         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
8133           FND_MESSAGE.set_name('OZF', 'OZF_EARN_OVERPAY_NOT_ALLOWED');
8134           FND_MSG_PUB.add;
8135         END IF;
8136         x_return_status := FND_API.g_ret_sts_error;
8137         RETURN;
8138       END IF;
8139     END IF;
8140 
8141     -- if no trade profile exists for the party, check sys parameters
8142     IF l_un_earned_pay_allow_to IS NULL THEN
8143       OPEN csr_sys_pay_unearned;
8144       FETCH csr_sys_pay_unearned INTO l_un_earned_pay_allow_to
8145                                     , l_un_earned_pay_thold_type
8146                                     , l_un_earned_pay_thold_amount
8147                                     , l_un_earned_pay_thold_flag;
8148       CLOSE csr_sys_pay_unearned;
8149 
8150       IF l_un_earned_pay_allow_to IS NULL OR
8151          l_un_earned_pay_allow_to = 'ALLOW_SELECTED'
8152       THEN
8153         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
8154           FND_MESSAGE.set_name('OZF', 'OZF_EARN_OVERPAY_NOT_ALLOWED');
8155           FND_MSG_PUB.add;
8156         END IF;
8157         x_return_status := FND_API.g_ret_sts_error;
8158         RETURN;
8159       END IF;
8160     END IF;
8161 
8162     IF l_un_earned_pay_thold_type IS NULL OR
8163        l_un_earned_pay_thold_type <> 'UNCONDITIONAL'
8164     THEN
8165       OPEN csr_earnings(p_funds_util_flt.cust_account_id, p_funds_util_flt.activity_id);
8166       FETCH csr_earnings INTO l_total_amt_earned, l_total_amt_remaining;
8167       CLOSE csr_earnings;
8168 
8169       IF l_un_earned_pay_thold_type = 'PERCENT' THEN
8170         l_threshold_amount := l_un_earned_pay_thold_amount * l_total_amt_earned / 100.0;
8171       ELSIF l_un_earned_pay_thold_type = 'AMOUNT' THEN
8172         l_threshold_amount := l_un_earned_pay_thold_amount;
8173       ELSE
8174         l_threshold_amount := 0.0;
8175       END IF;
8176 
8177     -- Fix for Bug 8716894
8178     --nepanda : fix for bug # 9508390  - issue # 3
8179     l_over_paid_amount := p_over_paid_amount; --p_funds_util_flt.total_amount - l_total_amt_remaining;
8180 
8181     IF OZF_DEBUG_HIGH_ON THEN
8182          OZF_Utility_PVT.debug_message('l_over_paid_amount :' || l_over_paid_amount);
8183     END IF;
8184 
8185       IF l_over_paid_amount > l_threshold_amount THEN
8186         IF l_un_earned_pay_thold_flag IS NULL OR l_un_earned_pay_thold_flag <> 'T' THEN
8187           IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
8188             FND_MESSAGE.set_name('OZF', 'OZF_EARN_OVERPAY_OVER_THOLD');
8189             FND_MSG_PUB.add;
8190           END IF;
8191           x_return_status := FND_API.g_ret_sts_error;
8192           RETURN;
8193         END IF;
8194       END IF;
8195     END IF;
8196   END IF;
8197 
8198   IF OZF_DEBUG_HIGH_ON THEN
8199     OZF_Utility_PVT.debug_message('l_un_earned_pay_allow_to = '||l_un_earned_pay_allow_to);
8200     OZF_Utility_PVT.debug_message('l_un_earned_pay_thold_type = '||l_un_earned_pay_thold_type);
8201     OZF_Utility_PVT.debug_message('l_over_paid_amount = '||l_over_paid_amount);
8202     OZF_Utility_PVT.debug_message('l_threshold_amount = '||l_threshold_amount);
8203   END IF;
8204 
8205   IF OZF_DEBUG_HIGH_ON THEN
8206      OZF_Utility_PVT.debug_message(l_full_name||': end');
8207   END IF;
8208 END Validate_Over_Utilization;
8209 
8210 ---------------------------------------------------------------------
8211 -- PROCEDURE
8212 --    Create_Pay_Over_Adjustments
8213 --
8214 -- HISTORY
8215 --    11/03/2009  psomyaju  Create.
8216 --    10/06/2009  kpatro    Fix for Bug#8583847
8217 ---------------------------------------------------------------------
8218 
8219 PROCEDURE Create_Pay_Over_Adjustments ( px_line_util_tbl         IN OUT NOCOPY line_util_tbl_type
8220                                       , p_funds_util_flt         funds_util_flt_type
8221                                       , p_tot_accrual_amt        NUMBER
8222                                       , p_pay_over_amount        NUMBER
8223                                       , p_prorate_earnings_flag  VARCHAR2
8224                                       , p_new_total_amount       NUMBER
8225 				      , p_util_quantity          NUMBER
8226 				      , p_new_total_units        NUMBER
8227 				      , p_offer_quantity         NUMBER
8228                                       , p_currency_rec           IN currency_rec_type
8229                                       , x_return_status          OUT NOCOPY VARCHAR2
8230                                       , x_msg_data               OUT NOCOPY VARCHAR2
8231                                       , x_msg_count              OUT NOCOPY NUMBER
8232                                       ) IS
8233 
8234 TYPE NumberTab   IS TABLE OF NUMBER;
8235 TYPE Varchar2Tab IS TABLE OF VARCHAR2(30);
8236 
8237 l_item_id     NumberTab;
8238 l_fund_id     NumberTab;
8239 l_accrued_amt NumberTab;
8240 l_level_type  Varchar2Tab;
8241 
8242 -- Bugfix 5059770
8243 -- Bugfix 5191444
8244 CURSOR csr_offer_products ( cv_offer_id           NUMBER
8245                           , cv_product_id         NUMBER
8246                           , cv_product_level_type VARCHAR2
8247                           , cv_cust_account_id    NUMBER
8248                           , cv_plan_currency_code VARCHAR2
8249                           ) IS
8250   SELECT  product_level_type
8251         , product_id
8252         , fund_id
8253         , SUM(amount)
8254   FROM  ozf_funds_utilized_all_b
8255   WHERE utilization_type = 'ACCRUAL'
8256     AND org_id = MO_GLOBAL.GET_CURRENT_ORG_ID()
8257     AND gl_posted_flag = 'Y'
8258     AND plan_type = 'OFFR'
8259     AND plan_id = cv_offer_id
8260     AND cust_account_id = cv_cust_account_id
8261     AND (cv_product_id IS NULL OR product_id = cv_product_id)
8262     AND (cv_product_level_type IS NULL OR product_level_type = cv_product_level_type)
8263     AND plan_currency_code = cv_plan_currency_code
8264   GROUP BY product_level_type,product_id,fund_id;
8265 
8266 CURSOR csr_funds_util_info(cv_utilization_id IN NUMBER) IS
8267   SELECT product_id, product_level_type, fund_id
8268   FROM ozf_funds_utilized_all_b
8269   WHERE utilization_id = cv_utilization_id;
8270 
8271 CURSOR csr_source_fund_tot ( cv_offer_id NUMBER ) IS
8272   SELECT  SUM(approved_original_amount)
8273   FROM    ozf_act_budgets a
8274   WHERE   transfer_type = 'REQUEST'
8275   AND     status_code = 'APPROVED' --ninarasi : fix for bug # 12611938
8276     AND   act_budget_used_by_id = cv_offer_id
8277     AND   EXISTS ( SELECT 1
8278                    FROM  ozf_funds_all_vl
8279                    WHERE fund_id = budget_source_id
8280                    AND   CURRENCY_CODE_TC = a.approved_in_currency);
8281 
8282 -- Bugfix 4493735
8283 CURSOR csr_source_fund(cv_offer_id NUMBER) IS
8284   SELECT budget_source_id
8285        , approved_original_amount
8286        , count(*) over () total_funds
8287   FROM   ozf_act_budgets a
8288   WHERE  transfer_type = 'REQUEST'
8289   AND    status_code = 'APPROVED' --ninarasi : fix for bug # 12611938
8290     AND  act_budget_used_by_id = cv_offer_id
8291     AND  EXISTS ( SELECT 1
8292                    FROM  ozf_funds_all_vl
8293                    WHERE fund_id = budget_source_id
8294                    AND   currency_code_tc = a.approved_in_currency);
8295 
8296 l_adj_util_id             NUMBER := 0;
8297 l_prorate_adj_amt         NUMBER := 0;
8298 l_tot_accrual_amt         NUMBER := 0;
8299 l_fund_tot_amount         NUMBER := 0;
8300 l_tot_prorate_adj_amt     NUMBER := 0;
8301 l_util_product_id         NUMBER := 0;
8302 l_util_fund_id            NUMBER := 0;
8303 l_util_product_level_type VARCHAR2(15);
8304 l_tot_amt_rem             NUMBER := 0;
8305 l_last_asso_index         NUMBER := 0;
8306 i                         NUMBER := 0;
8307 j                         NUMBER := 0;
8308 
8309 l_api_version   CONSTANT  NUMBER       := 1.0;
8310 l_api_name      CONSTANT  VARCHAR2(30) := 'Create_Pay_Over_Adjustments';
8311 l_full_name     CONSTANT  VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
8312 l_return_status           VARCHAR2(15);
8313 l_msg_count               NUMBER;
8314 l_msg_data                VARCHAR2(15);
8315 
8316 l_new_line_amount NUMBER := p_new_total_amount;
8317 l_total_units NUMBER := 0;
8318 l_total_quantity NUMBER := 0;
8319 
8320 BEGIN
8321 
8322 
8323   IF OZF_DEBUG_HIGH_ON THEN
8324     OZF_Utility_PVT.debug_message(l_full_name||': start');
8325   END IF;
8326 
8327   SAVEPOINT Create_Pay_Over_Adjustments;
8328 
8329   /*
8330   Adjustments Creation:
8331   For Scan Data offer, single adjustment should be created for entire pay over amount, since prorate is not applicable
8332   to Scan Data offers.
8333   If prorate flag is set and its NOT total pay over earnings case, then create adjustments on prorate basis, i.e.
8334   pay over amount must be distributed uniformly across utilizations based on total amount remaining of all qualified
8335   utilizations w.r.t. association amount for each utilization.
8336   If prorate flag is set and its total pay over earnings case, then entire pay over amount will be consider as pay over
8337   earnings amount. Identify all the utilizations on the basis of customer, offer and product combination and calculate
8338   adjustment amount such that entire pay over amount should be distribute uniformly among all the qualified utilzations
8339   w.r.t. each utilization amount.
8340   If prorate flag is not set, then calculate adjustment amount such that pay over amount should be distributed uniformly
8341   among all the funds associate with offer. If single fund associated with offer then single adjustment should be created
8342   for entire pay over amount.
8343   */
8344 
8345 
8346    l_new_line_amount := NVL (l_new_line_amount,0);
8347 
8348   j := px_line_util_tbl.COUNT + 1;
8349   l_last_asso_index := px_line_util_tbl.LAST;
8350 
8351   IF p_funds_util_flt.offer_type = 'SCAN_DATA' THEN
8352     --IF px_line_util_tbl.COUNT > 0 THEN --ninarasi fix for EA bug 14152217
8353 
8354       px_line_util_tbl(j).utilization_id := -1;
8355       px_line_util_tbl(j).amount  := p_pay_over_amount;
8356       px_line_util_tbl(j).scan_unit := p_new_total_units; --EA ninarasi fix for 14016226
8357       px_line_util_tbl(j).claim_line_id := p_funds_util_flt.claim_line_id;
8358       px_line_util_tbl(j).activity_product_id := p_funds_util_flt.activity_product_id;
8359       px_line_util_tbl(j).uom_code := p_funds_util_flt.uom_code;
8360       px_line_util_tbl(j).quantity := p_util_quantity; --EA
8361       px_line_util_tbl(j).update_from_tbl_flag := FND_API.g_true;
8362     --END IF;
8363   ELSE
8364   IF p_prorate_earnings_flag = 'T' THEN
8365     IF px_line_util_tbl.COUNT > 0 THEN
8366       FOR i IN px_line_util_tbl.FIRST..px_line_util_tbl.LAST
8367       LOOP
8368 
8369           --Fix for Bug 8583847
8370           IF SIGN(px_line_util_tbl(i).amount) = SIGN(NVL (l_new_line_amount,0)) THEN
8371            l_prorate_adj_amt := px_line_util_tbl(i).amount * (l_new_line_amount / p_tot_accrual_amt);
8372            l_prorate_adj_amt := OZF_UTILITY_PVT.CurrRound(l_prorate_adj_amt, p_currency_rec.association_currency_code);
8373 
8374 
8375                   OPEN  csr_funds_util_info(px_line_util_tbl(i).utilization_id);
8376                   FETCH csr_funds_util_info INTO l_util_product_id
8377                                                , l_util_product_level_type
8378                                                , l_util_fund_id;
8379                   CLOSE csr_funds_util_info;
8380 
8381           IF OZF_DEBUG_HIGH_ON THEN
8382             OZF_Utility_PVT.debug_message('px_line_util_tbl(i).amount = '||px_line_util_tbl(i).amount);
8383             OZF_Utility_PVT.debug_message('p_pay_over_amount = '||p_pay_over_amount);
8384             OZF_Utility_PVT.debug_message('p_tot_accrual_amt = '||p_tot_accrual_amt);
8385             OZF_Utility_PVT.debug_message('l_prorate_adj_amt = '||l_prorate_adj_amt);
8386             OZF_Utility_PVT.debug_message('l_util_product_id = '||l_util_product_id);
8387             OZF_Utility_PVT.debug_message('l_util_product_level_type = '||l_util_product_level_type);
8388             OZF_Utility_PVT.debug_message('l_util_fund_id = '||l_util_fund_id);
8389           END IF;
8390 
8391           Create_Fund_Adjustment ( p_offer_id             => p_funds_util_flt.activity_id
8392                                  , p_cust_account_id      => p_funds_util_flt.cust_account_id
8393                                  , p_product_id           => l_util_product_id
8394                                  , p_product_level_type   => l_util_product_level_type
8395                                  , p_fund_id              => l_util_fund_id
8396                                  , p_reference_type       => p_funds_util_flt.reference_type
8397                                  , p_reference_id         => p_funds_util_flt.reference_id
8398                                  , x_return_status        => l_return_status
8399                                  , x_msg_count            => l_msg_count
8400                                  , x_msg_data             => l_msg_data
8401                                  , x_adj_util_id          => l_adj_util_id
8402                                  );
8403 
8404           IF l_return_status = FND_API.g_ret_sts_error THEN
8405             RAISE FND_API.g_exc_error;
8406           ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
8407             RAISE FND_API.g_exc_error;
8408           END IF;
8409 
8410           IF i = l_last_asso_index THEN
8411             --Fix for Bug 8583847
8412             px_line_util_tbl(j).amount := l_new_line_amount - l_tot_prorate_adj_amt;
8413           ELSE
8414             px_line_util_tbl(j).amount := l_prorate_adj_amt;
8415           END IF;
8416 
8417           l_tot_prorate_adj_amt := NVL(l_tot_prorate_adj_amt,0) + l_prorate_adj_amt;
8418 
8419           IF OZF_DEBUG_HIGH_ON THEN
8420             OZF_Utility_PVT.debug_message('Prorate Partial Pay Over Earnings Adjustments ...');
8421             OZF_Utility_PVT.debug_message('Utilization_Id = '||px_line_util_tbl(j).utilization_id);
8422             OZF_Utility_PVT.debug_message('Claim_Line_Id = '||px_line_util_tbl(j).claim_line_id);
8423             OZF_Utility_PVT.debug_message('Adjustment Amount = '||px_line_util_tbl(j).amount);
8424             OZF_Utility_PVT.debug_message('l_tot_prorate_adj_amt = '||l_tot_prorate_adj_amt);
8425           END IF;
8426 
8427           px_line_util_tbl(j).amount := OZF_UTILITY_PVT.CurrRound(px_line_util_tbl(j).amount, p_currency_rec.association_currency_code);
8428           px_line_util_tbl(j).utilization_id := l_adj_util_id;
8429           px_line_util_tbl(j).claim_line_id  := p_funds_util_flt.claim_line_id;
8430           px_line_util_tbl(j).update_from_tbl_flag := FND_API.g_true;
8431 
8432         END IF;
8433         j := j + 1;
8434       END LOOP;
8435     ELSE  -- px_line_util_tbl.COUNT = 0
8436       OPEN csr_offer_products ( p_funds_util_flt.activity_id
8437                               , NVL(p_funds_util_flt.activity_product_id,p_funds_util_flt.product_id)
8438                               , p_funds_util_flt.product_level_type
8439                               , p_funds_util_flt.cust_account_id
8440                               , p_currency_rec.transaction_currency_code
8441                               );
8442       FETCH csr_offer_products BULK COLLECT INTO l_level_type
8443                                                , l_item_id
8444                                                , l_fund_id
8445                                                , l_accrued_amt;
8446       CLOSE csr_offer_products;
8447 
8448       FOR i IN l_level_type.FIRST..l_level_type.LAST
8449       LOOP
8450         l_tot_accrual_amt := NVL(l_tot_accrual_amt,0) + l_accrued_amt(i);
8451       END LOOP;
8452 
8453 
8454       FOR i IN l_level_type.FIRST..l_level_type.LAST
8455       LOOP
8456 
8457         --Fix for Bug 8583847
8458         --l_tot_accrual_amt := NVL(l_tot_accrual_amt,0) + l_accrued_amt(i);
8459 
8460         --Fix for Bug 8583847
8461         l_prorate_adj_amt := l_accrued_amt(i) * (l_new_line_amount / l_tot_accrual_amt);
8462         l_prorate_adj_amt := OZF_UTILITY_PVT.CurrRound(l_prorate_adj_amt, p_currency_rec.association_currency_code);
8463 
8464         l_tot_prorate_adj_amt := NVL(l_tot_prorate_adj_amt,0) + l_prorate_adj_amt ;
8465 
8466           Create_Fund_Adjustment ( p_offer_id               => p_funds_util_flt.activity_id
8467                                , p_cust_account_id        => p_funds_util_flt.cust_account_id
8468                                , p_product_id             => l_item_id(i)
8469                                , p_product_level_type     => l_level_type(i)
8470                                , p_fund_id                => l_fund_id(i)
8471                                , p_reference_type         => p_funds_util_flt.reference_type
8472                                , p_reference_id           => p_funds_util_flt.reference_id
8473                                , x_return_status          => l_return_status
8474                                , x_msg_count              => l_msg_count
8475                                , x_msg_data               => l_msg_data
8476                                , x_adj_util_id            => l_adj_util_id
8477                               );
8478 
8479                 IF l_return_status = FND_API.g_ret_sts_error THEN
8480                   RAISE FND_API.g_exc_error;
8481                 ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
8482                   RAISE FND_API.g_exc_error;
8483                 END IF;
8484 
8485         IF i = l_level_type.LAST THEN
8486              px_line_util_tbl(j).amount := l_prorate_adj_amt;
8487         ELSE
8488           --Fix for Bug 8583847
8489           px_line_util_tbl(j).amount := px_line_util_tbl(j).amount + (l_new_line_amount - l_tot_prorate_adj_amt);
8490 
8491         END IF;
8492 
8493         px_line_util_tbl(j).amount := OZF_UTILITY_PVT.CurrRound(px_line_util_tbl(j).amount, p_currency_rec.association_currency_code);
8494         px_line_util_tbl(j).utilization_id := l_adj_util_id;
8495         px_line_util_tbl(j).claim_line_id  := p_funds_util_flt.claim_line_id;
8496         px_line_util_tbl(j).update_from_tbl_flag := FND_API.g_true;
8497 
8498         IF OZF_DEBUG_HIGH_ON THEN
8499           OZF_Utility_PVT.debug_message('Prorate Total Pay Over Earnings Adjustments ...');
8500           OZF_Utility_PVT.debug_message('Utilization_Id = '||px_line_util_tbl(j).utilization_id);
8501           OZF_Utility_PVT.debug_message('Claim_Line_Id = '||px_line_util_tbl(j).claim_line_id);
8502           OZF_Utility_PVT.debug_message('Adjustment Amount = '||px_line_util_tbl(j).amount);
8503         END IF;
8504         j := j + 1;
8505       END LOOP;
8506     END IF;
8507   ELSE --p_prorate_earnings_flag = 'F'
8508 
8509     OPEN  csr_source_fund_tot(p_funds_util_flt.activity_id);
8510     FETCH csr_source_fund_tot INTO l_fund_tot_amount;
8511     CLOSE csr_source_fund_tot;
8512 
8513     FOR r_source_fund IN csr_source_fund(p_funds_util_flt.activity_id)
8514     LOOP
8515       Create_Fund_Adjustment ( p_offer_id            => p_funds_util_flt.activity_id
8516                              , p_cust_account_id     => p_funds_util_flt.cust_account_id
8517                              , p_product_id          => NULL
8518                              , p_product_level_type  => NULL
8519                              , p_fund_id             => r_source_fund.budget_source_id
8520                              , p_reference_type      => p_funds_util_flt.reference_type
8521                              , p_reference_id        => p_funds_util_flt.reference_id
8522                              , x_return_status       => l_return_status
8523                              , x_msg_count           => l_msg_count
8524                              , x_msg_data            => l_msg_data
8525                              , x_adj_util_id         => l_adj_util_id
8526                              );
8527 
8528       IF l_return_status = FND_API.g_ret_sts_error THEN
8529         RAISE FND_API.g_exc_error;
8530       ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
8531         RAISE FND_API.g_exc_error;
8532       END IF;
8533 
8534       IF csr_source_fund%ROWCOUNT = r_source_fund.total_funds THEN
8535         px_line_util_tbl(j).amount := p_pay_over_amount - l_tot_amt_rem;
8536       ELSE
8537         IF l_fund_tot_amount = 0 THEN
8538           px_line_util_tbl(j).amount := p_pay_over_amount;
8539         ELSE
8540           px_line_util_tbl(j).amount := p_pay_over_amount * (r_source_fund.approved_original_amount/l_fund_tot_amount);
8541           l_tot_amt_rem := l_tot_amt_rem + px_line_util_tbl(j).amount;
8542         END IF;
8543       END IF;
8544 
8545       px_line_util_tbl(j).amount := OZF_UTILITY_PVT.CurrRound(px_line_util_tbl(j).amount, p_currency_rec.association_currency_code);
8546       px_line_util_tbl(j).utilization_id := l_adj_util_id;
8547       px_line_util_tbl(j).claim_line_id  := p_funds_util_flt.claim_line_id;
8548       px_line_util_tbl(j).update_from_tbl_flag := FND_API.g_true;
8549 
8550       IF OZF_DEBUG_HIGH_ON THEN
8551         OZF_Utility_PVT.debug_message('Non-Prorate Pay Over Earnings Adjustments ...');
8552         OZF_Utility_PVT.debug_message('Utilization_Id = '||px_line_util_tbl(j).utilization_id);
8553         OZF_Utility_PVT.debug_message('Claim_Line_Id = '||px_line_util_tbl(j).claim_line_id);
8554         OZF_Utility_PVT.debug_message('Adjustment Amount = '||px_line_util_tbl(j).amount);
8555       END IF;
8556       j := j + 1;
8557     END LOOP;
8558   END IF;  --p_prorate_earnings_flag
8559   END IF;  --SCAN_DATA check
8560 
8561 EXCEPTION
8562   WHEN FND_API.g_exc_error THEN
8563     ROLLBACK TO Create_Pay_Over_Adjustments;
8564     x_return_status := FND_API.g_ret_sts_error;
8565     FND_MSG_PUB.count_and_get (
8566            p_encoded => FND_API.g_false
8567           ,p_count   => x_msg_count
8568           ,p_data    => x_msg_data
8569     );
8570 
8571   WHEN FND_API.g_exc_unexpected_error THEN
8572     ROLLBACK TO Create_Pay_Over_Adjustments;
8573     x_return_status := FND_API.g_ret_sts_unexp_error ;
8574     FND_MSG_PUB.count_and_get (
8575            p_encoded => FND_API.g_false
8576           ,p_count   => x_msg_count
8577           ,p_data    => x_msg_data
8578     );
8579 
8580   WHEN OTHERS THEN
8581     ROLLBACK TO Create_Pay_Over_Adjustments;
8582     x_return_status := FND_API.g_ret_sts_unexp_error ;
8583     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
8584       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
8585     END IF;
8586     FND_MSG_PUB.count_and_get(
8587            p_encoded => FND_API.g_false
8588           ,p_count   => x_msg_count
8589           ,p_data    => x_msg_data
8590     );
8591 
8592 END Create_Pay_Over_Adjustments;
8593 
8594 ---------------------------------------------------------------------
8595 -- PROCEDURE
8596 --    Populate_Line_Util_Tbl
8597 --
8598 -- HISTORY
8599 --    11-Mar-2009  psomyaju  Create.
8600 --    10-Jun-2009  kpatro   Fix for Bug 8583847
8601 ---------------------------------------------------------------------
8602 PROCEDURE Populate_Line_Util_Tbl ( p_funds_util_flt         funds_util_flt_type
8603                                  , p_source_object_class    IN VARCHAR2
8604                                  , p_source_object_id       IN NUMBER
8605                                  , p_request_header_id      IN NUMBER
8606                                  , p_batch_line_id          IN NUMBER
8607                                  , p_batch_type             IN VARCHAR2
8608                                  , p_summary_view           IN VARCHAR2
8609                                  , p_cre_util_amount        IN NUMBER
8610                                  , p_prorate_earnings_flag  IN VARCHAR2
8611                                  , p_currency_rec           IN currency_rec_type
8612 				 , p_offer_quantity         IN NUMBER
8613                                  , x_funds_rem_tbl          OUT NOCOPY funds_rem_tbl_type
8614                                  , x_tot_accrual_amt        OUT NOCOPY NUMBER
8615                                  , x_line_amount            OUT NOCOPY NUMBER
8616                                  , x_line_util_tbl          OUT NOCOPY line_util_tbl_type
8617 				 , x_util_quantity          OUT NOCOPY NUMBER
8618 				 , x_new_total_units        OUT NOCOPY NUMBER
8619                                  , x_return_status          OUT NOCOPY VARCHAR2
8620                                  , x_msg_data               OUT NOCOPY VARCHAR2
8621                                  , x_msg_count              OUT NOCOPY NUMBER
8622                                  ) IS
8623 
8624 l_funds_util_csr        NUMBER;
8625 l_funds_util_sql        VARCHAR2(3000);
8626 l_total_pay_over_flag   BOOLEAN := FALSE;
8627 l_util_id               NUMBER  := 0;
8628 l_fu_amt_rem            NUMBER  := 0;
8629 l_total_amt_rem         NUMBER  := 0;
8630 l_tot_accrual_amt       NUMBER  := 0;
8631 l_total_prorate_amount  NUMBER  := 0;
8632 l_prorate_amount        NUMBER  := 0;
8633 l_fu_scan_unit_rem      NUMBER  := 0;
8634 l_total_scan_unit_rem   NUMBER  := 0;
8635 l_fu_currency_code      VARCHAR2(15);
8636 l_util_uom_code         VARCHAR2(15);
8637 l_util_quantity         NUMBER  := 0;
8638 l_ignore                NUMBER  := 0;
8639 l_funds_used_units      NUMBER  := 0;
8640 j                       NUMBER  := 0;
8641 l_exit                  BOOLEAN := FALSE;
8642 l_line_rem_amount       NUMBER  := 0;
8643 l_fu_exchange_rate      NUMBER  := 0;
8644 l_fu_exc_rate_type      VARCHAR2(30);
8645 l_fu_exc_rate_date      DATE;
8646 l_conv_exchange_rate    NUMBER  := 0;
8647 l_batch_curr_claim_amt  NUMBER  := 0;
8648 
8649 l_api_version   CONSTANT  NUMBER       := 1.0;
8650 l_api_name      CONSTANT  VARCHAR2(30) := 'Populate_Line_Util_Tbl';
8651 l_full_name     CONSTANT  VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
8652 l_return_status         VARCHAR2(15);
8653 l_msg_count             NUMBER;
8654 l_msg_data              VARCHAR2(15);
8655 l_new_total_units       NUMBER := 0; --EA ninarasi fix for 14016226
8656 
8657 l_line_util_tbl         line_util_tbl_type;
8658 l_funds_rem_tbl         funds_rem_tbl_type;
8659 l_currency_rec          currency_rec_type := p_currency_rec;
8660 l_exit_nonprorate       BOOLEAN := FALSE; --changed 13550004
8661 l_plan_currency_code    VARCHAR2(15);
8662 l_plan_curr_amount_remaining NUMBER := 0;
8663 l_plan_curr_amount NUMBER := 0;
8664 l_acctd_amount_remaining NUMBER := 0;
8665 -- Bugfix 4926327
8666 -- Bugfix 5101106
8667 /*CURSOR csr_funds_used_units(cv_activity_product_id IN NUMBER) IS
8668   SELECT NVL(SUM(scan_unit * amp.quantity ), 0)
8669   FROM   ozf_claim_lines_util lu,
8670          ozf_claims c,
8671          ozf_claim_lines cl,
8672          ams_act_products amp
8673   WHERE  lu.activity_product_id = cv_activity_product_id
8674   AND    cl.claim_line_id = lu.claim_line_id
8675   AND    cl.claim_id =  c.claim_id
8676   AND    c.status_code <> 'CLOSED'
8677   AND    lu.activity_product_id = amp.activity_product_id
8678   AND    lu.utilization_id <> -1; */
8679 
8680 /* ER 9226258
8681 CURSOR csr_sd_accruals ( cv_batch_id          NUMBER
8682                        , cv_product_id        NUMBER
8683                        , cv_request_header_id NUMBER
8684                        , cv_batch_line_id     NUMBER
8685                        ) IS
8686  SELECT  util.utilization_id
8687        , line.batch_curr_claim_amount
8688        , offr.transaction_currency_code
8689  FROM    ozf_funds_utilized_all_b util
8690        , ozf_sd_batch_lines_all   line
8691        , ozf_offers offr
8692  WHERE   util.utilization_id = line.utilization_id
8693    AND   util.plan_id = offr.qp_list_header_id
8694    AND   line.batch_id = cv_batch_id
8695    AND   line.item_id = cv_product_id
8696    AND   util.reference_id = cv_request_header_id
8697    AND   line.batch_line_id = NVL(cv_batch_line_id,batch_line_id)
8698    AND   util.reference_type = 'SD_REQUEST'
8699    AND   line.batch_curr_claim_amount <> 0;
8700 */
8701 
8702 CURSOR csr_sd_accruals ( cv_batch_id          NUMBER
8703                        , cv_product_id        NUMBER
8704                        , cv_request_header_id NUMBER
8705                        , cv_batch_line_id     NUMBER
8706                        ) IS
8707  SELECT  util.utilization_id
8708        , util.exchange_rate
8709        , util.exchange_rate_date
8710        , util.exchange_rate_type
8711        , line.batch_curr_claim_amount
8712  FROM    ozf_funds_utilized_all_b util
8713        , ozf_sd_batch_lines_all   line
8714  WHERE   util.utilization_id = line.utilization_id
8715    AND   line.status_code ='APPROVED' --//Bugfix: 9794989
8716    AND   line.purge_flag ='N'
8717    AND   line.batch_id = cv_batch_id
8718    AND   line.item_id = cv_product_id
8719    AND   util.reference_id = cv_request_header_id
8720    AND   line.batch_line_id = NVL(cv_batch_line_id,batch_line_id)
8721    AND   util.reference_type = 'SD_REQUEST'
8722    AND   line.batch_curr_claim_amount <> 0;
8723 BEGIN
8724 
8725   IF OZF_DEBUG_HIGH_ON THEN
8726     OZF_Utility_PVT.debug_message(l_full_name||': start');
8727   END IF;
8728 
8729  -- Fix for Bug 8583847
8730  l_line_rem_amount := p_cre_util_amount;
8731 
8732  SAVEPOINT Populate_Line_Util_Tbl;
8733 
8734  l_new_total_units := p_funds_util_flt.total_units;
8735  l_util_quantity := p_funds_util_flt.quantity;
8736 
8737 
8738   --For Supplier Ship Debit claims, since we already know the utilizations during batch creation.
8739   --Hence, with batch lines, we can get the qualified utilizations directly.
8740   IF NVL(p_source_object_class,'X') = 'SD_SUPPLIER' THEN
8741 
8742   --//ER 9226258
8743     OPEN csr_sd_accruals ( p_source_object_id
8744                          , p_funds_util_flt.product_id
8745                          , p_request_header_id
8746                          , p_batch_line_id
8747                          );
8748     j := 0;
8749     LOOP
8750       FETCH csr_sd_accruals INTO l_util_id,l_fu_exchange_rate,l_fu_exc_rate_date,l_fu_exc_rate_type,l_batch_curr_claim_amt;
8751       EXIT WHEN csr_sd_accruals%NOTFOUND;
8752 
8753       x_line_util_tbl(j).utilization_id         := l_util_id;
8754       x_line_util_tbl(j).claim_line_id          := p_funds_util_flt.claim_line_id;
8755       x_line_util_tbl(j).activity_product_id    := p_funds_util_flt.activity_product_id;
8756       x_line_util_tbl(j).uom_code               := l_util_uom_code;
8757       x_line_util_tbl(j).update_from_tbl_flag   := FND_API.g_true;
8758 
8759       IF (l_currency_rec.transaction_currency_code = l_currency_rec.claim_currency_code) THEN
8760          x_line_util_tbl(j).amount := l_batch_curr_claim_amt;
8761       ELSE
8762          IF l_currency_rec.claim_currency_code <> l_currency_rec.functional_currency_code THEN
8763             OZF_UTILITY_PVT.Convert_Currency (
8764                         p_from_currency   => l_currency_rec.claim_currency_code
8765                       , p_to_currency     => l_currency_rec.functional_currency_code
8766                       , p_conv_type       => l_fu_exc_rate_type
8767                       , p_conv_rate       => l_fu_exchange_rate
8768                       , p_conv_date       => SYSDATE
8769                       , p_from_amount     => l_batch_curr_claim_amt
8770                       , x_return_status   => l_return_status
8771                       , x_to_amount       => x_line_util_tbl(j).amount
8772                       , x_rate            => l_conv_exchange_rate
8773             );
8774 
8775             IF l_return_status = FND_API.g_ret_sts_error THEN
8776                RAISE FND_API.g_exc_error;
8777             ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
8778                RAISE FND_API.g_exc_unexpected_error;
8779             END IF;
8780 
8781             x_line_util_tbl(j).amount := OZF_UTILITY_PVT.CurrRound( x_line_util_tbl(j).amount, l_currency_rec.functional_currency_code);
8782 
8783          ELSE
8784             x_line_util_tbl(j).amount := l_batch_curr_claim_amt;
8785          END IF;
8786       END IF;
8787       j := j + 1;
8788     END LOOP;
8789     CLOSE csr_sd_accruals;
8790 
8791 
8792     /*
8793     OPEN csr_sd_accruals ( p_source_object_id
8794                          , p_funds_util_flt.product_id
8795                          , p_request_header_id
8796                          , p_batch_line_id
8797                          );
8798     j := 0;
8799     LOOP
8800       FETCH csr_sd_accruals INTO l_util_id,l_fu_amt_rem,l_fu_currency_code;
8801       EXIT WHEN csr_sd_accruals%NOTFOUND;
8802       x_line_util_tbl(j).utilization_id := l_util_id;
8803       x_line_util_tbl(j).amount := OZF_UTILITY_PVT.CurrRound(l_fu_amt_rem, l_currency_rec.association_currency_code);
8804       x_line_util_tbl(j).claim_line_id := p_funds_util_flt.claim_line_id;
8805       x_line_util_tbl(j).activity_product_id := p_funds_util_flt.activity_product_id;
8806       x_line_util_tbl(j).uom_code := l_util_uom_code;
8807       x_line_util_tbl(j).update_from_tbl_flag := FND_API.g_true;
8808       j := j + 1;
8809     END LOOP;
8810     CLOSE csr_sd_accruals;
8811     */
8812   ELSE
8813 
8814     IF p_funds_util_flt.pay_over_all_flag IS NULL THEN
8815        l_total_pay_over_flag := FALSE;
8816     ELSE
8817        l_total_pay_over_flag := p_funds_util_flt.pay_over_all_flag;
8818     END IF;
8819 
8820 
8821     --Get all the qualified utilizations for current claim line association on the basis of
8822     --customer, offer and product. If pay over earnings is allowed, then no need to identify
8823     --utilizations. We will create adjustments for entire pay over earnings.
8824     --Bugfix 5144750
8825     IF NOT (l_total_pay_over_flag) THEN
8826       Get_Utiz_Sql_Stmt ( p_api_version         => 1.0
8827                         , p_init_msg_list       => FND_API.g_false
8828                         , p_commit              => FND_API.g_false
8829                         , p_validation_level    => FND_API.g_valid_level_full
8830                         , x_return_status       => l_return_status
8831                         , x_msg_count           => l_msg_count
8832                         , x_msg_data            => l_msg_data
8833                         , p_summary_view        => p_summary_view
8834                         , p_funds_util_flt      => p_funds_util_flt
8835                         , px_currency_rec        => l_currency_rec
8836                         , p_cust_account_id     => p_funds_util_flt.cust_account_id
8837                         , x_utiz_sql_stmt       => l_funds_util_sql
8838                         );
8839       IF l_return_status = FND_API.g_ret_sts_error THEN
8840         RAISE FND_API.g_exc_error;
8841       ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
8842         RAISE FND_API.g_exc_error;
8843       END IF;
8844 
8845       j := 0;
8846       l_funds_util_csr := DBMS_SQL.open_cursor;
8847       FND_DSQL.set_cursor(l_funds_util_csr);
8848       DBMS_SQL.parse(l_funds_util_csr, l_funds_util_sql, DBMS_SQL.native);
8849       DBMS_SQL.define_column(l_funds_util_csr, 1, l_util_id);
8850       DBMS_SQL.define_column(l_funds_util_csr, 2, l_fu_amt_rem);
8851       DBMS_SQL.define_column(l_funds_util_csr, 3, l_fu_scan_unit_rem);
8852       DBMS_SQL.define_column(l_funds_util_csr, 4, l_fu_currency_code, 15);
8853       DBMS_SQL.define_column(l_funds_util_csr, 5, l_plan_currency_code, 15);
8854       DBMS_SQL.define_column(l_funds_util_csr, 6, l_plan_curr_amount_remaining);
8855       DBMS_SQL.define_column(l_funds_util_csr, 7, l_plan_curr_amount);
8856       DBMS_SQL.define_column(l_funds_util_csr, 8, l_acctd_amount_remaining);
8857       FND_DSQL.do_binds;
8858 
8859       l_ignore := DBMS_SQL.execute(l_funds_util_csr);
8860       LOOP
8861         IF DBMS_SQL.fetch_rows(l_funds_util_csr) > 0 THEN
8862           DBMS_SQL.column_value(l_funds_util_csr, 1, l_util_id);
8863           DBMS_SQL.column_value(l_funds_util_csr, 2, l_fu_amt_rem);
8864           DBMS_SQL.column_value(l_funds_util_csr, 3, l_fu_scan_unit_rem);
8865           DBMS_SQL.column_value(l_funds_util_csr, 4, l_fu_currency_code);
8866           DBMS_SQL.column_value(l_funds_util_csr, 5, l_plan_currency_code);
8867           DBMS_SQL.column_value(l_funds_util_csr, 6, l_plan_curr_amount_remaining);
8868           DBMS_SQL.column_value(l_funds_util_csr, 7, l_plan_curr_amount);
8869           DBMS_SQL.column_value(l_funds_util_csr, 8, l_acctd_amount_remaining);
8870 
8871 
8872           /*OPEN  csr_funds_used_units(p_funds_util_flt.activity_product_id); --ninarasi fix for bug 13530939
8873           FETCH csr_funds_used_units INTO l_funds_used_units;
8874           CLOSE csr_funds_used_units;*/
8875 
8876           l_funds_rem_tbl(j).utilization_id := l_util_id;
8877           l_funds_rem_tbl(j).amount_remaining := l_fu_amt_rem;
8878           --l_funds_rem_tbl(j).scan_unit_remaining := l_fu_scan_unit_rem - l_funds_used_units; --ninarasi fix for bug 13530939
8879           l_total_amt_rem := l_total_amt_rem + l_funds_rem_tbl(j).amount_remaining;
8880 	  --l_total_scan_unit_rem := l_total_scan_unit_rem + l_funds_rem_tbl(j).scan_unit_remaining;
8881            l_funds_rem_tbl(j).plan_currency_code := l_plan_currency_code;
8882            l_funds_rem_tbl(j).plan_curr_amount_remaining := l_plan_curr_amount_remaining;
8883            l_funds_rem_tbl(j).plan_curr_amount := l_plan_curr_amount;
8884            l_funds_rem_tbl(j).acctd_amount_remaining := l_acctd_amount_remaining;
8885 
8886 
8887           j := j + 1;
8888         ELSE
8889           EXIT;
8890         END IF;
8891       END LOOP;
8892       DBMS_SQL.close_cursor(l_funds_util_csr);
8893 
8894       --If total amount remaining for all the qualified adjustments is zero, then raise warning.
8895       --Raise error, if negative assocation is taking place for positive amount remaining.
8896       IF l_total_amt_rem = 0 AND
8897         NVL(p_funds_util_flt.offer_type,'X') NOT IN ('ACCRUAL','DEAL','LUMPSUM','NET_ACCRUAL','VOLUME_OFFER', 'SCAN_DATA') THEN
8898           IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
8899             FND_MESSAGE.set_name('OZF', 'OZF_EARN_AVAIL_AMT_ZERO');
8900             FND_MSG_PUB.add;
8901           END IF;
8902           RAISE FND_API.g_exc_error;
8903       ELSE
8904         -- Fix for Bug 14023475 : Restrict the Negative Qty association for scan data offer.
8905 	IF p_batch_type = 'BATCH' THEN
8906            NULL;
8907         ELSE
8908           IF (p_cre_util_amount < 0 and l_total_amt_rem >0)
8909            THEN
8910               IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
8911                  FND_MESSAGE.set_name('OZF', 'OZF_ASSO_NEG_AMT');
8912                  FND_MSG_PUB.add;
8913               END IF;
8914               RAISE FND_API.g_exc_error;
8915           END IF;
8916         END IF;
8917 
8918       END IF;
8919 
8920       /* Earnings Calculation:
8921       If prorate earnigns flag is set, then calculate earnings on prorate basis i.e. distribute association amount uniformly
8922       across qualified utilizations based on amount remaining of utilizations.
8923       Otherwise, earnings should be calculated in FIFO basis, i.e. in set of qualified utilizations, first utilization will
8924       consume association amount for its entire amount remaining value. Remaining amount will be used by next utilization in
8925       similar fashion and so on until either association amount exhaust or all utilizations processed. In this case, if association
8926       amount left after all utilizations process, this amount will be considered as pay over earnings and adjustment need to be
8927       created for this amount.
8928       */
8929 
8930       IF OZF_DEBUG_HIGH_ON THEN
8931         OZF_Utility_PVT.debug_message('l_funds_rem_tbl.COUNT = '||l_funds_rem_tbl.COUNT);
8932         OZF_Utility_PVT.debug_message('p_cre_util_amount = '||p_cre_util_amount);
8933         OZF_Utility_PVT.debug_message('l_total_amt_rem = '||l_total_amt_rem);
8934         OZF_Utility_PVT.debug_message('p_prorate_earnings_flag = '||p_prorate_earnings_flag);
8935       END IF;
8936 
8937       -- Bugfix 6042226
8938       IF l_funds_rem_tbl.COUNT > 0 THEN
8939         IF l_funds_rem_tbl.COUNT > 1     AND
8940            p_prorate_earnings_flag = 'T' AND
8941            ABS(p_cre_util_amount) <= ABS(l_total_amt_rem)
8942         THEN
8943 
8944            FOR i IN l_funds_rem_tbl.FIRST..l_funds_rem_tbl.LAST
8945            LOOP
8946 
8947              IF i = l_funds_rem_tbl.LAST THEN
8948               -- Modified the logic for multiple lines - kpatro
8949 
8950                 l_prorate_amount := l_funds_rem_tbl(i).amount_remaining * (p_cre_util_amount / l_total_amt_rem);
8951                 l_prorate_amount := OZF_UTILITY_PVT.CurrRound(l_prorate_amount,l_currency_rec.association_currency_code);
8952 
8953                -- modified the condition to <> to account for either less or greater condition
8954 
8955                 IF (l_total_prorate_amount + l_prorate_amount) <> p_cre_util_amount THEN
8956                      l_prorate_amount := p_cre_util_amount - l_total_prorate_amount;
8957                 END IF;
8958                 x_line_util_tbl(i).amount := l_prorate_amount;
8959              ELSE
8960                  l_prorate_amount := l_funds_rem_tbl(i).amount_remaining * (p_cre_util_amount / l_total_amt_rem);
8961                  x_line_util_tbl(i).amount := OZF_UTILITY_PVT.CurrRound(l_prorate_amount, l_currency_rec.association_currency_code);
8962              END IF;
8963 
8964              l_total_prorate_amount := l_total_prorate_amount + x_line_util_tbl(i).amount;
8965 
8966              -- commented the below code as it is calcualted above --by kpatro
8967              --x_line_util_tbl(i).amount := l_prorate_amount;
8968              x_line_util_tbl(i).utilization_id := l_funds_rem_tbl(i).utilization_id;
8969              x_line_util_tbl(i).claim_line_id := p_funds_util_flt.claim_line_id;
8970              x_line_util_tbl(i).activity_product_id := p_funds_util_flt.activity_product_id;
8971              x_line_util_tbl(i).uom_code := p_funds_util_flt.uom_code;
8972              x_line_util_tbl(i).quantity := p_funds_util_flt.quantity;
8973              x_line_util_tbl(i).update_from_tbl_flag := FND_API.g_true;
8974            END LOOP;
8975         ELSE
8976           -- Fix for Bug 8583847
8977           --l_line_rem_amount := p_cre_util_amount;
8978           FOR i IN l_funds_rem_tbl.FIRST..l_funds_rem_tbl.LAST
8979           LOOP
8980             l_fu_amt_rem := l_funds_rem_tbl(i).amount_remaining;
8981             IF OZF_DEBUG_HIGH_ON THEN
8982             OZF_Utility_PVT.debug_message('NN : l_fu_amt_rem = ' || l_fu_amt_rem);
8983             END IF;
8984             IF ((SIGN(p_cre_util_amount) = -1 AND SIGN(l_total_amt_rem) = -1) AND
8985                 (p_cre_util_amount > l_total_amt_rem)) OR
8986                ((SIGN(p_cre_util_amount) = 1 AND SIGN(l_total_amt_rem) = 1) AND
8987                 (p_cre_util_amount < l_total_amt_rem))
8988             THEN
8989               IF l_line_rem_amount >= l_fu_amt_rem THEN
8990                 x_line_util_tbl(i).amount := l_fu_amt_rem;
8991                 IF OZF_DEBUG_HIGH_ON THEN
8992                 OZF_Utility_PVT.debug_message('NN : Inside IF l_fu_amt_rem = ' || l_fu_amt_rem);
8993                 END IF;
8994               ELSE
8995                 x_line_util_tbl(i).amount := l_line_rem_amount;
8996                 IF OZF_DEBUG_HIGH_ON THEN
8997                 OZF_Utility_PVT.debug_message('NN : Inside ELSE l_line_rem_amount = ' || l_line_rem_amount);
8998                 END IF;
8999               END IF;
9000             ELSE
9001               -- Bugfix 5404951
9002               IF (p_batch_type = 'BATCH' AND p_source_object_class = 'SPECIAL_PRICE' AND p_funds_util_flt.offer_type = 'SCAN_DATA') THEN
9003                 x_line_util_tbl(i).amount := p_cre_util_amount;
9004                 l_exit := TRUE;
9005               END IF;
9006               IF SIGN(p_cre_util_amount) = SIGN(l_fu_amt_rem) THEN
9007                  l_tot_accrual_amt  := NVL(l_tot_accrual_amt,0) + l_fu_amt_rem;
9008               END IF;
9009             END IF;
9010 
9011             --Last utilization amount rounding
9012             -- Fix for non prorate condition -- kpatro
9013              -- Fix for Bug#12930435
9014              /*IF ABS(l_line_rem_amount) >= ABS(l_fu_amt_rem) THEN
9015               x_line_util_tbl(i).amount := l_fu_amt_rem;
9016             ELSE
9017               x_line_util_tbl(i).amount := l_line_rem_amount;
9018              END IF;*/
9019         --New change bug 13550004
9020             IF l_line_rem_amount >= 0 then
9021                 IF l_line_rem_amount >= l_fu_amt_rem THEN
9022                   x_line_util_tbl(i).amount := l_fu_amt_rem;
9023                   IF OZF_DEBUG_HIGH_ON THEN
9024                   OZF_Utility_PVT.debug_message('NN : Inside IF new change x_line_util_tbl(i).amount= ' || x_line_util_tbl(i).amount);
9025                   END IF;
9026                 ELSE
9027                   x_line_util_tbl(i).amount := l_line_rem_amount;
9028                   l_exit_nonprorate := TRUE; --changed 13550004
9029                   IF OZF_DEBUG_HIGH_ON THEN
9030                    OZF_Utility_PVT.debug_message('NN : Inside IF111 new change x_line_util_tbl(i).amount= ' || x_line_util_tbl(i).amount);
9031                    END IF;
9032                 END IF;
9033             ELSE
9034                 IF l_line_rem_amount < l_fu_amt_rem THEN
9035                   x_line_util_tbl(i).amount := l_fu_amt_rem;
9036                   IF OZF_DEBUG_HIGH_ON THEN
9037                    OZF_Utility_PVT.debug_message('NN : Inside ELSE new change x_line_util_tbl(i).amount= ' || x_line_util_tbl(i).amount);
9038                    END IF;
9039                 ELSE
9040                   x_line_util_tbl(i).amount := l_line_rem_amount;
9041                   l_exit_nonprorate := TRUE; --changed 13550004
9042                   IF OZF_DEBUG_HIGH_ON THEN
9043                   OZF_Utility_PVT.debug_message('NN : Inside ELSE111 new change x_line_util_tbl(i).amount= ' || x_line_util_tbl(i).amount);
9044                   END IF;
9045                 END IF;
9046             END IF;
9047 
9048             IF p_funds_util_flt.offer_type = 'SCAN_DATA' THEN
9049                   x_line_util_tbl(i).scan_unit := x_line_util_tbl(i).amount / G_SCAN_VALUE;
9050 	          x_line_util_tbl(i).quantity  := x_line_util_tbl(i).scan_unit * p_offer_quantity; --EA   ninarasi fix for 14016226
9051 
9052 	          l_util_quantity := l_util_quantity - x_line_util_tbl(i).quantity ; --EA ninarasi fix for 14016226
9053                   l_new_total_units := l_new_total_units - x_line_util_tbl(i).scan_unit; --EA ninarasi fix for 14016226
9054             END IF;
9055 
9056             x_line_util_tbl(i).utilization_id           := l_funds_rem_tbl(i).utilization_id;
9057             x_line_util_tbl(i).claim_line_id            := p_funds_util_flt.claim_line_id;
9058             x_line_util_tbl(i).activity_product_id      := p_funds_util_flt.activity_product_id;
9059             x_line_util_tbl(i).uom_code                 := p_funds_util_flt.uom_code;
9060             x_line_util_tbl(i).update_from_tbl_flag     := FND_API.g_true;
9061 
9062             l_line_rem_amount := l_line_rem_amount - l_fu_amt_rem;
9063             IF OZF_DEBUG_HIGH_ON THEN
9064             OZF_Utility_PVT.debug_message('NN : l_line_rem_amount after= ' || l_line_rem_amount);
9065             END IF;
9066             IF l_line_rem_amount = 0 OR l_exit_nonprorate = TRUE THEN --changed 13550004
9067               EXIT;
9068             END IF;
9069           END LOOP;
9070         END IF; --l_prorate_earnings_flag = 'T'
9071       END IF;   --l_funds_rem_tbl.COUNT > 0
9072 
9073       x_tot_accrual_amt := l_total_amt_rem;
9074 
9075     END IF;  --l_total_pay_over_flag = FALSE
9076   END IF;  --SD_SUPPLIER Check
9077 
9078   IF OZF_DEBUG_HIGH_ON THEN
9079     IF x_line_util_tbl.COUNT > 0 THEN
9080       FOR i IN x_line_util_tbl.FIRST..x_line_util_tbl.LAST
9081       LOOP
9082         OZF_Utility_PVT.debug_message('x_line_util_tbl('||i||').utilization_id = '||x_line_util_tbl(i).utilization_id);
9083         OZF_Utility_PVT.debug_message('x_line_util_tbl('||i||').claim_line_id = '||x_line_util_tbl(i).claim_line_id);
9084         OZF_Utility_PVT.debug_message('x_line_util_tbl('||i||').activity_product_id = '||x_line_util_tbl(i).activity_product_id);
9085         OZF_Utility_PVT.debug_message('x_line_util_tbl('||i||').uom_code = '||x_line_util_tbl(i).uom_code);
9086         OZF_Utility_PVT.debug_message('x_line_util_tbl('||i||').amount = '||x_line_util_tbl(i).amount);
9087         OZF_Utility_PVT.debug_message('x_line_util_tbl('||i||').quantity = '||x_line_util_tbl(i).quantity);
9088         OZF_Utility_PVT.debug_message('x_line_util_tbl('||i||').scan_unit = '||x_line_util_tbl(i).scan_unit);
9089       END LOOP;
9090     END IF;
9091   END IF;
9092 
9093    -- Fix for Bug 8583847
9094     x_line_amount := l_line_rem_amount;
9095     x_util_quantity := l_util_quantity;
9096     x_new_total_units := l_new_total_units;
9097 
9098 
9099    IF OZF_DEBUG_HIGH_ON THEN
9100     OZF_Utility_PVT.debug_message(l_full_name||': end');
9101     OZF_Utility_PVT.debug_message('x_line_amount :' || x_line_amount);
9102   END IF;
9103 
9104   x_funds_rem_tbl := l_funds_rem_tbl;
9105 
9106 
9107 EXCEPTION
9108   WHEN FND_API.g_exc_error THEN
9109     ROLLBACK TO Populate_Line_Util_Tbl;
9110     x_return_status := FND_API.g_ret_sts_error;
9111     FND_MSG_PUB.count_and_get (
9112            p_encoded => FND_API.g_false
9113           ,p_count   => x_msg_count
9114           ,p_data    => x_msg_data
9115     );
9116 
9117   WHEN FND_API.g_exc_unexpected_error THEN
9118     ROLLBACK TO Populate_Line_Util_Tbl;
9119     x_return_status := FND_API.g_ret_sts_unexp_error ;
9120     FND_MSG_PUB.count_and_get (
9121            p_encoded => FND_API.g_false
9122           ,p_count   => x_msg_count
9123           ,p_data    => x_msg_data
9124     );
9125 
9126   WHEN OTHERS THEN
9127     ROLLBACK TO Populate_Line_Util_Tbl;
9128     x_return_status := FND_API.g_ret_sts_unexp_error ;
9129     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
9130       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
9131     END IF;
9132     FND_MSG_PUB.count_and_get(
9133            p_encoded => FND_API.g_false
9134           ,p_count   => x_msg_count
9135           ,p_data    => x_msg_data
9136     );
9137 
9138 END Populate_Line_Util_Tbl;
9139 
9140 ---------------------------------------------------------------------
9141 -- PROCEDURE
9142 --    Get_Scan_Data_Details
9143 --
9144 -- HISTORY
9145 --    06/04/2009  psomyaju  Create.
9146 --    07/26/2009  BKUNJAN   Modified.
9147 ---------------------------------------------------------------------
9148 PROCEDURE Get_Scan_Data_Details ( p_offer_status                       VARCHAR2
9149                                 , p_batch_type                         VARCHAR2
9150                                 , p_source_object_class                VARCHAR2
9151                                 , px_funds_util_flt      IN OUT NOCOPY funds_util_flt_type
9152                                 , px_currency_rec        IN OUT NOCOPY currency_rec_type
9153 				, x_offer_quantity            OUT    NOCOPY NUMBER
9154                                 , x_return_status        OUT    NOCOPY VARCHAR2
9155                                 , x_msg_count            OUT    NOCOPY NUMBER
9156                                 , x_msg_data             OUT    NOCOPY VARCHAR2
9157                                 )
9158 IS
9159 
9160 l_api_version   CONSTANT  NUMBER       := 1.0;
9161 l_api_name      CONSTANT  VARCHAR2(30) := 'Get_Scan_Data_Details';
9162 l_full_name     CONSTANT  VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
9163 l_asso_total_units        NUMBER;
9164 l_asso_uom_code           VARCHAR2(30);
9165 l_asso_total_quantity     NUMBER;
9166 l_net_asso_amount         NUMBER;
9167 l_net_total_units         NUMBER;
9168 l_offer_uom_code          VARCHAR2(30);
9169 l_offer_quantity          NUMBER;
9170 l_product_id              NUMBER;
9171 l_offer_status            VARCHAR2(30);
9172 
9173 CURSOR csr_activity_product_id(cv_plan_id IN NUMBER, cv_product_id IN NUMBER) IS
9174   SELECT activity_product_id
9175   FROM ozf_funds_utilized_all_b
9176   WHERE plan_type = 'OFFR'
9177   AND org_id = MO_GLOBAL.GET_CURRENT_ORG_ID()
9178   AND plan_id = cv_plan_id
9179   AND product_level_type = 'PRODUCT'
9180   AND product_id = cv_product_id;
9181 
9182 CURSOR csr_offer_profile(cv_activity_product_id IN NUMBER) IS
9183   SELECT uom_code
9184   ,      quantity
9185   ,      scan_value
9186   ,      inventory_item_id
9187   FROM ams_act_products
9188   WHERE activity_product_id = cv_activity_product_id;
9189 
9190 CURSOR csr_offer_status(cv_offer_id IN NUMBER) IS
9191   SELECT status_code
9192        , transaction_currency_code
9193   FROM   ozf_offers
9194   WHERE  qp_list_header_id = cv_offer_id;
9195 
9196 BEGIN
9197 
9198     IF OZF_DEBUG_HIGH_ON THEN
9199       OZF_Utility_PVT.debug_message(l_full_name||': start');
9200     END IF;
9201 
9202     SAVEPOINT Get_Scan_Data_Details;
9203 
9204     IF px_funds_util_flt.activity_product_id IS NULL THEN
9205       OPEN csr_activity_product_id(px_funds_util_flt.activity_id, px_funds_util_flt.product_id);
9206       FETCH csr_activity_product_id INTO px_funds_util_flt.activity_product_id;
9207       CLOSE csr_activity_product_id;
9208     END IF;
9209 
9210     --For given offer and product, get the UOM, quantity, scan value.
9211     OPEN  csr_offer_profile(px_funds_util_flt.activity_product_id);
9212     FETCH csr_offer_profile INTO l_offer_uom_code
9213                                , l_offer_quantity
9214                                , G_SCAN_VALUE
9215                                , l_product_id;
9216     CLOSE csr_offer_profile;
9217 
9218     IF OZF_DEBUG_HIGH_ON THEN
9219       OZF_Utility_PVT.debug_message('l_offer_uom_code = '||l_offer_uom_code);
9220       OZF_Utility_PVT.debug_message('l_offer_quantity = '||l_offer_quantity);
9221       OZF_Utility_PVT.debug_message('l_product_id = '||l_product_id);
9222     END IF;
9223     --Get offer details for which accruals need to associated with claim.
9224     OPEN  csr_offer_status(px_funds_util_flt.activity_id);
9225     FETCH csr_offer_status INTO l_offer_status,px_currency_rec.transaction_currency_code;
9226     CLOSE csr_offer_status;
9227 
9228     --If UOM is not supplied to program unit, then product UOM defined for offer should be derived.
9229     IF px_funds_util_flt.uom_code IS NULL THEN
9230        l_asso_uom_code :=  l_offer_uom_code;
9231     ELSE
9232        l_asso_uom_code :=  px_funds_util_flt.uom_code;
9233     END IF;
9234     --Assign Coupon Count
9235     l_asso_total_units := px_funds_util_flt.total_units;
9236 
9237     IF OZF_DEBUG_HIGH_ON THEN
9238       OZF_Utility_PVT.debug_message('px_funds_util_flt.quantity = '||px_funds_util_flt.quantity);
9239       OZF_Utility_PVT.debug_message('px_funds_util_flt.total_units = '||px_funds_util_flt.total_units);
9240       OZF_Utility_PVT.debug_message('px_funds_util_flt.total_amount = '||px_funds_util_flt.total_amount);
9241       OZF_Utility_PVT.debug_message('l_asso_uom_code = '||l_asso_uom_code);
9242       OZF_Utility_PVT.debug_message('l_asso_total_units = '||l_asso_total_units);
9243       OZF_Utility_PVT.debug_message('l_offer_uom_code = '||l_offer_uom_code);
9244       OZF_Utility_PVT.debug_message('G_SCAN_VALUE = '||G_SCAN_VALUE);
9245     END IF;
9246 
9247     /*SCAN DATA Quantity Calculation:
9248       If there is mismatch between UOM supplied to program unit and derived from offer, then convert
9249       supplied quantity from supplied UOM to derived offer UOM. Otherwise, consider the quantity supplied
9250       to program unit.
9251       If quantity not supplied to program unit, then derive quantity from offer and calculate quantity as
9252       units times offer quantity. If there is mismatch between UOM supplied to program unit and UOM derived
9253       from offer, then convert quantity from offer UOM to UOM supplied to program unit.
9254 
9255       SCAN DATA Prorate Condition:
9256       If prorate flag is not checked, then re-calculate quantity on the basis of net units. Here, net units is
9257       difference of units between current association process and already associated lines. If there is UOM
9258       mismatch between supplied and derived from offer, then convert quantity from offer UOM to supplied UOM.
9259     */
9260     IF px_funds_util_flt.total_units IS NULL AND G_SCAN_VALUE <> 0 THEN --Moved this code up for EA -- Modified condition for Bug 15891695
9261         --l_asso_total_units := l_asso_total_quantity / G_SCAN_VALUE;
9262         --Added by BKUNJAN
9263         l_asso_total_units := px_funds_util_flt.total_amount / G_SCAN_VALUE;
9264 
9265     ELSE
9266         l_asso_total_units := px_funds_util_flt.total_units;
9267     END IF;
9268 
9269     IF px_funds_util_flt.quantity IS NOT NULL THEN
9270       IF l_asso_uom_code <> l_offer_uom_code THEN
9271           l_asso_total_quantity := inv_convert.inv_um_convert ( item_id         => l_product_id
9272                                                               , precision       => 2
9273                                                               , from_quantity   => px_funds_util_flt.quantity
9274                                                               , from_unit       => px_funds_util_flt.uom_code
9275                                                               , to_unit         => l_offer_uom_code
9276                                                               , from_name       => NULL
9277                                                               , to_name         => NULL
9278                                                               );
9279           IF l_asso_total_quantity = -99999 THEN
9280             IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
9281               FND_MESSAGE.set_name('OZF', 'OZF_CLAIM_CANT_CONVERT_UOM');
9282               FND_MSG_PUB.add;
9283             END IF;
9284             RAISE FND_API.g_exc_error;
9285           END IF;
9286       ELSE
9287          l_asso_total_quantity := px_funds_util_flt.quantity;
9288       END IF;
9289        l_asso_total_units := l_asso_total_quantity / l_offer_quantity;
9290     ELSE
9291       IF l_asso_total_units IS NOT NULL THEN
9292         --IF p_prorate_req_flag THEN
9293             --l_asso_total_quantity := l_asso_total_units * l_offer_quantity;
9294         --ELSE
9295         --  l_net_total_units := NVL(l_asso_total_units, 0) - NVL(px_funds_util_flt.old_total_units, 0);
9296         --  l_asso_total_quantity := l_net_total_units * l_offer_quantity;
9297         --END IF;
9298         IF l_asso_uom_code <> l_offer_uom_code THEN
9299           l_asso_total_quantity := inv_convert.inv_um_convert ( item_id         => l_product_id
9300                                                               , precision       => 2
9301                                                               , from_quantity   => l_asso_total_units * l_offer_quantity
9302                                                               , from_unit       => l_offer_uom_code
9303                                                               , to_unit         => px_funds_util_flt.uom_code
9304                                                               , from_name       => NULL
9305                                                               , to_name         => NULL
9306                                                               );
9307           IF l_asso_total_quantity = -99999 THEN
9308             IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
9309               FND_MESSAGE.set_name('OZF', 'OZF_CLAIM_CANT_CONVERT_UOM');
9310               FND_MSG_PUB.add;
9311             END IF;
9312             RAISE FND_API.g_exc_error;
9313           END IF;
9314         ELSE
9315             l_asso_total_quantity := l_asso_total_units * l_offer_quantity;
9316         END IF;
9317       --ELSE --//BKUNJAN
9318       --   l_asso_total_units := px_funds_util_flt.total_amount / G_SCAN_VALUE;
9319       END IF;
9320     END IF;  -- IF px_funds_util_flt.quantity IS NOT NULL THEN
9321 
9322 
9323     --SCAN DATA offers, which are not processed from IDSM Batch ,
9324     --re-calculate association amount as unit times scan value.
9325     IF (p_batch_type = 'BATCH' and p_source_object_class = 'SPECIAL_PRICE') THEN
9326        NULL;
9327     ELSE
9328        px_funds_util_flt.total_amount := l_asso_total_units * G_SCAN_VALUE;
9329     END IF;
9330 
9331     --//BKUNJAN moved this code to down
9332    --If no units supplied to program unit, then unit will be association amount per scan value.
9333 
9334 
9335     --Calculate net associate amount as difference between association amount and
9336     --already associated amount with current claim line.
9337     l_net_asso_amount :=  NVL(ABS(px_funds_util_flt.total_amount), 0) - NVL(ABS(px_funds_util_flt.old_total_amount),0);
9338 
9339     --Raise error, if net association amount exists and offer is COMPLETED.
9340     IF l_net_asso_amount <> 0 THEN
9341       IF p_offer_status = 'COMPLETED' THEN
9342         IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
9343             FND_MESSAGE.set_name('OZF', 'OZF_CLAIM_ASSO_COMPLETE_OFFER');
9344             FND_MSG_PUB.add;
9345         END IF;
9346         RAISE FND_API.g_exc_error;
9347       END IF;
9348     END IF;
9349 
9350     IF OZF_DEBUG_HIGH_ON THEN
9351       OZF_Utility_PVT.debug_message('l_asso_total_quantity = '||l_asso_total_quantity);
9352       OZF_Utility_PVT.debug_message('l_asso_total_units = '||l_asso_total_units);
9353       OZF_Utility_PVT.debug_message('l_asso_uom_code = '||l_asso_uom_code);
9354     END IF;
9355 
9356     px_funds_util_flt.total_units := l_asso_total_units;
9357     px_funds_util_flt.quantity    := l_asso_total_quantity;
9358     px_funds_util_flt.uom_code    := l_asso_uom_code;
9359     x_offer_quantity := l_offer_quantity;
9360 
9361     IF OZF_DEBUG_HIGH_ON THEN
9362       OZF_Utility_PVT.debug_message(l_full_name||': end');
9363     END IF;
9364 
9365 EXCEPTION
9366   WHEN FND_API.g_exc_error THEN
9367     ROLLBACK TO Get_Scan_Data_Details;
9368     x_return_status := FND_API.g_ret_sts_error;
9369     FND_MSG_PUB.count_and_get (
9370            p_encoded => FND_API.g_false
9371           ,p_count   => x_msg_count
9372           ,p_data    => x_msg_data
9373     );
9374 
9375   WHEN FND_API.g_exc_unexpected_error THEN
9376     ROLLBACK TO Get_Scan_Data_Details;
9377     x_return_status := FND_API.g_ret_sts_unexp_error ;
9378     FND_MSG_PUB.count_and_get (
9379            p_encoded => FND_API.g_false
9380           ,p_count   => x_msg_count
9381           ,p_data    => x_msg_data
9382     );
9383 
9384   WHEN OTHERS THEN
9385     ROLLBACK TO Get_Scan_Data_Details;
9386     x_return_status := FND_API.g_ret_sts_unexp_error ;
9387     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
9388       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
9389     END IF;
9390     FND_MSG_PUB.count_and_get(
9391            p_encoded => FND_API.g_false
9392           ,p_count   => x_msg_count
9393           ,p_data    => x_msg_data
9394     );
9395 
9396 END Get_Scan_Data_Details;
9397 
9398 ---------------------------------------------------------------------
9399 -- PROCEDURE
9400 --    Get_Pay_Over_Amount
9401 -- PURPOSE
9402 --    If pay over earnings is allowed then this program unit
9403 --    calculates pay over amount for each qualified accrual of
9404 --    current association process
9405 -- HISTORY
9406 --    08/04/2009  psomyaju  Created.
9407 ---------------------------------------------------------------------
9408 
9409 PROCEDURE  Get_Pay_Over_Amount   ( p_util              IN  funds_rem_tbl_type
9410                                  , p_claim_amt         IN  NUMBER
9411                                  , p_claim_exc_rate    IN  NUMBER  DEFAULT 1
9412                                  , p_claim_exc_date    IN  DATE
9413                                  , p_claim_exc_type    IN  VARCHAR2
9414                                  , p_currency_rec      IN  currency_rec_type
9415                                  , x_pay_over_flag     OUT NOCOPY BOOLEAN
9416                                  , x_pay_over_amount   OUT NOCOPY NUMBER
9417                                  , x_return_status     OUT NOCOPY VARCHAR2
9418                                  , x_msg_count         OUT NOCOPY NUMBER
9419                                  , x_msg_data          OUT NOCOPY VARCHAR2
9420                                  )
9421 IS
9422 
9423 l_api_version   CONSTANT  NUMBER       := 1.0;
9424 l_api_name      CONSTANT  VARCHAR2(30) := 'Get_Pay_Over_Amount';
9425 l_full_name     CONSTANT  VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
9426 
9427 l_accrual_exchange_rate   NUMBER       := 1;
9428 l_accrual_exchange_date   DATE;
9429 l_accrual_exchange_type   VARCHAR2(30);
9430 --l_plan_curr_amount_rem    NUMBER       := 0; --ninarasi bug fix for 13550004 removed as part of performance fix
9431 --l_plan_curr_amount        NUMBER       := 0; --ninarasi bug fix for 13550004 removed as part of performance fix
9432 l_exec_curr_amount_rem    NUMBER       := 0;
9433 l_total_amt_rem           NUMBER       := 0;
9434 --l_acctd_amount_remaining  NUMBER       := 0; --ninarasi bug fix for 13550004 removed as part of performance fix
9435 --l_plan_currency_code      VARCHAR2(30);  --ninarasi bug fix for 13550004 removed as part of performance fix
9436 l_return_status           VARCHAR2(1);
9437 
9438 /*CURSOR c_util_dtls (cv_util_id IN NUMBER) IS
9439   SELECT   plan_currency_code
9440          , plan_curr_amount_remaining
9441          , plan_curr_amount
9442          , acctd_amount_remaining
9443   FROM     ozf_funds_utilized_all_b   util
9444   WHERE    utilization_id = cv_util_id;*/
9445 
9446 
9447 BEGIN
9448 
9449   IF OZF_DEBUG_HIGH_ON THEN
9450      OZF_Utility_PVT.debug_message(l_full_name||': start');
9451   END IF;
9452 
9453   SAVEPOINT Get_Pay_Over_Amount;
9454 
9455   -- Amount remaining will be calculated for all the utilizations qualified for associate earnings.
9456   IF p_util.COUNT > 0 THEN
9457     FOR i IN  p_util.FIRST..p_util.LAST
9458     LOOP
9459       /*OPEN  c_util_dtls(p_util(i).utilization_id); --ninarasi bug fix for 13550004
9460       FETCH c_util_dtls INTO  l_plan_currency_code              -- Transactional Currency
9461                             , l_plan_curr_amount_rem            -- Accrual amount remaining in transactional currency
9462                             , l_plan_curr_amount                -- Accrual amount in transactional currency
9463                             , l_acctd_amount_remaining;
9464       CLOSE c_util_dtls;
9465 
9466       IF OZF_DEBUG_HIGH_ON THEN
9467         OZF_Utility_PVT.debug_message('l_plan_currency_code = ' ||l_plan_currency_code);
9468         OZF_Utility_PVT.debug_message('l_plan_curr_amount_rem = ' ||l_plan_curr_amount_rem);
9469         OZF_Utility_PVT.debug_message('l_plan_curr_amount = ' ||l_plan_curr_amount);
9470         OZF_Utility_PVT.debug_message('l_acctd_amount_remaining = ' ||l_acctd_amount_remaining);
9471       END IF;*/
9472 
9473       IF  p_currency_rec.association_currency_code = p_currency_rec.transaction_currency_code THEN
9474         l_total_amt_rem := l_total_amt_rem + NVL(p_util(i).plan_curr_amount_remaining,0);
9475       ELSE
9476         l_total_amt_rem := l_total_amt_rem + NVL(p_util(i).acctd_amount_remaining,0);
9477       END IF;
9478     END LOOP;
9479   END IF;
9480 
9481 --nepanda : fix for bug # 9508390  - issue # 3
9482 x_pay_over_amount := p_claim_amt - l_total_amt_rem;
9483 IF x_pay_over_amount > 0 THEN
9484    x_pay_over_flag := TRUE;
9485 ELSE
9486    x_pay_over_amount := 0;
9487 END IF;
9488  /* Fix for Bug 12930435 */
9489   -- Pay over validation
9490   IF ( l_total_amt_rem >= 0 AND
9491        p_claim_amt > l_total_amt_rem
9492      ) OR
9493      ( l_total_amt_rem < 0 AND
9494        p_claim_amt < l_total_amt_rem
9495      ) THEN
9496 
9497        x_pay_over_flag := TRUE;
9498 
9499        x_pay_over_amount := p_claim_amt - l_total_amt_rem; --kdass
9500 
9501   ELSE
9502 
9503       x_pay_over_amount := 0; --kdass
9504   END IF;
9505 
9506   IF OZF_DEBUG_HIGH_ON THEN
9507     OZF_Utility_PVT.debug_message('l_exec_curr_amount_rem = ' || l_exec_curr_amount_rem);
9508     OZF_Utility_PVT.debug_message('l_total_amt_rem = ' || l_total_amt_rem);
9509     OZF_Utility_PVT.debug_message('p_claim_amt = ' || p_claim_amt);
9510     OZF_Utility_PVT.debug_message('x_pay_over_amount = ' || x_pay_over_amount);
9511     IF x_pay_over_flag THEN
9512       OZF_Utility_PVT.debug_message('x_pay_over_flag = TRUE');
9513     ELSE
9514       OZF_Utility_PVT.debug_message('x_pay_over_flag = FALSE');
9515     END IF;
9516   END IF;
9517 
9518   IF OZF_DEBUG_HIGH_ON THEN
9519      OZF_Utility_PVT.debug_message(l_full_name||': end');
9520   END IF;
9521 
9522 EXCEPTION
9523   WHEN FND_API.g_exc_error THEN
9524     ROLLBACK TO Get_Pay_Over_Amount;
9525     x_return_status := FND_API.g_ret_sts_error;
9526     FND_MSG_PUB.count_and_get (
9527            p_encoded => FND_API.g_false
9528           ,p_count   => x_msg_count
9529           ,p_data    => x_msg_data
9530     );
9531 
9532   WHEN FND_API.g_exc_unexpected_error THEN
9533     ROLLBACK TO Get_Pay_Over_Amount;
9534     x_return_status := FND_API.g_ret_sts_unexp_error ;
9535     FND_MSG_PUB.count_and_get (
9536            p_encoded => FND_API.g_false
9537           ,p_count   => x_msg_count
9538           ,p_data    => x_msg_data
9539     );
9540 
9541   WHEN OTHERS THEN
9542     ROLLBACK TO Get_Pay_Over_Amount;
9543     x_return_status := FND_API.g_ret_sts_unexp_error ;
9544     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
9545       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
9546     END IF;
9547     FND_MSG_PUB.count_and_get(
9548            p_encoded => FND_API.g_false
9549           ,p_count   => x_msg_count
9550           ,p_data    => x_msg_data
9551     );
9552 
9553 END Get_Pay_Over_Amount;
9554 
9555 ---------------------------------------------------------------------
9556 -- PROCEDURE
9557 --    Update_Group_Line_Util
9558 --
9559 -- HISTORY
9560 --    10/05/2001  mchang  Create.
9561 --    11/03/2009  psomyaju  Re-organized code for R12 multicurrency ER
9562 ---------------------------------------------------------------------
9563 PROCEDURE Update_Group_Line_Util(
9564    p_api_version         IN  NUMBER
9565   ,p_init_msg_list       IN  VARCHAR2  := FND_API.g_false
9566   ,p_commit              IN  VARCHAR2  := FND_API.g_false
9567   ,p_validation_level    IN  NUMBER    := FND_API.g_valid_level_full
9568 
9569   ,x_return_status       OUT NOCOPY VARCHAR2
9570   ,x_msg_count           OUT NOCOPY NUMBER
9571   ,x_msg_data            OUT NOCOPY VARCHAR2
9572 
9573   ,p_summary_view        IN  VARCHAR2  := NULL
9574   ,p_funds_util_flt      IN  funds_util_flt_type
9575   ,p_mode                IN  VARCHAR2  := OZF_CLAIM_UTILITY_PVT.g_auto_mode
9576 )
9577 IS
9578 l_api_version   CONSTANT NUMBER       := 1.0;
9579 l_api_name      CONSTANT VARCHAR2(30) := 'Update_Group_Line_Util';
9580 l_full_name     CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
9581 l_return_status          VARCHAR2(1);
9582 l_msg_count               NUMBER;
9583 l_msg_data                VARCHAR2(30);
9584 
9585 l_funds_util_flt          funds_util_flt_type   :=  p_funds_util_flt;
9586 l_line_util_tbl           line_util_tbl_type;
9587 l_funds_rem_tbl           funds_rem_tbl_type;
9588 l_currency_rec            currency_rec_type;
9589 
9590 
9591 l_asso_amount             NUMBER;
9592 l_exists_asso_amount      NUMBER := 0;
9593 --l_exists_asso_amount      NUMBER;
9594 l_net_asso_amount         NUMBER;
9595 l_cre_util_amount         NUMBER;
9596 l_create_util             BOOLEAN := TRUE;
9597 l_claim_status            VARCHAR2(15);
9598 l_source_object_class     VARCHAR2(1024);
9599 l_batch_type              VARCHAR2(1024);
9600 l_claim_date              DATE;
9601 l_claim_exc_type          VARCHAR2(1024);
9602 l_claim_exc_date          DATE;
9603 l_claim_exc_rate          NUMBER;
9604 l_source_object_id        NUMBER;
9605 l_request_header_id       NUMBER;       --Bugfix : 7717638
9606 l_batch_line_id           NUMBER;       --Bugfix : 7811671
9607 l_convert_exchange_rate   NUMBER;
9608 l_prorate_earnings_flag   VARCHAR2(15);
9609 l_prorate_req_flag        BOOLEAN := FALSE;
9610 l_pay_over_amount         NUMBER;
9611 l_tot_accrual_amt         NUMBER := 0;
9612 l_error_index             NUMBER;
9613 l_asso_total_units        NUMBER;
9614 l_asso_uom_code           VARCHAR2(15);
9615 l_asso_total_quantity     NUMBER;
9616 l_org_id                  NUMBER;
9617 l_claim_amt               NUMBER := 0;
9618 l_offer_currency          VARCHAR2(15);
9619 l_offer_status            VARCHAR2(30);
9620 l_pay_over_flag           BOOLEAN := FALSE;
9621 l_created_from            VARCHAR2(30);
9622 
9623 l_new_line_amount NUMBER := 0;
9624 l_util_quantity NUMBER := 0; --EA ninarasi fix for 14016226
9625 l_new_total_units NUMBER := 0; --EA
9626 l_offer_quantity NUMBER := 0; --EA
9627 
9628 -- Added for fix 11793070
9629 l_line_tbl           OZF_CLAIM_LINE_PVT.claim_line_tbl_type;
9630 
9631 CURSOR csr_function_currency(cv_org_id NUMBER) IS
9632   SELECT gs.currency_code
9633   FROM   gl_sets_of_books gs
9634   ,      ozf_sys_parameters org
9635   WHERE  org.set_of_books_id = gs.set_of_books_id
9636   AND   org.org_id = cv_org_id;
9637 
9638 -- Bugfix 5404951
9639 CURSOR csr_claim_status(cv_claim_line_id IN NUMBER) IS
9640   SELECT cla.status_code
9641        , cla.source_object_class
9642        , cla.batch_type
9643        , cla.currency_code
9644        , cla.creation_date
9645        , cla.exchange_rate_type
9646        , cla.exchange_rate_date
9647        , cla.exchange_rate
9648        , cla.source_object_id
9649        , cln.activity_id
9650        , cln.batch_line_id
9651        , cla.created_from
9652        , cla.org_id
9653   FROM   ozf_claims cla, ozf_claim_lines cln
9654   WHERE  cla.claim_id = cln.claim_id
9655     AND  cln.claim_line_id = cv_claim_line_id;
9656 
9657 BEGIN
9658   --------------------- initialize -----------------------
9659   SAVEPOINT Update_Group_Line_Util;
9660   x_return_status := FND_API.g_ret_sts_success;
9661 
9662   --Set to handle rounding issue at Update_Fund_Utils
9663  G_ENTERED_AMOUNT        := l_funds_util_flt.total_amount;
9664 
9665   IF OZF_DEBUG_HIGH_ON THEN
9666      OZF_Utility_PVT.debug_message(l_full_name||': start');
9667   END IF;
9668 
9669   IF FND_API.to_boolean(p_init_msg_list) THEN
9670     FND_MSG_PUB.initialize;
9671   END IF;
9672 
9673   IF NOT FND_API.compatible_api_call(
9674          l_api_version,
9675          p_api_version,
9676          l_api_name,
9677          g_pkg_name
9678   ) THEN
9679     RAISE FND_API.g_exc_unexpected_error;
9680   END IF;
9681 
9682    ----------------- copy line info to filter ---------------
9683   IF l_funds_util_flt.claim_line_id IS NOT NULL THEN
9684     Copy_Util_Flt(px_funds_util_flt => l_funds_util_flt);
9685   END IF;
9686 
9687 
9688   --Association processing depends on prorate condition. Get the details of prorate check
9689   --at system parameter level and claim lines level.
9690   Get_Prorate_Earnings_Flag ( p_funds_util_flt         => l_funds_util_flt
9691                             , x_prorate_earnings_flag  => l_prorate_earnings_flag
9692                             );
9693 
9694 
9695 
9696   --For SCAN DATA offers prorate is not required. Also, if claim line is not associated with any
9697   --earnings then we need not require to do earnings on prorate basis.
9698   IF  l_funds_util_flt.offer_type = 'SCAN_DATA' OR
9699       NVL(l_prorate_earnings_flag,'F') = 'F' OR
9700       NVL(l_funds_util_flt.old_total_amount ,0) = 0 THEN
9701         l_prorate_req_flag := FALSE;
9702   ELSE
9703         l_prorate_req_flag := TRUE;
9704   END IF;
9705 
9706    --In case of SCAN DATA offers, association amount based on quantity, UOM and units accrured. Hence,
9707   --we need to retrieve these specific information for SCAN DATA offers processing.
9708   IF l_funds_util_flt.offer_type = 'SCAN_DATA' THEN
9709      --Calculate association amount, quantity, total units and TRANSACTIONAL currency of SCAN DATA offer.
9710      --Since, association amount calculated for SCAN DATA offers based on offer quantity and scan units,
9711      --Hence, this amount will always be in TRANSACTIONAL currency.
9712 
9713     Get_Scan_Data_Details ( p_offer_status         => l_offer_status
9714                            , p_batch_type           => l_batch_type
9715                            , p_source_object_class  => l_source_object_class
9716                            , px_funds_util_flt      => l_funds_util_flt
9717                            , px_currency_rec        => l_currency_rec
9718 			   , x_offer_quantity       => l_offer_quantity
9719                            , x_return_status        => l_return_status
9720                            , x_msg_count            => l_msg_count
9721                            , x_msg_data             => l_msg_data
9722                            );
9723 
9724      IF l_return_status = FND_API.g_ret_sts_error THEN
9725         RAISE FND_API.g_exc_error;
9726      ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
9727         RAISE FND_API.g_exc_error;
9728      END IF;
9729 
9730      --For SCAN DATA offers, if units or quantity will be NULL then association amount will be NULL.
9731      --Abort process, as without these entities, association cannot be done.
9732      IF l_funds_util_flt.total_units IS NULL OR
9733         l_funds_util_flt.quantity IS NULL OR
9734         l_funds_util_flt.total_amount IS NULL THEN
9735         RETURN;
9736      END IF;
9737 
9738      -- Added for fix 11793070
9739 
9740      IF(l_funds_util_flt.claim_line_id IS NULL OR l_funds_util_flt.claim_line_id = FND_API.G_MISS_NUM
9741         AND l_funds_util_flt.created_from = 'ASSOUI') THEN
9742 	--Construct the claim line record
9743 	l_line_tbl(1).claim_id                := l_funds_util_flt.claim_id;
9744 	l_line_tbl(1).claim_currency_amount   := l_funds_util_flt.total_amount;
9745 	l_line_tbl(1).prorate_earnings_flag   := 'T';
9746 	l_line_tbl(1).PERFORMANCE_COMPLETE_FLAG   := 'F';
9747 	l_line_tbl(1).PERFORMANCE_ATTACHED_FLAG   := 'F';
9748 	l_line_tbl(1).SELECT_CUST_CHILDREN_FLAG   := 'F';
9749 
9750 	-- Create Claim Line
9751 	OZF_CLAIM_LINE_PVT.Create_Claim_Line_Tbl(
9752 	p_api_version       => 1.0
9753 	,p_init_msg_list     => FND_API.g_false
9754 	,p_commit            => FND_API.g_false
9755 	,p_validation_level  => FND_API.g_valid_level_full
9756 	,x_return_status     => l_return_status
9757 	,x_msg_count         => l_msg_count
9758 	,x_msg_data          => l_msg_data
9759 	,p_claim_line_tbl    => l_line_tbl
9760 	,x_error_index       => l_error_index
9761 	);
9762 
9763 	IF l_return_status = FND_API.g_ret_sts_error THEN
9764 	RAISE FND_API.g_exc_error;
9765 	ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
9766 	RAISE FND_API.g_exc_error;
9767 	END IF;
9768 
9769 	l_funds_util_flt.claim_line_id := l_line_tbl(1).claim_line_id;
9770      END IF;
9771   ELSE
9772     --Transactional currency (OFFER or ORDER currency) supposed to be passed to this program unit.
9773     IF l_funds_util_flt.utiz_currency_code IS NOT NULL THEN
9774        l_currency_rec.transaction_currency_code :=  l_funds_util_flt.utiz_currency_code;
9775     END IF;
9776     IF OZF_DEBUG_HIGH_ON THEN
9777        ozf_utility_pvt.debug_message('l_funds_util_flt.utiz_currency_code  = '||l_funds_util_flt.utiz_currency_code);
9778        ozf_utility_pvt.debug_message('l_currency_rec.transaction_currency_code = '||l_currency_rec.transaction_currency_code);
9779     END IF;
9780   END IF;
9781 
9782   --Get claim details of claim for which association is taking place.
9783   OPEN csr_claim_status(l_funds_util_flt.claim_line_id);
9784   FETCH csr_claim_status INTO   l_claim_status
9785                               , l_source_object_class
9786                               , l_batch_type
9787                               , l_currency_rec.claim_currency_code
9788                               , l_claim_date
9789                               , l_claim_exc_type
9790                               , l_claim_exc_date
9791                               , l_claim_exc_rate
9792                               , l_source_object_id
9793                               , l_request_header_id
9794                               , l_batch_line_id
9795                               , l_created_from
9796                               , l_org_id;
9797   CLOSE csr_claim_status;
9798 
9799   --Get FUNCTIONAL currency from system parameters
9800   OPEN  csr_function_currency(l_org_id);
9801   FETCH csr_function_currency INTO l_currency_rec.functional_currency_code;
9802   CLOSE csr_function_currency;
9803 
9804   --Set UNIVERSAL currency from profile.
9805   l_currency_rec.universal_currency_code := FND_PROFILE.VALUE('OZF_TP_COMMON_CURRENCY');
9806   IF OZF_DEBUG_HIGH_ON THEN
9807     ozf_utility_pvt.debug_message('Year Name = '||l_funds_util_flt.year_name);
9808     ozf_utility_pvt.debug_message('Activity Type Passed from UI = '||l_funds_util_flt.activity_type);
9809     ozf_utility_pvt.debug_message('l_claim_status = '||l_claim_status);
9810     ozf_utility_pvt.debug_message('l_claim_date = '||l_claim_date);
9811     ozf_utility_pvt.debug_message('l_claim_exc_type = '||l_claim_exc_type);
9812     ozf_utility_pvt.debug_message('l_claim_exc_date = '||l_claim_exc_date);
9813     ozf_utility_pvt.debug_message('l_claim_exc_rate = '||l_claim_exc_rate);
9814     ozf_utility_pvt.debug_message('l_source_object_id = '||l_source_object_id);
9815     ozf_utility_pvt.debug_message('l_source_object_class = '||l_source_object_class);
9816     ozf_utility_pvt.debug_message('l_request_header_id = '||l_request_header_id);
9817     ozf_utility_pvt.debug_message('l_batch_line_id = '||l_batch_line_id);
9818     ozf_utility_pvt.debug_message('l_batch_type = '||l_batch_type);
9819     ozf_utility_pvt.debug_message('l_created_from = '||l_created_from);
9820     ozf_utility_pvt.debug_message('p_summary_view = '||p_summary_view);
9821     ozf_utility_pvt.debug_message('l_org_id = '||l_org_id);
9822     ozf_utility_pvt.debug_message('Passed Association Amount = '||l_funds_util_flt.total_amount);
9823     ozf_utility_pvt.debug_message('Existing Association Amount = '||l_funds_util_flt.old_total_amount);
9824   END IF;
9825 
9826   --Raise error, if claim is not OPEN status and associate earnings is happening.
9827   IF l_claim_status <> 'OPEN' THEN
9828     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
9829       FND_MESSAGE.set_name('OZF', 'OZF_CLAIM_ASSO_NOT_OPEN');
9830       FND_MSG_PUB.add;
9831     END IF;
9832     RAISE FND_API.g_exc_error;
9833   END IF;
9834 
9835 
9836  IF OZF_DEBUG_HIGH_ON THEN
9837     ozf_utility_pvt.debug_message('l_prorate_earnings_flag = '||l_prorate_earnings_flag);
9838     ozf_utility_pvt.debug_message('l_funds_util_flt.offer_type = '||l_funds_util_flt.offer_type);
9839     ozf_utility_pvt.debug_message('l_funds_util_flt.old_total_amount = '||l_funds_util_flt.old_total_amount);
9840   END IF;
9841 
9842     l_original_total_amount :=l_funds_util_flt.total_amount;
9843 
9844  IF OZF_DEBUG_HIGH_ON THEN
9845     ozf_utility_pvt.debug_message('l_funds_util_flt.total_units = '||l_funds_util_flt.total_units);
9846     ozf_utility_pvt.debug_message('l_funds_util_flt.quantity  = '||l_funds_util_flt.quantity );
9847     ozf_utility_pvt.debug_message('l_funds_util_flt.total_amount = '||l_funds_util_flt.total_amount);
9848     ozf_utility_pvt.debug_message('l_currency_rec.transaction_currency_code = '||l_currency_rec.transaction_currency_code);
9849     ozf_utility_pvt.debug_message('l_currency_rec.claim_currency_code =  '||l_currency_rec.claim_currency_code);
9850     ozf_utility_pvt.debug_message('l_currency_rec.functional_currency_code = '||l_currency_rec.functional_currency_code);
9851   END IF;
9852 
9853   --Association can be done in either TRANSACTIONAL currency or FUNCTIONAL currency. We will set GLOBAL variable
9854   --G_ASSO_CURRENCY accordingly, so that entire association will be in single known currency and we need NOT to
9855   --convert them on case by case basis.
9856    IF l_currency_rec.claim_currency_code = l_currency_rec.transaction_currency_code THEN
9857       l_currency_rec.association_currency_code  := l_currency_rec.transaction_currency_code;
9858       l_asso_amount                             := l_funds_util_flt.total_amount;
9859       l_exists_asso_amount                      := l_funds_util_flt.old_total_amount;
9860       l_net_asso_amount                         := NVL(ABS(l_asso_amount), 0) - NVL(ABS(l_exists_asso_amount),0);
9861   ELSE
9862       IF l_funds_util_flt.total_amount IS NOT NULL AND l_funds_util_flt.total_amount <> 0 THEN
9863          l_currency_rec.association_currency_code       := l_currency_rec.functional_currency_code;
9864 
9865         IF l_currency_rec.claim_currency_code <> l_currency_rec.functional_currency_code THEN
9866            OZF_UTILITY_PVT.Convert_Currency ( p_from_currency   => l_currency_rec.claim_currency_code
9867                                             , p_to_currency     => l_currency_rec.functional_currency_code
9868                                             , p_conv_type       => l_claim_exc_type
9869                                             , p_conv_rate       => l_claim_exc_rate
9870                                             , p_conv_date       => l_claim_exc_date
9871                                             , p_from_amount     => l_funds_util_flt.total_amount
9872                                             , x_return_status   => l_return_status
9873                                             , x_to_amount       => l_asso_amount
9874                                             , x_rate            => l_convert_exchange_rate
9875                                             );
9876 
9877           IF l_return_status = FND_API.g_ret_sts_error THEN
9878             RAISE FND_API.g_exc_error;
9879           ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
9880             RAISE FND_API.g_exc_error;
9881           END IF;
9882 	  --ninarasi fix for bug 14089950
9883 	  --do not round the amount for scandata because it is units and quantity gets recalculated again in Populate_Line_Util_Tbl
9884           IF l_asso_amount IS NOT NULL AND l_funds_util_flt.offer_type <> 'SCAN_DATA' THEN
9885             l_asso_amount := OZF_UTILITY_PVT.CurrRound(l_asso_amount, l_currency_rec.association_currency_code);
9886           END IF;
9887         ELSE
9888           l_asso_amount := l_funds_util_flt.total_amount;
9889         END IF;
9890       ELSE
9891         l_asso_amount := 0;
9892       END IF;
9893 
9894       IF l_funds_util_flt.old_total_amount IS NOT NULL AND l_funds_util_flt.old_total_amount <> 0 THEN
9895         IF l_currency_rec.claim_currency_code <> l_currency_rec.association_currency_code THEN
9896           OZF_UTILITY_PVT.Convert_Currency ( p_from_currency   => l_currency_rec.claim_currency_code
9897                                            , p_to_currency     => l_currency_rec.functional_currency_code
9898                                            , p_conv_type       => l_claim_exc_type
9899                                            , p_conv_rate       => l_claim_exc_rate
9900                                            , p_conv_date       => l_claim_exc_date
9901                                            , p_from_amount     => l_funds_util_flt.old_total_amount
9902                                            , x_return_status   => l_return_status
9903                                            , x_to_amount       => l_exists_asso_amount
9904                                            , x_rate            => l_convert_exchange_rate
9905                                            );
9906 
9907           IF l_return_status = FND_API.g_ret_sts_error THEN
9908             RAISE FND_API.g_exc_error;
9909           ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
9910             RAISE FND_API.g_exc_error;
9911           END IF;
9912           IF l_exists_asso_amount IS NOT NULL THEN
9913             l_exists_asso_amount := OZF_UTILITY_PVT.CurrRound(l_exists_asso_amount, l_currency_rec.association_currency_code);
9914           END IF;
9915         ELSE
9916           l_exists_asso_amount := l_funds_util_flt.old_total_amount;
9917         END IF;
9918       ELSE
9919         l_exists_asso_amount := 0;
9920       END IF;
9921 
9922       l_net_asso_amount := NVL(ABS(l_asso_amount), 0) - NVL(ABS(l_exists_asso_amount),0);
9923   END IF;
9924 
9925   IF l_net_asso_amount IS NOT NULL THEN
9926     l_net_asso_amount := OZF_UTILITY_PVT.CurrRound(l_net_asso_amount, l_currency_rec.association_currency_code);
9927   END IF;
9928 
9929   IF OZF_DEBUG_HIGH_ON THEN
9930     ozf_utility_pvt.debug_message('l_currency_rec.association_currency_code = '||l_currency_rec.association_currency_code);
9931     ozf_utility_pvt.debug_message('l_asso_amount  = '||l_asso_amount);
9932     ozf_utility_pvt.debug_message('l_exists_asso_amount = '||l_exists_asso_amount);
9933     ozf_utility_pvt.debug_message('l_net_asso_amount = '||l_net_asso_amount);
9934   END IF;
9935 
9936   /* Prorate Condition:
9937   If prorate flag is set, then identify all the qualified utilizations for current association
9938   and reduce amount remaining with current association amount. Remove existing associations with
9939   current claim line and calculate fresh association earnings for claim line..
9940   Otherwise, if prorate flag is not set, and association amount is smaller than already associated
9941   amount with current claim line then reduce amount remaining of qualified utilizations as well as
9942   already associated amounts. Re-calculate FXGL for reduced associated amounts. No fresh association
9943   should be done.
9944   If prorate is not set and association amount is larger than already associated amount, then do association
9945   with supplied association amount.
9946   */
9947 
9948   IF l_prorate_req_flag THEN
9949          Delete_All_Line_Util ( p_api_version       => l_api_version
9950                               , p_init_msg_list     => FND_API.g_false
9951                               , p_commit            => FND_API.g_false
9952                               , p_validation_level  => FND_API.g_valid_level_full
9953                               , x_return_status     => l_return_status
9954                               , x_msg_data          => x_msg_data
9955                               , x_msg_count         => x_msg_count
9956                               , p_currency_rec      => l_currency_rec
9957                               , p_funds_util_flt    => l_funds_util_flt
9958                               );
9959 
9960          IF l_return_status = FND_API.g_ret_sts_error THEN
9961            RAISE FND_API.g_exc_error;
9962          ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
9963            RAISE FND_API.g_exc_error;
9964          END IF;
9965          l_cre_util_amount := NVL(l_asso_amount, 0);
9966   ELSE
9967       IF l_net_asso_amount < 0 THEN
9968          Delete_Group_Line_Util ( p_api_version       => l_api_version
9969                                 , p_init_msg_list     => FND_API.g_false
9970                                 , p_commit            => FND_API.g_false
9971                                 , p_validation_level  => FND_API.g_valid_level_full
9972                                 , x_return_status     => l_return_status
9973                                 , x_msg_data          => x_msg_data
9974                                 , x_msg_count         => x_msg_count
9975                                 , p_funds_util_flt    => l_funds_util_flt
9976                                 );
9977 
9978          IF l_return_status = FND_API.g_ret_sts_error THEN
9979            RAISE FND_API.g_exc_error;
9980          ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
9981            RAISE FND_API.g_exc_error;
9982          END IF;
9983          l_create_util := FALSE;
9984       ELSE
9985          l_cre_util_amount := NVL(l_asso_amount, 0) - NVL(l_exists_asso_amount ,0);
9986          l_cre_util_amount := OZF_UTILITY_PVT.CurrRound(l_cre_util_amount, l_currency_rec.association_currency_code);
9987       END IF;
9988   END IF;
9989 
9990   IF OZF_DEBUG_HIGH_ON THEN
9991     ozf_utility_pvt.debug_message('l_cre_util_amount  = '||l_cre_util_amount);
9992     IF l_create_util THEN
9993       ozf_utility_pvt.debug_message('l_create_util = TRUE');
9994     ELSE
9995       ozf_utility_pvt.debug_message('l_create_util = FALSE');
9996     END IF;
9997   END IF;
9998 
9999   /* Associate Earnings:
10000   Identify all the utilizations qualifies for current association based on customer, offer
10001   and product combinations. If prorate is checked, then calculate association amount based
10002   on prorate basis.
10003   */
10004 
10005   IF l_create_util THEN
10006       Populate_Line_Util_Tbl ( p_funds_util_flt         => l_funds_util_flt
10007                              , p_source_object_class    => l_source_object_class
10008                              , p_source_object_id       => l_source_object_id
10009                              , p_request_header_id      => l_request_header_id
10010                              , p_batch_line_id          => l_batch_line_id
10011                              , p_batch_type             => l_batch_type
10012                              , p_summary_view           => p_summary_view
10013                              , p_cre_util_amount        => l_cre_util_amount
10014                              , p_prorate_earnings_flag  => l_prorate_earnings_flag
10015                              , p_currency_rec           => l_currency_rec
10016 			     , p_offer_quantity         => l_offer_quantity
10017                              , x_funds_rem_tbl          => l_funds_rem_tbl
10018                              , x_tot_accrual_amt        => l_tot_accrual_amt
10019                              , x_line_amount            => l_new_line_amount
10020                              , x_line_util_tbl          => l_line_util_tbl
10021 			     , x_util_quantity          => l_util_quantity --EA ninarasi fix for 14016226
10022 			     , x_new_total_units        => l_new_total_units --EA ninarasi fix for 14016226
10023                              , x_msg_data               => x_msg_data
10024                              , x_msg_count              => x_msg_count
10025                              , x_return_status          => l_return_status
10026                              );
10027 
10028 
10029       IF l_return_status = FND_API.g_ret_sts_error THEN
10030         RAISE FND_API.g_exc_error;
10031       ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
10032         RAISE FND_API.g_exc_error;
10033       END IF;
10034 
10035       --For Supplier Ship Debit claims, claim line amount will always be total association
10036       --amount and never fall into pay over earnings category. Hence, skip pay over earnings
10037       --these type of claims.
10038 
10039       IF NVL(l_source_object_class,'X') = 'SD_SUPPLIER' THEN
10040         GOTO Create_Line_Util;
10041       END IF;
10042 
10043       l_claim_amt := l_asso_amount - NVL(l_exists_asso_amount,0);
10044 
10045       /* Validate Over Utilization:
10046       Pay over earnings will take place only for Normal claims created from Trade Management
10047       UI. Since, utilizations amount remaining in BUDGET currency, while association amount
10048       is in CLAIM currency. Hence, need to validate over utilizations w.r.t. association amount
10049       in CLAIM currency.
10050       */
10051 
10052       IF (( NVL(l_created_from, 'NONE') <> 'AUTOPAY')
10053          AND (NVL(l_batch_type, 'NONE') <> 'BATCH'
10054          AND  NVL(l_source_object_class, 'NONE') NOT IN ('BATCH','SPECIAL_PRICE') --//Bug fix : 9751679
10055          AND  l_funds_util_flt.offer_type IS NOT NULL))
10056       THEN
10057           Get_Pay_Over_Amount  ( p_util             => l_funds_rem_tbl
10058                                , p_claim_amt        => l_cre_util_amount
10059                                , p_claim_exc_rate   => l_claim_exc_rate
10060                                , p_claim_exc_date   => l_claim_exc_date
10061                                , p_claim_exc_type   => l_claim_exc_type
10062                                , p_currency_rec     => l_currency_rec
10063                                , x_pay_over_flag    => l_pay_over_flag
10064                                , x_pay_over_amount  => l_pay_over_amount
10065                                , x_return_status    => l_return_status
10066                                , x_msg_data         => x_msg_data
10067                                , x_msg_count        => x_msg_count
10068                                );
10069       END IF;
10070 
10071       IF OZF_DEBUG_HIGH_ON THEN
10072         IF l_pay_over_flag THEN
10073            OZF_Utility_PVT.debug_message('l_pay_over_flag = TRUE');
10074         ELSE
10075            OZF_Utility_PVT.debug_message('l_pay_over_flag = FALSE');
10076         END IF;
10077         OZF_Utility_PVT.debug_message('l_claim_amt = '||l_claim_amt);
10078         OZF_Utility_PVT.debug_message('l_cre_util_amount = '||l_cre_util_amount);
10079         OZF_Utility_PVT.debug_message('l_pay_over_amount = '||l_pay_over_amount);
10080         OZF_Utility_PVT.debug_message('l_tot_accrual_amt = '|| NVL(l_tot_accrual_amt,0));
10081         OZF_Utility_PVT.debug_message('l_new_line_amount = '|| NVL(l_new_line_amount,0));
10082         OZF_Utility_PVT.debug_message('l_funds_rem_tbl.count = '|| l_funds_rem_tbl.count);
10083       END IF;
10084 
10085      IF (l_pay_over_flag OR (p_summary_view <> 'ACTIVITY' AND (l_cre_util_amount > 0 and l_cre_util_amount > l_tot_accrual_amt)) OR (p_summary_view <> 'ACTIVITY' AND NVL(l_tot_accrual_amt,0) = 0)) THEN --ninarasi fix for bug 14611756
10086         Validate_Over_Utilization(
10087            p_api_version            => l_api_version
10088           ,p_init_msg_list          => FND_API.g_false
10089           ,p_validation_level       => p_validation_level
10090           ,x_return_status          => l_return_status
10091           ,x_msg_count              => x_msg_count
10092           ,x_msg_data               => x_msg_data
10093           ,p_currency_rec           => l_currency_rec
10094           ,p_funds_util_flt         => l_funds_util_flt
10095           ,p_over_paid_amount       => l_pay_over_amount --nepanda : fix for bug # 9508390  - issue # 3
10096         );
10097         IF l_return_status = fnd_api.g_ret_sts_error THEN
10098            RAISE FND_API.g_exc_error;
10099         ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10100            RAISE FND_API.g_exc_unexpected_error;
10101         END IF;
10102 
10103       END IF;
10104 
10105       --If pay over earnings is allowed, then create adjustments for over paid earnings w.r.t.
10106       --association amount.
10107       -- Changed the code to create the adjustment for multiple and single product lines -- kpatro
10108         IF (NVL(l_new_line_amount,0) <> 0 AND l_pay_over_amount > 0) THEN
10109         Create_Pay_Over_Adjustments ( px_line_util_tbl        => l_line_util_tbl
10110                                     , p_funds_util_flt        => l_funds_util_flt
10111                                     , p_tot_accrual_amt       => NVL(l_tot_accrual_amt,0)
10112                                     , p_pay_over_amount       => l_pay_over_amount
10113                                     , p_prorate_earnings_flag => l_prorate_earnings_flag
10114                                     , p_new_total_amount      => l_new_line_amount
10115                                     , p_currency_rec          => l_currency_rec
10116 				    , p_util_quantity         => l_util_quantity --EA ninarasi fix for 14016226
10117 				    , p_new_total_units       => l_new_total_units --EA
10118 				    , p_offer_quantity        => l_offer_quantity --EA
10119                                     , x_return_status         => l_return_status
10120                                     , x_msg_data              => x_msg_data
10121                                     , x_msg_count             => x_msg_count
10122 
10123                                     );
10124         IF l_return_status = FND_API.g_ret_sts_error THEN
10125           RAISE FND_API.g_exc_error;
10126         ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
10127           RAISE FND_API.g_exc_error;
10128         END IF;
10129 	ELSE
10130 	 --for scandata offers replace the util quantity and units with the original quantity
10131 	 --ninarasi fix for bug 14089950
10132 	 IF l_funds_util_flt.offer_type = 'SCAN_DATA' THEN
10133 	   FOR i in l_line_util_tbl.FIRST..l_line_util_tbl.LAST LOOP
10134 	     IF l_line_util_tbl.exists(i) THEN
10135 	       l_line_util_tbl(i).scan_unit := l_funds_util_flt.total_units;
10136                l_line_util_tbl(i).quantity := l_funds_util_flt.quantity;
10137 	     END IF;
10138 	   END LOOP;
10139 	 END IF;
10140 
10141         END IF;
10142   END IF;
10143 
10144      <<create_line_util>>
10145 
10146   --Do associate earnings for qualified utilizations with current claim line. Calculate
10147   --FXGL. Reduce amount remaining of utilizations w.r.t. corresponding association amounts.
10148 
10149   IF l_line_util_tbl.COUNT > 0 THEN
10150     Create_Line_Util_Tbl ( p_api_version            => l_api_version
10151                          , p_init_msg_list          => FND_API.g_false
10152                          , p_commit                 => FND_API.g_false
10153                          , p_validation_level       => p_validation_level
10154                          , x_return_status          => l_return_status
10155                          , x_msg_data               => x_msg_data
10156                          , x_msg_count              => x_msg_count
10157                          , p_currency_rec           => l_currency_rec
10158                          , p_line_util_tbl          => l_line_util_tbl
10159                          , x_error_index            => l_error_index
10160                          );
10161 
10162     IF l_return_status =  fnd_api.g_ret_sts_error THEN
10163       RAISE FND_API.g_exc_error;
10164     ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10165       RAISE FND_API.g_exc_unexpected_error;
10166     END IF;
10167   END IF;
10168 
10169   ------------------------- finish -------------------------------
10170   -- Check for commit
10171   IF FND_API.to_boolean(p_commit) THEN
10172     COMMIT;
10173   END IF;
10174 
10175   FND_MSG_PUB.count_and_get(
10176          p_encoded => FND_API.g_false,
10177          p_count   => x_msg_count,
10178          p_data    => x_msg_data
10179   );
10180 
10181   IF OZF_DEBUG_HIGH_ON THEN
10182      OZF_Utility_PVT.debug_message(l_full_name ||': end');
10183   END IF;
10184 
10185 EXCEPTION
10186   WHEN FND_API.g_exc_error THEN
10187     ROLLBACK TO Update_Group_Line_Util;
10188     x_return_status := FND_API.g_ret_sts_error;
10189     FND_MSG_PUB.count_and_get (
10190            p_encoded => FND_API.g_false
10191           ,p_count   => x_msg_count
10192           ,p_data    => x_msg_data
10193     );
10194 
10195   WHEN FND_API.g_exc_unexpected_error THEN
10196     ROLLBACK TO Update_Group_Line_Util;
10197     x_return_status := FND_API.g_ret_sts_unexp_error ;
10198     FND_MSG_PUB.count_and_get (
10199            p_encoded => FND_API.g_false
10200           ,p_count   => x_msg_count
10201           ,p_data    => x_msg_data
10202     );
10203 
10204   WHEN OTHERS THEN
10205     ROLLBACK TO Update_Group_Line_Util;
10206     x_return_status := FND_API.g_ret_sts_unexp_error ;
10207     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
10208       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
10209     END IF;
10210     FND_MSG_PUB.count_and_get(
10211            p_encoded => FND_API.g_false
10212           ,p_count   => x_msg_count
10213           ,p_data    => x_msg_data
10214     );
10215 
10216 END Update_Group_Line_Util;
10217 
10218 ---------------------------------------------------------------
10219 -- PROCEDURE
10220 --    Del_Line_Util_By_Group
10221 --
10222 -- NOTE
10223 --    p_line_util_rec contains claim_line_util_id
10224 --                           , utilization_id
10225 --                           , claim_line_id
10226 --                           , acctd_amount
10227 --
10228 -- HISTORY
10229 --    10/30/2002  mchang  Create.
10230 --    08-Aug-06   azahmed  Modified for FXGL ER
10231 --                         amount passed to Update_funds_util
10232 ---------------------------------------------------------------
10233 PROCEDURE Del_Line_Util_By_Group(
10234    x_return_status     OUT NOCOPY VARCHAR2
10235   ,x_msg_count         OUT NOCOPY NUMBER
10236   ,x_msg_data          OUT NOCOPY VARCHAR2
10237 
10238   ,p_line_util_rec     IN  line_util_rec_type
10239   ,px_currency_rec      IN OUT NOCOPY currency_rec_type
10240   ,px_upd_line_util_tbl   IN OUT NOCOPY upd_fund_util_tbl_type --ninarasi fix for bug 13530939
10241 )
10242 IS
10243 l_api_name        CONSTANT VARCHAR2(30) := 'Del_Line_Util_By_Group';
10244 l_full_name       CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
10245 l_return_status            VARCHAR2(1);
10246 
10247 l_line_util_rec          line_util_rec_type := p_line_util_rec;
10248 l_amount          NUMBER;
10249 l_currency_rec   currency_rec_type := px_currency_rec;
10250 
10251 BEGIN
10252   --------------------- initialize -----------------------
10253   SAVEPOINT Del_Line_Util_By_Group;
10254 
10255   x_return_status := FND_API.G_RET_STS_SUCCESS;
10256 
10257  IF p_line_util_rec.utilization_id > 0 THEN
10258           ------------------ Update Uitlization ------------------
10259 
10260 
10261   --Set UNIVERSAL currency from profile.
10262   l_currency_rec.universal_currency_code := FND_PROFILE.VALUE('OZF_TP_COMMON_CURRENCY');
10263 
10264    IF l_currency_rec.claim_currency_code = l_currency_rec.transaction_currency_code THEN
10265       l_currency_rec.association_currency_code := l_currency_rec.transaction_currency_code;
10266      --l_amount := p_line_util_rec.amount; --kdass
10267    ELSE
10268      l_currency_rec.association_currency_code := l_currency_rec.functional_currency_code;
10269      --l_amount := p_line_util_rec.acctd_amount; --kdass
10270    END IF;
10271    l_amount := p_line_util_rec.amount; --kdass
10272      Update_Fund_Utils(
10273                 p_line_util_rec  => l_line_util_rec
10274               , p_asso_amount    => NVL(l_amount,0)
10275               , p_mode           => 'NONE'
10276               , px_currency_rec  => l_currency_rec
10277               , x_return_status  => l_return_status
10278               , x_msg_count      => x_msg_count
10279               , x_msg_data       => x_msg_data
10280               , px_upd_line_util_tbl =>  px_upd_line_util_tbl --ninarasi fix for bug 13530939
10281               );
10282           IF l_return_status =  fnd_api.g_ret_sts_error THEN
10283             RAISE FND_API.g_exc_error;
10284           ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10285             RAISE FND_API.g_exc_unexpected_error;
10286       END IF;
10287 
10288  END IF; -- p_line_util_rec.utilization_id > 0
10289 
10290   ------------------------ delete ------------------------
10291   DELETE FROM ozf_claim_lines_util_all
10292     WHERE claim_line_util_id = p_line_util_rec.claim_line_util_id;
10293 
10294   IF (SQL%NOTFOUND) THEN
10295     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_error) THEN
10296       FND_MESSAGE.set_name('OZF', 'OZF_API_RECORD_NOT_FOUND');
10297       FND_MSG_PUB.add;
10298     END IF;
10299     RAISE FND_API.g_exc_error;
10300   END IF;
10301 
10302   -------------------- finish --------------------------
10303   FND_MSG_PUB.count_and_get(
10304         p_encoded => FND_API.g_false,
10305         p_count   => x_msg_count,
10306         p_data    => x_msg_data
10307   );
10308 
10309 EXCEPTION
10310   WHEN FND_API.g_exc_error THEN
10311     ROLLBACK TO Del_Line_Util_By_Group;
10312     x_return_status := FND_API.g_ret_sts_error;
10313     FND_MSG_PUB.count_and_get(
10314             p_encoded => FND_API.g_false,
10315             p_count   => x_msg_count,
10316             p_data    => x_msg_data
10317     );
10318 
10319   WHEN FND_API.g_exc_unexpected_error THEN
10320     ROLLBACK TO Del_Line_Util_By_Group;
10321     x_return_status := FND_API.g_ret_sts_unexp_error ;
10322     FND_MSG_PUB.count_and_get(
10323             p_encoded => FND_API.g_false,
10324             p_count   => x_msg_count,
10325             p_data    => x_msg_data
10326     );
10327 
10328   WHEN OTHERS THEN
10329     ROLLBACK TO Del_Line_Util_By_Group;
10330     x_return_status := FND_API.g_ret_sts_unexp_error ;
10331     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
10332       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
10333     END IF;
10334     FND_MSG_PUB.count_and_get(
10335             p_encoded => FND_API.g_false,
10336             p_count   => x_msg_count,
10337             p_data    => x_msg_data
10338     );
10339 
10340 END Del_Line_Util_By_Group;
10341 
10342 ---------------------------------------------------------------------
10343 -- PROCEDURE
10344 --    Delete_Group_Line_Util
10345 --
10346 -- HISTORY
10347 --    10/05/2001  mchang  Create.
10348 --    15-Mar-06   azahmed  Bugfix 5101106
10349 --    08-Aug-06  azahmed  Modified for FXGL ER
10350 ---------------------------------------------------------------------
10351 PROCEDURE Delete_Group_Line_Util(
10352    p_api_version         IN  NUMBER
10353   ,p_init_msg_list       IN  VARCHAR2  := FND_API.g_false
10354   ,p_commit              IN  VARCHAR2  := FND_API.g_false
10355   ,p_validation_level    IN  NUMBER    := FND_API.g_valid_level_full
10356 
10357   ,x_return_status       OUT NOCOPY VARCHAR2
10358   ,x_msg_count           OUT NOCOPY NUMBER
10359   ,x_msg_data            OUT NOCOPY VARCHAR2
10360 
10361   ,p_funds_util_flt      IN  funds_util_flt_type
10362   ,p_mode                IN  VARCHAR2  := OZF_CLAIM_UTILITY_PVT.g_auto_mode
10363 )
10364 IS
10365 l_api_version   CONSTANT NUMBER       := 1.0;
10366 l_api_name      CONSTANT VARCHAR2(30) := 'Delete_Group_Line_Util';
10367 l_full_name     CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
10368 l_return_status          VARCHAR2(1);
10369 
10370 l_funds_util_flt funds_util_flt_type := p_funds_util_flt;
10371 
10372 TYPE FundsUtilCsrTyp IS REF CURSOR;
10373 l_funds_util_csr         NUMBER; --FundsUtilCsrTyp;
10374 l_funds_util_sql         VARCHAR2(3000);
10375 l_line_util_tbl          line_util_tbl_type;
10376 l_upd_line_util_rec      line_util_rec_type;
10377 l_lu_line_util_id        NUMBER;
10378 l_lu_utilization_id      NUMBER;
10379 l_lu_amt                 NUMBER;
10380 l_lu_scan_unit           NUMBER;
10381 l_del_total_amount       NUMBER;
10382 l_del_total_units        NUMBER;
10383 l_counter                PLS_INTEGER := 1;
10384 i                        PLS_INTEGER;
10385 l_funds_util_end         VARCHAR2(1) := 'N';
10386 l_final_lu_amt     NUMBER;
10387 l_object_version_number  NUMBER;
10388 l_ignore                 NUMBER;
10389 l_offer_uom_code         VARCHAR2(3);
10390 l_offer_quantity         NUMBER;
10391 l_scan_value             NUMBER;
10392 l_product_id             NUMBER;
10393 l_lu_acctd_amt           NUMBER;
10394 l_utiz_currency          VARCHAR2(15);
10395 l_utiz_amount            NUMBER;
10396 l_lu_currency_code       VARCHAR2(15);
10397 
10398 l_currency_rec           currency_rec_type;
10399 l_upd_fund_util_tbl      upd_fund_util_tbl_type; --ninarasi fix for bug 13530939
10400 
10401 CURSOR csr_final_lu_amt(cv_claim_line_id IN NUMBER) IS
10402   SELECT SUM(amount)
10403   FROM ozf_claim_lines_util
10404   WHERE claim_line_id = cv_claim_line_id;
10405 
10406 -- fix for bug 5042046
10407 CURSOR csr_function_currency IS
10408   SELECT gs.currency_code
10409   FROM   gl_sets_of_books gs
10410   ,      ozf_sys_parameters org
10411   WHERE  org.set_of_books_id = gs.set_of_books_id
10412   AND   org.org_id = MO_GLOBAL.GET_CURRENT_ORG_ID();
10413 
10414 CURSOR csr_claim_currency(cv_claim_line_id IN NUMBER) IS
10415 SELECT currency_code from ozf_claim_lines
10416 where claim_line_id = cv_claim_line_id;
10417 
10418 CURSOR csr_offer_currency(cv_plan_id IN NUMBER) IS
10419   SELECT transaction_currency_code
10420   FROM ozf_offers
10421   WHERE qp_list_header_id = cv_plan_id;
10422 
10423 CURSOR csr_over_util(cv_claim_line_id IN NUMBER, cv_act_product_id IN NUMBER) IS
10424   SELECT claim_line_util_id, acctd_amount, scan_unit
10425   FROM ozf_claim_lines_util
10426   WHERE claim_line_id = cv_claim_line_id
10427   AND activity_product_id = cv_act_product_id
10428   AND utilization_id = -1;
10429 
10430 CURSOR csr_acc_over_util(cv_claim_line_id IN NUMBER, cv_offer_id IN NUMBER) IS
10431   SELECT claim_line_util_id, acctd_amount
10432   FROM   ozf_claim_lines_util util
10433   WHERE  claim_line_id = cv_claim_line_id
10434   AND    activity_product_id = cv_offer_id
10435   AND    utilization_id = -2;
10436 
10437 -- Bugfix 5101106: Recalculate qty
10438 CURSOR csr_offer_profile(cv_activity_product_id IN NUMBER) IS
10439   SELECT uom_code
10440   ,      quantity
10441   ,      scan_value
10442   ,      inventory_item_id
10443   FROM ams_act_products
10444   WHERE activity_product_id = cv_activity_product_id;
10445 
10446 --Changed cursor csr_utiz_amount currency for Multicurrency ER.
10447 /*
10448 CURSOR csr_utiz_amount(cv_line_util_id IN NUMBER) IS
10449 SELECT util_curr_amount
10450 FROM ozf_claim_lines_util
10451 WHERE claim_line_util_id = cv_line_util_id;
10452 */
10453 
10454 CURSOR csr_utiz_amount(cv_line_util_id IN NUMBER) IS
10455 SELECT DECODE(ln.currency_code,fu.plan_currency_code,ln.plan_curr_amount,ln.acctd_amount)
10456 FROM ozf_claim_lines_util ln
10457    , ozf_funds_utilized_all_b fu
10458 WHERE claim_line_util_id = cv_line_util_id
10459   AND fu.utilization_id = ln.utilization_id;
10460 
10461 BEGIN
10462   --------------------- initialize -----------------------
10463   SAVEPOINT Delete_Group_Line_Util;
10464 
10465   IF OZF_DEBUG_HIGH_ON THEN
10466      OZF_Utility_PVT.debug_message(l_full_name||': start');
10467   END IF;
10468 
10469   IF FND_API.to_boolean(p_init_msg_list) THEN
10470     FND_MSG_PUB.initialize;
10471   END IF;
10472 
10473   IF NOT FND_API.compatible_api_call(
10474          l_api_version,
10475          p_api_version,
10476          l_api_name,
10477          g_pkg_name
10478   ) THEN
10479     RAISE FND_API.g_exc_unexpected_error;
10480   END IF;
10481 
10482   x_return_status := FND_API.g_ret_sts_success;
10483 
10484   OPEN  csr_function_currency;
10485   FETCH csr_function_currency INTO l_currency_rec.functional_currency_code;
10486   CLOSE csr_function_currency;
10487 
10488 
10489   Init_Line_Util_Rec(
10490      x_line_util_rec   => l_upd_line_util_rec
10491   );
10492 
10493   ----------------- copy line info to filter ---------------
10494 
10495   IF p_funds_util_flt.claim_line_id IS NOT NULL THEN
10496     Copy_Util_Flt(px_funds_util_flt => l_funds_util_flt);
10497   END IF;
10498 
10499 -- derive claim currency
10500   OPEN csr_claim_currency(l_funds_util_flt.claim_line_id);
10501   FETCH csr_claim_currency INTO l_currency_rec.claim_currency_code;
10502   CLOSE csr_claim_currency;
10503 
10504    --kdass
10505   l_currency_rec.transaction_currency_code := l_funds_util_flt.utiz_currency_code;
10506 
10507   IF l_currency_rec.claim_currency_code = l_currency_rec.transaction_currency_code THEN
10508      l_currency_rec.association_currency_code  := l_currency_rec.transaction_currency_code;
10509   ELSE
10510       l_currency_rec.association_currency_code  := l_currency_rec.functional_currency_code;
10511   END IF;
10512 
10513  IF OZF_DEBUG_HIGH_ON THEN
10514    ozf_utility_pvt.debug_message('l_currency_rec.claim_currency_code       :' || l_currency_rec.claim_currency_code);
10515    ozf_utility_pvt.debug_message('l_currency_rec.transaction_currency_code :' || l_currency_rec.transaction_currency_code);
10516    ozf_utility_pvt.debug_message('l_currency_rec.association_currency_code :' || l_currency_rec.association_currency_code);
10517    ozf_utility_pvt.debug_message('l_currency_rec.functional_currency_code  :' || l_currency_rec.functional_currency_code);
10518  END IF;
10519   -- Modified for FXGL ER
10520   -- deletion also to take place in amount and not acctd_amount
10521 
10522   --------------------- start -----------------------
10523   l_del_total_amount := NVL(l_funds_util_flt.old_total_amount, 0) - NVL(l_funds_util_flt.total_amount ,0);
10524   l_del_total_amount := OZF_UTILITY_PVT.CurrRound(l_del_total_amount, l_currency_rec.claim_currency_code);
10525   l_del_total_units := NVL(l_funds_util_flt.old_total_units, 0) - NVL(l_funds_util_flt.total_units, 0);
10526 
10527   ------------ reduce the over-utilization first ---------
10528   IF p_funds_util_flt.offer_type = 'SCAN_DATA' THEN
10529     OPEN csr_over_util(l_funds_util_flt.claim_line_id, l_funds_util_flt.activity_product_id);
10530     LOOP
10531       FETCH csr_over_util INTO l_lu_line_util_id, l_lu_amt, l_lu_scan_unit;
10532       EXIT WHEN csr_over_util%NOTFOUND;
10533 
10534       IF l_funds_util_flt.total_amount IS NULL THEN
10535         l_line_util_tbl(l_counter).claim_line_util_id := l_lu_line_util_id;
10536         l_line_util_tbl(l_counter).utilization_id := -1;
10537         l_line_util_tbl(l_counter).amount := l_lu_amt;
10538         l_line_util_tbl(l_counter).scan_unit := l_lu_scan_unit;
10539         l_line_util_tbl(l_counter).claim_line_id := l_funds_util_flt.claim_line_id;
10540       ELSIF l_del_total_amount >= l_lu_acctd_amt THEN
10541         l_line_util_tbl(l_counter).claim_line_util_id := l_lu_line_util_id;
10542         l_line_util_tbl(l_counter).utilization_id := -1;
10543         l_line_util_tbl(l_counter).amount := l_lu_amt;
10544         l_line_util_tbl(l_counter).scan_unit := l_lu_scan_unit;
10545         l_line_util_tbl(l_counter).claim_line_id := l_funds_util_flt.claim_line_id;
10546 
10547         l_del_total_amount := l_del_total_amount - l_lu_amt;
10548         l_del_total_units := l_del_total_units - l_lu_scan_unit;
10549       ELSE
10550         l_upd_line_util_rec.claim_line_util_id := l_lu_line_util_id;
10551         l_upd_line_util_rec.amount := l_lu_amt - l_del_total_amount;
10552         l_upd_line_util_rec.scan_unit := l_lu_scan_unit - l_del_total_units;
10553 
10554         l_del_total_amount := 0;
10555         l_del_total_units := 0;
10556       END IF;
10557 
10558       l_counter := l_counter + 1;
10559 
10560       EXIT WHEN l_del_total_amount = 0;
10561     END LOOP;
10562     CLOSE csr_over_util;
10563   ELSIF p_funds_util_flt.activity_type = 'OFFR' AND
10564         p_funds_util_flt.activity_id IS NOT NULL
10565   THEN
10566         NULL;
10567 /* -- We do not create -2 utilizations for non scan offers currently
10568     OPEN csr_acc_over_util(l_funds_util_flt.claim_line_id, l_funds_util_flt.activity_id);
10569     LOOP
10570       FETCH csr_acc_over_util INTO l_lu_line_util_id, l_lu_acctd_amt;
10571       EXIT WHEN csr_acc_over_util%NOTFOUND;
10572 
10573       IF p_funds_util_flt.total_amount IS NULL THEN
10574         l_line_util_tbl(l_counter).claim_line_util_id := l_lu_line_util_id;
10575         l_line_util_tbl(l_counter).utilization_id := l_lu_line_util_id;
10576         l_line_util_tbl(l_counter).acctd_amount := l_lu_acctd_amt;
10577         l_line_util_tbl(l_counter).claim_line_id := l_funds_util_flt.claim_line_id;
10578       ELSIF l_del_total_amount >= l_lu_acctd_amt THEN
10579         l_line_util_tbl(l_counter).claim_line_util_id := l_lu_line_util_id;
10580         l_line_util_tbl(l_counter).utilization_id := l_lu_line_util_id;
10581         l_line_util_tbl(l_counter).acctd_amount := l_lu_acctd_amt;
10582         l_line_util_tbl(l_counter).claim_line_id := l_funds_util_flt.claim_line_id;
10583 
10584         l_del_total_amount := l_del_total_amount - l_lu_acctd_amt;
10585       ELSE
10586         l_upd_line_util_rec.claim_line_util_id := l_lu_line_util_id;
10587         l_upd_line_util_rec.acctd_amount := l_lu_acctd_amt - l_del_total_amount;
10588 
10589         l_del_total_amount := 0;
10590       END IF;
10591 
10592       l_counter := l_counter + 1;
10593 
10594       EXIT WHEN l_del_total_amount = 0;
10595     END LOOP;
10596     CLOSE csr_acc_over_util;
10597     */
10598   END IF;
10599 
10600 
10601  IF l_del_total_amount <> 0 THEN  -- added for bugfix 4448859
10602      Get_Utiz_Sql_Stmt(
10603        p_api_version         => l_api_version
10604       ,p_init_msg_list       => FND_API.g_false
10605       ,p_commit              => FND_API.g_false
10606       ,p_validation_level    => FND_API.g_valid_level_full
10607       ,x_return_status       => l_return_status
10608       ,x_msg_count           => x_msg_count
10609       ,x_msg_data            => x_msg_data
10610       ,p_summary_view        => 'DEL_GRP_LINE_UTIL'
10611       ,p_funds_util_flt      => l_funds_util_flt
10612       ,px_currency_rec        => l_currency_rec
10613       ,p_cust_account_id     => l_funds_util_flt.cust_account_id
10614       ,x_utiz_sql_stmt       => l_funds_util_sql
10615     );
10616     IF l_return_status = FND_API.g_ret_sts_error THEN
10617        RAISE FND_API.g_exc_error;
10618     ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
10619        RAISE FND_API.g_exc_unexpected_error;
10620     END IF;
10621 
10622     -- use FND_DSQL package for dynamic sql and bind variables
10623     l_funds_util_csr := DBMS_SQL.open_cursor;
10624     FND_DSQL.set_cursor(l_funds_util_csr);
10625     DBMS_SQL.parse(l_funds_util_csr, l_funds_util_sql, DBMS_SQL.native);
10626     DBMS_SQL.define_column(l_funds_util_csr, 1, l_lu_line_util_id);
10627     DBMS_SQL.define_column(l_funds_util_csr, 2, l_lu_utilization_id);
10628     DBMS_SQL.define_column(l_funds_util_csr, 3, l_lu_amt);
10629     DBMS_SQL.define_column(l_funds_util_csr, 4, l_lu_scan_unit);
10630     DBMS_SQL.define_column(l_funds_util_csr, 5, l_lu_currency_code, 15);
10631 --    DBMS_SQL.define_column(l_funds_util_csr, 5, l_utiz_amount);
10632     FND_DSQL.do_binds;
10633 
10634     l_ignore := DBMS_SQL.execute(l_funds_util_csr);
10635     --OPEN l_funds_util_csr FOR l_funds_util_sql;
10636     LOOP
10637       /*
10638       FETCH l_funds_util_csr INTO l_lu_line_util_id
10639                               , l_lu_utilization_id
10640                               , l_lu_acctd_amt
10641                               , l_lu_scan_unit;
10642     EXIT WHEN l_funds_util_csr%NOTFOUND;
10643     */
10644 
10645       IF DBMS_SQL.fetch_rows(l_funds_util_csr) > 0 THEN
10646         DBMS_SQL.column_value(l_funds_util_csr, 1, l_lu_line_util_id);
10647         DBMS_SQL.column_value(l_funds_util_csr, 2, l_lu_utilization_id);
10648         DBMS_SQL.column_value(l_funds_util_csr, 3, l_lu_amt);
10649         DBMS_SQL.column_value(l_funds_util_csr, 4, l_lu_scan_unit);
10650         DBMS_SQL.column_value(l_funds_util_csr, 5, l_lu_currency_code);
10651 --        DBMS_SQL.define_column(l_funds_util_csr, 5, l_utiz_amount);
10652 
10653         --If CLAIM and TRANSACTIONAL currencies are same, then l_utiz_amount will be
10654         --in TRANSACTIONAL currency otherwise it will be in FUNCTIONAL currency.
10655         --Changed for Claims Multicurrency ER.
10656         OPEN csr_utiz_amount(l_lu_line_util_id);
10657         FETCH csr_utiz_amount INTO l_utiz_amount;
10658         CLOSE csr_utiz_amount;
10659 
10660         IF OZF_DEBUG_LOW_ON THEN
10661         OZF_Utility_PVT.debug_message('l_lu_line_util_id. : '||l_lu_line_util_id);
10662         OZF_Utility_PVT.debug_message('l_lu_utilization_id. : '||l_lu_utilization_id);
10663         OZF_Utility_PVT.debug_message('l_lu_amt. : '||l_lu_amt);
10664         OZF_Utility_PVT.debug_message('l_utiz_amount. : '||l_utiz_amount);
10665         OZF_Utility_PVT.debug_message('l_del_total_amount. : '||l_del_total_amount);
10666         END IF;
10667 
10668         IF p_funds_util_flt.total_amount IS NULL THEN
10669            l_line_util_tbl(l_counter).claim_line_util_id := l_lu_line_util_id;
10670            l_line_util_tbl(l_counter).utilization_id := l_lu_utilization_id;
10671            l_line_util_tbl(l_counter).amount := l_utiz_amount;
10672            IF p_funds_util_flt.offer_type = 'SCAN_DATA' THEN
10673                l_line_util_tbl(l_counter).scan_unit := l_lu_scan_unit;
10674                l_line_util_tbl(l_counter).uom_code := l_funds_util_flt.uom_code;
10675                l_line_util_tbl(l_counter).quantity := l_funds_util_flt.quantity;
10676            END IF;
10677         l_line_util_tbl(l_counter).claim_line_id := l_funds_util_flt.claim_line_id;
10678         l_counter := l_counter + 1;
10679         ELSE
10680            IF ABS(l_del_total_amount) >= ABS(l_lu_amt) THEN
10681               l_line_util_tbl(l_counter).claim_line_util_id := l_lu_line_util_id;
10682               l_line_util_tbl(l_counter).utilization_id := l_lu_utilization_id;
10683               l_line_util_tbl(l_counter).amount := l_utiz_amount;
10684               IF p_funds_util_flt.offer_type = 'SCAN_DATA' THEN
10685                   l_line_util_tbl(l_counter).scan_unit := l_lu_scan_unit;
10686                   l_line_util_tbl(l_counter).uom_code := l_funds_util_flt.uom_code;
10687                   l_line_util_tbl(l_counter).quantity := l_funds_util_flt.quantity;
10688                   l_del_total_units := l_del_total_units - l_lu_scan_unit;
10689               END IF;
10690               l_line_util_tbl(l_counter).claim_line_id := l_funds_util_flt.claim_line_id;
10691 
10692               l_del_total_amount := l_del_total_amount - l_lu_amt;
10693 
10694            ELSE
10695               l_upd_line_util_rec.claim_line_util_id := l_lu_line_util_id;
10696               l_upd_line_util_rec.amount := l_lu_amt - l_del_total_amount;
10697               IF p_funds_util_flt.offer_type = 'SCAN_DATA' THEN
10698                    l_upd_line_util_rec.scan_unit := l_lu_scan_unit - l_del_total_units;
10699                    l_upd_line_util_rec.uom_code := l_funds_util_flt.uom_code;
10700                    l_upd_line_util_rec.quantity := l_funds_util_flt.quantity;
10701               END IF;
10702 
10703               l_funds_util_end := 'Y';
10704           END IF;
10705 
10706           l_counter := l_counter + 1;
10707 
10708           EXIT WHEN l_del_total_amount = 0;
10709           EXIT WHEN l_funds_util_end = 'Y';
10710       END IF;
10711     ELSE
10712      EXIT;
10713      END IF;
10714     END LOOP;
10715   --CLOSE l_funds_util_csr;
10716   DBMS_SQL.close_cursor(l_funds_util_csr);
10717   END IF; -- l_del_total_amount <> 0
10718 
10719   --------------------- 1. delete -----------------------
10720   i := l_line_util_tbl.FIRST;
10721   IF i IS NOT NULL THEN
10722      IF p_funds_util_flt.activity_type = 'OFFR' AND
10723         p_funds_util_flt.activity_id IS NOT NULL THEN
10724         OPEN csr_offer_currency(p_funds_util_flt.activity_id);
10725         FETCH csr_offer_currency INTO l_currency_rec.offer_currency_code; -- BKUNJAN Need to check where it is used ?
10726         CLOSE csr_offer_currency;
10727      END IF;
10728 
10729      LOOP
10730         IF l_line_util_tbl(i).claim_line_util_id IS NOT NULL THEN
10731         ozf_utility_pvt.debug_message('kd: calling Del_Line_Util_By_Group');
10732            Del_Line_Util_By_Group(
10733                  x_return_status     => l_return_status
10734                 ,x_msg_count         => x_msg_count
10735                 ,x_msg_data          => x_msg_data
10736                 ,p_line_util_rec     => l_line_util_tbl(i)
10737                 ,px_currency_rec      => l_currency_rec
10738                 ,px_upd_line_util_tbl  => l_upd_fund_util_tbl
10739            );
10740 
10741            IF l_upd_fund_util_tbl.COUNT = G_BULK_LIMIT OR i = l_line_util_tbl.LAST THEN --ninarasi fix for bug 13530939
10742                    FORALL i IN NVL(l_upd_fund_util_tbl.FIRST,1) .. NVL(l_upd_fund_util_tbl.LAST,0)
10743                       UPDATE ozf_funds_utilized_all_b
10744                       SET    amount_remaining                  = amount_remaining - l_upd_fund_util_tbl(i).amount_remaining
10745                            , acctd_amount_remaining          = acctd_amount_remaining - l_upd_fund_util_tbl(i).acctd_amount_remaining
10746                            , plan_curr_amount_remaining      = plan_curr_amount_remaining - l_upd_fund_util_tbl(i).plan_curr_amount_remaining
10747                            , univ_curr_amount_remaining      = univ_curr_amount_remaining - l_upd_fund_util_tbl(i).univ_curr_amount_remaining
10748                            , fund_request_amount_remaining   = fund_request_amount_remaining - l_upd_fund_util_tbl(i).fund_request_amount_remaining
10749                      WHERE utilization_id = l_upd_fund_util_tbl(i).utilization_id;
10750 
10751                 l_upd_fund_util_tbl.delete;
10752            END IF;
10753 
10754            IF l_return_status =  fnd_api.g_ret_sts_error THEN
10755              RAISE FND_API.g_exc_error;
10756            ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10757              RAISE FND_API.g_exc_unexpected_error;
10758            END IF;
10759         END IF;
10760      EXIT WHEN i = l_line_util_tbl.LAST;
10761      i := l_line_util_tbl.NEXT(i);
10762      END LOOP;
10763   END IF;
10764 
10765   --------------------- 2. update -----------------------
10766   IF (l_upd_line_util_rec.claim_line_util_id is not null
10767   AND l_upd_line_util_rec.claim_line_util_id <> FND_API.G_MISS_NUM) THEN
10768         -- Bugfix 5101106: Recalculate qty
10769     IF p_funds_util_flt.offer_type = 'SCAN_DATA' THEN
10770          OPEN csr_offer_profile(l_funds_util_flt.activity_product_id);
10771          FETCH csr_offer_profile INTO l_offer_uom_code
10772                                , l_offer_quantity
10773                                , l_scan_value
10774                                , l_product_id;
10775          CLOSE csr_offer_profile;
10776          IF l_funds_util_flt.uom_code <> l_offer_uom_code THEN
10777            l_upd_line_util_rec.quantity := inv_convert.inv_um_convert(
10778                  item_id         => l_product_id
10779                  ,precision       => 2
10780                  ,from_quantity   => l_upd_line_util_rec.scan_unit * l_offer_quantity
10781                  ,from_unit       => l_offer_uom_code
10782                  ,to_unit         => l_funds_util_flt.uom_code
10783                  ,from_name       => NULL
10784                  ,to_name         => NULL
10785                 );
10786           ELSE
10787              l_upd_line_util_rec.quantity := l_upd_line_util_rec.scan_unit * l_offer_quantity;
10788           END IF;
10789      END IF;
10790 
10791      Update_Line_Util(
10792          p_api_version         => l_api_version
10793         ,p_init_msg_list       => FND_API.g_false
10794         ,p_commit              => FND_API.g_false
10795         ,p_validation_level    => FND_API.g_valid_level_full
10796         ,x_return_status       => l_return_status
10797         ,x_msg_count           => x_msg_count
10798         ,x_msg_data            => x_msg_data
10799         ,p_line_util_rec       => l_upd_line_util_rec
10800         ,x_object_version      => l_object_version_number
10801         ,px_upd_line_util_tbl  => l_upd_fund_util_tbl
10802      );
10803 
10804       FORALL i IN NVL(l_upd_fund_util_tbl.FIRST,1) .. NVL(l_upd_fund_util_tbl.LAST,0) --ninarasi fix for bug 13530939
10805               UPDATE ozf_funds_utilized_all_b
10806               SET    amount_remaining                  = amount_remaining - l_upd_fund_util_tbl(i).amount_remaining
10807                    , acctd_amount_remaining          = acctd_amount_remaining - l_upd_fund_util_tbl(i).acctd_amount_remaining
10808                    , plan_curr_amount_remaining      = plan_curr_amount_remaining - l_upd_fund_util_tbl(i).plan_curr_amount_remaining
10809                    , univ_curr_amount_remaining      = univ_curr_amount_remaining - l_upd_fund_util_tbl(i).univ_curr_amount_remaining
10810                    , fund_request_amount_remaining   = fund_request_amount_remaining - l_upd_fund_util_tbl(i).fund_request_amount_remaining
10811              WHERE utilization_id = l_upd_fund_util_tbl(i).utilization_id;
10812 
10813      l_upd_fund_util_tbl.delete;
10814 
10815      IF l_return_status =  fnd_api.g_ret_sts_error THEN
10816        RAISE FND_API.g_exc_error;
10817      ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
10818        RAISE FND_API.g_exc_unexpected_error;
10819      END IF;
10820      l_upd_line_util_rec.object_version_number := l_object_version_number;
10821   END IF;
10822 
10823   -- 3. update claim line earnings_associated_flag -------
10824   -- if there is no more earnings associated.
10825   OPEN csr_final_lu_amt(p_funds_util_flt.claim_line_id);
10826   FETCH csr_final_lu_amt INTO l_final_lu_amt;
10827   CLOSE csr_final_lu_amt;
10828 
10829   IF l_final_lu_amt = 0 OR
10830      l_final_lu_amt IS NULL THEN
10831      UPDATE ozf_claim_lines_all
10832        SET earnings_associated_flag = 'F'
10833        WHERE claim_line_id = p_funds_util_flt.claim_line_id;
10834   END IF;
10835 
10836   ------------------------- finish -------------------------------
10837   -- Check for commit
10838   IF FND_API.to_boolean(p_commit) THEN
10839     COMMIT;
10840   END IF;
10841 
10842   FND_MSG_PUB.count_and_get(
10843          p_encoded => FND_API.g_false,
10844          p_count   => x_msg_count,
10845          p_data    => x_msg_data
10846   );
10847 
10848   IF OZF_DEBUG_HIGH_ON THEN
10849      OZF_Utility_PVT.debug_message(l_full_name ||': end');
10850   END IF;
10851 
10852 EXCEPTION
10853   WHEN FND_API.g_exc_error THEN
10854     ROLLBACK TO Delete_Group_Line_Util;
10855     x_return_status := FND_API.g_ret_sts_error;
10856     FND_MSG_PUB.count_and_get (
10857            p_encoded => FND_API.g_false
10858           ,p_count   => x_msg_count
10859           ,p_data    => x_msg_data
10860     );
10861 
10862   WHEN FND_API.g_exc_unexpected_error THEN
10863     ROLLBACK TO Delete_Group_Line_Util;
10864     x_return_status := FND_API.g_ret_sts_unexp_error ;
10865     FND_MSG_PUB.count_and_get (
10866            p_encoded => FND_API.g_false
10867           ,p_count   => x_msg_count
10868           ,p_data    => x_msg_data
10869     );
10870 
10871   WHEN OTHERS THEN
10872     ROLLBACK TO Delete_Group_Line_Util;
10873     x_return_status := FND_API.g_ret_sts_unexp_error ;
10874     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
10875       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
10876     END IF;
10877     FND_MSG_PUB.count_and_get(
10878            p_encoded => FND_API.g_false
10879           ,p_count   => x_msg_count
10880           ,p_data    => x_msg_data
10881     );
10882 
10883 END Delete_Group_Line_Util;
10884 
10885 
10886 ---------------------------------------------------------------------
10887 -- PROCEDURE
10888 --    Update_Dummy_Utilizations
10889 --
10890 -- HISTORY
10891 --    10/15/2002  yizhang  Create.
10892 --   08-Aug-06    azahmed  Modified for FXGL ER
10893 --
10894 ---------------------------------------------------------------------
10895 PROCEDURE Update_Dummy_Utilizations(
10896    p_api_version        IN  NUMBER
10897   ,p_init_msg_list      IN  VARCHAR2  := FND_API.g_false
10898   ,p_commit             IN  VARCHAR2  := FND_API.g_false
10899   ,p_validation_level   IN  NUMBER    := FND_API.g_valid_level_full
10900 
10901   ,x_return_status      OUT NOCOPY VARCHAR2
10902   ,x_msg_count          OUT NOCOPY NUMBER
10903   ,x_msg_data           OUT NOCOPY VARCHAR2
10904 
10905   ,p_claim_line_util_id IN  NUMBER
10906   ,p_mode               IN  VARCHAR2  := OZF_CLAIM_UTILITY_PVT.g_auto_mode
10907 )
10908 IS
10909 l_api_version CONSTANT NUMBER       := 1.0;
10910 l_api_name    CONSTANT VARCHAR2(30) := 'Update_Dummy_Utilizations';
10911 l_full_name   CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
10912 l_return_status VARCHAR2(1);
10913 l_error_index          NUMBER;
10914 
10915 l_line_util_tbl       line_util_tbl_type;
10916 l_amount_rem    NUMBER;
10917 l_scan_unit_rem       NUMBER;
10918 l_total_amount        NUMBER;
10919 l_total_units         NUMBER;
10920 l_counter             NUMBER         := 1;
10921 
10922 l_claim_line_id       NUMBER;
10923 l_activity_product_id NUMBER;
10924 l_utilization_id      NUMBER;
10925 l_uom_code            VARCHAR2(3);
10926 l_quantity            NUMBER;
10927 l_currency_rec        currency_rec_type;
10928 
10929 CURSOR csr_line_util_info(cv_claim_line_util_id IN NUMBER) IS
10930   SELECT claim_line_id
10931         ,activity_product_id
10932         ,uom_code
10933         ,quantity
10934         ,amount
10935         ,scan_unit
10936         ,utilization_id
10937   FROM ozf_claim_lines_util
10938   WHERE claim_line_util_id = cv_claim_line_util_id;
10939 
10940 CURSOR csr_funds_utilized(cv_activity_product_id IN NUMBER) IS
10941   SELECT utilization_id
10942         ,amount_remaining
10943         ,scan_unit_remaining
10944   FROM ozf_funds_utilized_all_vl
10945   WHERE activity_product_id = cv_activity_product_id
10946   AND   adjustment_type_id = -8
10947   AND   utilization_type ='ADJUSTMENT'
10948   AND   amount_remaining <> 0;
10949 
10950 /*
10951 CURSOR csr_acc_adjustment(cv_offer_id IN NUMBER) IS
10952   SELECT utilization_id
10953         ,acctd_amount_remaining
10954   FROM ozf_funds_utilized_all_vl
10955   WHERE plan_type = 'OFFR'
10956   AND   plan_id = cv_offer_id
10957   AND   adjustment_type_id = -11
10958   AND   utilization_type = 'ADJUSTMENT';
10959 */
10960 
10961 BEGIN
10962   ----------------------- initialize --------------------
10963   SAVEPOINT Update_Dummy_Utilizations;
10964 
10965   IF OZF_DEBUG_HIGH_ON THEN
10966      OZF_Utility_PVT.debug_message(l_full_name||': start');
10967   END IF;
10968 
10969   IF FND_API.to_boolean(p_init_msg_list) THEN
10970     FND_MSG_PUB.initialize;
10971   END IF;
10972 
10973   IF NOT FND_API.compatible_api_call(
10974        l_api_version,
10975        p_api_version,
10976        l_api_name,
10977        g_pkg_name
10978   ) THEN
10979     RAISE FND_API.g_exc_unexpected_error;
10980   END IF;
10981 
10982   x_return_status := FND_API.g_ret_sts_success;
10983 
10984   -------------------- update -----------------------
10985   OPEN csr_line_util_info(p_claim_line_util_id);
10986   FETCH csr_line_util_info INTO l_claim_line_id
10987                                ,l_activity_product_id
10988                                ,l_uom_code
10989                                ,l_quantity
10990                                ,l_total_amount
10991                                ,l_total_units
10992                                ,l_utilization_id;
10993   CLOSE csr_line_util_info;
10994 
10995   -- associate adjustment for scan data
10996   -- amounts are reduced in claim currency and not in functional currency
10997   IF l_utilization_id = -1 THEN
10998     OPEN csr_funds_utilized(l_activity_product_id);
10999     LOOP
11000       FETCH csr_funds_utilized INTO l_line_util_tbl(l_counter).utilization_id
11001                                   , l_amount_rem
11002                                   , l_scan_unit_rem;
11003       EXIT WHEN csr_funds_utilized%NOTFOUND;
11004       IF l_total_amount > l_amount_rem THEN
11005          l_line_util_tbl(l_counter).amount := l_amount_rem;
11006          l_total_amount := l_total_amount - l_amount_rem;
11007          l_line_util_tbl(l_counter).scan_unit := l_scan_unit_rem;
11008          l_total_units := l_total_units - l_scan_unit_rem;
11009       ELSE
11010          l_line_util_tbl(l_counter).amount := l_total_amount;
11011          l_total_amount := 0;
11012          l_line_util_tbl(l_counter).scan_unit := l_total_units;
11013          l_total_units := 0;
11014       END IF;
11015 
11016       l_line_util_tbl(l_counter).claim_line_id := l_claim_line_id;
11017       l_line_util_tbl(l_counter).activity_product_id := l_activity_product_id;
11018       l_line_util_tbl(l_counter).uom_code := l_uom_code;
11019       l_line_util_tbl(l_counter).quantity := l_quantity;
11020       l_counter := l_counter + 1;
11021       EXIT WHEN l_total_amount = 0;
11022     END LOOP;
11023     CLOSE csr_funds_utilized;
11024 
11025   -- associate adjustment for accrual offer
11026   /*
11027   ELSIF l_utilization_id = -2 THEN
11028     OPEN csr_acc_adjustment(l_activity_product_id);
11029     LOOP
11030       FETCH csr_acc_adjustment INTO l_line_util_tbl(l_counter).utilization_id
11031                                   , l_acctd_amount_rem;
11032       EXIT WHEN csr_acc_adjustment%NOTFOUND;
11033       IF l_total_amount > l_acctd_amount_rem THEN
11034          l_line_util_tbl(l_counter).acctd_amount := l_acctd_amount_rem;
11035          l_total_amount := l_total_amount - l_acctd_amount_rem;
11036       ELSE
11037          l_line_util_tbl(l_counter).acctd_amount := l_total_amount;
11038          l_total_amount := 0;
11039       END IF;
11040 
11041       l_line_util_tbl(l_counter).claim_line_id := l_claim_line_id;
11042       l_counter := l_counter + 1;
11043       EXIT WHEN l_total_amount = 0;
11044     END LOOP;
11045     CLOSE csr_acc_adjustment;
11046   */
11047   END IF;
11048 
11049   -- delete dummy utilizations
11050   DELETE FROM ozf_claim_lines_util_all
11051   WHERE claim_line_util_id = p_claim_line_util_id;
11052 
11053   ---------- Create Group Line Utils -------------------
11054   --Need to check
11055   IF l_counter > 1 THEN
11056     Create_Line_Util_Tbl(
11057         p_api_version            => l_api_version
11058        ,p_init_msg_list          => FND_API.g_false
11059        ,p_commit                 => FND_API.g_false
11060        ,p_validation_level       => p_validation_level
11061        ,x_return_status          => l_return_status
11062        ,x_msg_data               => x_msg_data
11063        ,x_msg_count              => x_msg_count
11064        ,p_line_util_tbl          => l_line_util_tbl
11065        ,p_currency_rec           => l_currency_rec
11066        ,p_mode                   => p_mode
11067        ,x_error_index            => l_error_index
11068     );
11069     IF l_return_status =  fnd_api.g_ret_sts_error THEN
11070       RAISE FND_API.g_exc_error;
11071     ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11072       RAISE FND_API.g_exc_unexpected_error;
11073     END IF;
11074   END IF;
11075 
11076   ------------------------- finish -------------------------------
11077   -- Check for commit
11078   IF FND_API.to_boolean(p_commit) THEN
11079     COMMIT;
11080   END IF;
11081 
11082   FND_MSG_PUB.count_and_get(
11083          p_encoded => FND_API.g_false,
11084          p_count   => x_msg_count,
11085          p_data    => x_msg_data
11086   );
11087 
11088   IF OZF_DEBUG_HIGH_ON THEN
11089      OZF_Utility_PVT.debug_message(l_full_name ||': end');
11090   END IF;
11091 
11092 EXCEPTION
11093   WHEN FND_API.g_exc_error THEN
11094     ROLLBACK TO Update_Dummy_Utilizations;
11095     x_return_status := FND_API.g_ret_sts_error;
11096     FND_MSG_PUB.count_and_get (
11097            p_encoded => FND_API.g_false
11098           ,p_count   => x_msg_count
11099           ,p_data    => x_msg_data
11100     );
11101 
11102   WHEN FND_API.g_exc_unexpected_error THEN
11103     ROLLBACK TO Update_Dummy_Utilizations;
11104     x_return_status := FND_API.g_ret_sts_unexp_error ;
11105     FND_MSG_PUB.count_and_get (
11106            p_encoded => FND_API.g_false
11107           ,p_count   => x_msg_count
11108           ,p_data    => x_msg_data
11109     );
11110 
11111   WHEN OTHERS THEN
11112     ROLLBACK TO Update_Dummy_Utilizations;
11113     x_return_status := FND_API.g_ret_sts_unexp_error ;
11114     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
11115       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
11116     END IF;
11117     FND_MSG_PUB.count_and_get(
11118            p_encoded => FND_API.g_false
11119           ,p_count   => x_msg_count
11120           ,p_data    => x_msg_data
11121     );
11122 
11123 END Update_Dummy_Utilizations;
11124 
11125 ---------------------------------------------------------------------
11126 -- PROCEDURE
11127 --    Adjust_Fund_Utilization
11128 --
11129 -- HISTORY
11130 --    10/15/2002  yizhang  Create.
11131 --    01/15/2003  yizhang  Calling point moved from post-approval to
11132 --                         post-closed
11133 --    03/24/2003  yizhang  p_mode is used to indicate the calling point
11134 --                         of the procedure.
11135 --   16/03/06     azahmed  Bugfix 5101106
11136 --   27/03/06     azahmed  Bugfix 5119143
11137 ---------------------------------------------------------------------
11138 PROCEDURE Adjust_Fund_Utilization(
11139    p_api_version        IN  NUMBER
11140   ,p_init_msg_list      IN  VARCHAR2  := FND_API.g_false
11141   ,p_commit             IN  VARCHAR2  := FND_API.g_false
11142   ,p_validation_level   IN  NUMBER    := FND_API.g_valid_level_full
11143 
11144   ,x_return_status      OUT NOCOPY VARCHAR2
11145   ,x_msg_count          OUT NOCOPY NUMBER
11146   ,x_msg_data           OUT NOCOPY VARCHAR2
11147 
11148   ,p_claim_id           IN  NUMBER
11149   ,p_mode               IN  VARCHAR2  := OZF_CLAIM_UTILITY_PVT.g_auto_mode
11150 
11151   ,x_next_status        OUT NOCOPY VARCHAR2
11152 )
11153 IS
11154 l_api_version CONSTANT NUMBER       := 1.0;
11155 l_api_name    CONSTANT VARCHAR2(30) := 'Adjust_Fund_Utilization';
11156 l_full_name   CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
11157 l_return_status VARCHAR2(1);
11158 
11159 l_next_status          VARCHAR2(15) := 'CLOSED';
11160 l_cust_account_id      NUMBER;
11161 l_claim_line_util_id   NUMBER;
11162 l_activity_product_id  NUMBER;
11163 l_scan_unit            NUMBER;
11164 l_acctd_amount         NUMBER;
11165 l_plan_id              NUMBER;
11166 l_fund_id              NUMBER;
11167 l_act_budget_id        NUMBER;
11168 l_bill_to_site_id      NUMBER;
11169 l_ship_to_site_id      NUMBER;
11170 l_fund_curr_code       VARCHAR2(15);
11171 l_plan_curr_code       VARCHAR2(15);
11172 l_adjustment_flag      VARCHAR2(1);
11173 
11174 l_utilization_id       NUMBER;
11175 l_total_scan_unit      NUMBER;
11176 l_old_scan_unit_rem    NUMBER;
11177 l_new_scan_unit_rem    NUMBER;
11178 l_act_budgets_rec      ozf_actbudgets_pvt.act_budgets_rec_type;
11179 l_act_util_rec         ozf_actbudgets_pvt.act_util_rec_type ;
11180 
11181 CURSOR csr_scan_over_utilized(cv_claim_id IN NUMBER) IS
11182   SELECT lu.claim_line_util_id
11183         ,lu.activity_product_id
11184         ,lu.scan_unit
11185         ,ap.act_product_used_by_id
11186         ,ap.adjustment_flag
11187   FROM ozf_claim_lines_util lu, ams_act_products ap
11188       ,ozf_claim_lines cln
11189   WHERE cln.claim_id = cv_claim_id
11190   AND   lu.claim_line_id = cln.claim_line_id
11191   AND   lu.activity_product_id = ap.activity_product_id
11192   AND   lu.utilization_id = -1
11193   ORDER BY ap.adjustment_flag DESC;
11194 
11195 -- cursor modified for bugfix 5101106  :  coupon count * offer quantity = quantity
11196 -- the coupon count(scan_unit) is independent of the offer and claim uom code
11197 CURSOR csr_line_utils(cv_claim_id IN NUMBER) IS
11198  SELECT SUM(lu.scan_unit * amp.quantity)
11199         ,lu.utilization_id
11200   FROM  ozf_claim_lines_util_all lu
11201        ,ozf_claim_lines_all cln
11202        ,ams_act_products amp
11203   WHERE cln.claim_id = cv_claim_id
11204   AND   lu.claim_line_id = cln.claim_line_id
11205   AND   lu.activity_product_id = amp.activity_product_id
11206   GROUP BY lu.utilization_id;
11207 
11208 --modified for bugfix 5119143
11209 CURSOR csr_claim_info(cv_claim_id IN NUMBER) IS
11210 select CUST_ACCOUNT_ID , CUST_BILLTO_ACCT_SITE_ID ,  CUST_SHIPTO_ACCT_SITE_ID
11211 from ozf_claims_all
11212 where claim_id = cv_claim_id;
11213 
11214 
11215 BEGIN
11216   ----------------------- initialize --------------------
11217   SAVEPOINT Adjust_Fund_Utilization;
11218 
11219   IF OZF_DEBUG_HIGH_ON THEN
11220      OZF_Utility_PVT.debug_message(l_full_name||': start');
11221   END IF;
11222 
11223   IF FND_API.to_boolean(p_init_msg_list) THEN
11224     FND_MSG_PUB.initialize;
11225   END IF;
11226 
11227   IF NOT FND_API.compatible_api_call(
11228        l_api_version,
11229        p_api_version,
11230        l_api_name,
11231        g_pkg_name
11232   ) THEN
11233     RAISE FND_API.g_exc_unexpected_error;
11234   END IF;
11235 
11236   x_return_status := FND_API.g_ret_sts_success;
11237 
11238   -- initialized x_next_status; Settlement process needs to be contine if there is no
11239   -- associated earnings attached to claim line.
11240   x_next_status := 'CLOSED';
11241 
11242   ------------------ dummy utilizations -------------------
11243  IF p_mode = 'ADJ_FUND' THEN
11244   -- create adjustment for scan data offers
11245   OPEN csr_scan_over_utilized(p_claim_id);
11246   LOOP
11247     FETCH csr_scan_over_utilized INTO l_claim_line_util_id
11248                                 ,l_activity_product_id
11249                                 ,l_scan_unit
11250                                 ,l_plan_id
11251                                 ,l_adjustment_flag;
11252     EXIT WHEN csr_scan_over_utilized%NOTFOUND;
11253 
11254     --Bugfix 5119143 get customer Info
11255     OPEN csr_claim_info(p_claim_id);
11256     FETCH csr_claim_info INTO l_cust_account_id , l_bill_to_site_id  , l_ship_to_site_id ;
11257     CLOSE csr_claim_info;
11258 
11259     IF l_adjustment_flag = 'Y' THEN
11260       --modified for Bugfix 5119143
11261       Ozf_Fund_Adjustment_Pvt.adjust_utilized_budget(
11262        p_claim_id             => p_claim_id
11263       ,p_offer_id             => l_plan_id
11264       ,p_product_activity_id  => l_activity_product_id
11265       ,p_amount               => l_scan_unit
11266       ,p_cust_acct_id         => l_cust_account_id
11267       ,p_bill_to_cust_acct_id => l_cust_account_id
11268       ,p_bill_to_site_use_id  => l_bill_to_site_id
11269       ,p_ship_to_site_use_id  => l_ship_to_site_id
11270       ,p_api_version          => l_api_version
11271       ,p_init_msg_list        => FND_API.g_false
11272       ,p_commit               => FND_API.g_false
11273       ,x_msg_count            => x_msg_count
11274       ,x_msg_data             => x_msg_data
11275       ,x_return_status        => l_return_status
11276       );
11277       IF l_return_status =  fnd_api.g_ret_sts_error THEN
11278         RAISE FND_API.g_exc_error;
11279       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11280         RAISE FND_API.g_exc_unexpected_error;
11281       END IF;
11282 
11283       Update_Dummy_Utilizations(
11284           p_api_version          => l_api_version
11285          ,p_init_msg_list        => FND_API.g_false
11286          ,p_commit               => FND_API.g_false
11287          ,p_validation_level     => p_validation_level
11288          ,x_return_status        => l_return_status
11289          ,x_msg_count            => x_msg_count
11290          ,x_msg_data             => x_msg_data
11291          ,p_claim_line_util_id   => l_claim_line_util_id
11292          ,p_mode                 => p_mode
11293       );
11294       IF l_return_status = fnd_api.g_ret_sts_error THEN
11295         RAISE FND_API.g_exc_error;
11296       ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
11297         RAISE FND_API.g_exc_unexpected_error;
11298       END IF;
11299 
11300       x_next_status := 'CLOSED';
11301     ELSE
11302       x_next_status := 'PENDING_CLOSED';
11303 
11304       -- yizhang: 15-JAN-2003: should we decide to support adjustment workflow, we need to
11305       --          change the settlement fetcher process
11306       -- mchang: As of 24-OCT-2002, we don't support manual adjust over utilization. TM raise
11307       --         error everytime if ams_act_products.adjustment_flag is 'N'.
11308       IF OZF_DEBUG_LOW_ON THEN
11309          FND_MESSAGE.Set_Name('OZF','OZF_API_DEBUG_MESSAGE');
11310          FND_MESSAGE.Set_Token('TEXT',l_full_name||' : Manual Adjust Over Utilization is not supported.');
11311          FND_MSG_PUB.Add;
11312       END IF;
11313 
11314       RAISE FND_API.g_exc_unexpected_error;
11315       --Invoke_Adjustment_Workflow();
11316     END IF;
11317 
11318   END LOOP;
11319   CLOSE csr_scan_over_utilized;
11320  ELSIF p_mode = 'UPD_SCAN' THEN
11321   ----------------- update scan_unit_remaining -------------------
11322    --modified for bugfix 5101106
11323   OPEN csr_line_utils(p_claim_id);
11324   LOOP
11325     FETCH csr_line_utils INTO l_total_scan_unit
11326                              ,l_utilization_id
11327 ;
11328 
11329     EXIT WHEN csr_line_utils%NOTFOUND;
11330 
11331     IF l_total_scan_unit IS NOT NULL THEN
11332 
11333         UPDATE ozf_funds_utilized_all_b
11334         SET scan_unit_remaining =   scan_unit_remaining - l_total_scan_unit
11335         WHERE utilization_id = l_utilization_id;
11336     END IF;
11337 
11338   END LOOP;
11339   CLOSE csr_line_utils;
11340  END IF;
11341 
11342   ------------------------- finish -------------------------------
11343   -- Check for commit
11344   IF FND_API.to_boolean(p_commit) THEN
11345     COMMIT;
11346   END IF;
11347 
11348   FND_MSG_PUB.count_and_get(
11349          p_encoded => FND_API.g_false,
11350          p_count   => x_msg_count,
11351          p_data    => x_msg_data
11352   );
11353 
11354   IF OZF_DEBUG_HIGH_ON THEN
11355      OZF_Utility_PVT.debug_message(l_full_name ||': end');
11356   END IF;
11357 
11358 EXCEPTION
11359   WHEN FND_API.g_exc_error THEN
11360     ROLLBACK TO Adjust_Fund_Utilization;
11361     x_return_status := FND_API.g_ret_sts_error;
11362     FND_MSG_PUB.count_and_get (
11363            p_encoded => FND_API.g_false
11364           ,p_count   => x_msg_count
11365           ,p_data    => x_msg_data
11366     );
11367 
11368   WHEN FND_API.g_exc_unexpected_error THEN
11369     ROLLBACK TO Adjust_Fund_Utilization;
11370     x_return_status := FND_API.g_ret_sts_unexp_error ;
11371     FND_MSG_PUB.count_and_get (
11372            p_encoded => FND_API.g_false
11373           ,p_count   => x_msg_count
11374           ,p_data    => x_msg_data
11375     );
11376 
11377   WHEN OTHERS THEN
11378     ROLLBACK TO Adjust_Fund_Utilization;
11379     x_return_status := FND_API.g_ret_sts_unexp_error ;
11380     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
11381       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
11382     END IF;
11383     FND_MSG_PUB.count_and_get(
11384            p_encoded => FND_API.g_false
11385           ,p_count   => x_msg_count
11386           ,p_data    => x_msg_data
11387     );
11388 
11389 END Adjust_Fund_Utilization;
11390 ---------------------------------------------------------------------
11391 -- FUNCTION
11392 --    Calculate_FXGL_Amount
11393 --
11394 -- PURPOSE
11395 --    Returns FXGL amount of the claim line util
11396 --
11397 -- PARAMETERS
11398 --
11399 --
11400 -- NOTES
11401 -- created by azahmed For FXGL ER:
11402 -- 16-04-2009  psomyaju  Modified for Multicurrency ER.
11403 ---------------------------------------------------------------------
11404 FUNCTION Calculate_FXGL_Amount(
11405    p_line_util_rec       IN  line_util_rec_type
11406   ,p_currency_rec        IN  currency_rec_type
11407 ) RETURN NUMBER
11408 IS
11409 
11410 CURSOR csr_funds_util_details(cv_utilization_id IN NUMBER) IS
11411   SELECT exchange_rate_type
11412        , exchange_rate_date
11413        , exchange_rate
11414   FROM   ozf_funds_utilized_all_b
11415   WHERE  utilization_id = cv_utilization_id;
11416 
11417 l_fu_plan_currency_code   VARCHAR2(15);
11418 l_fu_exc_rate             NUMBER;
11419 l_fu_exc_date             DATE;
11420 l_fu_exc_type             VARCHAR2(30);
11421 l_return_status           VARCHAR2(1);
11422 l_utilized_amount         NUMBER := 0;
11423 l_fxgl_acctd_amount       NUMBER := 0;
11424 l_fu_plan_amount          NUMBER := 0;
11425 
11426 BEGIN
11427 
11428    IF p_currency_rec.claim_currency_code = p_currency_rec.transaction_currency_code AND
11429       p_currency_rec.claim_currency_code <> p_currency_rec.functional_currency_code
11430    THEN
11431 
11432       OPEN csr_funds_util_details(p_line_util_rec.utilization_id);
11433       FETCH csr_funds_util_details INTO l_fu_exc_type, l_fu_exc_date, l_fu_exc_rate;
11434       CLOSE csr_funds_util_details;
11435 
11436 
11437         IF p_line_util_rec.exchange_rate <> l_fu_exc_rate AND l_fu_exc_rate IS NOT NULL THEN
11438                OZF_UTILITY_PVT.Convert_Currency(
11439                  p_from_currency   => p_currency_rec.transaction_currency_code
11440                 ,p_to_currency     => p_currency_rec.functional_currency_code
11441                 ,p_conv_type       => l_fu_exc_type
11442                 ,p_conv_date       => l_fu_exc_date
11443                 ,p_from_amount     => p_line_util_rec.amount
11444                 ,x_return_status   => l_return_status
11445                 ,x_to_amount       => l_utilized_amount
11446                 ,x_rate            => l_fu_exc_rate
11447               );
11448               IF l_return_status = FND_API.g_ret_sts_error THEN
11449                       RAISE FND_API.g_exc_error;
11450               ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
11451                   RAISE FND_API.g_exc_unexpected_error;
11452         END IF;
11453         l_fxgl_acctd_amount := p_line_util_rec.acctd_amount - l_utilized_amount;
11454 
11455       END IF;
11456    ELSE
11457       l_fxgl_acctd_amount := 0;
11458    END IF;
11459 
11460 
11461       RETURN l_fxgl_acctd_amount;
11462 END Calculate_FXGL_Amount;
11463 
11464 
11465 ---------------------------------------------------------------------
11466 -- FUNCTION
11467 --    Perform_Approval_Required
11468 --
11469 -- PURPOSE
11470 --    Returns TRUE if the claim requires performance approval.
11471 --
11472 -- PARAMETERS
11473 --    p_claim_id
11474 --
11475 -- NOTES
11476 ---------------------------------------------------------------------
11477 FUNCTION Perform_Approval_Required(
11478    p_claim_id           IN  NUMBER
11479 ) RETURN VARCHAR2
11480 IS
11481 
11482 l_offer_id               NUMBER;
11483 l_cust_account_id        NUMBER;
11484 l_performance_flag       VARCHAR2(1);
11485 l_resale_flag            VARCHAR2(1);
11486 l_offer_perf_tbl         offer_performance_tbl_type;
11487 l_activity_type          VARCHAR2(30);
11488 l_perf_appr_require      VARCHAR2(1);
11489 l_cust_setup_id          NUMBER;
11490 
11491 CURSOR csr_line_util(cv_claim_id IN NUMBER) IS
11492   SELECT fu.plan_id, fu.cust_account_id, ln.activity_type
11493   FROM ozf_claim_lines_util lu, ozf_funds_utilized_all_b fu, ozf_claim_lines ln
11494   WHERE lu.utilization_id = fu.utilization_id
11495   AND lu.claim_line_id = ln.claim_line_id
11496   AND ln.claim_id = cv_claim_id
11497   GROUP BY fu.plan_id, fu.cust_account_id, ln.activity_type;
11498 
11499 CURSOR csr_claim_setup_info(cv_claim_id IN NUMBER) IS
11500   SELECT custom_setup_id
11501   FROM ozf_claims
11502   WHERE claim_id = cv_claim_id;
11503 
11504 CURSOR csr_claim_perf_req_attr(cv_custom_setup_id IN NUMBER) IS
11505   SELECT NVL(attr_available_flag, 'N')
11506   FROM ams_custom_setup_attr
11507   WHERE custom_setup_id = cv_custom_setup_id
11508   AND object_attribute = 'PAPR';
11509 
11510 BEGIN
11511 
11512    -- Fix for bug 5177341
11513   OPEN csr_claim_setup_info(p_claim_id);
11514   FETCH csr_claim_setup_info INTO l_cust_setup_id;
11515   CLOSE csr_claim_setup_info;
11516 
11517   OPEN csr_claim_perf_req_attr(l_cust_setup_id);
11518   FETCH csr_claim_perf_req_attr INTO l_perf_appr_require;
11519   CLOSE csr_claim_perf_req_attr;
11520 
11521   -- Approval is not required if claim performance approval is false
11522   -- in Claim Custom set up.
11523   IF l_perf_appr_require = 'N' THEN
11524       RETURN FND_API.g_false;
11525   END IF;
11526 
11527    -- Approval is not required if override profile is Yes
11528    IF FND_PROFILE.VALUE('OZF_OFFR_OVERRIDE_PERF_FLAG') = 'Y' THEN
11529       RETURN FND_API.g_false;
11530    END IF;
11531 
11532    OPEN csr_line_util(p_claim_id);
11533    LOOP
11534       FETCH csr_line_util INTO l_offer_id, l_cust_account_id, l_activity_type;
11535       EXIT WHEN csr_line_util%NOTFOUND;
11536 
11537       -- check resale data for speical pricing requests
11538       IF l_activity_type = 'SPECIAL_PRICE' THEN
11539          l_resale_flag := 'T';
11540       ELSE
11541          l_resale_flag := 'F';
11542       END IF;
11543 
11544       IF l_offer_id IS NOT NULL AND l_cust_account_id IS NOT NULL THEN
11545          Check_Offer_Performance(
11546              p_cust_account_id      => l_cust_account_id
11547             ,p_offer_id             => l_offer_id
11548             ,p_resale_flag          => l_resale_flag
11549             ,p_check_all_flag       => 'F'
11550             ,x_performance_flag     => l_performance_flag
11551             ,x_offer_perf_tbl       => l_offer_perf_tbl
11552          );
11553 
11554          IF l_performance_flag = FND_API.g_false THEN
11555             -- update ozf_claims.approved_flag
11556             UPDATE ozf_claims_all
11557             SET approved_flag = 'F'
11558             WHERE claim_id = p_claim_id;
11559 
11560             RETURN FND_API.g_true;
11561          END IF;
11562       END IF;
11563    END LOOP;
11564    CLOSE csr_line_util;
11565 
11566    -- update ozf_claims.approved_flag
11567    UPDATE ozf_claims_all
11568    SET approved_flag = 'T'
11569    WHERE claim_id = p_claim_id;
11570 
11571    RETURN FND_API.g_false;
11572 END Perform_Approval_Required;
11573 
11574 
11575 ---------------------------------------------------------------------
11576 -- FUNCTION
11577 --    Earnings_Approval_Required
11578 --
11579 -- PURPOSE
11580 --    Returns TRUE if the claim requires earnings approval.
11581 --
11582 -- PARAMETERS
11583 --    p_claim_id
11584 --
11585 -- NOTES
11586 ---------------------------------------------------------------------
11587 FUNCTION Earnings_Approval_Required(
11588    p_claim_id           IN  NUMBER
11589 ) RETURN VARCHAR2
11590 IS
11591 
11592 l_cust_account_id            NUMBER;
11593 l_un_earned_pay_thold_type   VARCHAR2(30);
11594 l_un_earned_pay_thold_amount NUMBER;
11595 l_plan_id                    NUMBER;
11596 l_amount_claim_asso          NUMBER;
11597 l_amount_earned              NUMBER;
11598 l_amount_remaining           NUMBER;
11599 l_amount_threshold           NUMBER;
11600 l_util_cust_account_id       NUMBER;
11601 l_un_earned_pay_thold_flag   VARCHAR2(1) := 'F';
11602 l_currOrgId                  NUMBER     := MO_GLOBAL.GET_CURRENT_ORG_ID();
11603 
11604 CURSOR csr_claim_info(cv_claim_id IN NUMBER) IS
11605   SELECT cust_account_id
11606   FROM ozf_claims
11607   WHERE claim_id = cv_claim_id;
11608 
11609 CURSOR csr_cust_trd_prfl_info(cv_cust_account_id IN NUMBER) IS
11610   SELECT un_earned_pay_thold_type, un_earned_pay_thold_amount, un_earned_pay_thold_flag
11611   FROM ozf_cust_trd_prfls
11612   WHERE cust_account_id = cv_cust_account_id;
11613 
11614 CURSOR csr_pty_trd_prfl_info(cv_cust_account_id IN NUMBER) IS
11615   SELECT p.un_earned_pay_thold_type, p.un_earned_pay_thold_amount, p.un_earned_pay_thold_flag
11616   FROM ozf_cust_trd_prfls p, hz_cust_accounts c
11617   WHERE p.party_id = c.party_id
11618   AND p.cust_account_id IS NULL
11619   AND c.cust_account_id = cv_cust_account_id;
11620 
11621  -- fix for bug 5042046
11622 CURSOR csr_sys_param_info IS
11623   SELECT un_earned_pay_thold_type, un_earned_pay_thold_amount, un_earned_pay_thold_flag
11624   FROM ozf_sys_parameters
11625   WHERE  org_id = l_currOrgId;
11626 
11627 CURSOR csr_line_util(cv_claim_id IN NUMBER) IS
11628   SELECT fu.plan_id, fu.cust_account_id, sum(lu.acctd_amount)
11629   FROM ozf_claim_lines_util lu, ozf_funds_utilized_all_b fu
11630      , ozf_claim_lines ln
11631   WHERE lu.utilization_id = fu.utilization_id
11632   AND lu.claim_line_id = ln.claim_line_id
11633   AND ln.claim_id = cv_claim_id
11634   GROUP BY fu.plan_id, fu.cust_account_id;
11635 
11636 CURSOR csr_funds_util(cv_cust_account_id IN NUMBER, cv_plan_id IN NUMBER) IS
11637   SELECT sum(acctd_amount), sum(acctd_amount_remaining)
11638   FROM ozf_funds_utilized_all_b
11639   WHERE utilization_type in ('ACCRUAL', 'ADJUSTMENT')
11640   AND org_id = l_currOrgId
11641   AND gl_posted_flag = 'Y'
11642   AND plan_type = 'OFFR'
11643   AND plan_id = cv_plan_id
11644   AND cust_account_id = cv_cust_account_id;
11645 
11646 BEGIN
11647    -- get claim info
11648    OPEN csr_claim_info(p_claim_id);
11649    FETCH csr_claim_info INTO l_cust_account_id;
11650    CLOSE csr_claim_info;
11651 
11652    l_un_earned_pay_thold_type := NULL;
11653    l_un_earned_pay_thold_amount := NULL;
11654 
11655    OPEN csr_cust_trd_prfl_info(l_cust_account_id);
11656    FETCH csr_cust_trd_prfl_info INTO l_un_earned_pay_thold_type,
11657                                      l_un_earned_pay_thold_amount,
11658                                      l_un_earned_pay_thold_flag;
11659    CLOSE csr_cust_trd_prfl_info;
11660 
11661    IF l_un_earned_pay_thold_type IS NULL THEN
11662       OPEN csr_pty_trd_prfl_info(l_cust_account_id);
11663       FETCH csr_pty_trd_prfl_info INTO l_un_earned_pay_thold_type,
11664                                        l_un_earned_pay_thold_amount,
11665                                        l_un_earned_pay_thold_flag;
11666       CLOSE csr_pty_trd_prfl_info;
11667 
11668       IF l_un_earned_pay_thold_type IS NULL THEN
11669          OPEN csr_sys_param_info;
11670          FETCH csr_sys_param_info INTO l_un_earned_pay_thold_type,
11671                                        l_un_earned_pay_thold_amount,
11672                                        l_un_earned_pay_thold_flag;
11673          CLOSE csr_sys_param_info;
11674       END IF;
11675    END IF;
11676 
11677    -- Fix for bug 5177341
11678    IF l_un_earned_pay_thold_type = 'UNCONDITIONAL' OR
11679            l_un_earned_pay_thold_flag = 'F' THEN
11680       RETURN FND_API.g_false;
11681    END IF;
11682 
11683    OPEN csr_line_util(p_claim_id);
11684    LOOP
11685       FETCH csr_line_util INTO l_plan_id,
11686                                l_util_cust_account_id,
11687                                l_amount_claim_asso;
11688       EXIT WHEN csr_line_util%NOTFOUND;
11689 
11690       OPEN csr_funds_util(l_util_cust_account_id, l_plan_id);
11691       FETCH csr_funds_util INTO l_amount_earned, l_amount_remaining;
11692       CLOSE csr_funds_util;
11693 
11694       --Fix for bug 7527018
11695       IF l_amount_remaining < 0 THEN
11696          IF l_un_earned_pay_thold_type = 'PERCENT' THEN
11697             l_amount_threshold := l_amount_earned * (l_un_earned_pay_thold_amount / 100.0);
11698          ELSE
11699             l_amount_threshold := l_un_earned_pay_thold_amount;
11700          END IF;
11701 
11702          --Fix for bug 7527018
11703          IF ABS(l_amount_remaining) > l_amount_threshold THEN
11704             RETURN FND_API.g_true;
11705          END IF;
11706       END IF;
11707    END LOOP;
11708    CLOSE csr_line_util;
11709 
11710    RETURN FND_API.g_false;
11711 END Earnings_Approval_Required;
11712 
11713 
11714 ---------------------------------------------------------------------
11715 -- PROCEDURE
11716 --   Check_Offer_Performance_Tbl
11717 --
11718 -- PURPOSE
11719 --    For the associated earnings in the given claim, find the offer
11720 --    performance requirements that the customer has not met.
11721 --
11722 -- PARAMETERS
11723 --    p_claim_id          : customer account id
11724 --
11725 -- HISTORY
11726 ---------------------------------------------------------------------
11727 PROCEDURE Check_Offer_Performance_Tbl(
11728    p_claim_id                  IN  NUMBER
11729 
11730   ,x_offer_perf_tbl            OUT NOCOPY offer_performance_tbl_type
11731 )
11732 IS
11733 
11734 l_offer_id               NUMBER;
11735 l_cust_account_id        NUMBER;
11736 l_performance_flag       VARCHAR2(1);
11737 l_resale_flag            VARCHAR2(1);
11738 l_offer_perf_tbl         offer_performance_tbl_type;
11739 l_x_offer_perf_tbl       offer_performance_tbl_type;
11740 l_activity_type          VARCHAR2(30);
11741 l_counter                PLS_INTEGER := 0;
11742 
11743 CURSOR csr_line_util(cv_claim_id IN NUMBER) IS
11744   SELECT fu.plan_id, fu.cust_account_id, ln.activity_type
11745   FROM ozf_claim_lines_util lu, ozf_funds_utilized_all_b fu, ozf_claim_lines ln
11746   WHERE lu.utilization_id = fu.utilization_id
11747   AND lu.claim_line_id = ln.claim_line_id
11748   AND ln.claim_id = cv_claim_id
11749   GROUP BY fu.plan_id, fu.cust_account_id, ln.activity_type;
11750 
11751 BEGIN
11752    OPEN csr_line_util(p_claim_id);
11753    LOOP
11754       FETCH csr_line_util INTO l_offer_id, l_cust_account_id, l_activity_type;
11755       EXIT WHEN csr_line_util%NOTFOUND;
11756 
11757       -- check resale data for speical pricing requests
11758       IF l_activity_type = 'SPECIAL_PRICE' THEN
11759          l_resale_flag := 'T';
11760       ELSE
11761          l_resale_flag := 'F';
11762       END IF;
11763 
11764       IF l_offer_id IS NOT NULL AND l_cust_account_id IS NOT NULL THEN
11765          Check_Offer_Performance(
11766              p_cust_account_id      => l_cust_account_id
11767             ,p_offer_id             => l_offer_id
11768             ,p_resale_flag          => l_resale_flag
11769             ,p_check_all_flag       => 'F'
11770             ,x_performance_flag     => l_performance_flag
11771             ,x_offer_perf_tbl       => l_offer_perf_tbl
11772          );
11773 
11774          IF l_performance_flag = FND_API.g_false AND
11775             l_offer_perf_tbl.count > 0
11776          THEN
11777             FOR j IN l_offer_perf_tbl.FIRST..l_offer_perf_tbl.LAST LOOP
11778                l_counter := l_counter + 1;
11779                l_x_offer_perf_tbl(l_counter) := l_offer_perf_tbl(j);
11780             END LOOP;
11781          END IF;
11782       END IF;
11783    END LOOP;
11784    CLOSE csr_line_util;
11785 
11786    x_offer_perf_tbl := l_x_offer_perf_tbl;
11787 END Check_Offer_Performance_Tbl;
11788 
11789 
11790 ---------------------------------------------------------------------
11791 -- PROCEDURE
11792 --   Check_Offer_Earning_Tbl
11793 --
11794 -- PURPOSE
11795 --    For the associated earnings in the given claim, find the offers
11796 --    whose paid amount is greater than the available amount
11797 --
11798 -- PARAMETERS
11799 --    p_claim_id          : customer account id
11800 --
11801 -- HISTORY
11802 ---------------------------------------------------------------------
11803 PROCEDURE Check_Offer_Earning_Tbl(
11804    p_claim_id                  IN  NUMBER
11805 
11806   ,x_offer_earn_tbl            OUT NOCOPY offer_earning_tbl_type
11807 )
11808 IS
11809 l_plan_id                    NUMBER;
11810 l_acctd_amount               NUMBER;
11811 l_counter                    PLS_INTEGER := 0;
11812 
11813 CURSOR csr_pay_over_earn(cv_claim_id IN NUMBER) IS
11814   SELECT lu.activity_product_id, sum(lu.acctd_amount)
11815   FROM ozf_claim_lines_util lu, ozf_claim_lines ln
11816   WHERE lu.claim_line_id = ln.claim_line_id
11817   AND lu.utilization_id = -2
11818   AND ln.claim_id = cv_claim_id
11819   GROUP BY lu.activity_product_id;
11820 
11821 BEGIN
11822    OPEN csr_pay_over_earn(p_claim_id);
11823    LOOP
11824       FETCH csr_pay_over_earn INTO l_plan_id, l_acctd_amount;
11825       EXIT WHEN csr_pay_over_earn%NOTFOUND;
11826 
11827       l_counter := l_counter + 1;
11828       x_offer_earn_tbl(l_counter).offer_id := l_plan_id;
11829       x_offer_earn_tbl(l_counter).acctd_amount_over := l_acctd_amount;
11830 
11831    END LOOP;
11832    CLOSE csr_pay_over_earn;
11833 
11834 END Check_Offer_Earning_Tbl;
11835 
11836 ---------------------------------------------------------------------
11837 -- PROCEDURE
11838 --   Initiate_SD_Payment
11839 --   R12.1 Enhancements
11840 --
11841 -- PARAMETERS
11842 --    p_ship_debit_id   : Ship - Debit Request/Batch Id
11843 --    p_ship_debit_type : Request Type (SUPPLIER/INTERNAL)
11844 --    p_claim_number    : For SUPPLIER type ONLY
11845 --
11846 -- NOTE
11847 --   Functionally, each request/batch will create only one claim at a time.
11848 --   Although, this API is designed to create multiple claims for each S-D
11849 --   request/batch process.
11850 --
11851 -- HISTORY
11852 --   19-OCT-2007  psomyaju  Created.
11853 ---------------------------------------------------------------------
11854 
11855 PROCEDURE Initiate_SD_Payment(
11856    p_api_version         IN  NUMBER
11857   ,p_init_msg_list       IN  VARCHAR2  := FND_API.g_false
11858   ,p_commit              IN  VARCHAR2  := FND_API.g_false
11859   ,p_validation_level    IN  NUMBER    := FND_API.g_valid_level_full
11860   ,x_return_status       OUT NOCOPY VARCHAR2
11861   ,x_msg_count           OUT NOCOPY NUMBER
11862   ,x_msg_data            OUT NOCOPY VARCHAR2
11863   ,p_ship_debit_id       IN  NUMBER
11864   ,p_ship_debit_type     IN  VARCHAR2
11865   ,x_claim_id            OUT NOCOPY NUMBER
11866 )
11867 IS
11868 l_api_version                   CONSTANT NUMBER       := 1.0;
11869 l_api_name                      CONSTANT VARCHAR2(30) := 'Initiate_SD_Payment';
11870 l_full_name                     CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
11871 l_return_status                 VARCHAR2(1);
11872 
11873 l_cust_account_id               NUMBER;
11874 l_product_id                    NUMBER;
11875 l_total_amount                  NUMBER   := 0;
11876 l_claim_id                      NUMBER;
11877 l_payment_method                VARCHAR2(30);
11878 l_cust_billto_acct_site_id      NUMBER;
11879 l_claim_line_id                 NUMBER;
11880 l_counter                       PLS_INTEGER := 1;
11881 l_error_index                   NUMBER;
11882 l_activity_type                 VARCHAR2(30);
11883 l_activity_id                   NUMBER;
11884 l_bill_to_site_id               NUMBER;
11885 l_product_level_type            VARCHAR2(30);
11886 l_amount                        NUMBER;
11887 l_amount_offr_curr              NUMBER;
11888 l_batch_id                      NUMBER;
11889 l_source_object_class           VARCHAR2(30);
11890 l_claim_number                  VARCHAR2(30);
11891 l_rec_num                       NUMBER := 1;
11892 l_check                         NUMBER := 0;
11893 l_batch_type                    VARCHAR2(30);
11894 l_resource_id                   NUMBER;
11895 l_access_id                     NUMBER;
11896 l_claim_currency_code           VARCHAR2(15);
11897 l_rate                          NUMBER;
11898 l_trans_currency_code           VARCHAR2(15);
11899 l_sd_org_id                     NUMBER;
11900 l_func_currency_code            VARCHAR2(15);
11901 l_acctd_amount                  NUMBER := 0;
11902 l_exchange_rate_type            VARCHAR2(30);
11903 l_conv_exc_rate                 NUMBER;
11904 
11905 --Vendor details added for bug 6921727
11906 l_vendor_id                     NUMBER;
11907 l_vendor_site_id                NUMBER;
11908 
11909 --Claim lines with/without grouping w.r.t. SDR/Product - Bugfix 7811671
11910 l_sd_claim_lines_grouping       VARCHAR2(30) := NVL(FND_PROFILE.VALUE('OZF_SD_CLAIM_LINES_GROUPING'),'Y');
11911 l_batch_line_id                 NUMBER;
11912 
11913 l_funds_util_flt                ozf_claim_accrual_pvt.funds_util_flt_type;
11914 l_claim_rec                     OZF_CLAIM_PVT.claim_rec_type;
11915 l_line_tbl                      OZF_CLAIM_LINE_PVT.claim_line_tbl_type;
11916 l_access_rec                    ams_access_pvt.access_rec_type;
11917 
11918 --User details added for bug 7578152
11919 l_resp_name                     VARCHAR2(100) := 'Oracle Trade Management User';
11920 l_appl_id                       NUMBER;
11921 l_resp_id                       NUMBER;
11922 l_user_id                       NUMBER;
11923 
11924 -- Fix for Bug 10047209
11925 l_create_claim VARCHAR2(1) := 'N';
11926 
11927 TYPE cust_util_rec_type IS RECORD
11928 ( cust_account_id   NUMBER
11929 , bill_to_site_id   NUMBER
11930 , claim_number      VARCHAR2(30)
11931 );
11932 
11933 TYPE cust_util_tbl_type is TABLE OF cust_util_rec_type
11934 INDEX BY BINARY_INTEGER;
11935 
11936 l_cust_util_tbl  cust_util_tbl_type;
11937 
11938 CURSOR csr_uom_code(cv_item_id IN NUMBER) IS
11939   SELECT  ms.primary_uom_code
11940   FROM    mtl_system_items ms
11941         , mtl_parameters mp
11942         , org_organization_definitions ood
11943   WHERE  ms.inventory_item_id = cv_item_id
11944     AND  ms.organization_id = mp.organization_id
11945     AND  mp.organization_id = mp.master_organization_Id
11946     AND  mp.organization_id = ood.organization_id
11947     AND  ood.operating_unit = MO_GLOBAL.GET_CURRENT_ORG_ID();
11948 
11949 CURSOR csr_req_uom_code ( cv_request_id IN NUMBER
11950                         , cv_product_id IN NUMBER
11951                         ) IS
11952   SELECT item_uom
11953   FROM   ozf_sd_request_lines_all
11954   WHERE  request_header_id = cv_request_id
11955     AND  inventory_item_id = cv_product_id;
11956 
11957 CURSOR csr_batch_uom_code ( cv_batch_id IN NUMBER
11958                           , cv_product_id IN NUMBER
11959                           ) IS
11960   SELECT shipped_quantity_uom
11961   FROM   ozf_sd_batch_lines_all
11962   WHERE  batch_id = cv_batch_id
11963     AND  item_id = cv_product_id;
11964 
11965 --Bugfix:7169388
11966 --For associate earnings, claim amount and not functional amount
11967 --should be use.
11968 --Bugfix:7231613
11969 --For associate earnings, accrual currency must be passed. Derived
11970 --it from budget currency as for Ship-Debit, offer sourced from
11971 --single budget retrieved from profile.
11972 /* ER 9226258
11973 CURSOR csr_claim_lines(cv_claim_id IN NUMBER) IS
11974   SELECT claim_line_id
11975        , activity_type
11976        , activity_id
11977        , item_type
11978        , item_id
11979 --     , acctd_amount
11980        , amount                             --7169388
11981        , fund.approved_in_currency          --7231613  --//SD MC
11982   FROM   ozf_claim_lines_all lines
11983        , ozf_offers offr
11984        , ozf_act_budgets fund
11985        , ozf_sd_request_headers_all_b req
11986   WHERE  claim_id = cv_claim_id
11987     AND  lines.activity_id = req.request_header_id
11988     AND  req.offer_id = offr.qp_list_header_id
11989     AND  offr.qp_list_header_id = fund.act_budget_used_by_id
11990     AND  arc_act_budget_used_by = 'OFFR'
11991     AND  transfer_type = 'REQUEST';
11992 */
11993 --// ER 9226258 - Modified
11994 CURSOR csr_claim_lines(cv_claim_id IN NUMBER) IS
11995   SELECT lines.claim_line_id
11996        , lines.activity_type
11997        , lines.activity_id
11998        , lines.item_type
11999        , lines.item_id
12000        , lines.amount
12001        , offr.transaction_currency_code
12002   FROM   ozf_claim_lines_all lines
12003        , ozf_offers offr
12004        , ozf_sd_request_headers_all_b req
12005   WHERE  claim_id = cv_claim_id
12006     AND  lines.activity_id = req.request_header_id
12007     AND  req.offer_id = offr.qp_list_header_id;
12008 
12009 --Added created_by for bug 7578152
12010 CURSOR csr_request_currency(cv_request_id IN NUMBER) IS
12011   SELECT request_currency_code, org_id, created_by
12012   FROM   ozf_sd_request_headers_all_b
12013   WHERE  request_header_id = cv_request_id;
12014 
12015 CURSOR csr_request_header(cv_request_id IN NUMBER) IS
12016   SELECT  DISTINCT
12017           util.cust_account_id
12018         , util.bill_to_site_use_id
12019   FROM    ozf_funds_utilized_all_b util
12020   WHERE   util.reference_id = cv_request_id;
12021 
12022 --RMA (-ive accruals) support for bugfix: 6913855
12023 CURSOR csr_request_lines(cv_request_id IN NUMBER
12024                         ,cv_cust_account_id IN NUMBER
12025                         ,cv_bill_to_site_id IN NUMBER) IS
12026   SELECT  cust_account_id
12027         , reference_type
12028         , reference_id
12029         , bill_to_site_use_id
12030         , product_level_type
12031         , product_id
12032         , exchange_rate_type
12033         , SUM(plan_curr_amount_remaining)
12034         , SUM(acctd_amount_remaining) --// ER 9226258
12035   FROM    ozf_funds_utilized_all_b
12036   WHERE   reference_id = cv_request_id
12037     AND   cust_account_id = cv_cust_account_id
12038     AND   NVL(bill_to_site_use_id,1) = NVL(cv_bill_to_site_id,1)
12039     AND   reference_type = 'SD_REQUEST'
12040     AND   gl_posted_flag = 'Y'
12041   GROUP BY
12042           cust_account_id
12043         , reference_type
12044         , reference_id
12045         , bill_to_site_use_id
12046         , product_level_type
12047         , product_id
12048         , exchange_rate_type;
12049 
12050 CURSOR cur_offer_currency(cv_request_id IN NUMBER) IS
12051   SELECT  offr.transaction_currency_code
12052   FROM    ozf_sd_request_headers_all_b req
12053         , ozf_offers offr
12054   WHERE   offr.qp_list_header_id = req.offer_id
12055     AND   req.request_header_id = cv_request_id;
12056 
12057 --Vendor details added for bug 6921727
12058 --Added created_by for bug 7578152
12059 CURSOR csr_batch_currency(cv_batch_id IN NUMBER) IS
12060   SELECT currency_code, org_id, vendor_id, vendor_site_id, created_by
12061   FROM   ozf_sd_batch_headers_all
12062   WHERE  batch_id = cv_batch_id;
12063 
12064 CURSOR csr_batch_header(cv_batch_id IN NUMBER) IS
12065   SELECT  DISTINCT
12066           util.cust_account_id
12067         , util.bill_to_site_use_id
12068         , head.claim_number
12069   FROM    ozf_funds_utilized_all_b util
12070         , ozf_sd_batch_headers_all head
12071         , ozf_sd_batch_lines_all   line
12072   WHERE   head.batch_id = cv_batch_id
12073     AND   head.batch_id = line.batch_id
12074     --Fix for Bug 10047209
12075     --AND   line.status_code ='APPROVED' --//Bugfix: 9794989
12076     AND   line.purge_flag ='N'
12077     AND   util.utilization_id = line.utilization_id;
12078 
12079 CURSOR csr_batch_lines(cv_batch_id IN NUMBER
12080                       ,cv_cust_account_id IN NUMBER
12081                       ,cv_bill_to_site_id IN NUMBER) IS
12082   SELECT   util.cust_account_id
12083          , util.reference_type
12084          , util.reference_id
12085          , util.bill_to_site_use_id
12086          , util.product_level_type
12087          , util.product_id
12088          , sum(line.batch_curr_claim_amount)
12089   FROM     ozf_funds_utilized_all_b     util
12090          , ozf_sd_batch_lines_all       line
12091   WHERE    line.batch_id = cv_batch_id
12092     --Fix for Bug 10047209
12093     --AND    line.status_code ='APPROVED' --//Bugfix: 9794989
12094     AND    line.purge_flag ='N'
12095     AND    util.cust_account_id = cv_cust_account_id
12096     AND    NVL(util.bill_to_site_use_id,1) = NVL(cv_bill_to_site_id,1)
12097     AND    util.utilization_id = line.utilization_id
12098   GROUP BY cust_account_id
12099          , bill_to_site_use_id
12100          , reference_type
12101          , reference_id
12102          , product_level_type
12103          , product_id;
12104 
12105 --Added for bugfix 7811671
12106 CURSOR csr_batch_nongrp_lines(cv_batch_id IN NUMBER
12107                              ,cv_cust_account_id IN NUMBER
12108                              ,cv_bill_to_site_id IN NUMBER) IS
12109   SELECT   util.cust_account_id
12110          , util.reference_type
12111          , util.reference_id
12112          , util.bill_to_site_use_id
12113          , util.product_level_type
12114          , util.product_id
12115          , line.batch_curr_claim_amount
12116          , line.batch_line_id
12117   FROM     ozf_funds_utilized_all_b     util
12118          , ozf_sd_batch_lines_all       line
12119   WHERE    line.batch_id = cv_batch_id
12120      --Fix for Bug 10047209
12121     -- AND    line.status_code ='APPROVED' --//Bugfix: 9794989
12122     AND    line.purge_flag ='N'
12123     AND    util.cust_account_id = cv_cust_account_id
12124     AND    NVL(util.bill_to_site_use_id,1) = NVL(cv_bill_to_site_id,1)
12125     AND    util.utilization_id = line.utilization_id;
12126 
12127 CURSOR csr_access(cv_claim_id IN NUMBER) IS
12128   SELECT  resource_id
12129   FROM    ozf_sd_request_access req
12130         , ozf_claim_lines       line
12131         , ozf_claims            cla
12132   WHERE   cla.claim_id = cv_claim_id
12133     AND   cla.claim_id = line.claim_id
12134     AND   line.activity_id = req.request_header_id
12135     AND   req.enabled_flag = 'Y'
12136     AND   NOT EXISTS ( SELECT NULL
12137                        FROM   ams_act_access
12138                        WHERE  user_or_role_id = req.resource_id
12139                          AND  arc_user_or_role_type = 'USER'
12140                          AND  arc_act_access_to_object = 'CLAM'
12141                       );
12142 
12143 --Added csr_resp for bug 7578152
12144 CURSOR csr_resp IS
12145   SELECT application_id, responsibility_id
12146   FROM   fnd_responsibility_vl
12147   WHERE  responsibility_name = l_resp_name;
12148 
12149 --// ER 9226258 : to get the functional currency
12150 CURSOR csr_function_currency IS
12151   SELECT gs.currency_code
12152   FROM   gl_sets_of_books gs
12153        , ozf_sys_parameters org
12154   WHERE  org.set_of_books_id = gs.set_of_books_id
12155    AND  org.org_id = MO_GLOBAL.GET_CURRENT_ORG_ID();
12156 
12157  --Fix for Bug 10047209
12158 CURSOR csr_neg_batch_flag(cv_vendor_site_id IN NUMBER, cv_org_id IN NUMBER) IS
12159   SELECT supplier_neg_batch_flag
12160   FROM ozf_supp_trd_prfls_all
12161   WHERE supplier_site_id = cv_vendor_site_id
12162   AND org_id = cv_org_id;
12163 
12164   l_sup_neg_batch  VARCHAR2(5);
12165   l_sd_claim_open_status  VARCHAR2(30) := NVL(FND_PROFILE.VALUE('OZF_CRE_SSD_CLAIMS_OPEN_STATUS'),'N');
12166 
12167 BEGIN
12168   --------------------- initialize -----------------------
12169   SAVEPOINT Initiate_SD_Payment;
12170 
12171   IF OZF_DEBUG_HIGH_ON THEN
12172      OZF_Utility_PVT.debug_message(l_full_name||': start');
12173      OZF_Utility_PVT.debug_message('p_ship_debit_id : '||p_ship_debit_id);
12174      OZF_Utility_PVT.debug_message('p_ship_debit_type : '||p_ship_debit_type);
12175   END IF;
12176 
12177   IF FND_API.to_boolean(p_init_msg_list) THEN
12178     FND_MSG_PUB.initialize;
12179   END IF;
12180 
12181   IF NOT FND_API.compatible_api_call(
12182          l_api_version,
12183          p_api_version,
12184          l_api_name,
12185          g_pkg_name
12186   ) THEN
12187     RAISE FND_API.g_exc_unexpected_error;
12188   END IF;
12189 
12190   x_return_status := FND_API.g_ret_sts_success;
12191 
12192   ----------------- Process batch ----------------
12193 
12194   IF p_ship_debit_id IS NULL OR p_ship_debit_type IS NULL THEN
12195     RETURN;
12196   END IF;
12197 
12198   --Added csr_resp for bug 7578152
12199   OPEN  csr_resp;
12200   FETCH csr_resp INTO l_appl_id, l_resp_id;
12201   CLOSE csr_resp;
12202 
12203   IF p_ship_debit_type = 'INTERNAL' THEN
12204 
12205   --Defaulting values for INTERNAL claim
12206         l_payment_method := 'ACCOUNTING_ONLY';
12207         l_source_object_class := 'SD_INTERNAL';
12208         l_batch_id := NULL;
12209         l_batch_type := NULL;
12210 
12211   --For INTERNAL claims, claim currency will be request header currency
12212   --Added l_user_id for bug 7578152
12213         OPEN  csr_request_currency(p_ship_debit_id);
12214         FETCH csr_request_currency INTO l_claim_currency_code, l_sd_org_id, l_user_id;
12215         CLOSE csr_request_currency;
12216 
12217   --For supplied request, get customer account/bill to site combinations of
12218   --qualified accruals
12219         IF OZF_DEBUG_HIGH_ON THEN
12220           OZF_Utility_PVT.debug_message('bill_to_site_id duplicate check verification on following cust_account_id/bill_to_site_id');
12221         END IF;
12222 
12223         OPEN csr_request_header(p_ship_debit_id);
12224         LOOP
12225          FETCH csr_request_header INTO  l_cust_util_tbl(l_counter).cust_account_id
12226                                        ,l_cust_util_tbl(l_counter).bill_to_site_id;
12227 
12228          EXIT WHEN csr_request_header%NOTFOUND;
12229 
12230          IF OZF_DEBUG_HIGH_ON THEN
12231             OZF_Utility_PVT.debug_message('cust_account_id : '||l_cust_util_tbl(l_counter).cust_account_id);
12232             OZF_Utility_PVT.debug_message('bill_to_site_id : '||l_cust_util_tbl(l_counter).bill_to_site_id);
12233          END IF;
12234 
12235          l_counter := l_counter + 1;
12236         END LOOP;
12237         CLOSE csr_request_header;
12238 
12239   ELSIF p_ship_debit_type = 'SUPPLIER' THEN
12240 
12241   --Default values for SUPPLIER claim
12242         -- fix for bug 10047209
12243 	/*l_payment_method := 'AP_DEBIT';
12244         l_source_object_class := 'SD_SUPPLIER';
12245         l_batch_id := p_ship_debit_id;
12246         l_batch_type := 'SD_BATCH';
12247         */
12248   --For SUPPLIER claims, claim currency will be batch header currency
12249   --Vendor details added for bug 6921727
12250   --Added l_user_id for bug 7578152
12251         OPEN  csr_batch_currency(p_ship_debit_id);
12252         FETCH csr_batch_currency INTO l_claim_currency_code, l_sd_org_id, l_vendor_id, l_vendor_site_id, l_user_id;
12253         CLOSE csr_batch_currency;
12254 
12255   --For supplied batch_id, get customer accounts, respective bill to site id and
12256   --claim number defined in batch header. If claim number doesn't exists, system
12257   --will use default claim number generation mechanism.
12258 
12259         IF OZF_DEBUG_HIGH_ON THEN
12260           OZF_Utility_PVT.debug_message('bill_to_site_id duplicate check verification on following cust_account_id/bill_to_site_id');
12261         END IF;
12262 
12263         OPEN csr_batch_header(p_ship_debit_id);
12264         LOOP
12265          FETCH csr_batch_header INTO  l_cust_util_tbl(l_counter).cust_account_id
12266                                     , l_cust_util_tbl(l_counter).bill_to_site_id
12267                                     , l_cust_util_tbl(l_counter).claim_number;
12268          EXIT WHEN csr_batch_header%NOTFOUND;
12269 
12270          IF OZF_DEBUG_HIGH_ON THEN
12271             OZF_Utility_PVT.debug_message('cust_account_id : '||l_cust_util_tbl(l_counter).cust_account_id);
12272             OZF_Utility_PVT.debug_message('bill_to_site_id : '||l_cust_util_tbl(l_counter).bill_to_site_id);
12273             OZF_Utility_PVT.debug_message('claim_number : '||l_cust_util_tbl(l_counter).claim_number);
12274          END IF;
12275 
12276          l_counter := l_counter + 1;
12277         END LOOP;
12278         CLOSE csr_batch_header;
12279 
12280   END IF;
12281 
12282   --Initialization of organization context
12283   MO_GLOBAL.init('OZF');
12284   MO_GLOBAL.set_policy_context('S', l_sd_org_id);
12285 
12286   -- Initialization added for bug 7578152
12287   IF Nvl(fnd_global.user_id,-1) IN (0,-1) THEN
12288     FND_GLOBAL.APPS_INITIALIZE(l_user_id, l_resp_id, l_appl_id);
12289   END IF;
12290 
12291    -- Fix for Bug 10047209
12292   OPEN csr_neg_batch_flag(l_vendor_site_id,l_sd_org_id);
12293   FETCH csr_neg_batch_flag INTO l_sup_neg_batch;
12294   CLOSE csr_neg_batch_flag;
12295 
12296    IF OZF_DEBUG_HIGH_ON THEN
12297       OZF_Utility_PVT.debug_message('-ve batch flag : '||l_sup_neg_batch);
12298    END IF;
12299 
12300   OZF_Utility_PVT.debug_message('Org Id : OZF_CLAIM_ACCRUAL_PVT - '||MO_GLOBAL.GET_CURRENT_ORG_ID());
12301   OZF_Utility_PVT.debug_message('User Id : OZF_CLAIM_ACCRUAL_PVT - '||fnd_global.user_id);
12302 
12303   IF OZF_DEBUG_HIGH_ON THEN
12304         OZF_Utility_PVT.debug_message('l_payment_method : '||l_payment_method);
12305         OZF_Utility_PVT.debug_message('l_source_object_class : '||l_source_object_class);
12306         OZF_Utility_PVT.debug_message('l_batch_id : '||l_batch_id);
12307         OZF_Utility_PVT.debug_message('l_batch_type : '||l_batch_type);
12308         OZF_Utility_PVT.debug_message('l_claim_currency_code : '||l_claim_currency_code);
12309   END IF;
12310 
12311   l_counter := 1;
12312 
12313  --If customer accounts have multiple bill to sites, then process will be aborted.
12314  --Since, INTERNAL claims always have 1:1 mapping with customer accounts and bill
12315  --to sites, so below validation will fail only for SUPPLIER claims.
12316   IF l_cust_util_tbl.COUNT > 1 THEN
12317      FOR i IN l_cust_util_tbl.FIRST..l_cust_util_tbl.LAST
12318      LOOP
12319         l_cust_account_id := l_cust_util_tbl(i).cust_account_id;
12320         l_check := 0;
12321         FOR j IN l_cust_util_tbl.FIRST..l_cust_util_tbl.LAST
12322         LOOP
12323            IF l_cust_account_id = l_cust_util_tbl(j).cust_account_id THEN
12324               l_check := l_check + 1;
12325            END IF;
12326         END LOOP;
12327         IF l_check > 1 THEN
12328            IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
12329               FND_MESSAGE.Set_Name('OZF', 'OZF_DUP_CUST_ACCTS');
12330               FND_MESSAGE.Set_Token('CUST_ACCOUNT_ID', l_cust_util_tbl(i).cust_account_id);
12331               FND_MSG_PUB.ADD;
12332             END IF;
12333             RAISE FND_API.g_exc_unexpected_error;
12334         END IF;
12335      END LOOP;
12336   END IF;
12337 
12338   --Create claims for cust_account_id and bill_to_site_id
12339   IF OZF_DEBUG_HIGH_ON THEN
12340      OZF_Utility_PVT.debug_message('bill_to_site_id duplicate check passed: Creating claims for each cust_account_id/bill_to_site_id');
12341   END IF;
12342 
12343   IF l_cust_util_tbl.COUNT > 0 THEN
12344     FOR i IN l_cust_util_tbl.FIRST..l_cust_util_tbl.LAST
12345     LOOP
12346       l_cust_account_id   := l_cust_util_tbl(i).cust_account_id;
12347       l_bill_to_site_id   := l_cust_util_tbl(i).bill_to_site_id;
12348       l_claim_number      := l_cust_util_tbl(i).claim_number;
12349 
12350       IF OZF_DEBUG_HIGH_ON THEN
12351           OZF_Utility_PVT.debug_message('l_cust_account_id : '||l_cust_account_id);
12352           OZF_Utility_PVT.debug_message('l_bill_to_site_id : '||l_bill_to_site_id);
12353           OZF_Utility_PVT.debug_message('l_claim_number : '||l_claim_number);
12354       END IF;
12355 
12356       IF p_ship_debit_type = 'INTERNAL' THEN
12357          --Get all accurals which are qualifying for supplied request and
12358          --customer account and bill to sites combinations.
12359          OPEN  csr_request_lines(p_ship_debit_id
12360                                 ,l_cust_account_id
12361                                 ,l_bill_to_site_id);
12362 
12363       ELSIF p_ship_debit_type = 'SUPPLIER' THEN
12364          --Get all accruals which are qualifying for supplied batch and
12365          --customer account and bill to sites combinations.
12366          --l_sd_claim_lines_grouping check added for bugfix 7811671
12367          IF l_sd_claim_lines_grouping = 'Y' THEN
12368          OPEN  csr_batch_lines(p_ship_debit_id
12369                               ,l_cust_account_id
12370                               ,l_bill_to_site_id);
12371          ELSIF l_sd_claim_lines_grouping = 'N' THEN
12372            OPEN  csr_batch_nongrp_lines(p_ship_debit_id
12373                                        ,l_cust_account_id
12374                                        ,l_bill_to_site_id);
12375          END IF;
12376       END IF;
12377 
12378       LOOP
12379       IF  p_ship_debit_type = 'INTERNAL' THEN
12380             FETCH csr_request_lines INTO l_cust_account_id
12381                                        , l_activity_type
12382                                        , l_activity_id
12383                                        , l_bill_to_site_id
12384                                        , l_product_level_type
12385                                        , l_product_id
12386                                        , l_exchange_rate_type
12387                                        , l_amount
12388                                        , l_acctd_amount;  --// ER 9226258
12389 
12390             EXIT WHEN csr_request_lines%NOTFOUND;
12391       ELSIF p_ship_debit_type = 'SUPPLIER' THEN
12392         IF l_sd_claim_lines_grouping = 'Y' THEN
12393             FETCH csr_batch_lines INTO   l_cust_account_id
12394                                        , l_activity_type
12395                                        , l_activity_id
12396                                        , l_bill_to_site_id
12397                                        , l_product_level_type
12398                                        , l_product_id
12399                                        , l_amount;
12400 
12401             EXIT WHEN csr_batch_lines%NOTFOUND;
12402         ELSIF l_sd_claim_lines_grouping = 'N' THEN
12403             FETCH csr_batch_nongrp_lines INTO   l_cust_account_id
12404                                               , l_activity_type
12405                                               , l_activity_id
12406                                               , l_bill_to_site_id
12407                                               , l_product_level_type
12408                                               , l_product_id
12409                                               , l_amount
12410                                               , l_batch_line_id;
12411 
12412             EXIT WHEN csr_batch_nongrp_lines%NOTFOUND;
12413         END IF;
12414       END IF;
12415 
12416      IF OZF_DEBUG_HIGH_ON THEN
12417           OZF_Utility_PVT.debug_message(' l_cust_account_id : '||l_cust_account_id);
12418           OZF_Utility_PVT.debug_message(' l_activity_type : '||l_activity_type);
12419           OZF_Utility_PVT.debug_message(' l_activity_id : '||l_activity_id);
12420           OZF_Utility_PVT.debug_message(' l_bill_to_site_id : '||l_bill_to_site_id);
12421           OZF_Utility_PVT.debug_message(' l_product_level_type : '||l_product_level_type);
12422           OZF_Utility_PVT.debug_message(' l_product_id : '||l_product_id);
12423           OZF_Utility_PVT.debug_message(' l_amount : '||l_amount);
12424           OZF_Utility_PVT.debug_message(' l_batch_line_id : '||l_batch_line_id);
12425      END IF;
12426 
12427       --If claim currency and accrual(offer) currency is not same, then convert
12428       --offer accounted amount from functional currency to claim currency.
12429       --This case is applicable only for INTERNAL claims as for SUPPLIER claims
12430       --amount in claim (batch header) currency retrieved.
12431       IF p_ship_debit_type = 'INTERNAL' THEN
12432         OPEN cur_offer_currency(p_ship_debit_id);
12433         FETCH cur_offer_currency INTO l_trans_currency_code;
12434         CLOSE cur_offer_currency;
12435 
12436         --// ER 9226258 : Get Functional Currency code
12437         OPEN  csr_function_currency;
12438         FETCH csr_function_currency INTO l_func_currency_code;
12439         CLOSE csr_function_currency;
12440 
12441 
12442         IF OZF_DEBUG_HIGH_ON THEN
12443            OZF_Utility_PVT.debug_message('Starts Currency Conversion for INTERNAL Claims ');
12444            OZF_Utility_PVT.debug_message(' l_trans_currency_code  : '|| l_trans_currency_code);
12445            OZF_Utility_PVT.debug_message(' l_claim_currency_code : '|| l_claim_currency_code);
12446            OZF_Utility_PVT.debug_message(' l_func_currency_code  : '|| l_func_currency_code);
12447         END IF;
12448 
12449         IF (l_trans_currency_code <> l_claim_currency_code) THEN
12450            IF (l_claim_currency_code = l_func_currency_code) THEN
12451               l_amount := l_acctd_amount;
12452            ELSE
12453                OZF_UTILITY_PVT.Convert_Currency(
12454                          p_from_currency   => l_func_currency_code
12455                         ,p_to_currency     => l_claim_currency_code
12456                         ,p_conv_type       => l_exchange_rate_type
12457                         ,p_conv_date       => SYSDATE
12458                         ,p_from_amount     => l_acctd_amount
12459                         ,x_return_status   => l_return_status
12460                         ,x_to_amount       => l_amount
12461                         ,x_rate            => l_conv_exc_rate
12462                       );
12463 
12464                IF l_return_status = FND_API.g_ret_sts_error THEN
12465                   RAISE FND_API.g_exc_error;
12466                ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
12467                   RAISE FND_API.g_exc_unexpected_error;
12468                END IF;
12469            END IF;
12470         END IF;
12471       END IF;
12472         /*
12473         IF l_trans_currency_code <> l_claim_currency_code THEN
12474             l_amount_offr_curr := l_amount;
12475             OZF_UTILITY_PVT.Convert_Currency(
12476                  p_from_currency   => l_trans_currency_code
12477                 ,p_to_currency     => l_claim_currency_code
12478                 ,p_conv_date       => SYSDATE   --SD MC
12479                 ,p_from_amount     => l_amount_offr_curr
12480                 ,x_return_status   => l_return_status
12481                 ,x_to_amount       => l_amount
12482             );
12483             IF l_return_status = FND_API.g_ret_sts_error THEN
12484               RAISE FND_API.g_exc_error;
12485             ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
12486               RAISE FND_API.g_exc_unexpected_error;
12487             END IF;
12488         END IF;
12489         */
12490 
12491 
12492       --Ship-Debit doesn't require Prorate Earnings.
12493       --Setting line level attributes w.r.t. accrual details
12494       l_line_tbl(l_counter).prorate_earnings_flag     := 'F';
12495       l_line_tbl(l_counter).claim_currency_amount     := l_amount;
12496       l_line_tbl(l_counter).activity_type             := l_activity_type;
12497       l_line_tbl(l_counter).activity_id               := l_activity_id;
12498       l_line_tbl(l_counter).item_type                 := l_product_level_type;
12499       l_line_tbl(l_counter).item_id                   := l_product_id;
12500       l_line_tbl(l_counter).batch_line_id             := l_batch_line_id;
12501 
12502       --If product UOM is not defined then get primary UOM of product.
12503       IF l_product_level_type = 'PRODUCT' AND l_product_id IS NOT NULL THEN
12504         IF p_ship_debit_type = 'INTERNAL' THEN
12505           OPEN  csr_req_uom_code(p_ship_debit_id,l_line_tbl(l_counter).item_id);
12506           FETCH csr_req_uom_code INTO l_line_tbl(l_counter).quantity_uom;
12507           CLOSE csr_req_uom_code;
12508         ELSIF p_ship_debit_type = 'SUPPLIER' THEN
12509           OPEN  csr_batch_uom_code(p_ship_debit_id,l_line_tbl(l_counter).item_id);
12510           FETCH csr_batch_uom_code INTO l_line_tbl(l_counter).quantity_uom;
12511           CLOSE csr_batch_uom_code;
12512         END IF;
12513 
12514         IF l_line_tbl(l_counter).quantity_uom IS NULL THEN
12515             OPEN csr_uom_code(l_line_tbl(l_counter).item_id);
12516             FETCH csr_uom_code INTO l_line_tbl(l_counter).quantity_uom;
12517             IF csr_uom_code%NOTFOUND THEN
12518               CLOSE csr_uom_code;
12519               IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
12520                 FND_MESSAGE.Set_Name('OZF', 'OZF_PRODUCT_UOM_MISSING');
12521                 FND_MESSAGE.Set_Token('ITEM_ID', l_line_tbl(l_counter).item_id);
12522                 FND_MSG_PUB.ADD;
12523               END IF;
12524               RAISE FND_API.g_exc_unexpected_error;
12525             END IF;
12526             CLOSE csr_uom_code;
12527         END IF;
12528       END IF;
12529 
12530       --Calculate claim header amount in claim currency
12531       l_total_amount := l_total_amount + l_amount;
12532       l_counter := l_counter + 1;
12533 
12534       END LOOP;
12535 
12536       --Raise error, if no accrual exists, may happen for INTERNAL claims only
12537       IF l_counter = 1 THEN
12538          IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
12539             FND_MESSAGE.Set_Name('OZF', 'OZF_ACCRUAL_NOT_EXISTS');
12540             FND_MSG_PUB.ADD;
12541           END IF;
12542           RAISE FND_API.g_exc_unexpected_error;
12543       END IF;
12544 
12545       IF p_ship_debit_type = 'INTERNAL' THEN
12546         CLOSE csr_request_lines;
12547       ELSIF p_ship_debit_type = 'SUPPLIER' THEN
12548         IF l_sd_claim_lines_grouping = 'Y' THEN
12549         CLOSE csr_batch_lines;
12550         ELSIF l_sd_claim_lines_grouping = 'N' THEN
12551           CLOSE csr_batch_nongrp_lines;
12552         END IF;
12553       END IF;
12554 
12555       --kpatro for bug 10047209
12556       IF(p_ship_debit_type = 'SUPPLIER') THEN
12557 	 --kdass bug 12916663 - If profile value OZF_CRE_SSD_CLAIMS_OPEN_STATUS is No, then default settlement method
12558          IF l_sd_claim_open_status = 'N' THEN
12559             IF (l_total_amount <0)THEN
12560                l_payment_method := 'AP_DEFAULT';
12561             ELSIF(l_total_amount >0) THEN
12562                l_payment_method := 'AP_DEBIT';
12563             END IF;
12564 	 END IF;
12565            l_source_object_class := 'SD_SUPPLIER';
12566            l_batch_id := p_ship_debit_id;
12567            l_batch_type := 'SD_BATCH';
12568 	END IF;
12569 
12570 
12571       --If multiple claims are created then claim number of batch will be used for first claim only.
12572       IF l_rec_num > 1 THEN
12573         l_claim_number := NULL;
12574       END IF;
12575 
12576       --Setting values for claim header attributes
12577       --Vendor details added for bug 6921727
12578       l_claim_rec.cust_account_id          := l_cust_account_id;
12579       l_claim_rec.claim_class              := 'CLAIM';
12580       l_claim_rec.source_object_class      := l_source_object_class;
12581       l_claim_rec.source_object_id         := p_ship_debit_id;
12582       l_claim_rec.batch_id                 := l_batch_id;
12583       l_claim_rec.batch_type               := l_batch_type;
12584       l_claim_rec.currency_code            := l_claim_currency_code;
12585       l_claim_rec.amount                   := l_total_amount;
12586       l_claim_rec.payment_method           := l_payment_method;
12587       l_claim_rec.cust_billto_acct_site_id := l_cust_billto_acct_site_id;
12588       l_claim_rec.status_code              := 'OPEN';
12589       l_claim_rec.claim_number             := l_claim_number;
12590       l_claim_rec.vendor_id                := l_vendor_id;
12591       l_claim_rec.vendor_site_id           := l_vendor_site_id;
12592       l_claim_rec.org_id                   := l_sd_org_id;
12593       l_claim_rec.user_status_id           := to_number(
12594                                               ozf_utility_pvt.get_default_user_status(
12595                                               p_status_type   => 'OZF_CLAIM_STATUS',
12596                                               p_status_code   => l_claim_rec.status_code));
12597 
12598       IF OZF_DEBUG_HIGH_ON THEN
12599           OZF_Utility_PVT.debug_message('Claim header information:');
12600           OZF_Utility_PVT.debug_message('l_cust_account_id : '||l_claim_rec.cust_account_id);
12601           OZF_Utility_PVT.debug_message('claim_class : '||l_claim_rec.claim_class);
12602           OZF_Utility_PVT.debug_message('source_object_class : '||l_claim_rec.source_object_class);
12603           OZF_Utility_PVT.debug_message('source_object_number : '||l_claim_rec.source_object_number);
12604           OZF_Utility_PVT.debug_message('batch_id : '||l_claim_rec.batch_id);
12605           OZF_Utility_PVT.debug_message('batch_type : '||l_claim_rec.batch_type);
12606           OZF_Utility_PVT.debug_message('currency_code : '||l_claim_rec.currency_code);
12607           OZF_Utility_PVT.debug_message('amount : '||l_claim_rec.amount);
12608           OZF_Utility_PVT.debug_message('payment_method : '||l_claim_rec.payment_method);
12609           OZF_Utility_PVT.debug_message('cust_billto_acct_site_id : '||l_claim_rec.cust_billto_acct_site_id);
12610           OZF_Utility_PVT.debug_message('status_code : '||l_claim_rec.status_code);
12611           OZF_Utility_PVT.debug_message('claim_number : '||l_claim_rec.claim_number);
12612           OZF_Utility_PVT.debug_message('vendor_id : '||l_claim_rec.vendor_id);
12613           OZF_Utility_PVT.debug_message('vendor_site_id : '||l_claim_rec.vendor_site_id);
12614           OZF_Utility_PVT.debug_message('org_id : '||l_claim_rec.org_id);
12615           OZF_Utility_PVT.debug_message('user_status_id : '||l_claim_rec.user_status_id);
12616       END IF;
12617 
12618 
12619       --For INTERNAL negative claims, claim class need to be changed.
12620       /*IF p_ship_debit_type = 'INTERNAL' and l_claim_rec.amount < 0 THEN
12621           l_claim_rec.claim_class := 'CHARGE';
12622       END IF;
12623       */
12624       --kpatro for bug 10047209
12625       IF l_claim_rec.amount < 0 THEN
12626           l_claim_rec.claim_class := 'CHARGE';
12627       END IF;
12628 
12629               --kpatro for bug 10047209
12630         IF (p_ship_debit_type IN ('SUPPLIER','INTERNAL') and l_claim_rec.amount <> 0) THEN
12631 
12632             IF (p_ship_debit_type = 'SUPPLIER' AND l_claim_rec.amount < 0) THEN
12633 
12634 		 FND_FILE.PUT_LINE(FND_FILE.LOG, 'p_ship_debit_type = ' || p_ship_debit_type);
12635 	         FND_FILE.PUT_LINE(FND_FILE.LOG, 'l_claim_rec.amount = ' || l_claim_rec.amount);
12636 		 FND_FILE.PUT_LINE(FND_FILE.LOG, 'l_sup_neg_batch = ' || l_sup_neg_batch);
12637 		 FND_FILE.PUT_LINE(FND_FILE.LOG, 'l_create_claim11 = ' || l_create_claim);
12638 
12639 		IF (l_sup_neg_batch is null) THEN
12640 		   IF(nvl(fnd_profile.value('OZF_NEG_BATCH_CREATION'),'N') = 'Y') THEN
12641 		     l_create_claim := 'Y';
12642 		     -- Fix for Bug 10047209
12643                    ELSE
12644 		     l_create_claim := 'N';
12645 		   END IF;
12646 		ELSIF(l_sup_neg_batch = 'YES') THEN
12647 		    l_create_claim := 'Y';
12648 		    FND_FILE.PUT_LINE(FND_FILE.LOG, 'l_create_claim22 = ' || l_create_claim);
12649 		ELSE
12650 		    l_create_claim := 'N';
12651 		   --Raise error, if claim amount is negative.
12652 		    IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
12653                        FND_MESSAGE.Set_Name('OZF', 'OZF_CLAIM_AMOUNT_NEGATIVE');
12654                        FND_MSG_PUB.ADD;
12655                     END IF;
12656                    RAISE FND_API.g_exc_unexpected_error;
12657 
12658 		END IF;
12659 	    -- Fix for Bug 10047209
12660             ELSIF(p_ship_debit_type IN ('SUPPLIER','INTERNAL') and l_claim_rec.amount > 0) THEN
12661 	       	  l_create_claim := 'Y';
12662 	    END IF;
12663 
12664 	   IF (l_create_claim = 'Y') THEN
12665 	    --Claim header creation
12666             OZF_CLAIM_PVT.Create_Claim(
12667                  p_api_version            => l_api_version
12668                 ,x_return_status          => l_return_status
12669                 ,x_msg_data               => x_msg_data
12670                 ,x_msg_count              => x_msg_count
12671                 ,p_claim                  => l_claim_rec
12672                 ,x_claim_id               => l_claim_id
12673                 );
12674 
12675             IF l_return_status = FND_API.G_RET_STS_ERROR THEN
12676                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12677             ELSIF l_return_status = FND_API.G_RET_STS_UNEXP_ERROR THEN
12678                 RAISE FND_API.G_EXC_UNEXPECTED_ERROR;
12679             END IF;
12680 
12681             IF l_rec_num = 1 THEN
12682               x_claim_id := l_claim_id;
12683             END IF;
12684 
12685             IF OZF_DEBUG_HIGH_ON THEN
12686               OZF_Utility_PVT.debug_message('Created claim id:'||l_claim_id);
12687             END IF;
12688 
12689             FOR i IN l_line_tbl.FIRST..l_line_tbl.LAST
12690             LOOP
12691               IF l_line_tbl.exists(i) IS NOT NULL THEN
12692                 l_line_tbl(i).claim_id := l_claim_id;
12693               END IF;
12694             END LOOP;
12695 
12696             --Claim line creation
12697             OZF_CLAIM_LINE_PVT.Create_Claim_Line_Tbl(
12698                  p_api_version       => 1.0
12699                 ,p_init_msg_list     => FND_API.g_false
12700                 ,p_commit            => FND_API.g_false
12701                 ,p_validation_level  => FND_API.g_valid_level_full
12702                 ,x_return_status     => l_return_status
12703                 ,x_msg_count         => x_msg_count
12704                 ,x_msg_data          => x_msg_data
12705                 ,p_claim_line_tbl    => l_line_tbl
12706                 ,x_error_index       => l_error_index
12707                 );
12708 
12709             IF l_return_status = FND_API.g_ret_sts_error THEN
12710                 RAISE FND_API.g_exc_error;
12711             ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
12712                 RAISE FND_API.g_exc_error;
12713             END IF;
12714 
12715             IF OZF_DEBUG_HIGH_ON THEN
12716               OZF_Utility_PVT.debug_message('Claim lines created for claim_id=' || l_claim_id);
12717             END IF;
12718 
12719             OPEN csr_claim_lines(l_claim_id);
12720             LOOP
12721               FETCH csr_claim_lines INTO l_funds_util_flt.claim_line_id
12722                                        , l_funds_util_flt.activity_type
12723                                        , l_funds_util_flt.activity_id
12724                                        , l_funds_util_flt.product_level_type
12725                                        , l_funds_util_flt.product_id
12726                                        , l_funds_util_flt.total_amount
12727                                        , l_funds_util_flt.utiz_currency_code;  --7231613
12728               EXIT WHEN csr_claim_lines%NOTFOUND;
12729 
12730               --Claim Line/Utilizations association
12731               Update_Group_Line_Util(
12732                      p_api_version            => l_api_version
12733                     ,p_init_msg_list          => FND_API.g_false
12734                     ,p_commit                 => FND_API.g_false
12735                     ,p_validation_level       => p_validation_level
12736                     ,x_return_status          => l_return_status
12737                     ,x_msg_count              => x_msg_count
12738                     ,x_msg_data               => x_msg_data
12739                     ,p_summary_view           => 'ACTIVITY'
12740                     ,p_funds_util_flt         => l_funds_util_flt
12741                     ,p_mode                   => OZF_CLAIM_UTILITY_PVT.g_auto_mode
12742                     );
12743 
12744               IF l_return_status = FND_API.g_ret_sts_error THEN
12745                 RAISE FND_API.g_exc_error;
12746               ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
12747                 RAISE FND_API.g_exc_error;
12748               END IF;
12749 
12750             END LOOP;
12751             CLOSE csr_claim_lines;
12752 
12753 	    --kdass bug 12916663 - If profile value OZF_CRE_SSD_CLAIMS_OPEN_STATUS is No, then initiate claim for settlement
12754             IF l_sd_claim_open_status = 'N' THEN
12755             --Settle claim using settlement method as ACCOUNTING_ONLY/AP_DEBIT as per case.
12756             Settle_Claim(
12757                      p_claim_id            => l_claim_id
12758                     ,x_return_status       => l_return_status
12759                     ,x_msg_count           => x_msg_count
12760                     ,x_msg_data            => x_msg_data
12761                     );
12762 
12763 
12764             IF l_return_status =  fnd_api.g_ret_sts_error THEN
12765                 RAISE FND_API.g_exc_error;
12766             ELSIF l_return_status = fnd_api.g_ret_sts_unexp_error THEN
12767                 RAISE FND_API.g_exc_unexpected_error;
12768             END IF;
12769 	  END IF; --IF l_sd_claim_open_status = 'N' THEN
12770 	END IF; -- l_create_claim := 'Y';
12771        ELSIF l_claim_rec.amount = 0 THEN
12772           --Raise error, if claim amount is zero. Claim header is not allowed with
12773           --zero amount.
12774           IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
12775             FND_MESSAGE.Set_Name('OZF', 'OZF_EARN_AVAIL_AMT_ZERO');
12776             FND_MSG_PUB.ADD;
12777           END IF;
12778           RAISE FND_API.g_exc_unexpected_error;
12779        /*ELSE
12780           --Raise error, if claim amount is negative.
12781           IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
12782             FND_MESSAGE.Set_Name('OZF', 'OZF_CLAIM_AMOUNT_NEGATIVE');
12783             FND_MSG_PUB.ADD;
12784           END IF;
12785           RAISE FND_API.g_exc_unexpected_error;
12786 	  */
12787        END IF;
12788 
12789        --Set Claim Access
12790        OPEN  csr_access(l_claim_id);
12791        LOOP
12792          FETCH csr_access INTO l_resource_id;
12793          EXIT WHEN csr_access%NOTFOUND;
12794          l_access_rec.user_or_role_id := l_resource_id;
12795          l_access_rec.arc_user_or_role_type := 'USER';
12796          l_access_rec.act_access_to_object_id := l_claim_id;
12797          l_access_rec.arc_act_access_to_object := 'CLAM';
12798 
12799          IF OZF_DEBUG_HIGH_ON THEN
12800               OZF_Utility_PVT.debug_message('Claim access information:');
12801               OZF_Utility_PVT.debug_message('user_or_role_id : '||l_access_rec.user_or_role_id);
12802               OZF_Utility_PVT.debug_message('arc_user_or_role_type : '||l_access_rec.arc_user_or_role_type);
12803               OZF_Utility_PVT.debug_message('act_access_to_object_id : '||l_access_rec.act_access_to_object_id);
12804               OZF_Utility_PVT.debug_message('arc_act_access_to_object : '||l_access_rec.arc_act_access_to_object);
12805          END IF;
12806 
12807          ams_access_pvt.create_access ( p_api_version => l_api_version
12808                                       , p_init_msg_list => fnd_api.g_false
12809                                       , p_validation_level => fnd_api.g_valid_level_full
12810                                       , x_return_status => x_return_status
12811                                       , x_msg_count => x_msg_count
12812                                       , x_msg_data => x_msg_data
12813                                       , p_commit => fnd_api.g_false
12814                                       , p_access_rec => l_access_rec
12815                                       , x_access_id => l_access_id
12816                                       );
12817        END LOOP;
12818        CLOSE csr_access;
12819 
12820        l_rec_num := l_rec_num + 1;
12821 
12822     END LOOP;
12823   ELSE
12824       --Raise error, if no accrual exists, may happen for INTERNAL claims only
12825       IF FND_MSG_PUB.Check_Msg_Level (FND_MSG_PUB.G_MSG_LVL_ERROR) THEN
12826         FND_MESSAGE.Set_Name('OZF', 'OZF_ACCRUAL_NOT_EXISTS');
12827         FND_MSG_PUB.ADD;
12828       END IF;
12829       RAISE FND_API.g_exc_unexpected_error;
12830   END IF;
12831 
12832   ------------------------- finish -------------------------------
12833   -- Check for commit
12834   IF FND_API.to_boolean(p_commit) THEN
12835     COMMIT;
12836   END IF;
12837 
12838   FND_MSG_PUB.count_and_get(
12839          p_encoded => FND_API.g_false,
12840          p_count   => x_msg_count,
12841          p_data    => x_msg_data
12842   );
12843 
12844   IF OZF_DEBUG_HIGH_ON THEN
12845      OZF_Utility_PVT.debug_message(l_full_name ||': end');
12846   END IF;
12847 
12848 EXCEPTION
12849   WHEN FND_API.g_exc_error THEN
12850     ROLLBACK TO Initiate_SD_Payment;
12851     x_return_status := FND_API.g_ret_sts_error;
12852     FND_MSG_PUB.count_and_get (
12853            p_encoded => FND_API.g_false
12854           ,p_count   => x_msg_count
12855           ,p_data    => x_msg_data
12856     );
12857 
12858   WHEN FND_API.g_exc_unexpected_error THEN
12859     ROLLBACK TO Initiate_SD_Payment;
12860     x_return_status := FND_API.g_ret_sts_unexp_error ;
12861     FND_MSG_PUB.count_and_get (
12862            p_encoded => FND_API.g_false
12863           ,p_count   => x_msg_count
12864           ,p_data    => x_msg_data
12865     );
12866 
12867   WHEN OTHERS THEN
12868     ROLLBACK TO Initiate_SD_Payment;
12869     x_return_status := FND_API.g_ret_sts_unexp_error ;
12870     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
12871       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
12872     END IF;
12873     FND_MSG_PUB.count_and_get(
12874            p_encoded => FND_API.g_false
12875           ,p_count   => x_msg_count
12876           ,p_data    => x_msg_data
12877     );
12878 
12879 END Initiate_SD_Payment;
12880 
12881 ---------------------------------------------------------------------
12882 -- PROCEDURE
12883 --    Accruals_Association
12884 --
12885 -- PURPOSE
12886 --    Earning Association for the claim.
12887 --
12888 -- PARAMETERS
12889 --    p_claim_id:
12890 --    p_funds_util_flt:
12891 --    p_summary_view:
12892 --
12893 -- NOTES
12894 -- HISTORY
12895 --   10-FEB-2011  kpatro  Created.
12896 ---------------------------------------------------------------------
12897 PROCEDURE Accruals_Association(
12898    p_api_version         IN  NUMBER
12899   ,p_init_msg_list       IN  VARCHAR2  := FND_API.g_false
12900   ,p_commit              IN  VARCHAR2  := FND_API.g_false
12901   ,p_validation_level    IN  NUMBER    := FND_API.g_valid_level_full
12902 
12903   ,x_return_status       OUT NOCOPY VARCHAR2
12904   ,x_msg_count           OUT NOCOPY NUMBER
12905   ,x_msg_data            OUT NOCOPY VARCHAR2
12906 
12907   ,p_claim_id            IN  NUMBER
12908   ,p_funds_util_flt      IN  funds_util_flt_type
12909   ,p_summary_view        IN  VARCHAR2
12910 )
12911 IS
12912 l_api_version CONSTANT NUMBER       := 1.0;
12913 l_api_name    CONSTANT VARCHAR2(30) := 'Accruals_Association';
12914 l_full_name   CONSTANT VARCHAR2(60) := g_pkg_name ||'.'|| l_api_name;
12915 l_return_status              VARCHAR2(1);
12916 l_error_index                NUMBER;
12917 l_created_from               VARCHAR2(30);
12918 l_old_claim_line_id          NUMBER;
12919 l_line_object_version_number NUMBER;
12920 l_ind                        NUMBER :=1;
12921 l_msg_count                  NUMBER;
12922 l_msg_data                   VARCHAR2(2000);
12923 
12924 l_funds_util_flt     funds_util_flt_type := p_funds_util_flt;
12925 l_line_tbl           OZF_CLAIM_LINE_PVT.claim_line_tbl_type;
12926 l_claim_line_tbl  OZF_Claim_Line_PVT.claim_line_tbl_type;
12927 
12928 
12929 CURSOR csr_claim_header(cv_claim_id IN NUMBER) IS
12930   SELECT created_from
12931   FROM ozf_claims_all
12932   WHERE claim_id = cv_claim_id;
12933 
12934 CURSOR claim_line_id_csr(p_id in number) IS
12935   SELECT claim_line_id, object_version_number
12936   FROM ozf_claim_lines_all
12937   WHERE claim_id = p_id
12938   AND (earnings_associated_flag IS NULL
12939   OR earnings_associated_flag ='F');
12940 
12941 
12942 
12943 BEGIN
12944   --------------------- initialize -----------------------
12945   SAVEPOINT Accruals_Association;
12946 
12947   IF OZF_DEBUG_HIGH_ON THEN
12948      OZF_Utility_PVT.debug_message(l_full_name||': start');
12949   END IF;
12950 
12951   IF FND_API.to_boolean(p_init_msg_list) THEN
12952     FND_MSG_PUB.initialize;
12953   END IF;
12954 
12955   IF NOT FND_API.compatible_api_call(
12956          l_api_version,
12957          p_api_version,
12958          l_api_name,
12959          g_pkg_name
12960   ) THEN
12961     RAISE FND_API.g_exc_unexpected_error;
12962   END IF;
12963 
12964   x_return_status := FND_API.g_ret_sts_success;
12965 
12966   ----------------- start ----------------
12967 
12968   OPEN csr_claim_header(p_claim_id);
12969   FETCH csr_claim_header into l_created_from;
12970   CLOSE csr_claim_header;
12971 
12972   IF ((l_funds_util_flt.total_amount IS NULL OR l_funds_util_flt.total_amount = 0) AND l_funds_util_flt.offer_type <>'SCAN_DATA') THEN
12973      RETURN;
12974   END IF;
12975 
12976   -- Delete the Non Promotional Line If any before association
12977      OPEN claim_line_id_csr(p_claim_id);
12978         LOOP
12979           FETCH claim_line_id_csr into l_old_claim_line_id, l_line_object_version_number;
12980           EXIT when claim_line_id_csr%NOTFOUND;
12981             l_claim_line_tbl(l_ind).claim_line_id := l_old_claim_line_id;
12982             l_claim_line_tbl(l_ind).object_version_number := l_line_object_version_number;
12983             l_ind := l_ind +1;
12984            END LOOP;
12985       CLOSE claim_line_id_csr;
12986 
12987        IF(l_claim_line_tbl.COUNT > 0 ) THEN
12988             OZF_Utility_PVT.debug_message('Deleting the Non promotional Lines');
12989             OZF_Claim_Line_PVT.Delete_Claim_Line_Tbl(
12990                p_api_version            => l_api_version
12991               ,p_init_msg_list          => FND_API.g_false
12992               ,p_commit                 => FND_API.g_false
12993               ,p_validation_level       => FND_API.g_valid_level_full
12994               ,x_return_status          => l_return_status
12995               ,x_msg_count              => l_msg_count
12996               ,x_msg_data               => l_msg_data
12997               ,p_claim_line_tbl         => l_claim_line_tbl
12998               ,p_change_object_version  => FND_API.g_false
12999               ,x_error_index            => l_error_index
13000             );
13001             IF l_return_status = FND_API.g_ret_sts_error THEN
13002                RAISE FND_API.g_exc_unexpected_error;
13003             ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
13004                RAISE FND_API.g_exc_unexpected_error;
13005             END IF;
13006          END IF; -- End of delete claim line
13007 
13008 
13009 
13010 
13011   IF(l_funds_util_flt.created_from = 'ASSOUI' AND (l_funds_util_flt.offer_type <>'SCAN_DATA' OR (l_funds_util_flt.offer_type IS NULL
13012 						   AND p_summary_view IN ('PERIOD','PRODUCT','ORDER'))))
13013   THEN
13014   --Construct the claim line record
13015   l_line_tbl(1).claim_id                := p_claim_id;
13016   l_line_tbl(1).claim_currency_amount   := l_funds_util_flt.total_amount;
13017   l_line_tbl(1).prorate_earnings_flag   := 'T';
13018   l_line_tbl(1).PERFORMANCE_COMPLETE_FLAG   := 'F';
13019   l_line_tbl(1).PERFORMANCE_ATTACHED_FLAG   := 'F';
13020   l_line_tbl(1).SELECT_CUST_CHILDREN_FLAG   := 'F';
13021 
13022    -- Create Claim Line
13023     OZF_CLAIM_LINE_PVT.Create_Claim_Line_Tbl(
13024        p_api_version       => 1.0
13025       ,p_init_msg_list     => FND_API.g_false
13026       ,p_commit            => FND_API.g_false
13027       ,p_validation_level  => FND_API.g_valid_level_full
13028       ,x_return_status     => l_return_status
13029       ,x_msg_count         => x_msg_count
13030       ,x_msg_data          => x_msg_data
13031       ,p_claim_line_tbl    => l_line_tbl
13032       ,x_error_index       => l_error_index
13033     );
13034 
13035     IF l_return_status = FND_API.g_ret_sts_error THEN
13036       RAISE FND_API.g_exc_error;
13037     ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
13038       RAISE FND_API.g_exc_error;
13039     END IF;
13040 
13041     l_funds_util_flt.claim_line_id := l_line_tbl(1).claim_line_id;
13042    END IF;
13043 
13044       -- Associate the earning to claim line
13045       Update_Group_Line_Util(
13046          p_api_version         => 1.0
13047         ,p_init_msg_list       => FND_API.g_false
13048         ,p_commit              => FND_API.g_false
13049         ,p_validation_level    => FND_API.G_VALID_LEVEL_FULL
13050         ,x_return_status       => l_return_status
13051         ,x_msg_count           => x_msg_count
13052         ,x_msg_data            => x_msg_data
13053         ,p_summary_view        => p_summary_view
13054         ,p_funds_util_flt      => l_funds_util_flt
13055       );
13056 
13057       IF l_return_status = FND_API.g_ret_sts_error THEN
13058         RAISE FND_API.g_exc_error;
13059       ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
13060         RAISE FND_API.g_exc_error;
13061       END IF;
13062       -- Auto split the Claim Line if there is multiple product associated with the same offer
13063       OZF_Claim_Line_PVT.split_claim_line
13064       (
13065          p_api_version         => 1.0
13066         ,p_init_msg_list       => FND_API.g_false
13067         ,p_commit              => FND_API.g_false
13068         ,p_validation_level    => FND_API.G_VALID_LEVEL_FULL
13069         ,x_return_status       => l_return_status
13070         ,x_msg_count           => x_msg_count
13071         ,x_msg_data            => x_msg_data
13072         ,p_claim_line_id       => l_funds_util_flt.claim_line_id
13073       );
13074          IF l_return_status = FND_API.g_ret_sts_error THEN
13075         RAISE FND_API.g_exc_error;
13076       ELSIF l_return_status = FND_API.g_ret_sts_unexp_error THEN
13077         RAISE FND_API.g_exc_error;
13078       END IF;
13079 
13080 
13081   ------------------------- finish -------------------------------
13082   -- Check for commit
13083   IF FND_API.to_boolean(p_commit) THEN
13084     COMMIT;
13085   END IF;
13086 
13087   FND_MSG_PUB.count_and_get(
13088          p_encoded => FND_API.g_false,
13089          p_count   => x_msg_count,
13090          p_data    => x_msg_data
13091   );
13092 
13093   IF OZF_DEBUG_HIGH_ON THEN
13094      OZF_Utility_PVT.debug_message(l_full_name ||': end');
13095   END IF;
13096 
13097 EXCEPTION
13098   WHEN FND_API.g_exc_error THEN
13099     ROLLBACK TO Accruals_Association;
13100     x_return_status := FND_API.g_ret_sts_error;
13101     FND_MSG_PUB.count_and_get (
13102            p_encoded => FND_API.g_false
13103           ,p_count   => x_msg_count
13104           ,p_data    => x_msg_data
13105     );
13106 
13107   WHEN FND_API.g_exc_unexpected_error THEN
13108     ROLLBACK TO Accruals_Association;
13109     x_return_status := FND_API.g_ret_sts_unexp_error ;
13110     FND_MSG_PUB.count_and_get (
13111            p_encoded => FND_API.g_false
13112           ,p_count   => x_msg_count
13113           ,p_data    => x_msg_data
13114     );
13115 
13116   WHEN OTHERS THEN
13117     ROLLBACK TO Accruals_Association;
13118     x_return_status := FND_API.g_ret_sts_unexp_error ;
13119     IF FND_MSG_PUB.check_msg_level(FND_MSG_PUB.g_msg_lvl_unexp_error) THEN
13120       FND_MSG_PUB.add_exc_msg(g_pkg_name, l_api_name);
13121     END IF;
13122     FND_MSG_PUB.count_and_get(
13123            p_encoded => FND_API.g_false
13124           ,p_count   => x_msg_count
13125           ,p_data    => x_msg_data
13126     );
13127 
13128 END Accruals_Association;
13129 
13130 END OZF_Claim_Accrual_PVT;